[v4] rev-list: teach --no-object-names to enable piping
diff mbox series

Message ID 20190618222917.261701-1-emilyshaffer@google.com
State New
Headers show
Series
  • [v4] rev-list: teach --no-object-names to enable piping
Related show

Commit Message

Emily Shaffer June 18, 2019, 10:29 p.m. UTC
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
---
Since v3, added a corresponding "--object-names" arg to pair with
"--no-object-names", and "last-one-wins" logic. Also added a test to
validate this new arg and the logic.

I did not take Junio's suggestion of naming the arg "show_object_names"
as "arg_show_object_names" better matches the existing style of
builtin/revlist.c.

In adding the test, I noticed that I had left in a test about --oid-only
that doesn't apply after the changes from v2->v3; that test is removed.

 - Emily

 builtin/rev-list.c       | 19 ++++++++++++++++++-
 t/t6000-rev-list-misc.sh | 20 ++++++++++++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)

Comments

Junio C Hamano June 19, 2019, 2:08 p.m. UTC | #1
Emily Shaffer <emilyshaffer@google.com> writes:

> Since v3, added a corresponding "--object-names" arg to pair with
> "--no-object-names", and "last-one-wins" logic. Also added a test to
> validate this new arg and the logic.

Thanks for a quick turnaround (unfortunately, I was OOO yesterday
and I am half-sick today, so please expect slow responses---sorry
about that).

> In adding the test, I noticed that I had left in a test about --oid-only
> that doesn't apply after the changes from v2->v3; that test is removed.

I noticed in range-diff, too.  So now --object-names can be used
with --pretty (not that "rev-list --pretty --objects" makes much
sense in the first place, so no point in testing that it works).
Emily Shaffer June 19, 2019, 7:31 p.m. UTC | #2
On Wed, Jun 19, 2019 at 07:08:14AM -0700, Junio C Hamano wrote:
> Emily Shaffer <emilyshaffer@google.com> writes:
> 
> > Since v3, added a corresponding "--object-names" arg to pair with
> > "--no-object-names", and "last-one-wins" logic. Also added a test to
> > validate this new arg and the logic.
> 
> Thanks for a quick turnaround (unfortunately, I was OOO yesterday
> and I am half-sick today, so please expect slow responses---sorry
> about that).
> 
> > In adding the test, I noticed that I had left in a test about --oid-only
> > that doesn't apply after the changes from v2->v3; that test is removed.
> 
> I noticed in range-diff, too.  So now --object-names can be used
> with --pretty (not that "rev-list --pretty --objects" makes much
> sense in the first place, so no point in testing that it works).

Yeah, it works. It looks weird, but it works pretty much as you'd
expect:

emilyshaffer@podkayne:~/git-second [stray-whitespace]$ tg2 rev-list
--object-names --objects --pretty=short --max-count=1 HEAD | head -n 20
commit 701c66d5f2fafe163892fa0968ce8bca041dbc92
Author: Emily Shaffer <emilyshaffer@google.com>

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

    d4b1d372d16aaff35b221afce017f90542fd9293 
    41d4cd23fd97f599053a19555a173894da71e560 .clang-format
    42cdc4bbfb05934bb9c3ed2fe0e0d45212c32d7a .editorconfig

emilyshaffer@podkayne:~/git-second [stray-whitespace]$ tg2 rev-list
--no-object-names --objects --pretty=short --max-count=1 HEAD | head -n
20
commit 701c66d5f2fafe163892fa0968ce8bca041dbc92
Author: Emily Shaffer <emilyshaffer@google.com>

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

    d4b1d372d16aaff35b221afce017f90542fd9293
    41d4cd23fd97f599053a19555a173894da71e560
    42cdc4bbfb05934bb9c3ed2fe0e0d45212c32d7a

Ah, but when I was grabbing these samples, I noticed that I didn't
update the manpage for rev-list. So please wait while I reroll again...
sorry!

 - Emily
Jeff King June 19, 2019, 9:30 p.m. UTC | #3
On Wed, Jun 19, 2019 at 12:31:34PM -0700, Emily Shaffer wrote:

> > I noticed in range-diff, too.  So now --object-names can be used
> > with --pretty (not that "rev-list --pretty --objects" makes much
> > sense in the first place, so no point in testing that it works).
> 
> Yeah, it works. It looks weird, but it works pretty much as you'd
> expect:

I think that something like:

  git rev-list --in-commit-order --oneline --objects HEAD

could make sense to get a rough idea of which commits are mentioning
which objects (though in most cases I'd probably use "log --find-object"
these days, since it's more precise; rev-list is looking in a funny
reverse order).

-Peff

Patch
diff mbox series

diff --git a/builtin/rev-list.c b/builtin/rev-list.c
index 660172b014..301ccb970b 100644
--- a/builtin/rev-list.c
+++ b/builtin/rev-list.c
@@ -49,6 +49,7 @@  static const char rev_list_usage[] =
 "    --objects | --objects-edge\n"
 "    --unpacked\n"
 "    --header | --pretty\n"
+"    --[no-]object-names\n"
 "    --abbrev=<n> | --no-abbrev\n"
 "    --abbrev-commit\n"
 "    --left-right\n"
@@ -75,6 +76,9 @@  enum missing_action {
 };
 static enum missing_action arg_missing_action;
 
+/* display only the oid of each object encountered */
+static int arg_show_object_names = 1;
+
 #define DEFAULT_OIDSET_SIZE     (16*1024)
 
 static void finish_commit(struct commit *commit);
@@ -255,7 +259,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;
-	show_object_with_name(stdout, obj, name);
+	if (arg_show_object_names)
+		show_object_with_name(stdout, obj, name);
+	else
+		printf("%s\n", oid_to_hex(&obj->oid));
 }
 
 static void show_edge(struct commit *commit)
@@ -484,6 +491,16 @@  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, ("--no-object-names"))) {
+			arg_show_object_names = 0;
+			continue;
+		}
+
+		if (!strcmp(arg, ("--object-names"))) {
+			arg_show_object_names = 1;
+			continue;
+		}
+
 		usage(rev_list_usage);
 
 	}
diff --git a/t/t6000-rev-list-misc.sh b/t/t6000-rev-list-misc.sh
index 0507999729..52a9e38d66 100755
--- a/t/t6000-rev-list-misc.sh
+++ b/t/t6000-rev-list-misc.sh
@@ -48,6 +48,26 @@  test_expect_success 'rev-list --objects with pathspecs and copied files' '
 	! grep one output
 '
 
+test_expect_success 'rev-list --objects --no-object-names has no space/names' '
+	git rev-list --objects --no-object-names HEAD >output &&
+	! grep wanted_file output &&
+	! grep unwanted_file output &&
+	! grep " " output
+'
+
+test_expect_success 'rev-list --objects --no-object-names works with cat-file' '
+	git rev-list --objects --no-object-names --all >list-output &&
+	git cat-file --batch-check <list-output >cat-output &&
+	! grep missing cat-output
+'
+
+test_expect_success '--no-object-names and --object-names are last-one-wins' '
+	git rev-list --objects --no-object-names --object-names --all >output &&
+	grep wanted_file output &&
+	git rev-list --objects --object-names --no-object-names --all >output &&
+	! grep wanted_file output
+'
+
 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 &&