/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.252 by Jelmer Vernooij
Clarify history, copyright.
2
# Copyright (C) 2009 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.261 by Jelmer Vernooij
More formatting fixes.
20
from dulwich.objects import (
21
    Commit,
22
    Tag,
23
    )
24
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
25
from bzrlib import (
26
    branch,
0.200.513 by Jelmer Vernooij
Fix imports.
27
    bzrdir,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
28
    config,
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
29
    errors,
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
30
    repository,
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
31
    revision,
0.200.82 by Jelmer Vernooij
Support listing tags.
32
    tag,
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
33
    transport,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
34
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
35
from bzrlib.decorators import (
36
    needs_read_lock,
37
    )
38
from bzrlib.trace import (
0.200.342 by Jelmer Vernooij
Report git sha during pull.
39
    is_quiet,
0.200.261 by Jelmer Vernooij
More formatting fixes.
40
    mutter,
41
    )
42
0.200.513 by Jelmer Vernooij
Fix imports.
43
from bzrlib.plugins.git import (
44
    get_rich_root_format,
45
    )
0.200.386 by Jelmer Vernooij
Move config to a separate file, support BranchConfig.username().
46
from bzrlib.plugins.git.config import (
47
    GitBranchConfig,
48
    )
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
49
from bzrlib.plugins.git.errors import (
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
50
    NoPushSupport,
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
51
    NoSuchRef,
52
    )
0.200.261 by Jelmer Vernooij
More formatting fixes.
53
0.238.5 by Jelmer Vernooij
Remove old backwards compatibility code.
54
from bzrlib.foreign import ForeignBranch
0.200.388 by Jelmer Vernooij
Support bzr 1.14 as well.
55
0.200.261 by Jelmer Vernooij
More formatting fixes.
56
0.200.462 by Jelmer Vernooij
Import tags when pulling.
57
def extract_tags(refs, mapping):
58
    ret = {}
59
    for k,v in refs.iteritems():
60
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
61
            v = refs.get(k+"^{}", v)
62
            ret[k[len("refs/tags/"):]] = mapping.revision_id_foreign_to_bzr(v)
63
    return ret
64
65
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
66
class GitPullResult(branch.PullResult):
67
68
    def _lookup_revno(self, revid):
69
        assert isinstance(revid, str), "was %r" % revid
70
        # Try in source branch first, it'll be faster
71
        return self.target_branch.revision_id_to_revno(revid)
72
73
    @property
74
    def old_revno(self):
75
        return self._lookup_revno(self.old_revid)
76
77
    @property
78
    def new_revno(self):
79
        return self._lookup_revno(self.new_revid)
80
81
0.200.261 by Jelmer Vernooij
More formatting fixes.
82
class LocalGitTagDict(tag.BasicTags):
83
    """Dictionary with tags in a local repository."""
0.200.82 by Jelmer Vernooij
Support listing tags.
84
0.200.89 by Jelmer Vernooij
Support sprouting branches.
85
    def __init__(self, branch):
86
        self.branch = branch
87
        self.repository = branch.repository
0.200.82 by Jelmer Vernooij
Support listing tags.
88
89
    def get_tag_dict(self):
90
        ret = {}
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
91
        for k,v in self.repository._git.refs.as_dict("refs/tags").iteritems():
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
92
            obj = self.repository._git.get_object(v)
93
            while isinstance(obj, Tag):
94
                v = obj.object[1]
95
                obj = self.repository._git.get_object(v)
96
            if not isinstance(obj, Commit):
0.200.261 by Jelmer Vernooij
More formatting fixes.
97
                mutter("Tag %s points at object %r that is not a commit, "
98
                       "ignoring", k, obj)
0.200.194 by Jelmer Vernooij
Look for commit object in heavyweight tags.
99
                continue
0.200.180 by Jelmer Vernooij
Simplify tag handling.
100
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
0.200.82 by Jelmer Vernooij
Support listing tags.
101
        return ret
102
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
103
    def set_tag(self, name, revid):
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
104
        self.repository._git.refs["refs/tags/%s" % name], _ = \
0.200.462 by Jelmer Vernooij
Import tags when pulling.
105
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
0.200.86 by Jelmer Vernooij
Clearer error when setting tags.
106
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
107
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
108
class DictTagDict(LocalGitTagDict):
109
110
111
    def __init__(self, branch, tags):
112
        super(DictTagDict, self).__init__(branch)
113
        self._tags = tags
114
115
    def get_tag_dict(self):
116
        return self._tags
117
118
119
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
120
class GitBranchFormat(branch.BranchFormat):
121
0.200.70 by Jelmer Vernooij
Implement GitBranchFormat.get_format_description.
122
    def get_format_description(self):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
123
        return 'Git Branch'
124
0.200.82 by Jelmer Vernooij
Support listing tags.
125
    def supports_tags(self):
126
        return True
127
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
128
    def make_tags(self, branch):
0.228.3 by Jelmer Vernooij
Fix tags when fetching from remotes.
129
        if getattr(branch.repository, "get_refs", None) is not None:
130
            from bzrlib.plugins.git.remote import RemoteGitTagDict
131
            return RemoteGitTagDict(branch)
0.200.261 by Jelmer Vernooij
More formatting fixes.
132
        else:
133
            return LocalGitTagDict(branch)
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
134
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
135
0.200.388 by Jelmer Vernooij
Support bzr 1.14 as well.
136
class GitBranch(ForeignBranch):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
137
    """An adapter to git repositories for bzr Branch objects."""
138
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
139
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
0.200.82 by Jelmer Vernooij
Support listing tags.
140
        self.repository = repository
0.200.246 by Jelmer Vernooij
Cope with API changes in 1.13.
141
        self._format = GitBranchFormat()
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
142
        self.control_files = lockfiles
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
143
        self.bzrdir = bzrdir
0.231.1 by Jelmer Vernooij
Check that regenerated objects have the expected sha1.
144
        super(GitBranch, self).__init__(repository.get_mapping())
0.239.1 by Jelmer Vernooij
Avoid re-connecting to fetch tags we already know.
145
        if tagsdict is not None:
146
            self.tags = DictTagDict(self, tagsdict)
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
147
        self.name = name
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
148
        self._head = None
0.200.143 by Jelmer Vernooij
Reoncile InterGitRepository objects.
149
        self.base = bzrdir.transport.base
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
150
0.238.3 by Jelmer Vernooij
Remove svn references, prefer git send format when submitting changes against a git branch.
151
    def get_child_submit_format(self):
152
        """Return the preferred format of submissions to this branch."""
153
        ret = self.get_config().get_user_option("child_submit_format")
154
        if ret is not None:
155
            return ret
156
        return "git"
157
0.200.293 by Jelmer Vernooij
Fix branch nicks.
158
    def _get_nick(self, local=False, possible_master_transports=None):
159
        """Find the nick name for this branch.
160
161
        :return: Branch nick
162
        """
163
        return self.name
164
0.200.331 by Jelmer Vernooij
Add stub for setting nick function.
165
    def _set_nick(self, nick):
166
        raise NotImplementedError
167
168
    nick = property(_get_nick, _set_nick)
0.200.293 by Jelmer Vernooij
Fix branch nicks.
169
0.200.412 by Jelmer Vernooij
Implement GitBranch.__repr__.
170
    def __repr__(self):
171
        return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
172
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
173
    def generate_revision_history(self, revid, old_revid=None):
174
        # FIXME: Check that old_revid is in the ancestry of revid
175
        newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
176
        self._set_head(newhead)
177
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
178
    def lock_write(self):
179
        self.control_files.lock_write()
180
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
181
    def get_stacked_on_url(self):
182
        # Git doesn't do stacking (yet...)
183
        return None
184
185
    def get_parent(self):
186
        """See Branch.get_parent()."""
0.200.312 by Jelmer Vernooij
Add notes about parent locations.
187
        # 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.
188
        return None
189
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
190
    def set_parent(self, url):
0.200.312 by Jelmer Vernooij
Add notes about parent locations.
191
        # FIXME: Set "origin" url in .git/config ?
0.200.175 by Jelmer Vernooij
Add optimized handling when fetching from git to git.
192
        pass
193
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
194
    def lock_read(self):
195
        self.control_files.lock_read()
196
0.200.432 by Jelmer Vernooij
Support Branch.is_locked, required for loggerhead.
197
    def is_locked(self):
198
        return self.control_files.is_locked()
199
0.200.139 by Jelmer Vernooij
Share more code between local and remote classes, support opening remote branches.
200
    def unlock(self):
201
        self.control_files.unlock()
202
203
    def get_physical_lock_status(self):
204
        return False
205
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
206
    @needs_read_lock
207
    def last_revision(self):
208
        # perhaps should escape this ?
0.200.57 by Jelmer Vernooij
Fix more tests.
209
        if self.head is None:
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
210
            return revision.NULL_REVISION
0.200.112 by Jelmer Vernooij
Fix the build.
211
        return self.mapping.revision_id_foreign_to_bzr(self.head)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
212
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
213
    def _basic_push(self, target, overwrite=False, stop_revision=None):
214
        return branch.InterBranch.get(self, target)._basic_push(
215
            overwrite, stop_revision)
216
0.200.465 by Jelmer Vernooij
Use dulwich standard functionality for finding missing revisions.
217
 
218
class LocalGitBranch(GitBranch):
219
    """A local Git branch."""
220
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
221
    def _get_checkout_format(self):
222
        """Return the most suitable metadir for a checkout of this branch.
223
        Weaves are used if this branch's repository uses weaves.
224
        """
225
        format = self.repository.bzrdir.checkout_metadir()
226
        format.set_branch_format(self._format)
227
        return format
228
0.200.261 by Jelmer Vernooij
More formatting fixes.
229
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
230
        accelerator_tree=None, hardlink=False):
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
231
        if lightweight:
0.230.1 by Jelmer Vernooij
Support lightweight checkouts.
232
            t = transport.get_transport(to_location)
233
            t.ensure_base()
234
            format = self._get_checkout_format()
235
            checkout = format.initialize_on_transport(t)
236
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
237
                self)
238
            tree = checkout.create_workingtree(revision_id,
239
                from_branch=from_branch, hardlink=hardlink)
240
            return tree
241
        else:
242
            return self._create_heavyweight_checkout(to_location, revision_id,
0.200.261 by Jelmer Vernooij
More formatting fixes.
243
            hardlink)
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
244
245
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
246
                                     hardlink=False):
247
        """Create a new heavyweight checkout of this branch.
248
249
        :param to_location: URL of location to create the new checkout in.
250
        :param revision_id: Revision that should be the tip of the checkout.
251
        :param hardlink: Whether to hardlink
252
        :return: WorkingTree object of checkout.
253
        """
0.200.513 by Jelmer Vernooij
Fix imports.
254
        checkout_branch = bzrdir.BzrDir.create_branch_convenience(
0.200.210 by Jelmer Vernooij
properly error out about not support lightweight checkouts.
255
            to_location, force_new_tree=False, format=get_rich_root_format())
256
        checkout = checkout_branch.bzrdir
257
        checkout_branch.bind(self)
258
        # pull up to the specified revision_id to set the initial 
259
        # branch tip correctly, and seed it with history.
260
        checkout_branch.pull(self, stop_revision=revision_id)
261
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
262
0.200.57 by Jelmer Vernooij
Fix more tests.
263
    def _gen_revision_history(self):
0.200.58 by Jelmer Vernooij
Fix remaining tests.
264
        if self.head is None:
265
            return []
0.200.261 by Jelmer Vernooij
More formatting fixes.
266
        ret = list(self.repository.iter_reverse_revision_history(
267
            self.last_revision()))
0.200.59 by Jelmer Vernooij
Add more tests, fix revision history.
268
        ret.reverse()
0.200.57 by Jelmer Vernooij
Fix more tests.
269
        return ret
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
270
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
271
    def _get_head(self):
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
272
        try:
273
            return self.repository._git.ref(self.name)
274
        except KeyError:
275
            return None
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
276
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
277
    def set_last_revision_info(self, revno, revid):
278
        self.set_last_revision(revid)
279
280
    def set_last_revision(self, revid):
0.200.523 by Jelmer Vernooij
Fix undefined error.
281
        (newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
282
                revid)
0.200.523 by Jelmer Vernooij
Fix undefined error.
283
        self.head = newhead
0.200.507 by Jelmer Vernooij
Implement set_last_revision{_info,}.
284
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
285
    def _set_head(self, value):
286
        self._head = value
0.200.480 by Jelmer Vernooij
Cope with API changes in Dulwich.
287
        self.repository._git.refs[self.name] = self._head
0.200.461 by Jelmer Vernooij
Reduce number of round trips when fetching from Git.
288
        self._clear_cached_state()
289
290
    head = property(_get_head, _set_head)
291
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
292
    def get_config(self):
293
        return GitBranchConfig(self)
294
295
    def get_push_location(self):
296
        """See Branch.get_push_location."""
297
        push_loc = self.get_config().get_user_option('push_location')
298
        return push_loc
299
300
    def set_push_location(self, location):
301
        """See Branch.set_push_location."""
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
302
        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.
303
                                          store=config.STORE_LOCATION)
0.200.43 by David Allouche
Ultra-experimental support for "bzr pull". No test. No sanity.
304
305
    def supports_tags(self):
0.200.82 by Jelmer Vernooij
Support listing tags.
306
        return True
0.200.96 by Jelmer Vernooij
Fix branch.
307
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
308
0.200.342 by Jelmer Vernooij
Report git sha during pull.
309
class GitBranchPullResult(branch.PullResult):
310
311
    def report(self, to_file):
312
        if not is_quiet():
313
            if self.old_revid == self.new_revid:
314
                to_file.write('No revisions to pull.\n')
315
            else:
316
                to_file.write('Now on revision %d (git sha: %s).\n' % 
317
                        (self.new_revno, self.new_git_head))
318
        self._show_tag_conficts(to_file)
319
320
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
321
class GitBranchPushResult(branch.BranchPushResult):
322
323
    def _lookup_revno(self, revid):
324
        assert isinstance(revid, str), "was %r" % revid
325
        # Try in source branch first, it'll be faster
326
        try:
327
            return self.source_branch.revision_id_to_revno(revid)
0.200.523 by Jelmer Vernooij
Fix undefined error.
328
        except errors.NoSuchRevision:
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
329
            # FIXME: Check using graph.find_distance_to_null() ?
330
            return self.target_branch.revision_id_to_revno(revid)
331
332
    @property
333
    def old_revno(self):
334
        return self._lookup_revno(self.old_revid)
335
336
    @property
337
    def new_revno(self):
338
        return self._lookup_revno(self.new_revid)
339
340
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
341
class InterFromGitBranch(branch.GenericInterBranch):
0.200.261 by Jelmer Vernooij
More formatting fixes.
342
    """InterBranch implementation that pulls from Git into bzr."""
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
343
344
    @classmethod
345
    def is_compatible(self, source, target):
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
346
        return (isinstance(source, GitBranch) and 
347
                not isinstance(target, GitBranch))
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
348
349
    def update_revisions(self, stop_revision=None, overwrite=False,
350
        graph=None):
351
        """See InterBranch.update_revisions()."""
0.226.2 by Jelmer Vernooij
Cope with new fetch_spec argument.
352
        interrepo = repository.InterRepository.get(self.source.repository, 
353
            self.target.repository)
0.200.342 by Jelmer Vernooij
Report git sha during pull.
354
        self._head = None
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
355
        self._last_revid = None
356
        def determine_wants(heads):
357
            if not self.source.name in heads:
0.200.278 by Jelmer Vernooij
Update branch head appropriately during dpull.
358
                raise NoSuchRef(self.source.name, heads.keys())
0.200.314 by Jelmer Vernooij
Support stop_revision.
359
            if stop_revision is not None:
360
                self._last_revid = stop_revision
0.200.342 by Jelmer Vernooij
Report git sha during pull.
361
                self._head, mapping = self.source.repository.lookup_git_revid(
0.200.316 by Jelmer Vernooij
Fix formatting.
362
                    stop_revision)
0.200.314 by Jelmer Vernooij
Support stop_revision.
363
            else:
0.200.342 by Jelmer Vernooij
Report git sha during pull.
364
                self._head = heads[self.source.name]
0.200.316 by Jelmer Vernooij
Fix formatting.
365
                self._last_revid = \
0.200.342 by Jelmer Vernooij
Report git sha during pull.
366
                    self.source.mapping.revision_id_foreign_to_bzr(self._head)
0.200.226 by Jelmer Vernooij
Merge thin-pack work.
367
            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.
368
                return []
0.200.342 by Jelmer Vernooij
Report git sha during pull.
369
            return [self._head]
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
370
        interrepo.fetch_objects(determine_wants, self.source.mapping)
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
371
        if overwrite:
0.200.314 by Jelmer Vernooij
Support stop_revision.
372
            prev_last_revid = None
0.200.313 by Jelmer Vernooij
Support overwrite parameter.
373
        else:
0.200.314 by Jelmer Vernooij
Support stop_revision.
374
            prev_last_revid = self.target.last_revision()
375
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
0.200.225 by Jelmer Vernooij
Implement custom InterBranch to support fetching from remote git branches.
376
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
377
    def pull(self, overwrite=False, stop_revision=None,
378
             possible_transports=None, _hook_master=None, run_hooks=True,
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
379
             _override_hook_target=None, local=False):
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
380
        """See Branch.pull.
381
382
        :param _hook_master: Private parameter - set the branch to
383
            be supplied as the master to pull hooks.
384
        :param run_hooks: Private parameter - if false, this branch
385
            is being called because it's the master of the primary branch,
386
            so it should not run its hooks.
387
        :param _override_hook_target: Private parameter - set the branch to be
388
            supplied as the target_branch to pull hooks.
389
        """
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
390
        # This type of branch can't be bound.
391
        if local:
392
            raise errors.LocalRequiresBoundBranch()
0.200.342 by Jelmer Vernooij
Report git sha during pull.
393
        result = GitBranchPullResult()
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
394
        result.source_branch = self.source
395
        if _override_hook_target is None:
396
            result.target_branch = self.target
397
        else:
398
            result.target_branch = _override_hook_target
399
        self.source.lock_read()
400
        try:
401
            # We assume that during 'pull' the target repository is closer than
402
            # the source one.
403
            graph = self.target.repository.get_graph(self.source.repository)
404
            result.old_revno, result.old_revid = \
405
                self.target.last_revision_info()
0.200.342 by Jelmer Vernooij
Report git sha during pull.
406
            self.update_revisions(stop_revision, overwrite=overwrite, 
407
                graph=graph)
408
            result.new_git_head = self._head
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
409
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
410
                overwrite)
411
            result.new_revno, result.new_revid = self.target.last_revision_info()
412
            if _hook_master:
413
                result.master_branch = _hook_master
414
                result.local_branch = result.target_branch
415
            else:
416
                result.master_branch = result.target_branch
417
                result.local_branch = None
418
            if run_hooks:
419
                for hook in branch.Branch.hooks['post_pull']:
420
                    hook(result)
421
        finally:
422
            self.source.unlock()
423
        return result
424
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
425
    def _basic_push(self, overwrite=False, stop_revision=None):
426
        result = branch.BranchPushResult()
427
        result.source_branch = self.source
428
        result.target_branch = self.target
0.200.505 by Jelmer Vernooij
Remove duplicate code.
429
        graph = self.target.repository.get_graph(self.source.repository)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
430
        result.old_revno, result.old_revid = self.target.last_revision_info()
431
        self.update_revisions(stop_revision, overwrite=overwrite, 
432
            graph=graph)
433
        result.new_git_head = self._head
434
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
435
            overwrite)
436
        result.new_revno, result.new_revid = self.target.last_revision_info()
437
        return result
438
0.200.338 by Jelmer Vernooij
Fix dpushing without changes necessary.
439
0.200.512 by Jelmer Vernooij
Support pushing git->git.
440
class InterGitBranch(branch.GenericInterBranch):
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
441
    """InterBranch implementation that pulls between Git branches."""
442
0.200.512 by Jelmer Vernooij
Support pushing git->git.
443
444
class InterGitLocalRemoteBranch(InterGitBranch):
445
    """InterBranch that copies from a local to a remote git branch."""
446
447
    @classmethod
448
    def is_compatible(self, source, target):
449
        from bzrlib.plugins.git.remote import RemoteGitBranch
450
        return (isinstance(source, LocalGitBranch) and 
451
                isinstance(target, RemoteGitBranch))
452
453
    def _basic_push(self, overwrite=False, stop_revision=None):
454
        result = GitBranchPushResult()
455
        result.source_branch = self.source
456
        result.target_branch = self.target
457
        if stop_revision is None:
458
            stop_revision = self.source.last_revision()
459
        # FIXME: Check for diverged branches
460
        def get_changed_refs(old_refs):
0.200.544 by Jelmer Vernooij
Support pushing from git -> empty git repo.
461
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
0.200.512 by Jelmer Vernooij
Support pushing git->git.
462
            refs = { "refs/heads/master": self.source.repository.lookup_git_revid(stop_revision)[0] }
463
            result.new_revid = stop_revision
464
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
465
                refs["refs/tags/%s" % name] = sha
466
            return refs
467
        self.target.repository.send_pack(get_changed_refs, 
468
                self.source.repository._git.object_store.generate_pack_contents)
469
        return result
470
471
472
class InterGitRemoteLocalBranch(InterGitBranch):
473
    """InterBranch that copies from a remote to a local git branch."""
474
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
475
    @classmethod
476
    def is_compatible(self, source, target):
477
        from bzrlib.plugins.git.remote import RemoteGitBranch
478
        return (isinstance(source, RemoteGitBranch) and 
479
                isinstance(target, LocalGitBranch))
480
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
481
    def _basic_push(self, overwrite=False, stop_revision=None):
482
        result = branch.BranchPushResult()
483
        result.source_branch = self.source
484
        result.target_branch = self.target
485
        result.old_revid = self.target.last_revision()
486
        refs, stop_revision = self.update_refs(stop_revision)
487
        self.target.generate_revision_history(stop_revision, result.old_revid)
488
        self.update_tags(refs)
0.200.505 by Jelmer Vernooij
Remove duplicate code.
489
        result.new_revid = self.target.last_revision()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
490
        return result
491
492
    def update_tags(self, refs):
493
        for name, revid in extract_tags(refs, self.target.mapping).iteritems():
494
            self.target.tags.set_tag(name, revid)
495
496
    def update_refs(self, stop_revision=None):
497
        interrepo = repository.InterRepository.get(self.source.repository, 
498
            self.target.repository)
499
        if stop_revision is None:
500
            refs = interrepo.fetch_refs(branches=["HEAD"])
501
            stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
502
        else:
503
            refs = interrepo.fetch_refs(revision_id=stop_revision)
504
        return refs, stop_revision
505
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
506
    def pull(self, stop_revision=None, overwrite=False, 
0.200.446 by Jelmer Vernooij
Support new 'local' argument.
507
        possible_transports=None, local=False):
508
        # This type of branch can't be bound.
509
        if local:
510
            raise errors.LocalRequiresBoundBranch()
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
511
        result = GitPullResult()
512
        result.source_branch = self.source
513
        result.target_branch = self.target
514
        result.old_revid = self.target.last_revision()
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
515
        refs, stop_revision = self.update_refs(stop_revision)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
516
        self.target.generate_revision_history(stop_revision, result.old_revid)
0.200.501 by Jelmer Vernooij
Support push from git into bzr.
517
        self.update_tags(refs)
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
518
        result.new_revid = self.target.last_revision()
519
        return result
520
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
521
    
522
class InterToGitBranch(branch.InterBranch):
523
    """InterBranch implementation that pulls from Git into bzr."""
524
525
    @classmethod
526
    def is_compatible(self, source, target):
527
        return (not isinstance(source, GitBranch) and 
528
                isinstance(target, GitBranch))
529
0.200.542 by Jelmer Vernooij
Proper error for push in 1.14.
530
    def update_revisions(self, *args, **kwargs):
531
        raise NoPushSupport()
532
0.200.472 by Jelmer Vernooij
Fix printing error when user attempts to push into git.
533
    def push(self, overwrite=True, stop_revision=None, 
534
             _override_hook_source_branch=None):
535
        raise NoPushSupport()
536
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
537
    def lossy_push(self, stop_revision=None):
0.200.504 by Jelmer Vernooij
Lazily find revno's for git branches.
538
        result = GitBranchPushResult()
0.200.503 by Jelmer Vernooij
Remove dpull, return BranchPushResult in lossy_push.
539
        result.source_branch = self.source
540
        result.target_branch = self.target
541
        result.old_revid = self.target.last_revision()
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
542
        if stop_revision is None:
543
            stop_revision = self.source.last_revision()
544
        # FIXME: Check for diverged branches
545
        refs = { "refs/heads/master": stop_revision }
546
        for name, revid in self.source.tags.get_tag_dict().iteritems():
547
            if self.source.repository.has_revision(revid):
548
                refs["refs/tags/%s" % name] = revid
549
        revidmap, new_refs = self.target.repository.dfetch_refs(
550
            self.source.repository, refs)
551
        if revidmap != {}:
552
            self.target.generate_revision_history(revidmap[stop_revision])
0.200.520 by Jelmer Vernooij
Proper output from dpush.
553
            result.new_revid = revidmap[stop_revision]
554
        else:
555
            result.new_revid = result.old_revid
0.200.503 by Jelmer Vernooij
Remove dpull, return BranchPushResult in lossy_push.
556
        result.revidmap = revidmap
557
        return result
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
558
0.200.334 by Jelmer Vernooij
Support pulling from git to git.
559
560
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
0.200.468 by Jelmer Vernooij
Move dpush logic onto InterBranch.
561
branch.InterBranch.register_optimiser(InterFromGitBranch)
562
branch.InterBranch.register_optimiser(InterToGitBranch)
0.200.512 by Jelmer Vernooij
Support pushing git->git.
563
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)