From 63e346ae46bafc8eea4c56a7d74623bb9a914552 Mon Sep 17 00:00:00 2001 From: Louis Knight-Webb Date: Fri, 18 Jul 2025 17:37:42 +0100 Subject: [PATCH] Fix merge (#262) Leaves the reverse of the merge on branch --- backend/src/services/git_service.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/backend/src/services/git_service.rs b/backend/src/services/git_service.rs index 17934885..e841dbe9 100644 --- a/backend/src/services/git_service.rs +++ b/backend/src/services/git_service.rs @@ -1,8 +1,8 @@ use std::path::{Path, PathBuf}; use git2::{ - BranchType, Cred, DiffOptions, Error as GitError, FetchOptions, RebaseOptions, RemoteCallbacks, - Repository, WorktreeAddOptions, + build::CheckoutBuilder, BranchType, Cred, DiffOptions, Error as GitError, FetchOptions, + RebaseOptions, RemoteCallbacks, Repository, WorktreeAddOptions, }; use regex; use tracing::{debug, info}; @@ -217,6 +217,22 @@ impl GitService { base_branch_name, )?; + // Fix: Update main repo's HEAD if it's pointing to the base branch + let main_repo = self.open_repo()?; + let refname = format!("refs/heads/{}", base_branch_name); + + if let Ok(main_head) = main_repo.head() { + if let Some(branch_name) = main_head.shorthand() { + if branch_name == base_branch_name { + // Only update main repo's HEAD if it's currently on the base branch + main_repo.set_head(&refname)?; + let mut co = CheckoutBuilder::new(); + co.force(); + main_repo.checkout_head(Some(&mut co))?; + } + } + } + info!("Created squash merge commit: {}", squash_commit_id); Ok(squash_commit_id.to_string()) }