shorten_unambiguous_ref(): add strict mode
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Bert Wesarg committed
Apr 13, 2009 at 12:25 UTC
6e7b3309d356077337b8222683a743c27fa7276c
4 files changed
+19
-7
builtin-branch.c
+2
-2
@@ -311,14 +311,14 @@ static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
311
if (branch && branch->merge && branch->merge[0]->dst &&
312
show_upstream_ref)
313
strbuf_addf(stat, "[%s] ",
314
- shorten_unambiguous_ref(branch->merge[0]->dst));
314
+ shorten_unambiguous_ref(branch->merge[0]->dst, 0));
315
return;
316
}
317
318
strbuf_addch(stat, '[');
319
if (show_upstream_ref)
320
strbuf_addf(stat, "%s: ",
321
- shorten_unambiguous_ref(branch->merge[0]->dst));
321
+ shorten_unambiguous_ref(branch->merge[0]->dst, 0));
322
if (!ours)
323
strbuf_addf(stat, "behind %d] ", theirs);
324
else if (!theirs)
builtin-for-each-ref.c
+1
-1
@@ -601,7 +601,7 @@ static void populate_value(struct refinfo *ref)
601
if (formatp) {
602
formatp++;
603
if (!strcmp(formatp, "short"))
604
- refname = shorten_unambiguous_ref(refname);
604
+ refname = shorten_unambiguous_ref(refname, 0);
605
else
606
die("unknown %.*s format %s",
607
(int)(formatp - name), name, formatp);
refs.c
+15
-3
@@ -1681,7 +1681,7 @@ static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
1681
return;
1682
}
1683
1684
-char *shorten_unambiguous_ref(const char *ref)
1684
+char *shorten_unambiguous_ref(const char *ref, int strict)
1685
{
1686
int i;
1687
static char **scanf_fmts;
@@ -1718,6 +1718,7 @@ char *shorten_unambiguous_ref(const char *ref)
1718
/* skip first rule, it will always match */
1719
for (i = nr_rules - 1; i > 0 ; --i) {
1720
int j;
1721
+ int rules_to_fail = i;
1722
int short_name_len;
1723
1724
if (1 != sscanf(ref, scanf_fmts[i], short_name))
@@ -1725,15 +1726,26 @@ char *shorten_unambiguous_ref(const char *ref)
1726
1727
short_name_len = strlen(short_name);
1728
1729
+ /*
1730
+ * in strict mode, all (except the matched one) rules
1731
+ * must fail to resolve to a valid non-ambiguous ref
1732
+ */
1733
+ if (strict)
1734
+ rules_to_fail = nr_rules;
1735
+
1736
/*
1737
* check if the short name resolves to a valid ref,
1738
* but use only rules prior to the matched one
1739
*/
1732
- for (j = 0; j < i; j++) {
1740
+ for (j = 0; j < rules_to_fail; j++) {
1741
const char *rule = ref_rev_parse_rules[j];
1742
unsigned char short_objectname[20];
1743
char refname[PATH_MAX];
1744
1745
+ /* skip matched rule */
1746
+ if (i == j)
1747
+ continue;
1748
+
1749
/*
1750
* the short name is ambiguous, if it resolves
1751
* (with this previous rule) to a valid ref
@@ -1749,7 +1761,7 @@ char *shorten_unambiguous_ref(const char *ref)
1761
* short name is non-ambiguous if all previous rules
1762
* haven't resolved to a valid ref
1763
*/
1752
- if (j == i)
1764
+ if (j == rules_to_fail)
1765
return short_name;
1766
}
1767
refs.h
+1
-1
@@ -81,7 +81,7 @@ extern int for_each_reflog(each_ref_fn, void *);
81
extern int check_ref_format(const char *target);
82
83
extern const char *prettify_ref(const struct ref *ref);
84
-extern char *shorten_unambiguous_ref(const char *ref);
84
+extern char *shorten_unambiguous_ref(const char *ref, int strict);
85
86
/** rename ref, return 0 on success **/
87
extern int rename_ref(const char *oldref, const char *newref, const char *logmsg);