0

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"

Does anyone have any idea on how to accomplish this?

Community
  • 1
  • 1
Tom Hanlon
  • 63
  • 2
  • 8
  • possible duplicate of [Add to the end of a line containing a pattern - with sed or awk](http://stackoverflow.com/questions/9591744/add-to-the-end-of-a-line-containing-a-pattern-with-sed-or-awk) – larsks Feb 25 '15 at 19:29
  • @larsks Thanks but I think the issue I'm having is specific to git hooks in particular. – Tom Hanlon Feb 25 '15 at 19:43
  • 1
    @TomHanlon: no, it's totally generic. The commit-message file (argument `$1`) is an ordinary file and must be edited in the usual ordinary ways. Appending to the file appends to the file, not to lines *within* the file. Using `echo -n` is no help as the file already has a newline after its last line. – torek Feb 25 '15 at 20:26
  • Thanks @torek -- looks like my path forward will be identifying the blank trailing line, removing it, and appending to the final line using sed. – Tom Hanlon Feb 25 '15 at 21:51

1 Answers1

5

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.

Tom Hanlon
  • 63
  • 2
  • 8