changeset 717:ae5968ffe6fe

svnwrap: fix handling of quotable URLs (fixes #197, refs #132) The way hgsubversion handles URLs that may or may not be quoted is somewhat fragile. As part of fixing issue 132 in 925ff8c5989c, the path component of URLs was always quoted. The URL has been attempted encoded since the initial check-in. The fix from 925ff8c5989c was incomplete; reverting it allows us to clone a URL with a '~' in it.[1] Encoding the URL as UTF-8 seldom works as expected, as the default string encoding is ASCII, causing Python to be unable to decode any URL containing an 8-bit character. The core problem here is that we don't know whether the URL specified by the user is quoted or not. Rather than trying to deal with this ourselves, we pass the problem on to Subversion. Then, we obtain the URL from the RA instance, where it is always quoted. (It's worth noting that the editor interface, on the other hand, always deals with unquoted paths...) Thus, the following invariants should apply to SubversionRepo attributes: - svn_url and root will always be quoted. - subdir will always be unquoted. Tests are added that verify that it won't affect the conversion whether a URL is specified in quoted or unquoted form. Furthermore, a test fixture for this is added *twice*, so that we can thoroughly test both quoted and unquoted URLs. I'm not adding a test dedicated to tildes in URLs; it doesn't seem necessary. [1] Such as <https://svn.kenai.com/svn/winsw~subversion>.
author Dan Villiom Podlaski Christiansen <danchr@gmail.com>
date Mon, 04 Oct 2010 21:00:36 -0500
parents 38ebf7714cdf
children db0eb6237420
files hgsubversion/svnwrap/subvertpy_wrapper.py hgsubversion/svnwrap/svn_swig_wrapper.py tests/fixtures/non_ascii_path_1.svndump tests/fixtures/non_ascii_path_2.svndump tests/test_fetch_command.py tests/test_urls.py tests/test_util.py
diffstat 7 files changed, 296 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/svnwrap/subvertpy_wrapper.py
+++ b/hgsubversion/svnwrap/subvertpy_wrapper.py
@@ -5,7 +5,6 @@ import os
 import shutil
 import sys
 import tempfile
-import urlparse
 import urllib
 import collections
 
@@ -176,13 +175,18 @@ class SubversionRepo(object):
 
         self.init_ra_and_client()
 
+        self.svn_url = self.remote.get_url()
         self.uuid = self.remote.get_uuid()
         self.root = self.remote.get_repos_root()
+        assert self.svn_url.startswith(self.root)
 
         # *will* have a leading '/', would not if we used get_repos_root2
-        self.subdir = url[len(self.root):]
+        self.subdir = self.svn_url[len(self.root):]
         if not self.subdir or self.subdir[-1] != '/':
             self.subdir += '/'
+        # the RA interface always yields quoted paths, but the editor interface
+        # expects unquoted paths
+        self.subdir = urllib.unquote(self.subdir)
         self.hasdiff3 = True
 
     def init_ra_and_client(self):
@@ -218,7 +222,7 @@ class SubversionRepo(object):
         if self.password:
             auth.set_parameter(subvertpy.AUTH_PARAM_DEFAULT_PASSWORD, self.password)
 
-        self.remote = ra.RemoteAccess(url=self.svn_url.encode('utf-8'),
+        self.remote = ra.RemoteAccess(url=self.svn_url,
                                       client_string_func=getclientstring,
                                       auth=auth)
 
--- a/hgsubversion/svnwrap/svn_swig_wrapper.py
+++ b/hgsubversion/svnwrap/svn_swig_wrapper.py
@@ -5,7 +5,6 @@ import os
 import shutil
 import sys
 import tempfile
-import urlparse
 import urllib
 import collections
 
@@ -172,11 +171,16 @@ class SubversionRepo(object):
 
         self.init_ra_and_client()
         self.uuid = ra.get_uuid(self.ra, self.pool)
-        self.root = urllib.unquote(ra.get_repos_root(self.ra, self.pool))
+        self.svn_url = ra.get_session_url(self.ra, self.pool)
+        self.root = ra.get_repos_root(self.ra, self.pool)
+        assert self.svn_url.startswith(self.root)
         # *will* have a leading '/', would not if we used get_repos_root2
-        self.subdir = url[len(self.root):]
+        self.subdir = self.svn_url[len(self.root):]
         if not self.subdir or self.subdir[-1] != '/':
             self.subdir += '/'
+        # the RA interface always yields quoted paths, but the editor interface
+        # expects unquoted paths
+        self.subdir = urllib.unquote(self.subdir)
         self.hasdiff3 = True
 
     def init_ra_and_client(self):
@@ -201,11 +205,7 @@ class SubversionRepo(object):
         callbacks.auth_baton = self.auth_baton
         self.callbacks = callbacks
         try:
-            url = self.svn_url.encode('utf-8')
-            scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
-            path=urllib.quote(path)
-            url = urlparse.urlunparse((scheme, netloc, path, params, query, fragment))
-            self.ra = ra.open2(url, callbacks,
+            self.ra = ra.open2(self.svn_url, callbacks,
                                svn_config, self.pool)
         except SubversionException, e:
             if e.apr_err == core.SVN_ERR_RA_SERF_SSL_CERT_UNTRUSTED:
new file mode 100644
--- /dev/null
+++ b/tests/fixtures/non_ascii_path_1.svndump
@@ -0,0 +1,112 @@
+SVN-fs-dump-format-version: 2
+
+UUID: a7b92ce5-70a8-4e75-99da-c3ca360ae490
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2010-08-16T13:08:38.494252Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 20
+Create project bøb.
+K 10
+svn:author
+V 6
+danchr
+K 8
+svn:date
+V 27
+2010-08-16T13:10:12.395716Z
+PROPS-END
+
+Node-path: bøb
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: bøb/branches
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: bøb/tags
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: bøb/trunk
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: bøb/trunk/A
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 1
+Text-content-md5: 68b329da9893e34099c7d8ad5cb9c940
+Text-content-sha1: adc83b19e793491b1c6ea0fd8b46cd9f32e592fc
+Content-length: 11
+
+PROPS-END
+
+
+
+Revision-number: 2
+Prop-content-length: 112
+Content-length: 112
+
+K 7
+svn:log
+V 11
+Add a file.
+K 10
+svn:author
+V 6
+danchr
+K 8
+svn:date
+V 27
+2010-08-16T14:04:00.624391Z
+PROPS-END
+
+Node-path: bøb/trunk/B
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 1
+Text-content-md5: 68b329da9893e34099c7d8ad5cb9c940
+Text-content-sha1: adc83b19e793491b1c6ea0fd8b46cd9f32e592fc
+Content-length: 11
+
+PROPS-END
+
+
+
new file mode 100644
--- /dev/null
+++ b/tests/fixtures/non_ascii_path_2.svndump
@@ -0,0 +1,112 @@
+SVN-fs-dump-format-version: 2
+
+UUID: a7b92ce5-70a8-4e75-99da-c3ca360ae490
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2010-08-16T13:08:38.494252Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 20
+Create project bøb.
+K 10
+svn:author
+V 6
+danchr
+K 8
+svn:date
+V 27
+2010-08-16T13:10:12.395716Z
+PROPS-END
+
+Node-path: bøb
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: bøb/branches
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: bøb/tags
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: bøb/trunk
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: bøb/trunk/A
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 1
+Text-content-md5: 68b329da9893e34099c7d8ad5cb9c940
+Text-content-sha1: adc83b19e793491b1c6ea0fd8b46cd9f32e592fc
+Content-length: 11
+
+PROPS-END
+
+
+
+Revision-number: 2
+Prop-content-length: 112
+Content-length: 112
+
+K 7
+svn:log
+V 11
+Add a file.
+K 10
+svn:author
+V 6
+danchr
+K 8
+svn:date
+V 27
+2010-08-16T14:04:00.624391Z
+PROPS-END
+
+Node-path: bøb/trunk/B
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 1
+Text-content-md5: 68b329da9893e34099c7d8ad5cb9c940
+Text-content-sha1: adc83b19e793491b1c6ea0fd8b46cd9f32e592fc
+Content-length: 11
+
+PROPS-END
+
+
+
--- a/tests/test_fetch_command.py
+++ b/tests/test_fetch_command.py
@@ -1,7 +1,10 @@
 import test_util
 
+import os
 import unittest
+import urllib
 
+from mercurial import commands
 from mercurial import hg
 from mercurial import node
 from mercurial import ui
@@ -148,6 +151,42 @@ class TestBasicRepoLayout(test_util.Test
     def test_fetch_when_trunk_has_no_files_stupid(self):
         self.test_fetch_when_trunk_has_no_files(stupid=True)
 
+    def test_path_quoting(self, stupid=False):
+        test_util.load_svndump_fixture(self.repo_path,
+                                       'non_ascii_path_1.svndump')
+        subdir = '/b\xC3\xB8b'
+        quoted_subdir = urllib.quote(subdir)
+
+        repo_url = test_util.fileurl(self.repo_path)
+        wc_path = self.wc_path
+        wc2_path = wc_path + '-2'
+
+        ui = self.ui(stupid=stupid)
+
+        commands.clone(ui, repo_url + subdir, wc_path)
+        commands.clone(ui, repo_url + quoted_subdir, wc2_path)
+        repo  = hg.repository(ui, wc_path)
+        repo2 = hg.repository(ui, wc2_path)
+
+        self.assertEqual(repo['tip'].extra()['convert_revision'],
+                         repo2['tip'].extra()['convert_revision'])
+        self.assertEqual(len(repo), len(repo2))
+
+        for r in repo:
+            self.assertEqual(repo[r].hex(), repo2[r].hex())
+
+    def test_path_quoting_stupid(self):
+        self.test_path_quoting(True)
+
+    def test_identical_fixtures(self):
+        '''ensure that the non_ascii_path_N fixtures are identical'''
+        fixturepaths = [
+            os.path.join(test_util.FIXTURES, 'non_ascii_path_1.svndump'),
+            os.path.join(test_util.FIXTURES, 'non_ascii_path_2.svndump'),
+        ]
+        self.assertMultiLineEqual(open(fixturepaths[0]).read(),
+                                  open(fixturepaths[1]).read())
+
 class TestStupidPull(test_util.TestBase):
     def test_stupid(self):
         repo = test_util.load_fixture_and_fetch('two_heads.svndump',
--- a/tests/test_urls.py
+++ b/tests/test_urls.py
@@ -1,5 +1,8 @@
 import test_util
+
 import unittest
+import urllib
+
 from hgsubversion.svnwrap import parse_url
 from hgsubversion import svnrepo
 
@@ -58,6 +61,19 @@ class TestSubversionUrls(test_util.TestB
         repo = svnrepo.svnremoterepo(ui, 'svn+https://joe@foo/bar')
         self.assertEqual(('https://foo/bar', 'bob', None), repo.svnauth)
 
+    def test_quoting(self):
+        ui = self.ui()
+        test_util.load_svndump_fixture(self.repo_path,
+                                       'non_ascii_path_1.svndump')
+
+        repo_url = test_util.fileurl(self.repo_path)
+        subdir = '/b\xC3\xB8b'
+        quoted_subdir = urllib.quote(subdir)
+
+        repo1 = svnrepo.svnremoterepo(ui, repo_url + subdir)
+        repo2 = svnrepo.svnremoterepo(ui, repo_url + quoted_subdir)
+        self.assertEqual(repo1.svnurl, repo2.svnurl)
+
 def suite():
     all = [unittest.TestLoader().loadTestsFromTestCase(TestSubversionUrls)]
     return unittest.TestSuite(all)
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -91,6 +91,8 @@ subdir = {'truncatedhistory.svndump': '/
           'empty_dir_in_trunk_not_repo_root.svndump': '/project',
           'project_root_not_repo_root.svndump': '/dummyproj',
           'project_name_with_space.svndump': '/project name',
+          'non_ascii_path_1.svndump': '/b\xC3\xB8b',
+          'non_ascii_path_2.svndump': '/b%C3%B8b',
           }
 
 FIXTURES = os.path.join(os.path.abspath(os.path.dirname(__file__)),