Skip to content

Commit e01c6b1

Browse files
committed
Merge branch 'js/dirname-basename'
dirname() emulation has been added, as Msys2 lacks it. * js/dirname-basename: mingw: avoid linking to the C library's isalpha() t0060: loosen overly strict expectations t0060: verify that basename() and dirname() work as expected compat/basename.c: provide a dirname() compatibility function compat/basename: make basename() conform to POSIX Refactor skipping DOS drive prefixes
2 parents ebcdd63 + e7d5ce8 commit e01c6b1

File tree

7 files changed

+225
-24
lines changed

7 files changed

+225
-24
lines changed

compat/basename.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,71 @@
11
#include "../git-compat-util.h"
2+
#include "../strbuf.h"
23

34
/* Adapted from libiberty's basename.c. */
45
char *gitbasename (char *path)
56
{
67
const char *base;
7-
/* Skip over the disk name in MSDOS pathnames. */
8-
if (has_dos_drive_prefix(path))
9-
path += 2;
8+
9+
if (path)
10+
skip_dos_drive_prefix(&path);
11+
12+
if (!path || !*path)
13+
return ".";
14+
1015
for (base = path; *path; path++) {
11-
if (is_dir_sep(*path))
12-
base = path + 1;
16+
if (!is_dir_sep(*path))
17+
continue;
18+
do {
19+
path++;
20+
} while (is_dir_sep(*path));
21+
if (*path)
22+
base = path;
23+
else
24+
while (--path != base && is_dir_sep(*path))
25+
*path = '\0';
1326
}
1427
return (char *)base;
1528
}
29+
30+
char *gitdirname(char *path)
31+
{
32+
static struct strbuf buf = STRBUF_INIT;
33+
char *p = path, *slash = NULL, c;
34+
int dos_drive_prefix;
35+
36+
if (!p)
37+
return ".";
38+
39+
if ((dos_drive_prefix = skip_dos_drive_prefix(&p)) && !*p)
40+
goto dot;
41+
42+
/*
43+
* POSIX.1-2001 says dirname("/") should return "/", and dirname("//")
44+
* should return "//", but dirname("///") should return "/" again.
45+
*/
46+
if (is_dir_sep(*p)) {
47+
if (!p[1] || (is_dir_sep(p[1]) && !p[2]))
48+
return path;
49+
slash = ++p;
50+
}
51+
while ((c = *(p++)))
52+
if (is_dir_sep(c)) {
53+
char *tentative = p - 1;
54+
55+
/* POSIX.1-2001 says to ignore trailing slashes */
56+
while (is_dir_sep(*p))
57+
p++;
58+
if (*p)
59+
slash = tentative;
60+
}
61+
62+
if (slash) {
63+
*slash = '\0';
64+
return path;
65+
}
66+
67+
dot:
68+
strbuf_reset(&buf);
69+
strbuf_addf(&buf, "%.*s.", dos_drive_prefix, path);
70+
return buf.buf;
71+
}

compat/mingw.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,28 +1935,31 @@ pid_t waitpid(pid_t pid, int *status, int options)
19351935
return -1;
19361936
}
19371937

1938+
int mingw_skip_dos_drive_prefix(char **path)
1939+
{
1940+
int ret = has_dos_drive_prefix(*path);
1941+
*path += ret;
1942+
return ret;
1943+
}
1944+
19381945
int mingw_offset_1st_component(const char *path)
19391946
{
1940-
int offset = 0;
1941-
if (has_dos_drive_prefix(path))
1942-
offset = 2;
1947+
char *pos = (char *)path;
19431948

19441949
/* unc paths */
1945-
else if (is_dir_sep(path[0]) && is_dir_sep(path[1])) {
1946-
1950+
if (!skip_dos_drive_prefix(&pos) &&
1951+
is_dir_sep(pos[0]) && is_dir_sep(pos[1])) {
19471952
/* skip server name */
1948-
char *pos = strpbrk(path + 2, "\\/");
1953+
pos = strpbrk(pos + 2, "\\/");
19491954
if (!pos)
19501955
return 0; /* Error: malformed unc path */
19511956

19521957
do {
19531958
pos++;
19541959
} while (*pos && !is_dir_sep(*pos));
1955-
1956-
offset = pos - path;
19571960
}
19581961

1959-
return offset + is_dir_sep(path[offset]);
1962+
return pos + is_dir_sep(*pos) - path;
19601963
}
19611964

19621965
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)

compat/mingw.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ HANDLE winansi_get_osfhandle(int fd);
383383
* git specific compatibility
384384
*/
385385

386-
#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
386+
#define has_dos_drive_prefix(path) \
387+
(isalpha(*(path)) && (path)[1] == ':' ? 2 : 0)
388+
int mingw_skip_dos_drive_prefix(char **path);
389+
#define skip_dos_drive_prefix mingw_skip_dos_drive_prefix
387390
#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
388391
static inline char *mingw_find_last_dir_sep(const char *path)
389392
{

git-compat-util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ struct itimerval {
253253
#else
254254
#define basename gitbasename
255255
extern char *gitbasename(char *);
256+
#define dirname gitdirname
257+
extern char *gitdirname(char *);
256258
#endif
257259

258260
#ifndef NO_ICONV
@@ -335,6 +337,14 @@ static inline int git_has_dos_drive_prefix(const char *path)
335337
#define has_dos_drive_prefix git_has_dos_drive_prefix
336338
#endif
337339

340+
#ifndef skip_dos_drive_prefix
341+
static inline int git_skip_dos_drive_prefix(char **path)
342+
{
343+
return 0;
344+
}
345+
#define skip_dos_drive_prefix git_skip_dos_drive_prefix
346+
#endif
347+
338348
#ifndef is_dir_sep
339349
static inline int git_is_dir_sep(int c)
340350
{

path.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -782,13 +782,10 @@ const char *relative_path(const char *in, const char *prefix,
782782
else if (!prefix_len)
783783
return in;
784784

785-
if (have_same_root(in, prefix)) {
785+
if (have_same_root(in, prefix))
786786
/* bypass dos_drive, for "c:" is identical to "C:" */
787-
if (has_dos_drive_prefix(in)) {
788-
i = 2;
789-
j = 2;
790-
}
791-
} else {
787+
i = j = has_dos_drive_prefix(in);
788+
else {
792789
return in;
793790
}
794791

@@ -943,11 +940,10 @@ const char *remove_leading_path(const char *in, const char *prefix)
943940
int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
944941
{
945942
char *dst0;
943+
int i;
946944

947-
if (has_dos_drive_prefix(src)) {
945+
for (i = has_dos_drive_prefix(src); i > 0; i--)
948946
*dst++ = *src++;
949-
*dst++ = *src++;
950-
}
951947
dst0 = dst;
952948

953949
if (is_dir_sep(*src)) {

t/t0060-path-utils.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ case $(uname -s) in
5959
;;
6060
esac
6161

62+
test_expect_success basename 'test-path-utils basename'
63+
test_expect_success dirname 'test-path-utils dirname'
64+
6265
norm_path "" ""
6366
norm_path . ""
6467
norm_path ./ ""

test-path-utils.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,130 @@ static void normalize_argv_string(const char **var, const char *input)
3939
die("Bad value: %s\n", input);
4040
}
4141

42+
struct test_data {
43+
const char *from; /* input: transform from this ... */
44+
const char *to; /* output: ... to this. */
45+
const char *alternative; /* output: ... or this. */
46+
};
47+
48+
static int test_function(struct test_data *data, char *(*func)(char *input),
49+
const char *funcname)
50+
{
51+
int failed = 0, i;
52+
char buffer[1024];
53+
char *to;
54+
55+
for (i = 0; data[i].to; i++) {
56+
if (!data[i].from)
57+
to = func(NULL);
58+
else {
59+
strcpy(buffer, data[i].from);
60+
to = func(buffer);
61+
}
62+
if (!strcmp(to, data[i].to))
63+
continue;
64+
if (!data[i].alternative)
65+
error("FAIL: %s(%s) => '%s' != '%s'\n",
66+
funcname, data[i].from, to, data[i].to);
67+
else if (!strcmp(to, data[i].alternative))
68+
continue;
69+
else
70+
error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
71+
funcname, data[i].from, to, data[i].to,
72+
data[i].alternative);
73+
failed = 1;
74+
}
75+
return failed;
76+
}
77+
78+
static struct test_data basename_data[] = {
79+
/* --- POSIX type paths --- */
80+
{ NULL, "." },
81+
{ "", "." },
82+
{ ".", "." },
83+
{ "..", ".." },
84+
{ "/", "/" },
85+
{ "//", "/", "//" },
86+
{ "///", "/", "//" },
87+
{ "////", "/", "//" },
88+
{ "usr", "usr" },
89+
{ "/usr", "usr" },
90+
{ "/usr/", "usr" },
91+
{ "/usr//", "usr" },
92+
{ "/usr/lib", "lib" },
93+
{ "usr/lib", "lib" },
94+
{ "usr/lib///", "lib" },
95+
96+
#if defined(__MINGW32__) || defined(_MSC_VER)
97+
/* --- win32 type paths --- */
98+
{ "\\usr", "usr" },
99+
{ "\\usr\\", "usr" },
100+
{ "\\usr\\\\", "usr" },
101+
{ "\\usr\\lib", "lib" },
102+
{ "usr\\lib", "lib" },
103+
{ "usr\\lib\\\\\\", "lib" },
104+
{ "C:/usr", "usr" },
105+
{ "C:/usr", "usr" },
106+
{ "C:/usr/", "usr" },
107+
{ "C:/usr//", "usr" },
108+
{ "C:/usr/lib", "lib" },
109+
{ "C:usr/lib", "lib" },
110+
{ "C:usr/lib///", "lib" },
111+
{ "C:", "." },
112+
{ "C:a", "a" },
113+
{ "C:/", "/" },
114+
{ "C:///", "/" },
115+
{ "\\", "\\", "/" },
116+
{ "\\\\", "\\", "/" },
117+
{ "\\\\\\", "\\", "/" },
118+
#endif
119+
{ NULL, NULL }
120+
};
121+
122+
static struct test_data dirname_data[] = {
123+
/* --- POSIX type paths --- */
124+
{ NULL, "." },
125+
{ "", "." },
126+
{ ".", "." },
127+
{ "..", "." },
128+
{ "/", "/" },
129+
{ "//", "/", "//" },
130+
{ "///", "/", "//" },
131+
{ "////", "/", "//" },
132+
{ "usr", "." },
133+
{ "/usr", "/" },
134+
{ "/usr/", "/" },
135+
{ "/usr//", "/" },
136+
{ "/usr/lib", "/usr" },
137+
{ "usr/lib", "usr" },
138+
{ "usr/lib///", "usr" },
139+
140+
#if defined(__MINGW32__) || defined(_MSC_VER)
141+
/* --- win32 type paths --- */
142+
{ "\\", "\\" },
143+
{ "\\\\", "\\\\" },
144+
{ "\\usr", "\\" },
145+
{ "\\usr\\", "\\" },
146+
{ "\\usr\\\\", "\\" },
147+
{ "\\usr\\lib", "\\usr" },
148+
{ "usr\\lib", "usr" },
149+
{ "usr\\lib\\\\\\", "usr" },
150+
{ "C:a", "C:." },
151+
{ "C:/", "C:/" },
152+
{ "C:///", "C:/" },
153+
{ "C:/usr", "C:/" },
154+
{ "C:/usr/", "C:/" },
155+
{ "C:/usr//", "C:/" },
156+
{ "C:/usr/lib", "C:/usr" },
157+
{ "C:usr/lib", "C:usr" },
158+
{ "C:usr/lib///", "C:usr" },
159+
{ "\\\\\\", "\\" },
160+
{ "\\\\\\\\", "\\" },
161+
{ "C:", "C:.", "." },
162+
#endif
163+
{ NULL, NULL }
164+
};
165+
42166
int main(int argc, char **argv)
43167
{
44168
if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
@@ -133,6 +257,12 @@ int main(int argc, char **argv)
133257
return 0;
134258
}
135259

260+
if (argc == 2 && !strcmp(argv[1], "basename"))
261+
return test_function(basename_data, basename, argv[1]);
262+
263+
if (argc == 2 && !strcmp(argv[1], "dirname"))
264+
return test_function(dirname_data, dirname, argv[1]);
265+
136266
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
137267
argv[1] ? argv[1] : "(there was none)");
138268
return 1;

0 commit comments

Comments
 (0)