Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | 0b027f6 | 2023-03-21 06:25:58 +0000 | [diff] [blame] | 2 | #include "abspath.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 4 | #include "dir.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 5 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 6 | #include "gettext.h" |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 7 | #include "pathspec.h" |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 8 | #include "attr.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 9 | #include "read-cache.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 10 | #include "repository.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 11 | #include "setup.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 12 | #include "strvec.h" |
Elijah Newren | cb2a513 | 2023-04-22 20:17:09 +0000 | [diff] [blame] | 13 | #include "symlinks.h" |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 14 | #include "quote.h" |
Elijah Newren | dd77d58 | 2023-05-16 06:34:03 +0000 | [diff] [blame] | 15 | #include "wildmatch.h" |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * Finds which of the given pathspecs match items in the index. |
| 19 | * |
| 20 | * For each pathspec, sets the corresponding entry in the seen[] array |
| 21 | * (which should be specs items long, i.e. the same size as pathspec) |
| 22 | * to the nature of the "closest" (i.e. most specific) match found for |
| 23 | * that pathspec in the index, if it was a closer type of match than |
| 24 | * the existing entry. As an optimization, matching is skipped |
| 25 | * altogether if seen[] already only contains non-zero entries. |
| 26 | * |
| 27 | * If seen[] has not already been written to, it may make sense |
Adam Spiers | 4b78d7b | 2013-01-06 16:58:09 +0000 | [diff] [blame] | 28 | * to use find_pathspecs_matching_against_index() instead. |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 29 | */ |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 30 | void add_pathspec_matches_against_index(const struct pathspec *pathspec, |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 31 | struct index_state *istate, |
Matheus Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 32 | char *seen, |
| 33 | enum ps_skip_worktree_action sw_action) |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 34 | { |
| 35 | int num_unmatched = 0, i; |
| 36 | |
| 37 | /* |
| 38 | * Since we are walking the index as if we were walking the directory, |
| 39 | * we have to mark the matched pathspec as seen; otherwise we will |
| 40 | * mistakenly think that the user gave a pathspec that did not match |
| 41 | * anything. |
| 42 | */ |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 43 | for (i = 0; i < pathspec->nr; i++) |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 44 | if (!seen[i]) |
| 45 | num_unmatched++; |
| 46 | if (!num_unmatched) |
| 47 | return; |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 48 | for (i = 0; i < istate->cache_nr; i++) { |
| 49 | const struct cache_entry *ce = istate->cache[i]; |
Derrick Stolee | 49fdd51 | 2021-09-24 15:39:07 +0000 | [diff] [blame] | 50 | if (sw_action == PS_IGNORE_SKIP_WORKTREE && |
| 51 | (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate))) |
Matheus Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 52 | continue; |
Nguyễn Thái Ngọc Duy | d17ef3a | 2018-08-13 18:14:30 +0200 | [diff] [blame] | 53 | ce_path_match(istate, ce, pathspec, seen); |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Finds which of the given pathspecs match items in the index. |
| 59 | * |
Adam Spiers | 4b78d7b | 2013-01-06 16:58:09 +0000 | [diff] [blame] | 60 | * This is a one-shot wrapper around add_pathspec_matches_against_index() |
| 61 | * which allocates, populates, and returns a seen[] array indicating the |
| 62 | * nature of the "closest" (i.e. most specific) matches which each of the |
| 63 | * given pathspecs achieves against all items in the index. |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 64 | */ |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 65 | char *find_pathspecs_matching_against_index(const struct pathspec *pathspec, |
Junio C Hamano | fe069dc | 2021-05-07 12:47:39 +0900 | [diff] [blame] | 66 | struct index_state *istate, |
Matheus Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 67 | enum ps_skip_worktree_action sw_action) |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 68 | { |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 69 | char *seen = xcalloc(pathspec->nr, 1); |
Matheus Tavares | 719630e | 2021-04-08 17:41:25 -0300 | [diff] [blame] | 70 | add_pathspec_matches_against_index(pathspec, istate, seen, sw_action); |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 71 | return seen; |
| 72 | } |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 73 | |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 74 | char *find_pathspecs_matching_skip_worktree(const struct pathspec *pathspec) |
| 75 | { |
| 76 | struct index_state *istate = the_repository->index; |
| 77 | char *seen = xcalloc(pathspec->nr, 1); |
| 78 | int i; |
| 79 | |
| 80 | for (i = 0; i < istate->cache_nr; i++) { |
| 81 | struct cache_entry *ce = istate->cache[i]; |
Derrick Stolee | 49fdd51 | 2021-09-24 15:39:07 +0000 | [diff] [blame] | 82 | if (ce_skip_worktree(ce) || !path_in_sparse_checkout(ce->name, istate)) |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 83 | ce_path_match(istate, ce, pathspec, seen); |
| 84 | } |
| 85 | |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 86 | return seen; |
| 87 | } |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 88 | |
| 89 | /* |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 90 | * Magic pathspec |
| 91 | * |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 92 | * Possible future magic semantics include stuff like: |
| 93 | * |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 94 | * { PATHSPEC_RECURSIVE, '*', "recursive" }, |
| 95 | * { PATHSPEC_REGEXP, '\0', "regexp" }, |
| 96 | * |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 97 | */ |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 98 | |
| 99 | static struct pathspec_magic { |
| 100 | unsigned bit; |
| 101 | char mnemonic; /* this cannot be ':'! */ |
| 102 | const char *name; |
| 103 | } pathspec_magic[] = { |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 104 | { PATHSPEC_FROMTOP, '/', "top" }, |
| 105 | { PATHSPEC_LITERAL, '\0', "literal" }, |
| 106 | { PATHSPEC_GLOB, '\0', "glob" }, |
| 107 | { PATHSPEC_ICASE, '\0', "icase" }, |
| 108 | { PATHSPEC_EXCLUDE, '!', "exclude" }, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 109 | { PATHSPEC_ATTR, '\0', "attr" }, |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 110 | }; |
| 111 | |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 112 | static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic) |
Nguyễn Thái Ngọc Duy | 1649612 | 2013-12-06 14:30:49 +0700 | [diff] [blame] | 113 | { |
| 114 | int i; |
| 115 | strbuf_addstr(sb, ":("); |
| 116 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 117 | if (magic & pathspec_magic[i].bit) { |
Nguyễn Thái Ngọc Duy | 1649612 | 2013-12-06 14:30:49 +0700 | [diff] [blame] | 118 | if (sb->buf[sb->len - 1] != '(') |
| 119 | strbuf_addch(sb, ','); |
| 120 | strbuf_addstr(sb, pathspec_magic[i].name); |
| 121 | } |
| 122 | strbuf_addf(sb, ",prefix:%d)", prefixlen); |
| 123 | } |
| 124 | |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 125 | static size_t strcspn_escaped(const char *s, const char *stop) |
| 126 | { |
| 127 | const char *i; |
| 128 | |
| 129 | for (i = s; *i; i++) { |
| 130 | /* skip the escaped character */ |
| 131 | if (i[0] == '\\' && i[1]) { |
| 132 | i++; |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | if (strchr(stop, *i)) |
| 137 | break; |
| 138 | } |
| 139 | return i - s; |
| 140 | } |
| 141 | |
| 142 | static inline int invalid_value_char(const char ch) |
| 143 | { |
| 144 | if (isalnum(ch) || strchr(",-_", ch)) |
| 145 | return 0; |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | static char *attr_value_unescape(const char *value) |
| 150 | { |
| 151 | const char *src; |
| 152 | char *dst, *ret; |
| 153 | |
| 154 | ret = xmallocz(strlen(value)); |
| 155 | for (src = value, dst = ret; *src; src++, dst++) { |
| 156 | if (*src == '\\') { |
| 157 | if (!src[1]) |
| 158 | die(_("Escape character '\\' not allowed as " |
| 159 | "last character in attr value")); |
| 160 | src++; |
| 161 | } |
| 162 | if (invalid_value_char(*src)) |
| 163 | die("cannot use '%c' for value matching", *src); |
| 164 | *dst = *src; |
| 165 | } |
| 166 | *dst = '\0'; |
| 167 | return ret; |
| 168 | } |
| 169 | |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 170 | static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value) |
| 171 | { |
| 172 | struct string_list_item *si; |
| 173 | struct string_list list = STRING_LIST_INIT_DUP; |
| 174 | |
| 175 | if (item->attr_check || item->attr_match) |
| 176 | die(_("Only one 'attr:' specification is allowed.")); |
| 177 | |
| 178 | if (!value || !*value) |
| 179 | die(_("attr spec must not be empty")); |
| 180 | |
| 181 | string_list_split(&list, value, ' ', -1); |
| 182 | string_list_remove_empty_items(&list, 0); |
| 183 | |
| 184 | item->attr_check = attr_check_alloc(); |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 185 | CALLOC_ARRAY(item->attr_match, list.nr); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 186 | |
| 187 | for_each_string_list_item(si, &list) { |
| 188 | size_t attr_len; |
| 189 | char *attr_name; |
| 190 | const struct git_attr *a; |
| 191 | |
| 192 | int j = item->attr_match_nr++; |
| 193 | const char *attr = si->string; |
| 194 | struct attr_match *am = &item->attr_match[j]; |
| 195 | |
| 196 | switch (*attr) { |
| 197 | case '!': |
| 198 | am->match_mode = MATCH_UNSPECIFIED; |
| 199 | attr++; |
| 200 | attr_len = strlen(attr); |
| 201 | break; |
| 202 | case '-': |
| 203 | am->match_mode = MATCH_UNSET; |
| 204 | attr++; |
| 205 | attr_len = strlen(attr); |
| 206 | break; |
| 207 | default: |
| 208 | attr_len = strcspn(attr, "="); |
| 209 | if (attr[attr_len] != '=') |
| 210 | am->match_mode = MATCH_SET; |
| 211 | else { |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 212 | const char *v = &attr[attr_len + 1]; |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 213 | am->match_mode = MATCH_VALUE; |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 214 | am->value = attr_value_unescape(v); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 215 | } |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | attr_name = xmemdupz(attr, attr_len); |
| 220 | a = git_attr(attr_name); |
| 221 | if (!a) |
| 222 | die(_("invalid attribute name %s"), attr_name); |
| 223 | |
| 224 | attr_check_append(item->attr_check, a); |
| 225 | |
| 226 | free(attr_name); |
| 227 | } |
| 228 | |
| 229 | if (item->attr_check->nr != item->attr_match_nr) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 230 | BUG("should have same number of entries"); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 231 | |
| 232 | string_list_clear(&list, 0); |
| 233 | } |
| 234 | |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 235 | static inline int get_literal_global(void) |
| 236 | { |
| 237 | static int literal = -1; |
| 238 | |
| 239 | if (literal < 0) |
| 240 | literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0); |
| 241 | |
| 242 | return literal; |
| 243 | } |
| 244 | |
| 245 | static inline int get_glob_global(void) |
| 246 | { |
| 247 | static int glob = -1; |
| 248 | |
| 249 | if (glob < 0) |
| 250 | glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0); |
| 251 | |
| 252 | return glob; |
| 253 | } |
| 254 | |
| 255 | static inline int get_noglob_global(void) |
| 256 | { |
| 257 | static int noglob = -1; |
| 258 | |
| 259 | if (noglob < 0) |
| 260 | noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0); |
| 261 | |
| 262 | return noglob; |
| 263 | } |
| 264 | |
| 265 | static inline int get_icase_global(void) |
| 266 | { |
| 267 | static int icase = -1; |
| 268 | |
| 269 | if (icase < 0) |
| 270 | icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0); |
| 271 | |
| 272 | return icase; |
| 273 | } |
| 274 | |
| 275 | static int get_global_magic(int element_magic) |
| 276 | { |
| 277 | int global_magic = 0; |
| 278 | |
| 279 | if (get_literal_global()) |
| 280 | global_magic |= PATHSPEC_LITERAL; |
| 281 | |
| 282 | /* --glob-pathspec is overridden by :(literal) */ |
| 283 | if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL)) |
| 284 | global_magic |= PATHSPEC_GLOB; |
| 285 | |
| 286 | if (get_glob_global() && get_noglob_global()) |
| 287 | die(_("global 'glob' and 'noglob' pathspec settings are incompatible")); |
| 288 | |
| 289 | if (get_icase_global()) |
| 290 | global_magic |= PATHSPEC_ICASE; |
| 291 | |
| 292 | if ((global_magic & PATHSPEC_LITERAL) && |
| 293 | (global_magic & ~PATHSPEC_LITERAL)) |
| 294 | die(_("global 'literal' pathspec setting is incompatible " |
| 295 | "with all other global pathspec settings")); |
| 296 | |
| 297 | /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ |
| 298 | if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB)) |
| 299 | global_magic |= PATHSPEC_LITERAL; |
| 300 | |
| 301 | return global_magic; |
| 302 | } |
| 303 | |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 304 | /* |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 305 | * Parse the pathspec element looking for long magic |
| 306 | * |
| 307 | * saves all magic in 'magic' |
| 308 | * if prefix magic is used, save the prefix length in 'prefix_len' |
| 309 | * returns the position in 'elem' after all magic has been parsed |
| 310 | */ |
| 311 | static const char *parse_long_magic(unsigned *magic, int *prefix_len, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 312 | struct pathspec_item *item, |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 313 | const char *elem) |
| 314 | { |
| 315 | const char *pos; |
| 316 | const char *nextat; |
| 317 | |
| 318 | for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) { |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 319 | size_t len = strcspn_escaped(pos, ",)"); |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 320 | int i; |
| 321 | |
| 322 | if (pos[len] == ',') |
| 323 | nextat = pos + len + 1; /* handle ',' */ |
| 324 | else |
| 325 | nextat = pos + len; /* handle ')' and '\0' */ |
| 326 | |
| 327 | if (!len) |
| 328 | continue; |
| 329 | |
| 330 | if (starts_with(pos, "prefix:")) { |
| 331 | char *endptr; |
| 332 | *prefix_len = strtol(pos + 7, &endptr, 10); |
| 333 | if (endptr - pos != len) |
| 334 | die(_("invalid parameter for pathspec magic 'prefix'")); |
| 335 | continue; |
| 336 | } |
| 337 | |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 338 | if (starts_with(pos, "attr:")) { |
| 339 | char *attr_body = xmemdupz(pos + 5, len - 5); |
| 340 | parse_pathspec_attr_match(item, attr_body); |
| 341 | *magic |= PATHSPEC_ATTR; |
| 342 | free(attr_body); |
| 343 | continue; |
| 344 | } |
| 345 | |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 346 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { |
| 347 | if (strlen(pathspec_magic[i].name) == len && |
| 348 | !strncmp(pathspec_magic[i].name, pos, len)) { |
| 349 | *magic |= pathspec_magic[i].bit; |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (ARRAY_SIZE(pathspec_magic) <= i) |
| 355 | die(_("Invalid pathspec magic '%.*s' in '%s'"), |
| 356 | (int) len, pos, elem); |
| 357 | } |
| 358 | |
| 359 | if (*pos != ')') |
| 360 | die(_("Missing ')' at the end of pathspec magic in '%s'"), |
| 361 | elem); |
| 362 | pos++; |
| 363 | |
| 364 | return pos; |
| 365 | } |
| 366 | |
| 367 | /* |
Brandon Williams | b4bebdc | 2017-01-04 10:04:06 -0800 | [diff] [blame] | 368 | * Parse the pathspec element looking for short magic |
| 369 | * |
| 370 | * saves all magic in 'magic' |
| 371 | * returns the position in 'elem' after all magic has been parsed |
| 372 | */ |
| 373 | static const char *parse_short_magic(unsigned *magic, const char *elem) |
| 374 | { |
| 375 | const char *pos; |
| 376 | |
| 377 | for (pos = elem + 1; *pos && *pos != ':'; pos++) { |
| 378 | char ch = *pos; |
| 379 | int i; |
| 380 | |
Linus Torvalds | 42ebeb9 | 2017-02-07 21:05:28 -0800 | [diff] [blame] | 381 | /* Special case alias for '!' */ |
| 382 | if (ch == '^') { |
| 383 | *magic |= PATHSPEC_EXCLUDE; |
| 384 | continue; |
| 385 | } |
| 386 | |
Brandon Williams | b4bebdc | 2017-01-04 10:04:06 -0800 | [diff] [blame] | 387 | if (!is_pathspec_magic(ch)) |
| 388 | break; |
| 389 | |
| 390 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { |
| 391 | if (pathspec_magic[i].mnemonic == ch) { |
| 392 | *magic |= pathspec_magic[i].bit; |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (ARRAY_SIZE(pathspec_magic) <= i) |
| 398 | die(_("Unimplemented pathspec magic '%c' in '%s'"), |
| 399 | ch, elem); |
| 400 | } |
| 401 | |
| 402 | if (*pos == ':') |
| 403 | pos++; |
| 404 | |
| 405 | return pos; |
| 406 | } |
| 407 | |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 408 | static const char *parse_element_magic(unsigned *magic, int *prefix_len, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 409 | struct pathspec_item *item, |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 410 | const char *elem) |
| 411 | { |
| 412 | if (elem[0] != ':' || get_literal_global()) |
| 413 | return elem; /* nothing to do */ |
| 414 | else if (elem[1] == '(') |
| 415 | /* longhand */ |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 416 | return parse_long_magic(magic, prefix_len, item, elem); |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 417 | else |
| 418 | /* shorthand */ |
| 419 | return parse_short_magic(magic, elem); |
| 420 | } |
| 421 | |
Brandon Williams | b4bebdc | 2017-01-04 10:04:06 -0800 | [diff] [blame] | 422 | /* |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 423 | * Perform the initialization of a pathspec_item based on a pathspec element. |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 424 | */ |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 425 | static void init_pathspec_item(struct pathspec_item *item, unsigned flags, |
| 426 | const char *prefix, int prefixlen, |
| 427 | const char *elt) |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 428 | { |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 429 | unsigned magic = 0, element_magic = 0; |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 430 | const char *copyfrom = elt; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 431 | char *match; |
Brandon Williams | 5590215 | 2017-01-04 10:04:09 -0800 | [diff] [blame] | 432 | int pathspec_prefix = -1; |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 433 | |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 434 | item->attr_check = NULL; |
| 435 | item->attr_match = NULL; |
| 436 | item->attr_match_nr = 0; |
| 437 | |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 438 | /* PATHSPEC_LITERAL_PATH ignores magic */ |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 439 | if (flags & PATHSPEC_LITERAL_PATH) { |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 440 | magic = PATHSPEC_LITERAL; |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 441 | } else { |
| 442 | copyfrom = parse_element_magic(&element_magic, |
| 443 | &pathspec_prefix, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 444 | item, |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 445 | elt); |
| 446 | magic |= element_magic; |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 447 | magic |= get_global_magic(element_magic); |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 448 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 449 | |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 450 | item->magic = magic; |
| 451 | |
Nguyễn Thái Ngọc Duy | 233c3e6 | 2013-07-14 15:36:04 +0700 | [diff] [blame] | 452 | if (pathspec_prefix >= 0 && |
| 453 | (prefixlen || (prefix && *prefix))) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 454 | BUG("'prefix' magic is supposed to be used at worktree's root"); |
Nguyễn Thái Ngọc Duy | 233c3e6 | 2013-07-14 15:36:04 +0700 | [diff] [blame] | 455 | |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 456 | if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB)) |
| 457 | die(_("%s: 'literal' and 'glob' are incompatible"), elt); |
| 458 | |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 459 | /* Create match string which will be used for pathspec matching */ |
Nguyễn Thái Ngọc Duy | 233c3e6 | 2013-07-14 15:36:04 +0700 | [diff] [blame] | 460 | if (pathspec_prefix >= 0) { |
| 461 | match = xstrdup(copyfrom); |
| 462 | prefixlen = pathspec_prefix; |
| 463 | } else if (magic & PATHSPEC_FROMTOP) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 464 | match = xstrdup(copyfrom); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 465 | prefixlen = 0; |
| 466 | } else { |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 467 | match = prefix_path_gently(prefix, prefixlen, |
| 468 | &prefixlen, copyfrom); |
Emily Shaffer | 5c20398 | 2020-03-02 20:05:06 -0800 | [diff] [blame] | 469 | if (!match) { |
| 470 | const char *hint_path = get_git_work_tree(); |
| 471 | if (!hint_path) |
| 472 | hint_path = get_git_dir(); |
Emily Shaffer | e0020b2 | 2020-02-14 17:00:13 -0800 | [diff] [blame] | 473 | die(_("%s: '%s' is outside repository at '%s'"), elt, |
Emily Shaffer | 5c20398 | 2020-03-02 20:05:06 -0800 | [diff] [blame] | 474 | copyfrom, absolute_path(hint_path)); |
| 475 | } |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 476 | } |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 477 | |
Brandon Williams | 34305f7 | 2017-01-04 10:04:00 -0800 | [diff] [blame] | 478 | item->match = match; |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 479 | item->len = strlen(item->match); |
| 480 | item->prefix = prefixlen; |
| 481 | |
Nguyễn Thái Ngọc Duy | dad2586 | 2013-07-14 15:35:35 +0700 | [diff] [blame] | 482 | /* |
| 483 | * Prefix the pathspec (keep all magic) and assign to |
| 484 | * original. Useful for passing to another command. |
| 485 | */ |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 486 | if ((flags & PATHSPEC_PREFIX_ORIGIN) && |
Patrick Steinhardt | be4dbbb | 2017-04-04 11:16:56 +0200 | [diff] [blame] | 487 | !get_literal_global()) { |
Nguyễn Thái Ngọc Duy | dad2586 | 2013-07-14 15:35:35 +0700 | [diff] [blame] | 488 | struct strbuf sb = STRBUF_INIT; |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 489 | |
| 490 | /* Preserve the actual prefix length of each pattern */ |
| 491 | prefix_magic(&sb, prefixlen, element_magic); |
| 492 | |
Nguyễn Thái Ngọc Duy | dad2586 | 2013-07-14 15:35:35 +0700 | [diff] [blame] | 493 | strbuf_addstr(&sb, match); |
| 494 | item->original = strbuf_detach(&sb, NULL); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 495 | } else { |
| 496 | item->original = xstrdup(elt); |
| 497 | } |
Nguyễn Thái Ngọc Duy | b69bb3f | 2013-07-14 15:35:33 +0700 | [diff] [blame] | 498 | |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 499 | if (magic & PATHSPEC_LITERAL) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 500 | item->nowildcard_len = item->len; |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 501 | } else { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 502 | item->nowildcard_len = simple_length(item->match); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 503 | if (item->nowildcard_len < prefixlen) |
| 504 | item->nowildcard_len = prefixlen; |
| 505 | } |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 506 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 507 | item->flags = 0; |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 508 | if (magic & PATHSPEC_GLOB) { |
| 509 | /* |
| 510 | * FIXME: should we enable ONESTAR in _GLOB for |
| 511 | * pattern "* * / * . c"? |
| 512 | */ |
| 513 | } else { |
| 514 | if (item->nowildcard_len < item->len && |
| 515 | item->match[item->nowildcard_len] == '*' && |
| 516 | no_wildcard(item->match + item->nowildcard_len + 1)) |
| 517 | item->flags |= PATHSPEC_ONESTAR; |
| 518 | } |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 519 | |
| 520 | /* sanity checks, pathspec matchers assume these are sane */ |
Stefan Beller | 2d81c48 | 2017-01-09 15:16:50 -0800 | [diff] [blame] | 521 | if (item->nowildcard_len > item->len || |
| 522 | item->prefix > item->len) { |
Johannes Schindelin | c3c3486 | 2018-05-02 11:38:41 +0200 | [diff] [blame] | 523 | BUG("error initializing pathspec_item"); |
Stefan Beller | 2d81c48 | 2017-01-09 15:16:50 -0800 | [diff] [blame] | 524 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static int pathspec_item_cmp(const void *a_, const void *b_) |
| 528 | { |
| 529 | struct pathspec_item *a, *b; |
| 530 | |
| 531 | a = (struct pathspec_item *)a_; |
| 532 | b = (struct pathspec_item *)b_; |
| 533 | return strcmp(a->match, b->match); |
| 534 | } |
| 535 | |
Jeff King | 8e32caa | 2023-06-01 13:38:14 -0400 | [diff] [blame] | 536 | void pathspec_magic_names(unsigned magic, struct strbuf *out) |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 537 | { |
Brandon Williams | 93f3ddb | 2017-01-04 10:04:02 -0800 | [diff] [blame] | 538 | int i; |
| 539 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 540 | const struct pathspec_magic *m = pathspec_magic + i; |
| 541 | if (!(magic & m->bit)) |
| 542 | continue; |
Jeff King | 8e32caa | 2023-06-01 13:38:14 -0400 | [diff] [blame] | 543 | if (out->len) |
| 544 | strbuf_addstr(out, ", "); |
Brandon Williams | 2aee584 | 2017-01-04 10:04:03 -0800 | [diff] [blame] | 545 | |
| 546 | if (m->mnemonic) |
Jeff King | 8e32caa | 2023-06-01 13:38:14 -0400 | [diff] [blame] | 547 | strbuf_addf(out, _("'%s' (mnemonic: '%c')"), |
Brandon Williams | 2aee584 | 2017-01-04 10:04:03 -0800 | [diff] [blame] | 548 | m->name, m->mnemonic); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 549 | else |
Jeff King | 8e32caa | 2023-06-01 13:38:14 -0400 | [diff] [blame] | 550 | strbuf_addf(out, "'%s'", m->name); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 551 | } |
Jeff King | 8e32caa | 2023-06-01 13:38:14 -0400 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | static void NORETURN unsupported_magic(const char *pattern, |
| 555 | unsigned magic) |
| 556 | { |
| 557 | struct strbuf sb = STRBUF_INIT; |
| 558 | pathspec_magic_names(magic, &sb); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 559 | /* |
| 560 | * We may want to substitute "this command" with a command |
Ævar Arnfjörð Bjarmason | 5a7d41d | 2023-02-06 23:58:58 +0100 | [diff] [blame] | 561 | * name. E.g. when "git add -p" or "git add -i" dies when running |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 562 | * "checkout -p" |
| 563 | */ |
| 564 | die(_("%s: pathspec magic not supported by this command: %s"), |
| 565 | pattern, sb.buf); |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 566 | } |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 567 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 568 | void parse_pathspec(struct pathspec *pathspec, |
| 569 | unsigned magic_mask, unsigned flags, |
| 570 | const char *prefix, const char **argv) |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 571 | { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 572 | struct pathspec_item *item; |
| 573 | const char *entry = argv ? *argv : NULL; |
Emily Xie | 9e4e8a6 | 2017-06-06 23:33:08 -0400 | [diff] [blame] | 574 | int i, n, prefixlen, nr_exclude = 0; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 575 | |
| 576 | memset(pathspec, 0, sizeof(*pathspec)); |
| 577 | |
Nguyễn Thái Ngọc Duy | 6330a17 | 2013-07-14 15:35:32 +0700 | [diff] [blame] | 578 | if (flags & PATHSPEC_MAXDEPTH_VALID) |
| 579 | pathspec->magic |= PATHSPEC_MAXDEPTH; |
| 580 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 581 | /* No arguments, no prefix -> no pathspec */ |
| 582 | if (!entry && !prefix) |
| 583 | return; |
| 584 | |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 585 | if ((flags & PATHSPEC_PREFER_CWD) && |
| 586 | (flags & PATHSPEC_PREFER_FULL)) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 587 | BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible"); |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 588 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 589 | /* No arguments with prefix -> prefix pathspec */ |
| 590 | if (!entry) { |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 591 | if (flags & PATHSPEC_PREFER_FULL) |
| 592 | return; |
| 593 | |
| 594 | if (!(flags & PATHSPEC_PREFER_CWD)) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 595 | BUG("PATHSPEC_PREFER_CWD requires arguments"); |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 596 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 597 | pathspec->items = CALLOC_ARRAY(item, 1); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 598 | item->match = xstrdup(prefix); |
| 599 | item->original = xstrdup(prefix); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 600 | item->nowildcard_len = item->len = strlen(prefix); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 601 | item->prefix = item->len; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 602 | pathspec->nr = 1; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 603 | return; |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 604 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 605 | |
| 606 | n = 0; |
Emily Xie | d426430 | 2016-06-22 19:00:24 -0400 | [diff] [blame] | 607 | while (argv[n]) { |
Emily Xie | 9e4e8a6 | 2017-06-06 23:33:08 -0400 | [diff] [blame] | 608 | if (*argv[n] == '\0') |
| 609 | die("empty string is not a valid pathspec. " |
| 610 | "please use . instead if you meant to match all paths"); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 611 | n++; |
Emily Xie | d426430 | 2016-06-22 19:00:24 -0400 | [diff] [blame] | 612 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 613 | |
| 614 | pathspec->nr = n; |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 615 | ALLOC_ARRAY(pathspec->items, n + 1); |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 616 | item = pathspec->items; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 617 | prefixlen = prefix ? strlen(prefix) : 0; |
| 618 | |
| 619 | for (i = 0; i < n; i++) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 620 | entry = argv[i]; |
| 621 | |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 622 | init_pathspec_item(item + i, flags, prefix, prefixlen, entry); |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 623 | |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 624 | if (item[i].magic & PATHSPEC_EXCLUDE) |
| 625 | nr_exclude++; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 626 | if (item[i].magic & magic_mask) |
Brandon Williams | 2aee584 | 2017-01-04 10:04:03 -0800 | [diff] [blame] | 627 | unsupported_magic(entry, item[i].magic & magic_mask); |
Nguyễn Thái Ngọc Duy | 8745024 | 2013-07-14 15:35:34 +0700 | [diff] [blame] | 628 | |
| 629 | if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) && |
| 630 | has_symlink_leading_path(item[i].match, item[i].len)) { |
| 631 | die(_("pathspec '%s' is beyond a symbolic link"), entry); |
| 632 | } |
| 633 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 634 | if (item[i].nowildcard_len < item[i].len) |
| 635 | pathspec->has_wildcard = 1; |
| 636 | pathspec->magic |= item[i].magic; |
| 637 | } |
| 638 | |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 639 | /* |
| 640 | * If everything is an exclude pattern, add one positive pattern |
Ville Skyttä | 6412757 | 2017-06-25 13:20:41 +0300 | [diff] [blame] | 641 | * that matches everything. We allocated an extra one for this. |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 642 | */ |
| 643 | if (nr_exclude == n) { |
| 644 | int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen; |
Junio C Hamano | b02fdbc | 2022-05-29 15:39:51 -0700 | [diff] [blame] | 645 | init_pathspec_item(item + n, 0, prefix, plen, "."); |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 646 | pathspec->nr++; |
| 647 | } |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 648 | |
| 649 | if (pathspec->magic & PATHSPEC_MAXDEPTH) { |
| 650 | if (flags & PATHSPEC_KEEP_ORDER) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 651 | BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible"); |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 652 | QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp); |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 653 | } |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 654 | } |
| 655 | |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 656 | void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask, |
| 657 | unsigned flags, const char *prefix, |
| 658 | const char *file, int nul_term_line) |
| 659 | { |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 660 | struct strvec parsed_file = STRVEC_INIT; |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 661 | strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul : |
| 662 | strbuf_getline; |
| 663 | struct strbuf buf = STRBUF_INIT; |
| 664 | struct strbuf unquoted = STRBUF_INIT; |
| 665 | FILE *in; |
| 666 | |
| 667 | if (!strcmp(file, "-")) |
| 668 | in = stdin; |
| 669 | else |
| 670 | in = xfopen(file, "r"); |
| 671 | |
| 672 | while (getline_fn(&buf, in) != EOF) { |
| 673 | if (!nul_term_line && buf.buf[0] == '"') { |
| 674 | strbuf_reset(&unquoted); |
| 675 | if (unquote_c_style(&unquoted, buf.buf, NULL)) |
| 676 | die(_("line is badly quoted: %s"), buf.buf); |
| 677 | strbuf_swap(&buf, &unquoted); |
| 678 | } |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 679 | strvec_push(&parsed_file, buf.buf); |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 680 | strbuf_reset(&buf); |
| 681 | } |
| 682 | |
| 683 | strbuf_release(&unquoted); |
| 684 | strbuf_release(&buf); |
| 685 | if (in != stdin) |
| 686 | fclose(in); |
| 687 | |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 688 | parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.v); |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 689 | strvec_clear(&parsed_file); |
Alexandr Miloslavskiy | 24e4750 | 2019-11-19 16:48:51 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Nguyễn Thái Ngọc Duy | e4d92cd | 2013-07-14 15:35:27 +0700 | [diff] [blame] | 692 | void copy_pathspec(struct pathspec *dst, const struct pathspec *src) |
| 693 | { |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 694 | int i, j; |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 695 | |
Nguyễn Thái Ngọc Duy | e4d92cd | 2013-07-14 15:35:27 +0700 | [diff] [blame] | 696 | *dst = *src; |
René Scharfe | 6e57841 | 2023-01-01 22:16:48 +0100 | [diff] [blame] | 697 | DUP_ARRAY(dst->items, src->items, dst->nr); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 698 | |
| 699 | for (i = 0; i < dst->nr; i++) { |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 700 | struct pathspec_item *d = &dst->items[i]; |
| 701 | struct pathspec_item *s = &src->items[i]; |
| 702 | |
| 703 | d->match = xstrdup(s->match); |
| 704 | d->original = xstrdup(s->original); |
| 705 | |
René Scharfe | 6e57841 | 2023-01-01 22:16:48 +0100 | [diff] [blame] | 706 | DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 707 | for (j = 0; j < d->attr_match_nr; j++) { |
| 708 | const char *value = s->attr_match[j].value; |
| 709 | d->attr_match[j].value = xstrdup_or_null(value); |
| 710 | } |
| 711 | |
| 712 | d->attr_check = attr_check_dup(s->attr_check); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 713 | } |
Nguyễn Thái Ngọc Duy | e4d92cd | 2013-07-14 15:35:27 +0700 | [diff] [blame] | 714 | } |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 715 | |
Junio C Hamano | ed6e803 | 2016-06-02 14:09:22 -0700 | [diff] [blame] | 716 | void clear_pathspec(struct pathspec *pathspec) |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 717 | { |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 718 | int i, j; |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 719 | |
| 720 | for (i = 0; i < pathspec->nr; i++) { |
| 721 | free(pathspec->items[i].match); |
| 722 | free(pathspec->items[i].original); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 723 | |
Brandon Williams | 5ce10c0 | 2017-04-07 12:29:19 -0700 | [diff] [blame] | 724 | for (j = 0; j < pathspec->items[i].attr_match_nr; j++) |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 725 | free(pathspec->items[i].attr_match[j].value); |
| 726 | free(pathspec->items[i].attr_match); |
| 727 | |
| 728 | if (pathspec->items[i].attr_check) |
| 729 | attr_check_free(pathspec->items[i].attr_check); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 730 | } |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 731 | |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 732 | FREE_AND_NULL(pathspec->items); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 733 | pathspec->nr = 0; |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 734 | } |
Nguyễn Thái Ngọc Duy | 22af33b | 2018-11-18 17:47:59 +0100 | [diff] [blame] | 735 | |
Derrick Stolee | 847a9e5 | 2021-04-01 01:49:39 +0000 | [diff] [blame] | 736 | 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] | 737 | const char *name, int namelen, |
| 738 | const struct pathspec_item *item) |
| 739 | { |
| 740 | int i; |
| 741 | char *to_free = NULL; |
| 742 | |
| 743 | if (name[namelen]) |
| 744 | name = to_free = xmemdupz(name, namelen); |
| 745 | |
John Cai | 44451a2 | 2023-05-06 04:15:29 +0000 | [diff] [blame] | 746 | git_check_attr(istate, name, item->attr_check); |
Nguyễn Thái Ngọc Duy | 22af33b | 2018-11-18 17:47:59 +0100 | [diff] [blame] | 747 | |
| 748 | free(to_free); |
| 749 | |
| 750 | for (i = 0; i < item->attr_match_nr; i++) { |
| 751 | const char *value; |
| 752 | int matched; |
| 753 | enum attr_match_mode match_mode; |
| 754 | |
| 755 | value = item->attr_check->items[i].value; |
| 756 | match_mode = item->attr_match[i].match_mode; |
| 757 | |
| 758 | if (ATTR_TRUE(value)) |
| 759 | matched = (match_mode == MATCH_SET); |
| 760 | else if (ATTR_FALSE(value)) |
| 761 | matched = (match_mode == MATCH_UNSET); |
| 762 | else if (ATTR_UNSET(value)) |
| 763 | matched = (match_mode == MATCH_UNSPECIFIED); |
| 764 | else |
| 765 | matched = (match_mode == MATCH_VALUE && |
| 766 | !strcmp(item->attr_match[i].value, value)); |
| 767 | if (!matched) |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | return 1; |
| 772 | } |
Shaoxuan Yuan | b29ad38 | 2022-08-07 12:13:33 +0800 | [diff] [blame] | 773 | |
| 774 | int pathspec_needs_expanded_index(struct index_state *istate, |
| 775 | const struct pathspec *pathspec) |
| 776 | { |
| 777 | unsigned int i, pos; |
| 778 | int res = 0; |
| 779 | char *skip_worktree_seen = NULL; |
| 780 | |
| 781 | /* |
| 782 | * If index is not sparse, no index expansion is needed. |
| 783 | */ |
| 784 | if (!istate->sparse_index) |
| 785 | return 0; |
| 786 | |
| 787 | /* |
| 788 | * When using a magic pathspec, assume for the sake of simplicity that |
| 789 | * the index needs to be expanded to match all matchable files. |
| 790 | */ |
| 791 | if (pathspec->magic) |
| 792 | return 1; |
| 793 | |
| 794 | for (i = 0; i < pathspec->nr; i++) { |
| 795 | struct pathspec_item item = pathspec->items[i]; |
| 796 | |
| 797 | /* |
| 798 | * If the pathspec item has a wildcard, the index should be expanded |
| 799 | * if the pathspec has the possibility of matching a subset of entries inside |
| 800 | * of a sparse directory (but not the entire directory). |
| 801 | * |
| 802 | * If the pathspec item is a literal path, the index only needs to be expanded |
| 803 | * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't |
| 804 | * expand for in-cone files) and b) it doesn't match any sparse directories |
| 805 | * (since we can reset whole sparse directories without expanding them). |
| 806 | */ |
| 807 | if (item.nowildcard_len < item.len) { |
| 808 | /* |
| 809 | * Special case: if the pattern is a path inside the cone |
| 810 | * followed by only wildcards, the pattern cannot match |
| 811 | * partial sparse directories, so we know we don't need to |
| 812 | * expand the index. |
| 813 | * |
| 814 | * Examples: |
| 815 | * - in-cone/foo***: doesn't need expanded index |
| 816 | * - not-in-cone/bar*: may need expanded index |
| 817 | * - **.c: may need expanded index |
| 818 | */ |
| 819 | if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len && |
| 820 | path_in_cone_mode_sparse_checkout(item.original, istate)) |
| 821 | continue; |
| 822 | |
| 823 | for (pos = 0; pos < istate->cache_nr; pos++) { |
| 824 | struct cache_entry *ce = istate->cache[pos]; |
| 825 | |
| 826 | if (!S_ISSPARSEDIR(ce->ce_mode)) |
| 827 | continue; |
| 828 | |
| 829 | /* |
| 830 | * If the pre-wildcard length is longer than the sparse |
| 831 | * directory name and the sparse directory is the first |
| 832 | * component of the pathspec, need to expand the index. |
| 833 | */ |
| 834 | if (item.nowildcard_len > ce_namelen(ce) && |
| 835 | !strncmp(item.original, ce->name, ce_namelen(ce))) { |
| 836 | res = 1; |
| 837 | break; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * If the pre-wildcard length is shorter than the sparse |
| 842 | * directory and the pathspec does not match the whole |
| 843 | * directory, need to expand the index. |
| 844 | */ |
| 845 | if (!strncmp(item.original, ce->name, item.nowildcard_len) && |
| 846 | wildmatch(item.original, ce->name, 0)) { |
| 847 | res = 1; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | } else if (!path_in_cone_mode_sparse_checkout(item.original, istate) && |
| 852 | !matches_skip_worktree(pathspec, i, &skip_worktree_seen)) |
| 853 | res = 1; |
| 854 | |
| 855 | if (res > 0) |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | free(skip_worktree_seen); |
| 860 | return res; |
| 861 | } |