如何以编程方式配置Git分支的描述?

时间:2014-03-11 22:02:08

标签: git config stdin git-branch heredoc

git branch --edit-description命令打开一个文本编辑器,供用户手动输入Git当前分支的描述。

我所追求的是:

git config branch.my_branch.description < <(fmt --width=72 <<MY_DESCRIPTION
Subject (50 chars max)

Intro (one sentence, line wraps after 72 chars)

Description (multiple paragraphs, line wraps after 72 chars)
MY_DESCRIPTION

根据联机帮助页git config --file=config-file仅用于读取或写入非标准配置文件。最近的补丁http://git.661346.n2.nabble.com/PATCH-config-git-config-from-file-handle-quot-quot-filename-as-stdin-td7603641.html也无助于从标准输入中读取配置值。

任何想法如何以简单的方式解决这个问题,理想情况下使用Git内置插件?如果不可能,后者不是必需的。感谢您的宝贵意见。

1 个答案:

答案 0 :(得分:1)

您使用git config走在正确的轨道上。 --edit-description选项只在默认(本地)git配置文件中设置branch.branchname.description

(描述是转义序列编码,但git config会为你做。)

在shell-ese中:

$ git config branch.my_branch.description 'Subject (50 chars max)

Intro (one sentence, line wraps after 72 chars)

Description (multiple paragraphs, line wraps after 72 chars)
MY_DESCRIPTION
'

诀窍。如果您在文件中有说明,例如/tmp/file

$ git config branch.my_branch.description "$(cat /tmp/file)"

做到了。显然,您可以使用任何其他命令替换cat,例如fmt,您可以使用here- documents:

$ git config branch.my_branch.description "$(cat << END)"
some lines

of text

that wind up in the description.
END

(在我的测试中,最后一个的编码配置条目不会以换行符结尾,但这似乎没问题,即使其他版本以换行符结尾)。


编辑:bash不喜欢上面的here-document语法;这似乎有效:

cmd "$(cat << END
here-document text
goes here
END
)"

在bash和/ bin / sh中,在我的测试系统上。