/breezy-svn/trunk

To get this branch, use:
bzr branch https://code.breezy-vcs.org/breezy-svn/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Copyright (C) 2005-2009 Jelmer Vernooij <jelmer@samba.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""bzr<->svn mapping format 2."""

from __future__ import absolute_import

from breezy.errors import (
    InvalidRevisionId,
    )
from breezy.bzr.inventory import (
    ROOT_ID,
    )
from breezy.sixish import text_type

from breezy.plugins.svn.errors import (
    LayoutUnusable,
    NotSvnBranchPath,
    NoLayoutTagSetSupport,
    )
from breezy.plugins.svn.layout import (
    RepositoryLayout,
    get_root_paths,
    )
from breezy.plugins.svn.layout.standard import (
    RootLayout,
    TrunkLayout,
    )
from breezy.plugins.svn.mapping import (
    BzrSvnMapping,
    escape_svn_path,
    unescape_svn_path,
    )

SVN_PROP_BZR_MERGE = 'bzr:merge'

class BzrSvnMappingv1(BzrSvnMapping):
    """This was the initial version of the mappings as used by bzr-svn
    0.2.

    It does not support pushing revisions to Subversion as-is, but only
    as part of a merge.
    """
    name = "v1"
    roundtripping = False
    revid_prefix = b"svn-v1"
    restricts_branch_paths = True
    can_use_fileprops= True

    def __init__(self, layout):
        super(BzrSvnMappingv1, self).__init__()
        self._layout = layout

    @classmethod
    def revision_id_bzr_to_foreign(cls, revid):
        if not revid.startswith("svn-v1:"):
            raise InvalidRevisionId(revid, "")
        revid = revid[len("svn-v1:"):]
        at = revid.index("@")
        fash = revid.rindex("-")
        uuid = revid[at+1:fash]
        branch_path = unescape_svn_path(revid[fash+1:])
        revnum = int(revid[0:at])
        assert revnum >= 0
        return (uuid, branch_path, revnum), cls(LegacyLayout.from_branch_path(branch_path))

    @classmethod
    def revision_id_foreign_to_bzr(cls, (uuid, path, revnum)):
        assert isinstance(path, str)
        return b"svn-v1:%d@%s-%s" % (revnum, uuid, escape_svn_path(path))

    def __eq__(self, other):
        return type(self) == type(other)

    def __ne__(self, other):
        return type(self) != type(other)

    def __hash__(self):
        return hash(type(self))

    def is_branch(self, branch_path):
        return self._layout.is_branch(branch_path)

    def is_tag(self, tag_path):
        return False

    def import_revision_fileprops(self, fileprops, rev):
        return True

    def generate_file_id(self, foreign_revid, inv_path):
        if inv_path == u"":
            return ROOT_ID
        revid = self.revision_id_foreign_to_bzr(foreign_revid)
        return b"%s-%s" % (revid, escape_svn_path(inv_path.encode("utf-8")))

    def import_fileid_map_fileprops(self, fileprops):
        return {}

    def import_text_parents_fileprops(self, fileprops):
        return {}

    def import_text_revisions_fileprops(self, fileprops):
        return {}

    def get_rhs_parents_fileprops(self, fileprops):
        value = fileprops.get(SVN_PROP_BZR_MERGE, "")
        if value == "":
            return ()
        return (value.splitlines()[-1])

    @classmethod
    def from_repository(cls, repository, _hinted_branch_path=None):
        if _hinted_branch_path is None:
            return cls(TrunkLegacyLayout())

        return cls(LegacyLayout.from_branch_path(_hinted_branch_path))

    @classmethod
    def get_test_instance(cls):
        return cls(TrunkLegacyLayout())

    def get_guessed_layout(self, repository):
        return self._layout

    def check_layout(self, repository, layout):
        if isinstance(layout, RootLayout):
            self._layout = RootLegacyLayout()
        elif isinstance(layout, TrunkLayout):
            self._layout = TrunkLegacyLayout(layout.level or 0)
        else:
            raise LayoutUnusable(layout, self)

    def supports_tags(self):
        return False


class BzrSvnMappingv2(BzrSvnMappingv1):
    """The second version of the mappings as used in the 0.3.x series.

    It does not support pushing revisions to Subversion as-is, but only
    as part of a merge.
    """
    name = "v2"
    roundtripping = False
    revid_prefix = b"svn-v2"
    restricts_branch_paths = True
    can_use_fileprops = True

    @classmethod
    def revision_id_bzr_to_foreign(cls, revid):
        if not revid.startswith("svn-v2:"):
            raise InvalidRevisionId(revid, "")
        revid = revid[len("svn-v2:"):]
        at = revid.index("@")
        fash = revid.rindex("-")
        uuid = revid[at+1:fash]
        branch_path = unescape_svn_path(revid[fash+1:])
        revnum = int(revid[0:at])
        assert revnum >= 0
        return (uuid, branch_path, revnum), cls(LegacyLayout.from_branch_path(branch_path))

    def revision_id_foreign_to_bzr(self, (uuid, path, revnum)):
        assert isinstance(path, str)
        return b"svn-v2:%d@%s-%s" % (revnum, uuid, escape_svn_path(path))

    def __eq__(self, other):
        return type(self) == type(other)

    def __ne__(self, other):
        return type(self) != type(other)

    def __hash__(self):
        return hash(type(self))


class LegacyLayout(RepositoryLayout):

    def get_tag_path(self, name, project=""):
        raise NoLayoutTagSetSupport(self)

    def get_branch_path(self, name, project=""):
        return None

    @classmethod
    def from_branch_path(cls, path):
        parts = path.strip("/").split("/")
        for i in range(0,len(parts)):
            if parts[i] == "trunk" or \
               parts[i] == "branches" or \
               parts[i] == "tags":
                return TrunkLegacyLayout(level=i)

        return RootLegacyLayout()


class TrunkLegacyLayout(LegacyLayout):

    def __init__(self, level=0):
        super(TrunkLegacyLayout, self).__init__()
        self.level = level

    def parse(self, path):
        assert isinstance(path, text_type)
        parts = path.strip(u"/").split(u"/")
        if len(parts) == 0 or self.level >= len(parts):
            raise NotSvnBranchPath(path, self)

        if parts[self.level] == u"trunk" or parts[self.level] == u"hooks":
            return ("branch",
                    u"/".join(parts[0:self.level]),
                    u"/".join(parts[0:self.level+1]).strip(u"/"),
                    u"/".join(parts[self.level+1:]).strip(u"/"))
        elif ((parts[self.level] == u"tags" or parts[self.level] == u"branches") and
              len(parts) >= self.level+2):
            return ("branch",
                    u"/".join(parts[0:self.level]),
                    u"/".join(parts[0:self.level+2]).strip(u"/"),
                    u"/".join(parts[self.level+2:]).strip(u"/"))
        else:
            raise NotSvnBranchPath(path, self)

    def is_branch(self, path, project=None):
        parts = path.strip(u"/").split(u"/")
        if len(parts) == self.level+1 and parts[self.level] == u"trunk":
            return True

        if len(parts) == self.level+2 and \
           (parts[self.level] == u"branches" or parts[self.level] == u"tags"):
            return True

        return False

    def get_branches(self, repository, revnum, project=u"", pb=None):
        return get_root_paths(repository,
             [(u"*/" * self.level) + x for x in u"branches/*", u"tags/*", u"trunk"],
             revnum, self.is_branch, project)

    def get_tags(self, repository, revnum, project=u"", pb=None):
        return []


class RootLegacyLayout(LegacyLayout):

    def parse(self, path):
        assert isinstance(path, text_type)
        return ("branch", u"", u"", path)

    def is_branch(self, path, project=None):
        return path == u""

    def get_branches(self, repository, revnum, project="", pb=None):
        return [(u"", u"", u"trunk", True)]

    def get_tags(self, repository, revnum, project="", pb=None):
        return []