/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 git_repository.py

GitRepository._parse_rev sets Revision.timezone to a float instead of a string.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
2
 
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
3
2
#
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
18
17
"""An adapter between a Git Repository and a Bazaar Branch"""
19
18
 
20
19
from bzrlib import (
21
 
    errors,
22
 
    inventory,
 
20
    deprecated_graph,
23
21
    repository,
24
22
    revision,
25
 
    revisiontree,
26
 
    )
27
 
from bzrlib.foreign import (
28
 
    ForeignRepository,
29
 
    )
30
 
 
31
 
from bzrlib.plugins.git.commit import (
32
 
    GitCommitBuilder,
33
 
    )
34
 
from bzrlib.plugins.git.mapping import (
35
 
    default_mapping,
36
 
    foreign_git,
37
 
    mapping_registry,
38
 
    )
39
 
from bzrlib.plugins.git.tree import (
40
 
    GitRevisionTree,
41
 
    )
42
 
from bzrlib.plugins.git.versionedfiles import (
43
 
    GitRevisions,
44
 
    GitTexts,
45
 
    )
46
 
 
47
 
 
48
 
from dulwich.objects import (
49
 
    Commit,
50
 
    )
51
 
 
52
 
 
53
 
class GitRepository(ForeignRepository):
 
23
    urlutils,
 
24
    )
 
25
 
 
26
from bzrlib.plugins.git import (
 
27
    ids,
 
28
    model,
 
29
    )
 
30
 
 
31
 
 
32
class GitRepository(repository.Repository):
54
33
    """An adapter to git repositories for bzr."""
55
34
 
56
 
    _serializer = None
57
 
    _commit_builder_class = GitCommitBuilder
58
 
    vcs = foreign_git
59
 
 
60
35
    def __init__(self, gitdir, lockfiles):
61
 
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir,
62
 
            lockfiles)
63
 
        from bzrlib.plugins.git import fetch, push
64
 
        for optimiser in [fetch.InterRemoteGitNonGitRepository,
65
 
                          fetch.InterLocalGitNonGitRepository,
66
 
                          fetch.InterGitGitRepository,
67
 
                          push.InterToLocalGitRepository,
68
 
                          push.InterToRemoteGitRepository]:
69
 
            repository.InterRepository.register_optimiser(optimiser)
 
36
        self.bzrdir = gitdir
 
37
        self.control_files = lockfiles
 
38
        gitdirectory = gitdir.transport.local_abspath('.')
 
39
        self._git = model.GitModel(gitdirectory)
 
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
 
47
        for lines in self._git.ancestor_lines(git_revisions):
 
48
            yield self._parse_rev(lines)
70
49
 
71
50
    def is_shared(self):
72
 
        return False
73
 
 
74
 
    def supports_rich_root(self):
75
51
        return True
76
52
 
77
 
    def _warn_if_deprecated(self, branch=None):
78
 
        # This class isn't deprecated
79
 
        pass
80
 
 
81
 
    def get_mapping(self):
82
 
        return default_mapping
83
 
 
84
 
    def make_working_trees(self):
85
 
        return not self._git.bare
86
 
 
87
 
    def revision_graph_can_have_wrong_parents(self):
88
 
        return False
89
 
 
90
 
    def dfetch(self, source, stop_revision):
91
 
        interrepo = repository.InterRepository.get(source, self)
92
 
        return interrepo.dfetch(stop_revision)
93
 
 
94
 
 
95
 
class LocalGitRepository(GitRepository):
96
 
    """Git repository on the file system."""
97
 
 
98
 
    def __init__(self, gitdir, lockfiles):
99
 
        GitRepository.__init__(self, gitdir, lockfiles)
100
 
        self.base = gitdir.root_transport.base
101
 
        self._git = gitdir._git
102
 
        self.signatures = None
103
 
        self.revisions = GitRevisions(self, self._git.object_store)
104
 
        self.inventories = None
105
 
        self.texts = GitTexts(self)
106
 
 
107
 
    def _iter_revision_ids(self):
108
 
        mapping = self.get_mapping()
109
 
        for sha in self._git.object_store:
110
 
            o = self._git.object_store[sha]
111
 
            if not isinstance(o, Commit):
112
 
                continue
113
 
            rev, roundtrip_revid, verifiers = mapping.import_commit(o,
114
 
                self.lookup_foreign_revision_id)
115
 
            yield o.id, rev.revision_id, roundtrip_revid
116
 
 
117
 
    def all_revision_ids(self):
118
 
        ret = set([])
119
 
        for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
120
 
            ret.add(revid)
121
 
            if roundtrip_revid:
122
 
                ret.add(roundtrip_revid)
123
 
        return ret
124
 
 
125
 
    def get_parent_map(self, revids):
126
 
        parent_map = {}
127
 
        for revision_id in revids:
128
 
            assert isinstance(revision_id, str)
129
 
            if revision_id == revision.NULL_REVISION:
130
 
                parent_map[revision_id] = ()
131
 
                continue
132
 
            hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
133
 
            try:
134
 
                commit = self._git[hexsha]
135
 
            except KeyError:
136
 
                continue
137
 
            parent_map[revision_id] = [
138
 
                self.lookup_foreign_revision_id(p, mapping)
139
 
                for p in commit.parents]
140
 
        return parent_map
141
 
 
142
 
    def get_ancestry(self, revision_id, topo_sorted=True):
143
 
        """See Repository.get_ancestry().
144
 
        """
145
 
        if revision_id is None:
146
 
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
147
 
        assert isinstance(revision_id, str)
148
 
        ancestry = []
149
 
        graph = self.get_graph()
150
 
        for rev, parents in graph.iter_ancestry([revision_id]):
151
 
            ancestry.append(rev)
152
 
        ancestry.reverse()
153
 
        return [None] + ancestry
154
 
 
155
 
    def get_signature_text(self, revision_id):
156
 
        raise errors.NoSuchRevision(self, revision_id)
157
 
 
158
 
    def pack(self, hint=None, clean_obsolete_packs=False):
159
 
        self._git.object_store.pack_loose_objects()
160
 
 
161
 
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
162
 
        """Lookup a revision id.
163
 
 
164
 
        """
165
 
        assert type(foreign_revid) is str
166
 
        if mapping is None:
167
 
            mapping = self.get_mapping()
168
 
        from dulwich.protocol import (
169
 
            ZERO_SHA,
170
 
            )
171
 
        if foreign_revid == ZERO_SHA:
172
 
            return revision.NULL_REVISION
173
 
        commit = self._git[foreign_revid]
174
 
        rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
175
 
            lambda x: None)
176
 
        # FIXME: check testament before doing this?
177
 
        if roundtrip_revid:
178
 
            return roundtrip_revid
 
53
    def get_revision_graph(self, revision_id=None):
 
54
        result = {}
 
55
        if revision_id is not None:
 
56
            param = [ids.convert_revision_id_bzr_to_git(revision_id)]
179
57
        else:
180
 
            return rev.revision_id
181
 
 
182
 
    def has_signature_for_revision_id(self, revision_id):
183
 
        return False
184
 
 
185
 
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
186
 
        try:
187
 
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
188
 
        except errors.InvalidRevisionId:
189
 
            if mapping is None:
190
 
                mapping = self.get_mapping()
191
 
            try:
192
 
                return (self._git.refs[mapping.revid_as_refname(bzr_revid)],
193
 
                        mapping)
194
 
            except KeyError:
195
 
                # Update refs from Git commit objects
196
 
                # FIXME: Hitting this a lot will be very inefficient...
197
 
                for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
198
 
                    if not roundtrip_revid:
199
 
                        continue
200
 
                    refname = mapping.revid_as_refname(roundtrip_revid)
201
 
                    self._git.refs[refname] = git_sha
202
 
                    if roundtrip_revid == bzr_revid:
203
 
                        return git_sha, mapping
204
 
                raise errors.NoSuchRevision(self, bzr_revid)
 
58
            param = None
 
59
        for node, parents in self._git.ancestry(param).iteritems():
 
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
 
64
        return result
 
65
 
 
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
205
78
 
206
79
    def get_revision(self, revision_id):
207
 
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
208
 
        try:
209
 
            commit = self._git[git_commit_id]
210
 
        except KeyError:
211
 
            raise errors.NoSuchRevision(self, revision_id)
212
 
        revision, roundtrip_revid, verifiers = mapping.import_commit(
213
 
            commit, self.lookup_foreign_revision_id)
214
 
        assert revision is not None
215
 
        # FIXME: check verifiers ?
216
 
        if roundtrip_revid:
217
 
            revision.revision_id = roundtrip_revid
218
 
        return revision
 
80
        if revision_id in self._revision_cache:
 
81
            return self._revision_cache[revision_id]
 
82
        raw = self._git.rev_list(
 
83
            [ids.convert_revision_id_bzr_to_git(revision_id)],
 
84
            max_count=1, header=True)
 
85
        return self._parse_rev(raw)
219
86
 
220
87
    def has_revision(self, revision_id):
221
88
        try:
222
 
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
223
 
        except errors.NoSuchRevision:
 
89
            self.get_revision(revision_id)
 
90
        except NoSuchRevision:
224
91
            return False
225
 
        return (git_commit_id in self._git)
226
 
 
227
 
    def has_revisions(self, revision_ids):
228
 
        return set(filter(self.has_revision, revision_ids))
229
 
 
230
 
    def get_revisions(self, revids):
231
 
        return [self.get_revision(r) for r in revids]
 
92
        else:
 
93
            return True
 
94
 
 
95
    def get_revisions(self, revisions):
 
96
        return [self.get_revision(r) for r in revisions]
 
97
 
 
98
    @classmethod
 
99
    def _parse_rev(klass, raw):
 
100
        """Parse a single git revision.
 
101
 
 
102
        * The first line is the git commit id.
 
103
        * Following lines conform to the 'name value' structure, until the
 
104
          first blank line.
 
105
        * All lines after the first blank line and until the NULL line have 4
 
106
          leading spaces and constitute the commit message.
 
107
 
 
108
        :param raw: sequence of newline-terminated strings, its last item is a
 
109
            single NULL character.
 
110
        :return: a `bzrlib.revision.Revision` object.
 
111
        """
 
112
        parents = []
 
113
        message_lines = []
 
114
        in_message = False
 
115
        committer_was_set = False
 
116
        revision_id = ids.convert_revision_id_git_to_bzr(raw[0][:-1])
 
117
        rev = revision.Revision(revision_id)
 
118
        rev.inventory_sha1 = ""
 
119
        assert raw[-1] == '\x00', (
 
120
            "Last item of raw was not a single NULL character.")
 
121
        for line in raw[1:-1]:
 
122
            if in_message:
 
123
                assert line[:4] == '    ', (
 
124
                    "Unexpected line format in commit message: %r" % line)
 
125
                message_lines.append(line[4:])
 
126
                continue
 
127
            if line == '\n':
 
128
                in_message = True
 
129
                continue
 
130
            name, value = line[:-1].split(' ', 1)
 
131
            if name == 'parent':
 
132
                rev.parent_ids.append(
 
133
                    ids.convert_revision_id_git_to_bzr(value))
 
134
                continue
 
135
            if name == 'author':
 
136
                author, timestamp, timezone = value.rsplit(' ', 2)
 
137
                rev.properties['author'] = author
 
138
                rev.properties['git-author-timestamp'] = timestamp
 
139
                rev.properties['git-author-timezone'] = timezone
 
140
                if not committer_was_set:
 
141
                    rev.committer = author
 
142
                    rev.timestamp = float(timestamp)
 
143
                    rev.timezone = klass._parse_tz(timezone)
 
144
                continue
 
145
            if name == 'committer':
 
146
                committer_was_set = True
 
147
                committer, timestamp, timezone = value.rsplit(' ', 2)
 
148
                rev.committer = committer
 
149
                rev.timestamp = float(timestamp)
 
150
                rev.timezone = klass._parse_tz(timezone)
 
151
                continue
 
152
            if name == 'tree':
 
153
                rev.properties['git-tree-id'] = value
 
154
                continue
 
155
 
 
156
        rev.message = ''.join(message_lines)
 
157
        return rev
 
158
 
 
159
    @classmethod
 
160
    def _parse_tz(klass, tz):
 
161
        """Parse a timezone specification in the [+|-]HHMM format.
 
162
 
 
163
        :return: the timezone offset in seconds.
 
164
        """
 
165
        assert len(tz) == 5
 
166
        sign = {'+': +1, '-': -1}[tz[0]]
 
167
        hours = int(tz[1:3])
 
168
        minutes = int(tz[3:])
 
169
        return float(sign * 60 * (60 * hours + minutes))
232
170
 
233
171
    def revision_trees(self, revids):
234
172
        for revid in revids:
235
173
            yield self.revision_tree(revid)
236
174
 
237
175
    def revision_tree(self, revision_id):
238
 
        revision_id = revision.ensure_null(revision_id)
239
 
        if revision_id == revision.NULL_REVISION:
240
 
            inv = inventory.Inventory(root_id=None)
241
 
            inv.revision_id = revision_id
242
 
            return revisiontree.RevisionTree(self, inv, revision_id)
243
176
        return GitRevisionTree(self, revision_id)
244
177
 
245
178
    def get_inventory(self, revision_id):
246
 
        assert revision_id != None
247
 
        return self.revision_tree(revision_id).inventory
248
 
 
249
 
    def set_make_working_trees(self, trees):
250
 
        pass
251
 
 
252
 
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
253
 
        progress=None):
254
 
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
255
 
 
256
 
    def _get_versioned_file_checker(self, text_key_references=None,
257
 
                        ancestors=None):
258
 
        return GitVersionedFileChecker(self,
259
 
            text_key_references=text_key_references, ancestors=ancestors)
260
 
 
261
 
 
262
 
class GitVersionedFileChecker(repository._VersionedFileChecker):
263
 
 
264
 
    file_ids = []
265
 
 
266
 
    def _check_file_version_parents(self, texts, progress_bar):
267
 
        return {}, []
268
 
 
269
 
 
270
 
class GitRepositoryFormat(repository.RepositoryFormat):
271
 
    """Git repository format."""
272
 
 
273
 
    supports_tree_reference = False
274
 
    rich_root_data = True
275
 
 
276
 
    def get_format_description(self):
277
 
        return "Git Repository"
278
 
 
279
 
    def initialize(self, url, shared=False, _internal=False):
280
 
        raise errors.UninitializableFormat(self)
281
 
 
282
 
    def check_conversion_target(self, target_repo_format):
283
 
        return target_repo_format.rich_root_data
284
 
 
285
 
    def get_foreign_tests_repository_factory(self):
286
 
        from bzrlib.plugins.git.tests.test_repository import (
287
 
            ForeignTestsRepositoryFactory,
288
 
            )
289
 
        return ForeignTestsRepositoryFactory()
290
 
 
291
 
    def network_name(self):
292
 
        return "git"
 
179
        revision = self.get_revision(revision_id)
 
180
        inventory = GitInventory(revision_id)
 
181
        tree_id = revision.properties['git-tree-id']
 
182
        type_map = {'blob': 'file', 'tree': 'directory' }
 
183
        def get_inventory(tree_id, prefix):
 
184
            for perms, type, obj_id, name in self._git.get_inventory(tree_id):
 
185
                full_path = prefix + name
 
186
                if type == 'blob':
 
187
                    text_sha1 = obj_id
 
188
                else:
 
189
                    text_sha1 = None
 
190
                executable = (perms[-3] in ('1', '3', '5', '7'))
 
191
                entry = GitEntry(full_path, type_map[type], revision_id,
 
192
                                 text_sha1, executable)
 
193
                inventory.entries[full_path] = entry
 
194
                if type == 'tree':
 
195
                    get_inventory(obj_id, full_path+'/')
 
196
        get_inventory(tree_id, '')
 
197
        return inventory
 
198
 
 
199
 
 
200
class GitRevisionTree(object):
 
201
 
 
202
    def __init__(self, repository, revision_id):
 
203
        self.repository = repository
 
204
        self.revision_id = revision_id
 
205
        self.inventory = repository.get_inventory(revision_id)
 
206
 
 
207
    def get_file(self, file_id):
 
208
        return iterablefile.IterableFile(self.get_file_lines(file_id))
 
209
 
 
210
    def get_file_lines(self, file_id):
 
211
        obj_id = self.inventory[file_id].text_sha1
 
212
        return self.repository._git.cat_file('blob', obj_id)
 
213
 
 
214
    def is_executable(self, file_id):
 
215
        return self.inventory[file_id].executable
 
216
 
 
217
 
 
218
class GitInventory(object):
 
219
 
 
220
    def __init__(self, revision_id):
 
221
        self.entries = {}
 
222
        self.root = GitEntry('', 'directory', revision_id)
 
223
        self.entries[''] = self.root
 
224
 
 
225
    def __getitem__(self, key):
 
226
        return self.entries[key]
 
227
 
 
228
    def iter_entries(self):
 
229
        return iter(sorted(self.entries.items()))
 
230
 
 
231
    def iter_entries_by_dir(self):
 
232
        return self.iter_entries()
 
233
 
 
234
    def __len__(self):
 
235
        return len(self.entries)
 
236
 
 
237
 
 
238
class GitEntry(object):
 
239
 
 
240
    def __init__(self, path, kind, revision, text_sha1=None, executable=False,
 
241
                 text_size=None):
 
242
        self.path = path
 
243
        self.file_id = path
 
244
        self.kind = kind
 
245
        self.executable = executable
 
246
        self.name = osutils.basename(path)
 
247
        if path == '':
 
248
            self.parent_id = None
 
249
        else:
 
250
            self.parent_id = osutils.dirname(path)
 
251
        self.revision = revision
 
252
        self.symlink_target = None
 
253
        self.text_sha1 = text_sha1
 
254
        self.text_size = None
 
255
 
 
256
    def __repr__(self):
 
257
        return "GitEntry(%r, %r, %r, %r)" % (self.path, self.kind,
 
258
                                             self.revision, self.parent_id)
 
259
 
 
260