/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):
53
        if revision_id is None:
54
            revisions = None
55
        else:
56
            revisions = [revision_id]
57
        return self.get_revision_graph_with_ghosts(revisions).get_ancestors()
58
59
    def get_revision_graph_with_ghosts(self, revision_ids=None):
0.200.20 by John Arbash Meinel
All tests are passing again
60
        result = {}
61
        for node, parents in self._git.ancestry(None).iteritems():
62
            bzr_node = ids.convert_revision_id_git_to_bzr(node)
63
            bzr_parents = [ids.convert_revision_id_git_to_bzr(n)
64
                           for n in parents]
65
            result[bzr_node] = bzr_parents
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
66
        return result
67
68
    def get_revision(self, revision_id):
69
        if revision_id in self._revision_cache:
70
            return self._revision_cache[revision_id]
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
71
        raw = self._git.rev_list([gitrevid_from_bzr(revision_id)], max_count=1,
72
                                 header=True)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
73
        return self.parse_rev(raw)
74
75
    def has_revision(self, revision_id):
76
        try:
77
            self.get_revision(revision_id)
78
        except NoSuchRevision:
79
            return False
80
        else:
81
            return True
82
83
    def get_revisions(self, revisions):
84
        return [self.get_revision(r) for r in revisions]
85
86
    def parse_rev(self, raw):
87
        # first field is the rev itself.
88
        # then its 'field value'
89
        # until the EOF??
90
        parents = []
91
        log = []
92
        in_log = False
93
        committer = None
94
        revision_id = bzrrevid_from_git(raw[0][:-1])
95
        for field in raw[1:]:
96
            #if field.startswith('author '):
97
            #    committer = field[7:]
98
            if field.startswith('parent '):
99
                parents.append(bzrrevid_from_git(field.split()[1]))
100
            elif field.startswith('committer '):
101
                commit_fields = field.split()
102
                if committer is None:
103
                    committer = ' '.join(commit_fields[1:-3])
104
                timestamp = commit_fields[-2]
105
                timezone = commit_fields[-1]
106
            elif field.startswith('tree '):
107
                tree_id = field.split()[1]
108
            elif in_log:
109
                log.append(field[4:])
110
            elif field == '\n':
111
                in_log = True
112
113
        log = ''.join(log)
114
        result = Revision(revision_id)
115
        result.parent_ids = parents
116
        result.message = log
117
        result.inventory_sha1 = ""
118
        result.timezone = timezone and int(timezone)
119
        result.timestamp = float(timestamp)
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
120
        result.committer = committer
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
121
        result.properties['git-tree-id'] = tree_id
122
        return result
123
124
    def revision_trees(self, revids):
125
        for revid in revids:
126
            yield self.revision_tree(revid)
127
128
    def revision_tree(self, revision_id):
129
        return GitRevisionTree(self, revision_id)
130
131
    def get_inventory(self, revision_id):
132
        revision = self.get_revision(revision_id)
133
        inventory = GitInventory(revision_id)
134
        tree_id = revision.properties['git-tree-id']
135
        type_map = {'blob': 'file', 'tree': 'directory' }
136
        def get_inventory(tree_id, prefix):
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
137
            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.
138
                full_path = prefix + name
139
                if type == 'blob':
140
                    text_sha1 = obj_id
141
                else:
142
                    text_sha1 = None
143
                executable = (perms[-3] in ('1', '3', '5', '7'))
144
                entry = GitEntry(full_path, type_map[type], revision_id,
145
                                 text_sha1, executable)
146
                inventory.entries[full_path] = entry
147
                if type == 'tree':
148
                    get_inventory(obj_id, full_path+'/')
149
        get_inventory(tree_id, '')
150
        return inventory
151
152
153
class GitRevisionTree(object):
154
155
    def __init__(self, repository, revision_id):
156
        self.repository = repository
157
        self.revision_id = revision_id
158
        self.inventory = repository.get_inventory(revision_id)
159
160
    def get_file(self, file_id):
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
161
        return iterablefile.IterableFile(self.get_file_lines(file_id))
162
163
    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.
164
        obj_id = self.inventory[file_id].text_sha1
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
165
        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.
166
167
    def is_executable(self, file_id):
168
        return self.inventory[file_id].executable
169
170
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
171
class GitInventory(object):
172
173
    def __init__(self, revision_id):
174
        self.entries = {}
175
        self.root = GitEntry('', 'directory', revision_id)
176
        self.entries[''] = self.root
177
178
    def __getitem__(self, key):
179
        return self.entries[key]
180
181
    def iter_entries(self):
182
        return iter(sorted(self.entries.items()))
183
184
    def iter_entries_by_dir(self):
185
        return self.iter_entries()
186
187
    def __len__(self):
188
        return len(self.entries)
189
190
191
class GitEntry(object):
192
193
    def __init__(self, path, kind, revision, text_sha1=None, executable=False,
194
                 text_size=None):
195
        self.path = path
196
        self.file_id = path
197
        self.kind = kind
198
        self.executable = executable
199
        self.name = osutils.basename(path)
200
        if path == '':
201
            self.parent_id = None
202
        else:
203
            self.parent_id = osutils.dirname(path)
204
        self.revision = revision
205
        self.symlink_target = None
206
        self.text_sha1 = text_sha1
207
        self.text_size = None
208
209
    def __repr__(self):
210
        return "GitEntry(%r, %r, %r, %r)" % (self.path, self.kind,
211
                                             self.revision, self.parent_id)
212
213