Skip to content

Commit 7502b23

Browse files
committed
Merge branch 'jk/init-core-worktree-at-root'
We avoid setting core.worktree when the repository location is the ".git" directory directly at the top level of the working tree, but the code misdetected the case in which the working tree is at the root level of the filesystem (which arguably is a silly thing to do, but still valid). * jk/init-core-worktree-at-root: init: don't set core.worktree when initializing /.git
2 parents b02a94d + 84ccad8 commit 7502b23

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

builtin/init-db.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ static int git_init_db_config(const char *k, const char *v, void *cb)
182182
return 0;
183183
}
184184

185+
/*
186+
* If the git_dir is not directly inside the working tree, then git will not
187+
* find it by default, and we need to set the worktree explicitly.
188+
*/
189+
static int needs_work_tree_config(const char *git_dir, const char *work_tree)
190+
{
191+
if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
192+
return 0;
193+
if (skip_prefix(git_dir, work_tree, &git_dir) &&
194+
!strcmp(git_dir, "/.git"))
195+
return 0;
196+
return 1;
197+
}
198+
185199
static int create_default_files(const char *template_path)
186200
{
187201
const char *git_dir = get_git_dir();
@@ -274,10 +288,8 @@ static int create_default_files(const char *template_path)
274288
/* allow template config file to override the default */
275289
if (log_all_ref_updates == -1)
276290
git_config_set("core.logallrefupdates", "true");
277-
if (!starts_with(git_dir, work_tree) ||
278-
strcmp(git_dir + strlen(work_tree), "/.git")) {
291+
if (needs_work_tree_config(git_dir, work_tree))
279292
git_config_set("core.worktree", work_tree);
280-
}
281293
}
282294

283295
if (!reinit) {

0 commit comments

Comments
 (0)