使用git interpret-trailers的--where end未在文件末尾添加trailer问题排查
git interpret-trailers 未将trailer添加到文件末尾的问题分析与解决
你的操作命令及结果如下:
$ cat ./test # foo # bar # baz $ git interpret-trailers --in-place --where end --unfold --trailer "should-appear-at: bottom" ./test $ cat ./test # foo should-appear-at: bottom # bar # baz
错误原因
你对--where end参数的作用理解有误。这个参数的实际效果是在文件中已存在的trailer区块末尾添加新trailer,而当文件中完全没有任何trailer时,git interpret-trailers会默认遵循commit message的结构逻辑,将新创建的trailer区块插入到第一个非空段落的后面(这里的段落指连续的非空行块)。你的test文件里# foo是第一个非空行,后面跟着空行,工具就把trailer插在了这个位置,而非文件末尾。
解决方法
方法一:调整文件结构后执行命令
先给文件末尾追加一个空行,让工具识别到文件末尾是合适的trailer插入位置:
echo "" >> ./test git interpret-trailers --in-place --where end --unfold --trailer "should-appear-at: bottom" ./test
执行后文件内容会变为:
# foo # bar # baz should-appear-at: bottom
方法二:直接用shell命令追加(更简洁)
如果不需要依赖git interpret-trailers的trailer格式化逻辑,只是单纯在文件末尾添加该行,直接用echo追加更高效:
echo -e "\nshould-appear-at: bottom" >> ./test
内容的提问来源于stack exchange,提问作者Mr.
相关产品推荐
相关产品推荐

