Git development
 help / color / mirror / Atom feed
From: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
To: git@vger.kernel.org
Cc: christian.couder@gmail.com, ps@pks.im, gitster@pobox.com,
	usmanakinyemi202@gmail.com, siddharthasthana31@gmail.com,
	kaartic.sivaraam@gmail.com, okhuomonajayi54@gmail.com
Subject: [PATCH v6 3/6] notes: use the tree's repository when reading notes
Date: Thu, 26 Nov 2026 22:33:03 +0100	[thread overview]
Message-ID: <20261126213306.87466-4-okhuomonajayi54@gmail.com> (raw)
In-Reply-To: <20261126213306.87466-1-okhuomonajayi54@gmail.com>

In the previous step we added a '.repo' member to struct notes_tree
so a notes tree records which repository it belongs to. Notes trees
are passed down many call chains, but functions like note_tree_find()
still read objects and hash algorithms from the_repository.

There is a catch in the notes API's NULL contract. Passing NULL in
place of a notes tree is allowed and means "use the default notes
tree", so a 't' handed around a call chain may be NULL and 't->repo'
cannot be dereferenced blindly. The high-level entry points cope with
this: add_note(), remove_note() and get_note() replace a NULL 't'
with the default notes tree via notes_tree_or_default() on entry, so
afterwards 't->repo' is safe. The internal format_note() does the
same swap and initializes the tree if needed. The leaf helpers
note_tree_find(), add_non_note() and load_subtree() do not check for
NULL themselves; they rely on being reached only through those entry
points (get_note() -> note_tree_find(), and note_tree_search() lazily
unpacking a subtree through load_subtree(), still after the tree has
been resolved).

Given those guarantees, read objects and hash algorithms from t->repo
instead of the_repository in note_tree_find(), add_non_note(),
load_subtree(), remove_note() and format_note().

copy_note() also takes a notes tree but does not resolve NULL before
using it, so a mechanical the_repository -> t->repo replacement there
would be unsafe; it is handled in the next patch.

Signed-off-by: Okhuomon Ajayi <okhuomonajayi54@gmail.com>
---
 notes.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/notes.c b/notes.c
index 6bfe80f176..da63d35935 100644
--- a/notes.c
+++ b/notes.c
@@ -153,7 +153,7 @@ static struct leaf_node *note_tree_find(struct notes_tree *t,
 	void **p = note_tree_search(t, &tree, &n, key_sha1);
 	if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
 		struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
-		if (hasheq(key_sha1, l->key_oid.hash, the_repository->hash_algo))
+		if (hasheq(key_sha1, l->key_oid.hash, t->repo->hash_algo))
 			return l;
 	}
 	return NULL;
@@ -357,7 +357,7 @@ static void add_non_note(struct notes_tree *t, char *path,
 	n->next = NULL;
 	n->path = path;
 	n->mode = mode;
-	oidread(&n->oid, sha1, the_repository->hash_algo);
+	oidread(&n->oid, sha1, t->repo->hash_algo);
 	t->prev_non_note = n;
 
 	if (!t->first_non_note) {
@@ -402,9 +402,9 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 	void *buf;
 	struct tree_desc desc;
 	struct name_entry entry;
-	const unsigned hashsz = the_hash_algo->rawsz;
+	const unsigned hashsz = t->repo->hash_algo->rawsz;
 
-	buf = fill_tree_descriptor(the_repository, &desc, &subtree->val_oid);
+	buf = fill_tree_descriptor(t->repo, &desc, &subtree->val_oid);
 	if (!buf)
 		die("Could not read %s for notes-index",
 		     oid_to_hex(&subtree->val_oid));
@@ -462,8 +462,8 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 		CALLOC_ARRAY(l, 1);
 		oidcpy(&l->key_oid, &object_oid);
 		oidcpy(&l->val_oid, &entry.oid);
-		oid_set_algo(&l->key_oid, the_hash_algo);
-		oid_set_algo(&l->val_oid, the_hash_algo);
+		oid_set_algo(&l->key_oid, t->repo->hash_algo);
+		oid_set_algo(&l->val_oid, t->repo->hash_algo);
 		if (note_tree_insert(t, node, n, l, type,
 				     combine_notes_concatenate))
 			die("Failed to load %s %s into notes tree "
@@ -493,7 +493,7 @@ static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
 				strbuf_addch(&non_note_path, '/');
 			}
 			strbuf_addstr(&non_note_path, entry.path);
-			oid_set_algo(&entry.oid, the_hash_algo);
+			oid_set_algo(&entry.oid, t->repo->hash_algo);
 			add_non_note(t, strbuf_detach(&non_note_path, NULL),
 				     entry.mode, entry.oid.hash);
 		}
@@ -1173,8 +1173,8 @@ int remove_note(struct notes_tree *t, const unsigned char *object_sha1)
 	struct leaf_node l;
 
 	t = notes_tree_or_default(t);
-	oidread(&l.key_oid, object_sha1, the_repository->hash_algo);
-	oidclr(&l.val_oid, the_repository->hash_algo);
+	oidread(&l.key_oid, object_sha1, t->repo->hash_algo);
+	oidclr(&l.val_oid, t->repo->hash_algo);
 	note_tree_remove(t, t->root, 0, &l);
 	if (is_null_oid(&l.val_oid)) /* no note was removed */
 		return 1;
@@ -1298,7 +1298,7 @@ static void format_note(struct notes_tree *t, const struct object_id *object_oid
 	if (!oid)
 		return;
 
-	if (!(msg = odb_read_object(the_repository->objects, oid, &type, &msglen)) ||
+	if (!(msg = odb_read_object(t->repo->objects, oid, &type, &msglen)) ||
 	    type != OBJ_BLOB) {
 		free(msg);
 		return;
-- 
2.56.0


  parent reply	other threads:[~2026-11-26 21:33 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-31 10:45 [Outreachy PATCH] notes-utils: use the passed repository for notes refs Okhuomon Ajayi
2026-11-02 11:01 ` Patrick Steinhardt
2026-11-02 11:10   ` Okhuomon Ajayi
2026-11-03  8:49 ` [Outreachy PATCH v2 0/2] notes-utils: stop using the_repository Okhuomon Ajayi
2026-11-03  8:49   ` [Outreachy PATCH v2 1/2] notes-utils: use the passed repository for notes refs Okhuomon Ajayi
2026-11-03  9:45     ` Karthik Nayak
2026-11-03 18:01       ` Junio C Hamano
2026-11-05  7:43         ` [Outreachy PATCH] notes-utils: hide functions that still need the_repository Okhuomon Ajayi
2026-11-05 14:22           ` Patrick Steinhardt
2026-11-05 14:47             ` Okhuomon Ajayi
2026-11-05 17:56             ` Junio C Hamano
2026-11-05 20:33               ` [Outreachy PATCH 0/6] notes: use the repository stored in the notes tree Okhuomon Ajayi
2026-11-05 20:33                 ` [Outreachy PATCH 1/6] notes: store the repository in struct notes_tree Okhuomon Ajayi
2026-11-09  9:18                   ` Patrick Steinhardt
2026-11-09 10:27                     ` Okhuomon Ajayi
2026-11-05 20:33                 ` [Outreachy PATCH 2/6] notes: use the tree's repository when reading notes Okhuomon Ajayi
2026-11-05 20:33                 ` [Outreachy PATCH 3/6] notes: use the tree's repository when writing and pruning Okhuomon Ajayi
2026-11-05 20:33                 ` [Outreachy PATCH 4/6] notes: pass the repository to the ref glob helpers Okhuomon Ajayi
2026-11-05 20:33                 ` [Outreachy PATCH 5/6] notes: pass the repository to the combine_notes callbacks Okhuomon Ajayi
2026-11-09  9:18                   ` Patrick Steinhardt
2026-11-09 10:11                     ` Okhuomon Ajayi
2026-11-24 15:28                       ` Christian Couder
2026-11-24 16:13                         ` Okhuomon Ajayi
2026-11-13 11:40                     ` Okhuomon Ajayi
2026-11-23  9:23                       ` Okhuomon Ajayi
2026-11-24 15:26                   ` Christian Couder
2026-11-24 15:49                     ` Okhuomon Ajayi
2026-11-05 20:33                 ` [Outreachy PATCH 6/6] notes-utils: use the given repository Okhuomon Ajayi
2026-11-24 17:21                 ` [PATCH v2 0/5] notes: start passing the repository through notes.[ch] Okhuomon Ajayi
2026-11-24 17:21                   ` [PATCH v2 1/5] notes: store the repository in struct notes_tree Okhuomon Ajayi
2026-11-25  0:34                     ` Junio C Hamano
2026-11-25  6:39                       ` Okhuomon Ajayi
2026-11-24 17:21                   ` [PATCH v2 2/5] notes: use the tree's repository when reading notes Okhuomon Ajayi
2026-11-25  0:54                     ` Junio C Hamano
2026-11-25  6:45                       ` Okhuomon Ajayi
2026-11-25  8:25                         ` Christian Couder
2026-11-25 18:13                           ` Okhuomon Ajayi
2026-11-25 19:09                             ` Junio C Hamano
2026-11-25 20:36                               ` Okhuomon Ajayi
2026-11-25 17:50                         ` Junio C Hamano
2026-11-25 18:19                           ` Okhuomon Ajayi
2026-11-24 17:21                   ` [PATCH v2 3/5] notes: use the tree's repository when writing and pruning Okhuomon Ajayi
2026-11-25  0:54                     ` Junio C Hamano
2026-11-25  6:48                       ` Okhuomon Ajayi
2026-11-24 17:21                   ` [PATCH v2 4/5] notes: pass the repository to the ref glob helpers Okhuomon Ajayi
2026-11-25  5:12                     ` Junio C Hamano
2026-11-24 17:21                   ` [PATCH v2 5/5] notes: pass the repository to the combine_notes callbacks Okhuomon Ajayi
2026-11-24 22:50                   ` [PATCH v2 0/5] notes: start passing the repository through notes.[ch] Junio C Hamano
2026-11-25  6:24                     ` Okhuomon Ajayi
2026-11-25  1:14                   ` Junio C Hamano
2026-11-25  6:35                     ` Okhuomon Ajayi
2026-11-25 22:02                   ` [PATCH v5 0/6] " Okhuomon Ajayi
2026-11-25 22:02                     ` [PATCH v5 1/6] notes: add notes_tree_or_default() Okhuomon Ajayi
2026-11-26  2:57                       ` Junio C Hamano
2026-11-26 16:57                         ` Okhuomon Ajayi
2026-11-25 22:02                     ` [PATCH v5 2/6] notes: store the repository in struct notes_tree Okhuomon Ajayi
2026-11-26  3:17                       ` Junio C Hamano
2026-11-25 22:02                     ` [PATCH v5 3/6] notes: use the tree's repository when reading notes Okhuomon Ajayi
2026-11-26  3:37                       ` Junio C Hamano
2026-11-26 20:57                         ` Okhuomon Ajayi
2026-11-25 22:02                     ` [PATCH v5 4/6] notes: use the tree's repository when writing and pruning Okhuomon Ajayi
2026-11-25 22:02                     ` [PATCH v5 5/6] notes: pass the repository to the ref glob helpers Okhuomon Ajayi
2026-11-25 22:02                     ` [PATCH v5 6/6] notes: pass the repository to the combine_notes callbacks Okhuomon Ajayi
2026-11-26  3:57                     ` [PATCH v5 0/6] notes: start passing the repository through notes.[ch] Junio C Hamano
2026-11-26 21:07                       ` Okhuomon Ajayi
2026-11-26 21:33                   ` [PATCH v6 " Okhuomon Ajayi
2026-11-26 21:33                     ` [PATCH v6 1/6] notes: add notes_tree_or_default() Okhuomon Ajayi
2026-11-26 21:33                     ` [PATCH v6 2/6] notes: store the repository in struct notes_tree Okhuomon Ajayi
2026-11-26 21:33                     ` Okhuomon Ajayi [this message]
2026-11-27 13:13                       ` [PATCH v6 3/6] notes: use the tree's repository when reading notes Christian Couder
2026-11-26 21:33                     ` [PATCH v6 4/6] notes: use the tree's repository when writing and pruning Okhuomon Ajayi
2026-11-26 21:33                     ` [PATCH v6 5/6] notes: pass the repository to the ref glob helpers Okhuomon Ajayi
2026-11-26 21:33                     ` [PATCH v6 6/6] notes: pass the repository to the combine_notes callbacks Okhuomon Ajayi
2026-11-27 13:22                     ` [PATCH v6 0/6] notes: start passing the repository through notes.[ch] Christian Couder
2026-11-27 13:52                       ` Okhuomon Ajayi
2026-11-27 18:19                         ` Junio C Hamano
2026-11-27 22:04                     ` [PATCH v7 " Okhuomon Ajayi
2026-11-27 22:04                       ` [PATCH v7 1/6] notes: add notes_tree_or_default() Okhuomon Ajayi
2026-11-27 22:04                       ` [PATCH v7 2/6] notes: store the repository in struct notes_tree Okhuomon Ajayi
2026-11-27 22:04                       ` [PATCH v7 3/6] notes: use the tree's repository when reading notes Okhuomon Ajayi
2026-11-27 22:04                       ` [PATCH v7 4/6] notes: use the tree's repository when writing and pruning Okhuomon Ajayi
2026-11-27 22:04                       ` [PATCH v7 5/6] notes: pass the repository to the ref glob helpers Okhuomon Ajayi
2026-11-27 22:04                       ` [PATCH v7 6/6] notes: pass the repository to the combine_notes callbacks Okhuomon Ajayi
2026-11-03  8:49   ` [Outreachy PATCH v2 2/2] notes-utils: pass the repository to init_copy_notes_for_rewrite() Okhuomon Ajayi
2026-11-03  9:48   ` [Outreachy PATCH v2 0/2] notes-utils: stop using the_repository Karthik Nayak
2026-11-03 10:27     ` Patrick Steinhardt

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=20261126213306.87466-4-okhuomonajayi54@gmail.com \
    --to=okhuomonajayi54@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=kaartic.sivaraam@gmail.com \
    --cc=ps@pks.im \
    --cc=siddharthasthana31@gmail.com \
    --cc=usmanakinyemi202@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