summaryrefslogtreecommitdiff
path: root/src/path.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2018-05-30 12:18:04 +0200
committerPatrick Steinhardt <ps@pks.im>2018-06-01 12:49:09 +0200
commit92159bd46568870264d72741390e387ce5dbe271 (patch)
tree2a1765bd21179a84a3e1893220d996283ed4f028 /src/path.c
parent771dfd1dd1c27a4693dfdfea521c07e72f456b29 (diff)
downloadlibgit2-92159bd46568870264d72741390e387ce5dbe271.tar.gz
path: unify `git_path_is_*` APIs
Right now, there's quite a lot of different function calls to determine whether a path component matches a specific name after normalization from the filesystem. We have a function for each of {gitattributes, gitmodules, gitignore} multiplicated with {generic, NTFS, HFS} checks. In the long time, this is unmaintainable in case there are e.g. new filesystems with specific semantics, blowing up the number of functions we need to implement. Replace all functions with a simple `git_path_is_gitfile` function, which accepts an enum pointing out the filename that is to be checked against as well as the filesystem normalizations to check for. This greatly simplifies implementation at the expense of the caller having to invoke a somewhat longer function call.
Diffstat (limited to 'src/path.c')
-rw-r--r--src/path.c92
1 files changed, 34 insertions, 58 deletions
diff --git a/src/path.c b/src/path.c
index 58a5aaf4a..77833eeaf 100644
--- a/src/path.c
+++ b/src/path.c
@@ -1766,14 +1766,14 @@ static bool verify_component(
if (flags & GIT_PATH_REJECT_DOT_GIT_HFS) {
if (!verify_dotgit_hfs(component, len))
return false;
- if (S_ISLNK(mode) && git_path_is_hfs_dotgit_modules(component, len))
+ if (S_ISLNK(mode) && git_path_is_gitfile(component, len, GIT_PATH_GITFILE_GITMODULES, GIT_PATH_FS_HFS))
return false;
}
if (flags & GIT_PATH_REJECT_DOT_GIT_NTFS) {
if (!verify_dotgit_ntfs(repo, component, len))
return false;
- if (S_ISLNK(mode) && git_path_is_ntfs_dotgit_modules(component, len))
+ if (S_ISLNK(mode) && git_path_is_gitfile(component, len, GIT_PATH_GITFILE_GITMODULES, GIT_PATH_FS_NTFS))
return false;
}
@@ -1872,64 +1872,40 @@ int git_path_normalize_slashes(git_buf *out, const char *path)
return 0;
}
-static int verify_dotgit_generic(const char *name, size_t len, const char *dotgit_name, size_t dotgit_len, const char *shortname_pfix)
-{
- if (!verify_dotgit_ntfs_generic(name, len, dotgit_name, dotgit_len, shortname_pfix))
- return false;
-
- return verify_dotgit_hfs_generic(name, len, dotgit_name, dotgit_len);
-}
-
-int git_path_is_ntfs_dotgit_modules(const char *name, size_t len)
-{
- return !verify_dotgit_ntfs_generic(name, len, "gitmodules", CONST_STRLEN("gitmodules"), "gi7eba");
-}
-
-int git_path_is_hfs_dotgit_modules(const char *name, size_t len)
-{
- return !verify_dotgit_hfs_generic(name, len, "gitmodules", CONST_STRLEN("gitmodules"));
-}
-
-int git_path_is_dotgit_modules(const char *name, size_t len)
-{
- if (git_path_is_hfs_dotgit_modules(name, len))
- return 1;
-
- return git_path_is_ntfs_dotgit_modules(name, len);
-}
-
-int git_path_is_ntfs_dotgit_ignore(const char *name, size_t len)
-{
- return !verify_dotgit_ntfs_generic(name, len, "gitignore", CONST_STRLEN("gitignore"), "gi250a");
-}
+static const struct {
+ const char *file;
+ const char *hash;
+ size_t filelen;
+} gitfiles[] = {
+ { "gitignore", "gi250a", CONST_STRLEN("gitignore") },
+ { "gitmodules", "gi7eba", CONST_STRLEN("gitmodules") },
+ { "gitattributes", "gi7d29", CONST_STRLEN("gitattributes") }
+};
-int git_path_is_hfs_dotgit_ignore(const char *name, size_t len)
+extern int git_path_is_gitfile(const char *path, size_t pathlen, git_path_gitfile gitfile, git_path_fs fs)
{
- return !verify_dotgit_hfs_generic(name, len, "gitignore", CONST_STRLEN("gitignore"));
-}
-
-int git_path_is_dotgit_ignore(const char *name, size_t len)
-{
- if (git_path_is_hfs_dotgit_ignore(name, len))
- return 1;
-
- return git_path_is_ntfs_dotgit_ignore(name, len);
-}
+ const char *file, *hash;
+ size_t filelen;
-int git_path_is_hfs_dotgit_attributes(const char *name, size_t len)
-{
- return !verify_dotgit_hfs_generic(name, len, "gitattributes", CONST_STRLEN("gitattributes"));
-}
-
-int git_path_is_ntfs_dotgit_attributes(const char *name, size_t len)
-{
- return !verify_dotgit_ntfs_generic(name, len, "gitattributes", CONST_STRLEN("gitattributes"), "gi7d29");
-}
-
-int git_path_is_dotgit_attributes(const char *name, size_t len)
-{
- if (git_path_is_hfs_dotgit_attributes(name, len))
- return 1;
+ if (gitfile < 0 && gitfile >= ARRAY_SIZE(gitfiles)) {
+ giterr_set(GITERR_OS, "invalid gitfile for path validation");
+ return -1;
+ }
- return git_path_is_ntfs_dotgit_attributes(name, len);
+ file = gitfiles[gitfile].file;
+ filelen = gitfiles[gitfile].filelen;
+ hash = gitfiles[gitfile].hash;
+
+ switch (fs) {
+ case GIT_PATH_FS_GENERIC:
+ return !verify_dotgit_ntfs_generic(path, pathlen, file, filelen, hash) ||
+ !verify_dotgit_hfs_generic(path, pathlen, file, filelen);
+ case GIT_PATH_FS_NTFS:
+ return !verify_dotgit_ntfs_generic(path, pathlen, file, filelen, hash);
+ case GIT_PATH_FS_HFS:
+ return !verify_dotgit_hfs_generic(path, pathlen, file, filelen);
+ default:
+ giterr_set(GITERR_OS, "invalid filesystem for path validation");
+ return -1;
+ }
}