contrib: Make prepare-commit-msg hook safe for older branches

Message ID 20200602091605.GA3872557@redhat.com
State New
Headers show
Series
  • contrib: Make prepare-commit-msg hook safe for older branches
Related show

Commit Message

Kees Cook via Gcc-patches June 2, 2020, 9:16 a.m.
If a user installs this script as .git/hooks/prepare-commit-msg and then
works on an old branch which doesn't have the mklog.py script, trying to
commit will fail with an error like:

environment: /.../gcc/contrib/mklog.py: No such file or directory

This makes it exit cleanly so it's possible to commit.

contrib/ChangeLog:

	* prepare-commit-msg: Do nothing if the mklog.py script isn't
	present.

OK for master?
commit b4645b28abbed1c55e330071266f9e31a7816c28
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jun 2 10:10:19 2020 +0100

    contrib: Make prepare-commit-msg hook safe for older branches
    
    If a user installs this script as .git/hooks/prepare-commit-msg and then
    works on an old branch which doesn't have the mklog.py script, trying to
    commit will fail with an error like:
    
    environment: /.../gcc/contrib/mklog.py: No such file or directory
    
    This makes it exit cleanly so it's possible to commit.
    
    contrib/ChangeLog:
    
            * prepare-commit-msg: Do nothing if the mklog.py script isn't
            present.

Comments

Martin Liška June 2, 2020, 9:19 a.m. | #1
On 6/2/20 11:16 AM, Jonathan Wakely wrote:
> OK for master?


I like the patch.

Martin

Patch

diff --git a/contrib/prepare-commit-msg b/contrib/prepare-commit-msg
index fd59bfbcf88..24f0783aae2 100755
--- a/contrib/prepare-commit-msg
+++ b/contrib/prepare-commit-msg
@@ -23,6 +23,9 @@  COMMIT_MSG_FILE=$1
 COMMIT_SOURCE=$2
 SHA1=$3
 
+# We might be on a branch before the file was added.
+if ! [ -x contrib/mklog.py ]; then exit 0; fi
+
 # Can't do anything if $COMMIT_MSG_FILE isn't a file.
 if ! [ -f "$COMMIT_MSG_FILE" ]; then exit 0; fi