Repository navigation
fix: refuse to cherry-pick over local changes - #2444
Conversation
cherryPick rewrote the working tree with the picked commit even when the files it touched had staged or unstaged changes, or an untracked file was in the way, silently discarding those bytes. Like git, check the paths the commit changes against HEAD, the index, and the working tree first and throw CherryPickLocalChangesError instead. When conflicts are written out (abortOnConflict: false) every tracked file is rewritten, so in that mode any local change to a tracked file blocks the cherry-pick. Fixes isomorphic-git#2436
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (7)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthroughCherry-pick now checks for local changes before writing to the working tree and reports affected paths through ChangesCherry-pick local-change protection
Contributor listing
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Bug fix · Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to Cherry-pick now refuses to overwrite local changes and reports the affected files. No blocking issue was found, so the change looks safe to merge. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The new check reduces accidental overwrites without introducing a new filesystem principal or repository access boundary. Protection against concurrent changes and partial failures remains unproven. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
Full details: Out of Scope Changes checkExplanation The cherry-pick implementation, tests, API documentation, and error export support issue Full details: Docstring CoverageExplanation Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 5 files. (2 skipped: 2 unsupported.)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. A rabbit checks each changed track, Comment |
|
🎉 This PR is included in version 1.42.6 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Fixes #2436
cherryPickoverwrote local changes in the working tree with the picked commit's version of a file. It created the new commit and returned successfully, and the local bytes were gone.git cherry-pickrefuses in the same situation.What changed
Before merging,
_cherryPicknow collects the files the picked commit changes (a walk of its parent tree against its own tree) and compares each one against HEAD, the index, and the working tree. It throws a newCherryPickLocalChangesError(data.filepaths) before anything is written when one of these files has:A file that is missing from the working tree is not reported, because there is nothing to lose.
The check only runs when the working tree is actually going to be written:
diris set, and either!dryRun && !noUpdateBranch, orabortOnConflict: false. Local edits to files the commit does not touch are kept, the same as in git.When
abortOnConflict: falsehits a conflict,mergeTreewrites out every blob of the merged tree, not only the conflicted files. That would revert edits to unrelated tracked files too, so in that mode the check covers every tracked file. This is stricter than git, which only writes the touched paths. An alternative is to makemergeTreewrite only the paths that differ fromours. I tried that, buttest-abortMergerelies on the full write-out, because its fixtures' working trees don't match HEAD. That felt like a separate decision formerge, so I leftmergeTreealone. Happy to open a follow-up if you'd like that behavior changed.One difference from git: git refuses a cherry-pick whenever the index differs from HEAD at all. Here, only staged changes to files the commit touches block it. Other staged changes are left in the index untouched, so nothing is lost.
I checked each scenario in the new tests against native git. Git refuses (exit 128) for the unstaged edit, staged edit, untracked file, and edit-to-deleted-file cases, and succeeds when only an unrelated file is dirty.
Tests
New
cherryPick › local changescases in__tests__/test-cherryPick.js:abortOnConflict: false+noUpdateBranch, and an unrelated tracked edit when conflicts would be written outnoUpdateBranch(the working tree isn't written)The five rejection cases for touched files fail on
mainand pass with this change. The full Node suite (jest --ciwith the proxy and git-http-mock-server running) passes: 192 suites, 1385 tests, 18 skipped.eslint src __tests__is clean.tsc -p tsconfig.jsonreports the same errors with and without this change, all of them in the build output and a fixtures import. I did not run the browser suites.I'm fixing a bug or typo
npm run add-contributorand follow the prompts to add yourself to the READMESummary by CodeRabbit