Skip to content

Commit

Permalink
Merge branch 'bw/short-ref-strict'
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Junio C Hamano committed Apr 18, 2009
2 parents bd15ef0 + 45972ff commit a9b772a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Documentation/git-for-each-ref.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
5 changes: 5 additions & 0 deletions Documentation/git-rev-parse.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
4 changes: 2 additions & 2 deletions builtin-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion builtin-for-each-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
23 changes: 21 additions & 2 deletions builtin-rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand Down Expand Up @@ -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;

Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 15 additions & 3 deletions refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1718,22 +1718,34 @@ 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))
continue;

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
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion refs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 15 additions & 3 deletions t/t6300-for-each-ref.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -315,12 +316,23 @@ test_expect_success 'Check ambiguous head and tag refs' '
test_cmp expected actual
'

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 &&
Expand Down

0 comments on commit a9b772a

Please sign in to comment.