object-file: get rid of `the_repository` in `finalize_object_file()`

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 17, 2025 at 06:56 UTC cbb388f3e53660c88220c40a8dddb976672ae03d
11 files changed +32 -25
builtin/fast-import.c
+2 -2
@@ -821,11 +821,11 @@ static char *keep_pack(const char *curr_index_name)
821 die_errno("failed to write keep file");
822
823 odb_pack_name(pack_data->repo, &name, pack_data->hash, "pack");
824 - if (finalize_object_file(pack_data->pack_name, name.buf))
824 + if (finalize_object_file(pack_data->repo, pack_data->pack_name, name.buf))
825 die("cannot store pack file");
826
827 odb_pack_name(pack_data->repo, &name, pack_data->hash, "idx");
828 - if (finalize_object_file(curr_index_name, name.buf))
828 + if (finalize_object_file(pack_data->repo, curr_index_name, name.buf))
829 die("cannot store index file");
830 free((void *)curr_index_name);
831 return strbuf_detach(&name, NULL);
builtin/index-pack.c
+1 -1
@@ -1598,7 +1598,7 @@ static void rename_tmp_packfile(const char **final_name,
1598 if (!*final_name || strcmp(*final_name, curr_name)) {
1599 if (!*final_name)
1600 *final_name = odb_pack_name(the_repository, name, hash, ext);
1601 - if (finalize_object_file(curr_name, *final_name))
1601 + if (finalize_object_file(the_repository, curr_name, *final_name))
1602 die(_("unable to rename temporary '*.%s' file to '%s'"),
1603 ext, *final_name);
1604 } else if (make_read_only_if_same) {
builtin/pack-objects.c
+1 -1
@@ -1449,7 +1449,7 @@ static void write_pack_file(void)
1449 strbuf_setlen(&tmpname, tmpname_len);
1450 }
1451
1452 - rename_tmp_packfile_idx(&tmpname, &idx_tmp_name);
1452 + rename_tmp_packfile_idx(the_repository, &tmpname, &idx_tmp_name);
1453
1454 free(idx_tmp_name);
1455 strbuf_release(&tmpname);
bulk-checkin.c
+1 -1
@@ -46,7 +46,7 @@ static void finish_tmp_packfile(struct strbuf *basename,
46 stage_tmp_packfiles(the_repository, basename, pack_tmp_name,
47 written_list, nr_written, NULL, pack_idx_opts, hash,
48 &idx_tmp_name);
49 - rename_tmp_packfile_idx(basename, &idx_tmp_name);
49 + rename_tmp_packfile_idx(the_repository, basename, &idx_tmp_name);
50
51 free(idx_tmp_name);
52 }
http.c
+2 -2
@@ -2331,7 +2331,7 @@ int http_get_file(const char *url, const char *filename,
2331 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
2332 fclose(result);
2333
2334 - if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
2334 + if (ret == HTTP_OK && finalize_object_file(the_repository, tmpfile.buf, filename))
2335 ret = HTTP_ERROR;
2336 cleanup:
2337 strbuf_release(&tmpfile);
@@ -2815,7 +2815,7 @@ int finish_http_object_request(struct http_object_request *freq)
2815 return -1;
2816 }
2817 odb_loose_path(the_repository->objects->sources, &filename, &freq->oid);
2818 - freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
2818 + freq->rename = finalize_object_file(the_repository, freq->tmpfile.buf, filename.buf);
2819 strbuf_release(&filename);
2820
2821 return freq->rename;
midx-write.c
+1 -1
@@ -667,7 +667,7 @@ static void write_midx_reverse_index(struct write_midx_context *ctx,
667 tmp_file = write_rev_file_order(ctx->repo, NULL, ctx->pack_order,
668 ctx->entries_nr, midx_hash, WRITE_REV);
669
670 - if (finalize_object_file(tmp_file, buf.buf))
670 + if (finalize_object_file(ctx->repo, tmp_file, buf.buf))
671 die(_("cannot store reverse index file"));
672
673 strbuf_release(&buf);
object-file.c
+8 -6
@@ -584,12 +584,14 @@ out:
584 /*
585 * Move the just written object into its final resting place.
586 */
587 -int finalize_object_file(const char *tmpfile, const char *filename)
587 +int finalize_object_file(struct repository *repo,
588 + const char *tmpfile, const char *filename)
589 {
589 - return finalize_object_file_flags(tmpfile, filename, 0);
590 + return finalize_object_file_flags(repo, tmpfile, filename, 0);
591 }
592
592 -int finalize_object_file_flags(const char *tmpfile, const char *filename,
593 +int finalize_object_file_flags(struct repository *repo,
594 + const char *tmpfile, const char *filename,
595 enum finalize_object_file_flags flags)
596 {
597 unsigned retries = 0;
@@ -649,7 +651,7 @@ retry:
651 }
652
653 out:
652 - if (adjust_shared_perm(the_repository, filename))
654 + if (adjust_shared_perm(repo, filename))
655 return error(_("unable to set permission to '%s'"), filename);
656 return 0;
657 }
@@ -889,7 +891,7 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
891 warning_errno(_("failed utime() on %s"), tmp_file.buf);
892 }
893
892 - return finalize_object_file_flags(tmp_file.buf, filename.buf,
894 + return finalize_object_file_flags(the_repository, tmp_file.buf, filename.buf,
895 FOF_SKIP_COLLISION_CHECK);
896 }
897
@@ -1020,7 +1022,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
1022 strbuf_release(&dir);
1023 }
1024
1023 - err = finalize_object_file_flags(tmp_file.buf, filename.buf,
1025 + err = finalize_object_file_flags(the_repository, tmp_file.buf, filename.buf,
1026 FOF_SKIP_COLLISION_CHECK);
1027 if (!err && compat)
1028 err = repo_add_loose_object_map(the_repository, oid, &compat_oid);
object-file.h
+4 -2
@@ -218,8 +218,10 @@ enum finalize_object_file_flags {
218 FOF_SKIP_COLLISION_CHECK = 1,
219 };
220
221 -int finalize_object_file(const char *tmpfile, const char *filename);
222 -int finalize_object_file_flags(const char *tmpfile, const char *filename,
221 +int finalize_object_file(struct repository *repo,
222 + const char *tmpfile, const char *filename);
223 +int finalize_object_file_flags(struct repository *repo,
224 + const char *tmpfile, const char *filename,
225 enum finalize_object_file_flags flags);
226
227 void hash_object_file(const struct git_hash_algo *algo, const void *buf,
pack-write.c
+9 -7
@@ -538,22 +538,24 @@ struct hashfile *create_tmp_packfile(struct repository *repo,
538 return hashfd(repo->hash_algo, fd, *pack_tmp_name);
539 }
540
541 -static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source,
541 +static void rename_tmp_packfile(struct repository *repo,
542 + struct strbuf *name_prefix, const char *source,
543 const char *ext)
544 {
545 size_t name_prefix_len = name_prefix->len;
546
547 strbuf_addstr(name_prefix, ext);
547 - if (finalize_object_file(source, name_prefix->buf))
548 + if (finalize_object_file(repo, source, name_prefix->buf))
549 die("unable to rename temporary file to '%s'",
550 name_prefix->buf);
551 strbuf_setlen(name_prefix, name_prefix_len);
552 }
553
553 -void rename_tmp_packfile_idx(struct strbuf *name_buffer,
554 +void rename_tmp_packfile_idx(struct repository *repo,
555 + struct strbuf *name_buffer,
556 char **idx_tmp_name)
557 {
556 - rename_tmp_packfile(name_buffer, *idx_tmp_name, "idx");
558 + rename_tmp_packfile(repo, name_buffer, *idx_tmp_name, "idx");
559 }
560
561 void stage_tmp_packfiles(struct repository *repo,
@@ -586,11 +588,11 @@ void stage_tmp_packfiles(struct repository *repo,
588 hash);
589 }
590
589 - rename_tmp_packfile(name_buffer, pack_tmp_name, "pack");
591 + rename_tmp_packfile(repo, name_buffer, pack_tmp_name, "pack");
592 if (rev_tmp_name)
591 - rename_tmp_packfile(name_buffer, rev_tmp_name, "rev");
593 + rename_tmp_packfile(repo, name_buffer, rev_tmp_name, "rev");
594 if (mtimes_tmp_name)
593 - rename_tmp_packfile(name_buffer, mtimes_tmp_name, "mtimes");
595 + rename_tmp_packfile(repo, name_buffer, mtimes_tmp_name, "mtimes");
596
597 free(rev_tmp_name);
598 free(mtimes_tmp_name);
pack.h
+2 -1
@@ -145,7 +145,8 @@ void stage_tmp_packfiles(struct repository *repo,
145 struct pack_idx_option *pack_idx_opts,
146 unsigned char hash[],
147 char **idx_tmp_name);
148 -void rename_tmp_packfile_idx(struct strbuf *basename,
148 +void rename_tmp_packfile_idx(struct repository *repo,
149 + struct strbuf *basename,
150 char **idx_tmp_name);
151
152 #endif
tmp-objdir.c
+1 -1
@@ -227,7 +227,7 @@ static int migrate_one(struct tmp_objdir *t,
227 return -1;
228 return migrate_paths(t, src, dst, flags);
229 }
230 - return finalize_object_file_flags(src->buf, dst->buf, flags);
230 + return finalize_object_file_flags(t->repo, src->buf, dst->buf, flags);
231 }
232
233 static int is_loose_object_shard(const char *name)