/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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Support for Subversion branches

Bazaar can be used with Subversion branches through the bzr-svn plugin.

Most Bazaar commands should work fine with Subversion branches.

bzr-svn also adds new commands to Bazaar:

 - bzr svn-import
 - bzr svn-layout

For more information about bzr-svn, see the bzr-svn FAQ.

"""

from __future__ import absolute_import

import breezy

from .info import (
    bzr_plugin_version as version_info,
    bzr_compatible_versions,
    subvertpy_minimum_version,
    )

if version_info[3] == 'final':
    version_string = '%d.%d.%d' % version_info[:3]
else:
    version_string = '%d.%d.%d%s%d' % version_info
__version__ = version_string

from breezy.i18n import load_plugin_translations
translation = load_plugin_translations("bzr-svn")
gettext = translation.gettext


from breezy import (
    config as _mod_bzr_config,  # Or we mask plugins.svn.config
    urlutils,
    )
from breezy.branch import (
    format_registry as branch_format_registry,
    network_format_registry as branch_network_format_registry,
    )
from breezy.commands import (
    plugin_cmds,
    )
from breezy.controldir import (
    ControlDirFormat,
    Prober,
    format_registry,
    network_format_registry,
    )
from breezy.errors import (
    DependencyNotPresent,
    InvalidRevisionId,
    InProcessTransport,
    InvalidHttpResponse,
    NotBranchError,
    NoSuchFile,
    UnsupportedFormatError,
    )
from breezy.repository import (
    format_registry as repository_format_registry,
    network_format_registry as repository_network_format_registry,
    )
from breezy.transport import (
    ConnectedTransport,
    register_lazy_transport,
    register_transport_proto,
    )



def check_subversion_version():
    """Check that Subversion is compatible.

    """
    # Installed ?
    from subvertpy import ra, __version__ as subvertpy_version
    ra_version = ra.version()
    if (ra_version[1] >= 5 and getattr(ra, 'SVN_REVISION', None) and
        27729 <= ra.SVN_REVISION < 31470):
        raise DependencyNotPresent("subvertpy",
                'bzr-svn: Installed Subversion has buggy svn.ra.get_log() '
                'implementation, please install newer.')

    from breezy.trace import mutter
    versions = ["Subversion %d.%d.%d (%s)" % ra_version]
    if getattr(ra, "api_version", None) is not None and ra.api_version() != ra_version:
        versions.append("Subversion API %d.%d.%d (%s)" % ra.api_version())
    versions.append("subvertpy %d.%d.%d" % subvertpy_version)
    mutter("bzr-svn: using " + ", ".join(versions))

    if subvertpy_version < subvertpy_minimum_version:
        raise DependencyNotPresent("subvertpy", "bzr-svn: at least subvertpy %d.%d.%d is required, %d.%d.%d is installed." % (subvertpy_minimum_version + subvertpy_version))


def get_client_string():
    """Return a string that can be send as part of the User Agent string."""
    return "bzr%s+bzr-svn%s" % (breezy.__version__, __version__)


def init_subvertpy():
    try:
        import subvertpy
    except ImportError as e:
        raise DependencyNotPresent("subvertpy", "bzr-svn: %s" % str(e))

    check_subversion_version()

    def get_ssh_vendor():
        import breezy.transport.ssh
        return breezy.transport.ssh._get_ssh_vendor()
    import subvertpy.ra_svn
    subvertpy.ra_svn.get_ssh_vendor = get_ssh_vendor

_versions_checked = False
def lazy_check_versions():
    """Check whether all dependencies have the right versions.

    :note: Only checks once, caches the result."""
    global _versions_checked
    if _versions_checked:
        return
    _versions_checked = True
    init_subvertpy()


class SvnProber(Prober):

    @classmethod
    def _check_versions(cls):
        lazy_check_versions()


class SvnWorkingTreeProber(SvnProber):

    @classmethod
    def _check_versions(cls):
        lazy_check_versions()

    def probe_transport(self, transport):
        from breezy.transport.local import LocalTransport

        if (not isinstance(transport, LocalTransport)
            or not transport.has(".svn")):
            raise NotBranchError(path=transport.base)

        self._check_versions()
        import subvertpy
        from subvertpy.wc import check_wc
        try:
            version = check_wc(transport.local_abspath('.').encode("utf-8"))
        except subvertpy.SubversionException as e:
            msg, num = e.args
            if num == subvertpy.ERR_WC_UPGRADE_REQUIRED:
                raise UnsupportedFormatError(msg)
            raise
        from .workingtree import SvnWorkingTreeDirFormat
        return SvnWorkingTreeDirFormat(version)

    @classmethod
    def known_formats(cls):
        try:
            cls._check_versions()
        except DependencyNotPresent:
            return set()
        else:
            from .workingtree import SvnWorkingTreeDirFormat
            return set([SvnWorkingTreeDirFormat()])


def dav_options(transport, url):
    # FIXME: Integrate this into HttpTransport.options().
    from breezy.transport.http import HttpTransport, Request
    if isinstance(transport, HttpTransport):
        req = Request('OPTIONS', url, accepted_errors=[200, 403, 404, 405])
        req.follow_redirections = True
        resp = transport._perform(req)
        if resp.code == 404:
            raise NoSuchFile(transport._path)
        if resp.code in (403, 405):
            raise InvalidHttpResponse(transport.base,
                "OPTIONS not supported or forbidden for remote URL")
        return resp.headers.getheaders('DAV')
    else:
        try:
            from breezy.transport.http._pycurl import PyCurlTransport
        except DependencyNotPresent:
            pass
        else:
            import pycurl
            from cStringIO import StringIO
            if isinstance(transport, PyCurlTransport):
                conn = transport._get_curl()
                try:
                    conn.setopt(pycurl.URL, url)
                    conn.setopt(pycurl.FOLLOWLOCATION, 0)
                    transport._set_curl_options(conn)
                    conn.setopt(pycurl.CUSTOMREQUEST, 'OPTIONS')
                    header = StringIO()
                    data = StringIO()
                    conn.setopt(pycurl.HEADERFUNCTION, header.write)
                    conn.setopt(pycurl.WRITEFUNCTION, data.write)
                    transport._curl_perform(conn, header)
                    code = conn.getinfo(pycurl.HTTP_CODE)
                    if code == 404:
                        raise NoSuchFile(transport._path)
                    if code in (403, 405):
                        raise InvalidHttpResponse(transport.base,
                            "OPTIONS not supported or forbidden for remote URL")
                    if code == 200:
                        headers = transport._parse_headers(header)
                        return headers.getheaders('DAV')
                    raise InvalidHttpResponse(transport.base,
                            "Invalid HTTP response: %d" % code)
                finally:
                    conn.unsetopt(pycurl.CUSTOMREQUEST)
    raise NotImplementedError


class SvnRemoteProber(SvnProber):

    _supported_schemes = ["http", "https", "file", "svn"]

    def probe_transport(self, transport):

        try:
            url = transport.external_url()
        except InProcessTransport:
            # bzr-svn not supported on MemoryTransport
            raise NotBranchError(path=transport.base)

        if url.startswith("readonly+"):
            url = url[len("readonly+"):]

        if url.startswith("file://"):
            # Cheaper way to figure out if there is a svn repo
            maybe = False
            subtransport = transport
            while subtransport:
                try:
                    if subtransport.has("format"):
                        maybe = True
                        break
                except UnicodeEncodeError:
                    pass
                prevsubtransport = subtransport
                subtransport = prevsubtransport.clone("..")
                if subtransport.base == prevsubtransport.base:
                    break
            if not maybe:
                raise NotBranchError(path=transport.base)

        scheme = url.split(":")[0]
        if (not scheme.startswith("svn+") and
            not scheme in self._supported_schemes):
            raise NotBranchError(path=transport.base)

        # If this is a HTTP transport, use the existing connection to check
        # that the remote end supports version control.
        if scheme in ("http", "https"):
            priv_transport = getattr(transport, "_decorated", transport)
            url = ConnectedTransport._unsplit_url(
                priv_transport._unqualified_scheme,
                None, None, priv_transport._host,
                priv_transport._port, priv_transport._path)
            try:
                dav_entries = dav_options(transport, url)
            except (InProcessTransport, NoSuchFile, urlutils.InvalidURL, InvalidHttpResponse):
                raise NotBranchError(path=transport.base)
            except NotImplementedError:
                pass # Custom http implementation?
            else:
                import itertools
                dav_entries = list(itertools.chain(
                    *[entry.split(",") for entry in dav_entries]))
                if not "version-control" in dav_entries:
                    raise NotBranchError(path=transport.base)

        self._check_versions()
        from .transport import get_svn_ra_transport
        from .errors import DavRequestFailed
        import subvertpy
        try:
            transport = get_svn_ra_transport(transport)
        except subvertpy.SubversionException as e:
            msg, num = e.args
            if num == subvertpy.ERR_RA_DAV_NOT_VCC:
                raise NotBranchError(path=transport.base)
            if num in (subvertpy.ERR_RA_ILLEGAL_URL,
                       subvertpy.ERR_RA_LOCAL_REPOS_OPEN_FAILED,
                       subvertpy.ERR_BAD_URL):
                from breezy.trace import mutter
                mutter("Unable to open %r with Subversion: %s", transport, msg)
                raise NotBranchError(path=transport.base)
            raise
        except (InProcessTransport, NoSuchFile, urlutils.InvalidURL, InvalidHttpResponse):
            raise NotBranchError(path=transport.base)
        except DavRequestFailed as e:
            if "501 Unsupported method" in e[1]:
                raise NotBranchError(path=transport.base)
            else:
                raise

        from .remote import SvnRemoteFormat
        return SvnRemoteFormat()

    @classmethod
    def known_formats(cls):
        from .remote import SvnRemoteFormat
        return set([SvnRemoteFormat()])


register_transport_proto('svn+ssh://',
    help="Access using the Subversion smart server tunneled over SSH.")
register_transport_proto('svn+http://')
register_transport_proto('svn+https://')
register_transport_proto('svn://',
    help="Access using the Subversion smart server.")
register_lazy_transport('svn://', __name__ + '.transport',
                        'SvnRaTransport')
register_lazy_transport('svn+', __name__ + '.transport',
                        'SvnRaTransport')

#BzrDirFormat.register_control_server_format(format.SvnRemoteFormat)
# Register as the first control server format, since the default smart
# server implementation tries to do a POST request against .bzr/smart and
# this causes some Subversion servers to reply with 401 Authentication required
# even though they are accessible without authentication.
ControlDirFormat.register_prober(SvnWorkingTreeProber)
ControlDirFormat._server_probers.insert(0, SvnRemoteProber)

network_format_registry.register_lazy("svn-wc",
    __name__ + '.workingtree', 'SvnWorkingTreeDirFormat')
network_format_registry.register_lazy("subversion",
    __name__ + '.remote', 'SvnRemoteFormat')
branch_format_registry.register_extra_lazy(
    __name__ + '.branch', 'SvnBranchFormat')
from breezy.workingtree import (
    format_registry as workingtree_format_registry,
    )
workingtree_format_registry.register_extra_lazy(
    __name__ + '.workingtree', 'SvnWorkingTreeFormat')
branch_network_format_registry.register_lazy("subversion",
    __name__ + '.branch', 'SvnBranchFormat')
repository_network_format_registry.register_lazy("subversion",
    __name__ + '.repository', 'SvnRepositoryFormat')
register_extra_lazy_repository_format = getattr(repository_format_registry,
    'register_extra_lazy')
register_extra_lazy_repository_format(__name__ + '.repository',
    'SvnRepositoryFormat')

format_registry.register_lazy("subversion", "breezy.plugins.svn.remote",
                         "SvnRemoteFormat",
                         "Subversion repository. ",
                         native=False)
format_registry.register_lazy("subversion-wc", "breezy.plugins.svn.workingtree",
                         "SvnWorkingTreeDirFormat",
                         "Subversion working copy. ",
                         native=False, hidden=True)

_mod_bzr_config.credential_store_registry.register_lazy(
    "subversion", "breezy.plugins.svn.auth", "SubversionCredentialStore",
    help=__doc__, fallback=True)


plugin_cmds.register_lazy('cmd_svn_import', [], __name__ + '.commands')
plugin_cmds.register_lazy('cmd_svn_branching_scheme', [],
                          __name__ + '.mapping3.commands')
plugin_cmds.register_lazy('cmd_svn_layout', [],
                          __name__ + '.commands')
plugin_cmds.register_lazy('cmd_svn_branches', [],
                          __name__ + '.commands')
plugin_cmds.register_lazy('cmd_fix_svn_ancestry', [],
                          __name__ + '.commands')


try:
    # breezy.filters.filter_stacks_registry as introduced in 2.6
    from breezy.filters import filter_stacks_registry
    filter_stacks_registry.register_lazy("svn-keywords",
            "breezy.plugins.svn.keywords", "create_svn_keywords_filter")
except ImportError:
    # fall back to lazy_register_filter_stack_map on older bzr
    from breezy.filters import lazy_register_filter_stack_map
    lazy_register_filter_stack_map("svn-keywords",
            "breezy.plugins.svn.keywords", "create_svn_keywords_filter")


def info_svn_repository(repository, stats, outf):
    if "svn-uuid" in stats:
        outf.write("Subversion UUID: %s\n" % stats["svn-uuid"])
    if "svn-last-revnum" in stats:
        outf.write("Subversion Last Revision: %d\n" % stats["svn-last-revnum"])


def extract_svn_foreign_revid(rev):
    try:
        foreign_revid = rev.foreign_revid
    except AttributeError:
        from breezy.plugins.svn.mapping import mapping_registry
        foreign_revid, mapping = \
            mapping_registry.parse_revision_id(rev.revision_id)
        return foreign_revid
    else:
        from breezy.plugins.svn.mapping import foreign_vcs_svn 
        if rev.mapping.vcs == foreign_vcs_svn:
            return foreign_revid
        else:
            raise InvalidRevisionId(rev.revision_id, None)


def update_stanza(rev, stanza):
    try:
        (uuid, branch_path, revno) = extract_svn_foreign_revid(rev)
    except InvalidRevisionId:
        pass
    else:
        stanza.add("svn-revno", str(revno))
        stanza.add("svn-uuid", uuid)

from breezy.hooks import install_lazy_named_hook
install_lazy_named_hook("breezy.version_info_formats.format_rio",
    "RioVersionInfoBuilder.hooks", "revision", update_stanza, "svn metadata")
install_lazy_named_hook("breezy.info", "hooks",
        'repository', info_svn_repository, "svn repository info")

try:
    from breezy.registry import register_lazy
except ImportError:
    from breezy.diff import format_registry as diff_format_registry
    diff_format_registry.register_lazy('svn', __name__ + '.send',
            'SvnDiffTree', 'Subversion diff format')

    from breezy.revisionspec import (
        revspec_registry,
        )
    revspec_registry.register_lazy("svn:", "breezy.plugins.svn.revspec",
        "RevisionSpec_svn")

    from breezy.send import format_registry as send_format_registry
    send_format_registry.register_lazy('svn', __name__ + '.send',
                                       'send_svn', 'Subversion diff format')
    from breezy.foreign import (
        foreign_vcs_registry,
        )
    foreign_vcs_registry.register_lazy("svn", "breezy.plugins.svn.mapping",
                                       "foreign_vcs_svn")
    from breezy.help_topics import topic_registry
    topic_registry.register_lazy('svn-layout',
                                 __name__ + '.layout',
                                 'help_layout', 'Subversion repository layouts')
else:
    register_lazy("breezy.diff", "format_registry", 'svn', __name__ + '.send',
            'SvnDiffTree', help='Subversion diff format')
    register_lazy("breezy.revisionspec", "revspec_registry", "svn:",
            "breezy.plugins.svn.revspec", "RevisionSpec_svn")
    register_lazy("breezy.send", "format_registry", 'svn',
            __name__ + '.send', 'send_svn', 'Subversion diff format')
    register_lazy("breezy.foreign", "foreign_vcs_registry", "svn",
            "breezy.plugins.svn.mapping", "foreign_vcs_svn")
    register_lazy("breezy.help_topics", "topic_registry", 'svn-layout',
            __name__ + '.layout', 'help_layout',
            'Subversion repository layouts')


_mod_bzr_config.option_registry.register_lazy('layout',
    __name__ + '.config', 'svn_layout_option')
_mod_bzr_config.option_registry.register_lazy('guessed-layout',
    __name__ + '.config', 'svn_guessed_layout_option')
_mod_bzr_config.option_registry.register_lazy('branches',
    __name__ + '.config', 'svn_branches_option')
_mod_bzr_config.option_registry.register_lazy('tags',
    __name__ + '.config', 'svn_tags_option')
_mod_bzr_config.option_registry.register_lazy('override-svn-revprops',
    __name__ + '.config', 'svn_override_revprops')
_mod_bzr_config.option_registry.register_lazy('log-strip-trailing-newline',
    __name__ + '.config', 'svn_log_strip_trailing_new_line')
_mod_bzr_config.option_registry.register_lazy('push_merged_revisions',
    __name__ + '.config', 'svn_push_merged_revisions')
_mod_bzr_config.option_registry.register_lazy('allow_metadata_in_file_properties',
    __name__ + '.config', 'svn_allow_metadata_in_fileprops')

def test_suite():
    """Returns the testsuite for bzr-svn."""
    from unittest import TestSuite
    from breezy.plugins.svn import tests
    suite = TestSuite()
    suite.addTest(tests.test_suite())
    return suite


if __name__ == '__main__':
    print ("This is a Bazaar plugin. Copy this directory to ~/.bazaar/plugins "
          "to use it.\n")
elif __name__ != 'breezy.plugins.svn':
    raise ImportError('The Subversion plugin must be installed as'
                      ' breezy.plugins.svn not %s' % __name__)