From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS53758 23.128.96.0/24 X-Spam-Status: No, score=-4.4 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by dcvr.yhbt.net (Postfix) with ESMTP id 71FE31F953 for ; Wed, 1 Dec 2021 22:16:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353483AbhLAWUD (ORCPT ); Wed, 1 Dec 2021 17:20:03 -0500 Received: from outgoing-auth-1.mit.edu ([18.9.28.11]:33991 "EHLO outgoing.mit.edu" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1353480AbhLAWTy (ORCPT ); Wed, 1 Dec 2021 17:19:54 -0500 Received: from localhost (198-27-191-186.fiber.dynamic.sonic.net [198.27.191.186]) (authenticated bits=0) (User authenticated as andersk@ATHENA.MIT.EDU) by outgoing.mit.edu (8.14.7/8.12.4) with ESMTP id 1B1MGM8n012673 (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 1 Dec 2021 17:16:23 -0500 From: Anders Kaseorg To: Junio C Hamano Cc: git@vger.kernel.org, Johannes Schindelin , Jeff King , Andreas Heiduk , =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason=20?= , Jiang Xin , Anders Kaseorg Subject: [PATCH v7 8/8] branch: protect branches checked out in all worktrees Date: Wed, 1 Dec 2021 14:15:47 -0800 Message-Id: <20211201221547.1796213-9-andersk@mit.edu> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211201221547.1796213-1-andersk@mit.edu> References: <20211201221547.1796213-1-andersk@mit.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org 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 --- branch.c | 13 +++++++++---- t/t3200-branch.sh | 7 +++++++ 2 files changed, 16 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; } diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index 498cea31bd..9c45170661 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -168,6 +168,13 @@ test_expect_success 'git branch -M foo bar should fail when bar is checked out' test_must_fail git branch -M bar foo ' +test_expect_success 'git branch -M foo bar should fail when bar is checked out in worktree' ' + git branch -f bar && + test_when_finished "git worktree remove wt && git branch -D wt" && + git worktree add wt && + test_must_fail git branch -M bar wt +' + test_expect_success 'git branch -M baz bam should succeed when baz is checked out' ' git checkout -b baz && git branch bam && -- 2.34.1