##// END OF EJS Templates
copies: don't double-detect items in the directory copy check
Matt Mackall -
r6426:e2c49ef2 default
parent child Browse files
Show More
@@ -1,192 +1,193
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
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
7
7
8 from node import nullid, nullrev
8 from node import nullid, nullrev
9 from i18n import _
9 from i18n import _
10 import util, ancestor
10 import util, ancestor
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 l = [d for d in d1 if d not in d3 and d not in d2]
14 l = [d for d in d1 if d not in d3 and d not in d2]
15 l.sort()
15 l.sort()
16 return l
16 return l
17
17
18 def _dirname(f):
18 def _dirname(f):
19 s = f.rfind("/")
19 s = f.rfind("/")
20 if s == -1:
20 if s == -1:
21 return ""
21 return ""
22 return f[:s]
22 return f[:s]
23
23
24 def _dirs(files):
24 def _dirs(files):
25 d = {}
25 d = {}
26 for f in files:
26 for f in files:
27 f = _dirname(f)
27 f = _dirname(f)
28 while f not in d:
28 while f not in d:
29 d[f] = True
29 d[f] = True
30 f = _dirname(f)
30 f = _dirname(f)
31 return d
31 return d
32
32
33 def _findoldnames(fctx, limit):
33 def _findoldnames(fctx, limit):
34 "find files that path was copied from, back to linkrev limit"
34 "find files that path was copied from, back to linkrev limit"
35 old = {}
35 old = {}
36 seen = {}
36 seen = {}
37 orig = fctx.path()
37 orig = fctx.path()
38 visit = [(fctx, 0)]
38 visit = [(fctx, 0)]
39 while visit:
39 while visit:
40 fc, depth = visit.pop()
40 fc, depth = visit.pop()
41 s = str(fc)
41 s = str(fc)
42 if s in seen:
42 if s in seen:
43 continue
43 continue
44 seen[s] = 1
44 seen[s] = 1
45 if fc.path() != orig and fc.path() not in old:
45 if fc.path() != orig and fc.path() not in old:
46 old[fc.path()] = (depth, fc.path()) # remember depth
46 old[fc.path()] = (depth, fc.path()) # remember depth
47 if fc.rev() < limit and fc.rev() is not None:
47 if fc.rev() < limit and fc.rev() is not None:
48 continue
48 continue
49 visit += [(p, depth - 1) for p in fc.parents()]
49 visit += [(p, depth - 1) for p in fc.parents()]
50
50
51 # return old names sorted by depth
51 # return old names sorted by depth
52 old = old.values()
52 old = old.values()
53 old.sort()
53 old.sort()
54 return [o[1] for o in old]
54 return [o[1] for o in old]
55
55
56 def copies(repo, c1, c2, ca, checkdirs=False):
56 def copies(repo, c1, c2, ca, checkdirs=False):
57 """
57 """
58 Find moves and copies between context c1 and c2
58 Find moves and copies between context c1 and c2
59 """
59 """
60 # avoid silly behavior for update from empty dir
60 # avoid silly behavior for update from empty dir
61 if not c1 or not c2:
61 if not c1 or not c2:
62 return {}, {}
62 return {}, {}
63
63
64 rev1, rev2 = c1.rev(), c2.rev()
64 rev1, rev2 = c1.rev(), c2.rev()
65 if rev1 is None: # c1 is a workingctx
65 if rev1 is None: # c1 is a workingctx
66 rev1 = c1.parents()[0].rev()
66 rev1 = c1.parents()[0].rev()
67 if rev2 is None: # c2 is a workingctx
67 if rev2 is None: # c2 is a workingctx
68 rev2 = c2.parents()[0].rev()
68 rev2 = c2.parents()[0].rev()
69 pr = repo.changelog.parentrevs
69 pr = repo.changelog.parentrevs
70 def parents(rev):
70 def parents(rev):
71 return [p for p in pr(rev) if p != nullrev]
71 return [p for p in pr(rev) if p != nullrev]
72 limit = min(ancestor.symmetricdifference(rev1, rev2, parents))
72 limit = min(ancestor.symmetricdifference(rev1, rev2, parents))
73 m1 = c1.manifest()
73 m1 = c1.manifest()
74 m2 = c2.manifest()
74 m2 = c2.manifest()
75 ma = ca.manifest()
75 ma = ca.manifest()
76
76
77 def makectx(f, n):
77 def makectx(f, n):
78 if len(n) != 20: # in a working context?
78 if len(n) != 20: # in a working context?
79 if c1.rev() is None:
79 if c1.rev() is None:
80 return c1.filectx(f)
80 return c1.filectx(f)
81 return c2.filectx(f)
81 return c2.filectx(f)
82 return repo.filectx(f, fileid=n)
82 return repo.filectx(f, fileid=n)
83 ctx = util.cachefunc(makectx)
83 ctx = util.cachefunc(makectx)
84
84
85 copy = {}
85 copy = {}
86 fullcopy = {}
86 fullcopy = {}
87 diverge = {}
87 diverge = {}
88
88
89 def checkcopies(f, m1, m2):
89 def checkcopies(f, m1, m2):
90 '''check possible copies of f from m1 to m2'''
90 '''check possible copies of f from m1 to m2'''
91 c1 = ctx(f, m1[f])
91 c1 = ctx(f, m1[f])
92 for of in _findoldnames(c1, limit):
92 for of in _findoldnames(c1, limit):
93 fullcopy[f] = of # remember for dir rename detection
93 fullcopy[f] = of # remember for dir rename detection
94 if of in m2: # original file not in other manifest?
94 if of in m2: # original file not in other manifest?
95 # if the original file is unchanged on the other branch,
95 # if the original file is unchanged on the other branch,
96 # no merge needed
96 # no merge needed
97 if m2[of] != ma.get(of):
97 if m2[of] != ma.get(of):
98 c2 = ctx(of, m2[of])
98 c2 = ctx(of, m2[of])
99 ca = c1.ancestor(c2)
99 ca = c1.ancestor(c2)
100 # related and named changed on only one side?
100 # related and named changed on only one side?
101 if ca and (ca.path() == f or ca.path() == c2.path()):
101 if ca and (ca.path() == f or ca.path() == c2.path()):
102 if c1 != ca or c2 != ca: # merge needed?
102 if c1 != ca or c2 != ca: # merge needed?
103 copy[f] = of
103 copy[f] = of
104 elif of in ma:
104 elif of in ma:
105 diverge.setdefault(of, []).append(f)
105 diverge.setdefault(of, []).append(f)
106
106
107 repo.ui.debug(_(" searching for copies back to rev %d\n") % limit)
107 repo.ui.debug(_(" searching for copies back to rev %d\n") % limit)
108
108
109 u1 = _nonoverlap(m1, m2, ma)
109 u1 = _nonoverlap(m1, m2, ma)
110 u2 = _nonoverlap(m2, m1, ma)
110 u2 = _nonoverlap(m2, m1, ma)
111
111
112 if u1:
112 if u1:
113 repo.ui.debug(_(" unmatched files in local:\n %s\n")
113 repo.ui.debug(_(" unmatched files in local:\n %s\n")
114 % "\n ".join(u1))
114 % "\n ".join(u1))
115 if u2:
115 if u2:
116 repo.ui.debug(_(" unmatched files in other:\n %s\n")
116 repo.ui.debug(_(" unmatched files in other:\n %s\n")
117 % "\n ".join(u2))
117 % "\n ".join(u2))
118
118
119 for f in u1:
119 for f in u1:
120 checkcopies(f, m1, m2)
120 checkcopies(f, m1, m2)
121 for f in u2:
121 for f in u2:
122 checkcopies(f, m2, m1)
122 checkcopies(f, m2, m1)
123
123
124 diverge2 = {}
124 diverge2 = {}
125 for of, fl in diverge.items():
125 for of, fl in diverge.items():
126 if len(fl) == 1:
126 if len(fl) == 1:
127 del diverge[of] # not actually divergent
127 del diverge[of] # not actually divergent
128 else:
128 else:
129 diverge2.update(dict.fromkeys(fl)) # reverse map for below
129 diverge2.update(dict.fromkeys(fl)) # reverse map for below
130
130
131 if fullcopy:
131 if fullcopy:
132 repo.ui.debug(_(" all copies found (* = to merge, ! = divergent):\n"))
132 repo.ui.debug(_(" all copies found (* = to merge, ! = divergent):\n"))
133 for f in fullcopy:
133 for f in fullcopy:
134 note = ""
134 note = ""
135 if f in copy: note += "*"
135 if f in copy: note += "*"
136 if f in diverge2: note += "!"
136 if f in diverge2: note += "!"
137 repo.ui.debug(_(" %s -> %s %s\n") % (f, fullcopy[f], note))
137 repo.ui.debug(_(" %s -> %s %s\n") % (f, fullcopy[f], note))
138 del diverge2
138 del diverge2
139
139
140 if not fullcopy or not checkdirs:
140 if not fullcopy or not checkdirs:
141 return copy, diverge
141 return copy, diverge
142
142
143 repo.ui.debug(_(" checking for directory renames\n"))
143 repo.ui.debug(_(" checking for directory renames\n"))
144
144
145 # generate a directory move map
145 # generate a directory move map
146 d1, d2 = _dirs(m1), _dirs(m2)
146 d1, d2 = _dirs(m1), _dirs(m2)
147 invalid = {}
147 invalid = {}
148 dirmove = {}
148 dirmove = {}
149
149
150 # examine each file copy for a potential directory move, which is
150 # examine each file copy for a potential directory move, which is
151 # when all the files in a directory are moved to a new directory
151 # when all the files in a directory are moved to a new directory
152 for dst, src in fullcopy.items():
152 for dst, src in fullcopy.items():
153 dsrc, ddst = _dirname(src), _dirname(dst)
153 dsrc, ddst = _dirname(src), _dirname(dst)
154 if dsrc in invalid:
154 if dsrc in invalid:
155 # already seen to be uninteresting
155 # already seen to be uninteresting
156 continue
156 continue
157 elif dsrc in d1 and ddst in d1:
157 elif dsrc in d1 and ddst in d1:
158 # directory wasn't entirely moved locally
158 # directory wasn't entirely moved locally
159 invalid[dsrc] = True
159 invalid[dsrc] = True
160 elif dsrc in d2 and ddst in d2:
160 elif dsrc in d2 and ddst in d2:
161 # directory wasn't entirely moved remotely
161 # directory wasn't entirely moved remotely
162 invalid[dsrc] = True
162 invalid[dsrc] = True
163 elif dsrc in dirmove and dirmove[dsrc] != ddst:
163 elif dsrc in dirmove and dirmove[dsrc] != ddst:
164 # files from the same directory moved to two different places
164 # files from the same directory moved to two different places
165 invalid[dsrc] = True
165 invalid[dsrc] = True
166 else:
166 else:
167 # looks good so far
167 # looks good so far
168 dirmove[dsrc + "/"] = ddst + "/"
168 dirmove[dsrc + "/"] = ddst + "/"
169
169
170 for i in invalid:
170 for i in invalid:
171 if i in dirmove:
171 if i in dirmove:
172 del dirmove[i]
172 del dirmove[i]
173 del d1, d2, invalid
173 del d1, d2, invalid
174
174
175 if not dirmove:
175 if not dirmove:
176 return copy, diverge
176 return copy, diverge
177
177
178 for d in dirmove:
178 for d in dirmove:
179 repo.ui.debug(_(" dir %s -> %s\n") % (d, dirmove[d]))
179 repo.ui.debug(_(" dir %s -> %s\n") % (d, dirmove[d]))
180
180
181 # check unaccounted nonoverlapping files against directory moves
181 # check unaccounted nonoverlapping files against directory moves
182 for f in u1 + u2:
182 for f in u1 + u2:
183 if f not in fullcopy:
183 if f not in fullcopy:
184 for d in dirmove:
184 for d in dirmove:
185 if f.startswith(d):
185 if f.startswith(d):
186 # new file added in a directory that was moved, move it
186 # new file added in a directory that was moved, move it
187 df = dirmove[d] + f[len(d):]
187 df = dirmove[d] + f[len(d):]
188 if df not in copy:
188 copy[f] = df
189 copy[f] = df
189 repo.ui.debug(_(" file %s -> %s\n") % (f, copy[f]))
190 repo.ui.debug(_(" file %s -> %s\n") % (f, copy[f]))
190 break
191 break
191
192
192 return copy, diverge
193 return copy, diverge
General Comments 0
You need to be logged in to leave comments. Login now