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
,
18 const char *path
, const char *base
, int baselen
,
19 int check_only
, const struct path_simplify
*simplify
);
20 static int get_dtype(struct dirent
*de
, const char *path
);
22 int common_prefix(const char **pathspec
)
24 const char *path
, *slash
, *next
;
31 slash
= strrchr(path
, '/');
35 prefix
= slash
- path
+ 1;
36 while ((next
= *++pathspec
) != NULL
) {
37 int len
= strlen(next
);
38 if (len
>= prefix
&& !memcmp(path
, next
, prefix
))
44 if (next
[--len
] != '/')
46 if (memcmp(path
, next
, len
+1))
55 static inline int special_char(unsigned char c1
)
57 return !c1
|| c1
== '*' || c1
== '[' || c1
== '?' || c1
== '\\';
61 * Does 'match' match the given name?
64 * (1) the 'match' string is leading directory of 'name', or
65 * (2) the 'match' string is a wildcard and matches 'name', or
66 * (3) the 'match' string is exactly the same as 'name'.
68 * and the return value tells which case it was.
70 * It returns 0 when there is no match.
72 static int match_one(const char *match
, const char *name
, int namelen
)
76 /* If the match was just the prefix, we matched */
78 return MATCHED_RECURSIVELY
;
81 unsigned char c1
= *match
;
82 unsigned char c2
= *name
;
94 * If we don't match the matchstring exactly,
95 * we need to match by fnmatch
97 matchlen
= strlen(match
);
98 if (strncmp(match
, name
, matchlen
))
99 return !fnmatch(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
101 if (namelen
== matchlen
)
102 return MATCHED_EXACTLY
;
103 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
104 return MATCHED_RECURSIVELY
;
109 * Given a name and a list of pathspecs, see if the name matches
110 * any of the pathspecs. The caller is also interested in seeing
111 * all pathspec matches some names it calls this function with
112 * (otherwise the user could have mistyped the unmatched pathspec),
113 * and a mark is left in seen[] array for pathspec element that
114 * actually matched anything.
116 int match_pathspec(const char **pathspec
, const char *name
, int namelen
, int prefix
, char *seen
)
124 for (retval
= 0; (match
= *pathspec
++) != NULL
; seen
++) {
126 if (retval
&& *seen
== MATCHED_EXACTLY
)
129 how
= match_one(match
, name
, namelen
);
140 static int no_wildcard(const char *string
)
142 return string
[strcspn(string
, "*?[{\\")] == '\0';
145 void add_exclude(const char *string
, const char *base
,
146 int baselen
, struct exclude_list
*which
)
153 if (*string
== '!') {
157 len
= strlen(string
);
158 if (len
&& string
[len
- 1] == '/') {
160 x
= xmalloc(sizeof(*x
) + len
);
162 memcpy(s
, string
, len
- 1);
166 flags
= EXC_FLAG_MUSTBEDIR
;
168 x
= xmalloc(sizeof(*x
));
171 x
->to_exclude
= to_exclude
;
172 x
->patternlen
= strlen(string
);
174 x
->baselen
= baselen
;
176 if (!strchr(string
, '/'))
177 x
->flags
|= EXC_FLAG_NODIR
;
178 if (no_wildcard(string
))
179 x
->flags
|= EXC_FLAG_NOWILDCARD
;
180 if (*string
== '*' && no_wildcard(string
+1))
181 x
->flags
|= EXC_FLAG_ENDSWITH
;
182 ALLOC_GROW(which
->excludes
, which
->nr
+ 1, which
->alloc
);
183 which
->excludes
[which
->nr
++] = x
;
186 static int add_excludes_from_file_1(const char *fname
,
190 struct exclude_list
*which
)
197 fd
= open(fname
, O_RDONLY
);
198 if (fd
< 0 || fstat(fd
, &st
) < 0)
200 size
= xsize_t(st
.st_size
);
205 buf
= xmalloc(size
+1);
206 if (read_in_full(fd
, buf
, size
) != size
)
217 for (i
= 0; i
< size
; i
++) {
218 if (buf
[i
] == '\n') {
219 if (entry
!= buf
+ i
&& entry
[0] != '#') {
220 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
221 add_exclude(entry
, base
, baselen
, which
);
234 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
236 if (add_excludes_from_file_1(fname
, "", 0, NULL
,
237 &dir
->exclude_list
[EXC_FILE
]) < 0)
238 die("cannot use %s as an exclude file", fname
);
241 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
243 struct exclude_list
*el
;
244 struct exclude_stack
*stk
= NULL
;
247 if ((!dir
->exclude_per_dir
) ||
248 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
249 return; /* too long a path -- ignore */
251 /* Pop the ones that are not the prefix of the path being checked. */
252 el
= &dir
->exclude_list
[EXC_DIRS
];
253 while ((stk
= dir
->exclude_stack
) != NULL
) {
254 if (stk
->baselen
<= baselen
&&
255 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
257 dir
->exclude_stack
= stk
->prev
;
258 while (stk
->exclude_ix
< el
->nr
)
259 free(el
->excludes
[--el
->nr
]);
264 /* Read from the parent directories and push them down. */
265 current
= stk
? stk
->baselen
: -1;
266 while (current
< baselen
) {
267 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
275 cp
= strchr(base
+ current
+ 1, '/');
277 die("oops in prep_exclude");
280 stk
->prev
= dir
->exclude_stack
;
281 stk
->baselen
= cp
- base
;
282 stk
->exclude_ix
= el
->nr
;
283 memcpy(dir
->basebuf
+ current
, base
+ current
,
284 stk
->baselen
- current
);
285 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
286 add_excludes_from_file_1(dir
->basebuf
,
287 dir
->basebuf
, stk
->baselen
,
289 dir
->exclude_stack
= stk
;
290 current
= stk
->baselen
;
292 dir
->basebuf
[baselen
] = '\0';
295 /* Scan the list and let the last match determine the fate.
296 * Return 1 for exclude, 0 for include and -1 for undecided.
298 static int excluded_1(const char *pathname
,
299 int pathlen
, const char *basename
, int *dtype
,
300 struct exclude_list
*el
)
305 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
306 struct exclude
*x
= el
->excludes
[i
];
307 const char *exclude
= x
->pattern
;
308 int to_exclude
= x
->to_exclude
;
310 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
311 if (*dtype
== DT_UNKNOWN
)
312 *dtype
= get_dtype(NULL
, pathname
);
313 if (*dtype
!= DT_DIR
)
317 if (x
->flags
& EXC_FLAG_NODIR
) {
319 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
320 if (!strcmp(exclude
, basename
))
322 } else if (x
->flags
& EXC_FLAG_ENDSWITH
) {
323 if (x
->patternlen
- 1 <= pathlen
&&
324 !strcmp(exclude
+ 1, pathname
+ pathlen
- x
->patternlen
+ 1))
327 if (fnmatch(exclude
, basename
, 0) == 0)
332 /* match with FNM_PATHNAME:
333 * exclude has base (baselen long) implicitly
336 int baselen
= x
->baselen
;
340 if (pathlen
< baselen
||
341 (baselen
&& pathname
[baselen
-1] != '/') ||
342 strncmp(pathname
, x
->base
, baselen
))
345 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
346 if (!strcmp(exclude
, pathname
+ baselen
))
349 if (fnmatch(exclude
, pathname
+baselen
,
356 return -1; /* undecided */
359 int excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
361 int pathlen
= strlen(pathname
);
363 const char *basename
= strrchr(pathname
, '/');
364 basename
= (basename
) ? basename
+1 : pathname
;
366 prep_exclude(dir
, pathname
, basename
-pathname
);
367 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
368 switch (excluded_1(pathname
, pathlen
, basename
,
369 dtype_p
, &dir
->exclude_list
[st
])) {
379 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
381 struct dir_entry
*ent
;
383 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
385 memcpy(ent
->name
, pathname
, len
);
390 struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
392 if (cache_name_exists(pathname
, len
, ignore_case
))
395 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
396 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
399 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
401 if (cache_name_pos(pathname
, len
) >= 0)
404 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
405 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
409 index_nonexistent
= 0,
415 * The index sorts alphabetically by entry name, which
416 * means that a gitlink sorts as '\0' at the end, while
417 * a directory (which is defined not as an entry, but as
418 * the files it contains) will sort with the '/' at the
421 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
423 int pos
= cache_name_pos(dirname
, len
);
426 while (pos
< active_nr
) {
427 struct cache_entry
*ce
= active_cache
[pos
++];
428 unsigned char endchar
;
430 if (strncmp(ce
->name
, dirname
, len
))
432 endchar
= ce
->name
[len
];
436 return index_directory
;
437 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
440 return index_nonexistent
;
444 * When we find a directory when traversing the filesystem, we
445 * have three distinct cases:
448 * - see it as a directory
451 * and which one we choose depends on a combination of existing
452 * git index contents and the flags passed into the directory
455 * Case 1: If we *already* have entries in the index under that
456 * directory name, we always recurse into the directory to see
459 * Case 2: If we *already* have that directory name as a gitlink,
460 * we always continue to see it as a gitlink, regardless of whether
461 * there is an actual git directory there or not (it might not
462 * be checked out as a subproject!)
464 * Case 3: if we didn't have it in the index previously, we
465 * have a few sub-cases:
467 * (a) if "show_other_directories" is true, we show it as
468 * just a directory, unless "hide_empty_directories" is
469 * also true and the directory is empty, in which case
470 * we just ignore it entirely.
471 * (b) if it looks like a git directory, and we don't have
472 * 'no_gitlinks' set we treat it as a gitlink, and show it
474 * (c) otherwise, we recurse into it.
476 enum directory_treatment
{
479 recurse_into_directory
,
482 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
483 const char *dirname
, int len
,
484 const struct path_simplify
*simplify
)
486 /* The "len-1" is to strip the final '/' */
487 switch (directory_exists_in_index(dirname
, len
-1)) {
488 case index_directory
:
489 return recurse_into_directory
;
492 if (dir
->show_other_directories
)
493 return ignore_directory
;
494 return show_directory
;
496 case index_nonexistent
:
497 if (dir
->show_other_directories
)
499 if (!dir
->no_gitlinks
) {
500 unsigned char sha1
[20];
501 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
502 return show_directory
;
504 return recurse_into_directory
;
507 /* This is the "show_other_directories" case */
508 if (!dir
->hide_empty_directories
)
509 return show_directory
;
510 if (!read_directory_recursive(dir
, dirname
, dirname
, len
, 1, simplify
))
511 return ignore_directory
;
512 return show_directory
;
516 * This is an inexact early pruning of any recursive directory
517 * reading - if the path cannot possibly be in the pathspec,
518 * return true, and we'll skip it early.
520 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
524 const char *match
= simplify
->path
;
525 int len
= simplify
->len
;
531 if (!memcmp(path
, match
, len
))
540 static int in_pathspec(const char *path
, int len
, const struct path_simplify
*simplify
)
543 for (; simplify
->path
; simplify
++) {
544 if (len
== simplify
->len
545 && !memcmp(path
, simplify
->path
, len
))
552 static int get_dtype(struct dirent
*de
, const char *path
)
554 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
557 if (dtype
!= DT_UNKNOWN
)
559 if (lstat(path
, &st
))
561 if (S_ISREG(st
.st_mode
))
563 if (S_ISDIR(st
.st_mode
))
565 if (S_ISLNK(st
.st_mode
))
571 * Read a directory tree. We currently ignore anything but
572 * directories, regular files and symlinks. That's because git
573 * doesn't handle them at all yet. Maybe that will change some
576 * Also, we ignore the name ".git" (even if it is not a directory).
577 * That likely will not change.
579 static int read_directory_recursive(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
, int check_only
, const struct path_simplify
*simplify
)
581 DIR *fdir
= opendir(path
);
586 char fullname
[PATH_MAX
+ 1];
587 memcpy(fullname
, base
, baselen
);
589 while ((de
= readdir(fdir
)) != NULL
) {
593 if ((de
->d_name
[0] == '.') &&
594 (de
->d_name
[1] == 0 ||
595 !strcmp(de
->d_name
+ 1, ".") ||
596 !strcmp(de
->d_name
+ 1, "git")))
598 len
= strlen(de
->d_name
);
599 /* Ignore overly long pathnames! */
600 if (len
+ baselen
+ 8 > sizeof(fullname
))
602 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
603 if (simplify_away(fullname
, baselen
+ len
, simplify
))
607 exclude
= excluded(dir
, fullname
, &dtype
);
608 if (exclude
&& dir
->collect_ignored
609 && in_pathspec(fullname
, baselen
+ len
, simplify
))
610 dir_add_ignored(dir
, fullname
, baselen
+ len
);
613 * Excluded? If we don't explicitly want to show
614 * ignored files, ignore it
616 if (exclude
&& !dir
->show_ignored
)
619 if (dtype
== DT_UNKNOWN
)
620 dtype
= get_dtype(de
, fullname
);
623 * Do we want to see just the ignored files?
624 * We still need to recurse into directories,
625 * even if we don't ignore them, since the
626 * directory may contain files that we do..
628 if (!exclude
&& dir
->show_ignored
) {
637 memcpy(fullname
+ baselen
+ len
, "/", 2);
639 switch (treat_directory(dir
, fullname
, baselen
+ len
, simplify
)) {
641 if (exclude
!= dir
->show_ignored
)
644 case recurse_into_directory
:
645 contents
+= read_directory_recursive(dir
,
646 fullname
, fullname
, baselen
+ len
, 0, simplify
);
648 case ignore_directory
:
660 dir_add_name(dir
, fullname
, baselen
+ len
);
669 static int cmp_name(const void *p1
, const void *p2
)
671 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
672 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
674 return cache_name_compare(e1
->name
, e1
->len
,
679 * Return the length of the "simple" part of a path match limiter.
681 static int simple_length(const char *match
)
683 const char special
[256] = {
685 ['\\'] = 1, ['*'] = 1,
691 unsigned char c
= *match
++;
698 static struct path_simplify
*create_simplify(const char **pathspec
)
701 struct path_simplify
*simplify
= NULL
;
706 for (nr
= 0 ; ; nr
++) {
709 alloc
= alloc_nr(alloc
);
710 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
715 simplify
[nr
].path
= match
;
716 simplify
[nr
].len
= simple_length(match
);
718 simplify
[nr
].path
= NULL
;
719 simplify
[nr
].len
= 0;
723 static void free_simplify(struct path_simplify
*simplify
)
728 int read_directory(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
, const char **pathspec
)
730 struct path_simplify
*simplify
= create_simplify(pathspec
);
732 read_directory_recursive(dir
, path
, base
, baselen
, 0, simplify
);
733 free_simplify(simplify
);
734 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
735 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
739 int file_exists(const char *f
)
742 return lstat(f
, &sb
) == 0;
746 * get_relative_cwd() gets the prefix of the current working directory
747 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
749 * As a convenience, it also returns NULL if 'dir' is already NULL. The
750 * reason for this behaviour is that it is natural for functions returning
751 * directory names to return NULL to say "this directory does not exist"
752 * or "this directory is invalid". These cases are usually handled the
753 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
754 * returns NULL for both of them.
756 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
757 * unifies the handling of "outside work tree" with "no work tree at all".
759 char *get_relative_cwd(char *buffer
, int size
, const char *dir
)
765 if (!getcwd(buffer
, size
))
766 die("can't find the current directory: %s", strerror(errno
));
768 if (!is_absolute_path(dir
))
769 dir
= make_absolute_path(dir
);
771 while (*dir
&& *dir
== *cwd
) {
782 int is_inside_dir(const char *dir
)
784 char buffer
[PATH_MAX
];
785 return get_relative_cwd(buffer
, sizeof(buffer
), dir
) != NULL
;
788 int remove_dir_recursively(struct strbuf
*path
, int only_empty
)
790 DIR *dir
= opendir(path
->buf
);
792 int ret
= 0, original_len
= path
->len
, len
;
796 if (path
->buf
[original_len
- 1] != '/')
797 strbuf_addch(path
, '/');
800 while ((e
= readdir(dir
)) != NULL
) {
802 if ((e
->d_name
[0] == '.') &&
803 ((e
->d_name
[1] == 0) ||
804 ((e
->d_name
[1] == '.') && e
->d_name
[2] == 0)))
805 continue; /* "." and ".." */
807 strbuf_setlen(path
, len
);
808 strbuf_addstr(path
, e
->d_name
);
809 if (lstat(path
->buf
, &st
))
811 else if (S_ISDIR(st
.st_mode
)) {
812 if (!remove_dir_recursively(path
, only_empty
))
813 continue; /* happy */
814 } else if (!only_empty
&& !unlink(path
->buf
))
815 continue; /* happy, too */
817 /* path too long, stat fails, or non-directory still exists */
823 strbuf_setlen(path
, original_len
);
825 ret
= rmdir(path
->buf
);
829 void setup_standard_excludes(struct dir_struct
*dir
)
833 dir
->exclude_per_dir
= ".gitignore";
834 path
= git_path("info/exclude");
835 if (!access(path
, R_OK
))
836 add_excludes_from_file(dir
, path
);
837 if (excludes_file
&& !access(excludes_file
, R_OK
))
838 add_excludes_from_file(dir
, excludes_file
);
841 int remove_path(const char *name
)
845 if (unlink(name
) && errno
!= ENOENT
)
848 slash
= strrchr(name
, '/');
850 char *dirs
= xstrdup(name
);
851 slash
= dirs
+ (slash
- name
);
854 } while (rmdir(dirs
) && (slash
= strrchr(dirs
, '/')));