On Thu, Nov 10 2022, Jacob Abel wrote: So, on a second read-through... > 'git worktree add' [-f] [--detach] [--checkout] [--lock [--reason <string>]] > - [[-b | -B] <new-branch>] <path> [<commit-ish>] > + [[-b | -B | --orphan] <new-branch>] <path> [<commit-ish>] This synopsis is now at least partially wrong, and .... > +--orphan <new-branch>:: > + With `add`, create a new orphan branch named `<new-branch>` in the new > + worktree. See `--orphan` in linkgit:git-switch[1] for details. > + > --porcelain:: > .... > #define BUILTIN_WORKTREE_ADD_USAGE \ > N_("git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]]\n" \ > - " [[-b | -B] <new-branch>] <path> [<commit-ish>]") > + " [[-b | -B | --orphan] <new-branch>] <path> [<commit-ish>]") ...here we say the same, but surely it's only: git worktree add --orphan new-branch /tmp/orphan And not e.g.: git worktree add --orphan new-branch /tmp/orphan origin/next Or whatever, but it's incompatible with <commit-ish>. I think this on top should fix it up: diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 1310bfb564f..3afef985154 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -10,7 +10,9 @@ SYNOPSIS -------- [verse] 'git worktree add' [-f] [--detach] [--checkout] [--lock [--reason <string>]] - [[-b | -B | --orphan] <new-branch>] <path> [<commit-ish>] + [[-b | -B] <new-branch>] <path> [<commit-ish>] +'git worktree add' [-f] [--lock [--reason <string>]] + --orphan <new-branch> <path> 'git worktree list' [-v | --porcelain [-z]] 'git worktree lock' [--reason <string>] <worktree> 'git worktree move' <worktree> <new-path> diff --git a/builtin/worktree.c b/builtin/worktree.c index 71786b72f6b..2b811630b3a 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -17,7 +17,10 @@ #define BUILTIN_WORKTREE_ADD_USAGE \ N_("git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]]\n" \ - " [[-b | -B | --orphan] <new-branch>] <path> [<commit-ish>]") + " [[-b | -B] <new-branch>] <path> [<commit-ish>]"), \ + N_("git worktree add [-f] [--lock [--reason <string>]]\n" \ + " --orphan <new-branch> <path>") + #define BUILTIN_WORKTREE_LIST_USAGE \ N_("git worktree list [-v | --porcelain [-z]]") #define BUILTIN_WORKTREE_LOCK_USAGE \ @@ -668,6 +671,9 @@ static int add(int ac, const char **av, const char *prefix) if (opts.orphan_branch && !opts.checkout) die(_("'%s' and '%s' cannot be used together"), "--orphan", "--no-checkout"); + if (opts.orphan_branch && ac == 2) + die(_("'%s' and '%s' cannot be used together"), "--orphan", + _("<commit-ish>")); if (lock_reason && !keep_locked) die(_("the option '%s' requires '%s'"), "--reason", "--lock"); if (lock_reason) diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index 93c340f4aff..47461d02115 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -326,6 +326,10 @@ test_expect_success '"add" --orphan/--no-checkout mutually exclusive' ' test_must_fail git worktree add --orphan poodle --no-checkout bamboo ' +test_expect_success '"add" --orphan and <commit-ish> mutually exclusive' ' + test_must_fail git worktree add --orphan poodle bamboo main +' + test_expect_success '"add" -B/--detach mutually exclusive' ' test_must_fail git worktree add -B poodle --detach bamboo main ' > - if (ac < 2 && !new_branch && !opts.detach) { > + /* > + * As the orphan cannot be created until the contents of branch > + * are staged, opts.orphan_branch should be treated as both a boolean > + * indicating that `--orphan` was selected and as the name of the new > + * orphan branch from this point on. > + */ I've re-read this a couple of times, and I honestly still don't see what point is trying to drive home. So, "--orphan" is an OPT_STRING(), so it always has a value: $ ./git worktree add --orphan error: option `orphan' requires a value But we init it to NULL, and above we just used it as a boolean *and* below. I.e. this comment would seem to me to fit with code where the reader might be surprised that we're using "opts.orphan_branch" as a string from then on, but we're just copying that to "new_branch", then we always use "opts.orphan_branch" as a boolean for the rest of the function. I may be missing something, but I think this would probably be better just without this comment. E.g. we use "--track", "--lock-reason" etc. in similar ways, and those don't have a comment like that. > + if (opts.orphan_branch) { > + new_branch = opts.orphan_branch; > + } > + > + if (ac < 2 && !new_branch && !opts.detach && !opts.orphan_branch) { In general we shouldn't combine random "if"'s just because a a sufficiently smart compiler could discover a way to reduce work. But in this case these seem to be inherently connected, we always want the not-DWIM behavior with "orphan", no? So shouldn't this just be: if (opts.orphan_branch) { ... } else if (ac < 2 && !new_branch && !opts.detach) { .... } ?