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
);
21 int common_prefix(const char **pathspec
)
23 const char *path
, *slash
, *next
;
30 slash
= strrchr(path
, '/');
34 prefix
= slash
- path
+ 1;
35 while ((next
= *++pathspec
) != NULL
) {
36 int len
= strlen(next
);
37 if (len
>= prefix
&& !memcmp(path
, next
, prefix
))
43 if (next
[--len
] != '/')
45 if (memcmp(path
, next
, len
+1))
55 * Does 'match' matches the given name?
58 * (1) the 'match' string is leading directory of 'name', or
59 * (2) the 'match' string is a wildcard and matches 'name', or
60 * (3) the 'match' string is exactly the same as 'name'.
62 * and the return value tells which case it was.
64 * It returns 0 when there is no match.
66 static int match_one(const char *match
, const char *name
, int namelen
)
70 /* If the match was just the prefix, we matched */
71 matchlen
= strlen(match
);
73 return MATCHED_RECURSIVELY
;
76 * If we don't match the matchstring exactly,
77 * we need to match by fnmatch
79 if (strncmp(match
, name
, matchlen
))
80 return !fnmatch(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
83 return MATCHED_EXACTLY
;
84 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
85 return MATCHED_RECURSIVELY
;
90 * Given a name and a list of pathspecs, see if the name matches
91 * any of the pathspecs. The caller is also interested in seeing
92 * all pathspec matches some names it calls this function with
93 * (otherwise the user could have mistyped the unmatched pathspec),
94 * and a mark is left in seen[] array for pathspec element that
95 * actually matched anything.
97 int match_pathspec(const char **pathspec
, const char *name
, int namelen
, int prefix
, char *seen
)
105 for (retval
= 0; (match
= *pathspec
++) != NULL
; seen
++) {
107 if (retval
&& *seen
== MATCHED_EXACTLY
)
110 how
= match_one(match
, name
, namelen
);
121 static int no_wildcard(const char *string
)
123 return string
[strcspn(string
, "*?[{")] == '\0';
126 void add_exclude(const char *string
, const char *base
,
127 int baselen
, struct exclude_list
*which
)
129 struct exclude
*x
= xmalloc(sizeof (*x
));
132 if (*string
== '!') {
137 x
->patternlen
= strlen(string
);
139 x
->baselen
= baselen
;
141 if (!strchr(string
, '/'))
142 x
->flags
|= EXC_FLAG_NODIR
;
143 if (no_wildcard(string
))
144 x
->flags
|= EXC_FLAG_NOWILDCARD
;
145 if (*string
== '*' && no_wildcard(string
+1))
146 x
->flags
|= EXC_FLAG_ENDSWITH
;
147 ALLOC_GROW(which
->excludes
, which
->nr
+ 1, which
->alloc
);
148 which
->excludes
[which
->nr
++] = x
;
151 static int add_excludes_from_file_1(const char *fname
,
155 struct exclude_list
*which
)
162 fd
= open(fname
, O_RDONLY
);
163 if (fd
< 0 || fstat(fd
, &st
) < 0)
165 size
= xsize_t(st
.st_size
);
170 buf
= xmalloc(size
+1);
171 if (read_in_full(fd
, buf
, size
) != size
)
179 for (i
= 0; i
< size
; i
++) {
180 if (buf
[i
] == '\n') {
181 if (entry
!= buf
+ i
&& entry
[0] != '#') {
182 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
183 add_exclude(entry
, base
, baselen
, which
);
196 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
198 if (add_excludes_from_file_1(fname
, "", 0, NULL
,
199 &dir
->exclude_list
[EXC_FILE
]) < 0)
200 die("cannot use %s as an exclude file", fname
);
203 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
205 struct exclude_list
*el
;
206 struct exclude_stack
*stk
= NULL
;
209 if ((!dir
->exclude_per_dir
) ||
210 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
211 return; /* too long a path -- ignore */
213 /* Pop the ones that are not the prefix of the path being checked. */
214 el
= &dir
->exclude_list
[EXC_DIRS
];
215 while ((stk
= dir
->exclude_stack
) != NULL
) {
216 if (stk
->baselen
<= baselen
&&
217 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
219 dir
->exclude_stack
= stk
->prev
;
220 while (stk
->exclude_ix
< el
->nr
)
221 free(el
->excludes
[--el
->nr
]);
226 /* Read from the parent directories and push them down. */
227 current
= stk
? stk
->baselen
: -1;
228 while (current
< baselen
) {
229 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
237 cp
= strchr(base
+ current
+ 1, '/');
239 die("oops in prep_exclude");
242 stk
->prev
= dir
->exclude_stack
;
243 stk
->baselen
= cp
- base
;
244 stk
->exclude_ix
= el
->nr
;
245 memcpy(dir
->basebuf
+ current
, base
+ current
,
246 stk
->baselen
- current
);
247 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
248 add_excludes_from_file_1(dir
->basebuf
,
249 dir
->basebuf
, stk
->baselen
,
251 dir
->exclude_stack
= stk
;
252 current
= stk
->baselen
;
254 dir
->basebuf
[baselen
] = '\0';
257 /* Scan the list and let the last match determines the fate.
258 * Return 1 for exclude, 0 for include and -1 for undecided.
260 static int excluded_1(const char *pathname
,
261 int pathlen
, const char *basename
,
262 struct exclude_list
*el
)
267 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
268 struct exclude
*x
= el
->excludes
[i
];
269 const char *exclude
= x
->pattern
;
270 int to_exclude
= x
->to_exclude
;
272 if (x
->flags
& EXC_FLAG_NODIR
) {
274 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
275 if (!strcmp(exclude
, basename
))
277 } else if (x
->flags
& EXC_FLAG_ENDSWITH
) {
278 if (x
->patternlen
- 1 <= pathlen
&&
279 !strcmp(exclude
+ 1, pathname
+ pathlen
- x
->patternlen
+ 1))
282 if (fnmatch(exclude
, basename
, 0) == 0)
287 /* match with FNM_PATHNAME:
288 * exclude has base (baselen long) implicitly
291 int baselen
= x
->baselen
;
295 if (pathlen
< baselen
||
296 (baselen
&& pathname
[baselen
-1] != '/') ||
297 strncmp(pathname
, x
->base
, baselen
))
300 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
301 if (!strcmp(exclude
, pathname
+ baselen
))
304 if (fnmatch(exclude
, pathname
+baselen
,
311 return -1; /* undecided */
314 int excluded(struct dir_struct
*dir
, const char *pathname
)
316 int pathlen
= strlen(pathname
);
318 const char *basename
= strrchr(pathname
, '/');
319 basename
= (basename
) ? basename
+1 : pathname
;
321 prep_exclude(dir
, pathname
, basename
-pathname
);
322 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
323 switch (excluded_1(pathname
, pathlen
, basename
, &dir
->exclude_list
[st
])) {
333 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
335 struct dir_entry
*ent
;
337 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
339 memcpy(ent
->name
, pathname
, len
);
344 struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
346 if (cache_name_pos(pathname
, len
) >= 0)
349 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
350 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
353 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
355 if (cache_name_pos(pathname
, len
) >= 0)
358 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
359 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
363 index_nonexistent
= 0,
369 * The index sorts alphabetically by entry name, which
370 * means that a gitlink sorts as '\0' at the end, while
371 * a directory (which is defined not as an entry, but as
372 * the files it contains) will sort with the '/' at the
375 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
377 int pos
= cache_name_pos(dirname
, len
);
380 while (pos
< active_nr
) {
381 struct cache_entry
*ce
= active_cache
[pos
++];
382 unsigned char endchar
;
384 if (strncmp(ce
->name
, dirname
, len
))
386 endchar
= ce
->name
[len
];
390 return index_directory
;
391 if (!endchar
&& S_ISGITLINK(ntohl(ce
->ce_mode
)))
394 return index_nonexistent
;
398 * When we find a directory when traversing the filesystem, we
399 * have three distinct cases:
402 * - see it as a directory
405 * and which one we choose depends on a combination of existing
406 * git index contents and the flags passed into the directory
409 * Case 1: If we *already* have entries in the index under that
410 * directory name, we always recurse into the directory to see
413 * Case 2: If we *already* have that directory name as a gitlink,
414 * we always continue to see it as a gitlink, regardless of whether
415 * there is an actual git directory there or not (it might not
416 * be checked out as a subproject!)
418 * Case 3: if we didn't have it in the index previously, we
419 * have a few sub-cases:
421 * (a) if "show_other_directories" is true, we show it as
422 * just a directory, unless "hide_empty_directories" is
423 * also true and the directory is empty, in which case
424 * we just ignore it entirely.
425 * (b) if it looks like a git directory, and we don't have
426 * 'no_gitlinks' set we treat it as a gitlink, and show it
428 * (c) otherwise, we recurse into it.
430 enum directory_treatment
{
433 recurse_into_directory
,
436 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
437 const char *dirname
, int len
,
438 const struct path_simplify
*simplify
)
440 /* The "len-1" is to strip the final '/' */
441 switch (directory_exists_in_index(dirname
, len
-1)) {
442 case index_directory
:
443 return recurse_into_directory
;
446 if (dir
->show_other_directories
)
447 return ignore_directory
;
448 return show_directory
;
450 case index_nonexistent
:
451 if (dir
->show_other_directories
)
453 if (!dir
->no_gitlinks
) {
454 unsigned char sha1
[20];
455 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
456 return show_directory
;
458 return recurse_into_directory
;
461 /* This is the "show_other_directories" case */
462 if (!dir
->hide_empty_directories
)
463 return show_directory
;
464 if (!read_directory_recursive(dir
, dirname
, dirname
, len
, 1, simplify
))
465 return ignore_directory
;
466 return show_directory
;
470 * This is an inexact early pruning of any recursive directory
471 * reading - if the path cannot possibly be in the pathspec,
472 * return true, and we'll skip it early.
474 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
478 const char *match
= simplify
->path
;
479 int len
= simplify
->len
;
485 if (!memcmp(path
, match
, len
))
494 static int in_pathspec(const char *path
, int len
, const struct path_simplify
*simplify
)
497 for (; simplify
->path
; simplify
++) {
498 if (len
== simplify
->len
499 && !memcmp(path
, simplify
->path
, len
))
506 static int get_dtype(struct dirent
*de
, const char *path
)
508 int dtype
= DTYPE(de
);
511 if (dtype
!= DT_UNKNOWN
)
513 if (lstat(path
, &st
))
515 if (S_ISREG(st
.st_mode
))
517 if (S_ISDIR(st
.st_mode
))
519 if (S_ISLNK(st
.st_mode
))
525 * Read a directory tree. We currently ignore anything but
526 * directories, regular files and symlinks. That's because git
527 * doesn't handle them at all yet. Maybe that will change some
530 * Also, we ignore the name ".git" (even if it is not a directory).
531 * That likely will not change.
533 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
)
535 DIR *fdir
= opendir(path
);
540 char fullname
[PATH_MAX
+ 1];
541 memcpy(fullname
, base
, baselen
);
543 while ((de
= readdir(fdir
)) != NULL
) {
547 if ((de
->d_name
[0] == '.') &&
548 (de
->d_name
[1] == 0 ||
549 !strcmp(de
->d_name
+ 1, ".") ||
550 !strcmp(de
->d_name
+ 1, "git")))
552 len
= strlen(de
->d_name
);
553 /* Ignore overly long pathnames! */
554 if (len
+ baselen
+ 8 > sizeof(fullname
))
556 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
557 if (simplify_away(fullname
, baselen
+ len
, simplify
))
560 exclude
= excluded(dir
, fullname
);
561 if (exclude
&& dir
->collect_ignored
562 && in_pathspec(fullname
, baselen
+ len
, simplify
))
563 dir_add_ignored(dir
, fullname
, baselen
+ len
);
566 * Excluded? If we don't explicitly want to show
567 * ignored files, ignore it
569 if (exclude
&& !dir
->show_ignored
)
572 dtype
= get_dtype(de
, fullname
);
575 * Do we want to see just the ignored files?
576 * We still need to recurse into directories,
577 * even if we don't ignore them, since the
578 * directory may contain files that we do..
580 if (!exclude
&& dir
->show_ignored
) {
589 memcpy(fullname
+ baselen
+ len
, "/", 2);
591 switch (treat_directory(dir
, fullname
, baselen
+ len
, simplify
)) {
593 if (exclude
!= dir
->show_ignored
)
596 case recurse_into_directory
:
597 contents
+= read_directory_recursive(dir
,
598 fullname
, fullname
, baselen
+ len
, 0, simplify
);
600 case ignore_directory
:
612 dir_add_name(dir
, fullname
, baselen
+ len
);
621 static int cmp_name(const void *p1
, const void *p2
)
623 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
624 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
626 return cache_name_compare(e1
->name
, e1
->len
,
631 * Return the length of the "simple" part of a path match limiter.
633 static int simple_length(const char *match
)
635 const char special
[256] = {
637 ['\\'] = 1, ['*'] = 1,
643 unsigned char c
= *match
++;
650 static struct path_simplify
*create_simplify(const char **pathspec
)
653 struct path_simplify
*simplify
= NULL
;
658 for (nr
= 0 ; ; nr
++) {
661 alloc
= alloc_nr(alloc
);
662 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
667 simplify
[nr
].path
= match
;
668 simplify
[nr
].len
= simple_length(match
);
670 simplify
[nr
].path
= NULL
;
671 simplify
[nr
].len
= 0;
675 static void free_simplify(struct path_simplify
*simplify
)
681 int read_directory(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
, const char **pathspec
)
683 struct path_simplify
*simplify
= create_simplify(pathspec
);
685 read_directory_recursive(dir
, path
, base
, baselen
, 0, simplify
);
686 free_simplify(simplify
);
687 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
688 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
692 int file_exists(const char *f
)
695 return lstat(f
, &sb
) == 0;
699 * get_relative_cwd() gets the prefix of the current working directory
700 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
702 * As a convenience, it also returns NULL if 'dir' is already NULL. The
703 * reason for this behaviour is that it is natural for functions returning
704 * directory names to return NULL to say "this directory does not exist"
705 * or "this directory is invalid". These cases are usually handled the
706 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
707 * returns NULL for both of them.
709 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
710 * unifies the handling of "outside work tree" with "no work tree at all".
712 char *get_relative_cwd(char *buffer
, int size
, const char *dir
)
718 if (!getcwd(buffer
, size
))
719 die("can't find the current directory: %s", strerror(errno
));
721 if (!is_absolute_path(dir
))
722 dir
= make_absolute_path(dir
);
724 while (*dir
&& *dir
== *cwd
) {
735 int is_inside_dir(const char *dir
)
737 char buffer
[PATH_MAX
];
738 return get_relative_cwd(buffer
, sizeof(buffer
), dir
) != NULL
;
741 int remove_dir_recursively(struct strbuf
*path
, int only_empty
)
743 DIR *dir
= opendir(path
->buf
);
745 int ret
= 0, original_len
= path
->len
, len
;
749 if (path
->buf
[original_len
- 1] != '/')
750 strbuf_addch(path
, '/');
753 while ((e
= readdir(dir
)) != NULL
) {
755 if ((e
->d_name
[0] == '.') &&
756 ((e
->d_name
[1] == 0) ||
757 ((e
->d_name
[1] == '.') && e
->d_name
[2] == 0)))
758 continue; /* "." and ".." */
760 strbuf_setlen(path
, len
);
761 strbuf_addstr(path
, e
->d_name
);
762 if (lstat(path
->buf
, &st
))
764 else if (S_ISDIR(st
.st_mode
)) {
765 if (!remove_dir_recursively(path
, only_empty
))
766 continue; /* happy */
767 } else if (!only_empty
&& !unlink(path
->buf
))
768 continue; /* happy, too */
770 /* path too long, stat fails, or non-directory still exists */
776 strbuf_setlen(path
, original_len
);
778 ret
= rmdir(path
->buf
);
782 void setup_standard_excludes(struct dir_struct
*dir
)
786 dir
->exclude_per_dir
= ".gitignore";
787 path
= git_path("info/exclude");
788 if (!access(path
, R_OK
))
789 add_excludes_from_file(dir
, path
);
790 if (excludes_file
&& !access(excludes_file
, R_OK
))
791 add_excludes_from_file(dir
, excludes_file
);