/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'
0.200.380 by Jelmer Vernooij
Support copy on inventory entries.
54
        self.text_id = None
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
55
        self.symlink_target = None
56
57
    @property
58
    def text_sha1(self):
59
        return osutils.sha_string(self.object.data)
60
61
    @property
62
    def text_size(self):
63
        return len(self.object.data)
64
0.200.397 by Jelmer Vernooij
Fix GitInventory.__repr__ to include more information.
65
    def __repr__(self):
66
        return ("%s(%r, %r, parent_id=%r, sha1=%r, len=%s, revision=%s)"
67
                % (self.__class__.__name__,
68
                   self.file_id,
69
                   self.name,
70
                   self.parent_id,
71
                   self.text_sha1,
72
                   self.text_size,
73
                   self.revision))
74
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
75
    def kind_character(self):
76
        """See InventoryEntry.kind_character."""
77
        return ''
78
0.200.380 by Jelmer Vernooij
Support copy on inventory entries.
79
    def copy(self):
80
        other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
81
        other.executable = self.executable
82
        other.text_id = self.text_id
83
        other.text_sha1 = self.text_sha1
84
        other.text_size = self.text_size
85
        other.revision = self.revision
86
        return other
87
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
88
89
class GitInventoryLink(GitInventoryEntry):
90
91
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
92
        super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
93
        self.text_sha1 = None
94
        self.text_size = None
95
        self.kind = 'symlink'
96
97
    @property
98
    def symlink_target(self):
99
        return self.object.data
100
101
    def kind_character(self):
102
        """See InventoryEntry.kind_character."""
103
        return ''
104
0.200.380 by Jelmer Vernooij
Support copy on inventory entries.
105
    def copy(self):
106
        other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
107
        other.symlink_target = self.symlink_target
108
        other.revision = self.revision
109
        return other
110
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
111
112
class GitInventoryDirectory(GitInventoryEntry):
113
114
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
115
        super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
116
        self.text_sha1 = None
117
        self.text_size = None
118
        self.symlink_target = None
119
        self.kind = 'directory'
120
121
    def kind_character(self):
122
        """See InventoryEntry.kind_character."""
123
        return '/'
124
125
    @property
126
    def children(self):
127
        ret = {}
128
        for mode, name, hexsha in self.object.entries():
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
129
            basename = name.decode("utf-8")
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
130
            child_path = osutils.pathjoin(self.path, basename)
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
131
            entry_kind = (mode & 0700000) / 0100000
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
132
            fs_mode = mode & 0777
133
            executable = bool(fs_mode & 0111)
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
134
            if entry_kind == 0:
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
135
                kind_class = GitInventoryDirectory
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
136
            elif entry_kind == 1:
137
                file_kind = (mode & 070000) / 010000
138
                if file_kind == 0:
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
139
                    kind_class = GitInventoryFile
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
140
                elif file_kind == 2:
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
141
                    kind_class = GitInventoryLink
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
142
                else:
143
                    raise AssertionError(
144
                        "Unknown file kind, perms=%o." % (mode,))
0.200.335 by Jelmer Vernooij
Move inventory code to separate module.
145
            else:
146
                raise AssertionError(
0.200.336 by Jelmer Vernooij
Use custom GitInventory class.
147
                    "Unknown blob kind, perms=%r." % (mode,))
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
148
            ret[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
149
        return ret
150
0.200.380 by Jelmer Vernooij
Support copy on inventory entries.
151
    def copy(self):
152
        other = inventory.InventoryDirectory(self.file_id, self.name, 
153
                                             self.parent_id)
154
        other.revision = self.revision
155
        # note that children are *not* copied; they're pulled across when
156
        # others are added
157
        return other
158
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
159
160
class GitInventory(inventory.Inventory):
161
162
    def __init__(self, tree_id, mapping, store, revision_id):
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
163
        super(GitInventory, self).__init__(revision_id=revision_id)
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
164
        self.store = store
165
        self.mapping = mapping
0.200.397 by Jelmer Vernooij
Fix GitInventory.__repr__ to include more information.
166
        self.root = GitInventoryDirectory(self, None, tree_id, u"", u"", False)
0.200.337 by Jelmer Vernooij
Use CommonInventory to lazily evaluate trees.
167
168
    def _get_ie(self, path):
169
        parts = path.split("/")
170
        ie = self.root
171
        for name in parts:
172
            ie = ie.children[name] 
173
        return ie
174
175
    def has_filename(self, path):
176
        try:
177
            self._get_ie(path)
178
            return True
179
        except KeyError:
180
            return False
181
182
    def has_id(self, file_id):
183
        try:
184
            self.id2path(file_id)
185
            return True
186
        except errors.NoSuchId:
187
            return False
188
189
    def id2path(self, file_id):
190
        path = self.mapping.parse_file_id(file_id)
191
        try:
192
            ie = self._get_ie(path)
193
            assert ie.path == path
194
        except KeyError:
195
            raise errors.NoSuchId(None, file_id)
196
197
    def path2id(self, path):
198
        try:
199
            return self._get_ie(path).file_id
200
        except KeyError:
201
            return None
202
203
    def __getitem__(self, file_id):
204
        if file_id == inventory.ROOT_ID:
205
            return self.root
206
        path = self.mapping.parse_file_id(file_id)
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
207
        try:
208
            return self._get_ie(path)
209
        except KeyError:
210
            raise errors.NoSuchId(None, file_id)