[PATCH 1 of 1] handle file URIs correctly, according to RFC 2396 (issue1153)

Sune Foldager cryo at cyanite.org
Tue Dec 1 06:54:51 CST 2009


# HG changeset patch
# User Sune Foldager <cryo at cyanite.org>
# Date 1258593504 -3600
# Node ID 735b356aaeaa7e66375d52a59963e7f659e76649
# Parent  2e67734e1453e44de0816c3f562005e89c09c21f
handle file URIs correctly, according to RFC 2396 (issue1153)

The new code aims to implement the RFC correctly for file URIs.
Previously they were handled incorrectly in several ways, which
could cause problem on Windows in particular.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1197,7 +1197,19 @@
     if path.startswith(sc):
         path = path[len(sc):]
         if path.startswith('//'):
-            path = path[2:]
+            if scheme == 'file':
+                i = path.find('/', 2)
+                if i == -1:
+                    return ''
+                # On Windows, absolute paths are rooted at the current drive
+                # root. On POSIX they are rooted at the file system root.
+                if os.name == 'nt':
+                    droot = os.path.splitdrive(os.getcwd())[0] + '/'
+                    path = os.path.join(droot, path[i+1:])
+                else:
+                    path = path[i:]
+            else:
+                path = path[2:]
     return path
 
 def uirepr(s):
diff --git a/tests/test-clone b/tests/test-clone
--- a/tests/test-clone
+++ b/tests/test-clone
@@ -44,10 +44,10 @@
 hg cat a
 
 echo
-echo % "check that we drop the file:// from the path before"
+echo % "check that we drop the file: from the path before"
 echo % "writing the .hgrc"
 cd ../..
-hg clone file://a e
+hg clone file:a e
 grep 'file:' e/.hg/hgrc
 
 echo
diff --git a/tests/test-clone.out b/tests/test-clone.out
--- a/tests/test-clone.out
+++ b/tests/test-clone.out
@@ -29,7 +29,7 @@
 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
 a
 
-% check that we drop the file:// from the path before
+% check that we drop the file: from the path before
 % writing the .hgrc
 updating to branch default
 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
diff --git a/tests/test-pull b/tests/test-pull
--- a/tests/test-pull
+++ b/tests/test-pull
@@ -24,3 +24,8 @@
 hg init empty
 cd empty
 hg pull -u ../test
+
+echo % test file: uri handling
+hg pull -q file://../test-doesnt-exist
+hg pull -q file:../test
+hg pull -q file://foobar`pwd`/../test
diff --git a/tests/test-pull.out b/tests/test-pull.out
--- a/tests/test-pull.out
+++ b/tests/test-pull.out
@@ -30,3 +30,5 @@
 adding file changes
 added 1 changesets with 1 changes to 1 files
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+% test file: uri handling
+abort: repository /test-doesnt-exist not found!


More information about the Mercurial-devel mailing list