/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.335 by Jelmer Vernooij
Move inventory code to separate module.
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
"""Git inventory."""
19
20
21
from bzrlib import (
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
22
    errors,
0.200.335 by Jelmer Vernooij
Move inventory code to separate module.
23
    inventory,
24
    osutils,
25
    urlutils,
26
    )
27
28
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
29
class GitInventoryEntry(inventory.InventoryEntry):
30
31
    def __init__(self, inv, parent_id, hexsha, path, name, executable):
32
        self.name = name
33
        self.parent_id = parent_id
34
        self._inventory = inv
35
        self._object = None
36
        self.hexsha = hexsha
37
        self.path = path
38
        self.revision = self._inventory.revision_id
39
        self.executable = executable
40
        self.file_id = self._inventory.mapping.generate_file_id(path.encode('utf-8'))
41
42
    @property
43
    def object(self):
44
        if self._object is None:
45
            self._object = self._inventory.store[self.hexsha]
46
        return self._object
47
48
49
class GitInventoryFile(GitInventoryEntry):
50
51
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
52
        super(GitInventoryFile, self).__init__(inv, parent_id, hexsha, path, basename, executable)
53
        self.kind = 'file'
54
        self.symlink_target = None
55
56
    @property
57
    def text_sha1(self):
58
        return osutils.sha_string(self.object.data)
59
60
    @property
61
    def text_size(self):
62
        return len(self.object.data)
63
64
    def kind_character(self):
65
        """See InventoryEntry.kind_character."""
66
        return ''
67
68
69
class GitInventoryLink(GitInventoryEntry):
70
71
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
72
        super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
73
        self.text_sha1 = None
74
        self.text_size = None
75
        self.kind = 'symlink'
76
77
    @property
78
    def symlink_target(self):
79
        return self.object.data
80
81
    def kind_character(self):
82
        """See InventoryEntry.kind_character."""
83
        return ''
84
85
86
class GitInventoryDirectory(GitInventoryEntry):
87
88
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
89
        super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
90
        self.text_sha1 = None
91
        self.text_size = None
92
        self.symlink_target = None
93
        self.kind = 'directory'
94
95
    def kind_character(self):
96
        """See InventoryEntry.kind_character."""
97
        return '/'
98
99
    @property
100
    def children(self):
101
        ret = {}
102
        for mode, name, hexsha in self.object.entries():
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
103
            basename = name.decode("utf-8")
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
104
            child_path = osutils.pathjoin(self.path, basename)
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
105
            entry_kind = (mode & 0700000) / 0100000
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
106
            fs_mode = mode & 0777
107
            executable = bool(fs_mode & 0111)
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
108
            if entry_kind == 0:
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
109
                kind_class = GitInventoryDirectory
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
110
            elif entry_kind == 1:
111
                file_kind = (mode & 070000) / 010000
112
                if file_kind == 0:
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
113
                    kind_class = GitInventoryFile
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
114
                elif file_kind == 2:
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
115
                    kind_class = GitInventoryLink
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
116
                else:
117
                    raise AssertionError(
118
                        "Unknown file kind, perms=%o." % (mode,))
0.200.335 by Jelmer Vernooij
Move inventory code to separate module.
119
            else:
120
                raise AssertionError(
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
121
                    "Unknown blob kind, perms=%r." % (mode,))
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
122
            ret[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
123
        return ret
124
125
126
class GitInventory(inventory.Inventory):
127
128
    def __init__(self, tree_id, mapping, store, revision_id):
129
        super(GitInventory, self).__init__(revision_id)
130
        self.store = store
131
        self.mapping = mapping
132
        self.root = GitInventoryDirectory(self, None, tree_id, "", "", False)
133
134
    def _get_ie(self, path):
135
        parts = path.split("/")
136
        ie = self.root
137
        for name in parts:
138
            ie = ie.children[name] 
139
        return ie
140
141
    def has_filename(self, path):
142
        try:
143
            self._get_ie(path)
144
            return True
145
        except KeyError:
146
            return False
147
148
    def has_id(self, file_id):
149
        try:
150
            self.id2path(file_id)
151
            return True
152
        except errors.NoSuchId:
153
            return False
154
155
    def id2path(self, file_id):
156
        path = self.mapping.parse_file_id(file_id)
157
        try:
158
            ie = self._get_ie(path)
159
            assert ie.path == path
160
        except KeyError:
161
            raise errors.NoSuchId(None, file_id)
162
163
    def path2id(self, path):
164
        try:
165
            return self._get_ie(path).file_id
166
        except KeyError:
167
            return None
168
169
    def __getitem__(self, file_id):
170
        if file_id == inventory.ROOT_ID:
171
            return self.root
172
        path = self.mapping.parse_file_id(file_id)
173
        return self._get_ie(path)