Skip to content

Commit c502d28

Browse files
pks-tgitster
authored andcommitted
midx: write multi-pack indices via their source
Similar to the preceding commit, refactor the writing side of multi-pack indices so that we pass in the object database source where the index should be written to. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d1288f4 commit c502d28

File tree

4 files changed

+47
-51
lines changed

4 files changed

+47
-51
lines changed

builtin/multi-pack-index.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
147147
N_("refs snapshot for selecting bitmap commits")),
148148
OPT_END(),
149149
};
150+
struct odb_source *source;
150151
int ret;
151152

152153
opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE;
@@ -165,7 +166,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
165166
if (argc)
166167
usage_with_options(builtin_multi_pack_index_write_usage,
167168
options);
168-
handle_object_dir_option(repo);
169+
source = handle_object_dir_option(repo);
169170

170171
FREE_AND_NULL(options);
171172

@@ -174,7 +175,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
174175

175176
read_packs_from_stdin(&packs);
176177

177-
ret = write_midx_file_only(repo, opts.object_dir, &packs,
178+
ret = write_midx_file_only(source, &packs,
178179
opts.preferred_pack,
179180
opts.refs_snapshot, opts.flags);
180181

@@ -185,7 +186,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
185186

186187
}
187188

188-
ret = write_midx_file(repo, opts.object_dir, opts.preferred_pack,
189+
ret = write_midx_file(source, opts.preferred_pack,
189190
opts.refs_snapshot, opts.flags);
190191

191192
free(opts.refs_snapshot);
@@ -233,6 +234,8 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv,
233234
N_("force progress reporting"), MIDX_PROGRESS),
234235
OPT_END(),
235236
};
237+
struct odb_source *source;
238+
236239
options = add_common_options(builtin_multi_pack_index_expire_options);
237240

238241
trace2_cmd_mode(argv[0]);
@@ -245,11 +248,11 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv,
245248
if (argc)
246249
usage_with_options(builtin_multi_pack_index_expire_usage,
247250
options);
248-
handle_object_dir_option(the_repository);
251+
source = handle_object_dir_option(the_repository);
249252

250253
FREE_AND_NULL(options);
251254

252-
return expire_midx_packs(the_repository, opts.object_dir, opts.flags);
255+
return expire_midx_packs(source, opts.flags);
253256
}
254257

255258
static int cmd_multi_pack_index_repack(int argc, const char **argv,
@@ -264,6 +267,7 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv,
264267
N_("force progress reporting"), MIDX_PROGRESS),
265268
OPT_END(),
266269
};
270+
struct odb_source *source;
267271

268272
options = add_common_options(builtin_multi_pack_index_repack_options);
269273

@@ -278,12 +282,11 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv,
278282
if (argc)
279283
usage_with_options(builtin_multi_pack_index_repack_usage,
280284
options);
281-
handle_object_dir_option(the_repository);
285+
source = handle_object_dir_option(the_repository);
282286

283287
FREE_AND_NULL(options);
284288

285-
return midx_repack(the_repository, opts.object_dir,
286-
(size_t)opts.batch_size, opts.flags);
289+
return midx_repack(source, (size_t)opts.batch_size, opts.flags);
287290
}
288291

289292
int cmd_multi_pack_index(int argc,

builtin/repack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ int cmd_repack(int argc,
17111711
unsigned flags = 0;
17121712
if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
17131713
flags |= MIDX_WRITE_INCREMENTAL;
1714-
write_midx_file(the_repository, repo_get_object_directory(the_repository),
1714+
write_midx_file(the_repository->objects->sources,
17151715
NULL, NULL, flags);
17161716
}
17171717

midx-write.c

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -913,15 +913,6 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
913913
return ret;
914914
}
915915

916-
static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
917-
const char *object_dir)
918-
{
919-
struct odb_source *source = odb_find_source(r->objects, object_dir);
920-
if (!source)
921-
die(_("could not find object directory matching %s"), object_dir);
922-
return get_multi_pack_index(source);
923-
}
924-
925916
static int fill_packs_from_midx(struct write_midx_context *ctx,
926917
const char *preferred_pack_name, uint32_t flags)
927918
{
@@ -1012,7 +1003,7 @@ static int link_midx_to_chain(struct multi_pack_index *m)
10121003
return ret;
10131004
}
10141005

1015-
static void clear_midx_files(struct repository *r, const char *object_dir,
1006+
static void clear_midx_files(struct odb_source *source,
10161007
const char **hashes, uint32_t hashes_nr,
10171008
unsigned incremental)
10181009
{
@@ -1031,30 +1022,31 @@ static void clear_midx_files(struct repository *r, const char *object_dir,
10311022
uint32_t i, j;
10321023

10331024
for (i = 0; i < ARRAY_SIZE(exts); i++) {
1034-
clear_incremental_midx_files_ext(object_dir, exts[i],
1025+
clear_incremental_midx_files_ext(source->path, exts[i],
10351026
hashes, hashes_nr);
10361027
for (j = 0; j < hashes_nr; j++)
1037-
clear_midx_files_ext(object_dir, exts[i], hashes[j]);
1028+
clear_midx_files_ext(source->path, exts[i], hashes[j]);
10381029
}
10391030

10401031
if (incremental)
1041-
get_midx_filename(r->hash_algo, &buf, object_dir);
1032+
get_midx_filename(source->odb->repo->hash_algo, &buf, source->path);
10421033
else
1043-
get_midx_chain_filename(&buf, object_dir);
1034+
get_midx_chain_filename(&buf, source->path);
10441035

10451036
if (unlink(buf.buf) && errno != ENOENT)
10461037
die_errno(_("failed to clear multi-pack-index at %s"), buf.buf);
10471038

10481039
strbuf_release(&buf);
10491040
}
10501041

1051-
static int write_midx_internal(struct repository *r, const char *object_dir,
1042+
static int write_midx_internal(struct odb_source *source,
10521043
struct string_list *packs_to_include,
10531044
struct string_list *packs_to_drop,
10541045
const char *preferred_pack_name,
10551046
const char *refs_snapshot,
10561047
unsigned flags)
10571048
{
1049+
struct repository *r = source->odb->repo;
10581050
struct strbuf midx_name = STRBUF_INIT;
10591051
unsigned char midx_hash[GIT_MAX_RAWSZ];
10601052
uint32_t i, start_pack;
@@ -1078,15 +1070,15 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
10781070
if (ctx.incremental)
10791071
strbuf_addf(&midx_name,
10801072
"%s/pack/multi-pack-index.d/tmp_midx_XXXXXX",
1081-
object_dir);
1073+
source->path);
10821074
else
1083-
get_midx_filename(r->hash_algo, &midx_name, object_dir);
1075+
get_midx_filename(r->hash_algo, &midx_name, source->path);
10841076
if (safe_create_leading_directories(r, midx_name.buf))
10851077
die_errno(_("unable to create leading directories of %s"),
10861078
midx_name.buf);
10871079

10881080
if (!packs_to_include || ctx.incremental) {
1089-
struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1081+
struct multi_pack_index *m = get_multi_pack_index(source);
10901082
if (m && !midx_checksum_valid(m)) {
10911083
warning(_("ignoring existing multi-pack-index; checksum mismatch"));
10921084
m = NULL;
@@ -1140,7 +1132,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11401132

11411133
ctx.to_include = packs_to_include;
11421134

1143-
for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
1135+
for_each_file_in_pack_dir(source->path, add_pack_to_midx, &ctx);
11441136
stop_progress(&ctx.progress);
11451137

11461138
if ((ctx.m && ctx.nr == ctx.m->num_packs + ctx.m->num_packs_in_base) &&
@@ -1160,7 +1152,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11601152
* corresponding bitmap (or one wasn't requested).
11611153
*/
11621154
if (!want_bitmap)
1163-
clear_midx_files_ext(object_dir, "bitmap", NULL);
1155+
clear_midx_files_ext(source->path, "bitmap", NULL);
11641156
goto cleanup;
11651157
}
11661158
}
@@ -1328,7 +1320,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
13281320
if (ctx.incremental) {
13291321
struct strbuf lock_name = STRBUF_INIT;
13301322

1331-
get_midx_chain_filename(&lock_name, object_dir);
1323+
get_midx_chain_filename(&lock_name, source->path);
13321324
hold_lock_file_for_update(&lk, lock_name.buf, LOCK_DIE_ON_ERROR);
13331325
strbuf_release(&lock_name);
13341326

@@ -1391,7 +1383,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
13911383

13921384
if (flags & MIDX_WRITE_REV_INDEX &&
13931385
git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1394-
write_midx_reverse_index(&ctx, object_dir, midx_hash);
1386+
write_midx_reverse_index(&ctx, source->path, midx_hash);
13951387

13961388
if (flags & MIDX_WRITE_BITMAP) {
13971389
struct packing_data pdata;
@@ -1414,7 +1406,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14141406
FREE_AND_NULL(ctx.entries);
14151407
ctx.entries_nr = 0;
14161408

1417-
if (write_midx_bitmap(&ctx, object_dir,
1409+
if (write_midx_bitmap(&ctx, source->path,
14181410
midx_hash, &pdata, commits, commits_nr,
14191411
flags) < 0) {
14201412
error(_("could not write multi-pack bitmap"));
@@ -1448,7 +1440,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14481440
return -1;
14491441

14501442
get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
1451-
object_dir, midx_hash, MIDX_EXT_MIDX);
1443+
source->path, midx_hash, MIDX_EXT_MIDX);
14521444

14531445
if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
14541446
error_errno(_("unable to rename new multi-pack-index layer"));
@@ -1481,7 +1473,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14811473
if (commit_lock_file(&lk) < 0)
14821474
die_errno(_("could not write multi-pack-index"));
14831475

1484-
clear_midx_files(r, object_dir, keep_hashes,
1476+
clear_midx_files(source, keep_hashes,
14851477
ctx.num_multi_pack_indexes_before + 1,
14861478
ctx.incremental);
14871479

@@ -1510,29 +1502,29 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
15101502
return result;
15111503
}
15121504

1513-
int write_midx_file(struct repository *r, const char *object_dir,
1505+
int write_midx_file(struct odb_source *source,
15141506
const char *preferred_pack_name,
15151507
const char *refs_snapshot, unsigned flags)
15161508
{
1517-
return write_midx_internal(r, object_dir, NULL, NULL,
1509+
return write_midx_internal(source, NULL, NULL,
15181510
preferred_pack_name, refs_snapshot,
15191511
flags);
15201512
}
15211513

1522-
int write_midx_file_only(struct repository *r, const char *object_dir,
1514+
int write_midx_file_only(struct odb_source *source,
15231515
struct string_list *packs_to_include,
15241516
const char *preferred_pack_name,
15251517
const char *refs_snapshot, unsigned flags)
15261518
{
1527-
return write_midx_internal(r, object_dir, packs_to_include, NULL,
1519+
return write_midx_internal(source, packs_to_include, NULL,
15281520
preferred_pack_name, refs_snapshot, flags);
15291521
}
15301522

1531-
int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
1523+
int expire_midx_packs(struct odb_source *source, unsigned flags)
15321524
{
15331525
uint32_t i, *count, result = 0;
15341526
struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
1535-
struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1527+
struct multi_pack_index *m = get_multi_pack_index(source);
15361528
struct progress *progress = NULL;
15371529

15381530
if (!m)
@@ -1545,7 +1537,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
15451537

15461538
if (flags & MIDX_PROGRESS)
15471539
progress = start_delayed_progress(
1548-
r,
1540+
source->odb->repo,
15491541
_("Counting referenced objects"),
15501542
m->num_objects);
15511543
for (i = 0; i < m->num_objects; i++) {
@@ -1557,7 +1549,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
15571549

15581550
if (flags & MIDX_PROGRESS)
15591551
progress = start_delayed_progress(
1560-
r,
1552+
source->odb->repo,
15611553
_("Finding and deleting unreferenced packfiles"),
15621554
m->num_packs);
15631555
for (i = 0; i < m->num_packs; i++) {
@@ -1585,7 +1577,7 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
15851577
free(count);
15861578

15871579
if (packs_to_drop.nr)
1588-
result = write_midx_internal(r, object_dir, NULL,
1580+
result = write_midx_internal(source, NULL,
15891581
&packs_to_drop, NULL, NULL, flags);
15901582

15911583
string_list_clear(&packs_to_drop, 0);
@@ -1710,14 +1702,15 @@ static void fill_included_packs_batch(struct repository *r,
17101702
free(pack_info);
17111703
}
17121704

1713-
int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
1705+
int midx_repack(struct odb_source *source, size_t batch_size, unsigned flags)
17141706
{
1707+
struct repository *r = source->odb->repo;
17151708
int result = 0;
17161709
uint32_t i, packs_to_repack = 0;
17171710
unsigned char *include_pack;
17181711
struct child_process cmd = CHILD_PROCESS_INIT;
17191712
FILE *cmd_in;
1720-
struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1713+
struct multi_pack_index *m = get_multi_pack_index(source);
17211714

17221715
/*
17231716
* When updating the default for these configuration
@@ -1751,7 +1744,7 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
17511744

17521745
strvec_push(&cmd.args, "pack-objects");
17531746

1754-
strvec_pushf(&cmd.args, "%s/pack/pack", object_dir);
1747+
strvec_pushf(&cmd.args, "%s/pack/pack", source->path);
17551748

17561749
if (delta_base_offset)
17571750
strvec_push(&cmd.args, "--delta-base-offset");
@@ -1792,7 +1785,7 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
17921785
goto cleanup;
17931786
}
17941787

1795-
result = write_midx_internal(r, object_dir, NULL, NULL, NULL, NULL,
1788+
result = write_midx_internal(source, NULL, NULL, NULL, NULL,
17961789
flags);
17971790

17981791
cleanup:

midx.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,17 @@ int prepare_multi_pack_index_one(struct odb_source *source);
126126
* Variant of write_midx_file which writes a MIDX containing only the packs
127127
* specified in packs_to_include.
128128
*/
129-
int write_midx_file(struct repository *r, const char *object_dir,
129+
int write_midx_file(struct odb_source *source,
130130
const char *preferred_pack_name, const char *refs_snapshot,
131131
unsigned flags);
132-
int write_midx_file_only(struct repository *r, const char *object_dir,
132+
int write_midx_file_only(struct odb_source *source,
133133
struct string_list *packs_to_include,
134134
const char *preferred_pack_name,
135135
const char *refs_snapshot, unsigned flags);
136136
void clear_midx_file(struct repository *r);
137137
int verify_midx_file(struct odb_source *source, unsigned flags);
138-
int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags);
139-
int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags);
138+
int expire_midx_packs(struct odb_source *source, unsigned flags);
139+
int midx_repack(struct odb_source *source, size_t batch_size, unsigned flags);
140140

141141
void close_midx(struct multi_pack_index *m);
142142

0 commit comments

Comments
 (0)