Skip to content

Commit c00c0a6

Browse files
pks-tgitster
authored andcommitted
midx: compute paths via their source
With the preceding commits we started to always have the object database source available when we load, write or access multi-pack indices. With this in place we can change how MIDX paths are computed so that we don't have to pass in the combination of a hash algorithm and object directory anymore, but only the object database source. Refactor the code accordingly. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 42a6dd4 commit c00c0a6

File tree

5 files changed

+62
-75
lines changed

5 files changed

+62
-75
lines changed

midx-write.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
#define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
2727

2828
extern int midx_checksum_valid(struct multi_pack_index *m);
29-
extern void clear_midx_files_ext(const char *object_dir, const char *ext,
29+
extern void clear_midx_files_ext(struct odb_source *source, const char *ext,
3030
const char *keep_hash);
31-
extern void clear_incremental_midx_files_ext(const char *object_dir,
31+
extern void clear_incremental_midx_files_ext(struct odb_source *source,
3232
const char *ext,
3333
const char **keep_hashes,
3434
uint32_t hashes_nr);
@@ -112,6 +112,7 @@ struct write_midx_context {
112112
struct string_list *to_include;
113113

114114
struct repository *repo;
115+
struct odb_source *source;
115116
};
116117

117118
static int should_include_pack(const struct write_midx_context *ctx,
@@ -648,7 +649,6 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
648649
}
649650

650651
static void write_midx_reverse_index(struct write_midx_context *ctx,
651-
const char *object_dir,
652652
unsigned char *midx_hash)
653653
{
654654
struct strbuf buf = STRBUF_INIT;
@@ -657,11 +657,10 @@ static void write_midx_reverse_index(struct write_midx_context *ctx,
657657
trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo);
658658

659659
if (ctx->incremental)
660-
get_split_midx_filename_ext(ctx->repo->hash_algo, &buf,
661-
object_dir, midx_hash,
662-
MIDX_EXT_REV);
660+
get_split_midx_filename_ext(ctx->source, &buf,
661+
midx_hash, MIDX_EXT_REV);
663662
else
664-
get_midx_filename_ext(ctx->repo->hash_algo, &buf, object_dir,
663+
get_midx_filename_ext(ctx->source, &buf,
665664
midx_hash, MIDX_EXT_REV);
666665

667666
tmp_file = write_rev_file_order(ctx->repo, NULL, ctx->pack_order,
@@ -836,7 +835,6 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
836835
}
837836

838837
static int write_midx_bitmap(struct write_midx_context *ctx,
839-
const char *object_dir,
840838
const unsigned char *midx_hash,
841839
struct packing_data *pdata,
842840
struct commit **commits,
@@ -852,12 +850,11 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
852850
trace2_region_enter("midx", "write_midx_bitmap", ctx->repo);
853851

854852
if (ctx->incremental)
855-
get_split_midx_filename_ext(ctx->repo->hash_algo, &bitmap_name,
856-
object_dir, midx_hash,
857-
MIDX_EXT_BITMAP);
853+
get_split_midx_filename_ext(ctx->source, &bitmap_name,
854+
midx_hash, MIDX_EXT_BITMAP);
858855
else
859-
get_midx_filename_ext(ctx->repo->hash_algo, &bitmap_name,
860-
object_dir, midx_hash, MIDX_EXT_BITMAP);
856+
get_midx_filename_ext(ctx->source, &bitmap_name,
857+
midx_hash, MIDX_EXT_BITMAP);
861858

862859
if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
863860
options |= BITMAP_OPT_HASH_CACHE;
@@ -981,11 +978,9 @@ static int link_midx_to_chain(struct multi_pack_index *m)
981978
for (i = 0; i < ARRAY_SIZE(midx_exts); i++) {
982979
const unsigned char *hash = get_midx_checksum(m);
983980

984-
get_midx_filename_ext(m->source->odb->repo->hash_algo, &from,
985-
m->source->path,
981+
get_midx_filename_ext(m->source, &from,
986982
hash, midx_exts[i].non_split);
987-
get_split_midx_filename_ext(m->source->odb->repo->hash_algo, &to,
988-
m->source->path, hash,
983+
get_split_midx_filename_ext(m->source, &to, hash,
989984
midx_exts[i].split);
990985

991986
if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
@@ -1023,16 +1018,16 @@ static void clear_midx_files(struct odb_source *source,
10231018
uint32_t i, j;
10241019

10251020
for (i = 0; i < ARRAY_SIZE(exts); i++) {
1026-
clear_incremental_midx_files_ext(source->path, exts[i],
1021+
clear_incremental_midx_files_ext(source, exts[i],
10271022
hashes, hashes_nr);
10281023
for (j = 0; j < hashes_nr; j++)
1029-
clear_midx_files_ext(source->path, exts[i], hashes[j]);
1024+
clear_midx_files_ext(source, exts[i], hashes[j]);
10301025
}
10311026

10321027
if (incremental)
1033-
get_midx_filename(source->odb->repo->hash_algo, &buf, source->path);
1028+
get_midx_filename(source, &buf);
10341029
else
1035-
get_midx_chain_filename(&buf, source->path);
1030+
get_midx_chain_filename(source, &buf);
10361031

10371032
if (unlink(buf.buf) && errno != ENOENT)
10381033
die_errno(_("failed to clear multi-pack-index at %s"), buf.buf);
@@ -1065,6 +1060,7 @@ static int write_midx_internal(struct odb_source *source,
10651060
trace2_region_enter("midx", "write_midx_internal", r);
10661061

10671062
ctx.repo = r;
1063+
ctx.source = source;
10681064

10691065
ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
10701066

@@ -1073,7 +1069,7 @@ static int write_midx_internal(struct odb_source *source,
10731069
"%s/pack/multi-pack-index.d/tmp_midx_XXXXXX",
10741070
source->path);
10751071
else
1076-
get_midx_filename(r->hash_algo, &midx_name, source->path);
1072+
get_midx_filename(source, &midx_name);
10771073
if (safe_create_leading_directories(r, midx_name.buf))
10781074
die_errno(_("unable to create leading directories of %s"),
10791075
midx_name.buf);
@@ -1153,7 +1149,7 @@ static int write_midx_internal(struct odb_source *source,
11531149
* corresponding bitmap (or one wasn't requested).
11541150
*/
11551151
if (!want_bitmap)
1156-
clear_midx_files_ext(source->path, "bitmap", NULL);
1152+
clear_midx_files_ext(source, "bitmap", NULL);
11571153
goto cleanup;
11581154
}
11591155
}
@@ -1321,7 +1317,7 @@ static int write_midx_internal(struct odb_source *source,
13211317
if (ctx.incremental) {
13221318
struct strbuf lock_name = STRBUF_INIT;
13231319

1324-
get_midx_chain_filename(&lock_name, source->path);
1320+
get_midx_chain_filename(source, &lock_name);
13251321
hold_lock_file_for_update(&lk, lock_name.buf, LOCK_DIE_ON_ERROR);
13261322
strbuf_release(&lock_name);
13271323

@@ -1384,7 +1380,7 @@ static int write_midx_internal(struct odb_source *source,
13841380

13851381
if (flags & MIDX_WRITE_REV_INDEX &&
13861382
git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1387-
write_midx_reverse_index(&ctx, source->path, midx_hash);
1383+
write_midx_reverse_index(&ctx, midx_hash);
13881384

13891385
if (flags & MIDX_WRITE_BITMAP) {
13901386
struct packing_data pdata;
@@ -1407,7 +1403,7 @@ static int write_midx_internal(struct odb_source *source,
14071403
FREE_AND_NULL(ctx.entries);
14081404
ctx.entries_nr = 0;
14091405

1410-
if (write_midx_bitmap(&ctx, source->path,
1406+
if (write_midx_bitmap(&ctx,
14111407
midx_hash, &pdata, commits, commits_nr,
14121408
flags) < 0) {
14131409
error(_("could not write multi-pack bitmap"));
@@ -1440,8 +1436,8 @@ static int write_midx_internal(struct odb_source *source,
14401436
if (link_midx_to_chain(ctx.base_midx) < 0)
14411437
return -1;
14421438

1443-
get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
1444-
source->path, midx_hash, MIDX_EXT_MIDX);
1439+
get_split_midx_filename_ext(source, &final_midx_name,
1440+
midx_hash, MIDX_EXT_MIDX);
14451441

14461442
if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
14471443
error_errno(_("unable to rename new multi-pack-index layer"));

midx.c

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
#define MIDX_PACK_ERROR ((void *)(intptr_t)-1)
1717

1818
int midx_checksum_valid(struct multi_pack_index *m);
19-
void clear_midx_files_ext(const char *object_dir, const char *ext,
19+
void clear_midx_files_ext(struct odb_source *source, const char *ext,
2020
const char *keep_hash);
21-
void clear_incremental_midx_files_ext(const char *object_dir, const char *ext,
21+
void clear_incremental_midx_files_ext(struct odb_source *source, const char *ext,
2222
char **keep_hashes,
2323
uint32_t hashes_nr);
2424
int cmp_idx_or_pack_name(const char *idx_or_pack_name,
@@ -29,19 +29,17 @@ const unsigned char *get_midx_checksum(struct multi_pack_index *m)
2929
return m->data + m->data_len - m->source->odb->repo->hash_algo->rawsz;
3030
}
3131

32-
void get_midx_filename(const struct git_hash_algo *hash_algo,
33-
struct strbuf *out, const char *object_dir)
32+
void get_midx_filename(struct odb_source *source, struct strbuf *out)
3433
{
35-
get_midx_filename_ext(hash_algo, out, object_dir, NULL, NULL);
34+
get_midx_filename_ext(source, out, NULL, NULL);
3635
}
3736

38-
void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
39-
struct strbuf *out, const char *object_dir,
37+
void get_midx_filename_ext(struct odb_source *source, struct strbuf *out,
4038
const unsigned char *hash, const char *ext)
4139
{
42-
strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
40+
strbuf_addf(out, "%s/pack/multi-pack-index", source->path);
4341
if (ext)
44-
strbuf_addf(out, "-%s.%s", hash_to_hex_algop(hash, hash_algo), ext);
42+
strbuf_addf(out, "-%s.%s", hash_to_hex_algop(hash, source->odb->repo->hash_algo), ext);
4543
}
4644

4745
static int midx_read_oid_fanout(const unsigned char *chunk_start,
@@ -222,24 +220,23 @@ static struct multi_pack_index *load_multi_pack_index_one(struct odb_source *sou
222220
return NULL;
223221
}
224222

225-
void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir)
223+
void get_midx_chain_dirname(struct odb_source *source, struct strbuf *buf)
226224
{
227-
strbuf_addf(buf, "%s/pack/multi-pack-index.d", object_dir);
225+
strbuf_addf(buf, "%s/pack/multi-pack-index.d", source->path);
228226
}
229227

230-
void get_midx_chain_filename(struct strbuf *buf, const char *object_dir)
228+
void get_midx_chain_filename(struct odb_source *source, struct strbuf *buf)
231229
{
232-
get_midx_chain_dirname(buf, object_dir);
230+
get_midx_chain_dirname(source, buf);
233231
strbuf_addstr(buf, "/multi-pack-index-chain");
234232
}
235233

236-
void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
237-
struct strbuf *buf, const char *object_dir,
234+
void get_split_midx_filename_ext(struct odb_source *source, struct strbuf *buf,
238235
const unsigned char *hash, const char *ext)
239236
{
240-
get_midx_chain_dirname(buf, object_dir);
237+
get_midx_chain_dirname(source, buf);
241238
strbuf_addf(buf, "/multi-pack-index-%s.%s",
242-
hash_to_hex_algop(hash, hash_algo), ext);
239+
hash_to_hex_algop(hash, source->odb->repo->hash_algo), ext);
243240
}
244241

245242
static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
@@ -326,7 +323,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(struct odb_source *source,
326323
valid = 0;
327324

328325
strbuf_reset(&buf);
329-
get_split_midx_filename_ext(hash_algo, &buf, source->path,
326+
get_split_midx_filename_ext(source, &buf,
330327
layer.hash, MIDX_EXT_MIDX);
331328
m = load_multi_pack_index_one(source, buf.buf);
332329

@@ -358,7 +355,7 @@ static struct multi_pack_index *load_multi_pack_index_chain(struct odb_source *s
358355
int fd;
359356
struct multi_pack_index *m = NULL;
360357

361-
get_midx_chain_filename(&chain_file, source->path);
358+
get_midx_chain_filename(source, &chain_file);
362359
if (open_multi_pack_index_chain(source->odb->repo->hash_algo, chain_file.buf, &fd, &st)) {
363360
int incomplete;
364361
/* ownership of fd is taken over by load function */
@@ -374,8 +371,7 @@ struct multi_pack_index *load_multi_pack_index(struct odb_source *source)
374371
struct strbuf midx_name = STRBUF_INIT;
375372
struct multi_pack_index *m;
376373

377-
get_midx_filename(source->odb->repo->hash_algo, &midx_name,
378-
source->path);
374+
get_midx_filename(source, &midx_name);
379375

380376
m = load_multi_pack_index_one(source, midx_name.buf);
381377
if (!m)
@@ -762,7 +758,7 @@ static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUS
762758
die_errno(_("failed to remove %s"), full_path);
763759
}
764760

765-
void clear_midx_files_ext(const char *object_dir, const char *ext,
761+
void clear_midx_files_ext(struct odb_source *source, const char *ext,
766762
const char *keep_hash)
767763
{
768764
struct clear_midx_data data;
@@ -776,7 +772,7 @@ void clear_midx_files_ext(const char *object_dir, const char *ext,
776772
}
777773
data.ext = ext;
778774

779-
for_each_file_in_pack_dir(object_dir,
775+
for_each_file_in_pack_dir(source->path,
780776
clear_midx_file_ext,
781777
&data);
782778

@@ -785,7 +781,7 @@ void clear_midx_files_ext(const char *object_dir, const char *ext,
785781
free(data.keep);
786782
}
787783

788-
void clear_incremental_midx_files_ext(const char *object_dir, const char *ext,
784+
void clear_incremental_midx_files_ext(struct odb_source *source, const char *ext,
789785
char **keep_hashes,
790786
uint32_t hashes_nr)
791787
{
@@ -801,7 +797,7 @@ void clear_incremental_midx_files_ext(const char *object_dir, const char *ext,
801797
data.keep_nr = hashes_nr;
802798
data.ext = ext;
803799

804-
for_each_file_in_pack_subdir(object_dir, "multi-pack-index.d",
800+
for_each_file_in_pack_subdir(source->path, "multi-pack-index.d",
805801
clear_midx_file_ext, &data);
806802

807803
for (i = 0; i < hashes_nr; i++)
@@ -813,7 +809,7 @@ void clear_midx_file(struct repository *r)
813809
{
814810
struct strbuf midx = STRBUF_INIT;
815811

816-
get_midx_filename(r->hash_algo, &midx, r->objects->sources->path);
812+
get_midx_filename(r->objects->sources, &midx);
817813

818814
if (r->objects) {
819815
struct odb_source *source;
@@ -828,8 +824,8 @@ void clear_midx_file(struct repository *r)
828824
if (remove_path(midx.buf))
829825
die(_("failed to clear multi-pack-index at %s"), midx.buf);
830826

831-
clear_midx_files_ext(r->objects->sources->path, MIDX_EXT_BITMAP, NULL);
832-
clear_midx_files_ext(r->objects->sources->path, MIDX_EXT_REV, NULL);
827+
clear_midx_files_ext(r->objects->sources, MIDX_EXT_BITMAP, NULL);
828+
clear_midx_files_ext(r->objects->sources, MIDX_EXT_REV, NULL);
833829

834830
strbuf_release(&midx);
835831
}
@@ -888,7 +884,7 @@ int verify_midx_file(struct odb_source *source, unsigned flags)
888884
struct stat sb;
889885
struct strbuf filename = STRBUF_INIT;
890886

891-
get_midx_filename(r->hash_algo, &filename, source->path);
887+
get_midx_filename(source, &filename);
892888

893889
if (!stat(filename.buf, &sb)) {
894890
error(_("multi-pack-index file exists, but failed to parse"));

midx.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,12 @@ struct multi_pack_index {
8686
#define MIDX_EXT_MIDX "midx"
8787

8888
const unsigned char *get_midx_checksum(struct multi_pack_index *m);
89-
void get_midx_filename(const struct git_hash_algo *hash_algo,
90-
struct strbuf *out, const char *object_dir);
91-
void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
92-
struct strbuf *out, const char *object_dir,
89+
void get_midx_filename(struct odb_source *source, struct strbuf *out);
90+
void get_midx_filename_ext(struct odb_source *source, struct strbuf *out,
9391
const unsigned char *hash, const char *ext);
94-
void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir);
95-
void get_midx_chain_filename(struct strbuf *buf, const char *object_dir);
96-
void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
97-
struct strbuf *buf, const char *object_dir,
92+
void get_midx_chain_dirname(struct odb_source *source, struct strbuf *out);
93+
void get_midx_chain_filename(struct odb_source *source, struct strbuf *out);
94+
void get_split_midx_filename_ext(struct odb_source *source, struct strbuf *buf,
9895
const unsigned char *hash, const char *ext);
9996

10097
struct multi_pack_index *load_multi_pack_index(struct odb_source *source);

pack-bitmap.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,13 +418,12 @@ char *midx_bitmap_filename(struct multi_pack_index *midx)
418418
{
419419
struct strbuf buf = STRBUF_INIT;
420420
if (midx->has_chain)
421-
get_split_midx_filename_ext(midx->source->odb->repo->hash_algo, &buf,
422-
midx->source->path,
421+
get_split_midx_filename_ext(midx->source, &buf,
423422
get_midx_checksum(midx),
424423
MIDX_EXT_BITMAP);
425424
else
426-
get_midx_filename_ext(midx->source->odb->repo->hash_algo, &buf,
427-
midx->source->path, get_midx_checksum(midx),
425+
get_midx_filename_ext(midx->source, &buf,
426+
get_midx_checksum(midx),
428427
MIDX_EXT_BITMAP);
429428

430429
return strbuf_detach(&buf, NULL);
@@ -463,8 +462,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
463462

464463
if (bitmap_git->pack || bitmap_git->midx) {
465464
struct strbuf buf = STRBUF_INIT;
466-
get_midx_filename(midx->source->odb->repo->hash_algo, &buf,
467-
midx->source->path);
465+
get_midx_filename(midx->source, &buf);
468466
trace2_data_string("bitmap", bitmap_repo(bitmap_git),
469467
"ignoring extra midx bitmap file", buf.buf);
470468
close(fd);

pack-revindex.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,12 @@ int load_midx_revindex(struct multi_pack_index *m)
389389
"source", "rev");
390390

391391
if (m->has_chain)
392-
get_split_midx_filename_ext(m->source->odb->repo->hash_algo, &revindex_name,
393-
m->source->path, get_midx_checksum(m),
392+
get_split_midx_filename_ext(m->source, &revindex_name,
393+
get_midx_checksum(m),
394394
MIDX_EXT_REV);
395395
else
396-
get_midx_filename_ext(m->source->odb->repo->hash_algo, &revindex_name,
397-
m->source->path, get_midx_checksum(m),
396+
get_midx_filename_ext(m->source, &revindex_name,
397+
get_midx_checksum(m),
398398
MIDX_EXT_REV);
399399

400400
ret = load_revindex_from_disk(m->source->odb->repo->hash_algo,

0 commit comments

Comments
 (0)