sha1_name.c: teach get_short_sha1() a commit-only option

When the caller knows that the parameter is meant to name a commit, e.g. "56789a" in describe name "v1.2.3-4-g56789a", pass that as a hint so that lower level can use it to disambiguate objects when there is only one commit whose name begins with 56789a even if there are objects of other types whose names share the same prefix. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 20, 2012 at 23:03 UTC aa1dec9ef6ff184792520eb4539af1555c01604c
2 files changed +10
cache.h
+1
@@ -812,6 +812,7 @@ struct object_context {
812 };
813
814 #define GET_SHA1_QUIETLY 01
815 +#define GET_SHA1_COMMIT 02
816
817 extern int get_sha1(const char *str, unsigned char *sha1);
818 extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
sha1_name.c
+9
@@ -218,6 +218,12 @@ static int finish_object_disambiguation(struct disambiguate_state *ds,
218 return 0;
219 }
220
221 +static int disambiguate_commit_only(const unsigned char *sha1, void *cb_data_unused)
222 +{
223 + int kind = sha1_object_info(sha1, NULL);
224 + return kind == OBJ_COMMIT;
225 +}
226 +
227 static int get_short_sha1(const char *name, int len, unsigned char *sha1,
228 unsigned flags)
229 {
@@ -253,6 +259,9 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
259 prepare_alt_odb();
260
261 memset(&ds, 0, sizeof(ds));
262 + if (flags & GET_SHA1_COMMIT)
263 + ds.fn = disambiguate_commit_only;
264 +
265 find_short_object_filename(len, hex_pfx, &ds);
266 find_short_packed_object(len, bin_pfx, &ds);
267 status = finish_object_disambiguation(&ds, sha1);