简体   繁体   中英

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. I can use --pretty=oneline to display just the information I want, except it displays the full hash, and doesn't colorize the output.

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.

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?

To leave an answer for those who will come next/

@torec mentioned in his comment the following:

git rev-list and git log are essentially the same command

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 :

    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.

You can use sed after git rev-list to delete all the lines starting with 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' .

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.

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 .

See commit d1c5ae7 (11 Jul 2021) by brian m. carlson ( bk2204 ) .
(Merged by Junio C Hamano -- gitster -- in commit fe3fec5 , 22 Jul 2021)

rev-list : add option for --pretty=format without header

Signed-off-by: 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.

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.

This makes it unsuitable for many scripting needs, and forces users to use git log instead.

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 ".
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.

rev-list-options now includes in its man page :

--no-commit-header

Suppress the header line containing "commit" and the object ID printed before the specified format. This has no effect on the built-in formats; only custom formats are affected.

--commit-header

Overrides a previous --no-commit-header .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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