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 <gitster@pobox.com>
Junio C Hamano committed
Jul 2, 2012 at 10:00 UTC
e2643617d75e53e5a69278f8d7382553d1c14cf0
3 files changed
+30
-5
cache.h
+1
@@ -813,6 +813,7 @@ struct object_context {
813
814
#define GET_SHA1_QUIETLY 01
815
#define GET_SHA1_COMMIT 02
816
+#define GET_SHA1_COMMITTISH 04
817
818
extern int get_sha1(const char *str, unsigned char *sha1);
819
extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
sha1_name.c
+28
-4
@@ -224,6 +224,24 @@ static int disambiguate_commit_only(const unsigned char *sha1, void *cb_data_unu
224
return kind == OBJ_COMMIT;
225
}
226
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
+
245
static int get_short_sha1(const char *name, int len, unsigned char *sha1,
246
unsigned flags)
247
{
@@ -261,6 +279,8 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
279
memset(&ds, 0, sizeof(ds));
280
if (flags & GET_SHA1_COMMIT)
281
ds.fn = disambiguate_commit_only;
282
+ else if (flags & GET_SHA1_COMMITTISH)
283
+ ds.fn = disambiguate_committish_only;
284
285
find_short_object_filename(len, hex_pfx, &ds);
286
find_short_packed_object(len, bin_pfx, &ds);
@@ -440,7 +460,7 @@ static int get_parent(const char *name, int len,
460
unsigned char *result, int idx)
461
{
462
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);
464
struct commit *commit;
465
struct commit_list *p;
466
@@ -473,7 +493,7 @@ static int get_nth_ancestor(const char *name, int len,
493
struct commit *commit;
494
int ret;
495
476
- ret = get_sha1_1(name, len, sha1, 0);
496
+ ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
497
if (ret)
498
return ret;
499
commit = lookup_commit_reference(sha1);
@@ -519,6 +539,7 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
539
unsigned char outer[20];
540
const char *sp;
541
unsigned int expected_type = 0;
542
+ unsigned lookup_flags = 0;
543
struct object *o;
544
545
/*
@@ -554,7 +575,10 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
575
else
576
return -1;
577
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))
582
return -1;
583
584
o = parse_object(outer);
@@ -666,7 +690,7 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned l
690
if (!ret)
691
return 0;
692
669
- return get_short_sha1(name, len, sha1, 0);
693
+ return get_short_sha1(name, len, sha1, lookup_flags);
694
}
695
696
/*
t/t1512-rev-parse-disambiguation.sh
+1
-1
@@ -85,7 +85,7 @@ test_expect_success 'first commit' '
85
git commit -m a2onsxbvj
86
'
87
88
-test_expect_failure 'disambiguate commit-ish' '
88
+test_expect_success 'disambiguate commit-ish' '
89
# feed commit-ish in an unambiguous way
90
git rev-parse --verify 0000000000e4f^{commit} &&
91