|
| 1 | +use crate::OutputFormat; |
| 2 | + |
| 3 | +pub struct Options { |
| 4 | + pub format: OutputFormat, |
| 5 | + pub resolve_content_merge: Option<gix::merge::blob::builtin_driver::text::Conflict>, |
| 6 | + pub in_memory: bool, |
| 7 | +} |
| 8 | + |
| 9 | +pub(super) mod function { |
| 10 | + |
| 11 | + use crate::OutputFormat; |
| 12 | + use anyhow::{anyhow, bail, Context}; |
| 13 | + use gix::bstr::BString; |
| 14 | + use gix::bstr::ByteSlice; |
| 15 | + use gix::merge::blob::builtin_driver::binary; |
| 16 | + use gix::merge::blob::builtin_driver::text::Conflict; |
| 17 | + use gix::merge::tree::UnresolvedConflict; |
| 18 | + use gix::prelude::Write; |
| 19 | + |
| 20 | + use super::Options; |
| 21 | + |
| 22 | + #[allow(clippy::too_many_arguments)] |
| 23 | + pub fn tree( |
| 24 | + mut repo: gix::Repository, |
| 25 | + out: &mut dyn std::io::Write, |
| 26 | + err: &mut dyn std::io::Write, |
| 27 | + base: BString, |
| 28 | + ours: BString, |
| 29 | + theirs: BString, |
| 30 | + Options { |
| 31 | + format, |
| 32 | + resolve_content_merge, |
| 33 | + in_memory, |
| 34 | + }: Options, |
| 35 | + ) -> anyhow::Result<()> { |
| 36 | + if format != OutputFormat::Human { |
| 37 | + bail!("JSON output isn't implemented yet"); |
| 38 | + } |
| 39 | + repo.object_cache_size_if_unset(repo.compute_object_cache_size_for_tree_diffs(&**repo.index_or_empty()?)); |
| 40 | + if in_memory { |
| 41 | + repo.objects.enable_object_memory(); |
| 42 | + } |
| 43 | + let (base_ref, base_id) = refname_and_tree(&repo, base)?; |
| 44 | + let (ours_ref, ours_id) = refname_and_tree(&repo, ours)?; |
| 45 | + let (theirs_ref, theirs_id) = refname_and_tree(&repo, theirs)?; |
| 46 | + |
| 47 | + let mut options = repo.tree_merge_options()?; |
| 48 | + if let Some(resolve) = resolve_content_merge { |
| 49 | + options.blob_merge.text.conflict = resolve; |
| 50 | + options.blob_merge.resolve_binary_with = match resolve { |
| 51 | + Conflict::Keep { .. } => None, |
| 52 | + Conflict::ResolveWithOurs => Some(binary::ResolveWith::Ours), |
| 53 | + Conflict::ResolveWithTheirs => Some(binary::ResolveWith::Theirs), |
| 54 | + Conflict::ResolveWithUnion => None, |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + let base_id_str = base_id.to_string(); |
| 59 | + let ours_id_str = ours_id.to_string(); |
| 60 | + let theirs_id_str = theirs_id.to_string(); |
| 61 | + let labels = gix::merge::blob::builtin_driver::text::Labels { |
| 62 | + ancestor: base_ref |
| 63 | + .as_ref() |
| 64 | + .map_or(base_id_str.as_str().into(), |n| n.as_bstr()) |
| 65 | + .into(), |
| 66 | + current: ours_ref |
| 67 | + .as_ref() |
| 68 | + .map_or(ours_id_str.as_str().into(), |n| n.as_bstr()) |
| 69 | + .into(), |
| 70 | + other: theirs_ref |
| 71 | + .as_ref() |
| 72 | + .map_or(theirs_id_str.as_str().into(), |n| n.as_bstr()) |
| 73 | + .into(), |
| 74 | + }; |
| 75 | + let mut res = repo.merge_trees(base_id, ours_id, theirs_id, labels, options)?; |
| 76 | + { |
| 77 | + let _span = gix::trace::detail!("Writing merged tree"); |
| 78 | + let mut written = 0; |
| 79 | + let tree_id = res |
| 80 | + .tree |
| 81 | + .write(|tree| { |
| 82 | + written += 1; |
| 83 | + repo.write(tree) |
| 84 | + }) |
| 85 | + .map_err(|err| anyhow!("{err}"))?; |
| 86 | + writeln!(out, "{tree_id} (wrote {written} trees)")?; |
| 87 | + } |
| 88 | + |
| 89 | + if !res.conflicts.is_empty() { |
| 90 | + writeln!(err, "{} possibly resolved conflicts", res.conflicts.len())?; |
| 91 | + } |
| 92 | + if res.has_unresolved_conflicts(UnresolvedConflict::Renames) { |
| 93 | + bail!("Tree conflicted") |
| 94 | + } |
| 95 | + Ok(()) |
| 96 | + } |
| 97 | + |
| 98 | + fn refname_and_tree( |
| 99 | + repo: &gix::Repository, |
| 100 | + revspec: BString, |
| 101 | + ) -> anyhow::Result<(Option<BString>, gix::hash::ObjectId)> { |
| 102 | + let spec = repo.rev_parse(revspec.as_bstr())?; |
| 103 | + let tree_id = spec |
| 104 | + .single() |
| 105 | + .context("Expected revspec to expand to a single rev only")? |
| 106 | + .object()? |
| 107 | + .peel_to_tree()? |
| 108 | + .id; |
| 109 | + let refname = spec.first_reference().map(|r| r.name.shorten().as_bstr().to_owned()); |
| 110 | + Ok((refname, tree_id)) |
| 111 | + } |
| 112 | +} |
0 commit comments