From patchwork Sat Feb 25 14:21:51 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: 13152218 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 9637DC6FA8E for ; Sat, 25 Feb 2023 14:21:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229620AbjBYOV5 (ORCPT ); Sat, 25 Feb 2023 09:21:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbjBYOV4 (ORCPT ); Sat, 25 Feb 2023 09:21:56 -0500 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 7925D12877 for ; Sat, 25 Feb 2023 06:21:54 -0800 (PST) Received: by mail-wm1-x333.google.com with SMTP id ay29-20020a05600c1e1d00b003e9f4c2b623so4203589wmb.3 for ; Sat, 25 Feb 2023 06:21:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1677334913; 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=k7nblxR5IqPkMDhIwUwGVspgEq0WwHbrjFMvIUf0fhU=; b=IvUOaEvUsIDq911X/r2thBMAl/zmrALblJfVgv8H1eYp+ThvV1o5/ngDxsQYdRSwpB 3OUL3BFLODt12lmiGD0oDXvycrf06wa8MA4/ptJ2EVdSUFj2+r3G47hkfreDSG3kFE42 tpUDpTG7GKRxKONg64ccPHi+I42cLVs6ryovD32/9QHFgalL8siLpq9WhZmQ0/9k27rH 2YachitoVCQphWL9DojA/xbfw+u7vRhx7Tvj7pqq+z/UUkS0YerFLZj0CKdLxaHeE+8f oeusPWs2BBkr6skn7vh71JBMabk3BQ+JcRM6tVZpXY5E2uFPiSZ/+H78N7GVbqIJ6SGe TLnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677334913; 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=k7nblxR5IqPkMDhIwUwGVspgEq0WwHbrjFMvIUf0fhU=; b=J7Rxl/pjkap4VQqQRdaYntU2l/T4iLARTM/Cd7pvvK51roEvNh9db1VpwDEbqCKNxA r9O/m1NAvSbNgMA9mHT7DdS74meI5nV8XevspPTtW7xX2b9cTiktao9GMnhCKJJebfPM jKJbyuZArPlX2KLC45meUwaqOr0c5iDWse5Gj7AmBeFGGqTTSnDhtJ6S25zMCsXWiilM rFiwfUfz4VrAxQZFyHOwYDNHaT/NLUnwrAmworK59pGO/8SpS8kAySdyoRKTyFNCHOsl V/0/TnsvaatVf0vnqGiagimoNtODbqKg0giL9jVVLdc7UkR4NjeMEQRJySPkao+iGwA9 +M6Q== X-Gm-Message-State: AO0yUKWzKrwNbYqXKDzIakGktrWlyqH/A/vb13PRDqi1/HVp2v97ATQK vpMj7Gzg+y7RSUriPa8uLPA= X-Google-Smtp-Source: AK7set/INNpbskzt4YdXsdF8E+JoVF83gGUVXqkqfiCtyuKLR8HM7xoSpph+4KUNLWQLxiyOcByMWg== X-Received: by 2002:a05:600c:2e88:b0:3eb:38e6:f64d with SMTP id p8-20020a05600c2e8800b003eb38e6f64dmr793001wmn.39.1677334912911; Sat, 25 Feb 2023 06:21:52 -0800 (PST) Received: from [192.168.2.52] (59.red-88-14-203.dynamicip.rima-tde.net. [88.14.203.59]) by smtp.gmail.com with ESMTPSA id r1-20020a1c2b01000000b003e9ae2a8cbfsm2867897wmr.2.2023.02.25.06.21.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 25 Feb 2023 06:21:52 -0800 (PST) Subject: [PATCH v4 1/4] worktree: introduce is_shared_symref() From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Phillip Wood , Eric Sunshine References: <6fed3b1b-1c4e-9298-19b6-7ad9c04c87dd@gmail.com> Message-ID: Date: Sat, 25 Feb 2023 15:21:51 +0100 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: <6fed3b1b-1c4e-9298-19b6-7ad9c04c87dd@gmail.com> Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a new function, is_shared_symref(), which contains the heart of find_shared_symref(). Refactor find_shared_symref() to use the new function is_shared_symref(). Soon, we will use is_shared_symref() to search for symref beyond the first worktree that matches. Signed-off-by: Rubén Justo --- worktree.c | 62 +++++++++++++++++++++++++++--------------------------- worktree.h | 6 ++++++ 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/worktree.c b/worktree.c index aa43c64119..40cb9874b7 100644 --- a/worktree.c +++ b/worktree.c @@ -403,44 +403,44 @@ int is_worktree_being_bisected(const struct worktree *wt, * bisect). New commands that do similar things should update this * function as well. */ -const struct worktree *find_shared_symref(struct worktree **worktrees, - const char *symref, - const char *target) +int is_shared_symref(const struct worktree *wt, const char *symref, + const char *target) { - const struct worktree *existing = NULL; - int i = 0; + const char *symref_target; + struct ref_store *refs; + int flags; - for (i = 0; worktrees[i]; i++) { - struct worktree *wt = worktrees[i]; - const char *symref_target; - struct ref_store *refs; - int flags; + if (wt->is_bare) + return 0; - if (wt->is_bare) - continue; + if (wt->is_detached && !strcmp(symref, "HEAD")) { + if (is_worktree_being_rebased(wt, target)) + return 1; + if (is_worktree_being_bisected(wt, target)) + return 1; + } - if (wt->is_detached && !strcmp(symref, "HEAD")) { - if (is_worktree_being_rebased(wt, target)) { - existing = wt; - break; - } - if (is_worktree_being_bisected(wt, target)) { - existing = wt; - break; - } - } + refs = get_worktree_ref_store(wt); + symref_target = refs_resolve_ref_unsafe(refs, symref, 0, + NULL, &flags); + if ((flags & REF_ISSYMREF) && + symref_target && !strcmp(symref_target, target)) + return 1; - refs = get_worktree_ref_store(wt); - symref_target = refs_resolve_ref_unsafe(refs, symref, 0, - NULL, &flags); - if ((flags & REF_ISSYMREF) && - symref_target && !strcmp(symref_target, target)) { - existing = wt; - break; - } + return 0; +} + +const struct worktree *find_shared_symref(struct worktree **worktrees, + const char *symref, + const char *target) +{ + + for (int i = 0; worktrees[i]; i++) { + if (is_shared_symref(worktrees[i], symref, target)) + return worktrees[i]; } - return existing; + return NULL; } int submodule_uses_worktrees(const char *path) diff --git a/worktree.h b/worktree.h index 9dcea6fc8c..7889c4761d 100644 --- a/worktree.h +++ b/worktree.h @@ -149,6 +149,12 @@ const struct worktree *find_shared_symref(struct worktree **worktrees, const char *symref, const char *target); +/* + * Returns true if a symref points to a ref in a worktree. + */ +int is_shared_symref(const struct worktree *wt, + const char *symref, const char *target); + /* * Similar to head_ref() for all HEADs _except_ one from the current * worktree, which is covered by head_ref(). From patchwork Sat Feb 25 14:22: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: 13152219 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 9885BC7EE2F for ; Sat, 25 Feb 2023 14:22:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229685AbjBYOWI (ORCPT ); Sat, 25 Feb 2023 09:22:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229630AbjBYOWH (ORCPT ); Sat, 25 Feb 2023 09:22:07 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EEC6193DC for ; Sat, 25 Feb 2023 06:22:05 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id m25-20020a7bcb99000000b003e7842b75f2so1238129wmi.3 for ; Sat, 25 Feb 2023 06:22:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1677334924; 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=N9xPMjZ11z018dKlc5kxB17cefsu7v9Kt+D1O4UMWRE=; b=TmY8q07aVEoaVYlMpkVrYRInnJvit3Cr/Yq2LmW8Ta3QPqU1C2psHwLfDnVbM2vLv6 3c6tKYUzPCfZzcgdRCtPhFYoz2s6kN4joEl7ycjguaWxaKEAJ7D9vcDENXCi0X9nJfBp eb51i6Sh5bmnzDSoS4uV1c+2eMqx22OG0yJZn8R4dj9s+WLzw+EqqdKtddHUa5MpoiHO /XHWrmQEm/Yqgy+q/mvpvbHODf994ar8bjpNy8R1r4JOfKRR7AsrMSNeIojUmc8v9p9Q dv6fEUtLsltktDOu7OGwJFt5vQVu3gkkxjSFT/LYEnU2LEvAj87I5NLX3K7ukalx/yzs gBKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677334924; 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=N9xPMjZ11z018dKlc5kxB17cefsu7v9Kt+D1O4UMWRE=; b=1/kZpADd7Jaq4CJ6Qa/W4EqMDxQ0ZeiUtxEjZ2NSE6CcIhlqR2e9qbf8ML8JZSow6j 9WodcDDYxYmDH2necHfwnvXIE3HGg3ZLrbuw0FDYc1gh1uKr2zSAWRhFktaqUSm1Gtup MgIt2HXBY7PH4Fg0vSDD/DqCfwZVST528vwElKQtn7KcjhrMPXu1fjaDTRlvq6GICm1l FDq9VktIVDrL0cgKKNjGQ3P6jZN/tNmXh5DzLRHwlgc232Xs3hzSFspM10x3QfdBiPDM zholgi7WB2jV+prSyBBstqlkn+rbqxXZ3ZSBNlz6gI4lyFyJ8ywpgSDtNu41xxJJpmrd nGxA== X-Gm-Message-State: AO0yUKWJyWanxUhjIavtCs+Ej/l848q4MS5QgcpbsdwlJ+a1oeTT1krX uhqMlG/k97rPxwYeyjbDLj8= X-Google-Smtp-Source: AK7set/MFlSbUu1OoV958kYBTjIZv+nIatZomBC7xo4dyvJD9zwwjxmwgt1W7BTLWNVzHF2yPx9t+A== X-Received: by 2002:a05:600c:30ca:b0:3eb:38e6:f650 with SMTP id h10-20020a05600c30ca00b003eb38e6f650mr721037wmn.41.1677334923896; Sat, 25 Feb 2023 06:22:03 -0800 (PST) Received: from [192.168.2.52] (59.red-88-14-203.dynamicip.rima-tde.net. [88.14.203.59]) by smtp.gmail.com with ESMTPSA id g14-20020a7bc4ce000000b003eb20d4d4a8sm2657249wmk.44.2023.02.25.06.22.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 25 Feb 2023 06:22:03 -0800 (PST) Subject: [PATCH v4 2/4] branch: fix die_if_checked_out() when ignore_current_worktree From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Phillip Wood , Eric Sunshine References: <6fed3b1b-1c4e-9298-19b6-7ad9c04c87dd@gmail.com> Message-ID: <356b96bf-4130-6a91-4ce5-4228b071e183@gmail.com> Date: Sat, 25 Feb 2023 15:22:02 +0100 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: <6fed3b1b-1c4e-9298-19b6-7ad9c04c87dd@gmail.com> Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In 8d9fdd7 (worktree.c: check whether branch is rebased in another worktree, 2016-04-22) die_if_checked_out() learned a new option ignore_current_worktree, to modify the operation from "die() if the branch is checked out in any worktree" to "die() if the branch is checked out in any worktree other than the current one". Unfortunately we implemented it by checking the flag is_current in the worktree that find_shared_symref() returns. When the same branch is checked out in several worktrees simultaneously, find_shared_symref() will return the first matching worktree in the list composed by get_worktrees(). If one of the worktrees with the checked out branch is the current worktree, find_shared_symref() may or may not return it, depending on the order in the list. Instead of find_shared_symref(), let's do the search using use the recently introduced API is_shared_symref(), and consider ignore_current_worktree when necessary. Signed-off-by: Rubén Justo --- branch.c | 14 +++++++++----- worktree.c | 3 +-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/branch.c b/branch.c index e5614b53b3..64b7dbfd17 100644 --- a/branch.c +++ b/branch.c @@ -820,12 +820,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); diff --git a/worktree.c b/worktree.c index 40cb9874b7..34043d8fe0 100644 --- a/worktree.c +++ b/worktree.c @@ -435,10 +435,9 @@ const struct worktree *find_shared_symref(struct worktree **worktrees, const char *target) { - for (int i = 0; worktrees[i]; i++) { + for (int i = 0; worktrees[i]; i++) if (is_shared_symref(worktrees[i], symref, target)) return worktrees[i]; - } return NULL; } From patchwork Sat Feb 25 14:22:13 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: 13152220 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 700F5C64EC7 for ; Sat, 25 Feb 2023 14:22:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229630AbjBYOWU (ORCPT ); Sat, 25 Feb 2023 09:22:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbjBYOWT (ORCPT ); Sat, 25 Feb 2023 09:22:19 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46F6E193DD for ; Sat, 25 Feb 2023 06:22:17 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id p8so1925417wrt.12 for ; Sat, 25 Feb 2023 06:22:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1677334935; 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=egpw6IbPtsqdvKDFaTTWbrJWu7ZWoJQFKvDraKDVG/o=; b=n6XYZJdmkNub2KGZRHgYmCrAeG7heC+VVVMjVxhsjjVKdSr0CLtEn7R1o+Gd3tNveS e4WrbThlSvxowW6OAQ6ots5qO45pdkGWlmVDJHl7dicfM4TICPclZK+lUi8CoB27gVAH Ga6CYFfekfsF/DSsp75uys/Q9fYuIHv0Gv3EbkWJVkYWbKI8++pSkFOySMxAx23ReHm/ aSzg5MPblszHWKisWa58yrao73P5IzkxfQLlckPIktChcnqIXyuGMmBLd/vrRd5+M5Vg 4iWHjNI6SJ9R67EHiwpZ6F3VTHSoD9LXk4S8QgWVHIOEC0r5RzYW6vdzGY15rt2uITsX VPgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677334935; 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=egpw6IbPtsqdvKDFaTTWbrJWu7ZWoJQFKvDraKDVG/o=; b=pvmJjn6m/vGjzj2eiHavOY2rNWdSqtxm7DnbGUmVPYKlwqYjr34xo0gg1TS5hKDUOw EGV80FsP4OzR+e92nhbGi7jK9LIrNmuQo/+OPc2dy2HzrtGRlXH4qZB/LGPmrSo1/tny z58JETrl9Y0HTajfytuMwZXGueiQhm8RFRrZmIekdNvUlNyzDG/b/Dy6c5ikn/9qHbHY P+yRQx6kPm+VAo0TRL3pjyGpAjFIXvM0Xh/MGia4HU8MW9wl9DZXQeiWJWohvSTJ1wv8 8owCu1/oWQAG56+vDB958LTJyjPqVg6sUOPUw2IRLZxjICl2xNRN1pMNpVNAvImRguUU nkUQ== X-Gm-Message-State: AO0yUKXyapNCRfQL1pfzIEyYYv5L4B7WoKkcODxRo/glF6TAvyH9a6ep HBifo730QETWxSCB+QDohNM= X-Google-Smtp-Source: AK7set9yCEjo9+LVXDDTzh0tIMVhK5m/Fs8t+im+oCajY0wmHiHB6Y7L5Kb2Zs+RK3p0MRyj3P1xiQ== X-Received: by 2002:a05:6000:108c:b0:2c7:1d55:a56b with SMTP id y12-20020a056000108c00b002c71d55a56bmr4504418wrw.70.1677334935725; Sat, 25 Feb 2023 06:22:15 -0800 (PST) Received: from [192.168.2.52] (59.red-88-14-203.dynamicip.rima-tde.net. [88.14.203.59]) by smtp.gmail.com with ESMTPSA id a2-20020adfdd02000000b002c3f03d8851sm1936383wrm.16.2023.02.25.06.22.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 25 Feb 2023 06:22:15 -0800 (PST) Subject: [PATCH v4 3/4] rebase: refuse to switch to a branch already checked out elsewhere (test) From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Phillip Wood , Eric Sunshine References: <6fed3b1b-1c4e-9298-19b6-7ad9c04c87dd@gmail.com> Message-ID: <6ff38da4-4465-5ffb-36ec-d73a09f3c900@gmail.com> Date: Sat, 25 Feb 2023 15:22:13 +0100 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: <6fed3b1b-1c4e-9298-19b6-7ad9c04c87dd@gmail.com> Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In b5cabb4a9 (rebase: refuse to switch to branch already checked out elsewhere, 2020-02-23) we add a condition to prevent a rebase operation involving a switch to a branch that is already checked out in another worktree. A bug has recently been fixed that caused this to not work as expected. Let's add a test to notice if this changes in the future. Signed-off-by: Rubén Justo --- t/t3400-rebase.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh index d5a8ee39fc..3ce918fdb8 100755 --- a/t/t3400-rebase.sh +++ b/t/t3400-rebase.sh @@ -388,6 +388,20 @@ test_expect_success 'switch to branch checked out here' ' git rebase main main ' +test_expect_success 'switch to branch checked out elsewhere fails' ' + test_when_finished " + git worktree remove wt1 && + git worktree remove wt2 && + git branch -d shared + " && + git worktree add wt1 -b shared && + git worktree add wt2 -f shared && + # we test in both worktrees to ensure that works + # as expected with "first" and "next" worktrees + test_must_fail git -C wt1 rebase shared shared && + test_must_fail git -C wt2 rebase shared shared +' + test_expect_success 'switch to branch not checked out' ' git checkout main && git branch other && From patchwork Sat Feb 25 14:22:23 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: 13152221 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 3ED12C64EC7 for ; Sat, 25 Feb 2023 14:22:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229696AbjBYOWh (ORCPT ); Sat, 25 Feb 2023 09:22:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229693AbjBYOWg (ORCPT ); Sat, 25 Feb 2023 09:22:36 -0500 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B326D1ABE9 for ; Sat, 25 Feb 2023 06:22:26 -0800 (PST) Received: by mail-wm1-x336.google.com with SMTP id j19-20020a05600c1c1300b003e9b564fae9so4228366wms.2 for ; Sat, 25 Feb 2023 06:22:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1677334945; 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=fDotPcpW2EKBQ6i1l/eY6VaNFaS2+Vw4/ImQtg8gDnA=; b=bflD9EilmAXdr92yQsjINkd8WJa1I7ERaHHDsJv6vXkNRQbIn8+Ul8yidpgeq6fOEG yhm/TRh+lhXZZSSes98s7oHF7SQsAH8uVTKZ+lp0XAj5stbLfvG3xwUvj8nKH3IAfhLX Q0oS1fRs0+kFrS1xn4nbiHfxO2v65m6Xc9WGj3KZnkb6xToyS7kCY3CghdaoyuB4BW5A rqUDXqzSzWfStOFotBeJcZEp7CVsWziR/wVLSRyQ5Zu/yufOtLaYtqWP2uhhZtdIDCif 1HQ4HXAnnbbwGUBLJT9fXvvr4UXs6HaReMLKidSxM8RGIKAllU4DD2LBtY4yaq6mcoj+ GfVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677334945; 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=fDotPcpW2EKBQ6i1l/eY6VaNFaS2+Vw4/ImQtg8gDnA=; b=7pVITSLbZ4ooT17rytgo4H/jMVTnUd8kQH7oSH9q2qx+9YRo2JAXmfK1rEMKWx9sZ1 91+5ntIYYony9M8LnK02UmgNKRmdKaTxSdTJ7ez0IO3SGoZwF4idvLFSk9t+lEbnqlsE 5qCTHpDZLTqJLfdYZZoIeU2gXaMaVAknjhLQ26Osy5zFroArUY1LgVJryld8KbUG0f8T o9fKWnAX068zf1nKowf3xIpP/JtO65vFODwLaearaTbiKhR0v8S4qwtZO6Fad/TU3tR/ xXeKKY4/k0J35iFBPlQQdZLXoZRFWyZ+J5SoUm0JmPPYxnRz7mXszAa76htZ8OEl19XX Jrqg== X-Gm-Message-State: AO0yUKXZErBtZGu06AGWs6j4nALRowb4vic+gdn1qjnfH4w6pVGXX6kv NW0qux07qqs/AloVMCliwE8= X-Google-Smtp-Source: AK7set+zUxfiE8By7RLaRPwDMB+ZqzfUadWaJlJtloAJBKOQ8oNdFQqtmnAoJR0X4VLcGYPB78kB/A== X-Received: by 2002:a05:600c:348f:b0:3ea:f132:63d8 with SMTP id a15-20020a05600c348f00b003eaf13263d8mr5189420wmq.5.1677334945141; Sat, 25 Feb 2023 06:22:25 -0800 (PST) Received: from [192.168.2.52] (59.red-88-14-203.dynamicip.rima-tde.net. [88.14.203.59]) by smtp.gmail.com with ESMTPSA id m25-20020a7bca59000000b003db0bb81b6asm2778462wml.1.2023.02.25.06.22.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 25 Feb 2023 06:22:24 -0800 (PST) Subject: [PATCH v4 4/4] switch: reject if the branch is already checked out elsewhere (test) From: =?utf-8?q?Rub=C3=A9n_Justo?= To: Git List Cc: Junio C Hamano , Phillip Wood , Eric Sunshine References: <6fed3b1b-1c4e-9298-19b6-7ad9c04c87dd@gmail.com> Message-ID: <8f335d0e-144d-2795-06b5-004f6752b632@gmail.com> Date: Sat, 25 Feb 2023 15:22:23 +0100 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: <6fed3b1b-1c4e-9298-19b6-7ad9c04c87dd@gmail.com> Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Since 5883034 (checkout: reject if the branch is already checked out elsewhere) in normal use, we do not allow multiple worktrees having the same checked out branch. A bug has recently been fixed that caused this to not work as expected. Let's add a test to notice if this changes in the future. Signed-off-by: Rubén Justo --- t/t2060-switch.sh | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/t/t2060-switch.sh b/t/t2060-switch.sh index 5a7caf958c..e247a4735b 100755 --- a/t/t2060-switch.sh +++ b/t/t2060-switch.sh @@ -146,4 +146,33 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' test_cmp_config "" --default "" branch.main2.merge ' +test_expect_success 'switch back when temporarily detached and checked out elsewhere ' ' + test_when_finished " + git worktree remove wt1 ||: + git worktree remove wt2 ||: + git checkout - ||: + git branch -D shared ||: + " && + git checkout -b shared && + test_commit shared-first && + HASH1=$(git rev-parse --verify HEAD) && + test_commit shared-second && + test_commit shared-third && + HASH2=$(git rev-parse --verify HEAD) && + git worktree add wt1 -f shared && + git -C wt1 bisect start && + git -C wt1 bisect good $HASH1 && + git -C wt1 bisect bad $HASH2 && + git worktree add wt2 -f shared && + git -C wt2 bisect start && + git -C wt2 bisect good $HASH1 && + git -C wt2 bisect bad $HASH2 && + # we test in both worktrees to ensure that works + # as expected with "first" and "next" worktrees + test_must_fail git -C wt1 switch shared && + git -C wt1 switch --ignore-other-worktrees shared && + test_must_fail git -C wt2 switch shared && + git -C wt2 switch --ignore-other-worktrees shared +' + test_done