/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: 2010-02-12 00:39:11 UTC
  • mto: (0.200.718 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100212003911-hz63ctlfgsvrzrjh
Cope with has_index not existing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
    bzrdir,
28
28
    config,
29
29
    errors,
30
 
    foreign,
31
30
    repository,
32
31
    revision,
33
32
    tag,
52
51
    NoSuchRef,
53
52
    )
54
53
 
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):
 
54
from bzrlib.foreign import ForeignBranch
 
55
 
 
56
 
 
57
def extract_tags(refs):
65
58
    ret = {}
66
59
    for k,v in refs.iteritems():
67
60
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
68
61
            v = refs.get(k+"^{}", v)
69
 
            ret[k[len("refs/tags/"):]] = mapping.revision_id_foreign_to_bzr(v)
 
62
            ret[k[len("refs/tags/"):]] = v
70
63
    return ret
71
64
 
72
65
 
95
88
 
96
89
    def get_tag_dict(self):
97
90
        ret = {}
98
 
        for k,v in self.repository._git.refs.as_dict("refs/tags").iteritems():
99
 
            obj = self.repository._git.get_object(v)
 
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
100
97
            while isinstance(obj, Tag):
101
98
                v = obj.object[1]
102
 
                obj = self.repository._git.get_object(v)
 
99
                obj = self.repository._git[v]
103
100
            if not isinstance(obj, Commit):
104
101
                mutter("Tag %s points at object %r that is not a commit, "
105
102
                       "ignoring", k, obj)
107
104
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
108
105
        return ret
109
106
 
 
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
 
111
            if name in extra:
 
112
                extra.remove(name)
 
113
            self.set_tag(k, revid)
 
114
        for name in extra:
 
115
            if name.startswith("refs/tags/"):
 
116
                del self.repository._git[name]
 
117
        
110
118
    def set_tag(self, name, revid):
111
119
        self.repository._git.refs["refs/tags/%s" % name], _ = \
112
120
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
113
121
 
114
122
 
 
123
class DictTagDict(LocalGitTagDict):
 
124
 
 
125
 
 
126
    def __init__(self, branch, tags):
 
127
        super(DictTagDict, self).__init__(branch)
 
128
        self._tags = tags
 
129
 
 
130
    def get_tag_dict(self):
 
131
        return self._tags
 
132
 
 
133
 
 
134
 
115
135
class GitBranchFormat(branch.BranchFormat):
116
136
 
117
137
    def get_format_description(self):
118
138
        return 'Git Branch'
119
139
 
 
140
    def network_name(self):
 
141
        return "git"
 
142
 
120
143
    def supports_tags(self):
121
144
        return True
122
145
 
 
146
    def get_foreign_tests_branch_factory(self):
 
147
        from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
 
148
        return ForeignTestsBranchFactory()
 
149
 
123
150
    def make_tags(self, branch):
124
151
        if getattr(branch.repository, "get_refs", None) is not None:
125
152
            from bzrlib.plugins.git.remote import RemoteGitTagDict
131
158
class GitBranch(ForeignBranch):
132
159
    """An adapter to git repositories for bzr Branch objects."""
133
160
 
134
 
    def __init__(self, bzrdir, repository, name, lockfiles):
 
161
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
135
162
        self.repository = repository
136
163
        self._format = GitBranchFormat()
137
164
        self.control_files = lockfiles
138
165
        self.bzrdir = bzrdir
139
166
        super(GitBranch, self).__init__(repository.get_mapping())
 
167
        if tagsdict is not None:
 
168
            self.tags = DictTagDict(self, tagsdict)
140
169
        self.name = name
141
170
        self._head = None
142
 
        self.base = bzrdir.transport.base
 
171
        self.base = bzrdir.root_transport.base
 
172
 
 
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.
 
176
        """
 
177
        return get_rich_root_format()
 
178
 
 
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")
 
182
        if ret is not None:
 
183
            return ret
 
184
        return "git"
143
185
 
144
186
    def _get_nick(self, local=False, possible_master_transports=None):
145
187
        """Find the nick name for this branch.
166
208
 
167
209
    def get_stacked_on_url(self):
168
210
        # Git doesn't do stacking (yet...)
169
 
        return None
 
211
        raise errors.UnstackableBranchFormat(self._format, self.base)
170
212
 
171
213
    def get_parent(self):
172
214
        """See Branch.get_parent()."""
200
242
        return branch.InterBranch.get(self, target)._basic_push(
201
243
            overwrite, stop_revision)
202
244
 
203
 
 
 
245
 
204
246
class LocalGitBranch(GitBranch):
205
247
    """A local Git branch."""
206
248
 
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
 
 
215
249
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
216
250
        accelerator_tree=None, hardlink=False):
217
251
        if lightweight:
219
253
            t.ensure_base()
220
254
            format = self._get_checkout_format()
221
255
            checkout = format.initialize_on_transport(t)
222
 
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
 
256
            from_branch = branch.BranchReferenceFormat().initialize(checkout,
223
257
                self)
224
258
            tree = checkout.create_workingtree(revision_id,
225
259
                from_branch=from_branch, hardlink=hardlink)
228
262
            return self._create_heavyweight_checkout(to_location, revision_id,
229
263
            hardlink)
230
264
 
231
 
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
 
265
    def _create_heavyweight_checkout(self, to_location, revision_id=None,
232
266
                                     hardlink=False):
233
267
        """Create a new heavyweight checkout of this branch.
234
268
 
241
275
            to_location, force_new_tree=False, format=get_rich_root_format())
242
276
        checkout = checkout_branch.bzrdir
243
277
        checkout_branch.bind(self)
244
 
        # pull up to the specified revision_id to set the initial 
 
278
        # pull up to the specified revision_id to set the initial
245
279
        # branch tip correctly, and seed it with history.
246
280
        checkout_branch.pull(self, stop_revision=revision_id)
247
281
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
299
333
            if self.old_revid == self.new_revid:
300
334
                to_file.write('No revisions to pull.\n')
301
335
            else:
302
 
                to_file.write('Now on revision %d (git sha: %s).\n' % 
 
336
                to_file.write('Now on revision %d (git sha: %s).\n' %
303
337
                        (self.new_revno, self.new_git_head))
304
338
        self._show_tag_conficts(to_file)
305
339
 
328
362
    """InterBranch implementation that pulls from Git into bzr."""
329
363
 
330
364
    @classmethod
331
 
    def is_compatible(self, source, target):
332
 
        return (isinstance(source, GitBranch) and 
333
 
                not isinstance(target, GitBranch))
 
365
    def _get_interrepo(self, source, target):
 
366
        return repository.InterRepository.get(source.repository,
 
367
            target.repository)
 
368
 
 
369
    @classmethod
 
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))
334
374
 
335
375
    def update_revisions(self, stop_revision=None, overwrite=False,
336
376
        graph=None):
337
377
        """See InterBranch.update_revisions()."""
338
 
        interrepo = repository.InterRepository.get(self.source.repository, 
339
 
            self.target.repository)
 
378
        interrepo = self._get_interrepo(self.source, self.target)
340
379
        self._head = None
341
380
        self._last_revid = None
342
381
        def determine_wants(heads):
344
383
                raise NoSuchRef(self.source.name, heads.keys())
345
384
            if stop_revision is not None:
346
385
                self._last_revid = stop_revision
347
 
                self._head, mapping = self.source.repository.lookup_git_revid(
 
386
                self._head, mapping = self.source.repository.lookup_bzr_revision_id(
348
387
                    stop_revision)
349
388
            else:
350
389
                self._head = heads[self.source.name]
389
428
            graph = self.target.repository.get_graph(self.source.repository)
390
429
            result.old_revno, result.old_revid = \
391
430
                self.target.last_revision_info()
392
 
            self.update_revisions(stop_revision, overwrite=overwrite, 
 
431
            self.update_revisions(stop_revision, overwrite=overwrite,
393
432
                graph=graph)
394
433
            result.new_git_head = self._head
395
434
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
414
453
        result.target_branch = self.target
415
454
        graph = self.target.repository.get_graph(self.source.repository)
416
455
        result.old_revno, result.old_revid = self.target.last_revision_info()
417
 
        self.update_revisions(stop_revision, overwrite=overwrite, 
418
 
            graph=graph)
 
456
        self.update_revisions(stop_revision, overwrite=overwrite, graph=graph)
419
457
        result.new_git_head = self._head
420
458
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
421
459
            overwrite)
433
471
    @classmethod
434
472
    def is_compatible(self, source, target):
435
473
        from bzrlib.plugins.git.remote import RemoteGitBranch
436
 
        return (isinstance(source, LocalGitBranch) and 
 
474
        return (isinstance(source, LocalGitBranch) and
437
475
                isinstance(target, RemoteGitBranch))
438
476
 
439
477
    def _basic_push(self, overwrite=False, stop_revision=None):
445
483
        # FIXME: Check for diverged branches
446
484
        def get_changed_refs(old_refs):
447
485
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
448
 
            refs = { "refs/heads/master": self.source.repository.lookup_git_revid(stop_revision)[0] }
 
486
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
449
487
            result.new_revid = stop_revision
450
488
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
451
489
                refs["refs/tags/%s" % name] = sha
452
490
            return refs
453
 
        self.target.repository.send_pack(get_changed_refs, 
 
491
        self.target.repository.send_pack(get_changed_refs,
454
492
                self.source.repository._git.object_store.generate_pack_contents)
455
493
        return result
456
494
 
461
499
    @classmethod
462
500
    def is_compatible(self, source, target):
463
501
        from bzrlib.plugins.git.remote import RemoteGitBranch
464
 
        return (isinstance(source, RemoteGitBranch) and 
 
502
        return (isinstance(source, RemoteGitBranch) and
465
503
                isinstance(target, LocalGitBranch))
466
504
 
467
505
    def _basic_push(self, overwrite=False, stop_revision=None):
476
514
        return result
477
515
 
478
516
    def update_tags(self, refs):
479
 
        for name, revid in extract_tags(refs, self.target.mapping).iteritems():
 
517
        for name, v in extract_tags(refs).iteritems():
 
518
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
480
519
            self.target.tags.set_tag(name, revid)
481
520
 
482
521
    def update_refs(self, stop_revision=None):
483
 
        interrepo = repository.InterRepository.get(self.source.repository, 
 
522
        interrepo = repository.InterRepository.get(self.source.repository,
484
523
            self.target.repository)
485
524
        if stop_revision is None:
486
525
            refs = interrepo.fetch_refs(branches=["HEAD"])
489
528
            refs = interrepo.fetch_refs(revision_id=stop_revision)
490
529
        return refs, stop_revision
491
530
 
492
 
    def pull(self, stop_revision=None, overwrite=False, 
 
531
    def pull(self, stop_revision=None, overwrite=False,
493
532
        possible_transports=None, local=False):
494
533
        # This type of branch can't be bound.
495
534
        if local:
504
543
        result.new_revid = self.target.last_revision()
505
544
        return result
506
545
 
507
 
    
 
546
 
508
547
class InterToGitBranch(branch.InterBranch):
509
548
    """InterBranch implementation that pulls from Git into bzr."""
510
549
 
 
550
    @staticmethod
 
551
    def _get_branch_formats_to_test():
 
552
        return None, None
 
553
 
511
554
    @classmethod
512
555
    def is_compatible(self, source, target):
513
 
        return (not isinstance(source, GitBranch) and 
 
556
        return (not isinstance(source, GitBranch) and
514
557
                isinstance(target, GitBranch))
515
558
 
516
559
    def update_revisions(self, *args, **kwargs):
517
560
        raise NoPushSupport()
518
561
 
519
 
    def push(self, overwrite=True, stop_revision=None, 
 
562
    def push(self, overwrite=True, stop_revision=None,
520
563
             _override_hook_source_branch=None):
521
564
        raise NoPushSupport()
522
565
 
524
567
        result = GitBranchPushResult()
525
568
        result.source_branch = self.source
526
569
        result.target_branch = self.target
527
 
        result.old_revid = self.target.last_revision()
 
570
        try:
 
571
            result.old_revid = self.target.last_revision()
 
572
        except NoSuchRef:
 
573
            result.old_revid = revision.NULL_REVISION
528
574
        if stop_revision is None:
529
575
            stop_revision = self.source.last_revision()
530
576
        # FIXME: Check for diverged branches