Support 'diff=pgm' attribute
[git/git.git] / builtin-unpack-objects.c
CommitLineData
64413630 1#include "builtin.h"
bad50dc8 2#include "cache.h"
74536958 3#include "object.h"
8ee378a0 4#include "delta.h"
a733cb60 5#include "pack.h"
8e440259
PE
6#include "blob.h"
7#include "commit.h"
8#include "tag.h"
9#include "tree.h"
bad50dc8 10
3b67d291
JH
11static int dry_run, quiet, recover, has_errors;
12static const char unpack_usage[] = "git-unpack-objects [-n] [-q] [-r] < pack-file";
bad50dc8 13
67e5a5ec
LT
14/* We always read in 4kB chunks. */
15static unsigned char buffer[4096];
d7dd0223
NP
16static unsigned int offset, len;
17static off_t consumed_bytes;
67e5a5ec 18static SHA_CTX ctx;
bad50dc8 19
67e5a5ec
LT
20/*
21 * Make sure at least "min" bytes are available in the buffer, and
22 * return the pointer to the buffer.
23 */
79a65697 24static void *fill(int min)
67e5a5ec
LT
25{
26 if (min <= len)
27 return buffer + offset;
67e5a5ec
LT
28 if (min > sizeof(buffer))
29 die("cannot fill %d bytes", min);
30 if (offset) {
31 SHA1_Update(&ctx, buffer, offset);
79a65697 32 memmove(buffer, buffer + offset, len);
67e5a5ec
LT
33 offset = 0;
34 }
35 do {
1c15afb9 36 int ret = xread(0, buffer + len, sizeof(buffer) - len);
67e5a5ec
LT
37 if (ret <= 0) {
38 if (!ret)
39 die("early EOF");
67e5a5ec
LT
40 die("read error on input: %s", strerror(errno));
41 }
42 len += ret;
43 } while (len < min);
44 return buffer;
45}
74536958 46
67e5a5ec
LT
47static void use(int bytes)
48{
49 if (bytes > len)
50 die("used more bytes than were available");
51 len -= bytes;
52 offset += bytes;
d7dd0223
NP
53
54 /* make sure off_t is sufficiently large not to wrap */
55 if (consumed_bytes > consumed_bytes + bytes)
56 die("pack too large for current definition of off_t");
209c554a 57 consumed_bytes += bytes;
67e5a5ec 58}
bad50dc8 59
67e5a5ec 60static void *get_data(unsigned long size)
bad50dc8 61{
67e5a5ec
LT
62 z_stream stream;
63 void *buf = xmalloc(size);
64
67e5a5ec
LT
65 memset(&stream, 0, sizeof(stream));
66
67 stream.next_out = buf;
68 stream.avail_out = size;
69 stream.next_in = fill(1);
70 stream.avail_in = len;
71 inflateInit(&stream);
72
73 for (;;) {
74 int ret = inflate(&stream, 0);
75 use(len - stream.avail_in);
76 if (stream.total_out == size && ret == Z_STREAM_END)
77 break;
f986f2c8
JH
78 if (ret != Z_OK) {
79 error("inflate returned %d\n", ret);
80 free(buf);
81 buf = NULL;
3b67d291 82 if (!recover)
f986f2c8
JH
83 exit(1);
84 has_errors = 1;
85 break;
86 }
67e5a5ec
LT
87 stream.next_in = fill(1);
88 stream.avail_in = len;
89 }
ee639140 90 inflateEnd(&stream);
67e5a5ec 91 return buf;
bad50dc8
LT
92}
93
67e5a5ec
LT
94struct delta_info {
95 unsigned char base_sha1[20];
d7dd0223
NP
96 unsigned nr;
97 off_t base_offset;
67e5a5ec
LT
98 unsigned long size;
99 void *delta;
100 struct delta_info *next;
101};
102
103static struct delta_info *delta_list;
104
209c554a 105static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
d7dd0223 106 off_t base_offset,
209c554a 107 void *delta, unsigned long size)
bad50dc8 108{
67e5a5ec 109 struct delta_info *info = xmalloc(sizeof(*info));
bad50dc8 110
e702496e 111 hashcpy(info->base_sha1, base_sha1);
209c554a 112 info->base_offset = base_offset;
67e5a5ec
LT
113 info->size = size;
114 info->delta = delta;
209c554a 115 info->nr = nr;
67e5a5ec
LT
116 info->next = delta_list;
117 delta_list = info;
bad50dc8
LT
118}
119
209c554a 120struct obj_info {
d7dd0223 121 off_t offset;
209c554a
NP
122 unsigned char sha1[20];
123};
124
125static struct obj_info *obj_list;
67e5a5ec 126
21666f1a
NP
127static void added_object(unsigned nr, enum object_type type,
128 void *data, unsigned long size);
209c554a 129
21666f1a
NP
130static void write_object(unsigned nr, enum object_type type,
131 void *buf, unsigned long size)
cca7081a 132{
21666f1a 133 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
cca7081a 134 die("failed to write object");
209c554a 135 added_object(nr, type, buf, size);
cca7081a
LT
136}
137
21666f1a 138static void resolve_delta(unsigned nr, enum object_type type,
f986f2c8
JH
139 void *base, unsigned long base_size,
140 void *delta, unsigned long delta_size)
bad50dc8 141{
67e5a5ec
LT
142 void *result;
143 unsigned long result_size;
bad50dc8 144
67e5a5ec
LT
145 result = patch_delta(base, base_size,
146 delta, delta_size,
147 &result_size);
148 if (!result)
149 die("failed to apply delta");
150 free(delta);
21666f1a 151 write_object(nr, type, result, result_size);
67e5a5ec 152 free(result);
bad50dc8
LT
153}
154
21666f1a
NP
155static void added_object(unsigned nr, enum object_type type,
156 void *data, unsigned long size)
74536958 157{
67e5a5ec
LT
158 struct delta_info **p = &delta_list;
159 struct delta_info *info;
160
161 while ((info = *p) != NULL) {
209c554a
NP
162 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
163 info->base_offset == obj_list[nr].offset) {
67e5a5ec
LT
164 *p = info->next;
165 p = &delta_list;
209c554a
NP
166 resolve_delta(info->nr, type, data, size,
167 info->delta, info->size);
67e5a5ec
LT
168 free(info);
169 continue;
170 }
171 p = &info->next;
172 }
173}
174
21666f1a 175static void unpack_non_delta_entry(enum object_type type, unsigned long size,
209c554a 176 unsigned nr)
67e5a5ec
LT
177{
178 void *buf = get_data(size);
8ee378a0 179
f986f2c8 180 if (!dry_run && buf)
21666f1a 181 write_object(nr, type, buf, size);
67e5a5ec 182 free(buf);
8ee378a0
JH
183}
184
21666f1a 185static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
209c554a 186 unsigned nr)
8ee378a0 187{
67e5a5ec
LT
188 void *delta_data, *base;
189 unsigned long base_size;
67e5a5ec 190 unsigned char base_sha1[20];
8ee378a0 191
21666f1a 192 if (type == OBJ_REF_DELTA) {
209c554a
NP
193 hashcpy(base_sha1, fill(20));
194 use(20);
195 delta_data = get_data(delta_size);
196 if (dry_run || !delta_data) {
197 free(delta_data);
198 return;
199 }
200 if (!has_sha1_file(base_sha1)) {
201 hashcpy(obj_list[nr].sha1, null_sha1);
202 add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
203 return;
204 }
205 } else {
206 unsigned base_found = 0;
207 unsigned char *pack, c;
d7dd0223 208 off_t base_offset;
209c554a 209 unsigned lo, mid, hi;
c4fb06c0 210
209c554a
NP
211 pack = fill(1);
212 c = *pack;
213 use(1);
214 base_offset = c & 127;
215 while (c & 128) {
216 base_offset += 1;
8723f216 217 if (!base_offset || MSB(base_offset, 7))
209c554a
NP
218 die("offset value overflow for delta base object");
219 pack = fill(1);
220 c = *pack;
221 use(1);
222 base_offset = (base_offset << 7) + (c & 127);
223 }
224 base_offset = obj_list[nr].offset - base_offset;
8ee378a0 225
209c554a
NP
226 delta_data = get_data(delta_size);
227 if (dry_run || !delta_data) {
228 free(delta_data);
229 return;
230 }
231 lo = 0;
232 hi = nr;
233 while (lo < hi) {
234 mid = (lo + hi)/2;
235 if (base_offset < obj_list[mid].offset) {
236 hi = mid;
237 } else if (base_offset > obj_list[mid].offset) {
238 lo = mid + 1;
239 } else {
240 hashcpy(base_sha1, obj_list[mid].sha1);
241 base_found = !is_null_sha1(base_sha1);
242 break;
243 }
244 }
245 if (!base_found) {
246 /* The delta base object is itself a delta that
247 has not been resolved yet. */
248 hashcpy(obj_list[nr].sha1, null_sha1);
249 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
250 return;
251 }
8ee378a0 252 }
209c554a 253
21666f1a 254 base = read_sha1_file(base_sha1, &type, &base_size);
f986f2c8
JH
255 if (!base) {
256 error("failed to read delta-pack base object %s",
257 sha1_to_hex(base_sha1));
3b67d291 258 if (!recover)
f986f2c8
JH
259 exit(1);
260 has_errors = 1;
261 return;
262 }
209c554a 263 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
ee639140 264 free(base);
8ee378a0
JH
265}
266
d36f7b80 267static void unpack_one(unsigned nr, unsigned total)
8ee378a0 268{
01247d87 269 unsigned shift;
a733cb60 270 unsigned char *pack, c;
67e5a5ec
LT
271 unsigned long size;
272 enum object_type type;
74536958 273
209c554a
NP
274 obj_list[nr].offset = consumed_bytes;
275
67e5a5ec
LT
276 pack = fill(1);
277 c = *pack;
278 use(1);
a733cb60
LT
279 type = (c >> 4) & 7;
280 size = (c & 15);
01247d87 281 shift = 4;
a733cb60 282 while (c & 0x80) {
67e5a5ec 283 pack = fill(1);
209c554a 284 c = *pack;
67e5a5ec 285 use(1);
01247d87
LT
286 size += (c & 0x7f) << shift;
287 shift += 7;
a733cb60 288 }
d36f7b80
LT
289 if (!quiet) {
290 static unsigned long last_sec;
291 static unsigned last_percent;
292 struct timeval now;
209c554a 293 unsigned percentage = ((nr+1) * 100) / total;
d36f7b80
LT
294
295 gettimeofday(&now, NULL);
296 if (percentage != last_percent || now.tv_sec != last_sec) {
297 last_sec = now.tv_sec;
298 last_percent = percentage;
209c554a
NP
299 fprintf(stderr, "%4u%% (%u/%u) done\r",
300 percentage, (nr+1), total);
d36f7b80
LT
301 }
302 }
a733cb60
LT
303 switch (type) {
304 case OBJ_COMMIT:
305 case OBJ_TREE:
306 case OBJ_BLOB:
307 case OBJ_TAG:
209c554a 308 unpack_non_delta_entry(type, size, nr);
a733cb60 309 return;
eb32d236 310 case OBJ_REF_DELTA:
209c554a
NP
311 case OBJ_OFS_DELTA:
312 unpack_delta_entry(type, size, nr);
a733cb60 313 return;
67e5a5ec 314 default:
f986f2c8
JH
315 error("bad object type %d", type);
316 has_errors = 1;
3b67d291 317 if (recover)
f986f2c8
JH
318 return;
319 exit(1);
74536958
LT
320 }
321}
322
74536958
LT
323static void unpack_all(void)
324{
67e5a5ec
LT
325 int i;
326 struct pack_header *hdr = fill(sizeof(struct pack_header));
67e5a5ec
LT
327 unsigned nr_objects = ntohl(hdr->hdr_entries);
328
329 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
330 die("bad pack file");
d60fc1c8
NP
331 if (!pack_version_ok(hdr->hdr_version))
332 die("unknown pack file version %d", ntohl(hdr->hdr_version));
67e5a5ec
LT
333 fprintf(stderr, "Unpacking %d objects\n", nr_objects);
334
209c554a 335 obj_list = xmalloc(nr_objects * sizeof(*obj_list));
67e5a5ec
LT
336 use(sizeof(struct pack_header));
337 for (i = 0; i < nr_objects; i++)
209c554a 338 unpack_one(i, nr_objects);
67e5a5ec
LT
339 if (delta_list)
340 die("unresolved deltas left after unpacking");
74536958
LT
341}
342
64413630 343int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
bad50dc8
LT
344{
345 int i;
67e5a5ec 346 unsigned char sha1[20];
bad50dc8 347
8e273641 348 git_config(git_default_config);
53228a5f 349
476e8011
JH
350 quiet = !isatty(2);
351
bad50dc8
LT
352 for (i = 1 ; i < argc; i++) {
353 const char *arg = argv[i];
354
355 if (*arg == '-') {
74536958
LT
356 if (!strcmp(arg, "-n")) {
357 dry_run = 1;
358 continue;
359 }
d36f7b80
LT
360 if (!strcmp(arg, "-q")) {
361 quiet = 1;
362 continue;
363 }
f986f2c8 364 if (!strcmp(arg, "-r")) {
3b67d291 365 recover = 1;
f986f2c8
JH
366 continue;
367 }
cc44c765 368 if (!prefixcmp(arg, "--pack_header=")) {
bed006fb
NP
369 struct pack_header *hdr;
370 char *c;
371
372 hdr = (struct pack_header *)buffer;
373 hdr->hdr_signature = htonl(PACK_SIGNATURE);
374 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
375 if (*c != ',')
376 die("bad %s", arg);
377 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
378 if (*c)
379 die("bad %s", arg);
380 len = sizeof(*hdr);
381 continue;
382 }
bad50dc8
LT
383 usage(unpack_usage);
384 }
67e5a5ec
LT
385
386 /* We don't take any non-flag arguments now.. Maybe some day */
bad50dc8 387 usage(unpack_usage);
67e5a5ec
LT
388 }
389 SHA1_Init(&ctx);
74536958 390 unpack_all();
67e5a5ec
LT
391 SHA1_Update(&ctx, buffer, offset);
392 SHA1_Final(sha1, &ctx);
a89fccd2 393 if (hashcmp(fill(20), sha1))
67e5a5ec
LT
394 die("final sha1 did not match");
395 use(20);
396
397 /* Write the last part of the buffer to stdout */
398 while (len) {
1c15afb9
JH
399 int ret = xwrite(1, buffer + offset, len);
400 if (ret <= 0)
67e5a5ec 401 break;
67e5a5ec
LT
402 len -= ret;
403 offset += ret;
404 }
405
406 /* All done */
d36f7b80
LT
407 if (!quiet)
408 fprintf(stderr, "\n");
f986f2c8 409 return has_errors;
bad50dc8 410}