git/list[1] front-page[2] threads[3] people[4] search[5] about
 

"git intepret-trailers" vs. "sed script" to add the signature

From
Kaartic Sivaraam <kaarticsivaraam91196@gmail.com>
Date
Jul 1, 2017, 16:16 UTC
Message-ID
<1498925790.4321.7.camel@gmail.com>
In-Reply-To
<1498918546.4321.1.camel@gmail.com>
On Sat, 2017-07-01 at 19:45 +0530, Kaartic Sivaraam wrote:
Show 7 quoted lines
> On Fri, 2017-06-30 at 09:44 -0700, Junio C Hamano wrote:
> > It does look like a hack.  I was wondering if "interpret-trailers"
> > is mature enough and can be used for this by now.
> 
> It does look promising except for a few differences from the hook
> which
> I'll explain in the following mail.

interpet-trailers =================

After enabling the script I tried the following (shown here as a diff) to add the signature with "interpret-trailers",

    diff --git a/templates/hooks--prepare-commit-msg.sample b/templates/hooks--prepare-commit-msg.sample
    index 6473bcacd..9f8cbe7fd 100755
    --- a/templates/hooks--prepare-commit-msg.sample
    +++ b/templates/hooks--prepare-commit-msg.sample
    @@ -33,4 +33,4 @@ case "$2,$3" in
     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" "$1"
It adds the signature if it's not present in the following cases,
* commit
* merge
* commit --amend
* commit -F
* cherry-pick

It's pretty good in adding the signature except that it's not in line with "git commit -s" whose resulting "spacing" (new lines before and after) as shown in the editor is given below,

Show 7 quoted lines
> 
> 
> Signed-off-by: Test <hello@example.org>
> 
> # Please enter the commit message for your changes. Lines starting
> # with '#' will be ignored, and an empty message aborts the commit.
> ...

The spacing of "git interpret-trailers" in the editor for the relevant cases are,

commit ------

Show 5 quoted lines
> 
> Signed-off-by: Test <hello@example.org>
> # Please enter the commit message for your changes. Lines starting
> # with '#' will be ignored, and an empty message aborts the commit.
> ...

commit --amend --------------

Show 7 quoted lines
> Empty commit to test amending 
>  
> Signed-off-by: Test <hello@example.org> 
>  
> # Please enter the commit message for your changes. Lines starting 
> # with '#' will be ignored, and an empty message aborts the commit. 
> ...

merge -----

Show 9 quoted lines
> Merge branch 'hook-test' into hook-test-merge
> 
> Signed-off-by: Test <hello@example.org>
> 
> # Please enter a commit message to explain why this merge is necessary,
> # especially if it merges an updated upstream into a topic branch.
> #
> # Lines starting with '#' will be ignored, and an empty message aborts
> # the commit.

So, it seems that excepting for 'commit' it has quite a nice spacing. I guess we could add something like the following to fix that,

    # Add new line after SOB in case of "git commit"
    NEW_LINE='\
    '
    if [ -z "$2" ]
    then
      sed -i "1i$NEW_LINE" "$1"
    fi

sed-script ========== I also tried to add the signature that immitates the "-s" option of "git commit" using "sed" but it works only in following cases,

* commit
* commit --amend
* merge

It doesn't seem to work in cases where user doesn't edit the message using the editor. I'm not sure why.

I'm not including a patch of my manual way here as "git interpret- trailers" (with the fix added) seems quite promising (at least to me).

I'll send a typical patch that uses "git interpret-headers" as a follow-up.

-- 
Regards,
Kaartic Sivaraam <kaarticsivaraam91196@gmail.com>
Previous: Kaartic SivaraamNext: Kaartic Sivaraam
Message 4 of 58 in “hooks: add signature to the top of the commit message”
  1. hooks: add signature to the top of the commit messageKaartic Sivaraam, Jun 30, 2017
  2. Junio C HamanoJun 30, 2017
  3. Kaartic SivaraamJul 1, 2017
  4. "git intepret-trailers" vs. "sed script" to add the signatureKaartic Sivaraam, Jul 1, 2017
  5. hooks: add signature using "interpret-trailers"Kaartic Sivaraam, Jul 1, 2017
  6. Junio C HamanoJul 3, 2017
  7. Kaartic SivaraamJul 4, 2017
  8. Junio C HamanoJul 5, 2017
  9. hooks: add signature using "interpret-trailers"Kaartic Sivaraam, Jul 5, 2017
  10. hooks: add signature using "interpret-trailers"Kaartic Sivaraam, Jul 5, 2017
  11. Junio C HamanoJul 5, 2017
  12. Ramsay JonesJul 5, 2017
  13. Kaartic SivaraamJul 6, 2017
  14. Junio C HamanoJul 1, 2017
  15. Philip OakleyJul 1, 2017
  16. Junio C HamanoJul 1, 2017
  17. Philip OakleyJul 1, 2017
  18. Kaartic SivaraamJul 1, 2017
  19. Junio C HamanoJul 1, 2017
  20. Kaartic SivaraamJul 2, 2017
  21. hooks: replace irrelevant hook sampleKaartic Sivaraam, Jul 2, 2017
  22. hooks: replace irrelevant hook sampleKaartic Sivaraam, Jul 5, 2017
  23. Junio C HamanoJul 5, 2017
  24. Kaartic SivaraamJul 7, 2017
  25. Junio C HamanoJul 7, 2017
  26. Kaartic SivaraamJul 7, 2017
  27. 1/2 hooks: replace irrelevant hook sampleKaartic Sivaraam, Jul 7, 2017
  28. 2/2 hooks: add signature using "interpret-trailers"Kaartic Sivaraam, Jul 7, 2017
  29. Junio C HamanoJul 7, 2017
  30. 1/4 hook: cleanup scriptKaartic Sivaraam, Jul 10, 2017
  31. 2/4 hook: name the positional variablesKaartic Sivaraam, Jul 10, 2017
  32. Junio C HamanoJul 10, 2017
  33. 3/4 hook: add signature using "interpret-trailers"Kaartic Sivaraam, Jul 10, 2017
  34. Ramsay JonesJul 10, 2017
  35. Junio C HamanoJul 10, 2017
  36. 1/4 hook: cleanup scriptKaartic Sivaraam, Jul 11, 2017
  37. 3/4 hook: add sign-off using "interpret-trailers"Kaartic Sivaraam, Jul 11, 2017
  38. hook: use correct logical variableKaartic Sivaraam, Aug 14, 2017
  39. Stefan BellerAug 14, 2017
  40. Junio C HamanoAug 14, 2017
  41. Kaartic SivaraamAug 15, 2017
  42. Junio C HamanoAug 15, 2017
  43. Kaartic SivaraamAug 17, 2017
  44. hook: use correct logical variableKaartic Sivaraam, Aug 17, 2017
  45. Kaartic SivaraamAug 15, 2017
  46. 2/4 hook: name the positional variablesKaartic Sivaraam, Jul 11, 2017
  47. 4/4 hook: add a simple first exampleKaartic Sivaraam, Jul 11, 2017
  48. 4/4 hook: add a simple first exampleKaartic Sivaraam, Jul 11, 2017
  49. Kaartic SivaraamJul 11, 2017
  50. Kaartic SivaraamJul 11, 2017
  51. 4/4 hook: add a simple first exampleKaartic Sivaraam, Jul 10, 2017
  52. Junio C HamanoJul 10, 2017
  53. Kaartic SivaraamJul 11, 2017
  54. Junio C HamanoJul 11, 2017
  55. Kaartic SivaraamJul 11, 2017
  56. Kaartic SivaraamJul 11, 2017
  57. Junio C HamanoJul 10, 2017
  58. hooks: add script to HOOKS that allows adding notes from commit messageKaartic Sivaraam, Jul 2, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.