Skip to content

Check first_commit_message against origin/master

John McDonnell requested to merge jmd/update-changelog-on-first-commit into master

What does this MR do and why?

This is a follow on to !1286 (merged)

I experience a situation where the changelog-on-first-commit check failed as I hadn't recently fetched the lastest ref to master, but I had done a git rebase origin master (which updates the remote ref to master) so this check was in place, but my local ref ref/heads/master was still pointing at an old ref.

I could choose to manually resolve this by doing a git fetch origin master:master but as this is an automated check, it seems reasonable to consider doing this automatically as part of the lefthook script to avoid any unexpected oddities as we presumably do want the check to run against what origin/master actually is, rather than what our local working directory might be pointing too.

How to set up and validate locally

# Assume someone hasn't done a git fetch recently to update their refs to master origin/master
git update-ref refs/heads/master efc97fcbe31fd38b85a64feaf946a25aa856b81c
git update-ref refs/remotes/origin/master efc97fcbe31fd38b85a64feaf946a25aa856b81c

# Current lefthook command will evaluate these commits
git log origin/master..HEAD --oneline
git log master..HEAD --oneline

# This actually evaluates to the oldest commit, i.e. what is in my outdated ref to master
first_commit_message=$(git log --format=%B -n 1 $(git log master..HEAD --pretty=format:"%h" | tail -1))
echo $first_commit_message

# using head doesn't work, because if we have multiple commits locally it would take the latest one, which isn't the one we want to use for evaluating
touch foobar && git add foobar && git commit -m "I am foobar"
first_commit_message=$(git log --format=%B -n 1 $(git log master..HEAD --pretty=format:"%h" | head -1))
echo $first_commit_message

# what I think we actually want is to update the refs to master or origin/master and compare against it
git fetch origin master
first_commit_message=$(git log --format=%B -n 1 $(git log origin/master..HEAD --pretty=format:"%h" | tail -1))
echo $first_commit_message

# An alternative solution might be updating our local ref to master `git fetch origin master:master` 
which would allow us to continue to use `git log master..HEAD` but (with no strong opinion) I prefer
the idea of updating the remote ref rather than the local one.  
# However my opinion is that it seems more obvious when reading the script to update the ref to origin/master

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by John McDonnell

Merge request reports