2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2020 The Jujutsu Authors
|
2020-12-12 08:00:42 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2023-07-10 15:17:00 +00:00
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::fmt::{Debug, Error, Formatter};
|
2023-06-30 05:58:47 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
2021-06-05 20:57:11 +00:00
|
|
|
use std::io::Read;
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-10-21 05:09:09 +00:00
|
|
|
use itertools::Itertools;
|
2022-04-28 15:53:51 +00:00
|
|
|
use thiserror::Error;
|
2023-07-25 17:51:44 +00:00
|
|
|
use tracing::instrument;
|
2021-10-21 05:09:09 +00:00
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
use crate::backend::{
|
2023-05-31 23:04:25 +00:00
|
|
|
BackendError, ConflictId, FileId, ObjectId, TreeEntriesNonRecursiveIterator, TreeEntry, TreeId,
|
|
|
|
TreeValue,
|
2021-09-12 06:52:38 +00:00
|
|
|
};
|
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};
|
2023-08-06 16:21:35 +00:00
|
|
|
use crate::merge::{trivial_merge, Merge};
|
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
|
|
|
|
2022-04-28 15:53:51 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum TreeMergeError {
|
|
|
|
#[error("Failed to read file with ID {} ", .file_id.hex())]
|
|
|
|
ReadError {
|
|
|
|
source: std::io::Error,
|
|
|
|
file_id: FileId,
|
|
|
|
},
|
|
|
|
#[error("Backend error: {0}")]
|
|
|
|
BackendError(#[from] BackendError),
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 05:58:47 +00:00
|
|
|
impl PartialEq for Tree {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.id == other.id && self.dir == other.dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Tree {}
|
|
|
|
|
|
|
|
impl Hash for Tree {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.dir.hash(state);
|
|
|
|
self.id.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[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,
|
2021-11-10 16:43:17 +00:00
|
|
|
id: TreeId::new(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
|
|
|
|
}
|
|
|
|
|
2021-11-15 05:16:06 +00:00
|
|
|
pub fn entries_non_recursive(&self) -> TreeEntriesNonRecursiveIterator {
|
2020-12-12 08:00:42 +00:00
|
|
|
self.data.entries()
|
|
|
|
}
|
|
|
|
|
2021-11-15 04:29:18 +00:00
|
|
|
pub fn entries(&self) -> TreeEntriesIterator<'static> {
|
|
|
|
TreeEntriesIterator::new(self.clone(), &EverythingMatcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn entries_matching<'matcher>(
|
|
|
|
&self,
|
|
|
|
matcher: &'matcher dyn Matcher,
|
|
|
|
) -> TreeEntriesIterator<'matcher> {
|
|
|
|
TreeEntriesIterator::new(self.clone(), matcher)
|
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
|
|
|
}
|
|
|
|
|
2023-06-14 08:18:54 +00:00
|
|
|
fn known_sub_tree(&self, subdir: &RepoPath, id: &TreeId) -> Tree {
|
|
|
|
self.store.get_tree(subdir, id).unwrap()
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:41:25 +00:00
|
|
|
fn sub_tree_recursive(&self, components: &[RepoPathComponent]) -> Option<Tree> {
|
2023-06-14 09:00:30 +00:00
|
|
|
if let Some((first, tail)) = components.split_first() {
|
|
|
|
tail.iter()
|
|
|
|
.try_fold(self.sub_tree(first)?, |tree, name| tree.sub_tree(name))
|
|
|
|
} else {
|
2020-12-12 08:00:42 +00:00
|
|
|
// 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.
|
2023-06-14 09:00:30 +00:00
|
|
|
Some(self.clone())
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 17:51:44 +00:00
|
|
|
#[instrument(skip(matcher))]
|
2021-06-05 21:29:40 +00:00
|
|
|
pub fn diff<'matcher>(
|
|
|
|
&self,
|
|
|
|
other: &Tree,
|
|
|
|
matcher: &'matcher dyn Matcher,
|
|
|
|
) -> TreeDiffIterator<'matcher> {
|
2023-07-06 20:25:51 +00:00
|
|
|
TreeDiffIterator::new(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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 03:47:03 +00:00
|
|
|
pub fn conflicts_matching(&self, matcher: &dyn Matcher) -> Vec<(RepoPath, ConflictId)> {
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut conflicts = vec![];
|
2022-12-04 03:47:03 +00:00
|
|
|
for (name, value) in self.entries_matching(matcher) {
|
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
|
|
|
|
}
|
2022-12-04 03:47:03 +00:00
|
|
|
|
2023-07-25 17:51:44 +00:00
|
|
|
#[instrument]
|
2022-12-04 03:47:03 +00:00
|
|
|
pub fn conflicts(&self) -> Vec<(RepoPath, ConflictId)> {
|
|
|
|
self.conflicts_matching(&EverythingMatcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_conflict(&self) -> bool {
|
|
|
|
!self.conflicts().is_empty()
|
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2020-12-20 08:07:51 +00:00
|
|
|
|
2021-11-15 04:29:18 +00:00
|
|
|
pub struct TreeEntriesIterator<'matcher> {
|
2023-05-27 13:00:24 +00:00
|
|
|
stack: Vec<TreeEntriesDirItem>,
|
|
|
|
matcher: &'matcher dyn Matcher,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TreeEntriesDirItem {
|
2021-11-15 05:16:06 +00:00
|
|
|
entry_iterator: TreeEntriesNonRecursiveIterator<'static>,
|
2022-12-08 12:06:02 +00:00
|
|
|
// On drop, tree must outlive entry_iterator
|
2023-06-30 14:33:24 +00:00
|
|
|
tree: Box<Tree>,
|
2020-12-20 08:07:51 +00:00
|
|
|
}
|
|
|
|
|
2023-05-27 13:00:24 +00:00
|
|
|
impl TreeEntriesDirItem {
|
|
|
|
fn new(tree: Tree) -> Self {
|
2023-06-30 14:33:24 +00:00
|
|
|
let tree = Box::new(tree);
|
2021-11-15 05:16:06 +00:00
|
|
|
let entry_iterator = tree.entries_non_recursive();
|
|
|
|
let entry_iterator: TreeEntriesNonRecursiveIterator<'static> =
|
|
|
|
unsafe { std::mem::transmute(entry_iterator) };
|
2020-12-20 08:07:51 +00:00
|
|
|
Self {
|
2021-11-15 05:16:06 +00:00
|
|
|
entry_iterator,
|
2022-12-08 12:06:02 +00:00
|
|
|
tree,
|
2023-05-27 13:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'matcher> TreeEntriesIterator<'matcher> {
|
|
|
|
fn new(tree: Tree, matcher: &'matcher dyn Matcher) -> Self {
|
|
|
|
// TODO: Restrict walk according to Matcher::visit()
|
|
|
|
Self {
|
|
|
|
stack: vec![TreeEntriesDirItem::new(tree)],
|
2021-11-15 04:29:18 +00:00
|
|
|
matcher,
|
2020-12-20 08:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 04:29:18 +00:00
|
|
|
impl Iterator for TreeEntriesIterator<'_> {
|
2020-12-20 08:07:51 +00:00
|
|
|
type Item = (RepoPath, TreeValue);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2023-05-27 13:00:24 +00:00
|
|
|
while let Some(top) = self.stack.last_mut() {
|
|
|
|
if let Some(entry) = top.entry_iterator.next() {
|
|
|
|
let path = top.tree.dir().join(entry.name());
|
|
|
|
match entry.value() {
|
|
|
|
TreeValue::Tree(id) => {
|
2023-05-27 13:49:22 +00:00
|
|
|
// TODO: Handle the other cases (specific files and trees)
|
|
|
|
if self.matcher.visit(&path).is_nothing() {
|
|
|
|
continue;
|
|
|
|
}
|
2023-06-14 08:18:54 +00:00
|
|
|
let subtree = top.tree.known_sub_tree(&path, id);
|
2023-05-27 13:00:24 +00:00
|
|
|
self.stack.push(TreeEntriesDirItem::new(subtree));
|
2021-11-15 05:16:06 +00:00
|
|
|
}
|
2023-05-27 13:00:24 +00:00
|
|
|
value => {
|
|
|
|
if self.matcher.matches(&path) {
|
|
|
|
return Some((path, value.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
self.stack.pop();
|
|
|
|
}
|
2020-12-20 08:07:51 +00:00
|
|
|
}
|
2023-05-27 13:00:24 +00:00
|
|
|
None
|
2020-12-20 08:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
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> {
|
2023-07-06 12:19:33 +00:00
|
|
|
pub fn from_options(left: Option<T>, right: Option<T>) -> Self {
|
|
|
|
match (left, right) {
|
|
|
|
(Some(left), Some(right)) => Diff::Modified(left, right),
|
|
|
|
(None, Some(right)) => Diff::Added(right),
|
|
|
|
(Some(left), None) => Diff::Removed(left),
|
|
|
|
(None, None) => panic!("left and right cannot both be 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
|
|
|
}
|
|
|
|
|
2023-05-02 18:51:53 +00:00
|
|
|
struct TreeEntryDiffIterator<'trees> {
|
2023-07-06 07:05:27 +00:00
|
|
|
tree1: &'trees Tree,
|
|
|
|
tree2: &'trees Tree,
|
|
|
|
basename_iter: Box<dyn Iterator<Item = &'trees RepoPathComponent> + 'trees>,
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 18:51:53 +00:00
|
|
|
impl<'trees> TreeEntryDiffIterator<'trees> {
|
|
|
|
fn new(tree1: &'trees Tree, tree2: &'trees Tree) -> Self {
|
2023-07-06 07:05:27 +00:00
|
|
|
let basename_iter = Box::new(tree1.data.names().merge(tree2.data.names()).dedup());
|
|
|
|
TreeEntryDiffIterator {
|
|
|
|
tree1,
|
|
|
|
tree2,
|
|
|
|
basename_iter,
|
|
|
|
}
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 18:51:53 +00:00
|
|
|
impl<'trees> Iterator for TreeEntryDiffIterator<'trees> {
|
2021-06-06 05:55:10 +00:00
|
|
|
type Item = (
|
2023-05-04 08:00:29 +00:00
|
|
|
&'trees 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> {
|
2023-07-06 07:05:27 +00:00
|
|
|
for basename in self.basename_iter.by_ref() {
|
|
|
|
let value1 = self.tree1.value(basename);
|
|
|
|
let value2 = self.tree2.value(basename);
|
|
|
|
if value1 != value2 {
|
|
|
|
return Some((basename, value1, value2));
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-06 07:05:27 +00:00
|
|
|
None
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
pub struct TreeDiffIterator<'matcher> {
|
2023-05-04 08:10:54 +00:00
|
|
|
stack: Vec<TreeDiffItem>,
|
2021-06-05 21:29:40 +00:00
|
|
|
matcher: &'matcher dyn Matcher,
|
2023-05-04 08:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TreeDiffDirItem {
|
|
|
|
path: RepoPath,
|
2021-06-05 20:57:11 +00:00
|
|
|
// Iterator over the diffs between tree1 and tree2
|
2023-05-04 08:10:54 +00:00
|
|
|
entry_iterator: TreeEntryDiffIterator<'static>,
|
2022-12-08 12:06:02 +00:00
|
|
|
// On drop, tree1 and tree2 must outlive entry_iterator
|
2023-06-30 14:33:24 +00:00
|
|
|
tree1: Box<Tree>,
|
|
|
|
tree2: Box<Tree>,
|
2023-05-04 08:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum TreeDiffItem {
|
|
|
|
Dir(TreeDiffDirItem),
|
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.
|
2023-05-04 08:10:54 +00:00
|
|
|
File(RepoPath, Diff<TreeValue>),
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-05 21:29:40 +00:00
|
|
|
impl<'matcher> TreeDiffIterator<'matcher> {
|
2023-07-06 20:25:51 +00:00
|
|
|
fn new(tree1: Tree, tree2: Tree, matcher: &'matcher dyn Matcher) -> Self {
|
|
|
|
let root_dir = RepoPath::root();
|
2023-05-04 08:10:54 +00:00
|
|
|
let mut stack = Vec::new();
|
2023-07-06 20:25:51 +00:00
|
|
|
if !matcher.visit(&root_dir).is_nothing() {
|
|
|
|
stack.push(TreeDiffItem::Dir(TreeDiffDirItem::new(
|
|
|
|
root_dir, tree1, tree2,
|
|
|
|
)));
|
2023-05-04 08:10:54 +00:00
|
|
|
};
|
|
|
|
Self { stack, matcher }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeDiffDirItem {
|
|
|
|
fn new(path: RepoPath, tree1: Tree, tree2: Tree) -> Self {
|
2023-06-30 14:33:24 +00:00
|
|
|
let tree1 = Box::new(tree1);
|
|
|
|
let tree2 = Box::new(tree2);
|
2023-07-06 20:16:59 +00:00
|
|
|
let iter: TreeEntryDiffIterator = TreeEntryDiffIterator::new(&tree1, &tree2);
|
2023-05-04 08:10:54 +00:00
|
|
|
let iter: TreeEntryDiffIterator<'static> = unsafe { std::mem::transmute(iter) };
|
2021-06-05 20:57:11 +00:00
|
|
|
Self {
|
2023-05-04 08:10:54 +00:00
|
|
|
path,
|
|
|
|
entry_iterator: iter,
|
2022-12-08 12:06:02 +00:00
|
|
|
tree1,
|
|
|
|
tree2,
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-04 06:32:02 +00:00
|
|
|
|
2023-05-04 08:10:54 +00:00
|
|
|
fn subdir(
|
|
|
|
&self,
|
2023-07-06 13:48:46 +00:00
|
|
|
subdir_path: &RepoPath,
|
2023-05-04 06:32:02 +00:00
|
|
|
before: Option<&TreeValue>,
|
|
|
|
after: Option<&TreeValue>,
|
2023-05-04 08:10:54 +00:00
|
|
|
) -> Self {
|
2023-05-04 06:32:02 +00:00
|
|
|
let before_tree = match before {
|
2023-07-06 13:48:46 +00:00
|
|
|
Some(TreeValue::Tree(id_before)) => self.tree1.known_sub_tree(subdir_path, id_before),
|
2023-05-04 06:32:02 +00:00
|
|
|
_ => Tree::null(self.tree1.store().clone(), subdir_path.clone()),
|
|
|
|
};
|
|
|
|
let after_tree = match after {
|
2023-07-06 13:48:46 +00:00
|
|
|
Some(TreeValue::Tree(id_after)) => self.tree2.known_sub_tree(subdir_path, id_after),
|
2023-05-04 06:32:02 +00:00
|
|
|
_ => Tree::null(self.tree2.store().clone(), subdir_path.clone()),
|
|
|
|
};
|
2023-07-06 13:48:46 +00:00
|
|
|
Self::new(subdir_path.clone(), before_tree, after_tree)
|
2023-05-04 06:32:02 +00:00
|
|
|
}
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
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> {
|
2023-05-04 08:10:54 +00:00
|
|
|
while let Some(top) = self.stack.last_mut() {
|
|
|
|
let (dir, (name, before, after)) = match top {
|
|
|
|
TreeDiffItem::Dir(dir) => {
|
|
|
|
if let Some(entry) = dir.entry_iterator.next() {
|
|
|
|
(dir, entry)
|
|
|
|
} else {
|
|
|
|
self.stack.pop().unwrap();
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
2023-05-04 08:10:54 +00:00
|
|
|
TreeDiffItem::File(..) => {
|
|
|
|
if let TreeDiffItem::File(name, diff) = self.stack.pop().unwrap() {
|
|
|
|
return Some((name, diff));
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-06-05 20:57:11 +00:00
|
|
|
|
2023-07-06 13:48:46 +00:00
|
|
|
let path = dir.path.join(name);
|
2022-04-23 01:01:49 +00:00
|
|
|
let tree_before = matches!(before, Some(TreeValue::Tree(_)));
|
|
|
|
let tree_after = matches!(after, Some(TreeValue::Tree(_)));
|
2023-05-04 08:10:54 +00:00
|
|
|
let post_subdir =
|
2023-07-06 13:48:46 +00:00
|
|
|
if (tree_before || tree_after) && !self.matcher.visit(&path).is_nothing() {
|
|
|
|
let subdir = dir.subdir(&path, before, after);
|
2023-05-04 08:10:54 +00:00
|
|
|
self.stack.push(TreeDiffItem::Dir(subdir));
|
|
|
|
self.stack.len() - 1
|
|
|
|
} else {
|
|
|
|
self.stack.len()
|
|
|
|
};
|
2023-07-06 13:48:46 +00:00
|
|
|
if self.matcher.matches(&path) {
|
2022-04-23 01:01:49 +00:00
|
|
|
if !tree_before && tree_after {
|
2023-07-06 13:53:28 +00:00
|
|
|
if let Some(value_before) = before {
|
|
|
|
return Some((path, Diff::Removed(value_before.clone())));
|
2022-04-23 01:01:49 +00:00
|
|
|
}
|
|
|
|
} else if tree_before && !tree_after {
|
2023-07-06 13:53:28 +00:00
|
|
|
if let Some(value_after) = after {
|
2023-05-04 08:10:54 +00:00
|
|
|
self.stack.insert(
|
|
|
|
post_subdir,
|
2023-07-06 13:53:28 +00:00
|
|
|
TreeDiffItem::File(path, Diff::Added(value_after.clone())),
|
2023-05-04 08:10:54 +00:00
|
|
|
);
|
2022-04-23 01:01:49 +00:00
|
|
|
}
|
|
|
|
} else if !tree_before && !tree_after {
|
2023-07-06 13:48:46 +00:00
|
|
|
return Some((path, Diff::from_options(before.cloned(), after.cloned())));
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-04 08:10:54 +00:00
|
|
|
None
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn merge_trees(
|
|
|
|
side1_tree: &Tree,
|
|
|
|
base_tree: &Tree,
|
|
|
|
side2_tree: &Tree,
|
2023-06-30 05:58:47 +00:00
|
|
|
) -> Result<Tree, TreeMergeError> {
|
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);
|
|
|
|
|
2023-06-30 05:58:47 +00:00
|
|
|
if let Some(resolved) = trivial_merge(&[base_tree], &[side1_tree, side2_tree]) {
|
2023-05-30 04:46:38 +00:00
|
|
|
return Ok((*resolved).clone());
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
2023-07-06 20:16:59 +00:00
|
|
|
for (basename, maybe_base, maybe_side2) in TreeEntryDiffIterator::new(base_tree, side2_tree) {
|
2023-05-04 08:00:29 +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
|
2023-06-28 10:09:54 +00:00
|
|
|
new_tree.set_or_remove(basename, maybe_side2.cloned());
|
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 =
|
2023-05-04 08:00:29 +00:00
|
|
|
merge_tree_value(store, dir, basename, maybe_base, maybe_side1, maybe_side2)?;
|
2023-06-28 10:09:54 +00:00
|
|
|
new_tree.set_or_remove(basename, new_value);
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-30 05:58:47 +00:00
|
|
|
Ok(store.write_tree(dir, new_tree)?)
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 03:51:24 +00:00
|
|
|
/// Returns `Some(TreeId)` if this is a directory or missing. If it's missing,
|
|
|
|
/// we treat it as an empty tree.
|
|
|
|
fn maybe_tree_id<'id>(
|
|
|
|
value: Option<&'id TreeValue>,
|
|
|
|
empty_tree_id: &'id TreeId,
|
|
|
|
) -> Option<&'id TreeId> {
|
|
|
|
match value {
|
|
|
|
Some(TreeValue::Tree(id)) => Some(id),
|
|
|
|
None => Some(empty_tree_id),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 20:57:11 +00:00
|
|
|
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>,
|
2022-04-28 15:53:51 +00:00
|
|
|
) -> Result<Option<TreeValue>, TreeMergeError> {
|
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
|
2021-10-21 05:09:09 +00:00
|
|
|
|
2022-04-18 03:51:24 +00:00
|
|
|
let empty_tree_id = store.empty_tree_id();
|
|
|
|
let base_tree_id = maybe_tree_id(maybe_base, empty_tree_id);
|
|
|
|
let side1_tree_id = maybe_tree_id(maybe_side1, empty_tree_id);
|
|
|
|
let side2_tree_id = maybe_tree_id(maybe_side2, empty_tree_id);
|
|
|
|
Ok(match (base_tree_id, side1_tree_id, side2_tree_id) {
|
|
|
|
(Some(base_id), Some(side1_id), Some(side2_id)) => {
|
2021-06-06 05:59:52 +00:00
|
|
|
let subdir = dir.join(basename);
|
2022-04-18 03:51:24 +00:00
|
|
|
let base_tree = store.get_tree(&subdir, base_id)?;
|
|
|
|
let side1_tree = store.get_tree(&subdir, side1_id)?;
|
|
|
|
let side2_tree = store.get_tree(&subdir, side2_id)?;
|
2023-06-30 05:58:47 +00:00
|
|
|
let merged_tree = merge_trees(&side1_tree, &base_tree, &side2_tree)?;
|
|
|
|
if merged_tree.id() == empty_tree_id {
|
2021-06-05 20:57:11 +00:00
|
|
|
None
|
|
|
|
} else {
|
2023-06-30 05:58:47 +00:00
|
|
|
Some(TreeValue::Tree(merged_tree.id().clone()))
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2021-10-21 05:09:09 +00:00
|
|
|
// Start by creating a Conflict object. Conflicts can cleanly represent a single
|
|
|
|
// resolved state, the absence of a state, or a conflicted state.
|
2023-08-06 13:52:40 +00:00
|
|
|
let conflict = Merge::new(
|
2023-05-31 23:04:25 +00:00
|
|
|
vec![maybe_base.cloned()],
|
|
|
|
vec![maybe_side1.cloned(), maybe_side2.cloned()],
|
|
|
|
);
|
2022-03-31 16:21:50 +00:00
|
|
|
let filename = dir.join(basename);
|
2023-08-06 13:52:40 +00:00
|
|
|
let merge = simplify_conflict(store, &filename, conflict)?;
|
2023-08-06 19:38:08 +00:00
|
|
|
match merge.into_resolved() {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(conflict) => {
|
|
|
|
if let Some(tree_value) =
|
|
|
|
try_resolve_file_conflict(store, &filename, &conflict)?
|
|
|
|
{
|
|
|
|
Some(tree_value)
|
|
|
|
} else {
|
|
|
|
let conflict_id = store.write_conflict(&filename, &conflict)?;
|
|
|
|
Some(TreeValue::Conflict(conflict_id))
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 05:09:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-06-05 20:57:11 +00:00
|
|
|
|
2023-06-12 13:34:35 +00:00
|
|
|
pub fn try_resolve_file_conflict(
|
2021-10-21 05:09:09 +00:00
|
|
|
store: &Store,
|
|
|
|
filename: &RepoPath,
|
2023-08-06 13:52:40 +00:00
|
|
|
conflict: &Merge<Option<TreeValue>>,
|
2023-06-12 20:16:45 +00:00
|
|
|
) -> Result<Option<TreeValue>, TreeMergeError> {
|
2023-05-31 23:04:25 +00:00
|
|
|
// If there are any non-file or any missing parts in the conflict, we can't
|
|
|
|
// 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
|
|
|
|
// resulting file should be executable.
|
2023-07-18 07:19:54 +00:00
|
|
|
let Some(file_id_conflict) = conflict.maybe_map(|term| match term {
|
2023-06-07 21:42:27 +00:00
|
|
|
Some(TreeValue::File { id, executable: _ }) => Some(id),
|
|
|
|
_ => None,
|
2023-07-18 07:19:54 +00:00
|
|
|
}) else {
|
2023-06-07 21:42:27 +00:00
|
|
|
return Ok(None);
|
|
|
|
};
|
2023-07-18 07:19:54 +00:00
|
|
|
let Some(executable_conflict) = conflict.maybe_map(|term| match term {
|
2023-06-07 21:42:27 +00:00
|
|
|
Some(TreeValue::File { id: _, executable }) => Some(executable),
|
|
|
|
_ => None,
|
2023-07-18 07:19:54 +00:00
|
|
|
}) else {
|
2023-06-07 21:42:27 +00:00
|
|
|
return Ok(None);
|
|
|
|
};
|
2023-07-18 07:19:54 +00:00
|
|
|
let Some(&&executable) = executable_conflict.resolve_trivial() else {
|
2021-10-21 05:09:09 +00:00
|
|
|
// We're unable to determine whether the result should be executable
|
|
|
|
return Ok(None);
|
|
|
|
};
|
2023-06-07 21:42:27 +00:00
|
|
|
if let Some(&resolved_file_id) = file_id_conflict.resolve_trivial() {
|
2023-06-12 20:31:44 +00:00
|
|
|
// Don't bother reading the file contents if the conflict can be trivially
|
|
|
|
// resolved.
|
|
|
|
return Ok(Some(TreeValue::File {
|
|
|
|
id: resolved_file_id.clone(),
|
|
|
|
executable,
|
|
|
|
}));
|
|
|
|
}
|
2021-10-21 05:09:09 +00:00
|
|
|
let mut removed_contents = vec![];
|
|
|
|
let mut added_contents = vec![];
|
2023-06-07 21:42:27 +00:00
|
|
|
for &file_id in file_id_conflict.removes() {
|
2021-10-21 05:09:09 +00:00
|
|
|
let mut content = vec![];
|
|
|
|
store
|
2023-06-07 21:42:27 +00:00
|
|
|
.read_file(filename, file_id)?
|
2022-04-28 15:53:51 +00:00
|
|
|
.read_to_end(&mut content)
|
|
|
|
.map_err(|err| TreeMergeError::ReadError {
|
|
|
|
source: err,
|
2023-06-07 21:42:27 +00:00
|
|
|
file_id: file_id.clone(),
|
2022-04-28 15:53:51 +00:00
|
|
|
})?;
|
2021-10-21 05:09:09 +00:00
|
|
|
removed_contents.push(content);
|
|
|
|
}
|
2023-06-07 21:42:27 +00:00
|
|
|
for &file_id in file_id_conflict.adds() {
|
2021-10-21 05:09:09 +00:00
|
|
|
let mut content = vec![];
|
|
|
|
store
|
2023-06-07 21:42:27 +00:00
|
|
|
.read_file(filename, file_id)?
|
2022-04-28 15:53:51 +00:00
|
|
|
.read_to_end(&mut content)
|
|
|
|
.map_err(|err| TreeMergeError::ReadError {
|
|
|
|
source: err,
|
2023-06-07 21:42:27 +00:00
|
|
|
file_id: file_id.clone(),
|
2022-04-28 15:53:51 +00:00
|
|
|
})?;
|
2021-10-21 05:09:09 +00:00
|
|
|
added_contents.push(content);
|
|
|
|
}
|
|
|
|
let merge_result = files::merge(
|
|
|
|
&removed_contents.iter().map(Vec::as_slice).collect_vec(),
|
|
|
|
&added_contents.iter().map(Vec::as_slice).collect_vec(),
|
|
|
|
);
|
|
|
|
match merge_result {
|
2023-06-12 20:16:45 +00:00
|
|
|
MergeResult::Resolved(merged_content) => {
|
2023-06-27 08:36:26 +00:00
|
|
|
let id = store.write_file(filename, &mut merged_content.0.as_slice())?;
|
2023-06-12 20:31:44 +00:00
|
|
|
Ok(Some(TreeValue::File { id, executable }))
|
2023-06-12 20:16:45 +00:00
|
|
|
}
|
2021-10-21 05:09:09 +00:00
|
|
|
MergeResult::Conflict(_) => Ok(None),
|
|
|
|
}
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 16:21:50 +00:00
|
|
|
fn simplify_conflict(
|
|
|
|
store: &Store,
|
|
|
|
path: &RepoPath,
|
2023-08-06 13:52:40 +00:00
|
|
|
conflict: Merge<Option<TreeValue>>,
|
|
|
|
) -> Result<Merge<Option<TreeValue>>, 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
|
|
|
|
|
2023-06-29 13:50:23 +00:00
|
|
|
let expanded = conflict.try_map(|term| match term {
|
|
|
|
Some(TreeValue::Conflict(id)) => store.read_conflict(path, id),
|
2023-08-06 13:52:40 +00:00
|
|
|
_ => Ok(Merge::resolved(term.clone())),
|
2023-06-29 13:50:23 +00:00
|
|
|
})?;
|
|
|
|
Ok(expanded.flatten().simplify())
|
2021-06-05 20:57:11 +00:00
|
|
|
}
|