Skip to content

Commit

Permalink
real_path(): properly handle nonexistent top-level paths
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mhagger authored and gitster committed Sep 6, 2012
1 parent 7bcf48d commit f4c21e8
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion abspath.c
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion t/t0060-path-utils.sh
Expand Up @@ -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")" &&
Expand Down

0 comments on commit f4c21e8

Please sign in to comment.