git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] rev-list: teach --oid-only to enable piping

From
Emily Shaffer <emilyshaffer@google.com>
Date
Jun 13, 2019, 21:51 UTC
Message-ID
<20190613215102.44627-1-emilyshaffer@google.com>
In-Reply-To
<20190607225900.89299-1-emilyshaffer@google.com>

Allow easier parsing by cat-file by giving rev-list an option to print only the OID of an object without any additional information. This is a short-term shim; later on, rev-list should be taught how to print the types of objects it finds in a format similar to cat-file's.

Before this commit, the output from rev-list needed to be massaged before being piped to cat-file, like so:

  git rev-list --objects HEAD | cut -f 1 -d ' ' \
    | git cat-file --batch-check

This was especially unexpected when dealing with root trees, as an invisible whitespace exists at the end of the OID:

  git rev-list --objects --filter=tree:1 --max-count=1 HEAD \
    | xargs -I% echo "AA%AA"
Now, it can be piped directly, as in the added test case:
  git rev-list --objects --oid-only HEAD | git cat-file --batch-check
Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
Change-Id: I489bdf0a8215532e540175188883ff7541d70e1b
---
It didn't appear that using an existing --pretty string would do the
trick, as the formatting options for --pretty apply specifically to
commit objects (you can specify the commit hash and the tree hash, but
I didn't see a way to more generally pretty-print all types of objects).
rev-list doesn't appear to use parse-options-h, so I added a new option
as simply as I could see without breaking existing style; it seemed
wrong to add a flag to the `struct rev_list_info` as the definition of
struct and flag values alike are contained in bisect.h. There are a
couple other options to rev-list which are stored as globals.

At the moment, this doesn't work with --abbrev, and although I think it'd be a good fit, right now --abbrev doesn't seem to work without --abbrev-commit, and both those options end up being eaten by setup_revisions() rather than by rev-list itself.

 - Emily
 builtin/rev-list.c       | 21 ++++++++++++++++++++-
 t/t6000-rev-list-misc.sh | 18 ++++++++++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)
diff --git a/builtin/rev-list.c b/builtin/rev-list.c
index 9f31837d30..ff07ea9564 100644
--- a/builtin/rev-list.c
+++ b/builtin/rev-list.c
@@ -48,7 +48,7 @@ static const char rev_list_usage[] =
 "    --children\n"
 "    --objects | --objects-edge\n"
 "    --unpacked\n"
-"    --header | --pretty\n"
+"    --header | --pretty | --oid-only\n"
 "    --abbrev=<n> | --no-abbrev\n"
 "    --abbrev-commit\n"
 "    --left-right\n"
@@ -75,6 +75,8 @@ enum missing_action {
 };
 static enum missing_action arg_missing_action;
 
+static int arg_oid_only; /* display only the oid of each object encountered */
+
 #define DEFAULT_OIDSET_SIZE     (16*1024)
 
 static void finish_commit(struct commit *commit, void *data);
@@ -90,6 +92,11 @@ static void show_commit(struct commit *commit, void *data)
 		return;
 	}
 
+	if (arg_oid_only) {
+		printf("%s\n", oid_to_hex(&commit->object.oid));
+		return;
+	}
+
 	graph_show_commit(revs->graph);
 
 	if (revs->count) {
@@ -255,6 +262,10 @@ static void show_object(struct object *obj, const char *name, void *cb_data)
 	display_progress(progress, ++progress_counter);
 	if (info->flags & REV_LIST_QUIET)
 		return;
+	if (arg_oid_only) {
+		printf("%s\n", oid_to_hex(&obj->oid));
+		return;
+	}
 	show_object_with_name(stdout, obj, name);
 }
 
@@ -484,6 +495,11 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
 		if (skip_prefix(arg, "--missing=", &arg))
 			continue; /* already handled above */
 
+		if (!strcmp(arg, ("--oid-only"))) {
+			arg_oid_only = 1;
+			continue;
+		}
+
 		usage(rev_list_usage);
 
 	}
@@ -499,6 +515,9 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
 		/* Only --header was specified */
 		revs.commit_format = CMIT_FMT_RAW;
 
+	if (arg_oid_only && revs.commit_format != CMIT_FMT_UNSPECIFIED)
+		die(_("cannot combine --oid-only and --pretty or --header"));
+
 	if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
 	     (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
 	      !revs.pending.nr) &&
diff --git a/t/t6000-rev-list-misc.sh b/t/t6000-rev-list-misc.sh
index 0507999729..996ba1799b 100755
--- a/t/t6000-rev-list-misc.sh
+++ b/t/t6000-rev-list-misc.sh
@@ -48,6 +48,24 @@ test_expect_success 'rev-list --objects with pathspecs and copied files' '
 	! grep one output
 '
 
+test_expect_success 'rev-list --objects --oid-only has no whitespace/names' '
+	git rev-list --objects --oid-only HEAD >output &&
+	! grep wanted_file output &&
+	! grep unwanted_file output &&
+	! grep " " output
+'
+
+test_expect_success 'rev-list --objects --oid-only is usable by cat-file' '
+	git rev-list --objects --oid-only --all >list-output &&
+	git cat-file --batch-check <list-output >cat-output &&
+	! grep missing cat-output
+'
+
+test_expect_success 'rev-list --oid-only is incompatible with --pretty' '
+	test_must_fail git rev-list --objects --oid-only --pretty HEAD &&
+	test_must_fail git rev-list --objects --oid-only --header HEAD
+'
+
 test_expect_success 'rev-list A..B and rev-list ^A B are the same' '
 	git commit --allow-empty -m another &&
 	git tag -a -m "annotated" v1.0 &&
-- 
2.22.0.rc2.383.gf4fbbf30c2-goog
Previous: Jeff KingNext: Jeff King
Message 8 of 22 in “revision: remove stray whitespace when name empty”
  1. revision: remove stray whitespace when name emptyEmily Shaffer, Jun 7, 2019
  2. Eric SunshineJun 8, 2019
  3. Emily ShafferJun 12, 2019
  4. Jeff KingJun 9, 2019
  5. Junio C HamanoJun 10, 2019
  6. Emily ShafferJun 12, 2019
  7. Jeff KingJun 13, 2019
  8. rev-list: teach --oid-only to enable pipingEmily Shaffer, Jun 13, 2019
  9. Jeff KingJun 14, 2019
  10. Junio C HamanoJun 14, 2019
  11. Emily ShafferJun 14, 2019
  12. Emily ShafferJun 14, 2019
  13. Jeff KingJun 19, 2019
  14. rev-list: teach --no-object-names to enable pipingEmily Shaffer, Jun 14, 2019
  15. Junio C HamanoJun 17, 2019
  16. Emily ShafferJun 18, 2019
  17. rev-list: teach --no-object-names to enable pipingEmily Shaffer, Jun 18, 2019
  18. Junio C HamanoJun 19, 2019
  19. Emily ShafferJun 19, 2019
  20. Jeff KingJun 19, 2019
  21. rev-list: teach --no-object-names to enable pipingEmily Shaffer, Jun 19, 2019
  22. Jeff KingJun 19, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.