Log a template message every time I commit in Git

54 Views Asked by At

I am making my own template that involves my a bit of custom git hooks. I opened the hooks folder, and everything was a .sample file which I know nothing about.

What I am doing is manipulating the commit-msg.sample file in my hooks folder, and I messed it up... My textbook stated: "We'll use the commit-msg hook as the example hook:"

#!/bin/sh
MSG_FILE="$1"
echo "\nHi from the template commit-msg hook" >> $MSG_FILE

I decided that I would manipulate the file manually, and my file commit-msg.sample file looks like this.

#!/bin/sh
MSG_FILE="$1"
"\nHi from the template commit-msg hook" >> $MSG_FILE
# a bunch of comments....
MSG_FILE="$1"

test "" = "$(grep '^Signed-off-by: ' "$1" |
     sort | uniq -c | sed -e '/^[   ]*1[    ]/d')" || {
    echo >&2 \nHi from the template commit-msg hook  >> $MSG_FILE
    exit 1
}

However, when I do git log -1, I end up with the default and no "\nHi from the template commit-msg hook". I want it so that whenever I do git log -1, it also displays that message.

What am I doing wrong here? Any explanation would be great.

Here is my reproductible example:

  1. Create a .git_template directory
  2. Add a hooks and info folder
  3. Copy and paste all the files from hooks and info folders in the default directory (usually the /usr/share/git-core/templates) and put them into the .git_template 's hooks and and info directories respectively
  4. Now in the .git_template/hooks you should find a commit-msg.sample.
  5. below #!/bin/sh, delete everything else and write down

echo "\nHi from the template commit-msg hook" >> $MSG_FILE

  1. In the terminal, do the chmod +x commit-msg.sample
  2. Using the absolute file path, do the following command.

git config --global init.templatedir [ABSOLUTE PATH]/.git_template

  1. In a completely new folder, initialize a git repository. Add anything say index.js then do git add * then git commit -m "my message"
  2. Now do git log -1
  3. Now the expected output is "my message \nHi from the template commit-msg hook". Basically it's supposed to be the git commit message plus the hook's message. However, I only get the git commit message which is "my message".

If there's a step that I did wrong, please let me know.

0

There are 0 best solutions below