Patchwork posix: fix split() for the case where the path is at the root of the filesystem

login
register
mail settings
Submitter Remy Blank
Date Jan. 6, 2013, 1:37 a.m.
Message ID <a5751f51c9670d2fb299.1357436223@pat.athome>
Download mbox | patch
Permalink /patch/401/
State Superseded
Headers show

Comments

Remy Blank - Jan. 6, 2013, 1:37 a.m.
# HG changeset patch
# User Remy Blank <remy.blank at pobox.com>
# Date 1357401181 -3600
# Node ID a5751f51c9670d2fb29973e2ab469a71e78eace4
# Parent  83aa4359c49f67bcb98fb9c7d885ed4ac7443239
posix: fix split() for the case where the path is at the root of the filesystem

posixpath.split() strips '/' from the dirname *unless it is the root*. This
patch reproduces this behavior in posix.split(). The old behavior causes a
crash when creating a file at the root of the repo with localrepo.wfile()
when the repo is at the root of the filesystem.
Remy Blank - Jan. 6, 2013, 1:47 a.m.
Ah, sorry, there's a trailing whitespace in here. Updated patch follows.

-- Remy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20130106/2d3ca71d/attachment.pgp>

Patch

diff --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -21,14 +21,32 @@  umask = os.umask(0)
 os.umask(umask)
 
 def split(p):
-    '''Same as os.path.split, but faster'''
+    '''Same as os.path.split, but faster
+    
+    >>> split('/absolute/path/to/file')
+    ('/absolute/path/to', 'file')
+    >>> split('relative/path/to/file')
+    ('relative/path/to', 'file')
+    >>> split('file_alone')
+    ('', 'file_alone')
+    >>> split('path/to/directory/')
+    ('path/to/directory', '')
+    >>> split('/multiple/path//separators')
+    ('/multiple/path', 'separators')
+    >>> split('/file_at_root')
+    ('/', 'file_at_root')
+    >>> split('///multiple_leading_separators_at_root')
+    ('///', 'multiple_leading_separators_at_root')
+    >>> split('')
+    ('', '')
+    '''
     ht = p.rsplit('/', 1)
     if len(ht) == 1:
         return '', p
     nh = ht[0].rstrip('/')
     if nh:
         return nh, ht[1]
-    return ht
+    return ht[0] + '/', ht[1]
 
 def openhardlinks():
     '''return true if it is safe to hold open file handles to hardlinks'''
diff --git a/tests/test-doctest.py b/tests/test-doctest.py
--- a/tests/test-doctest.py
+++ b/tests/test-doctest.py
@@ -6,6 +6,7 @@  import doctest
 
 import mercurial.util
 doctest.testmod(mercurial.util)
+doctest.testmod(mercurial.util.platform)
 
 import mercurial.changelog
 doctest.testmod(mercurial.changelog)