The sample hook to prepare the commit message before a commit allows users to opt-in to add the signature to the commit message. The signature is added at a place that isn't consistent with the "-s" option of "git commit". Further, it could go out of view in certain cases. Add the signature in a way similar to "-s" option of "git commit" using git's interpret-trailers command. It works well in all cases except when the user invokes "git commit" without any arguments. In that case manually add a new line after the first line to ensure it's consistent with the output of "-s" option. While at it, name the input parameters to improve readability of script. Signed-off-by: Kaartic Sivaraam <kaarticsivaraam91196@xxxxxxxxx> --- I've tried the various commands that I could think of and it seems to work well. I guess it's safe to use. templates/hooks--prepare-commit-msg.sample | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/templates/hooks--prepare-commit-msg.sample b/templates/hooks--prepare-commit-msg.sample index 86b8f227e..c44a0a056 100755 --- a/templates/hooks--prepare-commit-msg.sample +++ b/templates/hooks--prepare-commit-msg.sample @@ -20,17 +20,27 @@ # The third example adds a Signed-off-by line to the message, that can # still be edited. This is rarely a good idea. -case "$2,$3" in +COMMIT_MSG_FILE=$1 +COMMIT_SOURCE=$2 +SHA1=$3 +NEW_LINE='\ +' + +case "$COMMIT_SOURCE,$SHA1" in merge,) - @PERL_PATH@ -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;; + @PERL_PATH@ -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$COMMIT_MSG_FILE" ;; # ,|template,) # @PERL_PATH@ -i.bak -pe ' # print "\n" . `git diff --cached --name-status -r` -# if /^#/ && $first++ == 0' "$1" ;; +# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;; *) ;; esac # SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') -# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" +# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE" +# if [ -z "$COMMIT_SOURCE" ] +# then +# sed -i "1i$NEW_LINE" "$COMMIT_MSG_FILE" +# fi -- 2.11.0