conflicts: add a method returning the resolved value, if resolved

This commit is contained in:
Martin von Zweigbergk 2023-06-13 01:32:46 -07:00 committed by Martin von Zweigbergk
parent 453d3a11fc
commit 9c486ebe45
2 changed files with 19 additions and 3 deletions

View file

@ -70,6 +70,16 @@ impl<T> Conflict<T> {
&self.adds
}
/// Returns the resolved value, if this conflict is resolved. Does not
/// resolve trivial conflicts.
pub fn as_resolved(&self) -> Option<&T> {
if let [value] = &self.adds[..] {
Some(value)
} else {
None
}
}
/// Simplify the conflict by joining diffs like A->B and B->C into A->C.
/// Also drops trivial diffs like A->A.
pub fn simplify(mut self) -> Self
@ -631,6 +641,13 @@ mod tests {
);
}
#[test]
fn test_as_resolved() {
assert_eq!(Conflict::new(vec![], vec![0]).as_resolved(), Some(&0));
// Even a trivially resolvable conflict is not resolved
assert_eq!(Conflict::new(vec![0], vec![0, 1]).as_resolved(), None);
}
#[test]
fn test_simplify() {
fn c(removes: &[u32], adds: &[u32]) -> Conflict<u32> {

View file

@ -615,9 +615,8 @@ fn merge_tree_value(
);
let filename = dir.join(basename);
let conflict = simplify_conflict(store, &filename, conflict)?;
if conflict.removes().is_empty() && conflict.adds().len() == 1 {
// A single add means that the current state is that state.
return Ok(conflict.adds()[0].clone());
if let Some(value) = conflict.as_resolved() {
return Ok(value.clone());
}
if let Some(tree_value) = try_resolve_file_conflict(store, &filename, &conflict)? {
Some(tree_value)