diff options
author | Junio C Hamano <gitster@pobox.com> | 2009-04-17 21:42:12 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2009-04-17 21:42:12 -0700 |
commit | a9b772a0116869f0f93e57ca0b49fd66325bb04a (patch) | |
tree | 21ff148b24d3a0239d5a68a647a6884569a4a5d2 | |
parent | bd15ef078afac3f0f147e815d3b85d6d2a48dd1a (diff) | |
parent | 45972ffbedf8175555434006595c71a5b0e643f1 (diff) | |
download | git-a9b772a0116869f0f93e57ca0b49fd66325bb04a.tar.gz git-a9b772a0116869f0f93e57ca0b49fd66325bb04a.tar.xz |
Merge branch 'bw/short-ref-strict'
* bw/short-ref-strict:
remote.c: use shorten_unambiguous_ref
rev-parse: --abbrev-ref option to shorten ref name
for-each-ref: utilize core.warnAmbiguousRefs for :short-format
shorten_unambiguous_ref(): add strict mode
-rw-r--r-- | Documentation/git-for-each-ref.txt | 2 | ||||
-rw-r--r-- | Documentation/git-rev-parse.txt | 5 | ||||
-rw-r--r-- | builtin-branch.c | 4 | ||||
-rw-r--r-- | builtin-for-each-ref.c | 6 | ||||
-rw-r--r-- | builtin-rev-parse.c | 23 | ||||
-rw-r--r-- | refs.c | 18 | ||||
-rw-r--r-- | refs.h | 2 | ||||
-rw-r--r-- | remote.c | 6 | ||||
-rwxr-xr-x | t/t6300-for-each-ref.sh | 18 |
9 files changed, 67 insertions, 17 deletions
diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt index b362e9ed1..8dc873fd4 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/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt index 5ed2bc840..fba30b12e 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-branch.c b/builtin-branch.c index 327582169..91098ca9b 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 c8114c8af..91e8f95fd 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); + 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/builtin-rev-parse.c b/builtin-rev-parse.c index 81d5a6ffc..22c6d6ad1 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; @@ -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; } @@ -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); @@ -1461,11 +1461,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb) return 0; base = branch->merge[0]->dst; - if (!prefixcmp(base, "refs/remotes/")) { - base += strlen("refs/remotes/"); - } else if (!prefixcmp(base, "refs/heads/")) { - base += strlen("refs/heads/"); - } + base = shorten_unambiguous_ref(base, 0); if (!num_theirs) strbuf_addf(sb, "Your branch is ahead of '%s' " "by %d commit%s.\n", diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh index daf02d5c1..8052c86ad 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 && |