/brz/remove-bazaar

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