Skip to content

Commit ffe2b81

Browse files
committed
Merge branch 'ps/setup-split-discovery-and-setup' into jch
The repository discovery and repository configuration phases, which were previously intertwined in 'setup.c', have been split. Repository discovery now populates a 'struct repo_discovery' without modifying the repository state, and repository configuration takes this structure to initialize the repository, paving the way for clean unification of repository configuration. * ps/setup-split-discovery-and-setup: setup: mark `set_git_work_tree()` as file-local setup: pass worktree to `init_db()` setup: drop redundant configuration of `startup_info->have_repository` setup: make repository discovery self-contained setup: propagate prefix via repository discovery setup: drop static `cwd` variable setup: move prefix into repository setup: embed repository format in discovery setup: introduce explicit repository discovery setup: split up concerns of `setup_git_env_internal()` setup: unify setup of shallow file setup: mark bogus worktree in `apply_repository_format()` setup: rename `check_repository_format_gently()`
2 parents 59be409 + 6d66931 commit ffe2b81

13 files changed

Lines changed: 283 additions & 241 deletions

File tree

builtin/clone.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,6 @@ int cmd_clone(int argc,
11161116
die_errno(_("could not create work tree dir '%s'"),
11171117
work_tree);
11181118
junk_work_tree = work_tree;
1119-
set_git_work_tree(the_repository, work_tree);
11201119
}
11211120

11221121
if (real_git_dir) {
@@ -1186,9 +1185,10 @@ int cmd_clone(int argc,
11861185
* repository, and reference backends may persist that information into
11871186
* their on-disk data structures.
11881187
*/
1189-
init_db(the_repository, git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN,
1190-
ref_storage_format, NULL,
1191-
do_not_override_repo_unix_permissions, INIT_DB_QUIET | INIT_DB_SKIP_REFDB);
1188+
init_db(the_repository, git_dir, real_git_dir, work_tree, option_template,
1189+
GIT_HASH_UNKNOWN, ref_storage_format, NULL,
1190+
do_not_override_repo_unix_permissions,
1191+
INIT_DB_QUIET | INIT_DB_SKIP_REFDB);
11921192

11931193
if (real_git_dir) {
11941194
free((char *)git_dir);

builtin/init-db.c

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -231,39 +231,25 @@ int cmd_init_db(int argc,
231231
if (!bare) {
232232
const char *git_dir_parent = strrchr(git_dir, '/');
233233

234-
if (work_tree) {
235-
set_git_work_tree(the_repository, work_tree);
236-
} else {
237-
char *work_tree_cfg = NULL;
238-
234+
if (!work_tree) {
239235
if (git_dir_parent) {
240236
char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
241-
work_tree_cfg = real_pathdup(rel, 1);
237+
work_tree = real_pathdup(rel, 1);
242238
free(rel);
239+
} else {
240+
work_tree = xgetcwd();
243241
}
244-
245-
if (!work_tree_cfg)
246-
work_tree_cfg = xgetcwd();
247-
248-
set_git_work_tree(the_repository, work_tree_cfg);
249-
250-
free(work_tree_cfg);
251242
}
252243

253-
if (access(repo_get_work_tree(the_repository), X_OK))
254-
die_errno (_("Cannot access work tree '%s'"),
255-
repo_get_work_tree(the_repository));
256-
}
257-
else {
258-
if (real_git_dir)
259-
die(_("--separate-git-dir incompatible with bare repository"));
260-
if (work_tree)
261-
set_git_work_tree(the_repository, work_tree);
244+
if (access(work_tree, X_OK))
245+
die_errno (_("Cannot access work tree '%s'"), work_tree);
246+
} else if (real_git_dir) {
247+
die(_("--separate-git-dir incompatible with bare repository"));
262248
}
263249

264250
flags |= INIT_DB_EXIST_OK;
265-
ret = init_db(the_repository, git_dir, real_git_dir, template_dir, hash_algo,
266-
ref_storage_format, initial_branch,
251+
ret = init_db(the_repository, git_dir, real_git_dir, work_tree,
252+
template_dir, hash_algo, ref_storage_format, initial_branch,
267253
init_shared_repository, flags);
268254

269255
free(template_dir_to_free);

builtin/repo.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static int get_path_commondir_absolute(struct repository *repo, struct strbuf *b
8484
if (!common_dir)
8585
return error(_("unable to get common directory"));
8686

87-
format_path(buf, common_dir, startup_info->prefix, PATH_FORMAT_CANONICAL);
87+
format_path(buf, common_dir, repo->prefix, PATH_FORMAT_CANONICAL);
8888
return 0;
8989
}
9090

@@ -95,7 +95,7 @@ static int get_path_commondir_relative(struct repository *repo, struct strbuf *b
9595
if (!common_dir)
9696
return error(_("unable to get common directory"));
9797

98-
format_path(buf, common_dir, startup_info->prefix, PATH_FORMAT_RELATIVE);
98+
format_path(buf, common_dir, repo->prefix, PATH_FORMAT_RELATIVE);
9999
return 0;
100100
}
101101

@@ -106,7 +106,7 @@ static int get_path_gitdir_absolute(struct repository *repo, struct strbuf *buf)
106106
if (!git_dir)
107107
return error(_("unable to get git directory"));
108108

109-
format_path(buf, git_dir, startup_info->prefix, PATH_FORMAT_CANONICAL);
109+
format_path(buf, git_dir, repo->prefix, PATH_FORMAT_CANONICAL);
110110
return 0;
111111
}
112112

@@ -117,7 +117,7 @@ static int get_path_gitdir_relative(struct repository *repo, struct strbuf *buf)
117117
if (!git_dir)
118118
return error(_("unable to get git directory"));
119119

120-
format_path(buf, git_dir, startup_info->prefix, PATH_FORMAT_RELATIVE);
120+
format_path(buf, git_dir, repo->prefix, PATH_FORMAT_RELATIVE);
121121
return 0;
122122
}
123123

builtin/rev-parse.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ static int show_file(const char *arg, int output_prefix)
255255
show_default();
256256
if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
257257
if (output_prefix) {
258-
const char *prefix = startup_info->prefix;
258+
const char *prefix = the_repository->prefix;
259259
char *fname = prefix_filename(prefix, arg);
260260
show(fname);
261261
free(fname);
@@ -832,7 +832,8 @@ int cmd_rev_parse(int argc,
832832
prefix = argv[++i];
833833
if (!prefix)
834834
die(_("--prefix requires an argument"));
835-
startup_info->prefix = prefix;
835+
FREE_AND_NULL(the_repository->prefix);
836+
the_repository->prefix = xstrdup(prefix);
836837
output_prefix = 1;
837838
continue;
838839
}

builtin/update-index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ static enum parse_opt_result unresolve_callback(
875875
const char *arg, int unset)
876876
{
877877
int *has_errors = opt->value;
878-
const char *prefix = startup_info->prefix;
878+
const char *prefix = the_repository->prefix;
879879

880880
BUG_ON_OPT_NEG(unset);
881881
BUG_ON_OPT_ARG(arg);
@@ -896,7 +896,7 @@ static enum parse_opt_result reupdate_callback(
896896
const char *arg, int unset)
897897
{
898898
int *has_errors = opt->value;
899-
const char *prefix = startup_info->prefix;
899+
const char *prefix = the_repository->prefix;
900900

901901
BUG_ON_OPT_NEG(unset);
902902
BUG_ON_OPT_ARG(arg);

common-init.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#include "exec-cmd.h"
66
#include "gettext.h"
77
#include "attr.h"
8+
#include "odb.h"
9+
#include "parse.h"
810
#include "repository.h"
11+
#include "replace-object.h"
912
#include "setup.h"
1013
#include "strbuf.h"
1114
#include "trace2.h"
@@ -31,6 +34,22 @@ static void restore_sigpipe_to_default(void)
3134
signal(SIGPIPE, SIG_DFL);
3235
}
3336

37+
static void setup_environment(void)
38+
{
39+
char *git_replace_ref_base;
40+
const char *replace_ref_base;
41+
42+
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
43+
disable_replace_refs();
44+
replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
45+
git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
46+
: "refs/replace/");
47+
update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base);
48+
49+
if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT, 0))
50+
fetch_if_missing = 0;
51+
}
52+
3453
void init_git(const char **argv)
3554
{
3655
struct strbuf tmp = STRBUF_INIT;
@@ -51,6 +70,7 @@ void init_git(const char **argv)
5170
git_setup_gettext();
5271

5372
initialize_repository(the_repository);
73+
setup_environment();
5474

5575
attr_start();
5676

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
306306
} else if (!strcmp(cmd, "--shallow-file")) {
307307
(*argv)++;
308308
(*argc)--;
309-
set_alternate_shallow_file(the_repository, (*argv)[0], 1);
309+
setenv(GIT_SHALLOW_FILE_ENVIRONMENT, (*argv)[0], 1);
310310
if (envchanged)
311311
*envchanged = 1;
312312
} else if (!strcmp(cmd, "-C")) {

object-name.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,8 +1708,8 @@ static char *resolve_relative_path(struct repository *r, const char *rel)
17081708
die(_("relative path syntax can't be used outside working tree"));
17091709

17101710
/* die() inside prefix_path() if resolved path is outside worktree */
1711-
return prefix_path(the_repository, startup_info->prefix,
1712-
startup_info->prefix ? strlen(startup_info->prefix) : 0,
1711+
return prefix_path(the_repository, the_repository->prefix,
1712+
the_repository->prefix ? strlen(the_repository->prefix) : 0,
17131713
rel);
17141714
}
17151715

repository.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ void repo_clear(struct repository *repo)
376376

377377
FREE_AND_NULL(repo->gitdir);
378378
FREE_AND_NULL(repo->commondir);
379+
FREE_AND_NULL(repo->prefix);
379380
FREE_AND_NULL(repo->graft_file);
380381
FREE_AND_NULL(repo->index_file);
381382
FREE_AND_NULL(repo->worktree);

repository.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ struct repository {
5252
*/
5353
char *commondir;
5454

55+
/*
56+
* The "prefix", a path to the current working directory relative to
57+
* the work tree root, or NULL, if the current working directory is not
58+
* a strict subdirectory of the work tree root. The prefix always ends
59+
* with a '/' character.
60+
*/
61+
char *prefix;
62+
5563
/*
5664
* Holds any information related to accessing the raw object content.
5765
*/

0 commit comments

Comments
 (0)