Skip to content

Commit 81ea995

Browse files
authored
Merge pull request libgit2#4630 from tiennou/fix/worktree-from-bare
Worktrees can be made from bare repositories
2 parents a572323 + a82082d commit 81ea995

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/repository.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,15 @@ static int load_config_data(git_repository *repo, const git_config *config)
268268
{
269269
int is_bare;
270270

271+
int err = git_config_get_bool(&is_bare, config, "core.bare");
272+
if (err < 0 && err != GIT_ENOTFOUND)
273+
return err;
274+
271275
/* Try to figure out if it's bare, default to non-bare if it's not set */
272-
if (git_config_get_bool(&is_bare, config, "core.bare") < 0)
273-
repo->is_bare = 0;
276+
if (err != GIT_ENOTFOUND)
277+
repo->is_bare = is_bare && !repo->is_worktree;
274278
else
275-
repo->is_bare = is_bare;
279+
repo->is_bare = 0;
276280

277281
return 0;
278282
}

src/worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static int open_worktree_dir(git_worktree **out, const char *parent, const char
139139
if ((wt->name = git__strdup(name)) == NULL
140140
|| (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL
141141
|| (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL
142-
|| (wt->parent_path = git__strdup(parent)) == NULL
142+
|| (parent && (wt->parent_path = git__strdup(parent)) == NULL)
143143
|| (wt->worktree_path = git_path_dirname(wt->gitlink_path)) == NULL) {
144144
error = -1;
145145
goto out;

tests/worktree/worktree.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,26 @@ void test_worktree_worktree__init(void)
228228
git_repository_free(repo);
229229
}
230230

231+
void test_worktree_worktree__add_from_bare(void)
232+
{
233+
git_worktree *wt;
234+
git_repository *repo, *wtrepo;
235+
236+
repo = cl_git_sandbox_init("short_tag.git");
237+
238+
cl_assert_equal_i(1, git_repository_is_bare(repo));
239+
cl_assert_equal_i(0, git_repository_is_worktree(repo));
240+
241+
cl_git_pass(git_worktree_add(&wt, repo, "worktree-frombare", "worktree-frombare", NULL));
242+
cl_git_pass(git_repository_open(&wtrepo, "worktree-frombare"));
243+
cl_assert_equal_i(0, git_repository_is_bare(wtrepo));
244+
cl_assert_equal_i(1, git_repository_is_worktree(wtrepo));
245+
246+
git_worktree_free(wt);
247+
git_repository_free(repo);
248+
git_repository_free(wtrepo);
249+
}
250+
231251
void test_worktree_worktree__add_locked(void)
232252
{
233253
git_worktree *wt;

0 commit comments

Comments
 (0)