/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
0.200.56 by Jelmer Vernooij
Switch to using GitPython rather than our own in-house stuff.
19
import git
0.200.45 by David Allouche
More performance hacking, introduce sqlite cache, escape characters in commits that break serializers.
20
import os
0.200.57 by Jelmer Vernooij
Fix more tests.
21
import time
0.200.45 by David Allouche
More performance hacking, introduce sqlite cache, escape characters in commits that break serializers.
22
23
import bzrlib
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
24
from bzrlib import (
0.200.20 by John Arbash Meinel
All tests are passing again
25
    deprecated_graph,
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
26
    errors,
0.200.38 by David Allouche
Reimplement GitRepository.get_inventory, simpler and faster.
27
    inventory,
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
28
    osutils,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
29
    repository,
0.200.29 by David Allouche
Smoke test for GitRepository.get_revision, and corresponding fixes.
30
    revision,
0.200.39 by David Allouche
Black-box text for "bzr log" in a git tree. Further simplification of GitRevisionTree.
31
    revisiontree,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
32
    urlutils,
0.200.60 by Jelmer Vernooij
Support signature functions.
33
    versionedfile,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
34
    )
0.200.45 by David Allouche
More performance hacking, introduce sqlite cache, escape characters in commits that break serializers.
35
from bzrlib.transport import get_transport
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
36
0.209.1 by Ali Sabil
Fixed wrong import for versionedfiles in repository.py
37
from bzrlib.plugins.git.foreign import (
0.208.5 by Jelmer Vernooij
Add log show function for git.
38
    versionedfiles,
39
    ForeignRevision,
0.200.20 by John Arbash Meinel
All tests are passing again
40
    )
0.200.97 by Jelmer Vernooij
use mapping object.
41
from bzrlib.plugins.git.mapping import default_mapping
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
42
43
44
class GitRepository(repository.Repository):
45
    """An adapter to git repositories for bzr."""
46
0.200.41 by David Allouche
Define _serializer = None in GitRepository.
47
    _serializer = None
48
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
49
    def __init__(self, gitdir, lockfiles):
0.200.61 by Jelmer Vernooij
Fix tests.
50
        self.base = gitdir.root_transport.base
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
51
        self.bzrdir = gitdir
52
        self.control_files = lockfiles
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
53
        self._git = gitdir._git
0.200.56 by Jelmer Vernooij
Switch to using GitPython rather than our own in-house stuff.
54
        self.texts = None
0.200.92 by Jelmer Vernooij
Update versionedfiles.
55
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
56
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
0.203.1 by Aaron Bentley
Make checkouts work
57
        self._format = GitFormat()
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
58
        self._fallback_repositories = []
0.200.45 by David Allouche
More performance hacking, introduce sqlite cache, escape characters in commits that break serializers.
59
0.200.74 by Jelmer Vernooij
Implement Repository.all_revision_ids().
60
    def _all_revision_ids(self):
61
        if self._git.heads == []:
62
            return set()
63
        ret = set()
64
        skip = 0
65
        max_count = 1000
66
        cms = None
67
        while cms != []:
68
            cms = self._git.commits("--all", max_count=max_count, skip=skip)
69
            skip += max_count
0.200.104 by Jelmer Vernooij
Use bzr-foreign function names for converting between git and bzr revids.
70
            ret.update([default_mapping.revision_id_foreign_to_bzr(cm.id) for cm in cms])
0.200.74 by Jelmer Vernooij
Implement Repository.all_revision_ids().
71
        return ret
72
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
73
    def is_shared(self):
74
        return True
75
0.200.40 by David Allouche
GitRepository.supports_rich_root() => False
76
    def supports_rich_root(self):
77
        return False
78
0.200.82 by Jelmer Vernooij
Support listing tags.
79
    #def get_revision_delta(self, revision_id):
80
    #    parent_revid = self.get_revision(revision_id).parent_ids[0]
81
    #    diff = self._git.diff(ids.convert_revision_id_bzr_to_git(parent_revid),
82
    #                   ids.convert_revision_id_bzr_to_git(revision_id))
83
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
84
    def get_ancestry(self, revision_id):
0.200.65 by Jelmer Vernooij
Implement get_ancestry properly.
85
        revision_id = revision.ensure_null(revision_id)
86
        ret = []
87
        if revision_id != revision.NULL_REVISION:
88
            skip = 0
89
            max_count = 1000
90
            cms = None
91
            while cms != []:
0.200.106 by Jelmer Vernooij
Test that getting an unknown revision fails.
92
                cms = self._git.commits(self.lookup_git_revid(revision_id, default_mapping), max_count=max_count, skip=skip)
0.200.65 by Jelmer Vernooij
Implement get_ancestry properly.
93
                skip += max_count
0.200.104 by Jelmer Vernooij
Use bzr-foreign function names for converting between git and bzr revids.
94
                ret += [default_mapping.revision_id_foreign_to_bzr(cm.id) for cm in cms]
0.200.65 by Jelmer Vernooij
Implement get_ancestry properly.
95
        return [None] + ret
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
96
97
    def get_signature_text(self, revision_id):
98
        raise errors.NoSuchRevision(self, revision_id)
99
0.200.60 by Jelmer Vernooij
Support signature functions.
100
    def has_signature_for_revision_id(self, revision_id):
101
        return False
102
0.200.57 by Jelmer Vernooij
Fix more tests.
103
    def get_parent_map(self, revision_ids):
104
        ret = {}
105
        for revid in revision_ids:
0.200.77 by Jelmer Vernooij
Handle NULL_REVISION in get_parent_map.
106
            if revid == revision.NULL_REVISION:
107
                ret[revid] = ()
108
            else:
0.200.105 by Jelmer Vernooij
Add common function for finding git commit by bzr revid.
109
                commit = self._git.commit(self.lookup_git_revid(revid, default_mapping))
0.200.104 by Jelmer Vernooij
Use bzr-foreign function names for converting between git and bzr revids.
110
                ret[revid] = tuple([default_mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
0.200.57 by Jelmer Vernooij
Fix more tests.
111
        return ret
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
112
0.200.105 by Jelmer Vernooij
Add common function for finding git commit by bzr revid.
113
    def lookup_git_revid(self, bzr_revid, mapping):
114
        try:
115
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
116
        except errors.InvalidRevisionId:
117
            raise errors.NoSuchRevision(bzr_revid, self)
118
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
119
    def get_revision(self, revision_id):
0.200.105 by Jelmer Vernooij
Add common function for finding git commit by bzr revid.
120
        git_commit_id = self.lookup_git_revid(revision_id, default_mapping)
0.200.57 by Jelmer Vernooij
Fix more tests.
121
        commit = self._git.commit(git_commit_id)
0.204.5 by James Westby
Lose the debuggin prints.
122
        # print "fetched revision:", git_commit_id
0.200.57 by Jelmer Vernooij
Fix more tests.
123
        revision = self._parse_rev(commit)
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
124
        return revision
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
125
126
    def has_revision(self, revision_id):
127
        try:
128
            self.get_revision(revision_id)
129
        except NoSuchRevision:
130
            return False
131
        else:
132
            return True
133
134
    def get_revisions(self, revisions):
135
        return [self.get_revision(r) for r in revisions]
136
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
137
    @classmethod
0.200.57 by Jelmer Vernooij
Fix more tests.
138
    def _parse_rev(klass, commit):
139
        """Convert a git commit to a bzr revision.
140
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
141
        :return: a `bzrlib.revision.Revision` object.
142
        """
0.208.5 by Jelmer Vernooij
Add log show function for git.
143
        rev = ForeignRevision(commit.id, default_mapping, default_mapping.revision_id_foreign_to_bzr(commit.id))
0.200.104 by Jelmer Vernooij
Use bzr-foreign function names for converting between git and bzr revids.
144
        rev.parent_ids = tuple([default_mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
145
        rev.inventory_sha1 = ""
0.200.63 by Jelmer Vernooij
Ignore decoding errors since git doesn't support storing encoding.
146
        rev.message = commit.message.decode("utf-8", "replace")
0.208.7 by Jelmer Vernooij
Cope with utf8 author/committer names.
147
        rev.committer = str(commit.committer).decode("utf-8", "replace")
148
        rev.properties['author'] = str(commit.author).decode("utf-8", "replace")
0.200.57 by Jelmer Vernooij
Fix more tests.
149
        rev.timestamp = time.mktime(commit.committed_date)
150
        rev.timezone = 0
0.200.32 by David Allouche
Rewrite GitRepository._parse_rev, with unit tests.
151
        return rev
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
152
153
    def revision_trees(self, revids):
154
        for revid in revids:
155
            yield self.revision_tree(revid)
156
157
    def revision_tree(self, revision_id):
0.200.57 by Jelmer Vernooij
Fix more tests.
158
        revision_id = revision.ensure_null(revision_id)
159
160
        if revision_id == revision.NULL_REVISION:
161
            inv = inventory.Inventory(root_id=None)
162
            inv.revision_id = revision_id
163
            return revisiontree.RevisionTree(self, inv, revision_id)
164
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
165
        return GitRevisionTree(self, revision_id)
166
167
    def get_inventory(self, revision_id):
0.200.57 by Jelmer Vernooij
Fix more tests.
168
        assert revision_id != None
169
        return self.revision_tree(revision_id).inventory
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
170
0.200.108 by Jelmer Vernooij
Support bzr init --git.
171
    def set_make_working_trees(self, trees):
172
        pass
173
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
174
175
def escape_file_id(file_id):
176
    return file_id.replace('_', '__').replace(' ', '_s')
0.200.39 by David Allouche
Black-box text for "bzr log" in a git tree. Further simplification of GitRevisionTree.
177
0.200.45 by David Allouche
More performance hacking, introduce sqlite cache, escape characters in commits that break serializers.
178
0.200.88 by Jelmer Vernooij
Fix RevisionTree.get_file_text().
179
def unescape_file_id(file_id):
180
    return file_id.replace("_s", " ").replace("__", "_")
181
182
0.200.39 by David Allouche
Black-box text for "bzr log" in a git tree. Further simplification of GitRevisionTree.
183
class GitRevisionTree(revisiontree.RevisionTree):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
184
185
    def __init__(self, repository, revision_id):
0.200.39 by David Allouche
Black-box text for "bzr log" in a git tree. Further simplification of GitRevisionTree.
186
        self._repository = repository
0.200.58 by Jelmer Vernooij
Fix remaining tests.
187
        self.revision_id = revision_id
0.200.105 by Jelmer Vernooij
Add common function for finding git commit by bzr revid.
188
        git_id = repository.lookup_git_revid(revision_id, default_mapping)
0.200.57 by Jelmer Vernooij
Fix more tests.
189
        self.tree = repository._git.commit(git_id).tree
0.200.58 by Jelmer Vernooij
Fix remaining tests.
190
        self._inventory = inventory.Inventory(revision_id=revision_id)
191
        self._inventory.root.revision = revision_id
192
        self._build_inventory(self.tree, self._inventory.root, "")
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
193
0.200.79 by Jelmer Vernooij
Implement RevisionTree.get_revision_id().
194
    def get_revision_id(self):
195
        return self.revision_id
196
0.200.87 by Jelmer Vernooij
Remove cache usage.
197
    def get_file_text(self, file_id):
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
198
        entry = self._inventory[file_id]
0.200.87 by Jelmer Vernooij
Remove cache usage.
199
        if entry.kind == 'directory': return ""
0.200.88 by Jelmer Vernooij
Fix RevisionTree.get_file_text().
200
        return self._repository._git.blob(entry.text_id).data
0.203.1 by Aaron Bentley
Make checkouts work
201
0.200.58 by Jelmer Vernooij
Fix remaining tests.
202
    def _build_inventory(self, tree, ie, path):
203
        assert isinstance(path, str)
0.209.2 by Ali Sabil
Ported GitRepository to the new git.Tree API
204
        for key in tree:
205
            b = tree.get(key)
0.200.58 by Jelmer Vernooij
Fix remaining tests.
206
            basename = b.name.decode("utf-8")
207
            if path == "":
208
                child_path = b.name
209
            else:
210
                child_path = urlutils.join(path, b.name)
211
            file_id = escape_file_id(child_path.encode('utf-8'))
212
            if b.mode[0] == '0':
213
                child_ie = inventory.InventoryDirectory(file_id, basename, ie.file_id)
214
            elif b.mode[0] == '1':
215
                if b.mode[1] == '0':
216
                    child_ie = inventory.InventoryFile(file_id, basename, ie.file_id)
217
                    child_ie.text_sha1 = osutils.sha_string(b.data)
218
                elif b.mode[1] == '2':
219
                    child_ie = inventory.InventoryLink(file_id, basename, ie.file_id)
220
                    child_ie.text_sha1 = osutils.sha_string("")
221
                else:
222
                    raise AssertionError(
223
                        "Unknown file kind, perms=%r." % (b.mode,))
0.200.88 by Jelmer Vernooij
Fix RevisionTree.get_file_text().
224
                child_ie.text_id = b.id
0.200.58 by Jelmer Vernooij
Fix remaining tests.
225
                child_ie.text_size = b.size
226
            else:
227
                raise AssertionError(
228
                    "Unknown blob kind, perms=%r." % (b.mode,))
229
            child_ie.executable = bool(int(b.mode[3:], 8) & 0111)
230
            child_ie.revision = self.revision_id
0.200.88 by Jelmer Vernooij
Fix RevisionTree.get_file_text().
231
            self._inventory.add(child_ie)
0.200.58 by Jelmer Vernooij
Fix remaining tests.
232
            if b.mode[0] == '0':
233
                self._build_inventory(b, child_ie, child_path)
234
0.203.1 by Aaron Bentley
Make checkouts work
235
236
class GitFormat(object):
237
238
    supports_tree_reference = False
0.200.71 by Jelmer Vernooij
Implement GitRepositoryFormat.get_format_description.
239
240
    def get_format_description(self):
241
        return "Git Repository"