]>
Commit | Line | Data |
---|---|---|
26c8a533 | 1 | /* |
3a429d3b | 2 | * Utilities for paths and pathnames |
26c8a533 LT |
3 | */ |
4 | #include "cache.h" | |
395de250 | 5 | #include "strbuf.h" |
a5ccdbe4 | 6 | #include "string-list.h" |
77a6d840 | 7 | #include "dir.h" |
26c8a533 | 8 | |
f66450ae | 9 | static int get_st_mode_bits(const char *path, int *mode) |
0117c2f0 TB |
10 | { |
11 | struct stat st; | |
12 | if (lstat(path, &st) < 0) | |
13 | return -1; | |
14 | *mode = st.st_mode; | |
15 | return 0; | |
16 | } | |
0117c2f0 | 17 | |
26c8a533 LT |
18 | static char bad_path[] = "/bad-path/"; |
19 | ||
4ef9caf5 | 20 | static struct strbuf *get_pathname(void) |
e7676d2f | 21 | { |
4ef9caf5 NTND |
22 | static struct strbuf pathname_array[4] = { |
23 | STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT | |
24 | }; | |
e7676d2f | 25 | static int index; |
4ef9caf5 NTND |
26 | struct strbuf *sb = &pathname_array[3 & ++index]; |
27 | strbuf_reset(sb); | |
28 | return sb; | |
e7676d2f LT |
29 | } |
30 | ||
26c8a533 LT |
31 | static char *cleanup_path(char *path) |
32 | { | |
33 | /* Clean it up */ | |
34 | if (!memcmp(path, "./", 2)) { | |
35 | path += 2; | |
36 | while (*path == '/') | |
37 | path++; | |
38 | } | |
39 | return path; | |
40 | } | |
41 | ||
4ef9caf5 NTND |
42 | static void strbuf_cleanup_path(struct strbuf *sb) |
43 | { | |
44 | char *path = cleanup_path(sb->buf); | |
45 | if (path > sb->buf) | |
46 | strbuf_remove(sb, 0, path - sb->buf); | |
47 | } | |
48 | ||
108bebea AR |
49 | char *mksnpath(char *buf, size_t n, const char *fmt, ...) |
50 | { | |
51 | va_list args; | |
52 | unsigned len; | |
53 | ||
54 | va_start(args, fmt); | |
55 | len = vsnprintf(buf, n, fmt, args); | |
56 | va_end(args); | |
57 | if (len >= n) { | |
9db56f71 | 58 | strlcpy(buf, bad_path, n); |
108bebea AR |
59 | return buf; |
60 | } | |
61 | return cleanup_path(buf); | |
62 | } | |
63 | ||
557bd833 | 64 | static int dir_prefix(const char *buf, const char *dir) |
fe2d7776 | 65 | { |
557bd833 NTND |
66 | int len = strlen(dir); |
67 | return !strncmp(buf, dir, len) && | |
68 | (is_dir_sep(buf[len]) || buf[len] == '\0'); | |
69 | } | |
fe2d7776 | 70 | |
557bd833 NTND |
71 | /* $buf =~ m|$dir/+$file| but without regex */ |
72 | static int is_dir_file(const char *buf, const char *dir, const char *file) | |
73 | { | |
74 | int len = strlen(dir); | |
75 | if (strncmp(buf, dir, len) || !is_dir_sep(buf[len])) | |
76 | return 0; | |
77 | while (is_dir_sep(buf[len])) | |
78 | len++; | |
79 | return !strcmp(buf + len, file); | |
80 | } | |
81 | ||
82 | static void replace_dir(struct strbuf *buf, int len, const char *newdir) | |
83 | { | |
84 | int newlen = strlen(newdir); | |
85 | int need_sep = (buf->buf[len] && !is_dir_sep(buf->buf[len])) && | |
86 | !is_dir_sep(newdir[newlen - 1]); | |
87 | if (need_sep) | |
88 | len--; /* keep one char, to be replaced with '/' */ | |
89 | strbuf_splice(buf, 0, len, newdir, newlen); | |
90 | if (need_sep) | |
91 | buf->buf[newlen] = '/'; | |
92 | } | |
93 | ||
c7b3a3d2 | 94 | static const char *common_list[] = { |
df56607d | 95 | "/branches", "/hooks", "/info", "!/logs", "/lost-found", |
529fef20 | 96 | "/objects", "/refs", "/remotes", "/worktrees", "/rr-cache", "/svn", |
77a6d840 | 97 | "config", "!gc.pid", "packed-refs", "shallow", |
c7b3a3d2 NTND |
98 | NULL |
99 | }; | |
100 | ||
101 | static void update_common_dir(struct strbuf *buf, int git_dir_len) | |
102 | { | |
103 | char *base = buf->buf + git_dir_len; | |
104 | const char **p; | |
105 | ||
6cfbdcb2 NTND |
106 | if (is_dir_file(base, "logs", "HEAD") || |
107 | is_dir_file(base, "info", "sparse-checkout")) | |
c7b3a3d2 NTND |
108 | return; /* keep this in $GIT_DIR */ |
109 | for (p = common_list; *p; p++) { | |
110 | const char *path = *p; | |
111 | int is_dir = 0; | |
77a6d840 NTND |
112 | if (*path == '!') |
113 | path++; | |
c7b3a3d2 NTND |
114 | if (*path == '/') { |
115 | path++; | |
116 | is_dir = 1; | |
117 | } | |
118 | if (is_dir && dir_prefix(base, path)) { | |
119 | replace_dir(buf, git_dir_len, get_git_common_dir()); | |
120 | return; | |
121 | } | |
122 | if (!is_dir && !strcmp(base, path)) { | |
123 | replace_dir(buf, git_dir_len, get_git_common_dir()); | |
124 | return; | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
77a6d840 NTND |
129 | void report_linked_checkout_garbage(void) |
130 | { | |
131 | struct strbuf sb = STRBUF_INIT; | |
132 | const char **p; | |
133 | int len; | |
134 | ||
135 | if (!git_common_dir_env) | |
136 | return; | |
137 | strbuf_addf(&sb, "%s/", get_git_dir()); | |
138 | len = sb.len; | |
139 | for (p = common_list; *p; p++) { | |
140 | const char *path = *p; | |
141 | if (*path == '!') | |
142 | continue; | |
143 | strbuf_setlen(&sb, len); | |
144 | strbuf_addstr(&sb, path); | |
145 | if (file_exists(sb.buf)) | |
146 | report_garbage("unused in linked checkout", sb.buf); | |
147 | } | |
148 | strbuf_release(&sb); | |
fe2d7776 AR |
149 | } |
150 | ||
557bd833 NTND |
151 | static void adjust_git_path(struct strbuf *buf, int git_dir_len) |
152 | { | |
153 | const char *base = buf->buf + git_dir_len; | |
154 | if (git_graft_env && is_dir_file(base, "info", "grafts")) | |
155 | strbuf_splice(buf, 0, buf->len, | |
156 | get_graft_file(), strlen(get_graft_file())); | |
157 | else if (git_index_env && !strcmp(base, "index")) | |
158 | strbuf_splice(buf, 0, buf->len, | |
159 | get_index_file(), strlen(get_index_file())); | |
160 | else if (git_db_env && dir_prefix(base, "objects")) | |
161 | replace_dir(buf, git_dir_len + 7, get_object_directory()); | |
c7b3a3d2 NTND |
162 | else if (git_common_dir_env) |
163 | update_common_dir(buf, git_dir_len); | |
557bd833 NTND |
164 | } |
165 | ||
8afdaf39 | 166 | static void do_git_path(struct strbuf *buf, const char *fmt, va_list args) |
fe2d7776 | 167 | { |
557bd833 NTND |
168 | int gitdir_len; |
169 | strbuf_addstr(buf, get_git_dir()); | |
4ef9caf5 NTND |
170 | if (buf->len && !is_dir_sep(buf->buf[buf->len - 1])) |
171 | strbuf_addch(buf, '/'); | |
557bd833 | 172 | gitdir_len = buf->len; |
4ef9caf5 | 173 | strbuf_vaddf(buf, fmt, args); |
557bd833 | 174 | adjust_git_path(buf, gitdir_len); |
4ef9caf5 | 175 | strbuf_cleanup_path(buf); |
fe2d7776 AR |
176 | } |
177 | ||
1a83c240 | 178 | void strbuf_git_path(struct strbuf *sb, const char *fmt, ...) |
aba13e7c AR |
179 | { |
180 | va_list args; | |
181 | va_start(args, fmt); | |
8afdaf39 | 182 | do_git_path(sb, fmt, args); |
aba13e7c | 183 | va_end(args); |
aba13e7c AR |
184 | } |
185 | ||
57a23b77 | 186 | const char *git_path(const char *fmt, ...) |
aba13e7c | 187 | { |
57a23b77 | 188 | struct strbuf *pathname = get_pathname(); |
aba13e7c AR |
189 | va_list args; |
190 | va_start(args, fmt); | |
57a23b77 | 191 | do_git_path(pathname, fmt, args); |
aba13e7c | 192 | va_end(args); |
57a23b77 | 193 | return pathname->buf; |
aba13e7c AR |
194 | } |
195 | ||
aba13e7c | 196 | char *git_pathdup(const char *fmt, ...) |
21cf3227 | 197 | { |
4ef9caf5 | 198 | struct strbuf path = STRBUF_INIT; |
21cf3227 | 199 | va_list args; |
21cf3227 | 200 | va_start(args, fmt); |
8afdaf39 | 201 | do_git_path(&path, fmt, args); |
21cf3227 | 202 | va_end(args); |
4ef9caf5 | 203 | return strbuf_detach(&path, NULL); |
21cf3227 HKNN |
204 | } |
205 | ||
21cf3227 | 206 | char *mkpathdup(const char *fmt, ...) |
26c8a533 | 207 | { |
21cf3227 | 208 | struct strbuf sb = STRBUF_INIT; |
26c8a533 | 209 | va_list args; |
26c8a533 | 210 | va_start(args, fmt); |
21cf3227 | 211 | strbuf_vaddf(&sb, fmt, args); |
26c8a533 | 212 | va_end(args); |
4ef9caf5 NTND |
213 | strbuf_cleanup_path(&sb); |
214 | return strbuf_detach(&sb, NULL); | |
26c8a533 LT |
215 | } |
216 | ||
dcf69262 | 217 | const char *mkpath(const char *fmt, ...) |
26c8a533 | 218 | { |
26c8a533 | 219 | va_list args; |
4ef9caf5 | 220 | struct strbuf *pathname = get_pathname(); |
26c8a533 | 221 | va_start(args, fmt); |
4ef9caf5 | 222 | strbuf_vaddf(pathname, fmt, args); |
26c8a533 | 223 | va_end(args); |
4ef9caf5 | 224 | return cleanup_path(pathname->buf); |
26c8a533 | 225 | } |
f2db68ed | 226 | |
f5895fd3 JK |
227 | static void do_submodule_path(struct strbuf *buf, const char *path, |
228 | const char *fmt, va_list args) | |
0bad611b | 229 | { |
0bad611b | 230 | const char *git_dir; |
0bad611b | 231 | |
4ef9caf5 NTND |
232 | strbuf_addstr(buf, path); |
233 | if (buf->len && buf->buf[buf->len - 1] != '/') | |
234 | strbuf_addch(buf, '/'); | |
235 | strbuf_addstr(buf, ".git"); | |
0bad611b | 236 | |
4ef9caf5 | 237 | git_dir = read_gitfile(buf->buf); |
0bad611b | 238 | if (git_dir) { |
4ef9caf5 NTND |
239 | strbuf_reset(buf); |
240 | strbuf_addstr(buf, git_dir); | |
0bad611b | 241 | } |
4ef9caf5 | 242 | strbuf_addch(buf, '/'); |
0bad611b | 243 | |
4ef9caf5 | 244 | strbuf_vaddf(buf, fmt, args); |
4ef9caf5 | 245 | strbuf_cleanup_path(buf); |
f5895fd3 JK |
246 | } |
247 | ||
f5895fd3 JK |
248 | char *git_pathdup_submodule(const char *path, const char *fmt, ...) |
249 | { | |
250 | va_list args; | |
251 | struct strbuf buf = STRBUF_INIT; | |
252 | va_start(args, fmt); | |
253 | do_submodule_path(&buf, path, fmt, args); | |
254 | va_end(args); | |
255 | return strbuf_detach(&buf, NULL); | |
256 | } | |
257 | ||
258 | void strbuf_git_path_submodule(struct strbuf *buf, const char *path, | |
259 | const char *fmt, ...) | |
260 | { | |
261 | va_list args; | |
262 | va_start(args, fmt); | |
263 | do_submodule_path(buf, path, fmt, args); | |
264 | va_end(args); | |
265 | } | |
266 | ||
c847f537 | 267 | int validate_headref(const char *path) |
0870ca7f JH |
268 | { |
269 | struct stat st; | |
270 | char *buf, buffer[256]; | |
c847f537 | 271 | unsigned char sha1[20]; |
0104ca09 HO |
272 | int fd; |
273 | ssize_t len; | |
0870ca7f JH |
274 | |
275 | if (lstat(path, &st) < 0) | |
276 | return -1; | |
277 | ||
278 | /* Make sure it is a "refs/.." symlink */ | |
279 | if (S_ISLNK(st.st_mode)) { | |
280 | len = readlink(path, buffer, sizeof(buffer)-1); | |
222b1673 | 281 | if (len >= 5 && !memcmp("refs/", buffer, 5)) |
0870ca7f JH |
282 | return 0; |
283 | return -1; | |
284 | } | |
285 | ||
286 | /* | |
287 | * Anything else, just open it and try to see if it is a symbolic ref. | |
288 | */ | |
289 | fd = open(path, O_RDONLY); | |
290 | if (fd < 0) | |
291 | return -1; | |
93d26e4c | 292 | len = read_in_full(fd, buffer, sizeof(buffer)-1); |
0870ca7f JH |
293 | close(fd); |
294 | ||
295 | /* | |
296 | * Is it a symbolic ref? | |
297 | */ | |
c847f537 | 298 | if (len < 4) |
0870ca7f | 299 | return -1; |
c847f537 JH |
300 | if (!memcmp("ref:", buffer, 4)) { |
301 | buf = buffer + 4; | |
302 | len -= 4; | |
303 | while (len && isspace(*buf)) | |
304 | buf++, len--; | |
222b1673 | 305 | if (len >= 5 && !memcmp("refs/", buf, 5)) |
c847f537 JH |
306 | return 0; |
307 | } | |
308 | ||
309 | /* | |
310 | * Is this a detached HEAD? | |
311 | */ | |
312 | if (!get_sha1_hex(buffer, sha1)) | |
0870ca7f | 313 | return 0; |
c847f537 | 314 | |
0870ca7f JH |
315 | return -1; |
316 | } | |
317 | ||
395de250 | 318 | static struct passwd *getpw_str(const char *username, size_t len) |
54f4b874 | 319 | { |
d79374c7 | 320 | struct passwd *pw; |
5c0b13f8 | 321 | char *username_z = xmemdupz(username, len); |
395de250 MM |
322 | pw = getpwnam(username_z); |
323 | free(username_z); | |
324 | return pw; | |
325 | } | |
54f4b874 | 326 | |
395de250 MM |
327 | /* |
328 | * Return a string with ~ and ~user expanded via getpw*. If buf != NULL, | |
329 | * then it is a newly allocated string. Returns NULL on getpw failure or | |
330 | * if path is NULL. | |
331 | */ | |
332 | char *expand_user_path(const char *path) | |
333 | { | |
334 | struct strbuf user_path = STRBUF_INIT; | |
395de250 MM |
335 | const char *to_copy = path; |
336 | ||
337 | if (path == NULL) | |
338 | goto return_null; | |
339 | if (path[0] == '~') { | |
53ec551c | 340 | const char *first_slash = strchrnul(path, '/'); |
395de250 MM |
341 | const char *username = path + 1; |
342 | size_t username_len = first_slash - username; | |
df2a79f4 MM |
343 | if (username_len == 0) { |
344 | const char *home = getenv("HOME"); | |
79bf1490 JN |
345 | if (!home) |
346 | goto return_null; | |
cedc61a9 | 347 | strbuf_addstr(&user_path, home); |
df2a79f4 MM |
348 | } else { |
349 | struct passwd *pw = getpw_str(username, username_len); | |
350 | if (!pw) | |
351 | goto return_null; | |
cedc61a9 | 352 | strbuf_addstr(&user_path, pw->pw_dir); |
54f4b874 | 353 | } |
395de250 | 354 | to_copy = first_slash; |
d79374c7 | 355 | } |
cedc61a9 | 356 | strbuf_addstr(&user_path, to_copy); |
395de250 MM |
357 | return strbuf_detach(&user_path, NULL); |
358 | return_null: | |
359 | strbuf_release(&user_path); | |
360 | return NULL; | |
54f4b874 AE |
361 | } |
362 | ||
d79374c7 JH |
363 | /* |
364 | * First, one directory to try is determined by the following algorithm. | |
365 | * | |
366 | * (0) If "strict" is given, the path is used as given and no DWIM is | |
367 | * done. Otherwise: | |
368 | * (1) "~/path" to mean path under the running user's home directory; | |
369 | * (2) "~user/path" to mean path under named user's home directory; | |
370 | * (3) "relative/path" to mean cwd relative directory; or | |
371 | * (4) "/absolute/path" to mean absolute directory. | |
372 | * | |
c8c3f1d0 PT |
373 | * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git" |
374 | * in this order. We select the first one that is a valid git repository, and | |
375 | * chdir() to it. If none match, or we fail to chdir, we return NULL. | |
d79374c7 JH |
376 | * |
377 | * If all goes well, we return the directory we used to chdir() (but | |
378 | * before ~user is expanded), avoiding getcwd() resolving symbolic | |
379 | * links. User relative paths are also returned as they are given, | |
380 | * except DWIM suffixing. | |
381 | */ | |
1c64b48e | 382 | const char *enter_repo(const char *path, int strict) |
54f4b874 | 383 | { |
d79374c7 JH |
384 | static char used_path[PATH_MAX]; |
385 | static char validated_path[PATH_MAX]; | |
386 | ||
387 | if (!path) | |
54f4b874 AE |
388 | return NULL; |
389 | ||
d79374c7 JH |
390 | if (!strict) { |
391 | static const char *suffix[] = { | |
b3256eb8 | 392 | "/.git", "", ".git/.git", ".git", NULL, |
d79374c7 | 393 | }; |
03106768 | 394 | const char *gitfile; |
d79374c7 JH |
395 | int len = strlen(path); |
396 | int i; | |
1c64b48e | 397 | while ((1 < len) && (path[len-1] == '/')) |
d79374c7 | 398 | len--; |
1c64b48e | 399 | |
d79374c7 | 400 | if (PATH_MAX <= len) |
54f4b874 | 401 | return NULL; |
1c64b48e EFL |
402 | strncpy(used_path, path, len); used_path[len] = 0 ; |
403 | strcpy(validated_path, used_path); | |
404 | ||
405 | if (used_path[0] == '~') { | |
406 | char *newpath = expand_user_path(used_path); | |
395de250 MM |
407 | if (!newpath || (PATH_MAX - 10 < strlen(newpath))) { |
408 | free(newpath); | |
d79374c7 | 409 | return NULL; |
395de250 MM |
410 | } |
411 | /* | |
412 | * Copy back into the static buffer. A pity | |
413 | * since newpath was not bounded, but other | |
414 | * branches of the if are limited by PATH_MAX | |
415 | * anyway. | |
416 | */ | |
417 | strcpy(used_path, newpath); free(newpath); | |
d79374c7 JH |
418 | } |
419 | else if (PATH_MAX - 10 < len) | |
420 | return NULL; | |
1c64b48e | 421 | len = strlen(used_path); |
d79374c7 | 422 | for (i = 0; suffix[i]; i++) { |
b3256eb8 | 423 | struct stat st; |
1c64b48e | 424 | strcpy(used_path + len, suffix[i]); |
b3256eb8 JK |
425 | if (!stat(used_path, &st) && |
426 | (S_ISREG(st.st_mode) || | |
427 | (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) { | |
d79374c7 JH |
428 | strcat(validated_path, suffix[i]); |
429 | break; | |
430 | } | |
431 | } | |
03106768 PH |
432 | if (!suffix[i]) |
433 | return NULL; | |
434 | gitfile = read_gitfile(used_path) ; | |
435 | if (gitfile) | |
436 | strcpy(used_path, gitfile); | |
437 | if (chdir(used_path)) | |
0870ca7f | 438 | return NULL; |
d79374c7 | 439 | path = validated_path; |
0870ca7f | 440 | } |
d79374c7 JH |
441 | else if (chdir(path)) |
442 | return NULL; | |
54f4b874 | 443 | |
d79374c7 | 444 | if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && |
c847f537 | 445 | validate_headref("HEAD") == 0) { |
717c3972 | 446 | set_git_dir("."); |
1644162a | 447 | check_repository_format(); |
d79374c7 | 448 | return path; |
54f4b874 AE |
449 | } |
450 | ||
451 | return NULL; | |
452 | } | |
138086a7 | 453 | |
cbe43b84 | 454 | static int calc_shared_perm(int mode) |
138086a7 | 455 | { |
cbe43b84 | 456 | int tweak; |
138086a7 | 457 | |
5a688fe4 | 458 | if (shared_repository < 0) |
cbe43b84 | 459 | tweak = -shared_repository; |
5a688fe4 | 460 | else |
cbe43b84 | 461 | tweak = shared_repository; |
5a688fe4 JH |
462 | |
463 | if (!(mode & S_IWUSR)) | |
464 | tweak &= ~0222; | |
465 | if (mode & S_IXUSR) | |
466 | /* Copy read bits to execute bits */ | |
467 | tweak |= (tweak & 0444) >> 2; | |
468 | if (shared_repository < 0) | |
469 | mode = (mode & ~0777) | tweak; | |
470 | else | |
8c6202d8 | 471 | mode |= tweak; |
06cbe855 | 472 | |
cbe43b84 TB |
473 | return mode; |
474 | } | |
475 | ||
476 | ||
477 | int adjust_shared_perm(const char *path) | |
478 | { | |
479 | int old_mode, new_mode; | |
480 | ||
481 | if (!shared_repository) | |
482 | return 0; | |
483 | if (get_st_mode_bits(path, &old_mode) < 0) | |
484 | return -1; | |
485 | ||
486 | new_mode = calc_shared_perm(old_mode); | |
487 | if (S_ISDIR(old_mode)) { | |
06cbe855 | 488 | /* Copy read bits to execute bits */ |
cbe43b84 TB |
489 | new_mode |= (new_mode & 0444) >> 2; |
490 | new_mode |= FORCE_DIR_SET_GID; | |
06cbe855 HO |
491 | } |
492 | ||
cbe43b84 TB |
493 | if (((old_mode ^ new_mode) & ~S_IFMT) && |
494 | chmod(path, (new_mode & ~S_IFMT)) < 0) | |
138086a7 JH |
495 | return -2; |
496 | return 0; | |
497 | } | |
e5392c51 | 498 | |
7fbd4221 JX |
499 | static int have_same_root(const char *path1, const char *path2) |
500 | { | |
501 | int is_abs1, is_abs2; | |
502 | ||
503 | is_abs1 = is_absolute_path(path1); | |
504 | is_abs2 = is_absolute_path(path2); | |
505 | return (is_abs1 && is_abs2 && tolower(path1[0]) == tolower(path2[0])) || | |
506 | (!is_abs1 && !is_abs2); | |
507 | } | |
508 | ||
e02ca72f JX |
509 | /* |
510 | * Give path as relative to prefix. | |
511 | * | |
512 | * The strbuf may or may not be used, so do not assume it contains the | |
513 | * returned path. | |
514 | */ | |
515 | const char *relative_path(const char *in, const char *prefix, | |
516 | struct strbuf *sb) | |
044bbbcb | 517 | { |
e02ca72f JX |
518 | int in_len = in ? strlen(in) : 0; |
519 | int prefix_len = prefix ? strlen(prefix) : 0; | |
520 | int in_off = 0; | |
521 | int prefix_off = 0; | |
288123f0 JH |
522 | int i = 0, j = 0; |
523 | ||
e02ca72f JX |
524 | if (!in_len) |
525 | return "./"; | |
526 | else if (!prefix_len) | |
527 | return in; | |
528 | ||
7fbd4221 JX |
529 | if (have_same_root(in, prefix)) { |
530 | /* bypass dos_drive, for "c:" is identical to "C:" */ | |
531 | if (has_dos_drive_prefix(in)) { | |
532 | i = 2; | |
533 | j = 2; | |
534 | } | |
535 | } else { | |
536 | return in; | |
537 | } | |
538 | ||
e02ca72f JX |
539 | while (i < prefix_len && j < in_len && prefix[i] == in[j]) { |
540 | if (is_dir_sep(prefix[i])) { | |
541 | while (is_dir_sep(prefix[i])) | |
288123f0 | 542 | i++; |
e02ca72f JX |
543 | while (is_dir_sep(in[j])) |
544 | j++; | |
545 | prefix_off = i; | |
546 | in_off = j; | |
547 | } else { | |
548 | i++; | |
549 | j++; | |
550 | } | |
551 | } | |
552 | ||
553 | if ( | |
554 | /* "prefix" seems like prefix of "in" */ | |
555 | i >= prefix_len && | |
556 | /* | |
557 | * but "/foo" is not a prefix of "/foobar" | |
558 | * (i.e. prefix not end with '/') | |
559 | */ | |
560 | prefix_off < prefix_len) { | |
561 | if (j >= in_len) { | |
562 | /* in="/a/b", prefix="/a/b" */ | |
563 | in_off = in_len; | |
564 | } else if (is_dir_sep(in[j])) { | |
565 | /* in="/a/b/c", prefix="/a/b" */ | |
566 | while (is_dir_sep(in[j])) | |
288123f0 | 567 | j++; |
e02ca72f JX |
568 | in_off = j; |
569 | } else { | |
570 | /* in="/a/bbb/c", prefix="/a/b" */ | |
571 | i = prefix_off; | |
572 | } | |
573 | } else if ( | |
574 | /* "in" is short than "prefix" */ | |
575 | j >= in_len && | |
576 | /* "in" not end with '/' */ | |
577 | in_off < in_len) { | |
578 | if (is_dir_sep(prefix[i])) { | |
579 | /* in="/a/b", prefix="/a/b/c/" */ | |
580 | while (is_dir_sep(prefix[i])) | |
581 | i++; | |
582 | in_off = in_len; | |
583 | } | |
584 | } | |
585 | in += in_off; | |
586 | in_len -= in_off; | |
587 | ||
588 | if (i >= prefix_len) { | |
589 | if (!in_len) | |
590 | return "./"; | |
591 | else | |
592 | return in; | |
593 | } | |
594 | ||
595 | strbuf_reset(sb); | |
596 | strbuf_grow(sb, in_len); | |
597 | ||
598 | while (i < prefix_len) { | |
599 | if (is_dir_sep(prefix[i])) { | |
600 | strbuf_addstr(sb, "../"); | |
601 | while (is_dir_sep(prefix[i])) | |
602 | i++; | |
288123f0 | 603 | continue; |
288123f0 JH |
604 | } |
605 | i++; | |
288123f0 | 606 | } |
e02ca72f JX |
607 | if (!is_dir_sep(prefix[prefix_len - 1])) |
608 | strbuf_addstr(sb, "../"); | |
609 | ||
610 | strbuf_addstr(sb, in); | |
611 | ||
612 | return sb->buf; | |
044bbbcb | 613 | } |
ae299be0 | 614 | |
41894ae3 JX |
615 | /* |
616 | * A simpler implementation of relative_path | |
617 | * | |
618 | * Get relative path by removing "prefix" from "in". This function | |
619 | * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter | |
620 | * to increase performance when traversing the path to work_tree. | |
621 | */ | |
622 | const char *remove_leading_path(const char *in, const char *prefix) | |
623 | { | |
624 | static char buf[PATH_MAX + 1]; | |
625 | int i = 0, j = 0; | |
626 | ||
627 | if (!prefix || !prefix[0]) | |
628 | return in; | |
629 | while (prefix[i]) { | |
630 | if (is_dir_sep(prefix[i])) { | |
631 | if (!is_dir_sep(in[j])) | |
632 | return in; | |
633 | while (is_dir_sep(prefix[i])) | |
634 | i++; | |
635 | while (is_dir_sep(in[j])) | |
636 | j++; | |
637 | continue; | |
638 | } else if (in[j] != prefix[i]) { | |
639 | return in; | |
640 | } | |
641 | i++; | |
642 | j++; | |
643 | } | |
644 | if ( | |
645 | /* "/foo" is a prefix of "/foo" */ | |
646 | in[j] && | |
647 | /* "/foo" is not a prefix of "/foobar" */ | |
648 | !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j]) | |
649 | ) | |
650 | return in; | |
651 | while (is_dir_sep(in[j])) | |
652 | j++; | |
653 | if (!in[j]) | |
654 | strcpy(buf, "."); | |
655 | else | |
656 | strcpy(buf, in + j); | |
657 | return buf; | |
658 | } | |
659 | ||
ae299be0 | 660 | /* |
f2a782b8 | 661 | * It is okay if dst == src, but they should not overlap otherwise. |
ae299be0 | 662 | * |
f2a782b8 JS |
663 | * Performs the following normalizations on src, storing the result in dst: |
664 | * - Ensures that components are separated by '/' (Windows only) | |
665 | * - Squashes sequences of '/'. | |
ae299be0 DR |
666 | * - Removes "." components. |
667 | * - Removes ".." components, and the components the precede them. | |
f2a782b8 JS |
668 | * Returns failure (non-zero) if a ".." component appears as first path |
669 | * component anytime during the normalization. Otherwise, returns success (0). | |
ae299be0 DR |
670 | * |
671 | * Note that this function is purely textual. It does not follow symlinks, | |
672 | * verify the existence of the path, or make any system calls. | |
645a29c4 NTND |
673 | * |
674 | * prefix_len != NULL is for a specific case of prefix_pathspec(): | |
675 | * assume that src == dst and src[0..prefix_len-1] is already | |
676 | * normalized, any time "../" eats up to the prefix_len part, | |
677 | * prefix_len is reduced. In the end prefix_len is the remaining | |
678 | * prefix that has not been overridden by user pathspec. | |
ae299be0 | 679 | */ |
645a29c4 | 680 | int normalize_path_copy_len(char *dst, const char *src, int *prefix_len) |
ae299be0 | 681 | { |
f3cad0ad | 682 | char *dst0; |
ae299be0 | 683 | |
f3cad0ad JS |
684 | if (has_dos_drive_prefix(src)) { |
685 | *dst++ = *src++; | |
686 | *dst++ = *src++; | |
ae299be0 | 687 | } |
f3cad0ad | 688 | dst0 = dst; |
ae299be0 | 689 | |
f3cad0ad | 690 | if (is_dir_sep(*src)) { |
ae299be0 | 691 | *dst++ = '/'; |
f3cad0ad JS |
692 | while (is_dir_sep(*src)) |
693 | src++; | |
694 | } | |
695 | ||
696 | for (;;) { | |
697 | char c = *src; | |
698 | ||
699 | /* | |
700 | * A path component that begins with . could be | |
701 | * special: | |
702 | * (1) "." and ends -- ignore and terminate. | |
703 | * (2) "./" -- ignore them, eat slash and continue. | |
704 | * (3) ".." and ends -- strip one and terminate. | |
705 | * (4) "../" -- strip one, eat slash and continue. | |
706 | */ | |
707 | if (c == '.') { | |
708 | if (!src[1]) { | |
709 | /* (1) */ | |
710 | src++; | |
711 | } else if (is_dir_sep(src[1])) { | |
712 | /* (2) */ | |
713 | src += 2; | |
714 | while (is_dir_sep(*src)) | |
715 | src++; | |
716 | continue; | |
717 | } else if (src[1] == '.') { | |
718 | if (!src[2]) { | |
719 | /* (3) */ | |
720 | src += 2; | |
721 | goto up_one; | |
722 | } else if (is_dir_sep(src[2])) { | |
723 | /* (4) */ | |
724 | src += 3; | |
725 | while (is_dir_sep(*src)) | |
726 | src++; | |
727 | goto up_one; | |
728 | } | |
729 | } | |
730 | } | |
ae299be0 | 731 | |
f3cad0ad JS |
732 | /* copy up to the next '/', and eat all '/' */ |
733 | while ((c = *src++) != '\0' && !is_dir_sep(c)) | |
734 | *dst++ = c; | |
735 | if (is_dir_sep(c)) { | |
736 | *dst++ = '/'; | |
737 | while (is_dir_sep(c)) | |
738 | c = *src++; | |
739 | src--; | |
740 | } else if (!c) | |
741 | break; | |
742 | continue; | |
743 | ||
744 | up_one: | |
745 | /* | |
746 | * dst0..dst is prefix portion, and dst[-1] is '/'; | |
747 | * go up one level. | |
748 | */ | |
f42302b4 JS |
749 | dst--; /* go to trailing '/' */ |
750 | if (dst <= dst0) | |
f3cad0ad | 751 | return -1; |
f42302b4 JS |
752 | /* Windows: dst[-1] cannot be backslash anymore */ |
753 | while (dst0 < dst && dst[-1] != '/') | |
754 | dst--; | |
645a29c4 NTND |
755 | if (prefix_len && *prefix_len > dst - dst0) |
756 | *prefix_len = dst - dst0; | |
f3cad0ad | 757 | } |
ae299be0 | 758 | *dst = '\0'; |
f3cad0ad | 759 | return 0; |
ae299be0 | 760 | } |
0454dd93 | 761 | |
645a29c4 NTND |
762 | int normalize_path_copy(char *dst, const char *src) |
763 | { | |
764 | return normalize_path_copy_len(dst, src, NULL); | |
765 | } | |
766 | ||
0454dd93 DR |
767 | /* |
768 | * path = Canonical absolute path | |
9e2326c7 MH |
769 | * prefixes = string_list containing normalized, absolute paths without |
770 | * trailing slashes (except for the root directory, which is denoted by "/"). | |
0454dd93 | 771 | * |
9e2326c7 | 772 | * Determines, for each path in prefixes, whether the "prefix" |
0454dd93 DR |
773 | * is an ancestor directory of path. Returns the length of the longest |
774 | * ancestor directory, excluding any trailing slashes, or -1 if no prefix | |
31171d9e MH |
775 | * is an ancestor. (Note that this means 0 is returned if prefixes is |
776 | * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories | |
0454dd93 DR |
777 | * are not considered to be their own ancestors. path must be in a |
778 | * canonical form: empty components, or "." or ".." components are not | |
9e2326c7 | 779 | * allowed. |
0454dd93 | 780 | */ |
31171d9e | 781 | int longest_ancestor_length(const char *path, struct string_list *prefixes) |
0454dd93 | 782 | { |
a5ccdbe4 | 783 | int i, max_len = -1; |
0454dd93 | 784 | |
31171d9e | 785 | if (!strcmp(path, "/")) |
0454dd93 DR |
786 | return -1; |
787 | ||
31171d9e MH |
788 | for (i = 0; i < prefixes->nr; i++) { |
789 | const char *ceil = prefixes->items[i].string; | |
a5ccdbe4 MH |
790 | int len = strlen(ceil); |
791 | ||
9e2326c7 MH |
792 | if (len == 1 && ceil[0] == '/') |
793 | len = 0; /* root matches anything, with length 0 */ | |
794 | else if (!strncmp(path, ceil, len) && path[len] == '/') | |
795 | ; /* match of length len */ | |
796 | else | |
797 | continue; /* no match */ | |
0454dd93 | 798 | |
9e2326c7 | 799 | if (len > max_len) |
0454dd93 | 800 | max_len = len; |
0454dd93 DR |
801 | } |
802 | ||
803 | return max_len; | |
804 | } | |
4fcc86b0 JS |
805 | |
806 | /* strip arbitrary amount of directory separators at end of path */ | |
807 | static inline int chomp_trailing_dir_sep(const char *path, int len) | |
808 | { | |
809 | while (len && is_dir_sep(path[len - 1])) | |
810 | len--; | |
811 | return len; | |
812 | } | |
813 | ||
814 | /* | |
815 | * If path ends with suffix (complete path components), returns the | |
816 | * part before suffix (sans trailing directory separators). | |
817 | * Otherwise returns NULL. | |
818 | */ | |
819 | char *strip_path_suffix(const char *path, const char *suffix) | |
820 | { | |
821 | int path_len = strlen(path), suffix_len = strlen(suffix); | |
822 | ||
823 | while (suffix_len) { | |
824 | if (!path_len) | |
825 | return NULL; | |
826 | ||
827 | if (is_dir_sep(path[path_len - 1])) { | |
828 | if (!is_dir_sep(suffix[suffix_len - 1])) | |
829 | return NULL; | |
830 | path_len = chomp_trailing_dir_sep(path, path_len); | |
831 | suffix_len = chomp_trailing_dir_sep(suffix, suffix_len); | |
832 | } | |
833 | else if (path[--path_len] != suffix[--suffix_len]) | |
834 | return NULL; | |
835 | } | |
836 | ||
837 | if (path_len && !is_dir_sep(path[path_len - 1])) | |
838 | return NULL; | |
839 | return xstrndup(path, chomp_trailing_dir_sep(path, path_len)); | |
840 | } | |
34b6cb8b SP |
841 | |
842 | int daemon_avoid_alias(const char *p) | |
843 | { | |
844 | int sl, ndot; | |
845 | ||
846 | /* | |
847 | * This resurrects the belts and suspenders paranoia check by HPA | |
848 | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() | |
9517e6b8 | 849 | * does not do getcwd() based path canonicalization. |
34b6cb8b SP |
850 | * |
851 | * sl becomes true immediately after seeing '/' and continues to | |
852 | * be true as long as dots continue after that without intervening | |
853 | * non-dot character. | |
854 | */ | |
855 | if (!p || (*p != '/' && *p != '~')) | |
856 | return -1; | |
857 | sl = 1; ndot = 0; | |
858 | p++; | |
859 | ||
860 | while (1) { | |
861 | char ch = *p++; | |
862 | if (sl) { | |
863 | if (ch == '.') | |
864 | ndot++; | |
865 | else if (ch == '/') { | |
866 | if (ndot < 3) | |
867 | /* reject //, /./ and /../ */ | |
868 | return -1; | |
869 | ndot = 0; | |
870 | } | |
871 | else if (ch == 0) { | |
872 | if (0 < ndot && ndot < 3) | |
873 | /* reject /.$ and /..$ */ | |
874 | return -1; | |
875 | return 0; | |
876 | } | |
877 | else | |
878 | sl = ndot = 0; | |
879 | } | |
880 | else if (ch == 0) | |
881 | return 0; | |
882 | else if (ch == '/') { | |
883 | sl = 1; | |
884 | ndot = 0; | |
885 | } | |
886 | } | |
887 | } | |
4bb43de2 | 888 | |
1d1d69bc JS |
889 | static int only_spaces_and_periods(const char *path, size_t len, size_t skip) |
890 | { | |
891 | if (len < skip) | |
892 | return 0; | |
893 | len -= skip; | |
894 | path += skip; | |
895 | while (len-- > 0) { | |
896 | char c = *(path++); | |
897 | if (c != ' ' && c != '.') | |
898 | return 0; | |
899 | } | |
900 | return 1; | |
901 | } | |
902 | ||
903 | int is_ntfs_dotgit(const char *name) | |
904 | { | |
905 | int len; | |
906 | ||
907 | for (len = 0; ; len++) | |
908 | if (!name[len] || name[len] == '\\' || is_dir_sep(name[len])) { | |
909 | if (only_spaces_and_periods(name, len, 4) && | |
910 | !strncasecmp(name, ".git", 4)) | |
911 | return 1; | |
912 | if (only_spaces_and_periods(name, len, 5) && | |
913 | !strncasecmp(name, "git~1", 5)) | |
914 | return 1; | |
915 | if (name[len] != '\\') | |
916 | return 0; | |
917 | name += len + 1; | |
918 | len = -1; | |
919 | } | |
920 | } | |
ea19289b PT |
921 | |
922 | char *xdg_config_home(const char *filename) | |
923 | { | |
924 | const char *home, *config_home; | |
925 | ||
926 | assert(filename); | |
927 | config_home = getenv("XDG_CONFIG_HOME"); | |
928 | if (config_home && *config_home) | |
929 | return mkpathdup("%s/git/%s", config_home, filename); | |
930 | ||
931 | home = getenv("HOME"); | |
932 | if (home) | |
933 | return mkpathdup("%s/.config/git/%s", home, filename); | |
934 | return NULL; | |
935 | } | |
f932729c JK |
936 | |
937 | GIT_PATH_FUNC(git_path_cherry_pick_head, "CHERRY_PICK_HEAD") | |
938 | GIT_PATH_FUNC(git_path_revert_head, "REVERT_HEAD") | |
939 | GIT_PATH_FUNC(git_path_squash_msg, "SQUASH_MSG") | |
940 | GIT_PATH_FUNC(git_path_merge_msg, "MERGE_MSG") | |
941 | GIT_PATH_FUNC(git_path_merge_rr, "MERGE_RR") | |
942 | GIT_PATH_FUNC(git_path_merge_mode, "MERGE_MODE") | |
943 | GIT_PATH_FUNC(git_path_merge_head, "MERGE_HEAD") | |
944 | GIT_PATH_FUNC(git_path_fetch_head, "FETCH_HEAD") | |
945 | GIT_PATH_FUNC(git_path_shallow, "shallow") |