@matheusbsbs / matheus-bagliano / commits / f69c501832
Fork 0

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 <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Jun 10, 2010 at 13:47 UTC f69c501832ecd6880602c55565508e70c3a013d5
5 files changed +61
Documentation/rev-list-options.txt
+9
@@ -98,6 +98,15 @@ you would get an output like this:
98 This implies the '--topo-order' option by default, but the
99 '--date-order' option may also be specified.
100
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 +
110 ifndef::git-rev-list[]
111 Diff Formatting
112 ~~~~~~~~~~~~~~~
builtin/rev-list.c
+16
@@ -50,6 +50,15 @@ static void show_commit(struct commit *commit, void *data)
50
51 graph_show_commit(revs->graph);
52
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 +
62 if (info->show_timestamp)
63 printf("%lu ", commit->date);
64 if (info->header_prefix)
@@ -400,5 +409,12 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
409 quiet ? finish_object : show_object,
410 &info);
411
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 +
419 return 0;
420 }
revision.c
+2
@@ -1146,6 +1146,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
1146 revs->boundary = 1;
1147 } else if (!strcmp(arg, "--left-right")) {
1148 revs->left_right = 1;
1149 + } else if (!strcmp(arg, "--count")) {
1150 + revs->count = 1;
1151 } else if (!strcmp(arg, "--cherry-pick")) {
1152 revs->cherry_pick = 1;
1153 revs->limited = 1;
revision.h
+5
@@ -57,6 +57,7 @@ struct rev_info {
57 limited:1,
58 unpacked:1,
59 boundary:2,
60 + count:1,
61 left_right:1,
62 rewrite_parents:1,
63 print_parents:1,
@@ -131,6 +132,10 @@ struct rev_info {
132
133 /* notes-specific options: which refs to show */
134 struct display_notes_opt notes_opt;
135 +
136 + /* commit counts */
137 + int count_left;
138 + int count_right;
139 };
140
141 #define REV_TREE_SAME 0
t/t6007-rev-list-cherry-pick-file.sh
+29
@@ -32,6 +32,23 @@ test_expect_success setup '
32 git tag B
33 '
34
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 +
52 test_expect_success '--cherry-pick foo comes up empty' '
53 test -z "$(git rev-list --left-right --cherry-pick B...C -- foo)"
54 '
@@ -54,4 +71,16 @@ test_expect_success '--cherry-pick with independent, but identical branches' '
71 HEAD...master -- foo)"
72 '
73
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 +
86 test_done