On 11/06/20 10:54 +0200, Martin Liška wrote:
On 6/11/20 10:48 AM, Jonathan Wakely wrote:
On 11/06/20 09:54 +0200, Martin Liška wrote:
On 6/10/20 2:20 PM, Jonathan Wakely wrote:
Oops, this line was left in while I was testing it by amending
existing commits!

Here's an updated patch without that line.

I generally like the suggested idea and I have suggestion to usage of the 
GCC_FORCE_MKLOG.
Right now, we use the env variable only in 'git config alias.gcc-commit-mklog' 
alias.

Ah yes, I forgot about that alias (I don't use it).

I use it (and I want to distinguish in between it and normal commit command) ;)

OK. I'd rather just have one command that always does the right thing,
so want the hook to always run. But I can just change my local copy.

Wouldn't it be better to only add

git config gcc-config.mklog-hook-type [always|smart-amend]

and do not support GCC_FORCE_MKLOG=no or GCC_FORCE_MKLOG=smart-amend?

The point of GCC_FORCE_MKLOG=no was as an override to disable the hook
if you have it enabled in your git config options.

If my git config enables the hook, but I want it off for a single
commit, I can use the env var to disable it.

For your scenario, you want the hook off by default, but enabled when
the env var is set (by the gcc-commit-mklog alias).

Sure, that works for me.

Or do you have always GCC_FORCE_MKLOG=1 on (I mean for git commit)?

No, I was actually using a modified version of the script that didn't
check the env var at all, because I always wanted it to be used.

I bet some other people (including me) want to have 2 different commands.


I'll prepare a patch that replaces the env var completely, and also
modifies the contrib/gcc-git-customization.sh script to optionally
enable the hook.

I would preserve it for the alias.

OK, I misunderstood what you were suggesting.

Is the attached patch what you're asking for?

Martin


Do we want to drop the gcc-config.gcc-commit-mklog alias now, since it
won't do anything different from 'git commit'?



commit c0e9ad58b132d01a3d7bb7eaeb981324eb55074a
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Thu Jun 11 10:22:04 2020 +0100

    contrib: Make prepare-commit-msg hook smarter for amends
    
    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.

diff --git a/contrib/prepare-commit-msg b/contrib/prepare-commit-msg
index cc9ba2e6ba1..969847df6f4 100755
--- a/contrib/prepare-commit-msg
+++ b/contrib/prepare-commit-msg
@@ -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

Reply via email to