From patchwork Sun Mar 26 22:33:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13188444 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C3E5C74A5B for ; Sun, 26 Mar 2023 22:33:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229596AbjCZWdI (ORCPT ); Sun, 26 Mar 2023 18:33:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjCZWdH (ORCPT ); Sun, 26 Mar 2023 18:33:07 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE4F85254 for ; Sun, 26 Mar 2023 15:33:05 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id l37so4019916wms.2 for ; Sun, 26 Mar 2023 15:33:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679869984; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:cc:to:from:subject:from:to:cc :subject:date:message-id:reply-to; bh=toK5B9WWBmA3jJXXLztDdz8Jxjtik+Fe/YbmP8A/VnQ=; b=N0USfUX7uFM9HN1spchrCcy9ud+TXEaaNO4w8tWHPCzW/utyVy0m2rQLXyc2ZQ0EE+ 3Oc0/SSHBn1IIVXPAa0xTEjmTIRskY2WtBL5Xo5FLonjbBWwB0C7fKxM1jGyAbkjbGdW wRE2p1AZYOuak2nFoZ3STLsf+pLRn/qfTxZUWlplpm63+u1aSNfInKOFT7D5NOTvRO/E 9M3Vo/+vpEMtlcfZcGIWTngMP1x4nUwfeT7Kmf5NrvIC/Su5s9XOPIrJ7qk8GlZSxNS/ 1AmhaHLUgTTplQjj8p34ZaK2NaAZKeN/d84VtNW29P4ksGjBhXZLDrJIMBvFGFO+psFg 0oSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679869984; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:cc:to:from:subject :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=toK5B9WWBmA3jJXXLztDdz8Jxjtik+Fe/YbmP8A/VnQ=; b=1jhUrM0o7BDZ4DNDOah5Ydkf2W8w/lJX/Pr2G+5K+ByLdbQN0SJjH4TQrCJBy4UqoN xSHITM71KeRMGztL3i9+UHE7SrdmTJsbDcINlsXF0BmumC84310f3UN3WLkxXOAC/8NL N/RFyRtHGfdJo6Xhl5cTUSCBBqVvI9iLPpzKAhOLvJXylUwxlTYufP1SBJ64Jr/tZr5O tv9v9AJ/U8jwxCEltWnhAOjunXFUrqWZlDwpMidtrswyom2/3aapn6c5Yk3EMdqKuh8S l0HLNycHwTw1i4GD0bv+Rxu4tTW0UsoIXrHpPVsVQgSJt7B43L5olQckSdDaRHF6h4v1 g0oQ== X-Gm-Message-State: AO0yUKXBZIZIcgvNonQoQhV88nSv3S10mGkNSf1GnLMUhQkM47Ds0Yqs 8Mjm6X+xLPmfInaQgDvNhOU= X-Google-Smtp-Source: AK7set9syhagd19aw4E/i1ahsbPaDOX/oegGA4WOFsIxDz+Bv+mo6lsZQ2ekCIlkdC61rujEHFc3xA== X-Received: by 2002:a7b:ca4a:0:b0:3ed:af6b:7fb3 with SMTP id m10-20020a7bca4a000000b003edaf6b7fb3mr8056560wml.2.1679869984357; Sun, 26 Mar 2023 15:33:04 -0700 (PDT) Received: from [192.168.2.52] (96.red-88-14-208.dynamicip.rima-tde.net. [88.14.208.96]) by smtp.gmail.com with ESMTPSA id i11-20020a05600c290b00b003ee20b4b2dasm11595829wmd.46.2023.03.26.15.33.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 26 Mar 2023 15:33:04 -0700 (PDT) Subject: [PATCH v5 1/5] branch: test for failures while renaming branches From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Jonathan Tan References: <34a58449-4f2e-66ef-ea01-119186aebd23@gmail.com> <2193a4ed-b263-068e-92f8-847dcb053f8c@gmail.com> <139555f1-21ab-d987-a58e-c7f04ce222d3@gmail.com> Message-ID: <2c22e77e-314f-1025-3355-740e19b743fa@gmail.com> Date: Mon, 27 Mar 2023 00:33:02 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When we introduced replace_each_worktree_head_symref() in 70999e9cec (branch -m: update all per-worktree HEADs, 2016-03-27), we implemented a best effort approach. If we are asked to rename a branch that is simultaneously checked out in multiple worktrees, we try to update all of those worktrees. If we fail updating any of them, we die() as a signal that something has gone wrong. However, at this point, the branch ref has already been renamed and also updated the HEADs of the successfully updated worktrees. Despite returning an error, we do not try to rollback those changes. Let's add a test to notice if we change this behavior in the future. In next commits we will change replace_each_worktree_head_symref() to work more closely with its only caller, copy_or_rename_branch(). Let's move the former closer to its caller, to facilitate those changes. Signed-off-by: Rubén Justo --- branch.c | 27 --------------------------- branch.h | 8 -------- builtin/branch.c | 32 ++++++++++++++++++++++++++++++++ t/t3200-branch.sh | 15 +++++++++++++++ 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/branch.c b/branch.c index eacef62b7c..b45454593a 100644 --- a/branch.c +++ b/branch.c @@ -835,30 +835,3 @@ void die_if_checked_out(const char *branch, int ignore_current_worktree) free_worktrees(worktrees); } - -int replace_each_worktree_head_symref(const char *oldref, const char *newref, - const char *logmsg) -{ - int ret = 0; - struct worktree **worktrees = get_worktrees(); - int i; - - for (i = 0; worktrees[i]; i++) { - struct ref_store *refs; - - if (worktrees[i]->is_detached) - continue; - if (!worktrees[i]->head_ref) - continue; - if (strcmp(oldref, worktrees[i]->head_ref)) - continue; - - refs = get_worktree_ref_store(worktrees[i]); - if (refs_create_symref(refs, "HEAD", newref, logmsg)) - ret = error(_("HEAD of working tree %s is not updated"), - worktrees[i]->path); - } - - free_worktrees(worktrees); - return ret; -} diff --git a/branch.h b/branch.h index ef56103c05..30c01aed73 100644 --- a/branch.h +++ b/branch.h @@ -155,12 +155,4 @@ int read_branch_desc(struct strbuf *, const char *branch_name); */ void die_if_checked_out(const char *branch, int ignore_current_worktree); -/* - * Update all per-worktree HEADs pointing at the old ref to point the new ref. - * This will be used when renaming a branch. Returns 0 if successful, non-zero - * otherwise. - */ -int replace_each_worktree_head_symref(const char *oldref, const char *newref, - const char *logmsg); - #endif diff --git a/builtin/branch.c b/builtin/branch.c index f63fd45edb..c7ace2f2da 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -509,6 +509,38 @@ static void reject_rebase_or_bisect_branch(const char *target) free_worktrees(worktrees); } +/* + * Update all per-worktree HEADs pointing at the old ref to point the new ref. + * This will be used when renaming a branch. Returns 0 if successful, non-zero + * otherwise. + */ +static int replace_each_worktree_head_symref(const char *oldref, const char *newref, + const char *logmsg) +{ + int ret = 0; + struct worktree **worktrees = get_worktrees(); + int i; + + for (i = 0; worktrees[i]; i++) { + struct ref_store *refs; + + if (worktrees[i]->is_detached) + continue; + if (!worktrees[i]->head_ref) + continue; + if (strcmp(oldref, worktrees[i]->head_ref)) + continue; + + refs = get_worktree_ref_store(worktrees[i]); + if (refs_create_symref(refs, "HEAD", newref, logmsg)) + ret = error(_("HEAD of working tree %s is not updated"), + worktrees[i]->path); + } + + free_worktrees(worktrees); + return ret; +} + static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force) { struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT; diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index 5a8a48287c..7abd938e15 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -239,6 +239,21 @@ test_expect_success 'git branch -M baz bam should succeed when baz is checked ou git worktree prune ' +test_expect_success 'git branch -M fails if updating any linked working tree fails' ' + git worktree add -b baz bazdir1 && + git worktree add -f bazdir2 baz && + touch .git/worktrees/bazdir1/HEAD.lock && + test_must_fail git branch -M baz bam && + test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam && + git branch -M bam baz && + rm .git/worktrees/bazdir1/HEAD.lock && + touch .git/worktrees/bazdir2/HEAD.lock && + test_must_fail git branch -M baz bam && + test $(git -C bazdir1 rev-parse --abbrev-ref HEAD) = bam && + rm -rf bazdir1 bazdir2 && + git worktree prune +' + test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' ' git checkout -b baz && git worktree add -f bazdir baz && From patchwork Sun Mar 26 22:33:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13188445 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4CB0EC77B60 for ; Sun, 26 Mar 2023 22:33:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229675AbjCZWdQ (ORCPT ); Sun, 26 Mar 2023 18:33:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjCZWdP (ORCPT ); Sun, 26 Mar 2023 18:33:15 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A718B5B95 for ; Sun, 26 Mar 2023 15:33:13 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id r29so6790166wra.13 for ; Sun, 26 Mar 2023 15:33:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679869992; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:cc:to:from:subject:from:to:cc :subject:date:message-id:reply-to; bh=k3eEDgVMjqRKrLoyBhLp9klTbhEdzKp+KDFdtTVEKDQ=; b=cpFJ0BcY0yPOkbCl8+rBwZ4MATBW1QvKDJ5Avf7PQr6pqJKutMWFxKTdzD5AS6QDra TBKW505KbGt/PAm+N+NM8KBB8FcR2xjQjyEZNrtDy9cMjQubLDdsRgVspEdFWEbw06iW RhiPLwBi/2mqUCRRq78/ZpIhwj83/Y1wlZpwBkCEHSx1X2UhvZsVOG3my/4UZukuP+f5 AQuw0OLrcgUfhRos8x5C5u40CnG1lmXkhj/+CF8OtKeihei4vrVtYEAReKQt1TnOgFqx 4zkxiV7gTVOoCM1C/4jrQuv5D/gQSFvvTDQSDBdjEr5evCMvYWZjCyujg/Va996oHV3l lrIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679869992; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:cc:to:from:subject :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=k3eEDgVMjqRKrLoyBhLp9klTbhEdzKp+KDFdtTVEKDQ=; b=3gJbtRysYKdKB9NB4asWhLGSiMjasUTd1Km+hU4+SZeLNitW7vEY8dv12NGBeWnFgn NuOiLOP/YXiX5vNYuAvZ3uHlgE2CNlk7uHFRLnTnJhFbPa7sg3XLk74H134vUYLL7TAC jHPWwpmU8njfQ6EyqFXINVIqSs5lP2Pj8E9mOb1XMq8SvhSqpcC+MwS++H0kajZQ5zav A1D7/IPWJHQYvhOE5yJgMy+v6eFLXsEEz4jslzdagybhLecUN9kcPHozQwW1YvYyHZgb NHBuCpmoc4mvL+uiqt5gkthdnZjyBy+S3WRHPq2b3bBXPyIaEpiWK6U4smO8qRFK5T6H i2JQ== X-Gm-Message-State: AAQBX9eqYmnbLQPxvDu/ioM1bFNOrgBAT7CX61w38t+d408BrRj8DLcQ nmBNX8ZbZ1R3y1P3YjiHSYI= X-Google-Smtp-Source: AKy350YBIFr6gdPNPRq4Y5vx7s5+xo3j2YW5PG5K0VI+OngoWJsUQR0IU8wMs4XrD9OZeDyPgRZIbA== X-Received: by 2002:adf:fa92:0:b0:2ce:9e0a:10ae with SMTP id h18-20020adffa92000000b002ce9e0a10aemr8105818wrr.53.1679869992125; Sun, 26 Mar 2023 15:33:12 -0700 (PDT) Received: from [192.168.2.52] (96.red-88-14-208.dynamicip.rima-tde.net. [88.14.208.96]) by smtp.gmail.com with ESMTPSA id m6-20020a056000008600b002cde25fba30sm23574729wrx.1.2023.03.26.15.33.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 26 Mar 2023 15:33:11 -0700 (PDT) Subject: [PATCH v5 2/5] branch: use get_worktrees() in copy_or_rename_branch() From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Jonathan Tan References: <34a58449-4f2e-66ef-ea01-119186aebd23@gmail.com> <2193a4ed-b263-068e-92f8-847dcb053f8c@gmail.com> <139555f1-21ab-d987-a58e-c7f04ce222d3@gmail.com> Message-ID: <4b1386fd-1bf7-6fdf-5c0b-49b564b1a7f4@gmail.com> Date: Mon, 27 Mar 2023 00:33:09 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Obtaining the list of worktrees, using get_worktrees(), is not a lightweight operation, because it involves reading from disk. Let's stop calling get_worktrees() in reject_rebase_or_bisect_branch() and in replace_each_worktree_head_symref(). Make them receive the list of worktrees from their only caller, copy_or_rename_branch(). Signed-off-by: Rubén Justo --- builtin/branch.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index c7ace2f2da..bac67c27d5 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -486,9 +486,9 @@ static void print_current_branch_name(void) die(_("HEAD (%s) points outside of refs/heads/"), refname); } -static void reject_rebase_or_bisect_branch(const char *target) +static void reject_rebase_or_bisect_branch(struct worktree **worktrees, + const char *target) { - struct worktree **worktrees = get_worktrees(); int i; for (i = 0; worktrees[i]; i++) { @@ -505,8 +505,6 @@ static void reject_rebase_or_bisect_branch(const char *target) die(_("Branch %s is being bisected at %s"), target, wt->path); } - - free_worktrees(worktrees); } /* @@ -514,11 +512,11 @@ static void reject_rebase_or_bisect_branch(const char *target) * This will be used when renaming a branch. Returns 0 if successful, non-zero * otherwise. */ -static int replace_each_worktree_head_symref(const char *oldref, const char *newref, +static int replace_each_worktree_head_symref(struct worktree **worktrees, + const char *oldref, const char *newref, const char *logmsg) { int ret = 0; - struct worktree **worktrees = get_worktrees(); int i; for (i = 0; worktrees[i]; i++) { @@ -537,7 +535,6 @@ static int replace_each_worktree_head_symref(const char *oldref, const char *new worktrees[i]->path); } - free_worktrees(worktrees); return ret; } @@ -548,6 +545,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int const char *interpreted_oldname = NULL; const char *interpreted_newname = NULL; int recovery = 0; + struct worktree **worktrees = get_worktrees(); if (strbuf_check_branch_ref(&oldref, oldname)) { /* @@ -576,7 +574,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int else validate_new_branchname(newname, &newref, force); - reject_rebase_or_bisect_branch(oldref.buf); + reject_rebase_or_bisect_branch(worktrees, oldref.buf); if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) || !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) { @@ -607,7 +605,8 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int } if (!copy && - replace_each_worktree_head_symref(oldref.buf, newref.buf, logmsg.buf)) + replace_each_worktree_head_symref(worktrees, oldref.buf, newref.buf, + logmsg.buf)) die(_("Branch renamed to %s, but HEAD is not updated!"), newname); strbuf_release(&logmsg); @@ -622,6 +621,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int strbuf_release(&newref); strbuf_release(&oldsection); strbuf_release(&newsection); + free_worktrees(worktrees); } static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION") From patchwork Sun Mar 26 22:33:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13188446 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3B640C7619A for ; Sun, 26 Mar 2023 22:33:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229679AbjCZWd1 (ORCPT ); Sun, 26 Mar 2023 18:33:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjCZWd0 (ORCPT ); Sun, 26 Mar 2023 18:33:26 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9089A5FC2 for ; Sun, 26 Mar 2023 15:33:21 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id s13so4008952wmr.4 for ; Sun, 26 Mar 2023 15:33:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679870000; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:cc:to:from:subject:from:to:cc :subject:date:message-id:reply-to; bh=C/DkrLXu7pjfDBF3lHvNI6Ice7JfF4XQfo/OLyP8uYI=; b=by0SOzqbfW70mUH4EfflnG/zXACv6NP8UidQmbqRLMOSUtRcFk2mplywkNjtLqfkD/ jL8UeX+Z13OsCcjkahubSp/Nqb1sVUmj6UkFaVREOOmxXmi/dvHdHz3Jo7NnD7EI4Tos N3rQus+khy8UOtSoHFdVe6IcFDrydebXrnCJQbnCosQxcEBO32zoykAoe9b3TXqQw/SM i1xpIXB3Qjmsp3+gaWouMNEH9V7ODWQR8kJ75B3FeqOHA2Kq7N8IjNIsMkDDnmv/4hkR iRKJ0mNdeZFcUHCGQ/DWI0CWZGEyytj7qPss1Pmi7QRXP3g5aEDtF6RVTPNIf2QMPVfV YhfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679870000; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:cc:to:from:subject :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=C/DkrLXu7pjfDBF3lHvNI6Ice7JfF4XQfo/OLyP8uYI=; b=qUwxqIL9A0/BT2aGNNkrm4ceArFB+IPnxeoCR4csGNGpCtj7rl607fx8CnJ08C4cYJ Lfxp8HPio0JztZ0RuGcot4KjaZMWVtztuOX2ciQzS0Qxru9wcRRVjfd0WkylE5fUOWs9 Ts1WUSrK7xu9oB4aedc27y3tM+6q9rHU3sZmdZMLN3rRxXX4REA6sXrdYmvhGzkoXdkw 5UiA+qqL5Um44T2xG6KYBFLAs1H5uBLkqaaWtWAW5euhVbVQ7FnK6Y81R6uNR9uqMwhk IpgGCmGYvJLDOeSpg5J6xmubhTuc+4/YpxfrjAEr0HRgozXW9M0G0QXuGjZG65PqRZ+5 7pnw== X-Gm-Message-State: AO0yUKUlT1NATTkOm22jDFlhst8hGvCjcQ9d9ITWW01KcF1SwM7faFJe 08+tldg5NWT7cdZdrY9rQZjQEQ9tk+Y= X-Google-Smtp-Source: AK7set+1PTTo5t5ii85A/K6tG6lz8nNoEBFMDJVsb8IBgKVLtZODf3IFnD6IoFv1aXKvGtc2Vfn5Og== X-Received: by 2002:a7b:ce16:0:b0:3ed:9b20:c7c1 with SMTP id m22-20020a7bce16000000b003ed9b20c7c1mr7074620wmc.20.1679869999862; Sun, 26 Mar 2023 15:33:19 -0700 (PDT) Received: from [192.168.2.52] (96.red-88-14-208.dynamicip.rima-tde.net. [88.14.208.96]) by smtp.gmail.com with ESMTPSA id l18-20020a05600c4f1200b003ef67848a21sm4833596wmq.13.2023.03.26.15.33.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 26 Mar 2023 15:33:19 -0700 (PDT) Subject: [PATCH v5 3/5] branch: description for orphan branch errors From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Jonathan Tan References: <34a58449-4f2e-66ef-ea01-119186aebd23@gmail.com> <2193a4ed-b263-068e-92f8-847dcb053f8c@gmail.com> <139555f1-21ab-d987-a58e-c7f04ce222d3@gmail.com> Message-ID: Date: Mon, 27 Mar 2023 00:33:17 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In bcfc82bd48 (branch: description for non-existent branch errors, 2022-10-08) we checked the HEAD in the current worktree to detect if the branch to operate with is an orphan branch, so as to avoid the confusing error: "No branch named...". If we are asked to operate with an orphan branch in a different working tree than the current one, we need to check the HEAD in that different working tree. Let's extend the check we did in bcfc82bd48, to check the HEADs in all worktrees linked to the current repository, using the helper introduced in 31ad6b61bd (branch: add branch_checked_out() helper, 2022-06-15). The helper, branch_checked_out(), does its work obtaining internally a list of worktrees linked to the current repository. Obtaining that list is not a lightweight work because it implies disk access. In copy_or_rename_branch() we already have a list of worktrees. Let's use that already obtained list, and avoid using here the helper. Signed-off-by: Rubén Justo --- builtin/branch.c | 21 ++++++++++++++++----- t/t3202-show-branch.sh | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index bac67c27d5..90dcbb0c6e 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -538,13 +538,15 @@ static int replace_each_worktree_head_symref(struct worktree **worktrees, return ret; } +#define IS_HEAD 1 + static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force) { struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT; struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT; const char *interpreted_oldname = NULL; const char *interpreted_newname = NULL; - int recovery = 0; + int recovery = 0, oldref_usage = 0; struct worktree **worktrees = get_worktrees(); if (strbuf_check_branch_ref(&oldref, oldname)) { @@ -558,8 +560,17 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int die(_("Invalid branch name: '%s'"), oldname); } - if ((copy || strcmp(head, oldname)) && !ref_exists(oldref.buf)) { - if (copy && !strcmp(head, oldname)) + for (int i = 0; worktrees[i]; i++) { + struct worktree *wt = worktrees[i]; + + if (wt->head_ref && !strcmp(oldref.buf, wt->head_ref)) { + oldref_usage |= IS_HEAD; + break; + } + } + + if ((copy || !(oldref_usage & IS_HEAD)) && !ref_exists(oldref.buf)) { + if (oldref_usage & IS_HEAD) die(_("No commit on branch '%s' yet."), oldname); else die(_("No branch named '%s'."), oldname); @@ -838,7 +849,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) strbuf_addf(&branch_ref, "refs/heads/%s", branch_name); if (!ref_exists(branch_ref.buf)) - error((!argc || !strcmp(head, branch_name)) + error((!argc || branch_checked_out(branch_ref.buf)) ? _("No commit on branch '%s' yet.") : _("No branch named '%s'."), branch_name); @@ -883,7 +894,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) } if (!ref_exists(branch->refname)) { - if (!argc || !strcmp(head, branch->name)) + if (!argc || branch_checked_out(branch->refname)) die(_("No commit on branch '%s' yet."), branch->name); die(_("branch '%s' does not exist"), branch->name); } diff --git a/t/t3202-show-branch.sh b/t/t3202-show-branch.sh index ea7cfd1951..be20ebe1d5 100755 --- a/t/t3202-show-branch.sh +++ b/t/t3202-show-branch.sh @@ -221,4 +221,22 @@ test_expect_success 'fatal descriptions on non-existent branch' ' test_cmp expect actual ' +test_expect_success 'error descriptions on orphan branch' ' + test_when_finished git worktree remove -f wt && + git worktree add wt --detach && + git -C wt checkout --orphan orphan-branch && + test_branch_op_in_wt() { + test_orphan_error() { + test_must_fail git $* 2>actual && + test_i18ngrep "No commit on branch .orphan-branch. yet.$" actual + } && + test_orphan_error -C wt branch $1 $2 && # implicit branch + test_orphan_error -C wt branch $1 orphan-branch $2 && # explicit branch + test_orphan_error branch $1 orphan-branch $2 # different worktree + } && + test_branch_op_in_wt --edit-description && + test_branch_op_in_wt --set-upstream-to=ne && + test_branch_op_in_wt -c new-branch +' + test_done From patchwork Sun Mar 26 22:33:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13188447 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2780CC74A5B for ; Sun, 26 Mar 2023 22:33:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229456AbjCZWde (ORCPT ); Sun, 26 Mar 2023 18:33:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229677AbjCZWdd (ORCPT ); Sun, 26 Mar 2023 18:33:33 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A32E35BB7 for ; Sun, 26 Mar 2023 15:33:30 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id m6-20020a05600c3b0600b003ee6e324b19so4077029wms.1 for ; Sun, 26 Mar 2023 15:33:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679870009; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:cc:to:from:subject:from:to:cc :subject:date:message-id:reply-to; bh=j57P/WfSTCKB9tc8adZCFsCizHoqldcjpkxjP0CpWrU=; b=N9ye3rLkKlF/7/q24jxZTJRdEkL6z1rlx78uFNlTEi86tXoqgLHwHcUDn9TQx6rM86 r6SMwVYx7aybRXAbcLE/6QEBvK0vqBEtx5e+7mfdg3slCLJnfXJEq1O9lxCqs2Qmozu4 NVLMt4FmDSTclYVvuJpzCvkBZGM7FabuPTNVC86ITR9Gv2sTZe5sbXJ6l52aWIILyy95 d9eDoeis3TmBr+IlTqgPKJ3+fXewTFQiauQEgbp51pnRoRnEhASKZpWgGdmZ9hP5xGbY k0w0jQ8w7pB/keDmkyvp+QKxz4p9haXjYKvNCymsy7z2SZwJpYhjrGV6LUQ1gp05A3yR yCSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679870009; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:cc:to:from:subject :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=j57P/WfSTCKB9tc8adZCFsCizHoqldcjpkxjP0CpWrU=; b=WwAvdKYymK/tcGizNb6UAuN9AO+FEI6gZknSQEpDoR5kFLTKGu0A8jtrNnXfv9OwQM ZGkb572xpUIw/0x+010Kt42THInDCG7UnMrFTr9Sp1AxTyOJf8AnRr8lDxL+fl6N4TVK 6EshpbT97wAQIaw+nsp0btahESNI4wMbMzYAImvCT1Qb2E99r5vkpYerZlqWHCPBlmwa 5LbxNX6agpoaUS4/u4JGKIeKRBKWZz2MfKSKcdTCIFjuWXq1U6p1ftHgh26twmWwzkBB LpM+fFDyhs+Il6CfPXdi8FN8XX8f8pefunoZzkNRltMxDAzI+EfPP+GtI9HGBylkdJTx /+Fw== X-Gm-Message-State: AO0yUKVNVdHbVNNgLn/EVs0n8SGAH2Rhgzv0C7+A6Oca0n3SMfkroZDK 3jQZWbgMhXmF91flSppcF1A= X-Google-Smtp-Source: AK7set9ZYeGSrOAaebef2Gpu3rV6yF71i18CFTEvh9P6mVnJAdx4unsvg9AdvWAm3lVGIP7Xi30Rjg== X-Received: by 2002:a7b:cbd2:0:b0:3ed:2619:6485 with SMTP id n18-20020a7bcbd2000000b003ed26196485mr8157378wmi.3.1679870009087; Sun, 26 Mar 2023 15:33:29 -0700 (PDT) Received: from [192.168.2.52] (96.red-88-14-208.dynamicip.rima-tde.net. [88.14.208.96]) by smtp.gmail.com with ESMTPSA id v15-20020a5d590f000000b002cfec8b7f89sm23634693wrd.77.2023.03.26.15.33.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 26 Mar 2023 15:33:28 -0700 (PDT) Subject: [PATCH v5 4/5] branch: rename orphan branches in any worktree From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Jonathan Tan References: <34a58449-4f2e-66ef-ea01-119186aebd23@gmail.com> <2193a4ed-b263-068e-92f8-847dcb053f8c@gmail.com> <139555f1-21ab-d987-a58e-c7f04ce222d3@gmail.com> Message-ID: Date: Mon, 27 Mar 2023 00:33:27 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In cfaff3aac (branch -m: allow renaming a yet-unborn branch, 2020-12-13) we added support for renaming an orphan branch when that branch is checked out in the current worktree. Let's also allow renaming an orphan branch checked out in a worktree different than the current one. Signed-off-by: Rubén Justo --- builtin/branch.c | 6 ++++-- t/t3200-branch.sh | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index 90dcbb0c6e..a93b9fc0ab 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -539,6 +539,7 @@ static int replace_each_worktree_head_symref(struct worktree **worktrees, } #define IS_HEAD 1 +#define IS_ORPHAN 2 static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force) { @@ -565,6 +566,8 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int if (wt->head_ref && !strcmp(oldref.buf, wt->head_ref)) { oldref_usage |= IS_HEAD; + if (is_null_oid(&wt->head_oid)) + oldref_usage |= IS_ORPHAN; break; } } @@ -599,8 +602,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int strbuf_addf(&logmsg, "Branch: renamed %s to %s", oldref.buf, newref.buf); - if (!copy && - (!head || strcmp(oldname, head) || !is_null_oid(&head_oid)) && + if (!copy && !(oldref_usage & IS_ORPHAN) && rename_ref(oldref.buf, newref.buf, logmsg.buf)) die(_("Branch rename failed")); if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf)) diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh index 7abd938e15..98b6c8ac34 100755 --- a/t/t3200-branch.sh +++ b/t/t3200-branch.sh @@ -298,6 +298,20 @@ test_expect_success 'git branch -M and -C fail on detached HEAD' ' test_cmp expect err ' +test_expect_success 'git branch -m should work with orphan branches' ' + test_when_finished git checkout - && + test_when_finished git worktree remove -f wt && + git worktree add wt --detach && + # rename orphan in another worktreee + git -C wt checkout --orphan orphan-foo-wt && + git branch -m orphan-foo-wt orphan-bar-wt && + test orphan-bar-wt=$(git -C orphan-worktree branch --show-current) && + # rename orphan in the current worktree + git checkout --orphan orphan-foo && + git branch -m orphan-foo orphan-bar && + test orphan-bar=$(git branch --show-current) +' + test_expect_success 'git branch -d on orphan HEAD (merged)' ' test_when_finished git checkout main && git checkout --orphan orphan && From patchwork Sun Mar 26 22:33:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Rub=C3=A9n_Justo?= X-Patchwork-Id: 13188448 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 28D32C74A5B for ; Sun, 26 Mar 2023 22:33:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229736AbjCZWds (ORCPT ); Sun, 26 Mar 2023 18:33:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229677AbjCZWdp (ORCPT ); Sun, 26 Mar 2023 18:33:45 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A845A5BB0 for ; Sun, 26 Mar 2023 15:33:43 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id i5-20020a05600c354500b003edd24054e0so6213810wmq.4 for ; Sun, 26 Mar 2023 15:33:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679870022; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:cc:to:from:subject:from:to:cc :subject:date:message-id:reply-to; bh=D0eDrvLFfET3SCwzd47DF8gAlmuhuF22T0GNWHaMMlo=; b=F6I6TnxeKAE7GGjZk9A71fZY3PJdoukAGD5gZEiOD95X3DWGYtSzBHDqu/CyeiImca C334FD9FSN4RsEZSPiRujPYpskIrXLIFIps7S41fhhFSN6XjEkPiTAnyRN22AXouwcgh rR9T2W/1ze8XFCSVc7DA6pk9LCG3yTC9lZfKr8SwhG6JRMcOp7+rruBby67WZDcKXre+ NeyDm+eZNBFMvKc9bdFMvxvmuMiIIf0wQJy2HpwyvH7+fjm+WFdb5VyJW+MgBYd/25e0 wLOke4EshfnDBPoR5Fsyt3AMi4Bl0N0kU4e8sMuCyfK5asuvXwxfXpZEQcA1mJ0SKn86 HzeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679870022; h=content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:references:cc:to:from:subject :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=D0eDrvLFfET3SCwzd47DF8gAlmuhuF22T0GNWHaMMlo=; b=5AV8VwWwit1oJnPqjiYYUNRuT++ZQS6vXOwDJZQAqdE66Q0pYdFnzUCz5+iW9JgWXW 0g7B+sl7faz/QN4BpcWUqjyE4lQKWeipIA0/7bJXj4VUUzNWZpRLJj4pDnKPtRGQ2ZCk tolLtdTY8TOVvySFUW82V5CPRMxwvwWWC7LQNl/KttwrhtZ+3b31wptcir8w0sPoS7aa OGcUP1exl+VHYTRJNXaoKrePdh/oTeNtgzLPhJIyHode/HAYB9tWVnUCzTd2XuERjCsh ZIJOEdLx8Br6RLlvY7/KWzLhuybndMVCWpEBe9uyco8gBgP/sXkwO2+q/aIrznoacI2P o+4A== X-Gm-Message-State: AO0yUKXPmrZz5Ehac4P5aMKYsElMD2+knp8x5856mns5AqlQYVWUDBW5 f5TQwG3lJR8z5K5fI4xT8YM= X-Google-Smtp-Source: AK7set8LE6kRtREjbqY3hPtzrXB9snmVmxP5/B3gEEr3YZ0iLGEaifZlCNIJBt4jPOGQ6ApnBWL+iw== X-Received: by 2002:a7b:c84e:0:b0:3db:8de:6993 with SMTP id c14-20020a7bc84e000000b003db08de6993mr7261567wml.4.1679870022122; Sun, 26 Mar 2023 15:33:42 -0700 (PDT) Received: from [192.168.2.52] (96.red-88-14-208.dynamicip.rima-tde.net. [88.14.208.96]) by smtp.gmail.com with ESMTPSA id n20-20020a7bc5d4000000b003ee10fb56ebsm11727359wmk.9.2023.03.26.15.33.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 26 Mar 2023 15:33:41 -0700 (PDT) Subject: [PATCH v5 5/5] branch: avoid unnecessary worktrees traversals From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Jonathan Tan References: <34a58449-4f2e-66ef-ea01-119186aebd23@gmail.com> <2193a4ed-b263-068e-92f8-847dcb053f8c@gmail.com> <139555f1-21ab-d987-a58e-c7f04ce222d3@gmail.com> Message-ID: Date: Mon, 27 Mar 2023 00:33:40 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When we rename a branch ref, we need to update any worktree that have its HEAD pointing to the branch ref being renamed, so to make it use the new ref name. If we know in advance that we're renaming a branch that is not currently checked out in any worktree, we can skip this step entirely. Let's do it so. Signed-off-by: Rubén Justo --- builtin/branch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/branch.c b/builtin/branch.c index a93b9fc0ab..6a564b22b0 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -617,7 +617,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int interpreted_oldname); } - if (!copy && + if (!copy && (oldref_usage & IS_HEAD) && replace_each_worktree_head_symref(worktrees, oldref.buf, newref.buf, logmsg.buf)) die(_("Branch renamed to %s, but HEAD is not updated!"), newname);