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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
parent
b3f66fd3e3
commit
835183603b
|
|
@ -414,6 +414,7 @@ static const char *const init_db_usage[] = {
|
||||||
int cmd_init_db(int argc, const char **argv, const char *prefix)
|
int cmd_init_db(int argc, const char **argv, const char *prefix)
|
||||||
{
|
{
|
||||||
const char *git_dir;
|
const char *git_dir;
|
||||||
|
const char *work_tree;
|
||||||
const char *template_dir = NULL;
|
const char *template_dir = NULL;
|
||||||
unsigned int flags = 0;
|
unsigned int flags = 0;
|
||||||
const struct option init_db_options[] = {
|
const struct option init_db_options[] = {
|
||||||
|
|
@ -480,8 +481,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
|
||||||
* without --bare. Catch the error early.
|
* without --bare. Catch the error early.
|
||||||
*/
|
*/
|
||||||
git_dir = getenv(GIT_DIR_ENVIRONMENT);
|
git_dir = getenv(GIT_DIR_ENVIRONMENT);
|
||||||
if ((!git_dir || is_bare_repository_cfg == 1)
|
work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
|
||||||
&& getenv(GIT_WORK_TREE_ENVIRONMENT))
|
if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
|
||||||
die("%s (or --work-tree=<directory>) not allowed without "
|
die("%s (or --work-tree=<directory>) not allowed without "
|
||||||
"specifying %s (or --git-dir=<directory>)",
|
"specifying %s (or --git-dir=<directory>)",
|
||||||
GIT_WORK_TREE_ENVIRONMENT,
|
GIT_WORK_TREE_ENVIRONMENT,
|
||||||
|
|
@ -496,7 +497,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
|
||||||
if (is_bare_repository_cfg < 0)
|
if (is_bare_repository_cfg < 0)
|
||||||
is_bare_repository_cfg = guess_repository_type(git_dir);
|
is_bare_repository_cfg = guess_repository_type(git_dir);
|
||||||
|
|
||||||
startup_info->setup_explicit = 1;
|
|
||||||
if (!is_bare_repository_cfg) {
|
if (!is_bare_repository_cfg) {
|
||||||
if (git_dir) {
|
if (git_dir) {
|
||||||
const char *git_dir_parent = strrchr(git_dir, '/');
|
const char *git_dir_parent = strrchr(git_dir, '/');
|
||||||
|
|
@ -511,10 +511,18 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
|
||||||
if (!getcwd(git_work_tree_cfg, PATH_MAX))
|
if (!getcwd(git_work_tree_cfg, PATH_MAX))
|
||||||
die_errno ("Cannot access current working directory");
|
die_errno ("Cannot access current working directory");
|
||||||
}
|
}
|
||||||
|
if (work_tree)
|
||||||
|
set_git_work_tree(make_absolute_path(work_tree));
|
||||||
|
else
|
||||||
|
set_git_work_tree(git_work_tree_cfg);
|
||||||
if (access(get_git_work_tree(), X_OK))
|
if (access(get_git_work_tree(), X_OK))
|
||||||
die_errno ("Cannot access work tree '%s'",
|
die_errno ("Cannot access work tree '%s'",
|
||||||
get_git_work_tree());
|
get_git_work_tree());
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
if (work_tree)
|
||||||
|
set_git_work_tree(make_absolute_path(work_tree));
|
||||||
|
}
|
||||||
|
|
||||||
set_git_dir(make_absolute_path(git_dir));
|
set_git_dir(make_absolute_path(git_dir));
|
||||||
|
|
||||||
|
|
|
||||||
1
cache.h
1
cache.h
|
|
@ -1119,7 +1119,6 @@ const char *split_cmdline_strerror(int cmdline_errno);
|
||||||
/* git.c */
|
/* git.c */
|
||||||
struct startup_info {
|
struct startup_info {
|
||||||
int have_repository;
|
int have_repository;
|
||||||
int setup_explicit;
|
|
||||||
};
|
};
|
||||||
extern struct startup_info *startup_info;
|
extern struct startup_info *startup_info;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -137,36 +137,20 @@ static int git_work_tree_initialized;
|
||||||
*/
|
*/
|
||||||
void set_git_work_tree(const char *new_work_tree)
|
void set_git_work_tree(const char *new_work_tree)
|
||||||
{
|
{
|
||||||
|
if (git_work_tree_initialized) {
|
||||||
|
new_work_tree = make_absolute_path(new_work_tree);
|
||||||
|
if (strcmp(new_work_tree, work_tree))
|
||||||
|
die("internal error: work tree has already been set\n"
|
||||||
|
"Current worktree: %s\nNew worktree: %s",
|
||||||
|
work_tree, new_work_tree);
|
||||||
|
return;
|
||||||
|
}
|
||||||
git_work_tree_initialized = 1;
|
git_work_tree_initialized = 1;
|
||||||
free(work_tree);
|
|
||||||
work_tree = xstrdup(make_absolute_path(new_work_tree));
|
work_tree = xstrdup(make_absolute_path(new_work_tree));
|
||||||
is_bare_repository_cfg = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *get_git_work_tree(void)
|
const char *get_git_work_tree(void)
|
||||||
{
|
{
|
||||||
if (startup_info && !startup_info->setup_explicit) {
|
|
||||||
if (is_bare_repository_cfg == 1)
|
|
||||||
return NULL;
|
|
||||||
if (work_tree)
|
|
||||||
is_bare_repository_cfg = 0;
|
|
||||||
return work_tree;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!git_work_tree_initialized) {
|
|
||||||
work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
|
|
||||||
/* core.bare = true overrides implicit and config work tree */
|
|
||||||
if (!work_tree && is_bare_repository_cfg < 1) {
|
|
||||||
work_tree = git_work_tree_cfg;
|
|
||||||
/* make_absolute_path also normalizes the path */
|
|
||||||
if (work_tree && !is_absolute_path(work_tree))
|
|
||||||
work_tree = xstrdup(make_absolute_path(git_path("%s", work_tree)));
|
|
||||||
} else if (work_tree)
|
|
||||||
work_tree = xstrdup(make_absolute_path(work_tree));
|
|
||||||
git_work_tree_initialized = 1;
|
|
||||||
if (work_tree)
|
|
||||||
is_bare_repository_cfg = 0;
|
|
||||||
}
|
|
||||||
return work_tree;
|
return work_tree;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue