]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-path-utils.c
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / t / helper / test-path-utils.c
CommitLineData
b8d5cf4f 1#include "test-tool.h"
ae299be0 2#include "cache.h"
31171d9e 3#include "string-list.h"
dc2d9ba3 4#include "utf8.h"
ae299be0 5
9e2326c7
MH
6/*
7 * A "string_list_each_func_t" function that normalizes an entry from
8 * GIT_CEILING_DIRECTORIES. If the path is unusable for some reason,
9 * die with an explanation.
10 */
11static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
12{
62f17513 13 char *ceil = item->string;
9e2326c7 14
62f17513 15 if (!*ceil)
9e2326c7 16 die("Empty path is not supported");
9e2326c7
MH
17 if (!is_absolute_path(ceil))
18 die("Path \"%s\" is not absolute", ceil);
62f17513 19 if (normalize_path_copy(ceil, ceil) < 0)
9e2326c7 20 die("Path \"%s\" could not be normalized", ceil);
9e2326c7
MH
21 return 1;
22}
23
203439b2
JX
24static void normalize_argv_string(const char **var, const char *input)
25{
26 if (!strcmp(input, "<null>"))
27 *var = NULL;
28 else if (!strcmp(input, "<empty>"))
29 *var = "";
30 else
31 *var = input;
32
33 if (*var && (**var == '<' || **var == '('))
34 die("Bad value: %s\n", input);
35}
36
7d1aaa68
JS
37struct test_data {
38 const char *from; /* input: transform from this ... */
39 const char *to; /* output: ... to this. */
371471ce 40 const char *alternative; /* output: ... or this. */
7d1aaa68
JS
41};
42
29c2eda8
RS
43/*
44 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
45 * have const parameters.
46 */
47static char *posix_basename(char *path)
48{
49 return basename(path);
50}
51
52static char *posix_dirname(char *path)
53{
54 return dirname(path);
55}
56
7d1aaa68
JS
57static int test_function(struct test_data *data, char *(*func)(char *input),
58 const char *funcname)
59{
60 int failed = 0, i;
61 char buffer[1024];
62 char *to;
63
64 for (i = 0; data[i].to; i++) {
65 if (!data[i].from)
66 to = func(NULL);
67 else {
7b11a18a 68 xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
7d1aaa68
JS
69 to = func(buffer);
70 }
371471ce
JS
71 if (!strcmp(to, data[i].to))
72 continue;
73 if (!data[i].alternative)
7d1aaa68
JS
74 error("FAIL: %s(%s) => '%s' != '%s'\n",
75 funcname, data[i].from, to, data[i].to);
371471ce
JS
76 else if (!strcmp(to, data[i].alternative))
77 continue;
78 else
79 error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
80 funcname, data[i].from, to, data[i].to,
81 data[i].alternative);
82 failed = 1;
7d1aaa68
JS
83 }
84 return failed;
85}
86
87static struct test_data basename_data[] = {
88 /* --- POSIX type paths --- */
89 { NULL, "." },
90 { "", "." },
91 { ".", "." },
92 { "..", ".." },
93 { "/", "/" },
371471ce
JS
94 { "//", "/", "//" },
95 { "///", "/", "//" },
96 { "////", "/", "//" },
7d1aaa68
JS
97 { "usr", "usr" },
98 { "/usr", "usr" },
99 { "/usr/", "usr" },
100 { "/usr//", "usr" },
101 { "/usr/lib", "lib" },
102 { "usr/lib", "lib" },
103 { "usr/lib///", "lib" },
104
105#if defined(__MINGW32__) || defined(_MSC_VER)
7d1aaa68
JS
106 /* --- win32 type paths --- */
107 { "\\usr", "usr" },
108 { "\\usr\\", "usr" },
109 { "\\usr\\\\", "usr" },
110 { "\\usr\\lib", "lib" },
111 { "usr\\lib", "lib" },
112 { "usr\\lib\\\\\\", "lib" },
113 { "C:/usr", "usr" },
114 { "C:/usr", "usr" },
115 { "C:/usr/", "usr" },
116 { "C:/usr//", "usr" },
117 { "C:/usr/lib", "lib" },
118 { "C:usr/lib", "lib" },
119 { "C:usr/lib///", "lib" },
120 { "C:", "." },
121 { "C:a", "a" },
122 { "C:/", "/" },
123 { "C:///", "/" },
371471ce
JS
124 { "\\", "\\", "/" },
125 { "\\\\", "\\", "/" },
126 { "\\\\\\", "\\", "/" },
7d1aaa68
JS
127#endif
128 { NULL, NULL }
129};
130
131static struct test_data dirname_data[] = {
132 /* --- POSIX type paths --- */
133 { NULL, "." },
134 { "", "." },
135 { ".", "." },
136 { "..", "." },
137 { "/", "/" },
371471ce
JS
138 { "//", "/", "//" },
139 { "///", "/", "//" },
140 { "////", "/", "//" },
7d1aaa68
JS
141 { "usr", "." },
142 { "/usr", "/" },
143 { "/usr/", "/" },
144 { "/usr//", "/" },
145 { "/usr/lib", "/usr" },
146 { "usr/lib", "usr" },
147 { "usr/lib///", "usr" },
148
149#if defined(__MINGW32__) || defined(_MSC_VER)
7d1aaa68
JS
150 /* --- win32 type paths --- */
151 { "\\", "\\" },
152 { "\\\\", "\\\\" },
153 { "\\usr", "\\" },
154 { "\\usr\\", "\\" },
155 { "\\usr\\\\", "\\" },
156 { "\\usr\\lib", "\\usr" },
157 { "usr\\lib", "usr" },
158 { "usr\\lib\\\\\\", "usr" },
159 { "C:a", "C:." },
160 { "C:/", "C:/" },
161 { "C:///", "C:/" },
162 { "C:/usr", "C:/" },
163 { "C:/usr/", "C:/" },
164 { "C:/usr//", "C:/" },
165 { "C:/usr/lib", "C:/usr" },
166 { "C:usr/lib", "C:usr" },
167 { "C:usr/lib///", "C:usr" },
168 { "\\\\\\", "\\" },
169 { "\\\\\\\\", "\\" },
371471ce 170 { "C:", "C:.", "." },
7d1aaa68
JS
171#endif
172 { NULL, NULL }
173};
174
801ed010
JK
175static int check_dotfile(const char *x, const char **argv,
176 int (*is_hfs)(const char *),
177 int (*is_ntfs)(const char *))
dc2d9ba3 178{
801ed010
JK
179 int res = 0, expect = 1;
180 for (; *argv; argv++) {
181 if (!strcmp("--not", *argv))
182 expect = !expect;
183 else if (expect != (is_hfs(*argv) || is_ntfs(*argv)))
184 res = error("'%s' is %s.git%s", *argv,
185 expect ? "not " : "", x);
186 else
187 fprintf(stderr, "ok: '%s' is %s.git%s\n",
188 *argv, expect ? "" : "not ", x);
189 }
190 return !!res;
dc2d9ba3
JS
191}
192
b819f1d2
JS
193static int cmp_by_st_size(const void *a, const void *b)
194{
195 intptr_t x = (intptr_t)((struct string_list_item *)a)->util;
196 intptr_t y = (intptr_t)((struct string_list_item *)b)->util;
197
198 return x > y ? -1 : (x < y ? +1 : 0);
199}
200
a62f9d1a
GS
201/*
202 * A very simple, reproducible pseudo-random generator. Copied from
203 * `test-genrandom.c`.
204 */
205static uint64_t my_random_value = 1234;
206
207static uint64_t my_random(void)
208{
209 my_random_value = my_random_value * 1103515245 + 12345;
210 return my_random_value;
211}
212
213/*
214 * A fast approximation of the square root, without requiring math.h.
215 *
216 * It uses Newton's method to approximate the solution of 0 = x^2 - value.
217 */
218static double my_sqrt(double value)
219{
220 const double epsilon = 1e-6;
221 double x = value;
222
223 if (value == 0)
224 return 0;
225
226 for (;;) {
227 double delta = (value / x - x) / 2;
228 if (delta < epsilon && delta > -epsilon)
229 return x + delta;
230 x += delta;
231 }
232}
233
234static int protect_ntfs_hfs_benchmark(int argc, const char **argv)
235{
236 size_t i, j, nr, min_len = 3, max_len = 20;
237 char **names;
238 int repetitions = 15, file_mode = 0100644;
239 uint64_t begin, end;
240 double m[3][2], v[3][2];
241 uint64_t cumul;
242 double cumul2;
243
244 if (argc > 1 && !strcmp(argv[1], "--with-symlink-mode")) {
245 file_mode = 0120000;
246 argc--;
247 argv++;
248 }
249
250 nr = argc > 1 ? strtoul(argv[1], NULL, 0) : 1000000;
251 ALLOC_ARRAY(names, nr);
252
253 if (argc > 2) {
254 min_len = strtoul(argv[2], NULL, 0);
255 if (argc > 3)
256 max_len = strtoul(argv[3], NULL, 0);
257 if (min_len > max_len)
258 die("min_len > max_len");
259 }
260
261 for (i = 0; i < nr; i++) {
262 size_t len = min_len + (my_random() % (max_len + 1 - min_len));
263
264 names[i] = xmallocz(len);
265 while (len > 0)
266 names[i][--len] = (char)(' ' + (my_random() % ('\x7f' - ' ')));
267 }
268
269 for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
270 for (protect_hfs = 0; protect_hfs < 2; protect_hfs++) {
271 cumul = 0;
272 cumul2 = 0;
273 for (i = 0; i < repetitions; i++) {
274 begin = getnanotime();
275 for (j = 0; j < nr; j++)
276 verify_path(names[j], file_mode);
277 end = getnanotime();
278 printf("protect_ntfs = %d, protect_hfs = %d: %lfms\n", protect_ntfs, protect_hfs, (end-begin) / (double)1e6);
279 cumul += end - begin;
280 cumul2 += (end - begin) * (end - begin);
281 }
282 m[protect_ntfs][protect_hfs] = cumul / (double)repetitions;
283 v[protect_ntfs][protect_hfs] = my_sqrt(cumul2 / (double)repetitions - m[protect_ntfs][protect_hfs] * m[protect_ntfs][protect_hfs]);
284 printf("mean: %lfms, stddev: %lfms\n", m[protect_ntfs][protect_hfs] / (double)1e6, v[protect_ntfs][protect_hfs] / (double)1e6);
285 }
286
287 for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
288 for (protect_hfs = 0; protect_hfs < 2; protect_hfs++)
289 printf("ntfs=%d/hfs=%d: %lf%% slower\n", protect_ntfs, protect_hfs, (m[protect_ntfs][protect_hfs] - m[0][0]) * 100 / m[0][0]);
290
291 return 0;
292}
293
b8d5cf4f 294int cmd__path_utils(int argc, const char **argv)
ae299be0 295{
f42302b4 296 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
62f17513 297 char *buf = xmallocz(strlen(argv[2]));
f42302b4 298 int rv = normalize_path_copy(buf, argv[2]);
e287a5b0
ÆAB
299 puts(rv ? "++failed++" : buf);
300 free(buf);
2cd85c40 301 return 0;
ae299be0
DR
302 }
303
e2a57aac 304 if (argc >= 2 && !strcmp(argv[1], "real_path")) {
3d7747e3 305 struct strbuf realpath = STRBUF_INIT;
d553e737 306 while (argc > 2) {
3d7747e3
AM
307 strbuf_realpath(&realpath, argv[2], 1);
308 puts(realpath.buf);
d553e737
DR
309 argc--;
310 argv++;
311 }
3d7747e3 312 strbuf_release(&realpath);
2cd85c40 313 return 0;
d553e737
DR
314 }
315
87a246e1
MH
316 if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
317 while (argc > 2) {
318 puts(absolute_path(argv[2]));
319 argc--;
320 argv++;
321 }
322 return 0;
323 }
324
0454dd93 325 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
31171d9e
MH
326 int len;
327 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
9e2326c7 328 char *path = xstrdup(argv[2]);
31171d9e 329
9e2326c7
MH
330 /*
331 * We have to normalize the arguments because under
332 * Windows, bash mangles arguments that look like
333 * absolute POSIX paths or colon-separate lists of
334 * absolute POSIX paths into DOS paths (e.g.,
335 * "/foo:/foo/bar" might be converted to
336 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
337 * whereas longest_ancestor_length() requires paths
338 * that use forward slashes.
339 */
340 if (normalize_path_copy(path, path))
341 die("Path \"%s\" could not be normalized", argv[2]);
31171d9e 342 string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
9e2326c7
MH
343 filter_string_list(&ceiling_dirs, 0,
344 normalize_ceiling_entry, NULL);
345 len = longest_ancestor_length(path, &ceiling_dirs);
31171d9e 346 string_list_clear(&ceiling_dirs, 0);
9e2326c7 347 free(path);
0454dd93 348 printf("%d\n", len);
2cd85c40 349 return 0;
0454dd93
DR
350 }
351
9e813723 352 if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
3f2e2297 353 const char *prefix = argv[2];
9e813723
MH
354 int prefix_len = strlen(prefix);
355 int nongit_ok;
356 setup_git_directory_gently(&nongit_ok);
357 while (argc > 3) {
e287a5b0
ÆAB
358 char *pfx = prefix_path(prefix, prefix_len, argv[3]);
359
360 puts(pfx);
361 free(pfx);
9e813723
MH
362 argc--;
363 argv++;
364 }
365 return 0;
366 }
367
4fcc86b0
JS
368 if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
369 char *prefix = strip_path_suffix(argv[2], argv[3]);
370 printf("%s\n", prefix ? prefix : "(null)");
e287a5b0 371 free(prefix);
4fcc86b0
JS
372 return 0;
373 }
374
7ffd18fc 375 if (argc == 3 && !strcmp(argv[1], "print_path")) {
abd4284b
JX
376 puts(argv[2]);
377 return 0;
378 }
379
203439b2 380 if (argc == 4 && !strcmp(argv[1], "relative_path")) {
e02ca72f 381 struct strbuf sb = STRBUF_INIT;
203439b2
JX
382 const char *in, *prefix, *rel;
383 normalize_argv_string(&in, argv[2]);
384 normalize_argv_string(&prefix, argv[3]);
e02ca72f 385 rel = relative_path(in, prefix, &sb);
203439b2
JX
386 if (!rel)
387 puts("(null)");
388 else
389 puts(strlen(rel) > 0 ? rel : "(empty)");
e02ca72f 390 strbuf_release(&sb);
203439b2
JX
391 return 0;
392 }
393
7d1aaa68 394 if (argc == 2 && !strcmp(argv[1], "basename"))
29c2eda8 395 return test_function(basename_data, posix_basename, argv[1]);
7d1aaa68
JS
396
397 if (argc == 2 && !strcmp(argv[1], "dirname"))
29c2eda8 398 return test_function(dirname_data, posix_dirname, argv[1]);
7d1aaa68 399
dc2d9ba3 400 if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) {
801ed010
JK
401 return check_dotfile("modules", argv + 2,
402 is_hfs_dotgitmodules,
403 is_ntfs_dotgitmodules);
404 }
405 if (argc > 2 && !strcmp(argv[1], "is_dotgitignore")) {
406 return check_dotfile("ignore", argv + 2,
407 is_hfs_dotgitignore,
408 is_ntfs_dotgitignore);
409 }
410 if (argc > 2 && !strcmp(argv[1], "is_dotgitattributes")) {
411 return check_dotfile("attributes", argv + 2,
412 is_hfs_dotgitattributes,
413 is_ntfs_dotgitattributes);
414 }
415 if (argc > 2 && !strcmp(argv[1], "is_dotmailmap")) {
416 return check_dotfile("mailmap", argv + 2,
417 is_hfs_dotmailmap,
418 is_ntfs_dotmailmap);
dc2d9ba3
JS
419 }
420
5868bd86
JS
421 if (argc > 2 && !strcmp(argv[1], "file-size")) {
422 int res = 0, i;
423 struct stat st;
424
425 for (i = 2; i < argc; i++)
426 if (stat(argv[i], &st))
427 res = error_errno("Cannot stat '%s'", argv[i]);
428 else
429 printf("%"PRIuMAX"\n", (uintmax_t)st.st_size);
430 return !!res;
431 }
432
af9912ef
JS
433 if (argc == 4 && !strcmp(argv[1], "skip-n-bytes")) {
434 int fd = open(argv[2], O_RDONLY), offset = atoi(argv[3]);
435 char buffer[65536];
436
437 if (fd < 0)
438 die_errno("could not open '%s'", argv[2]);
439 if (lseek(fd, offset, SEEK_SET) < 0)
440 die_errno("could not skip %d bytes", offset);
441 for (;;) {
442 ssize_t count = read(fd, buffer, sizeof(buffer));
443 if (count < 0)
444 die_errno("could not read '%s'", argv[2]);
445 if (!count)
446 break;
447 if (write(1, buffer, count) < 0)
448 die_errno("could not write to stdout");
449 }
450 close(fd);
451 return 0;
452 }
453
b819f1d2
JS
454 if (argc > 5 && !strcmp(argv[1], "slice-tests")) {
455 int res = 0;
456 long offset, stride, i;
457 struct string_list list = STRING_LIST_INIT_NODUP;
458 struct stat st;
459
460 offset = strtol(argv[2], NULL, 10);
461 stride = strtol(argv[3], NULL, 10);
462 if (stride < 1)
463 stride = 1;
464 for (i = 4; i < argc; i++)
465 if (stat(argv[i], &st))
466 res = error_errno("Cannot stat '%s'", argv[i]);
467 else
468 string_list_append(&list, argv[i])->util =
469 (void *)(intptr_t)st.st_size;
470 QSORT(list.items, list.nr, cmp_by_st_size);
471 for (i = offset; i < list.nr; i+= stride)
472 printf("%s\n", list.items[i].string);
473
474 return !!res;
475 }
476
a62f9d1a
GS
477 if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
478 return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
479
d2c84dad
JS
480 if (argc > 1 && !strcmp(argv[1], "is_valid_path")) {
481 int res = 0, expect = 1, i;
482
483 for (i = 2; i < argc; i++)
484 if (!strcmp("--not", argv[i]))
485 expect = 0;
486 else if (expect != is_valid_path(argv[i]))
487 res = error("'%s' is%s a valid path",
488 argv[i], expect ? " not" : "");
489 else
490 fprintf(stderr,
491 "'%s' is%s a valid path\n",
492 argv[i], expect ? "" : " not");
493
494 return !!res;
495 }
496
2cd85c40
JS
497 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
498 argv[1] ? argv[1] : "(there was none)");
499 return 1;
ae299be0 500}