]>
Commit | Line | Data |
---|---|---|
ae299be0 | 1 | #include "cache.h" |
31171d9e | 2 | #include "string-list.h" |
dc2d9ba3 | 3 | #include "utf8.h" |
ae299be0 | 4 | |
9e2326c7 MH |
5 | /* |
6 | * A "string_list_each_func_t" function that normalizes an entry from | |
7 | * GIT_CEILING_DIRECTORIES. If the path is unusable for some reason, | |
8 | * die with an explanation. | |
9 | */ | |
10 | static int normalize_ceiling_entry(struct string_list_item *item, void *unused) | |
11 | { | |
62f17513 | 12 | char *ceil = item->string; |
9e2326c7 | 13 | |
62f17513 | 14 | if (!*ceil) |
9e2326c7 | 15 | die("Empty path is not supported"); |
9e2326c7 MH |
16 | if (!is_absolute_path(ceil)) |
17 | die("Path \"%s\" is not absolute", ceil); | |
62f17513 | 18 | if (normalize_path_copy(ceil, ceil) < 0) |
9e2326c7 | 19 | die("Path \"%s\" could not be normalized", ceil); |
9e2326c7 MH |
20 | return 1; |
21 | } | |
22 | ||
203439b2 JX |
23 | static void normalize_argv_string(const char **var, const char *input) |
24 | { | |
25 | if (!strcmp(input, "<null>")) | |
26 | *var = NULL; | |
27 | else if (!strcmp(input, "<empty>")) | |
28 | *var = ""; | |
29 | else | |
30 | *var = input; | |
31 | ||
32 | if (*var && (**var == '<' || **var == '(')) | |
33 | die("Bad value: %s\n", input); | |
34 | } | |
35 | ||
7d1aaa68 JS |
36 | struct test_data { |
37 | const char *from; /* input: transform from this ... */ | |
38 | const char *to; /* output: ... to this. */ | |
371471ce | 39 | const char *alternative; /* output: ... or this. */ |
7d1aaa68 JS |
40 | }; |
41 | ||
42 | static int test_function(struct test_data *data, char *(*func)(char *input), | |
43 | const char *funcname) | |
44 | { | |
45 | int failed = 0, i; | |
46 | char buffer[1024]; | |
47 | char *to; | |
48 | ||
49 | for (i = 0; data[i].to; i++) { | |
50 | if (!data[i].from) | |
51 | to = func(NULL); | |
52 | else { | |
7b11a18a | 53 | xsnprintf(buffer, sizeof(buffer), "%s", data[i].from); |
7d1aaa68 JS |
54 | to = func(buffer); |
55 | } | |
371471ce JS |
56 | if (!strcmp(to, data[i].to)) |
57 | continue; | |
58 | if (!data[i].alternative) | |
7d1aaa68 JS |
59 | error("FAIL: %s(%s) => '%s' != '%s'\n", |
60 | funcname, data[i].from, to, data[i].to); | |
371471ce JS |
61 | else if (!strcmp(to, data[i].alternative)) |
62 | continue; | |
63 | else | |
64 | error("FAIL: %s(%s) => '%s' != '%s', '%s'\n", | |
65 | funcname, data[i].from, to, data[i].to, | |
66 | data[i].alternative); | |
67 | failed = 1; | |
7d1aaa68 JS |
68 | } |
69 | return failed; | |
70 | } | |
71 | ||
72 | static struct test_data basename_data[] = { | |
73 | /* --- POSIX type paths --- */ | |
74 | { NULL, "." }, | |
75 | { "", "." }, | |
76 | { ".", "." }, | |
77 | { "..", ".." }, | |
78 | { "/", "/" }, | |
371471ce JS |
79 | { "//", "/", "//" }, |
80 | { "///", "/", "//" }, | |
81 | { "////", "/", "//" }, | |
7d1aaa68 JS |
82 | { "usr", "usr" }, |
83 | { "/usr", "usr" }, | |
84 | { "/usr/", "usr" }, | |
85 | { "/usr//", "usr" }, | |
86 | { "/usr/lib", "lib" }, | |
87 | { "usr/lib", "lib" }, | |
88 | { "usr/lib///", "lib" }, | |
89 | ||
90 | #if defined(__MINGW32__) || defined(_MSC_VER) | |
7d1aaa68 JS |
91 | /* --- win32 type paths --- */ |
92 | { "\\usr", "usr" }, | |
93 | { "\\usr\\", "usr" }, | |
94 | { "\\usr\\\\", "usr" }, | |
95 | { "\\usr\\lib", "lib" }, | |
96 | { "usr\\lib", "lib" }, | |
97 | { "usr\\lib\\\\\\", "lib" }, | |
98 | { "C:/usr", "usr" }, | |
99 | { "C:/usr", "usr" }, | |
100 | { "C:/usr/", "usr" }, | |
101 | { "C:/usr//", "usr" }, | |
102 | { "C:/usr/lib", "lib" }, | |
103 | { "C:usr/lib", "lib" }, | |
104 | { "C:usr/lib///", "lib" }, | |
105 | { "C:", "." }, | |
106 | { "C:a", "a" }, | |
107 | { "C:/", "/" }, | |
108 | { "C:///", "/" }, | |
371471ce JS |
109 | { "\\", "\\", "/" }, |
110 | { "\\\\", "\\", "/" }, | |
111 | { "\\\\\\", "\\", "/" }, | |
7d1aaa68 JS |
112 | #endif |
113 | { NULL, NULL } | |
114 | }; | |
115 | ||
116 | static struct test_data dirname_data[] = { | |
117 | /* --- POSIX type paths --- */ | |
118 | { NULL, "." }, | |
119 | { "", "." }, | |
120 | { ".", "." }, | |
121 | { "..", "." }, | |
122 | { "/", "/" }, | |
371471ce JS |
123 | { "//", "/", "//" }, |
124 | { "///", "/", "//" }, | |
125 | { "////", "/", "//" }, | |
7d1aaa68 JS |
126 | { "usr", "." }, |
127 | { "/usr", "/" }, | |
128 | { "/usr/", "/" }, | |
129 | { "/usr//", "/" }, | |
130 | { "/usr/lib", "/usr" }, | |
131 | { "usr/lib", "usr" }, | |
132 | { "usr/lib///", "usr" }, | |
133 | ||
134 | #if defined(__MINGW32__) || defined(_MSC_VER) | |
7d1aaa68 JS |
135 | /* --- win32 type paths --- */ |
136 | { "\\", "\\" }, | |
137 | { "\\\\", "\\\\" }, | |
138 | { "\\usr", "\\" }, | |
139 | { "\\usr\\", "\\" }, | |
140 | { "\\usr\\\\", "\\" }, | |
141 | { "\\usr\\lib", "\\usr" }, | |
142 | { "usr\\lib", "usr" }, | |
143 | { "usr\\lib\\\\\\", "usr" }, | |
144 | { "C:a", "C:." }, | |
145 | { "C:/", "C:/" }, | |
146 | { "C:///", "C:/" }, | |
147 | { "C:/usr", "C:/" }, | |
148 | { "C:/usr/", "C:/" }, | |
149 | { "C:/usr//", "C:/" }, | |
150 | { "C:/usr/lib", "C:/usr" }, | |
151 | { "C:usr/lib", "C:usr" }, | |
152 | { "C:usr/lib///", "C:usr" }, | |
153 | { "\\\\\\", "\\" }, | |
154 | { "\\\\\\\\", "\\" }, | |
371471ce | 155 | { "C:", "C:.", "." }, |
7d1aaa68 JS |
156 | #endif |
157 | { NULL, NULL } | |
158 | }; | |
159 | ||
dc2d9ba3 JS |
160 | static int is_dotgitmodules(const char *path) |
161 | { | |
162 | return is_hfs_dotgitmodules(path) || is_ntfs_dotgitmodules(path); | |
163 | } | |
164 | ||
3f2e2297 | 165 | int cmd_main(int argc, const char **argv) |
ae299be0 | 166 | { |
f42302b4 | 167 | if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) { |
62f17513 | 168 | char *buf = xmallocz(strlen(argv[2])); |
f42302b4 JS |
169 | int rv = normalize_path_copy(buf, argv[2]); |
170 | if (rv) | |
171 | buf = "++failed++"; | |
ae299be0 | 172 | puts(buf); |
2cd85c40 | 173 | return 0; |
ae299be0 DR |
174 | } |
175 | ||
e2a57aac | 176 | if (argc >= 2 && !strcmp(argv[1], "real_path")) { |
d553e737 | 177 | while (argc > 2) { |
e2a57aac | 178 | puts(real_path(argv[2])); |
d553e737 DR |
179 | argc--; |
180 | argv++; | |
181 | } | |
2cd85c40 | 182 | return 0; |
d553e737 DR |
183 | } |
184 | ||
87a246e1 MH |
185 | if (argc >= 2 && !strcmp(argv[1], "absolute_path")) { |
186 | while (argc > 2) { | |
187 | puts(absolute_path(argv[2])); | |
188 | argc--; | |
189 | argv++; | |
190 | } | |
191 | return 0; | |
192 | } | |
193 | ||
0454dd93 | 194 | if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) { |
31171d9e MH |
195 | int len; |
196 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; | |
9e2326c7 | 197 | char *path = xstrdup(argv[2]); |
31171d9e | 198 | |
9e2326c7 MH |
199 | /* |
200 | * We have to normalize the arguments because under | |
201 | * Windows, bash mangles arguments that look like | |
202 | * absolute POSIX paths or colon-separate lists of | |
203 | * absolute POSIX paths into DOS paths (e.g., | |
204 | * "/foo:/foo/bar" might be converted to | |
205 | * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"), | |
206 | * whereas longest_ancestor_length() requires paths | |
207 | * that use forward slashes. | |
208 | */ | |
209 | if (normalize_path_copy(path, path)) | |
210 | die("Path \"%s\" could not be normalized", argv[2]); | |
31171d9e | 211 | string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1); |
9e2326c7 MH |
212 | filter_string_list(&ceiling_dirs, 0, |
213 | normalize_ceiling_entry, NULL); | |
214 | len = longest_ancestor_length(path, &ceiling_dirs); | |
31171d9e | 215 | string_list_clear(&ceiling_dirs, 0); |
9e2326c7 | 216 | free(path); |
0454dd93 | 217 | printf("%d\n", len); |
2cd85c40 | 218 | return 0; |
0454dd93 DR |
219 | } |
220 | ||
9e813723 | 221 | if (argc >= 4 && !strcmp(argv[1], "prefix_path")) { |
3f2e2297 | 222 | const char *prefix = argv[2]; |
9e813723 MH |
223 | int prefix_len = strlen(prefix); |
224 | int nongit_ok; | |
225 | setup_git_directory_gently(&nongit_ok); | |
226 | while (argc > 3) { | |
227 | puts(prefix_path(prefix, prefix_len, argv[3])); | |
228 | argc--; | |
229 | argv++; | |
230 | } | |
231 | return 0; | |
232 | } | |
233 | ||
4fcc86b0 JS |
234 | if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) { |
235 | char *prefix = strip_path_suffix(argv[2], argv[3]); | |
236 | printf("%s\n", prefix ? prefix : "(null)"); | |
237 | return 0; | |
238 | } | |
239 | ||
7ffd18fc | 240 | if (argc == 3 && !strcmp(argv[1], "print_path")) { |
abd4284b JX |
241 | puts(argv[2]); |
242 | return 0; | |
243 | } | |
244 | ||
203439b2 | 245 | if (argc == 4 && !strcmp(argv[1], "relative_path")) { |
e02ca72f | 246 | struct strbuf sb = STRBUF_INIT; |
203439b2 JX |
247 | const char *in, *prefix, *rel; |
248 | normalize_argv_string(&in, argv[2]); | |
249 | normalize_argv_string(&prefix, argv[3]); | |
e02ca72f | 250 | rel = relative_path(in, prefix, &sb); |
203439b2 JX |
251 | if (!rel) |
252 | puts("(null)"); | |
253 | else | |
254 | puts(strlen(rel) > 0 ? rel : "(empty)"); | |
e02ca72f | 255 | strbuf_release(&sb); |
203439b2 JX |
256 | return 0; |
257 | } | |
258 | ||
7d1aaa68 JS |
259 | if (argc == 2 && !strcmp(argv[1], "basename")) |
260 | return test_function(basename_data, basename, argv[1]); | |
261 | ||
262 | if (argc == 2 && !strcmp(argv[1], "dirname")) | |
263 | return test_function(dirname_data, dirname, argv[1]); | |
264 | ||
dc2d9ba3 JS |
265 | if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) { |
266 | int res = 0, expect = 1, i; | |
267 | for (i = 2; i < argc; i++) | |
268 | if (!strcmp("--not", argv[i])) | |
269 | expect = !expect; | |
270 | else if (expect != is_dotgitmodules(argv[i])) | |
271 | res = error("'%s' is %s.gitmodules", argv[i], | |
272 | expect ? "not " : ""); | |
273 | else | |
274 | fprintf(stderr, "ok: '%s' is %s.gitmodules\n", | |
275 | argv[i], expect ? "" : "not "); | |
276 | return !!res; | |
277 | } | |
278 | ||
2cd85c40 JS |
279 | fprintf(stderr, "%s: unknown function name: %s\n", argv[0], |
280 | argv[1] ? argv[1] : "(there was none)"); | |
281 | return 1; | |
ae299be0 | 282 | } |