2020-12-12 08:00:42 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-06-05 20:57:11 +00:00
|
|
|
use std::cmp::Ordering;
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::fmt::{Debug, Error, Formatter};
|
2021-06-05 20:57:11 +00:00
|
|
|
use std::io::Read;
|
|
|
|
use std::iter::Peekable;
|
2021-03-14 17:37:28 +00:00
|
|
|
use std::pin::Pin;
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
use crate::backend::{
|
|
|
|
BackendError, Conflict, ConflictId, ConflictPart, TreeEntriesNonRecursiveIter, TreeEntry,
|
|
|
|
TreeId, TreeValue,
|
|
|
|
};
|
2021-06-05 20:57:11 +00:00
|
|
|
use crate::files::MergeResult;
|
2021-06-05 21:29:40 +00:00
|
|
|
use crate::matchers::{EverythingMatcher, Matcher};
|
2021-05-19 16:41:25 +00:00
|
|
|
use crate::repo_path::{RepoPath, RepoPathComponent, RepoPathJoin};
|
2021-09-12 06:52:38 +00:00
|
|
|
use crate::store::Store;
|
|
|
|
use crate::{backend, files};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Tree {
|
2021-09-12 06:52:38 +00:00
|
|
|
store: Arc<Store>,
|
2021-05-19 16:41:25 +00:00
|
|
|
dir: RepoPath,
|
2020-12-12 08:00:42 +00:00
|
|
|
id: TreeId,
|
2021-09-12 06:52:38 +00:00
|
|
|
data: Arc<backend::Tree>,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for Tree {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
|
|
|
|
f.debug_struct("Tree")
|
|
|
|
.field("dir", &self.dir)
|
|
|
|
.field("id", &self.id)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub struct DiffSummary {
|
2021-05-17 04:55:51 +00:00
|
|
|
pub modified: Vec<RepoPath>,
|
|
|
|
pub added: Vec<RepoPath>,
|
|
|
|
pub removed: Vec<RepoPath>,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 05:08:12 +00:00
|
|
|
impl DiffSummary {
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.modified.is_empty() && self.added.is_empty() && self.removed.is_empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
impl Tree {
|
2021-09-12 06:52:38 +00:00
|
|
|
pub fn new(store: Arc<Store>, dir: RepoPath, id: TreeId, data: Arc<backend::Tree>) -> Self {
|
2020-12-12 08:00:42 +00:00
|
|
|
Tree {
|
|
|
|
store,
|
|
|
|
dir,
|
|
|
|
id,
|
|
|
|
data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
pub fn null(store: Arc<Store>, dir: RepoPath) -> Self {
|
2020-12-12 08:00:42 +00:00
|
|
|
Tree {
|
|
|
|
store,
|
|
|
|
dir,
|
|
|
|
id: TreeId(vec![]),
|
2021-09-12 06:52:38 +00:00
|
|
|
data: Arc::new(backend::Tree::default()),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
pub fn store(&self) -> &Arc<Store> {
|
2020-12-12 08:00:42 +00:00
|
|
|
&self.store
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:41:25 +00:00
|
|
|
pub fn dir(&self) -> &RepoPath {
|
2020-12-12 08:00:42 +00:00
|
|
|
&self.dir
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn id(&self) -> &TreeId {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
pub fn data(&self) -> &backend::Tree {
|
2020-12-12 08:00:42 +00:00
|
|
|
&self.data
|
|
|
|
}
|
|
|
|
|
2020-12-20 08:26:06 +00:00
|
|
|
pub fn entries_non_recursive(&self) -> TreeEntriesNonRecursiveIter {
|
2020-12-12 08:00:42 +00:00
|
|
|
self.data.entries()
|
|
|
|
}
|
|
|
|
|
2020-12-20 08:26:06 +00:00
|
|
|
pub fn entries(&self) -> TreeEntriesIter {
|
|
|
|
TreeEntriesIter::new(self.clone())
|
2020-12-20 08:07:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 05:50:14 +00:00
|
|
|
pub fn entry(&self, basename: &RepoPathComponent) -> Option<TreeEntry> {
|
2020-12-12 08:00:42 +00:00
|
|
|
self.data.entry(basename)
|
|
|
|
}
|
|
|
|
|
2021-06-06 05:50:14 +00:00
|
|
|
pub fn value(&self, basename: &RepoPathComponent) -> Option<&TreeValue> {
|
2020-12-12 08:00:42 +00:00
|
|
|
self.data.value(basename)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path_value(&self, path: &RepoPath) -> Option<TreeValue> {
|
2021-05-19 16:41:25 +00:00
|
|
|
assert_eq!(self.dir(), &RepoPath::root());
|
2020-12-12 08:00:42 +00:00
|
|
|
match path.split() {
|
|
|
|
Some((dir, basename)) => self
|
|
|
|
.sub_tree_recursive(dir.components())
|
2021-06-06 05:50:14 +00:00
|
|
|
.and_then(|tree| tree.data.value(basename).cloned()),
|
2020-12-12 08:00:42 +00:00
|
|
|
None => Some(TreeValue::Tree(self.id.clone())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:41:25 +00:00
|
|
|
pub fn sub_tree(&self, name: &RepoPathComponent) -> Option<Tree> {
|
2021-06-06 05:50:14 +00:00
|
|
|
self.data.value(name).and_then(|sub_tree| match sub_tree {
|
|
|
|
TreeValue::Tree(sub_tree_id) => {
|
|
|
|
let subdir = self.dir.join(name);
|
|
|
|
Some(self.store.get_tree(&subdir, sub_tree_id).unwrap())
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:41:25 +00:00
|
|
|
pub fn known_sub_tree(&self, name: &RepoPathComponent, id: &TreeId) -> Tree {
|
2020-12-12 08:00:42 +00:00
|
|
|
let subdir = self.dir.join(name);
|
|
|
|
self.store.get_tree(&subdir, id).unwrap()
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:41:25 +00:00
|
|
|
fn sub_tree_recursive(&self, components: &[RepoPathComponent]) -> Option<Tree> {
|
2020-12-12 08:00:42 +00:00
|
|
|
if components.is_empty() {
|
|
|
|
// TODO: It would be nice to be able to return a reference here, but
|
|
|
|
// then we would have to figure out how to share Tree instances
|
|
|
|
// across threads.
|
|
|
|
Some(Tree {
|
|
|
|
store: self.store.clone(),
|
|
|
|
dir: self.dir.clone(),
|
|
|
|
id: self.id.clone(),
|
|
|
|
data: self.data.clone(),
|
|
|
|
})
|
|
|
|
} else {
|
2021-06-06 05:50:14 +00:00
|
|
|
match self.data.entry(&components[0]) {
|
2020-12-12 08:00:42 +00:00
|
|
|
None => None,
|
|
|
|
Some(entry) => match entry.value() {
|
|
|
|
TreeValue::Tree(sub_tree_id) => {
|
2021-06-06 05:50:14 +00:00
|
|
|
let sub_tree = self.known_sub_tree(entry.name(), sub_tree_id);
|
2020-12-12 08:00:42 +00:00
|
|
|
sub_tree.sub_tree_recursive(&components[1..])
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
pub fn diff<'matcher>(
|
|
|
|
&self,
|
|
|
|
other: &Tree,
|
|
|
|
matcher: &'matcher dyn Matcher,
|
|
|
|
) -> TreeDiffIterator<'matcher> {
|
|
|
|
recursive_tree_diff(self.clone(), other.clone(), matcher)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
pub fn diff_summary(&self, other: &Tree, matcher: &dyn Matcher) -> DiffSummary {
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut modified = vec![];
|
|
|
|
let mut added = vec![];
|
|
|
|
let mut removed = vec![];
|
2021-06-05 21:29:40 +00:00
|
|
|
for (file, diff) in self.diff(other, matcher) {
|
2021-04-07 18:27:40 +00:00
|
|
|
match diff {
|
|
|
|
Diff::Modified(_, _) => modified.push(file.clone()),
|
|
|
|
Diff::Added(_) => added.push(file.clone()),
|
|
|
|
Diff::Removed(_) => removed.push(file.clone()),
|
|
|
|
}
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
modified.sort();
|
|
|
|
added.sort();
|
|
|
|
removed.sort();
|
|
|
|
DiffSummary {
|
|
|
|
modified,
|
|
|
|
added,
|
|
|
|
removed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_conflict(&self) -> bool {
|
|
|
|
!self.conflicts().is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn conflicts(&self) -> Vec<(RepoPath, ConflictId)> {
|
|
|
|
let mut conflicts = vec![];
|
2020-12-20 08:26:06 +00:00
|
|
|
for (name, value) in self.entries() {
|
2020-12-12 08:00:42 +00:00
|
|
|
if let TreeValue::Conflict(id) = value {
|
|
|
|
conflicts.push((name.clone(), id.clone()));
|
|
|
|
}
|
2020-12-20 08:07:51 +00:00
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
conflicts
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 08:07:51 +00:00
|
|
|
|
2020-12-20 08:26:06 +00:00
|
|
|
pub struct TreeEntriesIter {
|
|
|
|
stack: Vec<(Pin<Box<Tree>>, TreeEntriesNonRecursiveIter<'static>)>,
|
2020-12-20 08:07:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 08:26:06 +00:00
|
|
|
impl TreeEntriesIter {
|
2020-12-20 08:07:51 +00:00
|
|
|
fn new(tree: Tree) -> Self {
|
|
|
|
let tree = Box::pin(tree);
|
2020-12-20 08:26:06 +00:00
|
|
|
let iter = tree.entries_non_recursive();
|
|
|
|
let iter: TreeEntriesNonRecursiveIter<'static> = unsafe { std::mem::transmute(iter) };
|
2020-12-20 08:07:51 +00:00
|
|
|
Self {
|
|
|
|
stack: vec![(tree, iter)],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 08:26:06 +00:00
|
|
|
impl Iterator for TreeEntriesIter {
|
2020-12-20 08:07:51 +00:00
|
|
|
type Item = (RepoPath, TreeValue);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
while !self.stack.is_empty() {
|
|
|
|
let (tree, iter) = self.stack.last_mut().unwrap();
|
|
|
|
match iter.next() {
|
|
|
|
None => {
|
|
|
|
// No more entries in this directory
|
|
|
|
self.stack.pop().unwrap();
|
|
|
|
}
|
|
|
|
Some(entry) => {
|
|
|
|
match entry.value() {
|
|
|
|
TreeValue::Tree(id) => {
|
2021-06-06 05:50:14 +00:00
|
|
|
let subtree = tree.known_sub_tree(entry.name(), id);
|
2020-12-20 08:07:51 +00:00
|
|
|
let subtree = Box::pin(subtree);
|
2020-12-20 08:26:06 +00:00
|
|
|
let iter = subtree.entries_non_recursive();
|
|
|
|
let subtree_iter: TreeEntriesNonRecursiveIter<'static> =
|
2020-12-20 08:07:51 +00:00
|
|
|
unsafe { std::mem::transmute(iter) };
|
|
|
|
self.stack.push((subtree, subtree_iter));
|
|
|
|
}
|
|
|
|
other => {
|
2021-06-06 05:50:14 +00:00
|
|
|
let path = tree.dir().join(entry.name());
|
2020-12-20 08:07:51 +00:00
|
|
|
return Some((path, other.clone()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2021-06-05 20:57:11 +00:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum Diff<T> {
|
|
|
|
Modified(T, T),
|
|
|
|
Added(T),
|
|
|
|
Removed(T),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Diff<T> {
|
|
|
|
pub fn as_options(&self) -> (Option<&T>, Option<&T>) {
|
|
|
|
match self {
|
|
|
|
Diff::Modified(left, right) => (Some(left), Some(right)),
|
|
|
|
Diff::Added(right) => (None, Some(right)),
|
|
|
|
Diff::Removed(left) => (Some(left), None),
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 20:28:00 +00:00
|
|
|
|
|
|
|
pub fn into_options(self) -> (Option<T>, Option<T>) {
|
|
|
|
match self {
|
|
|
|
Diff::Modified(left, right) => (Some(left), Some(right)),
|
|
|
|
Diff::Added(right) => (None, Some(right)),
|
|
|
|
Diff::Removed(left) => (Some(left), None),
|
|
|
|
}
|
|
|
|
}
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
struct TreeEntryDiffIterator<'trees, 'matcher> {
|
|
|
|
it1: Peekable<TreeEntriesNonRecursiveIter<'trees>>,
|
|
|
|
it2: Peekable<TreeEntriesNonRecursiveIter<'trees>>,
|
|
|
|
// TODO: Restrict walk according to Matcher::visit()
|
|
|
|
_matcher: &'matcher dyn Matcher,
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
impl<'trees, 'matcher> TreeEntryDiffIterator<'trees, 'matcher> {
|
|
|
|
fn new(tree1: &'trees Tree, tree2: &'trees Tree, matcher: &'matcher dyn Matcher) -> Self {
|
2021-06-05 20:57:11 +00:00
|
|
|
let it1 = tree1.entries_non_recursive().peekable();
|
|
|
|
let it2 = tree2.entries_non_recursive().peekable();
|
2021-06-05 21:29:40 +00:00
|
|
|
TreeEntryDiffIterator {
|
|
|
|
it1,
|
|
|
|
it2,
|
|
|
|
_matcher: matcher,
|
|
|
|
}
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
impl<'trees, 'matcher> Iterator for TreeEntryDiffIterator<'trees, 'matcher> {
|
2021-06-06 05:55:10 +00:00
|
|
|
type Item = (
|
|
|
|
RepoPathComponent,
|
2021-06-05 21:29:40 +00:00
|
|
|
Option<&'trees TreeValue>,
|
|
|
|
Option<&'trees TreeValue>,
|
2021-06-06 05:55:10 +00:00
|
|
|
);
|
2021-06-05 20:57:11 +00:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
loop {
|
|
|
|
let entry1 = self.it1.peek();
|
|
|
|
let entry2 = self.it2.peek();
|
|
|
|
match (&entry1, &entry2) {
|
|
|
|
(Some(before), Some(after)) => {
|
|
|
|
match before.name().cmp(after.name()) {
|
|
|
|
Ordering::Less => {
|
|
|
|
// entry removed
|
|
|
|
let before = self.it1.next().unwrap();
|
2021-06-06 05:50:14 +00:00
|
|
|
return Some((before.name().clone(), Some(before.value()), None));
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
Ordering::Greater => {
|
|
|
|
// entry added
|
|
|
|
let after = self.it2.next().unwrap();
|
2021-06-06 05:50:14 +00:00
|
|
|
return Some((after.name().clone(), None, Some(after.value())));
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
Ordering::Equal => {
|
|
|
|
// entry modified or clean
|
|
|
|
let before = self.it1.next().unwrap();
|
|
|
|
let after = self.it2.next().unwrap();
|
|
|
|
if before.value() != after.value() {
|
|
|
|
return Some((
|
2021-06-06 05:50:14 +00:00
|
|
|
before.name().clone(),
|
2021-06-05 20:57:11 +00:00
|
|
|
Some(before.value()),
|
|
|
|
Some(after.value()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Some(_), None) => {
|
|
|
|
// second iterator exhausted
|
|
|
|
let before = self.it1.next().unwrap();
|
2021-06-06 05:50:14 +00:00
|
|
|
return Some((before.name().clone(), Some(before.value()), None));
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
(None, Some(_)) => {
|
|
|
|
// first iterator exhausted
|
|
|
|
let after = self.it2.next().unwrap();
|
2021-06-06 05:50:14 +00:00
|
|
|
return Some((after.name().clone(), None, Some(after.value())));
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
(None, None) => {
|
|
|
|
// both iterators exhausted
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
fn diff_entries<'trees, 'matcher>(
|
|
|
|
tree1: &'trees Tree,
|
|
|
|
tree2: &'trees Tree,
|
|
|
|
matcher: &'matcher dyn Matcher,
|
|
|
|
) -> TreeEntryDiffIterator<'trees, 'matcher> {
|
|
|
|
// TODO: make TreeEntryDiffIterator an enum with one variant that iterates over
|
|
|
|
// the tree entries and filters by the matcher (i.e. what
|
|
|
|
// TreeEntryDiffIterator does now) and another variant that iterates over
|
|
|
|
// what the matcher says to visit
|
|
|
|
TreeEntryDiffIterator::new(tree1, tree2, matcher)
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
pub fn recursive_tree_diff(root1: Tree, root2: Tree, matcher: &dyn Matcher) -> TreeDiffIterator {
|
|
|
|
TreeDiffIterator::new(RepoPath::root(), root1, root2, matcher)
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
pub struct TreeDiffIterator<'matcher> {
|
2021-06-05 20:57:11 +00:00
|
|
|
dir: RepoPath,
|
|
|
|
tree1: Pin<Box<Tree>>,
|
|
|
|
tree2: Pin<Box<Tree>>,
|
2021-06-05 21:29:40 +00:00
|
|
|
matcher: &'matcher dyn Matcher,
|
2021-06-05 20:57:11 +00:00
|
|
|
// Iterator over the diffs between tree1 and tree2
|
2021-06-05 21:29:40 +00:00
|
|
|
entry_iterator: TreeEntryDiffIterator<'static, 'matcher>,
|
2021-06-05 20:57:11 +00:00
|
|
|
// This is used for making sure that when a directory gets replaced by a file, we
|
|
|
|
// yield the value for the addition of the file after we yield the values
|
|
|
|
// for removing files in the directory.
|
|
|
|
added_file: Option<(RepoPath, TreeValue)>,
|
|
|
|
// Iterator over the diffs of a subdirectory, if we're currently visiting one.
|
2021-06-05 21:29:40 +00:00
|
|
|
subdir_iterator: Option<Box<TreeDiffIterator<'matcher>>>,
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
impl<'matcher> TreeDiffIterator<'matcher> {
|
|
|
|
fn new(
|
|
|
|
dir: RepoPath,
|
|
|
|
tree1: Tree,
|
|
|
|
tree2: Tree,
|
|
|
|
matcher: &'matcher dyn Matcher,
|
|
|
|
) -> TreeDiffIterator {
|
2021-06-05 20:57:11 +00:00
|
|
|
let tree1 = Box::pin(tree1);
|
|
|
|
let tree2 = Box::pin(tree2);
|
2021-06-05 21:29:40 +00:00
|
|
|
let root_entry_iterator: TreeEntryDiffIterator = diff_entries(&tree1, &tree2, matcher);
|
|
|
|
let root_entry_iterator: TreeEntryDiffIterator<'static, 'matcher> =
|
2021-06-05 20:57:11 +00:00
|
|
|
unsafe { std::mem::transmute(root_entry_iterator) };
|
|
|
|
Self {
|
|
|
|
dir,
|
|
|
|
tree1,
|
|
|
|
tree2,
|
2021-06-05 21:29:40 +00:00
|
|
|
matcher,
|
2021-06-05 20:57:11 +00:00
|
|
|
entry_iterator: root_entry_iterator,
|
|
|
|
added_file: None,
|
|
|
|
subdir_iterator: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
impl Iterator for TreeDiffIterator<'_> {
|
2021-06-05 20:57:11 +00:00
|
|
|
type Item = (RepoPath, Diff<TreeValue>);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
loop {
|
|
|
|
// First return results from any subdirectory we're currently visiting.
|
|
|
|
if let Some(subdir_iterator) = &mut self.subdir_iterator {
|
|
|
|
if let Some(element) = subdir_iterator.next() {
|
|
|
|
return Some(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((name, value)) = self.added_file.take() {
|
|
|
|
return Some((name, Diff::Added(value)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: whenever we say "file" below, it may also be a symlink or a conflict.
|
|
|
|
if let Some((name, before, after)) = self.entry_iterator.next() {
|
|
|
|
let tree_before = matches!(before, Some(TreeValue::Tree(_)));
|
|
|
|
let tree_after = matches!(after, Some(TreeValue::Tree(_)));
|
|
|
|
if tree_before || tree_after {
|
2021-06-05 21:29:40 +00:00
|
|
|
let subdir = &name;
|
|
|
|
let subdir_path = self.dir.join(subdir);
|
2021-06-05 20:57:11 +00:00
|
|
|
let before_tree = match before {
|
|
|
|
Some(TreeValue::Tree(id_before)) => {
|
2021-06-14 07:18:38 +00:00
|
|
|
self.tree1.known_sub_tree(subdir, id_before)
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
_ => Tree::null(self.tree1.store().clone(), subdir_path.clone()),
|
|
|
|
};
|
|
|
|
let after_tree = match after {
|
|
|
|
Some(TreeValue::Tree(id_after)) => {
|
2021-06-14 07:18:38 +00:00
|
|
|
self.tree2.known_sub_tree(subdir, id_after)
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
_ => Tree::null(self.tree2.store().clone(), subdir_path.clone()),
|
|
|
|
};
|
|
|
|
self.subdir_iterator = Some(Box::new(TreeDiffIterator::new(
|
|
|
|
subdir_path,
|
|
|
|
before_tree,
|
|
|
|
after_tree,
|
2021-06-05 21:29:40 +00:00
|
|
|
self.matcher,
|
2021-06-05 20:57:11 +00:00
|
|
|
)));
|
|
|
|
}
|
2021-06-05 21:29:40 +00:00
|
|
|
let file_path = self.dir.join(&name);
|
|
|
|
if self.matcher.matches(&file_path) {
|
|
|
|
if !tree_before && tree_after {
|
|
|
|
if let Some(file_before) = before {
|
2021-06-05 20:57:11 +00:00
|
|
|
return Some((file_path, Diff::Removed(file_before.clone())));
|
|
|
|
}
|
2021-06-05 21:29:40 +00:00
|
|
|
} else if tree_before && !tree_after {
|
|
|
|
if let Some(file_after) = after {
|
|
|
|
self.added_file = Some((file_path, file_after.clone()));
|
|
|
|
}
|
|
|
|
} else if !tree_before && !tree_after {
|
|
|
|
match (before, after) {
|
|
|
|
(Some(file_before), Some(file_after)) => {
|
|
|
|
return Some((
|
|
|
|
file_path,
|
|
|
|
Diff::Modified(file_before.clone(), file_after.clone()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
(None, Some(file_after)) => {
|
|
|
|
return Some((file_path, Diff::Added(file_after.clone())));
|
|
|
|
}
|
|
|
|
(Some(file_before), None) => {
|
|
|
|
return Some((file_path, Diff::Removed(file_before.clone())));
|
|
|
|
}
|
|
|
|
(None, None) => {
|
|
|
|
panic!("unexpected diff")
|
|
|
|
}
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn merge_trees(
|
|
|
|
side1_tree: &Tree,
|
|
|
|
base_tree: &Tree,
|
|
|
|
side2_tree: &Tree,
|
2021-09-12 06:52:38 +00:00
|
|
|
) -> Result<TreeId, BackendError> {
|
2021-09-16 15:19:17 +00:00
|
|
|
let store = base_tree.store();
|
2021-06-05 20:57:11 +00:00
|
|
|
let dir = base_tree.dir();
|
|
|
|
assert_eq!(side1_tree.dir(), dir);
|
|
|
|
assert_eq!(side2_tree.dir(), dir);
|
|
|
|
|
|
|
|
if base_tree.id() == side1_tree.id() {
|
|
|
|
return Ok(side2_tree.id().clone());
|
|
|
|
}
|
|
|
|
if base_tree.id() == side2_tree.id() || side1_tree.id() == side2_tree.id() {
|
|
|
|
return Ok(side1_tree.id().clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start with a tree identical to side 1 and modify based on changes from base
|
|
|
|
// to side 2.
|
|
|
|
let mut new_tree = side1_tree.data().clone();
|
2021-06-05 21:29:40 +00:00
|
|
|
for (basename, maybe_base, maybe_side2) in
|
|
|
|
diff_entries(base_tree, side2_tree, &EverythingMatcher)
|
|
|
|
{
|
2021-06-06 05:50:14 +00:00
|
|
|
let maybe_side1 = side1_tree.value(&basename);
|
2021-06-05 20:57:11 +00:00
|
|
|
if maybe_side1 == maybe_base {
|
|
|
|
// side 1 is unchanged: use the value from side 2
|
|
|
|
match maybe_side2 {
|
2021-06-06 05:50:14 +00:00
|
|
|
None => new_tree.remove(&basename),
|
|
|
|
Some(side2) => new_tree.set(basename, side2.clone()),
|
2021-06-05 20:57:11 +00:00
|
|
|
};
|
|
|
|
} else if maybe_side1 == maybe_side2 {
|
|
|
|
// Both sides changed in the same way: new_tree already has the
|
|
|
|
// value
|
|
|
|
} else {
|
|
|
|
// The two sides changed in different ways
|
2021-06-06 05:59:52 +00:00
|
|
|
let new_value =
|
|
|
|
merge_tree_value(store, dir, &basename, maybe_base, maybe_side1, maybe_side2)?;
|
2021-06-05 20:57:11 +00:00
|
|
|
match new_value {
|
2021-06-06 05:50:14 +00:00
|
|
|
None => new_tree.remove(&basename),
|
|
|
|
Some(value) => new_tree.set(basename, value),
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
store.write_tree(dir, &new_tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge_tree_value(
|
2021-09-16 15:19:17 +00:00
|
|
|
store: &Arc<Store>,
|
2021-06-05 20:57:11 +00:00
|
|
|
dir: &RepoPath,
|
2021-06-06 05:59:52 +00:00
|
|
|
basename: &RepoPathComponent,
|
2021-06-05 20:57:11 +00:00
|
|
|
maybe_base: Option<&TreeValue>,
|
|
|
|
maybe_side1: Option<&TreeValue>,
|
|
|
|
maybe_side2: Option<&TreeValue>,
|
2021-09-12 06:52:38 +00:00
|
|
|
) -> Result<Option<TreeValue>, BackendError> {
|
2021-06-05 20:57:11 +00:00
|
|
|
// Resolve non-trivial conflicts:
|
|
|
|
// * resolve tree conflicts by recursing
|
|
|
|
// * try to resolve file conflicts by merging the file contents
|
|
|
|
// * leave other conflicts (e.g. file/dir conflicts, remove/modify conflicts)
|
|
|
|
// unresolved
|
|
|
|
Ok(match (maybe_base, maybe_side1, maybe_side2) {
|
|
|
|
(
|
|
|
|
Some(TreeValue::Tree(base)),
|
|
|
|
Some(TreeValue::Tree(side1)),
|
|
|
|
Some(TreeValue::Tree(side2)),
|
|
|
|
) => {
|
2021-06-06 05:59:52 +00:00
|
|
|
let subdir = dir.join(basename);
|
2021-06-05 20:57:11 +00:00
|
|
|
let merged_tree_id = merge_trees(
|
2021-06-14 07:18:38 +00:00
|
|
|
&store.get_tree(&subdir, side1).unwrap(),
|
|
|
|
&store.get_tree(&subdir, base).unwrap(),
|
|
|
|
&store.get_tree(&subdir, side2).unwrap(),
|
2021-06-05 20:57:11 +00:00
|
|
|
)?;
|
|
|
|
if &merged_tree_id == store.empty_tree_id() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(TreeValue::Tree(merged_tree_id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let maybe_merged = match (maybe_base, maybe_side1, maybe_side2) {
|
|
|
|
(
|
|
|
|
Some(TreeValue::Normal {
|
|
|
|
id: base_id,
|
|
|
|
executable: base_executable,
|
|
|
|
}),
|
|
|
|
Some(TreeValue::Normal {
|
|
|
|
id: side1_id,
|
|
|
|
executable: side1_executable,
|
|
|
|
}),
|
|
|
|
Some(TreeValue::Normal {
|
|
|
|
id: side2_id,
|
|
|
|
executable: side2_executable,
|
|
|
|
}),
|
|
|
|
) => {
|
|
|
|
let executable = if base_executable == side1_executable {
|
|
|
|
*side2_executable
|
|
|
|
} else if base_executable == side2_executable {
|
|
|
|
*side1_executable
|
|
|
|
} else {
|
|
|
|
assert_eq!(side1_executable, side2_executable);
|
|
|
|
*side1_executable
|
|
|
|
};
|
|
|
|
|
2021-06-14 07:18:38 +00:00
|
|
|
let filename = dir.join(basename);
|
2021-06-05 20:57:11 +00:00
|
|
|
let mut base_content = vec![];
|
|
|
|
store
|
2021-06-14 07:18:38 +00:00
|
|
|
.read_file(&filename, base_id)?
|
2021-06-05 20:57:11 +00:00
|
|
|
.read_to_end(&mut base_content)?;
|
|
|
|
let mut side1_content = vec![];
|
|
|
|
store
|
2021-06-14 07:18:38 +00:00
|
|
|
.read_file(&filename, side1_id)?
|
2021-06-05 20:57:11 +00:00
|
|
|
.read_to_end(&mut side1_content)?;
|
|
|
|
let mut side2_content = vec![];
|
|
|
|
store
|
2021-06-14 07:18:38 +00:00
|
|
|
.read_file(&filename, side2_id)?
|
2021-06-05 20:57:11 +00:00
|
|
|
.read_to_end(&mut side2_content)?;
|
|
|
|
|
2021-06-30 16:21:23 +00:00
|
|
|
let merge_result =
|
|
|
|
files::merge(&[&base_content], &[&side1_content, &side2_content]);
|
2021-06-05 20:57:11 +00:00
|
|
|
match merge_result {
|
|
|
|
MergeResult::Resolved(merged_content) => {
|
|
|
|
let id = store.write_file(&filename, &mut merged_content.as_slice())?;
|
|
|
|
Some(TreeValue::Normal { id, executable })
|
|
|
|
}
|
|
|
|
MergeResult::Conflict(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
match maybe_merged {
|
|
|
|
Some(merged) => Some(merged),
|
|
|
|
None => {
|
|
|
|
let mut conflict = Conflict::default();
|
|
|
|
if let Some(base) = maybe_base {
|
|
|
|
conflict.removes.push(ConflictPart {
|
|
|
|
value: base.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if let Some(side1) = maybe_side1 {
|
|
|
|
conflict.adds.push(ConflictPart {
|
|
|
|
value: side1.clone(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if let Some(side2) = maybe_side2 {
|
|
|
|
conflict.adds.push(ConflictPart {
|
|
|
|
value: side2.clone(),
|
|
|
|
});
|
|
|
|
}
|
2021-10-21 05:00:52 +00:00
|
|
|
let conflict = simplify_conflict(store, &conflict)?;
|
|
|
|
if conflict.adds.is_empty() {
|
|
|
|
// If there are no values to add, then the path doesn't exist
|
|
|
|
None
|
|
|
|
} else if conflict.removes.is_empty() && conflict.adds.len() == 1 {
|
|
|
|
// A single add means that the current state is that state.
|
|
|
|
Some(conflict.adds[0].value.clone())
|
|
|
|
} else {
|
|
|
|
let conflict_id = store.write_conflict(&conflict)?;
|
|
|
|
Some(TreeValue::Conflict(conflict_id))
|
|
|
|
}
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
fn conflict_part_to_conflict(store: &Store, part: &ConflictPart) -> Result<Conflict, BackendError> {
|
2021-06-05 20:57:11 +00:00
|
|
|
match &part.value {
|
|
|
|
TreeValue::Conflict(id) => {
|
|
|
|
let conflict = store.read_conflict(id)?;
|
|
|
|
Ok(conflict)
|
|
|
|
}
|
|
|
|
other => Ok(Conflict {
|
|
|
|
removes: vec![],
|
|
|
|
adds: vec![ConflictPart {
|
|
|
|
value: other.clone(),
|
|
|
|
}],
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn simplify_conflict(
|
2021-09-12 06:52:38 +00:00
|
|
|
store: &Store,
|
2021-06-05 20:57:11 +00:00
|
|
|
conflict: &Conflict,
|
2021-10-21 05:00:52 +00:00
|
|
|
) -> Result<Conflict, BackendError> {
|
2021-06-05 20:57:11 +00:00
|
|
|
// Important cases to simplify:
|
|
|
|
//
|
|
|
|
// D
|
|
|
|
// |
|
|
|
|
// B C
|
|
|
|
// |/
|
|
|
|
// A
|
|
|
|
//
|
|
|
|
// 1. rebase C to B, then back to A => there should be no conflict
|
|
|
|
// 2. rebase C to B, then to D => the conflict should not mention B
|
|
|
|
// 3. rebase B to C and D to B', then resolve the conflict in B' and rebase D'
|
|
|
|
// on top => the conflict should be between B'', B, and D; it should not
|
|
|
|
// mention the conflict in B'
|
|
|
|
|
|
|
|
// Case 1 above:
|
|
|
|
// After first rebase, the conflict is {+B-A+C}. After rebasing back,
|
|
|
|
// the unsimplified conflict is {+A-B+{+B-A+C}}. Since the
|
|
|
|
// inner conflict is positive, we can simply move it into the outer conflict. We
|
|
|
|
// thus get {+A-B+B-A+C}, which we can then simplify to just C (because {+C} ==
|
|
|
|
// C).
|
|
|
|
//
|
|
|
|
// Case 2 above:
|
|
|
|
// After first rebase, the conflict is {+B-A+C}. After rebasing to D,
|
|
|
|
// the unsimplified conflict is {+D-C+{+B-A+C}}. As in the
|
|
|
|
// previous case, the inner conflict can be moved into the outer one. We then
|
|
|
|
// get {+D-C+B-A+C}. That can be simplified to
|
|
|
|
// {+D+B-A}, which is the desired conflict.
|
|
|
|
//
|
|
|
|
// Case 3 above:
|
|
|
|
// TODO: describe this case
|
|
|
|
|
|
|
|
// First expand any diffs with nested conflicts.
|
|
|
|
let mut new_removes = vec![];
|
|
|
|
let mut new_adds = vec![];
|
|
|
|
for part in &conflict.adds {
|
|
|
|
match part.value {
|
|
|
|
TreeValue::Conflict(_) => {
|
2021-06-14 07:18:38 +00:00
|
|
|
let conflict = conflict_part_to_conflict(store, part)?;
|
2021-06-05 20:57:11 +00:00
|
|
|
new_removes.extend_from_slice(&conflict.removes);
|
|
|
|
new_adds.extend_from_slice(&conflict.adds);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
new_adds.push(part.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for part in &conflict.removes {
|
|
|
|
match part.value {
|
|
|
|
TreeValue::Conflict(_) => {
|
2021-06-14 07:18:38 +00:00
|
|
|
let conflict = conflict_part_to_conflict(store, part)?;
|
2021-06-05 20:57:11 +00:00
|
|
|
new_removes.extend_from_slice(&conflict.adds);
|
|
|
|
new_adds.extend_from_slice(&conflict.removes);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
new_removes.push(part.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove pairs of entries that match in the removes and adds.
|
|
|
|
let mut add_index = 0;
|
|
|
|
while add_index < new_adds.len() {
|
|
|
|
let add = &new_adds[add_index];
|
|
|
|
add_index += 1;
|
|
|
|
for (remove_index, remove) in new_removes.iter().enumerate() {
|
|
|
|
if remove.value == add.value {
|
|
|
|
new_removes.remove(remove_index);
|
|
|
|
add_index -= 1;
|
|
|
|
new_adds.remove(add_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We should probably remove duplicate entries here too. So if we have
|
|
|
|
// {+A+A}, that would become just {+A}. Similarly {+B-A+B} would be just
|
|
|
|
// {+B-A}.
|
|
|
|
|
2021-10-21 05:00:52 +00:00
|
|
|
Ok(Conflict {
|
2021-06-05 20:57:11 +00:00
|
|
|
adds: new_adds,
|
|
|
|
removes: new_removes,
|
2021-10-21 05:00:52 +00:00
|
|
|
})
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|