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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 27, 2016 at 08:38 UTC 5b33cb1fd733f581da07ae8afa7e9547eafd248e
4 files changed +51
cache.h
+2
@@ -1222,6 +1222,8 @@ extern int get_oid(const char *str, struct object_id *oid);
1222 typedef int each_abbrev_fn(const unsigned char *sha1, void *);
1223 extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *);
1224
1225 +extern int set_disambiguate_hint_config(const char *var, const char *value);
1226 +
1227 /*
1228 * Try to read a SHA1 in hexadecimal format from the 40 characters
1229 * starting at hex. Write the 20-byte result to sha1 in binary form.
config.c
+3
@@ -841,6 +841,9 @@ static int git_default_core_config(const char *var, const char *value)
841 return 0;
842 }
843
844 + if (!strcmp(var, "core.disambiguate"))
845 + return set_disambiguate_hint_config(var, value);
846 +
847 if (!strcmp(var, "core.loosecompression")) {
848 int level = git_config_int(var, value);
849 if (level == -1)
sha1_name.c
+32
@@ -283,6 +283,36 @@ static int disambiguate_blob_only(const unsigned char *sha1, void *cb_data_unuse
283 return kind == OBJ_BLOB;
284 }
285
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 +
316 static int init_object_disambiguation(const char *name, int len,
317 struct disambiguate_state *ds)
318 {
@@ -373,6 +403,8 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
403 ds.fn = disambiguate_treeish_only;
404 else if (flags & GET_SHA1_BLOB)
405 ds.fn = disambiguate_blob_only;
406 + else
407 + ds.fn = default_disambiguate_hint;
408
409 find_short_object_filename(&ds);
410 find_short_packed_object(&ds);
t/t1512-rev-parse-disambiguation.sh
+14
@@ -347,4 +347,18 @@ test_expect_success C_LOCALE_OUTPUT 'failed type-selector still shows hint' '
347 test_line_count = 3 hints
348 '
349
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 +
364 test_done