@@ -454,8 +454,8 @@ static int mark_tagged(const char *path, const unsigned char *sha1, int flag,
454
454
return 0 ;
455
455
}
456
456
457
- static void add_to_write_order (struct object_entry * * wo ,
458
- int * endp ,
457
+ static inline void add_to_write_order (struct object_entry * * wo ,
458
+ unsigned int * endp ,
459
459
struct object_entry * e )
460
460
{
461
461
if (e -> filled )
@@ -465,32 +465,62 @@ static void add_to_write_order(struct object_entry **wo,
465
465
}
466
466
467
467
static void add_descendants_to_write_order (struct object_entry * * wo ,
468
- int * endp ,
468
+ unsigned int * endp ,
469
469
struct object_entry * e )
470
470
{
471
- struct object_entry * child ;
472
-
473
- for (child = e -> delta_child ; child ; child = child -> delta_sibling )
474
- add_to_write_order (wo , endp , child );
475
- for (child = e -> delta_child ; child ; child = child -> delta_sibling )
476
- add_descendants_to_write_order (wo , endp , child );
471
+ int add_to_order = 1 ;
472
+ while (e ) {
473
+ if (add_to_order ) {
474
+ struct object_entry * s ;
475
+ /* add this node... */
476
+ add_to_write_order (wo , endp , e );
477
+ /* all its siblings... */
478
+ for (s = e -> delta_sibling ; s ; s = s -> delta_sibling ) {
479
+ add_to_write_order (wo , endp , s );
480
+ }
481
+ }
482
+ /* drop down a level to add left subtree nodes if possible */
483
+ if (e -> delta_child ) {
484
+ add_to_order = 1 ;
485
+ e = e -> delta_child ;
486
+ } else {
487
+ add_to_order = 0 ;
488
+ /* our sibling might have some children, it is next */
489
+ if (e -> delta_sibling ) {
490
+ e = e -> delta_sibling ;
491
+ continue ;
492
+ }
493
+ /* go back to our parent node */
494
+ e = e -> delta ;
495
+ while (e && !e -> delta_sibling ) {
496
+ /* we're on the right side of a subtree, keep
497
+ * going up until we can go right again */
498
+ e = e -> delta ;
499
+ }
500
+ if (!e ) {
501
+ /* done- we hit our original root node */
502
+ return ;
503
+ }
504
+ /* pass it off to sibling at this level */
505
+ e = e -> delta_sibling ;
506
+ }
507
+ };
477
508
}
478
509
479
510
static void add_family_to_write_order (struct object_entry * * wo ,
480
- int * endp ,
511
+ unsigned int * endp ,
481
512
struct object_entry * e )
482
513
{
483
514
struct object_entry * root ;
484
515
485
516
for (root = e ; root -> delta ; root = root -> delta )
486
517
; /* nothing */
487
- add_to_write_order (wo , endp , root );
488
518
add_descendants_to_write_order (wo , endp , root );
489
519
}
490
520
491
521
static struct object_entry * * compute_write_order (void )
492
522
{
493
- int i , wo_end ;
523
+ unsigned int i , wo_end , last_untagged ;
494
524
495
525
struct object_entry * * wo = xmalloc (nr_objects * sizeof (* wo ));
496
526
@@ -506,8 +536,8 @@ static struct object_entry **compute_write_order(void)
506
536
* Make sure delta_sibling is sorted in the original
507
537
* recency order.
508
538
*/
509
- for (i = nr_objects - 1 ; 0 <= i ; i -- ) {
510
- struct object_entry * e = & objects [i ];
539
+ for (i = nr_objects ; i > 0 ; ) {
540
+ struct object_entry * e = & objects [-- i ];
511
541
if (!e -> delta )
512
542
continue ;
513
543
/* Mark me as the first child */
@@ -521,14 +551,15 @@ static struct object_entry **compute_write_order(void)
521
551
for_each_tag_ref (mark_tagged , NULL );
522
552
523
553
/*
524
- * Give the commits in the original recency order until
554
+ * Give the objects in the original recency order until
525
555
* we see a tagged tip.
526
556
*/
527
557
for (i = wo_end = 0 ; i < nr_objects ; i ++ ) {
528
558
if (objects [i ].tagged )
529
559
break ;
530
560
add_to_write_order (wo , & wo_end , & objects [i ]);
531
561
}
562
+ last_untagged = i ;
532
563
533
564
/*
534
565
* Then fill all the tagged tips.
@@ -541,7 +572,7 @@ static struct object_entry **compute_write_order(void)
541
572
/*
542
573
* And then all remaining commits and tags.
543
574
*/
544
- for (i = 0 ; i < nr_objects ; i ++ ) {
575
+ for (i = last_untagged ; i < nr_objects ; i ++ ) {
545
576
if (objects [i ].type != OBJ_COMMIT &&
546
577
objects [i ].type != OBJ_TAG )
547
578
continue ;
@@ -551,7 +582,7 @@ static struct object_entry **compute_write_order(void)
551
582
/*
552
583
* And then all the trees.
553
584
*/
554
- for (i = 0 ; i < nr_objects ; i ++ ) {
585
+ for (i = last_untagged ; i < nr_objects ; i ++ ) {
555
586
if (objects [i ].type != OBJ_TREE )
556
587
continue ;
557
588
add_to_write_order (wo , & wo_end , & objects [i ]);
@@ -560,8 +591,13 @@ static struct object_entry **compute_write_order(void)
560
591
/*
561
592
* Finally all the rest in really tight order
562
593
*/
563
- for (i = 0 ; i < nr_objects ; i ++ )
564
- add_family_to_write_order (wo , & wo_end , & objects [i ]);
594
+ for (i = last_untagged ; i < nr_objects ; i ++ ) {
595
+ if (!objects [i ].filled )
596
+ add_family_to_write_order (wo , & wo_end , & objects [i ]);
597
+ }
598
+
599
+ if (wo_end != nr_objects )
600
+ die ("ordered %u objects, expected %" PRIu32 , wo_end , nr_objects );
565
601
566
602
return wo ;
567
603
}
0 commit comments