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
14 struct path_simplify
{
19 static int read_directory_recursive(struct dir_struct
*dir
, const char *path
, int len
,
20 int check_only
, const struct path_simplify
*simplify
);
21 static int get_dtype(struct dirent
*de
, const char *path
, int len
);
23 /* helper string functions with support for the ignore_case flag */
24 int strcmp_icase(const char *a
, const char *b
)
26 return ignore_case
? strcasecmp(a
, b
) : strcmp(a
, b
);
29 int strncmp_icase(const char *a
, const char *b
, size_t count
)
31 return ignore_case
? strncasecmp(a
, b
, count
) : strncmp(a
, b
, count
);
34 int fnmatch_icase(const char *pattern
, const char *string
, int flags
)
36 return fnmatch(pattern
, string
, flags
| (ignore_case
? FNM_CASEFOLD
: 0));
39 static size_t common_prefix_len(const char **pathspec
)
41 const char *n
, *first
;
48 while ((n
= *pathspec
++)) {
50 for (i
= 0; first
== n
|| i
< max
; i
++) {
52 if (!c
|| c
!= first
[i
] || is_glob_special(c
))
57 if (first
== n
|| len
< max
) {
67 * Returns a copy of the longest leading path common among all
70 char *common_prefix(const char **pathspec
)
72 unsigned long len
= common_prefix_len(pathspec
);
74 return len
? xmemdupz(*pathspec
, len
) : NULL
;
77 int fill_directory(struct dir_struct
*dir
, const char **pathspec
)
82 * Calculate common prefix for the pathspec, and
83 * use that to optimize the directory walk
85 len
= common_prefix_len(pathspec
);
87 /* Read the directory and prune it */
88 read_directory(dir
, pathspec
? *pathspec
: "", len
, pathspec
);
92 int within_depth(const char *name
, int namelen
,
93 int depth
, int max_depth
)
95 const char *cp
= name
, *cpe
= name
+ namelen
;
101 if (depth
> max_depth
)
108 * Does 'match' match the given name?
109 * A match is found if
111 * (1) the 'match' string is leading directory of 'name', or
112 * (2) the 'match' string is a wildcard and matches 'name', or
113 * (3) the 'match' string is exactly the same as 'name'.
115 * and the return value tells which case it was.
117 * It returns 0 when there is no match.
119 static int match_one(const char *match
, const char *name
, int namelen
)
123 /* If the match was just the prefix, we matched */
125 return MATCHED_RECURSIVELY
;
129 unsigned char c1
= tolower(*match
);
130 unsigned char c2
= tolower(*name
);
131 if (c1
== '\0' || is_glob_special(c1
))
141 unsigned char c1
= *match
;
142 unsigned char c2
= *name
;
143 if (c1
== '\0' || is_glob_special(c1
))
155 * If we don't match the matchstring exactly,
156 * we need to match by fnmatch
158 matchlen
= strlen(match
);
159 if (strncmp_icase(match
, name
, matchlen
))
160 return !fnmatch_icase(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
162 if (namelen
== matchlen
)
163 return MATCHED_EXACTLY
;
164 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
165 return MATCHED_RECURSIVELY
;
170 * Given a name and a list of pathspecs, returns the nature of the
171 * closest (i.e. most specific) match of the name to any of the
174 * The caller typically calls this multiple times with the same
175 * pathspec and seen[] array but with different name/namelen
176 * (e.g. entries from the index) and is interested in seeing if and
177 * how each pathspec matches all the names it calls this function
178 * with. A mark is left in the seen[] array for each pathspec element
179 * indicating the closest type of match that element achieved, so if
180 * seen[n] remains zero after multiple invocations, that means the nth
181 * pathspec did not match any names, which could indicate that the
182 * user mistyped the nth pathspec.
184 int match_pathspec(const char **pathspec
, const char *name
, int namelen
,
185 int prefix
, char *seen
)
195 for (i
= 0; pathspec
[i
] != NULL
; i
++) {
197 const char *match
= pathspec
[i
] + prefix
;
198 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
200 how
= match_one(match
, name
, namelen
);
204 if (seen
&& seen
[i
] < how
)
212 * Does 'match' match the given name?
213 * A match is found if
215 * (1) the 'match' string is leading directory of 'name', or
216 * (2) the 'match' string is a wildcard and matches 'name', or
217 * (3) the 'match' string is exactly the same as 'name'.
219 * and the return value tells which case it was.
221 * It returns 0 when there is no match.
223 static int match_pathspec_item(const struct pathspec_item
*item
, int prefix
,
224 const char *name
, int namelen
)
226 /* name/namelen has prefix cut off by caller */
227 const char *match
= item
->match
+ prefix
;
228 int matchlen
= item
->len
- prefix
;
230 /* If the match was just the prefix, we matched */
232 return MATCHED_RECURSIVELY
;
234 if (matchlen
<= namelen
&& !strncmp(match
, name
, matchlen
)) {
235 if (matchlen
== namelen
)
236 return MATCHED_EXACTLY
;
238 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
239 return MATCHED_RECURSIVELY
;
242 if (item
->use_wildcard
&& !fnmatch(match
, name
, 0))
243 return MATCHED_FNMATCH
;
249 * Given a name and a list of pathspecs, returns the nature of the
250 * closest (i.e. most specific) match of the name to any of the
253 * The caller typically calls this multiple times with the same
254 * pathspec and seen[] array but with different name/namelen
255 * (e.g. entries from the index) and is interested in seeing if and
256 * how each pathspec matches all the names it calls this function
257 * with. A mark is left in the seen[] array for each pathspec element
258 * indicating the closest type of match that element achieved, so if
259 * seen[n] remains zero after multiple invocations, that means the nth
260 * pathspec did not match any names, which could indicate that the
261 * user mistyped the nth pathspec.
263 int match_pathspec_depth(const struct pathspec
*ps
,
264 const char *name
, int namelen
,
265 int prefix
, char *seen
)
270 if (!ps
->recursive
|| ps
->max_depth
== -1)
271 return MATCHED_RECURSIVELY
;
273 if (within_depth(name
, namelen
, 0, ps
->max_depth
))
274 return MATCHED_EXACTLY
;
282 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
284 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
286 how
= match_pathspec_item(ps
->items
+i
, prefix
, name
, namelen
);
287 if (ps
->recursive
&& ps
->max_depth
!= -1 &&
288 how
&& how
!= MATCHED_FNMATCH
) {
289 int len
= ps
->items
[i
].len
;
290 if (name
[len
] == '/')
292 if (within_depth(name
+len
, namelen
-len
, 0, ps
->max_depth
))
293 how
= MATCHED_EXACTLY
;
300 if (seen
&& seen
[i
] < how
)
308 * Return the length of the "simple" part of a path match limiter.
310 static int simple_length(const char *match
)
315 unsigned char c
= *match
++;
317 if (c
== '\0' || is_glob_special(c
))
322 static int no_wildcard(const char *string
)
324 return string
[simple_length(string
)] == '\0';
327 void parse_exclude_pattern(const char **pattern
,
332 const char *p
= *pattern
;
337 *flags
|= EXC_FLAG_NEGATIVE
;
341 if (len
&& p
[len
- 1] == '/') {
343 *flags
|= EXC_FLAG_MUSTBEDIR
;
345 for (i
= 0; i
< len
; i
++) {
350 *flags
|= EXC_FLAG_NODIR
;
351 *nowildcardlen
= simple_length(p
);
353 * we should have excluded the trailing slash from 'p' too,
354 * but that's one more allocation. Instead just make sure
355 * nowildcardlen does not exceed real patternlen
357 if (*nowildcardlen
> len
)
358 *nowildcardlen
= len
;
359 if (*p
== '*' && no_wildcard(p
+ 1))
360 *flags
|= EXC_FLAG_ENDSWITH
;
365 void add_exclude(const char *string
, const char *base
,
366 int baselen
, struct exclude_list
*el
, int srcpos
)
373 parse_exclude_pattern(&string
, &patternlen
, &flags
, &nowildcardlen
);
374 if (flags
& EXC_FLAG_MUSTBEDIR
) {
376 x
= xmalloc(sizeof(*x
) + patternlen
+ 1);
378 memcpy(s
, string
, patternlen
);
379 s
[patternlen
] = '\0';
382 x
= xmalloc(sizeof(*x
));
385 x
->patternlen
= patternlen
;
386 x
->nowildcardlen
= nowildcardlen
;
388 x
->baselen
= baselen
;
391 ALLOC_GROW(el
->excludes
, el
->nr
+ 1, el
->alloc
);
392 el
->excludes
[el
->nr
++] = x
;
396 static void *read_skip_worktree_file_from_index(const char *path
, size_t *size
)
400 enum object_type type
;
402 struct index_state
*istate
= &the_index
;
405 pos
= index_name_pos(istate
, path
, len
);
408 if (!ce_skip_worktree(istate
->cache
[pos
]))
410 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
411 if (!data
|| type
!= OBJ_BLOB
) {
420 * Frees memory within el which was allocated for exclude patterns and
421 * the file buffer. Does not free el itself.
423 void clear_exclude_list(struct exclude_list
*el
)
427 for (i
= 0; i
< el
->nr
; i
++)
428 free(el
->excludes
[i
]);
437 int add_excludes_from_file_to_list(const char *fname
,
440 struct exclude_list
*el
,
444 int fd
, i
, lineno
= 1;
448 fd
= open(fname
, O_RDONLY
);
449 if (fd
< 0 || fstat(fd
, &st
) < 0) {
453 (buf
= read_skip_worktree_file_from_index(fname
, &size
)) == NULL
)
459 if (buf
[size
-1] != '\n') {
460 buf
= xrealloc(buf
, size
+1);
465 size
= xsize_t(st
.st_size
);
470 buf
= xmalloc(size
+1);
471 if (read_in_full(fd
, buf
, size
) != size
) {
482 for (i
= 0; i
< size
; i
++) {
483 if (buf
[i
] == '\n') {
484 if (entry
!= buf
+ i
&& entry
[0] != '#') {
485 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
486 add_exclude(entry
, base
, baselen
, el
, lineno
);
495 struct exclude_list
*add_exclude_list(struct dir_struct
*dir
,
496 int group_type
, const char *src
)
498 struct exclude_list
*el
;
499 struct exclude_list_group
*group
;
501 group
= &dir
->exclude_list_group
[group_type
];
502 ALLOC_GROW(group
->el
, group
->nr
+ 1, group
->alloc
);
503 el
= &group
->el
[group
->nr
++];
504 memset(el
, 0, sizeof(*el
));
510 * Used to set up core.excludesfile and .git/info/exclude lists.
512 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
514 struct exclude_list
*el
;
515 el
= add_exclude_list(dir
, EXC_FILE
, fname
);
516 if (add_excludes_from_file_to_list(fname
, "", 0, el
, 0) < 0)
517 die("cannot use %s as an exclude file", fname
);
521 * Loads the per-directory exclude list for the substring of base
522 * which has a char length of baselen.
524 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
526 struct exclude_list_group
*group
;
527 struct exclude_list
*el
;
528 struct exclude_stack
*stk
= NULL
;
531 if ((!dir
->exclude_per_dir
) ||
532 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
533 return; /* too long a path -- ignore */
535 group
= &dir
->exclude_list_group
[EXC_DIRS
];
537 /* Pop the exclude lists from the EXCL_DIRS exclude_list_group
538 * which originate from directories not in the prefix of the
539 * path being checked. */
540 while ((stk
= dir
->exclude_stack
) != NULL
) {
541 if (stk
->baselen
<= baselen
&&
542 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
544 el
= &group
->el
[dir
->exclude_stack
->exclude_ix
];
545 dir
->exclude_stack
= stk
->prev
;
546 free((char *)el
->src
); /* see strdup() below */
547 clear_exclude_list(el
);
552 /* Read from the parent directories and push them down. */
553 current
= stk
? stk
->baselen
: -1;
554 while (current
< baselen
) {
555 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
563 cp
= strchr(base
+ current
+ 1, '/');
565 die("oops in prep_exclude");
568 stk
->prev
= dir
->exclude_stack
;
569 stk
->baselen
= cp
- base
;
570 memcpy(dir
->basebuf
+ current
, base
+ current
,
571 stk
->baselen
- current
);
572 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
574 * dir->basebuf gets reused by the traversal, but we
575 * need fname to remain unchanged to ensure the src
576 * member of each struct exclude correctly
577 * back-references its source file. Other invocations
578 * of add_exclude_list provide stable strings, so we
579 * strdup() and free() here in the caller.
581 el
= add_exclude_list(dir
, EXC_DIRS
, strdup(dir
->basebuf
));
582 stk
->exclude_ix
= group
->nr
- 1;
583 add_excludes_from_file_to_list(dir
->basebuf
,
584 dir
->basebuf
, stk
->baselen
,
586 dir
->exclude_stack
= stk
;
587 current
= stk
->baselen
;
589 dir
->basebuf
[baselen
] = '\0';
592 int match_basename(const char *basename
, int basenamelen
,
593 const char *pattern
, int prefix
, int patternlen
,
596 if (prefix
== patternlen
) {
597 if (!strcmp_icase(pattern
, basename
))
599 } else if (flags
& EXC_FLAG_ENDSWITH
) {
600 if (patternlen
- 1 <= basenamelen
&&
601 !strcmp_icase(pattern
+ 1,
602 basename
+ basenamelen
- patternlen
+ 1))
605 if (fnmatch_icase(pattern
, basename
, 0) == 0)
611 int match_pathname(const char *pathname
, int pathlen
,
612 const char *base
, int baselen
,
613 const char *pattern
, int prefix
, int patternlen
,
620 * match with FNM_PATHNAME; the pattern has base implicitly
623 if (*pattern
== '/') {
629 * baselen does not count the trailing slash. base[] may or
630 * may not end with a trailing slash though.
632 if (pathlen
< baselen
+ 1 ||
633 (baselen
&& pathname
[baselen
] != '/') ||
634 strncmp_icase(pathname
, base
, baselen
))
637 namelen
= baselen
? pathlen
- baselen
- 1 : pathlen
;
638 name
= pathname
+ pathlen
- namelen
;
642 * if the non-wildcard part is longer than the
643 * remaining pathname, surely it cannot match.
645 if (prefix
> namelen
)
648 if (strncmp_icase(pattern
, name
, prefix
))
655 return fnmatch_icase(pattern
, name
, FNM_PATHNAME
) == 0;
659 * Scan the given exclude list in reverse to see whether pathname
660 * should be ignored. The first match (i.e. the last on the list), if
661 * any, determines the fate. Returns the exclude_list element which
662 * matched, or NULL for undecided.
664 static struct exclude
*last_exclude_matching_from_list(const char *pathname
,
666 const char *basename
,
668 struct exclude_list
*el
)
673 return NULL
; /* undefined */
675 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
676 struct exclude
*x
= el
->excludes
[i
];
677 const char *exclude
= x
->pattern
;
678 int prefix
= x
->nowildcardlen
;
680 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
681 if (*dtype
== DT_UNKNOWN
)
682 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
683 if (*dtype
!= DT_DIR
)
687 if (x
->flags
& EXC_FLAG_NODIR
) {
688 if (match_basename(basename
,
689 pathlen
- (basename
- pathname
),
690 exclude
, prefix
, x
->patternlen
,
696 assert(x
->baselen
== 0 || x
->base
[x
->baselen
- 1] == '/');
697 if (match_pathname(pathname
, pathlen
,
698 x
->base
, x
->baselen
? x
->baselen
- 1 : 0,
699 exclude
, prefix
, x
->patternlen
, x
->flags
))
702 return NULL
; /* undecided */
706 * Scan the list and let the last match determine the fate.
707 * Return 1 for exclude, 0 for include and -1 for undecided.
709 int is_excluded_from_list(const char *pathname
,
710 int pathlen
, const char *basename
, int *dtype
,
711 struct exclude_list
*el
)
713 struct exclude
*exclude
;
714 exclude
= last_exclude_matching_from_list(pathname
, pathlen
, basename
, dtype
, el
);
716 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
717 return -1; /* undecided */
721 * Loads the exclude lists for the directory containing pathname, then
722 * scans all exclude lists to determine whether pathname is excluded.
723 * Returns the exclude_list element which matched, or NULL for
726 static struct exclude
*last_exclude_matching(struct dir_struct
*dir
,
727 const char *pathname
,
730 int pathlen
= strlen(pathname
);
732 struct exclude_list_group
*group
;
733 struct exclude
*exclude
;
734 const char *basename
= strrchr(pathname
, '/');
735 basename
= (basename
) ? basename
+1 : pathname
;
737 prep_exclude(dir
, pathname
, basename
-pathname
);
739 for (i
= EXC_CMDL
; i
<= EXC_FILE
; i
++) {
740 group
= &dir
->exclude_list_group
[i
];
741 for (j
= group
->nr
- 1; j
>= 0; j
--) {
742 exclude
= last_exclude_matching_from_list(
743 pathname
, pathlen
, basename
, dtype_p
,
753 * Loads the exclude lists for the directory containing pathname, then
754 * scans all exclude lists to determine whether pathname is excluded.
755 * Returns 1 if true, otherwise 0.
757 static int is_excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
759 struct exclude
*exclude
=
760 last_exclude_matching(dir
, pathname
, dtype_p
);
762 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
766 void path_exclude_check_init(struct path_exclude_check
*check
,
767 struct dir_struct
*dir
)
770 check
->exclude
= NULL
;
771 strbuf_init(&check
->path
, 256);
774 void path_exclude_check_clear(struct path_exclude_check
*check
)
776 strbuf_release(&check
->path
);
780 * For each subdirectory in name, starting with the top-most, checks
781 * to see if that subdirectory is excluded, and if so, returns the
782 * corresponding exclude structure. Otherwise, checks whether name
783 * itself (which is presumably a file) is excluded.
785 * A path to a directory known to be excluded is left in check->path to
786 * optimize for repeated checks for files in the same excluded directory.
788 struct exclude
*last_exclude_matching_path(struct path_exclude_check
*check
,
789 const char *name
, int namelen
,
793 struct strbuf
*path
= &check
->path
;
794 struct exclude
*exclude
;
797 * we allow the caller to pass namelen as an optimization; it
798 * must match the length of the name, as we eventually call
799 * is_excluded() on the whole name string.
802 namelen
= strlen(name
);
805 * If path is non-empty, and name is equal to path or a
806 * subdirectory of path, name should be excluded, because
807 * it's inside a directory which is already known to be
808 * excluded and was previously left in check->path.
811 path
->len
<= namelen
&&
812 !memcmp(name
, path
->buf
, path
->len
) &&
813 (!name
[path
->len
] || name
[path
->len
] == '/'))
814 return check
->exclude
;
816 strbuf_setlen(path
, 0);
817 for (i
= 0; name
[i
]; i
++) {
822 exclude
= last_exclude_matching(check
->dir
,
825 check
->exclude
= exclude
;
829 strbuf_addch(path
, ch
);
832 /* An entry in the index; cannot be a directory with subentries */
833 strbuf_setlen(path
, 0);
835 return last_exclude_matching(check
->dir
, name
, dtype
);
839 * Is this name excluded? This is for a caller like show_files() that
840 * do not honor directory hierarchy and iterate through paths that are
841 * possibly in an ignored directory.
843 int is_path_excluded(struct path_exclude_check
*check
,
844 const char *name
, int namelen
, int *dtype
)
846 struct exclude
*exclude
=
847 last_exclude_matching_path(check
, name
, namelen
, dtype
);
849 return exclude
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
853 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
855 struct dir_entry
*ent
;
857 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
859 memcpy(ent
->name
, pathname
, len
);
864 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
866 if (cache_name_exists(pathname
, len
, ignore_case
))
869 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
870 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
873 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
875 if (!cache_name_is_other(pathname
, len
))
878 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
879 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
883 index_nonexistent
= 0,
889 * Do not use the alphabetically stored index to look up
890 * the directory name; instead, use the case insensitive
893 static enum exist_status
directory_exists_in_index_icase(const char *dirname
, int len
)
895 struct cache_entry
*ce
= index_name_exists(&the_index
, dirname
, len
+ 1, ignore_case
);
896 unsigned char endchar
;
899 return index_nonexistent
;
900 endchar
= ce
->name
[len
];
903 * The cache_entry structure returned will contain this dirname
904 * and possibly additional path components.
907 return index_directory
;
910 * If there are no additional path components, then this cache_entry
911 * represents a submodule. Submodules, despite being directories,
912 * are stored in the cache without a closing slash.
914 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
917 /* This should never be hit, but it exists just in case. */
918 return index_nonexistent
;
922 * The index sorts alphabetically by entry name, which
923 * means that a gitlink sorts as '\0' at the end, while
924 * a directory (which is defined not as an entry, but as
925 * the files it contains) will sort with the '/' at the
928 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
933 return directory_exists_in_index_icase(dirname
, len
);
935 pos
= cache_name_pos(dirname
, len
);
938 while (pos
< active_nr
) {
939 struct cache_entry
*ce
= active_cache
[pos
++];
940 unsigned char endchar
;
942 if (strncmp(ce
->name
, dirname
, len
))
944 endchar
= ce
->name
[len
];
948 return index_directory
;
949 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
952 return index_nonexistent
;
956 * When we find a directory when traversing the filesystem, we
957 * have three distinct cases:
960 * - see it as a directory
963 * and which one we choose depends on a combination of existing
964 * git index contents and the flags passed into the directory
967 * Case 1: If we *already* have entries in the index under that
968 * directory name, we always recurse into the directory to see
971 * Case 2: If we *already* have that directory name as a gitlink,
972 * we always continue to see it as a gitlink, regardless of whether
973 * there is an actual git directory there or not (it might not
974 * be checked out as a subproject!)
976 * Case 3: if we didn't have it in the index previously, we
977 * have a few sub-cases:
979 * (a) if "show_other_directories" is true, we show it as
980 * just a directory, unless "hide_empty_directories" is
981 * also true and the directory is empty, in which case
982 * we just ignore it entirely.
983 * (b) if it looks like a git directory, and we don't have
984 * 'no_gitlinks' set we treat it as a gitlink, and show it
986 * (c) otherwise, we recurse into it.
988 enum directory_treatment
{
991 recurse_into_directory
994 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
995 const char *dirname
, int len
,
996 const struct path_simplify
*simplify
)
998 /* The "len-1" is to strip the final '/' */
999 switch (directory_exists_in_index(dirname
, len
-1)) {
1000 case index_directory
:
1001 return recurse_into_directory
;
1004 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
1005 return ignore_directory
;
1006 return show_directory
;
1008 case index_nonexistent
:
1009 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
1011 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
1012 unsigned char sha1
[20];
1013 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
1014 return show_directory
;
1016 return recurse_into_directory
;
1019 /* This is the "show_other_directories" case */
1020 if (!(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
1021 return show_directory
;
1022 if (!read_directory_recursive(dir
, dirname
, len
, 1, simplify
))
1023 return ignore_directory
;
1024 return show_directory
;
1028 * This is an inexact early pruning of any recursive directory
1029 * reading - if the path cannot possibly be in the pathspec,
1030 * return true, and we'll skip it early.
1032 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
1036 const char *match
= simplify
->path
;
1037 int len
= simplify
->len
;
1043 if (!memcmp(path
, match
, len
))
1053 * This function tells us whether an excluded path matches a
1054 * list of "interesting" pathspecs. That is, whether a path matched
1055 * by any of the pathspecs could possibly be ignored by excluding
1056 * the specified path. This can happen if:
1058 * 1. the path is mentioned explicitly in the pathspec
1060 * 2. the path is a directory prefix of some element in the
1063 static int exclude_matches_pathspec(const char *path
, int len
,
1064 const struct path_simplify
*simplify
)
1067 for (; simplify
->path
; simplify
++) {
1068 if (len
== simplify
->len
1069 && !memcmp(path
, simplify
->path
, len
))
1071 if (len
< simplify
->len
1072 && simplify
->path
[len
] == '/'
1073 && !memcmp(path
, simplify
->path
, len
))
1080 static int get_index_dtype(const char *path
, int len
)
1083 struct cache_entry
*ce
;
1085 ce
= cache_name_exists(path
, len
, 0);
1087 if (!ce_uptodate(ce
))
1089 if (S_ISGITLINK(ce
->ce_mode
))
1092 * Nobody actually cares about the
1093 * difference between DT_LNK and DT_REG
1098 /* Try to look it up as a directory */
1099 pos
= cache_name_pos(path
, len
);
1103 while (pos
< active_nr
) {
1104 ce
= active_cache
[pos
++];
1105 if (strncmp(ce
->name
, path
, len
))
1107 if (ce
->name
[len
] > '/')
1109 if (ce
->name
[len
] < '/')
1111 if (!ce_uptodate(ce
))
1112 break; /* continue? */
1118 static int get_dtype(struct dirent
*de
, const char *path
, int len
)
1120 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
1123 if (dtype
!= DT_UNKNOWN
)
1125 dtype
= get_index_dtype(path
, len
);
1126 if (dtype
!= DT_UNKNOWN
)
1128 if (lstat(path
, &st
))
1130 if (S_ISREG(st
.st_mode
))
1132 if (S_ISDIR(st
.st_mode
))
1134 if (S_ISLNK(st
.st_mode
))
1139 enum path_treatment
{
1145 static enum path_treatment
treat_one_path(struct dir_struct
*dir
,
1146 struct strbuf
*path
,
1147 const struct path_simplify
*simplify
,
1148 int dtype
, struct dirent
*de
)
1150 int exclude
= is_excluded(dir
, path
->buf
, &dtype
);
1151 if (exclude
&& (dir
->flags
& DIR_COLLECT_IGNORED
)
1152 && exclude_matches_pathspec(path
->buf
, path
->len
, simplify
))
1153 dir_add_ignored(dir
, path
->buf
, path
->len
);
1156 * Excluded? If we don't explicitly want to show
1157 * ignored files, ignore it
1159 if (exclude
&& !(dir
->flags
& DIR_SHOW_IGNORED
))
1160 return path_ignored
;
1162 if (dtype
== DT_UNKNOWN
)
1163 dtype
= get_dtype(de
, path
->buf
, path
->len
);
1166 * Do we want to see just the ignored files?
1167 * We still need to recurse into directories,
1168 * even if we don't ignore them, since the
1169 * directory may contain files that we do..
1171 if (!exclude
&& (dir
->flags
& DIR_SHOW_IGNORED
)) {
1172 if (dtype
!= DT_DIR
)
1173 return path_ignored
;
1178 return path_ignored
;
1180 strbuf_addch(path
, '/');
1181 switch (treat_directory(dir
, path
->buf
, path
->len
, simplify
)) {
1182 case show_directory
:
1183 if (exclude
!= !!(dir
->flags
1184 & DIR_SHOW_IGNORED
))
1185 return path_ignored
;
1187 case recurse_into_directory
:
1188 return path_recurse
;
1189 case ignore_directory
:
1190 return path_ignored
;
1197 return path_handled
;
1200 static enum path_treatment
treat_path(struct dir_struct
*dir
,
1202 struct strbuf
*path
,
1204 const struct path_simplify
*simplify
)
1208 if (is_dot_or_dotdot(de
->d_name
) || !strcmp(de
->d_name
, ".git"))
1209 return path_ignored
;
1210 strbuf_setlen(path
, baselen
);
1211 strbuf_addstr(path
, de
->d_name
);
1212 if (simplify_away(path
->buf
, path
->len
, simplify
))
1213 return path_ignored
;
1216 return treat_one_path(dir
, path
, simplify
, dtype
, de
);
1220 * Read a directory tree. We currently ignore anything but
1221 * directories, regular files and symlinks. That's because git
1222 * doesn't handle them at all yet. Maybe that will change some
1225 * Also, we ignore the name ".git" (even if it is not a directory).
1226 * That likely will not change.
1228 static int read_directory_recursive(struct dir_struct
*dir
,
1229 const char *base
, int baselen
,
1231 const struct path_simplify
*simplify
)
1236 struct strbuf path
= STRBUF_INIT
;
1238 strbuf_add(&path
, base
, baselen
);
1240 fdir
= opendir(path
.len
? path
.buf
: ".");
1244 while ((de
= readdir(fdir
)) != NULL
) {
1245 switch (treat_path(dir
, de
, &path
, baselen
, simplify
)) {
1247 contents
+= read_directory_recursive(dir
, path
.buf
,
1259 dir_add_name(dir
, path
.buf
, path
.len
);
1263 strbuf_release(&path
);
1268 static int cmp_name(const void *p1
, const void *p2
)
1270 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
1271 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
1273 return cache_name_compare(e1
->name
, e1
->len
,
1277 static struct path_simplify
*create_simplify(const char **pathspec
)
1280 struct path_simplify
*simplify
= NULL
;
1285 for (nr
= 0 ; ; nr
++) {
1288 alloc
= alloc_nr(alloc
);
1289 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
1291 match
= *pathspec
++;
1294 simplify
[nr
].path
= match
;
1295 simplify
[nr
].len
= simple_length(match
);
1297 simplify
[nr
].path
= NULL
;
1298 simplify
[nr
].len
= 0;
1302 static void free_simplify(struct path_simplify
*simplify
)
1307 static int treat_leading_path(struct dir_struct
*dir
,
1308 const char *path
, int len
,
1309 const struct path_simplify
*simplify
)
1311 struct strbuf sb
= STRBUF_INIT
;
1312 int baselen
, rc
= 0;
1315 while (len
&& path
[len
- 1] == '/')
1321 cp
= path
+ baselen
+ !!baselen
;
1322 cp
= memchr(cp
, '/', path
+ len
- cp
);
1326 baselen
= cp
- path
;
1327 strbuf_setlen(&sb
, 0);
1328 strbuf_add(&sb
, path
, baselen
);
1329 if (!is_directory(sb
.buf
))
1331 if (simplify_away(sb
.buf
, sb
.len
, simplify
))
1333 if (treat_one_path(dir
, &sb
, simplify
,
1334 DT_DIR
, NULL
) == path_ignored
)
1335 break; /* do not recurse into it */
1336 if (len
<= baselen
) {
1338 break; /* finished checking */
1341 strbuf_release(&sb
);
1345 int read_directory(struct dir_struct
*dir
, const char *path
, int len
, const char **pathspec
)
1347 struct path_simplify
*simplify
;
1349 if (has_symlink_leading_path(path
, len
))
1352 simplify
= create_simplify(pathspec
);
1353 if (!len
|| treat_leading_path(dir
, path
, len
, simplify
))
1354 read_directory_recursive(dir
, path
, len
, 0, simplify
);
1355 free_simplify(simplify
);
1356 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
1357 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
1361 int file_exists(const char *f
)
1364 return lstat(f
, &sb
) == 0;
1368 * Given two normalized paths (a trailing slash is ok), if subdir is
1369 * outside dir, return -1. Otherwise return the offset in subdir that
1370 * can be used as relative path to dir.
1372 int dir_inside_of(const char *subdir
, const char *dir
)
1376 assert(dir
&& subdir
&& *dir
&& *subdir
);
1378 while (*dir
&& *subdir
&& *dir
== *subdir
) {
1384 /* hel[p]/me vs hel[l]/yeah */
1385 if (*dir
&& *subdir
)
1389 return !*dir
? offset
: -1; /* same dir */
1391 /* foo/[b]ar vs foo/[] */
1392 if (is_dir_sep(dir
[-1]))
1393 return is_dir_sep(subdir
[-1]) ? offset
: -1;
1395 /* foo[/]bar vs foo[] */
1396 return is_dir_sep(*subdir
) ? offset
+ 1 : -1;
1399 int is_inside_dir(const char *dir
)
1404 if (!getcwd(cwd
, sizeof(cwd
)))
1405 die_errno("can't find the current directory");
1406 return dir_inside_of(cwd
, dir
) >= 0;
1409 int is_empty_dir(const char *path
)
1411 DIR *dir
= opendir(path
);
1418 while ((e
= readdir(dir
)) != NULL
)
1419 if (!is_dot_or_dotdot(e
->d_name
)) {
1428 static int remove_dir_recurse(struct strbuf
*path
, int flag
, int *kept_up
)
1432 int ret
= 0, original_len
= path
->len
, len
, kept_down
= 0;
1433 int only_empty
= (flag
& REMOVE_DIR_EMPTY_ONLY
);
1434 int keep_toplevel
= (flag
& REMOVE_DIR_KEEP_TOPLEVEL
);
1435 unsigned char submodule_head
[20];
1437 if ((flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
1438 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
1439 /* Do not descend and nuke a nested git work tree. */
1445 flag
&= ~REMOVE_DIR_KEEP_TOPLEVEL
;
1446 dir
= opendir(path
->buf
);
1448 /* an empty dir could be removed even if it is unreadble */
1450 return rmdir(path
->buf
);
1454 if (path
->buf
[original_len
- 1] != '/')
1455 strbuf_addch(path
, '/');
1458 while ((e
= readdir(dir
)) != NULL
) {
1460 if (is_dot_or_dotdot(e
->d_name
))
1463 strbuf_setlen(path
, len
);
1464 strbuf_addstr(path
, e
->d_name
);
1465 if (lstat(path
->buf
, &st
))
1467 else if (S_ISDIR(st
.st_mode
)) {
1468 if (!remove_dir_recurse(path
, flag
, &kept_down
))
1469 continue; /* happy */
1470 } else if (!only_empty
&& !unlink(path
->buf
))
1471 continue; /* happy, too */
1473 /* path too long, stat fails, or non-directory still exists */
1479 strbuf_setlen(path
, original_len
);
1480 if (!ret
&& !keep_toplevel
&& !kept_down
)
1481 ret
= rmdir(path
->buf
);
1484 * report the uplevel that it is not an error that we
1485 * did not rmdir() our directory.
1491 int remove_dir_recursively(struct strbuf
*path
, int flag
)
1493 return remove_dir_recurse(path
, flag
, NULL
);
1496 void setup_standard_excludes(struct dir_struct
*dir
)
1500 dir
->exclude_per_dir
= ".gitignore";
1501 path
= git_path("info/exclude");
1502 if (!access(path
, R_OK
))
1503 add_excludes_from_file(dir
, path
);
1504 if (excludes_file
&& !access(excludes_file
, R_OK
))
1505 add_excludes_from_file(dir
, excludes_file
);
1508 int remove_path(const char *name
)
1512 if (unlink(name
) && errno
!= ENOENT
)
1515 slash
= strrchr(name
, '/');
1517 char *dirs
= xstrdup(name
);
1518 slash
= dirs
+ (slash
- name
);
1521 } while (rmdir(dirs
) == 0 && (slash
= strrchr(dirs
, '/')));
1527 static int pathspec_item_cmp(const void *a_
, const void *b_
)
1529 struct pathspec_item
*a
, *b
;
1531 a
= (struct pathspec_item
*)a_
;
1532 b
= (struct pathspec_item
*)b_
;
1533 return strcmp(a
->match
, b
->match
);
1536 int init_pathspec(struct pathspec
*pathspec
, const char **paths
)
1538 const char **p
= paths
;
1541 memset(pathspec
, 0, sizeof(*pathspec
));
1546 pathspec
->raw
= paths
;
1547 pathspec
->nr
= p
- paths
;
1551 pathspec
->items
= xmalloc(sizeof(struct pathspec_item
)*pathspec
->nr
);
1552 for (i
= 0; i
< pathspec
->nr
; i
++) {
1553 struct pathspec_item
*item
= pathspec
->items
+i
;
1554 const char *path
= paths
[i
];
1557 item
->len
= strlen(path
);
1558 item
->use_wildcard
= !no_wildcard(path
);
1559 if (item
->use_wildcard
)
1560 pathspec
->has_wildcard
= 1;
1563 qsort(pathspec
->items
, pathspec
->nr
,
1564 sizeof(struct pathspec_item
), pathspec_item_cmp
);
1569 void free_pathspec(struct pathspec
*pathspec
)
1571 free(pathspec
->items
);
1572 pathspec
->items
= NULL
;
1576 * Frees memory within dir which was allocated for exclude lists and
1577 * the exclude_stack. Does not free dir itself.
1579 void clear_directory(struct dir_struct
*dir
)
1582 struct exclude_list_group
*group
;
1583 struct exclude_list
*el
;
1584 struct exclude_stack
*stk
;
1586 for (i
= EXC_CMDL
; i
<= EXC_FILE
; i
++) {
1587 group
= &dir
->exclude_list_group
[i
];
1588 for (j
= 0; j
< group
->nr
; j
++) {
1591 free((char *)el
->src
);
1592 clear_exclude_list(el
);
1597 stk
= dir
->exclude_stack
;
1599 struct exclude_stack
*prev
= stk
->prev
;