3 #include "string-list.h"
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.
11 static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
13 char *ceil = item->string;
16 die("Empty path is not supported");
17 if (!is_absolute_path(ceil))
18 die("Path \"%s\" is not absolute", ceil);
19 if (normalize_path_copy(ceil, ceil) < 0)
20 die("Path \"%s\" could not be normalized", ceil);
24 static void normalize_argv_string(const char **var, const char *input)
26 if (!strcmp(input, "<null>"))
28 else if (!strcmp(input, "<empty>"))
33 if (*var && (**var == '<' || **var == '('))
34 die("Bad value: %s\n", input);
38 const char *from; /* input: transform from this ... */
39 const char *to; /* output: ... to this. */
40 const char *alternative; /* output: ... or this. */
44 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
45 * have const parameters.
47 static char *posix_basename(char *path)
49 return basename(path);
52 static char *posix_dirname(char *path)
57 static int test_function(struct test_data *data, char *(*func)(char *input),
64 for (i = 0; data[i].to; i++) {
68 xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
71 if (!strcmp(to, data[i].to))
73 if (!data[i].alternative)
74 error("FAIL: %s(%s) => '%s' != '%s'\n",
75 funcname, data[i].from, to, data[i].to);
76 else if (!strcmp(to, data[i].alternative))
79 error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
80 funcname, data[i].from, to, data[i].to,
87 static struct test_data basename_data[] = {
88 /* --- POSIX type paths --- */
96 { "////", "/", "//" },
101 { "/usr/lib", "lib" },
102 { "usr/lib", "lib" },
103 { "usr/lib///", "lib" },
105 #if defined(__MINGW32__) || defined(_MSC_VER)
106 /* --- win32 type paths --- */
108 { "\\usr\\", "usr" },
109 { "\\usr\\\\", "usr" },
110 { "\\usr\\lib", "lib" },
111 { "usr\\lib", "lib" },
112 { "usr\\lib\\\\\\", "lib" },
115 { "C:/usr/", "usr" },
116 { "C:/usr//", "usr" },
117 { "C:/usr/lib", "lib" },
118 { "C:usr/lib", "lib" },
119 { "C:usr/lib///", "lib" },
125 { "\\\\", "\\", "/" },
126 { "\\\\\\", "\\", "/" },
131 static struct test_data dirname_data[] = {
132 /* --- POSIX type paths --- */
139 { "///", "/", "//" },
140 { "////", "/", "//" },
145 { "/usr/lib", "/usr" },
146 { "usr/lib", "usr" },
147 { "usr/lib///", "usr" },
149 #if defined(__MINGW32__) || defined(_MSC_VER)
150 /* --- win32 type paths --- */
155 { "\\usr\\\\", "\\" },
156 { "\\usr\\lib", "\\usr" },
157 { "usr\\lib", "usr" },
158 { "usr\\lib\\\\\\", "usr" },
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" },
169 { "\\\\\\\\", "\\" },
170 { "C:", "C:.", "." },
175 static int check_dotfile(const char *x, const char **argv,
176 int (*is_hfs)(const char *),
177 int (*is_ntfs)(const char *))
179 int res = 0, expect = 1;
180 for (; *argv; argv++) {
181 if (!strcmp("--not", *argv))
183 else if (expect != (is_hfs(*argv) || is_ntfs(*argv)))
184 res = error("'%s' is %s.git%s", *argv,
185 expect ? "not " : "", x);
187 fprintf(stderr, "ok: '%s' is %s.git%s\n",
188 *argv, expect ? "" : "not ", x);
193 static int cmp_by_st_size(const void *a, const void *b)
195 intptr_t x = (intptr_t)((struct string_list_item *)a)->util;
196 intptr_t y = (intptr_t)((struct string_list_item *)b)->util;
198 return x > y ? -1 : (x < y ? +1 : 0);
202 * A very simple, reproducible pseudo-random generator. Copied from
203 * `test-genrandom.c`.
205 static uint64_t my_random_value = 1234;
207 static uint64_t my_random(void)
209 my_random_value = my_random_value * 1103515245 + 12345;
210 return my_random_value;
214 * A fast approximation of the square root, without requiring math.h.
216 * It uses Newton's method to approximate the solution of 0 = x^2 - value.
218 static double my_sqrt(double value)
220 const double epsilon = 1e-6;
227 double delta = (value / x - x) / 2;
228 if (delta < epsilon && delta > -epsilon)
234 static int protect_ntfs_hfs_benchmark(int argc, const char **argv)
236 size_t i, j, nr, min_len = 3, max_len = 20;
238 int repetitions = 15, file_mode = 0100644;
240 double m[3][2], v[3][2];
244 if (argc > 1 && !strcmp(argv[1], "--with-symlink-mode")) {
250 nr = argc > 1 ? strtoul(argv[1], NULL, 0) : 1000000;
251 ALLOC_ARRAY(names, nr);
254 min_len = strtoul(argv[2], NULL, 0);
256 max_len = strtoul(argv[3], NULL, 0);
257 if (min_len > max_len)
258 die("min_len > max_len");
261 for (i = 0; i < nr; i++) {
262 size_t len = min_len + (my_random() % (max_len + 1 - min_len));
264 names[i] = xmallocz(len);
266 names[i][--len] = (char)(' ' + (my_random() % ('\x7f' - ' ')));
269 for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
270 for (protect_hfs = 0; protect_hfs < 2; protect_hfs++) {
273 for (i = 0; i < repetitions; i++) {
274 begin = getnanotime();
275 for (j = 0; j < nr; j++)
276 verify_path(names[j], file_mode);
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);
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);
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]);
294 int cmd__path_utils(int argc, const char **argv)
296 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
297 char *buf = xmallocz(strlen(argv[2]));
298 int rv = normalize_path_copy(buf, argv[2]);
305 if (argc >= 2 && !strcmp(argv[1], "real_path")) {
306 struct strbuf realpath = STRBUF_INIT;
308 strbuf_realpath(&realpath, argv[2], 1);
313 strbuf_release(&realpath);
317 if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
319 puts(absolute_path(argv[2]));
326 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
328 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
329 char *path = xstrdup(argv[2]);
332 * We have to normalize the arguments because under
333 * Windows, bash mangles arguments that look like
334 * absolute POSIX paths or colon-separate lists of
335 * absolute POSIX paths into DOS paths (e.g.,
336 * "/foo:/foo/bar" might be converted to
337 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
338 * whereas longest_ancestor_length() requires paths
339 * that use forward slashes.
341 if (normalize_path_copy(path, path))
342 die("Path \"%s\" could not be normalized", argv[2]);
343 string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
344 filter_string_list(&ceiling_dirs, 0,
345 normalize_ceiling_entry, NULL);
346 len = longest_ancestor_length(path, &ceiling_dirs);
347 string_list_clear(&ceiling_dirs, 0);
353 if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
354 const char *prefix = argv[2];
355 int prefix_len = strlen(prefix);
357 setup_git_directory_gently(&nongit_ok);
359 puts(prefix_path(prefix, prefix_len, argv[3]));
366 if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
367 char *prefix = strip_path_suffix(argv[2], argv[3]);
368 printf("%s\n", prefix ? prefix : "(null)");
372 if (argc == 3 && !strcmp(argv[1], "print_path")) {
377 if (argc == 4 && !strcmp(argv[1], "relative_path")) {
378 struct strbuf sb = STRBUF_INIT;
379 const char *in, *prefix, *rel;
380 normalize_argv_string(&in, argv[2]);
381 normalize_argv_string(&prefix, argv[3]);
382 rel = relative_path(in, prefix, &sb);
386 puts(strlen(rel) > 0 ? rel : "(empty)");
391 if (argc == 2 && !strcmp(argv[1], "basename"))
392 return test_function(basename_data, posix_basename, argv[1]);
394 if (argc == 2 && !strcmp(argv[1], "dirname"))
395 return test_function(dirname_data, posix_dirname, argv[1]);
397 if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) {
398 return check_dotfile("modules", argv + 2,
399 is_hfs_dotgitmodules,
400 is_ntfs_dotgitmodules);
402 if (argc > 2 && !strcmp(argv[1], "is_dotgitignore")) {
403 return check_dotfile("ignore", argv + 2,
405 is_ntfs_dotgitignore);
407 if (argc > 2 && !strcmp(argv[1], "is_dotgitattributes")) {
408 return check_dotfile("attributes", argv + 2,
409 is_hfs_dotgitattributes,
410 is_ntfs_dotgitattributes);
412 if (argc > 2 && !strcmp(argv[1], "is_dotmailmap")) {
413 return check_dotfile("mailmap", argv + 2,
418 if (argc > 2 && !strcmp(argv[1], "file-size")) {
422 for (i = 2; i < argc; i++)
423 if (stat(argv[i], &st))
424 res = error_errno("Cannot stat '%s'", argv[i]);
426 printf("%"PRIuMAX"\n", (uintmax_t)st.st_size);
430 if (argc == 4 && !strcmp(argv[1], "skip-n-bytes")) {
431 int fd = open(argv[2], O_RDONLY), offset = atoi(argv[3]);
435 die_errno("could not open '%s'", argv[2]);
436 if (lseek(fd, offset, SEEK_SET) < 0)
437 die_errno("could not skip %d bytes", offset);
439 ssize_t count = read(fd, buffer, sizeof(buffer));
441 die_errno("could not read '%s'", argv[2]);
444 if (write(1, buffer, count) < 0)
445 die_errno("could not write to stdout");
451 if (argc > 5 && !strcmp(argv[1], "slice-tests")) {
453 long offset, stride, i;
454 struct string_list list = STRING_LIST_INIT_NODUP;
457 offset = strtol(argv[2], NULL, 10);
458 stride = strtol(argv[3], NULL, 10);
461 for (i = 4; i < argc; i++)
462 if (stat(argv[i], &st))
463 res = error_errno("Cannot stat '%s'", argv[i]);
465 string_list_append(&list, argv[i])->util =
466 (void *)(intptr_t)st.st_size;
467 QSORT(list.items, list.nr, cmp_by_st_size);
468 for (i = offset; i < list.nr; i+= stride)
469 printf("%s\n", list.items[i].string);
474 if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
475 return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
477 if (argc > 1 && !strcmp(argv[1], "is_valid_path")) {
478 int res = 0, expect = 1, i;
480 for (i = 2; i < argc; i++)
481 if (!strcmp("--not", argv[i]))
483 else if (expect != is_valid_path(argv[i]))
484 res = error("'%s' is%s a valid path",
485 argv[i], expect ? " not" : "");
488 "'%s' is%s a valid path\n",
489 argv[i], expect ? "" : " not");
494 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
495 argv[1] ? argv[1] : "(there was none)");