1#include "test-tool.h"
2#include "cache.h"
3#include "string-list.h"
4#include "utf8.h"
5
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 */
11static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
12{
13 char *ceil = item->string;
14
15 if (!*ceil)
16 die("Empty path is not supported");
17 if (!is_absolute_path(ceil))
18 die("Path \"%s\" is not absolute", ceil);
19 if (normalize_path_copy(ceil, ceil) < 0)
20 die("Path \"%s\" could not be normalized", ceil);
21 return 1;
22}
23
24static 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
37struct test_data {
38 const char *from; /* input: transform from this ... */
39 const char *to; /* output: ... to this. */
40 const char *alternative; /* output: ... or this. */
41};
42
43/*
44 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
45 * have const parameters.
46 */
47static char *posix_basename(char *path)
48{
49 return basename(path);
50}
51
52static char *posix_dirname(char *path)
53{
54 return dirname(path);
55}
56
57static 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 {
68 xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
69 to = func(buffer);
70 }
71 if (!strcmp(to, data[i].to))
72 continue;
73 if (!data[i].alternative)
74 error("FAIL: %s(%s) => '%s' != '%s'\n",
75 funcname, data[i].from, to, data[i].to);
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;
83 }
84 return failed;
85}
86
87static struct test_data basename_data[] = {
88 /* --- POSIX type paths --- */
89 { NULL, "." },
90 { "", "." },
91 { ".", "." },
92 { "..", ".." },
93 { "/", "/" },
94 { "//", "/", "//" },
95 { "///", "/", "//" },
96 { "////", "/", "//" },
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)
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:///", "/" },
124 { "\\", "\\", "/" },
125 { "\\\\", "\\", "/" },
126 { "\\\\\\", "\\", "/" },
127#endif
128 { NULL, NULL }
129};
130
131static struct test_data dirname_data[] = {
132 /* --- POSIX type paths --- */
133 { NULL, "." },
134 { "", "." },
135 { ".", "." },
136 { "..", "." },
137 { "/", "/" },
138 { "//", "/", "//" },
139 { "///", "/", "//" },
140 { "////", "/", "//" },
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)
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 { "\\\\\\\\", "\\" },
170 { "C:", "C:.", "." },
171#endif
172 { NULL, NULL }
173};
174
175static int is_dotgitmodules(const char *path)
176{
177 return is_hfs_dotgitmodules(path) || is_ntfs_dotgitmodules(path);
178}
179
180int cmd__path_utils(int argc, const char **argv)
181{
182 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
183 char *buf = xmallocz(strlen(argv[2]));
184 int rv = normalize_path_copy(buf, argv[2]);
185 if (rv)
186 buf = "++failed++";
187 puts(buf);
188 return 0;
189 }
190
191 if (argc >= 2 && !strcmp(argv[1], "real_path")) {
192 while (argc > 2) {
193 puts(real_path(argv[2]));
194 argc--;
195 argv++;
196 }
197 return 0;
198 }
199
200 if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
201 while (argc > 2) {
202 puts(absolute_path(argv[2]));
203 argc--;
204 argv++;
205 }
206 return 0;
207 }
208
209 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
210 int len;
211 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
212 char *path = xstrdup(argv[2]);
213
214 /*
215 * We have to normalize the arguments because under
216 * Windows, bash mangles arguments that look like
217 * absolute POSIX paths or colon-separate lists of
218 * absolute POSIX paths into DOS paths (e.g.,
219 * "/foo:/foo/bar" might be converted to
220 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
221 * whereas longest_ancestor_length() requires paths
222 * that use forward slashes.
223 */
224 if (normalize_path_copy(path, path))
225 die("Path \"%s\" could not be normalized", argv[2]);
226 string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
227 filter_string_list(&ceiling_dirs, 0,
228 normalize_ceiling_entry, NULL);
229 len = longest_ancestor_length(path, &ceiling_dirs);
230 string_list_clear(&ceiling_dirs, 0);
231 free(path);
232 printf("%d\n", len);
233 return 0;
234 }
235
236 if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
237 const char *prefix = argv[2];
238 int prefix_len = strlen(prefix);
239 int nongit_ok;
240 setup_git_directory_gently(&nongit_ok);
241 while (argc > 3) {
242 puts(prefix_path(prefix, prefix_len, argv[3]));
243 argc--;
244 argv++;
245 }
246 return 0;
247 }
248
249 if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
250 char *prefix = strip_path_suffix(argv[2], argv[3]);
251 printf("%s\n", prefix ? prefix : "(null)");
252 return 0;
253 }
254
255 if (argc == 3 && !strcmp(argv[1], "print_path")) {
256 puts(argv[2]);
257 return 0;
258 }
259
260 if (argc == 4 && !strcmp(argv[1], "relative_path")) {
261 struct strbuf sb = STRBUF_INIT;
262 const char *in, *prefix, *rel;
263 normalize_argv_string(&in, argv[2]);
264 normalize_argv_string(&prefix, argv[3]);
265 rel = relative_path(in, prefix, &sb);
266 if (!rel)
267 puts("(null)");
268 else
269 puts(strlen(rel) > 0 ? rel : "(empty)");
270 strbuf_release(&sb);
271 return 0;
272 }
273
274 if (argc == 2 && !strcmp(argv[1], "basename"))
275 return test_function(basename_data, posix_basename, argv[1]);
276
277 if (argc == 2 && !strcmp(argv[1], "dirname"))
278 return test_function(dirname_data, posix_dirname, argv[1]);
279
280 if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) {
281 int res = 0, expect = 1, i;
282 for (i = 2; i < argc; i++)
283 if (!strcmp("--not", argv[i]))
284 expect = !expect;
285 else if (expect != is_dotgitmodules(argv[i]))
286 res = error("'%s' is %s.gitmodules", argv[i],
287 expect ? "not " : "");
288 else
289 fprintf(stderr, "ok: '%s' is %s.gitmodules\n",
290 argv[i], expect ? "" : "not ");
291 return !!res;
292 }
293
294 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
295 argv[1] ? argv[1] : "(there was none)");
296 return 1;
297}