git-bundle: assorted fixes
[git/spearce.git] / index-pack.c
blob64d75f8c4d592ce39d390ed6e89fa58b9374c68d
1 #include "cache.h"
2 #include "delta.h"
3 #include "pack.h"
4 #include "csum-file.h"
5 #include "blob.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "tree.h"
10 static const char index_pack_usage[] =
11 "git-index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
13 struct object_entry
15 unsigned long offset;
16 unsigned long size;
17 unsigned int hdr_size;
18 enum object_type type;
19 enum object_type real_type;
20 unsigned char sha1[20];
23 union delta_base {
24 unsigned char sha1[20];
25 unsigned long offset;
29 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
30 * to memcmp() only the first 20 bytes.
32 #define UNION_BASE_SZ 20
34 struct delta_entry
36 union delta_base base;
37 int obj_no;
40 static struct object_entry *objects;
41 static struct delta_entry *deltas;
42 static int nr_objects;
43 static int nr_deltas;
44 static int nr_resolved_deltas;
46 static int from_stdin;
47 static int verbose;
49 static volatile sig_atomic_t progress_update;
51 static void progress_interval(int signum)
53 progress_update = 1;
56 static void setup_progress_signal(void)
58 struct sigaction sa;
59 struct itimerval v;
61 memset(&sa, 0, sizeof(sa));
62 sa.sa_handler = progress_interval;
63 sigemptyset(&sa.sa_mask);
64 sa.sa_flags = SA_RESTART;
65 sigaction(SIGALRM, &sa, NULL);
67 v.it_interval.tv_sec = 1;
68 v.it_interval.tv_usec = 0;
69 v.it_value = v.it_interval;
70 setitimer(ITIMER_REAL, &v, NULL);
74 static unsigned display_progress(unsigned n, unsigned total, unsigned last_pc)
76 unsigned percent = n * 100 / total;
77 if (percent != last_pc || progress_update) {
78 fprintf(stderr, "%4u%% (%u/%u) done\r", percent, n, total);
79 progress_update = 0;
81 return percent;
84 /* We always read in 4kB chunks. */
85 static unsigned char input_buffer[4096];
86 static unsigned long input_offset, input_len, consumed_bytes;
87 static SHA_CTX input_ctx;
88 static int input_fd, output_fd, pack_fd;
90 /* Discard current buffer used content. */
91 static void flush(void)
93 if (input_offset) {
94 if (output_fd >= 0)
95 write_or_die(output_fd, input_buffer, input_offset);
96 SHA1_Update(&input_ctx, input_buffer, input_offset);
97 memmove(input_buffer, input_buffer + input_offset, input_len);
98 input_offset = 0;
103 * Make sure at least "min" bytes are available in the buffer, and
104 * return the pointer to the buffer.
106 static void *fill(int min)
108 if (min <= input_len)
109 return input_buffer + input_offset;
110 if (min > sizeof(input_buffer))
111 die("cannot fill %d bytes", min);
112 flush();
113 do {
114 int ret = xread(input_fd, input_buffer + input_len,
115 sizeof(input_buffer) - input_len);
116 if (ret <= 0) {
117 if (!ret)
118 die("early EOF");
119 die("read error on input: %s", strerror(errno));
121 input_len += ret;
122 } while (input_len < min);
123 return input_buffer;
126 static void use(int bytes)
128 if (bytes > input_len)
129 die("used more bytes than were available");
130 input_len -= bytes;
131 input_offset += bytes;
132 consumed_bytes += bytes;
135 static const char *open_pack_file(const char *pack_name)
137 if (from_stdin) {
138 input_fd = 0;
139 if (!pack_name) {
140 static char tmpfile[PATH_MAX];
141 snprintf(tmpfile, sizeof(tmpfile),
142 "%s/pack_XXXXXX", get_object_directory());
143 output_fd = mkstemp(tmpfile);
144 pack_name = xstrdup(tmpfile);
145 } else
146 output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
147 if (output_fd < 0)
148 die("unable to create %s: %s\n", pack_name, strerror(errno));
149 pack_fd = output_fd;
150 } else {
151 input_fd = open(pack_name, O_RDONLY);
152 if (input_fd < 0)
153 die("cannot open packfile '%s': %s",
154 pack_name, strerror(errno));
155 output_fd = -1;
156 pack_fd = input_fd;
158 SHA1_Init(&input_ctx);
159 return pack_name;
162 static void parse_pack_header(void)
164 struct pack_header *hdr = fill(sizeof(struct pack_header));
166 /* Header consistency check */
167 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
168 die("pack signature mismatch");
169 if (!pack_version_ok(hdr->hdr_version))
170 die("pack version %d unsupported", ntohl(hdr->hdr_version));
172 nr_objects = ntohl(hdr->hdr_entries);
173 use(sizeof(struct pack_header));
176 static void bad_object(unsigned long offset, const char *format,
177 ...) NORETURN __attribute__((format (printf, 2, 3)));
179 static void bad_object(unsigned long offset, const char *format, ...)
181 va_list params;
182 char buf[1024];
184 va_start(params, format);
185 vsnprintf(buf, sizeof(buf), format, params);
186 va_end(params);
187 die("pack has bad object at offset %lu: %s", offset, buf);
190 static void *unpack_entry_data(unsigned long offset, unsigned long size)
192 z_stream stream;
193 void *buf = xmalloc(size);
195 memset(&stream, 0, sizeof(stream));
196 stream.next_out = buf;
197 stream.avail_out = size;
198 stream.next_in = fill(1);
199 stream.avail_in = input_len;
200 inflateInit(&stream);
202 for (;;) {
203 int ret = inflate(&stream, 0);
204 use(input_len - stream.avail_in);
205 if (stream.total_out == size && ret == Z_STREAM_END)
206 break;
207 if (ret != Z_OK)
208 bad_object(offset, "inflate returned %d", ret);
209 stream.next_in = fill(1);
210 stream.avail_in = input_len;
212 inflateEnd(&stream);
213 return buf;
216 static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
218 unsigned char *p, c;
219 unsigned long size, base_offset;
220 unsigned shift;
222 obj->offset = consumed_bytes;
224 p = fill(1);
225 c = *p;
226 use(1);
227 obj->type = (c >> 4) & 7;
228 size = (c & 15);
229 shift = 4;
230 while (c & 0x80) {
231 p = fill(1);
232 c = *p;
233 use(1);
234 size += (c & 0x7fUL) << shift;
235 shift += 7;
237 obj->size = size;
239 switch (obj->type) {
240 case OBJ_REF_DELTA:
241 hashcpy(delta_base->sha1, fill(20));
242 use(20);
243 break;
244 case OBJ_OFS_DELTA:
245 memset(delta_base, 0, sizeof(*delta_base));
246 p = fill(1);
247 c = *p;
248 use(1);
249 base_offset = c & 127;
250 while (c & 128) {
251 base_offset += 1;
252 if (!base_offset || base_offset & ~(~0UL >> 7))
253 bad_object(obj->offset, "offset value overflow for delta base object");
254 p = fill(1);
255 c = *p;
256 use(1);
257 base_offset = (base_offset << 7) + (c & 127);
259 delta_base->offset = obj->offset - base_offset;
260 if (delta_base->offset >= obj->offset)
261 bad_object(obj->offset, "delta base offset is out of bound");
262 break;
263 case OBJ_COMMIT:
264 case OBJ_TREE:
265 case OBJ_BLOB:
266 case OBJ_TAG:
267 break;
268 default:
269 bad_object(obj->offset, "unknown object type %d", obj->type);
271 obj->hdr_size = consumed_bytes - obj->offset;
273 return unpack_entry_data(obj->offset, obj->size);
276 static void *get_data_from_pack(struct object_entry *obj)
278 unsigned long from = obj[0].offset + obj[0].hdr_size;
279 unsigned long len = obj[1].offset - from;
280 unsigned char *src, *data;
281 z_stream stream;
282 int st;
284 src = xmalloc(len);
285 if (pread(pack_fd, src, len, from) != len)
286 die("cannot pread pack file: %s", strerror(errno));
287 data = xmalloc(obj->size);
288 memset(&stream, 0, sizeof(stream));
289 stream.next_out = data;
290 stream.avail_out = obj->size;
291 stream.next_in = src;
292 stream.avail_in = len;
293 inflateInit(&stream);
294 while ((st = inflate(&stream, Z_FINISH)) == Z_OK);
295 inflateEnd(&stream);
296 if (st != Z_STREAM_END || stream.total_out != obj->size)
297 die("serious inflate inconsistency");
298 free(src);
299 return data;
302 static int find_delta(const union delta_base *base)
304 int first = 0, last = nr_deltas;
306 while (first < last) {
307 int next = (first + last) / 2;
308 struct delta_entry *delta = &deltas[next];
309 int cmp;
311 cmp = memcmp(base, &delta->base, UNION_BASE_SZ);
312 if (!cmp)
313 return next;
314 if (cmp < 0) {
315 last = next;
316 continue;
318 first = next+1;
320 return -first-1;
323 static int find_delta_children(const union delta_base *base,
324 int *first_index, int *last_index)
326 int first = find_delta(base);
327 int last = first;
328 int end = nr_deltas - 1;
330 if (first < 0)
331 return -1;
332 while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
333 --first;
334 while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
335 ++last;
336 *first_index = first;
337 *last_index = last;
338 return 0;
341 static void sha1_object(const void *data, unsigned long size,
342 enum object_type type, unsigned char *sha1)
344 SHA_CTX ctx;
345 char header[50];
346 int header_size;
347 const char *type_str;
349 switch (type) {
350 case OBJ_COMMIT: type_str = commit_type; break;
351 case OBJ_TREE: type_str = tree_type; break;
352 case OBJ_BLOB: type_str = blob_type; break;
353 case OBJ_TAG: type_str = tag_type; break;
354 default:
355 die("bad type %d", type);
358 header_size = sprintf(header, "%s %lu", type_str, size) + 1;
360 SHA1_Init(&ctx);
361 SHA1_Update(&ctx, header, header_size);
362 SHA1_Update(&ctx, data, size);
363 SHA1_Final(sha1, &ctx);
366 static void resolve_delta(struct object_entry *delta_obj, void *base_data,
367 unsigned long base_size, enum object_type type)
369 void *delta_data;
370 unsigned long delta_size;
371 void *result;
372 unsigned long result_size;
373 union delta_base delta_base;
374 int j, first, last;
376 delta_obj->real_type = type;
377 delta_data = get_data_from_pack(delta_obj);
378 delta_size = delta_obj->size;
379 result = patch_delta(base_data, base_size, delta_data, delta_size,
380 &result_size);
381 free(delta_data);
382 if (!result)
383 bad_object(delta_obj->offset, "failed to apply delta");
384 sha1_object(result, result_size, type, delta_obj->sha1);
385 nr_resolved_deltas++;
387 hashcpy(delta_base.sha1, delta_obj->sha1);
388 if (!find_delta_children(&delta_base, &first, &last)) {
389 for (j = first; j <= last; j++) {
390 struct object_entry *child = objects + deltas[j].obj_no;
391 if (child->real_type == OBJ_REF_DELTA)
392 resolve_delta(child, result, result_size, type);
396 memset(&delta_base, 0, sizeof(delta_base));
397 delta_base.offset = delta_obj->offset;
398 if (!find_delta_children(&delta_base, &first, &last)) {
399 for (j = first; j <= last; j++) {
400 struct object_entry *child = objects + deltas[j].obj_no;
401 if (child->real_type == OBJ_OFS_DELTA)
402 resolve_delta(child, result, result_size, type);
406 free(result);
409 static int compare_delta_entry(const void *a, const void *b)
411 const struct delta_entry *delta_a = a;
412 const struct delta_entry *delta_b = b;
413 return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ);
416 /* Parse all objects and return the pack content SHA1 hash */
417 static void parse_pack_objects(unsigned char *sha1)
419 int i, percent = -1;
420 struct delta_entry *delta = deltas;
421 void *data;
422 struct stat st;
425 * First pass:
426 * - find locations of all objects;
427 * - calculate SHA1 of all non-delta objects;
428 * - remember base (SHA1 or offset) for all deltas.
430 if (verbose)
431 fprintf(stderr, "Indexing %d objects.\n", nr_objects);
432 for (i = 0; i < nr_objects; i++) {
433 struct object_entry *obj = &objects[i];
434 data = unpack_raw_entry(obj, &delta->base);
435 obj->real_type = obj->type;
436 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
437 nr_deltas++;
438 delta->obj_no = i;
439 delta++;
440 } else
441 sha1_object(data, obj->size, obj->type, obj->sha1);
442 free(data);
443 if (verbose)
444 percent = display_progress(i+1, nr_objects, percent);
446 objects[i].offset = consumed_bytes;
447 if (verbose)
448 fputc('\n', stderr);
450 /* Check pack integrity */
451 flush();
452 SHA1_Final(sha1, &input_ctx);
453 if (hashcmp(fill(20), sha1))
454 die("pack is corrupted (SHA1 mismatch)");
455 use(20);
457 /* If input_fd is a file, we should have reached its end now. */
458 if (fstat(input_fd, &st))
459 die("cannot fstat packfile: %s", strerror(errno));
460 if (S_ISREG(st.st_mode) &&
461 lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
462 die("pack has junk at the end");
464 if (!nr_deltas)
465 return;
467 /* Sort deltas by base SHA1/offset for fast searching */
468 qsort(deltas, nr_deltas, sizeof(struct delta_entry),
469 compare_delta_entry);
472 * Second pass:
473 * - for all non-delta objects, look if it is used as a base for
474 * deltas;
475 * - if used as a base, uncompress the object and apply all deltas,
476 * recursively checking if the resulting object is used as a base
477 * for some more deltas.
479 if (verbose)
480 fprintf(stderr, "Resolving %d deltas.\n", nr_deltas);
481 for (i = 0; i < nr_objects; i++) {
482 struct object_entry *obj = &objects[i];
483 union delta_base base;
484 int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last;
486 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
487 continue;
488 hashcpy(base.sha1, obj->sha1);
489 ref = !find_delta_children(&base, &ref_first, &ref_last);
490 memset(&base, 0, sizeof(base));
491 base.offset = obj->offset;
492 ofs = !find_delta_children(&base, &ofs_first, &ofs_last);
493 if (!ref && !ofs)
494 continue;
495 data = get_data_from_pack(obj);
496 if (ref)
497 for (j = ref_first; j <= ref_last; j++) {
498 struct object_entry *child = objects + deltas[j].obj_no;
499 if (child->real_type == OBJ_REF_DELTA)
500 resolve_delta(child, data,
501 obj->size, obj->type);
503 if (ofs)
504 for (j = ofs_first; j <= ofs_last; j++) {
505 struct object_entry *child = objects + deltas[j].obj_no;
506 if (child->real_type == OBJ_OFS_DELTA)
507 resolve_delta(child, data,
508 obj->size, obj->type);
510 free(data);
511 if (verbose)
512 percent = display_progress(nr_resolved_deltas,
513 nr_deltas, percent);
515 if (verbose && nr_resolved_deltas == nr_deltas)
516 fputc('\n', stderr);
519 static int write_compressed(int fd, void *in, unsigned int size)
521 z_stream stream;
522 unsigned long maxsize;
523 void *out;
525 memset(&stream, 0, sizeof(stream));
526 deflateInit(&stream, zlib_compression_level);
527 maxsize = deflateBound(&stream, size);
528 out = xmalloc(maxsize);
530 /* Compress it */
531 stream.next_in = in;
532 stream.avail_in = size;
533 stream.next_out = out;
534 stream.avail_out = maxsize;
535 while (deflate(&stream, Z_FINISH) == Z_OK);
536 deflateEnd(&stream);
538 size = stream.total_out;
539 write_or_die(fd, out, size);
540 free(out);
541 return size;
544 static void append_obj_to_pack(void *buf,
545 unsigned long size, enum object_type type)
547 struct object_entry *obj = &objects[nr_objects++];
548 unsigned char header[10];
549 unsigned long s = size;
550 int n = 0;
551 unsigned char c = (type << 4) | (s & 15);
552 s >>= 4;
553 while (s) {
554 header[n++] = c | 0x80;
555 c = s & 0x7f;
556 s >>= 7;
558 header[n++] = c;
559 write_or_die(output_fd, header, n);
560 obj[1].offset = obj[0].offset + n;
561 obj[1].offset += write_compressed(output_fd, buf, size);
562 sha1_object(buf, size, type, obj->sha1);
565 static int delta_pos_compare(const void *_a, const void *_b)
567 struct delta_entry *a = *(struct delta_entry **)_a;
568 struct delta_entry *b = *(struct delta_entry **)_b;
569 return a->obj_no - b->obj_no;
572 static void fix_unresolved_deltas(int nr_unresolved)
574 struct delta_entry **sorted_by_pos;
575 int i, n = 0, percent = -1;
578 * Since many unresolved deltas may well be themselves base objects
579 * for more unresolved deltas, we really want to include the
580 * smallest number of base objects that would cover as much delta
581 * as possible by picking the
582 * trunc deltas first, allowing for other deltas to resolve without
583 * additional base objects. Since most base objects are to be found
584 * before deltas depending on them, a good heuristic is to start
585 * resolving deltas in the same order as their position in the pack.
587 sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
588 for (i = 0; i < nr_deltas; i++) {
589 if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
590 continue;
591 sorted_by_pos[n++] = &deltas[i];
593 qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
595 for (i = 0; i < n; i++) {
596 struct delta_entry *d = sorted_by_pos[i];
597 void *data;
598 unsigned long size;
599 char type[10];
600 enum object_type obj_type;
601 int j, first, last;
603 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
604 continue;
605 data = read_sha1_file(d->base.sha1, type, &size);
606 if (!data)
607 continue;
608 if (!strcmp(type, blob_type)) obj_type = OBJ_BLOB;
609 else if (!strcmp(type, tree_type)) obj_type = OBJ_TREE;
610 else if (!strcmp(type, commit_type)) obj_type = OBJ_COMMIT;
611 else if (!strcmp(type, tag_type)) obj_type = OBJ_TAG;
612 else die("base object %s is of type '%s'",
613 sha1_to_hex(d->base.sha1), type);
615 find_delta_children(&d->base, &first, &last);
616 for (j = first; j <= last; j++) {
617 struct object_entry *child = objects + deltas[j].obj_no;
618 if (child->real_type == OBJ_REF_DELTA)
619 resolve_delta(child, data, size, obj_type);
622 append_obj_to_pack(data, size, obj_type);
623 free(data);
624 if (verbose)
625 percent = display_progress(nr_resolved_deltas,
626 nr_deltas, percent);
628 free(sorted_by_pos);
629 if (verbose)
630 fputc('\n', stderr);
633 static void readjust_pack_header_and_sha1(unsigned char *sha1)
635 struct pack_header hdr;
636 SHA_CTX ctx;
637 int size;
639 /* Rewrite pack header with updated object number */
640 if (lseek(output_fd, 0, SEEK_SET) != 0)
641 die("cannot seek back: %s", strerror(errno));
642 if (read_in_full(output_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
643 die("cannot read pack header back: %s", strerror(errno));
644 hdr.hdr_entries = htonl(nr_objects);
645 if (lseek(output_fd, 0, SEEK_SET) != 0)
646 die("cannot seek back: %s", strerror(errno));
647 write_or_die(output_fd, &hdr, sizeof(hdr));
648 if (lseek(output_fd, 0, SEEK_SET) != 0)
649 die("cannot seek back: %s", strerror(errno));
651 /* Recompute and store the new pack's SHA1 */
652 SHA1_Init(&ctx);
653 do {
654 unsigned char *buf[4096];
655 size = xread(output_fd, buf, sizeof(buf));
656 if (size < 0)
657 die("cannot read pack data back: %s", strerror(errno));
658 SHA1_Update(&ctx, buf, size);
659 } while (size > 0);
660 SHA1_Final(sha1, &ctx);
661 write_or_die(output_fd, sha1, 20);
664 static int sha1_compare(const void *_a, const void *_b)
666 struct object_entry *a = *(struct object_entry **)_a;
667 struct object_entry *b = *(struct object_entry **)_b;
668 return hashcmp(a->sha1, b->sha1);
672 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
673 * the SHA1 hash of sorted object names.
675 static const char *write_index_file(const char *index_name, unsigned char *sha1)
677 struct sha1file *f;
678 struct object_entry **sorted_by_sha, **list, **last;
679 unsigned int array[256];
680 int i, fd;
681 SHA_CTX ctx;
683 if (nr_objects) {
684 sorted_by_sha =
685 xcalloc(nr_objects, sizeof(struct object_entry *));
686 list = sorted_by_sha;
687 last = sorted_by_sha + nr_objects;
688 for (i = 0; i < nr_objects; ++i)
689 sorted_by_sha[i] = &objects[i];
690 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
691 sha1_compare);
694 else
695 sorted_by_sha = list = last = NULL;
697 if (!index_name) {
698 static char tmpfile[PATH_MAX];
699 snprintf(tmpfile, sizeof(tmpfile),
700 "%s/index_XXXXXX", get_object_directory());
701 fd = mkstemp(tmpfile);
702 index_name = xstrdup(tmpfile);
703 } else {
704 unlink(index_name);
705 fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
707 if (fd < 0)
708 die("unable to create %s: %s", index_name, strerror(errno));
709 f = sha1fd(fd, index_name);
712 * Write the first-level table (the list is sorted,
713 * but we use a 256-entry lookup to be able to avoid
714 * having to do eight extra binary search iterations).
716 for (i = 0; i < 256; i++) {
717 struct object_entry **next = list;
718 while (next < last) {
719 struct object_entry *obj = *next;
720 if (obj->sha1[0] != i)
721 break;
722 next++;
724 array[i] = htonl(next - sorted_by_sha);
725 list = next;
727 sha1write(f, array, 256 * sizeof(int));
729 /* recompute the SHA1 hash of sorted object names.
730 * currently pack-objects does not do this, but that
731 * can be fixed.
733 SHA1_Init(&ctx);
735 * Write the actual SHA1 entries..
737 list = sorted_by_sha;
738 for (i = 0; i < nr_objects; i++) {
739 struct object_entry *obj = *list++;
740 unsigned int offset = htonl(obj->offset);
741 sha1write(f, &offset, 4);
742 sha1write(f, obj->sha1, 20);
743 SHA1_Update(&ctx, obj->sha1, 20);
745 sha1write(f, sha1, 20);
746 sha1close(f, NULL, 1);
747 free(sorted_by_sha);
748 SHA1_Final(sha1, &ctx);
749 return index_name;
752 static void final(const char *final_pack_name, const char *curr_pack_name,
753 const char *final_index_name, const char *curr_index_name,
754 const char *keep_name, const char *keep_msg,
755 unsigned char *sha1)
757 char *report = "pack";
758 char name[PATH_MAX];
759 int err;
761 if (!from_stdin) {
762 close(input_fd);
763 } else {
764 err = close(output_fd);
765 if (err)
766 die("error while closing pack file: %s", strerror(errno));
767 chmod(curr_pack_name, 0444);
770 if (keep_msg) {
771 int keep_fd, keep_msg_len = strlen(keep_msg);
772 if (!keep_name) {
773 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
774 get_object_directory(), sha1_to_hex(sha1));
775 keep_name = name;
777 keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
778 if (keep_fd < 0) {
779 if (errno != EEXIST)
780 die("cannot write keep file");
781 } else {
782 if (keep_msg_len > 0) {
783 write_or_die(keep_fd, keep_msg, keep_msg_len);
784 write_or_die(keep_fd, "\n", 1);
786 close(keep_fd);
787 report = "keep";
791 if (final_pack_name != curr_pack_name) {
792 if (!final_pack_name) {
793 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
794 get_object_directory(), sha1_to_hex(sha1));
795 final_pack_name = name;
797 if (move_temp_to_file(curr_pack_name, final_pack_name))
798 die("cannot store pack file");
801 chmod(curr_index_name, 0444);
802 if (final_index_name != curr_index_name) {
803 if (!final_index_name) {
804 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
805 get_object_directory(), sha1_to_hex(sha1));
806 final_index_name = name;
808 if (move_temp_to_file(curr_index_name, final_index_name))
809 die("cannot store index file");
812 if (!from_stdin) {
813 printf("%s\n", sha1_to_hex(sha1));
814 } else {
815 char buf[48];
816 int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
817 report, sha1_to_hex(sha1));
818 write_or_die(1, buf, len);
821 * Let's just mimic git-unpack-objects here and write
822 * the last part of the input buffer to stdout.
824 while (input_len) {
825 err = xwrite(1, input_buffer + input_offset, input_len);
826 if (err <= 0)
827 break;
828 input_len -= err;
829 input_offset += err;
834 int main(int argc, char **argv)
836 int i, fix_thin_pack = 0;
837 const char *curr_pack, *pack_name = NULL;
838 const char *curr_index, *index_name = NULL;
839 const char *keep_name = NULL, *keep_msg = NULL;
840 char *index_name_buf = NULL, *keep_name_buf = NULL;
841 unsigned char sha1[20];
843 for (i = 1; i < argc; i++) {
844 const char *arg = argv[i];
846 if (*arg == '-') {
847 if (!strcmp(arg, "--stdin")) {
848 from_stdin = 1;
849 } else if (!strcmp(arg, "--fix-thin")) {
850 fix_thin_pack = 1;
851 } else if (!strcmp(arg, "--keep")) {
852 keep_msg = "";
853 } else if (!strncmp(arg, "--keep=", 7)) {
854 keep_msg = arg + 7;
855 } else if (!strncmp(arg, "--pack_header=", 14)) {
856 struct pack_header *hdr;
857 char *c;
859 hdr = (struct pack_header *)input_buffer;
860 hdr->hdr_signature = htonl(PACK_SIGNATURE);
861 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
862 if (*c != ',')
863 die("bad %s", arg);
864 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
865 if (*c)
866 die("bad %s", arg);
867 input_len = sizeof(*hdr);
868 } else if (!strcmp(arg, "-v")) {
869 verbose = 1;
870 } else if (!strcmp(arg, "-o")) {
871 if (index_name || (i+1) >= argc)
872 usage(index_pack_usage);
873 index_name = argv[++i];
874 } else
875 usage(index_pack_usage);
876 continue;
879 if (pack_name)
880 usage(index_pack_usage);
881 pack_name = arg;
884 if (!pack_name && !from_stdin)
885 usage(index_pack_usage);
886 if (fix_thin_pack && !from_stdin)
887 die("--fix-thin cannot be used without --stdin");
888 if (!index_name && pack_name) {
889 int len = strlen(pack_name);
890 if (!has_extension(pack_name, ".pack"))
891 die("packfile name '%s' does not end with '.pack'",
892 pack_name);
893 index_name_buf = xmalloc(len);
894 memcpy(index_name_buf, pack_name, len - 5);
895 strcpy(index_name_buf + len - 5, ".idx");
896 index_name = index_name_buf;
898 if (keep_msg && !keep_name && pack_name) {
899 int len = strlen(pack_name);
900 if (!has_extension(pack_name, ".pack"))
901 die("packfile name '%s' does not end with '.pack'",
902 pack_name);
903 keep_name_buf = xmalloc(len);
904 memcpy(keep_name_buf, pack_name, len - 5);
905 strcpy(keep_name_buf + len - 5, ".keep");
906 keep_name = keep_name_buf;
909 curr_pack = open_pack_file(pack_name);
910 parse_pack_header();
911 objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
912 deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
913 if (verbose)
914 setup_progress_signal();
915 parse_pack_objects(sha1);
916 if (nr_deltas != nr_resolved_deltas) {
917 if (fix_thin_pack) {
918 int nr_unresolved = nr_deltas - nr_resolved_deltas;
919 int nr_objects_initial = nr_objects;
920 if (nr_unresolved <= 0)
921 die("confusion beyond insanity");
922 objects = xrealloc(objects,
923 (nr_objects + nr_unresolved + 1)
924 * sizeof(*objects));
925 fix_unresolved_deltas(nr_unresolved);
926 if (verbose)
927 fprintf(stderr, "%d objects were added to complete this thin pack.\n",
928 nr_objects - nr_objects_initial);
929 readjust_pack_header_and_sha1(sha1);
931 if (nr_deltas != nr_resolved_deltas)
932 die("pack has %d unresolved deltas",
933 nr_deltas - nr_resolved_deltas);
934 } else {
935 /* Flush remaining pack final 20-byte SHA1. */
936 flush();
938 free(deltas);
939 curr_index = write_index_file(index_name, sha1);
940 final(pack_name, curr_pack,
941 index_name, curr_index,
942 keep_name, keep_msg,
943 sha1);
944 free(objects);
945 free(index_name_buf);
946 free(keep_name_buf);
948 return 0;