Repository navigation
Refactors the stashing flow a bit to reduce unnecessary work - #7474
Conversation
Co-Authored-By: Mathieu Dutour <[email protected]>
Co-Authored-By: iAmWillShepherd <[email protected]>
|
While this approach should make switching branches without errors faster by avoiding two git stash operations, i want to note it will most likely make switching branches with errors slower. Here's the before and after for that case. Before:
After:
This is probably the right approach, making the (probably) majority case faster and the (probably) minority case slower. (I'm saying probably here, cause I haven't spent the time to have solid analysis or data to back up this hunch.) I think its also worth thinking about how we might communicate this to the user so they understand why bringing their changes with them might take a bit longer than the usual switch branches. |
I definitely agree with that. I think it would be best to capture that in a separate issue if we end up going with this approach. |
So, I talked through this a bit more with @billygriffin and I think its fine to ignore this concern for this PR. The cases in which an error will be encountered during checkout is definitely a subset of all "bringing changes with you to a branch" workflows. That is to say, it will be slower in those cases, but those cases are most likely quite uncommon. |
|
@iAmWillShepherd looks like we've got some conflicts now that #7471 has been merged |
|
🍎 |
| return error | ||
| const { branchToCheckout } = gitContext | ||
|
|
||
| await dispatcher.moveChangesToBranchAndCheckout(repository, branchToCheckout) |
There was a problem hiding this comment.
i just noticed this await in another read through the code. i think we should not use await here so that we don't have a second _checkoutBranch executed before the first _checkoutBranch can finish. however this requires us to exit from the initial _checkoutBranch early if checkout is not successful. that feels like a more risky change to the behavior of checkout, which prior to now has never checked wether checkout was actually successful before making some state updates/refreshes.
since it doesn't seem like this is causing any obvious bugs i'm going to say that this is fine for now, but is something we should keep an eye on going forward. 👀
There was a problem hiding this comment.
A great shout @outofambit. _checkoutBranch does become re-entrant in this scenario. I think we should still await here though, not awaiting will not alter the re-entrant nature of the logic as performFailableOperation doesn't itself await the result of the error handlers. I think instead we should focus on your second idea and explore that a little bit more but I definitely agree with your assessment of risk in doing so.
I imagine that the fact that we have this disconnect in the awaitable chain will cause other problems such as the StashAndSwitchBranch progress not working properly. If you choose to move your changes to the next branch and hit Ok, the dialog progress will start spinning but if the checkout fails and the error handler takes over I would imagine that the dialog will close and the stash + checkout operation will continue in the background.
My gut feeling here is that a better approach would be to have _checkoutBranch catch this specific error inline and deal with it accordingly rather than waiting for the global error handler to pick it up. That, however, is a much bigger task that I don't think we'll have time to implement and test but definitely something worth investigating post 2.0.
A completely untested rough draft of how inline-management of this would work:
diff --git a/app/src/lib/stores/app-store.ts b/app/src/lib/stores/app-store.ts
index 05d219da5..5472d89d9 100644
--- a/app/src/lib/stores/app-store.ts
+++ b/app/src/lib/stores/app-store.ts
@@ -2918,10 +2918,48 @@ export class AppStore extends TypedBaseStore<IAppState> {
const checkoutSucceeded =
(await this.withAuthenticatingUser(repository, (repository, account) =>
gitStore.performFailableOperation(
- () =>
- checkoutBranch(repository, account, foundBranch, progress => {
- this.updateCheckoutProgress(repository, progress)
- }),
+ async () => {
+ try {
+ await checkoutBranch(
+ repository,
+ account,
+ foundBranch,
+ progress => {
+ this.updateCheckoutProgress(repository, progress)
+ }
+ )
+ return true
+ } catch (err) {
+ if (
+ !isLocalChangesWouldBeOverwrittenError(err) ||
+ stashToPop !== null
+ ) {
+ throw err
+ }
+
+ if (
+ !(await gitStore.performFailableOperation(() => {
+ return createDesktopStashEntry(repository, foundBranch.name)
+ }))
+ ) {
+ return false
+ }
+
+ stashToPop = await getLastDesktopStashEntryForBranch(
+ repository,
+ foundBranch.name
+ )
+
+ return await checkoutBranch(
+ repository,
+ account,
+ foundBranch,
+ progress => {
+ this.updateCheckoutProgress(repository, progress)
+ }
+ )
+ }
+ },|
Not sure if this error is associated with this PR, but I spotted it a few times. Happy to add repro steps if needed. 49d5d54, mac |
@tierninho thanks for sharing this! Could you file a new issue for that? with repro steps? if you're not seeing any weird behavior along with the error, then i think i'd rather look into that after merging this. |
niik
left a comment
There was a problem hiding this comment.
I have three pieces of feedback this time around but the suggested change is the only one that I think needs to be considered before merging.
| return error | ||
| const { branchToCheckout } = gitContext | ||
|
|
||
| await dispatcher.moveChangesToBranchAndCheckout(repository, branchToCheckout) |
There was a problem hiding this comment.
A great shout @outofambit. _checkoutBranch does become re-entrant in this scenario. I think we should still await here though, not awaiting will not alter the re-entrant nature of the logic as performFailableOperation doesn't itself await the result of the error handlers. I think instead we should focus on your second idea and explore that a little bit more but I definitely agree with your assessment of risk in doing so.
I imagine that the fact that we have this disconnect in the awaitable chain will cause other problems such as the StashAndSwitchBranch progress not working properly. If you choose to move your changes to the next branch and hit Ok, the dialog progress will start spinning but if the checkout fails and the error handler takes over I would imagine that the dialog will close and the stash + checkout operation will continue in the background.
My gut feeling here is that a better approach would be to have _checkoutBranch catch this specific error inline and deal with it accordingly rather than waiting for the global error handler to pick it up. That, however, is a much bigger task that I don't think we'll have time to implement and test but definitely something worth investigating post 2.0.
A completely untested rough draft of how inline-management of this would work:
diff --git a/app/src/lib/stores/app-store.ts b/app/src/lib/stores/app-store.ts
index 05d219da5..5472d89d9 100644
--- a/app/src/lib/stores/app-store.ts
+++ b/app/src/lib/stores/app-store.ts
@@ -2918,10 +2918,48 @@ export class AppStore extends TypedBaseStore<IAppState> {
const checkoutSucceeded =
(await this.withAuthenticatingUser(repository, (repository, account) =>
gitStore.performFailableOperation(
- () =>
- checkoutBranch(repository, account, foundBranch, progress => {
- this.updateCheckoutProgress(repository, progress)
- }),
+ async () => {
+ try {
+ await checkoutBranch(
+ repository,
+ account,
+ foundBranch,
+ progress => {
+ this.updateCheckoutProgress(repository, progress)
+ }
+ )
+ return true
+ } catch (err) {
+ if (
+ !isLocalChangesWouldBeOverwrittenError(err) ||
+ stashToPop !== null
+ ) {
+ throw err
+ }
+
+ if (
+ !(await gitStore.performFailableOperation(() => {
+ return createDesktopStashEntry(repository, foundBranch.name)
+ }))
+ ) {
+ return false
+ }
+
+ stashToPop = await getLastDesktopStashEntryForBranch(
+ repository,
+ foundBranch.name
+ )
+
+ return await checkoutBranch(
+ repository,
+ account,
+ foundBranch,
+ progress => {
+ this.updateCheckoutProgress(repository, progress)
+ }
+ )
+ }
+ },…as a result of error handler
Out for the day, and I think this needs to get in for the today's beta
Rather than depending on the error handler ping pong game See #7474 (comment)

Address some concerns brought up in #7464 under the heading "Only stash when there are problems with checkout" by attempting to implement the flow describe in #7464 (comment).
The following are the different flows this PR implements:
New branch is not ahead of the current branch (ideal)
New branch is ahead of the current branch; create stash (worst case)
DugiteError.LocalChangesOverwrittenNote, all values have been rounded
This PR differs from the flow referenced above in one key way: we resort to creating a temporary stash when there are deleted files in the working directory. What follows is the flow when deleted files exist in the working directory.
Deleted files exist
Same performance as the original Create stash on every checkout flow.
Since we are introducing a new flow that relies on creating a temporary stash,
CheckoutBranchhas been modified to return a value so that we can use it inAppStore._checkoutBranchto determine if checkout succeeded so that we can pop the temporary stash entry if necessary.cc @shiftkey, @outofambit , @billygriffin