diff --git a/src-tauri/src/git/mod.rs b/src-tauri/src/git/mod.rs index aad93508f..f4a9cf0a3 100644 --- a/src-tauri/src/git/mod.rs +++ b/src-tauri/src/git/mod.rs @@ -8,8 +8,8 @@ use tauri::State; use crate::shared::process_core::tokio_command; use crate::git_utils::{ - checkout_branch, commit_to_entry, diff_patch_to_string, diff_stats_for_path, - image_mime_type, list_git_roots as scan_git_roots, parse_github_repo, resolve_git_root, + checkout_branch, commit_to_entry, diff_patch_to_string, diff_stats_for_path, image_mime_type, + list_git_roots as scan_git_roots, parse_github_repo, resolve_git_root, }; use crate::state::AppState; use crate::types::{ @@ -1497,6 +1497,7 @@ pub(crate) async fn create_git_branch( mod tests { use super::*; use std::fs; + use std::path::Path; fn create_temp_repo() -> (PathBuf, Repository) { let root = std::env::temp_dir().join(format!( diff --git a/src-tauri/src/git_utils.rs b/src-tauri/src/git_utils.rs index 69d431726..6a7a79e1f 100644 --- a/src-tauri/src/git_utils.rs +++ b/src-tauri/src/git_utils.rs @@ -38,10 +38,12 @@ pub(crate) fn commit_to_entry(commit: git2::Commit) -> GitLogEntry { pub(crate) fn checkout_branch(repo: &Repository, name: &str) -> Result<(), git2::Error> { let refname = format!("refs/heads/{name}"); - repo.set_head(&refname)?; + let target = repo.revparse_single(&refname)?; + let mut options = git2::build::CheckoutBuilder::new(); options.safe(); - repo.checkout_head(Some(&mut options))?; + repo.checkout_tree(&target, Some(&mut options))?; + repo.set_head(&refname)?; Ok(()) } @@ -90,7 +92,10 @@ pub(crate) fn diff_patch_to_string(patch: &mut git2::Patch) -> Result Option {