/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 (
23
    Commit,
24
    Tag,
25
    )
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.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
32
    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.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
36
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
37
from bzrlib.decorators import (
38
    needs_read_lock,
39
    )
0.200.969 by Jelmer Vernooij
Use tuples with bzr revid and git sha to avoid lookups.
40
from bzrlib.revision import (
41
    NULL_REVISION,
42
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
43
from bzrlib.trace import (
0.200.342 by Jelmer Vernooij
Report git sha during pull.
44
    is_quiet,
0.200.261 by Jelmer Vernooij
More formatting fixes.
45
    mutter,
46
    )
47
0.200.386 by Jelmer Vernooij
Move config to a separate file, support BranchConfig.username().
48
from bzrlib.plugins.git.config import (
49
    GitBranchConfig,
50
    )
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
51
from bzrlib.plugins.git.errors import (
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
52
    NoPushSupport,
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
53
    NoSuchRef,
54
    )
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
55
from bzrlib.plugins.git.refs import (
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
56
    extract_tags,
57
    is_tag,
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
58
    ref_to_branch_name,
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
59
    ref_to_tag_name,
0.200.875 by Jelmer Vernooij
Use new tag_name_to_ref function.
60
    tag_name_to_ref,
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
61
    UnpeelMap,
0.200.872 by Jelmer Vernooij
Move refs code to separate module.
62
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
63
0.238.5 by Jelmer Vernooij
Remove old backwards compatibility code.
64
from bzrlib.foreign import ForeignBranch
0.200.388 by Jelmer Vernooij
Support bzr 1.14 as well.
65
0.200.261 by Jelmer Vernooij
More formatting fixes.
66
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
67
class GitPullResult(branch.PullResult):
0.200.956 by Jelmer Vernooij
Add some more format tests.
68
    """Result of a pull from a Git branch."""
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
69
70
    def _lookup_revno(self, revid):
71
        assert isinstance(revid, str), "was %r" % revid
72
        # Try in source branch first, it'll be faster
73
        return self.target_branch.revision_id_to_revno(revid)
74
75
    @property
76
    def old_revno(self):
77
        return self._lookup_revno(self.old_revid)
78
79
    @property
80
    def new_revno(self):
81
        return self._lookup_revno(self.new_revid)
82
83
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
84
class GitTags(tag.BasicTags):
85
    """Ref-based tag dictionary."""
0.200.82 by Jelmer Vernooij
Support listing tags.
86
0.200.89 by Jelmer Vernooij
Support sprouting branches.
87
    def __init__(self, branch):
88
        self.branch = branch
89
        self.repository = branch.repository
0.200.82 by Jelmer Vernooij
Support listing tags.
90
0.200.1066 by Jelmer Vernooij
Add GitTags.get_refs.
91
    def get_refs(self):
92
        raise NotImplementedError(self.get_refs)
93
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
94
    def _iter_tag_refs(self, refs):
95
        raise NotImplementedError(self._iter_tag_refs)
96
97
    def _merge_to_git(self, to_tags, refs, overwrite=False):
98
        target_repo = to_tags.repository
99
        conflicts = []
100
        for k, v in refs.iteritems():
101
            if not is_tag(k):
102
                continue
103
            if overwrite or not k in self.target.repository.refs:
104
                target_repo.refs[k] = v
105
            elif target_repo.repository.refs[k] == v:
106
                pass
107
            else:
108
                conflicts.append((ref_to_tag_name(k), v, target_repo.refs[k]))
109
        return conflicts
110
111
    def _merge_to_non_git(self, to_tags, refs, overwrite=False):
112
        unpeeled_map = defaultdict(set)
113
        conflicts = []
114
        result = dict(to_tags.get_tag_dict())
115
        for n, peeled, unpeeled, bzr_revid in self._iter_tag_refs(refs):
116
            if unpeeled is not None:
117
                unpeeled_map[peeled].add(unpeeled)
118
            if n not in result or overwrite:
119
                result[n] = bzr_revid
120
            elif result[n] == bzr_revid:
121
                pass
122
            else:
123
                conflicts.append((n, result[n], bzr_revid))
124
        to_tags._set_tag_dict(result)
125
        if len(unpeeled_map) > 0:
126
            map_file = UnpeelMap.from_repository(to_tags.branch.repository)
127
            map_file.update(unpeeled_map)
128
            map_file.save_in_repository(to_tags.branch.repository)
129
        return conflicts
130
131
    def merge_to(self, to_tags, overwrite=False, ignore_master=False,
132
                 source_refs=None):
133
        if source_refs is None:
0.200.1066 by Jelmer Vernooij
Add GitTags.get_refs.
134
            source_refs = self.get_refs()
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
135
        if self == to_tags:
136
            return
137
        if isinstance(to_tags, GitTags):
138
            return self._merge_to_git(to_tags, source_refs,
139
                                      overwrite=overwrite)
140
        else:
141
            if ignore_master:
142
                master = None
143
            else:
144
                master = to_tags.branch.get_master_branch()
145
            conflicts = self._merge_to_non_git(to_tags, source_refs,
146
                                              overwrite=overwrite)
147
            if master is not None:
148
                conflicts += self.merge_to(to_tags, overwrite=overwrite,
149
                                           source_refs=source_refs,
150
                                           ignore_master=ignore_master)
151
            return conflicts
152
153
    def get_tag_dict(self):
154
        ret = {}
0.200.1066 by Jelmer Vernooij
Add GitTags.get_refs.
155
        refs = self.get_refs()
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
156
        for (name, peeled, unpeeled, bzr_revid) in self._iter_tag_refs(refs):
157
            ret[name] = bzr_revid
158
        return ret
159
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
160
161
class LocalGitTagDict(GitTags):
162
    """Dictionary with tags in a local repository."""
163
164
    def __init__(self, branch):
165
        super(LocalGitTagDict, self).__init__(branch)
166
        self.refs = self.repository._git.refs
167
0.200.1066 by Jelmer Vernooij
Add GitTags.get_refs.
168
    def get_refs(self):
169
        return self.repository._git.get_refs()
170
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
171
    def _iter_tag_refs(self, refs):
172
        """Iterate over the tag refs.
173
174
        :param refs: Refs dictionary (name -> git sha1)
175
        :return: iterator over (name, peeled_sha1, unpeeled_sha1, bzr_revid)
176
        """
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
177
        for k, (peeled, unpeeled) in extract_tags(refs).iteritems():
0.200.609 by Jelmer Vernooij
Cope with tags pointing at nonexisting objects.
178
            try:
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
179
                obj = self.repository._git[peeled]
0.200.609 by Jelmer Vernooij
Cope with tags pointing at nonexisting objects.
180
            except KeyError:
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
181
                mutter("Tag %s points at unknown object %s, ignoring", peeled,
182
                       obj)
0.200.609 by Jelmer Vernooij
Cope with tags pointing at nonexisting objects.
183
                continue
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
184
            # FIXME: this shouldn't really be necessary, the repository
185
            # already should have these unpeeled.
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
186
            while isinstance(obj, Tag):
0.200.1060 by Jelmer Vernooij
Return unpeeled tags in extract_tags.
187
                peeled = obj.object[1]
188
                obj = self.repository._git[peeled]
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
189
            if not isinstance(obj, Commit):
0.200.261 by Jelmer Vernooij
More formatting fixes.
190
                mutter("Tag %s points at object %r that is not a commit, "
191
                       "ignoring", k, obj)
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
192
                continue
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
193
            yield (k, peeled, unpeeled,
194
                   self.branch.lookup_foreign_revision_id(peeled))
195
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
196
    def _set_tag_dict(self, to_dict):
0.200.1066 by Jelmer Vernooij
Add GitTags.get_refs.
197
        extra = set(self.get_refs().keys())
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
198
        for k, revid in to_dict.iteritems():
0.200.875 by Jelmer Vernooij
Use new tag_name_to_ref function.
199
            name = tag_name_to_ref(k)
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
200
            if name in extra:
201
                extra.remove(name)
202
            self.set_tag(k, revid)
203
        for name in extra:
0.200.1061 by Jelmer Vernooij
Add support for using unpeel map.
204
            if is_tag(name):
0.200.711 by Jelmer Vernooij
Support merging tags to a local Git repository.
205
                del self.repository._git[name]
0.200.956 by Jelmer Vernooij
Add some more format tests.
206
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
207
    def set_tag(self, name, revid):
0.200.1064 by Jelmer Vernooij
Use common base class for tags.
208
        self.refs[tag_name_to_ref(name)], _ = \
0.200.1030 by Jelmer Vernooij
More work on supporting roundtripping push.
209
            self.branch.lookup_bzr_revision_id(revid)
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
210
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
211
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
212
class DictTagDict(LocalGitTagDict):
213
214
    def __init__(self, branch, tags):
215
        super(DictTagDict, self).__init__(branch)
216
        self._tags = tags
217
218
    def get_tag_dict(self):
219
        return self._tags
220
221
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
222
class GitBranchFormat(branch.BranchFormat):
223
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
224
    def get_format_description(self):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
225
        return 'Git Branch'
226
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
227
    def network_name(self):
228
        return "git"
229
0.200.82 by Jelmer Vernooij
Support listing tags.
230
    def supports_tags(self):
231
        return True
232
0.243.1 by Jelmer Vernooij
Use foreign branch testing infrastructure.
233
    def get_foreign_tests_branch_factory(self):
234
        from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
235
        return ForeignTestsBranchFactory()
236
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
237
    def make_tags(self, branch):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
238
        if getattr(branch.repository, "get_refs", None) is not None:
239
            from bzrlib.plugins.git.remote import RemoteGitTagDict
240
            return RemoteGitTagDict(branch)
0.200.261 by Jelmer Vernooij
More formatting fixes.
241
        else:
242
            return LocalGitTagDict(branch)
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
243
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
244
0.200.911 by Jelmer Vernooij
Cope with locking changes in bzr.dev.
245
class GitReadLock(object):
246
247
    def __init__(self, unlock):
248
        self.unlock = unlock
249
250
251
class GitWriteLock(object):
252
253
    def __init__(self, unlock):
254
        self.unlock = unlock
255
256
0.200.388 by Jelmer Vernooij
Support bzr 1.14 as well.
257
class GitBranch(ForeignBranch):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
258
    """An adapter to git repositories for bzr Branch objects."""
259
0.200.770 by Jelmer Vernooij
Proper branch names.
260
    def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
0.200.82 by Jelmer Vernooij
Support listing tags.
261
        self.repository = repository
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
262
        self._format = GitBranchFormat()
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
263
        self.control_files = lockfiles
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
264
        self.bzrdir = bzrdir
0.231.1 by Jelmer Vernooij
Check that regenerated objects have the expected sha1.
265
        super(GitBranch, self).__init__(repository.get_mapping())
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
266
        if tagsdict is not None:
267
            self.tags = DictTagDict(self, tagsdict)
0.200.770 by Jelmer Vernooij
Proper branch names.
268
        self.ref = ref
269
        self.name = ref_to_branch_name(ref)
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
270
        self._head = None
0.200.630 by Jelmer Vernooij
Fix base url of Git branches - use the working tree path rather than the control directory path.
271
        self.base = bzrdir.root_transport.base
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
272
0.239.8 by Jelmer Vernooij
Support checkouts.
273
    def _get_checkout_format(self):
274
        """Return the most suitable metadir for a checkout of this branch.
275
        Weaves are used if this branch's repository uses weaves.
276
        """
0.200.927 by Jelmer Vernooij
Remove explicit use of rich root formats.
277
        return bzrdir.format_registry.make_bzrdir("default")
0.239.8 by Jelmer Vernooij
Support checkouts.
278
0.238.3 by Jelmer Vernooij
Remove svn references, prefer git send format when submitting changes against a git branch.
279
    def get_child_submit_format(self):
280
        """Return the preferred format of submissions to this branch."""
281
        ret = self.get_config().get_user_option("child_submit_format")
282
        if ret is not None:
283
            return ret
284
        return "git"
285
0.200.293 by Jelmer Vernooij
Fix branch nicks.
286
    def _get_nick(self, local=False, possible_master_transports=None):
287
        """Find the nick name for this branch.
288
289
        :return: Branch nick
290
        """
0.200.920 by Jelmer Vernooij
Fix some more tests.
291
        return self.name or "HEAD"
0.200.293 by Jelmer Vernooij
Fix branch nicks.
292
0.200.331 by Jelmer Vernooij
Add stub for setting nick function.
293
    def _set_nick(self, nick):
294
        raise NotImplementedError
295
296
    nick = property(_get_nick, _set_nick)
0.200.293 by Jelmer Vernooij
Fix branch nicks.
297
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
298
    def __repr__(self):
0.200.770 by Jelmer Vernooij
Proper branch names.
299
        return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
0.200.920 by Jelmer Vernooij
Fix some more tests.
300
            self.ref or "HEAD")
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
301
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
302
    def generate_revision_history(self, revid, old_revid=None):
303
        # FIXME: Check that old_revid is in the ancestry of revid
304
        newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
305
        self._set_head(newhead)
306
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
307
    def lock_write(self):
308
        self.control_files.lock_write()
0.200.911 by Jelmer Vernooij
Cope with locking changes in bzr.dev.
309
        return GitWriteLock(self.unlock)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
310
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
311
    def get_stacked_on_url(self):
312
        # Git doesn't do stacking (yet...)
0.200.631 by Jelmer Vernooij
Raise proper exception in Branch.get_stacked_on_url().
313
        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.
314
315
    def get_parent(self):
316
        """See Branch.get_parent()."""
0.200.312 by Jelmer Vernooij
Add notes about parent locations.
317
        # 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.
318
        return None
319
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
320
    def set_parent(self, url):
0.200.312 by Jelmer Vernooij
Add notes about parent locations.
321
        # FIXME: Set "origin" url in .git/config ?
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
322
        pass
323
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
324
    def lock_read(self):
325
        self.control_files.lock_read()
0.200.911 by Jelmer Vernooij
Cope with locking changes in bzr.dev.
326
        return GitReadLock(self.unlock)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
327
0.200.432 by Jelmer Vernooij
Support Branch.is_locked, required for loggerhead.
328
    def is_locked(self):
329
        return self.control_files.is_locked()
330
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
331
    def unlock(self):
332
        self.control_files.unlock()
333
334
    def get_physical_lock_status(self):
335
        return False
336
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
337
    @needs_read_lock
338
    def last_revision(self):
339
        # perhaps should escape this ?
0.200.57 by Jelmer Vernooij
Fix more tests.
340
        if self.head is None:
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
341
            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.
342
        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.
343
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
344
    def _basic_push(self, target, overwrite=False, stop_revision=None):
345
        return branch.InterBranch.get(self, target)._basic_push(
346
            overwrite, stop_revision)
347
0.252.49 by Jelmer Vernooij
Avoid trying to set HEAD for remote branches.
348
    def lookup_foreign_revision_id(self, foreign_revid):
0.200.956 by Jelmer Vernooij
Add some more format tests.
349
        return self.repository.lookup_foreign_revision_id(foreign_revid,
0.252.49 by Jelmer Vernooij
Avoid trying to set HEAD for remote branches.
350
            self.mapping)
351
0.200.1030 by Jelmer Vernooij
More work on supporting roundtripping push.
352
    def lookup_bzr_revision_id(self, revid):
353
        return self.repository.lookup_bzr_revision_id(
354
            revid, mapping=self.mapping)
355
0.200.692 by Jelmer Vernooij
Refuse pulling into non-rich-root branches rather than erroring out with an AttributeError.
356
0.200.465 by Jelmer Vernooij
Use dulwich standard functionality for finding missing revisions.
357
class LocalGitBranch(GitBranch):
358
    """A local Git branch."""
359
0.200.763 by Jelmer Vernooij
Provide proper colocated branch support.
360
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
0.200.956 by Jelmer Vernooij
Add some more format tests.
361
        super(LocalGitBranch, self).__init__(bzrdir, repository, name,
0.200.763 by Jelmer Vernooij
Provide proper colocated branch support.
362
              lockfiles, tagsdict)
0.200.918 by Jelmer Vernooij
Cope with 'self.ref is None' in a couple more places.
363
        refs = repository._git.get_refs()
364
        if not (name in refs.keys() or "HEAD" in refs.keys()):
0.200.763 by Jelmer Vernooij
Provide proper colocated branch support.
365
            raise errors.NotBranchError(self.base)
366
0.200.261 by Jelmer Vernooij
More formatting fixes.
367
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
368
        accelerator_tree=None, hardlink=False):
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
369
        if lightweight:
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
370
            t = transport.get_transport(to_location)
371
            t.ensure_base()
372
            format = self._get_checkout_format()
373
            checkout = format.initialize_on_transport(t)
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
374
            from_branch = branch.BranchReferenceFormat().initialize(checkout,
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
375
                self)
376
            tree = checkout.create_workingtree(revision_id,
377
                from_branch=from_branch, hardlink=hardlink)
378
            return tree
379
        else:
380
            return self._create_heavyweight_checkout(to_location, revision_id,
0.257.1 by Jelmer Vernooij
use transport repo objects even for local access.
381
                hardlink)
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
382
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
383
    def _create_heavyweight_checkout(self, to_location, revision_id=None,
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
384
                                     hardlink=False):
385
        """Create a new heavyweight checkout of this branch.
386
387
        :param to_location: URL of location to create the new checkout in.
388
        :param revision_id: Revision that should be the tip of the checkout.
389
        :param hardlink: Whether to hardlink
390
        :return: WorkingTree object of checkout.
391
        """
0.200.513 by Jelmer Vernooij
Fix imports.
392
        checkout_branch = bzrdir.BzrDir.create_branch_convenience(
0.200.927 by Jelmer Vernooij
Remove explicit use of rich root formats.
393
            to_location, force_new_tree=False)
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
394
        checkout = checkout_branch.bzrdir
395
        checkout_branch.bind(self)
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
396
        # 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.
397
        # branch tip correctly, and seed it with history.
398
        checkout_branch.pull(self, stop_revision=revision_id)
399
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
400
0.200.57 by Jelmer Vernooij
Fix more tests.
401
    def _gen_revision_history(self):
0.200.58 by Jelmer Vernooij
Fix remaining tests.
402
        if self.head is None:
403
            return []
0.200.261 by Jelmer Vernooij
More formatting fixes.
404
        ret = list(self.repository.iter_reverse_revision_history(
405
            self.last_revision()))
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
406
        ret.reverse()
0.200.57 by Jelmer Vernooij
Fix more tests.
407
        return ret
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
408
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
409
    def _get_head(self):
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
410
        try:
0.200.918 by Jelmer Vernooij
Cope with 'self.ref is None' in a couple more places.
411
            return self.repository._git.ref(self.ref or "HEAD")
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
412
        except KeyError:
413
            return None
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
414
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
415
    def set_last_revision_info(self, revno, revid):
416
        self.set_last_revision(revid)
417
418
    def set_last_revision(self, revid):
0.252.5 by Jelmer Vernooij
enable 'bzr push'.
419
        (newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
0.200.523 by Jelmer Vernooij
Fix undefined error.
420
        self.head = newhead
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
421
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
422
    def _set_head(self, value):
423
        self._head = value
0.200.918 by Jelmer Vernooij
Cope with 'self.ref is None' in a couple more places.
424
        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.
425
        self._clear_cached_state()
426
427
    head = property(_get_head, _set_head)
428
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
429
    def get_config(self):
430
        return GitBranchConfig(self)
431
432
    def get_push_location(self):
433
        """See Branch.get_push_location."""
434
        push_loc = self.get_config().get_user_option('push_location')
435
        return push_loc
436
437
    def set_push_location(self, location):
438
        """See Branch.set_push_location."""
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
439
        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.
440
                                          store=config.STORE_LOCATION)
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
441
442
    def supports_tags(self):
0.200.82 by Jelmer Vernooij
Support listing tags.
443
        return True
0.200.956 by Jelmer Vernooij
Add some more format tests.
444
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
445
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
446
def _quick_lookup_revno(local_branch, remote_branch, revid):
447
    assert isinstance(revid, str), "was %r" % revid
448
    # Try in source branch first, it'll be faster
449
    try:
450
        return local_branch.revision_id_to_revno(revid)
451
    except errors.NoSuchRevision:
452
        graph = local_branch.repository.get_graph()
453
        try:
454
            return graph.find_distance_to_null(revid)
455
        except errors.GhostRevisionsHaveNoRevno:
456
            # FIXME: Check using graph.find_distance_to_null() ?
457
            return remote_branch.revision_id_to_revno(revid)
458
459
0.200.342 by Jelmer Vernooij
Report git sha during pull.
460
class GitBranchPullResult(branch.PullResult):
461
0.252.36 by Jelmer Vernooij
Fix pull.
462
    def __init__(self):
463
        super(GitBranchPullResult, self).__init__()
464
        self.new_git_head = None
465
        self._old_revno = None
466
        self._new_revno = None
467
0.200.342 by Jelmer Vernooij
Report git sha during pull.
468
    def report(self, to_file):
469
        if not is_quiet():
470
            if self.old_revid == self.new_revid:
471
                to_file.write('No revisions to pull.\n')
0.200.728 by Jelmer Vernooij
Fix pulling when all revisions are already in the repo.
472
            elif self.new_git_head is not None:
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
473
                to_file.write('Now on revision %d (git sha: %s).\n' %
0.200.342 by Jelmer Vernooij
Report git sha during pull.
474
                        (self.new_revno, self.new_git_head))
0.200.728 by Jelmer Vernooij
Fix pulling when all revisions are already in the repo.
475
            else:
476
                to_file.write('Now on revision %d.\n' % (self.new_revno,))
0.200.342 by Jelmer Vernooij
Report git sha during pull.
477
        self._show_tag_conficts(to_file)
478
0.252.36 by Jelmer Vernooij
Fix pull.
479
    def _lookup_revno(self, revid):
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
480
        return _quick_lookup_revno(self.target_branch, self.source_branch, revid)
0.252.36 by Jelmer Vernooij
Fix pull.
481
482
    def _get_old_revno(self):
483
        if self._old_revno is not None:
484
            return self._old_revno
485
        return self._lookup_revno(self.old_revid)
486
487
    def _set_old_revno(self, revno):
488
        self._old_revno = revno
489
490
    old_revno = property(_get_old_revno, _set_old_revno)
491
492
    def _get_new_revno(self):
493
        if self._new_revno is not None:
494
            return self._new_revno
495
        return self._lookup_revno(self.new_revid)
496
497
    def _set_new_revno(self, revno):
498
        self._new_revno = revno
0.200.956 by Jelmer Vernooij
Add some more format tests.
499
0.252.36 by Jelmer Vernooij
Fix pull.
500
    new_revno = property(_get_new_revno, _set_new_revno)
501
0.200.342 by Jelmer Vernooij
Report git sha during pull.
502
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
503
class GitBranchPushResult(branch.BranchPushResult):
504
505
    def _lookup_revno(self, revid):
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
506
        return _quick_lookup_revno(self.source_branch, self.target_branch, revid)
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
507
508
    @property
509
    def old_revno(self):
510
        return self._lookup_revno(self.old_revid)
511
512
    @property
513
    def new_revno(self):
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
514
        new_original_revno = getattr(self, "new_original_revno", None)
515
        if new_original_revno:
516
            return new_original_revno
517
        if getattr(self, "new_original_revid", None) is not None:
518
            return self._lookup_revno(self.new_original_revid)
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
519
        return self._lookup_revno(self.new_revid)
520
521
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
522
class InterFromGitBranch(branch.GenericInterBranch):
0.200.261 by Jelmer Vernooij
More formatting fixes.
523
    """InterBranch implementation that pulls from Git into bzr."""
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
524
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
525
    @staticmethod
526
    def _get_branch_formats_to_test():
527
        return []
528
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
529
    @classmethod
0.200.692 by Jelmer Vernooij
Refuse pulling into non-rich-root branches rather than erroring out with an AttributeError.
530
    def _get_interrepo(self, source, target):
531
        return repository.InterRepository.get(source.repository,
532
            target.repository)
533
534
    @classmethod
535
    def is_compatible(cls, source, target):
536
        return (isinstance(source, GitBranch) and
537
                not isinstance(target, GitBranch) and
538
                (getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
539
0.247.7 by Michael Hudson
preserve the interface of update_revisions()
540
    def _update_revisions(self, stop_revision=None, overwrite=False,
0.247.2 by Michael Hudson
this works for my tests, but i'm pretty sure it's wrong in general
541
        graph=None, limit=None):
0.247.7 by Michael Hudson
preserve the interface of update_revisions()
542
        """Like InterBranch.update_revisions(), but with additions.
543
544
        Compared to the `update_revisions()` below, this function takes a
545
        `limit` argument that limits how many git commits will be converted
0.200.1062 by Jelmer Vernooij
Pass remote refs along in _update_revisions.
546
        and returns the new git head and remote refs.
0.247.7 by Michael Hudson
preserve the interface of update_revisions()
547
        """
0.200.692 by Jelmer Vernooij
Refuse pulling into non-rich-root branches rather than erroring out with an AttributeError.
548
        interrepo = self._get_interrepo(self.source, self.target)
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
549
        def determine_wants(heads):
0.200.917 by Jelmer Vernooij
Cope with implicit branches during pull.
550
            if self.source.ref is not None and not self.source.ref in heads:
0.200.777 by Jelmer Vernooij
Fix colocated remote branches.
551
                raise NoSuchRef(self.source.ref, heads.keys())
0.200.314 by Jelmer Vernooij
Support stop_revision.
552
            if stop_revision is not None:
0.200.728 by Jelmer Vernooij
Fix pulling when all revisions are already in the repo.
553
                self._last_revid = stop_revision
0.200.912 by Jelmer Vernooij
Merge roundtrip support.
554
                head, mapping = self.source.repository.lookup_bzr_revision_id(
0.200.316 by Jelmer Vernooij
Fix formatting.
555
                    stop_revision)
0.200.314 by Jelmer Vernooij
Support stop_revision.
556
            else:
0.200.917 by Jelmer Vernooij
Cope with implicit branches during pull.
557
                if self.source.ref is not None:
558
                    head = heads[self.source.ref]
559
                else:
560
                    head = heads["HEAD"]
0.252.44 by Jelmer Vernooij
Properly look up Bazaar revision ids for revision parents in case they are round-tripped.
561
                self._last_revid = self.source.lookup_foreign_revision_id(head)
0.200.728 by Jelmer Vernooij
Fix pulling when all revisions are already in the repo.
562
            if self.target.repository.has_revision(self._last_revid):
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
563
                return []
0.247.6 by Michael Hudson
away with underscore prefixed local variables
564
            return [head]
0.200.1002 by Jelmer Vernooij
Fix regression in git-import.
565
        pack_hint, head, refs = interrepo.fetch_objects(
0.247.2 by Michael Hudson
this works for my tests, but i'm pretty sure it's wrong in general
566
            determine_wants, self.source.mapping, limit=limit)
0.252.45 by Jelmer Vernooij
Finish fetching roundtripped revisions back into bzr.
567
        if (pack_hint is not None and
568
            self.target.repository._format.pack_compresses):
0.248.5 by Jelmer Vernooij
Reformatting, fix dpush.
569
            self.target.repository.pack(hint=pack_hint)
0.200.728 by Jelmer Vernooij
Fix pulling when all revisions are already in the repo.
570
        if head is not None:
0.252.44 by Jelmer Vernooij
Properly look up Bazaar revision ids for revision parents in case they are round-tripped.
571
            self._last_revid = self.source.lookup_foreign_revision_id(head)
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
572
        if overwrite:
0.200.314 by Jelmer Vernooij
Support stop_revision.
573
            prev_last_revid = None
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
574
        else:
0.200.314 by Jelmer Vernooij
Support stop_revision.
575
            prev_last_revid = self.target.last_revision()
0.248.5 by Jelmer Vernooij
Reformatting, fix dpush.
576
        self.target.generate_revision_history(self._last_revid,
577
            prev_last_revid)
0.200.1062 by Jelmer Vernooij
Pass remote refs along in _update_revisions.
578
        return head, refs
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
579
0.200.726 by Jelmer Vernooij
Factor out conversion of branch names to refs.
580
    def update_revisions(self, stop_revision=None, overwrite=False,
581
                         graph=None):
0.247.7 by Michael Hudson
preserve the interface of update_revisions()
582
        """See InterBranch.update_revisions()."""
583
        self._update_revisions(stop_revision, overwrite, graph)
584
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
585
    def pull(self, overwrite=False, stop_revision=None,
586
             possible_transports=None, _hook_master=None, run_hooks=True,
0.247.2 by Michael Hudson
this works for my tests, but i'm pretty sure it's wrong in general
587
             _override_hook_target=None, local=False, limit=None):
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
588
        """See Branch.pull.
589
590
        :param _hook_master: Private parameter - set the branch to
591
            be supplied as the master to pull hooks.
592
        :param run_hooks: Private parameter - if false, this branch
593
            is being called because it's the master of the primary branch,
594
            so it should not run its hooks.
595
        :param _override_hook_target: Private parameter - set the branch to be
596
            supplied as the target_branch to pull hooks.
0.247.2 by Michael Hudson
this works for my tests, but i'm pretty sure it's wrong in general
597
        :param limit: Only import this many revisons.  `None`, the default,
598
            means import all revisions.
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
599
        """
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
600
        # This type of branch can't be bound.
601
        if local:
602
            raise errors.LocalRequiresBoundBranch()
0.200.342 by Jelmer Vernooij
Report git sha during pull.
603
        result = GitBranchPullResult()
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
604
        result.source_branch = self.source
605
        if _override_hook_target is None:
606
            result.target_branch = self.target
607
        else:
608
            result.target_branch = _override_hook_target
609
        self.source.lock_read()
610
        try:
611
            # We assume that during 'pull' the target repository is closer than
612
            # the source one.
613
            graph = self.target.repository.get_graph(self.source.repository)
0.200.726 by Jelmer Vernooij
Factor out conversion of branch names to refs.
614
            (result.old_revno, result.old_revid) = \
615
                self.target.last_revision_info()
0.200.1062 by Jelmer Vernooij
Pass remote refs along in _update_revisions.
616
            result.new_git_head, remote_refs = self._update_revisions(
0.247.6 by Michael Hudson
away with underscore prefixed local variables
617
                stop_revision, overwrite=overwrite, graph=graph, limit=limit)
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
618
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
619
                overwrite)
0.200.726 by Jelmer Vernooij
Factor out conversion of branch names to refs.
620
            (result.new_revno, result.new_revid) = \
621
                self.target.last_revision_info()
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
622
            if _hook_master:
623
                result.master_branch = _hook_master
624
                result.local_branch = result.target_branch
625
            else:
626
                result.master_branch = result.target_branch
627
                result.local_branch = None
628
            if run_hooks:
629
                for hook in branch.Branch.hooks['post_pull']:
630
                    hook(result)
631
        finally:
632
            self.source.unlock()
633
        return result
634
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
635
    def _basic_push(self, overwrite=False, stop_revision=None):
636
        result = branch.BranchPushResult()
637
        result.source_branch = self.source
638
        result.target_branch = self.target
0.200.505 by Jelmer Vernooij
Remove duplicate code.
639
        graph = self.target.repository.get_graph(self.source.repository)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
640
        result.old_revno, result.old_revid = self.target.last_revision_info()
0.200.1062 by Jelmer Vernooij
Pass remote refs along in _update_revisions.
641
        result.new_git_head, remote_refs = self._update_revisions(
0.247.3 by Michael Hudson
oh, so it wasn't (particularly) wrong, but it was a bit obscure
642
            stop_revision, overwrite=overwrite, graph=graph)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
643
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
644
            overwrite)
645
        result.new_revno, result.new_revid = self.target.last_revision_info()
646
        return result
647
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
648
0.200.512 by Jelmer Vernooij
Support pushing git->git.
649
class InterGitBranch(branch.GenericInterBranch):
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
650
    """InterBranch implementation that pulls between Git branches."""
651
0.200.512 by Jelmer Vernooij
Support pushing git->git.
652
653
class InterGitLocalRemoteBranch(InterGitBranch):
654
    """InterBranch that copies from a local to a remote git branch."""
655
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
656
    @staticmethod
657
    def _get_branch_formats_to_test():
658
        return []
659
0.200.512 by Jelmer Vernooij
Support pushing git->git.
660
    @classmethod
661
    def is_compatible(self, source, target):
662
        from bzrlib.plugins.git.remote import RemoteGitBranch
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
663
        return (isinstance(source, LocalGitBranch) and
0.200.512 by Jelmer Vernooij
Support pushing git->git.
664
                isinstance(target, RemoteGitBranch))
665
666
    def _basic_push(self, overwrite=False, stop_revision=None):
0.200.891 by Jelmer Vernooij
Use ZERO_SHA constant where possible.
667
        from dulwich.protocol import ZERO_SHA
0.200.512 by Jelmer Vernooij
Support pushing git->git.
668
        result = GitBranchPushResult()
669
        result.source_branch = self.source
670
        result.target_branch = self.target
671
        if stop_revision is None:
672
            stop_revision = self.source.last_revision()
673
        # FIXME: Check for diverged branches
674
        def get_changed_refs(old_refs):
0.252.44 by Jelmer Vernooij
Properly look up Bazaar revision ids for revision parents in case they are round-tripped.
675
            result.old_revid = self.target.lookup_foreign_revision_id(old_refs.get(self.target.ref, ZERO_SHA))
0.200.822 by Jelmer Vernooij
Fix indication of number of revisions pushed in dpush.
676
            refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
0.200.512 by Jelmer Vernooij
Support pushing git->git.
677
            result.new_revid = stop_revision
678
            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.
679
                refs[tag_name_to_ref(name)] = sha
0.200.512 by Jelmer Vernooij
Support pushing git->git.
680
            return refs
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
681
        self.target.repository.send_pack(get_changed_refs,
0.200.726 by Jelmer Vernooij
Factor out conversion of branch names to refs.
682
            self.source.repository._git.object_store.generate_pack_contents)
0.200.512 by Jelmer Vernooij
Support pushing git->git.
683
        return result
684
685
686
class InterGitRemoteLocalBranch(InterGitBranch):
687
    """InterBranch that copies from a remote to a local git branch."""
688
0.200.996 by Jelmer Vernooij
Fix test run of InterBranches.
689
    @staticmethod
690
    def _get_branch_formats_to_test():
691
        return []
692
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
693
    @classmethod
694
    def is_compatible(self, source, target):
695
        from bzrlib.plugins.git.remote import RemoteGitBranch
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
696
        return (isinstance(source, RemoteGitBranch) and
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
697
                isinstance(target, LocalGitBranch))
698
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
699
    def _basic_push(self, overwrite=False, stop_revision=None):
700
        result = branch.BranchPushResult()
701
        result.source_branch = self.source
702
        result.target_branch = self.target
703
        result.old_revid = self.target.last_revision()
704
        refs, stop_revision = self.update_refs(stop_revision)
705
        self.target.generate_revision_history(stop_revision, result.old_revid)
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
706
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
707
            source_refs=refs, overwrite=overwrite)
0.200.505 by Jelmer Vernooij
Remove duplicate code.
708
        result.new_revid = self.target.last_revision()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
709
        return result
710
711
    def update_refs(self, stop_revision=None):
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
712
        interrepo = repository.InterRepository.get(self.source.repository,
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
713
            self.target.repository)
714
        if stop_revision is None:
0.200.940 by Jelmer Vernooij
Avoid confusion between different fetch functions with different semantics.
715
            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.
716
            stop_revision = self.target.lookup_foreign_revision_id(refs["HEAD"])
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
717
        else:
0.200.940 by Jelmer Vernooij
Avoid confusion between different fetch functions with different semantics.
718
            refs = interrepo.fetch(revision_id=stop_revision)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
719
        return refs, stop_revision
720
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
721
    def pull(self, stop_revision=None, overwrite=False,
0.200.732 by Jelmer Vernooij
Support run_hooks argument to InterGitRemoteLocalBranch.pull().
722
        possible_transports=None, run_hooks=True,local=False):
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
723
        # This type of branch can't be bound.
724
        if local:
725
            raise errors.LocalRequiresBoundBranch()
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
726
        result = GitPullResult()
727
        result.source_branch = self.source
728
        result.target_branch = self.target
729
        result.old_revid = self.target.last_revision()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
730
        refs, stop_revision = self.update_refs(stop_revision)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
731
        self.target.generate_revision_history(stop_revision, result.old_revid)
0.200.1065 by Jelmer Vernooij
Don't peel tags automatically when pushing back.
732
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
733
            overwrite=overwrite, source_refs=refs)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
734
        result.new_revid = self.target.last_revision()
735
        return result
736
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
737
0.200.960 by Jelmer Vernooij
Use GenericInterBranch.
738
class InterToGitBranch(branch.GenericInterBranch):
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
739
    """InterBranch implementation that pulls from Git into bzr."""
740
0.200.939 by Jelmer Vernooij
Use InterRepo directly.
741
    def __init__(self, source, target):
742
        super(InterToGitBranch, self).__init__(source, target)
743
        self.interrepo = repository.InterRepository.get(source.repository,
744
                                           target.repository)
745
0.200.631 by Jelmer Vernooij
Raise proper exception in Branch.get_stacked_on_url().
746
    @staticmethod
747
    def _get_branch_formats_to_test():
0.200.961 by Jelmer Vernooij
Cope with API changes to InterBranch._get_branch_formats_to_test.
748
        return []
0.200.631 by Jelmer Vernooij
Raise proper exception in Branch.get_stacked_on_url().
749
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
750
    @classmethod
751
    def is_compatible(self, source, target):
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
752
        return (not isinstance(source, GitBranch) and
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
753
                isinstance(target, GitBranch))
754
0.200.542 by Jelmer Vernooij
Proper error for push in 1.14.
755
    def update_revisions(self, *args, **kwargs):
756
        raise NoPushSupport()
757
0.252.38 by Jelmer Vernooij
Minor cleanups.
758
    def _get_new_refs(self, stop_revision=None):
759
        if stop_revision is None:
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
760
            (stop_revno, stop_revision) = self.source.last_revision_info()
0.200.969 by Jelmer Vernooij
Use tuples with bzr revid and git sha to avoid lookups.
761
        assert type(stop_revision) is str
0.200.916 by Jelmer Vernooij
Set refs/heads/master if no ref is set yet.
762
        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.
763
        refs = { main_ref: (None, stop_revision) }
0.252.37 by Jelmer Vernooij
Factor out some common code for finding refs to send.
764
        for name, revid in self.source.tags.get_tag_dict().iteritems():
765
            if self.source.repository.has_revision(revid):
0.200.969 by Jelmer Vernooij
Use tuples with bzr revid and git sha to avoid lookups.
766
                refs[tag_name_to_ref(name)] = (None, revid)
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
767
        return refs, main_ref, (stop_revno, stop_revision)
0.252.37 by Jelmer Vernooij
Factor out some common code for finding refs to send.
768
0.252.36 by Jelmer Vernooij
Fix pull.
769
    def pull(self, overwrite=False, stop_revision=None, local=False,
770
             possible_transports=None):
771
        from dulwich.protocol import ZERO_SHA
772
        result = GitBranchPullResult()
773
        result.source_branch = self.source
774
        result.target_branch = self.target
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
775
        new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
0.200.941 by Jelmer Vernooij
Pass update_refs argument to fetch_refs.
776
        def update_refs(old_refs):
777
            refs = dict(old_refs)
0.200.945 by Jelmer Vernooij
Move fixmes
778
            # FIXME: Check for diverged branches
0.200.941 by Jelmer Vernooij
Pass update_refs argument to fetch_refs.
779
            refs.update(new_refs)
780
            return refs
781
        old_refs, new_refs = self.interrepo.fetch_refs(update_refs)
0.200.1042 by Jelmer Vernooij
Fix pull into git branches.
782
        (result.old_revid, old_sha1) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
783
        if result.old_revid is None:
784
            result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
785
        result.new_revid = new_refs[main_ref][1]
0.252.36 by Jelmer Vernooij
Fix pull.
786
        return result
787
0.252.38 by Jelmer Vernooij
Minor cleanups.
788
    def push(self, overwrite=False, stop_revision=None,
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
789
             _override_hook_source_branch=None):
0.252.5 by Jelmer Vernooij
enable 'bzr push'.
790
        from dulwich.protocol import ZERO_SHA
791
        result = GitBranchPushResult()
792
        result.source_branch = self.source
793
        result.target_branch = self.target
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
794
        new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
0.200.941 by Jelmer Vernooij
Pass update_refs argument to fetch_refs.
795
        def update_refs(old_refs):
796
            refs = dict(old_refs)
0.200.945 by Jelmer Vernooij
Move fixmes
797
            # FIXME: Check for diverged branches
0.200.941 by Jelmer Vernooij
Pass update_refs argument to fetch_refs.
798
            refs.update(new_refs)
799
            return refs
800
        old_refs, new_refs = self.interrepo.fetch_refs(update_refs)
0.200.1035 by Jelmer Vernooij
Cope with tuples in refs dictionary.
801
        (result.old_revid, old_sha1) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
802
        if result.old_revid is None:
803
            result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
0.200.1042 by Jelmer Vernooij
Fix pull into git branches.
804
        result.new_revid = new_refs[main_ref][1]
0.252.5 by Jelmer Vernooij
enable 'bzr push'.
805
        return result
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
806
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
807
    def lossy_push(self, stop_revision=None):
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
808
        result = GitBranchPushResult()
0.200.503 by Jelmer Vernooij
Remove dpull, return BranchPushResult in lossy_push.
809
        result.source_branch = self.source
810
        result.target_branch = self.target
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
811
        new_refs, main_ref, stop_revinfo = self._get_new_refs(stop_revision)
0.200.942 by Jelmer Vernooij
pass update_refs to dfetch_refs.
812
        def update_refs(old_refs):
813
            refs = dict(old_refs)
0.200.945 by Jelmer Vernooij
Move fixmes
814
            # FIXME: Check for diverged branches
0.200.942 by Jelmer Vernooij
pass update_refs to dfetch_refs.
815
            refs.update(new_refs)
816
            return refs
817
        result.revidmap, old_refs, new_refs = self.interrepo.dfetch_refs(
818
            update_refs)
0.200.969 by Jelmer Vernooij
Use tuples with bzr revid and git sha to avoid lookups.
819
        result.old_revid = old_refs.get(self.target.ref, (None, NULL_REVISION))[1]
820
        result.new_revid = new_refs[main_ref][1]
0.200.1048 by Jelmer Vernooij
Make lookup of revno's after push/pull as efficient as possible.
821
        (result.new_original_revno, result.new_original_revid) = stop_revinfo
0.200.503 by Jelmer Vernooij
Remove dpull, return BranchPushResult in lossy_push.
822
        return result
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
823
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
824
825
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
826
branch.InterBranch.register_optimiser(InterFromGitBranch)
827
branch.InterBranch.register_optimiser(InterToGitBranch)
0.200.512 by Jelmer Vernooij
Support pushing git->git.
828
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)