Home > front end >  commit-msg hook reads all the data to commit_msg when amending
commit-msg hook reads all the data to commit_msg when amending

Time:01-29

In my commit-msg hook git passes commented lines from amend prompt to the script hook.

msg="$(cat "$1")"

When I am doing git commit --amend the output of the msg is

GRP-0903 CHANGE OTHER - Enhance commit-msg hook to restrict commit messages # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Sat Jan 28 19:41:37 2023  0400 # # On branch fix/GRP-0903-Button-icons-visibility-issue # Your branch is up to date with 'origin/fix/GRP-0903-Button-icons-visibility-issue'. # # Changes to be committed: # modified: .husky/commit-msg # deleted: tools/git-hooks/pre-commit.sh #

This is my original commit message - GRP-0903 CHANGE OTHER - Enhance commit-msg hook to restrict commit messages. The rest is coming from the amending prompt. This is the content of my editor when I am using amend.

GRP-0988 CHANGE OTHER - Enhance commit-msg hook to restrict commit messages

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Jan 28 19:41:37 2023  0400
#
# On branch fix/GRP-0903-Button-icons-visibility-issue
# Your branch is up to date with 'origin/fix/GRP-0903-Button-icons-visibility-issue'.
#
# Changes to be committed:
#   modified:   .husky/commit-msg
#   deleted:    tools/git-hooks/pre-commit.sh
#

When I do a normal commit without amending, it works fine. How can I make git pass only commit message to the hook ?

CodePudding user response:

I'm not aware of a way to get the message directly without the template, but you could emulate this by greping the commented out lines away instead of calling cat $1:

msg="$(grep -v '^#' $1)" 
  • Related