real_path(): properly handle nonexistent top-level paths
authorMichael Haggerty <mhagger@alum.mit.edu>
Thu, 6 Sep 2012 22:41:03 +0000 (00:41 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 6 Sep 2012 23:19:58 +0000 (16:19 -0700)
The change has two points:

1. Do not strip off a leading slash, because that erroneously turns an
   absolute path into a relative path.

2. Do not remove slashes from groups of multiple slashes; instead let
   chdir() handle them.  It could be, for example, that it wants to
   leave leading double-slashes alone.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
abspath.c
t/t0060-path-utils.sh

index 3e8325c7368e1cb2e7f38d7a90aafdf4b9edde0c..05f2d7934878a832f5a2efdf4222c16223050853 100644 (file)
--- a/abspath.c
+++ b/abspath.c
@@ -45,8 +45,8 @@ const char *real_path(const char *path)
                if (!is_directory(buf)) {
                        char *last_slash = find_last_dir_sep(buf);
                        if (last_slash) {
-                               *last_slash = '\0';
                                last_elem = xstrdup(last_slash + 1);
+                               last_slash[1] = '\0';
                        } else {
                                last_elem = xstrdup(buf);
                                *buf = '\0';
index 3121691c6f34f03b9b17e9e0717283f60b5e2255..30361f94a43b875605bd1c51d99dc3dfbc674003 100755 (executable)
@@ -148,7 +148,7 @@ test_expect_success 'real path rejects the empty string' '
        test_must_fail test-path-utils real_path ""
 '
 
-test_expect_failure POSIX 'real path works on absolute paths' '
+test_expect_success POSIX 'real path works on absolute paths' '
        nopath="hopefully-absent-path" &&
        test "/" = "$(test-path-utils real_path "/")" &&
        test "/$nopath" = "$(test-path-utils real_path "/$nopath")" &&