@@ -325,7 +325,8 @@ static int geometry_cmp(const void *va, const void *vb)
325325}
326326
327327static void init_pack_geometry (struct pack_geometry * * geometry_p ,
328- struct string_list * existing_kept_packs )
328+ struct string_list * existing_kept_packs ,
329+ const struct pack_objects_args * args )
329330{
330331 struct packed_git * p ;
331332 struct pack_geometry * geometry ;
@@ -335,6 +336,14 @@ static void init_pack_geometry(struct pack_geometry **geometry_p,
335336 geometry = * geometry_p ;
336337
337338 for (p = get_all_packs (the_repository ); p ; p = p -> next ) {
339+ if (args -> local && !p -> pack_local )
340+ /*
341+ * When asked to only repack local packfiles we skip
342+ * over any packfiles that are borrowed from alternate
343+ * object directories.
344+ */
345+ continue ;
346+
338347 if (!pack_kept_objects ) {
339348 /*
340349 * Any pack that has its pack_keep bit set will appear
@@ -448,8 +457,10 @@ static void split_pack_geometry(struct pack_geometry *geometry, int factor)
448457 geometry -> split = split ;
449458}
450459
451- static struct packed_git * get_largest_active_pack (struct pack_geometry * geometry )
460+ static struct packed_git * get_preferred_pack (struct pack_geometry * geometry )
452461{
462+ uint32_t i ;
463+
453464 if (!geometry ) {
454465 /*
455466 * No geometry means either an all-into-one repack (in which
@@ -464,7 +475,21 @@ static struct packed_git *get_largest_active_pack(struct pack_geometry *geometry
464475 }
465476 if (geometry -> split == geometry -> pack_nr )
466477 return NULL ;
467- return geometry -> pack [geometry -> pack_nr - 1 ];
478+
479+ /*
480+ * The preferred pack is the largest pack above the split line. In
481+ * other words, it is the largest pack that does not get rolled up in
482+ * the geometric repack.
483+ */
484+ for (i = geometry -> pack_nr ; i > geometry -> split ; i -- )
485+ /*
486+ * A pack that is not local would never be included in a
487+ * multi-pack index. We thus skip over any non-local packs.
488+ */
489+ if (geometry -> pack [i - 1 ]-> pack_local )
490+ return geometry -> pack [i - 1 ];
491+
492+ return NULL ;
468493}
469494
470495static void clear_pack_geometry (struct pack_geometry * geometry )
@@ -558,6 +583,17 @@ static void midx_included_packs(struct string_list *include,
558583 for (i = geometry -> split ; i < geometry -> pack_nr ; i ++ ) {
559584 struct packed_git * p = geometry -> pack [i ];
560585
586+ /*
587+ * The multi-pack index never refers to packfiles part
588+ * of an alternate object database, so we skip these.
589+ * While git-multi-pack-index(1) would silently ignore
590+ * them anyway, this allows us to skip executing the
591+ * command completely when we have only non-local
592+ * packfiles.
593+ */
594+ if (!p -> pack_local )
595+ continue ;
596+
561597 strbuf_addstr (& buf , pack_basename (p ));
562598 strbuf_strip_suffix (& buf , ".pack" );
563599 strbuf_addstr (& buf , ".idx" );
@@ -591,7 +627,7 @@ static int write_midx_included_packs(struct string_list *include,
591627{
592628 struct child_process cmd = CHILD_PROCESS_INIT ;
593629 struct string_list_item * item ;
594- struct packed_git * largest = get_largest_active_pack (geometry );
630+ struct packed_git * preferred = get_preferred_pack (geometry );
595631 FILE * in ;
596632 int ret ;
597633
@@ -612,9 +648,9 @@ static int write_midx_included_packs(struct string_list *include,
612648 if (write_bitmaps )
613649 strvec_push (& cmd .args , "--bitmap" );
614650
615- if (largest )
651+ if (preferred )
616652 strvec_pushf (& cmd .args , "--preferred-pack=%s" ,
617- pack_basename (largest ));
653+ pack_basename (preferred ));
618654
619655 if (refs_snapshot )
620656 strvec_pushf (& cmd .args , "--refs-snapshot=%s" , refs_snapshot );
@@ -853,6 +889,18 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
853889 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE ) && !write_midx )
854890 die (_ (incremental_bitmap_conflict_error ));
855891
892+ if (write_bitmaps && po_args .local && has_alt_odb (the_repository )) {
893+ /*
894+ * When asked to do a local repack, but we have
895+ * packfiles that are inherited from an alternate, then
896+ * we cannot guarantee that the multi-pack-index would
897+ * have full coverage of all objects. We thus disable
898+ * writing bitmaps in that case.
899+ */
900+ warning (_ ("disabling bitmap writing, as some objects are not being packed" ));
901+ write_bitmaps = 0 ;
902+ }
903+
856904 if (write_midx && write_bitmaps ) {
857905 struct strbuf path = STRBUF_INIT ;
858906
@@ -875,7 +923,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
875923 if (geometric_factor ) {
876924 if (pack_everything )
877925 die (_ ("options '%s' and '%s' cannot be used together" ), "--geometric" , "-A/-a" );
878- init_pack_geometry (& geometry , & existing_kept_packs );
926+ init_pack_geometry (& geometry , & existing_kept_packs , & po_args );
879927 split_pack_geometry (geometry , geometric_factor );
880928 }
881929
0 commit comments