/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

Fix some more tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
2
 
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
 
2
# Copyright (C) 2009-2010 Jelmer Vernooij <jelmer@samba.org>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
50
50
    NoPushSupport,
51
51
    NoSuchRef,
52
52
    )
 
53
from bzrlib.plugins.git.refs import (
 
54
    branch_name_to_ref,
 
55
    ref_to_branch_name,
 
56
    extract_tags,
 
57
    tag_name_to_ref,
 
58
    )
53
59
 
54
60
from bzrlib.foreign import ForeignBranch
55
61
 
56
62
 
57
 
def extract_tags(refs):
58
 
    """Extract the tags from a refs dictionary.
59
 
 
60
 
    :param refs: Refs to extract the tags from.
61
 
    :return: Dictionary mapping tag names to SHA1s.
62
 
    """
63
 
    ret = {}
64
 
    for k,v in refs.iteritems():
65
 
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
66
 
            v = refs.get(k+"^{}", v)
67
 
            ret[k[len("refs/tags/"):]] = v
68
 
    return ret
69
 
 
70
 
 
71
 
def branch_name_to_ref(name):
72
 
    """Map a branch name to a ref.
73
 
 
74
 
    :param name: Branch name
75
 
    :return: ref string
76
 
    """
77
 
    if name is None or name == "HEAD":
78
 
        return "HEAD"
79
 
    if not name.startswith("refs/"):
80
 
        return "refs/heads/%s" % name
81
 
    else:
82
 
        return name
83
 
 
84
 
 
85
 
def ref_to_branch_name(ref):
86
 
    """Map a ref to a branch name
87
 
 
88
 
    :param ref: Ref
89
 
    :return: A branch name
90
 
    """
91
 
    if ref == "HEAD":
92
 
        return "HEAD"
93
 
    if ref.startswith("refs/heads/"):
94
 
        return ref[len("refs/heads/"):]
95
 
    raise ValueError("unable to map ref %s back to branch name")
96
 
 
97
 
 
98
63
class GitPullResult(branch.PullResult):
99
64
 
100
65
    def _lookup_revno(self, revid):
133
98
                mutter("Tag %s points at object %r that is not a commit, "
134
99
                       "ignoring", k, obj)
135
100
                continue
136
 
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
 
101
            ret[k] = self.branch.lookup_foreign_revision_id(v)
137
102
        return ret
138
103
 
139
104
    def _set_tag_dict(self, to_dict):
140
105
        extra = set(self.repository._git.get_refs().keys())
141
106
        for k, revid in to_dict.iteritems():
142
 
            name = "refs/tags/%s" % k
 
107
            name = tag_name_to_ref(k)
143
108
            if name in extra:
144
109
                extra.remove(name)
145
110
            self.set_tag(k, revid)
148
113
                del self.repository._git[name]
149
114
        
150
115
    def set_tag(self, name, revid):
151
 
        self.repository._git.refs["refs/tags/%s" % name], _ = \
 
116
        self.repository._git.refs[tag_name_to_ref(name)], _ = \
152
117
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
153
118
 
154
119
 
185
150
            return LocalGitTagDict(branch)
186
151
 
187
152
 
 
153
class GitReadLock(object):
 
154
 
 
155
    def __init__(self, unlock):
 
156
        self.unlock = unlock
 
157
 
 
158
 
 
159
class GitWriteLock(object):
 
160
 
 
161
    def __init__(self, unlock):
 
162
        self.unlock = unlock
 
163
 
 
164
 
188
165
class GitBranch(ForeignBranch):
189
166
    """An adapter to git repositories for bzr Branch objects."""
190
167
 
219
196
 
220
197
        :return: Branch nick
221
198
        """
222
 
        return self.name
 
199
        return self.name or "HEAD"
223
200
 
224
201
    def _set_nick(self, nick):
225
202
        raise NotImplementedError
228
205
 
229
206
    def __repr__(self):
230
207
        return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
231
 
            self.ref)
 
208
            self.ref or "HEAD")
232
209
 
233
210
    def generate_revision_history(self, revid, old_revid=None):
234
211
        # FIXME: Check that old_revid is in the ancestry of revid
237
214
 
238
215
    def lock_write(self):
239
216
        self.control_files.lock_write()
 
217
        return GitWriteLock(self.unlock)
240
218
 
241
219
    def get_stacked_on_url(self):
242
220
        # Git doesn't do stacking (yet...)
253
231
 
254
232
    def lock_read(self):
255
233
        self.control_files.lock_read()
 
234
        return GitReadLock(self.unlock)
256
235
 
257
236
    def is_locked(self):
258
237
        return self.control_files.is_locked()
268
247
        # perhaps should escape this ?
269
248
        if self.head is None:
270
249
            return revision.NULL_REVISION
271
 
        return self.mapping.revision_id_foreign_to_bzr(self.head)
 
250
        return self.lookup_foreign_revision_id(self.head)
272
251
 
273
252
    def _basic_push(self, target, overwrite=False, stop_revision=None):
274
253
        return branch.InterBranch.get(self, target)._basic_push(
275
254
            overwrite, stop_revision)
276
255
 
 
256
    def lookup_foreign_revision_id(self, foreign_revid):
 
257
        return self.repository.lookup_foreign_revision_id(foreign_revid, 
 
258
            self.mapping)
 
259
 
277
260
 
278
261
class LocalGitBranch(GitBranch):
279
262
    """A local Git branch."""
281
264
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
282
265
        super(LocalGitBranch, self).__init__(bzrdir, repository, name, 
283
266
              lockfiles, tagsdict)
284
 
        if not name in repository._git.get_refs().keys():
 
267
        refs = repository._git.get_refs()
 
268
        if not (name in refs.keys() or "HEAD" in refs.keys()):
285
269
            raise errors.NotBranchError(self.base)
286
270
 
287
271
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
328
312
 
329
313
    def _get_head(self):
330
314
        try:
331
 
            return self.repository._git.ref(self.ref)
 
315
            return self.repository._git.ref(self.ref or "HEAD")
332
316
        except KeyError:
333
317
            return None
334
318
 
336
320
        self.set_last_revision(revid)
337
321
 
338
322
    def set_last_revision(self, revid):
339
 
        (newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
340
 
                revid)
 
323
        (newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
341
324
        self.head = newhead
342
325
 
343
326
    def _set_head(self, value):
344
327
        self._head = value
345
 
        self.repository._git.refs[self.ref] = self._head
 
328
        self.repository._git.refs[self.ref or "HEAD"] = self._head
346
329
        self._clear_cached_state()
347
330
 
348
331
    head = property(_get_head, _set_head)
362
345
 
363
346
    def supports_tags(self):
364
347
        return True
365
 
 
 
348
    
366
349
 
367
350
class GitBranchPullResult(branch.PullResult):
368
351
 
 
352
    def __init__(self):
 
353
        super(GitBranchPullResult, self).__init__()
 
354
        self.new_git_head = None
 
355
        self._old_revno = None
 
356
        self._new_revno = None
 
357
 
369
358
    def report(self, to_file):
370
359
        if not is_quiet():
371
360
            if self.old_revid == self.new_revid:
377
366
                to_file.write('Now on revision %d.\n' % (self.new_revno,))
378
367
        self._show_tag_conficts(to_file)
379
368
 
 
369
    def _lookup_revno(self, revid):
 
370
        assert isinstance(revid, str), "was %r" % revid
 
371
        # Try in source branch first, it'll be faster
 
372
        try:
 
373
            return self.source_branch.revision_id_to_revno(revid)
 
374
        except errors.NoSuchRevision:
 
375
            # FIXME: Check using graph.find_distance_to_null() ?
 
376
            return self.target_branch.revision_id_to_revno(revid)
 
377
 
 
378
    def _get_old_revno(self):
 
379
        if self._old_revno is not None:
 
380
            return self._old_revno
 
381
        return self._lookup_revno(self.old_revid)
 
382
 
 
383
    def _set_old_revno(self, revno):
 
384
        self._old_revno = revno
 
385
 
 
386
    old_revno = property(_get_old_revno, _set_old_revno)
 
387
 
 
388
    def _get_new_revno(self):
 
389
        if self._new_revno is not None:
 
390
            return self._new_revno
 
391
        return self._lookup_revno(self.new_revid)
 
392
 
 
393
    def _set_new_revno(self, revno):
 
394
        self._new_revno = revno
 
395
    
 
396
    new_revno = property(_get_new_revno, _set_new_revno)
 
397
 
380
398
 
381
399
class GitBranchPushResult(branch.BranchPushResult):
382
400
 
422
440
        """
423
441
        interrepo = self._get_interrepo(self.source, self.target)
424
442
        def determine_wants(heads):
425
 
            if not self.source.ref in heads:
 
443
            if self.source.ref is not None and not self.source.ref in heads:
426
444
                raise NoSuchRef(self.source.ref, heads.keys())
427
445
            if stop_revision is not None:
428
446
                self._last_revid = stop_revision
429
447
                head, mapping = self.source.repository.lookup_bzr_revision_id(
430
448
                    stop_revision)
431
449
            else:
432
 
                head = heads[self.source.ref]
433
 
                self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
434
 
                    head)
 
450
                if self.source.ref is not None:
 
451
                    head = heads[self.source.ref]
 
452
                else:
 
453
                    head = heads["HEAD"]
 
454
                self._last_revid = self.source.lookup_foreign_revision_id(head)
435
455
            if self.target.repository.has_revision(self._last_revid):
436
456
                return []
437
457
            return [head]
438
458
        pack_hint, head = interrepo.fetch_objects(
439
459
            determine_wants, self.source.mapping, limit=limit)
440
 
        if pack_hint is not None and self.target.repository._format.pack_compresses:
 
460
        if (pack_hint is not None and
 
461
            self.target.repository._format.pack_compresses):
441
462
            self.target.repository.pack(hint=pack_hint)
442
463
        if head is not None:
443
 
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
 
464
            self._last_revid = self.source.lookup_foreign_revision_id(head)
444
465
        if overwrite:
445
466
            prev_last_revid = None
446
467
        else:
532
553
                isinstance(target, RemoteGitBranch))
533
554
 
534
555
    def _basic_push(self, overwrite=False, stop_revision=None):
 
556
        from dulwich.protocol import ZERO_SHA
535
557
        result = GitBranchPushResult()
536
558
        result.source_branch = self.source
537
559
        result.target_branch = self.target
539
561
            stop_revision = self.source.last_revision()
540
562
        # FIXME: Check for diverged branches
541
563
        def get_changed_refs(old_refs):
542
 
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
543
 
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
 
564
            result.old_revid = self.target.lookup_foreign_revision_id(old_refs.get(self.target.ref, ZERO_SHA))
 
565
            refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
544
566
            result.new_revid = stop_revision
545
567
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
546
 
                refs["refs/tags/%s" % name] = sha
 
568
                refs[tag_name_to_ref(name)] = sha
547
569
            return refs
548
570
        self.target.repository.send_pack(get_changed_refs,
549
571
            self.source.repository._git.object_store.generate_pack_contents)
572
594
 
573
595
    def update_tags(self, refs):
574
596
        for name, v in extract_tags(refs).iteritems():
575
 
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
 
597
            revid = self.target.lookup_foreign_revision_id(v)
576
598
            self.target.tags.set_tag(name, revid)
577
599
 
578
600
    def update_refs(self, stop_revision=None):
580
602
            self.target.repository)
581
603
        if stop_revision is None:
582
604
            refs = interrepo.fetch_refs(branches=["HEAD"])
583
 
            stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
 
605
            stop_revision = self.target.lookup_foreign_revision_id(refs["HEAD"])
584
606
        else:
585
607
            refs = interrepo.fetch_refs(revision_id=stop_revision)
586
608
        return refs, stop_revision
616
638
    def update_revisions(self, *args, **kwargs):
617
639
        raise NoPushSupport()
618
640
 
619
 
    def push(self, overwrite=True, stop_revision=None,
 
641
    def _get_new_refs(self, stop_revision=None):
 
642
        if stop_revision is None:
 
643
            stop_revision = self.source.last_revision()
 
644
        main_ref = self.target.ref or "refs/heads/master"
 
645
        refs = { main_ref: stop_revision }
 
646
        for name, revid in self.source.tags.get_tag_dict().iteritems():
 
647
            if self.source.repository.has_revision(revid):
 
648
                refs[tag_name_to_ref(name)] = revid
 
649
        return refs, main_ref
 
650
 
 
651
    def pull(self, overwrite=False, stop_revision=None, local=False,
 
652
             possible_transports=None):
 
653
        from dulwich.protocol import ZERO_SHA
 
654
        result = GitBranchPullResult()
 
655
        result.source_branch = self.source
 
656
        result.target_branch = self.target
 
657
        # FIXME: Check for diverged branches
 
658
        old_refs = self.target.repository._git.get_refs()
 
659
        refs = dict(old_refs)
 
660
        new_refs, main_ref = self._get_new_refs(stop_revision)
 
661
        refs.update(new_refs)
 
662
        self.target.repository.fetch_refs(self.source.repository, refs)
 
663
        result.old_revid = self.target.lookup_foreign_revision_id(
 
664
            old_refs.get(main_ref, ZERO_SHA))
 
665
        result.new_revid = refs[main_ref]
 
666
        return result
 
667
 
 
668
    def push(self, overwrite=False, stop_revision=None,
620
669
             _override_hook_source_branch=None):
621
 
        raise NoPushSupport()
 
670
        from dulwich.protocol import ZERO_SHA
 
671
        result = GitBranchPushResult()
 
672
        result.source_branch = self.source
 
673
        result.target_branch = self.target
 
674
        # FIXME: Check for diverged branches
 
675
        old_refs = self.target.repository._git.get_refs()
 
676
        refs = dict(old_refs)
 
677
        new_refs, main_ref = self._get_new_refs(stop_revision)
 
678
        refs.update(new_refs)
 
679
        self.target.repository.fetch_refs(self.source.repository, refs)
 
680
        result.old_revid = self.target.lookup_foreign_revision_id(
 
681
            old_refs.get(main_ref, ZERO_SHA))
 
682
        result.new_revid = refs[main_ref]
 
683
        return result
622
684
 
623
685
    def lossy_push(self, stop_revision=None):
 
686
        from dulwich.protocol import ZERO_SHA
624
687
        result = GitBranchPushResult()
625
688
        result.source_branch = self.source
626
689
        result.target_branch = self.target
627
 
        try:
628
 
            result.old_revid = self.target.last_revision()
629
 
        except NoSuchRef:
630
 
            result.old_revid = revision.NULL_REVISION
631
 
        if stop_revision is None:
632
 
            stop_revision = self.source.last_revision()
633
690
        # FIXME: Check for diverged branches
634
 
        refs = { "refs/heads/master": stop_revision }
635
 
        for name, revid in self.source.tags.get_tag_dict().iteritems():
636
 
            if self.source.repository.has_revision(revid):
637
 
                refs["refs/tags/%s" % name] = revid
638
 
        revidmap, new_refs = self.target.repository.dfetch_refs(
 
691
        refs, main_ref = self._get_new_refs(stop_revision)
 
692
        result.revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
639
693
            self.source.repository, refs)
640
 
        if revidmap != {}:
641
 
            self.target.generate_revision_history(revidmap[stop_revision])
642
 
            result.new_revid = revidmap[stop_revision]
643
 
        else:
644
 
            result.new_revid = result.old_revid
645
 
        result.revidmap = revidmap
 
694
        result.old_revid = self.target.lookup_foreign_revision_id(
 
695
            old_refs.get(self.target.ref, ZERO_SHA))
 
696
        result.new_revid = self.target.lookup_foreign_revision_id(
 
697
            new_refs[main_ref])
646
698
        return result
647
699
 
648
700