Commit e54cf1a3eed8f2375b9e5d4dac9bf4ded57bdd01

Patrick Steinhardt 2017-05-24T11:07:20

path: expose `git_path_is_absolute` This function has previously been implemented in Windows-specific path handling code as `path__is_absolute`. As we will need this functionality in other parts, extract the logic into "path.h" alongside with a non-Windows implementation.

diff --git a/src/path.h b/src/path.h
index 360372c..d674b1b 100644
--- a/src/path.h
+++ b/src/path.h
@@ -105,6 +105,9 @@ GIT_INLINE(int) git_path_is_dot_or_dotdotW(const wchar_t *name)
 				(name[1] == L'.' && name[2] == L'\0')));
 }
 
+#define git_path_is_absolute(p) \
+	(git__isalpha((p)[0]) && (p)[1] == ':' && ((p)[2] == '\\' || (p)[2] == '/'))
+
 /**
  * Convert backslashes in path to forward slashes.
  */
@@ -119,6 +122,10 @@ GIT_INLINE(void) git_path_mkposix(char *path)
 }
 #else
 #	define git_path_mkposix(p) /* blank */
+
+#define git_path_is_absolute(p) \
+	((p)[0] == '/')
+
 #endif
 
 /**
diff --git a/src/win32/path_w32.c b/src/win32/path_w32.c
index f8416b8..8cea48b 100644
--- a/src/win32/path_w32.c
+++ b/src/win32/path_w32.c
@@ -20,9 +20,6 @@
 
 #define path__is_dirsep(p) ((p) == '/' || (p) == '\\')
 
-#define path__is_absolute(p) \
-	(git__isalpha((p)[0]) && (p)[1] == ':' && ((p)[2] == '\\' || (p)[2] == '/'))
-
 #define path__is_nt_namespace(p) \
 	(((p)[0] == '\\' && (p)[1] == '\\' && (p)[2] == '?' && (p)[3] == '\\') || \
 	 ((p)[0] == '/' && (p)[1] == '/' && (p)[2] == '?' && (p)[3] == '/'))
@@ -73,9 +70,9 @@ static wchar_t *path__skip_prefix(wchar_t *path)
 
 		if (wcsncmp(path, L"UNC\\", 4) == 0)
 			path = path__skip_server(path + 4);
-		else if (path__is_absolute(path))
+		else if (git_path_is_absolute(path))
 			path += PATH__ABSOLUTE_LEN;
-	} else if (path__is_absolute(path)) {
+	} else if (git_path_is_absolute(path)) {
 		path += PATH__ABSOLUTE_LEN;
 	} else if (path__is_unc(path)) {
 		path = path__skip_server(path + 2);
@@ -196,7 +193,7 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
 	dest += PATH__NT_NAMESPACE_LEN;
 
 	/* See if this is an absolute path (beginning with a drive letter) */
-	if (path__is_absolute(src)) {
+	if (git_path_is_absolute(src)) {
 		if (git__utf8_to_16(dest, MAX_PATH, src) < 0)
 			goto on_error;
 	}
@@ -220,7 +217,7 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
 		if (path__cwd(dest, MAX_PATH) < 0)
 			goto on_error;
 
-		if (!path__is_absolute(dest)) {
+		if (!git_path_is_absolute(dest)) {
 			errno = ENOENT;
 			goto on_error;
 		}