简体   繁体   English

从 git rev-list 格式中排除 Preceding Commit> 行

[英]Exclude Preceding Commit> line from git rev-list formatting

I'm trying to work on a branch diff command, and I've got it all working... Except for the formatting.我正在尝试使用分支 diff 命令,并且我已经完成了所有工作......除了格式。 I can use --pretty=oneline to display just the information I want, except it displays the full hash, and doesn't colorize the output.我可以使用--pretty=oneline只显示我想要的信息,除了它显示完整的哈希值,并且不会对输出进行着色。

So it'd just output this:所以它只是输出这个:

fa73c05913292fbce940075fc8f454bad5066666 Example Commit Message
de4dbeffa249393dddebb7b13ae555cb97cad5be Another Example Commit Message

If I try and do a custom format string, such as this: --pretty="format:%C(yellow)%h%C(reset) %s" , it works, but it also displays an additional line above it.如果我尝试执行自定义格式字符串,例如: --pretty="format:%C(yellow)%h%C(reset) %s" ,它可以工作,但它还会在其上方显示额外的一行。

Eg例如

commit >fa73c05913292fbce940075fc8f454bad5066666
fa73c05 Example Commit Message
commit >de4dbeffa249393dddebb7b13ae555cb97cad5be
de4dbef Another Example Commit Message

Is there a way to have git rev-list output a format without the preceding commit >abcdef3... lines?有没有办法让git rev-list输出格式而不需要前面的commit >abcdef3...行?

To leave an answer for those who will come next/给下一个来的人留下答案/

@torec mentioned in his comment the following: @torec 在他的评论中提到了以下内容:

git rev-list and git log are essentially the same command git rev-listgit log本质上是同一个命令

The answer is to use the following format:答案是使用以下格式:

# print out the log history in the desired format.
git log --pretty="format:..." <SHA-1>

There is literally no way to do this, unfortunately.不幸的是,实际上没有办法做到这一点。 git rev-list is implemented thusly : git rev-list这样实现的:

    if (revs->abbrev_commit && revs->abbrev)
        fputs(find_unique_abbrev(&commit->object.oid, revs->abbrev),
              stdout);
    else
        fputs(oid_to_hex(&commit->object.oid), stdout);

so no matter what options you put in, git rev-list will always print some kind of commit hash.所以无论你输入什么选项, git rev-list都会打印某种提交哈希。

You can use sed after git rev-list to delete all the lines starting with commit .您可以在 git rev-list 之后使用sed删除所有以commit开头的行。

git rev-list --pretty=format:"%C(yellow)%h%C(reset) %s" | sed '/^commit/d' git rev-list --pretty=format:"%C(yellow)%h%C(reset) %s" | sed '/^commit/d' . git rev-list --pretty=format:"%C(yellow)%h%C(reset) %s" | sed '/^commit/d'

If I try and do a custom format string, such as this: --pretty="format:%C(yellow)%h%C(reset) %s" , it works, but it also displays an additional line above it.如果我尝试执行自定义格式字符串,例如: --pretty="format:%C(yellow)%h%C(reset) %s" ,它可以工作,但它还会在其上方显示额外的一行。

Actually... With Git 2.33 (Q3 2021), " git rev-list " ( man ) learns to omit the commit <object-name> header lines from the output with the --no-commit-header option .实际上......在 Git 2.33(2021 年第三季度)中,“ git rev-listman学会了使用--no-commit-header选项从输出中省略commit <object-name>标题行。

See commit d1c5ae7 (11 Jul 2021) by brian m.请参阅brian m 的commit d1c5ae7 (2021 年 7 月 11 日) carlson ( bk2204 ) .卡尔森 ( bk2204 )
(Merged by Junio C Hamano -- gitster -- in commit fe3fec5 , 22 Jul 2021) (由Junio C gitster -- gitster --fe3fec5 提交中合并,2021 年 7 月 22 日)

rev-list : add option for --pretty=format without header rev-list : 为没有标题的--pretty=format添加选项

Signed-off-by: brian m.签字人:brian m. carlson卡尔森

In general, we encourage users to use plumbing commands, like git rev-list ( man ) , over porcelain commands, like git log ( man ) , when scripting.一般来说,我们鼓励用户在编写脚本时使用管道命令,如git rev-list ( man ) ,而不是瓷器命令,如git log ( man )

However, git rev-list has one glaring problem that prevents it from being used in certain cases: when --pretty is used with a custom format, it always prints out a line containing "commit" and the object ID.然而, git rev-list有一个明显的问题,它在某些情况下--pretty使用:当--pretty与自定义格式一起使用时,它总是打印出一行包含“提交”和对象 ID。

This makes it unsuitable for many scripting needs, and forces users to use git log instead.这使得它不适合许多脚本需求,并强制用户使用git log代替。

While we can't change this behavior for backwards compatibility, we can add an option to suppress this behavior, so let's do so, and call it " --no-commit-header ".虽然我们不能为了向后兼容而改变这种行为,但我们可以添加一个选项来抑制这种行为,所以让我们这样做,并称之为“ --no-commit-header ”。
Additionally, add the corresponding positive option to switch it back on.此外,添加相应的正选项以将其重新打开。

Note that this option doesn't affect the built-in formats, only custom formats.请注意,此选项不会影响内置格式,只会影响自定义格式。
This is exactly the same behavior as users already have from git log and is what most users will be used to.这与用户在git log已经拥有的行为完全相同,并且是大多数用户将习惯的行为。

rev-list-options now includes in its man page : rev-list-options现在包含在其手册页中

--no-commit-header

Suppress the header line containing "commit" and the object ID printed before the specified format.禁止包含“commit”的标题行和在指定格式之前打印的对象 ID。 This has no effect on the built-in formats;这对内置格式没有影响; only custom formats are affected.只有自定义格式会受到影响。

--commit-header

Overrides a previous --no-commit-header .覆盖之前的--no-commit-header

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM