##// END OF EJS Templates
templater: do not process \-escapes at parsestring() (issue4290)...
templater: do not process \-escapes at parsestring() (issue4290) This patch brings back pre-2.8.1 behavior. The result of parsestring() is stored in templater's cache, t.cache, and then it is parsed as a template string by compiletemplate(). So t.cache should keep an unparsed string no matter if it is sourced from config value. Otherwise backslashes would be processed twice. The test vector is borrowed from 64b4f0cd7336.

File last commit:

r24934:5abd0a76 default
r24948:db7463aa stable
Show More
scmutil.py
1160 lines | 37.6 KiB | text/x-python | PythonLexer
Adrian Buehlmann
add: introduce a warning message for non-portable filenames (issue2756) (BC)...
r13962 # scmutil.py - Mercurial core utility functions
#
# Copyright Matt Mackall <mpm@selenic.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from i18n import _
Sean Farley
log: remove any ancestors of nullrev (issue3772)...
r18466 from mercurial.node import nullrev
Drew Gottlieb
util: move dirs() and finddirs() from scmutil to util...
r24635 import util, error, osutil, revset, similar, encoding, phases
Augie Fackler
pathutil: tease out a new library to break an import cycle from canonpath use
r20033 import pathutil
Matt Mackall
scmutil: fold in wdutil
r14320 import match as matchmod
Pierre-Yves David
develwarn: include call site in the simple message version...
r24749 import os, errno, re, glob, tempfile, shutil, stat, inspect
Kevin Bullock
scmutil: split platform-specific bits into their own modules...
r18690
if os.name == 'nt':
import scmwindows as scmplatform
else:
import scmposix as scmplatform
systemrcpath = scmplatform.systemrcpath
userrcpath = scmplatform.userrcpath
Adrian Buehlmann
add: introduce a warning message for non-portable filenames (issue2756) (BC)...
r13962
Martin von Zweigbergk
status: create class for status lists...
r22913 class status(tuple):
'''Named tuple with a list of files per status. The 'deleted', 'unknown'
and 'ignored' properties are only relevant to the working copy.
'''
__slots__ = ()
def __new__(cls, modified, added, removed, deleted, unknown, ignored,
clean):
return tuple.__new__(cls, (modified, added, removed, deleted, unknown,
ignored, clean))
@property
def modified(self):
Martin von Zweigbergk
status: update and move documentation of status types to status class...
r22915 '''files that have been modified'''
Martin von Zweigbergk
status: create class for status lists...
r22913 return self[0]
@property
def added(self):
Martin von Zweigbergk
status: update and move documentation of status types to status class...
r22915 '''files that have been added'''
Martin von Zweigbergk
status: create class for status lists...
r22913 return self[1]
@property
def removed(self):
Martin von Zweigbergk
status: update and move documentation of status types to status class...
r22915 '''files that have been removed'''
Martin von Zweigbergk
status: create class for status lists...
r22913 return self[2]
@property
def deleted(self):
Martin von Zweigbergk
status: update and move documentation of status types to status class...
r22915 '''files that are in the dirstate, but have been deleted from the
working copy (aka "missing")
'''
Martin von Zweigbergk
status: create class for status lists...
r22913 return self[3]
@property
def unknown(self):
Martin von Zweigbergk
status: update and move documentation of status types to status class...
r22915 '''files not in the dirstate that are not ignored'''
Martin von Zweigbergk
status: create class for status lists...
r22913 return self[4]
@property
def ignored(self):
Martin von Zweigbergk
status: update and move documentation of status types to status class...
r22915 '''files not in the dirstate that are ignored (by _dirignore())'''
Martin von Zweigbergk
status: create class for status lists...
r22913 return self[5]
@property
def clean(self):
Martin von Zweigbergk
status: update and move documentation of status types to status class...
r22915 '''files that have not been modified'''
Martin von Zweigbergk
status: create class for status lists...
r22913 return self[6]
def __repr__(self, *args, **kwargs):
return (('<status modified=%r, added=%r, removed=%r, deleted=%r, '
'unknown=%r, ignored=%r, clean=%r>') % self)
Augie Fackler
itersubrepos: move to scmutil to break a direct import cycle
r20392 def itersubrepos(ctx1, ctx2):
"""find subrepos in ctx1 or ctx2"""
# Create a (subpath, ctx) mapping where we prefer subpaths from
# ctx1. The subpaths from ctx2 are important when the .hgsub file
# has been modified (in ctx2) but not yet committed (in ctx1).
subpaths = dict.fromkeys(ctx2.substate, ctx2)
subpaths.update(dict.fromkeys(ctx1.substate, ctx1))
for subpath, ctx in sorted(subpaths.iteritems()):
yield subpath, ctx.sub(subpath)
Patrick Mezard
discovery: add extinct changesets to outgoing.excluded...
r17248 def nochangesfound(ui, repo, excluded=None):
'''Report no changes for push/pull, excluded is None or a list of
nodes excluded from the push/pull.
'''
secretlist = []
if excluded:
for n in excluded:
Pierre-Yves David
outgoing: fix possible filtering crash in outgoing (issue3814)...
r18617 if n not in repo:
# discovery should not have included the filtered revision,
# we have to explicitly exclude it until discovery is cleanup.
continue
Patrick Mezard
discovery: add extinct changesets to outgoing.excluded...
r17248 ctx = repo[n]
if ctx.phase() >= phases.secret and not ctx.extinct():
secretlist.append(n)
Matt Mackall
scmutil: unify some 'no changes found' messages...
r15993 if secretlist:
ui.status(_("no changes found (ignored %d secret changesets)\n")
% len(secretlist))
else:
ui.status(_("no changes found\n"))
Kevin Bullock
scmutil: add bad character checking to checknewlabel...
r17821 def checknewlabel(repo, lbl, kind):
Durham Goode
translations: change label integer error to not specify the kind of label...
r19070 # Do not use the "kind" parameter in ui output.
# It makes strings difficult to translate.
Kevin Bullock
scmutil: add function to validate new branch, tag, and bookmark names...
r17817 if lbl in ['tip', '.', 'null']:
raise util.Abort(_("the name '%s' is reserved") % lbl)
Kevin Bullock
scmutil: add bad character checking to checknewlabel...
r17821 for c in (':', '\0', '\n', '\r'):
if c in lbl:
Wagner Bruna
scmutil: generalize message to make it more i18n-friendly
r17850 raise util.Abort(_("%r cannot be used in a name") % c)
Durham Goode
bookmark: don't allow integers as bookmark/branch/tag names...
r18566 try:
int(lbl)
Durham Goode
translations: change label integer error to not specify the kind of label...
r19070 raise util.Abort(_("cannot use an integer as a name"))
Durham Goode
bookmark: don't allow integers as bookmark/branch/tag names...
r18566 except ValueError:
pass
Kevin Bullock
scmutil: add function to validate new branch, tag, and bookmark names...
r17817
Adrian Buehlmann
move checkfilename from util to scmutil...
r13974 def checkfilename(f):
'''Check that the filename f is an acceptable filename for a tracked file'''
if '\r' in f or '\n' in f:
raise util.Abort(_("'\\n' and '\\r' disallowed in filenames: %r") % f)
Adrian Buehlmann
add: introduce a warning message for non-portable filenames (issue2756) (BC)...
r13962 def checkportable(ui, f):
'''Check if filename f is portable and warn or abort depending on config'''
Adrian Buehlmann
move checkfilename from util to scmutil...
r13974 checkfilename(f)
Adrian Buehlmann
scmutil: introduce casecollisionauditor...
r14138 abort, warn = checkportabilityalert(ui)
if abort or warn:
Adrian Buehlmann
add: introduce a warning message for non-portable filenames (issue2756) (BC)...
r13962 msg = util.checkwinfilename(f)
if msg:
Adrian Buehlmann
scmutil: introduce casecollisionauditor...
r14138 msg = "%s: %r" % (msg, f)
if abort:
raise util.Abort(msg)
ui.warn(_("warning: %s\n") % msg)
Kevin Gessner
add: notify when adding a file that would cause a case-folding collision...
r14068
Kevin Gessner
scmutil: refactor ui.portablefilenames processing...
r14067 def checkportabilityalert(ui):
'''check if the user's config requests nothing, a warning, or abort for
non-portable filenames'''
val = ui.config('ui', 'portablefilenames', 'warn')
lval = val.lower()
bval = util.parsebool(val)
abort = os.name == 'nt' or lval == 'abort'
warn = bval or lval == 'warn'
if bval is None and not (warn or abort or lval == 'ignore'):
Adrian Buehlmann
add: introduce a warning message for non-portable filenames (issue2756) (BC)...
r13962 raise error.ConfigError(
_("ui.portablefilenames value is invalid ('%s')") % val)
Kevin Gessner
scmutil: refactor ui.portablefilenames processing...
r14067 return abort, warn
Adrian Buehlmann
scmutil: introduce casecollisionauditor...
r14138 class casecollisionauditor(object):
Joshua Redstone
scmutil: 25% speedup in casecollisionauditor...
r17201 def __init__(self, ui, abort, dirstate):
Adrian Buehlmann
scmutil: introduce casecollisionauditor...
r14138 self._ui = ui
self._abort = abort
Joshua Redstone
scmutil: 25% speedup in casecollisionauditor...
r17201 allfiles = '\0'.join(dirstate._map)
self._loweredfiles = set(encoding.lower(allfiles).split('\0'))
self._dirstate = dirstate
# The purpose of _newfiles is so that we don't complain about
# case collisions if someone were to call this object with the
# same filename twice.
self._newfiles = set()
Kevin Gessner
scmutil: refactor ui.portablefilenames processing...
r14067
Adrian Buehlmann
scmutil: introduce casecollisionauditor...
r14138 def __call__(self, f):
FUJIWARA Katsunori
scmutil: skip checks in "casecollisionauditor" if filename is already checked...
r20006 if f in self._newfiles:
return
FUJIWARA Katsunori
i18n: use UTF-8 string to lower filename for case collision check...
r14980 fl = encoding.lower(f)
FUJIWARA Katsunori
scmutil: skip checks in "casecollisionauditor" if filename is already checked...
r20006 if fl in self._loweredfiles and f not in self._dirstate:
Adrian Buehlmann
scmutil: introduce casecollisionauditor...
r14138 msg = _('possible case-folding collision for %s') % f
if self._abort:
raise util.Abort(msg)
self._ui.warn(_("warning: %s\n") % msg)
Joshua Redstone
scmutil: 25% speedup in casecollisionauditor...
r17201 self._loweredfiles.add(fl)
self._newfiles.add(f)
Adrian Buehlmann
move opener from util to scmutil
r13970
Pierre-Yves David
develwarn: refactor the developer warning logic...
r24747 def develwarn(tui, msg):
"""issue a developer warning message"""
Pierre-Yves David
devel-warn: add a prefix to all messages ("devel-warn: ")...
r24755 msg = 'devel-warn: ' + msg
Pierre-Yves David
develwarn: refactor the developer warning logic...
r24747 if tui.tracebackflag:
util.debugstacktrace(msg, 2)
else:
Pierre-Yves David
develwarn: include call site in the simple message version...
r24749 curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
tui.write_err('%s at: %s:%s (%s)\n' % ((msg,) + calframe[2][1:4]))
Pierre-Yves David
develwarn: refactor the developer warning logic...
r24747
Gregory Szorc
repoview: move function for computing filtered hash...
r24723 def filteredhash(repo, maxrev):
"""build hash of filtered revisions in the current repoview.
Multiple caches perform up-to-date validation by checking that the
tiprev and tipnode stored in the cache file match the current repository.
However, this is not sufficient for validating repoviews because the set
of revisions in the view may change without the repository tiprev and
tipnode changing.
This function hashes all the revs filtered from the view and returns
that SHA-1 digest.
"""
cl = repo.changelog
if not cl.filteredrevs:
return None
key = None
revs = sorted(r for r in cl.filteredrevs if r <= maxrev)
if revs:
s = util.sha1()
for rev in revs:
s.update('%s;' % rev)
key = s.digest()
return key
FUJIWARA Katsunori
scmutil: rename classes from "opener" to "vfs"...
r17649 class abstractvfs(object):
Dan Villiom Podlaski Christiansen
opener: introduce an abstact superclass of it...
r14089 """Abstract base class; cannot be instantiated"""
def __init__(self, *args, **kwargs):
'''Prevent instantiation; don't call this from subclasses.'''
raise NotImplementedError('attempted instantiating ' + str(type(self)))
Matt Mackall
opener: introduce tryread helper...
r16455 def tryread(self, path):
Thomas Arendsen Hein
opener: coding style, use triple quotes for doc string
r16479 '''gracefully return an empty string for missing files'''
Matt Mackall
opener: introduce tryread helper...
r16455 try:
return self.read(path)
except IOError, inst:
if inst.errno != errno.ENOENT:
raise
return ""
FUJIWARA Katsunori
vfs: add "readlines" and "tryreadlines"...
r23368 def tryreadlines(self, path, mode='rb'):
'''gracefully return an empty array for missing files'''
try:
return self.readlines(path, mode=mode)
except IOError, inst:
if inst.errno != errno.ENOENT:
raise
return []
FUJIWARA Katsunori
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode...
r23370 def open(self, path, mode="r", text=False, atomictemp=False,
notindexed=False):
'''Open ``path`` file, which is relative to vfs root.
Newly created directories are marked as "not to be indexed by
the content indexing service", if ``notindexed`` is specified
for "write" mode access.
'''
FUJIWARA Katsunori
vfs: add "open()" for newly added code paths which open files via vfs...
r19897 self.open = self.__call__
FUJIWARA Katsunori
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode...
r23370 return self.__call__(path, mode, text, atomictemp, notindexed)
FUJIWARA Katsunori
vfs: add "open()" for newly added code paths which open files via vfs...
r19897
Dan Villiom Podlaski Christiansen
util & scmutil: adapt read/write helpers as request by mpm
r14167 def read(self, path):
fp = self(path, 'rb')
Dan Villiom Podlaski Christiansen
opener: add read & write utility methods...
r14097 try:
return fp.read()
finally:
fp.close()
FUJIWARA Katsunori
vfs: add "readlines" and "tryreadlines"...
r23368 def readlines(self, path, mode='rb'):
fp = self(path, mode=mode)
try:
return fp.readlines()
finally:
fp.close()
Dan Villiom Podlaski Christiansen
util & scmutil: adapt read/write helpers as request by mpm
r14167 def write(self, path, data):
fp = self(path, 'wb')
try:
return fp.write(data)
finally:
fp.close()
FUJIWARA Katsunori
vfs: add "writelines"...
r23371 def writelines(self, path, data, mode='wb', notindexed=False):
fp = self(path, mode=mode, notindexed=notindexed)
try:
return fp.writelines(data)
finally:
fp.close()
Dan Villiom Podlaski Christiansen
util & scmutil: adapt read/write helpers as request by mpm
r14167 def append(self, path, data):
fp = self(path, 'ab')
Dan Villiom Podlaski Christiansen
opener: add read & write utility methods...
r14097 try:
return fp.write(data)
finally:
fp.close()
FUJIWARA Katsunori
vfs: add "chmod()"
r20086 def chmod(self, path, mode):
return os.chmod(self.join(path), mode)
FUJIWARA Katsunori
localrepo: use file API via vfs while ensuring repository directory...
r17161 def exists(self, path=None):
return os.path.exists(self.join(path))
FUJIWARA Katsunori
changelog: use "vfs.fstat()" instead of "util.fstat()"...
r19899 def fstat(self, fp):
return util.fstat(fp)
FUJIWARA Katsunori
localrepo: use file API via vfs while ensuring repository directory...
r17161 def isdir(self, path=None):
return os.path.isdir(self.join(path))
FUJIWARA Katsunori
vfs: add "isfile()"
r20085 def isfile(self, path=None):
return os.path.isfile(self.join(path))
FUJIWARA Katsunori
localrepo: use "vfs.islink()" instead of "os.path.islink()"
r18949 def islink(self, path=None):
return os.path.islink(self.join(path))
Pierre-Yves David
vfs: add a 'reljoin' function for joining relative paths...
r23581 def reljoin(self, *paths):
"""join various elements of a path together (as os.path.join would do)
The vfs base is not injected so that path stay relative. This exists
to allow handling of strange encoding if needed."""
return os.path.join(*paths)
Pierre-Yves David
vfs: add a 'split' method...
r23582 def split(self, path):
"""split top-most element of a path (as os.path.split would do)
This exists to allow handling of strange encoding if needed."""
return os.path.split(path)
Chinmay Joshi
vfs: add lexists() in current api...
r21563 def lexists(self, path=None):
return os.path.lexists(self.join(path))
FUJIWARA Katsunori
context: use "vfs.lstat()" instead of "os.lstat()"...
r19900 def lstat(self, path=None):
return os.lstat(self.join(path))
Chinmay Joshi
vfs: add listdir for os.listdir in vfs...
r21799 def listdir(self, path=None):
return os.listdir(self.join(path))
FUJIWARA Katsunori
localrepo: use file API via vfs while ensuring repository directory...
r17161 def makedir(self, path=None, notindexed=True):
return util.makedir(self.join(path), notindexed)
def makedirs(self, path=None, mode=None):
return util.makedirs(self.join(path), mode)
FUJIWARA Katsunori
vfs: add "makelock()" and "readlock()"
r20090 def makelock(self, info, path):
return util.makelock(info, self.join(path))
FUJIWARA Katsunori
scmutil: reorder newly added functions for vfs support in dictionary order...
r17723 def mkdir(self, path=None):
return os.mkdir(self.join(path))
FUJIWARA Katsunori
vfs: add "mkstemp()"
r20980 def mkstemp(self, suffix='', prefix='tmp', dir=None, text=False):
fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
dir=self.join(dir), text=text)
dname, fname = util.split(name)
if dir:
return fd, os.path.join(dir, fname)
else:
return fd, fname
FUJIWARA Katsunori
store: invoke "osutil.listdir()" via vfs...
r17747 def readdir(self, path=None, stat=None, skip=None):
return osutil.listdir(self.join(path), stat, skip)
FUJIWARA Katsunori
vfs: add "makelock()" and "readlock()"
r20090 def readlock(self, path):
return util.readlock(self.join(path))
FUJIWARA Katsunori
localrepo: use "vfs.rename()" instead of "util.rename()"
r18948 def rename(self, src, dst):
return util.rename(self.join(src), self.join(dst))
FUJIWARA Katsunori
localrepo: use "vfs.readlink()" instead of "os.readlink()"
r18950 def readlink(self, path):
return os.readlink(self.join(path))
FUJIWARA Katsunori
vfs: add removedirs
r24693 def removedirs(self, path=None):
"""Remove a leaf directory and all empty intermediate ones
"""
return util.removedirs(self.join(path))
FUJIWARA Katsunori
vfs: add rmtree...
r24689 def rmtree(self, path=None, ignore_errors=False, forcibly=False):
"""Remove a directory tree recursively
If ``forcibly``, this tries to remove READ-ONLY files, too.
"""
if forcibly:
def onerror(function, path, excinfo):
if function is not os.remove:
raise
# read-only files cannot be unlinked under Windows
s = os.stat(path)
if (s.st_mode & stat.S_IWRITE) != 0:
raise
os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE)
os.remove(path)
else:
onerror = None
return shutil.rmtree(self.join(path),
ignore_errors=ignore_errors, onerror=onerror)
FUJIWARA Katsunori
localrepo: use "vfs.setflags()" instead of "util.setflags()"
r18951 def setflags(self, path, l, x):
return util.setflags(self.join(path), l, x)
FUJIWARA Katsunori
store: invoke "os.stat()" for "createmode" initialization via vfs...
r17726 def stat(self, path=None):
return os.stat(self.join(path))
FUJIWARA Katsunori
bookmarks: use "vfs.unlink()" instead of "util.unlink()"
r19895 def unlink(self, path=None):
return util.unlink(self.join(path))
Chinmay Joshi
vfs: add unlinkpath to vfs...
r21716 def unlinkpath(self, path=None, ignoremissing=False):
return util.unlinkpath(self.join(path), ignoremissing)
FUJIWARA Katsunori
bookmarks: use "vfs.utime()" instead of "os.utime()"
r19896 def utime(self, path=None, t=None):
return os.utime(self.join(path), t)
FUJIWARA Katsunori
vfs: add walk...
r24725 def walk(self, path=None, onerror=None):
"""Yield (dirpath, dirs, files) tuple for each directories under path
``dirpath`` is relative one from the root of this vfs. This
uses ``os.sep`` as path separator, even you specify POSIX
style ``path``.
"The root of this vfs" is represented as empty ``dirpath``.
"""
root = os.path.normpath(self.join(None))
# when dirpath == root, dirpath[prefixlen:] becomes empty
# because len(dirpath) < prefixlen.
prefixlen = len(pathutil.normasprefix(root))
for dirpath, dirs, files in os.walk(self.join(path), onerror=onerror):
yield (dirpath[prefixlen:], dirs, files)
FUJIWARA Katsunori
scmutil: rename classes from "opener" to "vfs"...
r17649 class vfs(abstractvfs):
'''Operate files relative to a base directory
Adrian Buehlmann
move opener from util to scmutil
r13970
This class is used to hide the details of COW semantics and
remote file access from higher level code.
'''
FUJIWARA Katsunori
vfs: split "expand" into "realpath"/"expandpath" to apply each separately...
r18945 def __init__(self, base, audit=True, expandpath=False, realpath=False):
if expandpath:
base = util.expandpath(base)
if realpath:
base = os.path.realpath(base)
Adrian Buehlmann
move opener from util to scmutil
r13970 self.base = base
Bryan O'Sullivan
scmutil: turn opener._audit into a property, mustaudit...
r17554 self._setmustaudit(audit)
self.createmode = None
self._trustnlink = None
def _getmustaudit(self):
return self._audit
def _setmustaudit(self, onoff):
self._audit = onoff
if onoff:
Augie Fackler
pathutil: tease out a new library to break an import cycle from canonpath use
r20033 self.audit = pathutil.pathauditor(self.base)
Adrian Buehlmann
move opener from util to scmutil
r13970 else:
Mads Kiilerich
scmutil: simplify vfs.audit - drop wrapped vfs.auditor
r18327 self.audit = util.always
Bryan O'Sullivan
scmutil: turn opener._audit into a property, mustaudit...
r17554
mustaudit = property(_getmustaudit, _setmustaudit)
Adrian Buehlmann
move opener from util to scmutil
r13970
@util.propertycache
Adrian Buehlmann
opener: rename _can_symlink to _cansymlink
r14261 def _cansymlink(self):
Adrian Buehlmann
move opener from util to scmutil
r13970 return util.checklink(self.base)
Matt Mackall
scmutil: don't try to match modes on filesystems without modes (issue3740)
r18192 @util.propertycache
def _chmod(self):
return util.checkexec(self.base)
Matt Mackall
vfs: backout fchmod change from 76b73ce0ffac...
r17763 def _fixfilemode(self, name):
Matt Mackall
scmutil: don't try to match modes on filesystems without modes (issue3740)
r18192 if self.createmode is None or not self._chmod:
Adrian Buehlmann
move opener from util to scmutil
r13970 return
Matt Mackall
vfs: backout fchmod change from 76b73ce0ffac...
r17763 os.chmod(name, self.createmode & 0666)
Adrian Buehlmann
move opener from util to scmutil
r13970
FUJIWARA Katsunori
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode...
r23370 def __call__(self, path, mode="r", text=False, atomictemp=False,
notindexed=False):
'''Open ``path`` file, which is relative to vfs root.
Newly created directories are marked as "not to be indexed by
the content indexing service", if ``notindexed`` is specified
for "write" mode access.
'''
Adrian Buehlmann
opener: add self._audit (issue2862)
r14720 if self._audit:
r = util.checkosfilename(path)
if r:
raise util.Abort("%s: %r" % (r, path))
Mads Kiilerich
scmutil: simplify vfs.audit - drop wrapped vfs.auditor
r18327 self.audit(path)
Idan Kamara
scmutil: add join method to opener to construct path relative to base...
r16199 f = self.join(path)
Adrian Buehlmann
move opener from util to scmutil
r13970
if not text and "b" not in mode:
mode += "b" # for that other OS
nlink = -1
Adrian Buehlmann
vfs: optimize __call__ by not calling util.split for reads...
r17937 if mode not in ('r', 'rb'):
dirname, basename = util.split(f)
# If basename is empty, then the path is malformed because it points
# to a directory. Let the posixfile() call below raise IOError.
if basename:
if atomictemp:
FUJIWARA Katsunori
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode...
r23370 util.ensuredirs(dirname, self.createmode, notindexed)
Adrian Buehlmann
vfs: optimize __call__ by not calling util.split for reads...
r17937 return util.atomictempfile(f, mode, self.createmode)
try:
if 'w' in mode:
util.unlink(f)
nlink = 0
else:
# nlinks() may behave differently for files on Windows
# shares if the file is open.
fd = util.posixfile(f)
nlink = util.nlinks(f)
if nlink < 1:
nlink = 2 # force mktempcopy (issue1922)
fd.close()
except (OSError, IOError), e:
if e.errno != errno.ENOENT:
raise
Adrian Buehlmann
move opener from util to scmutil
r13970 nlink = 0
FUJIWARA Katsunori
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode...
r23370 util.ensuredirs(dirname, self.createmode, notindexed)
Adrian Buehlmann
vfs: optimize __call__ by not calling util.split for reads...
r17937 if nlink > 0:
if self._trustnlink is None:
self._trustnlink = nlink > 1 or util.checknlink(f)
if nlink > 1 or not self._trustnlink:
util.rename(util.mktempcopy(f), f)
Adrian Buehlmann
move opener from util to scmutil
r13970 fp = util.posixfile(f, mode)
if nlink == 0:
Matt Mackall
vfs: backout fchmod change from 76b73ce0ffac...
r17763 self._fixfilemode(f)
Adrian Buehlmann
move opener from util to scmutil
r13970 return fp
def symlink(self, src, dst):
Mads Kiilerich
scmutil: simplify vfs.audit - drop wrapped vfs.auditor
r18327 self.audit(dst)
Idan Kamara
scmutil: add join method to opener to construct path relative to base...
r16199 linkname = self.join(dst)
Adrian Buehlmann
move opener from util to scmutil
r13970 try:
os.unlink(linkname)
except OSError:
pass
Bryan O'Sullivan
util: make ensuredirs safer against races
r18678 util.ensuredirs(os.path.dirname(linkname), self.createmode)
Adrian Buehlmann
move opener from util to scmutil
r13970
Adrian Buehlmann
opener: rename _can_symlink to _cansymlink
r14261 if self._cansymlink:
Adrian Buehlmann
move opener from util to scmutil
r13970 try:
os.symlink(src, linkname)
except OSError, err:
raise OSError(err.errno, _('could not symlink to %r: %s') %
(src, err.strerror), linkname)
else:
Matt Mackall
vfs: use self.write to write symlink placeholders...
r17768 self.write(dst, src)
Adrian Buehlmann
move canonpath from util to scmutil
r13971
Matt Harbison
vfs: make it possible to pass multiple path elements to join...
r24628 def join(self, path, *insidef):
FUJIWARA Katsunori
localrepo: use file API via vfs while ensuring repository directory...
r17161 if path:
Matt Harbison
vfs: make it possible to pass multiple path elements to join...
r24628 return os.path.join(self.base, path, *insidef)
Matt Mackall
scmutil: backout 83785bb56062 (issue3643)
r17681 else:
return self.base
Idan Kamara
scmutil: add join method to opener to construct path relative to base...
r16199
FUJIWARA Katsunori
scmutil: rename classes from "opener" to "vfs"...
r17649 opener = vfs
Bryan O'Sullivan
scmutil: abstract out mustaudit delegation
r17845 class auditvfs(object):
def __init__(self, vfs):
self.vfs = vfs
def _getmustaudit(self):
return self.vfs.mustaudit
def _setmustaudit(self, onoff):
self.vfs.mustaudit = onoff
mustaudit = property(_getmustaudit, _setmustaudit)
Bryan O'Sullivan
scmutil: add mustaudit delegation to filtervfs (issue3673)
r17846 class filtervfs(abstractvfs, auditvfs):
FUJIWARA Katsunori
scmutil: rename classes from "opener" to "vfs"...
r17649 '''Wrapper vfs for filtering filenames with a function.'''
Dan Villiom Podlaski Christiansen
add filteropener abstraction for store openers
r14090
Bryan O'Sullivan
scmutil: add mustaudit delegation to filtervfs (issue3673)
r17846 def __init__(self, vfs, filter):
auditvfs.__init__(self, vfs)
Dan Villiom Podlaski Christiansen
add filteropener abstraction for store openers
r14090 self._filter = filter
def __call__(self, path, *args, **kwargs):
Bryan O'Sullivan
scmutil: add mustaudit delegation to filtervfs (issue3673)
r17846 return self.vfs(self._filter(path), *args, **kwargs)
Dan Villiom Podlaski Christiansen
add filteropener abstraction for store openers
r14090
Matt Harbison
vfs: make it possible to pass multiple path elements to join...
r24628 def join(self, path, *insidef):
FUJIWARA Katsunori
vfs: define "join()" in each classes derived from "abstractvfs"...
r17725 if path:
Matt Harbison
vfs: make it possible to pass multiple path elements to join...
r24628 return self.vfs.join(self._filter(self.vfs.reljoin(path, *insidef)))
FUJIWARA Katsunori
vfs: define "join()" in each classes derived from "abstractvfs"...
r17725 else:
Bryan O'Sullivan
scmutil: add mustaudit delegation to filtervfs (issue3673)
r17846 return self.vfs.join(path)
FUJIWARA Katsunori
vfs: define "join()" in each classes derived from "abstractvfs"...
r17725
FUJIWARA Katsunori
scmutil: rename classes from "opener" to "vfs"...
r17649 filteropener = filtervfs
Pierre-Yves David
vfs: add a read only vfs...
r18213 class readonlyvfs(abstractvfs, auditvfs):
'''Wrapper vfs preventing any writing.'''
def __init__(self, vfs):
auditvfs.__init__(self, vfs)
def __call__(self, path, mode='r', *args, **kw):
if mode not in ('r', 'rb'):
raise util.Abort('this vfs is read only')
return self.vfs(path, mode, *args, **kw)
Adrian Buehlmann
move walkrepos from util to scmutil
r13975 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
Mads Kiilerich
help: improve hgweb help...
r17104 '''yield every hg repository under path, always recursively.
The recurse flag will only control recursion into repo working dirs'''
Adrian Buehlmann
move walkrepos from util to scmutil
r13975 def errhandler(err):
if err.filename == path:
raise err
Augie Fackler
walkrepos: use getattr instead of hasattr for samestat
r14961 samestat = getattr(os.path, 'samestat', None)
if followsym and samestat is not None:
Adrian Buehlmann
scmutil: rename local function _add_dir_if_not_there
r14227 def adddir(dirlst, dirname):
Adrian Buehlmann
move walkrepos from util to scmutil
r13975 match = False
dirstat = os.stat(dirname)
for lstdirstat in dirlst:
if samestat(dirstat, lstdirstat):
match = True
break
if not match:
dirlst.append(dirstat)
return not match
else:
followsym = False
if (seen_dirs is None) and followsym:
seen_dirs = []
Adrian Buehlmann
scmutil: rename local function _add_dir_if_not_there
r14227 adddir(seen_dirs, path)
Adrian Buehlmann
move walkrepos from util to scmutil
r13975 for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
dirs.sort()
if '.hg' in dirs:
yield root # found a repository
qroot = os.path.join(root, '.hg', 'patches')
if os.path.isdir(os.path.join(qroot, '.hg')):
yield qroot # we have a patch queue repo here
if recurse:
# avoid recursing inside the .hg directory
dirs.remove('.hg')
else:
dirs[:] = [] # don't descend further
elif followsym:
newdirs = []
for d in dirs:
fname = os.path.join(root, d)
Adrian Buehlmann
scmutil: rename local function _add_dir_if_not_there
r14227 if adddir(seen_dirs, fname):
Adrian Buehlmann
move walkrepos from util to scmutil
r13975 if os.path.islink(fname):
for hgname in walkrepos(fname, True, seen_dirs):
yield hgname
else:
newdirs.append(d)
dirs[:] = newdirs
Adrian Buehlmann
move rcpath from util to scmutil
r13984
Adrian Buehlmann
rename scmutil.os_rcpath to osrcpath
r14224 def osrcpath():
Adrian Buehlmann
move os_rcpath from util to scmutil
r13985 '''return default os-specific hgrc search path'''
Mads Kiilerich
config: introduce "built-in" default configuration settings in default.d...
r23142 path = []
defaultpath = os.path.join(util.datapath, 'default.d')
if os.path.isdir(defaultpath):
for f, kind in osutil.listdir(defaultpath):
if f.endswith('.rc'):
path.append(os.path.join(defaultpath, f))
path.extend(systemrcpath())
Adrian Buehlmann
rename scmutil.user_rcpath to userrcpath
r14226 path.extend(userrcpath())
Adrian Buehlmann
move os_rcpath from util to scmutil
r13985 path = [os.path.normpath(f) for f in path]
return path
Adrian Buehlmann
move rcpath from util to scmutil
r13984 _rcpath = None
def rcpath():
'''return hgrc search path. if env var HGRCPATH is set, use it.
for each item in path, if directory, use files ending in .rc,
else use item.
make HGRCPATH empty to only look in .hg/hgrc of current repo.
if no HGRCPATH, use default os-specific path.'''
global _rcpath
if _rcpath is None:
if 'HGRCPATH' in os.environ:
_rcpath = []
for p in os.environ['HGRCPATH'].split(os.pathsep):
if not p:
continue
p = util.expandpath(p)
if os.path.isdir(p):
for f, kind in osutil.listdir(p):
if f.endswith('.rc'):
_rcpath.append(os.path.join(p, f))
else:
_rcpath.append(p)
else:
Adrian Buehlmann
rename scmutil.os_rcpath to osrcpath
r14224 _rcpath = osrcpath()
Adrian Buehlmann
move rcpath from util to scmutil
r13984 return _rcpath
Adrian Buehlmann
move system_rcpath and user_rcpath to scmutil
r13986
Yuya Nishihara
scmutil: add function to help handling workingctx in arithmetic operation...
r24582 def intrev(repo, rev):
"""Return integer for a given revision that can be used in comparison or
arithmetic operation"""
if rev is None:
return len(repo)
return rev
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319 def revsingle(repo, revspec, default='.'):
Matt Mackall
revsingle: fix silly API issue (issue2992)
r19509 if not revspec and revspec != 0:
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319 return repo[default]
l = revrange(repo, [revspec])
Pierre-Yves David
revset-limit: use boolean testing instead of `len(revs) < 1`...
r22814 if not l:
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319 raise util.Abort(_('empty revision set'))
Pierre-Yves David
revsingle: use `last` instead of direct indexing...
r22815 return repo[l.last()]
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319
def revpair(repo, revs):
if not revs:
return repo.dirstate.p1(), None
l = revrange(repo, revs)
Pierre-Yves David
revpair: smartset compatibility...
r20862 if not l:
first = second = None
elif l.isascending():
first = l.min()
second = l.max()
elif l.isdescending():
first = l.max()
second = l.min()
else:
Pierre-Yves David
revpair: use `first` and `last` instead of direct indexing...
r22816 first = l.first()
second = l.last()
Pierre-Yves David
revpair: smartset compatibility...
r20862
if first is None:
Pierre-Yves David
revpair: drop useless conditional...
r20819 raise util.Abort(_('empty revision range'))
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319
Pierre-Yves David
revpair: smartset compatibility...
r20862 if first == second and len(revs) == 1 and _revrangesep not in revs[0]:
return repo.lookup(first), None
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319
Pierre-Yves David
revpair: smartset compatibility...
r20862 return repo.lookup(first), repo.lookup(second)
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319
_revrangesep = ':'
def revrange(repo, revs):
"""Yield revision as strings from a list of revision specifications."""
def revfix(repo, val, defval):
if not val and val != 0 and defval is not None:
return defval
Matt Mackall
scmutil: use context instead of lookup
r16379 return repo[val].rev()
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319
Lucas Moscovicz
scmutil: changed revrange code not to use append...
r20559 seen, l = set(), revset.baseset([])
Jordi Gutiérrez Hermoso
revrange: don't parse revset aliases as hash prefixes (issue4553)...
r24175
revsetaliases = [alias for (alias, _) in
repo.ui.configitems("revsetalias")]
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319 for spec in revs:
Bryan O'Sullivan
scmutil: speed up revrange...
r16390 if l and not seen:
seen = set(l)
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319 # attempt to parse old-style ranges first to deal with
# things like old-tag which contain query metacharacters
try:
Jordi Gutiérrez Hermoso
revrange: don't parse revset aliases as hash prefixes (issue4553)...
r24175 # ... except for revset aliases without arguments. These
# should be parsed as soon as possible, because they might
# clash with a hash prefix.
if spec in revsetaliases:
raise error.RepoLookupError
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319 if isinstance(spec, int):
seen.add(spec)
Durham Goode
scmutil: fix revrange when multiple revs are specified...
r20798 l = l + revset.baseset([spec])
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319 continue
if _revrangesep in spec:
start, end = spec.split(_revrangesep, 1)
Jordi Gutiérrez Hermoso
revrange: don't parse revset aliases as hash prefixes (issue4553)...
r24175 if start in revsetaliases or end in revsetaliases:
raise error.RepoLookupError
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319 start = revfix(repo, start, 0)
end = revfix(repo, end, len(repo) - 1)
Cristian Zamfir
hg log: solves bug regarding hg log -r 0:null (issue4039)...
r20699 if end == nullrev and start < 0:
Sean Farley
log: remove any ancestors of nullrev (issue3772)...
r18466 start = nullrev
Pierre-Yves David
clfilter: remove usage of `range` and `xrange` in scmutil.revrange...
r17992 rangeiter = repo.changelog.revs(start, end)
Bryan O'Sullivan
scmutil: speed up revrange...
r16390 if not seen and not l:
# by far the most common case: revs = ["-1:0"]
Lucas Moscovicz
scmutil: changed revrange code not to use append...
r20559 l = revset.baseset(rangeiter)
Bryan O'Sullivan
scmutil: speed up revrange...
r16390 # defer syncing seen until next iteration
continue
Pierre-Yves David
clfilter: remove usage of `range` and `xrange` in scmutil.revrange...
r17992 newrevs = set(rangeiter)
Bryan O'Sullivan
scmutil: speed up revrange...
r16390 if seen:
newrevs.difference_update(seen)
Bryan O'Sullivan
scmutil: seen.union should be seen.update (issue3476)
r16814 seen.update(newrevs)
Bryan O'Sullivan
scmutil: speed up revrange...
r16390 else:
seen = newrevs
Durham Goode
scmutil: fix revrange when multiple revs are specified...
r20798 l = l + revset.baseset(sorted(newrevs, reverse=start > end))
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319 continue
elif spec and spec in repo: # single unquoted rev
rev = revfix(repo, spec, None)
if rev in seen:
continue
seen.add(rev)
Durham Goode
scmutil: fix revrange when multiple revs are specified...
r20798 l = l + revset.baseset([rev])
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319 continue
except error.RepoLookupError:
pass
# fall through to new-style queries if old-style fails
Matt Mackall
revrange: pass repo to revset parser...
r20781 m = revset.match(repo.ui, spec, repo)
Lucas Moscovicz
scmutil: changed revrange to return lazysets for new style revsets...
r20551 if seen or l:
Yuya Nishihara
revset: make match function initiate query from full set by default...
r24114 dl = [r for r in m(repo) if r not in seen]
Durham Goode
scmutil: fix revrange when multiple revs are specified...
r20798 l = l + revset.baseset(dl)
Lucas Moscovicz
scmutil: changed revrange to return lazysets for new style revsets...
r20551 seen.update(dl)
else:
Yuya Nishihara
revset: make match function initiate query from full set by default...
r24114 l = m(repo)
Matt Mackall
scmutil: move revsingle/pair/range from cmdutil...
r14319
return l
Matt Mackall
scmutil: fold in wdutil
r14320
def expandpats(pats):
Mads Kiilerich
match: improve documentation - docstrings and more descriptive variable naming...
r21111 '''Expand bare globs when running on windows.
On posix we assume it already has already been done by sh.'''
Matt Mackall
scmutil: fold in wdutil
r14320 if not util.expandglobs:
return list(pats)
ret = []
Mads Kiilerich
match: improve documentation - docstrings and more descriptive variable naming...
r21111 for kindpat in pats:
kind, pat = matchmod._patsplit(kindpat, None)
Matt Mackall
scmutil: fold in wdutil
r14320 if kind is None:
try:
Mads Kiilerich
match: improve documentation - docstrings and more descriptive variable naming...
r21111 globbed = glob.glob(pat)
Matt Mackall
scmutil: fold in wdutil
r14320 except re.error:
Mads Kiilerich
match: improve documentation - docstrings and more descriptive variable naming...
r21111 globbed = [pat]
Matt Mackall
scmutil: fold in wdutil
r14320 if globbed:
ret.extend(globbed)
continue
Mads Kiilerich
match: improve documentation - docstrings and more descriptive variable naming...
r21111 ret.append(kindpat)
Matt Mackall
scmutil: fold in wdutil
r14320 return ret
Patrick Mezard
graphlog: restore FILE glob expansion on Windows...
r16171 def matchandpats(ctx, pats=[], opts={}, globbed=False, default='relpath'):
Mads Kiilerich
match: improve documentation - docstrings and more descriptive variable naming...
r21111 '''Return a matcher and the patterns that were used.
The matcher will warn about bad matches.'''
Matt Mackall
scmutil: fold in wdutil
r14320 if pats == ("",):
pats = []
if not globbed and default == 'relpath':
pats = expandpats(pats or [])
Matt Mackall
scmutil: match now accepts a context or a repo
r14670
m = ctx.match(pats, opts.get('include'), opts.get('exclude'),
Matt Mackall
context: add a match builder method...
r14669 default)
Matt Mackall
scmutil: fold in wdutil
r14320 def badfn(f, msg):
Matt Harbison
scmutil: replace 'ctx._repo' with 'ctx.repo()'
r24338 ctx.repo().ui.warn("%s: %s\n" % (m.rel(f), msg))
Matt Mackall
scmutil: fold in wdutil
r14320 m.bad = badfn
Martin von Zweigbergk
matcher: make e.g. 'relpath:.' lead to fast paths...
r24447 if m.always():
pats = []
Patrick Mezard
graphlog: restore FILE glob expansion on Windows...
r16171 return m, pats
def match(ctx, pats=[], opts={}, globbed=False, default='relpath'):
Mads Kiilerich
match: improve documentation - docstrings and more descriptive variable naming...
r21111 '''Return a matcher that will warn about bad matches.'''
Patrick Mezard
graphlog: restore FILE glob expansion on Windows...
r16171 return matchandpats(ctx, pats, opts, globbed, default)[0]
Matt Mackall
scmutil: fold in wdutil
r14320
def matchall(repo):
Mads Kiilerich
match: improve documentation - docstrings and more descriptive variable naming...
r21111 '''Return a matcher that will efficiently match everything.'''
Matt Mackall
scmutil: fold in wdutil
r14320 return matchmod.always(repo.root, repo.getcwd())
def matchfiles(repo, files):
Mads Kiilerich
match: improve documentation - docstrings and more descriptive variable naming...
r21111 '''Return a matcher that will efficiently match exactly these files.'''
Matt Mackall
scmutil: fold in wdutil
r14320 return matchmod.exact(repo.root, repo.getcwd(), files)
Matt Harbison
commit: propagate --addremove to subrepos if -S is specified (issue3759)...
r23537 def addremove(repo, matcher, prefix, opts={}, dry_run=None, similarity=None):
Matt Harbison
scmutil: pass a matcher to scmutil.addremove() instead of a list of patterns...
r23533 m = matcher
Matt Mackall
scmutil: fold in wdutil
r14320 if dry_run is None:
dry_run = opts.get('dry_run')
if similarity is None:
similarity = float(opts.get('similarity') or 0)
Matt Harbison
scmutil: pass a matcher to scmutil.addremove() instead of a list of patterns...
r23533
Matt Harbison
commit: propagate --addremove to subrepos if -S is specified (issue3759)...
r23537 ret = 0
join = lambda f: os.path.join(prefix, f)
Matt Harbison
addremove: support addremove with explicit paths in subrepos...
r23539 def matchessubrepo(matcher, subpath):
if matcher.exact(subpath):
return True
for f in matcher.files():
if f.startswith(subpath):
return True
return False
Matt Harbison
commit: propagate --addremove to subrepos if -S is specified (issue3759)...
r23537 wctx = repo[None]
for subpath in sorted(wctx.substate):
Matt Harbison
addremove: support addremove with explicit paths in subrepos...
r23539 if opts.get('subrepos') or matchessubrepo(m, subpath):
Matt Harbison
commit: propagate --addremove to subrepos if -S is specified (issue3759)...
r23537 sub = wctx.sub(subpath)
try:
submatch = matchmod.narrowmatcher(subpath, m)
if sub.addremove(submatch, prefix, opts, dry_run, similarity):
ret = 1
except error.LookupError:
repo.ui.status(_("skipping missing subrepository: %s\n")
% join(subpath))
Matt Mackall
addremove: return 1 if we failed to handle any explicit files
r16167 rejected = []
Matt Harbison
addremove: warn when addremove fails to operate on a named path...
r23534 origbad = m.bad
def badfn(f, msg):
if f in m.files():
origbad(f, msg)
rejected.append(f)
Matt Mackall
addremove: return 1 if we failed to handle any explicit files
r16167
Matt Harbison
addremove: warn when addremove fails to operate on a named path...
r23534 m.bad = badfn
Martin von Zweigbergk
addremove: add back forgotten files (BC)...
r23259 added, unknown, deleted, removed, forgotten = _interestingfiles(repo, m)
Matt Harbison
addremove: warn when addremove fails to operate on a named path...
r23534 m.bad = origbad
Siddharth Agarwal
scmutil.addremove: pull ui.status printing out of the loop...
r18863
Martin von Zweigbergk
addremove: add back forgotten files (BC)...
r23259 unknownset = set(unknown + forgotten)
Siddharth Agarwal
scmutil.addremove: pull ui.status printing out of the loop...
r18863 toprint = unknownset.copy()
toprint.update(deleted)
for abs in sorted(toprint):
if repo.ui.verbose or not m.exact(abs):
if abs in unknownset:
Matt Harbison
narrowmatcher: propagate the rel() method...
r23686 status = _('adding %s\n') % m.uipath(abs)
Siddharth Agarwal
scmutil.addremove: pull ui.status printing out of the loop...
r18863 else:
Matt Harbison
narrowmatcher: propagate the rel() method...
r23686 status = _('removing %s\n') % m.uipath(abs)
Siddharth Agarwal
scmutil.addremove: pull ui.status printing out of the loop...
r18863 repo.ui.status(status)
Siddharth Agarwal
scmutil.addremove: factor out code to find renames...
r19152 renames = _findrenames(repo, m, added + unknown, removed + deleted,
similarity)
Matt Mackall
scmutil: fold in wdutil
r14320
if not dry_run:
Martin von Zweigbergk
addremove: add back forgotten files (BC)...
r23259 _markchanges(repo, unknown + forgotten, deleted, renames)
Matt Mackall
scmutil: fold in wdutil
r14320
Matt Mackall
addremove: return 1 if we failed to handle any explicit files
r16167 for f in rejected:
if f in m.files():
return 1
Matt Harbison
commit: propagate --addremove to subrepos if -S is specified (issue3759)...
r23537 return ret
Matt Mackall
addremove: return 1 if we failed to handle any explicit files
r16167
Siddharth Agarwal
scmutil: add a function to mark that files have been operated on...
r19154 def marktouched(repo, files, similarity=0.0):
'''Assert that files have somehow been operated upon. files are relative to
the repo root.'''
m = matchfiles(repo, files)
rejected = []
m.bad = lambda x, y: rejected.append(x)
Martin von Zweigbergk
addremove: add back forgotten files (BC)...
r23259 added, unknown, deleted, removed, forgotten = _interestingfiles(repo, m)
Siddharth Agarwal
scmutil: add a function to mark that files have been operated on...
r19154
if repo.ui.verbose:
Martin von Zweigbergk
addremove: add back forgotten files (BC)...
r23259 unknownset = set(unknown + forgotten)
Siddharth Agarwal
scmutil: add a function to mark that files have been operated on...
r19154 toprint = unknownset.copy()
toprint.update(deleted)
for abs in sorted(toprint):
if abs in unknownset:
status = _('adding %s\n') % abs
else:
status = _('removing %s\n') % abs
repo.ui.status(status)
renames = _findrenames(repo, m, added + unknown, removed + deleted,
similarity)
Martin von Zweigbergk
addremove: add back forgotten files (BC)...
r23259 _markchanges(repo, unknown + forgotten, deleted, renames)
Siddharth Agarwal
scmutil: add a function to mark that files have been operated on...
r19154
for f in rejected:
if f in m.files():
return 1
return 0
Siddharth Agarwal
scmutil.addremove: factor out dirstate walk into another function...
r19150 def _interestingfiles(repo, matcher):
'''Walk dirstate with matcher, looking for files that addremove would care
about.
This is different from dirstate.status because it doesn't care about
whether files are modified or clean.'''
Martin von Zweigbergk
addremove: add back forgotten files (BC)...
r23259 added, unknown, deleted, removed, forgotten = [], [], [], [], []
Augie Fackler
pathutil: tease out a new library to break an import cycle from canonpath use
r20033 audit_path = pathutil.pathauditor(repo.root)
Siddharth Agarwal
scmutil.addremove: factor out dirstate walk into another function...
r19150
ctx = repo[None]
dirstate = repo.dirstate
Siddharth Agarwal
addremove: don't do full walks...
r19655 walkresults = dirstate.walk(matcher, sorted(ctx.substate), True, False,
full=False)
Siddharth Agarwal
scmutil.addremove: factor out dirstate walk into another function...
r19150 for abs, st in walkresults.iteritems():
dstate = dirstate[abs]
if dstate == '?' and audit_path.check(abs):
unknown.append(abs)
elif dstate != 'r' and not st:
deleted.append(abs)
Martin von Zweigbergk
addremove: add back forgotten files (BC)...
r23259 elif dstate == 'r' and st:
forgotten.append(abs)
Siddharth Agarwal
scmutil.addremove: factor out dirstate walk into another function...
r19150 # for finding renames
Martin von Zweigbergk
addremove: add back forgotten files (BC)...
r23259 elif dstate == 'r' and not st:
Siddharth Agarwal
scmutil.addremove: factor out dirstate walk into another function...
r19150 removed.append(abs)
elif dstate == 'a':
added.append(abs)
Martin von Zweigbergk
addremove: add back forgotten files (BC)...
r23259 return added, unknown, deleted, removed, forgotten
Siddharth Agarwal
scmutil.addremove: factor out dirstate walk into another function...
r19150
Siddharth Agarwal
scmutil.addremove: factor out code to find renames...
r19152 def _findrenames(repo, matcher, added, removed, similarity):
'''Find renames from removed files to added ones.'''
renames = {}
if similarity > 0:
for old, new, score in similar.findrenames(repo, added, removed,
similarity):
if (repo.ui.verbose or not matcher.exact(old)
or not matcher.exact(new)):
repo.ui.status(_('recording removal of %s as rename to %s '
'(%d%% similar)\n') %
(matcher.rel(old), matcher.rel(new),
score * 100))
renames[new] = old
return renames
Siddharth Agarwal
scmutil.addremove: factor out code to mark added/removed/renames...
r19153 def _markchanges(repo, unknown, deleted, renames):
'''Marks the files in unknown as added, the files in deleted as removed,
and the files in renames as copied.'''
wctx = repo[None]
wlock = repo.wlock()
try:
wctx.forget(deleted)
wctx.add(unknown)
for new, old in renames.iteritems():
wctx.copy(old, new)
finally:
wlock.release()
Matt Mackall
scmutil: fold in wdutil
r14320 def dirstatecopy(ui, repo, wctx, src, dst, dryrun=False, cwd=None):
"""Update the dirstate to reflect the intent of copying src to dst. For
different reasons it might not end with dst being marked as copied from src.
"""
origsrc = repo.dirstate.copied(src) or src
if dst == origsrc: # copying back a copy?
if repo.dirstate[dst] not in 'mn' and not dryrun:
repo.dirstate.normallookup(dst)
else:
if repo.dirstate[origsrc] == 'a' and origsrc == src:
if not ui.quiet:
ui.warn(_("%s has not been committed yet, so no copy "
"data will be stored for %s.\n")
% (repo.pathto(origsrc, cwd), repo.pathto(dst, cwd)))
if repo.dirstate[dst] in '?r' and not dryrun:
wctx.add([dst])
elif not dryrun:
wctx.copy(origsrc, dst)
Adrian Buehlmann
introduce new function scmutil.readrequires...
r14482
def readrequires(opener, supported):
'''Reads and parses .hg/requires and checks if all entries found
are in the list of supported features.'''
requirements = set(opener.read("requires").splitlines())
Pierre-Yves David
requirements: show all missing features in the error message....
r14746 missings = []
Adrian Buehlmann
introduce new function scmutil.readrequires...
r14482 for r in requirements:
if r not in supported:
Matt Mackall
requires: note apparent corruption
r14484 if not r or not r[0].isalnum():
raise error.RequirementError(_(".hg/requires file is corrupt"))
Pierre-Yves David
requirements: show all missing features in the error message....
r14746 missings.append(r)
missings.sort()
if missings:
Brodie Rao
cleanup: eradicate long lines
r16683 raise error.RequirementError(
Mads Kiilerich
repo: rephrase the "missing requirement" error message...
r20820 _("repository requires features unknown to this Mercurial: %s")
% " ".join(missings),
Pierre-Yves David
require: provide a link to a wiki page in addition of suggesting upgrade...
r20715 hint=_("see http://mercurial.selenic.com/wiki/MissingRequirement"
Mads Kiilerich
repo: rephrase the "missing requirement" error message...
r20820 " for more information"))
Adrian Buehlmann
introduce new function scmutil.readrequires...
r14482 return requirements
Idan Kamara
scmutil: introduce filecache...
r14928
Siddharth Agarwal
scmutil: rename filecacheentry to filecachesubentry...
r20043 class filecachesubentry(object):
Siddharth Agarwal
scmutil.filecacheentry: make stat argument to constructor mandatory...
r20042 def __init__(self, path, stat):
Idan Kamara
scmutil: introduce filecache...
r14928 self.path = path
Idan Kamara
filecache: allow filecacheentry to be created without stating in __init__...
r18315 self.cachestat = None
self._cacheable = None
Idan Kamara
scmutil: introduce filecache...
r14928
Idan Kamara
filecache: allow filecacheentry to be created without stating in __init__...
r18315 if stat:
Siddharth Agarwal
scmutil: rename filecacheentry to filecachesubentry...
r20043 self.cachestat = filecachesubentry.stat(self.path)
Idan Kamara
filecache: allow filecacheentry to be created without stating in __init__...
r18315
if self.cachestat:
self._cacheable = self.cachestat.cacheable()
else:
# None means we don't know yet
self._cacheable = None
Idan Kamara
scmutil: introduce filecache...
r14928
def refresh(self):
if self.cacheable():
Siddharth Agarwal
scmutil: rename filecacheentry to filecachesubentry...
r20043 self.cachestat = filecachesubentry.stat(self.path)
Idan Kamara
scmutil: introduce filecache...
r14928
def cacheable(self):
if self._cacheable is not None:
return self._cacheable
# we don't know yet, assume it is for now
return True
def changed(self):
# no point in going further if we can't cache it
if not self.cacheable():
return True
Siddharth Agarwal
scmutil: rename filecacheentry to filecachesubentry...
r20043 newstat = filecachesubentry.stat(self.path)
Idan Kamara
scmutil: introduce filecache...
r14928
# we may not know if it's cacheable yet, check again now
if newstat and self._cacheable is None:
self._cacheable = newstat.cacheable()
# check again
if not self._cacheable:
return True
if self.cachestat != newstat:
self.cachestat = newstat
return True
else:
return False
@staticmethod
def stat(path):
try:
return util.cachestat(path)
except OSError, e:
if e.errno != errno.ENOENT:
raise
Siddharth Agarwal
scmutil: introduce a filecacheentry that can watch multiple paths
r20044 class filecacheentry(object):
def __init__(self, paths, stat=True):
self._entries = []
for path in paths:
self._entries.append(filecachesubentry(path, stat))
def changed(self):
'''true if any entry has changed'''
for entry in self._entries:
if entry.changed():
return True
return False
def refresh(self):
for entry in self._entries:
entry.refresh()
Idan Kamara
scmutil: introduce filecache...
r14928 class filecache(object):
Siddharth Agarwal
scmutil.filecache: support watching over multiple files
r20045 '''A property like decorator that tracks files under .hg/ for updates.
Idan Kamara
scmutil: introduce filecache...
r14928
Records stat info when called in _filecache.
Siddharth Agarwal
scmutil.filecache: support watching over multiple files
r20045 On subsequent calls, compares old stat info with new info, and recreates the
object when any of the files changes, updating the new stat info in
_filecache.
Idan Kamara
scmutil: introduce filecache...
r14928
Mercurial either atomic renames or appends for files under .hg,
so to ensure the cache is reliable we need the filesystem to be able
to tell us if a file has been replaced. If it can't, we fallback to
recreating the object on every call (essentially the same behaviour as
Siddharth Agarwal
scmutil.filecache: support watching over multiple files
r20045 propertycache).
'''
def __init__(self, *paths):
self.paths = paths
Idan Kamara
filecache: refactor path join logic to a function...
r16198
def join(self, obj, fname):
Siddharth Agarwal
scmutil.filecache: support watching over multiple files
r20045 """Used to compute the runtime path of a cached file.
Idan Kamara
filecache: refactor path join logic to a function...
r16198
Users should subclass filecache and provide their own version of this
function to call the appropriate join function on 'obj' (an instance
of the class that its member function was decorated).
"""
return obj.join(fname)
Idan Kamara
scmutil: introduce filecache...
r14928
def __call__(self, func):
self.func = func
self.name = func.__name__
return self
def __get__(self, obj, type=None):
Idan Kamara
scmutil: update cached copy when filecached attribute is assigned (issue3263)...
r16115 # do we need to check if the file changed?
if self.name in obj.__dict__:
Idan Kamara
filecache: create an entry in _filecache when __set__ is called for a missing one...
r18316 assert self.name in obj._filecache, self.name
Idan Kamara
scmutil: update cached copy when filecached attribute is assigned (issue3263)...
r16115 return obj.__dict__[self.name]
Idan Kamara
scmutil: introduce filecache...
r14928 entry = obj._filecache.get(self.name)
if entry:
if entry.changed():
entry.obj = self.func(obj)
else:
Siddharth Agarwal
scmutil.filecache: support watching over multiple files
r20045 paths = [self.join(obj, path) for path in self.paths]
Idan Kamara
scmutil: introduce filecache...
r14928
# We stat -before- creating the object so our cache doesn't lie if
# a writer modified between the time we read and stat
Siddharth Agarwal
scmutil.filecache: support watching over multiple files
r20045 entry = filecacheentry(paths, True)
Idan Kamara
scmutil: introduce filecache...
r14928 entry.obj = self.func(obj)
obj._filecache[self.name] = entry
Idan Kamara
scmutil: update cached copy when filecached attribute is assigned (issue3263)...
r16115 obj.__dict__[self.name] = entry.obj
Idan Kamara
scmutil: introduce filecache...
r14928 return entry.obj
Idan Kamara
scmutil: update cached copy when filecached attribute is assigned (issue3263)...
r16115
def __set__(self, obj, value):
Idan Kamara
filecache: create an entry in _filecache when __set__ is called for a missing one...
r18316 if self.name not in obj._filecache:
# we add an entry for the missing value because X in __dict__
# implies X in _filecache
Siddharth Agarwal
scmutil.filecache: support watching over multiple files
r20045 paths = [self.join(obj, path) for path in self.paths]
ce = filecacheentry(paths, False)
Idan Kamara
filecache: create an entry in _filecache when __set__ is called for a missing one...
r18316 obj._filecache[self.name] = ce
else:
ce = obj._filecache[self.name]
ce.obj = value # update cached copy
Idan Kamara
scmutil: update cached copy when filecached attribute is assigned (issue3263)...
r16115 obj.__dict__[self.name] = value # update copy returned by obj.x
def __delete__(self, obj):
try:
del obj.__dict__[self.name]
except KeyError:
Augie Fackler
scmutil: clean up use of two-argument raise...
r18177 raise AttributeError(self.name)