environment: make `get_git_work_tree()` accept a repository

The `get_git_work_tree()` function retrieves the path of the work tree of `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 edc2c92624a5389836789579ff11417ca1c61ea0
18 files changed +36 -34
builtin/blame.c
+1 -1
@@ -1081,7 +1081,7 @@ parse_done:
1081 path = add_prefix(prefix, argv[1]);
1082 argv[1] = argv[2];
1083 } else { /* (2a) */
1084 - if (argc == 2 && is_a_rev(argv[1]) && !get_git_work_tree())
1084 + if (argc == 2 && is_a_rev(argv[1]) && !repo_get_work_tree(the_repository))
1085 die("missing <path> to blame");
1086 path = add_prefix(prefix, argv[argc - 1]);
1087 }
builtin/difftool.c
+2 -2
@@ -378,7 +378,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
378 struct hashmap wt_modified, tmp_modified;
379 int indices_loaded = 0;
380
381 - workdir = get_git_work_tree();
381 + workdir = repo_get_work_tree(the_repository);
382
383 /* Setup temp directories */
384 tmp = getenv("TMPDIR");
@@ -739,7 +739,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
739 if (!no_index){
740 setup_work_tree();
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);
742 + setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(repo_get_work_tree(the_repository)), 1);
743 } else if (dir_diff)
744 die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
745
builtin/fsmonitor--daemon.c
+2 -2
@@ -2,7 +2,6 @@
2 #include "abspath.h"
3 #include "config.h"
4 #include "dir.h"
5 -#include "environment.h"
5 #include "gettext.h"
6 #include "parse-options.h"
7 #include "fsmonitor-ll.h"
@@ -1291,7 +1290,8 @@ static int fsmonitor_run_daemon(void)
1290
1291 /* Prepare to (recursively) watch the <worktree-root> directory. */
1292 strbuf_init(&state.path_worktree_watch, 0);
1294 - strbuf_addstr(&state.path_worktree_watch, absolute_path(get_git_work_tree()));
1293 + strbuf_addstr(&state.path_worktree_watch,
1294 + absolute_path(repo_get_work_tree(the_repository)));
1295 state.nr_paths_watching = 1;
1296
1297 strbuf_init(&state.alias.alias, 0);
builtin/init-db.c
+2 -2
@@ -231,9 +231,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
231 set_git_work_tree(work_tree);
232 else
233 set_git_work_tree(git_work_tree_cfg);
234 - if (access(get_git_work_tree(), X_OK))
234 + if (access(repo_get_work_tree(the_repository), X_OK))
235 die_errno (_("Cannot access work tree '%s'"),
236 - get_git_work_tree());
236 + repo_get_work_tree(the_repository));
237 }
238 else {
239 if (real_git_dir)
builtin/reset.c
+3 -2
@@ -26,6 +26,7 @@
26 #include "object-name.h"
27 #include "parse-options.h"
28 #include "path.h"
29 +#include "repository.h"
30 #include "unpack-trees.h"
31 #include "cache-tree.h"
32 #include "setup.h"
@@ -441,7 +442,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
442 else
443 trace2_cmd_mode(reset_type_names[reset_type]);
444
444 - if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree()))
445 + if (reset_type != SOFT && (reset_type != MIXED || repo_get_work_tree(the_repository)))
446 setup_work_tree();
447
448 if (reset_type == MIXED && is_bare_repository())
@@ -474,7 +475,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
475 goto cleanup;
476 }
477 the_repository->index->updated_skipworktree = 1;
477 - if (!no_refresh && get_git_work_tree()) {
478 + if (!no_refresh && repo_get_work_tree(the_repository)) {
479 uint64_t t_begin, t_delta_in_ms;
480
481 t_begin = getnanotime();
builtin/rev-parse.c
+2 -2
@@ -967,7 +967,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
967 continue;
968 }
969 if (!strcmp(arg, "--show-toplevel")) {
970 - const char *work_tree = get_git_work_tree();
970 + const char *work_tree = repo_get_work_tree(the_repository);
971 if (work_tree)
972 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
973 else
@@ -992,7 +992,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
992 const char *pfx = prefix;
993 if (!is_inside_work_tree()) {
994 const char *work_tree =
995 - get_git_work_tree();
995 + repo_get_work_tree(the_repository);
996 if (work_tree)
997 printf("%s\n", work_tree);
998 continue;
builtin/stash.c
+1 -1
@@ -641,7 +641,7 @@ restore_untracked:
641 cp.git_cmd = 1;
642 cp.dir = prefix;
643 strvec_pushf(&cp.env, GIT_WORK_TREE_ENVIRONMENT"=%s",
644 - absolute_path(get_git_work_tree()));
644 + absolute_path(repo_get_work_tree(the_repository)));
645 strvec_pushf(&cp.env, GIT_DIR_ENVIRONMENT"=%s",
646 absolute_path(repo_get_git_dir(the_repository)));
647 strvec_push(&cp.args, "status");
builtin/submodule--helper.c
+1 -1
@@ -1709,7 +1709,7 @@ static int clone_submodule(const struct module_clone_data *clone_data,
1709 exit(128);
1710
1711 if (!is_absolute_path(clone_data->path))
1712 - clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(),
1712 + clone_data_path = to_free = xstrfmt("%s/%s", repo_get_work_tree(the_repository),
1713 clone_data->path);
1714
1715 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
builtin/update-index.c
+1 -1
@@ -1194,7 +1194,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1194 "remove or change it, if you really want to "
1195 "enable the untracked cache"));
1196 add_untracked_cache(the_repository->index);
1197 - report(_("Untracked cache enabled for '%s'"), get_git_work_tree());
1197 + report(_("Untracked cache enabled for '%s'"), repo_get_work_tree(the_repository));
1198 break;
1199 default:
1200 BUG("bad untracked_cache value: %d", untracked_cache);
dir.c
+2 -1
@@ -20,6 +20,7 @@
20 #include "object-store-ll.h"
21 #include "path.h"
22 #include "refs.h"
23 +#include "repository.h"
24 #include "wildmatch.h"
25 #include "pathspec.h"
26 #include "utf8.h"
@@ -2838,7 +2839,7 @@ static const char *get_ident_string(void)
2839 return sb.buf;
2840 if (uname(&uts) < 0)
2841 die_errno(_("failed to get kernel name and information"));
2841 - strbuf_addf(&sb, "Location %s, system %s", get_git_work_tree(),
2842 + strbuf_addf(&sb, "Location %s, system %s", repo_get_work_tree(the_repository),
2843 uts.sysname);
2844 return sb.buf;
2845 }
environment.c
+1 -6
@@ -219,7 +219,7 @@ void setup_git_env(const char *git_dir)
219 int is_bare_repository(void)
220 {
221 /* if core.bare is not 'false', let's see if there is a work tree */
222 - return is_bare_repository_cfg && !get_git_work_tree();
222 + return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
223 }
224
225 int have_git_dir(void)
@@ -268,11 +268,6 @@ void set_git_work_tree(const char *new_work_tree)
268 repo_set_worktree(the_repository, new_work_tree);
269 }
270
271 -const char *get_git_work_tree(void)
272 -{
273 - return the_repository->worktree;
274 -}
275 -
271 int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
272 {
273 int fd;
environment.h
-1
@@ -108,7 +108,6 @@ extern char *git_work_tree_cfg;
108 void set_git_dir(const char *path, int make_realpath);
109 const char *get_git_namespace(void);
110 const char *strip_namespace(const char *namespaced_ref);
111 -const char *get_git_work_tree(void);
111 void set_git_work_tree(const char *tree);
112
113 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
fsmonitor.c
+2 -1
@@ -8,6 +8,7 @@
8 #include "fsmonitor.h"
9 #include "fsmonitor-ipc.h"
10 #include "name-hash.h"
11 +#include "repository.h"
12 #include "run-command.h"
13 #include "strbuf.h"
14 #include "trace2.h"
@@ -169,7 +170,7 @@ static int query_fsmonitor_hook(struct repository *r,
170 strvec_pushf(&cp.args, "%d", version);
171 strvec_pushf(&cp.args, "%s", last_update);
172 cp.use_shell = 1;
172 - cp.dir = get_git_work_tree();
173 + cp.dir = repo_get_work_tree(the_repository);
174
175 trace2_region_enter("fsm_hook", "query", NULL);
176
pathspec.c
+1 -1
@@ -495,7 +495,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
495 if (!have_git_dir())
496 die(_("'%s' is outside the directory tree"),
497 copyfrom);
498 - hint_path = get_git_work_tree();
498 + hint_path = repo_get_work_tree(the_repository);
499 if (!hint_path)
500 hint_path = repo_get_git_dir(the_repository);
501 die(_("%s: '%s' is outside repository at '%s'"), elt,
repository.c
+5
@@ -126,6 +126,11 @@ const char *repo_get_graft_file(struct repository *repo)
126 return repo->graft_file;
127 }
128
129 +const char *repo_get_work_tree(struct repository *repo)
130 +{
131 + return repo->worktree;
132 +}
133 +
134 static void repo_set_commondir(struct repository *repo,
135 const char *commondir)
136 {
repository.h
+1
@@ -211,6 +211,7 @@ const char *repo_get_common_dir(struct repository *repo);
211 const char *repo_get_object_directory(struct repository *repo);
212 const char *repo_get_index_file(struct repository *repo);
213 const char *repo_get_graft_file(struct repository *repo);
214 +const char *repo_get_work_tree(struct repository *repo);
215
216 /*
217 * Define a custom repository layout. Any field can be NULL, which
setup.c
+8 -8
@@ -51,7 +51,7 @@ static int abspath_part_inside_repo(char *path)
51 size_t wtlen;
52 char *path0;
53 int off;
54 - const char *work_tree = precompose_string_if_needed(get_git_work_tree());
54 + const char *work_tree = precompose_string_if_needed(repo_get_work_tree(the_repository));
55 struct strbuf realpath = STRBUF_INIT;
56
57 if (!work_tree)
@@ -147,7 +147,7 @@ char *prefix_path(const char *prefix, int len, const char *path)
147 {
148 char *r = prefix_path_gently(prefix, len, NULL, path);
149 if (!r) {
150 - const char *hint_path = get_git_work_tree();
150 + const char *hint_path = repo_get_work_tree(the_repository);
151 if (!hint_path)
152 hint_path = repo_get_git_dir(the_repository);
153 die(_("'%s' is outside repository at '%s'"), path,
@@ -475,7 +475,7 @@ int is_inside_git_dir(void)
475 int is_inside_work_tree(void)
476 {
477 if (inside_work_tree < 0)
478 - inside_work_tree = is_inside_dir(get_git_work_tree());
478 + inside_work_tree = is_inside_dir(repo_get_work_tree(the_repository));
479 return inside_work_tree;
480 }
481
@@ -490,7 +490,7 @@ void setup_work_tree(void)
490 if (work_tree_config_is_bogus)
491 die(_("unable to set up work tree using invalid config"));
492
493 - work_tree = get_git_work_tree();
493 + work_tree = repo_get_work_tree(the_repository);
494 if (!work_tree || chdir_notify(work_tree))
495 die(_("this operation must be run in a work tree"));
496
@@ -547,7 +547,7 @@ static void setup_original_cwd(void)
547 * Get our worktree; we only protect the current working directory
548 * if it's in the worktree.
549 */
550 - worktree = get_git_work_tree();
550 + worktree = repo_get_work_tree(the_repository);
551 if (!worktree)
552 goto no_prevention_needed;
553
@@ -1062,9 +1062,9 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
1062 set_git_work_tree(".");
1063
1064 /* set_git_work_tree() must have been called by now */
1065 - worktree = get_git_work_tree();
1065 + worktree = repo_get_work_tree(the_repository);
1066
1067 - /* both get_git_work_tree() and cwd are already normalized */
1067 + /* both repo_get_work_tree() and cwd are already normalized */
1068 if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */
1069 set_git_dir(gitdirenv, 0);
1070 free(gitfile);
@@ -2192,7 +2192,7 @@ static int create_default_files(const char *template_path,
2192 char *path;
2193 int reinit;
2194 int filemode;
2195 - const char *work_tree = get_git_work_tree();
2195 + const char *work_tree = repo_get_work_tree(the_repository);
2196
2197 /*
2198 * First copy the templates -- we might have the default
trace.c
+1 -2
@@ -25,7 +25,6 @@
25
26 #include "git-compat-util.h"
27 #include "abspath.h"
28 -#include "environment.h"
28 #include "repository.h"
29 #include "quote.h"
30 #include "setup.h"
@@ -308,7 +307,7 @@ void trace_repo_setup(void)
307
308 cwd = xgetcwd();
309
311 - if (!(git_work_tree = get_git_work_tree()))
310 + if (!(git_work_tree = repo_get_work_tree(the_repository)))
311 git_work_tree = "(null)";
312
313 if (!startup_info->prefix)