threads / patch / 66725

patchnotes-utils: use the passed repository for notes refs

Subject: [Outreachy PATCH] notes-utils: use the passed repository for notes refs

## tl;dr

The two-patch cleanup was reviewed favourably, but Junio Hamano warned that the wider notes code still depends on the_repository. Read the story.

replies: 9people: 4as markdown or json

Okhuomon Ajayi· Oct 31, 2026, 10:45 UTC · lore

create_notes_commit() and commit_notes() receive a repository, but read and update the notes ref in the_repository.

Use the repository they are given.

Keep USE_THE_REPOSITORY_VARIABLE because init_copy_notes_for_rewrite() still uses the_repository.

Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
---
 notes-utils.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Show changes to notes-utils.c +2 −2
diff --git a/notes-utils.c b/notes-utils.c
index 5c1c75d5b8..d4606bc576 100644
--- a/notes-utils.c
+++ b/notes-utils.c
@@ -26,7 +26,7 @@ void create_notes_commit(struct repository *r,
 	if (!parents) {
 		/* Deduce parent commit from t->ref */
 		struct object_id parent_oid;
-		if (!refs_read_ref(get_main_ref_store(the_repository), t->ref, &parent_oid)) {
+		if (!refs_read_ref(get_main_ref_store(r), t->ref, &parent_oid)) {
 			struct commit *parent = lookup_commit(r, &parent_oid);
 			if (repo_parse_commit(r, parent))
 				die("Failed to find/parse commit %s", t->ref);
@@ -61,7 +61,7 @@ void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
 
 	create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
 	strbuf_insertstr(&buf, 0, "notes: ");
-	refs_update_ref(get_main_ref_store(the_repository), buf.buf,
+	refs_update_ref(get_main_ref_store(r), buf.buf,
 			t->update_ref, &commit_oid, NULL, 0,
 			UPDATE_REFS_DIE_ON_ERR);
 

base-commit: 50fe5cc63f9cd6e89b87a2c9ccf96a444f4cfdb7
-- 
2.56.0
Patrick Steinhardt· Nov 2, 2026, 11:01 UTC · re: Okhuomon Ajayi · lore

Re: [Outreachy PATCH] notes-utils: use the passed repository for notes refs

On Sat, Oct 31, 2026 at 11:45:26AM +0100, Okhuomon Ajayi wrote:
Show 7 quoted lines
> create_notes_commit() and commit_notes() receive a repository,
> but read and update the notes ref in the_repository.
> 
> Use the repository they are given.
> 
> Keep USE_THE_REPOSITORY_VARIABLE because
> init_copy_notes_for_rewrite() still uses the_repository.

I don't quite understand patches like this. They are _so_ close to getting rid of `USE_THE_REPOSITORY_VARIABLE`, but then they just skip on the final change that would make them work. I've already asked about this on one of your preceding patches.

Also, could you maybe reduce the frequency a bit with which you send new topics? There's a ton of in-flight patch series by you in this vicinity, and it gets a bit overwhelming. May I ask you to maybe batch related patches into bigger series (maybe 5-10 commits) so that the reviewer can focus on way fewer patch series overall, and then have less series in flight at the same point in time?

Thanks!
Patrick
Okhuomon Ajayi· Nov 2, 2026, 11:10 UTC · re: Patrick Steinhardt · lore

Re: [Outreachy PATCH] notes-utils: use the passed repository for notes refs

On Mon, Nov 2, 2026 at 12:01 PM Patrick Steinhardt <ps@pks.im> wrote:
> I don't quite understand patches like this. They are _so_ close to
> getting rid of `USE_THE_REPOSITORY_VARIABLE`, but then they just skip on
> the final change that would make them work. I've already asked about
> this on one of your preceding patches.

Thanks, Patrick. You're right, I should have gone the full way here after your comment on bundle-uri.

I'll send a v2 that also passes the repository to init_copy_notes_for_rewrite() and updates its callers, so that notes-utils.c can drop USE_THE_REPOSITORY_VARIABLE.

Show 6 quoted lines
> Also, could you maybe reduce the frequency a bit with which you send new
> topics? There's a ton of in-flight patch series by you in this vicinity,
> and it gets a bit overwhelming. May I ask you to maybe batch related
> patches into bigger series (maybe 5-10 commits) so that the reviewer can
> focus on way fewer patch series overall, and then have less series in
> flight at the same point in time?

Sorry about that. I'll stop sending new topics for now, and batch related conversions into one series from now on.

Thanks, Okhuomon

On Mon, Nov 2, 2026 at 12:01 PM Patrick Steinhardt <ps@pks.im> wrote:
Show 25 quoted lines
>
> On Sat, Oct 31, 2026 at 11:45:26AM +0100, Okhuomon Ajayi wrote:
> > create_notes_commit() and commit_notes() receive a repository,
> > but read and update the notes ref in the_repository.
> >
> > Use the repository they are given.
> >
> > Keep USE_THE_REPOSITORY_VARIABLE because
> > init_copy_notes_for_rewrite() still uses the_repository.
>
> I don't quite understand patches like this. They are _so_ close to
> getting rid of `USE_THE_REPOSITORY_VARIABLE`, but then they just skip on
> the final change that would make them work. I've already asked about
> this on one of your preceding patches.
>
> Also, could you maybe reduce the frequency a bit with which you send new
> topics? There's a ton of in-flight patch series by you in this vicinity,
> and it gets a bit overwhelming. May I ask you to maybe batch related
> patches into bigger series (maybe 5-10 commits) so that the reviewer can
> focus on way fewer patch series overall, and then have less series in
> flight at the same point in time?
>
> Thanks!
>
> Patrick
Okhuomon Ajayi· Nov 3, 2026, 08:49 UTC · re: Okhuomon Ajayi · lore

[Outreachy PATCH v2 0/2] notes-utils: stop using the_repository

This series removes the_repository from notes-utils.c.
Changes since v1:
- Add patch 2, which passes a repository to
  init_copy_notes_for_rewrite() and drops USE_THE_REPOSITORY_VARIABLE
  from notes-utils.c, as Patrick suggested. v1 stopped one step short
  of that.

The callers in builtin/am.c and builtin/notes.c pass the_repository, as those files still use it everywhere. copy() in builtin/notes.c does receive a repo that could be passed down instead, if that is preferred.

Thanks, Patrick, for the review.
Okhuomon Ajayi (2):
  notes-utils: use the passed repository for notes refs
  notes-utils: pass the repository to init_copy_notes_for_rewrite()
 builtin/am.c    |  2 +-
 builtin/notes.c |  2 +-
 notes-utils.c   | 11 +++++------
 notes-utils.h   |  3 ++-
 sequencer.c     |  2 +-
 5 files changed, 10 insertions(+), 10 deletions(-)
base-commit: 50fe5cc63f9cd6e89b87a2c9ccf96a444f4cfdb7
-- 
2.56.0
Okhuomon Ajayi· Nov 3, 2026, 08:49 UTC · re: Okhuomon Ajayi · lore

[Outreachy PATCH v2 1/2] notes-utils: use the passed repository for notes refs

create_notes_commit() and commit_notes() receive a repository, but read and update the notes ref in the_repository.

Use the repository they are given.

Keep USE_THE_REPOSITORY_VARIABLE because init_copy_notes_for_rewrite() still uses the_repository.

Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
---
 notes-utils.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Show changes to notes-utils.c +2 −2
diff --git a/notes-utils.c b/notes-utils.c
index 5c1c75d5b8..d4606bc576 100644
--- a/notes-utils.c
+++ b/notes-utils.c
@@ -26,7 +26,7 @@ void create_notes_commit(struct repository *r,
 	if (!parents) {
 		/* Deduce parent commit from t->ref */
 		struct object_id parent_oid;
-		if (!refs_read_ref(get_main_ref_store(the_repository), t->ref, &parent_oid)) {
+		if (!refs_read_ref(get_main_ref_store(r), t->ref, &parent_oid)) {
 			struct commit *parent = lookup_commit(r, &parent_oid);
 			if (repo_parse_commit(r, parent))
 				die("Failed to find/parse commit %s", t->ref);
@@ -61,7 +61,7 @@ void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
 
 	create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
 	strbuf_insertstr(&buf, 0, "notes: ");
-	refs_update_ref(get_main_ref_store(the_repository), buf.buf,
+	refs_update_ref(get_main_ref_store(r), buf.buf,
 			t->update_ref, &commit_oid, NULL, 0,
 			UPDATE_REFS_DIE_ON_ERR);
 
-- 
2.56.0
Karthik Nayak· Nov 3, 2026, 09:45 UTC · re: Okhuomon Ajayi · lore

Re: [Outreachy PATCH v2 1/2] notes-utils: use the passed repository for notes refs

Okhuomon Ajayi <okhuomonajayi54@gmail.com> writes:
Show 5 quoted lines
> create_notes_commit() and commit_notes() receive a repository,
> but read and update the notes ref in the_repository.
>
> Use the repository they are given.
>
Nit: It would also be nice to talk about why this is the same repository
and if its safe to do this swap.

Sometimes it's also useful to look at the history and argue about why it is the way it is.

For this file, all `the_repository` usages were removed first in 1d18d7581c (notes-utils.c: remove the_repository references, 2019-01-12), then it was added back in 2e5c4758b7 (cocci: apply rules to rewrite callers of "refs" interfaces, 2024-05-07). So seems like the latter was an automated addition of `the_repository`.

Show 18 quoted lines
> Keep USE_THE_REPOSITORY_VARIABLE because
> init_copy_notes_for_rewrite() still uses the_repository.
>
> Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
> ---
>  notes-utils.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/notes-utils.c b/notes-utils.c
> index 5c1c75d5b8..d4606bc576 100644
> --- a/notes-utils.c
> +++ b/notes-utils.c
> @@ -26,7 +26,7 @@ void create_notes_commit(struct repository *r,
>  	if (!parents) {
>  		/* Deduce parent commit from t->ref */
>  		struct object_id parent_oid;
> -		if (!refs_read_ref(get_main_ref_store(the_repository), t->ref, &parent_oid)) {
> +		if (!refs_read_ref(get_main_ref_store(r), t->ref, &parent_oid)) {

Here, the repository we read the reference from and the commit from should be the same. So r == the_repository.

Show 12 quoted lines
>  			struct commit *parent = lookup_commit(r, &parent_oid);
>  			if (repo_parse_commit(r, parent))
>  				die("Failed to find/parse commit %s", t->ref);
> @@ -61,7 +61,7 @@ void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
>
>  	create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
>  	strbuf_insertstr(&buf, 0, "notes: ");
> -	refs_update_ref(get_main_ref_store(the_repository), buf.buf,
> +	refs_update_ref(get_main_ref_store(r), buf.buf,
>  			t->update_ref, &commit_oid, NULL, 0,
>  			UPDATE_REFS_DIE_ON_ERR);
>
Similar here.
> --
> 2.56.0
The patch looks good.
Junio C Hamano· Nov 3, 2026, 18:01 UTC · re: Karthik Nayak · lore

Re: [Outreachy PATCH v2 1/2] notes-utils: use the passed repository for notes refs

Karthik Nayak <karthik.188@gmail.com> writes:
> Okhuomon Ajayi <okhuomonajayi54@gmail.com> writes:
>
>> Keep USE_THE_REPOSITORY_VARIABLE because
>> init_copy_notes_for_rewrite() still uses the_repository.
Unfortunately, this is a highly misleading comment to leave here.

The next person would be misled into thinking that it would be just a matter of giving a "struct repository *r" argument to that function to lift the USE_THE_REPOSITORY_VARIABLE declaration from there.

Looking at what <notes-utils.h> exports, the file has quite some way to go to be free from the_repository.

 * create_notes_commit() takes "struct repository *" to pretend as
   if it can work with any repository, but the first function it
   calls is write_notes_tree() that eventually calls into
   odb_write_object() with hardcoded the_repository->objects.
 * commit_notes() calls the above create_notes_commit() and shares
   the same problem.
 * init_copy_notes_for_rewrite(), in addition to having its own
   reference to the_repository, calls load_notes_trees() that makes
   calls to init_notes() that works only on the_repository.
 * copy_note_for_rewrite() calls copy_note() which calls add_note(),
   which eventually calls into load_subtree() that can only work
   with the_repository.

In short, I think adding "struct repository *" to anything in <notes-utils.h> is not yet a productive exercise at this point, before cleaning up what is in the underlying things exported from <notes.h>.

This is a tangent but there are precedents to mark the functions that still work only on the_repository or the_hash_algo:

    $ git grep 'ifdef *USE_THE' \*.h
    environment.h:# ifdef USE_THE_REPOSITORY_VARIABLE
    hash.h:#ifdef USE_THE_REPOSITORY_VARIABLE
    hex.h:#ifdef USE_THE_REPOSITORY_VARIABLE
    path.h:# ifdef USE_THE_REPOSITORY_VARIABLE
    repository.h:#ifdef USE_THE_REPOSITORY_VARIABLE

Perhaps we need similar protection to forbid use of these functions that still assume the_repository is available in this file?

 notes-utils.h | 4 ++++
 1 file changed, 4 insertions(+)
Show changes to diff +4 −0
diff --git c/notes-utils.h w/notes-utils.h
index c54b1fe141..1503991cf6 100644
--- c/notes-utils.h
+++ w/notes-utils.h
@@ -7,6 +7,9 @@ struct commit_list;
 struct object_id;
 struct repository;
 
+/* most of the things in notes.h still only work with the_repository */
+# ifdef USE_THE_REPOSITORY_VARIABLE
+
 /*
  * Create new notes commit from the given notes tree
  *
@@ -52,4 +55,5 @@ void finish_copy_notes_for_rewrite(struct repository *r,
 				   struct notes_rewrite_cfg *c,
 				   const char *msg);
 
+# endif
 #endif
Okhuomon Ajayi· Nov 3, 2026, 08:49 UTC · re: Okhuomon Ajayi · lore

[Outreachy PATCH v2 2/2] notes-utils: pass the repository to init_copy_notes_for_rewrite()

init_copy_notes_for_rewrite() reads the notes.rewrite* config from the_repository, as it is not given a repository. This is the last use of the_repository in notes-utils.c.

Add a repository parameter to it. commit_post_rewrite() in sequencer.c already has one and passes it on. The callers in builtin/am.c and builtin/notes.c still use the_repository, so pass that for now.

Drop USE_THE_REPOSITORY_VARIABLE, as notes-utils.c no longer uses the_repository.

Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
---
 builtin/am.c    | 2 +-
 builtin/notes.c | 2 +-
 notes-utils.c   | 7 +++----
 notes-utils.h   | 3 ++-
 sequencer.c     | 2 +-
 5 files changed, 8 insertions(+), 8 deletions(-)
Show changes to 5 files +8 −8

builtin/am.c, builtin/notes.c, notes-utils.c, notes-utils.h, sequencer.c

diff --git a/builtin/am.c b/builtin/am.c
index e9623b8307..daa4b601b6 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -536,7 +536,7 @@ static int copy_notes_for_rebase(const struct am_state *state)
 
 	assert(state->rebasing);
 
-	c = init_copy_notes_for_rewrite("rebase");
+	c = init_copy_notes_for_rewrite(the_repository, "rebase");
 	if (!c)
 		return 0;
 
diff --git a/builtin/notes.c b/builtin/notes.c
index 9f1f0ec840..20f7bb8377 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -366,7 +366,7 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
 	const char *msg = "Notes added by 'git notes copy'";
 
 	if (rewrite_cmd) {
-		c = init_copy_notes_for_rewrite(rewrite_cmd);
+		c = init_copy_notes_for_rewrite(the_repository, rewrite_cmd);
 		if (!c)
 			return 0;
 	} else {
diff --git a/notes-utils.c b/notes-utils.c
index d4606bc576..3a3b84f79c 100644
--- a/notes-utils.c
+++ b/notes-utils.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "config.h"
 #include "commit.h"
@@ -134,7 +132,8 @@ static int notes_rewrite_config(const char *k, const char *v,
 }
 
 
-struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
+struct notes_rewrite_cfg *init_copy_notes_for_rewrite(struct repository *r,
+						      const char *cmd)
 {
 	struct notes_rewrite_cfg *c = xmalloc(sizeof(struct notes_rewrite_cfg));
 	const char *rewrite_mode_env = getenv(GIT_NOTES_REWRITE_MODE_ENVIRONMENT);
@@ -162,7 +161,7 @@ struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd)
 		c->refs_from_env = 1;
 		string_list_add_refs_from_colon_sep(c->refs, rewrite_refs_env);
 	}
-	repo_config(the_repository, notes_rewrite_config, c);
+	repo_config(r, notes_rewrite_config, c);
 	if (!c->enabled || !c->refs->nr) {
 		string_list_clear(c->refs, 0);
 		free(c->refs);
diff --git a/notes-utils.h b/notes-utils.h
index c54b1fe141..ef3c0f6732 100644
--- a/notes-utils.h
+++ b/notes-utils.h
@@ -45,7 +45,8 @@ struct notes_rewrite_cfg {
 };
 
 int parse_notes_merge_strategy(const char *v, enum notes_merge_strategy *s);
-struct notes_rewrite_cfg *init_copy_notes_for_rewrite(const char *cmd);
+struct notes_rewrite_cfg *init_copy_notes_for_rewrite(struct repository *r,
+						      const char *cmd);
 int copy_note_for_rewrite(struct notes_rewrite_cfg *c,
 			  const struct object_id *from_obj, const struct object_id *to_obj);
 void finish_copy_notes_for_rewrite(struct repository *r,
diff --git a/sequencer.c b/sequencer.c
index 508ec71792..a40243c56b 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -1366,7 +1366,7 @@ void commit_post_rewrite(struct repository *r,
 {
 	struct notes_rewrite_cfg *cfg;
 
-	cfg = init_copy_notes_for_rewrite("amend");
+	cfg = init_copy_notes_for_rewrite(r, "amend");
 	if (cfg) {
 		/* we are amending, so old_head is not NULL */
 		copy_note_for_rewrite(cfg, &old_head->object.oid, new_head);
-- 
2.56.0
Karthik Nayak· Nov 3, 2026, 09:48 UTC · re: Okhuomon Ajayi · lore

Re: [Outreachy PATCH v2 0/2] notes-utils: stop using the_repository

Okhuomon Ajayi <okhuomonajayi54@gmail.com> writes:
Show 15 quoted lines
> This series removes the_repository from notes-utils.c.
>
> Changes since v1:
> - Add patch 2, which passes a repository to
>   init_copy_notes_for_rewrite() and drops USE_THE_REPOSITORY_VARIABLE
>   from notes-utils.c, as Patrick suggested. v1 stopped one step short
>   of that.
>
> The callers in builtin/am.c and builtin/notes.c pass the_repository,
> as those files still use it everywhere. copy() in builtin/notes.c
> does receive a repo that could be passed down instead, if that is
> preferred.
>
> Thanks, Patrick, for the review.
>
Overall the patches look good to me.
[snip]
Patrick Steinhardt· Nov 3, 2026, 10:27 UTC · re: Karthik Nayak · lore

Re: [Outreachy PATCH v2 0/2] notes-utils: stop using the_repository

On Tue, Nov 03, 2026 at 03:48:01AM -0600, Karthik Nayak wrote:
Show 19 quoted lines
> Okhuomon Ajayi <okhuomonajayi54@gmail.com> writes:
> 
> > This series removes the_repository from notes-utils.c.
> >
> > Changes since v1:
> > - Add patch 2, which passes a repository to
> >   init_copy_notes_for_rewrite() and drops USE_THE_REPOSITORY_VARIABLE
> >   from notes-utils.c, as Patrick suggested. v1 stopped one step short
> >   of that.
> >
> > The callers in builtin/am.c and builtin/notes.c pass the_repository,
> > as those files still use it everywhere. copy() in builtin/notes.c
> > does receive a repo that could be passed down instead, if that is
> > preferred.
> >
> > Thanks, Patrick, for the review.
> >
> 
> Overall the patches look good to me.
Agreed, the patches look good to me, too.
Patrick

← back to recent threads