/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 foreign/__init__.py

Add more tests for fetch code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
 
1
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
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
16
16
 
17
17
"""Foreign branch utilities."""
18
18
 
 
19
from bzrlib import errors
19
20
from bzrlib.branch import Branch
20
21
from bzrlib.commands import Command, Option
21
 
from bzrlib.repository import Repository
22
 
from bzrlib.revision import Revision
23
 
from bzrlib.lazy_import import lazy_import
24
 
lazy_import(globals(), """
25
 
from bzrlib import (
26
 
    errors,
27
 
    log,
28
 
    osutils,
29
 
    registry,
30
 
    )
31
 
""")
32
22
 
33
23
 
34
24
class ForeignBranch(Branch):
69
59
 
70
60
 
71
61
class cmd_dpush(Command):
72
 
    """Push diffs into a foreign version control system without any 
73
 
    Bazaar-specific metadata.
 
62
    """Push diffs into a foreign branch without any bzr-specific metadata.
74
63
 
75
64
    This will afterwards rebase the local Bazaar branch on the remote
76
65
    branch unless the --no-rebase option is used, in which case 
83
72
            short_name='d',
84
73
            type=unicode,
85
74
            ),
 
75
            Option("idmap-file", help="Write map with old and new revision ids.", type=str),
86
76
            Option('no-rebase', help="Don't rebase after push")]
87
77
 
88
78
    def run(self, location=None, remember=False, directory=None, 
89
 
            no_rebase=False):
 
79
            no_rebase=False, idmap_file=None):
90
80
        from bzrlib import urlutils
91
81
        from bzrlib.bzrdir import BzrDir
92
82
        from bzrlib.errors import BzrCommandError, NoWorkingTree
 
83
        from bzrlib.inventory import Inventory
 
84
        from bzrlib.revision import NULL_REVISION
93
85
        from bzrlib.trace import info
94
86
        from bzrlib.workingtree import WorkingTree
95
 
        from upgrade import update_workingtree_fileids
 
87
        from upgrade import update_workinginv_fileids
 
88
 
 
89
        def get_inv(wt, revid):
 
90
            if revid == NULL_REVISION:
 
91
                return Inventory()
 
92
            else:
 
93
                return wt.branch.repository.get_inventory(revid)
96
94
 
97
95
        if directory is None:
98
96
            directory = "."
116
114
        target_branch = bzrdir.open_branch()
117
115
        target_branch.lock_write()
118
116
        try:
119
 
            if not isinstance(target_branch, ForeignBranch):
 
117
            if getattr(target_branch, "dpull", None) is None:
120
118
                info("target branch is not a foreign branch, using regular push.")
121
119
                target_branch.pull(source_branch)
122
120
                no_rebase = True
123
121
            else:
124
122
                revid_map = target_branch.dpull(source_branch)
 
123
                if idmap_file:
 
124
                    f = open(idmap_file, "w")
 
125
                    try:
 
126
                        f.write("".join(["%s\t%s\n" % item for item in revid_map.iteritems()]))
 
127
                    finally:
 
128
                        f.close()
125
129
            # We successfully created the target, remember it
126
130
            if source_branch.get_push_location() is None or remember:
127
131
                source_branch.set_push_location(target_branch.base)
133
137
                                   stop_revision=new_last_revid)
134
138
                    source_wt.lock_write()
135
139
                    try:
136
 
                        update_workingtree_fileids(source_wt, 
137
 
                            source_wt.branch.repository.revision_tree(old_last_revid),
138
 
                            source_wt.branch.repository.revision_tree(new_last_revid))
 
140
                        update_workinginv_fileids(source_wt, 
 
141
                            get_inv(source_wt, old_last_revid), 
 
142
                            get_inv(source_wt, new_last_revid))
139
143
                    finally:
140
144
                        source_wt.unlock()
141
145
                else:
144
148
        finally:
145
149
            target_branch.unlock()
146
150
 
 
151
 
 
152
class cmd_foreign_mapping_upgrade(Command):
 
153
    """Upgrade revisions mapped from a foreign version control system.
 
154
    
 
155
    This will change the identity of revisions whose parents 
 
156
    were mapped from revisions in the other version control system.
 
157
 
 
158
    You are recommended to run "bzr check" in the local repository 
 
159
    after running this command.
 
160
    """
 
161
    aliases = ['svn-upgrade']
 
162
    takes_args = ['from_repository?']
 
163
    takes_options = ['verbose', 
 
164
            Option("idmap-file", help="Write map with old and new revision ids.", type=str)]
 
165
 
 
166
    def run(self, from_repository=None, verbose=False, idmap_file=None):
 
167
        from upgrade import upgrade_branch, upgrade_workingtree
 
168
        from bzrlib.branch import Branch
 
169
        from bzrlib.errors import NoWorkingTree, BzrCommandError
 
170
        from bzrlib.repository import Repository
 
171
        from bzrlib.trace import info
 
172
        from bzrlib.workingtree import WorkingTree
 
173
        try:
 
174
            wt_to = WorkingTree.open(".")
 
175
            branch_to = wt_to.branch
 
176
        except NoWorkingTree:
 
177
            wt_to = None
 
178
            branch_to = Branch.open(".")
 
179
 
 
180
        stored_loc = branch_to.get_parent()
 
181
        if from_repository is None:
 
182
            if stored_loc is None:
 
183
                raise BzrCommandError("No pull location known or"
 
184
                                             " specified.")
 
185
            else:
 
186
                import bzrlib.urlutils as urlutils
 
187
                display_url = urlutils.unescape_for_display(stored_loc,
 
188
                        self.outf.encoding)
 
189
                self.outf.write("Using saved location: %s\n" % display_url)
 
190
                from_repository = Branch.open(stored_loc).repository
 
191
        else:
 
192
            from_repository = Repository.open(from_repository)
 
193
 
 
194
        vcs = getattr(from_repository, "vcs", None)
 
195
        if vcs is None:
 
196
            raise BzrCommandError("Repository at %s is not a foreign repository.a" % from_repository.base)
 
197
 
 
198
        new_mapping = from_repository.get_mapping()
 
199
 
 
200
        if wt_to is not None:
 
201
            renames = upgrade_workingtree(wt_to, from_repository, 
 
202
                                          new_mapping=new_mapping,
 
203
                                          allow_changes=True, verbose=verbose)
 
204
        else:
 
205
            renames = upgrade_branch(branch_to, from_repository, 
 
206
                                     new_mapping=new_mapping,
 
207
                                     allow_changes=True, verbose=verbose)
 
208
 
 
209
        if renames == {}:
 
210
            info("Nothing to do.")
 
211
 
 
212
        if idmap_file is not None:
 
213
            f = open(idmap_file, 'w')
 
214
            try:
 
215
                for oldid, newid in renames.iteritems():
 
216
                    f.write("%s\t%s\n" % (oldid, newid))
 
217
            finally:
 
218
                f.close()
 
219
 
 
220
        if wt_to is not None:
 
221
            wt_to.set_last_revision(branch_to.last_revision())
 
222
 
 
223
 
147
224
def test_suite():
148
225
    from unittest import TestSuite
149
226
    from bzrlib.tests import TestUtil