Subject: Re: [PATCH] checkout: add --autostash option for branch switching

From: Harald Nordgren <[email-masked]>
Date: Tue, 17 Mar 2026 10:47:16 +0100

>> Why is the dry-run of unpack_trees() not an extra check? I was assuming 
>> that it was because we do the dry-run and then do it for real after 
>> possibly stashing any local changes. That's why I was wondering if we 
>> could avoid the dry-run by creating the stash if the non-dry-run 
>> unpack_trees() failed.
>
> Ah, I didn't even think about that possibility.
>
> Try to unpack anyway, and if unpack_trees() branch switching
> succeeds, we are done.  Otherwise, we can trust that unpack_trees()
> did not do _anything_ to the index or the working tree files, so we
> can create the stash at that time.
>
> Makes sense.

Interesting idea, and thanks for your help with this! I gave it a shot with
this simplification.

It passes the tests, which either means it works, or just that the test
coverage is not good enough to detect new issues introduced by me here.

Harald