2 #include "string-list.h"
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.
10 static int normalize_ceiling_entry(struct string_list_item
*item
, void *unused
)
12 char *ceil
= item
->string
;
15 die("Empty path is not supported");
16 if (!is_absolute_path(ceil
))
17 die("Path \"%s\" is not absolute", ceil
);
18 if (normalize_path_copy(ceil
, ceil
) < 0)
19 die("Path \"%s\" could not be normalized", ceil
);
23 static void normalize_argv_string(const char **var
, const char *input
)
25 if (!strcmp(input
, "<null>"))
27 else if (!strcmp(input
, "<empty>"))
32 if (*var
&& (**var
== '<' || **var
== '('))
33 die("Bad value: %s\n", input
);
37 const char *from
; /* input: transform from this ... */
38 const char *to
; /* output: ... to this. */
39 const char *alternative
; /* output: ... or this. */
42 static int test_function(struct test_data
*data
, char *(*func
)(char *input
),
49 for (i
= 0; data
[i
].to
; i
++) {
53 xsnprintf(buffer
, sizeof(buffer
), "%s", data
[i
].from
);
56 if (!strcmp(to
, data
[i
].to
))
58 if (!data
[i
].alternative
)
59 error("FAIL: %s(%s) => '%s' != '%s'\n",
60 funcname
, data
[i
].from
, to
, data
[i
].to
);
61 else if (!strcmp(to
, data
[i
].alternative
))
64 error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
65 funcname
, data
[i
].from
, to
, data
[i
].to
,
72 static struct test_data basename_data
[] = {
73 /* --- POSIX type paths --- */
81 { "////", "/", "//" },
86 { "/usr/lib", "lib" },
88 { "usr/lib///", "lib" },
90 #if defined(__MINGW32__) || defined(_MSC_VER)
91 /* --- win32 type paths --- */
94 { "\\usr\\\\", "usr" },
95 { "\\usr\\lib", "lib" },
96 { "usr\\lib", "lib" },
97 { "usr\\lib\\\\\\", "lib" },
100 { "C:/usr/", "usr" },
101 { "C:/usr//", "usr" },
102 { "C:/usr/lib", "lib" },
103 { "C:usr/lib", "lib" },
104 { "C:usr/lib///", "lib" },
110 { "\\\\", "\\", "/" },
111 { "\\\\\\", "\\", "/" },
116 static struct test_data dirname_data
[] = {
117 /* --- POSIX type paths --- */
124 { "///", "/", "//" },
125 { "////", "/", "//" },
130 { "/usr/lib", "/usr" },
131 { "usr/lib", "usr" },
132 { "usr/lib///", "usr" },
134 #if defined(__MINGW32__) || defined(_MSC_VER)
135 /* --- win32 type paths --- */
140 { "\\usr\\\\", "\\" },
141 { "\\usr\\lib", "\\usr" },
142 { "usr\\lib", "usr" },
143 { "usr\\lib\\\\\\", "usr" },
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" },
154 { "\\\\\\\\", "\\" },
155 { "C:", "C:.", "." },
160 static int is_dotgitmodules(const char *path
)
162 return is_hfs_dotgitmodules(path
) || is_ntfs_dotgitmodules(path
);
165 int cmd_main(int argc
, const char **argv
)
167 if (argc
== 3 && !strcmp(argv
[1], "normalize_path_copy")) {
168 char *buf
= xmallocz(strlen(argv
[2]));
169 int rv
= normalize_path_copy(buf
, argv
[2]);
176 if (argc
>= 2 && !strcmp(argv
[1], "real_path")) {
178 puts(real_path(argv
[2]));
185 if (argc
>= 2 && !strcmp(argv
[1], "absolute_path")) {
187 puts(absolute_path(argv
[2]));
194 if (argc
== 4 && !strcmp(argv
[1], "longest_ancestor_length")) {
196 struct string_list ceiling_dirs
= STRING_LIST_INIT_DUP
;
197 char *path
= xstrdup(argv
[2]);
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.
209 if (normalize_path_copy(path
, path
))
210 die("Path \"%s\" could not be normalized", argv
[2]);
211 string_list_split(&ceiling_dirs
, argv
[3], PATH_SEP
, -1);
212 filter_string_list(&ceiling_dirs
, 0,
213 normalize_ceiling_entry
, NULL
);
214 len
= longest_ancestor_length(path
, &ceiling_dirs
);
215 string_list_clear(&ceiling_dirs
, 0);
221 if (argc
>= 4 && !strcmp(argv
[1], "prefix_path")) {
222 const char *prefix
= argv
[2];
223 int prefix_len
= strlen(prefix
);
225 setup_git_directory_gently(&nongit_ok
);
227 puts(prefix_path(prefix
, prefix_len
, argv
[3]));
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)");
240 if (argc
== 3 && !strcmp(argv
[1], "print_path")) {
245 if (argc
== 4 && !strcmp(argv
[1], "relative_path")) {
246 struct strbuf sb
= STRBUF_INIT
;
247 const char *in
, *prefix
, *rel
;
248 normalize_argv_string(&in
, argv
[2]);
249 normalize_argv_string(&prefix
, argv
[3]);
250 rel
= relative_path(in
, prefix
, &sb
);
254 puts(strlen(rel
) > 0 ? rel
: "(empty)");
259 if (argc
== 2 && !strcmp(argv
[1], "basename"))
260 return test_function(basename_data
, basename
, argv
[1]);
262 if (argc
== 2 && !strcmp(argv
[1], "dirname"))
263 return test_function(dirname_data
, dirname
, argv
[1]);
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
]))
270 else if (expect
!= is_dotgitmodules(argv
[i
]))
271 res
= error("'%s' is %s.gitmodules", argv
[i
],
272 expect
? "not " : "");
274 fprintf(stderr
, "ok: '%s' is %s.gitmodules\n",
275 argv
[i
], expect
? "" : "not ");
279 fprintf(stderr
, "%s: unknown function name: %s\n", argv
[0],
280 argv
[1] ? argv
[1] : "(there was none)");