mirror of
https://github.com/martinvonz/jj.git
synced 2025-05-29 19:11:11 +00:00
conflicts: add try_map()
method
This simplifies `to_file_conflict()` and `try_resolve_file_conflict()` a bit.
This commit is contained in:
parent
b8f6a48c66
commit
b8221d4e21
@ -114,6 +114,14 @@ impl<T> Conflict<T> {
|
|||||||
{
|
{
|
||||||
trivial_merge(&self.removes, &self.adds)
|
trivial_merge(&self.removes, &self.adds)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new conflict by applying `f` to each remove and add, returning
|
||||||
|
/// `None if `f` returns `None` for any of them.
|
||||||
|
pub fn try_map<'a, U>(&'a self, mut f: impl FnMut(&'a T) -> Option<U>) -> Option<Conflict<U>> {
|
||||||
|
let removes = self.removes.iter().map(&mut f).collect::<Option<_>>()?;
|
||||||
|
let adds = self.adds.iter().map(&mut f).collect::<Option<_>>()?;
|
||||||
|
Some(Conflict { removes, adds })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Conflict<Option<T>> {
|
impl<T> Conflict<Option<T>> {
|
||||||
@ -174,29 +182,14 @@ impl Conflict<Option<TreeValue>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_file_conflict(&self) -> Option<Conflict<Option<FileId>>> {
|
pub fn to_file_conflict(&self) -> Option<Conflict<Option<FileId>>> {
|
||||||
fn collect_file_terms(terms: &[Option<TreeValue>]) -> Option<Vec<Option<FileId>>> {
|
self.try_map(|term| match term {
|
||||||
let mut file_terms = vec![];
|
None => Some(None),
|
||||||
for term in terms {
|
Some(TreeValue::File {
|
||||||
match term {
|
id,
|
||||||
None => {
|
executable: false,
|
||||||
file_terms.push(None);
|
}) => Some(Some(id.clone())),
|
||||||
}
|
_ => None,
|
||||||
Some(TreeValue::File {
|
})
|
||||||
id,
|
|
||||||
executable: false,
|
|
||||||
}) => {
|
|
||||||
file_terms.push(Some(id.clone()));
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(file_terms)
|
|
||||||
}
|
|
||||||
let file_removes = collect_file_terms(self.removes())?;
|
|
||||||
let file_adds = collect_file_terms(self.adds())?;
|
|
||||||
Some(Conflict::new(file_removes, file_adds))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Give a summary description of the conflict's "removes" and "adds"
|
/// Give a summary description of the conflict's "removes" and "adds"
|
||||||
@ -752,4 +745,25 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_try_map() {
|
||||||
|
fn sqrt(i: &i32) -> Option<i32> {
|
||||||
|
if *i >= 0 {
|
||||||
|
Some((*i as f64).sqrt() as i32)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn c(removes: &[i32], adds: &[i32]) -> Conflict<i32> {
|
||||||
|
Conflict::new(removes.to_vec(), adds.to_vec())
|
||||||
|
}
|
||||||
|
// 1-way conflict
|
||||||
|
assert_eq!(c(&[], &[1]).try_map(sqrt), Some(c(&[], &[1])));
|
||||||
|
assert_eq!(c(&[], &[-1]).try_map(sqrt), None);
|
||||||
|
// 3-way conflict
|
||||||
|
assert_eq!(c(&[1], &[4, 9]).try_map(sqrt), Some(c(&[1], &[2, 3])));
|
||||||
|
assert_eq!(c(&[-1], &[4, 9]).try_map(sqrt), None);
|
||||||
|
assert_eq!(c(&[1], &[-4, 9]).try_map(sqrt), None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -637,40 +637,31 @@ fn try_resolve_file_conflict(
|
|||||||
// merge it. We check early so we don't waste time reading file contents if
|
// merge it. We check early so we don't waste time reading file contents if
|
||||||
// we can't merge them anyway. At the same time we determine whether the
|
// we can't merge them anyway. At the same time we determine whether the
|
||||||
// resulting file should be executable.
|
// resulting file should be executable.
|
||||||
let mut executable_removes = vec![];
|
// TODO: Change to let-else once our MSRV is above 1.65
|
||||||
let mut executable_adds = vec![];
|
let file_id_conflict = if let Some(conflict) = conflict.try_map(|term| match term {
|
||||||
let mut removed_file_ids = vec![];
|
Some(TreeValue::File { id, executable: _ }) => Some(id),
|
||||||
let mut added_file_ids = vec![];
|
_ => None,
|
||||||
for term in conflict.removes() {
|
}) {
|
||||||
match term {
|
conflict
|
||||||
Some(TreeValue::File { id, executable }) => {
|
} else {
|
||||||
executable_removes.push(*executable);
|
return Ok(None);
|
||||||
removed_file_ids.push(id.clone());
|
};
|
||||||
}
|
// TODO: Change to let-else once our MSRV is above 1.65
|
||||||
_ => {
|
let executable_conflict = if let Some(conflict) = conflict.try_map(|term| match term {
|
||||||
return Ok(None);
|
Some(TreeValue::File { id: _, executable }) => Some(executable),
|
||||||
}
|
_ => None,
|
||||||
}
|
}) {
|
||||||
}
|
conflict
|
||||||
for term in conflict.adds() {
|
} else {
|
||||||
match term {
|
return Ok(None);
|
||||||
Some(TreeValue::File { id, executable }) => {
|
};
|
||||||
executable_adds.push(*executable);
|
let executable = if let Some(&executable) = executable_conflict.resolve_trivial() {
|
||||||
added_file_ids.push(id.clone());
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let executable = if let Some(executable) = trivial_merge(&executable_removes, &executable_adds)
|
|
||||||
{
|
|
||||||
*executable
|
*executable
|
||||||
} else {
|
} else {
|
||||||
// We're unable to determine whether the result should be executable
|
// We're unable to determine whether the result should be executable
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
if let Some(resolved_file_id) = trivial_merge(&removed_file_ids, &added_file_ids) {
|
if let Some(&resolved_file_id) = file_id_conflict.resolve_trivial() {
|
||||||
// Don't bother reading the file contents if the conflict can be trivially
|
// Don't bother reading the file contents if the conflict can be trivially
|
||||||
// resolved.
|
// resolved.
|
||||||
return Ok(Some(TreeValue::File {
|
return Ok(Some(TreeValue::File {
|
||||||
@ -680,25 +671,25 @@ fn try_resolve_file_conflict(
|
|||||||
}
|
}
|
||||||
let mut removed_contents = vec![];
|
let mut removed_contents = vec![];
|
||||||
let mut added_contents = vec![];
|
let mut added_contents = vec![];
|
||||||
for file_id in removed_file_ids {
|
for &file_id in file_id_conflict.removes() {
|
||||||
let mut content = vec![];
|
let mut content = vec![];
|
||||||
store
|
store
|
||||||
.read_file(filename, &file_id)?
|
.read_file(filename, file_id)?
|
||||||
.read_to_end(&mut content)
|
.read_to_end(&mut content)
|
||||||
.map_err(|err| TreeMergeError::ReadError {
|
.map_err(|err| TreeMergeError::ReadError {
|
||||||
source: err,
|
source: err,
|
||||||
file_id,
|
file_id: file_id.clone(),
|
||||||
})?;
|
})?;
|
||||||
removed_contents.push(content);
|
removed_contents.push(content);
|
||||||
}
|
}
|
||||||
for file_id in added_file_ids {
|
for &file_id in file_id_conflict.adds() {
|
||||||
let mut content = vec![];
|
let mut content = vec![];
|
||||||
store
|
store
|
||||||
.read_file(filename, &file_id)?
|
.read_file(filename, file_id)?
|
||||||
.read_to_end(&mut content)
|
.read_to_end(&mut content)
|
||||||
.map_err(|err| TreeMergeError::ReadError {
|
.map_err(|err| TreeMergeError::ReadError {
|
||||||
source: err,
|
source: err,
|
||||||
file_id,
|
file_id: file_id.clone(),
|
||||||
})?;
|
})?;
|
||||||
added_contents.push(content);
|
added_contents.push(content);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user