2 #include "string-list.h"
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.
9 static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
11 char *ceil = item->string;
14 die("Empty path is not supported");
15 if (!is_absolute_path(ceil))
16 die("Path \"%s\" is not absolute", ceil);
17 if (normalize_path_copy(ceil, ceil) < 0)
18 die("Path \"%s\" could not be normalized", ceil);
22 static void normalize_argv_string(const char **var, const char *input)
24 if (!strcmp(input, "<null>"))
26 else if (!strcmp(input, "<empty>"))
31 if (*var && (**var == '<' || **var == '('))
32 die("Bad value: %s\n", input);
36 const char *from; /* input: transform from this ... */
37 const char *to; /* output: ... to this. */
38 const char *alternative; /* output: ... or this. */
41 static int test_function(struct test_data *data, char *(*func)(char *input),
48 for (i = 0; data[i].to; i++) {
52 xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
55 if (!strcmp(to, data[i].to))
57 if (!data[i].alternative)
58 error("FAIL: %s(%s) => '%s' != '%s'\n",
59 funcname, data[i].from, to, data[i].to);
60 else if (!strcmp(to, data[i].alternative))
63 error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
64 funcname, data[i].from, to, data[i].to,
71 static struct test_data basename_data[] = {
72 /* --- POSIX type paths --- */
80 { "////", "/", "//" },
85 { "/usr/lib", "lib" },
87 { "usr/lib///", "lib" },
89 #if defined(__MINGW32__) || defined(_MSC_VER)
90 /* --- win32 type paths --- */
93 { "\\usr\\\\", "usr" },
94 { "\\usr\\lib", "lib" },
95 { "usr\\lib", "lib" },
96 { "usr\\lib\\\\\\", "lib" },
100 { "C:/usr//", "usr" },
101 { "C:/usr/lib", "lib" },
102 { "C:usr/lib", "lib" },
103 { "C:usr/lib///", "lib" },
109 { "\\\\", "\\", "/" },
110 { "\\\\\\", "\\", "/" },
115 static struct test_data dirname_data[] = {
116 /* --- POSIX type paths --- */
123 { "///", "/", "//" },
124 { "////", "/", "//" },
129 { "/usr/lib", "/usr" },
130 { "usr/lib", "usr" },
131 { "usr/lib///", "usr" },
133 #if defined(__MINGW32__) || defined(_MSC_VER)
134 /* --- win32 type paths --- */
139 { "\\usr\\\\", "\\" },
140 { "\\usr\\lib", "\\usr" },
141 { "usr\\lib", "usr" },
142 { "usr\\lib\\\\\\", "usr" },
147 { "C:/usr/", "C:/" },
148 { "C:/usr//", "C:/" },
149 { "C:/usr/lib", "C:/usr" },
150 { "C:usr/lib", "C:usr" },
151 { "C:usr/lib///", "C:usr" },
153 { "\\\\\\\\", "\\" },
154 { "C:", "C:.", "." },
159 int cmd_main(int argc, const char **argv)
161 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
162 char *buf = xmallocz(strlen(argv[2]));
163 int rv = normalize_path_copy(buf, argv[2]);
170 if (argc >= 2 && !strcmp(argv[1], "real_path")) {
172 puts(real_path(argv[2]));
179 if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
181 puts(absolute_path(argv[2]));
188 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
190 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
191 char *path = xstrdup(argv[2]);
194 * We have to normalize the arguments because under
195 * Windows, bash mangles arguments that look like
196 * absolute POSIX paths or colon-separate lists of
197 * absolute POSIX paths into DOS paths (e.g.,
198 * "/foo:/foo/bar" might be converted to
199 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
200 * whereas longest_ancestor_length() requires paths
201 * that use forward slashes.
203 if (normalize_path_copy(path, path))
204 die("Path \"%s\" could not be normalized", argv[2]);
205 string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
206 filter_string_list(&ceiling_dirs, 0,
207 normalize_ceiling_entry, NULL);
208 len = longest_ancestor_length(path, &ceiling_dirs);
209 string_list_clear(&ceiling_dirs, 0);
215 if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
216 const char *prefix = argv[2];
217 int prefix_len = strlen(prefix);
219 setup_git_directory_gently(&nongit_ok);
221 puts(prefix_path(prefix, prefix_len, argv[3]));
228 if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
229 char *prefix = strip_path_suffix(argv[2], argv[3]);
230 printf("%s\n", prefix ? prefix : "(null)");
234 if (argc == 3 && !strcmp(argv[1], "print_path")) {
239 if (argc == 4 && !strcmp(argv[1], "relative_path")) {
240 struct strbuf sb = STRBUF_INIT;
241 const char *in, *prefix, *rel;
242 normalize_argv_string(&in, argv[2]);
243 normalize_argv_string(&prefix, argv[3]);
244 rel = relative_path(in, prefix, &sb);
248 puts(strlen(rel) > 0 ? rel : "(empty)");
253 if (argc == 2 && !strcmp(argv[1], "basename"))
254 return test_function(basename_data, basename, argv[1]);
256 if (argc == 2 && !strcmp(argv[1], "dirname"))
257 return test_function(dirname_data, dirname, argv[1]);
259 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
260 argv[1] ? argv[1] : "(there was none)");