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