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 if (flags
& GFNM_ONESTAR
) {
50 int pattern_len
= strlen(++pattern
);
51 int string_len
= strlen(string
);
52 return string_len
< pattern_len
||
54 string
+ string_len
- pattern_len
);
56 return fnmatch(pattern
, string
, fnm_flags
);
59 static size_t common_prefix_len(const char **pathspec
)
61 const char *n
, *first
;
68 while ((n
= *pathspec
++)) {
70 for (i
= 0; first
== n
|| i
< max
; i
++) {
72 if (!c
|| c
!= first
[i
] || is_glob_special(c
))
77 if (first
== n
|| len
< max
) {
87 * Returns a copy of the longest leading path common among all
90 char *common_prefix(const char **pathspec
)
92 unsigned long len
= common_prefix_len(pathspec
);
94 return len
? xmemdupz(*pathspec
, len
) : NULL
;
97 int fill_directory(struct dir_struct
*dir
, const char **pathspec
)
102 * Calculate common prefix for the pathspec, and
103 * use that to optimize the directory walk
105 len
= common_prefix_len(pathspec
);
107 /* Read the directory and prune it */
108 read_directory(dir
, pathspec
? *pathspec
: "", len
, pathspec
);
112 int within_depth(const char *name
, int namelen
,
113 int depth
, int max_depth
)
115 const char *cp
= name
, *cpe
= name
+ namelen
;
121 if (depth
> max_depth
)
128 * Does 'match' match the given name?
129 * A match is found if
131 * (1) the 'match' string is leading directory of 'name', or
132 * (2) the 'match' string is a wildcard and matches 'name', or
133 * (3) the 'match' string is exactly the same as 'name'.
135 * and the return value tells which case it was.
137 * It returns 0 when there is no match.
139 static int match_one(const char *match
, const char *name
, int namelen
)
143 /* If the match was just the prefix, we matched */
145 return MATCHED_RECURSIVELY
;
149 unsigned char c1
= tolower(*match
);
150 unsigned char c2
= tolower(*name
);
151 if (c1
== '\0' || is_glob_special(c1
))
161 unsigned char c1
= *match
;
162 unsigned char c2
= *name
;
163 if (c1
== '\0' || is_glob_special(c1
))
175 * If we don't match the matchstring exactly,
176 * we need to match by fnmatch
178 matchlen
= strlen(match
);
179 if (strncmp_icase(match
, name
, matchlen
))
180 return !fnmatch_icase(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
182 if (namelen
== matchlen
)
183 return MATCHED_EXACTLY
;
184 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
185 return MATCHED_RECURSIVELY
;
190 * Given a name and a list of pathspecs, see if the name matches
191 * any of the pathspecs. The caller is also interested in seeing
192 * all pathspec matches some names it calls this function with
193 * (otherwise the user could have mistyped the unmatched pathspec),
194 * and a mark is left in seen[] array for pathspec element that
195 * actually matched anything.
197 int match_pathspec(const char **pathspec
, const char *name
, int namelen
,
198 int prefix
, char *seen
)
208 for (i
= 0; pathspec
[i
] != NULL
; i
++) {
210 const char *match
= pathspec
[i
] + prefix
;
211 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
213 how
= match_one(match
, name
, namelen
);
217 if (seen
&& seen
[i
] < how
)
225 * Does 'match' match the given name?
226 * A match is found if
228 * (1) the 'match' string is leading directory of 'name', or
229 * (2) the 'match' string is a wildcard and matches 'name', or
230 * (3) the 'match' string is exactly the same as 'name'.
232 * and the return value tells which case it was.
234 * It returns 0 when there is no match.
236 static int match_pathspec_item(const struct pathspec_item
*item
, int prefix
,
237 const char *name
, int namelen
)
239 /* name/namelen has prefix cut off by caller */
240 const char *match
= item
->match
+ prefix
;
241 int matchlen
= item
->len
- prefix
;
243 /* If the match was just the prefix, we matched */
245 return MATCHED_RECURSIVELY
;
247 if (matchlen
<= namelen
&& !strncmp(match
, name
, matchlen
)) {
248 if (matchlen
== namelen
)
249 return MATCHED_EXACTLY
;
251 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
252 return MATCHED_RECURSIVELY
;
255 if (item
->nowildcard_len
< item
->len
&&
256 !git_fnmatch(match
, name
,
257 item
->flags
& PATHSPEC_ONESTAR
? GFNM_ONESTAR
: 0,
258 item
->nowildcard_len
- prefix
))
259 return MATCHED_FNMATCH
;
265 * Given a name and a list of pathspecs, see if the name matches
266 * any of the pathspecs. The caller is also interested in seeing
267 * all pathspec matches some names it calls this function with
268 * (otherwise the user could have mistyped the unmatched pathspec),
269 * and a mark is left in seen[] array for pathspec element that
270 * actually matched anything.
272 int match_pathspec_depth(const struct pathspec
*ps
,
273 const char *name
, int namelen
,
274 int prefix
, char *seen
)
279 if (!ps
->recursive
|| ps
->max_depth
== -1)
280 return MATCHED_RECURSIVELY
;
282 if (within_depth(name
, namelen
, 0, ps
->max_depth
))
283 return MATCHED_EXACTLY
;
291 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
293 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
295 how
= match_pathspec_item(ps
->items
+i
, prefix
, name
, namelen
);
296 if (ps
->recursive
&& ps
->max_depth
!= -1 &&
297 how
&& how
!= MATCHED_FNMATCH
) {
298 int len
= ps
->items
[i
].len
;
299 if (name
[len
] == '/')
301 if (within_depth(name
+len
, namelen
-len
, 0, ps
->max_depth
))
302 how
= MATCHED_EXACTLY
;
309 if (seen
&& seen
[i
] < how
)
317 * Return the length of the "simple" part of a path match limiter.
319 static int simple_length(const char *match
)
324 unsigned char c
= *match
++;
326 if (c
== '\0' || is_glob_special(c
))
331 static int no_wildcard(const char *string
)
333 return string
[simple_length(string
)] == '\0';
336 void parse_exclude_pattern(const char **pattern
,
341 const char *p
= *pattern
;
346 *flags
|= EXC_FLAG_NEGATIVE
;
350 if (len
&& p
[len
- 1] == '/') {
352 *flags
|= EXC_FLAG_MUSTBEDIR
;
354 for (i
= 0; i
< len
; i
++) {
359 *flags
|= EXC_FLAG_NODIR
;
360 *nowildcardlen
= simple_length(p
);
362 * we should have excluded the trailing slash from 'p' too,
363 * but that's one more allocation. Instead just make sure
364 * nowildcardlen does not exceed real patternlen
366 if (*nowildcardlen
> len
)
367 *nowildcardlen
= len
;
368 if (*p
== '*' && no_wildcard(p
+ 1))
369 *flags
|= EXC_FLAG_ENDSWITH
;
374 void add_exclude(const char *string
, const char *base
,
375 int baselen
, struct exclude_list
*which
)
382 parse_exclude_pattern(&string
, &patternlen
, &flags
, &nowildcardlen
);
383 if (flags
& EXC_FLAG_MUSTBEDIR
) {
385 x
= xmalloc(sizeof(*x
) + patternlen
+ 1);
387 memcpy(s
, string
, patternlen
);
388 s
[patternlen
] = '\0';
391 x
= xmalloc(sizeof(*x
));
394 x
->patternlen
= patternlen
;
395 x
->nowildcardlen
= nowildcardlen
;
397 x
->baselen
= baselen
;
399 ALLOC_GROW(which
->excludes
, which
->nr
+ 1, which
->alloc
);
400 which
->excludes
[which
->nr
++] = x
;
403 static void *read_skip_worktree_file_from_index(const char *path
, size_t *size
)
407 enum object_type type
;
409 struct index_state
*istate
= &the_index
;
412 pos
= index_name_pos(istate
, path
, len
);
415 if (!ce_skip_worktree(istate
->cache
[pos
]))
417 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
418 if (!data
|| type
!= OBJ_BLOB
) {
426 void free_excludes(struct exclude_list
*el
)
430 for (i
= 0; i
< el
->nr
; i
++)
431 free(el
->excludes
[i
]);
438 int add_excludes_from_file_to_list(const char *fname
,
442 struct exclude_list
*which
,
450 fd
= open(fname
, O_RDONLY
);
451 if (fd
< 0 || fstat(fd
, &st
) < 0) {
453 warn_on_inaccessible(fname
);
457 (buf
= read_skip_worktree_file_from_index(fname
, &size
)) == NULL
)
463 if (buf
[size
-1] != '\n') {
464 buf
= xrealloc(buf
, size
+1);
469 size
= xsize_t(st
.st_size
);
474 buf
= xmalloc(size
+1);
475 if (read_in_full(fd
, buf
, size
) != size
) {
487 for (i
= 0; i
< size
; i
++) {
488 if (buf
[i
] == '\n') {
489 if (entry
!= buf
+ i
&& entry
[0] != '#') {
490 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
491 add_exclude(entry
, base
, baselen
, which
);
499 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
501 if (add_excludes_from_file_to_list(fname
, "", 0, NULL
,
502 &dir
->exclude_list
[EXC_FILE
], 0) < 0)
503 die("cannot use %s as an exclude file", fname
);
506 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
508 struct exclude_list
*el
;
509 struct exclude_stack
*stk
= NULL
;
512 if ((!dir
->exclude_per_dir
) ||
513 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
514 return; /* too long a path -- ignore */
516 /* Pop the ones that are not the prefix of the path being checked. */
517 el
= &dir
->exclude_list
[EXC_DIRS
];
518 while ((stk
= dir
->exclude_stack
) != NULL
) {
519 if (stk
->baselen
<= baselen
&&
520 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
522 dir
->exclude_stack
= stk
->prev
;
523 while (stk
->exclude_ix
< el
->nr
)
524 free(el
->excludes
[--el
->nr
]);
529 /* Read from the parent directories and push them down. */
530 current
= stk
? stk
->baselen
: -1;
531 while (current
< baselen
) {
532 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
540 cp
= strchr(base
+ current
+ 1, '/');
542 die("oops in prep_exclude");
545 stk
->prev
= dir
->exclude_stack
;
546 stk
->baselen
= cp
- base
;
547 stk
->exclude_ix
= el
->nr
;
548 memcpy(dir
->basebuf
+ current
, base
+ current
,
549 stk
->baselen
- current
);
550 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
551 add_excludes_from_file_to_list(dir
->basebuf
,
552 dir
->basebuf
, stk
->baselen
,
553 &stk
->filebuf
, el
, 1);
554 dir
->exclude_stack
= stk
;
555 current
= stk
->baselen
;
557 dir
->basebuf
[baselen
] = '\0';
560 int match_basename(const char *basename
, int basenamelen
,
561 const char *pattern
, int prefix
, int patternlen
,
564 if (prefix
== patternlen
) {
565 if (!strcmp_icase(pattern
, basename
))
567 } else if (flags
& EXC_FLAG_ENDSWITH
) {
568 if (patternlen
- 1 <= basenamelen
&&
569 !strcmp_icase(pattern
+ 1,
570 basename
+ basenamelen
- patternlen
+ 1))
573 if (fnmatch_icase(pattern
, basename
, 0) == 0)
579 int match_pathname(const char *pathname
, int pathlen
,
580 const char *base
, int baselen
,
581 const char *pattern
, int prefix
, int patternlen
,
588 * match with FNM_PATHNAME; the pattern has base implicitly
591 if (*pattern
== '/') {
597 * baselen does not count the trailing slash. base[] may or
598 * may not end with a trailing slash though.
600 if (pathlen
< baselen
+ 1 ||
601 (baselen
&& pathname
[baselen
] != '/') ||
602 strncmp_icase(pathname
, base
, baselen
))
605 namelen
= baselen
? pathlen
- baselen
- 1 : pathlen
;
606 name
= pathname
+ pathlen
- namelen
;
610 * if the non-wildcard part is longer than the
611 * remaining pathname, surely it cannot match.
613 if (prefix
> namelen
)
616 if (strncmp_icase(pattern
, name
, prefix
))
623 return fnmatch_icase(pattern
, name
, FNM_PATHNAME
) == 0;
626 /* Scan the list and let the last match determine the fate.
627 * Return 1 for exclude, 0 for include and -1 for undecided.
629 int excluded_from_list(const char *pathname
,
630 int pathlen
, const char *basename
, int *dtype
,
631 struct exclude_list
*el
)
636 return -1; /* undefined */
638 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
639 struct exclude
*x
= el
->excludes
[i
];
640 const char *exclude
= x
->pattern
;
641 int to_exclude
= x
->flags
& EXC_FLAG_NEGATIVE
? 0 : 1;
642 int prefix
= x
->nowildcardlen
;
644 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
645 if (*dtype
== DT_UNKNOWN
)
646 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
647 if (*dtype
!= DT_DIR
)
651 if (x
->flags
& EXC_FLAG_NODIR
) {
652 if (match_basename(basename
,
653 pathlen
- (basename
- pathname
),
654 exclude
, prefix
, x
->patternlen
,
660 assert(x
->baselen
== 0 || x
->base
[x
->baselen
- 1] == '/');
661 if (match_pathname(pathname
, pathlen
,
662 x
->base
, x
->baselen
? x
->baselen
- 1 : 0,
663 exclude
, prefix
, x
->patternlen
, x
->flags
))
666 return -1; /* undecided */
669 static int excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
671 int pathlen
= strlen(pathname
);
673 const char *basename
= strrchr(pathname
, '/');
674 basename
= (basename
) ? basename
+1 : pathname
;
676 prep_exclude(dir
, pathname
, basename
-pathname
);
677 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
678 switch (excluded_from_list(pathname
, pathlen
, basename
,
679 dtype_p
, &dir
->exclude_list
[st
])) {
689 void path_exclude_check_init(struct path_exclude_check
*check
,
690 struct dir_struct
*dir
)
693 strbuf_init(&check
->path
, 256);
696 void path_exclude_check_clear(struct path_exclude_check
*check
)
698 strbuf_release(&check
->path
);
702 * Is this name excluded? This is for a caller like show_files() that
703 * do not honor directory hierarchy and iterate through paths that are
704 * possibly in an ignored directory.
706 * A path to a directory known to be excluded is left in check->path to
707 * optimize for repeated checks for files in the same excluded directory.
709 int path_excluded(struct path_exclude_check
*check
,
710 const char *name
, int namelen
, int *dtype
)
713 struct strbuf
*path
= &check
->path
;
716 * we allow the caller to pass namelen as an optimization; it
717 * must match the length of the name, as we eventually call
718 * excluded() on the whole name string.
721 namelen
= strlen(name
);
724 path
->len
<= namelen
&&
725 !memcmp(name
, path
->buf
, path
->len
) &&
726 (!name
[path
->len
] || name
[path
->len
] == '/'))
729 strbuf_setlen(path
, 0);
730 for (i
= 0; name
[i
]; i
++) {
735 if (excluded(check
->dir
, path
->buf
, &dt
))
738 strbuf_addch(path
, ch
);
741 /* An entry in the index; cannot be a directory with subentries */
742 strbuf_setlen(path
, 0);
744 return excluded(check
->dir
, name
, dtype
);
747 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
749 struct dir_entry
*ent
;
751 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
753 memcpy(ent
->name
, pathname
, len
);
758 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
760 if (cache_name_exists(pathname
, len
, ignore_case
))
763 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
764 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
767 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
769 if (!cache_name_is_other(pathname
, len
))
772 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
773 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
777 index_nonexistent
= 0,
783 * Do not use the alphabetically stored index to look up
784 * the directory name; instead, use the case insensitive
787 static enum exist_status
directory_exists_in_index_icase(const char *dirname
, int len
)
789 struct cache_entry
*ce
= index_name_exists(&the_index
, dirname
, len
+ 1, ignore_case
);
790 unsigned char endchar
;
793 return index_nonexistent
;
794 endchar
= ce
->name
[len
];
797 * The cache_entry structure returned will contain this dirname
798 * and possibly additional path components.
801 return index_directory
;
804 * If there are no additional path components, then this cache_entry
805 * represents a submodule. Submodules, despite being directories,
806 * are stored in the cache without a closing slash.
808 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
811 /* This should never be hit, but it exists just in case. */
812 return index_nonexistent
;
816 * The index sorts alphabetically by entry name, which
817 * means that a gitlink sorts as '\0' at the end, while
818 * a directory (which is defined not as an entry, but as
819 * the files it contains) will sort with the '/' at the
822 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
827 return directory_exists_in_index_icase(dirname
, len
);
829 pos
= cache_name_pos(dirname
, len
);
832 while (pos
< active_nr
) {
833 struct cache_entry
*ce
= active_cache
[pos
++];
834 unsigned char endchar
;
836 if (strncmp(ce
->name
, dirname
, len
))
838 endchar
= ce
->name
[len
];
842 return index_directory
;
843 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
846 return index_nonexistent
;
850 * When we find a directory when traversing the filesystem, we
851 * have three distinct cases:
854 * - see it as a directory
857 * and which one we choose depends on a combination of existing
858 * git index contents and the flags passed into the directory
861 * Case 1: If we *already* have entries in the index under that
862 * directory name, we always recurse into the directory to see
865 * Case 2: If we *already* have that directory name as a gitlink,
866 * we always continue to see it as a gitlink, regardless of whether
867 * there is an actual git directory there or not (it might not
868 * be checked out as a subproject!)
870 * Case 3: if we didn't have it in the index previously, we
871 * have a few sub-cases:
873 * (a) if "show_other_directories" is true, we show it as
874 * just a directory, unless "hide_empty_directories" is
875 * also true and the directory is empty, in which case
876 * we just ignore it entirely.
877 * (b) if it looks like a git directory, and we don't have
878 * 'no_gitlinks' set we treat it as a gitlink, and show it
880 * (c) otherwise, we recurse into it.
882 enum directory_treatment
{
885 recurse_into_directory
888 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
889 const char *dirname
, int len
,
890 const struct path_simplify
*simplify
)
892 /* The "len-1" is to strip the final '/' */
893 switch (directory_exists_in_index(dirname
, len
-1)) {
894 case index_directory
:
895 return recurse_into_directory
;
898 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
899 return ignore_directory
;
900 return show_directory
;
902 case index_nonexistent
:
903 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
905 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
906 unsigned char sha1
[20];
907 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
908 return show_directory
;
910 return recurse_into_directory
;
913 /* This is the "show_other_directories" case */
914 if (!(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
915 return show_directory
;
916 if (!read_directory_recursive(dir
, dirname
, len
, 1, simplify
))
917 return ignore_directory
;
918 return show_directory
;
922 * This is an inexact early pruning of any recursive directory
923 * reading - if the path cannot possibly be in the pathspec,
924 * return true, and we'll skip it early.
926 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
930 const char *match
= simplify
->path
;
931 int len
= simplify
->len
;
937 if (!memcmp(path
, match
, len
))
947 * This function tells us whether an excluded path matches a
948 * list of "interesting" pathspecs. That is, whether a path matched
949 * by any of the pathspecs could possibly be ignored by excluding
950 * the specified path. This can happen if:
952 * 1. the path is mentioned explicitly in the pathspec
954 * 2. the path is a directory prefix of some element in the
957 static int exclude_matches_pathspec(const char *path
, int len
,
958 const struct path_simplify
*simplify
)
961 for (; simplify
->path
; simplify
++) {
962 if (len
== simplify
->len
963 && !memcmp(path
, simplify
->path
, len
))
965 if (len
< simplify
->len
966 && simplify
->path
[len
] == '/'
967 && !memcmp(path
, simplify
->path
, len
))
974 static int get_index_dtype(const char *path
, int len
)
977 struct cache_entry
*ce
;
979 ce
= cache_name_exists(path
, len
, 0);
981 if (!ce_uptodate(ce
))
983 if (S_ISGITLINK(ce
->ce_mode
))
986 * Nobody actually cares about the
987 * difference between DT_LNK and DT_REG
992 /* Try to look it up as a directory */
993 pos
= cache_name_pos(path
, len
);
997 while (pos
< active_nr
) {
998 ce
= active_cache
[pos
++];
999 if (strncmp(ce
->name
, path
, len
))
1001 if (ce
->name
[len
] > '/')
1003 if (ce
->name
[len
] < '/')
1005 if (!ce_uptodate(ce
))
1006 break; /* continue? */
1012 static int get_dtype(struct dirent
*de
, const char *path
, int len
)
1014 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
1017 if (dtype
!= DT_UNKNOWN
)
1019 dtype
= get_index_dtype(path
, len
);
1020 if (dtype
!= DT_UNKNOWN
)
1022 if (lstat(path
, &st
))
1024 if (S_ISREG(st
.st_mode
))
1026 if (S_ISDIR(st
.st_mode
))
1028 if (S_ISLNK(st
.st_mode
))
1033 enum path_treatment
{
1039 static enum path_treatment
treat_one_path(struct dir_struct
*dir
,
1040 struct strbuf
*path
,
1041 const struct path_simplify
*simplify
,
1042 int dtype
, struct dirent
*de
)
1044 int exclude
= excluded(dir
, path
->buf
, &dtype
);
1045 if (exclude
&& (dir
->flags
& DIR_COLLECT_IGNORED
)
1046 && exclude_matches_pathspec(path
->buf
, path
->len
, simplify
))
1047 dir_add_ignored(dir
, path
->buf
, path
->len
);
1050 * Excluded? If we don't explicitly want to show
1051 * ignored files, ignore it
1053 if (exclude
&& !(dir
->flags
& DIR_SHOW_IGNORED
))
1054 return path_ignored
;
1056 if (dtype
== DT_UNKNOWN
)
1057 dtype
= get_dtype(de
, path
->buf
, path
->len
);
1060 * Do we want to see just the ignored files?
1061 * We still need to recurse into directories,
1062 * even if we don't ignore them, since the
1063 * directory may contain files that we do..
1065 if (!exclude
&& (dir
->flags
& DIR_SHOW_IGNORED
)) {
1066 if (dtype
!= DT_DIR
)
1067 return path_ignored
;
1072 return path_ignored
;
1074 strbuf_addch(path
, '/');
1075 switch (treat_directory(dir
, path
->buf
, path
->len
, simplify
)) {
1076 case show_directory
:
1077 if (exclude
!= !!(dir
->flags
1078 & DIR_SHOW_IGNORED
))
1079 return path_ignored
;
1081 case recurse_into_directory
:
1082 return path_recurse
;
1083 case ignore_directory
:
1084 return path_ignored
;
1091 return path_handled
;
1094 static enum path_treatment
treat_path(struct dir_struct
*dir
,
1096 struct strbuf
*path
,
1098 const struct path_simplify
*simplify
)
1102 if (is_dot_or_dotdot(de
->d_name
) || !strcmp(de
->d_name
, ".git"))
1103 return path_ignored
;
1104 strbuf_setlen(path
, baselen
);
1105 strbuf_addstr(path
, de
->d_name
);
1106 if (simplify_away(path
->buf
, path
->len
, simplify
))
1107 return path_ignored
;
1110 return treat_one_path(dir
, path
, simplify
, dtype
, de
);
1114 * Read a directory tree. We currently ignore anything but
1115 * directories, regular files and symlinks. That's because git
1116 * doesn't handle them at all yet. Maybe that will change some
1119 * Also, we ignore the name ".git" (even if it is not a directory).
1120 * That likely will not change.
1122 static int read_directory_recursive(struct dir_struct
*dir
,
1123 const char *base
, int baselen
,
1125 const struct path_simplify
*simplify
)
1130 struct strbuf path
= STRBUF_INIT
;
1132 strbuf_add(&path
, base
, baselen
);
1134 fdir
= opendir(path
.len
? path
.buf
: ".");
1138 while ((de
= readdir(fdir
)) != NULL
) {
1139 switch (treat_path(dir
, de
, &path
, baselen
, simplify
)) {
1141 contents
+= read_directory_recursive(dir
, path
.buf
,
1153 dir_add_name(dir
, path
.buf
, path
.len
);
1157 strbuf_release(&path
);
1162 static int cmp_name(const void *p1
, const void *p2
)
1164 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
1165 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
1167 return cache_name_compare(e1
->name
, e1
->len
,
1171 static struct path_simplify
*create_simplify(const char **pathspec
)
1174 struct path_simplify
*simplify
= NULL
;
1179 for (nr
= 0 ; ; nr
++) {
1182 alloc
= alloc_nr(alloc
);
1183 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
1185 match
= *pathspec
++;
1188 simplify
[nr
].path
= match
;
1189 simplify
[nr
].len
= simple_length(match
);
1191 simplify
[nr
].path
= NULL
;
1192 simplify
[nr
].len
= 0;
1196 static void free_simplify(struct path_simplify
*simplify
)
1201 static int treat_leading_path(struct dir_struct
*dir
,
1202 const char *path
, int len
,
1203 const struct path_simplify
*simplify
)
1205 struct strbuf sb
= STRBUF_INIT
;
1206 int baselen
, rc
= 0;
1209 while (len
&& path
[len
- 1] == '/')
1215 cp
= path
+ baselen
+ !!baselen
;
1216 cp
= memchr(cp
, '/', path
+ len
- cp
);
1220 baselen
= cp
- path
;
1221 strbuf_setlen(&sb
, 0);
1222 strbuf_add(&sb
, path
, baselen
);
1223 if (!is_directory(sb
.buf
))
1225 if (simplify_away(sb
.buf
, sb
.len
, simplify
))
1227 if (treat_one_path(dir
, &sb
, simplify
,
1228 DT_DIR
, NULL
) == path_ignored
)
1229 break; /* do not recurse into it */
1230 if (len
<= baselen
) {
1232 break; /* finished checking */
1235 strbuf_release(&sb
);
1239 int read_directory(struct dir_struct
*dir
, const char *path
, int len
, const char **pathspec
)
1241 struct path_simplify
*simplify
;
1243 if (has_symlink_leading_path(path
, len
))
1246 simplify
= create_simplify(pathspec
);
1247 if (!len
|| treat_leading_path(dir
, path
, len
, simplify
))
1248 read_directory_recursive(dir
, path
, len
, 0, simplify
);
1249 free_simplify(simplify
);
1250 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
1251 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
1255 int file_exists(const char *f
)
1258 return lstat(f
, &sb
) == 0;
1262 * Given two normalized paths (a trailing slash is ok), if subdir is
1263 * outside dir, return -1. Otherwise return the offset in subdir that
1264 * can be used as relative path to dir.
1266 int dir_inside_of(const char *subdir
, const char *dir
)
1270 assert(dir
&& subdir
&& *dir
&& *subdir
);
1272 while (*dir
&& *subdir
&& *dir
== *subdir
) {
1278 /* hel[p]/me vs hel[l]/yeah */
1279 if (*dir
&& *subdir
)
1283 return !*dir
? offset
: -1; /* same dir */
1285 /* foo/[b]ar vs foo/[] */
1286 if (is_dir_sep(dir
[-1]))
1287 return is_dir_sep(subdir
[-1]) ? offset
: -1;
1289 /* foo[/]bar vs foo[] */
1290 return is_dir_sep(*subdir
) ? offset
+ 1 : -1;
1293 int is_inside_dir(const char *dir
)
1298 if (!getcwd(cwd
, sizeof(cwd
)))
1299 die_errno("can't find the current directory");
1300 return dir_inside_of(cwd
, dir
) >= 0;
1303 int is_empty_dir(const char *path
)
1305 DIR *dir
= opendir(path
);
1312 while ((e
= readdir(dir
)) != NULL
)
1313 if (!is_dot_or_dotdot(e
->d_name
)) {
1322 static int remove_dir_recurse(struct strbuf
*path
, int flag
, int *kept_up
)
1326 int ret
= 0, original_len
= path
->len
, len
, kept_down
= 0;
1327 int only_empty
= (flag
& REMOVE_DIR_EMPTY_ONLY
);
1328 int keep_toplevel
= (flag
& REMOVE_DIR_KEEP_TOPLEVEL
);
1329 unsigned char submodule_head
[20];
1331 if ((flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
1332 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
)) {
1333 /* Do not descend and nuke a nested git work tree. */
1339 flag
&= ~REMOVE_DIR_KEEP_TOPLEVEL
;
1340 dir
= opendir(path
->buf
);
1342 /* an empty dir could be removed even if it is unreadble */
1344 return rmdir(path
->buf
);
1348 if (path
->buf
[original_len
- 1] != '/')
1349 strbuf_addch(path
, '/');
1352 while ((e
= readdir(dir
)) != NULL
) {
1354 if (is_dot_or_dotdot(e
->d_name
))
1357 strbuf_setlen(path
, len
);
1358 strbuf_addstr(path
, e
->d_name
);
1359 if (lstat(path
->buf
, &st
))
1361 else if (S_ISDIR(st
.st_mode
)) {
1362 if (!remove_dir_recurse(path
, flag
, &kept_down
))
1363 continue; /* happy */
1364 } else if (!only_empty
&& !unlink(path
->buf
))
1365 continue; /* happy, too */
1367 /* path too long, stat fails, or non-directory still exists */
1373 strbuf_setlen(path
, original_len
);
1374 if (!ret
&& !keep_toplevel
&& !kept_down
)
1375 ret
= rmdir(path
->buf
);
1378 * report the uplevel that it is not an error that we
1379 * did not rmdir() our directory.
1385 int remove_dir_recursively(struct strbuf
*path
, int flag
)
1387 return remove_dir_recurse(path
, flag
, NULL
);
1390 void setup_standard_excludes(struct dir_struct
*dir
)
1395 dir
->exclude_per_dir
= ".gitignore";
1396 path
= git_path("info/exclude");
1397 if (!excludes_file
) {
1398 home_config_paths(NULL
, &xdg_path
, "ignore");
1399 excludes_file
= xdg_path
;
1401 if (!access_or_warn(path
, R_OK
))
1402 add_excludes_from_file(dir
, path
);
1403 if (excludes_file
&& !access_or_warn(excludes_file
, R_OK
))
1404 add_excludes_from_file(dir
, excludes_file
);
1407 int remove_path(const char *name
)
1411 if (unlink(name
) && errno
!= ENOENT
)
1414 slash
= strrchr(name
, '/');
1416 char *dirs
= xstrdup(name
);
1417 slash
= dirs
+ (slash
- name
);
1420 } while (rmdir(dirs
) == 0 && (slash
= strrchr(dirs
, '/')));
1426 static int pathspec_item_cmp(const void *a_
, const void *b_
)
1428 struct pathspec_item
*a
, *b
;
1430 a
= (struct pathspec_item
*)a_
;
1431 b
= (struct pathspec_item
*)b_
;
1432 return strcmp(a
->match
, b
->match
);
1435 int init_pathspec(struct pathspec
*pathspec
, const char **paths
)
1437 const char **p
= paths
;
1440 memset(pathspec
, 0, sizeof(*pathspec
));
1445 pathspec
->raw
= paths
;
1446 pathspec
->nr
= p
- paths
;
1450 pathspec
->items
= xmalloc(sizeof(struct pathspec_item
)*pathspec
->nr
);
1451 for (i
= 0; i
< pathspec
->nr
; i
++) {
1452 struct pathspec_item
*item
= pathspec
->items
+i
;
1453 const char *path
= paths
[i
];
1456 item
->len
= strlen(path
);
1457 item
->nowildcard_len
= simple_length(path
);
1459 if (item
->nowildcard_len
< item
->len
) {
1460 pathspec
->has_wildcard
= 1;
1461 if (path
[item
->nowildcard_len
] == '*' &&
1462 no_wildcard(path
+ item
->nowildcard_len
+ 1))
1463 item
->flags
|= PATHSPEC_ONESTAR
;
1467 qsort(pathspec
->items
, pathspec
->nr
,
1468 sizeof(struct pathspec_item
), pathspec_item_cmp
);
1473 void free_pathspec(struct pathspec
*pathspec
)
1475 free(pathspec
->items
);
1476 pathspec
->items
= NULL
;