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