Skip to content

Commit 9fd512c

Browse files
avargitster
authored andcommitted
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 63e95be (submodule: port resolve_relative_url from shell to C, 2016-04-15) and a2b26ff (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 a2b26ff was doing really doing the same as win32_is_dir_sep() added in 1cadad6 (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 <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 86f4e31 commit 9fd512c

File tree

9 files changed

+121
-38
lines changed

9 files changed

+121
-38
lines changed

builtin/submodule--helper.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,6 @@ static char *get_default_remote(void)
7272
return repo_get_default_remote(the_repository);
7373
}
7474

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-
8575
/*
8676
* Returns 1 if it was the last chop before ':'.
8777
*/
@@ -108,6 +98,16 @@ static int chop_last_dir(char **remoteurl, int is_relative)
10898
return 0;
10999
}
110100

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+
111111
/*
112112
* The `url` argument is the URL that navigates to the submodule origin
113113
* repo. When relative, this URL is relative to the superproject origin

compat/mingw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2830,7 +2830,7 @@ int is_valid_win32_path(const char *path, int allow_literal_nul)
28302830
}
28312831

28322832
c = path[i];
2833-
if (c && c != '.' && c != ':' && c != '/' && c != '\\')
2833+
if (c && c != '.' && c != ':' && !is_xplatform_dir_sep(c))
28342834
goto not_a_reserved_name;
28352835

28362836
/* contains reserved name */

compat/win32/path-utils.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ int win32_has_dos_drive_prefix(const char *path);
66

77
int win32_skip_dos_drive_prefix(char **path);
88
#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
1410
static inline char *win32_find_last_dir_sep(const char *path)
1511
{
1612
char *ret = NULL;

dir.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3890,3 +3890,32 @@ void relocate_gitdir(const char *path, const char *old_git_dir, const char *new_
38903890

38913891
connect_work_tree_and_git_dir(path, new_git_dir, 0);
38923892
}
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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,4 +578,67 @@ void connect_work_tree_and_git_dir(const char *work_tree,
578578
void relocate_gitdir(const char *path,
579579
const char *old_git_dir,
580580
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+
}
581644
#endif

fsck.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -975,27 +975,16 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
975975
return ret;
976976
}
977977

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)
987979
{
988-
return str[0] == '.' && (str[1] == '/' || str[1] == '\\');
980+
return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
981+
PATH_MATCH_XPLATFORM);
989982
}
990983

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)
997985
{
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);
999988
}
1000989

1001990
static int submodule_url_is_relative(const char *url)

git-compat-util.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@
236236
#include <sys/sysctl.h>
237237
#endif
238238

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+
239245
#if defined(__CYGWIN__)
240246
#include "compat/win32/path-utils.h"
241247
#endif
@@ -416,11 +422,11 @@ static inline int git_skip_dos_drive_prefix(char **path)
416422
#define skip_dos_drive_prefix git_skip_dos_drive_prefix
417423
#endif
418424

419-
#ifndef is_dir_sep
420425
static inline int git_is_dir_sep(int c)
421426
{
422427
return c == '/';
423428
}
429+
#ifndef is_dir_sep
424430
#define is_dir_sep git_is_dir_sep
425431
#endif
426432

path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ int is_ntfs_dotgit(const char *name)
14131413

14141414
for (;;) {
14151415
c = *(name++);
1416-
if (!c || c == '\\' || c == '/' || c == ':')
1416+
if (!c || is_xplatform_dir_sep(c) || c == ':')
14171417
return 1;
14181418
if (c != '.' && c != ' ')
14191419
return 0;

submodule-config.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,17 @@ int check_submodule_name(const char *name)
204204
return -1;
205205

206206
/*
207-
* Look for '..' as a path component. Check both '/' and '\\' as
207+
* Look for '..' as a path component. Check is_xplatform_dir_sep() as
208208
* separators rather than is_dir_sep(), because we want the name rules
209209
* to be consistent across platforms.
210210
*/
211211
goto in_component; /* always start inside component */
212212
while (*name) {
213213
char c = *name++;
214-
if (c == '/' || c == '\\') {
214+
if (is_xplatform_dir_sep(c)) {
215215
in_component:
216216
if (name[0] == '.' && name[1] == '.' &&
217-
(!name[2] || name[2] == '/' || name[2] == '\\'))
217+
(!name[2] || is_xplatform_dir_sep(name[2])))
218218
return -1;
219219
}
220220
}

0 commit comments

Comments
 (0)