From 9c486ebe450addedfadc5810c526dffac915c74b Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Tue, 13 Jun 2023 01:32:46 -0700 Subject: [PATCH] conflicts: add a method returning the resolved value, if resolved --- lib/src/conflicts.rs | 17 +++++++++++++++++ lib/src/tree.rs | 5 ++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/src/conflicts.rs b/lib/src/conflicts.rs index f7b082b26..a34c5b315 100644 --- a/lib/src/conflicts.rs +++ b/lib/src/conflicts.rs @@ -70,6 +70,16 @@ impl Conflict { &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 { diff --git a/lib/src/tree.rs b/lib/src/tree.rs index 802e7530e..311dda62a 100644 --- a/lib/src/tree.rs +++ b/lib/src/tree.rs @@ -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)