]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gcc-git: Add prepare-commit-msg hook.
authorJason Merrill <jason@redhat.com>
Fri, 22 May 2020 22:40:35 +0000 (18:40 -0400)
committerMartin Liska <mliska@suse.cz>
Tue, 26 May 2020 06:38:24 +0000 (08:38 +0200)
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.
* mklog.py: Add new option -c which appends
to a ChangeLog file.

contrib/gcc-git-customization.sh
contrib/mklog.py
contrib/prepare-commit-msg [new file with mode: 0755]

index 7a950ae5f38896c9b0219ab8dabeee62594c88b9..dcc42683fa66973f232aeca09a45d236a89ef832 100755 (executable)
@@ -30,6 +30,8 @@ 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'
 
+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
@@ -127,6 +129,17 @@ echo "(local branches starting <prefix>/ can be pushed directly to your"
 ask "personal area on the gcc server)" $old_pfx new_pfx
 git config "gcc-config.userpfx" "$new_pfx"
 
+echo
+ask "Install prepare-commit-msg git hook for 'git commit-mklog' alias" yes dohook
+if [ "x$dohook" = xyes ]; then
+    hookdir=`git rev-parse --git-path hooks`
+    if [ -f "$hookdir/prepare-commit-msg" ]; then
+       echo " Moving existing prepare-commit-msg hook to prepare-commit-msg.bak"
+       mv "$hookdir/prepare-commit-msg" "$hookdir/prepare-commit-msg.bak"
+    fi
+    install -c "`git rev-parse --show-toplevel`/contrib/prepare-commit-msg" "$hookdir"
+fi
+
 # Scan the existing settings to see if there are any we need to rewrite.
 vendors=$(git config --get-all "remote.${upstream}.fetch" "refs/vendors/" | sed -r "s:.*refs/vendors/([^/]+)/.*:\1:" | sort | uniq)
 url=$(git config --get "remote.${upstream}.url")
index 7a19b5d094902e65c51457cdef614fb7427dea02..fb58661b5ebb94521fd99dc59c47ccca5e2e00b8 100755 (executable)
@@ -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 100755 (executable)
index 0000000..820dacc
--- /dev/null
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+# Copyright (C) 2020 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING.  If not, write to
+# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+# Boston, MA 02110-1301, USA.
+
+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 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
+
+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 -c "$COMMIT_MSG_FILE"