Show More
@@ -1,358 +1,357 b'' | |||
|
1 | 1 | # copies.py - copy detection for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2008 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | import util |
|
9 | 9 | import heapq |
|
10 | 10 | |
|
11 | 11 | def _nonoverlap(d1, d2, d3): |
|
12 | 12 | "Return list of elements in d1 not in d2 or d3" |
|
13 | 13 | return sorted([d for d in d1 if d not in d3 and d not in d2]) |
|
14 | 14 | |
|
15 | 15 | def _dirname(f): |
|
16 | 16 | s = f.rfind("/") |
|
17 | 17 | if s == -1: |
|
18 | 18 | return "" |
|
19 | 19 | return f[:s] |
|
20 | 20 | |
|
21 | 21 | def _dirs(files): |
|
22 | 22 | d = set() |
|
23 | 23 | for f in files: |
|
24 | 24 | f = _dirname(f) |
|
25 | 25 | while f not in d: |
|
26 | 26 | d.add(f) |
|
27 | 27 | f = _dirname(f) |
|
28 | 28 | return d |
|
29 | 29 | |
|
30 | 30 | def _findlimit(repo, a, b): |
|
31 | 31 | """Find the earliest revision that's an ancestor of a or b but not both, |
|
32 | 32 | None if no such revision exists. |
|
33 | 33 | """ |
|
34 | 34 | # basic idea: |
|
35 | 35 | # - mark a and b with different sides |
|
36 | 36 | # - if a parent's children are all on the same side, the parent is |
|
37 | 37 | # on that side, otherwise it is on no side |
|
38 | 38 | # - walk the graph in topological order with the help of a heap; |
|
39 | 39 | # - add unseen parents to side map |
|
40 | 40 | # - clear side of any parent that has children on different sides |
|
41 | 41 | # - track number of interesting revs that might still be on a side |
|
42 | 42 | # - track the lowest interesting rev seen |
|
43 | 43 | # - quit when interesting revs is zero |
|
44 | 44 | |
|
45 | 45 | cl = repo.changelog |
|
46 | 46 | working = len(cl) # pseudo rev for the working directory |
|
47 | 47 | if a is None: |
|
48 | 48 | a = working |
|
49 | 49 | if b is None: |
|
50 | 50 | b = working |
|
51 | 51 | |
|
52 | 52 | side = {a: -1, b: 1} |
|
53 | 53 | visit = [-a, -b] |
|
54 | 54 | heapq.heapify(visit) |
|
55 | 55 | interesting = len(visit) |
|
56 | 56 | hascommonancestor = False |
|
57 | 57 | limit = working |
|
58 | 58 | |
|
59 | 59 | while interesting: |
|
60 | 60 | r = -heapq.heappop(visit) |
|
61 | 61 | if r == working: |
|
62 | 62 | parents = [cl.rev(p) for p in repo.dirstate.parents()] |
|
63 | 63 | else: |
|
64 | 64 | parents = cl.parentrevs(r) |
|
65 | 65 | for p in parents: |
|
66 | 66 | if p < 0: |
|
67 | 67 | continue |
|
68 | 68 | if p not in side: |
|
69 | 69 | # first time we see p; add it to visit |
|
70 | 70 | side[p] = side[r] |
|
71 | 71 | if side[p]: |
|
72 | 72 | interesting += 1 |
|
73 | 73 | heapq.heappush(visit, -p) |
|
74 | 74 | elif side[p] and side[p] != side[r]: |
|
75 | 75 | # p was interesting but now we know better |
|
76 | 76 | side[p] = 0 |
|
77 | 77 | interesting -= 1 |
|
78 | 78 | hascommonancestor = True |
|
79 | 79 | if side[r]: |
|
80 | 80 | limit = r # lowest rev visited |
|
81 | 81 | interesting -= 1 |
|
82 | 82 | |
|
83 | 83 | if not hascommonancestor: |
|
84 | 84 | return None |
|
85 | 85 | return limit |
|
86 | 86 | |
|
87 | 87 | def _chain(src, dst, a, b): |
|
88 | 88 | '''chain two sets of copies a->b''' |
|
89 | 89 | t = a.copy() |
|
90 | 90 | for k, v in b.iteritems(): |
|
91 | 91 | if v in t: |
|
92 | 92 | # found a chain |
|
93 | 93 | if t[v] != k: |
|
94 | 94 | # file wasn't renamed back to itself |
|
95 | 95 | t[k] = t[v] |
|
96 | 96 | if v not in dst: |
|
97 | 97 | # chain was a rename, not a copy |
|
98 | 98 | del t[v] |
|
99 | 99 | if v in src: |
|
100 | 100 | # file is a copy of an existing file |
|
101 | 101 | t[k] = v |
|
102 | 102 | |
|
103 | 103 | # remove criss-crossed copies |
|
104 | 104 | for k, v in t.items(): |
|
105 | 105 | if k in src and v in dst: |
|
106 | print "bad", k, v | |
|
107 | 106 | del t[k] |
|
108 | 107 | |
|
109 | 108 | return t |
|
110 | 109 | |
|
111 | 110 | def _tracefile(fctx, actx): |
|
112 | 111 | '''return file context that is the ancestor of fctx present in actx''' |
|
113 | 112 | stop = actx.rev() |
|
114 | 113 | am = actx.manifest() |
|
115 | 114 | |
|
116 | 115 | for f in fctx.ancestors(): |
|
117 | 116 | if am.get(f.path(), None) == f.filenode(): |
|
118 | 117 | return f |
|
119 | 118 | if f.rev() < stop: |
|
120 | 119 | return None |
|
121 | 120 | |
|
122 | 121 | def _dirstatecopies(d): |
|
123 | 122 | ds = d._repo.dirstate |
|
124 | 123 | c = ds.copies().copy() |
|
125 | 124 | for k in c.keys(): |
|
126 | 125 | if ds[k] not in 'anm': |
|
127 | 126 | del c[k] |
|
128 | 127 | return c |
|
129 | 128 | |
|
130 | 129 | def _forwardcopies(a, b): |
|
131 | 130 | '''find {dst@b: src@a} copy mapping where a is an ancestor of b''' |
|
132 | 131 | |
|
133 | 132 | # check for working copy |
|
134 | 133 | w = None |
|
135 | 134 | if b.rev() is None: |
|
136 | 135 | w = b |
|
137 | 136 | b = w.p1() |
|
138 | 137 | if a == b: |
|
139 | 138 | # short-circuit to avoid issues with merge states |
|
140 | 139 | return _dirstatecopies(w) |
|
141 | 140 | |
|
142 | 141 | # find where new files came from |
|
143 | 142 | # we currently don't try to find where old files went, too expensive |
|
144 | 143 | # this means we can miss a case like 'hg rm b; hg cp a b' |
|
145 | 144 | cm = {} |
|
146 | 145 | for f in b: |
|
147 | 146 | if f not in a: |
|
148 | 147 | ofctx = _tracefile(b[f], a) |
|
149 | 148 | if ofctx: |
|
150 | 149 | cm[f] = ofctx.path() |
|
151 | 150 | |
|
152 | 151 | # combine copies from dirstate if necessary |
|
153 | 152 | if w is not None: |
|
154 | 153 | cm = _chain(a, w, cm, _dirstatecopies(w)) |
|
155 | 154 | |
|
156 | 155 | return cm |
|
157 | 156 | |
|
158 | 157 | def _backwardcopies(a, b): |
|
159 | 158 | # because the forward mapping is 1:n, we can lose renames here |
|
160 | 159 | # in particular, we find renames better than copies |
|
161 | 160 | f = _forwardcopies(b, a) |
|
162 | 161 | r = {} |
|
163 | 162 | for k, v in f.iteritems(): |
|
164 | 163 | r[v] = k |
|
165 | 164 | return r |
|
166 | 165 | |
|
167 | 166 | def pathcopies(x, y): |
|
168 | 167 | '''find {dst@y: src@x} copy mapping for directed compare''' |
|
169 | 168 | if x == y or not x or not y: |
|
170 | 169 | return {} |
|
171 | 170 | a = y.ancestor(x) |
|
172 | 171 | if a == x: |
|
173 | 172 | return _forwardcopies(x, y) |
|
174 | 173 | if a == y: |
|
175 | 174 | return _backwardcopies(x, y) |
|
176 | 175 | return _chain(x, y, _backwardcopies(x, a), _forwardcopies(a, y)) |
|
177 | 176 | |
|
178 | 177 | def mergecopies(repo, c1, c2, ca, checkdirs=True): |
|
179 | 178 | """ |
|
180 | 179 | Find moves and copies between context c1 and c2 |
|
181 | 180 | """ |
|
182 | 181 | # avoid silly behavior for update from empty dir |
|
183 | 182 | if not c1 or not c2 or c1 == c2: |
|
184 | 183 | return {}, {} |
|
185 | 184 | |
|
186 | 185 | # avoid silly behavior for parent -> working dir |
|
187 | 186 | if c2.node() is None and c1.node() == repo.dirstate.p1(): |
|
188 | 187 | return repo.dirstate.copies(), {} |
|
189 | 188 | |
|
190 | 189 | limit = _findlimit(repo, c1.rev(), c2.rev()) |
|
191 | 190 | if limit is None: |
|
192 | 191 | # no common ancestor, no copies |
|
193 | 192 | return {}, {} |
|
194 | 193 | m1 = c1.manifest() |
|
195 | 194 | m2 = c2.manifest() |
|
196 | 195 | ma = ca.manifest() |
|
197 | 196 | |
|
198 | 197 | def makectx(f, n): |
|
199 | 198 | if len(n) != 20: # in a working context? |
|
200 | 199 | if c1.rev() is None: |
|
201 | 200 | return c1.filectx(f) |
|
202 | 201 | return c2.filectx(f) |
|
203 | 202 | return repo.filectx(f, fileid=n) |
|
204 | 203 | |
|
205 | 204 | ctx = util.lrucachefunc(makectx) |
|
206 | 205 | copy = {} |
|
207 | 206 | fullcopy = {} |
|
208 | 207 | diverge = {} |
|
209 | 208 | |
|
210 | 209 | def related(f1, f2, limit): |
|
211 | 210 | # Walk back to common ancestor to see if the two files originate |
|
212 | 211 | # from the same file. Since workingfilectx's rev() is None it messes |
|
213 | 212 | # up the integer comparison logic, hence the pre-step check for |
|
214 | 213 | # None (f1 and f2 can only be workingfilectx's initially). |
|
215 | 214 | |
|
216 | 215 | if f1 == f2: |
|
217 | 216 | return f1 # a match |
|
218 | 217 | |
|
219 | 218 | g1, g2 = f1.ancestors(), f2.ancestors() |
|
220 | 219 | try: |
|
221 | 220 | f1r, f2r = f1.rev(), f2.rev() |
|
222 | 221 | |
|
223 | 222 | if f1r is None: |
|
224 | 223 | f1 = g1.next() |
|
225 | 224 | if f2r is None: |
|
226 | 225 | f2 = g2.next() |
|
227 | 226 | |
|
228 | 227 | while True: |
|
229 | 228 | f1r, f2r = f1.rev(), f2.rev() |
|
230 | 229 | if f1r > f2r: |
|
231 | 230 | f1 = g1.next() |
|
232 | 231 | elif f2r > f1r: |
|
233 | 232 | f2 = g2.next() |
|
234 | 233 | elif f1 == f2: |
|
235 | 234 | return f1 # a match |
|
236 | 235 | elif f1r == f2r or f1r < limit or f2r < limit: |
|
237 | 236 | return False # copy no longer relevant |
|
238 | 237 | except StopIteration: |
|
239 | 238 | return False |
|
240 | 239 | |
|
241 | 240 | def checkcopies(f, m1, m2): |
|
242 | 241 | '''check possible copies of f from m1 to m2''' |
|
243 | 242 | of = None |
|
244 | 243 | seen = set([f]) |
|
245 | 244 | for oc in ctx(f, m1[f]).ancestors(): |
|
246 | 245 | ocr = oc.rev() |
|
247 | 246 | of = oc.path() |
|
248 | 247 | if of in seen: |
|
249 | 248 | # check limit late - grab last rename before |
|
250 | 249 | if ocr < limit: |
|
251 | 250 | break |
|
252 | 251 | continue |
|
253 | 252 | seen.add(of) |
|
254 | 253 | |
|
255 | 254 | fullcopy[f] = of # remember for dir rename detection |
|
256 | 255 | if of not in m2: |
|
257 | 256 | continue # no match, keep looking |
|
258 | 257 | if m2[of] == ma.get(of): |
|
259 | 258 | break # no merge needed, quit early |
|
260 | 259 | c2 = ctx(of, m2[of]) |
|
261 | 260 | cr = related(oc, c2, ca.rev()) |
|
262 | 261 | if cr and (of == f or of == c2.path()): # non-divergent |
|
263 | 262 | copy[f] = of |
|
264 | 263 | of = None |
|
265 | 264 | break |
|
266 | 265 | |
|
267 | 266 | if of in ma: |
|
268 | 267 | diverge.setdefault(of, []).append(f) |
|
269 | 268 | |
|
270 | 269 | repo.ui.debug(" searching for copies back to rev %d\n" % limit) |
|
271 | 270 | |
|
272 | 271 | u1 = _nonoverlap(m1, m2, ma) |
|
273 | 272 | u2 = _nonoverlap(m2, m1, ma) |
|
274 | 273 | |
|
275 | 274 | if u1: |
|
276 | 275 | repo.ui.debug(" unmatched files in local:\n %s\n" |
|
277 | 276 | % "\n ".join(u1)) |
|
278 | 277 | if u2: |
|
279 | 278 | repo.ui.debug(" unmatched files in other:\n %s\n" |
|
280 | 279 | % "\n ".join(u2)) |
|
281 | 280 | |
|
282 | 281 | for f in u1: |
|
283 | 282 | checkcopies(f, m1, m2) |
|
284 | 283 | for f in u2: |
|
285 | 284 | checkcopies(f, m2, m1) |
|
286 | 285 | |
|
287 | 286 | diverge2 = set() |
|
288 | 287 | for of, fl in diverge.items(): |
|
289 | 288 | if len(fl) == 1 or of in c2: |
|
290 | 289 | del diverge[of] # not actually divergent, or not a rename |
|
291 | 290 | else: |
|
292 | 291 | diverge2.update(fl) # reverse map for below |
|
293 | 292 | |
|
294 | 293 | if fullcopy: |
|
295 | 294 | repo.ui.debug(" all copies found (* = to merge, ! = divergent):\n") |
|
296 | 295 | for f in fullcopy: |
|
297 | 296 | note = "" |
|
298 | 297 | if f in copy: |
|
299 | 298 | note += "*" |
|
300 | 299 | if f in diverge2: |
|
301 | 300 | note += "!" |
|
302 | 301 | repo.ui.debug(" %s -> %s %s\n" % (f, fullcopy[f], note)) |
|
303 | 302 | del diverge2 |
|
304 | 303 | |
|
305 | 304 | if not fullcopy or not checkdirs: |
|
306 | 305 | return copy, diverge |
|
307 | 306 | |
|
308 | 307 | repo.ui.debug(" checking for directory renames\n") |
|
309 | 308 | |
|
310 | 309 | # generate a directory move map |
|
311 | 310 | d1, d2 = _dirs(m1), _dirs(m2) |
|
312 | 311 | invalid = set() |
|
313 | 312 | dirmove = {} |
|
314 | 313 | |
|
315 | 314 | # examine each file copy for a potential directory move, which is |
|
316 | 315 | # when all the files in a directory are moved to a new directory |
|
317 | 316 | for dst, src in fullcopy.iteritems(): |
|
318 | 317 | dsrc, ddst = _dirname(src), _dirname(dst) |
|
319 | 318 | if dsrc in invalid: |
|
320 | 319 | # already seen to be uninteresting |
|
321 | 320 | continue |
|
322 | 321 | elif dsrc in d1 and ddst in d1: |
|
323 | 322 | # directory wasn't entirely moved locally |
|
324 | 323 | invalid.add(dsrc) |
|
325 | 324 | elif dsrc in d2 and ddst in d2: |
|
326 | 325 | # directory wasn't entirely moved remotely |
|
327 | 326 | invalid.add(dsrc) |
|
328 | 327 | elif dsrc in dirmove and dirmove[dsrc] != ddst: |
|
329 | 328 | # files from the same directory moved to two different places |
|
330 | 329 | invalid.add(dsrc) |
|
331 | 330 | else: |
|
332 | 331 | # looks good so far |
|
333 | 332 | dirmove[dsrc + "/"] = ddst + "/" |
|
334 | 333 | |
|
335 | 334 | for i in invalid: |
|
336 | 335 | if i in dirmove: |
|
337 | 336 | del dirmove[i] |
|
338 | 337 | del d1, d2, invalid |
|
339 | 338 | |
|
340 | 339 | if not dirmove: |
|
341 | 340 | return copy, diverge |
|
342 | 341 | |
|
343 | 342 | for d in dirmove: |
|
344 | 343 | repo.ui.debug(" dir %s -> %s\n" % (d, dirmove[d])) |
|
345 | 344 | |
|
346 | 345 | # check unaccounted nonoverlapping files against directory moves |
|
347 | 346 | for f in u1 + u2: |
|
348 | 347 | if f not in fullcopy: |
|
349 | 348 | for d in dirmove: |
|
350 | 349 | if f.startswith(d): |
|
351 | 350 | # new file added in a directory that was moved, move it |
|
352 | 351 | df = dirmove[d] + f[len(d):] |
|
353 | 352 | if df not in copy: |
|
354 | 353 | copy[f] = df |
|
355 | 354 | repo.ui.debug(" file %s -> %s\n" % (f, copy[f])) |
|
356 | 355 | break |
|
357 | 356 | |
|
358 | 357 | return copy, diverge |
General Comments 0
You need to be logged in to leave comments.
Login now