]>
Commit | Line | Data |
---|---|---|
ae299be0 | 1 | #include "cache.h" |
31171d9e | 2 | #include "string-list.h" |
ae299be0 | 3 | |
9e2326c7 MH |
4 | /* |
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. | |
8 | */ | |
9 | static int normalize_ceiling_entry(struct string_list_item *item, void *unused) | |
10 | { | |
62f17513 | 11 | char *ceil = item->string; |
9e2326c7 | 12 | |
62f17513 | 13 | if (!*ceil) |
9e2326c7 | 14 | die("Empty path is not supported"); |
9e2326c7 MH |
15 | if (!is_absolute_path(ceil)) |
16 | die("Path \"%s\" is not absolute", ceil); | |
62f17513 | 17 | if (normalize_path_copy(ceil, ceil) < 0) |
9e2326c7 | 18 | die("Path \"%s\" could not be normalized", ceil); |
9e2326c7 MH |
19 | return 1; |
20 | } | |
21 | ||
203439b2 JX |
22 | static void normalize_argv_string(const char **var, const char *input) |
23 | { | |
24 | if (!strcmp(input, "<null>")) | |
25 | *var = NULL; | |
26 | else if (!strcmp(input, "<empty>")) | |
27 | *var = ""; | |
28 | else | |
29 | *var = input; | |
30 | ||
31 | if (*var && (**var == '<' || **var == '(')) | |
32 | die("Bad value: %s\n", input); | |
33 | } | |
34 | ||
7d1aaa68 JS |
35 | struct test_data { |
36 | const char *from; /* input: transform from this ... */ | |
37 | const char *to; /* output: ... to this. */ | |
371471ce | 38 | const char *alternative; /* output: ... or this. */ |
7d1aaa68 JS |
39 | }; |
40 | ||
29c2eda8 RS |
41 | /* |
42 | * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3) | |
43 | * have const parameters. | |
44 | */ | |
45 | static char *posix_basename(char *path) | |
46 | { | |
47 | return basename(path); | |
48 | } | |
49 | ||
50 | static char *posix_dirname(char *path) | |
51 | { | |
52 | return dirname(path); | |
53 | } | |
54 | ||
7d1aaa68 JS |
55 | static int test_function(struct test_data *data, char *(*func)(char *input), |
56 | const char *funcname) | |
57 | { | |
58 | int failed = 0, i; | |
59 | char buffer[1024]; | |
60 | char *to; | |
61 | ||
62 | for (i = 0; data[i].to; i++) { | |
63 | if (!data[i].from) | |
64 | to = func(NULL); | |
65 | else { | |
7b11a18a | 66 | xsnprintf(buffer, sizeof(buffer), "%s", data[i].from); |
7d1aaa68 JS |
67 | to = func(buffer); |
68 | } | |
371471ce JS |
69 | if (!strcmp(to, data[i].to)) |
70 | continue; | |
71 | if (!data[i].alternative) | |
7d1aaa68 JS |
72 | error("FAIL: %s(%s) => '%s' != '%s'\n", |
73 | funcname, data[i].from, to, data[i].to); | |
371471ce JS |
74 | else if (!strcmp(to, data[i].alternative)) |
75 | continue; | |
76 | else | |
77 | error("FAIL: %s(%s) => '%s' != '%s', '%s'\n", | |
78 | funcname, data[i].from, to, data[i].to, | |
79 | data[i].alternative); | |
80 | failed = 1; | |
7d1aaa68 JS |
81 | } |
82 | return failed; | |
83 | } | |
84 | ||
85 | static struct test_data basename_data[] = { | |
86 | /* --- POSIX type paths --- */ | |
87 | { NULL, "." }, | |
88 | { "", "." }, | |
89 | { ".", "." }, | |
90 | { "..", ".." }, | |
91 | { "/", "/" }, | |
371471ce JS |
92 | { "//", "/", "//" }, |
93 | { "///", "/", "//" }, | |
94 | { "////", "/", "//" }, | |
7d1aaa68 JS |
95 | { "usr", "usr" }, |
96 | { "/usr", "usr" }, | |
97 | { "/usr/", "usr" }, | |
98 | { "/usr//", "usr" }, | |
99 | { "/usr/lib", "lib" }, | |
100 | { "usr/lib", "lib" }, | |
101 | { "usr/lib///", "lib" }, | |
102 | ||
103 | #if defined(__MINGW32__) || defined(_MSC_VER) | |
7d1aaa68 JS |
104 | /* --- win32 type paths --- */ |
105 | { "\\usr", "usr" }, | |
106 | { "\\usr\\", "usr" }, | |
107 | { "\\usr\\\\", "usr" }, | |
108 | { "\\usr\\lib", "lib" }, | |
109 | { "usr\\lib", "lib" }, | |
110 | { "usr\\lib\\\\\\", "lib" }, | |
111 | { "C:/usr", "usr" }, | |
112 | { "C:/usr", "usr" }, | |
113 | { "C:/usr/", "usr" }, | |
114 | { "C:/usr//", "usr" }, | |
115 | { "C:/usr/lib", "lib" }, | |
116 | { "C:usr/lib", "lib" }, | |
117 | { "C:usr/lib///", "lib" }, | |
118 | { "C:", "." }, | |
119 | { "C:a", "a" }, | |
120 | { "C:/", "/" }, | |
121 | { "C:///", "/" }, | |
371471ce JS |
122 | { "\\", "\\", "/" }, |
123 | { "\\\\", "\\", "/" }, | |
124 | { "\\\\\\", "\\", "/" }, | |
7d1aaa68 JS |
125 | #endif |
126 | { NULL, NULL } | |
127 | }; | |
128 | ||
129 | static struct test_data dirname_data[] = { | |
130 | /* --- POSIX type paths --- */ | |
131 | { NULL, "." }, | |
132 | { "", "." }, | |
133 | { ".", "." }, | |
134 | { "..", "." }, | |
135 | { "/", "/" }, | |
371471ce JS |
136 | { "//", "/", "//" }, |
137 | { "///", "/", "//" }, | |
138 | { "////", "/", "//" }, | |
7d1aaa68 JS |
139 | { "usr", "." }, |
140 | { "/usr", "/" }, | |
141 | { "/usr/", "/" }, | |
142 | { "/usr//", "/" }, | |
143 | { "/usr/lib", "/usr" }, | |
144 | { "usr/lib", "usr" }, | |
145 | { "usr/lib///", "usr" }, | |
146 | ||
147 | #if defined(__MINGW32__) || defined(_MSC_VER) | |
7d1aaa68 JS |
148 | /* --- win32 type paths --- */ |
149 | { "\\", "\\" }, | |
150 | { "\\\\", "\\\\" }, | |
151 | { "\\usr", "\\" }, | |
152 | { "\\usr\\", "\\" }, | |
153 | { "\\usr\\\\", "\\" }, | |
154 | { "\\usr\\lib", "\\usr" }, | |
155 | { "usr\\lib", "usr" }, | |
156 | { "usr\\lib\\\\\\", "usr" }, | |
157 | { "C:a", "C:." }, | |
158 | { "C:/", "C:/" }, | |
159 | { "C:///", "C:/" }, | |
160 | { "C:/usr", "C:/" }, | |
161 | { "C:/usr/", "C:/" }, | |
162 | { "C:/usr//", "C:/" }, | |
163 | { "C:/usr/lib", "C:/usr" }, | |
164 | { "C:usr/lib", "C:usr" }, | |
165 | { "C:usr/lib///", "C:usr" }, | |
166 | { "\\\\\\", "\\" }, | |
167 | { "\\\\\\\\", "\\" }, | |
371471ce | 168 | { "C:", "C:.", "." }, |
7d1aaa68 JS |
169 | #endif |
170 | { NULL, NULL } | |
171 | }; | |
172 | ||
3f2e2297 | 173 | int cmd_main(int argc, const char **argv) |
ae299be0 | 174 | { |
f42302b4 | 175 | if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) { |
62f17513 | 176 | char *buf = xmallocz(strlen(argv[2])); |
f42302b4 JS |
177 | int rv = normalize_path_copy(buf, argv[2]); |
178 | if (rv) | |
179 | buf = "++failed++"; | |
ae299be0 | 180 | puts(buf); |
2cd85c40 | 181 | return 0; |
ae299be0 DR |
182 | } |
183 | ||
e2a57aac | 184 | if (argc >= 2 && !strcmp(argv[1], "real_path")) { |
d553e737 | 185 | while (argc > 2) { |
e2a57aac | 186 | puts(real_path(argv[2])); |
d553e737 DR |
187 | argc--; |
188 | argv++; | |
189 | } | |
2cd85c40 | 190 | return 0; |
d553e737 DR |
191 | } |
192 | ||
87a246e1 MH |
193 | if (argc >= 2 && !strcmp(argv[1], "absolute_path")) { |
194 | while (argc > 2) { | |
195 | puts(absolute_path(argv[2])); | |
196 | argc--; | |
197 | argv++; | |
198 | } | |
199 | return 0; | |
200 | } | |
201 | ||
0454dd93 | 202 | if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) { |
31171d9e MH |
203 | int len; |
204 | struct string_list ceiling_dirs = STRING_LIST_INIT_DUP; | |
9e2326c7 | 205 | char *path = xstrdup(argv[2]); |
31171d9e | 206 | |
9e2326c7 MH |
207 | /* |
208 | * We have to normalize the arguments because under | |
209 | * Windows, bash mangles arguments that look like | |
210 | * absolute POSIX paths or colon-separate lists of | |
211 | * absolute POSIX paths into DOS paths (e.g., | |
212 | * "/foo:/foo/bar" might be converted to | |
213 | * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"), | |
214 | * whereas longest_ancestor_length() requires paths | |
215 | * that use forward slashes. | |
216 | */ | |
217 | if (normalize_path_copy(path, path)) | |
218 | die("Path \"%s\" could not be normalized", argv[2]); | |
31171d9e | 219 | string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1); |
9e2326c7 MH |
220 | filter_string_list(&ceiling_dirs, 0, |
221 | normalize_ceiling_entry, NULL); | |
222 | len = longest_ancestor_length(path, &ceiling_dirs); | |
31171d9e | 223 | string_list_clear(&ceiling_dirs, 0); |
9e2326c7 | 224 | free(path); |
0454dd93 | 225 | printf("%d\n", len); |
2cd85c40 | 226 | return 0; |
0454dd93 DR |
227 | } |
228 | ||
9e813723 | 229 | if (argc >= 4 && !strcmp(argv[1], "prefix_path")) { |
3f2e2297 | 230 | const char *prefix = argv[2]; |
9e813723 MH |
231 | int prefix_len = strlen(prefix); |
232 | int nongit_ok; | |
233 | setup_git_directory_gently(&nongit_ok); | |
234 | while (argc > 3) { | |
235 | puts(prefix_path(prefix, prefix_len, argv[3])); | |
236 | argc--; | |
237 | argv++; | |
238 | } | |
239 | return 0; | |
240 | } | |
241 | ||
4fcc86b0 JS |
242 | if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) { |
243 | char *prefix = strip_path_suffix(argv[2], argv[3]); | |
244 | printf("%s\n", prefix ? prefix : "(null)"); | |
245 | return 0; | |
246 | } | |
247 | ||
7ffd18fc | 248 | if (argc == 3 && !strcmp(argv[1], "print_path")) { |
abd4284b JX |
249 | puts(argv[2]); |
250 | return 0; | |
251 | } | |
252 | ||
203439b2 | 253 | if (argc == 4 && !strcmp(argv[1], "relative_path")) { |
e02ca72f | 254 | struct strbuf sb = STRBUF_INIT; |
203439b2 JX |
255 | const char *in, *prefix, *rel; |
256 | normalize_argv_string(&in, argv[2]); | |
257 | normalize_argv_string(&prefix, argv[3]); | |
e02ca72f | 258 | rel = relative_path(in, prefix, &sb); |
203439b2 JX |
259 | if (!rel) |
260 | puts("(null)"); | |
261 | else | |
262 | puts(strlen(rel) > 0 ? rel : "(empty)"); | |
e02ca72f | 263 | strbuf_release(&sb); |
203439b2 JX |
264 | return 0; |
265 | } | |
266 | ||
7d1aaa68 | 267 | if (argc == 2 && !strcmp(argv[1], "basename")) |
29c2eda8 | 268 | return test_function(basename_data, posix_basename, argv[1]); |
7d1aaa68 JS |
269 | |
270 | if (argc == 2 && !strcmp(argv[1], "dirname")) | |
29c2eda8 | 271 | return test_function(dirname_data, posix_dirname, argv[1]); |
7d1aaa68 | 272 | |
2cd85c40 JS |
273 | fprintf(stderr, "%s: unknown function name: %s\n", argv[0], |
274 | argv[1] ? argv[1] : "(there was none)"); | |
275 | return 1; | |
ae299be0 | 276 | } |