Skip to content

Refactor checkout branch and stash logic - #10957

Merged
niik merged 97 commits into
developmentfrom
stash-reentrancy
Nov 24, 2020
Merged

niik merged 97 commits into
developmentfrom
stash-reentrancy

Conversation

@niik

@niik niik commented Nov 2, 2020

Copy link
Copy Markdown
Member

Closes #9297 - Infinite loop when checking out branch
Closes #10956 - GitHub Desktop may overwrite existing stash
Closes #8099 - Reduce complexity of checkout logic
Closes #7617 - Rewrite checkout logic to keep logic inline as opposed to relying on error handlers and continuations.

Description

I set out to understand the cause of #9297 and ended up having to significantly rework our stashing and checkout logic.

What was the problem with the previous logic

Aside from the two serious bugs of an infinite loop (#9297) and the potential for users to loose work by Desktop overwriting their existing stash (#10956) the checkout logic has been a source of much confusion and complexity for a long time. #8099 does a good job of providing an overview of the problem:

appStore._checkoutBranch is a 150 line method with a lot going on. It also has a reentrant problem described in #7617 (See below for more notes.)

For maintainability, it makes sense to extract this method into a few methods (or possibly into its own class). This would make it easier to maintain and open up the possibility for it to be unit tested more easily.

notes on #7617

#7617 describes why checkoutBranch becomes reentrant. Basically, if the checkout fails, it emits an error, exits the method, the error handler then takes over and stashes the current changes and calls checkoutBranch again. This is pretty hard to follow and #7617’s goal was to solve this. But when I tried to solve it in #8076 it felt like I was introducing more complexity to an already complex method. So I closed that PR and opened up this one. Now you are all caught up!

In what ways does the new logic improve things

First of all it removes the re-entrant problem described above by explicitly detecting when changes would be overwritten and acting upon it as it happens rather than relying on an error handler. I've already fixed one issue due to the re-entrant nature (#9038) and #9297 proved there's at least one more serious issue. My comment from the original implementation which introduced this unfortunately proved to be well founded

_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

Furthermore it ensures all different ways of checking out a branch (regular branch checkout, PR checkout, web links, etc) use the same consolidated logic.

Whether it achieves its goal of being easier to understand (and therefore also to maintain) is subjective but I absolutely think so. What was previously scattered across several flows in the application spanning hundreds of lines of code has now been reduced to a single linear flow that's easily debuggable. I think this is best illustrated by the top level logic:

return this.checkoutImplementation(repository, branch, account, strategy)
.then(() => this.onSuccessfulCheckout(repository, branch))
.catch(e => this.emitError(new CheckoutError(e, repository, branch)))
.then(() => this.refreshAfterCheckout(repository, branch))
.finally(() => this.updateCheckoutProgress(repository, null))

The three different strategies for checking out a branch has now been extracted into three distinct methods that no longer share unrelated logic making it easier to reason about each individual one

if (strategy === UncommittedChangesStrategy.StashOnCurrentBranch) {
return this.checkoutAndLeaveChanges(repository, branch, account)
} else if (strategy === UncommittedChangesStrategy.MoveToNewBranch) {
return this.checkoutAndBringChanges(repository, branch, account)
} else {
return this.checkoutIgnoringChanges(repository, branch, account)

Needless stashing

Thanks to this restructuring we're now able to only create a stash when it's really needed (i.e. when git checkout branch fails due to local changes being overwritten) whereas before there were scenarios (such as if there were deleted files in the working directory) where we'd always create a stash regardless of whether it was necessary.

Release notes

Notes:

[Fixed] Let the user know when a checkout fails due to use of assume-unchanged or skip-worktree - #9297
[Fixed] Always show confirmation prompt before overwriting existing stash entry - #10956 

@sakariye233

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants