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