/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to branch.py

  • Committer: Jelmer Vernooij
  • Date: 2009-05-16 21:13:17 UTC
  • mto: (0.200.527 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090516211317-a0s14tsrf2qusapf
Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
    bzrdir,
28
28
    config,
29
29
    errors,
 
30
    foreign,
30
31
    repository,
31
32
    revision,
32
33
    tag,
51
52
    NoSuchRef,
52
53
    )
53
54
 
54
 
from bzrlib.foreign import ForeignBranch
55
 
 
56
 
 
57
 
def extract_tags(refs):
 
55
try:
 
56
    from bzrlib.foreign import ForeignBranch
 
57
except ImportError:
 
58
    class ForeignBranch(branch.Branch):
 
59
        def __init__(self, mapping):
 
60
            self.mapping = mapping
 
61
            super(ForeignBranch, self).__init__()
 
62
 
 
63
 
 
64
def extract_tags(refs, mapping):
58
65
    ret = {}
59
66
    for k,v in refs.iteritems():
60
67
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
61
68
            v = refs.get(k+"^{}", v)
62
 
            ret[k[len("refs/tags/"):]] = v
 
69
            ret[k[len("refs/tags/"):]] = mapping.revision_id_foreign_to_bzr(v)
63
70
    return ret
64
71
 
65
72
 
88
95
 
89
96
    def get_tag_dict(self):
90
97
        ret = {}
91
 
        for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
92
 
            try:
93
 
                obj = self.repository._git[v]
94
 
            except KeyError:
95
 
                mutter("Tag %s points at unknown object %s, ignoring", v, obj)
96
 
                continue
 
98
        for k,v in self.repository._git.refs.as_dict("refs/tags").iteritems():
 
99
            obj = self.repository._git.get_object(v)
97
100
            while isinstance(obj, Tag):
98
101
                v = obj.object[1]
99
 
                obj = self.repository._git[v]
 
102
                obj = self.repository._git.get_object(v)
100
103
            if not isinstance(obj, Commit):
101
104
                mutter("Tag %s points at object %r that is not a commit, "
102
105
                       "ignoring", k, obj)
109
112
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
110
113
 
111
114
 
112
 
class DictTagDict(LocalGitTagDict):
113
 
 
114
 
 
115
 
    def __init__(self, branch, tags):
116
 
        super(DictTagDict, self).__init__(branch)
117
 
        self._tags = tags
118
 
 
119
 
    def get_tag_dict(self):
120
 
        return self._tags
121
 
 
122
 
 
123
 
 
124
115
class GitBranchFormat(branch.BranchFormat):
125
116
 
126
117
    def get_format_description(self):
127
118
        return 'Git Branch'
128
119
 
129
 
    def network_name(self):
130
 
        return "git"
131
 
 
132
120
    def supports_tags(self):
133
121
        return True
134
122
 
135
 
    def get_foreign_tests_branch_factory(self):
136
 
        from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
137
 
        return ForeignTestsBranchFactory()
138
 
 
139
123
    def make_tags(self, branch):
140
124
        if getattr(branch.repository, "get_refs", None) is not None:
141
125
            from bzrlib.plugins.git.remote import RemoteGitTagDict
147
131
class GitBranch(ForeignBranch):
148
132
    """An adapter to git repositories for bzr Branch objects."""
149
133
 
150
 
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
 
134
    def __init__(self, bzrdir, repository, name, lockfiles):
151
135
        self.repository = repository
152
136
        self._format = GitBranchFormat()
153
137
        self.control_files = lockfiles
154
138
        self.bzrdir = bzrdir
155
139
        super(GitBranch, self).__init__(repository.get_mapping())
156
 
        if tagsdict is not None:
157
 
            self.tags = DictTagDict(self, tagsdict)
158
140
        self.name = name
159
141
        self._head = None
160
 
        self.base = bzrdir.root_transport.base
161
 
 
162
 
    def _get_checkout_format(self):
163
 
        """Return the most suitable metadir for a checkout of this branch.
164
 
        Weaves are used if this branch's repository uses weaves.
165
 
        """
166
 
        return get_rich_root_format()
167
 
 
168
 
    def get_child_submit_format(self):
169
 
        """Return the preferred format of submissions to this branch."""
170
 
        ret = self.get_config().get_user_option("child_submit_format")
171
 
        if ret is not None:
172
 
            return ret
173
 
        return "git"
 
142
        self.base = bzrdir.transport.base
174
143
 
175
144
    def _get_nick(self, local=False, possible_master_transports=None):
176
145
        """Find the nick name for this branch.
197
166
 
198
167
    def get_stacked_on_url(self):
199
168
        # Git doesn't do stacking (yet...)
200
 
        raise errors.UnstackableBranchFormat(self._format, self.base)
 
169
        return None
201
170
 
202
171
    def get_parent(self):
203
172
        """See Branch.get_parent()."""
231
200
        return branch.InterBranch.get(self, target)._basic_push(
232
201
            overwrite, stop_revision)
233
202
 
234
 
 
 
203
 
235
204
class LocalGitBranch(GitBranch):
236
205
    """A local Git branch."""
237
206
 
 
207
    def _get_checkout_format(self):
 
208
        """Return the most suitable metadir for a checkout of this branch.
 
209
        Weaves are used if this branch's repository uses weaves.
 
210
        """
 
211
        format = self.repository.bzrdir.checkout_metadir()
 
212
        format.set_branch_format(self._format)
 
213
        return format
 
214
 
238
215
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
239
216
        accelerator_tree=None, hardlink=False):
240
217
        if lightweight:
242
219
            t.ensure_base()
243
220
            format = self._get_checkout_format()
244
221
            checkout = format.initialize_on_transport(t)
245
 
            from_branch = branch.BranchReferenceFormat().initialize(checkout,
 
222
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
246
223
                self)
247
224
            tree = checkout.create_workingtree(revision_id,
248
225
                from_branch=from_branch, hardlink=hardlink)
251
228
            return self._create_heavyweight_checkout(to_location, revision_id,
252
229
            hardlink)
253
230
 
254
 
    def _create_heavyweight_checkout(self, to_location, revision_id=None,
 
231
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
255
232
                                     hardlink=False):
256
233
        """Create a new heavyweight checkout of this branch.
257
234
 
264
241
            to_location, force_new_tree=False, format=get_rich_root_format())
265
242
        checkout = checkout_branch.bzrdir
266
243
        checkout_branch.bind(self)
267
 
        # pull up to the specified revision_id to set the initial
 
244
        # pull up to the specified revision_id to set the initial 
268
245
        # branch tip correctly, and seed it with history.
269
246
        checkout_branch.pull(self, stop_revision=revision_id)
270
247
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
322
299
            if self.old_revid == self.new_revid:
323
300
                to_file.write('No revisions to pull.\n')
324
301
            else:
325
 
                to_file.write('Now on revision %d (git sha: %s).\n' %
 
302
                to_file.write('Now on revision %d (git sha: %s).\n' % 
326
303
                        (self.new_revno, self.new_git_head))
327
304
        self._show_tag_conficts(to_file)
328
305
 
351
328
    """InterBranch implementation that pulls from Git into bzr."""
352
329
 
353
330
    @classmethod
354
 
    def _get_interrepo(self, source, target):
355
 
        return repository.InterRepository.get(source.repository,
356
 
            target.repository)
357
 
 
358
 
    @classmethod
359
 
    def is_compatible(cls, source, target):
360
 
        return (isinstance(source, GitBranch) and
361
 
                not isinstance(target, GitBranch) and
362
 
                (getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
 
331
    def is_compatible(self, source, target):
 
332
        return (isinstance(source, GitBranch) and 
 
333
                not isinstance(target, GitBranch))
363
334
 
364
335
    def update_revisions(self, stop_revision=None, overwrite=False,
365
336
        graph=None):
366
337
        """See InterBranch.update_revisions()."""
367
 
        interrepo = self._get_interrepo(self.source, self.target)
 
338
        interrepo = repository.InterRepository.get(self.source.repository, 
 
339
            self.target.repository)
368
340
        self._head = None
369
341
        self._last_revid = None
370
342
        def determine_wants(heads):
372
344
                raise NoSuchRef(self.source.name, heads.keys())
373
345
            if stop_revision is not None:
374
346
                self._last_revid = stop_revision
375
 
                self._head, mapping = self.source.repository.lookup_bzr_revision_id(
 
347
                self._head, mapping = self.source.repository.lookup_git_revid(
376
348
                    stop_revision)
377
349
            else:
378
350
                self._head = heads[self.source.name]
417
389
            graph = self.target.repository.get_graph(self.source.repository)
418
390
            result.old_revno, result.old_revid = \
419
391
                self.target.last_revision_info()
420
 
            self.update_revisions(stop_revision, overwrite=overwrite,
 
392
            self.update_revisions(stop_revision, overwrite=overwrite, 
421
393
                graph=graph)
422
394
            result.new_git_head = self._head
423
395
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
442
414
        result.target_branch = self.target
443
415
        graph = self.target.repository.get_graph(self.source.repository)
444
416
        result.old_revno, result.old_revid = self.target.last_revision_info()
445
 
        self.update_revisions(stop_revision, overwrite=overwrite, graph=graph)
 
417
        self.update_revisions(stop_revision, overwrite=overwrite, 
 
418
            graph=graph)
446
419
        result.new_git_head = self._head
447
420
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
448
421
            overwrite)
460
433
    @classmethod
461
434
    def is_compatible(self, source, target):
462
435
        from bzrlib.plugins.git.remote import RemoteGitBranch
463
 
        return (isinstance(source, LocalGitBranch) and
 
436
        return (isinstance(source, LocalGitBranch) and 
464
437
                isinstance(target, RemoteGitBranch))
465
438
 
466
439
    def _basic_push(self, overwrite=False, stop_revision=None):
471
444
            stop_revision = self.source.last_revision()
472
445
        # FIXME: Check for diverged branches
473
446
        def get_changed_refs(old_refs):
474
 
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
475
 
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
 
447
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs["refs/heads/master"])
 
448
            refs = { "refs/heads/master": self.source.repository.lookup_git_revid(stop_revision)[0] }
476
449
            result.new_revid = stop_revision
477
450
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
478
451
                refs["refs/tags/%s" % name] = sha
479
452
            return refs
480
 
        self.target.repository.send_pack(get_changed_refs,
 
453
        self.target.repository.send_pack(get_changed_refs, 
481
454
                self.source.repository._git.object_store.generate_pack_contents)
482
455
        return result
483
456
 
488
461
    @classmethod
489
462
    def is_compatible(self, source, target):
490
463
        from bzrlib.plugins.git.remote import RemoteGitBranch
491
 
        return (isinstance(source, RemoteGitBranch) and
 
464
        return (isinstance(source, RemoteGitBranch) and 
492
465
                isinstance(target, LocalGitBranch))
493
466
 
494
467
    def _basic_push(self, overwrite=False, stop_revision=None):
503
476
        return result
504
477
 
505
478
    def update_tags(self, refs):
506
 
        for name, v in extract_tags(refs).iteritems():
507
 
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
 
479
        for name, revid in extract_tags(refs, self.target.mapping).iteritems():
508
480
            self.target.tags.set_tag(name, revid)
509
481
 
510
482
    def update_refs(self, stop_revision=None):
511
 
        interrepo = repository.InterRepository.get(self.source.repository,
 
483
        interrepo = repository.InterRepository.get(self.source.repository, 
512
484
            self.target.repository)
513
485
        if stop_revision is None:
514
486
            refs = interrepo.fetch_refs(branches=["HEAD"])
517
489
            refs = interrepo.fetch_refs(revision_id=stop_revision)
518
490
        return refs, stop_revision
519
491
 
520
 
    def pull(self, stop_revision=None, overwrite=False,
 
492
    def pull(self, stop_revision=None, overwrite=False, 
521
493
        possible_transports=None, local=False):
522
494
        # This type of branch can't be bound.
523
495
        if local:
532
504
        result.new_revid = self.target.last_revision()
533
505
        return result
534
506
 
535
 
 
 
507
    
536
508
class InterToGitBranch(branch.InterBranch):
537
509
    """InterBranch implementation that pulls from Git into bzr."""
538
510
 
539
 
    @staticmethod
540
 
    def _get_branch_formats_to_test():
541
 
        return None, None
542
 
 
543
511
    @classmethod
544
512
    def is_compatible(self, source, target):
545
 
        return (not isinstance(source, GitBranch) and
 
513
        return (not isinstance(source, GitBranch) and 
546
514
                isinstance(target, GitBranch))
547
515
 
548
 
    def update_revisions(self, *args, **kwargs):
549
 
        raise NoPushSupport()
550
 
 
551
 
    def push(self, overwrite=True, stop_revision=None,
 
516
    def push(self, overwrite=True, stop_revision=None, 
552
517
             _override_hook_source_branch=None):
553
518
        raise NoPushSupport()
554
519
 
556
521
        result = GitBranchPushResult()
557
522
        result.source_branch = self.source
558
523
        result.target_branch = self.target
559
 
        try:
560
 
            result.old_revid = self.target.last_revision()
561
 
        except NoSuchRef:
562
 
            result.old_revid = revision.NULL_REVISION
 
524
        result.old_revid = self.target.last_revision()
563
525
        if stop_revision is None:
564
526
            stop_revision = self.source.last_revision()
565
527
        # FIXME: Check for diverged branches