From cd5b9ac60c1c9730c22bc3e2661914afb8f16340 Mon Sep 17 00:00:00 2001 From: luveti <4952718+luveti@users.noreply.github.com> Date: Sun, 13 Oct 2024 20:57:20 -0400 Subject: [PATCH 1/2] Fixed examples/pull.rs `Repository::commit` needs to be called, otherwise the index gets goofed up. In my specific case, it resulted in a file created in a fetched commit being staged for deletion. https://libgit2.org/libgit2/ex/HEAD/merge.html was referenced for this fix. I also added a call to `Repository::cleanup_state` that was used in the libgit2 example. They did not call `Repository::checkout_head` so I've removed that. --- examples/pull.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/examples/pull.rs b/examples/pull.rs index 27f461e546..057c30837a 100644 --- a/examples/pull.rs +++ b/examples/pull.rs @@ -139,8 +139,8 @@ fn normal_merge( &result_tree, &[&local_commit, &remote_commit], )?; - // Set working tree to match head. - repo.checkout_head(None)?; + // SEE https://libgit2.org/libgit2/ex/HEAD/merge.html + repo.cleanup_state()?; Ok(()) } @@ -181,6 +181,22 @@ fn do_merge<'a>( } }; } else if analysis.0.is_normal() { + // SEE https://libgit2.org/libgit2/ex/HEAD/merge.html + + let mut checkout_options = git2::build::CheckoutBuilder::default(); + checkout_options.allow_conflicts(true); + checkout_options.conflict_style_merge(true); + checkout_options.force(); + + let mut merge_options = MergeOptions::new(); + merge_options.diff3_style(true); + + repo.merge( + &[&fetch_commit], + Some(&mut merge_options), + Some(&mut checkout_options), + )?; + // do a normal merge let head_commit = repo.reference_to_annotated_commit(&repo.head()?)?; normal_merge(&repo, &head_commit, &fetch_commit)?; From 852a0cd685947616f6dcceabda39f9bf527d1341 Mon Sep 17 00:00:00 2001 From: luveti <4952718+luveti@users.noreply.github.com> Date: Sun, 13 Oct 2024 21:00:15 -0400 Subject: [PATCH 2/2] Fix reference --- examples/pull.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pull.rs b/examples/pull.rs index 057c30837a..dfee7e65b3 100644 --- a/examples/pull.rs +++ b/examples/pull.rs @@ -188,7 +188,7 @@ fn do_merge<'a>( checkout_options.conflict_style_merge(true); checkout_options.force(); - let mut merge_options = MergeOptions::new(); + let mut merge_options = git2::MergeOptions::new(); merge_options.diff3_style(true); repo.merge(