Reset correctly if rebase fails (#513)

* Commit changes from coding agent for task attempt 66756e53-531f-4155-88cf-079c19c69bc2

* Cleanup script changes for task attempt 66756e53-531f-4155-88cf-079c19c69bc2

* Commit changes from coding agent for task attempt 66756e53-531f-4155-88cf-079c19c69bc2

* give user instruction to resolve manually
This commit is contained in:
Louis Knight-Webb
2025-08-18 15:56:21 +01:00
committed by GitHub
parent a64a3a00d0
commit e054c984b7

View File

@@ -842,6 +842,9 @@ impl GitService {
let new_base_commit_id = base_branch.get().peel_to_commit()?.id();
// Remember the original task-branch commit before we touch anything
let original_head_oid = worktree_repo.head()?.peel_to_commit()?.id();
// Get the HEAD commit of the worktree (the changes to rebase)
let head = worktree_repo.head()?;
let task_branch_commit_id = head.peel_to_commit()?.id();
@@ -872,17 +875,32 @@ impl GitService {
new_base_commit_id,
)?;
if !unique_commits.is_empty() {
// Attempt the rebase operation
let rebase_result = if !unique_commits.is_empty() {
// Reset HEAD to the new base branch
let new_base_commit = worktree_repo.find_commit(new_base_commit_id)?;
worktree_repo.reset(new_base_commit.as_object(), git2::ResetType::Hard, None)?;
// Cherry-pick the unique commits
Self::cherry_pick_commits(&worktree_repo, &unique_commits, &signature)?;
Self::cherry_pick_commits(&worktree_repo, &unique_commits, &signature)
} else {
// No unique commits to rebase, just reset to new base
let new_base_commit = worktree_repo.find_commit(new_base_commit_id)?;
worktree_repo.reset(new_base_commit.as_object(), git2::ResetType::Hard, None)?;
Ok(())
};
// Handle rebase failure by restoring original state
if let Err(e) = rebase_result {
// Clean up any cherry-pick state
let _ = worktree_repo.cleanup_state();
// Restore original task branch state
if let Ok(orig_commit) = worktree_repo.find_commit(original_head_oid) {
let _ = worktree_repo.reset(orig_commit.as_object(), git2::ResetType::Hard, None);
}
return Err(e);
}
// Get the final commit ID after rebase
@@ -1164,7 +1182,7 @@ impl GitService {
let mut index = repo.index()?;
if index.has_conflicts() {
return Err(GitServiceError::MergeConflicts(format!(
"Cherry-pick failed due to conflicts on commit {commit_id}"
"Cherry-pick failed due to conflicts on commit {commit_id}, please resolve conflicts manually"
)));
}