environment: make `get_git_dir()` accept a repository

The `get_git_dir()` function retrieves the path to the Git 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 246deeac9517d6daba89bfcf6de6d290e39af585
21 files changed +56 -38
apply.c
+2 -1
@@ -30,6 +30,7 @@
30 #include "path.h"
31 #include "quote.h"
32 #include "read-cache.h"
33 +#include "repository.h"
34 #include "rerere.h"
35 #include "apply.h"
36 #include "entry.h"
@@ -4111,7 +4112,7 @@ static int read_apply_cache(struct apply_state *state)
4112 {
4113 if (state->index_file)
4114 return read_index_from(state->repo->index, state->index_file,
4114 - get_git_dir());
4115 + repo_get_git_dir(the_repository));
4116 else
4117 return repo_read_index(state->repo);
4118 }
builtin/am.c
+3 -2
@@ -1544,7 +1544,8 @@ static int run_apply(const struct am_state *state, const char *index_file)
1544 if (index_file) {
1545 /* Reload index as apply_all_patches() will have modified it. */
1546 discard_index(the_repository->index);
1547 - read_index_from(the_repository->index, index_file, get_git_dir());
1547 + read_index_from(the_repository->index, index_file,
1548 + repo_get_git_dir(the_repository));
1549 }
1550
1551 return 0;
@@ -1587,7 +1588,7 @@ static int fall_back_threeway(const struct am_state *state, const char *index_pa
1588 return error("could not build fake ancestor");
1589
1590 discard_index(the_repository->index);
1590 - read_index_from(the_repository->index, index_path, get_git_dir());
1591 + read_index_from(the_repository->index, index_path, repo_get_git_dir(the_repository));
1592
1593 if (write_index_as_tree(&bases[0], the_repository->index, index_path, 0, NULL))
1594 return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
builtin/commit.c
+4 -3
@@ -26,6 +26,7 @@
26 #include "path.h"
27 #include "preload-index.h"
28 #include "read-cache.h"
29 +#include "repository.h"
30 #include "string-list.h"
31 #include "rerere.h"
32 #include "unpack-trees.h"
@@ -407,7 +408,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
408
409 discard_index(the_repository->index);
410 read_index_from(the_repository->index, get_lock_file_path(&index_lock),
410 - get_git_dir());
411 + repo_get_git_dir(the_repository));
412 if (cache_tree_update(the_repository->index, WRITE_TREE_SILENT) == 0) {
413 if (reopen_lock_file(&index_lock) < 0)
414 die(_("unable to write index file"));
@@ -534,7 +535,7 @@ static const char *prepare_index(const char **argv, const char *prefix,
535
536 discard_index(the_repository->index);
537 ret = get_lock_file_path(&false_lock);
537 - read_index_from(the_repository->index, ret, get_git_dir());
538 + read_index_from(the_repository->index, ret, repo_get_git_dir(the_repository));
539 out:
540 string_list_clear(&partial, 0);
541 clear_pathspec(&pathspec);
@@ -1072,7 +1073,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
1073 */
1074 discard_index(the_repository->index);
1075 }
1075 - read_index_from(the_repository->index, index_file, get_git_dir());
1076 + read_index_from(the_repository->index, index_file, repo_get_git_dir(the_repository));
1077
1078 if (cache_tree_update(the_repository->index, 0)) {
1079 error(_("Error building trees"));
builtin/config.c
+1 -1
@@ -808,7 +808,7 @@ static void location_options_init(struct config_location_options *opts,
808 opts->options.respect_includes = opts->respect_includes_opt;
809 if (startup_info->have_repository) {
810 opts->options.commondir = get_git_common_dir();
811 - opts->options.git_dir = get_git_dir();
811 + opts->options.git_dir = repo_get_git_dir(the_repository);
812 }
813 }
814
builtin/difftool.c
+3 -2
@@ -22,6 +22,7 @@
22 #include "hex.h"
23 #include "parse-options.h"
24 #include "read-cache-ll.h"
25 +#include "repository.h"
26 #include "sparse-index.h"
27 #include "strvec.h"
28 #include "strbuf.h"
@@ -214,7 +215,7 @@ static void changed_files(struct hashmap *result, const char *index_path,
215 struct child_process update_index = CHILD_PROCESS_INIT;
216 struct child_process diff_files = CHILD_PROCESS_INIT;
217 struct strbuf buf = STRBUF_INIT;
217 - const char *git_dir = absolute_path(get_git_dir());
218 + const char *git_dir = absolute_path(repo_get_git_dir(the_repository));
219 FILE *fp;
220
221 strvec_pushl(&update_index.args,
@@ -737,7 +738,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
738
739 if (!no_index){
740 setup_work_tree();
740 - setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
741 + setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(the_repository)), 1);
742 setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
743 } else if (dir_diff)
744 die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
builtin/fsmonitor--daemon.c
+2 -1
@@ -1311,7 +1311,8 @@ static int fsmonitor_run_daemon(void)
1311 strbuf_addstr(&state.path_gitdir_watch, "/.git");
1312 if (!is_directory(state.path_gitdir_watch.buf)) {
1313 strbuf_reset(&state.path_gitdir_watch);
1314 - strbuf_addstr(&state.path_gitdir_watch, absolute_path(get_git_dir()));
1314 + strbuf_addstr(&state.path_gitdir_watch,
1315 + absolute_path(repo_get_git_dir(the_repository)));
1316 state.nr_paths_watching = 2;
1317 }
1318
builtin/merge.c
+3 -1
@@ -17,6 +17,7 @@
17 #include "object-name.h"
18 #include "parse-options.h"
19 #include "lockfile.h"
20 +#include "repository.h"
21 #include "run-command.h"
22 #include "hook.h"
23 #include "diff.h"
@@ -855,7 +856,8 @@ static void prepare_to_commit(struct commit_list *remoteheads)
856 if (invoked_hook)
857 discard_index(the_repository->index);
858 }
858 - read_index_from(the_repository->index, index_file, get_git_dir());
859 + read_index_from(the_repository->index, index_file,
860 + repo_get_git_dir(the_repository));
861 strbuf_addbuf(&msg, &merge_msg);
862 if (squash)
863 BUG("the control must not reach here under --squash");
builtin/stash.c
+2 -1
@@ -19,6 +19,7 @@
19 #include "entry.h"
20 #include "preload-index.h"
21 #include "read-cache.h"
22 +#include "repository.h"
23 #include "rerere.h"
24 #include "revision.h"
25 #include "setup.h"
@@ -642,7 +643,7 @@ restore_untracked:
643 strvec_pushf(&cp.env, GIT_WORK_TREE_ENVIRONMENT"=%s",
644 absolute_path(get_git_work_tree()));
645 strvec_pushf(&cp.env, GIT_DIR_ENVIRONMENT"=%s",
645 - absolute_path(get_git_dir()));
646 + absolute_path(repo_get_git_dir(the_repository)));
647 strvec_push(&cp.args, "status");
648 run_command(&cp);
649 }
cache-tree.c
+3 -2
@@ -1,7 +1,6 @@
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "git-compat-util.h"
4 -#include "environment.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "tree.h"
@@ -12,6 +11,7 @@
11 #include "object-store-ll.h"
12 #include "read-cache-ll.h"
13 #include "replace-object.h"
14 +#include "repository.h"
15 #include "promisor-remote.h"
16 #include "trace.h"
17 #include "trace2.h"
@@ -725,7 +725,8 @@ int write_index_as_tree(struct object_id *oid, struct index_state *index_state,
725
726 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
727
728 - entries = read_index_from(index_state, index_path, get_git_dir());
728 + entries = read_index_from(index_state, index_path,
729 + repo_get_git_dir(the_repository));
730 if (entries < 0) {
731 ret = WRITE_TREE_UNREADABLE_INDEX;
732 goto out;
config.c
+3 -1
@@ -6,6 +6,8 @@
6 *
7 */
8
9 +#define USE_THE_REPOSITORY_VARIABLE
10 +
11 #include "git-compat-util.h"
12 #include "abspath.h"
13 #include "advice.h"
@@ -2212,7 +2214,7 @@ void read_early_config(config_fn_t cb, void *data)
2214
2215 if (have_git_dir()) {
2216 opts.commondir = get_git_common_dir();
2215 - opts.git_dir = get_git_dir();
2217 + opts.git_dir = repo_get_git_dir(the_repository);
2218 /*
2219 * When setup_git_directory() was not yet asked to discover the
2220 * GIT_DIR, we ask discover_git_directory() to figure out whether there
environment.c
+2 -8
@@ -228,13 +228,6 @@ int have_git_dir(void)
228 || the_repository->gitdir;
229 }
230
231 -const char *get_git_dir(void)
232 -{
233 - if (!the_repository->gitdir)
234 - BUG("git environment hasn't been setup");
235 - return the_repository->gitdir;
236 -}
237 -
231 const char *get_git_common_dir(void)
232 {
233 if (!the_repository->commondir)
@@ -352,7 +345,8 @@ static void update_relative_gitdir(const char *name UNUSED,
345 const char *new_cwd,
346 void *data UNUSED)
347 {
355 - char *path = reparent_relative_path(old_cwd, new_cwd, get_git_dir());
348 + char *path = reparent_relative_path(old_cwd, new_cwd,
349 + repo_get_git_dir(the_repository));
350 struct tmp_objdir *tmp_objdir = tmp_objdir_unapply_primary_odb();
351
352 trace_printf_key(&trace_setup_key,
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_dir(void);
109 const char *get_git_common_dir(void);
110 const char *get_object_directory(void);
111 char *get_index_file(void);
pathspec.c
+1 -1
@@ -497,7 +497,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
497 copyfrom);
498 hint_path = get_git_work_tree();
499 if (!hint_path)
500 - hint_path = get_git_dir();
500 + hint_path = repo_get_git_dir(the_repository);
501 die(_("%s: '%s' is outside repository at '%s'"), elt,
502 copyfrom, absolute_path(hint_path));
503 }
read-cache.c
+4 -2
@@ -31,6 +31,7 @@
31 #include "path.h"
32 #include "preload-index.h"
33 #include "read-cache.h"
34 +#include "repository.h"
35 #include "resolve-undo.h"
36 #include "revision.h"
37 #include "strbuf.h"
@@ -3238,10 +3239,11 @@ static int should_delete_shared_index(const char *shared_index_path)
3239 static int clean_shared_index_files(const char *current_hex)
3240 {
3241 struct dirent *de;
3241 - DIR *dir = opendir(get_git_dir());
3242 + DIR *dir = opendir(repo_get_git_dir(the_repository));
3243
3244 if (!dir)
3244 - return error_errno(_("unable to open git dir: %s"), get_git_dir());
3245 + return error_errno(_("unable to open git dir: %s"),
3246 + repo_get_git_dir(the_repository));
3247
3248 while ((de = readdir(dir)) != NULL) {
3249 const char *sha1_hex;
repository.c
+7
@@ -91,6 +91,13 @@ static void expand_base_dir(char **out, const char *in,
91 *out = xstrfmt("%s/%s", base_dir, def_in);
92 }
93
94 +const char *repo_get_git_dir(struct repository *repo)
95 +{
96 + if (!repo->gitdir)
97 + BUG("repository hasn't been set up");
98 + return repo->gitdir;
99 +}
100 +
101 static void repo_set_commondir(struct repository *repo,
102 const char *commondir)
103 {
repository.h
+2
@@ -206,6 +206,8 @@ struct repository {
206 extern struct repository *the_repository;
207 #endif
208
209 +const char *repo_get_git_dir(struct repository *repo);
210 +
211 /*
212 * Define a custom repository layout. Any field can be NULL, which
213 * will default back to the path according to the default layout.
setup.c
+6 -6
@@ -149,7 +149,7 @@ char *prefix_path(const char *prefix, int len, const char *path)
149 if (!r) {
150 const char *hint_path = get_git_work_tree();
151 if (!hint_path)
152 - hint_path = get_git_dir();
152 + hint_path = repo_get_git_dir(the_repository);
153 die(_("'%s' is outside repository at '%s'"), path,
154 absolute_path(hint_path));
155 }
@@ -468,7 +468,7 @@ int is_nonbare_repository_dir(struct strbuf *path)
468 int is_inside_git_dir(void)
469 {
470 if (inside_git_dir < 0)
471 - inside_git_dir = is_inside_dir(get_git_dir());
471 + inside_git_dir = is_inside_dir(repo_get_git_dir(the_repository));
472 return inside_git_dir;
473 }
474
@@ -1836,7 +1836,7 @@ void check_repository_format(struct repository_format *fmt)
1836 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
1837 if (!fmt)
1838 fmt = &repo_fmt;
1839 - check_repository_format_gently(get_git_dir(), fmt, NULL);
1839 + check_repository_format_gently(repo_get_git_dir(the_repository), fmt, NULL);
1840 startup_info->have_repository = 1;
1841 repo_set_hash_algo(the_repository, fmt->hash_algo);
1842 repo_set_compat_hash_algo(the_repository, fmt->compat_hash_algo);
@@ -2224,7 +2224,7 @@ static int create_default_files(const char *template_path,
2224 * shared-repository settings, we would need to fix them up.
2225 */
2226 if (get_shared_repository()) {
2227 - adjust_shared_perm(get_git_dir());
2227 + adjust_shared_perm(repo_get_git_dir(the_repository));
2228 }
2229
2230 initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0);
@@ -2434,12 +2434,12 @@ int init_db(const char *git_dir, const char *real_git_dir,
2434 die(_("%s already exists"), real_git_dir);
2435
2436 set_git_dir(real_git_dir, 1);
2437 - git_dir = get_git_dir();
2437 + git_dir = repo_get_git_dir(the_repository);
2438 separate_git_dir(git_dir, original_git_dir);
2439 }
2440 else {
2441 set_git_dir(git_dir, 1);
2442 - git_dir = get_git_dir();
2442 + git_dir = repo_get_git_dir(the_repository);
2443 }
2444 startup_info->have_repository = 1;
2445
setup.h
+1 -1
@@ -176,7 +176,7 @@ int verify_repository_format(const struct repository_format *format,
176 struct strbuf *err);
177
178 /*
179 - * Check the repository format version in the path found in get_git_dir(),
179 + * Check the repository format version in the path found in repo_get_git_dir(the_repository),
180 * and die if it is a version we don't understand. Generally one would
181 * set_git_dir() before calling this, and use it only for "are we in a valid
182 * repo?".
trace.c
+4 -1
@@ -21,9 +21,12 @@
21 * along with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
24 +#define USE_THE_REPOSITORY_VARIABLE
25 +
26 #include "git-compat-util.h"
27 #include "abspath.h"
28 #include "environment.h"
29 +#include "repository.h"
30 #include "quote.h"
31 #include "setup.h"
32 #include "trace.h"
@@ -311,7 +314,7 @@ void trace_repo_setup(void)
314 if (!startup_info->prefix)
315 prefix = "(null)";
316
314 - trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(get_git_dir()));
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()));
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));
transport-helper.c
+1 -1
@@ -143,7 +143,7 @@ static struct child_process *get_helper(struct transport *transport)
143
144 if (have_git_dir())
145 strvec_pushf(&helper->env, "%s=%s",
146 - GIT_DIR_ENVIRONMENT, get_git_dir());
146 + GIT_DIR_ENVIRONMENT, repo_get_git_dir(the_repository));
147
148 helper->trace2_child_class = helper->args.v[0]; /* "remote-<name>" */
149
worktree.c
+2 -2
@@ -57,7 +57,7 @@ static void add_head_info(struct worktree *wt)
57
58 static int is_current_worktree(struct worktree *wt)
59 {
60 - char *git_dir = absolute_pathdup(get_git_dir());
60 + char *git_dir = absolute_pathdup(repo_get_git_dir(the_repository));
61 const char *wt_git_dir = get_worktree_git_dir(wt);
62 int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir));
63 free(git_dir);
@@ -171,7 +171,7 @@ struct worktree **get_worktrees(void)
171 const char *get_worktree_git_dir(const struct worktree *wt)
172 {
173 if (!wt)
174 - return get_git_dir();
174 + return repo_get_git_dir(the_repository);
175 else if (!wt->id)
176 return get_git_common_dir();
177 else