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