由Git

时间:2016-07-20 17:18:43

标签: git bash grep text-processing

当我为我的某个项目输入提交消息时,它们通常采用以下格式:

My Commit Header

main: Added a new class
doc: Documented the method that was added to main under the name `FooBar.baz()`.
subproject: Did something that requires a really long description.

这些问题是它们被Git自动包装的事实。使用git log生成更改日志时,我通常会得到类似的内容(使用仅打印正文的格式):

main: Added a new class
doc: Documented the method that was added to main
under the name `FooBar.baz()`.
subproject: Did something that requires a
really long description.

我现在要做的是合并这些行,以便输出中的每一行都以tag:开头,并以句点.结束。不幸的是,这些线条也可以包含点,如第二个例子所示。因此,行结尾可能被定义为一个句点,后跟换行符。

我可以使用正则表达式^[\w()]+:.*\n.*\.$,但我不知道如何使用grep将其应用于多行。

有没有办法合并使用标准Unix / Mac OS命令匹配正则表达式的两条线?

1 个答案:

答案 0 :(得分:2)

在提交消息中包含行不是git。它是您用来编写提交消息的编辑器。因此,要禁用包装,必须正确配置编辑器。但请注意,这可能与etiquette的普遍接受的writing good commit messages相反。

因此,可能更明智地允许编辑器包装行,并在查看更改日志时恢复它们,就像您请求的那样。以下脚本将执行展开作业(展开不会跨越由空行定义的边界):

<强> unwrap_commit_message_lines

#!/bin/bash

# This script is for the GNU variant of sed!
sed -n -e '/^$\|^[^ :]\+: / {x;p;d}'                               \
       -e '/^[^ :]\+: /! {H;x;s/\(^[^ :]\+: .\+\) *\n */\1 /;x};'  \
       -e '$ {x;p};'                                               \

用法示例:

$ git log --format=format:'------------%ncommit %H%n%n%b'|unwrap_commit_message_lines