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 if (which
->nr
== which
->alloc
) {
148 which
->alloc
= alloc_nr(which
->alloc
);
149 which
->excludes
= xrealloc(which
->excludes
,
150 which
->alloc
* sizeof(x
));
152 which
->excludes
[which
->nr
++] = x
;
155 static int add_excludes_from_file_1(const char *fname
,
158 struct exclude_list
*which
)
165 fd
= open(fname
, O_RDONLY
);
166 if (fd
< 0 || fstat(fd
, &st
) < 0)
168 size
= xsize_t(st
.st_size
);
173 buf
= xmalloc(size
+1);
174 if (read_in_full(fd
, buf
, size
) != size
)
180 for (i
= 0; i
< size
; i
++) {
181 if (buf
[i
] == '\n') {
182 if (entry
!= buf
+ i
&& entry
[0] != '#') {
183 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
184 add_exclude(entry
, base
, baselen
, which
);
197 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
199 if (add_excludes_from_file_1(fname
, "", 0,
200 &dir
->exclude_list
[EXC_FILE
]) < 0)
201 die("cannot use %s as an exclude file", fname
);
204 int push_exclude_per_directory(struct dir_struct
*dir
, const char *base
, int baselen
)
206 char exclude_file
[PATH_MAX
];
207 struct exclude_list
*el
= &dir
->exclude_list
[EXC_DIRS
];
208 int current_nr
= el
->nr
;
210 if (dir
->exclude_per_dir
) {
211 memcpy(exclude_file
, base
, baselen
);
212 strcpy(exclude_file
+ baselen
, dir
->exclude_per_dir
);
213 add_excludes_from_file_1(exclude_file
, base
, baselen
, el
);
218 void pop_exclude_per_directory(struct dir_struct
*dir
, int stk
)
220 struct exclude_list
*el
= &dir
->exclude_list
[EXC_DIRS
];
223 free(el
->excludes
[--el
->nr
]);
226 /* Scan the list and let the last match determines the fate.
227 * Return 1 for exclude, 0 for include and -1 for undecided.
229 static int excluded_1(const char *pathname
,
230 int pathlen
, const char *basename
,
231 struct exclude_list
*el
)
236 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
237 struct exclude
*x
= el
->excludes
[i
];
238 const char *exclude
= x
->pattern
;
239 int to_exclude
= x
->to_exclude
;
241 if (x
->flags
& EXC_FLAG_NODIR
) {
243 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
244 if (!strcmp(exclude
, basename
))
246 } else if (x
->flags
& EXC_FLAG_ENDSWITH
) {
247 if (x
->patternlen
- 1 <= pathlen
&&
248 !strcmp(exclude
+ 1, pathname
+ pathlen
- x
->patternlen
+ 1))
251 if (fnmatch(exclude
, basename
, 0) == 0)
256 /* match with FNM_PATHNAME:
257 * exclude has base (baselen long) implicitly
260 int baselen
= x
->baselen
;
264 if (pathlen
< baselen
||
265 (baselen
&& pathname
[baselen
-1] != '/') ||
266 strncmp(pathname
, x
->base
, baselen
))
269 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
270 if (!strcmp(exclude
, pathname
+ baselen
))
273 if (fnmatch(exclude
, pathname
+baselen
,
280 return -1; /* undecided */
283 int excluded(struct dir_struct
*dir
, const char *pathname
)
285 int pathlen
= strlen(pathname
);
287 const char *basename
= strrchr(pathname
, '/');
288 basename
= (basename
) ? basename
+1 : pathname
;
290 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
291 switch (excluded_1(pathname
, pathlen
, basename
, &dir
->exclude_list
[st
])) {
301 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
303 struct dir_entry
*ent
;
305 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
307 memcpy(ent
->name
, pathname
, len
);
312 struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
314 if (cache_name_pos(pathname
, len
) >= 0)
317 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
318 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
321 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
323 if (cache_name_pos(pathname
, len
) >= 0)
326 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
327 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
331 index_nonexistent
= 0,
337 * The index sorts alphabetically by entry name, which
338 * means that a gitlink sorts as '\0' at the end, while
339 * a directory (which is defined not as an entry, but as
340 * the files it contains) will sort with the '/' at the
343 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
345 int pos
= cache_name_pos(dirname
, len
);
348 while (pos
< active_nr
) {
349 struct cache_entry
*ce
= active_cache
[pos
++];
350 unsigned char endchar
;
352 if (strncmp(ce
->name
, dirname
, len
))
354 endchar
= ce
->name
[len
];
358 return index_directory
;
359 if (!endchar
&& S_ISGITLINK(ntohl(ce
->ce_mode
)))
362 return index_nonexistent
;
366 * When we find a directory when traversing the filesystem, we
367 * have three distinct cases:
370 * - see it as a directory
373 * and which one we choose depends on a combination of existing
374 * git index contents and the flags passed into the directory
377 * Case 1: If we *already* have entries in the index under that
378 * directory name, we always recurse into the directory to see
381 * Case 2: If we *already* have that directory name as a gitlink,
382 * we always continue to see it as a gitlink, regardless of whether
383 * there is an actual git directory there or not (it might not
384 * be checked out as a subproject!)
386 * Case 3: if we didn't have it in the index previously, we
387 * have a few sub-cases:
389 * (a) if "show_other_directories" is true, we show it as
390 * just a directory, unless "hide_empty_directories" is
391 * also true and the directory is empty, in which case
392 * we just ignore it entirely.
393 * (b) if it looks like a git directory, and we don't have
394 * 'no_gitlinks' set we treat it as a gitlink, and show it
396 * (c) otherwise, we recurse into it.
398 enum directory_treatment
{
401 recurse_into_directory
,
404 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
405 const char *dirname
, int len
,
406 const struct path_simplify
*simplify
)
408 /* The "len-1" is to strip the final '/' */
409 switch (directory_exists_in_index(dirname
, len
-1)) {
410 case index_directory
:
411 return recurse_into_directory
;
414 if (dir
->show_other_directories
)
415 return ignore_directory
;
416 return show_directory
;
418 case index_nonexistent
:
419 if (dir
->show_other_directories
)
421 if (!dir
->no_gitlinks
) {
422 unsigned char sha1
[20];
423 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
424 return show_directory
;
426 return recurse_into_directory
;
429 /* This is the "show_other_directories" case */
430 if (!dir
->hide_empty_directories
)
431 return show_directory
;
432 if (!read_directory_recursive(dir
, dirname
, dirname
, len
, 1, simplify
))
433 return ignore_directory
;
434 return show_directory
;
438 * This is an inexact early pruning of any recursive directory
439 * reading - if the path cannot possibly be in the pathspec,
440 * return true, and we'll skip it early.
442 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
446 const char *match
= simplify
->path
;
447 int len
= simplify
->len
;
453 if (!memcmp(path
, match
, len
))
462 static int in_pathspec(const char *path
, int len
, const struct path_simplify
*simplify
)
465 for (; simplify
->path
; simplify
++) {
466 if (len
== simplify
->len
467 && !memcmp(path
, simplify
->path
, len
))
474 static int get_dtype(struct dirent
*de
, const char *path
)
476 int dtype
= DTYPE(de
);
479 if (dtype
!= DT_UNKNOWN
)
481 if (lstat(path
, &st
))
483 if (S_ISREG(st
.st_mode
))
485 if (S_ISDIR(st
.st_mode
))
487 if (S_ISLNK(st
.st_mode
))
493 * Read a directory tree. We currently ignore anything but
494 * directories, regular files and symlinks. That's because git
495 * doesn't handle them at all yet. Maybe that will change some
498 * Also, we ignore the name ".git" (even if it is not a directory).
499 * That likely will not change.
501 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
)
503 DIR *fdir
= opendir(path
);
509 char fullname
[PATH_MAX
+ 1];
510 memcpy(fullname
, base
, baselen
);
512 exclude_stk
= push_exclude_per_directory(dir
, base
, baselen
);
514 while ((de
= readdir(fdir
)) != NULL
) {
518 if ((de
->d_name
[0] == '.') &&
519 (de
->d_name
[1] == 0 ||
520 !strcmp(de
->d_name
+ 1, ".") ||
521 !strcmp(de
->d_name
+ 1, "git")))
523 len
= strlen(de
->d_name
);
524 /* Ignore overly long pathnames! */
525 if (len
+ baselen
+ 8 > sizeof(fullname
))
527 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
528 if (simplify_away(fullname
, baselen
+ len
, simplify
))
531 exclude
= excluded(dir
, fullname
);
532 if (exclude
&& dir
->collect_ignored
533 && in_pathspec(fullname
, baselen
+ len
, simplify
))
534 dir_add_ignored(dir
, fullname
, baselen
+ len
);
537 * Excluded? If we don't explicitly want to show
538 * ignored files, ignore it
540 if (exclude
&& !dir
->show_ignored
)
543 dtype
= get_dtype(de
, fullname
);
546 * Do we want to see just the ignored files?
547 * We still need to recurse into directories,
548 * even if we don't ignore them, since the
549 * directory may contain files that we do..
551 if (!exclude
&& dir
->show_ignored
) {
560 memcpy(fullname
+ baselen
+ len
, "/", 2);
562 switch (treat_directory(dir
, fullname
, baselen
+ len
, simplify
)) {
564 if (exclude
!= dir
->show_ignored
)
567 case recurse_into_directory
:
568 contents
+= read_directory_recursive(dir
,
569 fullname
, fullname
, baselen
+ len
, 0, simplify
);
571 case ignore_directory
:
583 dir_add_name(dir
, fullname
, baselen
+ len
);
588 pop_exclude_per_directory(dir
, exclude_stk
);
594 static int cmp_name(const void *p1
, const void *p2
)
596 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
597 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
599 return cache_name_compare(e1
->name
, e1
->len
,
604 * Return the length of the "simple" part of a path match limiter.
606 static int simple_length(const char *match
)
608 const char special
[256] = {
610 ['\\'] = 1, ['*'] = 1,
616 unsigned char c
= *match
++;
623 static struct path_simplify
*create_simplify(const char **pathspec
)
626 struct path_simplify
*simplify
= NULL
;
631 for (nr
= 0 ; ; nr
++) {
634 alloc
= alloc_nr(alloc
);
635 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
640 simplify
[nr
].path
= match
;
641 simplify
[nr
].len
= simple_length(match
);
643 simplify
[nr
].path
= NULL
;
644 simplify
[nr
].len
= 0;
648 static void free_simplify(struct path_simplify
*simplify
)
654 int read_directory(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
, const char **pathspec
)
656 struct path_simplify
*simplify
= create_simplify(pathspec
);
660 * Make sure to do the per-directory exclude for all the
661 * directories leading up to our base.
664 if (dir
->exclude_per_dir
) {
666 pp
= xmalloc(baselen
+1);
667 memcpy(pp
, base
, baselen
+1);
672 push_exclude_per_directory(dir
, pp
, p
-pp
);
685 read_directory_recursive(dir
, path
, base
, baselen
, 0, simplify
);
686 free_simplify(simplify
);
688 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
689 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
693 int file_exists(const char *f
)
696 return lstat(f
, &sb
) == 0;
700 * get_relative_cwd() gets the prefix of the current working directory
701 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
703 * As a convenience, it also returns NULL if 'dir' is already NULL. The
704 * reason for this behaviour is that it is natural for functions returning
705 * directory names to return NULL to say "this directory does not exist"
706 * or "this directory is invalid". These cases are usually handled the
707 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
708 * returns NULL for both of them.
710 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
711 * unifies the handling of "outside work tree" with "no work tree at all".
713 char *get_relative_cwd(char *buffer
, int size
, const char *dir
)
719 if (!getcwd(buffer
, size
))
720 die("can't find the current directory: %s", strerror(errno
));
722 if (!is_absolute_path(dir
))
723 dir
= make_absolute_path(dir
);
725 while (*dir
&& *dir
== *cwd
) {
736 int is_inside_dir(const char *dir
)
738 char buffer
[PATH_MAX
];
739 return get_relative_cwd(buffer
, sizeof(buffer
), dir
) != NULL
;
742 int remove_dir_recursively(struct strbuf
*path
, int only_empty
)
744 DIR *dir
= opendir(path
->buf
);
746 int ret
= 0, original_len
= path
->len
, len
;
750 if (path
->buf
[original_len
- 1] != '/')
751 strbuf_addch(path
, '/');
754 while ((e
= readdir(dir
)) != NULL
) {
756 if ((e
->d_name
[0] == '.') &&
757 ((e
->d_name
[1] == 0) ||
758 ((e
->d_name
[1] == '.') && e
->d_name
[2] == 0)))
759 continue; /* "." and ".." */
761 strbuf_setlen(path
, len
);
762 strbuf_addstr(path
, e
->d_name
);
763 if (lstat(path
->buf
, &st
))
765 else if (S_ISDIR(st
.st_mode
)) {
766 if (!remove_dir_recursively(path
, only_empty
))
767 continue; /* happy */
768 } else if (!only_empty
&& !unlink(path
->buf
))
769 continue; /* happy, too */
771 /* path too long, stat fails, or non-directory still exists */
777 strbuf_setlen(path
, original_len
);
779 ret
= rmdir(path
->buf
);
783 void setup_standard_excludes(struct dir_struct
*dir
)
787 dir
->exclude_per_dir
= ".gitignore";
788 path
= git_path("info/exclude");
789 if (!access(path
, R_OK
))
790 add_excludes_from_file(dir
, path
);
791 if (excludes_file
&& !access(excludes_file
, R_OK
))
792 add_excludes_from_file(dir
, excludes_file
);