##// END OF EJS Templates
Check if repository exists...
mpm@selenic.com -
r405:99470ae6 default
parent child Browse files
Show More
@@ -1,1425 +1,1428 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, os
8 import sys, struct, os
9 from revlog import *
9 from revlog import *
10 from demandload import *
10 from demandload import *
11 demandload(globals(), "re lock urllib urllib2 transaction time socket")
11 demandload(globals(), "re lock urllib urllib2 transaction time socket")
12 demandload(globals(), "tempfile httprangereader difflib")
12 demandload(globals(), "tempfile httprangereader difflib")
13
13
14 def is_exec(f):
14 def is_exec(f):
15 return (os.stat(f).st_mode & 0100 != 0)
15 return (os.stat(f).st_mode & 0100 != 0)
16
16
17 def set_exec(f, mode):
17 def set_exec(f, mode):
18 s = os.stat(f).st_mode
18 s = os.stat(f).st_mode
19 if (s & 0100 != 0) == mode:
19 if (s & 0100 != 0) == mode:
20 return
20 return
21 if mode:
21 if mode:
22 # Turn on +x for every +r bit when making a file executable
22 # Turn on +x for every +r bit when making a file executable
23 # and obey umask.
23 # and obey umask.
24 umask = os.umask(0)
24 umask = os.umask(0)
25 os.umask(umask)
25 os.umask(umask)
26 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
26 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
27 else:
27 else:
28 os.chmod(f, s & 0666)
28 os.chmod(f, s & 0666)
29
29
30 class filelog(revlog):
30 class filelog(revlog):
31 def __init__(self, opener, path):
31 def __init__(self, opener, path):
32 revlog.__init__(self, opener,
32 revlog.__init__(self, opener,
33 os.path.join("data", path + ".i"),
33 os.path.join("data", path + ".i"),
34 os.path.join("data", path + ".d"))
34 os.path.join("data", path + ".d"))
35
35
36 def read(self, node):
36 def read(self, node):
37 t = self.revision(node)
37 t = self.revision(node)
38 if t[:2] != '\1\n':
38 if t[:2] != '\1\n':
39 return t
39 return t
40 s = t.find('\1\n', 2)
40 s = t.find('\1\n', 2)
41 return t[s+2:]
41 return t[s+2:]
42
42
43 def readmeta(self, node):
43 def readmeta(self, node):
44 t = self.revision(node)
44 t = self.revision(node)
45 if t[:2] != '\1\n':
45 if t[:2] != '\1\n':
46 return t
46 return t
47 s = t.find('\1\n', 2)
47 s = t.find('\1\n', 2)
48 mt = t[2:s]
48 mt = t[2:s]
49 for l in mt.splitlines():
49 for l in mt.splitlines():
50 k, v = l.split(": ", 1)
50 k, v = l.split(": ", 1)
51 m[k] = v
51 m[k] = v
52 return m
52 return m
53
53
54 def add(self, text, meta, transaction, link, p1=None, p2=None):
54 def add(self, text, meta, transaction, link, p1=None, p2=None):
55 if meta or text[:2] == '\1\n':
55 if meta or text[:2] == '\1\n':
56 mt = ""
56 mt = ""
57 if meta:
57 if meta:
58 mt = [ "%s: %s\n" % (k, v) for k,v in meta.items() ]
58 mt = [ "%s: %s\n" % (k, v) for k,v in meta.items() ]
59 text = "\1\n" + "".join(mt) + "\1\n" + text
59 text = "\1\n" + "".join(mt) + "\1\n" + text
60 return self.addrevision(text, transaction, link, p1, p2)
60 return self.addrevision(text, transaction, link, p1, p2)
61
61
62 def annotate(self, node):
62 def annotate(self, node):
63
63
64 def decorate(text, rev):
64 def decorate(text, rev):
65 return [(rev, l) for l in text.splitlines(1)]
65 return [(rev, l) for l in text.splitlines(1)]
66
66
67 def strip(annotation):
67 def strip(annotation):
68 return [e[1] for e in annotation]
68 return [e[1] for e in annotation]
69
69
70 def pair(parent, child):
70 def pair(parent, child):
71 new = []
71 new = []
72 sm = difflib.SequenceMatcher(None, strip(parent), strip(child))
72 sm = difflib.SequenceMatcher(None, strip(parent), strip(child))
73 for o, m, n, s, t in sm.get_opcodes():
73 for o, m, n, s, t in sm.get_opcodes():
74 if o == 'equal':
74 if o == 'equal':
75 new += parent[m:n]
75 new += parent[m:n]
76 else:
76 else:
77 new += child[s:t]
77 new += child[s:t]
78 return new
78 return new
79
79
80 # find all ancestors
80 # find all ancestors
81 needed = {node:1}
81 needed = {node:1}
82 visit = [node]
82 visit = [node]
83 while visit:
83 while visit:
84 n = visit.pop(0)
84 n = visit.pop(0)
85 for p in self.parents(n):
85 for p in self.parents(n):
86 if p not in needed:
86 if p not in needed:
87 needed[p] = 1
87 needed[p] = 1
88 visit.append(p)
88 visit.append(p)
89 else:
89 else:
90 # count how many times we'll use this
90 # count how many times we'll use this
91 needed[p] += 1
91 needed[p] += 1
92
92
93 # sort by revision which is a topological order
93 # sort by revision which is a topological order
94 visit = needed.keys()
94 visit = needed.keys()
95 visit = [ (self.rev(n), n) for n in visit ]
95 visit = [ (self.rev(n), n) for n in visit ]
96 visit.sort()
96 visit.sort()
97 visit = [ p[1] for p in visit ]
97 visit = [ p[1] for p in visit ]
98 hist = {}
98 hist = {}
99
99
100 for n in visit:
100 for n in visit:
101 curr = decorate(self.read(n), self.linkrev(n))
101 curr = decorate(self.read(n), self.linkrev(n))
102 for p in self.parents(n):
102 for p in self.parents(n):
103 if p != nullid:
103 if p != nullid:
104 curr = pair(hist[p], curr)
104 curr = pair(hist[p], curr)
105 # trim the history of unneeded revs
105 # trim the history of unneeded revs
106 needed[p] -= 1
106 needed[p] -= 1
107 if not needed[p]:
107 if not needed[p]:
108 del hist[p]
108 del hist[p]
109 hist[n] = curr
109 hist[n] = curr
110
110
111 return hist[n]
111 return hist[n]
112
112
113 class manifest(revlog):
113 class manifest(revlog):
114 def __init__(self, opener):
114 def __init__(self, opener):
115 self.mapcache = None
115 self.mapcache = None
116 self.listcache = None
116 self.listcache = None
117 self.addlist = None
117 self.addlist = None
118 revlog.__init__(self, opener, "00manifest.i", "00manifest.d")
118 revlog.__init__(self, opener, "00manifest.i", "00manifest.d")
119
119
120 def read(self, node):
120 def read(self, node):
121 if node == nullid: return {} # don't upset local cache
121 if node == nullid: return {} # don't upset local cache
122 if self.mapcache and self.mapcache[0] == node:
122 if self.mapcache and self.mapcache[0] == node:
123 return self.mapcache[1].copy()
123 return self.mapcache[1].copy()
124 text = self.revision(node)
124 text = self.revision(node)
125 map = {}
125 map = {}
126 flag = {}
126 flag = {}
127 self.listcache = (text, text.splitlines(1))
127 self.listcache = (text, text.splitlines(1))
128 for l in self.listcache[1]:
128 for l in self.listcache[1]:
129 (f, n) = l.split('\0')
129 (f, n) = l.split('\0')
130 map[f] = bin(n[:40])
130 map[f] = bin(n[:40])
131 flag[f] = (n[40:-1] == "x")
131 flag[f] = (n[40:-1] == "x")
132 self.mapcache = (node, map, flag)
132 self.mapcache = (node, map, flag)
133 return map
133 return map
134
134
135 def readflags(self, node):
135 def readflags(self, node):
136 if node == nullid: return {} # don't upset local cache
136 if node == nullid: return {} # don't upset local cache
137 if not self.mapcache or self.mapcache[0] != node:
137 if not self.mapcache or self.mapcache[0] != node:
138 self.read(node)
138 self.read(node)
139 return self.mapcache[2]
139 return self.mapcache[2]
140
140
141 def diff(self, a, b):
141 def diff(self, a, b):
142 # this is sneaky, as we're not actually using a and b
142 # this is sneaky, as we're not actually using a and b
143 if self.listcache and self.addlist and self.listcache[0] == a:
143 if self.listcache and self.addlist and self.listcache[0] == a:
144 d = mdiff.diff(self.listcache[1], self.addlist, 1)
144 d = mdiff.diff(self.listcache[1], self.addlist, 1)
145 if mdiff.patch(a, d) != b:
145 if mdiff.patch(a, d) != b:
146 sys.stderr.write("*** sortdiff failed, falling back ***\n")
146 sys.stderr.write("*** sortdiff failed, falling back ***\n")
147 return mdiff.textdiff(a, b)
147 return mdiff.textdiff(a, b)
148 return d
148 return d
149 else:
149 else:
150 return mdiff.textdiff(a, b)
150 return mdiff.textdiff(a, b)
151
151
152 def add(self, map, flags, transaction, link, p1=None, p2=None):
152 def add(self, map, flags, transaction, link, p1=None, p2=None):
153 files = map.keys()
153 files = map.keys()
154 files.sort()
154 files.sort()
155
155
156 self.addlist = ["%s\000%s%s\n" %
156 self.addlist = ["%s\000%s%s\n" %
157 (f, hex(map[f]), flags[f] and "x" or '')
157 (f, hex(map[f]), flags[f] and "x" or '')
158 for f in files]
158 for f in files]
159 text = "".join(self.addlist)
159 text = "".join(self.addlist)
160
160
161 n = self.addrevision(text, transaction, link, p1, p2)
161 n = self.addrevision(text, transaction, link, p1, p2)
162 self.mapcache = (n, map, flags)
162 self.mapcache = (n, map, flags)
163 self.listcache = (text, self.addlist)
163 self.listcache = (text, self.addlist)
164 self.addlist = None
164 self.addlist = None
165
165
166 return n
166 return n
167
167
168 class changelog(revlog):
168 class changelog(revlog):
169 def __init__(self, opener):
169 def __init__(self, opener):
170 revlog.__init__(self, opener, "00changelog.i", "00changelog.d")
170 revlog.__init__(self, opener, "00changelog.i", "00changelog.d")
171
171
172 def extract(self, text):
172 def extract(self, text):
173 if not text:
173 if not text:
174 return (nullid, "", "0", [], "")
174 return (nullid, "", "0", [], "")
175 last = text.index("\n\n")
175 last = text.index("\n\n")
176 desc = text[last + 2:]
176 desc = text[last + 2:]
177 l = text[:last].splitlines()
177 l = text[:last].splitlines()
178 manifest = bin(l[0])
178 manifest = bin(l[0])
179 user = l[1]
179 user = l[1]
180 date = l[2]
180 date = l[2]
181 files = l[3:]
181 files = l[3:]
182 return (manifest, user, date, files, desc)
182 return (manifest, user, date, files, desc)
183
183
184 def read(self, node):
184 def read(self, node):
185 return self.extract(self.revision(node))
185 return self.extract(self.revision(node))
186
186
187 def add(self, manifest, list, desc, transaction, p1=None, p2=None,
187 def add(self, manifest, list, desc, transaction, p1=None, p2=None,
188 user=None, date=None):
188 user=None, date=None):
189 user = (user or
189 user = (user or
190 os.environ.get("HGUSER") or
190 os.environ.get("HGUSER") or
191 os.environ.get("EMAIL") or
191 os.environ.get("EMAIL") or
192 os.environ.get("LOGNAME", "unknown") + '@' + socket.getfqdn())
192 os.environ.get("LOGNAME", "unknown") + '@' + socket.getfqdn())
193 date = date or "%d %d" % (time.time(), time.timezone)
193 date = date or "%d %d" % (time.time(), time.timezone)
194 list.sort()
194 list.sort()
195 l = [hex(manifest), user, date] + list + ["", desc]
195 l = [hex(manifest), user, date] + list + ["", desc]
196 text = "\n".join(l)
196 text = "\n".join(l)
197 return self.addrevision(text, transaction, self.count(), p1, p2)
197 return self.addrevision(text, transaction, self.count(), p1, p2)
198
198
199 class dirstate:
199 class dirstate:
200 def __init__(self, opener, ui, root):
200 def __init__(self, opener, ui, root):
201 self.opener = opener
201 self.opener = opener
202 self.root = root
202 self.root = root
203 self.dirty = 0
203 self.dirty = 0
204 self.ui = ui
204 self.ui = ui
205 self.map = None
205 self.map = None
206 self.pl = None
206 self.pl = None
207 self.copies = {}
207 self.copies = {}
208
208
209 def __del__(self):
209 def __del__(self):
210 if self.dirty:
210 if self.dirty:
211 self.write()
211 self.write()
212
212
213 def __getitem__(self, key):
213 def __getitem__(self, key):
214 try:
214 try:
215 return self.map[key]
215 return self.map[key]
216 except TypeError:
216 except TypeError:
217 self.read()
217 self.read()
218 return self[key]
218 return self[key]
219
219
220 def __contains__(self, key):
220 def __contains__(self, key):
221 if not self.map: self.read()
221 if not self.map: self.read()
222 return key in self.map
222 return key in self.map
223
223
224 def parents(self):
224 def parents(self):
225 if not self.pl:
225 if not self.pl:
226 self.read()
226 self.read()
227 return self.pl
227 return self.pl
228
228
229 def setparents(self, p1, p2 = nullid):
229 def setparents(self, p1, p2 = nullid):
230 self.dirty = 1
230 self.dirty = 1
231 self.pl = p1, p2
231 self.pl = p1, p2
232
232
233 def state(self, key):
233 def state(self, key):
234 try:
234 try:
235 return self[key][0]
235 return self[key][0]
236 except KeyError:
236 except KeyError:
237 return "?"
237 return "?"
238
238
239 def read(self):
239 def read(self):
240 if self.map is not None: return self.map
240 if self.map is not None: return self.map
241
241
242 self.map = {}
242 self.map = {}
243 self.pl = [nullid, nullid]
243 self.pl = [nullid, nullid]
244 try:
244 try:
245 st = self.opener("dirstate").read()
245 st = self.opener("dirstate").read()
246 if not st: return
246 if not st: return
247 except: return
247 except: return
248
248
249 self.pl = [st[:20], st[20: 40]]
249 self.pl = [st[:20], st[20: 40]]
250
250
251 pos = 40
251 pos = 40
252 while pos < len(st):
252 while pos < len(st):
253 e = struct.unpack(">cllll", st[pos:pos+17])
253 e = struct.unpack(">cllll", st[pos:pos+17])
254 l = e[4]
254 l = e[4]
255 pos += 17
255 pos += 17
256 f = st[pos:pos + l]
256 f = st[pos:pos + l]
257 if '\0' in f:
257 if '\0' in f:
258 f, c = f.split('\0')
258 f, c = f.split('\0')
259 self.copies[f] = c
259 self.copies[f] = c
260 self.map[f] = e[:4]
260 self.map[f] = e[:4]
261 pos += l
261 pos += l
262
262
263 def copy(self, source, dest):
263 def copy(self, source, dest):
264 self.read()
264 self.read()
265 self.dirty = 1
265 self.dirty = 1
266 self.copies[dest] = source
266 self.copies[dest] = source
267
267
268 def copied(self, file):
268 def copied(self, file):
269 return self.copies.get(file, None)
269 return self.copies.get(file, None)
270
270
271 def update(self, files, state):
271 def update(self, files, state):
272 ''' current states:
272 ''' current states:
273 n normal
273 n normal
274 m needs merging
274 m needs merging
275 r marked for removal
275 r marked for removal
276 a marked for addition'''
276 a marked for addition'''
277
277
278 if not files: return
278 if not files: return
279 self.read()
279 self.read()
280 self.dirty = 1
280 self.dirty = 1
281 for f in files:
281 for f in files:
282 if state == "r":
282 if state == "r":
283 self.map[f] = ('r', 0, 0, 0)
283 self.map[f] = ('r', 0, 0, 0)
284 else:
284 else:
285 s = os.stat(os.path.join(self.root, f))
285 s = os.stat(os.path.join(self.root, f))
286 self.map[f] = (state, s.st_mode, s.st_size, s.st_mtime)
286 self.map[f] = (state, s.st_mode, s.st_size, s.st_mtime)
287
287
288 def forget(self, files):
288 def forget(self, files):
289 if not files: return
289 if not files: return
290 self.read()
290 self.read()
291 self.dirty = 1
291 self.dirty = 1
292 for f in files:
292 for f in files:
293 try:
293 try:
294 del self.map[f]
294 del self.map[f]
295 except KeyError:
295 except KeyError:
296 self.ui.warn("not in dirstate: %s!\n" % f)
296 self.ui.warn("not in dirstate: %s!\n" % f)
297 pass
297 pass
298
298
299 def clear(self):
299 def clear(self):
300 self.map = {}
300 self.map = {}
301 self.dirty = 1
301 self.dirty = 1
302
302
303 def write(self):
303 def write(self):
304 st = self.opener("dirstate", "w")
304 st = self.opener("dirstate", "w")
305 st.write("".join(self.pl))
305 st.write("".join(self.pl))
306 for f, e in self.map.items():
306 for f, e in self.map.items():
307 c = self.copied(f)
307 c = self.copied(f)
308 if c:
308 if c:
309 f = f + "\0" + c
309 f = f + "\0" + c
310 e = struct.pack(">cllll", e[0], e[1], e[2], e[3], len(f))
310 e = struct.pack(">cllll", e[0], e[1], e[2], e[3], len(f))
311 st.write(e + f)
311 st.write(e + f)
312 self.dirty = 0
312 self.dirty = 0
313
313
314 def dup(self):
314 def dup(self):
315 self.read()
315 self.read()
316 return self.map.copy()
316 return self.map.copy()
317
317
318 # used to avoid circular references so destructors work
318 # used to avoid circular references so destructors work
319 def opener(base):
319 def opener(base):
320 p = base
320 p = base
321 def o(path, mode="r"):
321 def o(path, mode="r"):
322 if p[:7] == "http://":
322 if p[:7] == "http://":
323 f = os.path.join(p, urllib.quote(path))
323 f = os.path.join(p, urllib.quote(path))
324 return httprangereader.httprangereader(f)
324 return httprangereader.httprangereader(f)
325
325
326 f = os.path.join(p, path)
326 f = os.path.join(p, path)
327
327
328 mode += "b" # for that other OS
328 mode += "b" # for that other OS
329
329
330 if mode[0] != "r":
330 if mode[0] != "r":
331 try:
331 try:
332 s = os.stat(f)
332 s = os.stat(f)
333 except OSError:
333 except OSError:
334 d = os.path.dirname(f)
334 d = os.path.dirname(f)
335 if not os.path.isdir(d):
335 if not os.path.isdir(d):
336 os.makedirs(d)
336 os.makedirs(d)
337 else:
337 else:
338 if s.st_nlink > 1:
338 if s.st_nlink > 1:
339 file(f + ".tmp", "w").write(file(f).read())
339 file(f + ".tmp", "w").write(file(f).read())
340 os.rename(f+".tmp", f)
340 os.rename(f+".tmp", f)
341
341
342 return file(f, mode)
342 return file(f, mode)
343
343
344 return o
344 return o
345
345
346 class localrepository:
346 class localrepository:
347 def __init__(self, ui, path=None, create=0):
347 def __init__(self, ui, path=None, create=0):
348 self.remote = 0
348 self.remote = 0
349 if path and path[:7] == "http://":
349 if path and path[:7] == "http://":
350 self.remote = 1
350 self.remote = 1
351 self.path = path
351 self.path = path
352 else:
352 else:
353 if not path:
353 if not path:
354 p = os.getcwd()
354 p = os.getcwd()
355 while not os.path.isdir(os.path.join(p, ".hg")):
355 while not os.path.isdir(os.path.join(p, ".hg")):
356 p = os.path.dirname(p)
356 p = os.path.dirname(p)
357 if p == "/": raise "No repo found"
357 if p == "/": raise "No repo found"
358 path = p
358 path = p
359 self.path = os.path.join(path, ".hg")
359 self.path = os.path.join(path, ".hg")
360
360
361 if not create and not os.path.isdir(self.path):
362 raise "repository %s not found" % self.path
363
361 self.root = path
364 self.root = path
362 self.ui = ui
365 self.ui = ui
363
366
364 if create:
367 if create:
365 os.mkdir(self.path)
368 os.mkdir(self.path)
366 os.mkdir(self.join("data"))
369 os.mkdir(self.join("data"))
367
370
368 self.opener = opener(self.path)
371 self.opener = opener(self.path)
369 self.wopener = opener(self.root)
372 self.wopener = opener(self.root)
370 self.manifest = manifest(self.opener)
373 self.manifest = manifest(self.opener)
371 self.changelog = changelog(self.opener)
374 self.changelog = changelog(self.opener)
372 self.ignorelist = None
375 self.ignorelist = None
373 self.tagscache = None
376 self.tagscache = None
374 self.nodetagscache = None
377 self.nodetagscache = None
375
378
376 if not self.remote:
379 if not self.remote:
377 self.dirstate = dirstate(self.opener, ui, self.root)
380 self.dirstate = dirstate(self.opener, ui, self.root)
378 try:
381 try:
379 self.ui.readconfig(self.opener("hgrc"))
382 self.ui.readconfig(self.opener("hgrc"))
380 except IOError: pass
383 except IOError: pass
381
384
382 def ignore(self, f):
385 def ignore(self, f):
383 if self.ignorelist is None:
386 if self.ignorelist is None:
384 self.ignorelist = []
387 self.ignorelist = []
385 try:
388 try:
386 l = self.wfile(".hgignore")
389 l = self.wfile(".hgignore")
387 for pat in l:
390 for pat in l:
388 if pat != "\n":
391 if pat != "\n":
389 self.ignorelist.append(re.compile(pat[:-1]))
392 self.ignorelist.append(re.compile(pat[:-1]))
390 except IOError: pass
393 except IOError: pass
391 for pat in self.ignorelist:
394 for pat in self.ignorelist:
392 if pat.search(f): return True
395 if pat.search(f): return True
393 return False
396 return False
394
397
395 def tags(self):
398 def tags(self):
396 '''return a mapping of tag to node'''
399 '''return a mapping of tag to node'''
397 if not self.tagscache:
400 if not self.tagscache:
398 self.tagscache = {}
401 self.tagscache = {}
399 try:
402 try:
400 # read each head of the tags file, ending with the tip
403 # read each head of the tags file, ending with the tip
401 # and add each tag found to the map, with "newer" ones
404 # and add each tag found to the map, with "newer" ones
402 # taking precedence
405 # taking precedence
403 fl = self.file(".hgtags")
406 fl = self.file(".hgtags")
404 h = fl.heads()
407 h = fl.heads()
405 h.reverse()
408 h.reverse()
406 for r in h:
409 for r in h:
407 for l in fl.revision(r).splitlines():
410 for l in fl.revision(r).splitlines():
408 if l:
411 if l:
409 n, k = l.split(" ", 1)
412 n, k = l.split(" ", 1)
410 self.tagscache[k.strip()] = bin(n)
413 self.tagscache[k.strip()] = bin(n)
411 except KeyError: pass
414 except KeyError: pass
412 self.tagscache['tip'] = self.changelog.tip()
415 self.tagscache['tip'] = self.changelog.tip()
413
416
414 return self.tagscache
417 return self.tagscache
415
418
416 def tagslist(self):
419 def tagslist(self):
417 '''return a list of tags ordered by revision'''
420 '''return a list of tags ordered by revision'''
418 l = []
421 l = []
419 for t,n in self.tags().items():
422 for t,n in self.tags().items():
420 try:
423 try:
421 r = self.changelog.rev(n)
424 r = self.changelog.rev(n)
422 except:
425 except:
423 r = -2 # sort to the beginning of the list if unknown
426 r = -2 # sort to the beginning of the list if unknown
424 l.append((r,t,n))
427 l.append((r,t,n))
425 l.sort()
428 l.sort()
426 return [(t,n) for r,t,n in l]
429 return [(t,n) for r,t,n in l]
427
430
428 def nodetags(self, node):
431 def nodetags(self, node):
429 '''return the tags associated with a node'''
432 '''return the tags associated with a node'''
430 if not self.nodetagscache:
433 if not self.nodetagscache:
431 self.nodetagscache = {}
434 self.nodetagscache = {}
432 for t,n in self.tags().items():
435 for t,n in self.tags().items():
433 self.nodetagscache.setdefault(n,[]).append(t)
436 self.nodetagscache.setdefault(n,[]).append(t)
434 return self.nodetagscache.get(node, [])
437 return self.nodetagscache.get(node, [])
435
438
436 def lookup(self, key):
439 def lookup(self, key):
437 try:
440 try:
438 return self.tags()[key]
441 return self.tags()[key]
439 except KeyError:
442 except KeyError:
440 return self.changelog.lookup(key)
443 return self.changelog.lookup(key)
441
444
442 def join(self, f):
445 def join(self, f):
443 return os.path.join(self.path, f)
446 return os.path.join(self.path, f)
444
447
445 def wjoin(self, f):
448 def wjoin(self, f):
446 return os.path.join(self.root, f)
449 return os.path.join(self.root, f)
447
450
448 def file(self, f):
451 def file(self, f):
449 if f[0] == '/': f = f[1:]
452 if f[0] == '/': f = f[1:]
450 return filelog(self.opener, f)
453 return filelog(self.opener, f)
451
454
452 def wfile(self, f, mode='r'):
455 def wfile(self, f, mode='r'):
453 return self.wopener(f, mode)
456 return self.wopener(f, mode)
454
457
455 def transaction(self):
458 def transaction(self):
456 # save dirstate for undo
459 # save dirstate for undo
457 try:
460 try:
458 ds = self.opener("dirstate").read()
461 ds = self.opener("dirstate").read()
459 except IOError:
462 except IOError:
460 ds = ""
463 ds = ""
461 self.opener("undo.dirstate", "w").write(ds)
464 self.opener("undo.dirstate", "w").write(ds)
462
465
463 return transaction.transaction(self.opener, self.join("journal"),
466 return transaction.transaction(self.opener, self.join("journal"),
464 self.join("undo"))
467 self.join("undo"))
465
468
466 def recover(self):
469 def recover(self):
467 lock = self.lock()
470 lock = self.lock()
468 if os.path.exists(self.join("recover")):
471 if os.path.exists(self.join("recover")):
469 self.ui.status("attempting to rollback interrupted transaction\n")
472 self.ui.status("attempting to rollback interrupted transaction\n")
470 return transaction.rollback(self.opener, self.join("recover"))
473 return transaction.rollback(self.opener, self.join("recover"))
471 else:
474 else:
472 self.ui.warn("no interrupted transaction available\n")
475 self.ui.warn("no interrupted transaction available\n")
473
476
474 def undo(self):
477 def undo(self):
475 lock = self.lock()
478 lock = self.lock()
476 if os.path.exists(self.join("undo")):
479 if os.path.exists(self.join("undo")):
477 self.ui.status("attempting to rollback last transaction\n")
480 self.ui.status("attempting to rollback last transaction\n")
478 transaction.rollback(self.opener, self.join("undo"))
481 transaction.rollback(self.opener, self.join("undo"))
479 self.dirstate = None
482 self.dirstate = None
480 os.rename(self.join("undo.dirstate"), self.join("dirstate"))
483 os.rename(self.join("undo.dirstate"), self.join("dirstate"))
481 self.dirstate = dirstate(self.opener, self.ui, self.root)
484 self.dirstate = dirstate(self.opener, self.ui, self.root)
482 else:
485 else:
483 self.ui.warn("no undo information available\n")
486 self.ui.warn("no undo information available\n")
484
487
485 def lock(self, wait = 1):
488 def lock(self, wait = 1):
486 try:
489 try:
487 return lock.lock(self.join("lock"), 0)
490 return lock.lock(self.join("lock"), 0)
488 except lock.LockHeld, inst:
491 except lock.LockHeld, inst:
489 if wait:
492 if wait:
490 self.ui.warn("waiting for lock held by %s\n" % inst.args[0])
493 self.ui.warn("waiting for lock held by %s\n" % inst.args[0])
491 return lock.lock(self.join("lock"), wait)
494 return lock.lock(self.join("lock"), wait)
492 raise inst
495 raise inst
493
496
494 def rawcommit(self, files, text, user, date, p1=None, p2=None):
497 def rawcommit(self, files, text, user, date, p1=None, p2=None):
495 p1 = p1 or self.dirstate.parents()[0] or nullid
498 p1 = p1 or self.dirstate.parents()[0] or nullid
496 p2 = p2 or self.dirstate.parents()[1] or nullid
499 p2 = p2 or self.dirstate.parents()[1] or nullid
497 c1 = self.changelog.read(p1)
500 c1 = self.changelog.read(p1)
498 c2 = self.changelog.read(p2)
501 c2 = self.changelog.read(p2)
499 m1 = self.manifest.read(c1[0])
502 m1 = self.manifest.read(c1[0])
500 mf1 = self.manifest.readflags(c1[0])
503 mf1 = self.manifest.readflags(c1[0])
501 m2 = self.manifest.read(c2[0])
504 m2 = self.manifest.read(c2[0])
502
505
503 tr = self.transaction()
506 tr = self.transaction()
504 mm = m1.copy()
507 mm = m1.copy()
505 mfm = mf1.copy()
508 mfm = mf1.copy()
506 linkrev = self.changelog.count()
509 linkrev = self.changelog.count()
507 self.dirstate.setparents(p1, p2)
510 self.dirstate.setparents(p1, p2)
508 for f in files:
511 for f in files:
509 try:
512 try:
510 t = self.wfile(f).read()
513 t = self.wfile(f).read()
511 tm = is_exec(self.wjoin(f))
514 tm = is_exec(self.wjoin(f))
512 r = self.file(f)
515 r = self.file(f)
513 mfm[f] = tm
516 mfm[f] = tm
514 mm[f] = r.add(t, {}, tr, linkrev,
517 mm[f] = r.add(t, {}, tr, linkrev,
515 m1.get(f, nullid), m2.get(f, nullid))
518 m1.get(f, nullid), m2.get(f, nullid))
516 self.dirstate.update([f], "n")
519 self.dirstate.update([f], "n")
517 except IOError:
520 except IOError:
518 try:
521 try:
519 del mm[f]
522 del mm[f]
520 del mfm[f]
523 del mfm[f]
521 self.dirstate.forget([f])
524 self.dirstate.forget([f])
522 except:
525 except:
523 # deleted from p2?
526 # deleted from p2?
524 pass
527 pass
525
528
526 mnode = self.manifest.add(mm, mfm, tr, linkrev, c1[0], c2[0])
529 mnode = self.manifest.add(mm, mfm, tr, linkrev, c1[0], c2[0])
527 n = self.changelog.add(mnode, files, text, tr, p1, p2, user, date)
530 n = self.changelog.add(mnode, files, text, tr, p1, p2, user, date)
528 tr.close()
531 tr.close()
529
532
530 def commit(self, files = None, text = "", user = None, date = None):
533 def commit(self, files = None, text = "", user = None, date = None):
531 commit = []
534 commit = []
532 remove = []
535 remove = []
533 if files:
536 if files:
534 for f in files:
537 for f in files:
535 s = self.dirstate.state(f)
538 s = self.dirstate.state(f)
536 if s in 'nmai':
539 if s in 'nmai':
537 commit.append(f)
540 commit.append(f)
538 elif s == 'r':
541 elif s == 'r':
539 remove.append(f)
542 remove.append(f)
540 else:
543 else:
541 self.ui.warn("%s not tracked!\n" % f)
544 self.ui.warn("%s not tracked!\n" % f)
542 else:
545 else:
543 (c, a, d, u) = self.diffdir(self.root)
546 (c, a, d, u) = self.diffdir(self.root)
544 commit = c + a
547 commit = c + a
545 remove = d
548 remove = d
546
549
547 if not commit and not remove:
550 if not commit and not remove:
548 self.ui.status("nothing changed\n")
551 self.ui.status("nothing changed\n")
549 return
552 return
550
553
551 p1, p2 = self.dirstate.parents()
554 p1, p2 = self.dirstate.parents()
552 c1 = self.changelog.read(p1)
555 c1 = self.changelog.read(p1)
553 c2 = self.changelog.read(p2)
556 c2 = self.changelog.read(p2)
554 m1 = self.manifest.read(c1[0])
557 m1 = self.manifest.read(c1[0])
555 mf1 = self.manifest.readflags(c1[0])
558 mf1 = self.manifest.readflags(c1[0])
556 m2 = self.manifest.read(c2[0])
559 m2 = self.manifest.read(c2[0])
557 lock = self.lock()
560 lock = self.lock()
558 tr = self.transaction()
561 tr = self.transaction()
559
562
560 # check in files
563 # check in files
561 new = {}
564 new = {}
562 linkrev = self.changelog.count()
565 linkrev = self.changelog.count()
563 commit.sort()
566 commit.sort()
564 for f in commit:
567 for f in commit:
565 self.ui.note(f + "\n")
568 self.ui.note(f + "\n")
566 try:
569 try:
567 fp = self.wjoin(f)
570 fp = self.wjoin(f)
568 mf1[f] = is_exec(fp)
571 mf1[f] = is_exec(fp)
569 t = file(fp).read()
572 t = file(fp).read()
570 except IOError:
573 except IOError:
571 self.warn("trouble committing %s!\n" % f)
574 self.warn("trouble committing %s!\n" % f)
572 raise
575 raise
573
576
574 meta = {}
577 meta = {}
575 cp = self.dirstate.copied(f)
578 cp = self.dirstate.copied(f)
576 if cp:
579 if cp:
577 meta["copy"] = cp
580 meta["copy"] = cp
578 meta["copyrev"] = hex(m1.get(cp, m2.get(cp, nullid)))
581 meta["copyrev"] = hex(m1.get(cp, m2.get(cp, nullid)))
579 self.ui.debug(" %s: copy %s:%s\n" % (f, cp, meta["copyrev"]))
582 self.ui.debug(" %s: copy %s:%s\n" % (f, cp, meta["copyrev"]))
580
583
581 r = self.file(f)
584 r = self.file(f)
582 fp1 = m1.get(f, nullid)
585 fp1 = m1.get(f, nullid)
583 fp2 = m2.get(f, nullid)
586 fp2 = m2.get(f, nullid)
584 new[f] = r.add(t, meta, tr, linkrev, fp1, fp2)
587 new[f] = r.add(t, meta, tr, linkrev, fp1, fp2)
585
588
586 # update manifest
589 # update manifest
587 m1.update(new)
590 m1.update(new)
588 for f in remove: del m1[f]
591 for f in remove: del m1[f]
589 mn = self.manifest.add(m1, mf1, tr, linkrev, c1[0], c2[0])
592 mn = self.manifest.add(m1, mf1, tr, linkrev, c1[0], c2[0])
590
593
591 # add changeset
594 # add changeset
592 new = new.keys()
595 new = new.keys()
593 new.sort()
596 new.sort()
594
597
595 if not text:
598 if not text:
596 edittext = "\n" + "HG: manifest hash %s\n" % hex(mn)
599 edittext = "\n" + "HG: manifest hash %s\n" % hex(mn)
597 edittext += "".join(["HG: changed %s\n" % f for f in new])
600 edittext += "".join(["HG: changed %s\n" % f for f in new])
598 edittext += "".join(["HG: removed %s\n" % f for f in remove])
601 edittext += "".join(["HG: removed %s\n" % f for f in remove])
599 edittext = self.ui.edit(edittext)
602 edittext = self.ui.edit(edittext)
600 if not edittext.rstrip():
603 if not edittext.rstrip():
601 return 1
604 return 1
602 text = edittext
605 text = edittext
603
606
604 n = self.changelog.add(mn, new, text, tr, p1, p2, user, date)
607 n = self.changelog.add(mn, new, text, tr, p1, p2, user, date)
605 tr.close()
608 tr.close()
606
609
607 self.dirstate.setparents(n)
610 self.dirstate.setparents(n)
608 self.dirstate.update(new, "n")
611 self.dirstate.update(new, "n")
609 self.dirstate.forget(remove)
612 self.dirstate.forget(remove)
610
613
611 def diffdir(self, path, changeset = None):
614 def diffdir(self, path, changeset = None):
612 changed = []
615 changed = []
613 added = []
616 added = []
614 unknown = []
617 unknown = []
615 mf = {}
618 mf = {}
616
619
617 if changeset:
620 if changeset:
618 change = self.changelog.read(changeset)
621 change = self.changelog.read(changeset)
619 mf = self.manifest.read(change[0])
622 mf = self.manifest.read(change[0])
620 dc = dict.fromkeys(mf)
623 dc = dict.fromkeys(mf)
621 else:
624 else:
622 changeset = self.dirstate.parents()[0]
625 changeset = self.dirstate.parents()[0]
623 change = self.changelog.read(changeset)
626 change = self.changelog.read(changeset)
624 mf = self.manifest.read(change[0])
627 mf = self.manifest.read(change[0])
625 dc = self.dirstate.dup()
628 dc = self.dirstate.dup()
626
629
627 def fcmp(fn):
630 def fcmp(fn):
628 t1 = self.wfile(fn).read()
631 t1 = self.wfile(fn).read()
629 t2 = self.file(fn).revision(mf[fn])
632 t2 = self.file(fn).revision(mf[fn])
630 return cmp(t1, t2)
633 return cmp(t1, t2)
631
634
632 for dir, subdirs, files in os.walk(path):
635 for dir, subdirs, files in os.walk(path):
633 d = dir[len(self.root)+1:]
636 d = dir[len(self.root)+1:]
634 if ".hg" in subdirs: subdirs.remove(".hg")
637 if ".hg" in subdirs: subdirs.remove(".hg")
635
638
636 for f in files:
639 for f in files:
637 fn = os.path.join(d, f)
640 fn = os.path.join(d, f)
638 try: s = os.stat(os.path.join(self.root, fn))
641 try: s = os.stat(os.path.join(self.root, fn))
639 except: continue
642 except: continue
640 if fn in dc:
643 if fn in dc:
641 c = dc[fn]
644 c = dc[fn]
642 del dc[fn]
645 del dc[fn]
643 if not c:
646 if not c:
644 if fcmp(fn):
647 if fcmp(fn):
645 changed.append(fn)
648 changed.append(fn)
646 elif c[0] == 'm':
649 elif c[0] == 'm':
647 changed.append(fn)
650 changed.append(fn)
648 elif c[0] == 'a':
651 elif c[0] == 'a':
649 added.append(fn)
652 added.append(fn)
650 elif c[0] == 'r':
653 elif c[0] == 'r':
651 unknown.append(fn)
654 unknown.append(fn)
652 elif c[2] != s.st_size or (c[1] ^ s.st_mode) & 0100:
655 elif c[2] != s.st_size or (c[1] ^ s.st_mode) & 0100:
653 changed.append(fn)
656 changed.append(fn)
654 elif c[1] != s.st_mode or c[3] != s.st_mtime:
657 elif c[1] != s.st_mode or c[3] != s.st_mtime:
655 if fcmp(fn):
658 if fcmp(fn):
656 changed.append(fn)
659 changed.append(fn)
657 else:
660 else:
658 if self.ignore(fn): continue
661 if self.ignore(fn): continue
659 unknown.append(fn)
662 unknown.append(fn)
660
663
661 deleted = dc.keys()
664 deleted = dc.keys()
662 deleted.sort()
665 deleted.sort()
663
666
664 return (changed, added, deleted, unknown)
667 return (changed, added, deleted, unknown)
665
668
666 def diffrevs(self, node1, node2):
669 def diffrevs(self, node1, node2):
667 changed, added = [], []
670 changed, added = [], []
668
671
669 change = self.changelog.read(node1)
672 change = self.changelog.read(node1)
670 mf1 = self.manifest.read(change[0])
673 mf1 = self.manifest.read(change[0])
671 change = self.changelog.read(node2)
674 change = self.changelog.read(node2)
672 mf2 = self.manifest.read(change[0])
675 mf2 = self.manifest.read(change[0])
673
676
674 for fn in mf2:
677 for fn in mf2:
675 if mf1.has_key(fn):
678 if mf1.has_key(fn):
676 if mf1[fn] != mf2[fn]:
679 if mf1[fn] != mf2[fn]:
677 changed.append(fn)
680 changed.append(fn)
678 del mf1[fn]
681 del mf1[fn]
679 else:
682 else:
680 added.append(fn)
683 added.append(fn)
681
684
682 deleted = mf1.keys()
685 deleted = mf1.keys()
683 deleted.sort()
686 deleted.sort()
684
687
685 return (changed, added, deleted)
688 return (changed, added, deleted)
686
689
687 def add(self, list):
690 def add(self, list):
688 for f in list:
691 for f in list:
689 p = self.wjoin(f)
692 p = self.wjoin(f)
690 if not os.path.isfile(p):
693 if not os.path.isfile(p):
691 self.ui.warn("%s does not exist!\n" % f)
694 self.ui.warn("%s does not exist!\n" % f)
692 elif self.dirstate.state(f) == 'n':
695 elif self.dirstate.state(f) == 'n':
693 self.ui.warn("%s already tracked!\n" % f)
696 self.ui.warn("%s already tracked!\n" % f)
694 else:
697 else:
695 self.dirstate.update([f], "a")
698 self.dirstate.update([f], "a")
696
699
697 def forget(self, list):
700 def forget(self, list):
698 for f in list:
701 for f in list:
699 if self.dirstate.state(f) not in 'ai':
702 if self.dirstate.state(f) not in 'ai':
700 self.ui.warn("%s not added!\n" % f)
703 self.ui.warn("%s not added!\n" % f)
701 else:
704 else:
702 self.dirstate.forget([f])
705 self.dirstate.forget([f])
703
706
704 def remove(self, list):
707 def remove(self, list):
705 for f in list:
708 for f in list:
706 p = self.wjoin(f)
709 p = self.wjoin(f)
707 if os.path.isfile(p):
710 if os.path.isfile(p):
708 self.ui.warn("%s still exists!\n" % f)
711 self.ui.warn("%s still exists!\n" % f)
709 elif self.dirstate.state(f) == 'a':
712 elif self.dirstate.state(f) == 'a':
710 self.ui.warn("%s never committed!\n" % f)
713 self.ui.warn("%s never committed!\n" % f)
711 self.dirstate.forget(f)
714 self.dirstate.forget(f)
712 elif f not in self.dirstate:
715 elif f not in self.dirstate:
713 self.ui.warn("%s not tracked!\n" % f)
716 self.ui.warn("%s not tracked!\n" % f)
714 else:
717 else:
715 self.dirstate.update([f], "r")
718 self.dirstate.update([f], "r")
716
719
717 def copy(self, source, dest):
720 def copy(self, source, dest):
718 p = self.wjoin(dest)
721 p = self.wjoin(dest)
719 if not os.path.isfile(dest):
722 if not os.path.isfile(dest):
720 self.ui.warn("%s does not exist!\n" % dest)
723 self.ui.warn("%s does not exist!\n" % dest)
721 else:
724 else:
722 if self.dirstate.state(dest) == '?':
725 if self.dirstate.state(dest) == '?':
723 self.dirstate.update([dest], "a")
726 self.dirstate.update([dest], "a")
724 self.dirstate.copy(source, dest)
727 self.dirstate.copy(source, dest)
725
728
726 def heads(self):
729 def heads(self):
727 return self.changelog.heads()
730 return self.changelog.heads()
728
731
729 def branches(self, nodes):
732 def branches(self, nodes):
730 if not nodes: nodes = [self.changelog.tip()]
733 if not nodes: nodes = [self.changelog.tip()]
731 b = []
734 b = []
732 for n in nodes:
735 for n in nodes:
733 t = n
736 t = n
734 while n:
737 while n:
735 p = self.changelog.parents(n)
738 p = self.changelog.parents(n)
736 if p[1] != nullid or p[0] == nullid:
739 if p[1] != nullid or p[0] == nullid:
737 b.append((t, n, p[0], p[1]))
740 b.append((t, n, p[0], p[1]))
738 break
741 break
739 n = p[0]
742 n = p[0]
740 return b
743 return b
741
744
742 def between(self, pairs):
745 def between(self, pairs):
743 r = []
746 r = []
744
747
745 for top, bottom in pairs:
748 for top, bottom in pairs:
746 n, l, i = top, [], 0
749 n, l, i = top, [], 0
747 f = 1
750 f = 1
748
751
749 while n != bottom:
752 while n != bottom:
750 p = self.changelog.parents(n)[0]
753 p = self.changelog.parents(n)[0]
751 if i == f:
754 if i == f:
752 l.append(n)
755 l.append(n)
753 f = f * 2
756 f = f * 2
754 n = p
757 n = p
755 i += 1
758 i += 1
756
759
757 r.append(l)
760 r.append(l)
758
761
759 return r
762 return r
760
763
761 def newer(self, nodes):
764 def newer(self, nodes):
762 m = {}
765 m = {}
763 nl = []
766 nl = []
764 pm = {}
767 pm = {}
765 cl = self.changelog
768 cl = self.changelog
766 t = l = cl.count()
769 t = l = cl.count()
767
770
768 # find the lowest numbered node
771 # find the lowest numbered node
769 for n in nodes:
772 for n in nodes:
770 l = min(l, cl.rev(n))
773 l = min(l, cl.rev(n))
771 m[n] = 1
774 m[n] = 1
772
775
773 for i in xrange(l, t):
776 for i in xrange(l, t):
774 n = cl.node(i)
777 n = cl.node(i)
775 if n in m: # explicitly listed
778 if n in m: # explicitly listed
776 pm[n] = 1
779 pm[n] = 1
777 nl.append(n)
780 nl.append(n)
778 continue
781 continue
779 for p in cl.parents(n):
782 for p in cl.parents(n):
780 if p in pm: # parent listed
783 if p in pm: # parent listed
781 pm[n] = 1
784 pm[n] = 1
782 nl.append(n)
785 nl.append(n)
783 break
786 break
784
787
785 return nl
788 return nl
786
789
787 def getchangegroup(self, remote):
790 def getchangegroup(self, remote):
788 m = self.changelog.nodemap
791 m = self.changelog.nodemap
789 search = []
792 search = []
790 fetch = []
793 fetch = []
791 seen = {}
794 seen = {}
792 seenbranch = {}
795 seenbranch = {}
793
796
794 # if we have an empty repo, fetch everything
797 # if we have an empty repo, fetch everything
795 if self.changelog.tip() == nullid:
798 if self.changelog.tip() == nullid:
796 self.ui.status("requesting all changes\n")
799 self.ui.status("requesting all changes\n")
797 return remote.changegroup([nullid])
800 return remote.changegroup([nullid])
798
801
799 # otherwise, assume we're closer to the tip than the root
802 # otherwise, assume we're closer to the tip than the root
800 self.ui.status("searching for changes\n")
803 self.ui.status("searching for changes\n")
801 heads = remote.heads()
804 heads = remote.heads()
802 unknown = []
805 unknown = []
803 for h in heads:
806 for h in heads:
804 if h not in m:
807 if h not in m:
805 unknown.append(h)
808 unknown.append(h)
806
809
807 if not unknown:
810 if not unknown:
808 self.ui.status("nothing to do!\n")
811 self.ui.status("nothing to do!\n")
809 return None
812 return None
810
813
811 rep = {}
814 rep = {}
812 reqcnt = 0
815 reqcnt = 0
813
816
814 unknown = remote.branches(unknown)
817 unknown = remote.branches(unknown)
815 while unknown:
818 while unknown:
816 r = []
819 r = []
817 while unknown:
820 while unknown:
818 n = unknown.pop(0)
821 n = unknown.pop(0)
819 if n[0] in seen:
822 if n[0] in seen:
820 continue
823 continue
821
824
822 self.ui.debug("examining %s:%s\n" % (short(n[0]), short(n[1])))
825 self.ui.debug("examining %s:%s\n" % (short(n[0]), short(n[1])))
823 if n[0] == nullid:
826 if n[0] == nullid:
824 break
827 break
825 if n in seenbranch:
828 if n in seenbranch:
826 self.ui.debug("branch already found\n")
829 self.ui.debug("branch already found\n")
827 continue
830 continue
828 if n[1] and n[1] in m: # do we know the base?
831 if n[1] and n[1] in m: # do we know the base?
829 self.ui.debug("found incomplete branch %s:%s\n"
832 self.ui.debug("found incomplete branch %s:%s\n"
830 % (short(n[0]), short(n[1])))
833 % (short(n[0]), short(n[1])))
831 search.append(n) # schedule branch range for scanning
834 search.append(n) # schedule branch range for scanning
832 seenbranch[n] = 1
835 seenbranch[n] = 1
833 else:
836 else:
834 if n[1] not in seen and n[1] not in fetch:
837 if n[1] not in seen and n[1] not in fetch:
835 if n[2] in m and n[3] in m:
838 if n[2] in m and n[3] in m:
836 self.ui.debug("found new changeset %s\n" %
839 self.ui.debug("found new changeset %s\n" %
837 short(n[1]))
840 short(n[1]))
838 fetch.append(n[1]) # earliest unknown
841 fetch.append(n[1]) # earliest unknown
839 continue
842 continue
840
843
841 for a in n[2:4]:
844 for a in n[2:4]:
842 if a not in rep:
845 if a not in rep:
843 r.append(a)
846 r.append(a)
844 rep[a] = 1
847 rep[a] = 1
845
848
846 seen[n[0]] = 1
849 seen[n[0]] = 1
847
850
848 if r:
851 if r:
849 reqcnt += 1
852 reqcnt += 1
850 self.ui.debug("request %d: %s\n" %
853 self.ui.debug("request %d: %s\n" %
851 (reqcnt, " ".join(map(short, r))))
854 (reqcnt, " ".join(map(short, r))))
852 for p in range(0, len(r), 10):
855 for p in range(0, len(r), 10):
853 for b in remote.branches(r[p:p+10]):
856 for b in remote.branches(r[p:p+10]):
854 self.ui.debug("received %s:%s\n" %
857 self.ui.debug("received %s:%s\n" %
855 (short(b[0]), short(b[1])))
858 (short(b[0]), short(b[1])))
856 if b[0] not in m and b[0] not in seen:
859 if b[0] not in m and b[0] not in seen:
857 unknown.append(b)
860 unknown.append(b)
858
861
859 while search:
862 while search:
860 n = search.pop(0)
863 n = search.pop(0)
861 reqcnt += 1
864 reqcnt += 1
862 l = remote.between([(n[0], n[1])])[0]
865 l = remote.between([(n[0], n[1])])[0]
863 l.append(n[1])
866 l.append(n[1])
864 p = n[0]
867 p = n[0]
865 f = 1
868 f = 1
866 for i in l:
869 for i in l:
867 self.ui.debug("narrowing %d:%d %s\n" % (f, len(l), short(i)))
870 self.ui.debug("narrowing %d:%d %s\n" % (f, len(l), short(i)))
868 if i in m:
871 if i in m:
869 if f <= 2:
872 if f <= 2:
870 self.ui.debug("found new branch changeset %s\n" %
873 self.ui.debug("found new branch changeset %s\n" %
871 short(p))
874 short(p))
872 fetch.append(p)
875 fetch.append(p)
873 else:
876 else:
874 self.ui.debug("narrowed branch search to %s:%s\n"
877 self.ui.debug("narrowed branch search to %s:%s\n"
875 % (short(p), short(i)))
878 % (short(p), short(i)))
876 search.append((p, i))
879 search.append((p, i))
877 break
880 break
878 p, f = i, f * 2
881 p, f = i, f * 2
879
882
880 for f in fetch:
883 for f in fetch:
881 if f in m:
884 if f in m:
882 raise "already have", short(f[:4])
885 raise "already have", short(f[:4])
883
886
884 self.ui.note("adding new changesets starting at " +
887 self.ui.note("adding new changesets starting at " +
885 " ".join([short(f) for f in fetch]) + "\n")
888 " ".join([short(f) for f in fetch]) + "\n")
886
889
887 self.ui.debug("%d total queries\n" % reqcnt)
890 self.ui.debug("%d total queries\n" % reqcnt)
888
891
889 return remote.changegroup(fetch)
892 return remote.changegroup(fetch)
890
893
891 def changegroup(self, basenodes):
894 def changegroup(self, basenodes):
892 nodes = self.newer(basenodes)
895 nodes = self.newer(basenodes)
893
896
894 # construct the link map
897 # construct the link map
895 linkmap = {}
898 linkmap = {}
896 for n in nodes:
899 for n in nodes:
897 linkmap[self.changelog.rev(n)] = n
900 linkmap[self.changelog.rev(n)] = n
898
901
899 # construct a list of all changed files
902 # construct a list of all changed files
900 changed = {}
903 changed = {}
901 for n in nodes:
904 for n in nodes:
902 c = self.changelog.read(n)
905 c = self.changelog.read(n)
903 for f in c[3]:
906 for f in c[3]:
904 changed[f] = 1
907 changed[f] = 1
905 changed = changed.keys()
908 changed = changed.keys()
906 changed.sort()
909 changed.sort()
907
910
908 # the changegroup is changesets + manifests + all file revs
911 # the changegroup is changesets + manifests + all file revs
909 revs = [ self.changelog.rev(n) for n in nodes ]
912 revs = [ self.changelog.rev(n) for n in nodes ]
910
913
911 for y in self.changelog.group(linkmap): yield y
914 for y in self.changelog.group(linkmap): yield y
912 for y in self.manifest.group(linkmap): yield y
915 for y in self.manifest.group(linkmap): yield y
913 for f in changed:
916 for f in changed:
914 yield struct.pack(">l", len(f) + 4) + f
917 yield struct.pack(">l", len(f) + 4) + f
915 g = self.file(f).group(linkmap)
918 g = self.file(f).group(linkmap)
916 for y in g:
919 for y in g:
917 yield y
920 yield y
918
921
919 def addchangegroup(self, generator):
922 def addchangegroup(self, generator):
920
923
921 class genread:
924 class genread:
922 def __init__(self, generator):
925 def __init__(self, generator):
923 self.g = generator
926 self.g = generator
924 self.buf = ""
927 self.buf = ""
925 def read(self, l):
928 def read(self, l):
926 while l > len(self.buf):
929 while l > len(self.buf):
927 try:
930 try:
928 self.buf += self.g.next()
931 self.buf += self.g.next()
929 except StopIteration:
932 except StopIteration:
930 break
933 break
931 d, self.buf = self.buf[:l], self.buf[l:]
934 d, self.buf = self.buf[:l], self.buf[l:]
932 return d
935 return d
933
936
934 def getchunk():
937 def getchunk():
935 d = source.read(4)
938 d = source.read(4)
936 if not d: return ""
939 if not d: return ""
937 l = struct.unpack(">l", d)[0]
940 l = struct.unpack(">l", d)[0]
938 if l <= 4: return ""
941 if l <= 4: return ""
939 return source.read(l - 4)
942 return source.read(l - 4)
940
943
941 def getgroup():
944 def getgroup():
942 while 1:
945 while 1:
943 c = getchunk()
946 c = getchunk()
944 if not c: break
947 if not c: break
945 yield c
948 yield c
946
949
947 def csmap(x):
950 def csmap(x):
948 self.ui.debug("add changeset %s\n" % short(x))
951 self.ui.debug("add changeset %s\n" % short(x))
949 return self.changelog.count()
952 return self.changelog.count()
950
953
951 def revmap(x):
954 def revmap(x):
952 return self.changelog.rev(x)
955 return self.changelog.rev(x)
953
956
954 if not generator: return
957 if not generator: return
955 changesets = files = revisions = 0
958 changesets = files = revisions = 0
956
959
957 source = genread(generator)
960 source = genread(generator)
958 lock = self.lock()
961 lock = self.lock()
959 tr = self.transaction()
962 tr = self.transaction()
960
963
961 # pull off the changeset group
964 # pull off the changeset group
962 self.ui.status("adding changesets\n")
965 self.ui.status("adding changesets\n")
963 co = self.changelog.tip()
966 co = self.changelog.tip()
964 cn = self.changelog.addgroup(getgroup(), csmap, tr, 1) # unique
967 cn = self.changelog.addgroup(getgroup(), csmap, tr, 1) # unique
965 changesets = self.changelog.rev(cn) - self.changelog.rev(co)
968 changesets = self.changelog.rev(cn) - self.changelog.rev(co)
966
969
967 # pull off the manifest group
970 # pull off the manifest group
968 self.ui.status("adding manifests\n")
971 self.ui.status("adding manifests\n")
969 mm = self.manifest.tip()
972 mm = self.manifest.tip()
970 mo = self.manifest.addgroup(getgroup(), revmap, tr)
973 mo = self.manifest.addgroup(getgroup(), revmap, tr)
971
974
972 # process the files
975 # process the files
973 self.ui.status("adding file revisions\n")
976 self.ui.status("adding file revisions\n")
974 while 1:
977 while 1:
975 f = getchunk()
978 f = getchunk()
976 if not f: break
979 if not f: break
977 self.ui.debug("adding %s revisions\n" % f)
980 self.ui.debug("adding %s revisions\n" % f)
978 fl = self.file(f)
981 fl = self.file(f)
979 o = fl.tip()
982 o = fl.tip()
980 n = fl.addgroup(getgroup(), revmap, tr)
983 n = fl.addgroup(getgroup(), revmap, tr)
981 revisions += fl.rev(n) - fl.rev(o)
984 revisions += fl.rev(n) - fl.rev(o)
982 files += 1
985 files += 1
983
986
984 self.ui.status(("modified %d files, added %d changesets" +
987 self.ui.status(("modified %d files, added %d changesets" +
985 " and %d new revisions\n")
988 " and %d new revisions\n")
986 % (files, changesets, revisions))
989 % (files, changesets, revisions))
987
990
988 tr.close()
991 tr.close()
989 return
992 return
990
993
991 def update(self, node, allow=False, force=False):
994 def update(self, node, allow=False, force=False):
992 pl = self.dirstate.parents()
995 pl = self.dirstate.parents()
993 if not force and pl[1] != nullid:
996 if not force and pl[1] != nullid:
994 self.ui.warn("aborting: outstanding uncommitted merges\n")
997 self.ui.warn("aborting: outstanding uncommitted merges\n")
995 return
998 return
996
999
997 p1, p2 = pl[0], node
1000 p1, p2 = pl[0], node
998 pa = self.changelog.ancestor(p1, p2)
1001 pa = self.changelog.ancestor(p1, p2)
999 m1n = self.changelog.read(p1)[0]
1002 m1n = self.changelog.read(p1)[0]
1000 m2n = self.changelog.read(p2)[0]
1003 m2n = self.changelog.read(p2)[0]
1001 man = self.manifest.ancestor(m1n, m2n)
1004 man = self.manifest.ancestor(m1n, m2n)
1002 m1 = self.manifest.read(m1n)
1005 m1 = self.manifest.read(m1n)
1003 mf1 = self.manifest.readflags(m1n)
1006 mf1 = self.manifest.readflags(m1n)
1004 m2 = self.manifest.read(m2n)
1007 m2 = self.manifest.read(m2n)
1005 mf2 = self.manifest.readflags(m2n)
1008 mf2 = self.manifest.readflags(m2n)
1006 ma = self.manifest.read(man)
1009 ma = self.manifest.read(man)
1007 mfa = self.manifest.readflags(m2n)
1010 mfa = self.manifest.readflags(m2n)
1008
1011
1009 (c, a, d, u) = self.diffdir(self.root)
1012 (c, a, d, u) = self.diffdir(self.root)
1010
1013
1011 # resolve the manifest to determine which files
1014 # resolve the manifest to determine which files
1012 # we care about merging
1015 # we care about merging
1013 self.ui.note("resolving manifests\n")
1016 self.ui.note("resolving manifests\n")
1014 self.ui.debug(" ancestor %s local %s remote %s\n" %
1017 self.ui.debug(" ancestor %s local %s remote %s\n" %
1015 (short(man), short(m1n), short(m2n)))
1018 (short(man), short(m1n), short(m2n)))
1016
1019
1017 merge = {}
1020 merge = {}
1018 get = {}
1021 get = {}
1019 remove = []
1022 remove = []
1020 mark = {}
1023 mark = {}
1021
1024
1022 # construct a working dir manifest
1025 # construct a working dir manifest
1023 mw = m1.copy()
1026 mw = m1.copy()
1024 mfw = mf1.copy()
1027 mfw = mf1.copy()
1025 for f in a + c + u:
1028 for f in a + c + u:
1026 mw[f] = ""
1029 mw[f] = ""
1027 mfw[f] = is_exec(self.wjoin(f))
1030 mfw[f] = is_exec(self.wjoin(f))
1028 for f in d:
1031 for f in d:
1029 if f in mw: del mw[f]
1032 if f in mw: del mw[f]
1030
1033
1031 for f, n in mw.iteritems():
1034 for f, n in mw.iteritems():
1032 if f in m2:
1035 if f in m2:
1033 s = 0
1036 s = 0
1034
1037
1035 # are files different?
1038 # are files different?
1036 if n != m2[f]:
1039 if n != m2[f]:
1037 a = ma.get(f, nullid)
1040 a = ma.get(f, nullid)
1038 # are both different from the ancestor?
1041 # are both different from the ancestor?
1039 if n != a and m2[f] != a:
1042 if n != a and m2[f] != a:
1040 self.ui.debug(" %s versions differ, resolve\n" % f)
1043 self.ui.debug(" %s versions differ, resolve\n" % f)
1041 merge[f] = (m1.get(f, nullid), m2[f])
1044 merge[f] = (m1.get(f, nullid), m2[f])
1042 # merge executable bits
1045 # merge executable bits
1043 # "if we changed or they changed, change in merge"
1046 # "if we changed or they changed, change in merge"
1044 a, b, c = mfa.get(f, 0), mfw[f], mf2[f]
1047 a, b, c = mfa.get(f, 0), mfw[f], mf2[f]
1045 mode = ((a^b) | (a^c)) ^ a
1048 mode = ((a^b) | (a^c)) ^ a
1046 merge[f] = (m1.get(f, nullid), m2[f], mode)
1049 merge[f] = (m1.get(f, nullid), m2[f], mode)
1047 s = 1
1050 s = 1
1048 # are we clobbering?
1051 # are we clobbering?
1049 # is remote's version newer?
1052 # is remote's version newer?
1050 # or are we going back in time?
1053 # or are we going back in time?
1051 elif force or m2[f] != a or (p2 == pa and mw[f] == m1[f]):
1054 elif force or m2[f] != a or (p2 == pa and mw[f] == m1[f]):
1052 self.ui.debug(" remote %s is newer, get\n" % f)
1055 self.ui.debug(" remote %s is newer, get\n" % f)
1053 get[f] = m2[f]
1056 get[f] = m2[f]
1054 s = 1
1057 s = 1
1055 else:
1058 else:
1056 mark[f] = 1
1059 mark[f] = 1
1057
1060
1058 if not s and mfw[f] != mf2[f]:
1061 if not s and mfw[f] != mf2[f]:
1059 if force:
1062 if force:
1060 self.ui.debug(" updating permissions for %s\n" % f)
1063 self.ui.debug(" updating permissions for %s\n" % f)
1061 set_exec(self.wjoin(f), mf2[f])
1064 set_exec(self.wjoin(f), mf2[f])
1062 else:
1065 else:
1063 a, b, c = mfa.get(f, 0), mfw[f], mf2[f]
1066 a, b, c = mfa.get(f, 0), mfw[f], mf2[f]
1064 mode = ((a^b) | (a^c)) ^ a
1067 mode = ((a^b) | (a^c)) ^ a
1065 if mode != b:
1068 if mode != b:
1066 self.ui.debug(" updating permissions for %s\n" % f)
1069 self.ui.debug(" updating permissions for %s\n" % f)
1067 set_exec(self.wjoin(f), mode)
1070 set_exec(self.wjoin(f), mode)
1068 mark[f] = 1
1071 mark[f] = 1
1069 del m2[f]
1072 del m2[f]
1070 elif f in ma:
1073 elif f in ma:
1071 if not force and n != ma[f]:
1074 if not force and n != ma[f]:
1072 r = self.ui.prompt(
1075 r = self.ui.prompt(
1073 (" local changed %s which remote deleted\n" % f) +
1076 (" local changed %s which remote deleted\n" % f) +
1074 "(k)eep or (d)elete?", "[kd]", "k")
1077 "(k)eep or (d)elete?", "[kd]", "k")
1075 if r == "d":
1078 if r == "d":
1076 remove.append(f)
1079 remove.append(f)
1077 else:
1080 else:
1078 self.ui.debug("other deleted %s\n" % f)
1081 self.ui.debug("other deleted %s\n" % f)
1079 remove.append(f) # other deleted it
1082 remove.append(f) # other deleted it
1080 else:
1083 else:
1081 if n == m1.get(f, nullid): # same as parent
1084 if n == m1.get(f, nullid): # same as parent
1082 if p2 == pa: # going backwards?
1085 if p2 == pa: # going backwards?
1083 self.ui.debug("remote deleted %s\n" % f)
1086 self.ui.debug("remote deleted %s\n" % f)
1084 remove.append(f)
1087 remove.append(f)
1085 else:
1088 else:
1086 self.ui.debug("local created %s, keeping\n" % f)
1089 self.ui.debug("local created %s, keeping\n" % f)
1087 else:
1090 else:
1088 self.ui.debug("working dir created %s, keeping\n" % f)
1091 self.ui.debug("working dir created %s, keeping\n" % f)
1089
1092
1090 for f, n in m2.iteritems():
1093 for f, n in m2.iteritems():
1091 if f[0] == "/": continue
1094 if f[0] == "/": continue
1092 if not force and f in ma and n != ma[f]:
1095 if not force and f in ma and n != ma[f]:
1093 r = self.ui.prompt(
1096 r = self.ui.prompt(
1094 ("remote changed %s which local deleted\n" % f) +
1097 ("remote changed %s which local deleted\n" % f) +
1095 "(k)eep or (d)elete?", "[kd]", "k")
1098 "(k)eep or (d)elete?", "[kd]", "k")
1096 if r == "d": remove.append(f)
1099 if r == "d": remove.append(f)
1097 else:
1100 else:
1098 self.ui.debug("remote created %s\n" % f)
1101 self.ui.debug("remote created %s\n" % f)
1099 get[f] = n
1102 get[f] = n
1100
1103
1101 del mw, m1, m2, ma
1104 del mw, m1, m2, ma
1102
1105
1103 if force:
1106 if force:
1104 for f in merge:
1107 for f in merge:
1105 get[f] = merge[f][1]
1108 get[f] = merge[f][1]
1106 merge = {}
1109 merge = {}
1107
1110
1108 if pa == p1 or pa == p2:
1111 if pa == p1 or pa == p2:
1109 # we don't need to do any magic, just jump to the new rev
1112 # we don't need to do any magic, just jump to the new rev
1110 mode = 'n'
1113 mode = 'n'
1111 p1, p2 = p2, nullid
1114 p1, p2 = p2, nullid
1112 else:
1115 else:
1113 if not allow:
1116 if not allow:
1114 self.ui.status("this update spans a branch" +
1117 self.ui.status("this update spans a branch" +
1115 " affecting the following files:\n")
1118 " affecting the following files:\n")
1116 fl = merge.keys() + get.keys()
1119 fl = merge.keys() + get.keys()
1117 fl.sort()
1120 fl.sort()
1118 for f in fl:
1121 for f in fl:
1119 cf = ""
1122 cf = ""
1120 if f in merge: cf = " (resolve)"
1123 if f in merge: cf = " (resolve)"
1121 self.ui.status(" %s%s\n" % (f, cf))
1124 self.ui.status(" %s%s\n" % (f, cf))
1122 self.ui.warn("aborting update spanning branches!\n")
1125 self.ui.warn("aborting update spanning branches!\n")
1123 self.ui.status("(use update -m to perform a branch merge)\n")
1126 self.ui.status("(use update -m to perform a branch merge)\n")
1124 return 1
1127 return 1
1125 # we have to remember what files we needed to get/change
1128 # we have to remember what files we needed to get/change
1126 # because any file that's different from either one of its
1129 # because any file that's different from either one of its
1127 # parents must be in the changeset
1130 # parents must be in the changeset
1128 mode = 'm'
1131 mode = 'm'
1129 self.dirstate.update(mark.keys(), "m")
1132 self.dirstate.update(mark.keys(), "m")
1130
1133
1131 self.dirstate.setparents(p1, p2)
1134 self.dirstate.setparents(p1, p2)
1132
1135
1133 # get the files we don't need to change
1136 # get the files we don't need to change
1134 files = get.keys()
1137 files = get.keys()
1135 files.sort()
1138 files.sort()
1136 for f in files:
1139 for f in files:
1137 if f[0] == "/": continue
1140 if f[0] == "/": continue
1138 self.ui.note("getting %s\n" % f)
1141 self.ui.note("getting %s\n" % f)
1139 t = self.file(f).read(get[f])
1142 t = self.file(f).read(get[f])
1140 try:
1143 try:
1141 self.wfile(f, "w").write(t)
1144 self.wfile(f, "w").write(t)
1142 except IOError:
1145 except IOError:
1143 os.makedirs(os.path.dirname(self.wjoin(f)))
1146 os.makedirs(os.path.dirname(self.wjoin(f)))
1144 self.wfile(f, "w").write(t)
1147 self.wfile(f, "w").write(t)
1145 set_exec(self.wjoin(f), mf2[f])
1148 set_exec(self.wjoin(f), mf2[f])
1146 self.dirstate.update([f], mode)
1149 self.dirstate.update([f], mode)
1147
1150
1148 # merge the tricky bits
1151 # merge the tricky bits
1149 files = merge.keys()
1152 files = merge.keys()
1150 files.sort()
1153 files.sort()
1151 for f in files:
1154 for f in files:
1152 self.ui.status("merging %s\n" % f)
1155 self.ui.status("merging %s\n" % f)
1153 m, o, flag = merge[f]
1156 m, o, flag = merge[f]
1154 self.merge3(f, m, o)
1157 self.merge3(f, m, o)
1155 set_exec(self.wjoin(f), flag)
1158 set_exec(self.wjoin(f), flag)
1156 self.dirstate.update([f], 'm')
1159 self.dirstate.update([f], 'm')
1157
1160
1158 for f in remove:
1161 for f in remove:
1159 self.ui.note("removing %s\n" % f)
1162 self.ui.note("removing %s\n" % f)
1160 os.unlink(f)
1163 os.unlink(f)
1161 if mode == 'n':
1164 if mode == 'n':
1162 self.dirstate.forget(remove)
1165 self.dirstate.forget(remove)
1163 else:
1166 else:
1164 self.dirstate.update(remove, 'r')
1167 self.dirstate.update(remove, 'r')
1165
1168
1166 def merge3(self, fn, my, other):
1169 def merge3(self, fn, my, other):
1167 """perform a 3-way merge in the working directory"""
1170 """perform a 3-way merge in the working directory"""
1168
1171
1169 def temp(prefix, node):
1172 def temp(prefix, node):
1170 pre = "%s~%s." % (os.path.basename(fn), prefix)
1173 pre = "%s~%s." % (os.path.basename(fn), prefix)
1171 (fd, name) = tempfile.mkstemp("", pre)
1174 (fd, name) = tempfile.mkstemp("", pre)
1172 f = os.fdopen(fd, "w")
1175 f = os.fdopen(fd, "w")
1173 f.write(fl.revision(node))
1176 f.write(fl.revision(node))
1174 f.close()
1177 f.close()
1175 return name
1178 return name
1176
1179
1177 fl = self.file(fn)
1180 fl = self.file(fn)
1178 base = fl.ancestor(my, other)
1181 base = fl.ancestor(my, other)
1179 a = self.wjoin(fn)
1182 a = self.wjoin(fn)
1180 b = temp("base", base)
1183 b = temp("base", base)
1181 c = temp("other", other)
1184 c = temp("other", other)
1182
1185
1183 self.ui.note("resolving %s\n" % fn)
1186 self.ui.note("resolving %s\n" % fn)
1184 self.ui.debug("file %s: other %s ancestor %s\n" %
1187 self.ui.debug("file %s: other %s ancestor %s\n" %
1185 (fn, short(other), short(base)))
1188 (fn, short(other), short(base)))
1186
1189
1187 cmd = os.environ.get("HGMERGE", "hgmerge")
1190 cmd = os.environ.get("HGMERGE", "hgmerge")
1188 r = os.system("%s %s %s %s" % (cmd, a, b, c))
1191 r = os.system("%s %s %s %s" % (cmd, a, b, c))
1189 if r:
1192 if r:
1190 self.ui.warn("merging %s failed!\n" % fn)
1193 self.ui.warn("merging %s failed!\n" % fn)
1191
1194
1192 os.unlink(b)
1195 os.unlink(b)
1193 os.unlink(c)
1196 os.unlink(c)
1194
1197
1195 def verify(self):
1198 def verify(self):
1196 filelinkrevs = {}
1199 filelinkrevs = {}
1197 filenodes = {}
1200 filenodes = {}
1198 changesets = revisions = files = 0
1201 changesets = revisions = files = 0
1199 errors = 0
1202 errors = 0
1200
1203
1201 seen = {}
1204 seen = {}
1202 self.ui.status("checking changesets\n")
1205 self.ui.status("checking changesets\n")
1203 for i in range(self.changelog.count()):
1206 for i in range(self.changelog.count()):
1204 changesets += 1
1207 changesets += 1
1205 n = self.changelog.node(i)
1208 n = self.changelog.node(i)
1206 if n in seen:
1209 if n in seen:
1207 self.ui.warn("duplicate changeset at revision %d\n" % i)
1210 self.ui.warn("duplicate changeset at revision %d\n" % i)
1208 errors += 1
1211 errors += 1
1209 seen[n] = 1
1212 seen[n] = 1
1210
1213
1211 for p in self.changelog.parents(n):
1214 for p in self.changelog.parents(n):
1212 if p not in self.changelog.nodemap:
1215 if p not in self.changelog.nodemap:
1213 self.ui.warn("changeset %s has unknown parent %s\n" %
1216 self.ui.warn("changeset %s has unknown parent %s\n" %
1214 (short(n), short(p)))
1217 (short(n), short(p)))
1215 errors += 1
1218 errors += 1
1216 try:
1219 try:
1217 changes = self.changelog.read(n)
1220 changes = self.changelog.read(n)
1218 except Exception, inst:
1221 except Exception, inst:
1219 self.ui.warn("unpacking changeset %s: %s\n" % (short(n), inst))
1222 self.ui.warn("unpacking changeset %s: %s\n" % (short(n), inst))
1220 errors += 1
1223 errors += 1
1221
1224
1222 for f in changes[3]:
1225 for f in changes[3]:
1223 filelinkrevs.setdefault(f, []).append(i)
1226 filelinkrevs.setdefault(f, []).append(i)
1224
1227
1225 seen = {}
1228 seen = {}
1226 self.ui.status("checking manifests\n")
1229 self.ui.status("checking manifests\n")
1227 for i in range(self.manifest.count()):
1230 for i in range(self.manifest.count()):
1228 n = self.manifest.node(i)
1231 n = self.manifest.node(i)
1229 if n in seen:
1232 if n in seen:
1230 self.ui.warn("duplicate manifest at revision %d\n" % i)
1233 self.ui.warn("duplicate manifest at revision %d\n" % i)
1231 errors += 1
1234 errors += 1
1232 seen[n] = 1
1235 seen[n] = 1
1233
1236
1234 for p in self.manifest.parents(n):
1237 for p in self.manifest.parents(n):
1235 if p not in self.manifest.nodemap:
1238 if p not in self.manifest.nodemap:
1236 self.ui.warn("manifest %s has unknown parent %s\n" %
1239 self.ui.warn("manifest %s has unknown parent %s\n" %
1237 (short(n), short(p)))
1240 (short(n), short(p)))
1238 errors += 1
1241 errors += 1
1239
1242
1240 try:
1243 try:
1241 delta = mdiff.patchtext(self.manifest.delta(n))
1244 delta = mdiff.patchtext(self.manifest.delta(n))
1242 except KeyboardInterrupt:
1245 except KeyboardInterrupt:
1243 print "aborted"
1246 print "aborted"
1244 sys.exit(0)
1247 sys.exit(0)
1245 except Exception, inst:
1248 except Exception, inst:
1246 self.ui.warn("unpacking manifest %s: %s\n"
1249 self.ui.warn("unpacking manifest %s: %s\n"
1247 % (short(n), inst))
1250 % (short(n), inst))
1248 errors += 1
1251 errors += 1
1249
1252
1250 ff = [ l.split('\0') for l in delta.splitlines() ]
1253 ff = [ l.split('\0') for l in delta.splitlines() ]
1251 for f, fn in ff:
1254 for f, fn in ff:
1252 filenodes.setdefault(f, {})[bin(fn[:40])] = 1
1255 filenodes.setdefault(f, {})[bin(fn[:40])] = 1
1253
1256
1254 self.ui.status("crosschecking files in changesets and manifests\n")
1257 self.ui.status("crosschecking files in changesets and manifests\n")
1255 for f in filenodes:
1258 for f in filenodes:
1256 if f not in filelinkrevs:
1259 if f not in filelinkrevs:
1257 self.ui.warn("file %s in manifest but not in changesets\n" % f)
1260 self.ui.warn("file %s in manifest but not in changesets\n" % f)
1258 errors += 1
1261 errors += 1
1259
1262
1260 for f in filelinkrevs:
1263 for f in filelinkrevs:
1261 if f not in filenodes:
1264 if f not in filenodes:
1262 self.ui.warn("file %s in changeset but not in manifest\n" % f)
1265 self.ui.warn("file %s in changeset but not in manifest\n" % f)
1263 errors += 1
1266 errors += 1
1264
1267
1265 self.ui.status("checking files\n")
1268 self.ui.status("checking files\n")
1266 ff = filenodes.keys()
1269 ff = filenodes.keys()
1267 ff.sort()
1270 ff.sort()
1268 for f in ff:
1271 for f in ff:
1269 if f == "/dev/null": continue
1272 if f == "/dev/null": continue
1270 files += 1
1273 files += 1
1271 fl = self.file(f)
1274 fl = self.file(f)
1272 nodes = { nullid: 1 }
1275 nodes = { nullid: 1 }
1273 seen = {}
1276 seen = {}
1274 for i in range(fl.count()):
1277 for i in range(fl.count()):
1275 revisions += 1
1278 revisions += 1
1276 n = fl.node(i)
1279 n = fl.node(i)
1277
1280
1278 if n in seen:
1281 if n in seen:
1279 self.ui.warn("%s: duplicate revision %d\n" % (f, i))
1282 self.ui.warn("%s: duplicate revision %d\n" % (f, i))
1280 errors += 1
1283 errors += 1
1281
1284
1282 if n not in filenodes[f]:
1285 if n not in filenodes[f]:
1283 self.ui.warn("%s: %d:%s not in manifests\n"
1286 self.ui.warn("%s: %d:%s not in manifests\n"
1284 % (f, i, short(n)))
1287 % (f, i, short(n)))
1285 print len(filenodes[f].keys()), fl.count(), f
1288 print len(filenodes[f].keys()), fl.count(), f
1286 errors += 1
1289 errors += 1
1287 else:
1290 else:
1288 del filenodes[f][n]
1291 del filenodes[f][n]
1289
1292
1290 flr = fl.linkrev(n)
1293 flr = fl.linkrev(n)
1291 if flr not in filelinkrevs[f]:
1294 if flr not in filelinkrevs[f]:
1292 self.ui.warn("%s:%s points to unexpected changeset %d\n"
1295 self.ui.warn("%s:%s points to unexpected changeset %d\n"
1293 % (f, short(n), fl.linkrev(n)))
1296 % (f, short(n), fl.linkrev(n)))
1294 errors += 1
1297 errors += 1
1295 else:
1298 else:
1296 filelinkrevs[f].remove(flr)
1299 filelinkrevs[f].remove(flr)
1297
1300
1298 # verify contents
1301 # verify contents
1299 try:
1302 try:
1300 t = fl.read(n)
1303 t = fl.read(n)
1301 except Exception, inst:
1304 except Exception, inst:
1302 self.ui.warn("unpacking file %s %s: %s\n"
1305 self.ui.warn("unpacking file %s %s: %s\n"
1303 % (f, short(n), inst))
1306 % (f, short(n), inst))
1304 errors += 1
1307 errors += 1
1305
1308
1306 # verify parents
1309 # verify parents
1307 (p1, p2) = fl.parents(n)
1310 (p1, p2) = fl.parents(n)
1308 if p1 not in nodes:
1311 if p1 not in nodes:
1309 self.ui.warn("file %s:%s unknown parent 1 %s" %
1312 self.ui.warn("file %s:%s unknown parent 1 %s" %
1310 (f, short(n), short(p1)))
1313 (f, short(n), short(p1)))
1311 errors += 1
1314 errors += 1
1312 if p2 not in nodes:
1315 if p2 not in nodes:
1313 self.ui.warn("file %s:%s unknown parent 2 %s" %
1316 self.ui.warn("file %s:%s unknown parent 2 %s" %
1314 (f, short(n), short(p1)))
1317 (f, short(n), short(p1)))
1315 errors += 1
1318 errors += 1
1316 nodes[n] = 1
1319 nodes[n] = 1
1317
1320
1318 # cross-check
1321 # cross-check
1319 for node in filenodes[f]:
1322 for node in filenodes[f]:
1320 self.ui.warn("node %s in manifests not in %s\n"
1323 self.ui.warn("node %s in manifests not in %s\n"
1321 % (hex(n), f))
1324 % (hex(n), f))
1322 errors += 1
1325 errors += 1
1323
1326
1324 self.ui.status("%d files, %d changesets, %d total revisions\n" %
1327 self.ui.status("%d files, %d changesets, %d total revisions\n" %
1325 (files, changesets, revisions))
1328 (files, changesets, revisions))
1326
1329
1327 if errors:
1330 if errors:
1328 self.ui.warn("%d integrity errors encountered!\n" % errors)
1331 self.ui.warn("%d integrity errors encountered!\n" % errors)
1329 return 1
1332 return 1
1330
1333
1331 class remoterepository:
1334 class remoterepository:
1332 def __init__(self, ui, path):
1335 def __init__(self, ui, path):
1333 self.url = path
1336 self.url = path
1334 self.ui = ui
1337 self.ui = ui
1335 no_list = [ "localhost", "127.0.0.1" ]
1338 no_list = [ "localhost", "127.0.0.1" ]
1336 host = ui.config("http_proxy", "host")
1339 host = ui.config("http_proxy", "host")
1337 user = ui.config("http_proxy", "user")
1340 user = ui.config("http_proxy", "user")
1338 passwd = ui.config("http_proxy", "passwd")
1341 passwd = ui.config("http_proxy", "passwd")
1339 no = ui.config("http_proxy", "no")
1342 no = ui.config("http_proxy", "no")
1340 if no:
1343 if no:
1341 no_list = no_list + no.split(",")
1344 no_list = no_list + no.split(",")
1342
1345
1343 no_proxy = 0
1346 no_proxy = 0
1344 for h in no_list:
1347 for h in no_list:
1345 if (path.startswith("http://" + h + "/") or
1348 if (path.startswith("http://" + h + "/") or
1346 path.startswith("http://" + h + ":") or
1349 path.startswith("http://" + h + ":") or
1347 path == "http://" + h):
1350 path == "http://" + h):
1348 no_proxy = 1
1351 no_proxy = 1
1349
1352
1350 # Note: urllib2 takes proxy values from the environment and those will
1353 # Note: urllib2 takes proxy values from the environment and those will
1351 # take precedence
1354 # take precedence
1352
1355
1353 proxy_handler = urllib2.BaseHandler()
1356 proxy_handler = urllib2.BaseHandler()
1354 if host and not no_proxy:
1357 if host and not no_proxy:
1355 proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host})
1358 proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host})
1356
1359
1357 authinfo = None
1360 authinfo = None
1358 if user and passwd:
1361 if user and passwd:
1359 passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
1362 passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
1360 passmgr.add_password(None, host, user, passwd)
1363 passmgr.add_password(None, host, user, passwd)
1361 authinfo = urllib2.ProxyBasicAuthHandler(passmgr)
1364 authinfo = urllib2.ProxyBasicAuthHandler(passmgr)
1362
1365
1363 opener = urllib2.build_opener(proxy_handler, authinfo)
1366 opener = urllib2.build_opener(proxy_handler, authinfo)
1364 urllib2.install_opener(opener)
1367 urllib2.install_opener(opener)
1365
1368
1366 def do_cmd(self, cmd, **args):
1369 def do_cmd(self, cmd, **args):
1367 self.ui.debug("sending %s command\n" % cmd)
1370 self.ui.debug("sending %s command\n" % cmd)
1368 q = {"cmd": cmd}
1371 q = {"cmd": cmd}
1369 q.update(args)
1372 q.update(args)
1370 qs = urllib.urlencode(q)
1373 qs = urllib.urlencode(q)
1371 cu = "%s?%s" % (self.url, qs)
1374 cu = "%s?%s" % (self.url, qs)
1372 return urllib2.urlopen(cu)
1375 return urllib2.urlopen(cu)
1373
1376
1374 def heads(self):
1377 def heads(self):
1375 d = self.do_cmd("heads").read()
1378 d = self.do_cmd("heads").read()
1376 try:
1379 try:
1377 return map(bin, d[:-1].split(" "))
1380 return map(bin, d[:-1].split(" "))
1378 except:
1381 except:
1379 self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
1382 self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
1380 raise
1383 raise
1381
1384
1382 def branches(self, nodes):
1385 def branches(self, nodes):
1383 n = " ".join(map(hex, nodes))
1386 n = " ".join(map(hex, nodes))
1384 d = self.do_cmd("branches", nodes=n).read()
1387 d = self.do_cmd("branches", nodes=n).read()
1385 try:
1388 try:
1386 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
1389 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
1387 return br
1390 return br
1388 except:
1391 except:
1389 self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
1392 self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
1390 raise
1393 raise
1391
1394
1392 def between(self, pairs):
1395 def between(self, pairs):
1393 n = "\n".join(["-".join(map(hex, p)) for p in pairs])
1396 n = "\n".join(["-".join(map(hex, p)) for p in pairs])
1394 d = self.do_cmd("between", pairs=n).read()
1397 d = self.do_cmd("between", pairs=n).read()
1395 try:
1398 try:
1396 p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
1399 p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
1397 return p
1400 return p
1398 except:
1401 except:
1399 self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
1402 self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
1400 raise
1403 raise
1401
1404
1402 def changegroup(self, nodes):
1405 def changegroup(self, nodes):
1403 n = " ".join(map(hex, nodes))
1406 n = " ".join(map(hex, nodes))
1404 zd = zlib.decompressobj()
1407 zd = zlib.decompressobj()
1405 f = self.do_cmd("changegroup", roots=n)
1408 f = self.do_cmd("changegroup", roots=n)
1406 bytes = 0
1409 bytes = 0
1407 while 1:
1410 while 1:
1408 d = f.read(4096)
1411 d = f.read(4096)
1409 bytes += len(d)
1412 bytes += len(d)
1410 if not d:
1413 if not d:
1411 yield zd.flush()
1414 yield zd.flush()
1412 break
1415 break
1413 yield zd.decompress(d)
1416 yield zd.decompress(d)
1414 self.ui.note("%d bytes of data transfered\n" % bytes)
1417 self.ui.note("%d bytes of data transfered\n" % bytes)
1415
1418
1416 def repository(ui, path=None, create=0):
1419 def repository(ui, path=None, create=0):
1417 if path and path[:7] == "http://":
1420 if path and path[:7] == "http://":
1418 return remoterepository(ui, path)
1421 return remoterepository(ui, path)
1419 if path and path[:5] == "hg://":
1422 if path and path[:5] == "hg://":
1420 return remoterepository(ui, path.replace("hg://", "http://"))
1423 return remoterepository(ui, path.replace("hg://", "http://"))
1421 if path and path[:11] == "old-http://":
1424 if path and path[:11] == "old-http://":
1422 return localrepository(ui, path.replace("old-http://", "http://"))
1425 return localrepository(ui, path.replace("old-http://", "http://"))
1423 else:
1426 else:
1424 return localrepository(ui, path, create)
1427 return localrepository(ui, path, create)
1425
1428
General Comments 0
You need to be logged in to leave comments. Login now