Skip to content

Commit 6311cfa

Browse files
pcloudsgitster
authored andcommitted
init: do not set unnecessary core.worktree
The function needs_work_tree_config() that is called from create_default_files() is supposed to be fed the path to ".git" that looks as if it is at the top of the working tree, and decide if that location matches the actual worktree being used. This comparison allows "git init" to decide if core.worktree needs to be recorded in the working tree. In the current code, however, we feed the return value from get_git_dir(), which can be totally different from what the function expects when "gitdir" file is involved. Instead of giving the path to the ".git" at the top of the working tree, we end up feeding the actual path that the file points at. This original location of ".git" however is only known to init_db(). Make init_db() save it and have it passed to create_default_files() as a new parameter, which passes the correct location down to needs_work_tree_config() to fix this. Noticed-by: Max Nordlund <[email protected]> Helped-by: Michael J Gruber <[email protected]> Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1bd1907 commit 6311cfa

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

builtin/init-db.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ static int needs_work_tree_config(const char *git_dir, const char *work_tree)
171171
return 1;
172172
}
173173

174-
static int create_default_files(const char *template_path)
174+
static int create_default_files(const char *template_path,
175+
const char *original_git_dir)
175176
{
176177
struct stat st1;
177178
struct strbuf buf = STRBUF_INIT;
@@ -263,7 +264,7 @@ static int create_default_files(const char *template_path)
263264
/* allow template config file to override the default */
264265
if (log_all_ref_updates == -1)
265266
git_config_set("core.logallrefupdates", "true");
266-
if (needs_work_tree_config(get_git_dir(), work_tree))
267+
if (needs_work_tree_config(original_git_dir, work_tree))
267268
git_config_set("core.worktree", work_tree);
268269
}
269270

@@ -337,6 +338,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
337338
{
338339
int reinit;
339340
int exist_ok = flags & INIT_DB_EXIST_OK;
341+
char *original_git_dir = xstrdup(real_path(git_dir));
340342

341343
if (real_git_dir) {
342344
struct stat st;
@@ -375,7 +377,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
375377
*/
376378
check_repository_format();
377379

378-
reinit = create_default_files(template_dir);
380+
reinit = create_default_files(template_dir, original_git_dir);
379381

380382
create_object_directory();
381383

@@ -412,6 +414,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
412414
git_dir, len && git_dir[len-1] != '/' ? "/" : "");
413415
}
414416

417+
free(original_git_dir);
415418
return 0;
416419
}
417420

t/t0001-init.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,11 @@ test_expect_success 're-init from a linked worktree' '
400400
test_commit first &&
401401
git worktree add ../linked-worktree &&
402402
mv .git/info/exclude expected-exclude &&
403+
cp .git/config expected-config &&
403404
find .git/worktrees -print | sort >expected &&
404405
git -C ../linked-worktree init &&
405406
test_cmp expected-exclude .git/info/exclude &&
407+
test_cmp expected-config .git/config &&
406408
find .git/worktrees -print | sort >actual &&
407409
test_cmp expected actual
408410
)

0 commit comments

Comments
 (0)