2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
5 * See Documentation/technical/api-directory-listing.txt
7 * Copyright (C) Linus Torvalds, 2005-2006
8 * Junio Hamano, 2005-2006
13 #include "wildmatch.h"
15 struct path_simplify
{
20 static int read_directory_recursive(struct dir_struct
*dir
, const char *path
, int len
,
21 int check_only
, const struct path_simplify
*simplify
);
22 static int get_dtype(struct dirent
*de
, const char *path
, int len
);
24 /* helper string functions with support for the ignore_case flag */
25 int strcmp_icase(const char *a
, const char *b
)
27 return ignore_case
? strcasecmp(a
, b
) : strcmp(a
, b
);
30 int strncmp_icase(const char *a
, const char *b
, size_t count
)
32 return ignore_case
? strncasecmp(a
, b
, count
) : strncmp(a
, b
, count
);
35 int fnmatch_icase(const char *pattern
, const char *string
, int flags
)
37 return fnmatch(pattern
, string
, flags
| (ignore_case
? FNM_CASEFOLD
: 0));
40 inline int git_fnmatch(const char *pattern
, const char *string
,
41 int flags
, int prefix
)
44 if (flags
& GFNM_PATHNAME
)
45 fnm_flags
|= FNM_PATHNAME
;
47 if (strncmp(pattern
, string
, prefix
))
52 if (flags
& GFNM_ONESTAR
) {
53 int pattern_len
= strlen(++pattern
);
54 int string_len
= strlen(string
);
55 return string_len
< pattern_len
||
57 string
+ string_len
- pattern_len
);
59 return fnmatch(pattern
, string
, fnm_flags
);
62 static int fnmatch_icase_mem(const char *pattern
, int patternlen
,
63 const char *string
, int stringlen
,
67 struct strbuf pat_buf
= STRBUF_INIT
;
68 struct strbuf str_buf
= STRBUF_INIT
;
69 const char *use_pat
= pattern
;
70 const char *use_str
= string
;
72 if (pattern
[patternlen
]) {
73 strbuf_add(&pat_buf
, pattern
, patternlen
);
74 use_pat
= pat_buf
.buf
;
76 if (string
[stringlen
]) {
77 strbuf_add(&str_buf
, string
, stringlen
);
78 use_str
= str_buf
.buf
;
83 match_status
= wildmatch(use_pat
, use_str
, flags
, NULL
);
85 strbuf_release(&pat_buf
);
86 strbuf_release(&str_buf
);
91 static size_t common_prefix_len(const char **pathspec
)
93 const char *n
, *first
;
95 int literal
= limit_pathspec_to_literal();
101 while ((n
= *pathspec
++)) {
103 for (i
= 0; first
== n
|| i
< max
; i
++) {
105 if (!c
|| c
!= first
[i
] || (!literal
&& is_glob_special(c
)))
110 if (first
== n
|| len
< max
) {
120 * Returns a copy of the longest leading path common among all
123 char *common_prefix(const char **pathspec
)
125 unsigned long len
= common_prefix_len(pathspec
);
127 return len
? xmemdupz(*pathspec
, len
) : NULL
;
130 int fill_directory(struct dir_struct
*dir
, const char **pathspec
)
135 * Calculate common prefix for the pathspec, and
136 * use that to optimize the directory walk
138 len
= common_prefix_len(pathspec
);
140 /* Read the directory and prune it */
141 read_directory(dir
, pathspec
? *pathspec
: "", len
, pathspec
);
145 int within_depth(const char *name
, int namelen
,
146 int depth
, int max_depth
)
148 const char *cp
= name
, *cpe
= name
+ namelen
;
154 if (depth
> max_depth
)
161 * Does 'match' match the given name?
162 * A match is found if
164 * (1) the 'match' string is leading directory of 'name', or
165 * (2) the 'match' string is a wildcard and matches 'name', or
166 * (3) the 'match' string is exactly the same as 'name'.
168 * and the return value tells which case it was.
170 * It returns 0 when there is no match.
172 static int match_one(const char *match
, const char *name
, int namelen
)
175 int literal
= limit_pathspec_to_literal();
177 /* If the match was just the prefix, we matched */
179 return MATCHED_RECURSIVELY
;
183 unsigned char c1
= tolower(*match
);
184 unsigned char c2
= tolower(*name
);
185 if (c1
== '\0' || (!literal
&& is_glob_special(c1
)))
195 unsigned char c1
= *match
;
196 unsigned char c2
= *name
;
197 if (c1
== '\0' || (!literal
&& is_glob_special(c1
)))
208 * If we don't match the matchstring exactly,
209 * we need to match by fnmatch
211 matchlen
= strlen(match
);
212 if (strncmp_icase(match
, name
, matchlen
)) {
215 return !fnmatch_icase(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
218 if (namelen
== matchlen
)
219 return MATCHED_EXACTLY
;
220 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
221 return MATCHED_RECURSIVELY
;
226 * Given a name and a list of pathspecs, returns the nature of the
227 * closest (i.e. most specific) match of the name to any of the
230 * The caller typically calls this multiple times with the same
231 * pathspec and seen[] array but with different name/namelen
232 * (e.g. entries from the index) and is interested in seeing if and
233 * how each pathspec matches all the names it calls this function
234 * with. A mark is left in the seen[] array for each pathspec element
235 * indicating the closest type of match that element achieved, so if
236 * seen[n] remains zero after multiple invocations, that means the nth
237 * pathspec did not match any names, which could indicate that the
238 * user mistyped the nth pathspec.
240 int match_pathspec(const char **pathspec
, const char *name
, int namelen
,
241 int prefix
, char *seen
)
251 for (i
= 0; pathspec
[i
] != NULL
; i
++) {
253 const char *match
= pathspec
[i
] + prefix
;
254 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
256 how
= match_one(match
, name
, namelen
);
260 if (seen
&& seen
[i
] < how
)
268 * Does 'match' match the given name?
269 * A match is found if
271 * (1) the 'match' string is leading directory of 'name', or
272 * (2) the 'match' string is a wildcard and matches 'name', or
273 * (3) the 'match' string is exactly the same as 'name'.
275 * and the return value tells which case it was.
277 * It returns 0 when there is no match.
279 static int match_pathspec_item(const struct pathspec_item
*item
, int prefix
,
280 const char *name
, int namelen
)
282 /* name/namelen has prefix cut off by caller */
283 const char *match
= item
->match
+ prefix
;
284 int matchlen
= item
->len
- prefix
;
286 /* If the match was just the prefix, we matched */
288 return MATCHED_RECURSIVELY
;
290 if (matchlen
<= namelen
&& !strncmp(match
, name
, matchlen
)) {
291 if (matchlen
== namelen
)
292 return MATCHED_EXACTLY
;
294 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
295 return MATCHED_RECURSIVELY
;
298 if (item
->nowildcard_len
< item
->len
&&
299 !git_fnmatch(match
, name
,
300 item
->flags
& PATHSPEC_ONESTAR
? GFNM_ONESTAR
: 0,
301 item
->nowildcard_len
- prefix
))
302 return MATCHED_FNMATCH
;
308 * Given a name and a list of pathspecs, returns the nature of the
309 * closest (i.e. most specific) match of the name to any of the
312 * The caller typically calls this multiple times with the same
313 * pathspec and seen[] array but with different name/namelen
314 * (e.g. entries from the index) and is interested in seeing if and
315 * how each pathspec matches all the names it calls this function
316 * with. A mark is left in the seen[] array for each pathspec element
317 * indicating the closest type of match that element achieved, so if
318 * seen[n] remains zero after multiple invocations, that means the nth
319 * pathspec did not match any names, which could indicate that the
320 * user mistyped the nth pathspec.
322 int match_pathspec_depth(const struct pathspec
*ps
,
323 const char *name
, int namelen
,
324 int prefix
, char *seen
)
329 if (!ps
->recursive
|| ps
->max_depth
== -1)
330 return MATCHED_RECURSIVELY
;
332 if (within_depth(name
, namelen
, 0, ps
->max_depth
))
333 return MATCHED_EXACTLY
;
341 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
343 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
345 how
= match_pathspec_item(ps
->items
+i
, prefix
, name
, namelen
);
346 if (ps
->recursive
&& ps
->max_depth
!= -1 &&
347 how
&& how
!= MATCHED_FNMATCH
) {
348 int len
= ps
->items
[i
].len
;
349 if (name
[len
] == '/')
351 if (within_depth(name
+len
, namelen
-len
, 0, ps
->max_depth
))
352 how
= MATCHED_EXACTLY
;
359 if (seen
&& seen
[i
] < how
)
367 * Return the length of the "simple" part of a path match limiter.
369 static int simple_length(const char *match
)
374 unsigned char c
= *match
++;
376 if (c
== '\0' || is_glob_special(c
))
381 static int no_wildcard(const char *string
)
383 return string
[simple_length(string
)] == '\0';
386 void parse_exclude_pattern(const char **pattern
,
391 const char *p
= *pattern
;
396 *flags
|= EXC_FLAG_NEGATIVE
;
400 if (len
&& p
[len
- 1] == '/') {
402 *flags
|= EXC_FLAG_MUSTBEDIR
;
404 for (i
= 0; i
< len
; i
++) {
409 *flags
|= EXC_FLAG_NODIR
;
410 *nowildcardlen
= simple_length(p
);
412 * we should have excluded the trailing slash from 'p' too,
413 * but that's one more allocation. Instead just make sure
414 * nowildcardlen does not exceed real patternlen
416 if (*nowildcardlen
> len
)
417 *nowildcardlen
= len
;
418 if (*p
== '*' && no_wildcard(p
+ 1))
419 *flags
|= EXC_FLAG_ENDSWITH
;
424 void add_exclude(const char *string
, const char *base
,
425 int baselen
, struct exclude_list
*el
, int srcpos
)
432 parse_exclude_pattern(&string
, &patternlen
, &flags
, &nowildcardlen
);
433 if (flags
& EXC_FLAG_MUSTBEDIR
) {
435 x
= xmalloc(sizeof(*x
) + patternlen
+ 1);
437 memcpy(s
, string
, patternlen
);
438 s
[patternlen
] = '\0';
441 x
= xmalloc(sizeof(*x
));
444 x
->patternlen
= patternlen
;
445 x
->nowildcardlen
= nowildcardlen
;
447 x
->baselen
= baselen
;
450 ALLOC_GROW(el
->excludes
, el
->nr
+ 1, el
->alloc
);
451 el
->excludes
[el
->nr
++] = x
;
455 static void *read_skip_worktree_file_from_index(const char *path
, size_t *size
)
459 enum object_type type
;
461 struct index_state
*istate
= &the_index
;
464 pos
= index_name_pos(istate
, path
, len
);
467 if (!ce_skip_worktree(istate
->cache
[pos
]))
469 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
470 if (!data
|| type
!= OBJ_BLOB
) {
479 * Frees memory within el which was allocated for exclude patterns and
480 * the file buffer. Does not free el itself.
482 void clear_exclude_list(struct exclude_list
*el
)
486 for (i
= 0; i
< el
->nr
; i
++)
487 free(el
->excludes
[i
]);
496 int add_excludes_from_file_to_list(const char *fname
,
499 struct exclude_list
*el
,
503 int fd
, i
, lineno
= 1;
507 fd
= open(fname
, O_RDONLY
);
508 if (fd
< 0 || fstat(fd
, &st
) < 0) {
510 warn_on_inaccessible(fname
);
514 (buf
= read_skip_worktree_file_from_index(fname
, &size
)) == NULL
)
520 if (buf
[size
-1] != '\n') {
521 buf
= xrealloc(buf
, size
+1);
526 size
= xsize_t(st
.st_size
);
531 buf
= xmalloc(size
+1);
532 if (read_in_full(fd
, buf
, size
) != size
) {
543 for (i
= 0; i
< size
; i
++) {
544 if (buf
[i
] == '\n') {
545 if (entry
!= buf
+ i
&& entry
[0] != '#') {
546 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
547 add_exclude(entry
, base
, baselen
, el
, lineno
);
556 struct exclude_list
*add_exclude_list(struct dir_struct
*dir
,
557 int group_type
, const char *src
)
559 struct exclude_list
*el
;
560 struct exclude_list_group
*group
;
562 group
= &dir
->exclude_list_group
[group_type
];
563 ALLOC_GROW(group
->el
, group
->nr
+ 1, group
->alloc
);
564 el
= &group
->el
[group
->nr
++];
565 memset(el
, 0, sizeof(*el
));
571 * Used to set up core.excludesfile and .git/info/exclude lists.
573 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
575 struct exclude_list
*el
;
576 el
= add_exclude_list(dir
, EXC_FILE
, fname
);
577 if (add_excludes_from_file_to_list(fname
, "", 0, el
, 0) < 0)
578 die("cannot use %s as an exclude file", fname
);
581 int match_basename(const char *basename
, int basenamelen
,
582 const char *pattern
, int prefix
, int patternlen
,
585 if (prefix
== patternlen
) {
586 if (patternlen
== basenamelen
&&
587 !strncmp_icase(pattern
, basename
, basenamelen
))
589 } else if (flags
& EXC_FLAG_ENDSWITH
) {
590 /* "*literal" matching against "fooliteral" */
591 if (patternlen
- 1 <= basenamelen
&&
592 !strncmp_icase(pattern
+ 1,
593 basename
+ basenamelen
- (patternlen
- 1),
597 if (fnmatch_icase_mem(pattern
, patternlen
,
598 basename
, basenamelen
,
605 int match_pathname(const char *pathname
, int pathlen
,
606 const char *base
, int baselen
,
607 const char *pattern
, int prefix
, int patternlen
,
614 * match with FNM_PATHNAME; the pattern has base implicitly
617 if (*pattern
== '/') {
624 * baselen does not count the trailing slash. base[] may or
625 * may not end with a trailing slash though.
627 if (pathlen
< baselen
+ 1 ||
628 (baselen
&& pathname
[baselen
] != '/') ||
629 strncmp_icase(pathname
, base
, baselen
))
632 namelen
= baselen
? pathlen
- baselen
- 1 : pathlen
;
633 name
= pathname
+ pathlen
- namelen
;
637 * if the non-wildcard part is longer than the
638 * remaining pathname, surely it cannot match.
640 if (prefix
> namelen
)
643 if (strncmp_icase(pattern
, name
, prefix
))
646 patternlen
-= prefix
;
651 * If the whole pattern did not have a wildcard,
652 * then our prefix match is all we need; we
653 * do not need to call fnmatch at all.
655 if (!patternlen
&& !namelen
)
659 return fnmatch_icase_mem(pattern
, patternlen
,
665 * Scan the given exclude list in reverse to see whether pathname
666 * should be ignored. The first match (i.e. the last on the list), if
667 * any, determines the fate. Returns the exclude_list element which
668 * matched, or NULL for undecided.
670 static struct exclude
*last_exclude_matching_from_list(const char *pathname
,
672 const char *basename
,
674 struct exclude_list
*el
)
679 return NULL
; /* undefined */
681 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
682 struct exclude
*x
= el
->excludes
[i
];
683 const char *exclude
= x
->pattern
;
684 int prefix
= x
->nowildcardlen
;
686 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
687 if (*dtype
== DT_UNKNOWN
)
688 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
689 if (*dtype
!= DT_DIR
)
693 if (x
->flags
& EXC_FLAG_NODIR
) {
694 if (match_basename(basename
,
695 pathlen
- (basename
- pathname
),
696 exclude
, prefix
, x
->patternlen
,
702 assert(x
->baselen
== 0 || x
->base
[x
->baselen
- 1] == '/');
703 if (match_pathname(pathname
, pathlen
,
704 x
->base
, x
->baselen
? x
->baselen
- 1 : 0,
705 exclude
, prefix
, x
->patternlen
, x
->flags
))
708 return NULL
; /* undecided */
712 * Scan the list and let the last match determine the fate.
713 * Return 1 for exclude, 0 for include and -1 for undecided.
715 int is_excluded_from_list(const char *pathname
,
716 int pathlen
, const char *basename
, int *dtype
,
717 struct exclude_list
*el
)
719 struct exclude
*exclude
;
720 exclude
= last_exclude_matching_from_list(pathname
, pathlen
, basename
, dtype
, el
);
722 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
723 return -1; /* undecided */
726 static struct exclude
*last_exclude_matching_from_lists(struct dir_struct
*dir
,
727 const char *pathname
, int pathlen
, const char *basename
,
731 struct exclude_list_group
*group
;
732 struct exclude
*exclude
;
733 for (i
= EXC_CMDL
; i
<= EXC_FILE
; i
++) {
734 group
= &dir
->exclude_list_group
[i
];
735 for (j
= group
->nr
- 1; j
>= 0; j
--) {
736 exclude
= last_exclude_matching_from_list(
737 pathname
, pathlen
, basename
, dtype_p
,
747 * Loads the per-directory exclude list for the substring of base
748 * which has a char length of baselen.
750 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
752 struct exclude_list_group
*group
;
753 struct exclude_list
*el
;
754 struct exclude_stack
*stk
= NULL
;
757 group
= &dir
->exclude_list_group
[EXC_DIRS
];
759 /* Pop the exclude lists from the EXCL_DIRS exclude_list_group
760 * which originate from directories not in the prefix of the
761 * path being checked. */
762 while ((stk
= dir
->exclude_stack
) != NULL
) {
763 if (stk
->baselen
<= baselen
&&
764 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
766 el
= &group
->el
[dir
->exclude_stack
->exclude_ix
];
767 dir
->exclude_stack
= stk
->prev
;
769 free((char *)el
->src
); /* see strdup() below */
770 clear_exclude_list(el
);
775 /* Skip traversing into sub directories if the parent is excluded */
779 /* Read from the parent directories and push them down. */
780 current
= stk
? stk
->baselen
: -1;
781 while (current
< baselen
) {
782 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
790 cp
= strchr(base
+ current
+ 1, '/');
792 die("oops in prep_exclude");
795 stk
->prev
= dir
->exclude_stack
;
796 stk
->baselen
= cp
- base
;
797 stk
->exclude_ix
= group
->nr
;
798 el
= add_exclude_list(dir
, EXC_DIRS
, NULL
);
799 memcpy(dir
->basebuf
+ current
, base
+ current
,
800 stk
->baselen
- current
);
802 /* Abort if the directory is excluded */
805 dir
->basebuf
[stk
->baselen
- 1] = 0;
806 dir
->exclude
= last_exclude_matching_from_lists(dir
,
807 dir
->basebuf
, stk
->baselen
- 1,
808 dir
->basebuf
+ current
, &dt
);
809 dir
->basebuf
[stk
->baselen
- 1] = '/';
811 dir
->basebuf
[stk
->baselen
] = 0;
812 dir
->exclude_stack
= stk
;
817 /* Try to read per-directory file unless path is too long */
818 if (dir
->exclude_per_dir
&&
819 stk
->baselen
+ strlen(dir
->exclude_per_dir
) < PATH_MAX
) {
820 strcpy(dir
->basebuf
+ stk
->baselen
,
821 dir
->exclude_per_dir
);
823 * dir->basebuf gets reused by the traversal, but we
824 * need fname to remain unchanged to ensure the src
825 * member of each struct exclude correctly
826 * back-references its source file. Other invocations
827 * of add_exclude_list provide stable strings, so we
828 * strdup() and free() here in the caller.
830 el
->src
= strdup(dir
->basebuf
);
831 add_excludes_from_file_to_list(dir
->basebuf
,
832 dir
->basebuf
, stk
->baselen
, el
, 1);
834 dir
->exclude_stack
= stk
;
835 current
= stk
->baselen
;
837 dir
->basebuf
[baselen
] = '\0';
841 * Loads the exclude lists for the directory containing pathname, then
842 * scans all exclude lists to determine whether pathname is excluded.
843 * Returns the exclude_list element which matched, or NULL for
846 struct exclude
*last_exclude_matching(struct dir_struct
*dir
,
847 const char *pathname
,
850 int pathlen
= strlen(pathname
);
851 const char *basename
= strrchr(pathname
, '/');
852 basename
= (basename
) ? basename
+1 : pathname
;
854 prep_exclude(dir
, pathname
, basename
-pathname
);
857 return last_exclude_matching_from_lists(dir
, pathname
, pathlen
,
862 * Loads the exclude lists for the directory containing pathname, then
863 * scans all exclude lists to determine whether pathname is excluded.
864 * Returns 1 if true, otherwise 0.
866 int is_excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
868 struct exclude
*exclude
=
869 last_exclude_matching(dir
, pathname
, dtype_p
);
871 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
875 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
877 struct dir_entry
*ent
;
879 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
881 memcpy(ent
->name
, pathname
, len
);
886 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
888 if (!(dir
->flags
& DIR_SHOW_IGNORED
) &&
889 cache_name_exists(pathname
, len
, ignore_case
))
892 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
893 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
896 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
898 if (!cache_name_is_other(pathname
, len
))
901 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
902 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
906 index_nonexistent
= 0,
912 * Do not use the alphabetically stored index to look up
913 * the directory name; instead, use the case insensitive
916 static enum exist_status
directory_exists_in_index_icase(const char *dirname
, int len
)
918 struct cache_entry
*ce
= index_name_exists(&the_index
, dirname
, len
+ 1, ignore_case
);
919 unsigned char endchar
;
922 return index_nonexistent
;
923 endchar
= ce
->name
[len
];
926 * The cache_entry structure returned will contain this dirname
927 * and possibly additional path components.
930 return index_directory
;
933 * If there are no additional path components, then this cache_entry
934 * represents a submodule. Submodules, despite being directories,
935 * are stored in the cache without a closing slash.
937 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
940 /* This should never be hit, but it exists just in case. */
941 return index_nonexistent
;
945 * The index sorts alphabetically by entry name, which
946 * means that a gitlink sorts as '\0' at the end, while
947 * a directory (which is defined not as an entry, but as
948 * the files it contains) will sort with the '/' at the
951 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
956 return directory_exists_in_index_icase(dirname
, len
);
958 pos
= cache_name_pos(dirname
, len
);
961 while (pos
< active_nr
) {
962 struct cache_entry
*ce
= active_cache
[pos
++];
963 unsigned char endchar
;
965 if (strncmp(ce
->name
, dirname
, len
))
967 endchar
= ce
->name
[len
];
971 return index_directory
;
972 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
975 return index_nonexistent
;
979 * When we find a directory when traversing the filesystem, we
980 * have three distinct cases:
983 * - see it as a directory
986 * and which one we choose depends on a combination of existing
987 * git index contents and the flags passed into the directory
990 * Case 1: If we *already* have entries in the index under that
991 * directory name, we recurse into the directory to see all the files,
992 * unless the directory is excluded and we want to show ignored
995 * Case 2: If we *already* have that directory name as a gitlink,
996 * we always continue to see it as a gitlink, regardless of whether
997 * there is an actual git directory there or not (it might not
998 * be checked out as a subproject!)
1000 * Case 3: if we didn't have it in the index previously, we
1001 * have a few sub-cases:
1003 * (a) if "show_other_directories" is true, we show it as
1004 * just a directory, unless "hide_empty_directories" is
1005 * also true and the directory is empty, in which case
1006 * we just ignore it entirely.
1007 * if we are looking for ignored directories, look if it
1008 * contains only ignored files to decide if it must be shown as
1010 * (b) if it looks like a git directory, and we don't have
1011 * 'no_gitlinks' set we treat it as a gitlink, and show it
1013 * (c) otherwise, we recurse into it.
1015 enum directory_treatment
{
1018 recurse_into_directory
1021 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
1022 const char *dirname
, int len
, int exclude
,
1023 const struct path_simplify
*simplify
)
1025 /* The "len-1" is to strip the final '/' */
1026 switch (directory_exists_in_index(dirname
, len
-1)) {
1027 case index_directory
:
1028 if ((dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
) && exclude
)
1031 return recurse_into_directory
;
1034 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
1035 return ignore_directory
;
1036 return show_directory
;
1038 case index_nonexistent
:
1039 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
1041 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
1042 unsigned char sha1
[20];
1043 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
1044 return show_directory
;
1046 return recurse_into_directory
;
1049 /* This is the "show_other_directories" case */
1052 * We are looking for ignored files and our directory is not ignored,
1053 * check if it contains untracked files (i.e. is listed as untracked)
1055 if ((dir
->flags
& DIR_SHOW_IGNORED
) && !exclude
) {
1057 dir
->flags
&= ~(DIR_SHOW_IGNORED
|DIR_SHOW_OTHER_DIRECTORIES
);
1058 untracked
= read_directory_recursive(dir
, dirname
, len
, 1, simplify
);
1059 dir
->flags
|= DIR_SHOW_IGNORED
|DIR_SHOW_OTHER_DIRECTORIES
;
1062 return ignore_directory
;
1064 if (!(dir
->flags
& DIR_SHOW_IGNORED
) &&
1065 !(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
1066 return show_directory
;
1067 if (!read_directory_recursive(dir
, dirname
, len
, 1, simplify
))
1068 return ignore_directory
;
1069 return show_directory
;
1073 * This is an inexact early pruning of any recursive directory
1074 * reading - if the path cannot possibly be in the pathspec,
1075 * return true, and we'll skip it early.
1077 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
1081 const char *match
= simplify
->path
;
1082 int len
= simplify
->len
;
1088 if (!memcmp(path
, match
, len
))
1098 * This function tells us whether an excluded path matches a
1099 * list of "interesting" pathspecs. That is, whether a path matched
1100 * by any of the pathspecs could possibly be ignored by excluding
1101 * the specified path. This can happen if:
1103 * 1. the path is mentioned explicitly in the pathspec
1105 * 2. the path is a directory prefix of some element in the
1108 static int exclude_matches_pathspec(const char *path
, int len
,
1109 const struct path_simplify
*simplify
)
1112 for (; simplify
->path
; simplify
++) {
1113 if (len
== simplify
->len
1114 && !memcmp(path
, simplify
->path
, len
))
1116 if (len
< simplify
->len
1117 && simplify
->path
[len
] == '/'
1118 && !memcmp(path
, simplify
->path
, len
))
1125 static int get_index_dtype(const char *path
, int len
)
1128 struct cache_entry
*ce
;
1130 ce
= cache_name_exists(path
, len
, 0);
1132 if (!ce_uptodate(ce
))
1134 if (S_ISGITLINK(ce
->ce_mode
))
1137 * Nobody actually cares about the
1138 * difference between DT_LNK and DT_REG
1143 /* Try to look it up as a directory */
1144 pos
= cache_name_pos(path
, len
);
1148 while (pos
< active_nr
) {
1149 ce
= active_cache
[pos
++];
1150 if (strncmp(ce
->name
, path
, len
))
1152 if (ce
->name
[len
] > '/')
1154 if (ce
->name
[len
] < '/')
1156 if (!ce_uptodate(ce
))
1157 break; /* continue? */
1163 static int get_dtype(struct dirent
*de
, const char *path
, int len
)
1165 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
1168 if (dtype
!= DT_UNKNOWN
)
1170 dtype
= get_index_dtype(path
, len
);
1171 if (dtype
!= DT_UNKNOWN
)
1173 if (lstat(path
, &st
))
1175 if (S_ISREG(st
.st_mode
))
1177 if (S_ISDIR(st
.st_mode
))
1179 if (S_ISLNK(st
.st_mode
))
1184 enum path_treatment
{
1190 static enum path_treatment
treat_one_path(struct dir_struct
*dir
,
1191 struct strbuf
*path
,
1192 const struct path_simplify
*simplify
,
1193 int dtype
, struct dirent
*de
)
1196 if (dtype
== DT_UNKNOWN
)
1197 dtype
= get_dtype(de
, path
->buf
, path
->len
);
1199 /* Always exclude indexed files */
1200 if (dtype
!= DT_DIR
&&
1201 cache_name_exists(path
->buf
, path
->len
, ignore_case
))
1202 return path_ignored
;
1204 exclude
= is_excluded(dir
, path
->buf
, &dtype
);
1205 if (exclude
&& (dir
->flags
& DIR_COLLECT_IGNORED
)
1206 && exclude_matches_pathspec(path
->buf
, path
->len
, simplify
))
1207 dir_add_ignored(dir
, path
->buf
, path
->len
);
1210 * Excluded? If we don't explicitly want to show
1211 * ignored files, ignore it
1213 if (exclude
&& !(dir
->flags
& DIR_SHOW_IGNORED
))
1214 return path_ignored
;
1218 return path_ignored
;
1220 strbuf_addch(path
, '/');
1221 switch (treat_directory(dir
, path
->buf
, path
->len
, exclude
, simplify
)) {
1222 case show_directory
:
1224 case recurse_into_directory
:
1225 return path_recurse
;
1226 case ignore_directory
:
1227 return path_ignored
;
1232 if (exclude
== !(dir
->flags
& DIR_SHOW_IGNORED
))
1233 return path_ignored
;
1236 return path_handled
;
1239 static enum path_treatment
treat_path(struct dir_struct
*dir
,
1241 struct strbuf
*path
,
1243 const struct path_simplify
*simplify
)
1247 if (is_dot_or_dotdot(de
->d_name
) || !strcmp(de
->d_name
, ".git"))
1248 return path_ignored
;
1249 strbuf_setlen(path
, baselen
);
1250 strbuf_addstr(path
, de
->d_name
);
1251 if (simplify_away(path
->buf
, path
->len
, simplify
))
1252 return path_ignored
;
1255 return treat_one_path(dir
, path
, simplify
, dtype
, de
);
1259 * Read a directory tree. We currently ignore anything but
1260 * directories, regular files and symlinks. That's because git
1261 * doesn't handle them at all yet. Maybe that will change some
1264 * Also, we ignore the name ".git" (even if it is not a directory).
1265 * That likely will not change.
1267 static int read_directory_recursive(struct dir_struct
*dir
,
1268 const char *base
, int baselen
,
1270 const struct path_simplify
*simplify
)
1275 struct strbuf path
= STRBUF_INIT
;
1277 strbuf_add(&path
, base
, baselen
);
1279 fdir
= opendir(path
.len
? path
.buf
: ".");
1283 while ((de
= readdir(fdir
)) != NULL
) {
1284 switch (treat_path(dir
, de
, &path
, baselen
, simplify
)) {
1286 contents
+= read_directory_recursive(dir
, path
.buf
,
1287 path
.len
, check_only
, simplify
);
1297 dir_add_name(dir
, path
.buf
, path
.len
);
1301 strbuf_release(&path
);
1306 static int cmp_name(const void *p1
, const void *p2
)
1308 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
1309 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
1311 return cache_name_compare(e1
->name
, e1
->len
,
1315 static struct path_simplify
*create_simplify(const char **pathspec
)
1318 struct path_simplify
*simplify
= NULL
;
1323 for (nr
= 0 ; ; nr
++) {
1326 alloc
= alloc_nr(alloc
);
1327 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
1329 match
= *pathspec
++;
1332 simplify
[nr
].path
= match
;
1333 simplify
[nr
].len
= simple_length(match
);
1335 simplify
[nr
].path
= NULL
;
1336 simplify
[nr
].len
= 0;
1340 static void free_simplify(struct path_simplify
*simplify
)
1345 static int treat_leading_path(struct dir_struct
*dir
,
1346 const char *path
, int len
,
1347 const struct path_simplify
*simplify
)
1349 struct strbuf sb
= STRBUF_INIT
;
1350 int baselen
, rc
= 0;
1353 while (len
&& path
[len
- 1] == '/')
1359 cp
= path
+ baselen
+ !!baselen
;
1360 cp
= memchr(cp
, '/', path
+ len
- cp
);
1364 baselen
= cp
- path
;
1365 strbuf_setlen(&sb
, 0);
1366 strbuf_add(&sb
, path
, baselen
);
1367 if (!is_directory(sb
.buf
))
1369 if (simplify_away(sb
.buf
, sb
.len
, simplify
))
1371 if (treat_one_path(dir
, &sb
, simplify
,
1372 DT_DIR
, NULL
) == path_ignored
)
1373 break; /* do not recurse into it */
1374 if (len
<= baselen
) {
1376 break; /* finished checking */
1379 strbuf_release(&sb
);
1383 int read_directory(struct dir_struct
*dir
, const char *path
, int len
, const char **pathspec
)
1385 struct path_simplify
*simplify
;
1387 if (has_symlink_leading_path(path
, len
))
1390 simplify
= create_simplify(pathspec
);
1391 if (!len
|| treat_leading_path(dir
, path
, len
, simplify
))
1392 read_directory_recursive(dir
, path
, len
, 0, simplify
);
1393 free_simplify(simplify
);
1394 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
1395 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
1399 int file_exists(const char *f
)
1402 return lstat(f
, &sb
) == 0;
1406 * Given two normalized paths (a trailing slash is ok), if subdir is
1407 * outside dir, return -1. Otherwise return the offset in subdir that
1408 * can be used as relative path to dir.
1410 int dir_inside_of(const char *subdir
, const char *dir
)
1414 assert(dir
&& subdir
&& *dir
&& *subdir
);
1416 while (*dir
&& *subdir
&& *dir
== *subdir
) {
1422 /* hel[p]/me vs hel[l]/yeah */
1423 if (*dir
&& *subdir
)
1427 return !*dir
? offset
: -1; /* same dir */
1429 /* foo/[b]ar vs foo/[] */
1430 if (is_dir_sep(dir
[-1]))
1431 return is_dir_sep(subdir
[-1]) ? offset
: -1;
1433 /* foo[/]bar vs foo[] */
1434 return is_dir_sep(*subdir
) ? offset
+ 1 : -1;
1437 int is_inside_dir(const char *dir
)
1442 if (!getcwd(cwd
, sizeof(cwd
)))
1443 die_errno("can't find the current directory");
1444 return dir_inside_of(cwd
, dir
) >= 0;
1447 int is_empty_dir(const char *path
)
1449 DIR *dir
= opendir(path
);
1456 while ((e
= readdir(dir
)) != NULL
)
1457 if (!is_dot_or_dotdot(e
->d_name
)) {
1466 static int remove_dir_recurse(struct strbuf
*path
, int flag
, int *kept_up
)
1470 int ret
= 0, original_len
= path
->len
, len
, kept_down
= 0;
1471 int only_empty
= (flag
& REMOVE_DIR_EMPTY_ONLY
);
1472 int keep_toplevel
= (flag
& REMOVE_DIR_KEEP_TOPLEVEL
);
1473 unsigned char submodule_head
[20];
1475 if ((flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
1476 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
1477 /* Do not descend and nuke a nested git work tree. */
1483 flag
&= ~REMOVE_DIR_KEEP_TOPLEVEL
;
1484 dir
= opendir(path
->buf
);
1486 /* an empty dir could be removed even if it is unreadble */
1488 return rmdir(path
->buf
);
1492 if (path
->buf
[original_len
- 1] != '/')
1493 strbuf_addch(path
, '/');
1496 while ((e
= readdir(dir
)) != NULL
) {
1498 if (is_dot_or_dotdot(e
->d_name
))
1501 strbuf_setlen(path
, len
);
1502 strbuf_addstr(path
, e
->d_name
);
1503 if (lstat(path
->buf
, &st
))
1505 else if (S_ISDIR(st
.st_mode
)) {
1506 if (!remove_dir_recurse(path
, flag
, &kept_down
))
1507 continue; /* happy */
1508 } else if (!only_empty
&& !unlink(path
->buf
))
1509 continue; /* happy, too */
1511 /* path too long, stat fails, or non-directory still exists */
1517 strbuf_setlen(path
, original_len
);
1518 if (!ret
&& !keep_toplevel
&& !kept_down
)
1519 ret
= rmdir(path
->buf
);
1522 * report the uplevel that it is not an error that we
1523 * did not rmdir() our directory.
1529 int remove_dir_recursively(struct strbuf
*path
, int flag
)
1531 return remove_dir_recurse(path
, flag
, NULL
);
1534 void setup_standard_excludes(struct dir_struct
*dir
)
1539 dir
->exclude_per_dir
= ".gitignore";
1540 path
= git_path("info/exclude");
1541 if (!excludes_file
) {
1542 home_config_paths(NULL
, &xdg_path
, "ignore");
1543 excludes_file
= xdg_path
;
1545 if (!access_or_warn(path
, R_OK
))
1546 add_excludes_from_file(dir
, path
);
1547 if (excludes_file
&& !access_or_warn(excludes_file
, R_OK
))
1548 add_excludes_from_file(dir
, excludes_file
);
1551 int remove_path(const char *name
)
1555 if (unlink(name
) && errno
!= ENOENT
)
1558 slash
= strrchr(name
, '/');
1560 char *dirs
= xstrdup(name
);
1561 slash
= dirs
+ (slash
- name
);
1564 } while (rmdir(dirs
) == 0 && (slash
= strrchr(dirs
, '/')));
1570 static int pathspec_item_cmp(const void *a_
, const void *b_
)
1572 struct pathspec_item
*a
, *b
;
1574 a
= (struct pathspec_item
*)a_
;
1575 b
= (struct pathspec_item
*)b_
;
1576 return strcmp(a
->match
, b
->match
);
1579 int init_pathspec(struct pathspec
*pathspec
, const char **paths
)
1581 const char **p
= paths
;
1584 memset(pathspec
, 0, sizeof(*pathspec
));
1589 pathspec
->raw
= paths
;
1590 pathspec
->nr
= p
- paths
;
1594 pathspec
->items
= xmalloc(sizeof(struct pathspec_item
)*pathspec
->nr
);
1595 for (i
= 0; i
< pathspec
->nr
; i
++) {
1596 struct pathspec_item
*item
= pathspec
->items
+i
;
1597 const char *path
= paths
[i
];
1600 item
->len
= strlen(path
);
1602 if (limit_pathspec_to_literal()) {
1603 item
->nowildcard_len
= item
->len
;
1605 item
->nowildcard_len
= simple_length(path
);
1606 if (item
->nowildcard_len
< item
->len
) {
1607 pathspec
->has_wildcard
= 1;
1608 if (path
[item
->nowildcard_len
] == '*' &&
1609 no_wildcard(path
+ item
->nowildcard_len
+ 1))
1610 item
->flags
|= PATHSPEC_ONESTAR
;
1615 qsort(pathspec
->items
, pathspec
->nr
,
1616 sizeof(struct pathspec_item
), pathspec_item_cmp
);
1621 void free_pathspec(struct pathspec
*pathspec
)
1623 free(pathspec
->items
);
1624 pathspec
->items
= NULL
;
1627 int limit_pathspec_to_literal(void)
1629 static int flag
= -1;
1631 flag
= git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, 0);
1636 * Frees memory within dir which was allocated for exclude lists and
1637 * the exclude_stack. Does not free dir itself.
1639 void clear_directory(struct dir_struct
*dir
)
1642 struct exclude_list_group
*group
;
1643 struct exclude_list
*el
;
1644 struct exclude_stack
*stk
;
1646 for (i
= EXC_CMDL
; i
<= EXC_FILE
; i
++) {
1647 group
= &dir
->exclude_list_group
[i
];
1648 for (j
= 0; j
< group
->nr
; j
++) {
1651 free((char *)el
->src
);
1652 clear_exclude_list(el
);
1657 stk
= dir
->exclude_stack
;
1659 struct exclude_stack
*prev
= stk
->prev
;