environment: make `get_git_common_dir()` accept a repository

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>

Patrick Steinhardt committed Sep 12, 2024 at 13:29 UTC 661624a4f6299b44f56df162100fdca528c119c1
13 files changed +22 -21
builtin/config.c
+1 -1
@@ -807,7 +807,7 @@ static void location_options_init(struct config_location_options *opts,
807 else
808 opts->options.respect_includes = opts->respect_includes_opt;
809 if (startup_info->have_repository) {
810 - opts->options.commondir = get_git_common_dir();
810 + opts->options.commondir = repo_get_common_dir(the_repository);
811 opts->options.git_dir = repo_get_git_dir(the_repository);
812 }
813 }
builtin/gc.c
+1 -1
@@ -2132,7 +2132,7 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
2132 get_schedule_cmd(&cmd, NULL);
2133
2134 strbuf_addf(&tfilename, "%s/schedule_%s_XXXXXX",
2135 - get_git_common_dir(), frequency);
2135 + repo_get_common_dir(the_repository), frequency);
2136 tfile = xmks_tempfile(tfilename.buf);
2137 strbuf_release(&tfilename);
2138
builtin/rev-parse.c
+2 -1
@@ -19,6 +19,7 @@
19 #include "path.h"
20 #include "diff.h"
21 #include "read-cache-ll.h"
22 +#include "repository.h"
23 #include "revision.h"
24 #include "setup.h"
25 #include "split-index.h"
@@ -1042,7 +1043,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
1043 continue;
1044 }
1045 if (!strcmp(arg, "--git-common-dir")) {
1045 - print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
1046 + print_path(repo_get_common_dir(the_repository), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
1047 continue;
1048 }
1049 if (!strcmp(arg, "--is-inside-git-dir")) {
builtin/worktree.c
+2 -2
@@ -219,7 +219,7 @@ static void prune_worktrees(void)
219 }
220 closedir(dir);
221
222 - strbuf_add_absolute_path(&main_path, get_git_common_dir());
222 + strbuf_add_absolute_path(&main_path, repo_get_common_dir(the_repository));
223 /* massage main worktree absolute path to match 'gitdir' content */
224 strbuf_strip_suffix(&main_path, "/.");
225 string_list_append_nodup(&kept, strbuf_detach(&main_path, NULL));
@@ -492,7 +492,7 @@ static int add_worktree(const char *path, const char *refname,
492 strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
493 strbuf_realpath(&realpath, sb_git.buf, 1);
494 write_file(sb.buf, "%s", realpath.buf);
495 - strbuf_realpath(&realpath, get_git_common_dir(), 1);
495 + strbuf_realpath(&realpath, repo_get_common_dir(the_repository), 1);
496 write_file(sb_git.buf, "gitdir: %s/worktrees/%s",
497 realpath.buf, name);
498 strbuf_reset(&sb);
config.c
+1 -1
@@ -2213,7 +2213,7 @@ void read_early_config(config_fn_t cb, void *data)
2213 opts.respect_includes = 1;
2214
2215 if (have_git_dir()) {
2216 - opts.commondir = get_git_common_dir();
2216 + opts.commondir = repo_get_common_dir(the_repository);
2217 opts.git_dir = repo_get_git_dir(the_repository);
2218 /*
2219 * When setup_git_directory() was not yet asked to discover the
environment.c
-7
@@ -228,13 +228,6 @@ int have_git_dir(void)
228 || the_repository->gitdir;
229 }
230
231 -const char *get_git_common_dir(void)
232 -{
233 - if (!the_repository->commondir)
234 - BUG("git environment hasn't been setup");
235 - return the_repository->commondir;
236 -}
237 -
231 const char *get_git_namespace(void)
232 {
233 if (!git_namespace)
environment.h
-1
@@ -106,7 +106,6 @@ int have_git_dir(void);
106 extern int is_bare_repository_cfg;
107 int is_bare_repository(void);
108 extern char *git_work_tree_cfg;
109 -const char *get_git_common_dir(void);
109 const char *get_object_directory(void);
110 char *get_index_file(void);
111 char *get_graft_file(struct repository *r);
repository.c
+7
@@ -98,6 +98,13 @@ const char *repo_get_git_dir(struct repository *repo)
98 return repo->gitdir;
99 }
100
101 +const char *repo_get_common_dir(struct repository *repo)
102 +{
103 + if (!repo->commondir)
104 + BUG("repository hasn't been set up");
105 + return repo->commondir;
106 +}
107 +
108 static void repo_set_commondir(struct repository *repo,
109 const char *commondir)
110 {
repository.h
+1
@@ -207,6 +207,7 @@ extern struct repository *the_repository;
207 #endif
208
209 const char *repo_get_git_dir(struct repository *repo);
210 +const char *repo_get_common_dir(struct repository *repo);
211
212 /*
213 * Define a custom repository layout. Any field can be NULL, which
setup.c
+1 -1
@@ -2068,7 +2068,7 @@ static void copy_templates(const char *option_template)
2068 goto close_free_return;
2069 }
2070
2071 - strbuf_addstr(&path, get_git_common_dir());
2071 + strbuf_addstr(&path, repo_get_common_dir(the_repository));
2072 strbuf_complete(&path, '/');
2073 copy_templates_1(&path, &template_path, dir);
2074 close_free_return:
submodule.c
+1 -1
@@ -2462,7 +2462,7 @@ void absorb_git_dir_into_superproject(const char *path,
2462 } else {
2463 /* Is it already absorbed into the superprojects git dir? */
2464 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
2465 - char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
2465 + char *real_common_git_dir = real_pathdup(repo_get_common_dir(the_repository), 1);
2466
2467 if (!starts_with(real_sub_git_dir, real_common_git_dir))
2468 relocate_single_git_dir_into_superproject(path, super_prefix);
trace.c
+1 -1
@@ -315,7 +315,7 @@ void trace_repo_setup(void)
315 prefix = "(null)";
316
317 trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(repo_get_git_dir(the_repository)));
318 - trace_printf_key(&trace_setup_key, "setup: git_common_dir: %s\n", quote_crnl(get_git_common_dir()));
318 + trace_printf_key(&trace_setup_key, "setup: git_common_dir: %s\n", quote_crnl(repo_get_common_dir(the_repository)));
319 trace_printf_key(&trace_setup_key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
320 trace_printf_key(&trace_setup_key, "setup: cwd: %s\n", quote_crnl(cwd));
321 trace_printf_key(&trace_setup_key, "setup: prefix: %s\n", quote_crnl(prefix));
worktree.c
+4 -4
@@ -72,7 +72,7 @@ static struct worktree *get_main_worktree(int skip_reading_head)
72 struct worktree *worktree = NULL;
73 struct strbuf worktree_path = STRBUF_INIT;
74
75 - strbuf_add_real_path(&worktree_path, get_git_common_dir());
75 + strbuf_add_real_path(&worktree_path, repo_get_common_dir(the_repository));
76 strbuf_strip_suffix(&worktree_path, "/.git");
77
78 CALLOC_ARRAY(worktree, 1);
@@ -143,7 +143,7 @@ static struct worktree **get_worktrees_internal(int skip_reading_head)
143
144 list[counter++] = get_main_worktree(skip_reading_head);
145
146 - strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
146 + strbuf_addf(&path, "%s/worktrees", repo_get_common_dir(the_repository));
147 dir = opendir(path.buf);
148 strbuf_release(&path);
149 if (dir) {
@@ -173,7 +173,7 @@ const char *get_worktree_git_dir(const struct worktree *wt)
173 if (!wt)
174 return repo_get_git_dir(the_repository);
175 else if (!wt->id)
176 - return get_git_common_dir();
176 + return repo_get_common_dir(the_repository);
177 else
178 return git_common_path("worktrees/%s", wt->id);
179 }
@@ -626,7 +626,7 @@ static int is_main_worktree_path(const char *path)
626
627 strbuf_add_real_path(&target, path);
628 strbuf_strip_suffix(&target, "/.git");
629 - strbuf_add_real_path(&maindir, get_git_common_dir());
629 + strbuf_add_real_path(&maindir, repo_get_common_dir(the_repository));
630 strbuf_strip_suffix(&maindir, "/.git");
631 cmp = fspathcmp(maindir.buf, target.buf);
632