|
| 1 | +use crate::OutputFormat; |
| 2 | +use anyhow::{anyhow, bail, Context}; |
| 3 | +use gix::bstr::BString; |
| 4 | +use gix::bstr::ByteSlice; |
| 5 | +use gix::merge::tree::UnresolvedConflict; |
| 6 | +use gix::prelude::Write; |
| 7 | + |
| 8 | +use super::tree::Options; |
| 9 | + |
| 10 | +#[allow(clippy::too_many_arguments)] |
| 11 | +pub fn commit( |
| 12 | + mut repo: gix::Repository, |
| 13 | + out: &mut dyn std::io::Write, |
| 14 | + err: &mut dyn std::io::Write, |
| 15 | + ours: BString, |
| 16 | + theirs: BString, |
| 17 | + Options { |
| 18 | + format, |
| 19 | + file_favor, |
| 20 | + in_memory, |
| 21 | + }: Options, |
| 22 | +) -> anyhow::Result<()> { |
| 23 | + if format != OutputFormat::Human { |
| 24 | + bail!("JSON output isn't implemented yet"); |
| 25 | + } |
| 26 | + repo.object_cache_size_if_unset(repo.compute_object_cache_size_for_tree_diffs(&**repo.index_or_empty()?)); |
| 27 | + if in_memory { |
| 28 | + repo.objects.enable_object_memory(); |
| 29 | + } |
| 30 | + let (ours_ref, ours_id) = refname_and_commit(&repo, ours)?; |
| 31 | + let (theirs_ref, theirs_id) = refname_and_commit(&repo, theirs)?; |
| 32 | + |
| 33 | + let options = repo.tree_merge_options()?.with_file_favor(file_favor); |
| 34 | + let ours_id_str = ours_id.to_string(); |
| 35 | + let theirs_id_str = theirs_id.to_string(); |
| 36 | + let labels = gix::merge::blob::builtin_driver::text::Labels { |
| 37 | + ancestor: None, |
| 38 | + current: ours_ref |
| 39 | + .as_ref() |
| 40 | + .map_or(ours_id_str.as_str().into(), |n| n.as_bstr()) |
| 41 | + .into(), |
| 42 | + other: theirs_ref |
| 43 | + .as_ref() |
| 44 | + .map_or(theirs_id_str.as_str().into(), |n| n.as_bstr()) |
| 45 | + .into(), |
| 46 | + }; |
| 47 | + let res = repo |
| 48 | + .merge_commits(ours_id, theirs_id, labels, options.into())? |
| 49 | + .tree_merge; |
| 50 | + let has_conflicts = res.conflicts.is_empty(); |
| 51 | + let has_unresolved_conflicts = res.has_unresolved_conflicts(UnresolvedConflict::Renames); |
| 52 | + { |
| 53 | + let _span = gix::trace::detail!("Writing merged tree"); |
| 54 | + let mut written = 0; |
| 55 | + let tree_id = res |
| 56 | + .tree |
| 57 | + .detach() |
| 58 | + .write(|tree| { |
| 59 | + written += 1; |
| 60 | + repo.write(tree) |
| 61 | + }) |
| 62 | + .map_err(|err| anyhow!("{err}"))?; |
| 63 | + writeln!(out, "{tree_id} (wrote {written} trees)")?; |
| 64 | + } |
| 65 | + |
| 66 | + if !has_conflicts { |
| 67 | + writeln!(err, "{} possibly resolved conflicts", res.conflicts.len())?; |
| 68 | + } |
| 69 | + if has_unresolved_conflicts { |
| 70 | + bail!("Tree conflicted") |
| 71 | + } |
| 72 | + Ok(()) |
| 73 | +} |
| 74 | + |
| 75 | +fn refname_and_commit( |
| 76 | + repo: &gix::Repository, |
| 77 | + revspec: BString, |
| 78 | +) -> anyhow::Result<(Option<BString>, gix::hash::ObjectId)> { |
| 79 | + let spec = repo.rev_parse(revspec.as_bstr())?; |
| 80 | + let commit_id = spec |
| 81 | + .single() |
| 82 | + .context("Expected revspec to expand to a single rev only")? |
| 83 | + .object()? |
| 84 | + .peel_to_commit()? |
| 85 | + .id; |
| 86 | + let refname = spec.first_reference().map(|r| r.name.shorten().as_bstr().to_owned()); |
| 87 | + Ok((refname, commit_id)) |
| 88 | +} |
0 commit comments