gitbashgithooks

How to append string to the end of same line of Git commit message with Git commit hook


I'm trying to append a string to the end of a Git commit message and this SO post was a very helpful step in the right direction.

So far, this is working in .git/hooks/prepare-commit-msg but appending my string on a new line:

echo "foo" >> "$1"

with output:

"Initial commit
 foo"

I was looking into how to append on the same line with echo but I'm unable to successfully pass the -n argument to echo in a commit hook. In addition to echo, I also tried printf to no avail as well.

I'm looking to have the my commit message look like:

"Initial commit foo"

How can this be accomplished?


Solution

  • I finally got it after revisiting this recently. Here's my prepare-commit-hook:

    # Append string/emoji to each commit message
    commitMsgFile = "$1"
    existingMsg = `cat $commitMsgFile`
    echo "$existingMsg :shipit:" > "$1"
    

    I was unable to pass echo arguments but I was able to overwrite the original commit message with the addition of the string on the same line. Hope this helps someone in the future.