@@ -308,8 +308,24 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
308
308
* continue. A NULL pattern set indicates a full expansion to a
309
309
* full index.
310
310
*/
311
- if (pl && !pl -> use_cone_patterns )
311
+ if (pl && !pl -> use_cone_patterns ) {
312
312
pl = NULL ;
313
+ } else {
314
+ /*
315
+ * We might contract file entries into sparse-directory
316
+ * entries, and for that we will need the cache tree to
317
+ * be recomputed.
318
+ */
319
+ cache_tree_free (& istate -> cache_tree );
320
+
321
+ /*
322
+ * If there is a problem creating the cache tree, then we
323
+ * need to expand to a full index since we cannot satisfy
324
+ * the current request as a sparse index.
325
+ */
326
+ if (cache_tree_update (istate , 0 ))
327
+ pl = NULL ;
328
+ }
313
329
314
330
if (!istate -> repo )
315
331
istate -> repo = the_repository ;
@@ -327,8 +343,14 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
327
343
full = xcalloc (1 , sizeof (struct index_state ));
328
344
memcpy (full , istate , sizeof (struct index_state ));
329
345
346
+ /*
347
+ * This slightly-misnamed 'full' index might still be sparse if we
348
+ * are only modifying the list of sparse directories. This hinges
349
+ * on whether we have a non-NULL pattern list.
350
+ */
351
+ full -> sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED ;
352
+
330
353
/* then change the necessary things */
331
- full -> sparse_index = 0 ;
332
354
full -> cache_alloc = (3 * istate -> cache_alloc ) / 2 ;
333
355
full -> cache_nr = 0 ;
334
356
ALLOC_ARRAY (full -> cache , full -> cache_alloc );
@@ -340,11 +362,22 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
340
362
struct cache_entry * ce = istate -> cache [i ];
341
363
struct tree * tree ;
342
364
struct pathspec ps ;
365
+ int dtype ;
343
366
344
367
if (!S_ISSPARSEDIR (ce -> ce_mode )) {
345
368
set_index_entry (full , full -> cache_nr ++ , ce );
346
369
continue ;
347
370
}
371
+
372
+ /* We now have a sparse directory entry. Should we expand? */
373
+ if (pl &&
374
+ path_matches_pattern_list (ce -> name , ce -> ce_namelen ,
375
+ NULL , & dtype ,
376
+ pl , istate ) == NOT_MATCHED ) {
377
+ set_index_entry (full , full -> cache_nr ++ , ce );
378
+ continue ;
379
+ }
380
+
348
381
if (!(ce -> ce_flags & CE_SKIP_WORKTREE ))
349
382
warning (_ ("index entry is a directory, but not sparse (%08x)" ),
350
383
ce -> ce_flags );
@@ -370,7 +403,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl)
370
403
/* Copy back into original index. */
371
404
memcpy (& istate -> name_hash , & full -> name_hash , sizeof (full -> name_hash ));
372
405
memcpy (& istate -> dir_hash , & full -> dir_hash , sizeof (full -> dir_hash ));
373
- istate -> sparse_index = 0 ;
406
+ istate -> sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED ;
374
407
free (istate -> cache );
375
408
istate -> cache = full -> cache ;
376
409
istate -> cache_nr = full -> cache_nr ;
0 commit comments