@@ -51,6 +51,8 @@ struct object_entry {
5151 * objects against.
5252 */
5353 unsigned char no_try_delta ;
54+ unsigned char tagged ; /* near the very tip of refs */
55+ unsigned char filled ; /* assigned write-order */
5456};
5557
5658/*
@@ -96,6 +98,7 @@ static unsigned long window_memory_limit = 0;
9698 */
9799static int * object_ix ;
98100static int object_ix_hashsz ;
101+ static struct object_entry * locate_object_entry (const unsigned char * sha1 );
99102
100103/*
101104 * stats
@@ -200,6 +203,7 @@ static void copy_pack_data(struct sha1file *f,
200203 }
201204}
202205
206+ /* Return 0 if we will bust the pack-size limit */
203207static unsigned long write_object (struct sha1file * f ,
204208 struct object_entry * entry ,
205209 off_t write_offset )
@@ -434,6 +438,134 @@ static int write_one(struct sha1file *f,
434438 return 1 ;
435439}
436440
441+ static int mark_tagged (const char * path , const unsigned char * sha1 , int flag ,
442+ void * cb_data )
443+ {
444+ unsigned char peeled [20 ];
445+ struct object_entry * entry = locate_object_entry (sha1 );
446+
447+ if (entry )
448+ entry -> tagged = 1 ;
449+ if (!peel_ref (path , peeled )) {
450+ entry = locate_object_entry (peeled );
451+ if (entry )
452+ entry -> tagged = 1 ;
453+ }
454+ return 0 ;
455+ }
456+
457+ static void add_to_write_order (struct object_entry * * wo ,
458+ int * endp ,
459+ struct object_entry * e )
460+ {
461+ if (e -> filled )
462+ return ;
463+ wo [(* endp )++ ] = e ;
464+ e -> filled = 1 ;
465+ }
466+
467+ static void add_descendants_to_write_order (struct object_entry * * wo ,
468+ int * endp ,
469+ struct object_entry * e )
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 );
477+ }
478+
479+ static void add_family_to_write_order (struct object_entry * * wo ,
480+ int * endp ,
481+ struct object_entry * e )
482+ {
483+ struct object_entry * root ;
484+
485+ for (root = e ; root -> delta ; root = root -> delta )
486+ ; /* nothing */
487+ add_to_write_order (wo , endp , root );
488+ add_descendants_to_write_order (wo , endp , root );
489+ }
490+
491+ static struct object_entry * * compute_write_order (void )
492+ {
493+ int i , wo_end ;
494+
495+ struct object_entry * * wo = xmalloc (nr_objects * sizeof (* wo ));
496+
497+ for (i = 0 ; i < nr_objects ; i ++ ) {
498+ objects [i ].tagged = 0 ;
499+ objects [i ].filled = 0 ;
500+ objects [i ].delta_child = NULL ;
501+ objects [i ].delta_sibling = NULL ;
502+ }
503+
504+ /*
505+ * Fully connect delta_child/delta_sibling network.
506+ * Make sure delta_sibling is sorted in the original
507+ * recency order.
508+ */
509+ for (i = nr_objects - 1 ; 0 <= i ; i -- ) {
510+ struct object_entry * e = & objects [i ];
511+ if (!e -> delta )
512+ continue ;
513+ /* Mark me as the first child */
514+ e -> delta_sibling = e -> delta -> delta_child ;
515+ e -> delta -> delta_child = e ;
516+ }
517+
518+ /*
519+ * Mark objects that are at the tip of tags.
520+ */
521+ for_each_tag_ref (mark_tagged , NULL );
522+
523+ /*
524+ * Give the commits in the original recency order until
525+ * we see a tagged tip.
526+ */
527+ for (i = wo_end = 0 ; i < nr_objects ; i ++ ) {
528+ if (objects [i ].tagged )
529+ break ;
530+ add_to_write_order (wo , & wo_end , & objects [i ]);
531+ }
532+
533+ /*
534+ * Then fill all the tagged tips.
535+ */
536+ for (; i < nr_objects ; i ++ ) {
537+ if (objects [i ].tagged )
538+ add_to_write_order (wo , & wo_end , & objects [i ]);
539+ }
540+
541+ /*
542+ * And then all remaining commits and tags.
543+ */
544+ for (i = 0 ; i < nr_objects ; i ++ ) {
545+ if (objects [i ].type != OBJ_COMMIT &&
546+ objects [i ].type != OBJ_TAG )
547+ continue ;
548+ add_to_write_order (wo , & wo_end , & objects [i ]);
549+ }
550+
551+ /*
552+ * And then all the trees.
553+ */
554+ for (i = 0 ; i < nr_objects ; i ++ ) {
555+ if (objects [i ].type != OBJ_TREE )
556+ continue ;
557+ add_to_write_order (wo , & wo_end , & objects [i ]);
558+ }
559+
560+ /*
561+ * Finally all the rest in really tight order
562+ */
563+ for (i = 0 ; i < nr_objects ; i ++ )
564+ add_family_to_write_order (wo , & wo_end , & objects [i ]);
565+
566+ return wo ;
567+ }
568+
437569static void write_pack_file (void )
438570{
439571 uint32_t i = 0 , j ;
@@ -442,10 +574,12 @@ static void write_pack_file(void)
442574 struct pack_header hdr ;
443575 uint32_t nr_remaining = nr_result ;
444576 time_t last_mtime = 0 ;
577+ struct object_entry * * write_order ;
445578
446579 if (progress > pack_to_stdout )
447580 progress_state = start_progress ("Writing objects" , nr_result );
448581 written_list = xmalloc (nr_objects * sizeof (* written_list ));
582+ write_order = compute_write_order ();
449583
450584 do {
451585 unsigned char sha1 [20 ];
@@ -469,7 +603,8 @@ static void write_pack_file(void)
469603 offset = sizeof (hdr );
470604 nr_written = 0 ;
471605 for (; i < nr_objects ; i ++ ) {
472- if (!write_one (f , objects + i , & offset ))
606+ struct object_entry * e = write_order [i ];
607+ if (!write_one (f , e , & offset ))
473608 break ;
474609 display_progress (progress_state , written );
475610 }
@@ -546,6 +681,7 @@ static void write_pack_file(void)
546681 } while (nr_remaining && i < nr_objects );
547682
548683 free (written_list );
684+ free (write_order );
549685 stop_progress (& progress_state );
550686 if (written != nr_result )
551687 die ("wrote %" PRIu32 " objects while expecting %" PRIu32 ,
0 commit comments