1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-10-28 04:49:43 +01:00

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>
This commit is contained in:
Patrick Steinhardt 2024-09-12 13:29:40 +02:00 committed by Junio C Hamano
parent 14c90ac088
commit edc2c92624
18 changed files with 36 additions and 34 deletions

View file

@ -1081,7 +1081,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
path = add_prefix(prefix, argv[1]); path = add_prefix(prefix, argv[1]);
argv[1] = argv[2]; argv[1] = argv[2];
} else { /* (2a) */ } else { /* (2a) */
if (argc == 2 && is_a_rev(argv[1]) && !get_git_work_tree()) if (argc == 2 && is_a_rev(argv[1]) && !repo_get_work_tree(the_repository))
die("missing <path> to blame"); die("missing <path> to blame");
path = add_prefix(prefix, argv[argc - 1]); path = add_prefix(prefix, argv[argc - 1]);
} }

View file

@ -378,7 +378,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
struct hashmap wt_modified, tmp_modified; struct hashmap wt_modified, tmp_modified;
int indices_loaded = 0; int indices_loaded = 0;
workdir = get_git_work_tree(); workdir = repo_get_work_tree(the_repository);
/* Setup temp directories */ /* Setup temp directories */
tmp = getenv("TMPDIR"); tmp = getenv("TMPDIR");
@ -739,7 +739,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
if (!no_index){ if (!no_index){
setup_work_tree(); setup_work_tree();
setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(the_repository)), 1); setenv(GIT_DIR_ENVIRONMENT, absolute_path(repo_get_git_dir(the_repository)), 1);
setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1); setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(repo_get_work_tree(the_repository)), 1);
} else if (dir_diff) } else if (dir_diff)
die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index"); die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");

View file

@ -2,7 +2,6 @@
#include "abspath.h" #include "abspath.h"
#include "config.h" #include "config.h"
#include "dir.h" #include "dir.h"
#include "environment.h"
#include "gettext.h" #include "gettext.h"
#include "parse-options.h" #include "parse-options.h"
#include "fsmonitor-ll.h" #include "fsmonitor-ll.h"
@ -1291,7 +1290,8 @@ static int fsmonitor_run_daemon(void)
/* Prepare to (recursively) watch the <worktree-root> directory. */ /* Prepare to (recursively) watch the <worktree-root> directory. */
strbuf_init(&state.path_worktree_watch, 0); strbuf_init(&state.path_worktree_watch, 0);
strbuf_addstr(&state.path_worktree_watch, absolute_path(get_git_work_tree())); strbuf_addstr(&state.path_worktree_watch,
absolute_path(repo_get_work_tree(the_repository)));
state.nr_paths_watching = 1; state.nr_paths_watching = 1;
strbuf_init(&state.alias.alias, 0); strbuf_init(&state.alias.alias, 0);

View file

@ -231,9 +231,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
set_git_work_tree(work_tree); set_git_work_tree(work_tree);
else else
set_git_work_tree(git_work_tree_cfg); set_git_work_tree(git_work_tree_cfg);
if (access(get_git_work_tree(), X_OK)) if (access(repo_get_work_tree(the_repository), X_OK))
die_errno (_("Cannot access work tree '%s'"), die_errno (_("Cannot access work tree '%s'"),
get_git_work_tree()); repo_get_work_tree(the_repository));
} }
else { else {
if (real_git_dir) if (real_git_dir)

View file

@ -26,6 +26,7 @@
#include "object-name.h" #include "object-name.h"
#include "parse-options.h" #include "parse-options.h"
#include "path.h" #include "path.h"
#include "repository.h"
#include "unpack-trees.h" #include "unpack-trees.h"
#include "cache-tree.h" #include "cache-tree.h"
#include "setup.h" #include "setup.h"
@ -441,7 +442,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
else else
trace2_cmd_mode(reset_type_names[reset_type]); trace2_cmd_mode(reset_type_names[reset_type]);
if (reset_type != SOFT && (reset_type != MIXED || get_git_work_tree())) if (reset_type != SOFT && (reset_type != MIXED || repo_get_work_tree(the_repository)))
setup_work_tree(); setup_work_tree();
if (reset_type == MIXED && is_bare_repository()) if (reset_type == MIXED && is_bare_repository())
@ -474,7 +475,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
goto cleanup; goto cleanup;
} }
the_repository->index->updated_skipworktree = 1; the_repository->index->updated_skipworktree = 1;
if (!no_refresh && get_git_work_tree()) { if (!no_refresh && repo_get_work_tree(the_repository)) {
uint64_t t_begin, t_delta_in_ms; uint64_t t_begin, t_delta_in_ms;
t_begin = getnanotime(); t_begin = getnanotime();

View file

@ -967,7 +967,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
continue; continue;
} }
if (!strcmp(arg, "--show-toplevel")) { if (!strcmp(arg, "--show-toplevel")) {
const char *work_tree = get_git_work_tree(); const char *work_tree = repo_get_work_tree(the_repository);
if (work_tree) if (work_tree)
print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED); print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
else else
@ -992,7 +992,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
const char *pfx = prefix; const char *pfx = prefix;
if (!is_inside_work_tree()) { if (!is_inside_work_tree()) {
const char *work_tree = const char *work_tree =
get_git_work_tree(); repo_get_work_tree(the_repository);
if (work_tree) if (work_tree)
printf("%s\n", work_tree); printf("%s\n", work_tree);
continue; continue;

View file

@ -641,7 +641,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
cp.git_cmd = 1; cp.git_cmd = 1;
cp.dir = prefix; cp.dir = prefix;
strvec_pushf(&cp.env, GIT_WORK_TREE_ENVIRONMENT"=%s", strvec_pushf(&cp.env, GIT_WORK_TREE_ENVIRONMENT"=%s",
absolute_path(get_git_work_tree())); absolute_path(repo_get_work_tree(the_repository)));
strvec_pushf(&cp.env, GIT_DIR_ENVIRONMENT"=%s", strvec_pushf(&cp.env, GIT_DIR_ENVIRONMENT"=%s",
absolute_path(repo_get_git_dir(the_repository))); absolute_path(repo_get_git_dir(the_repository)));
strvec_push(&cp.args, "status"); strvec_push(&cp.args, "status");

View file

@ -1709,7 +1709,7 @@ static int clone_submodule(const struct module_clone_data *clone_data,
exit(128); exit(128);
if (!is_absolute_path(clone_data->path)) if (!is_absolute_path(clone_data->path))
clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(), clone_data_path = to_free = xstrfmt("%s/%s", repo_get_work_tree(the_repository),
clone_data->path); clone_data->path);
if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)

View file

@ -1194,7 +1194,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
"remove or change it, if you really want to " "remove or change it, if you really want to "
"enable the untracked cache")); "enable the untracked cache"));
add_untracked_cache(the_repository->index); add_untracked_cache(the_repository->index);
report(_("Untracked cache enabled for '%s'"), get_git_work_tree()); report(_("Untracked cache enabled for '%s'"), repo_get_work_tree(the_repository));
break; break;
default: default:
BUG("bad untracked_cache value: %d", untracked_cache); BUG("bad untracked_cache value: %d", untracked_cache);

3
dir.c
View file

@ -20,6 +20,7 @@
#include "object-store-ll.h" #include "object-store-ll.h"
#include "path.h" #include "path.h"
#include "refs.h" #include "refs.h"
#include "repository.h"
#include "wildmatch.h" #include "wildmatch.h"
#include "pathspec.h" #include "pathspec.h"
#include "utf8.h" #include "utf8.h"
@ -2838,7 +2839,7 @@ static const char *get_ident_string(void)
return sb.buf; return sb.buf;
if (uname(&uts) < 0) if (uname(&uts) < 0)
die_errno(_("failed to get kernel name and information")); die_errno(_("failed to get kernel name and information"));
strbuf_addf(&sb, "Location %s, system %s", get_git_work_tree(), strbuf_addf(&sb, "Location %s, system %s", repo_get_work_tree(the_repository),
uts.sysname); uts.sysname);
return sb.buf; return sb.buf;
} }

View file

@ -219,7 +219,7 @@ void setup_git_env(const char *git_dir)
int is_bare_repository(void) int is_bare_repository(void)
{ {
/* if core.bare is not 'false', let's see if there is a work tree */ /* if core.bare is not 'false', let's see if there is a work tree */
return is_bare_repository_cfg && !get_git_work_tree(); return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
} }
int have_git_dir(void) int have_git_dir(void)
@ -268,11 +268,6 @@ void set_git_work_tree(const char *new_work_tree)
repo_set_worktree(the_repository, new_work_tree); repo_set_worktree(the_repository, new_work_tree);
} }
const char *get_git_work_tree(void)
{
return the_repository->worktree;
}
int odb_mkstemp(struct strbuf *temp_filename, const char *pattern) int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
{ {
int fd; int fd;

View file

@ -108,7 +108,6 @@ extern char *git_work_tree_cfg;
void set_git_dir(const char *path, int make_realpath); void set_git_dir(const char *path, int make_realpath);
const char *get_git_namespace(void); const char *get_git_namespace(void);
const char *strip_namespace(const char *namespaced_ref); const char *strip_namespace(const char *namespaced_ref);
const char *get_git_work_tree(void);
void set_git_work_tree(const char *tree); void set_git_work_tree(const char *tree);
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"

View file

@ -8,6 +8,7 @@
#include "fsmonitor.h" #include "fsmonitor.h"
#include "fsmonitor-ipc.h" #include "fsmonitor-ipc.h"
#include "name-hash.h" #include "name-hash.h"
#include "repository.h"
#include "run-command.h" #include "run-command.h"
#include "strbuf.h" #include "strbuf.h"
#include "trace2.h" #include "trace2.h"
@ -169,7 +170,7 @@ static int query_fsmonitor_hook(struct repository *r,
strvec_pushf(&cp.args, "%d", version); strvec_pushf(&cp.args, "%d", version);
strvec_pushf(&cp.args, "%s", last_update); strvec_pushf(&cp.args, "%s", last_update);
cp.use_shell = 1; cp.use_shell = 1;
cp.dir = get_git_work_tree(); cp.dir = repo_get_work_tree(the_repository);
trace2_region_enter("fsm_hook", "query", NULL); trace2_region_enter("fsm_hook", "query", NULL);

View file

@ -495,7 +495,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
if (!have_git_dir()) if (!have_git_dir())
die(_("'%s' is outside the directory tree"), die(_("'%s' is outside the directory tree"),
copyfrom); copyfrom);
hint_path = get_git_work_tree(); hint_path = repo_get_work_tree(the_repository);
if (!hint_path) if (!hint_path)
hint_path = repo_get_git_dir(the_repository); hint_path = repo_get_git_dir(the_repository);
die(_("%s: '%s' is outside repository at '%s'"), elt, die(_("%s: '%s' is outside repository at '%s'"), elt,

View file

@ -126,6 +126,11 @@ const char *repo_get_graft_file(struct repository *repo)
return repo->graft_file; return repo->graft_file;
} }
const char *repo_get_work_tree(struct repository *repo)
{
return repo->worktree;
}
static void repo_set_commondir(struct repository *repo, static void repo_set_commondir(struct repository *repo,
const char *commondir) const char *commondir)
{ {

View file

@ -211,6 +211,7 @@ const char *repo_get_common_dir(struct repository *repo);
const char *repo_get_object_directory(struct repository *repo); const char *repo_get_object_directory(struct repository *repo);
const char *repo_get_index_file(struct repository *repo); const char *repo_get_index_file(struct repository *repo);
const char *repo_get_graft_file(struct repository *repo); const char *repo_get_graft_file(struct repository *repo);
const char *repo_get_work_tree(struct repository *repo);
/* /*
* Define a custom repository layout. Any field can be NULL, which * Define a custom repository layout. Any field can be NULL, which

16
setup.c
View file

@ -51,7 +51,7 @@ static int abspath_part_inside_repo(char *path)
size_t wtlen; size_t wtlen;
char *path0; char *path0;
int off; int off;
const char *work_tree = precompose_string_if_needed(get_git_work_tree()); const char *work_tree = precompose_string_if_needed(repo_get_work_tree(the_repository));
struct strbuf realpath = STRBUF_INIT; struct strbuf realpath = STRBUF_INIT;
if (!work_tree) if (!work_tree)
@ -147,7 +147,7 @@ char *prefix_path(const char *prefix, int len, const char *path)
{ {
char *r = prefix_path_gently(prefix, len, NULL, path); char *r = prefix_path_gently(prefix, len, NULL, path);
if (!r) { if (!r) {
const char *hint_path = get_git_work_tree(); const char *hint_path = repo_get_work_tree(the_repository);
if (!hint_path) if (!hint_path)
hint_path = repo_get_git_dir(the_repository); hint_path = repo_get_git_dir(the_repository);
die(_("'%s' is outside repository at '%s'"), path, die(_("'%s' is outside repository at '%s'"), path,
@ -475,7 +475,7 @@ int is_inside_git_dir(void)
int is_inside_work_tree(void) int is_inside_work_tree(void)
{ {
if (inside_work_tree < 0) if (inside_work_tree < 0)
inside_work_tree = is_inside_dir(get_git_work_tree()); inside_work_tree = is_inside_dir(repo_get_work_tree(the_repository));
return inside_work_tree; return inside_work_tree;
} }
@ -490,7 +490,7 @@ void setup_work_tree(void)
if (work_tree_config_is_bogus) if (work_tree_config_is_bogus)
die(_("unable to set up work tree using invalid config")); die(_("unable to set up work tree using invalid config"));
work_tree = get_git_work_tree(); work_tree = repo_get_work_tree(the_repository);
if (!work_tree || chdir_notify(work_tree)) if (!work_tree || chdir_notify(work_tree))
die(_("this operation must be run in a work tree")); die(_("this operation must be run in a work tree"));
@ -547,7 +547,7 @@ static void setup_original_cwd(void)
* Get our worktree; we only protect the current working directory * Get our worktree; we only protect the current working directory
* if it's in the worktree. * if it's in the worktree.
*/ */
worktree = get_git_work_tree(); worktree = repo_get_work_tree(the_repository);
if (!worktree) if (!worktree)
goto no_prevention_needed; goto no_prevention_needed;
@ -1062,9 +1062,9 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
set_git_work_tree("."); set_git_work_tree(".");
/* set_git_work_tree() must have been called by now */ /* set_git_work_tree() must have been called by now */
worktree = get_git_work_tree(); worktree = repo_get_work_tree(the_repository);
/* both get_git_work_tree() and cwd are already normalized */ /* both repo_get_work_tree() and cwd are already normalized */
if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */ if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */
set_git_dir(gitdirenv, 0); set_git_dir(gitdirenv, 0);
free(gitfile); free(gitfile);
@ -2192,7 +2192,7 @@ static int create_default_files(const char *template_path,
char *path; char *path;
int reinit; int reinit;
int filemode; int filemode;
const char *work_tree = get_git_work_tree(); const char *work_tree = repo_get_work_tree(the_repository);
/* /*
* First copy the templates -- we might have the default * First copy the templates -- we might have the default

View file

@ -25,7 +25,6 @@
#include "git-compat-util.h" #include "git-compat-util.h"
#include "abspath.h" #include "abspath.h"
#include "environment.h"
#include "repository.h" #include "repository.h"
#include "quote.h" #include "quote.h"
#include "setup.h" #include "setup.h"
@ -308,7 +307,7 @@ void trace_repo_setup(void)
cwd = xgetcwd(); cwd = xgetcwd();
if (!(git_work_tree = get_git_work_tree())) if (!(git_work_tree = repo_get_work_tree(the_repository)))
git_work_tree = "(null)"; git_work_tree = "(null)";
if (!startup_info->prefix) if (!startup_info->prefix)