Show More
@@ -1,576 +1,588 b'' | |||
|
1 | 1 | use crate::utils::hg_path::HgPath; |
|
2 | 2 | use crate::utils::hg_path::HgPathBuf; |
|
3 | 3 | use crate::Revision; |
|
4 | 4 | |
|
5 | 5 | use im_rc::ordmap::DiffItem; |
|
6 | 6 | use im_rc::ordmap::OrdMap; |
|
7 | 7 | |
|
8 | 8 | use std::cmp::Ordering; |
|
9 | 9 | use std::collections::HashMap; |
|
10 | 10 | use std::convert::TryInto; |
|
11 | 11 | |
|
12 | 12 | pub type PathCopies = HashMap<HgPathBuf, HgPathBuf>; |
|
13 | 13 | |
|
14 | 14 | #[derive(Clone, Debug, PartialEq)] |
|
15 | 15 | struct TimeStampedPathCopy { |
|
16 | 16 | /// revision at which the copy information was added |
|
17 | 17 | rev: Revision, |
|
18 | 18 | /// the copy source, (Set to None in case of deletion of the associated |
|
19 | 19 | /// key) |
|
20 | 20 | path: Option<HgPathBuf>, |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | 23 | /// maps CopyDestination to Copy Source (+ a "timestamp" for the operation) |
|
24 | 24 | type TimeStampedPathCopies = OrdMap<HgPathBuf, TimeStampedPathCopy>; |
|
25 | 25 | |
|
26 | 26 | /// hold parent 1, parent 2 and relevant files actions. |
|
27 | 27 | pub type RevInfo<'a> = (Revision, Revision, ChangedFiles<'a>); |
|
28 | 28 | |
|
29 | 29 | /// represent the files affected by a changesets |
|
30 | 30 | /// |
|
31 | 31 | /// This hold a subset of mercurial.metadata.ChangingFiles as we do not need |
|
32 | 32 | /// all the data categories tracked by it. |
|
33 | 33 | /// This hold a subset of mercurial.metadata.ChangingFiles as we do not need |
|
34 | 34 | /// all the data categories tracked by it. |
|
35 | 35 | pub struct ChangedFiles<'a> { |
|
36 | 36 | nb_items: u32, |
|
37 | 37 | index: &'a [u8], |
|
38 | 38 | data: &'a [u8], |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | /// Represent active changes that affect the copy tracing. |
|
42 | 42 | enum Action<'a> { |
|
43 | 43 | /// The parent ? children edge is removing a file |
|
44 | 44 | /// |
|
45 | 45 | /// (actually, this could be the edge from the other parent, but it does |
|
46 | 46 | /// not matters) |
|
47 | 47 | Removed(&'a HgPath), |
|
48 | 48 | /// The parent ? children edge introduce copy information between (dest, |
|
49 | 49 | /// source) |
|
50 | 50 | Copied(&'a HgPath, &'a HgPath), |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | /// This express the possible "special" case we can get in a merge |
|
54 | 54 | /// |
|
55 | 55 | /// See mercurial/metadata.py for details on these values. |
|
56 | 56 | #[derive(PartialEq)] |
|
57 | 57 | enum MergeCase { |
|
58 | 58 | /// Merged: file had history on both side that needed to be merged |
|
59 | 59 | Merged, |
|
60 | 60 | /// Salvaged: file was candidate for deletion, but survived the merge |
|
61 | 61 | Salvaged, |
|
62 | 62 | /// Normal: Not one of the two cases above |
|
63 | 63 | Normal, |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | type FileChange<'a> = (u8, &'a HgPath, &'a HgPath); |
|
67 | 67 | |
|
68 | 68 | const EMPTY: &[u8] = b""; |
|
69 | 69 | const COPY_MASK: u8 = 3; |
|
70 | 70 | const P1_COPY: u8 = 2; |
|
71 | 71 | const P2_COPY: u8 = 3; |
|
72 | 72 | const ACTION_MASK: u8 = 28; |
|
73 | 73 | const REMOVED: u8 = 12; |
|
74 | 74 | const MERGED: u8 = 8; |
|
75 | 75 | const SALVAGED: u8 = 16; |
|
76 | 76 | |
|
77 | 77 | impl<'a> ChangedFiles<'a> { |
|
78 | 78 | const INDEX_START: usize = 4; |
|
79 | 79 | const ENTRY_SIZE: u32 = 9; |
|
80 | 80 | const FILENAME_START: u32 = 1; |
|
81 | 81 | const COPY_SOURCE_START: u32 = 5; |
|
82 | 82 | |
|
83 | 83 | pub fn new(data: &'a [u8]) -> Self { |
|
84 | 84 | assert!( |
|
85 | 85 | data.len() >= 4, |
|
86 | 86 | "data size ({}) is too small to contain the header (4)", |
|
87 | 87 | data.len() |
|
88 | 88 | ); |
|
89 | 89 | let nb_items_raw: [u8; 4] = (&data[0..=3]) |
|
90 | 90 | .try_into() |
|
91 | 91 | .expect("failed to turn 4 bytes into 4 bytes"); |
|
92 | 92 | let nb_items = u32::from_be_bytes(nb_items_raw); |
|
93 | 93 | |
|
94 | 94 | let index_size = (nb_items * Self::ENTRY_SIZE) as usize; |
|
95 | 95 | let index_end = Self::INDEX_START + index_size; |
|
96 | 96 | |
|
97 | 97 | assert!( |
|
98 | 98 | data.len() >= index_end, |
|
99 | 99 | "data size ({}) is too small to fit the index_data ({})", |
|
100 | 100 | data.len(), |
|
101 | 101 | index_end |
|
102 | 102 | ); |
|
103 | 103 | |
|
104 | 104 | let ret = ChangedFiles { |
|
105 | 105 | nb_items, |
|
106 | 106 | index: &data[Self::INDEX_START..index_end], |
|
107 | 107 | data: &data[index_end..], |
|
108 | 108 | }; |
|
109 | 109 | let max_data = ret.filename_end(nb_items - 1) as usize; |
|
110 | 110 | assert!( |
|
111 | 111 | ret.data.len() >= max_data, |
|
112 | 112 | "data size ({}) is too small to fit all data ({})", |
|
113 | 113 | data.len(), |
|
114 | 114 | index_end + max_data |
|
115 | 115 | ); |
|
116 | 116 | ret |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | pub fn new_empty() -> Self { |
|
120 | 120 | ChangedFiles { |
|
121 | 121 | nb_items: 0, |
|
122 | 122 | index: EMPTY, |
|
123 | 123 | data: EMPTY, |
|
124 | 124 | } |
|
125 | 125 | } |
|
126 | 126 | |
|
127 | 127 | /// internal function to return an individual entry at a given index |
|
128 | 128 | fn entry(&'a self, idx: u32) -> FileChange<'a> { |
|
129 | 129 | if idx >= self.nb_items { |
|
130 | 130 | panic!( |
|
131 | 131 | "index for entry is higher that the number of file {} >= {}", |
|
132 | 132 | idx, self.nb_items |
|
133 | 133 | ) |
|
134 | 134 | } |
|
135 | 135 | let flags = self.flags(idx); |
|
136 | 136 | let filename = self.filename(idx); |
|
137 | 137 | let copy_idx = self.copy_idx(idx); |
|
138 | 138 | let copy_source = self.filename(copy_idx); |
|
139 | 139 | (flags, filename, copy_source) |
|
140 | 140 | } |
|
141 | 141 | |
|
142 | 142 | /// internal function to return the filename of the entry at a given index |
|
143 | 143 | fn filename(&self, idx: u32) -> &HgPath { |
|
144 | 144 | let filename_start; |
|
145 | 145 | if idx == 0 { |
|
146 | 146 | filename_start = 0; |
|
147 | 147 | } else { |
|
148 | 148 | filename_start = self.filename_end(idx - 1) |
|
149 | 149 | } |
|
150 | 150 | let filename_end = self.filename_end(idx); |
|
151 | 151 | let filename_start = filename_start as usize; |
|
152 | 152 | let filename_end = filename_end as usize; |
|
153 | 153 | HgPath::new(&self.data[filename_start..filename_end]) |
|
154 | 154 | } |
|
155 | 155 | |
|
156 | 156 | /// internal function to return the flag field of the entry at a given |
|
157 | 157 | /// index |
|
158 | 158 | fn flags(&self, idx: u32) -> u8 { |
|
159 | 159 | let idx = idx as usize; |
|
160 | 160 | self.index[idx * (Self::ENTRY_SIZE as usize)] |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | /// internal function to return the end of a filename part at a given index |
|
164 | 164 | fn filename_end(&self, idx: u32) -> u32 { |
|
165 | 165 | let start = (idx * Self::ENTRY_SIZE) + Self::FILENAME_START; |
|
166 | 166 | let end = (idx * Self::ENTRY_SIZE) + Self::COPY_SOURCE_START; |
|
167 | 167 | let start = start as usize; |
|
168 | 168 | let end = end as usize; |
|
169 | 169 | let raw = (&self.index[start..end]) |
|
170 | 170 | .try_into() |
|
171 | 171 | .expect("failed to turn 4 bytes into 4 bytes"); |
|
172 | 172 | u32::from_be_bytes(raw) |
|
173 | 173 | } |
|
174 | 174 | |
|
175 | 175 | /// internal function to return index of the copy source of the entry at a |
|
176 | 176 | /// given index |
|
177 | 177 | fn copy_idx(&self, idx: u32) -> u32 { |
|
178 | 178 | let start = (idx * Self::ENTRY_SIZE) + Self::COPY_SOURCE_START; |
|
179 | 179 | let end = (idx + 1) * Self::ENTRY_SIZE; |
|
180 | 180 | let start = start as usize; |
|
181 | 181 | let end = end as usize; |
|
182 | 182 | let raw = (&self.index[start..end]) |
|
183 | 183 | .try_into() |
|
184 | 184 | .expect("failed to turn 4 bytes into 4 bytes"); |
|
185 | 185 | u32::from_be_bytes(raw) |
|
186 | 186 | } |
|
187 | 187 | |
|
188 | 188 | /// Return an iterator over all the `Action` in this instance. |
|
189 | 189 | fn iter_actions(&self, parent: Parent) -> ActionsIterator { |
|
190 | 190 | ActionsIterator { |
|
191 | 191 | changes: &self, |
|
192 | 192 | parent: parent, |
|
193 | 193 | current: 0, |
|
194 | 194 | } |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | 197 | /// return the MergeCase value associated with a filename |
|
198 | 198 | fn get_merge_case(&self, path: &HgPath) -> MergeCase { |
|
199 | 199 | if self.nb_items == 0 { |
|
200 | 200 | return MergeCase::Normal; |
|
201 | 201 | } |
|
202 | 202 | let mut low_part = 0; |
|
203 | 203 | let mut high_part = self.nb_items; |
|
204 | 204 | |
|
205 | 205 | while low_part < high_part { |
|
206 | 206 | let cursor = (low_part + high_part - 1) / 2; |
|
207 | 207 | let (flags, filename, _source) = self.entry(cursor); |
|
208 | 208 | match path.cmp(filename) { |
|
209 | 209 | Ordering::Less => low_part = cursor + 1, |
|
210 | 210 | Ordering::Greater => high_part = cursor, |
|
211 | 211 | Ordering::Equal => { |
|
212 | 212 | return match flags & ACTION_MASK { |
|
213 | 213 | MERGED => MergeCase::Merged, |
|
214 | 214 | SALVAGED => MergeCase::Salvaged, |
|
215 | 215 | _ => MergeCase::Normal, |
|
216 | 216 | }; |
|
217 | 217 | } |
|
218 | 218 | } |
|
219 | 219 | } |
|
220 | 220 | MergeCase::Normal |
|
221 | 221 | } |
|
222 | 222 | } |
|
223 | 223 | |
|
224 | 224 | /// A struct responsible for answering "is X ancestors of Y" quickly |
|
225 | 225 | /// |
|
226 | 226 | /// The structure will delegate ancestors call to a callback, and cache the |
|
227 | 227 | /// result. |
|
228 | 228 | #[derive(Debug)] |
|
229 | 229 | struct AncestorOracle<'a, A: Fn(Revision, Revision) -> bool> { |
|
230 | 230 | inner: &'a A, |
|
231 | 231 | pairs: HashMap<(Revision, Revision), bool>, |
|
232 | 232 | } |
|
233 | 233 | |
|
234 | 234 | impl<'a, A: Fn(Revision, Revision) -> bool> AncestorOracle<'a, A> { |
|
235 | 235 | fn new(func: &'a A) -> Self { |
|
236 | 236 | Self { |
|
237 | 237 | inner: func, |
|
238 | 238 | pairs: HashMap::default(), |
|
239 | 239 | } |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | 242 | /// returns `true` if `anc` is an ancestors of `desc`, `false` otherwise |
|
243 | 243 | fn is_ancestor(&mut self, anc: Revision, desc: Revision) -> bool { |
|
244 | 244 | if anc > desc { |
|
245 | 245 | false |
|
246 | 246 | } else if anc == desc { |
|
247 | 247 | true |
|
248 | 248 | } else { |
|
249 | 249 | if let Some(b) = self.pairs.get(&(anc, desc)) { |
|
250 | 250 | *b |
|
251 | 251 | } else { |
|
252 | 252 | let b = (self.inner)(anc, desc); |
|
253 | 253 | self.pairs.insert((anc, desc), b); |
|
254 | 254 | b |
|
255 | 255 | } |
|
256 | 256 | } |
|
257 | 257 | } |
|
258 | 258 | } |
|
259 | 259 | |
|
260 | 260 | struct ActionsIterator<'a> { |
|
261 | 261 | changes: &'a ChangedFiles<'a>, |
|
262 | 262 | parent: Parent, |
|
263 | 263 | current: u32, |
|
264 | 264 | } |
|
265 | 265 | |
|
266 | 266 | impl<'a> Iterator for ActionsIterator<'a> { |
|
267 | 267 | type Item = Action<'a>; |
|
268 | 268 | |
|
269 | 269 | fn next(&mut self) -> Option<Action<'a>> { |
|
270 | 270 | let copy_flag = match self.parent { |
|
271 | 271 | Parent::FirstParent => P1_COPY, |
|
272 | 272 | Parent::SecondParent => P2_COPY, |
|
273 | 273 | }; |
|
274 | 274 | while self.current < self.changes.nb_items { |
|
275 | 275 | let (flags, file, source) = self.changes.entry(self.current); |
|
276 | 276 | self.current += 1; |
|
277 | 277 | if (flags & ACTION_MASK) == REMOVED { |
|
278 | 278 | return Some(Action::Removed(file)); |
|
279 | 279 | } |
|
280 | 280 | let copy = flags & COPY_MASK; |
|
281 | 281 | if copy == copy_flag { |
|
282 | 282 | return Some(Action::Copied(file, source)); |
|
283 | 283 | } |
|
284 | 284 | } |
|
285 | 285 | return None; |
|
286 | 286 | } |
|
287 | 287 | } |
|
288 | 288 | |
|
289 | 289 | /// A small struct whose purpose is to ensure lifetime of bytes referenced in |
|
290 | 290 | /// ChangedFiles |
|
291 | 291 | /// |
|
292 | 292 | /// It is passed to the RevInfoMaker callback who can assign any necessary |
|
293 | 293 | /// content to the `data` attribute. The copy tracing code is responsible for |
|
294 | 294 | /// keeping the DataHolder alive at least as long as the ChangedFiles object. |
|
295 | 295 | pub struct DataHolder<D> { |
|
296 | 296 | /// RevInfoMaker callback should assign data referenced by the |
|
297 | 297 | /// ChangedFiles struct it return to this attribute. The DataHolder |
|
298 | 298 | /// lifetime will be at least as long as the ChangedFiles one. |
|
299 | 299 | pub data: Option<D>, |
|
300 | 300 | } |
|
301 | 301 | |
|
302 | 302 | pub type RevInfoMaker<'a, D> = |
|
303 | 303 | Box<dyn for<'r> Fn(Revision, &'r mut DataHolder<D>) -> RevInfo<'r> + 'a>; |
|
304 | 304 | |
|
305 | 305 | /// enum used to carry information about the parent β child currently processed |
|
306 | 306 | #[derive(Copy, Clone, Debug)] |
|
307 | 307 | enum Parent { |
|
308 | 308 | /// The `p1(x) β x` edge |
|
309 | 309 | FirstParent, |
|
310 | 310 | /// The `p2(x) β x` edge |
|
311 | 311 | SecondParent, |
|
312 | 312 | } |
|
313 | 313 | |
|
314 | 314 | /// Same as mercurial.copies._combine_changeset_copies, but in Rust. |
|
315 | 315 | /// |
|
316 | 316 | /// Arguments are: |
|
317 | 317 | /// |
|
318 | 318 | /// revs: all revisions to be considered |
|
319 | 319 | /// children: a {parent ? [childrens]} mapping |
|
320 | 320 | /// target_rev: the final revision we are combining copies to |
|
321 | 321 | /// rev_info(rev): callback to get revision information: |
|
322 | 322 | /// * first parent |
|
323 | 323 | /// * second parent |
|
324 | 324 | /// * ChangedFiles |
|
325 | 325 | /// isancestors(low_rev, high_rev): callback to check if a revision is an |
|
326 | 326 | /// ancestor of another |
|
327 | 327 | pub fn combine_changeset_copies<A: Fn(Revision, Revision) -> bool, D>( |
|
328 | 328 | revs: Vec<Revision>, |
|
329 | 329 | children: HashMap<Revision, Vec<Revision>>, |
|
330 | 330 | target_rev: Revision, |
|
331 | 331 | rev_info: RevInfoMaker<D>, |
|
332 | 332 | is_ancestor: &A, |
|
333 | 333 | ) -> PathCopies { |
|
334 | 334 | let mut all_copies = HashMap::new(); |
|
335 | 335 | let mut oracle = AncestorOracle::new(is_ancestor); |
|
336 | 336 | |
|
337 | 337 | for rev in revs { |
|
338 | 338 | // Retrieve data computed in a previous iteration |
|
339 | 339 | let copies = all_copies.remove(&rev); |
|
340 | 340 | let copies = match copies { |
|
341 | 341 | Some(c) => c, |
|
342 | 342 | None => TimeStampedPathCopies::default(), // root of the walked set |
|
343 | 343 | }; |
|
344 | 344 | |
|
345 | 345 | let current_children = match children.get(&rev) { |
|
346 | 346 | Some(c) => c, |
|
347 | 347 | None => panic!("inconsistent `revs` and `children`"), |
|
348 | 348 | }; |
|
349 | 349 | |
|
350 | 350 | for child in current_children { |
|
351 | 351 | // We will chain the copies information accumulated for `rev` with |
|
352 | 352 | // the individual copies information for each of its children. |
|
353 | 353 | // Creating a new PathCopies for each `rev` β `children` vertex. |
|
354 | 354 | let mut d: DataHolder<D> = DataHolder { data: None }; |
|
355 | 355 | let (p1, p2, changes) = rev_info(*child, &mut d); |
|
356 | 356 | |
|
357 | 357 | let parent = if rev == p1 { |
|
358 | 358 | Parent::FirstParent |
|
359 | 359 | } else { |
|
360 | 360 | assert_eq!(rev, p2); |
|
361 | 361 | Parent::SecondParent |
|
362 | 362 | }; |
|
363 |
let |
|
|
364 | ||
|
365 | for action in changes.iter_actions(parent) { | |
|
366 | match action { | |
|
367 | Action::Copied(dest, source) => { | |
|
368 | let entry; | |
|
369 | if let Some(v) = copies.get(source) { | |
|
370 | entry = match &v.path { | |
|
371 | Some(path) => Some((*(path)).to_owned()), | |
|
372 | None => Some(source.to_owned()), | |
|
373 | } | |
|
374 | } else { | |
|
375 | entry = Some(source.to_owned()); | |
|
376 | } | |
|
377 | // Each new entry is introduced by the children, we | |
|
378 | // record this information as we will need it to take | |
|
379 | // the right decision when merging conflicting copy | |
|
380 | // information. See merge_copies_dict for details. | |
|
381 | let ttpc = TimeStampedPathCopy { | |
|
382 | rev: *child, | |
|
383 | path: entry, | |
|
384 | }; | |
|
385 | new_copies.insert(dest.to_owned(), ttpc); | |
|
386 | } | |
|
387 | Action::Removed(f) => { | |
|
388 | // We must drop copy information for removed file. | |
|
389 | // | |
|
390 | // We need to explicitly record them as dropped to | |
|
391 | // propagate this information when merging two | |
|
392 | // TimeStampedPathCopies object. | |
|
393 | if new_copies.contains_key(f.as_ref()) { | |
|
394 | let ttpc = TimeStampedPathCopy { | |
|
395 | rev: *child, | |
|
396 | path: None, | |
|
397 | }; | |
|
398 | new_copies.insert(f.to_owned(), ttpc); | |
|
399 | } | |
|
400 | } | |
|
401 | } | |
|
402 | } | |
|
363 | let new_copies = | |
|
364 | add_from_changes(&copies, &changes, parent, *child); | |
|
403 | 365 | |
|
404 | 366 | // Merge has two parents needs to combines their copy information. |
|
405 | 367 | // |
|
406 | 368 | // If the vertex from the other parent was already processed, we |
|
407 | 369 | // will have a value for the child ready to be used. We need to |
|
408 | 370 | // grab it and combine it with the one we already |
|
409 | 371 | // computed. If not we can simply store the newly |
|
410 | 372 | // computed data. The processing happening at |
|
411 | 373 | // the time of the second parent will take care of combining the |
|
412 | 374 | // two TimeStampedPathCopies instance. |
|
413 | 375 | match all_copies.remove(child) { |
|
414 | 376 | None => { |
|
415 | 377 | all_copies.insert(child, new_copies); |
|
416 | 378 | } |
|
417 | 379 | Some(other_copies) => { |
|
418 | 380 | let (minor, major) = match parent { |
|
419 | 381 | Parent::FirstParent => (other_copies, new_copies), |
|
420 | 382 | Parent::SecondParent => (new_copies, other_copies), |
|
421 | 383 | }; |
|
422 | 384 | let merged_copies = |
|
423 | 385 | merge_copies_dict(minor, major, &changes, &mut oracle); |
|
424 | 386 | all_copies.insert(child, merged_copies); |
|
425 | 387 | } |
|
426 | 388 | }; |
|
427 | 389 | } |
|
428 | 390 | } |
|
429 | 391 | |
|
430 | 392 | // Drop internal information (like the timestamp) and return the final |
|
431 | 393 | // mapping. |
|
432 | 394 | let tt_result = all_copies |
|
433 | 395 | .remove(&target_rev) |
|
434 | 396 | .expect("target revision was not processed"); |
|
435 | 397 | let mut result = PathCopies::default(); |
|
436 | 398 | for (dest, tt_source) in tt_result { |
|
437 | 399 | if let Some(path) = tt_source.path { |
|
438 | 400 | result.insert(dest, path); |
|
439 | 401 | } |
|
440 | 402 | } |
|
441 | 403 | result |
|
442 | 404 | } |
|
443 | 405 | |
|
406 | /// Combine ChangedFiles with some existing PathCopies information and return | |
|
407 | /// the result | |
|
408 | fn add_from_changes( | |
|
409 | base_copies: &TimeStampedPathCopies, | |
|
410 | changes: &ChangedFiles, | |
|
411 | parent: Parent, | |
|
412 | current_rev: Revision, | |
|
413 | ) -> TimeStampedPathCopies { | |
|
414 | let mut copies = base_copies.clone(); | |
|
415 | for action in changes.iter_actions(parent) { | |
|
416 | match action { | |
|
417 | Action::Copied(dest, source) => { | |
|
418 | let entry; | |
|
419 | if let Some(v) = base_copies.get(source) { | |
|
420 | entry = match &v.path { | |
|
421 | Some(path) => Some((*(path)).to_owned()), | |
|
422 | None => Some(source.to_owned()), | |
|
423 | } | |
|
424 | } else { | |
|
425 | entry = Some(source.to_owned()); | |
|
426 | } | |
|
427 | // Each new entry is introduced by the children, we | |
|
428 | // record this information as we will need it to take | |
|
429 | // the right decision when merging conflicting copy | |
|
430 | // information. See merge_copies_dict for details. | |
|
431 | let ttpc = TimeStampedPathCopy { | |
|
432 | rev: current_rev, | |
|
433 | path: entry, | |
|
434 | }; | |
|
435 | copies.insert(dest.to_owned(), ttpc); | |
|
436 | } | |
|
437 | Action::Removed(f) => { | |
|
438 | // We must drop copy information for removed file. | |
|
439 | // | |
|
440 | // We need to explicitly record them as dropped to | |
|
441 | // propagate this information when merging two | |
|
442 | // TimeStampedPathCopies object. | |
|
443 | if copies.contains_key(f.as_ref()) { | |
|
444 | let ttpc = TimeStampedPathCopy { | |
|
445 | rev: current_rev, | |
|
446 | path: None, | |
|
447 | }; | |
|
448 | copies.insert(f.to_owned(), ttpc); | |
|
449 | } | |
|
450 | } | |
|
451 | } | |
|
452 | } | |
|
453 | copies | |
|
454 | } | |
|
455 | ||
|
444 | 456 | /// merge two copies-mapping together, minor and major |
|
445 | 457 | /// |
|
446 | 458 | /// In case of conflict, value from "major" will be picked, unless in some |
|
447 | 459 | /// cases. See inline documentation for details. |
|
448 | 460 | #[allow(clippy::if_same_then_else)] |
|
449 | 461 | fn merge_copies_dict<A: Fn(Revision, Revision) -> bool>( |
|
450 | 462 | minor: TimeStampedPathCopies, |
|
451 | 463 | major: TimeStampedPathCopies, |
|
452 | 464 | changes: &ChangedFiles, |
|
453 | 465 | oracle: &mut AncestorOracle<A>, |
|
454 | 466 | ) -> TimeStampedPathCopies { |
|
455 | 467 | if minor.is_empty() { |
|
456 | 468 | return major; |
|
457 | 469 | } else if major.is_empty() { |
|
458 | 470 | return minor; |
|
459 | 471 | } |
|
460 | 472 | let mut override_minor = Vec::new(); |
|
461 | 473 | let mut override_major = Vec::new(); |
|
462 | 474 | |
|
463 | 475 | let mut to_major = |k: &HgPathBuf, v: &TimeStampedPathCopy| { |
|
464 | 476 | override_major.push((k.clone(), v.clone())) |
|
465 | 477 | }; |
|
466 | 478 | let mut to_minor = |k: &HgPathBuf, v: &TimeStampedPathCopy| { |
|
467 | 479 | override_minor.push((k.clone(), v.clone())) |
|
468 | 480 | }; |
|
469 | 481 | |
|
470 | 482 | // The diff function leverage detection of the identical subpart if minor |
|
471 | 483 | // and major has some common ancestors. This make it very fast is most |
|
472 | 484 | // case. |
|
473 | 485 | // |
|
474 | 486 | // In case where the two map are vastly different in size, the current |
|
475 | 487 | // approach is still slowish because the iteration will iterate over |
|
476 | 488 | // all the "exclusive" content of the larger on. This situation can be |
|
477 | 489 | // frequent when the subgraph of revision we are processing has a lot |
|
478 | 490 | // of roots. Each roots adding they own fully new map to the mix (and |
|
479 | 491 | // likely a small map, if the path from the root to the "main path" is |
|
480 | 492 | // small. |
|
481 | 493 | // |
|
482 | 494 | // We could do better by detecting such situation and processing them |
|
483 | 495 | // differently. |
|
484 | 496 | for d in minor.diff(&major) { |
|
485 | 497 | match d { |
|
486 | 498 | DiffItem::Add(k, v) => to_minor(k, v), |
|
487 | 499 | DiffItem::Remove(k, v) => to_major(k, v), |
|
488 | 500 | DiffItem::Update { old, new } => { |
|
489 | 501 | let (dest, src_major) = new; |
|
490 | 502 | let (_, src_minor) = old; |
|
491 | 503 | let mut pick_minor = || (to_major(dest, src_minor)); |
|
492 | 504 | let mut pick_major = || (to_minor(dest, src_major)); |
|
493 | 505 | if src_major.path == src_minor.path { |
|
494 | 506 | // we have the same value, but from other source; |
|
495 | 507 | if src_major.rev == src_minor.rev { |
|
496 | 508 | // If the two entry are identical, no need to do |
|
497 | 509 | // anything (but diff should not have yield them) |
|
498 | 510 | unreachable!(); |
|
499 | 511 | } else if oracle.is_ancestor(src_major.rev, src_minor.rev) |
|
500 | 512 | { |
|
501 | 513 | pick_minor(); |
|
502 | 514 | } else { |
|
503 | 515 | pick_major(); |
|
504 | 516 | } |
|
505 | 517 | } else if src_major.rev == src_minor.rev { |
|
506 | 518 | // We cannot get copy information for both p1 and p2 in the |
|
507 | 519 | // same rev. So this is the same value. |
|
508 | 520 | unreachable!(); |
|
509 | 521 | } else { |
|
510 | 522 | let action = changes.get_merge_case(&dest); |
|
511 | 523 | if src_major.path.is_none() |
|
512 | 524 | && action == MergeCase::Salvaged |
|
513 | 525 | { |
|
514 | 526 | // If the file is "deleted" in the major side but was |
|
515 | 527 | // salvaged by the merge, we keep the minor side alive |
|
516 | 528 | pick_minor(); |
|
517 | 529 | } else if src_minor.path.is_none() |
|
518 | 530 | && action == MergeCase::Salvaged |
|
519 | 531 | { |
|
520 | 532 | // If the file is "deleted" in the minor side but was |
|
521 | 533 | // salvaged by the merge, unconditionnaly preserve the |
|
522 | 534 | // major side. |
|
523 | 535 | pick_major(); |
|
524 | 536 | } else if action == MergeCase::Merged { |
|
525 | 537 | // If the file was actively merged, copy information |
|
526 | 538 | // from each side might conflict. The major side will |
|
527 | 539 | // win such conflict. |
|
528 | 540 | pick_major(); |
|
529 | 541 | } else if oracle.is_ancestor(src_major.rev, src_minor.rev) |
|
530 | 542 | { |
|
531 | 543 | // If the minor side is strictly newer than the major |
|
532 | 544 | // side, it should be kept. |
|
533 | 545 | pick_minor(); |
|
534 | 546 | } else if src_major.path.is_some() { |
|
535 | 547 | // without any special case, the "major" value win |
|
536 | 548 | // other the "minor" one. |
|
537 | 549 | pick_major(); |
|
538 | 550 | } else if oracle.is_ancestor(src_minor.rev, src_major.rev) |
|
539 | 551 | { |
|
540 | 552 | // the "major" rev is a direct ancestors of "minor", |
|
541 | 553 | // any different value should |
|
542 | 554 | // overwrite |
|
543 | 555 | pick_major(); |
|
544 | 556 | } else { |
|
545 | 557 | // major version is None (so the file was deleted on |
|
546 | 558 | // that branch) and that branch is independant (neither |
|
547 | 559 | // minor nor major is an ancestors of the other one.) |
|
548 | 560 | // We preserve the new |
|
549 | 561 | // information about the new file. |
|
550 | 562 | pick_minor(); |
|
551 | 563 | } |
|
552 | 564 | } |
|
553 | 565 | } |
|
554 | 566 | }; |
|
555 | 567 | } |
|
556 | 568 | |
|
557 | 569 | let updates; |
|
558 | 570 | let mut result; |
|
559 | 571 | if override_major.is_empty() { |
|
560 | 572 | result = major |
|
561 | 573 | } else if override_minor.is_empty() { |
|
562 | 574 | result = minor |
|
563 | 575 | } else { |
|
564 | 576 | if override_minor.len() < override_major.len() { |
|
565 | 577 | updates = override_minor; |
|
566 | 578 | result = minor; |
|
567 | 579 | } else { |
|
568 | 580 | updates = override_major; |
|
569 | 581 | result = major; |
|
570 | 582 | } |
|
571 | 583 | for (k, v) in updates { |
|
572 | 584 | result.insert(k, v); |
|
573 | 585 | } |
|
574 | 586 | } |
|
575 | 587 | result |
|
576 | 588 | } |
General Comments 0
You need to be logged in to leave comments.
Login now