David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 2 | #include "string-list.h" |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 3 | |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 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 | |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 31 | int main(int argc, char **argv) |
| 32 | { |
Johannes Sixt | f42302b | 2009-02-07 16:08:30 +0100 | [diff] [blame] | 33 | if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) { |
Johannes Schindelin | b8469ad | 2009-01-28 00:07:36 +0100 | [diff] [blame] | 34 | char *buf = xmalloc(PATH_MAX + 1); |
Johannes Sixt | f42302b | 2009-02-07 16:08:30 +0100 | [diff] [blame] | 35 | int rv = normalize_path_copy(buf, argv[2]); |
| 36 | if (rv) |
| 37 | buf = "++failed++"; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 38 | puts(buf); |
Johannes Sixt | 2cd85c4 | 2009-02-07 16:08:27 +0100 | [diff] [blame] | 39 | return 0; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Carlos Martín Nieto | e2a57aa | 2011-03-17 12:26:46 +0100 | [diff] [blame] | 42 | if (argc >= 2 && !strcmp(argv[1], "real_path")) { |
David Reiss | d553e73 | 2008-05-19 23:49:00 -0700 | [diff] [blame] | 43 | while (argc > 2) { |
Carlos Martín Nieto | e2a57aa | 2011-03-17 12:26:46 +0100 | [diff] [blame] | 44 | puts(real_path(argv[2])); |
David Reiss | d553e73 | 2008-05-19 23:49:00 -0700 | [diff] [blame] | 45 | argc--; |
| 46 | argv++; |
| 47 | } |
Johannes Sixt | 2cd85c4 | 2009-02-07 16:08:27 +0100 | [diff] [blame] | 48 | return 0; |
David Reiss | d553e73 | 2008-05-19 23:49:00 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Michael Haggerty | 87a246e | 2011-08-04 06:47:47 +0200 | [diff] [blame] | 51 | if (argc >= 2 && !strcmp(argv[1], "absolute_path")) { |
| 52 | while (argc > 2) { |
| 53 | puts(absolute_path(argv[2])); |
| 54 | argc--; |
| 55 | argv++; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 60 | if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) { |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 61 | int len; |
| 62 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 63 | char *path = xstrdup(argv[2]); |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 64 | |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 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]); |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 77 | string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1); |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 78 | filter_string_list(&ceiling_dirs, 0, |
| 79 | normalize_ceiling_entry, NULL); |
| 80 | len = longest_ancestor_length(path, &ceiling_dirs); |
Michael Haggerty | 31171d9 | 2012-10-28 17:16:24 +0100 | [diff] [blame] | 81 | string_list_clear(&ceiling_dirs, 0); |
Michael Haggerty | 9e2326c | 2012-10-28 17:16:25 +0100 | [diff] [blame] | 82 | free(path); |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 83 | printf("%d\n", len); |
Johannes Sixt | 2cd85c4 | 2009-02-07 16:08:27 +0100 | [diff] [blame] | 84 | return 0; |
David Reiss | 0454dd9 | 2008-05-19 23:49:26 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Michael Haggerty | 9e81372 | 2011-08-04 06:47:48 +0200 | [diff] [blame] | 87 | if (argc >= 4 && !strcmp(argv[1], "prefix_path")) { |
| 88 | char *prefix = argv[2]; |
| 89 | int prefix_len = strlen(prefix); |
| 90 | int nongit_ok; |
| 91 | setup_git_directory_gently(&nongit_ok); |
| 92 | while (argc > 3) { |
| 93 | puts(prefix_path(prefix, prefix_len, argv[3])); |
| 94 | argc--; |
| 95 | argv++; |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
Johannes Schindelin | 4fcc86b | 2009-02-19 20:10:49 +0100 | [diff] [blame] | 100 | if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) { |
| 101 | char *prefix = strip_path_suffix(argv[2], argv[3]); |
| 102 | printf("%s\n", prefix ? prefix : "(null)"); |
| 103 | return 0; |
| 104 | } |
| 105 | |
Johannes Sixt | 2cd85c4 | 2009-02-07 16:08:27 +0100 | [diff] [blame] | 106 | fprintf(stderr, "%s: unknown function name: %s\n", argv[0], |
| 107 | argv[1] ? argv[1] : "(there was none)"); |
| 108 | return 1; |
David Reiss | ae299be | 2008-05-19 23:48:54 -0700 | [diff] [blame] | 109 | } |