blob: 454ce364fac7767a78c3d712fb4f82c448f88f33 [file] [log] [blame]
Adam Spiers6f525e72013-01-06 16:58:08 +00001#ifndef PATHSPEC_H
2#define PATHSPEC_H
3
Elijah Newrenef3ca952018-08-15 10:54:05 -07004struct index_state;
5
Nguyễn Thái Ngọc Duy87323bd2013-07-14 15:35:28 +07006/* Pathspec magic */
7#define PATHSPEC_FROMTOP (1<<0)
Nguyễn Thái Ngọc Duy6330a172013-07-14 15:35:32 +07008#define PATHSPEC_MAXDEPTH (1<<1)
Nguyễn Thái Ngọc Duy5c6933d2013-07-14 15:36:06 +07009#define PATHSPEC_LITERAL (1<<2)
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +070010#define PATHSPEC_GLOB (1<<3)
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +070011#define PATHSPEC_ICASE (1<<4)
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +070012#define PATHSPEC_EXCLUDE (1<<5)
Brandon Williamsb0db7042017-03-13 11:23:21 -070013#define PATHSPEC_ATTR (1<<6)
Nguyễn Thái Ngọc Duy6330a172013-07-14 15:35:32 +070014#define PATHSPEC_ALL_MAGIC \
15 (PATHSPEC_FROMTOP | \
Nguyễn Thái Ngọc Duy5c6933d2013-07-14 15:36:06 +070016 PATHSPEC_MAXDEPTH | \
Nguyễn Thái Ngọc Duybd30c2e2013-07-14 15:36:08 +070017 PATHSPEC_LITERAL | \
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +070018 PATHSPEC_GLOB | \
Nguyễn Thái Ngọc Duyef79b1f2013-12-06 14:30:48 +070019 PATHSPEC_ICASE | \
Brandon Williamsb0db7042017-03-13 11:23:21 -070020 PATHSPEC_EXCLUDE | \
21 PATHSPEC_ATTR)
Nguyễn Thái Ngọc Duy87323bd2013-07-14 15:35:28 +070022
Junio C Hamanofadf96a2013-09-09 14:50:42 -070023#define PATHSPEC_ONESTAR 1 /* the pathspec pattern satisfies GFNM_ONESTAR */
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +070024
Heba Waly19ef3dd2019-11-17 21:04:49 +000025/**
26 * See glossary-context.txt for the syntax of pathspec.
27 * In memory, a pathspec set is represented by "struct pathspec" and is
28 * prepared by parse_pathspec().
29 */
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +070030struct pathspec {
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +070031 int nr;
32 unsigned int has_wildcard:1;
33 unsigned int recursive:1;
Brandon Williamseef3df52017-12-04 16:07:34 -080034 unsigned int recurse_submodules:1;
Nguyễn Thái Ngọc Duy87323bd2013-07-14 15:35:28 +070035 unsigned magic;
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +070036 int max_depth;
37 struct pathspec_item {
Brandon Williams8aee7692017-01-04 10:04:01 -080038 char *match;
39 char *original;
Nguyễn Thái Ngọc Duy87323bd2013-07-14 15:35:28 +070040 unsigned magic;
Nguyễn Thái Ngọc Duy645a29c2013-07-14 15:36:03 +070041 int len, prefix;
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +070042 int nowildcard_len;
43 int flags;
Brandon Williamsb0db7042017-03-13 11:23:21 -070044 int attr_match_nr;
45 struct attr_match {
46 char *value;
47 enum attr_match_mode {
48 MATCH_SET,
49 MATCH_UNSET,
50 MATCH_VALUE,
51 MATCH_UNSPECIFIED
52 } match_mode;
53 } *attr_match;
54 struct attr_check *attr_check;
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +070055 } *items;
56};
57
Nguyễn Thái Ngọc Duy8f4f8f42013-07-14 15:35:36 +070058#define GUARD_PATHSPEC(ps, mask) \
59 do { \
60 if ((ps)->magic & ~(mask)) \
61 die("BUG:%s:%d: unsupported magic %x", \
62 __FILE__, __LINE__, (ps)->magic & ~(mask)); \
63 } while (0)
64
Nguyễn Thái Ngọc Duyfc122612013-07-14 15:35:30 +070065/* parse_pathspec flags */
66#define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */
67#define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */
Nguyễn Thái Ngọc Duy6330a172013-07-14 15:35:32 +070068#define PATHSPEC_MAXDEPTH_VALID (1<<2) /* max_depth field is valid */
Nguyễn Thái Ngọc Duy87450242013-07-14 15:35:34 +070069/* die if a symlink is part of the given path's directory */
Brandon Williams2249d4d2017-05-11 15:04:26 -070070#define PATHSPEC_SYMLINK_LEADING_PATH (1<<3)
71#define PATHSPEC_PREFIX_ORIGIN (1<<4)
72#define PATHSPEC_KEEP_ORDER (1<<5)
Nguyễn Thái Ngọc Duy4a2d5ae2013-10-26 09:09:20 +070073/*
74 * For the callers that just need pure paths from somewhere else, not
75 * from command line. Global --*-pathspecs options are ignored. No
76 * magic is parsed in each pathspec either. If PATHSPEC_LITERAL is
77 * allowed, then it will automatically set for every pathspec.
78 */
Brandon Williams2249d4d2017-05-11 15:04:26 -070079#define PATHSPEC_LITERAL_PATH (1<<6)
Nguyễn Thái Ngọc Duyfc122612013-07-14 15:35:30 +070080
Heba Waly19ef3dd2019-11-17 21:04:49 +000081/**
Jonathan Nieder29c0e902017-09-20 21:41:12 -070082 * Given command line arguments and a prefix, convert the input to
83 * pathspec. die() if any magic in magic_mask is used.
84 *
85 * Any arguments used are copied. It is safe for the caller to modify
86 * or free 'prefix' and 'args' after calling this function.
Heba Waly19ef3dd2019-11-17 21:04:49 +000087 *
88 * - magic_mask specifies what features that are NOT supported by the following
89 * code. If a user attempts to use such a feature, parse_pathspec() can reject
90 * it early.
91 *
92 * - flags specifies other things that the caller wants parse_pathspec to
93 * perform.
94 *
95 * - prefix and args come from cmd_* functions
96 *
97 * parse_pathspec() helps catch unsupported features and reject them politely.
98 * At a lower level, different pathspec-related functions may not support the
99 * same set of features. Such pathspec-sensitive functions are guarded with
100 * GUARD_PATHSPEC(), which will die in an unfriendly way when an unsupported
101 * feature is requested.
102 *
103 * The command designers are supposed to make sure that GUARD_PATHSPEC() never
104 * dies. They have to make sure all unsupported features are caught by
105 * parse_pathspec(), not by GUARD_PATHSPEC. grepping GUARD_PATHSPEC() should
106 * give the designers all pathspec-sensitive codepaths and what features they
107 * support.
108 *
109 * A similar process is applied when a new pathspec magic is added. The designer
110 * lifts the GUARD_PATHSPEC restriction in the functions that support the new
111 * magic. At the same time (s)he has to make sure this new feature will be
112 * caught at parse_pathspec() in commands that cannot handle the new magic in
113 * some cases. grepping parse_pathspec() should help.
Jonathan Nieder29c0e902017-09-20 21:41:12 -0700114 */
Nguyễn Thái Ngọc Duy93e23792018-11-18 17:47:58 +0100115void parse_pathspec(struct pathspec *pathspec,
116 unsigned magic_mask,
117 unsigned flags,
118 const char *prefix,
119 const char **args);
Alexandr Miloslavskiy24e47502019-11-19 16:48:51 +0000120/*
121 * Same as parse_pathspec() but uses file as input.
122 * When 'file' is exactly "-" it uses 'stdin' instead.
123 */
124void parse_pathspec_file(struct pathspec *pathspec,
125 unsigned magic_mask,
126 unsigned flags,
127 const char *prefix,
128 const char *file,
129 int nul_term_line);
Heba Waly19ef3dd2019-11-17 21:04:49 +0000130
Nguyễn Thái Ngọc Duy93e23792018-11-18 17:47:58 +0100131void copy_pathspec(struct pathspec *dst, const struct pathspec *src);
132void clear_pathspec(struct pathspec *);
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +0700133
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700134static inline int ps_strncmp(const struct pathspec_item *item,
135 const char *s1, const char *s2, size_t n)
136{
137 if (item->magic & PATHSPEC_ICASE)
138 return strncasecmp(s1, s2, n);
139 else
140 return strncmp(s1, s2, n);
141}
142
143static inline int ps_strcmp(const struct pathspec_item *item,
144 const char *s1, const char *s2)
145{
146 if (item->magic & PATHSPEC_ICASE)
147 return strcasecmp(s1, s2);
148 else
149 return strcmp(s1, s2);
150}
151
Nguyễn Thái Ngọc Duy93e23792018-11-18 17:47:58 +0100152void add_pathspec_matches_against_index(const struct pathspec *pathspec,
153 const struct index_state *istate,
154 char *seen);
155char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
156 const struct index_state *istate);
Nguyễn Thái Ngọc Duy22af33b2018-11-18 17:47:59 +0100157int match_pathspec_attrs(const struct index_state *istate,
158 const char *name, int namelen,
159 const struct pathspec_item *item);
Adam Spiers6f525e72013-01-06 16:58:08 +0000160
161#endif /* PATHSPEC_H */