On 5/23/20 11:39 PM, Jason Merrill via Gcc-patches wrote:
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).
As mentioned in the previous email, I prefer to make it opt-in, not opt-out.
So I'm suggesting the 'git commit-mklog' alias that add the skeleton to
a git commit message.
I also changed the hook to use named variables. And I would like to preserve
the ending '# Please enter the commit message for your changes. Lines starting'
section, so I use the mklog.py to put the skeleton to a proper place.
Example:
gcc/ChangeLog:
* ipa-icf.c (make_pass_ipa_icf):
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch me/add-prepare-commit-msg-hook
# Changes to be committed:
# modified: gcc/ipa-icf.c
#
Thoughts?
Martin
>From bbceb70bcf56d6feac9e7f90eefa06147417e4d0 Mon Sep 17 00:00:00 2001
From: Martin Liska <[email protected]>
Date: Mon, 25 May 2020 19:38:00 +0200
Subject: [PATCH] Add prepare-commit-msg hook/
contrib/ChangeLog:
* gcc-git-customization.sh: Install new hook.
* mklog.py: Add new option -c which append
to a ChangeLog file.
* prepare-commit-msg: New file.
---
contrib/gcc-git-customization.sh | 5 +++++
contrib/mklog.py | 23 ++++++++++++++++++++++-
contrib/prepare-commit-msg | 32 ++++++++++++++++++++++++++++++++
3 files changed, 59 insertions(+), 1 deletion(-)
create mode 100644 contrib/prepare-commit-msg
diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh
index 7a950ae5f38..110f661f5e5 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_PREPARE_COMMIT=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/mklog.py b/contrib/mklog.py
index 7a19b5d0949..fb58661b5eb 100755
--- a/contrib/mklog.py
+++ b/contrib/mklog.py
@@ -30,6 +30,7 @@ import argparse
import os
import re
import sys
+from itertools import takewhile
import requests
@@ -221,6 +222,9 @@ if __name__ == '__main__':
help='Do not generate function names in ChangeLogs')
parser.add_argument('-p', '--fill-up-bug-titles', action='store_true',
help='Download title of mentioned PRs')
+ parser.add_argument('-c', '--changelog',
+ help='Append the ChangeLog to a git commit message '
+ 'file')
args = parser.parse_args()
if args.input == '-':
args.input = None
@@ -229,4 +233,21 @@ if __name__ == '__main__':
data = input.read()
output = generate_changelog(data, args.no_functions,
args.fill_up_bug_titles)
- print(output, end='')
+ if args.changelog:
+ lines = open(args.changelog).read().split('\n')
+ start = list(takewhile(lambda l: not l.startswith('#'), lines))
+ end = lines[len(start):]
+ with open(args.changelog, 'w') as f:
+ if start:
+ # appent empty line
+ if start[-1] != '':
+ start.append('')
+ else:
+ # append 2 empty lines
+ start = 2 * ['']
+ f.write('\n'.join(start))
+ f.write('\n')
+ f.write(output)
+ f.write('\n'.join(end))
+ else:
+ print(output, end='')
diff --git a/contrib/prepare-commit-msg b/contrib/prepare-commit-msg
new file mode 100644
index 00000000000..a14b2ece042
--- /dev/null
+++ b/contrib/prepare-commit-msg
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+COMMIT_MSG_FILE=$1
+COMMIT_SOURCE=$2
+SHA1=$3
+
+# Can't do anything if $COMMIT_MSG_FILE isn't a file.
+if ! [ -f "$COMMIT_MSG_FILE" ]; then exit 0; fi
+
+# Don't mess with existing entries unless requested to.
+if [ -z "$GCC_PREPARE_COMMIT" ]; 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, 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
+elif [ $COMMIT_SOURCE == commit ]; then
+ # The message of an existing commit.
+ cmd="show $SHA1"
+else
+ # Do nothing for merge or squash.
+ exit 0
+fi
+
+git $cmd | git gcc-mklog -c "$COMMIT_MSG_FILE"
--
2.26.2