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