3 #include "string-list.h"
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.
11 static int normalize_ceiling_entry(struct string_list_item
*item
, void *unused
)
13 char *ceil
= item
->string
;
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
);
24 static void normalize_argv_string(const char **var
, const char *input
)
26 if (!strcmp(input
, "<null>"))
28 else if (!strcmp(input
, "<empty>"))
33 if (*var
&& (**var
== '<' || **var
== '('))
34 die("Bad value: %s\n", input
);
38 const char *from
; /* input: transform from this ... */
39 const char *to
; /* output: ... to this. */
40 const char *alternative
; /* output: ... or this. */
44 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
45 * have const parameters.
47 static char *posix_basename(char *path
)
49 return basename(path
);
52 static char *posix_dirname(char *path
)
57 static int test_function(struct test_data
*data
, char *(*func
)(char *input
),
64 for (i
= 0; data
[i
].to
; i
++) {
68 xsnprintf(buffer
, sizeof(buffer
), "%s", data
[i
].from
);
71 if (!strcmp(to
, data
[i
].to
))
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
))
79 error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
80 funcname
, data
[i
].from
, to
, data
[i
].to
,
87 static struct test_data basename_data
[] = {
88 /* --- POSIX type paths --- */
96 { "////", "/", "//" },
101 { "/usr/lib", "lib" },
102 { "usr/lib", "lib" },
103 { "usr/lib///", "lib" },
105 #if defined(__MINGW32__) || defined(_MSC_VER)
106 /* --- win32 type paths --- */
108 { "\\usr\\", "usr" },
109 { "\\usr\\\\", "usr" },
110 { "\\usr\\lib", "lib" },
111 { "usr\\lib", "lib" },
112 { "usr\\lib\\\\\\", "lib" },
115 { "C:/usr/", "usr" },
116 { "C:/usr//", "usr" },
117 { "C:/usr/lib", "lib" },
118 { "C:usr/lib", "lib" },
119 { "C:usr/lib///", "lib" },
125 { "\\\\", "\\", "/" },
126 { "\\\\\\", "\\", "/" },
131 static struct test_data dirname_data
[] = {
132 /* --- POSIX type paths --- */
139 { "///", "/", "//" },
140 { "////", "/", "//" },
145 { "/usr/lib", "/usr" },
146 { "usr/lib", "usr" },
147 { "usr/lib///", "usr" },
149 #if defined(__MINGW32__) || defined(_MSC_VER)
150 /* --- win32 type paths --- */
155 { "\\usr\\\\", "\\" },
156 { "\\usr\\lib", "\\usr" },
157 { "usr\\lib", "usr" },
158 { "usr\\lib\\\\\\", "usr" },
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" },
169 { "\\\\\\\\", "\\" },
170 { "C:", "C:.", "." },
175 static int is_dotgitmodules(const char *path
)
177 return is_hfs_dotgitmodules(path
) || is_ntfs_dotgitmodules(path
);
180 static int cmp_by_st_size(const void *a
, const void *b
)
182 intptr_t x
= (intptr_t)((struct string_list_item
*)a
)->util
;
183 intptr_t y
= (intptr_t)((struct string_list_item
*)b
)->util
;
185 return x
> y
? -1 : (x
< y
? +1 : 0);
188 int cmd__path_utils(int argc
, const char **argv
)
190 if (argc
== 3 && !strcmp(argv
[1], "normalize_path_copy")) {
191 char *buf
= xmallocz(strlen(argv
[2]));
192 int rv
= normalize_path_copy(buf
, argv
[2]);
199 if (argc
>= 2 && !strcmp(argv
[1], "real_path")) {
201 puts(real_path(argv
[2]));
208 if (argc
>= 2 && !strcmp(argv
[1], "absolute_path")) {
210 puts(absolute_path(argv
[2]));
217 if (argc
== 4 && !strcmp(argv
[1], "longest_ancestor_length")) {
219 struct string_list ceiling_dirs
= STRING_LIST_INIT_DUP
;
220 char *path
= xstrdup(argv
[2]);
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.
232 if (normalize_path_copy(path
, path
))
233 die("Path \"%s\" could not be normalized", argv
[2]);
234 string_list_split(&ceiling_dirs
, argv
[3], PATH_SEP
, -1);
235 filter_string_list(&ceiling_dirs
, 0,
236 normalize_ceiling_entry
, NULL
);
237 len
= longest_ancestor_length(path
, &ceiling_dirs
);
238 string_list_clear(&ceiling_dirs
, 0);
244 if (argc
>= 4 && !strcmp(argv
[1], "prefix_path")) {
245 const char *prefix
= argv
[2];
246 int prefix_len
= strlen(prefix
);
248 setup_git_directory_gently(&nongit_ok
);
250 puts(prefix_path(prefix
, prefix_len
, argv
[3]));
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)");
263 if (argc
== 3 && !strcmp(argv
[1], "print_path")) {
268 if (argc
== 4 && !strcmp(argv
[1], "relative_path")) {
269 struct strbuf sb
= STRBUF_INIT
;
270 const char *in
, *prefix
, *rel
;
271 normalize_argv_string(&in
, argv
[2]);
272 normalize_argv_string(&prefix
, argv
[3]);
273 rel
= relative_path(in
, prefix
, &sb
);
277 puts(strlen(rel
) > 0 ? rel
: "(empty)");
282 if (argc
== 2 && !strcmp(argv
[1], "basename"))
283 return test_function(basename_data
, posix_basename
, argv
[1]);
285 if (argc
== 2 && !strcmp(argv
[1], "dirname"))
286 return test_function(dirname_data
, posix_dirname
, argv
[1]);
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
]))
293 else if (expect
!= is_dotgitmodules(argv
[i
]))
294 res
= error("'%s' is %s.gitmodules", argv
[i
],
295 expect
? "not " : "");
297 fprintf(stderr
, "ok: '%s' is %s.gitmodules\n",
298 argv
[i
], expect
? "" : "not ");
302 if (argc
> 2 && !strcmp(argv
[1], "file-size")) {
306 for (i
= 2; i
< argc
; i
++)
307 if (stat(argv
[i
], &st
))
308 res
= error_errno("Cannot stat '%s'", argv
[i
]);
310 printf("%"PRIuMAX
"\n", (uintmax_t)st
.st_size
);
314 if (argc
== 4 && !strcmp(argv
[1], "skip-n-bytes")) {
315 int fd
= open(argv
[2], O_RDONLY
), offset
= atoi(argv
[3]);
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
);
323 ssize_t count
= read(fd
, buffer
, sizeof(buffer
));
325 die_errno("could not read '%s'", argv
[2]);
328 if (write(1, buffer
, count
) < 0)
329 die_errno("could not write to stdout");
335 if (argc
> 5 && !strcmp(argv
[1], "slice-tests")) {
337 long offset
, stride
, i
;
338 struct string_list list
= STRING_LIST_INIT_NODUP
;
341 offset
= strtol(argv
[2], NULL
, 10);
342 stride
= strtol(argv
[3], NULL
, 10);
345 for (i
= 4; i
< argc
; i
++)
346 if (stat(argv
[i
], &st
))
347 res
= error_errno("Cannot stat '%s'", argv
[i
]);
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
);
358 fprintf(stderr
, "%s: unknown function name: %s\n", argv
[0],
359 argv
[1] ? argv
[1] : "(there was none)");