dir API: add a generalized path_match_flags() function

Add a path_match_flags() function and have the two sets of starts_with_dot_{,dot_}slash() functions added in 63e95beb085 (submodule: port resolve_relative_url from shell to C, 2016-04-15) and a2b26ffb1a8 (fsck: convert gitmodules url to URL passed to curl, 2020-04-18) be thin wrappers for it. As the latter of those notes the fsck version was copied from the initial builtin/submodule--helper.c version. Since the code added in a2b26ffb1a8 was doing really doing the same as win32_is_dir_sep() added in 1cadad6f658 (git clone <url> C:\cygwin\home\USER\repo' is working (again), 2018-12-15) let's move the latter to git-compat-util.h is a is_xplatform_dir_sep(). We can then call either it or the platform-specific is_dir_sep() from this new function. Let's likewise change code in various other places that was hardcoding checks for "'/' || '\\'" with the new is_xplatform_dir_sep(). As can be seen in those callers some of them still concern themselves with ':' (Mac OS classic?), but let's leave the question of whether that should be consolidated for some other time. As we expect to make wider use of the "native" case in the future, define and use two starts_with_dot_{,dot_}slash_native() convenience wrappers. This makes the diff in builtin/submodule--helper.c much smaller. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed May 16, 2022 at 20:10 UTC 9fd512c8d6322df821971024ab36b16bebd760fb
9 files changed +121 -38
builtin/submodule--helper.c
+10 -10
@@ -72,16 +72,6 @@ static char *get_default_remote(void)
72 return repo_get_default_remote(the_repository);
73 }
74
75 -static int starts_with_dot_slash(const char *str)
76 -{
77 - return str[0] == '.' && is_dir_sep(str[1]);
78 -}
79 -
80 -static int starts_with_dot_dot_slash(const char *str)
81 -{
82 - return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
83 -}
84 -
75 /*
76 * Returns 1 if it was the last chop before ':'.
77 */
@@ -108,6 +98,16 @@ static int chop_last_dir(char **remoteurl, int is_relative)
98 return 0;
99 }
100
101 +static int starts_with_dot_slash(const char *const path)
102 +{
103 + return starts_with_dot_slash_native(path);;
104 +}
105 +
106 +static int starts_with_dot_dot_slash(const char *const path)
107 +{
108 + return starts_with_dot_dot_slash_native(path);
109 +}
110 +
111 /*
112 * The `url` argument is the URL that navigates to the submodule origin
113 * repo. When relative, this URL is relative to the superproject origin
compat/mingw.c
+1 -1
@@ -2830,7 +2830,7 @@ not_a_reserved_name:
2830 }
2831
2832 c = path[i];
2833 - if (c && c != '.' && c != ':' && c != '/' && c != '\\')
2833 + if (c && c != '.' && c != ':' && !is_xplatform_dir_sep(c))
2834 goto not_a_reserved_name;
2835
2836 /* contains reserved name */
compat/win32/path-utils.h
+1 -5
@@ -6,11 +6,7 @@ int win32_has_dos_drive_prefix(const char *path);
6
7 int win32_skip_dos_drive_prefix(char **path);
8 #define skip_dos_drive_prefix win32_skip_dos_drive_prefix
9 -static inline int win32_is_dir_sep(int c)
10 -{
11 - return c == '/' || c == '\\';
12 -}
13 -#define is_dir_sep win32_is_dir_sep
9 +#define is_dir_sep is_xplatform_dir_sep
10 static inline char *win32_find_last_dir_sep(const char *path)
11 {
12 char *ret = NULL;
dir.c
+29
@@ -3890,3 +3890,32 @@ void relocate_gitdir(const char *path, const char *old_git_dir, const char *new_
3890
3891 connect_work_tree_and_git_dir(path, new_git_dir, 0);
3892 }
3893 +
3894 +int path_match_flags(const char *const str, const enum path_match_flags flags)
3895 +{
3896 + const char *p = str;
3897 +
3898 + if (flags & PATH_MATCH_NATIVE &&
3899 + flags & PATH_MATCH_XPLATFORM)
3900 + BUG("path_match_flags() must get one match kind, not multiple!");
3901 + else if (!(flags & PATH_MATCH_KINDS_MASK))
3902 + BUG("path_match_flags() must get at least one match kind!");
3903 +
3904 + if (flags & PATH_MATCH_STARTS_WITH_DOT_SLASH &&
3905 + flags & PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH)
3906 + BUG("path_match_flags() must get one platform kind, not multiple!");
3907 + else if (!(flags & PATH_MATCH_PLATFORM_MASK))
3908 + BUG("path_match_flags() must get at least one platform kind!");
3909 +
3910 + if (*p++ != '.')
3911 + return 0;
3912 + if (flags & PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH &&
3913 + *p++ != '.')
3914 + return 0;
3915 +
3916 + if (flags & PATH_MATCH_NATIVE)
3917 + return is_dir_sep(*p);
3918 + else if (flags & PATH_MATCH_XPLATFORM)
3919 + return is_xplatform_dir_sep(*p);
3920 + BUG("unreachable");
3921 +}
dir.h
+63
@@ -578,4 +578,67 @@ void connect_work_tree_and_git_dir(const char *work_tree,
578 void relocate_gitdir(const char *path,
579 const char *old_git_dir,
580 const char *new_git_dir);
581 +
582 +/**
583 + * The "enum path_matches_kind" determines how path_match_flags() will
584 + * behave. The flags come in sets, and one (and only one) must be
585 + * provided out of each "set":
586 + *
587 + * PATH_MATCH_NATIVE:
588 + * Path separator is is_dir_sep()
589 + * PATH_MATCH_XPLATFORM:
590 + * Path separator is is_xplatform_dir_sep()
591 + *
592 + * Do we use is_dir_sep() to check for a directory separator
593 + * (*_NATIVE), or do we always check for '/' or '\' (*_XPLATFORM). The
594 + * "*_NATIVE" version on Windows is the same as "*_XPLATFORM",
595 + * everywhere else "*_NATIVE" means "only /".
596 + *
597 + * PATH_MATCH_STARTS_WITH_DOT_SLASH:
598 + * Match a path starting with "./"
599 + * PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH:
600 + * Match a path starting with "../"
601 + *
602 + * The "/" in the above is adjusted based on the "*_NATIVE" and
603 + * "*_XPLATFORM" flags.
604 + */
605 +enum path_match_flags {
606 + PATH_MATCH_NATIVE = 1 << 0,
607 + PATH_MATCH_XPLATFORM = 1 << 1,
608 + PATH_MATCH_STARTS_WITH_DOT_SLASH = 1 << 2,
609 + PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH = 1 << 3,
610 +};
611 +#define PATH_MATCH_KINDS_MASK (PATH_MATCH_STARTS_WITH_DOT_SLASH | \
612 + PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH)
613 +#define PATH_MATCH_PLATFORM_MASK (PATH_MATCH_NATIVE | PATH_MATCH_XPLATFORM)
614 +
615 +/**
616 + * path_match_flags() checks if a given "path" matches a given "enum
617 + * path_match_flags" criteria.
618 + */
619 +int path_match_flags(const char *const path, const enum path_match_flags f);
620 +
621 +/**
622 + * starts_with_dot_slash_native(): convenience wrapper for
623 + * path_match_flags() with PATH_MATCH_STARTS_WITH_DOT_SLASH and
624 + * PATH_MATCH_NATIVE.
625 + */
626 +static inline int starts_with_dot_slash_native(const char *const path)
627 +{
628 + const enum path_match_flags what = PATH_MATCH_STARTS_WITH_DOT_SLASH;
629 +
630 + return path_match_flags(path, what | PATH_MATCH_NATIVE);
631 +}
632 +
633 +/**
634 + * starts_with_dot_slash_native(): convenience wrapper for
635 + * path_match_flags() with PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH and
636 + * PATH_MATCH_NATIVE.
637 + */
638 +static inline int starts_with_dot_dot_slash_native(const char *const path)
639 +{
640 + const enum path_match_flags what = PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH;
641 +
642 + return path_match_flags(path, what | PATH_MATCH_NATIVE);
643 +}
644 #endif
fsck.c
+6 -17
@@ -975,27 +975,16 @@ done:
975 return ret;
976 }
977
978 -/*
979 - * Like builtin/submodule--helper.c's starts_with_dot_slash, but without
980 - * relying on the platform-dependent is_dir_sep helper.
981 - *
982 - * This is for use in checking whether a submodule URL is interpreted as
983 - * relative to the current directory on any platform, since \ is a
984 - * directory separator on Windows but not on other platforms.
985 - */
986 -static int starts_with_dot_slash(const char *str)
978 +static int starts_with_dot_slash(const char *const path)
979 {
988 - return str[0] == '.' && (str[1] == '/' || str[1] == '\\');
980 + return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
981 + PATH_MATCH_XPLATFORM);
982 }
983
991 -/*
992 - * Like starts_with_dot_slash, this is a variant of submodule--helper's
993 - * helper of the same name with the twist that it accepts backslash as a
994 - * directory separator even on non-Windows platforms.
995 - */
996 -static int starts_with_dot_dot_slash(const char *str)
984 +static int starts_with_dot_dot_slash(const char *const path)
985 {
998 - return str[0] == '.' && starts_with_dot_slash(str + 1);
986 + return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
987 + PATH_MATCH_XPLATFORM);
988 }
989
990 static int submodule_url_is_relative(const char *url)
git-compat-util.h
+7 -1
@@ -236,6 +236,12 @@
236 #include <sys/sysctl.h>
237 #endif
238
239 +/* Used by compat/win32/path-utils.h, and more */
240 +static inline int is_xplatform_dir_sep(int c)
241 +{
242 + return c == '/' || c == '\\';
243 +}
244 +
245 #if defined(__CYGWIN__)
246 #include "compat/win32/path-utils.h"
247 #endif
@@ -416,11 +422,11 @@ static inline int git_skip_dos_drive_prefix(char **path)
422 #define skip_dos_drive_prefix git_skip_dos_drive_prefix
423 #endif
424
419 -#ifndef is_dir_sep
425 static inline int git_is_dir_sep(int c)
426 {
427 return c == '/';
428 }
429 +#ifndef is_dir_sep
430 #define is_dir_sep git_is_dir_sep
431 #endif
432
path.c
+1 -1
@@ -1413,7 +1413,7 @@ int is_ntfs_dotgit(const char *name)
1413
1414 for (;;) {
1415 c = *(name++);
1416 - if (!c || c == '\\' || c == '/' || c == ':')
1416 + if (!c || is_xplatform_dir_sep(c) || c == ':')
1417 return 1;
1418 if (c != '.' && c != ' ')
1419 return 0;
submodule-config.c
+3 -3
@@ -204,17 +204,17 @@ int check_submodule_name(const char *name)
204 return -1;
205
206 /*
207 - * Look for '..' as a path component. Check both '/' and '\\' as
207 + * Look for '..' as a path component. Check is_xplatform_dir_sep() as
208 * separators rather than is_dir_sep(), because we want the name rules
209 * to be consistent across platforms.
210 */
211 goto in_component; /* always start inside component */
212 while (*name) {
213 char c = *name++;
214 - if (c == '/' || c == '\\') {
214 + if (is_xplatform_dir_sep(c)) {
215 in_component:
216 if (name[0] == '.' && name[1] == '.' &&
217 - (!name[2] || name[2] == '/' || name[2] == '\\'))
217 + (!name[2] || is_xplatform_dir_sep(name[2])))
218 return -1;
219 }
220 }