blob: c31ed9af785abbb6af6b2959b43fdc0fe0de051c [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/*
Adam Spiersc082df22013-01-06 16:58:03 +000019 * Each excludes file will be parsed into a fresh exclude_list which
20 * is appended to the relevant exclude_list_group (either EXC_DIRS or
21 * EXC_FILE). An exclude_list within the EXC_CMDL exclude_list_group
22 * can also be used to represent the list of --exclude values passed
23 * via CLI args.
Adam Spiers95a68342012-12-27 02:32:21 +000024 */
Linus Torvalds453ec4b2006-05-16 19:02:14 -070025struct exclude_list {
26 int nr;
27 int alloc;
Adam Spiersc04318e2013-01-06 16:58:04 +000028
Adam Spiersc082df22013-01-06 16:58:03 +000029 /* remember pointer to exclude file contents so we can free() */
30 char *filebuf;
31
Adam Spiersc04318e2013-01-06 16:58:04 +000032 /* origin of list, e.g. path to filename, or descriptive string */
33 const char *src;
34
Linus Torvalds453ec4b2006-05-16 19:02:14 -070035 struct exclude {
Adam Spiersc04318e2013-01-06 16:58:04 +000036 /*
37 * This allows callers of last_exclude_matching() etc.
38 * to determine the origin of the matching pattern.
39 */
40 struct exclude_list *el;
41
Linus Torvalds453ec4b2006-05-16 19:02:14 -070042 const char *pattern;
Lars Knoll68492fc2007-10-28 21:27:13 +010043 int patternlen;
Nguyễn Thái Ngọc Duyf9f6e2c2012-06-07 14:53:36 +070044 int nowildcardlen;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070045 const char *base;
46 int baselen;
Lars Knoll68492fc2007-10-28 21:27:13 +010047 int flags;
Adam Spiersc04318e2013-01-06 16:58:04 +000048
49 /*
50 * Counting starts from 1 for line numbers in ignore files,
51 * and from -1 decrementing for patterns from CLI args.
52 */
53 int srcpos;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070054 } **excludes;
55};
56
Adam Spiers95a68342012-12-27 02:32:21 +000057/*
58 * The contents of the per-directory exclude files are lazily read on
59 * demand and then cached in memory, one per exclude_stack struct, in
60 * order to avoid opening and parsing each one every time that
61 * directory is traversed.
62 */
Junio C Hamano63d285c2007-11-29 02:17:44 -080063struct exclude_stack {
Adam Spiers95a68342012-12-27 02:32:21 +000064 struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
Junio C Hamano63d285c2007-11-29 02:17:44 -080065 int baselen;
Adam Spiersc082df22013-01-06 16:58:03 +000066 int exclude_ix; /* index of exclude_list within EXC_DIRS exclude_list_group */
67};
68
69struct exclude_list_group {
70 int nr, alloc;
71 struct exclude_list *el;
Junio C Hamano63d285c2007-11-29 02:17:44 -080072};
73
Linus Torvalds453ec4b2006-05-16 19:02:14 -070074struct dir_struct {
75 int nr, alloc;
Jeff King2abd31b2007-06-11 09:39:50 -040076 int ignored_nr, ignored_alloc;
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +010077 enum {
78 DIR_SHOW_IGNORED = 1<<0,
79 DIR_SHOW_OTHER_DIRECTORIES = 1<<1,
80 DIR_HIDE_EMPTY_DIRECTORIES = 1<<2,
81 DIR_NO_GITLINKS = 1<<3,
Karsten Blees0aaf62b2013-04-15 21:15:03 +020082 DIR_COLLECT_IGNORED = 1<<4,
Junio C Hamano2eac2a42013-08-15 12:13:46 -070083 DIR_SHOW_IGNORED_TOO = 1<<5,
84 DIR_COLLECT_KILLED_ONLY = 1<<6
Johannes Schindelin7c4c97c2009-02-16 13:20:25 +010085 } flags;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070086 struct dir_entry **entries;
Jeff King2abd31b2007-06-11 09:39:50 -040087 struct dir_entry **ignored;
Linus Torvalds453ec4b2006-05-16 19:02:14 -070088
89 /* Exclude info */
90 const char *exclude_per_dir;
Adam Spiersc082df22013-01-06 16:58:03 +000091
Junio C Hamano63d285c2007-11-29 02:17:44 -080092 /*
Adam Spiersc082df22013-01-06 16:58:03 +000093 * We maintain three groups of exclude pattern lists:
94 *
Junio C Hamano63d285c2007-11-29 02:17:44 -080095 * EXC_CMDL lists patterns explicitly given on the command line.
96 * EXC_DIRS lists patterns obtained from per-directory ignore files.
Adam Spiersc082df22013-01-06 16:58:03 +000097 * EXC_FILE lists patterns from fallback ignore files, e.g.
98 * - .git/info/exclude
99 * - core.excludesfile
100 *
101 * Each group contains multiple exclude lists, a single list
102 * per source.
Junio C Hamano63d285c2007-11-29 02:17:44 -0800103 */
104#define EXC_CMDL 0
105#define EXC_DIRS 1
106#define EXC_FILE 2
Adam Spiersc082df22013-01-06 16:58:03 +0000107 struct exclude_list_group exclude_list_group[3];
Junio C Hamano63d285c2007-11-29 02:17:44 -0800108
Adam Spiers95a68342012-12-27 02:32:21 +0000109 /*
110 * Temporary variables which are used during loading of the
111 * per-directory exclude lists.
112 *
113 * exclude_stack points to the top of the exclude_stack, and
114 * basebuf contains the full path to the current
Karsten Blees95c6f272013-04-15 21:12:14 +0200115 * (sub)directory in the traversal. Exclude points to the
116 * matching exclude struct if the directory is excluded.
Adam Spiers95a68342012-12-27 02:32:21 +0000117 */
Junio C Hamano63d285c2007-11-29 02:17:44 -0800118 struct exclude_stack *exclude_stack;
Karsten Blees95c6f272013-04-15 21:12:14 +0200119 struct exclude *exclude;
Junio C Hamano63d285c2007-11-29 02:17:44 -0800120 char basebuf[PATH_MAX];
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700121};
122
Adam Spiers52ed1892013-01-06 16:58:06 +0000123/*
124 * The ordering of these constants is significant, with
125 * higher-numbered match types signifying "closer" (i.e. more
126 * specific) matches which will override lower-numbered match types
127 * when populating the seen[] array.
128 */
Junio C Hamanoe813d502006-12-25 03:09:52 -0800129#define MATCHED_RECURSIVELY 1
130#define MATCHED_FNMATCH 2
131#define MATCHED_EXACTLY 3
Nguyễn Thái Ngọc Duy87323bd2013-07-14 15:35:28 +0700132extern int simple_length(const char *match);
133extern int no_wildcard(const char *string);
Nguyễn Thái Ngọc Duy827f4d62013-07-14 15:35:57 +0700134extern char *common_prefix(const struct pathspec *pathspec);
Nguyễn Thái Ngọc Duy854b0952014-01-24 20:40:30 +0700135extern int match_pathspec(const struct pathspec *pathspec,
136 const char *name, int namelen,
137 int prefix, char *seen);
Nguyễn Thái Ngọc Duybc96cc82010-12-15 22:02:44 +0700138extern int within_depth(const char *name, int namelen, int depth, int max_depth);
Linus Torvalds3c6a3702006-05-19 16:07:51 -0700139
Nguyễn Thái Ngọc Duy7327d3d2013-07-14 15:35:55 +0700140extern int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec);
141extern int read_directory(struct dir_struct *, const char *path, int len, const struct pathspec *pathspec);
Junio C Hamanof8a9d422006-12-04 16:00:46 -0800142
Adam Spiers07958052012-12-27 02:32:24 +0000143extern int is_excluded_from_list(const char *pathname, int pathlen, const char *basename,
144 int *dtype, struct exclude_list *el);
Jens Lehmann108da0d2010-07-10 00:18:38 +0200145struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len);
Junio C Hamanoeb417752012-06-01 11:28:00 -0700146
147/*
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700148 * these implement the matching logic for dir.c:excluded_from_list and
149 * attr.c:path_matches()
150 */
151extern int match_basename(const char *, int,
152 const char *, int, int, int);
153extern int match_pathname(const char *, int,
154 const char *, int,
155 const char *, int, int, int);
156
Karsten Bleesb07bc8c2013-04-15 21:12:57 +0200157extern struct exclude *last_exclude_matching(struct dir_struct *dir,
158 const char *name, int *dtype);
Junio C Hamano782cd4c2012-06-05 21:17:52 -0700159
Karsten Bleesb07bc8c2013-04-15 21:12:57 +0200160extern int is_excluded(struct dir_struct *dir, const char *name, int *dtype);
Junio C Hamanoeb417752012-06-01 11:28:00 -0700161
Adam Spiersc04318e2013-01-06 16:58:04 +0000162extern struct exclude_list *add_exclude_list(struct dir_struct *dir,
163 int group_type, const char *src);
Nguyễn Thái Ngọc Duycb097532009-08-20 20:47:04 +0700164extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
Adam Spiersc082df22013-01-06 16:58:03 +0000165 struct exclude_list *el, int check_index);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700166extern void add_excludes_from_file(struct dir_struct *, const char *fname);
Nguyễn Thái Ngọc Duy82dce992012-10-15 13:24:39 +0700167extern void parse_exclude_pattern(const char **string, int *patternlen, int *flags, int *nowildcardlen);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700168extern void add_exclude(const char *string, const char *base,
Adam Spiersc04318e2013-01-06 16:58:04 +0000169 int baselen, struct exclude_list *el, int srcpos);
Adam Spiersf6198812012-12-27 02:32:29 +0000170extern void clear_exclude_list(struct exclude_list *el);
Adam Spiers270be812013-01-06 16:58:05 +0000171extern void clear_directory(struct dir_struct *dir);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400172extern int file_exists(const char *);
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700173
Johannes Schindeline6636742007-08-01 01:29:17 +0100174extern int is_inside_dir(const char *dir);
Nguyễn Thái Ngọc Duy9b125da2011-03-26 16:04:24 +0700175extern int dir_inside_of(const char *subdir, const char *dir);
Johannes Schindeline6636742007-08-01 01:29:17 +0100176
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300177static inline int is_dot_or_dotdot(const char *name)
178{
179 return (name[0] == '.' &&
180 (name[1] == '\0' ||
181 (name[1] == '.' && name[2] == '\0')));
182}
183
Alexander Potashev55892d22009-01-11 15:19:12 +0300184extern int is_empty_dir(const char *dir);
185
Junio C Hamano039bc642007-11-14 00:05:00 -0800186extern void setup_standard_excludes(struct dir_struct *dir);
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -0700187
188#define REMOVE_DIR_EMPTY_ONLY 01
189#define REMOVE_DIR_KEEP_NESTED_GIT 02
Junio C Hamanoc844a802012-03-15 15:58:54 +0100190#define REMOVE_DIR_KEEP_TOPLEVEL 04
Junio C Hamanoa0f4afb2009-06-30 15:33:45 -0700191extern int remove_dir_recursively(struct strbuf *path, int flag);
Johannes Schindelin7155b722007-09-28 16:28:54 +0100192
Alex Riesen4a92d1b2008-09-27 00:56:46 +0200193/* tries to remove the path with empty directories along it, ignores ENOENT */
194extern int remove_path(const char *path);
195
Joshua Jensen8cf2a842010-10-03 09:56:41 +0000196extern int strcmp_icase(const char *a, const char *b);
197extern int strncmp_icase(const char *a, const char *b, size_t count);
198extern int fnmatch_icase(const char *pattern, const char *string, int flags);
199
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +0700200/*
201 * The prefix part of pattern must not contains wildcards.
202 */
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +0700203struct pathspec_item;
204extern int git_fnmatch(const struct pathspec_item *item,
205 const char *pattern, const char *string,
206 int prefix);
Nguyễn Thái Ngọc Duy5d747622012-11-24 11:33:49 +0700207
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +0700208static inline int ce_path_match(const struct cache_entry *ce,
209 const struct pathspec *pathspec,
210 char *seen)
211{
Nguyễn Thái Ngọc Duy854b0952014-01-24 20:40:30 +0700212 return match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +0700213}
214
Nguyễn Thái Ngọc Duyebb32892014-01-24 20:40:29 +0700215static inline int dir_path_match(const struct dir_entry *ent,
216 const struct pathspec *pathspec,
217 int prefix, char *seen)
218{
Nguyễn Thái Ngọc Duy854b0952014-01-24 20:40:30 +0700219 return match_pathspec(pathspec, ent->name, ent->len, prefix, seen);
Nguyễn Thái Ngọc Duyebb32892014-01-24 20:40:29 +0700220}
221
Linus Torvalds453ec4b2006-05-16 19:02:14 -0700222#endif