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