diff options
author | Junio C Hamano <gitster@pobox.com> | 2023-03-20 01:03:11 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2023-03-20 01:03:11 +0300 |
commit | 96a806f87a379fbc54b5cdb889518e9540d954c9 (patch) | |
tree | f0318d5114575e8f6d8b6fca5579d2b1e13feaa1 /branch.c | |
parent | c79786c486cb84e2f9dad41a04d982bb23075815 (diff) | |
parent | 894ea945095b74542c6c4f4aefbe20f5b68be437 (diff) |
Merge branch 'rj/avoid-switching-to-already-used-branch'
A few subcommands have been taught to stop users from working on a
branch that is being used in another worktree linked to the same
repository.
* rj/avoid-switching-to-already-used-branch:
switch: reject if the branch is already checked out elsewhere (test)
rebase: refuse to switch to a branch already checked out elsewhere (test)
branch: fix die_if_checked_out() when ignore_current_worktree
worktree: introduce is_shared_symref()
Diffstat (limited to 'branch.c')
-rw-r--r-- | branch.c | 14 |
1 files changed, 9 insertions, 5 deletions
@@ -821,12 +821,16 @@ void remove_branch_state(struct repository *r, int verbose) void die_if_checked_out(const char *branch, int ignore_current_worktree) { struct worktree **worktrees = get_worktrees(); - const struct worktree *wt; - wt = find_shared_symref(worktrees, "HEAD", branch); - if (wt && (!ignore_current_worktree || !wt->is_current)) { - skip_prefix(branch, "refs/heads/", &branch); - die(_("'%s' is already checked out at '%s'"), branch, wt->path); + for (int i = 0; worktrees[i]; i++) { + if (worktrees[i]->is_current && ignore_current_worktree) + continue; + + if (is_shared_symref(worktrees[i], "HEAD", branch)) { + skip_prefix(branch, "refs/heads/", &branch); + die(_("'%s' is already checked out at '%s'"), + branch, worktrees[i]->path); + } } free_worktrees(worktrees); |