133
123
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec",
134
124
"RevisionSpec_git")
137
from bzrlib.revisionspec import dwim_revspecs
141
from bzrlib.plugins.git.revspec import RevisionSpec_git
142
dwim_revspecs.append(RevisionSpec_git)
145
class GitControlDirFormat(ControlDirFormat):
147
_lock_class = TransportLock
149
colocated_branches = True
151
def __eq__(self, other):
152
return type(self) == type(other)
154
def is_supported(self):
157
def network_name(self):
126
from bzrlib.revisionspec import RevisionSpec_dwim
127
RevisionSpec_dwim.append_possible_lazy_revspec(
128
"bzrlib.plugins.git.revspec", "RevisionSpec_git")
161
131
class LocalGitProber(Prober):
163
133
def probe_transport(self, transport):
165
if not transport.has_any(['info/refs', '.git/branches',
167
raise bzr_errors.NotBranchError(path=transport.base)
168
except bzr_errors.NoSuchFile:
135
external_url = transport.external_url()
136
except bzr_errors.InProcessTransport:
137
raise bzr_errors.NotBranchError(path=transport.base)
138
if (external_url.startswith("http:") or
139
external_url.startswith("https:")):
140
# Already handled by RemoteGitProber
169
141
raise bzr_errors.NotBranchError(path=transport.base)
170
142
from bzrlib import urlutils
171
143
if urlutils.split(transport.base)[1] == ".git":
172
144
raise bzr_errors.NotBranchError(path=transport.base)
145
if not transport.has_any(['objects', '.git/objects']):
146
raise bzr_errors.NotBranchError(path=transport.base)
173
147
lazy_check_versions()
175
from bzrlib.plugins.git.transportgit import TransportRepo
177
gitrepo = TransportRepo(transport)
178
except dulwich.errors.NotGitRepository, e:
179
raise bzr_errors.NotBranchError(path=transport.base)
148
from bzrlib.plugins.git.dir import (
149
BareLocalGitControlDirFormat,
150
LocalGitControlDirFormat,
152
if transport.has_any(['.git/objects']):
153
return LocalGitControlDirFormat()
154
if transport.has('info') and transport.has('objects'):
155
return BareLocalGitControlDirFormat()
156
raise bzr_errors.NotBranchError(path=transport.base)
159
def known_formats(cls):
160
from bzrlib.plugins.git.dir import (
161
BareLocalGitControlDirFormat,
162
LocalGitControlDirFormat,
164
return set([BareLocalGitControlDirFormat(), LocalGitControlDirFormat()])
167
class RemoteGitProber(Prober):
169
def probe_http_transport(self, transport):
170
from bzrlib import urlutils
171
base_url, _ = urlutils.split_segment_parameters(transport.external_url())
172
url = urlutils.join(base_url, "info/refs") + "?service=git-upload-pack"
173
from bzrlib.transport.http._urllib import HttpTransport_urllib, Request
174
if isinstance(transport, HttpTransport_urllib):
175
req = Request('GET', url, accepted_errors=[200, 403, 404, 405],
176
headers={"Content-Type": "application/x-git-upload-pack-request"})
177
req.follow_redirections = True
178
resp = transport._perform(req)
179
if resp.code in (404, 405):
180
raise bzr_errors.NotBranchError(transport.base)
181
headers = resp.headers
182
refs_text = resp.read()
182
return BareLocalGitControlDirFormat()
185
from bzrlib.transport.http._pycurl import PyCurlTransport
186
except bzr_errors.DependencyNotPresent:
187
raise bzr_errors.NotBranchError(transport.base)
184
return LocalGitControlDirFormat()
187
class LocalGitControlDirFormat(GitControlDirFormat):
188
"""The .git directory control format."""
193
def _known_formats(self):
194
return set([LocalGitControlDirFormat()])
196
def open(self, transport, _found=None):
197
"""Open this directory.
200
lazy_check_versions()
201
from bzrlib.plugins.git.transportgit import TransportRepo
202
gitrepo = TransportRepo(transport)
203
from bzrlib.plugins.git.dir import LocalGitDir, GitLockableFiles, GitLock
204
lockfiles = GitLockableFiles(transport, GitLock())
205
return LocalGitDir(transport, lockfiles, gitrepo, self)
208
def probe_transport(klass, transport):
209
prober = LocalGitProber()
210
return prober.probe_transport(transport)
212
def get_format_description(self):
213
return "Local Git Repository"
215
def initialize_on_transport(self, transport):
216
from bzrlib.transport.local import LocalTransport
218
if not isinstance(transport, LocalTransport):
219
raise NotImplementedError(self.initialize,
220
"Can't create Git Repositories/branches on "
221
"non-local transports")
222
lazy_check_versions()
223
from dulwich.repo import Repo
224
Repo.init(transport.local_abspath(".").encode(osutils._fs_enc),
226
return self.open(transport)
228
def is_supported(self):
232
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
235
supports_workingtrees = False
238
def _known_formats(self):
239
return set([RemoteGitControlDirFormat()])
241
def get_format_description(self):
242
return "Local Git Repository (bare)"
245
class RemoteGitProber(Prober):
190
from cStringIO import StringIO
191
if isinstance(transport, PyCurlTransport):
192
conn = transport._get_curl()
193
conn.setopt(pycurl.URL, url)
194
conn.setopt(pycurl.FOLLOWLOCATION, 1)
195
transport._set_curl_options(conn)
196
conn.setopt(pycurl.HTTPGET, 1)
199
conn.setopt(pycurl.HEADERFUNCTION, header.write)
200
conn.setopt(pycurl.WRITEFUNCTION, data.write)
201
transport._curl_perform(conn, header,
202
["Content-Type: application/x-git-upload-pack-request"])
203
code = conn.getinfo(pycurl.HTTP_CODE)
204
if code in (404, 405):
205
raise bzr_errors.NotBranchError(transport.base)
207
raise bzr_errors.InvalidHttpResponse(transport._path,
209
headers = transport._parse_headers(header)
211
raise bzr_errors.NotBranchError(transport.base)
212
refs_text = data.getvalue()
213
ct = headers.getheader("Content-Type")
215
raise bzr_errors.NotBranchError(transport.base)
216
if ct.startswith("application/x-git"):
217
from bzrlib.plugins.git.remote import RemoteGitControlDirFormat
218
return RemoteGitControlDirFormat()
220
from bzrlib.plugins.git.dir import (
221
BareLocalGitControlDirFormat,
223
ret = BareLocalGitControlDirFormat()
224
ret._refs_text = refs_text
247
227
def probe_transport(self, transport):
249
if url.startswith('readonly+'):
250
url = url[len('readonly+'):]
251
if (not url.startswith("git://") and not url.startswith("git+")):
229
external_url = transport.external_url()
230
except bzr_errors.InProcessTransport:
231
raise bzr_errors.NotBranchError(path=transport.base)
233
if (external_url.startswith("http:") or
234
external_url.startswith("https:")):
235
return self.probe_http_transport(transport)
237
if (not external_url.startswith("git://") and
238
not external_url.startswith("git+")):
252
239
raise bzr_errors.NotBranchError(transport.base)
253
241
# little ugly, but works
254
from bzrlib.plugins.git.remote import GitSmartTransport
255
if not isinstance(transport, GitSmartTransport):
256
raise bzr_errors.NotBranchError(transport.base)
257
return RemoteGitControlDirFormat()
261
class RemoteGitControlDirFormat(GitControlDirFormat):
262
"""The .git directory control format."""
264
supports_workingtrees = False
242
from bzrlib.plugins.git.remote import (
244
RemoteGitControlDirFormat,
246
if isinstance(transport, GitSmartTransport):
247
return RemoteGitControlDirFormat()
248
raise bzr_errors.NotBranchError(path=transport.base)
267
def _known_formats(self):
251
def known_formats(cls):
252
from bzrlib.plugins.git.remote import RemoteGitControlDirFormat
268
253
return set([RemoteGitControlDirFormat()])
270
def open(self, transport, _found=None):
271
"""Open this directory.
274
# we dont grok readonly - git isn't integrated with transport.
276
if url.startswith('readonly+'):
277
url = url[len('readonly+'):]
278
if (not url.startswith("git://") and not url.startswith("git+")):
279
raise bzr_errors.NotBranchError(transport.base)
280
from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
281
if not isinstance(transport, GitSmartTransport):
282
raise bzr_errors.NotBranchError(transport.base)
283
from bzrlib.plugins.git.dir import GitLockableFiles, GitLock
284
lockfiles = GitLockableFiles(transport, GitLock())
285
return RemoteGitDir(transport, lockfiles, self)
288
def probe_transport(klass, transport):
289
"""Our format is present if the transport ends in '.not/'."""
290
prober = RemoteGitProber()
291
return prober.probe_transport(transport)
293
def get_format_description(self):
294
return "Remote Git Repository"
296
def initialize_on_transport(self, transport):
297
raise bzr_errors.UninitializableFormat(self)
301
ControlDirFormat.register_format(LocalGitControlDirFormat())
302
ControlDirFormat.register_format(BareLocalGitControlDirFormat())
303
ControlDirFormat.register_format(RemoteGitControlDirFormat())
304
ControlDirFormat.register_prober(LocalGitProber)
305
ControlDirFormat.register_prober(RemoteGitProber)
307
ControlDirFormat.register_control_format(LocalGitControlDirFormat)
308
ControlDirFormat.register_control_format(BareLocalGitControlDirFormat)
309
ControlDirFormat.register_control_format(RemoteGitControlDirFormat)
256
ControlDirFormat.register_prober(LocalGitProber)
257
ControlDirFormat._server_probers.append(RemoteGitProber)
311
259
register_transport_proto('git://',
312
260
help="Access using the Git smart server protocol.")
345
313
'Git Smart server protocol over TCP. (default port: 9418)')
315
transport_server_registry.register_lazy('git-receive-pack',
316
'bzrlib.plugins.git.server',
317
'serve_git_receive_pack',
318
help='Git Smart server receive pack command (inetd mode only)')
319
transport_server_registry.register_lazy('git-upload-pack',
320
'bzrlib.plugins.git.server',
321
'serve_git_upload_pack',
322
help='Git Smart server upload pack command (inetd mode only)')
348
324
from bzrlib.repository import (
325
format_registry as repository_format_registry,
349
326
network_format_registry as repository_network_format_registry,
351
328
repository_network_format_registry.register_lazy('git',
352
329
'bzrlib.plugins.git.repository', 'GitRepositoryFormat')
355
from bzrlib.controldir import (
356
network_format_registry as controldir_network_format_registry,
359
from bzrlib.bzrdir import (
360
network_format_registry as controldir_network_format_registry,
362
controldir_network_format_registry.register('git', GitControlDirFormat)
331
register_extra_lazy_repository_format = getattr(repository_format_registry,
332
"register_extra_lazy")
333
register_extra_lazy_repository_format('bzrlib.plugins.git.repository',
334
'GitRepositoryFormat')
336
from bzrlib.branch import (
337
network_format_registry as branch_network_format_registry,
339
branch_network_format_registry.register_lazy('git',
340
'bzrlib.plugins.git.branch', 'GitBranchFormat')
342
from bzrlib.branch import (
343
format_registry as branch_format_registry,
345
branch_format_registry.register_extra_lazy(
346
'bzrlib.plugins.git.branch',
350
from bzrlib.workingtree import (
351
format_registry as workingtree_format_registry,
353
workingtree_format_registry.register_extra_lazy(
354
'bzrlib.plugins.git.workingtree',
355
'GitWorkingTreeFormat',
358
controldir_network_format_registry.register_lazy('git',
359
"bzrlib.plugins.git.dir", "GitControlDirFormat")
364
361
send_format_registry.register_lazy('git', 'bzrlib.plugins.git.send',
365
362
'send_git', 'Git am-style diff format')
367
topic_registry.register_lazy('git',
368
'bzrlib.plugins.git.help',
369
'help_git', 'Using Bazaar with Git')
372
from bzrlib.diff import format_registry as diff_format_registry
376
diff_format_registry.register_lazy('git', 'bzrlib.plugins.git.send',
377
'GitDiffTree', 'Git am-style diff format')
364
topic_registry.register_lazy('git', 'bzrlib.plugins.git.help', 'help_git',
365
'Using Bazaar with Git')
367
from bzrlib.diff import format_registry as diff_format_registry
368
diff_format_registry.register_lazy('git', 'bzrlib.plugins.git.send',
369
'GitDiffTree', 'Git am-style diff format')
372
def update_git_cache(repository, revid):
373
"""Update the git cache after a local commit."""
374
if getattr(repository, "_git", None) is not None:
375
return # No need to update cache for git repositories
377
if not repository.control_transport.has("git"):
378
return # No existing cache, don't bother updating
380
lazy_check_versions()
381
except bzr_errors.DependencyNotPresent, e:
382
# dulwich is probably missing. silently ignore
383
trace.mutter("not updating git map for %r: %s",
386
from bzrlib.plugins.git.object_store import BazaarObjectStore
387
store = BazaarObjectStore(repository)
390
parent_revisions = set(repository.get_parent_map([revid])[revid])
391
missing_revisions = store._missing_revisions(parent_revisions)
392
if not missing_revisions:
393
# Only update if the cache was up to date previously
394
store._update_sha_map_revision(revid)
399
def post_commit_update_cache(local_branch, master_branch, old_revno, old_revid,
400
new_revno, new_revid):
401
if local_branch is not None:
402
update_git_cache(local_branch.repository, new_revid)
403
update_git_cache(master_branch.repository, new_revid)
406
def loggerhead_git_hook(branch_app, environ):
407
from bzrlib.config import GlobalConfig
408
branch = branch_app.branch
409
if GlobalConfig().get_user_option('http_git') != 'True':
411
from bzrlib.plugins.git.server import git_http_hook
412
return git_http_hook(branch, environ['REQUEST_METHOD'],
413
environ['PATH_INFO'])
415
install_lazy_named_hook("bzrlib.branch",
416
"Branch.hooks", "post_commit", post_commit_update_cache,
418
install_lazy_named_hook("bzrlib.plugins.loggerhead.apps.branch",
419
"BranchWSGIApp.hooks", "controller",
420
loggerhead_git_hook, "git support")
422
from bzrlib.directory_service import directories
424
directories.register_lazy('github:', 'bzrlib.plugins.git.directory',
427
directories.register_lazy('git@github.com:', 'bzrlib.plugins.git.directory',
379
431
def test_suite():
380
432
from bzrlib.plugins.git import tests