On 10/06/20 09:43 +0200, Martin Liška wrote:
On 6/9/20 9:46 PM, Jonathan Wakely wrote:
Is this worth adding to contrib/prepare-commit-msg?

I like the idea and I would make it conditional based on
an environment variable? Similarly to GCC_GIT_DIFF_FILE?

With this patch you can use the gcc-config.use-mklog-hook config key
instead of defining GCC_FORCE_MKLOG in the environment.

My new behaviour is enabled when that variable is set to
'smart-amend' (either in the environment, or in your git config).

WDYT?

commit a58493ff8f3d36645c6d4fad2fca04e3db2d267c
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Wed Jun 10 12:48:40 2020 +0100

    contrib: Make prepare-commit-msg hook smarter
    
    With this change defining GCC_FORCE_MKLOG=smart-amend in the environment
    will cause the prepare-commit-msg hook to compare the log of a commit
    being amended with the staged changes, and not run mklog.py
    unnecessarily.
    
    This also allows defining gcc-config.use-mklog-hook in Git config
    instead of the GCC_FORCE_MKLOG environment variable, and allows
    GCC_FORCE_MKLOG=no to do nothing even if the config key is set.
    
    contrib/ChangeLog:
    
            * prepare-commit-msg: Do not generate a new ChangeLog template
            for amended commits where the existing log is still OK. Check
            the gcc-config.use-mklog-hook Git config key if the
            GCC_FORCE_MKLOG environment variable is empty.

diff --git a/contrib/prepare-commit-msg b/contrib/prepare-commit-msg
index 24f0783aae2..cfcf0e7afe5 100755
--- a/contrib/prepare-commit-msg
+++ b/contrib/prepare-commit-msg
@@ -30,7 +30,11 @@ if ! [ -x contrib/mklog.py ]; then exit 0; fi
 if ! [ -f "$COMMIT_MSG_FILE" ]; then exit 0; fi
 
 # Don't do anything unless requested to.
-if [ -z "$GCC_FORCE_MKLOG" ]; then exit 0; fi
+if [ -z "$GCC_FORCE_MKLOG" ]; then
+    GCC_FORCE_MKLOG=$(git config gcc-config.use-mklog-hook) || exit 0
+    echo xxx $GCC_FORCE_MKLOG
+fi
+if [ -z "$GCC_FORCE_MKLOG" ] || [ $GCC_FORCE_MKLOG = no ]; then exit 0; fi
 
 if [ -z "$COMMIT_SOURCE" ] || [ $COMMIT_SOURCE = template ]; then
     # No source or "template" means new commit.
@@ -49,6 +53,19 @@ elif [ $COMMIT_SOURCE = commit ]; then
     # otherwise, assume a new commit with -C.
     if [ $SHA1 = HEAD ]; then
 	cmd="diff --cached HEAD^"
+	if [ "$GCC_FORCE_MKLOG" = "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

Reply via email to