Skip to content

Commit

Permalink
feat: add --tree-favor to gix merge tree|commit.
Browse files Browse the repository at this point in the history
With it one can decide which side to favor in case of
irreconcilable tree-conflicts.
  • Loading branch information
Byron committed Dec 2, 2024
1 parent 382aff9 commit a82f3f2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
6 changes: 5 additions & 1 deletion gitoxide-core/src/repository/merge/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn commit(
Options {
format,
file_favor,
tree_favor,
in_memory,
debug,
}: Options,
Expand All @@ -31,7 +32,10 @@ pub fn commit(
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 options = repo
.tree_merge_options()?
.with_file_favor(file_favor)
.with_tree_favor(tree_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 {
Expand Down
7 changes: 6 additions & 1 deletion gitoxide-core/src/repository/merge/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::OutputFormat;
pub struct Options {
pub format: OutputFormat,
pub file_favor: Option<gix::merge::tree::FileFavor>,
pub tree_favor: Option<gix::merge::tree::TreeFavor>,
pub in_memory: bool,
pub debug: bool,
}
Expand All @@ -29,6 +30,7 @@ pub(super) mod function {
Options {
format,
file_favor,
tree_favor,
in_memory,
debug,
}: Options,
Expand All @@ -44,7 +46,10 @@ pub(super) mod function {
let (ours_ref, ours_id) = refname_and_tree(&repo, ours)?;
let (theirs_ref, theirs_id) = refname_and_tree(&repo, theirs)?;

let options = repo.tree_merge_options()?.with_file_favor(file_favor);
let options = repo
.tree_merge_options()?
.with_file_favor(file_favor)
.with_tree_favor(tree_favor);
let base_id_str = base_id.to_string();
let ours_id_str = ours_id.to_string();
let theirs_id_str = theirs_id.to_string();
Expand Down
22 changes: 16 additions & 6 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,13 @@ pub fn main() -> Result<()> {
},
),
merge::SubCommands::Tree {
in_memory,
file_favor,
debug,
opts:
merge::SharedOptions {
in_memory,
file_favor,
tree_favor,
debug,
},
ours,
base,
theirs,
Expand All @@ -197,15 +201,20 @@ pub fn main() -> Result<()> {
format,
file_favor: file_favor.map(Into::into),
in_memory,
tree_favor: tree_favor.map(Into::into),
debug,
},
)
},
),
merge::SubCommands::Commit {
in_memory,
file_favor,
debug,
opts:
merge::SharedOptions {
in_memory,
file_favor,
tree_favor,
debug,
},
ours,
theirs,
} => prepare_and_run(
Expand All @@ -225,6 +234,7 @@ pub fn main() -> Result<()> {
core::repository::merge::tree::Options {
format,
file_favor: file_favor.map(Into::into),
tree_favor: tree_favor.map(Into::into),
in_memory,
debug,
},
Expand Down
61 changes: 39 additions & 22 deletions src/plumbing/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,14 @@ pub mod merge {
Theirs,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)]
pub enum TreeFavor {
/// Use only the previous tree entry in case of conflict.
Ancestor,
/// Use only ours tree entry in case of conflict.
Ours,
}

impl From<FileFavor> for gix::merge::tree::FileFavor {
fn from(value: FileFavor) -> Self {
match value {
Expand All @@ -384,6 +392,33 @@ pub mod merge {
}
}

impl From<TreeFavor> for gix::merge::tree::TreeFavor {
fn from(value: TreeFavor) -> Self {
match value {
TreeFavor::Ancestor => gix::merge::tree::TreeFavor::Ancestor,
TreeFavor::Ours => gix::merge::tree::TreeFavor::Ours,
}
}
}

#[derive(Debug, clap::Parser)]
pub struct SharedOptions {
/// 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')]
pub in_memory: bool,
/// Decide how to resolve content conflicts in files. If unset, write conflict markers and fail.
#[clap(long, short = 'f')]
pub file_favor: Option<FileFavor>,
/// Decide how to resolve conflicts in trees, i.e. modification/deletion. If unset, try to preserve both states and fail.
#[clap(long, short = 't')]
pub tree_favor: Option<TreeFavor>,
/// Print additional information about conflicts for debugging.
#[clap(long, short = 'd')]
pub debug: bool,
}

#[derive(Debug, clap::Parser)]
#[command(about = "perform merges of various kinds")]
pub struct Platform {
Expand Down Expand Up @@ -412,17 +447,8 @@ pub mod merge {

/// Merge a tree by specifying ours, base and theirs, writing it to the object database.
Tree {
/// 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>,
/// Print additional information about conflicts for debugging.
#[clap(long, short = 'd')]
debug: bool,
#[clap(flatten)]
opts: SharedOptions,

/// A revspec to our treeish.
#[clap(value_name = "OURS", value_parser = crate::shared::AsBString)]
Expand All @@ -436,17 +462,8 @@ pub mod merge {
},
/// 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>,
/// Print additional information about conflicts for debugging.
#[clap(long, short = 'd')]
debug: bool,
#[clap(flatten)]
opts: SharedOptions,

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

0 comments on commit a82f3f2

Please sign in to comment.