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)
181
raise bzr_errors.NotBranchError(transport.base)
182
headers = resp.headers
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
transport._set_curl_options(conn)
195
conn.setopt(pycurl.HTTPGET, 1)
198
conn.setopt(pycurl.HEADERFUNCTION, header.write)
199
conn.setopt(pycurl.WRITEFUNCTION, data.write)
200
transport._curl_perform(conn, header,
201
["Content-Type: application/x-git-upload-pack-request"])
202
code = conn.getinfo(pycurl.HTTP_CODE)
204
raise bzr_errors.NotBranchError(transport.base)
206
raise bzr_errors.InvalidHttpResponse(transport._path,
208
headers = transport._parse_headers(header)
210
raise bzr_errors.NotBranchError(transport.base)
211
ct = headers.getheader("Content-Type")
213
raise bzr_errors.NotBranchError(transport.base)
214
if ct.startswith("application/x-git"):
215
from bzrlib.plugins.git.remote import RemoteGitControlDirFormat
216
return RemoteGitControlDirFormat()
218
from bzrlib.plugins.git.dir import (
219
BareLocalGitControlDirFormat,
221
return BareLocalGitControlDirFormat()
247
223
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+")):
225
external_url = transport.external_url()
226
except bzr_errors.InProcessTransport:
227
raise bzr_errors.NotBranchError(path=transport.base)
229
if (external_url.startswith("http:") or
230
external_url.startswith("https:")):
231
return self.probe_http_transport(transport)
233
if (not external_url.startswith("git://") and
234
not external_url.startswith("git+")):
252
235
raise bzr_errors.NotBranchError(transport.base)
253
237
# 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
238
from bzrlib.plugins.git.remote import (
240
RemoteGitControlDirFormat,
242
if isinstance(transport, GitSmartTransport):
243
return RemoteGitControlDirFormat()
244
raise bzr_errors.NotBranchError(path=transport.base)
267
def _known_formats(self):
247
def known_formats(cls):
248
from bzrlib.plugins.git.remote import RemoteGitControlDirFormat
268
249
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)
252
if not getattr(Prober, "known_formats", None): # bzr < 2.4
253
from bzrlib.plugins.git.dir import (
254
LocalGitControlDirFormat, BareLocalGitControlDirFormat,
256
from bzrlib.plugins.git.remote import RemoteGitControlDirFormat
301
257
ControlDirFormat.register_format(LocalGitControlDirFormat())
302
258
ControlDirFormat.register_format(BareLocalGitControlDirFormat())
303
259
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)
260
# Provide RevisionTree.get_file_revision, so various parts of bzr-svn
261
# can avoid inventories.
262
from bzrlib.revisiontree import RevisionTree
263
def get_file_revision(tree, file_id, path=None):
264
return tree.inventory[file_id].revision
265
RevisionTree.get_file_revision = get_file_revision
267
ControlDirFormat.register_prober(LocalGitProber)
268
ControlDirFormat._server_probers.insert(0, RemoteGitProber)
311
270
register_transport_proto('git://',
312
271
help="Access using the Git smart server protocol.")
348
335
from bzrlib.repository import (
336
format_registry as repository_format_registry,
349
337
network_format_registry as repository_network_format_registry,
351
339
repository_network_format_registry.register_lazy('git',
352
340
'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)
343
register_extra_lazy_repository_format = getattr(repository_format_registry,
344
"register_extra_lazy")
345
except AttributeError: # bzr < 2.4
348
register_extra_lazy_repository_format('bzrlib.plugins.git.repository',
349
'GitRepositoryFormat')
351
from bzrlib.branch import (
352
network_format_registry as branch_network_format_registry,
354
branch_network_format_registry.register_lazy('git',
355
'bzrlib.plugins.git.branch', 'GitBranchFormat')
358
from bzrlib.branch import (
359
format_registry as branch_format_registry,
361
except ImportError: # bzr < 2.4
364
branch_format_registry.register_extra_lazy(
365
'bzrlib.plugins.git.branch',
370
from bzrlib.workingtree import (
371
format_registry as workingtree_format_registry,
373
except ImportError: # bzr < 2.4
376
workingtree_format_registry.register_extra_lazy(
377
'bzrlib.plugins.git.workingtree',
378
'GitWorkingTreeFormat',
381
controldir_network_format_registry.register_lazy('git',
382
"bzrlib.plugins.git.dir", "GitControlDirFormat")
364
384
send_format_registry.register_lazy('git', 'bzrlib.plugins.git.send',
365
385
'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')
387
topic_registry.register_lazy('git', 'bzrlib.plugins.git.help', 'help_git',
388
'Using Bazaar with Git')
390
from bzrlib.diff import format_registry as diff_format_registry
391
diff_format_registry.register_lazy('git', 'bzrlib.plugins.git.send',
392
'GitDiffTree', 'Git am-style diff format')
395
def update_git_cache(repository, revid):
396
"""Update the git cache after a local commit."""
397
if getattr(repository, "_git", None) is not None:
398
return # No need to update cache for git repositories
400
if not repository.control_transport.has("git"):
401
return # No existing cache, don't bother updating
403
lazy_check_versions()
404
except bzr_errors.DependencyNotPresent, e:
405
# dulwich is probably missing. silently ignore
406
trace.mutter("not updating git map for %r: %s",
409
from bzrlib.plugins.git.object_store import BazaarObjectStore
410
store = BazaarObjectStore(repository)
413
parent_revisions = set(repository.get_parent_map([revid])[revid])
414
missing_revisions = store._missing_revisions(parent_revisions)
415
if not missing_revisions:
416
# Only update if the cache was up to date previously
417
store._update_sha_map_revision(revid)
422
def post_commit_update_cache(local_branch, master_branch, old_revno, old_revid,
423
new_revno, new_revid):
424
if local_branch is not None:
425
update_git_cache(local_branch.repository, new_revid)
426
update_git_cache(master_branch.repository, new_revid)
372
from bzrlib.diff import format_registry as diff_format_registry
430
from bzrlib.hooks import install_lazy_named_hook
431
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')
434
install_lazy_named_hook("bzrlib.branch",
435
"Branch.hooks", "post_commit", post_commit_update_cache,
379
439
def test_suite():
380
440
from bzrlib.plugins.git import tests