Skip to content

fix(discard): discard staged changes under one index lock - #91

Merged
cheat2001 merged 2 commits into
cheat2001:mainfrom
ChrisLauinger77:feat/discard-rollback
Nov 13, 2026
Merged

cheat2001 merged 2 commits into
cheat2001:mainfrom
ChrisLauinger77:feat/discard-rollback

Conversation

@ChrisLauinger77

@ChrisLauinger77 ChrisLauinger77 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What & why

Take .git/index.lock before touching any file, run the per-file restore against a private copy of the index and write it once. A file that fails stays as it was and is reported through DiscardBatch. If another process holds the lock, nothing is discarded and the error names the lock file.

Paths are matched literally, so discarding [ab].txt no longer resets a.txt (the file name used to go to checkout as a pattern), and picking either name of a staged rename restores the original. No journal, no recovery on open.

Tests

New engine tests in tests/git_engine.rs:

  • a staged file with glob characters leaves the files its name would match staged
  • a held index.lock changes nothing, and the retry after it is gone discards and leaves no lock behind
  • discarding either name of a staged rename restores the original
  • a file that replaced a folder gives the folder back

The first three fail against main's code. cargo fmt --check, cargo clippy --all-targets -D warnings and the full Rust suite pass on macOS.

@ChrisLauinger77
ChrisLauinger77 marked this pull request as ready for review November 9, 2026 17:31
@ChrisLauinger77

Copy link
Copy Markdown
Contributor Author

this one is ready as well - but after #87

@cheat2001

Copy link
Copy Markdown
Owner

Thanks for splitting it out. I'm going to hold this one for now. The crash case is still the thing I need settled first, since a discard that dies halfway leaves the user's files in hidden folders inside their worktree, and those show up as untracked files.

I'm also wary of the size: stage.rs goes from about 560 to 2,700 lines. Could the index lock be taken before any file moves, so a lock failure leaves everything untouched? That might remove most of the need for rollback. If backups are still needed, .git/angkorgit-discard/ with cleanup on open sounds right to me. Once the two hook fixes move to #87, could you rebase this so it's discard only?

@ChrisLauinger77 ChrisLauinger77 changed the title feat(discard): back up and roll back staged discards feat(discard): lock early and recover interrupted staged discards Nov 11, 2026
@ChrisLauinger77
ChrisLauinger77 force-pushed the feat/discard-rollback branch 5 times, most recently from 655da6e to d47c7be Compare November 11, 2026 18:12
@ChrisLauinger77

Copy link
Copy Markdown
Contributor Author

Updated in d47c7be in response to your feedback:

  • Staged discards now take the real per-worktree index.lock before any file moves or checkout. Lock contention leaves the worktree and index untouched. Changes are prepared in a private index and published atomically.
  • Backups now live under the worktree's gitdir at angkorgit-discard/transaction-*/, outside git status. A synced journal lets repository open restore an interrupted operation or clean committed backups. Replaced or uncaptured entries are left untouched, with the retained backup path reported.
  • stage.rs is back to 462 lines. Discard orchestration, filesystem guards and recovery are separate modules. Rollback remains for checkout and I/O failures; lock contention no longer needs it. Child-process tests cover abrupt exits before and after index publication, including overlapping renames.
  • The two hook fixes are in feat(hooks): run post-checkout, post-merge and post-rewrite hooks #87. This PR is now a single discard commit based on feat(hooks): run post-checkout, post-merge and post-rewrite hooks #87's code. Since feat(hooks): run post-checkout, post-merge and post-rewrite hooks #87 is still open, GitHub's main-based diff includes it; here is the discard-only comparison.

Both CI runs are fully green: #91 and #87, including Linux, macOS, Windows, frontend checks and Playwright.

@ChrisLauinger77
ChrisLauinger77 force-pushed the feat/discard-rollback branch 6 times, most recently from 8cc758b to 3418712 Compare November 11, 2026 20:06
@ChrisLauinger77 ChrisLauinger77 changed the title feat(discard): lock early and recover interrupted staged discards feat(discard): transact staged batches with guarded recovery Nov 11, 2026
@ChrisLauinger77

Copy link
Copy Markdown
Contributor Author

Rebased onto main after #87 was squash-merged (abfebd7) and pushed with an explicit --force-with-lease.

This PR now contains only the discard transaction/recovery commit, 2a48d1d. The rebase completed without conflicts; git range-diff confirms the patch is unchanged, and the resulting file tree is byte-for-byte identical to the previous PR head. Updated the description to reflect the merged dependency.

Validation: cargo fmt --check and git diff --check pass. CI for the rebased commit has passed commit-message checks, frontend typecheck/unit tests/build, and Rust checks on Linux and macOS. Windows Rust checks and Playwright are still running.

@cheat2001

Copy link
Copy Markdown
Owner

Thanks for sticking with this. The crash case is settled now that backups live under .git, but the PR went the other way on size: about 3,700 lines of non-test code to replace roughly 100, with a journal, recovery, cross-device copying and Windows NT calls. I measured 2,200 staged files and it's the same speed as main, so the gain is crash safety for a rare case, and I can't maintain that much machinery for it.

What I had in mind was smaller. Take index.lock, run the existing per-file restore against the in-memory index, write the index once, and report any file that fails through DiscardBatch like #87 already does. No journal and no recovery on open.

Also, recovery runs on every tab switch, so a backup it can't recover shows the warning toast every time you switch, with a raw serde message. Even with recovery in, that would need to warn once and offer a way to clear it.

If the smaller version doesn't feel worth it to you, that's completely fine, just tell me and I'll close this. The batch reporting from #87 already covers the main problem, and I really appreciate all the work you've put into the hooks series.

Take .git/index.lock before touching any file, run the per-file restore
against a private copy of the index and write it once. A file that fails
stays as it was and is reported through DiscardBatch. Paths are matched
literally, so a name like [ab].txt no longer resets a.txt, and either name
of a staged rename restores the original. No journal, no recovery on open.
@ChrisLauinger77 ChrisLauinger77 changed the title feat(discard): transact staged batches with guarded recovery fix(discard): discard staged changes under one index lock Nov 13, 2026
@ChrisLauinger77

Copy link
Copy Markdown
Contributor Author

Thanks, that's fair. I've force-pushed the smaller version: take index.lock first, run the per-file restore against a private copy of the index, write it once, and report failed files through DiscardBatch. No journal and no recovery on open, so the tab-switch toast is gone too. It's about 200 lines in stage.rs, with no new modules or dependencies.

Two fixes came along because they fit naturally. Paths are matched literally now: on main, discarding [ab].txt also reset a.txt, because the file name went to checkout as a pattern. And picking either name of a staged rename restores the original. There are four new engine tests: glob characters, a held lock, renames, and a file that replaced a folder.

cheat2001
cheat2001 previously approved these changes Nov 13, 2026

@cheat2001 cheat2001 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks, this is exactly what I had in mind. I checked it against main here: discarding 2,200 staged files went from about 10.5 s to 5 s, and I could reproduce the [ab].txt bug on main, so good catch on that one. One wording nit in the changelog, it doesn't wait for the lock, it stops when another process holds it. I'll fix that when I merge.

@cheat2001
cheat2001 merged commit 087ddba into cheat2001:main Nov 13, 2026
6 checks passed
@ChrisLauinger77
ChrisLauinger77 deleted the feat/discard-rollback branch November 13, 2026 10:44
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.

2 participants