Skip to content

Refuse safe checkout while the index has unresolved conflicts - #15691

Open
krlvi wants to merge 1 commit into
masterfrom
unresolved-index-checkout-guard
Open

Refuse safe checkout while the index has unresolved conflicts#15691
krlvi wants to merge 1 commit into
masterfrom
unresolved-index-checkout-guard

Conversation

@krlvi

@krlvi krlvi commented Sep 1, 2026

Copy link
Copy Markdown
Member

Fixes GB-1937

The problem

Updating the workspace while .git/index still holds unresolved stage 1/2/3 entries died deep inside libgit2's checkout with the raw error unresolved conflicts exist in the index; class=Checkout; code=Conflict — surfaced verbatim to the user with no recovery guidance. Telemetry shows this on both macOS and Windows on stable 0.22.3.

How the state arises

libgit2 refuses any non-force checkout while the repository index has conflict entries — even though GitButler passes its own already-safety-merged in-memory index. Two ways users end up there:

  • GitButler's own conflicted checkout: when a workspace update is allowed to conflict with uncommitted changes, libgit2 writes the conflict stages into .git/index (that is how the conflicted-uncommitted-files state is represented). Until the user marks those files resolved, every subsequent workspace update hit the raw error.
  • A merge run outside GitButler: an unresolved git merge / git cherry-pick in the terminal leaves the same stage entries.

The fix

safe_checkout_from_head now checks for unresolved index entries before the checkout mutates anything and fails with a typed PreconditionFailed naming the conflicted files and pointing at the resolution path (but resolve <path>; desktop/Lite show conflicted uncommitted files with a resolve affordance). Since the checkout runs before any ref edits during materialization, refusing there preserves worktree, index, HEAD, and workspace metadata. This covers all checkout-based operations (workspace update, apply, unapply, …), not just upstream integration.

Two paths deliberately keep working while conflicts are unresolved, and are pinned by tests:

  • No-op checkouts (head tree unchanged, e.g. a reword) never touch the index or worktree.
  • Merge-base-override checkouts: their index rewrite clears the stages before the guard runs. Snapshot restore (undo) always passes an override — it is the escape hatch from exactly this state — and committing the conflicted file itself legitimately resolves it.

Copilot AI lite review requested due to automatic review settings September 1, 2026 15:29
@linear-code

linear-code Bot commented Sep 1, 2026

Copy link
Copy Markdown

GB-1937

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-01T15:42:21.921419Z 4a33e93 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions github-actions Bot added the rust Pull requests that update Rust code label Sep 1, 2026
@krlvi

krlvi commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

@jonathantanmy2 would you mind taking a look at this fix? Is it a reasonable one?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR prevents workspace/materialization checkouts from proceeding when the on-disk repository index (.git/index) contains unresolved conflict (stage 1/2/3) entries, returning a typed precondition failure with actionable guidance instead of surfacing libgit2’s raw checkout conflict error.

Changes:

  • Add a pre-check in safe_checkout_from_head that refuses non-no-op checkouts when the repo index has unresolved conflicts, producing a user-oriented PreconditionFailed error listing conflicted paths.
  • Add regression tests covering: (1) refusal happens before mutating worktree/index/HEAD, and (2) merge-base-override (“steamroll”) checkouts still succeed and clear stale index conflicts.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
crates/but-core/src/worktree/checkout/function.rs Adds an index-conflict precondition guard to safe_checkout_from_head and a helper to format a guided error message.
crates/but-core/tests/core/worktree/checkout.rs Adds tests validating refusal-before-mutation behavior and preserving override checkout semantics under unresolved index conflicts.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/but-core/src/worktree/checkout/function.rs Outdated
Copilot AI review requested due to automatic review settings September 1, 2026 15:41
@krlvi
krlvi force-pushed the unresolved-index-checkout-guard branch from 4a33e93 to 1a5cc16 Compare September 1, 2026 15:41

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4a33e93df0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/but-core/src/worktree/checkout/function.rs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread crates/but-core/tests/core/worktree/checkout.rs
A checkout that is allowed to conflict with uncommitted changes (or a
merge run outside GitButler) leaves stage 1/2/3 entries in .git/index.
The next workspace update then died deep inside libgit2's checkout with
a raw 'unresolved conflicts exist in the index' error.

Check for unresolved index entries before the checkout mutates anything
and fail with a typed precondition naming the conflicted files, so the
frontend can present it as an actionable warning.

Two paths deliberately keep working while conflicts are unresolved:
no-op checkouts (the head tree is unchanged, e.g. a reword), and
merge-base-override checkouts whose index rewrite clears the stages -
snapshot restore always passes an override and is the escape hatch from
exactly this state.
Copilot AI review requested due to automatic review settings September 1, 2026 15:52
@krlvi
krlvi force-pushed the unresolved-index-checkout-guard branch from 1a5cc16 to 66c5958 Compare September 1, 2026 15:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

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

Labels

rust Pull requests that update Rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants