|
1 | 1 | #include "cache.h" |
2 | 2 | #include "string-list.h" |
3 | 3 |
|
| 4 | +/* |
| 5 | + * A "string_list_each_func_t" function that normalizes an entry from |
| 6 | + * GIT_CEILING_DIRECTORIES. If the path is unusable for some reason, |
| 7 | + * die with an explanation. |
| 8 | + */ |
| 9 | +static int normalize_ceiling_entry(struct string_list_item *item, void *unused) |
| 10 | +{ |
| 11 | + const char *ceil = item->string; |
| 12 | + int len = strlen(ceil); |
| 13 | + char buf[PATH_MAX+1]; |
| 14 | + |
| 15 | + if (len == 0) |
| 16 | + die("Empty path is not supported"); |
| 17 | + if (len > PATH_MAX) |
| 18 | + die("Path \"%s\" is too long", ceil); |
| 19 | + if (!is_absolute_path(ceil)) |
| 20 | + die("Path \"%s\" is not absolute", ceil); |
| 21 | + if (normalize_path_copy(buf, ceil) < 0) |
| 22 | + die("Path \"%s\" could not be normalized", ceil); |
| 23 | + len = strlen(buf); |
| 24 | + if (len > 1 && buf[len-1] == '/') |
| 25 | + die("Normalized path \"%s\" ended with slash", buf); |
| 26 | + free(item->string); |
| 27 | + item->string = xstrdup(buf); |
| 28 | + return 1; |
| 29 | +} |
| 30 | + |
4 | 31 | int main(int argc, char **argv) |
5 | 32 | { |
6 | 33 | if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) { |
@@ -33,10 +60,26 @@ int main(int argc, char **argv) |
33 | 60 | if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) { |
34 | 61 | int len; |
35 | 62 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; |
| 63 | + char *path = xstrdup(argv[2]); |
36 | 64 |
|
| 65 | + /* |
| 66 | + * We have to normalize the arguments because under |
| 67 | + * Windows, bash mangles arguments that look like |
| 68 | + * absolute POSIX paths or colon-separate lists of |
| 69 | + * absolute POSIX paths into DOS paths (e.g., |
| 70 | + * "/foo:/foo/bar" might be converted to |
| 71 | + * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"), |
| 72 | + * whereas longest_ancestor_length() requires paths |
| 73 | + * that use forward slashes. |
| 74 | + */ |
| 75 | + if (normalize_path_copy(path, path)) |
| 76 | + die("Path \"%s\" could not be normalized", argv[2]); |
37 | 77 | string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1); |
38 | | - len = longest_ancestor_length(argv[2], &ceiling_dirs); |
| 78 | + filter_string_list(&ceiling_dirs, 0, |
| 79 | + normalize_ceiling_entry, NULL); |
| 80 | + len = longest_ancestor_length(path, &ceiling_dirs); |
39 | 81 | string_list_clear(&ceiling_dirs, 0); |
| 82 | + free(path); |
40 | 83 | printf("%d\n", len); |
41 | 84 | return 0; |
42 | 85 | } |
|
0 commit comments