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