blob: a5b38e0907a9b8c498d643653d445ed7cd71fe02 [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)) \
Ævar Arnfjörð Bjarmasona78537a2021-12-07 12:05:53 +010061 BUG("unsupported magic %x", (ps)->magic & ~(mask)); \
Nguyễn Thái Ngọc Duy8f4f8f42013-07-14 15:35:36 +070062 } while (0)
63
Nguyễn Thái Ngọc Duyfc122612013-07-14 15:35:30 +070064/* parse_pathspec flags */
65#define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */
66#define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */
Nguyễn Thái Ngọc Duy6330a172013-07-14 15:35:32 +070067#define PATHSPEC_MAXDEPTH_VALID (1<<2) /* max_depth field is valid */
Nguyễn Thái Ngọc Duy87450242013-07-14 15:35:34 +070068/* die if a symlink is part of the given path's directory */
Brandon Williams2249d4d2017-05-11 15:04:26 -070069#define PATHSPEC_SYMLINK_LEADING_PATH (1<<3)
70#define PATHSPEC_PREFIX_ORIGIN (1<<4)
71#define PATHSPEC_KEEP_ORDER (1<<5)
Nguyễn Thái Ngọc Duy4a2d5ae2013-10-26 09:09:20 +070072/*
73 * For the callers that just need pure paths from somewhere else, not
74 * from command line. Global --*-pathspecs options are ignored. No
75 * magic is parsed in each pathspec either. If PATHSPEC_LITERAL is
76 * allowed, then it will automatically set for every pathspec.
77 */
Brandon Williams2249d4d2017-05-11 15:04:26 -070078#define PATHSPEC_LITERAL_PATH (1<<6)
Nguyễn Thái Ngọc Duyfc122612013-07-14 15:35:30 +070079
Heba Waly19ef3dd2019-11-17 21:04:49 +000080/**
Jonathan Nieder29c0e902017-09-20 21:41:12 -070081 * Given command line arguments and a prefix, convert the input to
82 * pathspec. die() if any magic in magic_mask is used.
83 *
84 * Any arguments used are copied. It is safe for the caller to modify
85 * or free 'prefix' and 'args' after calling this function.
Heba Waly19ef3dd2019-11-17 21:04:49 +000086 *
87 * - magic_mask specifies what features that are NOT supported by the following
88 * code. If a user attempts to use such a feature, parse_pathspec() can reject
89 * it early.
90 *
91 * - flags specifies other things that the caller wants parse_pathspec to
92 * perform.
93 *
94 * - prefix and args come from cmd_* functions
95 *
96 * parse_pathspec() helps catch unsupported features and reject them politely.
97 * At a lower level, different pathspec-related functions may not support the
98 * same set of features. Such pathspec-sensitive functions are guarded with
99 * GUARD_PATHSPEC(), which will die in an unfriendly way when an unsupported
100 * feature is requested.
101 *
102 * The command designers are supposed to make sure that GUARD_PATHSPEC() never
103 * dies. They have to make sure all unsupported features are caught by
104 * parse_pathspec(), not by GUARD_PATHSPEC. grepping GUARD_PATHSPEC() should
105 * give the designers all pathspec-sensitive codepaths and what features they
106 * support.
107 *
108 * A similar process is applied when a new pathspec magic is added. The designer
109 * lifts the GUARD_PATHSPEC restriction in the functions that support the new
Felipe Contreras0e20b222021-06-15 14:11:10 +0000110 * magic while at the same time making sure this new feature will be
Heba Waly19ef3dd2019-11-17 21:04:49 +0000111 * caught at parse_pathspec() in commands that cannot handle the new magic in
112 * some cases. grepping parse_pathspec() should help.
Jonathan Nieder29c0e902017-09-20 21:41:12 -0700113 */
Nguyễn Thái Ngọc Duy93e23792018-11-18 17:47:58 +0100114void parse_pathspec(struct pathspec *pathspec,
115 unsigned magic_mask,
116 unsigned flags,
117 const char *prefix,
118 const char **args);
Alexandr Miloslavskiy24e47502019-11-19 16:48:51 +0000119/*
120 * Same as parse_pathspec() but uses file as input.
121 * When 'file' is exactly "-" it uses 'stdin' instead.
122 */
123void parse_pathspec_file(struct pathspec *pathspec,
124 unsigned magic_mask,
125 unsigned flags,
126 const char *prefix,
127 const char *file,
128 int nul_term_line);
Heba Waly19ef3dd2019-11-17 21:04:49 +0000129
Nguyễn Thái Ngọc Duy93e23792018-11-18 17:47:58 +0100130void copy_pathspec(struct pathspec *dst, const struct pathspec *src);
131void clear_pathspec(struct pathspec *);
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +0700132
Nguyễn Thái Ngọc Duy93d93532013-07-14 15:36:09 +0700133static inline int ps_strncmp(const struct pathspec_item *item,
134 const char *s1, const char *s2, size_t n)
135{
136 if (item->magic & PATHSPEC_ICASE)
137 return strncasecmp(s1, s2, n);
138 else
139 return strncmp(s1, s2, n);
140}
141
142static inline int ps_strcmp(const struct pathspec_item *item,
143 const char *s1, const char *s2)
144{
145 if (item->magic & PATHSPEC_ICASE)
146 return strcasecmp(s1, s2);
147 else
148 return strcmp(s1, s2);
149}
150
Matheus Tavares719630e2021-04-08 17:41:25 -0300151enum ps_skip_worktree_action {
152 PS_HEED_SKIP_WORKTREE = 0,
153 PS_IGNORE_SKIP_WORKTREE = 1
154};
Nguyễn Thái Ngọc Duy93e23792018-11-18 17:47:58 +0100155void add_pathspec_matches_against_index(const struct pathspec *pathspec,
Derrick Stolee847a9e52021-04-01 01:49:39 +0000156 struct index_state *istate,
Matheus Tavares719630e2021-04-08 17:41:25 -0300157 char *seen,
158 enum ps_skip_worktree_action sw_action);
Nguyễn Thái Ngọc Duy93e23792018-11-18 17:47:58 +0100159char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
Junio C Hamanofe069dc2021-05-07 12:47:39 +0900160 struct index_state *istate,
Matheus Tavares719630e2021-04-08 17:41:25 -0300161 enum ps_skip_worktree_action sw_action);
Matheus Tavaresa20f7042021-04-08 17:41:27 -0300162char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec);
163static inline int matches_skip_worktree(const struct pathspec *pathspec,
164 int item, char **seen_ptr)
165{
166 if (!*seen_ptr)
167 *seen_ptr = find_pathspecs_matching_skip_worktree(pathspec);
168 return (*seen_ptr)[item];
169}
Derrick Stolee847a9e52021-04-01 01:49:39 +0000170int match_pathspec_attrs(struct index_state *istate,
Nguyễn Thái Ngọc Duy22af33b2018-11-18 17:47:59 +0100171 const char *name, int namelen,
172 const struct pathspec_item *item);
Adam Spiers6f525e72013-01-06 16:58:08 +0000173
Elijah Newrenac48adf2023-02-24 00:09:31 +0000174int match_pathspec(struct index_state *istate,
175 const struct pathspec *pathspec,
176 const char *name, int namelen,
177 int prefix, char *seen, int is_dir);
178
Shaoxuan Yuanb29ad382022-08-07 12:13:33 +0800179/*
180 * Determine whether a pathspec will match only entire index entries (non-sparse
181 * files and/or entire sparse directories). If the pathspec has the potential to
182 * match partial contents of a sparse directory, return 1 to indicate the index
183 * should be expanded to match the appropriate index entries.
184 *
185 * For the sake of simplicity, always return 1 if using a more complex "magic"
186 * pathspec.
187 */
188int pathspec_needs_expanded_index(struct index_state *istate,
189 const struct pathspec *pathspec);
190
Adam Spiers6f525e72013-01-06 16:58:08 +0000191#endif /* PATHSPEC_H */