contrib: Make prepare-commit-msg hook smarter for amends
authorJonathan Wakely <jwakely@redhat.com>
Thu, 11 Jun 2020 19:22:17 +0000 (20:22 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 11 Jun 2020 19:22:33 +0000 (20:22 +0100)
With this change the prepare-commit-msg hook can compare the log of a
commit being amended with the staged changes, and not run mklog.py
unnecessarily. This is controlled by a git config option,
gcc-config.mklog-hook-type.

contrib/ChangeLog:

* prepare-commit-msg: Use the gcc-config.mklog-hook-type Git
config key instead of the GCC_FORCE_MKLOG environment variable.
Optionally disable generating a new ChangeLog template for
amended commits when the existing log is still OK.

contrib/prepare-commit-msg

index cc9ba2e6ba1dadde6496934441297c06d5ec7906..969847df6f4f6eeaccb89faa862b29f6cbe2c948 100755 (executable)
@@ -49,6 +49,19 @@ elif [ $COMMIT_SOURCE = commit ]; then
     # otherwise, assume a new commit with -C.
     if [ $SHA1 = HEAD ]; then
        cmd="diff --cached HEAD^"
+       if [ "$(git config gcc-config.mklog-hook-type)" = "smart-amend" ]; then
+           # Check if the existing message still describes the staged changes.
+           f=$(mktemp /tmp/git-commit.XXXXXX) || exit 1
+           git log -1 --pretty=email HEAD > $f
+           printf '\n---\n\n' >> $f
+           git $cmd >> $f
+           if contrib/gcc-changelog/git_email.py "$f" >/dev/null 2>&1; then
+               # Existing commit message is still OK for amended commit.
+               rm $f
+               exit 0
+           fi
+           rm $f
+       fi
     else
        cmd="diff --cached"
     fi