Skip to content

Commit 8a10fea

Browse files
peffgitster
authored andcommitted
get_sha1: propagate flags to child functions
The get_sha1() function is actually implementation by many sub-functions, but we do not always pass our flags around to all of those functions. As a result, we may forget that our caller asked us to resolve with GET_SHA1_QUIETLY and output messages. The two triggerable cases are: 1. Resolving treeish:path will resolve the "treeish" portion using GET_SHA1_TREEISH, dropping all other flags. 2. The peel_onion() function did not take flags at all but recurses to get_sha1_1(), which does. The solution for both is to bitwise-OR their new flags with the existing ones (after dropping any mutually exclusive disambiguation flags). This bug can trigger with "git rev-parse --quiet", which asks for quiet resolution. But it can also happen in a more vanilla code path when we do a follow-up ONLY_TO_DIE invocation of get_sha1(), and that's what the tests check. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7243ffd commit 8a10fea

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

sha1_name.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -681,12 +681,12 @@ struct object *peel_to_type(const char *name, int namelen,
681681
}
682682
}
683683

684-
static int peel_onion(const char *name, int len, unsigned char *sha1)
684+
static int peel_onion(const char *name, int len, unsigned char *sha1,
685+
unsigned lookup_flags)
685686
{
686687
unsigned char outer[20];
687688
const char *sp;
688689
unsigned int expected_type = 0;
689-
unsigned lookup_flags = 0;
690690
struct object *o;
691691

692692
/*
@@ -726,10 +726,11 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
726726
else
727727
return -1;
728728

729+
lookup_flags &= ~GET_SHA1_DISAMBIGUATORS;
729730
if (expected_type == OBJ_COMMIT)
730-
lookup_flags = GET_SHA1_COMMITTISH;
731+
lookup_flags |= GET_SHA1_COMMITTISH;
731732
else if (expected_type == OBJ_TREE)
732-
lookup_flags = GET_SHA1_TREEISH;
733+
lookup_flags |= GET_SHA1_TREEISH;
733734

734735
if (get_sha1_1(name, sp - name - 2, outer, lookup_flags))
735736
return -1;
@@ -830,7 +831,7 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned l
830831
return get_nth_ancestor(name, len1, sha1, num);
831832
}
832833

833-
ret = peel_onion(name, len, sha1);
834+
ret = peel_onion(name, len, sha1, lookup_flags);
834835
if (!ret)
835836
return 0;
836837

@@ -1465,7 +1466,12 @@ static int get_sha1_with_context_1(const char *name,
14651466
if (*cp == ':') {
14661467
unsigned char tree_sha1[20];
14671468
int len = cp - name;
1468-
if (!get_sha1_1(name, len, tree_sha1, GET_SHA1_TREEISH)) {
1469+
unsigned sub_flags = flags;
1470+
1471+
sub_flags &= ~GET_SHA1_DISAMBIGUATORS;
1472+
sub_flags |= GET_SHA1_TREEISH;
1473+
1474+
if (!get_sha1_1(name, len, tree_sha1, sub_flags)) {
14691475
const char *filename = cp+1;
14701476
char *new_filename = NULL;
14711477

t/t1512-rev-parse-disambiguation.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,22 @@ test_expect_success 'ambiguous short sha1 ref' '
291291
grep "refname.*${REF}.*ambiguous" err
292292
'
293293

294-
test_expect_success C_LOCALE_OUTPUT 'ambiguity errors are not repeated' '
294+
test_expect_success C_LOCALE_OUTPUT 'ambiguity errors are not repeated (raw)' '
295295
test_must_fail git rev-parse 00000 2>stderr &&
296296
grep "is ambiguous" stderr >errors &&
297297
test_line_count = 1 errors
298298
'
299299

300+
test_expect_success C_LOCALE_OUTPUT 'ambiguity errors are not repeated (treeish)' '
301+
test_must_fail git rev-parse 00000:foo 2>stderr &&
302+
grep "is ambiguous" stderr >errors &&
303+
test_line_count = 1 errors
304+
'
305+
306+
test_expect_success C_LOCALE_OUTPUT 'ambiguity errors are not repeated (peel)' '
307+
test_must_fail git rev-parse 00000^{commit} 2>stderr &&
308+
grep "is ambiguous" stderr >errors &&
309+
test_line_count = 1 errors
310+
'
311+
300312
test_done

0 commit comments

Comments
 (0)