/brz/remove-bazaar

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