This patch introduces a prepare-commit-msg hook that appends a ChangeLog
skeleton to a commit message that doesn't already have one, and a 'git
amend-mklog' command to amend and append a new ChangeLog skeleton (to be
edited together with existing entries by hand).

Thoughts?

contrib/ChangeLog:

        * prepare-commit-msg: New file.
        * gcc-git-customization.sh: Install it.  Add amend-mklog alias.
---
 contrib/gcc-git-customization.sh |  5 ++++
 contrib/prepare-commit-msg       | 39 ++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 contrib/prepare-commit-msg

diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh
index 7a950ae5f38..a36403f0b2c 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.amend-mklog '!f() { GCC_PREPARE_COMMIT_FORCE_APPEND=1 git 
commit --amend "$@"; }; 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..61bc051268f
--- /dev/null
+++ b/contrib/prepare-commit-msg
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+#COMMIT_MSG_FILE=$1
+#COMMIT_SOURCE=$2
+#SHA1=$3
+
+#echo "# $*" > $HOME/prepare-commit-msg-args
+
+# Can't do anything if $1 isn't a file.
+if ! [ -f "$1" ]; then exit 0; fi
+
+# Don't mess with existing entries unless requested to.
+if [ -z "$GCC_PREPARE_COMMIT_FORCE_APPEND" ] &&
+       grep -qsF 'ChangeLog:' "$1"
+then exit 0; fi
+
+if [ -z "$2" ] || [ $2 == template ]; then
+    # No source or "template" means new commit.
+    cmd="diff --cached"
+elif [ $2 == message ]; then
+    # "message" means -m, but could be either a new commit or --amend.
+    # Guess which based on whether there are any changes staged.
+    if git diff --cached --quiet; then
+       cmd="show"
+    else
+       cmd="diff --cached"
+    fi
+
+    # Add a blank line before the ChangeLog entries.
+    echo >> "$1"
+elif [ $2 == commit ]; then
+    # The message of an existing commit.
+    cmd="show $3"
+else
+    # Do nothing for merge or squash.
+    exit 0
+fi
+
+git $cmd | git gcc-mklog >> "$1"

base-commit: 584d52b088f9fcf78704b504c3f1f07e17c1cded
-- 
2.18.1

Reply via email to