43
89
def get_tag_dict(self):
45
for k,v in self.repository._git.tags.iteritems():
46
obj = self.repository._git.get_object(v)
91
for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
93
obj = self.repository._git[v]
95
mutter("Tag %s points at unknown object %s, ignoring", v, obj)
47
97
while isinstance(obj, Tag):
49
obj = self.repository._git.get_object(v)
99
obj = self.repository._git[v]
50
100
if not isinstance(obj, Commit):
51
mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
101
mutter("Tag %s points at object %r that is not a commit, "
53
104
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
107
def _set_tag_dict(self, to_dict):
108
extra = set(self.repository._git.get_refs().keys())
109
for k, revid in to_dict.iteritems():
110
name = "refs/tags/%s" % k
113
self.set_tag(k, revid)
115
if name.startswith("refs/tags/"):
116
del self.repository._git[name]
56
118
def set_tag(self, name, revid):
57
self.repository._git.tags[name] = revid
60
class GitBranchConfig(config.BranchConfig):
61
"""BranchConfig that uses locations.conf in place of branch.conf"""
63
def __init__(self, branch):
64
config.BranchConfig.__init__(self, branch)
65
# do not provide a BranchDataConfig
66
self.option_sources = self.option_sources[0], self.option_sources[2]
68
def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
69
"""Force local to True"""
70
config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
119
self.repository._git.refs["refs/tags/%s" % name], _ = \
120
self.branch.mapping.revision_id_bzr_to_foreign(revid)
123
class DictTagDict(LocalGitTagDict):
126
def __init__(self, branch, tags):
127
super(DictTagDict, self).__init__(branch)
130
def get_tag_dict(self):
73
135
class GitBranchFormat(branch.BranchFormat):
75
137
def get_format_description(self):
76
138
return 'Git Branch'
140
def network_name(self):
78
143
def supports_tags(self):
146
def get_foreign_tests_branch_factory(self):
147
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
148
return ForeignTestsBranchFactory()
150
def make_tags(self, branch):
151
if getattr(branch.repository, "get_refs", None) is not None:
152
from bzrlib.plugins.git.remote import RemoteGitTagDict
153
return RemoteGitTagDict(branch)
155
return LocalGitTagDict(branch)
82
158
class GitBranch(ForeignBranch):
83
159
"""An adapter to git repositories for bzr Branch objects."""
85
def __init__(self, bzrdir, repository, name, head, lockfiles):
161
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
86
162
self.repository = repository
87
super(GitBranch, self).__init__(repository.get_mapping())
163
self._format = GitBranchFormat()
88
164
self.control_files = lockfiles
89
165
self.bzrdir = bzrdir
166
super(GitBranch, self).__init__(repository.get_mapping())
167
if tagsdict is not None:
168
self.tags = DictTagDict(self, tagsdict)
92
self.base = bzrdir.transport.base
93
self._format = GitBranchFormat()
95
def dpull(self, source, stop_revision=None):
96
if stop_revision is None:
97
stop_revision = source.last_revision()
98
# FIXME: Check for diverged branches
99
revidmap = self.repository.dfetch(source.repository, stop_revision)
100
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
171
self.base = bzrdir.root_transport.base
173
def _get_checkout_format(self):
174
"""Return the most suitable metadir for a checkout of this branch.
175
Weaves are used if this branch's repository uses weaves.
177
return get_rich_root_format()
179
def get_child_submit_format(self):
180
"""Return the preferred format of submissions to this branch."""
181
ret = self.get_config().get_user_option("child_submit_format")
186
def _get_nick(self, local=False, possible_master_transports=None):
187
"""Find the nick name for this branch.
193
def _set_nick(self, nick):
194
raise NotImplementedError
196
nick = property(_get_nick, _set_nick)
199
return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
201
def generate_revision_history(self, revid, old_revid=None):
202
# FIXME: Check that old_revid is in the ancestry of revid
203
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
204
self._set_head(newhead)
103
206
def lock_write(self):
104
207
self.control_files.lock_write()
106
209
def get_stacked_on_url(self):
107
210
# Git doesn't do stacking (yet...)
211
raise errors.UnstackableBranchFormat(self._format, self.base)
110
213
def get_parent(self):
111
214
"""See Branch.get_parent()."""
215
# FIXME: Set "origin" url from .git/config ?
114
218
def set_parent(self, url):
219
# FIXME: Set "origin" url in .git/config ?
117
222
def lock_read(self):
118
223
self.control_files.lock_read()
226
return self.control_files.is_locked()
120
228
def unlock(self):
121
229
self.control_files.unlock()
123
231
def get_physical_lock_status(self):
127
class LocalGitBranch(GitBranch):
130
235
def last_revision(self):
131
236
# perhaps should escape this ?
183
325
def supports_tags(self):
186
def sprout(self, to_bzrdir, revision_id=None):
187
"""See Branch.sprout()."""
188
result = to_bzrdir.create_branch()
189
self.copy_content_into(result, revision_id=revision_id)
190
result.set_parent(self.bzrdir.root_transport.base)
194
class InterGitGenericBranch(branch.InterBranch):
197
def is_compatible(self, source, target):
198
return isinstance(source, GitBranch)
329
class GitBranchPullResult(branch.PullResult):
331
def report(self, to_file):
333
if self.old_revid == self.new_revid:
334
to_file.write('No revisions to pull.\n')
336
to_file.write('Now on revision %d (git sha: %s).\n' %
337
(self.new_revno, self.new_git_head))
338
self._show_tag_conficts(to_file)
341
class GitBranchPushResult(branch.BranchPushResult):
343
def _lookup_revno(self, revid):
344
assert isinstance(revid, str), "was %r" % revid
345
# Try in source branch first, it'll be faster
347
return self.source_branch.revision_id_to_revno(revid)
348
except errors.NoSuchRevision:
349
# FIXME: Check using graph.find_distance_to_null() ?
350
return self.target_branch.revision_id_to_revno(revid)
354
return self._lookup_revno(self.old_revid)
358
return self._lookup_revno(self.new_revid)
361
class InterFromGitBranch(branch.GenericInterBranch):
362
"""InterBranch implementation that pulls from Git into bzr."""
365
def _get_interrepo(self, source, target):
366
return repository.InterRepository.get(source.repository,
370
def is_compatible(cls, source, target):
371
return (isinstance(source, GitBranch) and
372
not isinstance(target, GitBranch) and
373
(getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
200
375
def update_revisions(self, stop_revision=None, overwrite=False,
376
graph=None, limit=None):
202
377
"""See InterBranch.update_revisions()."""
203
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
205
self._last_revid = None
378
interrepo = self._get_interrepo(self.source, self.target)
206
379
def determine_wants(heads):
207
380
if not self.source.name in heads:
208
raise BzrError("No such remote branch '%s', found: %r" % (
209
self.source.name, heads.keys()))
210
head = heads[self.source.name]
211
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
212
if self.target.repository.has_revision(self._last_revid):
381
raise NoSuchRef(self.source.name, heads.keys())
382
if stop_revision is not None:
383
_last_revid = stop_revision
384
_head, mapping = self.source.repository.lookup_bzr_revision_id(
387
_head = heads[self.source.name]
388
_last_revid = self.source.mapping.revision_id_foreign_to_bzr(
390
if self.target.repository.has_revision(_last_revid):
215
interrepo.fetch_objects(determine_wants, self.source.mapping)
216
self.target.generate_revision_history(self._last_revid)
219
branch.InterBranch.register_optimiser(InterGitGenericBranch)
393
_, _head = interrepo.fetch_objects(
394
determine_wants, self.source.mapping, limit=limit)
396
_last_revid = self.target.last_revision()
398
_last_revid = self.source.mapping.revision_id_foreign_to_bzr(_head)
400
prev_last_revid = None
402
prev_last_revid = self.target.last_revision()
403
self.target.generate_revision_history(_last_revid, prev_last_revid)
406
def pull(self, overwrite=False, stop_revision=None,
407
possible_transports=None, _hook_master=None, run_hooks=True,
408
_override_hook_target=None, local=False, limit=None):
411
:param _hook_master: Private parameter - set the branch to
412
be supplied as the master to pull hooks.
413
:param run_hooks: Private parameter - if false, this branch
414
is being called because it's the master of the primary branch,
415
so it should not run its hooks.
416
:param _override_hook_target: Private parameter - set the branch to be
417
supplied as the target_branch to pull hooks.
418
:param limit: Only import this many revisons. `None`, the default,
419
means import all revisions.
421
# This type of branch can't be bound.
423
raise errors.LocalRequiresBoundBranch()
424
result = GitBranchPullResult()
425
result.source_branch = self.source
426
if _override_hook_target is None:
427
result.target_branch = self.target
429
result.target_branch = _override_hook_target
430
self.source.lock_read()
432
# We assume that during 'pull' the target repository is closer than
434
graph = self.target.repository.get_graph(self.source.repository)
435
result.old_revno, result.old_revid = \
436
self.target.last_revision_info()
437
_head = self.update_revisions(stop_revision, overwrite=overwrite,
438
graph=graph, limit=limit)
439
result.new_git_head = _head
440
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
442
result.new_revno, result.new_revid = self.target.last_revision_info()
444
result.master_branch = _hook_master
445
result.local_branch = result.target_branch
447
result.master_branch = result.target_branch
448
result.local_branch = None
450
for hook in branch.Branch.hooks['post_pull']:
456
def _basic_push(self, overwrite=False, stop_revision=None):
457
result = branch.BranchPushResult()
458
result.source_branch = self.source
459
result.target_branch = self.target
460
graph = self.target.repository.get_graph(self.source.repository)
461
result.old_revno, result.old_revid = self.target.last_revision_info()
462
_head = self.update_revisions(
463
stop_revision, overwrite=overwrite, graph=graph)
464
result.new_git_head = _head
465
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
467
result.new_revno, result.new_revid = self.target.last_revision_info()
471
class InterGitBranch(branch.GenericInterBranch):
472
"""InterBranch implementation that pulls between Git branches."""
475
class InterGitLocalRemoteBranch(InterGitBranch):
476
"""InterBranch that copies from a local to a remote git branch."""
479
def is_compatible(self, source, target):
480
from bzrlib.plugins.git.remote import RemoteGitBranch
481
return (isinstance(source, LocalGitBranch) and
482
isinstance(target, RemoteGitBranch))
484
def _basic_push(self, overwrite=False, stop_revision=None):
485
result = GitBranchPushResult()
486
result.source_branch = self.source
487
result.target_branch = self.target
488
if stop_revision is None:
489
stop_revision = self.source.last_revision()
490
# FIXME: Check for diverged branches
491
def get_changed_refs(old_refs):
492
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
493
refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
494
result.new_revid = stop_revision
495
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
496
refs["refs/tags/%s" % name] = sha
498
self.target.repository.send_pack(get_changed_refs,
499
self.source.repository._git.object_store.generate_pack_contents)
503
class InterGitRemoteLocalBranch(InterGitBranch):
504
"""InterBranch that copies from a remote to a local git branch."""
507
def is_compatible(self, source, target):
508
from bzrlib.plugins.git.remote import RemoteGitBranch
509
return (isinstance(source, RemoteGitBranch) and
510
isinstance(target, LocalGitBranch))
512
def _basic_push(self, overwrite=False, stop_revision=None):
513
result = branch.BranchPushResult()
514
result.source_branch = self.source
515
result.target_branch = self.target
516
result.old_revid = self.target.last_revision()
517
refs, stop_revision = self.update_refs(stop_revision)
518
self.target.generate_revision_history(stop_revision, result.old_revid)
519
self.update_tags(refs)
520
result.new_revid = self.target.last_revision()
523
def update_tags(self, refs):
524
for name, v in extract_tags(refs).iteritems():
525
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
526
self.target.tags.set_tag(name, revid)
528
def update_refs(self, stop_revision=None):
529
interrepo = repository.InterRepository.get(self.source.repository,
530
self.target.repository)
531
if stop_revision is None:
532
refs = interrepo.fetch_refs(branches=["HEAD"])
533
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
535
refs = interrepo.fetch_refs(revision_id=stop_revision)
536
return refs, stop_revision
538
def pull(self, stop_revision=None, overwrite=False,
539
possible_transports=None, local=False):
540
# This type of branch can't be bound.
542
raise errors.LocalRequiresBoundBranch()
543
result = GitPullResult()
544
result.source_branch = self.source
545
result.target_branch = self.target
546
result.old_revid = self.target.last_revision()
547
refs, stop_revision = self.update_refs(stop_revision)
548
self.target.generate_revision_history(stop_revision, result.old_revid)
549
self.update_tags(refs)
550
result.new_revid = self.target.last_revision()
554
class InterToGitBranch(branch.InterBranch):
555
"""InterBranch implementation that pulls from Git into bzr."""
558
def _get_branch_formats_to_test():
562
def is_compatible(self, source, target):
563
return (not isinstance(source, GitBranch) and
564
isinstance(target, GitBranch))
566
def update_revisions(self, *args, **kwargs):
567
raise NoPushSupport()
569
def push(self, overwrite=True, stop_revision=None,
570
_override_hook_source_branch=None):
571
raise NoPushSupport()
573
def lossy_push(self, stop_revision=None):
574
result = GitBranchPushResult()
575
result.source_branch = self.source
576
result.target_branch = self.target
578
result.old_revid = self.target.last_revision()
580
result.old_revid = revision.NULL_REVISION
581
if stop_revision is None:
582
stop_revision = self.source.last_revision()
583
# FIXME: Check for diverged branches
584
refs = { "refs/heads/master": stop_revision }
585
for name, revid in self.source.tags.get_tag_dict().iteritems():
586
if self.source.repository.has_revision(revid):
587
refs["refs/tags/%s" % name] = revid
588
revidmap, new_refs = self.target.repository.dfetch_refs(
589
self.source.repository, refs)
591
self.target.generate_revision_history(revidmap[stop_revision])
592
result.new_revid = revidmap[stop_revision]
594
result.new_revid = result.old_revid
595
result.revidmap = revidmap
599
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
600
branch.InterBranch.register_optimiser(InterFromGitBranch)
601
branch.InterBranch.register_optimiser(InterToGitBranch)
602
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)