Skip to content

Commit 56d863e

Browse files
ttaylorrgitster
authored andcommitted
midx: expose write_midx_file_only() publicly
Expose a variant of the write_midx_file() function which ignores packs that aren't included in an explicit "allow" list. This will be used in an upcoming patch to power a new `--stdin-packs` mode of `git multi-pack-index write` for callers that only want to include certain packs in a MIDX (and ignore any packs which may have happened to enter the repository independently, e.g., from pushes). Those patches will provide test coverage for this new function. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 73cd7d9 commit 56d863e

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

midx.c

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ struct write_midx_context {
475475
uint32_t num_large_offsets;
476476

477477
int preferred_pack_idx;
478+
479+
struct string_list *to_include;
478480
};
479481

480482
static void add_pack_to_midx(const char *full_path, size_t full_path_len,
@@ -484,8 +486,26 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
484486

485487
if (ends_with(file_name, ".idx")) {
486488
display_progress(ctx->progress, ++ctx->pack_paths_checked);
489+
/*
490+
* Note that at most one of ctx->m and ctx->to_include are set,
491+
* so we are testing midx_contains_pack() and
492+
* string_list_has_string() independently (guarded by the
493+
* appropriate NULL checks).
494+
*
495+
* We could support passing to_include while reusing an existing
496+
* MIDX, but don't currently since the reuse process drags
497+
* forward all packs from an existing MIDX (without checking
498+
* whether or not they appear in the to_include list).
499+
*
500+
* If we added support for that, these next two conditional
501+
* should be performed independently (likely checking
502+
* to_include before the existing MIDX).
503+
*/
487504
if (ctx->m && midx_contains_pack(ctx->m, file_name))
488505
return;
506+
else if (ctx->to_include &&
507+
!string_list_has_string(ctx->to_include, file_name))
508+
return;
489509

490510
ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
491511

@@ -1058,6 +1078,7 @@ static int write_midx_bitmap(char *midx_name, unsigned char *midx_hash,
10581078
}
10591079

10601080
static int write_midx_internal(const char *object_dir,
1081+
struct string_list *packs_to_include,
10611082
struct string_list *packs_to_drop,
10621083
const char *preferred_pack_name,
10631084
unsigned flags)
@@ -1082,10 +1103,17 @@ static int write_midx_internal(const char *object_dir,
10821103
die_errno(_("unable to create leading directories of %s"),
10831104
midx_name);
10841105

1085-
for (cur = get_multi_pack_index(the_repository); cur; cur = cur->next) {
1086-
if (!strcmp(object_dir, cur->object_dir)) {
1087-
ctx.m = cur;
1088-
break;
1106+
if (!packs_to_include) {
1107+
/*
1108+
* Only reference an existing MIDX when not filtering which
1109+
* packs to include, since all packs and objects are copied
1110+
* blindly from an existing MIDX if one is present.
1111+
*/
1112+
for (cur = get_multi_pack_index(the_repository); cur; cur = cur->next) {
1113+
if (!strcmp(object_dir, cur->object_dir)) {
1114+
ctx.m = cur;
1115+
break;
1116+
}
10891117
}
10901118
}
10911119

@@ -1136,10 +1164,13 @@ static int write_midx_internal(const char *object_dir,
11361164
else
11371165
ctx.progress = NULL;
11381166

1167+
ctx.to_include = packs_to_include;
1168+
11391169
for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
11401170
stop_progress(&ctx.progress);
11411171

1142-
if (ctx.m && ctx.nr == ctx.m->num_packs && !packs_to_drop) {
1172+
if ((ctx.m && ctx.nr == ctx.m->num_packs) &&
1173+
!(packs_to_include || packs_to_drop)) {
11431174
struct bitmap_index *bitmap_git;
11441175
int bitmap_exists;
11451176
int want_bitmap = flags & MIDX_WRITE_BITMAP;
@@ -1380,7 +1411,17 @@ int write_midx_file(const char *object_dir,
13801411
const char *preferred_pack_name,
13811412
unsigned flags)
13821413
{
1383-
return write_midx_internal(object_dir, NULL, preferred_pack_name, flags);
1414+
return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
1415+
flags);
1416+
}
1417+
1418+
int write_midx_file_only(const char *object_dir,
1419+
struct string_list *packs_to_include,
1420+
const char *preferred_pack_name,
1421+
unsigned flags)
1422+
{
1423+
return write_midx_internal(object_dir, packs_to_include, NULL,
1424+
preferred_pack_name, flags);
13841425
}
13851426

13861427
struct clear_midx_data {
@@ -1660,7 +1701,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
16601701
free(count);
16611702

16621703
if (packs_to_drop.nr) {
1663-
result = write_midx_internal(object_dir, &packs_to_drop, NULL, flags);
1704+
result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, flags);
16641705
m = NULL;
16651706
}
16661707

@@ -1851,7 +1892,7 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
18511892
goto cleanup;
18521893
}
18531894

1854-
result = write_midx_internal(object_dir, NULL, NULL, flags);
1895+
result = write_midx_internal(object_dir, NULL, NULL, NULL, flags);
18551896
m = NULL;
18561897

18571898
cleanup:

midx.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define MIDX_H
33

44
#include "repository.h"
5+
#include "string-list.h"
56

67
struct object_id;
78
struct pack_entry;
@@ -62,6 +63,14 @@ int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
6263
int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local);
6364

6465
int write_midx_file(const char *object_dir, const char *preferred_pack_name, unsigned flags);
66+
/*
67+
* Variant of write_midx_file which writes a MIDX containing only the packs
68+
* specified in packs_to_include.
69+
*/
70+
int write_midx_file_only(const char *object_dir,
71+
struct string_list *packs_to_include,
72+
const char *preferred_pack_name,
73+
unsigned flags);
6574
void clear_midx_file(struct repository *r);
6675
int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags);
6776
int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags);

0 commit comments

Comments
 (0)