88#include "notes.h"
99#include "gpg-interface.h"
1010#include "mergesort.h"
11+ #include "commit-slab.h"
1112
1213static struct commit_extra_header * read_commit_extra_header_lines (const char * buf , size_t len , const char * * );
1314
1415int save_commit_buffer = 1 ;
1516
1617const char * commit_type = "commit" ;
18+ static int commit_count ;
1719
1820static struct commit * check_commit (struct object * obj ,
1921 const unsigned char * sha1 ,
@@ -58,8 +60,11 @@ struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_n
5860struct commit * lookup_commit (const unsigned char * sha1 )
5961{
6062 struct object * obj = lookup_object (sha1 );
61- if (!obj )
62- return create_object (sha1 , OBJ_COMMIT , alloc_commit_node ());
63+ if (!obj ) {
64+ struct commit * c = alloc_commit_node ();
65+ c -> index = commit_count ++ ;
66+ return create_object (sha1 , OBJ_COMMIT , c );
67+ }
6368 if (!obj -> type )
6469 obj -> type = OBJ_COMMIT ;
6570 return check_commit (obj , sha1 , 0 );
@@ -506,6 +511,13 @@ struct commit *pop_commit(struct commit_list **stack)
506511 return item ;
507512}
508513
514+ /*
515+ * Topological sort support
516+ */
517+
518+ /* count number of children that have not been emitted */
519+ define_commit_slab (indegree_slab , int );
520+
509521/*
510522 * Performs an in-place topological sort on the list supplied.
511523 */
@@ -514,25 +526,29 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
514526 struct commit_list * next , * orig = * list ;
515527 struct commit_list * work , * * insert ;
516528 struct commit_list * * pptr ;
529+ struct indegree_slab indegree ;
517530
518531 if (!orig )
519532 return ;
520533 * list = NULL ;
521534
535+ init_indegree_slab (& indegree );
536+
522537 /* Mark them and clear the indegree */
523538 for (next = orig ; next ; next = next -> next ) {
524539 struct commit * commit = next -> item ;
525- commit -> indegree = 1 ;
540+ * ( indegree_slab_at ( & indegree , commit )) = 1 ;
526541 }
527542
528543 /* update the indegree */
529544 for (next = orig ; next ; next = next -> next ) {
530545 struct commit_list * parents = next -> item -> parents ;
531546 while (parents ) {
532547 struct commit * parent = parents -> item ;
548+ int * pi = indegree_slab_at (& indegree , parent );
533549
534- if (parent -> indegree )
535- parent -> indegree ++ ;
550+ if (* pi )
551+ ( * pi ) ++ ;
536552 parents = parents -> next ;
537553 }
538554 }
@@ -549,7 +565,7 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
549565 for (next = orig ; next ; next = next -> next ) {
550566 struct commit * commit = next -> item ;
551567
552- if (commit -> indegree == 1 )
568+ if (* ( indegree_slab_at ( & indegree , commit )) == 1 )
553569 insert = & commit_list_insert (commit , insert )-> next ;
554570 }
555571
@@ -570,16 +586,17 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
570586 commit = work_item -> item ;
571587 for (parents = commit -> parents ; parents ; parents = parents -> next ) {
572588 struct commit * parent = parents -> item ;
589+ int * pi = indegree_slab_at (& indegree , parent );
573590
574- if (!parent -> indegree )
591+ if (!* pi )
575592 continue ;
576593
577594 /*
578595 * parents are only enqueued for emission
579596 * when all their children have been emitted thereby
580597 * guaranteeing topological order.
581598 */
582- if (-- parent -> indegree == 1 ) {
599+ if (-- ( * pi ) == 1 ) {
583600 if (!lifo )
584601 commit_list_insert_by_date (parent , & work );
585602 else
@@ -590,10 +607,12 @@ void sort_in_topological_order(struct commit_list ** list, int lifo)
590607 * work_item is a commit all of whose children
591608 * have already been emitted. we can emit it now.
592609 */
593- commit -> indegree = 0 ;
610+ * ( indegree_slab_at ( & indegree , commit )) = 0 ;
594611 * pptr = work_item ;
595612 pptr = & work_item -> next ;
596613 }
614+
615+ clear_indegree_slab (& indegree );
597616}
598617
599618/* merge-base stuff */
0 commit comments