/brz/remove-bazaar

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