Skip to content

Commit 18bad9b

Browse files
committed
branch: check for bisects and rebases
The branch_checked_out() helper was added by the previous change, but it used an over-simplified view to check if a branch is checked out. It only focused on the HEAD symref, but ignored whether a bisect or rebase was happening. Teach branch_checked_out() to check for these things, and also add tests to ensure that we do not lose this functionality in the future. Now that this test coverage exists, we can safely refactor validate_new_branchname() to use branch_checked_out(). Note that we need to prepend "refs/heads/" to the 'state.branch' after calling wt_status_check_*(). We also need to duplicate wt->path so the value is not freed at the end of the call. Signed-off-by: Derrick Stolee <[email protected]>
1 parent dbb7eae commit 18bad9b

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

branch.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ static void prepare_checked_out_branches(void)
385385
worktrees = get_worktrees();
386386

387387
while (worktrees[i]) {
388+
struct wt_status_state state = { 0 };
388389
struct worktree *wt = worktrees[i++];
389390

390391
if (wt->is_bare)
@@ -394,6 +395,29 @@ static void prepare_checked_out_branches(void)
394395
strmap_put(&current_checked_out_branches,
395396
wt->head_ref,
396397
xstrdup(wt->path));
398+
399+
if (wt_status_check_rebase(wt, &state) &&
400+
(state.rebase_in_progress || state.rebase_interactive_in_progress) &&
401+
state.branch) {
402+
struct strbuf ref = STRBUF_INIT;
403+
strbuf_addf(&ref, "refs/heads/%s", state.branch);
404+
strmap_put(&current_checked_out_branches,
405+
ref.buf,
406+
xstrdup(wt->path));
407+
strbuf_release(&ref);
408+
}
409+
wt_status_state_free_buffers(&state);
410+
411+
if (wt_status_check_bisect(wt, &state) &&
412+
state.branch) {
413+
struct strbuf ref = STRBUF_INIT;
414+
strbuf_addf(&ref, "refs/heads/%s", state.branch);
415+
strmap_put(&current_checked_out_branches,
416+
ref.buf,
417+
xstrdup(wt->path));
418+
strbuf_release(&ref);
419+
}
420+
wt_status_state_free_buffers(&state);
397421
}
398422

399423
free_worktrees(worktrees);
@@ -419,23 +443,18 @@ int branch_checked_out(const char *refname, char **path)
419443
*/
420444
int validate_new_branchname(const char *name, struct strbuf *ref, int force)
421445
{
422-
struct worktree **worktrees;
423-
const struct worktree *wt;
424-
446+
char *path;
425447
if (!validate_branchname(name, ref))
426448
return 0;
427449

428450
if (!force)
429451
die(_("a branch named '%s' already exists"),
430452
ref->buf + strlen("refs/heads/"));
431453

432-
worktrees = get_worktrees();
433-
wt = find_shared_symref(worktrees, "HEAD", ref->buf);
434-
if (wt && !wt->is_bare)
454+
if (branch_checked_out(ref->buf, &path))
435455
die(_("cannot force update the branch '%s' "
436456
"checked out at '%s'"),
437-
ref->buf + strlen("refs/heads/"), wt->path);
438-
free_worktrees(worktrees);
457+
ref->buf + strlen("refs/heads/"), path);
439458

440459
return 1;
441460
}

t/t2407-worktree-heads.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,33 @@ test_expect_success 'refuse to overwrite: checked out in worktree' '
2121
done
2222
'
2323

24+
test_expect_success 'refuse to overwrite: worktree in bisect' '
25+
test_when_finished test_might_fail git -C wt-4 bisect reset &&
26+
27+
(
28+
git -C wt-4 bisect start &&
29+
git -C wt-4 bisect bad HEAD &&
30+
git -C wt-4 bisect good HEAD~3
31+
) &&
32+
33+
test_must_fail git branch -f wt-4 HEAD 2>err &&
34+
grep "cannot force update the branch '\''wt-4'\'' checked out at" err
35+
'
36+
37+
. "$TEST_DIRECTORY"/lib-rebase.sh
38+
39+
test_expect_success 'refuse to overwrite: worktree in rebase' '
40+
test_when_finished test_might_fail git -C wt-4 rebase --abort &&
41+
42+
(
43+
set_fake_editor &&
44+
FAKE_LINES="edit 1 2 3" \
45+
git -C wt-4 rebase -i HEAD~3 >rebase &&
46+
git -C wt-4 status
47+
) &&
48+
49+
test_must_fail git branch -f wt-4 HEAD 2>err &&
50+
grep "cannot force update the branch '\''wt-4'\'' checked out at" err
51+
'
52+
2453
test_done

0 commit comments

Comments
 (0)