]> git.ipfire.org Git - thirdparty/git.git/commitdiff
environment: make `get_object_directory()` accept a repository
authorPatrick Steinhardt <ps@pks.im>
Thu, 12 Sep 2024 11:29:30 +0000 (13:29 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 12 Sep 2024 17:15:39 +0000 (10:15 -0700)
The `get_object_directory()` function retrieves the path to the object
directory for `the_repository`. Make it accept a `struct repository`
such that it can work on arbitrary repositories and make it part of the
repository subsystem. This reduces our reliance on `the_repository` and
clarifies scope.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
20 files changed:
builtin/commit-graph.c
builtin/count-objects.c
builtin/multi-pack-index.c
builtin/pack-objects.c
builtin/prune.c
builtin/repack.c
bulk-checkin.c
environment.c
environment.h
fetch-pack.c
http-backend.c
object-file.c
pack-write.c
packfile.c
prune-packed.c
repository.c
repository.h
server-info.c
setup.c
tmp-objdir.c

index 7102ee90a0094b12560108eebe38f2637f83e701..7411e6244f2b100c7c20d048b46aaef388c5f412 100644 (file)
@@ -1,7 +1,6 @@
 #include "builtin.h"
 #include "commit.h"
 #include "config.h"
-#include "environment.h"
 #include "gettext.h"
 #include "hex.h"
 #include "parse-options.h"
@@ -95,7 +94,7 @@ static int graph_verify(int argc, const char **argv, const char *prefix)
                usage_with_options(builtin_commit_graph_verify_usage, options);
 
        if (!opts.obj_dir)
-               opts.obj_dir = get_object_directory();
+               opts.obj_dir = repo_get_object_directory(the_repository);
        if (opts.shallow)
                flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
        if (opts.progress)
@@ -275,7 +274,7 @@ static int graph_write(int argc, const char **argv, const char *prefix)
        if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
                die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
        if (!opts.obj_dir)
-               opts.obj_dir = get_object_directory();
+               opts.obj_dir = repo_get_object_directory(the_repository);
        if (opts.append)
                flags |= COMMIT_GRAPH_WRITE_APPEND;
        if (opts.split)
index ec6098a149d0e82be59f113935b61dfe55b4fc6c..42275f62d591b8224cdb6be409a36488c733d5b5 100644 (file)
@@ -7,7 +7,6 @@
 #include "builtin.h"
 #include "config.h"
 #include "dir.h"
-#include "environment.h"
 #include "gettext.h"
 #include "path.h"
 #include "repository.h"
@@ -116,7 +115,7 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
                report_linked_checkout_garbage(the_repository);
        }
 
-       for_each_loose_file_in_objdir(get_object_directory(),
+       for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
                                      count_loose, count_cruft, NULL, NULL);
 
        if (verbose) {
index 8805cbbeb3be3e10e073335af890f39daef6e003..55289e989dfdd9ac03718a1b117f2e5024fbc28e 100644 (file)
@@ -1,7 +1,6 @@
 #include "builtin.h"
 #include "abspath.h"
 #include "config.h"
-#include "environment.h"
 #include "gettext.h"
 #include "parse-options.h"
 #include "midx.h"
@@ -9,6 +8,7 @@
 #include "trace2.h"
 #include "object-store-ll.h"
 #include "replace-object.h"
+#include "repository.h"
 
 #define BUILTIN_MIDX_WRITE_USAGE \
        N_("git multi-pack-index [<options>] write [--preferred-pack=<pack>]" \
@@ -63,7 +63,7 @@ static int parse_object_dir(const struct option *opt, const char *arg,
        char **value = opt->value;
        free(*value);
        if (unset)
-               *value = xstrdup(get_object_directory());
+               *value = xstrdup(repo_get_object_directory(the_repository));
        else
                *value = real_pathdup(arg, 1);
        return 0;
index 778be80f5646d137de9ab83ee68a966d71c64858..44341b206d4b54d303049e66a061151d60729f78 100644 (file)
@@ -3940,7 +3940,7 @@ static int add_loose_object(const struct object_id *oid, const char *path,
  */
 static void add_unreachable_loose_objects(void)
 {
-       for_each_loose_file_in_objdir(get_object_directory(),
+       for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
                                      add_loose_object,
                                      NULL, NULL, NULL);
 }
index 57fe31467fe5ac9768f1b04f7c7e6dd3e9409468..47eeabbd13a1f6aaf4d17afb8fd72475ae6227ee 100644 (file)
@@ -193,12 +193,12 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
                revs.exclude_promisor_objects = 1;
        }
 
-       for_each_loose_file_in_objdir(get_object_directory(), prune_object,
-                                     prune_cruft, prune_subdir, &revs);
+       for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
+                                     prune_object, prune_cruft, prune_subdir, &revs);
 
        prune_packed_objects(show_only ? PRUNE_PACKED_DRY_RUN : 0);
-       remove_temporary_files(get_object_directory());
-       s = mkpathdup("%s/pack", get_object_directory());
+       remove_temporary_files(repo_get_object_directory(the_repository));
+       s = mkpathdup("%s/pack", repo_get_object_directory(the_repository));
        remove_temporary_files(s);
        free(s);
 
index 62cfa50c50f893406803cbbedef711f35167cb14..40feacb73f8712e33f723b7cf230abef6902e14d 100644 (file)
@@ -1240,7 +1240,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
        if (write_midx && write_bitmaps) {
                struct strbuf path = STRBUF_INIT;
 
-               strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
+               strbuf_addf(&path, "%s/%s_XXXXXX", repo_get_object_directory(the_repository),
                            "bitmap-ref-tips");
 
                refs_snapshot = xmks_tempfile(path.buf);
@@ -1249,7 +1249,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
                strbuf_release(&path);
        }
 
-       packdir = mkpathdup("%s/pack", get_object_directory());
+       packdir = mkpathdup("%s/pack", repo_get_object_directory(the_repository));
        packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
        packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
 
@@ -1519,7 +1519,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
                unsigned flags = 0;
                if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
                        flags |= MIDX_WRITE_INCREMENTAL;
-               write_midx_file(get_object_directory(), NULL, NULL, flags);
+               write_midx_file(repo_get_object_directory(the_repository),
+                               NULL, NULL, flags);
        }
 
 cleanup:
index 9089c214fa4e8f6d2120fe0d6e9dca262bcdbe76..2753d5bbe4af3d1cae43df41426a3ee4a6974f41 100644 (file)
@@ -75,7 +75,7 @@ static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state)
                close(fd);
        }
 
-       strbuf_addf(&packname, "%s/pack/pack-%s.", get_object_directory(),
+       strbuf_addf(&packname, "%s/pack/pack-%s.", repo_get_object_directory(the_repository),
                    hash_to_hex(hash));
        finish_tmp_packfile(&packname, state->pack_tmp_name,
                            state->written, state->nr_written,
@@ -113,7 +113,7 @@ static void flush_batch_fsync(void)
         * to ensure that the data in each new object file is durable before
         * the final name is visible.
         */
-       strbuf_addf(&temp_path, "%s/bulk_fsync_XXXXXX", get_object_directory());
+       strbuf_addf(&temp_path, "%s/bulk_fsync_XXXXXX", repo_get_object_directory(the_repository));
        temp = xmks_tempfile(temp_path.buf);
        fsync_or_die(get_tempfile_fd(temp), get_tempfile_path(temp));
        delete_tempfile(&temp);
index 7c4a142ca2533816e5aa24498aaf836f5ef6bf21..0a2057399e0305a8437bce6978bf691c1e482149 100644 (file)
@@ -273,13 +273,6 @@ const char *get_git_work_tree(void)
        return the_repository->worktree;
 }
 
-const char *get_object_directory(void)
-{
-       if (!the_repository->objects->odb)
-               BUG("git environment hasn't been setup");
-       return the_repository->objects->odb->path;
-}
-
 int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
 {
        int fd;
index d778614158f06fd552b05375fef2bb33193fb108..91125d829915ad0a309e031b7d3587bd0d74354d 100644 (file)
@@ -106,7 +106,6 @@ int have_git_dir(void);
 extern int is_bare_repository_cfg;
 int is_bare_repository(void);
 extern char *git_work_tree_cfg;
-const char *get_object_directory(void);
 char *get_index_file(void);
 char *get_graft_file(struct repository *r);
 void set_git_dir(const char *path, int make_realpath);
index 58b4581ad80b15a9feda3f3dfcbd3548723cd113..fddb90f2e78eed5babc5b0f0412c39fb93a93d3d 100644 (file)
@@ -1839,7 +1839,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 
                string_list_append_nodup(pack_lockfiles,
                                         xstrfmt("%s/pack/pack-%s.keep",
-                                                get_object_directory(),
+                                                repo_get_object_directory(the_repository),
                                                 packname));
        }
        string_list_clear(&packfile_uris, 0);
index 79ce097359b19db55579808f1f43e103738b0abc..73eec4ea3d844345ab607488490227d3dbacc02a 100644 (file)
@@ -601,7 +601,7 @@ static void get_head(struct strbuf *hdr, char *arg UNUSED)
 
 static void get_info_packs(struct strbuf *hdr, char *arg UNUSED)
 {
-       size_t objdirlen = strlen(get_object_directory());
+       size_t objdirlen = strlen(repo_get_object_directory(the_repository));
        struct strbuf buf = STRBUF_INIT;
        struct packed_git *p;
        size_t cnt = 0;
index c5994202ba0bad5c5bdcc0f5e92c4ab0e00c0116..fa4121b98ad121a957c2437205b13c894e236a6c 100644 (file)
@@ -2053,7 +2053,7 @@ static int start_loose_object_common(struct strbuf *tmp_file,
                else if (errno == EACCES)
                        return error(_("insufficient permission for adding "
                                       "an object to repository database %s"),
-                                    get_object_directory());
+                                    repo_get_object_directory(the_repository));
                else
                        return error_errno(
                                _("unable to create temporary file"));
@@ -2228,7 +2228,7 @@ int stream_loose_object(struct input_stream *in_stream, size_t len,
                prepare_loose_object_bulk_checkin();
 
        /* Since oid is not determined, save tmp file to odb path. */
-       strbuf_addf(&filename, "%s/", get_object_directory());
+       strbuf_addf(&filename, "%s/", repo_get_object_directory(the_repository));
        hdrlen = format_object_header(hdr, sizeof(hdr), OBJ_BLOB, len);
 
        /*
index d07f03d0ab060ac3c698adf265c100864849bd76..27965672f17822c342206b1c2518f76debd2b012 100644 (file)
@@ -12,6 +12,7 @@
 #include "pack-objects.h"
 #include "pack-revindex.h"
 #include "path.h"
+#include "repository.h"
 #include "strbuf.h"
 
 void reset_pack_idx_option(struct pack_idx_option *opts)
@@ -473,7 +474,7 @@ char *index_pack_lockfile(int ip_out, int *is_well_formed)
                packname[len-1] = 0;
                if (skip_prefix(packname, "keep\t", &name))
                        return xstrfmt("%s/pack/pack-%s.keep",
-                                      get_object_directory(), name);
+                                      repo_get_object_directory(the_repository), name);
                return NULL;
        }
        if (is_well_formed)
index cf12a539eac720c17413cd6122b3ed408299c701..df4ba677197193160156a77c4ab28330d900e882 100644 (file)
@@ -30,7 +30,7 @@ char *odb_pack_name(struct strbuf *buf,
                    const char *ext)
 {
        strbuf_reset(buf);
-       strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
+       strbuf_addf(buf, "%s/pack/pack-%s.%s", repo_get_object_directory(the_repository),
                    hash_to_hex(hash), ext);
        return buf->buf;
 }
index e54daf740a26345a182eb67d7ab2716916e41c37..2bb99c29dfb3c095f183fed97ac7a87d166fc790 100644 (file)
@@ -1,10 +1,12 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
-#include "environment.h"
 #include "gettext.h"
 #include "object-store-ll.h"
 #include "packfile.h"
 #include "progress.h"
 #include "prune-packed.h"
+#include "repository.h"
 
 static struct progress *progress;
 
@@ -37,7 +39,7 @@ void prune_packed_objects(int opts)
        if (opts & PRUNE_PACKED_VERBOSE)
                progress = start_delayed_progress(_("Removing duplicate objects"), 256);
 
-       for_each_loose_file_in_objdir(get_object_directory(),
+       for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
                                      prune_object, NULL, prune_subdir, &opts);
 
        /* Ensure we show 100% before finishing progress */
index c8dcba1997a3050b7e3235f1b4c782ecde09c419..49c42c25daaf9e0b6a6fd96e2a9bed1a92bdc4a1 100644 (file)
@@ -105,6 +105,13 @@ const char *repo_get_common_dir(struct repository *repo)
        return repo->commondir;
 }
 
+const char *repo_get_object_directory(struct repository *repo)
+{
+       if (!repo->objects->odb)
+               BUG("repository hasn't been set up");
+       return repo->objects->odb->path;
+}
+
 static void repo_set_commondir(struct repository *repo,
                               const char *commondir)
 {
index 404435ad029df830aa4328b0679e9f7bdd360899..778f1511ab1500d3d0edbbef3afc20e5c6e8e734 100644 (file)
@@ -208,6 +208,7 @@ extern struct repository *the_repository;
 
 const char *repo_get_git_dir(struct repository *repo);
 const char *repo_get_common_dir(struct repository *repo);
+const char *repo_get_object_directory(struct repository *repo);
 
 /*
  * Define a custom repository layout. Any field can be NULL, which
index 1508fa6f825ec7559ffca9e184c23c9fcbf914ad..c5af4cd98a6696a15cda42a4a41a76bb729db93a 100644 (file)
@@ -2,7 +2,6 @@
 
 #include "git-compat-util.h"
 #include "dir.h"
-#include "environment.h"
 #include "hex.h"
 #include "repository.h"
 #include "refs.h"
@@ -342,7 +341,8 @@ static int write_pack_info_file(struct update_info_ctx *uic)
 
 static int update_info_packs(int force)
 {
-       char *infofile = mkpathdup("%s/info/packs", get_object_directory());
+       char *infofile = mkpathdup("%s/info/packs",
+                                  repo_get_object_directory(the_repository));
        int ret;
 
        init_pack_info(infofile, force);
diff --git a/setup.c b/setup.c
index fe4a5dfc43b977addd1a7439affbb76e34125146..1ebcab625fe642c688a449b2d88f2c8c3e2fa21d 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -2282,7 +2282,7 @@ static void create_object_directory(void)
        struct strbuf path = STRBUF_INIT;
        size_t baselen;
 
-       strbuf_addstr(&path, get_object_directory());
+       strbuf_addstr(&path, repo_get_object_directory(the_repository));
        baselen = path.len;
 
        safe_create_dir(path.buf, 1);
index a8e4553f2749bad1df72269b625d22a7fbd5b2b2..c2fb9f91930a30bdbd813112517d7c9eafde1858 100644 (file)
@@ -13,6 +13,7 @@
 #include "strvec.h"
 #include "quote.h"
 #include "object-store-ll.h"
+#include "repository.h"
 
 struct tmp_objdir {
        struct strbuf path;
@@ -132,7 +133,8 @@ struct tmp_objdir *tmp_objdir_create(const char *prefix)
         * can recognize any stale objdirs left behind by a crash and delete
         * them.
         */
-       strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX", get_object_directory(), prefix);
+       strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX",
+                   repo_get_object_directory(the_repository), prefix);
 
        if (!mkdtemp(t->path.buf)) {
                /* free, not destroy, as we never touched the filesystem */
@@ -152,7 +154,7 @@ struct tmp_objdir *tmp_objdir_create(const char *prefix)
        }
 
        env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
-                  absolute_path(get_object_directory()));
+                  absolute_path(repo_get_object_directory(the_repository)));
        env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
        env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
                    absolute_path(t->path.buf));
@@ -267,7 +269,7 @@ int tmp_objdir_migrate(struct tmp_objdir *t)
        }
 
        strbuf_addbuf(&src, &t->path);
-       strbuf_addstr(&dst, get_object_directory());
+       strbuf_addstr(&dst, repo_get_object_directory(the_repository));
 
        ret = migrate_paths(&src, &dst);