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