Skip to content

Commit a58d55b

Browse files
derrickstoleegitster
authored andcommitted
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bd260c8 commit a58d55b

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
@@ -362,6 +362,7 @@ static void prepare_checked_out_branches(void)
362362
worktrees = get_worktrees();
363363

364364
while (worktrees[i]) {
365+
struct wt_status_state state = { 0 };
365366
struct worktree *wt = worktrees[i++];
366367

367368
if (wt->is_bare)
@@ -371,6 +372,29 @@ static void prepare_checked_out_branches(void)
371372
strmap_put(&current_checked_out_branches,
372373
wt->head_ref,
373374
xstrdup(wt->path));
375+
376+
if (wt_status_check_rebase(wt, &state) &&
377+
(state.rebase_in_progress || state.rebase_interactive_in_progress) &&
378+
state.branch) {
379+
struct strbuf ref = STRBUF_INIT;
380+
strbuf_addf(&ref, "refs/heads/%s", state.branch);
381+
strmap_put(&current_checked_out_branches,
382+
ref.buf,
383+
xstrdup(wt->path));
384+
strbuf_release(&ref);
385+
}
386+
wt_status_state_free_buffers(&state);
387+
388+
if (wt_status_check_bisect(wt, &state) &&
389+
state.branch) {
390+
struct strbuf ref = STRBUF_INIT;
391+
strbuf_addf(&ref, "refs/heads/%s", state.branch);
392+
strmap_put(&current_checked_out_branches,
393+
ref.buf,
394+
xstrdup(wt->path));
395+
strbuf_release(&ref);
396+
}
397+
wt_status_state_free_buffers(&state);
374398
}
375399

376400
free_worktrees(worktrees);
@@ -396,23 +420,18 @@ int branch_checked_out(const char *refname, char **path)
396420
*/
397421
int validate_new_branchname(const char *name, struct strbuf *ref, int force)
398422
{
399-
struct worktree **worktrees;
400-
const struct worktree *wt;
401-
423+
char *path;
402424
if (!validate_branchname(name, ref))
403425
return 0;
404426

405427
if (!force)
406428
die(_("a branch named '%s' already exists"),
407429
ref->buf + strlen("refs/heads/"));
408430

409-
worktrees = get_worktrees();
410-
wt = find_shared_symref(worktrees, "HEAD", ref->buf);
411-
if (wt && !wt->is_bare)
431+
if (branch_checked_out(ref->buf, &path))
412432
die(_("cannot force update the branch '%s' "
413433
"checked out at '%s'"),
414-
ref->buf + strlen("refs/heads/"), wt->path);
415-
free_worktrees(worktrees);
434+
ref->buf + strlen("refs/heads/"), path);
416435

417436
return 1;
418437
}

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)