git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH/RFC] Implemented return value for rev-list --quiet
@ 2011-04-26 22:36 Jonas Gehring
  2011-04-26 23:02 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Jonas Gehring @ 2011-04-26 22:36 UTC (permalink / raw
  To: git; +Cc: gitster

If --quiet is given, the program will return non-zero if the traversed
commit set was empty. This way, rev-list can be used to check commit
ancestry as described by the documentation for --quiet.

"Non-zero" is implemented as 1 in order to avoid confusion with the
usual 128 for die_builtin().

Signed-off-by: Jonas Gehring <jonas.gehring@boolsoft.org>
---
 builtin/rev-list.c        |    8 ++++++++
 revision.h                |    1 +
 t/t6041-rev-list-quiet.sh |   27 +++++++++++++++++++++++++++
 3 files changed, 36 insertions(+), 0 deletions(-)
 create mode 100755 t/t6041-rev-list-quiet.sh

diff --git a/builtin/rev-list.c b/builtin/rev-list.c
index 9bfb942..001d6af 100644
--- a/builtin/rev-list.c
+++ b/builtin/rev-list.c
@@ -157,12 +157,17 @@ static void show_commit(struct commit *commit, void *data)
 
 static void finish_commit(struct commit *commit, void *data)
 {
+	struct rev_list_info *info = data;
+	struct rev_info *revs = info->revs;
+
 	if (commit->parents) {
 		free_commit_list(commit->parents);
 		commit->parents = NULL;
 	}
 	free(commit->buffer);
 	commit->buffer = NULL;
+
+	revs->count_finished++;
 }
 
 static void finish_object(struct object *obj, const struct name_path *path, const char *name)
@@ -412,5 +417,8 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
 			printf("%d\n", revs.count_left + revs.count_right);
 	}
 
+	if (quiet)
+		/* Return non-zero if no commits have been traversed. */
+		return (revs.count_finished == 0 ? 1 : 0);
 	return 0;
 }
diff --git a/revision.h b/revision.h
index 9fd8f30..ddc35d2 100644
--- a/revision.h
+++ b/revision.h
@@ -141,6 +141,7 @@ struct rev_info {
 	/* commit counts */
 	int count_left;
 	int count_right;
+	int count_finished;
 };
 
 #define REV_TREE_SAME		0
diff --git a/t/t6041-rev-list-quiet.sh b/t/t6041-rev-list-quiet.sh
new file mode 100755
index 0000000..6cb9120
--- /dev/null
+++ b/t/t6041-rev-list-quiet.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+test_description='Non-zero return value for empty commit sets and --quiet'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	echo >fileA fileA
+	git add fileA
+	git commit -a -m "Initial"
+	echo >fileA fileA modified
+	git commit -a -m "fileA modified"
+'
+
+test_expect_success 'not quiet' '
+	test $(git rev-list master | wc -l) = 2 &&
+	test $(git rev-list master..master^ | wc -l) = 0 &&
+	test $(git rev-list master^..master | wc -l) = 1
+'
+
+test_expect_success '--quiet' '
+	test $(git rev-list --quiet master; echo $?) = 0 &&
+	test $(git rev-list --quiet master..master^; echo $?) != 0 &&
+	test $(git rev-list --quiet master^..master; echo $?) = 0
+'
+
+test_done
-- 
1.7.4.4

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH/RFC] Implemented return value for rev-list --quiet
  2011-04-26 22:36 [PATCH/RFC] Implemented return value for rev-list --quiet Jonas Gehring
@ 2011-04-26 23:02 ` Junio C Hamano
  2011-04-26 23:31   ` Jonas Gehring
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2011-04-26 23:02 UTC (permalink / raw
  To: Jonas Gehring; +Cc: git

Jonas Gehring <jonas.gehring@boolsoft.org> writes:

> If --quiet is given, the program will return non-zero if the traversed
> commit set was empty. This way, rev-list can be used to check commit
> ancestry as described by the documentation for --quiet.

Given two commits A and X, "rev-list --objects A..X" is a way to make sure
that everything between A and X exists.  When your ref is at A, you are
trying to fetch from a remote that wants to update you to X, and when you
happen to have X already, you run that command and see if it dies due to
disconnect in the history.  If it doesn't, you know you do not actually
have to transfer anything (this is called quickfetch test).  For the
purpose of this test, the caller is not interested in the output, so it is
perfectly OK to give --quiet to the command.

In reality, you would feed all the refs you locally have on the negative
side (i.e. "rev-list --objects --quiet X --not A B C D ...") to check if X
is connected to something you know you have a connected history behind it.
Returning non-zero to a quickfetch test when X is reachable from some of
your refs will break existing scripts.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH/RFC] Implemented return value for rev-list --quiet
  2011-04-26 23:02 ` Junio C Hamano
@ 2011-04-26 23:31   ` Jonas Gehring
  2011-04-26 23:47     ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Jonas Gehring @ 2011-04-26 23:31 UTC (permalink / raw
  To: Junio C Hamano; +Cc: git

On 04/27/2011 01:02 AM, Junio C Hamano wrote:
> Given two commits A and X, "rev-list --objects A..X" is a way to make sure
> that everything between A and X exists.

Thank you for this explanation, this really cleared up the intended
usage of --quiet for me. I wanted to use it for ancestry testing of two
commits, noticed the hard-coded return value of '0' in cmd_rev_list()
and thought that the implementation is probably missing.

After some additional digging, I realized that git-merge-base is the
right tool for my task.

Regards,
Jonas

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH/RFC] Implemented return value for rev-list --quiet
  2011-04-26 23:31   ` Jonas Gehring
@ 2011-04-26 23:47     ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2011-04-26 23:47 UTC (permalink / raw
  To: Jonas Gehring; +Cc: git

Jonas Gehring <jonas.gehring@boolsoft.org> writes:

> Thank you for this explanation, this really cleared up the intended
> ... I wanted to use it for ancestry testing of two
> commits,...
>
> After some additional digging, I realized that git-merge-base is the
> right tool for my task.

Either

	test "$(git merge-base A B)" = "$(git rev-parse A)"

or

	test -z "$(git rev-list -1 A..B)"

would be valid ways to see if A is an ancestor of B.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-04-26 23:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-26 22:36 [PATCH/RFC] Implemented return value for rev-list --quiet Jonas Gehring
2011-04-26 23:02 ` Junio C Hamano
2011-04-26 23:31   ` Jonas Gehring
2011-04-26 23:47     ` Junio C Hamano

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).