environment: stop storing "core.notesRef" globally

Stop storing the "core.notesRef" config value globally. Instead, retrieve the value in `default_notes_ref()`. The code is never called in a hot loop anyway, so doing this on every invocation should be perfectly fine. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 12, 2024 at 13:30 UTC 1e7e4a111f986088abc9194d55349419e5c110c3
6 files changed +29 -28
builtin/notes.c
+14 -8
@@ -897,6 +897,7 @@ static int merge(int argc, const char **argv, const char *prefix)
897 1, PARSE_OPT_NONEG),
898 OPT_END()
899 };
900 + char *notes_ref;
901
902 argc = parse_options(argc, argv, prefix, options,
903 git_notes_merge_usage, 0);
@@ -924,7 +925,8 @@ static int merge(int argc, const char **argv, const char *prefix)
925 if (do_commit)
926 return merge_commit(&o);
927
927 - o.local_ref = default_notes_ref();
928 + notes_ref = default_notes_ref(the_repository);
929 + o.local_ref = notes_ref;
930 strbuf_addstr(&remote_ref, argv[0]);
931 expand_loose_notes_ref(&remote_ref);
932 o.remote_ref = remote_ref.buf;
@@ -953,7 +955,7 @@ static int merge(int argc, const char **argv, const char *prefix)
955 }
956
957 strbuf_addf(&msg, "notes: Merged notes from %s into %s",
956 - remote_ref.buf, default_notes_ref());
958 + remote_ref.buf, notes_ref);
959 strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
960
961 result = notes_merge(&o, t, &result_oid);
@@ -961,7 +963,7 @@ static int merge(int argc, const char **argv, const char *prefix)
963 if (result >= 0) /* Merge resulted (trivially) in result_oid */
964 /* Update default notes ref with new commit */
965 refs_update_ref(get_main_ref_store(the_repository), msg.buf,
964 - default_notes_ref(), &result_oid, NULL, 0,
966 + notes_ref, &result_oid, NULL, 0,
967 UPDATE_REFS_DIE_ON_ERR);
968 else { /* Merge has unresolved conflicts */
969 struct worktree **worktrees;
@@ -973,14 +975,14 @@ static int merge(int argc, const char **argv, const char *prefix)
975 /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
976 worktrees = get_worktrees();
977 wt = find_shared_symref(worktrees, "NOTES_MERGE_REF",
976 - default_notes_ref());
978 + notes_ref);
979 if (wt)
980 die(_("a notes merge into %s is already in-progress at %s"),
979 - default_notes_ref(), wt->path);
981 + notes_ref, wt->path);
982 free_worktrees(worktrees);
981 - if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", default_notes_ref(), NULL))
983 + if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", notes_ref, NULL))
984 die(_("failed to store link to current notes ref (%s)"),
983 - default_notes_ref());
985 + notes_ref);
986 fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
987 "and commit the result with 'git notes merge --commit', "
988 "or abort the merge with 'git notes merge --abort'.\n"),
@@ -988,6 +990,7 @@ static int merge(int argc, const char **argv, const char *prefix)
990 }
991
992 free_notes(t);
993 + free(notes_ref);
994 strbuf_release(&remote_ref);
995 strbuf_release(&msg);
996 return result < 0; /* return non-zero on conflicts */
@@ -1084,6 +1087,7 @@ static int prune(int argc, const char **argv, const char *prefix)
1087 static int get_ref(int argc, const char **argv, const char *prefix)
1088 {
1089 struct option options[] = { OPT_END() };
1090 + char *notes_ref;
1091 argc = parse_options(argc, argv, prefix, options,
1092 git_notes_get_ref_usage, 0);
1093
@@ -1092,7 +1096,9 @@ static int get_ref(int argc, const char **argv, const char *prefix)
1096 usage_with_options(git_notes_get_ref_usage, options);
1097 }
1098
1095 - puts(default_notes_ref());
1099 + notes_ref = default_notes_ref(the_repository);
1100 + puts(notes_ref);
1101 + free(notes_ref);
1102 return 0;
1103 }
1104
config.c
-8
@@ -1555,14 +1555,6 @@ static int git_default_core_config(const char *var, const char *value,
1555 return git_config_string(&check_roundtrip_encoding, var, value);
1556 }
1557
1558 - if (!strcmp(var, "core.notesref")) {
1559 - if (!value)
1560 - return config_error_nonbool(var);
1561 - free(notes_ref_name);
1562 - notes_ref_name = xstrdup(value);
1563 - return 0;
1564 - }
1565 -
1558 if (!strcmp(var, "core.editor")) {
1559 FREE_AND_NULL(editor_program);
1560 return git_config_string(&editor_program, var, value);
environment.c
-1
@@ -67,7 +67,6 @@ enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
67 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
68 #endif
69 enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
70 -char *notes_ref_name;
70 int grafts_keep_true_parents;
71 int core_apply_sparse_checkout;
72 int core_sparse_checkout_cone;
environment.h
-2
@@ -203,8 +203,6 @@ enum object_creation_mode {
203 };
204 extern enum object_creation_mode object_creation_mode;
205
206 -extern char *notes_ref_name;
207 -
206 extern int grafts_keep_true_parents;
207
208 extern int repository_format_precious_objects;
notes.c
+13 -8
@@ -992,15 +992,16 @@ static int notes_display_config(const char *k, const char *v,
992 return 0;
993 }
994
995 -const char *default_notes_ref(void)
995 +char *default_notes_ref(struct repository *repo)
996 {
997 - const char *notes_ref = NULL;
997 + char *notes_ref = NULL;
998 +
999 if (!notes_ref)
999 - notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
1000 + notes_ref = xstrdup_or_null(getenv(GIT_NOTES_REF_ENVIRONMENT));
1001 if (!notes_ref)
1001 - notes_ref = notes_ref_name; /* value of core.notesRef config */
1002 + repo_config_get_string(repo, "core.notesref", &notes_ref);
1003 if (!notes_ref)
1003 - notes_ref = GIT_NOTES_DEFAULT_REF;
1004 + notes_ref = xstrdup(GIT_NOTES_DEFAULT_REF);
1005 return notes_ref;
1006 }
1007
@@ -1010,13 +1011,14 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
1011 struct object_id oid, object_oid;
1012 unsigned short mode;
1013 struct leaf_node root_tree;
1014 + char *to_free = NULL;
1015
1016 if (!t)
1017 t = &default_notes_tree;
1018 assert(!t->initialized);
1019
1020 if (!notes_ref)
1019 - notes_ref = default_notes_ref();
1021 + notes_ref = to_free = default_notes_ref(the_repository);
1022 update_ref_namespace(NAMESPACE_NOTES, xstrdup(notes_ref));
1023
1024 if (!combine_notes)
@@ -1033,7 +1035,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
1035
1036 if (flags & NOTES_INIT_EMPTY ||
1037 repo_get_oid_treeish(the_repository, notes_ref, &object_oid))
1036 - return;
1038 + goto out;
1039 if (flags & NOTES_INIT_WRITABLE && refs_read_ref(get_main_ref_store(the_repository), notes_ref, &object_oid))
1040 die("Cannot use notes ref %s", notes_ref);
1041 if (get_tree_entry(the_repository, &object_oid, "", &oid, &mode))
@@ -1043,6 +1045,9 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
1045 oidclr(&root_tree.key_oid, the_repository->hash_algo);
1046 oidcpy(&root_tree.val_oid, &oid);
1047 load_subtree(t, &root_tree, t->root, 0);
1048 +
1049 +out:
1050 + free(to_free);
1051 }
1052
1053 struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
@@ -1105,7 +1110,7 @@ void load_display_notes(struct display_notes_opt *opt)
1110
1111 if (!opt || opt->use_default_notes > 0 ||
1112 (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) {
1108 - string_list_append(&display_notes_refs, default_notes_ref());
1113 + string_list_append_nodup(&display_notes_refs, default_notes_ref(the_repository));
1114 display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT);
1115 if (display_ref_env) {
1116 string_list_add_refs_from_colon_sep(&display_notes_refs,
notes.h
+2 -1
@@ -4,6 +4,7 @@
4 #include "string-list.h"
5
6 struct object_id;
7 +struct repository;
8 struct strbuf;
9
10 /*
@@ -70,7 +71,7 @@ extern struct notes_tree {
71 * 3. The value of the core.notesRef config variable, if set
72 * 4. GIT_NOTES_DEFAULT_REF (i.e. "refs/notes/commits")
73 */
73 -const char *default_notes_ref(void);
74 +char *default_notes_ref(struct repository *repo);
75
76 /*
77 * Flags controlling behaviour of notes tree initialization