]> git.ipfire.org Git - thirdparty/git.git/commitdiff
real_path: canonicalize directory separators in root parts
authorJohannes Sixt <j6t@kdbg.org>
Wed, 21 Dec 2016 21:51:35 +0000 (22:51 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 22 Dec 2016 18:28:15 +0000 (10:28 -0800)
When an absolute path is resolved, resolution begins at the first path
component after the root part. The root part is just copied verbatim,
because it must not be inspected for symbolic links. For POSIX paths,
this is just the initial slash, but on Windows, the root part has the
forms c:\ or \\server\share. We do want to canonicalize the back-slashes
in the root part because these parts are compared to the result of
getcwd(), which does return a fully canonicalized path.

Factor out a helper that splits off the root part, and have it
canonicalize the copied part.

This change was prompted because t1504-ceiling-dirs.sh caught a breakage
in GIT_CEILING_DIRECTORIES handling on Windows.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Acked-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
abspath.c

index 79ee3108678c97e1dade83e4ff4943edf3d3da9e..1d56f5ed9f95002d018458c907625fff27472231 100644 (file)
--- a/abspath.c
+++ b/abspath.c
@@ -48,6 +48,19 @@ static void get_next_component(struct strbuf *next, struct strbuf *remaining)
        strbuf_remove(remaining, 0, end - remaining->buf);
 }
 
+/* copies root part from remaining to resolved, canonicalizing it on the way */
+static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)
+{
+       int offset = offset_1st_component(remaining->buf);
+
+       strbuf_reset(resolved);
+       strbuf_add(resolved, remaining->buf, offset);
+#ifdef GIT_WINDOWS_NATIVE
+       convert_slashes(resolved->buf);
+#endif
+       strbuf_remove(remaining, 0, offset);
+}
+
 /* We allow "recursive" symbolic links. Only within reason, though. */
 #define MAXSYMLINKS 5
 
@@ -80,14 +93,10 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,
                        goto error_out;
        }
 
-       strbuf_reset(resolved);
+       strbuf_addstr(&remaining, path);
+       get_root_part(resolved, &remaining);
 
-       if (is_absolute_path(path)) {
-               /* absolute path; start with only root as being resolved */
-               int offset = offset_1st_component(path);
-               strbuf_add(resolved, path, offset);
-               strbuf_addstr(&remaining, path + offset);
-       } else {
+       if (!resolved->len) {
                /* relative path; can use CWD as the initial resolved path */
                if (strbuf_getcwd(resolved)) {
                        if (die_on_error)
@@ -95,7 +104,6 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,
                        else
                                goto error_out;
                }
-               strbuf_addstr(&remaining, path);
        }
 
        /* Iterate over the remaining path components */
@@ -150,10 +158,7 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,
 
                        if (is_absolute_path(symlink.buf)) {
                                /* absolute symlink; set resolved to root */
-                               int offset = offset_1st_component(symlink.buf);
-                               strbuf_reset(resolved);
-                               strbuf_add(resolved, symlink.buf, offset);
-                               strbuf_remove(&symlink, 0, offset);
+                               get_root_part(resolved, &symlink);
                        } else {
                                /*
                                 * relative symlink