comparison mercurial/copies.py @ 6274:f3f383efbeae

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