]> git.ipfire.org Git - thirdparty/git.git/commitdiff
environment: make `get_git_common_dir()` accept a repository
authorPatrick Steinhardt <ps@pks.im>
Thu, 12 Sep 2024 11:29:27 +0000 (13:29 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 12 Sep 2024 17:15:39 +0000 (10:15 -0700)
The `get_git_common_dir()` function retrieves the path to the common
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>
13 files changed:
builtin/config.c
builtin/gc.c
builtin/rev-parse.c
builtin/worktree.c
config.c
environment.c
environment.h
repository.c
repository.h
setup.c
submodule.c
trace.c
worktree.c

index c10697a2efb342bde24d006d7c1f383b4c24e0f2..34a371414e843b8dc2b3628ef871054f2cb7fd17 100644 (file)
@@ -807,7 +807,7 @@ static void location_options_init(struct config_location_options *opts,
        else
                opts->options.respect_includes = opts->respect_includes_opt;
        if (startup_info->have_repository) {
-               opts->options.commondir = get_git_common_dir();
+               opts->options.commondir = repo_get_common_dir(the_repository);
                opts->options.git_dir = repo_get_git_dir(the_repository);
        }
 }
index 427faf1cfe1bdb66ad251e2bc040f88bc06bc0ea..0f3d74f8bd01bd7390e68c9d93c9e5f484c52f4b 100644 (file)
@@ -2132,7 +2132,7 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
        get_schedule_cmd(&cmd, NULL);
 
        strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
-                   get_git_common_dir(), frequency);
+                   repo_get_common_dir(the_repository), frequency);
        tfile = xmks_tempfile(tfilename.buf);
        strbuf_release(&tfilename);
 
index 4285dc34a7b553b808993973bb7ce2102bb85fae..cd85fe57bb05d0910d734208070b2a6da418125e 100644 (file)
@@ -19,6 +19,7 @@
 #include "path.h"
 #include "diff.h"
 #include "read-cache-ll.h"
+#include "repository.h"
 #include "revision.h"
 #include "setup.h"
 #include "split-index.h"
@@ -1042,7 +1043,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                continue;
                        }
                        if (!strcmp(arg, "--git-common-dir")) {
-                               print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
+                               print_path(repo_get_common_dir(the_repository), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
                                continue;
                        }
                        if (!strcmp(arg, "--is-inside-git-dir")) {
index 41e7f6a3271b71e23b8b4814fcc98e7320782d79..645b548bf3b721addc66258c7c0df2c2590234ea 100644 (file)
@@ -219,7 +219,7 @@ static void prune_worktrees(void)
        }
        closedir(dir);
 
-       strbuf_add_absolute_path(&main_path, get_git_common_dir());
+       strbuf_add_absolute_path(&main_path, repo_get_common_dir(the_repository));
        /* massage main worktree absolute path to match 'gitdir' content */
        strbuf_strip_suffix(&main_path, "/.");
        string_list_append_nodup(&kept, strbuf_detach(&main_path, NULL));
@@ -492,7 +492,7 @@ static int add_worktree(const char *path, const char *refname,
        strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
        strbuf_realpath(&realpath, sb_git.buf, 1);
        write_file(sb.buf, "%s", realpath.buf);
-       strbuf_realpath(&realpath, get_git_common_dir(), 1);
+       strbuf_realpath(&realpath, repo_get_common_dir(the_repository), 1);
        write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
                   realpath.buf, name);
        strbuf_reset(&sb);
index 1733ba85dcd02d5f2a56336a4d0903ad70dad9ef..0b87f0f90507b7a3391c4803166df2c9ba122d49 100644 (file)
--- a/config.c
+++ b/config.c
@@ -2213,7 +2213,7 @@ void read_early_config(config_fn_t cb, void *data)
        opts.respect_includes = 1;
 
        if (have_git_dir()) {
-               opts.commondir = get_git_common_dir();
+               opts.commondir = repo_get_common_dir(the_repository);
                opts.git_dir = repo_get_git_dir(the_repository);
        /*
         * When setup_git_directory() was not yet asked to discover the
index 040b1ff1ba803e1f428bc440f066b3807ce892b4..7c4a142ca2533816e5aa24498aaf836f5ef6bf21 100644 (file)
@@ -228,13 +228,6 @@ int have_git_dir(void)
                || the_repository->gitdir;
 }
 
-const char *get_git_common_dir(void)
-{
-       if (!the_repository->commondir)
-               BUG("git environment hasn't been setup");
-       return the_repository->commondir;
-}
-
 const char *get_git_namespace(void)
 {
        if (!git_namespace)
index 06d37d5c82b3aa9f01e85174b71464fcf0428502..d778614158f06fd552b05375fef2bb33193fb108 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_git_common_dir(void);
 const char *get_object_directory(void);
 char *get_index_file(void);
 char *get_graft_file(struct repository *r);
index 31afc62551ecc53849f7fc6458f4016a365e9677..c8dcba1997a3050b7e3235f1b4c782ecde09c419 100644 (file)
@@ -98,6 +98,13 @@ const char *repo_get_git_dir(struct repository *repo)
        return repo->gitdir;
 }
 
+const char *repo_get_common_dir(struct repository *repo)
+{
+       if (!repo->commondir)
+               BUG("repository hasn't been set up");
+       return repo->commondir;
+}
+
 static void repo_set_commondir(struct repository *repo,
                               const char *commondir)
 {
index cf2172c0aa57f1bcfa13b3288bdc26bb6e092352..404435ad029df830aa4328b0679e9f7bdd360899 100644 (file)
@@ -207,6 +207,7 @@ extern struct repository *the_repository;
 #endif
 
 const char *repo_get_git_dir(struct repository *repo);
+const char *repo_get_common_dir(struct repository *repo);
 
 /*
  * Define a custom repository layout. Any field can be NULL, which
diff --git a/setup.c b/setup.c
index 4a9c60922e7615ea81945c215fb4f8764e1afced..fe4a5dfc43b977addd1a7439affbb76e34125146 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -2068,7 +2068,7 @@ static void copy_templates(const char *option_template)
                goto close_free_return;
        }
 
-       strbuf_addstr(&path, get_git_common_dir());
+       strbuf_addstr(&path, repo_get_common_dir(the_repository));
        strbuf_complete(&path, '/');
        copy_templates_1(&path, &template_path, dir);
 close_free_return:
index 97516b0fec10d8c5f0df1b5d13b108d26b5b5a3f..c7d164a31abf5ceba54f00e19e14d127eaae7c33 100644 (file)
@@ -2462,7 +2462,7 @@ void absorb_git_dir_into_superproject(const char *path,
        } else {
                /* Is it already absorbed into the superprojects git dir? */
                char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
-               char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
+               char *real_common_git_dir = real_pathdup(repo_get_common_dir(the_repository), 1);
 
                if (!starts_with(real_sub_git_dir, real_common_git_dir))
                        relocate_single_git_dir_into_superproject(path, super_prefix);
diff --git a/trace.c b/trace.c
index 32c5cda7afd4c00c244fe71ca9f5487ac857d291..e6728c301f3963378590b65c94f22cf59c7f64e6 100644 (file)
--- a/trace.c
+++ b/trace.c
@@ -315,7 +315,7 @@ void trace_repo_setup(void)
                prefix = "(null)";
 
        trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(repo_get_git_dir(the_repository)));
-       trace_printf_key(&trace_setup_key, "setup: git_common_dir: %s\n", quote_crnl(get_git_common_dir()));
+       trace_printf_key(&trace_setup_key, "setup: git_common_dir: %s\n", quote_crnl(repo_get_common_dir(the_repository)));
        trace_printf_key(&trace_setup_key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
        trace_printf_key(&trace_setup_key, "setup: cwd: %s\n", quote_crnl(cwd));
        trace_printf_key(&trace_setup_key, "setup: prefix: %s\n", quote_crnl(prefix));
index 11335c5d9a3f48afc46fabb95c7edb204c22cbc6..0f032ccedffe636e1db2717e713663de4976963e 100644 (file)
@@ -72,7 +72,7 @@ static struct worktree *get_main_worktree(int skip_reading_head)
        struct worktree *worktree = NULL;
        struct strbuf worktree_path = STRBUF_INIT;
 
-       strbuf_add_real_path(&worktree_path, get_git_common_dir());
+       strbuf_add_real_path(&worktree_path, repo_get_common_dir(the_repository));
        strbuf_strip_suffix(&worktree_path, "/.git");
 
        CALLOC_ARRAY(worktree, 1);
@@ -143,7 +143,7 @@ static struct worktree **get_worktrees_internal(int skip_reading_head)
 
        list[counter++] = get_main_worktree(skip_reading_head);
 
-       strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
+       strbuf_addf(&path, "%s/worktrees", repo_get_common_dir(the_repository));
        dir = opendir(path.buf);
        strbuf_release(&path);
        if (dir) {
@@ -173,7 +173,7 @@ const char *get_worktree_git_dir(const struct worktree *wt)
        if (!wt)
                return repo_get_git_dir(the_repository);
        else if (!wt->id)
-               return get_git_common_dir();
+               return repo_get_common_dir(the_repository);
        else
                return git_common_path("worktrees/%s", wt->id);
 }
@@ -626,7 +626,7 @@ static int is_main_worktree_path(const char *path)
 
        strbuf_add_real_path(&target, path);
        strbuf_strip_suffix(&target, "/.git");
-       strbuf_add_real_path(&maindir, get_git_common_dir());
+       strbuf_add_real_path(&maindir, repo_get_common_dir(the_repository));
        strbuf_strip_suffix(&maindir, "/.git");
        cmp = fspathcmp(maindir.buf, target.buf);