##// END OF EJS Templates
localrepo: refactor retrieving of journal/undo files paths...
Idan Kamara -
r16236:97efd26e default
parent child Browse files
Show More
@@ -1,2344 +1,2348 b''
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 or any later version.
6 # GNU General Public License version 2 or any later version.
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, subrepo, discovery, pushkey
10 import repo, changegroup, subrepo, discovery, pushkey
11 import changelog, dirstate, filelog, manifest, context, bookmarks, phases
11 import changelog, dirstate, filelog, manifest, context, bookmarks, phases
12 import lock, transaction, store, encoding
12 import lock, transaction, store, encoding
13 import scmutil, util, extensions, hook, error, revset
13 import scmutil, util, extensions, hook, error, revset
14 import match as matchmod
14 import match as matchmod
15 import merge as mergemod
15 import merge as mergemod
16 import tags as tagsmod
16 import tags as tagsmod
17 from lock import release
17 from lock import release
18 import weakref, errno, os, time, inspect
18 import weakref, errno, os, time, inspect
19 propertycache = util.propertycache
19 propertycache = util.propertycache
20 filecache = scmutil.filecache
20 filecache = scmutil.filecache
21
21
22 class storecache(filecache):
22 class storecache(filecache):
23 """filecache for files in the store"""
23 """filecache for files in the store"""
24 def join(self, obj, fname):
24 def join(self, obj, fname):
25 return obj.sjoin(fname)
25 return obj.sjoin(fname)
26
26
27 class localrepository(repo.repository):
27 class localrepository(repo.repository):
28 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey',
28 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey',
29 'known', 'getbundle'))
29 'known', 'getbundle'))
30 supportedformats = set(('revlogv1', 'generaldelta'))
30 supportedformats = set(('revlogv1', 'generaldelta'))
31 supported = supportedformats | set(('store', 'fncache', 'shared',
31 supported = supportedformats | set(('store', 'fncache', 'shared',
32 'dotencode'))
32 'dotencode'))
33
33
34 def __init__(self, baseui, path=None, create=False):
34 def __init__(self, baseui, path=None, create=False):
35 repo.repository.__init__(self)
35 repo.repository.__init__(self)
36 self.root = os.path.realpath(util.expandpath(path))
36 self.root = os.path.realpath(util.expandpath(path))
37 self.path = os.path.join(self.root, ".hg")
37 self.path = os.path.join(self.root, ".hg")
38 self.origroot = path
38 self.origroot = path
39 self.auditor = scmutil.pathauditor(self.root, self._checknested)
39 self.auditor = scmutil.pathauditor(self.root, self._checknested)
40 self.opener = scmutil.opener(self.path)
40 self.opener = scmutil.opener(self.path)
41 self.wopener = scmutil.opener(self.root)
41 self.wopener = scmutil.opener(self.root)
42 self.baseui = baseui
42 self.baseui = baseui
43 self.ui = baseui.copy()
43 self.ui = baseui.copy()
44 self._dirtyphases = False
44 self._dirtyphases = False
45 # A list of callback to shape the phase if no data were found.
45 # A list of callback to shape the phase if no data were found.
46 # Callback are in the form: func(repo, roots) --> processed root.
46 # Callback are in the form: func(repo, roots) --> processed root.
47 # This list it to be filled by extension during repo setup
47 # This list it to be filled by extension during repo setup
48 self._phasedefaults = []
48 self._phasedefaults = []
49
49
50 try:
50 try:
51 self.ui.readconfig(self.join("hgrc"), self.root)
51 self.ui.readconfig(self.join("hgrc"), self.root)
52 extensions.loadall(self.ui)
52 extensions.loadall(self.ui)
53 except IOError:
53 except IOError:
54 pass
54 pass
55
55
56 if not os.path.isdir(self.path):
56 if not os.path.isdir(self.path):
57 if create:
57 if create:
58 if not os.path.exists(path):
58 if not os.path.exists(path):
59 util.makedirs(path)
59 util.makedirs(path)
60 util.makedir(self.path, notindexed=True)
60 util.makedir(self.path, notindexed=True)
61 requirements = ["revlogv1"]
61 requirements = ["revlogv1"]
62 if self.ui.configbool('format', 'usestore', True):
62 if self.ui.configbool('format', 'usestore', True):
63 os.mkdir(os.path.join(self.path, "store"))
63 os.mkdir(os.path.join(self.path, "store"))
64 requirements.append("store")
64 requirements.append("store")
65 if self.ui.configbool('format', 'usefncache', True):
65 if self.ui.configbool('format', 'usefncache', True):
66 requirements.append("fncache")
66 requirements.append("fncache")
67 if self.ui.configbool('format', 'dotencode', True):
67 if self.ui.configbool('format', 'dotencode', True):
68 requirements.append('dotencode')
68 requirements.append('dotencode')
69 # create an invalid changelog
69 # create an invalid changelog
70 self.opener.append(
70 self.opener.append(
71 "00changelog.i",
71 "00changelog.i",
72 '\0\0\0\2' # represents revlogv2
72 '\0\0\0\2' # represents revlogv2
73 ' dummy changelog to prevent using the old repo layout'
73 ' dummy changelog to prevent using the old repo layout'
74 )
74 )
75 if self.ui.configbool('format', 'generaldelta', False):
75 if self.ui.configbool('format', 'generaldelta', False):
76 requirements.append("generaldelta")
76 requirements.append("generaldelta")
77 requirements = set(requirements)
77 requirements = set(requirements)
78 else:
78 else:
79 raise error.RepoError(_("repository %s not found") % path)
79 raise error.RepoError(_("repository %s not found") % path)
80 elif create:
80 elif create:
81 raise error.RepoError(_("repository %s already exists") % path)
81 raise error.RepoError(_("repository %s already exists") % path)
82 else:
82 else:
83 try:
83 try:
84 requirements = scmutil.readrequires(self.opener, self.supported)
84 requirements = scmutil.readrequires(self.opener, self.supported)
85 except IOError, inst:
85 except IOError, inst:
86 if inst.errno != errno.ENOENT:
86 if inst.errno != errno.ENOENT:
87 raise
87 raise
88 requirements = set()
88 requirements = set()
89
89
90 self.sharedpath = self.path
90 self.sharedpath = self.path
91 try:
91 try:
92 s = os.path.realpath(self.opener.read("sharedpath").rstrip('\n'))
92 s = os.path.realpath(self.opener.read("sharedpath").rstrip('\n'))
93 if not os.path.exists(s):
93 if not os.path.exists(s):
94 raise error.RepoError(
94 raise error.RepoError(
95 _('.hg/sharedpath points to nonexistent directory %s') % s)
95 _('.hg/sharedpath points to nonexistent directory %s') % s)
96 self.sharedpath = s
96 self.sharedpath = s
97 except IOError, inst:
97 except IOError, inst:
98 if inst.errno != errno.ENOENT:
98 if inst.errno != errno.ENOENT:
99 raise
99 raise
100
100
101 self.store = store.store(requirements, self.sharedpath, scmutil.opener)
101 self.store = store.store(requirements, self.sharedpath, scmutil.opener)
102 self.spath = self.store.path
102 self.spath = self.store.path
103 self.sopener = self.store.opener
103 self.sopener = self.store.opener
104 self.sjoin = self.store.join
104 self.sjoin = self.store.join
105 self.opener.createmode = self.store.createmode
105 self.opener.createmode = self.store.createmode
106 self._applyrequirements(requirements)
106 self._applyrequirements(requirements)
107 if create:
107 if create:
108 self._writerequirements()
108 self._writerequirements()
109
109
110
110
111 self._branchcache = None
111 self._branchcache = None
112 self._branchcachetip = None
112 self._branchcachetip = None
113 self.filterpats = {}
113 self.filterpats = {}
114 self._datafilters = {}
114 self._datafilters = {}
115 self._transref = self._lockref = self._wlockref = None
115 self._transref = self._lockref = self._wlockref = None
116
116
117 # A cache for various files under .hg/ that tracks file changes,
117 # A cache for various files under .hg/ that tracks file changes,
118 # (used by the filecache decorator)
118 # (used by the filecache decorator)
119 #
119 #
120 # Maps a property name to its util.filecacheentry
120 # Maps a property name to its util.filecacheentry
121 self._filecache = {}
121 self._filecache = {}
122
122
123 def _applyrequirements(self, requirements):
123 def _applyrequirements(self, requirements):
124 self.requirements = requirements
124 self.requirements = requirements
125 openerreqs = set(('revlogv1', 'generaldelta'))
125 openerreqs = set(('revlogv1', 'generaldelta'))
126 self.sopener.options = dict((r, 1) for r in requirements
126 self.sopener.options = dict((r, 1) for r in requirements
127 if r in openerreqs)
127 if r in openerreqs)
128
128
129 def _writerequirements(self):
129 def _writerequirements(self):
130 reqfile = self.opener("requires", "w")
130 reqfile = self.opener("requires", "w")
131 for r in self.requirements:
131 for r in self.requirements:
132 reqfile.write("%s\n" % r)
132 reqfile.write("%s\n" % r)
133 reqfile.close()
133 reqfile.close()
134
134
135 def _checknested(self, path):
135 def _checknested(self, path):
136 """Determine if path is a legal nested repository."""
136 """Determine if path is a legal nested repository."""
137 if not path.startswith(self.root):
137 if not path.startswith(self.root):
138 return False
138 return False
139 subpath = path[len(self.root) + 1:]
139 subpath = path[len(self.root) + 1:]
140 normsubpath = util.pconvert(subpath)
140 normsubpath = util.pconvert(subpath)
141
141
142 # XXX: Checking against the current working copy is wrong in
142 # XXX: Checking against the current working copy is wrong in
143 # the sense that it can reject things like
143 # the sense that it can reject things like
144 #
144 #
145 # $ hg cat -r 10 sub/x.txt
145 # $ hg cat -r 10 sub/x.txt
146 #
146 #
147 # if sub/ is no longer a subrepository in the working copy
147 # if sub/ is no longer a subrepository in the working copy
148 # parent revision.
148 # parent revision.
149 #
149 #
150 # However, it can of course also allow things that would have
150 # However, it can of course also allow things that would have
151 # been rejected before, such as the above cat command if sub/
151 # been rejected before, such as the above cat command if sub/
152 # is a subrepository now, but was a normal directory before.
152 # is a subrepository now, but was a normal directory before.
153 # The old path auditor would have rejected by mistake since it
153 # The old path auditor would have rejected by mistake since it
154 # panics when it sees sub/.hg/.
154 # panics when it sees sub/.hg/.
155 #
155 #
156 # All in all, checking against the working copy seems sensible
156 # All in all, checking against the working copy seems sensible
157 # since we want to prevent access to nested repositories on
157 # since we want to prevent access to nested repositories on
158 # the filesystem *now*.
158 # the filesystem *now*.
159 ctx = self[None]
159 ctx = self[None]
160 parts = util.splitpath(subpath)
160 parts = util.splitpath(subpath)
161 while parts:
161 while parts:
162 prefix = '/'.join(parts)
162 prefix = '/'.join(parts)
163 if prefix in ctx.substate:
163 if prefix in ctx.substate:
164 if prefix == normsubpath:
164 if prefix == normsubpath:
165 return True
165 return True
166 else:
166 else:
167 sub = ctx.sub(prefix)
167 sub = ctx.sub(prefix)
168 return sub.checknested(subpath[len(prefix) + 1:])
168 return sub.checknested(subpath[len(prefix) + 1:])
169 else:
169 else:
170 parts.pop()
170 parts.pop()
171 return False
171 return False
172
172
173 @filecache('bookmarks')
173 @filecache('bookmarks')
174 def _bookmarks(self):
174 def _bookmarks(self):
175 return bookmarks.read(self)
175 return bookmarks.read(self)
176
176
177 @filecache('bookmarks.current')
177 @filecache('bookmarks.current')
178 def _bookmarkcurrent(self):
178 def _bookmarkcurrent(self):
179 return bookmarks.readcurrent(self)
179 return bookmarks.readcurrent(self)
180
180
181 def _writebookmarks(self, marks):
181 def _writebookmarks(self, marks):
182 bookmarks.write(self)
182 bookmarks.write(self)
183
183
184 @storecache('phaseroots')
184 @storecache('phaseroots')
185 def _phaseroots(self):
185 def _phaseroots(self):
186 self._dirtyphases = False
186 self._dirtyphases = False
187 phaseroots = phases.readroots(self)
187 phaseroots = phases.readroots(self)
188 phases.filterunknown(self, phaseroots)
188 phases.filterunknown(self, phaseroots)
189 return phaseroots
189 return phaseroots
190
190
191 @propertycache
191 @propertycache
192 def _phaserev(self):
192 def _phaserev(self):
193 cache = [phases.public] * len(self)
193 cache = [phases.public] * len(self)
194 for phase in phases.trackedphases:
194 for phase in phases.trackedphases:
195 roots = map(self.changelog.rev, self._phaseroots[phase])
195 roots = map(self.changelog.rev, self._phaseroots[phase])
196 if roots:
196 if roots:
197 for rev in roots:
197 for rev in roots:
198 cache[rev] = phase
198 cache[rev] = phase
199 for rev in self.changelog.descendants(*roots):
199 for rev in self.changelog.descendants(*roots):
200 cache[rev] = phase
200 cache[rev] = phase
201 return cache
201 return cache
202
202
203 @storecache('00changelog.i')
203 @storecache('00changelog.i')
204 def changelog(self):
204 def changelog(self):
205 c = changelog.changelog(self.sopener)
205 c = changelog.changelog(self.sopener)
206 if 'HG_PENDING' in os.environ:
206 if 'HG_PENDING' in os.environ:
207 p = os.environ['HG_PENDING']
207 p = os.environ['HG_PENDING']
208 if p.startswith(self.root):
208 if p.startswith(self.root):
209 c.readpending('00changelog.i.a')
209 c.readpending('00changelog.i.a')
210 return c
210 return c
211
211
212 @storecache('00manifest.i')
212 @storecache('00manifest.i')
213 def manifest(self):
213 def manifest(self):
214 return manifest.manifest(self.sopener)
214 return manifest.manifest(self.sopener)
215
215
216 @filecache('dirstate')
216 @filecache('dirstate')
217 def dirstate(self):
217 def dirstate(self):
218 warned = [0]
218 warned = [0]
219 def validate(node):
219 def validate(node):
220 try:
220 try:
221 self.changelog.rev(node)
221 self.changelog.rev(node)
222 return node
222 return node
223 except error.LookupError:
223 except error.LookupError:
224 if not warned[0]:
224 if not warned[0]:
225 warned[0] = True
225 warned[0] = True
226 self.ui.warn(_("warning: ignoring unknown"
226 self.ui.warn(_("warning: ignoring unknown"
227 " working parent %s!\n") % short(node))
227 " working parent %s!\n") % short(node))
228 return nullid
228 return nullid
229
229
230 return dirstate.dirstate(self.opener, self.ui, self.root, validate)
230 return dirstate.dirstate(self.opener, self.ui, self.root, validate)
231
231
232 def __getitem__(self, changeid):
232 def __getitem__(self, changeid):
233 if changeid is None:
233 if changeid is None:
234 return context.workingctx(self)
234 return context.workingctx(self)
235 return context.changectx(self, changeid)
235 return context.changectx(self, changeid)
236
236
237 def __contains__(self, changeid):
237 def __contains__(self, changeid):
238 try:
238 try:
239 return bool(self.lookup(changeid))
239 return bool(self.lookup(changeid))
240 except error.RepoLookupError:
240 except error.RepoLookupError:
241 return False
241 return False
242
242
243 def __nonzero__(self):
243 def __nonzero__(self):
244 return True
244 return True
245
245
246 def __len__(self):
246 def __len__(self):
247 return len(self.changelog)
247 return len(self.changelog)
248
248
249 def __iter__(self):
249 def __iter__(self):
250 for i in xrange(len(self)):
250 for i in xrange(len(self)):
251 yield i
251 yield i
252
252
253 def revs(self, expr, *args):
253 def revs(self, expr, *args):
254 '''Return a list of revisions matching the given revset'''
254 '''Return a list of revisions matching the given revset'''
255 expr = revset.formatspec(expr, *args)
255 expr = revset.formatspec(expr, *args)
256 m = revset.match(None, expr)
256 m = revset.match(None, expr)
257 return [r for r in m(self, range(len(self)))]
257 return [r for r in m(self, range(len(self)))]
258
258
259 def set(self, expr, *args):
259 def set(self, expr, *args):
260 '''
260 '''
261 Yield a context for each matching revision, after doing arg
261 Yield a context for each matching revision, after doing arg
262 replacement via revset.formatspec
262 replacement via revset.formatspec
263 '''
263 '''
264 for r in self.revs(expr, *args):
264 for r in self.revs(expr, *args):
265 yield self[r]
265 yield self[r]
266
266
267 def url(self):
267 def url(self):
268 return 'file:' + self.root
268 return 'file:' + self.root
269
269
270 def hook(self, name, throw=False, **args):
270 def hook(self, name, throw=False, **args):
271 return hook.hook(self.ui, self, name, throw, **args)
271 return hook.hook(self.ui, self, name, throw, **args)
272
272
273 tag_disallowed = ':\r\n'
273 tag_disallowed = ':\r\n'
274
274
275 def _tag(self, names, node, message, local, user, date, extra={}):
275 def _tag(self, names, node, message, local, user, date, extra={}):
276 if isinstance(names, str):
276 if isinstance(names, str):
277 allchars = names
277 allchars = names
278 names = (names,)
278 names = (names,)
279 else:
279 else:
280 allchars = ''.join(names)
280 allchars = ''.join(names)
281 for c in self.tag_disallowed:
281 for c in self.tag_disallowed:
282 if c in allchars:
282 if c in allchars:
283 raise util.Abort(_('%r cannot be used in a tag name') % c)
283 raise util.Abort(_('%r cannot be used in a tag name') % c)
284
284
285 branches = self.branchmap()
285 branches = self.branchmap()
286 for name in names:
286 for name in names:
287 self.hook('pretag', throw=True, node=hex(node), tag=name,
287 self.hook('pretag', throw=True, node=hex(node), tag=name,
288 local=local)
288 local=local)
289 if name in branches:
289 if name in branches:
290 self.ui.warn(_("warning: tag %s conflicts with existing"
290 self.ui.warn(_("warning: tag %s conflicts with existing"
291 " branch name\n") % name)
291 " branch name\n") % name)
292
292
293 def writetags(fp, names, munge, prevtags):
293 def writetags(fp, names, munge, prevtags):
294 fp.seek(0, 2)
294 fp.seek(0, 2)
295 if prevtags and prevtags[-1] != '\n':
295 if prevtags and prevtags[-1] != '\n':
296 fp.write('\n')
296 fp.write('\n')
297 for name in names:
297 for name in names:
298 m = munge and munge(name) or name
298 m = munge and munge(name) or name
299 if self._tagscache.tagtypes and name in self._tagscache.tagtypes:
299 if self._tagscache.tagtypes and name in self._tagscache.tagtypes:
300 old = self.tags().get(name, nullid)
300 old = self.tags().get(name, nullid)
301 fp.write('%s %s\n' % (hex(old), m))
301 fp.write('%s %s\n' % (hex(old), m))
302 fp.write('%s %s\n' % (hex(node), m))
302 fp.write('%s %s\n' % (hex(node), m))
303 fp.close()
303 fp.close()
304
304
305 prevtags = ''
305 prevtags = ''
306 if local:
306 if local:
307 try:
307 try:
308 fp = self.opener('localtags', 'r+')
308 fp = self.opener('localtags', 'r+')
309 except IOError:
309 except IOError:
310 fp = self.opener('localtags', 'a')
310 fp = self.opener('localtags', 'a')
311 else:
311 else:
312 prevtags = fp.read()
312 prevtags = fp.read()
313
313
314 # local tags are stored in the current charset
314 # local tags are stored in the current charset
315 writetags(fp, names, None, prevtags)
315 writetags(fp, names, None, prevtags)
316 for name in names:
316 for name in names:
317 self.hook('tag', node=hex(node), tag=name, local=local)
317 self.hook('tag', node=hex(node), tag=name, local=local)
318 return
318 return
319
319
320 try:
320 try:
321 fp = self.wfile('.hgtags', 'rb+')
321 fp = self.wfile('.hgtags', 'rb+')
322 except IOError, e:
322 except IOError, e:
323 if e.errno != errno.ENOENT:
323 if e.errno != errno.ENOENT:
324 raise
324 raise
325 fp = self.wfile('.hgtags', 'ab')
325 fp = self.wfile('.hgtags', 'ab')
326 else:
326 else:
327 prevtags = fp.read()
327 prevtags = fp.read()
328
328
329 # committed tags are stored in UTF-8
329 # committed tags are stored in UTF-8
330 writetags(fp, names, encoding.fromlocal, prevtags)
330 writetags(fp, names, encoding.fromlocal, prevtags)
331
331
332 fp.close()
332 fp.close()
333
333
334 self.invalidatecaches()
334 self.invalidatecaches()
335
335
336 if '.hgtags' not in self.dirstate:
336 if '.hgtags' not in self.dirstate:
337 self[None].add(['.hgtags'])
337 self[None].add(['.hgtags'])
338
338
339 m = matchmod.exact(self.root, '', ['.hgtags'])
339 m = matchmod.exact(self.root, '', ['.hgtags'])
340 tagnode = self.commit(message, user, date, extra=extra, match=m)
340 tagnode = self.commit(message, user, date, extra=extra, match=m)
341
341
342 for name in names:
342 for name in names:
343 self.hook('tag', node=hex(node), tag=name, local=local)
343 self.hook('tag', node=hex(node), tag=name, local=local)
344
344
345 return tagnode
345 return tagnode
346
346
347 def tag(self, names, node, message, local, user, date):
347 def tag(self, names, node, message, local, user, date):
348 '''tag a revision with one or more symbolic names.
348 '''tag a revision with one or more symbolic names.
349
349
350 names is a list of strings or, when adding a single tag, names may be a
350 names is a list of strings or, when adding a single tag, names may be a
351 string.
351 string.
352
352
353 if local is True, the tags are stored in a per-repository file.
353 if local is True, the tags are stored in a per-repository file.
354 otherwise, they are stored in the .hgtags file, and a new
354 otherwise, they are stored in the .hgtags file, and a new
355 changeset is committed with the change.
355 changeset is committed with the change.
356
356
357 keyword arguments:
357 keyword arguments:
358
358
359 local: whether to store tags in non-version-controlled file
359 local: whether to store tags in non-version-controlled file
360 (default False)
360 (default False)
361
361
362 message: commit message to use if committing
362 message: commit message to use if committing
363
363
364 user: name of user to use if committing
364 user: name of user to use if committing
365
365
366 date: date tuple to use if committing'''
366 date: date tuple to use if committing'''
367
367
368 if not local:
368 if not local:
369 for x in self.status()[:5]:
369 for x in self.status()[:5]:
370 if '.hgtags' in x:
370 if '.hgtags' in x:
371 raise util.Abort(_('working copy of .hgtags is changed '
371 raise util.Abort(_('working copy of .hgtags is changed '
372 '(please commit .hgtags manually)'))
372 '(please commit .hgtags manually)'))
373
373
374 self.tags() # instantiate the cache
374 self.tags() # instantiate the cache
375 self._tag(names, node, message, local, user, date)
375 self._tag(names, node, message, local, user, date)
376
376
377 @propertycache
377 @propertycache
378 def _tagscache(self):
378 def _tagscache(self):
379 '''Returns a tagscache object that contains various tags related caches.'''
379 '''Returns a tagscache object that contains various tags related caches.'''
380
380
381 # This simplifies its cache management by having one decorated
381 # This simplifies its cache management by having one decorated
382 # function (this one) and the rest simply fetch things from it.
382 # function (this one) and the rest simply fetch things from it.
383 class tagscache(object):
383 class tagscache(object):
384 def __init__(self):
384 def __init__(self):
385 # These two define the set of tags for this repository. tags
385 # These two define the set of tags for this repository. tags
386 # maps tag name to node; tagtypes maps tag name to 'global' or
386 # maps tag name to node; tagtypes maps tag name to 'global' or
387 # 'local'. (Global tags are defined by .hgtags across all
387 # 'local'. (Global tags are defined by .hgtags across all
388 # heads, and local tags are defined in .hg/localtags.)
388 # heads, and local tags are defined in .hg/localtags.)
389 # They constitute the in-memory cache of tags.
389 # They constitute the in-memory cache of tags.
390 self.tags = self.tagtypes = None
390 self.tags = self.tagtypes = None
391
391
392 self.nodetagscache = self.tagslist = None
392 self.nodetagscache = self.tagslist = None
393
393
394 cache = tagscache()
394 cache = tagscache()
395 cache.tags, cache.tagtypes = self._findtags()
395 cache.tags, cache.tagtypes = self._findtags()
396
396
397 return cache
397 return cache
398
398
399 def tags(self):
399 def tags(self):
400 '''return a mapping of tag to node'''
400 '''return a mapping of tag to node'''
401 return self._tagscache.tags
401 return self._tagscache.tags
402
402
403 def _findtags(self):
403 def _findtags(self):
404 '''Do the hard work of finding tags. Return a pair of dicts
404 '''Do the hard work of finding tags. Return a pair of dicts
405 (tags, tagtypes) where tags maps tag name to node, and tagtypes
405 (tags, tagtypes) where tags maps tag name to node, and tagtypes
406 maps tag name to a string like \'global\' or \'local\'.
406 maps tag name to a string like \'global\' or \'local\'.
407 Subclasses or extensions are free to add their own tags, but
407 Subclasses or extensions are free to add their own tags, but
408 should be aware that the returned dicts will be retained for the
408 should be aware that the returned dicts will be retained for the
409 duration of the localrepo object.'''
409 duration of the localrepo object.'''
410
410
411 # XXX what tagtype should subclasses/extensions use? Currently
411 # XXX what tagtype should subclasses/extensions use? Currently
412 # mq and bookmarks add tags, but do not set the tagtype at all.
412 # mq and bookmarks add tags, but do not set the tagtype at all.
413 # Should each extension invent its own tag type? Should there
413 # Should each extension invent its own tag type? Should there
414 # be one tagtype for all such "virtual" tags? Or is the status
414 # be one tagtype for all such "virtual" tags? Or is the status
415 # quo fine?
415 # quo fine?
416
416
417 alltags = {} # map tag name to (node, hist)
417 alltags = {} # map tag name to (node, hist)
418 tagtypes = {}
418 tagtypes = {}
419
419
420 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
420 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
421 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
421 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
422
422
423 # Build the return dicts. Have to re-encode tag names because
423 # Build the return dicts. Have to re-encode tag names because
424 # the tags module always uses UTF-8 (in order not to lose info
424 # the tags module always uses UTF-8 (in order not to lose info
425 # writing to the cache), but the rest of Mercurial wants them in
425 # writing to the cache), but the rest of Mercurial wants them in
426 # local encoding.
426 # local encoding.
427 tags = {}
427 tags = {}
428 for (name, (node, hist)) in alltags.iteritems():
428 for (name, (node, hist)) in alltags.iteritems():
429 if node != nullid:
429 if node != nullid:
430 try:
430 try:
431 # ignore tags to unknown nodes
431 # ignore tags to unknown nodes
432 self.changelog.lookup(node)
432 self.changelog.lookup(node)
433 tags[encoding.tolocal(name)] = node
433 tags[encoding.tolocal(name)] = node
434 except error.LookupError:
434 except error.LookupError:
435 pass
435 pass
436 tags['tip'] = self.changelog.tip()
436 tags['tip'] = self.changelog.tip()
437 tagtypes = dict([(encoding.tolocal(name), value)
437 tagtypes = dict([(encoding.tolocal(name), value)
438 for (name, value) in tagtypes.iteritems()])
438 for (name, value) in tagtypes.iteritems()])
439 return (tags, tagtypes)
439 return (tags, tagtypes)
440
440
441 def tagtype(self, tagname):
441 def tagtype(self, tagname):
442 '''
442 '''
443 return the type of the given tag. result can be:
443 return the type of the given tag. result can be:
444
444
445 'local' : a local tag
445 'local' : a local tag
446 'global' : a global tag
446 'global' : a global tag
447 None : tag does not exist
447 None : tag does not exist
448 '''
448 '''
449
449
450 return self._tagscache.tagtypes.get(tagname)
450 return self._tagscache.tagtypes.get(tagname)
451
451
452 def tagslist(self):
452 def tagslist(self):
453 '''return a list of tags ordered by revision'''
453 '''return a list of tags ordered by revision'''
454 if not self._tagscache.tagslist:
454 if not self._tagscache.tagslist:
455 l = []
455 l = []
456 for t, n in self.tags().iteritems():
456 for t, n in self.tags().iteritems():
457 r = self.changelog.rev(n)
457 r = self.changelog.rev(n)
458 l.append((r, t, n))
458 l.append((r, t, n))
459 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
459 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
460
460
461 return self._tagscache.tagslist
461 return self._tagscache.tagslist
462
462
463 def nodetags(self, node):
463 def nodetags(self, node):
464 '''return the tags associated with a node'''
464 '''return the tags associated with a node'''
465 if not self._tagscache.nodetagscache:
465 if not self._tagscache.nodetagscache:
466 nodetagscache = {}
466 nodetagscache = {}
467 for t, n in self.tags().iteritems():
467 for t, n in self.tags().iteritems():
468 nodetagscache.setdefault(n, []).append(t)
468 nodetagscache.setdefault(n, []).append(t)
469 for tags in nodetagscache.itervalues():
469 for tags in nodetagscache.itervalues():
470 tags.sort()
470 tags.sort()
471 self._tagscache.nodetagscache = nodetagscache
471 self._tagscache.nodetagscache = nodetagscache
472 return self._tagscache.nodetagscache.get(node, [])
472 return self._tagscache.nodetagscache.get(node, [])
473
473
474 def nodebookmarks(self, node):
474 def nodebookmarks(self, node):
475 marks = []
475 marks = []
476 for bookmark, n in self._bookmarks.iteritems():
476 for bookmark, n in self._bookmarks.iteritems():
477 if n == node:
477 if n == node:
478 marks.append(bookmark)
478 marks.append(bookmark)
479 return sorted(marks)
479 return sorted(marks)
480
480
481 def _branchtags(self, partial, lrev):
481 def _branchtags(self, partial, lrev):
482 # TODO: rename this function?
482 # TODO: rename this function?
483 tiprev = len(self) - 1
483 tiprev = len(self) - 1
484 if lrev != tiprev:
484 if lrev != tiprev:
485 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
485 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
486 self._updatebranchcache(partial, ctxgen)
486 self._updatebranchcache(partial, ctxgen)
487 self._writebranchcache(partial, self.changelog.tip(), tiprev)
487 self._writebranchcache(partial, self.changelog.tip(), tiprev)
488
488
489 return partial
489 return partial
490
490
491 def updatebranchcache(self):
491 def updatebranchcache(self):
492 tip = self.changelog.tip()
492 tip = self.changelog.tip()
493 if self._branchcache is not None and self._branchcachetip == tip:
493 if self._branchcache is not None and self._branchcachetip == tip:
494 return
494 return
495
495
496 oldtip = self._branchcachetip
496 oldtip = self._branchcachetip
497 self._branchcachetip = tip
497 self._branchcachetip = tip
498 if oldtip is None or oldtip not in self.changelog.nodemap:
498 if oldtip is None or oldtip not in self.changelog.nodemap:
499 partial, last, lrev = self._readbranchcache()
499 partial, last, lrev = self._readbranchcache()
500 else:
500 else:
501 lrev = self.changelog.rev(oldtip)
501 lrev = self.changelog.rev(oldtip)
502 partial = self._branchcache
502 partial = self._branchcache
503
503
504 self._branchtags(partial, lrev)
504 self._branchtags(partial, lrev)
505 # this private cache holds all heads (not just tips)
505 # this private cache holds all heads (not just tips)
506 self._branchcache = partial
506 self._branchcache = partial
507
507
508 def branchmap(self):
508 def branchmap(self):
509 '''returns a dictionary {branch: [branchheads]}'''
509 '''returns a dictionary {branch: [branchheads]}'''
510 self.updatebranchcache()
510 self.updatebranchcache()
511 return self._branchcache
511 return self._branchcache
512
512
513 def branchtags(self):
513 def branchtags(self):
514 '''return a dict where branch names map to the tipmost head of
514 '''return a dict where branch names map to the tipmost head of
515 the branch, open heads come before closed'''
515 the branch, open heads come before closed'''
516 bt = {}
516 bt = {}
517 for bn, heads in self.branchmap().iteritems():
517 for bn, heads in self.branchmap().iteritems():
518 tip = heads[-1]
518 tip = heads[-1]
519 for h in reversed(heads):
519 for h in reversed(heads):
520 if 'close' not in self.changelog.read(h)[5]:
520 if 'close' not in self.changelog.read(h)[5]:
521 tip = h
521 tip = h
522 break
522 break
523 bt[bn] = tip
523 bt[bn] = tip
524 return bt
524 return bt
525
525
526 def _readbranchcache(self):
526 def _readbranchcache(self):
527 partial = {}
527 partial = {}
528 try:
528 try:
529 f = self.opener("cache/branchheads")
529 f = self.opener("cache/branchheads")
530 lines = f.read().split('\n')
530 lines = f.read().split('\n')
531 f.close()
531 f.close()
532 except (IOError, OSError):
532 except (IOError, OSError):
533 return {}, nullid, nullrev
533 return {}, nullid, nullrev
534
534
535 try:
535 try:
536 last, lrev = lines.pop(0).split(" ", 1)
536 last, lrev = lines.pop(0).split(" ", 1)
537 last, lrev = bin(last), int(lrev)
537 last, lrev = bin(last), int(lrev)
538 if lrev >= len(self) or self[lrev].node() != last:
538 if lrev >= len(self) or self[lrev].node() != last:
539 # invalidate the cache
539 # invalidate the cache
540 raise ValueError('invalidating branch cache (tip differs)')
540 raise ValueError('invalidating branch cache (tip differs)')
541 for l in lines:
541 for l in lines:
542 if not l:
542 if not l:
543 continue
543 continue
544 node, label = l.split(" ", 1)
544 node, label = l.split(" ", 1)
545 label = encoding.tolocal(label.strip())
545 label = encoding.tolocal(label.strip())
546 partial.setdefault(label, []).append(bin(node))
546 partial.setdefault(label, []).append(bin(node))
547 except KeyboardInterrupt:
547 except KeyboardInterrupt:
548 raise
548 raise
549 except Exception, inst:
549 except Exception, inst:
550 if self.ui.debugflag:
550 if self.ui.debugflag:
551 self.ui.warn(str(inst), '\n')
551 self.ui.warn(str(inst), '\n')
552 partial, last, lrev = {}, nullid, nullrev
552 partial, last, lrev = {}, nullid, nullrev
553 return partial, last, lrev
553 return partial, last, lrev
554
554
555 def _writebranchcache(self, branches, tip, tiprev):
555 def _writebranchcache(self, branches, tip, tiprev):
556 try:
556 try:
557 f = self.opener("cache/branchheads", "w", atomictemp=True)
557 f = self.opener("cache/branchheads", "w", atomictemp=True)
558 f.write("%s %s\n" % (hex(tip), tiprev))
558 f.write("%s %s\n" % (hex(tip), tiprev))
559 for label, nodes in branches.iteritems():
559 for label, nodes in branches.iteritems():
560 for node in nodes:
560 for node in nodes:
561 f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
561 f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
562 f.close()
562 f.close()
563 except (IOError, OSError):
563 except (IOError, OSError):
564 pass
564 pass
565
565
566 def _updatebranchcache(self, partial, ctxgen):
566 def _updatebranchcache(self, partial, ctxgen):
567 # collect new branch entries
567 # collect new branch entries
568 newbranches = {}
568 newbranches = {}
569 for c in ctxgen:
569 for c in ctxgen:
570 newbranches.setdefault(c.branch(), []).append(c.node())
570 newbranches.setdefault(c.branch(), []).append(c.node())
571 # if older branchheads are reachable from new ones, they aren't
571 # if older branchheads are reachable from new ones, they aren't
572 # really branchheads. Note checking parents is insufficient:
572 # really branchheads. Note checking parents is insufficient:
573 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
573 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
574 for branch, newnodes in newbranches.iteritems():
574 for branch, newnodes in newbranches.iteritems():
575 bheads = partial.setdefault(branch, [])
575 bheads = partial.setdefault(branch, [])
576 bheads.extend(newnodes)
576 bheads.extend(newnodes)
577 if len(bheads) <= 1:
577 if len(bheads) <= 1:
578 continue
578 continue
579 bheads = sorted(bheads, key=lambda x: self[x].rev())
579 bheads = sorted(bheads, key=lambda x: self[x].rev())
580 # starting from tip means fewer passes over reachable
580 # starting from tip means fewer passes over reachable
581 while newnodes:
581 while newnodes:
582 latest = newnodes.pop()
582 latest = newnodes.pop()
583 if latest not in bheads:
583 if latest not in bheads:
584 continue
584 continue
585 minbhrev = self[bheads[0]].node()
585 minbhrev = self[bheads[0]].node()
586 reachable = self.changelog.reachable(latest, minbhrev)
586 reachable = self.changelog.reachable(latest, minbhrev)
587 reachable.remove(latest)
587 reachable.remove(latest)
588 if reachable:
588 if reachable:
589 bheads = [b for b in bheads if b not in reachable]
589 bheads = [b for b in bheads if b not in reachable]
590 partial[branch] = bheads
590 partial[branch] = bheads
591
591
592 def lookup(self, key):
592 def lookup(self, key):
593 if isinstance(key, int):
593 if isinstance(key, int):
594 return self.changelog.node(key)
594 return self.changelog.node(key)
595 elif key == '.':
595 elif key == '.':
596 return self.dirstate.p1()
596 return self.dirstate.p1()
597 elif key == 'null':
597 elif key == 'null':
598 return nullid
598 return nullid
599 elif key == 'tip':
599 elif key == 'tip':
600 return self.changelog.tip()
600 return self.changelog.tip()
601 n = self.changelog._match(key)
601 n = self.changelog._match(key)
602 if n:
602 if n:
603 return n
603 return n
604 if key in self._bookmarks:
604 if key in self._bookmarks:
605 return self._bookmarks[key]
605 return self._bookmarks[key]
606 if key in self.tags():
606 if key in self.tags():
607 return self.tags()[key]
607 return self.tags()[key]
608 if key in self.branchtags():
608 if key in self.branchtags():
609 return self.branchtags()[key]
609 return self.branchtags()[key]
610 n = self.changelog._partialmatch(key)
610 n = self.changelog._partialmatch(key)
611 if n:
611 if n:
612 return n
612 return n
613
613
614 # can't find key, check if it might have come from damaged dirstate
614 # can't find key, check if it might have come from damaged dirstate
615 if key in self.dirstate.parents():
615 if key in self.dirstate.parents():
616 raise error.Abort(_("working directory has unknown parent '%s'!")
616 raise error.Abort(_("working directory has unknown parent '%s'!")
617 % short(key))
617 % short(key))
618 try:
618 try:
619 if len(key) == 20:
619 if len(key) == 20:
620 key = hex(key)
620 key = hex(key)
621 except TypeError:
621 except TypeError:
622 pass
622 pass
623 raise error.RepoLookupError(_("unknown revision '%s'") % key)
623 raise error.RepoLookupError(_("unknown revision '%s'") % key)
624
624
625 def lookupbranch(self, key, remote=None):
625 def lookupbranch(self, key, remote=None):
626 repo = remote or self
626 repo = remote or self
627 if key in repo.branchmap():
627 if key in repo.branchmap():
628 return key
628 return key
629
629
630 repo = (remote and remote.local()) and remote or self
630 repo = (remote and remote.local()) and remote or self
631 return repo[key].branch()
631 return repo[key].branch()
632
632
633 def known(self, nodes):
633 def known(self, nodes):
634 nm = self.changelog.nodemap
634 nm = self.changelog.nodemap
635 result = []
635 result = []
636 for n in nodes:
636 for n in nodes:
637 r = nm.get(n)
637 r = nm.get(n)
638 resp = not (r is None or self._phaserev[r] >= phases.secret)
638 resp = not (r is None or self._phaserev[r] >= phases.secret)
639 result.append(resp)
639 result.append(resp)
640 return result
640 return result
641
641
642 def local(self):
642 def local(self):
643 return self
643 return self
644
644
645 def join(self, f):
645 def join(self, f):
646 return os.path.join(self.path, f)
646 return os.path.join(self.path, f)
647
647
648 def wjoin(self, f):
648 def wjoin(self, f):
649 return os.path.join(self.root, f)
649 return os.path.join(self.root, f)
650
650
651 def file(self, f):
651 def file(self, f):
652 if f[0] == '/':
652 if f[0] == '/':
653 f = f[1:]
653 f = f[1:]
654 return filelog.filelog(self.sopener, f)
654 return filelog.filelog(self.sopener, f)
655
655
656 def changectx(self, changeid):
656 def changectx(self, changeid):
657 return self[changeid]
657 return self[changeid]
658
658
659 def parents(self, changeid=None):
659 def parents(self, changeid=None):
660 '''get list of changectxs for parents of changeid'''
660 '''get list of changectxs for parents of changeid'''
661 return self[changeid].parents()
661 return self[changeid].parents()
662
662
663 def filectx(self, path, changeid=None, fileid=None):
663 def filectx(self, path, changeid=None, fileid=None):
664 """changeid can be a changeset revision, node, or tag.
664 """changeid can be a changeset revision, node, or tag.
665 fileid can be a file revision or node."""
665 fileid can be a file revision or node."""
666 return context.filectx(self, path, changeid, fileid)
666 return context.filectx(self, path, changeid, fileid)
667
667
668 def getcwd(self):
668 def getcwd(self):
669 return self.dirstate.getcwd()
669 return self.dirstate.getcwd()
670
670
671 def pathto(self, f, cwd=None):
671 def pathto(self, f, cwd=None):
672 return self.dirstate.pathto(f, cwd)
672 return self.dirstate.pathto(f, cwd)
673
673
674 def wfile(self, f, mode='r'):
674 def wfile(self, f, mode='r'):
675 return self.wopener(f, mode)
675 return self.wopener(f, mode)
676
676
677 def _link(self, f):
677 def _link(self, f):
678 return os.path.islink(self.wjoin(f))
678 return os.path.islink(self.wjoin(f))
679
679
680 def _loadfilter(self, filter):
680 def _loadfilter(self, filter):
681 if filter not in self.filterpats:
681 if filter not in self.filterpats:
682 l = []
682 l = []
683 for pat, cmd in self.ui.configitems(filter):
683 for pat, cmd in self.ui.configitems(filter):
684 if cmd == '!':
684 if cmd == '!':
685 continue
685 continue
686 mf = matchmod.match(self.root, '', [pat])
686 mf = matchmod.match(self.root, '', [pat])
687 fn = None
687 fn = None
688 params = cmd
688 params = cmd
689 for name, filterfn in self._datafilters.iteritems():
689 for name, filterfn in self._datafilters.iteritems():
690 if cmd.startswith(name):
690 if cmd.startswith(name):
691 fn = filterfn
691 fn = filterfn
692 params = cmd[len(name):].lstrip()
692 params = cmd[len(name):].lstrip()
693 break
693 break
694 if not fn:
694 if not fn:
695 fn = lambda s, c, **kwargs: util.filter(s, c)
695 fn = lambda s, c, **kwargs: util.filter(s, c)
696 # Wrap old filters not supporting keyword arguments
696 # Wrap old filters not supporting keyword arguments
697 if not inspect.getargspec(fn)[2]:
697 if not inspect.getargspec(fn)[2]:
698 oldfn = fn
698 oldfn = fn
699 fn = lambda s, c, **kwargs: oldfn(s, c)
699 fn = lambda s, c, **kwargs: oldfn(s, c)
700 l.append((mf, fn, params))
700 l.append((mf, fn, params))
701 self.filterpats[filter] = l
701 self.filterpats[filter] = l
702 return self.filterpats[filter]
702 return self.filterpats[filter]
703
703
704 def _filter(self, filterpats, filename, data):
704 def _filter(self, filterpats, filename, data):
705 for mf, fn, cmd in filterpats:
705 for mf, fn, cmd in filterpats:
706 if mf(filename):
706 if mf(filename):
707 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
707 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
708 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
708 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
709 break
709 break
710
710
711 return data
711 return data
712
712
713 @propertycache
713 @propertycache
714 def _encodefilterpats(self):
714 def _encodefilterpats(self):
715 return self._loadfilter('encode')
715 return self._loadfilter('encode')
716
716
717 @propertycache
717 @propertycache
718 def _decodefilterpats(self):
718 def _decodefilterpats(self):
719 return self._loadfilter('decode')
719 return self._loadfilter('decode')
720
720
721 def adddatafilter(self, name, filter):
721 def adddatafilter(self, name, filter):
722 self._datafilters[name] = filter
722 self._datafilters[name] = filter
723
723
724 def wread(self, filename):
724 def wread(self, filename):
725 if self._link(filename):
725 if self._link(filename):
726 data = os.readlink(self.wjoin(filename))
726 data = os.readlink(self.wjoin(filename))
727 else:
727 else:
728 data = self.wopener.read(filename)
728 data = self.wopener.read(filename)
729 return self._filter(self._encodefilterpats, filename, data)
729 return self._filter(self._encodefilterpats, filename, data)
730
730
731 def wwrite(self, filename, data, flags):
731 def wwrite(self, filename, data, flags):
732 data = self._filter(self._decodefilterpats, filename, data)
732 data = self._filter(self._decodefilterpats, filename, data)
733 if 'l' in flags:
733 if 'l' in flags:
734 self.wopener.symlink(data, filename)
734 self.wopener.symlink(data, filename)
735 else:
735 else:
736 self.wopener.write(filename, data)
736 self.wopener.write(filename, data)
737 if 'x' in flags:
737 if 'x' in flags:
738 util.setflags(self.wjoin(filename), False, True)
738 util.setflags(self.wjoin(filename), False, True)
739
739
740 def wwritedata(self, filename, data):
740 def wwritedata(self, filename, data):
741 return self._filter(self._decodefilterpats, filename, data)
741 return self._filter(self._decodefilterpats, filename, data)
742
742
743 def transaction(self, desc):
743 def transaction(self, desc):
744 tr = self._transref and self._transref() or None
744 tr = self._transref and self._transref() or None
745 if tr and tr.running():
745 if tr and tr.running():
746 return tr.nest()
746 return tr.nest()
747
747
748 # abort here if the journal already exists
748 # abort here if the journal already exists
749 if os.path.exists(self.sjoin("journal")):
749 if os.path.exists(self.sjoin("journal")):
750 raise error.RepoError(
750 raise error.RepoError(
751 _("abandoned transaction found - run hg recover"))
751 _("abandoned transaction found - run hg recover"))
752
752
753 journalfiles = self._writejournal(desc)
753 self._writejournal(desc)
754 renames = [(x, undoname(x)) for x in journalfiles]
754 renames = [(x, undoname(x)) for x in self._journalfiles()]
755
755
756 tr = transaction.transaction(self.ui.warn, self.sopener,
756 tr = transaction.transaction(self.ui.warn, self.sopener,
757 self.sjoin("journal"),
757 self.sjoin("journal"),
758 aftertrans(renames),
758 aftertrans(renames),
759 self.store.createmode)
759 self.store.createmode)
760 self._transref = weakref.ref(tr)
760 self._transref = weakref.ref(tr)
761 return tr
761 return tr
762
762
763 def _journalfiles(self):
764 return (self.sjoin('journal'), self.join('journal.dirstate'),
765 self.join('journal.branch'), self.join('journal.desc'),
766 self.join('journal.bookmarks'),
767 self.sjoin('journal.phaseroots'))
768
769 def undofiles(self):
770 return [undoname(x) for x in self._journalfiles()]
771
763 def _writejournal(self, desc):
772 def _writejournal(self, desc):
764 # save dirstate for rollback
773 # save dirstate for rollback
765 try:
774 try:
766 ds = self.opener.read("dirstate")
775 ds = self.opener.read("dirstate")
767 except IOError:
776 except IOError:
768 ds = ""
777 ds = ""
769 self.opener.write("journal.dirstate", ds)
778 self.opener.write("journal.dirstate", ds)
770 self.opener.write("journal.branch",
779 self.opener.write("journal.branch",
771 encoding.fromlocal(self.dirstate.branch()))
780 encoding.fromlocal(self.dirstate.branch()))
772 self.opener.write("journal.desc",
781 self.opener.write("journal.desc",
773 "%d\n%s\n" % (len(self), desc))
782 "%d\n%s\n" % (len(self), desc))
774
783
775 bkname = self.join('bookmarks')
784 bkname = self.join('bookmarks')
776 if os.path.exists(bkname):
785 if os.path.exists(bkname):
777 util.copyfile(bkname, self.join('journal.bookmarks'))
786 util.copyfile(bkname, self.join('journal.bookmarks'))
778 else:
787 else:
779 self.opener.write('journal.bookmarks', '')
788 self.opener.write('journal.bookmarks', '')
780 phasesname = self.sjoin('phaseroots')
789 phasesname = self.sjoin('phaseroots')
781 if os.path.exists(phasesname):
790 if os.path.exists(phasesname):
782 util.copyfile(phasesname, self.sjoin('journal.phaseroots'))
791 util.copyfile(phasesname, self.sjoin('journal.phaseroots'))
783 else:
792 else:
784 self.sopener.write('journal.phaseroots', '')
793 self.sopener.write('journal.phaseroots', '')
785
794
786 return (self.sjoin('journal'), self.join('journal.dirstate'),
787 self.join('journal.branch'), self.join('journal.desc'),
788 self.join('journal.bookmarks'),
789 self.sjoin('journal.phaseroots'))
790
791 def recover(self):
795 def recover(self):
792 lock = self.lock()
796 lock = self.lock()
793 try:
797 try:
794 if os.path.exists(self.sjoin("journal")):
798 if os.path.exists(self.sjoin("journal")):
795 self.ui.status(_("rolling back interrupted transaction\n"))
799 self.ui.status(_("rolling back interrupted transaction\n"))
796 transaction.rollback(self.sopener, self.sjoin("journal"),
800 transaction.rollback(self.sopener, self.sjoin("journal"),
797 self.ui.warn)
801 self.ui.warn)
798 self.invalidate()
802 self.invalidate()
799 return True
803 return True
800 else:
804 else:
801 self.ui.warn(_("no interrupted transaction available\n"))
805 self.ui.warn(_("no interrupted transaction available\n"))
802 return False
806 return False
803 finally:
807 finally:
804 lock.release()
808 lock.release()
805
809
806 def rollback(self, dryrun=False, force=False):
810 def rollback(self, dryrun=False, force=False):
807 wlock = lock = None
811 wlock = lock = None
808 try:
812 try:
809 wlock = self.wlock()
813 wlock = self.wlock()
810 lock = self.lock()
814 lock = self.lock()
811 if os.path.exists(self.sjoin("undo")):
815 if os.path.exists(self.sjoin("undo")):
812 return self._rollback(dryrun, force)
816 return self._rollback(dryrun, force)
813 else:
817 else:
814 self.ui.warn(_("no rollback information available\n"))
818 self.ui.warn(_("no rollback information available\n"))
815 return 1
819 return 1
816 finally:
820 finally:
817 release(lock, wlock)
821 release(lock, wlock)
818
822
819 def _rollback(self, dryrun, force):
823 def _rollback(self, dryrun, force):
820 ui = self.ui
824 ui = self.ui
821 try:
825 try:
822 args = self.opener.read('undo.desc').splitlines()
826 args = self.opener.read('undo.desc').splitlines()
823 (oldlen, desc, detail) = (int(args[0]), args[1], None)
827 (oldlen, desc, detail) = (int(args[0]), args[1], None)
824 if len(args) >= 3:
828 if len(args) >= 3:
825 detail = args[2]
829 detail = args[2]
826 oldtip = oldlen - 1
830 oldtip = oldlen - 1
827
831
828 if detail and ui.verbose:
832 if detail and ui.verbose:
829 msg = (_('repository tip rolled back to revision %s'
833 msg = (_('repository tip rolled back to revision %s'
830 ' (undo %s: %s)\n')
834 ' (undo %s: %s)\n')
831 % (oldtip, desc, detail))
835 % (oldtip, desc, detail))
832 else:
836 else:
833 msg = (_('repository tip rolled back to revision %s'
837 msg = (_('repository tip rolled back to revision %s'
834 ' (undo %s)\n')
838 ' (undo %s)\n')
835 % (oldtip, desc))
839 % (oldtip, desc))
836 except IOError:
840 except IOError:
837 msg = _('rolling back unknown transaction\n')
841 msg = _('rolling back unknown transaction\n')
838 desc = None
842 desc = None
839
843
840 if not force and self['.'] != self['tip'] and desc == 'commit':
844 if not force and self['.'] != self['tip'] and desc == 'commit':
841 raise util.Abort(
845 raise util.Abort(
842 _('rollback of last commit while not checked out '
846 _('rollback of last commit while not checked out '
843 'may lose data'), hint=_('use -f to force'))
847 'may lose data'), hint=_('use -f to force'))
844
848
845 ui.status(msg)
849 ui.status(msg)
846 if dryrun:
850 if dryrun:
847 return 0
851 return 0
848
852
849 parents = self.dirstate.parents()
853 parents = self.dirstate.parents()
850 transaction.rollback(self.sopener, self.sjoin('undo'), ui.warn)
854 transaction.rollback(self.sopener, self.sjoin('undo'), ui.warn)
851 if os.path.exists(self.join('undo.bookmarks')):
855 if os.path.exists(self.join('undo.bookmarks')):
852 util.rename(self.join('undo.bookmarks'),
856 util.rename(self.join('undo.bookmarks'),
853 self.join('bookmarks'))
857 self.join('bookmarks'))
854 if os.path.exists(self.sjoin('undo.phaseroots')):
858 if os.path.exists(self.sjoin('undo.phaseroots')):
855 util.rename(self.sjoin('undo.phaseroots'),
859 util.rename(self.sjoin('undo.phaseroots'),
856 self.sjoin('phaseroots'))
860 self.sjoin('phaseroots'))
857 self.invalidate()
861 self.invalidate()
858
862
859 parentgone = (parents[0] not in self.changelog.nodemap or
863 parentgone = (parents[0] not in self.changelog.nodemap or
860 parents[1] not in self.changelog.nodemap)
864 parents[1] not in self.changelog.nodemap)
861 if parentgone:
865 if parentgone:
862 util.rename(self.join('undo.dirstate'), self.join('dirstate'))
866 util.rename(self.join('undo.dirstate'), self.join('dirstate'))
863 try:
867 try:
864 branch = self.opener.read('undo.branch')
868 branch = self.opener.read('undo.branch')
865 self.dirstate.setbranch(branch)
869 self.dirstate.setbranch(branch)
866 except IOError:
870 except IOError:
867 ui.warn(_('named branch could not be reset: '
871 ui.warn(_('named branch could not be reset: '
868 'current branch is still \'%s\'\n')
872 'current branch is still \'%s\'\n')
869 % self.dirstate.branch())
873 % self.dirstate.branch())
870
874
871 self.dirstate.invalidate()
875 self.dirstate.invalidate()
872 parents = tuple([p.rev() for p in self.parents()])
876 parents = tuple([p.rev() for p in self.parents()])
873 if len(parents) > 1:
877 if len(parents) > 1:
874 ui.status(_('working directory now based on '
878 ui.status(_('working directory now based on '
875 'revisions %d and %d\n') % parents)
879 'revisions %d and %d\n') % parents)
876 else:
880 else:
877 ui.status(_('working directory now based on '
881 ui.status(_('working directory now based on '
878 'revision %d\n') % parents)
882 'revision %d\n') % parents)
879 self.destroyed()
883 self.destroyed()
880 return 0
884 return 0
881
885
882 def invalidatecaches(self):
886 def invalidatecaches(self):
883 def delcache(name):
887 def delcache(name):
884 try:
888 try:
885 delattr(self, name)
889 delattr(self, name)
886 except AttributeError:
890 except AttributeError:
887 pass
891 pass
888
892
889 delcache('_tagscache')
893 delcache('_tagscache')
890 delcache('_phaserev')
894 delcache('_phaserev')
891
895
892 self._branchcache = None # in UTF-8
896 self._branchcache = None # in UTF-8
893 self._branchcachetip = None
897 self._branchcachetip = None
894
898
895 def invalidatedirstate(self):
899 def invalidatedirstate(self):
896 '''Invalidates the dirstate, causing the next call to dirstate
900 '''Invalidates the dirstate, causing the next call to dirstate
897 to check if it was modified since the last time it was read,
901 to check if it was modified since the last time it was read,
898 rereading it if it has.
902 rereading it if it has.
899
903
900 This is different to dirstate.invalidate() that it doesn't always
904 This is different to dirstate.invalidate() that it doesn't always
901 rereads the dirstate. Use dirstate.invalidate() if you want to
905 rereads the dirstate. Use dirstate.invalidate() if you want to
902 explicitly read the dirstate again (i.e. restoring it to a previous
906 explicitly read the dirstate again (i.e. restoring it to a previous
903 known good state).'''
907 known good state).'''
904 if 'dirstate' in self.__dict__:
908 if 'dirstate' in self.__dict__:
905 for k in self.dirstate._filecache:
909 for k in self.dirstate._filecache:
906 try:
910 try:
907 delattr(self.dirstate, k)
911 delattr(self.dirstate, k)
908 except AttributeError:
912 except AttributeError:
909 pass
913 pass
910 delattr(self, 'dirstate')
914 delattr(self, 'dirstate')
911
915
912 def invalidate(self):
916 def invalidate(self):
913 for k in self._filecache:
917 for k in self._filecache:
914 # dirstate is invalidated separately in invalidatedirstate()
918 # dirstate is invalidated separately in invalidatedirstate()
915 if k == 'dirstate':
919 if k == 'dirstate':
916 continue
920 continue
917
921
918 try:
922 try:
919 delattr(self, k)
923 delattr(self, k)
920 except AttributeError:
924 except AttributeError:
921 pass
925 pass
922 self.invalidatecaches()
926 self.invalidatecaches()
923
927
924 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
928 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
925 try:
929 try:
926 l = lock.lock(lockname, 0, releasefn, desc=desc)
930 l = lock.lock(lockname, 0, releasefn, desc=desc)
927 except error.LockHeld, inst:
931 except error.LockHeld, inst:
928 if not wait:
932 if not wait:
929 raise
933 raise
930 self.ui.warn(_("waiting for lock on %s held by %r\n") %
934 self.ui.warn(_("waiting for lock on %s held by %r\n") %
931 (desc, inst.locker))
935 (desc, inst.locker))
932 # default to 600 seconds timeout
936 # default to 600 seconds timeout
933 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
937 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
934 releasefn, desc=desc)
938 releasefn, desc=desc)
935 if acquirefn:
939 if acquirefn:
936 acquirefn()
940 acquirefn()
937 return l
941 return l
938
942
939 def _afterlock(self, callback):
943 def _afterlock(self, callback):
940 """add a callback to the current repository lock.
944 """add a callback to the current repository lock.
941
945
942 The callback will be executed on lock release."""
946 The callback will be executed on lock release."""
943 l = self._lockref and self._lockref()
947 l = self._lockref and self._lockref()
944 if l:
948 if l:
945 l.postrelease.append(callback)
949 l.postrelease.append(callback)
946
950
947 def lock(self, wait=True):
951 def lock(self, wait=True):
948 '''Lock the repository store (.hg/store) and return a weak reference
952 '''Lock the repository store (.hg/store) and return a weak reference
949 to the lock. Use this before modifying the store (e.g. committing or
953 to the lock. Use this before modifying the store (e.g. committing or
950 stripping). If you are opening a transaction, get a lock as well.)'''
954 stripping). If you are opening a transaction, get a lock as well.)'''
951 l = self._lockref and self._lockref()
955 l = self._lockref and self._lockref()
952 if l is not None and l.held:
956 if l is not None and l.held:
953 l.lock()
957 l.lock()
954 return l
958 return l
955
959
956 def unlock():
960 def unlock():
957 self.store.write()
961 self.store.write()
958 if self._dirtyphases:
962 if self._dirtyphases:
959 phases.writeroots(self)
963 phases.writeroots(self)
960 self._dirtyphases = False
964 self._dirtyphases = False
961 for k, ce in self._filecache.items():
965 for k, ce in self._filecache.items():
962 if k == 'dirstate':
966 if k == 'dirstate':
963 continue
967 continue
964 ce.refresh()
968 ce.refresh()
965
969
966 l = self._lock(self.sjoin("lock"), wait, unlock,
970 l = self._lock(self.sjoin("lock"), wait, unlock,
967 self.invalidate, _('repository %s') % self.origroot)
971 self.invalidate, _('repository %s') % self.origroot)
968 self._lockref = weakref.ref(l)
972 self._lockref = weakref.ref(l)
969 return l
973 return l
970
974
971 def wlock(self, wait=True):
975 def wlock(self, wait=True):
972 '''Lock the non-store parts of the repository (everything under
976 '''Lock the non-store parts of the repository (everything under
973 .hg except .hg/store) and return a weak reference to the lock.
977 .hg except .hg/store) and return a weak reference to the lock.
974 Use this before modifying files in .hg.'''
978 Use this before modifying files in .hg.'''
975 l = self._wlockref and self._wlockref()
979 l = self._wlockref and self._wlockref()
976 if l is not None and l.held:
980 if l is not None and l.held:
977 l.lock()
981 l.lock()
978 return l
982 return l
979
983
980 def unlock():
984 def unlock():
981 self.dirstate.write()
985 self.dirstate.write()
982 ce = self._filecache.get('dirstate')
986 ce = self._filecache.get('dirstate')
983 if ce:
987 if ce:
984 ce.refresh()
988 ce.refresh()
985
989
986 l = self._lock(self.join("wlock"), wait, unlock,
990 l = self._lock(self.join("wlock"), wait, unlock,
987 self.invalidatedirstate, _('working directory of %s') %
991 self.invalidatedirstate, _('working directory of %s') %
988 self.origroot)
992 self.origroot)
989 self._wlockref = weakref.ref(l)
993 self._wlockref = weakref.ref(l)
990 return l
994 return l
991
995
992 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
996 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
993 """
997 """
994 commit an individual file as part of a larger transaction
998 commit an individual file as part of a larger transaction
995 """
999 """
996
1000
997 fname = fctx.path()
1001 fname = fctx.path()
998 text = fctx.data()
1002 text = fctx.data()
999 flog = self.file(fname)
1003 flog = self.file(fname)
1000 fparent1 = manifest1.get(fname, nullid)
1004 fparent1 = manifest1.get(fname, nullid)
1001 fparent2 = fparent2o = manifest2.get(fname, nullid)
1005 fparent2 = fparent2o = manifest2.get(fname, nullid)
1002
1006
1003 meta = {}
1007 meta = {}
1004 copy = fctx.renamed()
1008 copy = fctx.renamed()
1005 if copy and copy[0] != fname:
1009 if copy and copy[0] != fname:
1006 # Mark the new revision of this file as a copy of another
1010 # Mark the new revision of this file as a copy of another
1007 # file. This copy data will effectively act as a parent
1011 # file. This copy data will effectively act as a parent
1008 # of this new revision. If this is a merge, the first
1012 # of this new revision. If this is a merge, the first
1009 # parent will be the nullid (meaning "look up the copy data")
1013 # parent will be the nullid (meaning "look up the copy data")
1010 # and the second one will be the other parent. For example:
1014 # and the second one will be the other parent. For example:
1011 #
1015 #
1012 # 0 --- 1 --- 3 rev1 changes file foo
1016 # 0 --- 1 --- 3 rev1 changes file foo
1013 # \ / rev2 renames foo to bar and changes it
1017 # \ / rev2 renames foo to bar and changes it
1014 # \- 2 -/ rev3 should have bar with all changes and
1018 # \- 2 -/ rev3 should have bar with all changes and
1015 # should record that bar descends from
1019 # should record that bar descends from
1016 # bar in rev2 and foo in rev1
1020 # bar in rev2 and foo in rev1
1017 #
1021 #
1018 # this allows this merge to succeed:
1022 # this allows this merge to succeed:
1019 #
1023 #
1020 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
1024 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
1021 # \ / merging rev3 and rev4 should use bar@rev2
1025 # \ / merging rev3 and rev4 should use bar@rev2
1022 # \- 2 --- 4 as the merge base
1026 # \- 2 --- 4 as the merge base
1023 #
1027 #
1024
1028
1025 cfname = copy[0]
1029 cfname = copy[0]
1026 crev = manifest1.get(cfname)
1030 crev = manifest1.get(cfname)
1027 newfparent = fparent2
1031 newfparent = fparent2
1028
1032
1029 if manifest2: # branch merge
1033 if manifest2: # branch merge
1030 if fparent2 == nullid or crev is None: # copied on remote side
1034 if fparent2 == nullid or crev is None: # copied on remote side
1031 if cfname in manifest2:
1035 if cfname in manifest2:
1032 crev = manifest2[cfname]
1036 crev = manifest2[cfname]
1033 newfparent = fparent1
1037 newfparent = fparent1
1034
1038
1035 # find source in nearest ancestor if we've lost track
1039 # find source in nearest ancestor if we've lost track
1036 if not crev:
1040 if not crev:
1037 self.ui.debug(" %s: searching for copy revision for %s\n" %
1041 self.ui.debug(" %s: searching for copy revision for %s\n" %
1038 (fname, cfname))
1042 (fname, cfname))
1039 for ancestor in self[None].ancestors():
1043 for ancestor in self[None].ancestors():
1040 if cfname in ancestor:
1044 if cfname in ancestor:
1041 crev = ancestor[cfname].filenode()
1045 crev = ancestor[cfname].filenode()
1042 break
1046 break
1043
1047
1044 if crev:
1048 if crev:
1045 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
1049 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
1046 meta["copy"] = cfname
1050 meta["copy"] = cfname
1047 meta["copyrev"] = hex(crev)
1051 meta["copyrev"] = hex(crev)
1048 fparent1, fparent2 = nullid, newfparent
1052 fparent1, fparent2 = nullid, newfparent
1049 else:
1053 else:
1050 self.ui.warn(_("warning: can't find ancestor for '%s' "
1054 self.ui.warn(_("warning: can't find ancestor for '%s' "
1051 "copied from '%s'!\n") % (fname, cfname))
1055 "copied from '%s'!\n") % (fname, cfname))
1052
1056
1053 elif fparent2 != nullid:
1057 elif fparent2 != nullid:
1054 # is one parent an ancestor of the other?
1058 # is one parent an ancestor of the other?
1055 fparentancestor = flog.ancestor(fparent1, fparent2)
1059 fparentancestor = flog.ancestor(fparent1, fparent2)
1056 if fparentancestor == fparent1:
1060 if fparentancestor == fparent1:
1057 fparent1, fparent2 = fparent2, nullid
1061 fparent1, fparent2 = fparent2, nullid
1058 elif fparentancestor == fparent2:
1062 elif fparentancestor == fparent2:
1059 fparent2 = nullid
1063 fparent2 = nullid
1060
1064
1061 # is the file changed?
1065 # is the file changed?
1062 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
1066 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
1063 changelist.append(fname)
1067 changelist.append(fname)
1064 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
1068 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
1065
1069
1066 # are just the flags changed during merge?
1070 # are just the flags changed during merge?
1067 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
1071 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
1068 changelist.append(fname)
1072 changelist.append(fname)
1069
1073
1070 return fparent1
1074 return fparent1
1071
1075
1072 def commit(self, text="", user=None, date=None, match=None, force=False,
1076 def commit(self, text="", user=None, date=None, match=None, force=False,
1073 editor=False, extra={}):
1077 editor=False, extra={}):
1074 """Add a new revision to current repository.
1078 """Add a new revision to current repository.
1075
1079
1076 Revision information is gathered from the working directory,
1080 Revision information is gathered from the working directory,
1077 match can be used to filter the committed files. If editor is
1081 match can be used to filter the committed files. If editor is
1078 supplied, it is called to get a commit message.
1082 supplied, it is called to get a commit message.
1079 """
1083 """
1080
1084
1081 def fail(f, msg):
1085 def fail(f, msg):
1082 raise util.Abort('%s: %s' % (f, msg))
1086 raise util.Abort('%s: %s' % (f, msg))
1083
1087
1084 if not match:
1088 if not match:
1085 match = matchmod.always(self.root, '')
1089 match = matchmod.always(self.root, '')
1086
1090
1087 if not force:
1091 if not force:
1088 vdirs = []
1092 vdirs = []
1089 match.dir = vdirs.append
1093 match.dir = vdirs.append
1090 match.bad = fail
1094 match.bad = fail
1091
1095
1092 wlock = self.wlock()
1096 wlock = self.wlock()
1093 try:
1097 try:
1094 wctx = self[None]
1098 wctx = self[None]
1095 merge = len(wctx.parents()) > 1
1099 merge = len(wctx.parents()) > 1
1096
1100
1097 if (not force and merge and match and
1101 if (not force and merge and match and
1098 (match.files() or match.anypats())):
1102 (match.files() or match.anypats())):
1099 raise util.Abort(_('cannot partially commit a merge '
1103 raise util.Abort(_('cannot partially commit a merge '
1100 '(do not specify files or patterns)'))
1104 '(do not specify files or patterns)'))
1101
1105
1102 changes = self.status(match=match, clean=force)
1106 changes = self.status(match=match, clean=force)
1103 if force:
1107 if force:
1104 changes[0].extend(changes[6]) # mq may commit unchanged files
1108 changes[0].extend(changes[6]) # mq may commit unchanged files
1105
1109
1106 # check subrepos
1110 # check subrepos
1107 subs = []
1111 subs = []
1108 commitsubs = set()
1112 commitsubs = set()
1109 newstate = wctx.substate.copy()
1113 newstate = wctx.substate.copy()
1110 # only manage subrepos and .hgsubstate if .hgsub is present
1114 # only manage subrepos and .hgsubstate if .hgsub is present
1111 if '.hgsub' in wctx:
1115 if '.hgsub' in wctx:
1112 # we'll decide whether to track this ourselves, thanks
1116 # we'll decide whether to track this ourselves, thanks
1113 if '.hgsubstate' in changes[0]:
1117 if '.hgsubstate' in changes[0]:
1114 changes[0].remove('.hgsubstate')
1118 changes[0].remove('.hgsubstate')
1115 if '.hgsubstate' in changes[2]:
1119 if '.hgsubstate' in changes[2]:
1116 changes[2].remove('.hgsubstate')
1120 changes[2].remove('.hgsubstate')
1117
1121
1118 # compare current state to last committed state
1122 # compare current state to last committed state
1119 # build new substate based on last committed state
1123 # build new substate based on last committed state
1120 oldstate = wctx.p1().substate
1124 oldstate = wctx.p1().substate
1121 for s in sorted(newstate.keys()):
1125 for s in sorted(newstate.keys()):
1122 if not match(s):
1126 if not match(s):
1123 # ignore working copy, use old state if present
1127 # ignore working copy, use old state if present
1124 if s in oldstate:
1128 if s in oldstate:
1125 newstate[s] = oldstate[s]
1129 newstate[s] = oldstate[s]
1126 continue
1130 continue
1127 if not force:
1131 if not force:
1128 raise util.Abort(
1132 raise util.Abort(
1129 _("commit with new subrepo %s excluded") % s)
1133 _("commit with new subrepo %s excluded") % s)
1130 if wctx.sub(s).dirty(True):
1134 if wctx.sub(s).dirty(True):
1131 if not self.ui.configbool('ui', 'commitsubrepos'):
1135 if not self.ui.configbool('ui', 'commitsubrepos'):
1132 raise util.Abort(
1136 raise util.Abort(
1133 _("uncommitted changes in subrepo %s") % s,
1137 _("uncommitted changes in subrepo %s") % s,
1134 hint=_("use --subrepos for recursive commit"))
1138 hint=_("use --subrepos for recursive commit"))
1135 subs.append(s)
1139 subs.append(s)
1136 commitsubs.add(s)
1140 commitsubs.add(s)
1137 else:
1141 else:
1138 bs = wctx.sub(s).basestate()
1142 bs = wctx.sub(s).basestate()
1139 newstate[s] = (newstate[s][0], bs, newstate[s][2])
1143 newstate[s] = (newstate[s][0], bs, newstate[s][2])
1140 if oldstate.get(s, (None, None, None))[1] != bs:
1144 if oldstate.get(s, (None, None, None))[1] != bs:
1141 subs.append(s)
1145 subs.append(s)
1142
1146
1143 # check for removed subrepos
1147 # check for removed subrepos
1144 for p in wctx.parents():
1148 for p in wctx.parents():
1145 r = [s for s in p.substate if s not in newstate]
1149 r = [s for s in p.substate if s not in newstate]
1146 subs += [s for s in r if match(s)]
1150 subs += [s for s in r if match(s)]
1147 if subs:
1151 if subs:
1148 if (not match('.hgsub') and
1152 if (not match('.hgsub') and
1149 '.hgsub' in (wctx.modified() + wctx.added())):
1153 '.hgsub' in (wctx.modified() + wctx.added())):
1150 raise util.Abort(
1154 raise util.Abort(
1151 _("can't commit subrepos without .hgsub"))
1155 _("can't commit subrepos without .hgsub"))
1152 changes[0].insert(0, '.hgsubstate')
1156 changes[0].insert(0, '.hgsubstate')
1153
1157
1154 elif '.hgsub' in changes[2]:
1158 elif '.hgsub' in changes[2]:
1155 # clean up .hgsubstate when .hgsub is removed
1159 # clean up .hgsubstate when .hgsub is removed
1156 if ('.hgsubstate' in wctx and
1160 if ('.hgsubstate' in wctx and
1157 '.hgsubstate' not in changes[0] + changes[1] + changes[2]):
1161 '.hgsubstate' not in changes[0] + changes[1] + changes[2]):
1158 changes[2].insert(0, '.hgsubstate')
1162 changes[2].insert(0, '.hgsubstate')
1159
1163
1160 # make sure all explicit patterns are matched
1164 # make sure all explicit patterns are matched
1161 if not force and match.files():
1165 if not force and match.files():
1162 matched = set(changes[0] + changes[1] + changes[2])
1166 matched = set(changes[0] + changes[1] + changes[2])
1163
1167
1164 for f in match.files():
1168 for f in match.files():
1165 if f == '.' or f in matched or f in wctx.substate:
1169 if f == '.' or f in matched or f in wctx.substate:
1166 continue
1170 continue
1167 if f in changes[3]: # missing
1171 if f in changes[3]: # missing
1168 fail(f, _('file not found!'))
1172 fail(f, _('file not found!'))
1169 if f in vdirs: # visited directory
1173 if f in vdirs: # visited directory
1170 d = f + '/'
1174 d = f + '/'
1171 for mf in matched:
1175 for mf in matched:
1172 if mf.startswith(d):
1176 if mf.startswith(d):
1173 break
1177 break
1174 else:
1178 else:
1175 fail(f, _("no match under directory!"))
1179 fail(f, _("no match under directory!"))
1176 elif f not in self.dirstate:
1180 elif f not in self.dirstate:
1177 fail(f, _("file not tracked!"))
1181 fail(f, _("file not tracked!"))
1178
1182
1179 if (not force and not extra.get("close") and not merge
1183 if (not force and not extra.get("close") and not merge
1180 and not (changes[0] or changes[1] or changes[2])
1184 and not (changes[0] or changes[1] or changes[2])
1181 and wctx.branch() == wctx.p1().branch()):
1185 and wctx.branch() == wctx.p1().branch()):
1182 return None
1186 return None
1183
1187
1184 ms = mergemod.mergestate(self)
1188 ms = mergemod.mergestate(self)
1185 for f in changes[0]:
1189 for f in changes[0]:
1186 if f in ms and ms[f] == 'u':
1190 if f in ms and ms[f] == 'u':
1187 raise util.Abort(_("unresolved merge conflicts "
1191 raise util.Abort(_("unresolved merge conflicts "
1188 "(see hg help resolve)"))
1192 "(see hg help resolve)"))
1189
1193
1190 cctx = context.workingctx(self, text, user, date, extra, changes)
1194 cctx = context.workingctx(self, text, user, date, extra, changes)
1191 if editor:
1195 if editor:
1192 cctx._text = editor(self, cctx, subs)
1196 cctx._text = editor(self, cctx, subs)
1193 edited = (text != cctx._text)
1197 edited = (text != cctx._text)
1194
1198
1195 # commit subs and write new state
1199 # commit subs and write new state
1196 if subs:
1200 if subs:
1197 for s in sorted(commitsubs):
1201 for s in sorted(commitsubs):
1198 sub = wctx.sub(s)
1202 sub = wctx.sub(s)
1199 self.ui.status(_('committing subrepository %s\n') %
1203 self.ui.status(_('committing subrepository %s\n') %
1200 subrepo.subrelpath(sub))
1204 subrepo.subrelpath(sub))
1201 sr = sub.commit(cctx._text, user, date)
1205 sr = sub.commit(cctx._text, user, date)
1202 newstate[s] = (newstate[s][0], sr)
1206 newstate[s] = (newstate[s][0], sr)
1203 subrepo.writestate(self, newstate)
1207 subrepo.writestate(self, newstate)
1204
1208
1205 # Save commit message in case this transaction gets rolled back
1209 # Save commit message in case this transaction gets rolled back
1206 # (e.g. by a pretxncommit hook). Leave the content alone on
1210 # (e.g. by a pretxncommit hook). Leave the content alone on
1207 # the assumption that the user will use the same editor again.
1211 # the assumption that the user will use the same editor again.
1208 msgfn = self.savecommitmessage(cctx._text)
1212 msgfn = self.savecommitmessage(cctx._text)
1209
1213
1210 p1, p2 = self.dirstate.parents()
1214 p1, p2 = self.dirstate.parents()
1211 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
1215 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
1212 try:
1216 try:
1213 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
1217 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
1214 ret = self.commitctx(cctx, True)
1218 ret = self.commitctx(cctx, True)
1215 except:
1219 except:
1216 if edited:
1220 if edited:
1217 self.ui.write(
1221 self.ui.write(
1218 _('note: commit message saved in %s\n') % msgfn)
1222 _('note: commit message saved in %s\n') % msgfn)
1219 raise
1223 raise
1220
1224
1221 # update bookmarks, dirstate and mergestate
1225 # update bookmarks, dirstate and mergestate
1222 bookmarks.update(self, p1, ret)
1226 bookmarks.update(self, p1, ret)
1223 for f in changes[0] + changes[1]:
1227 for f in changes[0] + changes[1]:
1224 self.dirstate.normal(f)
1228 self.dirstate.normal(f)
1225 for f in changes[2]:
1229 for f in changes[2]:
1226 self.dirstate.drop(f)
1230 self.dirstate.drop(f)
1227 self.dirstate.setparents(ret)
1231 self.dirstate.setparents(ret)
1228 ms.reset()
1232 ms.reset()
1229 finally:
1233 finally:
1230 wlock.release()
1234 wlock.release()
1231
1235
1232 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
1236 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
1233 return ret
1237 return ret
1234
1238
1235 def commitctx(self, ctx, error=False):
1239 def commitctx(self, ctx, error=False):
1236 """Add a new revision to current repository.
1240 """Add a new revision to current repository.
1237 Revision information is passed via the context argument.
1241 Revision information is passed via the context argument.
1238 """
1242 """
1239
1243
1240 tr = lock = None
1244 tr = lock = None
1241 removed = list(ctx.removed())
1245 removed = list(ctx.removed())
1242 p1, p2 = ctx.p1(), ctx.p2()
1246 p1, p2 = ctx.p1(), ctx.p2()
1243 user = ctx.user()
1247 user = ctx.user()
1244
1248
1245 lock = self.lock()
1249 lock = self.lock()
1246 try:
1250 try:
1247 tr = self.transaction("commit")
1251 tr = self.transaction("commit")
1248 trp = weakref.proxy(tr)
1252 trp = weakref.proxy(tr)
1249
1253
1250 if ctx.files():
1254 if ctx.files():
1251 m1 = p1.manifest().copy()
1255 m1 = p1.manifest().copy()
1252 m2 = p2.manifest()
1256 m2 = p2.manifest()
1253
1257
1254 # check in files
1258 # check in files
1255 new = {}
1259 new = {}
1256 changed = []
1260 changed = []
1257 linkrev = len(self)
1261 linkrev = len(self)
1258 for f in sorted(ctx.modified() + ctx.added()):
1262 for f in sorted(ctx.modified() + ctx.added()):
1259 self.ui.note(f + "\n")
1263 self.ui.note(f + "\n")
1260 try:
1264 try:
1261 fctx = ctx[f]
1265 fctx = ctx[f]
1262 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1266 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1263 changed)
1267 changed)
1264 m1.set(f, fctx.flags())
1268 m1.set(f, fctx.flags())
1265 except OSError, inst:
1269 except OSError, inst:
1266 self.ui.warn(_("trouble committing %s!\n") % f)
1270 self.ui.warn(_("trouble committing %s!\n") % f)
1267 raise
1271 raise
1268 except IOError, inst:
1272 except IOError, inst:
1269 errcode = getattr(inst, 'errno', errno.ENOENT)
1273 errcode = getattr(inst, 'errno', errno.ENOENT)
1270 if error or errcode and errcode != errno.ENOENT:
1274 if error or errcode and errcode != errno.ENOENT:
1271 self.ui.warn(_("trouble committing %s!\n") % f)
1275 self.ui.warn(_("trouble committing %s!\n") % f)
1272 raise
1276 raise
1273 else:
1277 else:
1274 removed.append(f)
1278 removed.append(f)
1275
1279
1276 # update manifest
1280 # update manifest
1277 m1.update(new)
1281 m1.update(new)
1278 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1282 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1279 drop = [f for f in removed if f in m1]
1283 drop = [f for f in removed if f in m1]
1280 for f in drop:
1284 for f in drop:
1281 del m1[f]
1285 del m1[f]
1282 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1286 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1283 p2.manifestnode(), (new, drop))
1287 p2.manifestnode(), (new, drop))
1284 files = changed + removed
1288 files = changed + removed
1285 else:
1289 else:
1286 mn = p1.manifestnode()
1290 mn = p1.manifestnode()
1287 files = []
1291 files = []
1288
1292
1289 # update changelog
1293 # update changelog
1290 self.changelog.delayupdate()
1294 self.changelog.delayupdate()
1291 n = self.changelog.add(mn, files, ctx.description(),
1295 n = self.changelog.add(mn, files, ctx.description(),
1292 trp, p1.node(), p2.node(),
1296 trp, p1.node(), p2.node(),
1293 user, ctx.date(), ctx.extra().copy())
1297 user, ctx.date(), ctx.extra().copy())
1294 p = lambda: self.changelog.writepending() and self.root or ""
1298 p = lambda: self.changelog.writepending() and self.root or ""
1295 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1299 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1296 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1300 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1297 parent2=xp2, pending=p)
1301 parent2=xp2, pending=p)
1298 self.changelog.finalize(trp)
1302 self.changelog.finalize(trp)
1299 # set the new commit is proper phase
1303 # set the new commit is proper phase
1300 targetphase = phases.newcommitphase(self.ui)
1304 targetphase = phases.newcommitphase(self.ui)
1301 if targetphase:
1305 if targetphase:
1302 # retract boundary do not alter parent changeset.
1306 # retract boundary do not alter parent changeset.
1303 # if a parent have higher the resulting phase will
1307 # if a parent have higher the resulting phase will
1304 # be compliant anyway
1308 # be compliant anyway
1305 #
1309 #
1306 # if minimal phase was 0 we don't need to retract anything
1310 # if minimal phase was 0 we don't need to retract anything
1307 phases.retractboundary(self, targetphase, [n])
1311 phases.retractboundary(self, targetphase, [n])
1308 tr.close()
1312 tr.close()
1309 self.updatebranchcache()
1313 self.updatebranchcache()
1310 return n
1314 return n
1311 finally:
1315 finally:
1312 if tr:
1316 if tr:
1313 tr.release()
1317 tr.release()
1314 lock.release()
1318 lock.release()
1315
1319
1316 def destroyed(self):
1320 def destroyed(self):
1317 '''Inform the repository that nodes have been destroyed.
1321 '''Inform the repository that nodes have been destroyed.
1318 Intended for use by strip and rollback, so there's a common
1322 Intended for use by strip and rollback, so there's a common
1319 place for anything that has to be done after destroying history.'''
1323 place for anything that has to be done after destroying history.'''
1320 # XXX it might be nice if we could take the list of destroyed
1324 # XXX it might be nice if we could take the list of destroyed
1321 # nodes, but I don't see an easy way for rollback() to do that
1325 # nodes, but I don't see an easy way for rollback() to do that
1322
1326
1323 # Ensure the persistent tag cache is updated. Doing it now
1327 # Ensure the persistent tag cache is updated. Doing it now
1324 # means that the tag cache only has to worry about destroyed
1328 # means that the tag cache only has to worry about destroyed
1325 # heads immediately after a strip/rollback. That in turn
1329 # heads immediately after a strip/rollback. That in turn
1326 # guarantees that "cachetip == currenttip" (comparing both rev
1330 # guarantees that "cachetip == currenttip" (comparing both rev
1327 # and node) always means no nodes have been added or destroyed.
1331 # and node) always means no nodes have been added or destroyed.
1328
1332
1329 # XXX this is suboptimal when qrefresh'ing: we strip the current
1333 # XXX this is suboptimal when qrefresh'ing: we strip the current
1330 # head, refresh the tag cache, then immediately add a new head.
1334 # head, refresh the tag cache, then immediately add a new head.
1331 # But I think doing it this way is necessary for the "instant
1335 # But I think doing it this way is necessary for the "instant
1332 # tag cache retrieval" case to work.
1336 # tag cache retrieval" case to work.
1333 self.invalidatecaches()
1337 self.invalidatecaches()
1334
1338
1335 # Discard all cache entries to force reloading everything.
1339 # Discard all cache entries to force reloading everything.
1336 self._filecache.clear()
1340 self._filecache.clear()
1337
1341
1338 def walk(self, match, node=None):
1342 def walk(self, match, node=None):
1339 '''
1343 '''
1340 walk recursively through the directory tree or a given
1344 walk recursively through the directory tree or a given
1341 changeset, finding all files matched by the match
1345 changeset, finding all files matched by the match
1342 function
1346 function
1343 '''
1347 '''
1344 return self[node].walk(match)
1348 return self[node].walk(match)
1345
1349
1346 def status(self, node1='.', node2=None, match=None,
1350 def status(self, node1='.', node2=None, match=None,
1347 ignored=False, clean=False, unknown=False,
1351 ignored=False, clean=False, unknown=False,
1348 listsubrepos=False):
1352 listsubrepos=False):
1349 """return status of files between two nodes or node and working directory
1353 """return status of files between two nodes or node and working directory
1350
1354
1351 If node1 is None, use the first dirstate parent instead.
1355 If node1 is None, use the first dirstate parent instead.
1352 If node2 is None, compare node1 with working directory.
1356 If node2 is None, compare node1 with working directory.
1353 """
1357 """
1354
1358
1355 def mfmatches(ctx):
1359 def mfmatches(ctx):
1356 mf = ctx.manifest().copy()
1360 mf = ctx.manifest().copy()
1357 for fn in mf.keys():
1361 for fn in mf.keys():
1358 if not match(fn):
1362 if not match(fn):
1359 del mf[fn]
1363 del mf[fn]
1360 return mf
1364 return mf
1361
1365
1362 if isinstance(node1, context.changectx):
1366 if isinstance(node1, context.changectx):
1363 ctx1 = node1
1367 ctx1 = node1
1364 else:
1368 else:
1365 ctx1 = self[node1]
1369 ctx1 = self[node1]
1366 if isinstance(node2, context.changectx):
1370 if isinstance(node2, context.changectx):
1367 ctx2 = node2
1371 ctx2 = node2
1368 else:
1372 else:
1369 ctx2 = self[node2]
1373 ctx2 = self[node2]
1370
1374
1371 working = ctx2.rev() is None
1375 working = ctx2.rev() is None
1372 parentworking = working and ctx1 == self['.']
1376 parentworking = working and ctx1 == self['.']
1373 match = match or matchmod.always(self.root, self.getcwd())
1377 match = match or matchmod.always(self.root, self.getcwd())
1374 listignored, listclean, listunknown = ignored, clean, unknown
1378 listignored, listclean, listunknown = ignored, clean, unknown
1375
1379
1376 # load earliest manifest first for caching reasons
1380 # load earliest manifest first for caching reasons
1377 if not working and ctx2.rev() < ctx1.rev():
1381 if not working and ctx2.rev() < ctx1.rev():
1378 ctx2.manifest()
1382 ctx2.manifest()
1379
1383
1380 if not parentworking:
1384 if not parentworking:
1381 def bad(f, msg):
1385 def bad(f, msg):
1382 # 'f' may be a directory pattern from 'match.files()',
1386 # 'f' may be a directory pattern from 'match.files()',
1383 # so 'f not in ctx1' is not enough
1387 # so 'f not in ctx1' is not enough
1384 if f not in ctx1 and f not in ctx1.dirs():
1388 if f not in ctx1 and f not in ctx1.dirs():
1385 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1389 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1386 match.bad = bad
1390 match.bad = bad
1387
1391
1388 if working: # we need to scan the working dir
1392 if working: # we need to scan the working dir
1389 subrepos = []
1393 subrepos = []
1390 if '.hgsub' in self.dirstate:
1394 if '.hgsub' in self.dirstate:
1391 subrepos = ctx2.substate.keys()
1395 subrepos = ctx2.substate.keys()
1392 s = self.dirstate.status(match, subrepos, listignored,
1396 s = self.dirstate.status(match, subrepos, listignored,
1393 listclean, listunknown)
1397 listclean, listunknown)
1394 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1398 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1395
1399
1396 # check for any possibly clean files
1400 # check for any possibly clean files
1397 if parentworking and cmp:
1401 if parentworking and cmp:
1398 fixup = []
1402 fixup = []
1399 # do a full compare of any files that might have changed
1403 # do a full compare of any files that might have changed
1400 for f in sorted(cmp):
1404 for f in sorted(cmp):
1401 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1405 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1402 or ctx1[f].cmp(ctx2[f])):
1406 or ctx1[f].cmp(ctx2[f])):
1403 modified.append(f)
1407 modified.append(f)
1404 else:
1408 else:
1405 fixup.append(f)
1409 fixup.append(f)
1406
1410
1407 # update dirstate for files that are actually clean
1411 # update dirstate for files that are actually clean
1408 if fixup:
1412 if fixup:
1409 if listclean:
1413 if listclean:
1410 clean += fixup
1414 clean += fixup
1411
1415
1412 try:
1416 try:
1413 # updating the dirstate is optional
1417 # updating the dirstate is optional
1414 # so we don't wait on the lock
1418 # so we don't wait on the lock
1415 wlock = self.wlock(False)
1419 wlock = self.wlock(False)
1416 try:
1420 try:
1417 for f in fixup:
1421 for f in fixup:
1418 self.dirstate.normal(f)
1422 self.dirstate.normal(f)
1419 finally:
1423 finally:
1420 wlock.release()
1424 wlock.release()
1421 except error.LockError:
1425 except error.LockError:
1422 pass
1426 pass
1423
1427
1424 if not parentworking:
1428 if not parentworking:
1425 mf1 = mfmatches(ctx1)
1429 mf1 = mfmatches(ctx1)
1426 if working:
1430 if working:
1427 # we are comparing working dir against non-parent
1431 # we are comparing working dir against non-parent
1428 # generate a pseudo-manifest for the working dir
1432 # generate a pseudo-manifest for the working dir
1429 mf2 = mfmatches(self['.'])
1433 mf2 = mfmatches(self['.'])
1430 for f in cmp + modified + added:
1434 for f in cmp + modified + added:
1431 mf2[f] = None
1435 mf2[f] = None
1432 mf2.set(f, ctx2.flags(f))
1436 mf2.set(f, ctx2.flags(f))
1433 for f in removed:
1437 for f in removed:
1434 if f in mf2:
1438 if f in mf2:
1435 del mf2[f]
1439 del mf2[f]
1436 else:
1440 else:
1437 # we are comparing two revisions
1441 # we are comparing two revisions
1438 deleted, unknown, ignored = [], [], []
1442 deleted, unknown, ignored = [], [], []
1439 mf2 = mfmatches(ctx2)
1443 mf2 = mfmatches(ctx2)
1440
1444
1441 modified, added, clean = [], [], []
1445 modified, added, clean = [], [], []
1442 for fn in mf2:
1446 for fn in mf2:
1443 if fn in mf1:
1447 if fn in mf1:
1444 if (fn not in deleted and
1448 if (fn not in deleted and
1445 (mf1.flags(fn) != mf2.flags(fn) or
1449 (mf1.flags(fn) != mf2.flags(fn) or
1446 (mf1[fn] != mf2[fn] and
1450 (mf1[fn] != mf2[fn] and
1447 (mf2[fn] or ctx1[fn].cmp(ctx2[fn]))))):
1451 (mf2[fn] or ctx1[fn].cmp(ctx2[fn]))))):
1448 modified.append(fn)
1452 modified.append(fn)
1449 elif listclean:
1453 elif listclean:
1450 clean.append(fn)
1454 clean.append(fn)
1451 del mf1[fn]
1455 del mf1[fn]
1452 elif fn not in deleted:
1456 elif fn not in deleted:
1453 added.append(fn)
1457 added.append(fn)
1454 removed = mf1.keys()
1458 removed = mf1.keys()
1455
1459
1456 if working and modified and not self.dirstate._checklink:
1460 if working and modified and not self.dirstate._checklink:
1457 # Symlink placeholders may get non-symlink-like contents
1461 # Symlink placeholders may get non-symlink-like contents
1458 # via user error or dereferencing by NFS or Samba servers,
1462 # via user error or dereferencing by NFS or Samba servers,
1459 # so we filter out any placeholders that don't look like a
1463 # so we filter out any placeholders that don't look like a
1460 # symlink
1464 # symlink
1461 sane = []
1465 sane = []
1462 for f in modified:
1466 for f in modified:
1463 if ctx2.flags(f) == 'l':
1467 if ctx2.flags(f) == 'l':
1464 d = ctx2[f].data()
1468 d = ctx2[f].data()
1465 if len(d) >= 1024 or '\n' in d or util.binary(d):
1469 if len(d) >= 1024 or '\n' in d or util.binary(d):
1466 self.ui.debug('ignoring suspect symlink placeholder'
1470 self.ui.debug('ignoring suspect symlink placeholder'
1467 ' "%s"\n' % f)
1471 ' "%s"\n' % f)
1468 continue
1472 continue
1469 sane.append(f)
1473 sane.append(f)
1470 modified = sane
1474 modified = sane
1471
1475
1472 r = modified, added, removed, deleted, unknown, ignored, clean
1476 r = modified, added, removed, deleted, unknown, ignored, clean
1473
1477
1474 if listsubrepos:
1478 if listsubrepos:
1475 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1479 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1476 if working:
1480 if working:
1477 rev2 = None
1481 rev2 = None
1478 else:
1482 else:
1479 rev2 = ctx2.substate[subpath][1]
1483 rev2 = ctx2.substate[subpath][1]
1480 try:
1484 try:
1481 submatch = matchmod.narrowmatcher(subpath, match)
1485 submatch = matchmod.narrowmatcher(subpath, match)
1482 s = sub.status(rev2, match=submatch, ignored=listignored,
1486 s = sub.status(rev2, match=submatch, ignored=listignored,
1483 clean=listclean, unknown=listunknown,
1487 clean=listclean, unknown=listunknown,
1484 listsubrepos=True)
1488 listsubrepos=True)
1485 for rfiles, sfiles in zip(r, s):
1489 for rfiles, sfiles in zip(r, s):
1486 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1490 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1487 except error.LookupError:
1491 except error.LookupError:
1488 self.ui.status(_("skipping missing subrepository: %s\n")
1492 self.ui.status(_("skipping missing subrepository: %s\n")
1489 % subpath)
1493 % subpath)
1490
1494
1491 for l in r:
1495 for l in r:
1492 l.sort()
1496 l.sort()
1493 return r
1497 return r
1494
1498
1495 def heads(self, start=None):
1499 def heads(self, start=None):
1496 heads = self.changelog.heads(start)
1500 heads = self.changelog.heads(start)
1497 # sort the output in rev descending order
1501 # sort the output in rev descending order
1498 return sorted(heads, key=self.changelog.rev, reverse=True)
1502 return sorted(heads, key=self.changelog.rev, reverse=True)
1499
1503
1500 def branchheads(self, branch=None, start=None, closed=False):
1504 def branchheads(self, branch=None, start=None, closed=False):
1501 '''return a (possibly filtered) list of heads for the given branch
1505 '''return a (possibly filtered) list of heads for the given branch
1502
1506
1503 Heads are returned in topological order, from newest to oldest.
1507 Heads are returned in topological order, from newest to oldest.
1504 If branch is None, use the dirstate branch.
1508 If branch is None, use the dirstate branch.
1505 If start is not None, return only heads reachable from start.
1509 If start is not None, return only heads reachable from start.
1506 If closed is True, return heads that are marked as closed as well.
1510 If closed is True, return heads that are marked as closed as well.
1507 '''
1511 '''
1508 if branch is None:
1512 if branch is None:
1509 branch = self[None].branch()
1513 branch = self[None].branch()
1510 branches = self.branchmap()
1514 branches = self.branchmap()
1511 if branch not in branches:
1515 if branch not in branches:
1512 return []
1516 return []
1513 # the cache returns heads ordered lowest to highest
1517 # the cache returns heads ordered lowest to highest
1514 bheads = list(reversed(branches[branch]))
1518 bheads = list(reversed(branches[branch]))
1515 if start is not None:
1519 if start is not None:
1516 # filter out the heads that cannot be reached from startrev
1520 # filter out the heads that cannot be reached from startrev
1517 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1521 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1518 bheads = [h for h in bheads if h in fbheads]
1522 bheads = [h for h in bheads if h in fbheads]
1519 if not closed:
1523 if not closed:
1520 bheads = [h for h in bheads if
1524 bheads = [h for h in bheads if
1521 ('close' not in self.changelog.read(h)[5])]
1525 ('close' not in self.changelog.read(h)[5])]
1522 return bheads
1526 return bheads
1523
1527
1524 def branches(self, nodes):
1528 def branches(self, nodes):
1525 if not nodes:
1529 if not nodes:
1526 nodes = [self.changelog.tip()]
1530 nodes = [self.changelog.tip()]
1527 b = []
1531 b = []
1528 for n in nodes:
1532 for n in nodes:
1529 t = n
1533 t = n
1530 while True:
1534 while True:
1531 p = self.changelog.parents(n)
1535 p = self.changelog.parents(n)
1532 if p[1] != nullid or p[0] == nullid:
1536 if p[1] != nullid or p[0] == nullid:
1533 b.append((t, n, p[0], p[1]))
1537 b.append((t, n, p[0], p[1]))
1534 break
1538 break
1535 n = p[0]
1539 n = p[0]
1536 return b
1540 return b
1537
1541
1538 def between(self, pairs):
1542 def between(self, pairs):
1539 r = []
1543 r = []
1540
1544
1541 for top, bottom in pairs:
1545 for top, bottom in pairs:
1542 n, l, i = top, [], 0
1546 n, l, i = top, [], 0
1543 f = 1
1547 f = 1
1544
1548
1545 while n != bottom and n != nullid:
1549 while n != bottom and n != nullid:
1546 p = self.changelog.parents(n)[0]
1550 p = self.changelog.parents(n)[0]
1547 if i == f:
1551 if i == f:
1548 l.append(n)
1552 l.append(n)
1549 f = f * 2
1553 f = f * 2
1550 n = p
1554 n = p
1551 i += 1
1555 i += 1
1552
1556
1553 r.append(l)
1557 r.append(l)
1554
1558
1555 return r
1559 return r
1556
1560
1557 def pull(self, remote, heads=None, force=False):
1561 def pull(self, remote, heads=None, force=False):
1558 lock = self.lock()
1562 lock = self.lock()
1559 try:
1563 try:
1560 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1564 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1561 force=force)
1565 force=force)
1562 common, fetch, rheads = tmp
1566 common, fetch, rheads = tmp
1563 if not fetch:
1567 if not fetch:
1564 self.ui.status(_("no changes found\n"))
1568 self.ui.status(_("no changes found\n"))
1565 added = []
1569 added = []
1566 result = 0
1570 result = 0
1567 else:
1571 else:
1568 if heads is None and list(common) == [nullid]:
1572 if heads is None and list(common) == [nullid]:
1569 self.ui.status(_("requesting all changes\n"))
1573 self.ui.status(_("requesting all changes\n"))
1570 elif heads is None and remote.capable('changegroupsubset'):
1574 elif heads is None and remote.capable('changegroupsubset'):
1571 # issue1320, avoid a race if remote changed after discovery
1575 # issue1320, avoid a race if remote changed after discovery
1572 heads = rheads
1576 heads = rheads
1573
1577
1574 if remote.capable('getbundle'):
1578 if remote.capable('getbundle'):
1575 cg = remote.getbundle('pull', common=common,
1579 cg = remote.getbundle('pull', common=common,
1576 heads=heads or rheads)
1580 heads=heads or rheads)
1577 elif heads is None:
1581 elif heads is None:
1578 cg = remote.changegroup(fetch, 'pull')
1582 cg = remote.changegroup(fetch, 'pull')
1579 elif not remote.capable('changegroupsubset'):
1583 elif not remote.capable('changegroupsubset'):
1580 raise util.Abort(_("partial pull cannot be done because "
1584 raise util.Abort(_("partial pull cannot be done because "
1581 "other repository doesn't support "
1585 "other repository doesn't support "
1582 "changegroupsubset."))
1586 "changegroupsubset."))
1583 else:
1587 else:
1584 cg = remote.changegroupsubset(fetch, heads, 'pull')
1588 cg = remote.changegroupsubset(fetch, heads, 'pull')
1585 clstart = len(self.changelog)
1589 clstart = len(self.changelog)
1586 result = self.addchangegroup(cg, 'pull', remote.url())
1590 result = self.addchangegroup(cg, 'pull', remote.url())
1587 clend = len(self.changelog)
1591 clend = len(self.changelog)
1588 added = [self.changelog.node(r) for r in xrange(clstart, clend)]
1592 added = [self.changelog.node(r) for r in xrange(clstart, clend)]
1589
1593
1590 # compute target subset
1594 # compute target subset
1591 if heads is None:
1595 if heads is None:
1592 # We pulled every thing possible
1596 # We pulled every thing possible
1593 # sync on everything common
1597 # sync on everything common
1594 subset = common + added
1598 subset = common + added
1595 else:
1599 else:
1596 # We pulled a specific subset
1600 # We pulled a specific subset
1597 # sync on this subset
1601 # sync on this subset
1598 subset = heads
1602 subset = heads
1599
1603
1600 # Get remote phases data from remote
1604 # Get remote phases data from remote
1601 remotephases = remote.listkeys('phases')
1605 remotephases = remote.listkeys('phases')
1602 publishing = bool(remotephases.get('publishing', False))
1606 publishing = bool(remotephases.get('publishing', False))
1603 if remotephases and not publishing:
1607 if remotephases and not publishing:
1604 # remote is new and unpublishing
1608 # remote is new and unpublishing
1605 pheads, _dr = phases.analyzeremotephases(self, subset,
1609 pheads, _dr = phases.analyzeremotephases(self, subset,
1606 remotephases)
1610 remotephases)
1607 phases.advanceboundary(self, phases.public, pheads)
1611 phases.advanceboundary(self, phases.public, pheads)
1608 phases.advanceboundary(self, phases.draft, subset)
1612 phases.advanceboundary(self, phases.draft, subset)
1609 else:
1613 else:
1610 # Remote is old or publishing all common changesets
1614 # Remote is old or publishing all common changesets
1611 # should be seen as public
1615 # should be seen as public
1612 phases.advanceboundary(self, phases.public, subset)
1616 phases.advanceboundary(self, phases.public, subset)
1613 finally:
1617 finally:
1614 lock.release()
1618 lock.release()
1615
1619
1616 return result
1620 return result
1617
1621
1618 def checkpush(self, force, revs):
1622 def checkpush(self, force, revs):
1619 """Extensions can override this function if additional checks have
1623 """Extensions can override this function if additional checks have
1620 to be performed before pushing, or call it if they override push
1624 to be performed before pushing, or call it if they override push
1621 command.
1625 command.
1622 """
1626 """
1623 pass
1627 pass
1624
1628
1625 def push(self, remote, force=False, revs=None, newbranch=False):
1629 def push(self, remote, force=False, revs=None, newbranch=False):
1626 '''Push outgoing changesets (limited by revs) from the current
1630 '''Push outgoing changesets (limited by revs) from the current
1627 repository to remote. Return an integer:
1631 repository to remote. Return an integer:
1628 - None means nothing to push
1632 - None means nothing to push
1629 - 0 means HTTP error
1633 - 0 means HTTP error
1630 - 1 means we pushed and remote head count is unchanged *or*
1634 - 1 means we pushed and remote head count is unchanged *or*
1631 we have outgoing changesets but refused to push
1635 we have outgoing changesets but refused to push
1632 - other values as described by addchangegroup()
1636 - other values as described by addchangegroup()
1633 '''
1637 '''
1634 # there are two ways to push to remote repo:
1638 # there are two ways to push to remote repo:
1635 #
1639 #
1636 # addchangegroup assumes local user can lock remote
1640 # addchangegroup assumes local user can lock remote
1637 # repo (local filesystem, old ssh servers).
1641 # repo (local filesystem, old ssh servers).
1638 #
1642 #
1639 # unbundle assumes local user cannot lock remote repo (new ssh
1643 # unbundle assumes local user cannot lock remote repo (new ssh
1640 # servers, http servers).
1644 # servers, http servers).
1641
1645
1642 # get local lock as we might write phase data
1646 # get local lock as we might write phase data
1643 locallock = self.lock()
1647 locallock = self.lock()
1644 try:
1648 try:
1645 self.checkpush(force, revs)
1649 self.checkpush(force, revs)
1646 lock = None
1650 lock = None
1647 unbundle = remote.capable('unbundle')
1651 unbundle = remote.capable('unbundle')
1648 if not unbundle:
1652 if not unbundle:
1649 lock = remote.lock()
1653 lock = remote.lock()
1650 try:
1654 try:
1651 # discovery
1655 # discovery
1652 fci = discovery.findcommonincoming
1656 fci = discovery.findcommonincoming
1653 commoninc = fci(self, remote, force=force)
1657 commoninc = fci(self, remote, force=force)
1654 common, inc, remoteheads = commoninc
1658 common, inc, remoteheads = commoninc
1655 fco = discovery.findcommonoutgoing
1659 fco = discovery.findcommonoutgoing
1656 outgoing = fco(self, remote, onlyheads=revs,
1660 outgoing = fco(self, remote, onlyheads=revs,
1657 commoninc=commoninc, force=force)
1661 commoninc=commoninc, force=force)
1658
1662
1659
1663
1660 if not outgoing.missing:
1664 if not outgoing.missing:
1661 # nothing to push
1665 # nothing to push
1662 scmutil.nochangesfound(self.ui, outgoing.excluded)
1666 scmutil.nochangesfound(self.ui, outgoing.excluded)
1663 ret = None
1667 ret = None
1664 else:
1668 else:
1665 # something to push
1669 # something to push
1666 if not force:
1670 if not force:
1667 discovery.checkheads(self, remote, outgoing,
1671 discovery.checkheads(self, remote, outgoing,
1668 remoteheads, newbranch,
1672 remoteheads, newbranch,
1669 bool(inc))
1673 bool(inc))
1670
1674
1671 # create a changegroup from local
1675 # create a changegroup from local
1672 if revs is None and not outgoing.excluded:
1676 if revs is None and not outgoing.excluded:
1673 # push everything,
1677 # push everything,
1674 # use the fast path, no race possible on push
1678 # use the fast path, no race possible on push
1675 cg = self._changegroup(outgoing.missing, 'push')
1679 cg = self._changegroup(outgoing.missing, 'push')
1676 else:
1680 else:
1677 cg = self.getlocalbundle('push', outgoing)
1681 cg = self.getlocalbundle('push', outgoing)
1678
1682
1679 # apply changegroup to remote
1683 # apply changegroup to remote
1680 if unbundle:
1684 if unbundle:
1681 # local repo finds heads on server, finds out what
1685 # local repo finds heads on server, finds out what
1682 # revs it must push. once revs transferred, if server
1686 # revs it must push. once revs transferred, if server
1683 # finds it has different heads (someone else won
1687 # finds it has different heads (someone else won
1684 # commit/push race), server aborts.
1688 # commit/push race), server aborts.
1685 if force:
1689 if force:
1686 remoteheads = ['force']
1690 remoteheads = ['force']
1687 # ssh: return remote's addchangegroup()
1691 # ssh: return remote's addchangegroup()
1688 # http: return remote's addchangegroup() or 0 for error
1692 # http: return remote's addchangegroup() or 0 for error
1689 ret = remote.unbundle(cg, remoteheads, 'push')
1693 ret = remote.unbundle(cg, remoteheads, 'push')
1690 else:
1694 else:
1691 # we return an integer indicating remote head count change
1695 # we return an integer indicating remote head count change
1692 ret = remote.addchangegroup(cg, 'push', self.url())
1696 ret = remote.addchangegroup(cg, 'push', self.url())
1693
1697
1694 if ret:
1698 if ret:
1695 # push succeed, synchonize target of the push
1699 # push succeed, synchonize target of the push
1696 cheads = outgoing.missingheads
1700 cheads = outgoing.missingheads
1697 elif revs is None:
1701 elif revs is None:
1698 # All out push fails. synchronize all common
1702 # All out push fails. synchronize all common
1699 cheads = outgoing.commonheads
1703 cheads = outgoing.commonheads
1700 else:
1704 else:
1701 # I want cheads = heads(::missingheads and ::commonheads)
1705 # I want cheads = heads(::missingheads and ::commonheads)
1702 # (missingheads is revs with secret changeset filtered out)
1706 # (missingheads is revs with secret changeset filtered out)
1703 #
1707 #
1704 # This can be expressed as:
1708 # This can be expressed as:
1705 # cheads = ( (missingheads and ::commonheads)
1709 # cheads = ( (missingheads and ::commonheads)
1706 # + (commonheads and ::missingheads))"
1710 # + (commonheads and ::missingheads))"
1707 # )
1711 # )
1708 #
1712 #
1709 # while trying to push we already computed the following:
1713 # while trying to push we already computed the following:
1710 # common = (::commonheads)
1714 # common = (::commonheads)
1711 # missing = ((commonheads::missingheads) - commonheads)
1715 # missing = ((commonheads::missingheads) - commonheads)
1712 #
1716 #
1713 # We can pick:
1717 # We can pick:
1714 # * missingheads part of comon (::commonheads)
1718 # * missingheads part of comon (::commonheads)
1715 common = set(outgoing.common)
1719 common = set(outgoing.common)
1716 cheads = [node for node in revs if node in common]
1720 cheads = [node for node in revs if node in common]
1717 # and
1721 # and
1718 # * commonheads parents on missing
1722 # * commonheads parents on missing
1719 revset = self.set('%ln and parents(roots(%ln))',
1723 revset = self.set('%ln and parents(roots(%ln))',
1720 outgoing.commonheads,
1724 outgoing.commonheads,
1721 outgoing.missing)
1725 outgoing.missing)
1722 cheads.extend(c.node() for c in revset)
1726 cheads.extend(c.node() for c in revset)
1723 # even when we don't push, exchanging phase data is useful
1727 # even when we don't push, exchanging phase data is useful
1724 remotephases = remote.listkeys('phases')
1728 remotephases = remote.listkeys('phases')
1725 if not remotephases: # old server or public only repo
1729 if not remotephases: # old server or public only repo
1726 phases.advanceboundary(self, phases.public, cheads)
1730 phases.advanceboundary(self, phases.public, cheads)
1727 # don't push any phase data as there is nothing to push
1731 # don't push any phase data as there is nothing to push
1728 else:
1732 else:
1729 ana = phases.analyzeremotephases(self, cheads, remotephases)
1733 ana = phases.analyzeremotephases(self, cheads, remotephases)
1730 pheads, droots = ana
1734 pheads, droots = ana
1731 ### Apply remote phase on local
1735 ### Apply remote phase on local
1732 if remotephases.get('publishing', False):
1736 if remotephases.get('publishing', False):
1733 phases.advanceboundary(self, phases.public, cheads)
1737 phases.advanceboundary(self, phases.public, cheads)
1734 else: # publish = False
1738 else: # publish = False
1735 phases.advanceboundary(self, phases.public, pheads)
1739 phases.advanceboundary(self, phases.public, pheads)
1736 phases.advanceboundary(self, phases.draft, cheads)
1740 phases.advanceboundary(self, phases.draft, cheads)
1737 ### Apply local phase on remote
1741 ### Apply local phase on remote
1738
1742
1739 # Get the list of all revs draft on remote by public here.
1743 # Get the list of all revs draft on remote by public here.
1740 # XXX Beware that revset break if droots is not strictly
1744 # XXX Beware that revset break if droots is not strictly
1741 # XXX root we may want to ensure it is but it is costly
1745 # XXX root we may want to ensure it is but it is costly
1742 outdated = self.set('heads((%ln::%ln) and public())',
1746 outdated = self.set('heads((%ln::%ln) and public())',
1743 droots, cheads)
1747 droots, cheads)
1744 for newremotehead in outdated:
1748 for newremotehead in outdated:
1745 r = remote.pushkey('phases',
1749 r = remote.pushkey('phases',
1746 newremotehead.hex(),
1750 newremotehead.hex(),
1747 str(phases.draft),
1751 str(phases.draft),
1748 str(phases.public))
1752 str(phases.public))
1749 if not r:
1753 if not r:
1750 self.ui.warn(_('updating %s to public failed!\n')
1754 self.ui.warn(_('updating %s to public failed!\n')
1751 % newremotehead)
1755 % newremotehead)
1752 finally:
1756 finally:
1753 if lock is not None:
1757 if lock is not None:
1754 lock.release()
1758 lock.release()
1755 finally:
1759 finally:
1756 locallock.release()
1760 locallock.release()
1757
1761
1758 self.ui.debug("checking for updated bookmarks\n")
1762 self.ui.debug("checking for updated bookmarks\n")
1759 rb = remote.listkeys('bookmarks')
1763 rb = remote.listkeys('bookmarks')
1760 for k in rb.keys():
1764 for k in rb.keys():
1761 if k in self._bookmarks:
1765 if k in self._bookmarks:
1762 nr, nl = rb[k], hex(self._bookmarks[k])
1766 nr, nl = rb[k], hex(self._bookmarks[k])
1763 if nr in self:
1767 if nr in self:
1764 cr = self[nr]
1768 cr = self[nr]
1765 cl = self[nl]
1769 cl = self[nl]
1766 if cl in cr.descendants():
1770 if cl in cr.descendants():
1767 r = remote.pushkey('bookmarks', k, nr, nl)
1771 r = remote.pushkey('bookmarks', k, nr, nl)
1768 if r:
1772 if r:
1769 self.ui.status(_("updating bookmark %s\n") % k)
1773 self.ui.status(_("updating bookmark %s\n") % k)
1770 else:
1774 else:
1771 self.ui.warn(_('updating bookmark %s'
1775 self.ui.warn(_('updating bookmark %s'
1772 ' failed!\n') % k)
1776 ' failed!\n') % k)
1773
1777
1774 return ret
1778 return ret
1775
1779
1776 def changegroupinfo(self, nodes, source):
1780 def changegroupinfo(self, nodes, source):
1777 if self.ui.verbose or source == 'bundle':
1781 if self.ui.verbose or source == 'bundle':
1778 self.ui.status(_("%d changesets found\n") % len(nodes))
1782 self.ui.status(_("%d changesets found\n") % len(nodes))
1779 if self.ui.debugflag:
1783 if self.ui.debugflag:
1780 self.ui.debug("list of changesets:\n")
1784 self.ui.debug("list of changesets:\n")
1781 for node in nodes:
1785 for node in nodes:
1782 self.ui.debug("%s\n" % hex(node))
1786 self.ui.debug("%s\n" % hex(node))
1783
1787
1784 def changegroupsubset(self, bases, heads, source):
1788 def changegroupsubset(self, bases, heads, source):
1785 """Compute a changegroup consisting of all the nodes that are
1789 """Compute a changegroup consisting of all the nodes that are
1786 descendants of any of the bases and ancestors of any of the heads.
1790 descendants of any of the bases and ancestors of any of the heads.
1787 Return a chunkbuffer object whose read() method will return
1791 Return a chunkbuffer object whose read() method will return
1788 successive changegroup chunks.
1792 successive changegroup chunks.
1789
1793
1790 It is fairly complex as determining which filenodes and which
1794 It is fairly complex as determining which filenodes and which
1791 manifest nodes need to be included for the changeset to be complete
1795 manifest nodes need to be included for the changeset to be complete
1792 is non-trivial.
1796 is non-trivial.
1793
1797
1794 Another wrinkle is doing the reverse, figuring out which changeset in
1798 Another wrinkle is doing the reverse, figuring out which changeset in
1795 the changegroup a particular filenode or manifestnode belongs to.
1799 the changegroup a particular filenode or manifestnode belongs to.
1796 """
1800 """
1797 cl = self.changelog
1801 cl = self.changelog
1798 if not bases:
1802 if not bases:
1799 bases = [nullid]
1803 bases = [nullid]
1800 csets, bases, heads = cl.nodesbetween(bases, heads)
1804 csets, bases, heads = cl.nodesbetween(bases, heads)
1801 # We assume that all ancestors of bases are known
1805 # We assume that all ancestors of bases are known
1802 common = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1806 common = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1803 return self._changegroupsubset(common, csets, heads, source)
1807 return self._changegroupsubset(common, csets, heads, source)
1804
1808
1805 def getlocalbundle(self, source, outgoing):
1809 def getlocalbundle(self, source, outgoing):
1806 """Like getbundle, but taking a discovery.outgoing as an argument.
1810 """Like getbundle, but taking a discovery.outgoing as an argument.
1807
1811
1808 This is only implemented for local repos and reuses potentially
1812 This is only implemented for local repos and reuses potentially
1809 precomputed sets in outgoing."""
1813 precomputed sets in outgoing."""
1810 if not outgoing.missing:
1814 if not outgoing.missing:
1811 return None
1815 return None
1812 return self._changegroupsubset(outgoing.common,
1816 return self._changegroupsubset(outgoing.common,
1813 outgoing.missing,
1817 outgoing.missing,
1814 outgoing.missingheads,
1818 outgoing.missingheads,
1815 source)
1819 source)
1816
1820
1817 def getbundle(self, source, heads=None, common=None):
1821 def getbundle(self, source, heads=None, common=None):
1818 """Like changegroupsubset, but returns the set difference between the
1822 """Like changegroupsubset, but returns the set difference between the
1819 ancestors of heads and the ancestors common.
1823 ancestors of heads and the ancestors common.
1820
1824
1821 If heads is None, use the local heads. If common is None, use [nullid].
1825 If heads is None, use the local heads. If common is None, use [nullid].
1822
1826
1823 The nodes in common might not all be known locally due to the way the
1827 The nodes in common might not all be known locally due to the way the
1824 current discovery protocol works.
1828 current discovery protocol works.
1825 """
1829 """
1826 cl = self.changelog
1830 cl = self.changelog
1827 if common:
1831 if common:
1828 nm = cl.nodemap
1832 nm = cl.nodemap
1829 common = [n for n in common if n in nm]
1833 common = [n for n in common if n in nm]
1830 else:
1834 else:
1831 common = [nullid]
1835 common = [nullid]
1832 if not heads:
1836 if not heads:
1833 heads = cl.heads()
1837 heads = cl.heads()
1834 return self.getlocalbundle(source,
1838 return self.getlocalbundle(source,
1835 discovery.outgoing(cl, common, heads))
1839 discovery.outgoing(cl, common, heads))
1836
1840
1837 def _changegroupsubset(self, commonrevs, csets, heads, source):
1841 def _changegroupsubset(self, commonrevs, csets, heads, source):
1838
1842
1839 cl = self.changelog
1843 cl = self.changelog
1840 mf = self.manifest
1844 mf = self.manifest
1841 mfs = {} # needed manifests
1845 mfs = {} # needed manifests
1842 fnodes = {} # needed file nodes
1846 fnodes = {} # needed file nodes
1843 changedfiles = set()
1847 changedfiles = set()
1844 fstate = ['', {}]
1848 fstate = ['', {}]
1845 count = [0]
1849 count = [0]
1846
1850
1847 # can we go through the fast path ?
1851 # can we go through the fast path ?
1848 heads.sort()
1852 heads.sort()
1849 if heads == sorted(self.heads()):
1853 if heads == sorted(self.heads()):
1850 return self._changegroup(csets, source)
1854 return self._changegroup(csets, source)
1851
1855
1852 # slow path
1856 # slow path
1853 self.hook('preoutgoing', throw=True, source=source)
1857 self.hook('preoutgoing', throw=True, source=source)
1854 self.changegroupinfo(csets, source)
1858 self.changegroupinfo(csets, source)
1855
1859
1856 # filter any nodes that claim to be part of the known set
1860 # filter any nodes that claim to be part of the known set
1857 def prune(revlog, missing):
1861 def prune(revlog, missing):
1858 return [n for n in missing
1862 return [n for n in missing
1859 if revlog.linkrev(revlog.rev(n)) not in commonrevs]
1863 if revlog.linkrev(revlog.rev(n)) not in commonrevs]
1860
1864
1861 def lookup(revlog, x):
1865 def lookup(revlog, x):
1862 if revlog == cl:
1866 if revlog == cl:
1863 c = cl.read(x)
1867 c = cl.read(x)
1864 changedfiles.update(c[3])
1868 changedfiles.update(c[3])
1865 mfs.setdefault(c[0], x)
1869 mfs.setdefault(c[0], x)
1866 count[0] += 1
1870 count[0] += 1
1867 self.ui.progress(_('bundling'), count[0],
1871 self.ui.progress(_('bundling'), count[0],
1868 unit=_('changesets'), total=len(csets))
1872 unit=_('changesets'), total=len(csets))
1869 return x
1873 return x
1870 elif revlog == mf:
1874 elif revlog == mf:
1871 clnode = mfs[x]
1875 clnode = mfs[x]
1872 mdata = mf.readfast(x)
1876 mdata = mf.readfast(x)
1873 for f in changedfiles:
1877 for f in changedfiles:
1874 if f in mdata:
1878 if f in mdata:
1875 fnodes.setdefault(f, {}).setdefault(mdata[f], clnode)
1879 fnodes.setdefault(f, {}).setdefault(mdata[f], clnode)
1876 count[0] += 1
1880 count[0] += 1
1877 self.ui.progress(_('bundling'), count[0],
1881 self.ui.progress(_('bundling'), count[0],
1878 unit=_('manifests'), total=len(mfs))
1882 unit=_('manifests'), total=len(mfs))
1879 return mfs[x]
1883 return mfs[x]
1880 else:
1884 else:
1881 self.ui.progress(
1885 self.ui.progress(
1882 _('bundling'), count[0], item=fstate[0],
1886 _('bundling'), count[0], item=fstate[0],
1883 unit=_('files'), total=len(changedfiles))
1887 unit=_('files'), total=len(changedfiles))
1884 return fstate[1][x]
1888 return fstate[1][x]
1885
1889
1886 bundler = changegroup.bundle10(lookup)
1890 bundler = changegroup.bundle10(lookup)
1887 reorder = self.ui.config('bundle', 'reorder', 'auto')
1891 reorder = self.ui.config('bundle', 'reorder', 'auto')
1888 if reorder == 'auto':
1892 if reorder == 'auto':
1889 reorder = None
1893 reorder = None
1890 else:
1894 else:
1891 reorder = util.parsebool(reorder)
1895 reorder = util.parsebool(reorder)
1892
1896
1893 def gengroup():
1897 def gengroup():
1894 # Create a changenode group generator that will call our functions
1898 # Create a changenode group generator that will call our functions
1895 # back to lookup the owning changenode and collect information.
1899 # back to lookup the owning changenode and collect information.
1896 for chunk in cl.group(csets, bundler, reorder=reorder):
1900 for chunk in cl.group(csets, bundler, reorder=reorder):
1897 yield chunk
1901 yield chunk
1898 self.ui.progress(_('bundling'), None)
1902 self.ui.progress(_('bundling'), None)
1899
1903
1900 # Create a generator for the manifestnodes that calls our lookup
1904 # Create a generator for the manifestnodes that calls our lookup
1901 # and data collection functions back.
1905 # and data collection functions back.
1902 count[0] = 0
1906 count[0] = 0
1903 for chunk in mf.group(prune(mf, mfs), bundler, reorder=reorder):
1907 for chunk in mf.group(prune(mf, mfs), bundler, reorder=reorder):
1904 yield chunk
1908 yield chunk
1905 self.ui.progress(_('bundling'), None)
1909 self.ui.progress(_('bundling'), None)
1906
1910
1907 mfs.clear()
1911 mfs.clear()
1908
1912
1909 # Go through all our files in order sorted by name.
1913 # Go through all our files in order sorted by name.
1910 count[0] = 0
1914 count[0] = 0
1911 for fname in sorted(changedfiles):
1915 for fname in sorted(changedfiles):
1912 filerevlog = self.file(fname)
1916 filerevlog = self.file(fname)
1913 if not len(filerevlog):
1917 if not len(filerevlog):
1914 raise util.Abort(_("empty or missing revlog for %s") % fname)
1918 raise util.Abort(_("empty or missing revlog for %s") % fname)
1915 fstate[0] = fname
1919 fstate[0] = fname
1916 fstate[1] = fnodes.pop(fname, {})
1920 fstate[1] = fnodes.pop(fname, {})
1917
1921
1918 nodelist = prune(filerevlog, fstate[1])
1922 nodelist = prune(filerevlog, fstate[1])
1919 if nodelist:
1923 if nodelist:
1920 count[0] += 1
1924 count[0] += 1
1921 yield bundler.fileheader(fname)
1925 yield bundler.fileheader(fname)
1922 for chunk in filerevlog.group(nodelist, bundler, reorder):
1926 for chunk in filerevlog.group(nodelist, bundler, reorder):
1923 yield chunk
1927 yield chunk
1924
1928
1925 # Signal that no more groups are left.
1929 # Signal that no more groups are left.
1926 yield bundler.close()
1930 yield bundler.close()
1927 self.ui.progress(_('bundling'), None)
1931 self.ui.progress(_('bundling'), None)
1928
1932
1929 if csets:
1933 if csets:
1930 self.hook('outgoing', node=hex(csets[0]), source=source)
1934 self.hook('outgoing', node=hex(csets[0]), source=source)
1931
1935
1932 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1936 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1933
1937
1934 def changegroup(self, basenodes, source):
1938 def changegroup(self, basenodes, source):
1935 # to avoid a race we use changegroupsubset() (issue1320)
1939 # to avoid a race we use changegroupsubset() (issue1320)
1936 return self.changegroupsubset(basenodes, self.heads(), source)
1940 return self.changegroupsubset(basenodes, self.heads(), source)
1937
1941
1938 def _changegroup(self, nodes, source):
1942 def _changegroup(self, nodes, source):
1939 """Compute the changegroup of all nodes that we have that a recipient
1943 """Compute the changegroup of all nodes that we have that a recipient
1940 doesn't. Return a chunkbuffer object whose read() method will return
1944 doesn't. Return a chunkbuffer object whose read() method will return
1941 successive changegroup chunks.
1945 successive changegroup chunks.
1942
1946
1943 This is much easier than the previous function as we can assume that
1947 This is much easier than the previous function as we can assume that
1944 the recipient has any changenode we aren't sending them.
1948 the recipient has any changenode we aren't sending them.
1945
1949
1946 nodes is the set of nodes to send"""
1950 nodes is the set of nodes to send"""
1947
1951
1948 cl = self.changelog
1952 cl = self.changelog
1949 mf = self.manifest
1953 mf = self.manifest
1950 mfs = {}
1954 mfs = {}
1951 changedfiles = set()
1955 changedfiles = set()
1952 fstate = ['']
1956 fstate = ['']
1953 count = [0]
1957 count = [0]
1954
1958
1955 self.hook('preoutgoing', throw=True, source=source)
1959 self.hook('preoutgoing', throw=True, source=source)
1956 self.changegroupinfo(nodes, source)
1960 self.changegroupinfo(nodes, source)
1957
1961
1958 revset = set([cl.rev(n) for n in nodes])
1962 revset = set([cl.rev(n) for n in nodes])
1959
1963
1960 def gennodelst(log):
1964 def gennodelst(log):
1961 return [log.node(r) for r in log if log.linkrev(r) in revset]
1965 return [log.node(r) for r in log if log.linkrev(r) in revset]
1962
1966
1963 def lookup(revlog, x):
1967 def lookup(revlog, x):
1964 if revlog == cl:
1968 if revlog == cl:
1965 c = cl.read(x)
1969 c = cl.read(x)
1966 changedfiles.update(c[3])
1970 changedfiles.update(c[3])
1967 mfs.setdefault(c[0], x)
1971 mfs.setdefault(c[0], x)
1968 count[0] += 1
1972 count[0] += 1
1969 self.ui.progress(_('bundling'), count[0],
1973 self.ui.progress(_('bundling'), count[0],
1970 unit=_('changesets'), total=len(nodes))
1974 unit=_('changesets'), total=len(nodes))
1971 return x
1975 return x
1972 elif revlog == mf:
1976 elif revlog == mf:
1973 count[0] += 1
1977 count[0] += 1
1974 self.ui.progress(_('bundling'), count[0],
1978 self.ui.progress(_('bundling'), count[0],
1975 unit=_('manifests'), total=len(mfs))
1979 unit=_('manifests'), total=len(mfs))
1976 return cl.node(revlog.linkrev(revlog.rev(x)))
1980 return cl.node(revlog.linkrev(revlog.rev(x)))
1977 else:
1981 else:
1978 self.ui.progress(
1982 self.ui.progress(
1979 _('bundling'), count[0], item=fstate[0],
1983 _('bundling'), count[0], item=fstate[0],
1980 total=len(changedfiles), unit=_('files'))
1984 total=len(changedfiles), unit=_('files'))
1981 return cl.node(revlog.linkrev(revlog.rev(x)))
1985 return cl.node(revlog.linkrev(revlog.rev(x)))
1982
1986
1983 bundler = changegroup.bundle10(lookup)
1987 bundler = changegroup.bundle10(lookup)
1984 reorder = self.ui.config('bundle', 'reorder', 'auto')
1988 reorder = self.ui.config('bundle', 'reorder', 'auto')
1985 if reorder == 'auto':
1989 if reorder == 'auto':
1986 reorder = None
1990 reorder = None
1987 else:
1991 else:
1988 reorder = util.parsebool(reorder)
1992 reorder = util.parsebool(reorder)
1989
1993
1990 def gengroup():
1994 def gengroup():
1991 '''yield a sequence of changegroup chunks (strings)'''
1995 '''yield a sequence of changegroup chunks (strings)'''
1992 # construct a list of all changed files
1996 # construct a list of all changed files
1993
1997
1994 for chunk in cl.group(nodes, bundler, reorder=reorder):
1998 for chunk in cl.group(nodes, bundler, reorder=reorder):
1995 yield chunk
1999 yield chunk
1996 self.ui.progress(_('bundling'), None)
2000 self.ui.progress(_('bundling'), None)
1997
2001
1998 count[0] = 0
2002 count[0] = 0
1999 for chunk in mf.group(gennodelst(mf), bundler, reorder=reorder):
2003 for chunk in mf.group(gennodelst(mf), bundler, reorder=reorder):
2000 yield chunk
2004 yield chunk
2001 self.ui.progress(_('bundling'), None)
2005 self.ui.progress(_('bundling'), None)
2002
2006
2003 count[0] = 0
2007 count[0] = 0
2004 for fname in sorted(changedfiles):
2008 for fname in sorted(changedfiles):
2005 filerevlog = self.file(fname)
2009 filerevlog = self.file(fname)
2006 if not len(filerevlog):
2010 if not len(filerevlog):
2007 raise util.Abort(_("empty or missing revlog for %s") % fname)
2011 raise util.Abort(_("empty or missing revlog for %s") % fname)
2008 fstate[0] = fname
2012 fstate[0] = fname
2009 nodelist = gennodelst(filerevlog)
2013 nodelist = gennodelst(filerevlog)
2010 if nodelist:
2014 if nodelist:
2011 count[0] += 1
2015 count[0] += 1
2012 yield bundler.fileheader(fname)
2016 yield bundler.fileheader(fname)
2013 for chunk in filerevlog.group(nodelist, bundler, reorder):
2017 for chunk in filerevlog.group(nodelist, bundler, reorder):
2014 yield chunk
2018 yield chunk
2015 yield bundler.close()
2019 yield bundler.close()
2016 self.ui.progress(_('bundling'), None)
2020 self.ui.progress(_('bundling'), None)
2017
2021
2018 if nodes:
2022 if nodes:
2019 self.hook('outgoing', node=hex(nodes[0]), source=source)
2023 self.hook('outgoing', node=hex(nodes[0]), source=source)
2020
2024
2021 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
2025 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
2022
2026
2023 def addchangegroup(self, source, srctype, url, emptyok=False):
2027 def addchangegroup(self, source, srctype, url, emptyok=False):
2024 """Add the changegroup returned by source.read() to this repo.
2028 """Add the changegroup returned by source.read() to this repo.
2025 srctype is a string like 'push', 'pull', or 'unbundle'. url is
2029 srctype is a string like 'push', 'pull', or 'unbundle'. url is
2026 the URL of the repo where this changegroup is coming from.
2030 the URL of the repo where this changegroup is coming from.
2027
2031
2028 Return an integer summarizing the change to this repo:
2032 Return an integer summarizing the change to this repo:
2029 - nothing changed or no source: 0
2033 - nothing changed or no source: 0
2030 - more heads than before: 1+added heads (2..n)
2034 - more heads than before: 1+added heads (2..n)
2031 - fewer heads than before: -1-removed heads (-2..-n)
2035 - fewer heads than before: -1-removed heads (-2..-n)
2032 - number of heads stays the same: 1
2036 - number of heads stays the same: 1
2033 """
2037 """
2034 def csmap(x):
2038 def csmap(x):
2035 self.ui.debug("add changeset %s\n" % short(x))
2039 self.ui.debug("add changeset %s\n" % short(x))
2036 return len(cl)
2040 return len(cl)
2037
2041
2038 def revmap(x):
2042 def revmap(x):
2039 return cl.rev(x)
2043 return cl.rev(x)
2040
2044
2041 if not source:
2045 if not source:
2042 return 0
2046 return 0
2043
2047
2044 self.hook('prechangegroup', throw=True, source=srctype, url=url)
2048 self.hook('prechangegroup', throw=True, source=srctype, url=url)
2045
2049
2046 changesets = files = revisions = 0
2050 changesets = files = revisions = 0
2047 efiles = set()
2051 efiles = set()
2048
2052
2049 # write changelog data to temp files so concurrent readers will not see
2053 # write changelog data to temp files so concurrent readers will not see
2050 # inconsistent view
2054 # inconsistent view
2051 cl = self.changelog
2055 cl = self.changelog
2052 cl.delayupdate()
2056 cl.delayupdate()
2053 oldheads = cl.heads()
2057 oldheads = cl.heads()
2054
2058
2055 tr = self.transaction("\n".join([srctype, util.hidepassword(url)]))
2059 tr = self.transaction("\n".join([srctype, util.hidepassword(url)]))
2056 try:
2060 try:
2057 trp = weakref.proxy(tr)
2061 trp = weakref.proxy(tr)
2058 # pull off the changeset group
2062 # pull off the changeset group
2059 self.ui.status(_("adding changesets\n"))
2063 self.ui.status(_("adding changesets\n"))
2060 clstart = len(cl)
2064 clstart = len(cl)
2061 class prog(object):
2065 class prog(object):
2062 step = _('changesets')
2066 step = _('changesets')
2063 count = 1
2067 count = 1
2064 ui = self.ui
2068 ui = self.ui
2065 total = None
2069 total = None
2066 def __call__(self):
2070 def __call__(self):
2067 self.ui.progress(self.step, self.count, unit=_('chunks'),
2071 self.ui.progress(self.step, self.count, unit=_('chunks'),
2068 total=self.total)
2072 total=self.total)
2069 self.count += 1
2073 self.count += 1
2070 pr = prog()
2074 pr = prog()
2071 source.callback = pr
2075 source.callback = pr
2072
2076
2073 source.changelogheader()
2077 source.changelogheader()
2074 srccontent = cl.addgroup(source, csmap, trp)
2078 srccontent = cl.addgroup(source, csmap, trp)
2075 if not (srccontent or emptyok):
2079 if not (srccontent or emptyok):
2076 raise util.Abort(_("received changelog group is empty"))
2080 raise util.Abort(_("received changelog group is empty"))
2077 clend = len(cl)
2081 clend = len(cl)
2078 changesets = clend - clstart
2082 changesets = clend - clstart
2079 for c in xrange(clstart, clend):
2083 for c in xrange(clstart, clend):
2080 efiles.update(self[c].files())
2084 efiles.update(self[c].files())
2081 efiles = len(efiles)
2085 efiles = len(efiles)
2082 self.ui.progress(_('changesets'), None)
2086 self.ui.progress(_('changesets'), None)
2083
2087
2084 # pull off the manifest group
2088 # pull off the manifest group
2085 self.ui.status(_("adding manifests\n"))
2089 self.ui.status(_("adding manifests\n"))
2086 pr.step = _('manifests')
2090 pr.step = _('manifests')
2087 pr.count = 1
2091 pr.count = 1
2088 pr.total = changesets # manifests <= changesets
2092 pr.total = changesets # manifests <= changesets
2089 # no need to check for empty manifest group here:
2093 # no need to check for empty manifest group here:
2090 # if the result of the merge of 1 and 2 is the same in 3 and 4,
2094 # if the result of the merge of 1 and 2 is the same in 3 and 4,
2091 # no new manifest will be created and the manifest group will
2095 # no new manifest will be created and the manifest group will
2092 # be empty during the pull
2096 # be empty during the pull
2093 source.manifestheader()
2097 source.manifestheader()
2094 self.manifest.addgroup(source, revmap, trp)
2098 self.manifest.addgroup(source, revmap, trp)
2095 self.ui.progress(_('manifests'), None)
2099 self.ui.progress(_('manifests'), None)
2096
2100
2097 needfiles = {}
2101 needfiles = {}
2098 if self.ui.configbool('server', 'validate', default=False):
2102 if self.ui.configbool('server', 'validate', default=False):
2099 # validate incoming csets have their manifests
2103 # validate incoming csets have their manifests
2100 for cset in xrange(clstart, clend):
2104 for cset in xrange(clstart, clend):
2101 mfest = self.changelog.read(self.changelog.node(cset))[0]
2105 mfest = self.changelog.read(self.changelog.node(cset))[0]
2102 mfest = self.manifest.readdelta(mfest)
2106 mfest = self.manifest.readdelta(mfest)
2103 # store file nodes we must see
2107 # store file nodes we must see
2104 for f, n in mfest.iteritems():
2108 for f, n in mfest.iteritems():
2105 needfiles.setdefault(f, set()).add(n)
2109 needfiles.setdefault(f, set()).add(n)
2106
2110
2107 # process the files
2111 # process the files
2108 self.ui.status(_("adding file changes\n"))
2112 self.ui.status(_("adding file changes\n"))
2109 pr.step = _('files')
2113 pr.step = _('files')
2110 pr.count = 1
2114 pr.count = 1
2111 pr.total = efiles
2115 pr.total = efiles
2112 source.callback = None
2116 source.callback = None
2113
2117
2114 while True:
2118 while True:
2115 chunkdata = source.filelogheader()
2119 chunkdata = source.filelogheader()
2116 if not chunkdata:
2120 if not chunkdata:
2117 break
2121 break
2118 f = chunkdata["filename"]
2122 f = chunkdata["filename"]
2119 self.ui.debug("adding %s revisions\n" % f)
2123 self.ui.debug("adding %s revisions\n" % f)
2120 pr()
2124 pr()
2121 fl = self.file(f)
2125 fl = self.file(f)
2122 o = len(fl)
2126 o = len(fl)
2123 if not fl.addgroup(source, revmap, trp):
2127 if not fl.addgroup(source, revmap, trp):
2124 raise util.Abort(_("received file revlog group is empty"))
2128 raise util.Abort(_("received file revlog group is empty"))
2125 revisions += len(fl) - o
2129 revisions += len(fl) - o
2126 files += 1
2130 files += 1
2127 if f in needfiles:
2131 if f in needfiles:
2128 needs = needfiles[f]
2132 needs = needfiles[f]
2129 for new in xrange(o, len(fl)):
2133 for new in xrange(o, len(fl)):
2130 n = fl.node(new)
2134 n = fl.node(new)
2131 if n in needs:
2135 if n in needs:
2132 needs.remove(n)
2136 needs.remove(n)
2133 if not needs:
2137 if not needs:
2134 del needfiles[f]
2138 del needfiles[f]
2135 self.ui.progress(_('files'), None)
2139 self.ui.progress(_('files'), None)
2136
2140
2137 for f, needs in needfiles.iteritems():
2141 for f, needs in needfiles.iteritems():
2138 fl = self.file(f)
2142 fl = self.file(f)
2139 for n in needs:
2143 for n in needs:
2140 try:
2144 try:
2141 fl.rev(n)
2145 fl.rev(n)
2142 except error.LookupError:
2146 except error.LookupError:
2143 raise util.Abort(
2147 raise util.Abort(
2144 _('missing file data for %s:%s - run hg verify') %
2148 _('missing file data for %s:%s - run hg verify') %
2145 (f, hex(n)))
2149 (f, hex(n)))
2146
2150
2147 dh = 0
2151 dh = 0
2148 if oldheads:
2152 if oldheads:
2149 heads = cl.heads()
2153 heads = cl.heads()
2150 dh = len(heads) - len(oldheads)
2154 dh = len(heads) - len(oldheads)
2151 for h in heads:
2155 for h in heads:
2152 if h not in oldheads and 'close' in self[h].extra():
2156 if h not in oldheads and 'close' in self[h].extra():
2153 dh -= 1
2157 dh -= 1
2154 htext = ""
2158 htext = ""
2155 if dh:
2159 if dh:
2156 htext = _(" (%+d heads)") % dh
2160 htext = _(" (%+d heads)") % dh
2157
2161
2158 self.ui.status(_("added %d changesets"
2162 self.ui.status(_("added %d changesets"
2159 " with %d changes to %d files%s\n")
2163 " with %d changes to %d files%s\n")
2160 % (changesets, revisions, files, htext))
2164 % (changesets, revisions, files, htext))
2161
2165
2162 if changesets > 0:
2166 if changesets > 0:
2163 p = lambda: cl.writepending() and self.root or ""
2167 p = lambda: cl.writepending() and self.root or ""
2164 self.hook('pretxnchangegroup', throw=True,
2168 self.hook('pretxnchangegroup', throw=True,
2165 node=hex(cl.node(clstart)), source=srctype,
2169 node=hex(cl.node(clstart)), source=srctype,
2166 url=url, pending=p)
2170 url=url, pending=p)
2167
2171
2168 added = [cl.node(r) for r in xrange(clstart, clend)]
2172 added = [cl.node(r) for r in xrange(clstart, clend)]
2169 publishing = self.ui.configbool('phases', 'publish', True)
2173 publishing = self.ui.configbool('phases', 'publish', True)
2170 if srctype == 'push':
2174 if srctype == 'push':
2171 # Old server can not push the boundary themself.
2175 # Old server can not push the boundary themself.
2172 # New server won't push the boundary if changeset already
2176 # New server won't push the boundary if changeset already
2173 # existed locally as secrete
2177 # existed locally as secrete
2174 #
2178 #
2175 # We should not use added here but the list of all change in
2179 # We should not use added here but the list of all change in
2176 # the bundle
2180 # the bundle
2177 if publishing:
2181 if publishing:
2178 phases.advanceboundary(self, phases.public, srccontent)
2182 phases.advanceboundary(self, phases.public, srccontent)
2179 else:
2183 else:
2180 phases.advanceboundary(self, phases.draft, srccontent)
2184 phases.advanceboundary(self, phases.draft, srccontent)
2181 phases.retractboundary(self, phases.draft, added)
2185 phases.retractboundary(self, phases.draft, added)
2182 elif srctype != 'strip':
2186 elif srctype != 'strip':
2183 # publishing only alter behavior during push
2187 # publishing only alter behavior during push
2184 #
2188 #
2185 # strip should not touch boundary at all
2189 # strip should not touch boundary at all
2186 phases.retractboundary(self, phases.draft, added)
2190 phases.retractboundary(self, phases.draft, added)
2187
2191
2188 # make changelog see real files again
2192 # make changelog see real files again
2189 cl.finalize(trp)
2193 cl.finalize(trp)
2190
2194
2191 tr.close()
2195 tr.close()
2192
2196
2193 if changesets > 0:
2197 if changesets > 0:
2194 def runhooks():
2198 def runhooks():
2195 # forcefully update the on-disk branch cache
2199 # forcefully update the on-disk branch cache
2196 self.ui.debug("updating the branch cache\n")
2200 self.ui.debug("updating the branch cache\n")
2197 self.updatebranchcache()
2201 self.updatebranchcache()
2198 self.hook("changegroup", node=hex(cl.node(clstart)),
2202 self.hook("changegroup", node=hex(cl.node(clstart)),
2199 source=srctype, url=url)
2203 source=srctype, url=url)
2200
2204
2201 for n in added:
2205 for n in added:
2202 self.hook("incoming", node=hex(n), source=srctype,
2206 self.hook("incoming", node=hex(n), source=srctype,
2203 url=url)
2207 url=url)
2204 self._afterlock(runhooks)
2208 self._afterlock(runhooks)
2205
2209
2206 finally:
2210 finally:
2207 tr.release()
2211 tr.release()
2208 # never return 0 here:
2212 # never return 0 here:
2209 if dh < 0:
2213 if dh < 0:
2210 return dh - 1
2214 return dh - 1
2211 else:
2215 else:
2212 return dh + 1
2216 return dh + 1
2213
2217
2214 def stream_in(self, remote, requirements):
2218 def stream_in(self, remote, requirements):
2215 lock = self.lock()
2219 lock = self.lock()
2216 try:
2220 try:
2217 fp = remote.stream_out()
2221 fp = remote.stream_out()
2218 l = fp.readline()
2222 l = fp.readline()
2219 try:
2223 try:
2220 resp = int(l)
2224 resp = int(l)
2221 except ValueError:
2225 except ValueError:
2222 raise error.ResponseError(
2226 raise error.ResponseError(
2223 _('Unexpected response from remote server:'), l)
2227 _('Unexpected response from remote server:'), l)
2224 if resp == 1:
2228 if resp == 1:
2225 raise util.Abort(_('operation forbidden by server'))
2229 raise util.Abort(_('operation forbidden by server'))
2226 elif resp == 2:
2230 elif resp == 2:
2227 raise util.Abort(_('locking the remote repository failed'))
2231 raise util.Abort(_('locking the remote repository failed'))
2228 elif resp != 0:
2232 elif resp != 0:
2229 raise util.Abort(_('the server sent an unknown error code'))
2233 raise util.Abort(_('the server sent an unknown error code'))
2230 self.ui.status(_('streaming all changes\n'))
2234 self.ui.status(_('streaming all changes\n'))
2231 l = fp.readline()
2235 l = fp.readline()
2232 try:
2236 try:
2233 total_files, total_bytes = map(int, l.split(' ', 1))
2237 total_files, total_bytes = map(int, l.split(' ', 1))
2234 except (ValueError, TypeError):
2238 except (ValueError, TypeError):
2235 raise error.ResponseError(
2239 raise error.ResponseError(
2236 _('Unexpected response from remote server:'), l)
2240 _('Unexpected response from remote server:'), l)
2237 self.ui.status(_('%d files to transfer, %s of data\n') %
2241 self.ui.status(_('%d files to transfer, %s of data\n') %
2238 (total_files, util.bytecount(total_bytes)))
2242 (total_files, util.bytecount(total_bytes)))
2239 start = time.time()
2243 start = time.time()
2240 for i in xrange(total_files):
2244 for i in xrange(total_files):
2241 # XXX doesn't support '\n' or '\r' in filenames
2245 # XXX doesn't support '\n' or '\r' in filenames
2242 l = fp.readline()
2246 l = fp.readline()
2243 try:
2247 try:
2244 name, size = l.split('\0', 1)
2248 name, size = l.split('\0', 1)
2245 size = int(size)
2249 size = int(size)
2246 except (ValueError, TypeError):
2250 except (ValueError, TypeError):
2247 raise error.ResponseError(
2251 raise error.ResponseError(
2248 _('Unexpected response from remote server:'), l)
2252 _('Unexpected response from remote server:'), l)
2249 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
2253 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
2250 # for backwards compat, name was partially encoded
2254 # for backwards compat, name was partially encoded
2251 ofp = self.sopener(store.decodedir(name), 'w')
2255 ofp = self.sopener(store.decodedir(name), 'w')
2252 for chunk in util.filechunkiter(fp, limit=size):
2256 for chunk in util.filechunkiter(fp, limit=size):
2253 ofp.write(chunk)
2257 ofp.write(chunk)
2254 ofp.close()
2258 ofp.close()
2255 elapsed = time.time() - start
2259 elapsed = time.time() - start
2256 if elapsed <= 0:
2260 if elapsed <= 0:
2257 elapsed = 0.001
2261 elapsed = 0.001
2258 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
2262 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
2259 (util.bytecount(total_bytes), elapsed,
2263 (util.bytecount(total_bytes), elapsed,
2260 util.bytecount(total_bytes / elapsed)))
2264 util.bytecount(total_bytes / elapsed)))
2261
2265
2262 # new requirements = old non-format requirements + new format-related
2266 # new requirements = old non-format requirements + new format-related
2263 # requirements from the streamed-in repository
2267 # requirements from the streamed-in repository
2264 requirements.update(set(self.requirements) - self.supportedformats)
2268 requirements.update(set(self.requirements) - self.supportedformats)
2265 self._applyrequirements(requirements)
2269 self._applyrequirements(requirements)
2266 self._writerequirements()
2270 self._writerequirements()
2267
2271
2268 self.invalidate()
2272 self.invalidate()
2269 return len(self.heads()) + 1
2273 return len(self.heads()) + 1
2270 finally:
2274 finally:
2271 lock.release()
2275 lock.release()
2272
2276
2273 def clone(self, remote, heads=[], stream=False):
2277 def clone(self, remote, heads=[], stream=False):
2274 '''clone remote repository.
2278 '''clone remote repository.
2275
2279
2276 keyword arguments:
2280 keyword arguments:
2277 heads: list of revs to clone (forces use of pull)
2281 heads: list of revs to clone (forces use of pull)
2278 stream: use streaming clone if possible'''
2282 stream: use streaming clone if possible'''
2279
2283
2280 # now, all clients that can request uncompressed clones can
2284 # now, all clients that can request uncompressed clones can
2281 # read repo formats supported by all servers that can serve
2285 # read repo formats supported by all servers that can serve
2282 # them.
2286 # them.
2283
2287
2284 # if revlog format changes, client will have to check version
2288 # if revlog format changes, client will have to check version
2285 # and format flags on "stream" capability, and use
2289 # and format flags on "stream" capability, and use
2286 # uncompressed only if compatible.
2290 # uncompressed only if compatible.
2287
2291
2288 if stream and not heads:
2292 if stream and not heads:
2289 # 'stream' means remote revlog format is revlogv1 only
2293 # 'stream' means remote revlog format is revlogv1 only
2290 if remote.capable('stream'):
2294 if remote.capable('stream'):
2291 return self.stream_in(remote, set(('revlogv1',)))
2295 return self.stream_in(remote, set(('revlogv1',)))
2292 # otherwise, 'streamreqs' contains the remote revlog format
2296 # otherwise, 'streamreqs' contains the remote revlog format
2293 streamreqs = remote.capable('streamreqs')
2297 streamreqs = remote.capable('streamreqs')
2294 if streamreqs:
2298 if streamreqs:
2295 streamreqs = set(streamreqs.split(','))
2299 streamreqs = set(streamreqs.split(','))
2296 # if we support it, stream in and adjust our requirements
2300 # if we support it, stream in and adjust our requirements
2297 if not streamreqs - self.supportedformats:
2301 if not streamreqs - self.supportedformats:
2298 return self.stream_in(remote, streamreqs)
2302 return self.stream_in(remote, streamreqs)
2299 return self.pull(remote, heads)
2303 return self.pull(remote, heads)
2300
2304
2301 def pushkey(self, namespace, key, old, new):
2305 def pushkey(self, namespace, key, old, new):
2302 self.hook('prepushkey', throw=True, namespace=namespace, key=key,
2306 self.hook('prepushkey', throw=True, namespace=namespace, key=key,
2303 old=old, new=new)
2307 old=old, new=new)
2304 ret = pushkey.push(self, namespace, key, old, new)
2308 ret = pushkey.push(self, namespace, key, old, new)
2305 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
2309 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
2306 ret=ret)
2310 ret=ret)
2307 return ret
2311 return ret
2308
2312
2309 def listkeys(self, namespace):
2313 def listkeys(self, namespace):
2310 self.hook('prelistkeys', throw=True, namespace=namespace)
2314 self.hook('prelistkeys', throw=True, namespace=namespace)
2311 values = pushkey.list(self, namespace)
2315 values = pushkey.list(self, namespace)
2312 self.hook('listkeys', namespace=namespace, values=values)
2316 self.hook('listkeys', namespace=namespace, values=values)
2313 return values
2317 return values
2314
2318
2315 def debugwireargs(self, one, two, three=None, four=None, five=None):
2319 def debugwireargs(self, one, two, three=None, four=None, five=None):
2316 '''used to test argument passing over the wire'''
2320 '''used to test argument passing over the wire'''
2317 return "%s %s %s %s %s" % (one, two, three, four, five)
2321 return "%s %s %s %s %s" % (one, two, three, four, five)
2318
2322
2319 def savecommitmessage(self, text):
2323 def savecommitmessage(self, text):
2320 fp = self.opener('last-message.txt', 'wb')
2324 fp = self.opener('last-message.txt', 'wb')
2321 try:
2325 try:
2322 fp.write(text)
2326 fp.write(text)
2323 finally:
2327 finally:
2324 fp.close()
2328 fp.close()
2325 return self.pathto(fp.name[len(self.root)+1:])
2329 return self.pathto(fp.name[len(self.root)+1:])
2326
2330
2327 # used to avoid circular references so destructors work
2331 # used to avoid circular references so destructors work
2328 def aftertrans(files):
2332 def aftertrans(files):
2329 renamefiles = [tuple(t) for t in files]
2333 renamefiles = [tuple(t) for t in files]
2330 def a():
2334 def a():
2331 for src, dest in renamefiles:
2335 for src, dest in renamefiles:
2332 util.rename(src, dest)
2336 util.rename(src, dest)
2333 return a
2337 return a
2334
2338
2335 def undoname(fn):
2339 def undoname(fn):
2336 base, name = os.path.split(fn)
2340 base, name = os.path.split(fn)
2337 assert name.startswith('journal')
2341 assert name.startswith('journal')
2338 return os.path.join(base, name.replace('journal', 'undo', 1))
2342 return os.path.join(base, name.replace('journal', 'undo', 1))
2339
2343
2340 def instance(ui, path, create):
2344 def instance(ui, path, create):
2341 return localrepository(ui, util.urllocalpath(path), create)
2345 return localrepository(ui, util.urllocalpath(path), create)
2342
2346
2343 def islocal(path):
2347 def islocal(path):
2344 return True
2348 return True
General Comments 0
You need to be logged in to leave comments. Login now