From 32517390e8614a50e5e37acd46ea531ad1f0e267 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Wed, 4 Nov 2020 12:18:24 +0100 Subject: [PATCH] git hooks: commit msg: allow to opt in for auto-sign-off Just the subset when author and the committer is the same. Signed-off-by: Miklos Vajna Change-Id: Icff594a6a82bd0395e31f8e46cb56cb0e046e638 --- .git-hooks/README | 11 +++++++++++ .git-hooks/commit-msg | 23 +++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 .git-hooks/README diff --git a/.git-hooks/README b/.git-hooks/README new file mode 100644 index 0000000000..992de439c9 --- /dev/null +++ b/.git-hooks/README @@ -0,0 +1,11 @@ +commit-msg +---------- + +This hooks supports the `commit.signOff` option. + +This is a boolean value which lets you enable the -s/--signoff option of +commit by default. Note: Adding the Signed-off-by: line to a commit should be +a conscious act and means that you certify you have the rights to submit this +work under the same open source license. Please see the README.CONTRIBUTING.md +document for further discussion. The option is ignored if the author and the +committer is not the same. diff --git a/.git-hooks/commit-msg b/.git-hooks/commit-msg index cd7bc4e643..d3e3f871ee 100755 --- a/.git-hooks/commit-msg +++ b/.git-hooks/commit-msg @@ -28,9 +28,26 @@ if test ! -f "$1" ; then exit 1 fi +# $RANDOM will be undefined if not using bash, so don't use set -u +random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin) +dest="$1.tmp.${random}" + +trap 'rm -f "${dest}"' EXIT + +GIT_AUTHOR="$(git var GIT_AUTHOR_IDENT | sed 's/^\(.*>\).*$/\1/')" +GIT_COMMITTER="$(git var GIT_COMMITTER_IDENT | sed 's/^\(.*>\).*$/\1/')" +git config --bool --get commit.signOff >/dev/null +if [ $? = 0 -a "${GIT_AUTHOR}" = "${GIT_COMMITTER}" ]; then + SOB="Signed-off-by: ${GIT_AUTHOR}" + git -c trailer.ifexists=doNothing interpret-trailers \ + --trailer "${SOB}" < "$1" > "${dest}" + mv "${dest}" "$1" +fi + if ! grep -q "^Signed-off-by:" "$1"; then echo "Commit message is not signed off: $1" echo "Use 'git commit -s' to sign off the commit message." + echo "Use 'git config commit.signOff true' to automatically sign off commits." exit 1 fi @@ -39,12 +56,6 @@ if test "false" = "`git config --bool --get gerrit.createChangeId`" ; then exit 0 fi -# $RANDOM will be undefined if not using bash, so don't use set -u -random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin) -dest="$1.tmp.${random}" - -trap 'rm -f "${dest}"' EXIT - if ! git stripspace --strip-comments < "$1" > "${dest}" ; then echo "cannot strip comments from $1" exit 1