1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Detection of changes between trees.
use crate::osutils::is_inside_any;
use crate::tree::TreeChange;
use pyo3::prelude::*;
/// Describes changes from one tree to another.
///
/// Contains seven lists with TreeChange objects.
///
/// added
/// removed
/// renamed
/// copied
/// kind_changed
/// modified
/// unchanged
/// unversioned
///
/// Each id is listed only once.
///
/// Files that are both modified and renamed or copied are listed only in
/// renamed or copied, with the text_modified flag true. The text_modified
/// applies either to the content of the file or the target of the
/// symbolic link, depending of the kind of file.
///
/// Files are only considered renamed if their name has changed or
/// their parent directory has changed. Renaming a directory
/// does not count as renaming all its contents.
///
/// The lists are normally sorted when the delta is created.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TreeDelta {
/// Files that were added between the trees.
pub added: Vec<TreeChange>,
/// Files that were removed between the trees.
pub removed: Vec<TreeChange>,
/// Files that were renamed between the trees.
pub renamed: Vec<TreeChange>,
/// Files that were copied between the trees.
pub copied: Vec<TreeChange>,
/// Files that changed kind between the trees.
pub kind_changed: Vec<TreeChange>,
/// Files that were modified between the trees.
pub modified: Vec<TreeChange>,
/// Files that were unchanged between the trees.
pub unchanged: Vec<TreeChange>,
/// Files that are unversioned in the trees.
pub unversioned: Vec<TreeChange>,
/// Files that are missing in the trees.
pub missing: Vec<TreeChange>,
}
impl TreeDelta {
/// Check if there are any changes in this delta.
pub fn has_changed(&self) -> bool {
!self.added.is_empty()
|| !self.removed.is_empty()
|| !self.renamed.is_empty()
|| !self.copied.is_empty()
|| !self.kind_changed.is_empty()
|| !self.modified.is_empty()
}
}
impl<'a, 'py> FromPyObject<'a, 'py> for TreeDelta {
type Error = PyErr;
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> PyResult<Self> {
let added = ob.getattr("added")?.extract()?;
let removed = ob.getattr("removed")?.extract()?;
let renamed = ob.getattr("renamed")?.extract()?;
let copied = ob.getattr("copied")?.extract()?;
let kind_changed = ob.getattr("kind_changed")?.extract()?;
let modified = ob.getattr("modified")?.extract()?;
let unchanged = ob.getattr("unchanged")?.extract()?;
let unversioned = ob.getattr("unversioned")?.extract()?;
let missing = ob.getattr("missing")?.extract()?;
Ok(TreeDelta {
added,
removed,
renamed,
copied,
kind_changed,
modified,
unchanged,
unversioned,
missing,
})
}
}
/// Filter out excluded paths from a list of tree changes.
///
/// This function filters out tree changes that are in excluded paths.
///
/// # Arguments
/// * `iter_changes` - Iterator of tree changes
/// * `exclude` - List of paths to exclude
///
/// # Returns
/// Iterator of tree changes that aren't in excluded paths
pub fn filter_excluded<'a>(
iter_changes: impl Iterator<Item = TreeChange> + 'a,
exclude: &'a [&'a std::path::Path],
) -> impl Iterator<Item = TreeChange> + 'a {
iter_changes.filter(|change| {
let new_excluded = if let Some(p) = change.path.1.as_ref() {
is_inside_any(exclude, p.as_path())
} else {
false
};
let old_excluded = if let Some(p) = change.path.0.as_ref() {
is_inside_any(exclude, p.as_path())
} else {
false
};
if old_excluded && new_excluded {
false
} else if old_excluded || new_excluded {
// TODO(jelmer): Perhaps raise an error here instead?
false
} else {
true
}
})
}