git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kaartic Sivaraam <kaarticsivaraam91196@gmail.com>
To: gitster@pobox.com
Cc: git@vger.kernel.org
Subject: [PATCH] hooks: add script to HOOKS that allows adding notes from commit message
Date: Sun,  2 Jul 2017 16:59:16 +0530	[thread overview]
Message-ID: <20170702112916.15845-1-kaarticsivaraam91196@gmail.com> (raw)
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


      parent reply	other threads:[~2017-07-02 11:29 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-30 15:43 [PATCH] hooks: add signature to the top of the commit message Kaartic Sivaraam
2017-06-30 16:44 ` Junio C Hamano
2017-07-01 14:15   ` Kaartic Sivaraam
2017-07-01 16:16     ` "git intepret-trailers" vs. "sed script" to add the signature Kaartic Sivaraam
2017-07-01 17:32       ` [PATCH/RFC] hooks: add signature using "interpret-trailers" Kaartic Sivaraam
2017-07-03 16:58       ` "git intepret-trailers" vs. "sed script" to add the signature Junio C Hamano
2017-07-04 19:16         ` Kaartic Sivaraam
2017-07-05  1:48           ` Junio C Hamano
2017-07-05 17:00           ` [PATCH] hooks: add signature using "interpret-trailers" Kaartic Sivaraam
2017-07-05 17:35             ` Kaartic Sivaraam
2017-07-05 19:37               ` Junio C Hamano
2017-07-05 20:14               ` Ramsay Jones
2017-07-06 14:30                 ` Kaartic Sivaraam
2017-07-01 17:36     ` [PATCH] hooks: add signature to the top of the commit message Junio C Hamano
2017-07-01 18:40       ` Philip Oakley
2017-07-01 20:28         ` Junio C Hamano
2017-07-01 21:00           ` Philip Oakley
2017-07-01 18:52       ` Kaartic Sivaraam
2017-07-01 20:31         ` Junio C Hamano
2017-07-02 11:19           ` Kaartic Sivaraam
2017-07-02 11:27             ` [PATCH/RFC] hooks: replace irrelevant hook sample Kaartic Sivaraam
2017-07-05 16:51               ` [PATCH] " Kaartic Sivaraam
2017-07-05 19:50                 ` Junio C Hamano
2017-07-07 11:53                   ` Kaartic Sivaraam
2017-07-07 15:05                     ` Junio C Hamano
2017-07-07 15:24                       ` Kaartic Sivaraam
2017-07-07 16:07                         ` [PATCH 1/2] " Kaartic Sivaraam
2017-07-07 16:07                           ` [PATCH 2/2] hooks: add signature using "interpret-trailers" Kaartic Sivaraam
2017-07-07 18:27                           ` [PATCH 1/2] hooks: replace irrelevant hook sample Junio C Hamano
2017-07-10 14:17                             ` [PATCH 1/4] hook: cleanup script Kaartic Sivaraam
2017-07-10 14:17                               ` [PATCH 2/4] hook: name the positional variables Kaartic Sivaraam
2017-07-10 19:51                                 ` Junio C Hamano
2017-07-10 14:17                               ` [PATCH 3/4] hook: add signature using "interpret-trailers" Kaartic Sivaraam
2017-07-10 15:13                                 ` Ramsay Jones
2017-07-10 19:53                                   ` Junio C Hamano
2017-07-11 14:11                                     ` [PATCH 1/4] hook: cleanup script Kaartic Sivaraam
2017-07-11 14:11                                       ` [PATCH 2/4] hook: name the positional variables Kaartic Sivaraam
2017-07-11 14:11                                       ` [PATCH 3/4] hook: add sign-off using "interpret-trailers" Kaartic Sivaraam
2017-08-14  8:46                                         ` [PATCH] hook: use correct logical variable Kaartic Sivaraam
2017-08-14 17:54                                           ` Stefan Beller
2017-08-14 18:19                                           ` Junio C Hamano
2017-08-15  9:31                                             ` Kaartic Sivaraam
2017-08-15 17:28                                               ` Junio C Hamano
2017-08-17  2:47                                                 ` Kaartic Sivaraam
2017-08-17  2:50                                                   ` [PATCH v2/RFC] " Kaartic Sivaraam
2017-08-15  9:32                                             ` [PATCH] " Kaartic Sivaraam
2017-07-11 14:11                                       ` [PATCH 4/4] hook: add a simple first example Kaartic Sivaraam
2017-07-11 14:30                                         ` Kaartic Sivaraam
2017-07-11 13:10                                   ` [PATCH 3/4] hook: add signature using "interpret-trailers" Kaartic Sivaraam
2017-07-11 13:18                                   ` Kaartic Sivaraam
2017-07-10 14:17                               ` [PATCH 4/4] hook: add a simple first example Kaartic Sivaraam
2017-07-10 20:02                                 ` Junio C Hamano
2017-07-11 13:29                                   ` Kaartic Sivaraam
2017-07-11 16:03                                     ` Junio C Hamano
2017-07-11 18:04                                       ` Kaartic Sivaraam
2017-07-11 18:06                                       ` Kaartic Sivaraam
2017-07-10 19:50                               ` [PATCH 1/4] hook: cleanup script Junio C Hamano
2017-07-02 11:29             ` Kaartic Sivaraam [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170702112916.15845-1-kaarticsivaraam91196@gmail.com \
    --to=kaarticsivaraam91196@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).