Commit 315a43b2f16fab679126f519a5dce1c9e1e335af

Edward Thomson 2021-11-01T17:37:06

path: introduce `git_path_str_is_valid` Add a `git_str` based validity check; the existing `git_path_is_valid` defers to it.

diff --git a/src/fs_path.c b/src/fs_path.c
index de3b039..9079c30 100644
--- a/src/fs_path.c
+++ b/src/fs_path.c
@@ -1647,7 +1647,7 @@ GIT_INLINE(bool) validate_length(
 }
 #endif
 
-bool git_fs_path_is_valid_str_ext(
+bool git_fs_path_str_is_valid_ext(
 	const git_str *path,
 	unsigned int flags,
 	bool (*validate_char_cb)(char ch, void *payload),
diff --git a/src/fs_path.h b/src/fs_path.h
index 40b4342..947c4ae 100644
--- a/src/fs_path.h
+++ b/src/fs_path.h
@@ -628,7 +628,7 @@ extern int git_fs_path_from_url_or_path(git_str *local_path_out, const char *url
  * Validate a filesystem path; with custom callbacks per-character and
  * per-path component.
  */
-extern bool git_fs_path_is_valid_str_ext(
+extern bool git_fs_path_str_is_valid_ext(
 	const git_str *path,
 	unsigned int flags,
 	bool (*validate_char_cb)(char ch, void *payload),
@@ -645,7 +645,7 @@ GIT_INLINE(bool) git_fs_path_is_valid_ext(
 	void *payload)
 {
 	const git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX);
-	return git_fs_path_is_valid_str_ext(
+	return git_fs_path_str_is_valid_ext(
 		&str,
 		flags,
 		validate_char_cb,
@@ -666,15 +666,15 @@ GIT_INLINE(bool) git_fs_path_is_valid(
 	unsigned int flags)
 {
 	const git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX);
-	return git_fs_path_is_valid_str_ext(&str, flags, NULL, NULL, NULL, NULL);
+	return git_fs_path_str_is_valid_ext(&str, flags, NULL, NULL, NULL, NULL);
 }
 
 /** Validate a filesystem path in a `git_str`. */
-GIT_INLINE(bool) git_fs_path_is_valid_str(
+GIT_INLINE(bool) git_fs_path_str_is_valid(
 	const git_str *path,
 	unsigned int flags)
 {
-	return git_fs_path_is_valid_str_ext(path, flags, NULL, NULL, NULL, NULL);
+	return git_fs_path_str_is_valid_ext(path, flags, NULL, NULL, NULL, NULL);
 }
 
 /**
diff --git a/src/path.c b/src/path.c
index a6b396f..933444d 100644
--- a/src/path.c
+++ b/src/path.c
@@ -285,9 +285,9 @@ GIT_INLINE(unsigned int) dotgit_flags(
 	return flags;
 }
 
-bool git_path_is_valid(
+bool git_path_str_is_valid(
 	git_repository *repo,
-	const char *path,
+	const git_str *path,
 	uint16_t file_mode,
 	unsigned int flags)
 {
@@ -301,7 +301,7 @@ bool git_path_is_valid(
 	data.file_mode = file_mode;
 	data.flags = flags;
 
-	return git_fs_path_is_valid_ext(path, flags, NULL, validate_repo_component, NULL, &data);
+	return git_fs_path_str_is_valid_ext(path, flags, NULL, validate_repo_component, NULL, &data);
 }
 
 static const struct {
diff --git a/src/path.h b/src/path.h
index f874a16..f2ac848 100644
--- a/src/path.h
+++ b/src/path.h
@@ -25,10 +25,20 @@
 #define GIT_PATH_REJECT_INDEX_DEFAULTS \
 	GIT_FS_PATH_REJECT_TRAVERSAL | GIT_PATH_REJECT_DOT_GIT
 
-extern bool git_path_is_valid(
+extern bool git_path_str_is_valid(
 	git_repository *repo,
-	const char *path,
+	const git_str *path,
 	uint16_t file_mode,
 	unsigned int flags);
 
+GIT_INLINE(bool) git_path_is_valid(
+	git_repository *repo,
+	const char *path,
+	uint16_t file_mode,
+	unsigned int flags)
+{
+	git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX);
+	return git_path_str_is_valid(repo, &str, file_mode, flags);
+}
+
 #endif
diff --git a/tests/path/core.c b/tests/path/core.c
index ccb328b..eb6e5b8 100644
--- a/tests/path/core.c
+++ b/tests/path/core.c
@@ -71,25 +71,25 @@ void test_path_core__isvalid_standard_str(void)
 	unsigned int flags = GIT_FS_PATH_REJECT_EMPTY_COMPONENT;
 
 	str.size = 0;
-	cl_assert_equal_b(false, git_fs_path_is_valid_str(&str, flags));
+	cl_assert_equal_b(false, git_fs_path_str_is_valid(&str, flags));
 
 	str.size = 3;
-	cl_assert_equal_b(true, git_fs_path_is_valid_str(&str, flags));
+	cl_assert_equal_b(true, git_fs_path_str_is_valid(&str, flags));
 
 	str.size = 4;
-	cl_assert_equal_b(false, git_fs_path_is_valid_str(&str, flags));
+	cl_assert_equal_b(false, git_fs_path_str_is_valid(&str, flags));
 
 	str.size = 5;
-	cl_assert_equal_b(true, git_fs_path_is_valid_str(&str, flags));
+	cl_assert_equal_b(true, git_fs_path_str_is_valid(&str, flags));
 
 	str.size = 7;
-	cl_assert_equal_b(true, git_fs_path_is_valid_str(&str, flags));
+	cl_assert_equal_b(true, git_fs_path_str_is_valid(&str, flags));
 
 	str.size = 8;
-	cl_assert_equal_b(false, git_fs_path_is_valid_str(&str, flags));
+	cl_assert_equal_b(false, git_fs_path_str_is_valid(&str, flags));
 
 	str.size = strlen(str.ptr);
-	cl_assert_equal_b(false, git_fs_path_is_valid_str(&str, flags));
+	cl_assert_equal_b(false, git_fs_path_str_is_valid(&str, flags));
 }
 
 void test_path_core__isvalid_empty_dir_component(void)