Пример #1
0
def tree_lookup_path(lookup_obj, root_sha, path):
    """Look up an object in a Git tree.

    :param lookup_obj: Callback for retrieving object by SHA1
    :param root_sha: SHA1 of the root tree
    :param path: Path to lookup
    :return: A tuple of (mode, SHA) of the resulting path.
    """
    tree = lookup_obj(root_sha)
    if not isinstance(tree, Tree):
        raise NotTreeError(root_sha)
    return tree.lookup_path(lookup_obj, path)
Пример #2
0
 def _get_object(self, sha, cls):
     assert len(sha) in (20, 40)
     ret = self.get_object(sha)
     if ret._type != cls._type:
         if cls is Commit:
             raise NotCommitError(ret)
         elif cls is Blob:
             raise NotBlobError(ret)
         elif cls is Tree:
             raise NotTreeError(ret)
         else:
             raise Exception("Type invalid: %r != %r" %
                             (ret._type, cls._type))
     return ret
Пример #3
0
 def _get_object(self, sha, cls):
     assert len(sha) in (20, 40)
     ret = self.get_object(sha)
     if not isinstance(ret, cls):
         if cls is Commit:
             raise NotCommitError(ret)
         elif cls is Blob:
             raise NotBlobError(ret)
         elif cls is Tree:
             raise NotTreeError(ret)
         elif cls is Tag:
             raise NotTagError(ret)
         else:
             raise Exception("Type invalid: %r != %r" % (
               ret.type_name, cls.type_name))
     return ret
def tree_lookup_path(lookup_obj, root_sha, path):
    """Lookup an object in a Git tree.

    :param lookup_obj: Callback for retrieving object by SHA1
    :param root_sha: SHA1 of the root tree
    :param path: Path to lookup
    """
    parts = path.split("/")
    sha = root_sha
    for p in parts:
        obj = lookup_obj(sha)
        if type(obj) is not Tree:
            raise NotTreeError(sha)
        if p == '':
            continue
        mode, sha = obj[p]
    return mode, sha
Пример #5
0
    def lookup_path(self, lookup_obj, path):
        """Look up an object in a Git tree.

        :param lookup_obj: Callback for retrieving object by SHA1
        :param path: Path to lookup
        :return: A tuple of (mode, SHA) of the resulting path.
        """
        parts = path.split('/')
        sha = self.id
        mode = None
        for p in parts:
            if not p:
                continue
            obj = lookup_obj(sha)
            if not isinstance(obj, Tree):
                raise NotTreeError(sha)
            mode, sha = obj[p]
        return mode, sha
Пример #6
0
    def tree(self, sha=None):
        """
        Return the tree with given SHA, or if no SHA given, return the
        HEAD commit's tree. Raise an error if an object matches the SHA, 
        but is not a tree.

        :param sha: tree reference. 
        
        Note that a commit reference would not work. To get a commit's 
        tree, just provide ``c.tree``, which contains the SHA we need.
        """
        if sha is None:
            obj = self.repo[self.head().tree]
        else:
            obj = self.repo[sha]
        if type(obj) is Tree:
            return obj
        else:
            raise NotTreeError('Object is not a Tree')
Пример #7
0
    def _obj_from_tree(self, tree, path):
        """
        Walk a tree recursively to retrieve and return a blob or sub-tree 
        from the given path, or return None if one does not exist.

        :param tree: a dulwich.objects.Tree object.
        :param path: path relative to the repository root. 

        :return: Tree object, Blob object, or None if the path could 
                 not be found.
        
        For example, providing ``hopper/git.py`` would return the 
        ``git.py`` blob within the ``hopper`` sub-tree.
        """
        if type(tree) is not Tree:
            raise NotTreeError('Object is not a tree')
        # remove trailing slashes from path (so basename doesn't return '')
        if path[-1] == os.sep:
            path = path[:-1]

        # we need the head of the path, which is either the file itself or a
        # directory.
        head = path.split(os.sep)[0]
        if len(head) > 1:
            # clip head from path for recursion
            new_path = os.sep.join(path.split(os.sep)[1:])

        for entry in tree.iteritems():
            # these are dulwich.objects.TreeEntry objects
            if entry.path == head:
                # get the Tree or Blob.
                obj = self.repo[entry.sha]
                # return if we're at the right path
                if head == path:
                    return obj
                # otherwise recurse if it's a Tree
                elif type(obj) is Tree:
                    return self._obj_from_tree(obj, new_path)

        # if we get here the path wasn't there.
        return None
    def lookup_path(self, lookup_obj, path):
        """Look up an object in a Git tree.

        Args:
          lookup_obj: Callback for retrieving object by SHA1
          path: Path to lookup
        Returns: A tuple of (mode, SHA) of the resulting path.
        """
        parts = path.split(b"/")
        sha = self.id
        mode = None
        for i, p in enumerate(parts):
            if not p:
                continue
            if mode is not None and S_ISGITLINK(mode):
                raise SubmoduleEncountered(b'/'.join(parts[:i]), sha)
            obj = lookup_obj(sha)
            if not isinstance(obj, Tree):
                raise NotTreeError(sha)
            mode, sha = obj[p]
        return mode, sha
Пример #9
0
    def _write_tree_to_wt(self, tree, basepath):
        """
        Walk a tree recursively and write each blob's data to the working 
        tree.

        :param tree: a dulwich.objects.Tree object.
        :param basepath: blob data is written to:
                         ``os.path.join(basepath, blob_path)``.
                         Recursive calls will append the sub-tree
                         name to the original call.
        """
        if type(tree) is not Tree:
            raise NotTreeError('Object is not a tree')
        for entry in tree.iteritems():
            obj = self.repo[entry.sha]
            if type(obj) is Blob:
                path = os.path.join(basepath, entry.path)
                with open(path, 'wb') as fp:
                    fp.write(obj.data)
            elif type(obj) is Tree:
                new_basepath = os.path.join(basepath, entry.path)
                self._write_tree_to_wt(obj, new_basepath)
Пример #10
0
 def from_path(cls, filename):
     tree = ShaFile.from_path(filename)
     if not isinstance(tree, cls):
         raise NotTreeError(filename)
     return tree
Пример #11
0
 def tree_items(self, path):
     tree = self.get(path)
     if not isinstance(tree, Tree):
         raise NotTreeError(path)
     return [item.decode(self.encoding) for item in tree]