/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to inventory.py

  • Committer: Robert Collins
  • Date: 2010-05-05 00:05:29 UTC
  • mto: This revision was merged to the branch mainline in revision 5206.
  • Revision ID: robertc@robertcollins.net-20100505000529-ltmllyms5watqj5u
Make 'pydoc bzrlib.tests.build_tree_shape' useful.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 (
22
 
    errors,
23
 
    inventory,
24
 
    osutils,
25
 
    urlutils,
26
 
    )
27
 
 
28
 
 
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.text_id = None
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
 
 
65
 
    def kind_character(self):
66
 
        """See InventoryEntry.kind_character."""
67
 
        return ''
68
 
 
69
 
    def copy(self):
70
 
        other = inventory.InventoryFile(self.file_id, self.name, self.parent_id)
71
 
        other.executable = self.executable
72
 
        other.text_id = self.text_id
73
 
        other.text_sha1 = self.text_sha1
74
 
        other.text_size = self.text_size
75
 
        other.revision = self.revision
76
 
        return other
77
 
 
78
 
 
79
 
class GitInventoryLink(GitInventoryEntry):
80
 
 
81
 
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
82
 
        super(GitInventoryLink, self).__init__(inv, parent_id, hexsha, path, basename, executable)
83
 
        self.text_sha1 = None
84
 
        self.text_size = None
85
 
        self.kind = 'symlink'
86
 
 
87
 
    @property
88
 
    def symlink_target(self):
89
 
        return self.object.data
90
 
 
91
 
    def kind_character(self):
92
 
        """See InventoryEntry.kind_character."""
93
 
        return ''
94
 
 
95
 
    def copy(self):
96
 
        other = inventory.InventoryLink(self.file_id, self.name, self.parent_id)
97
 
        other.symlink_target = self.symlink_target
98
 
        other.revision = self.revision
99
 
        return other
100
 
 
101
 
 
102
 
class GitInventoryDirectory(GitInventoryEntry):
103
 
 
104
 
    def __init__(self, inv, parent_id, hexsha, path, basename, executable):
105
 
        super(GitInventoryDirectory, self).__init__(inv, parent_id, hexsha, path, basename, executable)
106
 
        self.text_sha1 = None
107
 
        self.text_size = None
108
 
        self.symlink_target = None
109
 
        self.kind = 'directory'
110
 
 
111
 
    def kind_character(self):
112
 
        """See InventoryEntry.kind_character."""
113
 
        return '/'
114
 
 
115
 
    @property
116
 
    def children(self):
117
 
        ret = {}
118
 
        for mode, name, hexsha in self.object.entries():
119
 
            basename = name.decode("utf-8")
120
 
            child_path = osutils.pathjoin(self.path, basename)
121
 
            entry_kind = (mode & 0700000) / 0100000
122
 
            fs_mode = mode & 0777
123
 
            executable = bool(fs_mode & 0111)
124
 
            if entry_kind == 0:
125
 
                kind_class = GitInventoryDirectory
126
 
            elif entry_kind == 1:
127
 
                file_kind = (mode & 070000) / 010000
128
 
                if file_kind == 0:
129
 
                    kind_class = GitInventoryFile
130
 
                elif file_kind == 2:
131
 
                    kind_class = GitInventoryLink
132
 
                else:
133
 
                    raise AssertionError(
134
 
                        "Unknown file kind, perms=%o." % (mode,))
135
 
            else:
136
 
                raise AssertionError(
137
 
                    "Unknown blob kind, perms=%r." % (mode,))
138
 
            ret[basename] = kind_class(self._inventory, self.file_id, hexsha, child_path, basename, executable)
139
 
        return ret
140
 
 
141
 
    def copy(self):
142
 
        other = inventory.InventoryDirectory(self.file_id, self.name, 
143
 
                                             self.parent_id)
144
 
        other.revision = self.revision
145
 
        # note that children are *not* copied; they're pulled across when
146
 
        # others are added
147
 
        return other
148
 
 
149
 
 
150
 
class GitInventory(inventory.Inventory):
151
 
 
152
 
    def __init__(self, tree_id, mapping, store, revision_id):
153
 
        super(GitInventory, self).__init__(revision_id=revision_id)
154
 
        self.store = store
155
 
        self.mapping = mapping
156
 
        self.root = GitInventoryDirectory(self, None, tree_id, "", "", False)
157
 
 
158
 
    def _get_ie(self, path):
159
 
        parts = path.split("/")
160
 
        ie = self.root
161
 
        for name in parts:
162
 
            ie = ie.children[name] 
163
 
        return ie
164
 
 
165
 
    def has_filename(self, path):
166
 
        try:
167
 
            self._get_ie(path)
168
 
            return True
169
 
        except KeyError:
170
 
            return False
171
 
 
172
 
    def has_id(self, file_id):
173
 
        try:
174
 
            self.id2path(file_id)
175
 
            return True
176
 
        except errors.NoSuchId:
177
 
            return False
178
 
 
179
 
    def id2path(self, file_id):
180
 
        path = self.mapping.parse_file_id(file_id)
181
 
        try:
182
 
            ie = self._get_ie(path)
183
 
            assert ie.path == path
184
 
        except KeyError:
185
 
            raise errors.NoSuchId(None, file_id)
186
 
 
187
 
    def path2id(self, path):
188
 
        try:
189
 
            return self._get_ie(path).file_id
190
 
        except KeyError:
191
 
            return None
192
 
 
193
 
    def __getitem__(self, file_id):
194
 
        if file_id == inventory.ROOT_ID:
195
 
            return self.root
196
 
        path = self.mapping.parse_file_id(file_id)
197
 
        try:
198
 
            return self._get_ie(path)
199
 
        except KeyError:
200
 
            raise errors.NoSuchId(None, file_id)