/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/foreign.py

  • Committer: John Arbash Meinel
  • Date: 2011-04-22 14:12:22 UTC
  • mfrom: (5809 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5836.
  • Revision ID: john@arbash-meinel.com-20110422141222-nx2j0hbkihcb8j16
Merge newer bzr.dev and resolve conflicts.
Try to write some documentation about how the _dirblock_state works.
Fix up the tests so that they pass again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008-2012 Canonical Ltd
 
1
# Copyright (C) 2008-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
 
17
18
"""Foreign branch utilities."""
18
19
 
19
 
from .branch import (
 
20
 
 
21
from bzrlib.branch import (
20
22
    Branch,
 
23
    InterBranch,
21
24
    )
22
 
from .repository import Repository
23
 
from .revision import Revision
24
 
from . import (
 
25
from bzrlib.commands import Command, Option
 
26
from bzrlib.repository import Repository
 
27
from bzrlib.revision import Revision
 
28
from bzrlib.lazy_import import lazy_import
 
29
lazy_import(globals(), """
 
30
from bzrlib import (
25
31
    errors,
26
32
    registry,
 
33
    transform,
27
34
    )
28
 
 
 
35
""")
29
36
 
30
37
class VcsMapping(object):
31
38
    """Describes the mapping between the semantics of Bazaar and a foreign VCS.
77
84
        The factory must be a callable that takes one parameter: the key.
78
85
        It must produce an instance of VcsMapping when called.
79
86
        """
80
 
        if b":" in key:
 
87
        if ":" in key:
81
88
            raise ValueError("mapping name can not contain colon (:)")
82
89
        registry.Registry.register(self, key, factory, help)
83
90
 
104
111
    """
105
112
 
106
113
    def __init__(self, foreign_revid, mapping, *args, **kwargs):
107
 
        if "inventory_sha1" not in kwargs:
108
 
            kwargs["inventory_sha1"] = b""
 
114
        if not "inventory_sha1" in kwargs:
 
115
            kwargs["inventory_sha1"] = ""
109
116
        super(ForeignRevision, self).__init__(*args, **kwargs)
110
117
        self.foreign_revid = foreign_revid
111
118
        self.mapping = mapping
133
140
        :param foreign_revid: Foreign revision id.
134
141
        :return: Dictionary mapping string keys to string values.
135
142
        """
136
 
        return {}
 
143
        return { }
137
144
 
138
145
    def serialize_foreign_revid(self, foreign_revid):
139
146
        """Serialize a foreign revision id for this VCS.
140
147
 
141
148
        :param foreign_revid: Foreign revision id
142
 
        :return: Bytestring with serialized revid, will not contain any
 
149
        :return: Bytestring with serialized revid, will not contain any 
143
150
            newlines.
144
151
        """
145
152
        raise NotImplementedError(self.serialize_foreign_revid)
171
178
        :param revid: The bzr revision id
172
179
        :return: tuple with foreign revid and vcs mapping
173
180
        """
174
 
        if b":" not in revid or b"-" not in revid:
 
181
        if not ":" in revid or not "-" in revid:
175
182
            raise errors.InvalidRevisionId(revid, None)
176
183
        try:
177
 
            foreign_vcs = self.get(revid.split(b"-")[0].decode('ascii'))
 
184
            foreign_vcs = self.get(revid.split("-")[0])
178
185
        except KeyError:
179
186
            raise errors.InvalidRevisionId(revid, None)
180
187
        return foreign_vcs.mapping_registry.revision_id_bzr_to_foreign(revid)
224
231
    def __init__(self, mapping):
225
232
        self.mapping = mapping
226
233
        super(ForeignBranch, self).__init__()
 
234
 
 
235
 
 
236
def update_workingtree_fileids(wt, target_tree):
 
237
    """Update the file ids in a working tree based on another tree.
 
238
 
 
239
    :param wt: Working tree in which to update file ids
 
240
    :param target_tree: Tree to retrieve new file ids from, based on path
 
241
    """
 
242
    tt = transform.TreeTransform(wt)
 
243
    try:
 
244
        for f, p, c, v, d, n, k, e in target_tree.iter_changes(wt):
 
245
            if v == (True, False):
 
246
                trans_id = tt.trans_id_tree_path(p[0])
 
247
                tt.unversion_file(trans_id)
 
248
            elif v == (False, True):
 
249
                trans_id = tt.trans_id_tree_path(p[1])
 
250
                tt.version_file(f, trans_id)
 
251
        tt.apply()
 
252
    finally:
 
253
        tt.finalize()
 
254
    if len(wt.get_parent_ids()) == 1:
 
255
        wt.set_parent_trees([(target_tree.get_revision_id(), target_tree)])
 
256
    else:
 
257
        wt.set_last_revision(target_tree.get_revision_id())
 
258
 
 
259
 
 
260
class cmd_dpush(Command):
 
261
    __doc__ = """Push into a different VCS without any custom bzr metadata.
 
262
 
 
263
    This will afterwards rebase the local branch on the remote
 
264
    branch unless the --no-rebase option is used, in which case 
 
265
    the two branches will be out of sync after the push. 
 
266
    """
 
267
    hidden = True
 
268
    takes_args = ['location?']
 
269
    takes_options = [
 
270
        'remember',
 
271
        Option('directory',
 
272
               help='Branch to push from, '
 
273
               'rather than the one containing the working directory.',
 
274
               short_name='d',
 
275
               type=unicode,
 
276
               ),
 
277
        Option('no-rebase', help="Do not rebase after push."),
 
278
        Option('strict',
 
279
               help='Refuse to push if there are uncommitted changes in'
 
280
               ' the working tree, --no-strict disables the check.'),
 
281
        ]
 
282
 
 
283
    def run(self, location=None, remember=False, directory=None,
 
284
            no_rebase=False, strict=None):
 
285
        from bzrlib import urlutils
 
286
        from bzrlib.bzrdir import BzrDir
 
287
        from bzrlib.errors import BzrCommandError, NoWorkingTree
 
288
        from bzrlib.workingtree import WorkingTree
 
289
 
 
290
        if directory is None:
 
291
            directory = "."
 
292
        try:
 
293
            source_wt = WorkingTree.open_containing(directory)[0]
 
294
            source_branch = source_wt.branch
 
295
        except NoWorkingTree:
 
296
            source_branch = Branch.open(directory)
 
297
            source_wt = None
 
298
        if source_wt is not None:
 
299
            source_wt.check_changed_or_out_of_date(
 
300
                strict, 'dpush_strict',
 
301
                more_error='Use --no-strict to force the push.',
 
302
                more_warning='Uncommitted changes will not be pushed.')
 
303
        stored_loc = source_branch.get_push_location()
 
304
        if location is None:
 
305
            if stored_loc is None:
 
306
                raise BzrCommandError("No push location known or specified.")
 
307
            else:
 
308
                display_url = urlutils.unescape_for_display(stored_loc,
 
309
                        self.outf.encoding)
 
310
                self.outf.write("Using saved location: %s\n" % display_url)
 
311
                location = stored_loc
 
312
 
 
313
        bzrdir = BzrDir.open(location)
 
314
        target_branch = bzrdir.open_branch()
 
315
        target_branch.lock_write()
 
316
        try:
 
317
            try:
 
318
                push_result = source_branch.lossy_push(target_branch)
 
319
            except errors.LossyPushToSameVCS:
 
320
                raise BzrCommandError("%r and %r are in the same VCS, lossy "
 
321
                    "push not necessary. Please use regular push." %
 
322
                    (source_branch, target_branch))
 
323
            # We successfully created the target, remember it
 
324
            if source_branch.get_push_location() is None or remember:
 
325
                source_branch.set_push_location(target_branch.base)
 
326
            if not no_rebase:
 
327
                old_last_revid = source_branch.last_revision()
 
328
                source_branch.pull(target_branch, overwrite=True)
 
329
                new_last_revid = source_branch.last_revision()
 
330
                if source_wt is not None and old_last_revid != new_last_revid:
 
331
                    source_wt.lock_write()
 
332
                    try:
 
333
                        target = source_wt.branch.repository.revision_tree(
 
334
                            new_last_revid)
 
335
                        update_workingtree_fileids(source_wt, target)
 
336
                    finally:
 
337
                        source_wt.unlock()
 
338
            push_result.report(self.outf)
 
339
        finally:
 
340
            target_branch.unlock()
 
341
 
 
342
 
 
343
class InterToForeignBranch(InterBranch):
 
344
 
 
345
    def lossy_push(self, stop_revision=None):
 
346
        """Push deltas into another branch.
 
347
 
 
348
        :note: This does not, like push, retain the revision ids from 
 
349
            the source branch and will, rather than adding bzr-specific 
 
350
            metadata, push only those semantics of the revision that can be 
 
351
            natively represented by this branch' VCS.
 
352
 
 
353
        :param target: Target branch
 
354
        :param stop_revision: Revision to push, defaults to last revision.
 
355
        :return: BranchPushResult with an extra member revidmap: 
 
356
            A dictionary mapping revision ids from the target branch 
 
357
            to new revision ids in the target branch, for each 
 
358
            revision that was pushed.
 
359
        """
 
360
        raise NotImplementedError(self.lossy_push)