Skip to content

Commit 5b33cb1

Browse files
peffgitster
authored andcommitted
get_short_sha1: make default disambiguation configurable
When we find ambiguous short sha1s, we may get a disambiguation rule from our caller's context. But if we don't, we fall back to treating all sha1s the same, even though most projects will tend to refer only to commits by their short sha1s. This patch introduces a configuration option that lets the user pick a different fallback (e.g., only commits). It's possible that we may want to make this the default, but it's a good idea to start as a config option for two reasons: 1. It lets people experiment with this and see if it's a good idea (i.e., the "tend to" above is an assumption; we don't really know if this will break some obscure cases). 2. Even if we do flip the default, it gives people an escape hatch if it causes problems (you can sometimes override it by asking for "1234^{tree}", but not all combinations are possible). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1ffa26c commit 5b33cb1

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,8 @@ extern int get_oid(const char *str, struct object_id *oid);
12221222
typedef int each_abbrev_fn(const unsigned char *sha1, void *);
12231223
extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
12241224

1225+
extern int set_disambiguate_hint_config(const char *var, const char *value);
1226+
12251227
/*
12261228
* Try to read a SHA1 in hexadecimal format from the 40 characters
12271229
* starting at hex. Write the 20-byte result to sha1 in binary form.

config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,9 @@ static int git_default_core_config(const char *var, const char *value)
841841
return 0;
842842
}
843843

844+
if (!strcmp(var, "core.disambiguate"))
845+
return set_disambiguate_hint_config(var, value);
846+
844847
if (!strcmp(var, "core.loosecompression")) {
845848
int level = git_config_int(var, value);
846849
if (level == -1)

sha1_name.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,36 @@ static int disambiguate_blob_only(const unsigned char *sha1, void *cb_data_unuse
283283
return kind == OBJ_BLOB;
284284
}
285285

286+
static disambiguate_hint_fn default_disambiguate_hint;
287+
288+
int set_disambiguate_hint_config(const char *var, const char *value)
289+
{
290+
static const struct {
291+
const char *name;
292+
disambiguate_hint_fn fn;
293+
} hints[] = {
294+
{ "none", NULL },
295+
{ "commit", disambiguate_commit_only },
296+
{ "committish", disambiguate_committish_only },
297+
{ "tree", disambiguate_tree_only },
298+
{ "treeish", disambiguate_treeish_only },
299+
{ "blob", disambiguate_blob_only }
300+
};
301+
int i;
302+
303+
if (!value)
304+
return config_error_nonbool(var);
305+
306+
for (i = 0; i < ARRAY_SIZE(hints); i++) {
307+
if (!strcasecmp(value, hints[i].name)) {
308+
default_disambiguate_hint = hints[i].fn;
309+
return 0;
310+
}
311+
}
312+
313+
return error("unknown hint type for '%s': %s", var, value);
314+
}
315+
286316
static int init_object_disambiguation(const char *name, int len,
287317
struct disambiguate_state *ds)
288318
{
@@ -373,6 +403,8 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
373403
ds.fn = disambiguate_treeish_only;
374404
else if (flags & GET_SHA1_BLOB)
375405
ds.fn = disambiguate_blob_only;
406+
else
407+
ds.fn = default_disambiguate_hint;
376408

377409
find_short_object_filename(&ds);
378410
find_short_packed_object(&ds);

t/t1512-rev-parse-disambiguation.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,18 @@ test_expect_success C_LOCALE_OUTPUT 'failed type-selector still shows hint' '
347347
test_line_count = 3 hints
348348
'
349349

350+
test_expect_success 'core.disambiguate config can prefer types' '
351+
# ambiguous between tree and tag
352+
sha1=0000000000f &&
353+
test_must_fail git rev-parse $sha1 &&
354+
git rev-parse $sha1^{commit} &&
355+
git -c core.disambiguate=committish rev-parse $sha1
356+
'
357+
358+
test_expect_success 'core.disambiguate does not override context' '
359+
# treeish ambiguous between tag and tree
360+
test_must_fail \
361+
git -c core.disambiguate=committish rev-parse $sha1^{tree}
362+
'
363+
350364
test_done

0 commit comments

Comments
 (0)