jujutsu_lib/
tree.rs

1// Copyright 2020 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::cmp::Ordering;
16use std::fmt::{Debug, Error, Formatter};
17use std::io::Read;
18use std::iter::Peekable;
19use std::pin::Pin;
20use std::sync::Arc;
21
22use itertools::Itertools;
23use thiserror::Error;
24
25use crate::backend::{
26    BackendError, Conflict, ConflictId, ConflictPart, FileId, ObjectId,
27    TreeEntriesNonRecursiveIterator, TreeEntry, TreeId, TreeValue,
28};
29use crate::files::MergeResult;
30use crate::matchers::{EverythingMatcher, Matcher};
31use crate::repo_path::{RepoPath, RepoPathComponent, RepoPathJoin};
32use crate::store::Store;
33use crate::{backend, files};
34
35#[derive(Debug, Error)]
36pub enum TreeMergeError {
37    #[error("Failed to read file with ID {} ", .file_id.hex())]
38    ReadError {
39        source: std::io::Error,
40        file_id: FileId,
41    },
42    #[error("Backend error: {0}")]
43    BackendError(#[from] BackendError),
44}
45
46#[derive(Clone)]
47pub struct Tree {
48    store: Arc<Store>,
49    dir: RepoPath,
50    id: TreeId,
51    data: Arc<backend::Tree>,
52}
53
54impl Debug for Tree {
55    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
56        f.debug_struct("Tree")
57            .field("dir", &self.dir)
58            .field("id", &self.id)
59            .finish()
60    }
61}
62
63#[derive(Debug, PartialEq, Eq, Clone)]
64pub struct DiffSummary {
65    pub modified: Vec<RepoPath>,
66    pub added: Vec<RepoPath>,
67    pub removed: Vec<RepoPath>,
68}
69
70impl DiffSummary {
71    pub fn is_empty(&self) -> bool {
72        self.modified.is_empty() && self.added.is_empty() && self.removed.is_empty()
73    }
74}
75
76impl Tree {
77    pub fn new(store: Arc<Store>, dir: RepoPath, id: TreeId, data: Arc<backend::Tree>) -> Self {
78        Tree {
79            store,
80            dir,
81            id,
82            data,
83        }
84    }
85
86    pub fn null(store: Arc<Store>, dir: RepoPath) -> Self {
87        Tree {
88            store,
89            dir,
90            id: TreeId::new(vec![]),
91            data: Arc::new(backend::Tree::default()),
92        }
93    }
94
95    pub fn store(&self) -> &Arc<Store> {
96        &self.store
97    }
98
99    pub fn dir(&self) -> &RepoPath {
100        &self.dir
101    }
102
103    pub fn id(&self) -> &TreeId {
104        &self.id
105    }
106
107    pub fn data(&self) -> &backend::Tree {
108        &self.data
109    }
110
111    pub fn entries_non_recursive(&self) -> TreeEntriesNonRecursiveIterator {
112        self.data.entries()
113    }
114
115    pub fn entries(&self) -> TreeEntriesIterator<'static> {
116        TreeEntriesIterator::new(self.clone(), &EverythingMatcher)
117    }
118
119    pub fn entries_matching<'matcher>(
120        &self,
121        matcher: &'matcher dyn Matcher,
122    ) -> TreeEntriesIterator<'matcher> {
123        TreeEntriesIterator::new(self.clone(), matcher)
124    }
125
126    pub fn entry(&self, basename: &RepoPathComponent) -> Option<TreeEntry> {
127        self.data.entry(basename)
128    }
129
130    pub fn value(&self, basename: &RepoPathComponent) -> Option<&TreeValue> {
131        self.data.value(basename)
132    }
133
134    pub fn path_value(&self, path: &RepoPath) -> Option<TreeValue> {
135        assert_eq!(self.dir(), &RepoPath::root());
136        match path.split() {
137            Some((dir, basename)) => self
138                .sub_tree_recursive(dir.components())
139                .and_then(|tree| tree.data.value(basename).cloned()),
140            None => Some(TreeValue::Tree(self.id.clone())),
141        }
142    }
143
144    pub fn sub_tree(&self, name: &RepoPathComponent) -> Option<Tree> {
145        self.data.value(name).and_then(|sub_tree| match sub_tree {
146            TreeValue::Tree(sub_tree_id) => {
147                let subdir = self.dir.join(name);
148                Some(self.store.get_tree(&subdir, sub_tree_id).unwrap())
149            }
150            _ => None,
151        })
152    }
153
154    pub fn known_sub_tree(&self, name: &RepoPathComponent, id: &TreeId) -> Tree {
155        let subdir = self.dir.join(name);
156        self.store.get_tree(&subdir, id).unwrap()
157    }
158
159    fn sub_tree_recursive(&self, components: &[RepoPathComponent]) -> Option<Tree> {
160        if components.is_empty() {
161            // TODO: It would be nice to be able to return a reference here, but
162            // then we would have to figure out how to share Tree instances
163            // across threads.
164            Some(Tree {
165                store: self.store.clone(),
166                dir: self.dir.clone(),
167                id: self.id.clone(),
168                data: self.data.clone(),
169            })
170        } else {
171            match self.data.entry(&components[0]) {
172                None => None,
173                Some(entry) => match entry.value() {
174                    TreeValue::Tree(sub_tree_id) => {
175                        let sub_tree = self.known_sub_tree(entry.name(), sub_tree_id);
176                        sub_tree.sub_tree_recursive(&components[1..])
177                    }
178                    _ => None,
179                },
180            }
181        }
182    }
183
184    pub fn diff<'matcher>(
185        &self,
186        other: &Tree,
187        matcher: &'matcher dyn Matcher,
188    ) -> TreeDiffIterator<'matcher> {
189        recursive_tree_diff(self.clone(), other.clone(), matcher)
190    }
191
192    pub fn diff_summary(&self, other: &Tree, matcher: &dyn Matcher) -> DiffSummary {
193        let mut modified = vec![];
194        let mut added = vec![];
195        let mut removed = vec![];
196        for (file, diff) in self.diff(other, matcher) {
197            match diff {
198                Diff::Modified(_, _) => modified.push(file.clone()),
199                Diff::Added(_) => added.push(file.clone()),
200                Diff::Removed(_) => removed.push(file.clone()),
201            }
202        }
203        modified.sort();
204        added.sort();
205        removed.sort();
206        DiffSummary {
207            modified,
208            added,
209            removed,
210        }
211    }
212
213    pub fn conflicts_matching(&self, matcher: &dyn Matcher) -> Vec<(RepoPath, ConflictId)> {
214        let mut conflicts = vec![];
215        for (name, value) in self.entries_matching(matcher) {
216            if let TreeValue::Conflict(id) = value {
217                conflicts.push((name.clone(), id.clone()));
218            }
219        }
220        conflicts
221    }
222
223    pub fn conflicts(&self) -> Vec<(RepoPath, ConflictId)> {
224        self.conflicts_matching(&EverythingMatcher)
225    }
226
227    pub fn has_conflict(&self) -> bool {
228        !self.conflicts().is_empty()
229    }
230}
231
232pub struct TreeEntriesIterator<'matcher> {
233    entry_iterator: TreeEntriesNonRecursiveIterator<'static>,
234    // On drop, tree must outlive entry_iterator
235    tree: Pin<Box<Tree>>,
236    subdir_iterator: Option<Box<TreeEntriesIterator<'matcher>>>,
237    matcher: &'matcher dyn Matcher,
238}
239
240impl<'matcher> TreeEntriesIterator<'matcher> {
241    fn new(tree: Tree, matcher: &'matcher dyn Matcher) -> Self {
242        let tree = Box::pin(tree);
243        // TODO: Restrict walk according to Matcher::visit()
244        let entry_iterator = tree.entries_non_recursive();
245        let entry_iterator: TreeEntriesNonRecursiveIterator<'static> =
246            unsafe { std::mem::transmute(entry_iterator) };
247        Self {
248            entry_iterator,
249            tree,
250            subdir_iterator: None,
251            matcher,
252        }
253    }
254}
255
256impl Iterator for TreeEntriesIterator<'_> {
257    type Item = (RepoPath, TreeValue);
258
259    fn next(&mut self) -> Option<Self::Item> {
260        loop {
261            // First return results from any subdirectory we're currently visiting.
262            if let Some(subdir_iter) = &mut self.subdir_iterator {
263                if let Some(item) = subdir_iter.next() {
264                    return Some(item);
265                }
266                self.subdir_iterator = None;
267            }
268            let entry = self.entry_iterator.next()?;
269            match entry.value() {
270                TreeValue::Tree(id) => {
271                    let subtree = self.tree.known_sub_tree(entry.name(), id);
272                    self.subdir_iterator =
273                        Some(Box::new(TreeEntriesIterator::new(subtree, self.matcher)));
274                }
275                other => {
276                    let path = self.tree.dir().join(entry.name());
277                    if !self.matcher.matches(&path) {
278                        continue;
279                    }
280                    return Some((path, other.clone()));
281                }
282            };
283        }
284    }
285}
286
287#[derive(Debug, PartialEq, Eq, Clone)]
288pub enum Diff<T> {
289    Modified(T, T),
290    Added(T),
291    Removed(T),
292}
293
294impl<T> Diff<T> {
295    pub fn as_options(&self) -> (Option<&T>, Option<&T>) {
296        match self {
297            Diff::Modified(left, right) => (Some(left), Some(right)),
298            Diff::Added(right) => (None, Some(right)),
299            Diff::Removed(left) => (Some(left), None),
300        }
301    }
302
303    pub fn into_options(self) -> (Option<T>, Option<T>) {
304        match self {
305            Diff::Modified(left, right) => (Some(left), Some(right)),
306            Diff::Added(right) => (None, Some(right)),
307            Diff::Removed(left) => (Some(left), None),
308        }
309    }
310}
311
312struct TreeEntryDiffIterator<'trees, 'matcher> {
313    it1: Peekable<TreeEntriesNonRecursiveIterator<'trees>>,
314    it2: Peekable<TreeEntriesNonRecursiveIterator<'trees>>,
315    // TODO: Restrict walk according to Matcher::visit()
316    _matcher: &'matcher dyn Matcher,
317}
318
319impl<'trees, 'matcher> TreeEntryDiffIterator<'trees, 'matcher> {
320    fn new(tree1: &'trees Tree, tree2: &'trees Tree, matcher: &'matcher dyn Matcher) -> Self {
321        let it1 = tree1.entries_non_recursive().peekable();
322        let it2 = tree2.entries_non_recursive().peekable();
323        TreeEntryDiffIterator {
324            it1,
325            it2,
326            _matcher: matcher,
327        }
328    }
329}
330
331impl<'trees, 'matcher> Iterator for TreeEntryDiffIterator<'trees, 'matcher> {
332    type Item = (
333        RepoPathComponent,
334        Option<&'trees TreeValue>,
335        Option<&'trees TreeValue>,
336    );
337
338    fn next(&mut self) -> Option<Self::Item> {
339        loop {
340            let entry1 = self.it1.peek();
341            let entry2 = self.it2.peek();
342            match (&entry1, &entry2) {
343                (Some(before), Some(after)) => {
344                    match before.name().cmp(after.name()) {
345                        Ordering::Less => {
346                            // entry removed
347                            let before = self.it1.next().unwrap();
348                            return Some((before.name().clone(), Some(before.value()), None));
349                        }
350                        Ordering::Greater => {
351                            // entry added
352                            let after = self.it2.next().unwrap();
353                            return Some((after.name().clone(), None, Some(after.value())));
354                        }
355                        Ordering::Equal => {
356                            // entry modified or clean
357                            let before = self.it1.next().unwrap();
358                            let after = self.it2.next().unwrap();
359                            if before.value() != after.value() {
360                                return Some((
361                                    before.name().clone(),
362                                    Some(before.value()),
363                                    Some(after.value()),
364                                ));
365                            }
366                        }
367                    }
368                }
369                (Some(_), None) => {
370                    // second iterator exhausted
371                    let before = self.it1.next().unwrap();
372                    return Some((before.name().clone(), Some(before.value()), None));
373                }
374                (None, Some(_)) => {
375                    // first iterator exhausted
376                    let after = self.it2.next().unwrap();
377                    return Some((after.name().clone(), None, Some(after.value())));
378                }
379                (None, None) => {
380                    // both iterators exhausted
381                    return None;
382                }
383            }
384        }
385    }
386}
387
388fn diff_entries<'trees, 'matcher>(
389    tree1: &'trees Tree,
390    tree2: &'trees Tree,
391    matcher: &'matcher dyn Matcher,
392) -> TreeEntryDiffIterator<'trees, 'matcher> {
393    // TODO: make TreeEntryDiffIterator an enum with one variant that iterates over
394    // the tree entries and filters by the matcher (i.e. what
395    // TreeEntryDiffIterator does now) and another variant that iterates over
396    // what the matcher says to visit
397    TreeEntryDiffIterator::new(tree1, tree2, matcher)
398}
399
400pub fn recursive_tree_diff(root1: Tree, root2: Tree, matcher: &dyn Matcher) -> TreeDiffIterator {
401    TreeDiffIterator::new(RepoPath::root(), root1, root2, matcher)
402}
403
404pub struct TreeDiffIterator<'matcher> {
405    dir: RepoPath,
406    matcher: &'matcher dyn Matcher,
407    // Iterator over the diffs between tree1 and tree2
408    entry_iterator: TreeEntryDiffIterator<'static, 'matcher>,
409    // On drop, tree1 and tree2 must outlive entry_iterator
410    tree1: Pin<Box<Tree>>,
411    tree2: Pin<Box<Tree>>,
412    // This is used for making sure that when a directory gets replaced by a file, we
413    // yield the value for the addition of the file after we yield the values
414    // for removing files in the directory.
415    added_file: Option<(RepoPath, TreeValue)>,
416    // Iterator over the diffs of a subdirectory, if we're currently visiting one.
417    subdir_iterator: Option<Box<TreeDiffIterator<'matcher>>>,
418}
419
420impl<'matcher> TreeDiffIterator<'matcher> {
421    fn new(
422        dir: RepoPath,
423        tree1: Tree,
424        tree2: Tree,
425        matcher: &'matcher dyn Matcher,
426    ) -> TreeDiffIterator {
427        let tree1 = Box::pin(tree1);
428        let tree2 = Box::pin(tree2);
429        let root_entry_iterator: TreeEntryDiffIterator = diff_entries(&tree1, &tree2, matcher);
430        let root_entry_iterator: TreeEntryDiffIterator<'static, 'matcher> =
431            unsafe { std::mem::transmute(root_entry_iterator) };
432        Self {
433            dir,
434            matcher,
435            entry_iterator: root_entry_iterator,
436            tree1,
437            tree2,
438            added_file: None,
439            subdir_iterator: None,
440        }
441    }
442}
443
444impl Iterator for TreeDiffIterator<'_> {
445    type Item = (RepoPath, Diff<TreeValue>);
446
447    fn next(&mut self) -> Option<Self::Item> {
448        loop {
449            // First return results from any subdirectory we're currently visiting.
450            if let Some(subdir_iterator) = &mut self.subdir_iterator {
451                if let Some(element) = subdir_iterator.next() {
452                    return Some(element);
453                }
454                self.subdir_iterator = None;
455            }
456
457            if let Some((name, value)) = self.added_file.take() {
458                return Some((name, Diff::Added(value)));
459            }
460
461            // Note: whenever we say "file" below, it may also be a symlink or a conflict.
462            let (name, before, after) = self.entry_iterator.next()?;
463            let tree_before = matches!(before, Some(TreeValue::Tree(_)));
464            let tree_after = matches!(after, Some(TreeValue::Tree(_)));
465            if tree_before || tree_after {
466                let subdir = &name;
467                let subdir_path = self.dir.join(subdir);
468                let before_tree = match before {
469                    Some(TreeValue::Tree(id_before)) => {
470                        self.tree1.known_sub_tree(subdir, id_before)
471                    }
472                    _ => Tree::null(self.tree1.store().clone(), subdir_path.clone()),
473                };
474                let after_tree = match after {
475                    Some(TreeValue::Tree(id_after)) => self.tree2.known_sub_tree(subdir, id_after),
476                    _ => Tree::null(self.tree2.store().clone(), subdir_path.clone()),
477                };
478                self.subdir_iterator = Some(Box::new(TreeDiffIterator::new(
479                    subdir_path,
480                    before_tree,
481                    after_tree,
482                    self.matcher,
483                )));
484            }
485            let file_path = self.dir.join(&name);
486            if self.matcher.matches(&file_path) {
487                if !tree_before && tree_after {
488                    if let Some(file_before) = before {
489                        return Some((file_path, Diff::Removed(file_before.clone())));
490                    }
491                } else if tree_before && !tree_after {
492                    if let Some(file_after) = after {
493                        self.added_file = Some((file_path, file_after.clone()));
494                    }
495                } else if !tree_before && !tree_after {
496                    match (before, after) {
497                        (Some(file_before), Some(file_after)) => {
498                            return Some((
499                                file_path,
500                                Diff::Modified(file_before.clone(), file_after.clone()),
501                            ));
502                        }
503                        (None, Some(file_after)) => {
504                            return Some((file_path, Diff::Added(file_after.clone())));
505                        }
506                        (Some(file_before), None) => {
507                            return Some((file_path, Diff::Removed(file_before.clone())));
508                        }
509                        (None, None) => {
510                            panic!("unexpected diff")
511                        }
512                    }
513                }
514            }
515        }
516    }
517}
518
519pub fn merge_trees(
520    side1_tree: &Tree,
521    base_tree: &Tree,
522    side2_tree: &Tree,
523) -> Result<TreeId, TreeMergeError> {
524    let store = base_tree.store();
525    let dir = base_tree.dir();
526    assert_eq!(side1_tree.dir(), dir);
527    assert_eq!(side2_tree.dir(), dir);
528
529    if base_tree.id() == side1_tree.id() {
530        return Ok(side2_tree.id().clone());
531    }
532    if base_tree.id() == side2_tree.id() || side1_tree.id() == side2_tree.id() {
533        return Ok(side1_tree.id().clone());
534    }
535
536    // Start with a tree identical to side 1 and modify based on changes from base
537    // to side 2.
538    let mut new_tree = side1_tree.data().clone();
539    for (basename, maybe_base, maybe_side2) in
540        diff_entries(base_tree, side2_tree, &EverythingMatcher)
541    {
542        let maybe_side1 = side1_tree.value(&basename);
543        if maybe_side1 == maybe_base {
544            // side 1 is unchanged: use the value from side 2
545            match maybe_side2 {
546                None => new_tree.remove(&basename),
547                Some(side2) => new_tree.set(basename, side2.clone()),
548            };
549        } else if maybe_side1 == maybe_side2 {
550            // Both sides changed in the same way: new_tree already has the
551            // value
552        } else {
553            // The two sides changed in different ways
554            let new_value =
555                merge_tree_value(store, dir, &basename, maybe_base, maybe_side1, maybe_side2)?;
556            match new_value {
557                None => new_tree.remove(&basename),
558                Some(value) => new_tree.set(basename, value),
559            }
560        }
561    }
562    Ok(store.write_tree(dir, &new_tree)?)
563}
564
565/// Returns `Some(TreeId)` if this is a directory or missing. If it's missing,
566/// we treat it as an empty tree.
567fn maybe_tree_id<'id>(
568    value: Option<&'id TreeValue>,
569    empty_tree_id: &'id TreeId,
570) -> Option<&'id TreeId> {
571    match value {
572        Some(TreeValue::Tree(id)) => Some(id),
573        None => Some(empty_tree_id),
574        _ => None,
575    }
576}
577
578fn merge_tree_value(
579    store: &Arc<Store>,
580    dir: &RepoPath,
581    basename: &RepoPathComponent,
582    maybe_base: Option<&TreeValue>,
583    maybe_side1: Option<&TreeValue>,
584    maybe_side2: Option<&TreeValue>,
585) -> Result<Option<TreeValue>, TreeMergeError> {
586    // Resolve non-trivial conflicts:
587    //   * resolve tree conflicts by recursing
588    //   * try to resolve file conflicts by merging the file contents
589    //   * leave other conflicts (e.g. file/dir conflicts, remove/modify conflicts)
590    //     unresolved
591
592    let empty_tree_id = store.empty_tree_id();
593    let base_tree_id = maybe_tree_id(maybe_base, empty_tree_id);
594    let side1_tree_id = maybe_tree_id(maybe_side1, empty_tree_id);
595    let side2_tree_id = maybe_tree_id(maybe_side2, empty_tree_id);
596    Ok(match (base_tree_id, side1_tree_id, side2_tree_id) {
597        (Some(base_id), Some(side1_id), Some(side2_id)) => {
598            let subdir = dir.join(basename);
599            let base_tree = store.get_tree(&subdir, base_id)?;
600            let side1_tree = store.get_tree(&subdir, side1_id)?;
601            let side2_tree = store.get_tree(&subdir, side2_id)?;
602            let merged_tree_id = merge_trees(&side1_tree, &base_tree, &side2_tree)?;
603            if merged_tree_id == *empty_tree_id {
604                None
605            } else {
606                Some(TreeValue::Tree(merged_tree_id))
607            }
608        }
609        _ => {
610            // Start by creating a Conflict object. Conflicts can cleanly represent a single
611            // resolved state, the absence of a state, or a conflicted state.
612            let mut conflict = Conflict::default();
613            if let Some(base) = maybe_base {
614                conflict.removes.push(ConflictPart {
615                    value: base.clone(),
616                });
617            }
618            if let Some(side1) = maybe_side1 {
619                conflict.adds.push(ConflictPart {
620                    value: side1.clone(),
621                });
622            }
623            if let Some(side2) = maybe_side2 {
624                conflict.adds.push(ConflictPart {
625                    value: side2.clone(),
626                });
627            }
628            let filename = dir.join(basename);
629            let conflict = simplify_conflict(store, &filename, &conflict)?;
630            if conflict.adds.is_empty() {
631                // If there are no values to add, then the path doesn't exist
632                return Ok(None);
633            }
634            if conflict.removes.is_empty() && conflict.adds.len() == 1 {
635                // A single add means that the current state is that state.
636                return Ok(Some(conflict.adds[0].value.clone()));
637            }
638            if let Some((merged_content, executable)) =
639                try_resolve_file_conflict(store, &filename, &conflict)?
640            {
641                let id = store.write_file(&filename, &mut merged_content.as_slice())?;
642                Some(TreeValue::File { id, executable })
643            } else {
644                let conflict_id = store.write_conflict(&filename, &conflict)?;
645                Some(TreeValue::Conflict(conflict_id))
646            }
647        }
648    })
649}
650
651fn try_resolve_file_conflict(
652    store: &Store,
653    filename: &RepoPath,
654    conflict: &Conflict,
655) -> Result<Option<(Vec<u8>, bool)>, TreeMergeError> {
656    // If there are any non-file parts in the conflict, we can't merge it. We check
657    // early so we don't waste time reading file contents if we can't merge them
658    // anyway. At the same time we determine whether the resulting file should
659    // be executable.
660    let mut exec_delta = 0;
661    let mut regular_delta = 0;
662    let mut removed_file_ids = vec![];
663    let mut added_file_ids = vec![];
664    for part in &conflict.removes {
665        match &part.value {
666            TreeValue::File { id, executable } => {
667                if *executable {
668                    exec_delta -= 1;
669                } else {
670                    regular_delta -= 1;
671                }
672                removed_file_ids.push(id.clone());
673            }
674            _ => {
675                return Ok(None);
676            }
677        }
678    }
679    for part in &conflict.adds {
680        match &part.value {
681            TreeValue::File { id, executable } => {
682                if *executable {
683                    exec_delta += 1;
684                } else {
685                    regular_delta += 1;
686                }
687                added_file_ids.push(id.clone());
688            }
689            _ => {
690                return Ok(None);
691            }
692        }
693    }
694    let executable = if exec_delta > 0 && regular_delta <= 0 {
695        true
696    } else if regular_delta > 0 && exec_delta <= 0 {
697        false
698    } else {
699        // We're unable to determine whether the result should be executable
700        return Ok(None);
701    };
702    let mut removed_contents = vec![];
703    let mut added_contents = vec![];
704    for file_id in removed_file_ids {
705        let mut content = vec![];
706        store
707            .read_file(filename, &file_id)?
708            .read_to_end(&mut content)
709            .map_err(|err| TreeMergeError::ReadError {
710                source: err,
711                file_id,
712            })?;
713        removed_contents.push(content);
714    }
715    for file_id in added_file_ids {
716        let mut content = vec![];
717        store
718            .read_file(filename, &file_id)?
719            .read_to_end(&mut content)
720            .map_err(|err| TreeMergeError::ReadError {
721                source: err,
722                file_id,
723            })?;
724        added_contents.push(content);
725    }
726    let merge_result = files::merge(
727        &removed_contents.iter().map(Vec::as_slice).collect_vec(),
728        &added_contents.iter().map(Vec::as_slice).collect_vec(),
729    );
730    match merge_result {
731        MergeResult::Resolved(merged_content) => Ok(Some((merged_content, executable))),
732        MergeResult::Conflict(_) => Ok(None),
733    }
734}
735
736fn conflict_part_to_conflict(
737    store: &Store,
738    path: &RepoPath,
739    part: &ConflictPart,
740) -> Result<Conflict, BackendError> {
741    match &part.value {
742        TreeValue::Conflict(id) => {
743            let conflict = store.read_conflict(path, id)?;
744            Ok(conflict)
745        }
746        other => Ok(Conflict {
747            removes: vec![],
748            adds: vec![ConflictPart {
749                value: other.clone(),
750            }],
751        }),
752    }
753}
754
755fn simplify_conflict(
756    store: &Store,
757    path: &RepoPath,
758    conflict: &Conflict,
759) -> Result<Conflict, BackendError> {
760    // Important cases to simplify:
761    //
762    // D
763    // |
764    // B C
765    // |/
766    // A
767    //
768    // 1. rebase C to B, then back to A => there should be no conflict
769    // 2. rebase C to B, then to D => the conflict should not mention B
770    // 3. rebase B to C and D to B', then resolve the conflict in B' and rebase D'
771    // on top =>    the conflict should be between B'', B, and D; it should not
772    // mention the conflict in B'
773
774    // Case 1 above:
775    // After first rebase, the conflict is {+B-A+C}. After rebasing back,
776    // the unsimplified conflict is {+A-B+{+B-A+C}}. Since the
777    // inner conflict is positive, we can simply move it into the outer conflict. We
778    // thus get {+A-B+B-A+C}, which we can then simplify to just C (because {+C} ==
779    // C).
780    //
781    // Case 2 above:
782    // After first rebase, the conflict is {+B-A+C}. After rebasing to D,
783    // the unsimplified conflict is {+D-C+{+B-A+C}}. As in the
784    // previous case, the inner conflict can be moved into the outer one. We then
785    // get {+D-C+B-A+C}. That can be simplified to
786    // {+D+B-A}, which is the desired conflict.
787    //
788    // Case 3 above:
789    // TODO: describe this case
790
791    // First expand any diffs with nested conflicts.
792    let mut new_removes = vec![];
793    let mut new_adds = vec![];
794    for part in &conflict.adds {
795        match part.value {
796            TreeValue::Conflict(_) => {
797                let conflict = conflict_part_to_conflict(store, path, part)?;
798                new_removes.extend_from_slice(&conflict.removes);
799                new_adds.extend_from_slice(&conflict.adds);
800            }
801            _ => {
802                new_adds.push(part.clone());
803            }
804        }
805    }
806    for part in &conflict.removes {
807        match part.value {
808            TreeValue::Conflict(_) => {
809                let conflict = conflict_part_to_conflict(store, path, part)?;
810                new_removes.extend_from_slice(&conflict.adds);
811                new_adds.extend_from_slice(&conflict.removes);
812            }
813            _ => {
814                new_removes.push(part.clone());
815            }
816        }
817    }
818
819    // Remove pairs of entries that match in the removes and adds.
820    let mut add_index = 0;
821    while add_index < new_adds.len() {
822        let add = &new_adds[add_index];
823        add_index += 1;
824        for (remove_index, remove) in new_removes.iter().enumerate() {
825            if remove.value == add.value {
826                new_removes.remove(remove_index);
827                add_index -= 1;
828                new_adds.remove(add_index);
829                break;
830            }
831        }
832    }
833
834    // TODO: We should probably remove duplicate entries here too. So if we have
835    // {+A+A}, that would become just {+A}. Similarly {+B-A+B} would be just
836    // {+B-A}.
837
838    Ok(Conflict {
839        adds: new_adds,
840        removes: new_removes,
841    })
842}