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

[PATCH v2 07/16] object-file: get rid of `the_repository` in `finalize_object_file()`

From
Patrick Steinhardt <ps@pks.im>
Date
Jul 17, 2025, 04:56 UTC
Message-ID
<20250717-pks-object-file-wo-the-repository-v2-7-36d2cd6c700e@pks.im>
In-Reply-To
<20250717-pks-object-file-wo-the-repository-v2-0-36d2cd6c700e@pks.im>

We implicitly depend on `the_repository` when moving an object file into place in `finalize_object_file()`. Get rid of this global dependency by passing in a repository.

Note that one might be pressed to inject an object database instead of a repository. But the function doesn't really care about the ODB at all. All it does is to move a file into place while checking whether there is any collision. As such, the functionality it provides is independent of the object database and only needs the repository as parameter so that it can adjust permissions of the file we are about to finalize.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/fast-import.c  |  4 ++--
 builtin/index-pack.c   |  2 +-
 builtin/pack-objects.c |  2 +-
 bulk-checkin.c         |  2 +-
 http.c                 |  4 ++--
 midx-write.c           |  2 +-
 object-file.c          | 14 ++++++++------
 object-file.h          |  6 ++++--
 pack-write.c           | 16 +++++++++-------
 pack.h                 |  3 ++-
 tmp-objdir.c           |  2 +-
 11 files changed, 32 insertions(+), 25 deletions(-)
diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index b1389c59211..89f57898b15 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -821,11 +821,11 @@ static char *keep_pack(const char *curr_index_name)
 		die_errno("failed to write keep file");
 
 	odb_pack_name(pack_data->repo, &name, pack_data->hash, "pack");
-	if (finalize_object_file(pack_data->pack_name, name.buf))
+	if (finalize_object_file(pack_data->repo, pack_data->pack_name, name.buf))
 		die("cannot store pack file");
 
 	odb_pack_name(pack_data->repo, &name, pack_data->hash, "idx");
-	if (finalize_object_file(curr_index_name, name.buf))
+	if (finalize_object_file(pack_data->repo, curr_index_name, name.buf))
 		die("cannot store index file");
 	free((void *)curr_index_name);
 	return strbuf_detach(&name, NULL);
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 19c67a85344..dabeb825a6c 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1598,7 +1598,7 @@ static void rename_tmp_packfile(const char **final_name,
 	if (!*final_name || strcmp(*final_name, curr_name)) {
 		if (!*final_name)
 			*final_name = odb_pack_name(the_repository, name, hash, ext);
-		if (finalize_object_file(curr_name, *final_name))
+		if (finalize_object_file(the_repository, curr_name, *final_name))
 			die(_("unable to rename temporary '*.%s' file to '%s'"),
 			    ext, *final_name);
 	} else if (make_read_only_if_same) {
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index a44f0ce1c78..e8e85d8278b 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -1449,7 +1449,7 @@ static void write_pack_file(void)
 				strbuf_setlen(&tmpname, tmpname_len);
 			}
 
-			rename_tmp_packfile_idx(&tmpname, &idx_tmp_name);
+			rename_tmp_packfile_idx(the_repository, &tmpname, &idx_tmp_name);
 
 			free(idx_tmp_name);
 			strbuf_release(&tmpname);
diff --git a/bulk-checkin.c b/bulk-checkin.c
index 16df86c0ba8..b2809ab0398 100644
--- a/bulk-checkin.c
+++ b/bulk-checkin.c
@@ -46,7 +46,7 @@ static void finish_tmp_packfile(struct strbuf *basename,
 	stage_tmp_packfiles(the_repository, basename, pack_tmp_name,
 			    written_list, nr_written, NULL, pack_idx_opts, hash,
 			    &idx_tmp_name);
-	rename_tmp_packfile_idx(basename, &idx_tmp_name);
+	rename_tmp_packfile_idx(the_repository, basename, &idx_tmp_name);
 
 	free(idx_tmp_name);
 }
diff --git a/http.c b/http.c
index 9b62f627dc5..7cc797116bb 100644
--- a/http.c
+++ b/http.c
@@ -2331,7 +2331,7 @@ int http_get_file(const char *url, const char *filename,
 	ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
 	fclose(result);
 
-	if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
+	if (ret == HTTP_OK && finalize_object_file(the_repository, tmpfile.buf, filename))
 		ret = HTTP_ERROR;
 cleanup:
 	strbuf_release(&tmpfile);
@@ -2815,7 +2815,7 @@ int finish_http_object_request(struct http_object_request *freq)
 		return -1;
 	}
 	odb_loose_path(the_repository->objects->sources, &filename, &freq->oid);
-	freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
+	freq->rename = finalize_object_file(the_repository, freq->tmpfile.buf, filename.buf);
 	strbuf_release(&filename);
 
 	return freq->rename;
diff --git a/midx-write.c b/midx-write.c
index f2cfb85476e..effacade2d3 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -667,7 +667,7 @@ static void write_midx_reverse_index(struct write_midx_context *ctx,
 	tmp_file = write_rev_file_order(ctx->repo, NULL, ctx->pack_order,
 					ctx->entries_nr, midx_hash, WRITE_REV);
 
-	if (finalize_object_file(tmp_file, buf.buf))
+	if (finalize_object_file(ctx->repo, tmp_file, buf.buf))
 		die(_("cannot store reverse index file"));
 
 	strbuf_release(&buf);
diff --git a/object-file.c b/object-file.c
index 800eeae85af..6a7049a9e98 100644
--- a/object-file.c
+++ b/object-file.c
@@ -584,12 +584,14 @@ static int check_collision(const char *source, const char *dest)
 /*
  * Move the just written object into its final resting place.
  */
-int finalize_object_file(const char *tmpfile, const char *filename)
+int finalize_object_file(struct repository *repo,
+			 const char *tmpfile, const char *filename)
 {
-	return finalize_object_file_flags(tmpfile, filename, 0);
+	return finalize_object_file_flags(repo, tmpfile, filename, 0);
 }
 
-int finalize_object_file_flags(const char *tmpfile, const char *filename,
+int finalize_object_file_flags(struct repository *repo,
+			       const char *tmpfile, const char *filename,
 			       enum finalize_object_file_flags flags)
 {
 	unsigned retries = 0;
@@ -649,7 +651,7 @@ int finalize_object_file_flags(const char *tmpfile, const char *filename,
 	}
 
 out:
-	if (adjust_shared_perm(the_repository, filename))
+	if (adjust_shared_perm(repo, filename))
 		return error(_("unable to set permission to '%s'"), filename);
 	return 0;
 }
@@ -889,7 +891,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
 			warning_errno(_("failed utime() on %s"), tmp_file.buf);
 	}
 
-	return finalize_object_file_flags(tmp_file.buf, filename.buf,
+	return finalize_object_file_flags(the_repository, tmp_file.buf, filename.buf,
 					  FOF_SKIP_COLLISION_CHECK);
 }
 
@@ -1020,7 +1022,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
 		strbuf_release(&dir);
 	}
 
-	err = finalize_object_file_flags(tmp_file.buf, filename.buf,
+	err = finalize_object_file_flags(the_repository, tmp_file.buf, filename.buf,
 					 FOF_SKIP_COLLISION_CHECK);
 	if (!err && compat)
 		err = repo_add_loose_object_map(the_repository, oid, &compat_oid);
diff --git a/object-file.h b/object-file.h
index 5b63a05ab51..370139e0762 100644
--- a/object-file.h
+++ b/object-file.h
@@ -218,8 +218,10 @@ enum finalize_object_file_flags {
 	FOF_SKIP_COLLISION_CHECK = 1,
 };
 
-int finalize_object_file(const char *tmpfile, const char *filename);
-int finalize_object_file_flags(const char *tmpfile, const char *filename,
+int finalize_object_file(struct repository *repo,
+			 const char *tmpfile, const char *filename);
+int finalize_object_file_flags(struct repository *repo,
+			       const char *tmpfile, const char *filename,
 			       enum finalize_object_file_flags flags);
 
 void hash_object_file(const struct git_hash_algo *algo, const void *buf,
diff --git a/pack-write.c b/pack-write.c
index eccdc798e36..83eaf88541e 100644
--- a/pack-write.c
+++ b/pack-write.c
@@ -538,22 +538,24 @@ struct hashfile *create_tmp_packfile(struct repository *repo,
 	return hashfd(repo->hash_algo, fd, *pack_tmp_name);
 }
 
-static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source,
+static void rename_tmp_packfile(struct repository *repo,
+				struct strbuf *name_prefix, const char *source,
 				const char *ext)
 {
 	size_t name_prefix_len = name_prefix->len;
 
 	strbuf_addstr(name_prefix, ext);
-	if (finalize_object_file(source, name_prefix->buf))
+	if (finalize_object_file(repo, source, name_prefix->buf))
 		die("unable to rename temporary file to '%s'",
 		    name_prefix->buf);
 	strbuf_setlen(name_prefix, name_prefix_len);
 }
 
-void rename_tmp_packfile_idx(struct strbuf *name_buffer,
+void rename_tmp_packfile_idx(struct repository *repo,
+			     struct strbuf *name_buffer,
 			     char **idx_tmp_name)
 {
-	rename_tmp_packfile(name_buffer, *idx_tmp_name, "idx");
+	rename_tmp_packfile(repo, name_buffer, *idx_tmp_name, "idx");
 }
 
 void stage_tmp_packfiles(struct repository *repo,
@@ -586,11 +588,11 @@ void stage_tmp_packfiles(struct repository *repo,
 						    hash);
 	}
 
-	rename_tmp_packfile(name_buffer, pack_tmp_name, "pack");
+	rename_tmp_packfile(repo, name_buffer, pack_tmp_name, "pack");
 	if (rev_tmp_name)
-		rename_tmp_packfile(name_buffer, rev_tmp_name, "rev");
+		rename_tmp_packfile(repo, name_buffer, rev_tmp_name, "rev");
 	if (mtimes_tmp_name)
-		rename_tmp_packfile(name_buffer, mtimes_tmp_name, "mtimes");
+		rename_tmp_packfile(repo, name_buffer, mtimes_tmp_name, "mtimes");
 
 	free(rev_tmp_name);
 	free(mtimes_tmp_name);
diff --git a/pack.h b/pack.h
index 5d4393eaffe..ec76472e49b 100644
--- a/pack.h
+++ b/pack.h
@@ -145,7 +145,8 @@ void stage_tmp_packfiles(struct repository *repo,
 			 struct pack_idx_option *pack_idx_opts,
 			 unsigned char hash[],
 			 char **idx_tmp_name);
-void rename_tmp_packfile_idx(struct strbuf *basename,
+void rename_tmp_packfile_idx(struct repository *repo,
+			     struct strbuf *basename,
 			     char **idx_tmp_name);
 
 #endif
diff --git a/tmp-objdir.c b/tmp-objdir.c
index ae01eae9c41..9f5a1788cd7 100644
--- a/tmp-objdir.c
+++ b/tmp-objdir.c
@@ -227,7 +227,7 @@ static int migrate_one(struct tmp_objdir *t,
 			return -1;
 		return migrate_paths(t, src, dst, flags);
 	}
-	return finalize_object_file_flags(src->buf, dst->buf, flags);
+	return finalize_object_file_flags(t->repo, src->buf, dst->buf, flags);
 }
 
 static int is_loose_object_shard(const char *name)
-- 
2.50.1.465.gcb3da1c9e6.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 51 of 60 in “object-file: get rid of `the_repository`”
  1. 00/19 object-file: get rid of `the_repository`Patrick Steinhardt, Jul 9, 2025
  2. 01/19 object-file: fix -Wsign-compare warningsPatrick Steinhardt, Jul 9, 2025
  3. 02/19 object-file: stop using `the_hash_algo`Patrick Steinhardt, Jul 9, 2025
  4. Karthik NayakJul 11, 2025
  5. 03/19 object-file: get rid of `the_repository` in `has_loose_object()`Patrick Steinhardt, Jul 9, 2025
  6. 04/19 object-file: inline `check_and_freshen()` functionsPatrick Steinhardt, Jul 9, 2025
  7. 05/19 object-file: get rid of `the_repository` when freshening objectsPatrick Steinhardt, Jul 9, 2025
  8. Karthik NayakJul 11, 2025
  9. 06/19 object-file: get rid of `the_repository` in `loose_object_info()`Patrick Steinhardt, Jul 9, 2025
  10. 07/19 object-file: get rid of `the_repository` in `finalize_object_file()`Patrick Steinhardt, Jul 9, 2025
  11. 08/19 loose: write loose objects map via their sourcePatrick Steinhardt, Jul 9, 2025
  12. Karthik NayakJul 11, 2025
  13. Patrick SteinhardtJul 15, 2025
  14. 09/19 odb: introduce `odb_write_object()`Patrick Steinhardt, Jul 9, 2025
  15. Toon ClaesJul 10, 2025
  16. Patrick SteinhardtJul 15, 2025
  17. 10/19 object-file: get rid of `the_repository` when writing objectsPatrick Steinhardt, Jul 9, 2025
  18. 11/19 object-file: inline `for_each_loose_file_in_objdir_buf()`Patrick Steinhardt, Jul 9, 2025
  19. 12/19 object-file: remove declaration for `for_each_file_in_obj_subdir()`Patrick Steinhardt, Jul 9, 2025
  20. 13/19 object-file: get rid of `the_repository` in loose object iteratorsPatrick Steinhardt, Jul 9, 2025
  21. Toon ClaesJul 10, 2025
  22. 14/19 object-file: get rid of `the_repository` in `read_loose_object()`Patrick Steinhardt, Jul 9, 2025
  23. 15/19 object-file: get rid of `the_repository` in `force_object_loose()`Patrick Steinhardt, Jul 9, 2025
  24. Toon ClaesJul 10, 2025
  25. Karthik NayakJul 11, 2025
  26. Patrick SteinhardtJul 15, 2025
  27. Toon ClaesJul 15, 2025
  28. 16/19 object-file: get rid of `the_repository` in index-related functionsPatrick Steinhardt, Jul 9, 2025
  29. 17/19 environment: move compression level into repo settingsPatrick Steinhardt, Jul 9, 2025
  30. Phillip WoodJul 9, 2025
  31. Junio C HamanoJul 11, 2025
  32. Patrick SteinhardtJul 15, 2025
  33. Patrick SteinhardtJul 15, 2025
  34. Phillip WoodJul 15, 2025
  35. Patrick SteinhardtJul 15, 2025
  36. Patrick SteinhardtJul 16, 2025
  37. Phillip WoodJul 17, 2025
  38. Junio C HamanoJul 17, 2025
  39. Junio C HamanoJul 15, 2025
  40. Ayush ChandekarJul 17, 2025
  41. 18/19 environment: move object creation mode into repo settingsPatrick Steinhardt, Jul 9, 2025
  42. 19/19 object-file: drop USE_THE_REPOSITORY_VARIABLEPatrick Steinhardt, Jul 9, 2025
  43. 00/16 object-file: get rid of `the_repository`Patrick Steinhardt, Jul 17, 2025
  44. 01/16 object-file: fix -Wsign-compare warningsPatrick Steinhardt, Jul 17, 2025
  45. Jeff KingApr 5, 2026
  46. 02/16 object-file: stop using `the_hash_algo`Patrick Steinhardt, Jul 17, 2025
  47. 03/16 object-file: get rid of `the_repository` in `has_loose_object()`Patrick Steinhardt, Jul 17, 2025
  48. 04/16 object-file: inline `check_and_freshen()` functionsPatrick Steinhardt, Jul 17, 2025
  49. 05/16 object-file: get rid of `the_repository` when freshening objectsPatrick Steinhardt, Jul 17, 2025
  50. 06/16 object-file: get rid of `the_repository` in `loose_object_info()`Patrick Steinhardt, Jul 17, 2025
  51. 07/16 object-file: get rid of `the_repository` in `finalize_object_file()`Patrick Steinhardt, Jul 17, 2025
  52. 08/16 loose: write loose objects map via their sourcePatrick Steinhardt, Jul 17, 2025
  53. 09/16 odb: introduce `odb_write_object()`Patrick Steinhardt, Jul 17, 2025
  54. 10/16 object-file: get rid of `the_repository` when writing objectsPatrick Steinhardt, Jul 17, 2025
  55. 11/16 object-file: inline `for_each_loose_file_in_objdir_buf()`Patrick Steinhardt, Jul 17, 2025
  56. 12/16 object-file: remove declaration for `for_each_file_in_obj_subdir()`Patrick Steinhardt, Jul 17, 2025
  57. 13/16 object-file: get rid of `the_repository` in loose object iteratorsPatrick Steinhardt, Jul 17, 2025
  58. 14/16 object-file: get rid of `the_repository` in `read_loose_object()`Patrick Steinhardt, Jul 17, 2025
  59. 15/16 object-file: get rid of `the_repository` in `force_object_loose()`Patrick Steinhardt, Jul 17, 2025
  60. 16/16 object-file: get rid of `the_repository` in index-related functionsPatrick Steinhardt, Jul 17, 2025

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.