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