/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

  • Committer: Robert Collins
  • Date: 2010-05-06 07:48:22 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506074822-0bsgf2j4h8jx0xkk
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
our first in-tree matcher. See the module docstring for details.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
19
 
from bzrlib.branch import Branch
20
 
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
 
 
33
 
 
34
 
class ForeignBranch(Branch):
35
 
    """Branch that exists in a foreign version control system."""
36
 
 
37
 
    def __init__(self, mapping):
38
 
        self.mapping = mapping
39
 
        super(ForeignBranch, self).__init__()
40
 
 
41
 
    def dpull(self, source, stop_revision=None):
42
 
        """Pull deltas from another branch.
43
 
 
44
 
        :note: This does not, like pull, retain the revision ids from 
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.
48
 
 
49
 
        :param source: Source branch
50
 
        :param stop_revision: Revision to pull, defaults to last revision.
51
 
        """
52
 
        raise NotImplementedError(self.dpull)
53
 
 
54
 
 
55
 
class FakeControlFiles(object):
56
 
    """Dummy implementation of ControlFiles.
57
 
    
58
 
    This is required as some code relies on controlfiles being 
59
 
    available."""
60
 
    def get_utf8(self, name):
61
 
        raise errors.NoSuchFile(name)
62
 
 
63
 
    def get(self, name):
64
 
        raise errors.NoSuchFile(name)
65
 
 
66
 
    def break_lock(self):
67
 
        pass
68
 
 
69
 
 
70
 
class cmd_dpush(Command):
71
 
    """Push diffs into a foreign version control system without any 
72
 
    Bazaar-specific metadata.
73
 
 
74
 
    This will afterwards rebase the local Bazaar branch on the remote
75
 
    branch unless the --no-rebase option is used, in which case 
76
 
    the two branches will be out of sync. 
77
 
    """
78
 
    takes_args = ['location?']
79
 
    takes_options = ['remember', Option('directory',
80
 
            help='Branch to push from, '
81
 
                 'rather than the one containing the working directory.',
82
 
            short_name='d',
83
 
            type=unicode,
84
 
            ),
85
 
            Option('no-rebase', help="Don't rebase after push")]
86
 
 
87
 
    def run(self, location=None, remember=False, directory=None, 
88
 
            no_rebase=False):
89
 
        from bzrlib import urlutils
90
 
        from bzrlib.bzrdir import BzrDir
91
 
        from bzrlib.errors import BzrCommandError, NoWorkingTree
92
 
        from bzrlib.trace import info
93
 
        from bzrlib.workingtree import WorkingTree
94
 
 
95
 
        if directory is None:
96
 
            directory = "."
97
 
        try:
98
 
            source_wt = WorkingTree.open_containing(directory)[0]
99
 
            source_branch = source_wt.branch
100
 
        except NoWorkingTree:
101
 
            source_branch = Branch.open_containing(directory)[0]
102
 
            source_wt = None
103
 
        stored_loc = source_branch.get_push_location()
104
 
        if location is None:
105
 
            if stored_loc is None:
106
 
                raise BzrCommandError("No push location known or specified.")
107
 
            else:
108
 
                display_url = urlutils.unescape_for_display(stored_loc,
109
 
                        self.outf.encoding)
110
 
                self.outf.write("Using saved location: %s\n" % display_url)
111
 
                location = stored_loc
112
 
 
113
 
        bzrdir = BzrDir.open(location)
114
 
        target_branch = bzrdir.open_branch()
115
 
        target_branch.lock_write()
116
 
        if not isinstance(target_branch, ForeignBranch):
117
 
            info("target branch is not a foreign branch, using regular push.")
118
 
            target_branch.pull(source_branch)
119
 
            no_rebase = True
120
 
        else:
121
 
            revid_map = target_branch.dpull(source_branch)
122
 
        # We successfully created the target, remember it
123
 
        if source_branch.get_push_location() is None or remember:
124
 
            source_branch.set_push_location(target_branch.base)
125
 
        if not no_rebase:
126
 
            _, old_last_revid = source_branch.last_revision_info()
127
 
            new_last_revid = revid_map[old_last_revid]
128
 
            if source_wt is not None:
129
 
                source_wt.pull(target_branch, overwrite=True, 
130
 
                               stop_revision=new_last_revid)
131
 
            else:
132
 
                source_branch.pull(target_branch, overwrite=True, 
133
 
                                   stop_revision=new_last_revid)
134
 
 
135
 
def test_suite():
136
 
    from unittest import TestSuite
137
 
    from bzrlib.tests import TestUtil
138
 
    loader = TestUtil.TestLoader()
139
 
    suite = TestSuite()
140
 
    testmod_names = ['test_versionedfiles', ]
141
 
    suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
142
 
    return suite
143
 
 
144
 
 
145
 
def escape_commit_message(message):
146
 
    """Replace xml-incompatible control characters."""
147
 
    if message is None:
148
 
        return None
149
 
    import re
150
 
    # FIXME: RBC 20060419 this should be done by the revision
151
 
    # serialiser not by commit. Then we can also add an unescaper
152
 
    # in the deserializer and start roundtripping revision messages
153
 
    # precisely. See repository_implementations/test_repository.py
154
 
    
155
 
    # Python strings can include characters that can't be
156
 
    # represented in well-formed XML; escape characters that
157
 
    # aren't listed in the XML specification
158
 
    # (http://www.w3.org/TR/REC-xml/#NT-Char).
159
 
    message, _ = re.subn(
160
 
        u'[^\x09\x0A\x0D\u0020-\uD7FF\uE000-\uFFFD]+',
161
 
        lambda match: match.group(0).encode('unicode_escape'),
162
 
        message)
163
 
    return message
164
 
 
165