2 * Utilities for paths and pathnames
4 #include "git-compat-util.h"
6 #include "environment.h"
9 #include "repository.h"
11 #include "string-list.h"
15 #include "submodule-config.h"
18 #include "object-store-ll.h"
22 static int get_st_mode_bits(const char *path
, int *mode
)
25 if (lstat(path
, &st
) < 0)
31 static char bad_path
[] = "/bad-path/";
33 static struct strbuf
*get_pathname(void)
35 static struct strbuf pathname_array
[4] = {
36 STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
39 struct strbuf
*sb
= &pathname_array
[index
];
40 index
= (index
+ 1) % ARRAY_SIZE(pathname_array
);
45 static const char *cleanup_path(const char *path
)
48 if (skip_prefix(path
, "./", &path
)) {
55 static void strbuf_cleanup_path(struct strbuf
*sb
)
57 const char *path
= cleanup_path(sb
->buf
);
59 strbuf_remove(sb
, 0, path
- sb
->buf
);
62 char *mksnpath(char *buf
, size_t n
, const char *fmt
, ...)
68 len
= vsnprintf(buf
, n
, fmt
, args
);
71 strlcpy(buf
, bad_path
, n
);
74 return (char *)cleanup_path(buf
);
77 static int dir_prefix(const char *buf
, const char *dir
)
79 int len
= strlen(dir
);
80 return !strncmp(buf
, dir
, len
) &&
81 (is_dir_sep(buf
[len
]) || buf
[len
] == '\0');
84 /* $buf =~ m|$dir/+$file| but without regex */
85 static int is_dir_file(const char *buf
, const char *dir
, const char *file
)
87 int len
= strlen(dir
);
88 if (strncmp(buf
, dir
, len
) || !is_dir_sep(buf
[len
]))
90 while (is_dir_sep(buf
[len
]))
92 return !strcmp(buf
+ len
, file
);
95 static void replace_dir(struct strbuf
*buf
, int len
, const char *newdir
)
97 int newlen
= strlen(newdir
);
98 int need_sep
= (buf
->buf
[len
] && !is_dir_sep(buf
->buf
[len
])) &&
99 !is_dir_sep(newdir
[newlen
- 1]);
101 len
--; /* keep one char, to be replaced with '/' */
102 strbuf_splice(buf
, 0, len
, newdir
, newlen
);
104 buf
->buf
[newlen
] = '/';
108 /* Not considered garbage for report_linked_checkout_garbage */
109 unsigned ignore_garbage
:1;
111 /* Belongs to the common dir, though it may contain paths that don't */
112 unsigned is_common
:1;
116 static struct common_dir common_list
[] = {
117 { 0, 1, 1, "branches" },
118 { 0, 1, 1, "common" },
119 { 0, 1, 1, "hooks" },
121 { 0, 0, 0, "info/sparse-checkout" },
123 { 1, 0, 0, "logs/HEAD" },
124 { 0, 1, 0, "logs/refs/bisect" },
125 { 0, 1, 0, "logs/refs/rewritten" },
126 { 0, 1, 0, "logs/refs/worktree" },
127 { 0, 1, 1, "lost-found" },
128 { 0, 1, 1, "objects" },
130 { 0, 1, 0, "refs/bisect" },
131 { 0, 1, 0, "refs/rewritten" },
132 { 0, 1, 0, "refs/worktree" },
133 { 0, 1, 1, "remotes" },
134 { 0, 1, 1, "worktrees" },
135 { 0, 1, 1, "rr-cache" },
137 { 0, 0, 1, "config" },
138 { 1, 0, 1, "gc.pid" },
139 { 0, 0, 1, "packed-refs" },
140 { 0, 0, 1, "shallow" },
145 * A compressed trie. A trie node consists of zero or more characters that
146 * are common to all elements with this prefix, optionally followed by some
147 * children. If value is not NULL, the trie node is a terminal node.
149 * For example, consider the following set of strings:
155 * The trie would look like:
156 * root: len = 0, children a and d non-NULL, value = NULL.
157 * a: len = 2, contents = bc, value = (data for "abc")
158 * d: len = 2, contents = ef, children i non-NULL, value = (data for "def")
159 * i: len = 3, contents = nit, children e and i non-NULL, value = NULL
160 * e: len = 0, children all NULL, value = (data for "definite")
161 * i: len = 2, contents = on, children all NULL,
162 * value = (data for "definition")
165 struct trie
*children
[256];
171 static struct trie
*make_trie_node(const char *key
, void *value
)
173 struct trie
*new_node
= xcalloc(1, sizeof(*new_node
));
174 new_node
->len
= strlen(key
);
176 new_node
->contents
= xmalloc(new_node
->len
);
177 memcpy(new_node
->contents
, key
, new_node
->len
);
179 new_node
->value
= value
;
184 * Add a key/value pair to a trie. The key is assumed to be \0-terminated.
185 * If there was an existing value for this key, return it.
187 static void *add_to_trie(struct trie
*root
, const char *key
, void *value
)
194 /* we have reached the end of the key */
200 for (i
= 0; i
< root
->len
; i
++) {
201 if (root
->contents
[i
] == key
[i
])
205 * Split this node: child will contain this node's
208 child
= xmalloc(sizeof(*child
));
209 memcpy(child
->children
, root
->children
, sizeof(root
->children
));
211 child
->len
= root
->len
- i
- 1;
213 child
->contents
= xstrndup(root
->contents
+ i
+ 1,
216 child
->value
= root
->value
;
220 memset(root
->children
, 0, sizeof(root
->children
));
221 root
->children
[(unsigned char)root
->contents
[i
]] = child
;
223 /* This is the newly-added child. */
224 root
->children
[(unsigned char)key
[i
]] =
225 make_trie_node(key
+ i
+ 1, value
);
229 /* We have matched the entire compressed section */
231 child
= root
->children
[(unsigned char)key
[root
->len
]];
233 return add_to_trie(child
, key
+ root
->len
+ 1, value
);
235 child
= make_trie_node(key
+ root
->len
+ 1, value
);
236 root
->children
[(unsigned char)key
[root
->len
]] = child
;
246 typedef int (*match_fn
)(const char *unmatched
, void *value
, void *baton
);
249 * Search a trie for some key. Find the longest /-or-\0-terminated
250 * prefix of the key for which the trie contains a value. If there is
251 * no such prefix, return -1. Otherwise call fn with the unmatched
252 * portion of the key and the found value. If fn returns 0 or
253 * positive, then return its return value. If fn returns negative,
254 * then call fn with the next-longest /-terminated prefix of the key
255 * (i.e. a parent directory) for which the trie contains a value, and
256 * handle its return value the same way. If there is no shorter
257 * /-terminated prefix with a value left, then return the negative
258 * return value of the most recent fn invocation.
260 * The key is partially normalized: consecutive slashes are skipped.
262 * For example, consider the trie containing only [logs,
263 * logs/refs/bisect], both with values, but not logs/refs.
265 * | key | unmatched | prefix to node | return value |
266 * |--------------------|----------------|------------------|--------------|
267 * | a | not called | n/a | -1 |
268 * | logstore | not called | n/a | -1 |
269 * | logs | \0 | logs | as per fn |
270 * | logs/ | / | logs | as per fn |
271 * | logs/refs | /refs | logs | as per fn |
272 * | logs/refs/ | /refs/ | logs | as per fn |
273 * | logs/refs/b | /refs/b | logs | as per fn |
274 * | logs/refs/bisected | /refs/bisected | logs | as per fn |
275 * | logs/refs/bisect | \0 | logs/refs/bisect | as per fn |
276 * | logs/refs/bisect/ | / | logs/refs/bisect | as per fn |
277 * | logs/refs/bisect/a | /a | logs/refs/bisect | as per fn |
278 * | (If fn in the previous line returns -1, then fn is called once more:) |
279 * | logs/refs/bisect/a | /refs/bisect/a | logs | as per fn |
280 * |--------------------|----------------|------------------|--------------|
282 static int trie_find(struct trie
*root
, const char *key
, match_fn fn
,
290 /* we have reached the end of the key */
291 if (root
->value
&& !root
->len
)
292 return fn(key
, root
->value
, baton
);
297 for (i
= 0; i
< root
->len
; i
++) {
298 /* Partial path normalization: skip consecutive slashes. */
299 if (key
[i
] == '/' && key
[i
+1] == '/') {
303 if (root
->contents
[i
] != key
[i
])
307 /* Matched the entire compressed section */
312 return fn(key
, root
->value
, baton
);
317 /* Partial path normalization: skip consecutive slashes */
318 while (key
[0] == '/' && key
[1] == '/')
321 child
= root
->children
[(unsigned char)*key
];
323 result
= trie_find(child
, key
+ 1, fn
, baton
);
327 if (result
>= 0 || (*key
!= '/' && *key
!= 0))
330 return fn(key
, root
->value
, baton
);
335 static struct trie common_trie
;
336 static int common_trie_done_setup
;
338 static void init_common_trie(void)
340 struct common_dir
*p
;
342 if (common_trie_done_setup
)
345 for (p
= common_list
; p
->path
; p
++)
346 add_to_trie(&common_trie
, p
->path
, p
);
348 common_trie_done_setup
= 1;
352 * Helper function for update_common_dir: returns 1 if the dir
355 static int check_common(const char *unmatched
, void *value
,
358 struct common_dir
*dir
= value
;
360 if (dir
->is_dir
&& (unmatched
[0] == 0 || unmatched
[0] == '/'))
361 return dir
->is_common
;
363 if (!dir
->is_dir
&& unmatched
[0] == 0)
364 return dir
->is_common
;
369 static void update_common_dir(struct strbuf
*buf
, int git_dir_len
,
370 const char *common_dir
)
372 char *base
= buf
->buf
+ git_dir_len
;
373 int has_lock_suffix
= strbuf_strip_suffix(buf
, LOCK_SUFFIX
);
376 if (trie_find(&common_trie
, base
, check_common
, NULL
) > 0)
377 replace_dir(buf
, git_dir_len
, common_dir
);
380 strbuf_addstr(buf
, LOCK_SUFFIX
);
383 void report_linked_checkout_garbage(void)
385 struct strbuf sb
= STRBUF_INIT
;
386 const struct common_dir
*p
;
389 if (!the_repository
->different_commondir
)
391 strbuf_addf(&sb
, "%s/", get_git_dir());
393 for (p
= common_list
; p
->path
; p
++) {
394 const char *path
= p
->path
;
395 if (p
->ignore_garbage
)
397 strbuf_setlen(&sb
, len
);
398 strbuf_addstr(&sb
, path
);
399 if (file_exists(sb
.buf
))
400 report_garbage(PACKDIR_FILE_GARBAGE
, sb
.buf
);
405 static void adjust_git_path(const struct repository
*repo
,
406 struct strbuf
*buf
, int git_dir_len
)
408 const char *base
= buf
->buf
+ git_dir_len
;
409 if (is_dir_file(base
, "info", "grafts"))
410 strbuf_splice(buf
, 0, buf
->len
,
411 repo
->graft_file
, strlen(repo
->graft_file
));
412 else if (!strcmp(base
, "index"))
413 strbuf_splice(buf
, 0, buf
->len
,
414 repo
->index_file
, strlen(repo
->index_file
));
415 else if (dir_prefix(base
, "objects"))
416 replace_dir(buf
, git_dir_len
+ 7, repo
->objects
->odb
->path
);
417 else if (git_hooks_path
&& dir_prefix(base
, "hooks"))
418 replace_dir(buf
, git_dir_len
+ 5, git_hooks_path
);
419 else if (repo
->different_commondir
)
420 update_common_dir(buf
, git_dir_len
, repo
->commondir
);
423 static void strbuf_worktree_gitdir(struct strbuf
*buf
,
424 const struct repository
*repo
,
425 const struct worktree
*wt
)
428 strbuf_addstr(buf
, repo
->gitdir
);
430 strbuf_addstr(buf
, repo
->commondir
);
432 strbuf_git_common_path(buf
, repo
, "worktrees/%s", wt
->id
);
435 static void do_git_path(const struct repository
*repo
,
436 const struct worktree
*wt
, struct strbuf
*buf
,
437 const char *fmt
, va_list args
)
440 strbuf_worktree_gitdir(buf
, repo
, wt
);
441 if (buf
->len
&& !is_dir_sep(buf
->buf
[buf
->len
- 1]))
442 strbuf_addch(buf
, '/');
443 gitdir_len
= buf
->len
;
444 strbuf_vaddf(buf
, fmt
, args
);
446 adjust_git_path(repo
, buf
, gitdir_len
);
447 strbuf_cleanup_path(buf
);
450 char *repo_git_path(const struct repository
*repo
,
451 const char *fmt
, ...)
453 struct strbuf path
= STRBUF_INIT
;
456 do_git_path(repo
, NULL
, &path
, fmt
, args
);
458 return strbuf_detach(&path
, NULL
);
461 void strbuf_repo_git_path(struct strbuf
*sb
,
462 const struct repository
*repo
,
463 const char *fmt
, ...)
467 do_git_path(repo
, NULL
, sb
, fmt
, args
);
471 char *git_path_buf(struct strbuf
*buf
, const char *fmt
, ...)
476 do_git_path(the_repository
, NULL
, buf
, fmt
, args
);
481 void strbuf_git_path(struct strbuf
*sb
, const char *fmt
, ...)
485 do_git_path(the_repository
, NULL
, sb
, fmt
, args
);
489 const char *git_path(const char *fmt
, ...)
491 struct strbuf
*pathname
= get_pathname();
494 do_git_path(the_repository
, NULL
, pathname
, fmt
, args
);
496 return pathname
->buf
;
499 char *git_pathdup(const char *fmt
, ...)
501 struct strbuf path
= STRBUF_INIT
;
504 do_git_path(the_repository
, NULL
, &path
, fmt
, args
);
506 return strbuf_detach(&path
, NULL
);
509 char *mkpathdup(const char *fmt
, ...)
511 struct strbuf sb
= STRBUF_INIT
;
514 strbuf_vaddf(&sb
, fmt
, args
);
516 strbuf_cleanup_path(&sb
);
517 return strbuf_detach(&sb
, NULL
);
520 const char *mkpath(const char *fmt
, ...)
523 struct strbuf
*pathname
= get_pathname();
525 strbuf_vaddf(pathname
, fmt
, args
);
527 return cleanup_path(pathname
->buf
);
530 const char *worktree_git_path(const struct worktree
*wt
, const char *fmt
, ...)
532 struct strbuf
*pathname
= get_pathname();
535 do_git_path(the_repository
, wt
, pathname
, fmt
, args
);
537 return pathname
->buf
;
540 static void do_worktree_path(const struct repository
*repo
,
542 const char *fmt
, va_list args
)
544 strbuf_addstr(buf
, repo
->worktree
);
545 if(buf
->len
&& !is_dir_sep(buf
->buf
[buf
->len
- 1]))
546 strbuf_addch(buf
, '/');
548 strbuf_vaddf(buf
, fmt
, args
);
549 strbuf_cleanup_path(buf
);
552 char *repo_worktree_path(const struct repository
*repo
, const char *fmt
, ...)
554 struct strbuf path
= STRBUF_INIT
;
561 do_worktree_path(repo
, &path
, fmt
, args
);
564 return strbuf_detach(&path
, NULL
);
567 void strbuf_repo_worktree_path(struct strbuf
*sb
,
568 const struct repository
*repo
,
569 const char *fmt
, ...)
577 do_worktree_path(repo
, sb
, fmt
, args
);
581 /* Returns 0 on success, negative on failure. */
582 static int do_submodule_path(struct strbuf
*buf
, const char *path
,
583 const char *fmt
, va_list args
)
585 struct strbuf git_submodule_common_dir
= STRBUF_INIT
;
586 struct strbuf git_submodule_dir
= STRBUF_INIT
;
589 ret
= submodule_to_gitdir(&git_submodule_dir
, path
);
593 strbuf_complete(&git_submodule_dir
, '/');
594 strbuf_addbuf(buf
, &git_submodule_dir
);
595 strbuf_vaddf(buf
, fmt
, args
);
597 if (get_common_dir_noenv(&git_submodule_common_dir
, git_submodule_dir
.buf
))
598 update_common_dir(buf
, git_submodule_dir
.len
, git_submodule_common_dir
.buf
);
600 strbuf_cleanup_path(buf
);
603 strbuf_release(&git_submodule_dir
);
604 strbuf_release(&git_submodule_common_dir
);
608 char *git_pathdup_submodule(const char *path
, const char *fmt
, ...)
612 struct strbuf buf
= STRBUF_INIT
;
614 err
= do_submodule_path(&buf
, path
, fmt
, args
);
617 strbuf_release(&buf
);
620 return strbuf_detach(&buf
, NULL
);
623 int strbuf_git_path_submodule(struct strbuf
*buf
, const char *path
,
624 const char *fmt
, ...)
629 err
= do_submodule_path(buf
, path
, fmt
, args
);
635 static void do_git_common_path(const struct repository
*repo
,
640 strbuf_addstr(buf
, repo
->commondir
);
641 if (buf
->len
&& !is_dir_sep(buf
->buf
[buf
->len
- 1]))
642 strbuf_addch(buf
, '/');
643 strbuf_vaddf(buf
, fmt
, args
);
644 strbuf_cleanup_path(buf
);
647 const char *git_common_path(const char *fmt
, ...)
649 struct strbuf
*pathname
= get_pathname();
652 do_git_common_path(the_repository
, pathname
, fmt
, args
);
654 return pathname
->buf
;
657 void strbuf_git_common_path(struct strbuf
*sb
,
658 const struct repository
*repo
,
659 const char *fmt
, ...)
663 do_git_common_path(repo
, sb
, fmt
, args
);
667 int validate_headref(const char *path
)
672 struct object_id oid
;
676 if (lstat(path
, &st
) < 0)
679 /* Make sure it is a "refs/.." symlink */
680 if (S_ISLNK(st
.st_mode
)) {
681 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
682 if (len
>= 5 && !memcmp("refs/", buffer
, 5))
688 * Anything else, just open it and try to see if it is a symbolic ref.
690 fd
= open(path
, O_RDONLY
);
693 len
= read_in_full(fd
, buffer
, sizeof(buffer
)-1);
701 * Is it a symbolic ref?
703 if (skip_prefix(buffer
, "ref:", &refname
)) {
704 while (isspace(*refname
))
706 if (starts_with(refname
, "refs/"))
711 * Is this a detached HEAD?
713 if (!get_oid_hex(buffer
, &oid
))
719 static struct passwd
*getpw_str(const char *username
, size_t len
)
722 char *username_z
= xmemdupz(username
, len
);
723 pw
= getpwnam(username_z
);
729 * Return a string with ~ and ~user expanded via getpw*. Returns NULL on getpw
730 * failure or if path is NULL.
732 * If real_home is true, strbuf_realpath($HOME) is used in the `~/` expansion.
734 * If the path starts with `%(prefix)/`, the remainder is interpreted as
735 * relative to where Git is installed, and expanded to the absolute path.
737 char *interpolate_path(const char *path
, int real_home
)
739 struct strbuf user_path
= STRBUF_INIT
;
740 const char *to_copy
= path
;
745 if (skip_prefix(path
, "%(prefix)/", &path
))
746 return system_path(path
);
748 if (path
[0] == '~') {
749 const char *first_slash
= strchrnul(path
, '/');
750 const char *username
= path
+ 1;
751 size_t username_len
= first_slash
- username
;
752 if (username_len
== 0) {
753 const char *home
= getenv("HOME");
757 strbuf_add_real_path(&user_path
, home
);
759 strbuf_addstr(&user_path
, home
);
760 #ifdef GIT_WINDOWS_NATIVE
761 convert_slashes(user_path
.buf
);
764 struct passwd
*pw
= getpw_str(username
, username_len
);
767 strbuf_addstr(&user_path
, pw
->pw_dir
);
769 to_copy
= first_slash
;
771 strbuf_addstr(&user_path
, to_copy
);
772 return strbuf_detach(&user_path
, NULL
);
774 strbuf_release(&user_path
);
779 * First, one directory to try is determined by the following algorithm.
781 * (0) If "strict" is given, the path is used as given and no DWIM is
783 * (1) "~/path" to mean path under the running user's home directory;
784 * (2) "~user/path" to mean path under named user's home directory;
785 * (3) "relative/path" to mean cwd relative directory; or
786 * (4) "/absolute/path" to mean absolute directory.
788 * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git"
789 * in this order. We select the first one that is a valid git repository, and
790 * chdir() to it. If none match, or we fail to chdir, we return NULL.
792 * If all goes well, we return the directory we used to chdir() (but
793 * before ~user is expanded), avoiding getcwd() resolving symbolic
794 * links. User relative paths are also returned as they are given,
795 * except DWIM suffixing.
797 const char *enter_repo(const char *path
, int strict
)
799 static struct strbuf validated_path
= STRBUF_INIT
;
800 static struct strbuf used_path
= STRBUF_INIT
;
806 static const char *suffix
[] = {
807 "/.git", "", ".git/.git", ".git", NULL
,
810 int len
= strlen(path
);
812 while ((1 < len
) && (path
[len
-1] == '/'))
816 * We can handle arbitrary-sized buffers, but this remains as a
817 * sanity check on untrusted input.
822 strbuf_reset(&used_path
);
823 strbuf_reset(&validated_path
);
824 strbuf_add(&used_path
, path
, len
);
825 strbuf_add(&validated_path
, path
, len
);
827 if (used_path
.buf
[0] == '~') {
828 char *newpath
= interpolate_path(used_path
.buf
, 0);
831 strbuf_attach(&used_path
, newpath
, strlen(newpath
),
834 for (i
= 0; suffix
[i
]; i
++) {
836 size_t baselen
= used_path
.len
;
837 strbuf_addstr(&used_path
, suffix
[i
]);
838 if (!stat(used_path
.buf
, &st
) &&
839 (S_ISREG(st
.st_mode
) ||
840 (S_ISDIR(st
.st_mode
) && is_git_directory(used_path
.buf
)))) {
841 strbuf_addstr(&validated_path
, suffix
[i
]);
844 strbuf_setlen(&used_path
, baselen
);
848 gitfile
= read_gitfile(used_path
.buf
);
850 strbuf_reset(&used_path
);
851 strbuf_addstr(&used_path
, gitfile
);
853 if (chdir(used_path
.buf
))
855 path
= validated_path
.buf
;
858 const char *gitfile
= read_gitfile(path
);
865 if (is_git_directory(".")) {
867 check_repository_format(NULL
);
874 static int calc_shared_perm(int mode
)
878 if (get_shared_repository() < 0)
879 tweak
= -get_shared_repository();
881 tweak
= get_shared_repository();
883 if (!(mode
& S_IWUSR
))
886 /* Copy read bits to execute bits */
887 tweak
|= (tweak
& 0444) >> 2;
888 if (get_shared_repository() < 0)
889 mode
= (mode
& ~0777) | tweak
;
897 int adjust_shared_perm(const char *path
)
899 int old_mode
, new_mode
;
901 if (!get_shared_repository())
903 if (get_st_mode_bits(path
, &old_mode
) < 0)
906 new_mode
= calc_shared_perm(old_mode
);
907 if (S_ISDIR(old_mode
)) {
908 /* Copy read bits to execute bits */
909 new_mode
|= (new_mode
& 0444) >> 2;
912 * g+s matters only if any extra access is granted
913 * based on group membership.
915 if (FORCE_DIR_SET_GID
&& (new_mode
& 060))
916 new_mode
|= FORCE_DIR_SET_GID
;
919 if (((old_mode
^ new_mode
) & ~S_IFMT
) &&
920 chmod(path
, (new_mode
& ~S_IFMT
)) < 0)
925 void safe_create_dir(const char *dir
, int share
)
927 if (mkdir(dir
, 0777) < 0) {
928 if (errno
!= EEXIST
) {
933 else if (share
&& adjust_shared_perm(dir
))
934 die(_("Could not make %s writable by group"), dir
);
937 static int have_same_root(const char *path1
, const char *path2
)
939 int is_abs1
, is_abs2
;
941 is_abs1
= is_absolute_path(path1
);
942 is_abs2
= is_absolute_path(path2
);
943 return (is_abs1
&& is_abs2
&& tolower(path1
[0]) == tolower(path2
[0])) ||
944 (!is_abs1
&& !is_abs2
);
948 * Give path as relative to prefix.
950 * The strbuf may or may not be used, so do not assume it contains the
953 const char *relative_path(const char *in
, const char *prefix
,
956 int in_len
= in
? strlen(in
) : 0;
957 int prefix_len
= prefix
? strlen(prefix
) : 0;
964 else if (!prefix_len
)
967 if (have_same_root(in
, prefix
))
968 /* bypass dos_drive, for "c:" is identical to "C:" */
969 i
= j
= has_dos_drive_prefix(in
);
974 while (i
< prefix_len
&& j
< in_len
&& prefix
[i
] == in
[j
]) {
975 if (is_dir_sep(prefix
[i
])) {
976 while (is_dir_sep(prefix
[i
]))
978 while (is_dir_sep(in
[j
]))
989 /* "prefix" seems like prefix of "in" */
992 * but "/foo" is not a prefix of "/foobar"
993 * (i.e. prefix not end with '/')
995 prefix_off
< prefix_len
) {
997 /* in="/a/b", prefix="/a/b" */
999 } else if (is_dir_sep(in
[j
])) {
1000 /* in="/a/b/c", prefix="/a/b" */
1001 while (is_dir_sep(in
[j
]))
1005 /* in="/a/bbb/c", prefix="/a/b" */
1009 /* "in" is short than "prefix" */
1011 /* "in" not end with '/' */
1013 if (is_dir_sep(prefix
[i
])) {
1014 /* in="/a/b", prefix="/a/b/c/" */
1015 while (is_dir_sep(prefix
[i
]))
1023 if (i
>= prefix_len
) {
1031 strbuf_grow(sb
, in_len
);
1033 while (i
< prefix_len
) {
1034 if (is_dir_sep(prefix
[i
])) {
1035 strbuf_addstr(sb
, "../");
1036 while (is_dir_sep(prefix
[i
]))
1042 if (!is_dir_sep(prefix
[prefix_len
- 1]))
1043 strbuf_addstr(sb
, "../");
1045 strbuf_addstr(sb
, in
);
1051 * A simpler implementation of relative_path
1053 * Get relative path by removing "prefix" from "in". This function
1054 * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter
1055 * to increase performance when traversing the path to work_tree.
1057 const char *remove_leading_path(const char *in
, const char *prefix
)
1059 static struct strbuf buf
= STRBUF_INIT
;
1062 if (!prefix
|| !prefix
[0])
1065 if (is_dir_sep(prefix
[i
])) {
1066 if (!is_dir_sep(in
[j
]))
1068 while (is_dir_sep(prefix
[i
]))
1070 while (is_dir_sep(in
[j
]))
1073 } else if (in
[j
] != prefix
[i
]) {
1080 /* "/foo" is a prefix of "/foo" */
1082 /* "/foo" is not a prefix of "/foobar" */
1083 !is_dir_sep(prefix
[i
-1]) && !is_dir_sep(in
[j
])
1086 while (is_dir_sep(in
[j
]))
1091 strbuf_addstr(&buf
, ".");
1093 strbuf_addstr(&buf
, in
+ j
);
1098 * It is okay if dst == src, but they should not overlap otherwise.
1099 * The "dst" buffer must be at least as long as "src"; normalizing may shrink
1100 * the size of the path, but will never grow it.
1102 * Performs the following normalizations on src, storing the result in dst:
1103 * - Ensures that components are separated by '/' (Windows only)
1104 * - Squashes sequences of '/' except "//server/share" on Windows
1105 * - Removes "." components.
1106 * - Removes ".." components, and the components the precede them.
1107 * Returns failure (non-zero) if a ".." component appears as first path
1108 * component anytime during the normalization. Otherwise, returns success (0).
1110 * Note that this function is purely textual. It does not follow symlinks,
1111 * verify the existence of the path, or make any system calls.
1113 * prefix_len != NULL is for a specific case of prefix_pathspec():
1114 * assume that src == dst and src[0..prefix_len-1] is already
1115 * normalized, any time "../" eats up to the prefix_len part,
1116 * prefix_len is reduced. In the end prefix_len is the remaining
1117 * prefix that has not been overridden by user pathspec.
1119 * NEEDSWORK: This function doesn't perform normalization w.r.t. trailing '/'.
1120 * For everything but the root folder itself, the normalized path should not
1121 * end with a '/', then the callers need to be fixed up accordingly.
1124 int normalize_path_copy_len(char *dst
, const char *src
, int *prefix_len
)
1130 * Copy initial part of absolute path: "/", "C:/", "//server/share/".
1132 end
= src
+ offset_1st_component(src
);
1141 while (is_dir_sep(*src
))
1148 * A path component that begins with . could be
1150 * (1) "." and ends -- ignore and terminate.
1151 * (2) "./" -- ignore them, eat slash and continue.
1152 * (3) ".." and ends -- strip one and terminate.
1153 * (4) "../" -- strip one, eat slash and continue.
1159 } else if (is_dir_sep(src
[1])) {
1162 while (is_dir_sep(*src
))
1165 } else if (src
[1] == '.') {
1170 } else if (is_dir_sep(src
[2])) {
1173 while (is_dir_sep(*src
))
1180 /* copy up to the next '/', and eat all '/' */
1181 while ((c
= *src
++) != '\0' && !is_dir_sep(c
))
1183 if (is_dir_sep(c
)) {
1185 while (is_dir_sep(c
))
1194 * dst0..dst is prefix portion, and dst[-1] is '/';
1197 dst
--; /* go to trailing '/' */
1200 /* Windows: dst[-1] cannot be backslash anymore */
1201 while (dst0
< dst
&& dst
[-1] != '/')
1203 if (prefix_len
&& *prefix_len
> dst
- dst0
)
1204 *prefix_len
= dst
- dst0
;
1210 int normalize_path_copy(char *dst
, const char *src
)
1212 return normalize_path_copy_len(dst
, src
, NULL
);
1215 int strbuf_normalize_path(struct strbuf
*src
)
1217 struct strbuf dst
= STRBUF_INIT
;
1219 strbuf_grow(&dst
, src
->len
);
1220 if (normalize_path_copy(dst
.buf
, src
->buf
) < 0) {
1221 strbuf_release(&dst
);
1226 * normalize_path does not tell us the new length, so we have to
1227 * compute it by looking for the new NUL it placed
1229 strbuf_setlen(&dst
, strlen(dst
.buf
));
1230 strbuf_swap(src
, &dst
);
1231 strbuf_release(&dst
);
1236 * path = Canonical absolute path
1237 * prefixes = string_list containing normalized, absolute paths without
1238 * trailing slashes (except for the root directory, which is denoted by "/").
1240 * Determines, for each path in prefixes, whether the "prefix"
1241 * is an ancestor directory of path. Returns the length of the longest
1242 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
1243 * is an ancestor. (Note that this means 0 is returned if prefixes is
1244 * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories
1245 * are not considered to be their own ancestors. path must be in a
1246 * canonical form: empty components, or "." or ".." components are not
1249 int longest_ancestor_length(const char *path
, struct string_list
*prefixes
)
1251 int i
, max_len
= -1;
1253 if (!strcmp(path
, "/"))
1256 for (i
= 0; i
< prefixes
->nr
; i
++) {
1257 const char *ceil
= prefixes
->items
[i
].string
;
1258 int len
= strlen(ceil
);
1261 * For root directories (`/`, `C:/`, `//server/share/`)
1262 * adjust the length to exclude the trailing slash.
1264 if (len
> 0 && ceil
[len
- 1] == '/')
1267 if (strncmp(path
, ceil
, len
) ||
1268 path
[len
] != '/' || !path
[len
+ 1])
1269 continue; /* no match */
1278 /* strip arbitrary amount of directory separators at end of path */
1279 static inline int chomp_trailing_dir_sep(const char *path
, int len
)
1281 while (len
&& is_dir_sep(path
[len
- 1]))
1287 * If path ends with suffix (complete path components), returns the offset of
1288 * the last character in the path before the suffix (sans trailing directory
1289 * separators), and -1 otherwise.
1291 static ssize_t
stripped_path_suffix_offset(const char *path
, const char *suffix
)
1293 int path_len
= strlen(path
), suffix_len
= strlen(suffix
);
1295 while (suffix_len
) {
1299 if (is_dir_sep(path
[path_len
- 1])) {
1300 if (!is_dir_sep(suffix
[suffix_len
- 1]))
1302 path_len
= chomp_trailing_dir_sep(path
, path_len
);
1303 suffix_len
= chomp_trailing_dir_sep(suffix
, suffix_len
);
1305 else if (path
[--path_len
] != suffix
[--suffix_len
])
1309 if (path_len
&& !is_dir_sep(path
[path_len
- 1]))
1311 return chomp_trailing_dir_sep(path
, path_len
);
1315 * Returns true if the path ends with components, considering only complete path
1316 * components, and false otherwise.
1318 int ends_with_path_components(const char *path
, const char *components
)
1320 return stripped_path_suffix_offset(path
, components
) != -1;
1324 * If path ends with suffix (complete path components), returns the
1325 * part before suffix (sans trailing directory separators).
1326 * Otherwise returns NULL.
1328 char *strip_path_suffix(const char *path
, const char *suffix
)
1330 ssize_t offset
= stripped_path_suffix_offset(path
, suffix
);
1332 return offset
== -1 ? NULL
: xstrndup(path
, offset
);
1335 int daemon_avoid_alias(const char *p
)
1340 * This resurrects the belts and suspenders paranoia check by HPA
1341 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
1342 * does not do getcwd() based path canonicalization.
1344 * sl becomes true immediately after seeing '/' and continues to
1345 * be true as long as dots continue after that without intervening
1346 * non-dot character.
1348 if (!p
|| (*p
!= '/' && *p
!= '~'))
1358 else if (ch
== '/') {
1360 /* reject //, /./ and /../ */
1365 if (0 < ndot
&& ndot
< 3)
1366 /* reject /.$ and /..$ */
1375 else if (ch
== '/') {
1383 * On NTFS, we need to be careful to disallow certain synonyms of the `.git/`
1386 * - For historical reasons, file names that end in spaces or periods are
1387 * automatically trimmed. Therefore, `.git . . ./` is a valid way to refer
1390 * - For other historical reasons, file names that do not conform to the 8.3
1391 * format (up to eight characters for the basename, three for the file
1392 * extension, certain characters not allowed such as `+`, etc) are associated
1393 * with a so-called "short name", at least on the `C:` drive by default.
1394 * Which means that `git~1/` is a valid way to refer to `.git/`.
1396 * Note: Technically, `.git/` could receive the short name `git~2` if the
1397 * short name `git~1` were already used. In Git, however, we guarantee that
1398 * `.git` is the first item in a directory, therefore it will be associated
1399 * with the short name `git~1` (unless short names are disabled).
1401 * - For yet other historical reasons, NTFS supports so-called "Alternate Data
1402 * Streams", i.e. metadata associated with a given file, referred to via
1403 * `<filename>:<stream-name>:<stream-type>`. There exists a default stream
1404 * type for directories, allowing `.git/` to be accessed via
1405 * `.git::$INDEX_ALLOCATION/`.
1407 * When this function returns 1, it indicates that the specified file/directory
1408 * name refers to a `.git` file or directory, or to any of these synonyms, and
1409 * Git should therefore not track it.
1411 * For performance reasons, _all_ Alternate Data Streams of `.git/` are
1412 * forbidden, not just `::$INDEX_ALLOCATION`.
1414 * This function is intended to be used by `git fsck` even on platforms where
1415 * the backslash is a regular filename character, therefore it needs to handle
1416 * backlash characters in the provided `name` specially: they are interpreted
1417 * as directory separators.
1419 int is_ntfs_dotgit(const char *name
)
1424 * Note that when we don't find `.git` or `git~1` we end up with `name`
1425 * advanced partway through the string. That's okay, though, as we
1426 * return immediately in those cases, without looking at `name` any
1432 if (((c
= *(name
++)) != 'g' && c
!= 'G') ||
1433 ((c
= *(name
++)) != 'i' && c
!= 'I') ||
1434 ((c
= *(name
++)) != 't' && c
!= 'T'))
1436 } else if (c
== 'g' || c
== 'G') {
1438 if (((c
= *(name
++)) != 'i' && c
!= 'I') ||
1439 ((c
= *(name
++)) != 't' && c
!= 'T') ||
1448 if (!c
|| is_xplatform_dir_sep(c
) || c
== ':')
1450 if (c
!= '.' && c
!= ' ')
1455 static int is_ntfs_dot_generic(const char *name
,
1456 const char *dotgit_name
,
1458 const char *dotgit_ntfs_shortname_prefix
)
1463 if ((name
[0] == '.' && !strncasecmp(name
+ 1, dotgit_name
, len
))) {
1465 only_spaces_and_periods
:
1470 if (c
!= ' ' && c
!= '.')
1476 * Is it a regular NTFS short name, i.e. shortened to 6 characters,
1477 * followed by ~1, ... ~4?
1479 if (!strncasecmp(name
, dotgit_name
, 6) && name
[6] == '~' &&
1480 name
[7] >= '1' && name
[7] <= '4') {
1482 goto only_spaces_and_periods
;
1486 * Is it a fall-back NTFS short name (for details, see
1487 * https://en.wikipedia.org/wiki/8.3_filename?
1489 for (i
= 0, saw_tilde
= 0; i
< 8; i
++)
1490 if (name
[i
] == '\0')
1492 else if (saw_tilde
) {
1493 if (name
[i
] < '0' || name
[i
] > '9')
1495 } else if (name
[i
] == '~') {
1496 if (name
[++i
] < '1' || name
[i
] > '9')
1501 else if (name
[i
] & 0x80) {
1503 * We know our needles contain only ASCII, so we clamp
1504 * here to make the results of tolower() sane.
1507 } else if (tolower(name
[i
]) != dotgit_ntfs_shortname_prefix
[i
])
1510 goto only_spaces_and_periods
;
1514 * Inline helper to make sure compiler resolves strlen() on literals at
1517 static inline int is_ntfs_dot_str(const char *name
, const char *dotgit_name
,
1518 const char *dotgit_ntfs_shortname_prefix
)
1520 return is_ntfs_dot_generic(name
, dotgit_name
, strlen(dotgit_name
),
1521 dotgit_ntfs_shortname_prefix
);
1524 int is_ntfs_dotgitmodules(const char *name
)
1526 return is_ntfs_dot_str(name
, "gitmodules", "gi7eba");
1529 int is_ntfs_dotgitignore(const char *name
)
1531 return is_ntfs_dot_str(name
, "gitignore", "gi250a");
1534 int is_ntfs_dotgitattributes(const char *name
)
1536 return is_ntfs_dot_str(name
, "gitattributes", "gi7d29");
1539 int is_ntfs_dotmailmap(const char *name
)
1541 return is_ntfs_dot_str(name
, "mailmap", "maba30");
1544 int looks_like_command_line_option(const char *str
)
1546 return str
&& str
[0] == '-';
1549 char *xdg_config_home_for(const char *subdir
, const char *filename
)
1551 const char *home
, *config_home
;
1555 config_home
= getenv("XDG_CONFIG_HOME");
1556 if (config_home
&& *config_home
)
1557 return mkpathdup("%s/%s/%s", config_home
, subdir
, filename
);
1559 home
= getenv("HOME");
1561 return mkpathdup("%s/.config/%s/%s", home
, subdir
, filename
);
1566 char *xdg_config_home(const char *filename
)
1568 return xdg_config_home_for("git", filename
);
1571 char *xdg_cache_home(const char *filename
)
1573 const char *home
, *cache_home
;
1576 cache_home
= getenv("XDG_CACHE_HOME");
1577 if (cache_home
&& *cache_home
)
1578 return mkpathdup("%s/git/%s", cache_home
, filename
);
1580 home
= getenv("HOME");
1582 return mkpathdup("%s/.cache/git/%s", home
, filename
);
1586 REPO_GIT_PATH_FUNC(squash_msg
, "SQUASH_MSG")
1587 REPO_GIT_PATH_FUNC(merge_msg
, "MERGE_MSG")
1588 REPO_GIT_PATH_FUNC(merge_rr
, "MERGE_RR")
1589 REPO_GIT_PATH_FUNC(merge_mode
, "MERGE_MODE")
1590 REPO_GIT_PATH_FUNC(merge_head
, "MERGE_HEAD")
1591 REPO_GIT_PATH_FUNC(merge_autostash
, "MERGE_AUTOSTASH")
1592 REPO_GIT_PATH_FUNC(auto_merge
, "AUTO_MERGE")
1593 REPO_GIT_PATH_FUNC(fetch_head
, "FETCH_HEAD")
1594 REPO_GIT_PATH_FUNC(shallow
, "shallow")