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

[PATCH] hooks: add script to HOOKS that allows adding notes from commit message

From
Kaartic Sivaraam <kaarticsivaraam91196@gmail.com>
Date
Jul 2, 2017, 11:29 UTC
Message-ID
<20170702112916.15845-1-kaarticsivaraam91196@gmail.com>
In-Reply-To
<1498994345.1667.5.camel@gmail.com>

This script is a little different from others in that it uses THREE hooks to acheive it's goal, which is to allow users to add notes for a commit while writing the commit message. It's working isn't guaranteed even if one of the hooks aren't executed.

It currently works in the following scenartios,
* commit
* commit --amend
* commit -t

From what I'm aware of it's not possible to make it work with 'merge' as there isn't any hook that checks the message of a merge commit. The notes are extracted from the commit message using the hook that checks the commit message.

Use cases other than those specified here aren't tested. It might/ might not be possible to use this script in other cases (with/without modifications).

The usage of various hooks are as follows,
prepare-commit-msg: Used to add the template in which the user
                    enters the notes for the commit. Leaving it
                    doesn't add any notes for the commit.
commit-msg: Used to extract the notes from the commit message if
            it exists and save it to a file. It removes the the
            content between the template to avoid it being added
            to the commit message.
post-commit: Used to check if the file with notes for the last commit
             exists and if it does, uses it to add notes for the commit.
             Note: It's often times an file with empty lines when notes
             aren't given for a commit. It would not be added as a note
             for the commit as 'git notes' doesn't accept files with
             empty lines.
---
 templates/hooks--commit-msg.sample         | 28 +++++++++++++++++++
 templates/hooks--post-commit.sample        | 18 +++++++++++++
 templates/hooks--prepare-commit-msg.sample | 43 +++++++++++++++++++++++-------
 3 files changed, 80 insertions(+), 9 deletions(-)
 create mode 100644 templates/hooks--post-commit.sample
diff --git a/templates/hooks--commit-msg.sample b/templates/hooks--commit-msg.sample
index b58d1184a..b15b72906 100755
--- a/templates/hooks--commit-msg.sample
+++ b/templates/hooks--commit-msg.sample
@@ -15,6 +15,34 @@
 # SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
 # grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
 
+# START : NOTES FOR A COMMIT FROM COMMIT MESSAGE
+# Script that allows you to enter notes for a commit while entering the
+# commit message in the editor.
+#
+# **NOTE** : It depends on the "prepare-commit-msg" and "post-commit"
+# hooks. They are to be enabled for it to function correctly.
+NOTES_HEADER='---------- NOTES : START ----------'
+NOTES_FOOTER='----------- NOTES : END -----------'
+GIT_DIR=`git rev-parse --git-dir`
+TEMP_NOTES_FILE="$GIT_DIR/.git/.NOTES_FOR_HEAD"
+
+save_notes_to_file() {
+  sed -n "/$NOTES_HEADER/,/$NOTES_FOOTER/ {
+    /$NOTES_HEADER/n
+    /$NOTES_FOOTER/ !{
+      p
+    }
+  }" "$1" >"$TEMP_NOTES_FILE"
+}
+
+delete_notes_from_message() {
+  sed -i "/$NOTES_HEADER/,/$NOTES_FOOTER/ d" "$1"
+}
+
+grep -q -e "^$NOTES_HEADER" "$1" && save_notes_to_file "$1" && delete_notes_from_message "$1"
+# END : NOTES FOR A COMMIT FROM COMMIT MESSAGE
+
+
 # This example catches duplicate Signed-off-by lines.
 
 test "" = "$(grep '^Signed-off-by: ' "$1" |
diff --git a/templates/hooks--post-commit.sample b/templates/hooks--post-commit.sample
new file mode 100644
index 000000000..ed99f114a
--- /dev/null
+++ b/templates/hooks--post-commit.sample
@@ -0,0 +1,18 @@
+#! /bin/sh
+# The following script allows you to enter notes for a commit
+# while entering the commit message in the editor.
+# 
+# **NOTE** : It depends on the "commit-msg" and "pre-commit-msg"
+# hooks. They are to be enabled for it to function correctly.
+
+
+# START : NOTES FOR A COMMIT FROM COMMIT MESSAGE
+GIT_DIR=`git rev-parse --git-dir`
+TEMP_NOTES_FILE="$GIT_DIR/.NOTES-FOR-HEAD"
+
+if [ -e "TEMP_NOTES_FILE" ]
+then
+  git notes add -F $TEMP_NOTES_FILE
+  rm $TEMP_NOTES_FILE
+fi
+# END : NOTES FOR A COMMIT FROM COMMIT MESSAGE
diff --git a/templates/hooks--prepare-commit-msg.sample b/templates/hooks--prepare-commit-msg.sample
index 86b8f227e..364f66677 100755
--- a/templates/hooks--prepare-commit-msg.sample
+++ b/templates/hooks--prepare-commit-msg.sample
@@ -9,8 +9,13 @@
 #
 # To enable this hook, rename this file to "prepare-commit-msg".
 
-# This hook includes three examples.  The first comments out the
-# "Conflicts:" part of a merge commit.
+# This hook includes three examples.  The first one is a script
+# that allows you to enter notes for a commit while entering the
+# commit message in the editor.
+#
+# **NOTE** : The script described above depends on the "commit-msg"
+# and "post-commit" hooks. They are to be enabled for it to function
+# correctly.
 #
 # The second includes the output of "git diff --name-status -r"
 # into the message, just before the "git status" output.  It is
@@ -20,17 +25,37 @@
 # 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
-  merge,)
-    @PERL_PATH@ -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
+# START : NOTES FOR A COMMIT FROM COMMIT MESSAGE
+REQUEST_NOTES_TEMPLATE='\
+# Add notes about this commit in the NOTES template below.\
+# DO NOT MODIFY the template to prevent surprises\
+# If you do not wish to add notes for this commit, leave it untouched\
+---------- NOTES : START ----------\
+\
+\
+----------- NOTES : END -----------\
+'
+COMMENT_IDENTIFIER='^# Please enter the .*'
+
+add_notes_template() {
+  sed -i "/$COMMENT_IDENTIFIER/ i\
+$REQUEST_NOTES_TEMPLATE" "$1"
+}
+
+case "$2," in
+  ,|commit,|template,) add_notes_template "$1"
+                       ;;
+esac
+# END : NOTES FOR A COMMIT FROM COMMIT MESSAGE
 
-# ,|template,)
+# case "$2,$3" in
+#  ,|template,)
 #   @PERL_PATH@ -i.bak -pe '
 #      print "\n" . `git diff --cached --name-status -r`
 #	 if /^#/ && $first++ == 0' "$1" ;;
-
-  *) ;;
-esac
+#
+#  *) ;;
+# esac
 
 # SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
 # grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
-- 
2.11.0
Previous: Junio C Hamano
Message 58 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.