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