Skip to content

Commit 8af1a1e

Browse files
derrickstoleedscho
authored andcommitted
test-tool: add pack-deltas helper
When trying to demonstrate certain behavior in tests, it can be helpful to create packfiles that have specific delta structures. 'git pack-objects' uses various algorithms to select deltas based on their compression rates, but that does not always demonstrate all possible packfile shapes. This becomes especially important when wanting to test 'git index-pack' and its ability to parse certain pack shapes. We have prior art in t/lib-pack.sh, where certain delta structures are produced by manually writing certain opaque pack contents. However, producing these script updates is cumbersome and difficult to do as a contributor. Instead, create a new test-tool, 'test-tool pack-deltas', that reads a list of instructions for which objects to include in a packfile and how those objects should be written in delta form. At the moment, this only supports REF_DELTAs as those are the kinds of deltas needed to exercise a bug in 'git index-pack'. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 702b188 commit 8af1a1e

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ TEST_BUILTINS_OBJS += test-mergesort.o
827827
TEST_BUILTINS_OBJS += test-mktemp.o
828828
TEST_BUILTINS_OBJS += test-name-hash.o
829829
TEST_BUILTINS_OBJS += test-online-cpus.o
830+
TEST_BUILTINS_OBJS += test-pack-deltas.o
830831
TEST_BUILTINS_OBJS += test-pack-mtimes.o
831832
TEST_BUILTINS_OBJS += test-parse-options.o
832833
TEST_BUILTINS_OBJS += test-parse-pathspec-file.o

t/helper/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ test_tool_sources = [
3737
'test-mktemp.c',
3838
'test-name-hash.c',
3939
'test-online-cpus.c',
40+
'test-pack-deltas.c',
4041
'test-pack-mtimes.c',
4142
'test-parse-options.c',
4243
'test-parse-pathspec-file.c',

t/helper/test-pack-deltas.c

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
3+
#include "test-tool.h"
4+
#include "git-compat-util.h"
5+
#include "delta.h"
6+
#include "git-zlib.h"
7+
#include "hash.h"
8+
#include "hex.h"
9+
#include "pack.h"
10+
#include "pack-objects.h"
11+
#include "parse.h"
12+
#include "setup.h"
13+
#include "strbuf.h"
14+
#include "string-list.h"
15+
16+
static const char usage_str[] = "test-tool pack-deltas <nr_entries>";
17+
18+
static unsigned long do_compress(void **pptr, unsigned long size)
19+
{
20+
git_zstream stream;
21+
void *in, *out;
22+
unsigned long maxsize;
23+
24+
git_deflate_init(&stream, 1);
25+
maxsize = git_deflate_bound(&stream, size);
26+
27+
in = *pptr;
28+
out = xmalloc(maxsize);
29+
*pptr = out;
30+
31+
stream.next_in = in;
32+
stream.avail_in = size;
33+
stream.next_out = out;
34+
stream.avail_out = maxsize;
35+
while (git_deflate(&stream, Z_FINISH) == Z_OK)
36+
; /* nothing */
37+
git_deflate_end(&stream);
38+
39+
free(in);
40+
return stream.total_out;
41+
}
42+
43+
static void write_ref_delta(struct hashfile *f,
44+
struct object_id *oid,
45+
struct object_id *base)
46+
{
47+
unsigned char header[MAX_PACK_OBJECT_HEADER];
48+
unsigned long size, base_size, delta_size, compressed_size, hdrlen;
49+
enum object_type type;
50+
void *base_buf, *delta_buf;
51+
void *buf = repo_read_object_file(the_repository,
52+
oid, &type,
53+
&size);
54+
55+
if (!buf)
56+
die("unable to read %s", oid_to_hex(oid));
57+
58+
base_buf = repo_read_object_file(the_repository,
59+
base, &type,
60+
&base_size);
61+
62+
if (!base_buf)
63+
die("unable to read %s", oid_to_hex(base));
64+
65+
delta_buf = diff_delta(base_buf, base_size,
66+
buf, size, &delta_size, 0);
67+
68+
compressed_size = do_compress(&delta_buf, delta_size);
69+
70+
hdrlen = encode_in_pack_object_header(header, sizeof(header),
71+
OBJ_REF_DELTA, delta_size);
72+
hashwrite(f, header, hdrlen);
73+
hashwrite(f, base->hash, the_repository->hash_algo->rawsz);
74+
hashwrite(f, delta_buf, compressed_size);
75+
76+
free(buf);
77+
free(base_buf);
78+
free(delta_buf);
79+
}
80+
81+
int cmd__pack_deltas(int argc, const char **argv)
82+
{
83+
unsigned long n;
84+
struct hashfile *f;
85+
struct strbuf line = STRBUF_INIT;
86+
87+
if (argc != 2) {
88+
usage(usage_str);
89+
return -1;
90+
}
91+
92+
if (!git_parse_ulong(argv[1], &n) || n != (uint32_t)n)
93+
die("invalid number of objects: %s", argv[1]);
94+
95+
setup_git_directory();
96+
97+
f = hashfd(1, "<stdout>");
98+
write_pack_header(f, n);
99+
100+
/* Read each line from stdin into 'line' */
101+
while (strbuf_getline_lf(&line, stdin) != EOF) {
102+
const char *type_str, *content_oid_str, *base_oid_str = NULL;
103+
struct object_id content_oid, base_oid;
104+
struct string_list items = STRING_LIST_INIT_NODUP;
105+
/*
106+
* Tokenize into two or three parts:
107+
* 1. REF_DELTA, OFS_DELTA, or FULL.
108+
* 2. The object ID for the content object.
109+
* 3. The object ID for the base object (optional).
110+
*/
111+
if (string_list_split_in_place(&items, line.buf, " ", 3) < 0)
112+
die("invalid input format: %s", line.buf);
113+
114+
if (items.nr < 2)
115+
die("invalid input format: %s", line.buf);
116+
117+
type_str = items.items[0].string;
118+
content_oid_str = items.items[1].string;
119+
120+
if (get_oid_hex(content_oid_str, &content_oid))
121+
die("invalid object: %s", content_oid_str);
122+
if (items.nr >= 3) {
123+
base_oid_str = items.items[2].string;
124+
if (get_oid_hex(base_oid_str, &base_oid))
125+
die("invalid object: %s", base_oid_str);
126+
}
127+
string_list_clear(&items, 0);
128+
129+
if (!strcmp(type_str, "REF_DELTA"))
130+
write_ref_delta(f, &content_oid, &base_oid);
131+
else if (!strcmp(type_str, "OFS_DELTA"))
132+
die("OFS_DELTA not implemented");
133+
else if (!strcmp(type_str, "FULL"))
134+
die("FULL not implemented");
135+
else
136+
die("unknown pack type: %s", type_str);
137+
}
138+
139+
finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK,
140+
CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
141+
strbuf_release(&line);
142+
return 0;
143+
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static struct test_cmd cmds[] = {
4747
{ "mktemp", cmd__mktemp },
4848
{ "name-hash", cmd__name_hash },
4949
{ "online-cpus", cmd__online_cpus },
50+
{ "pack-deltas", cmd__pack_deltas },
5051
{ "pack-mtimes", cmd__pack_mtimes },
5152
{ "parse-options", cmd__parse_options },
5253
{ "parse-options-flags", cmd__parse_options_flags },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ int cmd__mergesort(int argc, const char **argv);
4040
int cmd__mktemp(int argc, const char **argv);
4141
int cmd__name_hash(int argc, const char **argv);
4242
int cmd__online_cpus(int argc, const char **argv);
43+
int cmd__pack_deltas(int argc, const char **argv);
4344
int cmd__pack_mtimes(int argc, const char **argv);
4445
int cmd__parse_options(int argc, const char **argv);
4546
int cmd__parse_options_flags(int argc, const char **argv);

0 commit comments

Comments
 (0)