git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Thomas Rast <trast@student.ethz.ch>
To: Andrew Sayers <andrew-git@pileofstuff.org>
Cc: "Shawn O. Pearce" <spearce@spearce.org>,
	git@vger.kernel.org, "John Tapsell" <johnflux@gmail.com>,
	"Steven Michalske" <smichalske@gmail.com>,
	"Michael J Gruber" <git@drmicha.warpmail.net>,
	"SZEDER Gábor" <szeder@ira.uka.de>
Subject: [PATCH v2 1/2] rev-list: introduce --count option
Date: Sat, 12 Jun 2010 11:59:10 +0200	[thread overview]
Message-ID: <74f186ce201602262fe3df71d4fa22e8184608ec.1276336602.git.trast@student.ethz.ch> (raw)
In-Reply-To: <cover.1276336602.git.trast@student.ethz.ch>

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>
---
 Documentation/rev-list-options.txt   |    9 +++++++++
 builtin/rev-list.c                   |   16 ++++++++++++++++
 revision.c                           |    2 ++
 revision.h                           |    5 +++++
 t/t6007-rev-list-cherry-pick-file.sh |   29 +++++++++++++++++++++++++++++
 5 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/Documentation/rev-list-options.txt b/Documentation/rev-list-options.txt
index b9fb7a8..066ade9 100644
--- a/Documentation/rev-list-options.txt
+++ b/Documentation/rev-list-options.txt
@@ -98,6 +98,15 @@ you would get an output like this:
 This implies the '--topo-order' option by default, but the
 '--date-order' option may also be specified.
 
+ifdef::git-rev-list[]
+--count::
+	Print a number stating how many commits would have been
+	listed, and suppress all other output.  When used together
+	with '--left-right', instead print the counts for left and
+	right commits, separated by a tab.
+endif::git-rev-list[]
+
+
 ifndef::git-rev-list[]
 Diff Formatting
 ~~~~~~~~~~~~~~~
diff --git a/builtin/rev-list.c b/builtin/rev-list.c
index 51ceb19..efe9360 100644
--- a/builtin/rev-list.c
+++ b/builtin/rev-list.c
@@ -50,6 +50,15 @@ static void show_commit(struct commit *commit, void *data)
 
 	graph_show_commit(revs->graph);
 
+	if (revs->count) {
+		if (commit->object.flags & SYMMETRIC_LEFT)
+			revs->count_left++;
+		else
+			revs->count_right++;
+		finish_commit(commit, data);
+		return;
+	}
+
 	if (info->show_timestamp)
 		printf("%lu ", commit->date);
 	if (info->header_prefix)
@@ -400,5 +409,12 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
 			     quiet ? finish_object : show_object,
 			     &info);
 
+	if (revs.count) {
+		if (revs.left_right)
+			printf("%d\t%d\n", revs.count_left, revs.count_right);
+		else
+			printf("%d\n", revs.count_left + revs.count_right);
+	}
+
 	return 0;
 }
diff --git a/revision.c b/revision.c
index f4b8b38..21b133c 100644
--- a/revision.c
+++ b/revision.c
@@ -1146,6 +1146,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 		revs->boundary = 1;
 	} else if (!strcmp(arg, "--left-right")) {
 		revs->left_right = 1;
+	} else if (!strcmp(arg, "--count")) {
+		revs->count = 1;
 	} else if (!strcmp(arg, "--cherry-pick")) {
 		revs->cherry_pick = 1;
 		revs->limited = 1;
diff --git a/revision.h b/revision.h
index 568f1c9..bafa728 100644
--- a/revision.h
+++ b/revision.h
@@ -57,6 +57,7 @@ struct rev_info {
 			limited:1,
 			unpacked:1,
 			boundary:2,
+			count:1,
 			left_right:1,
 			rewrite_parents:1,
 			print_parents:1,
@@ -131,6 +132,10 @@ struct rev_info {
 
 	/* notes-specific options: which refs to show */
 	struct display_notes_opt notes_opt;
+
+	/* commit counts */
+	int count_left;
+	int count_right;
 };
 
 #define REV_TREE_SAME		0
diff --git a/t/t6007-rev-list-cherry-pick-file.sh b/t/t6007-rev-list-cherry-pick-file.sh
index 4b8611c..b565638 100755
--- a/t/t6007-rev-list-cherry-pick-file.sh
+++ b/t/t6007-rev-list-cherry-pick-file.sh
@@ -32,6 +32,23 @@ test_expect_success setup '
 	git tag B
 '
 
+cat >expect <<EOF
+<tags/B
+>tags/C
+EOF
+
+test_expect_success '--left-right' '
+	git rev-list --left-right B...C > actual &&
+	git name-rev --stdin --name-only --refs="*tags/*" \
+		< actual > actual.named &&
+	test_cmp actual.named expect
+'
+
+test_expect_success '--count' '
+	git rev-list --count B...C > actual &&
+	test "$(cat actual)" = 2
+'
+
 test_expect_success '--cherry-pick foo comes up empty' '
 	test -z "$(git rev-list --left-right --cherry-pick B...C -- foo)"
 '
@@ -54,4 +71,16 @@ test_expect_success '--cherry-pick with independent, but identical branches' '
 		HEAD...master -- foo)"
 '
 
+cat >expect <<EOF
+1	2
+EOF
+
+# Insert an extra commit to break the symmetry
+test_expect_success '--count --left-right' '
+	git checkout branch &&
+	test_commit D &&
+	git rev-list --count --left-right B...D > actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
1.7.1.561.g94582

  reply	other threads:[~2010-06-12  9:59 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-06  0:05 [PATCH] bash completion: Support "unpushed commits" warnings in __git_ps1 Andrew Sayers
2010-06-06 18:14 ` Thomas Rast
2010-06-06 20:49   ` Andrew Sayers
2010-06-06 21:07     ` Jakub Narebski
2010-06-06 22:19       ` Andrew Sayers
2010-06-07  7:42     ` Thomas Rast
2010-06-08 21:36       ` [RFC/PATCHv2] bash completion: Support "divergence from upstream" " Andrew Sayers
2010-06-09  8:21         ` Peter Kjellerstedt
2010-06-09  8:45         ` John Tapsell
2010-06-09 21:02           ` Steven Michalske
2010-06-09  9:17         ` Michael J Gruber
2010-06-09 20:48           ` Michael J Gruber
2010-06-09 21:03             ` Michael J Gruber
2010-06-10 11:47         ` [PATCH 0/2] " Thomas Rast
2010-06-10 11:47           ` [PATCH 1/2] rev-list: introduce --count option Thomas Rast
2010-06-10 11:47           ` [PATCH 2/2] bash completion: Support "divergence from upstream" warnings in __git_ps1 Thomas Rast
2010-06-12  0:00             ` SZEDER Gábor
2010-06-12 10:03               ` [PATCH v2 0/2] " Thomas Rast
2010-06-12  9:59                 ` Thomas Rast [this message]
2010-06-12  9:59                 ` [PATCH v2 2/2] " Thomas Rast
2010-06-14  3:13                   ` Junio C Hamano
2010-06-14  7:44                     ` Thomas Rast
2010-06-14 12:36                   ` SZEDER Gábor
2010-06-12 10:11                 ` vger doesn't like UTF-8 from send-email Thomas Rast
2010-06-12 15:06                   ` [PATCH] send-email: ask about and declare 8bit mails Thomas Rast
2010-06-12 16:28                     ` Junio C Hamano
2010-06-13 15:09                       ` Thomas Rast
2010-06-13  4:15                   ` vger doesn't like UTF-8 from send-email Michael Witten
2010-06-14 11:57                     ` Erik Faye-Lund
2010-06-12 20:50                 ` [PATCH] bash completion: Support "divergence from upstream" messages in __git_ps1 Andrew Sayers
2010-06-14  7:42                   ` Thomas Rast
2010-06-15 21:50                     ` [PATCHv4] " Andrew Sayers
2010-06-16 19:05                       ` Junio C Hamano
2010-06-16 19:11                         ` Thomas Rast
2010-06-17 21:31                         ` [PATCHv5 0/2] " Andrew Sayers
2010-06-18 16:10                           ` Junio C Hamano
2010-06-18 21:02                             ` Andrew Sayers
2010-06-17 21:32                         ` [PATCHv5 1/2] " Andrew Sayers
2010-06-17 21:32                         ` [PATCHv5 2/2] bash-completion: Fix __git_ps1 to work with "set -u" Andrew Sayers
2010-06-10 13:31           ` [PATCH 0/2] bash completion: Support "divergence from upstream" warnings in __git_ps1 Michael J Gruber
2010-06-10 12:03         ` [RFC/PATCHv2] " Thomas Rast
2010-06-06 20:12 ` [PATCH] bash completion: Support "unpushed commits" " Thomas Rast

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=74f186ce201602262fe3df71d4fa22e8184608ec.1276336602.git.trast@student.ethz.ch \
    --to=trast@student.ethz.ch \
    --cc=andrew-git@pileofstuff.org \
    --cc=git@drmicha.warpmail.net \
    --cc=git@vger.kernel.org \
    --cc=johnflux@gmail.com \
    --cc=smichalske@gmail.com \
    --cc=spearce@spearce.org \
    --cc=szeder@ira.uka.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).