|
| 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 | +} |
0 commit comments