git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 1/2] shorten_unambiguous_ref(): add strict mode
@ 2009-04-13 10:25 Bert Wesarg
  2009-04-13 10:25 ` [PATCH 2/2] for-each-ref: utilize core.warnAmbiguousRefs for :short-format Bert Wesarg
  2009-04-13 11:20 ` [RFC/PATCH 3/2] rev-parse: --abbrev-ref option to shorten ref name Bert Wesarg
  0 siblings, 2 replies; 4+ messages in thread
From: Bert Wesarg @ 2009-04-13 10:25 UTC (permalink / raw
  To: Junio C Hamano; +Cc: Bert Wesarg, Jeff King, git

Add the strict mode of abbreviation to shorten_unambiguous_ref(), i.e. the
resulting ref won't trigger the ambiguous ref warning.

All users of shorten_unambiguous_ref() still use the loose mode.

Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
---
Cc: Jeff King <peff@peff.net>
Cc: git@vger.kernel.org

 builtin-branch.c       |    4 ++--
 builtin-for-each-ref.c |    2 +-
 refs.c                 |   18 +++++++++++++++---
 refs.h                 |    2 +-
 4 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/builtin-branch.c b/builtin-branch.c
index 3275821..91098ca 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -311,14 +311,14 @@ static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
 		if (branch && branch->merge && branch->merge[0]->dst &&
 		    show_upstream_ref)
 			strbuf_addf(stat, "[%s] ",
-			    shorten_unambiguous_ref(branch->merge[0]->dst));
+			    shorten_unambiguous_ref(branch->merge[0]->dst, 0));
 		return;
 	}
 
 	strbuf_addch(stat, '[');
 	if (show_upstream_ref)
 		strbuf_addf(stat, "%s: ",
-			shorten_unambiguous_ref(branch->merge[0]->dst));
+			shorten_unambiguous_ref(branch->merge[0]->dst, 0));
 	if (!ours)
 		strbuf_addf(stat, "behind %d] ", theirs);
 	else if (!theirs)
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index c8114c8..cfff686 100644
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
@@ -601,7 +601,7 @@ static void populate_value(struct refinfo *ref)
 		if (formatp) {
 			formatp++;
 			if (!strcmp(formatp, "short"))
-				refname = shorten_unambiguous_ref(refname);
+				refname = shorten_unambiguous_ref(refname, 0);
 			else
 				die("unknown %.*s format %s",
 				    (int)(formatp - name), name, formatp);
diff --git a/refs.c b/refs.c
index 82afb87..e65a3b4 100644
--- a/refs.c
+++ b/refs.c
@@ -1681,7 +1681,7 @@ static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
 	return;
 }
 
-char *shorten_unambiguous_ref(const char *ref)
+char *shorten_unambiguous_ref(const char *ref, int strict)
 {
 	int i;
 	static char **scanf_fmts;
@@ -1718,6 +1718,7 @@ char *shorten_unambiguous_ref(const char *ref)
 	/* skip first rule, it will always match */
 	for (i = nr_rules - 1; i > 0 ; --i) {
 		int j;
+		int rules_to_fail = i;
 		int short_name_len;
 
 		if (1 != sscanf(ref, scanf_fmts[i], short_name))
@@ -1726,14 +1727,25 @@ char *shorten_unambiguous_ref(const char *ref)
 		short_name_len = strlen(short_name);
 
 		/*
+		 * in strict mode, all (except the matched one) rules
+		 * must fail to resolve to a valid non-ambiguous ref
+		 */
+		if (strict)
+			rules_to_fail = nr_rules;
+
+		/*
 		 * check if the short name resolves to a valid ref,
 		 * but use only rules prior to the matched one
 		 */
-		for (j = 0; j < i; j++) {
+		for (j = 0; j < rules_to_fail; j++) {
 			const char *rule = ref_rev_parse_rules[j];
 			unsigned char short_objectname[20];
 			char refname[PATH_MAX];
 
+			/* skip matched rule */
+			if (i == j)
+				continue;
+
 			/*
 			 * the short name is ambiguous, if it resolves
 			 * (with this previous rule) to a valid ref
@@ -1749,7 +1761,7 @@ char *shorten_unambiguous_ref(const char *ref)
 		 * short name is non-ambiguous if all previous rules
 		 * haven't resolved to a valid ref
 		 */
-		if (j == i)
+		if (j == rules_to_fail)
 			return short_name;
 	}
 
diff --git a/refs.h b/refs.h
index 50abbbb..29d17a4 100644
--- a/refs.h
+++ b/refs.h
@@ -81,7 +81,7 @@ extern int for_each_reflog(each_ref_fn, void *);
 extern int check_ref_format(const char *target);
 
 extern const char *prettify_ref(const struct ref *ref);
-extern char *shorten_unambiguous_ref(const char *ref);
+extern char *shorten_unambiguous_ref(const char *ref, int strict);
 
 /** rename ref, return 0 on success **/
 extern int rename_ref(const char *oldref, const char *newref, const char *logmsg);
-- 
tg: (c965c02..) bw/short_ref-warnAmbiguousRefs (depends on: master)

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

* [PATCH 2/2] for-each-ref: utilize core.warnAmbiguousRefs for :short-format
  2009-04-13 10:25 [PATCH 1/2] shorten_unambiguous_ref(): add strict mode Bert Wesarg
@ 2009-04-13 10:25 ` Bert Wesarg
  2009-04-13 11:20 ` [RFC/PATCH 3/2] rev-parse: --abbrev-ref option to shorten ref name Bert Wesarg
  1 sibling, 0 replies; 4+ messages in thread
From: Bert Wesarg @ 2009-04-13 10:25 UTC (permalink / raw
  To: Junio C Hamano; +Cc: Bert Wesarg, Jeff King, git, szeder, Shawn O. Pearce

core.warnAmbiguousRefs is used to select strict mode for the
abbreviation for the ":short" format specifier of "refname" and "upstream".

In strict mode, the abbreviated ref will never trigger the
'warn_ambiguous_refs' warning. I.e. for these refs:

  refs/heads/xyzzy
  refs/tags/xyzzy

the abbreviated forms are:

  heads/xyzzy
  tags/xyzzy

Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
---

Cc: "Jeff King" <peff@peff.net>
Cc: git@vger.kernel.org
Cc: szeder@ira.uka.de
Cc: "Shawn O. Pearce" <spearce@spearce.org>

 Documentation/git-for-each-ref.txt |    2 ++
 builtin-for-each-ref.c             |    6 +++++-
 t/t6300-for-each-ref.sh            |   18 +++++++++++++++---
 3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
index b362e9e..8dc873f 100644
--- a/Documentation/git-for-each-ref.txt
+++ b/Documentation/git-for-each-ref.txt
@@ -75,6 +75,8 @@ For all objects, the following names can be used:
 refname::
 	The name of the ref (the part after $GIT_DIR/).
 	For a non-ambiguous short name of the ref append `:short`.
+	The option core.warnAmbiguousRefs is used to select the strict
+	abbreviation mode.
 
 objecttype::
 	The type of the object (`blob`, `tree`, `commit`, `tag`).
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index cfff686..91e8f95 100644
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
@@ -601,7 +601,8 @@ static void populate_value(struct refinfo *ref)
 		if (formatp) {
 			formatp++;
 			if (!strcmp(formatp, "short"))
-				refname = shorten_unambiguous_ref(refname, 0);
+				refname = shorten_unambiguous_ref(refname,
+						      warn_ambiguous_refs);
 			else
 				die("unknown %.*s format %s",
 				    (int)(formatp - name), name, formatp);
@@ -917,6 +918,9 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
 		sort = default_sort();
 	sort_atom_limit = used_atom_cnt;
 
+	/* for warn_ambiguous_refs */
+	git_config(git_default_config, NULL);
+
 	memset(&cbdata, 0, sizeof(cbdata));
 	cbdata.grab_pattern = argv;
 	for_each_ref(grab_single_ref, &cbdata);
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index daf02d5..8052c86 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -301,10 +301,11 @@ test_expect_success 'Check for invalid refname format' '
 
 cat >expected <<\EOF
 heads/master
-master
+tags/master
 EOF
 
-test_expect_success 'Check ambiguous head and tag refs' '
+test_expect_success 'Check ambiguous head and tag refs (strict)' '
+	git config --bool core.warnambiguousrefs true &&
 	git checkout -b newtag &&
 	echo "Using $datestamp" > one &&
 	git add one &&
@@ -316,11 +317,22 @@ test_expect_success 'Check ambiguous head and tag refs' '
 '
 
 cat >expected <<\EOF
+heads/master
+master
+EOF
+
+test_expect_success 'Check ambiguous head and tag refs (loose)' '
+	git config --bool core.warnambiguousrefs false &&
+	git for-each-ref --format "%(refname:short)" refs/heads/master refs/tags/master >actual &&
+	test_cmp expected actual
+'
+
+cat >expected <<\EOF
 heads/ambiguous
 ambiguous
 EOF
 
-test_expect_success 'Check ambiguous head and tag refs II' '
+test_expect_success 'Check ambiguous head and tag refs II (loose)' '
 	git checkout master &&
 	git tag ambiguous testtag^0 &&
 	git branch ambiguous testtag^0 &&
-- 
tg: (c4ae76d..) bw/utilize-it (depends on: bw/short_ref-warnAmbiguousRefs)

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

* [RFC/PATCH 3/2] rev-parse: --abbrev-ref option to shorten ref name
  2009-04-13 10:25 [PATCH 1/2] shorten_unambiguous_ref(): add strict mode Bert Wesarg
  2009-04-13 10:25 ` [PATCH 2/2] for-each-ref: utilize core.warnAmbiguousRefs for :short-format Bert Wesarg
@ 2009-04-13 11:20 ` Bert Wesarg
  2009-04-13 16:43   ` Junio C Hamano
  1 sibling, 1 reply; 4+ messages in thread
From: Bert Wesarg @ 2009-04-13 11:20 UTC (permalink / raw
  To: Junio C Hamano; +Cc: Bert Wesarg, Jeff King, git

This applies the shorten_unambiguous_ref function to the object name.
Default mode is controlled by core.warnAmbiguousRefs. Else it is given as
optional argument to --abbrev-ref={strict|loose}.

This should be faster than 'git for-each-ref --format="%(refname:short)" <ref>'
for single refs.

Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
---
Cc: Jeff King <peff@peff.net>
Cc: git@vger.kernel.org

Can someone check, if I need to alter the filter, thanks.

Regards,
Bert

 Documentation/git-rev-parse.txt |    5 +++++
 builtin-rev-parse.c             |   23 +++++++++++++++++++++--
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt
index 5ed2bc8..fba30b1 100644
--- a/Documentation/git-rev-parse.txt
+++ b/Documentation/git-rev-parse.txt
@@ -84,6 +84,11 @@ OPTIONS
 	unfortunately named tag "master"), and show them as full
 	refnames (e.g. "refs/heads/master").
 
+--abbrev-ref[={strict|loose}]::
+	A non-ambiguous short name of the objects name.
+	The option core.warnAmbiguousRefs is used to select the strict
+	abbreviation mode.
+
 --all::
 	Show all refs found in `$GIT_DIR/refs`.
 
diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c
index 81d5a6f..22c6d6a 100644
--- a/builtin-rev-parse.c
+++ b/builtin-rev-parse.c
@@ -26,6 +26,8 @@ static int show_type = NORMAL;
 #define SHOW_SYMBOLIC_FULL 2
 static int symbolic;
 static int abbrev;
+static int abbrev_ref;
+static int abbrev_ref_strict;
 static int output_sq;
 
 /*
@@ -109,8 +111,8 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
 		return;
 	def = NULL;
 
-	if (symbolic && name) {
-		if (symbolic == SHOW_SYMBOLIC_FULL) {
+	if ((symbolic || abbrev_ref) && name) {
+		if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
 			unsigned char discard[20];
 			char *full;
 
@@ -125,6 +127,9 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
 				 */
 				break;
 			case 1: /* happy */
+				if (abbrev_ref)
+					full = shorten_unambiguous_ref(full,
+						abbrev_ref_strict);
 				show_with_type(type, full);
 				break;
 			default: /* ambiguous */
@@ -506,6 +511,20 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				symbolic = SHOW_SYMBOLIC_FULL;
 				continue;
 			}
+			if (!prefixcmp(arg, "--abbrev-ref") &&
+			    (!arg[12] || arg[12] == '=')) {
+				abbrev_ref = 1;
+				abbrev_ref_strict = warn_ambiguous_refs;
+				if (arg[12] == '=') {
+					if (!strcmp(arg + 13, "strict"))
+						abbrev_ref_strict = 1;
+					else if (!strcmp(arg + 13, "loose"))
+						abbrev_ref_strict = 0;
+					else
+						die("unknown mode for %s", arg);
+				}
+				continue;
+			}
 			if (!strcmp(arg, "--all")) {
 				for_each_ref(show_reference, NULL);
 				continue;
-- 
tg: (5fec79c..) bw/rev-parse_--abbrev_ref (depends on: bw/utilize-it)

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

* Re: [RFC/PATCH 3/2] rev-parse: --abbrev-ref option to shorten ref name
  2009-04-13 11:20 ` [RFC/PATCH 3/2] rev-parse: --abbrev-ref option to shorten ref name Bert Wesarg
@ 2009-04-13 16:43   ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2009-04-13 16:43 UTC (permalink / raw
  To: Bert Wesarg; +Cc: Jeff King, git

Bert Wesarg <bert.wesarg@googlemail.com> writes:

> This applies the shorten_unambiguous_ref function to the object name.
> Default mode is controlled by core.warnAmbiguousRefs. Else it is given as
> optional argument to --abbrev-ref={strict|loose}.
>
> This should be faster than 'git for-each-ref --format="%(refname:short)" <ref>'
> for single refs.
>
> Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
> ---
> Cc: Jeff King <peff@peff.net>
> Cc: git@vger.kernel.org
>
> Can someone check, if I need to alter the filter, thanks.

I do not think so.

This --abbrev-ref option is similar to --symbolic in that how a ref from
the command line, if any, is output, and should not change the filter at
all.  For example, "rev-parse --abbrev-ref master -M" shouldn't filter -M
out by implying --revs-only.

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

end of thread, other threads:[~2009-04-13 16:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-13 10:25 [PATCH 1/2] shorten_unambiguous_ref(): add strict mode Bert Wesarg
2009-04-13 10:25 ` [PATCH 2/2] for-each-ref: utilize core.warnAmbiguousRefs for :short-format Bert Wesarg
2009-04-13 11:20 ` [RFC/PATCH 3/2] rev-parse: --abbrev-ref option to shorten ref name Bert Wesarg
2009-04-13 16:43   ` 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).