Show More
@@ -1,574 +1,575 b'' | |||||
1 | # hg.py - repository classes for mercurial |
|
1 | # hg.py - repository classes for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005 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 | import sys, struct, sha, socket, os, time, base64, re, urllib2, binascii |
|
8 | import sys, struct, sha, socket, os, time, base64, re, urllib2, binascii | |
9 | from mercurial import byterange |
|
9 | from mercurial import byterange | |
10 | from mercurial.transaction import * |
|
10 | from mercurial.transaction import * | |
11 | from mercurial.revlog import * |
|
11 | from mercurial.revlog import * | |
12 |
|
12 | |||
13 | def hex(node): return binascii.hexlify(node) |
|
13 | def hex(node): return binascii.hexlify(node) | |
14 | def bin(node): return binascii.unhexlify(node) |
|
14 | def bin(node): return binascii.unhexlify(node) | |
15 |
|
15 | |||
16 | class filelog(revlog): |
|
16 | class filelog(revlog): | |
17 | def __init__(self, opener, path): |
|
17 | def __init__(self, opener, path): | |
18 | s = self.encodepath(path) |
|
18 | s = self.encodepath(path) | |
19 | revlog.__init__(self, opener, os.path.join("data", s + "i"), |
|
19 | revlog.__init__(self, opener, os.path.join("data", s + "i"), | |
20 | os.path.join("data", s)) |
|
20 | os.path.join("data", s)) | |
21 |
|
21 | |||
22 | def encodepath(self, path): |
|
22 | def encodepath(self, path): | |
23 | s = sha.sha(path).digest() |
|
23 | s = sha.sha(path).digest() | |
24 | s = base64.encodestring(s)[:-3] |
|
24 | s = base64.encodestring(s)[:-3] | |
25 | s = re.sub("\+", "%", s) |
|
25 | s = re.sub("\+", "%", s) | |
26 | s = re.sub("/", "_", s) |
|
26 | s = re.sub("/", "_", s) | |
27 | return s |
|
27 | return s | |
28 |
|
28 | |||
29 | def read(self, node): |
|
29 | def read(self, node): | |
30 | return self.revision(node) |
|
30 | return self.revision(node) | |
31 | def add(self, text, transaction, link, p1=None, p2=None): |
|
31 | def add(self, text, transaction, link, p1=None, p2=None): | |
32 | return self.addrevision(text, transaction, link, p1, p2) |
|
32 | return self.addrevision(text, transaction, link, p1, p2) | |
33 |
|
33 | |||
34 | def resolvedag(self, old, new, transaction, link): |
|
34 | def resolvedag(self, old, new, transaction, link): | |
35 | """resolve unmerged heads in our DAG""" |
|
35 | """resolve unmerged heads in our DAG""" | |
36 | if old == new: return None |
|
36 | if old == new: return None | |
37 | a = self.ancestor(old, new) |
|
37 | a = self.ancestor(old, new) | |
38 | if old == a: return new |
|
38 | if old == a: return new | |
39 | return self.merge3(old, new, a, transaction, link) |
|
39 | return self.merge3(old, new, a, transaction, link) | |
40 |
|
40 | |||
41 | def merge3(self, my, other, base, transaction, link): |
|
41 | def merge3(self, my, other, base, transaction, link): | |
42 | """perform a 3-way merge and append the result""" |
|
42 | """perform a 3-way merge and append the result""" | |
43 | def temp(prefix, node): |
|
43 | def temp(prefix, node): | |
44 | (fd, name) = tempfile.mkstemp(prefix) |
|
44 | (fd, name) = tempfile.mkstemp(prefix) | |
45 | f = os.fdopen(fd, "w") |
|
45 | f = os.fdopen(fd, "w") | |
46 | f.write(self.revision(node)) |
|
46 | f.write(self.revision(node)) | |
47 | f.close() |
|
47 | f.close() | |
48 | return name |
|
48 | return name | |
49 |
|
49 | |||
50 | a = temp("local", my) |
|
50 | a = temp("local", my) | |
51 | b = temp("remote", other) |
|
51 | b = temp("remote", other) | |
52 | c = temp("parent", base) |
|
52 | c = temp("parent", base) | |
53 |
|
53 | |||
54 | cmd = os.environ["HGMERGE"] |
|
54 | cmd = os.environ["HGMERGE"] | |
55 | r = os.system("%s %s %s %s" % (cmd, a, b, c)) |
|
55 | r = os.system("%s %s %s %s" % (cmd, a, b, c)) | |
56 | if r: |
|
56 | if r: | |
57 | raise "Merge failed, implement rollback!" |
|
57 | raise "Merge failed, implement rollback!" | |
58 |
|
58 | |||
59 | t = open(a).read() |
|
59 | t = open(a).read() | |
60 | os.unlink(a) |
|
60 | os.unlink(a) | |
61 | os.unlink(b) |
|
61 | os.unlink(b) | |
62 | os.unlink(c) |
|
62 | os.unlink(c) | |
63 | return self.addrevision(t, transaction, link, my, other) |
|
63 | return self.addrevision(t, transaction, link, my, other) | |
64 |
|
64 | |||
65 | def merge(self, other, transaction, linkseq, link): |
|
65 | def merge(self, other, transaction, linkseq, link): | |
66 | """perform a merge and resolve resulting heads""" |
|
66 | """perform a merge and resolve resulting heads""" | |
67 | (o, n) = self.mergedag(other, transaction, linkseq) |
|
67 | (o, n) = self.mergedag(other, transaction, linkseq) | |
68 | return self.resolvedag(o, n, transaction, link) |
|
68 | return self.resolvedag(o, n, transaction, link) | |
69 |
|
69 | |||
70 | class manifest(revlog): |
|
70 | class manifest(revlog): | |
71 | def __init__(self, opener): |
|
71 | def __init__(self, opener): | |
72 | self.mapcache = None |
|
72 | self.mapcache = None | |
73 | self.listcache = None |
|
73 | self.listcache = None | |
74 | self.addlist = None |
|
74 | self.addlist = None | |
75 | revlog.__init__(self, opener, "00manifest.i", "00manifest.d") |
|
75 | revlog.__init__(self, opener, "00manifest.i", "00manifest.d") | |
76 |
|
76 | |||
77 | def read(self, node): |
|
77 | def read(self, node): | |
78 | if self.mapcache and self.mapcache[0] == node: |
|
78 | if self.mapcache and self.mapcache[0] == node: | |
79 | return self.mapcache[1] |
|
79 | return self.mapcache[1] | |
80 | text = self.revision(node) |
|
80 | text = self.revision(node) | |
81 | map = {} |
|
81 | map = {} | |
82 | self.listcache = text.splitlines(1) |
|
82 | self.listcache = text.splitlines(1) | |
83 | for l in self.listcache: |
|
83 | for l in self.listcache: | |
84 | (f, n) = l.split('\0') |
|
84 | (f, n) = l.split('\0') | |
85 | map[f] = bin(n[:40]) |
|
85 | map[f] = bin(n[:40]) | |
86 | self.mapcache = (node, map) |
|
86 | self.mapcache = (node, map) | |
87 | return map |
|
87 | return map | |
88 |
|
88 | |||
89 | def diff(self, a, b): |
|
89 | def diff(self, a, b): | |
90 | # this is sneaky, as we're not actually using a and b |
|
90 | # this is sneaky, as we're not actually using a and b | |
91 | if self.listcache: |
|
91 | if self.listcache: | |
92 | return mdiff.diff(self.listcache, self.addlist, 1) |
|
92 | return mdiff.diff(self.listcache, self.addlist, 1) | |
93 | else: |
|
93 | else: | |
94 | return mdiff.diff(a, b) |
|
94 | return mdiff.diff(a, b) | |
95 |
|
95 | |||
96 | def add(self, map, transaction, link, p1=None, p2=None): |
|
96 | def add(self, map, transaction, link, p1=None, p2=None): | |
97 | files = map.keys() |
|
97 | files = map.keys() | |
98 | files.sort() |
|
98 | files.sort() | |
99 |
|
99 | |||
100 | self.addlist = ["%s\000%s\n" % (f, hex(map[f])) for f in files] |
|
100 | self.addlist = ["%s\000%s\n" % (f, hex(map[f])) for f in files] | |
101 | text = "".join(self.addlist) |
|
101 | text = "".join(self.addlist) | |
102 |
|
102 | |||
103 | n = self.addrevision(text, transaction, link, p1, p2) |
|
103 | n = self.addrevision(text, transaction, link, p1, p2) | |
104 | self.mapcache = (n, map) |
|
104 | self.mapcache = (n, map) | |
105 | self.listcache = self.addlist |
|
105 | self.listcache = self.addlist | |
106 |
|
106 | |||
107 | return n |
|
107 | return n | |
108 |
|
108 | |||
109 | class changelog(revlog): |
|
109 | class changelog(revlog): | |
110 | def __init__(self, opener): |
|
110 | def __init__(self, opener): | |
111 | revlog.__init__(self, opener, "00changelog.i", "00changelog.d") |
|
111 | revlog.__init__(self, opener, "00changelog.i", "00changelog.d") | |
112 |
|
112 | |||
113 | def extract(self, text): |
|
113 | def extract(self, text): | |
114 | last = text.index("\n\n") |
|
114 | last = text.index("\n\n") | |
115 | desc = text[last + 2:] |
|
115 | desc = text[last + 2:] | |
116 | l = text[:last].splitlines() |
|
116 | l = text[:last].splitlines() | |
117 | manifest = bin(l[0]) |
|
117 | manifest = bin(l[0]) | |
118 | user = l[1] |
|
118 | user = l[1] | |
119 | date = l[2] |
|
119 | date = l[2] | |
120 | files = l[3:] |
|
120 | files = l[3:] | |
121 | return (manifest, user, date, files, desc) |
|
121 | return (manifest, user, date, files, desc) | |
122 |
|
122 | |||
123 | def read(self, node): |
|
123 | def read(self, node): | |
124 | return self.extract(self.revision(node)) |
|
124 | return self.extract(self.revision(node)) | |
125 |
|
125 | |||
126 | def add(self, manifest, list, desc, transaction, p1=None, p2=None): |
|
126 | def add(self, manifest, list, desc, transaction, p1=None, p2=None): | |
127 | try: user = os.environ["HGUSER"] |
|
127 | try: user = os.environ["HGUSER"] | |
128 | except: user = os.environ["LOGNAME"] + '@' + socket.getfqdn() |
|
128 | except: user = os.environ["LOGNAME"] + '@' + socket.getfqdn() | |
129 | date = "%d %d" % (time.time(), time.timezone) |
|
129 | date = "%d %d" % (time.time(), time.timezone) | |
130 | list.sort() |
|
130 | list.sort() | |
131 | l = [hex(manifest), user, date] + list + ["", desc] |
|
131 | l = [hex(manifest), user, date] + list + ["", desc] | |
132 | text = "\n".join(l) |
|
132 | text = "\n".join(l) | |
133 | return self.addrevision(text, transaction, self.count(), p1, p2) |
|
133 | return self.addrevision(text, transaction, self.count(), p1, p2) | |
134 |
|
134 | |||
135 | def merge3(self, my, other, base): |
|
135 | def merge3(self, my, other, base): | |
136 | pass |
|
136 | pass | |
137 |
|
137 | |||
138 | class dircache: |
|
138 | class dircache: | |
139 | def __init__(self, opener): |
|
139 | def __init__(self, opener): | |
140 | self.opener = opener |
|
140 | self.opener = opener | |
141 | self.dirty = 0 |
|
141 | self.dirty = 0 | |
142 | self.map = None |
|
142 | self.map = None | |
143 | def __del__(self): |
|
143 | def __del__(self): | |
144 | if self.dirty: self.write() |
|
144 | if self.dirty: self.write() | |
145 | def __getitem__(self, key): |
|
145 | def __getitem__(self, key): | |
146 | try: |
|
146 | try: | |
147 | return self.map[key] |
|
147 | return self.map[key] | |
148 | except TypeError: |
|
148 | except TypeError: | |
149 | self.read() |
|
149 | self.read() | |
150 | return self[key] |
|
150 | return self[key] | |
151 |
|
151 | |||
152 | def read(self): |
|
152 | def read(self): | |
153 | if self.map is not None: return self.map |
|
153 | if self.map is not None: return self.map | |
154 |
|
154 | |||
155 | self.map = {} |
|
155 | self.map = {} | |
156 | try: |
|
156 | try: | |
157 | st = self.opener("dircache").read() |
|
157 | st = self.opener("dircache").read() | |
158 | except: return |
|
158 | except: return | |
159 |
|
159 | |||
160 | pos = 0 |
|
160 | pos = 0 | |
161 | while pos < len(st): |
|
161 | while pos < len(st): | |
162 | e = struct.unpack(">llll", st[pos:pos+16]) |
|
162 | e = struct.unpack(">llll", st[pos:pos+16]) | |
163 | l = e[3] |
|
163 | l = e[3] | |
164 | pos += 16 |
|
164 | pos += 16 | |
165 | f = st[pos:pos + l] |
|
165 | f = st[pos:pos + l] | |
166 | self.map[f] = e[:3] |
|
166 | self.map[f] = e[:3] | |
167 | pos += l |
|
167 | pos += l | |
168 |
|
168 | |||
169 | def update(self, files): |
|
169 | def update(self, files): | |
170 | if not files: return |
|
170 | if not files: return | |
171 | self.read() |
|
171 | self.read() | |
172 | self.dirty = 1 |
|
172 | self.dirty = 1 | |
173 | for f in files: |
|
173 | for f in files: | |
174 | try: |
|
174 | try: | |
175 | s = os.stat(f) |
|
175 | s = os.stat(f) | |
176 | self.map[f] = (s.st_mode, s.st_size, s.st_mtime) |
|
176 | self.map[f] = (s.st_mode, s.st_size, s.st_mtime) | |
177 | except IOError: |
|
177 | except IOError: | |
178 | self.remove(f) |
|
178 | self.remove(f) | |
179 |
|
179 | |||
180 | def taint(self, files): |
|
180 | def taint(self, files): | |
181 | if not files: return |
|
181 | if not files: return | |
182 | self.read() |
|
182 | self.read() | |
183 | self.dirty = 1 |
|
183 | self.dirty = 1 | |
184 | for f in files: |
|
184 | for f in files: | |
185 | self.map[f] = (0, -1, 0) |
|
185 | self.map[f] = (0, -1, 0) | |
186 |
|
186 | |||
187 | def remove(self, files): |
|
187 | def remove(self, files): | |
188 | if not files: return |
|
188 | if not files: return | |
189 | self.read() |
|
189 | self.read() | |
190 | self.dirty = 1 |
|
190 | self.dirty = 1 | |
191 | for f in files: |
|
191 | for f in files: | |
192 | try: del self[f] |
|
192 | try: del self[f] | |
193 | except: pass |
|
193 | except: pass | |
194 |
|
194 | |||
195 | def clear(self): |
|
195 | def clear(self): | |
196 | self.map = {} |
|
196 | self.map = {} | |
197 | self.dirty = 1 |
|
197 | self.dirty = 1 | |
198 |
|
198 | |||
199 | def write(self): |
|
199 | def write(self): | |
200 | st = self.opener("dircache", "w") |
|
200 | st = self.opener("dircache", "w") | |
201 | for f, e in self.map.items(): |
|
201 | for f, e in self.map.items(): | |
202 | e = struct.pack(">llll", e[0], e[1], e[2], len(f)) |
|
202 | e = struct.pack(">llll", e[0], e[1], e[2], len(f)) | |
203 | st.write(e + f) |
|
203 | st.write(e + f) | |
204 | self.dirty = 0 |
|
204 | self.dirty = 0 | |
205 |
|
205 | |||
206 | def copy(self): |
|
206 | def copy(self): | |
207 | self.read() |
|
207 | self.read() | |
208 | return self.map.copy() |
|
208 | return self.map.copy() | |
209 |
|
209 | |||
210 | # used to avoid circular references so destructors work |
|
210 | # used to avoid circular references so destructors work | |
211 | def opener(base): |
|
211 | def opener(base): | |
212 | p = base |
|
212 | p = base | |
213 | def o(path, mode="r"): |
|
213 | def o(path, mode="r"): | |
214 | f = os.path.join(p, path) |
|
214 | f = os.path.join(p, path) | |
215 | if p[:7] == "http://": |
|
215 | if p[:7] == "http://": | |
216 | return httprangereader(f) |
|
216 | return httprangereader(f) | |
217 |
|
217 | |||
218 | if mode != "r" and os.path.isfile(f): |
|
218 | if mode != "r" and os.path.isfile(f): | |
219 | s = os.stat(f) |
|
219 | s = os.stat(f) | |
220 | if s.st_nlink > 1: |
|
220 | if s.st_nlink > 1: | |
221 | file(f + ".tmp", "w").write(file(f).read()) |
|
221 | file(f + ".tmp", "w").write(file(f).read()) | |
222 | os.rename(f+".tmp", f) |
|
222 | os.rename(f+".tmp", f) | |
223 |
|
223 | |||
224 | return file(f, mode) |
|
224 | return file(f, mode) | |
225 |
|
225 | |||
226 | return o |
|
226 | return o | |
227 |
|
227 | |||
228 | class repository: |
|
228 | class repository: | |
229 | def __init__(self, ui, path=None, create=0): |
|
229 | def __init__(self, ui, path=None, create=0): | |
230 | self.remote = 0 |
|
230 | self.remote = 0 | |
231 | if path and path[:7] == "http://": |
|
231 | if path and path[:7] == "http://": | |
232 | self.remote = 1 |
|
232 | self.remote = 1 | |
233 | self.path = path |
|
233 | self.path = path | |
234 | else: |
|
234 | else: | |
235 | if not path: |
|
235 | if not path: | |
236 | p = os.getcwd() |
|
236 | p = os.getcwd() | |
237 | while not os.path.isdir(os.path.join(p, ".hg")): |
|
237 | while not os.path.isdir(os.path.join(p, ".hg")): | |
238 | p = os.path.dirname(p) |
|
238 | p = os.path.dirname(p) | |
239 | if p == "/": raise "No repo found" |
|
239 | if p == "/": raise "No repo found" | |
240 | path = p |
|
240 | path = p | |
241 | self.path = os.path.join(path, ".hg") |
|
241 | self.path = os.path.join(path, ".hg") | |
242 |
|
242 | |||
243 | self.root = path |
|
243 | self.root = path | |
244 | self.ui = ui |
|
244 | self.ui = ui | |
245 |
|
245 | |||
246 | if create: |
|
246 | if create: | |
247 | os.mkdir(self.path) |
|
247 | os.mkdir(self.path) | |
248 | os.mkdir(self.join("data")) |
|
248 | os.mkdir(self.join("data")) | |
249 |
|
249 | |||
250 | self.opener = opener(self.path) |
|
250 | self.opener = opener(self.path) | |
251 | self.manifest = manifest(self.opener) |
|
251 | self.manifest = manifest(self.opener) | |
252 | self.changelog = changelog(self.opener) |
|
252 | self.changelog = changelog(self.opener) | |
253 | self.ignorelist = None |
|
253 | self.ignorelist = None | |
254 |
|
254 | |||
255 | if not self.remote: |
|
255 | if not self.remote: | |
256 | self.dircache = dircache(self.opener) |
|
256 | self.dircache = dircache(self.opener) | |
257 | try: |
|
257 | try: | |
258 | self.current = bin(self.opener("current").read()) |
|
258 | self.current = bin(self.opener("current").read()) | |
259 | except IOError: |
|
259 | except IOError: | |
260 | self.current = None |
|
260 | self.current = None | |
261 |
|
261 | |||
262 | def setcurrent(self, node): |
|
262 | def setcurrent(self, node): | |
263 | self.current = node |
|
263 | self.current = node | |
264 | self.opener("current", "w").write(hex(node)) |
|
264 | self.opener("current", "w").write(hex(node)) | |
265 |
|
265 | |||
266 | def ignore(self, f): |
|
266 | def ignore(self, f): | |
267 | if self.ignorelist is None: |
|
267 | if self.ignorelist is None: | |
268 | self.ignorelist = [] |
|
268 | self.ignorelist = [] | |
269 | try: |
|
269 | try: | |
270 | l = open(os.path.join(self.root, ".hgignore")).readlines() |
|
270 | l = open(os.path.join(self.root, ".hgignore")).readlines() | |
271 | for pat in l: |
|
271 | for pat in l: | |
272 | if pat != "\n": |
|
272 | if pat != "\n": | |
273 | self.ignorelist.append(re.compile(pat[:-1])) |
|
273 | self.ignorelist.append(re.compile(pat[:-1])) | |
274 | except IOError: pass |
|
274 | except IOError: pass | |
275 | for pat in self.ignorelist: |
|
275 | for pat in self.ignorelist: | |
276 | if pat.search(f): return True |
|
276 | if pat.search(f): return True | |
277 | return False |
|
277 | return False | |
278 |
|
278 | |||
279 | def join(self, f): |
|
279 | def join(self, f): | |
280 | return os.path.join(self.path, f) |
|
280 | return os.path.join(self.path, f) | |
281 |
|
281 | |||
282 | def file(self, f): |
|
282 | def file(self, f): | |
283 | return filelog(self.opener, f) |
|
283 | return filelog(self.opener, f) | |
284 |
|
284 | |||
285 | def transaction(self): |
|
285 | def transaction(self): | |
286 | return transaction(self.opener, self.join("journal")) |
|
286 | return transaction(self.opener, self.join("journal")) | |
287 |
|
287 | |||
288 | def merge(self, other): |
|
288 | def merge(self, other): | |
289 | tr = self.transaction() |
|
289 | tr = self.transaction() | |
290 | changed = {} |
|
290 | changed = {} | |
291 | new = {} |
|
291 | new = {} | |
292 |
|
|
292 | seqrev = self.changelog.count() | |
|
293 | # some magic to allow fiddling in nested scope | |||
|
294 | nextrev = [seqrev] | |||
293 |
|
295 | |||
294 | # helpers for back-linking file revisions to local changeset |
|
296 | # helpers for back-linking file revisions to local changeset | |
295 | # revisions so we can immediately get to changeset from annotate |
|
297 | # revisions so we can immediately get to changeset from annotate | |
296 | def accumulate(text): |
|
298 | def accumulate(text): | |
297 | n = nextrev |
|
|||
298 | # track which files are added in which changeset and the |
|
299 | # track which files are added in which changeset and the | |
299 | # corresponding _local_ changeset revision |
|
300 | # corresponding _local_ changeset revision | |
300 | files = self.changelog.extract(text)[3] |
|
301 | files = self.changelog.extract(text)[3] | |
301 | for f in files: |
|
302 | for f in files: | |
302 | changed.setdefault(f, []).append(n) |
|
303 | changed.setdefault(f, []).append(nextrev[0]) | |
303 | n += 1 |
|
304 | nextrev[0] += 1 | |
304 |
|
305 | |||
305 | def seq(start): |
|
306 | def seq(start): | |
306 | while 1: |
|
307 | while 1: | |
307 | yield start |
|
308 | yield start | |
308 | start += 1 |
|
309 | start += 1 | |
309 |
|
310 | |||
310 | def lseq(l): |
|
311 | def lseq(l): | |
311 | for r in l: |
|
312 | for r in l: | |
312 | yield r |
|
313 | yield r | |
313 |
|
314 | |||
314 | # begin the import/merge of changesets |
|
315 | # begin the import/merge of changesets | |
315 | self.ui.status("merging new changesets\n") |
|
316 | self.ui.status("merging new changesets\n") | |
316 | (co, cn) = self.changelog.mergedag(other.changelog, tr, |
|
317 | (co, cn) = self.changelog.mergedag(other.changelog, tr, | |
317 | seq(seqrev), accumulate) |
|
318 | seq(seqrev), accumulate) | |
318 | resolverev = self.changelog.count() |
|
319 | resolverev = self.changelog.count() | |
319 |
|
320 | |||
320 | # is there anything to do? |
|
321 | # is there anything to do? | |
321 | if co == cn: |
|
322 | if co == cn: | |
322 | tr.close() |
|
323 | tr.close() | |
323 | return |
|
324 | return | |
324 |
|
325 | |||
325 | # do we need to resolve? |
|
326 | # do we need to resolve? | |
326 | simple = (co == self.changelog.ancestor(co, cn)) |
|
327 | simple = (co == self.changelog.ancestor(co, cn)) | |
327 |
|
328 | |||
328 | # merge all files changed by the changesets, |
|
329 | # merge all files changed by the changesets, | |
329 | # keeping track of the new tips |
|
330 | # keeping track of the new tips | |
330 | changelist = changed.keys() |
|
331 | changelist = changed.keys() | |
331 | changelist.sort() |
|
332 | changelist.sort() | |
332 | for f in changelist: |
|
333 | for f in changelist: | |
333 | sys.stdout.write(".") |
|
334 | sys.stdout.write(".") | |
334 | sys.stdout.flush() |
|
335 | sys.stdout.flush() | |
335 | r = self.file(f) |
|
336 | r = self.file(f) | |
336 | node = r.merge(other.file(f), tr, lseq(changed[f]), resolverev) |
|
337 | node = r.merge(other.file(f), tr, lseq(changed[f]), resolverev) | |
337 | if node: |
|
338 | if node: | |
338 | new[f] = node |
|
339 | new[f] = node | |
339 | sys.stdout.write("\n") |
|
340 | sys.stdout.write("\n") | |
340 |
|
341 | |||
341 | # begin the merge of the manifest |
|
342 | # begin the merge of the manifest | |
342 | self.ui.status("merging manifests\n") |
|
343 | self.ui.status("merging manifests\n") | |
343 | (mm, mo) = self.manifest.mergedag(other.manifest, tr, seq(seqrev)) |
|
344 | (mm, mo) = self.manifest.mergedag(other.manifest, tr, seq(seqrev)) | |
344 |
|
345 | |||
345 | # For simple merges, we don't need to resolve manifests or changesets |
|
346 | # For simple merges, we don't need to resolve manifests or changesets | |
346 | if simple: |
|
347 | if simple: | |
347 | tr.close() |
|
348 | tr.close() | |
348 | return |
|
349 | return | |
349 |
|
350 | |||
350 | ma = self.manifest.ancestor(mm, mo) |
|
351 | ma = self.manifest.ancestor(mm, mo) | |
351 |
|
352 | |||
352 | # resolve the manifest to point to all the merged files |
|
353 | # resolve the manifest to point to all the merged files | |
353 | self.ui.status("resolving manifests\n") |
|
354 | self.ui.status("resolving manifests\n") | |
354 | mmap = self.manifest.read(mm) # mine |
|
355 | mmap = self.manifest.read(mm) # mine | |
355 | omap = self.manifest.read(mo) # other |
|
356 | omap = self.manifest.read(mo) # other | |
356 | amap = self.manifest.read(ma) # ancestor |
|
357 | amap = self.manifest.read(ma) # ancestor | |
357 | nmap = {} |
|
358 | nmap = {} | |
358 |
|
359 | |||
359 | for f, mid in mmap.iteritems(): |
|
360 | for f, mid in mmap.iteritems(): | |
360 | if f in omap: |
|
361 | if f in omap: | |
361 | if mid != omap[f]: |
|
362 | if mid != omap[f]: | |
362 | nmap[f] = new.get(f, mid) # use merged version |
|
363 | nmap[f] = new.get(f, mid) # use merged version | |
363 | else: |
|
364 | else: | |
364 | nmap[f] = new.get(f, mid) # they're the same |
|
365 | nmap[f] = new.get(f, mid) # they're the same | |
365 | del omap[f] |
|
366 | del omap[f] | |
366 | elif f in amap: |
|
367 | elif f in amap: | |
367 | if mid != amap[f]: |
|
368 | if mid != amap[f]: | |
368 | pass # we should prompt here |
|
369 | pass # we should prompt here | |
369 | else: |
|
370 | else: | |
370 | pass # other deleted it |
|
371 | pass # other deleted it | |
371 | else: |
|
372 | else: | |
372 | nmap[f] = new.get(f, mid) # we created it |
|
373 | nmap[f] = new.get(f, mid) # we created it | |
373 |
|
374 | |||
374 | del mmap |
|
375 | del mmap | |
375 |
|
376 | |||
376 | for f, oid in omap.iteritems(): |
|
377 | for f, oid in omap.iteritems(): | |
377 | if f in amap: |
|
378 | if f in amap: | |
378 | if oid != amap[f]: |
|
379 | if oid != amap[f]: | |
379 | pass # this is the nasty case, we should prompt |
|
380 | pass # this is the nasty case, we should prompt | |
380 | else: |
|
381 | else: | |
381 | pass # probably safe |
|
382 | pass # probably safe | |
382 | else: |
|
383 | else: | |
383 | nmap[f] = new.get(f, oid) # remote created it |
|
384 | nmap[f] = new.get(f, oid) # remote created it | |
384 |
|
385 | |||
385 | del omap |
|
386 | del omap | |
386 | del amap |
|
387 | del amap | |
387 |
|
388 | |||
388 | node = self.manifest.add(nmap, tr, resolverev, mm, mo) |
|
389 | node = self.manifest.add(nmap, tr, resolverev, mm, mo) | |
389 |
|
390 | |||
390 | # Now all files and manifests are merged, we add the changed files |
|
391 | # Now all files and manifests are merged, we add the changed files | |
391 | # and manifest id to the changelog |
|
392 | # and manifest id to the changelog | |
392 | self.ui.status("committing merge changeset\n") |
|
393 | self.ui.status("committing merge changeset\n") | |
393 | new = new.keys() |
|
394 | new = new.keys() | |
394 | new.sort() |
|
395 | new.sort() | |
395 | if co == cn: cn = -1 |
|
396 | if co == cn: cn = -1 | |
396 |
|
397 | |||
397 | edittext = "\n"+"".join(["HG: changed %s\n" % f for f in new]) |
|
398 | edittext = "\n"+"".join(["HG: changed %s\n" % f for f in new]) | |
398 | edittext = self.ui.edit(edittext) |
|
399 | edittext = self.ui.edit(edittext) | |
399 | n = self.changelog.add(node, new, edittext, tr, co, cn) |
|
400 | n = self.changelog.add(node, new, edittext, tr, co, cn) | |
400 |
|
401 | |||
401 | tr.close() |
|
402 | tr.close() | |
402 |
|
403 | |||
403 | def commit(self, update = None, text = ""): |
|
404 | def commit(self, update = None, text = ""): | |
404 | tr = self.transaction() |
|
405 | tr = self.transaction() | |
405 |
|
406 | |||
406 | try: |
|
407 | try: | |
407 | remove = [ l[:-1] for l in self.opener("to-remove") ] |
|
408 | remove = [ l[:-1] for l in self.opener("to-remove") ] | |
408 | os.unlink(self.join("to-remove")) |
|
409 | os.unlink(self.join("to-remove")) | |
409 |
|
410 | |||
410 | except IOError: |
|
411 | except IOError: | |
411 | remove = [] |
|
412 | remove = [] | |
412 |
|
413 | |||
413 | if update == None: |
|
414 | if update == None: | |
414 | update = self.diffdir(self.root)[0] |
|
415 | update = self.diffdir(self.root)[0] | |
415 |
|
416 | |||
416 | # check in files |
|
417 | # check in files | |
417 | new = {} |
|
418 | new = {} | |
418 | linkrev = self.changelog.count() |
|
419 | linkrev = self.changelog.count() | |
419 | for f in update: |
|
420 | for f in update: | |
420 | try: |
|
421 | try: | |
421 | t = file(f).read() |
|
422 | t = file(f).read() | |
422 | except IOError: |
|
423 | except IOError: | |
423 | remove.append(f) |
|
424 | remove.append(f) | |
424 | continue |
|
425 | continue | |
425 | r = self.file(f) |
|
426 | r = self.file(f) | |
426 | new[f] = r.add(t, tr, linkrev) |
|
427 | new[f] = r.add(t, tr, linkrev) | |
427 |
|
428 | |||
428 | # update manifest |
|
429 | # update manifest | |
429 | mmap = self.manifest.read(self.manifest.tip()) |
|
430 | mmap = self.manifest.read(self.manifest.tip()) | |
430 | mmap.update(new) |
|
431 | mmap.update(new) | |
431 | for f in remove: |
|
432 | for f in remove: | |
432 | del mmap[f] |
|
433 | del mmap[f] | |
433 | mnode = self.manifest.add(mmap, tr, linkrev) |
|
434 | mnode = self.manifest.add(mmap, tr, linkrev) | |
434 |
|
435 | |||
435 | # add changeset |
|
436 | # add changeset | |
436 | new = new.keys() |
|
437 | new = new.keys() | |
437 | new.sort() |
|
438 | new.sort() | |
438 |
|
439 | |||
439 | edittext = text + "\n"+"".join(["HG: changed %s\n" % f for f in new]) |
|
440 | edittext = text + "\n"+"".join(["HG: changed %s\n" % f for f in new]) | |
440 | edittext = self.ui.edit(edittext) |
|
441 | edittext = self.ui.edit(edittext) | |
441 |
|
442 | |||
442 | n = self.changelog.add(mnode, new, edittext, tr) |
|
443 | n = self.changelog.add(mnode, new, edittext, tr) | |
443 | tr.close() |
|
444 | tr.close() | |
444 |
|
445 | |||
445 | self.setcurrent(n) |
|
446 | self.setcurrent(n) | |
446 | self.dircache.update(new) |
|
447 | self.dircache.update(new) | |
447 | self.dircache.remove(remove) |
|
448 | self.dircache.remove(remove) | |
448 |
|
449 | |||
449 | def checkdir(self, path): |
|
450 | def checkdir(self, path): | |
450 | d = os.path.dirname(path) |
|
451 | d = os.path.dirname(path) | |
451 | if not d: return |
|
452 | if not d: return | |
452 | if not os.path.isdir(d): |
|
453 | if not os.path.isdir(d): | |
453 | self.checkdir(d) |
|
454 | self.checkdir(d) | |
454 | os.mkdir(d) |
|
455 | os.mkdir(d) | |
455 |
|
456 | |||
456 | def checkout(self, node): |
|
457 | def checkout(self, node): | |
457 | # checkout is really dumb at the moment |
|
458 | # checkout is really dumb at the moment | |
458 | # it ought to basically merge |
|
459 | # it ought to basically merge | |
459 | change = self.changelog.read(node) |
|
460 | change = self.changelog.read(node) | |
460 | mmap = self.manifest.read(change[0]) |
|
461 | mmap = self.manifest.read(change[0]) | |
461 |
|
462 | |||
462 | l = mmap.keys() |
|
463 | l = mmap.keys() | |
463 | l.sort() |
|
464 | l.sort() | |
464 | stats = [] |
|
465 | stats = [] | |
465 | for f in l: |
|
466 | for f in l: | |
466 | r = self.file(f) |
|
467 | r = self.file(f) | |
467 | t = r.revision(mmap[f]) |
|
468 | t = r.revision(mmap[f]) | |
468 | try: |
|
469 | try: | |
469 | file(f, "w").write(t) |
|
470 | file(f, "w").write(t) | |
470 | except: |
|
471 | except: | |
471 | self.checkdir(f) |
|
472 | self.checkdir(f) | |
472 | file(f, "w").write(t) |
|
473 | file(f, "w").write(t) | |
473 |
|
474 | |||
474 | self.setcurrent(node) |
|
475 | self.setcurrent(node) | |
475 | self.dircache.clear() |
|
476 | self.dircache.clear() | |
476 | self.dircache.update(l) |
|
477 | self.dircache.update(l) | |
477 |
|
478 | |||
478 | def diffdir(self, path): |
|
479 | def diffdir(self, path): | |
479 | dc = self.dircache.copy() |
|
480 | dc = self.dircache.copy() | |
480 | changed = [] |
|
481 | changed = [] | |
481 | mf = {} |
|
482 | mf = {} | |
482 | added = [] |
|
483 | added = [] | |
483 |
|
484 | |||
484 | if self.current: |
|
485 | if self.current: | |
485 | change = self.changelog.read(self.current) |
|
486 | change = self.changelog.read(self.current) | |
486 | mf = self.manifest.read(change[0]) |
|
487 | mf = self.manifest.read(change[0]) | |
487 |
|
488 | |||
488 | for dir, subdirs, files in os.walk(self.root): |
|
489 | for dir, subdirs, files in os.walk(self.root): | |
489 | d = dir[len(self.root)+1:] |
|
490 | d = dir[len(self.root)+1:] | |
490 | if ".hg" in subdirs: subdirs.remove(".hg") |
|
491 | if ".hg" in subdirs: subdirs.remove(".hg") | |
491 |
|
492 | |||
492 | for f in files: |
|
493 | for f in files: | |
493 | fn = os.path.join(d, f) |
|
494 | fn = os.path.join(d, f) | |
494 | try: s = os.stat(fn) |
|
495 | try: s = os.stat(fn) | |
495 | except: continue |
|
496 | except: continue | |
496 | if fn in dc: |
|
497 | if fn in dc: | |
497 | c = dc[fn] |
|
498 | c = dc[fn] | |
498 | del dc[fn] |
|
499 | del dc[fn] | |
499 | if c[1] != s.st_size: |
|
500 | if c[1] != s.st_size: | |
500 | changed.append(fn) |
|
501 | changed.append(fn) | |
501 | elif c[0] != s.st_mode or c[2] != s.st_mtime: |
|
502 | elif c[0] != s.st_mode or c[2] != s.st_mtime: | |
502 | t1 = file(fn).read() |
|
503 | t1 = file(fn).read() | |
503 | t2 = self.file(fn).revision(mf[fn]) |
|
504 | t2 = self.file(fn).revision(mf[fn]) | |
504 | if t1 != t2: |
|
505 | if t1 != t2: | |
505 | changed.append(fn) |
|
506 | changed.append(fn) | |
506 | else: |
|
507 | else: | |
507 | if self.ignore(fn): continue |
|
508 | if self.ignore(fn): continue | |
508 | added.append(fn) |
|
509 | added.append(fn) | |
509 |
|
510 | |||
510 | deleted = dc.keys() |
|
511 | deleted = dc.keys() | |
511 | deleted.sort() |
|
512 | deleted.sort() | |
512 |
|
513 | |||
513 | return (changed, added, deleted) |
|
514 | return (changed, added, deleted) | |
514 |
|
515 | |||
515 | def add(self, list): |
|
516 | def add(self, list): | |
516 | self.dircache.taint(list) |
|
517 | self.dircache.taint(list) | |
517 |
|
518 | |||
518 | def remove(self, list): |
|
519 | def remove(self, list): | |
519 | dl = self.opener("to-remove", "a") |
|
520 | dl = self.opener("to-remove", "a") | |
520 | for f in list: |
|
521 | for f in list: | |
521 | dl.write(f + "\n") |
|
522 | dl.write(f + "\n") | |
522 |
|
523 | |||
523 | class ui: |
|
524 | class ui: | |
524 | def __init__(self, verbose=False, debug=False): |
|
525 | def __init__(self, verbose=False, debug=False): | |
525 | self.verbose = verbose |
|
526 | self.verbose = verbose | |
526 | def write(self, *args): |
|
527 | def write(self, *args): | |
527 | for a in args: |
|
528 | for a in args: | |
528 | sys.stdout.write(str(a)) |
|
529 | sys.stdout.write(str(a)) | |
529 | def prompt(self, msg, pat): |
|
530 | def prompt(self, msg, pat): | |
530 | while 1: |
|
531 | while 1: | |
531 | sys.stdout.write(msg) |
|
532 | sys.stdout.write(msg) | |
532 | r = sys.stdin.readline()[:-1] |
|
533 | r = sys.stdin.readline()[:-1] | |
533 | if re.match(pat, r): |
|
534 | if re.match(pat, r): | |
534 | return r |
|
535 | return r | |
535 | def status(self, *msg): |
|
536 | def status(self, *msg): | |
536 | self.write(*msg) |
|
537 | self.write(*msg) | |
537 | def warn(self, msg): |
|
538 | def warn(self, msg): | |
538 | self.write(*msg) |
|
539 | self.write(*msg) | |
539 | def note(self, msg): |
|
540 | def note(self, msg): | |
540 | if self.verbose: self.write(*msg) |
|
541 | if self.verbose: self.write(*msg) | |
541 | def debug(self, msg): |
|
542 | def debug(self, msg): | |
542 | if self.debug: self.write(*msg) |
|
543 | if self.debug: self.write(*msg) | |
543 | def edit(self, text): |
|
544 | def edit(self, text): | |
544 | (fd, name) = tempfile.mkstemp("hg") |
|
545 | (fd, name) = tempfile.mkstemp("hg") | |
545 | f = os.fdopen(fd, "w") |
|
546 | f = os.fdopen(fd, "w") | |
546 | f.write(text) |
|
547 | f.write(text) | |
547 | f.close() |
|
548 | f.close() | |
548 |
|
549 | |||
549 | editor = os.environ.get("EDITOR", "vi") |
|
550 | editor = os.environ.get("EDITOR", "vi") | |
550 | r = os.system("%s %s" % (editor, name)) |
|
551 | r = os.system("%s %s" % (editor, name)) | |
551 | if r: |
|
552 | if r: | |
552 | raise "Edit failed!" |
|
553 | raise "Edit failed!" | |
553 |
|
554 | |||
554 | t = open(name).read() |
|
555 | t = open(name).read() | |
555 | t = re.sub("(?m)^HG:.*\n", "", t) |
|
556 | t = re.sub("(?m)^HG:.*\n", "", t) | |
556 |
|
557 | |||
557 | return t |
|
558 | return t | |
558 |
|
559 | |||
559 |
|
560 | |||
560 | class httprangereader: |
|
561 | class httprangereader: | |
561 | def __init__(self, url): |
|
562 | def __init__(self, url): | |
562 | self.url = url |
|
563 | self.url = url | |
563 | self.pos = 0 |
|
564 | self.pos = 0 | |
564 | def seek(self, pos): |
|
565 | def seek(self, pos): | |
565 | self.pos = pos |
|
566 | self.pos = pos | |
566 | def read(self, bytes=None): |
|
567 | def read(self, bytes=None): | |
567 | opener = urllib2.build_opener(byterange.HTTPRangeHandler()) |
|
568 | opener = urllib2.build_opener(byterange.HTTPRangeHandler()) | |
568 | urllib2.install_opener(opener) |
|
569 | urllib2.install_opener(opener) | |
569 | req = urllib2.Request(self.url) |
|
570 | req = urllib2.Request(self.url) | |
570 | end = '' |
|
571 | end = '' | |
571 | if bytes: end = self.pos + bytes |
|
572 | if bytes: end = self.pos + bytes | |
572 | req.add_header('Range', 'bytes=%d-%s' % (self.pos, end)) |
|
573 | req.add_header('Range', 'bytes=%d-%s' % (self.pos, end)) | |
573 | f = urllib2.urlopen(req) |
|
574 | f = urllib2.urlopen(req) | |
574 | return f.read() |
|
575 | return f.read() |
General Comments 0
You need to be logged in to leave comments.
Login now