]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-file: get rid of `the_repository` in `finalize_object_file()`
authorPatrick Steinhardt <ps@pks.im>
Thu, 17 Jul 2025 04:56:33 +0000 (06:56 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 17 Jul 2025 05:16:14 +0000 (22:16 -0700)
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>
builtin/fast-import.c
builtin/index-pack.c
builtin/pack-objects.c
bulk-checkin.c
http.c
midx-write.c
object-file.c
object-file.h
pack-write.c
pack.h
tmp-objdir.c

index b1389c59211a485d605f208945183a30d8a73e35..89f57898b15da67c0e66284b224befda00174d93 100644 (file)
@@ -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);
index 19c67a8534414a700604488dddc603194d1494ba..dabeb825a6c3fd23d1985159ded87b6a85d0b521 100644 (file)
@@ -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) {
index a44f0ce1c785edfa2c8e7fcefb13c1b6444a4e58..e8e85d8278bba682a872916cff99d256eed9fce3 100644 (file)
@@ -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);
index 16df86c0ba89db6a33e7425c54737a5896cfc3c5..b2809ab0398136ea0bd18b5d1c90f6a5729415ab 100644 (file)
@@ -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 9b62f627dc576efb7ee38f5089684aec758d5d54..7cc797116bbc173c66cb8a9551d13c4979ee6c59 100644 (file)
--- 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;
index f2cfb85476ed78b340693c86ab4099ab3fa8186c..effacade2d3c007853a8172005781f6f9cc35979 100644 (file)
@@ -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);
index 800eeae85af1ededb06f4c8117087c03978a7903..6a7049a9e9819c0d4c47f58c2789bc9691a05fb0 100644 (file)
@@ -584,12 +584,14 @@ out:
 /*
  * 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 @@ retry:
        }
 
 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);
index 5b63a05ab515ed896c1e4e57273c47ac114afa48..370139e0762f1ba0c96cff73f9c6d61475f96ab8 100644 (file)
@@ -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,
index eccdc798e368c310bb49795a2929b5676d7a70e0..83eaf88541eefb79e3933cb54783182cbddf88f6 100644 (file)
@@ -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 5d4393eaffef043c18b60eb473ed3e75a375d545..ec76472e49b0528df732c98a5b0fa2f5dc85b603 100644 (file)
--- 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
index ae01eae9c415d3967ee19ff2d55367cb07b950d0..9f5a1788cd7c488c8dc702b00dcda8e301d6a3a2 100644 (file)
@@ -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)