remove_leading_path: use a strbuf for internal storage
[git/mingw.git] / path.c
blobc597473e62265d1b59510c15a90617256a9897c3
1 /*
2 * Utilities for paths and pathnames
3 */
4 #include "cache.h"
5 #include "strbuf.h"
6 #include "string-list.h"
7 #include "dir.h"
9 static int get_st_mode_bits(const char *path, int *mode)
11 struct stat st;
12 if (lstat(path, &st) < 0)
13 return -1;
14 *mode = st.st_mode;
15 return 0;
18 static char bad_path[] = "/bad-path/";
20 static struct strbuf *get_pathname(void)
22 static struct strbuf pathname_array[4] = {
23 STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
25 static int index;
26 struct strbuf *sb = &pathname_array[3 & ++index];
27 strbuf_reset(sb);
28 return sb;
31 static char *cleanup_path(char *path)
33 /* Clean it up */
34 if (!memcmp(path, "./", 2)) {
35 path += 2;
36 while (*path == '/')
37 path++;
39 return path;
42 static void strbuf_cleanup_path(struct strbuf *sb)
44 char *path = cleanup_path(sb->buf);
45 if (path > sb->buf)
46 strbuf_remove(sb, 0, path - sb->buf);
49 char *mksnpath(char *buf, size_t n, const char *fmt, ...)
51 va_list args;
52 unsigned len;
54 va_start(args, fmt);
55 len = vsnprintf(buf, n, fmt, args);
56 va_end(args);
57 if (len >= n) {
58 strlcpy(buf, bad_path, n);
59 return buf;
61 return cleanup_path(buf);
64 static int dir_prefix(const char *buf, const char *dir)
66 int len = strlen(dir);
67 return !strncmp(buf, dir, len) &&
68 (is_dir_sep(buf[len]) || buf[len] == '\0');
71 /* $buf =~ m|$dir/+$file| but without regex */
72 static int is_dir_file(const char *buf, const char *dir, const char *file)
74 int len = strlen(dir);
75 if (strncmp(buf, dir, len) || !is_dir_sep(buf[len]))
76 return 0;
77 while (is_dir_sep(buf[len]))
78 len++;
79 return !strcmp(buf + len, file);
82 static void replace_dir(struct strbuf *buf, int len, const char *newdir)
84 int newlen = strlen(newdir);
85 int need_sep = (buf->buf[len] && !is_dir_sep(buf->buf[len])) &&
86 !is_dir_sep(newdir[newlen - 1]);
87 if (need_sep)
88 len--; /* keep one char, to be replaced with '/' */
89 strbuf_splice(buf, 0, len, newdir, newlen);
90 if (need_sep)
91 buf->buf[newlen] = '/';
94 static const char *common_list[] = {
95 "/branches", "/hooks", "/info", "!/logs", "/lost-found",
96 "/objects", "/refs", "/remotes", "/worktrees", "/rr-cache", "/svn",
97 "config", "!gc.pid", "packed-refs", "shallow",
98 NULL
101 static void update_common_dir(struct strbuf *buf, int git_dir_len)
103 char *base = buf->buf + git_dir_len;
104 const char **p;
106 if (is_dir_file(base, "logs", "HEAD") ||
107 is_dir_file(base, "info", "sparse-checkout"))
108 return; /* keep this in $GIT_DIR */
109 for (p = common_list; *p; p++) {
110 const char *path = *p;
111 int is_dir = 0;
112 if (*path == '!')
113 path++;
114 if (*path == '/') {
115 path++;
116 is_dir = 1;
118 if (is_dir && dir_prefix(base, path)) {
119 replace_dir(buf, git_dir_len, get_git_common_dir());
120 return;
122 if (!is_dir && !strcmp(base, path)) {
123 replace_dir(buf, git_dir_len, get_git_common_dir());
124 return;
129 void report_linked_checkout_garbage(void)
131 struct strbuf sb = STRBUF_INIT;
132 const char **p;
133 int len;
135 if (!git_common_dir_env)
136 return;
137 strbuf_addf(&sb, "%s/", get_git_dir());
138 len = sb.len;
139 for (p = common_list; *p; p++) {
140 const char *path = *p;
141 if (*path == '!')
142 continue;
143 strbuf_setlen(&sb, len);
144 strbuf_addstr(&sb, path);
145 if (file_exists(sb.buf))
146 report_garbage("unused in linked checkout", sb.buf);
148 strbuf_release(&sb);
151 static void adjust_git_path(struct strbuf *buf, int git_dir_len)
153 const char *base = buf->buf + git_dir_len;
154 if (git_graft_env && is_dir_file(base, "info", "grafts"))
155 strbuf_splice(buf, 0, buf->len,
156 get_graft_file(), strlen(get_graft_file()));
157 else if (git_index_env && !strcmp(base, "index"))
158 strbuf_splice(buf, 0, buf->len,
159 get_index_file(), strlen(get_index_file()));
160 else if (git_db_env && dir_prefix(base, "objects"))
161 replace_dir(buf, git_dir_len + 7, get_object_directory());
162 else if (git_common_dir_env)
163 update_common_dir(buf, git_dir_len);
166 static void do_git_path(struct strbuf *buf, const char *fmt, va_list args)
168 int gitdir_len;
169 strbuf_addstr(buf, get_git_dir());
170 if (buf->len && !is_dir_sep(buf->buf[buf->len - 1]))
171 strbuf_addch(buf, '/');
172 gitdir_len = buf->len;
173 strbuf_vaddf(buf, fmt, args);
174 adjust_git_path(buf, gitdir_len);
175 strbuf_cleanup_path(buf);
178 char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
180 va_list args;
181 strbuf_reset(buf);
182 va_start(args, fmt);
183 do_git_path(buf, fmt, args);
184 va_end(args);
185 return buf->buf;
188 void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
190 va_list args;
191 va_start(args, fmt);
192 do_git_path(sb, fmt, args);
193 va_end(args);
196 const char *git_path(const char *fmt, ...)
198 struct strbuf *pathname = get_pathname();
199 va_list args;
200 va_start(args, fmt);
201 do_git_path(pathname, fmt, args);
202 va_end(args);
203 return pathname->buf;
206 char *git_pathdup(const char *fmt, ...)
208 struct strbuf path = STRBUF_INIT;
209 va_list args;
210 va_start(args, fmt);
211 do_git_path(&path, fmt, args);
212 va_end(args);
213 return strbuf_detach(&path, NULL);
216 char *mkpathdup(const char *fmt, ...)
218 struct strbuf sb = STRBUF_INIT;
219 va_list args;
220 va_start(args, fmt);
221 strbuf_vaddf(&sb, fmt, args);
222 va_end(args);
223 strbuf_cleanup_path(&sb);
224 return strbuf_detach(&sb, NULL);
227 const char *mkpath(const char *fmt, ...)
229 va_list args;
230 struct strbuf *pathname = get_pathname();
231 va_start(args, fmt);
232 strbuf_vaddf(pathname, fmt, args);
233 va_end(args);
234 return cleanup_path(pathname->buf);
237 static void do_submodule_path(struct strbuf *buf, const char *path,
238 const char *fmt, va_list args)
240 const char *git_dir;
242 strbuf_addstr(buf, path);
243 if (buf->len && buf->buf[buf->len - 1] != '/')
244 strbuf_addch(buf, '/');
245 strbuf_addstr(buf, ".git");
247 git_dir = read_gitfile(buf->buf);
248 if (git_dir) {
249 strbuf_reset(buf);
250 strbuf_addstr(buf, git_dir);
252 strbuf_addch(buf, '/');
254 strbuf_vaddf(buf, fmt, args);
255 strbuf_cleanup_path(buf);
258 char *git_pathdup_submodule(const char *path, const char *fmt, ...)
260 va_list args;
261 struct strbuf buf = STRBUF_INIT;
262 va_start(args, fmt);
263 do_submodule_path(&buf, path, fmt, args);
264 va_end(args);
265 return strbuf_detach(&buf, NULL);
268 void strbuf_git_path_submodule(struct strbuf *buf, const char *path,
269 const char *fmt, ...)
271 va_list args;
272 va_start(args, fmt);
273 do_submodule_path(buf, path, fmt, args);
274 va_end(args);
277 int validate_headref(const char *path)
279 struct stat st;
280 char *buf, buffer[256];
281 unsigned char sha1[20];
282 int fd;
283 ssize_t len;
285 if (lstat(path, &st) < 0)
286 return -1;
288 /* Make sure it is a "refs/.." symlink */
289 if (S_ISLNK(st.st_mode)) {
290 len = readlink(path, buffer, sizeof(buffer)-1);
291 if (len >= 5 && !memcmp("refs/", buffer, 5))
292 return 0;
293 return -1;
297 * Anything else, just open it and try to see if it is a symbolic ref.
299 fd = open(path, O_RDONLY);
300 if (fd < 0)
301 return -1;
302 len = read_in_full(fd, buffer, sizeof(buffer)-1);
303 close(fd);
306 * Is it a symbolic ref?
308 if (len < 4)
309 return -1;
310 if (!memcmp("ref:", buffer, 4)) {
311 buf = buffer + 4;
312 len -= 4;
313 while (len && isspace(*buf))
314 buf++, len--;
315 if (len >= 5 && !memcmp("refs/", buf, 5))
316 return 0;
320 * Is this a detached HEAD?
322 if (!get_sha1_hex(buffer, sha1))
323 return 0;
325 return -1;
328 static struct passwd *getpw_str(const char *username, size_t len)
330 struct passwd *pw;
331 char *username_z = xmemdupz(username, len);
332 pw = getpwnam(username_z);
333 free(username_z);
334 return pw;
338 * Return a string with ~ and ~user expanded via getpw*. If buf != NULL,
339 * then it is a newly allocated string. Returns NULL on getpw failure or
340 * if path is NULL.
342 char *expand_user_path(const char *path)
344 struct strbuf user_path = STRBUF_INIT;
345 const char *to_copy = path;
347 if (path == NULL)
348 goto return_null;
349 if (path[0] == '~') {
350 const char *first_slash = strchrnul(path, '/');
351 const char *username = path + 1;
352 size_t username_len = first_slash - username;
353 if (username_len == 0) {
354 const char *home = getenv("HOME");
355 if (!home)
356 goto return_null;
357 strbuf_addstr(&user_path, home);
358 } else {
359 struct passwd *pw = getpw_str(username, username_len);
360 if (!pw)
361 goto return_null;
362 strbuf_addstr(&user_path, pw->pw_dir);
364 to_copy = first_slash;
366 strbuf_addstr(&user_path, to_copy);
367 return strbuf_detach(&user_path, NULL);
368 return_null:
369 strbuf_release(&user_path);
370 return NULL;
374 * First, one directory to try is determined by the following algorithm.
376 * (0) If "strict" is given, the path is used as given and no DWIM is
377 * done. Otherwise:
378 * (1) "~/path" to mean path under the running user's home directory;
379 * (2) "~user/path" to mean path under named user's home directory;
380 * (3) "relative/path" to mean cwd relative directory; or
381 * (4) "/absolute/path" to mean absolute directory.
383 * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git"
384 * in this order. We select the first one that is a valid git repository, and
385 * chdir() to it. If none match, or we fail to chdir, we return NULL.
387 * If all goes well, we return the directory we used to chdir() (but
388 * before ~user is expanded), avoiding getcwd() resolving symbolic
389 * links. User relative paths are also returned as they are given,
390 * except DWIM suffixing.
392 const char *enter_repo(const char *path, int strict)
394 static struct strbuf validated_path = STRBUF_INIT;
395 static struct strbuf used_path = STRBUF_INIT;
397 if (!path)
398 return NULL;
400 if (!strict) {
401 static const char *suffix[] = {
402 "/.git", "", ".git/.git", ".git", NULL,
404 const char *gitfile;
405 int len = strlen(path);
406 int i;
407 while ((1 < len) && (path[len-1] == '/'))
408 len--;
411 * We can handle arbitrary-sized buffers, but this remains as a
412 * sanity check on untrusted input.
414 if (PATH_MAX <= len)
415 return NULL;
417 strbuf_reset(&used_path);
418 strbuf_reset(&validated_path);
419 strbuf_add(&used_path, path, len);
420 strbuf_add(&validated_path, path, len);
422 if (used_path.buf[0] == '~') {
423 char *newpath = expand_user_path(used_path.buf);
424 if (!newpath)
425 return NULL;
426 strbuf_attach(&used_path, newpath, strlen(newpath),
427 strlen(newpath));
429 for (i = 0; suffix[i]; i++) {
430 struct stat st;
431 size_t baselen = used_path.len;
432 strbuf_addstr(&used_path, suffix[i]);
433 if (!stat(used_path.buf, &st) &&
434 (S_ISREG(st.st_mode) ||
435 (S_ISDIR(st.st_mode) && is_git_directory(used_path.buf)))) {
436 strbuf_addstr(&validated_path, suffix[i]);
437 break;
439 strbuf_setlen(&used_path, baselen);
441 if (!suffix[i])
442 return NULL;
443 gitfile = read_gitfile(used_path.buf) ;
444 if (gitfile) {
445 strbuf_reset(&used_path);
446 strbuf_addstr(&used_path, gitfile);
448 if (chdir(used_path.buf))
449 return NULL;
450 path = validated_path.buf;
452 else if (chdir(path))
453 return NULL;
455 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
456 validate_headref("HEAD") == 0) {
457 set_git_dir(".");
458 check_repository_format();
459 return path;
462 return NULL;
465 static int calc_shared_perm(int mode)
467 int tweak;
469 if (shared_repository < 0)
470 tweak = -shared_repository;
471 else
472 tweak = shared_repository;
474 if (!(mode & S_IWUSR))
475 tweak &= ~0222;
476 if (mode & S_IXUSR)
477 /* Copy read bits to execute bits */
478 tweak |= (tweak & 0444) >> 2;
479 if (shared_repository < 0)
480 mode = (mode & ~0777) | tweak;
481 else
482 mode |= tweak;
484 return mode;
488 int adjust_shared_perm(const char *path)
490 int old_mode, new_mode;
492 if (!shared_repository)
493 return 0;
494 if (get_st_mode_bits(path, &old_mode) < 0)
495 return -1;
497 new_mode = calc_shared_perm(old_mode);
498 if (S_ISDIR(old_mode)) {
499 /* Copy read bits to execute bits */
500 new_mode |= (new_mode & 0444) >> 2;
501 new_mode |= FORCE_DIR_SET_GID;
504 if (((old_mode ^ new_mode) & ~S_IFMT) &&
505 chmod(path, (new_mode & ~S_IFMT)) < 0)
506 return -2;
507 return 0;
510 static int have_same_root(const char *path1, const char *path2)
512 int is_abs1, is_abs2;
514 is_abs1 = is_absolute_path(path1);
515 is_abs2 = is_absolute_path(path2);
516 return (is_abs1 && is_abs2 && tolower(path1[0]) == tolower(path2[0])) ||
517 (!is_abs1 && !is_abs2);
521 * Give path as relative to prefix.
523 * The strbuf may or may not be used, so do not assume it contains the
524 * returned path.
526 const char *relative_path(const char *in, const char *prefix,
527 struct strbuf *sb)
529 int in_len = in ? strlen(in) : 0;
530 int prefix_len = prefix ? strlen(prefix) : 0;
531 int in_off = 0;
532 int prefix_off = 0;
533 int i = 0, j = 0;
535 if (!in_len)
536 return "./";
537 else if (!prefix_len)
538 return in;
540 if (have_same_root(in, prefix)) {
541 /* bypass dos_drive, for "c:" is identical to "C:" */
542 if (has_dos_drive_prefix(in)) {
543 i = 2;
544 j = 2;
546 } else {
547 return in;
550 while (i < prefix_len && j < in_len && prefix[i] == in[j]) {
551 if (is_dir_sep(prefix[i])) {
552 while (is_dir_sep(prefix[i]))
553 i++;
554 while (is_dir_sep(in[j]))
555 j++;
556 prefix_off = i;
557 in_off = j;
558 } else {
559 i++;
560 j++;
564 if (
565 /* "prefix" seems like prefix of "in" */
566 i >= prefix_len &&
568 * but "/foo" is not a prefix of "/foobar"
569 * (i.e. prefix not end with '/')
571 prefix_off < prefix_len) {
572 if (j >= in_len) {
573 /* in="/a/b", prefix="/a/b" */
574 in_off = in_len;
575 } else if (is_dir_sep(in[j])) {
576 /* in="/a/b/c", prefix="/a/b" */
577 while (is_dir_sep(in[j]))
578 j++;
579 in_off = j;
580 } else {
581 /* in="/a/bbb/c", prefix="/a/b" */
582 i = prefix_off;
584 } else if (
585 /* "in" is short than "prefix" */
586 j >= in_len &&
587 /* "in" not end with '/' */
588 in_off < in_len) {
589 if (is_dir_sep(prefix[i])) {
590 /* in="/a/b", prefix="/a/b/c/" */
591 while (is_dir_sep(prefix[i]))
592 i++;
593 in_off = in_len;
596 in += in_off;
597 in_len -= in_off;
599 if (i >= prefix_len) {
600 if (!in_len)
601 return "./";
602 else
603 return in;
606 strbuf_reset(sb);
607 strbuf_grow(sb, in_len);
609 while (i < prefix_len) {
610 if (is_dir_sep(prefix[i])) {
611 strbuf_addstr(sb, "../");
612 while (is_dir_sep(prefix[i]))
613 i++;
614 continue;
616 i++;
618 if (!is_dir_sep(prefix[prefix_len - 1]))
619 strbuf_addstr(sb, "../");
621 strbuf_addstr(sb, in);
623 return sb->buf;
627 * A simpler implementation of relative_path
629 * Get relative path by removing "prefix" from "in". This function
630 * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter
631 * to increase performance when traversing the path to work_tree.
633 const char *remove_leading_path(const char *in, const char *prefix)
635 static struct strbuf buf = STRBUF_INIT;
636 int i = 0, j = 0;
638 if (!prefix || !prefix[0])
639 return in;
640 while (prefix[i]) {
641 if (is_dir_sep(prefix[i])) {
642 if (!is_dir_sep(in[j]))
643 return in;
644 while (is_dir_sep(prefix[i]))
645 i++;
646 while (is_dir_sep(in[j]))
647 j++;
648 continue;
649 } else if (in[j] != prefix[i]) {
650 return in;
652 i++;
653 j++;
655 if (
656 /* "/foo" is a prefix of "/foo" */
657 in[j] &&
658 /* "/foo" is not a prefix of "/foobar" */
659 !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j])
661 return in;
662 while (is_dir_sep(in[j]))
663 j++;
665 strbuf_reset(&buf);
666 if (!in[j])
667 strbuf_addstr(&buf, ".");
668 else
669 strbuf_addstr(&buf, in + j);
670 return buf.buf;
674 * It is okay if dst == src, but they should not overlap otherwise.
676 * Performs the following normalizations on src, storing the result in dst:
677 * - Ensures that components are separated by '/' (Windows only)
678 * - Squashes sequences of '/'.
679 * - Removes "." components.
680 * - Removes ".." components, and the components the precede them.
681 * Returns failure (non-zero) if a ".." component appears as first path
682 * component anytime during the normalization. Otherwise, returns success (0).
684 * Note that this function is purely textual. It does not follow symlinks,
685 * verify the existence of the path, or make any system calls.
687 * prefix_len != NULL is for a specific case of prefix_pathspec():
688 * assume that src == dst and src[0..prefix_len-1] is already
689 * normalized, any time "../" eats up to the prefix_len part,
690 * prefix_len is reduced. In the end prefix_len is the remaining
691 * prefix that has not been overridden by user pathspec.
693 int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
695 char *dst0;
697 if (has_dos_drive_prefix(src)) {
698 *dst++ = *src++;
699 *dst++ = *src++;
701 dst0 = dst;
703 if (is_dir_sep(*src)) {
704 *dst++ = '/';
705 while (is_dir_sep(*src))
706 src++;
709 for (;;) {
710 char c = *src;
713 * A path component that begins with . could be
714 * special:
715 * (1) "." and ends -- ignore and terminate.
716 * (2) "./" -- ignore them, eat slash and continue.
717 * (3) ".." and ends -- strip one and terminate.
718 * (4) "../" -- strip one, eat slash and continue.
720 if (c == '.') {
721 if (!src[1]) {
722 /* (1) */
723 src++;
724 } else if (is_dir_sep(src[1])) {
725 /* (2) */
726 src += 2;
727 while (is_dir_sep(*src))
728 src++;
729 continue;
730 } else if (src[1] == '.') {
731 if (!src[2]) {
732 /* (3) */
733 src += 2;
734 goto up_one;
735 } else if (is_dir_sep(src[2])) {
736 /* (4) */
737 src += 3;
738 while (is_dir_sep(*src))
739 src++;
740 goto up_one;
745 /* copy up to the next '/', and eat all '/' */
746 while ((c = *src++) != '\0' && !is_dir_sep(c))
747 *dst++ = c;
748 if (is_dir_sep(c)) {
749 *dst++ = '/';
750 while (is_dir_sep(c))
751 c = *src++;
752 src--;
753 } else if (!c)
754 break;
755 continue;
757 up_one:
759 * dst0..dst is prefix portion, and dst[-1] is '/';
760 * go up one level.
762 dst--; /* go to trailing '/' */
763 if (dst <= dst0)
764 return -1;
765 /* Windows: dst[-1] cannot be backslash anymore */
766 while (dst0 < dst && dst[-1] != '/')
767 dst--;
768 if (prefix_len && *prefix_len > dst - dst0)
769 *prefix_len = dst - dst0;
771 *dst = '\0';
772 return 0;
775 int normalize_path_copy(char *dst, const char *src)
777 return normalize_path_copy_len(dst, src, NULL);
781 * path = Canonical absolute path
782 * prefixes = string_list containing normalized, absolute paths without
783 * trailing slashes (except for the root directory, which is denoted by "/").
785 * Determines, for each path in prefixes, whether the "prefix"
786 * is an ancestor directory of path. Returns the length of the longest
787 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
788 * is an ancestor. (Note that this means 0 is returned if prefixes is
789 * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories
790 * are not considered to be their own ancestors. path must be in a
791 * canonical form: empty components, or "." or ".." components are not
792 * allowed.
794 int longest_ancestor_length(const char *path, struct string_list *prefixes)
796 int i, max_len = -1;
798 if (!strcmp(path, "/"))
799 return -1;
801 for (i = 0; i < prefixes->nr; i++) {
802 const char *ceil = prefixes->items[i].string;
803 int len = strlen(ceil);
805 if (len == 1 && ceil[0] == '/')
806 len = 0; /* root matches anything, with length 0 */
807 else if (!strncmp(path, ceil, len) && path[len] == '/')
808 ; /* match of length len */
809 else
810 continue; /* no match */
812 if (len > max_len)
813 max_len = len;
816 return max_len;
819 /* strip arbitrary amount of directory separators at end of path */
820 static inline int chomp_trailing_dir_sep(const char *path, int len)
822 while (len && is_dir_sep(path[len - 1]))
823 len--;
824 return len;
828 * If path ends with suffix (complete path components), returns the
829 * part before suffix (sans trailing directory separators).
830 * Otherwise returns NULL.
832 char *strip_path_suffix(const char *path, const char *suffix)
834 int path_len = strlen(path), suffix_len = strlen(suffix);
836 while (suffix_len) {
837 if (!path_len)
838 return NULL;
840 if (is_dir_sep(path[path_len - 1])) {
841 if (!is_dir_sep(suffix[suffix_len - 1]))
842 return NULL;
843 path_len = chomp_trailing_dir_sep(path, path_len);
844 suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
846 else if (path[--path_len] != suffix[--suffix_len])
847 return NULL;
850 if (path_len && !is_dir_sep(path[path_len - 1]))
851 return NULL;
852 return xstrndup(path, chomp_trailing_dir_sep(path, path_len));
855 int daemon_avoid_alias(const char *p)
857 int sl, ndot;
860 * This resurrects the belts and suspenders paranoia check by HPA
861 * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
862 * does not do getcwd() based path canonicalization.
864 * sl becomes true immediately after seeing '/' and continues to
865 * be true as long as dots continue after that without intervening
866 * non-dot character.
868 if (!p || (*p != '/' && *p != '~'))
869 return -1;
870 sl = 1; ndot = 0;
871 p++;
873 while (1) {
874 char ch = *p++;
875 if (sl) {
876 if (ch == '.')
877 ndot++;
878 else if (ch == '/') {
879 if (ndot < 3)
880 /* reject //, /./ and /../ */
881 return -1;
882 ndot = 0;
884 else if (ch == 0) {
885 if (0 < ndot && ndot < 3)
886 /* reject /.$ and /..$ */
887 return -1;
888 return 0;
890 else
891 sl = ndot = 0;
893 else if (ch == 0)
894 return 0;
895 else if (ch == '/') {
896 sl = 1;
897 ndot = 0;
902 static int only_spaces_and_periods(const char *path, size_t len, size_t skip)
904 if (len < skip)
905 return 0;
906 len -= skip;
907 path += skip;
908 while (len-- > 0) {
909 char c = *(path++);
910 if (c != ' ' && c != '.')
911 return 0;
913 return 1;
916 int is_ntfs_dotgit(const char *name)
918 int len;
920 for (len = 0; ; len++)
921 if (!name[len] || name[len] == '\\' || is_dir_sep(name[len])) {
922 if (only_spaces_and_periods(name, len, 4) &&
923 !strncasecmp(name, ".git", 4))
924 return 1;
925 if (only_spaces_and_periods(name, len, 5) &&
926 !strncasecmp(name, "git~1", 5))
927 return 1;
928 if (name[len] != '\\')
929 return 0;
930 name += len + 1;
931 len = -1;
935 char *xdg_config_home(const char *filename)
937 const char *home, *config_home;
939 assert(filename);
940 config_home = getenv("XDG_CONFIG_HOME");
941 if (config_home && *config_home)
942 return mkpathdup("%s/git/%s", config_home, filename);
944 home = getenv("HOME");
945 if (home)
946 return mkpathdup("%s/.config/git/%s", home, filename);
947 return NULL;
950 GIT_PATH_FUNC(git_path_cherry_pick_head, "CHERRY_PICK_HEAD")
951 GIT_PATH_FUNC(git_path_revert_head, "REVERT_HEAD")
952 GIT_PATH_FUNC(git_path_squash_msg, "SQUASH_MSG")
953 GIT_PATH_FUNC(git_path_merge_msg, "MERGE_MSG")
954 GIT_PATH_FUNC(git_path_merge_rr, "MERGE_RR")
955 GIT_PATH_FUNC(git_path_merge_mode, "MERGE_MODE")
956 GIT_PATH_FUNC(git_path_merge_head, "MERGE_HEAD")
957 GIT_PATH_FUNC(git_path_fetch_head, "FETCH_HEAD")
958 GIT_PATH_FUNC(git_path_shallow, "shallow")