/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

Don't claim control directories can be accessed directly, always open the
branch itself.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
2
 
# Copyright (C) 2009-2010 Jelmer Vernooij <jelmer@samba.org>
 
2
# Copyright (C) 2009 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
 
    ref_to_branch_name,
55
 
    extract_tags,
56
 
    tag_name_to_ref,
57
 
    )
58
53
 
59
54
from bzrlib.foreign import ForeignBranch
60
55
 
61
56
 
 
57
def extract_tags(refs):
 
58
    ret = {}
 
59
    for k,v in refs.iteritems():
 
60
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
 
61
            v = refs.get(k+"^{}", v)
 
62
            ret[k[len("refs/tags/"):]] = v
 
63
    return ret
 
64
 
 
65
 
62
66
class GitPullResult(branch.PullResult):
63
67
 
64
68
    def _lookup_revno(self, revid):
103
107
    def _set_tag_dict(self, to_dict):
104
108
        extra = set(self.repository._git.get_refs().keys())
105
109
        for k, revid in to_dict.iteritems():
106
 
            name = tag_name_to_ref(k)
 
110
            name = "refs/tags/%s" % k
107
111
            if name in extra:
108
112
                extra.remove(name)
109
113
            self.set_tag(k, revid)
112
116
                del self.repository._git[name]
113
117
        
114
118
    def set_tag(self, name, revid):
115
 
        self.repository._git.refs[tag_name_to_ref(name)], _ = \
 
119
        self.repository._git.refs["refs/tags/%s" % name], _ = \
116
120
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
117
121
 
118
122
 
119
123
class DictTagDict(LocalGitTagDict):
120
124
 
 
125
 
121
126
    def __init__(self, branch, tags):
122
127
        super(DictTagDict, self).__init__(branch)
123
128
        self._tags = tags
126
131
        return self._tags
127
132
 
128
133
 
 
134
 
129
135
class GitBranchFormat(branch.BranchFormat):
130
136
 
131
137
    def get_format_description(self):
152
158
class GitBranch(ForeignBranch):
153
159
    """An adapter to git repositories for bzr Branch objects."""
154
160
 
155
 
    def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
 
161
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
156
162
        self.repository = repository
157
163
        self._format = GitBranchFormat()
158
164
        self.control_files = lockfiles
160
166
        super(GitBranch, self).__init__(repository.get_mapping())
161
167
        if tagsdict is not None:
162
168
            self.tags = DictTagDict(self, tagsdict)
163
 
        self.ref = ref
164
 
        self.name = ref_to_branch_name(ref)
 
169
        self.name = name
165
170
        self._head = None
166
171
        self.base = bzrdir.root_transport.base
 
172
        if self.name != "HEAD":
 
173
            self.base += ",%s" % self.name
167
174
 
168
175
    def _get_checkout_format(self):
169
176
        """Return the most suitable metadir for a checkout of this branch.
191
198
    nick = property(_get_nick, _set_nick)
192
199
 
193
200
    def __repr__(self):
194
 
        return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
195
 
            self.ref)
 
201
        return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
196
202
 
197
203
    def generate_revision_history(self, revid, old_revid=None):
198
204
        # FIXME: Check that old_revid is in the ancestry of revid
242
248
class LocalGitBranch(GitBranch):
243
249
    """A local Git branch."""
244
250
 
245
 
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
246
 
        super(LocalGitBranch, self).__init__(bzrdir, repository, name, 
247
 
              lockfiles, tagsdict)
248
 
        if not name in repository._git.get_refs().keys():
249
 
            raise errors.NotBranchError(self.base)
250
 
 
251
251
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
252
252
        accelerator_tree=None, hardlink=False):
253
253
        if lightweight:
292
292
 
293
293
    def _get_head(self):
294
294
        try:
295
 
            return self.repository._git.ref(self.ref)
 
295
            return self.repository._git.ref(self.name)
296
296
        except KeyError:
297
297
            return None
298
298
 
300
300
        self.set_last_revision(revid)
301
301
 
302
302
    def set_last_revision(self, revid):
303
 
        (newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
 
303
        (newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
 
304
                revid)
304
305
        self.head = newhead
305
306
 
306
307
    def _set_head(self, value):
307
308
        self._head = value
308
 
        self.repository._git.refs[self.ref] = self._head
 
309
        self.repository._git.refs[self.name] = self._head
309
310
        self._clear_cached_state()
310
311
 
311
312
    head = property(_get_head, _set_head)
385
386
        """
386
387
        interrepo = self._get_interrepo(self.source, self.target)
387
388
        def determine_wants(heads):
388
 
            if not self.source.ref in heads:
389
 
                raise NoSuchRef(self.source.ref, heads.keys())
 
389
            if not self.source.name in heads:
 
390
                raise NoSuchRef(self.source.name, heads.keys())
390
391
            if stop_revision is not None:
391
392
                self._last_revid = stop_revision
392
393
                head, mapping = self.source.repository.lookup_bzr_revision_id(
393
394
                    stop_revision)
394
395
            else:
395
 
                head = heads[self.source.ref]
 
396
                head = heads[self.source.name]
396
397
                self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
397
398
                    head)
398
399
            if self.target.repository.has_revision(self._last_revid):
399
400
                return []
400
401
            return [head]
401
 
        pack_hint, head = interrepo.fetch_objects(
 
402
        _, head = interrepo.fetch_objects(
402
403
            determine_wants, self.source.mapping, limit=limit)
403
 
        if pack_hint is not None and self.target.repository._format.pack_compresses:
404
 
            self.target.repository.pack(hint=pack_hint)
405
404
        if head is not None:
406
405
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
407
406
        if overwrite:
408
407
            prev_last_revid = None
409
408
        else:
410
409
            prev_last_revid = self.target.last_revision()
411
 
        self.target.generate_revision_history(self._last_revid,
412
 
            prev_last_revid)
 
410
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
413
411
        return head
414
412
 
415
413
    def update_revisions(self, stop_revision=None, overwrite=False,
495
493
                isinstance(target, RemoteGitBranch))
496
494
 
497
495
    def _basic_push(self, overwrite=False, stop_revision=None):
498
 
        from dulwich.protocol import ZERO_SHA
499
496
        result = GitBranchPushResult()
500
497
        result.source_branch = self.source
501
498
        result.target_branch = self.target
503
500
            stop_revision = self.source.last_revision()
504
501
        # FIXME: Check for diverged branches
505
502
        def get_changed_refs(old_refs):
506
 
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get(self.target.ref, ZERO_SHA))
507
 
            refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
 
503
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
 
504
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
508
505
            result.new_revid = stop_revision
509
506
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
510
 
                refs[tag_name_to_ref(name)] = sha
 
507
                refs["refs/tags/%s" % name] = sha
511
508
            return refs
512
509
        self.target.repository.send_pack(get_changed_refs,
513
510
            self.source.repository._git.object_store.generate_pack_contents)
550
547
        return refs, stop_revision
551
548
 
552
549
    def pull(self, stop_revision=None, overwrite=False,
553
 
        possible_transports=None, run_hooks=True,local=False):
 
550
        possible_transports=None, local=False):
554
551
        # This type of branch can't be bound.
555
552
        if local:
556
553
            raise errors.LocalRequiresBoundBranch()
582
579
 
583
580
    def push(self, overwrite=True, stop_revision=None,
584
581
             _override_hook_source_branch=None):
585
 
        from dulwich.protocol import ZERO_SHA
586
 
        result = GitBranchPushResult()
587
 
        result.source_branch = self.source
588
 
        result.target_branch = self.target
589
 
        if stop_revision is None:
590
 
            stop_revision = self.source.last_revision()
591
 
        # FIXME: Check for diverged branches
592
 
        refs = { self.target.ref: stop_revision }
593
 
        for name, revid in self.source.tags.get_tag_dict().iteritems():
594
 
            if self.source.repository.has_revision(revid):
595
 
                refs[tag_name_to_ref(name)] = revid
596
 
        old_refs = self.target.repository._git.get_refs()
597
 
        self.target.repository.fetch_refs(self.source.repository, refs)
598
 
        result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
599
 
            old_refs.get(self.target.ref, ZERO_SHA))
600
 
        result.new_revid = refs[self.target.ref]
601
 
        return result
 
582
        raise NoPushSupport()
602
583
 
603
584
    def lossy_push(self, stop_revision=None):
604
 
        return self._push(stop_revision=stop_revision, roundtrip=False)
605
 
        from dulwich.protocol import ZERO_SHA
606
585
        result = GitBranchPushResult()
607
586
        result.source_branch = self.source
608
587
        result.target_branch = self.target
 
588
        try:
 
589
            result.old_revid = self.target.last_revision()
 
590
        except NoSuchRef:
 
591
            result.old_revid = revision.NULL_REVISION
609
592
        if stop_revision is None:
610
593
            stop_revision = self.source.last_revision()
611
594
        # FIXME: Check for diverged branches
612
 
        refs = { self.target.ref: stop_revision }
 
595
        refs = { "refs/heads/master": stop_revision }
613
596
        for name, revid in self.source.tags.get_tag_dict().iteritems():
614
597
            if self.source.repository.has_revision(revid):
615
 
                refs[tag_name_to_ref(name)] = revid
616
 
        revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
 
598
                refs["refs/tags/%s" % name] = revid
 
599
        revidmap, new_refs = self.target.repository.dfetch_refs(
617
600
            self.source.repository, refs)
 
601
        if revidmap != {}:
 
602
            self.target.generate_revision_history(revidmap[stop_revision])
 
603
            result.new_revid = revidmap[stop_revision]
 
604
        else:
 
605
            result.new_revid = result.old_revid
618
606
        result.revidmap = revidmap
619
 
        result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
620
 
            old_refs.get(self.target.ref, ZERO_SHA))
621
 
        result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
622
 
            new_refs[self.target.ref])
623
607
        return result
624
608
 
625
609