文件结束意外(期待")"),将命令传递给git filter-branch

时间:2018-05-30 13:47:04

标签: bash git git-bash

您好我已经制作了一个脚本来大写提交者姓名并更改他们的电子邮件

git filter-branch --commit-filter \
'export GIT_AUTHOR_NAME=$( sed -r 's/\<./\U&/g' <<< $GIT_AUTHOR_NAME) \
export GIT_AUTHOR_EMAIL=$(sed 's/@[^,]*/@foo.net/' <<< $GIT_COMMITTER_EMAIL) \
export GIT_COMMITTER_NAME=$( sed -r 's/\<./\U&/g' <<< $GIT_AUTHOR_NAME) \
export GIT_COMMITTER_EMAIL=$(sed 's/@[^,]*/@foo.net/' <<< $GIT_COMMITTER_EMAIL) \
git commit-tree "$@"'

sed命令,当它们自己运行时,可以工作,但整体上它会抛出

Rewrite e3acbe28d660ffc6ef8e9a5a79ba9bd24c3b48bd (1/22)git commit-tree:48:
git commit-tree: Syntax error: end of file unexpected (expecting ")")
could not write rewritten commit

1 个答案:

答案 0 :(得分:2)

export命令中的单引号被读作语法,而非文字。

解决此问题的一种方法是在引用的heredoc中设置命令字符串,然后再展开它:

cmd=$(cat <<'EOF'
GIT_AUTHOR_NAME=$(sed -r 's/\<./\U&/g' <<<"$GIT_AUTHOR_NAME")
GIT_AUTHOR_EMAIL=$(sed 's/@[^,]*/@foo.net/' <<<"$GIT_COMMITTER_EMAIL")
GIT_COMMITTER_NAME=$(sed -r 's/\<./\U&/g' <<<"$GIT_AUTHOR_NAME")
GIT_COMMITTER_EMAIL=$(sed 's/@[^,]*/@foo.net/' <<<"$GIT_COMMITTER_EMAIL")
git commit-tree "$@"
EOF
)

git filter-branch --commit-filter "$cmd"

<<'EOF'用于防止在分配之前对heredoc内的扩展进行处理。

请注意export已消失 - shell会自动将任何更新导出到环境中已存在的变量,并在启动shell之前将其导出到环境中,这正是git filter-branch确保的方式这些值首先可用。