Skip to contentSkip to conversation

hooks/pre-commit: add example to add Signed-off-by line to message

PATCH In review Johannes Schindelin 3 participants 11 messages Jun 15, 2016 – Aug 11, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hooks/pre-commit: add example to add Signed-off-by line to message

It is good to have an example of how to supply a template for the commit message anyway.

---
 templates/hooks--pre-commit |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
templates/hooks--pre-commit +7 -0
index 723a9ef..931f127 100644
@@ -7,6 +7,13 @@
 #
 # To enable this hook, make this file executable.
 
+# Uncomment this to add a Signed-off-by line to the message.
+
+# . git-sh-setup
+# git var GIT_AUTHOR_IDENT | \
+# sed -n "s/^\(.*\) [0-9]\+ [-+][0-9]\+$/Signed-off-by: \1/p" \
+# > $GIT_DIR/SQUASH_MSG
+
 # This is slightly modified from Andrew Morton's Perfect Patch.
 # Lines you introduce should not have trailing whitespace.
 # Also check for an indentation that has SP before a TAB.
Signature · 1 line
1.4.4.1.gb475
#1 lore raw

PATCH 1/1 hooks/pre-commit: add example to add Signed-off-by line to message

Based on Johannes Schindelin's earlier patch to perform the same function.

The call to git-sh-setup was causing "Not a git repository" errors after the pre-commit script had run. I removed the call and used $(git-rev-parse --git-dir) instead, which seems to have fixed the problem.

---
 templates/hooks--pre-commit |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
templates/hooks--pre-commit +5 -0
index 723a9ef..87d9ccc 100644
@@ -7,6 +7,11 @@
 #
 # To enable this hook, make this file executable.
 
+# Uncomment the below to add a Signed-off-by line to the message.
+#git var GIT_AUTHOR_IDENT | \
+#sed -n "s/^\(.*\) [0-9]\+ [-+][0-9]\+$/Signed-off-by: \1/p" \
+# >> $(git-rev-parse --git-dir)/SQUASH_MSG
+
 # This is slightly modified from Andrew Morton's Perfect Patch.
 # Lines you introduce should not have trailing whitespace.
 # Also check for an indentation that has SP before a TAB.
Signature · 1 line
1.4.4.2.g120e3
#4 lore raw

Hi,

Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Johannes Schindelin

Hi,

Quoted 1 line · Andy Parkins
Quoted 7 lines
Quoted 4 lines

Ciao,
Dscho

Why SQUASH_MSG? Doesn't it confuse git-explain when it wants to detect that you are in the middle of a squash merge?

Yes, it confuses git-explain. Which did not existed when I concocted the first (broken) version of this patch. There are only two ways to give commit message templates AFAICT: misusing SQUASH_MSG and misusing MERGE_MSG.

Ciao,
Dscho

#5 lore raw
Quoted 7 lines · Johannes Schindelin
Quoted 2 lines

Why SQUASH_MSG? Doesn't it confuse git-explain when it wants to detect that you are in the middle of a squash merge?

Yes, it confuses git-explain. Which did not existed when I concocted the first (broken) version of this patch. There are only two ways to give commit message templates AFAICT: misusing SQUASH_MSG and misusing MERGE_MSG.

I wonder...

# An example hook script to check the commit log message. # Called by git-commit with one argument, the name of the file

# that has the commit message.  The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit.  The hook is allowed to edit the commit message file.
#6 lore raw

PATCH 1/1 hooks/commit-msg: add example to add Signed-off-by line to message

After checking to see if the commit message already has the target signed-off-by (for example in --amend commits), this patch generates a signed off by line from the repository owner and adds it to the commit message.

Based on Johannes Schindelin's earlier patch to perform the same function.

Originally, this was done in the pre-commit hook but Junio pointed out that the commit-msg hook allows the message to be edited. This has the aditional advantage that the commit-msg hook gets passed the name of the message file as a parameter, so it doesn't have to figure out GIT_DIR for itself.

---
 templates/hooks--commit-msg |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
templates/hooks--commit-msg +5 -0
index 0b906ca..ce76bdf 100644
@@ -8,6 +8,11 @@
 #
 # To enable this hook, make this file executable.
 
+# Uncomment the below to add a Signed-off-by line to the message.
+#SOB=$(git var GIT_AUTHOR_IDENT | \
+#	sed -n "s/^\(.*\) [0-9]\+ [-+][0-9]\+$/Signed-off-by: \1/p")
+#grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
+
 # This example catches duplicate Signed-off-by lines.
 
 test "" = "$(grep '^Signed-off-by: ' "$1" |
Signature · 1 line
1.4.4.2.g120e3
#7 lore raw

Re: [PATCH] hooks/commit-msg: add example to add Signed-off-by line to message

templates/hooks--commit-msg line 14

Quoted 4 lines · Andy Parkins
templates/hooks--commit-msg +4 -0
+# Uncomment the below to add a Signed-off-by line to the message.
+#SOB=$(git var GIT_AUTHOR_IDENT | \
+#	sed -n "s/^\(.*\) [0-9]\+ [-+][0-9]\+$/Signed-off-by: \1/p")
+#grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

I suspect this sed script is quite wrong. Did you test it?

* quoting with dq means you are losing one level of backslash to the shell and sed is not seeing them.

* one-or-more regexp '+' is typically not found in sed.

Also if this is meant to also handle cases where somebody commits a patch received from an e-mail chain that multiple people signed off their own changes to the patch, your name may already appear earlier in the chain of existing Signed-off-by:
lines to sign off an earlier edition of the patch, but what you are committing now would include your own changes you just made on top of what all these people did, and you would want to sign off that new change as well, I think. So checking the last instance of the existing signed-off-by line, ignoring earlier ones, may be more preferrable.

#8 lore raw

Re: [PATCH] hooks/commit-msg: add example to add Signed-off-by line to message

Quoted 1 line · Junio C Hamano

I suspect this sed script is quite wrong. Did you test it?

I plead not guilty; cut and pasted from Johannes. In it's defence; I've been using it since he gave it to me without trouble. My patch is wrapping it a little differently, but the sed is certainly the same.

Amazingly it does work though...

Quoted 2 lines

* quoting with dq means you are losing one level of backslash to the shell and sed is not seeing them.

Unusual. It doesn't seem to apply to non-recognised escapes:

$ echo "\1\(\)\+$/"
\1\(\)\+$/
Quoted 1 line

* one-or-more regexp '+' is typically not found in sed.

Seems to be in mine (GNU sed version 4.1.5).

$ echo "xxxxxx" | sed -n 's/\(x\+\)$/MATCH/p'
MATCH
Quoted 3 lines

off that new change as well, I think. So checking the last instance of the existing signed-off-by line, ignoring earlier ones, may be more preferrable.

Accepted.

Notwithstanding the fact that it seems to be working for me; I'll fix the problems you highlight and re-patch.

Andy

Signature · 2 lines
Dr Andy Parkins, M Eng (hons), MIEE
[email protected]
#9 lore raw
In reply to Junio C Hamano

Hi,

Quoted 10 lines · Junio C Hamano
Quoted 7 lines · Johannes Schindelin
Quoted 2 lines

Why SQUASH_MSG? Doesn't it confuse git-explain when it wants to detect that you are in the middle of a squash merge?

Yes, it confuses git-explain. Which did not existed when I concocted the first (broken) version of this patch. There are only two ways to give commit message templates AFAICT: misusing SQUASH_MSG and misusing MERGE_MSG.

Quoted 7 lines

I wonder...

# An example hook script to check the commit log message. # Called by git-commit with one argument, the name of the file

# that has the commit message.  The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit.  The hook is allowed to edit the commit message file.

Yes, I missed that.

Ciao,
Dscho

#10 lore raw
In reply to Johannes Schindelin

templates/hooks--pre-commit line 15

Quoted 4 lines · Johannes Schindelin
templates/hooks--pre-commit +4 -0
+# . git-sh-setup
+# git var GIT_AUTHOR_IDENT | \
+# sed -n "s/^\(.*\) [0-9]\+ [-+][0-9]\+$/Signed-off-by: \1/p" \
+# > $GIT_DIR/SQUASH_MSG

I've been using this; and very useful it is too. However, I just noticed that I was getting "Not a git repository" errors when I tried to commit in a subdirectory. I traced the problem to my use of this fragment. It seems that calling git-sh-setup inside the pre-commit hook was changing GIT_DIR (don't know why). When I changed this to

git var GIT_AUTHOR_IDENT | \
sed -n "s/^\(.*\) [0-9]\+ [-+][0-9]\+$/Signed-off-by: \1/p" \

Quoted 1 line
Quoted 1 line

$(git-rev-parse --git-dir)/SQUASH_MSG

It worked correctly again.

Andy

Signature · 1 line
Dr Andy Parkins, M Eng (hons), MIEE