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