##// END OF EJS Templates
copies: check if revisions are related (bug found with pylint)
Benoit Boissinot -
r10313:31141fd7 default
parent child Browse files
Show More
@@ -1,253 +1,253
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 from i18n import _
9 9 import util
10 10 import heapq
11 11
12 12 def _nonoverlap(d1, d2, d3):
13 13 "Return list of elements in d1 not in d2 or d3"
14 14 return sorted([d for d in d1 if d not in d3 and d not in d2])
15 15
16 16 def _dirname(f):
17 17 s = f.rfind("/")
18 18 if s == -1:
19 19 return ""
20 20 return f[:s]
21 21
22 22 def _dirs(files):
23 23 d = set()
24 24 for f in files:
25 25 f = _dirname(f)
26 26 while f not in d:
27 27 d.add(f)
28 28 f = _dirname(f)
29 29 return d
30 30
31 31 def _findlimit(repo, a, b):
32 32 """Find the earliest revision that's an ancestor of a or b but not both,
33 33 None if no such revision exists.
34 34 """
35 35 # basic idea:
36 36 # - mark a and b with different sides
37 37 # - if a parent's children are all on the same side, the parent is
38 38 # on that side, otherwise it is on no side
39 39 # - walk the graph in topological order with the help of a heap;
40 40 # - add unseen parents to side map
41 41 # - clear side of any parent that has children on different sides
42 42 # - track number of interesting revs that might still be on a side
43 43 # - track the lowest interesting rev seen
44 44 # - quit when interesting revs is zero
45 45
46 46 cl = repo.changelog
47 47 working = len(cl) # pseudo rev for the working directory
48 48 if a is None:
49 49 a = working
50 50 if b is None:
51 51 b = working
52 52
53 53 side = {a: -1, b: 1}
54 54 visit = [-a, -b]
55 55 heapq.heapify(visit)
56 56 interesting = len(visit)
57 57 hascommonancestor = False
58 58 limit = working
59 59
60 60 while interesting:
61 61 r = -heapq.heappop(visit)
62 62 if r == working:
63 63 parents = [cl.rev(p) for p in repo.dirstate.parents()]
64 64 else:
65 65 parents = cl.parentrevs(r)
66 66 for p in parents:
67 67 if p < 0:
68 68 continue
69 69 if p not in side:
70 70 # first time we see p; add it to visit
71 71 side[p] = side[r]
72 72 if side[p]:
73 73 interesting += 1
74 74 heapq.heappush(visit, -p)
75 75 elif side[p] and side[p] != side[r]:
76 76 # p was interesting but now we know better
77 77 side[p] = 0
78 78 interesting -= 1
79 79 hascommonancestor = True
80 80 if side[r]:
81 81 limit = r # lowest rev visited
82 82 interesting -= 1
83 83
84 84 if not hascommonancestor:
85 85 return None
86 86 return limit
87 87
88 88 def copies(repo, c1, c2, ca, checkdirs=False):
89 89 """
90 90 Find moves and copies between context c1 and c2
91 91 """
92 92 # avoid silly behavior for update from empty dir
93 93 if not c1 or not c2 or c1 == c2:
94 94 return {}, {}
95 95
96 96 # avoid silly behavior for parent -> working dir
97 97 if c2.node() is None and c1.node() == repo.dirstate.parents()[0]:
98 98 return repo.dirstate.copies(), {}
99 99
100 100 limit = _findlimit(repo, c1.rev(), c2.rev())
101 101 if limit is None:
102 102 # no common ancestor, no copies
103 103 return {}, {}
104 104 m1 = c1.manifest()
105 105 m2 = c2.manifest()
106 106 ma = ca.manifest()
107 107
108 108 def makectx(f, n):
109 109 if len(n) != 20: # in a working context?
110 110 if c1.rev() is None:
111 111 return c1.filectx(f)
112 112 return c2.filectx(f)
113 113 return repo.filectx(f, fileid=n)
114 114
115 115 ctx = util.lrucachefunc(makectx)
116 116 copy = {}
117 117 fullcopy = {}
118 118 diverge = {}
119 119
120 120 def related(f1, f2, limit):
121 121 g1, g2 = f1.ancestors(), f2.ancestors()
122 122 try:
123 123 while 1:
124 124 f1r, f2r = f1.rev(), f2.rev()
125 125 if f1r > f2r:
126 126 f1 = g1.next()
127 127 elif f2r > f1r:
128 128 f2 = g2.next()
129 129 elif f1 == f2:
130 130 return f1 # a match
131 131 elif f1r == f2r or f1r < limit or f2r < limit:
132 132 return False # copy no longer relevant
133 133 except StopIteration:
134 134 return False
135 135
136 136 def checkcopies(f, m1, m2):
137 137 '''check possible copies of f from m1 to m2'''
138 138 of = None
139 139 seen = set([f])
140 140 for oc in ctx(f, m1[f]).ancestors():
141 141 ocr = oc.rev()
142 142 of = oc.path()
143 143 if of in seen:
144 144 # check limit late - grab last rename before
145 145 if ocr < limit:
146 146 break
147 147 continue
148 148 seen.add(of)
149 149
150 150 fullcopy[f] = of # remember for dir rename detection
151 151 if of not in m2:
152 152 continue # no match, keep looking
153 153 if m2[of] == ma.get(of):
154 154 break # no merge needed, quit early
155 155 c2 = ctx(of, m2[of])
156 156 cr = related(oc, c2, ca.rev())
157 if of == f or of == c2.path(): # non-divergent
157 if cr and (of == f or of == c2.path()): # non-divergent
158 158 copy[f] = of
159 159 of = None
160 160 break
161 161
162 162 if of in ma:
163 163 diverge.setdefault(of, []).append(f)
164 164
165 165 repo.ui.debug(" searching for copies back to rev %d\n" % limit)
166 166
167 167 u1 = _nonoverlap(m1, m2, ma)
168 168 u2 = _nonoverlap(m2, m1, ma)
169 169
170 170 if u1:
171 171 repo.ui.debug(" unmatched files in local:\n %s\n"
172 172 % "\n ".join(u1))
173 173 if u2:
174 174 repo.ui.debug(" unmatched files in other:\n %s\n"
175 175 % "\n ".join(u2))
176 176
177 177 for f in u1:
178 178 checkcopies(f, m1, m2)
179 179 for f in u2:
180 180 checkcopies(f, m2, m1)
181 181
182 182 diverge2 = set()
183 183 for of, fl in diverge.items():
184 184 if len(fl) == 1:
185 185 del diverge[of] # not actually divergent
186 186 else:
187 187 diverge2.update(fl) # reverse map for below
188 188
189 189 if fullcopy:
190 190 repo.ui.debug(" all copies found (* = to merge, ! = divergent):\n")
191 191 for f in fullcopy:
192 192 note = ""
193 193 if f in copy:
194 194 note += "*"
195 195 if f in diverge2:
196 196 note += "!"
197 197 repo.ui.debug(" %s -> %s %s\n" % (f, fullcopy[f], note))
198 198 del diverge2
199 199
200 200 if not fullcopy or not checkdirs:
201 201 return copy, diverge
202 202
203 203 repo.ui.debug(" checking for directory renames\n")
204 204
205 205 # generate a directory move map
206 206 d1, d2 = _dirs(m1), _dirs(m2)
207 207 invalid = set()
208 208 dirmove = {}
209 209
210 210 # examine each file copy for a potential directory move, which is
211 211 # when all the files in a directory are moved to a new directory
212 212 for dst, src in fullcopy.iteritems():
213 213 dsrc, ddst = _dirname(src), _dirname(dst)
214 214 if dsrc in invalid:
215 215 # already seen to be uninteresting
216 216 continue
217 217 elif dsrc in d1 and ddst in d1:
218 218 # directory wasn't entirely moved locally
219 219 invalid.add(dsrc)
220 220 elif dsrc in d2 and ddst in d2:
221 221 # directory wasn't entirely moved remotely
222 222 invalid.add(dsrc)
223 223 elif dsrc in dirmove and dirmove[dsrc] != ddst:
224 224 # files from the same directory moved to two different places
225 225 invalid.add(dsrc)
226 226 else:
227 227 # looks good so far
228 228 dirmove[dsrc + "/"] = ddst + "/"
229 229
230 230 for i in invalid:
231 231 if i in dirmove:
232 232 del dirmove[i]
233 233 del d1, d2, invalid
234 234
235 235 if not dirmove:
236 236 return copy, diverge
237 237
238 238 for d in dirmove:
239 239 repo.ui.debug(" dir %s -> %s\n" % (d, dirmove[d]))
240 240
241 241 # check unaccounted nonoverlapping files against directory moves
242 242 for f in u1 + u2:
243 243 if f not in fullcopy:
244 244 for d in dirmove:
245 245 if f.startswith(d):
246 246 # new file added in a directory that was moved, move it
247 247 df = dirmove[d] + f[len(d):]
248 248 if df not in copy:
249 249 copy[f] = df
250 250 repo.ui.debug(" file %s -> %s\n" % (f, copy[f]))
251 251 break
252 252
253 253 return copy, diverge
General Comments 0
You need to be logged in to leave comments. Login now