]> git.ipfire.org Git - thirdparty/git.git/commitdiff
environment: stop storing "core.warnAmbiguousRefs" globally
authorPatrick Steinhardt <ps@pks.im>
Thu, 12 Sep 2024 11:30:24 +0000 (13:30 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 12 Sep 2024 17:15:44 +0000 (10:15 -0700)
Same as the preceding commits, storing the "core.warnAmbiguousRefs"
value globally is misdesigned as this setting may be set per repository.

Move the logic into the repo-settings subsystem. The usual pattern here
is that users are expected to call `prepare_repo_settings()` before they
access the settings themselves. This seems somewhat fragile though, as
it is easy to miss and leads to somewhat ugly code patterns at the call
sites.

Instead, introduce a new function that encapsulates this logic for us.
This also allows us to change how exactly the lazy initialization works
in the future, e.g. by only partially initializing values as requested
by the caller.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c
config.c
environment.c
environment.h
object-name.c
ref-filter.c
refs.c
repo-settings.c
repo-settings.h

index a5108266dafc24ea18e331b616a418b225add305..34b467544264c77717672fdd211285802ecae942 100644 (file)
@@ -19,6 +19,7 @@
 #include "path.h"
 #include "diff.h"
 #include "read-cache-ll.h"
+#include "repo-settings.h"
 #include "repository.h"
 #include "revision.h"
 #include "setup.h"
@@ -899,7 +900,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                        }
                        if (opt_with_value(arg, "--abbrev-ref", &arg)) {
                                abbrev_ref = 1;
-                               abbrev_ref_strict = warn_ambiguous_refs;
+                               abbrev_ref_strict =
+                                       repo_settings_get_warn_ambiguous_refs(the_repository);
                                if (arg) {
                                        if (!strcmp(arg, "strict"))
                                                abbrev_ref_strict = 1;
index a59890180a373ee3b309ec1d8e31e2021a958e62..53c68f3da61f4c0def6805e6796f47121478e0b8 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1447,11 +1447,6 @@ static int git_default_core_config(const char *var, const char *value,
                return 0;
        }
 
-       if (!strcmp(var, "core.warnambiguousrefs")) {
-               warn_ambiguous_refs = git_config_bool(var, value);
-               return 0;
-       }
-
        if (!strcmp(var, "core.abbrev")) {
                if (!value)
                        return config_error_nonbool(var);
index 6805c7b01dfe51aacf092a255fbe9b9c556b64b1..9dd000cda3686b31dd60b83aa2dbef6c52aa5b9f 100644 (file)
@@ -35,7 +35,6 @@ int minimum_abbrev = 4, default_abbrev = -1;
 int ignore_case;
 int assume_unchanged;
 int is_bare_repository_cfg = -1; /* unspecified */
-int warn_ambiguous_refs = 1;
 int warn_on_object_refname_ambiguity = 1;
 int repository_format_precious_objects;
 char *git_commit_encoding;
index 0cab644e2d35801b5b2f0c33a1c95484da070305..aa38133da9c03e0f1ba61f835db3e60291e78c59 100644 (file)
@@ -156,7 +156,6 @@ extern int has_symlinks;
 extern int minimum_abbrev, default_abbrev;
 extern int ignore_case;
 extern int assume_unchanged;
-extern int warn_ambiguous_refs;
 extern int warn_on_object_refname_ambiguity;
 extern char *apply_default_whitespace;
 extern char *apply_default_ignorewhitespace;
index 09c1bd93a3158b8d786374717093dcebc1fac50b..c892fbe80aa7173dfcc1995de5a75bc322c6adb7 100644 (file)
@@ -20,6 +20,7 @@
 #include "pretty.h"
 #include "object-store-ll.h"
 #include "read-cache-ll.h"
+#include "repo-settings.h"
 #include "repository.h"
 #include "setup.h"
 #include "midx.h"
@@ -959,7 +960,7 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
        int fatal = !(flags & GET_OID_QUIETLY);
 
        if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
-               if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
+               if (repo_settings_get_warn_ambiguous_refs(r) && warn_on_object_refname_ambiguity) {
                        refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
                        if (refs_found > 0) {
                                warning(warn_msg, len, str);
@@ -1020,7 +1021,7 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
        if (!refs_found)
                return -1;
 
-       if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
+       if (repo_settings_get_warn_ambiguous_refs(r) && !(flags & GET_OID_QUIETLY) &&
            (refs_found > 1 ||
             !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY)))
                warning(warn_msg, len, str);
index b6c6c101276b63f7f144984888aada0524a3af94..7f5cf5a1269bc6e197b19be4f2a25489789ba32a 100644 (file)
@@ -13,6 +13,7 @@
 #include "object-name.h"
 #include "object-store-ll.h"
 #include "oid-array.h"
+#include "repo-settings.h"
 #include "repository.h"
 #include "commit.h"
 #include "mailmap.h"
@@ -2160,7 +2161,7 @@ static const char *show_ref(struct refname_atom *atom, const char *refname)
        if (atom->option == R_SHORT)
                return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
                                                    refname,
-                                                   warn_ambiguous_refs);
+                                                   repo_settings_get_warn_ambiguous_refs(the_repository));
        else if (atom->option == R_LSTRIP)
                return lstrip_ref_components(refname, atom->lstrip);
        else if (atom->option == R_RSTRIP)
diff --git a/refs.c b/refs.c
index d7402bcd196bf4bbe625ae9ec6dbc8304726a8dc..3bee3e782999848c31c810b1c8161d2766dd2c37 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -730,7 +730,7 @@ int expand_ref(struct repository *repo, const char *str, int len,
                if (r) {
                        if (!refs_found++)
                                *ref = xstrdup(r);
-                       if (!warn_ambiguous_refs)
+                       if (!repo_settings_get_warn_ambiguous_refs(repo))
                                break;
                } else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) {
                        warning(_("ignoring dangling symref %s"), fullref.buf);
@@ -775,7 +775,7 @@ int repo_dwim_log(struct repository *r, const char *str, int len,
                        if (oid)
                                oidcpy(oid, &hash);
                }
-               if (!warn_ambiguous_refs)
+               if (!repo_settings_get_warn_ambiguous_refs(r))
                        break;
        }
        strbuf_release(&path);
index 1322fd2f972366ea2f76b9feb4a9c0bde2da17a9..4699b4b3650fc3571b0240e15b539ba19d179e20 100644 (file)
@@ -140,3 +140,12 @@ enum log_refs_config repo_settings_get_log_all_ref_updates(struct repository *re
 
        return LOG_REFS_UNSET;
 }
+
+int repo_settings_get_warn_ambiguous_refs(struct repository *repo)
+{
+       prepare_repo_settings(repo);
+       if (repo->settings.warn_ambiguous_refs < 0)
+               repo_cfg_bool(repo, "core.warnambiguousrefs",
+                             &repo->settings.warn_ambiguous_refs, 1);
+       return repo->settings.warn_ambiguous_refs;
+}
index 76adb96a669a0a0894481d1bbc2d47b8c6e7fb6d..51d6156a1172e3a504a2c11abb134a5cff927d89 100644 (file)
@@ -56,16 +56,20 @@ struct repo_settings {
        enum fetch_negotiation_setting fetch_negotiation_algorithm;
 
        int core_multi_pack_index;
+       int warn_ambiguous_refs; /* lazily loaded via accessor */
 };
 #define REPO_SETTINGS_INIT { \
        .index_version = -1, \
        .core_untracked_cache = UNTRACKED_CACHE_KEEP, \
        .fetch_negotiation_algorithm = FETCH_NEGOTIATION_CONSECUTIVE, \
+       .warn_ambiguous_refs = -1, \
 }
 
 void prepare_repo_settings(struct repository *r);
 
 /* Read the value for "core.logAllRefUpdates". */
 enum log_refs_config repo_settings_get_log_all_ref_updates(struct repository *repo);
+/* Read the value for "core.warnAmbiguousRefs". */
+int repo_settings_get_warn_ambiguous_refs(struct repository *repo);
 
 #endif /* REPO_SETTINGS_H */