/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

properly commit write group

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)
112
109
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
113
110
 
114
111
 
 
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
 
115
124
class GitBranchFormat(branch.BranchFormat):
116
125
 
117
126
    def get_format_description(self):
118
127
        return 'Git Branch'
119
128
 
 
129
    def network_name(self):
 
130
        return "git"
 
131
 
120
132
    def supports_tags(self):
121
133
        return True
122
134
 
 
135
    def get_foreign_tests_branch_factory(self):
 
136
        from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
 
137
        return ForeignTestsBranchFactory()
 
138
 
123
139
    def make_tags(self, branch):
124
140
        if getattr(branch.repository, "get_refs", None) is not None:
125
141
            from bzrlib.plugins.git.remote import RemoteGitTagDict
131
147
class GitBranch(ForeignBranch):
132
148
    """An adapter to git repositories for bzr Branch objects."""
133
149
 
134
 
    def __init__(self, bzrdir, repository, name, lockfiles):
 
150
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
135
151
        self.repository = repository
136
152
        self._format = GitBranchFormat()
137
153
        self.control_files = lockfiles
138
154
        self.bzrdir = bzrdir
139
155
        super(GitBranch, self).__init__(repository.get_mapping())
 
156
        if tagsdict is not None:
 
157
            self.tags = DictTagDict(self, tagsdict)
140
158
        self.name = name
141
159
        self._head = None
142
 
        self.base = bzrdir.transport.base
 
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"
143
174
 
144
175
    def _get_nick(self, local=False, possible_master_transports=None):
145
176
        """Find the nick name for this branch.
166
197
 
167
198
    def get_stacked_on_url(self):
168
199
        # Git doesn't do stacking (yet...)
169
 
        return None
 
200
        raise errors.UnstackableBranchFormat(self._format, self.base)
170
201
 
171
202
    def get_parent(self):
172
203
        """See Branch.get_parent()."""
200
231
        return branch.InterBranch.get(self, target)._basic_push(
201
232
            overwrite, stop_revision)
202
233
 
203
 
 
 
234
 
204
235
class LocalGitBranch(GitBranch):
205
236
    """A local Git branch."""
206
237
 
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
238
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
216
239
        accelerator_tree=None, hardlink=False):
217
240
        if lightweight:
219
242
            t.ensure_base()
220
243
            format = self._get_checkout_format()
221
244
            checkout = format.initialize_on_transport(t)
222
 
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
 
245
            from_branch = branch.BranchReferenceFormat().initialize(checkout,
223
246
                self)
224
247
            tree = checkout.create_workingtree(revision_id,
225
248
                from_branch=from_branch, hardlink=hardlink)
228
251
            return self._create_heavyweight_checkout(to_location, revision_id,
229
252
            hardlink)
230
253
 
231
 
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
 
254
    def _create_heavyweight_checkout(self, to_location, revision_id=None,
232
255
                                     hardlink=False):
233
256
        """Create a new heavyweight checkout of this branch.
234
257
 
241
264
            to_location, force_new_tree=False, format=get_rich_root_format())
242
265
        checkout = checkout_branch.bzrdir
243
266
        checkout_branch.bind(self)
244
 
        # pull up to the specified revision_id to set the initial 
 
267
        # pull up to the specified revision_id to set the initial
245
268
        # branch tip correctly, and seed it with history.
246
269
        checkout_branch.pull(self, stop_revision=revision_id)
247
270
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
299
322
            if self.old_revid == self.new_revid:
300
323
                to_file.write('No revisions to pull.\n')
301
324
            else:
302
 
                to_file.write('Now on revision %d (git sha: %s).\n' % 
 
325
                to_file.write('Now on revision %d (git sha: %s).\n' %
303
326
                        (self.new_revno, self.new_git_head))
304
327
        self._show_tag_conficts(to_file)
305
328
 
328
351
    """InterBranch implementation that pulls from Git into bzr."""
329
352
 
330
353
    @classmethod
331
 
    def is_compatible(self, source, target):
332
 
        return (isinstance(source, GitBranch) and 
333
 
                not isinstance(target, GitBranch))
 
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))
334
363
 
335
364
    def update_revisions(self, stop_revision=None, overwrite=False,
336
365
        graph=None):
337
366
        """See InterBranch.update_revisions()."""
338
 
        interrepo = repository.InterRepository.get(self.source.repository, 
339
 
            self.target.repository)
 
367
        interrepo = self._get_interrepo(self.source, self.target)
340
368
        self._head = None
341
369
        self._last_revid = None
342
370
        def determine_wants(heads):
344
372
                raise NoSuchRef(self.source.name, heads.keys())
345
373
            if stop_revision is not None:
346
374
                self._last_revid = stop_revision
347
 
                self._head, mapping = self.source.repository.lookup_git_revid(
 
375
                self._head, mapping = self.source.repository.lookup_bzr_revision_id(
348
376
                    stop_revision)
349
377
            else:
350
378
                self._head = heads[self.source.name]
389
417
            graph = self.target.repository.get_graph(self.source.repository)
390
418
            result.old_revno, result.old_revid = \
391
419
                self.target.last_revision_info()
392
 
            self.update_revisions(stop_revision, overwrite=overwrite, 
 
420
            self.update_revisions(stop_revision, overwrite=overwrite,
393
421
                graph=graph)
394
422
            result.new_git_head = self._head
395
423
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
414
442
        result.target_branch = self.target
415
443
        graph = self.target.repository.get_graph(self.source.repository)
416
444
        result.old_revno, result.old_revid = self.target.last_revision_info()
417
 
        self.update_revisions(stop_revision, overwrite=overwrite, 
418
 
            graph=graph)
 
445
        self.update_revisions(stop_revision, overwrite=overwrite, graph=graph)
419
446
        result.new_git_head = self._head
420
447
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
421
448
            overwrite)
433
460
    @classmethod
434
461
    def is_compatible(self, source, target):
435
462
        from bzrlib.plugins.git.remote import RemoteGitBranch
436
 
        return (isinstance(source, LocalGitBranch) and 
 
463
        return (isinstance(source, LocalGitBranch) and
437
464
                isinstance(target, RemoteGitBranch))
438
465
 
439
466
    def _basic_push(self, overwrite=False, stop_revision=None):
445
472
        # FIXME: Check for diverged branches
446
473
        def get_changed_refs(old_refs):
447
474
            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] }
 
475
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
449
476
            result.new_revid = stop_revision
450
477
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
451
478
                refs["refs/tags/%s" % name] = sha
452
479
            return refs
453
 
        self.target.repository.send_pack(get_changed_refs, 
 
480
        self.target.repository.send_pack(get_changed_refs,
454
481
                self.source.repository._git.object_store.generate_pack_contents)
455
482
        return result
456
483
 
461
488
    @classmethod
462
489
    def is_compatible(self, source, target):
463
490
        from bzrlib.plugins.git.remote import RemoteGitBranch
464
 
        return (isinstance(source, RemoteGitBranch) and 
 
491
        return (isinstance(source, RemoteGitBranch) and
465
492
                isinstance(target, LocalGitBranch))
466
493
 
467
494
    def _basic_push(self, overwrite=False, stop_revision=None):
476
503
        return result
477
504
 
478
505
    def update_tags(self, refs):
479
 
        for name, revid in extract_tags(refs, self.target.mapping).iteritems():
 
506
        for name, v in extract_tags(refs).iteritems():
 
507
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
480
508
            self.target.tags.set_tag(name, revid)
481
509
 
482
510
    def update_refs(self, stop_revision=None):
483
 
        interrepo = repository.InterRepository.get(self.source.repository, 
 
511
        interrepo = repository.InterRepository.get(self.source.repository,
484
512
            self.target.repository)
485
513
        if stop_revision is None:
486
514
            refs = interrepo.fetch_refs(branches=["HEAD"])
489
517
            refs = interrepo.fetch_refs(revision_id=stop_revision)
490
518
        return refs, stop_revision
491
519
 
492
 
    def pull(self, stop_revision=None, overwrite=False, 
 
520
    def pull(self, stop_revision=None, overwrite=False,
493
521
        possible_transports=None, local=False):
494
522
        # This type of branch can't be bound.
495
523
        if local:
504
532
        result.new_revid = self.target.last_revision()
505
533
        return result
506
534
 
507
 
    
 
535
 
508
536
class InterToGitBranch(branch.InterBranch):
509
537
    """InterBranch implementation that pulls from Git into bzr."""
510
538
 
 
539
    @staticmethod
 
540
    def _get_branch_formats_to_test():
 
541
        return None, None
 
542
 
511
543
    @classmethod
512
544
    def is_compatible(self, source, target):
513
 
        return (not isinstance(source, GitBranch) and 
 
545
        return (not isinstance(source, GitBranch) and
514
546
                isinstance(target, GitBranch))
515
547
 
516
548
    def update_revisions(self, *args, **kwargs):
517
549
        raise NoPushSupport()
518
550
 
519
 
    def push(self, overwrite=True, stop_revision=None, 
 
551
    def push(self, overwrite=True, stop_revision=None,
520
552
             _override_hook_source_branch=None):
521
553
        raise NoPushSupport()
522
554
 
524
556
        result = GitBranchPushResult()
525
557
        result.source_branch = self.source
526
558
        result.target_branch = self.target
527
 
        result.old_revid = self.target.last_revision()
 
559
        try:
 
560
            result.old_revid = self.target.last_revision()
 
561
        except NoSuchRef:
 
562
            result.old_revid = revision.NULL_REVISION
528
563
        if stop_revision is None:
529
564
            stop_revision = self.source.last_revision()
530
565
        # FIXME: Check for diverged branches