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 void add_exclude(const char *string
, const char *base
,
122 int baselen
, struct exclude_list
*which
)
124 struct exclude
*x
= xmalloc(sizeof (*x
));
128 x
->baselen
= baselen
;
129 if (which
->nr
== which
->alloc
) {
130 which
->alloc
= alloc_nr(which
->alloc
);
131 which
->excludes
= xrealloc(which
->excludes
,
132 which
->alloc
* sizeof(x
));
134 which
->excludes
[which
->nr
++] = x
;
137 static int add_excludes_from_file_1(const char *fname
,
140 struct exclude_list
*which
)
147 fd
= open(fname
, O_RDONLY
);
148 if (fd
< 0 || fstat(fd
, &st
) < 0)
150 size
= xsize_t(st
.st_size
);
155 buf
= xmalloc(size
+1);
156 if (read_in_full(fd
, buf
, size
) != size
)
162 for (i
= 0; i
< size
; i
++) {
163 if (buf
[i
] == '\n') {
164 if (entry
!= buf
+ i
&& entry
[0] != '#') {
165 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
166 add_exclude(entry
, base
, baselen
, which
);
179 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
181 if (add_excludes_from_file_1(fname
, "", 0,
182 &dir
->exclude_list
[EXC_FILE
]) < 0)
183 die("cannot use %s as an exclude file", fname
);
186 int push_exclude_per_directory(struct dir_struct
*dir
, const char *base
, int baselen
)
188 char exclude_file
[PATH_MAX
];
189 struct exclude_list
*el
= &dir
->exclude_list
[EXC_DIRS
];
190 int current_nr
= el
->nr
;
192 if (dir
->exclude_per_dir
) {
193 memcpy(exclude_file
, base
, baselen
);
194 strcpy(exclude_file
+ baselen
, dir
->exclude_per_dir
);
195 add_excludes_from_file_1(exclude_file
, base
, baselen
, el
);
200 void pop_exclude_per_directory(struct dir_struct
*dir
, int stk
)
202 struct exclude_list
*el
= &dir
->exclude_list
[EXC_DIRS
];
205 free(el
->excludes
[--el
->nr
]);
208 /* Scan the list and let the last match determines the fate.
209 * Return 1 for exclude, 0 for include and -1 for undecided.
211 static int excluded_1(const char *pathname
,
213 struct exclude_list
*el
)
218 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
219 struct exclude
*x
= el
->excludes
[i
];
220 const char *exclude
= x
->pattern
;
223 if (*exclude
== '!') {
228 if (!strchr(exclude
, '/')) {
230 const char *basename
= strrchr(pathname
, '/');
231 basename
= (basename
) ? basename
+1 : pathname
;
232 if (fnmatch(exclude
, basename
, 0) == 0)
236 /* match with FNM_PATHNAME:
237 * exclude has base (baselen long) implicitly
240 int baselen
= x
->baselen
;
244 if (pathlen
< baselen
||
245 (baselen
&& pathname
[baselen
-1] != '/') ||
246 strncmp(pathname
, x
->base
, baselen
))
249 if (fnmatch(exclude
, pathname
+baselen
,
255 return -1; /* undecided */
258 int excluded(struct dir_struct
*dir
, const char *pathname
)
260 int pathlen
= strlen(pathname
);
263 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
264 switch (excluded_1(pathname
, pathlen
, &dir
->exclude_list
[st
])) {
274 static struct dir_entry
*dir_entry_new(const char *pathname
, int len
) {
275 struct dir_entry
*ent
;
277 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
279 memcpy(ent
->name
, pathname
, len
);
284 struct dir_entry
*dir_add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
286 if (cache_name_pos(pathname
, len
) >= 0)
289 ALLOC_GROW(dir
->entries
, dir
->nr
+1, dir
->alloc
);
290 return dir
->entries
[dir
->nr
++] = dir_entry_new(pathname
, len
);
293 struct dir_entry
*dir_add_ignored(struct dir_struct
*dir
, const char *pathname
, int len
)
295 if (cache_name_pos(pathname
, len
) >= 0)
298 ALLOC_GROW(dir
->ignored
, dir
->ignored_nr
+1, dir
->ignored_alloc
);
299 return dir
->ignored
[dir
->ignored_nr
++] = dir_entry_new(pathname
, len
);
303 index_nonexistent
= 0,
309 * The index sorts alphabetically by entry name, which
310 * means that a gitlink sorts as '\0' at the end, while
311 * a directory (which is defined not as an entry, but as
312 * the files it contains) will sort with the '/' at the
315 static enum exist_status
directory_exists_in_index(const char *dirname
, int len
)
317 int pos
= cache_name_pos(dirname
, len
);
320 while (pos
< active_nr
) {
321 struct cache_entry
*ce
= active_cache
[pos
++];
322 unsigned char endchar
;
324 if (strncmp(ce
->name
, dirname
, len
))
326 endchar
= ce
->name
[len
];
330 return index_directory
;
331 if (!endchar
&& S_ISGITLINK(ntohl(ce
->ce_mode
)))
334 return index_nonexistent
;
338 * When we find a directory when traversing the filesystem, we
339 * have three distinct cases:
342 * - see it as a directory
345 * and which one we choose depends on a combination of existing
346 * git index contents and the flags passed into the directory
349 * Case 1: If we *already* have entries in the index under that
350 * directory name, we always recurse into the directory to see
353 * Case 2: If we *already* have that directory name as a gitlink,
354 * we always continue to see it as a gitlink, regardless of whether
355 * there is an actual git directory there or not (it might not
356 * be checked out as a subproject!)
358 * Case 3: if we didn't have it in the index previously, we
359 * have a few sub-cases:
361 * (a) if "show_other_directories" is true, we show it as
362 * just a directory, unless "hide_empty_directories" is
363 * also true and the directory is empty, in which case
364 * we just ignore it entirely.
365 * (b) if it looks like a git directory, and we don't have
366 * 'no_gitlinks' set we treat it as a gitlink, and show it
368 * (c) otherwise, we recurse into it.
370 enum directory_treatment
{
373 recurse_into_directory
,
376 static enum directory_treatment
treat_directory(struct dir_struct
*dir
,
377 const char *dirname
, int len
,
378 const struct path_simplify
*simplify
)
380 /* The "len-1" is to strip the final '/' */
381 switch (directory_exists_in_index(dirname
, len
-1)) {
382 case index_directory
:
383 return recurse_into_directory
;
386 if (dir
->show_other_directories
)
387 return ignore_directory
;
388 return show_directory
;
390 case index_nonexistent
:
391 if (dir
->show_other_directories
)
393 if (!dir
->no_gitlinks
) {
394 unsigned char sha1
[20];
395 if (resolve_gitlink_ref(dirname
, "HEAD", sha1
) == 0)
396 return show_directory
;
398 return recurse_into_directory
;
401 /* This is the "show_other_directories" case */
402 if (!dir
->hide_empty_directories
)
403 return show_directory
;
404 if (!read_directory_recursive(dir
, dirname
, dirname
, len
, 1, simplify
))
405 return ignore_directory
;
406 return show_directory
;
410 * This is an inexact early pruning of any recursive directory
411 * reading - if the path cannot possibly be in the pathspec,
412 * return true, and we'll skip it early.
414 static int simplify_away(const char *path
, int pathlen
, const struct path_simplify
*simplify
)
418 const char *match
= simplify
->path
;
419 int len
= simplify
->len
;
425 if (!memcmp(path
, match
, len
))
434 static int in_pathspec(const char *path
, int len
, const struct path_simplify
*simplify
)
437 for (; simplify
->path
; simplify
++) {
438 if (len
== simplify
->len
439 && !memcmp(path
, simplify
->path
, len
))
446 static int get_dtype(struct dirent
*de
, const char *path
)
448 int dtype
= DTYPE(de
);
451 if (dtype
!= DT_UNKNOWN
)
453 if (lstat(path
, &st
))
455 if (S_ISREG(st
.st_mode
))
457 if (S_ISDIR(st
.st_mode
))
459 if (S_ISLNK(st
.st_mode
))
465 * Read a directory tree. We currently ignore anything but
466 * directories, regular files and symlinks. That's because git
467 * doesn't handle them at all yet. Maybe that will change some
470 * Also, we ignore the name ".git" (even if it is not a directory).
471 * That likely will not change.
473 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
)
475 DIR *fdir
= opendir(path
);
481 char fullname
[PATH_MAX
+ 1];
482 memcpy(fullname
, base
, baselen
);
484 exclude_stk
= push_exclude_per_directory(dir
, base
, baselen
);
486 while ((de
= readdir(fdir
)) != NULL
) {
490 if ((de
->d_name
[0] == '.') &&
491 (de
->d_name
[1] == 0 ||
492 !strcmp(de
->d_name
+ 1, ".") ||
493 !strcmp(de
->d_name
+ 1, "git")))
495 len
= strlen(de
->d_name
);
496 /* Ignore overly long pathnames! */
497 if (len
+ baselen
+ 8 > sizeof(fullname
))
499 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
500 if (simplify_away(fullname
, baselen
+ len
, simplify
))
503 exclude
= excluded(dir
, fullname
);
504 if (exclude
&& dir
->collect_ignored
505 && in_pathspec(fullname
, baselen
+ len
, simplify
))
506 dir_add_ignored(dir
, fullname
, baselen
+ len
);
509 * Excluded? If we don't explicitly want to show
510 * ignored files, ignore it
512 if (exclude
&& !dir
->show_ignored
)
515 dtype
= get_dtype(de
, fullname
);
518 * Do we want to see just the ignored files?
519 * We still need to recurse into directories,
520 * even if we don't ignore them, since the
521 * directory may contain files that we do..
523 if (!exclude
&& dir
->show_ignored
) {
532 memcpy(fullname
+ baselen
+ len
, "/", 2);
534 switch (treat_directory(dir
, fullname
, baselen
+ len
, simplify
)) {
536 if (exclude
!= dir
->show_ignored
)
539 case recurse_into_directory
:
540 contents
+= read_directory_recursive(dir
,
541 fullname
, fullname
, baselen
+ len
, 0, simplify
);
543 case ignore_directory
:
555 dir_add_name(dir
, fullname
, baselen
+ len
);
560 pop_exclude_per_directory(dir
, exclude_stk
);
566 static int cmp_name(const void *p1
, const void *p2
)
568 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
569 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
571 return cache_name_compare(e1
->name
, e1
->len
,
576 * Return the length of the "simple" part of a path match limiter.
578 static int simple_length(const char *match
)
580 const char special
[256] = {
582 ['\\'] = 1, ['*'] = 1,
588 unsigned char c
= *match
++;
595 static struct path_simplify
*create_simplify(const char **pathspec
)
598 struct path_simplify
*simplify
= NULL
;
603 for (nr
= 0 ; ; nr
++) {
606 alloc
= alloc_nr(alloc
);
607 simplify
= xrealloc(simplify
, alloc
* sizeof(*simplify
));
612 simplify
[nr
].path
= match
;
613 simplify
[nr
].len
= simple_length(match
);
615 simplify
[nr
].path
= NULL
;
616 simplify
[nr
].len
= 0;
620 static void free_simplify(struct path_simplify
*simplify
)
626 int read_directory(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
, const char **pathspec
)
628 struct path_simplify
*simplify
= create_simplify(pathspec
);
631 * Make sure to do the per-directory exclude for all the
632 * directories leading up to our base.
635 if (dir
->exclude_per_dir
) {
636 char *p
, *pp
= xmalloc(baselen
+1);
637 memcpy(pp
, base
, baselen
+1);
642 push_exclude_per_directory(dir
, pp
, p
-pp
);
656 read_directory_recursive(dir
, path
, base
, baselen
, 0, simplify
);
657 free_simplify(simplify
);
658 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
659 qsort(dir
->ignored
, dir
->ignored_nr
, sizeof(struct dir_entry
*), cmp_name
);
664 file_exists(const char *f
)
667 return stat(f
, &sb
) == 0;
671 * get_relative_cwd() gets the prefix of the current working directory
672 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
674 * As a convenience, it also returns NULL if 'dir' is already NULL. The
675 * reason for this behaviour is that it is natural for functions returning
676 * directory names to return NULL to say "this directory does not exist"
677 * or "this directory is invalid". These cases are usually handled the
678 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
679 * returns NULL for both of them.
681 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
682 * unifies the handling of "outside work tree" with "no work tree at all".
684 char *get_relative_cwd(char *buffer
, int size
, const char *dir
)
690 if (!getcwd(buffer
, size
))
691 die("can't find the current directory: %s", strerror(errno
));
693 if (!is_absolute_path(dir
))
694 dir
= make_absolute_path(dir
);
696 while (*dir
&& *dir
== *cwd
) {
707 int is_inside_dir(const char *dir
)
709 char buffer
[PATH_MAX
];
710 return get_relative_cwd(buffer
, sizeof(buffer
), dir
) != NULL
;
713 void setup_standard_excludes(struct dir_struct
*dir
)
717 dir
->exclude_per_dir
= ".gitignore";
718 path
= git_path("info/exclude");
719 if (!access(path
, R_OK
))
720 add_excludes_from_file(dir
, path
);
721 if (excludes_file
&& !access(excludes_file
, R_OK
))
722 add_excludes_from_file(dir
, excludes_file
);