##// END OF EJS Templates
transaction: refactor transaction.abort and rollback to use the same code...
Henrik Stuart -
r8294:48a382c2 default
parent child Browse files
Show More
@@ -1,2172 +1,2172
1 # localrepo.py - read/write repository class for mercurial
1 # localrepo.py - read/write repository class for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference.
6 # GNU General Public License version 2, incorporated herein by reference.
7
7
8 from node import bin, hex, nullid, nullrev, short
8 from node import bin, hex, nullid, nullrev, short
9 from i18n import _
9 from i18n import _
10 import repo, changegroup
10 import repo, changegroup
11 import changelog, dirstate, filelog, manifest, context, weakref
11 import changelog, dirstate, filelog, manifest, context, weakref
12 import lock, transaction, stat, errno, ui, store, encoding
12 import lock, transaction, stat, errno, ui, store, encoding
13 import os, time, util, extensions, hook, inspect, error
13 import os, time, util, extensions, hook, inspect, error
14 import match as match_
14 import match as match_
15 import merge as merge_
15 import merge as merge_
16
16
17 from lock import release
17 from lock import release
18 propertycache = util.propertycache
18 propertycache = util.propertycache
19
19
20 class localrepository(repo.repository):
20 class localrepository(repo.repository):
21 capabilities = set(('lookup', 'changegroupsubset'))
21 capabilities = set(('lookup', 'changegroupsubset'))
22 supported = set('revlogv1 store fncache'.split())
22 supported = set('revlogv1 store fncache'.split())
23
23
24 def __init__(self, baseui, path=None, create=0):
24 def __init__(self, baseui, path=None, create=0):
25 repo.repository.__init__(self)
25 repo.repository.__init__(self)
26 self.root = os.path.realpath(path)
26 self.root = os.path.realpath(path)
27 self.path = os.path.join(self.root, ".hg")
27 self.path = os.path.join(self.root, ".hg")
28 self.origroot = path
28 self.origroot = path
29 self.opener = util.opener(self.path)
29 self.opener = util.opener(self.path)
30 self.wopener = util.opener(self.root)
30 self.wopener = util.opener(self.root)
31
31
32 if not os.path.isdir(self.path):
32 if not os.path.isdir(self.path):
33 if create:
33 if create:
34 if not os.path.exists(path):
34 if not os.path.exists(path):
35 os.mkdir(path)
35 os.mkdir(path)
36 os.mkdir(self.path)
36 os.mkdir(self.path)
37 requirements = ["revlogv1"]
37 requirements = ["revlogv1"]
38 if baseui.configbool('format', 'usestore', True):
38 if baseui.configbool('format', 'usestore', True):
39 os.mkdir(os.path.join(self.path, "store"))
39 os.mkdir(os.path.join(self.path, "store"))
40 requirements.append("store")
40 requirements.append("store")
41 if baseui.configbool('format', 'usefncache', True):
41 if baseui.configbool('format', 'usefncache', True):
42 requirements.append("fncache")
42 requirements.append("fncache")
43 # create an invalid changelog
43 # create an invalid changelog
44 self.opener("00changelog.i", "a").write(
44 self.opener("00changelog.i", "a").write(
45 '\0\0\0\2' # represents revlogv2
45 '\0\0\0\2' # represents revlogv2
46 ' dummy changelog to prevent using the old repo layout'
46 ' dummy changelog to prevent using the old repo layout'
47 )
47 )
48 reqfile = self.opener("requires", "w")
48 reqfile = self.opener("requires", "w")
49 for r in requirements:
49 for r in requirements:
50 reqfile.write("%s\n" % r)
50 reqfile.write("%s\n" % r)
51 reqfile.close()
51 reqfile.close()
52 else:
52 else:
53 raise error.RepoError(_("repository %s not found") % path)
53 raise error.RepoError(_("repository %s not found") % path)
54 elif create:
54 elif create:
55 raise error.RepoError(_("repository %s already exists") % path)
55 raise error.RepoError(_("repository %s already exists") % path)
56 else:
56 else:
57 # find requirements
57 # find requirements
58 requirements = set()
58 requirements = set()
59 try:
59 try:
60 requirements = set(self.opener("requires").read().splitlines())
60 requirements = set(self.opener("requires").read().splitlines())
61 except IOError, inst:
61 except IOError, inst:
62 if inst.errno != errno.ENOENT:
62 if inst.errno != errno.ENOENT:
63 raise
63 raise
64 for r in requirements - self.supported:
64 for r in requirements - self.supported:
65 raise error.RepoError(_("requirement '%s' not supported") % r)
65 raise error.RepoError(_("requirement '%s' not supported") % r)
66
66
67 self.store = store.store(requirements, self.path, util.opener)
67 self.store = store.store(requirements, self.path, util.opener)
68 self.spath = self.store.path
68 self.spath = self.store.path
69 self.sopener = self.store.opener
69 self.sopener = self.store.opener
70 self.sjoin = self.store.join
70 self.sjoin = self.store.join
71 self.opener.createmode = self.store.createmode
71 self.opener.createmode = self.store.createmode
72
72
73 self.baseui = baseui
73 self.baseui = baseui
74 self.ui = baseui.copy()
74 self.ui = baseui.copy()
75 try:
75 try:
76 self.ui.readconfig(self.join("hgrc"), self.root)
76 self.ui.readconfig(self.join("hgrc"), self.root)
77 extensions.loadall(self.ui)
77 extensions.loadall(self.ui)
78 except IOError:
78 except IOError:
79 pass
79 pass
80
80
81 self.tagscache = None
81 self.tagscache = None
82 self._tagstypecache = None
82 self._tagstypecache = None
83 self.branchcache = None
83 self.branchcache = None
84 self._ubranchcache = None # UTF-8 version of branchcache
84 self._ubranchcache = None # UTF-8 version of branchcache
85 self._branchcachetip = None
85 self._branchcachetip = None
86 self.nodetagscache = None
86 self.nodetagscache = None
87 self.filterpats = {}
87 self.filterpats = {}
88 self._datafilters = {}
88 self._datafilters = {}
89 self._transref = self._lockref = self._wlockref = None
89 self._transref = self._lockref = self._wlockref = None
90
90
91 @propertycache
91 @propertycache
92 def changelog(self):
92 def changelog(self):
93 c = changelog.changelog(self.sopener)
93 c = changelog.changelog(self.sopener)
94 if 'HG_PENDING' in os.environ:
94 if 'HG_PENDING' in os.environ:
95 p = os.environ['HG_PENDING']
95 p = os.environ['HG_PENDING']
96 if p.startswith(self.root):
96 if p.startswith(self.root):
97 c.readpending('00changelog.i.a')
97 c.readpending('00changelog.i.a')
98 self.sopener.defversion = c.version
98 self.sopener.defversion = c.version
99 return c
99 return c
100
100
101 @propertycache
101 @propertycache
102 def manifest(self):
102 def manifest(self):
103 return manifest.manifest(self.sopener)
103 return manifest.manifest(self.sopener)
104
104
105 @propertycache
105 @propertycache
106 def dirstate(self):
106 def dirstate(self):
107 return dirstate.dirstate(self.opener, self.ui, self.root)
107 return dirstate.dirstate(self.opener, self.ui, self.root)
108
108
109 def __getitem__(self, changeid):
109 def __getitem__(self, changeid):
110 if changeid == None:
110 if changeid == None:
111 return context.workingctx(self)
111 return context.workingctx(self)
112 return context.changectx(self, changeid)
112 return context.changectx(self, changeid)
113
113
114 def __nonzero__(self):
114 def __nonzero__(self):
115 return True
115 return True
116
116
117 def __len__(self):
117 def __len__(self):
118 return len(self.changelog)
118 return len(self.changelog)
119
119
120 def __iter__(self):
120 def __iter__(self):
121 for i in xrange(len(self)):
121 for i in xrange(len(self)):
122 yield i
122 yield i
123
123
124 def url(self):
124 def url(self):
125 return 'file:' + self.root
125 return 'file:' + self.root
126
126
127 def hook(self, name, throw=False, **args):
127 def hook(self, name, throw=False, **args):
128 return hook.hook(self.ui, self, name, throw, **args)
128 return hook.hook(self.ui, self, name, throw, **args)
129
129
130 tag_disallowed = ':\r\n'
130 tag_disallowed = ':\r\n'
131
131
132 def _tag(self, names, node, message, local, user, date, parent=None,
132 def _tag(self, names, node, message, local, user, date, parent=None,
133 extra={}):
133 extra={}):
134 use_dirstate = parent is None
134 use_dirstate = parent is None
135
135
136 if isinstance(names, str):
136 if isinstance(names, str):
137 allchars = names
137 allchars = names
138 names = (names,)
138 names = (names,)
139 else:
139 else:
140 allchars = ''.join(names)
140 allchars = ''.join(names)
141 for c in self.tag_disallowed:
141 for c in self.tag_disallowed:
142 if c in allchars:
142 if c in allchars:
143 raise util.Abort(_('%r cannot be used in a tag name') % c)
143 raise util.Abort(_('%r cannot be used in a tag name') % c)
144
144
145 for name in names:
145 for name in names:
146 self.hook('pretag', throw=True, node=hex(node), tag=name,
146 self.hook('pretag', throw=True, node=hex(node), tag=name,
147 local=local)
147 local=local)
148
148
149 def writetags(fp, names, munge, prevtags):
149 def writetags(fp, names, munge, prevtags):
150 fp.seek(0, 2)
150 fp.seek(0, 2)
151 if prevtags and prevtags[-1] != '\n':
151 if prevtags and prevtags[-1] != '\n':
152 fp.write('\n')
152 fp.write('\n')
153 for name in names:
153 for name in names:
154 m = munge and munge(name) or name
154 m = munge and munge(name) or name
155 if self._tagstypecache and name in self._tagstypecache:
155 if self._tagstypecache and name in self._tagstypecache:
156 old = self.tagscache.get(name, nullid)
156 old = self.tagscache.get(name, nullid)
157 fp.write('%s %s\n' % (hex(old), m))
157 fp.write('%s %s\n' % (hex(old), m))
158 fp.write('%s %s\n' % (hex(node), m))
158 fp.write('%s %s\n' % (hex(node), m))
159 fp.close()
159 fp.close()
160
160
161 prevtags = ''
161 prevtags = ''
162 if local:
162 if local:
163 try:
163 try:
164 fp = self.opener('localtags', 'r+')
164 fp = self.opener('localtags', 'r+')
165 except IOError:
165 except IOError:
166 fp = self.opener('localtags', 'a')
166 fp = self.opener('localtags', 'a')
167 else:
167 else:
168 prevtags = fp.read()
168 prevtags = fp.read()
169
169
170 # local tags are stored in the current charset
170 # local tags are stored in the current charset
171 writetags(fp, names, None, prevtags)
171 writetags(fp, names, None, prevtags)
172 for name in names:
172 for name in names:
173 self.hook('tag', node=hex(node), tag=name, local=local)
173 self.hook('tag', node=hex(node), tag=name, local=local)
174 return
174 return
175
175
176 if use_dirstate:
176 if use_dirstate:
177 try:
177 try:
178 fp = self.wfile('.hgtags', 'rb+')
178 fp = self.wfile('.hgtags', 'rb+')
179 except IOError:
179 except IOError:
180 fp = self.wfile('.hgtags', 'ab')
180 fp = self.wfile('.hgtags', 'ab')
181 else:
181 else:
182 prevtags = fp.read()
182 prevtags = fp.read()
183 else:
183 else:
184 try:
184 try:
185 prevtags = self.filectx('.hgtags', parent).data()
185 prevtags = self.filectx('.hgtags', parent).data()
186 except error.LookupError:
186 except error.LookupError:
187 pass
187 pass
188 fp = self.wfile('.hgtags', 'wb')
188 fp = self.wfile('.hgtags', 'wb')
189 if prevtags:
189 if prevtags:
190 fp.write(prevtags)
190 fp.write(prevtags)
191
191
192 # committed tags are stored in UTF-8
192 # committed tags are stored in UTF-8
193 writetags(fp, names, encoding.fromlocal, prevtags)
193 writetags(fp, names, encoding.fromlocal, prevtags)
194
194
195 if use_dirstate and '.hgtags' not in self.dirstate:
195 if use_dirstate and '.hgtags' not in self.dirstate:
196 self.add(['.hgtags'])
196 self.add(['.hgtags'])
197
197
198 tagnode = self.commit(['.hgtags'], message, user, date, p1=parent,
198 tagnode = self.commit(['.hgtags'], message, user, date, p1=parent,
199 extra=extra)
199 extra=extra)
200
200
201 for name in names:
201 for name in names:
202 self.hook('tag', node=hex(node), tag=name, local=local)
202 self.hook('tag', node=hex(node), tag=name, local=local)
203
203
204 return tagnode
204 return tagnode
205
205
206 def tag(self, names, node, message, local, user, date):
206 def tag(self, names, node, message, local, user, date):
207 '''tag a revision with one or more symbolic names.
207 '''tag a revision with one or more symbolic names.
208
208
209 names is a list of strings or, when adding a single tag, names may be a
209 names is a list of strings or, when adding a single tag, names may be a
210 string.
210 string.
211
211
212 if local is True, the tags are stored in a per-repository file.
212 if local is True, the tags are stored in a per-repository file.
213 otherwise, they are stored in the .hgtags file, and a new
213 otherwise, they are stored in the .hgtags file, and a new
214 changeset is committed with the change.
214 changeset is committed with the change.
215
215
216 keyword arguments:
216 keyword arguments:
217
217
218 local: whether to store tags in non-version-controlled file
218 local: whether to store tags in non-version-controlled file
219 (default False)
219 (default False)
220
220
221 message: commit message to use if committing
221 message: commit message to use if committing
222
222
223 user: name of user to use if committing
223 user: name of user to use if committing
224
224
225 date: date tuple to use if committing'''
225 date: date tuple to use if committing'''
226
226
227 for x in self.status()[:5]:
227 for x in self.status()[:5]:
228 if '.hgtags' in x:
228 if '.hgtags' in x:
229 raise util.Abort(_('working copy of .hgtags is changed '
229 raise util.Abort(_('working copy of .hgtags is changed '
230 '(please commit .hgtags manually)'))
230 '(please commit .hgtags manually)'))
231
231
232 self.tags() # instantiate the cache
232 self.tags() # instantiate the cache
233 self._tag(names, node, message, local, user, date)
233 self._tag(names, node, message, local, user, date)
234
234
235 def tags(self):
235 def tags(self):
236 '''return a mapping of tag to node'''
236 '''return a mapping of tag to node'''
237 if self.tagscache:
237 if self.tagscache:
238 return self.tagscache
238 return self.tagscache
239
239
240 globaltags = {}
240 globaltags = {}
241 tagtypes = {}
241 tagtypes = {}
242
242
243 def readtags(lines, fn, tagtype):
243 def readtags(lines, fn, tagtype):
244 filetags = {}
244 filetags = {}
245 count = 0
245 count = 0
246
246
247 def warn(msg):
247 def warn(msg):
248 self.ui.warn(_("%s, line %s: %s\n") % (fn, count, msg))
248 self.ui.warn(_("%s, line %s: %s\n") % (fn, count, msg))
249
249
250 for l in lines:
250 for l in lines:
251 count += 1
251 count += 1
252 if not l:
252 if not l:
253 continue
253 continue
254 s = l.split(" ", 1)
254 s = l.split(" ", 1)
255 if len(s) != 2:
255 if len(s) != 2:
256 warn(_("cannot parse entry"))
256 warn(_("cannot parse entry"))
257 continue
257 continue
258 node, key = s
258 node, key = s
259 key = encoding.tolocal(key.strip()) # stored in UTF-8
259 key = encoding.tolocal(key.strip()) # stored in UTF-8
260 try:
260 try:
261 bin_n = bin(node)
261 bin_n = bin(node)
262 except TypeError:
262 except TypeError:
263 warn(_("node '%s' is not well formed") % node)
263 warn(_("node '%s' is not well formed") % node)
264 continue
264 continue
265 if bin_n not in self.changelog.nodemap:
265 if bin_n not in self.changelog.nodemap:
266 warn(_("tag '%s' refers to unknown node") % key)
266 warn(_("tag '%s' refers to unknown node") % key)
267 continue
267 continue
268
268
269 h = []
269 h = []
270 if key in filetags:
270 if key in filetags:
271 n, h = filetags[key]
271 n, h = filetags[key]
272 h.append(n)
272 h.append(n)
273 filetags[key] = (bin_n, h)
273 filetags[key] = (bin_n, h)
274
274
275 for k, nh in filetags.iteritems():
275 for k, nh in filetags.iteritems():
276 if k not in globaltags:
276 if k not in globaltags:
277 globaltags[k] = nh
277 globaltags[k] = nh
278 tagtypes[k] = tagtype
278 tagtypes[k] = tagtype
279 continue
279 continue
280
280
281 # we prefer the global tag if:
281 # we prefer the global tag if:
282 # it supercedes us OR
282 # it supercedes us OR
283 # mutual supercedes and it has a higher rank
283 # mutual supercedes and it has a higher rank
284 # otherwise we win because we're tip-most
284 # otherwise we win because we're tip-most
285 an, ah = nh
285 an, ah = nh
286 bn, bh = globaltags[k]
286 bn, bh = globaltags[k]
287 if (bn != an and an in bh and
287 if (bn != an and an in bh and
288 (bn not in ah or len(bh) > len(ah))):
288 (bn not in ah or len(bh) > len(ah))):
289 an = bn
289 an = bn
290 ah.extend([n for n in bh if n not in ah])
290 ah.extend([n for n in bh if n not in ah])
291 globaltags[k] = an, ah
291 globaltags[k] = an, ah
292 tagtypes[k] = tagtype
292 tagtypes[k] = tagtype
293
293
294 # read the tags file from each head, ending with the tip
294 # read the tags file from each head, ending with the tip
295 f = None
295 f = None
296 for rev, node, fnode in self._hgtagsnodes():
296 for rev, node, fnode in self._hgtagsnodes():
297 f = (f and f.filectx(fnode) or
297 f = (f and f.filectx(fnode) or
298 self.filectx('.hgtags', fileid=fnode))
298 self.filectx('.hgtags', fileid=fnode))
299 readtags(f.data().splitlines(), f, "global")
299 readtags(f.data().splitlines(), f, "global")
300
300
301 try:
301 try:
302 data = encoding.fromlocal(self.opener("localtags").read())
302 data = encoding.fromlocal(self.opener("localtags").read())
303 # localtags are stored in the local character set
303 # localtags are stored in the local character set
304 # while the internal tag table is stored in UTF-8
304 # while the internal tag table is stored in UTF-8
305 readtags(data.splitlines(), "localtags", "local")
305 readtags(data.splitlines(), "localtags", "local")
306 except IOError:
306 except IOError:
307 pass
307 pass
308
308
309 self.tagscache = {}
309 self.tagscache = {}
310 self._tagstypecache = {}
310 self._tagstypecache = {}
311 for k, nh in globaltags.iteritems():
311 for k, nh in globaltags.iteritems():
312 n = nh[0]
312 n = nh[0]
313 if n != nullid:
313 if n != nullid:
314 self.tagscache[k] = n
314 self.tagscache[k] = n
315 self._tagstypecache[k] = tagtypes[k]
315 self._tagstypecache[k] = tagtypes[k]
316 self.tagscache['tip'] = self.changelog.tip()
316 self.tagscache['tip'] = self.changelog.tip()
317 return self.tagscache
317 return self.tagscache
318
318
319 def tagtype(self, tagname):
319 def tagtype(self, tagname):
320 '''
320 '''
321 return the type of the given tag. result can be:
321 return the type of the given tag. result can be:
322
322
323 'local' : a local tag
323 'local' : a local tag
324 'global' : a global tag
324 'global' : a global tag
325 None : tag does not exist
325 None : tag does not exist
326 '''
326 '''
327
327
328 self.tags()
328 self.tags()
329
329
330 return self._tagstypecache.get(tagname)
330 return self._tagstypecache.get(tagname)
331
331
332 def _hgtagsnodes(self):
332 def _hgtagsnodes(self):
333 last = {}
333 last = {}
334 ret = []
334 ret = []
335 for node in reversed(self.heads()):
335 for node in reversed(self.heads()):
336 c = self[node]
336 c = self[node]
337 rev = c.rev()
337 rev = c.rev()
338 try:
338 try:
339 fnode = c.filenode('.hgtags')
339 fnode = c.filenode('.hgtags')
340 except error.LookupError:
340 except error.LookupError:
341 continue
341 continue
342 ret.append((rev, node, fnode))
342 ret.append((rev, node, fnode))
343 if fnode in last:
343 if fnode in last:
344 ret[last[fnode]] = None
344 ret[last[fnode]] = None
345 last[fnode] = len(ret) - 1
345 last[fnode] = len(ret) - 1
346 return [item for item in ret if item]
346 return [item for item in ret if item]
347
347
348 def tagslist(self):
348 def tagslist(self):
349 '''return a list of tags ordered by revision'''
349 '''return a list of tags ordered by revision'''
350 l = []
350 l = []
351 for t, n in self.tags().iteritems():
351 for t, n in self.tags().iteritems():
352 try:
352 try:
353 r = self.changelog.rev(n)
353 r = self.changelog.rev(n)
354 except:
354 except:
355 r = -2 # sort to the beginning of the list if unknown
355 r = -2 # sort to the beginning of the list if unknown
356 l.append((r, t, n))
356 l.append((r, t, n))
357 return [(t, n) for r, t, n in sorted(l)]
357 return [(t, n) for r, t, n in sorted(l)]
358
358
359 def nodetags(self, node):
359 def nodetags(self, node):
360 '''return the tags associated with a node'''
360 '''return the tags associated with a node'''
361 if not self.nodetagscache:
361 if not self.nodetagscache:
362 self.nodetagscache = {}
362 self.nodetagscache = {}
363 for t, n in self.tags().iteritems():
363 for t, n in self.tags().iteritems():
364 self.nodetagscache.setdefault(n, []).append(t)
364 self.nodetagscache.setdefault(n, []).append(t)
365 return self.nodetagscache.get(node, [])
365 return self.nodetagscache.get(node, [])
366
366
367 def _branchtags(self, partial, lrev):
367 def _branchtags(self, partial, lrev):
368 # TODO: rename this function?
368 # TODO: rename this function?
369 tiprev = len(self) - 1
369 tiprev = len(self) - 1
370 if lrev != tiprev:
370 if lrev != tiprev:
371 self._updatebranchcache(partial, lrev+1, tiprev+1)
371 self._updatebranchcache(partial, lrev+1, tiprev+1)
372 self._writebranchcache(partial, self.changelog.tip(), tiprev)
372 self._writebranchcache(partial, self.changelog.tip(), tiprev)
373
373
374 return partial
374 return partial
375
375
376 def _branchheads(self):
376 def _branchheads(self):
377 tip = self.changelog.tip()
377 tip = self.changelog.tip()
378 if self.branchcache is not None and self._branchcachetip == tip:
378 if self.branchcache is not None and self._branchcachetip == tip:
379 return self.branchcache
379 return self.branchcache
380
380
381 oldtip = self._branchcachetip
381 oldtip = self._branchcachetip
382 self._branchcachetip = tip
382 self._branchcachetip = tip
383 if self.branchcache is None:
383 if self.branchcache is None:
384 self.branchcache = {} # avoid recursion in changectx
384 self.branchcache = {} # avoid recursion in changectx
385 else:
385 else:
386 self.branchcache.clear() # keep using the same dict
386 self.branchcache.clear() # keep using the same dict
387 if oldtip is None or oldtip not in self.changelog.nodemap:
387 if oldtip is None or oldtip not in self.changelog.nodemap:
388 partial, last, lrev = self._readbranchcache()
388 partial, last, lrev = self._readbranchcache()
389 else:
389 else:
390 lrev = self.changelog.rev(oldtip)
390 lrev = self.changelog.rev(oldtip)
391 partial = self._ubranchcache
391 partial = self._ubranchcache
392
392
393 self._branchtags(partial, lrev)
393 self._branchtags(partial, lrev)
394 # this private cache holds all heads (not just tips)
394 # this private cache holds all heads (not just tips)
395 self._ubranchcache = partial
395 self._ubranchcache = partial
396
396
397 # the branch cache is stored on disk as UTF-8, but in the local
397 # the branch cache is stored on disk as UTF-8, but in the local
398 # charset internally
398 # charset internally
399 for k, v in partial.iteritems():
399 for k, v in partial.iteritems():
400 self.branchcache[encoding.tolocal(k)] = v
400 self.branchcache[encoding.tolocal(k)] = v
401 return self.branchcache
401 return self.branchcache
402
402
403
403
404 def branchtags(self):
404 def branchtags(self):
405 '''return a dict where branch names map to the tipmost head of
405 '''return a dict where branch names map to the tipmost head of
406 the branch, open heads come before closed'''
406 the branch, open heads come before closed'''
407 bt = {}
407 bt = {}
408 for bn, heads in self._branchheads().iteritems():
408 for bn, heads in self._branchheads().iteritems():
409 head = None
409 head = None
410 for i in range(len(heads)-1, -1, -1):
410 for i in range(len(heads)-1, -1, -1):
411 h = heads[i]
411 h = heads[i]
412 if 'close' not in self.changelog.read(h)[5]:
412 if 'close' not in self.changelog.read(h)[5]:
413 head = h
413 head = h
414 break
414 break
415 # no open heads were found
415 # no open heads were found
416 if head is None:
416 if head is None:
417 head = heads[-1]
417 head = heads[-1]
418 bt[bn] = head
418 bt[bn] = head
419 return bt
419 return bt
420
420
421
421
422 def _readbranchcache(self):
422 def _readbranchcache(self):
423 partial = {}
423 partial = {}
424 try:
424 try:
425 f = self.opener("branchheads.cache")
425 f = self.opener("branchheads.cache")
426 lines = f.read().split('\n')
426 lines = f.read().split('\n')
427 f.close()
427 f.close()
428 except (IOError, OSError):
428 except (IOError, OSError):
429 return {}, nullid, nullrev
429 return {}, nullid, nullrev
430
430
431 try:
431 try:
432 last, lrev = lines.pop(0).split(" ", 1)
432 last, lrev = lines.pop(0).split(" ", 1)
433 last, lrev = bin(last), int(lrev)
433 last, lrev = bin(last), int(lrev)
434 if lrev >= len(self) or self[lrev].node() != last:
434 if lrev >= len(self) or self[lrev].node() != last:
435 # invalidate the cache
435 # invalidate the cache
436 raise ValueError('invalidating branch cache (tip differs)')
436 raise ValueError('invalidating branch cache (tip differs)')
437 for l in lines:
437 for l in lines:
438 if not l: continue
438 if not l: continue
439 node, label = l.split(" ", 1)
439 node, label = l.split(" ", 1)
440 partial.setdefault(label.strip(), []).append(bin(node))
440 partial.setdefault(label.strip(), []).append(bin(node))
441 except KeyboardInterrupt:
441 except KeyboardInterrupt:
442 raise
442 raise
443 except Exception, inst:
443 except Exception, inst:
444 if self.ui.debugflag:
444 if self.ui.debugflag:
445 self.ui.warn(str(inst), '\n')
445 self.ui.warn(str(inst), '\n')
446 partial, last, lrev = {}, nullid, nullrev
446 partial, last, lrev = {}, nullid, nullrev
447 return partial, last, lrev
447 return partial, last, lrev
448
448
449 def _writebranchcache(self, branches, tip, tiprev):
449 def _writebranchcache(self, branches, tip, tiprev):
450 try:
450 try:
451 f = self.opener("branchheads.cache", "w", atomictemp=True)
451 f = self.opener("branchheads.cache", "w", atomictemp=True)
452 f.write("%s %s\n" % (hex(tip), tiprev))
452 f.write("%s %s\n" % (hex(tip), tiprev))
453 for label, nodes in branches.iteritems():
453 for label, nodes in branches.iteritems():
454 for node in nodes:
454 for node in nodes:
455 f.write("%s %s\n" % (hex(node), label))
455 f.write("%s %s\n" % (hex(node), label))
456 f.rename()
456 f.rename()
457 except (IOError, OSError):
457 except (IOError, OSError):
458 pass
458 pass
459
459
460 def _updatebranchcache(self, partial, start, end):
460 def _updatebranchcache(self, partial, start, end):
461 for r in xrange(start, end):
461 for r in xrange(start, end):
462 c = self[r]
462 c = self[r]
463 b = c.branch()
463 b = c.branch()
464 bheads = partial.setdefault(b, [])
464 bheads = partial.setdefault(b, [])
465 bheads.append(c.node())
465 bheads.append(c.node())
466 for p in c.parents():
466 for p in c.parents():
467 pn = p.node()
467 pn = p.node()
468 if pn in bheads:
468 if pn in bheads:
469 bheads.remove(pn)
469 bheads.remove(pn)
470
470
471 def lookup(self, key):
471 def lookup(self, key):
472 if isinstance(key, int):
472 if isinstance(key, int):
473 return self.changelog.node(key)
473 return self.changelog.node(key)
474 elif key == '.':
474 elif key == '.':
475 return self.dirstate.parents()[0]
475 return self.dirstate.parents()[0]
476 elif key == 'null':
476 elif key == 'null':
477 return nullid
477 return nullid
478 elif key == 'tip':
478 elif key == 'tip':
479 return self.changelog.tip()
479 return self.changelog.tip()
480 n = self.changelog._match(key)
480 n = self.changelog._match(key)
481 if n:
481 if n:
482 return n
482 return n
483 if key in self.tags():
483 if key in self.tags():
484 return self.tags()[key]
484 return self.tags()[key]
485 if key in self.branchtags():
485 if key in self.branchtags():
486 return self.branchtags()[key]
486 return self.branchtags()[key]
487 n = self.changelog._partialmatch(key)
487 n = self.changelog._partialmatch(key)
488 if n:
488 if n:
489 return n
489 return n
490 try:
490 try:
491 if len(key) == 20:
491 if len(key) == 20:
492 key = hex(key)
492 key = hex(key)
493 except:
493 except:
494 pass
494 pass
495 raise error.RepoError(_("unknown revision '%s'") % key)
495 raise error.RepoError(_("unknown revision '%s'") % key)
496
496
497 def local(self):
497 def local(self):
498 return True
498 return True
499
499
500 def join(self, f):
500 def join(self, f):
501 return os.path.join(self.path, f)
501 return os.path.join(self.path, f)
502
502
503 def wjoin(self, f):
503 def wjoin(self, f):
504 return os.path.join(self.root, f)
504 return os.path.join(self.root, f)
505
505
506 def rjoin(self, f):
506 def rjoin(self, f):
507 return os.path.join(self.root, util.pconvert(f))
507 return os.path.join(self.root, util.pconvert(f))
508
508
509 def file(self, f):
509 def file(self, f):
510 if f[0] == '/':
510 if f[0] == '/':
511 f = f[1:]
511 f = f[1:]
512 return filelog.filelog(self.sopener, f)
512 return filelog.filelog(self.sopener, f)
513
513
514 def changectx(self, changeid):
514 def changectx(self, changeid):
515 return self[changeid]
515 return self[changeid]
516
516
517 def parents(self, changeid=None):
517 def parents(self, changeid=None):
518 '''get list of changectxs for parents of changeid'''
518 '''get list of changectxs for parents of changeid'''
519 return self[changeid].parents()
519 return self[changeid].parents()
520
520
521 def filectx(self, path, changeid=None, fileid=None):
521 def filectx(self, path, changeid=None, fileid=None):
522 """changeid can be a changeset revision, node, or tag.
522 """changeid can be a changeset revision, node, or tag.
523 fileid can be a file revision or node."""
523 fileid can be a file revision or node."""
524 return context.filectx(self, path, changeid, fileid)
524 return context.filectx(self, path, changeid, fileid)
525
525
526 def getcwd(self):
526 def getcwd(self):
527 return self.dirstate.getcwd()
527 return self.dirstate.getcwd()
528
528
529 def pathto(self, f, cwd=None):
529 def pathto(self, f, cwd=None):
530 return self.dirstate.pathto(f, cwd)
530 return self.dirstate.pathto(f, cwd)
531
531
532 def wfile(self, f, mode='r'):
532 def wfile(self, f, mode='r'):
533 return self.wopener(f, mode)
533 return self.wopener(f, mode)
534
534
535 def _link(self, f):
535 def _link(self, f):
536 return os.path.islink(self.wjoin(f))
536 return os.path.islink(self.wjoin(f))
537
537
538 def _filter(self, filter, filename, data):
538 def _filter(self, filter, filename, data):
539 if filter not in self.filterpats:
539 if filter not in self.filterpats:
540 l = []
540 l = []
541 for pat, cmd in self.ui.configitems(filter):
541 for pat, cmd in self.ui.configitems(filter):
542 if cmd == '!':
542 if cmd == '!':
543 continue
543 continue
544 mf = util.matcher(self.root, "", [pat], [], [])[1]
544 mf = util.matcher(self.root, "", [pat], [], [])[1]
545 fn = None
545 fn = None
546 params = cmd
546 params = cmd
547 for name, filterfn in self._datafilters.iteritems():
547 for name, filterfn in self._datafilters.iteritems():
548 if cmd.startswith(name):
548 if cmd.startswith(name):
549 fn = filterfn
549 fn = filterfn
550 params = cmd[len(name):].lstrip()
550 params = cmd[len(name):].lstrip()
551 break
551 break
552 if not fn:
552 if not fn:
553 fn = lambda s, c, **kwargs: util.filter(s, c)
553 fn = lambda s, c, **kwargs: util.filter(s, c)
554 # Wrap old filters not supporting keyword arguments
554 # Wrap old filters not supporting keyword arguments
555 if not inspect.getargspec(fn)[2]:
555 if not inspect.getargspec(fn)[2]:
556 oldfn = fn
556 oldfn = fn
557 fn = lambda s, c, **kwargs: oldfn(s, c)
557 fn = lambda s, c, **kwargs: oldfn(s, c)
558 l.append((mf, fn, params))
558 l.append((mf, fn, params))
559 self.filterpats[filter] = l
559 self.filterpats[filter] = l
560
560
561 for mf, fn, cmd in self.filterpats[filter]:
561 for mf, fn, cmd in self.filterpats[filter]:
562 if mf(filename):
562 if mf(filename):
563 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
563 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
564 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
564 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
565 break
565 break
566
566
567 return data
567 return data
568
568
569 def adddatafilter(self, name, filter):
569 def adddatafilter(self, name, filter):
570 self._datafilters[name] = filter
570 self._datafilters[name] = filter
571
571
572 def wread(self, filename):
572 def wread(self, filename):
573 if self._link(filename):
573 if self._link(filename):
574 data = os.readlink(self.wjoin(filename))
574 data = os.readlink(self.wjoin(filename))
575 else:
575 else:
576 data = self.wopener(filename, 'r').read()
576 data = self.wopener(filename, 'r').read()
577 return self._filter("encode", filename, data)
577 return self._filter("encode", filename, data)
578
578
579 def wwrite(self, filename, data, flags):
579 def wwrite(self, filename, data, flags):
580 data = self._filter("decode", filename, data)
580 data = self._filter("decode", filename, data)
581 try:
581 try:
582 os.unlink(self.wjoin(filename))
582 os.unlink(self.wjoin(filename))
583 except OSError:
583 except OSError:
584 pass
584 pass
585 if 'l' in flags:
585 if 'l' in flags:
586 self.wopener.symlink(data, filename)
586 self.wopener.symlink(data, filename)
587 else:
587 else:
588 self.wopener(filename, 'w').write(data)
588 self.wopener(filename, 'w').write(data)
589 if 'x' in flags:
589 if 'x' in flags:
590 util.set_flags(self.wjoin(filename), False, True)
590 util.set_flags(self.wjoin(filename), False, True)
591
591
592 def wwritedata(self, filename, data):
592 def wwritedata(self, filename, data):
593 return self._filter("decode", filename, data)
593 return self._filter("decode", filename, data)
594
594
595 def transaction(self):
595 def transaction(self):
596 tr = self._transref and self._transref() or None
596 tr = self._transref and self._transref() or None
597 if tr and tr.running():
597 if tr and tr.running():
598 return tr.nest()
598 return tr.nest()
599
599
600 # abort here if the journal already exists
600 # abort here if the journal already exists
601 if os.path.exists(self.sjoin("journal")):
601 if os.path.exists(self.sjoin("journal")):
602 raise error.RepoError(_("journal already exists - run hg recover"))
602 raise error.RepoError(_("journal already exists - run hg recover"))
603
603
604 # save dirstate for rollback
604 # save dirstate for rollback
605 try:
605 try:
606 ds = self.opener("dirstate").read()
606 ds = self.opener("dirstate").read()
607 except IOError:
607 except IOError:
608 ds = ""
608 ds = ""
609 self.opener("journal.dirstate", "w").write(ds)
609 self.opener("journal.dirstate", "w").write(ds)
610 self.opener("journal.branch", "w").write(self.dirstate.branch())
610 self.opener("journal.branch", "w").write(self.dirstate.branch())
611
611
612 renames = [(self.sjoin("journal"), self.sjoin("undo")),
612 renames = [(self.sjoin("journal"), self.sjoin("undo")),
613 (self.join("journal.dirstate"), self.join("undo.dirstate")),
613 (self.join("journal.dirstate"), self.join("undo.dirstate")),
614 (self.join("journal.branch"), self.join("undo.branch"))]
614 (self.join("journal.branch"), self.join("undo.branch"))]
615 tr = transaction.transaction(self.ui.warn, self.sopener,
615 tr = transaction.transaction(self.ui.warn, self.sopener,
616 self.sjoin("journal"),
616 self.sjoin("journal"),
617 aftertrans(renames),
617 aftertrans(renames),
618 self.store.createmode)
618 self.store.createmode)
619 self._transref = weakref.ref(tr)
619 self._transref = weakref.ref(tr)
620 return tr
620 return tr
621
621
622 def recover(self):
622 def recover(self):
623 lock = self.lock()
623 lock = self.lock()
624 try:
624 try:
625 if os.path.exists(self.sjoin("journal")):
625 if os.path.exists(self.sjoin("journal")):
626 self.ui.status(_("rolling back interrupted transaction\n"))
626 self.ui.status(_("rolling back interrupted transaction\n"))
627 transaction.rollback(self.sopener, self.sjoin("journal"))
627 transaction.rollback(self.sopener, self.sjoin("journal"), self.ui.warn)
628 self.invalidate()
628 self.invalidate()
629 return True
629 return True
630 else:
630 else:
631 self.ui.warn(_("no interrupted transaction available\n"))
631 self.ui.warn(_("no interrupted transaction available\n"))
632 return False
632 return False
633 finally:
633 finally:
634 lock.release()
634 lock.release()
635
635
636 def rollback(self):
636 def rollback(self):
637 wlock = lock = None
637 wlock = lock = None
638 try:
638 try:
639 wlock = self.wlock()
639 wlock = self.wlock()
640 lock = self.lock()
640 lock = self.lock()
641 if os.path.exists(self.sjoin("undo")):
641 if os.path.exists(self.sjoin("undo")):
642 self.ui.status(_("rolling back last transaction\n"))
642 self.ui.status(_("rolling back last transaction\n"))
643 transaction.rollback(self.sopener, self.sjoin("undo"))
643 transaction.rollback(self.sopener, self.sjoin("undo"), self.ui.warn)
644 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
644 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
645 try:
645 try:
646 branch = self.opener("undo.branch").read()
646 branch = self.opener("undo.branch").read()
647 self.dirstate.setbranch(branch)
647 self.dirstate.setbranch(branch)
648 except IOError:
648 except IOError:
649 self.ui.warn(_("Named branch could not be reset, "
649 self.ui.warn(_("Named branch could not be reset, "
650 "current branch still is: %s\n")
650 "current branch still is: %s\n")
651 % encoding.tolocal(self.dirstate.branch()))
651 % encoding.tolocal(self.dirstate.branch()))
652 self.invalidate()
652 self.invalidate()
653 self.dirstate.invalidate()
653 self.dirstate.invalidate()
654 else:
654 else:
655 self.ui.warn(_("no rollback information available\n"))
655 self.ui.warn(_("no rollback information available\n"))
656 finally:
656 finally:
657 release(lock, wlock)
657 release(lock, wlock)
658
658
659 def invalidate(self):
659 def invalidate(self):
660 for a in "changelog manifest".split():
660 for a in "changelog manifest".split():
661 if a in self.__dict__:
661 if a in self.__dict__:
662 delattr(self, a)
662 delattr(self, a)
663 self.tagscache = None
663 self.tagscache = None
664 self._tagstypecache = None
664 self._tagstypecache = None
665 self.nodetagscache = None
665 self.nodetagscache = None
666 self.branchcache = None
666 self.branchcache = None
667 self._ubranchcache = None
667 self._ubranchcache = None
668 self._branchcachetip = None
668 self._branchcachetip = None
669
669
670 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
670 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
671 try:
671 try:
672 l = lock.lock(lockname, 0, releasefn, desc=desc)
672 l = lock.lock(lockname, 0, releasefn, desc=desc)
673 except error.LockHeld, inst:
673 except error.LockHeld, inst:
674 if not wait:
674 if not wait:
675 raise
675 raise
676 self.ui.warn(_("waiting for lock on %s held by %r\n") %
676 self.ui.warn(_("waiting for lock on %s held by %r\n") %
677 (desc, inst.locker))
677 (desc, inst.locker))
678 # default to 600 seconds timeout
678 # default to 600 seconds timeout
679 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
679 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
680 releasefn, desc=desc)
680 releasefn, desc=desc)
681 if acquirefn:
681 if acquirefn:
682 acquirefn()
682 acquirefn()
683 return l
683 return l
684
684
685 def lock(self, wait=True):
685 def lock(self, wait=True):
686 l = self._lockref and self._lockref()
686 l = self._lockref and self._lockref()
687 if l is not None and l.held:
687 if l is not None and l.held:
688 l.lock()
688 l.lock()
689 return l
689 return l
690
690
691 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
691 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
692 _('repository %s') % self.origroot)
692 _('repository %s') % self.origroot)
693 self._lockref = weakref.ref(l)
693 self._lockref = weakref.ref(l)
694 return l
694 return l
695
695
696 def wlock(self, wait=True):
696 def wlock(self, wait=True):
697 l = self._wlockref and self._wlockref()
697 l = self._wlockref and self._wlockref()
698 if l is not None and l.held:
698 if l is not None and l.held:
699 l.lock()
699 l.lock()
700 return l
700 return l
701
701
702 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
702 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
703 self.dirstate.invalidate, _('working directory of %s') %
703 self.dirstate.invalidate, _('working directory of %s') %
704 self.origroot)
704 self.origroot)
705 self._wlockref = weakref.ref(l)
705 self._wlockref = weakref.ref(l)
706 return l
706 return l
707
707
708 def filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
708 def filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
709 """
709 """
710 commit an individual file as part of a larger transaction
710 commit an individual file as part of a larger transaction
711 """
711 """
712
712
713 fname = fctx.path()
713 fname = fctx.path()
714 text = fctx.data()
714 text = fctx.data()
715 flog = self.file(fname)
715 flog = self.file(fname)
716 fparent1 = manifest1.get(fname, nullid)
716 fparent1 = manifest1.get(fname, nullid)
717 fparent2 = manifest2.get(fname, nullid)
717 fparent2 = manifest2.get(fname, nullid)
718
718
719 meta = {}
719 meta = {}
720 copy = fctx.renamed()
720 copy = fctx.renamed()
721 if copy and copy[0] != fname:
721 if copy and copy[0] != fname:
722 # Mark the new revision of this file as a copy of another
722 # Mark the new revision of this file as a copy of another
723 # file. This copy data will effectively act as a parent
723 # file. This copy data will effectively act as a parent
724 # of this new revision. If this is a merge, the first
724 # of this new revision. If this is a merge, the first
725 # parent will be the nullid (meaning "look up the copy data")
725 # parent will be the nullid (meaning "look up the copy data")
726 # and the second one will be the other parent. For example:
726 # and the second one will be the other parent. For example:
727 #
727 #
728 # 0 --- 1 --- 3 rev1 changes file foo
728 # 0 --- 1 --- 3 rev1 changes file foo
729 # \ / rev2 renames foo to bar and changes it
729 # \ / rev2 renames foo to bar and changes it
730 # \- 2 -/ rev3 should have bar with all changes and
730 # \- 2 -/ rev3 should have bar with all changes and
731 # should record that bar descends from
731 # should record that bar descends from
732 # bar in rev2 and foo in rev1
732 # bar in rev2 and foo in rev1
733 #
733 #
734 # this allows this merge to succeed:
734 # this allows this merge to succeed:
735 #
735 #
736 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
736 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
737 # \ / merging rev3 and rev4 should use bar@rev2
737 # \ / merging rev3 and rev4 should use bar@rev2
738 # \- 2 --- 4 as the merge base
738 # \- 2 --- 4 as the merge base
739 #
739 #
740
740
741 cfname = copy[0]
741 cfname = copy[0]
742 crev = manifest1.get(cfname)
742 crev = manifest1.get(cfname)
743 newfparent = fparent2
743 newfparent = fparent2
744
744
745 if manifest2: # branch merge
745 if manifest2: # branch merge
746 if fparent2 == nullid or crev is None: # copied on remote side
746 if fparent2 == nullid or crev is None: # copied on remote side
747 if cfname in manifest2:
747 if cfname in manifest2:
748 crev = manifest2[cfname]
748 crev = manifest2[cfname]
749 newfparent = fparent1
749 newfparent = fparent1
750
750
751 # find source in nearest ancestor if we've lost track
751 # find source in nearest ancestor if we've lost track
752 if not crev:
752 if not crev:
753 self.ui.debug(_(" %s: searching for copy revision for %s\n") %
753 self.ui.debug(_(" %s: searching for copy revision for %s\n") %
754 (fname, cfname))
754 (fname, cfname))
755 for ancestor in self['.'].ancestors():
755 for ancestor in self['.'].ancestors():
756 if cfname in ancestor:
756 if cfname in ancestor:
757 crev = ancestor[cfname].filenode()
757 crev = ancestor[cfname].filenode()
758 break
758 break
759
759
760 self.ui.debug(_(" %s: copy %s:%s\n") % (fname, cfname, hex(crev)))
760 self.ui.debug(_(" %s: copy %s:%s\n") % (fname, cfname, hex(crev)))
761 meta["copy"] = cfname
761 meta["copy"] = cfname
762 meta["copyrev"] = hex(crev)
762 meta["copyrev"] = hex(crev)
763 fparent1, fparent2 = nullid, newfparent
763 fparent1, fparent2 = nullid, newfparent
764 elif fparent2 != nullid:
764 elif fparent2 != nullid:
765 # is one parent an ancestor of the other?
765 # is one parent an ancestor of the other?
766 fparentancestor = flog.ancestor(fparent1, fparent2)
766 fparentancestor = flog.ancestor(fparent1, fparent2)
767 if fparentancestor == fparent1:
767 if fparentancestor == fparent1:
768 fparent1, fparent2 = fparent2, nullid
768 fparent1, fparent2 = fparent2, nullid
769 elif fparentancestor == fparent2:
769 elif fparentancestor == fparent2:
770 fparent2 = nullid
770 fparent2 = nullid
771
771
772 # is the file unmodified from the parent? report existing entry
772 # is the file unmodified from the parent? report existing entry
773 if fparent2 == nullid and not flog.cmp(fparent1, text) and not meta:
773 if fparent2 == nullid and not flog.cmp(fparent1, text) and not meta:
774 return fparent1
774 return fparent1
775
775
776 changelist.append(fname)
776 changelist.append(fname)
777 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
777 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
778
778
779 def rawcommit(self, files, text, user, date, p1=None, p2=None, extra={}):
779 def rawcommit(self, files, text, user, date, p1=None, p2=None, extra={}):
780 if p1 is None:
780 if p1 is None:
781 p1, p2 = self.dirstate.parents()
781 p1, p2 = self.dirstate.parents()
782 return self.commit(files=files, text=text, user=user, date=date,
782 return self.commit(files=files, text=text, user=user, date=date,
783 p1=p1, p2=p2, extra=extra, empty_ok=True)
783 p1=p1, p2=p2, extra=extra, empty_ok=True)
784
784
785 def commit(self, files=None, text="", user=None, date=None,
785 def commit(self, files=None, text="", user=None, date=None,
786 match=None, force=False, force_editor=False,
786 match=None, force=False, force_editor=False,
787 p1=None, p2=None, extra={}, empty_ok=False):
787 p1=None, p2=None, extra={}, empty_ok=False):
788 wlock = lock = None
788 wlock = lock = None
789 if extra.get("close"):
789 if extra.get("close"):
790 force = True
790 force = True
791 if files:
791 if files:
792 files = list(set(files))
792 files = list(set(files))
793 try:
793 try:
794 wlock = self.wlock()
794 wlock = self.wlock()
795 lock = self.lock()
795 lock = self.lock()
796 use_dirstate = (p1 is None) # not rawcommit
796 use_dirstate = (p1 is None) # not rawcommit
797
797
798 if use_dirstate:
798 if use_dirstate:
799 p1, p2 = self.dirstate.parents()
799 p1, p2 = self.dirstate.parents()
800 update_dirstate = True
800 update_dirstate = True
801
801
802 if (not force and p2 != nullid and
802 if (not force and p2 != nullid and
803 (match and (match.files() or match.anypats()))):
803 (match and (match.files() or match.anypats()))):
804 raise util.Abort(_('cannot partially commit a merge '
804 raise util.Abort(_('cannot partially commit a merge '
805 '(do not specify files or patterns)'))
805 '(do not specify files or patterns)'))
806
806
807 if files:
807 if files:
808 modified, removed = [], []
808 modified, removed = [], []
809 for f in files:
809 for f in files:
810 s = self.dirstate[f]
810 s = self.dirstate[f]
811 if s in 'nma':
811 if s in 'nma':
812 modified.append(f)
812 modified.append(f)
813 elif s == 'r':
813 elif s == 'r':
814 removed.append(f)
814 removed.append(f)
815 else:
815 else:
816 self.ui.warn(_("%s not tracked!\n") % f)
816 self.ui.warn(_("%s not tracked!\n") % f)
817 changes = [modified, [], removed, [], []]
817 changes = [modified, [], removed, [], []]
818 else:
818 else:
819 changes = self.status(match=match)
819 changes = self.status(match=match)
820 else:
820 else:
821 p1, p2 = p1, p2 or nullid
821 p1, p2 = p1, p2 or nullid
822 update_dirstate = (self.dirstate.parents()[0] == p1)
822 update_dirstate = (self.dirstate.parents()[0] == p1)
823 changes = [files, [], [], [], []]
823 changes = [files, [], [], [], []]
824
824
825 ms = merge_.mergestate(self)
825 ms = merge_.mergestate(self)
826 for f in changes[0]:
826 for f in changes[0]:
827 if f in ms and ms[f] == 'u':
827 if f in ms and ms[f] == 'u':
828 raise util.Abort(_("unresolved merge conflicts "
828 raise util.Abort(_("unresolved merge conflicts "
829 "(see hg resolve)"))
829 "(see hg resolve)"))
830 wctx = context.workingctx(self, (p1, p2), text, user, date,
830 wctx = context.workingctx(self, (p1, p2), text, user, date,
831 extra, changes)
831 extra, changes)
832 r = self._commitctx(wctx, force, force_editor, empty_ok,
832 r = self._commitctx(wctx, force, force_editor, empty_ok,
833 use_dirstate, update_dirstate)
833 use_dirstate, update_dirstate)
834 ms.reset()
834 ms.reset()
835 return r
835 return r
836
836
837 finally:
837 finally:
838 release(lock, wlock)
838 release(lock, wlock)
839
839
840 def commitctx(self, ctx):
840 def commitctx(self, ctx):
841 """Add a new revision to current repository.
841 """Add a new revision to current repository.
842
842
843 Revision information is passed in the context.memctx argument.
843 Revision information is passed in the context.memctx argument.
844 commitctx() does not touch the working directory.
844 commitctx() does not touch the working directory.
845 """
845 """
846 wlock = lock = None
846 wlock = lock = None
847 try:
847 try:
848 wlock = self.wlock()
848 wlock = self.wlock()
849 lock = self.lock()
849 lock = self.lock()
850 return self._commitctx(ctx, force=True, force_editor=False,
850 return self._commitctx(ctx, force=True, force_editor=False,
851 empty_ok=True, use_dirstate=False,
851 empty_ok=True, use_dirstate=False,
852 update_dirstate=False)
852 update_dirstate=False)
853 finally:
853 finally:
854 release(lock, wlock)
854 release(lock, wlock)
855
855
856 def _commitctx(self, wctx, force=False, force_editor=False, empty_ok=False,
856 def _commitctx(self, wctx, force=False, force_editor=False, empty_ok=False,
857 use_dirstate=True, update_dirstate=True):
857 use_dirstate=True, update_dirstate=True):
858 tr = None
858 tr = None
859 valid = 0 # don't save the dirstate if this isn't set
859 valid = 0 # don't save the dirstate if this isn't set
860 try:
860 try:
861 commit = sorted(wctx.modified() + wctx.added())
861 commit = sorted(wctx.modified() + wctx.added())
862 remove = wctx.removed()
862 remove = wctx.removed()
863 extra = wctx.extra().copy()
863 extra = wctx.extra().copy()
864 branchname = extra['branch']
864 branchname = extra['branch']
865 user = wctx.user()
865 user = wctx.user()
866 text = wctx.description()
866 text = wctx.description()
867
867
868 p1, p2 = [p.node() for p in wctx.parents()]
868 p1, p2 = [p.node() for p in wctx.parents()]
869 c1 = self.changelog.read(p1)
869 c1 = self.changelog.read(p1)
870 c2 = self.changelog.read(p2)
870 c2 = self.changelog.read(p2)
871 m1 = self.manifest.read(c1[0]).copy()
871 m1 = self.manifest.read(c1[0]).copy()
872 m2 = self.manifest.read(c2[0])
872 m2 = self.manifest.read(c2[0])
873
873
874 if use_dirstate:
874 if use_dirstate:
875 oldname = c1[5].get("branch") # stored in UTF-8
875 oldname = c1[5].get("branch") # stored in UTF-8
876 if (not commit and not remove and not force and p2 == nullid
876 if (not commit and not remove and not force and p2 == nullid
877 and branchname == oldname):
877 and branchname == oldname):
878 self.ui.status(_("nothing changed\n"))
878 self.ui.status(_("nothing changed\n"))
879 return None
879 return None
880
880
881 xp1 = hex(p1)
881 xp1 = hex(p1)
882 if p2 == nullid: xp2 = ''
882 if p2 == nullid: xp2 = ''
883 else: xp2 = hex(p2)
883 else: xp2 = hex(p2)
884
884
885 self.hook("precommit", throw=True, parent1=xp1, parent2=xp2)
885 self.hook("precommit", throw=True, parent1=xp1, parent2=xp2)
886
886
887 tr = self.transaction()
887 tr = self.transaction()
888 trp = weakref.proxy(tr)
888 trp = weakref.proxy(tr)
889
889
890 # check in files
890 # check in files
891 new = {}
891 new = {}
892 changed = []
892 changed = []
893 linkrev = len(self)
893 linkrev = len(self)
894 for f in commit:
894 for f in commit:
895 self.ui.note(f + "\n")
895 self.ui.note(f + "\n")
896 try:
896 try:
897 fctx = wctx.filectx(f)
897 fctx = wctx.filectx(f)
898 newflags = fctx.flags()
898 newflags = fctx.flags()
899 new[f] = self.filecommit(fctx, m1, m2, linkrev, trp, changed)
899 new[f] = self.filecommit(fctx, m1, m2, linkrev, trp, changed)
900 if ((not changed or changed[-1] != f) and
900 if ((not changed or changed[-1] != f) and
901 m2.get(f) != new[f]):
901 m2.get(f) != new[f]):
902 # mention the file in the changelog if some
902 # mention the file in the changelog if some
903 # flag changed, even if there was no content
903 # flag changed, even if there was no content
904 # change.
904 # change.
905 if m1.flags(f) != newflags:
905 if m1.flags(f) != newflags:
906 changed.append(f)
906 changed.append(f)
907 m1.set(f, newflags)
907 m1.set(f, newflags)
908 if use_dirstate:
908 if use_dirstate:
909 self.dirstate.normal(f)
909 self.dirstate.normal(f)
910
910
911 except (OSError, IOError):
911 except (OSError, IOError):
912 if use_dirstate:
912 if use_dirstate:
913 self.ui.warn(_("trouble committing %s!\n") % f)
913 self.ui.warn(_("trouble committing %s!\n") % f)
914 raise
914 raise
915 else:
915 else:
916 remove.append(f)
916 remove.append(f)
917
917
918 updated, added = [], []
918 updated, added = [], []
919 for f in sorted(changed):
919 for f in sorted(changed):
920 if f in m1 or f in m2:
920 if f in m1 or f in m2:
921 updated.append(f)
921 updated.append(f)
922 else:
922 else:
923 added.append(f)
923 added.append(f)
924
924
925 # update manifest
925 # update manifest
926 m1.update(new)
926 m1.update(new)
927 removed = [f for f in sorted(remove) if f in m1 or f in m2]
927 removed = [f for f in sorted(remove) if f in m1 or f in m2]
928 removed1 = []
928 removed1 = []
929
929
930 for f in removed:
930 for f in removed:
931 if f in m1:
931 if f in m1:
932 del m1[f]
932 del m1[f]
933 removed1.append(f)
933 removed1.append(f)
934 mn = self.manifest.add(m1, trp, linkrev, c1[0], c2[0],
934 mn = self.manifest.add(m1, trp, linkrev, c1[0], c2[0],
935 (new, removed1))
935 (new, removed1))
936
936
937 # add changeset
937 # add changeset
938 if (not empty_ok and not text) or force_editor:
938 if (not empty_ok and not text) or force_editor:
939 edittext = []
939 edittext = []
940 if text:
940 if text:
941 edittext.append(text)
941 edittext.append(text)
942 edittext.append("")
942 edittext.append("")
943 edittext.append("") # Empty line between message and comments.
943 edittext.append("") # Empty line between message and comments.
944 edittext.append(_("HG: Enter commit message."
944 edittext.append(_("HG: Enter commit message."
945 " Lines beginning with 'HG:' are removed."))
945 " Lines beginning with 'HG:' are removed."))
946 edittext.append("HG: --")
946 edittext.append("HG: --")
947 edittext.append(_("HG: user: %s") % user)
947 edittext.append(_("HG: user: %s") % user)
948 if p2 != nullid:
948 if p2 != nullid:
949 edittext.append(_("HG: branch merge"))
949 edittext.append(_("HG: branch merge"))
950 if branchname:
950 if branchname:
951 edittext.append(_("HG: branch '%s'")
951 edittext.append(_("HG: branch '%s'")
952 % encoding.tolocal(branchname))
952 % encoding.tolocal(branchname))
953 edittext.extend([_("HG: added %s") % f for f in added])
953 edittext.extend([_("HG: added %s") % f for f in added])
954 edittext.extend([_("HG: changed %s") % f for f in updated])
954 edittext.extend([_("HG: changed %s") % f for f in updated])
955 edittext.extend([_("HG: removed %s") % f for f in removed])
955 edittext.extend([_("HG: removed %s") % f for f in removed])
956 if not added and not updated and not removed:
956 if not added and not updated and not removed:
957 edittext.append(_("HG: no files changed"))
957 edittext.append(_("HG: no files changed"))
958 edittext.append("")
958 edittext.append("")
959 # run editor in the repository root
959 # run editor in the repository root
960 olddir = os.getcwd()
960 olddir = os.getcwd()
961 os.chdir(self.root)
961 os.chdir(self.root)
962 text = self.ui.edit("\n".join(edittext), user)
962 text = self.ui.edit("\n".join(edittext), user)
963 os.chdir(olddir)
963 os.chdir(olddir)
964
964
965 lines = [line.rstrip() for line in text.rstrip().splitlines()]
965 lines = [line.rstrip() for line in text.rstrip().splitlines()]
966 while lines and not lines[0]:
966 while lines and not lines[0]:
967 del lines[0]
967 del lines[0]
968 if not lines and use_dirstate:
968 if not lines and use_dirstate:
969 raise util.Abort(_("empty commit message"))
969 raise util.Abort(_("empty commit message"))
970 text = '\n'.join(lines)
970 text = '\n'.join(lines)
971
971
972 self.changelog.delayupdate()
972 self.changelog.delayupdate()
973 n = self.changelog.add(mn, changed + removed, text, trp, p1, p2,
973 n = self.changelog.add(mn, changed + removed, text, trp, p1, p2,
974 user, wctx.date(), extra)
974 user, wctx.date(), extra)
975 p = lambda: self.changelog.writepending() and self.root or ""
975 p = lambda: self.changelog.writepending() and self.root or ""
976 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
976 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
977 parent2=xp2, pending=p)
977 parent2=xp2, pending=p)
978 self.changelog.finalize(trp)
978 self.changelog.finalize(trp)
979 tr.close()
979 tr.close()
980
980
981 if self.branchcache:
981 if self.branchcache:
982 self.branchtags()
982 self.branchtags()
983
983
984 if use_dirstate or update_dirstate:
984 if use_dirstate or update_dirstate:
985 self.dirstate.setparents(n)
985 self.dirstate.setparents(n)
986 if use_dirstate:
986 if use_dirstate:
987 for f in removed:
987 for f in removed:
988 self.dirstate.forget(f)
988 self.dirstate.forget(f)
989 valid = 1 # our dirstate updates are complete
989 valid = 1 # our dirstate updates are complete
990
990
991 self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
991 self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
992 return n
992 return n
993 finally:
993 finally:
994 if not valid: # don't save our updated dirstate
994 if not valid: # don't save our updated dirstate
995 self.dirstate.invalidate()
995 self.dirstate.invalidate()
996 del tr
996 del tr
997
997
998 def walk(self, match, node=None):
998 def walk(self, match, node=None):
999 '''
999 '''
1000 walk recursively through the directory tree or a given
1000 walk recursively through the directory tree or a given
1001 changeset, finding all files matched by the match
1001 changeset, finding all files matched by the match
1002 function
1002 function
1003 '''
1003 '''
1004 return self[node].walk(match)
1004 return self[node].walk(match)
1005
1005
1006 def status(self, node1='.', node2=None, match=None,
1006 def status(self, node1='.', node2=None, match=None,
1007 ignored=False, clean=False, unknown=False):
1007 ignored=False, clean=False, unknown=False):
1008 """return status of files between two nodes or node and working directory
1008 """return status of files between two nodes or node and working directory
1009
1009
1010 If node1 is None, use the first dirstate parent instead.
1010 If node1 is None, use the first dirstate parent instead.
1011 If node2 is None, compare node1 with working directory.
1011 If node2 is None, compare node1 with working directory.
1012 """
1012 """
1013
1013
1014 def mfmatches(ctx):
1014 def mfmatches(ctx):
1015 mf = ctx.manifest().copy()
1015 mf = ctx.manifest().copy()
1016 for fn in mf.keys():
1016 for fn in mf.keys():
1017 if not match(fn):
1017 if not match(fn):
1018 del mf[fn]
1018 del mf[fn]
1019 return mf
1019 return mf
1020
1020
1021 if isinstance(node1, context.changectx):
1021 if isinstance(node1, context.changectx):
1022 ctx1 = node1
1022 ctx1 = node1
1023 else:
1023 else:
1024 ctx1 = self[node1]
1024 ctx1 = self[node1]
1025 if isinstance(node2, context.changectx):
1025 if isinstance(node2, context.changectx):
1026 ctx2 = node2
1026 ctx2 = node2
1027 else:
1027 else:
1028 ctx2 = self[node2]
1028 ctx2 = self[node2]
1029
1029
1030 working = ctx2.rev() is None
1030 working = ctx2.rev() is None
1031 parentworking = working and ctx1 == self['.']
1031 parentworking = working and ctx1 == self['.']
1032 match = match or match_.always(self.root, self.getcwd())
1032 match = match or match_.always(self.root, self.getcwd())
1033 listignored, listclean, listunknown = ignored, clean, unknown
1033 listignored, listclean, listunknown = ignored, clean, unknown
1034
1034
1035 # load earliest manifest first for caching reasons
1035 # load earliest manifest first for caching reasons
1036 if not working and ctx2.rev() < ctx1.rev():
1036 if not working and ctx2.rev() < ctx1.rev():
1037 ctx2.manifest()
1037 ctx2.manifest()
1038
1038
1039 if not parentworking:
1039 if not parentworking:
1040 def bad(f, msg):
1040 def bad(f, msg):
1041 if f not in ctx1:
1041 if f not in ctx1:
1042 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1042 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1043 return False
1043 return False
1044 match.bad = bad
1044 match.bad = bad
1045
1045
1046 if working: # we need to scan the working dir
1046 if working: # we need to scan the working dir
1047 s = self.dirstate.status(match, listignored, listclean, listunknown)
1047 s = self.dirstate.status(match, listignored, listclean, listunknown)
1048 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1048 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1049
1049
1050 # check for any possibly clean files
1050 # check for any possibly clean files
1051 if parentworking and cmp:
1051 if parentworking and cmp:
1052 fixup = []
1052 fixup = []
1053 # do a full compare of any files that might have changed
1053 # do a full compare of any files that might have changed
1054 for f in cmp:
1054 for f in cmp:
1055 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1055 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1056 or ctx1[f].cmp(ctx2[f].data())):
1056 or ctx1[f].cmp(ctx2[f].data())):
1057 modified.append(f)
1057 modified.append(f)
1058 else:
1058 else:
1059 fixup.append(f)
1059 fixup.append(f)
1060
1060
1061 if listclean:
1061 if listclean:
1062 clean += fixup
1062 clean += fixup
1063
1063
1064 # update dirstate for files that are actually clean
1064 # update dirstate for files that are actually clean
1065 if fixup:
1065 if fixup:
1066 wlock = None
1066 wlock = None
1067 try:
1067 try:
1068 try:
1068 try:
1069 # updating the dirstate is optional
1069 # updating the dirstate is optional
1070 # so we don't wait on the lock
1070 # so we don't wait on the lock
1071 wlock = self.wlock(False)
1071 wlock = self.wlock(False)
1072 for f in fixup:
1072 for f in fixup:
1073 self.dirstate.normal(f)
1073 self.dirstate.normal(f)
1074 except error.LockError:
1074 except error.LockError:
1075 pass
1075 pass
1076 finally:
1076 finally:
1077 release(wlock)
1077 release(wlock)
1078
1078
1079 if not parentworking:
1079 if not parentworking:
1080 mf1 = mfmatches(ctx1)
1080 mf1 = mfmatches(ctx1)
1081 if working:
1081 if working:
1082 # we are comparing working dir against non-parent
1082 # we are comparing working dir against non-parent
1083 # generate a pseudo-manifest for the working dir
1083 # generate a pseudo-manifest for the working dir
1084 mf2 = mfmatches(self['.'])
1084 mf2 = mfmatches(self['.'])
1085 for f in cmp + modified + added:
1085 for f in cmp + modified + added:
1086 mf2[f] = None
1086 mf2[f] = None
1087 mf2.set(f, ctx2.flags(f))
1087 mf2.set(f, ctx2.flags(f))
1088 for f in removed:
1088 for f in removed:
1089 if f in mf2:
1089 if f in mf2:
1090 del mf2[f]
1090 del mf2[f]
1091 else:
1091 else:
1092 # we are comparing two revisions
1092 # we are comparing two revisions
1093 deleted, unknown, ignored = [], [], []
1093 deleted, unknown, ignored = [], [], []
1094 mf2 = mfmatches(ctx2)
1094 mf2 = mfmatches(ctx2)
1095
1095
1096 modified, added, clean = [], [], []
1096 modified, added, clean = [], [], []
1097 for fn in mf2:
1097 for fn in mf2:
1098 if fn in mf1:
1098 if fn in mf1:
1099 if (mf1.flags(fn) != mf2.flags(fn) or
1099 if (mf1.flags(fn) != mf2.flags(fn) or
1100 (mf1[fn] != mf2[fn] and
1100 (mf1[fn] != mf2[fn] and
1101 (mf2[fn] or ctx1[fn].cmp(ctx2[fn].data())))):
1101 (mf2[fn] or ctx1[fn].cmp(ctx2[fn].data())))):
1102 modified.append(fn)
1102 modified.append(fn)
1103 elif listclean:
1103 elif listclean:
1104 clean.append(fn)
1104 clean.append(fn)
1105 del mf1[fn]
1105 del mf1[fn]
1106 else:
1106 else:
1107 added.append(fn)
1107 added.append(fn)
1108 removed = mf1.keys()
1108 removed = mf1.keys()
1109
1109
1110 r = modified, added, removed, deleted, unknown, ignored, clean
1110 r = modified, added, removed, deleted, unknown, ignored, clean
1111 [l.sort() for l in r]
1111 [l.sort() for l in r]
1112 return r
1112 return r
1113
1113
1114 def add(self, list):
1114 def add(self, list):
1115 wlock = self.wlock()
1115 wlock = self.wlock()
1116 try:
1116 try:
1117 rejected = []
1117 rejected = []
1118 for f in list:
1118 for f in list:
1119 p = self.wjoin(f)
1119 p = self.wjoin(f)
1120 try:
1120 try:
1121 st = os.lstat(p)
1121 st = os.lstat(p)
1122 except:
1122 except:
1123 self.ui.warn(_("%s does not exist!\n") % f)
1123 self.ui.warn(_("%s does not exist!\n") % f)
1124 rejected.append(f)
1124 rejected.append(f)
1125 continue
1125 continue
1126 if st.st_size > 10000000:
1126 if st.st_size > 10000000:
1127 self.ui.warn(_("%s: files over 10MB may cause memory and"
1127 self.ui.warn(_("%s: files over 10MB may cause memory and"
1128 " performance problems\n"
1128 " performance problems\n"
1129 "(use 'hg revert %s' to unadd the file)\n")
1129 "(use 'hg revert %s' to unadd the file)\n")
1130 % (f, f))
1130 % (f, f))
1131 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1131 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1132 self.ui.warn(_("%s not added: only files and symlinks "
1132 self.ui.warn(_("%s not added: only files and symlinks "
1133 "supported currently\n") % f)
1133 "supported currently\n") % f)
1134 rejected.append(p)
1134 rejected.append(p)
1135 elif self.dirstate[f] in 'amn':
1135 elif self.dirstate[f] in 'amn':
1136 self.ui.warn(_("%s already tracked!\n") % f)
1136 self.ui.warn(_("%s already tracked!\n") % f)
1137 elif self.dirstate[f] == 'r':
1137 elif self.dirstate[f] == 'r':
1138 self.dirstate.normallookup(f)
1138 self.dirstate.normallookup(f)
1139 else:
1139 else:
1140 self.dirstate.add(f)
1140 self.dirstate.add(f)
1141 return rejected
1141 return rejected
1142 finally:
1142 finally:
1143 wlock.release()
1143 wlock.release()
1144
1144
1145 def forget(self, list):
1145 def forget(self, list):
1146 wlock = self.wlock()
1146 wlock = self.wlock()
1147 try:
1147 try:
1148 for f in list:
1148 for f in list:
1149 if self.dirstate[f] != 'a':
1149 if self.dirstate[f] != 'a':
1150 self.ui.warn(_("%s not added!\n") % f)
1150 self.ui.warn(_("%s not added!\n") % f)
1151 else:
1151 else:
1152 self.dirstate.forget(f)
1152 self.dirstate.forget(f)
1153 finally:
1153 finally:
1154 wlock.release()
1154 wlock.release()
1155
1155
1156 def remove(self, list, unlink=False):
1156 def remove(self, list, unlink=False):
1157 wlock = None
1157 wlock = None
1158 try:
1158 try:
1159 if unlink:
1159 if unlink:
1160 for f in list:
1160 for f in list:
1161 try:
1161 try:
1162 util.unlink(self.wjoin(f))
1162 util.unlink(self.wjoin(f))
1163 except OSError, inst:
1163 except OSError, inst:
1164 if inst.errno != errno.ENOENT:
1164 if inst.errno != errno.ENOENT:
1165 raise
1165 raise
1166 wlock = self.wlock()
1166 wlock = self.wlock()
1167 for f in list:
1167 for f in list:
1168 if unlink and os.path.exists(self.wjoin(f)):
1168 if unlink and os.path.exists(self.wjoin(f)):
1169 self.ui.warn(_("%s still exists!\n") % f)
1169 self.ui.warn(_("%s still exists!\n") % f)
1170 elif self.dirstate[f] == 'a':
1170 elif self.dirstate[f] == 'a':
1171 self.dirstate.forget(f)
1171 self.dirstate.forget(f)
1172 elif f not in self.dirstate:
1172 elif f not in self.dirstate:
1173 self.ui.warn(_("%s not tracked!\n") % f)
1173 self.ui.warn(_("%s not tracked!\n") % f)
1174 else:
1174 else:
1175 self.dirstate.remove(f)
1175 self.dirstate.remove(f)
1176 finally:
1176 finally:
1177 release(wlock)
1177 release(wlock)
1178
1178
1179 def undelete(self, list):
1179 def undelete(self, list):
1180 manifests = [self.manifest.read(self.changelog.read(p)[0])
1180 manifests = [self.manifest.read(self.changelog.read(p)[0])
1181 for p in self.dirstate.parents() if p != nullid]
1181 for p in self.dirstate.parents() if p != nullid]
1182 wlock = self.wlock()
1182 wlock = self.wlock()
1183 try:
1183 try:
1184 for f in list:
1184 for f in list:
1185 if self.dirstate[f] != 'r':
1185 if self.dirstate[f] != 'r':
1186 self.ui.warn(_("%s not removed!\n") % f)
1186 self.ui.warn(_("%s not removed!\n") % f)
1187 else:
1187 else:
1188 m = f in manifests[0] and manifests[0] or manifests[1]
1188 m = f in manifests[0] and manifests[0] or manifests[1]
1189 t = self.file(f).read(m[f])
1189 t = self.file(f).read(m[f])
1190 self.wwrite(f, t, m.flags(f))
1190 self.wwrite(f, t, m.flags(f))
1191 self.dirstate.normal(f)
1191 self.dirstate.normal(f)
1192 finally:
1192 finally:
1193 wlock.release()
1193 wlock.release()
1194
1194
1195 def copy(self, source, dest):
1195 def copy(self, source, dest):
1196 p = self.wjoin(dest)
1196 p = self.wjoin(dest)
1197 if not (os.path.exists(p) or os.path.islink(p)):
1197 if not (os.path.exists(p) or os.path.islink(p)):
1198 self.ui.warn(_("%s does not exist!\n") % dest)
1198 self.ui.warn(_("%s does not exist!\n") % dest)
1199 elif not (os.path.isfile(p) or os.path.islink(p)):
1199 elif not (os.path.isfile(p) or os.path.islink(p)):
1200 self.ui.warn(_("copy failed: %s is not a file or a "
1200 self.ui.warn(_("copy failed: %s is not a file or a "
1201 "symbolic link\n") % dest)
1201 "symbolic link\n") % dest)
1202 else:
1202 else:
1203 wlock = self.wlock()
1203 wlock = self.wlock()
1204 try:
1204 try:
1205 if self.dirstate[dest] in '?r':
1205 if self.dirstate[dest] in '?r':
1206 self.dirstate.add(dest)
1206 self.dirstate.add(dest)
1207 self.dirstate.copy(source, dest)
1207 self.dirstate.copy(source, dest)
1208 finally:
1208 finally:
1209 wlock.release()
1209 wlock.release()
1210
1210
1211 def heads(self, start=None, closed=True):
1211 def heads(self, start=None, closed=True):
1212 heads = self.changelog.heads(start)
1212 heads = self.changelog.heads(start)
1213 def display(head):
1213 def display(head):
1214 if closed:
1214 if closed:
1215 return True
1215 return True
1216 extras = self.changelog.read(head)[5]
1216 extras = self.changelog.read(head)[5]
1217 return ('close' not in extras)
1217 return ('close' not in extras)
1218 # sort the output in rev descending order
1218 # sort the output in rev descending order
1219 heads = [(-self.changelog.rev(h), h) for h in heads if display(h)]
1219 heads = [(-self.changelog.rev(h), h) for h in heads if display(h)]
1220 return [n for (r, n) in sorted(heads)]
1220 return [n for (r, n) in sorted(heads)]
1221
1221
1222 def branchheads(self, branch=None, start=None, closed=True):
1222 def branchheads(self, branch=None, start=None, closed=True):
1223 if branch is None:
1223 if branch is None:
1224 branch = self[None].branch()
1224 branch = self[None].branch()
1225 branches = self._branchheads()
1225 branches = self._branchheads()
1226 if branch not in branches:
1226 if branch not in branches:
1227 return []
1227 return []
1228 bheads = branches[branch]
1228 bheads = branches[branch]
1229 # the cache returns heads ordered lowest to highest
1229 # the cache returns heads ordered lowest to highest
1230 bheads.reverse()
1230 bheads.reverse()
1231 if start is not None:
1231 if start is not None:
1232 # filter out the heads that cannot be reached from startrev
1232 # filter out the heads that cannot be reached from startrev
1233 bheads = self.changelog.nodesbetween([start], bheads)[2]
1233 bheads = self.changelog.nodesbetween([start], bheads)[2]
1234 if not closed:
1234 if not closed:
1235 bheads = [h for h in bheads if
1235 bheads = [h for h in bheads if
1236 ('close' not in self.changelog.read(h)[5])]
1236 ('close' not in self.changelog.read(h)[5])]
1237 return bheads
1237 return bheads
1238
1238
1239 def branches(self, nodes):
1239 def branches(self, nodes):
1240 if not nodes:
1240 if not nodes:
1241 nodes = [self.changelog.tip()]
1241 nodes = [self.changelog.tip()]
1242 b = []
1242 b = []
1243 for n in nodes:
1243 for n in nodes:
1244 t = n
1244 t = n
1245 while 1:
1245 while 1:
1246 p = self.changelog.parents(n)
1246 p = self.changelog.parents(n)
1247 if p[1] != nullid or p[0] == nullid:
1247 if p[1] != nullid or p[0] == nullid:
1248 b.append((t, n, p[0], p[1]))
1248 b.append((t, n, p[0], p[1]))
1249 break
1249 break
1250 n = p[0]
1250 n = p[0]
1251 return b
1251 return b
1252
1252
1253 def between(self, pairs):
1253 def between(self, pairs):
1254 r = []
1254 r = []
1255
1255
1256 for top, bottom in pairs:
1256 for top, bottom in pairs:
1257 n, l, i = top, [], 0
1257 n, l, i = top, [], 0
1258 f = 1
1258 f = 1
1259
1259
1260 while n != bottom and n != nullid:
1260 while n != bottom and n != nullid:
1261 p = self.changelog.parents(n)[0]
1261 p = self.changelog.parents(n)[0]
1262 if i == f:
1262 if i == f:
1263 l.append(n)
1263 l.append(n)
1264 f = f * 2
1264 f = f * 2
1265 n = p
1265 n = p
1266 i += 1
1266 i += 1
1267
1267
1268 r.append(l)
1268 r.append(l)
1269
1269
1270 return r
1270 return r
1271
1271
1272 def findincoming(self, remote, base=None, heads=None, force=False):
1272 def findincoming(self, remote, base=None, heads=None, force=False):
1273 """Return list of roots of the subsets of missing nodes from remote
1273 """Return list of roots of the subsets of missing nodes from remote
1274
1274
1275 If base dict is specified, assume that these nodes and their parents
1275 If base dict is specified, assume that these nodes and their parents
1276 exist on the remote side and that no child of a node of base exists
1276 exist on the remote side and that no child of a node of base exists
1277 in both remote and self.
1277 in both remote and self.
1278 Furthermore base will be updated to include the nodes that exists
1278 Furthermore base will be updated to include the nodes that exists
1279 in self and remote but no children exists in self and remote.
1279 in self and remote but no children exists in self and remote.
1280 If a list of heads is specified, return only nodes which are heads
1280 If a list of heads is specified, return only nodes which are heads
1281 or ancestors of these heads.
1281 or ancestors of these heads.
1282
1282
1283 All the ancestors of base are in self and in remote.
1283 All the ancestors of base are in self and in remote.
1284 All the descendants of the list returned are missing in self.
1284 All the descendants of the list returned are missing in self.
1285 (and so we know that the rest of the nodes are missing in remote, see
1285 (and so we know that the rest of the nodes are missing in remote, see
1286 outgoing)
1286 outgoing)
1287 """
1287 """
1288 return self.findcommonincoming(remote, base, heads, force)[1]
1288 return self.findcommonincoming(remote, base, heads, force)[1]
1289
1289
1290 def findcommonincoming(self, remote, base=None, heads=None, force=False):
1290 def findcommonincoming(self, remote, base=None, heads=None, force=False):
1291 """Return a tuple (common, missing roots, heads) used to identify
1291 """Return a tuple (common, missing roots, heads) used to identify
1292 missing nodes from remote.
1292 missing nodes from remote.
1293
1293
1294 If base dict is specified, assume that these nodes and their parents
1294 If base dict is specified, assume that these nodes and their parents
1295 exist on the remote side and that no child of a node of base exists
1295 exist on the remote side and that no child of a node of base exists
1296 in both remote and self.
1296 in both remote and self.
1297 Furthermore base will be updated to include the nodes that exists
1297 Furthermore base will be updated to include the nodes that exists
1298 in self and remote but no children exists in self and remote.
1298 in self and remote but no children exists in self and remote.
1299 If a list of heads is specified, return only nodes which are heads
1299 If a list of heads is specified, return only nodes which are heads
1300 or ancestors of these heads.
1300 or ancestors of these heads.
1301
1301
1302 All the ancestors of base are in self and in remote.
1302 All the ancestors of base are in self and in remote.
1303 """
1303 """
1304 m = self.changelog.nodemap
1304 m = self.changelog.nodemap
1305 search = []
1305 search = []
1306 fetch = set()
1306 fetch = set()
1307 seen = set()
1307 seen = set()
1308 seenbranch = set()
1308 seenbranch = set()
1309 if base == None:
1309 if base == None:
1310 base = {}
1310 base = {}
1311
1311
1312 if not heads:
1312 if not heads:
1313 heads = remote.heads()
1313 heads = remote.heads()
1314
1314
1315 if self.changelog.tip() == nullid:
1315 if self.changelog.tip() == nullid:
1316 base[nullid] = 1
1316 base[nullid] = 1
1317 if heads != [nullid]:
1317 if heads != [nullid]:
1318 return [nullid], [nullid], list(heads)
1318 return [nullid], [nullid], list(heads)
1319 return [nullid], [], []
1319 return [nullid], [], []
1320
1320
1321 # assume we're closer to the tip than the root
1321 # assume we're closer to the tip than the root
1322 # and start by examining the heads
1322 # and start by examining the heads
1323 self.ui.status(_("searching for changes\n"))
1323 self.ui.status(_("searching for changes\n"))
1324
1324
1325 unknown = []
1325 unknown = []
1326 for h in heads:
1326 for h in heads:
1327 if h not in m:
1327 if h not in m:
1328 unknown.append(h)
1328 unknown.append(h)
1329 else:
1329 else:
1330 base[h] = 1
1330 base[h] = 1
1331
1331
1332 heads = unknown
1332 heads = unknown
1333 if not unknown:
1333 if not unknown:
1334 return base.keys(), [], []
1334 return base.keys(), [], []
1335
1335
1336 req = set(unknown)
1336 req = set(unknown)
1337 reqcnt = 0
1337 reqcnt = 0
1338
1338
1339 # search through remote branches
1339 # search through remote branches
1340 # a 'branch' here is a linear segment of history, with four parts:
1340 # a 'branch' here is a linear segment of history, with four parts:
1341 # head, root, first parent, second parent
1341 # head, root, first parent, second parent
1342 # (a branch always has two parents (or none) by definition)
1342 # (a branch always has two parents (or none) by definition)
1343 unknown = remote.branches(unknown)
1343 unknown = remote.branches(unknown)
1344 while unknown:
1344 while unknown:
1345 r = []
1345 r = []
1346 while unknown:
1346 while unknown:
1347 n = unknown.pop(0)
1347 n = unknown.pop(0)
1348 if n[0] in seen:
1348 if n[0] in seen:
1349 continue
1349 continue
1350
1350
1351 self.ui.debug(_("examining %s:%s\n")
1351 self.ui.debug(_("examining %s:%s\n")
1352 % (short(n[0]), short(n[1])))
1352 % (short(n[0]), short(n[1])))
1353 if n[0] == nullid: # found the end of the branch
1353 if n[0] == nullid: # found the end of the branch
1354 pass
1354 pass
1355 elif n in seenbranch:
1355 elif n in seenbranch:
1356 self.ui.debug(_("branch already found\n"))
1356 self.ui.debug(_("branch already found\n"))
1357 continue
1357 continue
1358 elif n[1] and n[1] in m: # do we know the base?
1358 elif n[1] and n[1] in m: # do we know the base?
1359 self.ui.debug(_("found incomplete branch %s:%s\n")
1359 self.ui.debug(_("found incomplete branch %s:%s\n")
1360 % (short(n[0]), short(n[1])))
1360 % (short(n[0]), short(n[1])))
1361 search.append(n[0:2]) # schedule branch range for scanning
1361 search.append(n[0:2]) # schedule branch range for scanning
1362 seenbranch.add(n)
1362 seenbranch.add(n)
1363 else:
1363 else:
1364 if n[1] not in seen and n[1] not in fetch:
1364 if n[1] not in seen and n[1] not in fetch:
1365 if n[2] in m and n[3] in m:
1365 if n[2] in m and n[3] in m:
1366 self.ui.debug(_("found new changeset %s\n") %
1366 self.ui.debug(_("found new changeset %s\n") %
1367 short(n[1]))
1367 short(n[1]))
1368 fetch.add(n[1]) # earliest unknown
1368 fetch.add(n[1]) # earliest unknown
1369 for p in n[2:4]:
1369 for p in n[2:4]:
1370 if p in m:
1370 if p in m:
1371 base[p] = 1 # latest known
1371 base[p] = 1 # latest known
1372
1372
1373 for p in n[2:4]:
1373 for p in n[2:4]:
1374 if p not in req and p not in m:
1374 if p not in req and p not in m:
1375 r.append(p)
1375 r.append(p)
1376 req.add(p)
1376 req.add(p)
1377 seen.add(n[0])
1377 seen.add(n[0])
1378
1378
1379 if r:
1379 if r:
1380 reqcnt += 1
1380 reqcnt += 1
1381 self.ui.debug(_("request %d: %s\n") %
1381 self.ui.debug(_("request %d: %s\n") %
1382 (reqcnt, " ".join(map(short, r))))
1382 (reqcnt, " ".join(map(short, r))))
1383 for p in xrange(0, len(r), 10):
1383 for p in xrange(0, len(r), 10):
1384 for b in remote.branches(r[p:p+10]):
1384 for b in remote.branches(r[p:p+10]):
1385 self.ui.debug(_("received %s:%s\n") %
1385 self.ui.debug(_("received %s:%s\n") %
1386 (short(b[0]), short(b[1])))
1386 (short(b[0]), short(b[1])))
1387 unknown.append(b)
1387 unknown.append(b)
1388
1388
1389 # do binary search on the branches we found
1389 # do binary search on the branches we found
1390 while search:
1390 while search:
1391 newsearch = []
1391 newsearch = []
1392 reqcnt += 1
1392 reqcnt += 1
1393 for n, l in zip(search, remote.between(search)):
1393 for n, l in zip(search, remote.between(search)):
1394 l.append(n[1])
1394 l.append(n[1])
1395 p = n[0]
1395 p = n[0]
1396 f = 1
1396 f = 1
1397 for i in l:
1397 for i in l:
1398 self.ui.debug(_("narrowing %d:%d %s\n") % (f, len(l), short(i)))
1398 self.ui.debug(_("narrowing %d:%d %s\n") % (f, len(l), short(i)))
1399 if i in m:
1399 if i in m:
1400 if f <= 2:
1400 if f <= 2:
1401 self.ui.debug(_("found new branch changeset %s\n") %
1401 self.ui.debug(_("found new branch changeset %s\n") %
1402 short(p))
1402 short(p))
1403 fetch.add(p)
1403 fetch.add(p)
1404 base[i] = 1
1404 base[i] = 1
1405 else:
1405 else:
1406 self.ui.debug(_("narrowed branch search to %s:%s\n")
1406 self.ui.debug(_("narrowed branch search to %s:%s\n")
1407 % (short(p), short(i)))
1407 % (short(p), short(i)))
1408 newsearch.append((p, i))
1408 newsearch.append((p, i))
1409 break
1409 break
1410 p, f = i, f * 2
1410 p, f = i, f * 2
1411 search = newsearch
1411 search = newsearch
1412
1412
1413 # sanity check our fetch list
1413 # sanity check our fetch list
1414 for f in fetch:
1414 for f in fetch:
1415 if f in m:
1415 if f in m:
1416 raise error.RepoError(_("already have changeset ")
1416 raise error.RepoError(_("already have changeset ")
1417 + short(f[:4]))
1417 + short(f[:4]))
1418
1418
1419 if base.keys() == [nullid]:
1419 if base.keys() == [nullid]:
1420 if force:
1420 if force:
1421 self.ui.warn(_("warning: repository is unrelated\n"))
1421 self.ui.warn(_("warning: repository is unrelated\n"))
1422 else:
1422 else:
1423 raise util.Abort(_("repository is unrelated"))
1423 raise util.Abort(_("repository is unrelated"))
1424
1424
1425 self.ui.debug(_("found new changesets starting at ") +
1425 self.ui.debug(_("found new changesets starting at ") +
1426 " ".join([short(f) for f in fetch]) + "\n")
1426 " ".join([short(f) for f in fetch]) + "\n")
1427
1427
1428 self.ui.debug(_("%d total queries\n") % reqcnt)
1428 self.ui.debug(_("%d total queries\n") % reqcnt)
1429
1429
1430 return base.keys(), list(fetch), heads
1430 return base.keys(), list(fetch), heads
1431
1431
1432 def findoutgoing(self, remote, base=None, heads=None, force=False):
1432 def findoutgoing(self, remote, base=None, heads=None, force=False):
1433 """Return list of nodes that are roots of subsets not in remote
1433 """Return list of nodes that are roots of subsets not in remote
1434
1434
1435 If base dict is specified, assume that these nodes and their parents
1435 If base dict is specified, assume that these nodes and their parents
1436 exist on the remote side.
1436 exist on the remote side.
1437 If a list of heads is specified, return only nodes which are heads
1437 If a list of heads is specified, return only nodes which are heads
1438 or ancestors of these heads, and return a second element which
1438 or ancestors of these heads, and return a second element which
1439 contains all remote heads which get new children.
1439 contains all remote heads which get new children.
1440 """
1440 """
1441 if base == None:
1441 if base == None:
1442 base = {}
1442 base = {}
1443 self.findincoming(remote, base, heads, force=force)
1443 self.findincoming(remote, base, heads, force=force)
1444
1444
1445 self.ui.debug(_("common changesets up to ")
1445 self.ui.debug(_("common changesets up to ")
1446 + " ".join(map(short, base.keys())) + "\n")
1446 + " ".join(map(short, base.keys())) + "\n")
1447
1447
1448 remain = set(self.changelog.nodemap)
1448 remain = set(self.changelog.nodemap)
1449
1449
1450 # prune everything remote has from the tree
1450 # prune everything remote has from the tree
1451 remain.remove(nullid)
1451 remain.remove(nullid)
1452 remove = base.keys()
1452 remove = base.keys()
1453 while remove:
1453 while remove:
1454 n = remove.pop(0)
1454 n = remove.pop(0)
1455 if n in remain:
1455 if n in remain:
1456 remain.remove(n)
1456 remain.remove(n)
1457 for p in self.changelog.parents(n):
1457 for p in self.changelog.parents(n):
1458 remove.append(p)
1458 remove.append(p)
1459
1459
1460 # find every node whose parents have been pruned
1460 # find every node whose parents have been pruned
1461 subset = []
1461 subset = []
1462 # find every remote head that will get new children
1462 # find every remote head that will get new children
1463 updated_heads = {}
1463 updated_heads = {}
1464 for n in remain:
1464 for n in remain:
1465 p1, p2 = self.changelog.parents(n)
1465 p1, p2 = self.changelog.parents(n)
1466 if p1 not in remain and p2 not in remain:
1466 if p1 not in remain and p2 not in remain:
1467 subset.append(n)
1467 subset.append(n)
1468 if heads:
1468 if heads:
1469 if p1 in heads:
1469 if p1 in heads:
1470 updated_heads[p1] = True
1470 updated_heads[p1] = True
1471 if p2 in heads:
1471 if p2 in heads:
1472 updated_heads[p2] = True
1472 updated_heads[p2] = True
1473
1473
1474 # this is the set of all roots we have to push
1474 # this is the set of all roots we have to push
1475 if heads:
1475 if heads:
1476 return subset, updated_heads.keys()
1476 return subset, updated_heads.keys()
1477 else:
1477 else:
1478 return subset
1478 return subset
1479
1479
1480 def pull(self, remote, heads=None, force=False):
1480 def pull(self, remote, heads=None, force=False):
1481 lock = self.lock()
1481 lock = self.lock()
1482 try:
1482 try:
1483 common, fetch, rheads = self.findcommonincoming(remote, heads=heads,
1483 common, fetch, rheads = self.findcommonincoming(remote, heads=heads,
1484 force=force)
1484 force=force)
1485 if fetch == [nullid]:
1485 if fetch == [nullid]:
1486 self.ui.status(_("requesting all changes\n"))
1486 self.ui.status(_("requesting all changes\n"))
1487
1487
1488 if not fetch:
1488 if not fetch:
1489 self.ui.status(_("no changes found\n"))
1489 self.ui.status(_("no changes found\n"))
1490 return 0
1490 return 0
1491
1491
1492 if heads is None and remote.capable('changegroupsubset'):
1492 if heads is None and remote.capable('changegroupsubset'):
1493 heads = rheads
1493 heads = rheads
1494
1494
1495 if heads is None:
1495 if heads is None:
1496 cg = remote.changegroup(fetch, 'pull')
1496 cg = remote.changegroup(fetch, 'pull')
1497 else:
1497 else:
1498 if not remote.capable('changegroupsubset'):
1498 if not remote.capable('changegroupsubset'):
1499 raise util.Abort(_("Partial pull cannot be done because other repository doesn't support changegroupsubset."))
1499 raise util.Abort(_("Partial pull cannot be done because other repository doesn't support changegroupsubset."))
1500 cg = remote.changegroupsubset(fetch, heads, 'pull')
1500 cg = remote.changegroupsubset(fetch, heads, 'pull')
1501 return self.addchangegroup(cg, 'pull', remote.url())
1501 return self.addchangegroup(cg, 'pull', remote.url())
1502 finally:
1502 finally:
1503 lock.release()
1503 lock.release()
1504
1504
1505 def push(self, remote, force=False, revs=None):
1505 def push(self, remote, force=False, revs=None):
1506 # there are two ways to push to remote repo:
1506 # there are two ways to push to remote repo:
1507 #
1507 #
1508 # addchangegroup assumes local user can lock remote
1508 # addchangegroup assumes local user can lock remote
1509 # repo (local filesystem, old ssh servers).
1509 # repo (local filesystem, old ssh servers).
1510 #
1510 #
1511 # unbundle assumes local user cannot lock remote repo (new ssh
1511 # unbundle assumes local user cannot lock remote repo (new ssh
1512 # servers, http servers).
1512 # servers, http servers).
1513
1513
1514 if remote.capable('unbundle'):
1514 if remote.capable('unbundle'):
1515 return self.push_unbundle(remote, force, revs)
1515 return self.push_unbundle(remote, force, revs)
1516 return self.push_addchangegroup(remote, force, revs)
1516 return self.push_addchangegroup(remote, force, revs)
1517
1517
1518 def prepush(self, remote, force, revs):
1518 def prepush(self, remote, force, revs):
1519 common = {}
1519 common = {}
1520 remote_heads = remote.heads()
1520 remote_heads = remote.heads()
1521 inc = self.findincoming(remote, common, remote_heads, force=force)
1521 inc = self.findincoming(remote, common, remote_heads, force=force)
1522
1522
1523 update, updated_heads = self.findoutgoing(remote, common, remote_heads)
1523 update, updated_heads = self.findoutgoing(remote, common, remote_heads)
1524 if revs is not None:
1524 if revs is not None:
1525 msng_cl, bases, heads = self.changelog.nodesbetween(update, revs)
1525 msng_cl, bases, heads = self.changelog.nodesbetween(update, revs)
1526 else:
1526 else:
1527 bases, heads = update, self.changelog.heads()
1527 bases, heads = update, self.changelog.heads()
1528
1528
1529 if not bases:
1529 if not bases:
1530 self.ui.status(_("no changes found\n"))
1530 self.ui.status(_("no changes found\n"))
1531 return None, 1
1531 return None, 1
1532 elif not force:
1532 elif not force:
1533 # check if we're creating new remote heads
1533 # check if we're creating new remote heads
1534 # to be a remote head after push, node must be either
1534 # to be a remote head after push, node must be either
1535 # - unknown locally
1535 # - unknown locally
1536 # - a local outgoing head descended from update
1536 # - a local outgoing head descended from update
1537 # - a remote head that's known locally and not
1537 # - a remote head that's known locally and not
1538 # ancestral to an outgoing head
1538 # ancestral to an outgoing head
1539
1539
1540 warn = 0
1540 warn = 0
1541
1541
1542 if remote_heads == [nullid]:
1542 if remote_heads == [nullid]:
1543 warn = 0
1543 warn = 0
1544 elif not revs and len(heads) > len(remote_heads):
1544 elif not revs and len(heads) > len(remote_heads):
1545 warn = 1
1545 warn = 1
1546 else:
1546 else:
1547 newheads = list(heads)
1547 newheads = list(heads)
1548 for r in remote_heads:
1548 for r in remote_heads:
1549 if r in self.changelog.nodemap:
1549 if r in self.changelog.nodemap:
1550 desc = self.changelog.heads(r, heads)
1550 desc = self.changelog.heads(r, heads)
1551 l = [h for h in heads if h in desc]
1551 l = [h for h in heads if h in desc]
1552 if not l:
1552 if not l:
1553 newheads.append(r)
1553 newheads.append(r)
1554 else:
1554 else:
1555 newheads.append(r)
1555 newheads.append(r)
1556 if len(newheads) > len(remote_heads):
1556 if len(newheads) > len(remote_heads):
1557 warn = 1
1557 warn = 1
1558
1558
1559 if warn:
1559 if warn:
1560 self.ui.warn(_("abort: push creates new remote heads!\n"))
1560 self.ui.warn(_("abort: push creates new remote heads!\n"))
1561 self.ui.status(_("(did you forget to merge?"
1561 self.ui.status(_("(did you forget to merge?"
1562 " use push -f to force)\n"))
1562 " use push -f to force)\n"))
1563 return None, 0
1563 return None, 0
1564 elif inc:
1564 elif inc:
1565 self.ui.warn(_("note: unsynced remote changes!\n"))
1565 self.ui.warn(_("note: unsynced remote changes!\n"))
1566
1566
1567
1567
1568 if revs is None:
1568 if revs is None:
1569 # use the fast path, no race possible on push
1569 # use the fast path, no race possible on push
1570 cg = self._changegroup(common.keys(), 'push')
1570 cg = self._changegroup(common.keys(), 'push')
1571 else:
1571 else:
1572 cg = self.changegroupsubset(update, revs, 'push')
1572 cg = self.changegroupsubset(update, revs, 'push')
1573 return cg, remote_heads
1573 return cg, remote_heads
1574
1574
1575 def push_addchangegroup(self, remote, force, revs):
1575 def push_addchangegroup(self, remote, force, revs):
1576 lock = remote.lock()
1576 lock = remote.lock()
1577 try:
1577 try:
1578 ret = self.prepush(remote, force, revs)
1578 ret = self.prepush(remote, force, revs)
1579 if ret[0] is not None:
1579 if ret[0] is not None:
1580 cg, remote_heads = ret
1580 cg, remote_heads = ret
1581 return remote.addchangegroup(cg, 'push', self.url())
1581 return remote.addchangegroup(cg, 'push', self.url())
1582 return ret[1]
1582 return ret[1]
1583 finally:
1583 finally:
1584 lock.release()
1584 lock.release()
1585
1585
1586 def push_unbundle(self, remote, force, revs):
1586 def push_unbundle(self, remote, force, revs):
1587 # local repo finds heads on server, finds out what revs it
1587 # local repo finds heads on server, finds out what revs it
1588 # must push. once revs transferred, if server finds it has
1588 # must push. once revs transferred, if server finds it has
1589 # different heads (someone else won commit/push race), server
1589 # different heads (someone else won commit/push race), server
1590 # aborts.
1590 # aborts.
1591
1591
1592 ret = self.prepush(remote, force, revs)
1592 ret = self.prepush(remote, force, revs)
1593 if ret[0] is not None:
1593 if ret[0] is not None:
1594 cg, remote_heads = ret
1594 cg, remote_heads = ret
1595 if force: remote_heads = ['force']
1595 if force: remote_heads = ['force']
1596 return remote.unbundle(cg, remote_heads, 'push')
1596 return remote.unbundle(cg, remote_heads, 'push')
1597 return ret[1]
1597 return ret[1]
1598
1598
1599 def changegroupinfo(self, nodes, source):
1599 def changegroupinfo(self, nodes, source):
1600 if self.ui.verbose or source == 'bundle':
1600 if self.ui.verbose or source == 'bundle':
1601 self.ui.status(_("%d changesets found\n") % len(nodes))
1601 self.ui.status(_("%d changesets found\n") % len(nodes))
1602 if self.ui.debugflag:
1602 if self.ui.debugflag:
1603 self.ui.debug(_("list of changesets:\n"))
1603 self.ui.debug(_("list of changesets:\n"))
1604 for node in nodes:
1604 for node in nodes:
1605 self.ui.debug("%s\n" % hex(node))
1605 self.ui.debug("%s\n" % hex(node))
1606
1606
1607 def changegroupsubset(self, bases, heads, source, extranodes=None):
1607 def changegroupsubset(self, bases, heads, source, extranodes=None):
1608 """This function generates a changegroup consisting of all the nodes
1608 """This function generates a changegroup consisting of all the nodes
1609 that are descendents of any of the bases, and ancestors of any of
1609 that are descendents of any of the bases, and ancestors of any of
1610 the heads.
1610 the heads.
1611
1611
1612 It is fairly complex as determining which filenodes and which
1612 It is fairly complex as determining which filenodes and which
1613 manifest nodes need to be included for the changeset to be complete
1613 manifest nodes need to be included for the changeset to be complete
1614 is non-trivial.
1614 is non-trivial.
1615
1615
1616 Another wrinkle is doing the reverse, figuring out which changeset in
1616 Another wrinkle is doing the reverse, figuring out which changeset in
1617 the changegroup a particular filenode or manifestnode belongs to.
1617 the changegroup a particular filenode or manifestnode belongs to.
1618
1618
1619 The caller can specify some nodes that must be included in the
1619 The caller can specify some nodes that must be included in the
1620 changegroup using the extranodes argument. It should be a dict
1620 changegroup using the extranodes argument. It should be a dict
1621 where the keys are the filenames (or 1 for the manifest), and the
1621 where the keys are the filenames (or 1 for the manifest), and the
1622 values are lists of (node, linknode) tuples, where node is a wanted
1622 values are lists of (node, linknode) tuples, where node is a wanted
1623 node and linknode is the changelog node that should be transmitted as
1623 node and linknode is the changelog node that should be transmitted as
1624 the linkrev.
1624 the linkrev.
1625 """
1625 """
1626
1626
1627 if extranodes is None:
1627 if extranodes is None:
1628 # can we go through the fast path ?
1628 # can we go through the fast path ?
1629 heads.sort()
1629 heads.sort()
1630 allheads = self.heads()
1630 allheads = self.heads()
1631 allheads.sort()
1631 allheads.sort()
1632 if heads == allheads:
1632 if heads == allheads:
1633 common = []
1633 common = []
1634 # parents of bases are known from both sides
1634 # parents of bases are known from both sides
1635 for n in bases:
1635 for n in bases:
1636 for p in self.changelog.parents(n):
1636 for p in self.changelog.parents(n):
1637 if p != nullid:
1637 if p != nullid:
1638 common.append(p)
1638 common.append(p)
1639 return self._changegroup(common, source)
1639 return self._changegroup(common, source)
1640
1640
1641 self.hook('preoutgoing', throw=True, source=source)
1641 self.hook('preoutgoing', throw=True, source=source)
1642
1642
1643 # Set up some initial variables
1643 # Set up some initial variables
1644 # Make it easy to refer to self.changelog
1644 # Make it easy to refer to self.changelog
1645 cl = self.changelog
1645 cl = self.changelog
1646 # msng is short for missing - compute the list of changesets in this
1646 # msng is short for missing - compute the list of changesets in this
1647 # changegroup.
1647 # changegroup.
1648 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1648 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1649 self.changegroupinfo(msng_cl_lst, source)
1649 self.changegroupinfo(msng_cl_lst, source)
1650 # Some bases may turn out to be superfluous, and some heads may be
1650 # Some bases may turn out to be superfluous, and some heads may be
1651 # too. nodesbetween will return the minimal set of bases and heads
1651 # too. nodesbetween will return the minimal set of bases and heads
1652 # necessary to re-create the changegroup.
1652 # necessary to re-create the changegroup.
1653
1653
1654 # Known heads are the list of heads that it is assumed the recipient
1654 # Known heads are the list of heads that it is assumed the recipient
1655 # of this changegroup will know about.
1655 # of this changegroup will know about.
1656 knownheads = {}
1656 knownheads = {}
1657 # We assume that all parents of bases are known heads.
1657 # We assume that all parents of bases are known heads.
1658 for n in bases:
1658 for n in bases:
1659 for p in cl.parents(n):
1659 for p in cl.parents(n):
1660 if p != nullid:
1660 if p != nullid:
1661 knownheads[p] = 1
1661 knownheads[p] = 1
1662 knownheads = knownheads.keys()
1662 knownheads = knownheads.keys()
1663 if knownheads:
1663 if knownheads:
1664 # Now that we know what heads are known, we can compute which
1664 # Now that we know what heads are known, we can compute which
1665 # changesets are known. The recipient must know about all
1665 # changesets are known. The recipient must know about all
1666 # changesets required to reach the known heads from the null
1666 # changesets required to reach the known heads from the null
1667 # changeset.
1667 # changeset.
1668 has_cl_set, junk, junk = cl.nodesbetween(None, knownheads)
1668 has_cl_set, junk, junk = cl.nodesbetween(None, knownheads)
1669 junk = None
1669 junk = None
1670 # Transform the list into a set.
1670 # Transform the list into a set.
1671 has_cl_set = set(has_cl_set)
1671 has_cl_set = set(has_cl_set)
1672 else:
1672 else:
1673 # If there were no known heads, the recipient cannot be assumed to
1673 # If there were no known heads, the recipient cannot be assumed to
1674 # know about any changesets.
1674 # know about any changesets.
1675 has_cl_set = set()
1675 has_cl_set = set()
1676
1676
1677 # Make it easy to refer to self.manifest
1677 # Make it easy to refer to self.manifest
1678 mnfst = self.manifest
1678 mnfst = self.manifest
1679 # We don't know which manifests are missing yet
1679 # We don't know which manifests are missing yet
1680 msng_mnfst_set = {}
1680 msng_mnfst_set = {}
1681 # Nor do we know which filenodes are missing.
1681 # Nor do we know which filenodes are missing.
1682 msng_filenode_set = {}
1682 msng_filenode_set = {}
1683
1683
1684 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1684 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1685 junk = None
1685 junk = None
1686
1686
1687 # A changeset always belongs to itself, so the changenode lookup
1687 # A changeset always belongs to itself, so the changenode lookup
1688 # function for a changenode is identity.
1688 # function for a changenode is identity.
1689 def identity(x):
1689 def identity(x):
1690 return x
1690 return x
1691
1691
1692 # A function generating function. Sets up an environment for the
1692 # A function generating function. Sets up an environment for the
1693 # inner function.
1693 # inner function.
1694 def cmp_by_rev_func(revlog):
1694 def cmp_by_rev_func(revlog):
1695 # Compare two nodes by their revision number in the environment's
1695 # Compare two nodes by their revision number in the environment's
1696 # revision history. Since the revision number both represents the
1696 # revision history. Since the revision number both represents the
1697 # most efficient order to read the nodes in, and represents a
1697 # most efficient order to read the nodes in, and represents a
1698 # topological sorting of the nodes, this function is often useful.
1698 # topological sorting of the nodes, this function is often useful.
1699 def cmp_by_rev(a, b):
1699 def cmp_by_rev(a, b):
1700 return cmp(revlog.rev(a), revlog.rev(b))
1700 return cmp(revlog.rev(a), revlog.rev(b))
1701 return cmp_by_rev
1701 return cmp_by_rev
1702
1702
1703 # If we determine that a particular file or manifest node must be a
1703 # If we determine that a particular file or manifest node must be a
1704 # node that the recipient of the changegroup will already have, we can
1704 # node that the recipient of the changegroup will already have, we can
1705 # also assume the recipient will have all the parents. This function
1705 # also assume the recipient will have all the parents. This function
1706 # prunes them from the set of missing nodes.
1706 # prunes them from the set of missing nodes.
1707 def prune_parents(revlog, hasset, msngset):
1707 def prune_parents(revlog, hasset, msngset):
1708 haslst = hasset.keys()
1708 haslst = hasset.keys()
1709 haslst.sort(cmp_by_rev_func(revlog))
1709 haslst.sort(cmp_by_rev_func(revlog))
1710 for node in haslst:
1710 for node in haslst:
1711 parentlst = [p for p in revlog.parents(node) if p != nullid]
1711 parentlst = [p for p in revlog.parents(node) if p != nullid]
1712 while parentlst:
1712 while parentlst:
1713 n = parentlst.pop()
1713 n = parentlst.pop()
1714 if n not in hasset:
1714 if n not in hasset:
1715 hasset[n] = 1
1715 hasset[n] = 1
1716 p = [p for p in revlog.parents(n) if p != nullid]
1716 p = [p for p in revlog.parents(n) if p != nullid]
1717 parentlst.extend(p)
1717 parentlst.extend(p)
1718 for n in hasset:
1718 for n in hasset:
1719 msngset.pop(n, None)
1719 msngset.pop(n, None)
1720
1720
1721 # This is a function generating function used to set up an environment
1721 # This is a function generating function used to set up an environment
1722 # for the inner function to execute in.
1722 # for the inner function to execute in.
1723 def manifest_and_file_collector(changedfileset):
1723 def manifest_and_file_collector(changedfileset):
1724 # This is an information gathering function that gathers
1724 # This is an information gathering function that gathers
1725 # information from each changeset node that goes out as part of
1725 # information from each changeset node that goes out as part of
1726 # the changegroup. The information gathered is a list of which
1726 # the changegroup. The information gathered is a list of which
1727 # manifest nodes are potentially required (the recipient may
1727 # manifest nodes are potentially required (the recipient may
1728 # already have them) and total list of all files which were
1728 # already have them) and total list of all files which were
1729 # changed in any changeset in the changegroup.
1729 # changed in any changeset in the changegroup.
1730 #
1730 #
1731 # We also remember the first changenode we saw any manifest
1731 # We also remember the first changenode we saw any manifest
1732 # referenced by so we can later determine which changenode 'owns'
1732 # referenced by so we can later determine which changenode 'owns'
1733 # the manifest.
1733 # the manifest.
1734 def collect_manifests_and_files(clnode):
1734 def collect_manifests_and_files(clnode):
1735 c = cl.read(clnode)
1735 c = cl.read(clnode)
1736 for f in c[3]:
1736 for f in c[3]:
1737 # This is to make sure we only have one instance of each
1737 # This is to make sure we only have one instance of each
1738 # filename string for each filename.
1738 # filename string for each filename.
1739 changedfileset.setdefault(f, f)
1739 changedfileset.setdefault(f, f)
1740 msng_mnfst_set.setdefault(c[0], clnode)
1740 msng_mnfst_set.setdefault(c[0], clnode)
1741 return collect_manifests_and_files
1741 return collect_manifests_and_files
1742
1742
1743 # Figure out which manifest nodes (of the ones we think might be part
1743 # Figure out which manifest nodes (of the ones we think might be part
1744 # of the changegroup) the recipient must know about and remove them
1744 # of the changegroup) the recipient must know about and remove them
1745 # from the changegroup.
1745 # from the changegroup.
1746 def prune_manifests():
1746 def prune_manifests():
1747 has_mnfst_set = {}
1747 has_mnfst_set = {}
1748 for n in msng_mnfst_set:
1748 for n in msng_mnfst_set:
1749 # If a 'missing' manifest thinks it belongs to a changenode
1749 # If a 'missing' manifest thinks it belongs to a changenode
1750 # the recipient is assumed to have, obviously the recipient
1750 # the recipient is assumed to have, obviously the recipient
1751 # must have that manifest.
1751 # must have that manifest.
1752 linknode = cl.node(mnfst.linkrev(mnfst.rev(n)))
1752 linknode = cl.node(mnfst.linkrev(mnfst.rev(n)))
1753 if linknode in has_cl_set:
1753 if linknode in has_cl_set:
1754 has_mnfst_set[n] = 1
1754 has_mnfst_set[n] = 1
1755 prune_parents(mnfst, has_mnfst_set, msng_mnfst_set)
1755 prune_parents(mnfst, has_mnfst_set, msng_mnfst_set)
1756
1756
1757 # Use the information collected in collect_manifests_and_files to say
1757 # Use the information collected in collect_manifests_and_files to say
1758 # which changenode any manifestnode belongs to.
1758 # which changenode any manifestnode belongs to.
1759 def lookup_manifest_link(mnfstnode):
1759 def lookup_manifest_link(mnfstnode):
1760 return msng_mnfst_set[mnfstnode]
1760 return msng_mnfst_set[mnfstnode]
1761
1761
1762 # A function generating function that sets up the initial environment
1762 # A function generating function that sets up the initial environment
1763 # the inner function.
1763 # the inner function.
1764 def filenode_collector(changedfiles):
1764 def filenode_collector(changedfiles):
1765 next_rev = [0]
1765 next_rev = [0]
1766 # This gathers information from each manifestnode included in the
1766 # This gathers information from each manifestnode included in the
1767 # changegroup about which filenodes the manifest node references
1767 # changegroup about which filenodes the manifest node references
1768 # so we can include those in the changegroup too.
1768 # so we can include those in the changegroup too.
1769 #
1769 #
1770 # It also remembers which changenode each filenode belongs to. It
1770 # It also remembers which changenode each filenode belongs to. It
1771 # does this by assuming the a filenode belongs to the changenode
1771 # does this by assuming the a filenode belongs to the changenode
1772 # the first manifest that references it belongs to.
1772 # the first manifest that references it belongs to.
1773 def collect_msng_filenodes(mnfstnode):
1773 def collect_msng_filenodes(mnfstnode):
1774 r = mnfst.rev(mnfstnode)
1774 r = mnfst.rev(mnfstnode)
1775 if r == next_rev[0]:
1775 if r == next_rev[0]:
1776 # If the last rev we looked at was the one just previous,
1776 # If the last rev we looked at was the one just previous,
1777 # we only need to see a diff.
1777 # we only need to see a diff.
1778 deltamf = mnfst.readdelta(mnfstnode)
1778 deltamf = mnfst.readdelta(mnfstnode)
1779 # For each line in the delta
1779 # For each line in the delta
1780 for f, fnode in deltamf.iteritems():
1780 for f, fnode in deltamf.iteritems():
1781 f = changedfiles.get(f, None)
1781 f = changedfiles.get(f, None)
1782 # And if the file is in the list of files we care
1782 # And if the file is in the list of files we care
1783 # about.
1783 # about.
1784 if f is not None:
1784 if f is not None:
1785 # Get the changenode this manifest belongs to
1785 # Get the changenode this manifest belongs to
1786 clnode = msng_mnfst_set[mnfstnode]
1786 clnode = msng_mnfst_set[mnfstnode]
1787 # Create the set of filenodes for the file if
1787 # Create the set of filenodes for the file if
1788 # there isn't one already.
1788 # there isn't one already.
1789 ndset = msng_filenode_set.setdefault(f, {})
1789 ndset = msng_filenode_set.setdefault(f, {})
1790 # And set the filenode's changelog node to the
1790 # And set the filenode's changelog node to the
1791 # manifest's if it hasn't been set already.
1791 # manifest's if it hasn't been set already.
1792 ndset.setdefault(fnode, clnode)
1792 ndset.setdefault(fnode, clnode)
1793 else:
1793 else:
1794 # Otherwise we need a full manifest.
1794 # Otherwise we need a full manifest.
1795 m = mnfst.read(mnfstnode)
1795 m = mnfst.read(mnfstnode)
1796 # For every file in we care about.
1796 # For every file in we care about.
1797 for f in changedfiles:
1797 for f in changedfiles:
1798 fnode = m.get(f, None)
1798 fnode = m.get(f, None)
1799 # If it's in the manifest
1799 # If it's in the manifest
1800 if fnode is not None:
1800 if fnode is not None:
1801 # See comments above.
1801 # See comments above.
1802 clnode = msng_mnfst_set[mnfstnode]
1802 clnode = msng_mnfst_set[mnfstnode]
1803 ndset = msng_filenode_set.setdefault(f, {})
1803 ndset = msng_filenode_set.setdefault(f, {})
1804 ndset.setdefault(fnode, clnode)
1804 ndset.setdefault(fnode, clnode)
1805 # Remember the revision we hope to see next.
1805 # Remember the revision we hope to see next.
1806 next_rev[0] = r + 1
1806 next_rev[0] = r + 1
1807 return collect_msng_filenodes
1807 return collect_msng_filenodes
1808
1808
1809 # We have a list of filenodes we think we need for a file, lets remove
1809 # We have a list of filenodes we think we need for a file, lets remove
1810 # all those we know the recipient must have.
1810 # all those we know the recipient must have.
1811 def prune_filenodes(f, filerevlog):
1811 def prune_filenodes(f, filerevlog):
1812 msngset = msng_filenode_set[f]
1812 msngset = msng_filenode_set[f]
1813 hasset = {}
1813 hasset = {}
1814 # If a 'missing' filenode thinks it belongs to a changenode we
1814 # If a 'missing' filenode thinks it belongs to a changenode we
1815 # assume the recipient must have, then the recipient must have
1815 # assume the recipient must have, then the recipient must have
1816 # that filenode.
1816 # that filenode.
1817 for n in msngset:
1817 for n in msngset:
1818 clnode = cl.node(filerevlog.linkrev(filerevlog.rev(n)))
1818 clnode = cl.node(filerevlog.linkrev(filerevlog.rev(n)))
1819 if clnode in has_cl_set:
1819 if clnode in has_cl_set:
1820 hasset[n] = 1
1820 hasset[n] = 1
1821 prune_parents(filerevlog, hasset, msngset)
1821 prune_parents(filerevlog, hasset, msngset)
1822
1822
1823 # A function generator function that sets up the a context for the
1823 # A function generator function that sets up the a context for the
1824 # inner function.
1824 # inner function.
1825 def lookup_filenode_link_func(fname):
1825 def lookup_filenode_link_func(fname):
1826 msngset = msng_filenode_set[fname]
1826 msngset = msng_filenode_set[fname]
1827 # Lookup the changenode the filenode belongs to.
1827 # Lookup the changenode the filenode belongs to.
1828 def lookup_filenode_link(fnode):
1828 def lookup_filenode_link(fnode):
1829 return msngset[fnode]
1829 return msngset[fnode]
1830 return lookup_filenode_link
1830 return lookup_filenode_link
1831
1831
1832 # Add the nodes that were explicitly requested.
1832 # Add the nodes that were explicitly requested.
1833 def add_extra_nodes(name, nodes):
1833 def add_extra_nodes(name, nodes):
1834 if not extranodes or name not in extranodes:
1834 if not extranodes or name not in extranodes:
1835 return
1835 return
1836
1836
1837 for node, linknode in extranodes[name]:
1837 for node, linknode in extranodes[name]:
1838 if node not in nodes:
1838 if node not in nodes:
1839 nodes[node] = linknode
1839 nodes[node] = linknode
1840
1840
1841 # Now that we have all theses utility functions to help out and
1841 # Now that we have all theses utility functions to help out and
1842 # logically divide up the task, generate the group.
1842 # logically divide up the task, generate the group.
1843 def gengroup():
1843 def gengroup():
1844 # The set of changed files starts empty.
1844 # The set of changed files starts empty.
1845 changedfiles = {}
1845 changedfiles = {}
1846 # Create a changenode group generator that will call our functions
1846 # Create a changenode group generator that will call our functions
1847 # back to lookup the owning changenode and collect information.
1847 # back to lookup the owning changenode and collect information.
1848 group = cl.group(msng_cl_lst, identity,
1848 group = cl.group(msng_cl_lst, identity,
1849 manifest_and_file_collector(changedfiles))
1849 manifest_and_file_collector(changedfiles))
1850 for chnk in group:
1850 for chnk in group:
1851 yield chnk
1851 yield chnk
1852
1852
1853 # The list of manifests has been collected by the generator
1853 # The list of manifests has been collected by the generator
1854 # calling our functions back.
1854 # calling our functions back.
1855 prune_manifests()
1855 prune_manifests()
1856 add_extra_nodes(1, msng_mnfst_set)
1856 add_extra_nodes(1, msng_mnfst_set)
1857 msng_mnfst_lst = msng_mnfst_set.keys()
1857 msng_mnfst_lst = msng_mnfst_set.keys()
1858 # Sort the manifestnodes by revision number.
1858 # Sort the manifestnodes by revision number.
1859 msng_mnfst_lst.sort(cmp_by_rev_func(mnfst))
1859 msng_mnfst_lst.sort(cmp_by_rev_func(mnfst))
1860 # Create a generator for the manifestnodes that calls our lookup
1860 # Create a generator for the manifestnodes that calls our lookup
1861 # and data collection functions back.
1861 # and data collection functions back.
1862 group = mnfst.group(msng_mnfst_lst, lookup_manifest_link,
1862 group = mnfst.group(msng_mnfst_lst, lookup_manifest_link,
1863 filenode_collector(changedfiles))
1863 filenode_collector(changedfiles))
1864 for chnk in group:
1864 for chnk in group:
1865 yield chnk
1865 yield chnk
1866
1866
1867 # These are no longer needed, dereference and toss the memory for
1867 # These are no longer needed, dereference and toss the memory for
1868 # them.
1868 # them.
1869 msng_mnfst_lst = None
1869 msng_mnfst_lst = None
1870 msng_mnfst_set.clear()
1870 msng_mnfst_set.clear()
1871
1871
1872 if extranodes:
1872 if extranodes:
1873 for fname in extranodes:
1873 for fname in extranodes:
1874 if isinstance(fname, int):
1874 if isinstance(fname, int):
1875 continue
1875 continue
1876 msng_filenode_set.setdefault(fname, {})
1876 msng_filenode_set.setdefault(fname, {})
1877 changedfiles[fname] = 1
1877 changedfiles[fname] = 1
1878 # Go through all our files in order sorted by name.
1878 # Go through all our files in order sorted by name.
1879 for fname in sorted(changedfiles):
1879 for fname in sorted(changedfiles):
1880 filerevlog = self.file(fname)
1880 filerevlog = self.file(fname)
1881 if not len(filerevlog):
1881 if not len(filerevlog):
1882 raise util.Abort(_("empty or missing revlog for %s") % fname)
1882 raise util.Abort(_("empty or missing revlog for %s") % fname)
1883 # Toss out the filenodes that the recipient isn't really
1883 # Toss out the filenodes that the recipient isn't really
1884 # missing.
1884 # missing.
1885 if fname in msng_filenode_set:
1885 if fname in msng_filenode_set:
1886 prune_filenodes(fname, filerevlog)
1886 prune_filenodes(fname, filerevlog)
1887 add_extra_nodes(fname, msng_filenode_set[fname])
1887 add_extra_nodes(fname, msng_filenode_set[fname])
1888 msng_filenode_lst = msng_filenode_set[fname].keys()
1888 msng_filenode_lst = msng_filenode_set[fname].keys()
1889 else:
1889 else:
1890 msng_filenode_lst = []
1890 msng_filenode_lst = []
1891 # If any filenodes are left, generate the group for them,
1891 # If any filenodes are left, generate the group for them,
1892 # otherwise don't bother.
1892 # otherwise don't bother.
1893 if len(msng_filenode_lst) > 0:
1893 if len(msng_filenode_lst) > 0:
1894 yield changegroup.chunkheader(len(fname))
1894 yield changegroup.chunkheader(len(fname))
1895 yield fname
1895 yield fname
1896 # Sort the filenodes by their revision #
1896 # Sort the filenodes by their revision #
1897 msng_filenode_lst.sort(cmp_by_rev_func(filerevlog))
1897 msng_filenode_lst.sort(cmp_by_rev_func(filerevlog))
1898 # Create a group generator and only pass in a changenode
1898 # Create a group generator and only pass in a changenode
1899 # lookup function as we need to collect no information
1899 # lookup function as we need to collect no information
1900 # from filenodes.
1900 # from filenodes.
1901 group = filerevlog.group(msng_filenode_lst,
1901 group = filerevlog.group(msng_filenode_lst,
1902 lookup_filenode_link_func(fname))
1902 lookup_filenode_link_func(fname))
1903 for chnk in group:
1903 for chnk in group:
1904 yield chnk
1904 yield chnk
1905 if fname in msng_filenode_set:
1905 if fname in msng_filenode_set:
1906 # Don't need this anymore, toss it to free memory.
1906 # Don't need this anymore, toss it to free memory.
1907 del msng_filenode_set[fname]
1907 del msng_filenode_set[fname]
1908 # Signal that no more groups are left.
1908 # Signal that no more groups are left.
1909 yield changegroup.closechunk()
1909 yield changegroup.closechunk()
1910
1910
1911 if msng_cl_lst:
1911 if msng_cl_lst:
1912 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1912 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1913
1913
1914 return util.chunkbuffer(gengroup())
1914 return util.chunkbuffer(gengroup())
1915
1915
1916 def changegroup(self, basenodes, source):
1916 def changegroup(self, basenodes, source):
1917 # to avoid a race we use changegroupsubset() (issue1320)
1917 # to avoid a race we use changegroupsubset() (issue1320)
1918 return self.changegroupsubset(basenodes, self.heads(), source)
1918 return self.changegroupsubset(basenodes, self.heads(), source)
1919
1919
1920 def _changegroup(self, common, source):
1920 def _changegroup(self, common, source):
1921 """Generate a changegroup of all nodes that we have that a recipient
1921 """Generate a changegroup of all nodes that we have that a recipient
1922 doesn't.
1922 doesn't.
1923
1923
1924 This is much easier than the previous function as we can assume that
1924 This is much easier than the previous function as we can assume that
1925 the recipient has any changenode we aren't sending them.
1925 the recipient has any changenode we aren't sending them.
1926
1926
1927 common is the set of common nodes between remote and self"""
1927 common is the set of common nodes between remote and self"""
1928
1928
1929 self.hook('preoutgoing', throw=True, source=source)
1929 self.hook('preoutgoing', throw=True, source=source)
1930
1930
1931 cl = self.changelog
1931 cl = self.changelog
1932 nodes = cl.findmissing(common)
1932 nodes = cl.findmissing(common)
1933 revset = set([cl.rev(n) for n in nodes])
1933 revset = set([cl.rev(n) for n in nodes])
1934 self.changegroupinfo(nodes, source)
1934 self.changegroupinfo(nodes, source)
1935
1935
1936 def identity(x):
1936 def identity(x):
1937 return x
1937 return x
1938
1938
1939 def gennodelst(log):
1939 def gennodelst(log):
1940 for r in log:
1940 for r in log:
1941 if log.linkrev(r) in revset:
1941 if log.linkrev(r) in revset:
1942 yield log.node(r)
1942 yield log.node(r)
1943
1943
1944 def changed_file_collector(changedfileset):
1944 def changed_file_collector(changedfileset):
1945 def collect_changed_files(clnode):
1945 def collect_changed_files(clnode):
1946 c = cl.read(clnode)
1946 c = cl.read(clnode)
1947 for fname in c[3]:
1947 for fname in c[3]:
1948 changedfileset[fname] = 1
1948 changedfileset[fname] = 1
1949 return collect_changed_files
1949 return collect_changed_files
1950
1950
1951 def lookuprevlink_func(revlog):
1951 def lookuprevlink_func(revlog):
1952 def lookuprevlink(n):
1952 def lookuprevlink(n):
1953 return cl.node(revlog.linkrev(revlog.rev(n)))
1953 return cl.node(revlog.linkrev(revlog.rev(n)))
1954 return lookuprevlink
1954 return lookuprevlink
1955
1955
1956 def gengroup():
1956 def gengroup():
1957 # construct a list of all changed files
1957 # construct a list of all changed files
1958 changedfiles = {}
1958 changedfiles = {}
1959
1959
1960 for chnk in cl.group(nodes, identity,
1960 for chnk in cl.group(nodes, identity,
1961 changed_file_collector(changedfiles)):
1961 changed_file_collector(changedfiles)):
1962 yield chnk
1962 yield chnk
1963
1963
1964 mnfst = self.manifest
1964 mnfst = self.manifest
1965 nodeiter = gennodelst(mnfst)
1965 nodeiter = gennodelst(mnfst)
1966 for chnk in mnfst.group(nodeiter, lookuprevlink_func(mnfst)):
1966 for chnk in mnfst.group(nodeiter, lookuprevlink_func(mnfst)):
1967 yield chnk
1967 yield chnk
1968
1968
1969 for fname in sorted(changedfiles):
1969 for fname in sorted(changedfiles):
1970 filerevlog = self.file(fname)
1970 filerevlog = self.file(fname)
1971 if not len(filerevlog):
1971 if not len(filerevlog):
1972 raise util.Abort(_("empty or missing revlog for %s") % fname)
1972 raise util.Abort(_("empty or missing revlog for %s") % fname)
1973 nodeiter = gennodelst(filerevlog)
1973 nodeiter = gennodelst(filerevlog)
1974 nodeiter = list(nodeiter)
1974 nodeiter = list(nodeiter)
1975 if nodeiter:
1975 if nodeiter:
1976 yield changegroup.chunkheader(len(fname))
1976 yield changegroup.chunkheader(len(fname))
1977 yield fname
1977 yield fname
1978 lookup = lookuprevlink_func(filerevlog)
1978 lookup = lookuprevlink_func(filerevlog)
1979 for chnk in filerevlog.group(nodeiter, lookup):
1979 for chnk in filerevlog.group(nodeiter, lookup):
1980 yield chnk
1980 yield chnk
1981
1981
1982 yield changegroup.closechunk()
1982 yield changegroup.closechunk()
1983
1983
1984 if nodes:
1984 if nodes:
1985 self.hook('outgoing', node=hex(nodes[0]), source=source)
1985 self.hook('outgoing', node=hex(nodes[0]), source=source)
1986
1986
1987 return util.chunkbuffer(gengroup())
1987 return util.chunkbuffer(gengroup())
1988
1988
1989 def addchangegroup(self, source, srctype, url, emptyok=False):
1989 def addchangegroup(self, source, srctype, url, emptyok=False):
1990 """add changegroup to repo.
1990 """add changegroup to repo.
1991
1991
1992 return values:
1992 return values:
1993 - nothing changed or no source: 0
1993 - nothing changed or no source: 0
1994 - more heads than before: 1+added heads (2..n)
1994 - more heads than before: 1+added heads (2..n)
1995 - less heads than before: -1-removed heads (-2..-n)
1995 - less heads than before: -1-removed heads (-2..-n)
1996 - number of heads stays the same: 1
1996 - number of heads stays the same: 1
1997 """
1997 """
1998 def csmap(x):
1998 def csmap(x):
1999 self.ui.debug(_("add changeset %s\n") % short(x))
1999 self.ui.debug(_("add changeset %s\n") % short(x))
2000 return len(cl)
2000 return len(cl)
2001
2001
2002 def revmap(x):
2002 def revmap(x):
2003 return cl.rev(x)
2003 return cl.rev(x)
2004
2004
2005 if not source:
2005 if not source:
2006 return 0
2006 return 0
2007
2007
2008 self.hook('prechangegroup', throw=True, source=srctype, url=url)
2008 self.hook('prechangegroup', throw=True, source=srctype, url=url)
2009
2009
2010 changesets = files = revisions = 0
2010 changesets = files = revisions = 0
2011
2011
2012 # write changelog data to temp files so concurrent readers will not see
2012 # write changelog data to temp files so concurrent readers will not see
2013 # inconsistent view
2013 # inconsistent view
2014 cl = self.changelog
2014 cl = self.changelog
2015 cl.delayupdate()
2015 cl.delayupdate()
2016 oldheads = len(cl.heads())
2016 oldheads = len(cl.heads())
2017
2017
2018 tr = self.transaction()
2018 tr = self.transaction()
2019 try:
2019 try:
2020 trp = weakref.proxy(tr)
2020 trp = weakref.proxy(tr)
2021 # pull off the changeset group
2021 # pull off the changeset group
2022 self.ui.status(_("adding changesets\n"))
2022 self.ui.status(_("adding changesets\n"))
2023 cor = len(cl) - 1
2023 cor = len(cl) - 1
2024 chunkiter = changegroup.chunkiter(source)
2024 chunkiter = changegroup.chunkiter(source)
2025 if cl.addgroup(chunkiter, csmap, trp) is None and not emptyok:
2025 if cl.addgroup(chunkiter, csmap, trp) is None and not emptyok:
2026 raise util.Abort(_("received changelog group is empty"))
2026 raise util.Abort(_("received changelog group is empty"))
2027 cnr = len(cl) - 1
2027 cnr = len(cl) - 1
2028 changesets = cnr - cor
2028 changesets = cnr - cor
2029
2029
2030 # pull off the manifest group
2030 # pull off the manifest group
2031 self.ui.status(_("adding manifests\n"))
2031 self.ui.status(_("adding manifests\n"))
2032 chunkiter = changegroup.chunkiter(source)
2032 chunkiter = changegroup.chunkiter(source)
2033 # no need to check for empty manifest group here:
2033 # no need to check for empty manifest group here:
2034 # if the result of the merge of 1 and 2 is the same in 3 and 4,
2034 # if the result of the merge of 1 and 2 is the same in 3 and 4,
2035 # no new manifest will be created and the manifest group will
2035 # no new manifest will be created and the manifest group will
2036 # be empty during the pull
2036 # be empty during the pull
2037 self.manifest.addgroup(chunkiter, revmap, trp)
2037 self.manifest.addgroup(chunkiter, revmap, trp)
2038
2038
2039 # process the files
2039 # process the files
2040 self.ui.status(_("adding file changes\n"))
2040 self.ui.status(_("adding file changes\n"))
2041 while 1:
2041 while 1:
2042 f = changegroup.getchunk(source)
2042 f = changegroup.getchunk(source)
2043 if not f:
2043 if not f:
2044 break
2044 break
2045 self.ui.debug(_("adding %s revisions\n") % f)
2045 self.ui.debug(_("adding %s revisions\n") % f)
2046 fl = self.file(f)
2046 fl = self.file(f)
2047 o = len(fl)
2047 o = len(fl)
2048 chunkiter = changegroup.chunkiter(source)
2048 chunkiter = changegroup.chunkiter(source)
2049 if fl.addgroup(chunkiter, revmap, trp) is None:
2049 if fl.addgroup(chunkiter, revmap, trp) is None:
2050 raise util.Abort(_("received file revlog group is empty"))
2050 raise util.Abort(_("received file revlog group is empty"))
2051 revisions += len(fl) - o
2051 revisions += len(fl) - o
2052 files += 1
2052 files += 1
2053
2053
2054 newheads = len(self.changelog.heads())
2054 newheads = len(self.changelog.heads())
2055 heads = ""
2055 heads = ""
2056 if oldheads and newheads != oldheads:
2056 if oldheads and newheads != oldheads:
2057 heads = _(" (%+d heads)") % (newheads - oldheads)
2057 heads = _(" (%+d heads)") % (newheads - oldheads)
2058
2058
2059 self.ui.status(_("added %d changesets"
2059 self.ui.status(_("added %d changesets"
2060 " with %d changes to %d files%s\n")
2060 " with %d changes to %d files%s\n")
2061 % (changesets, revisions, files, heads))
2061 % (changesets, revisions, files, heads))
2062
2062
2063 if changesets > 0:
2063 if changesets > 0:
2064 p = lambda: self.changelog.writepending() and self.root or ""
2064 p = lambda: self.changelog.writepending() and self.root or ""
2065 self.hook('pretxnchangegroup', throw=True,
2065 self.hook('pretxnchangegroup', throw=True,
2066 node=hex(self.changelog.node(cor+1)), source=srctype,
2066 node=hex(self.changelog.node(cor+1)), source=srctype,
2067 url=url, pending=p)
2067 url=url, pending=p)
2068
2068
2069 # make changelog see real files again
2069 # make changelog see real files again
2070 cl.finalize(trp)
2070 cl.finalize(trp)
2071
2071
2072 tr.close()
2072 tr.close()
2073 finally:
2073 finally:
2074 del tr
2074 del tr
2075
2075
2076 if changesets > 0:
2076 if changesets > 0:
2077 # forcefully update the on-disk branch cache
2077 # forcefully update the on-disk branch cache
2078 self.ui.debug(_("updating the branch cache\n"))
2078 self.ui.debug(_("updating the branch cache\n"))
2079 self.branchtags()
2079 self.branchtags()
2080 self.hook("changegroup", node=hex(self.changelog.node(cor+1)),
2080 self.hook("changegroup", node=hex(self.changelog.node(cor+1)),
2081 source=srctype, url=url)
2081 source=srctype, url=url)
2082
2082
2083 for i in xrange(cor + 1, cnr + 1):
2083 for i in xrange(cor + 1, cnr + 1):
2084 self.hook("incoming", node=hex(self.changelog.node(i)),
2084 self.hook("incoming", node=hex(self.changelog.node(i)),
2085 source=srctype, url=url)
2085 source=srctype, url=url)
2086
2086
2087 # never return 0 here:
2087 # never return 0 here:
2088 if newheads < oldheads:
2088 if newheads < oldheads:
2089 return newheads - oldheads - 1
2089 return newheads - oldheads - 1
2090 else:
2090 else:
2091 return newheads - oldheads + 1
2091 return newheads - oldheads + 1
2092
2092
2093
2093
2094 def stream_in(self, remote):
2094 def stream_in(self, remote):
2095 fp = remote.stream_out()
2095 fp = remote.stream_out()
2096 l = fp.readline()
2096 l = fp.readline()
2097 try:
2097 try:
2098 resp = int(l)
2098 resp = int(l)
2099 except ValueError:
2099 except ValueError:
2100 raise error.ResponseError(
2100 raise error.ResponseError(
2101 _('Unexpected response from remote server:'), l)
2101 _('Unexpected response from remote server:'), l)
2102 if resp == 1:
2102 if resp == 1:
2103 raise util.Abort(_('operation forbidden by server'))
2103 raise util.Abort(_('operation forbidden by server'))
2104 elif resp == 2:
2104 elif resp == 2:
2105 raise util.Abort(_('locking the remote repository failed'))
2105 raise util.Abort(_('locking the remote repository failed'))
2106 elif resp != 0:
2106 elif resp != 0:
2107 raise util.Abort(_('the server sent an unknown error code'))
2107 raise util.Abort(_('the server sent an unknown error code'))
2108 self.ui.status(_('streaming all changes\n'))
2108 self.ui.status(_('streaming all changes\n'))
2109 l = fp.readline()
2109 l = fp.readline()
2110 try:
2110 try:
2111 total_files, total_bytes = map(int, l.split(' ', 1))
2111 total_files, total_bytes = map(int, l.split(' ', 1))
2112 except (ValueError, TypeError):
2112 except (ValueError, TypeError):
2113 raise error.ResponseError(
2113 raise error.ResponseError(
2114 _('Unexpected response from remote server:'), l)
2114 _('Unexpected response from remote server:'), l)
2115 self.ui.status(_('%d files to transfer, %s of data\n') %
2115 self.ui.status(_('%d files to transfer, %s of data\n') %
2116 (total_files, util.bytecount(total_bytes)))
2116 (total_files, util.bytecount(total_bytes)))
2117 start = time.time()
2117 start = time.time()
2118 for i in xrange(total_files):
2118 for i in xrange(total_files):
2119 # XXX doesn't support '\n' or '\r' in filenames
2119 # XXX doesn't support '\n' or '\r' in filenames
2120 l = fp.readline()
2120 l = fp.readline()
2121 try:
2121 try:
2122 name, size = l.split('\0', 1)
2122 name, size = l.split('\0', 1)
2123 size = int(size)
2123 size = int(size)
2124 except (ValueError, TypeError):
2124 except (ValueError, TypeError):
2125 raise error.ResponseError(
2125 raise error.ResponseError(
2126 _('Unexpected response from remote server:'), l)
2126 _('Unexpected response from remote server:'), l)
2127 self.ui.debug(_('adding %s (%s)\n') % (name, util.bytecount(size)))
2127 self.ui.debug(_('adding %s (%s)\n') % (name, util.bytecount(size)))
2128 ofp = self.sopener(name, 'w')
2128 ofp = self.sopener(name, 'w')
2129 for chunk in util.filechunkiter(fp, limit=size):
2129 for chunk in util.filechunkiter(fp, limit=size):
2130 ofp.write(chunk)
2130 ofp.write(chunk)
2131 ofp.close()
2131 ofp.close()
2132 elapsed = time.time() - start
2132 elapsed = time.time() - start
2133 if elapsed <= 0:
2133 if elapsed <= 0:
2134 elapsed = 0.001
2134 elapsed = 0.001
2135 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
2135 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
2136 (util.bytecount(total_bytes), elapsed,
2136 (util.bytecount(total_bytes), elapsed,
2137 util.bytecount(total_bytes / elapsed)))
2137 util.bytecount(total_bytes / elapsed)))
2138 self.invalidate()
2138 self.invalidate()
2139 return len(self.heads()) + 1
2139 return len(self.heads()) + 1
2140
2140
2141 def clone(self, remote, heads=[], stream=False):
2141 def clone(self, remote, heads=[], stream=False):
2142 '''clone remote repository.
2142 '''clone remote repository.
2143
2143
2144 keyword arguments:
2144 keyword arguments:
2145 heads: list of revs to clone (forces use of pull)
2145 heads: list of revs to clone (forces use of pull)
2146 stream: use streaming clone if possible'''
2146 stream: use streaming clone if possible'''
2147
2147
2148 # now, all clients that can request uncompressed clones can
2148 # now, all clients that can request uncompressed clones can
2149 # read repo formats supported by all servers that can serve
2149 # read repo formats supported by all servers that can serve
2150 # them.
2150 # them.
2151
2151
2152 # if revlog format changes, client will have to check version
2152 # if revlog format changes, client will have to check version
2153 # and format flags on "stream" capability, and use
2153 # and format flags on "stream" capability, and use
2154 # uncompressed only if compatible.
2154 # uncompressed only if compatible.
2155
2155
2156 if stream and not heads and remote.capable('stream'):
2156 if stream and not heads and remote.capable('stream'):
2157 return self.stream_in(remote)
2157 return self.stream_in(remote)
2158 return self.pull(remote, heads)
2158 return self.pull(remote, heads)
2159
2159
2160 # used to avoid circular references so destructors work
2160 # used to avoid circular references so destructors work
2161 def aftertrans(files):
2161 def aftertrans(files):
2162 renamefiles = [tuple(t) for t in files]
2162 renamefiles = [tuple(t) for t in files]
2163 def a():
2163 def a():
2164 for src, dest in renamefiles:
2164 for src, dest in renamefiles:
2165 util.rename(src, dest)
2165 util.rename(src, dest)
2166 return a
2166 return a
2167
2167
2168 def instance(ui, path, create):
2168 def instance(ui, path, create):
2169 return localrepository(ui, util.drop_scheme('file', path), create)
2169 return localrepository(ui, util.drop_scheme('file', path), create)
2170
2170
2171 def islocal(path):
2171 def islocal(path):
2172 return True
2172 return True
@@ -1,140 +1,138
1 # transaction.py - simple journalling scheme for mercurial
1 # transaction.py - simple journalling scheme for mercurial
2 #
2 #
3 # This transaction scheme is intended to gracefully handle program
3 # This transaction scheme is intended to gracefully handle program
4 # errors and interruptions. More serious failures like system crashes
4 # errors and interruptions. More serious failures like system crashes
5 # can be recovered with an fsck-like tool. As the whole repository is
5 # can be recovered with an fsck-like tool. As the whole repository is
6 # effectively log-structured, this should amount to simply truncating
6 # effectively log-structured, this should amount to simply truncating
7 # anything that isn't referenced in the changelog.
7 # anything that isn't referenced in the changelog.
8 #
8 #
9 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
9 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
10 #
10 #
11 # This software may be used and distributed according to the terms of the
11 # This software may be used and distributed according to the terms of the
12 # GNU General Public License version 2, incorporated herein by reference.
12 # GNU General Public License version 2, incorporated herein by reference.
13
13
14 from i18n import _
14 from i18n import _
15 import os, errno
15 import os, errno
16 import error
16 import error
17
17
18 def active(func):
18 def active(func):
19 def _active(self, *args, **kwds):
19 def _active(self, *args, **kwds):
20 if self.count == 0:
20 if self.count == 0:
21 raise error.Abort(_(
21 raise error.Abort(_(
22 'cannot use transaction when it is already committed/aborted'))
22 'cannot use transaction when it is already committed/aborted'))
23 return func(self, *args, **kwds)
23 return func(self, *args, **kwds)
24 return _active
24 return _active
25
25
26 def _playback(journal, report, opener, entries, unlink=True):
27 for f, o, ignore in entries:
28 if o or not unlink:
29 try:
30 opener(f, 'a').truncate(o)
31 except:
32 report(_("failed to truncate %s\n") % f)
33 raise
34 else:
35 try:
36 fn = opener(f).name
37 os.unlink(fn)
38 except OSError, inst:
39 if inst.errno != errno.ENOENT:
40 raise
41 os.unlink(journal)
42
26 class transaction(object):
43 class transaction(object):
27 def __init__(self, report, opener, journal, after=None, createmode=None):
44 def __init__(self, report, opener, journal, after=None, createmode=None):
28 self.journal = None
45 self.journal = None
29
46
30 self.count = 1
47 self.count = 1
31 self.report = report
48 self.report = report
32 self.opener = opener
49 self.opener = opener
33 self.after = after
50 self.after = after
34 self.entries = []
51 self.entries = []
35 self.map = {}
52 self.map = {}
36 self.journal = journal
53 self.journal = journal
37
54
38 self.file = open(self.journal, "w")
55 self.file = open(self.journal, "w")
39 if createmode is not None:
56 if createmode is not None:
40 os.chmod(self.journal, createmode & 0666)
57 os.chmod(self.journal, createmode & 0666)
41
58
42 def __del__(self):
59 def __del__(self):
43 if self.journal:
60 if self.journal:
44 if self.entries: self._abort()
61 if self.entries: self._abort()
45 self.file.close()
62 self.file.close()
46
63
47 @active
64 @active
48 def add(self, file, offset, data=None):
65 def add(self, file, offset, data=None):
49 if file in self.map: return
66 if file in self.map: return
50 self.entries.append((file, offset, data))
67 self.entries.append((file, offset, data))
51 self.map[file] = len(self.entries) - 1
68 self.map[file] = len(self.entries) - 1
52 # add enough data to the journal to do the truncate
69 # add enough data to the journal to do the truncate
53 self.file.write("%s\0%d\n" % (file, offset))
70 self.file.write("%s\0%d\n" % (file, offset))
54 self.file.flush()
71 self.file.flush()
55
72
56 @active
73 @active
57 def find(self, file):
74 def find(self, file):
58 if file in self.map:
75 if file in self.map:
59 return self.entries[self.map[file]]
76 return self.entries[self.map[file]]
60 return None
77 return None
61
78
62 @active
79 @active
63 def replace(self, file, offset, data=None):
80 def replace(self, file, offset, data=None):
64 if file not in self.map:
81 if file not in self.map:
65 raise KeyError(file)
82 raise KeyError(file)
66 index = self.map[file]
83 index = self.map[file]
67 self.entries[index] = (file, offset, data)
84 self.entries[index] = (file, offset, data)
68 self.file.write("%s\0%d\n" % (file, offset))
85 self.file.write("%s\0%d\n" % (file, offset))
69 self.file.flush()
86 self.file.flush()
70
87
71 @active
88 @active
72 def nest(self):
89 def nest(self):
73 self.count += 1
90 self.count += 1
74 return self
91 return self
75
92
76 def running(self):
93 def running(self):
77 return self.count > 0
94 return self.count > 0
78
95
79 @active
96 @active
80 def close(self):
97 def close(self):
81 self.count -= 1
98 self.count -= 1
82 if self.count != 0:
99 if self.count != 0:
83 return
100 return
84 self.file.close()
101 self.file.close()
85 self.entries = []
102 self.entries = []
86 if self.after:
103 if self.after:
87 self.after()
104 self.after()
88 else:
105 else:
89 os.unlink(self.journal)
106 os.unlink(self.journal)
90 self.journal = None
107 self.journal = None
91
108
92 @active
109 @active
93 def abort(self):
110 def abort(self):
94 self._abort()
111 self._abort()
95
112
96 def _abort(self):
113 def _abort(self):
97 self.count = 0
114 self.count = 0
98 self.file.close()
115 self.file.close()
99
116
100 if not self.entries: return
117 if not self.entries: return
101
118
102 self.report(_("transaction abort!\n"))
119 self.report(_("transaction abort!\n"))
103
120
104 failed = False
121 try:
105 for f, o, ignore in self.entries:
106 try:
122 try:
107 self.opener(f, "a").truncate(o)
123 _playback(self.journal, self.report, self.opener, self.entries, False)
124 self.report(_("rollback completed\n"))
108 except:
125 except:
109 failed = True
110 self.report(_("failed to truncate %s\n") % f)
111
112 self.entries = []
113
114 if not failed:
115 os.unlink(self.journal)
116 self.report(_("rollback completed\n"))
117 else:
118 self.report(_("rollback failed - please run hg recover\n"))
126 self.report(_("rollback failed - please run hg recover\n"))
119
127 finally:
120 self.journal = None
128 self.journal = None
121
129
122
130
123 def rollback(opener, file):
131 def rollback(opener, file, report):
124 files = {}
132 entries = []
133
125 for l in open(file).readlines():
134 for l in open(file).readlines():
126 f, o = l.split('\0')
135 f, o = l.split('\0')
127 files[f] = int(o)
136 entries.append((f, int(o), None))
128 for f in files:
129 o = files[f]
130 if o:
131 opener(f, "a").truncate(int(o))
132 else:
133 try:
134 fn = opener(f).name
135 os.unlink(fn)
136 except OSError, inst:
137 if inst.errno != errno.ENOENT:
138 raise
139 os.unlink(file)
140
137
138 _playback(file, report, opener, entries)
@@ -1,85 +1,86
1 % before update 0, strip 2
1 % before update 0, strip 2
2 changeset: 0:cb9a9f314b8b
2 changeset: 0:cb9a9f314b8b
3 user: test
3 user: test
4 date: Thu Jan 01 00:00:00 1970 +0000
4 date: Thu Jan 01 00:00:00 1970 +0000
5 summary: a
5 summary: a
6
6
7 saving bundle to
7 saving bundle to
8 transaction abort!
8 transaction abort!
9 failed to truncate data/b.i
9 failed to truncate data/b.i
10 rollback failed - please run hg recover
10 rollback failed - please run hg recover
11 abort: Permission denied .hg/store/data/b.i
11 abort: Permission denied .hg/store/data/b.i
12 % after update 0, strip 2
12 % after update 0, strip 2
13 checking changesets
13 checking changesets
14 checking manifests
14 checking manifests
15 crosschecking files in changesets and manifests
15 crosschecking files in changesets and manifests
16 checking files
16 checking files
17 b@?: rev 1 points to nonexistent changeset 2
17 b@?: rev 1 points to nonexistent changeset 2
18 (expected 1)
18 (expected 1)
19 b@?: 736c29771fba not in manifests
19 b@?: 736c29771fba not in manifests
20 warning: orphan revlog 'data/c.i'
20 2 files, 2 changesets, 3 total revisions
21 2 files, 2 changesets, 3 total revisions
21 1 warnings encountered!
22 2 warnings encountered!
22 2 integrity errors encountered!
23 2 integrity errors encountered!
23 % journal contents
24 % journal contents
24 00changelog.i
25 00changelog.i
25 00manifest.i
26 00manifest.i
26 data/b.i
27 data/b.i
27 data/c.i
28 data/c.i
28 rolling back interrupted transaction
29 rolling back interrupted transaction
29 checking changesets
30 checking changesets
30 checking manifests
31 checking manifests
31 crosschecking files in changesets and manifests
32 crosschecking files in changesets and manifests
32 checking files
33 checking files
33 2 files, 2 changesets, 2 total revisions
34 2 files, 2 changesets, 2 total revisions
34 % before update 0, strip 2
35 % before update 0, strip 2
35 changeset: 0:cb9a9f314b8b
36 changeset: 0:cb9a9f314b8b
36 user: test
37 user: test
37 date: Thu Jan 01 00:00:00 1970 +0000
38 date: Thu Jan 01 00:00:00 1970 +0000
38 summary: a
39 summary: a
39
40
40 abort: Permission denied .hg/store/data/b.i
41 abort: Permission denied .hg/store/data/b.i
41 % after update 0, strip 2
42 % after update 0, strip 2
42 checking changesets
43 checking changesets
43 checking manifests
44 checking manifests
44 crosschecking files in changesets and manifests
45 crosschecking files in changesets and manifests
45 checking files
46 checking files
46 3 files, 4 changesets, 4 total revisions
47 3 files, 4 changesets, 4 total revisions
47 % journal contents
48 % journal contents
48 cat: .hg/store/journal: No such file or directory
49 cat: .hg/store/journal: No such file or directory
49 % before update 0, strip 2
50 % before update 0, strip 2
50 changeset: 0:cb9a9f314b8b
51 changeset: 0:cb9a9f314b8b
51 user: test
52 user: test
52 date: Thu Jan 01 00:00:00 1970 +0000
53 date: Thu Jan 01 00:00:00 1970 +0000
53 summary: a
54 summary: a
54
55
55 saving bundle to
56 saving bundle to
56 transaction abort!
57 transaction abort!
57 failed to truncate 00manifest.i
58 failed to truncate 00manifest.i
58 rollback failed - please run hg recover
59 rollback failed - please run hg recover
59 abort: Permission denied .hg/store/00manifest.i
60 abort: Permission denied .hg/store/00manifest.i
60 % after update 0, strip 2
61 % after update 0, strip 2
61 checking changesets
62 checking changesets
62 checking manifests
63 checking manifests
63 manifest@?: rev 2 points to nonexistent changeset 2
64 manifest@?: rev 2 points to nonexistent changeset 2
64 manifest@?: rev 3 points to nonexistent changeset 3
65 manifest@?: rev 3 points to nonexistent changeset 3
65 crosschecking files in changesets and manifests
66 crosschecking files in changesets and manifests
66 c@?: in manifest but not in changeset
67 c@3: in manifest but not in changeset
67 checking files
68 checking files
68 b@2: 736c29771fba in manifests not found
69 b@?: rev 1 points to nonexistent changeset 2
69 data/c.i@?: missing revlog!
70 (expected 1)
70 ?: empty or missing c
71 c@?: rev 0 points to nonexistent changeset 3
71 c@3: 149da44f2a4e in manifests not found
72 3 files, 2 changesets, 4 total revisions
72 3 files, 2 changesets, 2 total revisions
73 1 warnings encountered!
73 7 integrity errors encountered!
74 5 integrity errors encountered!
74 (first damaged changeset appears to be 2)
75 (first damaged changeset appears to be 3)
75 % journal contents
76 % journal contents
76 00changelog.i
77 00changelog.i
77 00manifest.i
78 00manifest.i
78 data/b.i
79 data/b.i
79 data/c.i
80 data/c.i
80 rolling back interrupted transaction
81 rolling back interrupted transaction
81 checking changesets
82 checking changesets
82 checking manifests
83 checking manifests
83 crosschecking files in changesets and manifests
84 crosschecking files in changesets and manifests
84 checking files
85 checking files
85 2 files, 2 changesets, 2 total revisions
86 2 files, 2 changesets, 2 total revisions
General Comments 0
You need to be logged in to leave comments. Login now