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