在git hook中附加多行消息

时间:2018-09-15 11:54:49

标签: bash git sed

我正在使用prepare-commit-message挂钩将自定义的多行消息附加到我的提交中。此消息来自其他来源。

我的钩子文件内容如下:

COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3

MESSAGE=$(some_command)

if [ -z ${2+x} ]; then
    sed -i.back "1s/^/$MESSAGE \n/" "$1"
fi

MESSAGE是一行文本时,此功能有效。但是当它是多行时,我得到

unescaped newline inside substitute pattern

echo $MESSAGE会给我类似的东西

aaaa
bbbb

基本上,我想做的是每当我提交时,提交消息应包含:

<here i will enter my commit message>

aaaa
bbbb
# Please enter the commit message for your changes. Lines starting
...

1 个答案:

答案 0 :(得分:1)

那里不需要Sed。您应该可以通过以下方式避免所有这些问题:

mv $1 $1.back
(echo $MESSAGE ; cat $1.back) > $1

使用sed似乎没有简单的方法来解决该问题,而无需对$ MESSAGE内的换行符进行复杂的ANSI-C引用,例如this堆栈溢出帖子。

相关问题