]>
Commit | Line | Data |
---|---|---|
b8d5cf4f | 1 | #include "test-tool.h" |
ae299be0 | 2 | #include "cache.h" |
31171d9e | 3 | #include "string-list.h" |
dc2d9ba3 | 4 | #include "utf8.h" |
ae299be0 | 5 | |
9e2326c7 MH |
6 | /* |
7 | * A "string_list_each_func_t" function that normalizes an entry from | |
8 | * GIT_CEILING_DIRECTORIES. If the path is unusable for some reason, | |
9 | * die with an explanation. | |
10 | */ | |
11 | static int normalize_ceiling_entry(struct string_list_item *item, void *unused) | |
12 | { | |
62f17513 | 13 | char *ceil = item->string; |
9e2326c7 | 14 | |
62f17513 | 15 | if (!*ceil) |
9e2326c7 | 16 | die("Empty path is not supported"); |
9e2326c7 MH |
17 | if (!is_absolute_path(ceil)) |
18 | die("Path \"%s\" is not absolute", ceil); | |
62f17513 | 19 | if (normalize_path_copy(ceil, ceil) < 0) |
9e2326c7 | 20 | die("Path \"%s\" could not be normalized", ceil); |
9e2326c7 MH |
21 | return 1; |
22 | } | |
23 | ||
203439b2 JX |
24 | static void normalize_argv_string(const char **var, const char *input) |
25 | { | |
26 | if (!strcmp(input, "<null>")) | |
27 | *var = NULL; | |
28 | else if (!strcmp(input, "<empty>")) | |
29 | *var = ""; | |
30 | else | |
31 | *var = input; | |
32 | ||
33 | if (*var && (**var == '<' || **var == '(')) | |
34 | die("Bad value: %s\n", input); | |
35 | } | |
36 | ||
7d1aaa68 JS |
37 | struct test_data { |
38 | const char *from; /* input: transform from this ... */ | |
39 | const char *to; /* output: ... to this. */ | |
371471ce | 40 | const char *alternative; /* output: ... or this. */ |
7d1aaa68 JS |
41 | }; |
42 | ||
29c2eda8 RS |
43 | /* |
44 | * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3) | |
45 | * have const parameters. | |
46 | */ | |
47 | static char *posix_basename(char *path) | |
48 | { | |
49 | return basename(path); | |
50 | } | |
51 | ||
52 | static char *posix_dirname(char *path) | |
53 | { | |
54 | return dirname(path); | |
55 | } | |
56 | ||
7d1aaa68 JS |
57 | static int test_function(struct test_data *data, char *(*func)(char *input), |
58 | const char *funcname) | |
59 | { | |
60 | int failed = 0, i; | |
61 | char buffer[1024]; | |
62 | char *to; | |
63 | ||
64 | for (i = 0; data[i].to; i++) { | |
65 | if (!data[i].from) | |
66 | to = func(NULL); | |
67 | else { | |
7b11a18a | 68 | xsnprintf(buffer, sizeof(buffer), "%s", data[i].from); |
7d1aaa68 JS |
69 | to = func(buffer); |
70 | } | |
371471ce JS |
71 | if (!strcmp(to, data[i].to)) |
72 | continue; | |
73 | if (!data[i].alternative) | |
7d1aaa68 JS |
74 | error("FAIL: %s(%s) => '%s' != '%s'\n", |
75 | funcname, data[i].from, to, data[i].to); | |
371471ce JS |
76 | else if (!strcmp(to, data[i].alternative)) |
77 | continue; | |
78 | else | |
79 | error("FAIL: %s(%s) => '%s' != '%s', '%s'\n", | |
80 | funcname, data[i].from, to, data[i].to, | |
81 | data[i].alternative); | |
82 | failed = 1; | |
7d1aaa68 JS |
83 | } |
84 | return failed; | |
85 | } | |
86 | ||
87 | static struct test_data basename_data[] = { | |
88 | /* --- POSIX type paths --- */ | |
89 | { NULL, "." }, | |
90 | { "", "." }, | |
91 | { ".", "." }, | |
92 | { "..", ".." }, | |
93 | { "/", "/" }, | |
371471ce JS |
94 | { "//", "/", "//" }, |
95 | { "///", "/", "//" }, | |
96 | { "////", "/", "//" }, | |
7d1aaa68 JS |
97 | { "usr", "usr" }, |
98 | { "/usr", "usr" }, | |
99 | { "/usr/", "usr" }, | |
100 | { "/usr//", "usr" }, | |
101 | { "/usr/lib", "lib" }, | |
102 | { "usr/lib", "lib" }, | |
103 | { "usr/lib///", "lib" }, | |
104 | ||
105 | #if defined(__MINGW32__) || defined(_MSC_VER) | |
7d1aaa68 JS |
106 | /* --- win32 type paths --- */ |
107 | { "\\usr", "usr" }, | |
108 | { "\\usr\\", "usr" }, | |
109 | { "\\usr\\\\", "usr" }, | |
110 | { "\\usr\\lib", "lib" }, | |
111 | { "usr\\lib", "lib" }, | |
112 | { "usr\\lib\\\\\\", "lib" }, | |
113 | { "C:/usr", "usr" }, | |
114 | { "C:/usr", "usr" }, | |
115 | { "C:/usr/", "usr" }, | |
116 | { "C:/usr//", "usr" }, | |
117 | { "C:/usr/lib", "lib" }, | |
118 | { "C:usr/lib", "lib" }, | |
119 | { "C:usr/lib///", "lib" }, | |
120 | { "C:", "." }, | |
121 | { "C:a", "a" }, | |
122 | { "C:/", "/" }, | |
123 | { "C:///", "/" }, | |
371471ce JS |
124 | { "\\", "\\", "/" }, |
125 | { "\\\\", "\\", "/" }, | |
126 | { "\\\\\\", "\\", "/" }, | |
7d1aaa68 JS |
127 | #endif |
128 | { NULL, NULL } | |
129 | }; | |
130 | ||
131 | static struct test_data dirname_data[] = { | |
132 | /* --- POSIX type paths --- */ | |
133 | { NULL, "." }, | |
134 | { "", "." }, | |
135 | { ".", "." }, | |
136 | { "..", "." }, | |
137 | { "/", "/" }, | |
371471ce JS |
138 | { "//", "/", "//" }, |
139 | { "///", "/", "//" }, | |
140 | { "////", "/", "//" }, | |
7d1aaa68 JS |
141 | { "usr", "." }, |
142 | { "/usr", "/" }, | |
143 | { "/usr/", "/" }, | |
144 | { "/usr//", "/" }, | |
145 | { "/usr/lib", "/usr" }, | |
146 | { "usr/lib", "usr" }, | |
147 | { "usr/lib///", "usr" }, | |
148 | ||
149 | #if defined(__MINGW32__) || defined(_MSC_VER) | |
7d1aaa68 JS |
150 | /* --- win32 type paths --- */ |
151 | { "\\", "\\" }, | |
152 | { "\\\\", "\\\\" }, | |
153 | { "\\usr", "\\" }, | |
154 | { "\\usr\\", "\\" }, | |
155 | { "\\usr\\\\", "\\" }, | |
156 | { "\\usr\\lib", "\\usr" }, | |
157 | { "usr\\lib", "usr" }, | |
158 | { "usr\\lib\\\\\\", "usr" }, | |
159 | { "C:a", "C:." }, | |
160 | { "C:/", "C:/" }, | |
161 | { "C:///", "C:/" }, | |
162 | { "C:/usr", "C:/" }, | |
163 | { "C:/usr/", "C:/" }, | |
164 | { "C:/usr//", "C:/" }, | |
165 | { "C:/usr/lib", "C:/usr" }, | |
166 | { "C:usr/lib", "C:usr" }, | |
167 | { "C:usr/lib///", "C:usr" }, | |
168 | { "\\\\\\", "\\" }, | |
169 | { "\\\\\\\\", "\\" }, | |
371471ce | 170 | { "C:", "C:.", "." }, |
7d1aaa68 JS |
171 | #endif |
172 | { NULL, NULL } | |
173 | }; | |
174 | ||
dc2d9ba3 JS |
175 | static int is_dotgitmodules(const char *path) |
176 | { | |
177 | return is_hfs_dotgitmodules(path) || is_ntfs_dotgitmodules(path); | |
178 | } | |
179 | ||
b819f1d2 JS |
180 | static int cmp_by_st_size(const void *a, const void *b) |
181 | { | |
182 | intptr_t x = (intptr_t)((struct string_list_item *)a)->util; | |
183 | intptr_t y = (intptr_t)((struct string_list_item *)b)->util; | |
184 | ||
185 | return x > y ? -1 : (x < y ? +1 : 0); | |
186 | } | |
187 | ||
b8d5cf4f | 188 | int cmd__path_utils(int argc, const char **argv) |
ae299be0 | 189 | { |
f42302b4 | 190 | if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) { |
62f17513 | 191 | char *buf = xmallocz(strlen(argv[2])); |
f42302b4 JS |
192 | int rv = normalize_path_copy(buf, argv[2]); |
193 | if (rv) | |
194 | buf = "++failed++"; | |
ae299be0 | 195 | puts(buf); |
2cd85c40 | 196 | return 0; |
ae299be0 DR |
197 | } |
198 | ||
e2a57aac | 199 | if (argc >= 2 && !strcmp(argv[1], "real_path")) { |
d553e737 | 200 | while (argc > 2) { |
e2a57aac | 201 | puts(real_path(argv[2])); |
d553e737 DR |
202 | argc--; |
203 | argv++; | |
204 | } | |
2cd85c40 | 205 | return 0; |
d553e737 DR |
206 | } |
207 | ||
87a246e1 MH |
208 | if (argc >= 2 && !strcmp(argv[1], "absolute_path")) { |
209 | while (argc > 2) { | |
210 | puts(absolute_path(argv[2])); | |
211 | argc--; | |
212 | argv++; | |
213 | } | |
214 | return 0; | |
215 | } | |
216 | ||
0454dd93 | 217 | if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) { |
31171d9e MH |
218 | int len; |
219 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; | |
9e2326c7 | 220 | char *path = xstrdup(argv[2]); |
31171d9e | 221 | |
9e2326c7 MH |
222 | /* |
223 | * We have to normalize the arguments because under | |
224 | * Windows, bash mangles arguments that look like | |
225 | * absolute POSIX paths or colon-separate lists of | |
226 | * absolute POSIX paths into DOS paths (e.g., | |
227 | * "/foo:/foo/bar" might be converted to | |
228 | * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"), | |
229 | * whereas longest_ancestor_length() requires paths | |
230 | * that use forward slashes. | |
231 | */ | |
232 | if (normalize_path_copy(path, path)) | |
233 | die("Path \"%s\" could not be normalized", argv[2]); | |
31171d9e | 234 | string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1); |
9e2326c7 MH |
235 | filter_string_list(&ceiling_dirs, 0, |
236 | normalize_ceiling_entry, NULL); | |
237 | len = longest_ancestor_length(path, &ceiling_dirs); | |
31171d9e | 238 | string_list_clear(&ceiling_dirs, 0); |
9e2326c7 | 239 | free(path); |
0454dd93 | 240 | printf("%d\n", len); |
2cd85c40 | 241 | return 0; |
0454dd93 DR |
242 | } |
243 | ||
9e813723 | 244 | if (argc >= 4 && !strcmp(argv[1], "prefix_path")) { |
3f2e2297 | 245 | const char *prefix = argv[2]; |
9e813723 MH |
246 | int prefix_len = strlen(prefix); |
247 | int nongit_ok; | |
248 | setup_git_directory_gently(&nongit_ok); | |
249 | while (argc > 3) { | |
250 | puts(prefix_path(prefix, prefix_len, argv[3])); | |
251 | argc--; | |
252 | argv++; | |
253 | } | |
254 | return 0; | |
255 | } | |
256 | ||
4fcc86b0 JS |
257 | if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) { |
258 | char *prefix = strip_path_suffix(argv[2], argv[3]); | |
259 | printf("%s\n", prefix ? prefix : "(null)"); | |
260 | return 0; | |
261 | } | |
262 | ||
7ffd18fc | 263 | if (argc == 3 && !strcmp(argv[1], "print_path")) { |
abd4284b JX |
264 | puts(argv[2]); |
265 | return 0; | |
266 | } | |
267 | ||
203439b2 | 268 | if (argc == 4 && !strcmp(argv[1], "relative_path")) { |
e02ca72f | 269 | struct strbuf sb = STRBUF_INIT; |
203439b2 JX |
270 | const char *in, *prefix, *rel; |
271 | normalize_argv_string(&in, argv[2]); | |
272 | normalize_argv_string(&prefix, argv[3]); | |
e02ca72f | 273 | rel = relative_path(in, prefix, &sb); |
203439b2 JX |
274 | if (!rel) |
275 | puts("(null)"); | |
276 | else | |
277 | puts(strlen(rel) > 0 ? rel : "(empty)"); | |
e02ca72f | 278 | strbuf_release(&sb); |
203439b2 JX |
279 | return 0; |
280 | } | |
281 | ||
7d1aaa68 | 282 | if (argc == 2 && !strcmp(argv[1], "basename")) |
29c2eda8 | 283 | return test_function(basename_data, posix_basename, argv[1]); |
7d1aaa68 JS |
284 | |
285 | if (argc == 2 && !strcmp(argv[1], "dirname")) | |
29c2eda8 | 286 | return test_function(dirname_data, posix_dirname, argv[1]); |
7d1aaa68 | 287 | |
dc2d9ba3 JS |
288 | if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) { |
289 | int res = 0, expect = 1, i; | |
290 | for (i = 2; i < argc; i++) | |
291 | if (!strcmp("--not", argv[i])) | |
292 | expect = !expect; | |
293 | else if (expect != is_dotgitmodules(argv[i])) | |
294 | res = error("'%s' is %s.gitmodules", argv[i], | |
295 | expect ? "not " : ""); | |
296 | else | |
297 | fprintf(stderr, "ok: '%s' is %s.gitmodules\n", | |
298 | argv[i], expect ? "" : "not "); | |
299 | return !!res; | |
300 | } | |
301 | ||
5868bd86 JS |
302 | if (argc > 2 && !strcmp(argv[1], "file-size")) { |
303 | int res = 0, i; | |
304 | struct stat st; | |
305 | ||
306 | for (i = 2; i < argc; i++) | |
307 | if (stat(argv[i], &st)) | |
308 | res = error_errno("Cannot stat '%s'", argv[i]); | |
309 | else | |
310 | printf("%"PRIuMAX"\n", (uintmax_t)st.st_size); | |
311 | return !!res; | |
312 | } | |
313 | ||
af9912ef JS |
314 | if (argc == 4 && !strcmp(argv[1], "skip-n-bytes")) { |
315 | int fd = open(argv[2], O_RDONLY), offset = atoi(argv[3]); | |
316 | char buffer[65536]; | |
317 | ||
318 | if (fd < 0) | |
319 | die_errno("could not open '%s'", argv[2]); | |
320 | if (lseek(fd, offset, SEEK_SET) < 0) | |
321 | die_errno("could not skip %d bytes", offset); | |
322 | for (;;) { | |
323 | ssize_t count = read(fd, buffer, sizeof(buffer)); | |
324 | if (count < 0) | |
325 | die_errno("could not read '%s'", argv[2]); | |
326 | if (!count) | |
327 | break; | |
328 | if (write(1, buffer, count) < 0) | |
329 | die_errno("could not write to stdout"); | |
330 | } | |
331 | close(fd); | |
332 | return 0; | |
333 | } | |
334 | ||
b819f1d2 JS |
335 | if (argc > 5 && !strcmp(argv[1], "slice-tests")) { |
336 | int res = 0; | |
337 | long offset, stride, i; | |
338 | struct string_list list = STRING_LIST_INIT_NODUP; | |
339 | struct stat st; | |
340 | ||
341 | offset = strtol(argv[2], NULL, 10); | |
342 | stride = strtol(argv[3], NULL, 10); | |
343 | if (stride < 1) | |
344 | stride = 1; | |
345 | for (i = 4; i < argc; i++) | |
346 | if (stat(argv[i], &st)) | |
347 | res = error_errno("Cannot stat '%s'", argv[i]); | |
348 | else | |
349 | string_list_append(&list, argv[i])->util = | |
350 | (void *)(intptr_t)st.st_size; | |
351 | QSORT(list.items, list.nr, cmp_by_st_size); | |
352 | for (i = offset; i < list.nr; i+= stride) | |
353 | printf("%s\n", list.items[i].string); | |
354 | ||
355 | return !!res; | |
356 | } | |
357 | ||
2cd85c40 JS |
358 | fprintf(stderr, "%s: unknown function name: %s\n", argv[0], |
359 | argv[1] ? argv[1] : "(there was none)"); | |
360 | return 1; | |
ae299be0 | 361 | } |