git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH&RFC] get_short_ref(): add strict mode

From
Bert Wesarg <bert.wesarg@googlemail.com>
Date
Apr 11, 2009, 17:14 UTC
Message-ID
<1239470086-13818-1-git-send-email-bert.wesarg@googlemail.com>
In-Reply-To
<36ca99e90810172355n2c578b07pd2868b15e6631ef7@mail.gmail.com>

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

The only user of this function ("refname:short") still uses the loose mode.
Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>
---
Cc: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org

I think of 3 alternatives to use this mode for the "refname" format (and probably others):

  a) Use core.warnAmbiguousRefs to control strict mode.
     This would change the current default behaviour, because this is true
     by default.
  b) Introduce a new core config variable to control this, either for
     for-each-ref alone ore globally.
  c) Introduce a "refname:short-strict" format to get the strict abbreviation.
I'm currently slighty in favour for option b).

Regards, Bert

 builtin-for-each-ref.c |   22 +++++++++++++++++-----
 1 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index 5cbb4b0..2f323c6 100644
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
@@ -569,7 +569,7 @@ static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
 /*
  * Shorten the refname to an non-ambiguous form
  */
-static char *get_short_ref(struct refinfo *ref)
+static char *get_short_ref(struct refinfo *ref, int strict)
 {
 	int i;
 	static char **scanf_fmts;
@@ -606,6 +606,7 @@ static char *get_short_ref(struct refinfo *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->refname, scanf_fmts[i], short_name))
@@ -614,14 +615,25 @@ static char *get_short_ref(struct refinfo *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
@@ -635,9 +647,9 @@ static char *get_short_ref(struct refinfo *ref)
 
 		/*
 		 * short name is non-ambiguous if all previous rules
-		 * haven't resolved to a valid ref
+		 * doesn't resolved to a valid ref
 		 */
-		if (j == i)
+		if (j == rules_to_fail)
 			return short_name;
 	}
 
@@ -684,7 +696,7 @@ static void populate_value(struct refinfo *ref)
 			if (formatp) {
 				formatp++;
 				if (!strcmp(formatp, "short"))
-					refname = get_short_ref(ref);
+					refname = get_short_ref(ref, 0);
 				else
 					die("unknown refname format %s",
 					    formatp);
-- 
tg: (e37347b..) bw/short_ref-warnAmbiguousRefs (depends on: master)
Previous: Bert WesargNext: Junio C Hamano
Message 13 of 16 in “for-each-ref: utilize core.warnambiguousrefs for strict refname:short format”
  1. 1/3 for-each-ref: utilize core.warnambiguousrefs for strict refname:short formatBert Wesarg, Sep 22, 2008
  2. 2/3 for-each-ref: factor out get_short_ref() into refs.c:abbreviate_refname()Bert Wesarg, Sep 22, 2008
  3. 3/3 git abbref-ref: new porcelain for abbreviate_ref()Bert Wesarg, Sep 22, 2008
  4. Shawn O. PearceSep 22, 2008
  5. Junio C HamanoSep 22, 2008
  6. Bert WesargSep 22, 2008
  7. Bert WesargSep 22, 2008
  8. Junio C HamanoSep 22, 2008
  9. Bert WesargSep 22, 2008
  10. Junio C HamanoOct 17, 2008
  11. Shawn O. PearceOct 18, 2008
  12. Bert WesargOct 18, 2008
  13. get_short_ref(): add strict modeBert Wesarg, Apr 11, 2009
  14. Junio C HamanoApr 11, 2009
  15. Bert WesargApr 11, 2009
  16. for-each-ref: refname:short utilize core.warnAmbiguousRefsBert Wesarg, Apr 11, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.