dirstate.py
591 lines
| 17.9 KiB
| text/x-python
|
PythonLexer
/ mercurial / dirstate.py
|
r8226 | # dirstate.py - working directory tracking for mercurial | ||
# | ||||
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2, incorporated herein by reference. | ||||
|
r1089 | |||
|
r6211 | from node import nullid | ||
|
r3891 | from i18n import _ | ||
|
r7034 | import struct, os, stat, util, errno, ignore | ||
|
r7093 | import cStringIO, osutil, sys, parsers | ||
|
r1089 | |||
|
r4610 | _unknown = ('?', 0, 0, 0) | ||
_format = ">cllll" | ||||
|
r8261 | propertycache = util.propertycache | ||
|
r4610 | |||
|
r6767 | def _finddirs(path): | ||
|
r7032 | pos = path.rfind('/') | ||
while pos != -1: | ||||
yield path[:pos] | ||||
|
r6767 | pos = path.rfind('/', 0, pos) | ||
|
r7096 | def _incdirs(dirs, path): | ||
for base in _finddirs(path): | ||||
if base in dirs: | ||||
dirs[base] += 1 | ||||
return | ||||
dirs[base] = 1 | ||||
def _decdirs(dirs, path): | ||||
for base in _finddirs(path): | ||||
if dirs[base] > 1: | ||||
dirs[base] -= 1 | ||||
return | ||||
del dirs[base] | ||||
|
r1559 | class dirstate(object): | ||
|
r2393 | |||
|
r1089 | def __init__(self, opener, ui, root): | ||
|
r4614 | self._opener = opener | ||
self._root = root | ||||
|
r6972 | self._rootdir = os.path.join(root, '') | ||
|
r4903 | self._dirty = False | ||
|
r4965 | self._dirtypl = False | ||
|
r4614 | self._ui = ui | ||
|
r1089 | |||
|
r8261 | @propertycache | ||
def _map(self): | ||||
self._read() | ||||
return self._map | ||||
@propertycache | ||||
def _copymap(self): | ||||
self._read() | ||||
return self._copymap | ||||
@propertycache | ||||
def _foldmap(self): | ||||
f = {} | ||||
for name in self._map: | ||||
f[os.path.normcase(name)] = name | ||||
return f | ||||
@propertycache | ||||
def _branch(self): | ||||
try: | ||||
return self._opener("branch").read().strip() or "default" | ||||
except IOError: | ||||
return "default" | ||||
@propertycache | ||||
def _pl(self): | ||||
try: | ||||
st = self._opener("dirstate").read(40) | ||||
if len(st) == 40: | ||||
return st[:20], st[20:40] | ||||
except IOError, err: | ||||
if err.errno != errno.ENOENT: raise | ||||
return [nullid, nullid] | ||||
@propertycache | ||||
def _dirs(self): | ||||
dirs = {} | ||||
for f,s in self._map.iteritems(): | ||||
if s[0] != 'r': | ||||
_incdirs(dirs, f) | ||||
return dirs | ||||
@propertycache | ||||
def _ignore(self): | ||||
files = [self._join('.hgignore')] | ||||
for name, path in self._ui.configitems("ui"): | ||||
if name == 'ignore' or name.startswith('ignore.'): | ||||
files.append(os.path.expanduser(path)) | ||||
return ignore.ignore(self._root, files, self._ui.warn) | ||||
@propertycache | ||||
def _slash(self): | ||||
return self._ui.configbool('ui', 'slash') and os.sep != '/' | ||||
@propertycache | ||||
def _checklink(self): | ||||
return util.checklink(self._root) | ||||
@propertycache | ||||
def _checkexec(self): | ||||
return util.checkexec(self._root) | ||||
@propertycache | ||||
def _checkcase(self): | ||||
return not util.checkcase(self._join('.hg')) | ||||
@propertycache | ||||
def normalize(self): | ||||
if self._checkcase: | ||||
return self._normalize | ||||
return lambda x, y=False: x | ||||
|
r4603 | |||
|
r4905 | def _join(self, f): | ||
|
r6972 | # much faster than os.path.join() | ||
|
r6973 | # it's safe because f is always a relative path | ||
|
r6972 | return self._rootdir + f | ||
|
r1089 | |||
|
r6743 | def flagfunc(self, fallback): | ||
if self._checklink: | ||||
if self._checkexec: | ||||
def f(x): | ||||
|
r6972 | p = self._join(x) | ||
|
r6743 | if os.path.islink(p): | ||
return 'l' | ||||
if util.is_exec(p): | ||||
return 'x' | ||||
return '' | ||||
return f | ||||
def f(x): | ||||
|
r6972 | if os.path.islink(self._join(x)): | ||
|
r6743 | return 'l' | ||
if 'x' in fallback(x): | ||||
return 'x' | ||||
return '' | ||||
return f | ||||
if self._checkexec: | ||||
def f(x): | ||||
if 'l' in fallback(x): | ||||
return 'l' | ||||
|
r6972 | if util.is_exec(self._join(x)): | ||
|
r6743 | return 'x' | ||
return '' | ||||
return f | ||||
return fallback | ||||
|
r1089 | def getcwd(self): | ||
cwd = os.getcwd() | ||||
|
r4614 | if cwd == self._root: return '' | ||
# self._root ends with a path separator if self._root is '/' or 'C:\' | ||||
rootsep = self._root | ||||
|
r5843 | if not util.endswithsep(rootsep): | ||
|
r4230 | rootsep += os.sep | ||
if cwd.startswith(rootsep): | ||||
return cwd[len(rootsep):] | ||||
else: | ||||
# we're outside the repo. return an absolute path. | ||||
return cwd | ||||
|
r1089 | |||
|
r4525 | def pathto(self, f, cwd=None): | ||
if cwd is None: | ||||
cwd = self.getcwd() | ||||
|
r4614 | path = util.pathto(self._root, cwd, f) | ||
|
r4527 | if self._slash: | ||
|
r5842 | return util.normpath(path) | ||
|
r4527 | return path | ||
|
r4525 | |||
|
r1089 | def __getitem__(self, key): | ||
|
r4906 | ''' current states: | ||
n normal | ||||
m needs merging | ||||
r marked for removal | ||||
a marked for addition | ||||
? not tracked''' | ||||
return self._map.get(key, ("?",))[0] | ||||
|
r1089 | |||
def __contains__(self, key): | ||||
|
r4614 | return key in self._map | ||
def __iter__(self): | ||||
|
r8209 | for x in sorted(self._map): | ||
|
r4614 | yield x | ||
|
r1089 | |||
def parents(self): | ||||
|
r4614 | return self._pl | ||
|
r1089 | |||
|
r4179 | def branch(self): | ||
return self._branch | ||||
|
r1089 | def setparents(self, p1, p2=nullid): | ||
|
r4965 | self._dirty = self._dirtypl = True | ||
|
r4614 | self._pl = p1, p2 | ||
|
r1089 | |||
|
r4179 | def setbranch(self, branch): | ||
self._branch = branch | ||||
|
r4614 | self._opener("branch", "w").write(branch + '\n') | ||
|
r4179 | |||
|
r4615 | def _read(self): | ||
|
r4614 | self._map = {} | ||
self._copymap = {} | ||||
|
r4607 | try: | ||
|
r4614 | st = self._opener("dirstate").read() | ||
|
r4607 | except IOError, err: | ||
if err.errno != errno.ENOENT: raise | ||||
return | ||||
if not st: | ||||
return | ||||
|
r7128 | p = parsers.parse_dirstate(self._map, self._copymap, st) | ||
|
r4952 | if not self._dirtypl: | ||
|
r7093 | self._pl = p | ||
|
r1089 | |||
|
r4613 | def invalidate(self): | ||
|
r6677 | for a in "_map _copymap _foldmap _branch _pl _dirs _ignore".split(): | ||
|
r4953 | if a in self.__dict__: | ||
delattr(self, a) | ||||
|
r4903 | self._dirty = False | ||
|
r4375 | |||
|
r1089 | def copy(self, source, dest): | ||
|
r7566 | """Mark dest as a copy of source. Unmark dest if source is None. | ||
""" | ||||
|
r6680 | if source == dest: | ||
return | ||||
|
r4903 | self._dirty = True | ||
|
r7566 | if source is not None: | ||
self._copymap[dest] = source | ||||
elif dest in self._copymap: | ||||
del self._copymap[dest] | ||||
|
r1089 | |||
def copied(self, file): | ||||
|
r4614 | return self._copymap.get(file, None) | ||
|
r3154 | |||
def copies(self): | ||||
|
r4614 | return self._copymap | ||
|
r1089 | |||
|
r6767 | def _droppath(self, f): | ||
if self[f] not in "?r" and "_dirs" in self.__dict__: | ||||
|
r7096 | _decdirs(self._dirs, f) | ||
|
r2953 | |||
|
r6767 | def _addpath(self, f, check=False): | ||
|
r5487 | oldstate = self[f] | ||
|
r6767 | if check or oldstate == "r": | ||
if '\r' in f or '\n' in f: | ||||
raise util.Abort( | ||||
_("'\\n' and '\\r' disallowed in filenames: %r") % f) | ||||
if f in self._dirs: | ||||
raise util.Abort(_('directory %r already in dirstate') % f) | ||||
# shadows | ||||
for d in _finddirs(f): | ||||
if d in self._dirs: | ||||
break | ||||
if d in self._map and self[d] != 'r': | ||||
raise util.Abort( | ||||
_('file %r in dirstate clashes with %r') % (d, f)) | ||||
if oldstate in "?r" and "_dirs" in self.__dict__: | ||||
|
r7096 | _incdirs(self._dirs, f) | ||
|
r5487 | |||
|
r4904 | def normal(self, f): | ||
|
r5210 | 'mark a file normal and clean' | ||
|
r4904 | self._dirty = True | ||
|
r6767 | self._addpath(f) | ||
|
r4905 | s = os.lstat(self._join(f)) | ||
|
r7119 | self._map[f] = ('n', s.st_mode, s.st_size, int(s.st_mtime)) | ||
|
r5915 | if f in self._copymap: | ||
|
r4904 | del self._copymap[f] | ||
|
r1089 | |||
|
r5210 | def normallookup(self, f): | ||
|
r4904 | 'mark a file normal, but possibly dirty' | ||
|
r6298 | if self._pl[1] != nullid and f in self._map: | ||
# if there is a merge going on and the file was either | ||||
# in state 'm' or dirty before being removed, restore that state. | ||||
entry = self._map[f] | ||||
if entry[0] == 'r' and entry[2] in (-1, -2): | ||||
source = self._copymap.get(f) | ||||
if entry[2] == -1: | ||||
self.merge(f) | ||||
elif entry[2] == -2: | ||||
self.normaldirty(f) | ||||
if source: | ||||
self.copy(source, f) | ||||
return | ||||
if entry[0] == 'm' or entry[0] == 'n' and entry[2] == -2: | ||||
return | ||||
|
r4903 | self._dirty = True | ||
|
r6767 | self._addpath(f) | ||
|
r7093 | self._map[f] = ('n', 0, -1, -1) | ||
|
r5210 | if f in self._copymap: | ||
del self._copymap[f] | ||||
def normaldirty(self, f): | ||||
'mark a file normal, but dirty' | ||||
self._dirty = True | ||||
|
r6767 | self._addpath(f) | ||
|
r7093 | self._map[f] = ('n', 0, -2, -1) | ||
|
r4904 | if f in self._copymap: | ||
del self._copymap[f] | ||||
def add(self, f): | ||||
'mark a file added' | ||||
self._dirty = True | ||||
|
r6767 | self._addpath(f, True) | ||
|
r7093 | self._map[f] = ('a', 0, -1, -1) | ||
|
r4904 | if f in self._copymap: | ||
del self._copymap[f] | ||||
|
r4616 | |||
|
r4904 | def remove(self, f): | ||
'mark a file removed' | ||||
self._dirty = True | ||||
|
r6767 | self._droppath(f) | ||
|
r6297 | size = 0 | ||
if self._pl[1] != nullid and f in self._map: | ||||
entry = self._map[f] | ||||
if entry[0] == 'm': | ||||
size = -1 | ||||
elif entry[0] == 'n' and entry[2] == -2: | ||||
size = -2 | ||||
|
r7093 | self._map[f] = ('r', 0, size, 0) | ||
|
r6297 | if size == 0 and f in self._copymap: | ||
|
r4904 | del self._copymap[f] | ||
|
r1089 | |||
|
r4904 | def merge(self, f): | ||
'mark a file merged' | ||||
|
r4903 | self._dirty = True | ||
|
r4905 | s = os.lstat(self._join(f)) | ||
|
r6767 | self._addpath(f) | ||
|
r7119 | self._map[f] = ('m', s.st_mode, s.st_size, int(s.st_mtime)) | ||
|
r4904 | if f in self._copymap: | ||
del self._copymap[f] | ||||
def forget(self, f): | ||||
'forget a file' | ||||
self._dirty = True | ||||
try: | ||||
|
r6807 | self._droppath(f) | ||
|
r4904 | del self._map[f] | ||
except KeyError: | ||||
|
r6057 | self._ui.warn(_("not in dirstate: %s\n") % f) | ||
|
r1089 | |||
|
r7068 | def _normalize(self, path, knownpath=False): | ||
|
r7069 | norm_path = os.path.normcase(path) | ||
|
r7068 | fold_path = self._foldmap.get(norm_path, None) | ||
if fold_path is None: | ||||
if knownpath or not os.path.exists(os.path.join(self._root, path)): | ||||
fold_path = path | ||||
else: | ||||
fold_path = self._foldmap.setdefault(norm_path, | ||||
util.fspath(path, self._root)) | ||||
return fold_path | ||||
|
r6677 | |||
|
r5065 | def clear(self): | ||
self._map = {} | ||||
|
r5487 | if "_dirs" in self.__dict__: | ||
delattr(self, "_dirs"); | ||||
|
r5065 | self._copymap = {} | ||
self._pl = [nullid, nullid] | ||||
|
r5123 | self._dirty = True | ||
|
r5065 | |||
|
r1755 | def rebuild(self, parent, files): | ||
|
r5065 | self.clear() | ||
|
r2832 | for f in files: | ||
|
r6750 | if 'x' in files.flags(f): | ||
|
r7093 | self._map[f] = ('n', 0777, -1, 0) | ||
|
r1755 | else: | ||
|
r7093 | self._map[f] = ('n', 0666, -1, 0) | ||
|
r4614 | self._pl = (parent, nullid) | ||
|
r4903 | self._dirty = True | ||
|
r1089 | |||
def write(self): | ||||
|
r4612 | if not self._dirty: | ||
|
r1794 | return | ||
|
r6326 | st = self._opener("dirstate", "w", atomictemp=True) | ||
|
r6327 | |||
try: | ||||
gran = int(self._ui.config('dirstate', 'granularity', 1)) | ||||
except ValueError: | ||||
gran = 1 | ||||
limit = sys.maxint | ||||
if gran > 0: | ||||
limit = util.fstat(st).st_mtime - gran | ||||
|
r4374 | cs = cStringIO.StringIO() | ||
|
r5327 | copymap = self._copymap | ||
pack = struct.pack | ||||
write = cs.write | ||||
write("".join(self._pl)) | ||||
|
r4614 | for f, e in self._map.iteritems(): | ||
|
r5327 | if f in copymap: | ||
f = "%s\0%s" % (f, copymap[f]) | ||||
|
r6326 | if e[3] > limit and e[0] == 'n': | ||
|
r7093 | e = (e[0], 0, -1, -1) | ||
|
r5327 | e = pack(_format, e[0], e[1], e[2], e[3], len(f)) | ||
write(e) | ||||
write(f) | ||||
|
r4374 | st.write(cs.getvalue()) | ||
|
r4507 | st.rename() | ||
|
r4965 | self._dirty = self._dirtypl = False | ||
|
r1089 | |||
|
r6032 | def _dirignore(self, f): | ||
|
r6479 | if f == '.': | ||
return False | ||||
|
r6032 | if self._ignore(f): | ||
return True | ||||
|
r6767 | for p in _finddirs(f): | ||
if self._ignore(p): | ||||
|
r6032 | return True | ||
return False | ||||
|
r6755 | def walk(self, match, unknown, ignored): | ||
|
r3529 | ''' | ||
walk recursively through the directory tree, finding all files | ||||
matched by the match function | ||||
|
r6818 | results are yielded in a tuple (filename, stat), where stat | ||
|
r3529 | and st is the stat result if the file was found in the directory. | ||
''' | ||||
|
r1089 | |||
|
r6578 | def fwarn(f, msg): | ||
|
r7023 | self._ui.warn('%s: %s\n' % (self.pathto(f), msg)) | ||
|
r6578 | return False | ||
|
r6589 | badfn = fwarn | ||
if hasattr(match, 'bad'): | ||||
badfn = match.bad | ||||
|
r6578 | |||
|
r6830 | def badtype(f, mode): | ||
|
r8310 | kind = _('unknown') | ||
|
r6830 | if stat.S_ISCHR(mode): kind = _('character device') | ||
elif stat.S_ISBLK(mode): kind = _('block device') | ||||
elif stat.S_ISFIFO(mode): kind = _('fifo') | ||||
elif stat.S_ISSOCK(mode): kind = _('socket') | ||||
elif stat.S_ISDIR(mode): kind = _('directory') | ||||
self._ui.warn(_('%s: unsupported file type (type is %s)\n') | ||||
% (self.pathto(f), kind)) | ||||
|
r4609 | ignore = self._ignore | ||
|
r6032 | dirignore = self._dirignore | ||
|
r4193 | if ignored: | ||
ignore = util.never | ||||
|
r6032 | dirignore = util.never | ||
|
r6971 | elif not unknown: | ||
# if unknown and ignored are False, skip step 2 | ||||
ignore = util.always | ||||
dirignore = util.always | ||||
|
r3534 | |||
|
r6834 | matchfn = match.matchfn | ||
|
r6831 | dmap = self._map | ||
|
r5000 | normpath = util.normpath | ||
|
r6822 | normalize = self.normalize | ||
|
r5396 | listdir = osutil.listdir | ||
|
r5000 | lstat = os.lstat | ||
|
r6830 | getkind = stat.S_IFMT | ||
|
r6828 | dirkind = stat.S_IFDIR | ||
|
r6830 | regkind = stat.S_IFREG | ||
lnkkind = stat.S_IFLNK | ||||
|
r6831 | join = self._join | ||
|
r6820 | work = [] | ||
wadd = work.append | ||||
|
r8151 | files = set(match.files()) | ||
|
r6831 | if not files or '.' in files: | ||
files = [''] | ||||
results = {'.hg': None} | ||||
|
r5000 | |||
|
r6826 | # step 1: find all explicit files | ||
|
r8209 | for ff in sorted(files): | ||
|
r6822 | nf = normalize(normpath(ff)) | ||
|
r6829 | if nf in results: | ||
|
r6820 | continue | ||
try: | ||||
|
r6831 | st = lstat(join(nf)) | ||
|
r6830 | kind = getkind(st.st_mode) | ||
if kind == dirkind: | ||||
|
r6828 | if not dirignore(nf): | ||
wadd(nf) | ||||
|
r6830 | elif kind == regkind or kind == lnkkind: | ||
results[nf] = st | ||||
|
r6828 | else: | ||
|
r6830 | badtype(ff, kind) | ||
if nf in dmap: | ||||
|
r6829 | results[nf] = None | ||
|
r6820 | except OSError, inst: | ||
keep = False | ||||
|
r6828 | prefix = nf + "/" | ||
|
r6821 | for fn in dmap: | ||
|
r6828 | if nf == fn or fn.startswith(prefix): | ||
|
r6820 | keep = True | ||
break | ||||
if not keep: | ||||
if inst.errno != errno.ENOENT: | ||||
fwarn(ff, inst.strerror) | ||||
|
r6832 | elif badfn(ff, inst.strerror): | ||
|
r6834 | if (nf in dmap or not ignore(nf)) and matchfn(nf): | ||
|
r6832 | results[nf] = None | ||
|
r6820 | |||
|
r6826 | # step 2: visit subdirectories | ||
while work: | ||||
nd = work.pop() | ||||
if hasattr(match, 'dir'): | ||||
match.dir(nd) | ||||
|
r7099 | skip = None | ||
|
r6826 | if nd == '.': | ||
nd = '' | ||||
else: | ||||
|
r7099 | skip = '.hg' | ||
try: | ||||
entries = listdir(join(nd), stat=True, skip=skip) | ||||
except OSError, inst: | ||||
if inst.errno == errno.EACCES: | ||||
fwarn(nd, inst.strerror) | ||||
continue | ||||
raise | ||||
|
r6826 | for f, kind, st in entries: | ||
|
r7068 | nf = normalize(nd and (nd + "/" + f) or f, True) | ||
|
r6829 | if nf not in results: | ||
|
r6828 | if kind == dirkind: | ||
if not ignore(nf): | ||||
wadd(nf) | ||||
|
r6834 | if nf in dmap and matchfn(nf): | ||
|
r6829 | results[nf] = None | ||
|
r6832 | elif kind == regkind or kind == lnkkind: | ||
if nf in dmap: | ||||
|
r6834 | if matchfn(nf): | ||
|
r6832 | results[nf] = st | ||
|
r6834 | elif matchfn(nf) and not ignore(nf): | ||
|
r6829 | results[nf] = st | ||
|
r6834 | elif nf in dmap and matchfn(nf): | ||
|
r6832 | results[nf] = None | ||
|
r1089 | |||
|
r6826 | # step 3: report unseen items in the dmap hash | ||
|
r8209 | visit = sorted([f for f in dmap if f not in results and match(f)]) | ||
|
r7118 | for nf, st in zip(visit, util.statfiles([join(i) for i in visit])): | ||
if not st is None and not getkind(st.st_mode) in (regkind, lnkkind): | ||||
st = None | ||||
results[nf] = st | ||||
|
r6829 | |||
|
r6831 | del results['.hg'] | ||
|
r6829 | return results | ||
|
r1089 | |||
|
r6753 | def status(self, match, ignored, clean, unknown): | ||
listignored, listclean, listunknown = ignored, clean, unknown | ||||
|
r2022 | lookup, modified, added, unknown, ignored = [], [], [], [], [] | ||
|
r2661 | removed, deleted, clean = [], [], [] | ||
|
r1089 | |||
|
r5003 | dmap = self._map | ||
ladd = lookup.append | ||||
madd = modified.append | ||||
aadd = added.append | ||||
uadd = unknown.append | ||||
iadd = ignored.append | ||||
radd = removed.append | ||||
dadd = deleted.append | ||||
cadd = clean.append | ||||
|
r6829 | for fn, st in self.walk(match, listunknown, listignored).iteritems(): | ||
|
r6591 | if fn not in dmap: | ||
|
r6753 | if (listignored or match.exact(fn)) and self._dirignore(fn): | ||
if listignored: | ||||
|
r6033 | iadd(fn) | ||
|
r6753 | elif listunknown: | ||
|
r5003 | uadd(fn) | ||
|
r1471 | continue | ||
|
r6591 | |||
|
r7093 | state, mode, size, time = dmap[fn] | ||
|
r6591 | |||
|
r6818 | if not st and state in "nma": | ||
dadd(fn) | ||||
elif state == 'n': | ||||
|
r6257 | if (size >= 0 and | ||
(size != st.st_size | ||||
or ((mode ^ st.st_mode) & 0100 and self._checkexec)) | ||||
|
r5210 | or size == -2 | ||
|
r4677 | or fn in self._copymap): | ||
|
r5003 | madd(fn) | ||
|
r2962 | elif time != int(st.st_mtime): | ||
|
r5003 | ladd(fn) | ||
|
r6753 | elif listclean: | ||
|
r5003 | cadd(fn) | ||
|
r6590 | elif state == 'm': | ||
|
r5003 | madd(fn) | ||
|
r6590 | elif state == 'a': | ||
|
r5003 | aadd(fn) | ||
|
r6590 | elif state == 'r': | ||
|
r5003 | radd(fn) | ||
|
r1183 | |||
|
r2661 | return (lookup, modified, added, removed, deleted, unknown, ignored, | ||
clean) | ||||