object-name: allow skipping ambiguity checks in `get_oid()` family

When reading an object ID via `get_oid_basic()` or any of its related functions we perform a check whether the object ID is ambiguous, which can be the case when a reference with the same name exists. While the check is generally helpful, there are cases where it only adds to the runtime overhead without providing much of a benefit. Add a new flag that allows us to disable the check. The flag will be used in a subsequent commit. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 12, 2025 at 16:56 UTC 46a2b52240ef687ad4c9d2aec4317796704c991a
2 files changed +15 -12
hash.h
+12 -11
@@ -193,17 +193,18 @@ struct object_id {
193 int algo; /* XXX requires 4-byte alignment */
194 };
195
196 -#define GET_OID_QUIETLY 01
197 -#define GET_OID_COMMIT 02
198 -#define GET_OID_COMMITTISH 04
199 -#define GET_OID_TREE 010
200 -#define GET_OID_TREEISH 020
201 -#define GET_OID_BLOB 040
202 -#define GET_OID_FOLLOW_SYMLINKS 0100
203 -#define GET_OID_RECORD_PATH 0200
204 -#define GET_OID_ONLY_TO_DIE 04000
205 -#define GET_OID_REQUIRE_PATH 010000
206 -#define GET_OID_HASH_ANY 020000
196 +#define GET_OID_QUIETLY 01
197 +#define GET_OID_COMMIT 02
198 +#define GET_OID_COMMITTISH 04
199 +#define GET_OID_TREE 010
200 +#define GET_OID_TREEISH 020
201 +#define GET_OID_BLOB 040
202 +#define GET_OID_FOLLOW_SYMLINKS 0100
203 +#define GET_OID_RECORD_PATH 0200
204 +#define GET_OID_ONLY_TO_DIE 04000
205 +#define GET_OID_REQUIRE_PATH 010000
206 +#define GET_OID_HASH_ANY 020000
207 +#define GET_OID_SKIP_AMBIGUITY_CHECK 040000
208
209 #define GET_OID_DISAMBIGUATORS \
210 (GET_OID_COMMIT | GET_OID_COMMITTISH | \
object-name.c
+3 -1
@@ -961,7 +961,9 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
961 int fatal = !(flags & GET_OID_QUIETLY);
962
963 if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
964 - if (repo_settings_get_warn_ambiguous_refs(r) && warn_on_object_refname_ambiguity) {
964 + if (!(flags & GET_OID_SKIP_AMBIGUITY_CHECK) &&
965 + repo_settings_get_warn_ambiguous_refs(r) &&
966 + warn_on_object_refname_ambiguity) {
967 refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
968 if (refs_found > 0) {
969 warning(warn_msg, len, str);