Skip to content

Commit 11dbb4a

Browse files
pks-tgitster
authored andcommitted
environment: stop storing "core.warnAmbiguousRefs" globally
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8e2e8a3 commit 11dbb4a

File tree

9 files changed

+23
-13
lines changed

9 files changed

+23
-13
lines changed

builtin/rev-parse.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "path.h"
2020
#include "diff.h"
2121
#include "read-cache-ll.h"
22+
#include "repo-settings.h"
2223
#include "repository.h"
2324
#include "revision.h"
2425
#include "setup.h"
@@ -899,7 +900,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
899900
}
900901
if (opt_with_value(arg, "--abbrev-ref", &arg)) {
901902
abbrev_ref = 1;
902-
abbrev_ref_strict = warn_ambiguous_refs;
903+
abbrev_ref_strict =
904+
repo_settings_get_warn_ambiguous_refs(the_repository);
903905
if (arg) {
904906
if (!strcmp(arg, "strict"))
905907
abbrev_ref_strict = 1;

config.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,11 +1447,6 @@ static int git_default_core_config(const char *var, const char *value,
14471447
return 0;
14481448
}
14491449

1450-
if (!strcmp(var, "core.warnambiguousrefs")) {
1451-
warn_ambiguous_refs = git_config_bool(var, value);
1452-
return 0;
1453-
}
1454-
14551450
if (!strcmp(var, "core.abbrev")) {
14561451
if (!value)
14571452
return config_error_nonbool(var);

environment.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ int minimum_abbrev = 4, default_abbrev = -1;
3535
int ignore_case;
3636
int assume_unchanged;
3737
int is_bare_repository_cfg = -1; /* unspecified */
38-
int warn_ambiguous_refs = 1;
3938
int warn_on_object_refname_ambiguity = 1;
4039
int repository_format_precious_objects;
4140
char *git_commit_encoding;

environment.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ extern int has_symlinks;
156156
extern int minimum_abbrev, default_abbrev;
157157
extern int ignore_case;
158158
extern int assume_unchanged;
159-
extern int warn_ambiguous_refs;
160159
extern int warn_on_object_refname_ambiguity;
161160
extern char *apply_default_whitespace;
162161
extern char *apply_default_ignorewhitespace;

object-name.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "pretty.h"
2121
#include "object-store-ll.h"
2222
#include "read-cache-ll.h"
23+
#include "repo-settings.h"
2324
#include "repository.h"
2425
#include "setup.h"
2526
#include "midx.h"
@@ -959,7 +960,7 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
959960
int fatal = !(flags & GET_OID_QUIETLY);
960961

961962
if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
962-
if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
963+
if (repo_settings_get_warn_ambiguous_refs(r) && warn_on_object_refname_ambiguity) {
963964
refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
964965
if (refs_found > 0) {
965966
warning(warn_msg, len, str);
@@ -1020,7 +1021,7 @@ static int get_oid_basic(struct repository *r, const char *str, int len,
10201021
if (!refs_found)
10211022
return -1;
10221023

1023-
if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
1024+
if (repo_settings_get_warn_ambiguous_refs(r) && !(flags & GET_OID_QUIETLY) &&
10241025
(refs_found > 1 ||
10251026
!get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY)))
10261027
warning(warn_msg, len, str);

ref-filter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "object-name.h"
1414
#include "object-store-ll.h"
1515
#include "oid-array.h"
16+
#include "repo-settings.h"
1617
#include "repository.h"
1718
#include "commit.h"
1819
#include "mailmap.h"
@@ -2160,7 +2161,7 @@ static const char *show_ref(struct refname_atom *atom, const char *refname)
21602161
if (atom->option == R_SHORT)
21612162
return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
21622163
refname,
2163-
warn_ambiguous_refs);
2164+
repo_settings_get_warn_ambiguous_refs(the_repository));
21642165
else if (atom->option == R_LSTRIP)
21652166
return lstrip_ref_components(refname, atom->lstrip);
21662167
else if (atom->option == R_RSTRIP)

refs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ int expand_ref(struct repository *repo, const char *str, int len,
730730
if (r) {
731731
if (!refs_found++)
732732
*ref = xstrdup(r);
733-
if (!warn_ambiguous_refs)
733+
if (!repo_settings_get_warn_ambiguous_refs(repo))
734734
break;
735735
} else if ((flag & REF_ISSYMREF) && strcmp(fullref.buf, "HEAD")) {
736736
warning(_("ignoring dangling symref %s"), fullref.buf);
@@ -775,7 +775,7 @@ int repo_dwim_log(struct repository *r, const char *str, int len,
775775
if (oid)
776776
oidcpy(oid, &hash);
777777
}
778-
if (!warn_ambiguous_refs)
778+
if (!repo_settings_get_warn_ambiguous_refs(r))
779779
break;
780780
}
781781
strbuf_release(&path);

repo-settings.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,12 @@ enum log_refs_config repo_settings_get_log_all_ref_updates(struct repository *re
140140

141141
return LOG_REFS_UNSET;
142142
}
143+
144+
int repo_settings_get_warn_ambiguous_refs(struct repository *repo)
145+
{
146+
prepare_repo_settings(repo);
147+
if (repo->settings.warn_ambiguous_refs < 0)
148+
repo_cfg_bool(repo, "core.warnambiguousrefs",
149+
&repo->settings.warn_ambiguous_refs, 1);
150+
return repo->settings.warn_ambiguous_refs;
151+
}

repo-settings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,20 @@ struct repo_settings {
5656
enum fetch_negotiation_setting fetch_negotiation_algorithm;
5757

5858
int core_multi_pack_index;
59+
int warn_ambiguous_refs; /* lazily loaded via accessor */
5960
};
6061
#define REPO_SETTINGS_INIT { \
6162
.index_version = -1, \
6263
.core_untracked_cache = UNTRACKED_CACHE_KEEP, \
6364
.fetch_negotiation_algorithm = FETCH_NEGOTIATION_CONSECUTIVE, \
65+
.warn_ambiguous_refs = -1, \
6466
}
6567

6668
void prepare_repo_settings(struct repository *r);
6769

6870
/* Read the value for "core.logAllRefUpdates". */
6971
enum log_refs_config repo_settings_get_log_all_ref_updates(struct repository *repo);
72+
/* Read the value for "core.warnAmbiguousRefs". */
73+
int repo_settings_get_warn_ambiguous_refs(struct repository *repo);
7074

7175
#endif /* REPO_SETTINGS_H */

0 commit comments

Comments
 (0)