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