Skip to content

Commit 47a2eea

Browse files
committed
worktree: detect from secondary worktree if main worktree is bare
Setup: 1. Have a bare repo with core.bare = true in config.worktree 2. Create a new worktree Behavior: From the secondary worktree the main worktree appears as non-bare. Expected: From the secondary worktree the main worktree should appear as bare. Why current behavior is not good? If the main worktree is detected as not bare it doesn't allow checking out the branch of the main worktree. There are possibly other problems associated with that behavior. Why is it happening? While we're inside the secondary worktree we don't initialize the main worktree's repository with its configuration. How is it fixed? Load actual configs of the main worktree. Also, skip the config loading step if we're already inside the current worktree because in that case we rely on is_bare_repository() to return the correct result. Other solutions considered: Alternatively, instead of incorrectly always using `the_repository` as the main worktree's repository, we can detect and load the actual repository of the main worktree and then use that repository's `is_bare` value extracted from correct configs. However, this approach is a bit riskier and could also affect performance. Since we had the assignment `worktree->repo = the_repository` for a long time already, I decided it's safe to keep it as it is for now; it can be still fixed separately from this change. Real life use case: 1. Have a bare repo 2. Create a worktree from the bare repo 3. In the secondary worktree enable sparse-checkout - this enables extensions.worktreeConfig and keeps core.bare=true setting in config.worktree of the bare worktree 4. The secondary worktree or any other non-bare worktree created won't be able to use branch main (not even once), but it should be able to. Signed-off-by: Olga Pilipenco <[email protected]>
1 parent 25b0f41 commit 47a2eea

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

t/t3200-branch.sh

+14
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,20 @@ test_expect_success 'bare main worktree has HEAD at branch deleted by secondary
411411
git -C secondary branch -D main
412412
'
413413

414+
test_expect_success 'secondary worktree can switch to main if common dir is bare worktree' '
415+
test_when_finished "rm -rf bare_repo non_bare_repo secondary_worktree" &&
416+
git init -b main non_bare_repo &&
417+
test_commit -C non_bare_repo x &&
418+
419+
git clone --bare non_bare_repo bare_repo &&
420+
git -C bare_repo config extensions.worktreeConfig true &&
421+
git -C bare_repo config unset core.bare &&
422+
git -C bare_repo config --worktree core.bare true &&
423+
424+
git -C bare_repo worktree add ../secondary_worktree &&
425+
git -C secondary_worktree checkout main
426+
'
427+
414428
test_expect_success 'git branch --list -v with --abbrev' '
415429
test_when_finished "git branch -D t" &&
416430
git branch t &&

worktree.c

+30-9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ static int is_current_worktree(struct worktree *wt)
6464
return is_current;
6565
}
6666

67+
static int is_bare_git_dir(const char *git_dir)
68+
{
69+
int bare = 0;
70+
struct config_set cs = { { 0 } };
71+
char *config_file;
72+
char *worktree_config_file;
73+
74+
config_file = xstrfmt("%s/config", git_dir);
75+
worktree_config_file = xstrfmt("%s/config.worktree", git_dir);
76+
77+
git_configset_init(&cs);
78+
git_configset_add_file(&cs, config_file);
79+
git_configset_add_file(&cs, worktree_config_file);
80+
81+
git_configset_get_bool(&cs, "core.bare", &bare);
82+
83+
cleanup:
84+
git_configset_clear(&cs);
85+
free(config_file);
86+
free(worktree_config_file);
87+
return bare;
88+
}
89+
6790
/**
6891
* get the main worktree
6992
*/
@@ -76,18 +99,16 @@ static struct worktree *get_main_worktree(int skip_reading_head)
7699
strbuf_strip_suffix(&worktree_path, "/.git");
77100

78101
CALLOC_ARRAY(worktree, 1);
102+
/*
103+
* NEEDSWORK: the_repository is not always main worktree's repository
104+
*/
79105
worktree->repo = the_repository;
80106
worktree->path = strbuf_detach(&worktree_path, NULL);
81-
/*
82-
* NEEDSWORK: If this function is called from a secondary worktree and
83-
* config.worktree is present, is_bare_repository_cfg will reflect the
84-
* contents of config.worktree, not the contents of the main worktree.
85-
* This means that worktree->is_bare may be set to 0 even if the main
86-
* worktree is configured to be bare.
87-
*/
88-
worktree->is_bare = (is_bare_repository_cfg == 1) ||
89-
is_bare_repository();
90107
worktree->is_current = is_current_worktree(worktree);
108+
worktree->is_bare = (is_bare_repository_cfg == 1) ||
109+
is_bare_repository() ||
110+
(!worktree->is_current && is_bare_git_dir(repo_get_common_dir(the_repository)));
111+
91112
if (!skip_reading_head)
92113
add_head_info(worktree);
93114
return worktree;

0 commit comments

Comments
 (0)