/brz/remove-bazaar

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