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 size_t common_prefix_len(const char **pathspec
)
39 const char *n
, *first
;
46 while ((n
= *pathspec
++)) {
48 for (i
= 0; first
== n
|| i
< max
; i
++) {
50 if (!c
|| c
!= first
[i
] || is_glob_special(c
))
55 if (first
== n
|| len
< max
) {
64 int fill_directory(struct dir_struct
*dir
, const char **pathspec
)
70 * Calculate common prefix for the pathspec, and
71 * use that to optimize the directory walk
73 len
= common_prefix_len(pathspec
);
77 path
= xmemdupz(*pathspec
, len
);
79 /* Read the directory and prune it */
80 read_directory(dir
, path
, len
, pathspec
);
86 int within_depth(const char *name
, int namelen
,
87 int depth
, int max_depth
)
89 const char *cp
= name
, *cpe
= name
+ namelen
;
95 if (depth
> max_depth
)
102 * Does 'match' match the given name?
103 * A match is found if
105 * (1) the 'match' string is leading directory of 'name', or
106 * (2) the 'match' string is a wildcard and matches 'name', or
107 * (3) the 'match' string is exactly the same as 'name'.
109 * and the return value tells which case it was.
111 * It returns 0 when there is no match.
113 static int match_one(const char *match
, const char *name
, int namelen
)
117 /* If the match was just the prefix, we matched */
119 return MATCHED_RECURSIVELY
;
123 unsigned char c1
= tolower(*match
);
124 unsigned char c2
= tolower(*name
);
125 if (c1
== '\0' || is_glob_special(c1
))
135 unsigned char c1
= *match
;
136 unsigned char c2
= *name
;
137 if (c1
== '\0' || is_glob_special(c1
))
149 * If we don't match the matchstring exactly,
150 * we need to match by fnmatch
152 matchlen
= strlen(match
);
153 if (strncmp_icase(match
, name
, matchlen
))
154 return !fnmatch_icase(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
156 if (namelen
== matchlen
)
157 return MATCHED_EXACTLY
;
158 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
159 return MATCHED_RECURSIVELY
;
164 * Given a name and a list of pathspecs, see if the name matches
165 * any of the pathspecs. The caller is also interested in seeing
166 * all pathspec matches some names it calls this function with
167 * (otherwise the user could have mistyped the unmatched pathspec),
168 * and a mark is left in seen[] array for pathspec element that
169 * actually matched anything.
171 int match_pathspec(const char **pathspec
, const char *name
, int namelen
,
172 int prefix
, char *seen
)
182 for (i
= 0; pathspec
[i
] != NULL
; i
++) {
184 const char *match
= pathspec
[i
] + prefix
;
185 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
187 how
= match_one(match
, name
, namelen
);
191 if (seen
&& seen
[i
] < how
)
199 * Does 'match' match the given name?
200 * A match is found if
202 * (1) the 'match' string is leading directory of 'name', or
203 * (2) the 'match' string is a wildcard and matches 'name', or
204 * (3) the 'match' string is exactly the same as 'name'.
206 * and the return value tells which case it was.
208 * It returns 0 when there is no match.
210 static int match_pathspec_item(const struct pathspec_item
*item
, int prefix
,
211 const char *name
, int namelen
)
213 /* name/namelen has prefix cut off by caller */
214 const char *match
= item
->match
+ prefix
;
215 int matchlen
= item
->len
- prefix
;
217 /* If the match was just the prefix, we matched */
219 return MATCHED_RECURSIVELY
;
221 if (matchlen
<= namelen
&& !strncmp(match
, name
, matchlen
)) {
222 if (matchlen
== namelen
)
223 return MATCHED_EXACTLY
;
225 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
226 return MATCHED_RECURSIVELY
;
229 if (item
->use_wildcard
&& !fnmatch(match
, name
, 0))
230 return MATCHED_FNMATCH
;
236 * Given a name and a list of pathspecs, see if the name matches
237 * any of the pathspecs. The caller is also interested in seeing
238 * all pathspec matches some names it calls this function with
239 * (otherwise the user could have mistyped the unmatched pathspec),
240 * and a mark is left in seen[] array for pathspec element that
241 * actually matched anything.
243 int match_pathspec_depth(const struct pathspec
*ps
,
244 const char *name
, int namelen
,
245 int prefix
, char *seen
)
250 if (!ps
->recursive
|| ps
->max_depth
== -1)
251 return MATCHED_RECURSIVELY
;
253 if (within_depth(name
, namelen
, 0, ps
->max_depth
))
254 return MATCHED_EXACTLY
;
262 for (i
= ps
->nr
- 1; i
>= 0; i
--) {
264 if (seen
&& seen
[i
] == MATCHED_EXACTLY
)
266 how
= match_pathspec_item(ps
->items
+i
, prefix
, name
, namelen
);
267 if (ps
->recursive
&& ps
->max_depth
!= -1 &&
268 how
&& how
!= MATCHED_FNMATCH
) {
269 int len
= ps
->items
[i
].len
;
270 if (name
[len
] == '/')
272 if (within_depth(name
+len
, namelen
-len
, 0, ps
->max_depth
))
273 how
= MATCHED_EXACTLY
;
280 if (seen
&& seen
[i
] < how
)
287 static int no_wildcard(const char *string
)
289 return string
[strcspn(string
, "*?[{\\")] == '\0';
292 void add_exclude(const char *string
, const char *base
,
293 int baselen
, struct exclude_list
*which
)
300 if (*string
== '!') {
304 len
= strlen(string
);
305 if (len
&& string
[len
- 1] == '/') {
307 x
= xmalloc(sizeof(*x
) + len
);
309 memcpy(s
, string
, len
- 1);
313 flags
= EXC_FLAG_MUSTBEDIR
;
315 x
= xmalloc(sizeof(*x
));
318 x
->to_exclude
= to_exclude
;
319 x
->patternlen
= strlen(string
);
321 x
->baselen
= baselen
;
323 if (!strchr(string
, '/'))
324 x
->flags
|= EXC_FLAG_NODIR
;
325 if (no_wildcard(string
))
326 x
->flags
|= EXC_FLAG_NOWILDCARD
;
327 if (*string
== '*' && no_wildcard(string
+1))
328 x
->flags
|= EXC_FLAG_ENDSWITH
;
329 ALLOC_GROW(which
->excludes
, which
->nr
+ 1, which
->alloc
);
330 which
->excludes
[which
->nr
++] = x
;
333 static void *read_skip_worktree_file_from_index(const char *path
, size_t *size
)
337 enum object_type type
;
339 struct index_state
*istate
= &the_index
;
342 pos
= index_name_pos(istate
, path
, len
);
345 if (!ce_skip_worktree(istate
->cache
[pos
]))
347 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
348 if (!data
|| type
!= OBJ_BLOB
) {
356 void free_excludes(struct exclude_list
*el
)
360 for (i
= 0; i
< el
->nr
; i
++)
361 free(el
->excludes
[i
]);
368 int add_excludes_from_file_to_list(const char *fname
,
372 struct exclude_list
*which
,
380 fd
= open(fname
, O_RDONLY
);
381 if (fd
< 0 || fstat(fd
, &st
) < 0) {
385 (buf
= read_skip_worktree_file_from_index(fname
, &size
)) == NULL
)
391 if (buf
[size
-1] != '\n') {
392 buf
= xrealloc(buf
, size
+1);
397 size
= xsize_t(st
.st_size
);
402 buf
= xmalloc(size
+1);
403 if (read_in_full(fd
, buf
, size
) != size
) {
415 for (i
= 0; i
< size
; i
++) {
416 if (buf
[i
] == '\n') {
417 if (entry
!= buf
+ i
&& entry
[0] != '#') {
418 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
419 add_exclude(entry
, base
, baselen
, which
);
427 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
429 if (add_excludes_from_file_to_list(fname
, "", 0, NULL
,
430 &dir
->exclude_list
[EXC_FILE
], 0) < 0)
431 die("cannot use %s as an exclude file", fname
);
434 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
436 struct exclude_list
*el
;
437 struct exclude_stack
*stk
= NULL
;
440 if ((!dir
->exclude_per_dir
) ||
441 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
442 return; /* too long a path -- ignore */
444 /* Pop the ones that are not the prefix of the path being checked. */
445 el
= &dir
->exclude_list
[EXC_DIRS
];
446 while ((stk
= dir
->exclude_stack
) != NULL
) {
447 if (stk
->baselen
<= baselen
&&
448 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
450 dir
->exclude_stack
= stk
->prev
;
451 while (stk
->exclude_ix
< el
->nr
)
452 free(el
->excludes
[--el
->nr
]);
457 /* Read from the parent directories and push them down. */
458 current
= stk
? stk
->baselen
: -1;
459 while (current
< baselen
) {
460 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
468 cp
= strchr(base
+ current
+ 1, '/');
470 die("oops in prep_exclude");
473 stk
->prev
= dir
->exclude_stack
;
474 stk
->baselen
= cp
- base
;
475 stk
->exclude_ix
= el
->nr
;
476 memcpy(dir
->basebuf
+ current
, base
+ current
,
477 stk
->baselen
- current
);
478 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
479 add_excludes_from_file_to_list(dir
->basebuf
,
480 dir
->basebuf
, stk
->baselen
,
481 &stk
->filebuf
, el
, 1);
482 dir
->exclude_stack
= stk
;
483 current
= stk
->baselen
;
485 dir
->basebuf
[baselen
] = '\0';
488 /* Scan the list and let the last match determine the fate.
489 * Return 1 for exclude, 0 for include and -1 for undecided.
491 int excluded_from_list(const char *pathname
,
492 int pathlen
, const char *basename
, int *dtype
,
493 struct exclude_list
*el
)
498 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
499 struct exclude
*x
= el
->excludes
[i
];
500 const char *exclude
= x
->pattern
;
501 int to_exclude
= x
->to_exclude
;
503 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
504 if (*dtype
== DT_UNKNOWN
)
505 *dtype
= get_dtype(NULL
, pathname
, pathlen
);
506 if (*dtype
!= DT_DIR
)
510 if (x
->flags
& EXC_FLAG_NODIR
) {
512 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
513 if (!strcmp_icase(exclude
, basename
))
515 } else if (x
->flags
& EXC_FLAG_ENDSWITH
) {
516 if (x
->patternlen
- 1 <= pathlen
&&
517 !strcmp_icase(exclude
+ 1, pathname
+ pathlen
- x
->patternlen
+ 1))
520 if (fnmatch_icase(exclude
, basename
, 0) == 0)
525 /* match with FNM_PATHNAME:
526 * exclude has base (baselen long) implicitly
529 int baselen
= x
->baselen
;
533 if (pathlen
< baselen
||
534 (baselen
&& pathname
[baselen
-1] != '/') ||
535 strncmp_icase(pathname
, x
->base
, baselen
))
538 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
539 if (!strcmp_icase(exclude
, pathname
+ baselen
))
542 if (fnmatch_icase(exclude
, pathname
+baselen
,
549 return -1; /* undecided */
552 int excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
554 int pathlen
= strlen(pathname
);
556 const char *basename
= strrchr(pathname
, '/');
557 basename
= (basename
) ? basename
+1 : pathname
;
559 prep_exclude(dir
, pathname
, basename
-pathname
);
560 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
561 switch (excluded_from_list(pathname
, pathlen
, basename
,
562 dtype_p
, &dir
->exclude_list
[st
])) {
572 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
574 struct dir_entry
*ent
;
576 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
578 memcpy(ent
->name
, pathname
, len
);
583 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
585 if (cache_name_exists(pathname
, len
, ignore_case
))
588 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
589 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
592 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
594 if (!cache_name_is_other(pathname
, len
))
597 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
598 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
602 index_nonexistent
= 0,
608 * Do not use the alphabetically stored index to look up
609 * the directory name; instead, use the case insensitive
612 static enum exist_status
directory_exists_in_index_icase(const char *dirname
, int len
)
614 struct cache_entry
*ce
= index_name_exists(&the_index
, dirname
, len
+ 1, ignore_case
);
615 unsigned char endchar
;
618 return index_nonexistent
;
619 endchar
= ce
->name
[len
];
622 * The cache_entry structure returned will contain this dirname
623 * and possibly additional path components.
626 return index_directory
;
629 * If there are no additional path components, then this cache_entry
630 * represents a submodule. Submodules, despite being directories,
631 * are stored in the cache without a closing slash.
633 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
636 /* This should never be hit, but it exists just in case. */
637 return index_nonexistent
;
641 * The index sorts alphabetically by entry name, which
642 * means that a gitlink sorts as '\0' at the end, while
643 * a directory (which is defined not as an entry, but as
644 * the files it contains) will sort with the '/' at the
647 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
652 return directory_exists_in_index_icase(dirname
, len
);
654 pos
= cache_name_pos(dirname
, len
);
657 while (pos
< active_nr
) {
658 struct cache_entry
*ce
= active_cache
[pos
++];
659 unsigned char endchar
;
661 if (strncmp(ce
->name
, dirname
, len
))
663 endchar
= ce
->name
[len
];
667 return index_directory
;
668 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
671 return index_nonexistent
;
675 * When we find a directory when traversing the filesystem, we
676 * have three distinct cases:
679 * - see it as a directory
682 * and which one we choose depends on a combination of existing
683 * git index contents and the flags passed into the directory
686 * Case 1: If we *already* have entries in the index under that
687 * directory name, we always recurse into the directory to see
690 * Case 2: If we *already* have that directory name as a gitlink,
691 * we always continue to see it as a gitlink, regardless of whether
692 * there is an actual git directory there or not (it might not
693 * be checked out as a subproject!)
695 * Case 3: if we didn't have it in the index previously, we
696 * have a few sub-cases:
698 * (a) if "show_other_directories" is true, we show it as
699 * just a directory, unless "hide_empty_directories" is
700 * also true and the directory is empty, in which case
701 * we just ignore it entirely.
702 * (b) if it looks like a git directory, and we don't have
703 * 'no_gitlinks' set we treat it as a gitlink, and show it
705 * (c) otherwise, we recurse into it.
707 enum directory_treatment
{
710 recurse_into_directory
713 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
714 const char *dirname
, int len
,
715 const struct path_simplify
*simplify
)
717 /* The "len-1" is to strip the final '/' */
718 switch (directory_exists_in_index(dirname
, len
-1)) {
719 case index_directory
:
720 return recurse_into_directory
;
723 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
724 return ignore_directory
;
725 return show_directory
;
727 case index_nonexistent
:
728 if (dir
->flags
& DIR_SHOW_OTHER_DIRECTORIES
)
730 if (!(dir
->flags
& DIR_NO_GITLINKS
)) {
731 unsigned char sha1
[20];
732 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
733 return show_directory
;
735 return recurse_into_directory
;
738 /* This is the "show_other_directories" case */
739 if (!(dir
->flags
& DIR_HIDE_EMPTY_DIRECTORIES
))
740 return show_directory
;
741 if (!read_directory_recursive(dir
, dirname
, len
, 1, simplify
))
742 return ignore_directory
;
743 return show_directory
;
747 * This is an inexact early pruning of any recursive directory
748 * reading - if the path cannot possibly be in the pathspec,
749 * return true, and we'll skip it early.
751 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
755 const char *match
= simplify
->path
;
756 int len
= simplify
->len
;
762 if (!memcmp(path
, match
, len
))
772 * This function tells us whether an excluded path matches a
773 * list of "interesting" pathspecs. That is, whether a path matched
774 * by any of the pathspecs could possibly be ignored by excluding
775 * the specified path. This can happen if:
777 * 1. the path is mentioned explicitly in the pathspec
779 * 2. the path is a directory prefix of some element in the
782 static int exclude_matches_pathspec(const char *path
, int len
,
783 const struct path_simplify
*simplify
)
786 for (; simplify
->path
; simplify
++) {
787 if (len
== simplify
->len
788 && !memcmp(path
, simplify
->path
, len
))
790 if (len
< simplify
->len
791 && simplify
->path
[len
] == '/'
792 && !memcmp(path
, simplify
->path
, len
))
799 static int get_index_dtype(const char *path
, int len
)
802 struct cache_entry
*ce
;
804 ce
= cache_name_exists(path
, len
, 0);
806 if (!ce_uptodate(ce
))
808 if (S_ISGITLINK(ce
->ce_mode
))
811 * Nobody actually cares about the
812 * difference between DT_LNK and DT_REG
817 /* Try to look it up as a directory */
818 pos
= cache_name_pos(path
, len
);
822 while (pos
< active_nr
) {
823 ce
= active_cache
[pos
++];
824 if (strncmp(ce
->name
, path
, len
))
826 if (ce
->name
[len
] > '/')
828 if (ce
->name
[len
] < '/')
830 if (!ce_uptodate(ce
))
831 break; /* continue? */
837 static int get_dtype(struct dirent
*de
, const char *path
, int len
)
839 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
842 if (dtype
!= DT_UNKNOWN
)
844 dtype
= get_index_dtype(path
, len
);
845 if (dtype
!= DT_UNKNOWN
)
847 if (lstat(path
, &st
))
849 if (S_ISREG(st
.st_mode
))
851 if (S_ISDIR(st
.st_mode
))
853 if (S_ISLNK(st
.st_mode
))
858 enum path_treatment
{
864 static enum path_treatment
treat_one_path(struct dir_struct
*dir
,
865 char *path
, int *len
,
866 const struct path_simplify
*simplify
,
867 int dtype
, struct dirent
*de
)
869 int exclude
= excluded(dir
, path
, &dtype
);
870 if (exclude
&& (dir
->flags
& DIR_COLLECT_IGNORED
)
871 && exclude_matches_pathspec(path
, *len
, simplify
))
872 dir_add_ignored(dir
, path
, *len
);
875 * Excluded? If we don't explicitly want to show
876 * ignored files, ignore it
878 if (exclude
&& !(dir
->flags
& DIR_SHOW_IGNORED
))
881 if (dtype
== DT_UNKNOWN
)
882 dtype
= get_dtype(de
, path
, *len
);
885 * Do we want to see just the ignored files?
886 * We still need to recurse into directories,
887 * even if we don't ignore them, since the
888 * directory may contain files that we do..
890 if (!exclude
&& (dir
->flags
& DIR_SHOW_IGNORED
)) {
899 memcpy(path
+ *len
, "/", 2);
901 switch (treat_directory(dir
, path
, *len
, simplify
)) {
903 if (exclude
!= !!(dir
->flags
907 case recurse_into_directory
:
909 case ignore_directory
:
920 static enum path_treatment
treat_path(struct dir_struct
*dir
,
922 char *path
, int path_max
,
924 const struct path_simplify
*simplify
,
929 if (is_dot_or_dotdot(de
->d_name
) || !strcmp(de
->d_name
, ".git"))
931 *len
= strlen(de
->d_name
);
932 /* Ignore overly long pathnames! */
933 if (*len
+ baselen
+ 8 > path_max
)
935 memcpy(path
+ baselen
, de
->d_name
, *len
+ 1);
937 if (simplify_away(path
, *len
, simplify
))
941 return treat_one_path(dir
, path
, len
, simplify
, dtype
, de
);
945 * Read a directory tree. We currently ignore anything but
946 * directories, regular files and symlinks. That's because git
947 * doesn't handle them at all yet. Maybe that will change some
950 * Also, we ignore the name ".git" (even if it is not a directory).
951 * That likely will not change.
953 static int read_directory_recursive(struct dir_struct
*dir
,
954 const char *base
, int baselen
,
956 const struct path_simplify
*simplify
)
958 DIR *fdir
= opendir(*base
? base
: ".");
963 char path
[PATH_MAX
+ 1];
964 memcpy(path
, base
, baselen
);
966 while ((de
= readdir(fdir
)) != NULL
) {
968 switch (treat_path(dir
, de
, path
, sizeof(path
),
969 baselen
, simplify
, &len
)) {
971 contents
+= read_directory_recursive
972 (dir
, path
, len
, 0, simplify
);
983 dir_add_name(dir
, path
, len
);
992 static int cmp_name(const void *p1
, const void *p2
)
994 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
995 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
997 return cache_name_compare(e1
->name
, e1
->len
,
1002 * Return the length of the "simple" part of a path match limiter.
1004 static int simple_length(const char *match
)
1009 unsigned char c
= *match
++;
1011 if (c
== '\0' || is_glob_special(c
))
1016 static struct path_simplify
*create_simplify(const char **pathspec
)
1019 struct path_simplify
*simplify
= NULL
;
1024 for (nr
= 0 ; ; nr
++) {
1027 alloc
= alloc_nr(alloc
);
1028 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
1030 match
= *pathspec
++;
1033 simplify
[nr
].path
= match
;
1034 simplify
[nr
].len
= simple_length(match
);
1036 simplify
[nr
].path
= NULL
;
1037 simplify
[nr
].len
= 0;
1041 static void free_simplify(struct path_simplify
*simplify
)
1046 static int treat_leading_path(struct dir_struct
*dir
,
1047 const char *path
, int len
,
1048 const struct path_simplify
*simplify
)
1050 char pathbuf
[PATH_MAX
];
1054 while (len
&& path
[len
- 1] == '/')
1060 cp
= path
+ baselen
+ !!baselen
;
1061 cp
= memchr(cp
, '/', path
+ len
- cp
);
1065 baselen
= cp
- path
;
1066 memcpy(pathbuf
, path
, baselen
);
1067 pathbuf
[baselen
] = '\0';
1068 if (!is_directory(pathbuf
))
1070 if (simplify_away(pathbuf
, baselen
, simplify
))
1073 if (treat_one_path(dir
, pathbuf
, &blen
, simplify
,
1074 DT_DIR
, NULL
) == path_ignored
)
1075 return 0; /* do not recurse into it */
1077 return 1; /* finished checking */
1081 int read_directory(struct dir_struct
*dir
, const char *path
, int len
, const char **pathspec
)
1083 struct path_simplify
*simplify
;
1085 if (has_symlink_leading_path(path
, len
))
1088 simplify
= create_simplify(pathspec
);
1089 if (!len
|| treat_leading_path(dir
, path
, len
, simplify
))
1090 read_directory_recursive(dir
, path
, len
, 0, simplify
);
1091 free_simplify(simplify
);
1092 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
1093 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
1097 int file_exists(const char *f
)
1100 return lstat(f
, &sb
) == 0;
1104 * Given two normalized paths (a trailing slash is ok), if subdir is
1105 * outside dir, return -1. Otherwise return the offset in subdir that
1106 * can be used as relative path to dir.
1108 int dir_inside_of(const char *subdir
, const char *dir
)
1112 assert(dir
&& subdir
&& *dir
&& *subdir
);
1114 while (*dir
&& *subdir
&& *dir
== *subdir
) {
1120 /* hel[p]/me vs hel[l]/yeah */
1121 if (*dir
&& *subdir
)
1125 return !*dir
? offset
: -1; /* same dir */
1127 /* foo/[b]ar vs foo/[] */
1128 if (is_dir_sep(dir
[-1]))
1129 return is_dir_sep(subdir
[-1]) ? offset
: -1;
1131 /* foo[/]bar vs foo[] */
1132 return is_dir_sep(*subdir
) ? offset
+ 1 : -1;
1135 int is_inside_dir(const char *dir
)
1140 if (!getcwd(cwd
, sizeof(cwd
)))
1141 die_errno("can't find the current directory");
1142 return dir_inside_of(cwd
, dir
) >= 0;
1145 int is_empty_dir(const char *path
)
1147 DIR *dir
= opendir(path
);
1154 while ((e
= readdir(dir
)) != NULL
)
1155 if (!is_dot_or_dotdot(e
->d_name
)) {
1164 int remove_dir_recursively(struct strbuf
*path
, int flag
)
1168 int ret
= 0, original_len
= path
->len
, len
;
1169 int only_empty
= (flag
& REMOVE_DIR_EMPTY_ONLY
);
1170 unsigned char submodule_head
[20];
1172 if ((flag
& REMOVE_DIR_KEEP_NESTED_GIT
) &&
1173 !resolve_gitlink_ref(path
->buf
, "HEAD", submodule_head
))
1174 /* Do not descend and nuke a nested git work tree. */
1177 dir
= opendir(path
->buf
);
1179 return rmdir(path
->buf
);
1180 if (path
->buf
[original_len
- 1] != '/')
1181 strbuf_addch(path
, '/');
1184 while ((e
= readdir(dir
)) != NULL
) {
1186 if (is_dot_or_dotdot(e
->d_name
))
1189 strbuf_setlen(path
, len
);
1190 strbuf_addstr(path
, e
->d_name
);
1191 if (lstat(path
->buf
, &st
))
1193 else if (S_ISDIR(st
.st_mode
)) {
1194 if (!remove_dir_recursively(path
, only_empty
))
1195 continue; /* happy */
1196 } else if (!only_empty
&& !unlink(path
->buf
))
1197 continue; /* happy, too */
1199 /* path too long, stat fails, or non-directory still exists */
1205 strbuf_setlen(path
, original_len
);
1207 ret
= rmdir(path
->buf
);
1211 void setup_standard_excludes(struct dir_struct
*dir
)
1215 dir
->exclude_per_dir
= ".gitignore";
1216 path
= git_path("info/exclude");
1217 if (!access(path
, R_OK
))
1218 add_excludes_from_file(dir
, path
);
1219 if (excludes_file
&& !access(excludes_file
, R_OK
))
1220 add_excludes_from_file(dir
, excludes_file
);
1223 int remove_path(const char *name
)
1227 if (unlink(name
) && errno
!= ENOENT
)
1230 slash
= strrchr(name
, '/');
1232 char *dirs
= xstrdup(name
);
1233 slash
= dirs
+ (slash
- name
);
1236 } while (rmdir(dirs
) == 0 && (slash
= strrchr(dirs
, '/')));
1242 static int pathspec_item_cmp(const void *a_
, const void *b_
)
1244 struct pathspec_item
*a
, *b
;
1246 a
= (struct pathspec_item
*)a_
;
1247 b
= (struct pathspec_item
*)b_
;
1248 return strcmp(a
->match
, b
->match
);
1251 int init_pathspec(struct pathspec
*pathspec
, const char **paths
)
1253 const char **p
= paths
;
1256 memset(pathspec
, 0, sizeof(*pathspec
));
1261 pathspec
->raw
= paths
;
1262 pathspec
->nr
= p
- paths
;
1266 pathspec
->items
= xmalloc(sizeof(struct pathspec_item
)*pathspec
->nr
);
1267 for (i
= 0; i
< pathspec
->nr
; i
++) {
1268 struct pathspec_item
*item
= pathspec
->items
+i
;
1269 const char *path
= paths
[i
];
1272 item
->len
= strlen(path
);
1273 item
->use_wildcard
= !no_wildcard(path
);
1274 if (item
->use_wildcard
)
1275 pathspec
->has_wildcard
= 1;
1278 qsort(pathspec
->items
, pathspec
->nr
,
1279 sizeof(struct pathspec_item
), pathspec_item_cmp
);
1284 void free_pathspec(struct pathspec
*pathspec
)
1286 free(pathspec
->items
);
1287 pathspec
->items
= NULL
;