Skip to content

Commit aa1dec9

Browse files
committed
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 <[email protected]>
1 parent 37c00e5 commit aa1dec9

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

cache.h

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

814814
#define GET_SHA1_QUIETLY 01
815+
#define GET_SHA1_COMMIT 02
815816

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

sha1_name.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ static int finish_object_disambiguation(struct disambiguate_state *ds,
218218
return 0;
219219
}
220220

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+
221227
static int get_short_sha1(const char *name, int len, unsigned char *sha1,
222228
unsigned flags)
223229
{
@@ -253,6 +259,9 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
253259
prepare_alt_odb();
254260

255261
memset(&ds, 0, sizeof(ds));
262+
if (flags & GET_SHA1_COMMIT)
263+
ds.fn = disambiguate_commit_only;
264+
256265
find_short_object_filename(len, hex_pfx, &ds);
257266
find_short_packed_object(len, bin_pfx, &ds);
258267
status = finish_object_disambiguation(&ds, sha1);

0 commit comments

Comments
 (0)