133
121
revspec_registry.register_lazy("git:", "bzrlib.plugins.git.revspec",
134
122
"RevisionSpec_git")
137
from bzrlib.revisionspec import dwim_revspecs
124
from bzrlib.revisionspec import dwim_revspecs, RevisionSpec_dwim
125
if getattr(RevisionSpec_dwim, "append_possible_lazy_revspec", None):
126
RevisionSpec_dwim.append_possible_lazy_revspec(
127
"bzrlib.plugins.git.revspec", "RevisionSpec_git")
141
129
from bzrlib.plugins.git.revspec import RevisionSpec_git
142
130
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):
161
133
class LocalGitProber(Prober):
163
135
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:
137
external_url = transport.external_url()
138
except bzr_errors.InProcessTransport:
139
raise bzr_errors.NotBranchError(path=transport.base)
140
if (external_url.startswith("http:") or
141
external_url.startswith("https:")):
142
# Already handled by RemoteGitProber
169
143
raise bzr_errors.NotBranchError(path=transport.base)
170
144
from bzrlib import urlutils
171
145
if urlutils.split(transport.base)[1] == ".git":
172
146
raise bzr_errors.NotBranchError(path=transport.base)
147
if not transport.has_any(['objects', '.git/objects']):
148
raise bzr_errors.NotBranchError(path=transport.base)
173
149
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)
150
from bzrlib.plugins.git.dir import (
151
BareLocalGitControlDirFormat,
152
LocalGitControlDirFormat,
154
if transport.has_any(['.git/objects']):
155
return LocalGitControlDirFormat()
156
if transport.has('info') and transport.has('objects'):
157
return BareLocalGitControlDirFormat()
158
raise bzr_errors.NotBranchError(path=transport.base)
161
def known_formats(cls):
162
from bzrlib.plugins.git.dir import (
163
BareLocalGitControlDirFormat,
164
LocalGitControlDirFormat,
166
return set([BareLocalGitControlDirFormat(), LocalGitControlDirFormat()])
169
class RemoteGitProber(Prober):
171
def probe_http_transport(self, transport):
172
from bzrlib import urlutils
173
url = urlutils.join(transport.external_url(), "info/refs") + "?service=git-upload-pack"
174
from bzrlib.transport.http._urllib import HttpTransport_urllib, Request
175
if isinstance(transport, HttpTransport_urllib):
176
req = Request('GET', url, accepted_errors=[200, 403, 404, 405],
177
headers={"Content-Type": "application/x-git-upload-pack-request"})
178
req.follow_redirections = True
179
resp = transport._perform(req)
180
if resp.code in (404, 405):
181
raise bzr_errors.NotBranchError(transport.base)
182
headers = resp.headers
183
refs_text = resp.read()
182
return BareLocalGitControlDirFormat()
186
from bzrlib.transport.http._pycurl import PyCurlTransport
187
except bzr_errors.DependencyNotPresent:
188
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):
191
from cStringIO import StringIO
192
if isinstance(transport, PyCurlTransport):
193
conn = transport._get_curl()
194
conn.setopt(pycurl.URL, url)
195
conn.setopt(pycurl.FOLLOWLOCATION, 1)
196
transport._set_curl_options(conn)
197
conn.setopt(pycurl.HTTPGET, 1)
200
conn.setopt(pycurl.HEADERFUNCTION, header.write)
201
conn.setopt(pycurl.WRITEFUNCTION, data.write)
202
transport._curl_perform(conn, header,
203
["Content-Type: application/x-git-upload-pack-request"])
204
code = conn.getinfo(pycurl.HTTP_CODE)
205
if code in (404, 405):
206
raise bzr_errors.NotBranchError(transport.base)
208
raise bzr_errors.InvalidHttpResponse(transport._path,
210
headers = transport._parse_headers(header)
212
raise bzr_errors.NotBranchError(transport.base)
213
refs_text = data.getvalue()
214
ct = headers.getheader("Content-Type")
216
raise bzr_errors.NotBranchError(transport.base)
217
if ct.startswith("application/x-git"):
218
from bzrlib.plugins.git.remote import RemoteGitControlDirFormat
219
return RemoteGitControlDirFormat()
221
from bzrlib.plugins.git.dir import (
222
BareLocalGitControlDirFormat,
224
ret = BareLocalGitControlDirFormat()
225
ret._refs_text = refs_text
247
228
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+")):
230
external_url = transport.external_url()
231
except bzr_errors.InProcessTransport:
232
raise bzr_errors.NotBranchError(path=transport.base)
234
if (external_url.startswith("http:") or
235
external_url.startswith("https:")):
236
return self.probe_http_transport(transport)
238
if (not external_url.startswith("git://") and
239
not external_url.startswith("git+")):
252
240
raise bzr_errors.NotBranchError(transport.base)
253
242
# 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
243
from bzrlib.plugins.git.remote import (
245
RemoteGitControlDirFormat,
247
if isinstance(transport, GitSmartTransport):
248
return RemoteGitControlDirFormat()
249
raise bzr_errors.NotBranchError(path=transport.base)
267
def _known_formats(self):
252
def known_formats(cls):
253
from bzrlib.plugins.git.remote import RemoteGitControlDirFormat
268
254
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)
257
if not getattr(Prober, "known_formats", None): # bzr < 2.4
258
from bzrlib.plugins.git.dir import (
259
LocalGitControlDirFormat, BareLocalGitControlDirFormat,
261
from bzrlib.plugins.git.remote import RemoteGitControlDirFormat
301
262
ControlDirFormat.register_format(LocalGitControlDirFormat())
302
263
ControlDirFormat.register_format(BareLocalGitControlDirFormat())
303
264
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)
265
# Provide RevisionTree.get_file_revision, so various parts of bzr-svn
266
# can avoid inventories.
267
from bzrlib.revisiontree import RevisionTree
268
def get_file_revision(tree, file_id, path=None):
269
return tree.inventory[file_id].revision
270
RevisionTree.get_file_revision = get_file_revision
272
ControlDirFormat.register_prober(LocalGitProber)
273
ControlDirFormat._server_probers.insert(0, RemoteGitProber)
311
275
register_transport_proto('git://',
312
276
help="Access using the Git smart server protocol.")
345
337
'Git Smart server protocol over TCP. (default port: 9418)')
339
transport_server_registry.register_lazy('git-receive-pack',
340
'bzrlib.plugins.git.server',
341
'serve_git_receive_pack',
342
help='Git Smart server receive pack command (inetd mode only)')
343
transport_server_registry.register_lazy('git-upload-pack',
344
'bzrlib.plugins.git.server',
345
'serve_git_upload_pack',
346
help='Git Smart server upload pack command (inetd mode only)')
348
348
from bzrlib.repository import (
349
format_registry as repository_format_registry,
349
350
network_format_registry as repository_network_format_registry,
351
352
repository_network_format_registry.register_lazy('git',
352
353
'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)
356
register_extra_lazy_repository_format = getattr(repository_format_registry,
357
"register_extra_lazy")
358
except AttributeError: # bzr < 2.4
361
register_extra_lazy_repository_format('bzrlib.plugins.git.repository',
362
'GitRepositoryFormat')
364
from bzrlib.branch import (
365
network_format_registry as branch_network_format_registry,
367
branch_network_format_registry.register_lazy('git',
368
'bzrlib.plugins.git.branch', 'GitBranchFormat')
371
from bzrlib.branch import (
372
format_registry as branch_format_registry,
374
except ImportError: # bzr < 2.4
377
branch_format_registry.register_extra_lazy(
378
'bzrlib.plugins.git.branch',
383
from bzrlib.workingtree import (
384
format_registry as workingtree_format_registry,
386
except ImportError: # bzr < 2.4
389
workingtree_format_registry.register_extra_lazy(
390
'bzrlib.plugins.git.workingtree',
391
'GitWorkingTreeFormat',
394
controldir_network_format_registry.register_lazy('git',
395
"bzrlib.plugins.git.dir", "GitControlDirFormat")
364
397
send_format_registry.register_lazy('git', 'bzrlib.plugins.git.send',
365
398
'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')
400
topic_registry.register_lazy('git', 'bzrlib.plugins.git.help', 'help_git',
401
'Using Bazaar with Git')
403
from bzrlib.diff import format_registry as diff_format_registry
404
diff_format_registry.register_lazy('git', 'bzrlib.plugins.git.send',
405
'GitDiffTree', 'Git am-style diff format')
408
def update_git_cache(repository, revid):
409
"""Update the git cache after a local commit."""
410
if getattr(repository, "_git", None) is not None:
411
return # No need to update cache for git repositories
413
if not repository.control_transport.has("git"):
414
return # No existing cache, don't bother updating
416
lazy_check_versions()
417
except bzr_errors.DependencyNotPresent, e:
418
# dulwich is probably missing. silently ignore
419
trace.mutter("not updating git map for %r: %s",
422
from bzrlib.plugins.git.object_store import BazaarObjectStore
423
store = BazaarObjectStore(repository)
426
parent_revisions = set(repository.get_parent_map([revid])[revid])
427
missing_revisions = store._missing_revisions(parent_revisions)
428
if not missing_revisions:
429
# Only update if the cache was up to date previously
430
store._update_sha_map_revision(revid)
435
def post_commit_update_cache(local_branch, master_branch, old_revno, old_revid,
436
new_revno, new_revid):
437
if local_branch is not None:
438
update_git_cache(local_branch.repository, new_revid)
439
update_git_cache(master_branch.repository, new_revid)
442
def loggerhead_git_hook(branch_app, environ):
443
from bzrlib.config import GlobalConfig
444
branch = branch_app.branch
445
if GlobalConfig().get_user_option('http_git') != 'True':
447
from bzrlib.plugins.git.server import git_http_hook
448
return git_http_hook(branch, environ['REQUEST_METHOD'],
449
environ['PATH_INFO'])
372
from bzrlib.diff import format_registry as diff_format_registry
452
from bzrlib.hooks import install_lazy_named_hook
453
except ImportError: # Compatibility with bzr < 2.4
376
diff_format_registry.register_lazy('git', 'bzrlib.plugins.git.send',
377
'GitDiffTree', 'Git am-style diff format')
456
install_lazy_named_hook("bzrlib.branch",
457
"Branch.hooks", "post_commit", post_commit_update_cache,
459
install_lazy_named_hook("bzrlib.plugins.loggerhead.apps.branch",
460
"BranchWSGIApp.hooks", "controller",
461
loggerhead_git_hook, "git support")
379
464
def test_suite():
380
465
from bzrlib.plugins.git import tests