4 #include "environment.h"
6 #include "string-list.h"
10 * A "string_list_each_func_t" function that normalizes an entry from
11 * GIT_CEILING_DIRECTORIES. If the path is unusable for some reason,
12 * die with an explanation.
14 static int normalize_ceiling_entry(struct string_list_item
*item
,
17 char *ceil
= item
->string
;
20 die("Empty path is not supported");
21 if (!is_absolute_path(ceil
))
22 die("Path \"%s\" is not absolute", ceil
);
23 if (normalize_path_copy(ceil
, ceil
) < 0)
24 die("Path \"%s\" could not be normalized", ceil
);
28 static void normalize_argv_string(const char **var
, const char *input
)
30 if (!strcmp(input
, "<null>"))
32 else if (!strcmp(input
, "<empty>"))
37 if (*var
&& (**var
== '<' || **var
== '('))
38 die("Bad value: %s\n", input
);
42 const char *from
; /* input: transform from this ... */
43 const char *to
; /* output: ... to this. */
44 const char *alternative
; /* output: ... or this. */
48 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
49 * have const parameters.
51 static char *posix_basename(char *path
)
53 return basename(path
);
56 static char *posix_dirname(char *path
)
61 static int test_function(struct test_data
*data
, char *(*func
)(char *input
),
68 for (i
= 0; data
[i
].to
; i
++) {
72 xsnprintf(buffer
, sizeof(buffer
), "%s", data
[i
].from
);
75 if (!strcmp(to
, data
[i
].to
))
77 if (!data
[i
].alternative
)
78 error("FAIL: %s(%s) => '%s' != '%s'\n",
79 funcname
, data
[i
].from
, to
, data
[i
].to
);
80 else if (!strcmp(to
, data
[i
].alternative
))
83 error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
84 funcname
, data
[i
].from
, to
, data
[i
].to
,
91 static struct test_data basename_data
[] = {
92 /* --- POSIX type paths --- */
100 { "////", "/", "//" },
105 { "/usr/lib", "lib" },
106 { "usr/lib", "lib" },
107 { "usr/lib///", "lib" },
109 #if defined(__MINGW32__) || defined(_MSC_VER)
110 /* --- win32 type paths --- */
112 { "\\usr\\", "usr" },
113 { "\\usr\\\\", "usr" },
114 { "\\usr\\lib", "lib" },
115 { "usr\\lib", "lib" },
116 { "usr\\lib\\\\\\", "lib" },
119 { "C:/usr/", "usr" },
120 { "C:/usr//", "usr" },
121 { "C:/usr/lib", "lib" },
122 { "C:usr/lib", "lib" },
123 { "C:usr/lib///", "lib" },
129 { "\\\\", "\\", "/" },
130 { "\\\\\\", "\\", "/" },
135 static struct test_data dirname_data
[] = {
136 /* --- POSIX type paths --- */
143 { "///", "/", "//" },
144 { "////", "/", "//" },
149 { "/usr/lib", "/usr" },
150 { "usr/lib", "usr" },
151 { "usr/lib///", "usr" },
153 #if defined(__MINGW32__) || defined(_MSC_VER)
154 /* --- win32 type paths --- */
159 { "\\usr\\\\", "\\" },
160 { "\\usr\\lib", "\\usr" },
161 { "usr\\lib", "usr" },
162 { "usr\\lib\\\\\\", "usr" },
167 { "C:/usr/", "C:/" },
168 { "C:/usr//", "C:/" },
169 { "C:/usr/lib", "C:/usr" },
170 { "C:usr/lib", "C:usr" },
171 { "C:usr/lib///", "C:usr" },
173 { "\\\\\\\\", "\\" },
174 { "C:", "C:.", "." },
179 static int check_dotfile(const char *x
, const char **argv
,
180 int (*is_hfs
)(const char *),
181 int (*is_ntfs
)(const char *))
183 int res
= 0, expect
= 1;
184 for (; *argv
; argv
++) {
185 if (!strcmp("--not", *argv
))
187 else if (expect
!= (is_hfs(*argv
) || is_ntfs(*argv
)))
188 res
= error("'%s' is %s.git%s", *argv
,
189 expect
? "not " : "", x
);
191 fprintf(stderr
, "ok: '%s' is %s.git%s\n",
192 *argv
, expect
? "" : "not ", x
);
197 static int cmp_by_st_size(const void *a
, const void *b
)
199 intptr_t x
= (intptr_t)((struct string_list_item
*)a
)->util
;
200 intptr_t y
= (intptr_t)((struct string_list_item
*)b
)->util
;
202 return x
> y
? -1 : (x
< y
? +1 : 0);
206 * A very simple, reproducible pseudo-random generator. Copied from
207 * `test-genrandom.c`.
209 static uint64_t my_random_value
= 1234;
211 static uint64_t my_random(void)
213 my_random_value
= my_random_value
* 1103515245 + 12345;
214 return my_random_value
;
218 * A fast approximation of the square root, without requiring math.h.
220 * It uses Newton's method to approximate the solution of 0 = x^2 - value.
222 static double my_sqrt(double value
)
224 const double epsilon
= 1e-6;
231 double delta
= (value
/ x
- x
) / 2;
232 if (delta
< epsilon
&& delta
> -epsilon
)
238 static int protect_ntfs_hfs_benchmark(int argc
, const char **argv
)
240 size_t i
, j
, nr
, min_len
= 3, max_len
= 20;
242 int repetitions
= 15, file_mode
= 0100644;
244 double m
[3][2], v
[3][2];
248 if (argc
> 1 && !strcmp(argv
[1], "--with-symlink-mode")) {
254 nr
= argc
> 1 ? strtoul(argv
[1], NULL
, 0) : 1000000;
255 ALLOC_ARRAY(names
, nr
);
258 min_len
= strtoul(argv
[2], NULL
, 0);
260 max_len
= strtoul(argv
[3], NULL
, 0);
261 if (min_len
> max_len
)
262 die("min_len > max_len");
265 for (i
= 0; i
< nr
; i
++) {
266 size_t len
= min_len
+ (my_random() % (max_len
+ 1 - min_len
));
268 names
[i
] = xmallocz(len
);
270 names
[i
][--len
] = (char)(' ' + (my_random() % ('\x7f' - ' ')));
273 for (protect_ntfs
= 0; protect_ntfs
< 2; protect_ntfs
++)
274 for (protect_hfs
= 0; protect_hfs
< 2; protect_hfs
++) {
277 for (i
= 0; i
< repetitions
; i
++) {
278 begin
= getnanotime();
279 for (j
= 0; j
< nr
; j
++)
280 verify_path(names
[j
], file_mode
);
282 printf("protect_ntfs = %d, protect_hfs = %d: %lfms\n", protect_ntfs
, protect_hfs
, (end
-begin
) / (double)1e6
);
283 cumul
+= end
- begin
;
284 cumul2
+= (end
- begin
) * (end
- begin
);
286 m
[protect_ntfs
][protect_hfs
] = cumul
/ (double)repetitions
;
287 v
[protect_ntfs
][protect_hfs
] = my_sqrt(cumul2
/ (double)repetitions
- m
[protect_ntfs
][protect_hfs
] * m
[protect_ntfs
][protect_hfs
]);
288 printf("mean: %lfms, stddev: %lfms\n", m
[protect_ntfs
][protect_hfs
] / (double)1e6
, v
[protect_ntfs
][protect_hfs
] / (double)1e6
);
291 for (protect_ntfs
= 0; protect_ntfs
< 2; protect_ntfs
++)
292 for (protect_hfs
= 0; protect_hfs
< 2; protect_hfs
++)
293 printf("ntfs=%d/hfs=%d: %lf%% slower\n", protect_ntfs
, protect_hfs
, (m
[protect_ntfs
][protect_hfs
] - m
[0][0]) * 100 / m
[0][0]);
298 int cmd__path_utils(int argc
, const char **argv
)
300 if (argc
== 3 && !strcmp(argv
[1], "normalize_path_copy")) {
301 char *buf
= xmallocz(strlen(argv
[2]));
302 int rv
= normalize_path_copy(buf
, argv
[2]);
303 puts(rv
? "++failed++" : buf
);
308 if (argc
>= 2 && !strcmp(argv
[1], "real_path")) {
309 struct strbuf realpath
= STRBUF_INIT
;
311 strbuf_realpath(&realpath
, argv
[2], 1);
316 strbuf_release(&realpath
);
320 if (argc
>= 2 && !strcmp(argv
[1], "absolute_path")) {
322 puts(absolute_path(argv
[2]));
329 if (argc
== 4 && !strcmp(argv
[1], "longest_ancestor_length")) {
331 struct string_list ceiling_dirs
= STRING_LIST_INIT_DUP
;
332 char *path
= xstrdup(argv
[2]);
335 * We have to normalize the arguments because under
336 * Windows, bash mangles arguments that look like
337 * absolute POSIX paths or colon-separate lists of
338 * absolute POSIX paths into DOS paths (e.g.,
339 * "/foo:/foo/bar" might be converted to
340 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
341 * whereas longest_ancestor_length() requires paths
342 * that use forward slashes.
344 if (normalize_path_copy(path
, path
))
345 die("Path \"%s\" could not be normalized", argv
[2]);
346 string_list_split(&ceiling_dirs
, argv
[3], PATH_SEP
, -1);
347 filter_string_list(&ceiling_dirs
, 0,
348 normalize_ceiling_entry
, NULL
);
349 len
= longest_ancestor_length(path
, &ceiling_dirs
);
350 string_list_clear(&ceiling_dirs
, 0);
356 if (argc
>= 4 && !strcmp(argv
[1], "prefix_path")) {
357 const char *prefix
= argv
[2];
358 int prefix_len
= strlen(prefix
);
360 setup_git_directory_gently(&nongit_ok
);
362 char *pfx
= prefix_path(prefix
, prefix_len
, argv
[3]);
372 if (argc
== 4 && !strcmp(argv
[1], "strip_path_suffix")) {
373 char *prefix
= strip_path_suffix(argv
[2], argv
[3]);
374 printf("%s\n", prefix
? prefix
: "(null)");
379 if (argc
== 3 && !strcmp(argv
[1], "print_path")) {
384 if (argc
== 4 && !strcmp(argv
[1], "relative_path")) {
385 struct strbuf sb
= STRBUF_INIT
;
386 const char *in
, *prefix
, *rel
;
387 normalize_argv_string(&in
, argv
[2]);
388 normalize_argv_string(&prefix
, argv
[3]);
389 rel
= relative_path(in
, prefix
, &sb
);
393 puts(strlen(rel
) > 0 ? rel
: "(empty)");
398 if (argc
== 2 && !strcmp(argv
[1], "basename"))
399 return test_function(basename_data
, posix_basename
, argv
[1]);
401 if (argc
== 2 && !strcmp(argv
[1], "dirname"))
402 return test_function(dirname_data
, posix_dirname
, argv
[1]);
404 if (argc
> 2 && !strcmp(argv
[1], "is_dotgitmodules")) {
405 return check_dotfile("modules", argv
+ 2,
406 is_hfs_dotgitmodules
,
407 is_ntfs_dotgitmodules
);
409 if (argc
> 2 && !strcmp(argv
[1], "is_dotgitignore")) {
410 return check_dotfile("ignore", argv
+ 2,
412 is_ntfs_dotgitignore
);
414 if (argc
> 2 && !strcmp(argv
[1], "is_dotgitattributes")) {
415 return check_dotfile("attributes", argv
+ 2,
416 is_hfs_dotgitattributes
,
417 is_ntfs_dotgitattributes
);
419 if (argc
> 2 && !strcmp(argv
[1], "is_dotmailmap")) {
420 return check_dotfile("mailmap", argv
+ 2,
425 if (argc
> 2 && !strcmp(argv
[1], "file-size")) {
429 for (i
= 2; i
< argc
; i
++)
430 if (stat(argv
[i
], &st
))
431 res
= error_errno("Cannot stat '%s'", argv
[i
]);
433 printf("%"PRIuMAX
"\n", (uintmax_t)st
.st_size
);
437 if (argc
== 4 && !strcmp(argv
[1], "skip-n-bytes")) {
438 int fd
= open(argv
[2], O_RDONLY
), offset
= atoi(argv
[3]);
442 die_errno("could not open '%s'", argv
[2]);
443 if (lseek(fd
, offset
, SEEK_SET
) < 0)
444 die_errno("could not skip %d bytes", offset
);
446 ssize_t count
= read(fd
, buffer
, sizeof(buffer
));
448 die_errno("could not read '%s'", argv
[2]);
451 if (write(1, buffer
, count
) < 0)
452 die_errno("could not write to stdout");
458 if (argc
> 5 && !strcmp(argv
[1], "slice-tests")) {
460 long offset
, stride
, i
;
461 struct string_list list
= STRING_LIST_INIT_NODUP
;
464 offset
= strtol(argv
[2], NULL
, 10);
465 stride
= strtol(argv
[3], NULL
, 10);
468 for (i
= 4; i
< argc
; i
++)
469 if (stat(argv
[i
], &st
))
470 res
= error_errno("Cannot stat '%s'", argv
[i
]);
472 string_list_append(&list
, argv
[i
])->util
=
473 (void *)(intptr_t)st
.st_size
;
474 QSORT(list
.items
, list
.nr
, cmp_by_st_size
);
475 for (i
= offset
; i
< list
.nr
; i
+= stride
)
476 printf("%s\n", list
.items
[i
].string
);
481 if (argc
> 1 && !strcmp(argv
[1], "protect_ntfs_hfs"))
482 return !!protect_ntfs_hfs_benchmark(argc
- 1, argv
+ 1);
484 if (argc
> 1 && !strcmp(argv
[1], "is_valid_path")) {
485 int res
= 0, expect
= 1, i
;
487 for (i
= 2; i
< argc
; i
++)
488 if (!strcmp("--not", argv
[i
]))
490 else if (expect
!= is_valid_path(argv
[i
]))
491 res
= error("'%s' is%s a valid path",
492 argv
[i
], expect
? " not" : "");
495 "'%s' is%s a valid path\n",
496 argv
[i
], expect
? "" : " not");
501 fprintf(stderr
, "%s: unknown function name: %s\n", argv
[0],
502 argv
[1] ? argv
[1] : "(there was none)");