我有以下命令在命令行中列出本地分支:
git for-each-ref --sort=committerdate refs/heads/ --format='%(color:yellow)%(refname:short)%(color:reset);(%(color:green)%(committerdate:relative)%(color:reset));%<(70,trunc)(%(contents:subject)%s)' | column -t -s';'
一切正常,除了 %<(70,trunc)
它只是打印出来而不是作为命令应用。
根据 the docs :
'%<(<N>[,trunc|ltrunc|mtrunc])': make the next placeholder take at least N columns, padding spaces on the right if necessary. Optionally truncate at the beginning (ltrunc), the middle (mtrunc) or the end (trunc) if the output is longer than N columns. Note that truncating only works correctly with N >= 2.
我认为这无关紧要,但这在 zsh 或 bash 中都不起作用。
最佳答案
您正在阅读错误的文档。漂亮的格式不是 for-each-ref
中使用的格式,for-each-ref
的文档中也没有任何建议。即使您阅读了 pretty formats 帮助页面,您也会发现这与您正在使用的占位符根本不匹配。你那里有像 %s
这样的东西,而不是像 %(contents:subject)
这样的东西。 (顺便说一句。您的模式中仍然有 %s
将始终被打印。
由于您的模式中有 %(contents:subject)
和 %s
,我猜您尝试转换 git log
模式到 git for-each-ref
模式,但它们完全不同,这就是填充/截断占位符不起作用的原因。它只提供漂亮的格式,而不是 for-each-ref 格式。
关于git 格式化 : truncating with columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43985121/