Skip to contentSkip to conversation

refs: drop all references to `the_repository`

PATCH Superseded Patrick Steinhardt 3 participants 33 messages May 16, 2024 – May 17, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover refs: drop all references to `the_repository`

Hi,

in "ps/refs-without-the-repository", we have removed all functions in the refs API that could be trivially converted to accept a proper ref store as input. This was a mechanical change, and because the resulting patches were quite large by themselves already, I decided to push out further dependencies on the_repository in refs-related code to a follow up.

This patch series here is that follow-up and removes all references to the_repository in "refs.c" and "refs/". This includes both explicit use of the_repository, but also implicit use via the_hash_algo.

The series is based on 19fe900cfc (The fourth batch, 2024-05-15) and pulls in "ps/refs-without-the-repository" at c8f815c208 (refs: remove functions without ref store, 2024-05-07) as dependency. It applies cleanly to both next and seen at the current point in time.

Patrick

Patrick Steinhardt (16):
refs: adjust names for init and init_db callbacks
refs: rename init_db callback to avoid confusion
refs: implement releasing ref storages
refs: track ref stores via strmap
refs: pass repo when retrieving submodule ref store
refs: refactor resolve_gitlink_ref() to accept a repository
refs: retrieve worktree ref stores via associated repository
refs: convert iteration over replace refs to accept ref store
refs: pass ref store when detecting dangling symrefs
refs: move object peeling into "object.c"
refs: pass repo when peeling objects
refs: drop git_default_branch_name()
refs: remove dwim_log()
refs/files: use correct repository
refs/files: remove references to the_hash_algo refs/packed: remove references to the_hash_algo

 attr.c                      |   3 +-
 builtin/clone.c             |   5 +-
 builtin/describe.c          |   2 +-
 builtin/fetch.c             |   3 +-
 builtin/gc.c                |   2 +-
 builtin/pack-objects.c      |   6 +-
 builtin/reflog.c            |   2 +-
 builtin/remote.c            |   3 +-
 builtin/repack.c            |   2 +-
 builtin/replace.c           |  13 +-
 builtin/show-ref.c          |   2 +-
 builtin/submodule--helper.c |  14 +-
 builtin/update-index.c      |   5 +-
 builtin/var.c               |   2 +-
 builtin/worktree.c          |   2 +-
 combine-diff.c              |   3 +-
 commit-graph.c              |   2 +-
 diff-lib.c                  |   3 +-
 dir.c                       |   3 +-
 ls-refs.c                   |   2 +-
 midx-write.c                |   2 +-
 object-file.c               |   2 +-
 object.c                    |  23 ++++
 object.h                    |  35 +++++
 read-cache.c                |   5 +-
 ref-filter.c                |   2 +-
 reflog-walk.c               |   4 +-
 reflog.c                    |   2 +-
 refs.c                      | 266 ++++++++++--------------------------
 refs.h                      |  51 +++----
 refs/debug.c                |  15 +-
 refs/files-backend.c        |  58 ++++----
 refs/iterator.c             |   6 +-
 refs/packed-backend.c       |  66 +++++----
 refs/packed-backend.h       |   6 +-
 refs/ref-cache.c            |   5 +-
 refs/refs-internal.h        |  55 ++------
 refs/reftable-backend.c     |  38 +++++-
 remote.c                    |  12 +-
 replace-object.c            |  10 +-
 repository.c                |  12 ++
 repository.h                |  14 ++
 setup.c                     |   7 +-
 submodule.c                 |   3 +-
 t/helper/test-reach.c       |   2 +-
 t/helper/test-ref-store.c   |   2 +-
 tag.c                       |   4 +-
 tag.h                       |   2 +-
 unpack-trees.c              |   3 +-
 upload-pack.c               |   2 +-
 worktree.c                  |   2 +
 worktree.h                  |   2 +
 52 files changed, 405 insertions(+), 392 deletions(-)
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#1 lore raw

PATCH 1/16 refs: adjust names for `init` and `init_db` callbacks

The names of the functions that implement the the init and init_db callbacks in the "files" and "packed" backends do not match the names of the callbacks, which is inconsistent. Rename them so that they match, which makes it easier to discover their respective implementations.

---
 refs/files-backend.c  | 10 +++++-----
 refs/packed-backend.c | 16 ++++++++--------
 refs/packed-backend.h |  6 +++---
 3 files changed, 16 insertions(+), 16 deletions(-)
refs/files-backend.c +5 -5
index a098d14ea0..2c9d5e0747 100644
@@ -89,9 +89,9 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)
  * Create a new submodule ref cache and add it to the internal
  * set of caches.
  */
-static struct ref_store *files_ref_store_create(struct repository *repo,
-						const char *gitdir,
-						unsigned int flags)
+static struct ref_store *files_ref_store_init(struct repository *repo,
+					      const char *gitdir,
+					      unsigned int flags)
 {
 	struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
 	struct ref_store *ref_store = (struct ref_store *)refs;
@@ -102,7 +102,7 @@ static struct ref_store *files_ref_store_create(struct repository *repo,
 	get_common_dir_noenv(&sb, gitdir);
 	refs->gitcommondir = strbuf_detach(&sb, NULL);
 	refs->packed_ref_store =
-		packed_ref_store_create(repo, refs->gitcommondir, flags);
+		packed_ref_store_init(repo, refs->gitcommondir, flags);
 
 	chdir_notify_reparent("files-backend $GIT_DIR", &refs->base.gitdir);
 	chdir_notify_reparent("files-backend $GIT_COMMONDIR",
@@ -3283,7 +3283,7 @@ static int files_init_db(struct ref_store *ref_store,
 
 struct ref_storage_be refs_be_files = {
 	.name = "files",
-	.init = files_ref_store_create,
+	.init = files_ref_store_init,
 	.init_db = files_init_db,
 	.transaction_prepare = files_transaction_prepare,
 	.transaction_finish = files_transaction_finish,
refs/packed-backend.c +8 -8
index 4e826c05ff..a3c5a75073 100644
@@ -200,9 +200,9 @@ static int release_snapshot(struct snapshot *snapshot)
 	}
 }
 
-struct ref_store *packed_ref_store_create(struct repository *repo,
-					  const char *gitdir,
-					  unsigned int store_flags)
+struct ref_store *packed_ref_store_init(struct repository *repo,
+					const char *gitdir,
+					unsigned int store_flags)
 {
 	struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
 	struct ref_store *ref_store = (struct ref_store *)refs;
@@ -1244,9 +1244,9 @@ int packed_refs_is_locked(struct ref_store *ref_store)
 static const char PACKED_REFS_HEADER[] =
 	"# pack-refs with: peeled fully-peeled sorted \n";
 
-static int packed_init_db(struct ref_store *ref_store UNUSED,
-			  int flags UNUSED,
-			  struct strbuf *err UNUSED)
+static int packed_ref_store_init_db(struct ref_store *ref_store UNUSED,
+				    int flags UNUSED,
+				    struct strbuf *err UNUSED)
 {
 	/* Nothing to do. */
 	return 0;
@@ -1706,8 +1706,8 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
 
 struct ref_storage_be refs_be_packed = {
 	.name = "packed",
-	.init = packed_ref_store_create,
-	.init_db = packed_init_db,
+	.init = packed_ref_store_init,
+	.init_db = packed_ref_store_init_db,
 	.transaction_prepare = packed_transaction_prepare,
 	.transaction_finish = packed_transaction_finish,
 	.transaction_abort = packed_transaction_abort,
refs/packed-backend.h +3 -3
index 9dd8a344c3..09437ad13b 100644
@@ -13,9 +13,9 @@ struct ref_transaction;
  * even among packed refs.
  */
 
-struct ref_store *packed_ref_store_create(struct repository *repo,
-					  const char *gitdir,
-					  unsigned int store_flags);
+struct ref_store *packed_ref_store_init(struct repository *repo,
+					const char *gitdir,
+					unsigned int store_flags);
 
 /*
  * Lock the packed-refs file for writing. Flags is passed to
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#2 lore raw

Re: [PATCH 01/16] refs: adjust names for `init` and `init_db` callbacks

Quoted 1 line · Patrick Steinhardt

The names of the functions that implement the the init and init_db

s/the//

Quoted 105 lines

callbacks in the "files" and "packed" backends do not match the names of the callbacks, which is inconsistent. Rename them so that they match, which makes it easier to discover their respective implementations.

---
 refs/files-backend.c  | 10 +++++-----
 refs/packed-backend.c | 16 ++++++++--------
 refs/packed-backend.h |  6 +++---
 3 files changed, 16 insertions(+), 16 deletions(-)
refs/files-backend.c +5 -5
index a098d14ea0..2c9d5e0747 100644
@@ -89,9 +89,9 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)
  * Create a new submodule ref cache and add it to the internal
  * set of caches.
  */
-static struct ref_store *files_ref_store_create(struct repository *repo,
-						const char *gitdir,
-						unsigned int flags)
+static struct ref_store *files_ref_store_init(struct repository *repo,
+					      const char *gitdir,
+					      unsigned int flags)
 {
 	struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
 	struct ref_store *ref_store = (struct ref_store *)refs;
@@ -102,7 +102,7 @@ static struct ref_store *files_ref_store_create(struct repository *repo,
 	get_common_dir_noenv(&sb, gitdir);
 	refs->gitcommondir = strbuf_detach(&sb, NULL);
 	refs->packed_ref_store =
-		packed_ref_store_create(repo, refs->gitcommondir, flags);
+		packed_ref_store_init(repo, refs->gitcommondir, flags);
 
 	chdir_notify_reparent("files-backend $GIT_DIR", &refs->base.gitdir);
 	chdir_notify_reparent("files-backend $GIT_COMMONDIR",
@@ -3283,7 +3283,7 @@ static int files_init_db(struct ref_store *ref_store,
 
 struct ref_storage_be refs_be_files = {
 	.name = "files",
-	.init = files_ref_store_create,
+	.init = files_ref_store_init,
 	.init_db = files_init_db,
 	.transaction_prepare = files_transaction_prepare,
 	.transaction_finish = files_transaction_finish,
refs/packed-backend.c +8 -8
index 4e826c05ff..a3c5a75073 100644
@@ -200,9 +200,9 @@ static int release_snapshot(struct snapshot *snapshot)
 	}
 }
 
-struct ref_store *packed_ref_store_create(struct repository *repo,
-					  const char *gitdir,
-					  unsigned int store_flags)
+struct ref_store *packed_ref_store_init(struct repository *repo,
+					const char *gitdir,
+					unsigned int store_flags)
 {
 	struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
 	struct ref_store *ref_store = (struct ref_store *)refs;
@@ -1244,9 +1244,9 @@ int packed_refs_is_locked(struct ref_store *ref_store)
 static const char PACKED_REFS_HEADER[] =
 	"# pack-refs with: peeled fully-peeled sorted \n";
 
-static int packed_init_db(struct ref_store *ref_store UNUSED,
-			  int flags UNUSED,
-			  struct strbuf *err UNUSED)
+static int packed_ref_store_init_db(struct ref_store *ref_store UNUSED,
+				    int flags UNUSED,
+				    struct strbuf *err UNUSED)
 {
 	/* Nothing to do. */
 	return 0;
@@ -1706,8 +1706,8 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
 
 struct ref_storage_be refs_be_packed = {
 	.name = "packed",
-	.init = packed_ref_store_create,
-	.init_db = packed_init_db,
+	.init = packed_ref_store_init,
+	.init_db = packed_ref_store_init_db,
 	.transaction_prepare = packed_transaction_prepare,
 	.transaction_finish = packed_transaction_finish,
 	.transaction_abort = packed_transaction_abort,
refs/packed-backend.h +3 -4
index 9dd8a344c3..09437ad13b 100644
@@ -13,9 +13,9 @@ struct ref_transaction;
  * even among packed refs.
  */
 
-struct ref_store *packed_ref_store_create(struct repository *repo,
-					  const char *gitdir,
-					  unsigned int store_flags);
+struct ref_store *packed_ref_store_init(struct repository *repo,
+					const char *gitdir,
+					unsigned int store_flags);
 
 /*
  * Lock the packed-refs file for writing. Flags is passed to
--
Quoted 1 line

2.45.1.190.g19fe900cfc.dirty

The rest of this patch looks good.

#3 lore raw

PATCH 2/16 refs: rename `init_db` callback to avoid confusion

In reply to Patrick Steinhardt

Reference backends have two callbacks init and init_db. The similarity of these two callbacks has repeatedly tripped myself whenever I was looking at those, where I always had to look up which of them does what.

Rename the init_db callback to create, which should hopefully be clearer.

---
 builtin/worktree.c      |  2 +-
 refs.c                  |  4 ++--
 refs.h                  |  4 ++--
 refs/debug.c            |  8 ++++----
 refs/files-backend.c    | 12 ++++++------
 refs/packed-backend.c   |  8 ++++----
 refs/refs-internal.h    |  8 ++++----
 refs/reftable-backend.c | 10 +++++-----
 setup.c                 |  2 +-
 9 files changed, 29 insertions(+), 29 deletions(-)
builtin/worktree.c +1 -1
index 108cfa156a..cce083d409 100644
@@ -509,7 +509,7 @@ static int add_worktree(const char *path, const char *refname,
 	}
 	wt_refs = get_worktree_ref_store(wt);
 
-	ret = refs_init_db(wt_refs, REFS_INIT_DB_IS_WORKTREE, &sb);
+	ret = ref_store_create(wt_refs, REF_STORE_CREATE_IS_WORKTREE, &sb);
 	if (ret)
 		goto done;
 
refs.c +2 -2
index a26c50a401..ad7987efab 100644
@@ -1938,9 +1938,9 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
 }
 
 /* backend functions */
-int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err)
+int ref_store_create(struct ref_store *refs, int flags, struct strbuf *err)
 {
-	return refs->be->init_db(refs, flags, err);
+	return refs->be->create(refs, flags, err);
 }
 
 int resolve_gitlink_ref(const char *submodule, const char *refname,
refs.h +2 -2
index d02dd79ca6..e9804e4c22 100644
@@ -114,9 +114,9 @@ int should_autocreate_reflog(const char *refname);
 
 int is_branch(const char *refname);
 
-#define REFS_INIT_DB_IS_WORKTREE (1 << 0)
+#define REF_STORE_CREATE_IS_WORKTREE (1 << 0)
 
-int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err);
+int ref_store_create(struct ref_store *refs, int flags, struct strbuf *err);
 
 /*
  * Return the peeled value of the oid currently being iterated via
refs/debug.c +4 -4
index c7531b17f0..58fb4557ed 100644
@@ -33,11 +33,11 @@ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor
 	return (struct ref_store *)res;
 }
 
-static int debug_init_db(struct ref_store *refs, int flags, struct strbuf *err)
+static int debug_create(struct ref_store *refs, int flags, struct strbuf *err)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
-	int res = drefs->refs->be->init_db(drefs->refs, flags, err);
-	trace_printf_key(&trace_refs, "init_db: %d\n", res);
+	int res = drefs->refs->be->create(drefs->refs, flags, err);
+	trace_printf_key(&trace_refs, "create: %d\n", res);
 	return res;
 }
 
@@ -427,7 +427,7 @@ static int debug_reflog_expire(struct ref_store *ref_store, const char *refname,
 struct ref_storage_be refs_be_debug = {
 	.name = "debug",
 	.init = NULL,
-	.init_db = debug_init_db,
+	.create = debug_create,
 
 	/*
 	 * None of these should be NULL. If the "files" backend (in
refs/files-backend.c +6 -6
index 2c9d5e0747..f766d18d5a 100644
@@ -3236,12 +3236,12 @@ static int files_reflog_expire(struct ref_store *ref_store,
 	return -1;
 }
 
-static int files_init_db(struct ref_store *ref_store,
-			 int flags,
-			 struct strbuf *err UNUSED)
+static int files_ref_store_create(struct ref_store *ref_store,
+				  int flags,
+				  struct strbuf *err UNUSED)
 {
 	struct files_ref_store *refs =
-		files_downcast(ref_store, REF_STORE_WRITE, "init_db");
+		files_downcast(ref_store, REF_STORE_WRITE, "create");
 	struct strbuf sb = STRBUF_INIT;
 
 	/*
@@ -3264,7 +3264,7 @@ static int files_init_db(struct ref_store *ref_store,
 	 * There is no need to create directories for common refs when creating
 	 * a worktree ref store.
 	 */
-	if (!(flags & REFS_INIT_DB_IS_WORKTREE)) {
+	if (!(flags & REF_STORE_CREATE_IS_WORKTREE)) {
 		/*
 		 * Create .git/refs/{heads,tags}
 		 */
@@ -3284,7 +3284,7 @@ static int files_init_db(struct ref_store *ref_store,
 struct ref_storage_be refs_be_files = {
 	.name = "files",
 	.init = files_ref_store_init,
-	.init_db = files_init_db,
+	.create = files_ref_store_create,
 	.transaction_prepare = files_transaction_prepare,
 	.transaction_finish = files_transaction_finish,
 	.transaction_abort = files_transaction_abort,
refs/packed-backend.c +4 -4
index a3c5a75073..716513efed 100644
@@ -1244,9 +1244,9 @@ int packed_refs_is_locked(struct ref_store *ref_store)
 static const char PACKED_REFS_HEADER[] =
 	"# pack-refs with: peeled fully-peeled sorted \n";
 
-static int packed_ref_store_init_db(struct ref_store *ref_store UNUSED,
-				    int flags UNUSED,
-				    struct strbuf *err UNUSED)
+static int packed_ref_store_create(struct ref_store *ref_store UNUSED,
+				   int flags UNUSED,
+				   struct strbuf *err UNUSED)
 {
 	/* Nothing to do. */
 	return 0;
@@ -1707,7 +1707,7 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
 struct ref_storage_be refs_be_packed = {
 	.name = "packed",
 	.init = packed_ref_store_init,
-	.init_db = packed_ref_store_init_db,
+	.create = packed_ref_store_create,
 	.transaction_prepare = packed_transaction_prepare,
 	.transaction_finish = packed_transaction_finish,
 	.transaction_abort = packed_transaction_abort,
refs/refs-internal.h +4 -4
index 56641aa57a..eb42212764 100644
@@ -530,9 +530,9 @@ typedef struct ref_store *ref_store_init_fn(struct repository *repo,
 					    const char *gitdir,
 					    unsigned int flags);
 
-typedef int ref_init_db_fn(struct ref_store *refs,
-			   int flags,
-			   struct strbuf *err);
+typedef int ref_store_create_fn(struct ref_store *refs,
+				int flags,
+				struct strbuf *err);
 
 typedef int ref_transaction_prepare_fn(struct ref_store *refs,
 				       struct ref_transaction *transaction,
@@ -668,7 +668,7 @@ typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refnam
 struct ref_storage_be {
 	const char *name;
 	ref_store_init_fn *init;
-	ref_init_db_fn *init_db;
+	ref_store_create_fn *create;
 
 	ref_transaction_prepare_fn *transaction_prepare;
 	ref_transaction_finish_fn *transaction_finish;
refs/reftable-backend.c +5 -5
index 010ef811b6..a4bb71cd76 100644
@@ -293,12 +293,12 @@ static struct ref_store *reftable_be_init(struct repository *repo,
 	return &refs->base;
 }
 
-static int reftable_be_init_db(struct ref_store *ref_store,
-			       int flags UNUSED,
-			       struct strbuf *err UNUSED)
+static int reftable_be_create(struct ref_store *ref_store,
+			      int flags UNUSED,
+			      struct strbuf *err UNUSED)
 {
 	struct reftable_ref_store *refs =
-		reftable_be_downcast(ref_store, REF_STORE_WRITE, "init_db");
+		reftable_be_downcast(ref_store, REF_STORE_WRITE, "create");
 	struct strbuf sb = STRBUF_INIT;
 
 	strbuf_addf(&sb, "%s/reftable", refs->base.gitdir);
@@ -2248,7 +2248,7 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
 struct ref_storage_be refs_be_reftable = {
 	.name = "reftable",
 	.init = reftable_be_init,
-	.init_db = reftable_be_init_db,
+	.create = reftable_be_create,
 	.transaction_prepare = reftable_be_transaction_prepare,
 	.transaction_finish = reftable_be_transaction_finish,
 	.transaction_abort = reftable_be_transaction_abort,
setup.c +1 -1
index c7d3375645..4a738f4c90 100644
@@ -2049,7 +2049,7 @@ void create_reference_database(unsigned int ref_storage_format,
 	int reinit = is_reinit();
 
 	repo_set_ref_storage_format(the_repository, ref_storage_format);
-	if (refs_init_db(get_main_ref_store(the_repository), 0, &err))
+	if (ref_store_create(get_main_ref_store(the_repository), 0, &err))
 		die("failed to set up refs db: %s", err.buf);
 
 	/*
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#4 lore raw

Re: [PATCH 02/16] refs: rename `init_db` callback to avoid confusion

Quoted 3 lines · Patrick Steinhardt

Reference backends have two callbacks init and init_db. The similarity of these two callbacks has repeatedly tripped myself whenever I was looking at those, where I always had to look up which of them does

Nit: It would read better with something like "repeatedly confused me whenever I was looking at them".

[snip]

#5 lore raw

Re: [PATCH 02/16] refs: rename `init_db` callback to avoid confusion

In reply to Patrick Steinhardt
Quoted 7 lines · Patrick Steinhardt

Reference backends have two callbacks init and init_db. The similarity of these two callbacks has repeatedly tripped myself whenever I was looking at those, where I always had to look up which of them does what.

Rename the init_db callback to create, which should hopefully be clearer.

Hmph, create() may be clearer than init_db(), but then I am not sure what init() would do, differently from create(), so this rename takes me back to the puzzled square one state X-<.

I am guessing that create is about creating on-disk structure, while init is about in-core structure out of an existing on-disk structure? Once I understand the differences in these two things, it is much less troublesome to tell them apart, regardless of what they are called. Between .init and .init_db, it would be obvious that the latter is about on-disk thing, without a rename done by this step. On the other hand, contrast between <create, init> is just as opaque as <init_db, init>---the names do not tell readers that these two are about on-disk and in-core structures.

Just my confused impression.

#6 lore raw

Re: [PATCH 02/16] refs: rename `init_db` callback to avoid confusion

Quoted 24 lines · Junio C Hamano
Quoted 7 lines · Patrick Steinhardt

Reference backends have two callbacks init and init_db. The similarity of these two callbacks has repeatedly tripped myself whenever I was looking at those, where I always had to look up which of them does what.

Rename the init_db callback to create, which should hopefully be clearer.

Hmph, create() may be clearer than init_db(), but then I am not sure what init() would do, differently from create(), so this rename takes me back to the puzzled square one state X-<.

I am guessing that create is about creating on-disk structure, while init is about in-core structure out of an existing on-disk structure? Once I understand the differences in these two things, it is much less troublesome to tell them apart, regardless of what they are called. Between .init and .init_db, it would be obvious that the latter is about on-disk thing, without a rename done by this step. On the other hand, contrast between <create, init> is just as opaque as <init_db, init>---the names do not tell readers that these two are about on-disk and in-core structures.

Quoted 1 line

Just my confused impression.

I certainly wouldn't claim that create() vs init() is perfect, either. We could easily avoid that confusion if we were happy to make it more verbose, e.g. by calling it create_on_disk(). I don't really think that we also need to do the same for init(), mostly because it is a common idiom in our codebase to have init() initialize data structures. But that may also be my own personal bias.

I'll go with create_on_disk() for now. It's more verbose, but given that this function isn't called all that much I think it's fine overall.

Patrick

#7 lore raw

PATCH 3/16 refs: implement releasing ref storages

In reply to Patrick Steinhardt

Ref storages are typically only initialized once for the_repository and then never released. Until now we got away with that without causing memory leaks because the_repository stays reachable, and because the ref backend is reachable via the_repository its memory basically never leaks.

This is about to change though because of the upcoming migration logic, which will create a secondary ref storage. In that case, we will either have to release the old or new ref storage to avoid leaks.

Implement a new release callback and expose it via a new ref_storage_release() function.

---
 refs.c                  |  6 ++++++
 refs.h                  |  5 +++++
 refs/debug.c            |  7 +++++++
 refs/files-backend.c    |  9 +++++++++
 refs/packed-backend.c   | 10 ++++++++++
 refs/refs-internal.h    |  6 ++++++
 refs/reftable-backend.c | 22 ++++++++++++++++++++++
 7 files changed, 65 insertions(+)
refs.c +6 -0
index ad7987efab..edb0a633d1 100644
@@ -2041,6 +2041,12 @@ static struct ref_store *ref_store_init(struct repository *repo,
 	return refs;
 }
 
+void ref_store_release(struct ref_store *ref_store)
+{
+	ref_store->be->release(ref_store);
+	free(ref_store->gitdir);
+}
+
 struct ref_store *get_main_ref_store(struct repository *r)
 {
 	if (r->refs_private)
refs.h +5 -0
index e9804e4c22..5ecdfb16dc 100644
@@ -118,6 +118,11 @@ int is_branch(const char *refname);
 
 int ref_store_create(struct ref_store *refs, int flags, struct strbuf *err);
 
+/*
+ * Release all memory and resources associated with the ref store.
+ */
+void ref_store_release(struct ref_store *ref_store);
+
 /*
  * Return the peeled value of the oid currently being iterated via
  * for_each_ref(), etc. This is equivalent to calling:
refs/debug.c +7 -0
index 58fb4557ed..27aae42134 100644
@@ -33,6 +33,12 @@ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor
 	return (struct ref_store *)res;
 }
 
+static void debug_release(struct ref_store *refs)
+{
+	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
+	drefs->refs->be->release(drefs->refs);
+}
+
 static int debug_create(struct ref_store *refs, int flags, struct strbuf *err)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
@@ -427,6 +433,7 @@ static int debug_reflog_expire(struct ref_store *ref_store, const char *refname,
 struct ref_storage_be refs_be_debug = {
 	.name = "debug",
 	.init = NULL,
+	.release = debug_release,
 	.create = debug_create,
 
 	/*
refs/files-backend.c +9 -0
index f766d18d5a..368df075c1 100644
@@ -149,6 +149,14 @@ static struct files_ref_store *files_downcast(struct ref_store *ref_store,
 	return refs;
 }
 
+static void files_ref_store_release(struct ref_store *ref_store)
+{
+	struct files_ref_store *refs = files_downcast(ref_store, 0, "release");
+	free_ref_cache(refs->loose);
+	free(refs->gitcommondir);
+	ref_store_release(refs->packed_ref_store);
+}
+
 static void files_reflog_path(struct files_ref_store *refs,
 			      struct strbuf *sb,
 			      const char *refname)
@@ -3284,6 +3292,7 @@ static int files_ref_store_create(struct ref_store *ref_store,
 struct ref_storage_be refs_be_files = {
 	.name = "files",
 	.init = files_ref_store_init,
+	.release = files_ref_store_release,
 	.create = files_ref_store_create,
 	.transaction_prepare = files_transaction_prepare,
 	.transaction_finish = files_transaction_finish,
refs/packed-backend.c +10 -0
index 716513efed..bebceb4aa7 100644
@@ -252,6 +252,15 @@ static void clear_snapshot(struct packed_ref_store *refs)
 	}
 }
 
+static void packed_ref_store_release(struct ref_store *ref_store)
+{
+	struct packed_ref_store *refs = packed_downcast(ref_store, 0, "release");
+	clear_snapshot(refs);
+	rollback_lock_file(&refs->lock);
+	delete_tempfile(&refs->tempfile);
+	free(refs->path);
+}
+
 static NORETURN void die_unterminated_line(const char *path,
 					   const char *p, size_t len)
 {
@@ -1707,6 +1716,7 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
 struct ref_storage_be refs_be_packed = {
 	.name = "packed",
 	.init = packed_ref_store_init,
+	.release = packed_ref_store_release,
 	.create = packed_ref_store_create,
 	.transaction_prepare = packed_transaction_prepare,
 	.transaction_finish = packed_transaction_finish,
refs/refs-internal.h +6 -0
index eb42212764..cc1fe6e633 100644
@@ -530,6 +530,11 @@ typedef struct ref_store *ref_store_init_fn(struct repository *repo,
 					    const char *gitdir,
 					    unsigned int flags);
 
+/*
+ * Release all memory and resources associated with the ref store.
+ */
+typedef void ref_store_release_fn(struct ref_store *refs);
+
 typedef int ref_store_create_fn(struct ref_store *refs,
 				int flags,
 				struct strbuf *err);
@@ -668,6 +673,7 @@ typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refnam
 struct ref_storage_be {
 	const char *name;
 	ref_store_init_fn *init;
+	ref_store_release_fn *release;
 	ref_store_create_fn *create;
 
 	ref_transaction_prepare_fn *transaction_prepare;
refs/reftable-backend.c +22 -0
index a4bb71cd76..6c262c2193 100644
@@ -293,6 +293,27 @@ static struct ref_store *reftable_be_init(struct repository *repo,
 	return &refs->base;
 }
 
+static void reftable_be_release(struct ref_store *ref_store)
+{
+	struct reftable_ref_store *refs = reftable_be_downcast(ref_store, 0, "release");
+	struct strmap_entry *entry;
+	struct hashmap_iter iter;
+
+	if (refs->main_stack) {
+		reftable_stack_destroy(refs->main_stack);
+		refs->main_stack = NULL;
+	}
+
+	if (refs->worktree_stack) {
+		reftable_stack_destroy(refs->worktree_stack);
+		refs->main_stack = NULL;
+	}
+
+	strmap_for_each_entry(&refs->worktree_stacks, &iter, entry)
+		reftable_stack_destroy(entry->value);
+	strmap_clear(&refs->worktree_stacks, 0);
+}
+
 static int reftable_be_create(struct ref_store *ref_store,
 			      int flags UNUSED,
 			      struct strbuf *err UNUSED)
@@ -2248,6 +2269,7 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
 struct ref_storage_be refs_be_reftable = {
 	.name = "reftable",
 	.init = reftable_be_init,
+	.release = reftable_be_release,
 	.create = reftable_be_create,
 	.transaction_prepare = reftable_be_transaction_prepare,
 	.transaction_finish = reftable_be_transaction_finish,
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#8 lore raw

Re: [PATCH 03/16] refs: implement releasing ref storages

Patrick Steinhardt <[email protected]> writes:

[snip]

refs/debug.c struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor · line 38

Quoted 11 lines
refs/debug.c +3 -0
index 58fb4557ed..27aae42134 100644
@@ -33,6 +33,12 @@ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor
 	return (struct ref_store *)res;
 }
 
+static void debug_release(struct ref_store *refs)
+{
+	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;

We should probably add a trace here, using trace_printf_key()

Quoted 47 lines
refs/debug.c +4 -0
+	drefs->refs->be->release(drefs->refs);
+}
+
 static int debug_create(struct ref_store *refs, int flags, struct strbuf *err)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
@@ -427,6 +433,7 @@ static int debug_reflog_expire(struct ref_store *ref_store, const char *refname,
 struct ref_storage_be refs_be_debug = {
 	.name = "debug",
 	.init = NULL,
+	.release = debug_release,
 	.create = debug_create,
 
 	/*
refs/files-backend.c +9 -0
index f766d18d5a..368df075c1 100644
@@ -149,6 +149,14 @@ static struct files_ref_store *files_downcast(struct ref_store *ref_store,
 	return refs;
 }
 
+static void files_ref_store_release(struct ref_store *ref_store)
+{
+	struct files_ref_store *refs = files_downcast(ref_store, 0, "release");
+	free_ref_cache(refs->loose);
+	free(refs->gitcommondir);
+	ref_store_release(refs->packed_ref_store);
+}
+
 static void files_reflog_path(struct files_ref_store *refs,
 			      struct strbuf *sb,
 			      const char *refname)
@@ -3284,6 +3292,7 @@ static int files_ref_store_create(struct ref_store *ref_store,
 struct ref_storage_be refs_be_files = {
 	.name = "files",
 	.init = files_ref_store_init,
+	.release = files_ref_store_release,
 	.create = files_ref_store_create,
 	.transaction_prepare = files_transaction_prepare,
 	.transaction_finish = files_transaction_finish,
refs/packed-backend.c +0 -0
index 716513efed..bebceb4aa7 100644
@@ -252,6 +252,15 @@ static void clear_snapshot(struct packed_ref_store *refs)
 	}

refs/packed-backend.c static void clear_snapshot(struct packed_ref_store *refs) · line 262

Quoted 10 lines
refs/packed-backend.c +8 -0
 }
 
+static void packed_ref_store_release(struct ref_store *ref_store)
+{
+	struct packed_ref_store *refs = packed_downcast(ref_store, 0, "release");
+	clear_snapshot(refs);
+	rollback_lock_file(&refs->lock);
+	delete_tempfile(&refs->tempfile);
+	free(refs->path);
+}

Verified that all the params inside packed_ref_store are cleaned up here, nice!

Quoted 47 lines
refs/packed-backend.c +1 -0
 static NORETURN void die_unterminated_line(const char *path,
 					   const char *p, size_t len)
 {
@@ -1707,6 +1716,7 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
 struct ref_storage_be refs_be_packed = {
 	.name = "packed",
 	.init = packed_ref_store_init,
+	.release = packed_ref_store_release,
 	.create = packed_ref_store_create,
 	.transaction_prepare = packed_transaction_prepare,
 	.transaction_finish = packed_transaction_finish,
refs/refs-internal.h +6 -0
index eb42212764..cc1fe6e633 100644
@@ -530,6 +530,11 @@ typedef struct ref_store *ref_store_init_fn(struct repository *repo,
 					    const char *gitdir,
 					    unsigned int flags);
 
+/*
+ * Release all memory and resources associated with the ref store.
+ */
+typedef void ref_store_release_fn(struct ref_store *refs);
+
 typedef int ref_store_create_fn(struct ref_store *refs,
 				int flags,
 				struct strbuf *err);
@@ -668,6 +673,7 @@ typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refnam
 struct ref_storage_be {
 	const char *name;
 	ref_store_init_fn *init;
+	ref_store_release_fn *release;
 	ref_store_create_fn *create;
 
 	ref_transaction_prepare_fn *transaction_prepare;
refs/reftable-backend.c +4 -0
index a4bb71cd76..6c262c2193 100644
@@ -293,6 +293,27 @@ static struct ref_store *reftable_be_init(struct repository *repo,
 	return &refs->base;
 }
 
+static void reftable_be_release(struct ref_store *ref_store)
+{
+	struct reftable_ref_store *refs = reftable_be_downcast(ref_store, 0, "release");
+	struct strmap_entry *entry;

refs/reftable-backend.c static struct ref_store *reftable_be_init(struct repository *repo, · line 309

Quoted 10 lines
refs/reftable-backend.c +10 -0
+	struct hashmap_iter iter;
+
+	if (refs->main_stack) {
+		reftable_stack_destroy(refs->main_stack);
+		refs->main_stack = NULL;
+	}
+
+	if (refs->worktree_stack) {
+		reftable_stack_destroy(refs->worktree_stack);
+		refs->main_stack = NULL;

This should be refs->worktree_stack, right?

Quoted 20 lines
refs/reftable-backend.c +8 -1
+	}
+
+	strmap_for_each_entry(&refs->worktree_stacks, &iter, entry)
+		reftable_stack_destroy(entry->value);
+	strmap_clear(&refs->worktree_stacks, 0);
+}
+
 static int reftable_be_create(struct ref_store *ref_store,
 			      int flags UNUSED,
 			      struct strbuf *err UNUSED)
@@ -2248,6 +2269,7 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
 struct ref_storage_be refs_be_reftable = {
 	.name = "reftable",
 	.init = reftable_be_init,
+	.release = reftable_be_release,
 	.create = reftable_be_create,
 	.transaction_prepare = reftable_be_transaction_prepare,
 	.transaction_finish = reftable_be_transaction_finish,
--

2.45.1.190.g19fe900cfc.dirty

#9 lore raw

Re: [PATCH 03/16] refs: implement releasing ref storages

Quoted 5 lines · Karthik Nayak

refs/debug.c struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor · line 38

Quoted 3 lines
refs/debug.c +3 -0
+static void debug_release(struct ref_store *refs)
+{
+	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;

We should probably add a trace here, using trace_printf_key()

A totally ignorant question. Should we be adding more traces with trace_* API instead of trace2_* API? If the latter aims to cover superset of use cases the former did, I was hoping that we can eventually deprecate the former, hence this question. Of course We could add a compatiblity layer that emulates trace_* API with a thin wrapper around trace2_* API, but if we do not add new callers, it may still be feasible to directly migrate the callers to use trace2_ API without having to invent such compatibility wrappers.

#10 lore raw

Re: [PATCH 03/16] refs: implement releasing ref storages

Quoted 8 lines · Junio C Hamano
Quoted 5 lines · Karthik Nayak

refs/debug.c struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor · line 38

Quoted 3 lines
refs/debug.c +3 -0
+static void debug_release(struct ref_store *refs)
+{
+	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;

We should probably add a trace here, using trace_printf_key()

Quoted 8 lines

A totally ignorant question. Should we be adding more traces with trace_* API instead of trace2_* API? If the latter aims to cover superset of use cases the former did, I was hoping that we can eventually deprecate the former, hence this question. Of course We could add a compatiblity layer that emulates trace_* API with a thin wrapper around trace2_* API, but if we do not add new callers, it may still be feasible to directly migrate the callers to use trace2_ API without having to invent such compatibility wrappers.

I cannot really answer this question as I ain't got much of a clue around the tracing APIs. But in this case I think we should indeed add this via trace_printf_key() so that we remain consistent with all the other wrappers in the debug store. I'd argue that either all functions here should use trace or trace2, but not a mixture.

Patrick

#11 lore raw

Re: [PATCH 03/16] refs: implement releasing ref storages

In reply to Karthik Nayak
Quoted 12 lines · Karthik Nayak

Patrick Steinhardt <[email protected]> writes:

[snip]

Quoted 8 lines
refs/debug.c +0 -0
index 58fb4557ed..27aae42134 100644
@@ -33,6 +33,12 @@ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor
 	return (struct ref_store *)res;
 }
 
Quoted 5 lines

refs/debug.c struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor · line 38

Quoted 3 lines
refs/debug.c +3 -0
+static void debug_release(struct ref_store *refs)
+{
+	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;

We should probably add a trace here, using trace_printf_key()

I didn't because we don't have debug_init() with a trace, either, and because there is no error code that we could report. But on the other hand it does not hurt to have it here, so let's just add it.

[snip]

Quoted 8 lines
Quoted 8 lines
refs/reftable-backend.c +0 -0
index a4bb71cd76..6c262c2193 100644
@@ -293,6 +293,27 @@ static struct ref_store *reftable_be_init(struct repository *repo,
 	return &refs->base;
 }
 
Quoted 16 lines
Quoted 4 lines
refs/reftable-backend.c +4 -0
+static void reftable_be_release(struct ref_store *ref_store)
+{
+	struct reftable_ref_store *refs = reftable_be_downcast(ref_store, 0, "release");
+	struct strmap_entry *entry;

refs/reftable-backend.c static struct ref_store *reftable_be_init(struct repository *repo, · line 309

Quoted 10 lines
refs/reftable-backend.c +10 -0
+	struct hashmap_iter iter;
+
+	if (refs->main_stack) {
+		reftable_stack_destroy(refs->main_stack);
+		refs->main_stack = NULL;
+	}
+
+	if (refs->worktree_stack) {
+		reftable_stack_destroy(refs->worktree_stack);
+		refs->main_stack = NULL;

This should be refs->worktree_stack, right?

Good catch, yes.

Patrick

#12 lore raw

PATCH 4/16 refs: track ref stores via strmap

In reply to Patrick Steinhardt

The refs code has two global maps that track the submodule and worktree ref stores. Even though both of these maps track values by strings, we still use a struct hashmap instead of a struct strmap. This has the benefit of saving us an allocation because we can combine key and value in a single struct. But it does introduce significant complexity that is completely unneeded.

Refactor the code to use struct strmaps instead to reduce complexity. It's unlikely that this will have any real-world impact on performance given that most repositories likely won't have all that many ref stores. Furthermore, this refactoring allows us to de-globalize those maps and move them into struct repository in a subsequent commit more easily.

---
 refs.c | 71 ++++++++++++----------------------------------------------
 1 file changed, 14 insertions(+), 57 deletions(-)
refs.c +14 -57
index edb0a633d1..542acb25ff 100644
@@ -6,7 +6,7 @@
 #include "advice.h"
 #include "config.h"
 #include "environment.h"
-#include "hashmap.h"
+#include "strmap.h"
 #include "gettext.h"
 #include "hex.h"
 #include "lockfile.h"
@@ -1960,66 +1960,27 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
 	return 0;
 }
 
-struct ref_store_hash_entry
-{
-	struct hashmap_entry ent;
-
-	struct ref_store *refs;
-
-	/* NUL-terminated identifier of the ref store: */
-	char name[FLEX_ARRAY];
-};
+/* A strmap of ref_stores, stored by submodule name: */
+static struct strmap submodule_ref_stores;
 
-static int ref_store_hash_cmp(const void *cmp_data UNUSED,
-			      const struct hashmap_entry *eptr,
-			      const struct hashmap_entry *entry_or_key,
-			      const void *keydata)
-{
-	const struct ref_store_hash_entry *e1, *e2;
-	const char *name;
-
-	e1 = container_of(eptr, const struct ref_store_hash_entry, ent);
-	e2 = container_of(entry_or_key, const struct ref_store_hash_entry, ent);
-	name = keydata ? keydata : e2->name;
-
-	return strcmp(e1->name, name);
-}
-
-static struct ref_store_hash_entry *alloc_ref_store_hash_entry(
-		const char *name, struct ref_store *refs)
-{
-	struct ref_store_hash_entry *entry;
-
-	FLEX_ALLOC_STR(entry, name, name);
-	hashmap_entry_init(&entry->ent, strhash(name));
-	entry->refs = refs;
-	return entry;
-}
-
-/* A hashmap of ref_stores, stored by submodule name: */
-static struct hashmap submodule_ref_stores;
-
-/* A hashmap of ref_stores, stored by worktree id: */
-static struct hashmap worktree_ref_stores;
+/* A strmap of ref_stores, stored by worktree id: */
+static struct strmap worktree_ref_stores;
 
 /*
  * Look up a ref store by name. If that ref_store hasn't been
  * registered yet, return NULL.
  */
-static struct ref_store *lookup_ref_store_map(struct hashmap *map,
+static struct ref_store *lookup_ref_store_map(struct strmap *map,
 					      const char *name)
 {
-	struct ref_store_hash_entry *entry;
-	unsigned int hash;
+	struct strmap_entry *entry;
 
-	if (!map->tablesize)
+	if (!map->map.tablesize)
 		/* It's initialized on demand in register_ref_store(). */
 		return NULL;
 
-	hash = strhash(name);
-	entry = hashmap_get_entry_from_hash(map, hash, name,
-					struct ref_store_hash_entry, ent);
-	return entry ? entry->refs : NULL;
+	entry = strmap_get_entry(map, name);
+	return entry ? entry->value : NULL;
 }
 
 /*
@@ -2064,18 +2025,14 @@ struct ref_store *get_main_ref_store(struct repository *r)
  * Associate a ref store with a name. It is a fatal error to call this
  * function twice for the same name.
  */
-static void register_ref_store_map(struct hashmap *map,
+static void register_ref_store_map(struct strmap *map,
 				   const char *type,
 				   struct ref_store *refs,
 				   const char *name)
 {
-	struct ref_store_hash_entry *entry;
-
-	if (!map->tablesize)
-		hashmap_init(map, ref_store_hash_cmp, NULL, 0);
-
-	entry = alloc_ref_store_hash_entry(name, refs);
-	if (hashmap_put(map, &entry->ent))
+	if (!map->map.tablesize)
+		strmap_init(map);
+	if (strmap_put(map, name, refs))
 		BUG("%s ref_store '%s' initialized twice", type, name);
 }
 
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#13 lore raw

Re: [PATCH 04/16] refs: track ref stores via strmap

Quoted 6 lines · Patrick Steinhardt

Refactor the code to use struct strmaps instead to reduce complexity. It's unlikely that this will have any real-world impact on performance given that most repositories likely won't have all that many ref stores.
...

 refs.c | 71 ++++++++++++----------------------------------------------
 1 file changed, 14 insertions(+), 57 deletions(-)

Nice.

#14 lore raw

PATCH 5/16 refs: pass repo when retrieving submodule ref store

In reply to Patrick Steinhardt

Looking up submodule ref stores has two deficiencies:

- The initialized subrepo will be attributed to the_repository.

- The submodule ref store will be tracked in a global map.

This makes it impossible to have submodule ref stores for a repository other than the_repository.

Modify the function to accept the parent repository as parameter and move the global map into struct repository. Like this it becomes possible to look up submodule ref stores for arbitrary repositories.

---
 builtin/submodule--helper.c |  6 ++++--
 refs.c                      | 22 +++++++---------------
 refs.h                      |  3 ++-
 refs/refs-internal.h        |  2 +-
 repository.c                |  8 ++++++++
 repository.h                |  8 ++++++++
 submodule.c                 |  3 ++-
 t/helper/test-ref-store.c   |  2 +-
 8 files changed, 33 insertions(+), 21 deletions(-)
builtin/submodule--helper.c +4 -2
index e604cb5ddb..5076a33500 100644
@@ -679,7 +679,8 @@ static void status_submodule(const char *path, const struct object_id *ce_oid,
 			     displaypath);
 	} else if (!(flags & OPT_CACHED)) {
 		struct object_id oid;
-		struct ref_store *refs = get_submodule_ref_store(path);
+		struct ref_store *refs = repo_get_submodule_ref_store(the_repository,
+								      path);
 
 		if (!refs) {
 			print_status(flags, '-', path, ce_oid, displaypath);
@@ -903,7 +904,8 @@ static void generate_submodule_summary(struct summary_cb *info,
 
 	if (!info->cached && oideq(&p->oid_dst, null_oid())) {
 		if (S_ISGITLINK(p->mod_dst)) {
-			struct ref_store *refs = get_submodule_ref_store(p->sm_path);
+			struct ref_store *refs = repo_get_submodule_ref_store(the_repository,
+									      p->sm_path);
 
 			if (refs)
 				refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
refs.c +7 -15
index 542acb25ff..86008ce7b4 100644
@@ -1949,8 +1949,7 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
 	struct ref_store *refs;
 	int flags;
 
-	refs = get_submodule_ref_store(submodule);
-
+	refs = repo_get_submodule_ref_store(the_repository, submodule);
 	if (!refs)
 		return -1;
 
@@ -1960,9 +1959,6 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
 	return 0;
 }
 
-/* A strmap of ref_stores, stored by submodule name: */
-static struct strmap submodule_ref_stores;
-
 /* A strmap of ref_stores, stored by worktree id: */
 static struct strmap worktree_ref_stores;
 
@@ -2036,7 +2032,8 @@ static void register_ref_store_map(struct strmap *map,
 		BUG("%s ref_store '%s' initialized twice", type, name);
 }
 
-struct ref_store *get_submodule_ref_store(const char *submodule)
+struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
+					       const char *submodule)
 {
 	struct strbuf submodule_sb = STRBUF_INIT;
 	struct ref_store *refs;
@@ -2057,7 +2054,7 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
 		/* We need to strip off one or more trailing slashes */
 		submodule = to_free = xmemdupz(submodule, len);
 
-	refs = lookup_ref_store_map(&submodule_ref_stores, submodule);
+	refs = lookup_ref_store_map(&repo->submodule_ref_stores, submodule);
 	if (refs)
 		goto done;
 
@@ -2069,20 +2066,15 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
 		goto done;
 
 	subrepo = xmalloc(sizeof(*subrepo));
-	/*
-	 * NEEDSWORK: Make get_submodule_ref_store() work with arbitrary
-	 * superprojects other than the_repository. This probably should be
-	 * done by making it take a struct repository * parameter instead of a
-	 * submodule path.
-	 */
-	if (repo_submodule_init(subrepo, the_repository, submodule,
+
+	if (repo_submodule_init(subrepo, repo, submodule,
 				null_oid())) {
 		free(subrepo);
 		goto done;
 	}
 	refs = ref_store_init(subrepo, submodule_sb.buf,
 			      REF_STORE_READ | REF_STORE_ODB);
-	register_ref_store_map(&submodule_ref_stores, "submodule",
+	register_ref_store_map(&repo->submodule_ref_stores, "submodule",
 			       refs, submodule);
 
 done:
refs.h +2 -1
index 5ecdfb16dc..fc1a873e8a 100644
@@ -954,7 +954,8 @@ struct ref_store *get_main_ref_store(struct repository *r);
  * For backwards compatibility, submodule=="" is treated the same as
  * submodule==NULL.
  */
-struct ref_store *get_submodule_ref_store(const char *submodule);
+struct ref_store *repo_get_submodule_ref_store(struct repository *repo,
+					       const char *submodule);
 struct ref_store *get_worktree_ref_store(const struct worktree *wt);
 
 /*
refs/refs-internal.h +1 -1
index cc1fe6e633..3c3f9a3555 100644
@@ -706,7 +706,7 @@ extern struct ref_storage_be refs_be_packed;
 /*
  * A representation of the reference store for the main repository or
  * a submodule. The ref_store instances for submodules are kept in a
- * hash map; see get_submodule_ref_store() for more info.
+ * hash map; see repo_get_submodule_ref_store() for more info.
  */
 struct ref_store {
 	/* The backend describing this ref_store's storage scheme: */
repository.c +8 -0
index c50849fd83..bb9b9e2b52 100644
@@ -14,6 +14,7 @@
 #include "sparse-index.h"
 #include "trace2.h"
 #include "promisor-remote.h"
+#include "refs.h"
 
 /* The main repository */
 static struct repository the_repo;
@@ -289,6 +290,9 @@ static void repo_clear_path_cache(struct repo_path_cache *cache)
 
 void repo_clear(struct repository *repo)
 {
+	struct hashmap_iter iter;
+	struct strmap_entry *e;
+
 	FREE_AND_NULL(repo->gitdir);
 	FREE_AND_NULL(repo->commondir);
 	FREE_AND_NULL(repo->graft_file);
@@ -329,6 +333,10 @@ void repo_clear(struct repository *repo)
 		FREE_AND_NULL(repo->remote_state);
 	}
 
+	strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
+		ref_store_release(e->value);
+	strmap_clear(&repo->submodule_ref_stores, 1);
+
 	repo_clear_path_cache(&repo->cached_paths);
 }
 
repository.h +8 -0
index 41ed22543a..0389df0461 100644
@@ -1,6 +1,8 @@
 #ifndef REPOSITORY_H
 #define REPOSITORY_H
 
+#include "strmap.h"
+
 struct config_set;
 struct fsmonitor_settings;
 struct git_hash_algo;
@@ -108,6 +110,12 @@ struct repository {
 	 */
 	struct ref_store *refs_private;
 
+	/*
+	 * A strmap of ref_stores, stored by submodule name, accessible via
+	 * `repo_get_submodule_ref_store()`.
+	 */
+	struct strmap submodule_ref_stores;
+
 	/*
 	 * Contains path to often used file names.
 	 */
submodule.c +2 -1
index f6313cd99f..759cf1e1cd 100644
@@ -99,7 +99,8 @@ int is_staging_gitmodules_ok(struct index_state *istate)
 static int for_each_remote_ref_submodule(const char *submodule,
 					 each_ref_fn fn, void *cb_data)
 {
-	return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
+	return refs_for_each_remote_ref(repo_get_submodule_ref_store(the_repository,
+								     submodule),
 					fn, cb_data);
 }
 
t/helper/test-ref-store.c +1 -1
index 82bbf6e2e6..a5a7609811 100644
@@ -82,7 +82,7 @@ static const char **get_store(const char **argv, struct ref_store **refs)
 		add_to_alternates_memory(sb.buf);
 		strbuf_release(&sb);
 
-		*refs = get_submodule_ref_store(gitdir);
+		*refs = repo_get_submodule_ref_store(the_repository, gitdir);
 	} else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
 		struct worktree **p, **worktrees = get_worktrees();
 
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#15 lore raw

Re: [PATCH 05/16] refs: pass repo when retrieving submodule ref store

Quoted 3 lines · Patrick Steinhardt

Modify the function to accept the parent repository as parameter and move the global map into struct repository. Like this it becomes possible to look up submodule ref stores for arbitrary repositories.

Hmph.

refs.c int resolve_gitlink_ref(const char *submodule, const char *refname, · line 1954

Quoted 13 lines
refs.c +1 -2
index 542acb25ff..86008ce7b4 100644
@@ -1949,8 +1949,7 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
 	struct ref_store *refs;
 	int flags;
 
-	refs = get_submodule_ref_store(submodule);
-
+	refs = repo_get_submodule_ref_store(the_repository, submodule);
 	if (!refs)
 		return -1;

This still wants to work on the_repository, which means at this 5th step of 16-patch series, we cannot do a submodule of a submodule that hangs below the top-level superproject yet?

Quoted 14 lines
refs.c +2 -7
@@ -2069,20 +2066,15 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
 		goto done;
 
 	subrepo = xmalloc(sizeof(*subrepo));
-	/*
-	 * NEEDSWORK: Make get_submodule_ref_store() work with arbitrary
-	 * superprojects other than the_repository. This probably should be
-	 * done by making it take a struct repository * parameter instead of a
-	 * submodule path.
-	 */
-	if (repo_submodule_init(subrepo, the_repository, submodule,
+
+	if (repo_submodule_init(subrepo, repo, submodule,
 				null_oid())) {

refs.c struct ref_store *get_submodule_ref_store(const char *submodule) · line 2080

Quoted 10 lines
refs.c +1 -1
 		free(subrepo);
 		goto done;
 	}
 	refs = ref_store_init(subrepo, submodule_sb.buf,
 			      REF_STORE_READ | REF_STORE_ODB);
-	register_ref_store_map(&submodule_ref_stores, "submodule",
+	register_ref_store_map(&repo->submodule_ref_stores, "submodule",
 			       refs, submodule);
 
 done:

Nice.

#16 lore raw

Re: [PATCH 05/16] refs: pass repo when retrieving submodule ref store

Quoted 16 lines · Junio C Hamano
Quoted 3 lines · Patrick Steinhardt

Modify the function to accept the parent repository as parameter and move the global map into struct repository. Like this it becomes possible to look up submodule ref stores for arbitrary repositories.

Hmph.

Quoted 8 lines
refs.c +0 -0
index 542acb25ff..86008ce7b4 100644
@@ -1949,8 +1949,7 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
 	struct ref_store *refs;
 	int flags;
 
Quoted 9 lines

refs.c int resolve_gitlink_ref(const char *submodule, const char *refname, · line 1954

Quoted 5 lines
refs.c +1 -2
-	refs = get_submodule_ref_store(submodule);
-
+	refs = repo_get_submodule_ref_store(the_repository, submodule);
 	if (!refs)
 		return -1;

This still wants to work on the_repository, which means at this 5th step of 16-patch series, we cannot do a submodule of a submodule that hangs below the top-level superproject yet?

Yeah, this gets fixed in the next patch.

Patrick

#17 lore raw

PATCH 6/16 refs: refactor `resolve_gitlink_ref()` to accept a repository

In reply to Patrick Steinhardt

In resolve_gitlink_ref() we implicitly rely on the_repository to look up the submodule ref store. Now that we can look up submodule ref stores for arbitrary repositories we can improve this function to instead accept a repository as parameter for which we want to resolve the gitlink.

Do so and adjust callers accordingly.

---
 attr.c                      | 3 ++-
 builtin/submodule--helper.c | 8 +++++---
 builtin/update-index.c      | 5 +++--
 combine-diff.c              | 3 ++-
 diff-lib.c                  | 3 ++-
 dir.c                       | 3 ++-
 object-file.c               | 2 +-
 read-cache.c                | 5 +++--
 refs.c                      | 7 ++++---
 refs.h                      | 5 +++--
 unpack-trees.c              | 3 ++-
 11 files changed, 29 insertions(+), 18 deletions(-)
attr.c +2 -1
index 33473bdce0..6c6eb05333 100644
@@ -1288,7 +1288,8 @@ static const char *builtin_object_mode_attr(struct index_state *istate, const ch
 			if (pos >= 0) {
 				 if (S_ISGITLINK(istate->cache[pos]->ce_mode))
 					 mode = istate->cache[pos]->ce_mode;
-			} else if (resolve_gitlink_ref(path, "HEAD", &oid) == 0) {
+			} else if (repo_resolve_gitlink_ref(the_repository, path,
+							    "HEAD", &oid) == 0) {
 				mode = S_IFGITLINK;
 			}
 		}
builtin/submodule--helper.c +5 -3
index 5076a33500..897f19868e 100644
@@ -2600,7 +2600,8 @@ static int update_submodule(struct update_data *update_data)
 
 	if (update_data->just_cloned)
 		oidcpy(&update_data->suboid, null_oid());
-	else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
+	else if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
+					  "HEAD", &update_data->suboid))
 		return die_message(_("Unable to find current revision in submodule path '%s'"),
 				   update_data->displaypath);
 
@@ -2627,7 +2628,8 @@ static int update_submodule(struct update_data *update_data)
 						   update_data->sm_path);
 		}
 
-		if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
+		if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
+					     remote_ref, &update_data->oid))
 			return die_message(_("Unable to find %s revision in submodule path '%s'"),
 					   remote_ref, update_data->sm_path);
 
@@ -3357,7 +3359,7 @@ static void die_on_repo_without_commits(const char *path)
 	strbuf_addstr(&sb, path);
 	if (is_nonbare_repository_dir(&sb)) {
 		struct object_id oid;
-		if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
+		if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
 			die(_("'%s' does not have a commit checked out"), path);
 	}
 	strbuf_release(&sb);
builtin/update-index.c +3 -2
index 20aa1c4c68..d343416ae2 100644
@@ -349,7 +349,8 @@ static int process_directory(const char *path, int len, struct stat *st)
 		if (S_ISGITLINK(ce->ce_mode)) {
 
 			/* Do nothing to the index if there is no HEAD! */
-			if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
+			if (repo_resolve_gitlink_ref(the_repository, path,
+						     "HEAD", &oid) < 0)
 				return 0;
 
 			return add_one_path(ce, path, len, st);
@@ -375,7 +376,7 @@ static int process_directory(const char *path, int len, struct stat *st)
 	}
 
 	/* No match - should we add it as a gitlink? */
-	if (!resolve_gitlink_ref(path, "HEAD", &oid))
+	if (!repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid))
 		return add_one_path(NULL, path, len, st);
 
 	/* Error out. */
combine-diff.c +2 -1
index d6d6fa1689..4960d904ac 100644
@@ -1066,7 +1066,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 			elem->mode = canon_mode(st.st_mode);
 		} else if (S_ISDIR(st.st_mode)) {
 			struct object_id oid;
-			if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
+			if (repo_resolve_gitlink_ref(the_repository, elem->path,
+						     "HEAD", &oid) < 0)
 				result = grab_blob(opt->repo, &elem->oid,
 						   elem->mode, &result_size,
 						   NULL, NULL);
diff-lib.c +2 -1
index 12b1541478..5a5a50c5a1 100644
@@ -66,7 +66,8 @@ static int check_removed(const struct cache_entry *ce, struct stat *st)
 		 * a directory --- the blob was removed!
 		 */
 		if (!S_ISGITLINK(ce->ce_mode) &&
-		    resolve_gitlink_ref(ce->name, "HEAD", &sub))
+		    repo_resolve_gitlink_ref(the_repository, ce->name,
+					     "HEAD", &sub))
 			return 1;
 	}
 	return 0;
dir.c +2 -1
index 2d83f3311a..f6066cc01d 100644
@@ -3318,7 +3318,8 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
 	struct object_id submodule_head;
 
 	if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
-	    !resolve_gitlink_ref(path->buf, "HEAD", &submodule_head)) {
+	    !repo_resolve_gitlink_ref(the_repository, path->buf,
+				      "HEAD", &submodule_head)) {
 		/* Do not descend and nuke a nested git work tree. */
 		if (kept_up)
 			*kept_up = 1;
object-file.c +1 -1
index 610b1f465c..a40300ce4a 100644
@@ -2669,7 +2669,7 @@ int index_path(struct index_state *istate, struct object_id *oid,
 		strbuf_release(&sb);
 		break;
 	case S_IFDIR:
-		return resolve_gitlink_ref(path, "HEAD", oid);
+		return repo_resolve_gitlink_ref(the_repository, path, "HEAD", oid);
 	default:
 		return error(_("%s: unsupported file type"), path);
 	}
read-cache.c +3 -2
index a6db25a16d..447109aa76 100644
@@ -271,7 +271,8 @@ static int ce_compare_gitlink(const struct cache_entry *ce)
 	 *
 	 * If so, we consider it always to match.
 	 */
-	if (resolve_gitlink_ref(ce->name, "HEAD", &oid) < 0)
+	if (repo_resolve_gitlink_ref(the_repository, ce->name,
+				     "HEAD", &oid) < 0)
 		return 0;
 	return !oideq(&oid, &ce->oid);
 }
@@ -711,7 +712,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 
 	namelen = strlen(path);
 	if (S_ISDIR(st_mode)) {
-		if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
+		if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
 			return error(_("'%s' does not have a commit checked out"), path);
 		while (namelen && path[namelen-1] == '/')
 			namelen--;
refs.c +4 -3
index 86008ce7b4..40e241216e 100644
@@ -1943,13 +1943,14 @@ int ref_store_create(struct ref_store *refs, int flags, struct strbuf *err)
 	return refs->be->create(refs, flags, err);
 }
 
-int resolve_gitlink_ref(const char *submodule, const char *refname,
-			struct object_id *oid)
+int repo_resolve_gitlink_ref(struct repository *r,
+			     const char *submodule, const char *refname,
+			     struct object_id *oid)
 {
 	struct ref_store *refs;
 	int flags;
 
-	refs = repo_get_submodule_ref_store(the_repository, submodule);
+	refs = repo_get_submodule_ref_store(r, submodule);
 	if (!refs)
 		return -1;
 
refs.h +3 -2
index fc1a873e8a..5a2b493507 100644
@@ -141,8 +141,9 @@ int peel_iterated_oid(const struct object_id *base, struct object_id *peeled);
  * successful, return 0 and set oid to the name of the object;
  * otherwise, return a non-zero value.
  */
-int resolve_gitlink_ref(const char *submodule, const char *refname,
-			struct object_id *oid);
+int repo_resolve_gitlink_ref(struct repository *r,
+			     const char *submodule, const char *refname,
+			     struct object_id *oid);
 
 /*
  * Return true iff abbrev_name is a possible abbreviation for
unpack-trees.c +2 -1
index c2b20b80d5..304ea2ed86 100644
@@ -2318,7 +2318,8 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
 
 	if (S_ISGITLINK(ce->ce_mode)) {
 		struct object_id oid;
-		int sub_head = resolve_gitlink_ref(ce->name, "HEAD", &oid);
+		int sub_head = repo_resolve_gitlink_ref(the_repository, ce->name,
+							"HEAD", &oid);
 		/*
 		 * If we are not going to update the submodule, then
 		 * we don't care.
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#18 lore raw

Re: [PATCH 06/16] refs: refactor `resolve_gitlink_ref()` to accept a repository

Quoted 11 lines · Patrick Steinhardt
refs.c +1 -2
index 86008ce7b4..40e241216e 100644
@@ -1943,13 +1943,14 @@ int ref_store_create(struct ref_store *refs, int flags, struct strbuf *err)
 	return refs->be->create(refs, flags, err);
 }
 
-int resolve_gitlink_ref(const char *submodule, const char *refname,
-			struct object_id *oid)
+int repo_resolve_gitlink_ref(struct repository *r,

refs.c int ref_store_create(struct ref_store *refs, int flags, struct strbuf *err) · line 1955

Quoted 10 lines
refs.c +3 -1
+			     const char *submodule, const char *refname,
+			     struct object_id *oid)
 {
 	struct ref_store *refs;
 	int flags;
 
-	refs = repo_get_submodule_ref_store(the_repository, submodule);
+	refs = repo_get_submodule_ref_store(r, submodule);
 	if (!refs)
 		return -1;

OK. Looking good.

#19 lore raw

PATCH 7/16 refs: retrieve worktree ref stores via associated repository

In reply to Patrick Steinhardt

Similar as with the preceding commit, the worktree ref stores are always looked up via the_repository. Also, again, those ref stores are stored in a global map.

Refactor the code so that worktrees have a pointer to their repository. Like this, we can move the global map into struct repository and stop using the_repository. With this change, we can now in theory look up worktree ref stores for repositories other than the_repository. In practice, the worktree code will need further changes to look up arbitrary worktrees.

---
 refs.c       | 27 ++++++++++++++-------------
 repository.c |  4 ++++
 repository.h |  6 ++++++
 worktree.c   |  2 ++
 worktree.h   |  2 ++
 5 files changed, 28 insertions(+), 13 deletions(-)
refs.c +14 -13
index 40e241216e..03bf5d0e69 100644
@@ -1960,9 +1960,6 @@ int repo_resolve_gitlink_ref(struct repository *r,
 	return 0;
 }
 
-/* A strmap of ref_stores, stored by worktree id: */
-static struct strmap worktree_ref_stores;
-
 /*
  * Look up a ref store by name. If that ref_store hasn't been
  * registered yet, return NULL.
@@ -2091,25 +2088,29 @@ struct ref_store *get_worktree_ref_store(const struct worktree *wt)
 	const char *id;
 
 	if (wt->is_current)
-		return get_main_ref_store(the_repository);
+		return get_main_ref_store(wt->repo);
 
 	id = wt->id ? wt->id : "/";
-	refs = lookup_ref_store_map(&worktree_ref_stores, id);
+	refs = lookup_ref_store_map(&wt->repo->worktree_ref_stores, id);
 	if (refs)
 		return refs;
 
-	if (wt->id)
-		refs = ref_store_init(the_repository,
-				      git_common_path("worktrees/%s", wt->id),
+	if (wt->id) {
+		struct strbuf common_path = STRBUF_INIT;
+		strbuf_git_common_path(&common_path, wt->repo,
+				      "worktrees/%s", wt->id);
+		refs = ref_store_init(wt->repo, common_path.buf,
 				      REF_STORE_ALL_CAPS);
-	else
-		refs = ref_store_init(the_repository,
-				      get_git_common_dir(),
+		strbuf_release(&common_path);
+	} else {
+		refs = ref_store_init(wt->repo, wt->repo->commondir,
 				      REF_STORE_ALL_CAPS);
+	}
 
 	if (refs)
-		register_ref_store_map(&worktree_ref_stores, "worktree",
-				       refs, id);
+		register_ref_store_map(&wt->repo->worktree_ref_stores,
+				       "worktree", refs, id);
+
 	return refs;
 }
 
repository.c +4 -0
index bb9b9e2b52..d29b0304fb 100644
@@ -337,6 +337,10 @@ void repo_clear(struct repository *repo)
 		ref_store_release(e->value);
 	strmap_clear(&repo->submodule_ref_stores, 1);
 
+	strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
+		ref_store_release(e->value);
+	strmap_clear(&repo->worktree_ref_stores, 1);
+
 	repo_clear_path_cache(&repo->cached_paths);
 }
 
repository.h +6 -0
index 0389df0461..4bd8969005 100644
@@ -116,6 +116,12 @@ struct repository {
 	 */
 	struct strmap submodule_ref_stores;
 
+	/*
+	 * A strmap of ref_stores, stored by worktree id, accessible via
+	 * `get_worktree_ref_store()`.
+	 */
+	struct strmap worktree_ref_stores;
+
 	/*
 	 * Contains path to often used file names.
 	 */
worktree.c +2 -0
index cf5eea8c93..12eadacc61 100644
@@ -65,6 +65,7 @@ static struct worktree *get_main_worktree(int skip_reading_head)
 	strbuf_strip_suffix(&worktree_path, "/.git");
 
 	CALLOC_ARRAY(worktree, 1);
+	worktree->repo = the_repository;
 	worktree->path = strbuf_detach(&worktree_path, NULL);
 	/*
 	 * NEEDSWORK: If this function is called from a secondary worktree and
@@ -98,6 +99,7 @@ struct worktree *get_linked_worktree(const char *id,
 	strbuf_strip_suffix(&worktree_path, "/.git");
 
 	CALLOC_ARRAY(worktree, 1);
+	worktree->repo = the_repository;
 	worktree->path = strbuf_detach(&worktree_path, NULL);
 	worktree->id = xstrdup(id);
 	if (!skip_reading_head)
worktree.h +2 -0
index f14784a2ff..7cc6d90e66 100644
@@ -6,6 +6,8 @@
 struct strbuf;
 
 struct worktree {
+	/* The repository this worktree belongs to. */
+	struct repository *repo;
 	char *path;
 	char *id;
 	char *head_ref;		/* NULL if HEAD is broken or detached */
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#20 lore raw

PATCH 8/16 refs: convert iteration over replace refs to accept ref store

In reply to Patrick Steinhardt

The function for_each_replace_ref() is a bit of an oddball across the refs interfaces as it accepts a pointer to the repository instead of a pointer to the ref store. The only reason for us to accept a repository is so that we can eventually pass it back to the callback function that the caller has provided. This is somewhat arbitrary though, as callers that need the repository can instead make it accessible via the callback payload.

Refactor the function to instead accept the ref store and adjust callers accordingly. This allows us to get rid of some of the boilerplate that we had to carry to pass along the repository and brings us in line with the other functions that iterate through refs.

---
 builtin/replace.c    | 13 ++++++----
 refs.c               | 58 ++++++--------------------------------------
 refs.h               | 17 ++-----------
 refs/iterator.c      |  6 ++---
 refs/refs-internal.h |  5 ++--
 replace-object.c     | 10 +++++---
 6 files changed, 28 insertions(+), 81 deletions(-)
builtin/replace.c +8 -5
index bc2a948c80..b09c78b77d 100644
@@ -43,11 +43,12 @@ enum replace_format {
 };
 
 struct show_data {
+	struct repository *repo;
 	const char *pattern;
 	enum replace_format format;
 };
 
-static int show_reference(struct repository *r, const char *refname,
+static int show_reference(const char *refname,
 			  const struct object_id *oid,
 			  int flag UNUSED, void *cb_data)
 {
@@ -62,11 +63,11 @@ static int show_reference(struct repository *r, const char *refname,
 			struct object_id object;
 			enum object_type obj_type, repl_type;
 
-			if (repo_get_oid(r, refname, &object))
+			if (repo_get_oid(data->repo, refname, &object))
 				return error(_("failed to resolve '%s' as a valid ref"), refname);
 
-			obj_type = oid_object_info(r, &object, NULL);
-			repl_type = oid_object_info(r, oid, NULL);
+			obj_type = oid_object_info(data->repo, &object, NULL);
+			repl_type = oid_object_info(data->repo, oid, NULL);
 
 			printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type),
 			       oid_to_hex(oid), type_name(repl_type));
@@ -80,6 +81,7 @@ static int list_replace_refs(const char *pattern, const char *format)
 {
 	struct show_data data;
 
+	data.repo = the_repository;
 	if (!pattern)
 		pattern = "*";
 	data.pattern = pattern;
@@ -99,7 +101,8 @@ static int list_replace_refs(const char *pattern, const char *format)
 			       "valid formats are 'short', 'medium' and 'long'"),
 			     format);
 
-	for_each_replace_ref(the_repository, show_reference, (void *)&data);
+	refs_for_each_replace_ref(get_main_ref_store(the_repository),
+				  show_reference, (void *)&data);
 
 	return 0;
 }
refs.c +7 -51

96 lines not on this page · raw message

refs.h +2 -15
index 5a2b493507..77f9887235 100644
@@ -298,16 +298,6 @@ struct ref_transaction;
 typedef int each_ref_fn(const char *refname,
 			const struct object_id *oid, int flags, void *cb_data);
 
-/*
- * The same as each_ref_fn, but also with a repository argument that
- * contains the repository associated with the callback.
- */
-typedef int each_repo_ref_fn(struct repository *r,
-			     const char *refname,
-			     const struct object_id *oid,
-			     int flags,
-			     void *cb_data);
-
 /*
  * The following functions invoke the specified callback function for
  * each reference indicated.  If the function ever returns a nonzero
@@ -329,6 +319,8 @@ int refs_for_each_branch_ref(struct ref_store *refs,
 			     each_ref_fn fn, void *cb_data);
 int refs_for_each_remote_ref(struct ref_store *refs,
 			     each_ref_fn fn, void *cb_data);
+int refs_for_each_replace_ref(struct ref_store *refs,
+			      each_ref_fn fn, void *cb_data);
 
 /*
  * references matching any pattern in "exclude_patterns" are omitted from the
@@ -353,11 +345,6 @@ int refs_for_each_fullref_in_prefixes(struct ref_store *refs,
 				      const char **exclude_patterns,
 				      each_ref_fn fn, void *cb_data);
 
-/**
- * iterate refs from the respective area.
- */
-int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data);
-
 /* iterates all refs that match the specified glob pattern. */
 int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn,
 			   const char *pattern, void *cb_data);
refs/iterator.c +3 -3
index 9db8b056d5..d355ebf0d5 100644
@@ -440,15 +440,15 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0,
 
 struct ref_iterator *current_ref_iter = NULL;
 
-int do_for_each_repo_ref_iterator(struct repository *r, struct ref_iterator *iter,
-				  each_repo_ref_fn fn, void *cb_data)
+int do_for_each_ref_iterator(struct ref_iterator *iter,
+			     each_ref_fn fn, void *cb_data)
 {
 	int retval = 0, ok;
 	struct ref_iterator *old_ref_iter = current_ref_iter;
 
 	current_ref_iter = iter;
 	while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
-		retval = fn(r, iter->refname, iter->oid, iter->flags, cb_data);
+		retval = fn(iter->refname, iter->oid, iter->flags, cb_data);
 		if (retval) {
 			/*
 			 * If ref_iterator_abort() returns ITER_ERROR,
refs/refs-internal.h +2 -3
index 3c3f9a3555..fc77240c93 100644
@@ -503,9 +503,8 @@ extern struct ref_iterator *current_ref_iter;
  * adapter between the callback style of reference iteration and the
  * iterator style.
  */
-int do_for_each_repo_ref_iterator(struct repository *r,
-				  struct ref_iterator *iter,
-				  each_repo_ref_fn fn, void *cb_data);
+int do_for_each_ref_iterator(struct ref_iterator *iter,
+			     each_ref_fn fn, void *cb_data);
 
 struct ref_store;
 
replace-object.c +6 -4
index 523215589d..73f5acbcd9 100644
@@ -8,12 +8,13 @@
 #include "repository.h"
 #include "commit.h"
 
-static int register_replace_ref(struct repository *r,
-				const char *refname,
+static int register_replace_ref(const char *refname,
 				const struct object_id *oid,
 				int flag UNUSED,
-				void *cb_data UNUSED)
+				void *cb_data)
 {
+	struct repository *r = cb_data;
+
 	/* Get sha1 from refname */
 	const char *slash = strrchr(refname, '/');
 	const char *hash = slash ? slash + 1 : refname;
@@ -50,7 +51,8 @@ void prepare_replace_object(struct repository *r)
 		xmalloc(sizeof(*r->objects->replace_map));
 	oidmap_init(r->objects->replace_map, 0);
 
-	for_each_replace_ref(r, register_replace_ref, NULL);
+	refs_for_each_replace_ref(get_main_ref_store(r),
+				  register_replace_ref, r);
 	r->objects->replace_map_initialized = 1;
 
 	pthread_mutex_unlock(&r->objects->replace_mutex);
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#21 lore raw

PATCH 9/16 refs: pass ref store when detecting dangling symrefs

In reply to Patrick Steinhardt

Both warn_dangling_symref() and warn_dangling_symrefs() derive the ref store via the_repository. Adapt them to instead take in the ref store as a parameter. While at it, rename the functions to have a ref_ prefix to align them with other functions that take a ref store.

---
 builtin/fetch.c  |  3 ++-
 builtin/remote.c |  3 ++-
 refs.c           | 40 ++++++++++++++++++++--------------------
 refs.h           |  7 ++++---
 4 files changed, 28 insertions(+), 25 deletions(-)
builtin/fetch.c +2 -1
index 3829d66b40..3df1c0c052 100644
@@ -1412,7 +1412,8 @@ static int prune_refs(struct display_state *display_state,
 					   _("(none)"), ref->name,
 					   &ref->new_oid, &ref->old_oid,
 					   summary_width);
-			warn_dangling_symref(stderr, dangling_msg, ref->name);
+			refs_warn_dangling_symref(get_main_ref_store(the_repository),
+						  stderr, dangling_msg, ref->name);
 		}
 	}
 
builtin/remote.c +2 -1
index ff70d6835a..c0b513cc95 100644
@@ -1477,7 +1477,8 @@ static int prune_remote(const char *remote, int dry_run)
 			       abbrev_ref(refname, "refs/remotes/"));
 	}
 
-	warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
+	refs_warn_dangling_symrefs(get_main_ref_store(the_repository),
+				   stdout, dangling_msg, &refs_to_prune);
 
 	string_list_clear(&refs_to_prune, 0);
 	free_remote_ref_states(&states);
refs.c +20 -20
index 345d6a1e06..e6bae9d52c 100644
@@ -447,6 +447,7 @@ enum peel_status peel_object(const struct object_id *name, struct object_id *oid
 }
 
 struct warn_if_dangling_data {
+	struct ref_store *refs;
 	FILE *fp;
 	const char *refname;
 	const struct string_list *refnames;
@@ -463,8 +464,7 @@ static int warn_if_dangling_symref(const char *refname,
 	if (!(flags & REF_ISSYMREF))
 		return 0;
 
-	resolves_to = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
-					      refname, 0, NULL, NULL);
+	resolves_to = refs_resolve_ref_unsafe(d->refs, refname, 0, NULL, NULL);
 	if (!resolves_to
 	    || (d->refname
 		? strcmp(resolves_to, d->refname)
@@ -477,28 +477,28 @@ static int warn_if_dangling_symref(const char *refname,
 	return 0;
 }
 
-void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname)
+void refs_warn_dangling_symref(struct ref_store *refs, FILE *fp,
+			       const char *msg_fmt, const char *refname)
 {
-	struct warn_if_dangling_data data;
-
-	data.fp = fp;
-	data.refname = refname;
-	data.refnames = NULL;
-	data.msg_fmt = msg_fmt;
-	refs_for_each_rawref(get_main_ref_store(the_repository),
-			     warn_if_dangling_symref, &data);
+	struct warn_if_dangling_data data = {
+		.refs = refs,
+		.fp = fp,
+		.refname = refname,
+		.msg_fmt = msg_fmt,
+	};
+	refs_for_each_rawref(refs, warn_if_dangling_symref, &data);
 }
 
-void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames)
+void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
+				const char *msg_fmt, const struct string_list *refnames)
 {
-	struct warn_if_dangling_data data;
-
-	data.fp = fp;
-	data.refname = NULL;
-	data.refnames = refnames;
-	data.msg_fmt = msg_fmt;
-	refs_for_each_rawref(get_main_ref_store(the_repository),
-			     warn_if_dangling_symref, &data);
+	struct warn_if_dangling_data data = {
+		.refs = refs,
+		.fp = fp,
+		.refnames = refnames,
+		.msg_fmt = msg_fmt,
+	};
+	refs_for_each_rawref(refs, warn_if_dangling_symref, &data);
 }
 
 int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
refs.h +4 -3
index 77f9887235..492ecfa4a1 100644
@@ -388,9 +388,10 @@ static inline const char *has_glob_specials(const char *pattern)
 	return strpbrk(pattern, "?*[");
 }
 
-void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname);
-void warn_dangling_symrefs(FILE *fp, const char *msg_fmt,
-			   const struct string_list *refnames);
+void refs_warn_dangling_symref(struct ref_store *refs, FILE *fp,
+			       const char *msg_fmt, const char *refname);
+void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
+				const char *msg_fmt, const struct string_list *refnames);
 
 /*
  * Flags for controlling behaviour of pack_refs()
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#22 lore raw

PATCH 10/16 refs: move object peeling into "object.c"

In reply to Patrick Steinhardt

Peeling an object has nothing to do with refs, but we still have the code in "refs.c". Move it over into "object.c", which is a more natural place to put it.

Ideally, we'd also move peel_iterated_oid() over into "object.c". But this function is tied to the refs interfaces because it uses a global ref iterator variable to optimize peeling when the iterator already has the peeled object ID readily available.

---
 object.c             | 21 +++++++++++++++++++++
 object.h             | 34 ++++++++++++++++++++++++++++++++++
 refs.c               | 22 ----------------------
 refs/refs-internal.h | 34 ----------------------------------
 4 files changed, 55 insertions(+), 56 deletions(-)
object.c +21 -0
index 51e384828e..995041926a 100644
@@ -207,6 +207,27 @@ struct object *lookup_object_by_type(struct repository *r,
 	}
 }
 
+enum peel_status peel_object(const struct object_id *name, struct object_id *oid)
+{
+	struct object *o = lookup_unknown_object(the_repository, name);
+
+	if (o->type == OBJ_NONE) {
+		int type = oid_object_info(the_repository, name, NULL);
+		if (type < 0 || !object_as_type(o, type, 0))
+			return PEEL_INVALID;
+	}
+
+	if (o->type != OBJ_TAG)
+		return PEEL_NON_TAG;
+
+	o = deref_tag_noverify(o);
+	if (!o)
+		return PEEL_INVALID;
+
+	oidcpy(oid, &o->oid);
+	return PEEL_PEELED;
+}
+
 struct object *parse_object_buffer(struct repository *r, const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
 {
 	struct object *obj;
object.h +34 -0
index 9293e703cc..31ccd1bb10 100644
@@ -256,6 +256,40 @@ struct object *lookup_unknown_object(struct repository *r, const struct object_i
 struct object *lookup_object_by_type(struct repository *r, const struct object_id *oid,
 				     enum object_type type);
 
+enum peel_status {
+	/* object was peeled successfully: */
+	PEEL_PEELED = 0,
+
+	/*
+	 * object cannot be peeled because the named object (or an
+	 * object referred to by a tag in the peel chain), does not
+	 * exist.
+	 */
+	PEEL_INVALID = -1,
+
+	/* object cannot be peeled because it is not a tag: */
+	PEEL_NON_TAG = -2,
+
+	/* ref_entry contains no peeled value because it is a symref: */
+	PEEL_IS_SYMREF = -3,
+
+	/*
+	 * ref_entry cannot be peeled because it is broken (i.e., the
+	 * symbolic reference cannot even be resolved to an object
+	 * name):
+	 */
+	PEEL_BROKEN = -4
+};
+
+/*
+ * Peel the named object; i.e., if the object is a tag, resolve the
+ * tag recursively until a non-tag is found.  If successful, store the
+ * result to oid and return PEEL_PEELED.  If the object is not a tag
+ * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively,
+ * and leave oid unchanged.
+ */
+enum peel_status peel_object(const struct object_id *name, struct object_id *oid);
+
 struct object_list *object_list_insert(struct object *item,
 				       struct object_list **list_p);
 
refs.c +0 -22
index e6bae9d52c..02d756f24f 100644
@@ -19,7 +19,6 @@
 #include "object-store-ll.h"
 #include "object.h"
 #include "path.h"
-#include "tag.h"
 #include "submodule.h"
 #include "worktree.h"
 #include "strvec.h"
@@ -425,27 +424,6 @@ static int for_each_filter_refs(const char *refname,
 	return filter->fn(refname, oid, flags, filter->cb_data);
 }
 
-enum peel_status peel_object(const struct object_id *name, struct object_id *oid)
-{
-	struct object *o = lookup_unknown_object(the_repository, name);
-
-	if (o->type == OBJ_NONE) {
-		int type = oid_object_info(the_repository, name, NULL);
-		if (type < 0 || !object_as_type(o, type, 0))
-			return PEEL_INVALID;
-	}
-
-	if (o->type != OBJ_TAG)
-		return PEEL_NON_TAG;
-
-	o = deref_tag_noverify(o);
-	if (!o)
-		return PEEL_INVALID;
-
-	oidcpy(oid, &o->oid);
-	return PEEL_PEELED;
-}
-
 struct warn_if_dangling_data {
 	struct ref_store *refs;
 	FILE *fp;
refs/refs-internal.h +0 -34
index fc77240c93..df9b16a872 100644
@@ -69,40 +69,6 @@ int ref_resolves_to_object(const char *refname,
 			   const struct object_id *oid,
 			   unsigned int flags);
 
-enum peel_status {
-	/* object was peeled successfully: */
-	PEEL_PEELED = 0,
-
-	/*
-	 * object cannot be peeled because the named object (or an
-	 * object referred to by a tag in the peel chain), does not
-	 * exist.
-	 */
-	PEEL_INVALID = -1,
-
-	/* object cannot be peeled because it is not a tag: */
-	PEEL_NON_TAG = -2,
-
-	/* ref_entry contains no peeled value because it is a symref: */
-	PEEL_IS_SYMREF = -3,
-
-	/*
-	 * ref_entry cannot be peeled because it is broken (i.e., the
-	 * symbolic reference cannot even be resolved to an object
-	 * name):
-	 */
-	PEEL_BROKEN = -4
-};
-
-/*
- * Peel the named object; i.e., if the object is a tag, resolve the
- * tag recursively until a non-tag is found.  If successful, store the
- * result to oid and return PEEL_PEELED.  If the object is not a tag
- * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively,
- * and leave oid unchanged.
- */
-enum peel_status peel_object(const struct object_id *name, struct object_id *oid);
-
 /**
  * Information needed for a single ref update. Set new_oid to the new
  * value or to null_oid to delete the ref. To check the old value
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#23 lore raw

Re: [PATCH 10/16] refs: move object peeling into "object.c"

Quoted 3 lines · Patrick Steinhardt

Peeling an object has nothing to do with refs, but we still have the code in "refs.c". Move it over into "object.c", which is a more natural place to put it.

Yay! Very nice.

Quoted 4 lines

Ideally, we'd also move peel_iterated_oid() over into "object.c". But this function is tied to the refs interfaces because it uses a global ref iterator variable to optimize peeling when the iterator already has the peeled object ID readily available.

Sure.

#24 lore raw

PATCH 11/16 refs: pass repo when peeling objects

In reply to Patrick Steinhardt

Both peel_object() and peel_iterated_oid() implicitly rely on the_repository to look up objects. Despite the fact that we want to get rid of the_repository, it also leads to some restrictions in our ref iterators when trying to retrieve the peeled value for a repository other than the_repository.

Refactor these functions such that both take a repository as argument and remove the now-unnecessary restrictions.

---
 builtin/describe.c      |  2 +-
 builtin/gc.c            |  2 +-
 builtin/pack-objects.c  |  6 +++---
 builtin/repack.c        |  2 +-
 builtin/show-ref.c      |  2 +-
 commit-graph.c          |  2 +-
 ls-refs.c               |  2 +-
 midx-write.c            |  2 +-
 object.c                | 10 ++++++----
 object.h                |  3 ++-
 ref-filter.c            |  2 +-
 refs.c                  |  4 ++--
 refs.h                  |  5 +++--
 refs/packed-backend.c   |  8 +++-----
 refs/ref-cache.c        |  5 +----
 refs/reftable-backend.c |  6 ++++--
 t/helper/test-reach.c   |  2 +-
 tag.c                   |  4 ++--
 tag.h                   |  2 +-
 upload-pack.c           |  2 +-
 20 files changed, 37 insertions(+), 36 deletions(-)
builtin/describe.c +1 -1
index 82aca00c80..e5287eddf2 100644
@@ -200,7 +200,7 @@ static int get_name(const char *path, const struct object_id *oid,
 	}
 
 	/* Is it annotated? */
-	if (!peel_iterated_oid(oid, &peeled)) {
+	if (!peel_iterated_oid(the_repository, oid, &peeled)) {
 		is_annotated = !oideq(oid, &peeled);
 	} else {
 		oidcpy(&peeled, oid);
builtin/gc.c +1 -1
index 054fca7835..72bac2554f 100644
@@ -846,7 +846,7 @@ static int dfs_on_ref(const char *refname UNUSED,
 	struct commit_list *stack = NULL;
 	struct commit *commit;
 
-	if (!peel_iterated_oid(oid, &peeled))
+	if (!peel_iterated_oid(the_repository, oid, &peeled))
 		oid = &peeled;
 	if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
 		return 0;
builtin/pack-objects.c +3 -3
index cd2396896d..62ddf41f84 100644
@@ -779,7 +779,7 @@ static int mark_tagged(const char *path UNUSED, const struct object_id *oid,
 
 	if (entry)
 		entry->tagged = 1;
-	if (!peel_iterated_oid(oid, &peeled)) {
+	if (!peel_iterated_oid(the_repository, oid, &peeled)) {
 		entry = packlist_find(&to_pack, &peeled);
 		if (entry)
 			entry->tagged = 1;
@@ -3125,7 +3125,7 @@ static int add_ref_tag(const char *tag UNUSED, const struct object_id *oid,
 {
 	struct object_id peeled;
 
-	if (!peel_iterated_oid(oid, &peeled) && obj_is_packed(&peeled))
+	if (!peel_iterated_oid(the_repository, oid, &peeled) && obj_is_packed(&peeled))
 		add_tag_chain(oid);
 	return 0;
 }
@@ -4074,7 +4074,7 @@ static int mark_bitmap_preferred_tip(const char *refname,
 	struct object_id peeled;
 	struct object *object;
 
-	if (!peel_iterated_oid(oid, &peeled))
+	if (!peel_iterated_oid(the_repository, oid, &peeled))
 		oid = &peeled;
 
 	object = parse_object_or_die(oid, refname);
builtin/repack.c +1 -1
index 43491a4cbf..58ad82dd97 100644
@@ -673,7 +673,7 @@ static int midx_snapshot_ref_one(const char *refname UNUSED,
 	struct midx_snapshot_ref_data *data = _data;
 	struct object_id peeled;
 
-	if (!peel_iterated_oid(oid, &peeled))
+	if (!peel_iterated_oid(the_repository, oid, &peeled))
 		oid = &peeled;
 
 	if (oidset_insert(&data->seen, oid))
builtin/show-ref.c +1 -1
index 151ef35134..3114bdc391 100644
@@ -50,7 +50,7 @@ static void show_one(const struct show_one_options *opts,
 	if (!opts->deref_tags)
 		return;
 
-	if (!peel_iterated_oid(oid, &peeled)) {
+	if (!peel_iterated_oid(the_repository, oid, &peeled)) {
 		hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev);
 		printf("%s %s^{}\n", hex, refname);
 	}
commit-graph.c +1 -1
index c4c156ff52..e5dd3553df 100644
@@ -1821,7 +1821,7 @@ static int add_ref_to_set(const char *refname UNUSED,
 	struct object_id peeled;
 	struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
 
-	if (!peel_iterated_oid(oid, &peeled))
+	if (!peel_iterated_oid(the_repository, oid, &peeled))
 		oid = &peeled;
 	if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
 		oidset_insert(data->commits, oid);
ls-refs.c +1 -1
index 8e3ffff811..398afe4ce3 100644
@@ -110,7 +110,7 @@ static int send_ref(const char *refname, const struct object_id *oid,
 
 	if (data->peel && oid) {
 		struct object_id peeled;
-		if (!peel_iterated_oid(oid, &peeled))
+		if (!peel_iterated_oid(the_repository, oid, &peeled))
 			strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
 	}
 
midx-write.c +1 -1
index 9d096d5a28..86173abdb9 100644
@@ -664,7 +664,7 @@ static int add_ref_to_pending(const char *refname,
 		return 0;
 	}
 
-	if (!peel_iterated_oid(oid, &peeled))
+	if (!peel_iterated_oid(the_repository, oid, &peeled))
 		oid = &peeled;
 
 	object = parse_object_or_die(oid, refname);
object.c +6 -4
index 995041926a..93b5d97fdb 100644
@@ -207,12 +207,14 @@ struct object *lookup_object_by_type(struct repository *r,
 	}
 }
 
-enum peel_status peel_object(const struct object_id *name, struct object_id *oid)
+enum peel_status peel_object(struct repository *r,
+			     const struct object_id *name,
+			     struct object_id *oid)
 {
-	struct object *o = lookup_unknown_object(the_repository, name);
+	struct object *o = lookup_unknown_object(r, name);
 
 	if (o->type == OBJ_NONE) {
-		int type = oid_object_info(the_repository, name, NULL);
+		int type = oid_object_info(r, name, NULL);
 		if (type < 0 || !object_as_type(o, type, 0))
 			return PEEL_INVALID;
 	}
@@ -220,7 +222,7 @@ enum peel_status peel_object(const struct object_id *name, struct object_id *oid
 	if (o->type != OBJ_TAG)
 		return PEEL_NON_TAG;
 
-	o = deref_tag_noverify(o);
+	o = deref_tag_noverify(r, o);
 	if (!o)
 		return PEEL_INVALID;
 
object.h +2 -1
index 31ccd1bb10..83fcc035e9 100644
@@ -288,7 +288,8 @@ enum peel_status {
  * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively,
  * and leave oid unchanged.
  */
-enum peel_status peel_object(const struct object_id *name, struct object_id *oid);
+enum peel_status peel_object(struct repository *r,
+			     const struct object_id *name, struct object_id *oid);
 
 struct object_list *object_list_insert(struct object *item,
 				       struct object_list **list_p);
ref-filter.c +1 -1
index 31cc096644..79e7d3910d 100644
@@ -2520,7 +2520,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
 	 * If it is a tag object, see if we use the peeled value. If we do,
 	 * grab the peeled OID.
 	 */
-	if (need_tagged && peel_iterated_oid(&obj->oid, &oi_deref.oid))
+	if (need_tagged && peel_iterated_oid(the_repository, &obj->oid, &oi_deref.oid))
 		die("bad tag");
 
 	return get_object(ref, 1, &obj, &oi_deref, err);
refs.c +2 -2
index 02d756f24f..7aecf11bf4 100644
@@ -2064,14 +2064,14 @@ int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts)
 	return refs->be->pack_refs(refs, opts);
 }
 
-int peel_iterated_oid(const struct object_id *base, struct object_id *peeled)
+int peel_iterated_oid(struct repository *r, const struct object_id *base, struct object_id *peeled)
 {
 	if (current_ref_iter &&
 	    (current_ref_iter->oid == base ||
 	     oideq(current_ref_iter->oid, base)))
 		return ref_iterator_peel(current_ref_iter, peeled);
 
-	return peel_object(base, peeled) ? -1 : 0;
+	return peel_object(r, base, peeled) ? -1 : 0;
 }
 
 int refs_create_symref(struct ref_store *refs,
refs.h +3 -2
index 492ecfa4a1..9769a25edd 100644
@@ -127,13 +127,14 @@ void ref_store_release(struct ref_store *ref_store);
  * Return the peeled value of the oid currently being iterated via
  * for_each_ref(), etc. This is equivalent to calling:
  *
- *   peel_object(oid, &peeled);
+ *   peel_object(r, oid, &peeled);
  *
  * with the "oid" value given to the each_ref_fn callback, except
  * that some ref storage may be able to answer the query without
  * actually loading the object in memory.
  */
-int peel_iterated_oid(const struct object_id *base, struct object_id *peeled);
+int peel_iterated_oid(struct repository *r,
+		      const struct object_id *base, struct object_id *peeled);
 
 /**
  * Resolve refname in the nested "gitlink" repository in the specified
refs/packed-backend.c +3 -5
index bebceb4aa7..1b06fd936a 100644
@@ -953,16 +953,13 @@ static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
 	struct packed_ref_iterator *iter =
 		(struct packed_ref_iterator *)ref_iterator;
 
-	if (iter->repo != the_repository)
-		BUG("peeling for non-the_repository is not supported");
-
 	if ((iter->base.flags & REF_KNOWS_PEELED)) {
 		oidcpy(peeled, &iter->peeled);
 		return is_null_oid(&iter->peeled) ? -1 : 0;
 	} else if ((iter->base.flags & (REF_ISBROKEN | REF_ISSYMREF))) {
 		return -1;
 	} else {
-		return peel_object(&iter->oid, peeled) ? -1 : 0;
+		return peel_object(iter->repo, &iter->oid, peeled) ? -1 : 0;
 	}
 }
 
@@ -1421,7 +1418,8 @@ static int write_with_updates(struct packed_ref_store *refs,
 			i++;
 		} else {
 			struct object_id peeled;
-			int peel_error = peel_object(&update->new_oid,
+			int peel_error = peel_object(refs->base.repo,
+						     &update->new_oid,
 						     &peeled);
 
 			if (write_packed_entry(out, update->refname,
refs/ref-cache.c +1 -4
index 9f9797209a..b6c53fc8ed 100644
@@ -441,10 +441,7 @@ static int cache_ref_iterator_peel(struct ref_iterator *ref_iterator,
 {
 	struct cache_ref_iterator *iter =
 		(struct cache_ref_iterator *)ref_iterator;
-
-	if (iter->repo != the_repository)
-		BUG("peeling for non-the_repository is not supported");
-	return peel_object(ref_iterator->oid, peeled) ? -1 : 0;
+	return peel_object(iter->repo, ref_iterator->oid, peeled) ? -1 : 0;
 }
 
 static int cache_ref_iterator_abort(struct ref_iterator *ref_iterator)
refs/reftable-backend.c +4 -2
index 6c262c2193..4a8f83cb1a 100644
@@ -1144,7 +1144,7 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
 			ref.refname = (char *)u->refname;
 			ref.update_index = ts;
 
-			peel_error = peel_object(&u->new_oid, &peeled);
+			peel_error = peel_object(arg->refs->base.repo, &u->new_oid, &peeled);
 			if (!peel_error) {
 				ref.value_type = REFTABLE_REF_VAL2;
 				memcpy(ref.value.val2.target_value, peeled.hash, GIT_MAX_RAWSZ);
@@ -2045,6 +2045,7 @@ static int reftable_be_delete_reflog(struct ref_store *ref_store,
 }
 
 struct reflog_expiry_arg {
+	struct reftable_ref_store *refs;
 	struct reftable_stack *stack;
 	struct reftable_log_record *records;
 	struct object_id update_oid;
@@ -2073,7 +2074,7 @@ static int write_reflog_expiry_table(struct reftable_writer *writer, void *cb_da
 		ref.refname = (char *)arg->refname;
 		ref.update_index = ts;
 
-		if (!peel_object(&arg->update_oid, &peeled)) {
+		if (!peel_object(arg->refs->base.repo, &arg->update_oid, &peeled)) {
 			ref.value_type = REFTABLE_REF_VAL2;
 			memcpy(ref.value.val2.target_value, peeled.hash, GIT_MAX_RAWSZ);
 			memcpy(ref.value.val2.value, arg->update_oid.hash, GIT_MAX_RAWSZ);
@@ -2235,6 +2236,7 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
 	    reftable_ref_record_val1(&ref_record))
 		oidread(&arg.update_oid, last_hash);
 
+	arg.refs = refs;
 	arg.records = rewritten;
 	arg.len = logs_nr;
 	arg.stack = stack,
t/helper/test-reach.c +1 -1
index 1e3b431e3e..1ba226f1f9 100644
@@ -62,7 +62,7 @@ int cmd__reach(int ac, const char **av)
 			die("failed to resolve %s", buf.buf + 2);
 
 		orig = parse_object(r, &oid);
-		peeled = deref_tag_noverify(orig);
+		peeled = deref_tag_noverify(the_repository, orig);
 
 		if (!peeled)
 			die("failed to load commit for input %s resulting in oid %s\n",
tag.c +2 -2
index fc3834db46..52bbe50819 100644
@@ -91,10 +91,10 @@ struct object *deref_tag(struct repository *r, struct object *o, const char *war
 	return o;
 }
 
-struct object *deref_tag_noverify(struct object *o)
+struct object *deref_tag_noverify(struct repository *r, struct object *o)
 {
 	while (o && o->type == OBJ_TAG) {
-		o = parse_object(the_repository, &o->oid);
+		o = parse_object(r, &o->oid);
 		if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged)
 			o = ((struct tag *)o)->tagged;
 		else
tag.h +1 -1
index 3ce8e72192..c49d7c19ad 100644
@@ -16,7 +16,7 @@ int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, u
 int parse_tag(struct tag *item);
 void release_tag_memory(struct tag *t);
 struct object *deref_tag(struct repository *r, struct object *, const char *, int);
-struct object *deref_tag_noverify(struct object *);
+struct object *deref_tag_noverify(struct repository *r, struct object *);
 int gpg_verify_tag(const struct object_id *oid,
 		   const char *name_to_report, unsigned flags);
 struct object_id *get_tagged_oid(struct tag *tag);
upload-pack.c +1 -1
index 8fbd138515..bbfb04c8bd 100644
@@ -1269,7 +1269,7 @@ static void write_v0_ref(struct upload_pack_data *data,
 		packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
 	}
 	capabilities = NULL;
-	if (!peel_iterated_oid(oid, &peeled))
+	if (!peel_iterated_oid(the_repository, oid, &peeled))
 		packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
 	return;
 }
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#25 lore raw

PATCH 12/16 refs: drop `git_default_branch_name()`

In reply to Patrick Steinhardt

The git_default_branch_name() function is a thin wrapper around repo_default_branch_name() with two differences:

- We implicitly rely on the_repository.

- We cache the default branch name.

None of the callsites of git_default_branch_name() are hot code paths though, so the caching of the branch name is not really required.

Refactor the callsites to use repo_default_branch_name() instead and drop git_default_branch_name(), thus getting rid of one more case where we rely on the_repository.

---
 builtin/clone.c |  5 ++++-
 builtin/var.c   |  2 +-
 refs.c          | 10 ----------
 refs.h          |  4 +---
 remote.c        | 12 ++++++++----
 setup.c         |  5 ++++-
 6 files changed, 18 insertions(+), 20 deletions(-)
builtin/clone.c +4 -1
index 554b29768c..bd3e8302ed 100644
@@ -1468,6 +1468,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 	} else if (remote_head) {
 		our_head_points_at = NULL;
 	} else {
+		char *to_free = NULL;
 		const char *branch;
 
 		if (!mapped_refs) {
@@ -1480,7 +1481,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 				"refs/heads/", &branch)) {
 			unborn_head  = xstrdup(transport_ls_refs_options.unborn_head_target);
 		} else {
-			branch = git_default_branch_name(0);
+			branch = to_free = repo_default_branch_name(the_repository, 0);
 			unborn_head = xstrfmt("refs/heads/%s", branch);
 		}
 
@@ -1496,6 +1497,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		 * a match.
 		 */
 		our_head_points_at = find_remote_branch(mapped_refs, branch);
+
+		free(to_free);
 	}
 
 	write_refspec_config(src_ref_prefix, our_head_points_at,
builtin/var.c +1 -1
index cf5567208a..5dc384810c 100644
@@ -46,7 +46,7 @@ static char *pager(int ident_flag UNUSED)
 
 static char *default_branch(int ident_flag UNUSED)
 {
-	return xstrdup_or_null(git_default_branch_name(1));
+	return repo_default_branch_name(the_repository, 1);
 }
 
 static char *shell_path(int ident_flag UNUSED)
refs.c +0 -10
index 7aecf11bf4..3618c8f7a4 100644
@@ -664,16 +664,6 @@ char *repo_default_branch_name(struct repository *r, int quiet)
 	return ret;
 }
 
-const char *git_default_branch_name(int quiet)
-{
-	static char *ret;
-
-	if (!ret)
-		ret = repo_default_branch_name(the_repository, quiet);
-
-	return ret;
-}
-
 /*
  * *string and *len will only be substituted, and *string returned (for
  * later free()ing) if the string passed in is a magic short-hand form
refs.h +1 -3
index 9769a25edd..95c3437443 100644
@@ -169,10 +169,8 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
 /*
  * Retrieves the default branch name for newly-initialized repositories.
  *
- * The return value of `repo_default_branch_name()` is an allocated string. The
- * return value of `git_default_branch_name()` is a singleton.
+ * The return value is an allocated string.
  */
-const char *git_default_branch_name(int quiet);
 char *repo_default_branch_name(struct repository *r, int quiet);
 
 /*
remote.c +8 -4
index ec8c158e60..85c390b199 100644
@@ -305,7 +305,7 @@ static void read_remotes_file(struct remote_state *remote_state,
 static void read_branches_file(struct remote_state *remote_state,
 			       struct remote *remote)
 {
-	char *frag;
+	char *frag, *to_free = NULL;
 	struct strbuf buf = STRBUF_INIT;
 	FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
 
@@ -333,7 +333,7 @@ static void read_branches_file(struct remote_state *remote_state,
 	if (frag)
 		*(frag++) = '\0';
 	else
-		frag = (char *)git_default_branch_name(0);
+		frag = to_free = repo_default_branch_name(the_repository, 0);
 
 	add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL));
 	refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
@@ -345,6 +345,8 @@ static void read_branches_file(struct remote_state *remote_state,
 	 */
 	refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag);
 	remote->fetch_tags = 1; /* always auto-follow */
+
+	free(to_free);
 }
 
 static int handle_config(const char *key, const char *value,
@@ -2388,11 +2390,13 @@ struct ref *guess_remote_head(const struct ref *head,
 
 	/* If a remote branch exists with the default branch name, let's use it. */
 	if (!all) {
-		char *ref = xstrfmt("refs/heads/%s",
-				    git_default_branch_name(0));
+		char *default_branch = repo_default_branch_name(the_repository, 0);
+		char *ref = xstrfmt("refs/heads/%s", default_branch);
 
 		r = find_ref_by_name(refs, ref);
 		free(ref);
+		free(default_branch);
+
 		if (r && oideq(&r->old_oid, &head->old_oid))
 			return copy_ref(r);
 
setup.c +4 -1
index 4a738f4c90..15ad84d3be 100644
@@ -2046,6 +2046,7 @@ void create_reference_database(unsigned int ref_storage_format,
 			       const char *initial_branch, int quiet)
 {
 	struct strbuf err = STRBUF_INIT;
+	char *to_free = NULL;
 	int reinit = is_reinit();
 
 	repo_set_ref_storage_format(the_repository, ref_storage_format);
@@ -2060,7 +2061,8 @@ void create_reference_database(unsigned int ref_storage_format,
 		char *ref;
 
 		if (!initial_branch)
-			initial_branch = git_default_branch_name(quiet);
+			initial_branch = to_free =
+				repo_default_branch_name(the_repository, quiet);
 
 		ref = xstrfmt("refs/heads/%s", initial_branch);
 		if (check_refname_format(ref, 0) < 0)
@@ -2077,6 +2079,7 @@ void create_reference_database(unsigned int ref_storage_format,
 			initial_branch);
 
 	strbuf_release(&err);
+	free(to_free);
 }
 
 static int create_default_files(const char *template_path,
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#26 lore raw

Re: [PATCH 12/16] refs: drop `git_default_branch_name()`

Quoted 29 lines · Patrick Steinhardt

The git_default_branch_name() function is a thin wrapper around repo_default_branch_name() with two differences:

- We implicitly rely on the_repository.

- We cache the default branch name.

None of the callsites of git_default_branch_name() are hot code paths though, so the caching of the branch name is not really required.

Refactor the callsites to use repo_default_branch_name() instead and drop git_default_branch_name(), thus getting rid of one more case where we rely on the_repository.

---
 builtin/clone.c |  5 ++++-
 builtin/var.c   |  2 +-
 refs.c          | 10 ----------
 refs.h          |  4 +---
 remote.c        | 12 ++++++++----
 setup.c         |  5 ++++-
 6 files changed, 18 insertions(+), 20 deletions(-)
builtin/clone.c +0 -0
index 554b29768c..bd3e8302ed 100644

builtin/clone.c int cmd_clone(int argc, const char **argv, const char *prefix) · line 1473

Quoted 6 lines
builtin/clone.c +1 -0
@@ -1468,6 +1468,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 	} else if (remote_head) {
 		our_head_points_at = NULL;
 	} else {
+		char *to_free = NULL;
 		const char *branch;
 

This is a really good pattern, Makes it much cleaner to understand.

[snip]

#27 lore raw

PATCH 13/16 refs: remove `dwim_log()`

In reply to Patrick Steinhardt

Remove dwim_log() in favor of repo_dwim_log() so that we can get rid of one more dependency on the_repository.

---
 builtin/reflog.c | 2 +-
 reflog-walk.c    | 4 ++--
 reflog.c         | 2 +-
 refs.c           | 5 -----
 refs.h           | 1 -
 5 files changed, 4 insertions(+), 10 deletions(-)
builtin/reflog.c +1 -1
index b4650cea16..0d2ff95c6e 100644
@@ -378,7 +378,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 		char *ref;
 		struct expire_reflog_policy_cb cb = { .cmd = cmd };
 
-		if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
+		if (!repo_dwim_log(the_repository, argv[i], strlen(argv[i]), NULL, &ref)) {
 			status |= error(_("%s points nowhere!"), argv[i]);
 			continue;
 		}
reflog-walk.c +2 -2
index f11b97e889..5f09552c5c 100644
@@ -191,8 +191,8 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
 		reflogs = read_complete_reflog(branch);
 		if (!reflogs || reflogs->nr == 0) {
 			char *b;
-			int ret = dwim_log(branch, strlen(branch),
-					   NULL, &b);
+			int ret = repo_dwim_log(the_repository, branch, strlen(branch),
+						NULL, &b);
 			if (ret > 1)
 				free(b);
 			else if (ret == 1) {
reflog.c +1 -1
index 8861c2d606..3c80950186 100644
@@ -409,7 +409,7 @@ int reflog_delete(const char *rev, enum expire_reflog_flags flags, int verbose)
 	if (!spec)
 		return error(_("not a reflog: %s"), rev);
 
-	if (!dwim_log(rev, spec - rev, NULL, &ref)) {
+	if (!repo_dwim_log(the_repository, rev, spec - rev, NULL, &ref)) {
 		status |= error(_("no reflog for '%s'"), rev);
 		goto cleanup;
 	}
refs.c +0 -5
index 3618c8f7a4..723bf7af20 100644
@@ -775,11 +775,6 @@ int repo_dwim_log(struct repository *r, const char *str, int len,
 	return logs_found;
 }
 
-int dwim_log(const char *str, int len, struct object_id *oid, char **log)
-{
-	return repo_dwim_log(the_repository, str, len, oid, log);
-}
-
 int is_per_worktree_ref(const char *refname)
 {
 	return starts_with(refname, "refs/worktree/") ||
refs.h +0 -1
index 95c3437443..fe0b6b44c5 100644
@@ -164,7 +164,6 @@ int expand_ref(struct repository *r, const char *str, int len, struct object_id
 int repo_dwim_ref(struct repository *r, const char *str, int len,
 		  struct object_id *oid, char **ref, int nonfatal_dangling_mark);
 int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref);
-int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
 
 /*
  * Retrieves the default branch name for newly-initialized repositories.
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#28 lore raw

PATCH 14/16 refs/files: use correct repository

In reply to Patrick Steinhardt

There are several places in the "files" backend where we use the_repository instead of the repository associated with the ref store itself. Adapt those to use the correct repository.

---
 refs/files-backend.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
refs/files-backend.c +13 -10
index 368df075c1..5294282770 100644
@@ -1237,7 +1237,8 @@ static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_
 /*
  * Return true if the specified reference should be packed.
  */
-static int should_pack_ref(const char *refname,
+static int should_pack_ref(struct files_ref_store *refs,
+			   const char *refname,
 			   const struct object_id *oid, unsigned int ref_flags,
 			   struct pack_refs_opts *opts)
 {
@@ -1253,7 +1254,7 @@ static int should_pack_ref(const char *refname,
 		return 0;
 
 	/* Do not pack broken refs: */
-	if (!ref_resolves_to_object(refname, the_repository, oid, ref_flags))
+	if (!ref_resolves_to_object(refname, refs->base.repo, oid, ref_flags))
 		return 0;
 
 	if (ref_excluded(opts->exclusions, refname))
@@ -1285,14 +1286,14 @@ static int files_pack_refs(struct ref_store *ref_store,
 	packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
 
 	iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
-					the_repository, 0);
+					refs->base.repo, 0);
 	while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
 		/*
 		 * If the loose reference can be packed, add an entry
 		 * in the packed ref cache. If the reference should be
 		 * pruned, also add it to refs_to_prune.
 		 */
-		if (!should_pack_ref(iter->refname, iter->oid, iter->flags, opts))
+		if (!should_pack_ref(refs, iter->refname, iter->oid, iter->flags, opts))
 			continue;
 
 		/*
@@ -1389,7 +1390,8 @@ static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
 	return ret;
 }
 
-static int write_ref_to_lockfile(struct ref_lock *lock,
+static int write_ref_to_lockfile(struct files_ref_store *refs,
+				 struct ref_lock *lock,
 				 const struct object_id *oid,
 				 int skip_oid_verification, struct strbuf *err);
 static int commit_ref_update(struct files_ref_store *refs,
@@ -1537,7 +1539,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
 	}
 	oidcpy(&lock->old_oid, &orig_oid);
 
-	if (write_ref_to_lockfile(lock, &orig_oid, 0, &err) ||
+	if (write_ref_to_lockfile(refs, lock, &orig_oid, 0, &err) ||
 	    commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) {
 		error("unable to write current sha1 into %s: %s", newrefname, err.buf);
 		strbuf_release(&err);
@@ -1557,7 +1559,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store,
 
 	flag = log_all_ref_updates;
 	log_all_ref_updates = LOG_REFS_NONE;
-	if (write_ref_to_lockfile(lock, &orig_oid, 0, &err) ||
+	if (write_ref_to_lockfile(refs, lock, &orig_oid, 0, &err) ||
 	    commit_ref_update(refs, lock, &orig_oid, NULL, &err)) {
 		error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
 		strbuf_release(&err);
@@ -1791,7 +1793,8 @@ static int files_log_ref_write(struct files_ref_store *refs,
  * Write oid into the open lockfile, then close the lockfile. On
  * errors, rollback the lockfile, fill in *err and return -1.
  */
-static int write_ref_to_lockfile(struct ref_lock *lock,
+static int write_ref_to_lockfile(struct files_ref_store *refs,
+				 struct ref_lock *lock,
 				 const struct object_id *oid,
 				 int skip_oid_verification, struct strbuf *err)
 {
@@ -1800,7 +1803,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
 	int fd;
 
 	if (!skip_oid_verification) {
-		o = parse_object(the_repository, oid);
+		o = parse_object(refs->base.repo, oid);
 		if (!o) {
 			strbuf_addf(
 				err,
@@ -2571,7 +2574,7 @@ static int lock_ref_for_update(struct files_ref_store *refs,
 			 * value, so we don't need to write it.
 			 */
 		} else if (write_ref_to_lockfile(
-				   lock, &update->new_oid,
+				   refs, lock, &update->new_oid,
 				   update->flags & REF_SKIP_OID_VERIFICATION,
 				   err)) {
 			char *write_err = strbuf_detach(err, NULL);
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#29 lore raw

PATCH 15/16 refs/files: remove references to `the_hash_algo`

In reply to Patrick Steinhardt

Remove references to the_hash_algo in favor of the hash algo specified by the repository associated with the files ref store.

---
 refs/files-backend.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
refs/files-backend.c +2 -2
index 5294282770..5684f1bfc6 100644
@@ -1822,7 +1822,7 @@ static int write_ref_to_lockfile(struct files_ref_store *refs,
 		}
 	}
 	fd = get_lock_file_fd(&lock->lk);
-	if (write_in_full(fd, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
+	if (write_in_full(fd, oid_to_hex(oid), refs->base.repo->hash_algo->hexsz) < 0 ||
 	    write_in_full(fd, &term, 1) < 0 ||
 	    fsync_component(FSYNC_COMPONENT_REFERENCE, get_lock_file_fd(&lock->lk)) < 0 ||
 	    close_ref_gently(lock) < 0) {
@@ -3223,7 +3223,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
 			rollback_lock_file(&reflog_lock);
 		} else if (update &&
 			   (write_in_full(get_lock_file_fd(&lock->lk),
-				oid_to_hex(&cb.last_kept_oid), the_hash_algo->hexsz) < 0 ||
+				oid_to_hex(&cb.last_kept_oid), refs->base.repo->hash_algo->hexsz) < 0 ||
 			    write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 ||
 			    close_ref_gently(lock) < 0)) {
 			status |= error("couldn't write %s",
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#30 lore raw

PATCH 16/16 refs/packed: remove references to `the_hash_algo`

In reply to Patrick Steinhardt

Remove references to the_hash_algo in favor of the hash algo specified by the repository associated with the packed ref store.

---
 refs/packed-backend.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
refs/packed-backend.c +20 -12
index 1b06fd936a..1d24c1ff28 100644
@@ -200,6 +200,11 @@ static int release_snapshot(struct snapshot *snapshot)
 	}
 }
 
+static size_t snapshot_hexsz(const struct snapshot *snapshot)
+{
+	return snapshot->refs->base.repo->hash_algo->hexsz;
+}
+
 struct ref_store *packed_ref_store_init(struct repository *repo,
 					const char *gitdir,
 					unsigned int store_flags)
@@ -289,11 +294,13 @@ struct snapshot_record {
 	size_t len;
 };
 
-static int cmp_packed_ref_records(const void *v1, const void *v2)
+static int cmp_packed_ref_records(const void *v1, const void *v2,
+				  void *cb_data)
 {
+	const struct snapshot *snapshot = cb_data;
 	const struct snapshot_record *e1 = v1, *e2 = v2;
-	const char *r1 = e1->start + the_hash_algo->hexsz + 1;
-	const char *r2 = e2->start + the_hash_algo->hexsz + 1;
+	const char *r1 = e1->start + snapshot_hexsz(snapshot) + 1;
+	const char *r2 = e2->start + snapshot_hexsz(snapshot) + 1;
 
 	while (1) {
 		if (*r1 == '\n')
@@ -314,9 +321,9 @@ static int cmp_packed_ref_records(const void *v1, const void *v2)
  * refname.
  */
 static int cmp_record_to_refname(const char *rec, const char *refname,
-				 int start)
+				 int start, const struct snapshot *snapshot)
 {
-	const char *r1 = rec + the_hash_algo->hexsz + 1;
+	const char *r1 = rec + snapshot_hexsz(snapshot) + 1;
 	const char *r2 = refname;
 
 	while (1) {
@@ -363,7 +370,7 @@ static void sort_snapshot(struct snapshot *snapshot)
 		if (!eol)
 			/* The safety check should prevent this. */
 			BUG("unterminated line found in packed-refs");
-		if (eol - pos < the_hash_algo->hexsz + 2)
+		if (eol - pos < snapshot_hexsz(snapshot) + 2)
 			die_invalid_line(snapshot->refs->path,
 					 pos, eof - pos);
 		eol++;
@@ -389,7 +396,7 @@ static void sort_snapshot(struct snapshot *snapshot)
 		if (sorted &&
 		    nr > 1 &&
 		    cmp_packed_ref_records(&records[nr - 2],
-					   &records[nr - 1]) >= 0)
+					   &records[nr - 1], snapshot) >= 0)
 			sorted = 0;
 
 		pos = eol;
@@ -399,7 +406,7 @@ static void sort_snapshot(struct snapshot *snapshot)
 		goto cleanup;
 
 	/* We need to sort the memory. First we sort the records array: */
-	QSORT(records, nr, cmp_packed_ref_records);
+	QSORT_S(records, nr, cmp_packed_ref_records, snapshot);
 
 	/*
 	 * Allocate a new chunk of memory, and copy the old memory to
@@ -475,7 +482,8 @@ static void verify_buffer_safe(struct snapshot *snapshot)
 		return;
 
 	last_line = find_start_of_record(start, eof - 1);
-	if (*(eof - 1) != '\n' || eof - last_line < the_hash_algo->hexsz + 2)
+	if (*(eof - 1) != '\n' ||
+	    eof - last_line < snapshot_hexsz(snapshot) + 2)
 		die_invalid_line(snapshot->refs->path,
 				 last_line, eof - last_line);
 }
@@ -570,7 +578,7 @@ static const char *find_reference_location_1(struct snapshot *snapshot,
 
 		mid = lo + (hi - lo) / 2;
 		rec = find_start_of_record(lo, mid);
-		cmp = cmp_record_to_refname(rec, refname, start);
+		cmp = cmp_record_to_refname(rec, refname, start, snapshot);
 		if (cmp < 0) {
 			lo = find_end_of_record(mid, hi);
 		} else if (cmp > 0) {
@@ -867,7 +875,7 @@ static int next_record(struct packed_ref_iterator *iter)
 	iter->base.flags = REF_ISPACKED;
 	p = iter->pos;
 
-	if (iter->eof - p < the_hash_algo->hexsz + 2 ||
+	if (iter->eof - p < snapshot_hexsz(iter->snapshot) + 2 ||
 	    parse_oid_hex(p, &iter->oid, &p) ||
 	    !isspace(*p++))
 		die_invalid_line(iter->snapshot->refs->path,
@@ -897,7 +905,7 @@ static int next_record(struct packed_ref_iterator *iter)
 
 	if (iter->pos < iter->eof && *iter->pos == '^') {
 		p = iter->pos + 1;
-		if (iter->eof - p < the_hash_algo->hexsz + 1 ||
+		if (iter->eof - p < snapshot_hexsz(iter->snapshot) + 1 ||
 		    parse_oid_hex(p, &iter->peeled, &p) ||
 		    *p++ != '\n')
 			die_invalid_line(iter->snapshot->refs->path,
Signature · 1 line
2.45.1.190.g19fe900cfc.dirty
#31 lore raw
In reply to Patrick Steinhardt

Hello,

Quoted 13 lines · Patrick Steinhardt

Hi,

in "ps/refs-without-the-repository", we have removed all functions in the refs API that could be trivially converted to accept a proper ref store as input. This was a mechanical change, and because the resulting patches were quite large by themselves already, I decided to push out further dependencies on the_repository in refs-related code to a follow up.

This patch series here is that follow-up and removes all references to the_repository in "refs.c" and "refs/". This includes both explicit use of the_repository, but also implicit use via the_hash_algo.

Quoted 6 lines

The series is based on 19fe900cfc (The fourth batch, 2024-05-15) and pulls in "ps/refs-without-the-repository" at c8f815c208 (refs: remove functions without ref store, 2024-05-07) as dependency. It applies cleanly to both next and seen at the current point in time.

Patrick

The commits here were easy to follow. Apart from some small nits, I don't have any changes to suggest here.

I did confirm we no longer have 'the_repository' anywhere in the refs code. Which is super. BTW, should we also remove the migration code added in refs.h, since 2.45 is now released?

Thanks

#32 lore raw
Quoted 22 lines · Karthik Nayak

Hello,

Quoted 18 lines · Patrick Steinhardt

Hi,

in "ps/refs-without-the-repository", we have removed all functions in the refs API that could be trivially converted to accept a proper ref store as input. This was a mechanical change, and because the resulting patches were quite large by themselves already, I decided to push out further dependencies on the_repository in refs-related code to a follow up.

This patch series here is that follow-up and removes all references to the_repository in "refs.c" and "refs/". This includes both explicit use of the_repository, but also implicit use via the_hash_algo.

The series is based on 19fe900cfc (The fourth batch, 2024-05-15) and pulls in "ps/refs-without-the-repository" at c8f815c208 (refs: remove functions without ref store, 2024-05-07) as dependency. It applies cleanly to both next and seen at the current point in time.

Quoted 5 lines
Quoted 1 line

Patrick

The commits here were easy to follow. Apart from some small nits, I don't have any changes to suggest here. I did confirm we no longer have 'the_repository' anywhere in the refs code. Which is super.

Thanks for your review!

Quoted 2 lines

BTW, should we also remove the migration code added in refs.h, since 2.45 is now released?

The migration code has only been added in v2.45, so it should only be released in v2.46 :)

Patrick