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
11 int common_prefix(const char **pathspec
)
13 const char *path
, *slash
, *next
;
20 slash
= strrchr(path
, '/');
24 prefix
= slash
- path
+ 1;
25 while ((next
= *++pathspec
) != NULL
) {
26 int len
= strlen(next
);
27 if (len
>= prefix
&& !memcmp(path
, next
, len
))
32 if (next
[--len
] != '/')
34 if (memcmp(path
, next
, len
+1))
43 static int match_one(const char *match
, const char *name
, int namelen
)
47 /* If the match was just the prefix, we matched */
48 matchlen
= strlen(match
);
53 * If we don't match the matchstring exactly,
54 * we need to match by fnmatch
56 if (strncmp(match
, name
, matchlen
))
57 return !fnmatch(match
, name
, 0);
60 * If we did match the string exactly, we still
61 * need to make sure that it happened on a path
62 * component boundary (ie either the last character
63 * of the match was '/', or the next character of
64 * the name was '/' or the terminating NUL.
66 return match
[matchlen
-1] == '/' ||
67 name
[matchlen
] == '/' ||
71 int match_pathspec(const char **pathspec
, const char *name
, int namelen
, int prefix
, char *seen
)
79 for (retval
= 0; (match
= *pathspec
++) != NULL
; seen
++) {
83 if (match_one(match
, name
, namelen
)) {
91 void add_exclude(const char *string
, const char *base
,
92 int baselen
, struct exclude_list
*which
)
94 struct exclude
*x
= xmalloc(sizeof (*x
));
99 if (which
->nr
== which
->alloc
) {
100 which
->alloc
= alloc_nr(which
->alloc
);
101 which
->excludes
= xrealloc(which
->excludes
,
102 which
->alloc
* sizeof(x
));
104 which
->excludes
[which
->nr
++] = x
;
107 static int add_excludes_from_file_1(const char *fname
,
110 struct exclude_list
*which
)
117 fd
= open(fname
, O_RDONLY
);
118 if (fd
< 0 || fstat(fd
, &st
) < 0)
125 buf
= xmalloc(size
+1);
126 if (read(fd
, buf
, size
) != size
)
132 for (i
= 0; i
< size
; i
++) {
133 if (buf
[i
] == '\n') {
134 if (entry
!= buf
+ i
&& entry
[0] != '#') {
135 buf
[i
- (i
&& buf
[i
-1] == '\r')] = 0;
136 add_exclude(entry
, base
, baselen
, which
);
149 void add_excludes_from_file(struct dir_struct
*dir
, const char *fname
)
151 if (add_excludes_from_file_1(fname
, "", 0,
152 &dir
->exclude_list
[EXC_FILE
]) < 0)
153 die("cannot use %s as an exclude file", fname
);
156 int push_exclude_per_directory(struct dir_struct
*dir
, const char *base
, int baselen
)
158 char exclude_file
[PATH_MAX
];
159 struct exclude_list
*el
= &dir
->exclude_list
[EXC_DIRS
];
160 int current_nr
= el
->nr
;
162 if (dir
->exclude_per_dir
) {
163 memcpy(exclude_file
, base
, baselen
);
164 strcpy(exclude_file
+ baselen
, dir
->exclude_per_dir
);
165 add_excludes_from_file_1(exclude_file
, base
, baselen
, el
);
170 void pop_exclude_per_directory(struct dir_struct
*dir
, int stk
)
172 struct exclude_list
*el
= &dir
->exclude_list
[EXC_DIRS
];
175 free(el
->excludes
[--el
->nr
]);
178 /* Scan the list and let the last match determines the fate.
179 * Return 1 for exclude, 0 for include and -1 for undecided.
181 static int excluded_1(const char *pathname
,
183 struct exclude_list
*el
)
188 for (i
= el
->nr
- 1; 0 <= i
; i
--) {
189 struct exclude
*x
= el
->excludes
[i
];
190 const char *exclude
= x
->pattern
;
193 if (*exclude
== '!') {
198 if (!strchr(exclude
, '/')) {
200 const char *basename
= strrchr(pathname
, '/');
201 basename
= (basename
) ? basename
+1 : pathname
;
202 if (fnmatch(exclude
, basename
, 0) == 0)
206 /* match with FNM_PATHNAME:
207 * exclude has base (baselen long) implicitly
210 int baselen
= x
->baselen
;
214 if (pathlen
< baselen
||
215 (baselen
&& pathname
[baselen
-1] != '/') ||
216 strncmp(pathname
, x
->base
, baselen
))
219 if (fnmatch(exclude
, pathname
+baselen
,
225 return -1; /* undecided */
228 int excluded(struct dir_struct
*dir
, const char *pathname
)
230 int pathlen
= strlen(pathname
);
233 for (st
= EXC_CMDL
; st
<= EXC_FILE
; st
++) {
234 switch (excluded_1(pathname
, pathlen
, &dir
->exclude_list
[st
])) {
244 static void add_name(struct dir_struct
*dir
, const char *pathname
, int len
)
246 struct dir_entry
*ent
;
248 if (cache_name_pos(pathname
, len
) >= 0)
251 if (dir
->nr
== dir
->alloc
) {
252 int alloc
= alloc_nr(dir
->alloc
);
254 dir
->entries
= xrealloc(dir
->entries
, alloc
*sizeof(ent
));
256 ent
= xmalloc(sizeof(*ent
) + len
+ 1);
258 memcpy(ent
->name
, pathname
, len
);
260 dir
->entries
[dir
->nr
++] = ent
;
263 static int dir_exists(const char *dirname
, int len
)
265 int pos
= cache_name_pos(dirname
, len
);
269 if (pos
>= active_nr
) /* can't */
271 return !strncmp(active_cache
[pos
]->name
, dirname
, len
);
275 * Read a directory tree. We currently ignore anything but
276 * directories, regular files and symlinks. That's because git
277 * doesn't handle them at all yet. Maybe that will change some
280 * Also, we ignore the name ".git" (even if it is not a directory).
281 * That likely will not change.
283 static int read_directory_recursive(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
, int check_only
)
285 DIR *fdir
= opendir(path
);
291 char fullname
[PATH_MAX
+ 1];
292 memcpy(fullname
, base
, baselen
);
294 exclude_stk
= push_exclude_per_directory(dir
, base
, baselen
);
296 while ((de
= readdir(fdir
)) != NULL
) {
299 if ((de
->d_name
[0] == '.') &&
300 (de
->d_name
[1] == 0 ||
301 !strcmp(de
->d_name
+ 1, ".") ||
302 !strcmp(de
->d_name
+ 1, "git")))
304 len
= strlen(de
->d_name
);
305 memcpy(fullname
+ baselen
, de
->d_name
, len
+1);
306 if (excluded(dir
, fullname
) != dir
->show_ignored
) {
307 if (!dir
->show_ignored
|| DTYPE(de
) != DT_DIR
) {
317 if (lstat(fullname
, &st
))
319 if (S_ISREG(st
.st_mode
) || S_ISLNK(st
.st_mode
))
321 if (!S_ISDIR(st
.st_mode
))
325 memcpy(fullname
+ baselen
+ len
, "/", 2);
327 if (dir
->show_other_directories
&&
328 !dir_exists(fullname
, baselen
+ len
)) {
329 if (dir
->hide_empty_directories
&&
330 !read_directory_recursive(dir
,
337 contents
+= read_directory_recursive(dir
,
338 fullname
, fullname
, baselen
+ len
, 0);
348 add_name(dir
, fullname
, baselen
+ len
);
353 pop_exclude_per_directory(dir
, exclude_stk
);
359 static int cmp_name(const void *p1
, const void *p2
)
361 const struct dir_entry
*e1
= *(const struct dir_entry
**)p1
;
362 const struct dir_entry
*e2
= *(const struct dir_entry
**)p2
;
364 return cache_name_compare(e1
->name
, e1
->len
,
368 int read_directory(struct dir_struct
*dir
, const char *path
, const char *base
, int baselen
)
371 * Make sure to do the per-directory exclude for all the
372 * directories leading up to our base.
375 if (dir
->exclude_per_dir
) {
376 char *p
, *pp
= xmalloc(baselen
+1);
377 memcpy(pp
, base
, baselen
+1);
382 push_exclude_per_directory(dir
, pp
, p
-pp
);
396 read_directory_recursive(dir
, path
, base
, baselen
, 0);
397 qsort(dir
->entries
, dir
->nr
, sizeof(struct dir_entry
*), cmp_name
);
402 file_exists(const char *f
)
405 return stat(f
, &sb
) == 0;