git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] object-file: explicitly write ".keep" lockfile

From
Justin Tobler <jltobler@gmail.com>
Date
Oct 16, 2026, 02:29 UTC
Message-ID
<20261016022901.3838402-3-jltobler@gmail.com>
In-Reply-To
<20261016022901.3838402-1-jltobler@gmail.com>

The `odb_transaction_write_pack()` interface for the "files" backend relies on git-index-pack(1) to write the specified packfile into an ODB transaction as a packfile. To prevent a concurrent housekeeping operation from deleting the newly written packfile prior to the references being updated, a ".keep" lockfile is also generated by git-index-pack(1) via the `--keep` option. Consequently, the ".keep" lockfile is written into the ODB transaction alongside the packfile and later migrated together to the primary ODB during transaction commit.

In a subsequent commit, writing the lockfile is deferred until transaction commit and thus cannot rely on git-index-pack(1) to write it. In preparation for this, update `odb_transaction_files_write_pack()` to explicitly generate the same ".keep" lockfile itself.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 object-file.c | 99 ++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 79 insertions(+), 20 deletions(-)
diff --git a/object-file.c b/object-file.c
index 210984f825..ebe46d35de 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1256,6 +1256,43 @@ int read_loose_object(struct repository *repo,
 	return ret;
 }
 
+static int write_pack_lock(struct odb_source *source, const char *pack_name,
+			   const char *keep_msg)
+{
+	struct strbuf lock_path = STRBUF_INIT;
+	struct strbuf contents = STRBUF_INIT;
+	int ret = -1;
+	int fd;
+
+	strbuf_addf(&lock_path, "%s/pack/pack-%s.keep",
+		    source->path, pack_name);
+	strbuf_addf(&contents, "%s\n", keep_msg);
+
+	fd = safe_create_file_with_leading_directories(source->odb->repo,
+						       lock_path.buf);
+	if (fd < 0)
+		goto out;
+
+	if (write_in_full(fd, contents.buf, contents.len) < 0) {
+		close(fd);
+		unlink(lock_path.buf);
+		goto out;
+	}
+
+	if (close(fd)) {
+		unlink(lock_path.buf);
+		goto out;
+	}
+
+	ret = 0;
+
+out:
+	strbuf_release(&lock_path);
+	strbuf_release(&contents);
+
+	return ret;
+}
+
 static int odb_transaction_files_commit(struct odb_transaction *base)
 {
 	struct odb_transaction_files *transaction =
@@ -1389,18 +1426,17 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
 			return -1;
 		}
 	} else {
+		struct strbuf final_path = STRBUF_INIT;
+		struct strbuf pack_name = STRBUF_INIT;
+		struct strbuf keep_msg = STRBUF_INIT;
 		char hostname[HOST_NAME_MAX + 1];
-		char *lockfile;
+		int ret;
 
 		strvec_pushl(&child.args, "index-pack", "--stdin", NULL);
 		push_header_arg(&child.args, &hdr);
 
 		if (xgethostname(hostname, sizeof(hostname)))
 			xsnprintf(hostname, sizeof(hostname), "localhost");
-		strvec_pushf(&child.args,
-			     "--keep=receive-pack %"PRIuMAX" on %s",
-			     (uintmax_t)getpid(),
-			     hostname);
 
 		if (!opts->quiet && err_fd)
 			strvec_push(&child.args, "--show-resolving-progress");
@@ -1424,31 +1460,54 @@ static int odb_transaction_files_write_pack(struct odb_transaction *base,
 			return -1;
 		}
 
-		/*
-		 * The lockfile filepath is expected to be the final location of
-		 * the ".keep" file after being migrated to the main ODB source.
-		 * This ensures the lockfile can be found and removed later
-		 * after the ODB transaction has been committed.
-		 */
-		lockfile = index_pack_lockfile(base->source, child.out, NULL);
-		if (lockfile) {
-			ALLOC_GROW(transaction->pack_lockfiles,
-				   transaction->pack_lockfiles_nr + 1,
-				   transaction->pack_lockfiles_alloc);
-			transaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =
-				register_tempfile(lockfile);
-			free(lockfile);
-		}
+		ret = read_index_pack_report(repo, child.out, &pack_name, NULL);
+
 		close(child.out);
 
 		status = finish_command(&child);
 		if (status) {
+			strbuf_release(&pack_name);
 			strbuf_addstr(err_msg, "index-pack abnormal exit");
 			return -1;
 		}
 
+		if (ret) {
+			strbuf_release(&pack_name);
+			strbuf_addstr(err_msg, "index-pack failed to report pack name");
+			return -1;
+		}
+
+		strbuf_addf(&keep_msg, "receive-pack %"PRIuMAX" on %s",
+			    (uintmax_t)getpid(), hostname);
+
+		if (write_pack_lock(transaction->quarantine, pack_name.buf, keep_msg.buf)) {
+			strbuf_release(&pack_name);
+			strbuf_release(&keep_msg);
+			strbuf_addstr(err_msg, "lockfile write failed");
+			return -1;
+		}
+
+		ALLOC_GROW(transaction->pack_lockfiles,
+			   transaction->pack_lockfiles_nr + 1,
+			   transaction->pack_lockfiles_alloc);
+
+		/*
+		 * The lockfile filepath is expected to be the final location of
+		 * the ".keep" file after being migrated to the main ODB source.
+		 * This ensures the lockfile can be found and removed later
+		 * after the ODB transaction has been committed.
+		 */
+		strbuf_addf(&final_path, "%s/pack/pack-%s.keep",
+			    base->source->path, pack_name.buf);
+		transaction->pack_lockfiles[transaction->pack_lockfiles_nr++] =
+			register_tempfile(final_path.buf);
+
 		odb_source_prepare(transaction->quarantine,
 				   ODB_PREPARE_FLUSH_CACHES);
+
+		strbuf_release(&final_path);
+		strbuf_release(&pack_name);
+		strbuf_release(&keep_msg);
 	}
 
 	return 0;
-- 
2.55.0.424.g13c7afec21
Previous: Justin ToblerNext: Patrick Steinhardt
Message 5 of 40 in “object-file: fix ".keep" lockfile ownership in ODB transactions”
  1. 0/3 object-file: fix ".keep" lockfile ownership in ODB transactionsJustin Tobler, Oct 16, 2026
  2. 1/3 pack-write: lift and expose index-pack report parsingJustin Tobler, Oct 16, 2026
  3. Patrick SteinhardtOct 16, 2026
  4. Justin ToblerOct 16, 2026
  5. 2/3 object-file: explicitly write ".keep" lockfileJustin Tobler, Oct 16, 2026
  6. Patrick SteinhardtOct 16, 2026
  7. Justin ToblerOct 16, 2026
  8. 3/3 object-file: write ".keep" lockfiles during transaction commitJustin Tobler, Oct 16, 2026
  9. Patrick SteinhardtOct 16, 2026
  10. Justin ToblerOct 16, 2026
  11. 0/4 object-file: fix ".keep" lockfile ownership in ODB transactionsJustin Tobler, Oct 16, 2026
  12. 1/4 pack-write: make index-pack output comment hash agnosticJustin Tobler, Oct 16, 2026
  13. 2/4 pack-write: lift and expose index-pack report parsingJustin Tobler, Oct 16, 2026
  14. 3/4 object-file: explicitly write ".keep" lockfileJustin Tobler, Oct 16, 2026
  15. Patrick SteinhardtOct 19, 2026
  16. Justin ToblerOct 19, 2026
  17. 4/4 object-file: write ".keep" lockfiles during transaction commitJustin Tobler, Oct 16, 2026
  18. Patrick SteinhardtOct 19, 2026
  19. 0/4 object-file: fix ".keep" lockfile ownership in ODB transactionsJustin Tobler, Oct 20, 2026
  20. 1/4 pack-write: make index-pack output comment hash agnosticJustin Tobler, Oct 20, 2026
  21. 2/4 pack-write: lift and expose index-pack report parsingJustin Tobler, Oct 20, 2026
  22. 3/4 object-file: explicitly write ".keep" lockfileJustin Tobler, Oct 20, 2026
  23. Patrick SteinhardtOct 20, 2026
  24. Justin ToblerOct 20, 2026
  25. Junio C HamanoOct 21, 2026
  26. 4/4 object-file: write ".keep" lockfiles during transaction commitJustin Tobler, Oct 20, 2026
  27. Patrick SteinhardtOct 20, 2026
  28. 0/4 object-file: fix ".keep" lockfile ownership in ODB transactionsJustin Tobler, Oct 21, 2026
  29. 1/4 pack-write: make index-pack output comment hash agnosticJustin Tobler, Oct 21, 2026
  30. Karthik NayakOct 26, 2026
  31. Justin ToblerOct 26, 2026
  32. 2/4 pack-write: lift and expose index-pack report parsingJustin Tobler, Oct 21, 2026
  33. 3/4 object-file: explicitly write ".keep" lockfileJustin Tobler, Oct 21, 2026
  34. Karthik NayakOct 26, 2026
  35. Justin ToblerOct 27, 2026
  36. Justin ToblerOct 27, 2026
  37. Karthik NayakOct 26, 2026
  38. 4/4 object-file: write ".keep" lockfiles during transaction commitJustin Tobler, Oct 21, 2026
  39. Karthik NayakOct 26, 2026
  40. Patrick SteinhardtOct 22, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.