git-format-rev
实验性:按需美化格式化修订
概要
(EXPERIMENTAL!) git format-rev --stdin-mode=<mode> --format=<pretty> [--[no-]notes=<ref>] [-z] [--[no-]null-output] [--[no-]null-input]描述
从标准输入美化格式化修订。
此命令是实验性的。行为可能会更改。
选项
--stdin-mode=<mode>- 如何解释标准输入数据:revs:每行或每条记录(参见输入和输出格式部分)被解释为提交。可以使用任何类型的修订表达式(参见 gitrevisions(7))。带注释的标签被剥离(参见 gitglossary(7))。参数rev也被接受。text:格式化在自由格式文本中找到的所有提交对象名称。这些必须是完整的对象名称,即缩写的十六进制对象名称不会被解释。
--format=<pretty>- 美化格式字符串。--notes=<ref>,--no-notes- 自定义注释引用。使用%N原子时显示注释。参见 git-notes(1)。-z,--null- 使用 NUL 字符终止输入和输出而不是换行符。此选项不能被否定。 如果输入和输出都可能包含换行符,或者此命令的输入也使用 NUL 字符终止,这很有用。--null-output,--no-null-output- 使用 NUL 字符终止输出而不是换行符。默认为--no-null-output。--null-input,--no-null-input- 使用 NUL 字符终止输入而不是换行符。默认为--no-null-input。
输入和输出格式
该命令默认使用换行符进行输入和输出终止。有关使用 NUL 字符作为终止符的信息,请参阅 -z、--null-output 和 --null-input 选项。
模式 --stdin-mode=revs 输出一个格式化提交后跟终止符。在"行"过于暗示换行符终止的情况下,可以将其称为_记录_。
请注意,这意味着终止符字符(换行符或 NUL)充当_终止符_,而不是_分隔符_。换句话说,最后一行或记录也由终止符字符终止。
模式 --stdin-mode=text 将每个对象名称替换为格式化提交,即格式 %s 将某个提交对象名称转换为 <subject> 而不带任何终止符。
由于每条记录都会立即刷新,因此可以安全地从此命令进行交互式读取和写入。
示例
命令 git-last-modified(1) 显示每个文件最后修改所在的提交。
$ git last-modified -- README.md Makefile
7798034171030be0909c56377a4e0e10e6d2df93 Makefile
c50fbb2dd225e7e82abba4380423ae105089f4d7 README.md我们可以将结果通过管道传递到此命令,以将对象名称替换为提交作者。
$ git last-modified -- README.md Makefile |
git format-rev --stdin-mode=text --format=%an
Junio C Hamano Makefile
Todd Zullinger README.md另一个示例是_在提交消息中格式化提交_。给定此提交消息:
Fix off-by-one error
Fix off-by-one error introduced in
e83c5163316f89bfbde7d9ab23ca2e25604af290.
We thought we fixed this in 5569bf9bbedd63a00780fc5c110e0cfab3aa97b9 but
that only covered 1/3 of the faulty cases.我们可以格式化提交并使用 par(1) 重新排列文本,例如在 commit-msg 钩子中:
$ git config set hook.reference-commits.event commit-msg
$ git config set hook.reference-commits.command reference-commits
$ cat $(which reference-commits)
#/bin/sh
msg="$1"
rewritten=$(mktemp)
git format-rev --stdin-mode=text --format=reference <"$msg" |
par >"$rewritten"
mv "$rewritten" "$msg"这将产生类似这样的内容:
Fix off-by-one error
Fix off-by-one error introduced in e83c5163316 (Implement better memory
allocator, 2005-04-07).
We thought we fixed this in 5569bf9bbed (Fix memory allocator,
2005-06-22) but that only covered 1/3 of the faulty cases.讨论
此命令允许您通过一次命令调用以任意顺序格式化任意数量的修订。考虑上面示例部分中的 git-last-modified(1) 案例:
- 可能有数百个文件
- 提交可能重复,即两个或更多文件最后在同一提交中修改
两个广泛使用的美化格式化提交的命令是 git-log(1) 和 git-show(1)。事实证明它们不太适合上述用例。
简而言之,如果您每行使用一个进程,使用这两个命令很简单。如果您只想使用一个进程,工作量要大得多,但仍然可行。相比之下,使用此命令只需另一个 shell 管道即可解决此问题。
另请参阅
Git
git(1) 套件的一部分
