Commit items represent commits (e.g. /.tag/some-commit or
/foo/latest), and for most purposes, they appear as the underlying
tree. S_ISDIR(item_mode(item)) will return true for both tree Items
-and Commits and the commit's oid is the tree hash. The commit hash
-will be item.coid, and nominal_oid(item) will return coid for commits,
-oid for everything else.
+and Commits and the commit's oid is the tree hash; the commit hash is
+item.coid.
"""
class IOError(exceptions.IOError):
- def __init__(self, errno, message):
+ def __init__(self, errno, message, terminus=None):
exceptions.IOError.__init__(self, errno, message)
-
-class Loop(IOError):
- def __init__(self, message, terminus=None):
- IOError.__init__(self, ELOOP, message)
self.terminus = terminus
default_file_mode = S_IFREG | 0o644
_multiple_slashes_rx = re.compile(r'//+')
def _decompose_path(path):
- """Return a reversed list of path elements, omitting any occurrences
- of "." and ignoring any leading or trailing slash."""
+ """Return a boolean indicating whether the path is absolute, and a
+ reversed list of path elements, omitting any occurrences of "."
+ and ignoring any leading or trailing slash. If the path is
+ effectively '/' or '.', return an empty list.
+
+ """
path = re.sub(_multiple_slashes_rx, '/', path)
+ if path == '/':
+ return True, True, []
+ is_absolute = must_be_dir = False
if path.startswith('/'):
+ is_absolute = True
path = path[1:]
- if path.endswith('/'):
- path = path[:-1]
- result = [x for x in path.split('/') if x != '.']
- result.reverse()
- return result
+ for suffix in ('/', '/.'):
+ if path.endswith(suffix):
+ must_be_dir = True
+ path = path[:-len(suffix)]
+ parts = [x for x in path.split('/') if x != '.']
+ parts.reverse()
+ if not parts:
+ must_be_dir = True # e.g. path was effectively '.' or '/', etc.
+ return is_absolute, must_be_dir, parts
Item = namedtuple('Item', ('meta', 'oid'))
_tags = Tags(meta=default_dir_mode)
-def nominal_oid(item):
- """If the item is a Commit, return its commit oid, otherwise return
- the item's oid, if it has one.
-
- """
- if isinstance(item, Commit):
- return item.coid
- return getattr(item, 'oid', None)
-
def copy_item(item):
"""Return a completely independent copy of item, such that
modifications will not affect the original.
break
return data, None
-def _find_dir_item_metadata(repo, item):
- """Return the metadata for the tree or commit item, or None if the
- tree has no metadata (i.e. older bup save, or non-bup tree).
+def _find_treeish_oid_metadata(repo, oid):
+ """Return the metadata for the tree or commit oid, or None if the tree
+ has no metadata (i.e. older bup save, or non-bup tree).
"""
- tree_data, bupm_oid = tree_data_and_bupm(repo, item.oid)
+ tree_data, bupm_oid = tree_data_and_bupm(repo, oid)
if bupm_oid:
with _FileReader(repo, bupm_oid) as meta_stream:
return _read_dir_meta(meta_stream)
assert S_ISREG(item_mode(item))
return _FileReader(repo, item.oid)
-def _commit_meta_from_auth_sec(author_sec):
- m = Metadata()
- m.mode = default_dir_mode
- m.uid = m.gid = m.size = 0
- m.atime = m.mtime = m.ctime = author_sec * 10**9
- return m
+def _commit_item_from_data(oid, data):
+ info = parse_commit(data)
+ return Commit(meta=default_dir_mode,
+ oid=info.tree.decode('hex'),
+ coid=oid)
-def _commit_meta_from_oidx(repo, oidx):
- it = repo.cat(oidx)
+def _commit_item_from_oid(repo, oid, require_meta):
+ it = repo.cat(oid.encode('hex'))
_, typ, size = next(it)
assert typ == 'commit'
- author_sec = parse_commit(''.join(it)).author_sec
- return _commit_meta_from_auth_sec(author_sec)
+ commit = _commit_item_from_data(oid, ''.join(it))
+ if require_meta:
+ meta = _find_treeish_oid_metadata(repo, commit.tree)
+ if meta:
+ commit = commit._replace(meta=meta)
+ return commit
+
+def _revlist_item_from_oid(repo, oid, require_meta):
+ if require_meta:
+ meta = _find_treeish_oid_metadata(repo, oid) or default_dir_mode
+ else:
+ meta = default_dir_mode
+ return RevList(oid=oid, meta=meta)
def parse_rev_auth_secs(f):
tree, author_secs = f.readline().split(None, 2)
# in parallel (i.e. meta vs refs).
for name, oid in tuple(repo.refs([], limit_to_heads=True)):
assert(name.startswith('refs/heads/'))
- name = name[11:]
- m = _commit_meta_from_oidx(repo, oid.encode('hex'))
- yield name, RevList(meta=m, oid=oid)
+ yield name[11:], _revlist_item_from_oid(repo, oid, False)
return
if '.' in names:
continue
assert typ == 'commit'
commit = parse_commit(''.join(it))
- yield ref, RevList(meta=_commit_meta_from_auth_sec(commit.author_sec),
- oid=oidx.decode('hex'))
+ yield ref, _revlist_item_from_oid(repo, oidx.decode('hex'), False)
def ordered_tree_entries(tree_data, bupm=None):
"""Yields (name, mangled_name, kind, gitmode, oid) for each item in
oidx = oid.encode('hex')
names = frozenset(name for name in (names or tuple()) \
if _save_name_rx.match(name) or name in ('.', 'latest'))
-
# Do this before we open the rev_list iterator so we're not nesting
if (not names) or ('.' in names):
- yield '.', RevList(oid=oid, meta=_commit_meta_from_oidx(repo, oidx))
-
+ yield '.', _revlist_item_from_oid(repo, oid, True)
+
revs = repo.rev_list((oidx,), format='%T %at', parse=parse_rev_auth_secs)
rev_items, rev_names = tee(revs)
revs = None # Don't disturb the tees
it = repo.cat(oidx)
_, typ, size = next(it)
if typ == 'commit':
- tree_oid = parse_commit(''.join(it)).tree.decode('hex')
- assert len(tree_oid) == 20
- # FIXME: more efficient/bulk?
- return RevList(meta=_commit_meta_from_oidx(repo, oidx), oid=oid)
+ return _commit_item_from_data(oid, ''.join(it))
for _ in it: pass
if typ == 'blob':
return Item(meta=default_file_mode, oid=oid)
yield x
def _resolve_path(repo, path, parent=None, want_meta=True, deref=False):
+ def raise_dir_required_but_not_dir(path, parent, past):
+ raise IOError(ENOTDIR,
+ "path %r%s resolves to non-directory %r"
+ % (path,
+ ' (relative to %r)' % parent if parent else '',
+ past),
+ terminus=past)
global _root
assert repo
assert len(path)
assert type(x[0]) in (bytes, str)
assert type(x[1]) in item_types
assert parent[0][1] == _root
- future = _decompose_path(path)
- if path.startswith('/'):
- if future == ['']: # path was effectively '/'
+ if not S_ISDIR(item_mode(parent[-1][1])):
+ raise IOError(ENOTDIR,
+ 'path resolution parent %r is not a directory'
+ % (parent,))
+ is_absolute, must_be_dir, future = _decompose_path(path)
+ if must_be_dir:
+ deref = True
+ if not future: # path was effectively '.' or '/'
+ if is_absolute:
return (('', _root),)
+ if parent:
+ return tuple(parent)
+ return [('', _root)]
+ if is_absolute:
past = [('', _root)]
else:
- if parent:
- past = list(parent)
- else:
- past = [('', _root)]
- if not future: # e.g. if path was effectively '.'
- return tuple(past)
+ past = list(parent) if parent else [('', _root)]
hops = 0
while True:
+ if not future:
+ if must_be_dir and not S_ISDIR(item_mode(past[-1][1])):
+ raise_dir_required_but_not_dir(path, parent, past)
+ return tuple(past)
segment = future.pop()
if segment == '..':
+ assert len(past) > 0
if len(past) > 1: # .. from / is /
+ assert S_ISDIR(item_mode(past[-1][1]))
past.pop()
else:
parent_name, parent_item = past[-1]
mode = item_mode(item)
if not S_ISLNK(mode):
if not S_ISDIR(mode):
- assert(not future)
past.append((segment, item),)
+ if future:
+ raise IOError(ENOTDIR,
+ 'path %r%s ends internally in non-directory here: %r'
+ % (path,
+ ' (relative to %r)' % parent if parent else '',
+ past),
+ terminus=past)
+ if must_be_dir:
+ raise_dir_required_but_not_dir(path, parent, past)
return tuple(past)
# It's treeish
if want_meta and type(item) in real_tree_types:
- dir_meta = _find_dir_item_metadata(repo, item)
+ dir_meta = _find_treeish_oid_metadata(repo, item.oid)
if dir_meta:
item = item._replace(meta=dir_meta)
- if not future:
- past.append((segment, item),)
- return tuple(past)
past.append((segment, item))
- else: # symlink
+ else: # symlink
if not future and not deref:
past.append((segment, item),)
- return tuple(past)
+ continue
+ if hops > 100:
+ raise IOError(ELOOP,
+ 'too many symlinks encountered while resolving %r%s'
+ % (path, ' relative to %r' % parent if parent else ''),
+ terminus=tuple(past + [(segment, item)]))
target = readlink(repo, item)
- target_future = _decompose_path(target)
- if target.startswith('/'):
- future = target_future
+ is_absolute, _, target_future = _decompose_path(target)
+ if is_absolute:
+ if not target_future: # path was effectively '/'
+ return (('', _root),)
past = [('', _root)]
- if target_future == ['']: # path was effectively '/'
- return tuple(past)
+ future = target_future
else:
future.extend(target_future)
hops += 1
- if hops > 100:
- raise Loop('too many symlinks encountered while resolving %r%s'
- % (path,
- 'relative to %r' % parent if parent else ''))
def lresolve(repo, path, parent=None, want_meta=True):
- """Perform exactly the same function as resolve(), except if the
- final path element is a symbolic link, don't follow it, just
- return it in the result."""
+ """Perform exactly the same function as resolve(), except if the final
+ path element is a symbolic link, don't follow it, just return it
+ in the result.
+
+ """
return _resolve_path(repo, path, parent=parent, want_meta=want_meta,
deref=False)
resolution, the result will represent the location of the missing
item, and that item in the result will be None.
+ Any attempt to traverse a non-directory will raise a VFS ENOTDIR
+ IOError exception.
+
Any symlinks along the path, including at the end, will be
- resolved. A Loop exception will be raised if too many symlinks
- are traversed whiile following the path. raised if too many
- symlinks are traversed while following the path. That exception
- is effectively like a normal ELOOP IOError exception, but will
- include a terminus element describing the location of the failure,
- which will be a tuple of (name, info) elements.
-
- Currently, a path ending in '/' will still resolve if it exists,
- even if not a directory. The parent, if specified, must be a
- sequence of (name, item) tuples, and will provide the starting
- point for the resolution of the path. The result may include
- elements of parent directly, so they must not be modified later.
- If this is a concern, pass in "name, copy_item(item) for
- name, item in parent" instead.
+ resolved. A VFS IOError with the errno attribute set to ELOOP
+ will be raised if too many symlinks are traversed while following
+ the path. That exception is effectively like a normal
+ ELOOP IOError exception, but will include a terminus element
+ describing the location of the failure, which will be a tuple of
+ (name, info) elements.
+
+ The parent, if specified, must be a sequence of (name, item)
+ tuples, and will provide the starting point for the resolution of
+ the path. If no parent is specified, resolution will start at
+ '/'.
+
+ The result may include elements of parent directly, so they must
+ not be modified later. If this is a concern, pass in "name,
+ copy_item(item) for name, item in parent" instead.
When want_meta is true, detailed metadata will be included in each
result item if it's avaiable, otherwise item.meta will be an