/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
1
# Copyright (C) 2007 Canonical Ltd
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
"""An adapter between a Git Repository and a Bazaar Branch"""
18
19
from bzrlib import (
0.200.20 by John Arbash Meinel
All tests are passing again
20
    deprecated_graph,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
21
    repository,
22
    urlutils,
23
    )
24
0.200.20 by John Arbash Meinel
All tests are passing again
25
from bzrlib.plugins.git.gitlib import (
26
    ids,
27
    model,
28
    )
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
29
30
31
class GitRepository(repository.Repository):
32
    """An adapter to git repositories for bzr."""
33
34
    def __init__(self, gitdir, lockfiles):
35
        self.bzrdir = gitdir
36
        self.control_files = lockfiles
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
37
        gitdirectory = gitdir.transport.local_abspath('.')
38
        self._git = model.GitModel(gitdirectory)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
39
        self._revision_cache = {}
40
41
    def _ancestor_revisions(self, revision_ids):
42
        if revision_ids is not None:
43
            git_revisions = [gitrevid_from_bzr(r) for r in revision_ids]
44
        else:
45
            git_revisions = None
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
46
        for lines in self._git.ancestor_lines(git_revisions):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
47
            yield self.parse_rev(lines)
48
49
    def is_shared(self):
50
        return True
51
52
    def get_revision_graph(self, revision_id=None):
0.200.20 by John Arbash Meinel
All tests are passing again
53
        result = {}
0.200.21 by John Arbash Meinel
Fix Repository.get_revision_graph()
54
        if revision_id is not None:
55
            param = [ids.convert_revision_id_bzr_to_git(revision_id)]
56
        else:
57
            param = None
58
        for node, parents in self._git.ancestry(param).iteritems():
0.200.20 by John Arbash Meinel
All tests are passing again
59
            bzr_node = ids.convert_revision_id_git_to_bzr(node)
60
            bzr_parents = [ids.convert_revision_id_git_to_bzr(n)
61
                           for n in parents]
62
            result[bzr_node] = bzr_parents
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
63
        return result
64
0.200.21 by John Arbash Meinel
Fix Repository.get_revision_graph()
65
    def get_revision_graph_with_ghosts(self, revision_ids=None):
66
        graph = deprecated_graph.Graph()
67
        if revision_ids is not None:
68
            revision_ids = [ids.convert_revision_id_bzr_to_git(r)
69
                            for r in revision_ids]
70
        for node, parents in self._git.ancestry(revision_ids).iteritems():
71
            bzr_node = ids.convert_revision_id_git_to_bzr(node)
72
            bzr_parents = [ids.convert_revision_id_git_to_bzr(n)
73
                           for n in parents]
74
75
            graph.add_node(bzr_node, bzr_parents)
76
        return graph
77
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
78
    def get_revision(self, revision_id):
79
        if revision_id in self._revision_cache:
80
            return self._revision_cache[revision_id]
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
81
        raw = self._git.rev_list([gitrevid_from_bzr(revision_id)], max_count=1,
82
                                 header=True)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
83
        return self.parse_rev(raw)
84
85
    def has_revision(self, revision_id):
86
        try:
87
            self.get_revision(revision_id)
88
        except NoSuchRevision:
89
            return False
90
        else:
91
            return True
92
93
    def get_revisions(self, revisions):
94
        return [self.get_revision(r) for r in revisions]
95
96
    def parse_rev(self, raw):
97
        # first field is the rev itself.
98
        # then its 'field value'
99
        # until the EOF??
100
        parents = []
101
        log = []
102
        in_log = False
103
        committer = None
104
        revision_id = bzrrevid_from_git(raw[0][:-1])
105
        for field in raw[1:]:
106
            #if field.startswith('author '):
107
            #    committer = field[7:]
108
            if field.startswith('parent '):
109
                parents.append(bzrrevid_from_git(field.split()[1]))
110
            elif field.startswith('committer '):
111
                commit_fields = field.split()
112
                if committer is None:
113
                    committer = ' '.join(commit_fields[1:-3])
114
                timestamp = commit_fields[-2]
115
                timezone = commit_fields[-1]
116
            elif field.startswith('tree '):
117
                tree_id = field.split()[1]
118
            elif in_log:
119
                log.append(field[4:])
120
            elif field == '\n':
121
                in_log = True
122
123
        log = ''.join(log)
124
        result = Revision(revision_id)
125
        result.parent_ids = parents
126
        result.message = log
127
        result.inventory_sha1 = ""
128
        result.timezone = timezone and int(timezone)
129
        result.timestamp = float(timestamp)
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
130
        result.committer = committer
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
131
        result.properties['git-tree-id'] = tree_id
132
        return result
133
134
    def revision_trees(self, revids):
135
        for revid in revids:
136
            yield self.revision_tree(revid)
137
138
    def revision_tree(self, revision_id):
139
        return GitRevisionTree(self, revision_id)
140
141
    def get_inventory(self, revision_id):
142
        revision = self.get_revision(revision_id)
143
        inventory = GitInventory(revision_id)
144
        tree_id = revision.properties['git-tree-id']
145
        type_map = {'blob': 'file', 'tree': 'directory' }
146
        def get_inventory(tree_id, prefix):
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
147
            for perms, type, obj_id, name in self._git.get_inventory(tree_id):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
148
                full_path = prefix + name
149
                if type == 'blob':
150
                    text_sha1 = obj_id
151
                else:
152
                    text_sha1 = None
153
                executable = (perms[-3] in ('1', '3', '5', '7'))
154
                entry = GitEntry(full_path, type_map[type], revision_id,
155
                                 text_sha1, executable)
156
                inventory.entries[full_path] = entry
157
                if type == 'tree':
158
                    get_inventory(obj_id, full_path+'/')
159
        get_inventory(tree_id, '')
160
        return inventory
161
162
163
class GitRevisionTree(object):
164
165
    def __init__(self, repository, revision_id):
166
        self.repository = repository
167
        self.revision_id = revision_id
168
        self.inventory = repository.get_inventory(revision_id)
169
170
    def get_file(self, file_id):
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
171
        return iterablefile.IterableFile(self.get_file_lines(file_id))
172
173
    def get_file_lines(self, file_id):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
174
        obj_id = self.inventory[file_id].text_sha1
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
175
        return self.repository._git.cat_file('blob', obj_id)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
176
177
    def is_executable(self, file_id):
178
        return self.inventory[file_id].executable
179
180
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
181
class GitInventory(object):
182
183
    def __init__(self, revision_id):
184
        self.entries = {}
185
        self.root = GitEntry('', 'directory', revision_id)
186
        self.entries[''] = self.root
187
188
    def __getitem__(self, key):
189
        return self.entries[key]
190
191
    def iter_entries(self):
192
        return iter(sorted(self.entries.items()))
193
194
    def iter_entries_by_dir(self):
195
        return self.iter_entries()
196
197
    def __len__(self):
198
        return len(self.entries)
199
200
201
class GitEntry(object):
202
203
    def __init__(self, path, kind, revision, text_sha1=None, executable=False,
204
                 text_size=None):
205
        self.path = path
206
        self.file_id = path
207
        self.kind = kind
208
        self.executable = executable
209
        self.name = osutils.basename(path)
210
        if path == '':
211
            self.parent_id = None
212
        else:
213
            self.parent_id = osutils.dirname(path)
214
        self.revision = revision
215
        self.symlink_target = None
216
        self.text_sha1 = text_sha1
217
        self.text_size = None
218
219
    def __repr__(self):
220
        return "GitEntry(%r, %r, %r, %r)" % (self.path, self.kind,
221
                                             self.revision, self.parent_id)
222
223