/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

Implement GitRepository.revision_graph_can_have_wrong_parents().

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
from bzrlib import (
26
26
    branch,
 
27
    bzrdir,
27
28
    config,
28
29
    errors,
29
30
    foreign,
40
41
    mutter,
41
42
    )
42
43
 
 
44
from bzrlib.plugins.git import (
 
45
    get_rich_root_format,
 
46
    )
43
47
from bzrlib.plugins.git.config import (
44
48
    GitBranchConfig,
45
49
    )
46
50
from bzrlib.plugins.git.errors import (
 
51
    NoPushSupport,
47
52
    NoSuchRef,
48
53
    )
49
54
 
56
61
            super(ForeignBranch, self).__init__()
57
62
 
58
63
 
 
64
def extract_tags(refs, mapping):
 
65
    ret = {}
 
66
    for k,v in refs.iteritems():
 
67
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
 
68
            v = refs.get(k+"^{}", v)
 
69
            ret[k[len("refs/tags/"):]] = mapping.revision_id_foreign_to_bzr(v)
 
70
    return ret
 
71
 
 
72
 
59
73
class GitPullResult(branch.PullResult):
60
74
 
61
75
    def _lookup_revno(self, revid):
81
95
 
82
96
    def get_tag_dict(self):
83
97
        ret = {}
84
 
        for k,v in self.repository._git.tags.iteritems():
 
98
        for k,v in self.repository._git.refs.as_dict("refs/tags").iteritems():
85
99
            obj = self.repository._git.get_object(v)
86
100
            while isinstance(obj, Tag):
87
101
                v = obj.object[1]
94
108
        return ret
95
109
 
96
110
    def set_tag(self, name, revid):
97
 
        self.repository._git.tags[name] = revid
 
111
        self.repository._git.refs["refs/tags/%s" % name], _ = \
 
112
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
98
113
 
99
114
 
100
115
class GitBranchFormat(branch.BranchFormat):
141
156
    def __repr__(self):
142
157
        return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
143
158
 
144
 
    def dpull(self, source, stop_revision=None):
145
 
        if stop_revision is None:
146
 
            stop_revision = source.last_revision()
147
 
        # FIXME: Check for diverged branches
148
 
        refs = { "refs/heads/master": stop_revision }
149
 
        for name, revid in source.tags.get_tag_dict().iteritems():
150
 
            if source.repository.has_revision(revid):
151
 
                refs["refs/tags/%s" % name] = revid
152
 
        revidmap, new_refs = self.repository.dfetch_refs(source.repository, 
153
 
                refs)
154
 
        if revidmap != {}:
155
 
            self.generate_revision_history(revidmap[stop_revision])
156
 
        return revidmap
157
 
 
158
159
    def generate_revision_history(self, revid, old_revid=None):
159
160
        # FIXME: Check that old_revid is in the ancestry of revid
160
161
        newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
188
189
    def get_physical_lock_status(self):
189
190
        return False
190
191
 
191
 
 
192
 
class LocalGitBranch(GitBranch):
193
 
    """A local Git branch."""
194
 
 
195
192
    @needs_read_lock
196
193
    def last_revision(self):
197
194
        # perhaps should escape this ?
199
196
            return revision.NULL_REVISION
200
197
        return self.mapping.revision_id_foreign_to_bzr(self.head)
201
198
 
 
199
    def _basic_push(self, target, overwrite=False, stop_revision=None):
 
200
        return branch.InterBranch.get(self, target)._basic_push(
 
201
            overwrite, stop_revision)
 
202
 
 
203
 
 
204
class LocalGitBranch(GitBranch):
 
205
    """A local Git branch."""
 
206
 
202
207
    def _get_checkout_format(self):
203
208
        """Return the most suitable metadir for a checkout of this branch.
204
209
        Weaves are used if this branch's repository uses weaves.
232
237
        :param hardlink: Whether to hardlink
233
238
        :return: WorkingTree object of checkout.
234
239
        """
235
 
        checkout_branch = BzrDir.create_branch_convenience(
 
240
        checkout_branch = bzrdir.BzrDir.create_branch_convenience(
236
241
            to_location, force_new_tree=False, format=get_rich_root_format())
237
242
        checkout = checkout_branch.bzrdir
238
243
        checkout_branch.bind(self)
250
255
        return ret
251
256
 
252
257
    def _get_head(self):
253
 
        return self.repository._git.ref(self.name)
 
258
        try:
 
259
            return self.repository._git.ref(self.name)
 
260
        except KeyError:
 
261
            return None
 
262
 
 
263
    def set_last_revision_info(self, revno, revid):
 
264
        self.set_last_revision(revid)
 
265
 
 
266
    def set_last_revision(self, revid):
 
267
        (newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
 
268
                revid)
 
269
        self.head = newhead
254
270
 
255
271
    def _set_head(self, value):
256
272
        self._head = value
257
 
        self.repository._git.set_ref(self.name, self._head)
 
273
        self.repository._git.refs[self.name] = self._head
258
274
        self._clear_cached_state()
259
275
 
260
276
    head = property(_get_head, _set_head)
288
304
        self._show_tag_conficts(to_file)
289
305
 
290
306
 
291
 
class InterGitGenericBranch(branch.InterBranch):
 
307
class GitBranchPushResult(branch.BranchPushResult):
 
308
 
 
309
    def _lookup_revno(self, revid):
 
310
        assert isinstance(revid, str), "was %r" % revid
 
311
        # Try in source branch first, it'll be faster
 
312
        try:
 
313
            return self.source_branch.revision_id_to_revno(revid)
 
314
        except errors.NoSuchRevision:
 
315
            # FIXME: Check using graph.find_distance_to_null() ?
 
316
            return self.target_branch.revision_id_to_revno(revid)
 
317
 
 
318
    @property
 
319
    def old_revno(self):
 
320
        return self._lookup_revno(self.old_revid)
 
321
 
 
322
    @property
 
323
    def new_revno(self):
 
324
        return self._lookup_revno(self.new_revid)
 
325
 
 
326
 
 
327
class InterFromGitBranch(branch.GenericInterBranch):
292
328
    """InterBranch implementation that pulls from Git into bzr."""
293
329
 
294
330
    @classmethod
372
408
            self.source.unlock()
373
409
        return result
374
410
 
375
 
 
376
 
 
377
 
 
378
 
branch.InterBranch.register_optimiser(InterGitGenericBranch)
379
 
 
380
 
 
381
 
class InterGitRemoteLocalBranch(branch.InterBranch):
 
411
    def _basic_push(self, overwrite=False, stop_revision=None):
 
412
        result = branch.BranchPushResult()
 
413
        result.source_branch = self.source
 
414
        result.target_branch = self.target
 
415
        graph = self.target.repository.get_graph(self.source.repository)
 
416
        result.old_revno, result.old_revid = self.target.last_revision_info()
 
417
        self.update_revisions(stop_revision, overwrite=overwrite, 
 
418
            graph=graph)
 
419
        result.new_git_head = self._head
 
420
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
 
421
            overwrite)
 
422
        result.new_revno, result.new_revid = self.target.last_revision_info()
 
423
        return result
 
424
 
 
425
 
 
426
class InterGitBranch(branch.GenericInterBranch):
382
427
    """InterBranch implementation that pulls between Git branches."""
383
428
 
 
429
 
 
430
class InterGitLocalRemoteBranch(InterGitBranch):
 
431
    """InterBranch that copies from a local to a remote git branch."""
 
432
 
 
433
    @classmethod
 
434
    def is_compatible(self, source, target):
 
435
        from bzrlib.plugins.git.remote import RemoteGitBranch
 
436
        return (isinstance(source, LocalGitBranch) and 
 
437
                isinstance(target, RemoteGitBranch))
 
438
 
 
439
    def _basic_push(self, overwrite=False, stop_revision=None):
 
440
        result = GitBranchPushResult()
 
441
        result.source_branch = self.source
 
442
        result.target_branch = self.target
 
443
        if stop_revision is None:
 
444
            stop_revision = self.source.last_revision()
 
445
        # FIXME: Check for diverged branches
 
446
        def get_changed_refs(old_refs):
 
447
            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] }
 
449
            result.new_revid = stop_revision
 
450
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
 
451
                refs["refs/tags/%s" % name] = sha
 
452
            return refs
 
453
        self.target.repository.send_pack(get_changed_refs, 
 
454
                self.source.repository._git.object_store.generate_pack_contents)
 
455
        return result
 
456
 
 
457
 
 
458
class InterGitRemoteLocalBranch(InterGitBranch):
 
459
    """InterBranch that copies from a remote to a local git branch."""
 
460
 
384
461
    @classmethod
385
462
    def is_compatible(self, source, target):
386
463
        from bzrlib.plugins.git.remote import RemoteGitBranch
387
464
        return (isinstance(source, RemoteGitBranch) and 
388
465
                isinstance(target, LocalGitBranch))
389
466
 
 
467
    def _basic_push(self, overwrite=False, stop_revision=None):
 
468
        result = branch.BranchPushResult()
 
469
        result.source_branch = self.source
 
470
        result.target_branch = self.target
 
471
        result.old_revid = self.target.last_revision()
 
472
        refs, stop_revision = self.update_refs(stop_revision)
 
473
        self.target.generate_revision_history(stop_revision, result.old_revid)
 
474
        self.update_tags(refs)
 
475
        result.new_revid = self.target.last_revision()
 
476
        return result
 
477
 
 
478
    def update_tags(self, refs):
 
479
        for name, revid in extract_tags(refs, self.target.mapping).iteritems():
 
480
            self.target.tags.set_tag(name, revid)
 
481
 
 
482
    def update_refs(self, stop_revision=None):
 
483
        interrepo = repository.InterRepository.get(self.source.repository, 
 
484
            self.target.repository)
 
485
        if stop_revision is None:
 
486
            refs = interrepo.fetch_refs(branches=["HEAD"])
 
487
            stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
 
488
        else:
 
489
            refs = interrepo.fetch_refs(revision_id=stop_revision)
 
490
        return refs, stop_revision
 
491
 
390
492
    def pull(self, stop_revision=None, overwrite=False, 
391
493
        possible_transports=None, local=False):
392
494
        # This type of branch can't be bound.
395
497
        result = GitPullResult()
396
498
        result.source_branch = self.source
397
499
        result.target_branch = self.target
398
 
        interrepo = repository.InterRepository.get(self.source.repository, 
399
 
            self.target.repository)
 
500
        result.old_revid = self.target.last_revision()
 
501
        refs, stop_revision = self.update_refs(stop_revision)
 
502
        self.target.generate_revision_history(stop_revision, result.old_revid)
 
503
        self.update_tags(refs)
 
504
        result.new_revid = self.target.last_revision()
 
505
        return result
 
506
 
 
507
    
 
508
class InterToGitBranch(branch.InterBranch):
 
509
    """InterBranch implementation that pulls from Git into bzr."""
 
510
 
 
511
    @classmethod
 
512
    def is_compatible(self, source, target):
 
513
        return (not isinstance(source, GitBranch) and 
 
514
                isinstance(target, GitBranch))
 
515
 
 
516
    def update_revisions(self, *args, **kwargs):
 
517
        raise NoPushSupport()
 
518
 
 
519
    def push(self, overwrite=True, stop_revision=None, 
 
520
             _override_hook_source_branch=None):
 
521
        raise NoPushSupport()
 
522
 
 
523
    def lossy_push(self, stop_revision=None):
 
524
        result = GitBranchPushResult()
 
525
        result.source_branch = self.source
 
526
        result.target_branch = self.target
400
527
        result.old_revid = self.target.last_revision()
401
528
        if stop_revision is None:
402
 
            refs = interrepo.fetch_refs(branches=["HEAD"])
403
 
            stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
 
529
            stop_revision = self.source.last_revision()
 
530
        # FIXME: Check for diverged branches
 
531
        refs = { "refs/heads/master": stop_revision }
 
532
        for name, revid in self.source.tags.get_tag_dict().iteritems():
 
533
            if self.source.repository.has_revision(revid):
 
534
                refs["refs/tags/%s" % name] = revid
 
535
        revidmap, new_refs = self.target.repository.dfetch_refs(
 
536
            self.source.repository, refs)
 
537
        if revidmap != {}:
 
538
            self.target.generate_revision_history(revidmap[stop_revision])
 
539
            result.new_revid = revidmap[stop_revision]
404
540
        else:
405
 
            refs = interrepo.fetch_refs(revision_id=stop_revision)
406
 
        self.target.generate_revision_history(stop_revision, result.old_revid)
407
 
        result.new_revid = self.target.last_revision()
 
541
            result.new_revid = result.old_revid
 
542
        result.revidmap = revidmap
408
543
        return result
409
544
 
410
545
 
411
546
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
 
547
branch.InterBranch.register_optimiser(InterFromGitBranch)
 
548
branch.InterBranch.register_optimiser(InterToGitBranch)
 
549
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)