首页 > 解决方案 > 如何在 git commit 正文的每一行前面添加一个字符

问题描述

我们的代码审查工具使用 Markdown 作为 CR 描述。我想通过从正在审查的 git 提交中获取信息来自动编写这些描述。

假设我有一个带有以下主题和正文的 git commit:

This is the subject line

This is a long paragraph spanning multiple lines wrapped to 80
characters. 

This is a second paragraph.

我想创建一个 git log 格式化程序,它从此提交产生以下输出:

`<commit short hash>` - This is the subject line

> This is a long paragraph spanning multiple lines wrapped to 80
> characters.
> 
> This is a second paragraph.

我已尝试使用以下命令来生成此输出,但我找不到将其>添加到正文每一行的方法

$ git log --format='`%h` - %s%n%n> %b%n' 'HEAD^..HEAD'
`e8aa4cf` - This is the subject line

> This is a long paragraph spanning multiple lines wrapped to 80
characters.

This is a second paragraph.

我尝试的另一个选择是用来w(80,2,2)填充身体,但这也缺少>每一行。

$ git log --format='`%h` - %s%n%n>%w(80,2,2)%b%n' 'HEAD^..HEAD'
`e8aa4cf` - This is the subject line

>  This is a long paragraph spanning multiple lines wrapped to 80 characters.

  This is a second paragraph.

这可以使用git log --format='X'吗?

标签: gitgit-log

解决方案


看起来git log --format不允许更改正文。

作为替代方案sed,可用于插入 padding >,例如:

git log --pretty=format:'`%h` - %s%n%w(80,8,2)%b' | sed 's/^        /> /' | sed -z 's/\n\n>/\n>\n>/g'

推荐阅读