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