Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 1 | #ifndef PATHSPEC_H |
| 2 | #define PATHSPEC_H |
| 3 | |
Elijah Newren | ef3ca95 | 2018-08-15 10:54:05 -0700 | [diff] [blame] | 4 | struct index_state; |
| 5 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 6 | /* Pathspec magic */ |
| 7 | #define PATHSPEC_FROMTOP (1<<0) |
Nguyễn Thái Ngọc Duy | 6330a17 | 2013-07-14 15:35:32 +0700 | [diff] [blame] | 8 | #define PATHSPEC_MAXDEPTH (1<<1) |
Nguyễn Thái Ngọc Duy | 5c6933d | 2013-07-14 15:36:06 +0700 | [diff] [blame] | 9 | #define PATHSPEC_LITERAL (1<<2) |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 10 | #define PATHSPEC_GLOB (1<<3) |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 11 | #define PATHSPEC_ICASE (1<<4) |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 12 | #define PATHSPEC_EXCLUDE (1<<5) |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 13 | #define PATHSPEC_ATTR (1<<6) |
Nguyễn Thái Ngọc Duy | 6330a17 | 2013-07-14 15:35:32 +0700 | [diff] [blame] | 14 | #define PATHSPEC_ALL_MAGIC \ |
| 15 | (PATHSPEC_FROMTOP | \ |
Nguyễn Thái Ngọc Duy | 5c6933d | 2013-07-14 15:36:06 +0700 | [diff] [blame] | 16 | PATHSPEC_MAXDEPTH | \ |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 17 | PATHSPEC_LITERAL | \ |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 18 | PATHSPEC_GLOB | \ |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 19 | PATHSPEC_ICASE | \ |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 20 | PATHSPEC_EXCLUDE | \ |
| 21 | PATHSPEC_ATTR) |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 22 | |
Junio C Hamano | fadf96a | 2013-09-09 14:50:42 -0700 | [diff] [blame] | 23 | #define PATHSPEC_ONESTAR 1 /* the pathspec pattern satisfies GFNM_ONESTAR */ |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 24 | |
Heba Waly | 19ef3dd | 2019-11-17 21:04:49 +0000 | [diff] [blame] | 25 | /** |
| 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 Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 30 | struct pathspec { |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 31 | int nr; |
| 32 | unsigned int has_wildcard:1; |
| 33 | unsigned int recursive:1; |
Brandon Williams | eef3df5 | 2017-12-04 16:07:34 -0800 | [diff] [blame] | 34 | unsigned int recurse_submodules:1; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 35 | unsigned magic; |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 36 | int max_depth; |
| 37 | struct pathspec_item { |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 38 | char *match; |
| 39 | char *original; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 40 | unsigned magic; |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 41 | int len, prefix; |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 42 | int nowildcard_len; |
| 43 | int flags; |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 44 | 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 Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 55 | } *items; |
| 56 | }; |
| 57 | |
Nguyễn Thái Ngọc Duy | 8f4f8f4 | 2013-07-14 15:35:36 +0700 | [diff] [blame] | 58 | #define GUARD_PATHSPEC(ps, mask) \ |
| 59 | do { \ |
| 60 | if ((ps)->magic & ~(mask)) \ |
Ævar Arnfjörð Bjarmason | a78537a | 2021-12-07 12:05:53 +0100 | [diff] [blame] | 61 | BUG("unsupported magic %x", (ps)->magic & ~(mask)); \ |
Nguyễn Thái Ngọc Duy | 8f4f8f4 | 2013-07-14 15:35:36 +0700 | [diff] [blame] | 62 | } while (0) |
| 63 | |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 64 | /* 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 Duy | 6330a17 | 2013-07-14 15:35:32 +0700 | [diff] [blame] | 67 | #define PATHSPEC_MAXDEPTH_VALID (1<<2) /* max_depth field is valid */ |
Nguyễn Thái Ngọc Duy | 8745024 | 2013-07-14 15:35:34 +0700 | [diff] [blame] | 68 | /* die if a symlink is part of the given path's directory */ |
Brandon Williams | 2249d4d | 2017-05-11 15:04:26 -0700 | [diff] [blame] | 69 | #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 Duy | 4a2d5ae | 2013-10-26 09:09:20 +0700 | [diff] [blame] | 72 | /* |
| 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 Williams | 2249d4d | 2017-05-11 15:04:26 -0700 | [diff] [blame] | 78 | #define PATHSPEC_LITERAL_PATH (1<<6) |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 79 | |
Heba Waly | 19ef3dd | 2019-11-17 21:04:49 +0000 | [diff] [blame] | 80 | /** |
Jonathan Nieder | 29c0e90 | 2017-09-20 21:41:12 -0700 | [diff] [blame] | 81 | * 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 Waly | 19ef3dd | 2019-11-17 21:04:49 +0000 | [diff] [blame] | 86 | * |
| 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 Contreras | 0e20b22 | 2021-06-15 14:11:10 +0000 | [diff] [blame] | 110 | * magic while at the same time making sure this new feature will be |
Heba Waly | 19ef3dd | 2019-11-17 21:04:49 +0000 | [diff] [blame] | 111 | * caught at parse_pathspec() in commands that cannot handle the new magic in |
| 112 | * some cases. grepping parse_pathspec() should help. |
Jonathan Nieder | 29c0e90 | 2017-09-20 21:41:12 -0700 | [diff] [blame] | 113 | */ |
Nguyễn Thái Ngọc Duy | 93e2379 | 2018-11-18 17:47:58 +0100 | [diff] [blame] | 114 | void parse_pathspec(struct pathspec *pathspec, |
| 115 | unsigned magic_mask, |
| 116 | unsigned flags, |
| 117 | const char *prefix, |
| 118 | const char **args); |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 119 | /* |
| 120 | * Same as parse_pathspec() but uses file as input. |
| 121 | * When 'file' is exactly "-" it uses 'stdin' instead. |
| 122 | */ |
| 123 | void 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 Waly | 19ef3dd | 2019-11-17 21:04:49 +0000 | [diff] [blame] | 129 | |
Nguyễn Thái Ngọc Duy | 93e2379 | 2018-11-18 17:47:58 +0100 | [diff] [blame] | 130 | void copy_pathspec(struct pathspec *dst, const struct pathspec *src); |
| 131 | void clear_pathspec(struct pathspec *); |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 132 | |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 133 | static 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 | |
| 142 | static 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 Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 151 | enum ps_skip_worktree_action { |
| 152 | PS_HEED_SKIP_WORKTREE = 0, |
| 153 | PS_IGNORE_SKIP_WORKTREE = 1 |
| 154 | }; |
Nguyễn Thái Ngọc Duy | 93e2379 | 2018-11-18 17:47:58 +0100 | [diff] [blame] | 155 | void add_pathspec_matches_against_index(const struct pathspec *pathspec, |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 156 | struct index_state *istate, |
Matheus Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 157 | char *seen, |
| 158 | enum ps_skip_worktree_action sw_action); |
Nguyễn Thái Ngọc Duy | 93e2379 | 2018-11-18 17:47:58 +0100 | [diff] [blame] | 159 | char *find_pathspecs_matching_against_index(const struct pathspec *pathspec, |
Junio C Hamano | fe069dc | 2021-05-07 12:47:39 +0900 | [diff] [blame] | 160 | struct index_state *istate, |
Matheus Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 161 | enum ps_skip_worktree_action sw_action); |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 162 | char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec); |
| 163 | static 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 Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 170 | int match_pathspec_attrs(struct index_state *istate, |
Nguyễn Thái Ngọc Duy | 22af33b | 2018-11-18 17:47:59 +0100 | [diff] [blame] | 171 | const char *name, int namelen, |
| 172 | const struct pathspec_item *item); |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 173 | |
Elijah Newren | ac48adf | 2023-02-24 00:09:31 +0000 | [diff] [blame] | 174 | int 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 Yuan | b29ad38 | 2022-08-07 12:13:33 +0800 | [diff] [blame] | 179 | /* |
| 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 | */ |
| 188 | int pathspec_needs_expanded_index(struct index_state *istate, |
| 189 | const struct pathspec *pathspec); |
| 190 | |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 191 | #endif /* PATHSPEC_H */ |