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