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