/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.1613 by Jelmer Vernooij
Handle encoding better in working tree iter changes.
1
# Copyright (C) 2007,2012 Canonical Ltd
2
# Copyright (C) 2009-2012 Jelmer Vernooij <jelmer@samba.org>
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
19
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
20
from __future__ import absolute_import
21
0.200.1547 by Jelmer Vernooij
Support setting branch nicks.
22
from cStringIO import StringIO
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
23
from collections import defaultdict
24
0.200.261 by Jelmer Vernooij
More formatting fixes.
25
from dulwich.objects import (
0.200.1153 by Jelmer Vernooij
Import ZERO_SHA from dulwich.objects.
26
    ZERO_SHA,
0.200.261 by Jelmer Vernooij
More formatting fixes.
27
    )
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
28
from dulwich.repo import check_ref_format
0.200.261 by Jelmer Vernooij
More formatting fixes.
29
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
30
from ... import (
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
31
    branch,
32
    config,
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
33
    controldir,
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
34
    errors,
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
35
    lock,
0.200.1097 by Jelmer Vernooij
Implement GitBranchFormat.initialize.
36
    repository as _mod_repository,
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
37
    revision,
0.200.82 by Jelmer Vernooij
Support listing tags.
38
    tag,
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
39
    transport,
0.200.1414 by Jelmer Vernooij
Fix pulling into bound branches.
40
    urlutils,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
41
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
42
from ...revision import (
0.200.969 by Jelmer Vernooij
Use tuples with bzr revid and git sha to avoid lookups.
43
    NULL_REVISION,
44
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
45
from ...trace import (
0.200.342 by Jelmer Vernooij
Report git sha during pull.
46
    is_quiet,
0.200.261 by Jelmer Vernooij
More formatting fixes.
47
    mutter,
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
48
    warning,
0.200.261 by Jelmer Vernooij
More formatting fixes.
49
    )
50
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
51
from .config import (
0.200.386 by Jelmer Vernooij
Move config to a separate file, support BranchConfig.username().
52
    GitBranchConfig,
0.200.1472 by Jelmer Vernooij
Provide basic implementation of Branch.get_config_stack.
53
    GitBranchStack,
0.200.386 by Jelmer Vernooij
Move config to a separate file, support BranchConfig.username().
54
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
55
from .errors import (
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
56
    NoPushSupport,
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
57
    NoSuchRef,
58
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
59
from .refs import (
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
60
    is_tag,
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
61
    ref_to_branch_name,
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
62
    ref_to_tag_name,
0.200.875 by Jelmer Vernooij
Use new tag_name_to_ref function.
63
    tag_name_to_ref,
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
64
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
65
from .unpeel_map import (
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
66
    UnpeelMap,
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
67
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
68
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
69
from ...foreign import ForeignBranch
0.200.388 by Jelmer Vernooij
Support bzr 1.14 as well.
70
0.200.261 by Jelmer Vernooij
More formatting fixes.
71
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
72
class GitPullResult(branch.PullResult):
0.200.956 by Jelmer Vernooij
Add some more format tests.
73
    """Result of a pull from a Git branch."""
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
74
75
    def _lookup_revno(self, revid):
76
        assert isinstance(revid, str), "was %r" % revid
77
        # Try in source branch first, it'll be faster
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
78
        with self.target_branch.lock_read():
0.200.1362 by Jelmer Vernooij
Add locking.
79
            return self.target_branch.revision_id_to_revno(revid)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
80
81
    @property
82
    def old_revno(self):
83
        return self._lookup_revno(self.old_revid)
84
85
    @property
86
    def new_revno(self):
87
        return self._lookup_revno(self.new_revid)
88
89
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
90
class GitTags(tag.BasicTags):
91
    """Ref-based tag dictionary."""
0.200.82 by Jelmer Vernooij
Support listing tags.
92
0.200.89 by Jelmer Vernooij
Support sprouting branches.
93
    def __init__(self, branch):
94
        self.branch = branch
95
        self.repository = branch.repository
0.200.82 by Jelmer Vernooij
Support listing tags.
96
0.200.1487 by Jelmer Vernooij
Use peeling.
97
    def get_refs_container(self):
98
        raise NotImplementedError(self.get_refs_container)
0.200.1066 by Jelmer Vernooij
Add GitTags.get_refs.
99
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
100
    def _iter_tag_refs(self, refs):
0.200.1487 by Jelmer Vernooij
Use peeling.
101
        """Iterate over the tag refs.
102
103
        :param refs: Refs dictionary (name -> git sha1)
104
        :return: iterator over (name, peeled_sha1, unpeeled_sha1, bzr_revid)
105
        """
106
        for k, unpeeled in refs.as_dict().iteritems():
107
            try:
108
                tag_name = ref_to_tag_name(k)
109
            except (ValueError, UnicodeDecodeError):
110
                continue
111
            peeled = refs.get_peeled(k)
112
            if peeled is None:
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
113
                peeled = self.repository.controldir._git.object_store.peel_sha(unpeeled).id
0.200.1580 by Jelmer Vernooij
Add assertion.
114
            assert type(tag_name) is unicode
0.200.1487 by Jelmer Vernooij
Use peeling.
115
            yield (tag_name, peeled, unpeeled,
116
                   self.branch.lookup_foreign_revision_id(peeled))
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
117
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
118
    def _merge_to_remote_git(self, target_repo, new_refs, overwrite=False):
119
        updates = {}
120
        conflicts = []
121
        def get_changed_refs(old_refs):
122
            ret = dict(old_refs)
123
            for k, v in new_refs.iteritems():
0.200.1402 by Jelmer Vernooij
Cope with tag changes in bzr.
124
                if not is_tag(k):
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
125
                    continue
126
                name = ref_to_tag_name(k)
127
                if old_refs.get(k) == v:
128
                    pass
129
                elif overwrite or not k in old_refs:
130
                    ret[k] = v
131
                    updates[name] = target_repo.lookup_foreign_revision_id(v)
132
                else:
133
                    conflicts.append((name, v, old_refs[k]))
134
            return ret
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
135
        target_repo.controldir.send_pack(get_changed_refs, lambda have, want: [])
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
136
        return updates, conflicts
137
138
    def _merge_to_local_git(self, target_repo, refs, overwrite=False):
139
        conflicts = []
140
        updates = {}
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
141
        for k, unpeeled in refs.as_dict().iteritems():
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
142
            if not is_tag(k):
143
                continue
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
144
            name = ref_to_tag_name(k)
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
145
            peeled = self.repository.controldir.get_peeled(k)
0.200.1489 by Jelmer Vernooij
More fixes to peel handling.
146
            if target_repo._git.refs.get(k) == unpeeled:
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
147
                pass
148
            elif overwrite or not k in target_repo._git.refs:
0.200.1460 by Jelmer Vernooij
Improve pulling into local git branches.
149
                target_repo._git.refs[k] = unpeeled or peeled
150
                updates[name] = target_repo.lookup_foreign_revision_id(peeled)
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
151
            else:
0.200.1493 by Jelmer Vernooij
Test fixes.
152
                conflicts.append((name, peeled, target_repo._git.refs[k]))
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
153
        return updates, conflicts
154
155
    def _merge_to_git(self, to_tags, refs, overwrite=False):
156
        target_repo = to_tags.repository
157
        if self.repository.has_same_location(target_repo):
158
            return {}, []
159
        if getattr(target_repo, "_git", None):
160
            return self._merge_to_local_git(target_repo, refs, overwrite)
161
        else:
162
            return self._merge_to_remote_git(target_repo, refs, overwrite)
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
163
164
    def _merge_to_non_git(self, to_tags, refs, overwrite=False):
165
        unpeeled_map = defaultdict(set)
166
        conflicts = []
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
167
        updates = {}
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
168
        result = dict(to_tags.get_tag_dict())
169
        for n, peeled, unpeeled, bzr_revid in self._iter_tag_refs(refs):
170
            if unpeeled is not None:
171
                unpeeled_map[peeled].add(unpeeled)
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
172
            if result.get(n) == bzr_revid:
173
                pass
174
            elif n not in result or overwrite:
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
175
                result[n] = bzr_revid
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
176
                updates[n] = bzr_revid
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
177
            else:
178
                conflicts.append((n, result[n], bzr_revid))
179
        to_tags._set_tag_dict(result)
180
        if len(unpeeled_map) > 0:
181
            map_file = UnpeelMap.from_repository(to_tags.branch.repository)
182
            map_file.update(unpeeled_map)
183
            map_file.save_in_repository(to_tags.branch.repository)
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
184
        return updates, conflicts
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
185
186
    def merge_to(self, to_tags, overwrite=False, ignore_master=False,
187
                 source_refs=None):
0.200.1113 by Jelmer Vernooij
Fix Tags.merge_to.
188
        """See Tags.merge_to."""
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
189
        if source_refs is None:
0.200.1487 by Jelmer Vernooij
Use peeling.
190
            source_refs = self.get_refs_container()
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
191
        if self == to_tags:
0.200.1402 by Jelmer Vernooij
Cope with tag changes in bzr.
192
            return {}, []
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
193
        if isinstance(to_tags, GitTags):
194
            return self._merge_to_git(to_tags, source_refs,
195
                                      overwrite=overwrite)
196
        else:
197
            if ignore_master:
198
                master = None
199
            else:
200
                master = to_tags.branch.get_master_branch()
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
201
            updates, conflicts = self._merge_to_non_git(to_tags, source_refs,
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
202
                                              overwrite=overwrite)
203
            if master is not None:
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
204
                extra_updates, extra_conflicts = self.merge_to(
205
                    master.tags, overwrite=overwrite,
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
206
                                           source_refs=source_refs,
207
                                           ignore_master=ignore_master)
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
208
                updates.update(extra_updates)
209
                conflicts += extra_conflicts
210
            return updates, conflicts
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
211
212
    def get_tag_dict(self):
213
        ret = {}
0.200.1487 by Jelmer Vernooij
Use peeling.
214
        refs = self.get_refs_container()
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
215
        for (name, peeled, unpeeled, bzr_revid) in self._iter_tag_refs(refs):
216
            ret[name] = bzr_revid
217
        return ret
218
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
219
220
class LocalGitTagDict(GitTags):
221
    """Dictionary with tags in a local repository."""
222
223
    def __init__(self, branch):
224
        super(LocalGitTagDict, self).__init__(branch)
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
225
        self.refs = self.repository.controldir._git.refs
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
226
0.200.1487 by Jelmer Vernooij
Use peeling.
227
    def get_refs_container(self):
228
        return self.refs
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
229
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
230
    def _set_tag_dict(self, to_dict):
0.200.1487 by Jelmer Vernooij
Use peeling.
231
        extra = set(self.refs.allkeys())
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
232
        for k, revid in to_dict.iteritems():
0.200.875 by Jelmer Vernooij
Use new tag_name_to_ref function.
233
            name = tag_name_to_ref(k)
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
234
            if name in extra:
235
                extra.remove(name)
236
            self.set_tag(k, revid)
237
        for name in extra:
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
238
            if is_tag(name):
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
239
                del self.repository._git[name]
0.200.956 by Jelmer Vernooij
Add some more format tests.
240
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
241
    def set_tag(self, name, revid):
0.200.1369 by Jelmer Vernooij
Clarify that ghost tags are not supported.
242
        try:
243
            git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
244
        except errors.NoSuchRevision:
245
            raise errors.GhostTagsNotSupported(self)
246
        self.refs[tag_name_to_ref(name)] = git_sha
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
247
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
248
0.200.1078 by Jelmer Vernooij
Fix git-import from remote repositories.
249
class DictTagDict(tag.BasicTags):
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
250
251
    def __init__(self, branch, tags):
252
        super(DictTagDict, self).__init__(branch)
253
        self._tags = tags
254
255
    def get_tag_dict(self):
256
        return self._tags
257
258
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
259
class GitSymrefBranchFormat(branch.BranchFormat):
260
261
    def get_format_description(self):
262
        return 'Git Symbolic Reference Branch'
263
264
    def network_name(self):
265
        return "git"
266
267
    def get_reference(self, controldir, name=None):
268
        return controldir.get_branch_reference(name)
269
270
    def set_reference(self, controldir, name, target):
0.200.1559 by Jelmer Vernooij
Fix compatibility with bzr 2.5.
271
        return controldir.set_branch_reference(target, name)
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
272
273
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
274
class GitBranchFormat(branch.BranchFormat):
275
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
276
    def get_format_description(self):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
277
        return 'Git Branch'
278
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
279
    def network_name(self):
280
        return "git"
281
0.200.82 by Jelmer Vernooij
Support listing tags.
282
    def supports_tags(self):
283
        return True
284
0.200.1105 by Jelmer Vernooij
Don't claim to support leaving locks.
285
    def supports_leaving_lock(self):
286
        return False
287
0.200.1369 by Jelmer Vernooij
Clarify that ghost tags are not supported.
288
    def supports_tags_referencing_ghosts(self):
289
        return False
290
291
    def tags_are_versioned(self):
292
        return False
293
0.200.1091 by Jelmer Vernooij
Provide _matchingbzrdir for testing.
294
    @property
0.200.1665 by Jelmer Vernooij
Rename _matchingbzrdir to _matchingcnotroldir.
295
    def _matchingcontroldir(self):
0.200.1644 by Jelmer Vernooij
More relative imports.
296
        from .dir import LocalGitControlDirFormat
0.200.1091 by Jelmer Vernooij
Provide _matchingbzrdir for testing.
297
        return LocalGitControlDirFormat()
298
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
299
    def get_foreign_tests_branch_factory(self):
0.200.1644 by Jelmer Vernooij
More relative imports.
300
        from .tests.test_branch import ForeignTestsBranchFactory
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
301
        return ForeignTestsBranchFactory()
302
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
303
    def make_tags(self, branch):
0.200.1487 by Jelmer Vernooij
Use peeling.
304
        try:
305
            return branch.tags
306
        except AttributeError:
307
            pass
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
308
        if getattr(branch.repository, "_git", None) is None:
0.200.1644 by Jelmer Vernooij
More relative imports.
309
            from .remote import RemoteGitTagDict
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
310
            return RemoteGitTagDict(branch)
0.200.261 by Jelmer Vernooij
More formatting fixes.
311
        else:
312
            return LocalGitTagDict(branch)
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
313
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
314
    def initialize(self, a_controldir, name=None, repository=None,
0.200.1378 by Jelmer Vernooij
Fix branch.
315
                   append_revisions_only=None):
0.200.1644 by Jelmer Vernooij
More relative imports.
316
        from .dir import LocalGitDir
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
317
        if not isinstance(a_controldir, LocalGitDir):
318
            raise errors.IncompatibleFormat(self, a_controldir._format)
319
        return a_controldir.create_branch(repository=repository, name=name,
0.200.1378 by Jelmer Vernooij
Fix branch.
320
            append_revisions_only=append_revisions_only)
0.200.1097 by Jelmer Vernooij
Implement GitBranchFormat.initialize.
321
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
322
0.200.388 by Jelmer Vernooij
Support bzr 1.14 as well.
323
class GitBranch(ForeignBranch):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
324
    """An adapter to git repositories for bzr Branch objects."""
325
0.200.1129 by Jelmer Vernooij
Implement GitBranch.control_transport.
326
    @property
327
    def control_transport(self):
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
328
        return self.controldir.control_transport
0.200.1129 by Jelmer Vernooij
Implement GitBranch.control_transport.
329
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
330
    def __init__(self, controldir, repository, ref):
331
        self.base = controldir.root_transport.base
0.200.82 by Jelmer Vernooij
Support listing tags.
332
        self.repository = repository
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
333
        self._format = GitBranchFormat()
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
334
        self.controldir = controldir
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
335
        self._lock_mode = None
336
        self._lock_count = 0
0.231.1 by Jelmer Vernooij
Check that regenerated objects have the expected sha1.
337
        super(GitBranch, self).__init__(repository.get_mapping())
0.200.770 by Jelmer Vernooij
Proper branch names.
338
        self.ref = ref
0.200.1361 by Jelmer Vernooij
Support branches where the ref can't be mapped back to a branch name.
339
        try:
340
            self.name = ref_to_branch_name(ref)
341
        except ValueError:
342
            self.name = None
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
343
        self._head = None
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
344
0.200.1360 by Jelmer Vernooij
Support lighweight argument to _get_checkout_format.
345
    def _get_checkout_format(self, lightweight=False):
0.239.8 by Jelmer Vernooij
Support checkouts.
346
        """Return the most suitable metadir for a checkout of this branch.
347
        Weaves are used if this branch's repository uses weaves.
348
        """
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
349
        return controldir.format_registry.make_controldir("default")
0.239.8 by Jelmer Vernooij
Support checkouts.
350
0.238.3 by Jelmer Vernooij
Remove svn references, prefer git send format when submitting changes against a git branch.
351
    def get_child_submit_format(self):
352
        """Return the preferred format of submissions to this branch."""
0.200.1584 by Jelmer Vernooij
Use config stacks in a few more places.
353
        ret = self.get_config_stack().get("child_submit_format")
0.238.3 by Jelmer Vernooij
Remove svn references, prefer git send format when submitting changes against a git branch.
354
        if ret is not None:
355
            return ret
356
        return "git"
357
0.200.1397 by Jelmer Vernooij
Fix use of get_config() for RemoteGitBranch.
358
    def get_config(self):
359
        return GitBranchConfig(self)
360
0.200.1472 by Jelmer Vernooij
Provide basic implementation of Branch.get_config_stack.
361
    def get_config_stack(self):
362
        return GitBranchStack(self)
363
0.200.293 by Jelmer Vernooij
Fix branch nicks.
364
    def _get_nick(self, local=False, possible_master_transports=None):
365
        """Find the nick name for this branch.
366
367
        :return: Branch nick
368
        """
0.200.1547 by Jelmer Vernooij
Support setting branch nicks.
369
        cs = self.repository._git.get_config_stack()
370
        try:
371
            return cs.get(("branch", self.name), "nick")
372
        except KeyError:
373
            pass
0.200.1534 by Jelmer Vernooij
Implement fetch between git branches, encode nicks.
374
        return self.name.encode('utf-8') or "HEAD"
0.200.293 by Jelmer Vernooij
Fix branch nicks.
375
0.200.331 by Jelmer Vernooij
Add stub for setting nick function.
376
    def _set_nick(self, nick):
0.200.1547 by Jelmer Vernooij
Support setting branch nicks.
377
        cf = self.repository._git.get_config()
378
        cf.set(("branch", self.name), "nick", nick)
379
        f = StringIO()
380
        cf.write_to_file(f)
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
381
        self.controldir.control_transport.put_bytes('config', f.getvalue())
0.200.331 by Jelmer Vernooij
Add stub for setting nick function.
382
383
    nick = property(_get_nick, _set_nick)
0.200.293 by Jelmer Vernooij
Fix branch nicks.
384
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
385
    def __repr__(self):
0.200.770 by Jelmer Vernooij
Proper branch names.
386
        return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
0.200.1311 by Jelmer Vernooij
More work on colocated branch support.
387
            self.name)
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
388
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
389
    def generate_revision_history(self, revid, old_revid=None):
0.200.1103 by Jelmer Vernooij
Support generate_revision_history(NULL_REVISION).
390
        if revid == NULL_REVISION:
391
            newhead = ZERO_SHA
392
        else:
393
            # FIXME: Check that old_revid is in the ancestry of revid
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
394
            newhead, self.mapping = self.repository.lookup_bzr_revision_id(revid)
0.200.1218 by Jelmer Vernooij
Support set_last_revision('null:').
395
            if self.mapping is None:
396
                raise AssertionError
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
397
        self._set_head(newhead)
398
0.200.1199 by Jelmer Vernooij
Support 'token' argument to Branch.lock_write.
399
    def lock_write(self, token=None):
400
        if token is not None:
401
            raise errors.TokenLockingNotSupported(self)
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
402
        if self._lock_mode:
0.200.1369 by Jelmer Vernooij
Clarify that ghost tags are not supported.
403
            if self._lock_mode == 'r':
404
                raise errors.ReadOnlyError(self)
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
405
            self._lock_count += 1
406
        else:
407
            self._lock_mode = 'w'
408
            self._lock_count = 1
409
        self.repository.lock_write()
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
410
        return lock.LogicalLockResult(self.unlock)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
411
0.200.1453 by Jelmer Vernooij
Provide Branch.leave_lock_in_place and Branch.dont_leave_lock_in_place.
412
    def leave_lock_in_place(self):
413
        raise NotImplementedError(self.leave_lock_in_place)
414
415
    def dont_leave_lock_in_place(self):
416
        raise NotImplementedError(self.dont_leave_lock_in_place)
417
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
418
    def get_stacked_on_url(self):
419
        # Git doesn't do stacking (yet...)
0.200.1660 by Jelmer Vernooij
Fix imports.
420
        raise branch.UnstackableBranchFormat(self._format, self.base)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
421
422
    def get_parent(self):
423
        """See Branch.get_parent()."""
0.200.312 by Jelmer Vernooij
Add notes about parent locations.
424
        # FIXME: Set "origin" url from .git/config ?
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
425
        return None
426
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
427
    def set_parent(self, url):
0.200.312 by Jelmer Vernooij
Add notes about parent locations.
428
        # FIXME: Set "origin" url in .git/config ?
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
429
        pass
430
0.200.1411 by Jelmer Vernooij
Fix control files.
431
    def break_lock(self):
432
        raise NotImplementedError(self.break_lock)
433
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
434
    def lock_read(self):
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
435
        if self._lock_mode:
436
            assert self._lock_mode in ('r', 'w')
437
            self._lock_count += 1
438
        else:
439
            self._lock_mode = 'r'
440
            self._lock_count = 1
441
        self.repository.lock_read()
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
442
        return lock.LogicalLockResult(self.unlock)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
443
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
444
    def peek_lock_mode(self):
445
        return self._lock_mode
446
0.200.432 by Jelmer Vernooij
Support Branch.is_locked, required for loggerhead.
447
    def is_locked(self):
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
448
        return (self._lock_mode is not None)
0.200.432 by Jelmer Vernooij
Support Branch.is_locked, required for loggerhead.
449
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
450
    def unlock(self):
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
451
        """See Branch.unlock()."""
452
        self._lock_count -= 1
453
        if self._lock_count == 0:
454
            self._lock_mode = None
455
            self._clear_cached_state()
456
        self.repository.unlock()
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
457
458
    def get_physical_lock_status(self):
459
        return False
460
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
461
    def last_revision(self):
0.200.1675 by Jelmer Vernooij
Remove uses of decorators.
462
        with self.lock_read():
463
            # perhaps should escape this ?
464
            if self.head is None:
465
                return revision.NULL_REVISION
466
            return self.lookup_foreign_revision_id(self.head)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
467
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
468
    def _basic_push(self, target, overwrite=False, stop_revision=None):
469
        return branch.InterBranch.get(self, target)._basic_push(
470
            overwrite, stop_revision)
471
0.252.49 by Jelmer Vernooij
Avoid trying to set HEAD for remote branches.
472
    def lookup_foreign_revision_id(self, foreign_revid):
0.200.956 by Jelmer Vernooij
Add some more format tests.
473
        return self.repository.lookup_foreign_revision_id(foreign_revid,
0.252.49 by Jelmer Vernooij
Avoid trying to set HEAD for remote branches.
474
            self.mapping)
475
0.200.1030 by Jelmer Vernooij
More work on supporting roundtripping push.
476
    def lookup_bzr_revision_id(self, revid):
477
        return self.repository.lookup_bzr_revision_id(
478
            revid, mapping=self.mapping)
479
0.200.1678 by Jelmer Vernooij
Fix tests.
480
    def get_unshelver(self, tree):
481
        raise errors.StoringUncommittedNotSupported(self)
482
0.200.692 by Jelmer Vernooij
Refuse pulling into non-rich-root branches rather than erroring out with an AttributeError.
483
0.200.465 by Jelmer Vernooij
Use dulwich standard functionality for finding missing revisions.
484
class LocalGitBranch(GitBranch):
485
    """A local Git branch."""
486
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
487
    def __init__(self, controldir, repository, ref):
488
        super(LocalGitBranch, self).__init__(controldir, repository, ref)
489
        refs = controldir.get_refs_container()
0.200.1487 by Jelmer Vernooij
Use peeling.
490
        if not (ref in refs or "HEAD" in refs):
0.200.763 by Jelmer Vernooij
Provide proper colocated branch support.
491
            raise errors.NotBranchError(self.base)
492
0.200.261 by Jelmer Vernooij
More formatting fixes.
493
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
494
        accelerator_tree=None, hardlink=False):
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
495
        if lightweight:
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
496
            t = transport.get_transport(to_location)
497
            t.ensure_base()
0.200.1360 by Jelmer Vernooij
Support lighweight argument to _get_checkout_format.
498
            format = self._get_checkout_format(lightweight=True)
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
499
            checkout = format.initialize_on_transport(t)
0.200.1662 by Jelmer Vernooij
Fix import.
500
            from breezy.bzr.branch import BranchReferenceFormat
501
            from_branch = BranchReferenceFormat().initialize(checkout, self)
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
502
            tree = checkout.create_workingtree(revision_id,
503
                from_branch=from_branch, hardlink=hardlink)
504
            return tree
505
        else:
506
            return self._create_heavyweight_checkout(to_location, revision_id,
0.257.1 by Jelmer Vernooij
use transport repo objects even for local access.
507
                hardlink)
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
508
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
509
    def _create_heavyweight_checkout(self, to_location, revision_id=None,
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
510
                                     hardlink=False):
511
        """Create a new heavyweight checkout of this branch.
512
513
        :param to_location: URL of location to create the new checkout in.
514
        :param revision_id: Revision that should be the tip of the checkout.
515
        :param hardlink: Whether to hardlink
516
        :return: WorkingTree object of checkout.
517
        """
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
518
        checkout_branch = controldir.ControlDir.create_branch_convenience(
0.200.1360 by Jelmer Vernooij
Support lighweight argument to _get_checkout_format.
519
            to_location, force_new_tree=False,
520
            format=self._get_checkout_format(lightweight=False))
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
521
        checkout = checkout_branch.controldir
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
522
        checkout_branch.bind(self)
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
523
        # pull up to the specified revision_id to set the initial
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
524
        # branch tip correctly, and seed it with history.
525
        checkout_branch.pull(self, stop_revision=revision_id)
526
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
527
0.200.1493 by Jelmer Vernooij
Test fixes.
528
    def fetch(self, from_branch, last_revision=None, limit=None):
529
        return branch.InterBranch.get(from_branch, self).fetch(
530
            stop_revision=last_revision, limit=limit)
531
0.200.57 by Jelmer Vernooij
Fix more tests.
532
    def _gen_revision_history(self):
0.200.58 by Jelmer Vernooij
Fix remaining tests.
533
        if self.head is None:
534
            return []
0.200.1279 by Jelmer Vernooij
Avoid using deprecated Repository.iter_reverse_revision_history.
535
        graph = self.repository.get_graph()
536
        ret = list(graph.iter_lefthand_ancestry(self.last_revision(),
537
            (revision.NULL_REVISION, )))
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
538
        ret.reverse()
0.200.57 by Jelmer Vernooij
Fix more tests.
539
        return ret
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
540
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
541
    def _get_head(self):
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
542
        try:
0.280.1 by Martin Pitt
Fix deprecated Repo.ref(), to make testsuite succeed again.
543
            return self.repository._git.refs[self.ref or "HEAD"]
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
544
        except KeyError:
545
            return None
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
546
0.200.1228 by Jelmer Vernooij
Provide Branch._read_last_revision_info.
547
    def _read_last_revision_info(self):
548
        last_revid = self.last_revision()
549
        graph = self.repository.get_graph()
550
        revno = graph.find_distance_to_null(last_revid,
551
            [(revision.NULL_REVISION, 0)])
552
        return revno, last_revid
553
554
    def set_last_revision_info(self, revno, revision_id):
555
        self.set_last_revision(revision_id)
556
        self._last_revision_info_cache = revno, revision_id
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
557
558
    def set_last_revision(self, revid):
0.200.1233 by Jelmer Vernooij
Implement Repository.iter_files_bytes.
559
        if not revid or not isinstance(revid, basestring):
560
            raise errors.InvalidRevisionId(revision_id=revid, branch=self)
0.200.1218 by Jelmer Vernooij
Support set_last_revision('null:').
561
        if revid == NULL_REVISION:
562
            newhead = ZERO_SHA
563
        else:
564
            (newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
565
            if self.mapping is None:
566
                raise AssertionError
567
        self._set_head(newhead)
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
568
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
569
    def _set_head(self, value):
570
        self._head = value
0.200.918 by Jelmer Vernooij
Cope with 'self.ref is None' in a couple more places.
571
        self.repository._git.refs[self.ref or "HEAD"] = self._head
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
572
        self._clear_cached_state()
573
574
    head = property(_get_head, _set_head)
575
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
576
    def get_push_location(self):
577
        """See Branch.get_push_location."""
0.200.1584 by Jelmer Vernooij
Use config stacks in a few more places.
578
        push_loc = self.get_config_stack().get('push_location')
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
579
        return push_loc
580
581
    def set_push_location(self, location):
582
        """See Branch.set_push_location."""
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
583
        self.get_config().set_user_option('push_location', location,
0.217.54 by John Carr
set_user_option breaks - doesnt have a local option in BranchConfig. Follow the bzr.dev syntax instead.
584
                                          store=config.STORE_LOCATION)
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
585
586
    def supports_tags(self):
0.200.82 by Jelmer Vernooij
Support listing tags.
587
        return True
0.200.956 by Jelmer Vernooij
Add some more format tests.
588
0.200.1681 by Jelmer Vernooij
Provide Branch.store_uncommitted.
589
    def store_uncommitted(self, creator):
590
        raise errors.StoringUncommittedNotSupported(self)
591
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
592
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
593
def _quick_lookup_revno(local_branch, remote_branch, revid):
594
    assert isinstance(revid, str), "was %r" % revid
595
    # Try in source branch first, it'll be faster
0.200.1362 by Jelmer Vernooij
Add locking.
596
    local_branch.lock_read()
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
597
    try:
598
        try:
0.200.1362 by Jelmer Vernooij
Add locking.
599
            return local_branch.revision_id_to_revno(revid)
600
        except errors.NoSuchRevision:
601
            graph = local_branch.repository.get_graph()
602
            try:
603
                return graph.find_distance_to_null(revid,
604
                    [(revision.NULL_REVISION, 0)])
605
            except errors.GhostRevisionsHaveNoRevno:
606
                # FIXME: Check using graph.find_distance_to_null() ?
607
                remote_branch.lock_read()
608
                try:
609
                    return remote_branch.revision_id_to_revno(revid)
610
                finally:
611
                    remote_branch.unlock()
612
    finally:
613
        local_branch.unlock()
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
614
615
0.200.342 by Jelmer Vernooij
Report git sha during pull.
616
class GitBranchPullResult(branch.PullResult):
617
0.252.36 by Jelmer Vernooij
Fix pull.
618
    def __init__(self):
619
        super(GitBranchPullResult, self).__init__()
620
        self.new_git_head = None
621
        self._old_revno = None
622
        self._new_revno = None
623
0.200.342 by Jelmer Vernooij
Report git sha during pull.
624
    def report(self, to_file):
625
        if not is_quiet():
626
            if self.old_revid == self.new_revid:
627
                to_file.write('No revisions to pull.\n')
0.200.728 by Jelmer Vernooij
Fix pulling when all revisions are already in the repo.
628
            elif self.new_git_head is not None:
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
629
                to_file.write('Now on revision %d (git sha: %s).\n' %
0.200.342 by Jelmer Vernooij
Report git sha during pull.
630
                        (self.new_revno, self.new_git_head))
0.200.728 by Jelmer Vernooij
Fix pulling when all revisions are already in the repo.
631
            else:
632
                to_file.write('Now on revision %d.\n' % (self.new_revno,))
0.200.342 by Jelmer Vernooij
Report git sha during pull.
633
        self._show_tag_conficts(to_file)
634
0.252.36 by Jelmer Vernooij
Fix pull.
635
    def _lookup_revno(self, revid):
0.200.1185 by Jelmer Vernooij
Some formatting fixes.
636
        return _quick_lookup_revno(self.target_branch, self.source_branch,
0.200.1513 by Jelmer Vernooij
Cope with zero shas.
637
            revid)
0.252.36 by Jelmer Vernooij
Fix pull.
638
639
    def _get_old_revno(self):
640
        if self._old_revno is not None:
641
            return self._old_revno
642
        return self._lookup_revno(self.old_revid)
643
644
    def _set_old_revno(self, revno):
645
        self._old_revno = revno
646
647
    old_revno = property(_get_old_revno, _set_old_revno)
648
649
    def _get_new_revno(self):
650
        if self._new_revno is not None:
651
            return self._new_revno
652
        return self._lookup_revno(self.new_revid)
653
654
    def _set_new_revno(self, revno):
655
        self._new_revno = revno
0.200.956 by Jelmer Vernooij
Add some more format tests.
656
0.252.36 by Jelmer Vernooij
Fix pull.
657
    new_revno = property(_get_new_revno, _set_new_revno)
658
0.200.342 by Jelmer Vernooij
Report git sha during pull.
659
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
660
class GitBranchPushResult(branch.BranchPushResult):
661
662
    def _lookup_revno(self, revid):
0.200.1185 by Jelmer Vernooij
Some formatting fixes.
663
        return _quick_lookup_revno(self.source_branch, self.target_branch,
664
            revid)
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
665
666
    @property
667
    def old_revno(self):
668
        return self._lookup_revno(self.old_revid)
669
670
    @property
671
    def new_revno(self):
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
672
        new_original_revno = getattr(self, "new_original_revno", None)
673
        if new_original_revno:
674
            return new_original_revno
675
        if getattr(self, "new_original_revid", None) is not None:
676
            return self._lookup_revno(self.new_original_revid)
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
677
        return self._lookup_revno(self.new_revid)
678
679
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
680
class InterFromGitBranch(branch.GenericInterBranch):
0.200.261 by Jelmer Vernooij
More formatting fixes.
681
    """InterBranch implementation that pulls from Git into bzr."""
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
682
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
683
    @staticmethod
684
    def _get_branch_formats_to_test():
0.200.1100 by Jelmer Vernooij
Provide test combinations for InterBranch implementations.
685
        try:
686
            default_format = branch.format_registry.get_default()
687
        except AttributeError:
688
            default_format = branch.BranchFormat._default_format
689
        return [
690
            (GitBranchFormat(), GitBranchFormat()),
691
            (GitBranchFormat(), default_format)]
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
692
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
693
    @classmethod
0.200.692 by Jelmer Vernooij
Refuse pulling into non-rich-root branches rather than erroring out with an AttributeError.
694
    def _get_interrepo(self, source, target):
0.200.1097 by Jelmer Vernooij
Implement GitBranchFormat.initialize.
695
        return _mod_repository.InterRepository.get(source.repository, target.repository)
0.200.692 by Jelmer Vernooij
Refuse pulling into non-rich-root branches rather than erroring out with an AttributeError.
696
697
    @classmethod
698
    def is_compatible(cls, source, target):
0.200.1222 by Jelmer Vernooij
Better checks in is_compatible methods.
699
        if not isinstance(source, GitBranch):
700
            return False
701
        if isinstance(target, GitBranch):
702
            # InterLocalGitRemoteGitBranch or InterToGitBranch should be used
703
            return False
704
        if getattr(cls._get_interrepo(source, target), "fetch_objects", None) is None:
705
            # fetch_objects is necessary for this to work
706
            return False
707
        return True
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
708
0.200.1305 by Jelmer Vernooij
Only actually fetch tags if "branch.fetch_tags" is set to true.
709
    def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
0.200.1265 by Jelmer Vernooij
Support limit option to Branch.fetch.
710
        self.fetch_objects(stop_revision, fetch_tags=fetch_tags, limit=limit)
0.260.1 by Jelmer Vernooij
Fix fetch from remote during merge.
711
0.200.1265 by Jelmer Vernooij
Support limit option to Branch.fetch.
712
    def fetch_objects(self, stop_revision, fetch_tags, limit=None):
0.200.692 by Jelmer Vernooij
Refuse pulling into non-rich-root branches rather than erroring out with an AttributeError.
713
        interrepo = self._get_interrepo(self.source, self.target)
0.200.1305 by Jelmer Vernooij
Only actually fetch tags if "branch.fetch_tags" is set to true.
714
        if fetch_tags is None:
0.200.1584 by Jelmer Vernooij
Use config stacks in a few more places.
715
            c = self.source.get_config_stack()
716
            fetch_tags = c.get('branch.fetch_tags')
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
717
        def determine_wants(heads):
0.200.917 by Jelmer Vernooij
Cope with implicit branches during pull.
718
            if self.source.ref is not None and not self.source.ref in heads:
0.200.1386 by Jelmer Vernooij
Friendlier message if HEAD is not found.
719
                raise NoSuchRef(self.source.ref, self.source.user_url, heads.keys())
0.259.6 by Jelmer Vernooij
Fetch tags during pull.
720
721
            if stop_revision is None:
0.200.917 by Jelmer Vernooij
Cope with implicit branches during pull.
722
                if self.source.ref is not None:
723
                    head = heads[self.source.ref]
724
                else:
725
                    head = heads["HEAD"]
0.252.44 by Jelmer Vernooij
Properly look up Bazaar revision ids for revision parents in case they are round-tripped.
726
                self._last_revid = self.source.lookup_foreign_revision_id(head)
0.259.6 by Jelmer Vernooij
Fetch tags during pull.
727
            else:
728
                self._last_revid = stop_revision
729
            real = interrepo.get_determine_wants_revids(
0.260.1 by Jelmer Vernooij
Fix fetch from remote during merge.
730
                [self._last_revid], include_tags=fetch_tags)
0.259.6 by Jelmer Vernooij
Fetch tags during pull.
731
            return real(heads)
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
732
        pack_hint, head, refs = interrepo.fetch_objects(
0.200.1265 by Jelmer Vernooij
Support limit option to Branch.fetch.
733
            determine_wants, self.source.mapping, limit=limit)
0.252.45 by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr.
734
        if (pack_hint is not None and
735
            self.target.repository._format.pack_compresses):
0.248.5 by Jelmer Vernooij
Reformatting, fix dpush.
736
            self.target.repository.pack(hint=pack_hint)
0.260.1 by Jelmer Vernooij
Fix fetch from remote during merge.
737
        return head, refs
738
0.200.1219 by Jelmer Vernooij
Remove InterBranch.update_revisions.
739
    def _update_revisions(self, stop_revision=None, overwrite=False):
0.200.1305 by Jelmer Vernooij
Only actually fetch tags if "branch.fetch_tags" is set to true.
740
        head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
741
        if overwrite:
0.200.314 by Jelmer Vernooij
Support stop_revision.
742
            prev_last_revid = None
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
743
        else:
0.200.314 by Jelmer Vernooij
Support stop_revision.
744
            prev_last_revid = self.target.last_revision()
0.248.5 by Jelmer Vernooij
Reformatting, fix dpush.
745
        self.target.generate_revision_history(self._last_revid,
0.259.6 by Jelmer Vernooij
Fetch tags during pull.
746
            prev_last_revid, self.source)
0.200.1062 by Jelmer Vernooij
Pass remote refs along in _update_revisions.
747
        return head, refs
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
748
0.200.1423 by Jelmer Vernooij
Fix space.
749
    def _basic_pull(self, stop_revision, overwrite, run_hooks,
0.200.1414 by Jelmer Vernooij
Fix pulling into bound branches.
750
              _override_hook_target, _hook_master):
0.200.342 by Jelmer Vernooij
Report git sha during pull.
751
        result = GitBranchPullResult()
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
752
        result.source_branch = self.source
753
        if _override_hook_target is None:
754
            result.target_branch = self.target
755
        else:
756
            result.target_branch = _override_hook_target
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
757
        with self.target.lock_write(), self.source.lock_read():
758
            # We assume that during 'pull' the target repository is closer than
759
            # the source one.
760
            (result.old_revno, result.old_revid) = \
761
                self.target.last_revision_info()
762
            result.new_git_head, remote_refs = self._update_revisions(
763
                stop_revision, overwrite=overwrite)
764
            tags_ret  = self.source.tags.merge_to(
765
                    self.target.tags, overwrite, ignore_master=True)
766
            if isinstance(tags_ret, tuple):
767
                result.tag_updates, result.tag_conflicts = tags_ret
768
            else:
769
                result.tag_conflicts = tags_ret
770
            (result.new_revno, result.new_revid) = \
771
                self.target.last_revision_info()
772
            if _hook_master:
773
                result.master_branch = _hook_master
774
                result.local_branch = result.target_branch
775
            else:
776
                result.master_branch = result.target_branch
777
                result.local_branch = None
778
            if run_hooks:
779
                for hook in branch.Branch.hooks['post_pull']:
780
                    hook(result)
781
            return result
0.200.1414 by Jelmer Vernooij
Fix pulling into bound branches.
782
783
    def pull(self, overwrite=False, stop_revision=None,
784
             possible_transports=None, _hook_master=None, run_hooks=True,
785
             _override_hook_target=None, local=False):
786
        """See Branch.pull.
787
788
        :param _hook_master: Private parameter - set the branch to
789
            be supplied as the master to pull hooks.
790
        :param run_hooks: Private parameter - if false, this branch
791
            is being called because it's the master of the primary branch,
792
            so it should not run its hooks.
793
        :param _override_hook_target: Private parameter - set the branch to be
794
            supplied as the target_branch to pull hooks.
795
        """
796
        # This type of branch can't be bound.
797
        bound_location = self.target.get_bound_location()
798
        if local and not bound_location:
799
            raise errors.LocalRequiresBoundBranch()
800
        master_branch = None
801
        source_is_master = False
802
        self.source.lock_read()
803
        if bound_location:
804
            # bound_location comes from a config file, some care has to be
805
            # taken to relate it to source.user_url
806
            normalized = urlutils.normalize_url(bound_location)
807
            try:
808
                relpath = self.source.user_transport.relpath(normalized)
809
                source_is_master = (relpath == '')
0.200.1660 by Jelmer Vernooij
Fix imports.
810
            except (errors.PathNotChild, urlutils.InvalidURL):
0.200.1414 by Jelmer Vernooij
Fix pulling into bound branches.
811
                source_is_master = False
812
        if not local and bound_location and not source_is_master:
813
            # not pulling from master, so we need to update master.
814
            master_branch = self.target.get_master_branch(possible_transports)
815
            master_branch.lock_write()
816
        try:
817
            try:
818
                if master_branch:
819
                    # pull from source into master.
820
                    master_branch.pull(self.source, overwrite, stop_revision,
821
                        run_hooks=False)
822
                result = self._basic_pull(stop_revision, overwrite, run_hooks,
823
                    _override_hook_target, _hook_master=master_branch)
824
            finally:
825
                self.source.unlock()
826
        finally:
827
            if master_branch:
828
                master_branch.unlock()
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
829
        return result
830
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
831
    def _basic_push(self, overwrite=False, stop_revision=None):
832
        result = branch.BranchPushResult()
833
        result.source_branch = self.source
834
        result.target_branch = self.target
835
        result.old_revno, result.old_revid = self.target.last_revision_info()
0.200.1219 by Jelmer Vernooij
Remove InterBranch.update_revisions.
836
        result.new_git_head, remote_refs = self._update_revisions(
837
            stop_revision, overwrite=overwrite)
0.200.1402 by Jelmer Vernooij
Cope with tag changes in bzr.
838
        tags_ret = self.source.tags.merge_to(self.target.tags,
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
839
            overwrite)
0.200.1402 by Jelmer Vernooij
Cope with tag changes in bzr.
840
        if isinstance(tags_ret, tuple):
841
            (result.tag_updates, result.tag_conflicts) = tags_ret
842
        else:
843
            result.tag_conflicts = tags_ret
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
844
        result.new_revno, result.new_revid = self.target.last_revision_info()
845
        return result
846
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
847
0.200.512 by Jelmer Vernooij
Support pushing git->git.
848
class InterGitBranch(branch.GenericInterBranch):
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
849
    """InterBranch implementation that pulls between Git branches."""
850
0.200.1493 by Jelmer Vernooij
Test fixes.
851
    def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
852
        raise NotImplementedError(self.fetch)
853
0.200.512 by Jelmer Vernooij
Support pushing git->git.
854
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
855
class InterLocalGitRemoteGitBranch(InterGitBranch):
0.200.512 by Jelmer Vernooij
Support pushing git->git.
856
    """InterBranch that copies from a local to a remote git branch."""
857
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
858
    @staticmethod
859
    def _get_branch_formats_to_test():
0.200.1100 by Jelmer Vernooij
Provide test combinations for InterBranch implementations.
860
        # FIXME
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
861
        return []
862
0.200.512 by Jelmer Vernooij
Support pushing git->git.
863
    @classmethod
864
    def is_compatible(self, source, target):
0.200.1644 by Jelmer Vernooij
More relative imports.
865
        from .remote import RemoteGitBranch
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
866
        return (isinstance(source, LocalGitBranch) and
0.200.512 by Jelmer Vernooij
Support pushing git->git.
867
                isinstance(target, RemoteGitBranch))
868
869
    def _basic_push(self, overwrite=False, stop_revision=None):
870
        result = GitBranchPushResult()
871
        result.source_branch = self.source
872
        result.target_branch = self.target
873
        if stop_revision is None:
874
            stop_revision = self.source.last_revision()
875
        # FIXME: Check for diverged branches
876
        def get_changed_refs(old_refs):
0.200.1300 by Jelmer Vernooij
Fix formatting.
877
            old_ref = old_refs.get(self.target.ref, ZERO_SHA)
878
            result.old_revid = self.target.lookup_foreign_revision_id(old_ref)
0.200.822 by Jelmer Vernooij
Fix indication of number of revisions pushed in dpush.
879
            refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
0.200.512 by Jelmer Vernooij
Support pushing git->git.
880
            result.new_revid = stop_revision
881
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
0.200.875 by Jelmer Vernooij
Use new tag_name_to_ref function.
882
                refs[tag_name_to_ref(name)] = sha
0.200.512 by Jelmer Vernooij
Support pushing git->git.
883
            return refs
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
884
        self.target.repository.send_pack(get_changed_refs,
0.200.726 by Jelmer Vernooij
Factor out conversion of branch names to refs.
885
            self.source.repository._git.object_store.generate_pack_contents)
0.200.512 by Jelmer Vernooij
Support pushing git->git.
886
        return result
887
888
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
889
class InterGitLocalGitBranch(InterGitBranch):
0.200.512 by Jelmer Vernooij
Support pushing git->git.
890
    """InterBranch that copies from a remote to a local git branch."""
891
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
892
    @staticmethod
893
    def _get_branch_formats_to_test():
0.200.1100 by Jelmer Vernooij
Provide test combinations for InterBranch implementations.
894
        # FIXME
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
895
        return []
896
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
897
    @classmethod
898
    def is_compatible(self, source, target):
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
899
        return (isinstance(source, GitBranch) and
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
900
                isinstance(target, LocalGitBranch))
901
0.200.1534 by Jelmer Vernooij
Implement fetch between git branches, encode nicks.
902
    def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
903
        interrepo = _mod_repository.InterRepository.get(self.source.repository,
904
            self.target.repository)
905
        if stop_revision is None:
906
            stop_revision = self.source.last_revision()
907
        determine_wants = interrepo.get_determine_wants_revids(
908
            [stop_revision], include_tags=fetch_tags)
909
        interrepo.fetch_objects(determine_wants, limit=limit)
910
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
911
    def _basic_push(self, overwrite=False, stop_revision=None):
0.200.1325 by Jelmer Vernooij
More test fixes.
912
        result = GitBranchPushResult()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
913
        result.source_branch = self.source
914
        result.target_branch = self.target
915
        result.old_revid = self.target.last_revision()
916
        refs, stop_revision = self.update_refs(stop_revision)
917
        self.target.generate_revision_history(stop_revision, result.old_revid)
0.200.1402 by Jelmer Vernooij
Cope with tag changes in bzr.
918
        tags_ret = self.source.tags.merge_to(self.target.tags,
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
919
            source_refs=refs, overwrite=overwrite)
0.200.1402 by Jelmer Vernooij
Cope with tag changes in bzr.
920
        if isinstance(tags_ret, tuple):
921
            (result.tag_updates, result.tag_conflicts) = tags_ret
922
        else:
923
            result.tag_conflicts = tags_ret
0.200.505 by Jelmer Vernooij
Remove duplicate code.
924
        result.new_revid = self.target.last_revision()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
925
        return result
926
927
    def update_refs(self, stop_revision=None):
0.200.1097 by Jelmer Vernooij
Implement GitBranchFormat.initialize.
928
        interrepo = _mod_repository.InterRepository.get(self.source.repository,
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
929
            self.target.repository)
930
        if stop_revision is None:
0.200.940 by Jelmer Vernooij
Avoid confusion between different fetch functions with different semantics.
931
            refs = interrepo.fetch(branches=["HEAD"])
0.252.44 by Jelmer Vernooij
Properly look up Bazaar revision ids for revision parents in case they are round-tripped.
932
            stop_revision = self.target.lookup_foreign_revision_id(refs["HEAD"])
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
933
        else:
0.200.940 by Jelmer Vernooij
Avoid confusion between different fetch functions with different semantics.
934
            refs = interrepo.fetch(revision_id=stop_revision)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
935
        return refs, stop_revision
936
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
937
    def pull(self, stop_revision=None, overwrite=False,
0.200.732 by Jelmer Vernooij
Support run_hooks argument to InterGitRemoteLocalBranch.pull().
938
        possible_transports=None, run_hooks=True,local=False):
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
939
        # This type of branch can't be bound.
940
        if local:
941
            raise errors.LocalRequiresBoundBranch()
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
942
        result = GitPullResult()
943
        result.source_branch = self.source
944
        result.target_branch = self.target
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
945
        with self.target.lock_write(), self.source.lock_read():
946
            result.old_revid = self.target.last_revision()
947
            refs, stop_revision = self.update_refs(stop_revision)
948
            self.target.generate_revision_history(stop_revision, result.old_revid)
949
            tags_ret = self.source.tags.merge_to(self.target.tags,
950
                overwrite=overwrite, source_refs=refs)
951
            if isinstance(tags_ret, tuple):
952
                (result.tag_updates, result.tag_conflicts) = tags_ret
953
            else:
954
                result.tag_conflicts = tags_ret
955
            result.new_revid = self.target.last_revision()
956
            result.local_branch = None
957
            result.master_branch = result.target_branch
958
            if run_hooks:
959
                for hook in branch.Branch.hooks['post_pull']:
960
                    hook(result)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
961
        return result
962
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
963
0.200.960 by Jelmer Vernooij
Use GenericInterBranch.
964
class InterToGitBranch(branch.GenericInterBranch):
0.200.1185 by Jelmer Vernooij
Some formatting fixes.
965
    """InterBranch implementation that pulls into a Git branch."""
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
966
0.200.939 by Jelmer Vernooij
Use InterRepo directly.
967
    def __init__(self, source, target):
968
        super(InterToGitBranch, self).__init__(source, target)
0.200.1097 by Jelmer Vernooij
Implement GitBranchFormat.initialize.
969
        self.interrepo = _mod_repository.InterRepository.get(source.repository,
0.200.939 by Jelmer Vernooij
Use InterRepo directly.
970
                                           target.repository)
971
0.200.631 by Jelmer Vernooij
Raise proper exception in Branch.get_stacked_on_url().
972
    @staticmethod
973
    def _get_branch_formats_to_test():
0.200.1100 by Jelmer Vernooij
Provide test combinations for InterBranch implementations.
974
        try:
975
            default_format = branch.format_registry.get_default()
976
        except AttributeError:
977
            default_format = branch.BranchFormat._default_format
978
        return [(default_format, GitBranchFormat())]
0.200.631 by Jelmer Vernooij
Raise proper exception in Branch.get_stacked_on_url().
979
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
980
    @classmethod
981
    def is_compatible(self, source, target):
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
982
        return (not isinstance(source, GitBranch) and
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
983
                isinstance(target, GitBranch))
984
0.200.1363 by Jelmer Vernooij
Only fetch tags if requested in config.
985
    def _get_new_refs(self, stop_revision=None, fetch_tags=None):
0.200.1398 by Jelmer Vernooij
Make GitSmartRemoteNotSupported derive from UnsupportedOperation.
986
        assert self.source.is_locked()
0.252.38 by Jelmer Vernooij
Minor cleanups.
987
        if stop_revision is None:
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
988
            (stop_revno, stop_revision) = self.source.last_revision_info()
0.263.1 by Jelmer Vernooij
Fix dpush for certain branches.
989
        else:
990
            stop_revno = self.source.revision_id_to_revno(stop_revision)
0.200.969 by Jelmer Vernooij
Use tuples with bzr revid and git sha to avoid lookups.
991
        assert type(stop_revision) is str
0.200.916 by Jelmer Vernooij
Set refs/heads/master if no ref is set yet.
992
        main_ref = self.target.ref or "refs/heads/master"
0.200.969 by Jelmer Vernooij
Use tuples with bzr revid and git sha to avoid lookups.
993
        refs = { main_ref: (None, stop_revision) }
0.200.1363 by Jelmer Vernooij
Only fetch tags if requested in config.
994
        if fetch_tags is None:
0.200.1584 by Jelmer Vernooij
Use config stacks in a few more places.
995
            c = self.source.get_config_stack()
996
            fetch_tags = c.get('branch.fetch_tags')
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
997
        for name, revid in self.source.tags.get_tag_dict().iteritems():
998
            if self.source.repository.has_revision(revid):
999
                ref = tag_name_to_ref(name)
1000
                if not check_ref_format(ref):
1001
                    warning("skipping tag with invalid characters %s (%s)",
1002
                        name, ref)
1003
                    continue
0.200.1398 by Jelmer Vernooij
Make GitSmartRemoteNotSupported derive from UnsupportedOperation.
1004
                if fetch_tags:
1005
                    # FIXME: Skip tags that are not in the ancestry
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1006
                    refs[ref] = (None, revid)
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
1007
        return refs, main_ref, (stop_revno, stop_revision)
0.252.37 by Jelmer Vernooij
Factor out some common code for finding refs to send.
1008
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1009
    def _update_refs(self, result, old_refs, new_refs, overwrite):
1010
        mutter("updating refs. old refs: %r, new refs: %r",
1011
               old_refs, new_refs)
1012
        result.tag_updates = {}
1013
        result.tag_conflicts = []
1014
        ret = dict(old_refs)
1015
        def ref_equals(refs, ref, git_sha, revid):
1016
            try:
1017
                value = refs[ref]
1018
            except KeyError:
1019
                return False
1020
            if (value[0] is not None and
1021
                git_sha is not None and
0.200.1479 by Jelmer Vernooij
Simplify branch ref handling.
1022
                value[0] == git_sha):
1023
                return True
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1024
            if (value[1] is not None and
1025
                revid is not None and
0.200.1479 by Jelmer Vernooij
Simplify branch ref handling.
1026
                value[1] == revid):
1027
                return True
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1028
            # FIXME: If one side only has the git sha available and the other only
1029
            # has the bzr revid, then this will cause us to show a tag as updated
0.200.1636 by Jelmer Vernooij
Some formatting fixes.
1030
            # that hasn't actually been updated.
0.200.1479 by Jelmer Vernooij
Simplify branch ref handling.
1031
            return False
0.200.1474 by Jelmer Vernooij
Cope with refs when pushing.
1032
        # FIXME: Check for diverged branches
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1033
        for ref, (git_sha, revid) in new_refs.iteritems():
0.200.1479 by Jelmer Vernooij
Simplify branch ref handling.
1034
            if ref_equals(ret, ref, git_sha, revid):
1035
                # Already up to date
1036
                if git_sha is None:
1037
                    git_sha = old_refs[ref][0]
1038
                if revid is None:
1039
                    revid = old_refs[ref][1]
1040
                ret[ref] = new_refs[ref] = (git_sha, revid)
1041
            elif ref not in ret or overwrite:
1042
                try:
1043
                    tag_name = ref_to_tag_name(ref)
1044
                except ValueError:
1045
                    pass
1046
                else:
1047
                    result.tag_updates[tag_name] = revid
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1048
                ret[ref] = (git_sha, revid)
1049
            else:
0.200.1474 by Jelmer Vernooij
Cope with refs when pushing.
1050
                # FIXME: Check diverged
1051
                diverged = False
1052
                if diverged:
1053
                    try:
1054
                        name = ref_to_tag_name(ref)
1055
                    except ValueError:
1056
                        pass
1057
                    else:
1058
                        result.tag_conflicts.append((name, revid, ret[name][1]))
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1059
                else:
0.200.1474 by Jelmer Vernooij
Cope with refs when pushing.
1060
                    ret[ref] = (git_sha, revid)
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1061
        return ret
1062
0.200.1493 by Jelmer Vernooij
Test fixes.
1063
    def fetch(self, stop_revision=None, fetch_tags=None, lossy=False, limit=None):
1064
        assert limit is None
1065
        if stop_revision is None:
1066
            stop_revision = self.source.last_revision()
1067
        ret = []
1068
        if fetch_tags:
1069
            for k, v in self.source.tags.get_tag_dict().iteritems():
1070
                ret.append((None, v))
1071
        ret.append((None, stop_revision))
0.200.1509 by Jelmer Vernooij
Properly raise exception when pulling from git into bzr without experimental mappings.
1072
        self.interrepo.fetch_objects(ret, lossy=lossy)
0.200.1493 by Jelmer Vernooij
Test fixes.
1073
0.252.36 by Jelmer Vernooij
Fix pull.
1074
    def pull(self, overwrite=False, stop_revision=None, local=False,
0.200.1131 by Jelmer Vernooij
Accept run_hooks argument to InterToGitBranch.pull().
1075
             possible_transports=None, run_hooks=True):
0.252.36 by Jelmer Vernooij
Fix pull.
1076
        result = GitBranchPullResult()
1077
        result.source_branch = self.source
1078
        result.target_branch = self.target
0.200.1353 by Jelmer Vernooij
Run various hooks.
1079
        self.source.lock_read()
0.200.1156 by Jelmer Vernooij
Disable push.
1080
        try:
0.200.1353 by Jelmer Vernooij
Run various hooks.
1081
            self.target.lock_write()
1082
            try:
0.200.1389 by Jelmer Vernooij
Some more tag fixes.
1083
                new_refs, main_ref, stop_revinfo = self._get_new_refs(
1084
                    stop_revision)
0.200.1353 by Jelmer Vernooij
Run various hooks.
1085
                def update_refs(old_refs):
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1086
                    return self._update_refs(result, old_refs, new_refs, overwrite)
0.200.1353 by Jelmer Vernooij
Run various hooks.
1087
                try:
1088
                    result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
1089
                        update_refs, lossy=False)
1090
                except NoPushSupport:
1091
                    raise errors.NoRoundtrippingSupport(self.source, self.target)
0.200.1513 by Jelmer Vernooij
Cope with zero shas.
1092
                (old_sha1, result.old_revid) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
0.200.1353 by Jelmer Vernooij
Run various hooks.
1093
                if result.old_revid is None:
1094
                    result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
1095
                result.new_revid = new_refs[main_ref][1]
1096
                result.local_branch = None
1097
                result.master_branch = self.target
1098
                if run_hooks:
1099
                    for hook in branch.Branch.hooks['post_pull']:
1100
                        hook(result)
1101
            finally:
1102
                self.target.unlock()
1103
        finally:
1104
            self.source.unlock()
0.252.36 by Jelmer Vernooij
Fix pull.
1105
        return result
1106
0.200.1260 by Jelmer Vernooij
Cope with new lossy argument.
1107
    def push(self, overwrite=False, stop_revision=None, lossy=False,
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
1108
             _override_hook_source_branch=None):
0.252.5 by Jelmer Vernooij
enable 'bzr push'.
1109
        result = GitBranchPushResult()
1110
        result.source_branch = self.source
1111
        result.target_branch = self.target
0.200.1356 by Jelmer Vernooij
Fix result properties for hook.
1112
        result.local_branch = None
1113
        result.master_branch = result.target_branch
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1114
        self.source.lock_read()
0.200.1323 by Jelmer Vernooij
Simplify push handling.
1115
        try:
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1116
            new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
1117
            def update_refs(old_refs):
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1118
                return self._update_refs(result, old_refs, new_refs, overwrite)
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1119
            try:
1120
                result.revidmap, old_refs, new_refs = self.interrepo.fetch_refs(
1121
                    update_refs, lossy=lossy)
1122
            except NoPushSupport:
1123
                raise errors.NoRoundtrippingSupport(self.source, self.target)
1124
            (old_sha1, result.old_revid) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
1125
            if result.old_revid is None:
1126
                result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
1127
            result.new_revid = new_refs[main_ref][1]
1128
            (result.new_original_revno, result.new_original_revid) = stop_revinfo
1129
            for hook in branch.Branch.hooks['post_push']:
1130
                hook(result)
1131
        finally:
1132
            self.source.unlock()
0.252.5 by Jelmer Vernooij
enable 'bzr push'.
1133
        return result
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
1134
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
1135
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
1136
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
1137
branch.InterBranch.register_optimiser(InterFromGitBranch)
1138
branch.InterBranch.register_optimiser(InterToGitBranch)
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
1139
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)