Skip to content

Commit edc2c92

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 14c90ac commit edc2c92

18 files changed

+36
-34
lines changed

builtin/blame.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
10811081
path = add_prefix(prefix, argv[1]);
10821082
argv[1] = argv[2];
10831083
} 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))
10851085
die("missing <path> to blame");
10861086
path = add_prefix(prefix, argv[argc - 1]);
10871087
}

builtin/difftool.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
378378
struct hashmap wt_modified, tmp_modified;
379379
int indices_loaded = 0;
380380

381-
workdir = get_git_work_tree();
381+
workdir = repo_get_work_tree(the_repository);
382382

383383
/* Setup temp directories */
384384
tmp = getenv("TMPDIR");
@@ -739,7 +739,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
739739
if (!no_index){
740740
setup_work_tree();
741741
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);
743743
} else if (dir_diff)
744744
die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
745745

builtin/fsmonitor--daemon.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "abspath.h"
33
#include "config.h"
44
#include "dir.h"
5-
#include "environment.h"
65
#include "gettext.h"
76
#include "parse-options.h"
87
#include "fsmonitor-ll.h"
@@ -1291,7 +1290,8 @@ static int fsmonitor_run_daemon(void)
12911290

12921291
/* Prepare to (recursively) watch the <worktree-root> directory. */
12931292
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)));
12951295
state.nr_paths_watching = 1;
12961296

12971297
strbuf_init(&state.alias.alias, 0);

builtin/init-db.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
231231
set_git_work_tree(work_tree);
232232
else
233233
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))
235235
die_errno (_("Cannot access work tree '%s'"),
236-
get_git_work_tree());
236+
repo_get_work_tree(the_repository));
237237
}
238238
else {
239239
if (real_git_dir)

builtin/reset.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "object-name.h"
2727
#include "parse-options.h"
2828
#include "path.h"
29+
#include "repository.h"
2930
#include "unpack-trees.h"
3031
#include "cache-tree.h"
3132
#include "setup.h"
@@ -441,7 +442,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
441442
else
442443
trace2_cmd_mode(reset_type_names[reset_type]);
443444

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)))
445446
setup_work_tree();
446447

447448
if (reset_type == MIXED && is_bare_repository())
@@ -474,7 +475,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
474475
goto cleanup;
475476
}
476477
the_repository->index->updated_skipworktree = 1;
477-
if (!no_refresh && get_git_work_tree()) {
478+
if (!no_refresh && repo_get_work_tree(the_repository)) {
478479
uint64_t t_begin, t_delta_in_ms;
479480

480481
t_begin = getnanotime();

builtin/rev-parse.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
967967
continue;
968968
}
969969
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);
971971
if (work_tree)
972972
print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
973973
else
@@ -992,7 +992,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
992992
const char *pfx = prefix;
993993
if (!is_inside_work_tree()) {
994994
const char *work_tree =
995-
get_git_work_tree();
995+
repo_get_work_tree(the_repository);
996996
if (work_tree)
997997
printf("%s\n", work_tree);
998998
continue;

builtin/stash.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
641641
cp.git_cmd = 1;
642642
cp.dir = prefix;
643643
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)));
645645
strvec_pushf(&cp.env, GIT_DIR_ENVIRONMENT"=%s",
646646
absolute_path(repo_get_git_dir(the_repository)));
647647
strvec_push(&cp.args, "status");

builtin/submodule--helper.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ static int clone_submodule(const struct module_clone_data *clone_data,
17091709
exit(128);
17101710

17111711
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),
17131713
clone_data->path);
17141714

17151715
if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)

builtin/update-index.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
11941194
"remove or change it, if you really want to "
11951195
"enable the untracked cache"));
11961196
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));
11981198
break;
11991199
default:
12001200
BUG("bad untracked_cache value: %d", untracked_cache);

dir.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "object-store-ll.h"
2121
#include "path.h"
2222
#include "refs.h"
23+
#include "repository.h"
2324
#include "wildmatch.h"
2425
#include "pathspec.h"
2526
#include "utf8.h"
@@ -2838,7 +2839,7 @@ static const char *get_ident_string(void)
28382839
return sb.buf;
28392840
if (uname(&uts) < 0)
28402841
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),
28422843
uts.sysname);
28432844
return sb.buf;
28442845
}

environment.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void setup_git_env(const char *git_dir)
219219
int is_bare_repository(void)
220220
{
221221
/* 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);
223223
}
224224

225225
int have_git_dir(void)
@@ -268,11 +268,6 @@ void set_git_work_tree(const char *new_work_tree)
268268
repo_set_worktree(the_repository, new_work_tree);
269269
}
270270

271-
const char *get_git_work_tree(void)
272-
{
273-
return the_repository->worktree;
274-
}
275-
276271
int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
277272
{
278273
int fd;

environment.h

-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ extern char *git_work_tree_cfg;
108108
void set_git_dir(const char *path, int make_realpath);
109109
const char *get_git_namespace(void);
110110
const char *strip_namespace(const char *namespaced_ref);
111-
const char *get_git_work_tree(void);
112111
void set_git_work_tree(const char *tree);
113112

114113
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"

fsmonitor.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "fsmonitor.h"
99
#include "fsmonitor-ipc.h"
1010
#include "name-hash.h"
11+
#include "repository.h"
1112
#include "run-command.h"
1213
#include "strbuf.h"
1314
#include "trace2.h"
@@ -169,7 +170,7 @@ static int query_fsmonitor_hook(struct repository *r,
169170
strvec_pushf(&cp.args, "%d", version);
170171
strvec_pushf(&cp.args, "%s", last_update);
171172
cp.use_shell = 1;
172-
cp.dir = get_git_work_tree();
173+
cp.dir = repo_get_work_tree(the_repository);
173174

174175
trace2_region_enter("fsm_hook", "query", NULL);
175176

pathspec.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
495495
if (!have_git_dir())
496496
die(_("'%s' is outside the directory tree"),
497497
copyfrom);
498-
hint_path = get_git_work_tree();
498+
hint_path = repo_get_work_tree(the_repository);
499499
if (!hint_path)
500500
hint_path = repo_get_git_dir(the_repository);
501501
die(_("%s: '%s' is outside repository at '%s'"), elt,

repository.c

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ const char *repo_get_graft_file(struct repository *repo)
126126
return repo->graft_file;
127127
}
128128

129+
const char *repo_get_work_tree(struct repository *repo)
130+
{
131+
return repo->worktree;
132+
}
133+
129134
static void repo_set_commondir(struct repository *repo,
130135
const char *commondir)
131136
{

repository.h

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ const char *repo_get_common_dir(struct repository *repo);
211211
const char *repo_get_object_directory(struct repository *repo);
212212
const char *repo_get_index_file(struct repository *repo);
213213
const char *repo_get_graft_file(struct repository *repo);
214+
const char *repo_get_work_tree(struct repository *repo);
214215

215216
/*
216217
* Define a custom repository layout. Any field can be NULL, which

setup.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static int abspath_part_inside_repo(char *path)
5151
size_t wtlen;
5252
char *path0;
5353
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));
5555
struct strbuf realpath = STRBUF_INIT;
5656

5757
if (!work_tree)
@@ -147,7 +147,7 @@ char *prefix_path(const char *prefix, int len, const char *path)
147147
{
148148
char *r = prefix_path_gently(prefix, len, NULL, path);
149149
if (!r) {
150-
const char *hint_path = get_git_work_tree();
150+
const char *hint_path = repo_get_work_tree(the_repository);
151151
if (!hint_path)
152152
hint_path = repo_get_git_dir(the_repository);
153153
die(_("'%s' is outside repository at '%s'"), path,
@@ -475,7 +475,7 @@ int is_inside_git_dir(void)
475475
int is_inside_work_tree(void)
476476
{
477477
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));
479479
return inside_work_tree;
480480
}
481481

@@ -490,7 +490,7 @@ void setup_work_tree(void)
490490
if (work_tree_config_is_bogus)
491491
die(_("unable to set up work tree using invalid config"));
492492

493-
work_tree = get_git_work_tree();
493+
work_tree = repo_get_work_tree(the_repository);
494494
if (!work_tree || chdir_notify(work_tree))
495495
die(_("this operation must be run in a work tree"));
496496

@@ -547,7 +547,7 @@ static void setup_original_cwd(void)
547547
* Get our worktree; we only protect the current working directory
548548
* if it's in the worktree.
549549
*/
550-
worktree = get_git_work_tree();
550+
worktree = repo_get_work_tree(the_repository);
551551
if (!worktree)
552552
goto no_prevention_needed;
553553

@@ -1062,9 +1062,9 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
10621062
set_git_work_tree(".");
10631063

10641064
/* set_git_work_tree() must have been called by now */
1065-
worktree = get_git_work_tree();
1065+
worktree = repo_get_work_tree(the_repository);
10661066

1067-
/* both get_git_work_tree() and cwd are already normalized */
1067+
/* both repo_get_work_tree() and cwd are already normalized */
10681068
if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */
10691069
set_git_dir(gitdirenv, 0);
10701070
free(gitfile);
@@ -2192,7 +2192,7 @@ static int create_default_files(const char *template_path,
21922192
char *path;
21932193
int reinit;
21942194
int filemode;
2195-
const char *work_tree = get_git_work_tree();
2195+
const char *work_tree = repo_get_work_tree(the_repository);
21962196

21972197
/*
21982198
* First copy the templates -- we might have the default

trace.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
#include "git-compat-util.h"
2727
#include "abspath.h"
28-
#include "environment.h"
2928
#include "repository.h"
3029
#include "quote.h"
3130
#include "setup.h"
@@ -308,7 +307,7 @@ void trace_repo_setup(void)
308307

309308
cwd = xgetcwd();
310309

311-
if (!(git_work_tree = get_git_work_tree()))
310+
if (!(git_work_tree = repo_get_work_tree(the_repository)))
312311
git_work_tree = "(null)";
313312

314313
if (!startup_info->prefix)

0 commit comments

Comments
 (0)