aboutsummaryrefslogtreecommitdiffstats
path: root/branch.c
diff options
context:
space:
mode:
authorAnders Kaseorg <andersk@mit.edu>2021-12-01 14:15:47 -0800
committerJunio C Hamano <gitster@pobox.com>2021-12-01 22:18:25 -0800
commit593a2a5d0639b4b4f91ff6e6ffb64e72020f8fd8 (patch)
tree2903e1a81584b4d98ec45d96134fbffefc517917 /branch.c
parent9fdf4f1db422cc259e4a3ce0023a255102c6fa3b (diff)
downloadgit-593a2a5d0639b4b4f91ff6e6ffb64e72020f8fd8.tar.gz
branch: protect branches checked out in all worktrees
Refuse to force-move a branch over the currently checked out branch of any working tree, not just the current one. Signed-off-by: Anders Kaseorg <andersk@mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'branch.c')
-rw-r--r--branch.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/branch.c b/branch.c
index c66b222abd..2cfe496d24 100644
--- a/branch.c
+++ b/branch.c
@@ -199,7 +199,8 @@ int validate_branchname(const char *name, struct strbuf *ref)
*/
int validate_new_branchname(const char *name, struct strbuf *ref, int force)
{
- const char *head;
+ struct worktree **worktrees;
+ const struct worktree *wt;
if (!validate_branchname(name, ref))
return 0;
@@ -208,9 +209,13 @@ int validate_new_branchname(const char *name, struct strbuf *ref, int force)
die(_("a branch named '%s' already exists"),
ref->buf + strlen("refs/heads/"));
- head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
- if (!is_bare_repository() && head && !strcmp(head, ref->buf))
- die(_("cannot force update the current branch"));
+ worktrees = get_worktrees();
+ wt = find_shared_symref(worktrees, "HEAD", ref->buf);
+ if (wt && !wt->is_bare)
+ die(_("cannot force update the branch '%s'"
+ "checked out at '%s'"),
+ ref->buf + strlen("refs/heads/"), wt->path);
+ free_worktrees(worktrees);
return 1;
}