/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
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
2
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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
0.358.1 by Jelmer Vernooij
Fix FSF address.
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
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
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
22
from io import BytesIO
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.1717 by Jelmer Vernooij
Ignore tags pointing at non-commit objects.
26
    NotCommitError,
0.200.1153 by Jelmer Vernooij
Import ZERO_SHA from dulwich.objects.
27
    ZERO_SHA,
0.200.261 by Jelmer Vernooij
More formatting fixes.
28
    )
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
29
from dulwich.repo import check_ref_format
0.200.261 by Jelmer Vernooij
More formatting fixes.
30
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
31
from .. import (
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
32
    branch,
33
    config,
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
34
    controldir,
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
35
    errors,
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
36
    lock,
0.200.1097 by Jelmer Vernooij
Implement GitBranchFormat.initialize.
37
    repository as _mod_repository,
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
38
    revision,
0.200.82 by Jelmer Vernooij
Support listing tags.
39
    tag,
0.375.1 by Jelmer Vernooij
Fix remote tests, warn when fetching git->bzr and bzr->git.
40
    trace,
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
41
    transport,
0.200.1414 by Jelmer Vernooij
Fix pulling into bound branches.
42
    urlutils,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
43
    )
6986.2.3 by Jelmer Vernooij
Merge trunk
44
from ..foreign import ForeignBranch
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
45
from ..revision import (
0.200.969 by Jelmer Vernooij
Use tuples with bzr revid and git sha to avoid lookups.
46
    NULL_REVISION,
47
    )
6986.2.3 by Jelmer Vernooij
Merge trunk
48
from ..sixish import (
7018.3.2 by Jelmer Vernooij
Fix some git tests.
49
    text_type,
50
    viewitems,
51
    )
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
52
from ..trace import (
0.200.342 by Jelmer Vernooij
Report git sha during pull.
53
    is_quiet,
0.200.261 by Jelmer Vernooij
More formatting fixes.
54
    mutter,
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
55
    warning,
0.200.261 by Jelmer Vernooij
More formatting fixes.
56
    )
57
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
58
from .config import (
0.200.386 by Jelmer Vernooij
Move config to a separate file, support BranchConfig.username().
59
    GitBranchConfig,
0.200.1472 by Jelmer Vernooij
Provide basic implementation of Branch.get_config_stack.
60
    GitBranchStack,
0.200.386 by Jelmer Vernooij
Move config to a separate file, support BranchConfig.username().
61
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
62
from .errors import (
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
63
    NoPushSupport,
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
64
    )
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
65
from .push import (
66
    remote_divergence,
67
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
68
from .refs import (
0.408.4 by Jelmer Vernooij
Fix some tests.
69
    branch_name_to_ref,
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
70
    is_tag,
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
71
    ref_to_branch_name,
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
72
    ref_to_tag_name,
0.346.1 by Jelmer Vernooij
Fix tag caching.
73
    remote_refs_dict_to_tag_refs,
0.200.875 by Jelmer Vernooij
Use new tag_name_to_ref function.
74
    tag_name_to_ref,
0.200.1292 by Jelmer Vernooij
Fix repeeling objects when determining what to send.
75
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
76
from .unpeel_map import (
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
77
    UnpeelMap,
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
78
    )
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
79
from .urls import (
80
    git_url_to_bzr_url,
81
    bzr_url_to_git_url,
82
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
83
0.200.388 by Jelmer Vernooij
Support bzr 1.14 as well.
84
7339.1.6 by Jelmer Vernooij
Also support revno-less push/pull operations.
85
def _calculate_revnos(branch):
86
    if branch._format.stores_revno():
87
        return True
88
    config = branch.get_config_stack()
89
    return config.get('calculate_revnos')
90
91
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
92
class GitPullResult(branch.PullResult):
0.200.956 by Jelmer Vernooij
Add some more format tests.
93
    """Result of a pull from a Git branch."""
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
94
95
    def _lookup_revno(self, revid):
6973.13.2 by Jelmer Vernooij
Fix some more tests.
96
        if not isinstance(revid, bytes):
0.361.1 by Jelmer Vernooij
Don't use assert.
97
            raise TypeError(revid)
7339.1.6 by Jelmer Vernooij
Also support revno-less push/pull operations.
98
        if not _calculate_revnos(self.target_branch):
99
            return None
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
100
        # Try in source branch first, it'll be faster
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
101
        with self.target_branch.lock_read():
0.200.1362 by Jelmer Vernooij
Add locking.
102
            return self.target_branch.revision_id_to_revno(revid)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
103
104
    @property
105
    def old_revno(self):
106
        return self._lookup_revno(self.old_revid)
107
108
    @property
109
    def new_revno(self):
110
        return self._lookup_revno(self.new_revid)
111
112
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
113
class GitTags(tag.BasicTags):
114
    """Ref-based tag dictionary."""
0.200.82 by Jelmer Vernooij
Support listing tags.
115
0.200.89 by Jelmer Vernooij
Support sprouting branches.
116
    def __init__(self, branch):
117
        self.branch = branch
118
        self.repository = branch.repository
0.200.82 by Jelmer Vernooij
Support listing tags.
119
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
120
    def _merge_to_remote_git(self, target_repo, source_tag_refs,
121
                             overwrite=False):
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
122
        updates = {}
123
        conflicts = []
7143.15.2 by Jelmer Vernooij
Run autopep8.
124
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
125
        def get_changed_refs(old_refs):
126
            ret = dict(old_refs)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
127
            for ref_name, tag_name, peeled, unpeeled in (
128
                    source_tag_refs.iteritems()):
0.346.1 by Jelmer Vernooij
Fix tag caching.
129
                if old_refs.get(ref_name) == unpeeled:
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
130
                    pass
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
131
                elif overwrite or ref_name not in old_refs:
0.346.1 by Jelmer Vernooij
Fix tag caching.
132
                    ret[ref_name] = unpeeled
7143.15.2 by Jelmer Vernooij
Run autopep8.
133
                    updates[tag_name] = target_repo.lookup_foreign_revision_id(
134
                        peeled)
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
135
                else:
0.346.1 by Jelmer Vernooij
Fix tag caching.
136
                    conflicts.append(
137
                        (tag_name,
7143.15.2 by Jelmer Vernooij
Run autopep8.
138
                         self.repository.lookup_foreign_revision_id(peeled),
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
139
                         target_repo.lookup_foreign_revision_id(
140
                             old_refs[ref_name])))
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
141
            return ret
7143.15.2 by Jelmer Vernooij
Run autopep8.
142
        target_repo.controldir.send_pack(
143
            get_changed_refs, lambda have, want: [])
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
144
        return updates, conflicts
145
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
146
    def _merge_to_local_git(self, target_repo, source_tag_refs,
147
                            overwrite=False):
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
148
        conflicts = []
149
        updates = {}
0.346.1 by Jelmer Vernooij
Fix tag caching.
150
        for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
151
            if target_repo._git.refs.get(ref_name) == unpeeled:
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
152
                pass
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
153
            elif overwrite or ref_name not in target_repo._git.refs:
7143.12.3 by Jelmer Vernooij
Ignore ghost tags.
154
                try:
7143.15.8 by Jelmer Vernooij
Merge trunk.
155
                    updates[tag_name] = (
7240.2.1 by Jelmer Vernooij
Don't copy tags that don't point to valid objects.
156
                        target_repo.lookup_foreign_revision_id(peeled))
7143.12.3 by Jelmer Vernooij
Ignore ghost tags.
157
                except KeyError:
158
                    trace.warning('%s does not point to a valid object',
159
                                  tag_name)
7142.3.3 by Jelmer Vernooij
Fix test.
160
                    continue
7240.2.1 by Jelmer Vernooij
Don't copy tags that don't point to valid objects.
161
                target_repo._git.refs[ref_name] = unpeeled or peeled
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
162
            else:
0.375.1 by Jelmer Vernooij
Fix remote tests, warn when fetching git->bzr and bzr->git.
163
                try:
7143.15.8 by Jelmer Vernooij
Merge trunk.
164
                    source_revid = self.repository.lookup_foreign_revision_id(
165
                        peeled)
0.375.1 by Jelmer Vernooij
Fix remote tests, warn when fetching git->bzr and bzr->git.
166
                    target_revid = target_repo.lookup_foreign_revision_id(
7143.15.2 by Jelmer Vernooij
Run autopep8.
167
                        target_repo._git.refs[ref_name])
0.375.1 by Jelmer Vernooij
Fix remote tests, warn when fetching git->bzr and bzr->git.
168
                except KeyError:
169
                    trace.warning('%s does not point to a valid object',
170
                                  ref_name)
171
                    continue
172
                conflicts.append((tag_name, source_revid, target_revid))
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
173
        return updates, conflicts
174
0.346.1 by Jelmer Vernooij
Fix tag caching.
175
    def _merge_to_git(self, to_tags, source_tag_refs, overwrite=False):
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
176
        target_repo = to_tags.repository
177
        if self.repository.has_same_location(target_repo):
178
            return {}, []
0.346.1 by Jelmer Vernooij
Fix tag caching.
179
        try:
180
            if getattr(target_repo, "_git", None):
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
181
                return self._merge_to_local_git(
182
                    target_repo, source_tag_refs, overwrite)
0.346.1 by Jelmer Vernooij
Fix tag caching.
183
            else:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
184
                return self._merge_to_remote_git(
185
                    target_repo, source_tag_refs, overwrite)
0.346.1 by Jelmer Vernooij
Fix tag caching.
186
        finally:
187
            to_tags.branch._tag_refs = None
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
188
0.346.1 by Jelmer Vernooij
Fix tag caching.
189
    def _merge_to_non_git(self, to_tags, source_tag_refs, overwrite=False):
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
190
        unpeeled_map = defaultdict(set)
191
        conflicts = []
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
192
        updates = {}
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
193
        result = dict(to_tags.get_tag_dict())
0.346.1 by Jelmer Vernooij
Fix tag caching.
194
        for ref_name, tag_name, peeled, unpeeled in source_tag_refs:
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
195
            if unpeeled is not None:
196
                unpeeled_map[peeled].add(unpeeled)
0.346.1 by Jelmer Vernooij
Fix tag caching.
197
            try:
198
                bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
199
            except NotCommitError:
200
                continue
201
            if result.get(tag_name) == bzr_revid:
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
202
                pass
0.346.1 by Jelmer Vernooij
Fix tag caching.
203
            elif tag_name not in result or overwrite:
204
                result[tag_name] = bzr_revid
205
                updates[tag_name] = bzr_revid
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
206
            else:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
207
                conflicts.append((tag_name, bzr_revid, result[tag_name]))
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
208
        to_tags._set_tag_dict(result)
209
        if len(unpeeled_map) > 0:
210
            map_file = UnpeelMap.from_repository(to_tags.branch.repository)
211
            map_file.update(unpeeled_map)
212
            map_file.save_in_repository(to_tags.branch.repository)
0.200.1396 by Jelmer Vernooij
Support updating tags in remote branches during pull.
213
        return updates, conflicts
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
214
215
    def merge_to(self, to_tags, overwrite=False, ignore_master=False,
0.346.1 by Jelmer Vernooij
Fix tag caching.
216
                 source_tag_refs=None):
0.200.1113 by Jelmer Vernooij
Fix Tags.merge_to.
217
        """See Tags.merge_to."""
0.346.1 by Jelmer Vernooij
Fix tag caching.
218
        if source_tag_refs is None:
219
            source_tag_refs = self.branch.get_tag_refs()
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
220
        if self == to_tags:
0.200.1402 by Jelmer Vernooij
Cope with tag changes in bzr.
221
            return {}, []
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
222
        if isinstance(to_tags, GitTags):
0.346.1 by Jelmer Vernooij
Fix tag caching.
223
            return self._merge_to_git(to_tags, source_tag_refs,
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
224
                                      overwrite=overwrite)
225
        else:
226
            if ignore_master:
227
                master = None
228
            else:
229
                master = to_tags.branch.get_master_branch()
230
            if master is not None:
0.331.1 by Jelmer Vernooij
Fix some tags tests.
231
                master.lock_write()
232
            try:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
233
                updates, conflicts = self._merge_to_non_git(
234
                    to_tags, source_tag_refs, overwrite=overwrite)
0.331.1 by Jelmer Vernooij
Fix some tags tests.
235
                if master is not None:
236
                    extra_updates, extra_conflicts = self.merge_to(
237
                        master.tags, overwrite=overwrite,
7143.15.2 by Jelmer Vernooij
Run autopep8.
238
                        source_tag_refs=source_tag_refs,
239
                        ignore_master=ignore_master)
0.331.1 by Jelmer Vernooij
Fix some tags tests.
240
                    updates.update(extra_updates)
241
                    conflicts += extra_conflicts
242
                return updates, conflicts
243
            finally:
244
                if master is not None:
245
                    master.unlock()
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
246
247
    def get_tag_dict(self):
248
        ret = {}
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
249
        for (ref_name, tag_name, peeled, unpeeled) in (
250
                self.branch.get_tag_refs()):
0.346.1 by Jelmer Vernooij
Fix tag caching.
251
            try:
252
                bzr_revid = self.branch.lookup_foreign_revision_id(peeled)
253
            except NotCommitError:
254
                continue
255
            else:
256
                ret[tag_name] = bzr_revid
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
257
        return ret
258
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
259
260
class LocalGitTagDict(GitTags):
261
    """Dictionary with tags in a local repository."""
262
263
    def __init__(self, branch):
264
        super(LocalGitTagDict, self).__init__(branch)
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
265
        self.refs = self.repository.controldir._git.refs
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
266
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
267
    def _set_tag_dict(self, to_dict):
0.200.1487 by Jelmer Vernooij
Use peeling.
268
        extra = set(self.refs.allkeys())
6973.13.2 by Jelmer Vernooij
Fix some more tests.
269
        for k, revid in viewitems(to_dict):
0.200.875 by Jelmer Vernooij
Use new tag_name_to_ref function.
270
            name = tag_name_to_ref(k)
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
271
            if name in extra:
272
                extra.remove(name)
273
            self.set_tag(k, revid)
274
        for name in extra:
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
275
            if is_tag(name):
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
276
                del self.repository._git[name]
0.200.956 by Jelmer Vernooij
Add some more format tests.
277
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
278
    def set_tag(self, name, revid):
0.200.1369 by Jelmer Vernooij
Clarify that ghost tags are not supported.
279
        try:
280
            git_sha, mapping = self.branch.lookup_bzr_revision_id(revid)
281
        except errors.NoSuchRevision:
282
            raise errors.GhostTagsNotSupported(self)
283
        self.refs[tag_name_to_ref(name)] = git_sha
0.346.1 by Jelmer Vernooij
Fix tag caching.
284
        self.branch._tag_refs = None
285
286
    def delete_tag(self, name):
287
        ref = tag_name_to_ref(name)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
288
        if ref not in self.refs:
0.346.1 by Jelmer Vernooij
Fix tag caching.
289
            raise errors.NoSuchTag(name)
290
        del self.refs[ref]
291
        self.branch._tag_refs = None
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
292
293
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
294
class GitBranchFormat(branch.BranchFormat):
295
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
296
    def network_name(self):
6973.6.2 by Jelmer Vernooij
Fix more tests.
297
        return b"git"
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
298
0.200.82 by Jelmer Vernooij
Support listing tags.
299
    def supports_tags(self):
300
        return True
301
0.200.1105 by Jelmer Vernooij
Don't claim to support leaving locks.
302
    def supports_leaving_lock(self):
303
        return False
304
0.200.1369 by Jelmer Vernooij
Clarify that ghost tags are not supported.
305
    def supports_tags_referencing_ghosts(self):
306
        return False
307
308
    def tags_are_versioned(self):
309
        return False
310
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
311
    def get_foreign_tests_branch_factory(self):
0.200.1644 by Jelmer Vernooij
More relative imports.
312
        from .tests.test_branch import ForeignTestsBranchFactory
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
313
        return ForeignTestsBranchFactory()
314
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
315
    def make_tags(self, branch):
0.200.1487 by Jelmer Vernooij
Use peeling.
316
        try:
317
            return branch.tags
318
        except AttributeError:
319
            pass
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
320
        if getattr(branch.repository, "_git", None) is None:
0.200.1644 by Jelmer Vernooij
More relative imports.
321
            from .remote import RemoteGitTagDict
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
322
            return RemoteGitTagDict(branch)
0.200.261 by Jelmer Vernooij
More formatting fixes.
323
        else:
324
            return LocalGitTagDict(branch)
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
325
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
326
    def initialize(self, a_controldir, name=None, repository=None,
0.200.1378 by Jelmer Vernooij
Fix branch.
327
                   append_revisions_only=None):
0.295.2 by Jelmer Vernooij
Make RemoteGitBranchFormat uninitializeable.
328
        raise NotImplementedError(self.initialize)
0.200.1097 by Jelmer Vernooij
Implement GitBranchFormat.initialize.
329
0.200.1728 by Jelmer Vernooij
Support following branch references.
330
    def get_reference(self, controldir, name=None):
7142.3.1 by Jelmer Vernooij
Support .nick on remote branches and fix get_branch_reference.
331
        return controldir.get_branch_reference(name=name)
0.200.1728 by Jelmer Vernooij
Support following branch references.
332
333
    def set_reference(self, controldir, name, target):
334
        return controldir.set_branch_reference(target, name)
335
7339.1.2 by Jelmer Vernooij
Add stores_revno method to BranchFormat.
336
    def stores_revno(self):
337
        """True if this branch format store revision numbers."""
338
        return False
339
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
340
0.295.1 by Jelmer Vernooij
Split up branch formats.
341
class LocalGitBranchFormat(GitBranchFormat):
342
343
    def get_format_description(self):
344
        return 'Local Git Branch'
345
346
    @property
347
    def _matchingcontroldir(self):
348
        from .dir import LocalGitControlDirFormat
349
        return LocalGitControlDirFormat()
350
0.295.2 by Jelmer Vernooij
Make RemoteGitBranchFormat uninitializeable.
351
    def initialize(self, a_controldir, name=None, repository=None,
352
                   append_revisions_only=None):
353
        from .dir import LocalGitDir
354
        if not isinstance(a_controldir, LocalGitDir):
355
            raise errors.IncompatibleFormat(self, a_controldir._format)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
356
        return a_controldir.create_branch(
357
            repository=repository, name=name,
358
            append_revisions_only=append_revisions_only)
0.295.2 by Jelmer Vernooij
Make RemoteGitBranchFormat uninitializeable.
359
0.295.1 by Jelmer Vernooij
Split up branch formats.
360
0.200.388 by Jelmer Vernooij
Support bzr 1.14 as well.
361
class GitBranch(ForeignBranch):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
362
    """An adapter to git repositories for bzr Branch objects."""
363
0.200.1129 by Jelmer Vernooij
Implement GitBranch.control_transport.
364
    @property
365
    def control_transport(self):
0.310.16 by Jelmer Vernooij
Update xfail.
366
        return self._control_transport
0.306.1 by Jelmer Vernooij
Set correct segment parameters on branches.
367
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
368
    @property
369
    def user_transport(self):
370
        return self._user_transport
371
0.295.1 by Jelmer Vernooij
Split up branch formats.
372
    def __init__(self, controldir, repository, ref, format):
0.200.82 by Jelmer Vernooij
Support listing tags.
373
        self.repository = repository
0.295.1 by Jelmer Vernooij
Split up branch formats.
374
        self._format = format
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
375
        self.controldir = controldir
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
376
        self._lock_mode = None
377
        self._lock_count = 0
0.231.1 by Jelmer Vernooij
Check that regenerated objects have the expected sha1.
378
        super(GitBranch, self).__init__(repository.get_mapping())
0.200.770 by Jelmer Vernooij
Proper branch names.
379
        self.ref = ref
0.200.1708 by Jelmer Vernooij
Fix to colocated branch tests.
380
        self._head = None
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
381
        self._user_transport = controldir.user_transport.clone('.')
0.310.16 by Jelmer Vernooij
Update xfail.
382
        self._control_transport = controldir.control_transport.clone('.')
0.346.1 by Jelmer Vernooij
Fix tag caching.
383
        self._tag_refs = None
0.306.1 by Jelmer Vernooij
Set correct segment parameters on branches.
384
        params = {}
0.200.1361 by Jelmer Vernooij
Support branches where the ref can't be mapped back to a branch name.
385
        try:
386
            self.name = ref_to_branch_name(ref)
387
        except ValueError:
388
            self.name = None
0.200.1708 by Jelmer Vernooij
Fix to colocated branch tests.
389
            if self.ref is not None:
0.310.16 by Jelmer Vernooij
Update xfail.
390
                params = {"ref": urlutils.escape(self.ref)}
0.200.1708 by Jelmer Vernooij
Fix to colocated branch tests.
391
        else:
392
            if self.name != "":
0.310.16 by Jelmer Vernooij
Update xfail.
393
                params = {"branch": urlutils.escape(self.name)}
394
        for k, v in params.items():
395
            self._user_transport.set_segment_parameter(k, v)
396
            self._control_transport.set_segment_parameter(k, v)
0.310.14 by Jelmer Vernooij
merge lightweight checkout-in-git support.
397
        self.base = controldir.user_transport.base
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
398
0.200.1360 by Jelmer Vernooij
Support lighweight argument to _get_checkout_format.
399
    def _get_checkout_format(self, lightweight=False):
0.239.8 by Jelmer Vernooij
Support checkouts.
400
        """Return the most suitable metadir for a checkout of this branch.
401
        Weaves are used if this branch's repository uses weaves.
402
        """
0.311.1 by Jelmer Vernooij
Support git lightweight checkouts.
403
        if lightweight:
404
            return controldir.format_registry.make_controldir("git")
405
        else:
406
            return controldir.format_registry.make_controldir("default")
0.239.8 by Jelmer Vernooij
Support checkouts.
407
0.238.3 by Jelmer Vernooij
Remove svn references, prefer git send format when submitting changes against a git branch.
408
    def get_child_submit_format(self):
409
        """Return the preferred format of submissions to this branch."""
0.200.1584 by Jelmer Vernooij
Use config stacks in a few more places.
410
        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.
411
        if ret is not None:
412
            return ret
413
        return "git"
414
0.200.1397 by Jelmer Vernooij
Fix use of get_config() for RemoteGitBranch.
415
    def get_config(self):
416
        return GitBranchConfig(self)
417
0.200.1472 by Jelmer Vernooij
Provide basic implementation of Branch.get_config_stack.
418
    def get_config_stack(self):
419
        return GitBranchStack(self)
420
0.200.293 by Jelmer Vernooij
Fix branch nicks.
421
    def _get_nick(self, local=False, possible_master_transports=None):
422
        """Find the nick name for this branch.
423
424
        :return: Branch nick
425
        """
7131.14.1 by Jelmer Vernooij
Fix committing to a bzr branch bound to a git branch.
426
        if getattr(self.repository, '_git', None):
427
            cs = self.repository._git.get_config_stack()
428
            try:
7143.15.12 by Jelmer Vernooij
Merge trunk.
429
                return cs.get((b"branch", self.name.encode('utf-8')),
7143.16.21 by Jelmer Vernooij
Fix regressions.
430
                              b"nick").decode("utf-8")
7131.14.1 by Jelmer Vernooij
Fix committing to a bzr branch bound to a git branch.
431
            except KeyError:
432
                pass
0.200.1694 by Jelmer Vernooij
Fix nick test.
433
        return self.name or u"HEAD"
0.200.293 by Jelmer Vernooij
Fix branch nicks.
434
0.200.331 by Jelmer Vernooij
Add stub for setting nick function.
435
    def _set_nick(self, nick):
0.200.1547 by Jelmer Vernooij
Support setting branch nicks.
436
        cf = self.repository._git.get_config()
7143.15.2 by Jelmer Vernooij
Run autopep8.
437
        cf.set((b"branch", self.name.encode('utf-8')),
438
               b"nick", nick.encode("utf-8"))
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
439
        f = BytesIO()
0.200.1547 by Jelmer Vernooij
Support setting branch nicks.
440
        cf.write_to_file(f)
0.288.3 by Jelmer Vernooij
Fix parent URL handling.
441
        self.repository._git._put_named_file('config', f.getvalue())
0.200.331 by Jelmer Vernooij
Add stub for setting nick function.
442
443
    nick = property(_get_nick, _set_nick)
0.200.293 by Jelmer Vernooij
Fix branch nicks.
444
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
445
    def __repr__(self):
0.200.770 by Jelmer Vernooij
Proper branch names.
446
        return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
7143.15.2 by Jelmer Vernooij
Run autopep8.
447
                                 self.name)
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
448
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
449
    def generate_revision_history(self, revid, last_rev=None,
450
                                  other_branch=None):
0.296.2 by Jelmer Vernooij
Handle DivergedBranches.
451
        if last_rev is not None:
452
            graph = self.repository.get_graph()
453
            if not graph.is_ancestor(last_rev, revid):
454
                # our previous tip is not merged into stop_revision
455
                raise errors.DivergedBranches(self, other_branch)
456
0.297.2 by Jelmer Vernooij
Simplify Branch.generate_revision_history.
457
        self.set_last_revision(revid)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
458
0.200.1199 by Jelmer Vernooij
Support 'token' argument to Branch.lock_write.
459
    def lock_write(self, token=None):
460
        if token is not None:
461
            raise errors.TokenLockingNotSupported(self)
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
462
        if self._lock_mode:
0.200.1369 by Jelmer Vernooij
Clarify that ghost tags are not supported.
463
            if self._lock_mode == 'r':
464
                raise errors.ReadOnlyError(self)
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
465
            self._lock_count += 1
466
        else:
0.383.1 by Jelmer Vernooij
Fix a bunch of locking issues.
467
            self._lock_ref()
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
468
            self._lock_mode = 'w'
469
            self._lock_count = 1
470
        self.repository.lock_write()
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
471
        return lock.LogicalLockResult(self.unlock)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
472
0.200.1453 by Jelmer Vernooij
Provide Branch.leave_lock_in_place and Branch.dont_leave_lock_in_place.
473
    def leave_lock_in_place(self):
474
        raise NotImplementedError(self.leave_lock_in_place)
475
476
    def dont_leave_lock_in_place(self):
477
        raise NotImplementedError(self.dont_leave_lock_in_place)
478
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
479
    def get_stacked_on_url(self):
480
        # Git doesn't do stacking (yet...)
0.200.1660 by Jelmer Vernooij
Fix imports.
481
        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.
482
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
483
    def _get_push_origin(self, cs):
7141.8.3 by Jelmer Vernooij
Review comments.
484
        """Get the name for the push origin.
485
486
        The exact behaviour is documented in the git-config(1) manpage.
487
        """
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
488
        try:
7141.8.3 by Jelmer Vernooij
Review comments.
489
            return cs.get((b'branch', self.name.encode('utf-8')), b'pushRemote')
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
490
        except KeyError:
491
            try:
7141.8.3 by Jelmer Vernooij
Review comments.
492
                return cs.get((b'branch', ), b'remote')
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
493
            except KeyError:
494
                try:
7141.8.3 by Jelmer Vernooij
Review comments.
495
                    return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
496
                except KeyError:
497
                    return b'origin'
498
499
    def _get_origin(self, cs):
500
        try:
7141.8.3 by Jelmer Vernooij
Review comments.
501
            return cs.get((b'branch', self.name.encode('utf-8')), b'remote')
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
502
        except KeyError:
503
            return b'origin'
504
505
    def _get_related_push_branch(self, cs):
506
        remote = self._get_push_origin(cs)
507
        try:
508
            location = cs.get((b"remote", remote), b"url")
509
        except KeyError:
510
            return None
511
512
        return git_url_to_bzr_url(location.decode('utf-8'), ref=self.ref)
513
514
    def _get_related_merge_branch(self, cs):
515
        remote = self._get_origin(cs)
516
        try:
517
            location = cs.get((b"remote", remote), b"url")
518
        except KeyError:
519
            return None
520
521
        try:
7141.8.3 by Jelmer Vernooij
Review comments.
522
            ref = cs.get((b"branch", remote), b"merge")
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
523
        except KeyError:
524
            ref = self.ref
525
526
        return git_url_to_bzr_url(location.decode('utf-8'), ref=ref)
527
0.288.3 by Jelmer Vernooij
Fix parent URL handling.
528
    def _get_parent_location(self):
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
529
        """See Branch.get_parent()."""
0.288.3 by Jelmer Vernooij
Fix parent URL handling.
530
        cs = self.repository._git.get_config_stack()
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
531
        return self._get_related_merge_branch(cs)
532
533
    def _write_git_config(self, cs):
534
        f = BytesIO()
535
        cs.write_to_file(f)
536
        self.repository._git._put_named_file('config', f.getvalue())
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
537
0.288.3 by Jelmer Vernooij
Fix parent URL handling.
538
    def set_parent(self, location):
539
        cs = self.repository._git.get_config()
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
540
        remote = self._get_origin(cs)
0.408.4 by Jelmer Vernooij
Fix some tests.
541
        this_url = urlutils.split_segment_parameters(self.user_url)[0]
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
542
        target_url, branch, ref = bzr_url_to_git_url(location)
0.408.4 by Jelmer Vernooij
Fix some tests.
543
        location = urlutils.relative_url(this_url, target_url)
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
544
        cs.set((b"remote", remote), b"url", location)
545
        if branch:
7141.8.3 by Jelmer Vernooij
Review comments.
546
            cs.set((b"branch", remote), b"merge", branch_name_to_ref(branch))
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
547
        elif ref:
7141.8.3 by Jelmer Vernooij
Review comments.
548
            cs.set((b"branch", remote), b"merge", ref)
0.408.4 by Jelmer Vernooij
Fix some tests.
549
        else:
550
            # TODO(jelmer): Maybe unset rather than setting to HEAD?
7141.8.3 by Jelmer Vernooij
Review comments.
551
            cs.set((b"branch", remote), b"merge", b'HEAD')
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
552
        self._write_git_config(cs)
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
553
0.200.1411 by Jelmer Vernooij
Fix control files.
554
    def break_lock(self):
555
        raise NotImplementedError(self.break_lock)
556
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
557
    def lock_read(self):
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
558
        if self._lock_mode:
0.361.1 by Jelmer Vernooij
Don't use assert.
559
            if self._lock_mode not in ('r', 'w'):
560
                raise ValueError(self._lock_mode)
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
561
            self._lock_count += 1
562
        else:
563
            self._lock_mode = 'r'
564
            self._lock_count = 1
565
        self.repository.lock_read()
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
566
        return lock.LogicalLockResult(self.unlock)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
567
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
568
    def peek_lock_mode(self):
569
        return self._lock_mode
570
0.200.432 by Jelmer Vernooij
Support Branch.is_locked, required for loggerhead.
571
    def is_locked(self):
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
572
        return (self._lock_mode is not None)
0.200.432 by Jelmer Vernooij
Support Branch.is_locked, required for loggerhead.
573
0.383.1 by Jelmer Vernooij
Fix a bunch of locking issues.
574
    def _lock_ref(self):
575
        pass
576
577
    def _unlock_ref(self):
578
        pass
579
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
580
    def unlock(self):
0.200.1250 by Jelmer Vernooij
Simplify lock handling.
581
        """See Branch.unlock()."""
582
        if self._lock_count == 0:
0.383.1 by Jelmer Vernooij
Fix a bunch of locking issues.
583
            raise errors.LockNotHeld(self)
584
        try:
585
            self._lock_count -= 1
586
            if self._lock_count == 0:
587
                if self._lock_mode == 'w':
588
                    self._unlock_ref()
589
                self._lock_mode = None
590
                self._clear_cached_state()
591
        finally:
592
            self.repository.unlock()
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
593
594
    def get_physical_lock_status(self):
595
        return False
596
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
597
    def last_revision(self):
0.200.1675 by Jelmer Vernooij
Remove uses of decorators.
598
        with self.lock_read():
599
            # perhaps should escape this ?
600
            if self.head is None:
601
                return revision.NULL_REVISION
602
            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.
603
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
604
    def _basic_push(self, target, overwrite=False, stop_revision=None):
605
        return branch.InterBranch.get(self, target)._basic_push(
606
            overwrite, stop_revision)
607
0.252.49 by Jelmer Vernooij
Avoid trying to set HEAD for remote branches.
608
    def lookup_foreign_revision_id(self, foreign_revid):
0.200.1760 by Jelmer Vernooij
Fall back to simple revision id generation.
609
        try:
610
            return self.repository.lookup_foreign_revision_id(foreign_revid,
7143.15.2 by Jelmer Vernooij
Run autopep8.
611
                                                              self.mapping)
0.200.1760 by Jelmer Vernooij
Fall back to simple revision id generation.
612
        except KeyError:
613
            # Let's try..
614
            return self.mapping.revision_id_foreign_to_bzr(foreign_revid)
0.252.49 by Jelmer Vernooij
Avoid trying to set HEAD for remote branches.
615
0.200.1030 by Jelmer Vernooij
More work on supporting roundtripping push.
616
    def lookup_bzr_revision_id(self, revid):
617
        return self.repository.lookup_bzr_revision_id(
618
            revid, mapping=self.mapping)
619
0.200.1678 by Jelmer Vernooij
Fix tests.
620
    def get_unshelver(self, tree):
621
        raise errors.StoringUncommittedNotSupported(self)
622
0.346.1 by Jelmer Vernooij
Fix tag caching.
623
    def _clear_cached_state(self):
624
        super(GitBranch, self)._clear_cached_state()
625
        self._tag_refs = None
626
627
    def _iter_tag_refs(self, refs):
628
        """Iterate over the tag refs.
629
630
        :param refs: Refs dictionary (name -> git sha1)
631
        :return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
632
        """
633
        raise NotImplementedError(self._iter_tag_refs)
634
0.375.1 by Jelmer Vernooij
Fix remote tests, warn when fetching git->bzr and bzr->git.
635
    def get_tag_refs(self):
636
        with self.lock_read():
637
            if self._tag_refs is None:
638
                self._tag_refs = list(self._iter_tag_refs())
639
            return self._tag_refs
640
7131.14.1 by Jelmer Vernooij
Fix committing to a bzr branch bound to a git branch.
641
    def import_last_revision_info_and_tags(self, source, revno, revid,
642
                                           lossy=False):
643
        """Set the last revision info, importing from another repo if necessary.
644
645
        This is used by the bound branch code to upload a revision to
646
        the master branch first before updating the tip of the local branch.
647
        Revisions referenced by source's tags are also transferred.
648
649
        :param source: Source branch to optionally fetch from
650
        :param revno: Revision number of the new tip
651
        :param revid: Revision id of the new tip
652
        :param lossy: Whether to discard metadata that can not be
653
            natively represented
654
        :return: Tuple with the new revision number and revision id
655
            (should only be different from the arguments when lossy=True)
656
        """
7131.14.2 by Jelmer Vernooij
Make sure resulting branch is correct too.
657
        push_result = source.push(
658
            self, stop_revision=revid, lossy=lossy, _stop_revno=revno)
659
        return (push_result.new_revno, push_result.new_revid)
7131.14.1 by Jelmer Vernooij
Fix committing to a bzr branch bound to a git branch.
660
7206.6.4 by Jelmer Vernooij
Move reconcile logic to breezy.bzr.reconcile.
661
    def reconcile(self, thorough=True):
662
        """Make sure the data stored in this branch is consistent."""
663
        from ..reconcile import ReconcileResult
664
        # Nothing to do here
665
        return ReconcileResult()
666
0.200.692 by Jelmer Vernooij
Refuse pulling into non-rich-root branches rather than erroring out with an AttributeError.
667
0.200.465 by Jelmer Vernooij
Use dulwich standard functionality for finding missing revisions.
668
class LocalGitBranch(GitBranch):
669
    """A local Git branch."""
670
0.200.1648 by Jelmer Vernooij
Fix compatibility with newer versions of breezy.
671
    def __init__(self, controldir, repository, ref):
0.295.1 by Jelmer Vernooij
Split up branch formats.
672
        super(LocalGitBranch, self).__init__(controldir, repository, ref,
7143.15.2 by Jelmer Vernooij
Run autopep8.
673
                                             LocalGitBranchFormat())
0.200.763 by Jelmer Vernooij
Provide proper colocated branch support.
674
0.200.261 by Jelmer Vernooij
More formatting fixes.
675
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
7143.15.2 by Jelmer Vernooij
Run autopep8.
676
                        accelerator_tree=None, hardlink=False):
0.200.1774 by Jelmer Vernooij
Simplify checkout creation.
677
        t = transport.get_transport(to_location)
678
        t.ensure_base()
679
        format = self._get_checkout_format(lightweight=lightweight)
680
        checkout = format.initialize_on_transport(t)
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
681
        if lightweight:
0.200.1774 by Jelmer Vernooij
Simplify checkout creation.
682
            from_branch = checkout.set_branch_reference(target_branch=self)
683
        else:
7143.15.14 by Jelmer Vernooij
Fix repo acquisition.
684
            policy = checkout.determine_repository_policy()
685
            policy.acquire_repository()
0.200.1774 by Jelmer Vernooij
Simplify checkout creation.
686
            checkout_branch = checkout.create_branch()
687
            checkout_branch.bind(self)
688
            checkout_branch.pull(self, stop_revision=revision_id)
689
            from_branch = None
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
690
        return checkout.create_workingtree(
691
            revision_id, from_branch=from_branch, hardlink=hardlink)
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
692
0.383.1 by Jelmer Vernooij
Fix a bunch of locking issues.
693
    def _lock_ref(self):
694
        self._ref_lock = self.repository._git.refs.lock_ref(self.ref)
695
696
    def _unlock_ref(self):
697
        self._ref_lock.unlock()
698
0.412.1 by Jelmer Vernooij
Implement Branch.break_lock.
699
    def break_lock(self):
700
        self.repository._git.refs.unlock_ref(self.ref)
701
0.200.1493 by Jelmer Vernooij
Test fixes.
702
    def fetch(self, from_branch, last_revision=None, limit=None):
703
        return branch.InterBranch.get(from_branch, self).fetch(
704
            stop_revision=last_revision, limit=limit)
705
0.200.57 by Jelmer Vernooij
Fix more tests.
706
    def _gen_revision_history(self):
0.200.58 by Jelmer Vernooij
Fix remaining tests.
707
        if self.head is None:
708
            return []
6968.1.1 by Jelmer Vernooij
Support 'bzr version-info' in horizon branches.
709
        last_revid = self.last_revision()
0.200.1279 by Jelmer Vernooij
Avoid using deprecated Repository.iter_reverse_revision_history.
710
        graph = self.repository.get_graph()
6968.1.1 by Jelmer Vernooij
Support 'bzr version-info' in horizon branches.
711
        try:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
712
            ret = list(graph.iter_lefthand_ancestry(
713
                last_revid, (revision.NULL_REVISION, )))
6968.1.1 by Jelmer Vernooij
Support 'bzr version-info' in horizon branches.
714
        except errors.RevisionNotPresent as e:
715
            raise errors.GhostRevisionsHaveNoRevno(last_revid, e.revision_id)
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
716
        ret.reverse()
0.200.57 by Jelmer Vernooij
Fix more tests.
717
        return ret
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
718
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
719
    def _get_head(self):
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
720
        try:
0.336.2 by Jelmer Vernooij
Don't use ZERO_SHA outside of remote communication to indicate empty history.
721
            return self.repository._git.refs[self.ref]
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
722
        except KeyError:
723
            return None
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
724
0.200.1228 by Jelmer Vernooij
Provide Branch._read_last_revision_info.
725
    def _read_last_revision_info(self):
726
        last_revid = self.last_revision()
727
        graph = self.repository.get_graph()
7143.8.1 by Jelmer Vernooij
Cope with the revno not being known.
728
        try:
7143.15.6 by Jelmer Vernooij
Merge trunk.
729
            revno = graph.find_distance_to_null(
730
                last_revid, [(revision.NULL_REVISION, 0)])
7143.8.1 by Jelmer Vernooij
Cope with the revno not being known.
731
        except errors.GhostRevisionsHaveNoRevno:
732
            revno = None
0.200.1228 by Jelmer Vernooij
Provide Branch._read_last_revision_info.
733
        return revno, last_revid
734
735
    def set_last_revision_info(self, revno, revision_id):
736
        self.set_last_revision(revision_id)
737
        self._last_revision_info_cache = revno, revision_id
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
738
739
    def set_last_revision(self, revid):
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
740
        if not revid or not isinstance(revid, bytes):
0.200.1233 by Jelmer Vernooij
Implement Repository.iter_files_bytes.
741
            raise errors.InvalidRevisionId(revision_id=revid, branch=self)
0.200.1218 by Jelmer Vernooij
Support set_last_revision('null:').
742
        if revid == NULL_REVISION:
0.336.2 by Jelmer Vernooij
Don't use ZERO_SHA outside of remote communication to indicate empty history.
743
            newhead = None
0.200.1218 by Jelmer Vernooij
Support set_last_revision('null:').
744
        else:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
745
            (newhead, self.mapping) = self.repository.lookup_bzr_revision_id(
746
                revid)
0.200.1218 by Jelmer Vernooij
Support set_last_revision('null:').
747
            if self.mapping is None:
748
                raise AssertionError
749
        self._set_head(newhead)
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
750
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
751
    def _set_head(self, value):
0.361.1 by Jelmer Vernooij
Don't use assert.
752
        if value == ZERO_SHA:
753
            raise ValueError(value)
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
754
        self._head = value
0.336.2 by Jelmer Vernooij
Don't use ZERO_SHA outside of remote communication to indicate empty history.
755
        if value is None:
756
            del self.repository._git.refs[self.ref]
757
        else:
758
            self.repository._git.refs[self.ref] = self._head
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
759
        self._clear_cached_state()
760
761
    head = property(_get_head, _set_head)
762
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
763
    def get_push_location(self):
764
        """See Branch.get_push_location."""
0.200.1584 by Jelmer Vernooij
Use config stacks in a few more places.
765
        push_loc = self.get_config_stack().get('push_location')
7141.8.1 by Jelmer Vernooij
Read parent branch properly from git config.
766
        if push_loc is not None:
767
            return push_loc
768
        cs = self.repository._git.get_config_stack()
769
        return self._get_related_push_branch(cs)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
770
771
    def set_push_location(self, location):
772
        """See Branch.set_push_location."""
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
773
        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.
774
                                          store=config.STORE_LOCATION)
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
775
776
    def supports_tags(self):
0.200.82 by Jelmer Vernooij
Support listing tags.
777
        return True
0.200.956 by Jelmer Vernooij
Add some more format tests.
778
0.200.1681 by Jelmer Vernooij
Provide Branch.store_uncommitted.
779
    def store_uncommitted(self, creator):
780
        raise errors.StoringUncommittedNotSupported(self)
781
0.346.1 by Jelmer Vernooij
Fix tag caching.
782
    def _iter_tag_refs(self):
783
        """Iterate over the tag refs.
784
785
        :param refs: Refs dictionary (name -> git sha1)
786
        :return: iterator over (ref_name, tag_name, peeled_sha1, unpeeled_sha1)
787
        """
788
        refs = self.repository._git.refs
7018.3.2 by Jelmer Vernooij
Fix some git tests.
789
        for ref_name, unpeeled in viewitems(refs.as_dict()):
0.346.1 by Jelmer Vernooij
Fix tag caching.
790
            try:
791
                tag_name = ref_to_tag_name(ref_name)
792
            except (ValueError, UnicodeDecodeError):
793
                continue
794
            peeled = refs.get_peeled(ref_name)
795
            if peeled is None:
0.382.1 by Jelmer Vernooij
Various fixes for annotated tags and symrefs.
796
                peeled = unpeeled
6973.6.2 by Jelmer Vernooij
Fix more tests.
797
            if not isinstance(tag_name, text_type):
0.361.1 by Jelmer Vernooij
Don't use assert.
798
                raise TypeError(tag_name)
0.346.1 by Jelmer Vernooij
Fix tag caching.
799
            yield (ref_name, tag_name, peeled, unpeeled)
800
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
801
    def create_memorytree(self):
802
        from .memorytree import GitMemoryTree
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
803
        return GitMemoryTree(self, self.repository._git.object_store,
804
                             self.head)
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
805
7350.2.1 by Jelmer Vernooij
Drop file_id return value from Tree.iter_references.
806
    def reference_parent(self, path, possible_transports=None):
807
        """Return the parent branch for a tree-reference.
0.429.10 by Jelmer Vernooij
use new read_submodule_head from dulwich.
808
7350.2.1 by Jelmer Vernooij
Drop file_id return value from Tree.iter_references.
809
        :param path: The path of the nested tree in the tree
810
        :return: A branch associated with the nested tree
0.429.10 by Jelmer Vernooij
use new read_submodule_head from dulwich.
811
        """
812
        # FIXME should provide multiple branches, based on config
0.429.18 by Jelmer Vernooij
Fix reference_parent tets.
813
        url = urlutils.join(self.user_url, path)
814
        return branch.Branch.open(
7143.15.2 by Jelmer Vernooij
Run autopep8.
815
            url,
816
            possible_transports=possible_transports)
0.429.10 by Jelmer Vernooij
use new read_submodule_head from dulwich.
817
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
818
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
819
def _quick_lookup_revno(local_branch, remote_branch, revid):
6973.13.2 by Jelmer Vernooij
Fix some more tests.
820
    if not isinstance(revid, bytes):
0.361.1 by Jelmer Vernooij
Don't use assert.
821
        raise TypeError(revid)
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
822
    # Try in source branch first, it'll be faster
0.200.1788 by Jelmer Vernooij
Use context managers.
823
    with local_branch.lock_read():
7339.1.6 by Jelmer Vernooij
Also support revno-less push/pull operations.
824
        if not _calculate_revnos(local_branch):
825
            return None
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
826
        try:
0.200.1362 by Jelmer Vernooij
Add locking.
827
            return local_branch.revision_id_to_revno(revid)
828
        except errors.NoSuchRevision:
829
            graph = local_branch.repository.get_graph()
830
            try:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
831
                return graph.find_distance_to_null(
832
                    revid, [(revision.NULL_REVISION, 0)])
0.200.1362 by Jelmer Vernooij
Add locking.
833
            except errors.GhostRevisionsHaveNoRevno:
7339.1.6 by Jelmer Vernooij
Also support revno-less push/pull operations.
834
                if not _calculate_revnos(remote_branch):
835
                    return None
0.200.1362 by Jelmer Vernooij
Add locking.
836
                # FIXME: Check using graph.find_distance_to_null() ?
0.200.1788 by Jelmer Vernooij
Use context managers.
837
                with remote_branch.lock_read():
0.200.1362 by Jelmer Vernooij
Add locking.
838
                    return remote_branch.revision_id_to_revno(revid)
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
839
840
0.200.342 by Jelmer Vernooij
Report git sha during pull.
841
class GitBranchPullResult(branch.PullResult):
842
0.252.36 by Jelmer Vernooij
Fix pull.
843
    def __init__(self):
844
        super(GitBranchPullResult, self).__init__()
845
        self.new_git_head = None
846
        self._old_revno = None
847
        self._new_revno = None
848
0.200.342 by Jelmer Vernooij
Report git sha during pull.
849
    def report(self, to_file):
850
        if not is_quiet():
851
            if self.old_revid == self.new_revid:
852
                to_file.write('No revisions to pull.\n')
0.200.728 by Jelmer Vernooij
Fix pulling when all revisions are already in the repo.
853
            elif self.new_git_head is not None:
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
854
                to_file.write('Now on revision %d (git sha: %s).\n' %
7143.15.2 by Jelmer Vernooij
Run autopep8.
855
                              (self.new_revno, self.new_git_head))
0.200.728 by Jelmer Vernooij
Fix pulling when all revisions are already in the repo.
856
            else:
857
                to_file.write('Now on revision %d.\n' % (self.new_revno,))
0.200.342 by Jelmer Vernooij
Report git sha during pull.
858
        self._show_tag_conficts(to_file)
859
0.252.36 by Jelmer Vernooij
Fix pull.
860
    def _lookup_revno(self, revid):
0.200.1185 by Jelmer Vernooij
Some formatting fixes.
861
        return _quick_lookup_revno(self.target_branch, self.source_branch,
7143.15.2 by Jelmer Vernooij
Run autopep8.
862
                                   revid)
0.252.36 by Jelmer Vernooij
Fix pull.
863
864
    def _get_old_revno(self):
865
        if self._old_revno is not None:
866
            return self._old_revno
867
        return self._lookup_revno(self.old_revid)
868
869
    def _set_old_revno(self, revno):
870
        self._old_revno = revno
871
872
    old_revno = property(_get_old_revno, _set_old_revno)
873
874
    def _get_new_revno(self):
875
        if self._new_revno is not None:
876
            return self._new_revno
877
        return self._lookup_revno(self.new_revid)
878
879
    def _set_new_revno(self, revno):
880
        self._new_revno = revno
0.200.956 by Jelmer Vernooij
Add some more format tests.
881
0.252.36 by Jelmer Vernooij
Fix pull.
882
    new_revno = property(_get_new_revno, _set_new_revno)
883
0.200.342 by Jelmer Vernooij
Report git sha during pull.
884
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
885
class GitBranchPushResult(branch.BranchPushResult):
886
887
    def _lookup_revno(self, revid):
0.200.1185 by Jelmer Vernooij
Some formatting fixes.
888
        return _quick_lookup_revno(self.source_branch, self.target_branch,
7143.15.2 by Jelmer Vernooij
Run autopep8.
889
                                   revid)
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
890
891
    @property
892
    def old_revno(self):
893
        return self._lookup_revno(self.old_revid)
894
895
    @property
896
    def new_revno(self):
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
897
        new_original_revno = getattr(self, "new_original_revno", None)
898
        if new_original_revno:
899
            return new_original_revno
900
        if getattr(self, "new_original_revid", None) is not None:
901
            return self._lookup_revno(self.new_original_revid)
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
902
        return self._lookup_revno(self.new_revid)
903
904
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
905
class InterFromGitBranch(branch.GenericInterBranch):
0.200.261 by Jelmer Vernooij
More formatting fixes.
906
    """InterBranch implementation that pulls from Git into bzr."""
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
907
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
908
    @staticmethod
909
    def _get_branch_formats_to_test():
0.200.1100 by Jelmer Vernooij
Provide test combinations for InterBranch implementations.
910
        try:
911
            default_format = branch.format_registry.get_default()
912
        except AttributeError:
913
            default_format = branch.BranchFormat._default_format
0.295.4 by Jelmer Vernooij
Fix imports.
914
        from .remote import RemoteGitBranchFormat
0.200.1100 by Jelmer Vernooij
Provide test combinations for InterBranch implementations.
915
        return [
0.295.1 by Jelmer Vernooij
Split up branch formats.
916
            (RemoteGitBranchFormat(), default_format),
917
            (LocalGitBranchFormat(), default_format)]
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
918
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
919
    @classmethod
0.200.692 by Jelmer Vernooij
Refuse pulling into non-rich-root branches rather than erroring out with an AttributeError.
920
    def _get_interrepo(self, source, target):
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
921
        return _mod_repository.InterRepository.get(
922
            source.repository, target.repository)
0.200.692 by Jelmer Vernooij
Refuse pulling into non-rich-root branches rather than erroring out with an AttributeError.
923
924
    @classmethod
925
    def is_compatible(cls, source, target):
0.200.1222 by Jelmer Vernooij
Better checks in is_compatible methods.
926
        if not isinstance(source, GitBranch):
927
            return False
928
        if isinstance(target, GitBranch):
929
            # InterLocalGitRemoteGitBranch or InterToGitBranch should be used
930
            return False
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
931
        if (getattr(cls._get_interrepo(source, target), "fetch_objects", None)
932
                is None):
0.200.1222 by Jelmer Vernooij
Better checks in is_compatible methods.
933
            # fetch_objects is necessary for this to work
934
            return False
935
        return True
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
936
0.200.1305 by Jelmer Vernooij
Only actually fetch tags if "branch.fetch_tags" is set to true.
937
    def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
0.200.1265 by Jelmer Vernooij
Support limit option to Branch.fetch.
938
        self.fetch_objects(stop_revision, fetch_tags=fetch_tags, limit=limit)
0.260.1 by Jelmer Vernooij
Fix fetch from remote during merge.
939
0.200.1265 by Jelmer Vernooij
Support limit option to Branch.fetch.
940
    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.
941
        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.
942
        if fetch_tags is None:
0.200.1584 by Jelmer Vernooij
Use config stacks in a few more places.
943
            c = self.source.get_config_stack()
944
            fetch_tags = c.get('branch.fetch_tags')
7143.15.2 by Jelmer Vernooij
Run autopep8.
945
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
946
        def determine_wants(heads):
0.259.6 by Jelmer Vernooij
Fetch tags during pull.
947
            if stop_revision is None:
0.336.6 by Jelmer Vernooij
Fix pull tests.
948
                try:
0.200.917 by Jelmer Vernooij
Cope with implicit branches during pull.
949
                    head = heads[self.source.ref]
0.336.6 by Jelmer Vernooij
Fix pull tests.
950
                except KeyError:
951
                    self._last_revid = revision.NULL_REVISION
0.200.917 by Jelmer Vernooij
Cope with implicit branches during pull.
952
                else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
953
                    self._last_revid = self.source.lookup_foreign_revision_id(
954
                        head)
0.259.6 by Jelmer Vernooij
Fetch tags during pull.
955
            else:
956
                self._last_revid = stop_revision
957
            real = interrepo.get_determine_wants_revids(
0.260.1 by Jelmer Vernooij
Fix fetch from remote during merge.
958
                [self._last_revid], include_tags=fetch_tags)
0.259.6 by Jelmer Vernooij
Fetch tags during pull.
959
            return real(heads)
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
960
        pack_hint, head, refs = interrepo.fetch_objects(
0.200.1265 by Jelmer Vernooij
Support limit option to Branch.fetch.
961
            determine_wants, self.source.mapping, limit=limit)
0.252.45 by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr.
962
        if (pack_hint is not None and
7143.15.2 by Jelmer Vernooij
Run autopep8.
963
                self.target.repository._format.pack_compresses):
0.248.5 by Jelmer Vernooij
Reformatting, fix dpush.
964
            self.target.repository.pack(hint=pack_hint)
0.260.1 by Jelmer Vernooij
Fix fetch from remote during merge.
965
        return head, refs
966
0.200.1219 by Jelmer Vernooij
Remove InterBranch.update_revisions.
967
    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.
968
        head, refs = self.fetch_objects(stop_revision, fetch_tags=None)
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
969
        if overwrite:
0.200.314 by Jelmer Vernooij
Support stop_revision.
970
            prev_last_revid = None
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
971
        else:
0.200.314 by Jelmer Vernooij
Support stop_revision.
972
            prev_last_revid = self.target.last_revision()
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
973
        self.target.generate_revision_history(
974
            self._last_revid, last_rev=prev_last_revid,
975
            other_branch=self.source)
0.200.1062 by Jelmer Vernooij
Pass remote refs along in _update_revisions.
976
        return head, refs
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
977
0.200.1423 by Jelmer Vernooij
Fix space.
978
    def _basic_pull(self, stop_revision, overwrite, run_hooks,
7143.15.2 by Jelmer Vernooij
Run autopep8.
979
                    _override_hook_target, _hook_master):
0.343.1 by Jelmer Vernooij
Support overwrite flag properly, remove DictTagDict.
980
        if overwrite is True:
981
            overwrite = set(["history", "tags"])
7142.3.2 by Jelmer Vernooij
Fix overwrite for git branches.
982
        elif not overwrite:
0.343.1 by Jelmer Vernooij
Support overwrite flag properly, remove DictTagDict.
983
            overwrite = set()
0.200.342 by Jelmer Vernooij
Report git sha during pull.
984
        result = GitBranchPullResult()
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
985
        result.source_branch = self.source
986
        if _override_hook_target is None:
987
            result.target_branch = self.target
988
        else:
989
            result.target_branch = _override_hook_target
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
990
        with self.target.lock_write(), self.source.lock_read():
991
            # We assume that during 'pull' the target repository is closer than
992
            # the source one.
993
            (result.old_revno, result.old_revid) = \
994
                self.target.last_revision_info()
995
            result.new_git_head, remote_refs = self._update_revisions(
0.343.1 by Jelmer Vernooij
Support overwrite flag properly, remove DictTagDict.
996
                stop_revision, overwrite=("history" in overwrite))
7143.15.2 by Jelmer Vernooij
Run autopep8.
997
            tags_ret = self.source.tags.merge_to(
998
                self.target.tags, ("tags" in overwrite), ignore_master=True)
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
999
            if isinstance(tags_ret, tuple):
1000
                result.tag_updates, result.tag_conflicts = tags_ret
1001
            else:
1002
                result.tag_conflicts = tags_ret
1003
            (result.new_revno, result.new_revid) = \
1004
                self.target.last_revision_info()
1005
            if _hook_master:
1006
                result.master_branch = _hook_master
1007
                result.local_branch = result.target_branch
1008
            else:
1009
                result.master_branch = result.target_branch
1010
                result.local_branch = None
1011
            if run_hooks:
1012
                for hook in branch.Branch.hooks['post_pull']:
1013
                    hook(result)
1014
            return result
0.200.1414 by Jelmer Vernooij
Fix pulling into bound branches.
1015
1016
    def pull(self, overwrite=False, stop_revision=None,
1017
             possible_transports=None, _hook_master=None, run_hooks=True,
1018
             _override_hook_target=None, local=False):
1019
        """See Branch.pull.
1020
1021
        :param _hook_master: Private parameter - set the branch to
1022
            be supplied as the master to pull hooks.
1023
        :param run_hooks: Private parameter - if false, this branch
1024
            is being called because it's the master of the primary branch,
1025
            so it should not run its hooks.
1026
        :param _override_hook_target: Private parameter - set the branch to be
1027
            supplied as the target_branch to pull hooks.
1028
        """
1029
        # This type of branch can't be bound.
1030
        bound_location = self.target.get_bound_location()
1031
        if local and not bound_location:
1032
            raise errors.LocalRequiresBoundBranch()
1033
        master_branch = None
1034
        source_is_master = False
1035
        self.source.lock_read()
1036
        if bound_location:
1037
            # bound_location comes from a config file, some care has to be
1038
            # taken to relate it to source.user_url
1039
            normalized = urlutils.normalize_url(bound_location)
1040
            try:
1041
                relpath = self.source.user_transport.relpath(normalized)
1042
                source_is_master = (relpath == '')
0.200.1660 by Jelmer Vernooij
Fix imports.
1043
            except (errors.PathNotChild, urlutils.InvalidURL):
0.200.1414 by Jelmer Vernooij
Fix pulling into bound branches.
1044
                source_is_master = False
1045
        if not local and bound_location and not source_is_master:
1046
            # not pulling from master, so we need to update master.
1047
            master_branch = self.target.get_master_branch(possible_transports)
1048
            master_branch.lock_write()
1049
        try:
1050
            try:
1051
                if master_branch:
1052
                    # pull from source into master.
1053
                    master_branch.pull(self.source, overwrite, stop_revision,
7143.15.2 by Jelmer Vernooij
Run autopep8.
1054
                                       run_hooks=False)
0.200.1414 by Jelmer Vernooij
Fix pulling into bound branches.
1055
                result = self._basic_pull(stop_revision, overwrite, run_hooks,
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1056
                                          _override_hook_target,
1057
                                          _hook_master=master_branch)
0.200.1414 by Jelmer Vernooij
Fix pulling into bound branches.
1058
            finally:
1059
                self.source.unlock()
1060
        finally:
1061
            if master_branch:
1062
                master_branch.unlock()
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
1063
        return result
1064
0.331.1 by Jelmer Vernooij
Fix some tags tests.
1065
    def _basic_push(self, overwrite, stop_revision):
1066
        if overwrite is True:
1067
            overwrite = set(["history", "tags"])
7142.3.2 by Jelmer Vernooij
Fix overwrite for git branches.
1068
        elif not overwrite:
0.331.1 by Jelmer Vernooij
Fix some tags tests.
1069
            overwrite = set()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
1070
        result = branch.BranchPushResult()
1071
        result.source_branch = self.source
1072
        result.target_branch = self.target
1073
        result.old_revno, result.old_revid = self.target.last_revision_info()
0.200.1219 by Jelmer Vernooij
Remove InterBranch.update_revisions.
1074
        result.new_git_head, remote_refs = self._update_revisions(
0.331.1 by Jelmer Vernooij
Fix some tags tests.
1075
            stop_revision, overwrite=("history" in overwrite))
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1076
        tags_ret = self.source.tags.merge_to(
1077
            self.target.tags, "tags" in overwrite, ignore_master=True)
0.331.1 by Jelmer Vernooij
Fix some tags tests.
1078
        (result.tag_updates, result.tag_conflicts) = tags_ret
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
1079
        result.new_revno, result.new_revid = self.target.last_revision_info()
1080
        return result
1081
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
1082
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1083
class InterGitBranch(branch.GenericInterBranch):
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
1084
    """InterBranch implementation that pulls between Git branches."""
1085
0.200.1493 by Jelmer Vernooij
Test fixes.
1086
    def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
1087
        raise NotImplementedError(self.fetch)
1088
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1089
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
1090
class InterLocalGitRemoteGitBranch(InterGitBranch):
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1091
    """InterBranch that copies from a local to a remote git branch."""
1092
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
1093
    @staticmethod
1094
    def _get_branch_formats_to_test():
0.295.4 by Jelmer Vernooij
Fix imports.
1095
        from .remote import RemoteGitBranchFormat
0.295.3 by Jelmer Vernooij
Fix some more tests.
1096
        return [
1097
            (LocalGitBranchFormat(), RemoteGitBranchFormat())]
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
1098
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1099
    @classmethod
1100
    def is_compatible(self, source, target):
0.200.1644 by Jelmer Vernooij
More relative imports.
1101
        from .remote import RemoteGitBranch
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
1102
        return (isinstance(source, LocalGitBranch) and
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1103
                isinstance(target, RemoteGitBranch))
1104
0.331.1 by Jelmer Vernooij
Fix some tags tests.
1105
    def _basic_push(self, overwrite, stop_revision):
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1106
        result = GitBranchPushResult()
1107
        result.source_branch = self.source
1108
        result.target_branch = self.target
1109
        if stop_revision is None:
1110
            stop_revision = self.source.last_revision()
7143.15.2 by Jelmer Vernooij
Run autopep8.
1111
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1112
        def get_changed_refs(old_refs):
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
1113
            old_ref = old_refs.get(self.target.ref, None)
1114
            if old_ref is None:
1115
                result.old_revid = revision.NULL_REVISION
1116
            else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
1117
                result.old_revid = self.target.lookup_foreign_revision_id(
1118
                    old_ref)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1119
            new_ref = self.source.repository.lookup_bzr_revision_id(
1120
                stop_revision)[0]
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
1121
            if not overwrite:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1122
                if remote_divergence(
1123
                        old_ref, new_ref,
1124
                        self.source.repository._git.object_store):
0.404.5 by Jelmer Vernooij
Check for diverged branches during push.
1125
                    raise errors.DivergedBranches(self.source, self.target)
7143.15.2 by Jelmer Vernooij
Run autopep8.
1126
            refs = {self.target.ref: new_ref}
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1127
            result.new_revid = stop_revision
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1128
            for name, sha in viewitems(
1129
                    self.source.repository._git.refs.as_dict(b"refs/tags")):
0.200.875 by Jelmer Vernooij
Use new tag_name_to_ref function.
1130
                refs[tag_name_to_ref(name)] = sha
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1131
            return refs
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1132
        self.target.repository.send_pack(
1133
            get_changed_refs,
1134
            self.source.repository._git.object_store.generate_pack_data)
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1135
        return result
1136
1137
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
1138
class InterGitLocalGitBranch(InterGitBranch):
0.200.512 by Jelmer Vernooij
Support pushing git->git.
1139
    """InterBranch that copies from a remote to a local git branch."""
1140
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
1141
    @staticmethod
1142
    def _get_branch_formats_to_test():
0.295.4 by Jelmer Vernooij
Fix imports.
1143
        from .remote import RemoteGitBranchFormat
0.295.3 by Jelmer Vernooij
Fix some more tests.
1144
        return [
1145
            (RemoteGitBranchFormat(), LocalGitBranchFormat()),
1146
            (LocalGitBranchFormat(), LocalGitBranchFormat())]
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
1147
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
1148
    @classmethod
1149
    def is_compatible(self, source, target):
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
1150
        return (isinstance(source, GitBranch) and
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
1151
                isinstance(target, LocalGitBranch))
1152
0.200.1534 by Jelmer Vernooij
Implement fetch between git branches, encode nicks.
1153
    def fetch(self, stop_revision=None, fetch_tags=None, limit=None):
1154
        interrepo = _mod_repository.InterRepository.get(self.source.repository,
7143.15.2 by Jelmer Vernooij
Run autopep8.
1155
                                                        self.target.repository)
0.200.1534 by Jelmer Vernooij
Implement fetch between git branches, encode nicks.
1156
        if stop_revision is None:
1157
            stop_revision = self.source.last_revision()
1158
        determine_wants = interrepo.get_determine_wants_revids(
1159
            [stop_revision], include_tags=fetch_tags)
1160
        interrepo.fetch_objects(determine_wants, limit=limit)
1161
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
1162
    def _basic_push(self, overwrite=False, stop_revision=None):
0.343.1 by Jelmer Vernooij
Support overwrite flag properly, remove DictTagDict.
1163
        if overwrite is True:
1164
            overwrite = set(["history", "tags"])
7142.3.2 by Jelmer Vernooij
Fix overwrite for git branches.
1165
        elif not overwrite:
0.343.1 by Jelmer Vernooij
Support overwrite flag properly, remove DictTagDict.
1166
            overwrite = set()
0.200.1325 by Jelmer Vernooij
More test fixes.
1167
        result = GitBranchPushResult()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
1168
        result.source_branch = self.source
1169
        result.target_branch = self.target
1170
        result.old_revid = self.target.last_revision()
1171
        refs, stop_revision = self.update_refs(stop_revision)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1172
        self.target.generate_revision_history(
1173
            stop_revision,
1174
            (result.old_revid if ("history" not in overwrite) else None),
1175
            other_branch=self.source)
1176
        tags_ret = self.source.tags.merge_to(
1177
            self.target.tags,
1178
            source_tag_refs=remote_refs_dict_to_tag_refs(refs),
1179
            overwrite=("tags" in overwrite))
0.200.1402 by Jelmer Vernooij
Cope with tag changes in bzr.
1180
        if isinstance(tags_ret, tuple):
1181
            (result.tag_updates, result.tag_conflicts) = tags_ret
1182
        else:
1183
            result.tag_conflicts = tags_ret
0.200.505 by Jelmer Vernooij
Remove duplicate code.
1184
        result.new_revid = self.target.last_revision()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
1185
        return result
1186
1187
    def update_refs(self, stop_revision=None):
0.296.1 by Jelmer Vernooij
Fix tag fetching.
1188
        interrepo = _mod_repository.InterRepository.get(
7143.15.2 by Jelmer Vernooij
Run autopep8.
1189
            self.source.repository, self.target.repository)
0.296.1 by Jelmer Vernooij
Fix tag fetching.
1190
        c = self.source.get_config_stack()
1191
        fetch_tags = c.get('branch.fetch_tags')
1192
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
1193
        if stop_revision is None:
7141.5.1 by Jelmer Vernooij
Fix fetching between non-default git branches.
1194
            refs = interrepo.fetch(branches=[self.source.ref], include_tags=fetch_tags)
0.336.5 by Jelmer Vernooij
Fix some tests.
1195
            try:
7141.5.1 by Jelmer Vernooij
Fix fetching between non-default git branches.
1196
                head = refs[self.source.ref]
0.336.5 by Jelmer Vernooij
Fix some tests.
1197
            except KeyError:
1198
                stop_revision = revision.NULL_REVISION
1199
            else:
1200
                stop_revision = self.target.lookup_foreign_revision_id(head)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
1201
        else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
1202
            refs = interrepo.fetch(
1203
                revision_id=stop_revision, include_tags=fetch_tags)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
1204
        return refs, stop_revision
1205
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
1206
    def pull(self, stop_revision=None, overwrite=False,
0.296.1 by Jelmer Vernooij
Fix tag fetching.
1207
             possible_transports=None, run_hooks=True, local=False):
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
1208
        # This type of branch can't be bound.
1209
        if local:
1210
            raise errors.LocalRequiresBoundBranch()
0.343.1 by Jelmer Vernooij
Support overwrite flag properly, remove DictTagDict.
1211
        if overwrite is True:
1212
            overwrite = set(["history", "tags"])
7142.3.2 by Jelmer Vernooij
Fix overwrite for git branches.
1213
        elif not overwrite:
0.343.1 by Jelmer Vernooij
Support overwrite flag properly, remove DictTagDict.
1214
            overwrite = set()
1215
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
1216
        result = GitPullResult()
1217
        result.source_branch = self.source
1218
        result.target_branch = self.target
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
1219
        with self.target.lock_write(), self.source.lock_read():
1220
            result.old_revid = self.target.last_revision()
1221
            refs, stop_revision = self.update_refs(stop_revision)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1222
            self.target.generate_revision_history(
1223
                stop_revision,
1224
                (result.old_revid if ("history" not in overwrite) else None),
1225
                other_branch=self.source)
1226
            tags_ret = self.source.tags.merge_to(
1227
                self.target.tags, overwrite=("tags" in overwrite),
1228
                source_tag_refs=remote_refs_dict_to_tag_refs(refs))
0.200.1670 by Jelmer Vernooij
Fix compatibility with newer versions of bzr.
1229
            if isinstance(tags_ret, tuple):
1230
                (result.tag_updates, result.tag_conflicts) = tags_ret
1231
            else:
1232
                result.tag_conflicts = tags_ret
1233
            result.new_revid = self.target.last_revision()
1234
            result.local_branch = None
1235
            result.master_branch = result.target_branch
1236
            if run_hooks:
1237
                for hook in branch.Branch.hooks['post_pull']:
1238
                    hook(result)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
1239
        return result
1240
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
1241
0.200.960 by Jelmer Vernooij
Use GenericInterBranch.
1242
class InterToGitBranch(branch.GenericInterBranch):
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1243
    """InterBranch implementation that pulls into a Git branch."""
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
1244
0.200.939 by Jelmer Vernooij
Use InterRepo directly.
1245
    def __init__(self, source, target):
1246
        super(InterToGitBranch, self).__init__(source, target)
0.200.1097 by Jelmer Vernooij
Implement GitBranchFormat.initialize.
1247
        self.interrepo = _mod_repository.InterRepository.get(source.repository,
7143.15.2 by Jelmer Vernooij
Run autopep8.
1248
                                                             target.repository)
0.200.939 by Jelmer Vernooij
Use InterRepo directly.
1249
0.200.631 by Jelmer Vernooij
Raise proper exception in Branch.get_stacked_on_url().
1250
    @staticmethod
1251
    def _get_branch_formats_to_test():
0.200.1100 by Jelmer Vernooij
Provide test combinations for InterBranch implementations.
1252
        try:
1253
            default_format = branch.format_registry.get_default()
1254
        except AttributeError:
1255
            default_format = branch.BranchFormat._default_format
0.295.4 by Jelmer Vernooij
Fix imports.
1256
        from .remote import RemoteGitBranchFormat
0.295.1 by Jelmer Vernooij
Split up branch formats.
1257
        return [
1258
            (default_format, LocalGitBranchFormat()),
1259
            (default_format, RemoteGitBranchFormat())]
0.200.631 by Jelmer Vernooij
Raise proper exception in Branch.get_stacked_on_url().
1260
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
1261
    @classmethod
1262
    def is_compatible(self, source, target):
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
1263
        return (not isinstance(source, GitBranch) and
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
1264
                isinstance(target, GitBranch))
1265
7131.14.1 by Jelmer Vernooij
Fix committing to a bzr branch bound to a git branch.
1266
    def _get_new_refs(self, stop_revision=None, fetch_tags=None,
1267
                      stop_revno=None):
0.361.1 by Jelmer Vernooij
Don't use assert.
1268
        if not self.source.is_locked():
1269
            raise errors.ObjectNotLocked(self.source)
0.252.38 by Jelmer Vernooij
Minor cleanups.
1270
        if stop_revision is None:
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
1271
            (stop_revno, stop_revision) = self.source.last_revision_info()
7131.14.1 by Jelmer Vernooij
Fix committing to a bzr branch bound to a git branch.
1272
        elif stop_revno is None:
7296.4.1 by Jelmer Vernooij
Fix pushing non-mainline revisions from bzr to git.
1273
            try:
1274
                stop_revno = self.source.revision_id_to_revno(stop_revision)
1275
            except errors.NoSuchRevision:
1276
                stop_revno = None
6973.13.2 by Jelmer Vernooij
Fix some more tests.
1277
        if not isinstance(stop_revision, bytes):
0.361.1 by Jelmer Vernooij
Don't use assert.
1278
            raise TypeError(stop_revision)
0.377.1 by Jelmer Vernooij
Fix some remote operations and add more tests.
1279
        main_ref = self.target.ref
7143.15.2 by Jelmer Vernooij
Run autopep8.
1280
        refs = {main_ref: (None, stop_revision)}
0.200.1363 by Jelmer Vernooij
Only fetch tags if requested in config.
1281
        if fetch_tags is None:
0.200.1584 by Jelmer Vernooij
Use config stacks in a few more places.
1282
            c = self.source.get_config_stack()
1283
            fetch_tags = c.get('branch.fetch_tags')
7018.3.2 by Jelmer Vernooij
Fix some git tests.
1284
        for name, revid in viewitems(self.source.tags.get_tag_dict()):
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1285
            if self.source.repository.has_revision(revid):
1286
                ref = tag_name_to_ref(name)
1287
                if not check_ref_format(ref):
1288
                    warning("skipping tag with invalid characters %s (%s)",
7143.15.2 by Jelmer Vernooij
Run autopep8.
1289
                            name, ref)
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1290
                    continue
0.200.1398 by Jelmer Vernooij
Make GitSmartRemoteNotSupported derive from UnsupportedOperation.
1291
                if fetch_tags:
1292
                    # FIXME: Skip tags that are not in the ancestry
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1293
                    refs[ref] = (None, revid)
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
1294
        return refs, main_ref, (stop_revno, stop_revision)
0.252.37 by Jelmer Vernooij
Factor out some common code for finding refs to send.
1295
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1296
    def _update_refs(self, result, old_refs, new_refs, overwrite):
1297
        mutter("updating refs. old refs: %r, new refs: %r",
1298
               old_refs, new_refs)
1299
        result.tag_updates = {}
1300
        result.tag_conflicts = []
1301
        ret = dict(old_refs)
7143.15.2 by Jelmer Vernooij
Run autopep8.
1302
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1303
        def ref_equals(refs, ref, git_sha, revid):
1304
            try:
1305
                value = refs[ref]
1306
            except KeyError:
1307
                return False
1308
            if (value[0] is not None and
1309
                git_sha is not None and
7143.15.2 by Jelmer Vernooij
Run autopep8.
1310
                    value[0] == git_sha):
0.200.1479 by Jelmer Vernooij
Simplify branch ref handling.
1311
                return True
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1312
            if (value[1] is not None and
1313
                revid is not None and
7143.15.2 by Jelmer Vernooij
Run autopep8.
1314
                    value[1] == revid):
0.200.1479 by Jelmer Vernooij
Simplify branch ref handling.
1315
                return True
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1316
            # FIXME: If one side only has the git sha available and the other
1317
            # only has the bzr revid, then this will cause us to show a tag as
1318
            # updated that hasn't actually been updated.
0.200.1479 by Jelmer Vernooij
Simplify branch ref handling.
1319
            return False
0.200.1474 by Jelmer Vernooij
Cope with refs when pushing.
1320
        # FIXME: Check for diverged branches
7018.3.8 by Jelmer Vernooij
Disable some flaky tests on python3, allow running without fastimport.
1321
        for ref, (git_sha, revid) in viewitems(new_refs):
0.200.1479 by Jelmer Vernooij
Simplify branch ref handling.
1322
            if ref_equals(ret, ref, git_sha, revid):
1323
                # Already up to date
1324
                if git_sha is None:
1325
                    git_sha = old_refs[ref][0]
1326
                if revid is None:
1327
                    revid = old_refs[ref][1]
1328
                ret[ref] = new_refs[ref] = (git_sha, revid)
1329
            elif ref not in ret or overwrite:
1330
                try:
1331
                    tag_name = ref_to_tag_name(ref)
1332
                except ValueError:
1333
                    pass
1334
                else:
1335
                    result.tag_updates[tag_name] = revid
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1336
                ret[ref] = (git_sha, revid)
1337
            else:
0.200.1474 by Jelmer Vernooij
Cope with refs when pushing.
1338
                # FIXME: Check diverged
1339
                diverged = False
1340
                if diverged:
1341
                    try:
1342
                        name = ref_to_tag_name(ref)
1343
                    except ValueError:
1344
                        pass
1345
                    else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
1346
                        result.tag_conflicts.append(
1347
                            (name, revid, ret[name][1]))
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1348
                else:
0.200.1474 by Jelmer Vernooij
Cope with refs when pushing.
1349
                    ret[ref] = (git_sha, revid)
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1350
        return ret
1351
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1352
    def fetch(self, stop_revision=None, fetch_tags=None, lossy=False,
1353
              limit=None):
0.200.1493 by Jelmer Vernooij
Test fixes.
1354
        if stop_revision is None:
1355
            stop_revision = self.source.last_revision()
1356
        ret = []
1357
        if fetch_tags:
7018.3.2 by Jelmer Vernooij
Fix some git tests.
1358
            for k, v in viewitems(self.source.tags.get_tag_dict()):
0.200.1493 by Jelmer Vernooij
Test fixes.
1359
                ret.append((None, v))
1360
        ret.append((None, stop_revision))
0.320.5 by Jelmer Vernooij
Always raise NoRoundtrippingSupport rather than NoPushSupport.
1361
        try:
1362
            self.interrepo.fetch_objects(ret, lossy=lossy, limit=limit)
1363
        except NoPushSupport:
1364
            raise errors.NoRoundtrippingSupport(self.source, self.target)
0.200.1493 by Jelmer Vernooij
Test fixes.
1365
0.252.36 by Jelmer Vernooij
Fix pull.
1366
    def pull(self, overwrite=False, stop_revision=None, local=False,
7131.14.1 by Jelmer Vernooij
Fix committing to a bzr branch bound to a git branch.
1367
             possible_transports=None, run_hooks=True, _stop_revno=None):
0.252.36 by Jelmer Vernooij
Fix pull.
1368
        result = GitBranchPullResult()
1369
        result.source_branch = self.source
1370
        result.target_branch = self.target
0.200.1788 by Jelmer Vernooij
Use context managers.
1371
        with self.source.lock_read(), self.target.lock_write():
1372
            new_refs, main_ref, stop_revinfo = self._get_new_refs(
7131.14.1 by Jelmer Vernooij
Fix committing to a bzr branch bound to a git branch.
1373
                stop_revision, stop_revno=_stop_revno)
7143.15.2 by Jelmer Vernooij
Run autopep8.
1374
0.200.1788 by Jelmer Vernooij
Use context managers.
1375
            def update_refs(old_refs):
1376
                return self._update_refs(result, old_refs, new_refs, overwrite)
0.200.1353 by Jelmer Vernooij
Run various hooks.
1377
            try:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1378
                result.revidmap, old_refs, new_refs = (
1379
                    self.interrepo.fetch_refs(update_refs, lossy=False))
0.200.1788 by Jelmer Vernooij
Use context managers.
1380
            except NoPushSupport:
1381
                raise errors.NoRoundtrippingSupport(self.source, self.target)
7143.15.2 by Jelmer Vernooij
Run autopep8.
1382
            (old_sha1, result.old_revid) = old_refs.get(
1383
                main_ref, (ZERO_SHA, NULL_REVISION))
0.200.1788 by Jelmer Vernooij
Use context managers.
1384
            if result.old_revid is None:
7143.15.2 by Jelmer Vernooij
Run autopep8.
1385
                result.old_revid = self.target.lookup_foreign_revision_id(
1386
                    old_sha1)
0.200.1788 by Jelmer Vernooij
Use context managers.
1387
            result.new_revid = new_refs[main_ref][1]
1388
            result.local_branch = None
1389
            result.master_branch = self.target
1390
            if run_hooks:
1391
                for hook in branch.Branch.hooks['post_pull']:
1392
                    hook(result)
0.252.36 by Jelmer Vernooij
Fix pull.
1393
        return result
1394
0.200.1260 by Jelmer Vernooij
Cope with new lossy argument.
1395
    def push(self, overwrite=False, stop_revision=None, lossy=False,
7131.14.1 by Jelmer Vernooij
Fix committing to a bzr branch bound to a git branch.
1396
             _override_hook_source_branch=None, _stop_revno=None):
0.252.5 by Jelmer Vernooij
enable 'bzr push'.
1397
        result = GitBranchPushResult()
1398
        result.source_branch = self.source
1399
        result.target_branch = self.target
0.200.1356 by Jelmer Vernooij
Fix result properties for hook.
1400
        result.local_branch = None
1401
        result.master_branch = result.target_branch
0.331.1 by Jelmer Vernooij
Fix some tags tests.
1402
        with self.source.lock_read(), self.target.lock_write():
7143.15.2 by Jelmer Vernooij
Run autopep8.
1403
            new_refs, main_ref, stop_revinfo = self._get_new_refs(
7131.14.3 by Jelmer Vernooij
Merge trunk.
1404
                stop_revision, stop_revno=_stop_revno)
7143.15.2 by Jelmer Vernooij
Run autopep8.
1405
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1406
            def update_refs(old_refs):
0.200.1392 by Jelmer Vernooij
Preserve existing refs.
1407
                return self._update_refs(result, old_refs, new_refs, overwrite)
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1408
            try:
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1409
                result.revidmap, old_refs, new_refs = (
1410
                    self.interrepo.fetch_refs(
1411
                        update_refs, lossy=lossy, overwrite=overwrite))
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1412
            except NoPushSupport:
1413
                raise errors.NoRoundtrippingSupport(self.source, self.target)
7143.15.2 by Jelmer Vernooij
Run autopep8.
1414
            (old_sha1, result.old_revid) = old_refs.get(
1415
                main_ref, (ZERO_SHA, NULL_REVISION))
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1416
            if result.old_revid is None:
7143.15.2 by Jelmer Vernooij
Run autopep8.
1417
                result.old_revid = self.target.lookup_foreign_revision_id(
1418
                    old_sha1)
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1419
            result.new_revid = new_refs[main_ref][1]
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
1420
            (result.new_original_revno,
1421
                result.new_original_revid) = stop_revinfo
0.200.1391 by Jelmer Vernooij
Warn on (and skip) invalid tags.
1422
            for hook in branch.Branch.hooks['post_push']:
1423
                hook(result)
0.252.5 by Jelmer Vernooij
enable 'bzr push'.
1424
        return result
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
1425
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
1426
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
1427
branch.InterBranch.register_optimiser(InterGitLocalGitBranch)
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
1428
branch.InterBranch.register_optimiser(InterFromGitBranch)
1429
branch.InterBranch.register_optimiser(InterToGitBranch)
0.200.1176 by Jelmer Vernooij
Fix fetch return value for inter git fetching.
1430
branch.InterBranch.register_optimiser(InterLocalGitRemoteGitBranch)