a73x

7cf29cea

Fix working tree sync: use reset(Hard) instead of checkout_head

a73x   2026-03-22 07:53

checkout_head only updates the working tree but leaves the index stale.
reset(Hard) updates both index and working tree to match HEAD, which is
what we need after updating a ref via plumbing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

diff --git a/src/patch.rs b/src/patch.rs
index 7a50c12..86528d9 100644
--- a/src/patch.rs
+++ b/src/patch.rs
@@ -448,14 +448,17 @@ pub fn merge(repo: &Repository, id_prefix: &str) -> Result<PatchState, crate::er
        )?;
    }

    // Update working tree if HEAD is on the target branch
    // Update working tree + index if HEAD is on the target branch
    if let Ok(head_ref) = repo.head() {
        if head_ref.is_branch()
            && head_ref.shorthand() == Some(&p.base_ref)
        {
            repo.checkout_head(Some(
                git2::build::CheckoutBuilder::new().force(),
            ))?;
            let target_commit = head_ref.peel_to_commit()?;
            repo.reset(
                target_commit.as_object(),
                git2::ResetType::Hard,
                None,
            )?;
        }
    }