/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.205.1 by Jelmer Vernooij
Import utility functions for foreign branches.
1
# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Foreign branch utilities."""
18
0.207.1 by Jelmer Vernooij
Add ForeignBranch class, make dpush fallback to regular push.
19
from bzrlib.branch import Branch
0.205.7 by Jelmer Vernooij
Import dpush.
20
from bzrlib.commands import Command, Option
0.219.1 by Jelmer Vernooij
Add ForeignRepository.
21
from bzrlib.repository import Repository
0.205.19 by Jelmer Vernooij
import bzr-svn improvements.
22
from bzrlib.revision import Revision
0.205.23 by Jelmer Vernooij
Add more docstrings.
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
0.205.6 by Jelmer Vernooij
Import FakeControlFiles.
33
0.207.1 by Jelmer Vernooij
Add ForeignBranch class, make dpush fallback to regular push.
34
class ForeignBranch(Branch):
0.205.18 by Jelmer Vernooij
Add docstrings.
35
    """Branch that exists in a foreign version control system."""
0.207.1 by Jelmer Vernooij
Add ForeignBranch class, make dpush fallback to regular push.
36
37
    def __init__(self, mapping):
0.205.21 by Jelmer Vernooij
Only register foreign property show function once.
38
        self.mapping = mapping
0.207.1 by Jelmer Vernooij
Add ForeignBranch class, make dpush fallback to regular push.
39
        super(ForeignBranch, self).__init__()
40
41
    def dpull(self, source, stop_revision=None):
0.205.18 by Jelmer Vernooij
Add docstrings.
42
        """Pull deltas from another branch.
43
44
        :note: This does not, like pull, retain the revision ids from 
0.205.23 by Jelmer Vernooij
Add more docstrings.
45
        the source branch and will, rather than adding bzr-specific metadata,
46
        push only those semantics of the revision that can be natively 
47
        represented in this branch.
0.205.18 by Jelmer Vernooij
Add docstrings.
48
49
        :param source: Source branch
50
        :param stop_revision: Revision to pull, defaults to last revision.
51
        """
0.205.25 by Jelmer Vernooij
Fix function names.
52
        raise NotImplementedError(self.dpull)
0.207.1 by Jelmer Vernooij
Add ForeignBranch class, make dpush fallback to regular push.
53
54
0.205.24 by Jelmer Vernooij
Remove elements pending to be included in bzrlib.
55
class ForeignRepository(Repository):
0.205.22 by Jelmer Vernooij
Merge improvements from bzr-svn.
56
57
    def has_foreign_revision(self, foreign_revid):
58
        raise NotImplementedError(self.has_foreign_revision)
59
0.205.25 by Jelmer Vernooij
Fix function names.
60
    def _all_revision_ids(self, mapping=None):
61
        raise NotImplementedError(self._all_revision_ids)
0.205.22 by Jelmer Vernooij
Merge improvements from bzr-svn.
62
63
    def get_mapping(self):
0.205.25 by Jelmer Vernooij
Fix function names.
64
        raise NotImplementedError(self.get_mapping)
0.205.22 by Jelmer Vernooij
Merge improvements from bzr-svn.
65
66
    def get_inventory_xml(self, revision_id):
67
        """See Repository.get_inventory_xml()."""
68
        return self.serialise_inventory(self.get_inventory(revision_id))
69
70
    def get_inventory_sha1(self, revision_id):
71
        """Get the sha1 for the XML representation of an inventory.
72
73
        :param revision_id: Revision id of the inventory for which to return 
74
         the SHA1.
75
        :return: XML string
76
        """
77
78
        return osutils.sha_string(self.get_inventory_xml(revision_id))
79
80
    def get_revision_xml(self, revision_id):
81
        """Return the XML representation of a revision.
82
83
        :param revision_id: Revision for which to return the XML.
84
        :return: XML string
85
        """
86
        return self._serializer.write_revision_to_string(self.get_revision(revision_id))
87
88
89
0.219.1 by Jelmer Vernooij
Add ForeignRepository.
90
class ForeignRepository(Repository):
91
92
    def has_foreign_revision(self, foreign_revid):
93
        raise NotImplementedError(self.has_foreign_revision)
94
95
    def all_revision_ids(self, mapping=None):
96
        raise NotImplementedError(self.all_revision_ids)
97
98
    def get_mapping(self):
99
        raise NotImplementedError(self.get_default_mapping)
100
101
    def get_inventory_xml(self, revision_id):
102
        """See Repository.get_inventory_xml()."""
103
        return self.serialise_inventory(self.get_inventory(revision_id))
104
105
    def get_inventory_sha1(self, revision_id):
106
        """Get the sha1 for the XML representation of an inventory.
107
108
        :param revision_id: Revision id of the inventory for which to return 
109
         the SHA1.
110
        :return: XML string
111
        """
112
113
        return osutils.sha_string(self.get_inventory_xml(revision_id))
114
115
    def get_revision_xml(self, revision_id):
116
        """Return the XML representation of a revision.
117
118
        :param revision_id: Revision for which to return the XML.
119
        :return: XML string
120
        """
121
        return self._serializer.write_revision_to_string(self.get_revision(revision_id))
122
123
124
0.205.6 by Jelmer Vernooij
Import FakeControlFiles.
125
class FakeControlFiles(object):
126
    """Dummy implementation of ControlFiles.
127
    
128
    This is required as some code relies on controlfiles being 
129
    available."""
130
    def get_utf8(self, name):
131
        raise errors.NoSuchFile(name)
132
133
    def get(self, name):
134
        raise errors.NoSuchFile(name)
135
136
    def break_lock(self):
137
        pass
138
0.205.7 by Jelmer Vernooij
Import dpush.
139
140
class cmd_dpush(Command):
0.205.8 by Jelmer Vernooij
Remove references to svn.
141
    """Push diffs into a foreign version control system without any 
142
    Bazaar-specific metadata.
0.205.7 by Jelmer Vernooij
Import dpush.
143
0.205.8 by Jelmer Vernooij
Remove references to svn.
144
    This will afterwards rebase the local Bazaar branch on the remote
0.205.7 by Jelmer Vernooij
Import dpush.
145
    branch unless the --no-rebase option is used, in which case 
146
    the two branches will be out of sync. 
147
    """
148
    takes_args = ['location?']
149
    takes_options = ['remember', Option('directory',
150
            help='Branch to push from, '
151
                 'rather than the one containing the working directory.',
152
            short_name='d',
153
            type=unicode,
154
            ),
155
            Option('no-rebase', help="Don't rebase after push")]
156
157
    def run(self, location=None, remember=False, directory=None, 
158
            no_rebase=False):
159
        from bzrlib import urlutils
160
        from bzrlib.bzrdir import BzrDir
161
        from bzrlib.errors import BzrCommandError, NoWorkingTree
0.205.23 by Jelmer Vernooij
Add more docstrings.
162
        from bzrlib.trace import info
0.205.7 by Jelmer Vernooij
Import dpush.
163
        from bzrlib.workingtree import WorkingTree
164
165
        if directory is None:
166
            directory = "."
167
        try:
168
            source_wt = WorkingTree.open_containing(directory)[0]
169
            source_branch = source_wt.branch
170
        except NoWorkingTree:
171
            source_branch = Branch.open_containing(directory)[0]
172
            source_wt = None
173
        stored_loc = source_branch.get_push_location()
174
        if location is None:
175
            if stored_loc is None:
176
                raise BzrCommandError("No push location known or specified.")
177
            else:
178
                display_url = urlutils.unescape_for_display(stored_loc,
179
                        self.outf.encoding)
180
                self.outf.write("Using saved location: %s\n" % display_url)
181
                location = stored_loc
182
183
        bzrdir = BzrDir.open(location)
184
        target_branch = bzrdir.open_branch()
185
        target_branch.lock_write()
0.207.1 by Jelmer Vernooij
Add ForeignBranch class, make dpush fallback to regular push.
186
        if not isinstance(target_branch, ForeignBranch):
187
            info("target branch is not a foreign branch, using regular push.")
188
            target_branch.pull(source_branch)
189
            no_rebase = True
190
        else:
191
            revid_map = target_branch.dpull(source_branch)
0.205.7 by Jelmer Vernooij
Import dpush.
192
        # We successfully created the target, remember it
193
        if source_branch.get_push_location() is None or remember:
194
            source_branch.set_push_location(target_branch.base)
195
        if not no_rebase:
196
            _, old_last_revid = source_branch.last_revision_info()
197
            new_last_revid = revid_map[old_last_revid]
198
            if source_wt is not None:
199
                source_wt.pull(target_branch, overwrite=True, 
200
                               stop_revision=new_last_revid)
201
            else:
202
                source_branch.pull(target_branch, overwrite=True, 
203
                                   stop_revision=new_last_revid)
204
0.205.15 by Jelmer Vernooij
Add test_suite function.
205
def test_suite():
206
    from unittest import TestSuite
207
    from bzrlib.tests import TestUtil
208
    loader = TestUtil.TestLoader()
209
    suite = TestSuite()
0.205.19 by Jelmer Vernooij
import bzr-svn improvements.
210
    testmod_names = ['test_versionedfiles', ]
0.205.15 by Jelmer Vernooij
Add test_suite function.
211
    suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
212
    return suite
0.205.7 by Jelmer Vernooij
Import dpush.
213
0.205.18 by Jelmer Vernooij
Add docstrings.
214
0.207.2 by Jelmer Vernooij
Import escape commit message function.
215
def escape_commit_message(message):
216
    """Replace xml-incompatible control characters."""
217
    if message is None:
218
        return None
219
    import re
220
    # FIXME: RBC 20060419 this should be done by the revision
221
    # serialiser not by commit. Then we can also add an unescaper
222
    # in the deserializer and start roundtripping revision messages
223
    # precisely. See repository_implementations/test_repository.py
224
    
225
    # Python strings can include characters that can't be
226
    # represented in well-formed XML; escape characters that
227
    # aren't listed in the XML specification
228
    # (http://www.w3.org/TR/REC-xml/#NT-Char).
229
    message, _ = re.subn(
230
        u'[^\x09\x0A\x0D\u0020-\uD7FF\uE000-\uFFFD]+',
231
        lambda match: match.group(0).encode('unicode_escape'),
232
        message)
233
    return message
234
0.205.19 by Jelmer Vernooij
import bzr-svn improvements.
235