/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.1 by Robert Collins
Commit initial content.
1
# Copyright (C) 2006 Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19
"""A GIT branch and repository format implementation for bzr."""
20
21
0.200.2 by Aaron Bentley
Get viz working, with new -r support
22
from StringIO import StringIO
23
0.200.1 by Robert Collins
Commit initial content.
24
import stgit
25
import stgit.git as git
26
27
from bzrlib.decorators import *
28
import bzrlib.branch
29
import bzrlib.bzrdir
30
import bzrlib.errors as errors
31
import bzrlib.repository
32
from bzrlib.revision import Revision
33
34
0.200.2 by Aaron Bentley
Get viz working, with new -r support
35
class GitTransport(object):
36
37
    def __init__(self):
38
        self.base = object()
39
40
    def get(self, relpath):
41
        assert relpath == 'branch.conf'
42
        return StringIO()
43
44
0.200.1 by Robert Collins
Commit initial content.
45
def gitrevid_from_bzr(revision_id):
46
    return revision_id[4:]
47
48
49
def bzrrevid_from_git(revision_id):
50
    return "git:" + revision_id
51
52
53
class GitLock(object):
54
    """A lock that thunks through to Git."""
55
56
    def lock_write(self):
57
        pass
58
59
    def lock_read(self):
60
        pass
61
62
    def unlock(self):
63
        pass
64
65
66
class GitLockableFiles(bzrlib.lockable_files.LockableFiles):
67
    """Git specific lockable files abstraction."""
68
69
    def __init__(self, lock):
70
        self._lock = lock
71
        self._transaction = None
72
        self._lock_mode = None
73
        self._lock_count = 0
0.200.2 by Aaron Bentley
Get viz working, with new -r support
74
        self._transport = GitTransport() 
0.200.1 by Robert Collins
Commit initial content.
75
76
77
class GitDir(bzrlib.bzrdir.BzrDir):
78
    """An adapter to the '.git' dir used by git."""
79
80
    def __init__(self, transport, lockfiles, format):
81
        self._format = format
82
        self.root_transport = transport
83
        self.transport = transport.clone('.git')
84
        self._lockfiles = lockfiles
85
86
    def get_branch_transport(self, branch_format):
87
        if branch_format is None:
88
            return self.transport
89
        if isinstance(branch_format, GitBzrDirFormat):
90
            return self.transport
91
        raise errors.IncompatibleFormat(branch_format, self._format)
92
93
    get_repository_transport = get_branch_transport
94
    get_workingtree_transport = get_branch_transport
95
96
    def is_supported(self):
97
        return True
98
99
    def open_branch(self, ignored=None):
100
        """'crate' a branch for this dir."""
101
        return GitBranch(self, self._lockfiles)
102
103
    def open_repository(self, shared=False):
104
        """'open' a repository for this dir."""
105
        return GitRepository(self._gitrepo, self, self._lockfiles)
106
107
108
class GitBzrDirFormat(bzrlib.bzrdir.BzrDirFormat):
109
    """The .git directory control format."""
110
111
    @classmethod
112
    def _known_formats(self):
113
        return set([GitBzrDirFormat()])
114
115
    def open(self, transport, _create=False, _found=None):
116
        """Open this directory.
117
        
118
        :param _create: create the git dir on the fly. private to GitDirFormat.
119
        """
120
        # we dont grok readonly - git isn't integrated with transport.
121
        url = transport.base
122
        if url.startswith('readonly+'):
123
            url = url[len('readonly+'):]
124
        if url.startswith('file://'):
125
            url = url[len('file://'):]
126
        url = url.encode('utf8')
127
        lockfiles = GitLockableFiles(GitLock())
128
        return GitDir(transport, lockfiles, self)
129
130
    @classmethod
131
    def probe_transport(klass, transport):
132
        """Our format is present if the transport ends in '.not/'."""
133
        # little ugly, but works
134
        format = klass() 
135
        # try a manual probe first, its a little faster perhaps ?
136
        if transport.has('.git'):
137
            return format
138
        # delegate to the main opening code. This pays a double rtt cost at the
139
        # moment, so perhaps we want probe_transport to return the opened thing
140
        # rather than an openener ? or we could return a curried thing with the
141
        # dir to open already instantiated ? Needs more thought.
142
        try:
143
            format.open(transport)
144
            return format
145
        except Exception, e:
146
            raise errors.NotBranchError(path=transport.base)
147
        raise errors.NotBranchError(path=transport.base)
148
149
150
bzrlib.bzrdir.BzrDirFormat.register_control_format(GitBzrDirFormat)
151
152
153
class GitBranch(bzrlib.branch.Branch):
154
    """An adapter to git repositories for bzr Branch objects."""
155
156
    def __init__(self, gitdir, lockfiles):
157
        self.bzrdir = gitdir
158
        self.control_files = lockfiles
159
        self.repository = GitRepository(gitdir, lockfiles)
160
        self.base = gitdir.root_transport.base
161
162
    def lock_write(self):
163
        self.control_files.lock_write()
164
165
    @needs_read_lock
166
    def last_revision(self):
167
        # perhaps should escape this ?
168
        return bzrrevid_from_git(git.get_head())
169
0.200.2 by Aaron Bentley
Get viz working, with new -r support
170
    def revision_history(self):
171
        history = [self.last_revision()]
172
        while True:
173
            revision = self.repository.get_revision(history[-1])
174
            if len(revision.parent_ids) == 0:
175
                break
176
            history.append(revision.parent_ids[0])
177
        return list(reversed(history))
178
0.200.1 by Robert Collins
Commit initial content.
179
    def lock_read(self):
180
        self.control_files.lock_read()
181
182
    def unlock(self):
183
        self.control_files.unlock()
184
185
186
class GitRepository(bzrlib.repository.Repository):
187
    """An adapter to git repositories for bzr."""
188
189
    def __init__(self, gitdir, lockfiles):
190
        self.bzrdir = gitdir
191
        self.control_files = lockfiles
192
193
    def get_revision(self, revision_id):
194
        raw = stgit.git._output_lines('git-rev-list --header --max-count=1 %s' % gitrevid_from_bzr(revision_id))
195
        # first field is the rev itself.
196
        # then its 'field value'
197
        # until the EOF??
198
        parents = []
199
        log = []
200
        in_log = False
201
        committer = None
202
        for field in raw[1:]:
203
            #if field.startswith('author '):
204
            #    committer = field[7:]
205
            if field.startswith('parent '):
206
                parents.append(bzrrevid_from_git(field.split()[1]))
207
            elif field.startswith('committer '):
208
                commit_fields = field.split()
209
                if committer is None:
210
                    committer = ' '.join(commit_fields[1:-3])
211
                timestamp = commit_fields[-2]
212
                timezone = commit_fields[-1]
213
            elif in_log:
214
                log.append(field)
215
            elif field == '\n':
216
                in_log = True
217
218
        log = ''.join(log)
219
        result = Revision(revision_id)
220
        result.parent_ids = parents
221
        result.message = log
222
        result.inventory_sha1 = ""
223
        result.timezone = timezone and int(timezone)
224
        result.timestamp = float(timestamp)
225
        result.committer = committer 
226
        return result