Skip to content

Commit e264361

Browse files
committed
sha1_name.c: many short names can only be committish
We know that the token "$name" that appear in "$name^{commit}", "$name^4", "$name~4" etc. can only name a committish (either a commit or a tag that peels to a commit). Teach get_short_sha1() to take advantage of that knowledge when disambiguating an abbreviated SHA-1 given as an object name. Signed-off-by: Junio C Hamano <[email protected]>
1 parent e48ba20 commit e264361

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ struct object_context {
813813

814814
#define GET_SHA1_QUIETLY 01
815815
#define GET_SHA1_COMMIT 02
816+
#define GET_SHA1_COMMITTISH 04
816817

817818
extern int get_sha1(const char *str, unsigned char *sha1);
818819
extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);

sha1_name.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ static int disambiguate_commit_only(const unsigned char *sha1, void *cb_data_unu
224224
return kind == OBJ_COMMIT;
225225
}
226226

227+
static int disambiguate_committish_only(const unsigned char *sha1, void *cb_data_unused)
228+
{
229+
struct object *obj;
230+
int kind;
231+
232+
kind = sha1_object_info(sha1, NULL);
233+
if (kind == OBJ_COMMIT)
234+
return 1;
235+
if (kind != OBJ_TAG)
236+
return 0;
237+
238+
/* We need to do this the hard way... */
239+
obj = deref_tag(lookup_object(sha1), NULL, 0);
240+
if (obj && obj->type == OBJ_COMMIT)
241+
return 1;
242+
return 0;
243+
}
244+
227245
static int get_short_sha1(const char *name, int len, unsigned char *sha1,
228246
unsigned flags)
229247
{
@@ -261,6 +279,8 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
261279
memset(&ds, 0, sizeof(ds));
262280
if (flags & GET_SHA1_COMMIT)
263281
ds.fn = disambiguate_commit_only;
282+
else if (flags & GET_SHA1_COMMITTISH)
283+
ds.fn = disambiguate_committish_only;
264284

265285
find_short_object_filename(len, hex_pfx, &ds);
266286
find_short_packed_object(len, bin_pfx, &ds);
@@ -440,7 +460,7 @@ static int get_parent(const char *name, int len,
440460
unsigned char *result, int idx)
441461
{
442462
unsigned char sha1[20];
443-
int ret = get_sha1_1(name, len, sha1, 0);
463+
int ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
444464
struct commit *commit;
445465
struct commit_list *p;
446466

@@ -473,7 +493,7 @@ static int get_nth_ancestor(const char *name, int len,
473493
struct commit *commit;
474494
int ret;
475495

476-
ret = get_sha1_1(name, len, sha1, 0);
496+
ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
477497
if (ret)
478498
return ret;
479499
commit = lookup_commit_reference(sha1);
@@ -519,6 +539,7 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
519539
unsigned char outer[20];
520540
const char *sp;
521541
unsigned int expected_type = 0;
542+
unsigned lookup_flags = 0;
522543
struct object *o;
523544

524545
/*
@@ -554,7 +575,10 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
554575
else
555576
return -1;
556577

557-
if (get_sha1_1(name, sp - name - 2, outer, 0))
578+
if (expected_type == OBJ_COMMIT)
579+
lookup_flags = GET_SHA1_COMMITTISH;
580+
581+
if (get_sha1_1(name, sp - name - 2, outer, lookup_flags))
558582
return -1;
559583

560584
o = parse_object(outer);
@@ -666,7 +690,7 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned l
666690
if (!ret)
667691
return 0;
668692

669-
return get_short_sha1(name, len, sha1, 0);
693+
return get_short_sha1(name, len, sha1, lookup_flags);
670694
}
671695

672696
/*

t/t1512-rev-parse-disambiguation.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ test_expect_success 'first commit' '
8585
git commit -m a2onsxbvj
8686
'
8787

88-
test_expect_failure 'disambiguate commit-ish' '
88+
test_expect_success 'disambiguate commit-ish' '
8989
# feed commit-ish in an unambiguous way
9090
git rev-parse --verify 0000000000e4f^{commit} &&
9191

0 commit comments

Comments
 (0)