Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 1 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 2 | #include "cache.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" |
| 5 | #include "pathspec.h" |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 6 | #include "attr.h" |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 7 | |
| 8 | /* |
| 9 | * Finds which of the given pathspecs match items in the index. |
| 10 | * |
| 11 | * For each pathspec, sets the corresponding entry in the seen[] array |
| 12 | * (which should be specs items long, i.e. the same size as pathspec) |
| 13 | * to the nature of the "closest" (i.e. most specific) match found for |
| 14 | * that pathspec in the index, if it was a closer type of match than |
| 15 | * the existing entry. As an optimization, matching is skipped |
| 16 | * altogether if seen[] already only contains non-zero entries. |
| 17 | * |
| 18 | * If seen[] has not already been written to, it may make sense |
Adam Spiers | 4b78d7b | 2013-01-06 16:58:09 +0000 | [diff] [blame] | 19 | * to use find_pathspecs_matching_against_index() instead. |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 20 | */ |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 21 | void add_pathspec_matches_against_index(const struct pathspec *pathspec, |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 22 | const struct index_state *istate, |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 23 | char *seen) |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 24 | { |
| 25 | int num_unmatched = 0, i; |
| 26 | |
| 27 | /* |
| 28 | * Since we are walking the index as if we were walking the directory, |
| 29 | * we have to mark the matched pathspec as seen; otherwise we will |
| 30 | * mistakenly think that the user gave a pathspec that did not match |
| 31 | * anything. |
| 32 | */ |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 33 | for (i = 0; i < pathspec->nr; i++) |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 34 | if (!seen[i]) |
| 35 | num_unmatched++; |
| 36 | if (!num_unmatched) |
| 37 | return; |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 38 | for (i = 0; i < istate->cache_nr; i++) { |
| 39 | const struct cache_entry *ce = istate->cache[i]; |
Nguyễn Thái Ngọc Duy | 429bb40 | 2014-01-24 20:40:28 +0700 | [diff] [blame] | 40 | ce_path_match(ce, pathspec, seen); |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Finds which of the given pathspecs match items in the index. |
| 46 | * |
Adam Spiers | 4b78d7b | 2013-01-06 16:58:09 +0000 | [diff] [blame] | 47 | * This is a one-shot wrapper around add_pathspec_matches_against_index() |
| 48 | * which allocates, populates, and returns a seen[] array indicating the |
| 49 | * nature of the "closest" (i.e. most specific) matches which each of the |
| 50 | * given pathspecs achieves against all items in the index. |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 51 | */ |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 52 | char *find_pathspecs_matching_against_index(const struct pathspec *pathspec, |
| 53 | const struct index_state *istate) |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 54 | { |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 55 | char *seen = xcalloc(pathspec->nr, 1); |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 56 | add_pathspec_matches_against_index(pathspec, istate, seen); |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 57 | return seen; |
| 58 | } |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 59 | |
| 60 | /* |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 61 | * Magic pathspec |
| 62 | * |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 63 | * Possible future magic semantics include stuff like: |
| 64 | * |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 65 | * { PATHSPEC_RECURSIVE, '*', "recursive" }, |
| 66 | * { PATHSPEC_REGEXP, '\0', "regexp" }, |
| 67 | * |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 68 | */ |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 69 | |
| 70 | static struct pathspec_magic { |
| 71 | unsigned bit; |
| 72 | char mnemonic; /* this cannot be ':'! */ |
| 73 | const char *name; |
| 74 | } pathspec_magic[] = { |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 75 | { PATHSPEC_FROMTOP, '/', "top" }, |
| 76 | { PATHSPEC_LITERAL, '\0', "literal" }, |
| 77 | { PATHSPEC_GLOB, '\0', "glob" }, |
| 78 | { PATHSPEC_ICASE, '\0', "icase" }, |
| 79 | { PATHSPEC_EXCLUDE, '!', "exclude" }, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 80 | { PATHSPEC_ATTR, '\0', "attr" }, |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 81 | }; |
| 82 | |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 83 | 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] | 84 | { |
| 85 | int i; |
| 86 | strbuf_addstr(sb, ":("); |
| 87 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 88 | if (magic & pathspec_magic[i].bit) { |
Nguyễn Thái Ngọc Duy | 1649612 | 2013-12-06 14:30:49 +0700 | [diff] [blame] | 89 | if (sb->buf[sb->len - 1] != '(') |
| 90 | strbuf_addch(sb, ','); |
| 91 | strbuf_addstr(sb, pathspec_magic[i].name); |
| 92 | } |
| 93 | strbuf_addf(sb, ",prefix:%d)", prefixlen); |
| 94 | } |
| 95 | |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 96 | static size_t strcspn_escaped(const char *s, const char *stop) |
| 97 | { |
| 98 | const char *i; |
| 99 | |
| 100 | for (i = s; *i; i++) { |
| 101 | /* skip the escaped character */ |
| 102 | if (i[0] == '\\' && i[1]) { |
| 103 | i++; |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | if (strchr(stop, *i)) |
| 108 | break; |
| 109 | } |
| 110 | return i - s; |
| 111 | } |
| 112 | |
| 113 | static inline int invalid_value_char(const char ch) |
| 114 | { |
| 115 | if (isalnum(ch) || strchr(",-_", ch)) |
| 116 | return 0; |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | static char *attr_value_unescape(const char *value) |
| 121 | { |
| 122 | const char *src; |
| 123 | char *dst, *ret; |
| 124 | |
| 125 | ret = xmallocz(strlen(value)); |
| 126 | for (src = value, dst = ret; *src; src++, dst++) { |
| 127 | if (*src == '\\') { |
| 128 | if (!src[1]) |
| 129 | die(_("Escape character '\\' not allowed as " |
| 130 | "last character in attr value")); |
| 131 | src++; |
| 132 | } |
| 133 | if (invalid_value_char(*src)) |
| 134 | die("cannot use '%c' for value matching", *src); |
| 135 | *dst = *src; |
| 136 | } |
| 137 | *dst = '\0'; |
| 138 | return ret; |
| 139 | } |
| 140 | |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 141 | static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value) |
| 142 | { |
| 143 | struct string_list_item *si; |
| 144 | struct string_list list = STRING_LIST_INIT_DUP; |
| 145 | |
| 146 | if (item->attr_check || item->attr_match) |
| 147 | die(_("Only one 'attr:' specification is allowed.")); |
| 148 | |
| 149 | if (!value || !*value) |
| 150 | die(_("attr spec must not be empty")); |
| 151 | |
| 152 | string_list_split(&list, value, ' ', -1); |
| 153 | string_list_remove_empty_items(&list, 0); |
| 154 | |
| 155 | item->attr_check = attr_check_alloc(); |
| 156 | item->attr_match = xcalloc(list.nr, sizeof(struct attr_match)); |
| 157 | |
| 158 | for_each_string_list_item(si, &list) { |
| 159 | size_t attr_len; |
| 160 | char *attr_name; |
| 161 | const struct git_attr *a; |
| 162 | |
| 163 | int j = item->attr_match_nr++; |
| 164 | const char *attr = si->string; |
| 165 | struct attr_match *am = &item->attr_match[j]; |
| 166 | |
| 167 | switch (*attr) { |
| 168 | case '!': |
| 169 | am->match_mode = MATCH_UNSPECIFIED; |
| 170 | attr++; |
| 171 | attr_len = strlen(attr); |
| 172 | break; |
| 173 | case '-': |
| 174 | am->match_mode = MATCH_UNSET; |
| 175 | attr++; |
| 176 | attr_len = strlen(attr); |
| 177 | break; |
| 178 | default: |
| 179 | attr_len = strcspn(attr, "="); |
| 180 | if (attr[attr_len] != '=') |
| 181 | am->match_mode = MATCH_SET; |
| 182 | else { |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 183 | const char *v = &attr[attr_len + 1]; |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 184 | am->match_mode = MATCH_VALUE; |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 185 | am->value = attr_value_unescape(v); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 186 | } |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | attr_name = xmemdupz(attr, attr_len); |
| 191 | a = git_attr(attr_name); |
| 192 | if (!a) |
| 193 | die(_("invalid attribute name %s"), attr_name); |
| 194 | |
| 195 | attr_check_append(item->attr_check, a); |
| 196 | |
| 197 | free(attr_name); |
| 198 | } |
| 199 | |
| 200 | if (item->attr_check->nr != item->attr_match_nr) |
| 201 | die("BUG: should have same number of entries"); |
| 202 | |
| 203 | string_list_clear(&list, 0); |
| 204 | } |
| 205 | |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 206 | static inline int get_literal_global(void) |
| 207 | { |
| 208 | static int literal = -1; |
| 209 | |
| 210 | if (literal < 0) |
| 211 | literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0); |
| 212 | |
| 213 | return literal; |
| 214 | } |
| 215 | |
| 216 | static inline int get_glob_global(void) |
| 217 | { |
| 218 | static int glob = -1; |
| 219 | |
| 220 | if (glob < 0) |
| 221 | glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0); |
| 222 | |
| 223 | return glob; |
| 224 | } |
| 225 | |
| 226 | static inline int get_noglob_global(void) |
| 227 | { |
| 228 | static int noglob = -1; |
| 229 | |
| 230 | if (noglob < 0) |
| 231 | noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0); |
| 232 | |
| 233 | return noglob; |
| 234 | } |
| 235 | |
| 236 | static inline int get_icase_global(void) |
| 237 | { |
| 238 | static int icase = -1; |
| 239 | |
| 240 | if (icase < 0) |
| 241 | icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0); |
| 242 | |
| 243 | return icase; |
| 244 | } |
| 245 | |
| 246 | static int get_global_magic(int element_magic) |
| 247 | { |
| 248 | int global_magic = 0; |
| 249 | |
| 250 | if (get_literal_global()) |
| 251 | global_magic |= PATHSPEC_LITERAL; |
| 252 | |
| 253 | /* --glob-pathspec is overridden by :(literal) */ |
| 254 | if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL)) |
| 255 | global_magic |= PATHSPEC_GLOB; |
| 256 | |
| 257 | if (get_glob_global() && get_noglob_global()) |
| 258 | die(_("global 'glob' and 'noglob' pathspec settings are incompatible")); |
| 259 | |
| 260 | if (get_icase_global()) |
| 261 | global_magic |= PATHSPEC_ICASE; |
| 262 | |
| 263 | if ((global_magic & PATHSPEC_LITERAL) && |
| 264 | (global_magic & ~PATHSPEC_LITERAL)) |
| 265 | die(_("global 'literal' pathspec setting is incompatible " |
| 266 | "with all other global pathspec settings")); |
| 267 | |
| 268 | /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ |
| 269 | if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB)) |
| 270 | global_magic |= PATHSPEC_LITERAL; |
| 271 | |
| 272 | return global_magic; |
| 273 | } |
| 274 | |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 275 | /* |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 276 | * Parse the pathspec element looking for long magic |
| 277 | * |
| 278 | * saves all magic in 'magic' |
| 279 | * if prefix magic is used, save the prefix length in 'prefix_len' |
| 280 | * returns the position in 'elem' after all magic has been parsed |
| 281 | */ |
| 282 | static const char *parse_long_magic(unsigned *magic, int *prefix_len, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 283 | struct pathspec_item *item, |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 284 | const char *elem) |
| 285 | { |
| 286 | const char *pos; |
| 287 | const char *nextat; |
| 288 | |
| 289 | for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) { |
Brandon Williams | c5af19f | 2017-03-13 11:23:22 -0700 | [diff] [blame] | 290 | size_t len = strcspn_escaped(pos, ",)"); |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 291 | int i; |
| 292 | |
| 293 | if (pos[len] == ',') |
| 294 | nextat = pos + len + 1; /* handle ',' */ |
| 295 | else |
| 296 | nextat = pos + len; /* handle ')' and '\0' */ |
| 297 | |
| 298 | if (!len) |
| 299 | continue; |
| 300 | |
| 301 | if (starts_with(pos, "prefix:")) { |
| 302 | char *endptr; |
| 303 | *prefix_len = strtol(pos + 7, &endptr, 10); |
| 304 | if (endptr - pos != len) |
| 305 | die(_("invalid parameter for pathspec magic 'prefix'")); |
| 306 | continue; |
| 307 | } |
| 308 | |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 309 | if (starts_with(pos, "attr:")) { |
| 310 | char *attr_body = xmemdupz(pos + 5, len - 5); |
| 311 | parse_pathspec_attr_match(item, attr_body); |
| 312 | *magic |= PATHSPEC_ATTR; |
| 313 | free(attr_body); |
| 314 | continue; |
| 315 | } |
| 316 | |
Brandon Williams | 8881fde | 2017-01-04 10:04:07 -0800 | [diff] [blame] | 317 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { |
| 318 | if (strlen(pathspec_magic[i].name) == len && |
| 319 | !strncmp(pathspec_magic[i].name, pos, len)) { |
| 320 | *magic |= pathspec_magic[i].bit; |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (ARRAY_SIZE(pathspec_magic) <= i) |
| 326 | die(_("Invalid pathspec magic '%.*s' in '%s'"), |
| 327 | (int) len, pos, elem); |
| 328 | } |
| 329 | |
| 330 | if (*pos != ')') |
| 331 | die(_("Missing ')' at the end of pathspec magic in '%s'"), |
| 332 | elem); |
| 333 | pos++; |
| 334 | |
| 335 | return pos; |
| 336 | } |
| 337 | |
| 338 | /* |
Brandon Williams | b4bebdc | 2017-01-04 10:04:06 -0800 | [diff] [blame] | 339 | * Parse the pathspec element looking for short magic |
| 340 | * |
| 341 | * saves all magic in 'magic' |
| 342 | * returns the position in 'elem' after all magic has been parsed |
| 343 | */ |
| 344 | static const char *parse_short_magic(unsigned *magic, const char *elem) |
| 345 | { |
| 346 | const char *pos; |
| 347 | |
| 348 | for (pos = elem + 1; *pos && *pos != ':'; pos++) { |
| 349 | char ch = *pos; |
| 350 | int i; |
| 351 | |
Linus Torvalds | 42ebeb9 | 2017-02-07 21:05:28 -0800 | [diff] [blame] | 352 | /* Special case alias for '!' */ |
| 353 | if (ch == '^') { |
| 354 | *magic |= PATHSPEC_EXCLUDE; |
| 355 | continue; |
| 356 | } |
| 357 | |
Brandon Williams | b4bebdc | 2017-01-04 10:04:06 -0800 | [diff] [blame] | 358 | if (!is_pathspec_magic(ch)) |
| 359 | break; |
| 360 | |
| 361 | for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) { |
| 362 | if (pathspec_magic[i].mnemonic == ch) { |
| 363 | *magic |= pathspec_magic[i].bit; |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (ARRAY_SIZE(pathspec_magic) <= i) |
| 369 | die(_("Unimplemented pathspec magic '%c' in '%s'"), |
| 370 | ch, elem); |
| 371 | } |
| 372 | |
| 373 | if (*pos == ':') |
| 374 | pos++; |
| 375 | |
| 376 | return pos; |
| 377 | } |
| 378 | |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 379 | static const char *parse_element_magic(unsigned *magic, int *prefix_len, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 380 | struct pathspec_item *item, |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 381 | const char *elem) |
| 382 | { |
| 383 | if (elem[0] != ':' || get_literal_global()) |
| 384 | return elem; /* nothing to do */ |
| 385 | else if (elem[1] == '(') |
| 386 | /* longhand */ |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 387 | return parse_long_magic(magic, prefix_len, item, elem); |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 388 | else |
| 389 | /* shorthand */ |
| 390 | return parse_short_magic(magic, elem); |
| 391 | } |
| 392 | |
Brandon Williams | b4bebdc | 2017-01-04 10:04:06 -0800 | [diff] [blame] | 393 | /* |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 394 | * 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] | 395 | */ |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 396 | static void init_pathspec_item(struct pathspec_item *item, unsigned flags, |
| 397 | const char *prefix, int prefixlen, |
| 398 | const char *elt) |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 399 | { |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 400 | unsigned magic = 0, element_magic = 0; |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 401 | const char *copyfrom = elt; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 402 | char *match; |
Brandon Williams | 5590215 | 2017-01-04 10:04:09 -0800 | [diff] [blame] | 403 | int pathspec_prefix = -1; |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 404 | |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 405 | item->attr_check = NULL; |
| 406 | item->attr_match = NULL; |
| 407 | item->attr_match_nr = 0; |
| 408 | |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 409 | /* PATHSPEC_LITERAL_PATH ignores magic */ |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 410 | if (flags & PATHSPEC_LITERAL_PATH) { |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 411 | magic = PATHSPEC_LITERAL; |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 412 | } else { |
| 413 | copyfrom = parse_element_magic(&element_magic, |
| 414 | &pathspec_prefix, |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 415 | item, |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 416 | elt); |
| 417 | magic |= element_magic; |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 418 | magic |= get_global_magic(element_magic); |
Brandon Williams | 1b6112c | 2017-01-04 10:04:08 -0800 | [diff] [blame] | 419 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 420 | |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 421 | item->magic = magic; |
| 422 | |
Nguyễn Thái Ngọc Duy | 233c3e6 | 2013-07-14 15:36:04 +0700 | [diff] [blame] | 423 | if (pathspec_prefix >= 0 && |
| 424 | (prefixlen || (prefix && *prefix))) |
| 425 | die("BUG: 'prefix' magic is supposed to be used at worktree's root"); |
| 426 | |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 427 | if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB)) |
| 428 | die(_("%s: 'literal' and 'glob' are incompatible"), elt); |
| 429 | |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 430 | /* 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] | 431 | if (pathspec_prefix >= 0) { |
| 432 | match = xstrdup(copyfrom); |
| 433 | prefixlen = pathspec_prefix; |
| 434 | } else if (magic & PATHSPEC_FROMTOP) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 435 | match = xstrdup(copyfrom); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 436 | prefixlen = 0; |
| 437 | } else { |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 438 | match = prefix_path_gently(prefix, prefixlen, |
| 439 | &prefixlen, copyfrom); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 440 | if (!match) |
| 441 | die(_("%s: '%s' is outside repository"), elt, copyfrom); |
| 442 | } |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 443 | |
Brandon Williams | 34305f7 | 2017-01-04 10:04:00 -0800 | [diff] [blame] | 444 | item->match = match; |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 445 | item->len = strlen(item->match); |
| 446 | item->prefix = prefixlen; |
| 447 | |
Nguyễn Thái Ngọc Duy | dad2586 | 2013-07-14 15:35:35 +0700 | [diff] [blame] | 448 | /* |
| 449 | * Prefix the pathspec (keep all magic) and assign to |
| 450 | * original. Useful for passing to another command. |
| 451 | */ |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 452 | if ((flags & PATHSPEC_PREFIX_ORIGIN) && |
Patrick Steinhardt | be4dbbb | 2017-04-04 11:16:56 +0200 | [diff] [blame] | 453 | !get_literal_global()) { |
Nguyễn Thái Ngọc Duy | dad2586 | 2013-07-14 15:35:35 +0700 | [diff] [blame] | 454 | struct strbuf sb = STRBUF_INIT; |
Brandon Williams | 5d8f084 | 2017-01-04 10:04:04 -0800 | [diff] [blame] | 455 | |
| 456 | /* Preserve the actual prefix length of each pattern */ |
| 457 | prefix_magic(&sb, prefixlen, element_magic); |
| 458 | |
Nguyễn Thái Ngọc Duy | dad2586 | 2013-07-14 15:35:35 +0700 | [diff] [blame] | 459 | strbuf_addstr(&sb, match); |
| 460 | item->original = strbuf_detach(&sb, NULL); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 461 | } else { |
| 462 | item->original = xstrdup(elt); |
| 463 | } |
Nguyễn Thái Ngọc Duy | b69bb3f | 2013-07-14 15:35:33 +0700 | [diff] [blame] | 464 | |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 465 | if (magic & PATHSPEC_LITERAL) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 466 | item->nowildcard_len = item->len; |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 467 | } else { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 468 | item->nowildcard_len = simple_length(item->match); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 469 | if (item->nowildcard_len < prefixlen) |
| 470 | item->nowildcard_len = prefixlen; |
| 471 | } |
Brandon Williams | 4f1bf4d | 2017-01-04 10:04:10 -0800 | [diff] [blame] | 472 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 473 | item->flags = 0; |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 474 | if (magic & PATHSPEC_GLOB) { |
| 475 | /* |
| 476 | * FIXME: should we enable ONESTAR in _GLOB for |
| 477 | * pattern "* * / * . c"? |
| 478 | */ |
| 479 | } else { |
| 480 | if (item->nowildcard_len < item->len && |
| 481 | item->match[item->nowildcard_len] == '*' && |
| 482 | no_wildcard(item->match + item->nowildcard_len + 1)) |
| 483 | item->flags |= PATHSPEC_ONESTAR; |
| 484 | } |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 485 | |
| 486 | /* sanity checks, pathspec matchers assume these are sane */ |
Stefan Beller | 2d81c48 | 2017-01-09 15:16:50 -0800 | [diff] [blame] | 487 | if (item->nowildcard_len > item->len || |
| 488 | item->prefix > item->len) { |
Brandon Williams | 2c3b407 | 2017-05-09 12:17:58 -0700 | [diff] [blame] | 489 | die ("BUG: error initializing pathspec_item"); |
Stefan Beller | 2d81c48 | 2017-01-09 15:16:50 -0800 | [diff] [blame] | 490 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static int pathspec_item_cmp(const void *a_, const void *b_) |
| 494 | { |
| 495 | struct pathspec_item *a, *b; |
| 496 | |
| 497 | a = (struct pathspec_item *)a_; |
| 498 | b = (struct pathspec_item *)b_; |
| 499 | return strcmp(a->match, b->match); |
| 500 | } |
| 501 | |
| 502 | static void NORETURN unsupported_magic(const char *pattern, |
Brandon Williams | 2aee584 | 2017-01-04 10:04:03 -0800 | [diff] [blame] | 503 | unsigned magic) |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 504 | { |
| 505 | struct strbuf sb = STRBUF_INIT; |
Brandon Williams | 93f3ddb | 2017-01-04 10:04:02 -0800 | [diff] [blame] | 506 | int i; |
| 507 | 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] | 508 | const struct pathspec_magic *m = pathspec_magic + i; |
| 509 | if (!(magic & m->bit)) |
| 510 | continue; |
| 511 | if (sb.len) |
Brandon Williams | 2aee584 | 2017-01-04 10:04:03 -0800 | [diff] [blame] | 512 | strbuf_addstr(&sb, ", "); |
| 513 | |
| 514 | if (m->mnemonic) |
| 515 | strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"), |
| 516 | m->name, m->mnemonic); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 517 | else |
| 518 | strbuf_addf(&sb, "'%s'", m->name); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 519 | } |
| 520 | /* |
| 521 | * We may want to substitute "this command" with a command |
| 522 | * name. E.g. when add--interactive dies when running |
| 523 | * "checkout -p" |
| 524 | */ |
| 525 | die(_("%s: pathspec magic not supported by this command: %s"), |
| 526 | pattern, sb.buf); |
Adam Spiers | 9d67b61 | 2013-01-06 16:58:10 +0000 | [diff] [blame] | 527 | } |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 528 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 529 | void parse_pathspec(struct pathspec *pathspec, |
| 530 | unsigned magic_mask, unsigned flags, |
| 531 | const char *prefix, const char **argv) |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 532 | { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 533 | struct pathspec_item *item; |
| 534 | const char *entry = argv ? *argv : NULL; |
Emily Xie | 9e4e8a6 | 2017-06-06 23:33:08 -0400 | [diff] [blame] | 535 | int i, n, prefixlen, nr_exclude = 0; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 536 | |
| 537 | memset(pathspec, 0, sizeof(*pathspec)); |
| 538 | |
Nguyễn Thái Ngọc Duy | 6330a17 | 2013-07-14 15:35:32 +0700 | [diff] [blame] | 539 | if (flags & PATHSPEC_MAXDEPTH_VALID) |
| 540 | pathspec->magic |= PATHSPEC_MAXDEPTH; |
| 541 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 542 | /* No arguments, no prefix -> no pathspec */ |
| 543 | if (!entry && !prefix) |
| 544 | return; |
| 545 | |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 546 | if ((flags & PATHSPEC_PREFER_CWD) && |
| 547 | (flags & PATHSPEC_PREFER_FULL)) |
| 548 | die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible"); |
| 549 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 550 | /* No arguments with prefix -> prefix pathspec */ |
| 551 | if (!entry) { |
Nguyễn Thái Ngọc Duy | fc12261 | 2013-07-14 15:35:30 +0700 | [diff] [blame] | 552 | if (flags & PATHSPEC_PREFER_FULL) |
| 553 | return; |
| 554 | |
| 555 | if (!(flags & PATHSPEC_PREFER_CWD)) |
| 556 | die("BUG: PATHSPEC_PREFER_CWD requires arguments"); |
| 557 | |
René Scharfe | 51a60f5 | 2014-07-19 15:56:26 +0200 | [diff] [blame] | 558 | pathspec->items = item = xcalloc(1, sizeof(*item)); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 559 | item->match = xstrdup(prefix); |
| 560 | item->original = xstrdup(prefix); |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 561 | item->nowildcard_len = item->len = strlen(prefix); |
Nguyễn Thái Ngọc Duy | 645a29c | 2013-07-14 15:36:03 +0700 | [diff] [blame] | 562 | item->prefix = item->len; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 563 | pathspec->nr = 1; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 564 | return; |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 565 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 566 | |
| 567 | n = 0; |
Emily Xie | d426430 | 2016-06-22 19:00:24 -0400 | [diff] [blame] | 568 | while (argv[n]) { |
Emily Xie | 9e4e8a6 | 2017-06-06 23:33:08 -0400 | [diff] [blame] | 569 | if (*argv[n] == '\0') |
| 570 | die("empty string is not a valid pathspec. " |
| 571 | "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] | 572 | n++; |
Emily Xie | d426430 | 2016-06-22 19:00:24 -0400 | [diff] [blame] | 573 | } |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 574 | |
| 575 | pathspec->nr = n; |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 576 | ALLOC_ARRAY(pathspec->items, n + 1); |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 577 | item = pathspec->items; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 578 | prefixlen = prefix ? strlen(prefix) : 0; |
| 579 | |
| 580 | for (i = 0; i < n; i++) { |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 581 | entry = argv[i]; |
| 582 | |
Brandon Williams | 27ec428 | 2017-01-04 10:04:11 -0800 | [diff] [blame] | 583 | init_pathspec_item(item + i, flags, prefix, prefixlen, entry); |
Brandon Williams | db7e859 | 2017-01-04 10:04:05 -0800 | [diff] [blame] | 584 | |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 585 | if (item[i].magic & PATHSPEC_EXCLUDE) |
| 586 | nr_exclude++; |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 587 | if (item[i].magic & magic_mask) |
Brandon Williams | 2aee584 | 2017-01-04 10:04:03 -0800 | [diff] [blame] | 588 | 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] | 589 | |
| 590 | if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) && |
| 591 | has_symlink_leading_path(item[i].match, item[i].len)) { |
| 592 | die(_("pathspec '%s' is beyond a symbolic link"), entry); |
| 593 | } |
| 594 | |
Nguyễn Thái Ngọc Duy | 87323bd | 2013-07-14 15:35:28 +0700 | [diff] [blame] | 595 | if (item[i].nowildcard_len < item[i].len) |
| 596 | pathspec->has_wildcard = 1; |
| 597 | pathspec->magic |= item[i].magic; |
| 598 | } |
| 599 | |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 600 | /* |
| 601 | * If everything is an exclude pattern, add one positive pattern |
Ville Skyttä | 6412757 | 2017-06-25 13:20:41 +0300 | [diff] [blame] | 602 | * that matches everything. We allocated an extra one for this. |
Linus Torvalds | 859b7f1 | 2017-02-07 21:08:15 -0800 | [diff] [blame] | 603 | */ |
| 604 | if (nr_exclude == n) { |
| 605 | int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen; |
| 606 | init_pathspec_item(item + n, 0, prefix, plen, ""); |
| 607 | pathspec->nr++; |
| 608 | } |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 609 | |
| 610 | if (pathspec->magic & PATHSPEC_MAXDEPTH) { |
| 611 | if (flags & PATHSPEC_KEEP_ORDER) |
| 612 | die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible"); |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 613 | 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] | 614 | } |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 615 | } |
| 616 | |
Nguyễn Thái Ngọc Duy | e4d92cd | 2013-07-14 15:35:27 +0700 | [diff] [blame] | 617 | void copy_pathspec(struct pathspec *dst, const struct pathspec *src) |
| 618 | { |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 619 | int i, j; |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 620 | |
Nguyễn Thái Ngọc Duy | e4d92cd | 2013-07-14 15:35:27 +0700 | [diff] [blame] | 621 | *dst = *src; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 622 | ALLOC_ARRAY(dst->items, dst->nr); |
René Scharfe | 45ccef8 | 2016-09-25 09:24:03 +0200 | [diff] [blame] | 623 | COPY_ARRAY(dst->items, src->items, dst->nr); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 624 | |
| 625 | for (i = 0; i < dst->nr; i++) { |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 626 | struct pathspec_item *d = &dst->items[i]; |
| 627 | struct pathspec_item *s = &src->items[i]; |
| 628 | |
| 629 | d->match = xstrdup(s->match); |
| 630 | d->original = xstrdup(s->original); |
| 631 | |
| 632 | ALLOC_ARRAY(d->attr_match, d->attr_match_nr); |
| 633 | COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr); |
| 634 | for (j = 0; j < d->attr_match_nr; j++) { |
| 635 | const char *value = s->attr_match[j].value; |
| 636 | d->attr_match[j].value = xstrdup_or_null(value); |
| 637 | } |
| 638 | |
| 639 | d->attr_check = attr_check_dup(s->attr_check); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 640 | } |
Nguyễn Thái Ngọc Duy | e4d92cd | 2013-07-14 15:35:27 +0700 | [diff] [blame] | 641 | } |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 642 | |
Junio C Hamano | ed6e803 | 2016-06-02 14:09:22 -0700 | [diff] [blame] | 643 | void clear_pathspec(struct pathspec *pathspec) |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 644 | { |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 645 | int i, j; |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 646 | |
| 647 | for (i = 0; i < pathspec->nr; i++) { |
| 648 | free(pathspec->items[i].match); |
| 649 | free(pathspec->items[i].original); |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 650 | |
Brandon Williams | 5ce10c0 | 2017-04-07 12:29:19 -0700 | [diff] [blame] | 651 | for (j = 0; j < pathspec->items[i].attr_match_nr; j++) |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 652 | free(pathspec->items[i].attr_match[j].value); |
| 653 | free(pathspec->items[i].attr_match); |
| 654 | |
| 655 | if (pathspec->items[i].attr_check) |
| 656 | attr_check_free(pathspec->items[i].attr_check); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 657 | } |
Brandon Williams | b0db704 | 2017-03-13 11:23:21 -0700 | [diff] [blame] | 658 | |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 659 | FREE_AND_NULL(pathspec->items); |
Brandon Williams | 8aee769 | 2017-01-04 10:04:01 -0800 | [diff] [blame] | 660 | pathspec->nr = 0; |
Adam Spiers | 512aaf9 | 2013-01-06 16:58:11 +0000 | [diff] [blame] | 661 | } |