~ubuntu-branches/ubuntu/raring/python-git/raring

« back to all changes in this revision

Viewing changes to lib/git/tree.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Watkins
  • Date: 2008-08-18 19:50:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080818195022-kuoirgrn2uslk6ll
Tags: upstream-0.1.4.1
ImportĀ upstreamĀ versionĀ 0.1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# tree.py
 
2
# Copyright (C) 2008 Michael Trier (mtrier@gmail.com) and contributors
 
3
#
 
4
# This module is part of GitPython and is released under
 
5
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
 
6
 
 
7
import os
 
8
from lazy import LazyMixin
 
9
import blob
 
10
 
 
11
class Tree(LazyMixin):
 
12
    def __init__(self, repo, **kwargs):
 
13
        LazyMixin.__init__(self)
 
14
        self.repo = repo
 
15
        self.id = None
 
16
        self.mode = None
 
17
        self.name = None
 
18
        self.contents = None
 
19
 
 
20
        for k, v in kwargs.items():
 
21
            setattr(self, k, v)
 
22
 
 
23
    def __bake__(self):
 
24
        temp = Tree.construct(self.repo, self.id)
 
25
        self.contents = temp.contents
 
26
 
 
27
    @classmethod
 
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)
 
31
 
 
32
    def construct_initialize(self, repo, id, text):
 
33
        self.repo = repo
 
34
        self.id = id
 
35
        self.contents = []
 
36
        self.__baked__ = False
 
37
 
 
38
        for line in text.splitlines():
 
39
            self.contents.append(self.content_from_string(self.repo, line))
 
40
 
 
41
        self.contents = [c for c in self.contents if c is not None]
 
42
 
 
43
        self.__bake_it__()
 
44
        return self
 
45
 
 
46
    def content_from_string(self, repo, text):
 
47
        """
 
48
        Parse a content item and create the appropriate object
 
49
 
 
50
        ``repo``
 
51
            is the Repo
 
52
 
 
53
         ``text``
 
54
            is the single line containing the items data in `git ls-tree` format
 
55
 
 
56
        Returns
 
57
            ``GitPython.Blob`` or ``GitPython.Tree``
 
58
        """
 
59
        try:
 
60
            mode, typ, id, name = text.expandtabs(1).split(" ", 4)
 
61
        except:
 
62
            return None
 
63
 
 
64
        if typ == "tree":
 
65
            return Tree(repo, **{'id': id, 'mode': mode, 'name': name})
 
66
        elif typ == "blob":
 
67
            return blob.Blob(repo, **{'id': id, 'mode': mode, 'name': name})
 
68
        elif typ == "commit":
 
69
            return None
 
70
        else:
 
71
          raise(TypeError, "Invalid type: %s" % typ)
 
72
 
 
73
    def __div__(self, file):
 
74
        """
 
75
        Find the named object in this tree's contents
 
76
 
 
77
        Examples::
 
78
 
 
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">
 
83
 
 
84
        Returns
 
85
            ``GitPython.Blob`` or ``GitPython.Tree`` or ``None`` if not found
 
86
        """
 
87
        contents = [c for c in self.contents if c.name == file]
 
88
        return contents and contents[0] or None
 
89
 
 
90
    @property
 
91
    def basename(self):
 
92
        os.path.basename(self.name)
 
93
 
 
94
    def __repr__(self):
 
95
        return '<GitPython.Tree "%s">' % self.id