rev-list: add option for --pretty=format without header
In general, we encourage users to use plumbing commands, like git
rev-list, over porcelain commands, like git log, 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.
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
brian m. carlson committedJul 11, 2021 at 21:55 UTCd1c5ae78ce1260c94c9e626b83dc0901e6843178
4 files changed+112-12
Documentation/rev-list-options.txt
+8
index 5bf2a85f69..23388f36c3 100644--- a/Documentation/rev-list-options.txt+++ b/Documentation/rev-list-options.txt@@ -1064,6 +1064,14 @@ ifdef::git-rev-list[] --header:: Print the contents of the commit in raw-format; each record is separated with a NUL character.++--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`. endif::git-rev-list[] --parents::
builtin/rev-list.c
+24-9
index 7677b1af5a..36cb909eba 100644--- a/builtin/rev-list.c+++ b/builtin/rev-list.c@@ -127,13 +127,15 @@ static void show_commit(struct commit *commit, void *data) if (info->header_prefix) fputs(info->header_prefix, stdout);- if (!revs->graph)- fputs(get_revision_mark(revs, commit), stdout);- 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);+ if (revs->include_header) {+ if (!revs->graph)+ fputs(get_revision_mark(revs, commit), stdout);+ 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);+ } if (revs->print_parents) { struct commit_list *parents = commit->parents; while (parents) {@@ -153,7 +155,7 @@ static void show_commit(struct commit *commit, void *data) show_decorations(revs, commit); if (revs->commit_format == CMIT_FMT_ONELINE) putchar(' ');- else+ else if (revs->include_header) putchar('\n'); if (revs->verbose_header) {@@ -512,6 +514,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix) repo_init_revisions(the_repository, &revs, prefix); revs.abbrev = DEFAULT_ABBREV; revs.commit_format = CMIT_FMT_UNSPECIFIED;+ revs.include_header = 1; /* * Scan the argument list before invoking setup_revisions(), so that we@@ -627,6 +630,16 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix) continue; }+ if (!strcmp(arg, ("--commit-header"))) {+ revs.include_header = 1;+ continue;+ }++ if (!strcmp(arg, ("--no-commit-header"))) {+ revs.include_header = 0;+ continue;+ }+ if (!strcmp(arg, "--disk-usage")) { show_disk_usage = 1; info.flags |= REV_LIST_QUIET;@@ -636,10 +649,12 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix) usage(rev_list_usage); }+ if (revs.commit_format != CMIT_FMT_USERFORMAT)+ revs.include_header = 1; if (revs.commit_format != CMIT_FMT_UNSPECIFIED) { /* The command line has a --pretty */ info.hdr_termination = '\n';- if (revs.commit_format == CMIT_FMT_ONELINE)+ if (revs.commit_format == CMIT_FMT_ONELINE || !revs.include_header) info.header_prefix = ""; else info.header_prefix = "commit ";
revision.h
+2-1
index 17698cb51a..7464434f60 100644--- a/revision.h+++ b/revision.h@@ -215,7 +215,8 @@ struct rev_info { missing_newline:1, date_mode_explicit:1, preserve_subject:1,- encode_email_headers:1;+ encode_email_headers:1,+ include_header:1; unsigned int disable_stdin:1; /* --show-linear-break */ unsigned int track_linear:1,