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 void read_directory_recursive(struct dir_struct
*dir
,
21 const char *path
, int len
,
23 const struct path_simplify
*simplify
,
25 static int get_dtype(struct dirent
*de
, const char *path
, int len
);
27 /* helper string functions with support for the ignore_case flag */
28 int strcmp_icase(const char *a
, const char *b
)
30 return ignore_case
? strcasecmp(a
, b
) : strcmp(a
, b
);
33 int strncmp_icase(const char *a
, const char *b
, size_t count
)
35 return ignore_case
? strncasecmp(a
, b
, count
) : strncmp(a
, b
, count
);
38 int fnmatch_icase(const char *pattern
, const char *string
, int flags
)
40 return fnmatch(pattern
, string
, flags
| (ignore_case
? FNM_CASEFOLD
: 0));
43 inline int git_fnmatch(const char *pattern
, const char *string
,
44 int flags
, int prefix
)
47 if (flags
& GFNM_PATHNAME
)
48 fnm_flags
|= FNM_PATHNAME
;
50 if (strncmp(pattern
, string
, prefix
))
55 if (flags
& GFNM_ONESTAR
) {
56 int pattern_len
= strlen(++pattern
);
57 int string_len
= strlen(string
);
58 return string_len
< pattern_len
||
60 string
+ string_len
- pattern_len
);
62 return fnmatch(pattern
, string
, fnm_flags
);
65 static size_t common_prefix_len(const char **pathspec
)
67 const char *n
, *first
;
69 int literal
= limit_pathspec_to_literal();
75 while ((n
= *pathspec
++)) {
77 for (i
= 0; first
== n
|| i
< max
; i
++) {
79 if (!c
|| c
!= first
[i
] || (!literal
&& is_glob_special(c
)))
84 if (first
== n
|| len
< max
) {
94 * Returns a copy of the longest leading path common among all
97 char *common_prefix(const char **pathspec
)
99 unsigned long len
= common_prefix_len(pathspec
);
101 return len
? xmemdupz(*pathspec
, len
) : NULL
;
104 int fill_directory(struct dir_struct
*dir
, const char **pathspec
)
109 * Calculate common prefix for the pathspec, and
110 * use that to optimize the directory walk
112 len
= common_prefix_len(pathspec
);
114 /* Read the directory and prune it */
115 read_directory(dir
, pathspec
? *pathspec
: "", len
, pathspec
);
119 int within_depth(const char *name
, int namelen
,
120 int depth
, int max_depth
)
122 const char *cp
= name
, *cpe
= name
+ namelen
;
128 if (depth
> max_depth
)
135 * Does 'match' match the given name?
136 * A match is found if
138 * (1) the 'match' string is leading directory of 'name', or
139 * (2) the 'match' string is a wildcard and matches 'name', or
140 * (3) the 'match' string is exactly the same as 'name'.
142 * and the return value tells which case it was.
144 * It returns 0 when there is no match.
146 static int match_one(const char *match
, const char *name
, int namelen
)
149 int literal
= limit_pathspec_to_literal();
151 /* If the match was just the prefix, we matched */
153 return MATCHED_RECURSIVELY
;
157 unsigned char c1
= tolower(*match
);
158 unsigned char c2
= tolower(*name
);
159 if (c1
== '\0' || (!literal
&& is_glob_special(c1
)))
169 unsigned char c1
= *match
;
170 unsigned char c2
= *name
;
171 if (c1
== '\0' || (!literal
&& is_glob_special(c1
)))
182 * If we don't match the matchstring exactly,
183 * we need to match by fnmatch
185 matchlen
= strlen(match
);
186 if (strncmp_icase(match
, name
, matchlen
)) {
189 return !fnmatch_icase(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
192 if (namelen
== matchlen
)
193 return MATCHED_EXACTLY
;
194 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
195 return MATCHED_RECURSIVELY
;
200 * Given a name and a list of pathspecs, returns the nature of the
201 * closest (i.e. most specific) match of the name to any of the
204 * The caller typically calls this multiple times with the same
205 * pathspec and seen[] array but with different name/namelen
206 * (e.g. entries from the index) and is interested in seeing if and
207 * how each pathspec matches all the names it calls this function
208 * with. A mark is left in the seen[] array for each pathspec element
209 * indicating the closest type of match that element achieved, so if
210 * seen[n] remains zero after multiple invocations, that means the nth
211 * pathspec did not match any names, which could indicate that the
212 * user mistyped the nth pathspec.
214 int match_pathspec(const char **pathspec
, const char *name
, int namelen
,
215 int prefix
, char *seen
)
225 for (i
= 0; pathspec
[i
] != NULL
; i
++) {
227 const char *match
= pathspec
[i
] + prefix
;
228 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
230 how
= match_one(match
, name
, namelen
);
234 if (seen
&& seen
[i
] < how
)
242 * Does 'match' match the given name?
243 * A match is found if
245 * (1) the 'match' string is leading directory of 'name', or
246 * (2) the 'match' string is a wildcard and matches 'name', or
247 * (3) the 'match' string is exactly the same as 'name'.
249 * and the return value tells which case it was.
251 * It returns 0 when there is no match.
253 static int match_pathspec_item(const struct pathspec_item
*item
, int prefix
,
254 const char *name
, int namelen
)
256 /* name/namelen has prefix cut off by caller */
257 const char *match
= item
->match
+ prefix
;
258 int matchlen
= item
->len
- prefix
;
260 /* If the match was just the prefix, we matched */
262 return MATCHED_RECURSIVELY
;
264 if (matchlen
<= namelen
&& !strncmp(match
, name
, matchlen
)) {
265 if (matchlen
== namelen
)
266 return MATCHED_EXACTLY
;
268 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
269 return MATCHED_RECURSIVELY
;
272 if (item
->nowildcard_len
< item
->len
&&
273 !git_fnmatch(match
, name
,
274 item
->flags
& PATHSPEC_ONESTAR
? GFNM_ONESTAR
: 0,
275 item
->nowildcard_len
- prefix
))
276 return MATCHED_FNMATCH
;
282 * Given a name and a list of pathspecs, returns the nature of the
283 * closest (i.e. most specific) match of the name to any of the
286 * The caller typically calls this multiple times with the same
287 * pathspec and seen[] array but with different name/namelen
288 * (e.g. entries from the index) and is interested in seeing if and
289 * how each pathspec matches all the names it calls this function
290 * with. A mark is left in the seen[] array for each pathspec element
291 * indicating the closest type of match that element achieved, so if
292 * seen[n] remains zero after multiple invocations, that means the nth
293 * pathspec did not match any names, which could indicate that the
294 * user mistyped the nth pathspec.
296 int match_pathspec_depth(const struct pathspec
*ps
,
297 const char *name
, int namelen
,
298 int prefix
, char *seen
)
303 if (!ps
->recursive
|| ps
->max_depth
== -1)
304 return MATCHED_RECURSIVELY
;
306 if (within_depth(name
, namelen
, 0, ps
->max_depth
))
307 return MATCHED_EXACTLY
;
315 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
317 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
319 how
= match_pathspec_item(ps
->items
+i
, prefix
, name
, namelen
);
320 if (ps
->recursive
&& ps
->max_depth
!= -1 &&
321 how
&& how
!= MATCHED_FNMATCH
) {
322 int len
= ps
->items
[i
].len
;
323 if (name
[len
] == '/')
325 if (within_depth(name
+len
, namelen
-len
, 0, ps
->max_depth
))
326 how
= MATCHED_EXACTLY
;
333 if (seen
&& seen
[i
] < how
)
341 * Return the length of the "simple" part of a path match limiter.
343 static int simple_length(const char *match
)
348 unsigned char c
= *match
++;
350 if (c
== '\0' || is_glob_special(c
))
355 static int no_wildcard(const char *string
)
357 return string
[simple_length(string
)] == '\0';
360 void parse_exclude_pattern(const char **pattern
,
365 const char *p
= *pattern
;
370 *flags
|= EXC_FLAG_NEGATIVE
;
374 if (len
&& p
[len
- 1] == '/') {
376 *flags
|= EXC_FLAG_MUSTBEDIR
;
378 for (i
= 0; i
< len
; i
++) {
383 *flags
|= EXC_FLAG_NODIR
;
384 *nowildcardlen
= simple_length(p
);
386 * we should have excluded the trailing slash from 'p' too,
387 * but that's one more allocation. Instead just make sure
388 * nowildcardlen does not exceed real patternlen
390 if (*nowildcardlen
> len
)
391 *nowildcardlen
= len
;
392 if (*p
== '*' && no_wildcard(p
+ 1))
393 *flags
|= EXC_FLAG_ENDSWITH
;
398 void add_exclude(const char *string
, const char *base
,
399 int baselen
, struct exclude_list
*el
, int srcpos
)
406 parse_exclude_pattern(&string
, &patternlen
, &flags
, &nowildcardlen
);
407 if (flags
& EXC_FLAG_MUSTBEDIR
) {
409 x
= xmalloc(sizeof(*x
) + patternlen
+ 1);
411 memcpy(s
, string
, patternlen
);
412 s
[patternlen
] = '\0';
415 x
= xmalloc(sizeof(*x
));
418 x
->patternlen
= patternlen
;
419 x
->nowildcardlen
= nowildcardlen
;
421 x
->baselen
= baselen
;
424 ALLOC_GROW(el
->excludes
, el
->nr
+ 1, el
->alloc
);
425 el
->excludes
[el
->nr
++] = x
;
429 static void *read_skip_worktree_file_from_index(const char *path
, size_t *size
)
433 enum object_type type
;
435 struct index_state
*istate
= &the_index
;
438 pos
= index_name_pos(istate
, path
, len
);
441 if (!ce_skip_worktree(istate
->cache
[pos
]))
443 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
444 if (!data
|| type
!= OBJ_BLOB
) {
453 * Frees memory within el which was allocated for exclude patterns and
454 * the file buffer. Does not free el itself.
456 void clear_exclude_list(struct exclude_list
*el
)
460 for (i
= 0; i
< el
->nr
; i
++)
461 free(el
->excludes
[i
]);
470 int add_excludes_from_file_to_list(const char *fname
,
473 struct exclude_list
*el
,
477 int fd
, i
, lineno
= 1;
481 fd
= open(fname
, O_RDONLY
);
482 if (fd
< 0 || fstat(fd
, &st
) < 0) {
484 warn_on_inaccessible(fname
);
488 (buf
= read_skip_worktree_file_from_index(fname
, &size
)) == NULL
)
494 if (buf
[size
-1] != '\n') {
495 buf
= xrealloc(buf
, size
+1);
500 size
= xsize_t(st
.st_size
);
505 buf
= xmalloc(size
+1);
506 if (read_in_full(fd
, buf
, size
) != size
) {
517 for (i
= 0; i
< size
; i
++) {
518 if (buf
[i
] == '\n') {
519 if (entry
!= buf
+ i
&& entry
[0] != '#') {
520 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
521 add_exclude(entry
, base
, baselen
, el
, lineno
);
530 struct exclude_list
*add_exclude_list(struct dir_struct
*dir
,
531 int group_type
, const char *src
)
533 struct exclude_list
*el
;
534 struct exclude_list_group
*group
;
536 group
= &dir
->exclude_list_group
[group_type
];
537 ALLOC_GROW(group
->el
, group
->nr
+ 1, group
->alloc
);
538 el
= &group
->el
[group
->nr
++];
539 memset(el
, 0, sizeof(*el
));
545 * Used to set up core.excludesfile and .git/info/exclude lists.
547 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
549 struct exclude_list
*el
;
550 el
= add_exclude_list(dir
, EXC_FILE
, fname
);
551 if (add_excludes_from_file_to_list(fname
, "", 0, el
, 0) < 0)
552 die("cannot use %s as an exclude file", fname
);
556 * Loads the per-directory exclude list for the substring of base
557 * which has a char length of baselen.
559 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
561 struct exclude_list_group
*group
;
562 struct exclude_list
*el
;
563 struct exclude_stack
*stk
= NULL
;
566 if ((!dir
->exclude_per_dir
) ||
567 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
568 return; /* too long a path -- ignore */
570 group
= &dir
->exclude_list_group
[EXC_DIRS
];
572 /* Pop the exclude lists from the EXCL_DIRS exclude_list_group
573 * which originate from directories not in the prefix of the
574 * path being checked. */
575 while ((stk
= dir
->exclude_stack
) != NULL
) {
576 if (stk
->baselen
<= baselen
&&
577 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
579 el
= &group
->el
[dir
->exclude_stack
->exclude_ix
];
580 dir
->exclude_stack
= stk
->prev
;
581 free((char *)el
->src
); /* see strdup() below */
582 clear_exclude_list(el
);
587 /* Read from the parent directories and push them down. */
588 current
= stk
? stk
->baselen
: -1;
589 while (current
< baselen
) {
590 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
598 cp
= strchr(base
+ current
+ 1, '/');
600 die("oops in prep_exclude");
603 stk
->prev
= dir
->exclude_stack
;
604 stk
->baselen
= cp
- base
;
605 memcpy(dir
->basebuf
+ current
, base
+ current
,
606 stk
->baselen
- current
);
607 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
609 * dir->basebuf gets reused by the traversal, but we
610 * need fname to remain unchanged to ensure the src
611 * member of each struct exclude correctly
612 * back-references its source file. Other invocations
613 * of add_exclude_list provide stable strings, so we
614 * strdup() and free() here in the caller.
616 el
= add_exclude_list(dir
, EXC_DIRS
, strdup(dir
->basebuf
));
617 stk
->exclude_ix
= group
->nr
- 1;
618 add_excludes_from_file_to_list(dir
->basebuf
,
619 dir
->basebuf
, stk
->baselen
,
621 dir
->exclude_stack
= stk
;
622 current
= stk
->baselen
;
624 dir
->basebuf
[baselen
] = '\0';
627 int match_basename(const char *basename
, int basenamelen
,
628 const char *pattern
, int prefix
, int patternlen
,
631 if (prefix
== patternlen
) {
632 if (!strcmp_icase(pattern
, basename
))
634 } else if (flags
& EXC_FLAG_ENDSWITH
) {
635 if (patternlen
- 1 <= basenamelen
&&
636 !strcmp_icase(pattern
+ 1,
637 basename
+ basenamelen
- patternlen
+ 1))
640 if (fnmatch_icase(pattern
, basename
, 0) == 0)
646 int match_pathname(const char *pathname
, int pathlen
,
647 const char *base
, int baselen
,
648 const char *pattern
, int prefix
, int patternlen
,
655 * match with FNM_PATHNAME; the pattern has base implicitly
658 if (*pattern
== '/') {
664 * baselen does not count the trailing slash. base[] may or
665 * may not end with a trailing slash though.
667 if (pathlen
< baselen
+ 1 ||
668 (baselen
&& pathname
[baselen
] != '/') ||
669 strncmp_icase(pathname
, base
, baselen
))
672 namelen
= baselen
? pathlen
- baselen
- 1 : pathlen
;
673 name
= pathname
+ pathlen
- namelen
;
677 * if the non-wildcard part is longer than the
678 * remaining pathname, surely it cannot match.
680 if (prefix
> namelen
)
683 if (strncmp_icase(pattern
, name
, prefix
))
690 return wildmatch(pattern
, name
,
691 WM_PATHNAME
| (ignore_case
? WM_CASEFOLD
: 0),
696 * Scan the given exclude list in reverse to see whether pathname
697 * should be ignored. The first match (i.e. the last on the list), if
698 * any, determines the fate. Returns the exclude_list element which
699 * matched, or NULL for undecided.
701 static struct exclude
*last_exclude_matching_from_list(const char *pathname
,
703 const char *basename
,
705 struct exclude_list
*el
)
710 return NULL
; /* undefined */
712 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
713 struct exclude
*x
= el
->excludes
[i
];
714 const char *exclude
= x
->pattern
;
715 int prefix
= x
->nowildcardlen
;
717 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
718 if (*dtype
== DT_UNKNOWN
)
719 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
720 if (*dtype
!= DT_DIR
)
724 if (x
->flags
& EXC_FLAG_NODIR
) {
725 if (match_basename(basename
,
726 pathlen
- (basename
- pathname
),
727 exclude
, prefix
, x
->patternlen
,
733 assert(x
->baselen
== 0 || x
->base
[x
->baselen
- 1] == '/');
734 if (match_pathname(pathname
, pathlen
,
735 x
->base
, x
->baselen
? x
->baselen
- 1 : 0,
736 exclude
, prefix
, x
->patternlen
, x
->flags
))
739 return NULL
; /* undecided */
743 * Scan the list and let the last match determine the fate.
744 * Return 1 for exclude, 0 for include and -1 for undecided.
746 int is_excluded_from_list(const char *pathname
,
747 int pathlen
, const char *basename
, int *dtype
,
748 struct exclude_list
*el
)
750 struct exclude
*exclude
;
751 exclude
= last_exclude_matching_from_list(pathname
, pathlen
, basename
, dtype
, el
);
753 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
754 return -1; /* undecided */
758 * Loads the exclude lists for the directory containing pathname, then
759 * scans all exclude lists to determine whether pathname is excluded.
760 * Returns the exclude_list element which matched, or NULL for
763 static struct exclude
*last_exclude_matching(struct dir_struct
*dir
,
764 const char *pathname
,
767 int pathlen
= strlen(pathname
);
769 struct exclude_list_group
*group
;
770 struct exclude
*exclude
;
771 const char *basename
= strrchr(pathname
, '/');
772 basename
= (basename
) ? basename
+1 : pathname
;
774 prep_exclude(dir
, pathname
, basename
-pathname
);
776 for (i
= EXC_CMDL
; i
<= EXC_FILE
; i
++) {
777 group
= &dir
->exclude_list_group
[i
];
778 for (j
= group
->nr
- 1; j
>= 0; j
--) {
779 exclude
= last_exclude_matching_from_list(
780 pathname
, pathlen
, basename
, dtype_p
,
790 * Loads the exclude lists for the directory containing pathname, then
791 * scans all exclude lists to determine whether pathname is excluded.
792 * Returns 1 if true, otherwise 0.
794 static int is_excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
796 struct exclude
*exclude
=
797 last_exclude_matching(dir
, pathname
, dtype_p
);
799 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
803 void path_exclude_check_init(struct path_exclude_check
*check
,
804 struct dir_struct
*dir
)
807 check
->exclude
= NULL
;
808 strbuf_init(&check
->path
, 256);
811 void path_exclude_check_clear(struct path_exclude_check
*check
)
813 strbuf_release(&check
->path
);
817 * For each subdirectory in name, starting with the top-most, checks
818 * to see if that subdirectory is excluded, and if so, returns the
819 * corresponding exclude structure. Otherwise, checks whether name
820 * itself (which is presumably a file) is excluded.
822 * A path to a directory known to be excluded is left in check->path to
823 * optimize for repeated checks for files in the same excluded directory.
825 struct exclude
*last_exclude_matching_path(struct path_exclude_check
*check
,
826 const char *name
, int namelen
,
830 struct strbuf
*path
= &check
->path
;
831 struct exclude
*exclude
;
834 * we allow the caller to pass namelen as an optimization; it
835 * must match the length of the name, as we eventually call
836 * is_excluded() on the whole name string.
839 namelen
= strlen(name
);
842 * If path is non-empty, and name is equal to path or a
843 * subdirectory of path, name should be excluded, because
844 * it's inside a directory which is already known to be
845 * excluded and was previously left in check->path.
848 path
->len
<= namelen
&&
849 !memcmp(name
, path
->buf
, path
->len
) &&
850 (!name
[path
->len
] || name
[path
->len
] == '/'))
851 return check
->exclude
;
853 strbuf_setlen(path
, 0);
854 for (i
= 0; name
[i
]; i
++) {
859 exclude
= last_exclude_matching(check
->dir
,
862 check
->exclude
= exclude
;
866 strbuf_addch(path
, ch
);
869 /* An entry in the index; cannot be a directory with subentries */
870 strbuf_setlen(path
, 0);
872 return last_exclude_matching(check
->dir
, name
, dtype
);
876 * Is this name excluded? This is for a caller like show_files() that
877 * do not honor directory hierarchy and iterate through paths that are
878 * possibly in an ignored directory.
880 int is_path_excluded(struct path_exclude_check
*check
,
881 const char *name
, int namelen
, int *dtype
)
883 struct exclude
*exclude
=
884 last_exclude_matching_path(check
, name
, namelen
, dtype
);
886 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
890 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
892 struct dir_entry
*ent
;
894 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
896 memcpy(ent
->name
, pathname
, len
);
901 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
903 if (!(dir
->flags
& DIR_SHOW_IGNORED
) &&
904 cache_name_exists(pathname
, len
, ignore_case
))
907 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
908 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
911 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
913 if (!cache_name_is_other(pathname
, len
))
916 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
917 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
921 index_nonexistent
= 0,
927 * Do not use the alphabetically stored index to look up
928 * the directory name; instead, use the case insensitive
931 static enum exist_status
directory_exists_in_index_icase(const char *dirname
, int len
)
933 struct cache_entry
*ce
= index_name_exists(&the_index
, dirname
, len
+ 1, ignore_case
);
934 unsigned char endchar
;
937 return index_nonexistent
;
938 endchar
= ce
->name
[len
];
941 * The cache_entry structure returned will contain this dirname
942 * and possibly additional path components.
945 return index_directory
;
948 * If there are no additional path components, then this cache_entry
949 * represents a submodule. Submodules, despite being directories,
950 * are stored in the cache without a closing slash.
952 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
955 /* This should never be hit, but it exists just in case. */
956 return index_nonexistent
;
960 * The index sorts alphabetically by entry name, which
961 * means that a gitlink sorts as '\0' at the end, while
962 * a directory (which is defined not as an entry, but as
963 * the files it contains) will sort with the '/' at the
966 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
971 return directory_exists_in_index_icase(dirname
, len
);
973 pos
= cache_name_pos(dirname
, len
);
976 while (pos
< active_nr
) {
977 struct cache_entry
*ce
= active_cache
[pos
++];
978 unsigned char endchar
;
980 if (strncmp(ce
->name
, dirname
, len
))
982 endchar
= ce
->name
[len
];
986 return index_directory
;
987 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
990 return index_nonexistent
;
994 * When we find a directory when traversing the filesystem, we
995 * have three distinct cases:
998 * - see it as a directory
1001 * and which one we choose depends on a combination of existing
1002 * git index contents and the flags passed into the directory
1003 * traversal routine.
1005 * Case 1: If we *already* have entries in the index under that
1006 * directory name, we recurse into the directory to see all the files,
1007 * unless the directory is excluded and we want to show ignored
1010 * Case 2: If we *already* have that directory name as a gitlink,
1011 * we always continue to see it as a gitlink, regardless of whether
1012 * there is an actual git directory there or not (it might not
1013 * be checked out as a subproject!)
1015 * Case 3: if we didn't have it in the index previously, we
1016 * have a few sub-cases:
1018 * (a) if "show_other_directories" is true, we show it as
1019 * just a directory, unless "hide_empty_directories" is
1020 * also true and the directory is empty, in which case
1021 * we just ignore it entirely.
1022 * if we are looking for ignored directories, look if it
1023 * contains only ignored files to decide if it must be shown as
1025 * (b) if it looks like a git directory, and we don't have
1026 * 'no_gitlinks' set we treat it as a gitlink, and show it
1028 * (c) otherwise, we recurse into it.
1030 enum directory_treatment
{
1033 recurse_into_directory
1036 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
1037 const char *dirname
, int len
, int exclude
,
1038 const struct path_simplify
*simplify
)
1041 /* The "len-1" is to strip the final '/' */
1042 switch (directory_exists_in_index(dirname
, len
-1)) {
1043 case index_directory
:
1044 if ((dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
) && exclude
)
1047 return recurse_into_directory
;
1050 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
1051 return ignore_directory
;
1052 return show_directory
;
1054 case index_nonexistent
:
1055 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
1057 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
1058 unsigned char sha1
[20];
1059 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
1060 return show_directory
;
1062 return recurse_into_directory
;
1065 /* This is the "show_other_directories" case */
1068 * We are looking for ignored files and our directory is not ignored,
1069 * check if it contains only ignored files
1071 if ((dir
->flags
& DIR_SHOW_IGNORED
) && !exclude
) {
1072 dir
->flags
&= ~DIR_SHOW_IGNORED
;
1073 dir
->flags
|= DIR_HIDE_EMPTY_DIRECTORIES
;
1074 read_directory_recursive(dir
, dirname
, len
, 1, simplify
, &contents
);
1075 dir
->flags
&= ~DIR_HIDE_EMPTY_DIRECTORIES
;
1076 dir
->flags
|= DIR_SHOW_IGNORED
;
1078 return contents
? ignore_directory
: show_directory
;
1080 if (!(dir
->flags
& DIR_SHOW_IGNORED
) &&
1081 !(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
1082 return show_directory
;
1083 read_directory_recursive(dir
, dirname
, len
, 1, simplify
, &contents
);
1085 return ignore_directory
;
1086 return show_directory
;
1090 * Decide what to do when we find a file while traversing the
1091 * filesystem. Mostly two cases:
1093 * 1. We are looking for ignored files
1094 * (a) File is ignored, include it
1095 * (b) File is in ignored path, include it
1096 * (c) File is not ignored, exclude it
1098 * 2. Other scenarios, include the file if not excluded
1100 * Return 1 for exclude, 0 for include.
1102 static int treat_file(struct dir_struct
*dir
, struct strbuf
*path
, int exclude
, int *dtype
)
1104 struct path_exclude_check check
;
1105 int exclude_file
= 0;
1108 exclude_file
= !(dir
->flags
& DIR_SHOW_IGNORED
);
1109 else if (dir
->flags
& DIR_SHOW_IGNORED
) {
1110 /* Always exclude indexed files */
1111 struct cache_entry
*ce
= index_name_exists(&the_index
,
1112 path
->buf
, path
->len
, ignore_case
);
1117 path_exclude_check_init(&check
, dir
);
1119 if (!is_path_excluded(&check
, path
->buf
, path
->len
, dtype
))
1122 path_exclude_check_clear(&check
);
1125 return exclude_file
;
1129 * This is an inexact early pruning of any recursive directory
1130 * reading - if the path cannot possibly be in the pathspec,
1131 * return true, and we'll skip it early.
1133 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
1137 const char *match
= simplify
->path
;
1138 int len
= simplify
->len
;
1144 if (!memcmp(path
, match
, len
))
1154 * This function tells us whether an excluded path matches a
1155 * list of "interesting" pathspecs. That is, whether a path matched
1156 * by any of the pathspecs could possibly be ignored by excluding
1157 * the specified path. This can happen if:
1159 * 1. the path is mentioned explicitly in the pathspec
1161 * 2. the path is a directory prefix of some element in the
1164 static int exclude_matches_pathspec(const char *path
, int len
,
1165 const struct path_simplify
*simplify
)
1168 for (; simplify
->path
; simplify
++) {
1169 if (len
== simplify
->len
1170 && !memcmp(path
, simplify
->path
, len
))
1172 if (len
< simplify
->len
1173 && simplify
->path
[len
] == '/'
1174 && !memcmp(path
, simplify
->path
, len
))
1181 static int get_index_dtype(const char *path
, int len
)
1184 struct cache_entry
*ce
;
1186 ce
= cache_name_exists(path
, len
, 0);
1188 if (!ce_uptodate(ce
))
1190 if (S_ISGITLINK(ce
->ce_mode
))
1193 * Nobody actually cares about the
1194 * difference between DT_LNK and DT_REG
1199 /* Try to look it up as a directory */
1200 pos
= cache_name_pos(path
, len
);
1204 while (pos
< active_nr
) {
1205 ce
= active_cache
[pos
++];
1206 if (strncmp(ce
->name
, path
, len
))
1208 if (ce
->name
[len
] > '/')
1210 if (ce
->name
[len
] < '/')
1212 if (!ce_uptodate(ce
))
1213 break; /* continue? */
1219 static int get_dtype(struct dirent
*de
, const char *path
, int len
)
1221 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
1224 if (dtype
!= DT_UNKNOWN
)
1226 dtype
= get_index_dtype(path
, len
);
1227 if (dtype
!= DT_UNKNOWN
)
1229 if (lstat(path
, &st
))
1231 if (S_ISREG(st
.st_mode
))
1233 if (S_ISDIR(st
.st_mode
))
1235 if (S_ISLNK(st
.st_mode
))
1240 enum path_treatment
{
1246 static enum path_treatment
treat_one_path(struct dir_struct
*dir
,
1247 struct strbuf
*path
,
1248 const struct path_simplify
*simplify
,
1249 int dtype
, struct dirent
*de
,
1250 int exclude_shortcut_ok
)
1254 if (dtype
== DT_UNKNOWN
)
1255 dtype
= get_dtype(de
, path
->buf
, path
->len
);
1257 if (exclude_shortcut_ok
&&
1258 !(dir
->flags
& DIR_SHOW_IGNORED
) &&
1259 !(dir
->flags
& DIR_COLLECT_IGNORED
) &&
1261 cache_name_exists(path
->buf
, path
->len
, ignore_case
))
1262 return path_ignored
;
1264 exclude
= is_excluded(dir
, path
->buf
, &dtype
);
1266 if (exclude
&& (dir
->flags
& DIR_COLLECT_IGNORED
)
1267 && exclude_matches_pathspec(path
->buf
, path
->len
, simplify
))
1268 dir_add_ignored(dir
, path
->buf
, path
->len
);
1271 * Excluded? If we don't explicitly want to show
1272 * ignored files, ignore it
1274 if (exclude
&& !(dir
->flags
& DIR_SHOW_IGNORED
))
1275 return path_ignored
;
1279 return path_ignored
;
1281 strbuf_addch(path
, '/');
1283 switch (treat_directory(dir
, path
->buf
, path
->len
, exclude
, simplify
)) {
1284 case show_directory
:
1286 case recurse_into_directory
:
1287 return path_recurse
;
1288 case ignore_directory
:
1289 return path_ignored
;
1294 switch (treat_file(dir
, path
, exclude
, &dtype
)) {
1296 return path_ignored
;
1301 return path_handled
;
1304 static enum path_treatment
treat_path(struct dir_struct
*dir
,
1306 struct strbuf
*path
,
1308 const struct path_simplify
*simplify
,
1309 int exclude_shortcut_ok
)
1313 if (is_dot_or_dotdot(de
->d_name
) || !strcmp(de
->d_name
, ".git"))
1314 return path_ignored
;
1315 strbuf_setlen(path
, baselen
);
1316 strbuf_addstr(path
, de
->d_name
);
1317 if (simplify_away(path
->buf
, path
->len
, simplify
))
1318 return path_ignored
;
1321 return treat_one_path(dir
, path
, simplify
, dtype
, de
, exclude_shortcut_ok
);
1325 * Read a directory tree. We currently ignore anything but
1326 * directories, regular files and symlinks. That's because git
1327 * doesn't handle them at all yet. Maybe that will change some
1330 * Also, we ignore the name ".git" (even if it is not a directory).
1331 * That likely will not change.
1333 static void read_directory_recursive(struct dir_struct
*dir
,
1334 const char *base
, int baselen
,
1336 const struct path_simplify
*simplify
,
1341 struct strbuf path
= STRBUF_INIT
;
1343 strbuf_add(&path
, base
, baselen
);
1345 fdir
= opendir(path
.len
? path
.buf
: ".");
1349 while ((de
= readdir(fdir
)) != NULL
) {
1350 switch (treat_path(dir
, de
, &path
, baselen
,
1352 !check_only
&& !contents
)) {
1354 read_directory_recursive(dir
, path
.buf
,
1365 * Update the last argument to treat_path if anything
1366 * else is done after this point. This is because if
1367 * treat_path's exclude_shortcut_ok is true, it may
1368 * incorrectly return path_ignored (and never reaches
1369 * this part) instead of path_handled.
1375 dir_add_name(dir
, path
.buf
, path
.len
);
1379 strbuf_release(&path
);
1382 static int cmp_name(const void *p1
, const void *p2
)
1384 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
1385 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
1387 return cache_name_compare(e1
->name
, e1
->len
,
1391 static struct path_simplify
*create_simplify(const char **pathspec
)
1394 struct path_simplify
*simplify
= NULL
;
1399 for (nr
= 0 ; ; nr
++) {
1402 alloc
= alloc_nr(alloc
);
1403 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
1405 match
= *pathspec
++;
1408 simplify
[nr
].path
= match
;
1409 simplify
[nr
].len
= simple_length(match
);
1411 simplify
[nr
].path
= NULL
;
1412 simplify
[nr
].len
= 0;
1416 static void free_simplify(struct path_simplify
*simplify
)
1421 static int treat_leading_path(struct dir_struct
*dir
,
1422 const char *path
, int len
,
1423 const struct path_simplify
*simplify
)
1425 struct strbuf sb
= STRBUF_INIT
;
1426 int baselen
, rc
= 0;
1429 while (len
&& path
[len
- 1] == '/')
1435 cp
= path
+ baselen
+ !!baselen
;
1436 cp
= memchr(cp
, '/', path
+ len
- cp
);
1440 baselen
= cp
- path
;
1441 strbuf_setlen(&sb
, 0);
1442 strbuf_add(&sb
, path
, baselen
);
1443 if (!is_directory(sb
.buf
))
1445 if (simplify_away(sb
.buf
, sb
.len
, simplify
))
1447 if (treat_one_path(dir
, &sb
, simplify
,
1448 DT_DIR
, NULL
, 0) == path_ignored
)
1449 break; /* do not recurse into it */
1450 if (len
<= baselen
) {
1452 break; /* finished checking */
1455 strbuf_release(&sb
);
1459 int read_directory(struct dir_struct
*dir
, const char *path
, int len
, const char **pathspec
)
1461 struct path_simplify
*simplify
;
1463 if (has_symlink_leading_path(path
, len
))
1466 simplify
= create_simplify(pathspec
);
1467 if (!len
|| treat_leading_path(dir
, path
, len
, simplify
))
1468 read_directory_recursive(dir
, path
, len
, 0, simplify
, NULL
);
1469 free_simplify(simplify
);
1470 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
1471 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
1475 int file_exists(const char *f
)
1478 return lstat(f
, &sb
) == 0;
1482 * Given two normalized paths (a trailing slash is ok), if subdir is
1483 * outside dir, return -1. Otherwise return the offset in subdir that
1484 * can be used as relative path to dir.
1486 int dir_inside_of(const char *subdir
, const char *dir
)
1490 assert(dir
&& subdir
&& *dir
&& *subdir
);
1492 while (*dir
&& *subdir
&& *dir
== *subdir
) {
1498 /* hel[p]/me vs hel[l]/yeah */
1499 if (*dir
&& *subdir
)
1503 return !*dir
? offset
: -1; /* same dir */
1505 /* foo/[b]ar vs foo/[] */
1506 if (is_dir_sep(dir
[-1]))
1507 return is_dir_sep(subdir
[-1]) ? offset
: -1;
1509 /* foo[/]bar vs foo[] */
1510 return is_dir_sep(*subdir
) ? offset
+ 1 : -1;
1513 int is_inside_dir(const char *dir
)
1518 if (!getcwd(cwd
, sizeof(cwd
)))
1519 die_errno("can't find the current directory");
1520 return dir_inside_of(cwd
, dir
) >= 0;
1523 int is_empty_dir(const char *path
)
1525 DIR *dir
= opendir(path
);
1532 while ((e
= readdir(dir
)) != NULL
)
1533 if (!is_dot_or_dotdot(e
->d_name
)) {
1542 static int remove_dir_recurse(struct strbuf
*path
, int flag
, int *kept_up
)
1546 int ret
= 0, original_len
= path
->len
, len
, kept_down
= 0;
1547 int only_empty
= (flag
& REMOVE_DIR_EMPTY_ONLY
);
1548 int keep_toplevel
= (flag
& REMOVE_DIR_KEEP_TOPLEVEL
);
1549 unsigned char submodule_head
[20];
1551 if ((flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
1552 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
1553 /* Do not descend and nuke a nested git work tree. */
1559 flag
&= ~REMOVE_DIR_KEEP_TOPLEVEL
;
1560 dir
= opendir(path
->buf
);
1562 /* an empty dir could be removed even if it is unreadble */
1564 return rmdir(path
->buf
);
1568 if (path
->buf
[original_len
- 1] != '/')
1569 strbuf_addch(path
, '/');
1572 while ((e
= readdir(dir
)) != NULL
) {
1574 if (is_dot_or_dotdot(e
->d_name
))
1577 strbuf_setlen(path
, len
);
1578 strbuf_addstr(path
, e
->d_name
);
1579 if (lstat(path
->buf
, &st
))
1581 else if (S_ISDIR(st
.st_mode
)) {
1582 if (!remove_dir_recurse(path
, flag
, &kept_down
))
1583 continue; /* happy */
1584 } else if (!only_empty
&& !unlink(path
->buf
))
1585 continue; /* happy, too */
1587 /* path too long, stat fails, or non-directory still exists */
1593 strbuf_setlen(path
, original_len
);
1594 if (!ret
&& !keep_toplevel
&& !kept_down
)
1595 ret
= rmdir(path
->buf
);
1598 * report the uplevel that it is not an error that we
1599 * did not rmdir() our directory.
1605 int remove_dir_recursively(struct strbuf
*path
, int flag
)
1607 return remove_dir_recurse(path
, flag
, NULL
);
1610 void setup_standard_excludes(struct dir_struct
*dir
)
1615 dir
->exclude_per_dir
= ".gitignore";
1616 path
= git_path("info/exclude");
1617 if (!excludes_file
) {
1618 home_config_paths(NULL
, &xdg_path
, "ignore");
1619 excludes_file
= xdg_path
;
1621 if (!access_or_warn(path
, R_OK
))
1622 add_excludes_from_file(dir
, path
);
1623 if (excludes_file
&& !access_or_warn(excludes_file
, R_OK
))
1624 add_excludes_from_file(dir
, excludes_file
);
1627 int remove_path(const char *name
)
1631 if (unlink(name
) && errno
!= ENOENT
)
1634 slash
= strrchr(name
, '/');
1636 char *dirs
= xstrdup(name
);
1637 slash
= dirs
+ (slash
- name
);
1640 } while (rmdir(dirs
) == 0 && (slash
= strrchr(dirs
, '/')));
1646 static int pathspec_item_cmp(const void *a_
, const void *b_
)
1648 struct pathspec_item
*a
, *b
;
1650 a
= (struct pathspec_item
*)a_
;
1651 b
= (struct pathspec_item
*)b_
;
1652 return strcmp(a
->match
, b
->match
);
1655 int init_pathspec(struct pathspec
*pathspec
, const char **paths
)
1657 const char **p
= paths
;
1660 memset(pathspec
, 0, sizeof(*pathspec
));
1665 pathspec
->raw
= paths
;
1666 pathspec
->nr
= p
- paths
;
1670 pathspec
->items
= xmalloc(sizeof(struct pathspec_item
)*pathspec
->nr
);
1671 for (i
= 0; i
< pathspec
->nr
; i
++) {
1672 struct pathspec_item
*item
= pathspec
->items
+i
;
1673 const char *path
= paths
[i
];
1676 item
->len
= strlen(path
);
1678 if (limit_pathspec_to_literal()) {
1679 item
->nowildcard_len
= item
->len
;
1681 item
->nowildcard_len
= simple_length(path
);
1682 if (item
->nowildcard_len
< item
->len
) {
1683 pathspec
->has_wildcard
= 1;
1684 if (path
[item
->nowildcard_len
] == '*' &&
1685 no_wildcard(path
+ item
->nowildcard_len
+ 1))
1686 item
->flags
|= PATHSPEC_ONESTAR
;
1691 qsort(pathspec
->items
, pathspec
->nr
,
1692 sizeof(struct pathspec_item
), pathspec_item_cmp
);
1697 void free_pathspec(struct pathspec
*pathspec
)
1699 free(pathspec
->items
);
1700 pathspec
->items
= NULL
;
1703 int limit_pathspec_to_literal(void)
1705 static int flag
= -1;
1707 flag
= git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, 0);
1712 * Frees memory within dir which was allocated for exclude lists and
1713 * the exclude_stack. Does not free dir itself.
1715 void clear_directory(struct dir_struct
*dir
)
1718 struct exclude_list_group
*group
;
1719 struct exclude_list
*el
;
1720 struct exclude_stack
*stk
;
1722 for (i
= EXC_CMDL
; i
<= EXC_FILE
; i
++) {
1723 group
= &dir
->exclude_list_group
[i
];
1724 for (j
= 0; j
< group
->nr
; j
++) {
1727 free((char *)el
->src
);
1728 clear_exclude_list(el
);
1733 stk
= dir
->exclude_stack
;
1735 struct exclude_stack
*prev
= stk
->prev
;