174
}
175
}
176
177
-static int pathspec_needs_expanded_index(const struct pathspec *pathspec)
178
-{
179
- unsigned int i, pos;
180
- int res = 0;
181
- char *skip_worktree_seen = NULL;
182
-
183
- /*
184
- * When using a magic pathspec, assume for the sake of simplicity that
185
- * the index needs to be expanded to match all matchable files.
186
- */
187
- if (pathspec->magic)
188
- return 1;
189
-
190
- for (i = 0; i < pathspec->nr; i++) {
191
- struct pathspec_item item = pathspec->items[i];
192
-
193
- /*
194
- * If the pathspec item has a wildcard, the index should be expanded
195
- * if the pathspec has the possibility of matching a subset of entries inside
196
- * of a sparse directory (but not the entire directory).
197
- *
198
- * If the pathspec item is a literal path, the index only needs to be expanded
199
- * if a) the pathspec isn't in the sparse checkout cone (to make sure we don't
200
- * expand for in-cone files) and b) it doesn't match any sparse directories
201
- * (since we can reset whole sparse directories without expanding them).
202
- */
203
- if (item.nowildcard_len < item.len) {
204
- /*
205
- * Special case: if the pattern is a path inside the cone
206
- * followed by only wildcards, the pattern cannot match
207
- * partial sparse directories, so we know we don't need to
208
- * expand the index.
209
- *
210
- * Examples:
211
- * - in-cone/foo***: doesn't need expanded index
212
- * - not-in-cone/bar*: may need expanded index
213
- * - **.c: may need expanded index
214
- */
215
- if (strspn(item.original + item.nowildcard_len, "*") == item.len - item.nowildcard_len &&
216
- path_in_cone_mode_sparse_checkout(item.original, &the_index))
217
- continue;
218
-
219
- for (pos = 0; pos < active_nr; pos++) {
220
- struct cache_entry *ce = active_cache[pos];
221
-
222
- if (!S_ISSPARSEDIR(ce->ce_mode))
223
- continue;
224
-
225
- /*
226
- * If the pre-wildcard length is longer than the sparse
227
- * directory name and the sparse directory is the first
228
- * component of the pathspec, need to expand the index.
229
- */
230
- if (item.nowildcard_len > ce_namelen(ce) &&
231
- !strncmp(item.original, ce->name, ce_namelen(ce))) {
232
- res = 1;
233
- break;
234
- }
235
-
236
- /*
237
- * If the pre-wildcard length is shorter than the sparse
238
- * directory and the pathspec does not match the whole
239
- * directory, need to expand the index.
240
- */
241
- if (!strncmp(item.original, ce->name, item.nowildcard_len) &&
242
- wildmatch(item.original, ce->name, 0)) {
243
- res = 1;
244
- break;
245
- }
246
- }
247
- } else if (!path_in_cone_mode_sparse_checkout(item.original, &the_index) &&
248
- !matches_skip_worktree(pathspec, i, &skip_worktree_seen))
249
- res = 1;
250
-
251
- if (res > 0)
252
- break;
253
- }
254
-
255
- free(skip_worktree_seen);
256
- return res;
257
-}
258
-
177
static int read_from_tree(const struct pathspec *pathspec,
178
struct object_id *tree_oid,
179
int intent_to_add)
191
opt.change = diff_change;
192
opt.add_remove = diff_addremove;
193
276
- if (pathspec->nr && the_index.sparse_index && pathspec_needs_expanded_index(pathspec))
194
+ if (pathspec->nr && pathspec_needs_expanded_index(&the_index, pathspec))
195
ensure_full_index(&the_index);
196
197
if (do_diff_cache(tree_oid, &opt))