Merge branch 'nd/files-backend-git-dir' into next
[git] / pathspec.h
1 #ifndef PATHSPEC_H
2 #define PATHSPEC_H
3
4 /* Pathspec magic */
5 #define PATHSPEC_FROMTOP        (1<<0)
6 #define PATHSPEC_MAXDEPTH       (1<<1)
7 #define PATHSPEC_LITERAL        (1<<2)
8 #define PATHSPEC_GLOB           (1<<3)
9 #define PATHSPEC_ICASE          (1<<4)
10 #define PATHSPEC_EXCLUDE        (1<<5)
11 #define PATHSPEC_ATTR           (1<<6)
12 #define PATHSPEC_ALL_MAGIC        \
13         (PATHSPEC_FROMTOP       | \
14          PATHSPEC_MAXDEPTH      | \
15          PATHSPEC_LITERAL       | \
16          PATHSPEC_GLOB          | \
17          PATHSPEC_ICASE         | \
18          PATHSPEC_EXCLUDE       | \
19          PATHSPEC_ATTR)
20
21 #define PATHSPEC_ONESTAR 1      /* the pathspec pattern satisfies GFNM_ONESTAR */
22
23 struct pathspec {
24         int nr;
25         unsigned int has_wildcard:1;
26         unsigned int recursive:1;
27         unsigned magic;
28         int max_depth;
29         struct pathspec_item {
30                 char *match;
31                 char *original;
32                 unsigned magic;
33                 int len, prefix;
34                 int nowildcard_len;
35                 int flags;
36                 int attr_match_nr;
37                 struct attr_match {
38                         char *value;
39                         enum attr_match_mode {
40                                 MATCH_SET,
41                                 MATCH_UNSET,
42                                 MATCH_VALUE,
43                                 MATCH_UNSPECIFIED
44                         } match_mode;
45                 } *attr_match;
46                 struct attr_check *attr_check;
47         } *items;
48 };
49
50 #define GUARD_PATHSPEC(ps, mask) \
51         do { \
52                 if ((ps)->magic & ~(mask))             \
53                         die("BUG:%s:%d: unsupported magic %x",  \
54                             __FILE__, __LINE__, (ps)->magic & ~(mask)); \
55         } while (0)
56
57 /* parse_pathspec flags */
58 #define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */
59 #define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */
60 #define PATHSPEC_MAXDEPTH_VALID (1<<2) /* max_depth field is valid */
61 /* strip the trailing slash if the given path is a gitlink */
62 #define PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP (1<<3)
63 /* die if a symlink is part of the given path's directory */
64 #define PATHSPEC_SYMLINK_LEADING_PATH (1<<4)
65 /*
66  * This is like a combination of ..LEADING_PATH and .._SLASH_CHEAP
67  * (but not the same): it strips the trailing slash if the given path
68  * is a gitlink but also checks and dies if gitlink is part of the
69  * leading path (i.e. the given path goes beyond a submodule). It's
70  * safer than _SLASH_CHEAP and also more expensive.
71  */
72 #define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)
73 #define PATHSPEC_PREFIX_ORIGIN (1<<6)
74 #define PATHSPEC_KEEP_ORDER (1<<7)
75 /*
76  * For the callers that just need pure paths from somewhere else, not
77  * from command line. Global --*-pathspecs options are ignored. No
78  * magic is parsed in each pathspec either. If PATHSPEC_LITERAL is
79  * allowed, then it will automatically set for every pathspec.
80  */
81 #define PATHSPEC_LITERAL_PATH (1<<8)
82
83 extern void parse_pathspec(struct pathspec *pathspec,
84                            unsigned magic_mask,
85                            unsigned flags,
86                            const char *prefix,
87                            const char **args);
88 extern void copy_pathspec(struct pathspec *dst, const struct pathspec *src);
89 extern void clear_pathspec(struct pathspec *);
90
91 static inline int ps_strncmp(const struct pathspec_item *item,
92                              const char *s1, const char *s2, size_t n)
93 {
94         if (item->magic & PATHSPEC_ICASE)
95                 return strncasecmp(s1, s2, n);
96         else
97                 return strncmp(s1, s2, n);
98 }
99
100 static inline int ps_strcmp(const struct pathspec_item *item,
101                             const char *s1, const char *s2)
102 {
103         if (item->magic & PATHSPEC_ICASE)
104                 return strcasecmp(s1, s2);
105         else
106                 return strcmp(s1, s2);
107 }
108
109 extern char *find_pathspecs_matching_against_index(const struct pathspec *pathspec);
110 extern void add_pathspec_matches_against_index(const struct pathspec *pathspec, char *seen);
111
112 #endif /* PATHSPEC_H */