Re: [PATCH RFC] gcc-git: Add prepare-commit-msg hook.

2020-05-27 Thread Martin Liška

On 5/26/20 8:02 PM, Jason Merrill wrote:

Maybe these environment variables should start with GCC_GIT, not just GCC, to 
give more indication what they're for.


All right, I renamed it and pushed to master.

Martin


Re: [PATCH RFC] gcc-git: Add prepare-commit-msg hook.

2020-05-26 Thread Jason Merrill via Gcc-patches

On 5/26/20 3:12 AM, Martin Liška wrote:

Hi.

There's a small improvement. When filling up the ChangeLog entries,
I see it handy to open a patch in a separate window. I would use
GCC_DIFF_FILE env. variable for saving of the diff file.


It seems easy enough to execute the command yourself in another window, 
but I'm not opposed to the change.


Maybe these environment variables should start with GCC_GIT, not just 
GCC, to give more indication what they're for.


Jason



Re: [PATCH RFC] gcc-git: Add prepare-commit-msg hook.

2020-05-26 Thread Martin Liška

Hi.

There's a small improvement. When filling up the ChangeLog entries,
I see it handy to open a patch in a separate window. I would use
GCC_DIFF_FILE env. variable for saving of the diff file.

Thoughts?
Martin
>From bffc1bdd709b5152f290febb334b009c2f225872 Mon Sep 17 00:00:00 2001
From: Martin Liska 
Date: Tue, 26 May 2020 09:08:31 +0200
Subject: [PATCH] prepare-commit-msg: Save diff if requested by user.

contrib/ChangeLog:

	* prepare-commit-msg: Save diff to a file if
	the GCC_DIFF_FILE variable is set.
---
 contrib/prepare-commit-msg | 5 +
 1 file changed, 5 insertions(+)

diff --git a/contrib/prepare-commit-msg b/contrib/prepare-commit-msg
index 820dacc7e42..b2123317308 100755
--- a/contrib/prepare-commit-msg
+++ b/contrib/prepare-commit-msg
@@ -54,4 +54,9 @@ else
 exit 0
 fi
 
+# Save diff to a file if requested.
+if ! [ -z "$GCC_DIFF_FILE" ]; then
+  git $cmd > "$GCC_DIFF_FILE";
+fi
+
 git $cmd | git gcc-mklog -c "$COMMIT_MSG_FILE"
-- 
2.26.2



Re: [PATCH RFC] gcc-git: Add prepare-commit-msg hook.

2020-05-26 Thread Martin Liška

On 5/25/20 11:59 PM, Jason Merrill wrote:

On 5/25/20 4:15 PM, Martin Liška wrote:

On 5/25/20 10:08 PM, Jason Merrill wrote:

Like so:


I like the patch, thanks!

The last question I have: Do we want to make installation of the hook
optional in gcc-git-customization.sh?


Sure:



Thanks, I've just installed your patch and I'm starting testing of it.

Martin


Re: [PATCH RFC] gcc-git: Add prepare-commit-msg hook.

2020-05-25 Thread Jason Merrill via Gcc-patches

On 5/25/20 4:15 PM, Martin Liška wrote:

On 5/25/20 10:08 PM, Jason Merrill wrote:

Like so:


I like the patch, thanks!

The last question I have: Do we want to make installation of the hook
optional in gcc-git-customization.sh?


Sure:

>From 04429ee04769e15842093f154e8c7887f75d9476 Mon Sep 17 00:00:00 2001
From: Jason Merrill 
Date: Fri, 22 May 2020 18:40:35 -0400
Subject: [PATCH] gcc-git: Add prepare-commit-msg hook.
To: gcc-patches@gcc.gnu.org

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 | 13 
 contrib/mklog.py | 23 -
 contrib/prepare-commit-msg   | 57 
 3 files changed, 92 insertions(+), 1 deletion(-)
 create mode 100755 contrib/prepare-commit-msg

diff --git a/contrib/gcc-git-customization.sh b/contrib/gcc-git-customization.sh
index 7a950ae5f38..dcc42683fa6 100755
--- a/contrib/gcc-git-customization.sh
+++ b/contrib/gcc-git-customization.sh
@@ -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
 # *.mddiff=md
@@ -127,6 +129,17 @@ echo "(local branches starting / 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")
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 100755
index 000..820dacc7e42
--- /dev/null
+++ b/contrib/prepare-commit-msg
@@ -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 

Re: [PATCH RFC] gcc-git: Add prepare-commit-msg hook.

2020-05-25 Thread Martin Liška

On 5/25/20 10:08 PM, Jason Merrill wrote:

Like so:


I like the patch, thanks!

The last question I have: Do we want to make installation of the hook
optional in gcc-git-customization.sh?

Martin


Re: [PATCH RFC] gcc-git: Add prepare-commit-msg hook.

2020-05-25 Thread Jason Merrill via Gcc-patches
On Mon, May 25, 2020 at 3:50 PM Jason Merrill  wrote:
> On Mon, May 25, 2020 at 2:14 PM Martin Liška  wrote:
> >
> > 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?
>
> I'll integrate your changes with mine.

Like so:
From 47424e02f3d7c5e01e292c43e0d5f5d932178d31 Mon Sep 17 00:00:00 2001
From: Jason Merrill 
Date: Fri, 22 May 2020 18:40:35 -0400
Subject: [PATCH] gcc-git: Add prepare-commit-msg hook.
To: gcc-patches@gcc.gnu.org

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 |  5 +++
 contrib/mklog.py | 23 -
 contrib/prepare-commit-msg   | 57 
 3 files changed, 84 insertions(+), 1 deletion(-)
 create mode 100755 contrib/prepare-commit-msg

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
 # *.mddiff=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 100755
index 000..820dacc7e42
--- /dev/null
+++ b/contrib/prepare-commit-msg
@@ -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 

Re: [PATCH RFC] gcc-git: Add prepare-commit-msg hook.

2020-05-25 Thread Jason Merrill via Gcc-patches
I'll integrate your changes with mine.

On Mon, May 25, 2020 at 2:14 PM Martin Liška  wrote:
>
> 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



Re: [PATCH RFC] gcc-git: Add prepare-commit-msg hook.

2020-05-25 Thread Martin Liška

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 
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
 # *.mddiff=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 000..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