##// END OF EJS Templates
copies-rust: leverage the immutability for efficient update...
marmoute -
r46585:cc759d3d default
parent child Browse files
Show More
@@ -1,269 +1,316
1 1 use crate::utils::hg_path::HgPathBuf;
2 2 use crate::Revision;
3 3
4 use im_rc::ordmap::DiffItem;
4 5 use im_rc::ordmap::OrdMap;
5 6
6 7 use std::collections::HashMap;
7 8 use std::collections::HashSet;
8 9
9 10 pub type PathCopies = HashMap<HgPathBuf, HgPathBuf>;
10 11
11 #[derive(Clone, Debug)]
12 #[derive(Clone, Debug, PartialEq)]
12 13 struct TimeStampedPathCopy {
13 14 /// revision at which the copy information was added
14 15 rev: Revision,
15 16 /// the copy source, (Set to None in case of deletion of the associated
16 17 /// key)
17 18 path: Option<HgPathBuf>,
18 19 }
19 20
20 21 /// maps CopyDestination to Copy Source (+ a "timestamp" for the operation)
21 22 type TimeStampedPathCopies = OrdMap<HgPathBuf, TimeStampedPathCopy>;
22 23
23 24 /// hold parent 1, parent 2 and relevant files actions.
24 25 pub type RevInfo = (Revision, Revision, ChangedFiles);
25 26
26 27 /// represent the files affected by a changesets
27 28 ///
28 29 /// This hold a subset of mercurial.metadata.ChangingFiles as we do not need
29 30 /// all the data categories tracked by it.
30 31 pub struct ChangedFiles {
31 32 removed: HashSet<HgPathBuf>,
32 33 merged: HashSet<HgPathBuf>,
33 34 salvaged: HashSet<HgPathBuf>,
34 35 copied_from_p1: PathCopies,
35 36 copied_from_p2: PathCopies,
36 37 }
37 38
38 39 impl ChangedFiles {
39 40 pub fn new(
40 41 removed: HashSet<HgPathBuf>,
41 42 merged: HashSet<HgPathBuf>,
42 43 salvaged: HashSet<HgPathBuf>,
43 44 copied_from_p1: PathCopies,
44 45 copied_from_p2: PathCopies,
45 46 ) -> Self {
46 47 ChangedFiles {
47 48 removed,
48 49 merged,
49 50 salvaged,
50 51 copied_from_p1,
51 52 copied_from_p2,
52 53 }
53 54 }
54 55
55 56 pub fn new_empty() -> Self {
56 57 ChangedFiles {
57 58 removed: HashSet::new(),
58 59 merged: HashSet::new(),
59 60 salvaged: HashSet::new(),
60 61 copied_from_p1: PathCopies::new(),
61 62 copied_from_p2: PathCopies::new(),
62 63 }
63 64 }
64 65 }
65 66
66 67 /// Same as mercurial.copies._combine_changeset_copies, but in Rust.
67 68 ///
68 69 /// Arguments are:
69 70 ///
70 71 /// revs: all revisions to be considered
71 72 /// children: a {parent ? [childrens]} mapping
72 73 /// target_rev: the final revision we are combining copies to
73 74 /// rev_info(rev): callback to get revision information:
74 75 /// * first parent
75 76 /// * second parent
76 77 /// * ChangedFiles
77 78 /// isancestors(low_rev, high_rev): callback to check if a revision is an
78 79 /// ancestor of another
79 80 pub fn combine_changeset_copies(
80 81 revs: Vec<Revision>,
81 82 children: HashMap<Revision, Vec<Revision>>,
82 83 target_rev: Revision,
83 84 rev_info: &impl Fn(Revision) -> RevInfo,
84 85 is_ancestor: &impl Fn(Revision, Revision) -> bool,
85 86 ) -> PathCopies {
86 87 let mut all_copies = HashMap::new();
87 88
88 89 for rev in revs {
89 90 // Retrieve data computed in a previous iteration
90 91 let copies = all_copies.remove(&rev);
91 92 let copies = match copies {
92 93 Some(c) => c,
93 94 None => TimeStampedPathCopies::default(), // root of the walked set
94 95 };
95 96
96 97 let current_children = match children.get(&rev) {
97 98 Some(c) => c,
98 99 None => panic!("inconsistent `revs` and `children`"),
99 100 };
100 101
101 102 for child in current_children {
102 103 // We will chain the copies information accumulated for `rev` with
103 104 // the individual copies information for each of its children.
104 105 // Creating a new PathCopies for each `rev` ? `children` vertex.
105 106 let (p1, p2, changes) = rev_info(*child);
106 107
107 108 let (parent, child_copies) = if rev == p1 {
108 109 (1, &changes.copied_from_p1)
109 110 } else {
110 111 assert_eq!(rev, p2);
111 112 (2, &changes.copied_from_p2)
112 113 };
113 114 let mut new_copies = copies.clone();
114 115
115 116 for (dest, source) in child_copies {
116 117 let entry;
117 118 if let Some(v) = copies.get(source) {
118 119 entry = match &v.path {
119 120 Some(path) => Some((*(path)).to_owned()),
120 121 None => Some(source.to_owned()),
121 122 }
122 123 } else {
123 124 entry = Some(source.to_owned());
124 125 }
125 126 // Each new entry is introduced by the children, we record this
126 127 // information as we will need it to take the right decision
127 128 // when merging conflicting copy information. See
128 129 // merge_copies_dict for details.
129 130 let ttpc = TimeStampedPathCopy {
130 131 rev: *child,
131 132 path: entry,
132 133 };
133 134 new_copies.insert(dest.to_owned(), ttpc);
134 135 }
135 136
136 137 // We must drop copy information for removed file.
137 138 //
138 139 // We need to explicitly record them as dropped to propagate this
139 140 // information when merging two TimeStampedPathCopies object.
140 141 for f in changes.removed.iter() {
141 142 if new_copies.contains_key(f.as_ref()) {
142 143 let ttpc = TimeStampedPathCopy {
143 144 rev: *child,
144 145 path: None,
145 146 };
146 147 new_copies.insert(f.to_owned(), ttpc);
147 148 }
148 149 }
149 150
150 151 // Merge has two parents needs to combines their copy information.
151 152 //
152 153 // If the vertex from the other parent was already processed, we
153 154 // will have a value for the child ready to be used. We need to
154 155 // grab it and combine it with the one we already
155 156 // computed. If not we can simply store the newly
156 157 // computed data. The processing happening at
157 158 // the time of the second parent will take care of combining the
158 159 // two TimeStampedPathCopies instance.
159 160 match all_copies.remove(child) {
160 161 None => {
161 162 all_copies.insert(child, new_copies);
162 163 }
163 164 Some(other_copies) => {
164 165 let (minor, major) = match parent {
165 166 1 => (other_copies, new_copies),
166 167 2 => (new_copies, other_copies),
167 168 _ => unreachable!(),
168 169 };
169 170 let merged_copies =
170 171 merge_copies_dict(minor, major, &changes, is_ancestor);
171 172 all_copies.insert(child, merged_copies);
172 173 }
173 174 };
174 175 }
175 176 }
176 177
177 178 // Drop internal information (like the timestamp) and return the final
178 179 // mapping.
179 180 let tt_result = all_copies
180 181 .remove(&target_rev)
181 182 .expect("target revision was not processed");
182 183 let mut result = PathCopies::default();
183 184 for (dest, tt_source) in tt_result {
184 185 if let Some(path) = tt_source.path {
185 186 result.insert(dest, path);
186 187 }
187 188 }
188 189 result
189 190 }
190 191
191 192 /// merge two copies-mapping together, minor and major
192 193 ///
193 194 /// In case of conflict, value from "major" will be picked, unless in some
194 195 /// cases. See inline documentation for details.
195 196 #[allow(clippy::if_same_then_else)]
196 197 fn merge_copies_dict(
197 198 minor: TimeStampedPathCopies,
198 199 major: TimeStampedPathCopies,
199 200 changes: &ChangedFiles,
200 201 is_ancestor: &impl Fn(Revision, Revision) -> bool,
201 202 ) -> TimeStampedPathCopies {
202 let mut result = minor.clone();
203 for (dest, src_major) in major {
204 let overwrite;
205 if let Some(src_minor) = minor.get(&dest) {
206 {
203 if minor.is_empty() {
204 return major;
205 } else if major.is_empty() {
206 return minor;
207 }
208 let mut override_minor = Vec::new();
209 let mut override_major = Vec::new();
210
211 let mut to_major = |k: &HgPathBuf, v: &TimeStampedPathCopy| {
212 override_major.push((k.clone(), v.clone()))
213 };
214 let mut to_minor = |k: &HgPathBuf, v: &TimeStampedPathCopy| {
215 override_minor.push((k.clone(), v.clone()))
216 };
217
218 // The diff function leverage detection of the identical subpart if minor
219 // and major has some common ancestors. This make it very fast is most
220 // case.
221 //
222 // In case where the two map are vastly different in size, the current
223 // approach is still slowish because the iteration will iterate over
224 // all the "exclusive" content of the larger on. This situation can be
225 // frequent when the subgraph of revision we are processing has a lot
226 // of roots. Each roots adding they own fully new map to the mix (and
227 // likely a small map, if the path from the root to the "main path" is
228 // small.
229 //
230 // We could do better by detecting such situation and processing them
231 // differently.
232 for d in minor.diff(&major) {
233 match d {
234 DiffItem::Add(k, v) => to_minor(k, v),
235 DiffItem::Remove(k, v) => to_major(k, v),
236 DiffItem::Update { old, new } => {
237 let (dest, src_major) = new;
238 let (_, src_minor) = old;
239 let mut pick_minor = || (to_major(dest, src_minor));
240 let mut pick_major = || (to_minor(dest, src_major));
207 241 if src_major.path == src_minor.path {
208 // we have the same value, no need to battle;
242 // we have the same value, but from other source;
209 243 if src_major.rev == src_minor.rev {
210 244 // If the two entry are identical, no need to do
211 // anything
212 overwrite = false;
245 // anything (but diff should not have yield them)
246 unreachable!();
213 247 } else if is_ancestor(src_major.rev, src_minor.rev) {
214 overwrite = false;
248 pick_minor();
215 249 } else {
216 overwrite = true;
250 pick_major();
217 251 }
218 252 } else if src_major.rev == src_minor.rev {
219 253 // We cannot get copy information for both p1 and p2 in the
220 254 // same rev. So this is the same value.
221 overwrite = false;
255 unreachable!();
222 256 } else if src_major.path.is_none()
223 && changes.salvaged.contains(&dest)
257 && changes.salvaged.contains(dest)
224 258 {
225 259 // If the file is "deleted" in the major side but was
226 260 // salvaged by the merge, we keep the minor side alive
227 overwrite = false;
261 pick_minor();
228 262 } else if src_minor.path.is_none()
229 && changes.salvaged.contains(&dest)
263 && changes.salvaged.contains(dest)
230 264 {
231 265 // If the file is "deleted" in the minor side but was
232 266 // salvaged by the merge, unconditionnaly preserve the
233 267 // major side.
234 overwrite = true;
235 } else if changes.merged.contains(&dest) {
268 pick_major();
269 } else if changes.merged.contains(dest) {
236 270 // If the file was actively merged, copy information from
237 271 // each side might conflict. The major side will win such
238 272 // conflict.
239 overwrite = true;
273 pick_major();
240 274 } else if is_ancestor(src_major.rev, src_minor.rev) {
241 275 // If the minor side is strictly newer than the major side,
242 276 // it should be kept.
243 overwrite = false;
277 pick_minor();
244 278 } else if src_major.path.is_some() {
245 279 // without any special case, the "major" value win other
246 280 // the "minor" one.
247 overwrite = true;
281 pick_major();
248 282 } else if is_ancestor(src_minor.rev, src_major.rev) {
249 283 // the "major" rev is a direct ancestors of "minor", any
250 284 // different value should overwrite
251 overwrite = true;
285 pick_major();
252 286 } else {
253 287 // major version is None (so the file was deleted on that
254 288 // branch) and that branch is independant (neither minor
255 289 // nor major is an ancestors of the other one.) We preserve
256 290 // the new information about the new file.
257 overwrite = false;
291 pick_minor();
258 292 }
259 293 }
294 };
295 }
296
297 let updates;
298 let mut result;
299 if override_major.is_empty() {
300 result = major
301 } else if override_minor.is_empty() {
302 result = minor
303 } else {
304 if override_minor.len() < override_major.len() {
305 updates = override_minor;
306 result = minor;
260 307 } else {
261 // minor had no value
262 overwrite = true;
308 updates = override_major;
309 result = major;
263 310 }
264 if overwrite {
265 result.insert(dest, src_major);
311 for (k, v) in updates {
312 result.insert(k, v);
266 313 }
267 314 }
268 315 result
269 316 }
General Comments 0
You need to be logged in to leave comments. Login now