]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Refactor skipping DOS drive prefixes
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Tue, 12 Jan 2016 07:57:22 +0000 (08:57 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 Jan 2016 18:39:40 +0000 (10:39 -0800)
Junio noticed that there is an implicit assumption in pretty much
all the code calling has_dos_drive_prefix(): it forces all of its
callsites to hardcode the knowledge that the DOS drive prefix is
always two bytes long.

While this assumption is pretty safe, we can still make the code
more readable and less error-prone by introducing a function that
skips the DOS drive prefix safely.

While at it, we change the has_dos_drive_prefix() return value: it
now returns the number of bytes to be skipped if there is a DOS
drive prefix.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
compat/basename.c
compat/mingw.c
compat/mingw.h
git-compat-util.h
path.c

index d8f8a3c6dc039ff8891d04857be737b29259d7c6..9f00421a26c306dbe5e40895594869f21c8405f7 100644 (file)
@@ -4,9 +4,7 @@
 char *gitbasename (char *path)
 {
        const char *base;
-       /* Skip over the disk name in MSDOS pathnames. */
-       if (has_dos_drive_prefix(path))
-               path += 2;
+       skip_dos_drive_prefix(&path);
        for (base = path; *path; path++) {
                if (is_dir_sep(*path))
                        base = path + 1;
index f74da235f598d8b0346fac8b48c4155f55ae5b81..10a51c058b512e4936d4d3eceaf7b9e88e2bb933 100644 (file)
@@ -1917,26 +1917,22 @@ pid_t waitpid(pid_t pid, int *status, int options)
 
 int mingw_offset_1st_component(const char *path)
 {
-       int offset = 0;
-       if (has_dos_drive_prefix(path))
-               offset = 2;
+       char *pos = (char *)path;
 
        /* unc paths */
-       else if (is_dir_sep(path[0]) && is_dir_sep(path[1])) {
-
+       if (!skip_dos_drive_prefix(&pos) &&
+                       is_dir_sep(pos[0]) && is_dir_sep(pos[1])) {
                /* skip server name */
-               char *pos = strpbrk(path + 2, "\\/");
+               pos = strpbrk(pos + 2, "\\/");
                if (!pos)
                        return 0; /* Error: malformed unc path */
 
                do {
                        pos++;
                } while (*pos && !is_dir_sep(*pos));
-
-               offset = pos - path;
        }
 
-       return offset + is_dir_sep(path[offset]);
+       return pos + is_dir_sep(*pos) - path;
 }
 
 int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
index 738865c6c068ed7d8849aff5a9b533dfb1ef8bab..9b5db4ecc1e747d0c60f37536b3941ac1810b55e 100644 (file)
@@ -358,7 +358,15 @@ HANDLE winansi_get_osfhandle(int fd);
  * git specific compatibility
  */
 
-#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
+#define has_dos_drive_prefix(path) \
+       (isalpha(*(path)) && (path)[1] == ':' ? 2 : 0)
+static inline int mingw_skip_dos_drive_prefix(char **path)
+{
+       int ret = has_dos_drive_prefix(*path);
+       *path += ret;
+       return ret;
+}
+#define skip_dos_drive_prefix mingw_skip_dos_drive_prefix
 #define is_dir_sep(c) ((c) == '/' || (c) == '\\')
 static inline char *mingw_find_last_dir_sep(const char *path)
 {
index 0feeae298340afbe22276ce595de4a6cba397926..38397d7afb76a6842453876c4eb00d7c11bcde34 100644 (file)
@@ -335,6 +335,14 @@ static inline int git_has_dos_drive_prefix(const char *path)
 #define has_dos_drive_prefix git_has_dos_drive_prefix
 #endif
 
+#ifndef skip_dos_drive_prefix
+static inline int git_skip_dos_drive_prefix(char **path)
+{
+       return 0;
+}
+#define skip_dos_drive_prefix git_skip_dos_drive_prefix
+#endif
+
 #ifndef is_dir_sep
 static inline int git_is_dir_sep(int c)
 {
diff --git a/path.c b/path.c
index 38f2ebd6bfad2b1557b06ce34a0154603533dd65..747d6da2c87edff4a1d1f2d20a4111d9f3a4e9d8 100644 (file)
--- a/path.c
+++ b/path.c
@@ -544,13 +544,10 @@ const char *relative_path(const char *in, const char *prefix,
        else if (!prefix_len)
                return in;
 
-       if (have_same_root(in, prefix)) {
+       if (have_same_root(in, prefix))
                /* bypass dos_drive, for "c:" is identical to "C:" */
-               if (has_dos_drive_prefix(in)) {
-                       i = 2;
-                       j = 2;
-               }
-       } else {
+               i = j = has_dos_drive_prefix(in);
+       else {
                return in;
        }
 
@@ -703,11 +700,10 @@ const char *remove_leading_path(const char *in, const char *prefix)
 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
 {
        char *dst0;
+       int i;
 
-       if (has_dos_drive_prefix(src)) {
+       for (i = has_dos_drive_prefix(src); i > 0; i--)
                *dst++ = *src++;
-               *dst++ = *src++;
-       }
        dst0 = dst;
 
        if (is_dir_sep(*src)) {