2
# Copyright (C) 2008 Michael Trier (mtrier@gmail.com) and contributors
4
# This module is part of GitPython and is released under
5
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
8
from lazy import LazyMixin
11
class Tree(LazyMixin):
12
def __init__(self, repo, **kwargs):
13
LazyMixin.__init__(self)
20
for k, v in kwargs.items():
24
temp = Tree.construct(self.repo, self.id)
25
self.contents = temp.contents
28
def construct(cls, repo, treeish, paths = []):
29
output = repo.git.ls_tree(treeish, *paths)
30
return Tree(repo, **{'id': treeish}).construct_initialize(repo, treeish, output)
32
def construct_initialize(self, repo, id, text):
36
self.__baked__ = False
38
for line in text.splitlines():
39
self.contents.append(self.content_from_string(self.repo, line))
41
self.contents = [c for c in self.contents if c is not None]
46
def content_from_string(self, repo, text):
48
Parse a content item and create the appropriate object
54
is the single line containing the items data in `git ls-tree` format
57
``GitPython.Blob`` or ``GitPython.Tree``
60
mode, typ, id, name = text.expandtabs(1).split(" ", 4)
65
return Tree(repo, **{'id': id, 'mode': mode, 'name': name})
67
return blob.Blob(repo, **{'id': id, 'mode': mode, 'name': name})
71
raise(TypeError, "Invalid type: %s" % typ)
73
def __div__(self, file):
75
Find the named object in this tree's contents
79
>>> Repo('/path/to/python-git').tree/'lib'
80
<GitPython.Tree "6cc23ee138be09ff8c28b07162720018b244e95e">
81
>>> Repo('/path/to/python-git').tree/'README.txt'
82
<GitPython.Blob "8b1e02c0fb554eed2ce2ef737a68bb369d7527df">
85
``GitPython.Blob`` or ``GitPython.Tree`` or ``None`` if not found
87
contents = [c for c in self.contents if c.name == file]
88
return contents and contents[0] or None
92
os.path.basename(self.name)
95
return '<GitPython.Tree "%s">' % self.id