get_sha1: detect buggy calls with multiple disambiguators

The get_sha1() family of functions takes a flags field, but some of the flags are mutually exclusive. In particular, we can only handle one disambiguating function, and the flags quietly override each other. Let's instead detect these as programming bugs. Technically some of the flags are supersets of the others, so treating COMMITTISH|TREEISH as just COMMITTISH is not wrong, but it's a good sign the caller is confused. And certainly asking for BLOB|TREE does not work. We can do the check easily with some bit-twiddling, and as a bonus, the bit-mask of disambiguators will come in handy in a future patch. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 26, 2016 at 07:59 UTC 259942f549eb235e9d7d095c2db8f3dc279f3958
2 files changed +9
cache.h
+5
@@ -1203,6 +1203,11 @@ struct object_context {
1203 #define GET_SHA1_FOLLOW_SYMLINKS 0100
1204 #define GET_SHA1_ONLY_TO_DIE 04000
1205
1206 +#define GET_SHA1_DISAMBIGUATORS \
1207 + (GET_SHA1_COMMIT | GET_SHA1_COMMITTISH | \
1208 + GET_SHA1_TREE | GET_SHA1_TREEISH | \
1209 + GET_SHA1_BLOB)
1210 +
1211 extern int get_sha1(const char *str, unsigned char *sha1);
1212 extern int get_sha1_commit(const char *str, unsigned char *sha1);
1213 extern int get_sha1_committish(const char *str, unsigned char *sha1);
sha1_name.c
+4
@@ -327,6 +327,10 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
327 prepare_alt_odb();
328
329 memset(&ds, 0, sizeof(ds));
330 +
331 + if (HAS_MULTI_BITS(flags & GET_SHA1_DISAMBIGUATORS))
332 + die("BUG: multiple get_short_sha1 disambiguator flags");
333 +
334 if (flags & GET_SHA1_COMMIT)
335 ds.fn = disambiguate_commit_only;
336 else if (flags & GET_SHA1_COMMITTISH)