Skip to content

Commit 396f257

Browse files
derrickstoleegitster
authored andcommitted
multi-pack-index: read packfile list
When constructing a multi-pack-index file for a given object directory, read the files within the enclosed pack directory and find matches that end with ".idx" and find the correct paired packfile using add_packed_git(). Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9208e31 commit 396f257

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

midx.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "cache.h"
22
#include "csum-file.h"
3+
#include "dir.h"
34
#include "lockfile.h"
5+
#include "packfile.h"
46
#include "object-store.h"
57
#include "midx.h"
68

@@ -109,12 +111,41 @@ static size_t write_midx_header(struct hashfile *f,
109111
return MIDX_HEADER_SIZE;
110112
}
111113

114+
struct pack_list {
115+
struct packed_git **list;
116+
uint32_t nr;
117+
uint32_t alloc_list;
118+
};
119+
120+
static void add_pack_to_midx(const char *full_path, size_t full_path_len,
121+
const char *file_name, void *data)
122+
{
123+
struct pack_list *packs = (struct pack_list *)data;
124+
125+
if (ends_with(file_name, ".idx")) {
126+
ALLOC_GROW(packs->list, packs->nr + 1, packs->alloc_list);
127+
128+
packs->list[packs->nr] = add_packed_git(full_path,
129+
full_path_len,
130+
0);
131+
if (!packs->list[packs->nr]) {
132+
warning(_("failed to add packfile '%s'"),
133+
full_path);
134+
return;
135+
}
136+
137+
packs->nr++;
138+
}
139+
}
140+
112141
int write_midx_file(const char *object_dir)
113142
{
114143
unsigned char num_chunks = 0;
115144
char *midx_name;
145+
uint32_t i;
116146
struct hashfile *f = NULL;
117147
struct lock_file lk;
148+
struct pack_list packs;
118149

119150
midx_name = get_midx_filename(object_dir);
120151
if (safe_create_leading_directories(midx_name)) {
@@ -123,14 +154,29 @@ int write_midx_file(const char *object_dir)
123154
midx_name);
124155
}
125156

157+
packs.nr = 0;
158+
packs.alloc_list = 16;
159+
packs.list = NULL;
160+
ALLOC_ARRAY(packs.list, packs.alloc_list);
161+
162+
for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
163+
126164
hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
127165
f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
128166
FREE_AND_NULL(midx_name);
129167

130-
write_midx_header(f, num_chunks, 0);
168+
write_midx_header(f, num_chunks, packs.nr);
131169

132170
finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
133171
commit_lock_file(&lk);
134172

173+
for (i = 0; i < packs.nr; i++) {
174+
if (packs.list[i]) {
175+
close_pack(packs.list[i]);
176+
free(packs.list[i]);
177+
}
178+
}
179+
180+
free(packs.list);
135181
return 0;
136182
}

t/t5319-multi-pack-index.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ test_description='multi-pack-indexes'
44
. ./test-lib.sh
55

66
midx_read_expect () {
7+
NUM_PACKS=$1
78
cat >expect <<-EOF
8-
header: 4d494458 1 0 0
9+
header: 4d494458 1 0 $NUM_PACKS
910
object-dir: .
1011
EOF
1112
test-tool read-midx . >actual &&
@@ -15,7 +16,7 @@ midx_read_expect () {
1516
test_expect_success 'write midx with no packs' '
1617
test_when_finished rm -f pack/multi-pack-index &&
1718
git multi-pack-index --object-dir=. write &&
18-
midx_read_expect
19+
midx_read_expect 0
1920
'
2021

2122
generate_objects () {
@@ -65,13 +66,13 @@ test_expect_success 'write midx with one v1 pack' '
6566
pack=$(git pack-objects --index-version=1 pack/test <obj-list) &&
6667
test_when_finished rm pack/test-$pack.pack pack/test-$pack.idx pack/multi-pack-index &&
6768
git multi-pack-index --object-dir=. write &&
68-
midx_read_expect
69+
midx_read_expect 1
6970
'
7071

7172
test_expect_success 'write midx with one v2 pack' '
7273
git pack-objects --index-version=2,0x40 pack/test <obj-list &&
7374
git multi-pack-index --object-dir=. write &&
74-
midx_read_expect
75+
midx_read_expect 1
7576
'
7677

7778
test_expect_success 'add more objects' '
@@ -85,21 +86,21 @@ test_expect_success 'add more objects' '
8586
test_expect_success 'write midx with two packs' '
8687
git pack-objects --index-version=1 pack/test-2 <obj-list &&
8788
git multi-pack-index --object-dir=. write &&
88-
midx_read_expect
89+
midx_read_expect 2
8990
'
9091

9192
test_expect_success 'add more packs' '
9293
for j in $(test_seq 11 20)
9394
do
9495
generate_objects $j &&
9596
commit_and_list_objects &&
96-
git pack-objects --index-version=2 test-pack <obj-list
97+
git pack-objects --index-version=2 pack/test-pack <obj-list
9798
done
9899
'
99100

100101
test_expect_success 'write midx with twelve packs' '
101102
git multi-pack-index --object-dir=. write &&
102-
midx_read_expect
103+
midx_read_expect 12
103104
'
104105

105106
test_done

0 commit comments

Comments
 (0)