Skip to content

Commit f69c501

Browse files
trastgitster
authored andcommitted
rev-list: introduce --count option
Add a --count option that, instead of actually listing the commits, merely counts them. This is mostly geared towards script use, and to this end it acts specially when used with --left-right: it outputs the left and right counts separately. Previously, scripts would have to run a shell loop or small inline script over to achieve the same. (Without --left-right, a simple |wc -l does the job.) Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3499cb1 commit f69c501

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

Documentation/rev-list-options.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ you would get an output like this:
9898
This implies the '--topo-order' option by default, but the
9999
'--date-order' option may also be specified.
100100

101+
ifdef::git-rev-list[]
102+
--count::
103+
Print a number stating how many commits would have been
104+
listed, and suppress all other output. When used together
105+
with '--left-right', instead print the counts for left and
106+
right commits, separated by a tab.
107+
endif::git-rev-list[]
108+
109+
101110
ifndef::git-rev-list[]
102111
Diff Formatting
103112
~~~~~~~~~~~~~~~

builtin/rev-list.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ static void show_commit(struct commit *commit, void *data)
5050

5151
graph_show_commit(revs->graph);
5252

53+
if (revs->count) {
54+
if (commit->object.flags & SYMMETRIC_LEFT)
55+
revs->count_left++;
56+
else
57+
revs->count_right++;
58+
finish_commit(commit, data);
59+
return;
60+
}
61+
5362
if (info->show_timestamp)
5463
printf("%lu ", commit->date);
5564
if (info->header_prefix)
@@ -400,5 +409,12 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
400409
quiet ? finish_object : show_object,
401410
&info);
402411

412+
if (revs.count) {
413+
if (revs.left_right)
414+
printf("%d\t%d\n", revs.count_left, revs.count_right);
415+
else
416+
printf("%d\n", revs.count_left + revs.count_right);
417+
}
418+
403419
return 0;
404420
}

revision.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
11461146
revs->boundary = 1;
11471147
} else if (!strcmp(arg, "--left-right")) {
11481148
revs->left_right = 1;
1149+
} else if (!strcmp(arg, "--count")) {
1150+
revs->count = 1;
11491151
} else if (!strcmp(arg, "--cherry-pick")) {
11501152
revs->cherry_pick = 1;
11511153
revs->limited = 1;

revision.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct rev_info {
5757
limited:1,
5858
unpacked:1,
5959
boundary:2,
60+
count:1,
6061
left_right:1,
6162
rewrite_parents:1,
6263
print_parents:1,
@@ -131,6 +132,10 @@ struct rev_info {
131132

132133
/* notes-specific options: which refs to show */
133134
struct display_notes_opt notes_opt;
135+
136+
/* commit counts */
137+
int count_left;
138+
int count_right;
134139
};
135140

136141
#define REV_TREE_SAME 0

t/t6007-rev-list-cherry-pick-file.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@ test_expect_success setup '
3232
git tag B
3333
'
3434

35+
cat >expect <<EOF
36+
<tags/B
37+
>tags/C
38+
EOF
39+
40+
test_expect_success '--left-right' '
41+
git rev-list --left-right B...C > actual &&
42+
git name-rev --stdin --name-only --refs="*tags/*" \
43+
< actual > actual.named &&
44+
test_cmp actual.named expect
45+
'
46+
47+
test_expect_success '--count' '
48+
git rev-list --count B...C > actual &&
49+
test "$(cat actual)" = 2
50+
'
51+
3552
test_expect_success '--cherry-pick foo comes up empty' '
3653
test -z "$(git rev-list --left-right --cherry-pick B...C -- foo)"
3754
'
@@ -54,4 +71,16 @@ test_expect_success '--cherry-pick with independent, but identical branches' '
5471
HEAD...master -- foo)"
5572
'
5673

74+
cat >expect <<EOF
75+
1 2
76+
EOF
77+
78+
# Insert an extra commit to break the symmetry
79+
test_expect_success '--count --left-right' '
80+
git checkout branch &&
81+
test_commit D &&
82+
git rev-list --count --left-right B...D > actual &&
83+
test_cmp expect actual
84+
'
85+
5786
test_done

0 commit comments

Comments
 (0)