Subject: Re: New orphan worktree?

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Mon, 22 Feb 2021 10:44:55 +0100

On Sun, Feb 21 2021, Eric Sunshine wrote:

> On Wed, Feb 17, 2021 at 8:26 PM Ævar Arnfjörð Bjarmason
> <[email-masked]> wrote:
>> On Wed, Jan 06 2021, Eric Sunshine wrote:
>> > Yep, when/if --orphan is added to `git worktree add`, it should mimic
>> > the behavior of --orphan in git-switch rather than git-checkout.
>>
>> How would a mode for "worktree add --orphan" that mimics checkout rather
>> than switch even look like? The "checkout --orphan" special-case is
>> because we retain the index, so you need to "git rm -rf .".
>>
>> But with worktrees we always get a new index, so AFAICT the only way to
>> make it work like "checkout" would be to have it be the only mode that
>> copies over the current worktree's index.
>
> I hadn't actually put any thought into it aside from (1) `--orphan`
> being a likely candidate for `git worktree add`, and (2) my uses of
> orphan branches always involved `git checkout --orphan && git rm -rf
> .`. I never got as far as thinking about the actual implementation.
>
>> In any case I implemented a rough version of this today, and it uses the
>> "switch" semantics. I only discovered this ML thread afterwards.
>>
>> It's surely full of bugs, and needs test work (see all the BUG(...)),
>> but if someone's interested in taking it further all it should need is
>> some more tests & dealing with the edge cases of incompatible options
>> etc. It's Signed-off-by: Ævar Arnfjörð Bjarmason <[email-masked]>.
>
> Thanks. This looks like a good start.
>
>> +test_expect_success '"add" worktree orphan branch' '
>> +       git worktree add --orphan -b orphan here-orphan &&
>
> Rather than making --orphan a boolean flag, we'd probably want to
> mirror the behavior of the other commands and have <branch> be an
> argument consumed by --orphan:
>
>     git worktree add --orphan <branch> <path>
>
> That would make --orphan, -b, and -B mutually exclusive, much like
> they are for git-checkout, and much like -c, -C, and --orphan are
> mutually exclusive for git-switch.

I see now (but didn't before, I haven't really used "switch" before)
that that's how it works.

But that doesn't seem to make much sense as a UI, maybe I'm missing
something but how do you:

    git switch --orphan existing-branch

Just like you can:

    git switch -C existing-branch <start-point>

It's actually this exact use-case that prompted me to write the --orphan
patch. I wanted to create a "meta" orphan branch in my git.git, but had
an existing local "meta" (from Jeff King) that I'd happened to have
checked out long ago which I first needed to "git branch -D".

Wouldn't it make more sense for a feature like this & back-compat to
start with switch's "--orphan" implying "-c", but you could also supply
"--orphan -C" instead? And in worktree have -b and -B work like they do
for other branches.