7 #include "object-store.h"
8 #include "hash-lookup.h"
12 #include "run-command.h"
13 #include "repository.h"
14 #include "chunk-format.h"
17 #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
18 #define MIDX_VERSION 1
19 #define MIDX_BYTE_FILE_VERSION 4
20 #define MIDX_BYTE_HASH_VERSION 5
21 #define MIDX_BYTE_NUM_CHUNKS 6
22 #define MIDX_BYTE_NUM_PACKS 8
23 #define MIDX_HEADER_SIZE 12
24 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
26 #define MIDX_CHUNK_ALIGNMENT 4
27 #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
28 #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
29 #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
30 #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
31 #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
32 #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
33 #define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
34 #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
35 #define MIDX_LARGE_OFFSET_NEEDED 0x80000000
37 #define PACK_EXPIRED UINT_MAX
39 static uint8_t oid_version(void)
41 switch (hash_algo_by_ptr(the_hash_algo
)) {
47 die(_("invalid hash version"));
51 static const unsigned char *get_midx_checksum(struct multi_pack_index
*m
)
53 return m
->data
+ m
->data_len
- the_hash_algo
->rawsz
;
56 static char *get_midx_filename(const char *object_dir
)
58 return xstrfmt("%s/pack/multi-pack-index", object_dir
);
61 char *get_midx_rev_filename(struct multi_pack_index
*m
)
63 return xstrfmt("%s/pack/multi-pack-index-%s.rev",
64 m
->object_dir
, hash_to_hex(get_midx_checksum(m
)));
67 static int midx_read_oid_fanout(const unsigned char *chunk_start
,
68 size_t chunk_size
, void *data
)
70 struct multi_pack_index
*m
= data
;
71 m
->chunk_oid_fanout
= (uint32_t *)chunk_start
;
73 if (chunk_size
!= 4 * 256) {
74 error(_("multi-pack-index OID fanout is of the wrong size"));
80 struct multi_pack_index
*load_multi_pack_index(const char *object_dir
, int local
)
82 struct multi_pack_index
*m
= NULL
;
86 void *midx_map
= NULL
;
87 uint32_t hash_version
;
88 char *midx_name
= get_midx_filename(object_dir
);
90 const char *cur_pack_name
;
91 struct chunkfile
*cf
= NULL
;
93 fd
= git_open(midx_name
);
98 error_errno(_("failed to read %s"), midx_name
);
102 midx_size
= xsize_t(st
.st_size
);
104 if (midx_size
< MIDX_MIN_SIZE
) {
105 error(_("multi-pack-index file %s is too small"), midx_name
);
109 FREE_AND_NULL(midx_name
);
111 midx_map
= xmmap(NULL
, midx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
114 FLEX_ALLOC_STR(m
, object_dir
, object_dir
);
116 m
->data_len
= midx_size
;
119 m
->signature
= get_be32(m
->data
);
120 if (m
->signature
!= MIDX_SIGNATURE
)
121 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
122 m
->signature
, MIDX_SIGNATURE
);
124 m
->version
= m
->data
[MIDX_BYTE_FILE_VERSION
];
125 if (m
->version
!= MIDX_VERSION
)
126 die(_("multi-pack-index version %d not recognized"),
129 hash_version
= m
->data
[MIDX_BYTE_HASH_VERSION
];
130 if (hash_version
!= oid_version()) {
131 error(_("multi-pack-index hash version %u does not match version %u"),
132 hash_version
, oid_version());
135 m
->hash_len
= the_hash_algo
->rawsz
;
137 m
->num_chunks
= m
->data
[MIDX_BYTE_NUM_CHUNKS
];
139 m
->num_packs
= get_be32(m
->data
+ MIDX_BYTE_NUM_PACKS
);
141 cf
= init_chunkfile(NULL
);
143 if (read_table_of_contents(cf
, m
->data
, midx_size
,
144 MIDX_HEADER_SIZE
, m
->num_chunks
))
147 if (pair_chunk(cf
, MIDX_CHUNKID_PACKNAMES
, &m
->chunk_pack_names
) == CHUNK_NOT_FOUND
)
148 die(_("multi-pack-index missing required pack-name chunk"));
149 if (read_chunk(cf
, MIDX_CHUNKID_OIDFANOUT
, midx_read_oid_fanout
, m
) == CHUNK_NOT_FOUND
)
150 die(_("multi-pack-index missing required OID fanout chunk"));
151 if (pair_chunk(cf
, MIDX_CHUNKID_OIDLOOKUP
, &m
->chunk_oid_lookup
) == CHUNK_NOT_FOUND
)
152 die(_("multi-pack-index missing required OID lookup chunk"));
153 if (pair_chunk(cf
, MIDX_CHUNKID_OBJECTOFFSETS
, &m
->chunk_object_offsets
) == CHUNK_NOT_FOUND
)
154 die(_("multi-pack-index missing required object offsets chunk"));
156 pair_chunk(cf
, MIDX_CHUNKID_LARGEOFFSETS
, &m
->chunk_large_offsets
);
158 m
->num_objects
= ntohl(m
->chunk_oid_fanout
[255]);
160 CALLOC_ARRAY(m
->pack_names
, m
->num_packs
);
161 CALLOC_ARRAY(m
->packs
, m
->num_packs
);
163 cur_pack_name
= (const char *)m
->chunk_pack_names
;
164 for (i
= 0; i
< m
->num_packs
; i
++) {
165 m
->pack_names
[i
] = cur_pack_name
;
167 cur_pack_name
+= strlen(cur_pack_name
) + 1;
169 if (i
&& strcmp(m
->pack_names
[i
], m
->pack_names
[i
- 1]) <= 0)
170 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
171 m
->pack_names
[i
- 1],
175 trace2_data_intmax("midx", the_repository
, "load/num_packs", m
->num_packs
);
176 trace2_data_intmax("midx", the_repository
, "load/num_objects", m
->num_objects
);
185 munmap(midx_map
, midx_size
);
191 void close_midx(struct multi_pack_index
*m
)
198 munmap((unsigned char *)m
->data
, m
->data_len
);
200 for (i
= 0; i
< m
->num_packs
; i
++) {
202 m
->packs
[i
]->multi_pack_index
= 0;
204 FREE_AND_NULL(m
->packs
);
205 FREE_AND_NULL(m
->pack_names
);
208 int prepare_midx_pack(struct repository
*r
, struct multi_pack_index
*m
, uint32_t pack_int_id
)
210 struct strbuf pack_name
= STRBUF_INIT
;
211 struct packed_git
*p
;
213 if (pack_int_id
>= m
->num_packs
)
214 die(_("bad pack-int-id: %u (%u total packs)"),
215 pack_int_id
, m
->num_packs
);
217 if (m
->packs
[pack_int_id
])
220 strbuf_addf(&pack_name
, "%s/pack/%s", m
->object_dir
,
221 m
->pack_names
[pack_int_id
]);
223 p
= add_packed_git(pack_name
.buf
, pack_name
.len
, m
->local
);
224 strbuf_release(&pack_name
);
229 p
->multi_pack_index
= 1;
230 m
->packs
[pack_int_id
] = p
;
231 install_packed_git(r
, p
);
232 list_add_tail(&p
->mru
, &r
->objects
->packed_git_mru
);
237 int bsearch_midx(const struct object_id
*oid
, struct multi_pack_index
*m
, uint32_t *result
)
239 return bsearch_hash(oid
->hash
, m
->chunk_oid_fanout
, m
->chunk_oid_lookup
,
240 the_hash_algo
->rawsz
, result
);
243 struct object_id
*nth_midxed_object_oid(struct object_id
*oid
,
244 struct multi_pack_index
*m
,
247 if (n
>= m
->num_objects
)
250 oidread(oid
, m
->chunk_oid_lookup
+ m
->hash_len
* n
);
254 off_t
nth_midxed_offset(struct multi_pack_index
*m
, uint32_t pos
)
256 const unsigned char *offset_data
;
259 offset_data
= m
->chunk_object_offsets
+ (off_t
)pos
* MIDX_CHUNK_OFFSET_WIDTH
;
260 offset32
= get_be32(offset_data
+ sizeof(uint32_t));
262 if (m
->chunk_large_offsets
&& offset32
& MIDX_LARGE_OFFSET_NEEDED
) {
263 if (sizeof(off_t
) < sizeof(uint64_t))
264 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
266 offset32
^= MIDX_LARGE_OFFSET_NEEDED
;
267 return get_be64(m
->chunk_large_offsets
+ sizeof(uint64_t) * offset32
);
273 uint32_t nth_midxed_pack_int_id(struct multi_pack_index
*m
, uint32_t pos
)
275 return get_be32(m
->chunk_object_offsets
+
276 (off_t
)pos
* MIDX_CHUNK_OFFSET_WIDTH
);
279 static int nth_midxed_pack_entry(struct repository
*r
,
280 struct multi_pack_index
*m
,
281 struct pack_entry
*e
,
284 uint32_t pack_int_id
;
285 struct packed_git
*p
;
287 if (pos
>= m
->num_objects
)
290 pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
292 if (prepare_midx_pack(r
, m
, pack_int_id
))
294 p
= m
->packs
[pack_int_id
];
297 * We are about to tell the caller where they can locate the
298 * requested object. We better make sure the packfile is
299 * still here and can be accessed before supplying that
300 * answer, as it may have been deleted since the MIDX was
303 if (!is_pack_valid(p
))
306 if (p
->num_bad_objects
) {
308 struct object_id oid
;
309 nth_midxed_object_oid(&oid
, m
, pos
);
310 for (i
= 0; i
< p
->num_bad_objects
; i
++)
312 p
->bad_object_sha1
+ the_hash_algo
->rawsz
* i
))
316 e
->offset
= nth_midxed_offset(m
, pos
);
322 int fill_midx_entry(struct repository
* r
,
323 const struct object_id
*oid
,
324 struct pack_entry
*e
,
325 struct multi_pack_index
*m
)
329 if (!bsearch_midx(oid
, m
, &pos
))
332 return nth_midxed_pack_entry(r
, m
, e
, pos
);
335 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
336 static int cmp_idx_or_pack_name(const char *idx_or_pack_name
,
337 const char *idx_name
)
339 /* Skip past any initial matching prefix. */
340 while (*idx_name
&& *idx_name
== *idx_or_pack_name
) {
346 * If we didn't match completely, we may have matched "pack-1234." and
347 * be left with "idx" and "pack" respectively, which is also OK. We do
348 * not have to check for "idx" and "idx", because that would have been
349 * a complete match (and in that case these strcmps will be false, but
350 * we'll correctly return 0 from the final strcmp() below.
352 * Technically this matches "fooidx" and "foopack", but we'd never have
353 * such names in the first place.
355 if (!strcmp(idx_name
, "idx") && !strcmp(idx_or_pack_name
, "pack"))
359 * This not only checks for a complete match, but also orders based on
360 * the first non-identical character, which means our ordering will
361 * match a raw strcmp(). That makes it OK to use this to binary search
362 * a naively-sorted list.
364 return strcmp(idx_or_pack_name
, idx_name
);
367 int midx_contains_pack(struct multi_pack_index
*m
, const char *idx_or_pack_name
)
369 uint32_t first
= 0, last
= m
->num_packs
;
371 while (first
< last
) {
372 uint32_t mid
= first
+ (last
- first
) / 2;
376 current
= m
->pack_names
[mid
];
377 cmp
= cmp_idx_or_pack_name(idx_or_pack_name
, current
);
390 int prepare_multi_pack_index_one(struct repository
*r
, const char *object_dir
, int local
)
392 struct multi_pack_index
*m
;
393 struct multi_pack_index
*m_search
;
395 prepare_repo_settings(r
);
396 if (!r
->settings
.core_multi_pack_index
)
399 for (m_search
= r
->objects
->multi_pack_index
; m_search
; m_search
= m_search
->next
)
400 if (!strcmp(object_dir
, m_search
->object_dir
))
403 m
= load_multi_pack_index(object_dir
, local
);
406 struct multi_pack_index
*mp
= r
->objects
->multi_pack_index
;
411 r
->objects
->multi_pack_index
= m
;
418 static size_t write_midx_header(struct hashfile
*f
,
419 unsigned char num_chunks
,
422 hashwrite_be32(f
, MIDX_SIGNATURE
);
423 hashwrite_u8(f
, MIDX_VERSION
);
424 hashwrite_u8(f
, oid_version());
425 hashwrite_u8(f
, num_chunks
);
426 hashwrite_u8(f
, 0); /* unused */
427 hashwrite_be32(f
, num_packs
);
429 return MIDX_HEADER_SIZE
;
433 uint32_t orig_pack_int_id
;
435 struct packed_git
*p
;
436 unsigned expired
: 1;
439 static int pack_info_compare(const void *_a
, const void *_b
)
441 struct pack_info
*a
= (struct pack_info
*)_a
;
442 struct pack_info
*b
= (struct pack_info
*)_b
;
443 return strcmp(a
->pack_name
, b
->pack_name
);
446 static int idx_or_pack_name_cmp(const void *_va
, const void *_vb
)
448 const char *pack_name
= _va
;
449 const struct pack_info
*compar
= _vb
;
451 return cmp_idx_or_pack_name(pack_name
, compar
->pack_name
);
454 struct write_midx_context
{
455 struct pack_info
*info
;
458 struct multi_pack_index
*m
;
459 struct progress
*progress
;
460 unsigned pack_paths_checked
;
462 struct pack_midx_entry
*entries
;
466 uint32_t *pack_order
;
467 unsigned large_offsets_needed
:1;
468 uint32_t num_large_offsets
;
470 int preferred_pack_idx
;
473 static void add_pack_to_midx(const char *full_path
, size_t full_path_len
,
474 const char *file_name
, void *data
)
476 struct write_midx_context
*ctx
= data
;
478 if (ends_with(file_name
, ".idx")) {
479 display_progress(ctx
->progress
, ++ctx
->pack_paths_checked
);
480 if (ctx
->m
&& midx_contains_pack(ctx
->m
, file_name
))
483 ALLOC_GROW(ctx
->info
, ctx
->nr
+ 1, ctx
->alloc
);
485 ctx
->info
[ctx
->nr
].p
= add_packed_git(full_path
,
489 if (!ctx
->info
[ctx
->nr
].p
) {
490 warning(_("failed to add packfile '%s'"),
495 if (open_pack_index(ctx
->info
[ctx
->nr
].p
)) {
496 warning(_("failed to open pack-index '%s'"),
498 close_pack(ctx
->info
[ctx
->nr
].p
);
499 FREE_AND_NULL(ctx
->info
[ctx
->nr
].p
);
503 ctx
->info
[ctx
->nr
].pack_name
= xstrdup(file_name
);
504 ctx
->info
[ctx
->nr
].orig_pack_int_id
= ctx
->nr
;
505 ctx
->info
[ctx
->nr
].expired
= 0;
510 struct pack_midx_entry
{
511 struct object_id oid
;
512 uint32_t pack_int_id
;
515 unsigned preferred
: 1;
518 static int midx_oid_compare(const void *_a
, const void *_b
)
520 const struct pack_midx_entry
*a
= (const struct pack_midx_entry
*)_a
;
521 const struct pack_midx_entry
*b
= (const struct pack_midx_entry
*)_b
;
522 int cmp
= oidcmp(&a
->oid
, &b
->oid
);
527 /* Sort objects in a preferred pack first when multiple copies exist. */
528 if (a
->preferred
> b
->preferred
)
530 if (a
->preferred
< b
->preferred
)
533 if (a
->pack_mtime
> b
->pack_mtime
)
535 else if (a
->pack_mtime
< b
->pack_mtime
)
538 return a
->pack_int_id
- b
->pack_int_id
;
541 static int nth_midxed_pack_midx_entry(struct multi_pack_index
*m
,
542 struct pack_midx_entry
*e
,
545 if (pos
>= m
->num_objects
)
548 nth_midxed_object_oid(&e
->oid
, m
, pos
);
549 e
->pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
550 e
->offset
= nth_midxed_offset(m
, pos
);
552 /* consider objects in midx to be from "old" packs */
557 static void fill_pack_entry(uint32_t pack_int_id
,
558 struct packed_git
*p
,
560 struct pack_midx_entry
*entry
,
563 if (nth_packed_object_id(&entry
->oid
, p
, cur_object
) < 0)
564 die(_("failed to locate object %d in packfile"), cur_object
);
566 entry
->pack_int_id
= pack_int_id
;
567 entry
->pack_mtime
= p
->mtime
;
569 entry
->offset
= nth_packed_object_offset(p
, cur_object
);
570 entry
->preferred
= !!preferred
;
574 * It is possible to artificially get into a state where there are many
575 * duplicate copies of objects. That can create high memory pressure if
576 * we are to create a list of all objects before de-duplication. To reduce
577 * this memory pressure without a significant performance drop, automatically
578 * group objects by the first byte of their object id. Use the IDX fanout
579 * tables to group the data, copy to a local array, then sort.
581 * Copy only the de-duplicated entries (selected by most-recent modified time
582 * of a packfile containing the object).
584 static struct pack_midx_entry
*get_sorted_entries(struct multi_pack_index
*m
,
585 struct pack_info
*info
,
587 uint32_t *nr_objects
,
590 uint32_t cur_fanout
, cur_pack
, cur_object
;
591 uint32_t alloc_fanout
, alloc_objects
, total_objects
= 0;
592 struct pack_midx_entry
*entries_by_fanout
= NULL
;
593 struct pack_midx_entry
*deduplicated_entries
= NULL
;
594 uint32_t start_pack
= m
? m
->num_packs
: 0;
596 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++)
597 total_objects
+= info
[cur_pack
].p
->num_objects
;
600 * As we de-duplicate by fanout value, we expect the fanout
601 * slices to be evenly distributed, with some noise. Hence,
602 * allocate slightly more than one 256th.
604 alloc_objects
= alloc_fanout
= total_objects
> 3200 ? total_objects
/ 200 : 16;
606 ALLOC_ARRAY(entries_by_fanout
, alloc_fanout
);
607 ALLOC_ARRAY(deduplicated_entries
, alloc_objects
);
610 for (cur_fanout
= 0; cur_fanout
< 256; cur_fanout
++) {
611 uint32_t nr_fanout
= 0;
614 uint32_t start
= 0, end
;
617 start
= ntohl(m
->chunk_oid_fanout
[cur_fanout
- 1]);
618 end
= ntohl(m
->chunk_oid_fanout
[cur_fanout
]);
620 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
621 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
622 nth_midxed_pack_midx_entry(m
,
623 &entries_by_fanout
[nr_fanout
],
625 if (nth_midxed_pack_int_id(m
, cur_object
) == preferred_pack
)
626 entries_by_fanout
[nr_fanout
].preferred
= 1;
628 entries_by_fanout
[nr_fanout
].preferred
= 0;
633 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++) {
634 uint32_t start
= 0, end
;
635 int preferred
= cur_pack
== preferred_pack
;
638 start
= get_pack_fanout(info
[cur_pack
].p
, cur_fanout
- 1);
639 end
= get_pack_fanout(info
[cur_pack
].p
, cur_fanout
);
641 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
642 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
643 fill_pack_entry(cur_pack
,
646 &entries_by_fanout
[nr_fanout
],
652 QSORT(entries_by_fanout
, nr_fanout
, midx_oid_compare
);
655 * The batch is now sorted by OID and then mtime (descending).
656 * Take only the first duplicate.
658 for (cur_object
= 0; cur_object
< nr_fanout
; cur_object
++) {
659 if (cur_object
&& oideq(&entries_by_fanout
[cur_object
- 1].oid
,
660 &entries_by_fanout
[cur_object
].oid
))
663 ALLOC_GROW(deduplicated_entries
, *nr_objects
+ 1, alloc_objects
);
664 memcpy(&deduplicated_entries
[*nr_objects
],
665 &entries_by_fanout
[cur_object
],
666 sizeof(struct pack_midx_entry
));
671 free(entries_by_fanout
);
672 return deduplicated_entries
;
675 static int write_midx_pack_names(struct hashfile
*f
, void *data
)
677 struct write_midx_context
*ctx
= data
;
679 unsigned char padding
[MIDX_CHUNK_ALIGNMENT
];
682 for (i
= 0; i
< ctx
->nr
; i
++) {
685 if (ctx
->info
[i
].expired
)
688 if (i
&& strcmp(ctx
->info
[i
].pack_name
, ctx
->info
[i
- 1].pack_name
) <= 0)
689 BUG("incorrect pack-file order: %s before %s",
690 ctx
->info
[i
- 1].pack_name
,
691 ctx
->info
[i
].pack_name
);
693 writelen
= strlen(ctx
->info
[i
].pack_name
) + 1;
694 hashwrite(f
, ctx
->info
[i
].pack_name
, writelen
);
698 /* add padding to be aligned */
699 i
= MIDX_CHUNK_ALIGNMENT
- (written
% MIDX_CHUNK_ALIGNMENT
);
700 if (i
< MIDX_CHUNK_ALIGNMENT
) {
701 memset(padding
, 0, sizeof(padding
));
702 hashwrite(f
, padding
, i
);
708 static int write_midx_oid_fanout(struct hashfile
*f
,
711 struct write_midx_context
*ctx
= data
;
712 struct pack_midx_entry
*list
= ctx
->entries
;
713 struct pack_midx_entry
*last
= ctx
->entries
+ ctx
->entries_nr
;
718 * Write the first-level table (the list is sorted,
719 * but we use a 256-entry lookup to be able to avoid
720 * having to do eight extra binary search iterations).
722 for (i
= 0; i
< 256; i
++) {
723 struct pack_midx_entry
*next
= list
;
725 while (next
< last
&& next
->oid
.hash
[0] == i
) {
730 hashwrite_be32(f
, count
);
737 static int write_midx_oid_lookup(struct hashfile
*f
,
740 struct write_midx_context
*ctx
= data
;
741 unsigned char hash_len
= the_hash_algo
->rawsz
;
742 struct pack_midx_entry
*list
= ctx
->entries
;
745 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
746 struct pack_midx_entry
*obj
= list
++;
748 if (i
< ctx
->entries_nr
- 1) {
749 struct pack_midx_entry
*next
= list
;
750 if (oidcmp(&obj
->oid
, &next
->oid
) >= 0)
751 BUG("OIDs not in order: %s >= %s",
752 oid_to_hex(&obj
->oid
),
753 oid_to_hex(&next
->oid
));
756 hashwrite(f
, obj
->oid
.hash
, (int)hash_len
);
762 static int write_midx_object_offsets(struct hashfile
*f
,
765 struct write_midx_context
*ctx
= data
;
766 struct pack_midx_entry
*list
= ctx
->entries
;
767 uint32_t i
, nr_large_offset
= 0;
769 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
770 struct pack_midx_entry
*obj
= list
++;
772 if (ctx
->pack_perm
[obj
->pack_int_id
] == PACK_EXPIRED
)
773 BUG("object %s is in an expired pack with int-id %d",
774 oid_to_hex(&obj
->oid
),
777 hashwrite_be32(f
, ctx
->pack_perm
[obj
->pack_int_id
]);
779 if (ctx
->large_offsets_needed
&& obj
->offset
>> 31)
780 hashwrite_be32(f
, MIDX_LARGE_OFFSET_NEEDED
| nr_large_offset
++);
781 else if (!ctx
->large_offsets_needed
&& obj
->offset
>> 32)
782 BUG("object %s requires a large offset (%"PRIx64
") but the MIDX is not writing large offsets!",
783 oid_to_hex(&obj
->oid
),
786 hashwrite_be32(f
, (uint32_t)obj
->offset
);
792 static int write_midx_large_offsets(struct hashfile
*f
,
795 struct write_midx_context
*ctx
= data
;
796 struct pack_midx_entry
*list
= ctx
->entries
;
797 struct pack_midx_entry
*end
= ctx
->entries
+ ctx
->entries_nr
;
798 uint32_t nr_large_offset
= ctx
->num_large_offsets
;
800 while (nr_large_offset
) {
801 struct pack_midx_entry
*obj
;
805 BUG("too many large-offset objects");
808 offset
= obj
->offset
;
813 hashwrite_be64(f
, offset
);
821 struct midx_pack_order_data
{
827 static int midx_pack_order_cmp(const void *va
, const void *vb
)
829 const struct midx_pack_order_data
*a
= va
, *b
= vb
;
830 if (a
->pack
< b
->pack
)
832 else if (a
->pack
> b
->pack
)
834 else if (a
->offset
< b
->offset
)
836 else if (a
->offset
> b
->offset
)
842 static uint32_t *midx_pack_order(struct write_midx_context
*ctx
)
844 struct midx_pack_order_data
*data
;
845 uint32_t *pack_order
;
848 ALLOC_ARRAY(data
, ctx
->entries_nr
);
849 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
850 struct pack_midx_entry
*e
= &ctx
->entries
[i
];
852 data
[i
].pack
= ctx
->pack_perm
[e
->pack_int_id
];
854 data
[i
].pack
|= (1U << 31);
855 data
[i
].offset
= e
->offset
;
858 QSORT(data
, ctx
->entries_nr
, midx_pack_order_cmp
);
860 ALLOC_ARRAY(pack_order
, ctx
->entries_nr
);
861 for (i
= 0; i
< ctx
->entries_nr
; i
++)
862 pack_order
[i
] = data
[i
].nr
;
868 static void write_midx_reverse_index(char *midx_name
, unsigned char *midx_hash
,
869 struct write_midx_context
*ctx
)
871 struct strbuf buf
= STRBUF_INIT
;
872 const char *tmp_file
;
874 strbuf_addf(&buf
, "%s-%s.rev", midx_name
, hash_to_hex(midx_hash
));
876 tmp_file
= write_rev_file_order(NULL
, ctx
->pack_order
, ctx
->entries_nr
,
877 midx_hash
, WRITE_REV
);
879 if (finalize_object_file(tmp_file
, buf
.buf
))
880 die(_("cannot store reverse index file"));
882 strbuf_release(&buf
);
885 static void clear_midx_files_ext(const char *object_dir
, const char *ext
,
886 unsigned char *keep_hash
);
888 static int midx_checksum_valid(struct multi_pack_index
*m
)
890 return hashfile_checksum_valid(m
->data
, m
->data_len
);
893 static int write_midx_internal(const char *object_dir
, struct multi_pack_index
*m
,
894 struct string_list
*packs_to_drop
,
895 const char *preferred_pack_name
,
899 unsigned char midx_hash
[GIT_MAX_RAWSZ
];
901 struct hashfile
*f
= NULL
;
903 struct write_midx_context ctx
= { 0 };
904 int pack_name_concat_len
= 0;
905 int dropped_packs
= 0;
907 struct chunkfile
*cf
;
909 midx_name
= get_midx_filename(object_dir
);
910 if (safe_create_leading_directories(midx_name
))
911 die_errno(_("unable to create leading directories of %s"),
917 ctx
.m
= load_multi_pack_index(object_dir
, 1);
919 if (ctx
.m
&& !midx_checksum_valid(ctx
.m
)) {
920 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
925 ctx
.alloc
= ctx
.m
? ctx
.m
->num_packs
: 16;
927 ALLOC_ARRAY(ctx
.info
, ctx
.alloc
);
930 for (i
= 0; i
< ctx
.m
->num_packs
; i
++) {
931 ALLOC_GROW(ctx
.info
, ctx
.nr
+ 1, ctx
.alloc
);
933 ctx
.info
[ctx
.nr
].orig_pack_int_id
= i
;
934 ctx
.info
[ctx
.nr
].pack_name
= xstrdup(ctx
.m
->pack_names
[i
]);
935 ctx
.info
[ctx
.nr
].p
= NULL
;
936 ctx
.info
[ctx
.nr
].expired
= 0;
938 if (flags
& MIDX_WRITE_REV_INDEX
) {
940 * If generating a reverse index, need to have
941 * packed_git's loaded to compare their
942 * mtimes and object count.
944 if (prepare_midx_pack(the_repository
, ctx
.m
, i
)) {
945 error(_("could not load pack"));
950 if (open_pack_index(ctx
.m
->packs
[i
]))
951 die(_("could not open index for %s"),
952 ctx
.m
->packs
[i
]->pack_name
);
953 ctx
.info
[ctx
.nr
].p
= ctx
.m
->packs
[i
];
960 ctx
.pack_paths_checked
= 0;
961 if (flags
& MIDX_PROGRESS
)
962 ctx
.progress
= start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
966 for_each_file_in_pack_dir(object_dir
, add_pack_to_midx
, &ctx
);
967 stop_progress(&ctx
.progress
);
969 if (ctx
.m
&& ctx
.nr
== ctx
.m
->num_packs
&& !packs_to_drop
)
972 ctx
.preferred_pack_idx
= -1;
973 if (preferred_pack_name
) {
974 for (i
= 0; i
< ctx
.nr
; i
++) {
975 if (!cmp_idx_or_pack_name(preferred_pack_name
,
976 ctx
.info
[i
].pack_name
)) {
977 ctx
.preferred_pack_idx
= i
;
983 if (ctx
.preferred_pack_idx
> -1) {
984 struct packed_git
*preferred
= ctx
.info
[ctx
.preferred_pack_idx
].p
;
985 if (!preferred
->num_objects
) {
986 error(_("cannot select preferred pack %s with no objects"),
987 preferred
->pack_name
);
993 ctx
.entries
= get_sorted_entries(ctx
.m
, ctx
.info
, ctx
.nr
, &ctx
.entries_nr
,
994 ctx
.preferred_pack_idx
);
996 ctx
.large_offsets_needed
= 0;
997 for (i
= 0; i
< ctx
.entries_nr
; i
++) {
998 if (ctx
.entries
[i
].offset
> 0x7fffffff)
999 ctx
.num_large_offsets
++;
1000 if (ctx
.entries
[i
].offset
> 0xffffffff)
1001 ctx
.large_offsets_needed
= 1;
1004 QSORT(ctx
.info
, ctx
.nr
, pack_info_compare
);
1006 if (packs_to_drop
&& packs_to_drop
->nr
) {
1008 int missing_drops
= 0;
1010 for (i
= 0; i
< ctx
.nr
&& drop_index
< packs_to_drop
->nr
; i
++) {
1011 int cmp
= strcmp(ctx
.info
[i
].pack_name
,
1012 packs_to_drop
->items
[drop_index
].string
);
1016 ctx
.info
[i
].expired
= 1;
1017 } else if (cmp
> 0) {
1018 error(_("did not see pack-file %s to drop"),
1019 packs_to_drop
->items
[drop_index
].string
);
1024 ctx
.info
[i
].expired
= 0;
1028 if (missing_drops
) {
1035 * pack_perm stores a permutation between pack-int-ids from the
1036 * previous multi-pack-index to the new one we are writing:
1038 * pack_perm[old_id] = new_id
1040 ALLOC_ARRAY(ctx
.pack_perm
, ctx
.nr
);
1041 for (i
= 0; i
< ctx
.nr
; i
++) {
1042 if (ctx
.info
[i
].expired
) {
1044 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = PACK_EXPIRED
;
1046 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = i
- dropped_packs
;
1050 for (i
= 0; i
< ctx
.nr
; i
++) {
1051 if (!ctx
.info
[i
].expired
)
1052 pack_name_concat_len
+= strlen(ctx
.info
[i
].pack_name
) + 1;
1055 /* Check that the preferred pack wasn't expired (if given). */
1056 if (preferred_pack_name
) {
1057 struct pack_info
*preferred
= bsearch(preferred_pack_name
,
1060 idx_or_pack_name_cmp
);
1063 warning(_("unknown preferred pack: '%s'"),
1064 preferred_pack_name
);
1066 uint32_t perm
= ctx
.pack_perm
[preferred
->orig_pack_int_id
];
1067 if (perm
== PACK_EXPIRED
)
1068 warning(_("preferred pack '%s' is expired"),
1069 preferred_pack_name
);
1073 if (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
)
1074 pack_name_concat_len
+= MIDX_CHUNK_ALIGNMENT
-
1075 (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
);
1077 hold_lock_file_for_update(&lk
, midx_name
, LOCK_DIE_ON_ERROR
);
1078 f
= hashfd(get_lock_file_fd(&lk
), get_lock_file_path(&lk
));
1083 if (ctx
.nr
- dropped_packs
== 0) {
1084 error(_("no pack files to index."));
1089 cf
= init_chunkfile(f
);
1091 add_chunk(cf
, MIDX_CHUNKID_PACKNAMES
, pack_name_concat_len
,
1092 write_midx_pack_names
);
1093 add_chunk(cf
, MIDX_CHUNKID_OIDFANOUT
, MIDX_CHUNK_FANOUT_SIZE
,
1094 write_midx_oid_fanout
);
1095 add_chunk(cf
, MIDX_CHUNKID_OIDLOOKUP
,
1096 (size_t)ctx
.entries_nr
* the_hash_algo
->rawsz
,
1097 write_midx_oid_lookup
);
1098 add_chunk(cf
, MIDX_CHUNKID_OBJECTOFFSETS
,
1099 (size_t)ctx
.entries_nr
* MIDX_CHUNK_OFFSET_WIDTH
,
1100 write_midx_object_offsets
);
1102 if (ctx
.large_offsets_needed
)
1103 add_chunk(cf
, MIDX_CHUNKID_LARGEOFFSETS
,
1104 (size_t)ctx
.num_large_offsets
* MIDX_CHUNK_LARGE_OFFSET_WIDTH
,
1105 write_midx_large_offsets
);
1107 write_midx_header(f
, get_num_chunks(cf
), ctx
.nr
- dropped_packs
);
1108 write_chunkfile(cf
, &ctx
);
1110 finalize_hashfile(f
, midx_hash
, CSUM_FSYNC
| CSUM_HASH_IN_STREAM
);
1113 if (flags
& MIDX_WRITE_REV_INDEX
)
1114 ctx
.pack_order
= midx_pack_order(&ctx
);
1116 if (flags
& MIDX_WRITE_REV_INDEX
)
1117 write_midx_reverse_index(midx_name
, midx_hash
, &ctx
);
1119 commit_lock_file(&lk
);
1121 clear_midx_files_ext(object_dir
, ".rev", midx_hash
);
1124 for (i
= 0; i
< ctx
.nr
; i
++) {
1125 if (ctx
.info
[i
].p
) {
1126 close_pack(ctx
.info
[i
].p
);
1127 free(ctx
.info
[i
].p
);
1129 free(ctx
.info
[i
].pack_name
);
1134 free(ctx
.pack_perm
);
1135 free(ctx
.pack_order
);
1140 int write_midx_file(const char *object_dir
,
1141 const char *preferred_pack_name
,
1144 return write_midx_internal(object_dir
, NULL
, NULL
, preferred_pack_name
,
1148 struct clear_midx_data
{
1153 static void clear_midx_file_ext(const char *full_path
, size_t full_path_len
,
1154 const char *file_name
, void *_data
)
1156 struct clear_midx_data
*data
= _data
;
1158 if (!(starts_with(file_name
, "multi-pack-index-") &&
1159 ends_with(file_name
, data
->ext
)))
1161 if (data
->keep
&& !strcmp(data
->keep
, file_name
))
1164 if (unlink(full_path
))
1165 die_errno(_("failed to remove %s"), full_path
);
1168 static void clear_midx_files_ext(const char *object_dir
, const char *ext
,
1169 unsigned char *keep_hash
)
1171 struct clear_midx_data data
;
1172 memset(&data
, 0, sizeof(struct clear_midx_data
));
1175 data
.keep
= xstrfmt("multi-pack-index-%s%s",
1176 hash_to_hex(keep_hash
), ext
);
1179 for_each_file_in_pack_dir(object_dir
,
1180 clear_midx_file_ext
,
1186 void clear_midx_file(struct repository
*r
)
1188 char *midx
= get_midx_filename(r
->objects
->odb
->path
);
1190 if (r
->objects
&& r
->objects
->multi_pack_index
) {
1191 close_midx(r
->objects
->multi_pack_index
);
1192 r
->objects
->multi_pack_index
= NULL
;
1195 if (remove_path(midx
))
1196 die(_("failed to clear multi-pack-index at %s"), midx
);
1198 clear_midx_files_ext(r
->objects
->odb
->path
, ".rev", NULL
);
1203 static int verify_midx_error
;
1205 __attribute__((format (printf
, 1, 2)))
1206 static void midx_report(const char *fmt
, ...)
1209 verify_midx_error
= 1;
1211 vfprintf(stderr
, fmt
, ap
);
1212 fprintf(stderr
, "\n");
1216 struct pair_pos_vs_id
1219 uint32_t pack_int_id
;
1222 static int compare_pair_pos_vs_id(const void *_a
, const void *_b
)
1224 struct pair_pos_vs_id
*a
= (struct pair_pos_vs_id
*)_a
;
1225 struct pair_pos_vs_id
*b
= (struct pair_pos_vs_id
*)_b
;
1227 return b
->pack_int_id
- a
->pack_int_id
;
1231 * Limit calls to display_progress() for performance reasons.
1232 * The interval here was arbitrarily chosen.
1234 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
1235 #define midx_display_sparse_progress(progress, n) \
1237 uint64_t _n = (n); \
1238 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1239 display_progress(progress, _n); \
1242 int verify_midx_file(struct repository
*r
, const char *object_dir
, unsigned flags
)
1244 struct pair_pos_vs_id
*pairs
= NULL
;
1246 struct progress
*progress
= NULL
;
1247 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1248 verify_midx_error
= 0;
1253 char *filename
= get_midx_filename(object_dir
);
1254 if (!stat(filename
, &sb
)) {
1255 error(_("multi-pack-index file exists, but failed to parse"));
1262 if (!midx_checksum_valid(m
))
1263 midx_report(_("incorrect checksum"));
1265 if (flags
& MIDX_PROGRESS
)
1266 progress
= start_delayed_progress(_("Looking for referenced packfiles"),
1268 for (i
= 0; i
< m
->num_packs
; i
++) {
1269 if (prepare_midx_pack(r
, m
, i
))
1270 midx_report("failed to load pack in position %d", i
);
1272 display_progress(progress
, i
+ 1);
1274 stop_progress(&progress
);
1276 for (i
= 0; i
< 255; i
++) {
1277 uint32_t oid_fanout1
= ntohl(m
->chunk_oid_fanout
[i
]);
1278 uint32_t oid_fanout2
= ntohl(m
->chunk_oid_fanout
[i
+ 1]);
1280 if (oid_fanout1
> oid_fanout2
)
1281 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32
" > %"PRIx32
" = fanout[%d]"),
1282 i
, oid_fanout1
, oid_fanout2
, i
+ 1);
1285 if (m
->num_objects
== 0) {
1286 midx_report(_("the midx contains no oid"));
1288 * Remaining tests assume that we have objects, so we can
1291 return verify_midx_error
;
1294 if (flags
& MIDX_PROGRESS
)
1295 progress
= start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1296 m
->num_objects
- 1);
1297 for (i
= 0; i
< m
->num_objects
- 1; i
++) {
1298 struct object_id oid1
, oid2
;
1300 nth_midxed_object_oid(&oid1
, m
, i
);
1301 nth_midxed_object_oid(&oid2
, m
, i
+ 1);
1303 if (oidcmp(&oid1
, &oid2
) >= 0)
1304 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1305 i
, oid_to_hex(&oid1
), oid_to_hex(&oid2
), i
+ 1);
1307 midx_display_sparse_progress(progress
, i
+ 1);
1309 stop_progress(&progress
);
1312 * Create an array mapping each object to its packfile id. Sort it
1313 * to group the objects by packfile. Use this permutation to visit
1314 * each of the objects and only require 1 packfile to be open at a
1317 ALLOC_ARRAY(pairs
, m
->num_objects
);
1318 for (i
= 0; i
< m
->num_objects
; i
++) {
1320 pairs
[i
].pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1323 if (flags
& MIDX_PROGRESS
)
1324 progress
= start_sparse_progress(_("Sorting objects by packfile"),
1326 display_progress(progress
, 0); /* TODO: Measure QSORT() progress */
1327 QSORT(pairs
, m
->num_objects
, compare_pair_pos_vs_id
);
1328 stop_progress(&progress
);
1330 if (flags
& MIDX_PROGRESS
)
1331 progress
= start_sparse_progress(_("Verifying object offsets"), m
->num_objects
);
1332 for (i
= 0; i
< m
->num_objects
; i
++) {
1333 struct object_id oid
;
1334 struct pack_entry e
;
1335 off_t m_offset
, p_offset
;
1337 if (i
> 0 && pairs
[i
-1].pack_int_id
!= pairs
[i
].pack_int_id
&&
1338 m
->packs
[pairs
[i
-1].pack_int_id
])
1340 close_pack_fd(m
->packs
[pairs
[i
-1].pack_int_id
]);
1341 close_pack_index(m
->packs
[pairs
[i
-1].pack_int_id
]);
1344 nth_midxed_object_oid(&oid
, m
, pairs
[i
].pos
);
1346 if (!fill_midx_entry(r
, &oid
, &e
, m
)) {
1347 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1348 pairs
[i
].pos
, oid_to_hex(&oid
));
1352 if (open_pack_index(e
.p
)) {
1353 midx_report(_("failed to load pack-index for packfile %s"),
1358 m_offset
= e
.offset
;
1359 p_offset
= find_pack_entry_one(oid
.hash
, e
.p
);
1361 if (m_offset
!= p_offset
)
1362 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64
" != %"PRIx64
),
1363 pairs
[i
].pos
, oid_to_hex(&oid
), m_offset
, p_offset
);
1365 midx_display_sparse_progress(progress
, i
+ 1);
1367 stop_progress(&progress
);
1371 return verify_midx_error
;
1374 int expire_midx_packs(struct repository
*r
, const char *object_dir
, unsigned flags
)
1376 uint32_t i
, *count
, result
= 0;
1377 struct string_list packs_to_drop
= STRING_LIST_INIT_DUP
;
1378 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1379 struct progress
*progress
= NULL
;
1384 CALLOC_ARRAY(count
, m
->num_packs
);
1386 if (flags
& MIDX_PROGRESS
)
1387 progress
= start_delayed_progress(_("Counting referenced objects"),
1389 for (i
= 0; i
< m
->num_objects
; i
++) {
1390 int pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1391 count
[pack_int_id
]++;
1392 display_progress(progress
, i
+ 1);
1394 stop_progress(&progress
);
1396 if (flags
& MIDX_PROGRESS
)
1397 progress
= start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
1399 for (i
= 0; i
< m
->num_packs
; i
++) {
1401 display_progress(progress
, i
+ 1);
1406 if (prepare_midx_pack(r
, m
, i
))
1409 if (m
->packs
[i
]->pack_keep
)
1412 pack_name
= xstrdup(m
->packs
[i
]->pack_name
);
1413 close_pack(m
->packs
[i
]);
1415 string_list_insert(&packs_to_drop
, m
->pack_names
[i
]);
1416 unlink_pack_path(pack_name
, 0);
1419 stop_progress(&progress
);
1423 if (packs_to_drop
.nr
)
1424 result
= write_midx_internal(object_dir
, m
, &packs_to_drop
, NULL
, flags
);
1426 string_list_clear(&packs_to_drop
, 0);
1430 struct repack_info
{
1432 uint32_t referenced_objects
;
1433 uint32_t pack_int_id
;
1436 static int compare_by_mtime(const void *a_
, const void *b_
)
1438 const struct repack_info
*a
, *b
;
1440 a
= (const struct repack_info
*)a_
;
1441 b
= (const struct repack_info
*)b_
;
1443 if (a
->mtime
< b
->mtime
)
1445 if (a
->mtime
> b
->mtime
)
1450 static int fill_included_packs_all(struct repository
*r
,
1451 struct multi_pack_index
*m
,
1452 unsigned char *include_pack
)
1454 uint32_t i
, count
= 0;
1455 int pack_kept_objects
= 0;
1457 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1459 for (i
= 0; i
< m
->num_packs
; i
++) {
1460 if (prepare_midx_pack(r
, m
, i
))
1462 if (!pack_kept_objects
&& m
->packs
[i
]->pack_keep
)
1465 include_pack
[i
] = 1;
1472 static int fill_included_packs_batch(struct repository
*r
,
1473 struct multi_pack_index
*m
,
1474 unsigned char *include_pack
,
1477 uint32_t i
, packs_to_repack
;
1479 struct repack_info
*pack_info
= xcalloc(m
->num_packs
, sizeof(struct repack_info
));
1480 int pack_kept_objects
= 0;
1482 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1484 for (i
= 0; i
< m
->num_packs
; i
++) {
1485 pack_info
[i
].pack_int_id
= i
;
1487 if (prepare_midx_pack(r
, m
, i
))
1490 pack_info
[i
].mtime
= m
->packs
[i
]->mtime
;
1493 for (i
= 0; batch_size
&& i
< m
->num_objects
; i
++) {
1494 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1495 pack_info
[pack_int_id
].referenced_objects
++;
1498 QSORT(pack_info
, m
->num_packs
, compare_by_mtime
);
1501 packs_to_repack
= 0;
1502 for (i
= 0; total_size
< batch_size
&& i
< m
->num_packs
; i
++) {
1503 int pack_int_id
= pack_info
[i
].pack_int_id
;
1504 struct packed_git
*p
= m
->packs
[pack_int_id
];
1505 size_t expected_size
;
1509 if (!pack_kept_objects
&& p
->pack_keep
)
1511 if (open_pack_index(p
) || !p
->num_objects
)
1514 expected_size
= (size_t)(p
->pack_size
1515 * pack_info
[i
].referenced_objects
);
1516 expected_size
/= p
->num_objects
;
1518 if (expected_size
>= batch_size
)
1522 total_size
+= expected_size
;
1523 include_pack
[pack_int_id
] = 1;
1528 if (packs_to_repack
< 2)
1534 int midx_repack(struct repository
*r
, const char *object_dir
, size_t batch_size
, unsigned flags
)
1538 unsigned char *include_pack
;
1539 struct child_process cmd
= CHILD_PROCESS_INIT
;
1541 struct strbuf base_name
= STRBUF_INIT
;
1542 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1545 * When updating the default for these configuration
1546 * variables in builtin/repack.c, these must be adjusted
1549 int delta_base_offset
= 1;
1550 int use_delta_islands
= 0;
1555 CALLOC_ARRAY(include_pack
, m
->num_packs
);
1558 if (fill_included_packs_batch(r
, m
, include_pack
, batch_size
))
1560 } else if (fill_included_packs_all(r
, m
, include_pack
))
1563 repo_config_get_bool(r
, "repack.usedeltabaseoffset", &delta_base_offset
);
1564 repo_config_get_bool(r
, "repack.usedeltaislands", &use_delta_islands
);
1566 strvec_push(&cmd
.args
, "pack-objects");
1568 strbuf_addstr(&base_name
, object_dir
);
1569 strbuf_addstr(&base_name
, "/pack/pack");
1570 strvec_push(&cmd
.args
, base_name
.buf
);
1572 if (delta_base_offset
)
1573 strvec_push(&cmd
.args
, "--delta-base-offset");
1574 if (use_delta_islands
)
1575 strvec_push(&cmd
.args
, "--delta-islands");
1577 if (flags
& MIDX_PROGRESS
)
1578 strvec_push(&cmd
.args
, "--progress");
1580 strvec_push(&cmd
.args
, "-q");
1582 strbuf_release(&base_name
);
1585 cmd
.in
= cmd
.out
= -1;
1587 if (start_command(&cmd
)) {
1588 error(_("could not start pack-objects"));
1593 cmd_in
= xfdopen(cmd
.in
, "w");
1595 for (i
= 0; i
< m
->num_objects
; i
++) {
1596 struct object_id oid
;
1597 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1599 if (!include_pack
[pack_int_id
])
1602 nth_midxed_object_oid(&oid
, m
, i
);
1603 fprintf(cmd_in
, "%s\n", oid_to_hex(&oid
));
1607 if (finish_command(&cmd
)) {
1608 error(_("could not finish pack-objects"));
1613 result
= write_midx_internal(object_dir
, m
, NULL
, NULL
, flags
);