rev-list: teach --no-object-names to enable piping

Allow easier parsing by cat-file by giving rev-list an option to print only the OID of a non-commit 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 --no-object-names HEAD | git cat-file --batch-check Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Change-Id: I489bdf0a8215532e540175188883ff7541d70e1b Signed-off-by: Junio C Hamano <gitster@pobox.com>

Emily Shaffer committed Jun 19, 2019 at 13:56 UTC 42357b4e8b8bfb2626208c8816e9d8a2604d17b8
4 files changed +49 -1
Documentation/git-rev-list.txt
+1
@@ -48,6 +48,7 @@ SYNOPSIS
48 [ --date=<format>]
49 [ [ --objects | --objects-edge | --objects-edge-aggressive ]
50 [ --unpacked ]
51 + [ --object-names | --no-object-names ]
52 [ --filter=<filter-spec> [ --filter-print-omitted ] ] ]
53 [ --missing=<missing-action> ]
54 [ --pretty | --header ]
Documentation/rev-list-options.txt
+10
@@ -708,6 +708,16 @@ ifdef::git-rev-list[]
708 Only useful with `--objects`; print the object IDs that are not
709 in packs.
710
711 +--object-names::
712 + Only useful with `--objects`; print the names of the object IDs
713 + that are found. This is the default behavior.
714 +
715 +--no-object-names::
716 + Only useful with `--objects`; does not print the names of the object
717 + IDs that are found. This inverts `--object-names`. This flag allows
718 + the output to be more easily parsed by commands such as
719 + linkgit:git-cat-file[1].
720 +
721 --filter=<filter-spec>::
722 Only useful with one of the `--objects*`; omits objects (usually
723 blobs) from the list of printed objects. The '<filter-spec>'
builtin/rev-list.c
+18 -1
@@ -49,6 +49,7 @@ static const char rev_list_usage[] =
49 " --objects | --objects-edge\n"
50 " --unpacked\n"
51 " --header | --pretty\n"
52 +" --[no-]object-names\n"
53 " --abbrev=<n> | --no-abbrev\n"
54 " --abbrev-commit\n"
55 " --left-right\n"
@@ -75,6 +76,9 @@ enum missing_action {
76 };
77 static enum missing_action arg_missing_action;
78
79 +/* display only the oid of each object encountered */
80 +static int arg_show_object_names = 1;
81 +
82 #define DEFAULT_OIDSET_SIZE (16*1024)
83
84 static void finish_commit(struct commit *commit);
@@ -255,7 +259,10 @@ static void show_object(struct object *obj, const char *name, void *cb_data)
259 display_progress(progress, ++progress_counter);
260 if (info->flags & REV_LIST_QUIET)
261 return;
258 - show_object_with_name(stdout, obj, name);
262 + if (arg_show_object_names)
263 + show_object_with_name(stdout, obj, name);
264 + else
265 + printf("%s\n", oid_to_hex(&obj->oid));
266 }
267
268 static void show_edge(struct commit *commit)
@@ -484,6 +491,16 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
491 if (skip_prefix(arg, "--missing=", &arg))
492 continue; /* already handled above */
493
494 + if (!strcmp(arg, ("--no-object-names"))) {
495 + arg_show_object_names = 0;
496 + continue;
497 + }
498 +
499 + if (!strcmp(arg, ("--object-names"))) {
500 + arg_show_object_names = 1;
501 + continue;
502 + }
503 +
504 usage(rev_list_usage);
505
506 }
t/t6000-rev-list-misc.sh
+20
@@ -48,6 +48,26 @@ test_expect_success 'rev-list --objects with pathspecs and copied files' '
48 ! grep one output
49 '
50
51 +test_expect_success 'rev-list --objects --no-object-names has no space/names' '
52 + git rev-list --objects --no-object-names HEAD >output &&
53 + ! grep wanted_file output &&
54 + ! grep unwanted_file output &&
55 + ! grep " " output
56 +'
57 +
58 +test_expect_success 'rev-list --objects --no-object-names works with cat-file' '
59 + git rev-list --objects --no-object-names --all >list-output &&
60 + git cat-file --batch-check <list-output >cat-output &&
61 + ! grep missing cat-output
62 +'
63 +
64 +test_expect_success '--no-object-names and --object-names are last-one-wins' '
65 + git rev-list --objects --no-object-names --object-names --all >output &&
66 + grep wanted_file output &&
67 + git rev-list --objects --object-names --no-object-names --all >output &&
68 + ! grep wanted_file output
69 +'
70 +
71 test_expect_success 'rev-list A..B and rev-list ^A B are the same' '
72 git commit --allow-empty -m another &&
73 git tag -a -m "annotated" v1.0 &&