]> Kevux Git Server - rit/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 b1389c5..89f5789 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 19c67a8..dabeb82 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 a44f0ce..e8e85d8 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 16df86c..b2809ab 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 9b62f62..7cc7971 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 f2cfb85..effacad 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 800eeae..6a7049a 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 5b63a05..370139e 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 eccdc79..83eaf88 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 5d4393e..ec76472 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 ae01eae..9f5a178 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)