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