|
| 1 | +use crate::git2_to_gix_object_id; |
| 2 | +use anyhow::{Context, Result}; |
| 3 | +use gix::merge::tree::{Options, TreatAsUnresolved}; |
| 4 | + |
| 5 | +pub trait GixRepositoryExt: Sized { |
| 6 | + /// Configure the repository for diff operations between trees. |
| 7 | + /// This means it needs an object cache relative to the amount of files in the repository. |
| 8 | + fn for_tree_diffing(self) -> Result<Self>; |
| 9 | + |
| 10 | + /// Returns `true` if the merge between `our_tree` and `their_tree` is free of conflicts. |
| 11 | + /// Conflicts entail content merges with conflict markers, or anything else that doesn't merge cleanly in the tree. |
| 12 | + /// |
| 13 | + /// # Important |
| 14 | + /// |
| 15 | + /// Make sure the repository is configured [`with_object_memory()`](gix::Repository::with_object_memory()). |
| 16 | + fn merges_cleanly_compat( |
| 17 | + &self, |
| 18 | + ancestor_tree: git2::Oid, |
| 19 | + our_tree: git2::Oid, |
| 20 | + their_tree: git2::Oid, |
| 21 | + ) -> Result<bool>; |
| 22 | + |
| 23 | + /// Just like the above, but with `gix` types. |
| 24 | + fn merges_cleanly( |
| 25 | + &self, |
| 26 | + ancestor_tree: gix::ObjectId, |
| 27 | + our_tree: gix::ObjectId, |
| 28 | + their_tree: gix::ObjectId, |
| 29 | + ) -> Result<bool>; |
| 30 | + |
| 31 | + /// Return default label names when merging trees. |
| 32 | + /// |
| 33 | + /// Note that these should probably rather be branch names, but that's for another day. |
| 34 | + fn default_merge_labels(&self) -> gix::merge::blob::builtin_driver::text::Labels<'static> { |
| 35 | + gix::merge::blob::builtin_driver::text::Labels { |
| 36 | + ancestor: Some("base".into()), |
| 37 | + current: Some("ours".into()), |
| 38 | + other: Some("theirs".into()), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// Tree merge options that enforce undecidable conflicts to be forcefully resolved |
| 43 | + /// to favor ours, both when dealing with content merges and with tree merges. |
| 44 | + fn merge_options_force_ours(&self) -> Result<gix::merge::tree::Options>; |
| 45 | + |
| 46 | + /// Return options suitable for merging so that the merge stops immediately after the first conflict. |
| 47 | + /// It also returns the conflict kind to use when checking for unresolved conflicts. |
| 48 | + fn merge_options_fail_fast( |
| 49 | + &self, |
| 50 | + ) -> Result<( |
| 51 | + gix::merge::tree::Options, |
| 52 | + gix::merge::tree::TreatAsUnresolved, |
| 53 | + )>; |
| 54 | + |
| 55 | + /// Just like [`Self::merge_options_fail_fast()`], but additionally don't perform rename tracking. |
| 56 | + /// This is useful if the merge result isn't going to be used, and we are only interested in knowing |
| 57 | + /// if a merge would succeed. |
| 58 | + fn merge_options_no_rewrites_fail_fast( |
| 59 | + &self, |
| 60 | + ) -> Result<(gix::merge::tree::Options, TreatAsUnresolved)>; |
| 61 | +} |
| 62 | + |
| 63 | +impl GixRepositoryExt for gix::Repository { |
| 64 | + fn for_tree_diffing(mut self) -> anyhow::Result<Self> { |
| 65 | + let bytes = self.compute_object_cache_size_for_tree_diffs(&***self.index_or_empty()?); |
| 66 | + self.object_cache_size_if_unset(bytes); |
| 67 | + Ok(self) |
| 68 | + } |
| 69 | + |
| 70 | + fn merges_cleanly_compat( |
| 71 | + &self, |
| 72 | + ancestor_tree: git2::Oid, |
| 73 | + our_tree: git2::Oid, |
| 74 | + their_tree: git2::Oid, |
| 75 | + ) -> Result<bool> { |
| 76 | + self.merges_cleanly( |
| 77 | + git2_to_gix_object_id(ancestor_tree), |
| 78 | + git2_to_gix_object_id(our_tree), |
| 79 | + git2_to_gix_object_id(their_tree), |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + fn merges_cleanly( |
| 84 | + &self, |
| 85 | + ancestor_tree: gix::ObjectId, |
| 86 | + our_tree: gix::ObjectId, |
| 87 | + their_tree: gix::ObjectId, |
| 88 | + ) -> Result<bool> { |
| 89 | + let (options, conflict_kind) = self.merge_options_no_rewrites_fail_fast()?; |
| 90 | + let merge_outcome = self |
| 91 | + .merge_trees( |
| 92 | + ancestor_tree, |
| 93 | + our_tree, |
| 94 | + their_tree, |
| 95 | + Default::default(), |
| 96 | + options, |
| 97 | + ) |
| 98 | + .context("failed to merge trees")?; |
| 99 | + Ok(!merge_outcome.has_unresolved_conflicts(conflict_kind)) |
| 100 | + } |
| 101 | + |
| 102 | + fn merge_options_force_ours(&self) -> Result<Options> { |
| 103 | + Ok(self |
| 104 | + .tree_merge_options()? |
| 105 | + .with_tree_favor(Some(gix::merge::tree::TreeFavor::Ours)) |
| 106 | + .with_file_favor(Some(gix::merge::tree::FileFavor::Ours))) |
| 107 | + } |
| 108 | + |
| 109 | + fn merge_options_fail_fast(&self) -> Result<(gix::merge::tree::Options, TreatAsUnresolved)> { |
| 110 | + let conflict_kind = TreatAsUnresolved::forced_resolution(); |
| 111 | + let options = self |
| 112 | + .tree_merge_options()? |
| 113 | + .with_fail_on_conflict(Some(conflict_kind)); |
| 114 | + Ok((options, conflict_kind)) |
| 115 | + } |
| 116 | + |
| 117 | + fn merge_options_no_rewrites_fail_fast( |
| 118 | + &self, |
| 119 | + ) -> Result<(gix::merge::tree::Options, TreatAsUnresolved)> { |
| 120 | + let (options, conflict_kind) = self.merge_options_fail_fast()?; |
| 121 | + Ok((options.with_rewrites(None), conflict_kind)) |
| 122 | + } |
| 123 | +} |
0 commit comments