Skip to content

Commit bda0839

Browse files
authored
Merge pull request libgit2#4982 from pks-t/pks/worktree-add-bare-head
Enable creation of worktree from bare repo's default branch
2 parents 4800593 + bf013fc commit bda0839

File tree

7 files changed

+114
-33
lines changed

7 files changed

+114
-33
lines changed

src/branch.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,20 @@ static int branch_equals(git_repository *repo, const char *path, void *payload)
153153

154154
int git_branch_is_checked_out(const git_reference *branch)
155155
{
156-
assert(branch && git_reference_is_branch(branch));
156+
git_repository *repo;
157+
int flags = 0;
158+
159+
assert(branch);
160+
161+
if (!git_reference_is_branch(branch))
162+
return 0;
163+
164+
repo = git_reference_owner(branch);
165+
166+
if (git_repository_is_bare(repo))
167+
flags |= GIT_REPOSITORY_FOREACH_HEAD_SKIP_REPO;
157168

158-
return git_repository_foreach_head(git_reference_owner(branch),
159-
branch_equals, (void *) branch) == 1;
169+
return git_repository_foreach_head(repo, branch_equals, flags, (void *) branch) == 1;
160170
}
161171

162172
int git_branch_delete(git_reference *branch)

src/refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ static int reference__rename(git_reference **out, git_reference *ref, const char
692692
payload.old_name = ref->name;
693693
memcpy(&payload.new_name, &normalized, sizeof(normalized));
694694

695-
error = git_repository_foreach_head(repo, update_wt_heads, &payload);
695+
error = git_repository_foreach_head(repo, update_wt_heads, 0, &payload);
696696
}
697697

698698
return error;

src/repository.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,30 +2210,37 @@ int git_repository_head_for_worktree(git_reference **out, git_repository *repo,
22102210
return error;
22112211
}
22122212

2213-
int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload)
2213+
int git_repository_foreach_head(git_repository *repo,
2214+
git_repository_foreach_head_cb cb,
2215+
int flags, void *payload)
22142216
{
22152217
git_strarray worktrees = GIT_VECTOR_INIT;
22162218
git_buf path = GIT_BUF_INIT;
22172219
int error;
22182220
size_t i;
22192221

2220-
/* Execute callback for HEAD of commondir */
2221-
if ((error = git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE)) < 0 ||
2222-
(error = cb(repo, path.ptr, payload) != 0))
2223-
goto out;
22242222

2225-
if ((error = git_worktree_list(&worktrees, repo)) < 0) {
2226-
error = 0;
2227-
goto out;
2223+
if (!(flags & GIT_REPOSITORY_FOREACH_HEAD_SKIP_REPO)) {
2224+
/* Gather HEAD of main repository */
2225+
if ((error = git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE)) < 0 ||
2226+
(error = cb(repo, path.ptr, payload) != 0))
2227+
goto out;
22282228
}
22292229

2230-
/* Execute callback for all worktree HEADs */
2231-
for (i = 0; i < worktrees.count; i++) {
2232-
if (get_worktree_file_path(&path, repo, worktrees.strings[i], GIT_HEAD_FILE) < 0)
2233-
continue;
2234-
2235-
if ((error = cb(repo, path.ptr, payload)) != 0)
2230+
if (!(flags & GIT_REPOSITORY_FOREACH_HEAD_SKIP_WORKTREES)) {
2231+
if ((error = git_worktree_list(&worktrees, repo)) < 0) {
2232+
error = 0;
22362233
goto out;
2234+
}
2235+
2236+
/* Gather HEADs of all worktrees */
2237+
for (i = 0; i < worktrees.count; i++) {
2238+
if (get_worktree_file_path(&path, repo, worktrees.strings[i], GIT_HEAD_FILE) < 0)
2239+
continue;
2240+
2241+
if ((error = cb(repo, path.ptr, payload)) != 0)
2242+
goto out;
2243+
}
22372244
}
22382245

22392246
out:

src/repository.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ int git_repository_create_head(const char *git_dir, const char *ref_name);
176176
*/
177177
typedef int (*git_repository_foreach_head_cb)(git_repository *repo, const char *path, void *payload);
178178

179+
enum {
180+
/* Skip enumeration of the main repository HEAD */
181+
GIT_REPOSITORY_FOREACH_HEAD_SKIP_REPO = (1u << 0),
182+
/* Skip enumeration of worktree HEADs */
183+
GIT_REPOSITORY_FOREACH_HEAD_SKIP_WORKTREES = (1u << 1),
184+
};
185+
179186
/*
180187
* Iterate over repository and all worktree HEADs.
181188
*
@@ -184,7 +191,9 @@ typedef int (*git_repository_foreach_head_cb)(git_repository *repo, const char *
184191
* executed with the given payload. The return value equals the
185192
* return value of the last executed callback function.
186193
*/
187-
int git_repository_foreach_head(git_repository *repo, git_repository_foreach_head_cb cb, void *payload);
194+
int git_repository_foreach_head(git_repository *repo,
195+
git_repository_foreach_head_cb cb,
196+
int flags, void *payload);
188197

189198
/*
190199
* Weak pointers to repository internals.

src/worktree.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ int git_worktree_add(git_worktree **out, git_repository *repo,
290290

291291
*out = NULL;
292292

293+
if (wtopts.ref) {
294+
if (!git_reference_is_branch(wtopts.ref)) {
295+
git_error_set(GIT_ERROR_WORKTREE, "reference is not a branch");
296+
err = -1;
297+
goto out;
298+
}
299+
300+
if (git_branch_is_checked_out(wtopts.ref)) {
301+
git_error_set(GIT_ERROR_WORKTREE, "reference is already checked out");
302+
err = -1;
303+
goto out;
304+
}
305+
}
306+
293307
/* Create gitdir directory ".git/worktrees/<name>" */
294308
if ((err = git_buf_joinpath(&gitdir, repo->commondir, "worktrees")) < 0)
295309
goto out;
@@ -342,18 +356,6 @@ int git_worktree_add(git_worktree **out, git_repository *repo,
342356

343357
/* Set up worktree reference */
344358
if (wtopts.ref) {
345-
if (!git_reference_is_branch(wtopts.ref)) {
346-
git_error_set(GIT_ERROR_WORKTREE, "reference is not a branch");
347-
err = -1;
348-
goto out;
349-
}
350-
351-
if (git_branch_is_checked_out(wtopts.ref)) {
352-
git_error_set(GIT_ERROR_WORKTREE, "reference is already checked out");
353-
err = -1;
354-
goto out;
355-
}
356-
357359
if ((err = git_reference_dup(&ref, wtopts.ref)) < 0)
358360
goto out;
359361
} else {

tests/refs/branches/checkedout.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "clar_libgit2.h"
2+
#include "refs.h"
3+
#include "worktree/worktree_helpers.h"
4+
5+
static git_repository *repo;
6+
7+
static void assert_checked_out(git_repository *repo, const char *branch, int checked_out)
8+
{
9+
git_reference *ref;
10+
11+
cl_git_pass(git_reference_lookup(&ref, repo, branch));
12+
cl_assert(git_branch_is_checked_out(ref) == checked_out);
13+
14+
git_reference_free(ref);
15+
}
16+
17+
void test_refs_branches_checkedout__simple_repo(void)
18+
{
19+
repo = cl_git_sandbox_init("testrepo");
20+
assert_checked_out(repo, "refs/heads/master", 1);
21+
assert_checked_out(repo, "refs/heads/executable", 0);
22+
cl_git_sandbox_cleanup();
23+
}
24+
25+
void test_refs_branches_checkedout__worktree(void)
26+
{
27+
static worktree_fixture fixture =
28+
WORKTREE_FIXTURE_INIT("testrepo", "testrepo-worktree");
29+
30+
setup_fixture_worktree(&fixture);
31+
32+
assert_checked_out(fixture.repo, "refs/heads/master", 1);
33+
assert_checked_out(fixture.repo, "refs/heads/testrepo-worktree", 1);
34+
35+
assert_checked_out(fixture.worktree, "refs/heads/master", 1);
36+
assert_checked_out(fixture.worktree, "refs/heads/testrepo-worktree", 1);
37+
38+
cleanup_fixture_worktree(&fixture);
39+
}
40+
41+
void test_refs_branches_checkedout__head_is_not_checked_out(void)
42+
{
43+
repo = cl_git_sandbox_init("testrepo");
44+
assert_checked_out(repo, "HEAD", 0);
45+
cl_git_sandbox_cleanup();
46+
}
47+
48+
void test_refs_branches_checkedout__master_in_bare_repo_is_not_checked_out(void)
49+
{
50+
repo = cl_git_sandbox_init("testrepo.git");
51+
assert_checked_out(repo, "refs/heads/master", 0);
52+
cl_git_sandbox_cleanup();
53+
}

tests/worktree/worktree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,8 @@ void test_worktree_worktree__foreach_head_gives_same_results_in_wt_and_repo(void
604604
cl_git_pass(git_reference_lookup(&heads[0], fixture.repo, GIT_HEAD_FILE));
605605
cl_git_pass(git_reference_lookup(&heads[1], fixture.worktree, GIT_HEAD_FILE));
606606

607-
cl_git_pass(git_repository_foreach_head(fixture.repo, read_head_ref, &repo_refs));
608-
cl_git_pass(git_repository_foreach_head(fixture.worktree, read_head_ref, &worktree_refs));
607+
cl_git_pass(git_repository_foreach_head(fixture.repo, read_head_ref, 0, &repo_refs));
608+
cl_git_pass(git_repository_foreach_head(fixture.worktree, read_head_ref, 0, &worktree_refs));
609609

610610
cl_assert_equal_i(repo_refs.length, ARRAY_SIZE(heads));
611611
cl_assert_equal_i(worktree_refs.length, ARRAY_SIZE(heads));

0 commit comments

Comments
 (0)