blob: ae1bc467ae909e6db151b055e212ded45fac34f6 [file] [log] [blame]
Linus Torvalds453ec4b2006-05-16 19:02:14 -07001#ifndef DIR_H
2#define DIR_H
3
Adam Spiers95a68342012-12-27 02:32:21 +00004/* See Documentation/technical/api-directory-listing.txt */
5
Junio C Hamanoeb417752012-06-01 11:28:00 -07006#include "strbuf.h"
7
Linus Torvalds453ec4b2006-05-16 19:02:14 -07008struct dir_entry {
Jeff Kinge96980e2007-06-12 23:42:14 +02009 unsigned int len;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070010 char name[FLEX_ARRAY]; /* more */
11};
12
Lars Knoll68492fc2007-10-28 21:27:13 +010013#define EXC_FLAG_NODIR 1
Lars Knoll68492fc2007-10-28 21:27:13 +010014#define EXC_FLAG_ENDSWITH 4
Junio C Hamanod6b8fc32008-01-31 01:17:48 -080015#define EXC_FLAG_MUSTBEDIR 8
Nguyễn Thái Ngọc Duy84460ee2012-10-15 13:24:38 +070016#define EXC_FLAG_NEGATIVE 16
Lars Knoll68492fc2007-10-28 21:27:13 +010017
Adam Spiers95a68342012-12-27 02:32:21 +000018/*
19 * Each .gitignore file will be parsed into patterns which are then
20 * appended to the relevant exclude_list (either EXC_DIRS or
21 * EXC_FILE). exclude_lists are also used to represent the list of
22 * --exclude values passed via CLI args (EXC_CMDL).
23 */
Linus Torvalds453ec4b2006-05-16 19:02:14 -070024struct exclude_list {
25 int nr;
26 int alloc;
27 struct exclude {
28 const char *pattern;
Lars Knoll68492fc2007-10-28 21:27:13 +010029 int patternlen;
Nguyễn Thái Ngọc Duyf9f6e2c2012-06-07 14:53:36 +070030 int nowildcardlen;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070031 const char *base;
32 int baselen;
Lars Knoll68492fc2007-10-28 21:27:13 +010033 int flags;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070034 } **excludes;
35};
36
Adam Spiers95a68342012-12-27 02:32:21 +000037/*
38 * The contents of the per-directory exclude files are lazily read on
39 * demand and then cached in memory, one per exclude_stack struct, in
40 * order to avoid opening and parsing each one every time that
41 * directory is traversed.
42 */
Junio C Hamano63d285c2007-11-29 02:17:44 -080043struct exclude_stack {
Adam Spiers95a68342012-12-27 02:32:21 +000044 struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
45 char *filebuf; /* remember pointer to per-directory exclude file contents so we can free() */
Junio C Hamano63d285c2007-11-29 02:17:44 -080046 int baselen;
47 int exclude_ix;
48};
49
Linus Torvalds453ec4b2006-05-16 19:02:14 -070050struct dir_struct {
51 int nr, alloc;
Jeff King2abd31b2007-06-11 09:39:50 -040052 int ignored_nr, ignored_alloc;
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +010053 enum {
54 DIR_SHOW_IGNORED = 1<<0,
55 DIR_SHOW_OTHER_DIRECTORIES = 1<<1,
56 DIR_HIDE_EMPTY_DIRECTORIES = 1<<2,
57 DIR_NO_GITLINKS = 1<<3,
58 DIR_COLLECT_IGNORED = 1<<4
59 } flags;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070060 struct dir_entry **entries;
Jeff King2abd31b2007-06-11 09:39:50 -040061 struct dir_entry **ignored;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070062
63 /* Exclude info */
64 const char *exclude_per_dir;
65 struct exclude_list exclude_list[3];
Junio C Hamano63d285c2007-11-29 02:17:44 -080066 /*
67 * We maintain three exclude pattern lists:
68 * EXC_CMDL lists patterns explicitly given on the command line.
69 * EXC_DIRS lists patterns obtained from per-directory ignore files.
70 * EXC_FILE lists patterns from fallback ignore files.
71 */
72#define EXC_CMDL 0
73#define EXC_DIRS 1
74#define EXC_FILE 2
75
Adam Spiers95a68342012-12-27 02:32:21 +000076 /*
77 * Temporary variables which are used during loading of the
78 * per-directory exclude lists.
79 *
80 * exclude_stack points to the top of the exclude_stack, and
81 * basebuf contains the full path to the current
82 * (sub)directory in the traversal.
83 */
Junio C Hamano63d285c2007-11-29 02:17:44 -080084 struct exclude_stack *exclude_stack;
85 char basebuf[PATH_MAX];
Linus Torvalds453ec4b2006-05-16 19:02:14 -070086};
87
Junio C Hamanoe813d502006-12-25 03:09:52 -080088#define MATCHED_RECURSIVELY 1
89#define MATCHED_FNMATCH 2
90#define MATCHED_EXACTLY 3
Clemens Buchacherf950eb92011-09-04 12:42:01 +020091extern char *common_prefix(const char **pathspec);
Linus Torvalds3c6a3702006-05-19 16:07:51 -070092extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
Nguyễn Thái Ngọc Duy61cf2822010-12-15 22:02:48 +070093extern int match_pathspec_depth(const struct pathspec *pathspec,
94 const char *name, int namelen,
95 int prefix, char *seen);
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +070096extern int within_depth(const char *name, int namelen, int depth, int max_depth);
Linus Torvalds3c6a3702006-05-19 16:07:51 -070097
Linus Torvalds1d8842d2009-05-14 13:22:36 -070098extern int fill_directory(struct dir_struct *dir, const char **pathspec);
Linus Torvaldsdba2e202009-07-08 19:24:39 -070099extern int read_directory(struct dir_struct *, const char *path, int len, const char **pathspec);
Junio C Hamanof8a9d422006-12-04 16:00:46 -0800100
Adam Spiers07958052012-12-27 02:32:24 +0000101extern int is_excluded_from_list(const char *pathname, int pathlen, const char *basename,
102 int *dtype, struct exclude_list *el);
Jens Lehmann108da0d2010-07-10 00:18:38 +0200103struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len);
Junio C Hamanoeb417752012-06-01 11:28:00 -0700104
105/*
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700106 * these implement the matching logic for dir.c:excluded_from_list and
107 * attr.c:path_matches()
108 */
109extern int match_basename(const char *, int,
110 const char *, int, int, int);
111extern int match_pathname(const char *, int,
112 const char *, int,
113 const char *, int, int, int);
114
115/*
Adam Spiers6d24e7a2012-12-27 02:32:25 +0000116 * The is_excluded() API is meant for callers that check each level of leading
117 * directory hierarchies with is_excluded() to avoid recursing into excluded
Junio C Hamanoeb417752012-06-01 11:28:00 -0700118 * directories. Callers that do not do so should use this API instead.
119 */
120struct path_exclude_check {
121 struct dir_struct *dir;
Adam Spiersa35341a2012-12-27 02:32:28 +0000122 struct exclude *exclude;
Junio C Hamanoeb417752012-06-01 11:28:00 -0700123 struct strbuf path;
124};
125extern void path_exclude_check_init(struct path_exclude_check *, struct dir_struct *);
126extern void path_exclude_check_clear(struct path_exclude_check *);
Adam Spiersa35341a2012-12-27 02:32:28 +0000127extern struct exclude *last_exclude_matching_path(struct path_exclude_check *, const char *,
128 int namelen, int *dtype);
Adam Spiers90130892012-12-27 02:32:23 +0000129extern int is_path_excluded(struct path_exclude_check *, const char *, int namelen, int *dtype);
Junio C Hamano782cd4c2012-06-05 21:17:52 -0700130
Junio C Hamanoeb417752012-06-01 11:28:00 -0700131
Nguyễn Thái Ngọc Duycb097532009-08-20 20:47:04 +0700132extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
Adam Spiers840fc332012-12-27 02:32:22 +0000133 char **buf_p, struct exclude_list *el, int check_index);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700134extern void add_excludes_from_file(struct dir_struct *, const char *fname);
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700135extern void parse_exclude_pattern(const char **string, int *patternlen, int *flags, int *nowildcardlen);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700136extern void add_exclude(const char *string, const char *base,
Adam Spiers840fc332012-12-27 02:32:22 +0000137 int baselen, struct exclude_list *el);
Adam Spiersf6198812012-12-27 02:32:29 +0000138extern void clear_exclude_list(struct exclude_list *el);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400139extern int file_exists(const char *);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700140
Johannes Schindeline6636742007-08-01 01:29:17 +0100141extern int is_inside_dir(const char *dir);
Nguyễn Thái Ngọc Duy9b125da2011-03-26 16:04:24 +0700142extern int dir_inside_of(const char *subdir, const char *dir);
Johannes Schindeline6636742007-08-01 01:29:17 +0100143
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300144static inline int is_dot_or_dotdot(const char *name)
145{
146 return (name[0] == '.' &&
147 (name[1] == '\0' ||
148 (name[1] == '.' && name[2] == '\0')));
149}
150
Alexander Potashev55892d22009-01-11 15:19:12 +0300151extern int is_empty_dir(const char *dir);
152
Junio C Hamano039bc642007-11-14 00:05:00 -0800153extern void setup_standard_excludes(struct dir_struct *dir);
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -0700154
155#define REMOVE_DIR_EMPTY_ONLY 01
156#define REMOVE_DIR_KEEP_NESTED_GIT 02
Junio C Hamanoc844a802012-03-15 15:58:54 +0100157#define REMOVE_DIR_KEEP_TOPLEVEL 04
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -0700158extern int remove_dir_recursively(struct strbuf *path, int flag);
Johannes Schindelin7155b722007-09-28 16:28:54 +0100159
Alex Riesen4a92d1b2008-09-27 00:56:46 +0200160/* tries to remove the path with empty directories along it, ignores ENOENT */
161extern int remove_path(const char *path);
162
Joshua Jensen8cf2a842010-10-03 09:56:41 +0000163extern int strcmp_icase(const char *a, const char *b);
164extern int strncmp_icase(const char *a, const char *b, size_t count);
165extern int fnmatch_icase(const char *pattern, const char *string, int flags);
166
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +0700167/*
168 * The prefix part of pattern must not contains wildcards.
169 */
170#define GFNM_PATHNAME 1 /* similar to FNM_PATHNAME */
Nguyễn Thái Ngọc Duy8c6abbc2012-11-24 11:33:50 +0700171#define GFNM_ONESTAR 2 /* there is only _one_ wildcard, a star */
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +0700172
173extern int git_fnmatch(const char *pattern, const char *string,
174 int flags, int prefix);
175
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700176#endif