dir.c: remove an implicit dependency on the_index in pathspec code
Make the match_patchspec API and friends take an index_state instead of assuming the_index in dir.c. All external call sites are converted blindly to keep the patch simple and retain current behavior. Individual call sites may receive further updates to use the right index instead of the_index. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Aug 13, 2018 at 18:14 UTC
6d2df284e7f4d7cd9f46992282ef59a72a9db527
21 files changed
+54
-45
archive.c
+1
-1
@@ -313,7 +313,7 @@ static int reject_entry(const struct object_id *oid, struct strbuf *base,
313
struct strbuf sb = STRBUF_INIT;
314
strbuf_addbuf(&sb, base);
315
strbuf_addstr(&sb, filename);
316
- if (!match_pathspec(context, sb.buf, sb.len, 0, NULL, 1))
316
+ if (!match_pathspec(&the_index, context, sb.buf, sb.len, 0, NULL, 1))
317
ret = READ_TREE_RECURSIVE;
318
strbuf_release(&sb);
319
}
builtin/add.c
+3
-3
@@ -40,7 +40,7 @@ static void chmod_pathspec(struct pathspec *pathspec, char flip)
40
for (i = 0; i < active_nr; i++) {
41
struct cache_entry *ce = active_cache[i];
42
43
- if (pathspec && !ce_path_match(ce, pathspec, NULL))
43
+ if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
44
continue;
45
46
if (chmod_cache_entry(ce, flip) < 0)
@@ -135,7 +135,7 @@ static int renormalize_tracked_files(const struct pathspec *pathspec, int flags)
135
continue; /* do not touch unmerged paths */
136
if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode))
137
continue; /* do not touch non blobs */
138
- if (pathspec && !ce_path_match(ce, pathspec, NULL))
138
+ if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
139
continue;
140
retval |= add_file_to_cache(ce->name, flags | HASH_RENORMALIZE);
141
}
@@ -155,7 +155,7 @@ static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,
155
i = dir->nr;
156
while (--i >= 0) {
157
struct dir_entry *entry = *src++;
158
- if (dir_path_match(entry, pathspec, prefix, seen))
158
+ if (dir_path_match(&the_index, entry, pathspec, prefix, seen))
159
*dst++ = entry;
160
}
161
dir->nr = dst - dir->entries;
builtin/checkout.c
+1
-1
@@ -318,7 +318,7 @@ static int checkout_paths(const struct checkout_opts *opts,
318
* match_pathspec() for _all_ entries when
319
* opts->source_tree != NULL.
320
*/
321
- if (ce_path_match(ce, &opts->pathspec, ps_matched))
321
+ if (ce_path_match(&the_index, ce, &opts->pathspec, ps_matched))
322
ce->ce_flags |= CE_MATCHED;
323
}
324
builtin/clean.c
+1
-1
@@ -976,7 +976,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
976
continue;
977
978
if (pathspec.nr)
979
- matches = dir_path_match(ent, &pathspec, 0, NULL);
979
+ matches = dir_path_match(&the_index, ent, &pathspec, 0, NULL);
980
981
if (pathspec.nr && !matches)
982
continue;
builtin/commit.c
+1
-1
@@ -251,7 +251,7 @@ static int list_paths(struct string_list *list, const char *with_tree,
251
252
if (ce->ce_flags & CE_UPDATE)
253
continue;
254
- if (!ce_path_match(ce, pattern, m))
254
+ if (!ce_path_match(&the_index, ce, pattern, m))
255
continue;
256
item = string_list_insert(list, ce->name);
257
if (ce_skip_worktree(ce))
builtin/grep.c
+3
-3
@@ -497,7 +497,7 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
497
strbuf_addstr(&name, ce->name);
498
499
if (S_ISREG(ce->ce_mode) &&
500
- match_pathspec(pathspec, name.buf, name.len, 0, NULL,
500
+ match_pathspec(&the_index, pathspec, name.buf, name.len, 0, NULL,
501
S_ISDIR(ce->ce_mode) ||
502
S_ISGITLINK(ce->ce_mode))) {
503
/*
@@ -515,7 +515,7 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
515
hit |= grep_file(opt, name.buf);
516
}
517
} else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
518
- submodule_path_match(pathspec, name.buf, NULL)) {
518
+ submodule_path_match(&the_index, pathspec, name.buf, NULL)) {
519
hit |= grep_submodule(opt, repo, pathspec, NULL, ce->name, ce->name);
520
} else {
521
continue;
@@ -679,7 +679,7 @@ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
679
680
fill_directory(&dir, &the_index, pathspec);
681
for (i = 0; i < dir.nr; i++) {
682
- if (!dir_path_match(dir.entries[i], pathspec, 0, NULL))
682
+ if (!dir_path_match(&the_index, dir.entries[i], pathspec, 0, NULL))
683
continue;
684
hit |= grep_file(opt, dir.entries[i]->name);
685
if (hit && opt->status_only)
builtin/ls-files.c
+3
-3
@@ -128,7 +128,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
128
if (len > ent->len)
129
die("git ls-files: internal error - directory entry not superset of prefix");
130
131
- if (!dir_path_match(ent, &pathspec, len, ps_matched))
131
+ if (!dir_path_match(&the_index, ent, &pathspec, len, ps_matched))
132
return;
133
134
fputs(tag, stdout);
@@ -228,7 +228,7 @@ static void show_ce(struct repository *repo, struct dir_struct *dir,
228
if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
229
is_submodule_active(repo, ce->name)) {
230
show_submodule(repo, dir, ce->name);
231
- } else if (match_pathspec(&pathspec, fullname, strlen(fullname),
231
+ } else if (match_pathspec(&the_index, &pathspec, fullname, strlen(fullname),
232
max_prefix_len, ps_matched,
233
S_ISDIR(ce->ce_mode) ||
234
S_ISGITLINK(ce->ce_mode))) {
@@ -264,7 +264,7 @@ static void show_ru_info(const struct index_state *istate)
264
len = strlen(path);
265
if (len < max_prefix_len)
266
continue; /* outside of the prefix */
267
- if (!match_pathspec(&pathspec, path, len,
267
+ if (!match_pathspec(&the_index, &pathspec, path, len,
268
max_prefix_len, ps_matched, 0))
269
continue; /* uninterested */
270
for (i = 0; i < 3; i++) {
builtin/rm.c
+1
-1
@@ -278,7 +278,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
278
279
for (i = 0; i < active_nr; i++) {
280
const struct cache_entry *ce = active_cache[i];
281
- if (!ce_path_match(ce, &pathspec, seen))
281
+ if (!ce_path_match(&the_index, ce, &pathspec, seen))
282
continue;
283
ALLOC_GROW(list.entry, list.nr + 1, list.alloc);
284
list.entry[list.nr].name = xstrdup(ce->name);
builtin/submodule--helper.c
+1
-1
@@ -331,7 +331,7 @@ static int module_list_compute(int argc, const char **argv,
331
for (i = 0; i < active_nr; i++) {
332
const struct cache_entry *ce = active_cache[i];
333
334
- if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
334
+ if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
335
0, ps_matched, 1) ||
336
!S_ISGITLINK(ce->ce_mode))
337
continue;
builtin/update-index.c
+1
-1
@@ -748,7 +748,7 @@ static int do_reupdate(int ac, const char **av,
748
int save_nr;
749
char *path;
750
751
- if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
751
+ if (ce_stage(ce) || !ce_path_match(&the_index, ce, &pathspec, NULL))
752
continue;
753
if (has_head)
754
old = read_one_ent(NULL, &head_oid,
diff-lib.c
+2
-2
@@ -109,7 +109,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
109
if (diff_can_quit_early(&revs->diffopt))
110
break;
111
112
- if (!ce_path_match(ce, &revs->prune_data, NULL))
112
+ if (!ce_path_match(&the_index, ce, &revs->prune_data, NULL))
113
continue;
114
115
if (ce_stage(ce)) {
@@ -474,7 +474,7 @@ static int oneway_diff(const struct cache_entry * const *src,
474
if (tree == o->df_conflict_entry)
475
tree = NULL;
476
477
- if (ce_path_match(idx ? idx : tree, &revs->prune_data, NULL)) {
477
+ if (ce_path_match(&the_index, idx ? idx : tree, &revs->prune_data, NULL)) {
478
do_oneway_diff(o, idx, tree);
479
if (diff_can_quit_early(&revs->diffopt)) {
480
o->exiting_early = 1;
dir.c
+16
-11
@@ -276,12 +276,13 @@ static int do_read_blob(const struct object_id *oid, struct oid_stat *oid_stat,
276
#define DO_MATCH_DIRECTORY (1<<1)
277
#define DO_MATCH_SUBMODULE (1<<2)
278
279
-static int match_attrs(const char *name, int namelen,
279
+static int match_attrs(const struct index_state *istate,
280
+ const char *name, int namelen,
281
const struct pathspec_item *item)
282
{
283
int i;
284
284
- git_check_attr(&the_index, name, item->attr_check);
285
+ git_check_attr(istate, name, item->attr_check);
286
for (i = 0; i < item->attr_match_nr; i++) {
287
const char *value;
288
int matched;
@@ -318,7 +319,8 @@ static int match_attrs(const char *name, int namelen,
319
*
320
* It returns 0 when there is no match.
321
*/
321
-static int match_pathspec_item(const struct pathspec_item *item, int prefix,
322
+static int match_pathspec_item(const struct index_state *istate,
323
+ const struct pathspec_item *item, int prefix,
324
const char *name, int namelen, unsigned flags)
325
{
326
/* name/namelen has prefix cut off by caller */
@@ -358,7 +360,7 @@ static int match_pathspec_item(const struct pathspec_item *item, int prefix,
360
strncmp(item->match, name - prefix, item->prefix))
361
return 0;
362
361
- if (item->attr_match_nr && !match_attrs(name, namelen, item))
363
+ if (item->attr_match_nr && !match_attrs(istate, name, namelen, item))
364
return 0;
365
366
/* If the match was just the prefix, we matched */
@@ -426,7 +428,8 @@ static int match_pathspec_item(const struct pathspec_item *item, int prefix,
428
* pathspec did not match any names, which could indicate that the
429
* user mistyped the nth pathspec.
430
*/
429
-static int do_match_pathspec(const struct pathspec *ps,
431
+static int do_match_pathspec(const struct index_state *istate,
432
+ const struct pathspec *ps,
433
const char *name, int namelen,
434
int prefix, char *seen,
435
unsigned flags)
@@ -472,7 +475,7 @@ static int do_match_pathspec(const struct pathspec *ps,
475
*/
476
if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE)
477
seen[i] = MATCHED_FNMATCH;
475
- how = match_pathspec_item(ps->items+i, prefix, name,
478
+ how = match_pathspec_item(istate, ps->items+i, prefix, name,
479
namelen, flags);
480
if (ps->recursive &&
481
(ps->magic & PATHSPEC_MAXDEPTH) &&
@@ -496,17 +499,18 @@ static int do_match_pathspec(const struct pathspec *ps,
499
return retval;
500
}
501
499
-int match_pathspec(const struct pathspec *ps,
502
+int match_pathspec(const struct index_state *istate,
503
+ const struct pathspec *ps,
504
const char *name, int namelen,
505
int prefix, char *seen, int is_dir)
506
{
507
int positive, negative;
508
unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0;
505
- positive = do_match_pathspec(ps, name, namelen,
509
+ positive = do_match_pathspec(istate, ps, name, namelen,
510
prefix, seen, flags);
511
if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive)
512
return positive;
509
- negative = do_match_pathspec(ps, name, namelen,
513
+ negative = do_match_pathspec(istate, ps, name, namelen,
514
prefix, seen,
515
flags | DO_MATCH_EXCLUDE);
516
return negative ? 0 : positive;
@@ -515,11 +519,12 @@ int match_pathspec(const struct pathspec *ps,
519
/**
520
* Check if a submodule is a superset of the pathspec
521
*/
518
-int submodule_path_match(const struct pathspec *ps,
522
+int submodule_path_match(const struct index_state *istate,
523
+ const struct pathspec *ps,
524
const char *submodule_name,
525
char *seen)
526
{
522
- int matched = do_match_pathspec(ps, submodule_name,
527
+ int matched = do_match_pathspec(istate, ps, submodule_name,
528
strlen(submodule_name),
529
0, seen,
530
DO_MATCH_DIRECTORY |
dir.h
+10
-6
@@ -216,7 +216,8 @@ extern int count_slashes(const char *s);
216
extern int simple_length(const char *match);
217
extern int no_wildcard(const char *string);
218
extern char *common_prefix(const struct pathspec *pathspec);
219
-extern int match_pathspec(const struct pathspec *pathspec,
219
+extern int match_pathspec(const struct index_state *istate,
220
+ const struct pathspec *pathspec,
221
const char *name, int namelen,
222
int prefix, char *seen, int is_dir);
223
extern int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);
@@ -326,25 +327,28 @@ extern int git_fnmatch(const struct pathspec_item *item,
327
const char *pattern, const char *string,
328
int prefix);
329
329
-extern int submodule_path_match(const struct pathspec *ps,
330
+extern int submodule_path_match(const struct index_state *istate,
331
+ const struct pathspec *ps,
332
const char *submodule_name,
333
char *seen);
334
333
-static inline int ce_path_match(const struct cache_entry *ce,
335
+static inline int ce_path_match(const struct index_state *istate,
336
+ const struct cache_entry *ce,
337
const struct pathspec *pathspec,
338
char *seen)
339
{
337
- return match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen,
340
+ return match_pathspec(istate, pathspec, ce->name, ce_namelen(ce), 0, seen,
341
S_ISDIR(ce->ce_mode) || S_ISGITLINK(ce->ce_mode));
342
}
343
341
-static inline int dir_path_match(const struct dir_entry *ent,
344
+static inline int dir_path_match(const struct index_state *istate,
345
+ const struct dir_entry *ent,
346
const struct pathspec *pathspec,
347
int prefix, char *seen)
348
{
349
int has_trailing_dir = ent->len && ent->name[ent->len - 1] == '/';
350
int len = has_trailing_dir ? ent->len - 1 : ent->len;
347
- return match_pathspec(pathspec, ent->name, len, prefix, seen,
351
+ return match_pathspec(istate, pathspec, ent->name, len, prefix, seen,
352
has_trailing_dir);
353
}
354
pathspec.c
+1
-1
@@ -37,7 +37,7 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,
37
return;
38
for (i = 0; i < istate->cache_nr; i++) {
39
const struct cache_entry *ce = istate->cache[i];
40
- ce_path_match(ce, pathspec, seen);
40
+ ce_path_match(&the_index, ce, pathspec, seen);
41
}
42
}
43
preload-index.c
+1
-1
@@ -58,7 +58,7 @@ static void *preload_thread(void *_data)
58
continue;
59
if (ce->ce_flags & CE_FSMONITOR_VALID)
60
continue;
61
- if (!ce_path_match(ce, &p->pathspec, NULL))
61
+ if (!ce_path_match(&the_index, ce, &p->pathspec, NULL))
62
continue;
63
if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
64
continue;
read-cache.c
+1
-1
@@ -1493,7 +1493,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
1493
if (ignore_submodules && S_ISGITLINK(ce->ce_mode))
1494
continue;
1495
1496
- if (pathspec && !ce_path_match(ce, pathspec, seen))
1496
+ if (pathspec && !ce_path_match(&the_index, ce, pathspec, seen))
1497
filtered = 1;
1498
1499
if (ce_stage(ce)) {
rerere.c
+1
-1
@@ -1120,7 +1120,7 @@ int rerere_forget(struct pathspec *pathspec)
1120
find_conflict(&conflict);
1121
for (i = 0; i < conflict.nr; i++) {
1122
struct string_list_item *it = &conflict.items[i];
1123
- if (!match_pathspec(pathspec, it->string,
1123
+ if (!match_pathspec(&the_index, pathspec, it->string,
1124
strlen(it->string), 0, NULL, 0))
1125
continue;
1126
rerere_forget_one_path(it->string, &merge_rr);
resolve-undo.c
+1
-1
@@ -188,7 +188,7 @@ void unmerge_index(struct index_state *istate, const struct pathspec *pathspec)
188
189
for (i = 0; i < istate->cache_nr; i++) {
190
const struct cache_entry *ce = istate->cache[i];
191
- if (!ce_path_match(ce, pathspec, NULL))
191
+ if (!ce_path_match(&the_index, ce, pathspec, NULL))
192
continue;
193
i = unmerge_index_entry_at(istate, i);
194
}
revision.c
+1
-1
@@ -1517,7 +1517,7 @@ static void prepare_show_merge(struct rev_info *revs)
1517
const struct cache_entry *ce = active_cache[i];
1518
if (!ce_stage(ce))
1519
continue;
1520
- if (ce_path_match(ce, &revs->prune_data, NULL)) {
1520
+ if (ce_path_match(&the_index, ce, &revs->prune_data, NULL)) {
1521
prune_num++;
1522
REALLOC_ARRAY(prune, prune_num);
1523
prune[prune_num-2] = ce->name;
submodule.c
+1
-1
@@ -258,7 +258,7 @@ int is_submodule_active(struct repository *repo, const char *path)
258
}
259
260
parse_pathspec(&ps, 0, 0, NULL, args.argv);
261
- ret = match_pathspec(&ps, path, strlen(path), 0, NULL, 1);
261
+ ret = match_pathspec(&the_index, &ps, path, strlen(path), 0, NULL, 1);
262
263
argv_array_clear(&args);
264
clear_pathspec(&ps);
wt-status.c
+3
-3
@@ -647,7 +647,7 @@ static void wt_status_collect_changes_initial(struct wt_status *s)
647
struct wt_status_change_data *d;
648
const struct cache_entry *ce = active_cache[i];
649
650
- if (!ce_path_match(ce, &s->pathspec, NULL))
650
+ if (!ce_path_match(&the_index, ce, &s->pathspec, NULL))
651
continue;
652
if (ce_intent_to_add(ce))
653
continue;
@@ -703,7 +703,7 @@ static void wt_status_collect_untracked(struct wt_status *s)
703
for (i = 0; i < dir.nr; i++) {
704
struct dir_entry *ent = dir.entries[i];
705
if (cache_name_is_other(ent->name, ent->len) &&
706
- dir_path_match(ent, &s->pathspec, 0, NULL))
706
+ dir_path_match(&the_index, ent, &s->pathspec, 0, NULL))
707
string_list_insert(&s->untracked, ent->name);
708
free(ent);
709
}
@@ -711,7 +711,7 @@ static void wt_status_collect_untracked(struct wt_status *s)
711
for (i = 0; i < dir.ignored_nr; i++) {
712
struct dir_entry *ent = dir.ignored[i];
713
if (cache_name_is_other(ent->name, ent->len) &&
714
- dir_path_match(ent, &s->pathspec, 0, NULL))
714
+ dir_path_match(&the_index, ent, &s->pathspec, 0, NULL))
715
string_list_insert(&s->ignored, ent->name);
716
free(ent);
717
}