##// END OF EJS Templates
avoid .split() in for loops and use tuples instead...
David Soria Parra -
r13200:6f011cf5 default
parent child Browse files
Show More
@@ -1,682 +1,683 b''
1 # dirstate.py - working directory tracking for mercurial
1 # dirstate.py - working directory tracking 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 nullid
8 from node import nullid
9 from i18n import _
9 from i18n import _
10 import util, ignore, osutil, parsers, encoding
10 import util, ignore, osutil, parsers, encoding
11 import struct, os, stat, errno
11 import struct, os, stat, errno
12 import cStringIO
12 import cStringIO
13
13
14 _format = ">cllll"
14 _format = ">cllll"
15 propertycache = util.propertycache
15 propertycache = util.propertycache
16
16
17 def _finddirs(path):
17 def _finddirs(path):
18 pos = path.rfind('/')
18 pos = path.rfind('/')
19 while pos != -1:
19 while pos != -1:
20 yield path[:pos]
20 yield path[:pos]
21 pos = path.rfind('/', 0, pos)
21 pos = path.rfind('/', 0, pos)
22
22
23 def _incdirs(dirs, path):
23 def _incdirs(dirs, path):
24 for base in _finddirs(path):
24 for base in _finddirs(path):
25 if base in dirs:
25 if base in dirs:
26 dirs[base] += 1
26 dirs[base] += 1
27 return
27 return
28 dirs[base] = 1
28 dirs[base] = 1
29
29
30 def _decdirs(dirs, path):
30 def _decdirs(dirs, path):
31 for base in _finddirs(path):
31 for base in _finddirs(path):
32 if dirs[base] > 1:
32 if dirs[base] > 1:
33 dirs[base] -= 1
33 dirs[base] -= 1
34 return
34 return
35 del dirs[base]
35 del dirs[base]
36
36
37 class dirstate(object):
37 class dirstate(object):
38
38
39 def __init__(self, opener, ui, root, validate):
39 def __init__(self, opener, ui, root, validate):
40 '''Create a new dirstate object.
40 '''Create a new dirstate object.
41
41
42 opener is an open()-like callable that can be used to open the
42 opener is an open()-like callable that can be used to open the
43 dirstate file; root is the root of the directory tracked by
43 dirstate file; root is the root of the directory tracked by
44 the dirstate.
44 the dirstate.
45 '''
45 '''
46 self._opener = opener
46 self._opener = opener
47 self._validate = validate
47 self._validate = validate
48 self._root = root
48 self._root = root
49 self._rootdir = os.path.join(root, '')
49 self._rootdir = os.path.join(root, '')
50 self._dirty = False
50 self._dirty = False
51 self._dirtypl = False
51 self._dirtypl = False
52 self._ui = ui
52 self._ui = ui
53
53
54 @propertycache
54 @propertycache
55 def _map(self):
55 def _map(self):
56 '''Return the dirstate contents as a map from filename to
56 '''Return the dirstate contents as a map from filename to
57 (state, mode, size, time).'''
57 (state, mode, size, time).'''
58 self._read()
58 self._read()
59 return self._map
59 return self._map
60
60
61 @propertycache
61 @propertycache
62 def _copymap(self):
62 def _copymap(self):
63 self._read()
63 self._read()
64 return self._copymap
64 return self._copymap
65
65
66 @propertycache
66 @propertycache
67 def _foldmap(self):
67 def _foldmap(self):
68 f = {}
68 f = {}
69 for name in self._map:
69 for name in self._map:
70 f[os.path.normcase(name)] = name
70 f[os.path.normcase(name)] = name
71 return f
71 return f
72
72
73 @propertycache
73 @propertycache
74 def _branch(self):
74 def _branch(self):
75 try:
75 try:
76 return self._opener("branch").read().strip() or "default"
76 return self._opener("branch").read().strip() or "default"
77 except IOError:
77 except IOError:
78 return "default"
78 return "default"
79
79
80 @propertycache
80 @propertycache
81 def _pl(self):
81 def _pl(self):
82 try:
82 try:
83 st = self._opener("dirstate").read(40)
83 st = self._opener("dirstate").read(40)
84 l = len(st)
84 l = len(st)
85 if l == 40:
85 if l == 40:
86 return st[:20], st[20:40]
86 return st[:20], st[20:40]
87 elif l > 0 and l < 40:
87 elif l > 0 and l < 40:
88 raise util.Abort(_('working directory state appears damaged!'))
88 raise util.Abort(_('working directory state appears damaged!'))
89 except IOError, err:
89 except IOError, err:
90 if err.errno != errno.ENOENT:
90 if err.errno != errno.ENOENT:
91 raise
91 raise
92 return [nullid, nullid]
92 return [nullid, nullid]
93
93
94 @propertycache
94 @propertycache
95 def _dirs(self):
95 def _dirs(self):
96 dirs = {}
96 dirs = {}
97 for f, s in self._map.iteritems():
97 for f, s in self._map.iteritems():
98 if s[0] != 'r':
98 if s[0] != 'r':
99 _incdirs(dirs, f)
99 _incdirs(dirs, f)
100 return dirs
100 return dirs
101
101
102 @propertycache
102 @propertycache
103 def _ignore(self):
103 def _ignore(self):
104 files = [self._join('.hgignore')]
104 files = [self._join('.hgignore')]
105 for name, path in self._ui.configitems("ui"):
105 for name, path in self._ui.configitems("ui"):
106 if name == 'ignore' or name.startswith('ignore.'):
106 if name == 'ignore' or name.startswith('ignore.'):
107 files.append(util.expandpath(path))
107 files.append(util.expandpath(path))
108 return ignore.ignore(self._root, files, self._ui.warn)
108 return ignore.ignore(self._root, files, self._ui.warn)
109
109
110 @propertycache
110 @propertycache
111 def _slash(self):
111 def _slash(self):
112 return self._ui.configbool('ui', 'slash') and os.sep != '/'
112 return self._ui.configbool('ui', 'slash') and os.sep != '/'
113
113
114 @propertycache
114 @propertycache
115 def _checklink(self):
115 def _checklink(self):
116 return util.checklink(self._root)
116 return util.checklink(self._root)
117
117
118 @propertycache
118 @propertycache
119 def _checkexec(self):
119 def _checkexec(self):
120 return util.checkexec(self._root)
120 return util.checkexec(self._root)
121
121
122 @propertycache
122 @propertycache
123 def _checkcase(self):
123 def _checkcase(self):
124 return not util.checkcase(self._join('.hg'))
124 return not util.checkcase(self._join('.hg'))
125
125
126 def _join(self, f):
126 def _join(self, f):
127 # much faster than os.path.join()
127 # much faster than os.path.join()
128 # it's safe because f is always a relative path
128 # it's safe because f is always a relative path
129 return self._rootdir + f
129 return self._rootdir + f
130
130
131 def flagfunc(self, fallback):
131 def flagfunc(self, fallback):
132 if self._checklink:
132 if self._checklink:
133 if self._checkexec:
133 if self._checkexec:
134 def f(x):
134 def f(x):
135 p = self._join(x)
135 p = self._join(x)
136 if os.path.islink(p):
136 if os.path.islink(p):
137 return 'l'
137 return 'l'
138 if util.is_exec(p):
138 if util.is_exec(p):
139 return 'x'
139 return 'x'
140 return ''
140 return ''
141 return f
141 return f
142 def f(x):
142 def f(x):
143 if os.path.islink(self._join(x)):
143 if os.path.islink(self._join(x)):
144 return 'l'
144 return 'l'
145 if 'x' in fallback(x):
145 if 'x' in fallback(x):
146 return 'x'
146 return 'x'
147 return ''
147 return ''
148 return f
148 return f
149 if self._checkexec:
149 if self._checkexec:
150 def f(x):
150 def f(x):
151 if 'l' in fallback(x):
151 if 'l' in fallback(x):
152 return 'l'
152 return 'l'
153 if util.is_exec(self._join(x)):
153 if util.is_exec(self._join(x)):
154 return 'x'
154 return 'x'
155 return ''
155 return ''
156 return f
156 return f
157 return fallback
157 return fallback
158
158
159 def getcwd(self):
159 def getcwd(self):
160 cwd = os.getcwd()
160 cwd = os.getcwd()
161 if cwd == self._root:
161 if cwd == self._root:
162 return ''
162 return ''
163 # self._root ends with a path separator if self._root is '/' or 'C:\'
163 # self._root ends with a path separator if self._root is '/' or 'C:\'
164 rootsep = self._root
164 rootsep = self._root
165 if not util.endswithsep(rootsep):
165 if not util.endswithsep(rootsep):
166 rootsep += os.sep
166 rootsep += os.sep
167 if cwd.startswith(rootsep):
167 if cwd.startswith(rootsep):
168 return cwd[len(rootsep):]
168 return cwd[len(rootsep):]
169 else:
169 else:
170 # we're outside the repo. return an absolute path.
170 # we're outside the repo. return an absolute path.
171 return cwd
171 return cwd
172
172
173 def pathto(self, f, cwd=None):
173 def pathto(self, f, cwd=None):
174 if cwd is None:
174 if cwd is None:
175 cwd = self.getcwd()
175 cwd = self.getcwd()
176 path = util.pathto(self._root, cwd, f)
176 path = util.pathto(self._root, cwd, f)
177 if self._slash:
177 if self._slash:
178 return util.normpath(path)
178 return util.normpath(path)
179 return path
179 return path
180
180
181 def __getitem__(self, key):
181 def __getitem__(self, key):
182 '''Return the current state of key (a filename) in the dirstate.
182 '''Return the current state of key (a filename) in the dirstate.
183
183
184 States are:
184 States are:
185 n normal
185 n normal
186 m needs merging
186 m needs merging
187 r marked for removal
187 r marked for removal
188 a marked for addition
188 a marked for addition
189 ? not tracked
189 ? not tracked
190 '''
190 '''
191 return self._map.get(key, ("?",))[0]
191 return self._map.get(key, ("?",))[0]
192
192
193 def __contains__(self, key):
193 def __contains__(self, key):
194 return key in self._map
194 return key in self._map
195
195
196 def __iter__(self):
196 def __iter__(self):
197 for x in sorted(self._map):
197 for x in sorted(self._map):
198 yield x
198 yield x
199
199
200 def parents(self):
200 def parents(self):
201 return [self._validate(p) for p in self._pl]
201 return [self._validate(p) for p in self._pl]
202
202
203 def branch(self):
203 def branch(self):
204 return encoding.tolocal(self._branch)
204 return encoding.tolocal(self._branch)
205
205
206 def setparents(self, p1, p2=nullid):
206 def setparents(self, p1, p2=nullid):
207 self._dirty = self._dirtypl = True
207 self._dirty = self._dirtypl = True
208 self._pl = p1, p2
208 self._pl = p1, p2
209
209
210 def setbranch(self, branch):
210 def setbranch(self, branch):
211 if branch in ['tip', '.', 'null']:
211 if branch in ['tip', '.', 'null']:
212 raise util.Abort(_('the name \'%s\' is reserved') % branch)
212 raise util.Abort(_('the name \'%s\' is reserved') % branch)
213 self._branch = encoding.fromlocal(branch)
213 self._branch = encoding.fromlocal(branch)
214 self._opener("branch", "w").write(self._branch + '\n')
214 self._opener("branch", "w").write(self._branch + '\n')
215
215
216 def _read(self):
216 def _read(self):
217 self._map = {}
217 self._map = {}
218 self._copymap = {}
218 self._copymap = {}
219 try:
219 try:
220 st = self._opener("dirstate").read()
220 st = self._opener("dirstate").read()
221 except IOError, err:
221 except IOError, err:
222 if err.errno != errno.ENOENT:
222 if err.errno != errno.ENOENT:
223 raise
223 raise
224 return
224 return
225 if not st:
225 if not st:
226 return
226 return
227
227
228 p = parsers.parse_dirstate(self._map, self._copymap, st)
228 p = parsers.parse_dirstate(self._map, self._copymap, st)
229 if not self._dirtypl:
229 if not self._dirtypl:
230 self._pl = p
230 self._pl = p
231
231
232 def invalidate(self):
232 def invalidate(self):
233 for a in "_map _copymap _foldmap _branch _pl _dirs _ignore".split():
233 for a in ("_map", "_copymap", "_foldmap", "_branch", "_pl", "_dirs",
234 "_ignore"):
234 if a in self.__dict__:
235 if a in self.__dict__:
235 delattr(self, a)
236 delattr(self, a)
236 self._dirty = False
237 self._dirty = False
237
238
238 def copy(self, source, dest):
239 def copy(self, source, dest):
239 """Mark dest as a copy of source. Unmark dest if source is None."""
240 """Mark dest as a copy of source. Unmark dest if source is None."""
240 if source == dest:
241 if source == dest:
241 return
242 return
242 self._dirty = True
243 self._dirty = True
243 if source is not None:
244 if source is not None:
244 self._copymap[dest] = source
245 self._copymap[dest] = source
245 elif dest in self._copymap:
246 elif dest in self._copymap:
246 del self._copymap[dest]
247 del self._copymap[dest]
247
248
248 def copied(self, file):
249 def copied(self, file):
249 return self._copymap.get(file, None)
250 return self._copymap.get(file, None)
250
251
251 def copies(self):
252 def copies(self):
252 return self._copymap
253 return self._copymap
253
254
254 def _droppath(self, f):
255 def _droppath(self, f):
255 if self[f] not in "?r" and "_dirs" in self.__dict__:
256 if self[f] not in "?r" and "_dirs" in self.__dict__:
256 _decdirs(self._dirs, f)
257 _decdirs(self._dirs, f)
257
258
258 def _addpath(self, f, check=False):
259 def _addpath(self, f, check=False):
259 oldstate = self[f]
260 oldstate = self[f]
260 if check or oldstate == "r":
261 if check or oldstate == "r":
261 if '\r' in f or '\n' in f:
262 if '\r' in f or '\n' in f:
262 raise util.Abort(
263 raise util.Abort(
263 _("'\\n' and '\\r' disallowed in filenames: %r") % f)
264 _("'\\n' and '\\r' disallowed in filenames: %r") % f)
264 if f in self._dirs:
265 if f in self._dirs:
265 raise util.Abort(_('directory %r already in dirstate') % f)
266 raise util.Abort(_('directory %r already in dirstate') % f)
266 # shadows
267 # shadows
267 for d in _finddirs(f):
268 for d in _finddirs(f):
268 if d in self._dirs:
269 if d in self._dirs:
269 break
270 break
270 if d in self._map and self[d] != 'r':
271 if d in self._map and self[d] != 'r':
271 raise util.Abort(
272 raise util.Abort(
272 _('file %r in dirstate clashes with %r') % (d, f))
273 _('file %r in dirstate clashes with %r') % (d, f))
273 if oldstate in "?r" and "_dirs" in self.__dict__:
274 if oldstate in "?r" and "_dirs" in self.__dict__:
274 _incdirs(self._dirs, f)
275 _incdirs(self._dirs, f)
275
276
276 def normal(self, f):
277 def normal(self, f):
277 '''Mark a file normal and clean.'''
278 '''Mark a file normal and clean.'''
278 self._dirty = True
279 self._dirty = True
279 self._addpath(f)
280 self._addpath(f)
280 s = os.lstat(self._join(f))
281 s = os.lstat(self._join(f))
281 self._map[f] = ('n', s.st_mode, s.st_size, int(s.st_mtime))
282 self._map[f] = ('n', s.st_mode, s.st_size, int(s.st_mtime))
282 if f in self._copymap:
283 if f in self._copymap:
283 del self._copymap[f]
284 del self._copymap[f]
284
285
285 def normallookup(self, f):
286 def normallookup(self, f):
286 '''Mark a file normal, but possibly dirty.'''
287 '''Mark a file normal, but possibly dirty.'''
287 if self._pl[1] != nullid and f in self._map:
288 if self._pl[1] != nullid and f in self._map:
288 # if there is a merge going on and the file was either
289 # if there is a merge going on and the file was either
289 # in state 'm' (-1) or coming from other parent (-2) before
290 # in state 'm' (-1) or coming from other parent (-2) before
290 # being removed, restore that state.
291 # being removed, restore that state.
291 entry = self._map[f]
292 entry = self._map[f]
292 if entry[0] == 'r' and entry[2] in (-1, -2):
293 if entry[0] == 'r' and entry[2] in (-1, -2):
293 source = self._copymap.get(f)
294 source = self._copymap.get(f)
294 if entry[2] == -1:
295 if entry[2] == -1:
295 self.merge(f)
296 self.merge(f)
296 elif entry[2] == -2:
297 elif entry[2] == -2:
297 self.otherparent(f)
298 self.otherparent(f)
298 if source:
299 if source:
299 self.copy(source, f)
300 self.copy(source, f)
300 return
301 return
301 if entry[0] == 'm' or entry[0] == 'n' and entry[2] == -2:
302 if entry[0] == 'm' or entry[0] == 'n' and entry[2] == -2:
302 return
303 return
303 self._dirty = True
304 self._dirty = True
304 self._addpath(f)
305 self._addpath(f)
305 self._map[f] = ('n', 0, -1, -1)
306 self._map[f] = ('n', 0, -1, -1)
306 if f in self._copymap:
307 if f in self._copymap:
307 del self._copymap[f]
308 del self._copymap[f]
308
309
309 def otherparent(self, f):
310 def otherparent(self, f):
310 '''Mark as coming from the other parent, always dirty.'''
311 '''Mark as coming from the other parent, always dirty.'''
311 if self._pl[1] == nullid:
312 if self._pl[1] == nullid:
312 raise util.Abort(_("setting %r to other parent "
313 raise util.Abort(_("setting %r to other parent "
313 "only allowed in merges") % f)
314 "only allowed in merges") % f)
314 self._dirty = True
315 self._dirty = True
315 self._addpath(f)
316 self._addpath(f)
316 self._map[f] = ('n', 0, -2, -1)
317 self._map[f] = ('n', 0, -2, -1)
317 if f in self._copymap:
318 if f in self._copymap:
318 del self._copymap[f]
319 del self._copymap[f]
319
320
320 def add(self, f):
321 def add(self, f):
321 '''Mark a file added.'''
322 '''Mark a file added.'''
322 self._dirty = True
323 self._dirty = True
323 self._addpath(f, True)
324 self._addpath(f, True)
324 self._map[f] = ('a', 0, -1, -1)
325 self._map[f] = ('a', 0, -1, -1)
325 if f in self._copymap:
326 if f in self._copymap:
326 del self._copymap[f]
327 del self._copymap[f]
327
328
328 def remove(self, f):
329 def remove(self, f):
329 '''Mark a file removed.'''
330 '''Mark a file removed.'''
330 self._dirty = True
331 self._dirty = True
331 self._droppath(f)
332 self._droppath(f)
332 size = 0
333 size = 0
333 if self._pl[1] != nullid and f in self._map:
334 if self._pl[1] != nullid and f in self._map:
334 # backup the previous state
335 # backup the previous state
335 entry = self._map[f]
336 entry = self._map[f]
336 if entry[0] == 'm': # merge
337 if entry[0] == 'm': # merge
337 size = -1
338 size = -1
338 elif entry[0] == 'n' and entry[2] == -2: # other parent
339 elif entry[0] == 'n' and entry[2] == -2: # other parent
339 size = -2
340 size = -2
340 self._map[f] = ('r', 0, size, 0)
341 self._map[f] = ('r', 0, size, 0)
341 if size == 0 and f in self._copymap:
342 if size == 0 and f in self._copymap:
342 del self._copymap[f]
343 del self._copymap[f]
343
344
344 def merge(self, f):
345 def merge(self, f):
345 '''Mark a file merged.'''
346 '''Mark a file merged.'''
346 self._dirty = True
347 self._dirty = True
347 s = os.lstat(self._join(f))
348 s = os.lstat(self._join(f))
348 self._addpath(f)
349 self._addpath(f)
349 self._map[f] = ('m', s.st_mode, s.st_size, int(s.st_mtime))
350 self._map[f] = ('m', s.st_mode, s.st_size, int(s.st_mtime))
350 if f in self._copymap:
351 if f in self._copymap:
351 del self._copymap[f]
352 del self._copymap[f]
352
353
353 def forget(self, f):
354 def forget(self, f):
354 '''Forget a file.'''
355 '''Forget a file.'''
355 self._dirty = True
356 self._dirty = True
356 try:
357 try:
357 self._droppath(f)
358 self._droppath(f)
358 del self._map[f]
359 del self._map[f]
359 except KeyError:
360 except KeyError:
360 self._ui.warn(_("not in dirstate: %s\n") % f)
361 self._ui.warn(_("not in dirstate: %s\n") % f)
361
362
362 def _normalize(self, path, knownpath):
363 def _normalize(self, path, knownpath):
363 norm_path = os.path.normcase(path)
364 norm_path = os.path.normcase(path)
364 fold_path = self._foldmap.get(norm_path, None)
365 fold_path = self._foldmap.get(norm_path, None)
365 if fold_path is None:
366 if fold_path is None:
366 if knownpath or not os.path.lexists(os.path.join(self._root, path)):
367 if knownpath or not os.path.lexists(os.path.join(self._root, path)):
367 fold_path = path
368 fold_path = path
368 else:
369 else:
369 fold_path = self._foldmap.setdefault(norm_path,
370 fold_path = self._foldmap.setdefault(norm_path,
370 util.fspath(path, self._root))
371 util.fspath(path, self._root))
371 return fold_path
372 return fold_path
372
373
373 def clear(self):
374 def clear(self):
374 self._map = {}
375 self._map = {}
375 if "_dirs" in self.__dict__:
376 if "_dirs" in self.__dict__:
376 delattr(self, "_dirs")
377 delattr(self, "_dirs")
377 self._copymap = {}
378 self._copymap = {}
378 self._pl = [nullid, nullid]
379 self._pl = [nullid, nullid]
379 self._dirty = True
380 self._dirty = True
380
381
381 def rebuild(self, parent, files):
382 def rebuild(self, parent, files):
382 self.clear()
383 self.clear()
383 for f in files:
384 for f in files:
384 if 'x' in files.flags(f):
385 if 'x' in files.flags(f):
385 self._map[f] = ('n', 0777, -1, 0)
386 self._map[f] = ('n', 0777, -1, 0)
386 else:
387 else:
387 self._map[f] = ('n', 0666, -1, 0)
388 self._map[f] = ('n', 0666, -1, 0)
388 self._pl = (parent, nullid)
389 self._pl = (parent, nullid)
389 self._dirty = True
390 self._dirty = True
390
391
391 def write(self):
392 def write(self):
392 if not self._dirty:
393 if not self._dirty:
393 return
394 return
394 st = self._opener("dirstate", "w", atomictemp=True)
395 st = self._opener("dirstate", "w", atomictemp=True)
395
396
396 # use the modification time of the newly created temporary file as the
397 # use the modification time of the newly created temporary file as the
397 # filesystem's notion of 'now'
398 # filesystem's notion of 'now'
398 now = int(util.fstat(st).st_mtime)
399 now = int(util.fstat(st).st_mtime)
399
400
400 cs = cStringIO.StringIO()
401 cs = cStringIO.StringIO()
401 copymap = self._copymap
402 copymap = self._copymap
402 pack = struct.pack
403 pack = struct.pack
403 write = cs.write
404 write = cs.write
404 write("".join(self._pl))
405 write("".join(self._pl))
405 for f, e in self._map.iteritems():
406 for f, e in self._map.iteritems():
406 if e[0] == 'n' and e[3] == now:
407 if e[0] == 'n' and e[3] == now:
407 # The file was last modified "simultaneously" with the current
408 # The file was last modified "simultaneously" with the current
408 # write to dirstate (i.e. within the same second for file-
409 # write to dirstate (i.e. within the same second for file-
409 # systems with a granularity of 1 sec). This commonly happens
410 # systems with a granularity of 1 sec). This commonly happens
410 # for at least a couple of files on 'update'.
411 # for at least a couple of files on 'update'.
411 # The user could change the file without changing its size
412 # The user could change the file without changing its size
412 # within the same second. Invalidate the file's stat data in
413 # within the same second. Invalidate the file's stat data in
413 # dirstate, forcing future 'status' calls to compare the
414 # dirstate, forcing future 'status' calls to compare the
414 # contents of the file. This prevents mistakenly treating such
415 # contents of the file. This prevents mistakenly treating such
415 # files as clean.
416 # files as clean.
416 e = (e[0], 0, -1, -1) # mark entry as 'unset'
417 e = (e[0], 0, -1, -1) # mark entry as 'unset'
417 self._map[f] = e
418 self._map[f] = e
418
419
419 if f in copymap:
420 if f in copymap:
420 f = "%s\0%s" % (f, copymap[f])
421 f = "%s\0%s" % (f, copymap[f])
421 e = pack(_format, e[0], e[1], e[2], e[3], len(f))
422 e = pack(_format, e[0], e[1], e[2], e[3], len(f))
422 write(e)
423 write(e)
423 write(f)
424 write(f)
424 st.write(cs.getvalue())
425 st.write(cs.getvalue())
425 st.rename()
426 st.rename()
426 self._dirty = self._dirtypl = False
427 self._dirty = self._dirtypl = False
427
428
428 def _dirignore(self, f):
429 def _dirignore(self, f):
429 if f == '.':
430 if f == '.':
430 return False
431 return False
431 if self._ignore(f):
432 if self._ignore(f):
432 return True
433 return True
433 for p in _finddirs(f):
434 for p in _finddirs(f):
434 if self._ignore(p):
435 if self._ignore(p):
435 return True
436 return True
436 return False
437 return False
437
438
438 def walk(self, match, subrepos, unknown, ignored):
439 def walk(self, match, subrepos, unknown, ignored):
439 '''
440 '''
440 Walk recursively through the directory tree, finding all files
441 Walk recursively through the directory tree, finding all files
441 matched by match.
442 matched by match.
442
443
443 Return a dict mapping filename to stat-like object (either
444 Return a dict mapping filename to stat-like object (either
444 mercurial.osutil.stat instance or return value of os.stat()).
445 mercurial.osutil.stat instance or return value of os.stat()).
445 '''
446 '''
446
447
447 def fwarn(f, msg):
448 def fwarn(f, msg):
448 self._ui.warn('%s: %s\n' % (self.pathto(f), msg))
449 self._ui.warn('%s: %s\n' % (self.pathto(f), msg))
449 return False
450 return False
450
451
451 def badtype(mode):
452 def badtype(mode):
452 kind = _('unknown')
453 kind = _('unknown')
453 if stat.S_ISCHR(mode):
454 if stat.S_ISCHR(mode):
454 kind = _('character device')
455 kind = _('character device')
455 elif stat.S_ISBLK(mode):
456 elif stat.S_ISBLK(mode):
456 kind = _('block device')
457 kind = _('block device')
457 elif stat.S_ISFIFO(mode):
458 elif stat.S_ISFIFO(mode):
458 kind = _('fifo')
459 kind = _('fifo')
459 elif stat.S_ISSOCK(mode):
460 elif stat.S_ISSOCK(mode):
460 kind = _('socket')
461 kind = _('socket')
461 elif stat.S_ISDIR(mode):
462 elif stat.S_ISDIR(mode):
462 kind = _('directory')
463 kind = _('directory')
463 return _('unsupported file type (type is %s)') % kind
464 return _('unsupported file type (type is %s)') % kind
464
465
465 ignore = self._ignore
466 ignore = self._ignore
466 dirignore = self._dirignore
467 dirignore = self._dirignore
467 if ignored:
468 if ignored:
468 ignore = util.never
469 ignore = util.never
469 dirignore = util.never
470 dirignore = util.never
470 elif not unknown:
471 elif not unknown:
471 # if unknown and ignored are False, skip step 2
472 # if unknown and ignored are False, skip step 2
472 ignore = util.always
473 ignore = util.always
473 dirignore = util.always
474 dirignore = util.always
474
475
475 matchfn = match.matchfn
476 matchfn = match.matchfn
476 badfn = match.bad
477 badfn = match.bad
477 dmap = self._map
478 dmap = self._map
478 normpath = util.normpath
479 normpath = util.normpath
479 listdir = osutil.listdir
480 listdir = osutil.listdir
480 lstat = os.lstat
481 lstat = os.lstat
481 getkind = stat.S_IFMT
482 getkind = stat.S_IFMT
482 dirkind = stat.S_IFDIR
483 dirkind = stat.S_IFDIR
483 regkind = stat.S_IFREG
484 regkind = stat.S_IFREG
484 lnkkind = stat.S_IFLNK
485 lnkkind = stat.S_IFLNK
485 join = self._join
486 join = self._join
486 work = []
487 work = []
487 wadd = work.append
488 wadd = work.append
488
489
489 exact = skipstep3 = False
490 exact = skipstep3 = False
490 if matchfn == match.exact: # match.exact
491 if matchfn == match.exact: # match.exact
491 exact = True
492 exact = True
492 dirignore = util.always # skip step 2
493 dirignore = util.always # skip step 2
493 elif match.files() and not match.anypats(): # match.match, no patterns
494 elif match.files() and not match.anypats(): # match.match, no patterns
494 skipstep3 = True
495 skipstep3 = True
495
496
496 if self._checkcase:
497 if self._checkcase:
497 normalize = self._normalize
498 normalize = self._normalize
498 skipstep3 = False
499 skipstep3 = False
499 else:
500 else:
500 normalize = lambda x, y: x
501 normalize = lambda x, y: x
501
502
502 files = sorted(match.files())
503 files = sorted(match.files())
503 subrepos.sort()
504 subrepos.sort()
504 i, j = 0, 0
505 i, j = 0, 0
505 while i < len(files) and j < len(subrepos):
506 while i < len(files) and j < len(subrepos):
506 subpath = subrepos[j] + "/"
507 subpath = subrepos[j] + "/"
507 if not files[i].startswith(subpath):
508 if not files[i].startswith(subpath):
508 i += 1
509 i += 1
509 continue
510 continue
510 while files and files[i].startswith(subpath):
511 while files and files[i].startswith(subpath):
511 del files[i]
512 del files[i]
512 j += 1
513 j += 1
513
514
514 if not files or '.' in files:
515 if not files or '.' in files:
515 files = ['']
516 files = ['']
516 results = dict.fromkeys(subrepos)
517 results = dict.fromkeys(subrepos)
517 results['.hg'] = None
518 results['.hg'] = None
518
519
519 # step 1: find all explicit files
520 # step 1: find all explicit files
520 for ff in files:
521 for ff in files:
521 nf = normalize(normpath(ff), False)
522 nf = normalize(normpath(ff), False)
522 if nf in results:
523 if nf in results:
523 continue
524 continue
524
525
525 try:
526 try:
526 st = lstat(join(nf))
527 st = lstat(join(nf))
527 kind = getkind(st.st_mode)
528 kind = getkind(st.st_mode)
528 if kind == dirkind:
529 if kind == dirkind:
529 skipstep3 = False
530 skipstep3 = False
530 if nf in dmap:
531 if nf in dmap:
531 #file deleted on disk but still in dirstate
532 #file deleted on disk but still in dirstate
532 results[nf] = None
533 results[nf] = None
533 match.dir(nf)
534 match.dir(nf)
534 if not dirignore(nf):
535 if not dirignore(nf):
535 wadd(nf)
536 wadd(nf)
536 elif kind == regkind or kind == lnkkind:
537 elif kind == regkind or kind == lnkkind:
537 results[nf] = st
538 results[nf] = st
538 else:
539 else:
539 badfn(ff, badtype(kind))
540 badfn(ff, badtype(kind))
540 if nf in dmap:
541 if nf in dmap:
541 results[nf] = None
542 results[nf] = None
542 except OSError, inst:
543 except OSError, inst:
543 if nf in dmap: # does it exactly match a file?
544 if nf in dmap: # does it exactly match a file?
544 results[nf] = None
545 results[nf] = None
545 else: # does it match a directory?
546 else: # does it match a directory?
546 prefix = nf + "/"
547 prefix = nf + "/"
547 for fn in dmap:
548 for fn in dmap:
548 if fn.startswith(prefix):
549 if fn.startswith(prefix):
549 match.dir(nf)
550 match.dir(nf)
550 skipstep3 = False
551 skipstep3 = False
551 break
552 break
552 else:
553 else:
553 badfn(ff, inst.strerror)
554 badfn(ff, inst.strerror)
554
555
555 # step 2: visit subdirectories
556 # step 2: visit subdirectories
556 while work:
557 while work:
557 nd = work.pop()
558 nd = work.pop()
558 skip = None
559 skip = None
559 if nd == '.':
560 if nd == '.':
560 nd = ''
561 nd = ''
561 else:
562 else:
562 skip = '.hg'
563 skip = '.hg'
563 try:
564 try:
564 entries = listdir(join(nd), stat=True, skip=skip)
565 entries = listdir(join(nd), stat=True, skip=skip)
565 except OSError, inst:
566 except OSError, inst:
566 if inst.errno == errno.EACCES:
567 if inst.errno == errno.EACCES:
567 fwarn(nd, inst.strerror)
568 fwarn(nd, inst.strerror)
568 continue
569 continue
569 raise
570 raise
570 for f, kind, st in entries:
571 for f, kind, st in entries:
571 nf = normalize(nd and (nd + "/" + f) or f, True)
572 nf = normalize(nd and (nd + "/" + f) or f, True)
572 if nf not in results:
573 if nf not in results:
573 if kind == dirkind:
574 if kind == dirkind:
574 if not ignore(nf):
575 if not ignore(nf):
575 match.dir(nf)
576 match.dir(nf)
576 wadd(nf)
577 wadd(nf)
577 if nf in dmap and matchfn(nf):
578 if nf in dmap and matchfn(nf):
578 results[nf] = None
579 results[nf] = None
579 elif kind == regkind or kind == lnkkind:
580 elif kind == regkind or kind == lnkkind:
580 if nf in dmap:
581 if nf in dmap:
581 if matchfn(nf):
582 if matchfn(nf):
582 results[nf] = st
583 results[nf] = st
583 elif matchfn(nf) and not ignore(nf):
584 elif matchfn(nf) and not ignore(nf):
584 results[nf] = st
585 results[nf] = st
585 elif nf in dmap and matchfn(nf):
586 elif nf in dmap and matchfn(nf):
586 results[nf] = None
587 results[nf] = None
587
588
588 # step 3: report unseen items in the dmap hash
589 # step 3: report unseen items in the dmap hash
589 if not skipstep3 and not exact:
590 if not skipstep3 and not exact:
590 visit = sorted([f for f in dmap if f not in results and matchfn(f)])
591 visit = sorted([f for f in dmap if f not in results and matchfn(f)])
591 for nf, st in zip(visit, util.statfiles([join(i) for i in visit])):
592 for nf, st in zip(visit, util.statfiles([join(i) for i in visit])):
592 if not st is None and not getkind(st.st_mode) in (regkind, lnkkind):
593 if not st is None and not getkind(st.st_mode) in (regkind, lnkkind):
593 st = None
594 st = None
594 results[nf] = st
595 results[nf] = st
595 for s in subrepos:
596 for s in subrepos:
596 del results[s]
597 del results[s]
597 del results['.hg']
598 del results['.hg']
598 return results
599 return results
599
600
600 def status(self, match, subrepos, ignored, clean, unknown):
601 def status(self, match, subrepos, ignored, clean, unknown):
601 '''Determine the status of the working copy relative to the
602 '''Determine the status of the working copy relative to the
602 dirstate and return a tuple of lists (unsure, modified, added,
603 dirstate and return a tuple of lists (unsure, modified, added,
603 removed, deleted, unknown, ignored, clean), where:
604 removed, deleted, unknown, ignored, clean), where:
604
605
605 unsure:
606 unsure:
606 files that might have been modified since the dirstate was
607 files that might have been modified since the dirstate was
607 written, but need to be read to be sure (size is the same
608 written, but need to be read to be sure (size is the same
608 but mtime differs)
609 but mtime differs)
609 modified:
610 modified:
610 files that have definitely been modified since the dirstate
611 files that have definitely been modified since the dirstate
611 was written (different size or mode)
612 was written (different size or mode)
612 added:
613 added:
613 files that have been explicitly added with hg add
614 files that have been explicitly added with hg add
614 removed:
615 removed:
615 files that have been explicitly removed with hg remove
616 files that have been explicitly removed with hg remove
616 deleted:
617 deleted:
617 files that have been deleted through other means ("missing")
618 files that have been deleted through other means ("missing")
618 unknown:
619 unknown:
619 files not in the dirstate that are not ignored
620 files not in the dirstate that are not ignored
620 ignored:
621 ignored:
621 files not in the dirstate that are ignored
622 files not in the dirstate that are ignored
622 (by _dirignore())
623 (by _dirignore())
623 clean:
624 clean:
624 files that have definitely not been modified since the
625 files that have definitely not been modified since the
625 dirstate was written
626 dirstate was written
626 '''
627 '''
627 listignored, listclean, listunknown = ignored, clean, unknown
628 listignored, listclean, listunknown = ignored, clean, unknown
628 lookup, modified, added, unknown, ignored = [], [], [], [], []
629 lookup, modified, added, unknown, ignored = [], [], [], [], []
629 removed, deleted, clean = [], [], []
630 removed, deleted, clean = [], [], []
630
631
631 dmap = self._map
632 dmap = self._map
632 ladd = lookup.append # aka "unsure"
633 ladd = lookup.append # aka "unsure"
633 madd = modified.append
634 madd = modified.append
634 aadd = added.append
635 aadd = added.append
635 uadd = unknown.append
636 uadd = unknown.append
636 iadd = ignored.append
637 iadd = ignored.append
637 radd = removed.append
638 radd = removed.append
638 dadd = deleted.append
639 dadd = deleted.append
639 cadd = clean.append
640 cadd = clean.append
640
641
641 lnkkind = stat.S_IFLNK
642 lnkkind = stat.S_IFLNK
642
643
643 for fn, st in self.walk(match, subrepos, listunknown,
644 for fn, st in self.walk(match, subrepos, listunknown,
644 listignored).iteritems():
645 listignored).iteritems():
645 if fn not in dmap:
646 if fn not in dmap:
646 if (listignored or match.exact(fn)) and self._dirignore(fn):
647 if (listignored or match.exact(fn)) and self._dirignore(fn):
647 if listignored:
648 if listignored:
648 iadd(fn)
649 iadd(fn)
649 elif listunknown:
650 elif listunknown:
650 uadd(fn)
651 uadd(fn)
651 continue
652 continue
652
653
653 state, mode, size, time = dmap[fn]
654 state, mode, size, time = dmap[fn]
654
655
655 if not st and state in "nma":
656 if not st and state in "nma":
656 dadd(fn)
657 dadd(fn)
657 elif state == 'n':
658 elif state == 'n':
658 # The "mode & lnkkind != lnkkind or self._checklink"
659 # The "mode & lnkkind != lnkkind or self._checklink"
659 # lines are an expansion of "islink => checklink"
660 # lines are an expansion of "islink => checklink"
660 # where islink means "is this a link?" and checklink
661 # where islink means "is this a link?" and checklink
661 # means "can we check links?".
662 # means "can we check links?".
662 if (size >= 0 and
663 if (size >= 0 and
663 (size != st.st_size
664 (size != st.st_size
664 or ((mode ^ st.st_mode) & 0100 and self._checkexec))
665 or ((mode ^ st.st_mode) & 0100 and self._checkexec))
665 and (mode & lnkkind != lnkkind or self._checklink)
666 and (mode & lnkkind != lnkkind or self._checklink)
666 or size == -2 # other parent
667 or size == -2 # other parent
667 or fn in self._copymap):
668 or fn in self._copymap):
668 madd(fn)
669 madd(fn)
669 elif (time != int(st.st_mtime)
670 elif (time != int(st.st_mtime)
670 and (mode & lnkkind != lnkkind or self._checklink)):
671 and (mode & lnkkind != lnkkind or self._checklink)):
671 ladd(fn)
672 ladd(fn)
672 elif listclean:
673 elif listclean:
673 cadd(fn)
674 cadd(fn)
674 elif state == 'm':
675 elif state == 'm':
675 madd(fn)
676 madd(fn)
676 elif state == 'a':
677 elif state == 'a':
677 aadd(fn)
678 aadd(fn)
678 elif state == 'r':
679 elif state == 'r':
679 radd(fn)
680 radd(fn)
680
681
681 return (lookup, modified, added, removed, deleted, unknown, ignored,
682 return (lookup, modified, added, removed, deleted, unknown, ignored,
682 clean)
683 clean)
@@ -1,1938 +1,1938 b''
1 # localrepo.py - read/write repository class for mercurial
1 # localrepo.py - read/write repository class for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from node import bin, hex, nullid, nullrev, short
8 from node import bin, hex, nullid, nullrev, short
9 from i18n import _
9 from i18n import _
10 import repo, changegroup, subrepo, discovery, pushkey
10 import repo, changegroup, subrepo, discovery, pushkey
11 import changelog, dirstate, filelog, manifest, context
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 import url as urlmod
18 from lock import release
18 from lock import release
19 import weakref, errno, os, time, inspect
19 import weakref, errno, os, time, inspect
20 propertycache = util.propertycache
20 propertycache = util.propertycache
21
21
22 class localrepository(repo.repository):
22 class localrepository(repo.repository):
23 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey'))
23 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey'))
24 supportedformats = set(('revlogv1', 'parentdelta'))
24 supportedformats = set(('revlogv1', 'parentdelta'))
25 supported = supportedformats | set(('store', 'fncache', 'shared',
25 supported = supportedformats | set(('store', 'fncache', 'shared',
26 'dotencode'))
26 'dotencode'))
27
27
28 def __init__(self, baseui, path=None, create=0):
28 def __init__(self, baseui, path=None, create=0):
29 repo.repository.__init__(self)
29 repo.repository.__init__(self)
30 self.root = os.path.realpath(util.expandpath(path))
30 self.root = os.path.realpath(util.expandpath(path))
31 self.path = os.path.join(self.root, ".hg")
31 self.path = os.path.join(self.root, ".hg")
32 self.origroot = path
32 self.origroot = path
33 self.auditor = util.path_auditor(self.root, self._checknested)
33 self.auditor = util.path_auditor(self.root, self._checknested)
34 self.opener = util.opener(self.path)
34 self.opener = util.opener(self.path)
35 self.wopener = util.opener(self.root)
35 self.wopener = util.opener(self.root)
36 self.baseui = baseui
36 self.baseui = baseui
37 self.ui = baseui.copy()
37 self.ui = baseui.copy()
38
38
39 try:
39 try:
40 self.ui.readconfig(self.join("hgrc"), self.root)
40 self.ui.readconfig(self.join("hgrc"), self.root)
41 extensions.loadall(self.ui)
41 extensions.loadall(self.ui)
42 except IOError:
42 except IOError:
43 pass
43 pass
44
44
45 if not os.path.isdir(self.path):
45 if not os.path.isdir(self.path):
46 if create:
46 if create:
47 if not os.path.exists(path):
47 if not os.path.exists(path):
48 util.makedirs(path)
48 util.makedirs(path)
49 os.mkdir(self.path)
49 os.mkdir(self.path)
50 requirements = ["revlogv1"]
50 requirements = ["revlogv1"]
51 if self.ui.configbool('format', 'usestore', True):
51 if self.ui.configbool('format', 'usestore', True):
52 os.mkdir(os.path.join(self.path, "store"))
52 os.mkdir(os.path.join(self.path, "store"))
53 requirements.append("store")
53 requirements.append("store")
54 if self.ui.configbool('format', 'usefncache', True):
54 if self.ui.configbool('format', 'usefncache', True):
55 requirements.append("fncache")
55 requirements.append("fncache")
56 if self.ui.configbool('format', 'dotencode', True):
56 if self.ui.configbool('format', 'dotencode', True):
57 requirements.append('dotencode')
57 requirements.append('dotencode')
58 # create an invalid changelog
58 # create an invalid changelog
59 self.opener("00changelog.i", "a").write(
59 self.opener("00changelog.i", "a").write(
60 '\0\0\0\2' # represents revlogv2
60 '\0\0\0\2' # represents revlogv2
61 ' dummy changelog to prevent using the old repo layout'
61 ' dummy changelog to prevent using the old repo layout'
62 )
62 )
63 if self.ui.configbool('format', 'parentdelta', False):
63 if self.ui.configbool('format', 'parentdelta', False):
64 requirements.append("parentdelta")
64 requirements.append("parentdelta")
65 else:
65 else:
66 raise error.RepoError(_("repository %s not found") % path)
66 raise error.RepoError(_("repository %s not found") % path)
67 elif create:
67 elif create:
68 raise error.RepoError(_("repository %s already exists") % path)
68 raise error.RepoError(_("repository %s already exists") % path)
69 else:
69 else:
70 # find requirements
70 # find requirements
71 requirements = set()
71 requirements = set()
72 try:
72 try:
73 requirements = set(self.opener("requires").read().splitlines())
73 requirements = set(self.opener("requires").read().splitlines())
74 except IOError, inst:
74 except IOError, inst:
75 if inst.errno != errno.ENOENT:
75 if inst.errno != errno.ENOENT:
76 raise
76 raise
77 for r in requirements - self.supported:
77 for r in requirements - self.supported:
78 raise error.RepoError(_("requirement '%s' not supported") % r)
78 raise error.RepoError(_("requirement '%s' not supported") % r)
79
79
80 self.sharedpath = self.path
80 self.sharedpath = self.path
81 try:
81 try:
82 s = os.path.realpath(self.opener("sharedpath").read())
82 s = os.path.realpath(self.opener("sharedpath").read())
83 if not os.path.exists(s):
83 if not os.path.exists(s):
84 raise error.RepoError(
84 raise error.RepoError(
85 _('.hg/sharedpath points to nonexistent directory %s') % s)
85 _('.hg/sharedpath points to nonexistent directory %s') % s)
86 self.sharedpath = s
86 self.sharedpath = s
87 except IOError, inst:
87 except IOError, inst:
88 if inst.errno != errno.ENOENT:
88 if inst.errno != errno.ENOENT:
89 raise
89 raise
90
90
91 self.store = store.store(requirements, self.sharedpath, util.opener)
91 self.store = store.store(requirements, self.sharedpath, util.opener)
92 self.spath = self.store.path
92 self.spath = self.store.path
93 self.sopener = self.store.opener
93 self.sopener = self.store.opener
94 self.sjoin = self.store.join
94 self.sjoin = self.store.join
95 self.opener.createmode = self.store.createmode
95 self.opener.createmode = self.store.createmode
96 self._applyrequirements(requirements)
96 self._applyrequirements(requirements)
97 if create:
97 if create:
98 self._writerequirements()
98 self._writerequirements()
99
99
100 # These two define the set of tags for this repository. _tags
100 # These two define the set of tags for this repository. _tags
101 # maps tag name to node; _tagtypes maps tag name to 'global' or
101 # maps tag name to node; _tagtypes maps tag name to 'global' or
102 # 'local'. (Global tags are defined by .hgtags across all
102 # 'local'. (Global tags are defined by .hgtags across all
103 # heads, and local tags are defined in .hg/localtags.) They
103 # heads, and local tags are defined in .hg/localtags.) They
104 # constitute the in-memory cache of tags.
104 # constitute the in-memory cache of tags.
105 self._tags = None
105 self._tags = None
106 self._tagtypes = None
106 self._tagtypes = None
107
107
108 self._branchcache = None
108 self._branchcache = None
109 self._branchcachetip = None
109 self._branchcachetip = None
110 self.nodetagscache = None
110 self.nodetagscache = None
111 self.filterpats = {}
111 self.filterpats = {}
112 self._datafilters = {}
112 self._datafilters = {}
113 self._transref = self._lockref = self._wlockref = None
113 self._transref = self._lockref = self._wlockref = None
114
114
115 def _applyrequirements(self, requirements):
115 def _applyrequirements(self, requirements):
116 self.requirements = requirements
116 self.requirements = requirements
117 self.sopener.options = {}
117 self.sopener.options = {}
118 if 'parentdelta' in requirements:
118 if 'parentdelta' in requirements:
119 self.sopener.options['parentdelta'] = 1
119 self.sopener.options['parentdelta'] = 1
120
120
121 def _writerequirements(self):
121 def _writerequirements(self):
122 reqfile = self.opener("requires", "w")
122 reqfile = self.opener("requires", "w")
123 for r in self.requirements:
123 for r in self.requirements:
124 reqfile.write("%s\n" % r)
124 reqfile.write("%s\n" % r)
125 reqfile.close()
125 reqfile.close()
126
126
127 def _checknested(self, path):
127 def _checknested(self, path):
128 """Determine if path is a legal nested repository."""
128 """Determine if path is a legal nested repository."""
129 if not path.startswith(self.root):
129 if not path.startswith(self.root):
130 return False
130 return False
131 subpath = path[len(self.root) + 1:]
131 subpath = path[len(self.root) + 1:]
132
132
133 # XXX: Checking against the current working copy is wrong in
133 # XXX: Checking against the current working copy is wrong in
134 # the sense that it can reject things like
134 # the sense that it can reject things like
135 #
135 #
136 # $ hg cat -r 10 sub/x.txt
136 # $ hg cat -r 10 sub/x.txt
137 #
137 #
138 # if sub/ is no longer a subrepository in the working copy
138 # if sub/ is no longer a subrepository in the working copy
139 # parent revision.
139 # parent revision.
140 #
140 #
141 # However, it can of course also allow things that would have
141 # However, it can of course also allow things that would have
142 # been rejected before, such as the above cat command if sub/
142 # been rejected before, such as the above cat command if sub/
143 # is a subrepository now, but was a normal directory before.
143 # is a subrepository now, but was a normal directory before.
144 # The old path auditor would have rejected by mistake since it
144 # The old path auditor would have rejected by mistake since it
145 # panics when it sees sub/.hg/.
145 # panics when it sees sub/.hg/.
146 #
146 #
147 # All in all, checking against the working copy seems sensible
147 # All in all, checking against the working copy seems sensible
148 # since we want to prevent access to nested repositories on
148 # since we want to prevent access to nested repositories on
149 # the filesystem *now*.
149 # the filesystem *now*.
150 ctx = self[None]
150 ctx = self[None]
151 parts = util.splitpath(subpath)
151 parts = util.splitpath(subpath)
152 while parts:
152 while parts:
153 prefix = os.sep.join(parts)
153 prefix = os.sep.join(parts)
154 if prefix in ctx.substate:
154 if prefix in ctx.substate:
155 if prefix == subpath:
155 if prefix == subpath:
156 return True
156 return True
157 else:
157 else:
158 sub = ctx.sub(prefix)
158 sub = ctx.sub(prefix)
159 return sub.checknested(subpath[len(prefix) + 1:])
159 return sub.checknested(subpath[len(prefix) + 1:])
160 else:
160 else:
161 parts.pop()
161 parts.pop()
162 return False
162 return False
163
163
164
164
165 @propertycache
165 @propertycache
166 def changelog(self):
166 def changelog(self):
167 c = changelog.changelog(self.sopener)
167 c = changelog.changelog(self.sopener)
168 if 'HG_PENDING' in os.environ:
168 if 'HG_PENDING' in os.environ:
169 p = os.environ['HG_PENDING']
169 p = os.environ['HG_PENDING']
170 if p.startswith(self.root):
170 if p.startswith(self.root):
171 c.readpending('00changelog.i.a')
171 c.readpending('00changelog.i.a')
172 self.sopener.options['defversion'] = c.version
172 self.sopener.options['defversion'] = c.version
173 return c
173 return c
174
174
175 @propertycache
175 @propertycache
176 def manifest(self):
176 def manifest(self):
177 return manifest.manifest(self.sopener)
177 return manifest.manifest(self.sopener)
178
178
179 @propertycache
179 @propertycache
180 def dirstate(self):
180 def dirstate(self):
181 warned = [0]
181 warned = [0]
182 def validate(node):
182 def validate(node):
183 try:
183 try:
184 r = self.changelog.rev(node)
184 r = self.changelog.rev(node)
185 return node
185 return node
186 except error.LookupError:
186 except error.LookupError:
187 if not warned[0]:
187 if not warned[0]:
188 warned[0] = True
188 warned[0] = True
189 self.ui.warn(_("warning: ignoring unknown"
189 self.ui.warn(_("warning: ignoring unknown"
190 " working parent %s!\n") % short(node))
190 " working parent %s!\n") % short(node))
191 return nullid
191 return nullid
192
192
193 return dirstate.dirstate(self.opener, self.ui, self.root, validate)
193 return dirstate.dirstate(self.opener, self.ui, self.root, validate)
194
194
195 def __getitem__(self, changeid):
195 def __getitem__(self, changeid):
196 if changeid is None:
196 if changeid is None:
197 return context.workingctx(self)
197 return context.workingctx(self)
198 return context.changectx(self, changeid)
198 return context.changectx(self, changeid)
199
199
200 def __contains__(self, changeid):
200 def __contains__(self, changeid):
201 try:
201 try:
202 return bool(self.lookup(changeid))
202 return bool(self.lookup(changeid))
203 except error.RepoLookupError:
203 except error.RepoLookupError:
204 return False
204 return False
205
205
206 def __nonzero__(self):
206 def __nonzero__(self):
207 return True
207 return True
208
208
209 def __len__(self):
209 def __len__(self):
210 return len(self.changelog)
210 return len(self.changelog)
211
211
212 def __iter__(self):
212 def __iter__(self):
213 for i in xrange(len(self)):
213 for i in xrange(len(self)):
214 yield i
214 yield i
215
215
216 def url(self):
216 def url(self):
217 return 'file:' + self.root
217 return 'file:' + self.root
218
218
219 def hook(self, name, throw=False, **args):
219 def hook(self, name, throw=False, **args):
220 return hook.hook(self.ui, self, name, throw, **args)
220 return hook.hook(self.ui, self, name, throw, **args)
221
221
222 tag_disallowed = ':\r\n'
222 tag_disallowed = ':\r\n'
223
223
224 def _tag(self, names, node, message, local, user, date, extra={}):
224 def _tag(self, names, node, message, local, user, date, extra={}):
225 if isinstance(names, str):
225 if isinstance(names, str):
226 allchars = names
226 allchars = names
227 names = (names,)
227 names = (names,)
228 else:
228 else:
229 allchars = ''.join(names)
229 allchars = ''.join(names)
230 for c in self.tag_disallowed:
230 for c in self.tag_disallowed:
231 if c in allchars:
231 if c in allchars:
232 raise util.Abort(_('%r cannot be used in a tag name') % c)
232 raise util.Abort(_('%r cannot be used in a tag name') % c)
233
233
234 branches = self.branchmap()
234 branches = self.branchmap()
235 for name in names:
235 for name in names:
236 self.hook('pretag', throw=True, node=hex(node), tag=name,
236 self.hook('pretag', throw=True, node=hex(node), tag=name,
237 local=local)
237 local=local)
238 if name in branches:
238 if name in branches:
239 self.ui.warn(_("warning: tag %s conflicts with existing"
239 self.ui.warn(_("warning: tag %s conflicts with existing"
240 " branch name\n") % name)
240 " branch name\n") % name)
241
241
242 def writetags(fp, names, munge, prevtags):
242 def writetags(fp, names, munge, prevtags):
243 fp.seek(0, 2)
243 fp.seek(0, 2)
244 if prevtags and prevtags[-1] != '\n':
244 if prevtags and prevtags[-1] != '\n':
245 fp.write('\n')
245 fp.write('\n')
246 for name in names:
246 for name in names:
247 m = munge and munge(name) or name
247 m = munge and munge(name) or name
248 if self._tagtypes and name in self._tagtypes:
248 if self._tagtypes and name in self._tagtypes:
249 old = self._tags.get(name, nullid)
249 old = self._tags.get(name, nullid)
250 fp.write('%s %s\n' % (hex(old), m))
250 fp.write('%s %s\n' % (hex(old), m))
251 fp.write('%s %s\n' % (hex(node), m))
251 fp.write('%s %s\n' % (hex(node), m))
252 fp.close()
252 fp.close()
253
253
254 prevtags = ''
254 prevtags = ''
255 if local:
255 if local:
256 try:
256 try:
257 fp = self.opener('localtags', 'r+')
257 fp = self.opener('localtags', 'r+')
258 except IOError:
258 except IOError:
259 fp = self.opener('localtags', 'a')
259 fp = self.opener('localtags', 'a')
260 else:
260 else:
261 prevtags = fp.read()
261 prevtags = fp.read()
262
262
263 # local tags are stored in the current charset
263 # local tags are stored in the current charset
264 writetags(fp, names, None, prevtags)
264 writetags(fp, names, None, prevtags)
265 for name in names:
265 for name in names:
266 self.hook('tag', node=hex(node), tag=name, local=local)
266 self.hook('tag', node=hex(node), tag=name, local=local)
267 return
267 return
268
268
269 try:
269 try:
270 fp = self.wfile('.hgtags', 'rb+')
270 fp = self.wfile('.hgtags', 'rb+')
271 except IOError:
271 except IOError:
272 fp = self.wfile('.hgtags', 'ab')
272 fp = self.wfile('.hgtags', 'ab')
273 else:
273 else:
274 prevtags = fp.read()
274 prevtags = fp.read()
275
275
276 # committed tags are stored in UTF-8
276 # committed tags are stored in UTF-8
277 writetags(fp, names, encoding.fromlocal, prevtags)
277 writetags(fp, names, encoding.fromlocal, prevtags)
278
278
279 if '.hgtags' not in self.dirstate:
279 if '.hgtags' not in self.dirstate:
280 self[None].add(['.hgtags'])
280 self[None].add(['.hgtags'])
281
281
282 m = matchmod.exact(self.root, '', ['.hgtags'])
282 m = matchmod.exact(self.root, '', ['.hgtags'])
283 tagnode = self.commit(message, user, date, extra=extra, match=m)
283 tagnode = self.commit(message, user, date, extra=extra, match=m)
284
284
285 for name in names:
285 for name in names:
286 self.hook('tag', node=hex(node), tag=name, local=local)
286 self.hook('tag', node=hex(node), tag=name, local=local)
287
287
288 return tagnode
288 return tagnode
289
289
290 def tag(self, names, node, message, local, user, date):
290 def tag(self, names, node, message, local, user, date):
291 '''tag a revision with one or more symbolic names.
291 '''tag a revision with one or more symbolic names.
292
292
293 names is a list of strings or, when adding a single tag, names may be a
293 names is a list of strings or, when adding a single tag, names may be a
294 string.
294 string.
295
295
296 if local is True, the tags are stored in a per-repository file.
296 if local is True, the tags are stored in a per-repository file.
297 otherwise, they are stored in the .hgtags file, and a new
297 otherwise, they are stored in the .hgtags file, and a new
298 changeset is committed with the change.
298 changeset is committed with the change.
299
299
300 keyword arguments:
300 keyword arguments:
301
301
302 local: whether to store tags in non-version-controlled file
302 local: whether to store tags in non-version-controlled file
303 (default False)
303 (default False)
304
304
305 message: commit message to use if committing
305 message: commit message to use if committing
306
306
307 user: name of user to use if committing
307 user: name of user to use if committing
308
308
309 date: date tuple to use if committing'''
309 date: date tuple to use if committing'''
310
310
311 if not local:
311 if not local:
312 for x in self.status()[:5]:
312 for x in self.status()[:5]:
313 if '.hgtags' in x:
313 if '.hgtags' in x:
314 raise util.Abort(_('working copy of .hgtags is changed '
314 raise util.Abort(_('working copy of .hgtags is changed '
315 '(please commit .hgtags manually)'))
315 '(please commit .hgtags manually)'))
316
316
317 self.tags() # instantiate the cache
317 self.tags() # instantiate the cache
318 self._tag(names, node, message, local, user, date)
318 self._tag(names, node, message, local, user, date)
319
319
320 def tags(self):
320 def tags(self):
321 '''return a mapping of tag to node'''
321 '''return a mapping of tag to node'''
322 if self._tags is None:
322 if self._tags is None:
323 (self._tags, self._tagtypes) = self._findtags()
323 (self._tags, self._tagtypes) = self._findtags()
324
324
325 return self._tags
325 return self._tags
326
326
327 def _findtags(self):
327 def _findtags(self):
328 '''Do the hard work of finding tags. Return a pair of dicts
328 '''Do the hard work of finding tags. Return a pair of dicts
329 (tags, tagtypes) where tags maps tag name to node, and tagtypes
329 (tags, tagtypes) where tags maps tag name to node, and tagtypes
330 maps tag name to a string like \'global\' or \'local\'.
330 maps tag name to a string like \'global\' or \'local\'.
331 Subclasses or extensions are free to add their own tags, but
331 Subclasses or extensions are free to add their own tags, but
332 should be aware that the returned dicts will be retained for the
332 should be aware that the returned dicts will be retained for the
333 duration of the localrepo object.'''
333 duration of the localrepo object.'''
334
334
335 # XXX what tagtype should subclasses/extensions use? Currently
335 # XXX what tagtype should subclasses/extensions use? Currently
336 # mq and bookmarks add tags, but do not set the tagtype at all.
336 # mq and bookmarks add tags, but do not set the tagtype at all.
337 # Should each extension invent its own tag type? Should there
337 # Should each extension invent its own tag type? Should there
338 # be one tagtype for all such "virtual" tags? Or is the status
338 # be one tagtype for all such "virtual" tags? Or is the status
339 # quo fine?
339 # quo fine?
340
340
341 alltags = {} # map tag name to (node, hist)
341 alltags = {} # map tag name to (node, hist)
342 tagtypes = {}
342 tagtypes = {}
343
343
344 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
344 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
345 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
345 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
346
346
347 # Build the return dicts. Have to re-encode tag names because
347 # Build the return dicts. Have to re-encode tag names because
348 # the tags module always uses UTF-8 (in order not to lose info
348 # the tags module always uses UTF-8 (in order not to lose info
349 # writing to the cache), but the rest of Mercurial wants them in
349 # writing to the cache), but the rest of Mercurial wants them in
350 # local encoding.
350 # local encoding.
351 tags = {}
351 tags = {}
352 for (name, (node, hist)) in alltags.iteritems():
352 for (name, (node, hist)) in alltags.iteritems():
353 if node != nullid:
353 if node != nullid:
354 tags[encoding.tolocal(name)] = node
354 tags[encoding.tolocal(name)] = node
355 tags['tip'] = self.changelog.tip()
355 tags['tip'] = self.changelog.tip()
356 tagtypes = dict([(encoding.tolocal(name), value)
356 tagtypes = dict([(encoding.tolocal(name), value)
357 for (name, value) in tagtypes.iteritems()])
357 for (name, value) in tagtypes.iteritems()])
358 return (tags, tagtypes)
358 return (tags, tagtypes)
359
359
360 def tagtype(self, tagname):
360 def tagtype(self, tagname):
361 '''
361 '''
362 return the type of the given tag. result can be:
362 return the type of the given tag. result can be:
363
363
364 'local' : a local tag
364 'local' : a local tag
365 'global' : a global tag
365 'global' : a global tag
366 None : tag does not exist
366 None : tag does not exist
367 '''
367 '''
368
368
369 self.tags()
369 self.tags()
370
370
371 return self._tagtypes.get(tagname)
371 return self._tagtypes.get(tagname)
372
372
373 def tagslist(self):
373 def tagslist(self):
374 '''return a list of tags ordered by revision'''
374 '''return a list of tags ordered by revision'''
375 l = []
375 l = []
376 for t, n in self.tags().iteritems():
376 for t, n in self.tags().iteritems():
377 try:
377 try:
378 r = self.changelog.rev(n)
378 r = self.changelog.rev(n)
379 except:
379 except:
380 r = -2 # sort to the beginning of the list if unknown
380 r = -2 # sort to the beginning of the list if unknown
381 l.append((r, t, n))
381 l.append((r, t, n))
382 return [(t, n) for r, t, n in sorted(l)]
382 return [(t, n) for r, t, n in sorted(l)]
383
383
384 def nodetags(self, node):
384 def nodetags(self, node):
385 '''return the tags associated with a node'''
385 '''return the tags associated with a node'''
386 if not self.nodetagscache:
386 if not self.nodetagscache:
387 self.nodetagscache = {}
387 self.nodetagscache = {}
388 for t, n in self.tags().iteritems():
388 for t, n in self.tags().iteritems():
389 self.nodetagscache.setdefault(n, []).append(t)
389 self.nodetagscache.setdefault(n, []).append(t)
390 for tags in self.nodetagscache.itervalues():
390 for tags in self.nodetagscache.itervalues():
391 tags.sort()
391 tags.sort()
392 return self.nodetagscache.get(node, [])
392 return self.nodetagscache.get(node, [])
393
393
394 def _branchtags(self, partial, lrev):
394 def _branchtags(self, partial, lrev):
395 # TODO: rename this function?
395 # TODO: rename this function?
396 tiprev = len(self) - 1
396 tiprev = len(self) - 1
397 if lrev != tiprev:
397 if lrev != tiprev:
398 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
398 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
399 self._updatebranchcache(partial, ctxgen)
399 self._updatebranchcache(partial, ctxgen)
400 self._writebranchcache(partial, self.changelog.tip(), tiprev)
400 self._writebranchcache(partial, self.changelog.tip(), tiprev)
401
401
402 return partial
402 return partial
403
403
404 def updatebranchcache(self):
404 def updatebranchcache(self):
405 tip = self.changelog.tip()
405 tip = self.changelog.tip()
406 if self._branchcache is not None and self._branchcachetip == tip:
406 if self._branchcache is not None and self._branchcachetip == tip:
407 return self._branchcache
407 return self._branchcache
408
408
409 oldtip = self._branchcachetip
409 oldtip = self._branchcachetip
410 self._branchcachetip = tip
410 self._branchcachetip = tip
411 if oldtip is None or oldtip not in self.changelog.nodemap:
411 if oldtip is None or oldtip not in self.changelog.nodemap:
412 partial, last, lrev = self._readbranchcache()
412 partial, last, lrev = self._readbranchcache()
413 else:
413 else:
414 lrev = self.changelog.rev(oldtip)
414 lrev = self.changelog.rev(oldtip)
415 partial = self._branchcache
415 partial = self._branchcache
416
416
417 self._branchtags(partial, lrev)
417 self._branchtags(partial, lrev)
418 # this private cache holds all heads (not just tips)
418 # this private cache holds all heads (not just tips)
419 self._branchcache = partial
419 self._branchcache = partial
420
420
421 def branchmap(self):
421 def branchmap(self):
422 '''returns a dictionary {branch: [branchheads]}'''
422 '''returns a dictionary {branch: [branchheads]}'''
423 self.updatebranchcache()
423 self.updatebranchcache()
424 return self._branchcache
424 return self._branchcache
425
425
426 def branchtags(self):
426 def branchtags(self):
427 '''return a dict where branch names map to the tipmost head of
427 '''return a dict where branch names map to the tipmost head of
428 the branch, open heads come before closed'''
428 the branch, open heads come before closed'''
429 bt = {}
429 bt = {}
430 for bn, heads in self.branchmap().iteritems():
430 for bn, heads in self.branchmap().iteritems():
431 tip = heads[-1]
431 tip = heads[-1]
432 for h in reversed(heads):
432 for h in reversed(heads):
433 if 'close' not in self.changelog.read(h)[5]:
433 if 'close' not in self.changelog.read(h)[5]:
434 tip = h
434 tip = h
435 break
435 break
436 bt[bn] = tip
436 bt[bn] = tip
437 return bt
437 return bt
438
438
439 def _readbranchcache(self):
439 def _readbranchcache(self):
440 partial = {}
440 partial = {}
441 try:
441 try:
442 f = self.opener("branchheads.cache")
442 f = self.opener("branchheads.cache")
443 lines = f.read().split('\n')
443 lines = f.read().split('\n')
444 f.close()
444 f.close()
445 except (IOError, OSError):
445 except (IOError, OSError):
446 return {}, nullid, nullrev
446 return {}, nullid, nullrev
447
447
448 try:
448 try:
449 last, lrev = lines.pop(0).split(" ", 1)
449 last, lrev = lines.pop(0).split(" ", 1)
450 last, lrev = bin(last), int(lrev)
450 last, lrev = bin(last), int(lrev)
451 if lrev >= len(self) or self[lrev].node() != last:
451 if lrev >= len(self) or self[lrev].node() != last:
452 # invalidate the cache
452 # invalidate the cache
453 raise ValueError('invalidating branch cache (tip differs)')
453 raise ValueError('invalidating branch cache (tip differs)')
454 for l in lines:
454 for l in lines:
455 if not l:
455 if not l:
456 continue
456 continue
457 node, label = l.split(" ", 1)
457 node, label = l.split(" ", 1)
458 label = encoding.tolocal(label.strip())
458 label = encoding.tolocal(label.strip())
459 partial.setdefault(label, []).append(bin(node))
459 partial.setdefault(label, []).append(bin(node))
460 except KeyboardInterrupt:
460 except KeyboardInterrupt:
461 raise
461 raise
462 except Exception, inst:
462 except Exception, inst:
463 if self.ui.debugflag:
463 if self.ui.debugflag:
464 self.ui.warn(str(inst), '\n')
464 self.ui.warn(str(inst), '\n')
465 partial, last, lrev = {}, nullid, nullrev
465 partial, last, lrev = {}, nullid, nullrev
466 return partial, last, lrev
466 return partial, last, lrev
467
467
468 def _writebranchcache(self, branches, tip, tiprev):
468 def _writebranchcache(self, branches, tip, tiprev):
469 try:
469 try:
470 f = self.opener("branchheads.cache", "w", atomictemp=True)
470 f = self.opener("branchheads.cache", "w", atomictemp=True)
471 f.write("%s %s\n" % (hex(tip), tiprev))
471 f.write("%s %s\n" % (hex(tip), tiprev))
472 for label, nodes in branches.iteritems():
472 for label, nodes in branches.iteritems():
473 for node in nodes:
473 for node in nodes:
474 f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
474 f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
475 f.rename()
475 f.rename()
476 except (IOError, OSError):
476 except (IOError, OSError):
477 pass
477 pass
478
478
479 def _updatebranchcache(self, partial, ctxgen):
479 def _updatebranchcache(self, partial, ctxgen):
480 # collect new branch entries
480 # collect new branch entries
481 newbranches = {}
481 newbranches = {}
482 for c in ctxgen:
482 for c in ctxgen:
483 newbranches.setdefault(c.branch(), []).append(c.node())
483 newbranches.setdefault(c.branch(), []).append(c.node())
484 # if older branchheads are reachable from new ones, they aren't
484 # if older branchheads are reachable from new ones, they aren't
485 # really branchheads. Note checking parents is insufficient:
485 # really branchheads. Note checking parents is insufficient:
486 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
486 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
487 for branch, newnodes in newbranches.iteritems():
487 for branch, newnodes in newbranches.iteritems():
488 bheads = partial.setdefault(branch, [])
488 bheads = partial.setdefault(branch, [])
489 bheads.extend(newnodes)
489 bheads.extend(newnodes)
490 if len(bheads) <= 1:
490 if len(bheads) <= 1:
491 continue
491 continue
492 # starting from tip means fewer passes over reachable
492 # starting from tip means fewer passes over reachable
493 while newnodes:
493 while newnodes:
494 latest = newnodes.pop()
494 latest = newnodes.pop()
495 if latest not in bheads:
495 if latest not in bheads:
496 continue
496 continue
497 minbhrev = self[min([self[bh].rev() for bh in bheads])].node()
497 minbhrev = self[min([self[bh].rev() for bh in bheads])].node()
498 reachable = self.changelog.reachable(latest, minbhrev)
498 reachable = self.changelog.reachable(latest, minbhrev)
499 reachable.remove(latest)
499 reachable.remove(latest)
500 bheads = [b for b in bheads if b not in reachable]
500 bheads = [b for b in bheads if b not in reachable]
501 partial[branch] = bheads
501 partial[branch] = bheads
502
502
503 def lookup(self, key):
503 def lookup(self, key):
504 if isinstance(key, int):
504 if isinstance(key, int):
505 return self.changelog.node(key)
505 return self.changelog.node(key)
506 elif key == '.':
506 elif key == '.':
507 return self.dirstate.parents()[0]
507 return self.dirstate.parents()[0]
508 elif key == 'null':
508 elif key == 'null':
509 return nullid
509 return nullid
510 elif key == 'tip':
510 elif key == 'tip':
511 return self.changelog.tip()
511 return self.changelog.tip()
512 n = self.changelog._match(key)
512 n = self.changelog._match(key)
513 if n:
513 if n:
514 return n
514 return n
515 if key in self.tags():
515 if key in self.tags():
516 return self.tags()[key]
516 return self.tags()[key]
517 if key in self.branchtags():
517 if key in self.branchtags():
518 return self.branchtags()[key]
518 return self.branchtags()[key]
519 n = self.changelog._partialmatch(key)
519 n = self.changelog._partialmatch(key)
520 if n:
520 if n:
521 return n
521 return n
522
522
523 # can't find key, check if it might have come from damaged dirstate
523 # can't find key, check if it might have come from damaged dirstate
524 if key in self.dirstate.parents():
524 if key in self.dirstate.parents():
525 raise error.Abort(_("working directory has unknown parent '%s'!")
525 raise error.Abort(_("working directory has unknown parent '%s'!")
526 % short(key))
526 % short(key))
527 try:
527 try:
528 if len(key) == 20:
528 if len(key) == 20:
529 key = hex(key)
529 key = hex(key)
530 except:
530 except:
531 pass
531 pass
532 raise error.RepoLookupError(_("unknown revision '%s'") % key)
532 raise error.RepoLookupError(_("unknown revision '%s'") % key)
533
533
534 def lookupbranch(self, key, remote=None):
534 def lookupbranch(self, key, remote=None):
535 repo = remote or self
535 repo = remote or self
536 if key in repo.branchmap():
536 if key in repo.branchmap():
537 return key
537 return key
538
538
539 repo = (remote and remote.local()) and remote or self
539 repo = (remote and remote.local()) and remote or self
540 return repo[key].branch()
540 return repo[key].branch()
541
541
542 def local(self):
542 def local(self):
543 return True
543 return True
544
544
545 def join(self, f):
545 def join(self, f):
546 return os.path.join(self.path, f)
546 return os.path.join(self.path, f)
547
547
548 def wjoin(self, f):
548 def wjoin(self, f):
549 return os.path.join(self.root, f)
549 return os.path.join(self.root, f)
550
550
551 def file(self, f):
551 def file(self, f):
552 if f[0] == '/':
552 if f[0] == '/':
553 f = f[1:]
553 f = f[1:]
554 return filelog.filelog(self.sopener, f)
554 return filelog.filelog(self.sopener, f)
555
555
556 def changectx(self, changeid):
556 def changectx(self, changeid):
557 return self[changeid]
557 return self[changeid]
558
558
559 def parents(self, changeid=None):
559 def parents(self, changeid=None):
560 '''get list of changectxs for parents of changeid'''
560 '''get list of changectxs for parents of changeid'''
561 return self[changeid].parents()
561 return self[changeid].parents()
562
562
563 def filectx(self, path, changeid=None, fileid=None):
563 def filectx(self, path, changeid=None, fileid=None):
564 """changeid can be a changeset revision, node, or tag.
564 """changeid can be a changeset revision, node, or tag.
565 fileid can be a file revision or node."""
565 fileid can be a file revision or node."""
566 return context.filectx(self, path, changeid, fileid)
566 return context.filectx(self, path, changeid, fileid)
567
567
568 def getcwd(self):
568 def getcwd(self):
569 return self.dirstate.getcwd()
569 return self.dirstate.getcwd()
570
570
571 def pathto(self, f, cwd=None):
571 def pathto(self, f, cwd=None):
572 return self.dirstate.pathto(f, cwd)
572 return self.dirstate.pathto(f, cwd)
573
573
574 def wfile(self, f, mode='r'):
574 def wfile(self, f, mode='r'):
575 return self.wopener(f, mode)
575 return self.wopener(f, mode)
576
576
577 def _link(self, f):
577 def _link(self, f):
578 return os.path.islink(self.wjoin(f))
578 return os.path.islink(self.wjoin(f))
579
579
580 def _loadfilter(self, filter):
580 def _loadfilter(self, filter):
581 if filter not in self.filterpats:
581 if filter not in self.filterpats:
582 l = []
582 l = []
583 for pat, cmd in self.ui.configitems(filter):
583 for pat, cmd in self.ui.configitems(filter):
584 if cmd == '!':
584 if cmd == '!':
585 continue
585 continue
586 mf = matchmod.match(self.root, '', [pat])
586 mf = matchmod.match(self.root, '', [pat])
587 fn = None
587 fn = None
588 params = cmd
588 params = cmd
589 for name, filterfn in self._datafilters.iteritems():
589 for name, filterfn in self._datafilters.iteritems():
590 if cmd.startswith(name):
590 if cmd.startswith(name):
591 fn = filterfn
591 fn = filterfn
592 params = cmd[len(name):].lstrip()
592 params = cmd[len(name):].lstrip()
593 break
593 break
594 if not fn:
594 if not fn:
595 fn = lambda s, c, **kwargs: util.filter(s, c)
595 fn = lambda s, c, **kwargs: util.filter(s, c)
596 # Wrap old filters not supporting keyword arguments
596 # Wrap old filters not supporting keyword arguments
597 if not inspect.getargspec(fn)[2]:
597 if not inspect.getargspec(fn)[2]:
598 oldfn = fn
598 oldfn = fn
599 fn = lambda s, c, **kwargs: oldfn(s, c)
599 fn = lambda s, c, **kwargs: oldfn(s, c)
600 l.append((mf, fn, params))
600 l.append((mf, fn, params))
601 self.filterpats[filter] = l
601 self.filterpats[filter] = l
602 return self.filterpats[filter]
602 return self.filterpats[filter]
603
603
604 def _filter(self, filterpats, filename, data):
604 def _filter(self, filterpats, filename, data):
605 for mf, fn, cmd in filterpats:
605 for mf, fn, cmd in filterpats:
606 if mf(filename):
606 if mf(filename):
607 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
607 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
608 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
608 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
609 break
609 break
610
610
611 return data
611 return data
612
612
613 @propertycache
613 @propertycache
614 def _encodefilterpats(self):
614 def _encodefilterpats(self):
615 return self._loadfilter('encode')
615 return self._loadfilter('encode')
616
616
617 @propertycache
617 @propertycache
618 def _decodefilterpats(self):
618 def _decodefilterpats(self):
619 return self._loadfilter('decode')
619 return self._loadfilter('decode')
620
620
621 def adddatafilter(self, name, filter):
621 def adddatafilter(self, name, filter):
622 self._datafilters[name] = filter
622 self._datafilters[name] = filter
623
623
624 def wread(self, filename):
624 def wread(self, filename):
625 if self._link(filename):
625 if self._link(filename):
626 data = os.readlink(self.wjoin(filename))
626 data = os.readlink(self.wjoin(filename))
627 else:
627 else:
628 data = self.wopener(filename, 'r').read()
628 data = self.wopener(filename, 'r').read()
629 return self._filter(self._encodefilterpats, filename, data)
629 return self._filter(self._encodefilterpats, filename, data)
630
630
631 def wwrite(self, filename, data, flags):
631 def wwrite(self, filename, data, flags):
632 data = self._filter(self._decodefilterpats, filename, data)
632 data = self._filter(self._decodefilterpats, filename, data)
633 if 'l' in flags:
633 if 'l' in flags:
634 self.wopener.symlink(data, filename)
634 self.wopener.symlink(data, filename)
635 else:
635 else:
636 self.wopener(filename, 'w').write(data)
636 self.wopener(filename, 'w').write(data)
637 if 'x' in flags:
637 if 'x' in flags:
638 util.set_flags(self.wjoin(filename), False, True)
638 util.set_flags(self.wjoin(filename), False, True)
639
639
640 def wwritedata(self, filename, data):
640 def wwritedata(self, filename, data):
641 return self._filter(self._decodefilterpats, filename, data)
641 return self._filter(self._decodefilterpats, filename, data)
642
642
643 def transaction(self, desc):
643 def transaction(self, desc):
644 tr = self._transref and self._transref() or None
644 tr = self._transref and self._transref() or None
645 if tr and tr.running():
645 if tr and tr.running():
646 return tr.nest()
646 return tr.nest()
647
647
648 # abort here if the journal already exists
648 # abort here if the journal already exists
649 if os.path.exists(self.sjoin("journal")):
649 if os.path.exists(self.sjoin("journal")):
650 raise error.RepoError(
650 raise error.RepoError(
651 _("abandoned transaction found - run hg recover"))
651 _("abandoned transaction found - run hg recover"))
652
652
653 # save dirstate for rollback
653 # save dirstate for rollback
654 try:
654 try:
655 ds = self.opener("dirstate").read()
655 ds = self.opener("dirstate").read()
656 except IOError:
656 except IOError:
657 ds = ""
657 ds = ""
658 self.opener("journal.dirstate", "w").write(ds)
658 self.opener("journal.dirstate", "w").write(ds)
659 self.opener("journal.branch", "w").write(
659 self.opener("journal.branch", "w").write(
660 encoding.fromlocal(self.dirstate.branch()))
660 encoding.fromlocal(self.dirstate.branch()))
661 self.opener("journal.desc", "w").write("%d\n%s\n" % (len(self), desc))
661 self.opener("journal.desc", "w").write("%d\n%s\n" % (len(self), desc))
662
662
663 renames = [(self.sjoin("journal"), self.sjoin("undo")),
663 renames = [(self.sjoin("journal"), self.sjoin("undo")),
664 (self.join("journal.dirstate"), self.join("undo.dirstate")),
664 (self.join("journal.dirstate"), self.join("undo.dirstate")),
665 (self.join("journal.branch"), self.join("undo.branch")),
665 (self.join("journal.branch"), self.join("undo.branch")),
666 (self.join("journal.desc"), self.join("undo.desc"))]
666 (self.join("journal.desc"), self.join("undo.desc"))]
667 tr = transaction.transaction(self.ui.warn, self.sopener,
667 tr = transaction.transaction(self.ui.warn, self.sopener,
668 self.sjoin("journal"),
668 self.sjoin("journal"),
669 aftertrans(renames),
669 aftertrans(renames),
670 self.store.createmode)
670 self.store.createmode)
671 self._transref = weakref.ref(tr)
671 self._transref = weakref.ref(tr)
672 return tr
672 return tr
673
673
674 def recover(self):
674 def recover(self):
675 lock = self.lock()
675 lock = self.lock()
676 try:
676 try:
677 if os.path.exists(self.sjoin("journal")):
677 if os.path.exists(self.sjoin("journal")):
678 self.ui.status(_("rolling back interrupted transaction\n"))
678 self.ui.status(_("rolling back interrupted transaction\n"))
679 transaction.rollback(self.sopener, self.sjoin("journal"),
679 transaction.rollback(self.sopener, self.sjoin("journal"),
680 self.ui.warn)
680 self.ui.warn)
681 self.invalidate()
681 self.invalidate()
682 return True
682 return True
683 else:
683 else:
684 self.ui.warn(_("no interrupted transaction available\n"))
684 self.ui.warn(_("no interrupted transaction available\n"))
685 return False
685 return False
686 finally:
686 finally:
687 lock.release()
687 lock.release()
688
688
689 def rollback(self, dryrun=False):
689 def rollback(self, dryrun=False):
690 wlock = lock = None
690 wlock = lock = None
691 try:
691 try:
692 wlock = self.wlock()
692 wlock = self.wlock()
693 lock = self.lock()
693 lock = self.lock()
694 if os.path.exists(self.sjoin("undo")):
694 if os.path.exists(self.sjoin("undo")):
695 try:
695 try:
696 args = self.opener("undo.desc", "r").read().splitlines()
696 args = self.opener("undo.desc", "r").read().splitlines()
697 if len(args) >= 3 and self.ui.verbose:
697 if len(args) >= 3 and self.ui.verbose:
698 desc = _("rolling back to revision %s"
698 desc = _("rolling back to revision %s"
699 " (undo %s: %s)\n") % (
699 " (undo %s: %s)\n") % (
700 int(args[0]) - 1, args[1], args[2])
700 int(args[0]) - 1, args[1], args[2])
701 elif len(args) >= 2:
701 elif len(args) >= 2:
702 desc = _("rolling back to revision %s (undo %s)\n") % (
702 desc = _("rolling back to revision %s (undo %s)\n") % (
703 int(args[0]) - 1, args[1])
703 int(args[0]) - 1, args[1])
704 except IOError:
704 except IOError:
705 desc = _("rolling back unknown transaction\n")
705 desc = _("rolling back unknown transaction\n")
706 self.ui.status(desc)
706 self.ui.status(desc)
707 if dryrun:
707 if dryrun:
708 return
708 return
709 transaction.rollback(self.sopener, self.sjoin("undo"),
709 transaction.rollback(self.sopener, self.sjoin("undo"),
710 self.ui.warn)
710 self.ui.warn)
711 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
711 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
712 try:
712 try:
713 branch = self.opener("undo.branch").read()
713 branch = self.opener("undo.branch").read()
714 self.dirstate.setbranch(branch)
714 self.dirstate.setbranch(branch)
715 except IOError:
715 except IOError:
716 self.ui.warn(_("Named branch could not be reset, "
716 self.ui.warn(_("Named branch could not be reset, "
717 "current branch still is: %s\n")
717 "current branch still is: %s\n")
718 % self.dirstate.branch())
718 % self.dirstate.branch())
719 self.invalidate()
719 self.invalidate()
720 self.dirstate.invalidate()
720 self.dirstate.invalidate()
721 self.destroyed()
721 self.destroyed()
722 else:
722 else:
723 self.ui.warn(_("no rollback information available\n"))
723 self.ui.warn(_("no rollback information available\n"))
724 return 1
724 return 1
725 finally:
725 finally:
726 release(lock, wlock)
726 release(lock, wlock)
727
727
728 def invalidatecaches(self):
728 def invalidatecaches(self):
729 self._tags = None
729 self._tags = None
730 self._tagtypes = None
730 self._tagtypes = None
731 self.nodetagscache = None
731 self.nodetagscache = None
732 self._branchcache = None # in UTF-8
732 self._branchcache = None # in UTF-8
733 self._branchcachetip = None
733 self._branchcachetip = None
734
734
735 def invalidate(self):
735 def invalidate(self):
736 for a in "changelog manifest".split():
736 for a in ("changelog", "manifest"):
737 if a in self.__dict__:
737 if a in self.__dict__:
738 delattr(self, a)
738 delattr(self, a)
739 self.invalidatecaches()
739 self.invalidatecaches()
740
740
741 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
741 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
742 try:
742 try:
743 l = lock.lock(lockname, 0, releasefn, desc=desc)
743 l = lock.lock(lockname, 0, releasefn, desc=desc)
744 except error.LockHeld, inst:
744 except error.LockHeld, inst:
745 if not wait:
745 if not wait:
746 raise
746 raise
747 self.ui.warn(_("waiting for lock on %s held by %r\n") %
747 self.ui.warn(_("waiting for lock on %s held by %r\n") %
748 (desc, inst.locker))
748 (desc, inst.locker))
749 # default to 600 seconds timeout
749 # default to 600 seconds timeout
750 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
750 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
751 releasefn, desc=desc)
751 releasefn, desc=desc)
752 if acquirefn:
752 if acquirefn:
753 acquirefn()
753 acquirefn()
754 return l
754 return l
755
755
756 def lock(self, wait=True):
756 def lock(self, wait=True):
757 '''Lock the repository store (.hg/store) and return a weak reference
757 '''Lock the repository store (.hg/store) and return a weak reference
758 to the lock. Use this before modifying the store (e.g. committing or
758 to the lock. Use this before modifying the store (e.g. committing or
759 stripping). If you are opening a transaction, get a lock as well.)'''
759 stripping). If you are opening a transaction, get a lock as well.)'''
760 l = self._lockref and self._lockref()
760 l = self._lockref and self._lockref()
761 if l is not None and l.held:
761 if l is not None and l.held:
762 l.lock()
762 l.lock()
763 return l
763 return l
764
764
765 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
765 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
766 _('repository %s') % self.origroot)
766 _('repository %s') % self.origroot)
767 self._lockref = weakref.ref(l)
767 self._lockref = weakref.ref(l)
768 return l
768 return l
769
769
770 def wlock(self, wait=True):
770 def wlock(self, wait=True):
771 '''Lock the non-store parts of the repository (everything under
771 '''Lock the non-store parts of the repository (everything under
772 .hg except .hg/store) and return a weak reference to the lock.
772 .hg except .hg/store) and return a weak reference to the lock.
773 Use this before modifying files in .hg.'''
773 Use this before modifying files in .hg.'''
774 l = self._wlockref and self._wlockref()
774 l = self._wlockref and self._wlockref()
775 if l is not None and l.held:
775 if l is not None and l.held:
776 l.lock()
776 l.lock()
777 return l
777 return l
778
778
779 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
779 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
780 self.dirstate.invalidate, _('working directory of %s') %
780 self.dirstate.invalidate, _('working directory of %s') %
781 self.origroot)
781 self.origroot)
782 self._wlockref = weakref.ref(l)
782 self._wlockref = weakref.ref(l)
783 return l
783 return l
784
784
785 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
785 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
786 """
786 """
787 commit an individual file as part of a larger transaction
787 commit an individual file as part of a larger transaction
788 """
788 """
789
789
790 fname = fctx.path()
790 fname = fctx.path()
791 text = fctx.data()
791 text = fctx.data()
792 flog = self.file(fname)
792 flog = self.file(fname)
793 fparent1 = manifest1.get(fname, nullid)
793 fparent1 = manifest1.get(fname, nullid)
794 fparent2 = fparent2o = manifest2.get(fname, nullid)
794 fparent2 = fparent2o = manifest2.get(fname, nullid)
795
795
796 meta = {}
796 meta = {}
797 copy = fctx.renamed()
797 copy = fctx.renamed()
798 if copy and copy[0] != fname:
798 if copy and copy[0] != fname:
799 # Mark the new revision of this file as a copy of another
799 # Mark the new revision of this file as a copy of another
800 # file. This copy data will effectively act as a parent
800 # file. This copy data will effectively act as a parent
801 # of this new revision. If this is a merge, the first
801 # of this new revision. If this is a merge, the first
802 # parent will be the nullid (meaning "look up the copy data")
802 # parent will be the nullid (meaning "look up the copy data")
803 # and the second one will be the other parent. For example:
803 # and the second one will be the other parent. For example:
804 #
804 #
805 # 0 --- 1 --- 3 rev1 changes file foo
805 # 0 --- 1 --- 3 rev1 changes file foo
806 # \ / rev2 renames foo to bar and changes it
806 # \ / rev2 renames foo to bar and changes it
807 # \- 2 -/ rev3 should have bar with all changes and
807 # \- 2 -/ rev3 should have bar with all changes and
808 # should record that bar descends from
808 # should record that bar descends from
809 # bar in rev2 and foo in rev1
809 # bar in rev2 and foo in rev1
810 #
810 #
811 # this allows this merge to succeed:
811 # this allows this merge to succeed:
812 #
812 #
813 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
813 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
814 # \ / merging rev3 and rev4 should use bar@rev2
814 # \ / merging rev3 and rev4 should use bar@rev2
815 # \- 2 --- 4 as the merge base
815 # \- 2 --- 4 as the merge base
816 #
816 #
817
817
818 cfname = copy[0]
818 cfname = copy[0]
819 crev = manifest1.get(cfname)
819 crev = manifest1.get(cfname)
820 newfparent = fparent2
820 newfparent = fparent2
821
821
822 if manifest2: # branch merge
822 if manifest2: # branch merge
823 if fparent2 == nullid or crev is None: # copied on remote side
823 if fparent2 == nullid or crev is None: # copied on remote side
824 if cfname in manifest2:
824 if cfname in manifest2:
825 crev = manifest2[cfname]
825 crev = manifest2[cfname]
826 newfparent = fparent1
826 newfparent = fparent1
827
827
828 # find source in nearest ancestor if we've lost track
828 # find source in nearest ancestor if we've lost track
829 if not crev:
829 if not crev:
830 self.ui.debug(" %s: searching for copy revision for %s\n" %
830 self.ui.debug(" %s: searching for copy revision for %s\n" %
831 (fname, cfname))
831 (fname, cfname))
832 for ancestor in self[None].ancestors():
832 for ancestor in self[None].ancestors():
833 if cfname in ancestor:
833 if cfname in ancestor:
834 crev = ancestor[cfname].filenode()
834 crev = ancestor[cfname].filenode()
835 break
835 break
836
836
837 if crev:
837 if crev:
838 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
838 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
839 meta["copy"] = cfname
839 meta["copy"] = cfname
840 meta["copyrev"] = hex(crev)
840 meta["copyrev"] = hex(crev)
841 fparent1, fparent2 = nullid, newfparent
841 fparent1, fparent2 = nullid, newfparent
842 else:
842 else:
843 self.ui.warn(_("warning: can't find ancestor for '%s' "
843 self.ui.warn(_("warning: can't find ancestor for '%s' "
844 "copied from '%s'!\n") % (fname, cfname))
844 "copied from '%s'!\n") % (fname, cfname))
845
845
846 elif fparent2 != nullid:
846 elif fparent2 != nullid:
847 # is one parent an ancestor of the other?
847 # is one parent an ancestor of the other?
848 fparentancestor = flog.ancestor(fparent1, fparent2)
848 fparentancestor = flog.ancestor(fparent1, fparent2)
849 if fparentancestor == fparent1:
849 if fparentancestor == fparent1:
850 fparent1, fparent2 = fparent2, nullid
850 fparent1, fparent2 = fparent2, nullid
851 elif fparentancestor == fparent2:
851 elif fparentancestor == fparent2:
852 fparent2 = nullid
852 fparent2 = nullid
853
853
854 # is the file changed?
854 # is the file changed?
855 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
855 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
856 changelist.append(fname)
856 changelist.append(fname)
857 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
857 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
858
858
859 # are just the flags changed during merge?
859 # are just the flags changed during merge?
860 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
860 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
861 changelist.append(fname)
861 changelist.append(fname)
862
862
863 return fparent1
863 return fparent1
864
864
865 def commit(self, text="", user=None, date=None, match=None, force=False,
865 def commit(self, text="", user=None, date=None, match=None, force=False,
866 editor=False, extra={}):
866 editor=False, extra={}):
867 """Add a new revision to current repository.
867 """Add a new revision to current repository.
868
868
869 Revision information is gathered from the working directory,
869 Revision information is gathered from the working directory,
870 match can be used to filter the committed files. If editor is
870 match can be used to filter the committed files. If editor is
871 supplied, it is called to get a commit message.
871 supplied, it is called to get a commit message.
872 """
872 """
873
873
874 def fail(f, msg):
874 def fail(f, msg):
875 raise util.Abort('%s: %s' % (f, msg))
875 raise util.Abort('%s: %s' % (f, msg))
876
876
877 if not match:
877 if not match:
878 match = matchmod.always(self.root, '')
878 match = matchmod.always(self.root, '')
879
879
880 if not force:
880 if not force:
881 vdirs = []
881 vdirs = []
882 match.dir = vdirs.append
882 match.dir = vdirs.append
883 match.bad = fail
883 match.bad = fail
884
884
885 wlock = self.wlock()
885 wlock = self.wlock()
886 try:
886 try:
887 wctx = self[None]
887 wctx = self[None]
888 merge = len(wctx.parents()) > 1
888 merge = len(wctx.parents()) > 1
889
889
890 if (not force and merge and match and
890 if (not force and merge and match and
891 (match.files() or match.anypats())):
891 (match.files() or match.anypats())):
892 raise util.Abort(_('cannot partially commit a merge '
892 raise util.Abort(_('cannot partially commit a merge '
893 '(do not specify files or patterns)'))
893 '(do not specify files or patterns)'))
894
894
895 changes = self.status(match=match, clean=force)
895 changes = self.status(match=match, clean=force)
896 if force:
896 if force:
897 changes[0].extend(changes[6]) # mq may commit unchanged files
897 changes[0].extend(changes[6]) # mq may commit unchanged files
898
898
899 # check subrepos
899 # check subrepos
900 subs = []
900 subs = []
901 removedsubs = set()
901 removedsubs = set()
902 for p in wctx.parents():
902 for p in wctx.parents():
903 removedsubs.update(s for s in p.substate if match(s))
903 removedsubs.update(s for s in p.substate if match(s))
904 for s in wctx.substate:
904 for s in wctx.substate:
905 removedsubs.discard(s)
905 removedsubs.discard(s)
906 if match(s) and wctx.sub(s).dirty():
906 if match(s) and wctx.sub(s).dirty():
907 subs.append(s)
907 subs.append(s)
908 if (subs or removedsubs):
908 if (subs or removedsubs):
909 if (not match('.hgsub') and
909 if (not match('.hgsub') and
910 '.hgsub' in (wctx.modified() + wctx.added())):
910 '.hgsub' in (wctx.modified() + wctx.added())):
911 raise util.Abort(_("can't commit subrepos without .hgsub"))
911 raise util.Abort(_("can't commit subrepos without .hgsub"))
912 if '.hgsubstate' not in changes[0]:
912 if '.hgsubstate' not in changes[0]:
913 changes[0].insert(0, '.hgsubstate')
913 changes[0].insert(0, '.hgsubstate')
914
914
915 # make sure all explicit patterns are matched
915 # make sure all explicit patterns are matched
916 if not force and match.files():
916 if not force and match.files():
917 matched = set(changes[0] + changes[1] + changes[2])
917 matched = set(changes[0] + changes[1] + changes[2])
918
918
919 for f in match.files():
919 for f in match.files():
920 if f == '.' or f in matched or f in wctx.substate:
920 if f == '.' or f in matched or f in wctx.substate:
921 continue
921 continue
922 if f in changes[3]: # missing
922 if f in changes[3]: # missing
923 fail(f, _('file not found!'))
923 fail(f, _('file not found!'))
924 if f in vdirs: # visited directory
924 if f in vdirs: # visited directory
925 d = f + '/'
925 d = f + '/'
926 for mf in matched:
926 for mf in matched:
927 if mf.startswith(d):
927 if mf.startswith(d):
928 break
928 break
929 else:
929 else:
930 fail(f, _("no match under directory!"))
930 fail(f, _("no match under directory!"))
931 elif f not in self.dirstate:
931 elif f not in self.dirstate:
932 fail(f, _("file not tracked!"))
932 fail(f, _("file not tracked!"))
933
933
934 if (not force and not extra.get("close") and not merge
934 if (not force and not extra.get("close") and not merge
935 and not (changes[0] or changes[1] or changes[2])
935 and not (changes[0] or changes[1] or changes[2])
936 and wctx.branch() == wctx.p1().branch()):
936 and wctx.branch() == wctx.p1().branch()):
937 return None
937 return None
938
938
939 ms = mergemod.mergestate(self)
939 ms = mergemod.mergestate(self)
940 for f in changes[0]:
940 for f in changes[0]:
941 if f in ms and ms[f] == 'u':
941 if f in ms and ms[f] == 'u':
942 raise util.Abort(_("unresolved merge conflicts "
942 raise util.Abort(_("unresolved merge conflicts "
943 "(see hg resolve)"))
943 "(see hg resolve)"))
944
944
945 cctx = context.workingctx(self, text, user, date, extra, changes)
945 cctx = context.workingctx(self, text, user, date, extra, changes)
946 if editor:
946 if editor:
947 cctx._text = editor(self, cctx, subs)
947 cctx._text = editor(self, cctx, subs)
948 edited = (text != cctx._text)
948 edited = (text != cctx._text)
949
949
950 # commit subs
950 # commit subs
951 if subs or removedsubs:
951 if subs or removedsubs:
952 state = wctx.substate.copy()
952 state = wctx.substate.copy()
953 for s in sorted(subs):
953 for s in sorted(subs):
954 sub = wctx.sub(s)
954 sub = wctx.sub(s)
955 self.ui.status(_('committing subrepository %s\n') %
955 self.ui.status(_('committing subrepository %s\n') %
956 subrepo.subrelpath(sub))
956 subrepo.subrelpath(sub))
957 sr = sub.commit(cctx._text, user, date)
957 sr = sub.commit(cctx._text, user, date)
958 state[s] = (state[s][0], sr)
958 state[s] = (state[s][0], sr)
959 subrepo.writestate(self, state)
959 subrepo.writestate(self, state)
960
960
961 # Save commit message in case this transaction gets rolled back
961 # Save commit message in case this transaction gets rolled back
962 # (e.g. by a pretxncommit hook). Leave the content alone on
962 # (e.g. by a pretxncommit hook). Leave the content alone on
963 # the assumption that the user will use the same editor again.
963 # the assumption that the user will use the same editor again.
964 msgfile = self.opener('last-message.txt', 'wb')
964 msgfile = self.opener('last-message.txt', 'wb')
965 msgfile.write(cctx._text)
965 msgfile.write(cctx._text)
966 msgfile.close()
966 msgfile.close()
967
967
968 p1, p2 = self.dirstate.parents()
968 p1, p2 = self.dirstate.parents()
969 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
969 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
970 try:
970 try:
971 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
971 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
972 ret = self.commitctx(cctx, True)
972 ret = self.commitctx(cctx, True)
973 except:
973 except:
974 if edited:
974 if edited:
975 msgfn = self.pathto(msgfile.name[len(self.root)+1:])
975 msgfn = self.pathto(msgfile.name[len(self.root)+1:])
976 self.ui.write(
976 self.ui.write(
977 _('note: commit message saved in %s\n') % msgfn)
977 _('note: commit message saved in %s\n') % msgfn)
978 raise
978 raise
979
979
980 # update dirstate and mergestate
980 # update dirstate and mergestate
981 for f in changes[0] + changes[1]:
981 for f in changes[0] + changes[1]:
982 self.dirstate.normal(f)
982 self.dirstate.normal(f)
983 for f in changes[2]:
983 for f in changes[2]:
984 self.dirstate.forget(f)
984 self.dirstate.forget(f)
985 self.dirstate.setparents(ret)
985 self.dirstate.setparents(ret)
986 ms.reset()
986 ms.reset()
987 finally:
987 finally:
988 wlock.release()
988 wlock.release()
989
989
990 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
990 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
991 return ret
991 return ret
992
992
993 def commitctx(self, ctx, error=False):
993 def commitctx(self, ctx, error=False):
994 """Add a new revision to current repository.
994 """Add a new revision to current repository.
995 Revision information is passed via the context argument.
995 Revision information is passed via the context argument.
996 """
996 """
997
997
998 tr = lock = None
998 tr = lock = None
999 removed = list(ctx.removed())
999 removed = list(ctx.removed())
1000 p1, p2 = ctx.p1(), ctx.p2()
1000 p1, p2 = ctx.p1(), ctx.p2()
1001 m1 = p1.manifest().copy()
1001 m1 = p1.manifest().copy()
1002 m2 = p2.manifest()
1002 m2 = p2.manifest()
1003 user = ctx.user()
1003 user = ctx.user()
1004
1004
1005 lock = self.lock()
1005 lock = self.lock()
1006 try:
1006 try:
1007 tr = self.transaction("commit")
1007 tr = self.transaction("commit")
1008 trp = weakref.proxy(tr)
1008 trp = weakref.proxy(tr)
1009
1009
1010 # check in files
1010 # check in files
1011 new = {}
1011 new = {}
1012 changed = []
1012 changed = []
1013 linkrev = len(self)
1013 linkrev = len(self)
1014 for f in sorted(ctx.modified() + ctx.added()):
1014 for f in sorted(ctx.modified() + ctx.added()):
1015 self.ui.note(f + "\n")
1015 self.ui.note(f + "\n")
1016 try:
1016 try:
1017 fctx = ctx[f]
1017 fctx = ctx[f]
1018 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1018 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1019 changed)
1019 changed)
1020 m1.set(f, fctx.flags())
1020 m1.set(f, fctx.flags())
1021 except OSError, inst:
1021 except OSError, inst:
1022 self.ui.warn(_("trouble committing %s!\n") % f)
1022 self.ui.warn(_("trouble committing %s!\n") % f)
1023 raise
1023 raise
1024 except IOError, inst:
1024 except IOError, inst:
1025 errcode = getattr(inst, 'errno', errno.ENOENT)
1025 errcode = getattr(inst, 'errno', errno.ENOENT)
1026 if error or errcode and errcode != errno.ENOENT:
1026 if error or errcode and errcode != errno.ENOENT:
1027 self.ui.warn(_("trouble committing %s!\n") % f)
1027 self.ui.warn(_("trouble committing %s!\n") % f)
1028 raise
1028 raise
1029 else:
1029 else:
1030 removed.append(f)
1030 removed.append(f)
1031
1031
1032 # update manifest
1032 # update manifest
1033 m1.update(new)
1033 m1.update(new)
1034 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1034 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1035 drop = [f for f in removed if f in m1]
1035 drop = [f for f in removed if f in m1]
1036 for f in drop:
1036 for f in drop:
1037 del m1[f]
1037 del m1[f]
1038 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1038 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1039 p2.manifestnode(), (new, drop))
1039 p2.manifestnode(), (new, drop))
1040
1040
1041 # update changelog
1041 # update changelog
1042 self.changelog.delayupdate()
1042 self.changelog.delayupdate()
1043 n = self.changelog.add(mn, changed + removed, ctx.description(),
1043 n = self.changelog.add(mn, changed + removed, ctx.description(),
1044 trp, p1.node(), p2.node(),
1044 trp, p1.node(), p2.node(),
1045 user, ctx.date(), ctx.extra().copy())
1045 user, ctx.date(), ctx.extra().copy())
1046 p = lambda: self.changelog.writepending() and self.root or ""
1046 p = lambda: self.changelog.writepending() and self.root or ""
1047 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1047 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1048 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1048 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1049 parent2=xp2, pending=p)
1049 parent2=xp2, pending=p)
1050 self.changelog.finalize(trp)
1050 self.changelog.finalize(trp)
1051 tr.close()
1051 tr.close()
1052
1052
1053 if self._branchcache:
1053 if self._branchcache:
1054 self.updatebranchcache()
1054 self.updatebranchcache()
1055 return n
1055 return n
1056 finally:
1056 finally:
1057 if tr:
1057 if tr:
1058 tr.release()
1058 tr.release()
1059 lock.release()
1059 lock.release()
1060
1060
1061 def destroyed(self):
1061 def destroyed(self):
1062 '''Inform the repository that nodes have been destroyed.
1062 '''Inform the repository that nodes have been destroyed.
1063 Intended for use by strip and rollback, so there's a common
1063 Intended for use by strip and rollback, so there's a common
1064 place for anything that has to be done after destroying history.'''
1064 place for anything that has to be done after destroying history.'''
1065 # XXX it might be nice if we could take the list of destroyed
1065 # XXX it might be nice if we could take the list of destroyed
1066 # nodes, but I don't see an easy way for rollback() to do that
1066 # nodes, but I don't see an easy way for rollback() to do that
1067
1067
1068 # Ensure the persistent tag cache is updated. Doing it now
1068 # Ensure the persistent tag cache is updated. Doing it now
1069 # means that the tag cache only has to worry about destroyed
1069 # means that the tag cache only has to worry about destroyed
1070 # heads immediately after a strip/rollback. That in turn
1070 # heads immediately after a strip/rollback. That in turn
1071 # guarantees that "cachetip == currenttip" (comparing both rev
1071 # guarantees that "cachetip == currenttip" (comparing both rev
1072 # and node) always means no nodes have been added or destroyed.
1072 # and node) always means no nodes have been added or destroyed.
1073
1073
1074 # XXX this is suboptimal when qrefresh'ing: we strip the current
1074 # XXX this is suboptimal when qrefresh'ing: we strip the current
1075 # head, refresh the tag cache, then immediately add a new head.
1075 # head, refresh the tag cache, then immediately add a new head.
1076 # But I think doing it this way is necessary for the "instant
1076 # But I think doing it this way is necessary for the "instant
1077 # tag cache retrieval" case to work.
1077 # tag cache retrieval" case to work.
1078 self.invalidatecaches()
1078 self.invalidatecaches()
1079
1079
1080 def walk(self, match, node=None):
1080 def walk(self, match, node=None):
1081 '''
1081 '''
1082 walk recursively through the directory tree or a given
1082 walk recursively through the directory tree or a given
1083 changeset, finding all files matched by the match
1083 changeset, finding all files matched by the match
1084 function
1084 function
1085 '''
1085 '''
1086 return self[node].walk(match)
1086 return self[node].walk(match)
1087
1087
1088 def status(self, node1='.', node2=None, match=None,
1088 def status(self, node1='.', node2=None, match=None,
1089 ignored=False, clean=False, unknown=False,
1089 ignored=False, clean=False, unknown=False,
1090 listsubrepos=False):
1090 listsubrepos=False):
1091 """return status of files between two nodes or node and working directory
1091 """return status of files between two nodes or node and working directory
1092
1092
1093 If node1 is None, use the first dirstate parent instead.
1093 If node1 is None, use the first dirstate parent instead.
1094 If node2 is None, compare node1 with working directory.
1094 If node2 is None, compare node1 with working directory.
1095 """
1095 """
1096
1096
1097 def mfmatches(ctx):
1097 def mfmatches(ctx):
1098 mf = ctx.manifest().copy()
1098 mf = ctx.manifest().copy()
1099 for fn in mf.keys():
1099 for fn in mf.keys():
1100 if not match(fn):
1100 if not match(fn):
1101 del mf[fn]
1101 del mf[fn]
1102 return mf
1102 return mf
1103
1103
1104 if isinstance(node1, context.changectx):
1104 if isinstance(node1, context.changectx):
1105 ctx1 = node1
1105 ctx1 = node1
1106 else:
1106 else:
1107 ctx1 = self[node1]
1107 ctx1 = self[node1]
1108 if isinstance(node2, context.changectx):
1108 if isinstance(node2, context.changectx):
1109 ctx2 = node2
1109 ctx2 = node2
1110 else:
1110 else:
1111 ctx2 = self[node2]
1111 ctx2 = self[node2]
1112
1112
1113 working = ctx2.rev() is None
1113 working = ctx2.rev() is None
1114 parentworking = working and ctx1 == self['.']
1114 parentworking = working and ctx1 == self['.']
1115 match = match or matchmod.always(self.root, self.getcwd())
1115 match = match or matchmod.always(self.root, self.getcwd())
1116 listignored, listclean, listunknown = ignored, clean, unknown
1116 listignored, listclean, listunknown = ignored, clean, unknown
1117
1117
1118 # load earliest manifest first for caching reasons
1118 # load earliest manifest first for caching reasons
1119 if not working and ctx2.rev() < ctx1.rev():
1119 if not working and ctx2.rev() < ctx1.rev():
1120 ctx2.manifest()
1120 ctx2.manifest()
1121
1121
1122 if not parentworking:
1122 if not parentworking:
1123 def bad(f, msg):
1123 def bad(f, msg):
1124 if f not in ctx1:
1124 if f not in ctx1:
1125 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1125 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1126 match.bad = bad
1126 match.bad = bad
1127
1127
1128 if working: # we need to scan the working dir
1128 if working: # we need to scan the working dir
1129 subrepos = []
1129 subrepos = []
1130 if '.hgsub' in self.dirstate:
1130 if '.hgsub' in self.dirstate:
1131 subrepos = ctx1.substate.keys()
1131 subrepos = ctx1.substate.keys()
1132 s = self.dirstate.status(match, subrepos, listignored,
1132 s = self.dirstate.status(match, subrepos, listignored,
1133 listclean, listunknown)
1133 listclean, listunknown)
1134 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1134 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1135
1135
1136 # check for any possibly clean files
1136 # check for any possibly clean files
1137 if parentworking and cmp:
1137 if parentworking and cmp:
1138 fixup = []
1138 fixup = []
1139 # do a full compare of any files that might have changed
1139 # do a full compare of any files that might have changed
1140 for f in sorted(cmp):
1140 for f in sorted(cmp):
1141 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1141 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1142 or ctx1[f].cmp(ctx2[f])):
1142 or ctx1[f].cmp(ctx2[f])):
1143 modified.append(f)
1143 modified.append(f)
1144 else:
1144 else:
1145 fixup.append(f)
1145 fixup.append(f)
1146
1146
1147 # update dirstate for files that are actually clean
1147 # update dirstate for files that are actually clean
1148 if fixup:
1148 if fixup:
1149 if listclean:
1149 if listclean:
1150 clean += fixup
1150 clean += fixup
1151
1151
1152 try:
1152 try:
1153 # updating the dirstate is optional
1153 # updating the dirstate is optional
1154 # so we don't wait on the lock
1154 # so we don't wait on the lock
1155 wlock = self.wlock(False)
1155 wlock = self.wlock(False)
1156 try:
1156 try:
1157 for f in fixup:
1157 for f in fixup:
1158 self.dirstate.normal(f)
1158 self.dirstate.normal(f)
1159 finally:
1159 finally:
1160 wlock.release()
1160 wlock.release()
1161 except error.LockError:
1161 except error.LockError:
1162 pass
1162 pass
1163
1163
1164 if not parentworking:
1164 if not parentworking:
1165 mf1 = mfmatches(ctx1)
1165 mf1 = mfmatches(ctx1)
1166 if working:
1166 if working:
1167 # we are comparing working dir against non-parent
1167 # we are comparing working dir against non-parent
1168 # generate a pseudo-manifest for the working dir
1168 # generate a pseudo-manifest for the working dir
1169 mf2 = mfmatches(self['.'])
1169 mf2 = mfmatches(self['.'])
1170 for f in cmp + modified + added:
1170 for f in cmp + modified + added:
1171 mf2[f] = None
1171 mf2[f] = None
1172 mf2.set(f, ctx2.flags(f))
1172 mf2.set(f, ctx2.flags(f))
1173 for f in removed:
1173 for f in removed:
1174 if f in mf2:
1174 if f in mf2:
1175 del mf2[f]
1175 del mf2[f]
1176 else:
1176 else:
1177 # we are comparing two revisions
1177 # we are comparing two revisions
1178 deleted, unknown, ignored = [], [], []
1178 deleted, unknown, ignored = [], [], []
1179 mf2 = mfmatches(ctx2)
1179 mf2 = mfmatches(ctx2)
1180
1180
1181 modified, added, clean = [], [], []
1181 modified, added, clean = [], [], []
1182 for fn in mf2:
1182 for fn in mf2:
1183 if fn in mf1:
1183 if fn in mf1:
1184 if (mf1.flags(fn) != mf2.flags(fn) or
1184 if (mf1.flags(fn) != mf2.flags(fn) or
1185 (mf1[fn] != mf2[fn] and
1185 (mf1[fn] != mf2[fn] and
1186 (mf2[fn] or ctx1[fn].cmp(ctx2[fn])))):
1186 (mf2[fn] or ctx1[fn].cmp(ctx2[fn])))):
1187 modified.append(fn)
1187 modified.append(fn)
1188 elif listclean:
1188 elif listclean:
1189 clean.append(fn)
1189 clean.append(fn)
1190 del mf1[fn]
1190 del mf1[fn]
1191 else:
1191 else:
1192 added.append(fn)
1192 added.append(fn)
1193 removed = mf1.keys()
1193 removed = mf1.keys()
1194
1194
1195 r = modified, added, removed, deleted, unknown, ignored, clean
1195 r = modified, added, removed, deleted, unknown, ignored, clean
1196
1196
1197 if listsubrepos:
1197 if listsubrepos:
1198 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1198 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1199 if working:
1199 if working:
1200 rev2 = None
1200 rev2 = None
1201 else:
1201 else:
1202 rev2 = ctx2.substate[subpath][1]
1202 rev2 = ctx2.substate[subpath][1]
1203 try:
1203 try:
1204 submatch = matchmod.narrowmatcher(subpath, match)
1204 submatch = matchmod.narrowmatcher(subpath, match)
1205 s = sub.status(rev2, match=submatch, ignored=listignored,
1205 s = sub.status(rev2, match=submatch, ignored=listignored,
1206 clean=listclean, unknown=listunknown,
1206 clean=listclean, unknown=listunknown,
1207 listsubrepos=True)
1207 listsubrepos=True)
1208 for rfiles, sfiles in zip(r, s):
1208 for rfiles, sfiles in zip(r, s):
1209 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1209 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1210 except error.LookupError:
1210 except error.LookupError:
1211 self.ui.status(_("skipping missing subrepository: %s\n")
1211 self.ui.status(_("skipping missing subrepository: %s\n")
1212 % subpath)
1212 % subpath)
1213
1213
1214 [l.sort() for l in r]
1214 [l.sort() for l in r]
1215 return r
1215 return r
1216
1216
1217 def heads(self, start=None):
1217 def heads(self, start=None):
1218 heads = self.changelog.heads(start)
1218 heads = self.changelog.heads(start)
1219 # sort the output in rev descending order
1219 # sort the output in rev descending order
1220 return sorted(heads, key=self.changelog.rev, reverse=True)
1220 return sorted(heads, key=self.changelog.rev, reverse=True)
1221
1221
1222 def branchheads(self, branch=None, start=None, closed=False):
1222 def branchheads(self, branch=None, start=None, closed=False):
1223 '''return a (possibly filtered) list of heads for the given branch
1223 '''return a (possibly filtered) list of heads for the given branch
1224
1224
1225 Heads are returned in topological order, from newest to oldest.
1225 Heads are returned in topological order, from newest to oldest.
1226 If branch is None, use the dirstate branch.
1226 If branch is None, use the dirstate branch.
1227 If start is not None, return only heads reachable from start.
1227 If start is not None, return only heads reachable from start.
1228 If closed is True, return heads that are marked as closed as well.
1228 If closed is True, return heads that are marked as closed as well.
1229 '''
1229 '''
1230 if branch is None:
1230 if branch is None:
1231 branch = self[None].branch()
1231 branch = self[None].branch()
1232 branches = self.branchmap()
1232 branches = self.branchmap()
1233 if branch not in branches:
1233 if branch not in branches:
1234 return []
1234 return []
1235 # the cache returns heads ordered lowest to highest
1235 # the cache returns heads ordered lowest to highest
1236 bheads = list(reversed(branches[branch]))
1236 bheads = list(reversed(branches[branch]))
1237 if start is not None:
1237 if start is not None:
1238 # filter out the heads that cannot be reached from startrev
1238 # filter out the heads that cannot be reached from startrev
1239 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1239 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1240 bheads = [h for h in bheads if h in fbheads]
1240 bheads = [h for h in bheads if h in fbheads]
1241 if not closed:
1241 if not closed:
1242 bheads = [h for h in bheads if
1242 bheads = [h for h in bheads if
1243 ('close' not in self.changelog.read(h)[5])]
1243 ('close' not in self.changelog.read(h)[5])]
1244 return bheads
1244 return bheads
1245
1245
1246 def branches(self, nodes):
1246 def branches(self, nodes):
1247 if not nodes:
1247 if not nodes:
1248 nodes = [self.changelog.tip()]
1248 nodes = [self.changelog.tip()]
1249 b = []
1249 b = []
1250 for n in nodes:
1250 for n in nodes:
1251 t = n
1251 t = n
1252 while 1:
1252 while 1:
1253 p = self.changelog.parents(n)
1253 p = self.changelog.parents(n)
1254 if p[1] != nullid or p[0] == nullid:
1254 if p[1] != nullid or p[0] == nullid:
1255 b.append((t, n, p[0], p[1]))
1255 b.append((t, n, p[0], p[1]))
1256 break
1256 break
1257 n = p[0]
1257 n = p[0]
1258 return b
1258 return b
1259
1259
1260 def between(self, pairs):
1260 def between(self, pairs):
1261 r = []
1261 r = []
1262
1262
1263 for top, bottom in pairs:
1263 for top, bottom in pairs:
1264 n, l, i = top, [], 0
1264 n, l, i = top, [], 0
1265 f = 1
1265 f = 1
1266
1266
1267 while n != bottom and n != nullid:
1267 while n != bottom and n != nullid:
1268 p = self.changelog.parents(n)[0]
1268 p = self.changelog.parents(n)[0]
1269 if i == f:
1269 if i == f:
1270 l.append(n)
1270 l.append(n)
1271 f = f * 2
1271 f = f * 2
1272 n = p
1272 n = p
1273 i += 1
1273 i += 1
1274
1274
1275 r.append(l)
1275 r.append(l)
1276
1276
1277 return r
1277 return r
1278
1278
1279 def pull(self, remote, heads=None, force=False):
1279 def pull(self, remote, heads=None, force=False):
1280 lock = self.lock()
1280 lock = self.lock()
1281 try:
1281 try:
1282 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1282 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1283 force=force)
1283 force=force)
1284 common, fetch, rheads = tmp
1284 common, fetch, rheads = tmp
1285 if not fetch:
1285 if not fetch:
1286 self.ui.status(_("no changes found\n"))
1286 self.ui.status(_("no changes found\n"))
1287 return 0
1287 return 0
1288
1288
1289 if heads is None and fetch == [nullid]:
1289 if heads is None and fetch == [nullid]:
1290 self.ui.status(_("requesting all changes\n"))
1290 self.ui.status(_("requesting all changes\n"))
1291 elif heads is None and remote.capable('changegroupsubset'):
1291 elif heads is None and remote.capable('changegroupsubset'):
1292 # issue1320, avoid a race if remote changed after discovery
1292 # issue1320, avoid a race if remote changed after discovery
1293 heads = rheads
1293 heads = rheads
1294
1294
1295 if heads is None:
1295 if heads is None:
1296 cg = remote.changegroup(fetch, 'pull')
1296 cg = remote.changegroup(fetch, 'pull')
1297 else:
1297 else:
1298 if not remote.capable('changegroupsubset'):
1298 if not remote.capable('changegroupsubset'):
1299 raise util.Abort(_("partial pull cannot be done because "
1299 raise util.Abort(_("partial pull cannot be done because "
1300 "other repository doesn't support "
1300 "other repository doesn't support "
1301 "changegroupsubset."))
1301 "changegroupsubset."))
1302 cg = remote.changegroupsubset(fetch, heads, 'pull')
1302 cg = remote.changegroupsubset(fetch, heads, 'pull')
1303 return self.addchangegroup(cg, 'pull', remote.url(), lock=lock)
1303 return self.addchangegroup(cg, 'pull', remote.url(), lock=lock)
1304 finally:
1304 finally:
1305 lock.release()
1305 lock.release()
1306
1306
1307 def push(self, remote, force=False, revs=None, newbranch=False):
1307 def push(self, remote, force=False, revs=None, newbranch=False):
1308 '''Push outgoing changesets (limited by revs) from the current
1308 '''Push outgoing changesets (limited by revs) from the current
1309 repository to remote. Return an integer:
1309 repository to remote. Return an integer:
1310 - 0 means HTTP error *or* nothing to push
1310 - 0 means HTTP error *or* nothing to push
1311 - 1 means we pushed and remote head count is unchanged *or*
1311 - 1 means we pushed and remote head count is unchanged *or*
1312 we have outgoing changesets but refused to push
1312 we have outgoing changesets but refused to push
1313 - other values as described by addchangegroup()
1313 - other values as described by addchangegroup()
1314 '''
1314 '''
1315 # there are two ways to push to remote repo:
1315 # there are two ways to push to remote repo:
1316 #
1316 #
1317 # addchangegroup assumes local user can lock remote
1317 # addchangegroup assumes local user can lock remote
1318 # repo (local filesystem, old ssh servers).
1318 # repo (local filesystem, old ssh servers).
1319 #
1319 #
1320 # unbundle assumes local user cannot lock remote repo (new ssh
1320 # unbundle assumes local user cannot lock remote repo (new ssh
1321 # servers, http servers).
1321 # servers, http servers).
1322
1322
1323 lock = None
1323 lock = None
1324 unbundle = remote.capable('unbundle')
1324 unbundle = remote.capable('unbundle')
1325 if not unbundle:
1325 if not unbundle:
1326 lock = remote.lock()
1326 lock = remote.lock()
1327 try:
1327 try:
1328 ret = discovery.prepush(self, remote, force, revs, newbranch)
1328 ret = discovery.prepush(self, remote, force, revs, newbranch)
1329 if ret[0] is None:
1329 if ret[0] is None:
1330 # and here we return 0 for "nothing to push" or 1 for
1330 # and here we return 0 for "nothing to push" or 1 for
1331 # "something to push but I refuse"
1331 # "something to push but I refuse"
1332 return ret[1]
1332 return ret[1]
1333
1333
1334 cg, remote_heads = ret
1334 cg, remote_heads = ret
1335 if unbundle:
1335 if unbundle:
1336 # local repo finds heads on server, finds out what revs it must
1336 # local repo finds heads on server, finds out what revs it must
1337 # push. once revs transferred, if server finds it has
1337 # push. once revs transferred, if server finds it has
1338 # different heads (someone else won commit/push race), server
1338 # different heads (someone else won commit/push race), server
1339 # aborts.
1339 # aborts.
1340 if force:
1340 if force:
1341 remote_heads = ['force']
1341 remote_heads = ['force']
1342 # ssh: return remote's addchangegroup()
1342 # ssh: return remote's addchangegroup()
1343 # http: return remote's addchangegroup() or 0 for error
1343 # http: return remote's addchangegroup() or 0 for error
1344 return remote.unbundle(cg, remote_heads, 'push')
1344 return remote.unbundle(cg, remote_heads, 'push')
1345 else:
1345 else:
1346 # we return an integer indicating remote head count change
1346 # we return an integer indicating remote head count change
1347 return remote.addchangegroup(cg, 'push', self.url(), lock=lock)
1347 return remote.addchangegroup(cg, 'push', self.url(), lock=lock)
1348 finally:
1348 finally:
1349 if lock is not None:
1349 if lock is not None:
1350 lock.release()
1350 lock.release()
1351
1351
1352 def changegroupinfo(self, nodes, source):
1352 def changegroupinfo(self, nodes, source):
1353 if self.ui.verbose or source == 'bundle':
1353 if self.ui.verbose or source == 'bundle':
1354 self.ui.status(_("%d changesets found\n") % len(nodes))
1354 self.ui.status(_("%d changesets found\n") % len(nodes))
1355 if self.ui.debugflag:
1355 if self.ui.debugflag:
1356 self.ui.debug("list of changesets:\n")
1356 self.ui.debug("list of changesets:\n")
1357 for node in nodes:
1357 for node in nodes:
1358 self.ui.debug("%s\n" % hex(node))
1358 self.ui.debug("%s\n" % hex(node))
1359
1359
1360 def changegroupsubset(self, bases, heads, source, extranodes=None):
1360 def changegroupsubset(self, bases, heads, source, extranodes=None):
1361 """Compute a changegroup consisting of all the nodes that are
1361 """Compute a changegroup consisting of all the nodes that are
1362 descendents of any of the bases and ancestors of any of the heads.
1362 descendents of any of the bases and ancestors of any of the heads.
1363 Return a chunkbuffer object whose read() method will return
1363 Return a chunkbuffer object whose read() method will return
1364 successive changegroup chunks.
1364 successive changegroup chunks.
1365
1365
1366 It is fairly complex as determining which filenodes and which
1366 It is fairly complex as determining which filenodes and which
1367 manifest nodes need to be included for the changeset to be complete
1367 manifest nodes need to be included for the changeset to be complete
1368 is non-trivial.
1368 is non-trivial.
1369
1369
1370 Another wrinkle is doing the reverse, figuring out which changeset in
1370 Another wrinkle is doing the reverse, figuring out which changeset in
1371 the changegroup a particular filenode or manifestnode belongs to.
1371 the changegroup a particular filenode or manifestnode belongs to.
1372
1372
1373 The caller can specify some nodes that must be included in the
1373 The caller can specify some nodes that must be included in the
1374 changegroup using the extranodes argument. It should be a dict
1374 changegroup using the extranodes argument. It should be a dict
1375 where the keys are the filenames (or 1 for the manifest), and the
1375 where the keys are the filenames (or 1 for the manifest), and the
1376 values are lists of (node, linknode) tuples, where node is a wanted
1376 values are lists of (node, linknode) tuples, where node is a wanted
1377 node and linknode is the changelog node that should be transmitted as
1377 node and linknode is the changelog node that should be transmitted as
1378 the linkrev.
1378 the linkrev.
1379 """
1379 """
1380
1380
1381 # Set up some initial variables
1381 # Set up some initial variables
1382 # Make it easy to refer to self.changelog
1382 # Make it easy to refer to self.changelog
1383 cl = self.changelog
1383 cl = self.changelog
1384 # Compute the list of changesets in this changegroup.
1384 # Compute the list of changesets in this changegroup.
1385 # Some bases may turn out to be superfluous, and some heads may be
1385 # Some bases may turn out to be superfluous, and some heads may be
1386 # too. nodesbetween will return the minimal set of bases and heads
1386 # too. nodesbetween will return the minimal set of bases and heads
1387 # necessary to re-create the changegroup.
1387 # necessary to re-create the changegroup.
1388 if not bases:
1388 if not bases:
1389 bases = [nullid]
1389 bases = [nullid]
1390 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1390 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1391
1391
1392 if extranodes is None:
1392 if extranodes is None:
1393 # can we go through the fast path ?
1393 # can we go through the fast path ?
1394 heads.sort()
1394 heads.sort()
1395 allheads = self.heads()
1395 allheads = self.heads()
1396 allheads.sort()
1396 allheads.sort()
1397 if heads == allheads:
1397 if heads == allheads:
1398 return self._changegroup(msng_cl_lst, source)
1398 return self._changegroup(msng_cl_lst, source)
1399
1399
1400 # slow path
1400 # slow path
1401 self.hook('preoutgoing', throw=True, source=source)
1401 self.hook('preoutgoing', throw=True, source=source)
1402
1402
1403 self.changegroupinfo(msng_cl_lst, source)
1403 self.changegroupinfo(msng_cl_lst, source)
1404
1404
1405 # We assume that all ancestors of bases are known
1405 # We assume that all ancestors of bases are known
1406 commonrevs = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1406 commonrevs = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1407
1407
1408 # Make it easy to refer to self.manifest
1408 # Make it easy to refer to self.manifest
1409 mnfst = self.manifest
1409 mnfst = self.manifest
1410 # We don't know which manifests are missing yet
1410 # We don't know which manifests are missing yet
1411 msng_mnfst_set = {}
1411 msng_mnfst_set = {}
1412 # Nor do we know which filenodes are missing.
1412 # Nor do we know which filenodes are missing.
1413 msng_filenode_set = {}
1413 msng_filenode_set = {}
1414
1414
1415 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1415 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1416 junk = None
1416 junk = None
1417
1417
1418 # A changeset always belongs to itself, so the changenode lookup
1418 # A changeset always belongs to itself, so the changenode lookup
1419 # function for a changenode is identity.
1419 # function for a changenode is identity.
1420 def identity(x):
1420 def identity(x):
1421 return x
1421 return x
1422
1422
1423 # A function generating function that sets up the initial environment
1423 # A function generating function that sets up the initial environment
1424 # the inner function.
1424 # the inner function.
1425 def filenode_collector(changedfiles):
1425 def filenode_collector(changedfiles):
1426 # This gathers information from each manifestnode included in the
1426 # This gathers information from each manifestnode included in the
1427 # changegroup about which filenodes the manifest node references
1427 # changegroup about which filenodes the manifest node references
1428 # so we can include those in the changegroup too.
1428 # so we can include those in the changegroup too.
1429 #
1429 #
1430 # It also remembers which changenode each filenode belongs to. It
1430 # It also remembers which changenode each filenode belongs to. It
1431 # does this by assuming the a filenode belongs to the changenode
1431 # does this by assuming the a filenode belongs to the changenode
1432 # the first manifest that references it belongs to.
1432 # the first manifest that references it belongs to.
1433 def collect_msng_filenodes(mnfstnode):
1433 def collect_msng_filenodes(mnfstnode):
1434 r = mnfst.rev(mnfstnode)
1434 r = mnfst.rev(mnfstnode)
1435 if mnfst.deltaparent(r) in mnfst.parentrevs(r):
1435 if mnfst.deltaparent(r) in mnfst.parentrevs(r):
1436 # If the previous rev is one of the parents,
1436 # If the previous rev is one of the parents,
1437 # we only need to see a diff.
1437 # we only need to see a diff.
1438 deltamf = mnfst.readdelta(mnfstnode)
1438 deltamf = mnfst.readdelta(mnfstnode)
1439 # For each line in the delta
1439 # For each line in the delta
1440 for f, fnode in deltamf.iteritems():
1440 for f, fnode in deltamf.iteritems():
1441 # And if the file is in the list of files we care
1441 # And if the file is in the list of files we care
1442 # about.
1442 # about.
1443 if f in changedfiles:
1443 if f in changedfiles:
1444 # Get the changenode this manifest belongs to
1444 # Get the changenode this manifest belongs to
1445 clnode = msng_mnfst_set[mnfstnode]
1445 clnode = msng_mnfst_set[mnfstnode]
1446 # Create the set of filenodes for the file if
1446 # Create the set of filenodes for the file if
1447 # there isn't one already.
1447 # there isn't one already.
1448 ndset = msng_filenode_set.setdefault(f, {})
1448 ndset = msng_filenode_set.setdefault(f, {})
1449 # And set the filenode's changelog node to the
1449 # And set the filenode's changelog node to the
1450 # manifest's if it hasn't been set already.
1450 # manifest's if it hasn't been set already.
1451 ndset.setdefault(fnode, clnode)
1451 ndset.setdefault(fnode, clnode)
1452 else:
1452 else:
1453 # Otherwise we need a full manifest.
1453 # Otherwise we need a full manifest.
1454 m = mnfst.read(mnfstnode)
1454 m = mnfst.read(mnfstnode)
1455 # For every file in we care about.
1455 # For every file in we care about.
1456 for f in changedfiles:
1456 for f in changedfiles:
1457 fnode = m.get(f, None)
1457 fnode = m.get(f, None)
1458 # If it's in the manifest
1458 # If it's in the manifest
1459 if fnode is not None:
1459 if fnode is not None:
1460 # See comments above.
1460 # See comments above.
1461 clnode = msng_mnfst_set[mnfstnode]
1461 clnode = msng_mnfst_set[mnfstnode]
1462 ndset = msng_filenode_set.setdefault(f, {})
1462 ndset = msng_filenode_set.setdefault(f, {})
1463 ndset.setdefault(fnode, clnode)
1463 ndset.setdefault(fnode, clnode)
1464 return collect_msng_filenodes
1464 return collect_msng_filenodes
1465
1465
1466 # If we determine that a particular file or manifest node must be a
1466 # If we determine that a particular file or manifest node must be a
1467 # node that the recipient of the changegroup will already have, we can
1467 # node that the recipient of the changegroup will already have, we can
1468 # also assume the recipient will have all the parents. This function
1468 # also assume the recipient will have all the parents. This function
1469 # prunes them from the set of missing nodes.
1469 # prunes them from the set of missing nodes.
1470 def prune(revlog, missingnodes):
1470 def prune(revlog, missingnodes):
1471 hasset = set()
1471 hasset = set()
1472 # If a 'missing' filenode thinks it belongs to a changenode we
1472 # If a 'missing' filenode thinks it belongs to a changenode we
1473 # assume the recipient must have, then the recipient must have
1473 # assume the recipient must have, then the recipient must have
1474 # that filenode.
1474 # that filenode.
1475 for n in missingnodes:
1475 for n in missingnodes:
1476 clrev = revlog.linkrev(revlog.rev(n))
1476 clrev = revlog.linkrev(revlog.rev(n))
1477 if clrev in commonrevs:
1477 if clrev in commonrevs:
1478 hasset.add(n)
1478 hasset.add(n)
1479 for n in hasset:
1479 for n in hasset:
1480 missingnodes.pop(n, None)
1480 missingnodes.pop(n, None)
1481 for r in revlog.ancestors(*[revlog.rev(n) for n in hasset]):
1481 for r in revlog.ancestors(*[revlog.rev(n) for n in hasset]):
1482 missingnodes.pop(revlog.node(r), None)
1482 missingnodes.pop(revlog.node(r), None)
1483
1483
1484 # Add the nodes that were explicitly requested.
1484 # Add the nodes that were explicitly requested.
1485 def add_extra_nodes(name, nodes):
1485 def add_extra_nodes(name, nodes):
1486 if not extranodes or name not in extranodes:
1486 if not extranodes or name not in extranodes:
1487 return
1487 return
1488
1488
1489 for node, linknode in extranodes[name]:
1489 for node, linknode in extranodes[name]:
1490 if node not in nodes:
1490 if node not in nodes:
1491 nodes[node] = linknode
1491 nodes[node] = linknode
1492
1492
1493 # Now that we have all theses utility functions to help out and
1493 # Now that we have all theses utility functions to help out and
1494 # logically divide up the task, generate the group.
1494 # logically divide up the task, generate the group.
1495 def gengroup():
1495 def gengroup():
1496 # The set of changed files starts empty.
1496 # The set of changed files starts empty.
1497 changedfiles = set()
1497 changedfiles = set()
1498 collect = changegroup.collector(cl, msng_mnfst_set, changedfiles)
1498 collect = changegroup.collector(cl, msng_mnfst_set, changedfiles)
1499
1499
1500 # Create a changenode group generator that will call our functions
1500 # Create a changenode group generator that will call our functions
1501 # back to lookup the owning changenode and collect information.
1501 # back to lookup the owning changenode and collect information.
1502 group = cl.group(msng_cl_lst, identity, collect)
1502 group = cl.group(msng_cl_lst, identity, collect)
1503 for cnt, chnk in enumerate(group):
1503 for cnt, chnk in enumerate(group):
1504 yield chnk
1504 yield chnk
1505 # revlog.group yields three entries per node, so
1505 # revlog.group yields three entries per node, so
1506 # dividing by 3 gives an approximation of how many
1506 # dividing by 3 gives an approximation of how many
1507 # nodes have been processed.
1507 # nodes have been processed.
1508 self.ui.progress(_('bundling'), cnt / 3,
1508 self.ui.progress(_('bundling'), cnt / 3,
1509 unit=_('changesets'))
1509 unit=_('changesets'))
1510 changecount = cnt / 3
1510 changecount = cnt / 3
1511 self.ui.progress(_('bundling'), None)
1511 self.ui.progress(_('bundling'), None)
1512
1512
1513 prune(mnfst, msng_mnfst_set)
1513 prune(mnfst, msng_mnfst_set)
1514 add_extra_nodes(1, msng_mnfst_set)
1514 add_extra_nodes(1, msng_mnfst_set)
1515 msng_mnfst_lst = msng_mnfst_set.keys()
1515 msng_mnfst_lst = msng_mnfst_set.keys()
1516 # Sort the manifestnodes by revision number.
1516 # Sort the manifestnodes by revision number.
1517 msng_mnfst_lst.sort(key=mnfst.rev)
1517 msng_mnfst_lst.sort(key=mnfst.rev)
1518 # Create a generator for the manifestnodes that calls our lookup
1518 # Create a generator for the manifestnodes that calls our lookup
1519 # and data collection functions back.
1519 # and data collection functions back.
1520 group = mnfst.group(msng_mnfst_lst,
1520 group = mnfst.group(msng_mnfst_lst,
1521 lambda mnode: msng_mnfst_set[mnode],
1521 lambda mnode: msng_mnfst_set[mnode],
1522 filenode_collector(changedfiles))
1522 filenode_collector(changedfiles))
1523 efiles = {}
1523 efiles = {}
1524 for cnt, chnk in enumerate(group):
1524 for cnt, chnk in enumerate(group):
1525 if cnt % 3 == 1:
1525 if cnt % 3 == 1:
1526 mnode = chnk[:20]
1526 mnode = chnk[:20]
1527 efiles.update(mnfst.readdelta(mnode))
1527 efiles.update(mnfst.readdelta(mnode))
1528 yield chnk
1528 yield chnk
1529 # see above comment for why we divide by 3
1529 # see above comment for why we divide by 3
1530 self.ui.progress(_('bundling'), cnt / 3,
1530 self.ui.progress(_('bundling'), cnt / 3,
1531 unit=_('manifests'), total=changecount)
1531 unit=_('manifests'), total=changecount)
1532 self.ui.progress(_('bundling'), None)
1532 self.ui.progress(_('bundling'), None)
1533 efiles = len(efiles)
1533 efiles = len(efiles)
1534
1534
1535 # These are no longer needed, dereference and toss the memory for
1535 # These are no longer needed, dereference and toss the memory for
1536 # them.
1536 # them.
1537 msng_mnfst_lst = None
1537 msng_mnfst_lst = None
1538 msng_mnfst_set.clear()
1538 msng_mnfst_set.clear()
1539
1539
1540 if extranodes:
1540 if extranodes:
1541 for fname in extranodes:
1541 for fname in extranodes:
1542 if isinstance(fname, int):
1542 if isinstance(fname, int):
1543 continue
1543 continue
1544 msng_filenode_set.setdefault(fname, {})
1544 msng_filenode_set.setdefault(fname, {})
1545 changedfiles.add(fname)
1545 changedfiles.add(fname)
1546 # Go through all our files in order sorted by name.
1546 # Go through all our files in order sorted by name.
1547 for idx, fname in enumerate(sorted(changedfiles)):
1547 for idx, fname in enumerate(sorted(changedfiles)):
1548 filerevlog = self.file(fname)
1548 filerevlog = self.file(fname)
1549 if not len(filerevlog):
1549 if not len(filerevlog):
1550 raise util.Abort(_("empty or missing revlog for %s") % fname)
1550 raise util.Abort(_("empty or missing revlog for %s") % fname)
1551 # Toss out the filenodes that the recipient isn't really
1551 # Toss out the filenodes that the recipient isn't really
1552 # missing.
1552 # missing.
1553 missingfnodes = msng_filenode_set.pop(fname, {})
1553 missingfnodes = msng_filenode_set.pop(fname, {})
1554 prune(filerevlog, missingfnodes)
1554 prune(filerevlog, missingfnodes)
1555 add_extra_nodes(fname, missingfnodes)
1555 add_extra_nodes(fname, missingfnodes)
1556 # If any filenodes are left, generate the group for them,
1556 # If any filenodes are left, generate the group for them,
1557 # otherwise don't bother.
1557 # otherwise don't bother.
1558 if missingfnodes:
1558 if missingfnodes:
1559 yield changegroup.chunkheader(len(fname))
1559 yield changegroup.chunkheader(len(fname))
1560 yield fname
1560 yield fname
1561 # Sort the filenodes by their revision # (topological order)
1561 # Sort the filenodes by their revision # (topological order)
1562 nodeiter = list(missingfnodes)
1562 nodeiter = list(missingfnodes)
1563 nodeiter.sort(key=filerevlog.rev)
1563 nodeiter.sort(key=filerevlog.rev)
1564 # Create a group generator and only pass in a changenode
1564 # Create a group generator and only pass in a changenode
1565 # lookup function as we need to collect no information
1565 # lookup function as we need to collect no information
1566 # from filenodes.
1566 # from filenodes.
1567 group = filerevlog.group(nodeiter,
1567 group = filerevlog.group(nodeiter,
1568 lambda fnode: missingfnodes[fnode])
1568 lambda fnode: missingfnodes[fnode])
1569 for chnk in group:
1569 for chnk in group:
1570 # even though we print the same progress on
1570 # even though we print the same progress on
1571 # most loop iterations, put the progress call
1571 # most loop iterations, put the progress call
1572 # here so that time estimates (if any) can be updated
1572 # here so that time estimates (if any) can be updated
1573 self.ui.progress(
1573 self.ui.progress(
1574 _('bundling'), idx, item=fname,
1574 _('bundling'), idx, item=fname,
1575 unit=_('files'), total=efiles)
1575 unit=_('files'), total=efiles)
1576 yield chnk
1576 yield chnk
1577 # Signal that no more groups are left.
1577 # Signal that no more groups are left.
1578 yield changegroup.closechunk()
1578 yield changegroup.closechunk()
1579 self.ui.progress(_('bundling'), None)
1579 self.ui.progress(_('bundling'), None)
1580
1580
1581 if msng_cl_lst:
1581 if msng_cl_lst:
1582 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1582 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1583
1583
1584 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1584 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1585
1585
1586 def changegroup(self, basenodes, source):
1586 def changegroup(self, basenodes, source):
1587 # to avoid a race we use changegroupsubset() (issue1320)
1587 # to avoid a race we use changegroupsubset() (issue1320)
1588 return self.changegroupsubset(basenodes, self.heads(), source)
1588 return self.changegroupsubset(basenodes, self.heads(), source)
1589
1589
1590 def _changegroup(self, nodes, source):
1590 def _changegroup(self, nodes, source):
1591 """Compute the changegroup of all nodes that we have that a recipient
1591 """Compute the changegroup of all nodes that we have that a recipient
1592 doesn't. Return a chunkbuffer object whose read() method will return
1592 doesn't. Return a chunkbuffer object whose read() method will return
1593 successive changegroup chunks.
1593 successive changegroup chunks.
1594
1594
1595 This is much easier than the previous function as we can assume that
1595 This is much easier than the previous function as we can assume that
1596 the recipient has any changenode we aren't sending them.
1596 the recipient has any changenode we aren't sending them.
1597
1597
1598 nodes is the set of nodes to send"""
1598 nodes is the set of nodes to send"""
1599
1599
1600 self.hook('preoutgoing', throw=True, source=source)
1600 self.hook('preoutgoing', throw=True, source=source)
1601
1601
1602 cl = self.changelog
1602 cl = self.changelog
1603 revset = set([cl.rev(n) for n in nodes])
1603 revset = set([cl.rev(n) for n in nodes])
1604 self.changegroupinfo(nodes, source)
1604 self.changegroupinfo(nodes, source)
1605
1605
1606 def identity(x):
1606 def identity(x):
1607 return x
1607 return x
1608
1608
1609 def gennodelst(log):
1609 def gennodelst(log):
1610 for r in log:
1610 for r in log:
1611 if log.linkrev(r) in revset:
1611 if log.linkrev(r) in revset:
1612 yield log.node(r)
1612 yield log.node(r)
1613
1613
1614 def lookuplinkrev_func(revlog):
1614 def lookuplinkrev_func(revlog):
1615 def lookuplinkrev(n):
1615 def lookuplinkrev(n):
1616 return cl.node(revlog.linkrev(revlog.rev(n)))
1616 return cl.node(revlog.linkrev(revlog.rev(n)))
1617 return lookuplinkrev
1617 return lookuplinkrev
1618
1618
1619 def gengroup():
1619 def gengroup():
1620 '''yield a sequence of changegroup chunks (strings)'''
1620 '''yield a sequence of changegroup chunks (strings)'''
1621 # construct a list of all changed files
1621 # construct a list of all changed files
1622 changedfiles = set()
1622 changedfiles = set()
1623 mmfs = {}
1623 mmfs = {}
1624 collect = changegroup.collector(cl, mmfs, changedfiles)
1624 collect = changegroup.collector(cl, mmfs, changedfiles)
1625
1625
1626 for cnt, chnk in enumerate(cl.group(nodes, identity, collect)):
1626 for cnt, chnk in enumerate(cl.group(nodes, identity, collect)):
1627 # revlog.group yields three entries per node, so
1627 # revlog.group yields three entries per node, so
1628 # dividing by 3 gives an approximation of how many
1628 # dividing by 3 gives an approximation of how many
1629 # nodes have been processed.
1629 # nodes have been processed.
1630 self.ui.progress(_('bundling'), cnt / 3, unit=_('changesets'))
1630 self.ui.progress(_('bundling'), cnt / 3, unit=_('changesets'))
1631 yield chnk
1631 yield chnk
1632 changecount = cnt / 3
1632 changecount = cnt / 3
1633 self.ui.progress(_('bundling'), None)
1633 self.ui.progress(_('bundling'), None)
1634
1634
1635 mnfst = self.manifest
1635 mnfst = self.manifest
1636 nodeiter = gennodelst(mnfst)
1636 nodeiter = gennodelst(mnfst)
1637 efiles = {}
1637 efiles = {}
1638 for cnt, chnk in enumerate(mnfst.group(nodeiter,
1638 for cnt, chnk in enumerate(mnfst.group(nodeiter,
1639 lookuplinkrev_func(mnfst))):
1639 lookuplinkrev_func(mnfst))):
1640 if cnt % 3 == 1:
1640 if cnt % 3 == 1:
1641 mnode = chnk[:20]
1641 mnode = chnk[:20]
1642 efiles.update(mnfst.readdelta(mnode))
1642 efiles.update(mnfst.readdelta(mnode))
1643 # see above comment for why we divide by 3
1643 # see above comment for why we divide by 3
1644 self.ui.progress(_('bundling'), cnt / 3,
1644 self.ui.progress(_('bundling'), cnt / 3,
1645 unit=_('manifests'), total=changecount)
1645 unit=_('manifests'), total=changecount)
1646 yield chnk
1646 yield chnk
1647 efiles = len(efiles)
1647 efiles = len(efiles)
1648 self.ui.progress(_('bundling'), None)
1648 self.ui.progress(_('bundling'), None)
1649
1649
1650 for idx, fname in enumerate(sorted(changedfiles)):
1650 for idx, fname in enumerate(sorted(changedfiles)):
1651 filerevlog = self.file(fname)
1651 filerevlog = self.file(fname)
1652 if not len(filerevlog):
1652 if not len(filerevlog):
1653 raise util.Abort(_("empty or missing revlog for %s") % fname)
1653 raise util.Abort(_("empty or missing revlog for %s") % fname)
1654 nodeiter = gennodelst(filerevlog)
1654 nodeiter = gennodelst(filerevlog)
1655 nodeiter = list(nodeiter)
1655 nodeiter = list(nodeiter)
1656 if nodeiter:
1656 if nodeiter:
1657 yield changegroup.chunkheader(len(fname))
1657 yield changegroup.chunkheader(len(fname))
1658 yield fname
1658 yield fname
1659 lookup = lookuplinkrev_func(filerevlog)
1659 lookup = lookuplinkrev_func(filerevlog)
1660 for chnk in filerevlog.group(nodeiter, lookup):
1660 for chnk in filerevlog.group(nodeiter, lookup):
1661 self.ui.progress(
1661 self.ui.progress(
1662 _('bundling'), idx, item=fname,
1662 _('bundling'), idx, item=fname,
1663 total=efiles, unit=_('files'))
1663 total=efiles, unit=_('files'))
1664 yield chnk
1664 yield chnk
1665 self.ui.progress(_('bundling'), None)
1665 self.ui.progress(_('bundling'), None)
1666
1666
1667 yield changegroup.closechunk()
1667 yield changegroup.closechunk()
1668
1668
1669 if nodes:
1669 if nodes:
1670 self.hook('outgoing', node=hex(nodes[0]), source=source)
1670 self.hook('outgoing', node=hex(nodes[0]), source=source)
1671
1671
1672 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1672 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1673
1673
1674 def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
1674 def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
1675 """Add the changegroup returned by source.read() to this repo.
1675 """Add the changegroup returned by source.read() to this repo.
1676 srctype is a string like 'push', 'pull', or 'unbundle'. url is
1676 srctype is a string like 'push', 'pull', or 'unbundle'. url is
1677 the URL of the repo where this changegroup is coming from.
1677 the URL of the repo where this changegroup is coming from.
1678
1678
1679 Return an integer summarizing the change to this repo:
1679 Return an integer summarizing the change to this repo:
1680 - nothing changed or no source: 0
1680 - nothing changed or no source: 0
1681 - more heads than before: 1+added heads (2..n)
1681 - more heads than before: 1+added heads (2..n)
1682 - fewer heads than before: -1-removed heads (-2..-n)
1682 - fewer heads than before: -1-removed heads (-2..-n)
1683 - number of heads stays the same: 1
1683 - number of heads stays the same: 1
1684 """
1684 """
1685 def csmap(x):
1685 def csmap(x):
1686 self.ui.debug("add changeset %s\n" % short(x))
1686 self.ui.debug("add changeset %s\n" % short(x))
1687 return len(cl)
1687 return len(cl)
1688
1688
1689 def revmap(x):
1689 def revmap(x):
1690 return cl.rev(x)
1690 return cl.rev(x)
1691
1691
1692 if not source:
1692 if not source:
1693 return 0
1693 return 0
1694
1694
1695 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1695 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1696
1696
1697 changesets = files = revisions = 0
1697 changesets = files = revisions = 0
1698 efiles = set()
1698 efiles = set()
1699
1699
1700 # write changelog data to temp files so concurrent readers will not see
1700 # write changelog data to temp files so concurrent readers will not see
1701 # inconsistent view
1701 # inconsistent view
1702 cl = self.changelog
1702 cl = self.changelog
1703 cl.delayupdate()
1703 cl.delayupdate()
1704 oldheads = len(cl.heads())
1704 oldheads = len(cl.heads())
1705
1705
1706 tr = self.transaction("\n".join([srctype, urlmod.hidepassword(url)]))
1706 tr = self.transaction("\n".join([srctype, urlmod.hidepassword(url)]))
1707 try:
1707 try:
1708 trp = weakref.proxy(tr)
1708 trp = weakref.proxy(tr)
1709 # pull off the changeset group
1709 # pull off the changeset group
1710 self.ui.status(_("adding changesets\n"))
1710 self.ui.status(_("adding changesets\n"))
1711 clstart = len(cl)
1711 clstart = len(cl)
1712 class prog(object):
1712 class prog(object):
1713 step = _('changesets')
1713 step = _('changesets')
1714 count = 1
1714 count = 1
1715 ui = self.ui
1715 ui = self.ui
1716 total = None
1716 total = None
1717 def __call__(self):
1717 def __call__(self):
1718 self.ui.progress(self.step, self.count, unit=_('chunks'),
1718 self.ui.progress(self.step, self.count, unit=_('chunks'),
1719 total=self.total)
1719 total=self.total)
1720 self.count += 1
1720 self.count += 1
1721 pr = prog()
1721 pr = prog()
1722 source.callback = pr
1722 source.callback = pr
1723
1723
1724 if (cl.addgroup(source, csmap, trp) is None
1724 if (cl.addgroup(source, csmap, trp) is None
1725 and not emptyok):
1725 and not emptyok):
1726 raise util.Abort(_("received changelog group is empty"))
1726 raise util.Abort(_("received changelog group is empty"))
1727 clend = len(cl)
1727 clend = len(cl)
1728 changesets = clend - clstart
1728 changesets = clend - clstart
1729 for c in xrange(clstart, clend):
1729 for c in xrange(clstart, clend):
1730 efiles.update(self[c].files())
1730 efiles.update(self[c].files())
1731 efiles = len(efiles)
1731 efiles = len(efiles)
1732 self.ui.progress(_('changesets'), None)
1732 self.ui.progress(_('changesets'), None)
1733
1733
1734 # pull off the manifest group
1734 # pull off the manifest group
1735 self.ui.status(_("adding manifests\n"))
1735 self.ui.status(_("adding manifests\n"))
1736 pr.step = _('manifests')
1736 pr.step = _('manifests')
1737 pr.count = 1
1737 pr.count = 1
1738 pr.total = changesets # manifests <= changesets
1738 pr.total = changesets # manifests <= changesets
1739 # no need to check for empty manifest group here:
1739 # no need to check for empty manifest group here:
1740 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1740 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1741 # no new manifest will be created and the manifest group will
1741 # no new manifest will be created and the manifest group will
1742 # be empty during the pull
1742 # be empty during the pull
1743 self.manifest.addgroup(source, revmap, trp)
1743 self.manifest.addgroup(source, revmap, trp)
1744 self.ui.progress(_('manifests'), None)
1744 self.ui.progress(_('manifests'), None)
1745
1745
1746 needfiles = {}
1746 needfiles = {}
1747 if self.ui.configbool('server', 'validate', default=False):
1747 if self.ui.configbool('server', 'validate', default=False):
1748 # validate incoming csets have their manifests
1748 # validate incoming csets have their manifests
1749 for cset in xrange(clstart, clend):
1749 for cset in xrange(clstart, clend):
1750 mfest = self.changelog.read(self.changelog.node(cset))[0]
1750 mfest = self.changelog.read(self.changelog.node(cset))[0]
1751 mfest = self.manifest.readdelta(mfest)
1751 mfest = self.manifest.readdelta(mfest)
1752 # store file nodes we must see
1752 # store file nodes we must see
1753 for f, n in mfest.iteritems():
1753 for f, n in mfest.iteritems():
1754 needfiles.setdefault(f, set()).add(n)
1754 needfiles.setdefault(f, set()).add(n)
1755
1755
1756 # process the files
1756 # process the files
1757 self.ui.status(_("adding file changes\n"))
1757 self.ui.status(_("adding file changes\n"))
1758 pr.step = 'files'
1758 pr.step = 'files'
1759 pr.count = 1
1759 pr.count = 1
1760 pr.total = efiles
1760 pr.total = efiles
1761 source.callback = None
1761 source.callback = None
1762
1762
1763 while 1:
1763 while 1:
1764 f = source.chunk()
1764 f = source.chunk()
1765 if not f:
1765 if not f:
1766 break
1766 break
1767 self.ui.debug("adding %s revisions\n" % f)
1767 self.ui.debug("adding %s revisions\n" % f)
1768 pr()
1768 pr()
1769 fl = self.file(f)
1769 fl = self.file(f)
1770 o = len(fl)
1770 o = len(fl)
1771 if fl.addgroup(source, revmap, trp) is None:
1771 if fl.addgroup(source, revmap, trp) is None:
1772 raise util.Abort(_("received file revlog group is empty"))
1772 raise util.Abort(_("received file revlog group is empty"))
1773 revisions += len(fl) - o
1773 revisions += len(fl) - o
1774 files += 1
1774 files += 1
1775 if f in needfiles:
1775 if f in needfiles:
1776 needs = needfiles[f]
1776 needs = needfiles[f]
1777 for new in xrange(o, len(fl)):
1777 for new in xrange(o, len(fl)):
1778 n = fl.node(new)
1778 n = fl.node(new)
1779 if n in needs:
1779 if n in needs:
1780 needs.remove(n)
1780 needs.remove(n)
1781 if not needs:
1781 if not needs:
1782 del needfiles[f]
1782 del needfiles[f]
1783 self.ui.progress(_('files'), None)
1783 self.ui.progress(_('files'), None)
1784
1784
1785 for f, needs in needfiles.iteritems():
1785 for f, needs in needfiles.iteritems():
1786 fl = self.file(f)
1786 fl = self.file(f)
1787 for n in needs:
1787 for n in needs:
1788 try:
1788 try:
1789 fl.rev(n)
1789 fl.rev(n)
1790 except error.LookupError:
1790 except error.LookupError:
1791 raise util.Abort(
1791 raise util.Abort(
1792 _('missing file data for %s:%s - run hg verify') %
1792 _('missing file data for %s:%s - run hg verify') %
1793 (f, hex(n)))
1793 (f, hex(n)))
1794
1794
1795 newheads = len(cl.heads())
1795 newheads = len(cl.heads())
1796 heads = ""
1796 heads = ""
1797 if oldheads and newheads != oldheads:
1797 if oldheads and newheads != oldheads:
1798 heads = _(" (%+d heads)") % (newheads - oldheads)
1798 heads = _(" (%+d heads)") % (newheads - oldheads)
1799
1799
1800 self.ui.status(_("added %d changesets"
1800 self.ui.status(_("added %d changesets"
1801 " with %d changes to %d files%s\n")
1801 " with %d changes to %d files%s\n")
1802 % (changesets, revisions, files, heads))
1802 % (changesets, revisions, files, heads))
1803
1803
1804 if changesets > 0:
1804 if changesets > 0:
1805 p = lambda: cl.writepending() and self.root or ""
1805 p = lambda: cl.writepending() and self.root or ""
1806 self.hook('pretxnchangegroup', throw=True,
1806 self.hook('pretxnchangegroup', throw=True,
1807 node=hex(cl.node(clstart)), source=srctype,
1807 node=hex(cl.node(clstart)), source=srctype,
1808 url=url, pending=p)
1808 url=url, pending=p)
1809
1809
1810 # make changelog see real files again
1810 # make changelog see real files again
1811 cl.finalize(trp)
1811 cl.finalize(trp)
1812
1812
1813 tr.close()
1813 tr.close()
1814 finally:
1814 finally:
1815 tr.release()
1815 tr.release()
1816 if lock:
1816 if lock:
1817 lock.release()
1817 lock.release()
1818
1818
1819 if changesets > 0:
1819 if changesets > 0:
1820 # forcefully update the on-disk branch cache
1820 # forcefully update the on-disk branch cache
1821 self.ui.debug("updating the branch cache\n")
1821 self.ui.debug("updating the branch cache\n")
1822 self.updatebranchcache()
1822 self.updatebranchcache()
1823 self.hook("changegroup", node=hex(cl.node(clstart)),
1823 self.hook("changegroup", node=hex(cl.node(clstart)),
1824 source=srctype, url=url)
1824 source=srctype, url=url)
1825
1825
1826 for i in xrange(clstart, clend):
1826 for i in xrange(clstart, clend):
1827 self.hook("incoming", node=hex(cl.node(i)),
1827 self.hook("incoming", node=hex(cl.node(i)),
1828 source=srctype, url=url)
1828 source=srctype, url=url)
1829
1829
1830 # never return 0 here:
1830 # never return 0 here:
1831 if newheads < oldheads:
1831 if newheads < oldheads:
1832 return newheads - oldheads - 1
1832 return newheads - oldheads - 1
1833 else:
1833 else:
1834 return newheads - oldheads + 1
1834 return newheads - oldheads + 1
1835
1835
1836
1836
1837 def stream_in(self, remote, requirements):
1837 def stream_in(self, remote, requirements):
1838 fp = remote.stream_out()
1838 fp = remote.stream_out()
1839 l = fp.readline()
1839 l = fp.readline()
1840 try:
1840 try:
1841 resp = int(l)
1841 resp = int(l)
1842 except ValueError:
1842 except ValueError:
1843 raise error.ResponseError(
1843 raise error.ResponseError(
1844 _('Unexpected response from remote server:'), l)
1844 _('Unexpected response from remote server:'), l)
1845 if resp == 1:
1845 if resp == 1:
1846 raise util.Abort(_('operation forbidden by server'))
1846 raise util.Abort(_('operation forbidden by server'))
1847 elif resp == 2:
1847 elif resp == 2:
1848 raise util.Abort(_('locking the remote repository failed'))
1848 raise util.Abort(_('locking the remote repository failed'))
1849 elif resp != 0:
1849 elif resp != 0:
1850 raise util.Abort(_('the server sent an unknown error code'))
1850 raise util.Abort(_('the server sent an unknown error code'))
1851 self.ui.status(_('streaming all changes\n'))
1851 self.ui.status(_('streaming all changes\n'))
1852 l = fp.readline()
1852 l = fp.readline()
1853 try:
1853 try:
1854 total_files, total_bytes = map(int, l.split(' ', 1))
1854 total_files, total_bytes = map(int, l.split(' ', 1))
1855 except (ValueError, TypeError):
1855 except (ValueError, TypeError):
1856 raise error.ResponseError(
1856 raise error.ResponseError(
1857 _('Unexpected response from remote server:'), l)
1857 _('Unexpected response from remote server:'), l)
1858 self.ui.status(_('%d files to transfer, %s of data\n') %
1858 self.ui.status(_('%d files to transfer, %s of data\n') %
1859 (total_files, util.bytecount(total_bytes)))
1859 (total_files, util.bytecount(total_bytes)))
1860 start = time.time()
1860 start = time.time()
1861 for i in xrange(total_files):
1861 for i in xrange(total_files):
1862 # XXX doesn't support '\n' or '\r' in filenames
1862 # XXX doesn't support '\n' or '\r' in filenames
1863 l = fp.readline()
1863 l = fp.readline()
1864 try:
1864 try:
1865 name, size = l.split('\0', 1)
1865 name, size = l.split('\0', 1)
1866 size = int(size)
1866 size = int(size)
1867 except (ValueError, TypeError):
1867 except (ValueError, TypeError):
1868 raise error.ResponseError(
1868 raise error.ResponseError(
1869 _('Unexpected response from remote server:'), l)
1869 _('Unexpected response from remote server:'), l)
1870 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1870 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1871 # for backwards compat, name was partially encoded
1871 # for backwards compat, name was partially encoded
1872 ofp = self.sopener(store.decodedir(name), 'w')
1872 ofp = self.sopener(store.decodedir(name), 'w')
1873 for chunk in util.filechunkiter(fp, limit=size):
1873 for chunk in util.filechunkiter(fp, limit=size):
1874 ofp.write(chunk)
1874 ofp.write(chunk)
1875 ofp.close()
1875 ofp.close()
1876 elapsed = time.time() - start
1876 elapsed = time.time() - start
1877 if elapsed <= 0:
1877 if elapsed <= 0:
1878 elapsed = 0.001
1878 elapsed = 0.001
1879 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1879 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1880 (util.bytecount(total_bytes), elapsed,
1880 (util.bytecount(total_bytes), elapsed,
1881 util.bytecount(total_bytes / elapsed)))
1881 util.bytecount(total_bytes / elapsed)))
1882
1882
1883 # new requirements = old non-format requirements + new format-related
1883 # new requirements = old non-format requirements + new format-related
1884 # requirements from the streamed-in repository
1884 # requirements from the streamed-in repository
1885 requirements.update(set(self.requirements) - self.supportedformats)
1885 requirements.update(set(self.requirements) - self.supportedformats)
1886 self._applyrequirements(requirements)
1886 self._applyrequirements(requirements)
1887 self._writerequirements()
1887 self._writerequirements()
1888
1888
1889 self.invalidate()
1889 self.invalidate()
1890 return len(self.heads()) + 1
1890 return len(self.heads()) + 1
1891
1891
1892 def clone(self, remote, heads=[], stream=False):
1892 def clone(self, remote, heads=[], stream=False):
1893 '''clone remote repository.
1893 '''clone remote repository.
1894
1894
1895 keyword arguments:
1895 keyword arguments:
1896 heads: list of revs to clone (forces use of pull)
1896 heads: list of revs to clone (forces use of pull)
1897 stream: use streaming clone if possible'''
1897 stream: use streaming clone if possible'''
1898
1898
1899 # now, all clients that can request uncompressed clones can
1899 # now, all clients that can request uncompressed clones can
1900 # read repo formats supported by all servers that can serve
1900 # read repo formats supported by all servers that can serve
1901 # them.
1901 # them.
1902
1902
1903 # if revlog format changes, client will have to check version
1903 # if revlog format changes, client will have to check version
1904 # and format flags on "stream" capability, and use
1904 # and format flags on "stream" capability, and use
1905 # uncompressed only if compatible.
1905 # uncompressed only if compatible.
1906
1906
1907 if stream and not heads:
1907 if stream and not heads:
1908 # 'stream' means remote revlog format is revlogv1 only
1908 # 'stream' means remote revlog format is revlogv1 only
1909 if remote.capable('stream'):
1909 if remote.capable('stream'):
1910 return self.stream_in(remote, set(('revlogv1',)))
1910 return self.stream_in(remote, set(('revlogv1',)))
1911 # otherwise, 'streamreqs' contains the remote revlog format
1911 # otherwise, 'streamreqs' contains the remote revlog format
1912 streamreqs = remote.capable('streamreqs')
1912 streamreqs = remote.capable('streamreqs')
1913 if streamreqs:
1913 if streamreqs:
1914 streamreqs = set(streamreqs.split(','))
1914 streamreqs = set(streamreqs.split(','))
1915 # if we support it, stream in and adjust our requirements
1915 # if we support it, stream in and adjust our requirements
1916 if not streamreqs - self.supportedformats:
1916 if not streamreqs - self.supportedformats:
1917 return self.stream_in(remote, streamreqs)
1917 return self.stream_in(remote, streamreqs)
1918 return self.pull(remote, heads)
1918 return self.pull(remote, heads)
1919
1919
1920 def pushkey(self, namespace, key, old, new):
1920 def pushkey(self, namespace, key, old, new):
1921 return pushkey.push(self, namespace, key, old, new)
1921 return pushkey.push(self, namespace, key, old, new)
1922
1922
1923 def listkeys(self, namespace):
1923 def listkeys(self, namespace):
1924 return pushkey.list(self, namespace)
1924 return pushkey.list(self, namespace)
1925
1925
1926 # used to avoid circular references so destructors work
1926 # used to avoid circular references so destructors work
1927 def aftertrans(files):
1927 def aftertrans(files):
1928 renamefiles = [tuple(t) for t in files]
1928 renamefiles = [tuple(t) for t in files]
1929 def a():
1929 def a():
1930 for src, dest in renamefiles:
1930 for src, dest in renamefiles:
1931 util.rename(src, dest)
1931 util.rename(src, dest)
1932 return a
1932 return a
1933
1933
1934 def instance(ui, path, create):
1934 def instance(ui, path, create):
1935 return localrepository(ui, util.drop_scheme('file', path), create)
1935 return localrepository(ui, util.drop_scheme('file', path), create)
1936
1936
1937 def islocal(path):
1937 def islocal(path):
1938 return True
1938 return True
@@ -1,1496 +1,1496 b''
1 # util.py - Mercurial utility functions and platform specfic implementations
1 # util.py - Mercurial utility functions and platform specfic implementations
2 #
2 #
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
6 #
6 #
7 # This software may be used and distributed according to the terms of the
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
8 # GNU General Public License version 2 or any later version.
9
9
10 """Mercurial utility functions and platform specfic implementations.
10 """Mercurial utility functions and platform specfic implementations.
11
11
12 This contains helper routines that are independent of the SCM core and
12 This contains helper routines that are independent of the SCM core and
13 hide platform-specific details from the core.
13 hide platform-specific details from the core.
14 """
14 """
15
15
16 from i18n import _
16 from i18n import _
17 import error, osutil, encoding
17 import error, osutil, encoding
18 import errno, re, shutil, sys, tempfile, traceback
18 import errno, re, shutil, sys, tempfile, traceback
19 import os, stat, time, calendar, textwrap, unicodedata, signal
19 import os, stat, time, calendar, textwrap, unicodedata, signal
20 import imp, socket
20 import imp, socket
21
21
22 # Python compatibility
22 # Python compatibility
23
23
24 def sha1(s):
24 def sha1(s):
25 return _fastsha1(s)
25 return _fastsha1(s)
26
26
27 def _fastsha1(s):
27 def _fastsha1(s):
28 # This function will import sha1 from hashlib or sha (whichever is
28 # This function will import sha1 from hashlib or sha (whichever is
29 # available) and overwrite itself with it on the first call.
29 # available) and overwrite itself with it on the first call.
30 # Subsequent calls will go directly to the imported function.
30 # Subsequent calls will go directly to the imported function.
31 if sys.version_info >= (2, 5):
31 if sys.version_info >= (2, 5):
32 from hashlib import sha1 as _sha1
32 from hashlib import sha1 as _sha1
33 else:
33 else:
34 from sha import sha as _sha1
34 from sha import sha as _sha1
35 global _fastsha1, sha1
35 global _fastsha1, sha1
36 _fastsha1 = sha1 = _sha1
36 _fastsha1 = sha1 = _sha1
37 return _sha1(s)
37 return _sha1(s)
38
38
39 import __builtin__
39 import __builtin__
40
40
41 if sys.version_info[0] < 3:
41 if sys.version_info[0] < 3:
42 def fakebuffer(sliceable, offset=0):
42 def fakebuffer(sliceable, offset=0):
43 return sliceable[offset:]
43 return sliceable[offset:]
44 else:
44 else:
45 def fakebuffer(sliceable, offset=0):
45 def fakebuffer(sliceable, offset=0):
46 return memoryview(sliceable)[offset:]
46 return memoryview(sliceable)[offset:]
47 try:
47 try:
48 buffer
48 buffer
49 except NameError:
49 except NameError:
50 __builtin__.buffer = fakebuffer
50 __builtin__.buffer = fakebuffer
51
51
52 import subprocess
52 import subprocess
53 closefds = os.name == 'posix'
53 closefds = os.name == 'posix'
54
54
55 def popen2(cmd, env=None, newlines=False):
55 def popen2(cmd, env=None, newlines=False):
56 # Setting bufsize to -1 lets the system decide the buffer size.
56 # Setting bufsize to -1 lets the system decide the buffer size.
57 # The default for bufsize is 0, meaning unbuffered. This leads to
57 # The default for bufsize is 0, meaning unbuffered. This leads to
58 # poor performance on Mac OS X: http://bugs.python.org/issue4194
58 # poor performance on Mac OS X: http://bugs.python.org/issue4194
59 p = subprocess.Popen(cmd, shell=True, bufsize=-1,
59 p = subprocess.Popen(cmd, shell=True, bufsize=-1,
60 close_fds=closefds,
60 close_fds=closefds,
61 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
61 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
62 universal_newlines=newlines,
62 universal_newlines=newlines,
63 env=env)
63 env=env)
64 return p.stdin, p.stdout
64 return p.stdin, p.stdout
65
65
66 def popen3(cmd, env=None, newlines=False):
66 def popen3(cmd, env=None, newlines=False):
67 p = subprocess.Popen(cmd, shell=True, bufsize=-1,
67 p = subprocess.Popen(cmd, shell=True, bufsize=-1,
68 close_fds=closefds,
68 close_fds=closefds,
69 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
69 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
70 stderr=subprocess.PIPE,
70 stderr=subprocess.PIPE,
71 universal_newlines=newlines,
71 universal_newlines=newlines,
72 env=env)
72 env=env)
73 return p.stdin, p.stdout, p.stderr
73 return p.stdin, p.stdout, p.stderr
74
74
75 def version():
75 def version():
76 """Return version information if available."""
76 """Return version information if available."""
77 try:
77 try:
78 import __version__
78 import __version__
79 return __version__.version
79 return __version__.version
80 except ImportError:
80 except ImportError:
81 return 'unknown'
81 return 'unknown'
82
82
83 # used by parsedate
83 # used by parsedate
84 defaultdateformats = (
84 defaultdateformats = (
85 '%Y-%m-%d %H:%M:%S',
85 '%Y-%m-%d %H:%M:%S',
86 '%Y-%m-%d %I:%M:%S%p',
86 '%Y-%m-%d %I:%M:%S%p',
87 '%Y-%m-%d %H:%M',
87 '%Y-%m-%d %H:%M',
88 '%Y-%m-%d %I:%M%p',
88 '%Y-%m-%d %I:%M%p',
89 '%Y-%m-%d',
89 '%Y-%m-%d',
90 '%m-%d',
90 '%m-%d',
91 '%m/%d',
91 '%m/%d',
92 '%m/%d/%y',
92 '%m/%d/%y',
93 '%m/%d/%Y',
93 '%m/%d/%Y',
94 '%a %b %d %H:%M:%S %Y',
94 '%a %b %d %H:%M:%S %Y',
95 '%a %b %d %I:%M:%S%p %Y',
95 '%a %b %d %I:%M:%S%p %Y',
96 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822"
96 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822"
97 '%b %d %H:%M:%S %Y',
97 '%b %d %H:%M:%S %Y',
98 '%b %d %I:%M:%S%p %Y',
98 '%b %d %I:%M:%S%p %Y',
99 '%b %d %H:%M:%S',
99 '%b %d %H:%M:%S',
100 '%b %d %I:%M:%S%p',
100 '%b %d %I:%M:%S%p',
101 '%b %d %H:%M',
101 '%b %d %H:%M',
102 '%b %d %I:%M%p',
102 '%b %d %I:%M%p',
103 '%b %d %Y',
103 '%b %d %Y',
104 '%b %d',
104 '%b %d',
105 '%H:%M:%S',
105 '%H:%M:%S',
106 '%I:%M:%S%p',
106 '%I:%M:%S%p',
107 '%H:%M',
107 '%H:%M',
108 '%I:%M%p',
108 '%I:%M%p',
109 )
109 )
110
110
111 extendeddateformats = defaultdateformats + (
111 extendeddateformats = defaultdateformats + (
112 "%Y",
112 "%Y",
113 "%Y-%m",
113 "%Y-%m",
114 "%b",
114 "%b",
115 "%b %Y",
115 "%b %Y",
116 )
116 )
117
117
118 def cachefunc(func):
118 def cachefunc(func):
119 '''cache the result of function calls'''
119 '''cache the result of function calls'''
120 # XXX doesn't handle keywords args
120 # XXX doesn't handle keywords args
121 cache = {}
121 cache = {}
122 if func.func_code.co_argcount == 1:
122 if func.func_code.co_argcount == 1:
123 # we gain a small amount of time because
123 # we gain a small amount of time because
124 # we don't need to pack/unpack the list
124 # we don't need to pack/unpack the list
125 def f(arg):
125 def f(arg):
126 if arg not in cache:
126 if arg not in cache:
127 cache[arg] = func(arg)
127 cache[arg] = func(arg)
128 return cache[arg]
128 return cache[arg]
129 else:
129 else:
130 def f(*args):
130 def f(*args):
131 if args not in cache:
131 if args not in cache:
132 cache[args] = func(*args)
132 cache[args] = func(*args)
133 return cache[args]
133 return cache[args]
134
134
135 return f
135 return f
136
136
137 def lrucachefunc(func):
137 def lrucachefunc(func):
138 '''cache most recent results of function calls'''
138 '''cache most recent results of function calls'''
139 cache = {}
139 cache = {}
140 order = []
140 order = []
141 if func.func_code.co_argcount == 1:
141 if func.func_code.co_argcount == 1:
142 def f(arg):
142 def f(arg):
143 if arg not in cache:
143 if arg not in cache:
144 if len(cache) > 20:
144 if len(cache) > 20:
145 del cache[order.pop(0)]
145 del cache[order.pop(0)]
146 cache[arg] = func(arg)
146 cache[arg] = func(arg)
147 else:
147 else:
148 order.remove(arg)
148 order.remove(arg)
149 order.append(arg)
149 order.append(arg)
150 return cache[arg]
150 return cache[arg]
151 else:
151 else:
152 def f(*args):
152 def f(*args):
153 if args not in cache:
153 if args not in cache:
154 if len(cache) > 20:
154 if len(cache) > 20:
155 del cache[order.pop(0)]
155 del cache[order.pop(0)]
156 cache[args] = func(*args)
156 cache[args] = func(*args)
157 else:
157 else:
158 order.remove(args)
158 order.remove(args)
159 order.append(args)
159 order.append(args)
160 return cache[args]
160 return cache[args]
161
161
162 return f
162 return f
163
163
164 class propertycache(object):
164 class propertycache(object):
165 def __init__(self, func):
165 def __init__(self, func):
166 self.func = func
166 self.func = func
167 self.name = func.__name__
167 self.name = func.__name__
168 def __get__(self, obj, type=None):
168 def __get__(self, obj, type=None):
169 result = self.func(obj)
169 result = self.func(obj)
170 setattr(obj, self.name, result)
170 setattr(obj, self.name, result)
171 return result
171 return result
172
172
173 def pipefilter(s, cmd):
173 def pipefilter(s, cmd):
174 '''filter string S through command CMD, returning its output'''
174 '''filter string S through command CMD, returning its output'''
175 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
175 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
176 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
176 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
177 pout, perr = p.communicate(s)
177 pout, perr = p.communicate(s)
178 return pout
178 return pout
179
179
180 def tempfilter(s, cmd):
180 def tempfilter(s, cmd):
181 '''filter string S through a pair of temporary files with CMD.
181 '''filter string S through a pair of temporary files with CMD.
182 CMD is used as a template to create the real command to be run,
182 CMD is used as a template to create the real command to be run,
183 with the strings INFILE and OUTFILE replaced by the real names of
183 with the strings INFILE and OUTFILE replaced by the real names of
184 the temporary files generated.'''
184 the temporary files generated.'''
185 inname, outname = None, None
185 inname, outname = None, None
186 try:
186 try:
187 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
187 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
188 fp = os.fdopen(infd, 'wb')
188 fp = os.fdopen(infd, 'wb')
189 fp.write(s)
189 fp.write(s)
190 fp.close()
190 fp.close()
191 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
191 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
192 os.close(outfd)
192 os.close(outfd)
193 cmd = cmd.replace('INFILE', inname)
193 cmd = cmd.replace('INFILE', inname)
194 cmd = cmd.replace('OUTFILE', outname)
194 cmd = cmd.replace('OUTFILE', outname)
195 code = os.system(cmd)
195 code = os.system(cmd)
196 if sys.platform == 'OpenVMS' and code & 1:
196 if sys.platform == 'OpenVMS' and code & 1:
197 code = 0
197 code = 0
198 if code:
198 if code:
199 raise Abort(_("command '%s' failed: %s") %
199 raise Abort(_("command '%s' failed: %s") %
200 (cmd, explain_exit(code)))
200 (cmd, explain_exit(code)))
201 return open(outname, 'rb').read()
201 return open(outname, 'rb').read()
202 finally:
202 finally:
203 try:
203 try:
204 if inname:
204 if inname:
205 os.unlink(inname)
205 os.unlink(inname)
206 except:
206 except:
207 pass
207 pass
208 try:
208 try:
209 if outname:
209 if outname:
210 os.unlink(outname)
210 os.unlink(outname)
211 except:
211 except:
212 pass
212 pass
213
213
214 filtertable = {
214 filtertable = {
215 'tempfile:': tempfilter,
215 'tempfile:': tempfilter,
216 'pipe:': pipefilter,
216 'pipe:': pipefilter,
217 }
217 }
218
218
219 def filter(s, cmd):
219 def filter(s, cmd):
220 "filter a string through a command that transforms its input to its output"
220 "filter a string through a command that transforms its input to its output"
221 for name, fn in filtertable.iteritems():
221 for name, fn in filtertable.iteritems():
222 if cmd.startswith(name):
222 if cmd.startswith(name):
223 return fn(s, cmd[len(name):].lstrip())
223 return fn(s, cmd[len(name):].lstrip())
224 return pipefilter(s, cmd)
224 return pipefilter(s, cmd)
225
225
226 def binary(s):
226 def binary(s):
227 """return true if a string is binary data"""
227 """return true if a string is binary data"""
228 return bool(s and '\0' in s)
228 return bool(s and '\0' in s)
229
229
230 def increasingchunks(source, min=1024, max=65536):
230 def increasingchunks(source, min=1024, max=65536):
231 '''return no less than min bytes per chunk while data remains,
231 '''return no less than min bytes per chunk while data remains,
232 doubling min after each chunk until it reaches max'''
232 doubling min after each chunk until it reaches max'''
233 def log2(x):
233 def log2(x):
234 if not x:
234 if not x:
235 return 0
235 return 0
236 i = 0
236 i = 0
237 while x:
237 while x:
238 x >>= 1
238 x >>= 1
239 i += 1
239 i += 1
240 return i - 1
240 return i - 1
241
241
242 buf = []
242 buf = []
243 blen = 0
243 blen = 0
244 for chunk in source:
244 for chunk in source:
245 buf.append(chunk)
245 buf.append(chunk)
246 blen += len(chunk)
246 blen += len(chunk)
247 if blen >= min:
247 if blen >= min:
248 if min < max:
248 if min < max:
249 min = min << 1
249 min = min << 1
250 nmin = 1 << log2(blen)
250 nmin = 1 << log2(blen)
251 if nmin > min:
251 if nmin > min:
252 min = nmin
252 min = nmin
253 if min > max:
253 if min > max:
254 min = max
254 min = max
255 yield ''.join(buf)
255 yield ''.join(buf)
256 blen = 0
256 blen = 0
257 buf = []
257 buf = []
258 if buf:
258 if buf:
259 yield ''.join(buf)
259 yield ''.join(buf)
260
260
261 Abort = error.Abort
261 Abort = error.Abort
262
262
263 def always(fn):
263 def always(fn):
264 return True
264 return True
265
265
266 def never(fn):
266 def never(fn):
267 return False
267 return False
268
268
269 def pathto(root, n1, n2):
269 def pathto(root, n1, n2):
270 '''return the relative path from one place to another.
270 '''return the relative path from one place to another.
271 root should use os.sep to separate directories
271 root should use os.sep to separate directories
272 n1 should use os.sep to separate directories
272 n1 should use os.sep to separate directories
273 n2 should use "/" to separate directories
273 n2 should use "/" to separate directories
274 returns an os.sep-separated path.
274 returns an os.sep-separated path.
275
275
276 If n1 is a relative path, it's assumed it's
276 If n1 is a relative path, it's assumed it's
277 relative to root.
277 relative to root.
278 n2 should always be relative to root.
278 n2 should always be relative to root.
279 '''
279 '''
280 if not n1:
280 if not n1:
281 return localpath(n2)
281 return localpath(n2)
282 if os.path.isabs(n1):
282 if os.path.isabs(n1):
283 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
283 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
284 return os.path.join(root, localpath(n2))
284 return os.path.join(root, localpath(n2))
285 n2 = '/'.join((pconvert(root), n2))
285 n2 = '/'.join((pconvert(root), n2))
286 a, b = splitpath(n1), n2.split('/')
286 a, b = splitpath(n1), n2.split('/')
287 a.reverse()
287 a.reverse()
288 b.reverse()
288 b.reverse()
289 while a and b and a[-1] == b[-1]:
289 while a and b and a[-1] == b[-1]:
290 a.pop()
290 a.pop()
291 b.pop()
291 b.pop()
292 b.reverse()
292 b.reverse()
293 return os.sep.join((['..'] * len(a)) + b) or '.'
293 return os.sep.join((['..'] * len(a)) + b) or '.'
294
294
295 def canonpath(root, cwd, myname, auditor=None):
295 def canonpath(root, cwd, myname, auditor=None):
296 """return the canonical path of myname, given cwd and root"""
296 """return the canonical path of myname, given cwd and root"""
297 if endswithsep(root):
297 if endswithsep(root):
298 rootsep = root
298 rootsep = root
299 else:
299 else:
300 rootsep = root + os.sep
300 rootsep = root + os.sep
301 name = myname
301 name = myname
302 if not os.path.isabs(name):
302 if not os.path.isabs(name):
303 name = os.path.join(root, cwd, name)
303 name = os.path.join(root, cwd, name)
304 name = os.path.normpath(name)
304 name = os.path.normpath(name)
305 if auditor is None:
305 if auditor is None:
306 auditor = path_auditor(root)
306 auditor = path_auditor(root)
307 if name != rootsep and name.startswith(rootsep):
307 if name != rootsep and name.startswith(rootsep):
308 name = name[len(rootsep):]
308 name = name[len(rootsep):]
309 auditor(name)
309 auditor(name)
310 return pconvert(name)
310 return pconvert(name)
311 elif name == root:
311 elif name == root:
312 return ''
312 return ''
313 else:
313 else:
314 # Determine whether `name' is in the hierarchy at or beneath `root',
314 # Determine whether `name' is in the hierarchy at or beneath `root',
315 # by iterating name=dirname(name) until that causes no change (can't
315 # by iterating name=dirname(name) until that causes no change (can't
316 # check name == '/', because that doesn't work on windows). For each
316 # check name == '/', because that doesn't work on windows). For each
317 # `name', compare dev/inode numbers. If they match, the list `rel'
317 # `name', compare dev/inode numbers. If they match, the list `rel'
318 # holds the reversed list of components making up the relative file
318 # holds the reversed list of components making up the relative file
319 # name we want.
319 # name we want.
320 root_st = os.stat(root)
320 root_st = os.stat(root)
321 rel = []
321 rel = []
322 while True:
322 while True:
323 try:
323 try:
324 name_st = os.stat(name)
324 name_st = os.stat(name)
325 except OSError:
325 except OSError:
326 break
326 break
327 if samestat(name_st, root_st):
327 if samestat(name_st, root_st):
328 if not rel:
328 if not rel:
329 # name was actually the same as root (maybe a symlink)
329 # name was actually the same as root (maybe a symlink)
330 return ''
330 return ''
331 rel.reverse()
331 rel.reverse()
332 name = os.path.join(*rel)
332 name = os.path.join(*rel)
333 auditor(name)
333 auditor(name)
334 return pconvert(name)
334 return pconvert(name)
335 dirname, basename = os.path.split(name)
335 dirname, basename = os.path.split(name)
336 rel.append(basename)
336 rel.append(basename)
337 if dirname == name:
337 if dirname == name:
338 break
338 break
339 name = dirname
339 name = dirname
340
340
341 raise Abort('%s not under root' % myname)
341 raise Abort('%s not under root' % myname)
342
342
343 _hgexecutable = None
343 _hgexecutable = None
344
344
345 def main_is_frozen():
345 def main_is_frozen():
346 """return True if we are a frozen executable.
346 """return True if we are a frozen executable.
347
347
348 The code supports py2exe (most common, Windows only) and tools/freeze
348 The code supports py2exe (most common, Windows only) and tools/freeze
349 (portable, not much used).
349 (portable, not much used).
350 """
350 """
351 return (hasattr(sys, "frozen") or # new py2exe
351 return (hasattr(sys, "frozen") or # new py2exe
352 hasattr(sys, "importers") or # old py2exe
352 hasattr(sys, "importers") or # old py2exe
353 imp.is_frozen("__main__")) # tools/freeze
353 imp.is_frozen("__main__")) # tools/freeze
354
354
355 def hgexecutable():
355 def hgexecutable():
356 """return location of the 'hg' executable.
356 """return location of the 'hg' executable.
357
357
358 Defaults to $HG or 'hg' in the search path.
358 Defaults to $HG or 'hg' in the search path.
359 """
359 """
360 if _hgexecutable is None:
360 if _hgexecutable is None:
361 hg = os.environ.get('HG')
361 hg = os.environ.get('HG')
362 if hg:
362 if hg:
363 set_hgexecutable(hg)
363 set_hgexecutable(hg)
364 elif main_is_frozen():
364 elif main_is_frozen():
365 set_hgexecutable(sys.executable)
365 set_hgexecutable(sys.executable)
366 else:
366 else:
367 exe = find_exe('hg') or os.path.basename(sys.argv[0])
367 exe = find_exe('hg') or os.path.basename(sys.argv[0])
368 set_hgexecutable(exe)
368 set_hgexecutable(exe)
369 return _hgexecutable
369 return _hgexecutable
370
370
371 def set_hgexecutable(path):
371 def set_hgexecutable(path):
372 """set location of the 'hg' executable"""
372 """set location of the 'hg' executable"""
373 global _hgexecutable
373 global _hgexecutable
374 _hgexecutable = path
374 _hgexecutable = path
375
375
376 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None, out=None):
376 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None, out=None):
377 '''enhanced shell command execution.
377 '''enhanced shell command execution.
378 run with environment maybe modified, maybe in different dir.
378 run with environment maybe modified, maybe in different dir.
379
379
380 if command fails and onerr is None, return status. if ui object,
380 if command fails and onerr is None, return status. if ui object,
381 print error message and return status, else raise onerr object as
381 print error message and return status, else raise onerr object as
382 exception.
382 exception.
383
383
384 if out is specified, it is assumed to be a file-like object that has a
384 if out is specified, it is assumed to be a file-like object that has a
385 write() method. stdout and stderr will be redirected to out.'''
385 write() method. stdout and stderr will be redirected to out.'''
386 def py2shell(val):
386 def py2shell(val):
387 'convert python object into string that is useful to shell'
387 'convert python object into string that is useful to shell'
388 if val is None or val is False:
388 if val is None or val is False:
389 return '0'
389 return '0'
390 if val is True:
390 if val is True:
391 return '1'
391 return '1'
392 return str(val)
392 return str(val)
393 origcmd = cmd
393 origcmd = cmd
394 cmd = quotecommand(cmd)
394 cmd = quotecommand(cmd)
395 env = dict(os.environ)
395 env = dict(os.environ)
396 env.update((k, py2shell(v)) for k, v in environ.iteritems())
396 env.update((k, py2shell(v)) for k, v in environ.iteritems())
397 env['HG'] = hgexecutable()
397 env['HG'] = hgexecutable()
398 if out is None:
398 if out is None:
399 rc = subprocess.call(cmd, shell=True, close_fds=closefds,
399 rc = subprocess.call(cmd, shell=True, close_fds=closefds,
400 env=env, cwd=cwd)
400 env=env, cwd=cwd)
401 else:
401 else:
402 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds,
402 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds,
403 env=env, cwd=cwd, stdout=subprocess.PIPE,
403 env=env, cwd=cwd, stdout=subprocess.PIPE,
404 stderr=subprocess.STDOUT)
404 stderr=subprocess.STDOUT)
405 for line in proc.stdout:
405 for line in proc.stdout:
406 out.write(line)
406 out.write(line)
407 proc.wait()
407 proc.wait()
408 rc = proc.returncode
408 rc = proc.returncode
409 if sys.platform == 'OpenVMS' and rc & 1:
409 if sys.platform == 'OpenVMS' and rc & 1:
410 rc = 0
410 rc = 0
411 if rc and onerr:
411 if rc and onerr:
412 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
412 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
413 explain_exit(rc)[0])
413 explain_exit(rc)[0])
414 if errprefix:
414 if errprefix:
415 errmsg = '%s: %s' % (errprefix, errmsg)
415 errmsg = '%s: %s' % (errprefix, errmsg)
416 try:
416 try:
417 onerr.warn(errmsg + '\n')
417 onerr.warn(errmsg + '\n')
418 except AttributeError:
418 except AttributeError:
419 raise onerr(errmsg)
419 raise onerr(errmsg)
420 return rc
420 return rc
421
421
422 def checksignature(func):
422 def checksignature(func):
423 '''wrap a function with code to check for calling errors'''
423 '''wrap a function with code to check for calling errors'''
424 def check(*args, **kwargs):
424 def check(*args, **kwargs):
425 try:
425 try:
426 return func(*args, **kwargs)
426 return func(*args, **kwargs)
427 except TypeError:
427 except TypeError:
428 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
428 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
429 raise error.SignatureError
429 raise error.SignatureError
430 raise
430 raise
431
431
432 return check
432 return check
433
433
434 def unlink(f):
434 def unlink(f):
435 """unlink and remove the directory if it is empty"""
435 """unlink and remove the directory if it is empty"""
436 os.unlink(f)
436 os.unlink(f)
437 # try removing directories that might now be empty
437 # try removing directories that might now be empty
438 try:
438 try:
439 os.removedirs(os.path.dirname(f))
439 os.removedirs(os.path.dirname(f))
440 except OSError:
440 except OSError:
441 pass
441 pass
442
442
443 def copyfile(src, dest):
443 def copyfile(src, dest):
444 "copy a file, preserving mode and atime/mtime"
444 "copy a file, preserving mode and atime/mtime"
445 if os.path.islink(src):
445 if os.path.islink(src):
446 try:
446 try:
447 os.unlink(dest)
447 os.unlink(dest)
448 except:
448 except:
449 pass
449 pass
450 os.symlink(os.readlink(src), dest)
450 os.symlink(os.readlink(src), dest)
451 else:
451 else:
452 try:
452 try:
453 shutil.copyfile(src, dest)
453 shutil.copyfile(src, dest)
454 shutil.copymode(src, dest)
454 shutil.copymode(src, dest)
455 except shutil.Error, inst:
455 except shutil.Error, inst:
456 raise Abort(str(inst))
456 raise Abort(str(inst))
457
457
458 def copyfiles(src, dst, hardlink=None):
458 def copyfiles(src, dst, hardlink=None):
459 """Copy a directory tree using hardlinks if possible"""
459 """Copy a directory tree using hardlinks if possible"""
460
460
461 if hardlink is None:
461 if hardlink is None:
462 hardlink = (os.stat(src).st_dev ==
462 hardlink = (os.stat(src).st_dev ==
463 os.stat(os.path.dirname(dst)).st_dev)
463 os.stat(os.path.dirname(dst)).st_dev)
464
464
465 num = 0
465 num = 0
466 if os.path.isdir(src):
466 if os.path.isdir(src):
467 os.mkdir(dst)
467 os.mkdir(dst)
468 for name, kind in osutil.listdir(src):
468 for name, kind in osutil.listdir(src):
469 srcname = os.path.join(src, name)
469 srcname = os.path.join(src, name)
470 dstname = os.path.join(dst, name)
470 dstname = os.path.join(dst, name)
471 hardlink, n = copyfiles(srcname, dstname, hardlink)
471 hardlink, n = copyfiles(srcname, dstname, hardlink)
472 num += n
472 num += n
473 else:
473 else:
474 if hardlink:
474 if hardlink:
475 try:
475 try:
476 os_link(src, dst)
476 os_link(src, dst)
477 except (IOError, OSError):
477 except (IOError, OSError):
478 hardlink = False
478 hardlink = False
479 shutil.copy(src, dst)
479 shutil.copy(src, dst)
480 else:
480 else:
481 shutil.copy(src, dst)
481 shutil.copy(src, dst)
482 num += 1
482 num += 1
483
483
484 return hardlink, num
484 return hardlink, num
485
485
486 class path_auditor(object):
486 class path_auditor(object):
487 '''ensure that a filesystem path contains no banned components.
487 '''ensure that a filesystem path contains no banned components.
488 the following properties of a path are checked:
488 the following properties of a path are checked:
489
489
490 - ends with a directory separator
490 - ends with a directory separator
491 - under top-level .hg
491 - under top-level .hg
492 - starts at the root of a windows drive
492 - starts at the root of a windows drive
493 - contains ".."
493 - contains ".."
494 - traverses a symlink (e.g. a/symlink_here/b)
494 - traverses a symlink (e.g. a/symlink_here/b)
495 - inside a nested repository (a callback can be used to approve
495 - inside a nested repository (a callback can be used to approve
496 some nested repositories, e.g., subrepositories)
496 some nested repositories, e.g., subrepositories)
497 '''
497 '''
498
498
499 def __init__(self, root, callback=None):
499 def __init__(self, root, callback=None):
500 self.audited = set()
500 self.audited = set()
501 self.auditeddir = set()
501 self.auditeddir = set()
502 self.root = root
502 self.root = root
503 self.callback = callback
503 self.callback = callback
504
504
505 def __call__(self, path):
505 def __call__(self, path):
506 if path in self.audited:
506 if path in self.audited:
507 return
507 return
508 # AIX ignores "/" at end of path, others raise EISDIR.
508 # AIX ignores "/" at end of path, others raise EISDIR.
509 if endswithsep(path):
509 if endswithsep(path):
510 raise Abort(_("path ends in directory separator: %s") % path)
510 raise Abort(_("path ends in directory separator: %s") % path)
511 normpath = os.path.normcase(path)
511 normpath = os.path.normcase(path)
512 parts = splitpath(normpath)
512 parts = splitpath(normpath)
513 if (os.path.splitdrive(path)[0]
513 if (os.path.splitdrive(path)[0]
514 or parts[0].lower() in ('.hg', '.hg.', '')
514 or parts[0].lower() in ('.hg', '.hg.', '')
515 or os.pardir in parts):
515 or os.pardir in parts):
516 raise Abort(_("path contains illegal component: %s") % path)
516 raise Abort(_("path contains illegal component: %s") % path)
517 if '.hg' in path.lower():
517 if '.hg' in path.lower():
518 lparts = [p.lower() for p in parts]
518 lparts = [p.lower() for p in parts]
519 for p in '.hg', '.hg.':
519 for p in '.hg', '.hg.':
520 if p in lparts[1:]:
520 if p in lparts[1:]:
521 pos = lparts.index(p)
521 pos = lparts.index(p)
522 base = os.path.join(*parts[:pos])
522 base = os.path.join(*parts[:pos])
523 raise Abort(_('path %r is inside repo %r') % (path, base))
523 raise Abort(_('path %r is inside repo %r') % (path, base))
524 def check(prefix):
524 def check(prefix):
525 curpath = os.path.join(self.root, prefix)
525 curpath = os.path.join(self.root, prefix)
526 try:
526 try:
527 st = os.lstat(curpath)
527 st = os.lstat(curpath)
528 except OSError, err:
528 except OSError, err:
529 # EINVAL can be raised as invalid path syntax under win32.
529 # EINVAL can be raised as invalid path syntax under win32.
530 # They must be ignored for patterns can be checked too.
530 # They must be ignored for patterns can be checked too.
531 if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL):
531 if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL):
532 raise
532 raise
533 else:
533 else:
534 if stat.S_ISLNK(st.st_mode):
534 if stat.S_ISLNK(st.st_mode):
535 raise Abort(_('path %r traverses symbolic link %r') %
535 raise Abort(_('path %r traverses symbolic link %r') %
536 (path, prefix))
536 (path, prefix))
537 elif (stat.S_ISDIR(st.st_mode) and
537 elif (stat.S_ISDIR(st.st_mode) and
538 os.path.isdir(os.path.join(curpath, '.hg'))):
538 os.path.isdir(os.path.join(curpath, '.hg'))):
539 if not self.callback or not self.callback(curpath):
539 if not self.callback or not self.callback(curpath):
540 raise Abort(_('path %r is inside repo %r') %
540 raise Abort(_('path %r is inside repo %r') %
541 (path, prefix))
541 (path, prefix))
542 parts.pop()
542 parts.pop()
543 prefixes = []
543 prefixes = []
544 while parts:
544 while parts:
545 prefix = os.sep.join(parts)
545 prefix = os.sep.join(parts)
546 if prefix in self.auditeddir:
546 if prefix in self.auditeddir:
547 break
547 break
548 check(prefix)
548 check(prefix)
549 prefixes.append(prefix)
549 prefixes.append(prefix)
550 parts.pop()
550 parts.pop()
551
551
552 self.audited.add(path)
552 self.audited.add(path)
553 # only add prefixes to the cache after checking everything: we don't
553 # only add prefixes to the cache after checking everything: we don't
554 # want to add "foo/bar/baz" before checking if there's a "foo/.hg"
554 # want to add "foo/bar/baz" before checking if there's a "foo/.hg"
555 self.auditeddir.update(prefixes)
555 self.auditeddir.update(prefixes)
556
556
557 def nlinks(pathname):
557 def nlinks(pathname):
558 """Return number of hardlinks for the given file."""
558 """Return number of hardlinks for the given file."""
559 return os.lstat(pathname).st_nlink
559 return os.lstat(pathname).st_nlink
560
560
561 if hasattr(os, 'link'):
561 if hasattr(os, 'link'):
562 os_link = os.link
562 os_link = os.link
563 else:
563 else:
564 def os_link(src, dst):
564 def os_link(src, dst):
565 raise OSError(0, _("Hardlinks not supported"))
565 raise OSError(0, _("Hardlinks not supported"))
566
566
567 def lookup_reg(key, name=None, scope=None):
567 def lookup_reg(key, name=None, scope=None):
568 return None
568 return None
569
569
570 def hidewindow():
570 def hidewindow():
571 """Hide current shell window.
571 """Hide current shell window.
572
572
573 Used to hide the window opened when starting asynchronous
573 Used to hide the window opened when starting asynchronous
574 child process under Windows, unneeded on other systems.
574 child process under Windows, unneeded on other systems.
575 """
575 """
576 pass
576 pass
577
577
578 if os.name == 'nt':
578 if os.name == 'nt':
579 from windows import *
579 from windows import *
580 else:
580 else:
581 from posix import *
581 from posix import *
582
582
583 def makelock(info, pathname):
583 def makelock(info, pathname):
584 try:
584 try:
585 return os.symlink(info, pathname)
585 return os.symlink(info, pathname)
586 except OSError, why:
586 except OSError, why:
587 if why.errno == errno.EEXIST:
587 if why.errno == errno.EEXIST:
588 raise
588 raise
589 except AttributeError: # no symlink in os
589 except AttributeError: # no symlink in os
590 pass
590 pass
591
591
592 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
592 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
593 os.write(ld, info)
593 os.write(ld, info)
594 os.close(ld)
594 os.close(ld)
595
595
596 def readlock(pathname):
596 def readlock(pathname):
597 try:
597 try:
598 return os.readlink(pathname)
598 return os.readlink(pathname)
599 except OSError, why:
599 except OSError, why:
600 if why.errno not in (errno.EINVAL, errno.ENOSYS):
600 if why.errno not in (errno.EINVAL, errno.ENOSYS):
601 raise
601 raise
602 except AttributeError: # no symlink in os
602 except AttributeError: # no symlink in os
603 pass
603 pass
604 return posixfile(pathname).read()
604 return posixfile(pathname).read()
605
605
606 def fstat(fp):
606 def fstat(fp):
607 '''stat file object that may not have fileno method.'''
607 '''stat file object that may not have fileno method.'''
608 try:
608 try:
609 return os.fstat(fp.fileno())
609 return os.fstat(fp.fileno())
610 except AttributeError:
610 except AttributeError:
611 return os.stat(fp.name)
611 return os.stat(fp.name)
612
612
613 # File system features
613 # File system features
614
614
615 def checkcase(path):
615 def checkcase(path):
616 """
616 """
617 Check whether the given path is on a case-sensitive filesystem
617 Check whether the given path is on a case-sensitive filesystem
618
618
619 Requires a path (like /foo/.hg) ending with a foldable final
619 Requires a path (like /foo/.hg) ending with a foldable final
620 directory component.
620 directory component.
621 """
621 """
622 s1 = os.stat(path)
622 s1 = os.stat(path)
623 d, b = os.path.split(path)
623 d, b = os.path.split(path)
624 p2 = os.path.join(d, b.upper())
624 p2 = os.path.join(d, b.upper())
625 if path == p2:
625 if path == p2:
626 p2 = os.path.join(d, b.lower())
626 p2 = os.path.join(d, b.lower())
627 try:
627 try:
628 s2 = os.stat(p2)
628 s2 = os.stat(p2)
629 if s2 == s1:
629 if s2 == s1:
630 return False
630 return False
631 return True
631 return True
632 except:
632 except:
633 return True
633 return True
634
634
635 _fspathcache = {}
635 _fspathcache = {}
636 def fspath(name, root):
636 def fspath(name, root):
637 '''Get name in the case stored in the filesystem
637 '''Get name in the case stored in the filesystem
638
638
639 The name is either relative to root, or it is an absolute path starting
639 The name is either relative to root, or it is an absolute path starting
640 with root. Note that this function is unnecessary, and should not be
640 with root. Note that this function is unnecessary, and should not be
641 called, for case-sensitive filesystems (simply because it's expensive).
641 called, for case-sensitive filesystems (simply because it's expensive).
642 '''
642 '''
643 # If name is absolute, make it relative
643 # If name is absolute, make it relative
644 if name.lower().startswith(root.lower()):
644 if name.lower().startswith(root.lower()):
645 l = len(root)
645 l = len(root)
646 if name[l] == os.sep or name[l] == os.altsep:
646 if name[l] == os.sep or name[l] == os.altsep:
647 l = l + 1
647 l = l + 1
648 name = name[l:]
648 name = name[l:]
649
649
650 if not os.path.lexists(os.path.join(root, name)):
650 if not os.path.lexists(os.path.join(root, name)):
651 return None
651 return None
652
652
653 seps = os.sep
653 seps = os.sep
654 if os.altsep:
654 if os.altsep:
655 seps = seps + os.altsep
655 seps = seps + os.altsep
656 # Protect backslashes. This gets silly very quickly.
656 # Protect backslashes. This gets silly very quickly.
657 seps.replace('\\','\\\\')
657 seps.replace('\\','\\\\')
658 pattern = re.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
658 pattern = re.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
659 dir = os.path.normcase(os.path.normpath(root))
659 dir = os.path.normcase(os.path.normpath(root))
660 result = []
660 result = []
661 for part, sep in pattern.findall(name):
661 for part, sep in pattern.findall(name):
662 if sep:
662 if sep:
663 result.append(sep)
663 result.append(sep)
664 continue
664 continue
665
665
666 if dir not in _fspathcache:
666 if dir not in _fspathcache:
667 _fspathcache[dir] = os.listdir(dir)
667 _fspathcache[dir] = os.listdir(dir)
668 contents = _fspathcache[dir]
668 contents = _fspathcache[dir]
669
669
670 lpart = part.lower()
670 lpart = part.lower()
671 lenp = len(part)
671 lenp = len(part)
672 for n in contents:
672 for n in contents:
673 if lenp == len(n) and n.lower() == lpart:
673 if lenp == len(n) and n.lower() == lpart:
674 result.append(n)
674 result.append(n)
675 break
675 break
676 else:
676 else:
677 # Cannot happen, as the file exists!
677 # Cannot happen, as the file exists!
678 result.append(part)
678 result.append(part)
679 dir = os.path.join(dir, lpart)
679 dir = os.path.join(dir, lpart)
680
680
681 return ''.join(result)
681 return ''.join(result)
682
682
683 def checkexec(path):
683 def checkexec(path):
684 """
684 """
685 Check whether the given path is on a filesystem with UNIX-like exec flags
685 Check whether the given path is on a filesystem with UNIX-like exec flags
686
686
687 Requires a directory (like /foo/.hg)
687 Requires a directory (like /foo/.hg)
688 """
688 """
689
689
690 # VFAT on some Linux versions can flip mode but it doesn't persist
690 # VFAT on some Linux versions can flip mode but it doesn't persist
691 # a FS remount. Frequently we can detect it if files are created
691 # a FS remount. Frequently we can detect it if files are created
692 # with exec bit on.
692 # with exec bit on.
693
693
694 try:
694 try:
695 EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
695 EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
696 fh, fn = tempfile.mkstemp(dir=path, prefix='hg-checkexec-')
696 fh, fn = tempfile.mkstemp(dir=path, prefix='hg-checkexec-')
697 try:
697 try:
698 os.close(fh)
698 os.close(fh)
699 m = os.stat(fn).st_mode & 0777
699 m = os.stat(fn).st_mode & 0777
700 new_file_has_exec = m & EXECFLAGS
700 new_file_has_exec = m & EXECFLAGS
701 os.chmod(fn, m ^ EXECFLAGS)
701 os.chmod(fn, m ^ EXECFLAGS)
702 exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m)
702 exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0777) == m)
703 finally:
703 finally:
704 os.unlink(fn)
704 os.unlink(fn)
705 except (IOError, OSError):
705 except (IOError, OSError):
706 # we don't care, the user probably won't be able to commit anyway
706 # we don't care, the user probably won't be able to commit anyway
707 return False
707 return False
708 return not (new_file_has_exec or exec_flags_cannot_flip)
708 return not (new_file_has_exec or exec_flags_cannot_flip)
709
709
710 def checklink(path):
710 def checklink(path):
711 """check whether the given path is on a symlink-capable filesystem"""
711 """check whether the given path is on a symlink-capable filesystem"""
712 # mktemp is not racy because symlink creation will fail if the
712 # mktemp is not racy because symlink creation will fail if the
713 # file already exists
713 # file already exists
714 name = tempfile.mktemp(dir=path, prefix='hg-checklink-')
714 name = tempfile.mktemp(dir=path, prefix='hg-checklink-')
715 try:
715 try:
716 os.symlink(".", name)
716 os.symlink(".", name)
717 os.unlink(name)
717 os.unlink(name)
718 return True
718 return True
719 except (OSError, AttributeError):
719 except (OSError, AttributeError):
720 return False
720 return False
721
721
722 def checknlink(testfile):
722 def checknlink(testfile):
723 '''check whether hardlink count reporting works properly'''
723 '''check whether hardlink count reporting works properly'''
724 f = testfile + ".hgtmp"
724 f = testfile + ".hgtmp"
725
725
726 try:
726 try:
727 os_link(testfile, f)
727 os_link(testfile, f)
728 except OSError:
728 except OSError:
729 return False
729 return False
730
730
731 try:
731 try:
732 # nlinks() may behave differently for files on Windows shares if
732 # nlinks() may behave differently for files on Windows shares if
733 # the file is open.
733 # the file is open.
734 fd = open(f)
734 fd = open(f)
735 return nlinks(f) > 1
735 return nlinks(f) > 1
736 finally:
736 finally:
737 fd.close()
737 fd.close()
738 os.unlink(f)
738 os.unlink(f)
739
739
740 return False
740 return False
741
741
742 def endswithsep(path):
742 def endswithsep(path):
743 '''Check path ends with os.sep or os.altsep.'''
743 '''Check path ends with os.sep or os.altsep.'''
744 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
744 return path.endswith(os.sep) or os.altsep and path.endswith(os.altsep)
745
745
746 def splitpath(path):
746 def splitpath(path):
747 '''Split path by os.sep.
747 '''Split path by os.sep.
748 Note that this function does not use os.altsep because this is
748 Note that this function does not use os.altsep because this is
749 an alternative of simple "xxx.split(os.sep)".
749 an alternative of simple "xxx.split(os.sep)".
750 It is recommended to use os.path.normpath() before using this
750 It is recommended to use os.path.normpath() before using this
751 function if need.'''
751 function if need.'''
752 return path.split(os.sep)
752 return path.split(os.sep)
753
753
754 def gui():
754 def gui():
755 '''Are we running in a GUI?'''
755 '''Are we running in a GUI?'''
756 return os.name == "nt" or os.name == "mac" or os.environ.get("DISPLAY")
756 return os.name == "nt" or os.name == "mac" or os.environ.get("DISPLAY")
757
757
758 def mktempcopy(name, emptyok=False, createmode=None):
758 def mktempcopy(name, emptyok=False, createmode=None):
759 """Create a temporary file with the same contents from name
759 """Create a temporary file with the same contents from name
760
760
761 The permission bits are copied from the original file.
761 The permission bits are copied from the original file.
762
762
763 If the temporary file is going to be truncated immediately, you
763 If the temporary file is going to be truncated immediately, you
764 can use emptyok=True as an optimization.
764 can use emptyok=True as an optimization.
765
765
766 Returns the name of the temporary file.
766 Returns the name of the temporary file.
767 """
767 """
768 d, fn = os.path.split(name)
768 d, fn = os.path.split(name)
769 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
769 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
770 os.close(fd)
770 os.close(fd)
771 # Temporary files are created with mode 0600, which is usually not
771 # Temporary files are created with mode 0600, which is usually not
772 # what we want. If the original file already exists, just copy
772 # what we want. If the original file already exists, just copy
773 # its mode. Otherwise, manually obey umask.
773 # its mode. Otherwise, manually obey umask.
774 try:
774 try:
775 st_mode = os.lstat(name).st_mode & 0777
775 st_mode = os.lstat(name).st_mode & 0777
776 except OSError, inst:
776 except OSError, inst:
777 if inst.errno != errno.ENOENT:
777 if inst.errno != errno.ENOENT:
778 raise
778 raise
779 st_mode = createmode
779 st_mode = createmode
780 if st_mode is None:
780 if st_mode is None:
781 st_mode = ~umask
781 st_mode = ~umask
782 st_mode &= 0666
782 st_mode &= 0666
783 os.chmod(temp, st_mode)
783 os.chmod(temp, st_mode)
784 if emptyok:
784 if emptyok:
785 return temp
785 return temp
786 try:
786 try:
787 try:
787 try:
788 ifp = posixfile(name, "rb")
788 ifp = posixfile(name, "rb")
789 except IOError, inst:
789 except IOError, inst:
790 if inst.errno == errno.ENOENT:
790 if inst.errno == errno.ENOENT:
791 return temp
791 return temp
792 if not getattr(inst, 'filename', None):
792 if not getattr(inst, 'filename', None):
793 inst.filename = name
793 inst.filename = name
794 raise
794 raise
795 ofp = posixfile(temp, "wb")
795 ofp = posixfile(temp, "wb")
796 for chunk in filechunkiter(ifp):
796 for chunk in filechunkiter(ifp):
797 ofp.write(chunk)
797 ofp.write(chunk)
798 ifp.close()
798 ifp.close()
799 ofp.close()
799 ofp.close()
800 except:
800 except:
801 try: os.unlink(temp)
801 try: os.unlink(temp)
802 except: pass
802 except: pass
803 raise
803 raise
804 return temp
804 return temp
805
805
806 class atomictempfile(object):
806 class atomictempfile(object):
807 """file-like object that atomically updates a file
807 """file-like object that atomically updates a file
808
808
809 All writes will be redirected to a temporary copy of the original
809 All writes will be redirected to a temporary copy of the original
810 file. When rename is called, the copy is renamed to the original
810 file. When rename is called, the copy is renamed to the original
811 name, making the changes visible.
811 name, making the changes visible.
812 """
812 """
813 def __init__(self, name, mode='w+b', createmode=None):
813 def __init__(self, name, mode='w+b', createmode=None):
814 self.__name = name
814 self.__name = name
815 self._fp = None
815 self._fp = None
816 self.temp = mktempcopy(name, emptyok=('w' in mode),
816 self.temp = mktempcopy(name, emptyok=('w' in mode),
817 createmode=createmode)
817 createmode=createmode)
818 self._fp = posixfile(self.temp, mode)
818 self._fp = posixfile(self.temp, mode)
819
819
820 def __getattr__(self, name):
820 def __getattr__(self, name):
821 return getattr(self._fp, name)
821 return getattr(self._fp, name)
822
822
823 def rename(self):
823 def rename(self):
824 if not self._fp.closed:
824 if not self._fp.closed:
825 self._fp.close()
825 self._fp.close()
826 rename(self.temp, localpath(self.__name))
826 rename(self.temp, localpath(self.__name))
827
827
828 def close(self):
828 def close(self):
829 if not self._fp:
829 if not self._fp:
830 return
830 return
831 if not self._fp.closed:
831 if not self._fp.closed:
832 try:
832 try:
833 os.unlink(self.temp)
833 os.unlink(self.temp)
834 except: pass
834 except: pass
835 self._fp.close()
835 self._fp.close()
836
836
837 def __del__(self):
837 def __del__(self):
838 self.close()
838 self.close()
839
839
840 def makedirs(name, mode=None):
840 def makedirs(name, mode=None):
841 """recursive directory creation with parent mode inheritance"""
841 """recursive directory creation with parent mode inheritance"""
842 parent = os.path.abspath(os.path.dirname(name))
842 parent = os.path.abspath(os.path.dirname(name))
843 try:
843 try:
844 os.mkdir(name)
844 os.mkdir(name)
845 if mode is not None:
845 if mode is not None:
846 os.chmod(name, mode)
846 os.chmod(name, mode)
847 return
847 return
848 except OSError, err:
848 except OSError, err:
849 if err.errno == errno.EEXIST:
849 if err.errno == errno.EEXIST:
850 return
850 return
851 if not name or parent == name or err.errno != errno.ENOENT:
851 if not name or parent == name or err.errno != errno.ENOENT:
852 raise
852 raise
853 makedirs(parent, mode)
853 makedirs(parent, mode)
854 makedirs(name, mode)
854 makedirs(name, mode)
855
855
856 class opener(object):
856 class opener(object):
857 """Open files relative to a base directory
857 """Open files relative to a base directory
858
858
859 This class is used to hide the details of COW semantics and
859 This class is used to hide the details of COW semantics and
860 remote file access from higher level code.
860 remote file access from higher level code.
861 """
861 """
862 def __init__(self, base, audit=True):
862 def __init__(self, base, audit=True):
863 self.base = base
863 self.base = base
864 if audit:
864 if audit:
865 self.auditor = path_auditor(base)
865 self.auditor = path_auditor(base)
866 else:
866 else:
867 self.auditor = always
867 self.auditor = always
868 self.createmode = None
868 self.createmode = None
869 self._trustnlink = None
869 self._trustnlink = None
870
870
871 @propertycache
871 @propertycache
872 def _can_symlink(self):
872 def _can_symlink(self):
873 return checklink(self.base)
873 return checklink(self.base)
874
874
875 def _fixfilemode(self, name):
875 def _fixfilemode(self, name):
876 if self.createmode is None:
876 if self.createmode is None:
877 return
877 return
878 os.chmod(name, self.createmode & 0666)
878 os.chmod(name, self.createmode & 0666)
879
879
880 def __call__(self, path, mode="r", text=False, atomictemp=False):
880 def __call__(self, path, mode="r", text=False, atomictemp=False):
881 self.auditor(path)
881 self.auditor(path)
882 f = os.path.join(self.base, path)
882 f = os.path.join(self.base, path)
883
883
884 if not text and "b" not in mode:
884 if not text and "b" not in mode:
885 mode += "b" # for that other OS
885 mode += "b" # for that other OS
886
886
887 nlink = -1
887 nlink = -1
888 dirname, basename = os.path.split(f)
888 dirname, basename = os.path.split(f)
889 # If basename is empty, then the path is malformed because it points
889 # If basename is empty, then the path is malformed because it points
890 # to a directory. Let the posixfile() call below raise IOError.
890 # to a directory. Let the posixfile() call below raise IOError.
891 if basename and mode not in ('r', 'rb'):
891 if basename and mode not in ('r', 'rb'):
892 if atomictemp:
892 if atomictemp:
893 if not os.path.isdir(dirname):
893 if not os.path.isdir(dirname):
894 makedirs(dirname, self.createmode)
894 makedirs(dirname, self.createmode)
895 return atomictempfile(f, mode, self.createmode)
895 return atomictempfile(f, mode, self.createmode)
896 try:
896 try:
897 if 'w' in mode:
897 if 'w' in mode:
898 os.unlink(f)
898 os.unlink(f)
899 nlink = 0
899 nlink = 0
900 else:
900 else:
901 # nlinks() may behave differently for files on Windows
901 # nlinks() may behave differently for files on Windows
902 # shares if the file is open.
902 # shares if the file is open.
903 fd = open(f)
903 fd = open(f)
904 nlink = nlinks(f)
904 nlink = nlinks(f)
905 fd.close()
905 fd.close()
906 except (OSError, IOError):
906 except (OSError, IOError):
907 nlink = 0
907 nlink = 0
908 if not os.path.isdir(dirname):
908 if not os.path.isdir(dirname):
909 makedirs(dirname, self.createmode)
909 makedirs(dirname, self.createmode)
910 if nlink > 0:
910 if nlink > 0:
911 if self._trustnlink is None:
911 if self._trustnlink is None:
912 self._trustnlink = nlink > 1 or checknlink(f)
912 self._trustnlink = nlink > 1 or checknlink(f)
913 if nlink > 1 or not self._trustnlink:
913 if nlink > 1 or not self._trustnlink:
914 rename(mktempcopy(f), f)
914 rename(mktempcopy(f), f)
915 fp = posixfile(f, mode)
915 fp = posixfile(f, mode)
916 if nlink == 0:
916 if nlink == 0:
917 self._fixfilemode(f)
917 self._fixfilemode(f)
918 return fp
918 return fp
919
919
920 def symlink(self, src, dst):
920 def symlink(self, src, dst):
921 self.auditor(dst)
921 self.auditor(dst)
922 linkname = os.path.join(self.base, dst)
922 linkname = os.path.join(self.base, dst)
923 try:
923 try:
924 os.unlink(linkname)
924 os.unlink(linkname)
925 except OSError:
925 except OSError:
926 pass
926 pass
927
927
928 dirname = os.path.dirname(linkname)
928 dirname = os.path.dirname(linkname)
929 if not os.path.exists(dirname):
929 if not os.path.exists(dirname):
930 makedirs(dirname, self.createmode)
930 makedirs(dirname, self.createmode)
931
931
932 if self._can_symlink:
932 if self._can_symlink:
933 try:
933 try:
934 os.symlink(src, linkname)
934 os.symlink(src, linkname)
935 except OSError, err:
935 except OSError, err:
936 raise OSError(err.errno, _('could not symlink to %r: %s') %
936 raise OSError(err.errno, _('could not symlink to %r: %s') %
937 (src, err.strerror), linkname)
937 (src, err.strerror), linkname)
938 else:
938 else:
939 f = self(dst, "w")
939 f = self(dst, "w")
940 f.write(src)
940 f.write(src)
941 f.close()
941 f.close()
942 self._fixfilemode(dst)
942 self._fixfilemode(dst)
943
943
944 class chunkbuffer(object):
944 class chunkbuffer(object):
945 """Allow arbitrary sized chunks of data to be efficiently read from an
945 """Allow arbitrary sized chunks of data to be efficiently read from an
946 iterator over chunks of arbitrary size."""
946 iterator over chunks of arbitrary size."""
947
947
948 def __init__(self, in_iter):
948 def __init__(self, in_iter):
949 """in_iter is the iterator that's iterating over the input chunks.
949 """in_iter is the iterator that's iterating over the input chunks.
950 targetsize is how big a buffer to try to maintain."""
950 targetsize is how big a buffer to try to maintain."""
951 def splitbig(chunks):
951 def splitbig(chunks):
952 for chunk in chunks:
952 for chunk in chunks:
953 if len(chunk) > 2**20:
953 if len(chunk) > 2**20:
954 pos = 0
954 pos = 0
955 while pos < len(chunk):
955 while pos < len(chunk):
956 end = pos + 2 ** 18
956 end = pos + 2 ** 18
957 yield chunk[pos:end]
957 yield chunk[pos:end]
958 pos = end
958 pos = end
959 else:
959 else:
960 yield chunk
960 yield chunk
961 self.iter = splitbig(in_iter)
961 self.iter = splitbig(in_iter)
962 self._queue = []
962 self._queue = []
963
963
964 def read(self, l):
964 def read(self, l):
965 """Read L bytes of data from the iterator of chunks of data.
965 """Read L bytes of data from the iterator of chunks of data.
966 Returns less than L bytes if the iterator runs dry."""
966 Returns less than L bytes if the iterator runs dry."""
967 left = l
967 left = l
968 buf = ''
968 buf = ''
969 queue = self._queue
969 queue = self._queue
970 while left > 0:
970 while left > 0:
971 # refill the queue
971 # refill the queue
972 if not queue:
972 if not queue:
973 target = 2**18
973 target = 2**18
974 for chunk in self.iter:
974 for chunk in self.iter:
975 queue.append(chunk)
975 queue.append(chunk)
976 target -= len(chunk)
976 target -= len(chunk)
977 if target <= 0:
977 if target <= 0:
978 break
978 break
979 if not queue:
979 if not queue:
980 break
980 break
981
981
982 chunk = queue.pop(0)
982 chunk = queue.pop(0)
983 left -= len(chunk)
983 left -= len(chunk)
984 if left < 0:
984 if left < 0:
985 queue.insert(0, chunk[left:])
985 queue.insert(0, chunk[left:])
986 buf += chunk[:left]
986 buf += chunk[:left]
987 else:
987 else:
988 buf += chunk
988 buf += chunk
989
989
990 return buf
990 return buf
991
991
992 def filechunkiter(f, size=65536, limit=None):
992 def filechunkiter(f, size=65536, limit=None):
993 """Create a generator that produces the data in the file size
993 """Create a generator that produces the data in the file size
994 (default 65536) bytes at a time, up to optional limit (default is
994 (default 65536) bytes at a time, up to optional limit (default is
995 to read all data). Chunks may be less than size bytes if the
995 to read all data). Chunks may be less than size bytes if the
996 chunk is the last chunk in the file, or the file is a socket or
996 chunk is the last chunk in the file, or the file is a socket or
997 some other type of file that sometimes reads less data than is
997 some other type of file that sometimes reads less data than is
998 requested."""
998 requested."""
999 assert size >= 0
999 assert size >= 0
1000 assert limit is None or limit >= 0
1000 assert limit is None or limit >= 0
1001 while True:
1001 while True:
1002 if limit is None:
1002 if limit is None:
1003 nbytes = size
1003 nbytes = size
1004 else:
1004 else:
1005 nbytes = min(limit, size)
1005 nbytes = min(limit, size)
1006 s = nbytes and f.read(nbytes)
1006 s = nbytes and f.read(nbytes)
1007 if not s:
1007 if not s:
1008 break
1008 break
1009 if limit:
1009 if limit:
1010 limit -= len(s)
1010 limit -= len(s)
1011 yield s
1011 yield s
1012
1012
1013 def makedate():
1013 def makedate():
1014 lt = time.localtime()
1014 lt = time.localtime()
1015 if lt[8] == 1 and time.daylight:
1015 if lt[8] == 1 and time.daylight:
1016 tz = time.altzone
1016 tz = time.altzone
1017 else:
1017 else:
1018 tz = time.timezone
1018 tz = time.timezone
1019 t = time.mktime(lt)
1019 t = time.mktime(lt)
1020 if t < 0:
1020 if t < 0:
1021 hint = _("check your clock")
1021 hint = _("check your clock")
1022 raise Abort(_("negative timestamp: %d") % t, hint=hint)
1022 raise Abort(_("negative timestamp: %d") % t, hint=hint)
1023 return t, tz
1023 return t, tz
1024
1024
1025 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1025 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1026 """represent a (unixtime, offset) tuple as a localized time.
1026 """represent a (unixtime, offset) tuple as a localized time.
1027 unixtime is seconds since the epoch, and offset is the time zone's
1027 unixtime is seconds since the epoch, and offset is the time zone's
1028 number of seconds away from UTC. if timezone is false, do not
1028 number of seconds away from UTC. if timezone is false, do not
1029 append time zone to string."""
1029 append time zone to string."""
1030 t, tz = date or makedate()
1030 t, tz = date or makedate()
1031 if t < 0:
1031 if t < 0:
1032 t = 0 # time.gmtime(lt) fails on Windows for lt < -43200
1032 t = 0 # time.gmtime(lt) fails on Windows for lt < -43200
1033 tz = 0
1033 tz = 0
1034 if "%1" in format or "%2" in format:
1034 if "%1" in format or "%2" in format:
1035 sign = (tz > 0) and "-" or "+"
1035 sign = (tz > 0) and "-" or "+"
1036 minutes = abs(tz) // 60
1036 minutes = abs(tz) // 60
1037 format = format.replace("%1", "%c%02d" % (sign, minutes // 60))
1037 format = format.replace("%1", "%c%02d" % (sign, minutes // 60))
1038 format = format.replace("%2", "%02d" % (minutes % 60))
1038 format = format.replace("%2", "%02d" % (minutes % 60))
1039 s = time.strftime(format, time.gmtime(float(t) - tz))
1039 s = time.strftime(format, time.gmtime(float(t) - tz))
1040 return s
1040 return s
1041
1041
1042 def shortdate(date=None):
1042 def shortdate(date=None):
1043 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1043 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1044 return datestr(date, format='%Y-%m-%d')
1044 return datestr(date, format='%Y-%m-%d')
1045
1045
1046 def strdate(string, format, defaults=[]):
1046 def strdate(string, format, defaults=[]):
1047 """parse a localized time string and return a (unixtime, offset) tuple.
1047 """parse a localized time string and return a (unixtime, offset) tuple.
1048 if the string cannot be parsed, ValueError is raised."""
1048 if the string cannot be parsed, ValueError is raised."""
1049 def timezone(string):
1049 def timezone(string):
1050 tz = string.split()[-1]
1050 tz = string.split()[-1]
1051 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1051 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
1052 sign = (tz[0] == "+") and 1 or -1
1052 sign = (tz[0] == "+") and 1 or -1
1053 hours = int(tz[1:3])
1053 hours = int(tz[1:3])
1054 minutes = int(tz[3:5])
1054 minutes = int(tz[3:5])
1055 return -sign * (hours * 60 + minutes) * 60
1055 return -sign * (hours * 60 + minutes) * 60
1056 if tz == "GMT" or tz == "UTC":
1056 if tz == "GMT" or tz == "UTC":
1057 return 0
1057 return 0
1058 return None
1058 return None
1059
1059
1060 # NOTE: unixtime = localunixtime + offset
1060 # NOTE: unixtime = localunixtime + offset
1061 offset, date = timezone(string), string
1061 offset, date = timezone(string), string
1062 if offset is not None:
1062 if offset is not None:
1063 date = " ".join(string.split()[:-1])
1063 date = " ".join(string.split()[:-1])
1064
1064
1065 # add missing elements from defaults
1065 # add missing elements from defaults
1066 for part in defaults:
1066 for part in defaults:
1067 found = [True for p in part if ("%"+p) in format]
1067 found = [True for p in part if ("%"+p) in format]
1068 if not found:
1068 if not found:
1069 date += "@" + defaults[part]
1069 date += "@" + defaults[part]
1070 format += "@%" + part[0]
1070 format += "@%" + part[0]
1071
1071
1072 timetuple = time.strptime(date, format)
1072 timetuple = time.strptime(date, format)
1073 localunixtime = int(calendar.timegm(timetuple))
1073 localunixtime = int(calendar.timegm(timetuple))
1074 if offset is None:
1074 if offset is None:
1075 # local timezone
1075 # local timezone
1076 unixtime = int(time.mktime(timetuple))
1076 unixtime = int(time.mktime(timetuple))
1077 offset = unixtime - localunixtime
1077 offset = unixtime - localunixtime
1078 else:
1078 else:
1079 unixtime = localunixtime + offset
1079 unixtime = localunixtime + offset
1080 return unixtime, offset
1080 return unixtime, offset
1081
1081
1082 def parsedate(date, formats=None, defaults=None):
1082 def parsedate(date, formats=None, defaults=None):
1083 """parse a localized date/time string and return a (unixtime, offset) tuple.
1083 """parse a localized date/time string and return a (unixtime, offset) tuple.
1084
1084
1085 The date may be a "unixtime offset" string or in one of the specified
1085 The date may be a "unixtime offset" string or in one of the specified
1086 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1086 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1087 """
1087 """
1088 if not date:
1088 if not date:
1089 return 0, 0
1089 return 0, 0
1090 if isinstance(date, tuple) and len(date) == 2:
1090 if isinstance(date, tuple) and len(date) == 2:
1091 return date
1091 return date
1092 if not formats:
1092 if not formats:
1093 formats = defaultdateformats
1093 formats = defaultdateformats
1094 date = date.strip()
1094 date = date.strip()
1095 try:
1095 try:
1096 when, offset = map(int, date.split(' '))
1096 when, offset = map(int, date.split(' '))
1097 except ValueError:
1097 except ValueError:
1098 # fill out defaults
1098 # fill out defaults
1099 if not defaults:
1099 if not defaults:
1100 defaults = {}
1100 defaults = {}
1101 now = makedate()
1101 now = makedate()
1102 for part in "d mb yY HI M S".split():
1102 for part in ("d", "mb", "yY", "HI", "M", "S"):
1103 if part not in defaults:
1103 if part not in defaults:
1104 if part[0] in "HMS":
1104 if part[0] in "HMS":
1105 defaults[part] = "00"
1105 defaults[part] = "00"
1106 else:
1106 else:
1107 defaults[part] = datestr(now, "%" + part[0])
1107 defaults[part] = datestr(now, "%" + part[0])
1108
1108
1109 for format in formats:
1109 for format in formats:
1110 try:
1110 try:
1111 when, offset = strdate(date, format, defaults)
1111 when, offset = strdate(date, format, defaults)
1112 except (ValueError, OverflowError):
1112 except (ValueError, OverflowError):
1113 pass
1113 pass
1114 else:
1114 else:
1115 break
1115 break
1116 else:
1116 else:
1117 raise Abort(_('invalid date: %r') % date)
1117 raise Abort(_('invalid date: %r') % date)
1118 # validate explicit (probably user-specified) date and
1118 # validate explicit (probably user-specified) date and
1119 # time zone offset. values must fit in signed 32 bits for
1119 # time zone offset. values must fit in signed 32 bits for
1120 # current 32-bit linux runtimes. timezones go from UTC-12
1120 # current 32-bit linux runtimes. timezones go from UTC-12
1121 # to UTC+14
1121 # to UTC+14
1122 if abs(when) > 0x7fffffff:
1122 if abs(when) > 0x7fffffff:
1123 raise Abort(_('date exceeds 32 bits: %d') % when)
1123 raise Abort(_('date exceeds 32 bits: %d') % when)
1124 if when < 0:
1124 if when < 0:
1125 raise Abort(_('negative date value: %d') % when)
1125 raise Abort(_('negative date value: %d') % when)
1126 if offset < -50400 or offset > 43200:
1126 if offset < -50400 or offset > 43200:
1127 raise Abort(_('impossible time zone offset: %d') % offset)
1127 raise Abort(_('impossible time zone offset: %d') % offset)
1128 return when, offset
1128 return when, offset
1129
1129
1130 def matchdate(date):
1130 def matchdate(date):
1131 """Return a function that matches a given date match specifier
1131 """Return a function that matches a given date match specifier
1132
1132
1133 Formats include:
1133 Formats include:
1134
1134
1135 '{date}' match a given date to the accuracy provided
1135 '{date}' match a given date to the accuracy provided
1136
1136
1137 '<{date}' on or before a given date
1137 '<{date}' on or before a given date
1138
1138
1139 '>{date}' on or after a given date
1139 '>{date}' on or after a given date
1140
1140
1141 """
1141 """
1142
1142
1143 def lower(date):
1143 def lower(date):
1144 d = dict(mb="1", d="1")
1144 d = dict(mb="1", d="1")
1145 return parsedate(date, extendeddateformats, d)[0]
1145 return parsedate(date, extendeddateformats, d)[0]
1146
1146
1147 def upper(date):
1147 def upper(date):
1148 d = dict(mb="12", HI="23", M="59", S="59")
1148 d = dict(mb="12", HI="23", M="59", S="59")
1149 for days in "31 30 29".split():
1149 for days in ("31", "30", "29"):
1150 try:
1150 try:
1151 d["d"] = days
1151 d["d"] = days
1152 return parsedate(date, extendeddateformats, d)[0]
1152 return parsedate(date, extendeddateformats, d)[0]
1153 except:
1153 except:
1154 pass
1154 pass
1155 d["d"] = "28"
1155 d["d"] = "28"
1156 return parsedate(date, extendeddateformats, d)[0]
1156 return parsedate(date, extendeddateformats, d)[0]
1157
1157
1158 date = date.strip()
1158 date = date.strip()
1159 if date[0] == "<":
1159 if date[0] == "<":
1160 when = upper(date[1:])
1160 when = upper(date[1:])
1161 return lambda x: x <= when
1161 return lambda x: x <= when
1162 elif date[0] == ">":
1162 elif date[0] == ">":
1163 when = lower(date[1:])
1163 when = lower(date[1:])
1164 return lambda x: x >= when
1164 return lambda x: x >= when
1165 elif date[0] == "-":
1165 elif date[0] == "-":
1166 try:
1166 try:
1167 days = int(date[1:])
1167 days = int(date[1:])
1168 except ValueError:
1168 except ValueError:
1169 raise Abort(_("invalid day spec: %s") % date[1:])
1169 raise Abort(_("invalid day spec: %s") % date[1:])
1170 when = makedate()[0] - days * 3600 * 24
1170 when = makedate()[0] - days * 3600 * 24
1171 return lambda x: x >= when
1171 return lambda x: x >= when
1172 elif " to " in date:
1172 elif " to " in date:
1173 a, b = date.split(" to ")
1173 a, b = date.split(" to ")
1174 start, stop = lower(a), upper(b)
1174 start, stop = lower(a), upper(b)
1175 return lambda x: x >= start and x <= stop
1175 return lambda x: x >= start and x <= stop
1176 else:
1176 else:
1177 start, stop = lower(date), upper(date)
1177 start, stop = lower(date), upper(date)
1178 return lambda x: x >= start and x <= stop
1178 return lambda x: x >= start and x <= stop
1179
1179
1180 def shortuser(user):
1180 def shortuser(user):
1181 """Return a short representation of a user name or email address."""
1181 """Return a short representation of a user name or email address."""
1182 f = user.find('@')
1182 f = user.find('@')
1183 if f >= 0:
1183 if f >= 0:
1184 user = user[:f]
1184 user = user[:f]
1185 f = user.find('<')
1185 f = user.find('<')
1186 if f >= 0:
1186 if f >= 0:
1187 user = user[f + 1:]
1187 user = user[f + 1:]
1188 f = user.find(' ')
1188 f = user.find(' ')
1189 if f >= 0:
1189 if f >= 0:
1190 user = user[:f]
1190 user = user[:f]
1191 f = user.find('.')
1191 f = user.find('.')
1192 if f >= 0:
1192 if f >= 0:
1193 user = user[:f]
1193 user = user[:f]
1194 return user
1194 return user
1195
1195
1196 def email(author):
1196 def email(author):
1197 '''get email of author.'''
1197 '''get email of author.'''
1198 r = author.find('>')
1198 r = author.find('>')
1199 if r == -1:
1199 if r == -1:
1200 r = None
1200 r = None
1201 return author[author.find('<') + 1:r]
1201 return author[author.find('<') + 1:r]
1202
1202
1203 def ellipsis(text, maxlength=400):
1203 def ellipsis(text, maxlength=400):
1204 """Trim string to at most maxlength (default: 400) characters."""
1204 """Trim string to at most maxlength (default: 400) characters."""
1205 if len(text) <= maxlength:
1205 if len(text) <= maxlength:
1206 return text
1206 return text
1207 else:
1207 else:
1208 return "%s..." % (text[:maxlength - 3])
1208 return "%s..." % (text[:maxlength - 3])
1209
1209
1210 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
1210 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
1211 '''yield every hg repository under path, recursively.'''
1211 '''yield every hg repository under path, recursively.'''
1212 def errhandler(err):
1212 def errhandler(err):
1213 if err.filename == path:
1213 if err.filename == path:
1214 raise err
1214 raise err
1215 if followsym and hasattr(os.path, 'samestat'):
1215 if followsym and hasattr(os.path, 'samestat'):
1216 def _add_dir_if_not_there(dirlst, dirname):
1216 def _add_dir_if_not_there(dirlst, dirname):
1217 match = False
1217 match = False
1218 samestat = os.path.samestat
1218 samestat = os.path.samestat
1219 dirstat = os.stat(dirname)
1219 dirstat = os.stat(dirname)
1220 for lstdirstat in dirlst:
1220 for lstdirstat in dirlst:
1221 if samestat(dirstat, lstdirstat):
1221 if samestat(dirstat, lstdirstat):
1222 match = True
1222 match = True
1223 break
1223 break
1224 if not match:
1224 if not match:
1225 dirlst.append(dirstat)
1225 dirlst.append(dirstat)
1226 return not match
1226 return not match
1227 else:
1227 else:
1228 followsym = False
1228 followsym = False
1229
1229
1230 if (seen_dirs is None) and followsym:
1230 if (seen_dirs is None) and followsym:
1231 seen_dirs = []
1231 seen_dirs = []
1232 _add_dir_if_not_there(seen_dirs, path)
1232 _add_dir_if_not_there(seen_dirs, path)
1233 for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
1233 for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
1234 dirs.sort()
1234 dirs.sort()
1235 if '.hg' in dirs:
1235 if '.hg' in dirs:
1236 yield root # found a repository
1236 yield root # found a repository
1237 qroot = os.path.join(root, '.hg', 'patches')
1237 qroot = os.path.join(root, '.hg', 'patches')
1238 if os.path.isdir(os.path.join(qroot, '.hg')):
1238 if os.path.isdir(os.path.join(qroot, '.hg')):
1239 yield qroot # we have a patch queue repo here
1239 yield qroot # we have a patch queue repo here
1240 if recurse:
1240 if recurse:
1241 # avoid recursing inside the .hg directory
1241 # avoid recursing inside the .hg directory
1242 dirs.remove('.hg')
1242 dirs.remove('.hg')
1243 else:
1243 else:
1244 dirs[:] = [] # don't descend further
1244 dirs[:] = [] # don't descend further
1245 elif followsym:
1245 elif followsym:
1246 newdirs = []
1246 newdirs = []
1247 for d in dirs:
1247 for d in dirs:
1248 fname = os.path.join(root, d)
1248 fname = os.path.join(root, d)
1249 if _add_dir_if_not_there(seen_dirs, fname):
1249 if _add_dir_if_not_there(seen_dirs, fname):
1250 if os.path.islink(fname):
1250 if os.path.islink(fname):
1251 for hgname in walkrepos(fname, True, seen_dirs):
1251 for hgname in walkrepos(fname, True, seen_dirs):
1252 yield hgname
1252 yield hgname
1253 else:
1253 else:
1254 newdirs.append(d)
1254 newdirs.append(d)
1255 dirs[:] = newdirs
1255 dirs[:] = newdirs
1256
1256
1257 _rcpath = None
1257 _rcpath = None
1258
1258
1259 def os_rcpath():
1259 def os_rcpath():
1260 '''return default os-specific hgrc search path'''
1260 '''return default os-specific hgrc search path'''
1261 path = system_rcpath()
1261 path = system_rcpath()
1262 path.extend(user_rcpath())
1262 path.extend(user_rcpath())
1263 path = [os.path.normpath(f) for f in path]
1263 path = [os.path.normpath(f) for f in path]
1264 return path
1264 return path
1265
1265
1266 def rcpath():
1266 def rcpath():
1267 '''return hgrc search path. if env var HGRCPATH is set, use it.
1267 '''return hgrc search path. if env var HGRCPATH is set, use it.
1268 for each item in path, if directory, use files ending in .rc,
1268 for each item in path, if directory, use files ending in .rc,
1269 else use item.
1269 else use item.
1270 make HGRCPATH empty to only look in .hg/hgrc of current repo.
1270 make HGRCPATH empty to only look in .hg/hgrc of current repo.
1271 if no HGRCPATH, use default os-specific path.'''
1271 if no HGRCPATH, use default os-specific path.'''
1272 global _rcpath
1272 global _rcpath
1273 if _rcpath is None:
1273 if _rcpath is None:
1274 if 'HGRCPATH' in os.environ:
1274 if 'HGRCPATH' in os.environ:
1275 _rcpath = []
1275 _rcpath = []
1276 for p in os.environ['HGRCPATH'].split(os.pathsep):
1276 for p in os.environ['HGRCPATH'].split(os.pathsep):
1277 if not p:
1277 if not p:
1278 continue
1278 continue
1279 p = expandpath(p)
1279 p = expandpath(p)
1280 if os.path.isdir(p):
1280 if os.path.isdir(p):
1281 for f, kind in osutil.listdir(p):
1281 for f, kind in osutil.listdir(p):
1282 if f.endswith('.rc'):
1282 if f.endswith('.rc'):
1283 _rcpath.append(os.path.join(p, f))
1283 _rcpath.append(os.path.join(p, f))
1284 else:
1284 else:
1285 _rcpath.append(p)
1285 _rcpath.append(p)
1286 else:
1286 else:
1287 _rcpath = os_rcpath()
1287 _rcpath = os_rcpath()
1288 return _rcpath
1288 return _rcpath
1289
1289
1290 def bytecount(nbytes):
1290 def bytecount(nbytes):
1291 '''return byte count formatted as readable string, with units'''
1291 '''return byte count formatted as readable string, with units'''
1292
1292
1293 units = (
1293 units = (
1294 (100, 1 << 30, _('%.0f GB')),
1294 (100, 1 << 30, _('%.0f GB')),
1295 (10, 1 << 30, _('%.1f GB')),
1295 (10, 1 << 30, _('%.1f GB')),
1296 (1, 1 << 30, _('%.2f GB')),
1296 (1, 1 << 30, _('%.2f GB')),
1297 (100, 1 << 20, _('%.0f MB')),
1297 (100, 1 << 20, _('%.0f MB')),
1298 (10, 1 << 20, _('%.1f MB')),
1298 (10, 1 << 20, _('%.1f MB')),
1299 (1, 1 << 20, _('%.2f MB')),
1299 (1, 1 << 20, _('%.2f MB')),
1300 (100, 1 << 10, _('%.0f KB')),
1300 (100, 1 << 10, _('%.0f KB')),
1301 (10, 1 << 10, _('%.1f KB')),
1301 (10, 1 << 10, _('%.1f KB')),
1302 (1, 1 << 10, _('%.2f KB')),
1302 (1, 1 << 10, _('%.2f KB')),
1303 (1, 1, _('%.0f bytes')),
1303 (1, 1, _('%.0f bytes')),
1304 )
1304 )
1305
1305
1306 for multiplier, divisor, format in units:
1306 for multiplier, divisor, format in units:
1307 if nbytes >= divisor * multiplier:
1307 if nbytes >= divisor * multiplier:
1308 return format % (nbytes / float(divisor))
1308 return format % (nbytes / float(divisor))
1309 return units[-1][2] % nbytes
1309 return units[-1][2] % nbytes
1310
1310
1311 def drop_scheme(scheme, path):
1311 def drop_scheme(scheme, path):
1312 sc = scheme + ':'
1312 sc = scheme + ':'
1313 if path.startswith(sc):
1313 if path.startswith(sc):
1314 path = path[len(sc):]
1314 path = path[len(sc):]
1315 if path.startswith('//'):
1315 if path.startswith('//'):
1316 if scheme == 'file':
1316 if scheme == 'file':
1317 i = path.find('/', 2)
1317 i = path.find('/', 2)
1318 if i == -1:
1318 if i == -1:
1319 return ''
1319 return ''
1320 # On Windows, absolute paths are rooted at the current drive
1320 # On Windows, absolute paths are rooted at the current drive
1321 # root. On POSIX they are rooted at the file system root.
1321 # root. On POSIX they are rooted at the file system root.
1322 if os.name == 'nt':
1322 if os.name == 'nt':
1323 droot = os.path.splitdrive(os.getcwd())[0] + '/'
1323 droot = os.path.splitdrive(os.getcwd())[0] + '/'
1324 path = os.path.join(droot, path[i + 1:])
1324 path = os.path.join(droot, path[i + 1:])
1325 else:
1325 else:
1326 path = path[i:]
1326 path = path[i:]
1327 else:
1327 else:
1328 path = path[2:]
1328 path = path[2:]
1329 return path
1329 return path
1330
1330
1331 def uirepr(s):
1331 def uirepr(s):
1332 # Avoid double backslash in Windows path repr()
1332 # Avoid double backslash in Windows path repr()
1333 return repr(s).replace('\\\\', '\\')
1333 return repr(s).replace('\\\\', '\\')
1334
1334
1335 #### naming convention of below implementation follows 'textwrap' module
1335 #### naming convention of below implementation follows 'textwrap' module
1336
1336
1337 class MBTextWrapper(textwrap.TextWrapper):
1337 class MBTextWrapper(textwrap.TextWrapper):
1338 """
1338 """
1339 Extend TextWrapper for double-width characters.
1339 Extend TextWrapper for double-width characters.
1340
1340
1341 Some Asian characters use two terminal columns instead of one.
1341 Some Asian characters use two terminal columns instead of one.
1342 A good example of this behavior can be seen with u'\u65e5\u672c',
1342 A good example of this behavior can be seen with u'\u65e5\u672c',
1343 the two Japanese characters for "Japan":
1343 the two Japanese characters for "Japan":
1344 len() returns 2, but when printed to a terminal, they eat 4 columns.
1344 len() returns 2, but when printed to a terminal, they eat 4 columns.
1345
1345
1346 (Note that this has nothing to do whatsoever with unicode
1346 (Note that this has nothing to do whatsoever with unicode
1347 representation, or encoding of the underlying string)
1347 representation, or encoding of the underlying string)
1348 """
1348 """
1349 def __init__(self, **kwargs):
1349 def __init__(self, **kwargs):
1350 textwrap.TextWrapper.__init__(self, **kwargs)
1350 textwrap.TextWrapper.__init__(self, **kwargs)
1351
1351
1352 def _cutdown(self, str, space_left):
1352 def _cutdown(self, str, space_left):
1353 l = 0
1353 l = 0
1354 ucstr = unicode(str, encoding.encoding)
1354 ucstr = unicode(str, encoding.encoding)
1355 colwidth = unicodedata.east_asian_width
1355 colwidth = unicodedata.east_asian_width
1356 for i in xrange(len(ucstr)):
1356 for i in xrange(len(ucstr)):
1357 l += colwidth(ucstr[i]) in 'WFA' and 2 or 1
1357 l += colwidth(ucstr[i]) in 'WFA' and 2 or 1
1358 if space_left < l:
1358 if space_left < l:
1359 return (ucstr[:i].encode(encoding.encoding),
1359 return (ucstr[:i].encode(encoding.encoding),
1360 ucstr[i:].encode(encoding.encoding))
1360 ucstr[i:].encode(encoding.encoding))
1361 return str, ''
1361 return str, ''
1362
1362
1363 # ----------------------------------------
1363 # ----------------------------------------
1364 # overriding of base class
1364 # overriding of base class
1365
1365
1366 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
1366 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
1367 space_left = max(width - cur_len, 1)
1367 space_left = max(width - cur_len, 1)
1368
1368
1369 if self.break_long_words:
1369 if self.break_long_words:
1370 cut, res = self._cutdown(reversed_chunks[-1], space_left)
1370 cut, res = self._cutdown(reversed_chunks[-1], space_left)
1371 cur_line.append(cut)
1371 cur_line.append(cut)
1372 reversed_chunks[-1] = res
1372 reversed_chunks[-1] = res
1373 elif not cur_line:
1373 elif not cur_line:
1374 cur_line.append(reversed_chunks.pop())
1374 cur_line.append(reversed_chunks.pop())
1375
1375
1376 #### naming convention of above implementation follows 'textwrap' module
1376 #### naming convention of above implementation follows 'textwrap' module
1377
1377
1378 def wrap(line, width, initindent='', hangindent=''):
1378 def wrap(line, width, initindent='', hangindent=''):
1379 maxindent = max(len(hangindent), len(initindent))
1379 maxindent = max(len(hangindent), len(initindent))
1380 if width <= maxindent:
1380 if width <= maxindent:
1381 # adjust for weird terminal size
1381 # adjust for weird terminal size
1382 width = max(78, maxindent + 1)
1382 width = max(78, maxindent + 1)
1383 wrapper = MBTextWrapper(width=width,
1383 wrapper = MBTextWrapper(width=width,
1384 initial_indent=initindent,
1384 initial_indent=initindent,
1385 subsequent_indent=hangindent)
1385 subsequent_indent=hangindent)
1386 return wrapper.fill(line)
1386 return wrapper.fill(line)
1387
1387
1388 def iterlines(iterator):
1388 def iterlines(iterator):
1389 for chunk in iterator:
1389 for chunk in iterator:
1390 for line in chunk.splitlines():
1390 for line in chunk.splitlines():
1391 yield line
1391 yield line
1392
1392
1393 def expandpath(path):
1393 def expandpath(path):
1394 return os.path.expanduser(os.path.expandvars(path))
1394 return os.path.expanduser(os.path.expandvars(path))
1395
1395
1396 def hgcmd():
1396 def hgcmd():
1397 """Return the command used to execute current hg
1397 """Return the command used to execute current hg
1398
1398
1399 This is different from hgexecutable() because on Windows we want
1399 This is different from hgexecutable() because on Windows we want
1400 to avoid things opening new shell windows like batch files, so we
1400 to avoid things opening new shell windows like batch files, so we
1401 get either the python call or current executable.
1401 get either the python call or current executable.
1402 """
1402 """
1403 if main_is_frozen():
1403 if main_is_frozen():
1404 return [sys.executable]
1404 return [sys.executable]
1405 return gethgcmd()
1405 return gethgcmd()
1406
1406
1407 def rundetached(args, condfn):
1407 def rundetached(args, condfn):
1408 """Execute the argument list in a detached process.
1408 """Execute the argument list in a detached process.
1409
1409
1410 condfn is a callable which is called repeatedly and should return
1410 condfn is a callable which is called repeatedly and should return
1411 True once the child process is known to have started successfully.
1411 True once the child process is known to have started successfully.
1412 At this point, the child process PID is returned. If the child
1412 At this point, the child process PID is returned. If the child
1413 process fails to start or finishes before condfn() evaluates to
1413 process fails to start or finishes before condfn() evaluates to
1414 True, return -1.
1414 True, return -1.
1415 """
1415 """
1416 # Windows case is easier because the child process is either
1416 # Windows case is easier because the child process is either
1417 # successfully starting and validating the condition or exiting
1417 # successfully starting and validating the condition or exiting
1418 # on failure. We just poll on its PID. On Unix, if the child
1418 # on failure. We just poll on its PID. On Unix, if the child
1419 # process fails to start, it will be left in a zombie state until
1419 # process fails to start, it will be left in a zombie state until
1420 # the parent wait on it, which we cannot do since we expect a long
1420 # the parent wait on it, which we cannot do since we expect a long
1421 # running process on success. Instead we listen for SIGCHLD telling
1421 # running process on success. Instead we listen for SIGCHLD telling
1422 # us our child process terminated.
1422 # us our child process terminated.
1423 terminated = set()
1423 terminated = set()
1424 def handler(signum, frame):
1424 def handler(signum, frame):
1425 terminated.add(os.wait())
1425 terminated.add(os.wait())
1426 prevhandler = None
1426 prevhandler = None
1427 if hasattr(signal, 'SIGCHLD'):
1427 if hasattr(signal, 'SIGCHLD'):
1428 prevhandler = signal.signal(signal.SIGCHLD, handler)
1428 prevhandler = signal.signal(signal.SIGCHLD, handler)
1429 try:
1429 try:
1430 pid = spawndetached(args)
1430 pid = spawndetached(args)
1431 while not condfn():
1431 while not condfn():
1432 if ((pid in terminated or not testpid(pid))
1432 if ((pid in terminated or not testpid(pid))
1433 and not condfn()):
1433 and not condfn()):
1434 return -1
1434 return -1
1435 time.sleep(0.1)
1435 time.sleep(0.1)
1436 return pid
1436 return pid
1437 finally:
1437 finally:
1438 if prevhandler is not None:
1438 if prevhandler is not None:
1439 signal.signal(signal.SIGCHLD, prevhandler)
1439 signal.signal(signal.SIGCHLD, prevhandler)
1440
1440
1441 try:
1441 try:
1442 any, all = any, all
1442 any, all = any, all
1443 except NameError:
1443 except NameError:
1444 def any(iterable):
1444 def any(iterable):
1445 for i in iterable:
1445 for i in iterable:
1446 if i:
1446 if i:
1447 return True
1447 return True
1448 return False
1448 return False
1449
1449
1450 def all(iterable):
1450 def all(iterable):
1451 for i in iterable:
1451 for i in iterable:
1452 if not i:
1452 if not i:
1453 return False
1453 return False
1454 return True
1454 return True
1455
1455
1456 def interpolate(prefix, mapping, s, fn=None):
1456 def interpolate(prefix, mapping, s, fn=None):
1457 """Return the result of interpolating items in the mapping into string s.
1457 """Return the result of interpolating items in the mapping into string s.
1458
1458
1459 prefix is a single character string, or a two character string with
1459 prefix is a single character string, or a two character string with
1460 a backslash as the first character if the prefix needs to be escaped in
1460 a backslash as the first character if the prefix needs to be escaped in
1461 a regular expression.
1461 a regular expression.
1462
1462
1463 fn is an optional function that will be applied to the replacement text
1463 fn is an optional function that will be applied to the replacement text
1464 just before replacement.
1464 just before replacement.
1465 """
1465 """
1466 fn = fn or (lambda s: s)
1466 fn = fn or (lambda s: s)
1467 r = re.compile(r'%s(%s)' % (prefix, '|'.join(mapping.keys())))
1467 r = re.compile(r'%s(%s)' % (prefix, '|'.join(mapping.keys())))
1468 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
1468 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
1469
1469
1470 def getport(port):
1470 def getport(port):
1471 """Return the port for a given network service.
1471 """Return the port for a given network service.
1472
1472
1473 If port is an integer, it's returned as is. If it's a string, it's
1473 If port is an integer, it's returned as is. If it's a string, it's
1474 looked up using socket.getservbyname(). If there's no matching
1474 looked up using socket.getservbyname(). If there's no matching
1475 service, util.Abort is raised.
1475 service, util.Abort is raised.
1476 """
1476 """
1477 try:
1477 try:
1478 return int(port)
1478 return int(port)
1479 except ValueError:
1479 except ValueError:
1480 pass
1480 pass
1481
1481
1482 try:
1482 try:
1483 return socket.getservbyname(port)
1483 return socket.getservbyname(port)
1484 except socket.error:
1484 except socket.error:
1485 raise Abort(_("no port number associated with service '%s'") % port)
1485 raise Abort(_("no port number associated with service '%s'") % port)
1486
1486
1487 _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True,
1487 _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True,
1488 '0': False, 'no': False, 'false': False, 'off': False,
1488 '0': False, 'no': False, 'false': False, 'off': False,
1489 'never': False}
1489 'never': False}
1490
1490
1491 def parsebool(s):
1491 def parsebool(s):
1492 """Parse s into a boolean.
1492 """Parse s into a boolean.
1493
1493
1494 If s is not a valid boolean, returns None.
1494 If s is not a valid boolean, returns None.
1495 """
1495 """
1496 return _booleans.get(s.lower(), None)
1496 return _booleans.get(s.lower(), None)
General Comments 0
You need to be logged in to leave comments. Login now