Commit 26b3cec0b3dc4fbc135c3cba5355fbcfce00dac3

Carlos Martín Nieto 2018-05-16T15:42:08

path: add a function to detect an .gitmodules file Given a path component it knows what to pass to the filesystem-specific functions so we're protected even from trees which try to use the 8.3 naming rules to get around us matching on the filename exactly. The logic and test strings come from the equivalent git change.

diff --git a/src/path.c b/src/path.c
index 8c54874..e1ee586 100644
--- a/src/path.c
+++ b/src/path.c
@@ -1841,3 +1841,16 @@ int git_path_normalize_slashes(git_buf *out, const char *path)
 
 	return 0;
 }
+
+static int verify_dotgit_generic(const char *name, const char *dotgit_name, const char *shortname_pfix)
+{
+	if (!verify_dotgit_ntfs_generic(name, dotgit_name, shortname_pfix))
+		return false;
+
+	return verify_dotgit_hfs_generic(name, strlen(name), dotgit_name, strlen(dotgit_name));
+}
+
+int git_path_is_dotgit_modules(const char *name)
+{
+	return !verify_dotgit_generic(name, "gitmodules", "gi7eba");
+}
diff --git a/tests/path/dotgit.c b/tests/path/dotgit.c
new file mode 100644
index 0000000..41174b1
--- /dev/null
+++ b/tests/path/dotgit.c
@@ -0,0 +1,110 @@
+#include "clar_libgit2.h"
+#include "path.h"
+
+#include "git2/sys/path.h"
+
+static char *gitmodules_altnames[] = {
+	".gitmodules",
+
+	".git\u200cmodules",
+
+	".Gitmodules",
+	".gitmoduleS",
+
+	".gitmodules ",
+	".gitmodules.",
+	".gitmodules  ",
+	".gitmodules. ",
+	".gitmodules .",
+	".gitmodules..",
+	".gitmodules   ",
+	".gitmodules.  ",
+	".gitmodules . ",
+	".gitmodules  .",
+
+	".Gitmodules ",
+	".Gitmodules.",
+	".Gitmodules  ",
+	".Gitmodules. ",
+	".Gitmodules .",
+	".Gitmodules..",
+	".Gitmodules   ",
+	".Gitmodules.  ",
+	".Gitmodules . ",
+	".Gitmodules  .",
+
+	"GITMOD~1",
+	"gitmod~1",
+	"GITMOD~2",
+	"gitmod~3",
+	"GITMOD~4",
+
+	"GITMOD~1 ",
+	"gitmod~2.",
+	"GITMOD~3  ",
+	"gitmod~4. ",
+	"GITMOD~1 .",
+	"gitmod~2   ",
+	"GITMOD~3.  ",
+	"gitmod~4 . ",
+
+	"GI7EBA~1",
+	"gi7eba~9",
+
+	"GI7EB~10",
+	"GI7EB~11",
+	"GI7EB~99",
+	"GI7EB~10",
+	"GI7E~100",
+	"GI7E~101",
+	"GI7E~999",
+	"~1000000",
+	"~9999999",
+};
+
+static char *gitmodules_not_altnames[] = {
+	".gitmodules x",
+	".gitmodules .x",
+
+	" .gitmodules",
+
+	"..gitmodules",
+
+	"gitmodules",
+
+	".gitmodule",
+
+	".gitmodules x ",
+	".gitmodules .x",
+
+	"GI7EBA~",
+	"GI7EBA~0",
+	"GI7EBA~~1",
+	"GI7EBA~X",
+	"Gx7EBA~1",
+	"GI7EBX~1",
+
+	"GI7EB~1",
+	"GI7EB~01",
+	"GI7EB~1",
+};
+
+void test_path_dotgit__dotgit_modules(void)
+{
+	size_t i;
+	cl_assert_equal_i(1, git_path_is_dotgit_modules(".gitmodules"));
+	cl_assert_equal_i(1, git_path_is_dotgit_modules(".git\xe2\x80\x8cmodules"));
+
+	for (i = 0; i < ARRAY_SIZE(gitmodules_altnames); i++) {
+		const char *name = gitmodules_altnames[i];
+		if (!git_path_is_dotgit_modules(name))
+			cl_fail(name);
+	}
+
+	for (i = 0; i < ARRAY_SIZE(gitmodules_not_altnames); i++) {
+		const char *name = gitmodules_not_altnames[i];
+		if (git_path_is_dotgit_modules(name))
+			cl_fail(name);
+	}
+
+}