[PATCH&RFC] get_short_ref(): add strict mode
From: Bert Wesarg <hidden>
Date: 2016-06-15 22:46:35
Subsystem:
the rest · Maintainer:
Linus Torvalds
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 <redacted>
---
Cc: Junio C Hamano <redacted>
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)