##// END OF EJS Templates
scmutil.addremove: factor out dirstate walk into another function...
Siddharth Agarwal -
r19150:7a4eab24 default
parent child Browse files
Show More
@@ -1,953 +1,964 b''
1 # scmutil.py - Mercurial core utility functions
1 # scmutil.py - Mercurial core utility functions
2 #
2 #
3 # Copyright Matt Mackall <mpm@selenic.com>
3 # Copyright 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 i18n import _
8 from i18n import _
9 from mercurial.node import nullrev
9 from mercurial.node import nullrev
10 import util, error, osutil, revset, similar, encoding, phases, parsers
10 import util, error, osutil, revset, similar, encoding, phases, parsers
11 import match as matchmod
11 import match as matchmod
12 import os, errno, re, stat, glob
12 import os, errno, re, stat, glob
13
13
14 if os.name == 'nt':
14 if os.name == 'nt':
15 import scmwindows as scmplatform
15 import scmwindows as scmplatform
16 else:
16 else:
17 import scmposix as scmplatform
17 import scmposix as scmplatform
18
18
19 systemrcpath = scmplatform.systemrcpath
19 systemrcpath = scmplatform.systemrcpath
20 userrcpath = scmplatform.userrcpath
20 userrcpath = scmplatform.userrcpath
21
21
22 def nochangesfound(ui, repo, excluded=None):
22 def nochangesfound(ui, repo, excluded=None):
23 '''Report no changes for push/pull, excluded is None or a list of
23 '''Report no changes for push/pull, excluded is None or a list of
24 nodes excluded from the push/pull.
24 nodes excluded from the push/pull.
25 '''
25 '''
26 secretlist = []
26 secretlist = []
27 if excluded:
27 if excluded:
28 for n in excluded:
28 for n in excluded:
29 if n not in repo:
29 if n not in repo:
30 # discovery should not have included the filtered revision,
30 # discovery should not have included the filtered revision,
31 # we have to explicitly exclude it until discovery is cleanup.
31 # we have to explicitly exclude it until discovery is cleanup.
32 continue
32 continue
33 ctx = repo[n]
33 ctx = repo[n]
34 if ctx.phase() >= phases.secret and not ctx.extinct():
34 if ctx.phase() >= phases.secret and not ctx.extinct():
35 secretlist.append(n)
35 secretlist.append(n)
36
36
37 if secretlist:
37 if secretlist:
38 ui.status(_("no changes found (ignored %d secret changesets)\n")
38 ui.status(_("no changes found (ignored %d secret changesets)\n")
39 % len(secretlist))
39 % len(secretlist))
40 else:
40 else:
41 ui.status(_("no changes found\n"))
41 ui.status(_("no changes found\n"))
42
42
43 def checknewlabel(repo, lbl, kind):
43 def checknewlabel(repo, lbl, kind):
44 # Do not use the "kind" parameter in ui output.
44 # Do not use the "kind" parameter in ui output.
45 # It makes strings difficult to translate.
45 # It makes strings difficult to translate.
46 if lbl in ['tip', '.', 'null']:
46 if lbl in ['tip', '.', 'null']:
47 raise util.Abort(_("the name '%s' is reserved") % lbl)
47 raise util.Abort(_("the name '%s' is reserved") % lbl)
48 for c in (':', '\0', '\n', '\r'):
48 for c in (':', '\0', '\n', '\r'):
49 if c in lbl:
49 if c in lbl:
50 raise util.Abort(_("%r cannot be used in a name") % c)
50 raise util.Abort(_("%r cannot be used in a name") % c)
51 try:
51 try:
52 int(lbl)
52 int(lbl)
53 raise util.Abort(_("cannot use an integer as a name"))
53 raise util.Abort(_("cannot use an integer as a name"))
54 except ValueError:
54 except ValueError:
55 pass
55 pass
56
56
57 def checkfilename(f):
57 def checkfilename(f):
58 '''Check that the filename f is an acceptable filename for a tracked file'''
58 '''Check that the filename f is an acceptable filename for a tracked file'''
59 if '\r' in f or '\n' in f:
59 if '\r' in f or '\n' in f:
60 raise util.Abort(_("'\\n' and '\\r' disallowed in filenames: %r") % f)
60 raise util.Abort(_("'\\n' and '\\r' disallowed in filenames: %r") % f)
61
61
62 def checkportable(ui, f):
62 def checkportable(ui, f):
63 '''Check if filename f is portable and warn or abort depending on config'''
63 '''Check if filename f is portable and warn or abort depending on config'''
64 checkfilename(f)
64 checkfilename(f)
65 abort, warn = checkportabilityalert(ui)
65 abort, warn = checkportabilityalert(ui)
66 if abort or warn:
66 if abort or warn:
67 msg = util.checkwinfilename(f)
67 msg = util.checkwinfilename(f)
68 if msg:
68 if msg:
69 msg = "%s: %r" % (msg, f)
69 msg = "%s: %r" % (msg, f)
70 if abort:
70 if abort:
71 raise util.Abort(msg)
71 raise util.Abort(msg)
72 ui.warn(_("warning: %s\n") % msg)
72 ui.warn(_("warning: %s\n") % msg)
73
73
74 def checkportabilityalert(ui):
74 def checkportabilityalert(ui):
75 '''check if the user's config requests nothing, a warning, or abort for
75 '''check if the user's config requests nothing, a warning, or abort for
76 non-portable filenames'''
76 non-portable filenames'''
77 val = ui.config('ui', 'portablefilenames', 'warn')
77 val = ui.config('ui', 'portablefilenames', 'warn')
78 lval = val.lower()
78 lval = val.lower()
79 bval = util.parsebool(val)
79 bval = util.parsebool(val)
80 abort = os.name == 'nt' or lval == 'abort'
80 abort = os.name == 'nt' or lval == 'abort'
81 warn = bval or lval == 'warn'
81 warn = bval or lval == 'warn'
82 if bval is None and not (warn or abort or lval == 'ignore'):
82 if bval is None and not (warn or abort or lval == 'ignore'):
83 raise error.ConfigError(
83 raise error.ConfigError(
84 _("ui.portablefilenames value is invalid ('%s')") % val)
84 _("ui.portablefilenames value is invalid ('%s')") % val)
85 return abort, warn
85 return abort, warn
86
86
87 class casecollisionauditor(object):
87 class casecollisionauditor(object):
88 def __init__(self, ui, abort, dirstate):
88 def __init__(self, ui, abort, dirstate):
89 self._ui = ui
89 self._ui = ui
90 self._abort = abort
90 self._abort = abort
91 allfiles = '\0'.join(dirstate._map)
91 allfiles = '\0'.join(dirstate._map)
92 self._loweredfiles = set(encoding.lower(allfiles).split('\0'))
92 self._loweredfiles = set(encoding.lower(allfiles).split('\0'))
93 self._dirstate = dirstate
93 self._dirstate = dirstate
94 # The purpose of _newfiles is so that we don't complain about
94 # The purpose of _newfiles is so that we don't complain about
95 # case collisions if someone were to call this object with the
95 # case collisions if someone were to call this object with the
96 # same filename twice.
96 # same filename twice.
97 self._newfiles = set()
97 self._newfiles = set()
98
98
99 def __call__(self, f):
99 def __call__(self, f):
100 fl = encoding.lower(f)
100 fl = encoding.lower(f)
101 if (fl in self._loweredfiles and f not in self._dirstate and
101 if (fl in self._loweredfiles and f not in self._dirstate and
102 f not in self._newfiles):
102 f not in self._newfiles):
103 msg = _('possible case-folding collision for %s') % f
103 msg = _('possible case-folding collision for %s') % f
104 if self._abort:
104 if self._abort:
105 raise util.Abort(msg)
105 raise util.Abort(msg)
106 self._ui.warn(_("warning: %s\n") % msg)
106 self._ui.warn(_("warning: %s\n") % msg)
107 self._loweredfiles.add(fl)
107 self._loweredfiles.add(fl)
108 self._newfiles.add(f)
108 self._newfiles.add(f)
109
109
110 class pathauditor(object):
110 class pathauditor(object):
111 '''ensure that a filesystem path contains no banned components.
111 '''ensure that a filesystem path contains no banned components.
112 the following properties of a path are checked:
112 the following properties of a path are checked:
113
113
114 - ends with a directory separator
114 - ends with a directory separator
115 - under top-level .hg
115 - under top-level .hg
116 - starts at the root of a windows drive
116 - starts at the root of a windows drive
117 - contains ".."
117 - contains ".."
118 - traverses a symlink (e.g. a/symlink_here/b)
118 - traverses a symlink (e.g. a/symlink_here/b)
119 - inside a nested repository (a callback can be used to approve
119 - inside a nested repository (a callback can be used to approve
120 some nested repositories, e.g., subrepositories)
120 some nested repositories, e.g., subrepositories)
121 '''
121 '''
122
122
123 def __init__(self, root, callback=None):
123 def __init__(self, root, callback=None):
124 self.audited = set()
124 self.audited = set()
125 self.auditeddir = set()
125 self.auditeddir = set()
126 self.root = root
126 self.root = root
127 self.callback = callback
127 self.callback = callback
128 if os.path.lexists(root) and not util.checkcase(root):
128 if os.path.lexists(root) and not util.checkcase(root):
129 self.normcase = util.normcase
129 self.normcase = util.normcase
130 else:
130 else:
131 self.normcase = lambda x: x
131 self.normcase = lambda x: x
132
132
133 def __call__(self, path):
133 def __call__(self, path):
134 '''Check the relative path.
134 '''Check the relative path.
135 path may contain a pattern (e.g. foodir/**.txt)'''
135 path may contain a pattern (e.g. foodir/**.txt)'''
136
136
137 path = util.localpath(path)
137 path = util.localpath(path)
138 normpath = self.normcase(path)
138 normpath = self.normcase(path)
139 if normpath in self.audited:
139 if normpath in self.audited:
140 return
140 return
141 # AIX ignores "/" at end of path, others raise EISDIR.
141 # AIX ignores "/" at end of path, others raise EISDIR.
142 if util.endswithsep(path):
142 if util.endswithsep(path):
143 raise util.Abort(_("path ends in directory separator: %s") % path)
143 raise util.Abort(_("path ends in directory separator: %s") % path)
144 parts = util.splitpath(path)
144 parts = util.splitpath(path)
145 if (os.path.splitdrive(path)[0]
145 if (os.path.splitdrive(path)[0]
146 or parts[0].lower() in ('.hg', '.hg.', '')
146 or parts[0].lower() in ('.hg', '.hg.', '')
147 or os.pardir in parts):
147 or os.pardir in parts):
148 raise util.Abort(_("path contains illegal component: %s") % path)
148 raise util.Abort(_("path contains illegal component: %s") % path)
149 if '.hg' in path.lower():
149 if '.hg' in path.lower():
150 lparts = [p.lower() for p in parts]
150 lparts = [p.lower() for p in parts]
151 for p in '.hg', '.hg.':
151 for p in '.hg', '.hg.':
152 if p in lparts[1:]:
152 if p in lparts[1:]:
153 pos = lparts.index(p)
153 pos = lparts.index(p)
154 base = os.path.join(*parts[:pos])
154 base = os.path.join(*parts[:pos])
155 raise util.Abort(_("path '%s' is inside nested repo %r")
155 raise util.Abort(_("path '%s' is inside nested repo %r")
156 % (path, base))
156 % (path, base))
157
157
158 normparts = util.splitpath(normpath)
158 normparts = util.splitpath(normpath)
159 assert len(parts) == len(normparts)
159 assert len(parts) == len(normparts)
160
160
161 parts.pop()
161 parts.pop()
162 normparts.pop()
162 normparts.pop()
163 prefixes = []
163 prefixes = []
164 while parts:
164 while parts:
165 prefix = os.sep.join(parts)
165 prefix = os.sep.join(parts)
166 normprefix = os.sep.join(normparts)
166 normprefix = os.sep.join(normparts)
167 if normprefix in self.auditeddir:
167 if normprefix in self.auditeddir:
168 break
168 break
169 curpath = os.path.join(self.root, prefix)
169 curpath = os.path.join(self.root, prefix)
170 try:
170 try:
171 st = os.lstat(curpath)
171 st = os.lstat(curpath)
172 except OSError, err:
172 except OSError, err:
173 # EINVAL can be raised as invalid path syntax under win32.
173 # EINVAL can be raised as invalid path syntax under win32.
174 # They must be ignored for patterns can be checked too.
174 # They must be ignored for patterns can be checked too.
175 if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL):
175 if err.errno not in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL):
176 raise
176 raise
177 else:
177 else:
178 if stat.S_ISLNK(st.st_mode):
178 if stat.S_ISLNK(st.st_mode):
179 raise util.Abort(
179 raise util.Abort(
180 _('path %r traverses symbolic link %r')
180 _('path %r traverses symbolic link %r')
181 % (path, prefix))
181 % (path, prefix))
182 elif (stat.S_ISDIR(st.st_mode) and
182 elif (stat.S_ISDIR(st.st_mode) and
183 os.path.isdir(os.path.join(curpath, '.hg'))):
183 os.path.isdir(os.path.join(curpath, '.hg'))):
184 if not self.callback or not self.callback(curpath):
184 if not self.callback or not self.callback(curpath):
185 raise util.Abort(_("path '%s' is inside nested "
185 raise util.Abort(_("path '%s' is inside nested "
186 "repo %r")
186 "repo %r")
187 % (path, prefix))
187 % (path, prefix))
188 prefixes.append(normprefix)
188 prefixes.append(normprefix)
189 parts.pop()
189 parts.pop()
190 normparts.pop()
190 normparts.pop()
191
191
192 self.audited.add(normpath)
192 self.audited.add(normpath)
193 # only add prefixes to the cache after checking everything: we don't
193 # only add prefixes to the cache after checking everything: we don't
194 # want to add "foo/bar/baz" before checking if there's a "foo/.hg"
194 # want to add "foo/bar/baz" before checking if there's a "foo/.hg"
195 self.auditeddir.update(prefixes)
195 self.auditeddir.update(prefixes)
196
196
197 def check(self, path):
197 def check(self, path):
198 try:
198 try:
199 self(path)
199 self(path)
200 return True
200 return True
201 except (OSError, util.Abort):
201 except (OSError, util.Abort):
202 return False
202 return False
203
203
204 class abstractvfs(object):
204 class abstractvfs(object):
205 """Abstract base class; cannot be instantiated"""
205 """Abstract base class; cannot be instantiated"""
206
206
207 def __init__(self, *args, **kwargs):
207 def __init__(self, *args, **kwargs):
208 '''Prevent instantiation; don't call this from subclasses.'''
208 '''Prevent instantiation; don't call this from subclasses.'''
209 raise NotImplementedError('attempted instantiating ' + str(type(self)))
209 raise NotImplementedError('attempted instantiating ' + str(type(self)))
210
210
211 def tryread(self, path):
211 def tryread(self, path):
212 '''gracefully return an empty string for missing files'''
212 '''gracefully return an empty string for missing files'''
213 try:
213 try:
214 return self.read(path)
214 return self.read(path)
215 except IOError, inst:
215 except IOError, inst:
216 if inst.errno != errno.ENOENT:
216 if inst.errno != errno.ENOENT:
217 raise
217 raise
218 return ""
218 return ""
219
219
220 def read(self, path):
220 def read(self, path):
221 fp = self(path, 'rb')
221 fp = self(path, 'rb')
222 try:
222 try:
223 return fp.read()
223 return fp.read()
224 finally:
224 finally:
225 fp.close()
225 fp.close()
226
226
227 def write(self, path, data):
227 def write(self, path, data):
228 fp = self(path, 'wb')
228 fp = self(path, 'wb')
229 try:
229 try:
230 return fp.write(data)
230 return fp.write(data)
231 finally:
231 finally:
232 fp.close()
232 fp.close()
233
233
234 def append(self, path, data):
234 def append(self, path, data):
235 fp = self(path, 'ab')
235 fp = self(path, 'ab')
236 try:
236 try:
237 return fp.write(data)
237 return fp.write(data)
238 finally:
238 finally:
239 fp.close()
239 fp.close()
240
240
241 def exists(self, path=None):
241 def exists(self, path=None):
242 return os.path.exists(self.join(path))
242 return os.path.exists(self.join(path))
243
243
244 def isdir(self, path=None):
244 def isdir(self, path=None):
245 return os.path.isdir(self.join(path))
245 return os.path.isdir(self.join(path))
246
246
247 def islink(self, path=None):
247 def islink(self, path=None):
248 return os.path.islink(self.join(path))
248 return os.path.islink(self.join(path))
249
249
250 def makedir(self, path=None, notindexed=True):
250 def makedir(self, path=None, notindexed=True):
251 return util.makedir(self.join(path), notindexed)
251 return util.makedir(self.join(path), notindexed)
252
252
253 def makedirs(self, path=None, mode=None):
253 def makedirs(self, path=None, mode=None):
254 return util.makedirs(self.join(path), mode)
254 return util.makedirs(self.join(path), mode)
255
255
256 def mkdir(self, path=None):
256 def mkdir(self, path=None):
257 return os.mkdir(self.join(path))
257 return os.mkdir(self.join(path))
258
258
259 def readdir(self, path=None, stat=None, skip=None):
259 def readdir(self, path=None, stat=None, skip=None):
260 return osutil.listdir(self.join(path), stat, skip)
260 return osutil.listdir(self.join(path), stat, skip)
261
261
262 def rename(self, src, dst):
262 def rename(self, src, dst):
263 return util.rename(self.join(src), self.join(dst))
263 return util.rename(self.join(src), self.join(dst))
264
264
265 def readlink(self, path):
265 def readlink(self, path):
266 return os.readlink(self.join(path))
266 return os.readlink(self.join(path))
267
267
268 def setflags(self, path, l, x):
268 def setflags(self, path, l, x):
269 return util.setflags(self.join(path), l, x)
269 return util.setflags(self.join(path), l, x)
270
270
271 def stat(self, path=None):
271 def stat(self, path=None):
272 return os.stat(self.join(path))
272 return os.stat(self.join(path))
273
273
274 class vfs(abstractvfs):
274 class vfs(abstractvfs):
275 '''Operate files relative to a base directory
275 '''Operate files relative to a base directory
276
276
277 This class is used to hide the details of COW semantics and
277 This class is used to hide the details of COW semantics and
278 remote file access from higher level code.
278 remote file access from higher level code.
279 '''
279 '''
280 def __init__(self, base, audit=True, expandpath=False, realpath=False):
280 def __init__(self, base, audit=True, expandpath=False, realpath=False):
281 if expandpath:
281 if expandpath:
282 base = util.expandpath(base)
282 base = util.expandpath(base)
283 if realpath:
283 if realpath:
284 base = os.path.realpath(base)
284 base = os.path.realpath(base)
285 self.base = base
285 self.base = base
286 self._setmustaudit(audit)
286 self._setmustaudit(audit)
287 self.createmode = None
287 self.createmode = None
288 self._trustnlink = None
288 self._trustnlink = None
289
289
290 def _getmustaudit(self):
290 def _getmustaudit(self):
291 return self._audit
291 return self._audit
292
292
293 def _setmustaudit(self, onoff):
293 def _setmustaudit(self, onoff):
294 self._audit = onoff
294 self._audit = onoff
295 if onoff:
295 if onoff:
296 self.audit = pathauditor(self.base)
296 self.audit = pathauditor(self.base)
297 else:
297 else:
298 self.audit = util.always
298 self.audit = util.always
299
299
300 mustaudit = property(_getmustaudit, _setmustaudit)
300 mustaudit = property(_getmustaudit, _setmustaudit)
301
301
302 @util.propertycache
302 @util.propertycache
303 def _cansymlink(self):
303 def _cansymlink(self):
304 return util.checklink(self.base)
304 return util.checklink(self.base)
305
305
306 @util.propertycache
306 @util.propertycache
307 def _chmod(self):
307 def _chmod(self):
308 return util.checkexec(self.base)
308 return util.checkexec(self.base)
309
309
310 def _fixfilemode(self, name):
310 def _fixfilemode(self, name):
311 if self.createmode is None or not self._chmod:
311 if self.createmode is None or not self._chmod:
312 return
312 return
313 os.chmod(name, self.createmode & 0666)
313 os.chmod(name, self.createmode & 0666)
314
314
315 def __call__(self, path, mode="r", text=False, atomictemp=False):
315 def __call__(self, path, mode="r", text=False, atomictemp=False):
316 if self._audit:
316 if self._audit:
317 r = util.checkosfilename(path)
317 r = util.checkosfilename(path)
318 if r:
318 if r:
319 raise util.Abort("%s: %r" % (r, path))
319 raise util.Abort("%s: %r" % (r, path))
320 self.audit(path)
320 self.audit(path)
321 f = self.join(path)
321 f = self.join(path)
322
322
323 if not text and "b" not in mode:
323 if not text and "b" not in mode:
324 mode += "b" # for that other OS
324 mode += "b" # for that other OS
325
325
326 nlink = -1
326 nlink = -1
327 if mode not in ('r', 'rb'):
327 if mode not in ('r', 'rb'):
328 dirname, basename = util.split(f)
328 dirname, basename = util.split(f)
329 # If basename is empty, then the path is malformed because it points
329 # If basename is empty, then the path is malformed because it points
330 # to a directory. Let the posixfile() call below raise IOError.
330 # to a directory. Let the posixfile() call below raise IOError.
331 if basename:
331 if basename:
332 if atomictemp:
332 if atomictemp:
333 util.ensuredirs(dirname, self.createmode)
333 util.ensuredirs(dirname, self.createmode)
334 return util.atomictempfile(f, mode, self.createmode)
334 return util.atomictempfile(f, mode, self.createmode)
335 try:
335 try:
336 if 'w' in mode:
336 if 'w' in mode:
337 util.unlink(f)
337 util.unlink(f)
338 nlink = 0
338 nlink = 0
339 else:
339 else:
340 # nlinks() may behave differently for files on Windows
340 # nlinks() may behave differently for files on Windows
341 # shares if the file is open.
341 # shares if the file is open.
342 fd = util.posixfile(f)
342 fd = util.posixfile(f)
343 nlink = util.nlinks(f)
343 nlink = util.nlinks(f)
344 if nlink < 1:
344 if nlink < 1:
345 nlink = 2 # force mktempcopy (issue1922)
345 nlink = 2 # force mktempcopy (issue1922)
346 fd.close()
346 fd.close()
347 except (OSError, IOError), e:
347 except (OSError, IOError), e:
348 if e.errno != errno.ENOENT:
348 if e.errno != errno.ENOENT:
349 raise
349 raise
350 nlink = 0
350 nlink = 0
351 util.ensuredirs(dirname, self.createmode)
351 util.ensuredirs(dirname, self.createmode)
352 if nlink > 0:
352 if nlink > 0:
353 if self._trustnlink is None:
353 if self._trustnlink is None:
354 self._trustnlink = nlink > 1 or util.checknlink(f)
354 self._trustnlink = nlink > 1 or util.checknlink(f)
355 if nlink > 1 or not self._trustnlink:
355 if nlink > 1 or not self._trustnlink:
356 util.rename(util.mktempcopy(f), f)
356 util.rename(util.mktempcopy(f), f)
357 fp = util.posixfile(f, mode)
357 fp = util.posixfile(f, mode)
358 if nlink == 0:
358 if nlink == 0:
359 self._fixfilemode(f)
359 self._fixfilemode(f)
360 return fp
360 return fp
361
361
362 def symlink(self, src, dst):
362 def symlink(self, src, dst):
363 self.audit(dst)
363 self.audit(dst)
364 linkname = self.join(dst)
364 linkname = self.join(dst)
365 try:
365 try:
366 os.unlink(linkname)
366 os.unlink(linkname)
367 except OSError:
367 except OSError:
368 pass
368 pass
369
369
370 util.ensuredirs(os.path.dirname(linkname), self.createmode)
370 util.ensuredirs(os.path.dirname(linkname), self.createmode)
371
371
372 if self._cansymlink:
372 if self._cansymlink:
373 try:
373 try:
374 os.symlink(src, linkname)
374 os.symlink(src, linkname)
375 except OSError, err:
375 except OSError, err:
376 raise OSError(err.errno, _('could not symlink to %r: %s') %
376 raise OSError(err.errno, _('could not symlink to %r: %s') %
377 (src, err.strerror), linkname)
377 (src, err.strerror), linkname)
378 else:
378 else:
379 self.write(dst, src)
379 self.write(dst, src)
380
380
381 def join(self, path):
381 def join(self, path):
382 if path:
382 if path:
383 return os.path.join(self.base, path)
383 return os.path.join(self.base, path)
384 else:
384 else:
385 return self.base
385 return self.base
386
386
387 opener = vfs
387 opener = vfs
388
388
389 class auditvfs(object):
389 class auditvfs(object):
390 def __init__(self, vfs):
390 def __init__(self, vfs):
391 self.vfs = vfs
391 self.vfs = vfs
392
392
393 def _getmustaudit(self):
393 def _getmustaudit(self):
394 return self.vfs.mustaudit
394 return self.vfs.mustaudit
395
395
396 def _setmustaudit(self, onoff):
396 def _setmustaudit(self, onoff):
397 self.vfs.mustaudit = onoff
397 self.vfs.mustaudit = onoff
398
398
399 mustaudit = property(_getmustaudit, _setmustaudit)
399 mustaudit = property(_getmustaudit, _setmustaudit)
400
400
401 class filtervfs(abstractvfs, auditvfs):
401 class filtervfs(abstractvfs, auditvfs):
402 '''Wrapper vfs for filtering filenames with a function.'''
402 '''Wrapper vfs for filtering filenames with a function.'''
403
403
404 def __init__(self, vfs, filter):
404 def __init__(self, vfs, filter):
405 auditvfs.__init__(self, vfs)
405 auditvfs.__init__(self, vfs)
406 self._filter = filter
406 self._filter = filter
407
407
408 def __call__(self, path, *args, **kwargs):
408 def __call__(self, path, *args, **kwargs):
409 return self.vfs(self._filter(path), *args, **kwargs)
409 return self.vfs(self._filter(path), *args, **kwargs)
410
410
411 def join(self, path):
411 def join(self, path):
412 if path:
412 if path:
413 return self.vfs.join(self._filter(path))
413 return self.vfs.join(self._filter(path))
414 else:
414 else:
415 return self.vfs.join(path)
415 return self.vfs.join(path)
416
416
417 filteropener = filtervfs
417 filteropener = filtervfs
418
418
419 class readonlyvfs(abstractvfs, auditvfs):
419 class readonlyvfs(abstractvfs, auditvfs):
420 '''Wrapper vfs preventing any writing.'''
420 '''Wrapper vfs preventing any writing.'''
421
421
422 def __init__(self, vfs):
422 def __init__(self, vfs):
423 auditvfs.__init__(self, vfs)
423 auditvfs.__init__(self, vfs)
424
424
425 def __call__(self, path, mode='r', *args, **kw):
425 def __call__(self, path, mode='r', *args, **kw):
426 if mode not in ('r', 'rb'):
426 if mode not in ('r', 'rb'):
427 raise util.Abort('this vfs is read only')
427 raise util.Abort('this vfs is read only')
428 return self.vfs(path, mode, *args, **kw)
428 return self.vfs(path, mode, *args, **kw)
429
429
430
430
431 def canonpath(root, cwd, myname, auditor=None):
431 def canonpath(root, cwd, myname, auditor=None):
432 '''return the canonical path of myname, given cwd and root'''
432 '''return the canonical path of myname, given cwd and root'''
433 if util.endswithsep(root):
433 if util.endswithsep(root):
434 rootsep = root
434 rootsep = root
435 else:
435 else:
436 rootsep = root + os.sep
436 rootsep = root + os.sep
437 name = myname
437 name = myname
438 if not os.path.isabs(name):
438 if not os.path.isabs(name):
439 name = os.path.join(root, cwd, name)
439 name = os.path.join(root, cwd, name)
440 name = os.path.normpath(name)
440 name = os.path.normpath(name)
441 if auditor is None:
441 if auditor is None:
442 auditor = pathauditor(root)
442 auditor = pathauditor(root)
443 if name != rootsep and name.startswith(rootsep):
443 if name != rootsep and name.startswith(rootsep):
444 name = name[len(rootsep):]
444 name = name[len(rootsep):]
445 auditor(name)
445 auditor(name)
446 return util.pconvert(name)
446 return util.pconvert(name)
447 elif name == root:
447 elif name == root:
448 return ''
448 return ''
449 else:
449 else:
450 # Determine whether `name' is in the hierarchy at or beneath `root',
450 # Determine whether `name' is in the hierarchy at or beneath `root',
451 # by iterating name=dirname(name) until that causes no change (can't
451 # by iterating name=dirname(name) until that causes no change (can't
452 # check name == '/', because that doesn't work on windows). The list
452 # check name == '/', because that doesn't work on windows). The list
453 # `rel' holds the reversed list of components making up the relative
453 # `rel' holds the reversed list of components making up the relative
454 # file name we want.
454 # file name we want.
455 rel = []
455 rel = []
456 while True:
456 while True:
457 try:
457 try:
458 s = util.samefile(name, root)
458 s = util.samefile(name, root)
459 except OSError:
459 except OSError:
460 s = False
460 s = False
461 if s:
461 if s:
462 if not rel:
462 if not rel:
463 # name was actually the same as root (maybe a symlink)
463 # name was actually the same as root (maybe a symlink)
464 return ''
464 return ''
465 rel.reverse()
465 rel.reverse()
466 name = os.path.join(*rel)
466 name = os.path.join(*rel)
467 auditor(name)
467 auditor(name)
468 return util.pconvert(name)
468 return util.pconvert(name)
469 dirname, basename = util.split(name)
469 dirname, basename = util.split(name)
470 rel.append(basename)
470 rel.append(basename)
471 if dirname == name:
471 if dirname == name:
472 break
472 break
473 name = dirname
473 name = dirname
474
474
475 raise util.Abort(_("%s not under root '%s'") % (myname, root))
475 raise util.Abort(_("%s not under root '%s'") % (myname, root))
476
476
477 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
477 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
478 '''yield every hg repository under path, always recursively.
478 '''yield every hg repository under path, always recursively.
479 The recurse flag will only control recursion into repo working dirs'''
479 The recurse flag will only control recursion into repo working dirs'''
480 def errhandler(err):
480 def errhandler(err):
481 if err.filename == path:
481 if err.filename == path:
482 raise err
482 raise err
483 samestat = getattr(os.path, 'samestat', None)
483 samestat = getattr(os.path, 'samestat', None)
484 if followsym and samestat is not None:
484 if followsym and samestat is not None:
485 def adddir(dirlst, dirname):
485 def adddir(dirlst, dirname):
486 match = False
486 match = False
487 dirstat = os.stat(dirname)
487 dirstat = os.stat(dirname)
488 for lstdirstat in dirlst:
488 for lstdirstat in dirlst:
489 if samestat(dirstat, lstdirstat):
489 if samestat(dirstat, lstdirstat):
490 match = True
490 match = True
491 break
491 break
492 if not match:
492 if not match:
493 dirlst.append(dirstat)
493 dirlst.append(dirstat)
494 return not match
494 return not match
495 else:
495 else:
496 followsym = False
496 followsym = False
497
497
498 if (seen_dirs is None) and followsym:
498 if (seen_dirs is None) and followsym:
499 seen_dirs = []
499 seen_dirs = []
500 adddir(seen_dirs, path)
500 adddir(seen_dirs, path)
501 for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
501 for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
502 dirs.sort()
502 dirs.sort()
503 if '.hg' in dirs:
503 if '.hg' in dirs:
504 yield root # found a repository
504 yield root # found a repository
505 qroot = os.path.join(root, '.hg', 'patches')
505 qroot = os.path.join(root, '.hg', 'patches')
506 if os.path.isdir(os.path.join(qroot, '.hg')):
506 if os.path.isdir(os.path.join(qroot, '.hg')):
507 yield qroot # we have a patch queue repo here
507 yield qroot # we have a patch queue repo here
508 if recurse:
508 if recurse:
509 # avoid recursing inside the .hg directory
509 # avoid recursing inside the .hg directory
510 dirs.remove('.hg')
510 dirs.remove('.hg')
511 else:
511 else:
512 dirs[:] = [] # don't descend further
512 dirs[:] = [] # don't descend further
513 elif followsym:
513 elif followsym:
514 newdirs = []
514 newdirs = []
515 for d in dirs:
515 for d in dirs:
516 fname = os.path.join(root, d)
516 fname = os.path.join(root, d)
517 if adddir(seen_dirs, fname):
517 if adddir(seen_dirs, fname):
518 if os.path.islink(fname):
518 if os.path.islink(fname):
519 for hgname in walkrepos(fname, True, seen_dirs):
519 for hgname in walkrepos(fname, True, seen_dirs):
520 yield hgname
520 yield hgname
521 else:
521 else:
522 newdirs.append(d)
522 newdirs.append(d)
523 dirs[:] = newdirs
523 dirs[:] = newdirs
524
524
525 def osrcpath():
525 def osrcpath():
526 '''return default os-specific hgrc search path'''
526 '''return default os-specific hgrc search path'''
527 path = systemrcpath()
527 path = systemrcpath()
528 path.extend(userrcpath())
528 path.extend(userrcpath())
529 path = [os.path.normpath(f) for f in path]
529 path = [os.path.normpath(f) for f in path]
530 return path
530 return path
531
531
532 _rcpath = None
532 _rcpath = None
533
533
534 def rcpath():
534 def rcpath():
535 '''return hgrc search path. if env var HGRCPATH is set, use it.
535 '''return hgrc search path. if env var HGRCPATH is set, use it.
536 for each item in path, if directory, use files ending in .rc,
536 for each item in path, if directory, use files ending in .rc,
537 else use item.
537 else use item.
538 make HGRCPATH empty to only look in .hg/hgrc of current repo.
538 make HGRCPATH empty to only look in .hg/hgrc of current repo.
539 if no HGRCPATH, use default os-specific path.'''
539 if no HGRCPATH, use default os-specific path.'''
540 global _rcpath
540 global _rcpath
541 if _rcpath is None:
541 if _rcpath is None:
542 if 'HGRCPATH' in os.environ:
542 if 'HGRCPATH' in os.environ:
543 _rcpath = []
543 _rcpath = []
544 for p in os.environ['HGRCPATH'].split(os.pathsep):
544 for p in os.environ['HGRCPATH'].split(os.pathsep):
545 if not p:
545 if not p:
546 continue
546 continue
547 p = util.expandpath(p)
547 p = util.expandpath(p)
548 if os.path.isdir(p):
548 if os.path.isdir(p):
549 for f, kind in osutil.listdir(p):
549 for f, kind in osutil.listdir(p):
550 if f.endswith('.rc'):
550 if f.endswith('.rc'):
551 _rcpath.append(os.path.join(p, f))
551 _rcpath.append(os.path.join(p, f))
552 else:
552 else:
553 _rcpath.append(p)
553 _rcpath.append(p)
554 else:
554 else:
555 _rcpath = osrcpath()
555 _rcpath = osrcpath()
556 return _rcpath
556 return _rcpath
557
557
558 def revsingle(repo, revspec, default='.'):
558 def revsingle(repo, revspec, default='.'):
559 if not revspec:
559 if not revspec:
560 return repo[default]
560 return repo[default]
561
561
562 l = revrange(repo, [revspec])
562 l = revrange(repo, [revspec])
563 if len(l) < 1:
563 if len(l) < 1:
564 raise util.Abort(_('empty revision set'))
564 raise util.Abort(_('empty revision set'))
565 return repo[l[-1]]
565 return repo[l[-1]]
566
566
567 def revpair(repo, revs):
567 def revpair(repo, revs):
568 if not revs:
568 if not revs:
569 return repo.dirstate.p1(), None
569 return repo.dirstate.p1(), None
570
570
571 l = revrange(repo, revs)
571 l = revrange(repo, revs)
572
572
573 if len(l) == 0:
573 if len(l) == 0:
574 if revs:
574 if revs:
575 raise util.Abort(_('empty revision range'))
575 raise util.Abort(_('empty revision range'))
576 return repo.dirstate.p1(), None
576 return repo.dirstate.p1(), None
577
577
578 if len(l) == 1 and len(revs) == 1 and _revrangesep not in revs[0]:
578 if len(l) == 1 and len(revs) == 1 and _revrangesep not in revs[0]:
579 return repo.lookup(l[0]), None
579 return repo.lookup(l[0]), None
580
580
581 return repo.lookup(l[0]), repo.lookup(l[-1])
581 return repo.lookup(l[0]), repo.lookup(l[-1])
582
582
583 _revrangesep = ':'
583 _revrangesep = ':'
584
584
585 def revrange(repo, revs):
585 def revrange(repo, revs):
586 """Yield revision as strings from a list of revision specifications."""
586 """Yield revision as strings from a list of revision specifications."""
587
587
588 def revfix(repo, val, defval):
588 def revfix(repo, val, defval):
589 if not val and val != 0 and defval is not None:
589 if not val and val != 0 and defval is not None:
590 return defval
590 return defval
591 return repo[val].rev()
591 return repo[val].rev()
592
592
593 seen, l = set(), []
593 seen, l = set(), []
594 for spec in revs:
594 for spec in revs:
595 if l and not seen:
595 if l and not seen:
596 seen = set(l)
596 seen = set(l)
597 # attempt to parse old-style ranges first to deal with
597 # attempt to parse old-style ranges first to deal with
598 # things like old-tag which contain query metacharacters
598 # things like old-tag which contain query metacharacters
599 try:
599 try:
600 if isinstance(spec, int):
600 if isinstance(spec, int):
601 seen.add(spec)
601 seen.add(spec)
602 l.append(spec)
602 l.append(spec)
603 continue
603 continue
604
604
605 if _revrangesep in spec:
605 if _revrangesep in spec:
606 start, end = spec.split(_revrangesep, 1)
606 start, end = spec.split(_revrangesep, 1)
607 start = revfix(repo, start, 0)
607 start = revfix(repo, start, 0)
608 end = revfix(repo, end, len(repo) - 1)
608 end = revfix(repo, end, len(repo) - 1)
609 if end == nullrev and start <= 0:
609 if end == nullrev and start <= 0:
610 start = nullrev
610 start = nullrev
611 rangeiter = repo.changelog.revs(start, end)
611 rangeiter = repo.changelog.revs(start, end)
612 if not seen and not l:
612 if not seen and not l:
613 # by far the most common case: revs = ["-1:0"]
613 # by far the most common case: revs = ["-1:0"]
614 l = list(rangeiter)
614 l = list(rangeiter)
615 # defer syncing seen until next iteration
615 # defer syncing seen until next iteration
616 continue
616 continue
617 newrevs = set(rangeiter)
617 newrevs = set(rangeiter)
618 if seen:
618 if seen:
619 newrevs.difference_update(seen)
619 newrevs.difference_update(seen)
620 seen.update(newrevs)
620 seen.update(newrevs)
621 else:
621 else:
622 seen = newrevs
622 seen = newrevs
623 l.extend(sorted(newrevs, reverse=start > end))
623 l.extend(sorted(newrevs, reverse=start > end))
624 continue
624 continue
625 elif spec and spec in repo: # single unquoted rev
625 elif spec and spec in repo: # single unquoted rev
626 rev = revfix(repo, spec, None)
626 rev = revfix(repo, spec, None)
627 if rev in seen:
627 if rev in seen:
628 continue
628 continue
629 seen.add(rev)
629 seen.add(rev)
630 l.append(rev)
630 l.append(rev)
631 continue
631 continue
632 except error.RepoLookupError:
632 except error.RepoLookupError:
633 pass
633 pass
634
634
635 # fall through to new-style queries if old-style fails
635 # fall through to new-style queries if old-style fails
636 m = revset.match(repo.ui, spec)
636 m = revset.match(repo.ui, spec)
637 dl = [r for r in m(repo, list(repo)) if r not in seen]
637 dl = [r for r in m(repo, list(repo)) if r not in seen]
638 l.extend(dl)
638 l.extend(dl)
639 seen.update(dl)
639 seen.update(dl)
640
640
641 return l
641 return l
642
642
643 def expandpats(pats):
643 def expandpats(pats):
644 if not util.expandglobs:
644 if not util.expandglobs:
645 return list(pats)
645 return list(pats)
646 ret = []
646 ret = []
647 for p in pats:
647 for p in pats:
648 kind, name = matchmod._patsplit(p, None)
648 kind, name = matchmod._patsplit(p, None)
649 if kind is None:
649 if kind is None:
650 try:
650 try:
651 globbed = glob.glob(name)
651 globbed = glob.glob(name)
652 except re.error:
652 except re.error:
653 globbed = [name]
653 globbed = [name]
654 if globbed:
654 if globbed:
655 ret.extend(globbed)
655 ret.extend(globbed)
656 continue
656 continue
657 ret.append(p)
657 ret.append(p)
658 return ret
658 return ret
659
659
660 def matchandpats(ctx, pats=[], opts={}, globbed=False, default='relpath'):
660 def matchandpats(ctx, pats=[], opts={}, globbed=False, default='relpath'):
661 if pats == ("",):
661 if pats == ("",):
662 pats = []
662 pats = []
663 if not globbed and default == 'relpath':
663 if not globbed and default == 'relpath':
664 pats = expandpats(pats or [])
664 pats = expandpats(pats or [])
665
665
666 m = ctx.match(pats, opts.get('include'), opts.get('exclude'),
666 m = ctx.match(pats, opts.get('include'), opts.get('exclude'),
667 default)
667 default)
668 def badfn(f, msg):
668 def badfn(f, msg):
669 ctx._repo.ui.warn("%s: %s\n" % (m.rel(f), msg))
669 ctx._repo.ui.warn("%s: %s\n" % (m.rel(f), msg))
670 m.bad = badfn
670 m.bad = badfn
671 return m, pats
671 return m, pats
672
672
673 def match(ctx, pats=[], opts={}, globbed=False, default='relpath'):
673 def match(ctx, pats=[], opts={}, globbed=False, default='relpath'):
674 return matchandpats(ctx, pats, opts, globbed, default)[0]
674 return matchandpats(ctx, pats, opts, globbed, default)[0]
675
675
676 def matchall(repo):
676 def matchall(repo):
677 return matchmod.always(repo.root, repo.getcwd())
677 return matchmod.always(repo.root, repo.getcwd())
678
678
679 def matchfiles(repo, files):
679 def matchfiles(repo, files):
680 return matchmod.exact(repo.root, repo.getcwd(), files)
680 return matchmod.exact(repo.root, repo.getcwd(), files)
681
681
682 def addremove(repo, pats=[], opts={}, dry_run=None, similarity=None):
682 def addremove(repo, pats=[], opts={}, dry_run=None, similarity=None):
683 if dry_run is None:
683 if dry_run is None:
684 dry_run = opts.get('dry_run')
684 dry_run = opts.get('dry_run')
685 if similarity is None:
685 if similarity is None:
686 similarity = float(opts.get('similarity') or 0)
686 similarity = float(opts.get('similarity') or 0)
687 # we'd use status here, except handling of symlinks and ignore is tricky
687 # we'd use status here, except handling of symlinks and ignore is tricky
688 added, unknown, deleted, removed = [], [], [], []
689 audit_path = pathauditor(repo.root)
690 m = match(repo[None], pats, opts)
688 m = match(repo[None], pats, opts)
691 rejected = []
689 rejected = []
692 m.bad = lambda x, y: rejected.append(x)
690 m.bad = lambda x, y: rejected.append(x)
693
691
694 ctx = repo[None]
692 added, unknown, deleted, removed = _interestingfiles(repo, m)
695 dirstate = repo.dirstate
696 walkresults = dirstate.walk(m, sorted(ctx.substate), True, False)
697 for abs, st in walkresults.iteritems():
698 dstate = dirstate[abs]
699 if dstate == '?' and audit_path.check(abs):
700 unknown.append(abs)
701 elif dstate != 'r' and not st:
702 deleted.append(abs)
703 # for finding renames
704 elif dstate == 'r':
705 removed.append(abs)
706 elif dstate == 'a':
707 added.append(abs)
708
693
709 unknownset = set(unknown)
694 unknownset = set(unknown)
710 toprint = unknownset.copy()
695 toprint = unknownset.copy()
711 toprint.update(deleted)
696 toprint.update(deleted)
712 for abs in sorted(toprint):
697 for abs in sorted(toprint):
713 if repo.ui.verbose or not m.exact(abs):
698 if repo.ui.verbose or not m.exact(abs):
714 rel = m.rel(abs)
699 rel = m.rel(abs)
715 if abs in unknownset:
700 if abs in unknownset:
716 status = _('adding %s\n') % ((pats and rel) or abs)
701 status = _('adding %s\n') % ((pats and rel) or abs)
717 else:
702 else:
718 status = _('removing %s\n') % ((pats and rel) or abs)
703 status = _('removing %s\n') % ((pats and rel) or abs)
719 repo.ui.status(status)
704 repo.ui.status(status)
720
705
721 copies = {}
706 copies = {}
722 if similarity > 0:
707 if similarity > 0:
723 for old, new, score in similar.findrenames(repo,
708 for old, new, score in similar.findrenames(repo,
724 added + unknown, removed + deleted, similarity):
709 added + unknown, removed + deleted, similarity):
725 if repo.ui.verbose or not m.exact(old) or not m.exact(new):
710 if repo.ui.verbose or not m.exact(old) or not m.exact(new):
726 repo.ui.status(_('recording removal of %s as rename to %s '
711 repo.ui.status(_('recording removal of %s as rename to %s '
727 '(%d%% similar)\n') %
712 '(%d%% similar)\n') %
728 (m.rel(old), m.rel(new), score * 100))
713 (m.rel(old), m.rel(new), score * 100))
729 copies[new] = old
714 copies[new] = old
730
715
731 if not dry_run:
716 if not dry_run:
732 wctx = repo[None]
717 wctx = repo[None]
733 wlock = repo.wlock()
718 wlock = repo.wlock()
734 try:
719 try:
735 wctx.forget(deleted)
720 wctx.forget(deleted)
736 wctx.add(unknown)
721 wctx.add(unknown)
737 for new, old in copies.iteritems():
722 for new, old in copies.iteritems():
738 wctx.copy(old, new)
723 wctx.copy(old, new)
739 finally:
724 finally:
740 wlock.release()
725 wlock.release()
741
726
742 for f in rejected:
727 for f in rejected:
743 if f in m.files():
728 if f in m.files():
744 return 1
729 return 1
745 return 0
730 return 0
746
731
732 def _interestingfiles(repo, matcher):
733 '''Walk dirstate with matcher, looking for files that addremove would care
734 about.
735
736 This is different from dirstate.status because it doesn't care about
737 whether files are modified or clean.'''
738 added, unknown, deleted, removed = [], [], [], []
739 audit_path = pathauditor(repo.root)
740
741 ctx = repo[None]
742 dirstate = repo.dirstate
743 walkresults = dirstate.walk(matcher, sorted(ctx.substate), True, False)
744 for abs, st in walkresults.iteritems():
745 dstate = dirstate[abs]
746 if dstate == '?' and audit_path.check(abs):
747 unknown.append(abs)
748 elif dstate != 'r' and not st:
749 deleted.append(abs)
750 # for finding renames
751 elif dstate == 'r':
752 removed.append(abs)
753 elif dstate == 'a':
754 added.append(abs)
755
756 return added, unknown, deleted, removed
757
747 def dirstatecopy(ui, repo, wctx, src, dst, dryrun=False, cwd=None):
758 def dirstatecopy(ui, repo, wctx, src, dst, dryrun=False, cwd=None):
748 """Update the dirstate to reflect the intent of copying src to dst. For
759 """Update the dirstate to reflect the intent of copying src to dst. For
749 different reasons it might not end with dst being marked as copied from src.
760 different reasons it might not end with dst being marked as copied from src.
750 """
761 """
751 origsrc = repo.dirstate.copied(src) or src
762 origsrc = repo.dirstate.copied(src) or src
752 if dst == origsrc: # copying back a copy?
763 if dst == origsrc: # copying back a copy?
753 if repo.dirstate[dst] not in 'mn' and not dryrun:
764 if repo.dirstate[dst] not in 'mn' and not dryrun:
754 repo.dirstate.normallookup(dst)
765 repo.dirstate.normallookup(dst)
755 else:
766 else:
756 if repo.dirstate[origsrc] == 'a' and origsrc == src:
767 if repo.dirstate[origsrc] == 'a' and origsrc == src:
757 if not ui.quiet:
768 if not ui.quiet:
758 ui.warn(_("%s has not been committed yet, so no copy "
769 ui.warn(_("%s has not been committed yet, so no copy "
759 "data will be stored for %s.\n")
770 "data will be stored for %s.\n")
760 % (repo.pathto(origsrc, cwd), repo.pathto(dst, cwd)))
771 % (repo.pathto(origsrc, cwd), repo.pathto(dst, cwd)))
761 if repo.dirstate[dst] in '?r' and not dryrun:
772 if repo.dirstate[dst] in '?r' and not dryrun:
762 wctx.add([dst])
773 wctx.add([dst])
763 elif not dryrun:
774 elif not dryrun:
764 wctx.copy(origsrc, dst)
775 wctx.copy(origsrc, dst)
765
776
766 def readrequires(opener, supported):
777 def readrequires(opener, supported):
767 '''Reads and parses .hg/requires and checks if all entries found
778 '''Reads and parses .hg/requires and checks if all entries found
768 are in the list of supported features.'''
779 are in the list of supported features.'''
769 requirements = set(opener.read("requires").splitlines())
780 requirements = set(opener.read("requires").splitlines())
770 missings = []
781 missings = []
771 for r in requirements:
782 for r in requirements:
772 if r not in supported:
783 if r not in supported:
773 if not r or not r[0].isalnum():
784 if not r or not r[0].isalnum():
774 raise error.RequirementError(_(".hg/requires file is corrupt"))
785 raise error.RequirementError(_(".hg/requires file is corrupt"))
775 missings.append(r)
786 missings.append(r)
776 missings.sort()
787 missings.sort()
777 if missings:
788 if missings:
778 raise error.RequirementError(
789 raise error.RequirementError(
779 _("unknown repository format: requires features '%s' (upgrade "
790 _("unknown repository format: requires features '%s' (upgrade "
780 "Mercurial)") % "', '".join(missings))
791 "Mercurial)") % "', '".join(missings))
781 return requirements
792 return requirements
782
793
783 class filecacheentry(object):
794 class filecacheentry(object):
784 def __init__(self, path, stat=True):
795 def __init__(self, path, stat=True):
785 self.path = path
796 self.path = path
786 self.cachestat = None
797 self.cachestat = None
787 self._cacheable = None
798 self._cacheable = None
788
799
789 if stat:
800 if stat:
790 self.cachestat = filecacheentry.stat(self.path)
801 self.cachestat = filecacheentry.stat(self.path)
791
802
792 if self.cachestat:
803 if self.cachestat:
793 self._cacheable = self.cachestat.cacheable()
804 self._cacheable = self.cachestat.cacheable()
794 else:
805 else:
795 # None means we don't know yet
806 # None means we don't know yet
796 self._cacheable = None
807 self._cacheable = None
797
808
798 def refresh(self):
809 def refresh(self):
799 if self.cacheable():
810 if self.cacheable():
800 self.cachestat = filecacheentry.stat(self.path)
811 self.cachestat = filecacheentry.stat(self.path)
801
812
802 def cacheable(self):
813 def cacheable(self):
803 if self._cacheable is not None:
814 if self._cacheable is not None:
804 return self._cacheable
815 return self._cacheable
805
816
806 # we don't know yet, assume it is for now
817 # we don't know yet, assume it is for now
807 return True
818 return True
808
819
809 def changed(self):
820 def changed(self):
810 # no point in going further if we can't cache it
821 # no point in going further if we can't cache it
811 if not self.cacheable():
822 if not self.cacheable():
812 return True
823 return True
813
824
814 newstat = filecacheentry.stat(self.path)
825 newstat = filecacheentry.stat(self.path)
815
826
816 # we may not know if it's cacheable yet, check again now
827 # we may not know if it's cacheable yet, check again now
817 if newstat and self._cacheable is None:
828 if newstat and self._cacheable is None:
818 self._cacheable = newstat.cacheable()
829 self._cacheable = newstat.cacheable()
819
830
820 # check again
831 # check again
821 if not self._cacheable:
832 if not self._cacheable:
822 return True
833 return True
823
834
824 if self.cachestat != newstat:
835 if self.cachestat != newstat:
825 self.cachestat = newstat
836 self.cachestat = newstat
826 return True
837 return True
827 else:
838 else:
828 return False
839 return False
829
840
830 @staticmethod
841 @staticmethod
831 def stat(path):
842 def stat(path):
832 try:
843 try:
833 return util.cachestat(path)
844 return util.cachestat(path)
834 except OSError, e:
845 except OSError, e:
835 if e.errno != errno.ENOENT:
846 if e.errno != errno.ENOENT:
836 raise
847 raise
837
848
838 class filecache(object):
849 class filecache(object):
839 '''A property like decorator that tracks a file under .hg/ for updates.
850 '''A property like decorator that tracks a file under .hg/ for updates.
840
851
841 Records stat info when called in _filecache.
852 Records stat info when called in _filecache.
842
853
843 On subsequent calls, compares old stat info with new info, and recreates
854 On subsequent calls, compares old stat info with new info, and recreates
844 the object when needed, updating the new stat info in _filecache.
855 the object when needed, updating the new stat info in _filecache.
845
856
846 Mercurial either atomic renames or appends for files under .hg,
857 Mercurial either atomic renames or appends for files under .hg,
847 so to ensure the cache is reliable we need the filesystem to be able
858 so to ensure the cache is reliable we need the filesystem to be able
848 to tell us if a file has been replaced. If it can't, we fallback to
859 to tell us if a file has been replaced. If it can't, we fallback to
849 recreating the object on every call (essentially the same behaviour as
860 recreating the object on every call (essentially the same behaviour as
850 propertycache).'''
861 propertycache).'''
851 def __init__(self, path):
862 def __init__(self, path):
852 self.path = path
863 self.path = path
853
864
854 def join(self, obj, fname):
865 def join(self, obj, fname):
855 """Used to compute the runtime path of the cached file.
866 """Used to compute the runtime path of the cached file.
856
867
857 Users should subclass filecache and provide their own version of this
868 Users should subclass filecache and provide their own version of this
858 function to call the appropriate join function on 'obj' (an instance
869 function to call the appropriate join function on 'obj' (an instance
859 of the class that its member function was decorated).
870 of the class that its member function was decorated).
860 """
871 """
861 return obj.join(fname)
872 return obj.join(fname)
862
873
863 def __call__(self, func):
874 def __call__(self, func):
864 self.func = func
875 self.func = func
865 self.name = func.__name__
876 self.name = func.__name__
866 return self
877 return self
867
878
868 def __get__(self, obj, type=None):
879 def __get__(self, obj, type=None):
869 # do we need to check if the file changed?
880 # do we need to check if the file changed?
870 if self.name in obj.__dict__:
881 if self.name in obj.__dict__:
871 assert self.name in obj._filecache, self.name
882 assert self.name in obj._filecache, self.name
872 return obj.__dict__[self.name]
883 return obj.__dict__[self.name]
873
884
874 entry = obj._filecache.get(self.name)
885 entry = obj._filecache.get(self.name)
875
886
876 if entry:
887 if entry:
877 if entry.changed():
888 if entry.changed():
878 entry.obj = self.func(obj)
889 entry.obj = self.func(obj)
879 else:
890 else:
880 path = self.join(obj, self.path)
891 path = self.join(obj, self.path)
881
892
882 # We stat -before- creating the object so our cache doesn't lie if
893 # We stat -before- creating the object so our cache doesn't lie if
883 # a writer modified between the time we read and stat
894 # a writer modified between the time we read and stat
884 entry = filecacheentry(path)
895 entry = filecacheentry(path)
885 entry.obj = self.func(obj)
896 entry.obj = self.func(obj)
886
897
887 obj._filecache[self.name] = entry
898 obj._filecache[self.name] = entry
888
899
889 obj.__dict__[self.name] = entry.obj
900 obj.__dict__[self.name] = entry.obj
890 return entry.obj
901 return entry.obj
891
902
892 def __set__(self, obj, value):
903 def __set__(self, obj, value):
893 if self.name not in obj._filecache:
904 if self.name not in obj._filecache:
894 # we add an entry for the missing value because X in __dict__
905 # we add an entry for the missing value because X in __dict__
895 # implies X in _filecache
906 # implies X in _filecache
896 ce = filecacheentry(self.join(obj, self.path), False)
907 ce = filecacheentry(self.join(obj, self.path), False)
897 obj._filecache[self.name] = ce
908 obj._filecache[self.name] = ce
898 else:
909 else:
899 ce = obj._filecache[self.name]
910 ce = obj._filecache[self.name]
900
911
901 ce.obj = value # update cached copy
912 ce.obj = value # update cached copy
902 obj.__dict__[self.name] = value # update copy returned by obj.x
913 obj.__dict__[self.name] = value # update copy returned by obj.x
903
914
904 def __delete__(self, obj):
915 def __delete__(self, obj):
905 try:
916 try:
906 del obj.__dict__[self.name]
917 del obj.__dict__[self.name]
907 except KeyError:
918 except KeyError:
908 raise AttributeError(self.name)
919 raise AttributeError(self.name)
909
920
910 class dirs(object):
921 class dirs(object):
911 '''a multiset of directory names from a dirstate or manifest'''
922 '''a multiset of directory names from a dirstate or manifest'''
912
923
913 def __init__(self, map, skip=None):
924 def __init__(self, map, skip=None):
914 self._dirs = {}
925 self._dirs = {}
915 addpath = self.addpath
926 addpath = self.addpath
916 if util.safehasattr(map, 'iteritems') and skip is not None:
927 if util.safehasattr(map, 'iteritems') and skip is not None:
917 for f, s in map.iteritems():
928 for f, s in map.iteritems():
918 if s[0] != skip:
929 if s[0] != skip:
919 addpath(f)
930 addpath(f)
920 else:
931 else:
921 for f in map:
932 for f in map:
922 addpath(f)
933 addpath(f)
923
934
924 def addpath(self, path):
935 def addpath(self, path):
925 dirs = self._dirs
936 dirs = self._dirs
926 for base in finddirs(path):
937 for base in finddirs(path):
927 if base in dirs:
938 if base in dirs:
928 dirs[base] += 1
939 dirs[base] += 1
929 return
940 return
930 dirs[base] = 1
941 dirs[base] = 1
931
942
932 def delpath(self, path):
943 def delpath(self, path):
933 dirs = self._dirs
944 dirs = self._dirs
934 for base in finddirs(path):
945 for base in finddirs(path):
935 if dirs[base] > 1:
946 if dirs[base] > 1:
936 dirs[base] -= 1
947 dirs[base] -= 1
937 return
948 return
938 del dirs[base]
949 del dirs[base]
939
950
940 def __iter__(self):
951 def __iter__(self):
941 return self._dirs.iterkeys()
952 return self._dirs.iterkeys()
942
953
943 def __contains__(self, d):
954 def __contains__(self, d):
944 return d in self._dirs
955 return d in self._dirs
945
956
946 if util.safehasattr(parsers, 'dirs'):
957 if util.safehasattr(parsers, 'dirs'):
947 dirs = parsers.dirs
958 dirs = parsers.dirs
948
959
949 def finddirs(path):
960 def finddirs(path):
950 pos = path.rfind('/')
961 pos = path.rfind('/')
951 while pos != -1:
962 while pos != -1:
952 yield path[:pos]
963 yield path[:pos]
953 pos = path.rfind('/', 0, pos)
964 pos = path.rfind('/', 0, pos)
General Comments 0
You need to be logged in to leave comments. Login now