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))
56 * Does 'match' matches the given name?
59 * (1) the 'match' string is leading directory of 'name', or
60 * (2) the 'match' string is a wildcard and matches 'name', or
61 * (3) the 'match' string is exactly the same as 'name'.
63 * and the return value tells which case it was.
65 * It returns 0 when there is no match.
67 static int match_one(const char *match
, const char *name
, int namelen
)
71 /* If the match was just the prefix, we matched */
73 return MATCHED_RECURSIVELY
;
76 unsigned char c1
= *match
;
77 unsigned char c2
= *name
;
89 * If we don't match the matchstring exactly,
90 * we need to match by fnmatch
92 matchlen
= strlen(match
);
93 if (strncmp(match
, name
, matchlen
))
94 return !fnmatch(match
, name
, 0) ? MATCHED_FNMATCH
: 0;
96 if (namelen
== matchlen
)
97 return MATCHED_EXACTLY
;
98 if (match
[matchlen
-1] == '/' || name
[matchlen
] == '/')
99 return MATCHED_RECURSIVELY
;
104 * Given a name and a list of pathspecs, see if the name matches
105 * any of the pathspecs. The caller is also interested in seeing
106 * all pathspec matches some names it calls this function with
107 * (otherwise the user could have mistyped the unmatched pathspec),
108 * and a mark is left in seen[] array for pathspec element that
109 * actually matched anything.
111 int match_pathspec(const char **pathspec
, const char *name
, int namelen
, int prefix
, char *seen
)
119 for (retval
= 0; (match
= *pathspec
++) != NULL
; seen
++) {
121 if (retval
&& *seen
== MATCHED_EXACTLY
)
124 how
= match_one(match
, name
, namelen
);
135 static int no_wildcard(const char *string
)
137 return string
[strcspn(string
, "*?[{")] == '\0';
140 void add_exclude(const char *string
, const char *base
,
141 int baselen
, struct exclude_list
*which
)
148 if (*string
== '!') {
152 len
= strlen(string
);
153 if (len
&& string
[len
- 1] == '/') {
155 x
= xmalloc(sizeof(*x
) + len
);
157 memcpy(s
, string
, len
- 1);
161 flags
= EXC_FLAG_MUSTBEDIR
;
163 x
= xmalloc(sizeof(*x
));
166 x
->to_exclude
= to_exclude
;
167 x
->patternlen
= strlen(string
);
169 x
->baselen
= baselen
;
171 if (!strchr(string
, '/'))
172 x
->flags
|= EXC_FLAG_NODIR
;
173 if (no_wildcard(string
))
174 x
->flags
|= EXC_FLAG_NOWILDCARD
;
175 if (*string
== '*' && no_wildcard(string
+1))
176 x
->flags
|= EXC_FLAG_ENDSWITH
;
177 ALLOC_GROW(which
->excludes
, which
->nr
+ 1, which
->alloc
);
178 which
->excludes
[which
->nr
++] = x
;
181 static int add_excludes_from_file_1(const char *fname
,
185 struct exclude_list
*which
)
192 fd
= open(fname
, O_RDONLY
);
193 if (fd
< 0 || fstat(fd
, &st
) < 0)
195 size
= xsize_t(st
.st_size
);
200 buf
= xmalloc(size
+1);
201 if (read_in_full(fd
, buf
, size
) != size
)
212 for (i
= 0; i
< size
; i
++) {
213 if (buf
[i
] == '\n') {
214 if (entry
!= buf
+ i
&& entry
[0] != '#') {
215 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
216 add_exclude(entry
, base
, baselen
, which
);
229 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
231 if (add_excludes_from_file_1(fname
, "", 0, NULL
,
232 &dir
->exclude_list
[EXC_FILE
]) < 0)
233 die("cannot use %s as an exclude file", fname
);
236 static void prep_exclude(struct dir_struct
*dir
, const char *base
, int baselen
)
238 struct exclude_list
*el
;
239 struct exclude_stack
*stk
= NULL
;
242 if ((!dir
->exclude_per_dir
) ||
243 (baselen
+ strlen(dir
->exclude_per_dir
) >= PATH_MAX
))
244 return; /* too long a path -- ignore */
246 /* Pop the ones that are not the prefix of the path being checked. */
247 el
= &dir
->exclude_list
[EXC_DIRS
];
248 while ((stk
= dir
->exclude_stack
) != NULL
) {
249 if (stk
->baselen
<= baselen
&&
250 !strncmp(dir
->basebuf
, base
, stk
->baselen
))
252 dir
->exclude_stack
= stk
->prev
;
253 while (stk
->exclude_ix
< el
->nr
)
254 free(el
->excludes
[--el
->nr
]);
259 /* Read from the parent directories and push them down. */
260 current
= stk
? stk
->baselen
: -1;
261 while (current
< baselen
) {
262 struct exclude_stack
*stk
= xcalloc(1, sizeof(*stk
));
270 cp
= strchr(base
+ current
+ 1, '/');
272 die("oops in prep_exclude");
275 stk
->prev
= dir
->exclude_stack
;
276 stk
->baselen
= cp
- base
;
277 stk
->exclude_ix
= el
->nr
;
278 memcpy(dir
->basebuf
+ current
, base
+ current
,
279 stk
->baselen
- current
);
280 strcpy(dir
->basebuf
+ stk
->baselen
, dir
->exclude_per_dir
);
281 add_excludes_from_file_1(dir
->basebuf
,
282 dir
->basebuf
, stk
->baselen
,
284 dir
->exclude_stack
= stk
;
285 current
= stk
->baselen
;
287 dir
->basebuf
[baselen
] = '\0';
290 /* Scan the list and let the last match determines the fate.
291 * Return 1 for exclude, 0 for include and -1 for undecided.
293 static int excluded_1(const char *pathname
,
294 int pathlen
, const char *basename
, int *dtype
,
295 struct exclude_list
*el
)
300 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
301 struct exclude
*x
= el
->excludes
[i
];
302 const char *exclude
= x
->pattern
;
303 int to_exclude
= x
->to_exclude
;
305 if (x
->flags
& EXC_FLAG_MUSTBEDIR
) {
306 if (*dtype
== DT_UNKNOWN
)
307 *dtype
= get_dtype(NULL
, pathname
);
308 if (*dtype
!= DT_DIR
)
312 if (x
->flags
& EXC_FLAG_NODIR
) {
314 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
315 if (!strcmp(exclude
, basename
))
317 } else if (x
->flags
& EXC_FLAG_ENDSWITH
) {
318 if (x
->patternlen
- 1 <= pathlen
&&
319 !strcmp(exclude
+ 1, pathname
+ pathlen
- x
->patternlen
+ 1))
322 if (fnmatch(exclude
, basename
, 0) == 0)
327 /* match with FNM_PATHNAME:
328 * exclude has base (baselen long) implicitly
331 int baselen
= x
->baselen
;
335 if (pathlen
< baselen
||
336 (baselen
&& pathname
[baselen
-1] != '/') ||
337 strncmp(pathname
, x
->base
, baselen
))
340 if (x
->flags
& EXC_FLAG_NOWILDCARD
) {
341 if (!strcmp(exclude
, pathname
+ baselen
))
344 if (fnmatch(exclude
, pathname
+baselen
,
351 return -1; /* undecided */
354 int excluded(struct dir_struct
*dir
, const char *pathname
, int *dtype_p
)
356 int pathlen
= strlen(pathname
);
358 const char *basename
= strrchr(pathname
, '/');
359 basename
= (basename
) ? basename
+1 : pathname
;
361 prep_exclude(dir
, pathname
, basename
-pathname
);
362 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
363 switch (excluded_1(pathname
, pathlen
, basename
,
364 dtype_p
, &dir
->exclude_list
[st
])) {
374 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
)
376 struct dir_entry
*ent
;
378 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
380 memcpy(ent
->name
, pathname
, len
);
385 static struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
387 if (cache_name_exists(pathname
, len
, ignore_case
))
390 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
391 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
394 static struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
396 if (cache_name_pos(pathname
, len
) >= 0)
399 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
400 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
404 index_nonexistent
= 0,
410 * The index sorts alphabetically by entry name, which
411 * means that a gitlink sorts as '\0' at the end, while
412 * a directory (which is defined not as an entry, but as
413 * the files it contains) will sort with the '/' at the
416 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
418 int pos
= cache_name_pos(dirname
, len
);
421 while (pos
< active_nr
) {
422 struct cache_entry
*ce
= active_cache
[pos
++];
423 unsigned char endchar
;
425 if (strncmp(ce
->name
, dirname
, len
))
427 endchar
= ce
->name
[len
];
431 return index_directory
;
432 if (!endchar
&& S_ISGITLINK(ce
->ce_mode
))
435 return index_nonexistent
;
439 * When we find a directory when traversing the filesystem, we
440 * have three distinct cases:
443 * - see it as a directory
446 * and which one we choose depends on a combination of existing
447 * git index contents and the flags passed into the directory
450 * Case 1: If we *already* have entries in the index under that
451 * directory name, we always recurse into the directory to see
454 * Case 2: If we *already* have that directory name as a gitlink,
455 * we always continue to see it as a gitlink, regardless of whether
456 * there is an actual git directory there or not (it might not
457 * be checked out as a subproject!)
459 * Case 3: if we didn't have it in the index previously, we
460 * have a few sub-cases:
462 * (a) if "show_other_directories" is true, we show it as
463 * just a directory, unless "hide_empty_directories" is
464 * also true and the directory is empty, in which case
465 * we just ignore it entirely.
466 * (b) if it looks like a git directory, and we don't have
467 * 'no_gitlinks' set we treat it as a gitlink, and show it
469 * (c) otherwise, we recurse into it.
471 enum directory_treatment
{
474 recurse_into_directory
,
477 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
478 const char *dirname
, int len
,
479 const struct path_simplify
*simplify
)
481 /* The "len-1" is to strip the final '/' */
482 switch (directory_exists_in_index(dirname
, len
-1)) {
483 case index_directory
:
484 return recurse_into_directory
;
487 if (dir
->show_other_directories
)
488 return ignore_directory
;
489 return show_directory
;
491 case index_nonexistent
:
492 if (dir
->show_other_directories
)
494 if (!dir
->no_gitlinks
) {
495 unsigned char sha1
[20];
496 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
497 return show_directory
;
499 return recurse_into_directory
;
502 /* This is the "show_other_directories" case */
503 if (!dir
->hide_empty_directories
)
504 return show_directory
;
505 if (!read_directory_recursive(dir
, dirname
, dirname
, len
, 1, simplify
))
506 return ignore_directory
;
507 return show_directory
;
511 * This is an inexact early pruning of any recursive directory
512 * reading - if the path cannot possibly be in the pathspec,
513 * return true, and we'll skip it early.
515 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
519 const char *match
= simplify
->path
;
520 int len
= simplify
->len
;
526 if (!memcmp(path
, match
, len
))
535 static int in_pathspec(const char *path
, int len
, const struct path_simplify
*simplify
)
538 for (; simplify
->path
; simplify
++) {
539 if (len
== simplify
->len
540 && !memcmp(path
, simplify
->path
, len
))
547 static int get_dtype(struct dirent
*de
, const char *path
)
549 int dtype
= de
? DTYPE(de
) : DT_UNKNOWN
;
552 if (dtype
!= DT_UNKNOWN
)
554 if (lstat(path
, &st
))
556 if (S_ISREG(st
.st_mode
))
558 if (S_ISDIR(st
.st_mode
))
560 if (S_ISLNK(st
.st_mode
))
566 * Read a directory tree. We currently ignore anything but
567 * directories, regular files and symlinks. That's because git
568 * doesn't handle them at all yet. Maybe that will change some
571 * Also, we ignore the name ".git" (even if it is not a directory).
572 * That likely will not change.
574 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
)
576 DIR *fdir
= opendir(path
);
581 char fullname
[PATH_MAX
+ 1];
582 memcpy(fullname
, base
, baselen
);
584 while ((de
= readdir(fdir
)) != NULL
) {
588 if ((de
->d_name
[0] == '.') &&
589 (de
->d_name
[1] == 0 ||
590 !strcmp(de
->d_name
+ 1, ".") ||
591 !strcmp(de
->d_name
+ 1, "git")))
593 len
= strlen(de
->d_name
);
594 /* Ignore overly long pathnames! */
595 if (len
+ baselen
+ 8 > sizeof(fullname
))
597 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
598 if (simplify_away(fullname
, baselen
+ len
, simplify
))
602 exclude
= excluded(dir
, fullname
, &dtype
);
603 if (exclude
&& dir
->collect_ignored
604 && in_pathspec(fullname
, baselen
+ len
, simplify
))
605 dir_add_ignored(dir
, fullname
, baselen
+ len
);
608 * Excluded? If we don't explicitly want to show
609 * ignored files, ignore it
611 if (exclude
&& !dir
->show_ignored
)
614 if (dtype
== DT_UNKNOWN
)
615 dtype
= get_dtype(de
, fullname
);
618 * Do we want to see just the ignored files?
619 * We still need to recurse into directories,
620 * even if we don't ignore them, since the
621 * directory may contain files that we do..
623 if (!exclude
&& dir
->show_ignored
) {
632 memcpy(fullname
+ baselen
+ len
, "/", 2);
634 switch (treat_directory(dir
, fullname
, baselen
+ len
, simplify
)) {
636 if (exclude
!= dir
->show_ignored
)
639 case recurse_into_directory
:
640 contents
+= read_directory_recursive(dir
,
641 fullname
, fullname
, baselen
+ len
, 0, simplify
);
643 case ignore_directory
:
655 dir_add_name(dir
, fullname
, baselen
+ len
);
664 static int cmp_name(const void *p1
, const void *p2
)
666 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
667 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
669 return cache_name_compare(e1
->name
, e1
->len
,
674 * Return the length of the "simple" part of a path match limiter.
676 static int simple_length(const char *match
)
681 unsigned char c
= *match
++;
688 static struct path_simplify
*create_simplify(const char **pathspec
)
691 struct path_simplify
*simplify
= NULL
;
696 for (nr
= 0 ; ; nr
++) {
699 alloc
= alloc_nr(alloc
);
700 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
705 simplify
[nr
].path
= match
;
706 simplify
[nr
].len
= simple_length(match
);
708 simplify
[nr
].path
= NULL
;
709 simplify
[nr
].len
= 0;
713 static void free_simplify(struct path_simplify
*simplify
)
718 int read_directory(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
, const char **pathspec
)
720 struct path_simplify
*simplify
;
722 if (has_symlink_leading_path(strlen(path
), path
))
725 simplify
= create_simplify(pathspec
);
726 read_directory_recursive(dir
, path
, base
, baselen
, 0, simplify
);
727 free_simplify(simplify
);
728 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
729 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
733 int file_exists(const char *f
)
736 return lstat(f
, &sb
) == 0;
740 * get_relative_cwd() gets the prefix of the current working directory
741 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
743 * As a convenience, it also returns NULL if 'dir' is already NULL. The
744 * reason for this behaviour is that it is natural for functions returning
745 * directory names to return NULL to say "this directory does not exist"
746 * or "this directory is invalid". These cases are usually handled the
747 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
748 * returns NULL for both of them.
750 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
751 * unifies the handling of "outside work tree" with "no work tree at all".
753 char *get_relative_cwd(char *buffer
, int size
, const char *dir
)
759 if (!getcwd(buffer
, size
))
760 die("can't find the current directory: %s", strerror(errno
));
762 if (!is_absolute_path(dir
))
763 dir
= make_absolute_path(dir
);
765 while (*dir
&& *dir
== *cwd
) {
776 int is_inside_dir(const char *dir
)
778 char buffer
[PATH_MAX
];
779 return get_relative_cwd(buffer
, sizeof(buffer
), dir
) != NULL
;
782 int remove_dir_recursively(struct strbuf
*path
, int only_empty
)
784 DIR *dir
= opendir(path
->buf
);
786 int ret
= 0, original_len
= path
->len
, len
;
790 if (path
->buf
[original_len
- 1] != '/')
791 strbuf_addch(path
, '/');
794 while ((e
= readdir(dir
)) != NULL
) {
796 if ((e
->d_name
[0] == '.') &&
797 ((e
->d_name
[1] == 0) ||
798 ((e
->d_name
[1] == '.') && e
->d_name
[2] == 0)))
799 continue; /* "." and ".." */
801 strbuf_setlen(path
, len
);
802 strbuf_addstr(path
, e
->d_name
);
803 if (lstat(path
->buf
, &st
))
805 else if (S_ISDIR(st
.st_mode
)) {
806 if (!remove_dir_recursively(path
, only_empty
))
807 continue; /* happy */
808 } else if (!only_empty
&& !unlink(path
->buf
))
809 continue; /* happy, too */
811 /* path too long, stat fails, or non-directory still exists */
817 strbuf_setlen(path
, original_len
);
819 ret
= rmdir(path
->buf
);
823 void setup_standard_excludes(struct dir_struct
*dir
)
827 dir
->exclude_per_dir
= ".gitignore";
828 path
= git_path("info/exclude");
829 if (!access(path
, R_OK
))
830 add_excludes_from_file(dir
, path
);
831 if (excludes_file
&& !access(excludes_file
, R_OK
))
832 add_excludes_from_file(dir
, excludes_file
);
835 int remove_path(const char *name
)
839 if (unlink(name
) && errno
!= ENOENT
)
842 slash
= strrchr(name
, '/');
844 char *dirs
= xstrdup(name
);
845 slash
= dirs
+ (slash
- name
);
848 } while (rmdir(dirs
) && (slash
= strrchr(dirs
, '/')));