From patchwork Sun May 7 12:06:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Abel X-Patchwork-Id: 13233717 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 46A6FC77B7D for ; Sun, 7 May 2023 12:06:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231426AbjEGMGW (ORCPT ); Sun, 7 May 2023 08:06:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230452AbjEGMGV (ORCPT ); Sun, 7 May 2023 08:06:21 -0400 Received: from mail-4018.proton.ch (mail-4018.proton.ch [185.70.40.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E4D859E3 for ; Sun, 7 May 2023 05:06:16 -0700 (PDT) Date: Sun, 07 May 2023 12:06:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nullpo.dev; s=protonmail3; t=1683461175; x=1683720375; bh=WiaPI7sx4T4N89L5oVeZ+tQyGGHxYTJXj6TK+LE8Btw=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=MAL80UGJd/gkEqsuTAy3S4dJoTfpQz5/Cf333WXnGHZtqSKBT0NkaO3iOyqCdb7gS cKCURMba5iLoC6hJNoauy9JfX0qQr6uomwz7GZs8lO+JSCimuY0AB+fh+HdOOZc7OL eSZjcqow6aEFyd97EXBXaaUFJWiomHG8oaV5FDkHkGbQCEMY9uRUMgxwOFeJ62oBcN fj/WfON2mfdU2vQ5AAhqXlfj3AHmExNMRU68rEii56XkoTLxtqDU8fJ4kxjHSEbF1r kAZFdtdt99modwsjUagdHzoJzJ9Ynadntr18jw3iz4G/lPLKJjy/cTTh9Mjev9cJK2 0jQ6/MCcHqaTQ== To: git@vger.kernel.org From: Jacob Abel Cc: Jacob Abel , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Eric Sunshine , Junio C Hamano , Phillip Wood , =?utf-8?q?Rub=C3=A9n_Justo?= , Taylor Blau , rsbecker@nexbridge.com Subject: [PATCH v10 1/8] worktree add: include -B in usage docs Message-ID: <20230507120530.14669-2-jacobabel@nullpo.dev> In-Reply-To: <20230507120530.14669-1-jacobabel@nullpo.dev> References: <20230507120530.14669-1-jacobabel@nullpo.dev> Feedback-ID: 21506737:user:proton MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Document `-B` next to where `-b` is already documented to bring the usage docs in line with other commands such as git checkout. Signed-off-by: Jacob Abel --- Documentation/git-worktree.txt | 2 +- builtin/worktree.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) -- 2.39.3 diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 063d6eeb99..b9c12779f1 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -10,7 +10,7 @@ SYNOPSIS -------- [verse] 'git worktree add' [-f] [--detach] [--checkout] [--lock [--reason ]] - [-b ] [] + [(-b | -B) ] [] 'git worktree list' [-v | --porcelain [-z]] 'git worktree lock' [--reason ] 'git worktree move' diff --git a/builtin/worktree.c b/builtin/worktree.c index 39e9e5c9ce..d1b4b53f2c 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -22,7 +22,7 @@ #define BUILTIN_WORKTREE_ADD_USAGE \ N_("git worktree add [-f] [--detach] [--checkout] [--lock [--reason ]]\n" \ - " [-b ] []") + " [(-b | -B) ] []") #define BUILTIN_WORKTREE_LIST_USAGE \ N_("git worktree list [-v | --porcelain [-z]]") #define BUILTIN_WORKTREE_LOCK_USAGE \ From patchwork Sun May 7 12:06:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Abel X-Patchwork-Id: 13233718 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 EF6E1C77B7D for ; Sun, 7 May 2023 12:06:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231449AbjEGMGe (ORCPT ); Sun, 7 May 2023 08:06:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230450AbjEGMGd (ORCPT ); Sun, 7 May 2023 08:06:33 -0400 Received: from mail-40136.proton.ch (mail-40136.proton.ch [185.70.40.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E48D132AC for ; Sun, 7 May 2023 05:06:28 -0700 (PDT) Date: Sun, 07 May 2023 12:06:15 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nullpo.dev; s=protonmail3; t=1683461186; x=1683720386; bh=fAO2i91IOn7YgmhgQbDw22dlL/NZ9iHIA4EB/RgWTw4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=EVx8yPbChEpF+3wPjQKznziiDOLR4XsEM+gVIwrJpbrSwISrfN5S2bePb1xcXPwa3 JqgQuJ6MLQuSPotVDg20QTp55kQSJ2zW9AIek236CTXJsuJz02qI72hNI8e8ZFCbWU LLR7cvwHlyjH9dR2EzJtQYYKkr1HGyVFcnyWsqtc4bIGJIhF+M2+G5SIG3/uqEEqpy zOykWcRGg2C2j/APus7ac1rQniyIHCngTA7yBJYTT6mUHLyvKcPHLb8vap62PX9rU4 3iyxq1qLVLOFxj7kpUQpT3DHdSFl2re+hie7gIYpc3WKjdEm0p3cWlh6KoBVHIqZ8c FILknj3mDKRIw== To: git@vger.kernel.org From: Jacob Abel Cc: Jacob Abel , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Eric Sunshine , Junio C Hamano , Phillip Wood , =?utf-8?q?Rub=C3=A9n_Justo?= , Taylor Blau , rsbecker@nexbridge.com Subject: [PATCH v10 2/8] t2400: cleanup created worktree in test Message-ID: <20230507120530.14669-3-jacobabel@nullpo.dev> In-Reply-To: <20230507120530.14669-1-jacobabel@nullpo.dev> References: <20230507120530.14669-1-jacobabel@nullpo.dev> Feedback-ID: 21506737:user:proton MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Signed-off-by: Jacob Abel --- t/t2400-worktree-add.sh | 1 + 1 file changed, 1 insertion(+) -- 2.39.3 diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index d587e0b20d..a3f108347a 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -326,6 +326,7 @@ test_expect_success 'add -B' ' ' test_expect_success 'add --quiet' ' + test_when_finished "git worktree remove -f -f another-worktree" && git worktree add --quiet another-worktree main 2>actual && test_must_be_empty actual ' From patchwork Sun May 7 12:06:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Abel X-Patchwork-Id: 13233719 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 AD24DC77B7C for ; Sun, 7 May 2023 12:06:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231484AbjEGMGs (ORCPT ); Sun, 7 May 2023 08:06:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230450AbjEGMGr (ORCPT ); Sun, 7 May 2023 08:06:47 -0400 Received: from mail-4018.proton.ch (mail-4018.proton.ch [185.70.40.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2049132AC for ; Sun, 7 May 2023 05:06:42 -0700 (PDT) Date: Sun, 07 May 2023 12:06:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nullpo.dev; s=protonmail3; t=1683461201; x=1683720401; bh=4nVEPKu2DssMn0Q2fVQT3c0kVmSWBAPoYcTqC5AH4YI=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=jdCZw2l4W5w1lICpFBD6aXhJMbq8qkKlOccAPFTEiCjf3fSYFjxoamYsFn19kvplE PHlarrQl99q//FrU6IMpfmjt7PpbJPjQtO71eB5MX8Goglsv73Ry5wuBE5SJqQOQxj zYxZLgd2Uno7fU4Dczr4hnVncKgeqUXeibyAIX2Idk4ifEJPINpNsglZtWblG2KIJs gULeBglax9egi0vAnw/IJzNEp7w2z0HWOXARQSAT14vbZX8Cd2dFkV5c+yoaMjO4tb E9bDXouewX2dr88ip6i3Cb+ueNULoVQxrrOGjcwzA5T5Cds7oTnmE72iaSTF9QD+Ks oDLS1V0nyvFGw== To: git@vger.kernel.org From: Jacob Abel Cc: Jacob Abel , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Eric Sunshine , Junio C Hamano , Phillip Wood , =?utf-8?q?Rub=C3=A9n_Justo?= , Taylor Blau , rsbecker@nexbridge.com Subject: [PATCH v10 3/8] t2400: refactor "worktree add" opt exclusion tests Message-ID: <20230507120530.14669-4-jacobabel@nullpo.dev> Feedback-ID: 21506737:user:proton MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Pull duplicate test code into a function so that additional opt combinations can be tested succinctly. Signed-off-by: Jacob Abel --- t/t2400-worktree-add.sh | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) -- 2.39.3 diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index a3f108347a..0ca3ec2022 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -298,17 +298,20 @@ test_expect_success '"add" no auto-vivify with --detach and omitted' ' test_must_fail git -C mish/mash symbolic-ref HEAD ' -test_expect_success '"add" -b/-B mutually exclusive' ' - test_must_fail git worktree add -b poodle -B poodle bamboo main -' - -test_expect_success '"add" -b/--detach mutually exclusive' ' - test_must_fail git worktree add -b poodle --detach bamboo main -' +# Helper function to test mutually exclusive options. +# +# Note: Quoted arguments containing spaces are not supported. +test_wt_add_excl () { + local opts="$*" && + test_expect_success "'worktree add' with '$opts' has mutually exclusive options" ' + test_must_fail git worktree add $opts 2>actual && + grep -E "fatal:( options)? .* cannot be used together" actual + ' +} -test_expect_success '"add" -B/--detach mutually exclusive' ' - test_must_fail git worktree add -B poodle --detach bamboo main -' +test_wt_add_excl -b poodle -B poodle bamboo main +test_wt_add_excl -b poodle --detach bamboo main +test_wt_add_excl -B poodle --detach bamboo main test_expect_success '"add -B" fails if the branch is checked out' ' git rev-parse newmain >before && From patchwork Sun May 7 12:06:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Abel X-Patchwork-Id: 13233720 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 65E21C7EE22 for ; Sun, 7 May 2023 12:06:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231489AbjEGMGu (ORCPT ); Sun, 7 May 2023 08:06:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231460AbjEGMGr (ORCPT ); Sun, 7 May 2023 08:06:47 -0400 Received: from mail-4323.proton.ch (mail-4323.proton.ch [185.70.43.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38DF313869 for ; Sun, 7 May 2023 05:06:43 -0700 (PDT) Date: Sun, 07 May 2023 12:06:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nullpo.dev; s=protonmail3; t=1683461201; x=1683720401; bh=3eAt6/lS1Yv/9utN376uB+/TUDJSDDERK30ilLJh+Ms=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=GVuXvuE2jYBSfU7ARzd6baN8ZKVyhzsVyARwDruT2BKIkx0WDTSJ4ikQAHf3PCTJT Pt0OrShjFVTaVA96V5WgJ/VsIcu8FXXveh1juLFQz4Tb+wYbZV3faKebUbtmraRKiu dk/ed4tFbhB+5iF+4DdS9WH6WB4iJngBekBvcNz665UTIHmygJrbzr5VpGNA6sq3TZ VSsYS6KOi1M+yFzN+JiVkkWSCmhpQ3hrUXAvucoi8970zD+U8Q087h4vc3rXDwNU07 U/TznKcDS8IQegt5IKwL1qiDVUza5QMucZUtK1Kh2yb2zE6nJluF2Yfvue2eqhXttL 5Lm+HM0TpnJ1w== To: git@vger.kernel.org From: Jacob Abel Cc: Jacob Abel , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Eric Sunshine , Junio C Hamano , Phillip Wood , =?utf-8?q?Rub=C3=A9n_Justo?= , Taylor Blau , rsbecker@nexbridge.com Subject: [PATCH v10 4/8] t2400: add tests to verify --quiet Message-ID: <20230507120530.14669-5-jacobabel@nullpo.dev> Feedback-ID: 21506737:user:proton MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add tests to verify that the command performs operations the same with `--quiet` as without it. Additionally verifies that all non-fatal output is suppressed. Signed-off-by: Jacob Abel --- t/t2400-worktree-add.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) -- 2.39.3 diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index 0ca3ec2022..841f15f59e 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -334,6 +334,13 @@ test_expect_success 'add --quiet' ' test_must_be_empty actual ' +test_expect_success 'add --quiet -b' ' + test_when_finished "git branch -D quietnewbranch" && + test_when_finished "git worktree remove -f -f another-worktree" && + git worktree add --quiet -b quietnewbranch another-worktree 2>actual && + test_must_be_empty actual +' + test_expect_success 'local clone from linked checkout' ' git clone --local here here-clone && ( cd here-clone && git fsck ) @@ -532,6 +539,35 @@ test_expect_success 'git worktree add --guess-remote sets up tracking' ' test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo ) ' +test_expect_success 'git worktree add --guess-remote sets up tracking (quiet)' ' + test_when_finished rm -rf repo_a repo_b foo && + setup_remote_repo repo_a repo_b && + ( + cd repo_b && + git worktree add --quiet --guess-remote ../foo 2>actual && + test_must_be_empty actual + ) && + ( + cd foo && + test_branch_upstream foo repo_a foo && + test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo + ) +' + +test_expect_success 'git worktree --no-guess-remote (quiet)' ' + test_when_finished rm -rf repo_a repo_b foo && + setup_remote_repo repo_a repo_b && + ( + cd repo_b && + git worktree add --quiet --no-guess-remote ../foo + ) && + ( + cd foo && + test_must_fail git config "branch.foo.remote" && + test_must_fail git config "branch.foo.merge" && + test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo + ) +' test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' ' test_when_finished rm -rf repo_a repo_b foo && From patchwork Sun May 7 12:06:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacob Abel X-Patchwork-Id: 13233721 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 95138C77B7D for ; Sun, 7 May 2023 12:07:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231501AbjEGMHG (ORCPT ); Sun, 7 May 2023 08:07:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230283AbjEGMHD (ORCPT ); Sun, 7 May 2023 08:07:03 -0400 Received: from mail-4022.proton.ch (mail-4022.proton.ch [185.70.40.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 750A04EC9 for ; Sun, 7 May 2023 05:06:58 -0700 (PDT) Date: Sun, 07 May 2023 12:06:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nullpo.dev; s=protonmail3; t=1683461216; x=1683720416; bh=O+Fl4bjZvnbGHrlijbEhzADoQ3wuGL1fXlnCfWpoOQ4=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=vfW8gRST5a7WiiMm67rzUDcEwvePiolBSslc4nK/IAgqrHhxU39W8FKJq3B0TsLDI ElgV2fkdXCSI4GZD1uXKHjL3TyGHUepecR0VWQ6dF6ocru8FmN6UQSiWityKRPd5aK gVERVhlSqA9m5lq7f/jNSf6JbEb83w7QyzPoXq/Hmdg7IdkOFbbKl2ezVQCUhUnT07 IpiD+Cfw98vq7bdwAlek5tiuSaYYvv+tbxyAAN3oqdjTViqF5QVGLRFMn6nkQLzVqq 7yADqxoS6PDP0gxU1C6n3Q6FkMgmlqwopvIGMt0SR2bIw7ZaQS3aXKl+xn5lyaoNJv H3DHgtbzDSa+A== To: git@vger.kernel.org From: Jacob Abel Cc: Jacob Abel , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Eric Sunshine , Junio C Hamano , Phillip Wood , =?utf-8?q?Rub=C3=A9n_Justo?= , Taylor Blau , rsbecker@nexbridge.com Subject: [PATCH v10 5/8] worktree add: add --orphan flag Message-ID: <20230507120530.14669-6-jacobabel@nullpo.dev> Feedback-ID: 21506737:user:proton MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add support for creating an orphan branch when adding a new worktree. The functionality of this flag is equivalent to git switch's --orphan option. Current Behavior: % git -C foo.git --no-pager branch -l + main % git -C foo.git worktree add main/ Preparing worktree (new branch 'main') HEAD is now at 6c93a75 a commit % % git init bar.git Initialized empty Git repository in /path/to/bar.git/ % git -C bar.git --no-pager branch -l % git -C bar.git worktree add main/ Preparing worktree (new branch 'main') fatal: not a valid object name: 'HEAD' % New Behavior: % git -C foo.git --no-pager branch -l + main % git -C foo.git worktree add main/ Preparing worktree (new branch 'main') HEAD is now at 6c93a75 a commit % % git init --bare bar.git Initialized empty Git repository in /path/to/bar.git/ % git -C bar.git --no-pager branch -l % git -C bar.git worktree add main/ Preparing worktree (new branch 'main') fatal: invalid reference: HEAD % git -C bar.git worktree add --orphan -b main/ Preparing worktree (new branch 'main') % git -C bar.git worktree add --orphan -b newbranch worktreedir/ Preparing worktree (new branch 'newbranch') % Signed-off-by: Ævar Arnfjörð Bjarmason Signed-off-by: Jacob Abel --- Documentation/git-worktree.txt | 6 ++- builtin/worktree.c | 67 +++++++++++++++++++++++++++------ t/t2400-worktree-add.sh | 68 ++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 12 deletions(-) -- 2.39.3 diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index b9c12779f1..485d865eb2 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -10,7 +10,7 @@ SYNOPSIS -------- [verse] 'git worktree add' [-f] [--detach] [--checkout] [--lock [--reason ]] - [(-b | -B) ] [] + [--orphan] [(-b | -B) ] [] 'git worktree list' [-v | --porcelain [-z]] 'git worktree lock' [--reason ] 'git worktree move' @@ -222,6 +222,10 @@ This can also be set up as the default behaviour by using the With `prune`, do not remove anything; just report what it would remove. +--orphan:: + With `add`, make the new worktree and index empty, associating + the worktree with a new orphan/unborn branch named ``. + --porcelain:: With `list`, output in an easy-to-parse format for scripts. This format will remain stable across Git versions and regardless of user diff --git a/builtin/worktree.c b/builtin/worktree.c index d1b4b53f2c..48de7fc3b0 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -22,7 +22,8 @@ #define BUILTIN_WORKTREE_ADD_USAGE \ N_("git worktree add [-f] [--detach] [--checkout] [--lock [--reason ]]\n" \ - " [(-b | -B) ] []") + " [--orphan] [(-b | -B) ] []") + #define BUILTIN_WORKTREE_LIST_USAGE \ N_("git worktree list [-v | --porcelain [-z]]") #define BUILTIN_WORKTREE_LOCK_USAGE \ @@ -95,6 +96,7 @@ struct add_opts { int detach; int quiet; int checkout; + int orphan; const char *keep_locked; }; @@ -368,6 +370,22 @@ static int checkout_worktree(const struct add_opts *opts, return run_command(&cp); } +static int make_worktree_orphan(const char * ref, const struct add_opts *opts, + struct strvec *child_env) +{ + struct strbuf symref = STRBUF_INIT; + struct child_process cp = CHILD_PROCESS_INIT; + + validate_new_branchname(ref, &symref, 0); + strvec_pushl(&cp.args, "symbolic-ref", "HEAD", symref.buf, NULL); + if (opts->quiet) + strvec_push(&cp.args, "--quiet"); + strvec_pushv(&cp.env, child_env->v); + strbuf_release(&symref); + cp.git_cmd = 1; + return run_command(&cp); +} + static int add_worktree(const char *path, const char *refname, const struct add_opts *opts) { @@ -397,7 +415,7 @@ static int add_worktree(const char *path, const char *refname, die_if_checked_out(symref.buf, 0); } commit = lookup_commit_reference_by_name(refname); - if (!commit) + if (!commit && !opts->orphan) die(_("invalid reference: %s"), refname); name = worktree_basename(path, &len); @@ -486,10 +504,10 @@ static int add_worktree(const char *path, const char *refname, strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path); cp.git_cmd = 1; - if (!is_branch) + if (!is_branch && commit) { strvec_pushl(&cp.args, "update-ref", "HEAD", oid_to_hex(&commit->object.oid), NULL); - else { + } else { strvec_pushl(&cp.args, "symbolic-ref", "HEAD", symref.buf, NULL); if (opts->quiet) @@ -501,6 +519,10 @@ static int add_worktree(const char *path, const char *refname, if (ret) goto done; + if (opts->orphan && + (ret = make_worktree_orphan(refname, opts, &child_env))) + goto done; + if (opts->checkout && (ret = checkout_worktree(opts, &child_env))) goto done; @@ -520,7 +542,7 @@ static int add_worktree(const char *path, const char *refname, * Hook failure does not warrant worktree deletion, so run hook after * is_junk is cleared, but do return appropriate code when hook fails. */ - if (!ret && opts->checkout) { + if (!ret && opts->checkout && !opts->orphan) { struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; strvec_pushl(&opt.env, "GIT_DIR", "GIT_WORK_TREE", NULL); @@ -568,7 +590,7 @@ static void print_preparing_worktree_line(int detach, else { struct commit *commit = lookup_commit_reference_by_name(branch); if (!commit) - die(_("invalid reference: %s"), branch); + BUG(_("unreachable: invalid reference: %s"), branch); fprintf_ln(stderr, _("Preparing worktree (detached HEAD %s)"), repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV)); } @@ -620,6 +642,7 @@ static int add(int ac, const char **av, const char *prefix) N_("create a new branch")), OPT_STRING('B', NULL, &new_branch_force, N_("branch"), N_("create or reset a branch")), + OPT_BOOL(0, "orphan", &opts.orphan, N_("create unborn/orphaned branch")), OPT_BOOL('d', "detach", &opts.detach, N_("detach HEAD at named commit")), OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")), OPT_BOOL(0, "lock", &keep_locked, N_("keep the new working tree locked")), @@ -640,6 +663,17 @@ static int add(int ac, const char **av, const char *prefix) ac = parse_options(ac, av, prefix, options, git_worktree_add_usage, 0); if (!!opts.detach + !!new_branch + !!new_branch_force > 1) die(_("options '%s', '%s', and '%s' cannot be used together"), "-b", "-B", "--detach"); + if (opts.detach && opts.orphan) + die(_("options '%s', and '%s' cannot be used together"), + "--orphan", "--detach"); + if (opts.orphan && opt_track) + die(_("'%s' and '%s' cannot be used together"), "--orphan", "--track"); + if (opts.orphan && !opts.checkout) + die(_("'%s' and '%s' cannot be used together"), "--orphan", + "--no-checkout"); + if (opts.orphan && ac == 2) + die(_("'%s' and '%s' cannot be used together"), "--orphan", + _("")); if (lock_reason && !keep_locked) die(_("the option '%s' requires '%s'"), "--reason", "--lock"); if (lock_reason) @@ -668,13 +702,17 @@ static int add(int ac, const char **av, const char *prefix) strbuf_release(&symref); } - if (ac < 2 && !new_branch && !opts.detach) { + if (opts.orphan && !new_branch) { + int n; + const char *s = worktree_basename(path, &n); + new_branch = xstrndup(s, n); + } else if (new_branch || opts.detach || opts.orphan) { + // No-op + } else if (ac < 2) { const char *s = dwim_branch(path, &new_branch); if (s) branch = s; - } - - if (ac == 2 && !new_branch && !opts.detach) { + } else if (ac == 2) { struct object_id oid; struct commit *commit; const char *remote; @@ -688,10 +726,17 @@ static int add(int ac, const char **av, const char *prefix) } } } + + if (!opts.orphan && !lookup_commit_reference_by_name(branch)) { + die(_("invalid reference: %s"), branch); + } + if (!opts.quiet) print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force); - if (new_branch) { + if (opts.orphan) { + branch = new_branch; + } else if (new_branch) { struct child_process cp = CHILD_PROCESS_INIT; cp.git_cmd = 1; strvec_push(&cp.args, "branch"); diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index 841f15f59e..fba90582b6 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -312,6 +312,10 @@ test_wt_add_excl () { test_wt_add_excl -b poodle -B poodle bamboo main test_wt_add_excl -b poodle --detach bamboo main test_wt_add_excl -B poodle --detach bamboo main +test_wt_add_excl --orphan --detach bamboo +test_wt_add_excl --orphan --no-checkout bamboo +test_wt_add_excl --orphan bamboo main +test_wt_add_excl --orphan -b bamboo wtdir/ main test_expect_success '"add -B" fails if the branch is checked out' ' git rev-parse newmain >before && @@ -341,6 +345,62 @@ test_expect_success 'add --quiet -b' ' test_must_be_empty actual ' +test_expect_success '"add --orphan"' ' + test_when_finished "git worktree remove -f -f orphandir" && + git worktree add --orphan -b neworphan orphandir && + echo refs/heads/neworphan >expected && + git -C orphandir symbolic-ref HEAD >actual && + test_cmp expected actual +' + +test_expect_success '"add --orphan (no -b)"' ' + test_when_finished "git worktree remove -f -f neworphan" && + git worktree add --orphan neworphan && + echo refs/heads/neworphan >expected && + git -C neworphan symbolic-ref HEAD >actual && + test_cmp expected actual +' + +test_expect_success '"add --orphan --quiet"' ' + test_when_finished "git worktree remove -f -f orphandir" && + git worktree add --quiet --orphan -b neworphan orphandir 2>log.actual && + test_must_be_empty log.actual && + echo refs/heads/neworphan >expected && + git -C orphandir symbolic-ref HEAD >actual && + test_cmp expected actual +' + +test_expect_success '"add --orphan" fails if the branch already exists' ' + test_when_finished "git branch -D existingbranch" && + git worktree add -b existingbranch orphandir main && + git worktree remove orphandir && + test_must_fail git worktree add --orphan -b existingbranch orphandir +' + +test_expect_success '"add --orphan" with empty repository' ' + test_when_finished "rm -rf empty_repo" && + echo refs/heads/newbranch >expected && + GIT_DIR="empty_repo" git init --bare && + git -C empty_repo worktree add --orphan -b newbranch worktreedir && + git -C empty_repo/worktreedir symbolic-ref HEAD >actual && + test_cmp expected actual +' + +test_expect_success '"add" worktree with orphan branch and lock' ' + git worktree add --lock --orphan -b orphanbr orphan-with-lock && + test_when_finished "git worktree unlock orphan-with-lock || :" && + test -f .git/worktrees/orphan-with-lock/locked +' + +test_expect_success '"add" worktree with orphan branch, lock, and reason' ' + lock_reason="why not" && + git worktree add --detach --lock --reason "$lock_reason" orphan-with-lock-reason main && + test_when_finished "git worktree unlock orphan-with-lock-reason || :" && + test -f .git/worktrees/orphan-with-lock-reason/locked && + echo "$lock_reason" >expect && + test_cmp expect .git/worktrees/orphan-with-lock-reason/locked +' + test_expect_success 'local clone from linked checkout' ' git clone --local here here-clone && ( cd here-clone && git fsck ) @@ -457,6 +517,14 @@ setup_remote_repo () { ) } +test_expect_success '"add" w/ no HEAD' ' + test_when_finished rm -rf repo_upstream repo_local foo && + setup_remote_repo repo_upstream repo_local && + git -C repo_local config --bool core.bare true && + git -C repo_local branch -D main && + git -C repo_local worktree add ./foo repo_upstream/foo +' + test_expect_success '--no-track avoids setting up tracking' ' test_when_finished rm -rf repo_upstream repo_local foo && setup_remote_repo repo_upstream repo_local && From patchwork Sun May 7 12:06:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Abel X-Patchwork-Id: 13233722 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 DCFBDC77B7D for ; Sun, 7 May 2023 12:07:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231517AbjEGMHU (ORCPT ); Sun, 7 May 2023 08:07:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230225AbjEGMHS (ORCPT ); Sun, 7 May 2023 08:07:18 -0400 Received: from mail-4018.proton.ch (mail-4018.proton.ch [185.70.40.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CF7311B6A for ; Sun, 7 May 2023 05:07:13 -0700 (PDT) Date: Sun, 07 May 2023 12:06:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nullpo.dev; s=protonmail3; t=1683461231; x=1683720431; bh=GBD9KRYfDWCz1DNOb3JfzyQ5/42VYlL6lgVC7m5TmLw=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=mhPPB2QXLpbYAFXBaeP92v/oJ7IwHhO4C5jS/EHqx4r2l8W0Szv2kAyQ/trKR6Wkc 17zPxi825fhLKZZLLDX2s+/MDU9gEtt5vtZIWKUq+7FZo1G7w2y7ZpnWvUeOQ4tCHO n+K/ms6FA9PuNQMESkeXoYsG5Xoo1ByTjMuY+SEXAaEyefR4TJgCWWjAJhIumod2Yd /0H2GSrZyjtqioh9jCuQXjlESzbqk97yXEiGPYJzH9IJinW531/eCsn9uDCdl6MAsu +pIUObG617bL82TpTaoVnlF72Lgxsb+350tWxV6Ca4/vU9H4zJO3G1jGn5WPH0cbVp /jIhlgDjqBMwQ== To: git@vger.kernel.org From: Jacob Abel Cc: Jacob Abel , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Eric Sunshine , Junio C Hamano , Phillip Wood , =?utf-8?q?Rub=C3=A9n_Justo?= , Taylor Blau , rsbecker@nexbridge.com Subject: [PATCH v10 6/8] worktree add: introduce "try --orphan" hint Message-ID: <20230507120530.14669-7-jacobabel@nullpo.dev> Feedback-ID: 21506737:user:proton MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a new advice/hint in `git worktree add` for when the user tries to create a new worktree from a reference that doesn't exist. Current Behavior: % git init foo Initialized empty Git repository in /path/to/foo/ % touch file % git -C foo commit -q -a -m "test commit" % git -C foo switch --orphan norefbranch % git -C foo worktree add newbranch/ Preparing worktree (new branch 'newbranch') fatal: invalid reference: HEAD % New Behavior: % git init --bare foo Initialized empty Git repository in /path/to/foo/ % touch file % git -C foo commit -q -a -m "test commit" % git -C foo switch --orphan norefbranch % git -C foo worktree add newbranch/ Preparing worktree (new branch 'newbranch') hint: If you meant to create a worktree containing a new orphan branch hint: (branch with no commits) for this repository, you can do so hint: using the --orphan option: hint: hint: git worktree add --orphan newbranch/ hint: hint: Disable this message with "git config advice.worktreeAddOrphan false" fatal: invalid reference: HEAD % git -C foo worktree add -b newbranch2 new_wt/ Preparing worktree (new branch 'newbranch') hint: If you meant to create a worktree containing a new orphan branch hint: (branch with no commits) for this repository, you can do so hint: using the --orphan option: hint: hint: git worktree add --orphan -b newbranch2 new_wt/ hint: hint: Disable this message with "git config advice.worktreeAddOrphan false" fatal: invalid reference: HEAD % Signed-off-by: Jacob Abel --- Documentation/config/advice.txt | 4 ++++ advice.c | 1 + advice.h | 1 + builtin/worktree.c | 26 +++++++++++++++++++++++ t/t2400-worktree-add.sh | 37 +++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+) -- 2.39.3 diff --git a/Documentation/config/advice.txt b/Documentation/config/advice.txt index c96b5b2e5d..c548a91e67 100644 --- a/Documentation/config/advice.txt +++ b/Documentation/config/advice.txt @@ -138,4 +138,8 @@ advice.*:: checkout. diverging:: Advice shown when a fast-forward is not possible. + worktreeAddOrphan:: + Advice shown when a user tries to create a worktree from an + invalid reference, to instruct how to create a new orphan + branch instead. -- diff --git a/advice.c b/advice.c index d6232439c3..e5a9bb9b44 100644 --- a/advice.c +++ b/advice.c @@ -78,6 +78,7 @@ static struct { [ADVICE_SUBMODULES_NOT_UPDATED] = { "submodulesNotUpdated", 1 }, [ADVICE_UPDATE_SPARSE_PATH] = { "updateSparsePath", 1 }, [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 }, + [ADVICE_WORKTREE_ADD_ORPHAN] = { "worktreeAddOrphan", 1 }, }; static const char turn_off_instructions[] = diff --git a/advice.h b/advice.h index 0f584163f5..2affbe1426 100644 --- a/advice.h +++ b/advice.h @@ -49,6 +49,7 @@ struct string_list; ADVICE_UPDATE_SPARSE_PATH, ADVICE_WAITING_FOR_EDITOR, ADVICE_SKIPPED_CHERRY_PICKS, + ADVICE_WORKTREE_ADD_ORPHAN, }; int git_default_advice_config(const char *var, const char *value); diff --git a/builtin/worktree.c b/builtin/worktree.c index 48de7fc3b0..15bdb380c7 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -1,5 +1,6 @@ #include "cache.h" #include "abspath.h" +#include "advice.h" #include "checkout.h" #include "config.h" #include "builtin.h" @@ -39,6 +40,20 @@ #define BUILTIN_WORKTREE_UNLOCK_USAGE \ N_("git worktree unlock ") +#define WORKTREE_ADD_ORPHAN_WITH_DASH_B_HINT_TEXT \ + _("If you meant to create a worktree containing a new orphan branch\n" \ + "(branch with no commits) for this repository, you can do so\n" \ + "using the --orphan flag:\n" \ + "\n" \ + " git worktree add --orphan -b %s %s\n") + +#define WORKTREE_ADD_ORPHAN_NO_DASH_B_HINT_TEXT \ + _("If you meant to create a worktree containing a new orphan branch\n" \ + "(branch with no commits) for this repository, you can do so\n" \ + "using the --orphan flag:\n" \ + "\n" \ + " git worktree add --orphan %s\n") + static const char * const git_worktree_usage[] = { BUILTIN_WORKTREE_ADD_USAGE, BUILTIN_WORKTREE_LIST_USAGE, @@ -634,6 +649,7 @@ static int add(int ac, const char **av, const char *prefix) const char *opt_track = NULL; const char *lock_reason = NULL; int keep_locked = 0; + int used_new_branch_options; struct option options[] = { OPT__FORCE(&opts.force, N_("checkout even if already checked out in other worktree"), @@ -686,6 +702,7 @@ static int add(int ac, const char **av, const char *prefix) path = prefix_filename(prefix, av[0]); branch = ac < 2 ? "HEAD" : av[1]; + used_new_branch_options = new_branch || new_branch_force; if (!strcmp(branch, "-")) branch = "@{-1}"; @@ -728,6 +745,15 @@ static int add(int ac, const char **av, const char *prefix) } if (!opts.orphan && !lookup_commit_reference_by_name(branch)) { + int attempt_hint = !opts.quiet && (ac < 2); + if (attempt_hint && used_new_branch_options) { + advise_if_enabled(ADVICE_WORKTREE_ADD_ORPHAN, + WORKTREE_ADD_ORPHAN_WITH_DASH_B_HINT_TEXT, + new_branch, path); + } else if (attempt_hint) { + advise_if_enabled(ADVICE_WORKTREE_ADD_ORPHAN, + WORKTREE_ADD_ORPHAN_NO_DASH_B_HINT_TEXT, path); + } die(_("invalid reference: %s"), branch); } diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index fba90582b6..46eef26179 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -401,6 +401,43 @@ test_expect_success '"add" worktree with orphan branch, lock, and reason' ' test_cmp expect .git/worktrees/orphan-with-lock-reason/locked ' +# Note: Quoted arguments containing spaces are not supported. +test_wt_add_orphan_hint () { + local context="$1" && + local use_branch=$2 && + shift 2 && + local opts="$*" && + test_expect_success "'worktree add' show orphan hint in bad/orphan HEAD w/ $context" ' + test_when_finished "rm -rf repo" && + git init repo && + (cd repo && test_commit commit) && + git -C repo switch --orphan noref && + test_must_fail git -C repo worktree add $opts foobar/ 2>actual && + ! grep "error: unknown switch" actual && + grep "hint: If you meant to create a worktree containing a new orphan branch" actual && + if [ $use_branch -eq 1 ] + then + grep -E "^hint:\s+git worktree add --orphan -b \S+ \S+\s*$" actual + else + grep -E "^hint:\s+git worktree add --orphan \S+\s*$" actual + fi + + ' +} + +test_wt_add_orphan_hint 'no opts' 0 +test_wt_add_orphan_hint '-b' 1 -b foobar_branch +test_wt_add_orphan_hint '-B' 1 -B foobar_branch + +test_expect_success "'worktree add' doesn't show orphan hint in bad/orphan HEAD w/ --quiet" ' + test_when_finished "rm -rf repo" && + git init repo && + (cd repo && test_commit commit) && + test_must_fail git -C repo worktree add --quiet foobar_branch foobar/ 2>actual && + ! grep "error: unknown switch" actual && + ! grep "hint: If you meant to create a worktree containing a new orphan branch" actual +' + test_expect_success 'local clone from linked checkout' ' git clone --local here here-clone && ( cd here-clone && git fsck ) From patchwork Sun May 7 12:07:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Abel X-Patchwork-Id: 13233723 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 86164C77B7D for ; Sun, 7 May 2023 12:07:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231549AbjEGMH1 (ORCPT ); Sun, 7 May 2023 08:07:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231528AbjEGMHW (ORCPT ); Sun, 7 May 2023 08:07:22 -0400 Received: from mail-4323.proton.ch (mail-4323.proton.ch [185.70.43.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 416FF49DC for ; Sun, 7 May 2023 05:07:17 -0700 (PDT) Date: Sun, 07 May 2023 12:07:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nullpo.dev; s=protonmail3; t=1683461235; x=1683720435; bh=JOV0KQN/oEZ7yyR34Mzqd510viJckaDJlFFy7Pnhg/A=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=Uqi2Rc/NOo/3SA3uWfAVSB+cPCjXgKJL2YUd0W/CKGK6V1y6gdk1hZJx28LqhPs7e 2ei8T/NCv30TqAoB3TFEelgPg6Sdl/ZHDFTxo6PBy5JzHiugxLTZZfPNV2Pe6gBXQD qmzownDI8KRkMW9FRaYVCVMqFHUxKGecIO4vm8X1yDGGlBMR4tfIYKjl5UWalkS0GA OghE/+rDwvX3EJIAL4lDmDd5kYpjeWV5usjqOQFbsvjoGc7PQrfNVr0Dw1hTSJmwZF aLSoWalCWrLSKiCQ2+gUEzoJk4qKVEfMZ/pPEJ/BV6C1Lw9c25JLkBwi/tojBwgnAZ ZwktEEuxLfpBA== To: git@vger.kernel.org From: Jacob Abel Cc: Jacob Abel , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Eric Sunshine , Junio C Hamano , Phillip Wood , =?utf-8?q?Rub=C3=A9n_Justo?= , Taylor Blau , rsbecker@nexbridge.com Subject: [PATCH v10 7/8] worktree add: extend DWIM to infer --orphan Message-ID: <20230507120530.14669-8-jacobabel@nullpo.dev> Feedback-ID: 21506737:user:proton MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Extend DWIM to try to infer `--orphan` when in an empty repository. i.e. a repository with an invalid/unborn HEAD, no local branches, and if `--guess-remote` is used then no remote branches. This behavior is equivalent to `git switch -c` or `git checkout -b` in an empty repository. Also warn the user (overriden with `-f`/`--force`) when they likely intend to checkout a remote branch to the worktree but have not yet fetched from the remote. i.e. when using `--guess-remote` and there is a remote but no local or remote refs. Current Behavior: % git --no-pager branch --list --remotes % git remote origin % git workree add ../main hint: If you meant to create a worktree containing a new orphan branch [...] hint: Disable this message with "git config advice.worktreeAddOrphan false" fatal: invalid reference: HEAD % git workree add --guess-remote ../main hint: If you meant to create a worktree containing a new orphan branch [...] hint: Disable this message with "git config advice.worktreeAddOrphan false" fatal: invalid reference: HEAD % git fetch --quiet % git --no-pager branch --list --remotes origin/HEAD -> origin/main origin/main % git workree add --guess-remote ../main Preparing worktree (new branch 'main') branch 'main' set up to track 'origin/main'. HEAD is now at dadc8e6dac commit message % New Behavior: % git --no-pager branch --list --remotes % git remote origin % git workree add ../main No possible source branch, inferring '--orphan' Preparing worktree (new branch 'main') % git worktree remove ../main % git workree add --guess-remote ../main fatal: No local or remote refs exist despite at least one remote present, stopping; use 'add -f' to overide or fetch a remote first % git workree add --guess-remote -f ../main No possible source branch, inferring '--orphan' Preparing worktree (new branch 'main') % git worktree remove ../main % git fetch --quiet % git --no-pager branch --list --remotes origin/HEAD -> origin/main origin/main % git workree add --guess-remote ../main Preparing worktree (new branch 'main') branch 'main' set up to track 'origin/main'. HEAD is now at dadc8e6dac commit message % Signed-off-by: Jacob Abel --- Documentation/git-worktree.txt | 10 + builtin/worktree.c | 114 +++++++++++- t/t2400-worktree-add.sh | 326 +++++++++++++++++++++++++++++++++ 3 files changed, 449 insertions(+), 1 deletion(-) -- 2.39.3 diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 485d865eb2..a4fbf5e838 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -95,6 +95,16 @@ exist, a new branch based on `HEAD` is automatically created as if `-b ` was given. If `` does exist, it will be checked out in the new worktree, if it's not checked out anywhere else, otherwise the command will refuse to create the worktree (unless `--force` is used). ++ +If `` is omitted, neither `--detach`, or `--orphan` is +used, and there are no valid local branches (or remote branches if +`--guess-remote` is specified) then, as a convenience, the new worktree is +associated with a new orphan branch named `` (after +`$(basename )` if neither `-b` or `-B` is used) as if `--orphan` was +passed to the command. In the event the repository has a remote and +`--guess-remote` is used, but no remote or local branches exist, then the +command fails with a warning reminding the user to fetch from their remote +first (or override by using `-f/--force`). list:: diff --git a/builtin/worktree.c b/builtin/worktree.c index 15bdb380c7..093b2cb032 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -12,6 +12,7 @@ #include "strvec.h" #include "branch.h" #include "refs.h" +#include "remote.h" #include "run-command.h" #include "hook.h" #include "sigchain.h" @@ -40,6 +41,9 @@ #define BUILTIN_WORKTREE_UNLOCK_USAGE \ N_("git worktree unlock ") +#define WORKTREE_ADD_DWIM_ORPHAN_INFER_TEXT \ + _("No possible source branch, inferring '--orphan'") + #define WORKTREE_ADD_ORPHAN_WITH_DASH_B_HINT_TEXT \ _("If you meant to create a worktree containing a new orphan branch\n" \ "(branch with no commits) for this repository, you can do so\n" \ @@ -613,6 +617,107 @@ static void print_preparing_worktree_line(int detach, } } +/** + * Callback to short circuit iteration over refs on the first reference + * corresponding to a valid oid. + * + * Returns 0 on failure and non-zero on success. + */ +static int first_valid_ref(const char *refname, + const struct object_id *oid, + int flags, + void *cb_data) +{ + return 1; +} + +/** + * Verifies HEAD and determines whether there exist any valid local references. + * + * - Checks whether HEAD points to a valid reference. + * + * - Checks whether any valid local branches exist. + * + * Returns 1 if any of the previous checks are true, otherwise returns 0. + */ +static int can_use_local_refs(const struct add_opts *opts) +{ + if (head_ref(first_valid_ref, NULL)) { + return 1; + } else if (for_each_branch_ref(first_valid_ref, NULL)) { + return 1; + } + return 0; +} + +/** + * Reports whether the necessary flags were set and whether the repository has + * remote references to attempt DWIM tracking of upstream branches. + * + * 1. Checks that `--guess-remote` was used or `worktree.guessRemote = true`. + * + * 2. Checks whether any valid remote branches exist. + * + * 3. Checks that there exists at least one remote and emits a warning/error + * if both checks 1. and 2. are false (can be bypassed with `--force`). + * + * Returns 1 if checks 1. and 2. are true, otherwise 0. + */ +static int can_use_remote_refs(const struct add_opts *opts) +{ + if (!guess_remote) { + if (!opts->quiet) + fprintf_ln(stderr, WORKTREE_ADD_DWIM_ORPHAN_INFER_TEXT); + return 0; + } else if (for_each_remote_ref(first_valid_ref, NULL)) { + return 1; + } else if (!opts->force && remote_get(NULL)) { + die(_("No local or remote refs exist despite at least one remote\n" + "present, stopping; use 'add -f' to overide or fetch a remote first")); + } else if (!opts->quiet) { + fprintf_ln(stderr, WORKTREE_ADD_DWIM_ORPHAN_INFER_TEXT); + } + return 0; +} + +/** + * Determines whether `--orphan` should be inferred in the evaluation of + * `worktree add path/` or `worktree add -b branch path/` and emits an error + * if the supplied arguments would produce an illegal combination when the + * `--orphan` flag is included. + * + * `opts` and `opt_track` contain the other options & flags supplied to the + * command. + * + * remote determines whether to check `can_use_remote_refs()` or not. This + * is primarily to differentiate between the basic `add` DWIM and `add -b`. + * + * Returns 1 when inferring `--orphan`, 0 otherwise, and emits an error when + * `--orphan` is inferred but doing so produces an illegal combination of + * options and flags. Additionally produces an error when remote refs are + * checked and the repo is in a state that looks like the user added a remote + * but forgot to fetch (and did not override the warning with -f). + */ +static int dwim_orphan(const struct add_opts *opts, int opt_track, int remote) +{ + if (can_use_local_refs(opts)) { + return 0; + } else if (remote && can_use_remote_refs(opts)) { + return 0; + } else if (!opts->quiet) { + fprintf_ln(stderr, WORKTREE_ADD_DWIM_ORPHAN_INFER_TEXT); + } + + if (opt_track) { + die(_("'%s' and '%s' cannot be used together"), "--orphan", + "--track"); + } else if (!opts->checkout) { + die(_("'%s' and '%s' cannot be used together"), "--orphan", + "--no-checkout"); + } + return 1; +} + static const char *dwim_branch(const char *path, const char **new_branch) { int n; @@ -723,12 +828,19 @@ static int add(int ac, const char **av, const char *prefix) int n; const char *s = worktree_basename(path, &n); new_branch = xstrndup(s, n); - } else if (new_branch || opts.detach || opts.orphan) { + } else if (opts.orphan || opts.detach) { // No-op + } else if (ac < 2 && new_branch) { + // DWIM: Infer --orphan when repo has no refs. + opts.orphan = dwim_orphan(&opts, !!opt_track, 0); } else if (ac < 2) { + // DWIM: Guess branch name from path. const char *s = dwim_branch(path, &new_branch); if (s) branch = s; + + // DWIM: Infer --orphan when repo has no refs. + opts.orphan = (!s) && dwim_orphan(&opts, !!opt_track, 1); } else if (ac == 2) { struct object_id oid; struct commit *commit; diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index 46eef26179..c7ca8df586 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -705,6 +705,332 @@ test_expect_success 'git worktree --no-guess-remote option overrides config' ' ) ' +test_dwim_orphan () { + local info_text="No possible source branch, inferring '--orphan'" && + local fetch_error_text="fatal: No local or remote refs exist despite at least one remote" && + local orphan_hint="hint: If you meant to create a worktree containing a new orphan branch" && + local invalid_ref_regex="^fatal: invalid reference:\s\+.*" && + local bad_combo_regex="^fatal: '[a-z-]\+' and '[a-z-]\+' cannot be used together" && + + local git_ns="repo" && + local dashc_args="-C $git_ns" && + local use_cd=0 && + + local bad_head=0 && + local empty_repo=1 && + local local_ref=0 && + local use_quiet=0 && + local remote=0 && + local remote_ref=0 && + local use_new_branch=0 && + + local outcome="$1" && + local outcome_text && + local success && + shift && + local args="" && + local context="" && + case "$outcome" in + "infer") + success=1 && + outcome_text='"add" DWIM infer --orphan' + ;; + "no_infer") + success=1 && + outcome_text='"add" DWIM doesnt infer --orphan' + ;; + "fetch_error") + success=0 && + outcome_text='"add" error need fetch' + ;; + "fatal_orphan_bad_combo") + success=0 && + outcome_text='"add" error inferred "--orphan" gives illegal opts combo' + ;; + *) + echo "test_dwim_orphan(): invalid outcome: '$outcome'" >&2 && + return 1 + ;; + esac && + while [ $# -gt 0 ] + do + case "$1" in + # How and from where to create the worktree + "-C_repo") + use_cd=0 && + git_ns="repo" && + dashc_args="-C $git_ns" && + context="$context, 'git -C repo'" + ;; + "-C_wt") + use_cd=0 && + git_ns="wt" && + dashc_args="-C $git_ns" && + context="$context, 'git -C wt'" + ;; + "cd_repo") + use_cd=1 && + git_ns="repo" && + dashc_args="" && + context="$context, 'cd repo && git'" + ;; + "cd_wt") + use_cd=1 && + git_ns="wt" && + dashc_args="" && + context="$context, 'cd wt && git'" + ;; + + # Bypass the "pull first" warning + "force") + args="$args --force" && + context="$context, --force" + ;; + + # Try to use remote refs when DWIM + "guess_remote") + args="$args --guess-remote" && + context="$context, --guess-remote" + ;; + "no_guess_remote") + args="$args --no-guess-remote" && + context="$context, --no-guess-remote" + ;; + + # Whether there is at least one local branch present + "local_ref") + empty_repo=0 && + local_ref=1 && + context="$context, >=1 local branches" + ;; + "no_local_ref") + empty_repo=0 && + context="$context, 0 local branches" + ;; + + # Whether the HEAD points at a valid ref (skip this opt when no refs) + "good_head") + # requires: local_ref + context="$context, valid HEAD" + ;; + "bad_head") + bad_head=1 && + context="$context, invalid (or orphan) HEAD" + ;; + + # Whether the code path is tested with the base add command or -b + "no_-b") + use_new_branch=0 && + context="$context, no --branch" + ;; + "-b") + use_new_branch=1 && + context="$context, --branch" + ;; + + # Whether to check that all output is suppressed (except errors) + # or that the output is as expected + "quiet") + use_quiet=1 && + args="$args --quiet" && + context="$context, --quiet" + ;; + "no_quiet") + use_quiet=0 && + context="$context, no --quiet (expect output)" + ;; + + # Whether there is at least one remote attached to the repo + "remote") + empty_repo=0 && + remote=1 && + context="$context, >=1 remotes" + ;; + "no_remote") + empty_repo=0 && + remote=0 && + context="$context, 0 remotes" + ;; + + # Whether there is at least one valid remote ref + "remote_ref") + # requires: remote + empty_repo=0 && + remote_ref=1 && + context="$context, >=1 fetched remote branches" + ;; + "no_remote_ref") + empty_repo=0 && + remote_ref=0 && + context="$context, 0 fetched remote branches" + ;; + + # Options or flags that become illegal when --orphan is inferred + "no_checkout") + args="$args --no-checkout" && + context="$context, --no-checkout" + ;; + "track") + args="$args --track" && + context="$context, --track" + ;; + + # All other options are illegal + *) + echo "test_dwim_orphan(): invalid arg: '$1'" >&2 && + return 1 + ;; + esac && + shift + done && + context="${context#', '}" && + if [ $use_new_branch -eq 1 ] + then + args="$args -b foo" + else + context="DWIM (no --branch), $context" + fi && + if [ $empty_repo -eq 1 ] + then + context="empty repo, $context" + fi && + args="$args ../foo" && + context="${context%', '}" && + test_expect_success "$outcome_text w/ $context" ' + test_when_finished "rm -rf repo" && + git init repo && + if [ $local_ref -eq 1 ] && [ "$git_ns" = "repo" ] + then + (cd repo && test_commit commit) && + if [ $bad_head -eq 1 ] + then + git -C repo symbolic-ref HEAD refs/heads/badbranch + fi + elif [ $local_ref -eq 1 ] && [ "$git_ns" = "wt" ] + then + test_when_finished "git -C repo worktree remove -f ../wt" && + git -C repo worktree add --orphan -b main ../wt && + (cd wt && test_commit commit) && + if [ $bad_head -eq 1 ] + then + git -C wt symbolic-ref HEAD refs/heads/badbranch + fi + elif [ $local_ref -eq 0 ] && [ "$git_ns" = "wt" ] + then + test_when_finished "git -C repo worktree remove -f ../wt" && + git -C repo worktree add --orphan -b orphanbranch ../wt + fi && + + if [ $remote -eq 1 ] + then + test_when_finished "rm -rf upstream" && + git init upstream && + (cd upstream && test_commit commit) && + git -C upstream switch -c foo && + git -C repo remote add upstream ../upstream + fi && + + if [ $remote_ref -eq 1 ] + then + git -C repo fetch + fi && + if [ $success -eq 1 ] + then + test_when_finished git -C repo worktree remove ../foo + fi && + ( + if [ $use_cd -eq 1 ] + then + cd $git_ns + fi && + if [ "$outcome" = "infer" ] + then + git $dashc_args worktree add $args 2>actual && + if [ $use_quiet -eq 1 ] + then + test_must_be_empty actual + else + grep "$info_text" actual + fi + elif [ "$outcome" = "no_infer" ] + then + git $dashc_args worktree add $args 2>actual && + if [ $use_quiet -eq 1 ] + then + test_must_be_empty actual + else + ! grep "$info_text" actual + fi + elif [ "$outcome" = "fetch_error" ] + then + test_must_fail git $dashc_args worktree add $args 2>actual && + grep "$fetch_error_text" actual + elif [ "$outcome" = "fatal_orphan_bad_combo" ] + then + test_must_fail git $dashc_args worktree add $args 2>actual && + if [ $use_quiet -eq 1 ] + then + ! grep "$info_text" actual + else + grep "$info_text" actual + fi && + grep "$bad_combo_regex" actual + elif [ "$outcome" = "warn_bad_head" ] + then + test_must_fail git $dashc_args worktree add $args 2>actual && + if [ $use_quiet -eq 1 ] + then + grep "$invalid_ref_regex" actual && + ! grep "$orphan_hint" actual + else + headpath=$(git $dashc_args rev-parse --sq --path-format=absolute --git-path HEAD) && + headcontents=$(cat "$headpath") && + grep "HEAD points to an invalid (or orphaned) reference" actual && + grep "HEAD path:\s*.$headpath." actual && + grep "HEAD contents:\s*.$headcontents." actual && + grep "$orphan_hint" actual && + ! grep "$info_text" actual + fi && + grep "$invalid_ref_regex" actual + else + # Unreachable + false + fi + ) && + if [ $success -ne 1 ] + then + test_path_is_missing foo + fi + ' +} + +for quiet_mode in "no_quiet" "quiet" +do + for changedir_type in "cd_repo" "cd_wt" "-C_repo" "-C_wt" + do + dwim_test_args="$quiet_mode $changedir_type" + test_dwim_orphan 'infer' $dwim_test_args no_-b + test_dwim_orphan 'no_infer' $dwim_test_args no_-b local_ref good_head + test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref no_remote no_remote_ref no_guess_remote + test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref no_guess_remote + test_dwim_orphan 'fetch_error' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote + test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote force + test_dwim_orphan 'no_infer' $dwim_test_args no_-b no_local_ref remote remote_ref guess_remote + + test_dwim_orphan 'infer' $dwim_test_args -b + test_dwim_orphan 'no_infer' $dwim_test_args -b local_ref good_head + test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref no_remote no_remote_ref no_guess_remote + test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref no_guess_remote + test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref guess_remote + test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote remote_ref guess_remote + done + + test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b no_checkout + test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b track + test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode -b no_checkout + test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode -b track +done + post_checkout_hook () { test_when_finished "rm -rf .git/hooks" && mkdir .git/hooks && From patchwork Sun May 7 12:07:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Abel X-Patchwork-Id: 13233724 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 90C0DC77B7D for ; Sun, 7 May 2023 12:07:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231540AbjEGMHj (ORCPT ); Sun, 7 May 2023 08:07:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230225AbjEGMHh (ORCPT ); Sun, 7 May 2023 08:07:37 -0400 Received: from mail-40136.proton.ch (mail-40136.proton.ch [185.70.40.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F7E114E48 for ; Sun, 7 May 2023 05:07:31 -0700 (PDT) Date: Sun, 07 May 2023 12:07:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nullpo.dev; s=protonmail3; t=1683461249; x=1683720449; bh=FMoAr+pgHWkKt3yYSft2rtS/oPhEwMshg9YNbWQomGM=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=HdGmK4N07/V5nF/zXeinRFCuPoI2lU7oMd197v+NZqWjpRjY5F+U3mWTrTAc1jjVs vT6R3g4gd1XAlApQkjHuFSi32O0MKQdzhaLwz/2GkrAbpdia8AEHLnwZH2qB5XC311 1sCvXkz3uhO1LaJxjINYUd0TYS+slt6U3sParrJPqRMaTSJXLktnO6Ajn+o+aEveMl hJE4Tm0NhRLuYnbSEfX3sQ1gctjkr0wZuflt7oY7spcFnJmaVC8x6R92fLW4o5ZAjb Om/r/CQAjrhP0Xv5mGjC9tGuPWZ7l+t/hgMJnK7/5M6+I8AZVVH8Flch/Z2vlL7CwC fuz2iYhH/s5jw== To: git@vger.kernel.org From: Jacob Abel Cc: Jacob Abel , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Eric Sunshine , Junio C Hamano , Phillip Wood , =?utf-8?q?Rub=C3=A9n_Justo?= , Taylor Blau , rsbecker@nexbridge.com Subject: [PATCH v10 8/8] worktree add: emit warn when there is a bad HEAD Message-ID: <20230507120530.14669-9-jacobabel@nullpo.dev> Feedback-ID: 21506737:user:proton MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a warning to `worktree add` when the command tries to reference HEAD, there exist valid local branches, and the HEAD points to a non-existent reference. Current Behavior: % git -C foo worktree list /path/to/repo/foo dadc8e6dac [main] /path/to/repo/foo_wt 0000000000 [badref] % git -C foo worktree add ../wt1 Preparing worktree (new branch 'wt1') HEAD is now at dadc8e6dac dummy commit % git -C foo_wt worktree add ../wt2 hint: If you meant to create a worktree containing a new orphan branch [...] hint: Disable this message with "git config advice.worktreeAddOrphan false" fatal: invalid reference: HEAD % New Behavior: % git -C foo worktree list /path/to/repo/foo dadc8e6dac [main] /path/to/repo/foo_wt 0000000000 [badref] % git -C foo worktree add ../wt1 Preparing worktree (new branch 'wt1') HEAD is now at dadc8e6dac dummy commit % git -C foo_wt worktree add ../wt2 warning: HEAD points to an invalid (or orphaned) reference. HEAD path: '/path/to/repo/foo/.git/worktrees/foo_wt/HEAD' HEAD contents: 'ref: refs/heads/badref' hint: If you meant to create a worktree containing a new orphan branch [...] hint: Disable this message with "git config advice.worktreeAddOrphan false" fatal: invalid reference: HEAD % Signed-off-by: Jacob Abel --- builtin/worktree.c | 34 +++++++++++++++++++++++++++++----- t/t2400-worktree-add.sh | 18 +++++++++++++++++- 2 files changed, 46 insertions(+), 6 deletions(-) -- 2.39.3 diff --git a/builtin/worktree.c b/builtin/worktree.c index 093b2cb032..5f62084334 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -638,6 +638,9 @@ static int first_valid_ref(const char *refname, * * - Checks whether any valid local branches exist. * + * - Emits a warning if there exist any valid branches but HEAD does not point + * to a valid reference. + * * Returns 1 if any of the previous checks are true, otherwise returns 0. */ static int can_use_local_refs(const struct add_opts *opts) @@ -645,6 +648,23 @@ static int can_use_local_refs(const struct add_opts *opts) if (head_ref(first_valid_ref, NULL)) { return 1; } else if (for_each_branch_ref(first_valid_ref, NULL)) { + if (!opts->quiet) { + struct strbuf path = STRBUF_INIT; + struct strbuf contents = STRBUF_INIT; + + strbuf_add_real_path(&path, get_worktree_git_dir(NULL)); + strbuf_addstr(&path, "/HEAD"); + strbuf_read_file(&contents, path.buf, 64); + strbuf_stripspace(&contents, 0); + strbuf_strip_suffix(&contents, "\n"); + + warning(_("HEAD points to an invalid (or orphaned) reference.\n" + "HEAD path: '%s'\n" + "HEAD contents: '%s'"), + path.buf, contents.buf); + strbuf_release(&path); + strbuf_release(&contents); + } return 1; } return 0; @@ -666,16 +686,12 @@ static int can_use_local_refs(const struct add_opts *opts) static int can_use_remote_refs(const struct add_opts *opts) { if (!guess_remote) { - if (!opts->quiet) - fprintf_ln(stderr, WORKTREE_ADD_DWIM_ORPHAN_INFER_TEXT); return 0; } else if (for_each_remote_ref(first_valid_ref, NULL)) { return 1; } else if (!opts->force && remote_get(NULL)) { die(_("No local or remote refs exist despite at least one remote\n" "present, stopping; use 'add -f' to overide or fetch a remote first")); - } else if (!opts->quiet) { - fprintf_ln(stderr, WORKTREE_ADD_DWIM_ORPHAN_INFER_TEXT); } return 0; } @@ -828,8 +844,12 @@ static int add(int ac, const char **av, const char *prefix) int n; const char *s = worktree_basename(path, &n); new_branch = xstrndup(s, n); - } else if (opts.orphan || opts.detach) { + } else if (opts.orphan) { // No-op + } else if (opts.detach) { + // Check HEAD + if (!strcmp(branch, "HEAD")) + can_use_local_refs(&opts); } else if (ac < 2 && new_branch) { // DWIM: Infer --orphan when repo has no refs. opts.orphan = dwim_orphan(&opts, !!opt_track, 0); @@ -854,6 +874,10 @@ static int add(int ac, const char **av, const char *prefix) branch = remote; } } + + if (!strcmp(branch, "HEAD")) + can_use_local_refs(&opts); + } if (!opts.orphan && !lookup_commit_reference_by_name(branch)) { diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index c7ca8df586..0ac468e69e 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -722,6 +722,7 @@ test_dwim_orphan () { local use_quiet=0 && local remote=0 && local remote_ref=0 && + local use_detach=0 && local use_new_branch=0 && local outcome="$1" && @@ -747,6 +748,10 @@ test_dwim_orphan () { success=0 && outcome_text='"add" error inferred "--orphan" gives illegal opts combo' ;; + "warn_bad_head") + success=0 && + outcome_text='"add" error, warn on bad HEAD, hint use orphan' + ;; *) echo "test_dwim_orphan(): invalid outcome: '$outcome'" >&2 && return 1 @@ -818,7 +823,7 @@ test_dwim_orphan () { context="$context, invalid (or orphan) HEAD" ;; - # Whether the code path is tested with the base add command or -b + # Whether the code path is tested with the base add command, -b, or --detach "no_-b") use_new_branch=0 && context="$context, no --branch" @@ -827,6 +832,10 @@ test_dwim_orphan () { use_new_branch=1 && context="$context, --branch" ;; + "detach") + use_detach=1 && + context="$context, --detach" + ;; # Whether to check that all output is suppressed (except errors) # or that the output is as expected @@ -887,6 +896,9 @@ test_dwim_orphan () { if [ $use_new_branch -eq 1 ] then args="$args -b foo" + elif [ $use_detach -eq 1 ] + then + args="$args --detach" else context="DWIM (no --branch), $context" fi && @@ -1023,6 +1035,10 @@ do test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref no_guess_remote test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref guess_remote test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote remote_ref guess_remote + + test_dwim_orphan 'warn_bad_head' $dwim_test_args no_-b local_ref bad_head + test_dwim_orphan 'warn_bad_head' $dwim_test_args -b local_ref bad_head + test_dwim_orphan 'warn_bad_head' $dwim_test_args detach local_ref bad_head done test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b no_checkout