blob: 2a366206a4e830d0bb66d6ac708cd512a94d6d37 [file] [log] [blame]
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001#include "cache.h"
2#include "delta.h"
3#include "pack.h"
4#include "csum-file.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02005#include "blob.h"
6#include "commit.h"
7#include "tag.h"
8#include "tree.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -04009#include "progress.h"
Martin Koegler0153be02008-02-25 22:46:12 +010010#include "fsck.h"
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070011
12static const char index_pack_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020013"git index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] [--strict] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070014
15struct object_entry
16{
Geert Boschaa7e44b2007-06-01 15:18:05 -040017 struct pack_idx_entry idx;
Nicolas Pitre2d477052006-10-20 14:45:21 -040018 unsigned long size;
19 unsigned int hdr_size;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070020 enum object_type type;
21 enum object_type real_type;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070022};
23
Nicolas Pitre53dda6f2006-09-21 00:08:33 -040024union delta_base {
25 unsigned char sha1[20];
Nicolas Pitred7dd0222007-04-09 01:06:30 -040026 off_t offset;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -040027};
28
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -040029struct base_data {
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -040030 struct base_data *base;
31 struct base_data *child;
Shawn O. Pearce03993e12008-07-13 22:07:46 -040032 struct object_entry *obj;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -040033 void *data;
34 unsigned long size;
35};
36
Nicolas Pitre3c552872006-10-17 16:23:26 -040037/*
38 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
39 * to memcmp() only the first 20 bytes.
40 */
41#define UNION_BASE_SZ 20
42
Martin Koegler0153be02008-02-25 22:46:12 +010043#define FLAG_LINK (1u<<20)
44#define FLAG_CHECKED (1u<<21)
45
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070046struct delta_entry
47{
Nicolas Pitre53dda6f2006-09-21 00:08:33 -040048 union delta_base base;
Nicolas Pitre636171c2006-10-25 23:28:17 -040049 int obj_no;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070050};
51
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070052static struct object_entry *objects;
53static struct delta_entry *deltas;
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -040054static struct base_data *base_cache;
Shawn O. Pearce92392b42008-07-15 04:45:34 +000055static size_t base_cache_used;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070056static int nr_objects;
57static int nr_deltas;
Nicolas Pitre636171c2006-10-25 23:28:17 -040058static int nr_resolved_deltas;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070059
Nicolas Pitree42797f2006-10-23 14:50:18 -040060static int from_stdin;
Martin Koegler0153be02008-02-25 22:46:12 +010061static int strict;
Nicolas Pitre3c9af362006-10-25 23:32:59 -040062static int verbose;
63
Nicolas Pitredc6a0752007-10-30 14:57:32 -040064static struct progress *progress;
Nicolas Pitree42797f2006-10-23 14:50:18 -040065
Nicolas Pitre2d477052006-10-20 14:45:21 -040066/* We always read in 4kB chunks. */
67static unsigned char input_buffer[4096];
Nicolas Pitred7dd0222007-04-09 01:06:30 -040068static unsigned int input_offset, input_len;
69static off_t consumed_bytes;
Nicolas Pitre2d477052006-10-20 14:45:21 -040070static SHA_CTX input_ctx;
Nicolas Pitreee5743c2007-04-09 01:06:32 -040071static uint32_t input_crc32;
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -050072static int input_fd, output_fd, pack_fd;
Nicolas Pitre2d477052006-10-20 14:45:21 -040073
Martin Koegler0153be02008-02-25 22:46:12 +010074static int mark_link(struct object *obj, int type, void *data)
75{
76 if (!obj)
77 return -1;
78
79 if (type != OBJ_ANY && obj->type != type)
80 die("object type mismatch at %s", sha1_to_hex(obj->sha1));
81
82 obj->flags |= FLAG_LINK;
83 return 0;
84}
85
86/* The content of each linked object must have been checked
87 or it must be already present in the object database */
88static void check_object(struct object *obj)
89{
90 if (!obj)
91 return;
92
93 if (!(obj->flags & FLAG_LINK))
94 return;
95
96 if (!(obj->flags & FLAG_CHECKED)) {
97 unsigned long size;
98 int type = sha1_object_info(obj->sha1, &size);
99 if (type != obj->type || type <= 0)
100 die("object of unexpected type");
101 obj->flags |= FLAG_CHECKED;
102 return;
103 }
104}
105
106static void check_objects(void)
107{
108 unsigned i, max;
109
110 max = get_max_object_index();
111 for (i = 0; i < max; i++)
112 check_object(get_indexed_object(i));
113}
114
115
Nicolas Pitre636171c2006-10-25 23:28:17 -0400116/* Discard current buffer used content. */
Rene Scharfea6e8a762006-11-18 13:07:06 +0100117static void flush(void)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400118{
119 if (input_offset) {
120 if (output_fd >= 0)
121 write_or_die(output_fd, input_buffer, input_offset);
122 SHA1_Update(&input_ctx, input_buffer, input_offset);
Jim Meyering554a2632006-12-11 19:06:34 +0100123 memmove(input_buffer, input_buffer + input_offset, input_len);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400124 input_offset = 0;
125 }
126}
127
Nicolas Pitre2d477052006-10-20 14:45:21 -0400128/*
129 * Make sure at least "min" bytes are available in the buffer, and
130 * return the pointer to the buffer.
131 */
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400132static void *fill(int min)
Nicolas Pitre2d477052006-10-20 14:45:21 -0400133{
134 if (min <= input_len)
135 return input_buffer + input_offset;
136 if (min > sizeof(input_buffer))
137 die("cannot fill %d bytes", min);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400138 flush();
Nicolas Pitre2d477052006-10-20 14:45:21 -0400139 do {
Johan Herland8a912bc2007-05-15 14:49:22 +0200140 ssize_t ret = xread(input_fd, input_buffer + input_len,
Nicolas Pitre2d477052006-10-20 14:45:21 -0400141 sizeof(input_buffer) - input_len);
142 if (ret <= 0) {
143 if (!ret)
144 die("early EOF");
145 die("read error on input: %s", strerror(errno));
146 }
147 input_len += ret;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500148 if (from_stdin)
149 display_throughput(progress, consumed_bytes + input_len);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400150 } while (input_len < min);
151 return input_buffer;
152}
153
154static void use(int bytes)
155{
156 if (bytes > input_len)
157 die("used more bytes than were available");
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400158 input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400159 input_len -= bytes;
160 input_offset += bytes;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400161
162 /* make sure off_t is sufficiently large not to wrap */
163 if (consumed_bytes > consumed_bytes + bytes)
164 die("pack too large for current definition of off_t");
Nicolas Pitre2d477052006-10-20 14:45:21 -0400165 consumed_bytes += bytes;
166}
167
Nicolas Pitre4049b9c2007-10-16 21:55:49 -0400168static char *open_pack_file(char *pack_name)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700169{
Nicolas Pitree42797f2006-10-23 14:50:18 -0400170 if (from_stdin) {
171 input_fd = 0;
172 if (!pack_name) {
173 static char tmpfile[PATH_MAX];
174 snprintf(tmpfile, sizeof(tmpfile),
Petr Baudis8b4eb6b2008-09-22 19:20:21 +0200175 "%s/pack/tmp_pack_XXXXXX", get_object_directory());
Luiz Fernando N. Capitulino7647b172007-08-14 16:45:58 -0300176 output_fd = xmkstemp(tmpfile);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400177 pack_name = xstrdup(tmpfile);
178 } else
179 output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
180 if (output_fd < 0)
181 die("unable to create %s: %s\n", pack_name, strerror(errno));
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500182 pack_fd = output_fd;
Nicolas Pitree42797f2006-10-23 14:50:18 -0400183 } else {
184 input_fd = open(pack_name, O_RDONLY);
185 if (input_fd < 0)
186 die("cannot open packfile '%s': %s",
187 pack_name, strerror(errno));
188 output_fd = -1;
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500189 pack_fd = input_fd;
Nicolas Pitree42797f2006-10-23 14:50:18 -0400190 }
Nicolas Pitre2d477052006-10-20 14:45:21 -0400191 SHA1_Init(&input_ctx);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400192 return pack_name;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700193}
194
195static void parse_pack_header(void)
196{
Nicolas Pitre2d477052006-10-20 14:45:21 -0400197 struct pack_header *hdr = fill(sizeof(struct pack_header));
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700198
199 /* Header consistency check */
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700200 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
Nicolas Pitree42797f2006-10-23 14:50:18 -0400201 die("pack signature mismatch");
Nicolas Pitred60fc1c2006-02-09 17:50:04 -0500202 if (!pack_version_ok(hdr->hdr_version))
Ramsay Jones6e1c2342008-07-03 16:52:09 +0100203 die("pack version %"PRIu32" unsupported",
204 ntohl(hdr->hdr_version));
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700205
206 nr_objects = ntohl(hdr->hdr_entries);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400207 use(sizeof(struct pack_header));
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700208}
209
210static void bad_object(unsigned long offset, const char *format,
211 ...) NORETURN __attribute__((format (printf, 2, 3)));
212
213static void bad_object(unsigned long offset, const char *format, ...)
214{
215 va_list params;
216 char buf[1024];
217
218 va_start(params, format);
219 vsnprintf(buf, sizeof(buf), format, params);
220 va_end(params);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400221 die("pack has bad object at offset %lu: %s", offset, buf);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700222}
223
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000224static void prune_base_data(struct base_data *retain)
225{
226 struct base_data *b = base_cache;
227 for (b = base_cache;
228 base_cache_used > delta_base_cache_limit && b;
229 b = b->child) {
230 if (b->data && b != retain) {
231 free(b->data);
232 b->data = NULL;
233 base_cache_used -= b->size;
234 }
235 }
236}
237
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400238static void link_base_data(struct base_data *base, struct base_data *c)
239{
240 if (base)
241 base->child = c;
242 else
243 base_cache = c;
244
245 c->base = base;
246 c->child = NULL;
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000247 base_cache_used += c->size;
248 prune_base_data(c);
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400249}
250
251static void unlink_base_data(struct base_data *c)
252{
253 struct base_data *base = c->base;
254 if (base)
255 base->child = NULL;
256 else
257 base_cache = NULL;
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000258 if (c->data) {
259 free(c->data);
260 base_cache_used -= c->size;
261 }
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400262}
263
Nicolas Pitre2d477052006-10-20 14:45:21 -0400264static void *unpack_entry_data(unsigned long offset, unsigned long size)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700265{
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700266 z_stream stream;
267 void *buf = xmalloc(size);
268
269 memset(&stream, 0, sizeof(stream));
270 stream.next_out = buf;
271 stream.avail_out = size;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400272 stream.next_in = fill(1);
273 stream.avail_in = input_len;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700274 inflateInit(&stream);
275
276 for (;;) {
277 int ret = inflate(&stream, 0);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400278 use(input_len - stream.avail_in);
279 if (stream.total_out == size && ret == Z_STREAM_END)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700280 break;
281 if (ret != Z_OK)
282 bad_object(offset, "inflate returned %d", ret);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400283 stream.next_in = fill(1);
284 stream.avail_in = input_len;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700285 }
286 inflateEnd(&stream);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700287 return buf;
288}
289
Nicolas Pitre2d477052006-10-20 14:45:21 -0400290static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700291{
Nicolas Pitre2d477052006-10-20 14:45:21 -0400292 unsigned char *p, c;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400293 unsigned long size;
294 off_t base_offset;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700295 unsigned shift;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400296 void *data;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700297
Geert Boschaa7e44b2007-06-01 15:18:05 -0400298 obj->idx.offset = consumed_bytes;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400299 input_crc32 = crc32(0, Z_NULL, 0);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400300
301 p = fill(1);
302 c = *p;
303 use(1);
304 obj->type = (c >> 4) & 7;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700305 size = (c & 15);
306 shift = 4;
307 while (c & 0x80) {
Nicolas Pitre2d477052006-10-20 14:45:21 -0400308 p = fill(1);
309 c = *p;
310 use(1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700311 size += (c & 0x7fUL) << shift;
312 shift += 7;
313 }
Nicolas Pitre2d477052006-10-20 14:45:21 -0400314 obj->size = size;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700315
Nicolas Pitre2d477052006-10-20 14:45:21 -0400316 switch (obj->type) {
Nicolas Pitreeb32d232006-09-21 00:06:49 -0400317 case OBJ_REF_DELTA:
Nicolas Pitre2d477052006-10-20 14:45:21 -0400318 hashcpy(delta_base->sha1, fill(20));
319 use(20);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400320 break;
321 case OBJ_OFS_DELTA:
322 memset(delta_base, 0, sizeof(*delta_base));
Nicolas Pitre2d477052006-10-20 14:45:21 -0400323 p = fill(1);
324 c = *p;
325 use(1);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400326 base_offset = c & 127;
327 while (c & 128) {
328 base_offset += 1;
Nicolas Pitre8723f212007-04-09 01:06:29 -0400329 if (!base_offset || MSB(base_offset, 7))
Geert Boschaa7e44b2007-06-01 15:18:05 -0400330 bad_object(obj->idx.offset, "offset value overflow for delta base object");
Nicolas Pitre2d477052006-10-20 14:45:21 -0400331 p = fill(1);
332 c = *p;
333 use(1);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400334 base_offset = (base_offset << 7) + (c & 127);
335 }
Geert Boschaa7e44b2007-06-01 15:18:05 -0400336 delta_base->offset = obj->idx.offset - base_offset;
337 if (delta_base->offset >= obj->idx.offset)
338 bad_object(obj->idx.offset, "delta base offset is out of bound");
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400339 break;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700340 case OBJ_COMMIT:
341 case OBJ_TREE:
342 case OBJ_BLOB:
343 case OBJ_TAG:
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700344 break;
345 default:
Geert Boschaa7e44b2007-06-01 15:18:05 -0400346 bad_object(obj->idx.offset, "unknown object type %d", obj->type);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700347 }
Geert Boschaa7e44b2007-06-01 15:18:05 -0400348 obj->hdr_size = consumed_bytes - obj->idx.offset;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700349
Geert Boschaa7e44b2007-06-01 15:18:05 -0400350 data = unpack_entry_data(obj->idx.offset, obj->size);
351 obj->idx.crc32 = input_crc32;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400352 return data;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400353}
354
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400355static void *get_data_from_pack(struct object_entry *obj)
Nicolas Pitre2d477052006-10-20 14:45:21 -0400356{
Nicolas Pitrea91ef6e2007-11-10 23:29:10 -0500357 off_t from = obj[0].idx.offset + obj[0].hdr_size;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400358 unsigned long len = obj[1].idx.offset - from;
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500359 unsigned long rdy = 0;
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500360 unsigned char *src, *data;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400361 z_stream stream;
362 int st;
363
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500364 src = xmalloc(len);
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500365 data = src;
366 do {
367 ssize_t n = pread(pack_fd, data + rdy, len - rdy, from + rdy);
Samuel Tardieufb742432008-10-06 19:28:41 +0200368 if (n < 0)
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500369 die("cannot pread pack file: %s", strerror(errno));
Samuel Tardieufb742432008-10-06 19:28:41 +0200370 if (!n)
371 die("premature end of pack file, %lu bytes missing",
372 len - rdy);
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500373 rdy += n;
374 } while (rdy < len);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400375 data = xmalloc(obj->size);
376 memset(&stream, 0, sizeof(stream));
377 stream.next_out = data;
378 stream.avail_out = obj->size;
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500379 stream.next_in = src;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400380 stream.avail_in = len;
381 inflateInit(&stream);
382 while ((st = inflate(&stream, Z_FINISH)) == Z_OK);
383 inflateEnd(&stream);
384 if (st != Z_STREAM_END || stream.total_out != obj->size)
385 die("serious inflate inconsistency");
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500386 free(src);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700387 return data;
388}
389
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400390static int find_delta(const union delta_base *base)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700391{
392 int first = 0, last = nr_deltas;
393
394 while (first < last) {
395 int next = (first + last) / 2;
396 struct delta_entry *delta = &deltas[next];
397 int cmp;
398
Nicolas Pitre3c552872006-10-17 16:23:26 -0400399 cmp = memcmp(base, &delta->base, UNION_BASE_SZ);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700400 if (!cmp)
401 return next;
402 if (cmp < 0) {
403 last = next;
404 continue;
405 }
406 first = next+1;
407 }
408 return -first-1;
409}
410
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400411static int find_delta_children(const union delta_base *base,
412 int *first_index, int *last_index)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700413{
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400414 int first = find_delta(base);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700415 int last = first;
416 int end = nr_deltas - 1;
417
418 if (first < 0)
419 return -1;
Nicolas Pitre3c552872006-10-17 16:23:26 -0400420 while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700421 --first;
Nicolas Pitre3c552872006-10-17 16:23:26 -0400422 while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700423 ++last;
424 *first_index = first;
425 *last_index = last;
426 return 0;
427}
428
429static void sha1_object(const void *data, unsigned long size,
Nicolas Pitre9096c662007-03-20 17:07:48 -0400430 enum object_type type, unsigned char *sha1)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700431{
Nicolas Pitrece9fbf12007-03-20 16:02:09 -0400432 hash_sha1_file(data, size, typename(type), sha1);
Nicolas Pitre9096c662007-03-20 17:07:48 -0400433 if (has_sha1_file(sha1)) {
Nicolas Pitre8685da42007-03-20 15:32:35 -0400434 void *has_data;
435 enum object_type has_type;
436 unsigned long has_size;
437 has_data = read_sha1_file(sha1, &has_type, &has_size);
438 if (!has_data)
439 die("cannot read existing object %s", sha1_to_hex(sha1));
440 if (size != has_size || type != has_type ||
441 memcmp(data, has_data, size) != 0)
442 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1));
Nicolas Pitrebbf4b412007-04-03 12:33:46 -0400443 free(has_data);
Nicolas Pitre8685da42007-03-20 15:32:35 -0400444 }
Martin Koegler0153be02008-02-25 22:46:12 +0100445 if (strict) {
446 if (type == OBJ_BLOB) {
447 struct blob *blob = lookup_blob(sha1);
448 if (blob)
449 blob->object.flags |= FLAG_CHECKED;
450 else
451 die("invalid blob object %s", sha1_to_hex(sha1));
452 } else {
453 struct object *obj;
454 int eaten;
455 void *buf = (void *) data;
456
457 /*
458 * we do not need to free the memory here, as the
459 * buf is deleted by the caller.
460 */
461 obj = parse_object_buffer(sha1, type, size, buf, &eaten);
462 if (!obj)
463 die("invalid %s", typename(type));
464 if (fsck_object(obj, 1, fsck_error_function))
465 die("Error in object");
466 if (fsck_walk(obj, mark_link, 0))
467 die("Not all child objects of %s are reachable", sha1_to_hex(obj->sha1));
468
469 if (obj->type == OBJ_TREE) {
470 struct tree *item = (struct tree *) obj;
471 item->buffer = NULL;
472 }
473 if (obj->type == OBJ_COMMIT) {
474 struct commit *commit = (struct commit *) obj;
475 commit->buffer = NULL;
476 }
477 obj->flags |= FLAG_CHECKED;
478 }
479 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700480}
481
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000482static void *get_base_data(struct base_data *c)
483{
484 if (!c->data) {
485 struct object_entry *obj = c->obj;
486
487 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
488 void *base = get_base_data(c->base);
489 void *raw = get_data_from_pack(obj);
490 c->data = patch_delta(
491 base, c->base->size,
492 raw, obj->size,
493 &c->size);
494 free(raw);
495 if (!c->data)
496 bad_object(obj->idx.offset, "failed to apply delta");
497 } else
498 c->data = get_data_from_pack(obj);
499
500 base_cache_used += c->size;
501 prune_base_data(c);
502 }
503 return c->data;
504}
505
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400506static void resolve_delta(struct object_entry *delta_obj,
507 struct base_data *base_obj, enum object_type type)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700508{
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700509 void *delta_data;
510 unsigned long delta_size;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400511 union delta_base delta_base;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700512 int j, first, last;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400513 struct base_data result;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700514
Nicolas Pitre636171c2006-10-25 23:28:17 -0400515 delta_obj->real_type = type;
516 delta_data = get_data_from_pack(delta_obj);
517 delta_size = delta_obj->size;
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000518 result.data = patch_delta(get_base_data(base_obj), base_obj->size,
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400519 delta_data, delta_size,
520 &result.size);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700521 free(delta_data);
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400522 if (!result.data)
Geert Boschaa7e44b2007-06-01 15:18:05 -0400523 bad_object(delta_obj->idx.offset, "failed to apply delta");
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400524 sha1_object(result.data, result.size, type, delta_obj->idx.sha1);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400525 nr_resolved_deltas++;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400526
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400527 result.obj = delta_obj;
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400528 link_base_data(base_obj, &result);
529
Geert Boschaa7e44b2007-06-01 15:18:05 -0400530 hashcpy(delta_base.sha1, delta_obj->idx.sha1);
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400531 if (!find_delta_children(&delta_base, &first, &last)) {
Nicolas Pitre636171c2006-10-25 23:28:17 -0400532 for (j = first; j <= last; j++) {
533 struct object_entry *child = objects + deltas[j].obj_no;
534 if (child->real_type == OBJ_REF_DELTA)
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400535 resolve_delta(child, &result, type);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400536 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700537 }
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400538
539 memset(&delta_base, 0, sizeof(delta_base));
Geert Boschaa7e44b2007-06-01 15:18:05 -0400540 delta_base.offset = delta_obj->idx.offset;
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400541 if (!find_delta_children(&delta_base, &first, &last)) {
Nicolas Pitre636171c2006-10-25 23:28:17 -0400542 for (j = first; j <= last; j++) {
543 struct object_entry *child = objects + deltas[j].obj_no;
544 if (child->real_type == OBJ_OFS_DELTA)
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400545 resolve_delta(child, &result, type);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400546 }
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400547 }
548
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400549 unlink_base_data(&result);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700550}
551
552static int compare_delta_entry(const void *a, const void *b)
553{
554 const struct delta_entry *delta_a = a;
555 const struct delta_entry *delta_b = b;
Nicolas Pitre3c552872006-10-17 16:23:26 -0400556 return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700557}
558
Nicolas Pitre2d477052006-10-20 14:45:21 -0400559/* Parse all objects and return the pack content SHA1 hash */
560static void parse_pack_objects(unsigned char *sha1)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700561{
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400562 int i;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400563 struct delta_entry *delta = deltas;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400564 struct stat st;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700565
566 /*
567 * First pass:
568 * - find locations of all objects;
569 * - calculate SHA1 of all non-delta objects;
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400570 * - remember base (SHA1 or offset) for all deltas.
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700571 */
Nicolas Pitre13aaf142007-04-20 14:10:07 -0400572 if (verbose)
Nicolas Pitre29e63ed2007-10-30 14:57:35 -0400573 progress = start_progress(
574 from_stdin ? "Receiving objects" : "Indexing objects",
575 nr_objects);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700576 for (i = 0; i < nr_objects; i++) {
577 struct object_entry *obj = &objects[i];
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400578 void *data = unpack_raw_entry(obj, &delta->base);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700579 obj->real_type = obj->type;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400580 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
581 nr_deltas++;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400582 delta->obj_no = i;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400583 delta++;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700584 } else
Geert Boschaa7e44b2007-06-01 15:18:05 -0400585 sha1_object(data, obj->size, obj->type, obj->idx.sha1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700586 free(data);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400587 display_progress(progress, i+1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700588 }
Geert Boschaa7e44b2007-06-01 15:18:05 -0400589 objects[i].idx.offset = consumed_bytes;
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400590 stop_progress(&progress);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400591
592 /* Check pack integrity */
Nicolas Pitre636171c2006-10-25 23:28:17 -0400593 flush();
Nicolas Pitre2d477052006-10-20 14:45:21 -0400594 SHA1_Final(sha1, &input_ctx);
595 if (hashcmp(fill(20), sha1))
Nicolas Pitree42797f2006-10-23 14:50:18 -0400596 die("pack is corrupted (SHA1 mismatch)");
Nicolas Pitre9bee2472006-10-25 23:31:53 -0400597 use(20);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400598
599 /* If input_fd is a file, we should have reached its end now. */
600 if (fstat(input_fd, &st))
Nicolas Pitree42797f2006-10-23 14:50:18 -0400601 die("cannot fstat packfile: %s", strerror(errno));
Johannes Schindelinfa257b02007-02-22 19:14:14 +0100602 if (S_ISREG(st.st_mode) &&
603 lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
Nicolas Pitree42797f2006-10-23 14:50:18 -0400604 die("pack has junk at the end");
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700605
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400606 if (!nr_deltas)
607 return;
608
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400609 /* Sort deltas by base SHA1/offset for fast searching */
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700610 qsort(deltas, nr_deltas, sizeof(struct delta_entry),
611 compare_delta_entry);
612
613 /*
614 * Second pass:
615 * - for all non-delta objects, look if it is used as a base for
616 * deltas;
617 * - if used as a base, uncompress the object and apply all deltas,
618 * recursively checking if the resulting object is used as a base
619 * for some more deltas.
620 */
Nicolas Pitre13aaf142007-04-20 14:10:07 -0400621 if (verbose)
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400622 progress = start_progress("Resolving deltas", nr_deltas);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700623 for (i = 0; i < nr_objects; i++) {
624 struct object_entry *obj = &objects[i];
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400625 union delta_base base;
626 int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400627 struct base_data base_obj;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700628
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400629 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700630 continue;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400631 hashcpy(base.sha1, obj->idx.sha1);
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400632 ref = !find_delta_children(&base, &ref_first, &ref_last);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400633 memset(&base, 0, sizeof(base));
Geert Boschaa7e44b2007-06-01 15:18:05 -0400634 base.offset = obj->idx.offset;
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400635 ofs = !find_delta_children(&base, &ofs_first, &ofs_last);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400636 if (!ref && !ofs)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700637 continue;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400638 base_obj.data = get_data_from_pack(obj);
639 base_obj.size = obj->size;
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400640 base_obj.obj = obj;
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400641 link_base_data(NULL, &base_obj);
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400642
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400643 if (ref)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400644 for (j = ref_first; j <= ref_last; j++) {
645 struct object_entry *child = objects + deltas[j].obj_no;
646 if (child->real_type == OBJ_REF_DELTA)
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400647 resolve_delta(child, &base_obj, obj->type);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400648 }
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400649 if (ofs)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400650 for (j = ofs_first; j <= ofs_last; j++) {
651 struct object_entry *child = objects + deltas[j].obj_no;
652 if (child->real_type == OBJ_OFS_DELTA)
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400653 resolve_delta(child, &base_obj, obj->type);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400654 }
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400655 unlink_base_data(&base_obj);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400656 display_progress(progress, nr_resolved_deltas);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700657 }
Nicolas Pitre636171c2006-10-25 23:28:17 -0400658}
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700659
Nicolas Pitre85221482008-08-29 16:08:01 -0400660static int write_compressed(struct sha1file *f, void *in, unsigned int size)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400661{
662 z_stream stream;
663 unsigned long maxsize;
664 void *out;
665
666 memset(&stream, 0, sizeof(stream));
667 deflateInit(&stream, zlib_compression_level);
668 maxsize = deflateBound(&stream, size);
669 out = xmalloc(maxsize);
670
671 /* Compress it */
672 stream.next_in = in;
673 stream.avail_in = size;
674 stream.next_out = out;
675 stream.avail_out = maxsize;
676 while (deflate(&stream, Z_FINISH) == Z_OK);
677 deflateEnd(&stream);
678
679 size = stream.total_out;
Nicolas Pitre85221482008-08-29 16:08:01 -0400680 sha1write(f, out, size);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400681 free(out);
682 return size;
683}
684
Nicolas Pitre85221482008-08-29 16:08:01 -0400685static struct object_entry *append_obj_to_pack(struct sha1file *f,
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400686 const unsigned char *sha1, void *buf,
Nicolas Pitre636171c2006-10-25 23:28:17 -0400687 unsigned long size, enum object_type type)
688{
689 struct object_entry *obj = &objects[nr_objects++];
690 unsigned char header[10];
691 unsigned long s = size;
692 int n = 0;
693 unsigned char c = (type << 4) | (s & 15);
694 s >>= 4;
695 while (s) {
696 header[n++] = c | 0x80;
697 c = s & 0x7f;
698 s >>= 7;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700699 }
Nicolas Pitre636171c2006-10-25 23:28:17 -0400700 header[n++] = c;
Nicolas Pitre85221482008-08-29 16:08:01 -0400701 crc32_begin(f);
702 sha1write(f, header, n);
Björn Steinbrink72de2882008-07-24 18:32:00 +0100703 obj[0].size = size;
704 obj[0].hdr_size = n;
705 obj[0].type = type;
706 obj[0].real_type = type;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400707 obj[1].idx.offset = obj[0].idx.offset + n;
Nicolas Pitre85221482008-08-29 16:08:01 -0400708 obj[1].idx.offset += write_compressed(f, buf, size);
709 obj[0].idx.crc32 = crc32_end(f);
Nicolas Pitre838cd342008-10-09 22:08:51 -0400710 sha1flush(f);
Geert Boschaa7e44b2007-06-01 15:18:05 -0400711 hashcpy(obj->idx.sha1, sha1);
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400712 return obj;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400713}
714
715static int delta_pos_compare(const void *_a, const void *_b)
716{
717 struct delta_entry *a = *(struct delta_entry **)_a;
718 struct delta_entry *b = *(struct delta_entry **)_b;
719 return a->obj_no - b->obj_no;
720}
721
Nicolas Pitre85221482008-08-29 16:08:01 -0400722static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400723{
724 struct delta_entry **sorted_by_pos;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400725 int i, n = 0;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400726
727 /*
728 * Since many unresolved deltas may well be themselves base objects
729 * for more unresolved deltas, we really want to include the
730 * smallest number of base objects that would cover as much delta
731 * as possible by picking the
732 * trunc deltas first, allowing for other deltas to resolve without
733 * additional base objects. Since most base objects are to be found
734 * before deltas depending on them, a good heuristic is to start
735 * resolving deltas in the same order as their position in the pack.
736 */
737 sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
738 for (i = 0; i < nr_deltas; i++) {
739 if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
740 continue;
741 sorted_by_pos[n++] = &deltas[i];
742 }
743 qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
744
745 for (i = 0; i < n; i++) {
746 struct delta_entry *d = sorted_by_pos[i];
Nicolas Pitre21666f12007-02-26 14:55:59 -0500747 enum object_type type;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400748 int j, first, last;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400749 struct base_data base_obj;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400750
751 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
752 continue;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400753 base_obj.data = read_sha1_file(d->base.sha1, &type, &base_obj.size);
754 if (!base_obj.data)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400755 continue;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400756
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400757 if (check_sha1_signature(d->base.sha1, base_obj.data,
758 base_obj.size, typename(type)))
759 die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
Nicolas Pitre85221482008-08-29 16:08:01 -0400760 base_obj.obj = append_obj_to_pack(f, d->base.sha1,
761 base_obj.data, base_obj.size, type);
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400762 link_base_data(NULL, &base_obj);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400763
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400764 find_delta_children(&d->base, &first, &last);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400765 for (j = first; j <= last; j++) {
766 struct object_entry *child = objects + deltas[j].obj_no;
767 if (child->real_type == OBJ_REF_DELTA)
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400768 resolve_delta(child, &base_obj, type);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400769 }
770
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400771 unlink_base_data(&base_obj);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400772 display_progress(progress, nr_resolved_deltas);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400773 }
774 free(sorted_by_pos);
775}
776
Nicolas Pitree42797f2006-10-23 14:50:18 -0400777static void final(const char *final_pack_name, const char *curr_pack_name,
778 const char *final_index_name, const char *curr_index_name,
Shawn Pearceb8077702006-10-29 04:41:59 -0500779 const char *keep_name, const char *keep_msg,
Nicolas Pitree42797f2006-10-23 14:50:18 -0400780 unsigned char *sha1)
781{
Shawn O. Pearce3a556022007-03-06 20:44:17 -0500782 const char *report = "pack";
Nicolas Pitree42797f2006-10-23 14:50:18 -0400783 char name[PATH_MAX];
784 int err;
785
786 if (!from_stdin) {
787 close(input_fd);
788 } else {
Linus Torvalds4c81b032008-05-30 08:42:16 -0700789 fsync_or_die(output_fd, curr_pack_name);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400790 err = close(output_fd);
791 if (err)
792 die("error while closing pack file: %s", strerror(errno));
793 chmod(curr_pack_name, 0444);
794 }
795
Shawn Pearceb8077702006-10-29 04:41:59 -0500796 if (keep_msg) {
797 int keep_fd, keep_msg_len = strlen(keep_msg);
798 if (!keep_name) {
799 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
800 get_object_directory(), sha1_to_hex(sha1));
801 keep_name = name;
802 }
Nicolas Pitre9ca4a202006-11-01 17:06:24 -0500803 keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
804 if (keep_fd < 0) {
805 if (errno != EEXIST)
806 die("cannot write keep file");
807 } else {
808 if (keep_msg_len > 0) {
809 write_or_die(keep_fd, keep_msg, keep_msg_len);
810 write_or_die(keep_fd, "\n", 1);
811 }
Jim Meyering91c8d592007-06-24 21:20:41 +0200812 if (close(keep_fd) != 0)
813 die("cannot write keep file");
Nicolas Pitre576162a2006-11-01 17:06:25 -0500814 report = "keep";
Shawn Pearceb8077702006-10-29 04:41:59 -0500815 }
Shawn Pearceb8077702006-10-29 04:41:59 -0500816 }
817
Nicolas Pitree42797f2006-10-23 14:50:18 -0400818 if (final_pack_name != curr_pack_name) {
819 if (!final_pack_name) {
820 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
821 get_object_directory(), sha1_to_hex(sha1));
822 final_pack_name = name;
823 }
824 if (move_temp_to_file(curr_pack_name, final_pack_name))
825 die("cannot store pack file");
826 }
827
828 chmod(curr_index_name, 0444);
829 if (final_index_name != curr_index_name) {
830 if (!final_index_name) {
831 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
832 get_object_directory(), sha1_to_hex(sha1));
833 final_index_name = name;
834 }
835 if (move_temp_to_file(curr_index_name, final_index_name))
836 die("cannot store index file");
837 }
Nicolas Pitre576162a2006-11-01 17:06:25 -0500838
839 if (!from_stdin) {
840 printf("%s\n", sha1_to_hex(sha1));
841 } else {
842 char buf[48];
843 int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
844 report, sha1_to_hex(sha1));
Junio C Hamanod1b2ddc2007-01-11 13:15:51 -0800845 write_or_die(1, buf, len);
Nicolas Pitre576162a2006-11-01 17:06:25 -0500846
847 /*
848 * Let's just mimic git-unpack-objects here and write
849 * the last part of the input buffer to stdout.
850 */
851 while (input_len) {
852 err = xwrite(1, input_buffer + input_offset, input_len);
853 if (err <= 0)
854 break;
855 input_len -= err;
856 input_offset += err;
857 }
858 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700859}
860
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100861static int git_index_pack_config(const char *k, const char *v, void *cb)
Nicolas Pitre4d00bda2007-11-01 23:26:04 -0400862{
863 if (!strcmp(k, "pack.indexversion")) {
864 pack_idx_default_version = git_config_int(k, v);
865 if (pack_idx_default_version > 2)
Ramsay Jones6e1c2342008-07-03 16:52:09 +0100866 die("bad pack.indexversion=%"PRIu32,
867 pack_idx_default_version);
Nicolas Pitre4d00bda2007-11-01 23:26:04 -0400868 return 0;
869 }
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100870 return git_default_config(k, v, cb);
Nicolas Pitre4d00bda2007-11-01 23:26:04 -0400871}
872
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700873int main(int argc, char **argv)
874{
Nicolas Pitre636171c2006-10-25 23:28:17 -0400875 int i, fix_thin_pack = 0;
Nicolas Pitre4049b9c2007-10-16 21:55:49 -0400876 char *curr_pack, *pack_name = NULL;
877 char *curr_index, *index_name = NULL;
Shawn Pearceb8077702006-10-29 04:41:59 -0500878 const char *keep_name = NULL, *keep_msg = NULL;
879 char *index_name_buf = NULL, *keep_name_buf = NULL;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400880 struct pack_idx_entry **idx_objects;
Nicolas Pitre85221482008-08-29 16:08:01 -0400881 unsigned char pack_sha1[20];
Nguyễn Thái Ngọc Duyd0b92a32008-08-26 21:32:42 +0700882 int nongit = 0;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700883
Nguyễn Thái Ngọc Duyd0b92a32008-08-26 21:32:42 +0700884 setup_git_directory_gently(&nongit);
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100885 git_config(git_index_pack_config, NULL);
Nicolas Pitre4d00bda2007-11-01 23:26:04 -0400886
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700887 for (i = 1; i < argc; i++) {
Nicolas Pitre4049b9c2007-10-16 21:55:49 -0400888 char *arg = argv[i];
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700889
890 if (*arg == '-') {
Nicolas Pitree42797f2006-10-23 14:50:18 -0400891 if (!strcmp(arg, "--stdin")) {
892 from_stdin = 1;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400893 } else if (!strcmp(arg, "--fix-thin")) {
894 fix_thin_pack = 1;
Martin Koegler0153be02008-02-25 22:46:12 +0100895 } else if (!strcmp(arg, "--strict")) {
896 strict = 1;
Shawn Pearceb8077702006-10-29 04:41:59 -0500897 } else if (!strcmp(arg, "--keep")) {
898 keep_msg = "";
Junio C Hamanocc44c762007-02-20 01:53:29 -0800899 } else if (!prefixcmp(arg, "--keep=")) {
Shawn Pearceb8077702006-10-29 04:41:59 -0500900 keep_msg = arg + 7;
Junio C Hamanocc44c762007-02-20 01:53:29 -0800901 } else if (!prefixcmp(arg, "--pack_header=")) {
Nicolas Pitrebed006f2006-11-01 17:06:20 -0500902 struct pack_header *hdr;
903 char *c;
904
905 hdr = (struct pack_header *)input_buffer;
906 hdr->hdr_signature = htonl(PACK_SIGNATURE);
907 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
908 if (*c != ',')
909 die("bad %s", arg);
910 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
911 if (*c)
912 die("bad %s", arg);
913 input_len = sizeof(*hdr);
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400914 } else if (!strcmp(arg, "-v")) {
915 verbose = 1;
Nicolas Pitree42797f2006-10-23 14:50:18 -0400916 } else if (!strcmp(arg, "-o")) {
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700917 if (index_name || (i+1) >= argc)
918 usage(index_pack_usage);
919 index_name = argv[++i];
Nicolas Pitre4ba7d712007-04-09 17:32:03 -0400920 } else if (!prefixcmp(arg, "--index-version=")) {
921 char *c;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400922 pack_idx_default_version = strtoul(arg + 16, &c, 10);
923 if (pack_idx_default_version > 2)
Nicolas Pitre4ba7d712007-04-09 17:32:03 -0400924 die("bad %s", arg);
925 if (*c == ',')
Geert Boschaa7e44b2007-06-01 15:18:05 -0400926 pack_idx_off32_limit = strtoul(c+1, &c, 0);
927 if (*c || pack_idx_off32_limit & 0x80000000)
Nicolas Pitre4ba7d712007-04-09 17:32:03 -0400928 die("bad %s", arg);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700929 } else
930 usage(index_pack_usage);
931 continue;
932 }
933
934 if (pack_name)
935 usage(index_pack_usage);
936 pack_name = arg;
937 }
938
Nicolas Pitree42797f2006-10-23 14:50:18 -0400939 if (!pack_name && !from_stdin)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700940 usage(index_pack_usage);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400941 if (fix_thin_pack && !from_stdin)
942 die("--fix-thin cannot be used without --stdin");
Nicolas Pitree42797f2006-10-23 14:50:18 -0400943 if (!index_name && pack_name) {
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700944 int len = strlen(pack_name);
Rene Scharfe5bb1cda2006-08-11 14:01:45 +0200945 if (!has_extension(pack_name, ".pack"))
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700946 die("packfile name '%s' does not end with '.pack'",
947 pack_name);
Pavel Roskin6689f082005-12-21 15:35:48 -0500948 index_name_buf = xmalloc(len);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700949 memcpy(index_name_buf, pack_name, len - 5);
950 strcpy(index_name_buf + len - 5, ".idx");
951 index_name = index_name_buf;
952 }
Shawn Pearceb8077702006-10-29 04:41:59 -0500953 if (keep_msg && !keep_name && pack_name) {
954 int len = strlen(pack_name);
955 if (!has_extension(pack_name, ".pack"))
956 die("packfile name '%s' does not end with '.pack'",
957 pack_name);
958 keep_name_buf = xmalloc(len);
959 memcpy(keep_name_buf, pack_name, len - 5);
960 strcpy(keep_name_buf + len - 5, ".keep");
961 keep_name = keep_name_buf;
962 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700963
Nicolas Pitree42797f2006-10-23 14:50:18 -0400964 curr_pack = open_pack_file(pack_name);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700965 parse_pack_header();
Nicolas Pitre636171c2006-10-25 23:28:17 -0400966 objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
967 deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
Nicolas Pitre85221482008-08-29 16:08:01 -0400968 parse_pack_objects(pack_sha1);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400969 if (nr_deltas == nr_resolved_deltas) {
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400970 stop_progress(&progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400971 /* Flush remaining pack final 20-byte SHA1. */
972 flush();
973 } else {
Nicolas Pitre636171c2006-10-25 23:28:17 -0400974 if (fix_thin_pack) {
Nicolas Pitre85221482008-08-29 16:08:01 -0400975 struct sha1file *f;
976 unsigned char read_sha1[20], tail_sha1[20];
Nicolas Pitrea984a062007-11-08 15:45:41 -0500977 char msg[48];
Nicolas Pitre636171c2006-10-25 23:28:17 -0400978 int nr_unresolved = nr_deltas - nr_resolved_deltas;
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400979 int nr_objects_initial = nr_objects;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400980 if (nr_unresolved <= 0)
981 die("confusion beyond insanity");
982 objects = xrealloc(objects,
983 (nr_objects + nr_unresolved + 1)
984 * sizeof(*objects));
Nicolas Pitre85221482008-08-29 16:08:01 -0400985 f = sha1fd(output_fd, curr_pack);
986 fix_unresolved_deltas(f, nr_unresolved);
Nicolas Pitrea984a062007-11-08 15:45:41 -0500987 sprintf(msg, "completed with %d local objects",
988 nr_objects - nr_objects_initial);
989 stop_progress_msg(&progress, msg);
Nicolas Pitre85221482008-08-29 16:08:01 -0400990 sha1close(f, tail_sha1, 0);
991 hashcpy(read_sha1, pack_sha1);
992 fixup_pack_header_footer(output_fd, pack_sha1,
Nicolas Pitreabeb40e2008-08-29 16:07:59 -0400993 curr_pack, nr_objects,
Nicolas Pitre85221482008-08-29 16:08:01 -0400994 read_sha1, consumed_bytes-20);
995 if (hashcmp(read_sha1, tail_sha1) != 0)
996 die("Unexpected tail checksum for %s "
997 "(disk corruption?)", curr_pack);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400998 }
999 if (nr_deltas != nr_resolved_deltas)
1000 die("pack has %d unresolved deltas",
1001 nr_deltas - nr_resolved_deltas);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001002 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001003 free(deltas);
Martin Koegler0153be02008-02-25 22:46:12 +01001004 if (strict)
1005 check_objects();
Geert Boschaa7e44b2007-06-01 15:18:05 -04001006
1007 idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
1008 for (i = 0; i < nr_objects; i++)
1009 idx_objects[i] = &objects[i].idx;
Nicolas Pitre85221482008-08-29 16:08:01 -04001010 curr_index = write_idx_file(index_name, idx_objects, nr_objects, pack_sha1);
Geert Boschaa7e44b2007-06-01 15:18:05 -04001011 free(idx_objects);
1012
Shawn Pearceb8077702006-10-29 04:41:59 -05001013 final(pack_name, curr_pack,
1014 index_name, curr_index,
1015 keep_name, keep_msg,
Nicolas Pitre85221482008-08-29 16:08:01 -04001016 pack_sha1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001017 free(objects);
1018 free(index_name_buf);
Shawn Pearceb8077702006-10-29 04:41:59 -05001019 free(keep_name_buf);
Nicolas Pitrec85228e2007-10-16 21:55:50 -04001020 if (pack_name == NULL)
1021 free(curr_pack);
1022 if (index_name == NULL)
1023 free(curr_index);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001024
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001025 return 0;
1026}