mirror of
https://github.com/git/git
synced 2024-08-25 10:45:59 +00:00
Merge branch 'nd/warn-ambiguous-object-name'
"git cmd <name>", when <name> happens to be a 40-hex string, directly uses the 40-hex string as an object name, even if a ref "refs/<some hierarchy>/<name>" exists. This disambiguation order is unlikely to change, but we should warn about the ambiguity just like we warn when more than one refs/ hierachies share the same name. * nd/warn-ambiguous-object-name: get_sha1: warn about full or short object names that look like refs
This commit is contained in:
commit
f4c52a0527
2
advice.c
2
advice.c
|
@ -14,6 +14,7 @@ int advice_resolve_conflict = 1;
|
|||
int advice_implicit_identity = 1;
|
||||
int advice_detached_head = 1;
|
||||
int advice_set_upstream_failure = 1;
|
||||
int advice_object_name_warning = 1;
|
||||
|
||||
static struct {
|
||||
const char *name;
|
||||
|
@ -33,6 +34,7 @@ static struct {
|
|||
{ "implicitidentity", &advice_implicit_identity },
|
||||
{ "detachedhead", &advice_detached_head },
|
||||
{ "setupstreamfailure", &advice_set_upstream_failure },
|
||||
{ "object_name_warning", &advice_object_name_warning },
|
||||
|
||||
/* make this an alias for backward compatibility */
|
||||
{ "pushnonfastforward", &advice_push_update_rejected }
|
||||
|
|
1
advice.h
1
advice.h
|
@ -17,6 +17,7 @@ extern int advice_resolve_conflict;
|
|||
extern int advice_implicit_identity;
|
||||
extern int advice_detached_head;
|
||||
extern int advice_set_upstream_failure;
|
||||
extern int advice_object_name_warning;
|
||||
|
||||
int git_default_advice_config(const char *var, const char *value);
|
||||
void advise(const char *advice, ...);
|
||||
|
|
25
sha1_name.c
25
sha1_name.c
|
@ -435,12 +435,31 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned l
|
|||
static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
|
||||
{
|
||||
static const char *warn_msg = "refname '%.*s' is ambiguous.";
|
||||
static const char *object_name_msg = N_(
|
||||
"Git normally never creates a ref that ends with 40 hex characters\n"
|
||||
"because it will be ignored when you just specify 40-hex. These refs\n"
|
||||
"may be created by mistake. For example,\n"
|
||||
"\n"
|
||||
" git checkout -b $br $(git rev-parse ...)\n"
|
||||
"\n"
|
||||
"where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
|
||||
"examine these refs and maybe delete them. Turn this message off by\n"
|
||||
"running \"git config advice.object_name_warning false\"");
|
||||
unsigned char tmp_sha1[20];
|
||||
char *real_ref = NULL;
|
||||
int refs_found = 0;
|
||||
int at, reflog_len;
|
||||
|
||||
if (len == 40 && !get_sha1_hex(str, sha1))
|
||||
if (len == 40 && !get_sha1_hex(str, sha1)) {
|
||||
refs_found = dwim_ref(str, len, tmp_sha1, &real_ref);
|
||||
if (refs_found > 0 && warn_ambiguous_refs) {
|
||||
warning(warn_msg, len, str);
|
||||
if (advice_object_name_warning)
|
||||
fprintf(stderr, "%s\n", _(object_name_msg));
|
||||
}
|
||||
free(real_ref);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* basic@{time or number or -number} format to query ref-log */
|
||||
reflog_len = at = 0;
|
||||
|
@ -481,7 +500,9 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
|
|||
if (!refs_found)
|
||||
return -1;
|
||||
|
||||
if (warn_ambiguous_refs && refs_found > 1)
|
||||
if (warn_ambiguous_refs &&
|
||||
(refs_found > 1 ||
|
||||
!get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY)))
|
||||
warning(warn_msg, len, str);
|
||||
|
||||
if (reflog_len) {
|
||||
|
|
|
@ -261,4 +261,22 @@ test_expect_success 'rev-parse --disambiguate' '
|
|||
test "$(sed -e "s/^\(.........\).*/\1/" actual | sort -u)" = 000000000
|
||||
'
|
||||
|
||||
test_expect_success 'ambiguous 40-hex ref' '
|
||||
TREE=$(git mktree </dev/null) &&
|
||||
REF=`git rev-parse HEAD` &&
|
||||
VAL=$(git commit-tree $TREE </dev/null) &&
|
||||
git update-ref refs/heads/$REF $VAL &&
|
||||
test `git rev-parse $REF 2>err` = $REF &&
|
||||
grep "refname.*${REF}.*ambiguous" err
|
||||
'
|
||||
|
||||
test_expect_success 'ambiguous short sha1 ref' '
|
||||
TREE=$(git mktree </dev/null) &&
|
||||
REF=`git rev-parse --short HEAD` &&
|
||||
VAL=$(git commit-tree $TREE </dev/null) &&
|
||||
git update-ref refs/heads/$REF $VAL &&
|
||||
test `git rev-parse $REF 2>err` = $VAL &&
|
||||
grep "refname.*${REF}.*ambiguous" err
|
||||
'
|
||||
|
||||
test_done
|
||||
|
|
Loading…
Reference in a new issue