@@ -166,12 +166,8 @@ static int verify_cache(struct cache_entry **cache,
166
166
fprintf (stderr , "...\n" );
167
167
break ;
168
168
}
169
- if (ce_stage (ce ))
170
- fprintf (stderr , "%s: unmerged (%s)\n" ,
171
- ce -> name , sha1_to_hex (ce -> sha1 ));
172
- else
173
- fprintf (stderr , "%s: not added yet\n" ,
174
- ce -> name );
169
+ fprintf (stderr , "%s: unmerged (%s)\n" ,
170
+ ce -> name , sha1_to_hex (ce -> sha1 ));
175
171
}
176
172
}
177
173
if (funny )
@@ -242,13 +238,17 @@ static int update_one(struct cache_tree *it,
242
238
int entries ,
243
239
const char * base ,
244
240
int baselen ,
241
+ int * skip_count ,
245
242
int flags )
246
243
{
247
244
struct strbuf buffer ;
248
245
int missing_ok = flags & WRITE_TREE_MISSING_OK ;
249
246
int dryrun = flags & WRITE_TREE_DRY_RUN ;
247
+ int to_invalidate = 0 ;
250
248
int i ;
251
249
250
+ * skip_count = 0 ;
251
+
252
252
if (0 <= it -> entry_count && has_sha1_file (it -> sha1 ))
253
253
return it -> entry_count ;
254
254
@@ -263,20 +263,23 @@ static int update_one(struct cache_tree *it,
263
263
/*
264
264
* Find the subtrees and update them.
265
265
*/
266
- for (i = 0 ; i < entries ; i ++ ) {
266
+ i = 0 ;
267
+ while (i < entries ) {
267
268
struct cache_entry * ce = cache [i ];
268
269
struct cache_tree_sub * sub ;
269
270
const char * path , * slash ;
270
- int pathlen , sublen , subcnt ;
271
+ int pathlen , sublen , subcnt , subskip ;
271
272
272
273
path = ce -> name ;
273
274
pathlen = ce_namelen (ce );
274
275
if (pathlen <= baselen || memcmp (base , path , baselen ))
275
276
break ; /* at the end of this level */
276
277
277
278
slash = strchr (path + baselen , '/' );
278
- if (!slash )
279
+ if (!slash ) {
280
+ i ++ ;
279
281
continue ;
282
+ }
280
283
/*
281
284
* a/bbb/c (base = a/, slash = /c)
282
285
* ==>
@@ -290,10 +293,13 @@ static int update_one(struct cache_tree *it,
290
293
cache + i , entries - i ,
291
294
path ,
292
295
baselen + sublen + 1 ,
296
+ & subskip ,
293
297
flags );
294
298
if (subcnt < 0 )
295
299
return subcnt ;
296
- i += subcnt - 1 ;
300
+ i += subcnt ;
301
+ sub -> count = subcnt ; /* to be used in the next loop */
302
+ * skip_count += subskip ;
297
303
sub -> used = 1 ;
298
304
}
299
305
@@ -304,7 +310,8 @@ static int update_one(struct cache_tree *it,
304
310
*/
305
311
strbuf_init (& buffer , 8192 );
306
312
307
- for (i = 0 ; i < entries ; i ++ ) {
313
+ i = 0 ;
314
+ while (i < entries ) {
308
315
struct cache_entry * ce = cache [i ];
309
316
struct cache_tree_sub * sub ;
310
317
const char * path , * slash ;
@@ -324,23 +331,43 @@ static int update_one(struct cache_tree *it,
324
331
if (!sub )
325
332
die ("cache-tree.c: '%.*s' in '%s' not found" ,
326
333
entlen , path + baselen , path );
327
- i += sub -> cache_tree -> entry_count - 1 ;
334
+ i += sub -> count ;
328
335
sha1 = sub -> cache_tree -> sha1 ;
329
336
mode = S_IFDIR ;
337
+ if (sub -> cache_tree -> entry_count < 0 )
338
+ to_invalidate = 1 ;
330
339
}
331
340
else {
332
341
sha1 = ce -> sha1 ;
333
342
mode = ce -> ce_mode ;
334
343
entlen = pathlen - baselen ;
344
+ i ++ ;
335
345
}
336
346
if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file (sha1 )) {
337
347
strbuf_release (& buffer );
338
348
return error ("invalid object %06o %s for '%.*s'" ,
339
349
mode , sha1_to_hex (sha1 ), entlen + baselen , path );
340
350
}
341
351
342
- if (ce -> ce_flags & (CE_REMOVE | CE_INTENT_TO_ADD ))
343
- continue ; /* entry being removed or placeholder */
352
+ /*
353
+ * CE_REMOVE entries are removed before the index is
354
+ * written to disk. Skip them to remain consistent
355
+ * with the future on-disk index.
356
+ */
357
+ if (ce -> ce_flags & CE_REMOVE ) {
358
+ * skip_count = * skip_count + 1 ;
359
+ continue ;
360
+ }
361
+
362
+ /*
363
+ * CE_INTENT_TO_ADD entries exist on on-disk index but
364
+ * they are not part of generated trees. Invalidate up
365
+ * to root to force cache-tree users to read elsewhere.
366
+ */
367
+ if (ce -> ce_flags & CE_INTENT_TO_ADD ) {
368
+ to_invalidate = 1 ;
369
+ continue ;
370
+ }
344
371
345
372
strbuf_grow (& buffer , entlen + 100 );
346
373
strbuf_addf (& buffer , "%o %.*s%c" , mode , entlen , path + baselen , '\0' );
@@ -360,7 +387,7 @@ static int update_one(struct cache_tree *it,
360
387
}
361
388
362
389
strbuf_release (& buffer );
363
- it -> entry_count = i ;
390
+ it -> entry_count = to_invalidate ? -1 : i - * skip_count ;
364
391
#if DEBUG
365
392
fprintf (stderr , "cache-tree update-one (%d ent, %d subtree) %s\n" ,
366
393
it -> entry_count , it -> subtree_nr ,
@@ -374,11 +401,11 @@ int cache_tree_update(struct cache_tree *it,
374
401
int entries ,
375
402
int flags )
376
403
{
377
- int i ;
404
+ int i , skip ;
378
405
i = verify_cache (cache , entries , flags );
379
406
if (i )
380
407
return i ;
381
- i = update_one (it , cache , entries , "" , 0 , flags );
408
+ i = update_one (it , cache , entries , "" , 0 , & skip , flags );
382
409
if (i < 0 )
383
410
return i ;
384
411
return 0 ;
0 commit comments