Skip to content

Conversation

@cte
Copy link
Collaborator

@cte cte commented Feb 11, 2025

Description

I evaluated a bunch of options, including detecting locked files, and ultimately I think the best option is to stop using git stash and instead using something which won't put your repo in a bad state when git errors occur for any reason.

The git stash operation is fundamentally unsafe because it is not guaranteed to be atomic; a failure in the middle of stashing can cause havoc. Ideally a failure should return the git workspace and index. The good news is that git has such an atomic operation; namely a commit. So if we simulate a stash by creating a "stash" branch and committing to it then a failure will leave the index exactly as it was before you tried to commit, and this is much safer.

To that end, I changed the saveCheckpoint logic to work exactly like it was before, but without using a destructive stash operation (note that I am using a non-destructive git stash create option in order to preserve the state of your staged and partially staged files).

Overall this is more complex, but safer.

The unit tests are pretty good and they pass without any modifications 💪

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Checklist:

  • My code follows the patterns of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation

Additional context

Related Issues

Reviewers


Important

Replaces git stash with a safer temporary branch commit method in CheckpointService for atomic checkpoint operations.

  • Behavior:
    • Replaces git stash with temporary branch commits in CheckpointService for atomic operations.
    • saveCheckpoint now creates a temporary branch, stages changes, commits them, and cherry-picks to a hidden branch.
    • Removes pushStash, applyStash, and popStash functions.
  • Error Handling:
    • Implements error recovery in saveCheckpoint by restoring the main branch and deleting temporary branches on failure.
  • Constants:
    • Adds CHECKPOINT_BRANCH and STASH_BRANCH constants to CheckpointService.

This description was created by Ellipsis for 6dbb596. It will automatically update as commits are pushed.

@changeset-bot
Copy link

changeset-bot bot commented Feb 11, 2025

⚠️ No Changeset found

Latest commit: 6dbb596

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

}

if (stashSha) {
console.log(`[restoreMain] applying stash ${stashSha}`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the injected logging function (this.log) instead of console.log for consistency & centralized logging (see our Dev Standards).

Suggested change
console.log(`[restoreMain] applying stash ${stashSha}`)
this.log(`[restoreMain] applying stash ${stashSha}`)

Copy link
Collaborator

@mrubens mrubens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@mrubens mrubens merged commit 43d4c5c into main Feb 11, 2025
6 checks passed
@mrubens mrubens deleted the cte/stashless-checkpoints branch February 11, 2025 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants