worktree.c: check whether branch is bisected in another worktree

Similar to the rebase case, we want to detect if "HEAD" in some worktree is being bisected because 1) we do not want to checkout this branch in another worktree, after bisect is done it will want to go back to this branch 2) we do not want to delete the branch is either or git bisect will fail to return to the (long gone) branch Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 22, 2016 at 20:01 UTC 04a3dfb8b5993fcb807fcbca8ed7ea608d30a204
2 files changed +32
t/t2025-worktree-add.sh
+13
@@ -263,4 +263,17 @@ test_expect_success 'check out from current worktree branch ok' '
263 )
264 '
265
266 +test_expect_success 'checkout a branch under bisect' '
267 + git worktree add under-bisect &&
268 + (
269 + cd under-bisect &&
270 + git bisect start &&
271 + git bisect bad &&
272 + git bisect good HEAD~2 &&
273 + git worktree list | grep "under-bisect.*detached HEAD" &&
274 + test_must_fail git worktree add new-bisect under-bisect &&
275 + ! test -d new-bisect
276 + )
277 +'
278 +
279 test_done
worktree.c
+19
@@ -234,6 +234,21 @@ static int is_worktree_being_rebased(const struct worktree *wt,
234 return found_rebase;
235 }
236
237 +static int is_worktree_being_bisected(const struct worktree *wt,
238 + const char *target)
239 +{
240 + struct wt_status_state state;
241 + int found_rebase;
242 +
243 + memset(&state, 0, sizeof(state));
244 + found_rebase = wt_status_check_bisect(wt, &state) &&
245 + state.branch &&
246 + starts_with(target, "refs/heads/") &&
247 + !strcmp(state.branch, target + strlen("refs/heads/"));
248 + free(state.branch);
249 + return found_rebase;
250 +}
251 +
252 /*
253 * note: this function should be able to detect shared symref even if
254 * HEAD is temporarily detached (e.g. in the middle of rebase or
@@ -261,6 +276,10 @@ const struct worktree *find_shared_symref(const char *symref,
276 existing = wt;
277 break;
278 }
279 + if (is_worktree_being_bisected(wt, target)) {
280 + existing = wt;
281 + break;
282 + }
283 }
284
285 strbuf_reset(&path);