mirror of
https://github.com/git/git.git
synced 2024-11-24 18:33:43 +08:00
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
This commit is contained in:
commit
a9b772a011
@ -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`).
|
||||
|
@ -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`.
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
18
refs.c
18
refs.c
@ -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))
|
||||
@ -1725,15 +1726,26 @@ 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;
|
||||
}
|
||||
|
||||
|
2
refs.h
2
refs.h
@ -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);
|
||||
|
6
remote.c
6
remote.c
@ -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",
|
||||
|
@ -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 &&
|
||||
@ -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 &&
|
||||
|
Loading…
Reference in New Issue
Block a user