Skip to content

Commit

Permalink
feat: add gix merge commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Nov 7, 2024
1 parent 7aee32a commit 4a5afc7
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
88 changes: 88 additions & 0 deletions gitoxide-core/src/repository/merge/commit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use crate::OutputFormat;
use anyhow::{anyhow, bail, Context};
use gix::bstr::BString;
use gix::bstr::ByteSlice;
use gix::merge::tree::UnresolvedConflict;
use gix::prelude::Write;

use super::tree::Options;

#[allow(clippy::too_many_arguments)]
pub fn commit(
mut repo: gix::Repository,
out: &mut dyn std::io::Write,
err: &mut dyn std::io::Write,
ours: BString,
theirs: BString,
Options {
format,
file_favor,
in_memory,
}: Options,
) -> anyhow::Result<()> {
if format != OutputFormat::Human {
bail!("JSON output isn't implemented yet");
}
repo.object_cache_size_if_unset(repo.compute_object_cache_size_for_tree_diffs(&**repo.index_or_empty()?));
if in_memory {
repo.objects.enable_object_memory();
}
let (ours_ref, ours_id) = refname_and_commit(&repo, ours)?;
let (theirs_ref, theirs_id) = refname_and_commit(&repo, theirs)?;

let options = repo.tree_merge_options()?.with_file_favor(file_favor);
let ours_id_str = ours_id.to_string();
let theirs_id_str = theirs_id.to_string();
let labels = gix::merge::blob::builtin_driver::text::Labels {
ancestor: None,
current: ours_ref
.as_ref()
.map_or(ours_id_str.as_str().into(), |n| n.as_bstr())
.into(),
other: theirs_ref
.as_ref()
.map_or(theirs_id_str.as_str().into(), |n| n.as_bstr())
.into(),
};
let res = repo
.merge_commits(ours_id, theirs_id, labels, options.into())?
.tree_merge;
let has_conflicts = res.conflicts.is_empty();
let has_unresolved_conflicts = res.has_unresolved_conflicts(UnresolvedConflict::Renames);
{
let _span = gix::trace::detail!("Writing merged tree");
let mut written = 0;
let tree_id = res
.tree
.detach()
.write(|tree| {
written += 1;
repo.write(tree)
})
.map_err(|err| anyhow!("{err}"))?;
writeln!(out, "{tree_id} (wrote {written} trees)")?;
}

if !has_conflicts {
writeln!(err, "{} possibly resolved conflicts", res.conflicts.len())?;
}
if has_unresolved_conflicts {
bail!("Tree conflicted")
}
Ok(())
}

fn refname_and_commit(
repo: &gix::Repository,
revspec: BString,
) -> anyhow::Result<(Option<BString>, gix::hash::ObjectId)> {
let spec = repo.rev_parse(revspec.as_bstr())?;
let commit_id = spec
.single()
.context("Expected revspec to expand to a single rev only")?
.object()?
.peel_to_commit()?
.id;
let refname = spec.first_reference().map(|r| r.name.shorten().as_bstr().to_owned());
Ok((refname, commit_id))
}
3 changes: 3 additions & 0 deletions gitoxide-core/src/repository/merge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ pub use file::file;

pub mod tree;
pub use tree::function::tree;

mod commit;
pub use commit::commit;
27 changes: 27 additions & 0 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,33 @@ pub fn main() -> Result<()> {
)
},
),
merge::SubCommands::Commit {
in_memory,
file_favor,
ours,
theirs,
} => prepare_and_run(
"merge-commit",
trace,
verbose,
progress,
progress_keep_open,
None,
move |_progress, out, err| {
core::repository::merge::commit(
repository(Mode::Lenient)?,
out,
err,
ours,
theirs,
core::repository::merge::tree::Options {
format,
file_favor: file_favor.map(Into::into),
in_memory,
},
)
},
),
},
Subcommands::MergeBase(crate::plumbing::options::merge_base::Command { first, others }) => prepare_and_run(
"merge-base",
Expand Down
18 changes: 18 additions & 0 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,25 @@ pub mod merge {
#[clap(value_name = "BASE", value_parser = crate::shared::AsBString)]
base: BString,
/// A revspec to their treeish.
#[clap(value_name = "THEIRS", value_parser = crate::shared::AsBString)]
theirs: BString,
},
/// Merge a commits by specifying ours, and theirs, writing the tree to the object database.
Commit {
/// Keep all objects to be written in memory to avoid any disk IO.
///
/// Note that the resulting tree won't be available or inspectable.
#[clap(long, short = 'm')]
in_memory: bool,
/// Decide how to resolve content conflicts in files. If unset, write conflict markers and fail.
#[clap(long, short = 'f')]
file_favor: Option<FileFavor>,

/// A revspec to our committish.
#[clap(value_name = "OURS", value_parser = crate::shared::AsBString)]
ours: BString,
/// A revspec to their committish.
#[clap(value_name = "THEIRS", value_parser = crate::shared::AsBString)]
theirs: BString,
},
}
Expand Down

0 comments on commit 4a5afc7

Please sign in to comment.