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 <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jun 14, 2022 at 19:27 UTC d2ba271aad0e7f90b475be6225c59cb4f1bfbe4f
2 files changed +49 -8
branch.c
+27 -8
@@ -362,6 +362,7 @@ static void prepare_checked_out_branches(void)
362 worktrees = get_worktrees();
363
364 while (worktrees[i]) {
365 + struct wt_status_state state = { 0 };
366 struct worktree *wt = worktrees[i++];
367
368 if (wt->is_bare)
@@ -371,6 +372,29 @@ static void prepare_checked_out_branches(void)
372 strmap_put(&current_checked_out_branches,
373 wt->head_ref,
374 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);
398 }
399
400 free_worktrees(worktrees);
@@ -390,9 +414,7 @@ const char *branch_checked_out(const char *refname)
414 */
415 int validate_new_branchname(const char *name, struct strbuf *ref, int force)
416 {
393 - struct worktree **worktrees;
394 - const struct worktree *wt;
395 -
417 + const char *path;
418 if (!validate_branchname(name, ref))
419 return 0;
420
@@ -400,13 +422,10 @@ int validate_new_branchname(const char *name, struct strbuf *ref, int force)
422 die(_("a branch named '%s' already exists"),
423 ref->buf + strlen("refs/heads/"));
424
403 - worktrees = get_worktrees();
404 - wt = find_shared_symref(worktrees, "HEAD", ref->buf);
405 - if (wt && !wt->is_bare)
425 + if ((path = branch_checked_out(ref->buf)))
426 die(_("cannot force update the branch '%s' "
427 "checked out at '%s'"),
408 - ref->buf + strlen("refs/heads/"), wt->path);
409 - free_worktrees(worktrees);
428 + ref->buf + strlen("refs/heads/"), path);
429
430 return 1;
431 }
t/t2407-worktree-heads.sh
+22
@@ -26,4 +26,26 @@ test_expect_success 'refuse to overwrite: checked out in worktree' '
26 done
27 '
28
29 +test_expect_success 'refuse to overwrite: worktree in bisect' '
30 + test_when_finished rm -rf .git/worktrees/wt-*/BISECT_* &&
31 +
32 + touch .git/worktrees/wt-4/BISECT_LOG &&
33 + echo refs/heads/fake-2 >.git/worktrees/wt-4/BISECT_START &&
34 +
35 + test_must_fail git branch -f fake-2 HEAD 2>err &&
36 + grep "cannot force update the branch '\''fake-2'\'' checked out at.*wt-4" err
37 +'
38 +
39 +test_expect_success 'refuse to overwrite: worktree in rebase' '
40 + test_when_finished rm -rf .git/worktrees/wt-*/rebase-merge &&
41 +
42 + mkdir -p .git/worktrees/wt-3/rebase-merge &&
43 + touch .git/worktrees/wt-3/rebase-merge/interactive &&
44 + echo refs/heads/fake-1 >.git/worktrees/wt-3/rebase-merge/head-name &&
45 + echo refs/heads/fake-2 >.git/worktrees/wt-3/rebase-merge/onto &&
46 +
47 + test_must_fail git branch -f fake-1 HEAD 2>err &&
48 + grep "cannot force update the branch '\''fake-1'\'' checked out at.*wt-3" err
49 +'
50 +
51 test_done