Skip to content

Commit a68d88f

Browse files
nasamuffingitster
authored andcommitted
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 <[email protected]> Change-Id: I489bdf0a8215532e540175188883ff7541d70e1b Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0aae918 commit a68d88f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

builtin/rev-list.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static const char rev_list_usage[] =
4949
" --objects | --objects-edge\n"
5050
" --unpacked\n"
5151
" --header | --pretty\n"
52+
" --no-object-names\n"
5253
" --abbrev=<n> | --no-abbrev\n"
5354
" --abbrev-commit\n"
5455
" --left-right\n"
@@ -75,6 +76,9 @@ enum missing_action {
7576
};
7677
static enum missing_action arg_missing_action;
7778

79+
/* display only the oid of each object encountered */
80+
static int arg_no_object_names;
81+
7882
#define DEFAULT_OIDSET_SIZE (16*1024)
7983

8084
static void finish_commit(struct commit *commit);
@@ -255,7 +259,10 @@ static void show_object(struct object *obj, const char *name, void *cb_data)
255259
display_progress(progress, ++progress_counter);
256260
if (info->flags & REV_LIST_QUIET)
257261
return;
258-
show_object_with_name(stdout, obj, name);
262+
if (arg_no_object_names)
263+
printf("%s\n", oid_to_hex(&obj->oid));
264+
else
265+
show_object_with_name(stdout, obj, name);
259266
}
260267

261268
static void show_edge(struct commit *commit)
@@ -484,6 +491,11 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
484491
if (skip_prefix(arg, "--missing=", &arg))
485492
continue; /* already handled above */
486493

494+
if (!strcmp(arg, ("--no-object-names"))) {
495+
arg_no_object_names = 1;
496+
continue;
497+
}
498+
487499
usage(rev_list_usage);
488500

489501
}

t/t6000-rev-list-misc.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ test_expect_success 'rev-list --objects with pathspecs and copied files' '
4848
! grep one output
4949
'
5050

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 'rev-list --oid-only is incompatible with --pretty' '
65+
test_must_fail git rev-list --objects --oid-only --pretty HEAD &&
66+
test_must_fail git rev-list --objects --oid-only --header HEAD
67+
'
68+
5169
test_expect_success 'rev-list A..B and rev-list ^A B are the same' '
5270
git commit --allow-empty -m another &&
5371
git tag -a -m "annotated" v1.0 &&

0 commit comments

Comments
 (0)