git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: Calvin Wan <calvinwan@google.com>,
	Justin Tobler <jltobler@gmail.com>,
	Junio C Hamano <gitster@pobox.com>,
	karthik nayak <karthik.188@gmail.com>
Subject: [PATCH v3 21/21] environment: stop storing "core.notesRef" globally
Date: Thu, 12 Sep 2024 13:30:26 +0200	[thread overview]
Message-ID: <08672e8a073109e16e3522bb8b6c05e905643610.1726139990.git.ps@pks.im> (raw)
In-Reply-To: <cover.1726139990.git.ps@pks.im>

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>
---
 builtin/notes.c | 22 ++++++++++++++--------
 config.c        |  8 --------
 environment.c   |  1 -
 environment.h   |  2 --
 notes.c         | 21 +++++++++++++--------
 notes.h         |  3 ++-
 6 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/builtin/notes.c b/builtin/notes.c
index 04f9dfb7fbd..5d594a07240 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -897,6 +897,7 @@ static int merge(int argc, const char **argv, const char *prefix)
 			      1, PARSE_OPT_NONEG),
 		OPT_END()
 	};
+	char *notes_ref;
 
 	argc = parse_options(argc, argv, prefix, options,
 			     git_notes_merge_usage, 0);
@@ -924,7 +925,8 @@ static int merge(int argc, const char **argv, const char *prefix)
 	if (do_commit)
 		return merge_commit(&o);
 
-	o.local_ref = default_notes_ref();
+	notes_ref = default_notes_ref(the_repository);
+	o.local_ref = notes_ref;
 	strbuf_addstr(&remote_ref, argv[0]);
 	expand_loose_notes_ref(&remote_ref);
 	o.remote_ref = remote_ref.buf;
@@ -953,7 +955,7 @@ static int merge(int argc, const char **argv, const char *prefix)
 	}
 
 	strbuf_addf(&msg, "notes: Merged notes from %s into %s",
-		    remote_ref.buf, default_notes_ref());
+		    remote_ref.buf, notes_ref);
 	strbuf_add(&(o.commit_msg), msg.buf + 7, msg.len - 7); /* skip "notes: " */
 
 	result = notes_merge(&o, t, &result_oid);
@@ -961,7 +963,7 @@ static int merge(int argc, const char **argv, const char *prefix)
 	if (result >= 0) /* Merge resulted (trivially) in result_oid */
 		/* Update default notes ref with new commit */
 		refs_update_ref(get_main_ref_store(the_repository), msg.buf,
-				default_notes_ref(), &result_oid, NULL, 0,
+				notes_ref, &result_oid, NULL, 0,
 				UPDATE_REFS_DIE_ON_ERR);
 	else { /* Merge has unresolved conflicts */
 		struct worktree **worktrees;
@@ -973,14 +975,14 @@ static int merge(int argc, const char **argv, const char *prefix)
 		/* Store ref-to-be-updated into .git/NOTES_MERGE_REF */
 		worktrees = get_worktrees();
 		wt = find_shared_symref(worktrees, "NOTES_MERGE_REF",
-					default_notes_ref());
+					notes_ref);
 		if (wt)
 			die(_("a notes merge into %s is already in-progress at %s"),
-			    default_notes_ref(), wt->path);
+			    notes_ref, wt->path);
 		free_worktrees(worktrees);
-		if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", default_notes_ref(), NULL))
+		if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", notes_ref, NULL))
 			die(_("failed to store link to current notes ref (%s)"),
-			    default_notes_ref());
+			    notes_ref);
 		fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s "
 				  "and commit the result with 'git notes merge --commit', "
 				  "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)
 	}
 
 	free_notes(t);
+	free(notes_ref);
 	strbuf_release(&remote_ref);
 	strbuf_release(&msg);
 	return result < 0; /* return non-zero on conflicts */
@@ -1084,6 +1087,7 @@ static int prune(int argc, const char **argv, const char *prefix)
 static int get_ref(int argc, const char **argv, const char *prefix)
 {
 	struct option options[] = { OPT_END() };
+	char *notes_ref;
 	argc = parse_options(argc, argv, prefix, options,
 			     git_notes_get_ref_usage, 0);
 
@@ -1092,7 +1096,9 @@ static int get_ref(int argc, const char **argv, const char *prefix)
 		usage_with_options(git_notes_get_ref_usage, options);
 	}
 
-	puts(default_notes_ref());
+	notes_ref = default_notes_ref(the_repository);
+	puts(notes_ref);
+	free(notes_ref);
 	return 0;
 }
 
diff --git a/config.c b/config.c
index 53c68f3da61..1266eab0860 100644
--- a/config.c
+++ b/config.c
@@ -1555,14 +1555,6 @@ static int git_default_core_config(const char *var, const char *value,
 		return git_config_string(&check_roundtrip_encoding, var, value);
 	}
 
-	if (!strcmp(var, "core.notesref")) {
-		if (!value)
-			return config_error_nonbool(var);
-		free(notes_ref_name);
-		notes_ref_name = xstrdup(value);
-		return 0;
-	}
-
 	if (!strcmp(var, "core.editor")) {
 		FREE_AND_NULL(editor_program);
 		return git_config_string(&editor_program, var, value);
diff --git a/environment.c b/environment.c
index 9dd000cda36..a2ce9980818 100644
--- a/environment.c
+++ b/environment.c
@@ -67,7 +67,6 @@ enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
 #endif
 enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
-char *notes_ref_name;
 int grafts_keep_true_parents;
 int core_apply_sparse_checkout;
 int core_sparse_checkout_cone;
diff --git a/environment.h b/environment.h
index aa38133da9c..923e12661e1 100644
--- a/environment.h
+++ b/environment.h
@@ -203,8 +203,6 @@ enum object_creation_mode {
 };
 extern enum object_creation_mode object_creation_mode;
 
-extern char *notes_ref_name;
-
 extern int grafts_keep_true_parents;
 
 extern int repository_format_precious_objects;
diff --git a/notes.c b/notes.c
index da42df282d5..f4f18daf07e 100644
--- a/notes.c
+++ b/notes.c
@@ -992,15 +992,16 @@ static int notes_display_config(const char *k, const char *v,
 	return 0;
 }
 
-const char *default_notes_ref(void)
+char *default_notes_ref(struct repository *repo)
 {
-	const char *notes_ref = NULL;
+	char *notes_ref = NULL;
+
 	if (!notes_ref)
-		notes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);
+		notes_ref = xstrdup_or_null(getenv(GIT_NOTES_REF_ENVIRONMENT));
 	if (!notes_ref)
-		notes_ref = notes_ref_name; /* value of core.notesRef config */
+		repo_config_get_string(repo, "core.notesref", &notes_ref);
 	if (!notes_ref)
-		notes_ref = GIT_NOTES_DEFAULT_REF;
+		notes_ref = xstrdup(GIT_NOTES_DEFAULT_REF);
 	return notes_ref;
 }
 
@@ -1010,13 +1011,14 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
 	struct object_id oid, object_oid;
 	unsigned short mode;
 	struct leaf_node root_tree;
+	char *to_free = NULL;
 
 	if (!t)
 		t = &default_notes_tree;
 	assert(!t->initialized);
 
 	if (!notes_ref)
-		notes_ref = default_notes_ref();
+		notes_ref = to_free = default_notes_ref(the_repository);
 	update_ref_namespace(NAMESPACE_NOTES, xstrdup(notes_ref));
 
 	if (!combine_notes)
@@ -1033,7 +1035,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
 
 	if (flags & NOTES_INIT_EMPTY ||
 	    repo_get_oid_treeish(the_repository, notes_ref, &object_oid))
-		return;
+		goto out;
 	if (flags & NOTES_INIT_WRITABLE && refs_read_ref(get_main_ref_store(the_repository), notes_ref, &object_oid))
 		die("Cannot use notes ref %s", notes_ref);
 	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,
 	oidclr(&root_tree.key_oid, the_repository->hash_algo);
 	oidcpy(&root_tree.val_oid, &oid);
 	load_subtree(t, &root_tree, t->root, 0);
+
+out:
+	free(to_free);
 }
 
 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)
 
 	if (!opt || opt->use_default_notes > 0 ||
 	    (opt->use_default_notes == -1 && !opt->extra_notes_refs.nr)) {
-		string_list_append(&display_notes_refs, default_notes_ref());
+		string_list_append_nodup(&display_notes_refs, default_notes_ref(the_repository));
 		display_ref_env = getenv(GIT_NOTES_DISPLAY_REF_ENVIRONMENT);
 		if (display_ref_env) {
 			string_list_add_refs_from_colon_sep(&display_notes_refs,
diff --git a/notes.h b/notes.h
index 235216944bc..6dc6d7b2654 100644
--- a/notes.h
+++ b/notes.h
@@ -4,6 +4,7 @@
 #include "string-list.h"
 
 struct object_id;
+struct repository;
 struct strbuf;
 
 /*
@@ -70,7 +71,7 @@ extern struct notes_tree {
  * 3. The value of the core.notesRef config variable, if set
  * 4. GIT_NOTES_DEFAULT_REF (i.e. "refs/notes/commits")
  */
-const char *default_notes_ref(void);
+char *default_notes_ref(struct repository *repo);
 
 /*
  * Flags controlling behaviour of notes tree initialization
-- 
2.46.0.551.gc5ee8f2d1c.dirty


  parent reply	other threads:[~2024-09-12 11:30 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-29  9:38 [PATCH 00/21] environment: guard reliance on `the_repository` Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 01/21] environment: make `get_git_dir()` accept a repository Patrick Steinhardt
2024-08-29 20:15   ` Justin Tobler
2024-08-30  7:42     ` Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 02/21] environment: make `get_git_common_dir()` " Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 03/21] environment: make `get_object_directory()` " Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 04/21] environment: make `get_index_file()` " Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 05/21] environment: make `get_graft_file()` " Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 06/21] environment: make `get_git_work_tree()` " Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 07/21] config: document `read_early_config()` and `read_very_early_config()` Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 08/21] config: make dependency on repo in `read_early_config()` explicit Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 09/21] environment: move `odb_mkstemp()` into object layer Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 10/21] environment: make `get_git_namespace()` self-contained Patrick Steinhardt
2024-08-29  9:38 ` [PATCH 11/21] environment: move `set_git_dir()` and related into setup layer Patrick Steinhardt
2024-08-29  9:39 ` [PATCH 12/21] environment: reorder header to split out `the_repository`-free section Patrick Steinhardt
2024-08-29  9:39 ` [PATCH 13/21] environment: guard state depending on a repository Patrick Steinhardt
2024-08-29  9:39 ` [PATCH 14/21] repo-settings: split out declarations into a standalone header Patrick Steinhardt
2024-08-29  9:39 ` [PATCH 15/21] branch: stop modifying `log_all_ref_updates` variable Patrick Steinhardt
2024-08-29  9:39 ` [PATCH 16/21] refs: stop modifying global " Patrick Steinhardt
2024-08-29  9:39 ` [PATCH 17/21] repo-settings: track defaults close to `struct repo_settings` Patrick Steinhardt
2024-08-29  9:39 ` [PATCH 18/21] environment: stop storing "core.logAllRefUpdates" globally Patrick Steinhardt
2024-08-29  9:39 ` [PATCH 19/21] environment: stop storing "core.preferSymlinkRefs" globally Patrick Steinhardt
2024-08-29  9:39 ` [PATCH 20/21] environment: stop storing "core.warnAmbiguousRefs" globally Patrick Steinhardt
2024-08-29  9:39 ` [PATCH 21/21] environment: stop storing "core.notesRef" globally Patrick Steinhardt
2024-08-29 19:59 ` [PATCH 00/21] environment: guard reliance on `the_repository` Junio C Hamano
2024-08-30  6:58   ` Patrick Steinhardt
2024-08-30 16:32     ` Junio C Hamano
2024-09-02  9:29       ` Patrick Steinhardt
2024-08-30  9:08 ` [PATCH v2 " Patrick Steinhardt
2024-08-30  9:08   ` [PATCH v2 01/21] environment: make `get_git_dir()` accept a repository Patrick Steinhardt
2024-09-11 21:12     ` karthik nayak
2024-09-12 11:17       ` Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 02/21] environment: make `get_git_common_dir()` " Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 03/21] environment: make `get_object_directory()` " Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 04/21] environment: make `get_index_file()` " Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 05/21] environment: make `get_graft_file()` " Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 06/21] environment: make `get_git_work_tree()` " Patrick Steinhardt
2024-09-11 15:15     ` Justin Tobler
2024-09-12 11:16       ` Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 07/21] config: document `read_early_config()` and `read_very_early_config()` Patrick Steinhardt
2024-09-11 15:59     ` Justin Tobler
2024-09-12 11:17       ` Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 08/21] config: make dependency on repo in `read_early_config()` explicit Patrick Steinhardt
2024-09-04  1:46     ` James Liu
2024-09-04  7:14       ` Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 09/21] environment: move `odb_mkstemp()` into object layer Patrick Steinhardt
2024-09-11 21:26     ` karthik nayak
2024-09-12 11:17       ` Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 10/21] environment: make `get_git_namespace()` self-contained Patrick Steinhardt
2024-09-11 16:21     ` Justin Tobler
2024-08-30  9:09   ` [PATCH v2 11/21] environment: move `set_git_dir()` and related into setup layer Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 12/21] environment: reorder header to split out `the_repository`-free section Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 13/21] environment: guard state depending on a repository Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 14/21] repo-settings: split out declarations into a standalone header Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 15/21] repo-settings: track defaults close to `struct repo_settings` Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 16/21] branch: stop modifying `log_all_ref_updates` variable Patrick Steinhardt
2024-09-11 17:14     ` Justin Tobler
2024-09-12 11:17       ` Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 17/21] refs: stop modifying global " Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 18/21] environment: stop storing "core.logAllRefUpdates" globally Patrick Steinhardt
2024-09-12 11:10     ` karthik nayak
2024-08-30  9:09   ` [PATCH v2 19/21] environment: stop storing "core.preferSymlinkRefs" globally Patrick Steinhardt
2024-08-30  9:09   ` [PATCH v2 20/21] environment: stop storing "core.warnAmbiguousRefs" globally Patrick Steinhardt
2024-09-04  2:10     ` James Liu
2024-08-30  9:10   ` [PATCH v2 21/21] environment: stop storing "core.notesRef" globally Patrick Steinhardt
2024-09-04  2:12   ` [PATCH v2 00/21] environment: guard reliance on `the_repository` James Liu
2024-09-04  7:14     ` Patrick Steinhardt
2024-09-12 11:14   ` karthik nayak
2024-09-12 11:17     ` Patrick Steinhardt
2024-09-12 11:29 ` [PATCH v3 " Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 01/21] environment: make `get_git_dir()` accept a repository Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 02/21] environment: make `get_git_common_dir()` " Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 03/21] environment: make `get_object_directory()` " Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 04/21] environment: make `get_index_file()` " Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 05/21] environment: make `get_graft_file()` " Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 06/21] environment: make `get_git_work_tree()` " Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 07/21] config: document `read_early_config()` and `read_very_early_config()` Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 08/21] config: make dependency on repo in `read_early_config()` explicit Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 09/21] environment: move object database functions into object layer Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 10/21] environment: make `get_git_namespace()` self-contained Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 11/21] environment: move `set_git_dir()` and related into setup layer Patrick Steinhardt
2024-09-12 11:29   ` [PATCH v3 12/21] environment: reorder header to split out `the_repository`-free section Patrick Steinhardt
2024-09-12 11:30   ` [PATCH v3 13/21] environment: guard state depending on a repository Patrick Steinhardt
2024-09-12 11:30   ` [PATCH v3 14/21] repo-settings: split out declarations into a standalone header Patrick Steinhardt
2024-09-12 11:30   ` [PATCH v3 15/21] repo-settings: track defaults close to `struct repo_settings` Patrick Steinhardt
2024-09-12 11:30   ` [PATCH v3 16/21] branch: stop modifying `log_all_ref_updates` variable Patrick Steinhardt
2024-09-12 11:30   ` [PATCH v3 17/21] refs: stop modifying global " Patrick Steinhardt
2024-09-12 11:30   ` [PATCH v3 18/21] environment: stop storing "core.logAllRefUpdates" globally Patrick Steinhardt
2024-09-12 11:30   ` [PATCH v3 19/21] environment: stop storing "core.preferSymlinkRefs" globally Patrick Steinhardt
2024-09-12 11:30   ` [PATCH v3 20/21] environment: stop storing "core.warnAmbiguousRefs" globally Patrick Steinhardt
2024-09-12 11:30   ` Patrick Steinhardt [this message]
2024-09-12 20:40   ` [PATCH v3 00/21] environment: guard reliance on `the_repository` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=08672e8a073109e16e3522bb8b6c05e905643610.1726139990.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=calvinwan@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jltobler@gmail.com \
    --cc=karthik.188@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).