Skip to content

Commit 8351836

Browse files
pcloudsgitster
authored andcommitted
Remove all logic from get_git_work_tree()
This logic is now only used by cmd_init_db(). setup_* functions do not rely on it any more. Move all the logic to cmd_init_db() and turn get_git_work_tree() into a simple function. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b3f66fd commit 8351836

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

builtin/init-db.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ static const char *const init_db_usage[] = {
414414
int cmd_init_db(int argc, const char **argv, const char *prefix)
415415
{
416416
const char *git_dir;
417+
const char *work_tree;
417418
const char *template_dir = NULL;
418419
unsigned int flags = 0;
419420
const struct option init_db_options[] = {
@@ -480,8 +481,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
480481
* without --bare. Catch the error early.
481482
*/
482483
git_dir = getenv(GIT_DIR_ENVIRONMENT);
483-
if ((!git_dir || is_bare_repository_cfg == 1)
484-
&& getenv(GIT_WORK_TREE_ENVIRONMENT))
484+
work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
485+
if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
485486
die("%s (or --work-tree=<directory>) not allowed without "
486487
"specifying %s (or --git-dir=<directory>)",
487488
GIT_WORK_TREE_ENVIRONMENT,
@@ -496,7 +497,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
496497
if (is_bare_repository_cfg < 0)
497498
is_bare_repository_cfg = guess_repository_type(git_dir);
498499

499-
startup_info->setup_explicit = 1;
500500
if (!is_bare_repository_cfg) {
501501
if (git_dir) {
502502
const char *git_dir_parent = strrchr(git_dir, '/');
@@ -511,10 +511,18 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
511511
if (!getcwd(git_work_tree_cfg, PATH_MAX))
512512
die_errno ("Cannot access current working directory");
513513
}
514+
if (work_tree)
515+
set_git_work_tree(make_absolute_path(work_tree));
516+
else
517+
set_git_work_tree(git_work_tree_cfg);
514518
if (access(get_git_work_tree(), X_OK))
515519
die_errno ("Cannot access work tree '%s'",
516520
get_git_work_tree());
517521
}
522+
else {
523+
if (work_tree)
524+
set_git_work_tree(make_absolute_path(work_tree));
525+
}
518526

519527
set_git_dir(make_absolute_path(git_dir));
520528

cache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,6 @@ const char *split_cmdline_strerror(int cmdline_errno);
11191119
/* git.c */
11201120
struct startup_info {
11211121
int have_repository;
1122-
int setup_explicit;
11231122
};
11241123
extern struct startup_info *startup_info;
11251124

environment.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -137,36 +137,20 @@ static int git_work_tree_initialized;
137137
*/
138138
void set_git_work_tree(const char *new_work_tree)
139139
{
140+
if (git_work_tree_initialized) {
141+
new_work_tree = make_absolute_path(new_work_tree);
142+
if (strcmp(new_work_tree, work_tree))
143+
die("internal error: work tree has already been set\n"
144+
"Current worktree: %s\nNew worktree: %s",
145+
work_tree, new_work_tree);
146+
return;
147+
}
140148
git_work_tree_initialized = 1;
141-
free(work_tree);
142149
work_tree = xstrdup(make_absolute_path(new_work_tree));
143-
is_bare_repository_cfg = 0;
144150
}
145151

146152
const char *get_git_work_tree(void)
147153
{
148-
if (startup_info && !startup_info->setup_explicit) {
149-
if (is_bare_repository_cfg == 1)
150-
return NULL;
151-
if (work_tree)
152-
is_bare_repository_cfg = 0;
153-
return work_tree;
154-
}
155-
156-
if (!git_work_tree_initialized) {
157-
work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
158-
/* core.bare = true overrides implicit and config work tree */
159-
if (!work_tree && is_bare_repository_cfg < 1) {
160-
work_tree = git_work_tree_cfg;
161-
/* make_absolute_path also normalizes the path */
162-
if (work_tree && !is_absolute_path(work_tree))
163-
work_tree = xstrdup(make_absolute_path(git_path("%s", work_tree)));
164-
} else if (work_tree)
165-
work_tree = xstrdup(make_absolute_path(work_tree));
166-
git_work_tree_initialized = 1;
167-
if (work_tree)
168-
is_bare_repository_cfg = 0;
169-
}
170154
return work_tree;
171155
}
172156

0 commit comments

Comments
 (0)