threads / patch / 66637

patchtmp-objdir: merge loose object index when migrating tmpdir

Subject: [PATCH] tmp-objdir: merge loose object index when migrating tmpdir

## tl;dr

With batch fsync and a compatibility hash both enabled, migrating the temporary object directory cannot handle an existing loose-object-idx file. Read the story.

replies: 33people: 4as markdown or json

Justin Tobler· Oct 22, 2026, 23:40 UTC · lore

When a repository is configured with a compatibility hash, each loose object write also records a mapping between the object and its compatibility hash in a "loose-object-idx" file. This index is located in the same object database the object is written to. If batch fsync of loose objects is also enabled, the loose objects and index file are instead written to a temporary object directory and expected to be migrated to the primary object directory when the batch transaction commits.

While migrating the loose objects works, the "loose-object-idx" file is unable to be migrated if an index already exists in the primary object directory. This occurs because `finalize_object_file_flags()` expects an already existing file to have the same contents. Otherwise, the following error is returned:

  error: files '.git/objects/tmp_objdir-bulk-fsync-XXXXXX/loose-object-idx' and '.git/objects/loose-object-idx' differ in contents
  error: unable to migrate temporary objects

Furthermore, it is not sufficient to just replace the preexisting index with the new one as the new index only contains mapping entries for the newly written objects.

Update the temporary directory migration logic to handle "loose-object-idx" files explicitly by appending new mapping entries to the primary index.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
Greetings,

While working on a separate patch series to convert the "files" ODB transaction to be eagerly created instead of lazily, I encountered this bug that causes an ODB transaction to fail on commit when a compatibility hash is also configured. This occurs because the temporary directory migration code does not know how to handle the loose object index file.

This patch addresses updating the migration logic to explicitly merge mapping entries in the transaction by appending them to the primary loose object index file.

Some other approaches I considered:
  - Storing the uncommitted object mapping entries in the transaction
    without writing a file and only writing on transaction commit. I
    chose to keep writing the temp index into the transaction so that
    child processes that write to the tempdir could still append entries.
  - Also writing mapping entires to the main ODB loose object index.
    This also didn't seem like a good apprach because it write entires
    that may not ultimately get committed.

Thanks for the review, -Justin

---
 loose.c                       | 72 +++++++++++++++++++++++++++++++++++
 loose.h                       |  2 +
 t/t1016-compatObjectFormat.sh | 27 +++++++++++++
 tmp-objdir.c                  | 11 +++++-
 4 files changed, 111 insertions(+), 1 deletion(-)
Show changes to 4 files +111 −1

loose.c, loose.h, t/t1016-compatObjectFormat.sh, tmp-objdir.c

diff --git a/loose.c b/loose.c
index d6a2bb5ba3..79d4c256e0 100644
--- a/loose.c
+++ b/loose.c
@@ -265,3 +265,75 @@ void loose_object_map_clear(struct loose_object_map **map)
 	free(m);
 	*map = NULL;
 }
+
+int loose_object_idx_merge(struct repository *repo, const char *src,
+			   const char *dst)
+{
+	struct lock_file lock = LOCK_INIT;
+	struct strbuf buf = STRBUF_INIT;
+	struct stat st;
+	FILE *fp;
+	int fd = -1;
+	int ret;
+
+	fp = fopen(src, "r");
+	if (!fp) {
+		ret = error_errno(_("unable to open %s"), src);
+		goto out;
+	}
+	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header)) {
+		ret = error(_("invalid loose object index %s"), src);
+		goto out;
+	}
+
+	if (repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0, -1) < 0) {
+		ret = error_errno(_("unable to lock %s"), dst);
+		goto out;
+	}
+
+	fd = open(dst, O_WRONLY | O_CREAT | O_APPEND, 0666);
+	if (fd < 0) {
+		ret = error_errno(_("unable to open %s"), dst);
+		goto out;
+	}
+	if (fstat(fd, &st) < 0) {
+		ret = error_errno(_("unable to stat %s"), dst);
+		goto out;
+	}
+
+	/* Create the loose-object-idx if one does not already exist. */
+	if (!st.st_size && write_in_full(fd, loose_object_header,
+					 strlen(loose_object_header)) < 0) {
+		ret = error_errno(_("failed to write loose object index header"));
+		goto out;
+	}
+
+	while (!strbuf_getwholeline(&buf, fp, '\n')) {
+		if (write_in_full(fd, buf.buf, buf.len) < 0) {
+			ret = error_errno(_("failed to write loose object index %s"),
+					  dst);
+			goto out;
+		}
+	}
+
+	ret = close(fd);
+	fd = -1;
+	if (ret) {
+		ret = error_errno(_("unable to close %s"), dst);
+		goto out;
+	}
+
+	adjust_shared_perm(repo, dst);
+
+	ret = 0;
+
+out:
+	rollback_lock_file(&lock);
+	strbuf_release(&buf);
+	if (fd >= 0)
+		close(fd);
+	if (fp)
+		fclose(fp);
+
+	return ret;
+}
diff --git a/loose.h b/loose.h
index ed663ac550..35b46f4be5 100644
--- a/loose.h
+++ b/loose.h
@@ -23,5 +23,7 @@ int repo_add_loose_object_map(struct odb_source_loose *loose,
 			      const struct object_id *compat_oid);
 int repo_read_loose_object_map(struct repository *repo);
 int repo_write_loose_object_map(struct repository *repo);
+int loose_object_idx_merge(struct repository *repo, const char *src,
+			   const char *dst);
 
 #endif
diff --git a/t/t1016-compatObjectFormat.sh b/t/t1016-compatObjectFormat.sh
index 9cafcee509..bebf71bc29 100755
--- a/t/t1016-compatObjectFormat.sh
+++ b/t/t1016-compatObjectFormat.sh
@@ -305,4 +305,31 @@ compare_oids GPG2 'commit' signedcommit4 "$signedcommit4_sha1_oid" "$signedcommi
 compare_oids GPG2 'tag' signedtag3 "$signedtag3_sha1_oid" "$signedtag3_sha256_oid"
 compare_oids GPG2 'tag' signedtag4 "$signedtag4_sha1_oid" "$signedtag4_sha256_oid"
 
+test_expect_success 'batched fsync migrates loose object index' '
+	test_when_finished "rm -rf batch-fsync" &&
+	git init --object-format=sha1 batch-fsync &&
+	(
+		cd batch-fsync &&
+		git config core.repositoryFormatVersion 1 &&
+		git config extensions.compatObjectFormat sha256 &&
+		git config core.fsync loose-object &&
+		git config core.fsyncMethod batch &&
+
+		# Write object to generate initial loose object index.
+		echo foo >foo &&
+		git add foo &&
+		test_grep "$(git rev-parse :foo)" .git/objects/loose-object-idx &&
+
+		# Write some additional objects and ensure the corresponding
+		# temporary loose object index entries stored in the transaction
+		# are merged into the primary ODB.
+		echo bar >bar &&
+		echo baz >baz &&
+		git add bar baz &&
+		test_grep "$(git rev-parse :foo)" .git/objects/loose-object-idx &&
+		test_grep "$(git rev-parse :bar)" .git/objects/loose-object-idx &&
+		test_grep "$(git rev-parse :baz)" .git/objects/loose-object-idx
+	)
+'
+
 test_done
diff --git a/tmp-objdir.c b/tmp-objdir.c
index 3debb60270..5307717354 100644
--- a/tmp-objdir.c
+++ b/tmp-objdir.c
@@ -4,6 +4,7 @@
 #include "chdir-notify.h"
 #include "dir.h"
 #include "environment.h"
+#include "loose.h"
 #include "object-file.h"
 #include "path.h"
 #include "string-list.h"
@@ -294,6 +295,11 @@ static int is_loose_object_shard(const char *name)
 	return strlen(name) == 2 && isxdigit(name[0]) && isxdigit(name[1]);
 }
 
+static int is_loose_object_idx(const char *name)
+{
+	return !strcmp(name, "loose-object-idx");
+}
+
 static int migrate_paths(struct tmp_objdir *t,
 			 struct strbuf *src, struct strbuf *dst,
 			 enum finalize_object_file_flags flags)
@@ -317,7 +323,10 @@ static int migrate_paths(struct tmp_objdir *t,
 		if (is_loose_object_shard(name))
 			flags_copy |= FOF_SKIP_COLLISION_CHECK;
 
-		ret |= migrate_one(t, src, dst, flags_copy);
+		if (is_loose_object_idx(name))
+			ret |= loose_object_idx_merge(t->repo, src->buf, dst->buf);
+		else
+			ret |= migrate_one(t, src, dst, flags_copy);
 
 		strbuf_setlen(src, src_len);
 		strbuf_setlen(dst, dst_len);

base-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e
-- 
2.56.0.173.gf57c9a7388
Patrick Steinhardt· Oct 23, 2026, 12:19 UTC · re: Justin Tobler · lore

Re: [PATCH] tmp-objdir: merge loose object index when migrating tmpdir

On Thu, Oct 22, 2026 at 06:40:07PM -0500, Justin Tobler wrote:
> When a repository is configured with a compatibility hash, each loose
> object write also records a mapping between the object and its
> compatibility hash in a "loose-object-idx" file. This index is located
> in the same object database the object is written to. If batch fsync of
You probably mean "object directory", not "database"?
Show 7 quoted lines
> loose objects is also enabled, the loose objects and index file are
> instead written to a temporary object directory and expected to be
> migrated to the primary object directory when the batch transaction
> commits.
> 
> While migrating the loose objects works, the "loose-object-idx" file is
> unable to be migrated if an index already exists in the primary object

The naming really throws me off, "index" to me is our staging area. What this is is a map. But oh, well, cannot change that now.

Show 14 quoted lines
> directory. This occurs because `finalize_object_file_flags()` expects an
> already existing file to have the same contents. Otherwise, the
> following error is returned:
> 
>   error: files '.git/objects/tmp_objdir-bulk-fsync-XXXXXX/loose-object-idx' and '.git/objects/loose-object-idx' differ in contents
>   error: unable to migrate temporary objects
> 
> Furthermore, it is not sufficient to just replace the preexisting index
> with the new one as the new index only contains mapping entries for the
> newly written objects.
> 
> Update the temporary directory migration logic to handle
> "loose-object-idx" files explicitly by appending new mapping entries to
> the primary index.
Sounds sensible.
Show 21 quoted lines
> Signed-off-by: Justin Tobler <jltobler@gmail.com>
> ---
> 
> Greetings,
> 
> While working on a separate patch series to convert the "files" ODB
> transaction to be eagerly created instead of lazily, I encountered this
> bug that causes an ODB transaction to fail on commit when a
> compatibility hash is also configured. This occurs because the temporary
> directory migration code does not know how to handle the loose object
> index file.
> 
> This patch addresses updating the migration logic to explicitly merge
> mapping entries in the transaction by appending them to the primary
> loose object index file.
> 
> Some other approaches I considered:
>   - Storing the uncommitted object mapping entries in the transaction
>     without writing a file and only writing on transaction commit. I
>     chose to keep writing the temp index into the transaction so that
>     child processes that write to the tempdir could still append entries.

Is that a theoretical scenario or do we actually have cases where a child process wants to write data into the quarantine directory, too?

>   - Also writing mapping entires to the main ODB loose object index.
>     This also didn't seem like a good apprach because it write entires
>     that may not ultimately get committed.

Agreed, we shouldn't commit this before the actual objects exist. I bet we don't properly handle this during housekeeping either -- or at least I never stumbled over logic that prunes mappings when pruning the corresponding objects. But that's a problem to solve another day.

Show 28 quoted lines
> diff --git a/loose.c b/loose.c
> index d6a2bb5ba3..79d4c256e0 100644
> --- a/loose.c
> +++ b/loose.c
> @@ -265,3 +265,75 @@ void loose_object_map_clear(struct loose_object_map **map)
>  	free(m);
>  	*map = NULL;
>  }
> +
> +int loose_object_idx_merge(struct repository *repo, const char *src,
> +			   const char *dst)
> +{
> +	struct lock_file lock = LOCK_INIT;
> +	struct strbuf buf = STRBUF_INIT;
> +	struct stat st;
> +	FILE *fp;
> +	int fd = -1;
> +	int ret;
> +
> +	fp = fopen(src, "r");
> +	if (!fp) {
> +		ret = error_errno(_("unable to open %s"), src);
> +		goto out;
> +	}
> +	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header)) {
> +		ret = error(_("invalid loose object index %s"), src);
> +		goto out;
> +	}
Curious. Why do we even expect that this file may be broken?
> +	if (repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0, -1) < 0) {
> +		ret = error_errno(_("unable to lock %s"), dst);
> +		goto out;
> +	}

With a timeout of -1 we'll block indefinitely. That doesn't feel like the best option to me, as the lockfile may be left over from a previous process. And Git locking up indefinitely feels suboptimal to me.

> +	fd = open(dst, O_WRONLY | O_CREAT | O_APPEND, 0666);

I don't think it's a good idea to append data under the lock. It may easily cause us to corrupt the file in case the write succeeds partially, only.

Our usual pattern here is to create the lockfile, write all data into the new file and then rename it into place.

I'm also a bit surprised that the file isn't sorted. Is a reader expected to always read _all_ mappings?

Show 23 quoted lines
> +	if (fd < 0) {
> +		ret = error_errno(_("unable to open %s"), dst);
> +		goto out;
> +	}
> +	if (fstat(fd, &st) < 0) {
> +		ret = error_errno(_("unable to stat %s"), dst);
> +		goto out;
> +	}
> +
> +	/* Create the loose-object-idx if one does not already exist. */
> +	if (!st.st_size && write_in_full(fd, loose_object_header,
> +					 strlen(loose_object_header)) < 0) {
> +		ret = error_errno(_("failed to write loose object index header"));
> +		goto out;
> +	}
> +
> +	while (!strbuf_getwholeline(&buf, fp, '\n')) {
> +		if (write_in_full(fd, buf.buf, buf.len) < 0) {
> +			ret = error_errno(_("failed to write loose object index %s"),
> +					  dst);
> +			goto out;
> +		}
> +	}

When merging, wouldn't we have to verify that the object IDs that we're adding don't already exist?

Show 13 quoted lines
> diff --git a/tmp-objdir.c b/tmp-objdir.c
> index 3debb60270..5307717354 100644
> --- a/tmp-objdir.c
> +++ b/tmp-objdir.c
> @@ -317,7 +323,10 @@ static int migrate_paths(struct tmp_objdir *t,
>  		if (is_loose_object_shard(name))
>  			flags_copy |= FOF_SKIP_COLLISION_CHECK;
>  
> -		ret |= migrate_one(t, src, dst, flags_copy);
> +		if (is_loose_object_idx(name))
> +			ret |= loose_object_idx_merge(t->repo, src->buf, dst->buf);
> +		else
> +			ret |= migrate_one(t, src, dst, flags_copy);
Makes sense.
Thanks!
Patrick
Justin Tobler· Oct 23, 2026, 21:25 UTC · re: Patrick Steinhardt · lore

Re: [PATCH] tmp-objdir: merge loose object index when migrating tmpdir

On 26/10/23 02:19PM, Patrick Steinhardt wrote:
Show 7 quoted lines
> On Thu, Oct 22, 2026 at 06:40:07PM -0500, Justin Tobler wrote:
> > When a repository is configured with a compatibility hash, each loose
> > object write also records a mapping between the object and its
> > compatibility hash in a "loose-object-idx" file. This index is located
> > in the same object database the object is written to. If batch fsync of
> 
> You probably mean "object directory", not "database"?
Ya, "object directory" would be more correct here. Will fix.
Show 10 quoted lines
> > loose objects is also enabled, the loose objects and index file are
> > instead written to a temporary object directory and expected to be
> > migrated to the primary object directory when the batch transaction
> > commits.
> > 
> > While migrating the loose objects works, the "loose-object-idx" file is
> > unable to be migrated if an index already exists in the primary object
> 
> The naming really throws me off, "index" to me is our staging area. What
> this is is a map. But oh, well, cannot change that now.

Agreed. I may change the commit message to call it a "mapping file" if that helps in any way.

[snip]
Show 8 quoted lines
> > Some other approaches I considered:
> >   - Storing the uncommitted object mapping entries in the transaction
> >     without writing a file and only writing on transaction commit. I
> >     chose to keep writing the temp index into the transaction so that
> >     child processes that write to the tempdir could still append entries.
> 
> Is that a theoretical scenario or do we actually have cases where a
> child process wants to write data into the quarantine directory, too?

I think a git-receive-pack(1) can spawn a git-unpack-objects(1) child process and write loose objects into the quarantine directory. Consequently, if a repository is configured with a compatibility hash, these loose objects would also end up writing mapping entries into the quarantine.

Show 8 quoted lines
> >   - Also writing mapping entires to the main ODB loose object index.
> >     This also didn't seem like a good apprach because it write entires
> >     that may not ultimately get committed.
> 
> Agreed, we shouldn't commit this before the actual objects exist. I bet
> we don't properly handle this during housekeeping either -- or at least
> I never stumbled over logic that prunes mappings when pruning the
> corresponding objects. But that's a problem to solve another day.

Yep, there doesn't appear to be any existing logic to prune entries from this mapping file. It can only grow in size. This seemed like a separate problem altogether though, so I didn't bother tackling that here.

Show 30 quoted lines
> > diff --git a/loose.c b/loose.c
> > index d6a2bb5ba3..79d4c256e0 100644
> > --- a/loose.c
> > +++ b/loose.c
> > @@ -265,3 +265,75 @@ void loose_object_map_clear(struct loose_object_map **map)
> >  	free(m);
> >  	*map = NULL;
> >  }
> > +
> > +int loose_object_idx_merge(struct repository *repo, const char *src,
> > +			   const char *dst)
> > +{
> > +	struct lock_file lock = LOCK_INIT;
> > +	struct strbuf buf = STRBUF_INIT;
> > +	struct stat st;
> > +	FILE *fp;
> > +	int fd = -1;
> > +	int ret;
> > +
> > +	fp = fopen(src, "r");
> > +	if (!fp) {
> > +		ret = error_errno(_("unable to open %s"), src);
> > +		goto out;
> > +	}
> > +	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header)) {
> > +		ret = error(_("invalid loose object index %s"), src);
> > +		goto out;
> > +	}
> 
> Curious. Why do we even expect that this file may be broken?

For our intended use case of reading the "loose-object-idx" file from the quarantine directory and merging its entries to the mapping file in the primary object directory, I wouldn't expect it to ever be broken. That is because realistically only a "loose-object-idx" that we create during the transaction should be written into the quarantine directory.

I chose to be more defensive here because the `loose_object_idx_merge()` function itself isn't aware of the circumstances regarding its use case. In theory, another caller could use any mapping file as the source. Ultimately though I don't feel too strongly either way and am happy to remove it if we don't like it.

Show 8 quoted lines
> > +	if (repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0, -1) < 0) {
> > +		ret = error_errno(_("unable to lock %s"), dst);
> > +		goto out;
> > +	}
> 
> With a timeout of -1 we'll block indefinitely. That doesn't feel like
> the best option to me, as the lockfile may be left over from a previous
> process. And Git locking up indefinitely feels suboptimal to me.

That's fair. I picked this value purely because I was closely following how `write_one_object()` implements its logic to update the "loose-object-idx" file.

Would it make sense to make this value configurable? Or would it be fine to just set it to 1000ms? If we change this here, maybe we should also change it for `write_one_object()` to match?

Show 8 quoted lines
> > +	fd = open(dst, O_WRONLY | O_CREAT | O_APPEND, 0666);
> 
> I don't think it's a good idea to append data under the lock. It may
> easily cause us to corrupt the file in case the write succeeds
> partially, only.
> 
> Our usual pattern here is to create the lockfile, write all data into
> the new file and then rename it into place.

So here I was also following how `write_one_object()` handles writing its mapping entries. I was trying to keep the `loose_object_idx_merge()` and `write_one_object()` approaches similar. If this really isn't the write approach though, I can explore adapting the patch to follow our more typical pattern.

> I'm also a bit surprised that the file isn't sorted. Is a reader
> expected to always read _all_ mappings?

I was surprised by this too. From my understanding, readers are not expected to consult the "loose-object-idx" file directly, but instead consult an in-memory hash map that is loaded from the file. When an entry is not found in map, the file is reloaded to try to pick up any missing entries.

I see this a preexisting quirk though and am not sure it is something that needs to be addressed as part of this change.

Show 26 quoted lines
> > +	if (fd < 0) {
> > +		ret = error_errno(_("unable to open %s"), dst);
> > +		goto out;
> > +	}
> > +	if (fstat(fd, &st) < 0) {
> > +		ret = error_errno(_("unable to stat %s"), dst);
> > +		goto out;
> > +	}
> > +
> > +	/* Create the loose-object-idx if one does not already exist. */
> > +	if (!st.st_size && write_in_full(fd, loose_object_header,
> > +					 strlen(loose_object_header)) < 0) {
> > +		ret = error_errno(_("failed to write loose object index header"));
> > +		goto out;
> > +	}
> > +
> > +	while (!strbuf_getwholeline(&buf, fp, '\n')) {
> > +		if (write_in_full(fd, buf.buf, buf.len) < 0) {
> > +			ret = error_errno(_("failed to write loose object index %s"),
> > +					  dst);
> > +			goto out;
> > +		}
> > +	}
> 
> When merging, wouldn't we have to verify that the object IDs that we're
> adding don't already exist?

So typically a loose object doesn't get written to the object directory at all if it already exists and thus doesn't create a new mapping entry. There is a race though where concurrent writes of the same object could cause the same entry to be appended twice. This is already an existing problem though as `write_one_object()` also doesn't verify whether the object is already in the file.

With this patch, I mostly avoided many of these preexisting quirks and just focused on addressing the underlying bug. If we feel strongly about it though, I can change the approach.

Thanks, -Justin

Patrick Steinhardt· Oct 26, 2026, 06:39 UTC · re: Justin Tobler · lore

Re: [PATCH] tmp-objdir: merge loose object index when migrating tmpdir

On Fri, Oct 23, 2026 at 04:25:12PM -0500, Justin Tobler wrote:
> On 26/10/23 02:19PM, Patrick Steinhardt wrote:
> > On Thu, Oct 22, 2026 at 06:40:07PM -0500, Justin Tobler wrote:
[snip]
Show 42 quoted lines
> > > diff --git a/loose.c b/loose.c
> > > index d6a2bb5ba3..79d4c256e0 100644
> > > --- a/loose.c
> > > +++ b/loose.c
> > > @@ -265,3 +265,75 @@ void loose_object_map_clear(struct loose_object_map **map)
> > >  	free(m);
> > >  	*map = NULL;
> > >  }
> > > +
> > > +int loose_object_idx_merge(struct repository *repo, const char *src,
> > > +			   const char *dst)
> > > +{
> > > +	struct lock_file lock = LOCK_INIT;
> > > +	struct strbuf buf = STRBUF_INIT;
> > > +	struct stat st;
> > > +	FILE *fp;
> > > +	int fd = -1;
> > > +	int ret;
> > > +
> > > +	fp = fopen(src, "r");
> > > +	if (!fp) {
> > > +		ret = error_errno(_("unable to open %s"), src);
> > > +		goto out;
> > > +	}
> > > +	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header)) {
> > > +		ret = error(_("invalid loose object index %s"), src);
> > > +		goto out;
> > > +	}
> > 
> > Curious. Why do we even expect that this file may be broken?
> 
> For our intended use case of reading the "loose-object-idx" file from
> the quarantine directory and merging its entries to the mapping file in
> the primary object directory, I wouldn't expect it to ever be broken.
> That is because realistically only a "loose-object-idx" that we create
> during the transaction should be written into the quarantine directory.
> 
> I chose to be more defensive here because the `loose_object_idx_merge()`
> function itself isn't aware of the circumstances regarding its use case.
> In theory, another caller could use any mapping file as the source.
> Ultimately though I don't feel too strongly either way and am happy to
> remove it if we don't like it.

Ah. You mean because we're called with arbitrary paths we want to verify whether our callers know what they're doing, and consequently we check whether the files look like a mapping file? Still a bit funny, but with that context it at least doesn't feel too unreasonable.

Show 16 quoted lines
> > > +	if (repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0, -1) < 0) {
> > > +		ret = error_errno(_("unable to lock %s"), dst);
> > > +		goto out;
> > > +	}
> > 
> > With a timeout of -1 we'll block indefinitely. That doesn't feel like
> > the best option to me, as the lockfile may be left over from a previous
> > process. And Git locking up indefinitely feels suboptimal to me.
> 
> That's fair. I picked this value purely because I was closely following
> how `write_one_object()` implements its logic to update the
> "loose-object-idx" file.
> 
> Would it make sense to make this value configurable? Or would it be fine
> to just set it to 1000ms? If we change this here, maybe we should also
> change it for `write_one_object()` to match?
Yeah, I think that'd be a good idea.
Show 14 quoted lines
> > > +	fd = open(dst, O_WRONLY | O_CREAT | O_APPEND, 0666);
> > 
> > I don't think it's a good idea to append data under the lock. It may
> > easily cause us to corrupt the file in case the write succeeds
> > partially, only.
> > 
> > Our usual pattern here is to create the lockfile, write all data into
> > the new file and then rename it into place.
> 
> So here I was also following how `write_one_object()` handles writing
> its mapping entries. I was trying to keep the `loose_object_idx_merge()`
> and `write_one_object()` approaches similar. If this really isn't the
> write approach though, I can explore adapting the patch to follow our
> more typical pattern.

I think this is quite broken. Right now nobody would care because we don't really use the compatibility hash algorithm yet. But once it starts becoming useful this _will_ lead to corrupted files. So I'd prefer to fix it rather than doubling down on the broken pattern.

Show 8 quoted lines
> > I'm also a bit surprised that the file isn't sorted. Is a reader
> > expected to always read _all_ mappings?
> 
> I was surprised by this too. From my understanding, readers are not
> expected to consult the "loose-object-idx" file directly, but instead
> consult an in-memory hash map that is loaded from the file. When an
> entry is not found in map, the file is reloaded to try to pick up any
> missing entries.

Sure. But that'd still require a linear scan through all mappings in here.

> I see this a preexisting quirk though and am not sure it is something
> that needs to be addressed as part of this change.

Yeah, I think it's fine to ignore this for now. This part here is certainly outside the scope of the patch series.

Show 37 quoted lines
> > > +	if (fd < 0) {
> > > +		ret = error_errno(_("unable to open %s"), dst);
> > > +		goto out;
> > > +	}
> > > +	if (fstat(fd, &st) < 0) {
> > > +		ret = error_errno(_("unable to stat %s"), dst);
> > > +		goto out;
> > > +	}
> > > +
> > > +	/* Create the loose-object-idx if one does not already exist. */
> > > +	if (!st.st_size && write_in_full(fd, loose_object_header,
> > > +					 strlen(loose_object_header)) < 0) {
> > > +		ret = error_errno(_("failed to write loose object index header"));
> > > +		goto out;
> > > +	}
> > > +
> > > +	while (!strbuf_getwholeline(&buf, fp, '\n')) {
> > > +		if (write_in_full(fd, buf.buf, buf.len) < 0) {
> > > +			ret = error_errno(_("failed to write loose object index %s"),
> > > +					  dst);
> > > +			goto out;
> > > +		}
> > > +	}
> > 
> > When merging, wouldn't we have to verify that the object IDs that we're
> > adding don't already exist?
> 
> So typically a loose object doesn't get written to the object directory
> at all if it already exists and thus doesn't create a new mapping entry.
> There is a race though where concurrent writes of the same object could
> cause the same entry to be appended twice. This is already an existing
> problem though as `write_one_object()` also doesn't verify whether the
> object is already in the file.
> 
> With this patch, I mostly avoided many of these preexisting quirks and
> just focused on addressing the underlying bug. If we feel strongly about
> it though, I can change the approach.
I feel strongly that this mechanism is quite broken in its current form:
  - The file is unsorted, making lookups inefficient.
  - We don't deduplicate at all.
  - We never prune entries.
  - Writes are not atomic.

The first three are conceptual issues that we'd have to fix to ever make this feature usable. But that certainly doesn't have to happen as part of this patch series.

The last part though shouldn't be too hard to fix, and you make the problem worse because you add a second writer that uses the same broken pattern. So that's a part I'd like to see fixd as part of this series, but I won't insist on it.

Thanks!
Patrick
Justin Tobler· Oct 26, 2026, 17:58 UTC · re: Patrick Steinhardt · lore

Re: [PATCH] tmp-objdir: merge loose object index when migrating tmpdir

On 26/10/26 07:39AM, Patrick Steinhardt wrote:
Show 26 quoted lines
> On Fri, Oct 23, 2026 at 04:25:12PM -0500, Justin Tobler wrote:
> > On 26/10/23 02:19PM, Patrick Steinhardt wrote:
> > > On Thu, Oct 22, 2026 at 06:40:07PM -0500, Justin Tobler wrote:
> > > > +	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header)) {
> > > > +		ret = error(_("invalid loose object index %s"), src);
> > > > +		goto out;
> > > > +	}
> > > 
> > > Curious. Why do we even expect that this file may be broken?
> > 
> > For our intended use case of reading the "loose-object-idx" file from
> > the quarantine directory and merging its entries to the mapping file in
> > the primary object directory, I wouldn't expect it to ever be broken.
> > That is because realistically only a "loose-object-idx" that we create
> > during the transaction should be written into the quarantine directory.
> > 
> > I chose to be more defensive here because the `loose_object_idx_merge()`
> > function itself isn't aware of the circumstances regarding its use case.
> > In theory, another caller could use any mapping file as the source.
> > Ultimately though I don't feel too strongly either way and am happy to
> > remove it if we don't like it.
> 
> Ah. You mean because we're called with arbitrary paths we want to verify
> whether our callers know what they're doing, and consequently we check
> whether the files look like a mapping file? Still a bit funny, but with
> that context it at least doesn't feel too unreasonable.

Your understanding is correct. This may be more defensive that realistically neccessary. Probably doesn't matter too much either way though.

Show 18 quoted lines
> > > > +	if (repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0, -1) < 0) {
> > > > +		ret = error_errno(_("unable to lock %s"), dst);
> > > > +		goto out;
> > > > +	}
> > > 
> > > With a timeout of -1 we'll block indefinitely. That doesn't feel like
> > > the best option to me, as the lockfile may be left over from a previous
> > > process. And Git locking up indefinitely feels suboptimal to me.
> > 
> > That's fair. I picked this value purely because I was closely following
> > how `write_one_object()` implements its logic to update the
> > "loose-object-idx" file.
> > 
> > Would it make sense to make this value configurable? Or would it be fine
> > to just set it to 1000ms? If we change this here, maybe we should also
> > change it for `write_one_object()` to match?
> 
> Yeah, I think that'd be a good idea.
Ok, I'll update to use 1000ms in the next version.
[snip]
Show 23 quoted lines
> > > When merging, wouldn't we have to verify that the object IDs that we're
> > > adding don't already exist?
> > 
> > So typically a loose object doesn't get written to the object directory
> > at all if it already exists and thus doesn't create a new mapping entry.
> > There is a race though where concurrent writes of the same object could
> > cause the same entry to be appended twice. This is already an existing
> > problem though as `write_one_object()` also doesn't verify whether the
> > object is already in the file.
> > 
> > With this patch, I mostly avoided many of these preexisting quirks and
> > just focused on addressing the underlying bug. If we feel strongly about
> > it though, I can change the approach.
> 
> I feel strongly that this mechanism is quite broken in its current form:
> 
>   - The file is unsorted, making lookups inefficient.
> 
>   - We don't deduplicate at all.
> 
>   - We never prune entries.
> 
>   - Writes are not atomic.
Ya, I completely agree. There are quite a few existing problems here.
Show 8 quoted lines
> The first three are conceptual issues that we'd have to fix to ever make
> this feature usable. But that certainly doesn't have to happen as part
> of this patch series.
> 
> The last part though shouldn't be too hard to fix, and you make the
> problem worse because you add a second writer that uses the same broken
> pattern. So that's a part I'd like to see fixd as part of this series,
> but I won't insist on it.

That's fair. I'll update in the next version to make writing the "loose-object-idx" file here atomic. I'm not entirely sure if I should go ahead and update the code in `write_one_object()` to also be atomic though.

One concern I have is operations that may write many loose objects, would rewrite the "loose-object-idx" file for each object written. If the mapping file becomes quite large, I suppose this could create a significant performance regression. If these loose object writes were batched in a transaction, we could wait to write the final mapping file at transaction commit, but that may not always be the case.

Right now this feature isn't really usable to begin with, so maybe it doesn't matter too much either way. For now, I'll proceed with just updating the mapping file writer adding in this patch to be atomic.

-Justin
Justin Tobler· Oct 29, 2026, 00:24 UTC · re: Justin Tobler · lore

[PATCH v2 0/4] loose: fix migrating "loose-object-idx" with batch fsync

Greetings,

While working on a separate patch series to convert the "files" ODB transaction to be eagerly created instead of lazily, I encountered this bug that causes an ODB transaction to fail on commit when a compatibility hash is also configured. This occurs because the temporary directory migration code does not know how to handle the loose object index file.

This patch addresses updating the migration logic to explicitly merge mapping entries in the transaction and atomically write a new "loose-object-idx" file.

Some other approaches I considered:
  - Storing the uncommitted object mapping entries in the transaction
    without writing a file and only writing on transaction commit. I
    chose to keep writing the temp index into the transaction so that
    child processes that write to the tempdir could still append entries.
  - Also writing mapping entires to the main ODB loose object index.
    This also didn't seem like a good apprach because it write entries
    that may not ultimately get committed.
Changes since V1:
  - The loose object index write after the merging maps is now atomic.
  - Added a fixed timeout to all loose object index writers.
  - Duplicate entries are now dropped when writing the merged mapping file.

Thanks for the review, -Justin

Justin Tobler (4):
  loose: use finite timeout when locking "loose-object-idx" file
  loose: lift loose object index parsing logic into helper
  loose: lift loose object index writing logic into helper
  tmp-objdir: merge loose object index when migrating tmpdir
 loose.c                       | 207 ++++++++++++++++++++++++----------
 loose.h                       |   2 +
 t/t1016-compatObjectFormat.sh |  27 +++++
 tmp-objdir.c                  |  11 +-
 4 files changed, 189 insertions(+), 58 deletions(-)
Range-diff against v1:
-:  ---------- > 1:  6f104c482e loose: use finite timeout when locking "loose-object-idx" file
-:  ---------- > 2:  4799421a74 loose: lift loose object index parsing logic into helper
-:  ---------- > 3:  4233e913ce loose: lift loose object index writing logic into helper
1:  34393e789c ! 4:  614aab27d1 tmp-objdir: merge loose object index when migrating tmpdir
    @@ Commit message
     
         When a repository is configured with a compatibility hash, each loose
         object write also records a mapping between the object and its
    -    compatibility hash in a "loose-object-idx" file. This index is located
    -    in the same object database the object is written to. If batch fsync of
    -    loose objects is also enabled, the loose objects and index file are
    -    instead written to a temporary object directory and expected to be
    -    migrated to the primary object directory when the batch transaction
    +    compatibility hash in a "loose-object-idx" file. This mapping file is
    +    located in the same object directory the object is written to. If batch
    +    fsync of loose objects is also enabled, the loose objects and mapping
    +    file are instead written to a temporary object directory and expected to
    +    be migrated to the primary object directory when the batch transaction
         commits.
     
         While migrating the loose objects works, the "loose-object-idx" file is
    -    unable to be migrated if an index already exists in the primary object
    -    directory. This occurs because `finalize_object_file_flags()` expects an
    -    already existing file to have the same contents. Otherwise, the
    -    following error is returned:
    +    unable to be migrated if a mapping file already exists in the primary
    +    object directory. This occurs because `finalize_object_file_flags()`
    +    expects an already existing file to have the same contents. Otherwise,
    +    the following error is returned:
     
           error: files '.git/objects/tmp_objdir-bulk-fsync-XXXXXX/loose-object-idx' and '.git/objects/loose-object-idx' differ in contents
           error: unable to migrate temporary objects
     
    -    Furthermore, it is not sufficient to just replace the preexisting index
    -    with the new one as the new index only contains mapping entries for the
    -    newly written objects.
    +    Furthermore, it is not sufficient to just replace the preexisting
    +    mapping file with the new one as the new file only contains mapping
    +    entries for the newly written objects.
     
         Update the temporary directory migration logic to handle
    -    "loose-object-idx" files explicitly by appending new mapping entries to
    -    the primary index.
    +    "loose-object-idx" files explicitly by merging new entries and
    +    atomically writing the resulting mapping file.
     
         Signed-off-by: Justin Tobler <jltobler@gmail.com>
     
    @@ loose.c: void loose_object_map_clear(struct loose_object_map **map)
     +			   const char *dst)
     +{
     +	struct lock_file lock = LOCK_INIT;
    -+	struct strbuf buf = STRBUF_INIT;
    -+	struct stat st;
    -+	FILE *fp;
    -+	int fd = -1;
    ++	struct loose_object_map *map;
     +	int ret;
    ++	int fd;
     +
    -+	fp = fopen(src, "r");
    -+	if (!fp) {
    -+		ret = error_errno(_("unable to open %s"), src);
    -+		goto out;
    -+	}
    -+	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header)) {
    -+		ret = error(_("invalid loose object index %s"), src);
    -+		goto out;
    -+	}
    ++	loose_object_map_init(&map);
     +
    -+	if (repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0, -1) < 0) {
    ++	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0,
    ++						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
    ++	if (fd < 0) {
     +		ret = error_errno(_("unable to lock %s"), dst);
     +		goto out;
     +	}
     +
    -+	fd = open(dst, O_WRONLY | O_CREAT | O_APPEND, 0666);
    -+	if (fd < 0) {
    -+		ret = error_errno(_("unable to open %s"), dst);
    -+		goto out;
    -+	}
    -+	if (fstat(fd, &st) < 0) {
    -+		ret = error_errno(_("unable to stat %s"), dst);
    ++	if (loose_object_map_read(repo, dst, map, NULL) < 0) {
    ++		ret = error(_("unable to read loose object index %s"), dst);
     +		goto out;
     +	}
     +
    -+	/* Create the loose-object-idx if one does not already exist. */
    -+	if (!st.st_size && write_in_full(fd, loose_object_header,
    -+					 strlen(loose_object_header)) < 0) {
    -+		ret = error_errno(_("failed to write loose object index header"));
    ++	if (loose_object_map_read(repo, src, map, NULL) < 0) {
    ++		ret = error(_("unable to read loose object index %s"), src);
     +		goto out;
     +	}
     +
    -+	while (!strbuf_getwholeline(&buf, fp, '\n')) {
    -+		if (write_in_full(fd, buf.buf, buf.len) < 0) {
    -+			ret = error_errno(_("failed to write loose object index %s"),
    -+					  dst);
    -+			goto out;
    -+		}
    ++	if (loose_object_map_write(repo, map, fd) < 0) {
    ++		ret = error_errno(_("unable to write %s"),
    ++				  get_lock_file_path(&lock));
    ++		goto out;
     +	}
     +
    -+	ret = close(fd);
    -+	fd = -1;
    -+	if (ret) {
    -+		ret = error_errno(_("unable to close %s"), dst);
    ++	if (commit_lock_file(&lock) < 0) {
    ++		ret = error_errno(_("unable to write %s"), dst);
     +		goto out;
     +	}
     +
    -+	adjust_shared_perm(repo, dst);
    -+
     +	ret = 0;
     +
     +out:
    ++	loose_object_map_clear(&map);
     +	rollback_lock_file(&lock);
    -+	strbuf_release(&buf);
    -+	if (fd >= 0)
    -+		close(fd);
    -+	if (fp)
    -+		fclose(fp);
     +
     +	return ret;
     +}
    @@ t/t1016-compatObjectFormat.sh: compare_oids GPG2 'commit' signedcommit4 "$signed
     +
     +		# Write some additional objects and ensure the corresponding
     +		# temporary loose object index entries stored in the transaction
    -+		# are merged into the primary ODB.
    ++		# are merged into the primary object directory.
     +		echo bar >bar &&
     +		echo baz >baz &&
     +		git add bar baz &&
base-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e
-- 
2.56.0.173.gf57c9a7388
Justin Tobler· Oct 29, 2026, 00:24 UTC · re: Justin Tobler · lore

[PATCH v2 1/4] loose: use finite timeout when locking "loose-object-idx" file

Loose object index file writers first attempt to acquire a lock via `repo_hold_lock_file_for_update_timeout()` and specify a timeout value of -1. With a negative timeout value, the lock acquisition retries indefinitely while the lockfile still exists. If a process dies while holding the lock and leaves a stale lockfile behind, every subsequent writer of the mapping file hangs forever instead of reporting an error.

Introduce LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS to set the timeout to 1000ms and use it for all callers that lock a "loose-object-idx" file. A value of 1000ms matches the default timeout used for "core.packedRefsTimeout" and should provide concurrent writers sufficient time to acquire the lock.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 loose.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Show changes to loose.c +6 −2
diff --git a/loose.c b/loose.c
index d6a2bb5ba3..c6f4ee0ec6 100644
--- a/loose.c
+++ b/loose.c
@@ -14,6 +14,8 @@
 
 static const char *loose_object_header = "# loose-object-idx\n";
 
+#define LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS 1000
+
 static inline int should_use_loose_object_map(struct repository *repo)
 {
 	return repo->compat_hash_algo && repo->gitdir;
@@ -137,7 +139,8 @@ int repo_write_loose_object_map(struct repository *repo)
 
 	repo_common_path_replace(repo, &path, "objects/loose-object-idx");
 	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, path.buf,
-						    LOCK_DIE_ON_ERROR, -1);
+						    LOCK_DIE_ON_ERROR,
+						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
 	iter = kh_begin(map);
 	if (write_in_full(fd, loose_object_header, strlen(loose_object_header)) < 0)
 		goto errout;
@@ -180,7 +183,8 @@ static int write_one_object(struct odb_source_loose *loose,
 
 	strbuf_addf(&path, "%s/loose-object-idx", loose->base.path);
 	repo_hold_lock_file_for_update_timeout(loose->base.odb->repo, &lock,
-					       path.buf, LOCK_DIE_ON_ERROR, -1);
+					       path.buf, LOCK_DIE_ON_ERROR,
+					       LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
 
 	fd = open(path.buf, O_WRONLY | O_CREAT | O_APPEND, 0666);
 	if (fd < 0)
-- 
2.56.0.173.gf57c9a7388
Patrick Steinhardt· Oct 29, 2026, 08:05 UTC · re: Justin Tobler · lore

Re: [PATCH v2 1/4] loose: use finite timeout when locking "loose-object-idx" file

On Wed, Oct 28, 2026 at 07:24:10PM -0500, Justin Tobler wrote:
Show 12 quoted lines
> Loose object index file writers first attempt to acquire a lock via
> `repo_hold_lock_file_for_update_timeout()` and specify a timeout value
> of -1. With a negative timeout value, the lock acquisition retries
> indefinitely while the lockfile still exists. If a process dies while
> holding the lock and leaves a stale lockfile behind, every subsequent
> writer of the mapping file hangs forever instead of reporting an error.
> 
> Introduce LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS to set the timeout to 1000ms
> and use it for all callers that lock a "loose-object-idx" file. A value
> of 1000ms matches the default timeout used for "core.packedRefsTimeout"
> and should provide concurrent writers sufficient time to acquire the
> lock.

Right, aborting eventually is way better compared to blocking indefinitely. Ideally we should also make this setting configurable. But I think it's fine to skip that for now and fix that at a later point in time.

Patrick
Justin Tobler· Oct 29, 2026, 00:24 UTC · re: Justin Tobler · lore

[PATCH v2 2/4] loose: lift loose object index parsing logic into helper

Mapping entries from the "loose-object-idx" file are parsed via `loose_object_map_load()` and inserted into the in-memory loose object map of the corresponding object source. In a subsequent commit, a temporary loose object index file must also be parsed and have its contents stored in a map that is not attached to any object source.

Extract the logic for parsing a loose object index file into a separate `loose_object_map_read()` helper function that allows callers to explicitly specify the map to insert the entries into. The `insert_loose_map()` helper function is also updated accordingly to explicitly operate on the provided map and optional cache instead of the object source.

While at it, stop treating all errors related to opening the "loose-object-idx" file as success and instead only ignore ENOENT. It is expected that a mapping file may not always be present, but other errors should be signaled back to callers. Note that this slightly changes the behavior of `repo_read_loose_object_map()`, which now stops loading the mapping files of subsequent object sources when one is unreadable, the same way it already does when one is malformed.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 loose.c | 87 +++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 54 insertions(+), 33 deletions(-)
Show changes to loose.c +54 −33
diff --git a/loose.c b/loose.c
index c6f4ee0ec6..5f72c644f9 100644
--- a/loose.c
+++ b/loose.c
@@ -48,67 +48,88 @@ static int insert_oid_pair(kh_oid_map_t *map, const struct object_id *key, const
 	return 1;
 }
 
-static int insert_loose_map(struct odb_source_loose *loose,
+static int insert_loose_map(struct loose_object_map *map,
+			    struct oidtree *cache,
 			    const struct object_id *oid,
 			    const struct object_id *compat_oid)
 {
-	struct loose_object_map *map = loose->map;
 	int inserted = 0;
 
 	inserted |= insert_oid_pair(map->to_compat, oid, compat_oid);
 	inserted |= insert_oid_pair(map->to_storage, compat_oid, oid);
-	if (inserted)
-		oidtree_insert(loose->cache, compat_oid, NULL);
+	if (inserted && cache)
+		oidtree_insert(cache, compat_oid, NULL);
 
 	return inserted;
 }
 
-int loose_object_map_load(struct odb_source_loose *loose)
+static int loose_object_map_read(struct repository *repo, const char *path,
+				 struct loose_object_map *map,
+				 struct oidtree *cache)
 {
-	struct repository *repo = loose->base.odb->repo;
 	struct strbuf buf = STRBUF_INIT;
-	char *path;
 	FILE *fp;
-	int ret = -1;
-
-	if (!should_use_loose_object_map(repo))
-		return 0;
-
-	if (!loose->map)
-		loose_object_map_init(&loose->map);
-	if (!loose->cache) {
-		ALLOC_ARRAY(loose->cache, 1);
-		oidtree_init(loose->cache);
-	}
-
-	insert_loose_map(loose, repo->hash_algo->empty_tree, repo->compat_hash_algo->empty_tree);
-	insert_loose_map(loose, repo->hash_algo->empty_blob, repo->compat_hash_algo->empty_blob);
-	insert_loose_map(loose, repo->hash_algo->null_oid, repo->compat_hash_algo->null_oid);
+	int ret;
 
-	path = xstrfmt("%s/loose-object-idx", loose->base.path);
 	fp = fopen(path, "rb");
 	if (!fp) {
-		free(path);
-		return 0;
+		if (errno == ENOENT)
+			return 0;
+		return -1;
 	}
 
-	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header))
-		goto err;
+	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header)) {
+		ret = -1;
+		goto out;
+	}
 	while (!strbuf_getline_lf(&buf, fp)) {
 		const char *p;
 		struct object_id oid, compat_oid;
 		if (parse_oid_hex_algop(buf.buf, &oid, &p, repo->hash_algo) ||
 		    *p++ != ' ' ||
 		    parse_oid_hex_algop(p, &compat_oid, &p, repo->compat_hash_algo) ||
-		    p != buf.buf + buf.len)
-			goto err;
-		insert_loose_map(loose, &oid, &compat_oid);
+		    p != buf.buf + buf.len) {
+			ret = -1;
+			goto out;
+		}
+		insert_loose_map(map, cache, &oid, &compat_oid);
 	}
 
 	ret = ferror(fp) ? -1 : 0;
-err:
-	fclose(fp);
+
+out:
 	strbuf_release(&buf);
+	fclose(fp);
+
+	return ret;
+}
+
+int loose_object_map_load(struct odb_source_loose *loose)
+{
+	struct repository *repo = loose->base.odb->repo;
+	char *path;
+	int ret;
+
+	if (!should_use_loose_object_map(repo))
+		return 0;
+
+	if (!loose->map)
+		loose_object_map_init(&loose->map);
+	if (!loose->cache) {
+		ALLOC_ARRAY(loose->cache, 1);
+		oidtree_init(loose->cache);
+	}
+
+	insert_loose_map(loose->map, loose->cache, repo->hash_algo->empty_tree,
+			 repo->compat_hash_algo->empty_tree);
+	insert_loose_map(loose->map, loose->cache, repo->hash_algo->empty_blob,
+			 repo->compat_hash_algo->empty_blob);
+	insert_loose_map(loose->map, loose->cache, repo->hash_algo->null_oid,
+			 repo->compat_hash_algo->null_oid);
+
+	path = xstrfmt("%s/loose-object-idx", loose->base.path);
+	ret = loose_object_map_read(repo, path, loose->map, loose->cache);
+
 	free(path);
 	return ret;
 }
@@ -223,7 +244,7 @@ int repo_add_loose_object_map(struct odb_source_loose *loose,
 	if (!should_use_loose_object_map(loose->base.odb->repo))
 		return 0;
 
-	inserted = insert_loose_map(loose, oid, compat_oid);
+	inserted = insert_loose_map(loose->map, loose->cache, oid, compat_oid);
 	if (inserted)
 		return write_one_object(loose, oid, compat_oid);
 	return 0;
-- 
2.56.0.173.gf57c9a7388
Patrick Steinhardt· Oct 29, 2026, 08:05 UTC · re: Justin Tobler · lore

Re: [PATCH v2 2/4] loose: lift loose object index parsing logic into helper

On Wed, Oct 28, 2026 at 07:24:11PM -0500, Justin Tobler wrote:
Show 23 quoted lines
> diff --git a/loose.c b/loose.c
> index c6f4ee0ec6..5f72c644f9 100644
> --- a/loose.c
> +++ b/loose.c
> @@ -48,67 +48,88 @@ static int insert_oid_pair(kh_oid_map_t *map, const struct object_id *key, const
>  	return 1;
>  }
>  
> -static int insert_loose_map(struct odb_source_loose *loose,
> +static int insert_loose_map(struct loose_object_map *map,
> +			    struct oidtree *cache,
>  			    const struct object_id *oid,
>  			    const struct object_id *compat_oid)
>  {
> -	struct loose_object_map *map = loose->map;
>  	int inserted = 0;
>  
>  	inserted |= insert_oid_pair(map->to_compat, oid, compat_oid);
>  	inserted |= insert_oid_pair(map->to_storage, compat_oid, oid);
> -	if (inserted)
> -		oidtree_insert(loose->cache, compat_oid, NULL);
> +	if (inserted && cache)
> +		oidtree_insert(cache, compat_oid, NULL);

Okay, so the `cache` is optional now. You mention that fact in the commit message, but you don't explain why that is. I assume a subsequent commit will make use of this.

Show 27 quoted lines
>  	return inserted;
>  }
>  
> -int loose_object_map_load(struct odb_source_loose *loose)
> +static int loose_object_map_read(struct repository *repo, const char *path,
> +				 struct loose_object_map *map,
> +				 struct oidtree *cache)
>  {
> -	struct repository *repo = loose->base.odb->repo;
>  	struct strbuf buf = STRBUF_INIT;
> -	char *path;
>  	FILE *fp;
> -	int ret = -1;
> -
> -	if (!should_use_loose_object_map(repo))
> -		return 0;
> -
> -	if (!loose->map)
> -		loose_object_map_init(&loose->map);
> -	if (!loose->cache) {
> -		ALLOC_ARRAY(loose->cache, 1);
> -		oidtree_init(loose->cache);
> -	}
> -
> -	insert_loose_map(loose, repo->hash_algo->empty_tree, repo->compat_hash_algo->empty_tree);
> -	insert_loose_map(loose, repo->hash_algo->empty_blob, repo->compat_hash_algo->empty_blob);
> -	insert_loose_map(loose, repo->hash_algo->null_oid, repo->compat_hash_algo->null_oid);
Okay, all of this stays in `loose_object_map_load()` further down.
Show 18 quoted lines
> +	int ret;
>  
> -	path = xstrfmt("%s/loose-object-idx", loose->base.path);
>  	fp = fopen(path, "rb");
>  	if (!fp) {
> -		free(path);
> -		return 0;
> +		if (errno == ENOENT)
> +			return 0;
> +		return -1;
>  	}
>  
> -	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header))
> -		goto err;
> +	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header)) {
> +		ret = -1;
> +		goto out;
> +	}

Ideally we'd of course print an error message telling the user what's happened. But we didn't before either, so this is fine as-is.

Show 56 quoted lines
>  	while (!strbuf_getline_lf(&buf, fp)) {
>  		const char *p;
>  		struct object_id oid, compat_oid;
>  		if (parse_oid_hex_algop(buf.buf, &oid, &p, repo->hash_algo) ||
>  		    *p++ != ' ' ||
>  		    parse_oid_hex_algop(p, &compat_oid, &p, repo->compat_hash_algo) ||
> -		    p != buf.buf + buf.len)
> -			goto err;
> -		insert_loose_map(loose, &oid, &compat_oid);
> +		    p != buf.buf + buf.len) {
> +			ret = -1;
> +			goto out;
> +		}
> +		insert_loose_map(map, cache, &oid, &compat_oid);
>  	}
>  
>  	ret = ferror(fp) ? -1 : 0;
> -err:
> -	fclose(fp);
> +
> +out:
>  	strbuf_release(&buf);
> +	fclose(fp);
> +
> +	return ret;
> +}
> +
> +int loose_object_map_load(struct odb_source_loose *loose)
> +{
> +	struct repository *repo = loose->base.odb->repo;
> +	char *path;
> +	int ret;
> +
> +	if (!should_use_loose_object_map(repo))
> +		return 0;
> +
> +	if (!loose->map)
> +		loose_object_map_init(&loose->map);
> +	if (!loose->cache) {
> +		ALLOC_ARRAY(loose->cache, 1);
> +		oidtree_init(loose->cache);
> +	}
> +
> +	insert_loose_map(loose->map, loose->cache, repo->hash_algo->empty_tree,
> +			 repo->compat_hash_algo->empty_tree);
> +	insert_loose_map(loose->map, loose->cache, repo->hash_algo->empty_blob,
> +			 repo->compat_hash_algo->empty_blob);
> +	insert_loose_map(loose->map, loose->cache, repo->hash_algo->null_oid,
> +			 repo->compat_hash_algo->null_oid);
> +
> +	path = xstrfmt("%s/loose-object-idx", loose->base.path);
> +	ret = loose_object_map_read(repo, path, loose->map, loose->cache);
> +
>  	free(path);
>  	return ret;
>  }

Right, and this here is the original function that we had with the remaining bits that are specific to the source.

Patrick
Justin Tobler· Oct 29, 2026, 21:10 UTC · re: Patrick Steinhardt · lore

Re: [PATCH v2 2/4] loose: lift loose object index parsing logic into helper

On 26/10/29 09:05AM, Patrick Steinhardt wrote:
Show 9 quoted lines
> On Wed, Oct 28, 2026 at 07:24:11PM -0500, Justin Tobler wrote:
> > -	if (inserted)
> > -		oidtree_insert(loose->cache, compat_oid, NULL);
> > +	if (inserted && cache)
> > +		oidtree_insert(cache, compat_oid, NULL);
> 
> Okay, so the `cache` is optional now. You mention that fact in the
> commit message, but you don't explain why that is. I assume a subsequent
> commit will make use of this.

Yes, this is used in the final patch. I'll update the commit message to make this a bit more clear.

-Justin
Justin Tobler· Oct 29, 2026, 00:24 UTC · re: Justin Tobler · lore

[PATCH v2 3/4] loose: lift loose object index writing logic into helper

Writing the in-memory loose object map to a "loose-object-idx" file is handled via `repo_write_loose_object_map()`, which always writes the map into the primary object directory. In a subsequent commit, a separate "loose-object-idx" file is staged prior to commit and must be constructed from a loose object map not attached to the primary loose object source.

Extract the logic for writing a loose object index file into a separate `loose_object_map_write()` helper function that allows callers to explicitly specify both the loose object map as well as the file descriptor to write the entries to.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 loose.c | 69 +++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 45 insertions(+), 24 deletions(-)
Show changes to loose.c +45 −24
diff --git a/loose.c b/loose.c
index 5f72c644f9..f2e111c25e 100644
--- a/loose.c
+++ b/loose.c
@@ -146,25 +146,17 @@ int repo_read_loose_object_map(struct repository *repo)
 	return 0;
 }
 
-int repo_write_loose_object_map(struct repository *repo)
+static int loose_object_map_write(struct repository *repo,
+				  struct loose_object_map *loose_map, int fd)
 {
-	struct odb_source_files *files = odb_source_files_downcast(repo->objects->source);
-	kh_oid_map_t *map = files->dirs->loose->map->to_compat;
-	struct lock_file lock;
-	int fd;
+	kh_oid_map_t *map = loose_map->to_compat;
+	struct strbuf buf = STRBUF_INIT;
 	khiter_t iter;
-	struct strbuf buf = STRBUF_INIT, path = STRBUF_INIT;
-
-	if (!should_use_loose_object_map(repo))
-		return 0;
+	int ret = -1;
 
-	repo_common_path_replace(repo, &path, "objects/loose-object-idx");
-	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, path.buf,
-						    LOCK_DIE_ON_ERROR,
-						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
 	iter = kh_begin(map);
 	if (write_in_full(fd, loose_object_header, strlen(loose_object_header)) < 0)
-		goto errout;
+		goto out;
 
 	for (; iter != kh_end(map); iter++) {
 		if (kh_exist(map, iter)) {
@@ -173,24 +165,53 @@ int repo_write_loose_object_map(struct repository *repo)
 				continue;
 			strbuf_addf(&buf, "%s %s\n", oid_to_hex(&kh_key(map, iter)), oid_to_hex(kh_value(map, iter)));
 			if (write_in_full(fd, buf.buf, buf.len) < 0)
-				goto errout;
+				goto out;
 			strbuf_reset(&buf);
 		}
 	}
+
+	ret = 0;
+
+out:
 	strbuf_release(&buf);
+
+	return ret;
+}
+
+int repo_write_loose_object_map(struct repository *repo)
+{
+	struct odb_source_files *files = odb_source_files_downcast(repo->objects->source);
+	struct strbuf path = STRBUF_INIT;
+	struct lock_file lock;
+	int ret;
+	int fd;
+
+	if (!should_use_loose_object_map(repo))
+		return 0;
+
+	repo_common_path_replace(repo, &path, "objects/loose-object-idx");
+	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, path.buf,
+						    LOCK_DIE_ON_ERROR,
+						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
+
+	if (loose_object_map_write(repo, files->dirs->loose->map, fd) < 0) {
+		ret = error_errno(_("unable to write %s"),
+				  get_lock_file_path(&lock));
+		goto out;
+	}
+
 	if (commit_lock_file(&lock) < 0) {
-		error_errno(_("could not write loose object index %s"), path.buf);
-		strbuf_release(&path);
-		return -1;
+		ret = error_errno(_("unable to write %s"), path.buf);
+		goto out;
 	}
-	strbuf_release(&path);
-	return 0;
-errout:
+
+	ret = 0;
+
+out:
 	rollback_lock_file(&lock);
-	strbuf_release(&buf);
-	error_errno(_("failed to write loose object index %s"), path.buf);
 	strbuf_release(&path);
-	return -1;
+
+	return ret;
 }
 
 static int write_one_object(struct odb_source_loose *loose,
-- 
2.56.0.173.gf57c9a7388
Patrick Steinhardt· Oct 29, 2026, 08:05 UTC · re: Justin Tobler · lore

Re: [PATCH v2 3/4] loose: lift loose object index writing logic into helper

On Wed, Oct 28, 2026 at 07:24:12PM -0500, Justin Tobler wrote:
Show 11 quoted lines
> Writing the in-memory loose object map to a "loose-object-idx" file is
> handled via `repo_write_loose_object_map()`, which always writes the map
> into the primary object directory. In a subsequent commit, a separate
> "loose-object-idx" file is staged prior to commit and must be
> constructed from a loose object map not attached to the primary loose
> object source.
> 
> Extract the logic for writing a loose object index file into a separate
> `loose_object_map_write()` helper function that allows callers to
> explicitly specify both the loose object map as well as the file
> descriptor to write the entries to.

Okay. This commit is essentially the equivalent to the preceding one, but for writes.

Show 11 quoted lines
> diff --git a/loose.c b/loose.c
> index 5f72c644f9..f2e111c25e 100644
> --- a/loose.c
> +++ b/loose.c
> @@ -146,25 +146,17 @@ int repo_read_loose_object_map(struct repository *repo)
>  	return 0;
>  }
>  
> -int repo_write_loose_object_map(struct repository *repo)
> +static int loose_object_map_write(struct repository *repo,
> +				  struct loose_object_map *loose_map, int fd)

Curious that you're using a file descriptor instead of a path as you did in the preceding commit. But I guess that's because of how we first have to lock the file, which we of course don't have to do on the writing side.

I guess we could've also moved locking into this function here to make both interfaces match, but maybe the next commit will give a hint why we don't.

Patrick
Justin Tobler· Oct 29, 2026, 21:18 UTC · re: Patrick Steinhardt · lore

Re: [PATCH v2 3/4] loose: lift loose object index writing logic into helper

On 26/10/29 09:05AM, Patrick Steinhardt wrote:
Show 9 quoted lines
> On Wed, Oct 28, 2026 at 07:24:12PM -0500, Justin Tobler wrote:
> > -int repo_write_loose_object_map(struct repository *repo)
> > +static int loose_object_map_write(struct repository *repo,
> > +				  struct loose_object_map *loose_map, int fd)
> 
> Curious that you're using a file descriptor instead of a path as you did
> in the preceding commit. But I guess that's because of how we first have
> to lock the file, which we of course don't have to do on the writing
> side.

That is correct. When we lock the file, it gives us back the file descriptor back for the lockfile. I decided to use the file descriptor here because it was readily available to pass around.

> I guess we could've also moved locking into this function here to make
> both interfaces match, but maybe the next commit will give a hint why we
> don't.

Yup, this becomes more apparent in the next commit, but we don't want to acquire the lock in the helper, so that callers can lock the file prior to reading any preexisting version to get a consistent view. I may leave a note about this in the next versions commit message.

-Justin
Justin Tobler· Oct 29, 2026, 00:24 UTC · re: Justin Tobler · lore

[PATCH v2 4/4] tmp-objdir: merge loose object index when migrating tmpdir

When a repository is configured with a compatibility hash, each loose object write also records a mapping between the object and its compatibility hash in a "loose-object-idx" file. This mapping file is located in the same object directory the object is written to. If batch fsync of loose objects is also enabled, the loose objects and mapping file are instead written to a temporary object directory and expected to be migrated to the primary object directory when the batch transaction commits.

While migrating the loose objects works, the "loose-object-idx" file is unable to be migrated if a mapping file already exists in the primary object directory. This occurs because `finalize_object_file_flags()` expects an already existing file to have the same contents. Otherwise, the following error is returned:

  error: files '.git/objects/tmp_objdir-bulk-fsync-XXXXXX/loose-object-idx' and '.git/objects/loose-object-idx' differ in contents
  error: unable to migrate temporary objects

Furthermore, it is not sufficient to just replace the preexisting mapping file with the new one as the new file only contains mapping entries for the newly written objects.

Update the temporary directory migration logic to handle "loose-object-idx" files explicitly by merging new entries and atomically writing the resulting mapping file.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 loose.c                       | 47 +++++++++++++++++++++++++++++++++++
 loose.h                       |  2 ++
 t/t1016-compatObjectFormat.sh | 27 ++++++++++++++++++++
 tmp-objdir.c                  | 11 +++++++-
 4 files changed, 86 insertions(+), 1 deletion(-)
Show changes to 4 files +86 −1

loose.c, loose.h, t/t1016-compatObjectFormat.sh, tmp-objdir.c

diff --git a/loose.c b/loose.c
index f2e111c25e..36da63684f 100644
--- a/loose.c
+++ b/loose.c
@@ -311,3 +311,50 @@ void loose_object_map_clear(struct loose_object_map **map)
 	free(m);
 	*map = NULL;
 }
+
+int loose_object_idx_merge(struct repository *repo, const char *src,
+			   const char *dst)
+{
+	struct lock_file lock = LOCK_INIT;
+	struct loose_object_map *map;
+	int ret;
+	int fd;
+
+	loose_object_map_init(&map);
+
+	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0,
+						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
+	if (fd < 0) {
+		ret = error_errno(_("unable to lock %s"), dst);
+		goto out;
+	}
+
+	if (loose_object_map_read(repo, dst, map, NULL) < 0) {
+		ret = error(_("unable to read loose object index %s"), dst);
+		goto out;
+	}
+
+	if (loose_object_map_read(repo, src, map, NULL) < 0) {
+		ret = error(_("unable to read loose object index %s"), src);
+		goto out;
+	}
+
+	if (loose_object_map_write(repo, map, fd) < 0) {
+		ret = error_errno(_("unable to write %s"),
+				  get_lock_file_path(&lock));
+		goto out;
+	}
+
+	if (commit_lock_file(&lock) < 0) {
+		ret = error_errno(_("unable to write %s"), dst);
+		goto out;
+	}
+
+	ret = 0;
+
+out:
+	loose_object_map_clear(&map);
+	rollback_lock_file(&lock);
+
+	return ret;
+}
diff --git a/loose.h b/loose.h
index ed663ac550..35b46f4be5 100644
--- a/loose.h
+++ b/loose.h
@@ -23,5 +23,7 @@ int repo_add_loose_object_map(struct odb_source_loose *loose,
 			      const struct object_id *compat_oid);
 int repo_read_loose_object_map(struct repository *repo);
 int repo_write_loose_object_map(struct repository *repo);
+int loose_object_idx_merge(struct repository *repo, const char *src,
+			   const char *dst);
 
 #endif
diff --git a/t/t1016-compatObjectFormat.sh b/t/t1016-compatObjectFormat.sh
index 9cafcee509..6d0f48226f 100755
--- a/t/t1016-compatObjectFormat.sh
+++ b/t/t1016-compatObjectFormat.sh
@@ -305,4 +305,31 @@ compare_oids GPG2 'commit' signedcommit4 "$signedcommit4_sha1_oid" "$signedcommi
 compare_oids GPG2 'tag' signedtag3 "$signedtag3_sha1_oid" "$signedtag3_sha256_oid"
 compare_oids GPG2 'tag' signedtag4 "$signedtag4_sha1_oid" "$signedtag4_sha256_oid"
 
+test_expect_success 'batched fsync migrates loose object index' '
+	test_when_finished "rm -rf batch-fsync" &&
+	git init --object-format=sha1 batch-fsync &&
+	(
+		cd batch-fsync &&
+		git config core.repositoryFormatVersion 1 &&
+		git config extensions.compatObjectFormat sha256 &&
+		git config core.fsync loose-object &&
+		git config core.fsyncMethod batch &&
+
+		# Write object to generate initial loose object index.
+		echo foo >foo &&
+		git add foo &&
+		test_grep "$(git rev-parse :foo)" .git/objects/loose-object-idx &&
+
+		# Write some additional objects and ensure the corresponding
+		# temporary loose object index entries stored in the transaction
+		# are merged into the primary object directory.
+		echo bar >bar &&
+		echo baz >baz &&
+		git add bar baz &&
+		test_grep "$(git rev-parse :foo)" .git/objects/loose-object-idx &&
+		test_grep "$(git rev-parse :bar)" .git/objects/loose-object-idx &&
+		test_grep "$(git rev-parse :baz)" .git/objects/loose-object-idx
+	)
+'
+
 test_done
diff --git a/tmp-objdir.c b/tmp-objdir.c
index 3debb60270..5307717354 100644
--- a/tmp-objdir.c
+++ b/tmp-objdir.c
@@ -4,6 +4,7 @@
 #include "chdir-notify.h"
 #include "dir.h"
 #include "environment.h"
+#include "loose.h"
 #include "object-file.h"
 #include "path.h"
 #include "string-list.h"
@@ -294,6 +295,11 @@ static int is_loose_object_shard(const char *name)
 	return strlen(name) == 2 && isxdigit(name[0]) && isxdigit(name[1]);
 }
 
+static int is_loose_object_idx(const char *name)
+{
+	return !strcmp(name, "loose-object-idx");
+}
+
 static int migrate_paths(struct tmp_objdir *t,
 			 struct strbuf *src, struct strbuf *dst,
 			 enum finalize_object_file_flags flags)
@@ -317,7 +323,10 @@ static int migrate_paths(struct tmp_objdir *t,
 		if (is_loose_object_shard(name))
 			flags_copy |= FOF_SKIP_COLLISION_CHECK;
 
-		ret |= migrate_one(t, src, dst, flags_copy);
+		if (is_loose_object_idx(name))
+			ret |= loose_object_idx_merge(t->repo, src->buf, dst->buf);
+		else
+			ret |= migrate_one(t, src, dst, flags_copy);
 
 		strbuf_setlen(src, src_len);
 		strbuf_setlen(dst, dst_len);
-- 
2.56.0.173.gf57c9a7388
Patrick Steinhardt· Oct 29, 2026, 08:05 UTC · re: Justin Tobler · lore

Re: [PATCH v2 4/4] tmp-objdir: merge loose object index when migrating tmpdir

On Wed, Oct 28, 2026 at 07:24:13PM -0500, Justin Tobler wrote:
Show 25 quoted lines
> diff --git a/loose.c b/loose.c
> index f2e111c25e..36da63684f 100644
> --- a/loose.c
> +++ b/loose.c
> @@ -311,3 +311,50 @@ void loose_object_map_clear(struct loose_object_map **map)
>  	free(m);
>  	*map = NULL;
>  }
> +
> +int loose_object_idx_merge(struct repository *repo, const char *src,
> +			   const char *dst)
> +{
> +	struct lock_file lock = LOCK_INIT;
> +	struct loose_object_map *map;
> +	int ret;
> +	int fd;
> +
> +	loose_object_map_init(&map);
> +
> +	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0,
> +						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
> +	if (fd < 0) {
> +		ret = error_errno(_("unable to lock %s"), dst);
> +		goto out;
> +	}

Okay, so this explains why you didn't want to pass a path to `loose_object_map_write()`: we need the lock to span across multiple other operations. Makes sense.

Show 9 quoted lines
> +	if (loose_object_map_read(repo, dst, map, NULL) < 0) {
> +		ret = error(_("unable to read loose object index %s"), dst);
> +		goto out;
> +	}
> +
> +	if (loose_object_map_read(repo, src, map, NULL) < 0) {
> +		ret = error(_("unable to read loose object index %s"), src);
> +		goto out;
> +	}

It makes sense that we read `dst` under the lock so that we don't risk losing data that a concurrent process writes into it. But to minimize the critical section, can't we move `src` out of it and read it before we lock?

Show 10 quoted lines
> diff --git a/loose.h b/loose.h
> index ed663ac550..35b46f4be5 100644
> --- a/loose.h
> +++ b/loose.h
> @@ -23,5 +23,7 @@ int repo_add_loose_object_map(struct odb_source_loose *loose,
>  			      const struct object_id *compat_oid);
>  int repo_read_loose_object_map(struct repository *repo);
>  int repo_write_loose_object_map(struct repository *repo);
> +int loose_object_idx_merge(struct repository *repo, const char *src,
> +			   const char *dst);
Do we maybe want to add a comment to this new function?
Thanks!
Patrick
Justin Tobler· Oct 29, 2026, 21:20 UTC · re: Patrick Steinhardt · lore

Re: [PATCH v2 4/4] tmp-objdir: merge loose object index when migrating tmpdir

On 26/10/29 09:05AM, Patrick Steinhardt wrote:
Show 15 quoted lines
> On Wed, Oct 28, 2026 at 07:24:13PM -0500, Justin Tobler wrote:
> > +	if (loose_object_map_read(repo, dst, map, NULL) < 0) {
> > +		ret = error(_("unable to read loose object index %s"), dst);
> > +		goto out;
> > +	}
> > +
> > +	if (loose_object_map_read(repo, src, map, NULL) < 0) {
> > +		ret = error(_("unable to read loose object index %s"), src);
> > +		goto out;
> > +	}
> 
> It makes sense that we read `dst` under the lock so that we don't risk
> losing data that a concurrent process writes into it. But to minimize
> the critical section, can't we move `src` out of it and read it before
> we lock?

Ya good point. The src file can be read prior to acquiring the lock. Will update.

Show 12 quoted lines
> > diff --git a/loose.h b/loose.h
> > index ed663ac550..35b46f4be5 100644
> > --- a/loose.h
> > +++ b/loose.h
> > @@ -23,5 +23,7 @@ int repo_add_loose_object_map(struct odb_source_loose *loose,
> >  			      const struct object_id *compat_oid);
> >  int repo_read_loose_object_map(struct repository *repo);
> >  int repo_write_loose_object_map(struct repository *repo);
> > +int loose_object_idx_merge(struct repository *repo, const char *src,
> > +			   const char *dst);
> 
> Do we maybe want to add a comment to this new function?
Will do!

Thanks, -Justin

Junio C Hamano· Oct 29, 2026, 22:22 UTC · re: Justin Tobler · lore

Re: [PATCH v2 0/4] loose: fix migrating "loose-object-idx" with batch fsync

Justin Tobler <jltobler@gmail.com> writes:
Show 29 quoted lines
> Greetings,
>
> While working on a separate patch series to convert the "files" ODB
> transaction to be eagerly created instead of lazily, I encountered this
> bug that causes an ODB transaction to fail on commit when a
> compatibility hash is also configured. This occurs because the temporary
> directory migration code does not know how to handle the loose object
> index file.
>
> This patch addresses updating the migration logic to explicitly merge
> mapping entries in the transaction and atomically write a new
> "loose-object-idx" file.
>
> Some other approaches I considered:
>   - Storing the uncommitted object mapping entries in the transaction
>     without writing a file and only writing on transaction commit. I
>     chose to keep writing the temp index into the transaction so that
>     child processes that write to the tempdir could still append entries.
>   - Also writing mapping entires to the main ODB loose object index.
>     This also didn't seem like a good apprach because it write entries
>     that may not ultimately get committed.
>
> Changes since V1:
>   - The loose object index write after the merging maps is now atomic.
>   - Added a fixed timeout to all loose object index writers.
>   - Duplicate entries are now dropped when writing the merged mapping file.
>
> Thanks for the review,
> -Justin
Thanks.

Merging this topic to 'seen' had a funny interaction with other topics in flight. repo_write_loose_object_map() function, from which loose_object_map_write() helper function was extracted out of, loses all callers, and will disappear in the merge. But the new helper, which gained a caller loose_object_idx_merge() in this series, remains.

Justin Tobler· Oct 29, 2026, 22:45 UTC · re: Junio C Hamano · lore

Re: [PATCH v2 0/4] loose: fix migrating "loose-object-idx" with batch fsync

On 26/10/29 03:22PM, Junio C Hamano wrote:
Show 6 quoted lines
> Merging this topic to 'seen' had a funny interaction with other
> topics in flight.  repo_write_loose_object_map() function, from
> which loose_object_map_write() helper function was extracted out of,
> loses all callers, and will disappear in the merge.  But the new
> helper, which gained a caller loose_object_idx_merge() in this
> series, remains.

Thanks for the heads up. I see the function gets dropped by de2910a42b (loose: drop unused `repo_write_loose_object_map()`, 2026-10-27). Keeping the new helper and dropping `repo_write_loose_object_map()` is indeed the correct resolution.

It does look like in `seen` the declaration of `repo_write_loose_object_map()` is still left behind in "loose.h" though.

I have a few small changes queued up locally for a v3, which I will send out soon.

Thanks, -Justin

Justin Tobler· Oct 29, 2026, 23:22 UTC · re: Justin Tobler · lore

[PATCH v3 0/4] loose: fix migrating "loose-object-idx" with batch fsync

Greetings,

While working on a separate patch series to convert the "files" ODB transaction to be eagerly created instead of lazily, I encountered this bug that causes an ODB transaction to fail on commit when a compatibility hash is also configured. This occurs because the temporary directory migration code does not know how to handle the loose object index file.

This patch addresses updating the migration logic to explicitly merge mapping entries in the transaction and atomically write a new "loose-object-idx" file.

Some other approaches I considered:
  - Storing the uncommitted object mapping entries in the transaction
    without writing a file and only writing on transaction commit. I
    chose to keep writing the temp index into the transaction so that
    child processes that write to the tempdir could still append entries.
  - Also writing mapping entires to the main ODB loose object index.
    This also didn't seem like a good apprach because it write entries
    that may not ultimately get committed.
Changes since V2:
  - Reading the src mapping file is now done prior to acquiring the lock
    to reduce the critical window.
  - Added a comment to `loose_object_idx_merge()` and updated some
    commit messages to improve clarity.
Changes since V1:
  - The loose object index write after the merging maps is now atomic.
  - Added a fixed timeout to all loose object index writers.
  - Duplicate entries are now dropped when writing the merged mapping file.

Thanks for the review, -Justin

Justin Tobler (4):
  loose: use finite timeout when locking "loose-object-idx" file
  loose: lift loose object index parsing logic into helper
  loose: lift loose object index writing logic into helper
  tmp-objdir: merge loose object index when migrating tmpdir
 loose.c                       | 207 ++++++++++++++++++++++++----------
 loose.h                       |   9 ++
 t/t1016-compatObjectFormat.sh |  27 +++++
 tmp-objdir.c                  |  11 +-
 4 files changed, 196 insertions(+), 58 deletions(-)
Range-diff against v2:
1:  6f104c482e = 1:  6f104c482e loose: use finite timeout when locking "loose-object-idx" file
2:  4799421a74 ! 2:  3ae068f364 loose: lift loose object index parsing logic into helper
    @@ Commit message
         explicitly specify the map to insert the entries into. The
         `insert_loose_map()` helper function is also updated accordingly to
         explicitly operate on the provided map and optional cache instead of the
    -    object source.
    +    object source. The cache is made optional for future callers that may
    +    only be interested in updating the map.
     
         While at it, stop treating all errors related to opening the
         "loose-object-idx" file as success and instead only ignore ENOENT. It is
3:  4233e913ce ! 3:  0a05eb5fb6 loose: lift loose object index writing logic into helper
    @@ Commit message
         Extract the logic for writing a loose object index file into a separate
         `loose_object_map_write()` helper function that allows callers to
         explicitly specify both the loose object map as well as the file
    -    descriptor to write the entries to.
    +    descriptor to write the entries to. Note that the responsibility of
    +    locking the file being written to is left to callers so that it is
    +    possible for a lock to be held for longer than just this single
    +    operation.
     
         Signed-off-by: Justin Tobler <jltobler@gmail.com>
     
4:  614aab27d1 ! 4:  3c4a3332a5 tmp-objdir: merge loose object index when migrating tmpdir
    @@ loose.c: void loose_object_map_clear(struct loose_object_map **map)
     +
     +	loose_object_map_init(&map);
     +
    ++	if (loose_object_map_read(repo, src, map, NULL) < 0) {
    ++		ret = error(_("unable to read loose object index %s"), src);
    ++		goto out;
    ++	}
    ++
     +	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0,
     +						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
     +	if (fd < 0) {
    @@ loose.c: void loose_object_map_clear(struct loose_object_map **map)
     +		goto out;
     +	}
     +
    -+	if (loose_object_map_read(repo, src, map, NULL) < 0) {
    -+		ret = error(_("unable to read loose object index %s"), src);
    -+		goto out;
    -+	}
    -+
     +	if (loose_object_map_write(repo, map, fd) < 0) {
     +		ret = error_errno(_("unable to write %s"),
     +				  get_lock_file_path(&lock));
    @@ loose.c: void loose_object_map_clear(struct loose_object_map **map)
     
      ## loose.h ##
     @@ loose.h: int repo_add_loose_object_map(struct odb_source_loose *loose,
    - 			      const struct object_id *compat_oid);
      int repo_read_loose_object_map(struct repository *repo);
      int repo_write_loose_object_map(struct repository *repo);
    + 
    ++/*
    ++ * Merge the mapping entries of the loose object index at `src` into the one at
    ++ * `dst`. The resulting mapping file is written atomically.
    ++ *
    ++ * Returns 0 on success, a negative error code otherwise.
    ++ */
     +int loose_object_idx_merge(struct repository *repo, const char *src,
     +			   const char *dst);
    - 
    ++
      #endif
     
      ## t/t1016-compatObjectFormat.sh ##
base-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e
-- 
2.56.0.173.gf57c9a7388
Justin Tobler· Oct 29, 2026, 23:22 UTC · re: Justin Tobler · lore

[PATCH v3 1/4] loose: use finite timeout when locking "loose-object-idx" file

Loose object index file writers first attempt to acquire a lock via `repo_hold_lock_file_for_update_timeout()` and specify a timeout value of -1. With a negative timeout value, the lock acquisition retries indefinitely while the lockfile still exists. If a process dies while holding the lock and leaves a stale lockfile behind, every subsequent writer of the mapping file hangs forever instead of reporting an error.

Introduce LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS to set the timeout to 1000ms and use it for all callers that lock a "loose-object-idx" file. A value of 1000ms matches the default timeout used for "core.packedRefsTimeout" and should provide concurrent writers sufficient time to acquire the lock.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 loose.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Show changes to loose.c +6 −2
diff --git a/loose.c b/loose.c
index d6a2bb5ba3..c6f4ee0ec6 100644
--- a/loose.c
+++ b/loose.c
@@ -14,6 +14,8 @@
 
 static const char *loose_object_header = "# loose-object-idx\n";
 
+#define LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS 1000
+
 static inline int should_use_loose_object_map(struct repository *repo)
 {
 	return repo->compat_hash_algo && repo->gitdir;
@@ -137,7 +139,8 @@ int repo_write_loose_object_map(struct repository *repo)
 
 	repo_common_path_replace(repo, &path, "objects/loose-object-idx");
 	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, path.buf,
-						    LOCK_DIE_ON_ERROR, -1);
+						    LOCK_DIE_ON_ERROR,
+						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
 	iter = kh_begin(map);
 	if (write_in_full(fd, loose_object_header, strlen(loose_object_header)) < 0)
 		goto errout;
@@ -180,7 +183,8 @@ static int write_one_object(struct odb_source_loose *loose,
 
 	strbuf_addf(&path, "%s/loose-object-idx", loose->base.path);
 	repo_hold_lock_file_for_update_timeout(loose->base.odb->repo, &lock,
-					       path.buf, LOCK_DIE_ON_ERROR, -1);
+					       path.buf, LOCK_DIE_ON_ERROR,
+					       LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
 
 	fd = open(path.buf, O_WRONLY | O_CREAT | O_APPEND, 0666);
 	if (fd < 0)
-- 
2.56.0.173.gf57c9a7388
Karthik Nayak· Oct 30, 2026, 10:43 UTC · re: Justin Tobler · lore

Re: [PATCH v3 1/4] loose: use finite timeout when locking "loose-object-idx" file

Justin Tobler <jltobler@gmail.com> writes:
Show 13 quoted lines
> Loose object index file writers first attempt to acquire a lock via
> `repo_hold_lock_file_for_update_timeout()` and specify a timeout value
> of -1. With a negative timeout value, the lock acquisition retries
> indefinitely while the lockfile still exists. If a process dies while
> holding the lock and leaves a stale lockfile behind, every subsequent
> writer of the mapping file hangs forever instead of reporting an error.
>
> Introduce LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS to set the timeout to 1000ms
> and use it for all callers that lock a "loose-object-idx" file. A value
> of 1000ms matches the default timeout used for "core.packedRefsTimeout"
> and should provide concurrent writers sufficient time to acquire the
> lock.
>

I was wondering why we don't use a config here too. Seems like Patrick also mentioned the same in his review. But this is a good middle step and we can add that in later.

#leftoverbits
[snip]
Justin Tobler· Oct 29, 2026, 23:22 UTC · re: Justin Tobler · lore

[PATCH v3 2/4] loose: lift loose object index parsing logic into helper

Mapping entries from the "loose-object-idx" file are parsed via `loose_object_map_load()` and inserted into the in-memory loose object map of the corresponding object source. In a subsequent commit, a temporary loose object index file must also be parsed and have its contents stored in a map that is not attached to any object source.

Extract the logic for parsing a loose object index file into a separate `loose_object_map_read()` helper function that allows callers to explicitly specify the map to insert the entries into. The `insert_loose_map()` helper function is also updated accordingly to explicitly operate on the provided map and optional cache instead of the object source. The cache is made optional for future callers that may only be interested in updating the map.

While at it, stop treating all errors related to opening the "loose-object-idx" file as success and instead only ignore ENOENT. It is expected that a mapping file may not always be present, but other errors should be signaled back to callers. Note that this slightly changes the behavior of `repo_read_loose_object_map()`, which now stops loading the mapping files of subsequent object sources when one is unreadable, the same way it already does when one is malformed.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 loose.c | 87 +++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 54 insertions(+), 33 deletions(-)
Show changes to loose.c +54 −33
diff --git a/loose.c b/loose.c
index c6f4ee0ec6..5f72c644f9 100644
--- a/loose.c
+++ b/loose.c
@@ -48,67 +48,88 @@ static int insert_oid_pair(kh_oid_map_t *map, const struct object_id *key, const
 	return 1;
 }
 
-static int insert_loose_map(struct odb_source_loose *loose,
+static int insert_loose_map(struct loose_object_map *map,
+			    struct oidtree *cache,
 			    const struct object_id *oid,
 			    const struct object_id *compat_oid)
 {
-	struct loose_object_map *map = loose->map;
 	int inserted = 0;
 
 	inserted |= insert_oid_pair(map->to_compat, oid, compat_oid);
 	inserted |= insert_oid_pair(map->to_storage, compat_oid, oid);
-	if (inserted)
-		oidtree_insert(loose->cache, compat_oid, NULL);
+	if (inserted && cache)
+		oidtree_insert(cache, compat_oid, NULL);
 
 	return inserted;
 }
 
-int loose_object_map_load(struct odb_source_loose *loose)
+static int loose_object_map_read(struct repository *repo, const char *path,
+				 struct loose_object_map *map,
+				 struct oidtree *cache)
 {
-	struct repository *repo = loose->base.odb->repo;
 	struct strbuf buf = STRBUF_INIT;
-	char *path;
 	FILE *fp;
-	int ret = -1;
-
-	if (!should_use_loose_object_map(repo))
-		return 0;
-
-	if (!loose->map)
-		loose_object_map_init(&loose->map);
-	if (!loose->cache) {
-		ALLOC_ARRAY(loose->cache, 1);
-		oidtree_init(loose->cache);
-	}
-
-	insert_loose_map(loose, repo->hash_algo->empty_tree, repo->compat_hash_algo->empty_tree);
-	insert_loose_map(loose, repo->hash_algo->empty_blob, repo->compat_hash_algo->empty_blob);
-	insert_loose_map(loose, repo->hash_algo->null_oid, repo->compat_hash_algo->null_oid);
+	int ret;
 
-	path = xstrfmt("%s/loose-object-idx", loose->base.path);
 	fp = fopen(path, "rb");
 	if (!fp) {
-		free(path);
-		return 0;
+		if (errno == ENOENT)
+			return 0;
+		return -1;
 	}
 
-	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header))
-		goto err;
+	if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header)) {
+		ret = -1;
+		goto out;
+	}
 	while (!strbuf_getline_lf(&buf, fp)) {
 		const char *p;
 		struct object_id oid, compat_oid;
 		if (parse_oid_hex_algop(buf.buf, &oid, &p, repo->hash_algo) ||
 		    *p++ != ' ' ||
 		    parse_oid_hex_algop(p, &compat_oid, &p, repo->compat_hash_algo) ||
-		    p != buf.buf + buf.len)
-			goto err;
-		insert_loose_map(loose, &oid, &compat_oid);
+		    p != buf.buf + buf.len) {
+			ret = -1;
+			goto out;
+		}
+		insert_loose_map(map, cache, &oid, &compat_oid);
 	}
 
 	ret = ferror(fp) ? -1 : 0;
-err:
-	fclose(fp);
+
+out:
 	strbuf_release(&buf);
+	fclose(fp);
+
+	return ret;
+}
+
+int loose_object_map_load(struct odb_source_loose *loose)
+{
+	struct repository *repo = loose->base.odb->repo;
+	char *path;
+	int ret;
+
+	if (!should_use_loose_object_map(repo))
+		return 0;
+
+	if (!loose->map)
+		loose_object_map_init(&loose->map);
+	if (!loose->cache) {
+		ALLOC_ARRAY(loose->cache, 1);
+		oidtree_init(loose->cache);
+	}
+
+	insert_loose_map(loose->map, loose->cache, repo->hash_algo->empty_tree,
+			 repo->compat_hash_algo->empty_tree);
+	insert_loose_map(loose->map, loose->cache, repo->hash_algo->empty_blob,
+			 repo->compat_hash_algo->empty_blob);
+	insert_loose_map(loose->map, loose->cache, repo->hash_algo->null_oid,
+			 repo->compat_hash_algo->null_oid);
+
+	path = xstrfmt("%s/loose-object-idx", loose->base.path);
+	ret = loose_object_map_read(repo, path, loose->map, loose->cache);
+
 	free(path);
 	return ret;
 }
@@ -223,7 +244,7 @@ int repo_add_loose_object_map(struct odb_source_loose *loose,
 	if (!should_use_loose_object_map(loose->base.odb->repo))
 		return 0;
 
-	inserted = insert_loose_map(loose, oid, compat_oid);
+	inserted = insert_loose_map(loose->map, loose->cache, oid, compat_oid);
 	if (inserted)
 		return write_one_object(loose, oid, compat_oid);
 	return 0;
-- 
2.56.0.173.gf57c9a7388
Karthik Nayak· Oct 30, 2026, 10:50 UTC · re: Justin Tobler · lore

Re: [PATCH v3 2/4] loose: lift loose object index parsing logic into helper

Justin Tobler <jltobler@gmail.com> writes:
Show 13 quoted lines
> diff --git a/loose.c b/loose.c
> index c6f4ee0ec6..5f72c644f9 100644
> --- a/loose.c
> +++ b/loose.c
> @@ -48,67 +48,88 @@ static int insert_oid_pair(kh_oid_map_t *map, const struct object_id *key, const
>  	return 1;
>  }
>
> -static int insert_loose_map(struct odb_source_loose *loose,
> +static int insert_loose_map(struct loose_object_map *map,
> +			    struct oidtree *cache,
>  			    const struct object_id *oid,
>  			    const struct object_id *compat_oid)
Nit: with the introduction of `loose_object_map_read()`, perhaps makes
sense to keep naming consistent and rename this to
`loose_object_map_insert()`?
[snip]
The changes look good.
Justin Tobler· Oct 30, 2026, 15:31 UTC · re: Karthik Nayak · lore

Re: [PATCH v3 2/4] loose: lift loose object index parsing logic into helper

On 26/10/30 07:50PM, Karthik Nayak wrote:
Show 19 quoted lines
> Justin Tobler <jltobler@gmail.com> writes:
> 
> > diff --git a/loose.c b/loose.c
> > index c6f4ee0ec6..5f72c644f9 100644
> > --- a/loose.c
> > +++ b/loose.c
> > @@ -48,67 +48,88 @@ static int insert_oid_pair(kh_oid_map_t *map, const struct object_id *key, const
> >  	return 1;
> >  }
> >
> > -static int insert_loose_map(struct odb_source_loose *loose,
> > +static int insert_loose_map(struct loose_object_map *map,
> > +			    struct oidtree *cache,
> >  			    const struct object_id *oid,
> >  			    const struct object_id *compat_oid)
> 
> Nit: with the introduction of `loose_object_map_read()`, perhaps makes
> sense to keep naming consistent and rename this to
> `loose_object_map_insert()`?

I don't feel too strongly either way and am not sure this by itself it worth a reroll. I'll keep it as-is for now.

-Justin
Justin Tobler· Oct 29, 2026, 23:22 UTC · re: Justin Tobler · lore

[PATCH v3 3/4] loose: lift loose object index writing logic into helper

Writing the in-memory loose object map to a "loose-object-idx" file is handled via `repo_write_loose_object_map()`, which always writes the map into the primary object directory. In a subsequent commit, a separate "loose-object-idx" file is staged prior to commit and must be constructed from a loose object map not attached to the primary loose object source.

Extract the logic for writing a loose object index file into a separate `loose_object_map_write()` helper function that allows callers to explicitly specify both the loose object map as well as the file descriptor to write the entries to. Note that the responsibility of locking the file being written to is left to callers so that it is possible for a lock to be held for longer than just this single operation.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 loose.c | 69 +++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 45 insertions(+), 24 deletions(-)
Show changes to loose.c +45 −24
diff --git a/loose.c b/loose.c
index 5f72c644f9..f2e111c25e 100644
--- a/loose.c
+++ b/loose.c
@@ -146,25 +146,17 @@ int repo_read_loose_object_map(struct repository *repo)
 	return 0;
 }
 
-int repo_write_loose_object_map(struct repository *repo)
+static int loose_object_map_write(struct repository *repo,
+				  struct loose_object_map *loose_map, int fd)
 {
-	struct odb_source_files *files = odb_source_files_downcast(repo->objects->source);
-	kh_oid_map_t *map = files->dirs->loose->map->to_compat;
-	struct lock_file lock;
-	int fd;
+	kh_oid_map_t *map = loose_map->to_compat;
+	struct strbuf buf = STRBUF_INIT;
 	khiter_t iter;
-	struct strbuf buf = STRBUF_INIT, path = STRBUF_INIT;
-
-	if (!should_use_loose_object_map(repo))
-		return 0;
+	int ret = -1;
 
-	repo_common_path_replace(repo, &path, "objects/loose-object-idx");
-	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, path.buf,
-						    LOCK_DIE_ON_ERROR,
-						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
 	iter = kh_begin(map);
 	if (write_in_full(fd, loose_object_header, strlen(loose_object_header)) < 0)
-		goto errout;
+		goto out;
 
 	for (; iter != kh_end(map); iter++) {
 		if (kh_exist(map, iter)) {
@@ -173,24 +165,53 @@ int repo_write_loose_object_map(struct repository *repo)
 				continue;
 			strbuf_addf(&buf, "%s %s\n", oid_to_hex(&kh_key(map, iter)), oid_to_hex(kh_value(map, iter)));
 			if (write_in_full(fd, buf.buf, buf.len) < 0)
-				goto errout;
+				goto out;
 			strbuf_reset(&buf);
 		}
 	}
+
+	ret = 0;
+
+out:
 	strbuf_release(&buf);
+
+	return ret;
+}
+
+int repo_write_loose_object_map(struct repository *repo)
+{
+	struct odb_source_files *files = odb_source_files_downcast(repo->objects->source);
+	struct strbuf path = STRBUF_INIT;
+	struct lock_file lock;
+	int ret;
+	int fd;
+
+	if (!should_use_loose_object_map(repo))
+		return 0;
+
+	repo_common_path_replace(repo, &path, "objects/loose-object-idx");
+	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, path.buf,
+						    LOCK_DIE_ON_ERROR,
+						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
+
+	if (loose_object_map_write(repo, files->dirs->loose->map, fd) < 0) {
+		ret = error_errno(_("unable to write %s"),
+				  get_lock_file_path(&lock));
+		goto out;
+	}
+
 	if (commit_lock_file(&lock) < 0) {
-		error_errno(_("could not write loose object index %s"), path.buf);
-		strbuf_release(&path);
-		return -1;
+		ret = error_errno(_("unable to write %s"), path.buf);
+		goto out;
 	}
-	strbuf_release(&path);
-	return 0;
-errout:
+
+	ret = 0;
+
+out:
 	rollback_lock_file(&lock);
-	strbuf_release(&buf);
-	error_errno(_("failed to write loose object index %s"), path.buf);
 	strbuf_release(&path);
-	return -1;
+
+	return ret;
 }
 
 static int write_one_object(struct odb_source_loose *loose,
-- 
2.56.0.173.gf57c9a7388
Justin Tobler· Oct 29, 2026, 23:22 UTC · re: Justin Tobler · lore

[PATCH v3 4/4] tmp-objdir: merge loose object index when migrating tmpdir

When a repository is configured with a compatibility hash, each loose object write also records a mapping between the object and its compatibility hash in a "loose-object-idx" file. This mapping file is located in the same object directory the object is written to. If batch fsync of loose objects is also enabled, the loose objects and mapping file are instead written to a temporary object directory and expected to be migrated to the primary object directory when the batch transaction commits.

While migrating the loose objects works, the "loose-object-idx" file is unable to be migrated if a mapping file already exists in the primary object directory. This occurs because `finalize_object_file_flags()` expects an already existing file to have the same contents. Otherwise, the following error is returned:

  error: files '.git/objects/tmp_objdir-bulk-fsync-XXXXXX/loose-object-idx' and '.git/objects/loose-object-idx' differ in contents
  error: unable to migrate temporary objects

Furthermore, it is not sufficient to just replace the preexisting mapping file with the new one as the new file only contains mapping entries for the newly written objects.

Update the temporary directory migration logic to handle "loose-object-idx" files explicitly by merging new entries and atomically writing the resulting mapping file.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 loose.c                       | 47 +++++++++++++++++++++++++++++++++++
 loose.h                       |  9 +++++++
 t/t1016-compatObjectFormat.sh | 27 ++++++++++++++++++++
 tmp-objdir.c                  | 11 +++++++-
 4 files changed, 93 insertions(+), 1 deletion(-)
Show changes to 4 files +93 −1

loose.c, loose.h, t/t1016-compatObjectFormat.sh, tmp-objdir.c

diff --git a/loose.c b/loose.c
index f2e111c25e..0ce9c7486e 100644
--- a/loose.c
+++ b/loose.c
@@ -311,3 +311,50 @@ void loose_object_map_clear(struct loose_object_map **map)
 	free(m);
 	*map = NULL;
 }
+
+int loose_object_idx_merge(struct repository *repo, const char *src,
+			   const char *dst)
+{
+	struct lock_file lock = LOCK_INIT;
+	struct loose_object_map *map;
+	int ret;
+	int fd;
+
+	loose_object_map_init(&map);
+
+	if (loose_object_map_read(repo, src, map, NULL) < 0) {
+		ret = error(_("unable to read loose object index %s"), src);
+		goto out;
+	}
+
+	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0,
+						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
+	if (fd < 0) {
+		ret = error_errno(_("unable to lock %s"), dst);
+		goto out;
+	}
+
+	if (loose_object_map_read(repo, dst, map, NULL) < 0) {
+		ret = error(_("unable to read loose object index %s"), dst);
+		goto out;
+	}
+
+	if (loose_object_map_write(repo, map, fd) < 0) {
+		ret = error_errno(_("unable to write %s"),
+				  get_lock_file_path(&lock));
+		goto out;
+	}
+
+	if (commit_lock_file(&lock) < 0) {
+		ret = error_errno(_("unable to write %s"), dst);
+		goto out;
+	}
+
+	ret = 0;
+
+out:
+	loose_object_map_clear(&map);
+	rollback_lock_file(&lock);
+
+	return ret;
+}
diff --git a/loose.h b/loose.h
index ed663ac550..6ad82dc0bb 100644
--- a/loose.h
+++ b/loose.h
@@ -24,4 +24,13 @@ int repo_add_loose_object_map(struct odb_source_loose *loose,
 int repo_read_loose_object_map(struct repository *repo);
 int repo_write_loose_object_map(struct repository *repo);
 
+/*
+ * Merge the mapping entries of the loose object index at `src` into the one at
+ * `dst`. The resulting mapping file is written atomically.
+ *
+ * Returns 0 on success, a negative error code otherwise.
+ */
+int loose_object_idx_merge(struct repository *repo, const char *src,
+			   const char *dst);
+
 #endif
diff --git a/t/t1016-compatObjectFormat.sh b/t/t1016-compatObjectFormat.sh
index 9cafcee509..6d0f48226f 100755
--- a/t/t1016-compatObjectFormat.sh
+++ b/t/t1016-compatObjectFormat.sh
@@ -305,4 +305,31 @@ compare_oids GPG2 'commit' signedcommit4 "$signedcommit4_sha1_oid" "$signedcommi
 compare_oids GPG2 'tag' signedtag3 "$signedtag3_sha1_oid" "$signedtag3_sha256_oid"
 compare_oids GPG2 'tag' signedtag4 "$signedtag4_sha1_oid" "$signedtag4_sha256_oid"
 
+test_expect_success 'batched fsync migrates loose object index' '
+	test_when_finished "rm -rf batch-fsync" &&
+	git init --object-format=sha1 batch-fsync &&
+	(
+		cd batch-fsync &&
+		git config core.repositoryFormatVersion 1 &&
+		git config extensions.compatObjectFormat sha256 &&
+		git config core.fsync loose-object &&
+		git config core.fsyncMethod batch &&
+
+		# Write object to generate initial loose object index.
+		echo foo >foo &&
+		git add foo &&
+		test_grep "$(git rev-parse :foo)" .git/objects/loose-object-idx &&
+
+		# Write some additional objects and ensure the corresponding
+		# temporary loose object index entries stored in the transaction
+		# are merged into the primary object directory.
+		echo bar >bar &&
+		echo baz >baz &&
+		git add bar baz &&
+		test_grep "$(git rev-parse :foo)" .git/objects/loose-object-idx &&
+		test_grep "$(git rev-parse :bar)" .git/objects/loose-object-idx &&
+		test_grep "$(git rev-parse :baz)" .git/objects/loose-object-idx
+	)
+'
+
 test_done
diff --git a/tmp-objdir.c b/tmp-objdir.c
index 3debb60270..5307717354 100644
--- a/tmp-objdir.c
+++ b/tmp-objdir.c
@@ -4,6 +4,7 @@
 #include "chdir-notify.h"
 #include "dir.h"
 #include "environment.h"
+#include "loose.h"
 #include "object-file.h"
 #include "path.h"
 #include "string-list.h"
@@ -294,6 +295,11 @@ static int is_loose_object_shard(const char *name)
 	return strlen(name) == 2 && isxdigit(name[0]) && isxdigit(name[1]);
 }
 
+static int is_loose_object_idx(const char *name)
+{
+	return !strcmp(name, "loose-object-idx");
+}
+
 static int migrate_paths(struct tmp_objdir *t,
 			 struct strbuf *src, struct strbuf *dst,
 			 enum finalize_object_file_flags flags)
@@ -317,7 +323,10 @@ static int migrate_paths(struct tmp_objdir *t,
 		if (is_loose_object_shard(name))
 			flags_copy |= FOF_SKIP_COLLISION_CHECK;
 
-		ret |= migrate_one(t, src, dst, flags_copy);
+		if (is_loose_object_idx(name))
+			ret |= loose_object_idx_merge(t->repo, src->buf, dst->buf);
+		else
+			ret |= migrate_one(t, src, dst, flags_copy);
 
 		strbuf_setlen(src, src_len);
 		strbuf_setlen(dst, dst_len);
-- 
2.56.0.173.gf57c9a7388
Karthik Nayak· Oct 30, 2026, 11:55 UTC · re: Justin Tobler · lore

Re: [PATCH v3 4/4] tmp-objdir: merge loose object index when migrating tmpdir

Justin Tobler <jltobler@gmail.com> writes:
Show 12 quoted lines
> diff --git a/loose.c b/loose.c
> index f2e111c25e..0ce9c7486e 100644
> --- a/loose.c
> +++ b/loose.c
> @@ -311,3 +311,50 @@ void loose_object_map_clear(struct loose_object_map **map)
>  	free(m);
>  	*map = NULL;
>  }
> +
> +int loose_object_idx_merge(struct repository *repo, const char *src,
> +			   const char *dst)
>
Nit: should we name this `loose_object_map_merge()` to stay consistent
with the other `loose_object_map...()` functions?
Show 33 quoted lines
> +{
> +	struct lock_file lock = LOCK_INIT;
> +	struct loose_object_map *map;
> +	int ret;
> +	int fd;
> +
> +	loose_object_map_init(&map);
> +
> +	if (loose_object_map_read(repo, src, map, NULL) < 0) {
> +		ret = error(_("unable to read loose object index %s"), src);
> +		goto out;
> +	}
> +
> +	fd = repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0,
> +						    LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS);
> +	if (fd < 0) {
> +		ret = error_errno(_("unable to lock %s"), dst);
> +		goto out;
> +	}
> +
> +	if (loose_object_map_read(repo, dst, map, NULL) < 0) {
> +		ret = error(_("unable to read loose object index %s"), dst);
> +		goto out;
> +	}
> +
> +	if (loose_object_map_write(repo, map, fd) < 0) {
> +		ret = error_errno(_("unable to write %s"),
> +				  get_lock_file_path(&lock));
> +		goto out;
> +	}
> +
> +	if (commit_lock_file(&lock) < 0) {
> +		ret = error_errno(_("unable to write %s"), dst);
Nit: Shouldn't this be unable to commit/rename? This will differentiate
from the above write error
Show 11 quoted lines
> +		goto out;
> +	}
> +
> +	ret = 0;
> +
> +out:
> +	loose_object_map_clear(&map);
> +	rollback_lock_file(&lock);
> +
> +	return ret;
> +}
The rest looks to be in order.
Justin Tobler· Oct 30, 2026, 15:40 UTC · re: Karthik Nayak · lore

Re: [PATCH v3 4/4] tmp-objdir: merge loose object index when migrating tmpdir

On 26/10/30 08:55PM, Karthik Nayak wrote:
Show 17 quoted lines
> Justin Tobler <jltobler@gmail.com> writes:
> 
> > diff --git a/loose.c b/loose.c
> > index f2e111c25e..0ce9c7486e 100644
> > --- a/loose.c
> > +++ b/loose.c
> > @@ -311,3 +311,50 @@ void loose_object_map_clear(struct loose_object_map **map)
> >  	free(m);
> >  	*map = NULL;
> >  }
> > +
> > +int loose_object_idx_merge(struct repository *repo, const char *src,
> > +			   const char *dst)
> >
> 
> Nit: should we name this `loose_object_map_merge()` to stay consistent
> with the other `loose_object_map...()` functions?

I specifically did not call it `loose_object_map_merge()` because, unlike the other similarly named functions, this one does not update the in-memory loose object map for the object source. Here we are only merging the "loose-object-idx" files.

Show 11 quoted lines
> > +	if (loose_object_map_write(repo, map, fd) < 0) {
> > +		ret = error_errno(_("unable to write %s"),
> > +				  get_lock_file_path(&lock));
> > +		goto out;
> > +	}
> > +
> > +	if (commit_lock_file(&lock) < 0) {
> > +		ret = error_errno(_("unable to write %s"), dst);
> 
> Nit: Shouldn't this be unable to commit/rename? This will differentiate
> from the above write error

I choose "write" here because logically we are trying atomically write the final "loose-object-idx" file. IMO, I think the difference in filepath should be sufficient to differentiate between the two as well. Ultimately though, I don't feel strongly either way about this and am not sure it's worth a reroll, so I'm inclined to leave it as-is.

-Justin
Patrick Steinhardt· Oct 30, 2026, 06:20 UTC · re: Justin Tobler · lore

Re: [PATCH v3 0/4] loose: fix migrating "loose-object-idx" with batch fsync

On Thu, Oct 29, 2026 at 06:22:16PM -0500, Justin Tobler wrote:
Show 5 quoted lines
> Changes since V2:
>   - Reading the src mapping file is now done prior to acquiring the lock
>     to reduce the critical window.
>   - Added a comment to `loose_object_idx_merge()` and updated some
>     commit messages to improve clarity.

The changes and range-diff both look exacty as expected, so I'm happy with this version. Thanks!

Patrick
Justin Tobler· Oct 30, 2026, 15:40 UTC · re: Patrick Steinhardt · lore

Re: [PATCH v3 0/4] loose: fix migrating "loose-object-idx" with batch fsync

On 26/10/30 07:20AM, Patrick Steinhardt wrote:
Show 9 quoted lines
> On Thu, Oct 29, 2026 at 06:22:16PM -0500, Justin Tobler wrote:
> > Changes since V2:
> >   - Reading the src mapping file is now done prior to acquiring the lock
> >     to reduce the critical window.
> >   - Added a comment to `loose_object_idx_merge()` and updated some
> >     commit messages to improve clarity.
> 
> The changes and range-diff both look exacty as expected, so I'm happy
> with this version. Thanks!
Thanks for the review!
-Justin
Karthik Nayak· Oct 30, 2026, 10:38 UTC · re: Justin Tobler · lore

Re: [PATCH v3 0/4] loose: fix migrating "loose-object-idx" with batch fsync

Justin Tobler <jltobler@gmail.com> writes:
Show 8 quoted lines
> Greetings,
>
> While working on a separate patch series to convert the "files" ODB
> transaction to be eagerly created instead of lazily, I encountered this
> bug that causes an ODB transaction to fail on commit when a
> compatibility hash is also configured. This occurs because the temporary
> directory migration code does not know how to handle the loose object
> index file.

Okay so reading a bit, and paraphrasing for my understanding: When we hae object compatibility setup in a repository. THe 'loose-object-idx' tracks the mapping from hash1<>hash2. The issue is batch fsync, where we use a temporary folder. I'm not sure what the 'error' here is, but I guess we'll see with the patches.

For pack files this is a non-issues as they're content addressed and individual packfiles have their own corresponding idx files.

> This patch addresses updating the migration logic to explicitly merge
> mapping entries in the transaction and atomically write a new
> "loose-object-idx" file.
>

This means we write a 'loose-object-idx' to the temporary directory and we need to merge it with the one in the main directory. Which seems to make sense.

Show 5 quoted lines
> Some other approaches I considered:
>   - Storing the uncommitted object mapping entries in the transaction
>     without writing a file and only writing on transaction commit. I
>     chose to keep writing the temp index into the transaction so that
>     child processes that write to the tempdir could still append entries.

So the child processes write the objects, so they write to the index too I presume. If not, we'd have to somehow get that information from all children to the parent which owns the transaction. That gets complicated / messy. Seems right.

>   - Also writing mapping entires to the main ODB loose object index.
>     This also didn't seem like a good apprach because it write entries
>     that may not ultimately get committed.
>

Yeah this approach definitely doesn't make sense from a transaction point of view.

[snip]
Will dig into the patches...
Justin Tobler· Oct 30, 2026, 15:43 UTC · re: Karthik Nayak · lore

Re: [PATCH v3 0/4] loose: fix migrating "loose-object-idx" with batch fsync

On 26/10/30 07:38PM, Karthik Nayak wrote:
> 
> Will dig into the patches...

Thanks for the review! IMO, I think the suggestions left are probably not worth a reroll, so I plan to leave the series as-is, but please let me know if you feel differently. :)

Thanks, -Justin

Karthik Nayak· Nov 3, 2026, 16:37 UTC · re: Justin Tobler · lore

Re: [PATCH v3 0/4] loose: fix migrating "loose-object-idx" with batch fsync

Justin Tobler <jltobler@gmail.com> writes:
Show 10 quoted lines
> On 26/10/30 07:38PM, Karthik Nayak wrote:
>>
>> Will dig into the patches...
>
> Thanks for the review! IMO, I think the suggestions left are probably
> not worth a reroll, so I plan to leave the series as-is, but please let
> me know if you feel differently. :)
>
> Thanks,
> -Justin
It's all good. It is in a good shape already!

← back to recent threads