On Mon, May 25, 2020 at 5:23 AM Martin Liška <mli...@suse.cz> wrote:
>
> On 5/22/20 11:01 PM, Jason Merrill wrote:
> > On Thu, May 21, 2020 at 6:03 PM Jason Merrill <ja...@redhat.com> wrote:
> >>
> >> On Fri, May 15, 2020 at 11:39 AM Martin Liška <mli...@suse.cz> wrote:
> >>>
> >>> On 5/15/20 3:22 PM, Marek Polacek wrote:
> >>>> On Fri, May 15, 2020 at 03:12:27PM +0200, Martin Liška wrote:
> >>>>> On 5/15/20 2:42 PM, Marek Polacek wrote:
> >>>>>> I actually use mklog -i all the time.  But I can work around it if it
> >>>>>> disappears.
> >>>>>
> >>>>> Ah, I can see a consumer.
> >>>>> There's an updated version that supports that.
> >>>>>
> >>>>> For the future, will you still use the option? Wouldn't be better
> >>>>> to put the ChangeLog content directly to commit message? Note
> >>>>> that you won't have to copy the entries to a particular ChangeLog file.
> >>>>
> >>>> The way I do it is to generate a patch using format-patch, use mklog -i
> >>>> on it, then add the ChangeLog entry to the commit message via commit 
> >>>> --amend.
> >>>
> >>> Hmm, you can do much better with:
> >>>
> >>> $ git diff | ./contrib/mklog > changelog && git commit -a -t changelog
> >>>
> >>> Or for an already created commit you can do:
> >>>
> >>> $ git diff HEAD~ | ./contrib/mklog > changelog && git commit -a --amend 
> >>> -e -F changelog
> >>
> >> With these git aliases:
> >>
> >>          mklog-editor = "!f() { git show | git gcc-mklog >> $1; }; f"
> >>          addlog = "!f() { GIT_EDITOR='git mklog-editor' git commit 
> >> --amend; }; f"
> >>
> >> I can 'git addlog' to append the output of mklog to the current
> >> commit.  Probably better would be to do something with
> >> prepare-commit-msg.
> >
> > This is pretty rudimentary, but good enough as a start:
>
> I like the idea of usage of the prepare commit hook.
>
> >
> > #!/bin/sh
> >
> > #COMMIT_MSG_FILE=$1
> > #COMMIT_SOURCE=$2
> > #SHA1=$3
>
> It's better to use the named arguments.
>
> >
> > if ! [ -f "$1" ]; then exit 0; fi
> >
> > #echo "# $0 $1 $2 $3" >> $1
> >
> > if fgrep 'ChangeLog:' $1 > /dev/null 2>&1; then exit 0; fi
> >
> > if [ -z "$2" ]; then
> >      cmd="diff --cached"
> > elif [ $2 == commit ]; then
> >      cmd="show $3"
> > else
> >      exit 0
> > fi
> >
> > git $cmd | git gcc-mklog >> $1
> >
>
> Well, that will generate changelog entry for each commit.
> For a user branch development, it's not desirable.

It isn't that useful for intermediate commits, but I was thinking it
wasn't harmful either.  But I can remove the on-by-default aspects.

> What about more explicit approach:
>
> 1) making an alias for: git diff | git gcc-mklog > commit.msg
> 2) hook:
>
> if test -f commit.msg; then
>    cat commit.msg >> "$COMMIT_MSG_FILE"
>    rm commit.msg
> fi
>
> So the changelog is created explicitly and included implicitly.

How about this?
commit 787893dc41fb8288994a2350943c22e2388476e7
Author: Jason Merrill <ja...@redhat.com>
Date:   Fri May 22 18:40:35 2020 -0400

    gcc-git: Add prepare-commit-msg.
    
    This patch introduces a prepare-commit-msg hook that appends a ChangeLog
    skeleton to a commit message when the GCC_FORCE_MKLOG environment variable
    is set, and a 'git commit-mklog' command set that variable while running
    'git commit'.
    
    contrib/ChangeLog:
    
            * prepare-commit-msg: New file.
            * gcc-git-customization.sh: Install it.  Add commit-mklog alias.

diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh
index 7a950ae5f38..a3f7da8d20b 100755
--- a/contrib/gcc-git-customization.sh
+++ b/contrib/gcc-git-customization.sh
@@ -30,6 +30,11 @@ git config alias.gcc-backport '!f() { rev=$1; git cherry-pick -x $@; } ; f'
 
 git config alias.gcc-mklog '!f() { "`git rev-parse --show-toplevel`/contrib/mklog.py" $@; } ; f'
 
+hookdir=`git rev-parse --git-path hooks`
+install "`git rev-parse --show-toplevel`/contrib/prepare-commit-msg" "$hookdir"
+
+git config alias.commit-mklog '!f() { GCC_FORCE_MKLOG=1 git commit "$@"; }; f'
+
 # Make diff on MD files use "(define" as a function marker.
 # Use this in conjunction with a .gitattributes file containing
 # *.md    diff=md
diff --git a/contrib/prepare-commit-msg b/contrib/prepare-commit-msg
new file mode 100644
index 00000000000..b06080b927c
--- /dev/null
+++ b/contrib/prepare-commit-msg
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+COMMIT_MSG_FILE=$1
+COMMIT_SOURCE=$2
+SHA1=$3
+
+#echo "# $*" > $HOME/prepare-commit-msg-args
+
+# Can't do anything if $COMMIT_MSG_FILE isn't a file.
+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 "$COMMIT_SOURCE" ] || [ $COMMIT_SOURCE = template ]; then
+    # No source or "template" means new commit.
+    cmd="diff --cached"
+
+elif [ $COMMIT_SOURCE = message ]; then
+    # "message" means -m, assume a new commit if there are any changes staged.
+    if ! git diff --cached --quiet; then
+	cmd="diff --cached"
+    else
+	cmd="diff --cached HEAD^"
+    fi
+
+    # Add a blank line before the ChangeLog entries.
+    echo >> "$COMMIT_MSG_FILE"
+
+elif [ $COMMIT_SOURCE = commit ]; then
+    # The message of an existing commit.  If it's HEAD, assume --amend;
+    # otherwise, assume a new commit with -C.
+    if [ $SHA1 = HEAD ]; then
+	cmd="diff --cached HEAD^"
+    else
+	cmd="diff --cached"
+    fi
+else
+    # Do nothing for merge or squash.
+    exit 0
+fi
+
+git $cmd | git gcc-mklog >> "$COMMIT_MSG_FILE"

Reply via email to