Merge early part of branch 'np/index-pack' into sp/keep-pack
[git/git.git] / index-pack.c
CommitLineData
9cf6d335
SV
1#include "cache.h"
2#include "delta.h"
3#include "pack.h"
4#include "csum-file.h"
8e440259
PE
5#include "blob.h"
6#include "commit.h"
7#include "tag.h"
8#include "tree.h"
3c9af366
NP
9#include <sys/time.h>
10#include <signal.h>
9cf6d335
SV
11
12static const char index_pack_usage[] =
3c9af366 13"git-index-pack [-v] [-o <index-file>] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
9cf6d335
SV
14
15struct object_entry
16{
17 unsigned long offset;
2d477051
NP
18 unsigned long size;
19 unsigned int hdr_size;
9cf6d335
SV
20 enum object_type type;
21 enum object_type real_type;
22 unsigned char sha1[20];
23};
24
53dda6ff
NP
25union delta_base {
26 unsigned char sha1[20];
27 unsigned long offset;
28};
29
3c552873
NP
30/*
31 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
32 * to memcmp() only the first 20 bytes.
33 */
34#define UNION_BASE_SZ 20
35
9cf6d335
SV
36struct delta_entry
37{
53dda6ff 38 union delta_base base;
636171cb 39 int obj_no;
9cf6d335
SV
40};
41
9cf6d335
SV
42static struct object_entry *objects;
43static struct delta_entry *deltas;
44static int nr_objects;
45static int nr_deltas;
636171cb 46static int nr_resolved_deltas;
9cf6d335 47
e42797f5 48static int from_stdin;
3c9af366
NP
49static int verbose;
50
51static volatile sig_atomic_t progress_update;
52
53static void progress_interval(int signum)
54{
55 progress_update = 1;
56}
57
58static void setup_progress_signal(void)
59{
60 struct sigaction sa;
61 struct itimerval v;
62
63 memset(&sa, 0, sizeof(sa));
64 sa.sa_handler = progress_interval;
65 sigemptyset(&sa.sa_mask);
66 sa.sa_flags = SA_RESTART;
67 sigaction(SIGALRM, &sa, NULL);
68
69 v.it_interval.tv_sec = 1;
70 v.it_interval.tv_usec = 0;
71 v.it_value = v.it_interval;
72 setitimer(ITIMER_REAL, &v, NULL);
73
74}
75
76static unsigned display_progress(unsigned n, unsigned total, unsigned last_pc)
77{
78 unsigned percent = n * 100 / total;
79 if (percent != last_pc || progress_update) {
80 fprintf(stderr, "%4u%% (%u/%u) done\r", percent, n, total);
81 progress_update = 0;
82 }
83 return percent;
84}
e42797f5 85
2d477051
NP
86/* We always read in 4kB chunks. */
87static unsigned char input_buffer[4096];
88static unsigned long input_offset, input_len, consumed_bytes;
89static SHA_CTX input_ctx;
e42797f5 90static int input_fd, output_fd, mmap_fd;
2d477051 91
636171cb
NP
92/* Discard current buffer used content. */
93static void flush()
94{
95 if (input_offset) {
96 if (output_fd >= 0)
97 write_or_die(output_fd, input_buffer, input_offset);
98 SHA1_Update(&input_ctx, input_buffer, input_offset);
99 memcpy(input_buffer, input_buffer + input_offset, input_len);
100 input_offset = 0;
101 }
102}
103
2d477051
NP
104/*
105 * Make sure at least "min" bytes are available in the buffer, and
106 * return the pointer to the buffer.
107 */
b89c4e93 108static void *fill(int min)
9cf6d335 109{
2d477051
NP
110 if (min <= input_len)
111 return input_buffer + input_offset;
112 if (min > sizeof(input_buffer))
113 die("cannot fill %d bytes", min);
636171cb 114 flush();
2d477051
NP
115 do {
116 int ret = xread(input_fd, input_buffer + input_len,
117 sizeof(input_buffer) - input_len);
118 if (ret <= 0) {
119 if (!ret)
120 die("early EOF");
121 die("read error on input: %s", strerror(errno));
122 }
123 input_len += ret;
124 } while (input_len < min);
125 return input_buffer;
126}
127
128static void use(int bytes)
129{
130 if (bytes > input_len)
131 die("used more bytes than were available");
132 input_len -= bytes;
133 input_offset += bytes;
134 consumed_bytes += bytes;
135}
9cf6d335 136
b89c4e93 137static const char *open_pack_file(const char *pack_name)
2d477051 138{
e42797f5
NP
139 if (from_stdin) {
140 input_fd = 0;
141 if (!pack_name) {
142 static char tmpfile[PATH_MAX];
143 snprintf(tmpfile, sizeof(tmpfile),
144 "%s/pack_XXXXXX", get_object_directory());
145 output_fd = mkstemp(tmpfile);
146 pack_name = xstrdup(tmpfile);
147 } else
148 output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
149 if (output_fd < 0)
150 die("unable to create %s: %s\n", pack_name, strerror(errno));
151 mmap_fd = output_fd;
152 } else {
153 input_fd = open(pack_name, O_RDONLY);
154 if (input_fd < 0)
155 die("cannot open packfile '%s': %s",
156 pack_name, strerror(errno));
157 output_fd = -1;
158 mmap_fd = input_fd;
159 }
2d477051 160 SHA1_Init(&input_ctx);
e42797f5 161 return pack_name;
9cf6d335
SV
162}
163
164static void parse_pack_header(void)
165{
2d477051 166 struct pack_header *hdr = fill(sizeof(struct pack_header));
9cf6d335
SV
167
168 /* Header consistency check */
9cf6d335 169 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
e42797f5 170 die("pack signature mismatch");
d60fc1c8 171 if (!pack_version_ok(hdr->hdr_version))
e42797f5 172 die("pack version %d unsupported", ntohl(hdr->hdr_version));
9cf6d335
SV
173
174 nr_objects = ntohl(hdr->hdr_entries);
2d477051 175 use(sizeof(struct pack_header));
9cf6d335
SV
176}
177
178static void bad_object(unsigned long offset, const char *format,
179 ...) NORETURN __attribute__((format (printf, 2, 3)));
180
181static void bad_object(unsigned long offset, const char *format, ...)
182{
183 va_list params;
184 char buf[1024];
185
186 va_start(params, format);
187 vsnprintf(buf, sizeof(buf), format, params);
188 va_end(params);
e42797f5 189 die("pack has bad object at offset %lu: %s", offset, buf);
9cf6d335
SV
190}
191
2d477051 192static void *unpack_entry_data(unsigned long offset, unsigned long size)
9cf6d335 193{
9cf6d335
SV
194 z_stream stream;
195 void *buf = xmalloc(size);
196
197 memset(&stream, 0, sizeof(stream));
198 stream.next_out = buf;
199 stream.avail_out = size;
2d477051
NP
200 stream.next_in = fill(1);
201 stream.avail_in = input_len;
9cf6d335
SV
202 inflateInit(&stream);
203
204 for (;;) {
205 int ret = inflate(&stream, 0);
2d477051
NP
206 use(input_len - stream.avail_in);
207 if (stream.total_out == size && ret == Z_STREAM_END)
9cf6d335
SV
208 break;
209 if (ret != Z_OK)
210 bad_object(offset, "inflate returned %d", ret);
2d477051
NP
211 stream.next_in = fill(1);
212 stream.avail_in = input_len;
9cf6d335
SV
213 }
214 inflateEnd(&stream);
9cf6d335
SV
215 return buf;
216}
217
2d477051 218static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
9cf6d335 219{
2d477051 220 unsigned char *p, c;
53dda6ff 221 unsigned long size, base_offset;
9cf6d335 222 unsigned shift;
9cf6d335 223
2d477051
NP
224 obj->offset = consumed_bytes;
225
226 p = fill(1);
227 c = *p;
228 use(1);
229 obj->type = (c >> 4) & 7;
9cf6d335
SV
230 size = (c & 15);
231 shift = 4;
232 while (c & 0x80) {
2d477051
NP
233 p = fill(1);
234 c = *p;
235 use(1);
9cf6d335
SV
236 size += (c & 0x7fUL) << shift;
237 shift += 7;
238 }
2d477051 239 obj->size = size;
9cf6d335 240
2d477051 241 switch (obj->type) {
eb32d236 242 case OBJ_REF_DELTA:
2d477051
NP
243 hashcpy(delta_base->sha1, fill(20));
244 use(20);
53dda6ff
NP
245 break;
246 case OBJ_OFS_DELTA:
247 memset(delta_base, 0, sizeof(*delta_base));
2d477051
NP
248 p = fill(1);
249 c = *p;
250 use(1);
53dda6ff
NP
251 base_offset = c & 127;
252 while (c & 128) {
253 base_offset += 1;
254 if (!base_offset || base_offset & ~(~0UL >> 7))
2d477051
NP
255 bad_object(obj->offset, "offset value overflow for delta base object");
256 p = fill(1);
257 c = *p;
258 use(1);
53dda6ff
NP
259 base_offset = (base_offset << 7) + (c & 127);
260 }
2d477051
NP
261 delta_base->offset = obj->offset - base_offset;
262 if (delta_base->offset >= obj->offset)
263 bad_object(obj->offset, "delta base offset is out of bound");
53dda6ff 264 break;
9cf6d335
SV
265 case OBJ_COMMIT:
266 case OBJ_TREE:
267 case OBJ_BLOB:
268 case OBJ_TAG:
9cf6d335
SV
269 break;
270 default:
2d477051 271 bad_object(obj->offset, "bad object type %d", obj->type);
9cf6d335 272 }
2d477051
NP
273 obj->hdr_size = consumed_bytes - obj->offset;
274
275 return unpack_entry_data(obj->offset, obj->size);
276}
277
b89c4e93 278static void *get_data_from_pack(struct object_entry *obj)
2d477051
NP
279{
280 unsigned long from = obj[0].offset + obj[0].hdr_size;
281 unsigned long len = obj[1].offset - from;
282 unsigned pg_offset = from % getpagesize();
283 unsigned char *map, *data;
284 z_stream stream;
285 int st;
9cf6d335 286
2d477051 287 map = mmap(NULL, len + pg_offset, PROT_READ, MAP_PRIVATE,
e42797f5 288 mmap_fd, from - pg_offset);
2d477051 289 if (map == MAP_FAILED)
e42797f5 290 die("cannot mmap pack file: %s", strerror(errno));
2d477051
NP
291 data = xmalloc(obj->size);
292 memset(&stream, 0, sizeof(stream));
293 stream.next_out = data;
294 stream.avail_out = obj->size;
295 stream.next_in = map + pg_offset;
296 stream.avail_in = len;
297 inflateInit(&stream);
298 while ((st = inflate(&stream, Z_FINISH)) == Z_OK);
299 inflateEnd(&stream);
300 if (st != Z_STREAM_END || stream.total_out != obj->size)
301 die("serious inflate inconsistency");
302 munmap(map, len + pg_offset);
9cf6d335
SV
303 return data;
304}
305
53dda6ff 306static int find_delta(const union delta_base *base)
9cf6d335
SV
307{
308 int first = 0, last = nr_deltas;
309
310 while (first < last) {
311 int next = (first + last) / 2;
312 struct delta_entry *delta = &deltas[next];
313 int cmp;
314
3c552873 315 cmp = memcmp(base, &delta->base, UNION_BASE_SZ);
9cf6d335
SV
316 if (!cmp)
317 return next;
318 if (cmp < 0) {
319 last = next;
320 continue;
321 }
322 first = next+1;
323 }
324 return -first-1;
325}
326
b89c4e93
NP
327static int find_delta_children(const union delta_base *base,
328 int *first_index, int *last_index)
9cf6d335 329{
53dda6ff 330 int first = find_delta(base);
9cf6d335
SV
331 int last = first;
332 int end = nr_deltas - 1;
333
334 if (first < 0)
335 return -1;
3c552873 336 while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
9cf6d335 337 --first;
3c552873 338 while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
9cf6d335
SV
339 ++last;
340 *first_index = first;
341 *last_index = last;
342 return 0;
343}
344
345static void sha1_object(const void *data, unsigned long size,
346 enum object_type type, unsigned char *sha1)
347{
348 SHA_CTX ctx;
349 char header[50];
350 int header_size;
351 const char *type_str;
352
353 switch (type) {
8e440259
PE
354 case OBJ_COMMIT: type_str = commit_type; break;
355 case OBJ_TREE: type_str = tree_type; break;
356 case OBJ_BLOB: type_str = blob_type; break;
357 case OBJ_TAG: type_str = tag_type; break;
9cf6d335
SV
358 default:
359 die("bad type %d", type);
360 }
361
362 header_size = sprintf(header, "%s %lu", type_str, size) + 1;
363
364 SHA1_Init(&ctx);
365 SHA1_Update(&ctx, header, header_size);
366 SHA1_Update(&ctx, data, size);
367 SHA1_Final(sha1, &ctx);
368}
369
636171cb 370static void resolve_delta(struct object_entry *delta_obj, void *base_data,
9cf6d335
SV
371 unsigned long base_size, enum object_type type)
372{
9cf6d335
SV
373 void *delta_data;
374 unsigned long delta_size;
375 void *result;
376 unsigned long result_size;
53dda6ff 377 union delta_base delta_base;
9cf6d335
SV
378 int j, first, last;
379
636171cb
NP
380 delta_obj->real_type = type;
381 delta_data = get_data_from_pack(delta_obj);
382 delta_size = delta_obj->size;
9cf6d335
SV
383 result = patch_delta(base_data, base_size, delta_data, delta_size,
384 &result_size);
385 free(delta_data);
386 if (!result)
636171cb
NP
387 bad_object(delta_obj->offset, "failed to apply delta");
388 sha1_object(result, result_size, type, delta_obj->sha1);
389 nr_resolved_deltas++;
53dda6ff 390
636171cb 391 hashcpy(delta_base.sha1, delta_obj->sha1);
b89c4e93 392 if (!find_delta_children(&delta_base, &first, &last)) {
636171cb
NP
393 for (j = first; j <= last; j++) {
394 struct object_entry *child = objects + deltas[j].obj_no;
395 if (child->real_type == OBJ_REF_DELTA)
396 resolve_delta(child, result, result_size, type);
397 }
53dda6ff
NP
398 }
399
400 memset(&delta_base, 0, sizeof(delta_base));
636171cb 401 delta_base.offset = delta_obj->offset;
b89c4e93 402 if (!find_delta_children(&delta_base, &first, &last)) {
636171cb
NP
403 for (j = first; j <= last; j++) {
404 struct object_entry *child = objects + deltas[j].obj_no;
405 if (child->real_type == OBJ_OFS_DELTA)
406 resolve_delta(child, result, result_size, type);
407 }
9cf6d335 408 }
53dda6ff 409
9cf6d335
SV
410 free(result);
411}
412
413static int compare_delta_entry(const void *a, const void *b)
414{
415 const struct delta_entry *delta_a = a;
416 const struct delta_entry *delta_b = b;
3c552873 417 return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ);
9cf6d335
SV
418}
419
2d477051
NP
420/* Parse all objects and return the pack content SHA1 hash */
421static void parse_pack_objects(unsigned char *sha1)
9cf6d335 422{
3c9af366 423 int i, percent = -1;
53dda6ff 424 struct delta_entry *delta = deltas;
9cf6d335 425 void *data;
2d477051 426 struct stat st;
9cf6d335
SV
427
428 /*
429 * First pass:
430 * - find locations of all objects;
431 * - calculate SHA1 of all non-delta objects;
b89c4e93 432 * - remember base (SHA1 or offset) for all deltas.
9cf6d335 433 */
3c9af366
NP
434 if (verbose)
435 fprintf(stderr, "Indexing %d objects.\n", nr_objects);
9cf6d335
SV
436 for (i = 0; i < nr_objects; i++) {
437 struct object_entry *obj = &objects[i];
2d477051 438 data = unpack_raw_entry(obj, &delta->base);
9cf6d335 439 obj->real_type = obj->type;
53dda6ff
NP
440 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
441 nr_deltas++;
636171cb 442 delta->obj_no = i;
53dda6ff 443 delta++;
9cf6d335 444 } else
2d477051 445 sha1_object(data, obj->size, obj->type, obj->sha1);
9cf6d335 446 free(data);
3c9af366
NP
447 if (verbose)
448 percent = display_progress(i+1, nr_objects, percent);
9cf6d335 449 }
2d477051 450 objects[i].offset = consumed_bytes;
3c9af366
NP
451 if (verbose)
452 fputc('\n', stderr);
2d477051
NP
453
454 /* Check pack integrity */
636171cb 455 flush();
2d477051
NP
456 SHA1_Final(sha1, &input_ctx);
457 if (hashcmp(fill(20), sha1))
e42797f5 458 die("pack is corrupted (SHA1 mismatch)");
9bee2478 459 use(20);
2d477051
NP
460
461 /* If input_fd is a file, we should have reached its end now. */
462 if (fstat(input_fd, &st))
e42797f5 463 die("cannot fstat packfile: %s", strerror(errno));
9bee2478 464 if (S_ISREG(st.st_mode) && st.st_size != consumed_bytes)
e42797f5 465 die("pack has junk at the end");
9cf6d335 466
3c9af366
NP
467 if (!nr_deltas)
468 return;
469
53dda6ff 470 /* Sort deltas by base SHA1/offset for fast searching */
9cf6d335
SV
471 qsort(deltas, nr_deltas, sizeof(struct delta_entry),
472 compare_delta_entry);
473
474 /*
475 * Second pass:
476 * - for all non-delta objects, look if it is used as a base for
477 * deltas;
478 * - if used as a base, uncompress the object and apply all deltas,
479 * recursively checking if the resulting object is used as a base
480 * for some more deltas.
481 */
3c9af366
NP
482 if (verbose)
483 fprintf(stderr, "Resolving %d deltas.\n", nr_deltas);
9cf6d335
SV
484 for (i = 0; i < nr_objects; i++) {
485 struct object_entry *obj = &objects[i];
53dda6ff
NP
486 union delta_base base;
487 int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last;
9cf6d335 488
53dda6ff 489 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
9cf6d335 490 continue;
53dda6ff 491 hashcpy(base.sha1, obj->sha1);
b89c4e93 492 ref = !find_delta_children(&base, &ref_first, &ref_last);
53dda6ff
NP
493 memset(&base, 0, sizeof(base));
494 base.offset = obj->offset;
b89c4e93 495 ofs = !find_delta_children(&base, &ofs_first, &ofs_last);
53dda6ff 496 if (!ref && !ofs)
9cf6d335 497 continue;
2d477051 498 data = get_data_from_pack(obj);
53dda6ff 499 if (ref)
636171cb
NP
500 for (j = ref_first; j <= ref_last; j++) {
501 struct object_entry *child = objects + deltas[j].obj_no;
502 if (child->real_type == OBJ_REF_DELTA)
503 resolve_delta(child, data,
2d477051 504 obj->size, obj->type);
636171cb 505 }
53dda6ff 506 if (ofs)
636171cb
NP
507 for (j = ofs_first; j <= ofs_last; j++) {
508 struct object_entry *child = objects + deltas[j].obj_no;
509 if (child->real_type == OBJ_OFS_DELTA)
510 resolve_delta(child, data,
2d477051 511 obj->size, obj->type);
636171cb 512 }
9cf6d335 513 free(data);
3c9af366
NP
514 if (verbose)
515 percent = display_progress(nr_resolved_deltas,
516 nr_deltas, percent);
9cf6d335 517 }
3c9af366
NP
518 if (verbose && nr_resolved_deltas == nr_deltas)
519 fputc('\n', stderr);
636171cb
NP
520}
521
522static int write_compressed(int fd, void *in, unsigned int size)
523{
524 z_stream stream;
525 unsigned long maxsize;
526 void *out;
527
528 memset(&stream, 0, sizeof(stream));
529 deflateInit(&stream, zlib_compression_level);
530 maxsize = deflateBound(&stream, size);
531 out = xmalloc(maxsize);
532
533 /* Compress it */
534 stream.next_in = in;
535 stream.avail_in = size;
536 stream.next_out = out;
537 stream.avail_out = maxsize;
538 while (deflate(&stream, Z_FINISH) == Z_OK);
539 deflateEnd(&stream);
540
541 size = stream.total_out;
542 write_or_die(fd, out, size);
543 free(out);
544 return size;
545}
546
547static void append_obj_to_pack(void *buf,
548 unsigned long size, enum object_type type)
549{
550 struct object_entry *obj = &objects[nr_objects++];
551 unsigned char header[10];
552 unsigned long s = size;
553 int n = 0;
554 unsigned char c = (type << 4) | (s & 15);
555 s >>= 4;
556 while (s) {
557 header[n++] = c | 0x80;
558 c = s & 0x7f;
559 s >>= 7;
560 }
561 header[n++] = c;
562 write_or_die(output_fd, header, n);
563 obj[1].offset = obj[0].offset + n;
564 obj[1].offset += write_compressed(output_fd, buf, size);
565 sha1_object(buf, size, type, obj->sha1);
566}
567
568static int delta_pos_compare(const void *_a, const void *_b)
569{
570 struct delta_entry *a = *(struct delta_entry **)_a;
571 struct delta_entry *b = *(struct delta_entry **)_b;
572 return a->obj_no - b->obj_no;
573}
9cf6d335 574
636171cb
NP
575static void fix_unresolved_deltas(int nr_unresolved)
576{
577 struct delta_entry **sorted_by_pos;
3c9af366 578 int i, n = 0, percent = -1;
636171cb
NP
579
580 /*
581 * Since many unresolved deltas may well be themselves base objects
582 * for more unresolved deltas, we really want to include the
583 * smallest number of base objects that would cover as much delta
584 * as possible by picking the
585 * trunc deltas first, allowing for other deltas to resolve without
586 * additional base objects. Since most base objects are to be found
587 * before deltas depending on them, a good heuristic is to start
588 * resolving deltas in the same order as their position in the pack.
589 */
590 sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
9cf6d335 591 for (i = 0; i < nr_deltas; i++) {
636171cb
NP
592 if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
593 continue;
594 sorted_by_pos[n++] = &deltas[i];
9cf6d335 595 }
636171cb
NP
596 qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
597
598 for (i = 0; i < n; i++) {
599 struct delta_entry *d = sorted_by_pos[i];
600 void *data;
601 unsigned long size;
602 char type[10];
603 enum object_type obj_type;
604 int j, first, last;
605
606 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
607 continue;
608 data = read_sha1_file(d->base.sha1, type, &size);
609 if (!data)
610 continue;
611 if (!strcmp(type, blob_type)) obj_type = OBJ_BLOB;
612 else if (!strcmp(type, tree_type)) obj_type = OBJ_TREE;
613 else if (!strcmp(type, commit_type)) obj_type = OBJ_COMMIT;
614 else if (!strcmp(type, tag_type)) obj_type = OBJ_TAG;
615 else die("base object %s is of type '%s'",
616 sha1_to_hex(d->base.sha1), type);
617
b89c4e93 618 find_delta_children(&d->base, &first, &last);
636171cb
NP
619 for (j = first; j <= last; j++) {
620 struct object_entry *child = objects + deltas[j].obj_no;
621 if (child->real_type == OBJ_REF_DELTA)
622 resolve_delta(child, data, size, obj_type);
623 }
624
625 append_obj_to_pack(data, size, obj_type);
626 free(data);
3c9af366
NP
627 if (verbose)
628 percent = display_progress(nr_resolved_deltas,
629 nr_deltas, percent);
636171cb
NP
630 }
631 free(sorted_by_pos);
3c9af366
NP
632 if (verbose)
633 fputc('\n', stderr);
636171cb
NP
634}
635
636static void readjust_pack_header_and_sha1(unsigned char *sha1)
637{
638 struct pack_header hdr;
639 SHA_CTX ctx;
640 int size;
641
642 /* Rewrite pack header with updated object number */
643 if (lseek(output_fd, 0, SEEK_SET) != 0)
644 die("cannot seek back: %s", strerror(errno));
645 if (xread(output_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
646 die("cannot read pack header back: %s", strerror(errno));
647 hdr.hdr_entries = htonl(nr_objects);
648 if (lseek(output_fd, 0, SEEK_SET) != 0)
649 die("cannot seek back: %s", strerror(errno));
650 write_or_die(output_fd, &hdr, sizeof(hdr));
651 if (lseek(output_fd, 0, SEEK_SET) != 0)
652 die("cannot seek back: %s", strerror(errno));
653
654 /* Recompute and store the new pack's SHA1 */
655 SHA1_Init(&ctx);
656 do {
657 unsigned char *buf[4096];
658 size = xread(output_fd, buf, sizeof(buf));
659 if (size < 0)
660 die("cannot read pack data back: %s", strerror(errno));
661 SHA1_Update(&ctx, buf, size);
662 } while (size > 0);
663 SHA1_Final(sha1, &ctx);
664 write_or_die(output_fd, sha1, 20);
9cf6d335
SV
665}
666
667static int sha1_compare(const void *_a, const void *_b)
668{
669 struct object_entry *a = *(struct object_entry **)_a;
670 struct object_entry *b = *(struct object_entry **)_b;
a89fccd2 671 return hashcmp(a->sha1, b->sha1);
9cf6d335
SV
672}
673
2d477051
NP
674/*
675 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
676 * the SHA1 hash of sorted object names.
677 */
b89c4e93 678static const char *write_index_file(const char *index_name, unsigned char *sha1)
9cf6d335
SV
679{
680 struct sha1file *f;
7e4a2a84 681 struct object_entry **sorted_by_sha, **list, **last;
9cf6d335 682 unsigned int array[256];
e42797f5 683 int i, fd;
84c8d8ae 684 SHA_CTX ctx;
9cf6d335 685
7e4a2a84
JH
686 if (nr_objects) {
687 sorted_by_sha =
688 xcalloc(nr_objects, sizeof(struct object_entry *));
689 list = sorted_by_sha;
690 last = sorted_by_sha + nr_objects;
691 for (i = 0; i < nr_objects; ++i)
692 sorted_by_sha[i] = &objects[i];
693 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
694 sha1_compare);
695
696 }
697 else
698 sorted_by_sha = list = last = NULL;
9cf6d335 699
e42797f5
NP
700 if (!index_name) {
701 static char tmpfile[PATH_MAX];
702 snprintf(tmpfile, sizeof(tmpfile),
703 "%s/index_XXXXXX", get_object_directory());
704 fd = mkstemp(tmpfile);
705 index_name = xstrdup(tmpfile);
706 } else {
707 unlink(index_name);
708 fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
709 }
710 if (fd < 0)
711 die("unable to create %s: %s", index_name, strerror(errno));
712 f = sha1fd(fd, index_name);
9cf6d335
SV
713
714 /*
715 * Write the first-level table (the list is sorted,
716 * but we use a 256-entry lookup to be able to avoid
717 * having to do eight extra binary search iterations).
718 */
719 for (i = 0; i < 256; i++) {
720 struct object_entry **next = list;
721 while (next < last) {
722 struct object_entry *obj = *next;
723 if (obj->sha1[0] != i)
724 break;
725 next++;
726 }
727 array[i] = htonl(next - sorted_by_sha);
728 list = next;
729 }
730 sha1write(f, array, 256 * sizeof(int));
731
84c8d8ae
JH
732 /* recompute the SHA1 hash of sorted object names.
733 * currently pack-objects does not do this, but that
734 * can be fixed.
735 */
736 SHA1_Init(&ctx);
9cf6d335
SV
737 /*
738 * Write the actual SHA1 entries..
739 */
740 list = sorted_by_sha;
741 for (i = 0; i < nr_objects; i++) {
742 struct object_entry *obj = *list++;
743 unsigned int offset = htonl(obj->offset);
744 sha1write(f, &offset, 4);
745 sha1write(f, obj->sha1, 20);
84c8d8ae 746 SHA1_Update(&ctx, obj->sha1, 20);
9cf6d335 747 }
2d477051 748 sha1write(f, sha1, 20);
9cf6d335
SV
749 sha1close(f, NULL, 1);
750 free(sorted_by_sha);
84c8d8ae 751 SHA1_Final(sha1, &ctx);
e42797f5
NP
752 return index_name;
753}
754
755static void final(const char *final_pack_name, const char *curr_pack_name,
756 const char *final_index_name, const char *curr_index_name,
757 unsigned char *sha1)
758{
759 char name[PATH_MAX];
760 int err;
761
762 if (!from_stdin) {
763 close(input_fd);
764 } else {
765 err = close(output_fd);
766 if (err)
767 die("error while closing pack file: %s", strerror(errno));
768 chmod(curr_pack_name, 0444);
9bee2478
NP
769
770 /*
771 * Let's just mimic git-unpack-objects here and write
772 * the last part of the buffer to stdout.
773 */
774 while (input_len) {
775 err = xwrite(1, input_buffer + input_offset, input_len);
776 if (err <= 0)
777 break;
778 input_len -= err;
779 input_offset += err;
780 }
e42797f5
NP
781 }
782
783 if (final_pack_name != curr_pack_name) {
784 if (!final_pack_name) {
785 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
786 get_object_directory(), sha1_to_hex(sha1));
787 final_pack_name = name;
788 }
789 if (move_temp_to_file(curr_pack_name, final_pack_name))
790 die("cannot store pack file");
791 }
792
793 chmod(curr_index_name, 0444);
794 if (final_index_name != curr_index_name) {
795 if (!final_index_name) {
796 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
797 get_object_directory(), sha1_to_hex(sha1));
798 final_index_name = name;
799 }
800 if (move_temp_to_file(curr_index_name, final_index_name))
801 die("cannot store index file");
802 }
9cf6d335
SV
803}
804
805int main(int argc, char **argv)
806{
636171cb 807 int i, fix_thin_pack = 0;
e42797f5
NP
808 const char *curr_pack, *pack_name = NULL;
809 const char *curr_index, *index_name = NULL;
9cf6d335 810 char *index_name_buf = NULL;
84c8d8ae 811 unsigned char sha1[20];
9cf6d335
SV
812
813 for (i = 1; i < argc; i++) {
814 const char *arg = argv[i];
815
816 if (*arg == '-') {
e42797f5
NP
817 if (!strcmp(arg, "--stdin")) {
818 from_stdin = 1;
636171cb
NP
819 } else if (!strcmp(arg, "--fix-thin")) {
820 fix_thin_pack = 1;
3c9af366
NP
821 } else if (!strcmp(arg, "-v")) {
822 verbose = 1;
e42797f5 823 } else if (!strcmp(arg, "-o")) {
9cf6d335
SV
824 if (index_name || (i+1) >= argc)
825 usage(index_pack_usage);
826 index_name = argv[++i];
827 } else
828 usage(index_pack_usage);
829 continue;
830 }
831
832 if (pack_name)
833 usage(index_pack_usage);
834 pack_name = arg;
835 }
836
e42797f5 837 if (!pack_name && !from_stdin)
9cf6d335 838 usage(index_pack_usage);
636171cb
NP
839 if (fix_thin_pack && !from_stdin)
840 die("--fix-thin cannot be used without --stdin");
e42797f5 841 if (!index_name && pack_name) {
9cf6d335 842 int len = strlen(pack_name);
5bb1cda5 843 if (!has_extension(pack_name, ".pack"))
9cf6d335
SV
844 die("packfile name '%s' does not end with '.pack'",
845 pack_name);
6689f087 846 index_name_buf = xmalloc(len);
9cf6d335
SV
847 memcpy(index_name_buf, pack_name, len - 5);
848 strcpy(index_name_buf + len - 5, ".idx");
849 index_name = index_name_buf;
850 }
851
e42797f5 852 curr_pack = open_pack_file(pack_name);
9cf6d335 853 parse_pack_header();
636171cb
NP
854 objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
855 deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
3c9af366
NP
856 if (verbose)
857 setup_progress_signal();
2d477051 858 parse_pack_objects(sha1);
636171cb
NP
859 if (nr_deltas != nr_resolved_deltas) {
860 if (fix_thin_pack) {
861 int nr_unresolved = nr_deltas - nr_resolved_deltas;
3c9af366 862 int nr_objects_initial = nr_objects;
636171cb
NP
863 if (nr_unresolved <= 0)
864 die("confusion beyond insanity");
865 objects = xrealloc(objects,
866 (nr_objects + nr_unresolved + 1)
867 * sizeof(*objects));
868 fix_unresolved_deltas(nr_unresolved);
3c9af366
NP
869 if (verbose)
870 fprintf(stderr, "%d objects were added to complete this thin pack.\n",
871 nr_objects - nr_objects_initial);
636171cb
NP
872 readjust_pack_header_and_sha1(sha1);
873 }
874 if (nr_deltas != nr_resolved_deltas)
875 die("pack has %d unresolved deltas",
876 nr_deltas - nr_resolved_deltas);
877 } else {
878 /* Flush remaining pack final 20-byte SHA1. */
636171cb
NP
879 flush();
880 }
9cf6d335 881 free(deltas);
e42797f5
NP
882 curr_index = write_index_file(index_name, sha1);
883 final(pack_name, curr_pack, index_name, curr_index, sha1);
9cf6d335
SV
884 free(objects);
885 free(index_name_buf);
886
9bee2478
NP
887 if (!from_stdin)
888 printf("%s\n", sha1_to_hex(sha1));
84c8d8ae 889
9cf6d335
SV
890 return 0;
891}