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