The sixth batch
[alt-git.git] / path.c
blobd838d4be9b6da916ff63c16e343e6bba9de42d08
1 /*
2 * Utilities for paths and pathnames
3 */
4 #include "git-compat-util.h"
5 #include "abspath.h"
6 #include "environment.h"
7 #include "gettext.h"
8 #include "hex.h"
9 #include "repository.h"
10 #include "strbuf.h"
11 #include "string-list.h"
12 #include "dir.h"
13 #include "worktree.h"
14 #include "setup.h"
15 #include "submodule-config.h"
16 #include "path.h"
17 #include "packfile.h"
18 #include "object-store-ll.h"
19 #include "lockfile.h"
20 #include "exec-cmd.h"
22 static int get_st_mode_bits(const char *path, int *mode)
24 struct stat st;
25 if (lstat(path, &st) < 0)
26 return -1;
27 *mode = st.st_mode;
28 return 0;
31 static struct strbuf *get_pathname(void)
33 static struct strbuf pathname_array[4] = {
34 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
36 static int index;
37 struct strbuf *sb = &pathname_array[index];
38 index = (index + 1) % ARRAY_SIZE(pathname_array);
39 strbuf_reset(sb);
40 return sb;
43 static const char *cleanup_path(const char *path)
45 /* Clean it up */
46 if (skip_prefix(path, "./", &path)) {
47 while (*path == '/')
48 path++;
50 return path;
53 static void strbuf_cleanup_path(struct strbuf *sb)
55 const char *path = cleanup_path(sb->buf);
56 if (path > sb->buf)
57 strbuf_remove(sb, 0, path - sb->buf);
60 static int dir_prefix(const char *buf, const char *dir)
62 int len = strlen(dir);
63 return !strncmp(buf, dir, len) &&
64 (is_dir_sep(buf[len]) || buf[len] == '\0');
67 /* $buf =~ m|$dir/+$file| but without regex */
68 static int is_dir_file(const char *buf, const char *dir, const char *file)
70 int len = strlen(dir);
71 if (strncmp(buf, dir, len) || !is_dir_sep(buf[len]))
72 return 0;
73 while (is_dir_sep(buf[len]))
74 len++;
75 return !strcmp(buf + len, file);
78 static void replace_dir(struct strbuf *buf, int len, const char *newdir)
80 int newlen = strlen(newdir);
81 int need_sep = (buf->buf[len] && !is_dir_sep(buf->buf[len])) &&
82 !is_dir_sep(newdir[newlen - 1]);
83 if (need_sep)
84 len--; /* keep one char, to be replaced with '/' */
85 strbuf_splice(buf, 0, len, newdir, newlen);
86 if (need_sep)
87 buf->buf[newlen] = '/';
90 struct common_dir {
91 /* Not considered garbage for report_linked_checkout_garbage */
92 unsigned ignore_garbage:1;
93 unsigned is_dir:1;
94 /* Belongs to the common dir, though it may contain paths that don't */
95 unsigned is_common:1;
96 const char *path;
99 static struct common_dir common_list[] = {
100 { 0, 1, 1, "branches" },
101 { 0, 1, 1, "common" },
102 { 0, 1, 1, "hooks" },
103 { 0, 1, 1, "info" },
104 { 0, 0, 0, "info/sparse-checkout" },
105 { 1, 1, 1, "logs" },
106 { 1, 0, 0, "logs/HEAD" },
107 { 0, 1, 0, "logs/refs/bisect" },
108 { 0, 1, 0, "logs/refs/rewritten" },
109 { 0, 1, 0, "logs/refs/worktree" },
110 { 0, 1, 1, "lost-found" },
111 { 0, 1, 1, "objects" },
112 { 0, 1, 1, "refs" },
113 { 0, 1, 0, "refs/bisect" },
114 { 0, 1, 0, "refs/rewritten" },
115 { 0, 1, 0, "refs/worktree" },
116 { 0, 1, 1, "remotes" },
117 { 0, 1, 1, "worktrees" },
118 { 0, 1, 1, "rr-cache" },
119 { 0, 1, 1, "svn" },
120 { 0, 0, 1, "config" },
121 { 1, 0, 1, "gc.pid" },
122 { 0, 0, 1, "packed-refs" },
123 { 0, 0, 1, "shallow" },
124 { 0, 0, 0, NULL }
128 * A compressed trie. A trie node consists of zero or more characters that
129 * are common to all elements with this prefix, optionally followed by some
130 * children. If value is not NULL, the trie node is a terminal node.
132 * For example, consider the following set of strings:
133 * abc
134 * def
135 * definite
136 * definition
138 * The trie would look like:
139 * root: len = 0, children a and d non-NULL, value = NULL.
140 * a: len = 2, contents = bc, value = (data for "abc")
141 * d: len = 2, contents = ef, children i non-NULL, value = (data for "def")
142 * i: len = 3, contents = nit, children e and i non-NULL, value = NULL
143 * e: len = 0, children all NULL, value = (data for "definite")
144 * i: len = 2, contents = on, children all NULL,
145 * value = (data for "definition")
147 struct trie {
148 struct trie *children[256];
149 int len;
150 char *contents;
151 void *value;
154 static struct trie *make_trie_node(const char *key, void *value)
156 struct trie *new_node = xcalloc(1, sizeof(*new_node));
157 new_node->len = strlen(key);
158 if (new_node->len) {
159 new_node->contents = xmalloc(new_node->len);
160 memcpy(new_node->contents, key, new_node->len);
162 new_node->value = value;
163 return new_node;
167 * Add a key/value pair to a trie. The key is assumed to be \0-terminated.
168 * If there was an existing value for this key, return it.
170 static void *add_to_trie(struct trie *root, const char *key, void *value)
172 struct trie *child;
173 void *old;
174 int i;
176 if (!*key) {
177 /* we have reached the end of the key */
178 old = root->value;
179 root->value = value;
180 return old;
183 for (i = 0; i < root->len; i++) {
184 if (root->contents[i] == key[i])
185 continue;
188 * Split this node: child will contain this node's
189 * existing children.
191 child = xmalloc(sizeof(*child));
192 memcpy(child->children, root->children, sizeof(root->children));
194 child->len = root->len - i - 1;
195 if (child->len) {
196 child->contents = xstrndup(root->contents + i + 1,
197 child->len);
199 child->value = root->value;
200 root->value = NULL;
201 root->len = i;
203 memset(root->children, 0, sizeof(root->children));
204 root->children[(unsigned char)root->contents[i]] = child;
206 /* This is the newly-added child. */
207 root->children[(unsigned char)key[i]] =
208 make_trie_node(key + i + 1, value);
209 return NULL;
212 /* We have matched the entire compressed section */
213 if (key[i]) {
214 child = root->children[(unsigned char)key[root->len]];
215 if (child) {
216 return add_to_trie(child, key + root->len + 1, value);
217 } else {
218 child = make_trie_node(key + root->len + 1, value);
219 root->children[(unsigned char)key[root->len]] = child;
220 return NULL;
224 old = root->value;
225 root->value = value;
226 return old;
229 typedef int (*match_fn)(const char *unmatched, void *value, void *baton);
232 * Search a trie for some key. Find the longest /-or-\0-terminated
233 * prefix of the key for which the trie contains a value. If there is
234 * no such prefix, return -1. Otherwise call fn with the unmatched
235 * portion of the key and the found value. If fn returns 0 or
236 * positive, then return its return value. If fn returns negative,
237 * then call fn with the next-longest /-terminated prefix of the key
238 * (i.e. a parent directory) for which the trie contains a value, and
239 * handle its return value the same way. If there is no shorter
240 * /-terminated prefix with a value left, then return the negative
241 * return value of the most recent fn invocation.
243 * The key is partially normalized: consecutive slashes are skipped.
245 * For example, consider the trie containing only [logs,
246 * logs/refs/bisect], both with values, but not logs/refs.
248 * | key | unmatched | prefix to node | return value |
249 * |--------------------|----------------|------------------|--------------|
250 * | a | not called | n/a | -1 |
251 * | logstore | not called | n/a | -1 |
252 * | logs | \0 | logs | as per fn |
253 * | logs/ | / | logs | as per fn |
254 * | logs/refs | /refs | logs | as per fn |
255 * | logs/refs/ | /refs/ | logs | as per fn |
256 * | logs/refs/b | /refs/b | logs | as per fn |
257 * | logs/refs/bisected | /refs/bisected | logs | as per fn |
258 * | logs/refs/bisect | \0 | logs/refs/bisect | as per fn |
259 * | logs/refs/bisect/ | / | logs/refs/bisect | as per fn |
260 * | logs/refs/bisect/a | /a | logs/refs/bisect | as per fn |
261 * | (If fn in the previous line returns -1, then fn is called once more:) |
262 * | logs/refs/bisect/a | /refs/bisect/a | logs | as per fn |
263 * |--------------------|----------------|------------------|--------------|
265 static int trie_find(struct trie *root, const char *key, match_fn fn,
266 void *baton)
268 int i;
269 int result;
270 struct trie *child;
272 if (!*key) {
273 /* we have reached the end of the key */
274 if (root->value && !root->len)
275 return fn(key, root->value, baton);
276 else
277 return -1;
280 for (i = 0; i < root->len; i++) {
281 /* Partial path normalization: skip consecutive slashes. */
282 if (key[i] == '/' && key[i+1] == '/') {
283 key++;
284 continue;
286 if (root->contents[i] != key[i])
287 return -1;
290 /* Matched the entire compressed section */
291 key += i;
292 if (!*key) {
293 /* End of key */
294 if (root->value)
295 return fn(key, root->value, baton);
296 else
297 return -1;
300 /* Partial path normalization: skip consecutive slashes */
301 while (key[0] == '/' && key[1] == '/')
302 key++;
304 child = root->children[(unsigned char)*key];
305 if (child)
306 result = trie_find(child, key + 1, fn, baton);
307 else
308 result = -1;
310 if (result >= 0 || (*key != '/' && *key != 0))
311 return result;
312 if (root->value)
313 return fn(key, root->value, baton);
314 else
315 return -1;
318 static struct trie common_trie;
319 static int common_trie_done_setup;
321 static void init_common_trie(void)
323 struct common_dir *p;
325 if (common_trie_done_setup)
326 return;
328 for (p = common_list; p->path; p++)
329 add_to_trie(&common_trie, p->path, p);
331 common_trie_done_setup = 1;
335 * Helper function for update_common_dir: returns 1 if the dir
336 * prefix is common.
338 static int check_common(const char *unmatched, void *value,
339 void *baton UNUSED)
341 struct common_dir *dir = value;
343 if (dir->is_dir && (unmatched[0] == 0 || unmatched[0] == '/'))
344 return dir->is_common;
346 if (!dir->is_dir && unmatched[0] == 0)
347 return dir->is_common;
349 return 0;
352 static void update_common_dir(struct strbuf *buf, int git_dir_len,
353 const char *common_dir)
355 char *base = buf->buf + git_dir_len;
356 int has_lock_suffix = strbuf_strip_suffix(buf, LOCK_SUFFIX);
358 init_common_trie();
359 if (trie_find(&common_trie, base, check_common, NULL) > 0)
360 replace_dir(buf, git_dir_len, common_dir);
362 if (has_lock_suffix)
363 strbuf_addstr(buf, LOCK_SUFFIX);
366 void report_linked_checkout_garbage(void)
368 struct strbuf sb = STRBUF_INIT;
369 const struct common_dir *p;
370 int len;
372 if (!the_repository->different_commondir)
373 return;
374 strbuf_addf(&sb, "%s/", get_git_dir());
375 len = sb.len;
376 for (p = common_list; p->path; p++) {
377 const char *path = p->path;
378 if (p->ignore_garbage)
379 continue;
380 strbuf_setlen(&sb, len);
381 strbuf_addstr(&sb, path);
382 if (file_exists(sb.buf))
383 report_garbage(PACKDIR_FILE_GARBAGE, sb.buf);
385 strbuf_release(&sb);
388 static void adjust_git_path(const struct repository *repo,
389 struct strbuf *buf, int git_dir_len)
391 const char *base = buf->buf + git_dir_len;
392 if (is_dir_file(base, "info", "grafts"))
393 strbuf_splice(buf, 0, buf->len,
394 repo->graft_file, strlen(repo->graft_file));
395 else if (!strcmp(base, "index"))
396 strbuf_splice(buf, 0, buf->len,
397 repo->index_file, strlen(repo->index_file));
398 else if (dir_prefix(base, "objects"))
399 replace_dir(buf, git_dir_len + 7, repo->objects->odb->path);
400 else if (git_hooks_path && dir_prefix(base, "hooks"))
401 replace_dir(buf, git_dir_len + 5, git_hooks_path);
402 else if (repo->different_commondir)
403 update_common_dir(buf, git_dir_len, repo->commondir);
406 static void strbuf_worktree_gitdir(struct strbuf *buf,
407 const struct repository *repo,
408 const struct worktree *wt)
410 if (!wt)
411 strbuf_addstr(buf, repo->gitdir);
412 else if (!wt->id)
413 strbuf_addstr(buf, repo->commondir);
414 else
415 strbuf_git_common_path(buf, repo, "worktrees/%s", wt->id);
418 static void do_git_path(const struct repository *repo,
419 const struct worktree *wt, struct strbuf *buf,
420 const char *fmt, va_list args)
422 int gitdir_len;
423 strbuf_worktree_gitdir(buf, repo, wt);
424 if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
425 strbuf_addch(buf, '/');
426 gitdir_len = buf->len;
427 strbuf_vaddf(buf, fmt, args);
428 if (!wt)
429 adjust_git_path(repo, buf, gitdir_len);
430 strbuf_cleanup_path(buf);
433 char *repo_git_path(const struct repository *repo,
434 const char *fmt, ...)
436 struct strbuf path = STRBUF_INIT;
437 va_list args;
438 va_start(args, fmt);
439 do_git_path(repo, NULL, &path, fmt, args);
440 va_end(args);
441 return strbuf_detach(&path, NULL);
444 void strbuf_repo_git_path(struct strbuf *sb,
445 const struct repository *repo,
446 const char *fmt, ...)
448 va_list args;
449 va_start(args, fmt);
450 do_git_path(repo, NULL, sb, fmt, args);
451 va_end(args);
454 char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
456 va_list args;
457 strbuf_reset(buf);
458 va_start(args, fmt);
459 do_git_path(the_repository, NULL, buf, fmt, args);
460 va_end(args);
461 return buf->buf;
464 void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
466 va_list args;
467 va_start(args, fmt);
468 do_git_path(the_repository, NULL, sb, fmt, args);
469 va_end(args);
472 const char *git_path(const char *fmt, ...)
474 struct strbuf *pathname = get_pathname();
475 va_list args;
476 va_start(args, fmt);
477 do_git_path(the_repository, NULL, pathname, fmt, args);
478 va_end(args);
479 return pathname->buf;
482 char *git_pathdup(const char *fmt, ...)
484 struct strbuf path = STRBUF_INIT;
485 va_list args;
486 va_start(args, fmt);
487 do_git_path(the_repository, NULL, &path, fmt, args);
488 va_end(args);
489 return strbuf_detach(&path, NULL);
492 char *mkpathdup(const char *fmt, ...)
494 struct strbuf sb = STRBUF_INIT;
495 va_list args;
496 va_start(args, fmt);
497 strbuf_vaddf(&sb, fmt, args);
498 va_end(args);
499 strbuf_cleanup_path(&sb);
500 return strbuf_detach(&sb, NULL);
503 const char *mkpath(const char *fmt, ...)
505 va_list args;
506 struct strbuf *pathname = get_pathname();
507 va_start(args, fmt);
508 strbuf_vaddf(pathname, fmt, args);
509 va_end(args);
510 return cleanup_path(pathname->buf);
513 const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...)
515 struct strbuf *pathname = get_pathname();
516 va_list args;
517 va_start(args, fmt);
518 do_git_path(the_repository, wt, pathname, fmt, args);
519 va_end(args);
520 return pathname->buf;
523 static void do_worktree_path(const struct repository *repo,
524 struct strbuf *buf,
525 const char *fmt, va_list args)
527 strbuf_addstr(buf, repo->worktree);
528 if(buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
529 strbuf_addch(buf, '/');
531 strbuf_vaddf(buf, fmt, args);
532 strbuf_cleanup_path(buf);
535 char *repo_worktree_path(const struct repository *repo, const char *fmt, ...)
537 struct strbuf path = STRBUF_INIT;
538 va_list args;
540 if (!repo->worktree)
541 return NULL;
543 va_start(args, fmt);
544 do_worktree_path(repo, &path, fmt, args);
545 va_end(args);
547 return strbuf_detach(&path, NULL);
550 void strbuf_repo_worktree_path(struct strbuf *sb,
551 const struct repository *repo,
552 const char *fmt, ...)
554 va_list args;
556 if (!repo->worktree)
557 return;
559 va_start(args, fmt);
560 do_worktree_path(repo, sb, fmt, args);
561 va_end(args);
564 /* Returns 0 on success, negative on failure. */
565 static int do_submodule_path(struct strbuf *buf, const char *path,
566 const char *fmt, va_list args)
568 struct strbuf git_submodule_common_dir = STRBUF_INIT;
569 struct strbuf git_submodule_dir = STRBUF_INIT;
570 int ret;
572 ret = submodule_to_gitdir(&git_submodule_dir, path);
573 if (ret)
574 goto cleanup;
576 strbuf_complete(&git_submodule_dir, '/');
577 strbuf_addbuf(buf, &git_submodule_dir);
578 strbuf_vaddf(buf, fmt, args);
580 if (get_common_dir_noenv(&git_submodule_common_dir, git_submodule_dir.buf))
581 update_common_dir(buf, git_submodule_dir.len, git_submodule_common_dir.buf);
583 strbuf_cleanup_path(buf);
585 cleanup:
586 strbuf_release(&git_submodule_dir);
587 strbuf_release(&git_submodule_common_dir);
588 return ret;
591 char *git_pathdup_submodule(const char *path, const char *fmt, ...)
593 int err;
594 va_list args;
595 struct strbuf buf = STRBUF_INIT;
596 va_start(args, fmt);
597 err = do_submodule_path(&buf, path, fmt, args);
598 va_end(args);
599 if (err) {
600 strbuf_release(&buf);
601 return NULL;
603 return strbuf_detach(&buf, NULL);
606 int strbuf_git_path_submodule(struct strbuf *buf, const char *path,
607 const char *fmt, ...)
609 int err;
610 va_list args;
611 va_start(args, fmt);
612 err = do_submodule_path(buf, path, fmt, args);
613 va_end(args);
615 return err;
618 static void do_git_common_path(const struct repository *repo,
619 struct strbuf *buf,
620 const char *fmt,
621 va_list args)
623 strbuf_addstr(buf, repo->commondir);
624 if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
625 strbuf_addch(buf, '/');
626 strbuf_vaddf(buf, fmt, args);
627 strbuf_cleanup_path(buf);
630 const char *git_common_path(const char *fmt, ...)
632 struct strbuf *pathname = get_pathname();
633 va_list args;
634 va_start(args, fmt);
635 do_git_common_path(the_repository, pathname, fmt, args);
636 va_end(args);
637 return pathname->buf;
640 void strbuf_git_common_path(struct strbuf *sb,
641 const struct repository *repo,
642 const char *fmt, ...)
644 va_list args;
645 va_start(args, fmt);
646 do_git_common_path(repo, sb, fmt, args);
647 va_end(args);
650 int validate_headref(const char *path)
652 struct stat st;
653 char buffer[256];
654 const char *refname;
655 struct object_id oid;
656 int fd;
657 ssize_t len;
659 if (lstat(path, &st) < 0)
660 return -1;
662 /* Make sure it is a "refs/.." symlink */
663 if (S_ISLNK(st.st_mode)) {
664 len = readlink(path, buffer, sizeof(buffer)-1);
665 if (len >= 5 && !memcmp("refs/", buffer, 5))
666 return 0;
667 return -1;
671 * Anything else, just open it and try to see if it is a symbolic ref.
673 fd = open(path, O_RDONLY);
674 if (fd < 0)
675 return -1;
676 len = read_in_full(fd, buffer, sizeof(buffer)-1);
677 close(fd);
679 if (len < 0)
680 return -1;
681 buffer[len] = '\0';
684 * Is it a symbolic ref?
686 if (skip_prefix(buffer, "ref:", &refname)) {
687 while (isspace(*refname))
688 refname++;
689 if (starts_with(refname, "refs/"))
690 return 0;
694 * Is this a detached HEAD?
696 if (!get_oid_hex(buffer, &oid))
697 return 0;
699 return -1;
702 static struct passwd *getpw_str(const char *username, size_t len)
704 struct passwd *pw;
705 char *username_z = xmemdupz(username, len);
706 pw = getpwnam(username_z);
707 free(username_z);
708 return pw;
712 * Return a string with ~ and ~user expanded via getpw*. Returns NULL on getpw
713 * failure or if path is NULL.
715 * If real_home is true, strbuf_realpath($HOME) is used in the `~/` expansion.
717 * If the path starts with `%(prefix)/`, the remainder is interpreted as
718 * relative to where Git is installed, and expanded to the absolute path.
720 char *interpolate_path(const char *path, int real_home)
722 struct strbuf user_path = STRBUF_INIT;
723 const char *to_copy = path;
725 if (!path)
726 goto return_null;
728 if (skip_prefix(path, "%(prefix)/", &path))
729 return system_path(path);
731 if (path[0] == '~') {
732 const char *first_slash = strchrnul(path, '/');
733 const char *username = path + 1;
734 size_t username_len = first_slash - username;
735 if (username_len == 0) {
736 const char *home = getenv("HOME");
737 if (!home)
738 goto return_null;
739 if (real_home)
740 strbuf_add_real_path(&user_path, home);
741 else
742 strbuf_addstr(&user_path, home);
743 #ifdef GIT_WINDOWS_NATIVE
744 convert_slashes(user_path.buf);
745 #endif
746 } else {
747 struct passwd *pw = getpw_str(username, username_len);
748 if (!pw)
749 goto return_null;
750 strbuf_addstr(&user_path, pw->pw_dir);
752 to_copy = first_slash;
754 strbuf_addstr(&user_path, to_copy);
755 return strbuf_detach(&user_path, NULL);
756 return_null:
757 strbuf_release(&user_path);
758 return NULL;
762 * First, one directory to try is determined by the following algorithm.
764 * (0) If "strict" is given, the path is used as given and no DWIM is
765 * done. Otherwise:
766 * (1) "~/path" to mean path under the running user's home directory;
767 * (2) "~user/path" to mean path under named user's home directory;
768 * (3) "relative/path" to mean cwd relative directory; or
769 * (4) "/absolute/path" to mean absolute directory.
771 * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git"
772 * in this order. We select the first one that is a valid git repository, and
773 * chdir() to it. If none match, or we fail to chdir, we return NULL.
775 * If all goes well, we return the directory we used to chdir() (but
776 * before ~user is expanded), avoiding getcwd() resolving symbolic
777 * links. User relative paths are also returned as they are given,
778 * except DWIM suffixing.
780 const char *enter_repo(const char *path, int strict)
782 static struct strbuf validated_path = STRBUF_INIT;
783 static struct strbuf used_path = STRBUF_INIT;
785 if (!path)
786 return NULL;
788 if (!strict) {
789 static const char *suffix[] = {
790 "/.git", "", ".git/.git", ".git", NULL,
792 const char *gitfile;
793 int len = strlen(path);
794 int i;
795 while ((1 < len) && (path[len-1] == '/'))
796 len--;
799 * We can handle arbitrary-sized buffers, but this remains as a
800 * sanity check on untrusted input.
802 if (PATH_MAX <= len)
803 return NULL;
805 strbuf_reset(&used_path);
806 strbuf_reset(&validated_path);
807 strbuf_add(&used_path, path, len);
808 strbuf_add(&validated_path, path, len);
810 if (used_path.buf[0] == '~') {
811 char *newpath = interpolate_path(used_path.buf, 0);
812 if (!newpath)
813 return NULL;
814 strbuf_attach(&used_path, newpath, strlen(newpath),
815 strlen(newpath));
817 for (i = 0; suffix[i]; i++) {
818 struct stat st;
819 size_t baselen = used_path.len;
820 strbuf_addstr(&used_path, suffix[i]);
821 if (!stat(used_path.buf, &st) &&
822 (S_ISREG(st.st_mode) ||
823 (S_ISDIR(st.st_mode) && is_git_directory(used_path.buf)))) {
824 strbuf_addstr(&validated_path, suffix[i]);
825 break;
827 strbuf_setlen(&used_path, baselen);
829 if (!suffix[i])
830 return NULL;
831 gitfile = read_gitfile(used_path.buf);
832 die_upon_dubious_ownership(gitfile, NULL, used_path.buf);
833 if (gitfile) {
834 strbuf_reset(&used_path);
835 strbuf_addstr(&used_path, gitfile);
837 if (chdir(used_path.buf))
838 return NULL;
839 path = validated_path.buf;
841 else {
842 const char *gitfile = read_gitfile(path);
843 die_upon_dubious_ownership(gitfile, NULL, path);
844 if (gitfile)
845 path = gitfile;
846 if (chdir(path))
847 return NULL;
850 if (is_git_directory(".")) {
851 set_git_dir(".", 0);
852 check_repository_format(NULL);
853 return path;
856 return NULL;
859 int calc_shared_perm(int mode)
861 int tweak;
863 if (get_shared_repository() < 0)
864 tweak = -get_shared_repository();
865 else
866 tweak = get_shared_repository();
868 if (!(mode & S_IWUSR))
869 tweak &= ~0222;
870 if (mode & S_IXUSR)
871 /* Copy read bits to execute bits */
872 tweak |= (tweak & 0444) >> 2;
873 if (get_shared_repository() < 0)
874 mode = (mode & ~0777) | tweak;
875 else
876 mode |= tweak;
878 return mode;
882 int adjust_shared_perm(const char *path)
884 int old_mode, new_mode;
886 if (!get_shared_repository())
887 return 0;
888 if (get_st_mode_bits(path, &old_mode) < 0)
889 return -1;
891 new_mode = calc_shared_perm(old_mode);
892 if (S_ISDIR(old_mode)) {
893 /* Copy read bits to execute bits */
894 new_mode |= (new_mode & 0444) >> 2;
897 * g+s matters only if any extra access is granted
898 * based on group membership.
900 if (FORCE_DIR_SET_GID && (new_mode & 060))
901 new_mode |= FORCE_DIR_SET_GID;
904 if (((old_mode ^ new_mode) & ~S_IFMT) &&
905 chmod(path, (new_mode & ~S_IFMT)) < 0)
906 return -2;
907 return 0;
910 void safe_create_dir(const char *dir, int share)
912 if (mkdir(dir, 0777) < 0) {
913 if (errno != EEXIST) {
914 perror(dir);
915 exit(1);
918 else if (share && adjust_shared_perm(dir))
919 die(_("Could not make %s writable by group"), dir);
922 static int have_same_root(const char *path1, const char *path2)
924 int is_abs1, is_abs2;
926 is_abs1 = is_absolute_path(path1);
927 is_abs2 = is_absolute_path(path2);
928 return (is_abs1 && is_abs2 && tolower(path1[0]) == tolower(path2[0])) ||
929 (!is_abs1 && !is_abs2);
933 * Give path as relative to prefix.
935 * The strbuf may or may not be used, so do not assume it contains the
936 * returned path.
938 const char *relative_path(const char *in, const char *prefix,
939 struct strbuf *sb)
941 int in_len = in ? strlen(in) : 0;
942 int prefix_len = prefix ? strlen(prefix) : 0;
943 int in_off = 0;
944 int prefix_off = 0;
945 int i = 0, j = 0;
947 if (!in_len)
948 return "./";
949 else if (!prefix_len)
950 return in;
952 if (have_same_root(in, prefix))
953 /* bypass dos_drive, for "c:" is identical to "C:" */
954 i = j = has_dos_drive_prefix(in);
955 else {
956 return in;
959 while (i < prefix_len && j < in_len && prefix[i] == in[j]) {
960 if (is_dir_sep(prefix[i])) {
961 while (is_dir_sep(prefix[i]))
962 i++;
963 while (is_dir_sep(in[j]))
964 j++;
965 prefix_off = i;
966 in_off = j;
967 } else {
968 i++;
969 j++;
973 if (
974 /* "prefix" seems like prefix of "in" */
975 i >= prefix_len &&
977 * but "/foo" is not a prefix of "/foobar"
978 * (i.e. prefix not end with '/')
980 prefix_off < prefix_len) {
981 if (j >= in_len) {
982 /* in="/a/b", prefix="/a/b" */
983 in_off = in_len;
984 } else if (is_dir_sep(in[j])) {
985 /* in="/a/b/c", prefix="/a/b" */
986 while (is_dir_sep(in[j]))
987 j++;
988 in_off = j;
989 } else {
990 /* in="/a/bbb/c", prefix="/a/b" */
991 i = prefix_off;
993 } else if (
994 /* "in" is short than "prefix" */
995 j >= in_len &&
996 /* "in" not end with '/' */
997 in_off < in_len) {
998 if (is_dir_sep(prefix[i])) {
999 /* in="/a/b", prefix="/a/b/c/" */
1000 while (is_dir_sep(prefix[i]))
1001 i++;
1002 in_off = in_len;
1005 in += in_off;
1006 in_len -= in_off;
1008 if (i >= prefix_len) {
1009 if (!in_len)
1010 return "./";
1011 else
1012 return in;
1015 strbuf_reset(sb);
1016 strbuf_grow(sb, in_len);
1018 while (i < prefix_len) {
1019 if (is_dir_sep(prefix[i])) {
1020 strbuf_addstr(sb, "../");
1021 while (is_dir_sep(prefix[i]))
1022 i++;
1023 continue;
1025 i++;
1027 if (!is_dir_sep(prefix[prefix_len - 1]))
1028 strbuf_addstr(sb, "../");
1030 strbuf_addstr(sb, in);
1032 return sb->buf;
1036 * A simpler implementation of relative_path
1038 * Get relative path by removing "prefix" from "in". This function
1039 * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter
1040 * to increase performance when traversing the path to work_tree.
1042 const char *remove_leading_path(const char *in, const char *prefix)
1044 static struct strbuf buf = STRBUF_INIT;
1045 int i = 0, j = 0;
1047 if (!prefix || !prefix[0])
1048 return in;
1049 while (prefix[i]) {
1050 if (is_dir_sep(prefix[i])) {
1051 if (!is_dir_sep(in[j]))
1052 return in;
1053 while (is_dir_sep(prefix[i]))
1054 i++;
1055 while (is_dir_sep(in[j]))
1056 j++;
1057 continue;
1058 } else if (in[j] != prefix[i]) {
1059 return in;
1061 i++;
1062 j++;
1064 if (
1065 /* "/foo" is a prefix of "/foo" */
1066 in[j] &&
1067 /* "/foo" is not a prefix of "/foobar" */
1068 !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j])
1070 return in;
1071 while (is_dir_sep(in[j]))
1072 j++;
1074 strbuf_reset(&buf);
1075 if (!in[j])
1076 strbuf_addstr(&buf, ".");
1077 else
1078 strbuf_addstr(&buf, in + j);
1079 return buf.buf;
1083 * It is okay if dst == src, but they should not overlap otherwise.
1084 * The "dst" buffer must be at least as long as "src"; normalizing may shrink
1085 * the size of the path, but will never grow it.
1087 * Performs the following normalizations on src, storing the result in dst:
1088 * - Ensures that components are separated by '/' (Windows only)
1089 * - Squashes sequences of '/' except "//server/share" on Windows
1090 * - Removes "." components.
1091 * - Removes ".." components, and the components the precede them.
1092 * Returns failure (non-zero) if a ".." component appears as first path
1093 * component anytime during the normalization. Otherwise, returns success (0).
1095 * Note that this function is purely textual. It does not follow symlinks,
1096 * verify the existence of the path, or make any system calls.
1098 * prefix_len != NULL is for a specific case of prefix_pathspec():
1099 * assume that src == dst and src[0..prefix_len-1] is already
1100 * normalized, any time "../" eats up to the prefix_len part,
1101 * prefix_len is reduced. In the end prefix_len is the remaining
1102 * prefix that has not been overridden by user pathspec.
1104 * NEEDSWORK: This function doesn't perform normalization w.r.t. trailing '/'.
1105 * For everything but the root folder itself, the normalized path should not
1106 * end with a '/', then the callers need to be fixed up accordingly.
1109 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
1111 char *dst0;
1112 const char *end;
1115 * Copy initial part of absolute path: "/", "C:/", "//server/share/".
1117 end = src + offset_1st_component(src);
1118 while (src < end) {
1119 char c = *src++;
1120 if (is_dir_sep(c))
1121 c = '/';
1122 *dst++ = c;
1124 dst0 = dst;
1126 while (is_dir_sep(*src))
1127 src++;
1129 for (;;) {
1130 char c = *src;
1133 * A path component that begins with . could be
1134 * special:
1135 * (1) "." and ends -- ignore and terminate.
1136 * (2) "./" -- ignore them, eat slash and continue.
1137 * (3) ".." and ends -- strip one and terminate.
1138 * (4) "../" -- strip one, eat slash and continue.
1140 if (c == '.') {
1141 if (!src[1]) {
1142 /* (1) */
1143 src++;
1144 } else if (is_dir_sep(src[1])) {
1145 /* (2) */
1146 src += 2;
1147 while (is_dir_sep(*src))
1148 src++;
1149 continue;
1150 } else if (src[1] == '.') {
1151 if (!src[2]) {
1152 /* (3) */
1153 src += 2;
1154 goto up_one;
1155 } else if (is_dir_sep(src[2])) {
1156 /* (4) */
1157 src += 3;
1158 while (is_dir_sep(*src))
1159 src++;
1160 goto up_one;
1165 /* copy up to the next '/', and eat all '/' */
1166 while ((c = *src++) != '\0' && !is_dir_sep(c))
1167 *dst++ = c;
1168 if (is_dir_sep(c)) {
1169 *dst++ = '/';
1170 while (is_dir_sep(c))
1171 c = *src++;
1172 src--;
1173 } else if (!c)
1174 break;
1175 continue;
1177 up_one:
1179 * dst0..dst is prefix portion, and dst[-1] is '/';
1180 * go up one level.
1182 dst--; /* go to trailing '/' */
1183 if (dst <= dst0)
1184 return -1;
1185 /* Windows: dst[-1] cannot be backslash anymore */
1186 while (dst0 < dst && dst[-1] != '/')
1187 dst--;
1188 if (prefix_len && *prefix_len > dst - dst0)
1189 *prefix_len = dst - dst0;
1191 *dst = '\0';
1192 return 0;
1195 int normalize_path_copy(char *dst, const char *src)
1197 return normalize_path_copy_len(dst, src, NULL);
1200 int strbuf_normalize_path(struct strbuf *src)
1202 struct strbuf dst = STRBUF_INIT;
1204 strbuf_grow(&dst, src->len);
1205 if (normalize_path_copy(dst.buf, src->buf) < 0) {
1206 strbuf_release(&dst);
1207 return -1;
1211 * normalize_path does not tell us the new length, so we have to
1212 * compute it by looking for the new NUL it placed
1214 strbuf_setlen(&dst, strlen(dst.buf));
1215 strbuf_swap(src, &dst);
1216 strbuf_release(&dst);
1217 return 0;
1221 * path = Canonical absolute path
1222 * prefixes = string_list containing normalized, absolute paths without
1223 * trailing slashes (except for the root directory, which is denoted by "/").
1225 * Determines, for each path in prefixes, whether the "prefix"
1226 * is an ancestor directory of path. Returns the length of the longest
1227 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
1228 * is an ancestor. (Note that this means 0 is returned if prefixes is
1229 * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories
1230 * are not considered to be their own ancestors. path must be in a
1231 * canonical form: empty components, or "." or ".." components are not
1232 * allowed.
1234 int longest_ancestor_length(const char *path, struct string_list *prefixes)
1236 int i, max_len = -1;
1238 if (!strcmp(path, "/"))
1239 return -1;
1241 for (i = 0; i < prefixes->nr; i++) {
1242 const char *ceil = prefixes->items[i].string;
1243 int len = strlen(ceil);
1246 * For root directories (`/`, `C:/`, `//server/share/`)
1247 * adjust the length to exclude the trailing slash.
1249 if (len > 0 && ceil[len - 1] == '/')
1250 len--;
1252 if (strncmp(path, ceil, len) ||
1253 path[len] != '/' || !path[len + 1])
1254 continue; /* no match */
1256 if (len > max_len)
1257 max_len = len;
1260 return max_len;
1263 /* strip arbitrary amount of directory separators at end of path */
1264 static inline int chomp_trailing_dir_sep(const char *path, int len)
1266 while (len && is_dir_sep(path[len - 1]))
1267 len--;
1268 return len;
1272 * If path ends with suffix (complete path components), returns the offset of
1273 * the last character in the path before the suffix (sans trailing directory
1274 * separators), and -1 otherwise.
1276 static ssize_t stripped_path_suffix_offset(const char *path, const char *suffix)
1278 int path_len = strlen(path), suffix_len = strlen(suffix);
1280 while (suffix_len) {
1281 if (!path_len)
1282 return -1;
1284 if (is_dir_sep(path[path_len - 1])) {
1285 if (!is_dir_sep(suffix[suffix_len - 1]))
1286 return -1;
1287 path_len = chomp_trailing_dir_sep(path, path_len);
1288 suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
1290 else if (path[--path_len] != suffix[--suffix_len])
1291 return -1;
1294 if (path_len && !is_dir_sep(path[path_len - 1]))
1295 return -1;
1296 return chomp_trailing_dir_sep(path, path_len);
1300 * Returns true if the path ends with components, considering only complete path
1301 * components, and false otherwise.
1303 int ends_with_path_components(const char *path, const char *components)
1305 return stripped_path_suffix_offset(path, components) != -1;
1309 * If path ends with suffix (complete path components), returns the
1310 * part before suffix (sans trailing directory separators).
1311 * Otherwise returns NULL.
1313 char *strip_path_suffix(const char *path, const char *suffix)
1315 ssize_t offset = stripped_path_suffix_offset(path, suffix);
1317 return offset == -1 ? NULL : xstrndup(path, offset);
1320 int daemon_avoid_alias(const char *p)
1322 int sl, ndot;
1325 * This resurrects the belts and suspenders paranoia check by HPA
1326 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
1327 * does not do getcwd() based path canonicalization.
1329 * sl becomes true immediately after seeing '/' and continues to
1330 * be true as long as dots continue after that without intervening
1331 * non-dot character.
1333 if (!p || (*p != '/' && *p != '~'))
1334 return -1;
1335 sl = 1; ndot = 0;
1336 p++;
1338 while (1) {
1339 char ch = *p++;
1340 if (sl) {
1341 if (ch == '.')
1342 ndot++;
1343 else if (ch == '/') {
1344 if (ndot < 3)
1345 /* reject //, /./ and /../ */
1346 return -1;
1347 ndot = 0;
1349 else if (ch == 0) {
1350 if (0 < ndot && ndot < 3)
1351 /* reject /.$ and /..$ */
1352 return -1;
1353 return 0;
1355 else
1356 sl = ndot = 0;
1358 else if (ch == 0)
1359 return 0;
1360 else if (ch == '/') {
1361 sl = 1;
1362 ndot = 0;
1368 * On NTFS, we need to be careful to disallow certain synonyms of the `.git/`
1369 * directory:
1371 * - For historical reasons, file names that end in spaces or periods are
1372 * automatically trimmed. Therefore, `.git . . ./` is a valid way to refer
1373 * to `.git/`.
1375 * - For other historical reasons, file names that do not conform to the 8.3
1376 * format (up to eight characters for the basename, three for the file
1377 * extension, certain characters not allowed such as `+`, etc) are associated
1378 * with a so-called "short name", at least on the `C:` drive by default.
1379 * Which means that `git~1/` is a valid way to refer to `.git/`.
1381 * Note: Technically, `.git/` could receive the short name `git~2` if the
1382 * short name `git~1` were already used. In Git, however, we guarantee that
1383 * `.git` is the first item in a directory, therefore it will be associated
1384 * with the short name `git~1` (unless short names are disabled).
1386 * - For yet other historical reasons, NTFS supports so-called "Alternate Data
1387 * Streams", i.e. metadata associated with a given file, referred to via
1388 * `<filename>:<stream-name>:<stream-type>`. There exists a default stream
1389 * type for directories, allowing `.git/` to be accessed via
1390 * `.git::$INDEX_ALLOCATION/`.
1392 * When this function returns 1, it indicates that the specified file/directory
1393 * name refers to a `.git` file or directory, or to any of these synonyms, and
1394 * Git should therefore not track it.
1396 * For performance reasons, _all_ Alternate Data Streams of `.git/` are
1397 * forbidden, not just `::$INDEX_ALLOCATION`.
1399 * This function is intended to be used by `git fsck` even on platforms where
1400 * the backslash is a regular filename character, therefore it needs to handle
1401 * backlash characters in the provided `name` specially: they are interpreted
1402 * as directory separators.
1404 int is_ntfs_dotgit(const char *name)
1406 char c;
1409 * Note that when we don't find `.git` or `git~1` we end up with `name`
1410 * advanced partway through the string. That's okay, though, as we
1411 * return immediately in those cases, without looking at `name` any
1412 * further.
1414 c = *(name++);
1415 if (c == '.') {
1416 /* .git */
1417 if (((c = *(name++)) != 'g' && c != 'G') ||
1418 ((c = *(name++)) != 'i' && c != 'I') ||
1419 ((c = *(name++)) != 't' && c != 'T'))
1420 return 0;
1421 } else if (c == 'g' || c == 'G') {
1422 /* git ~1 */
1423 if (((c = *(name++)) != 'i' && c != 'I') ||
1424 ((c = *(name++)) != 't' && c != 'T') ||
1425 *(name++) != '~' ||
1426 *(name++) != '1')
1427 return 0;
1428 } else
1429 return 0;
1431 for (;;) {
1432 c = *(name++);
1433 if (!c || is_xplatform_dir_sep(c) || c == ':')
1434 return 1;
1435 if (c != '.' && c != ' ')
1436 return 0;
1440 static int is_ntfs_dot_generic(const char *name,
1441 const char *dotgit_name,
1442 size_t len,
1443 const char *dotgit_ntfs_shortname_prefix)
1445 int saw_tilde;
1446 size_t i;
1448 if ((name[0] == '.' && !strncasecmp(name + 1, dotgit_name, len))) {
1449 i = len + 1;
1450 only_spaces_and_periods:
1451 for (;;) {
1452 char c = name[i++];
1453 if (!c || c == ':')
1454 return 1;
1455 if (c != ' ' && c != '.')
1456 return 0;
1461 * Is it a regular NTFS short name, i.e. shortened to 6 characters,
1462 * followed by ~1, ... ~4?
1464 if (!strncasecmp(name, dotgit_name, 6) && name[6] == '~' &&
1465 name[7] >= '1' && name[7] <= '4') {
1466 i = 8;
1467 goto only_spaces_and_periods;
1471 * Is it a fall-back NTFS short name (for details, see
1472 * https://en.wikipedia.org/wiki/8.3_filename?
1474 for (i = 0, saw_tilde = 0; i < 8; i++)
1475 if (name[i] == '\0')
1476 return 0;
1477 else if (saw_tilde) {
1478 if (name[i] < '0' || name[i] > '9')
1479 return 0;
1480 } else if (name[i] == '~') {
1481 if (name[++i] < '1' || name[i] > '9')
1482 return 0;
1483 saw_tilde = 1;
1484 } else if (i >= 6)
1485 return 0;
1486 else if (name[i] & 0x80) {
1488 * We know our needles contain only ASCII, so we clamp
1489 * here to make the results of tolower() sane.
1491 return 0;
1492 } else if (tolower(name[i]) != dotgit_ntfs_shortname_prefix[i])
1493 return 0;
1495 goto only_spaces_and_periods;
1499 * Inline helper to make sure compiler resolves strlen() on literals at
1500 * compile time.
1502 static inline int is_ntfs_dot_str(const char *name, const char *dotgit_name,
1503 const char *dotgit_ntfs_shortname_prefix)
1505 return is_ntfs_dot_generic(name, dotgit_name, strlen(dotgit_name),
1506 dotgit_ntfs_shortname_prefix);
1509 int is_ntfs_dotgitmodules(const char *name)
1511 return is_ntfs_dot_str(name, "gitmodules", "gi7eba");
1514 int is_ntfs_dotgitignore(const char *name)
1516 return is_ntfs_dot_str(name, "gitignore", "gi250a");
1519 int is_ntfs_dotgitattributes(const char *name)
1521 return is_ntfs_dot_str(name, "gitattributes", "gi7d29");
1524 int is_ntfs_dotmailmap(const char *name)
1526 return is_ntfs_dot_str(name, "mailmap", "maba30");
1529 int looks_like_command_line_option(const char *str)
1531 return str && str[0] == '-';
1534 char *xdg_config_home_for(const char *subdir, const char *filename)
1536 const char *home, *config_home;
1538 assert(subdir);
1539 assert(filename);
1540 config_home = getenv("XDG_CONFIG_HOME");
1541 if (config_home && *config_home)
1542 return mkpathdup("%s/%s/%s", config_home, subdir, filename);
1544 home = getenv("HOME");
1545 if (home)
1546 return mkpathdup("%s/.config/%s/%s", home, subdir, filename);
1548 return NULL;
1551 char *xdg_config_home(const char *filename)
1553 return xdg_config_home_for("git", filename);
1556 char *xdg_cache_home(const char *filename)
1558 const char *home, *cache_home;
1560 assert(filename);
1561 cache_home = getenv("XDG_CACHE_HOME");
1562 if (cache_home && *cache_home)
1563 return mkpathdup("%s/git/%s", cache_home, filename);
1565 home = getenv("HOME");
1566 if (home)
1567 return mkpathdup("%s/.cache/git/%s", home, filename);
1568 return NULL;
1571 REPO_GIT_PATH_FUNC(squash_msg, "SQUASH_MSG")
1572 REPO_GIT_PATH_FUNC(merge_msg, "MERGE_MSG")
1573 REPO_GIT_PATH_FUNC(merge_rr, "MERGE_RR")
1574 REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE")
1575 REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD")
1576 REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD")
1577 REPO_GIT_PATH_FUNC(shallow, "shallow")