2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
5 * Copyright (C) Linus Torvalds, 2005-2006
6 * Junio Hamano, 2005-2006
12 struct path_simplify
{
17 static int read_directory_recursive(struct dir_struct
*dir
, const char *path
, int len
,
18 int check_only
, const struct path_simplify
*simplify
);
19 static int get_dtype(struct dirent
*de
, const char *path
, int len
);
21 /* helper string functions with support for the ignore_case flag */
22 int strcmp_icase(const char *a
, const char *b
)
24 return ignore_case
? strcasecmp(a
, b
) : strcmp(a
, b
);
27 int strncmp_icase(const char *a
, const char *b
, size_t count
)
29 return ignore_case
? strncasecmp(a
, b
, count
) : strncmp(a
, b
, count
);
32 int fnmatch_icase(const char *pattern
, const char *string
, int flags
)
34 return fnmatch(pattern
, string
, flags
| (ignore_case
? FNM_CASEFOLD
: 0));
37 inline int git_fnmatch(const char *pattern
, const char *string
,
38 int flags
, int prefix
)
41 if (flags
& GFNM_PATHNAME
)
42 fnm_flags
|= FNM_PATHNAME
;
44 if (strncmp(pattern
, string
, prefix
))
49 return fnmatch(pattern
, string
, fnm_flags
);
52 static size_t common_prefix_len(const char **pathspec
)
54 const char *n
, *first
;
61 while ((n
= *pathspec
++)) {
63 for (i
= 0; first
== n
|| i
< max
; i
++) {
65 if (!c
|| c
!= first
[i
] || is_glob_special(c
))
70 if (first
== n
|| len
< max
) {
80 * Returns a copy of the longest leading path common among all
83 char *common_prefix(const char **pathspec
)
85 unsigned long len
= common_prefix_len(pathspec
);
87 return len
? xmemdupz(*pathspec
, len
) : NULL
;
90 int fill_directory(struct dir_struct
*dir
, const char **pathspec
)
95 * Calculate common prefix for the pathspec, and
96 * use that to optimize the directory walk
98 len
= common_prefix_len(pathspec
);
100 /* Read the directory and prune it */
101 read_directory(dir
, pathspec
? *pathspec
: "", len
, pathspec
);
105 int within_depth(const char *name
, int namelen
,
106 int depth
, int max_depth
)
108 const char *cp
= name
, *cpe
= name
+ namelen
;
114 if (depth
> max_depth
)
121 * Does 'match' match the given name?
122 * A match is found if
124 * (1) the 'match' string is leading directory of 'name', or
125 * (2) the 'match' string is a wildcard and matches 'name', or
126 * (3) the 'match' string is exactly the same as 'name'.
128 * and the return value tells which case it was.
130 * It returns 0 when there is no match.
132 static int match_one(const char *match
, const char *name
, int namelen
)
136 /* If the match was just the prefix, we matched */
138 return MATCHED_RECURSIVELY
;
142 unsigned char c1
= tolower(*match
);
143 unsigned char c2
= tolower(*name
);
144 if (c1
== '\0' || is_glob_special(c1
))
154 unsigned char c1
= *match
;
155 unsigned char c2
= *name
;
156 if (c1
== '\0' || is_glob_special(c1
))
168 * If we don't match the matchstring exactly,
169 * we need to match by fnmatch
171 matchlen
= strlen(match
);
172 if (strncmp_icase(match
, name
, matchlen
))
173 return !fnmatch_icase(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
175 if (namelen
== matchlen
)
176 return MATCHED_EXACTLY
;
177 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
178 return MATCHED_RECURSIVELY
;
183 * Given a name and a list of pathspecs, see if the name matches
184 * any of the pathspecs. The caller is also interested in seeing
185 * all pathspec matches some names it calls this function with
186 * (otherwise the user could have mistyped the unmatched pathspec),
187 * and a mark is left in seen[] array for pathspec element that
188 * actually matched anything.
190 int match_pathspec(const char **pathspec
, const char *name
, int namelen
,
191 int prefix
, char *seen
)
201 for (i
= 0; pathspec
[i
] != NULL
; i
++) {
203 const char *match
= pathspec
[i
] + prefix
;
204 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
206 how
= match_one(match
, name
, namelen
);
210 if (seen
&& seen
[i
] < how
)
218 * Does 'match' match the given name?
219 * A match is found if
221 * (1) the 'match' string is leading directory of 'name', or
222 * (2) the 'match' string is a wildcard and matches 'name', or
223 * (3) the 'match' string is exactly the same as 'name'.
225 * and the return value tells which case it was.
227 * It returns 0 when there is no match.
229 static int match_pathspec_item(const struct pathspec_item
*item
, int prefix
,
230 const char *name
, int namelen
)
232 /* name/namelen has prefix cut off by caller */
233 const char *match
= item
->match
+ prefix
;
234 int matchlen
= item
->len
- prefix
;
236 /* If the match was just the prefix, we matched */
238 return MATCHED_RECURSIVELY
;
240 if (matchlen
<= namelen
&& !strncmp(match
, name
, matchlen
)) {
241 if (matchlen
== namelen
)
242 return MATCHED_EXACTLY
;
244 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
245 return MATCHED_RECURSIVELY
;
248 if (item
->nowildcard_len
< item
->len
&&
249 !git_fnmatch(match
, name
, 0, item
->nowildcard_len
- prefix
))
250 return MATCHED_FNMATCH
;
256 * Given a name and a list of pathspecs, see if the name matches
257 * any of the pathspecs. The caller is also interested in seeing
258 * all pathspec matches some names it calls this function with
259 * (otherwise the user could have mistyped the unmatched pathspec),
260 * and a mark is left in seen[] array for pathspec element that
261 * actually matched anything.
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
*which
)
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
;
390 ALLOC_GROW(which
->excludes
, which
->nr
+ 1, which
->alloc
);
391 which
->excludes
[which
->nr
++] = x
;
394 static void *read_skip_worktree_file_from_index(const char *path
, size_t *size
)
398 enum object_type type
;
400 struct index_state
*istate
= &the_index
;
403 pos
= index_name_pos(istate
, path
, len
);
406 if (!ce_skip_worktree(istate
->cache
[pos
]))
408 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
409 if (!data
|| type
!= OBJ_BLOB
) {
417 void free_excludes(struct exclude_list
*el
)
421 for (i
= 0; i
< el
->nr
; i
++)
422 free(el
->excludes
[i
]);
429 int add_excludes_from_file_to_list(const char *fname
,
433 struct exclude_list
*which
,
441 fd
= open(fname
, O_RDONLY
);
442 if (fd
< 0 || fstat(fd
, &st
) < 0) {
444 warn_on_inaccessible(fname
);
448 (buf
= read_skip_worktree_file_from_index(fname
, &size
)) == NULL
)
454 if (buf
[size
-1] != '\n') {
455 buf
= xrealloc(buf
, size
+1);
460 size
= xsize_t(st
.st_size
);
465 buf
= xmalloc(size
+1);
466 if (read_in_full(fd
, buf
, size
) != size
) {
478 for (i
= 0; i
< size
; i
++) {
479 if (buf
[i
] == '\n') {
480 if (entry
!= buf
+ i
&& entry
[0] != '#') {
481 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
482 add_exclude(entry
, base
, baselen
, which
);
490 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
492 if (add_excludes_from_file_to_list(fname
, "", 0, NULL
,
493 &dir
->exclude_list
[EXC_FILE
], 0) < 0)
494 die("cannot use %s as an exclude file", fname
);
497 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
499 struct exclude_list
*el
;
500 struct exclude_stack
*stk
= NULL
;
503 if ((!dir
->exclude_per_dir
) ||
504 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
505 return; /* too long a path -- ignore */
507 /* Pop the ones that are not the prefix of the path being checked. */
508 el
= &dir
->exclude_list
[EXC_DIRS
];
509 while ((stk
= dir
->exclude_stack
) != NULL
) {
510 if (stk
->baselen
<= baselen
&&
511 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
513 dir
->exclude_stack
= stk
->prev
;
514 while (stk
->exclude_ix
< el
->nr
)
515 free(el
->excludes
[--el
->nr
]);
520 /* Read from the parent directories and push them down. */
521 current
= stk
? stk
->baselen
: -1;
522 while (current
< baselen
) {
523 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
531 cp
= strchr(base
+ current
+ 1, '/');
533 die("oops in prep_exclude");
536 stk
->prev
= dir
->exclude_stack
;
537 stk
->baselen
= cp
- base
;
538 stk
->exclude_ix
= el
->nr
;
539 memcpy(dir
->basebuf
+ current
, base
+ current
,
540 stk
->baselen
- current
);
541 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
542 add_excludes_from_file_to_list(dir
->basebuf
,
543 dir
->basebuf
, stk
->baselen
,
544 &stk
->filebuf
, el
, 1);
545 dir
->exclude_stack
= stk
;
546 current
= stk
->baselen
;
548 dir
->basebuf
[baselen
] = '\0';
551 int match_basename(const char *basename
, int basenamelen
,
552 const char *pattern
, int prefix
, int patternlen
,
555 if (prefix
== patternlen
) {
556 if (!strcmp_icase(pattern
, basename
))
558 } else if (flags
& EXC_FLAG_ENDSWITH
) {
559 if (patternlen
- 1 <= basenamelen
&&
560 !strcmp_icase(pattern
+ 1,
561 basename
+ basenamelen
- patternlen
+ 1))
564 if (fnmatch_icase(pattern
, basename
, 0) == 0)
570 int match_pathname(const char *pathname
, int pathlen
,
571 const char *base
, int baselen
,
572 const char *pattern
, int prefix
, int patternlen
,
579 * match with FNM_PATHNAME; the pattern has base implicitly
582 if (*pattern
== '/') {
588 * baselen does not count the trailing slash. base[] may or
589 * may not end with a trailing slash though.
591 if (pathlen
< baselen
+ 1 ||
592 (baselen
&& pathname
[baselen
] != '/') ||
593 strncmp_icase(pathname
, base
, baselen
))
596 namelen
= baselen
? pathlen
- baselen
- 1 : pathlen
;
597 name
= pathname
+ pathlen
- namelen
;
601 * if the non-wildcard part is longer than the
602 * remaining pathname, surely it cannot match.
604 if (prefix
> namelen
)
607 if (strncmp_icase(pattern
, name
, prefix
))
614 return fnmatch_icase(pattern
, name
, FNM_PATHNAME
) == 0;
617 /* Scan the list and let the last match determine the fate.
618 * Return 1 for exclude, 0 for include and -1 for undecided.
620 int excluded_from_list(const char *pathname
,
621 int pathlen
, const char *basename
, int *dtype
,
622 struct exclude_list
*el
)
627 return -1; /* undefined */
629 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
630 struct exclude
*x
= el
->excludes
[i
];
631 const char *exclude
= x
->pattern
;
632 int to_exclude
= x
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
633 int prefix
= x
->nowildcardlen
;
635 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
636 if (*dtype
== DT_UNKNOWN
)
637 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
638 if (*dtype
!= DT_DIR
)
642 if (x
->flags
& EXC_FLAG_NODIR
) {
643 if (match_basename(basename
,
644 pathlen
- (basename
- pathname
),
645 exclude
, prefix
, x
->patternlen
,
651 assert(x
->baselen
== 0 || x
->base
[x
->baselen
- 1] == '/');
652 if (match_pathname(pathname
, pathlen
,
653 x
->base
, x
->baselen
? x
->baselen
- 1 : 0,
654 exclude
, prefix
, x
->patternlen
, x
->flags
))
657 return -1; /* undecided */
660 static int excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
662 int pathlen
= strlen(pathname
);
664 const char *basename
= strrchr(pathname
, '/');
665 basename
= (basename
) ? basename
+1 : pathname
;
667 prep_exclude(dir
, pathname
, basename
-pathname
);
668 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
669 switch (excluded_from_list(pathname
, pathlen
, basename
,
670 dtype_p
, &dir
->exclude_list
[st
])) {
680 void path_exclude_check_init(struct path_exclude_check
*check
,
681 struct dir_struct
*dir
)
684 strbuf_init(&check
->path
, 256);
687 void path_exclude_check_clear(struct path_exclude_check
*check
)
689 strbuf_release(&check
->path
);
693 * Is this name excluded? This is for a caller like show_files() that
694 * do not honor directory hierarchy and iterate through paths that are
695 * possibly in an ignored directory.
697 * A path to a directory known to be excluded is left in check->path to
698 * optimize for repeated checks for files in the same excluded directory.
700 int path_excluded(struct path_exclude_check
*check
,
701 const char *name
, int namelen
, int *dtype
)
704 struct strbuf
*path
= &check
->path
;
707 * we allow the caller to pass namelen as an optimization; it
708 * must match the length of the name, as we eventually call
709 * excluded() on the whole name string.
712 namelen
= strlen(name
);
715 path
->len
<= namelen
&&
716 !memcmp(name
, path
->buf
, path
->len
) &&
717 (!name
[path
->len
] || name
[path
->len
] == '/'))
720 strbuf_setlen(path
, 0);
721 for (i
= 0; name
[i
]; i
++) {
726 if (excluded(check
->dir
, path
->buf
, &dt
))
729 strbuf_addch(path
, ch
);
732 /* An entry in the index; cannot be a directory with subentries */
733 strbuf_setlen(path
, 0);
735 return excluded(check
->dir
, name
, dtype
);
738 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
740 struct dir_entry
*ent
;
742 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
744 memcpy(ent
->name
, pathname
, len
);
749 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
751 if (cache_name_exists(pathname
, len
, ignore_case
))
754 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
755 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
758 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
760 if (!cache_name_is_other(pathname
, len
))
763 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
764 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
768 index_nonexistent
= 0,
774 * Do not use the alphabetically stored index to look up
775 * the directory name; instead, use the case insensitive
778 static enum exist_status
directory_exists_in_index_icase(const char *dirname
, int len
)
780 struct cache_entry
*ce
= index_name_exists(&the_index
, dirname
, len
+ 1, ignore_case
);
781 unsigned char endchar
;
784 return index_nonexistent
;
785 endchar
= ce
->name
[len
];
788 * The cache_entry structure returned will contain this dirname
789 * and possibly additional path components.
792 return index_directory
;
795 * If there are no additional path components, then this cache_entry
796 * represents a submodule. Submodules, despite being directories,
797 * are stored in the cache without a closing slash.
799 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
802 /* This should never be hit, but it exists just in case. */
803 return index_nonexistent
;
807 * The index sorts alphabetically by entry name, which
808 * means that a gitlink sorts as '\0' at the end, while
809 * a directory (which is defined not as an entry, but as
810 * the files it contains) will sort with the '/' at the
813 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
818 return directory_exists_in_index_icase(dirname
, len
);
820 pos
= cache_name_pos(dirname
, len
);
823 while (pos
< active_nr
) {
824 struct cache_entry
*ce
= active_cache
[pos
++];
825 unsigned char endchar
;
827 if (strncmp(ce
->name
, dirname
, len
))
829 endchar
= ce
->name
[len
];
833 return index_directory
;
834 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
837 return index_nonexistent
;
841 * When we find a directory when traversing the filesystem, we
842 * have three distinct cases:
845 * - see it as a directory
848 * and which one we choose depends on a combination of existing
849 * git index contents and the flags passed into the directory
852 * Case 1: If we *already* have entries in the index under that
853 * directory name, we always recurse into the directory to see
856 * Case 2: If we *already* have that directory name as a gitlink,
857 * we always continue to see it as a gitlink, regardless of whether
858 * there is an actual git directory there or not (it might not
859 * be checked out as a subproject!)
861 * Case 3: if we didn't have it in the index previously, we
862 * have a few sub-cases:
864 * (a) if "show_other_directories" is true, we show it as
865 * just a directory, unless "hide_empty_directories" is
866 * also true and the directory is empty, in which case
867 * we just ignore it entirely.
868 * (b) if it looks like a git directory, and we don't have
869 * 'no_gitlinks' set we treat it as a gitlink, and show it
871 * (c) otherwise, we recurse into it.
873 enum directory_treatment
{
876 recurse_into_directory
879 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
880 const char *dirname
, int len
,
881 const struct path_simplify
*simplify
)
883 /* The "len-1" is to strip the final '/' */
884 switch (directory_exists_in_index(dirname
, len
-1)) {
885 case index_directory
:
886 return recurse_into_directory
;
889 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
890 return ignore_directory
;
891 return show_directory
;
893 case index_nonexistent
:
894 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
896 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
897 unsigned char sha1
[20];
898 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
899 return show_directory
;
901 return recurse_into_directory
;
904 /* This is the "show_other_directories" case */
905 if (!(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
906 return show_directory
;
907 if (!read_directory_recursive(dir
, dirname
, len
, 1, simplify
))
908 return ignore_directory
;
909 return show_directory
;
913 * This is an inexact early pruning of any recursive directory
914 * reading - if the path cannot possibly be in the pathspec,
915 * return true, and we'll skip it early.
917 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
921 const char *match
= simplify
->path
;
922 int len
= simplify
->len
;
928 if (!memcmp(path
, match
, len
))
938 * This function tells us whether an excluded path matches a
939 * list of "interesting" pathspecs. That is, whether a path matched
940 * by any of the pathspecs could possibly be ignored by excluding
941 * the specified path. This can happen if:
943 * 1. the path is mentioned explicitly in the pathspec
945 * 2. the path is a directory prefix of some element in the
948 static int exclude_matches_pathspec(const char *path
, int len
,
949 const struct path_simplify
*simplify
)
952 for (; simplify
->path
; simplify
++) {
953 if (len
== simplify
->len
954 && !memcmp(path
, simplify
->path
, len
))
956 if (len
< simplify
->len
957 && simplify
->path
[len
] == '/'
958 && !memcmp(path
, simplify
->path
, len
))
965 static int get_index_dtype(const char *path
, int len
)
968 struct cache_entry
*ce
;
970 ce
= cache_name_exists(path
, len
, 0);
972 if (!ce_uptodate(ce
))
974 if (S_ISGITLINK(ce
->ce_mode
))
977 * Nobody actually cares about the
978 * difference between DT_LNK and DT_REG
983 /* Try to look it up as a directory */
984 pos
= cache_name_pos(path
, len
);
988 while (pos
< active_nr
) {
989 ce
= active_cache
[pos
++];
990 if (strncmp(ce
->name
, path
, len
))
992 if (ce
->name
[len
] > '/')
994 if (ce
->name
[len
] < '/')
996 if (!ce_uptodate(ce
))
997 break; /* continue? */
1003 static int get_dtype(struct dirent
*de
, const char *path
, int len
)
1005 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
1008 if (dtype
!= DT_UNKNOWN
)
1010 dtype
= get_index_dtype(path
, len
);
1011 if (dtype
!= DT_UNKNOWN
)
1013 if (lstat(path
, &st
))
1015 if (S_ISREG(st
.st_mode
))
1017 if (S_ISDIR(st
.st_mode
))
1019 if (S_ISLNK(st
.st_mode
))
1024 enum path_treatment
{
1030 static enum path_treatment
treat_one_path(struct dir_struct
*dir
,
1031 struct strbuf
*path
,
1032 const struct path_simplify
*simplify
,
1033 int dtype
, struct dirent
*de
)
1035 int exclude
= excluded(dir
, path
->buf
, &dtype
);
1036 if (exclude
&& (dir
->flags
& DIR_COLLECT_IGNORED
)
1037 && exclude_matches_pathspec(path
->buf
, path
->len
, simplify
))
1038 dir_add_ignored(dir
, path
->buf
, path
->len
);
1041 * Excluded? If we don't explicitly want to show
1042 * ignored files, ignore it
1044 if (exclude
&& !(dir
->flags
& DIR_SHOW_IGNORED
))
1045 return path_ignored
;
1047 if (dtype
== DT_UNKNOWN
)
1048 dtype
= get_dtype(de
, path
->buf
, path
->len
);
1051 * Do we want to see just the ignored files?
1052 * We still need to recurse into directories,
1053 * even if we don't ignore them, since the
1054 * directory may contain files that we do..
1056 if (!exclude
&& (dir
->flags
& DIR_SHOW_IGNORED
)) {
1057 if (dtype
!= DT_DIR
)
1058 return path_ignored
;
1063 return path_ignored
;
1065 strbuf_addch(path
, '/');
1066 switch (treat_directory(dir
, path
->buf
, path
->len
, simplify
)) {
1067 case show_directory
:
1068 if (exclude
!= !!(dir
->flags
1069 & DIR_SHOW_IGNORED
))
1070 return path_ignored
;
1072 case recurse_into_directory
:
1073 return path_recurse
;
1074 case ignore_directory
:
1075 return path_ignored
;
1082 return path_handled
;
1085 static enum path_treatment
treat_path(struct dir_struct
*dir
,
1087 struct strbuf
*path
,
1089 const struct path_simplify
*simplify
)
1093 if (is_dot_or_dotdot(de
->d_name
) || !strcmp(de
->d_name
, ".git"))
1094 return path_ignored
;
1095 strbuf_setlen(path
, baselen
);
1096 strbuf_addstr(path
, de
->d_name
);
1097 if (simplify_away(path
->buf
, path
->len
, simplify
))
1098 return path_ignored
;
1101 return treat_one_path(dir
, path
, simplify
, dtype
, de
);
1105 * Read a directory tree. We currently ignore anything but
1106 * directories, regular files and symlinks. That's because git
1107 * doesn't handle them at all yet. Maybe that will change some
1110 * Also, we ignore the name ".git" (even if it is not a directory).
1111 * That likely will not change.
1113 static int read_directory_recursive(struct dir_struct
*dir
,
1114 const char *base
, int baselen
,
1116 const struct path_simplify
*simplify
)
1121 struct strbuf path
= STRBUF_INIT
;
1123 strbuf_add(&path
, base
, baselen
);
1125 fdir
= opendir(path
.len
? path
.buf
: ".");
1129 while ((de
= readdir(fdir
)) != NULL
) {
1130 switch (treat_path(dir
, de
, &path
, baselen
, simplify
)) {
1132 contents
+= read_directory_recursive(dir
, path
.buf
,
1144 dir_add_name(dir
, path
.buf
, path
.len
);
1148 strbuf_release(&path
);
1153 static int cmp_name(const void *p1
, const void *p2
)
1155 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
1156 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
1158 return cache_name_compare(e1
->name
, e1
->len
,
1162 static struct path_simplify
*create_simplify(const char **pathspec
)
1165 struct path_simplify
*simplify
= NULL
;
1170 for (nr
= 0 ; ; nr
++) {
1173 alloc
= alloc_nr(alloc
);
1174 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
1176 match
= *pathspec
++;
1179 simplify
[nr
].path
= match
;
1180 simplify
[nr
].len
= simple_length(match
);
1182 simplify
[nr
].path
= NULL
;
1183 simplify
[nr
].len
= 0;
1187 static void free_simplify(struct path_simplify
*simplify
)
1192 static int treat_leading_path(struct dir_struct
*dir
,
1193 const char *path
, int len
,
1194 const struct path_simplify
*simplify
)
1196 struct strbuf sb
= STRBUF_INIT
;
1197 int baselen
, rc
= 0;
1200 while (len
&& path
[len
- 1] == '/')
1206 cp
= path
+ baselen
+ !!baselen
;
1207 cp
= memchr(cp
, '/', path
+ len
- cp
);
1211 baselen
= cp
- path
;
1212 strbuf_setlen(&sb
, 0);
1213 strbuf_add(&sb
, path
, baselen
);
1214 if (!is_directory(sb
.buf
))
1216 if (simplify_away(sb
.buf
, sb
.len
, simplify
))
1218 if (treat_one_path(dir
, &sb
, simplify
,
1219 DT_DIR
, NULL
) == path_ignored
)
1220 break; /* do not recurse into it */
1221 if (len
<= baselen
) {
1223 break; /* finished checking */
1226 strbuf_release(&sb
);
1230 int read_directory(struct dir_struct
*dir
, const char *path
, int len
, const char **pathspec
)
1232 struct path_simplify
*simplify
;
1234 if (has_symlink_leading_path(path
, len
))
1237 simplify
= create_simplify(pathspec
);
1238 if (!len
|| treat_leading_path(dir
, path
, len
, simplify
))
1239 read_directory_recursive(dir
, path
, len
, 0, simplify
);
1240 free_simplify(simplify
);
1241 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
1242 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
1246 int file_exists(const char *f
)
1249 return lstat(f
, &sb
) == 0;
1253 * Given two normalized paths (a trailing slash is ok), if subdir is
1254 * outside dir, return -1. Otherwise return the offset in subdir that
1255 * can be used as relative path to dir.
1257 int dir_inside_of(const char *subdir
, const char *dir
)
1261 assert(dir
&& subdir
&& *dir
&& *subdir
);
1263 while (*dir
&& *subdir
&& *dir
== *subdir
) {
1269 /* hel[p]/me vs hel[l]/yeah */
1270 if (*dir
&& *subdir
)
1274 return !*dir
? offset
: -1; /* same dir */
1276 /* foo/[b]ar vs foo/[] */
1277 if (is_dir_sep(dir
[-1]))
1278 return is_dir_sep(subdir
[-1]) ? offset
: -1;
1280 /* foo[/]bar vs foo[] */
1281 return is_dir_sep(*subdir
) ? offset
+ 1 : -1;
1284 int is_inside_dir(const char *dir
)
1289 if (!getcwd(cwd
, sizeof(cwd
)))
1290 die_errno("can't find the current directory");
1291 return dir_inside_of(cwd
, dir
) >= 0;
1294 int is_empty_dir(const char *path
)
1296 DIR *dir
= opendir(path
);
1303 while ((e
= readdir(dir
)) != NULL
)
1304 if (!is_dot_or_dotdot(e
->d_name
)) {
1313 static int remove_dir_recurse(struct strbuf
*path
, int flag
, int *kept_up
)
1317 int ret
= 0, original_len
= path
->len
, len
, kept_down
= 0;
1318 int only_empty
= (flag
& REMOVE_DIR_EMPTY_ONLY
);
1319 int keep_toplevel
= (flag
& REMOVE_DIR_KEEP_TOPLEVEL
);
1320 unsigned char submodule_head
[20];
1322 if ((flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
1323 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
1324 /* Do not descend and nuke a nested git work tree. */
1330 flag
&= ~REMOVE_DIR_KEEP_TOPLEVEL
;
1331 dir
= opendir(path
->buf
);
1333 /* an empty dir could be removed even if it is unreadble */
1335 return rmdir(path
->buf
);
1339 if (path
->buf
[original_len
- 1] != '/')
1340 strbuf_addch(path
, '/');
1343 while ((e
= readdir(dir
)) != NULL
) {
1345 if (is_dot_or_dotdot(e
->d_name
))
1348 strbuf_setlen(path
, len
);
1349 strbuf_addstr(path
, e
->d_name
);
1350 if (lstat(path
->buf
, &st
))
1352 else if (S_ISDIR(st
.st_mode
)) {
1353 if (!remove_dir_recurse(path
, flag
, &kept_down
))
1354 continue; /* happy */
1355 } else if (!only_empty
&& !unlink(path
->buf
))
1356 continue; /* happy, too */
1358 /* path too long, stat fails, or non-directory still exists */
1364 strbuf_setlen(path
, original_len
);
1365 if (!ret
&& !keep_toplevel
&& !kept_down
)
1366 ret
= rmdir(path
->buf
);
1369 * report the uplevel that it is not an error that we
1370 * did not rmdir() our directory.
1376 int remove_dir_recursively(struct strbuf
*path
, int flag
)
1378 return remove_dir_recurse(path
, flag
, NULL
);
1381 void setup_standard_excludes(struct dir_struct
*dir
)
1386 dir
->exclude_per_dir
= ".gitignore";
1387 path
= git_path("info/exclude");
1388 if (!excludes_file
) {
1389 home_config_paths(NULL
, &xdg_path
, "ignore");
1390 excludes_file
= xdg_path
;
1392 if (!access_or_warn(path
, R_OK
))
1393 add_excludes_from_file(dir
, path
);
1394 if (excludes_file
&& !access_or_warn(excludes_file
, R_OK
))
1395 add_excludes_from_file(dir
, excludes_file
);
1398 int remove_path(const char *name
)
1402 if (unlink(name
) && errno
!= ENOENT
)
1405 slash
= strrchr(name
, '/');
1407 char *dirs
= xstrdup(name
);
1408 slash
= dirs
+ (slash
- name
);
1411 } while (rmdir(dirs
) == 0 && (slash
= strrchr(dirs
, '/')));
1417 static int pathspec_item_cmp(const void *a_
, const void *b_
)
1419 struct pathspec_item
*a
, *b
;
1421 a
= (struct pathspec_item
*)a_
;
1422 b
= (struct pathspec_item
*)b_
;
1423 return strcmp(a
->match
, b
->match
);
1426 int init_pathspec(struct pathspec
*pathspec
, const char **paths
)
1428 const char **p
= paths
;
1431 memset(pathspec
, 0, sizeof(*pathspec
));
1436 pathspec
->raw
= paths
;
1437 pathspec
->nr
= p
- paths
;
1441 pathspec
->items
= xmalloc(sizeof(struct pathspec_item
)*pathspec
->nr
);
1442 for (i
= 0; i
< pathspec
->nr
; i
++) {
1443 struct pathspec_item
*item
= pathspec
->items
+i
;
1444 const char *path
= paths
[i
];
1447 item
->len
= strlen(path
);
1448 item
->nowildcard_len
= simple_length(path
);
1449 if (item
->nowildcard_len
< item
->len
)
1450 pathspec
->has_wildcard
= 1;
1453 qsort(pathspec
->items
, pathspec
->nr
,
1454 sizeof(struct pathspec_item
), pathspec_item_cmp
);
1459 void free_pathspec(struct pathspec
*pathspec
)
1461 free(pathspec
->items
);
1462 pathspec
->items
= NULL
;