mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-27 14:47:05 +00:00
merge: move non-consuming Merge<Option<TreeValue>> methods to generic type
The next patch will add .is_tree() callers, and the other methods don't required owned type.
This commit is contained in:
parent
8268af9b4f
commit
2977900482
1 changed files with 52 additions and 43 deletions
|
@ -539,13 +539,21 @@ impl MergedTreeValue {
|
||||||
.collect();
|
.collect();
|
||||||
backend::Conflict { removes, adds }
|
backend::Conflict { removes, adds }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Merge<Option<T>>
|
||||||
|
where
|
||||||
|
T: Borrow<TreeValue>,
|
||||||
|
{
|
||||||
/// Whether this merge should be recursed into when doing directory walks.
|
/// Whether this merge should be recursed into when doing directory walks.
|
||||||
pub fn is_tree(&self) -> bool {
|
pub fn is_tree(&self) -> bool {
|
||||||
self.is_present()
|
self.is_present()
|
||||||
&& self
|
&& self.iter().all(|value| {
|
||||||
.iter()
|
matches!(
|
||||||
.all(|value| matches!(value, Some(TreeValue::Tree(_)) | None))
|
borrow_tree_value(value.as_ref()),
|
||||||
|
Some(TreeValue::Tree(_)) | None
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If this merge contains only files or absent entries, returns a merge of
|
/// If this merge contains only files or absent entries, returns a merge of
|
||||||
|
@ -553,7 +561,7 @@ impl MergedTreeValue {
|
||||||
/// `Merge::with_new_file_ids()` to produce a new merge with the original
|
/// `Merge::with_new_file_ids()` to produce a new merge with the original
|
||||||
/// executable bits preserved.
|
/// executable bits preserved.
|
||||||
pub fn to_file_merge(&self) -> Option<Merge<Option<FileId>>> {
|
pub fn to_file_merge(&self) -> Option<Merge<Option<FileId>>> {
|
||||||
self.maybe_map(|term| match term {
|
self.maybe_map(|term| match borrow_tree_value(term.as_ref()) {
|
||||||
None => Some(None),
|
None => Some(None),
|
||||||
Some(TreeValue::File { id, executable: _ }) => Some(Some(id.clone())),
|
Some(TreeValue::File { id, executable: _ }) => Some(Some(id.clone())),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
@ -563,51 +571,13 @@ impl MergedTreeValue {
|
||||||
/// If this merge contains only files or absent entries, returns a merge of
|
/// If this merge contains only files or absent entries, returns a merge of
|
||||||
/// the files' executable bits.
|
/// the files' executable bits.
|
||||||
pub fn to_executable_merge(&self) -> Option<Merge<bool>> {
|
pub fn to_executable_merge(&self) -> Option<Merge<bool>> {
|
||||||
self.maybe_map(|term| match term {
|
self.maybe_map(|term| match borrow_tree_value(term.as_ref()) {
|
||||||
None => Some(false),
|
None => Some(false),
|
||||||
Some(TreeValue::File { id: _, executable }) => Some(*executable),
|
Some(TreeValue::File { id: _, executable }) => Some(*executable),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new merge with the file ids from the given merge. In other
|
|
||||||
/// words, only the executable bits from `self` will be preserved.
|
|
||||||
pub fn with_new_file_ids(&self, file_ids: &Merge<Option<FileId>>) -> Self {
|
|
||||||
assert_eq!(self.values.len(), file_ids.values.len());
|
|
||||||
let values = zip(self.iter(), file_ids.iter())
|
|
||||||
.map(|(tree_value, file_id)| {
|
|
||||||
if let Some(TreeValue::File { id: _, executable }) = tree_value {
|
|
||||||
Some(TreeValue::File {
|
|
||||||
id: file_id.as_ref().unwrap().clone(),
|
|
||||||
executable: *executable,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
assert!(tree_value.is_none());
|
|
||||||
assert!(file_id.is_none());
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
Merge { values }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Give a summary description of the conflict's "removes" and "adds"
|
|
||||||
pub fn describe(&self, file: &mut dyn Write) -> std::io::Result<()> {
|
|
||||||
file.write_all(b"Conflict:\n")?;
|
|
||||||
for term in self.removes().flatten() {
|
|
||||||
file.write_all(format!(" Removing {}\n", describe_conflict_term(term)).as_bytes())?;
|
|
||||||
}
|
|
||||||
for term in self.adds().flatten() {
|
|
||||||
file.write_all(format!(" Adding {}\n", describe_conflict_term(term)).as_bytes())?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Merge<Option<T>>
|
|
||||||
where
|
|
||||||
T: Borrow<TreeValue>,
|
|
||||||
{
|
|
||||||
/// If every non-`None` term of a `MergedTreeValue`
|
/// If every non-`None` term of a `MergedTreeValue`
|
||||||
/// is a `TreeValue::Tree`, this converts it to
|
/// is a `TreeValue::Tree`, this converts it to
|
||||||
/// a `Merge<Tree>`, with empty trees instead of
|
/// a `Merge<Tree>`, with empty trees instead of
|
||||||
|
@ -635,6 +605,45 @@ where
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new merge with the file ids from the given merge. In other
|
||||||
|
/// words, only the executable bits from `self` will be preserved.
|
||||||
|
pub fn with_new_file_ids(&self, file_ids: &Merge<Option<FileId>>) -> Merge<Option<TreeValue>> {
|
||||||
|
assert_eq!(self.values.len(), file_ids.values.len());
|
||||||
|
let values = zip(self.iter(), file_ids.iter())
|
||||||
|
.map(|(tree_value, file_id)| {
|
||||||
|
if let Some(TreeValue::File { id: _, executable }) =
|
||||||
|
borrow_tree_value(tree_value.as_ref())
|
||||||
|
{
|
||||||
|
Some(TreeValue::File {
|
||||||
|
id: file_id.as_ref().unwrap().clone(),
|
||||||
|
executable: *executable,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
assert!(tree_value.is_none());
|
||||||
|
assert!(file_id.is_none());
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
Merge { values }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Give a summary description of the conflict's "removes" and "adds"
|
||||||
|
pub fn describe(&self, file: &mut dyn Write) -> std::io::Result<()> {
|
||||||
|
file.write_all(b"Conflict:\n")?;
|
||||||
|
for term in self.removes().flatten() {
|
||||||
|
file.write_all(
|
||||||
|
format!(" Removing {}\n", describe_conflict_term(term.borrow())).as_bytes(),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
for term in self.adds().flatten() {
|
||||||
|
file.write_all(
|
||||||
|
format!(" Adding {}\n", describe_conflict_term(term.borrow())).as_bytes(),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn borrow_tree_value<T: Borrow<TreeValue> + ?Sized>(term: Option<&T>) -> Option<&TreeValue> {
|
fn borrow_tree_value<T: Borrow<TreeValue> + ?Sized>(term: Option<&T>) -> Option<&TreeValue> {
|
||||||
|
|
Loading…
Reference in a new issue