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