{"thread":{"id":"59705","subject":"[PATCH v10 0/8] worktree: Support `--orphan` when creating new worktrees","startedAt":"2023-05-07T12:06:08Z","lastAt":"2023-05-07T12:19:40Z","messageCount":4,"participants":["Jacob Abel"],"isPatch":true,"patchVersion":10,"patchTotal":8},"messages":[{"id":"476717","messageId":"20230507120530.14669-1-jacobabel@nullpo.dev","threadId":"59705","inReplyTo":null,"subject":"[PATCH v10 0/8] worktree: Support `--orphan` when creating new worktrees","fromName":"Jacob Abel","fromEmail":"jacobabel@nullpo.dev","sentAt":"2023-05-07T12:05:52Z","receivedAt":"2023-05-07T12:06:08Z","isPatch":true,"sender":{"key":"jacobabel@nullpo.dev","avatar":"https://avatars.githubusercontent.com/u/9424043?v=4"},"body":"This patchset introduces the ability to create new worktrees from orphan/unborn\nbranches and introduces DWIM behavior to create worktrees from an orphan branch\nwhen no valid refs exists locally in the repository (as is typical in newly\ninitialized repositories) or on a remote (when `--guess-remote` is used).\n\nThis addresses the issue of `git worktree add` failing when attempting to create\na worktree from a newly initialized repository (which can be seen in this SO\nquestion [1]).\n\nThis patchset has eight parts:\n  * adding `-B` to the usage docs (noticed during dev and it seemed too small\n    to justify a separate submission)\n  * cleaning up a left-behind worktree in t2400\n  * adding a helper fn to simplify testing for mutual exclusion of options\n    in `t/t2400-worktree-add.sh`\n  * adding additional test cases to verify both that behavior doesn't change\n    when using `--quiet` and that the extraneous output is properly suppressed.\n  * adding the ability to create a worktree from an unborn/orphan branch\n    to `git-worktree-add`\n  * adding an advise for using --orphan when `git worktree add` fails due to\n    a bad ref.\n  * adding functionality to DWIM when there are no existing branches and the\n    user likely intends to create an orphan branch.\n  * updating worktree add to emit a warning (containing debug information\n    about the current HEAD) when trying to use a HEAD that points to a\n    non-existant (or unborn) reference and there exist other valid branches.\n\nChanges from v9:\n  * Revert `test_when_finished cat actual` changes in t2400 (2/8)[2].\n  * Rename commit 2/8 to reflect changes.\n  * Revert `test_when_finished cat actual` changes in t2400 (3/8)[3].\n  * Revert `test_when_finished cat actual` changes in t2400 (4/8)[4].\n  * Revert `test_when_finished cat actual` changes in t2400 (5/8).\n  * Remove extraneous whitespace from command in t2400 (5/8).\n  * Revert `test_when_finished cat actual` changes in t2400 (6/8).\n  * Include `advice.h` in `worktree.c` to resolve missing include when\n    applying patch on top of main (6/8).\n  * Revert `test_when_finished cat actual` changes in t2400 (7/8).\n  * Remove extraneous whitespace from comment in `worktree.c` (7/8).\n\n1. https://stackoverflow.com/a/68717229/15064705/\n2. https://lore.kernel.org/git/xmqq8reqkyfz.fsf@gitster.g/\n3. https://lore.kernel.org/git/xmqqmt36jixr.fsf@gitster.g/\n4. https://lore.kernel.org/git/xmqqfs8yjisl.fsf@gitster.g/\n\nJacob Abel (8):\n  worktree add: include -B in usage docs\n  t2400: cleanup created worktree in test\n  t2400: refactor \"worktree add\" opt exclusion tests\n  t2400: add tests to verify --quiet\n  worktree add: add --orphan flag\n  worktree add: introduce \"try --orphan\" hint\n  worktree add: extend DWIM to infer --orphan\n  worktree add: emit warn when there is a bad HEAD\n\n Documentation/config/advice.txt |   4 +\n Documentation/git-worktree.txt  |  16 +-\n advice.c                        |   1 +\n advice.h                        |   1 +\n builtin/worktree.c              | 227 +++++++++++++-\n t/t2400-worktree-add.sh         | 507 +++++++++++++++++++++++++++++++-\n 6 files changed, 735 insertions(+), 21 deletions(-)\n\nRange-diff against v9:\n1:  91153fdb4c = 1:  91153fdb4c worktree add: include -B in usage docs\n2:  8cfbc89dd5 ! 2:  0f30e9a9e3 t2400: print captured git output when finished\n    @@ Metadata\n     Author: Jacob Abel <jacobabel@nullpo.dev>\n\n      ## Commit message ##\n    -    t2400: print captured git output when finished\n    -\n    -    Update tests that capture stderr so that at the end of the test they\n    -    print the captured text back out to stderr. This simplifies debugging\n    -    when inspecting test logs after executing with `-x`.\n    +    t2400: cleanup created worktree in test\n\n         Signed-off-by: Jacob Abel <jacobabel@nullpo.dev>\n\n    @@ t/t2400-worktree-add.sh: test_expect_success 'add -B' '\n\n      test_expect_success 'add --quiet' '\n     +\ttest_when_finished \"git worktree remove -f -f another-worktree\" &&\n    -+\ttest_when_finished cat actual >&2 &&\n      \tgit worktree add --quiet another-worktree main 2>actual &&\n      \ttest_must_be_empty actual\n      '\n3:  ab03d92c3a ! 3:  06e8c53bc6 t2400: refactor \"worktree add\" opt exclusion tests\n    @@ t/t2400-worktree-add.sh: test_expect_success '\"add\" no auto-vivify with --detach\n     +test_wt_add_excl () {\n     +\tlocal opts=\"$*\" &&\n     +\ttest_expect_success \"'worktree add' with '$opts' has mutually exclusive options\" '\n    -+\t\ttest_when_finished cat actual >&2 &&\n     +\t\ttest_must_fail git worktree add $opts 2>actual &&\n     +\t\tgrep -E \"fatal:( options)? .* cannot be used together\" actual\n     +\t'\n4:  d9a3468c93 ! 4:  d9330db91f t2400: add tests to verify --quiet\n    @@ t/t2400-worktree-add.sh: test_expect_success 'add --quiet' '\n     +test_expect_success 'add --quiet -b' '\n     +\ttest_when_finished \"git branch -D quietnewbranch\" &&\n     +\ttest_when_finished \"git worktree remove -f -f another-worktree\" &&\n    -+\ttest_when_finished cat actual >&2 &&\n     +\tgit worktree add --quiet -b quietnewbranch another-worktree 2>actual &&\n     +\ttest_must_be_empty actual\n     +'\n    @@ t/t2400-worktree-add.sh: test_expect_success 'git worktree add --guess-remote se\n      '\n     +test_expect_success 'git worktree add --guess-remote sets up tracking (quiet)' '\n     +\ttest_when_finished rm -rf repo_a repo_b foo &&\n    -+\ttest_when_finished cat repo_b/actual >&2 &&\n     +\tsetup_remote_repo repo_a repo_b &&\n     +\t(\n     +\t\tcd repo_b &&\n5:  8ef9587deb ! 5:  a5a78e5f53 worktree add: add --orphan flag\n    @@ t/t2400-worktree-add.sh: test_expect_success 'add --quiet -b' '\n     +\n     +test_expect_success '\"add --orphan --quiet\"' '\n     +\ttest_when_finished \"git worktree remove -f -f orphandir\" &&\n    -+\ttest_when_finished cat log.actual >&2 &&\n     +\tgit worktree add --quiet --orphan -b neworphan orphandir 2>log.actual &&\n     +\ttest_must_be_empty log.actual &&\n     +\techo refs/heads/neworphan >expected &&\n    @@ t/t2400-worktree-add.sh: test_expect_success 'add --quiet -b' '\n     +\ttest_when_finished \"rm -rf empty_repo\" &&\n     +\techo refs/heads/newbranch >expected &&\n     +\tGIT_DIR=\"empty_repo\" git init --bare &&\n    -+\tgit -C empty_repo  worktree add --orphan -b newbranch worktreedir &&\n    ++\tgit -C empty_repo worktree add --orphan -b newbranch worktreedir &&\n     +\tgit -C empty_repo/worktreedir symbolic-ref HEAD >actual &&\n     +\ttest_cmp expected actual\n     +'\n6:  d2800266f9 ! 6:  96b1946e64 worktree add: introduce \"try --orphan\" hint\n    @@ advice.h: struct string_list;\n      int git_default_advice_config(const char *var, const char *value);\n\n      ## builtin/worktree.c ##\n    +@@\n    + #include \"cache.h\"\n    + #include \"abspath.h\"\n    ++#include \"advice.h\"\n    + #include \"checkout.h\"\n    + #include \"config.h\"\n    + #include \"builtin.h\"\n     @@\n      #define BUILTIN_WORKTREE_UNLOCK_USAGE \\\n      \tN_(\"git worktree unlock <worktree>\")\n    @@ t/t2400-worktree-add.sh: test_expect_success '\"add\" worktree with orphan branch,\n     +\t\tgit init repo &&\n     +\t\t(cd repo && test_commit commit) &&\n     +\t\tgit -C repo switch --orphan noref &&\n    -+\t\ttest_when_finished cat actual >&2 &&\n     +\t\ttest_must_fail git -C repo worktree add $opts foobar/ 2>actual &&\n     +\t\t! grep \"error: unknown switch\" actual &&\n     +\t\tgrep \"hint: If you meant to create a worktree containing a new orphan branch\" actual &&\n    @@ t/t2400-worktree-add.sh: test_expect_success '\"add\" worktree with orphan branch,\n     +\ttest_when_finished \"rm -rf repo\" &&\n     +\tgit init repo &&\n     +\t(cd repo && test_commit commit) &&\n    -+\ttest_when_finished cat actual >&2 &&\n     +\ttest_must_fail git -C repo worktree add --quiet foobar_branch foobar/ 2>actual &&\n     +\t! grep \"error: unknown switch\" actual &&\n     +\t! grep \"hint: If you meant to create a worktree containing a new orphan branch\" actual\n7:  e5e139766c ! 7:  52fef9672c worktree add: extend DWIM to infer --orphan\n    @@ builtin/worktree.c: static void print_preparing_worktree_line(int detach,\n     +/**\n     + * Determines whether `--orphan` should be inferred in the evaluation of\n     + * `worktree add path/` or `worktree add -b branch path/` and emits an error\n    -+ * if the supplied arguments would produce an illegal combination  when the\n    ++ * if the supplied arguments would produce an illegal combination when the\n     + * `--orphan` flag is included.\n     + *\n     + * `opts` and `opt_track` contain the other options & flags supplied to the\n    @@ t/t2400-worktree-add.sh: test_expect_success 'git worktree --no-guess-remote opt\n     +\t\tthen\n     +\t\t\ttest_when_finished git -C repo worktree remove ../foo\n     +\t\tfi &&\n    -+\t\tif [ $use_cd -eq 1 ]\n    -+\t\tthen\n    -+\t\t\ttest_when_finished cat \"$git_ns/actual\" >&2\n    -+\t\telse\n    -+\t\t\ttest_when_finished cat actual >&2\n    -+\t\tfi &&\n     +\t\t(\n     +\t\t\tif [ $use_cd -eq 1 ]\n     +\t\t\tthen\n8:  296226ffd5 = 8:  8c3fded12b worktree add: emit warn when there is a bad HEAD\n--\n2.39.3\n\n\n"},{"id":"476718","messageId":"20230507120530.14669-2-jacobabel@nullpo.dev","threadId":"59705","inReplyTo":"20230507120530.14669-1-jacobabel@nullpo.dev","subject":"[PATCH v10 1/8] worktree add: include -B in usage docs","fromName":"Jacob Abel","fromEmail":"jacobabel@nullpo.dev","sentAt":"2023-05-07T12:06:04Z","receivedAt":"2023-05-07T12:06:23Z","isPatch":true,"sender":{"key":"jacobabel@nullpo.dev","avatar":"https://avatars.githubusercontent.com/u/9424043?v=4"},"body":"Document `-B` next to where `-b` is already documented to bring the\nusage docs in line with other commands such as git checkout.\n\nSigned-off-by: Jacob Abel <jacobabel@nullpo.dev>\n---\n Documentation/git-worktree.txt | 2 +-\n builtin/worktree.c             | 2 +-\n 2 files changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt\nindex 063d6eeb99..b9c12779f1 100644\n--- a/Documentation/git-worktree.txt\n+++ b/Documentation/git-worktree.txt\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [verse]\n 'git worktree add' [-f] [--detach] [--checkout] [--lock [--reason <string>]]\n-\t\t   [-b <new-branch>] <path> [<commit-ish>]\n+\t\t   [(-b | -B) <new-branch>] <path> [<commit-ish>]\n 'git worktree list' [-v | --porcelain [-z]]\n 'git worktree lock' [--reason <string>] <worktree>\n 'git worktree move' <worktree> <new-path>\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex 39e9e5c9ce..d1b4b53f2c 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -22,7 +22,7 @@\n\n #define BUILTIN_WORKTREE_ADD_USAGE \\\n \tN_(\"git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]]\\n\" \\\n-\t   \"                 [-b <new-branch>] <path> [<commit-ish>]\")\n+\t   \"                 [(-b | -B) <new-branch>] <path> [<commit-ish>]\")\n #define BUILTIN_WORKTREE_LIST_USAGE \\\n \tN_(\"git worktree list [-v | --porcelain [-z]]\")\n #define BUILTIN_WORKTREE_LOCK_USAGE \\\n--\n2.39.3\n\n\n"},{"id":"476719","messageId":"20230507120530.14669-3-jacobabel@nullpo.dev","threadId":"59705","inReplyTo":"20230507120530.14669-1-jacobabel@nullpo.dev","subject":"[PATCH v10 2/8] t2400: cleanup created worktree in test","fromName":"Jacob Abel","fromEmail":"jacobabel@nullpo.dev","sentAt":"2023-05-07T12:06:15Z","receivedAt":"2023-05-07T12:06:36Z","isPatch":true,"sender":{"key":"jacobabel@nullpo.dev","avatar":"https://avatars.githubusercontent.com/u/9424043?v=4"},"body":"Signed-off-by: Jacob Abel <jacobabel@nullpo.dev>\n---\n t/t2400-worktree-add.sh | 1 +\n 1 file changed, 1 insertion(+)\n\ndiff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh\nindex d587e0b20d..a3f108347a 100755\n--- a/t/t2400-worktree-add.sh\n+++ b/t/t2400-worktree-add.sh\n@@ -326,6 +326,7 @@ test_expect_success 'add -B' '\n '\n\n test_expect_success 'add --quiet' '\n+\ttest_when_finished \"git worktree remove -f -f another-worktree\" &&\n \tgit worktree add --quiet another-worktree main 2>actual &&\n \ttest_must_be_empty actual\n '\n--\n2.39.3\n\n\n"},{"id":"476726","messageId":"wv62fffrtu7frn7zi34ggv25rt3v6jf4pccas3panksqdmwljc@mro5pqva4t36","threadId":"59705","inReplyTo":"20230507120530.14669-1-jacobabel@nullpo.dev","subject":"Re: [PATCH v10 0/8] worktree: Support `--orphan` when creating new worktrees","fromName":"Jacob Abel","fromEmail":"jacobabel@nullpo.dev","sentAt":"2023-05-07T12:14:50Z","receivedAt":"2023-05-07T12:19:40Z","isPatch":true,"sender":{"key":"jacobabel@nullpo.dev","avatar":"https://avatars.githubusercontent.com/u/9424043?v=4"},"body":"Ok it looks like something with Protonmail is arbitrarily discarding\nemail headers for me.\n\nPlease ignore this revision for the time being and I'll just RESEND it\nanother day once I can figure out what is going on.\n\n"}]}