Skip to content

fix: refuse to cherry-pick over local changes - #2444

Merged
jcubic merged 2 commits into
isomorphic-git:mainfrom
TonyWu2333:fix/cherry-pick-dirty-worktree
Oct 1, 2026
Merged

jcubic merged 2 commits into
isomorphic-git:mainfrom
TonyWu2333:fix/cherry-pick-dirty-worktree

Conversation

@TonyWu2333

@TonyWu2333 TonyWu2333 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2436

cherryPick overwrote 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-pick refuses in the same situation.

What changed

Before merging, _cherryPick now 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 new CherryPickLocalChangesError (data.filepaths) before anything is written when one of these files has:

  • an unstaged edit (including an edit to a file the commit deletes),
  • a staged change, or
  • an untracked file or directory where the commit adds a file.

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: dir is set, and either !dryRun && !noUpdateBranch, or abortOnConflict: false. Local edits to files the commit does not touch are kept, the same as in git.

When abortOnConflict: false hits a conflict, mergeTree writes 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 make mergeTree write only the paths that differ from ours. I tried that, but test-abortMerge relies on the full write-out, because its fixtures' working trees don't match HEAD. That felt like a separate decision for merge, so I left mergeTree alone. 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 changes cases in __tests__/test-cherryPick.js:

  • reject: unstaged edit, staged edit, untracked file in the way, edit to a file the commit deletes, edit with abortOnConflict: false + noUpdateBranch, and an unrelated tracked edit when conflicts would be written out
  • allow: unrelated local edits (kept intact), and noUpdateBranch (the working tree isn't written)

The five rejection cases for touched files fail on main and pass with this change. The full Node suite (jest --ci with the proxy and git-http-mock-server running) passes: 192 suites, 1385 tests, 18 skipped. eslint src __tests__ is clean. tsc -p tsconfig.json reports 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

  • if this is your first time contributing, run npm run add-contributor and follow the prompts to add yourself to the README
  • squash merge the PR with commit message "fix: [Description of fix]"

Summary by CodeRabbit

  • Bug Fixes
    • Cherry-picking now stops when it would overwrite local changes, including staged edits, unstaged edits, or untracked files in the way. The error identifies the affected files so you can protect or move your work before trying again.
    • Changes in unrelated files are preserved, and conflicts can still be written when no local changes would be overwritten.

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
@coderabbitai

coderabbitai Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

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 configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 95630823-8a06-4521-9357-c3efe07c30a1

📥 Commits

Reviewing files that changed from the base of the PR and between afd8a5e and 82c1a9c.

📒 Files selected for processing (7)
  • .all-contributorsrc
  • README.md
  • __tests__/test-cherryPick.js
  • src/api/cherryPick.js
  • src/commands/cherryPick.js
  • src/errors/CherryPickLocalChangesError.js
  • src/errors/index.js

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

Cherry-pick now checks for local changes before writing to the working tree and reports affected paths through CherryPickLocalChangesError. Tests cover blocked writes, preserved edits, and conflict handling. The contributor list now includes TonyWu.

Changes

Cherry-pick local-change protection

Layer / File(s) Summary
Local-change error contract
src/errors/CherryPickLocalChangesError.js, src/errors/index.js, src/api/cherryPick.js
Adds and exports CherryPickLocalChangesError. The API documentation describes when cherry-pick raises it.
Preflight checks
src/commands/cherryPick.js
Checks applicable paths against HEAD, the index, and the working tree before operations that write to the working tree.
Behavior tests
__tests__/test-cherryPick.js
Tests rejection when local changes would be overwritten, preservation of unrelated edits, noUpdateBranch behavior, and conflict output.

Contributor listing

Layer / File(s) Summary
Contributor entries
.all-contributorsrc, README.md
Adds TonyWu with code, test, and documentation contributions.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 82c1a

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 Review

Security architecture risk: 🔵 Low · up to 82c1a

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
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed control affects the repository worktree and index reachable through the caller’s supplied filesystem and repository paths. No new recipient, service identity, or credential authority is introduced by the reviewed API flow or error payload.

Trust Boundaries and Controls

  • observed — The preflight worktree walker uses lstat-derived directory classification and reads symlink contents with readlink rather than traversing their targets. This supports non-following traversal under the expected filesystem semantics; it does not prove every downstream write path or external filesystem implementation safe.

Resilience and Maintainability Implications

  • inferred — The gate provides protection for a stable repository state, not an atomic reservation through completion. Validation precedes later locks and writes; successful application updates worktree files before updating the index. Concurrent changes and interrupted writes therefore require separate consistency guarantees. Their PR-specific exposure change was not established.

Hardening Proposals

  • proposed — Define whether callers must serialize repository mutations. If concurrent mutation is supported, consider operation-wide serialization or revalidation before writes, together with explicit recovery semantics for partial worktree/index application.
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The cherry-pick implementation, tests, API documentation, and error export support issue #2436. The additions for TonyWu in .all-contributorsrc and README.md only change contributor attribution. T… Remove the TonyWu attribution changes from .all-contributorsrc and README.md, or link them to a directly relevant objective.
Docstring Coverage ⚠️ Warning 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… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issue #2436 requires cherry-pick to refuse before writing when a tracked working-tree file with local changes would be overwritten. src/commands/cherryPick.js compares the picked commit paths with H…
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing cherry-pick operations from overwriting local changes.
Full details: Out of Scope Changes check

Explanation

The cherry-pick implementation, tests, API documentation, and error export support issue #2436. The additions for TonyWu in .all-contributorsrc and README.md only change contributor attribution. They have no demonstrated connection to the linked issue or its coding objective.

Full details: Docstring Coverage

Explanation

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.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


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.

❤️ Share

A rabbit checks each changed track,
And guards the bytes before they’re slack.
If local edits block the way,
A named error stops the play.
Tony’s name joins the list today.

Comment @coderabbitai help to get the list of available commands.

@jcubic
jcubic merged commit 431453f into isomorphic-git:main Oct 1, 2026
4 checks passed
@jcubic

jcubic commented Oct 1, 2026

Copy link
Copy Markdown
Member

🎉 This PR is included in version 1.42.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

@jcubic jcubic added the released label Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] isomorphic-git cherry-pick overwrites a dirty tracked file

2 participants