/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 support for older versions of Dulwich.

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()."""
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:
344
367
                raise NoSuchRef(self.source.name, heads.keys())
345
368
            if stop_revision is not None:
346
369
                self._last_revid = stop_revision
347
 
                self._head, mapping = self.source.repository.lookup_git_revid(
 
370
                self._head, mapping = self.source.repository.lookup_bzr_revision_id(
348
371
                    stop_revision)
349
372
            else:
350
373
                self._head = heads[self.source.name]
445
468
        # FIXME: Check for diverged branches
446
469
        def get_changed_refs(old_refs):
447
470
            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] }
 
471
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
449
472
            result.new_revid = stop_revision
450
473
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
451
474
                refs["refs/tags/%s" % name] = sha
476
499
        return result
477
500
 
478
501
    def update_tags(self, refs):
479
 
        for name, revid in extract_tags(refs, self.target.mapping).iteritems():
 
502
        for name, v in extract_tags(refs).iteritems():
 
503
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
480
504
            self.target.tags.set_tag(name, revid)
481
505
 
482
506
    def update_refs(self, stop_revision=None):
508
532
class InterToGitBranch(branch.InterBranch):
509
533
    """InterBranch implementation that pulls from Git into bzr."""
510
534
 
 
535
    @staticmethod
 
536
    def _get_branch_formats_to_test():
 
537
        return None, None
 
538
 
511
539
    @classmethod
512
540
    def is_compatible(self, source, target):
513
541
        return (not isinstance(source, GitBranch) and 
524
552
        result = GitBranchPushResult()
525
553
        result.source_branch = self.source
526
554
        result.target_branch = self.target
527
 
        result.old_revid = self.target.last_revision()
 
555
        try:
 
556
            result.old_revid = self.target.last_revision()
 
557
        except NoSuchRef:
 
558
            result.old_revid = revision.NULL_REVISION
528
559
        if stop_revision is None:
529
560
            stop_revision = self.source.last_revision()
530
561
        # FIXME: Check for diverged branches