@@ -938,44 +938,65 @@ static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
938938	return  result ;
939939}
940940
941- static  int  fill_packs_from_midx (struct  write_midx_context  * ctx ,
942- 				const  char  * preferred_pack_name , uint32_t  flags )
941+ static  int  fill_packs_from_midx_1 (struct  write_midx_context  * ctx ,
942+ 				  struct  multi_pack_index  * m ,
943+ 				  uint32_t  flags )
943944{
944- 	struct  multi_pack_index  * m ;
945- 
946- 	for  (m  =  ctx -> m ; m ; m  =  m -> base_midx ) {
947- 		uint32_t  i ;
945+ 	for  (uint32_t  i  =  0 ; i  <  m -> num_packs ; i ++ ) {
946+ 		struct  packed_git  * p  =  NULL ;
948947
949- 		for  (i  =  0 ; i  <  m -> num_packs ; i ++ ) {
950- 			ALLOC_GROW (ctx -> info , ctx -> nr  +  1 , ctx -> alloc );
948+ 		ALLOC_GROW (ctx -> info , ctx -> nr  +  1 , ctx -> alloc );
951949
952- 			/* 
953- 			 * If generating a reverse index, need to have 
954- 			 * packed_git's loaded to compare their 
955- 			 * mtimes and object count. 
956- 			 * 
957- 			 * If a preferred pack is specified, need to 
958- 			 * have packed_git's loaded to ensure the chosen 
959- 			 * preferred pack has a non-zero object count. 
960- 			 */ 
961- 			if  (flags  &  MIDX_WRITE_REV_INDEX  || 
962- 			    preferred_pack_name ) {
963- 				if  (prepare_midx_pack (ctx -> repo , m ,
964- 						      m -> num_packs_in_base  +  i )) {
965- 					error (_ ("could not load pack" ));
966- 					return  1 ;
967- 				}
968- 
969- 				if  (open_pack_index (m -> packs [i ]))
970- 					die (_ ("could not open index for %s" ),
971- 					    m -> packs [i ]-> pack_name );
950+ 		/* 
951+ 		 * If generating a reverse index, need to have 
952+ 		 * packed_git's loaded to compare their 
953+ 		 * mtimes and object count. 
954+ 		 */ 
955+ 		if  (flags  &  MIDX_WRITE_REV_INDEX ) {
956+ 			p  =  prepare_midx_pack (ctx -> repo , m ,
957+ 					      m -> num_packs_in_base  +  i );
958+ 			if  (!p ) {
959+ 				error (_ ("could not load pack" ));
960+ 				return  1 ;
972961			}
973962
974- 			fill_pack_info ( & ctx -> info [ ctx -> nr ++ ],  m -> packs [ i ], 
975- 				        m -> pack_names [ i ] ,
976- 				        m -> num_packs_in_base   +   i );
963+ 			if  ( open_pack_index ( p )) 
964+ 				die ( _ ( "could not open index for %s" ) ,
965+ 				    p -> pack_name );
977966		}
967+ 
968+ 		fill_pack_info (& ctx -> info [ctx -> nr ++ ], p ,
969+ 			       m -> pack_names [i ],
970+ 			       m -> num_packs_in_base  +  i );
971+ 	}
972+ 
973+ 	return  0 ;
974+ }
975+ 
976+ static  int  fill_packs_from_midx (struct  write_midx_context  * ctx ,
977+ 				const  char  * preferred_pack_name , uint32_t  flags )
978+ {
979+ 	struct  multi_pack_index  * m ;
980+ 
981+ 	if  (preferred_pack_name ) {
982+ 		/* 
983+ 		 * If a preferred pack is specified, need to have 
984+ 		 * packed_git's loaded to ensure the chosen preferred 
985+ 		 * pack has a non-zero object count. 
986+ 		 * 
987+ 		 * Trick ourselves into thinking that we're writing a 
988+ 		 * reverse index in this case in order to open up the 
989+ 		 * pack index file. 
990+ 		 */ 
991+ 		flags  |= MIDX_WRITE_REV_INDEX ;
978992	}
993+ 
994+ 	for  (m  =  ctx -> m ; m ; m  =  m -> base_midx ) {
995+ 		int  ret  =  fill_packs_from_midx_1 (ctx , m , flags );
996+ 		if  (ret )
997+ 			return  ret ;
998+ 	}
999+ 
9791000	return  0 ;
9801001}
9811002
@@ -1578,20 +1599,19 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
15781599					  _ ("Finding and deleting unreferenced packfiles" ),
15791600					  m -> num_packs );
15801601	for  (i  =  0 ; i  <  m -> num_packs ; i ++ ) {
1602+ 		struct  packed_git  * p ;
15811603		char  * pack_name ;
15821604		display_progress (progress , i  +  1 );
15831605
15841606		if  (count [i ])
15851607			continue ;
15861608
1587- 		if  (prepare_midx_pack (r , m , i ))
1609+ 		p  =  prepare_midx_pack (r , m , i );
1610+ 		if  (!p  ||  p -> pack_keep  ||  p -> is_cruft )
15881611			continue ;
15891612
1590- 		if  (m -> packs [i ]-> pack_keep  ||  m -> packs [i ]-> is_cruft )
1591- 			continue ;
1592- 
1593- 		pack_name  =  xstrdup (m -> packs [i ]-> pack_name );
1594- 		close_pack (m -> packs [i ]);
1613+ 		pack_name  =  xstrdup (p -> pack_name );
1614+ 		close_pack (p );
15951615
15961616		string_list_insert (& packs_to_drop , m -> pack_names [i ]);
15971617		unlink_pack_path (pack_name , 0 );
@@ -1636,9 +1656,12 @@ static int want_included_pack(struct repository *r,
16361656			      uint32_t  pack_int_id )
16371657{
16381658	struct  packed_git  * p ;
1639- 	if  (prepare_midx_pack (r , m , pack_int_id ))
1659+ 
1660+ 	ASSERT (m  &&  !m -> base_midx );
1661+ 
1662+ 	p  =  prepare_midx_pack (r , m , pack_int_id );
1663+ 	if  (!p )
16401664		return  0 ;
1641- 	p  =  m -> packs [pack_int_id ];
16421665	if  (!pack_kept_objects  &&  p -> pack_keep )
16431666		return  0 ;
16441667	if  (p -> is_cruft )
@@ -1655,6 +1678,8 @@ static void fill_included_packs_all(struct repository *r,
16551678	uint32_t  i ;
16561679	int  pack_kept_objects  =  0 ;
16571680
1681+ 	ASSERT (m  &&  !m -> base_midx );
1682+ 
16581683	repo_config_get_bool (r , "repack.packkeptobjects" , & pack_kept_objects );
16591684
16601685	for  (i  =  0 ; i  <  m -> num_packs ; i ++ ) {
@@ -1675,17 +1700,18 @@ static void fill_included_packs_batch(struct repository *r,
16751700	struct  repack_info  * pack_info ;
16761701	int  pack_kept_objects  =  0 ;
16771702
1703+ 	ASSERT (m  &&  !m -> base_midx );
1704+ 
16781705	CALLOC_ARRAY (pack_info , m -> num_packs );
16791706
16801707	repo_config_get_bool (r , "repack.packkeptobjects" , & pack_kept_objects );
16811708
16821709	for  (i  =  0 ; i  <  m -> num_packs ; i ++ ) {
1683- 		pack_info [i ].pack_int_id  =  i ;
1684- 
1685- 		if  (prepare_midx_pack (r , m , i ))
1686- 			continue ;
1710+ 		struct  packed_git  * p  =  prepare_midx_pack (r , m , i );
16871711
1688- 		pack_info [i ].mtime  =  m -> packs [i ]-> mtime ;
1712+ 		pack_info [i ].pack_int_id  =  i ;
1713+ 		if  (p )
1714+ 			pack_info [i ].mtime  =  p -> mtime ;
16891715	}
16901716
16911717	for  (i  =  0 ; i  <  m -> num_objects ; i ++ ) {
0 commit comments