fetch: centralize logic to print remote URL
[alt-git.git] / path.c
blob492e17ad12106233ddde63b724992f388693be10
1 /*
2 * Utilities for paths and pathnames
3 */
4 #include "cache.h"
5 #include "repository.h"
6 #include "strbuf.h"
7 #include "string-list.h"
8 #include "dir.h"
9 #include "worktree.h"
10 #include "submodule-config.h"
11 #include "path.h"
12 #include "packfile.h"
13 #include "object-store.h"
14 #include "lockfile.h"
15 #include "exec-cmd.h"
17 static int get_st_mode_bits(const char *path, int *mode)
19 struct stat st;
20 if (lstat(path, &st) < 0)
21 return -1;
22 *mode = st.st_mode;
23 return 0;
26 static char bad_path[] = "/bad-path/";
28 static struct strbuf *get_pathname(void)
30 static struct strbuf pathname_array[4] = {
31 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
33 static int index;
34 struct strbuf *sb = &pathname_array[index];
35 index = (index + 1) % ARRAY_SIZE(pathname_array);
36 strbuf_reset(sb);
37 return sb;
40 static const char *cleanup_path(const char *path)
42 /* Clean it up */
43 if (skip_prefix(path, "./", &path)) {
44 while (*path == '/')
45 path++;
47 return path;
50 static void strbuf_cleanup_path(struct strbuf *sb)
52 const char *path = cleanup_path(sb->buf);
53 if (path > sb->buf)
54 strbuf_remove(sb, 0, path - sb->buf);
57 char *mksnpath(char *buf, size_t n, const char *fmt, ...)
59 va_list args;
60 unsigned len;
62 va_start(args, fmt);
63 len = vsnprintf(buf, n, fmt, args);
64 va_end(args);
65 if (len >= n) {
66 strlcpy(buf, bad_path, n);
67 return buf;
69 return (char *)cleanup_path(buf);
72 static int dir_prefix(const char *buf, const char *dir)
74 int len = strlen(dir);
75 return !strncmp(buf, dir, len) &&
76 (is_dir_sep(buf[len]) || buf[len] == '\0');
79 /* $buf =~ m|$dir/+$file| but without regex */
80 static int is_dir_file(const char *buf, const char *dir, const char *file)
82 int len = strlen(dir);
83 if (strncmp(buf, dir, len) || !is_dir_sep(buf[len]))
84 return 0;
85 while (is_dir_sep(buf[len]))
86 len++;
87 return !strcmp(buf + len, file);
90 static void replace_dir(struct strbuf *buf, int len, const char *newdir)
92 int newlen = strlen(newdir);
93 int need_sep = (buf->buf[len] && !is_dir_sep(buf->buf[len])) &&
94 !is_dir_sep(newdir[newlen - 1]);
95 if (need_sep)
96 len--; /* keep one char, to be replaced with '/' */
97 strbuf_splice(buf, 0, len, newdir, newlen);
98 if (need_sep)
99 buf->buf[newlen] = '/';
102 struct common_dir {
103 /* Not considered garbage for report_linked_checkout_garbage */
104 unsigned ignore_garbage:1;
105 unsigned is_dir:1;
106 /* Belongs to the common dir, though it may contain paths that don't */
107 unsigned is_common:1;
108 const char *path;
111 static struct common_dir common_list[] = {
112 { 0, 1, 1, "branches" },
113 { 0, 1, 1, "common" },
114 { 0, 1, 1, "hooks" },
115 { 0, 1, 1, "info" },
116 { 0, 0, 0, "info/sparse-checkout" },
117 { 1, 1, 1, "logs" },
118 { 1, 0, 0, "logs/HEAD" },
119 { 0, 1, 0, "logs/refs/bisect" },
120 { 0, 1, 0, "logs/refs/rewritten" },
121 { 0, 1, 0, "logs/refs/worktree" },
122 { 0, 1, 1, "lost-found" },
123 { 0, 1, 1, "objects" },
124 { 0, 1, 1, "refs" },
125 { 0, 1, 0, "refs/bisect" },
126 { 0, 1, 0, "refs/rewritten" },
127 { 0, 1, 0, "refs/worktree" },
128 { 0, 1, 1, "remotes" },
129 { 0, 1, 1, "worktrees" },
130 { 0, 1, 1, "rr-cache" },
131 { 0, 1, 1, "svn" },
132 { 0, 0, 1, "config" },
133 { 1, 0, 1, "gc.pid" },
134 { 0, 0, 1, "packed-refs" },
135 { 0, 0, 1, "shallow" },
136 { 0, 0, 0, NULL }
140 * A compressed trie. A trie node consists of zero or more characters that
141 * are common to all elements with this prefix, optionally followed by some
142 * children. If value is not NULL, the trie node is a terminal node.
144 * For example, consider the following set of strings:
145 * abc
146 * def
147 * definite
148 * definition
150 * The trie would look like:
151 * root: len = 0, children a and d non-NULL, value = NULL.
152 * a: len = 2, contents = bc, value = (data for "abc")
153 * d: len = 2, contents = ef, children i non-NULL, value = (data for "def")
154 * i: len = 3, contents = nit, children e and i non-NULL, value = NULL
155 * e: len = 0, children all NULL, value = (data for "definite")
156 * i: len = 2, contents = on, children all NULL,
157 * value = (data for "definition")
159 struct trie {
160 struct trie *children[256];
161 int len;
162 char *contents;
163 void *value;
166 static struct trie *make_trie_node(const char *key, void *value)
168 struct trie *new_node = xcalloc(1, sizeof(*new_node));
169 new_node->len = strlen(key);
170 if (new_node->len) {
171 new_node->contents = xmalloc(new_node->len);
172 memcpy(new_node->contents, key, new_node->len);
174 new_node->value = value;
175 return new_node;
179 * Add a key/value pair to a trie. The key is assumed to be \0-terminated.
180 * If there was an existing value for this key, return it.
182 static void *add_to_trie(struct trie *root, const char *key, void *value)
184 struct trie *child;
185 void *old;
186 int i;
188 if (!*key) {
189 /* we have reached the end of the key */
190 old = root->value;
191 root->value = value;
192 return old;
195 for (i = 0; i < root->len; i++) {
196 if (root->contents[i] == key[i])
197 continue;
200 * Split this node: child will contain this node's
201 * existing children.
203 child = xmalloc(sizeof(*child));
204 memcpy(child->children, root->children, sizeof(root->children));
206 child->len = root->len - i - 1;
207 if (child->len) {
208 child->contents = xstrndup(root->contents + i + 1,
209 child->len);
211 child->value = root->value;
212 root->value = NULL;
213 root->len = i;
215 memset(root->children, 0, sizeof(root->children));
216 root->children[(unsigned char)root->contents[i]] = child;
218 /* This is the newly-added child. */
219 root->children[(unsigned char)key[i]] =
220 make_trie_node(key + i + 1, value);
221 return NULL;
224 /* We have matched the entire compressed section */
225 if (key[i]) {
226 child = root->children[(unsigned char)key[root->len]];
227 if (child) {
228 return add_to_trie(child, key + root->len + 1, value);
229 } else {
230 child = make_trie_node(key + root->len + 1, value);
231 root->children[(unsigned char)key[root->len]] = child;
232 return NULL;
236 old = root->value;
237 root->value = value;
238 return old;
241 typedef int (*match_fn)(const char *unmatched, void *value, void *baton);
244 * Search a trie for some key. Find the longest /-or-\0-terminated
245 * prefix of the key for which the trie contains a value. If there is
246 * no such prefix, return -1. Otherwise call fn with the unmatched
247 * portion of the key and the found value. If fn returns 0 or
248 * positive, then return its return value. If fn returns negative,
249 * then call fn with the next-longest /-terminated prefix of the key
250 * (i.e. a parent directory) for which the trie contains a value, and
251 * handle its return value the same way. If there is no shorter
252 * /-terminated prefix with a value left, then return the negative
253 * return value of the most recent fn invocation.
255 * The key is partially normalized: consecutive slashes are skipped.
257 * For example, consider the trie containing only [logs,
258 * logs/refs/bisect], both with values, but not logs/refs.
260 * | key | unmatched | prefix to node | return value |
261 * |--------------------|----------------|------------------|--------------|
262 * | a | not called | n/a | -1 |
263 * | logstore | not called | n/a | -1 |
264 * | logs | \0 | logs | as per fn |
265 * | logs/ | / | logs | as per fn |
266 * | logs/refs | /refs | logs | as per fn |
267 * | logs/refs/ | /refs/ | logs | as per fn |
268 * | logs/refs/b | /refs/b | logs | as per fn |
269 * | logs/refs/bisected | /refs/bisected | logs | as per fn |
270 * | logs/refs/bisect | \0 | logs/refs/bisect | as per fn |
271 * | logs/refs/bisect/ | / | logs/refs/bisect | as per fn |
272 * | logs/refs/bisect/a | /a | logs/refs/bisect | as per fn |
273 * | (If fn in the previous line returns -1, then fn is called once more:) |
274 * | logs/refs/bisect/a | /refs/bisect/a | logs | as per fn |
275 * |--------------------|----------------|------------------|--------------|
277 static int trie_find(struct trie *root, const char *key, match_fn fn,
278 void *baton)
280 int i;
281 int result;
282 struct trie *child;
284 if (!*key) {
285 /* we have reached the end of the key */
286 if (root->value && !root->len)
287 return fn(key, root->value, baton);
288 else
289 return -1;
292 for (i = 0; i < root->len; i++) {
293 /* Partial path normalization: skip consecutive slashes. */
294 if (key[i] == '/' && key[i+1] == '/') {
295 key++;
296 continue;
298 if (root->contents[i] != key[i])
299 return -1;
302 /* Matched the entire compressed section */
303 key += i;
304 if (!*key) {
305 /* End of key */
306 if (root->value)
307 return fn(key, root->value, baton);
308 else
309 return -1;
312 /* Partial path normalization: skip consecutive slashes */
313 while (key[0] == '/' && key[1] == '/')
314 key++;
316 child = root->children[(unsigned char)*key];
317 if (child)
318 result = trie_find(child, key + 1, fn, baton);
319 else
320 result = -1;
322 if (result >= 0 || (*key != '/' && *key != 0))
323 return result;
324 if (root->value)
325 return fn(key, root->value, baton);
326 else
327 return -1;
330 static struct trie common_trie;
331 static int common_trie_done_setup;
333 static void init_common_trie(void)
335 struct common_dir *p;
337 if (common_trie_done_setup)
338 return;
340 for (p = common_list; p->path; p++)
341 add_to_trie(&common_trie, p->path, p);
343 common_trie_done_setup = 1;
347 * Helper function for update_common_dir: returns 1 if the dir
348 * prefix is common.
350 static int check_common(const char *unmatched, void *value, void *baton)
352 struct common_dir *dir = value;
354 if (dir->is_dir && (unmatched[0] == 0 || unmatched[0] == '/'))
355 return dir->is_common;
357 if (!dir->is_dir && unmatched[0] == 0)
358 return dir->is_common;
360 return 0;
363 static void update_common_dir(struct strbuf *buf, int git_dir_len,
364 const char *common_dir)
366 char *base = buf->buf + git_dir_len;
367 int has_lock_suffix = strbuf_strip_suffix(buf, LOCK_SUFFIX);
369 init_common_trie();
370 if (trie_find(&common_trie, base, check_common, NULL) > 0)
371 replace_dir(buf, git_dir_len, common_dir);
373 if (has_lock_suffix)
374 strbuf_addstr(buf, LOCK_SUFFIX);
377 void report_linked_checkout_garbage(void)
379 struct strbuf sb = STRBUF_INIT;
380 const struct common_dir *p;
381 int len;
383 if (!the_repository->different_commondir)
384 return;
385 strbuf_addf(&sb, "%s/", get_git_dir());
386 len = sb.len;
387 for (p = common_list; p->path; p++) {
388 const char *path = p->path;
389 if (p->ignore_garbage)
390 continue;
391 strbuf_setlen(&sb, len);
392 strbuf_addstr(&sb, path);
393 if (file_exists(sb.buf))
394 report_garbage(PACKDIR_FILE_GARBAGE, sb.buf);
396 strbuf_release(&sb);
399 static void adjust_git_path(const struct repository *repo,
400 struct strbuf *buf, int git_dir_len)
402 const char *base = buf->buf + git_dir_len;
403 if (is_dir_file(base, "info", "grafts"))
404 strbuf_splice(buf, 0, buf->len,
405 repo->graft_file, strlen(repo->graft_file));
406 else if (!strcmp(base, "index"))
407 strbuf_splice(buf, 0, buf->len,
408 repo->index_file, strlen(repo->index_file));
409 else if (dir_prefix(base, "objects"))
410 replace_dir(buf, git_dir_len + 7, repo->objects->odb->path);
411 else if (git_hooks_path && dir_prefix(base, "hooks"))
412 replace_dir(buf, git_dir_len + 5, git_hooks_path);
413 else if (repo->different_commondir)
414 update_common_dir(buf, git_dir_len, repo->commondir);
417 static void strbuf_worktree_gitdir(struct strbuf *buf,
418 const struct repository *repo,
419 const struct worktree *wt)
421 if (!wt)
422 strbuf_addstr(buf, repo->gitdir);
423 else if (!wt->id)
424 strbuf_addstr(buf, repo->commondir);
425 else
426 strbuf_git_common_path(buf, repo, "worktrees/%s", wt->id);
429 static void do_git_path(const struct repository *repo,
430 const struct worktree *wt, struct strbuf *buf,
431 const char *fmt, va_list args)
433 int gitdir_len;
434 strbuf_worktree_gitdir(buf, repo, wt);
435 if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
436 strbuf_addch(buf, '/');
437 gitdir_len = buf->len;
438 strbuf_vaddf(buf, fmt, args);
439 if (!wt)
440 adjust_git_path(repo, buf, gitdir_len);
441 strbuf_cleanup_path(buf);
444 char *repo_git_path(const struct repository *repo,
445 const char *fmt, ...)
447 struct strbuf path = STRBUF_INIT;
448 va_list args;
449 va_start(args, fmt);
450 do_git_path(repo, NULL, &path, fmt, args);
451 va_end(args);
452 return strbuf_detach(&path, NULL);
455 void strbuf_repo_git_path(struct strbuf *sb,
456 const struct repository *repo,
457 const char *fmt, ...)
459 va_list args;
460 va_start(args, fmt);
461 do_git_path(repo, NULL, sb, fmt, args);
462 va_end(args);
465 char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
467 va_list args;
468 strbuf_reset(buf);
469 va_start(args, fmt);
470 do_git_path(the_repository, NULL, buf, fmt, args);
471 va_end(args);
472 return buf->buf;
475 void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
477 va_list args;
478 va_start(args, fmt);
479 do_git_path(the_repository, NULL, sb, fmt, args);
480 va_end(args);
483 const char *git_path(const char *fmt, ...)
485 struct strbuf *pathname = get_pathname();
486 va_list args;
487 va_start(args, fmt);
488 do_git_path(the_repository, NULL, pathname, fmt, args);
489 va_end(args);
490 return pathname->buf;
493 char *git_pathdup(const char *fmt, ...)
495 struct strbuf path = STRBUF_INIT;
496 va_list args;
497 va_start(args, fmt);
498 do_git_path(the_repository, NULL, &path, fmt, args);
499 va_end(args);
500 return strbuf_detach(&path, NULL);
503 char *mkpathdup(const char *fmt, ...)
505 struct strbuf sb = STRBUF_INIT;
506 va_list args;
507 va_start(args, fmt);
508 strbuf_vaddf(&sb, fmt, args);
509 va_end(args);
510 strbuf_cleanup_path(&sb);
511 return strbuf_detach(&sb, NULL);
514 const char *mkpath(const char *fmt, ...)
516 va_list args;
517 struct strbuf *pathname = get_pathname();
518 va_start(args, fmt);
519 strbuf_vaddf(pathname, fmt, args);
520 va_end(args);
521 return cleanup_path(pathname->buf);
524 const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...)
526 struct strbuf *pathname = get_pathname();
527 va_list args;
528 va_start(args, fmt);
529 do_git_path(the_repository, wt, pathname, fmt, args);
530 va_end(args);
531 return pathname->buf;
534 static void do_worktree_path(const struct repository *repo,
535 struct strbuf *buf,
536 const char *fmt, va_list args)
538 strbuf_addstr(buf, repo->worktree);
539 if(buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
540 strbuf_addch(buf, '/');
542 strbuf_vaddf(buf, fmt, args);
543 strbuf_cleanup_path(buf);
546 char *repo_worktree_path(const struct repository *repo, const char *fmt, ...)
548 struct strbuf path = STRBUF_INIT;
549 va_list args;
551 if (!repo->worktree)
552 return NULL;
554 va_start(args, fmt);
555 do_worktree_path(repo, &path, fmt, args);
556 va_end(args);
558 return strbuf_detach(&path, NULL);
561 void strbuf_repo_worktree_path(struct strbuf *sb,
562 const struct repository *repo,
563 const char *fmt, ...)
565 va_list args;
567 if (!repo->worktree)
568 return;
570 va_start(args, fmt);
571 do_worktree_path(repo, sb, fmt, args);
572 va_end(args);
575 /* Returns 0 on success, negative on failure. */
576 static int do_submodule_path(struct strbuf *buf, const char *path,
577 const char *fmt, va_list args)
579 struct strbuf git_submodule_common_dir = STRBUF_INIT;
580 struct strbuf git_submodule_dir = STRBUF_INIT;
581 int ret;
583 ret = submodule_to_gitdir(&git_submodule_dir, path);
584 if (ret)
585 goto cleanup;
587 strbuf_complete(&git_submodule_dir, '/');
588 strbuf_addbuf(buf, &git_submodule_dir);
589 strbuf_vaddf(buf, fmt, args);
591 if (get_common_dir_noenv(&git_submodule_common_dir, git_submodule_dir.buf))
592 update_common_dir(buf, git_submodule_dir.len, git_submodule_common_dir.buf);
594 strbuf_cleanup_path(buf);
596 cleanup:
597 strbuf_release(&git_submodule_dir);
598 strbuf_release(&git_submodule_common_dir);
599 return ret;
602 char *git_pathdup_submodule(const char *path, const char *fmt, ...)
604 int err;
605 va_list args;
606 struct strbuf buf = STRBUF_INIT;
607 va_start(args, fmt);
608 err = do_submodule_path(&buf, path, fmt, args);
609 va_end(args);
610 if (err) {
611 strbuf_release(&buf);
612 return NULL;
614 return strbuf_detach(&buf, NULL);
617 int strbuf_git_path_submodule(struct strbuf *buf, const char *path,
618 const char *fmt, ...)
620 int err;
621 va_list args;
622 va_start(args, fmt);
623 err = do_submodule_path(buf, path, fmt, args);
624 va_end(args);
626 return err;
629 static void do_git_common_path(const struct repository *repo,
630 struct strbuf *buf,
631 const char *fmt,
632 va_list args)
634 strbuf_addstr(buf, repo->commondir);
635 if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
636 strbuf_addch(buf, '/');
637 strbuf_vaddf(buf, fmt, args);
638 strbuf_cleanup_path(buf);
641 const char *git_common_path(const char *fmt, ...)
643 struct strbuf *pathname = get_pathname();
644 va_list args;
645 va_start(args, fmt);
646 do_git_common_path(the_repository, pathname, fmt, args);
647 va_end(args);
648 return pathname->buf;
651 void strbuf_git_common_path(struct strbuf *sb,
652 const struct repository *repo,
653 const char *fmt, ...)
655 va_list args;
656 va_start(args, fmt);
657 do_git_common_path(repo, sb, fmt, args);
658 va_end(args);
661 int validate_headref(const char *path)
663 struct stat st;
664 char buffer[256];
665 const char *refname;
666 struct object_id oid;
667 int fd;
668 ssize_t len;
670 if (lstat(path, &st) < 0)
671 return -1;
673 /* Make sure it is a "refs/.." symlink */
674 if (S_ISLNK(st.st_mode)) {
675 len = readlink(path, buffer, sizeof(buffer)-1);
676 if (len >= 5 && !memcmp("refs/", buffer, 5))
677 return 0;
678 return -1;
682 * Anything else, just open it and try to see if it is a symbolic ref.
684 fd = open(path, O_RDONLY);
685 if (fd < 0)
686 return -1;
687 len = read_in_full(fd, buffer, sizeof(buffer)-1);
688 close(fd);
690 if (len < 0)
691 return -1;
692 buffer[len] = '\0';
695 * Is it a symbolic ref?
697 if (skip_prefix(buffer, "ref:", &refname)) {
698 while (isspace(*refname))
699 refname++;
700 if (starts_with(refname, "refs/"))
701 return 0;
705 * Is this a detached HEAD?
707 if (!get_oid_hex(buffer, &oid))
708 return 0;
710 return -1;
713 static struct passwd *getpw_str(const char *username, size_t len)
715 struct passwd *pw;
716 char *username_z = xmemdupz(username, len);
717 pw = getpwnam(username_z);
718 free(username_z);
719 return pw;
723 * Return a string with ~ and ~user expanded via getpw*. Returns NULL on getpw
724 * failure or if path is NULL.
726 * If real_home is true, strbuf_realpath($HOME) is used in the `~/` expansion.
728 * If the path starts with `%(prefix)/`, the remainder is interpreted as
729 * relative to where Git is installed, and expanded to the absolute path.
731 char *interpolate_path(const char *path, int real_home)
733 struct strbuf user_path = STRBUF_INIT;
734 const char *to_copy = path;
736 if (!path)
737 goto return_null;
739 if (skip_prefix(path, "%(prefix)/", &path))
740 return system_path(path);
742 if (path[0] == '~') {
743 const char *first_slash = strchrnul(path, '/');
744 const char *username = path + 1;
745 size_t username_len = first_slash - username;
746 if (username_len == 0) {
747 const char *home = getenv("HOME");
748 if (!home)
749 goto return_null;
750 if (real_home)
751 strbuf_add_real_path(&user_path, home);
752 else
753 strbuf_addstr(&user_path, home);
754 #ifdef GIT_WINDOWS_NATIVE
755 convert_slashes(user_path.buf);
756 #endif
757 } else {
758 struct passwd *pw = getpw_str(username, username_len);
759 if (!pw)
760 goto return_null;
761 strbuf_addstr(&user_path, pw->pw_dir);
763 to_copy = first_slash;
765 strbuf_addstr(&user_path, to_copy);
766 return strbuf_detach(&user_path, NULL);
767 return_null:
768 strbuf_release(&user_path);
769 return NULL;
773 * First, one directory to try is determined by the following algorithm.
775 * (0) If "strict" is given, the path is used as given and no DWIM is
776 * done. Otherwise:
777 * (1) "~/path" to mean path under the running user's home directory;
778 * (2) "~user/path" to mean path under named user's home directory;
779 * (3) "relative/path" to mean cwd relative directory; or
780 * (4) "/absolute/path" to mean absolute directory.
782 * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git"
783 * in this order. We select the first one that is a valid git repository, and
784 * chdir() to it. If none match, or we fail to chdir, we return NULL.
786 * If all goes well, we return the directory we used to chdir() (but
787 * before ~user is expanded), avoiding getcwd() resolving symbolic
788 * links. User relative paths are also returned as they are given,
789 * except DWIM suffixing.
791 const char *enter_repo(const char *path, int strict)
793 static struct strbuf validated_path = STRBUF_INIT;
794 static struct strbuf used_path = STRBUF_INIT;
796 if (!path)
797 return NULL;
799 if (!strict) {
800 static const char *suffix[] = {
801 "/.git", "", ".git/.git", ".git", NULL,
803 const char *gitfile;
804 int len = strlen(path);
805 int i;
806 while ((1 < len) && (path[len-1] == '/'))
807 len--;
810 * We can handle arbitrary-sized buffers, but this remains as a
811 * sanity check on untrusted input.
813 if (PATH_MAX <= len)
814 return NULL;
816 strbuf_reset(&used_path);
817 strbuf_reset(&validated_path);
818 strbuf_add(&used_path, path, len);
819 strbuf_add(&validated_path, path, len);
821 if (used_path.buf[0] == '~') {
822 char *newpath = interpolate_path(used_path.buf, 0);
823 if (!newpath)
824 return NULL;
825 strbuf_attach(&used_path, newpath, strlen(newpath),
826 strlen(newpath));
828 for (i = 0; suffix[i]; i++) {
829 struct stat st;
830 size_t baselen = used_path.len;
831 strbuf_addstr(&used_path, suffix[i]);
832 if (!stat(used_path.buf, &st) &&
833 (S_ISREG(st.st_mode) ||
834 (S_ISDIR(st.st_mode) && is_git_directory(used_path.buf)))) {
835 strbuf_addstr(&validated_path, suffix[i]);
836 break;
838 strbuf_setlen(&used_path, baselen);
840 if (!suffix[i])
841 return NULL;
842 gitfile = read_gitfile(used_path.buf);
843 if (gitfile) {
844 strbuf_reset(&used_path);
845 strbuf_addstr(&used_path, gitfile);
847 if (chdir(used_path.buf))
848 return NULL;
849 path = validated_path.buf;
851 else {
852 const char *gitfile = read_gitfile(path);
853 if (gitfile)
854 path = gitfile;
855 if (chdir(path))
856 return NULL;
859 if (is_git_directory(".")) {
860 set_git_dir(".", 0);
861 check_repository_format(NULL);
862 return path;
865 return NULL;
868 static int calc_shared_perm(int mode)
870 int tweak;
872 if (get_shared_repository() < 0)
873 tweak = -get_shared_repository();
874 else
875 tweak = get_shared_repository();
877 if (!(mode & S_IWUSR))
878 tweak &= ~0222;
879 if (mode & S_IXUSR)
880 /* Copy read bits to execute bits */
881 tweak |= (tweak & 0444) >> 2;
882 if (get_shared_repository() < 0)
883 mode = (mode & ~0777) | tweak;
884 else
885 mode |= tweak;
887 return mode;
891 int adjust_shared_perm(const char *path)
893 int old_mode, new_mode;
895 if (!get_shared_repository())
896 return 0;
897 if (get_st_mode_bits(path, &old_mode) < 0)
898 return -1;
900 new_mode = calc_shared_perm(old_mode);
901 if (S_ISDIR(old_mode)) {
902 /* Copy read bits to execute bits */
903 new_mode |= (new_mode & 0444) >> 2;
906 * g+s matters only if any extra access is granted
907 * based on group membership.
909 if (FORCE_DIR_SET_GID && (new_mode & 060))
910 new_mode |= FORCE_DIR_SET_GID;
913 if (((old_mode ^ new_mode) & ~S_IFMT) &&
914 chmod(path, (new_mode & ~S_IFMT)) < 0)
915 return -2;
916 return 0;
919 void safe_create_dir(const char *dir, int share)
921 if (mkdir(dir, 0777) < 0) {
922 if (errno != EEXIST) {
923 perror(dir);
924 exit(1);
927 else if (share && adjust_shared_perm(dir))
928 die(_("Could not make %s writable by group"), dir);
931 static int have_same_root(const char *path1, const char *path2)
933 int is_abs1, is_abs2;
935 is_abs1 = is_absolute_path(path1);
936 is_abs2 = is_absolute_path(path2);
937 return (is_abs1 && is_abs2 && tolower(path1[0]) == tolower(path2[0])) ||
938 (!is_abs1 && !is_abs2);
942 * Give path as relative to prefix.
944 * The strbuf may or may not be used, so do not assume it contains the
945 * returned path.
947 const char *relative_path(const char *in, const char *prefix,
948 struct strbuf *sb)
950 int in_len = in ? strlen(in) : 0;
951 int prefix_len = prefix ? strlen(prefix) : 0;
952 int in_off = 0;
953 int prefix_off = 0;
954 int i = 0, j = 0;
956 if (!in_len)
957 return "./";
958 else if (!prefix_len)
959 return in;
961 if (have_same_root(in, prefix))
962 /* bypass dos_drive, for "c:" is identical to "C:" */
963 i = j = has_dos_drive_prefix(in);
964 else {
965 return in;
968 while (i < prefix_len && j < in_len && prefix[i] == in[j]) {
969 if (is_dir_sep(prefix[i])) {
970 while (is_dir_sep(prefix[i]))
971 i++;
972 while (is_dir_sep(in[j]))
973 j++;
974 prefix_off = i;
975 in_off = j;
976 } else {
977 i++;
978 j++;
982 if (
983 /* "prefix" seems like prefix of "in" */
984 i >= prefix_len &&
986 * but "/foo" is not a prefix of "/foobar"
987 * (i.e. prefix not end with '/')
989 prefix_off < prefix_len) {
990 if (j >= in_len) {
991 /* in="/a/b", prefix="/a/b" */
992 in_off = in_len;
993 } else if (is_dir_sep(in[j])) {
994 /* in="/a/b/c", prefix="/a/b" */
995 while (is_dir_sep(in[j]))
996 j++;
997 in_off = j;
998 } else {
999 /* in="/a/bbb/c", prefix="/a/b" */
1000 i = prefix_off;
1002 } else if (
1003 /* "in" is short than "prefix" */
1004 j >= in_len &&
1005 /* "in" not end with '/' */
1006 in_off < in_len) {
1007 if (is_dir_sep(prefix[i])) {
1008 /* in="/a/b", prefix="/a/b/c/" */
1009 while (is_dir_sep(prefix[i]))
1010 i++;
1011 in_off = in_len;
1014 in += in_off;
1015 in_len -= in_off;
1017 if (i >= prefix_len) {
1018 if (!in_len)
1019 return "./";
1020 else
1021 return in;
1024 strbuf_reset(sb);
1025 strbuf_grow(sb, in_len);
1027 while (i < prefix_len) {
1028 if (is_dir_sep(prefix[i])) {
1029 strbuf_addstr(sb, "../");
1030 while (is_dir_sep(prefix[i]))
1031 i++;
1032 continue;
1034 i++;
1036 if (!is_dir_sep(prefix[prefix_len - 1]))
1037 strbuf_addstr(sb, "../");
1039 strbuf_addstr(sb, in);
1041 return sb->buf;
1045 * A simpler implementation of relative_path
1047 * Get relative path by removing "prefix" from "in". This function
1048 * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter
1049 * to increase performance when traversing the path to work_tree.
1051 const char *remove_leading_path(const char *in, const char *prefix)
1053 static struct strbuf buf = STRBUF_INIT;
1054 int i = 0, j = 0;
1056 if (!prefix || !prefix[0])
1057 return in;
1058 while (prefix[i]) {
1059 if (is_dir_sep(prefix[i])) {
1060 if (!is_dir_sep(in[j]))
1061 return in;
1062 while (is_dir_sep(prefix[i]))
1063 i++;
1064 while (is_dir_sep(in[j]))
1065 j++;
1066 continue;
1067 } else if (in[j] != prefix[i]) {
1068 return in;
1070 i++;
1071 j++;
1073 if (
1074 /* "/foo" is a prefix of "/foo" */
1075 in[j] &&
1076 /* "/foo" is not a prefix of "/foobar" */
1077 !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j])
1079 return in;
1080 while (is_dir_sep(in[j]))
1081 j++;
1083 strbuf_reset(&buf);
1084 if (!in[j])
1085 strbuf_addstr(&buf, ".");
1086 else
1087 strbuf_addstr(&buf, in + j);
1088 return buf.buf;
1092 * It is okay if dst == src, but they should not overlap otherwise.
1093 * The "dst" buffer must be at least as long as "src"; normalizing may shrink
1094 * the size of the path, but will never grow it.
1096 * Performs the following normalizations on src, storing the result in dst:
1097 * - Ensures that components are separated by '/' (Windows only)
1098 * - Squashes sequences of '/' except "//server/share" on Windows
1099 * - Removes "." components.
1100 * - Removes ".." components, and the components the precede them.
1101 * Returns failure (non-zero) if a ".." component appears as first path
1102 * component anytime during the normalization. Otherwise, returns success (0).
1104 * Note that this function is purely textual. It does not follow symlinks,
1105 * verify the existence of the path, or make any system calls.
1107 * prefix_len != NULL is for a specific case of prefix_pathspec():
1108 * assume that src == dst and src[0..prefix_len-1] is already
1109 * normalized, any time "../" eats up to the prefix_len part,
1110 * prefix_len is reduced. In the end prefix_len is the remaining
1111 * prefix that has not been overridden by user pathspec.
1113 * NEEDSWORK: This function doesn't perform normalization w.r.t. trailing '/'.
1114 * For everything but the root folder itself, the normalized path should not
1115 * end with a '/', then the callers need to be fixed up accordingly.
1118 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
1120 char *dst0;
1121 const char *end;
1124 * Copy initial part of absolute path: "/", "C:/", "//server/share/".
1126 end = src + offset_1st_component(src);
1127 while (src < end) {
1128 char c = *src++;
1129 if (is_dir_sep(c))
1130 c = '/';
1131 *dst++ = c;
1133 dst0 = dst;
1135 while (is_dir_sep(*src))
1136 src++;
1138 for (;;) {
1139 char c = *src;
1142 * A path component that begins with . could be
1143 * special:
1144 * (1) "." and ends -- ignore and terminate.
1145 * (2) "./" -- ignore them, eat slash and continue.
1146 * (3) ".." and ends -- strip one and terminate.
1147 * (4) "../" -- strip one, eat slash and continue.
1149 if (c == '.') {
1150 if (!src[1]) {
1151 /* (1) */
1152 src++;
1153 } else if (is_dir_sep(src[1])) {
1154 /* (2) */
1155 src += 2;
1156 while (is_dir_sep(*src))
1157 src++;
1158 continue;
1159 } else if (src[1] == '.') {
1160 if (!src[2]) {
1161 /* (3) */
1162 src += 2;
1163 goto up_one;
1164 } else if (is_dir_sep(src[2])) {
1165 /* (4) */
1166 src += 3;
1167 while (is_dir_sep(*src))
1168 src++;
1169 goto up_one;
1174 /* copy up to the next '/', and eat all '/' */
1175 while ((c = *src++) != '\0' && !is_dir_sep(c))
1176 *dst++ = c;
1177 if (is_dir_sep(c)) {
1178 *dst++ = '/';
1179 while (is_dir_sep(c))
1180 c = *src++;
1181 src--;
1182 } else if (!c)
1183 break;
1184 continue;
1186 up_one:
1188 * dst0..dst is prefix portion, and dst[-1] is '/';
1189 * go up one level.
1191 dst--; /* go to trailing '/' */
1192 if (dst <= dst0)
1193 return -1;
1194 /* Windows: dst[-1] cannot be backslash anymore */
1195 while (dst0 < dst && dst[-1] != '/')
1196 dst--;
1197 if (prefix_len && *prefix_len > dst - dst0)
1198 *prefix_len = dst - dst0;
1200 *dst = '\0';
1201 return 0;
1204 int normalize_path_copy(char *dst, const char *src)
1206 return normalize_path_copy_len(dst, src, NULL);
1210 * path = Canonical absolute path
1211 * prefixes = string_list containing normalized, absolute paths without
1212 * trailing slashes (except for the root directory, which is denoted by "/").
1214 * Determines, for each path in prefixes, whether the "prefix"
1215 * is an ancestor directory of path. Returns the length of the longest
1216 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
1217 * is an ancestor. (Note that this means 0 is returned if prefixes is
1218 * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories
1219 * are not considered to be their own ancestors. path must be in a
1220 * canonical form: empty components, or "." or ".." components are not
1221 * allowed.
1223 int longest_ancestor_length(const char *path, struct string_list *prefixes)
1225 int i, max_len = -1;
1227 if (!strcmp(path, "/"))
1228 return -1;
1230 for (i = 0; i < prefixes->nr; i++) {
1231 const char *ceil = prefixes->items[i].string;
1232 int len = strlen(ceil);
1235 * For root directories (`/`, `C:/`, `//server/share/`)
1236 * adjust the length to exclude the trailing slash.
1238 if (len > 0 && ceil[len - 1] == '/')
1239 len--;
1241 if (strncmp(path, ceil, len) ||
1242 path[len] != '/' || !path[len + 1])
1243 continue; /* no match */
1245 if (len > max_len)
1246 max_len = len;
1249 return max_len;
1252 /* strip arbitrary amount of directory separators at end of path */
1253 static inline int chomp_trailing_dir_sep(const char *path, int len)
1255 while (len && is_dir_sep(path[len - 1]))
1256 len--;
1257 return len;
1261 * If path ends with suffix (complete path components), returns the offset of
1262 * the last character in the path before the suffix (sans trailing directory
1263 * separators), and -1 otherwise.
1265 static ssize_t stripped_path_suffix_offset(const char *path, const char *suffix)
1267 int path_len = strlen(path), suffix_len = strlen(suffix);
1269 while (suffix_len) {
1270 if (!path_len)
1271 return -1;
1273 if (is_dir_sep(path[path_len - 1])) {
1274 if (!is_dir_sep(suffix[suffix_len - 1]))
1275 return -1;
1276 path_len = chomp_trailing_dir_sep(path, path_len);
1277 suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
1279 else if (path[--path_len] != suffix[--suffix_len])
1280 return -1;
1283 if (path_len && !is_dir_sep(path[path_len - 1]))
1284 return -1;
1285 return chomp_trailing_dir_sep(path, path_len);
1289 * Returns true if the path ends with components, considering only complete path
1290 * components, and false otherwise.
1292 int ends_with_path_components(const char *path, const char *components)
1294 return stripped_path_suffix_offset(path, components) != -1;
1298 * If path ends with suffix (complete path components), returns the
1299 * part before suffix (sans trailing directory separators).
1300 * Otherwise returns NULL.
1302 char *strip_path_suffix(const char *path, const char *suffix)
1304 ssize_t offset = stripped_path_suffix_offset(path, suffix);
1306 return offset == -1 ? NULL : xstrndup(path, offset);
1309 int daemon_avoid_alias(const char *p)
1311 int sl, ndot;
1314 * This resurrects the belts and suspenders paranoia check by HPA
1315 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
1316 * does not do getcwd() based path canonicalization.
1318 * sl becomes true immediately after seeing '/' and continues to
1319 * be true as long as dots continue after that without intervening
1320 * non-dot character.
1322 if (!p || (*p != '/' && *p != '~'))
1323 return -1;
1324 sl = 1; ndot = 0;
1325 p++;
1327 while (1) {
1328 char ch = *p++;
1329 if (sl) {
1330 if (ch == '.')
1331 ndot++;
1332 else if (ch == '/') {
1333 if (ndot < 3)
1334 /* reject //, /./ and /../ */
1335 return -1;
1336 ndot = 0;
1338 else if (ch == 0) {
1339 if (0 < ndot && ndot < 3)
1340 /* reject /.$ and /..$ */
1341 return -1;
1342 return 0;
1344 else
1345 sl = ndot = 0;
1347 else if (ch == 0)
1348 return 0;
1349 else if (ch == '/') {
1350 sl = 1;
1351 ndot = 0;
1357 * On NTFS, we need to be careful to disallow certain synonyms of the `.git/`
1358 * directory:
1360 * - For historical reasons, file names that end in spaces or periods are
1361 * automatically trimmed. Therefore, `.git . . ./` is a valid way to refer
1362 * to `.git/`.
1364 * - For other historical reasons, file names that do not conform to the 8.3
1365 * format (up to eight characters for the basename, three for the file
1366 * extension, certain characters not allowed such as `+`, etc) are associated
1367 * with a so-called "short name", at least on the `C:` drive by default.
1368 * Which means that `git~1/` is a valid way to refer to `.git/`.
1370 * Note: Technically, `.git/` could receive the short name `git~2` if the
1371 * short name `git~1` were already used. In Git, however, we guarantee that
1372 * `.git` is the first item in a directory, therefore it will be associated
1373 * with the short name `git~1` (unless short names are disabled).
1375 * - For yet other historical reasons, NTFS supports so-called "Alternate Data
1376 * Streams", i.e. metadata associated with a given file, referred to via
1377 * `<filename>:<stream-name>:<stream-type>`. There exists a default stream
1378 * type for directories, allowing `.git/` to be accessed via
1379 * `.git::$INDEX_ALLOCATION/`.
1381 * When this function returns 1, it indicates that the specified file/directory
1382 * name refers to a `.git` file or directory, or to any of these synonyms, and
1383 * Git should therefore not track it.
1385 * For performance reasons, _all_ Alternate Data Streams of `.git/` are
1386 * forbidden, not just `::$INDEX_ALLOCATION`.
1388 * This function is intended to be used by `git fsck` even on platforms where
1389 * the backslash is a regular filename character, therefore it needs to handle
1390 * backlash characters in the provided `name` specially: they are interpreted
1391 * as directory separators.
1393 int is_ntfs_dotgit(const char *name)
1395 char c;
1398 * Note that when we don't find `.git` or `git~1` we end up with `name`
1399 * advanced partway through the string. That's okay, though, as we
1400 * return immediately in those cases, without looking at `name` any
1401 * further.
1403 c = *(name++);
1404 if (c == '.') {
1405 /* .git */
1406 if (((c = *(name++)) != 'g' && c != 'G') ||
1407 ((c = *(name++)) != 'i' && c != 'I') ||
1408 ((c = *(name++)) != 't' && c != 'T'))
1409 return 0;
1410 } else if (c == 'g' || c == 'G') {
1411 /* git ~1 */
1412 if (((c = *(name++)) != 'i' && c != 'I') ||
1413 ((c = *(name++)) != 't' && c != 'T') ||
1414 *(name++) != '~' ||
1415 *(name++) != '1')
1416 return 0;
1417 } else
1418 return 0;
1420 for (;;) {
1421 c = *(name++);
1422 if (!c || is_xplatform_dir_sep(c) || c == ':')
1423 return 1;
1424 if (c != '.' && c != ' ')
1425 return 0;
1429 static int is_ntfs_dot_generic(const char *name,
1430 const char *dotgit_name,
1431 size_t len,
1432 const char *dotgit_ntfs_shortname_prefix)
1434 int saw_tilde;
1435 size_t i;
1437 if ((name[0] == '.' && !strncasecmp(name + 1, dotgit_name, len))) {
1438 i = len + 1;
1439 only_spaces_and_periods:
1440 for (;;) {
1441 char c = name[i++];
1442 if (!c || c == ':')
1443 return 1;
1444 if (c != ' ' && c != '.')
1445 return 0;
1450 * Is it a regular NTFS short name, i.e. shortened to 6 characters,
1451 * followed by ~1, ... ~4?
1453 if (!strncasecmp(name, dotgit_name, 6) && name[6] == '~' &&
1454 name[7] >= '1' && name[7] <= '4') {
1455 i = 8;
1456 goto only_spaces_and_periods;
1460 * Is it a fall-back NTFS short name (for details, see
1461 * https://en.wikipedia.org/wiki/8.3_filename?
1463 for (i = 0, saw_tilde = 0; i < 8; i++)
1464 if (name[i] == '\0')
1465 return 0;
1466 else if (saw_tilde) {
1467 if (name[i] < '0' || name[i] > '9')
1468 return 0;
1469 } else if (name[i] == '~') {
1470 if (name[++i] < '1' || name[i] > '9')
1471 return 0;
1472 saw_tilde = 1;
1473 } else if (i >= 6)
1474 return 0;
1475 else if (name[i] & 0x80) {
1477 * We know our needles contain only ASCII, so we clamp
1478 * here to make the results of tolower() sane.
1480 return 0;
1481 } else if (tolower(name[i]) != dotgit_ntfs_shortname_prefix[i])
1482 return 0;
1484 goto only_spaces_and_periods;
1488 * Inline helper to make sure compiler resolves strlen() on literals at
1489 * compile time.
1491 static inline int is_ntfs_dot_str(const char *name, const char *dotgit_name,
1492 const char *dotgit_ntfs_shortname_prefix)
1494 return is_ntfs_dot_generic(name, dotgit_name, strlen(dotgit_name),
1495 dotgit_ntfs_shortname_prefix);
1498 int is_ntfs_dotgitmodules(const char *name)
1500 return is_ntfs_dot_str(name, "gitmodules", "gi7eba");
1503 int is_ntfs_dotgitignore(const char *name)
1505 return is_ntfs_dot_str(name, "gitignore", "gi250a");
1508 int is_ntfs_dotgitattributes(const char *name)
1510 return is_ntfs_dot_str(name, "gitattributes", "gi7d29");
1513 int is_ntfs_dotmailmap(const char *name)
1515 return is_ntfs_dot_str(name, "mailmap", "maba30");
1518 int looks_like_command_line_option(const char *str)
1520 return str && str[0] == '-';
1523 char *xdg_config_home_for(const char *subdir, const char *filename)
1525 const char *home, *config_home;
1527 assert(subdir);
1528 assert(filename);
1529 config_home = getenv("XDG_CONFIG_HOME");
1530 if (config_home && *config_home)
1531 return mkpathdup("%s/%s/%s", config_home, subdir, filename);
1533 home = getenv("HOME");
1534 if (home)
1535 return mkpathdup("%s/.config/%s/%s", home, subdir, filename);
1537 return NULL;
1540 char *xdg_config_home(const char *filename)
1542 return xdg_config_home_for("git", filename);
1545 char *xdg_cache_home(const char *filename)
1547 const char *home, *cache_home;
1549 assert(filename);
1550 cache_home = getenv("XDG_CACHE_HOME");
1551 if (cache_home && *cache_home)
1552 return mkpathdup("%s/git/%s", cache_home, filename);
1554 home = getenv("HOME");
1555 if (home)
1556 return mkpathdup("%s/.cache/git/%s", home, filename);
1557 return NULL;
1560 REPO_GIT_PATH_FUNC(squash_msg, "SQUASH_MSG")
1561 REPO_GIT_PATH_FUNC(merge_msg, "MERGE_MSG")
1562 REPO_GIT_PATH_FUNC(merge_rr, "MERGE_RR")
1563 REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE")
1564 REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD")
1565 REPO_GIT_PATH_FUNC(merge_autostash, "MERGE_AUTOSTASH")
1566 REPO_GIT_PATH_FUNC(auto_merge, "AUTO_MERGE")
1567 REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD")
1568 REPO_GIT_PATH_FUNC(shallow, "shallow")