blob: 60ed41a993bf9e213b7dfde5ff43528eff6b6252 [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 Pitre9126f002008-10-01 14:05:20 -040070static git_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);
Nicolas Pitre9126f002008-10-01 14:05:20 -0400122 git_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 Pitre9126f002008-10-01 14:05:20 -0400191 git_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
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400224static void free_base_data(struct base_data *c)
225{
226 if (c->data) {
227 free(c->data);
228 c->data = NULL;
229 base_cache_used -= c->size;
230 }
231}
232
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000233static void prune_base_data(struct base_data *retain)
234{
235 struct base_data *b = base_cache;
236 for (b = base_cache;
237 base_cache_used > delta_base_cache_limit && b;
238 b = b->child) {
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400239 if (b->data && b != retain)
240 free_base_data(b);
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000241 }
242}
243
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400244static void link_base_data(struct base_data *base, struct base_data *c)
245{
246 if (base)
247 base->child = c;
248 else
249 base_cache = c;
250
251 c->base = base;
252 c->child = NULL;
Nicolas Pitre9441b612008-10-17 15:57:57 -0400253 if (c->data)
254 base_cache_used += c->size;
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000255 prune_base_data(c);
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400256}
257
258static void unlink_base_data(struct base_data *c)
259{
260 struct base_data *base = c->base;
261 if (base)
262 base->child = NULL;
263 else
264 base_cache = NULL;
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400265 free_base_data(c);
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400266}
267
Nicolas Pitre2d477052006-10-20 14:45:21 -0400268static void *unpack_entry_data(unsigned long offset, unsigned long size)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700269{
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700270 z_stream stream;
271 void *buf = xmalloc(size);
272
273 memset(&stream, 0, sizeof(stream));
274 stream.next_out = buf;
275 stream.avail_out = size;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400276 stream.next_in = fill(1);
277 stream.avail_in = input_len;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700278 inflateInit(&stream);
279
280 for (;;) {
281 int ret = inflate(&stream, 0);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400282 use(input_len - stream.avail_in);
283 if (stream.total_out == size && ret == Z_STREAM_END)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700284 break;
285 if (ret != Z_OK)
286 bad_object(offset, "inflate returned %d", ret);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400287 stream.next_in = fill(1);
288 stream.avail_in = input_len;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700289 }
290 inflateEnd(&stream);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700291 return buf;
292}
293
Nicolas Pitre2d477052006-10-20 14:45:21 -0400294static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700295{
Nicolas Pitre2d477052006-10-20 14:45:21 -0400296 unsigned char *p, c;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400297 unsigned long size;
298 off_t base_offset;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700299 unsigned shift;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400300 void *data;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700301
Geert Boschaa7e44b2007-06-01 15:18:05 -0400302 obj->idx.offset = consumed_bytes;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400303 input_crc32 = crc32(0, Z_NULL, 0);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400304
305 p = fill(1);
306 c = *p;
307 use(1);
308 obj->type = (c >> 4) & 7;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700309 size = (c & 15);
310 shift = 4;
311 while (c & 0x80) {
Nicolas Pitre2d477052006-10-20 14:45:21 -0400312 p = fill(1);
313 c = *p;
314 use(1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700315 size += (c & 0x7fUL) << shift;
316 shift += 7;
317 }
Nicolas Pitre2d477052006-10-20 14:45:21 -0400318 obj->size = size;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700319
Nicolas Pitre2d477052006-10-20 14:45:21 -0400320 switch (obj->type) {
Nicolas Pitreeb32d232006-09-21 00:06:49 -0400321 case OBJ_REF_DELTA:
Nicolas Pitre2d477052006-10-20 14:45:21 -0400322 hashcpy(delta_base->sha1, fill(20));
323 use(20);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400324 break;
325 case OBJ_OFS_DELTA:
326 memset(delta_base, 0, sizeof(*delta_base));
Nicolas Pitre2d477052006-10-20 14:45:21 -0400327 p = fill(1);
328 c = *p;
329 use(1);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400330 base_offset = c & 127;
331 while (c & 128) {
332 base_offset += 1;
Nicolas Pitre8723f212007-04-09 01:06:29 -0400333 if (!base_offset || MSB(base_offset, 7))
Geert Boschaa7e44b2007-06-01 15:18:05 -0400334 bad_object(obj->idx.offset, "offset value overflow for delta base object");
Nicolas Pitre2d477052006-10-20 14:45:21 -0400335 p = fill(1);
336 c = *p;
337 use(1);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400338 base_offset = (base_offset << 7) + (c & 127);
339 }
Geert Boschaa7e44b2007-06-01 15:18:05 -0400340 delta_base->offset = obj->idx.offset - base_offset;
Nicolas Pitred8f32552008-10-29 19:02:45 -0400341 if (delta_base->offset <= 0 || delta_base->offset >= obj->idx.offset)
Geert Boschaa7e44b2007-06-01 15:18:05 -0400342 bad_object(obj->idx.offset, "delta base offset is out of bound");
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400343 break;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700344 case OBJ_COMMIT:
345 case OBJ_TREE:
346 case OBJ_BLOB:
347 case OBJ_TAG:
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700348 break;
349 default:
Geert Boschaa7e44b2007-06-01 15:18:05 -0400350 bad_object(obj->idx.offset, "unknown object type %d", obj->type);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700351 }
Geert Boschaa7e44b2007-06-01 15:18:05 -0400352 obj->hdr_size = consumed_bytes - obj->idx.offset;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700353
Geert Boschaa7e44b2007-06-01 15:18:05 -0400354 data = unpack_entry_data(obj->idx.offset, obj->size);
355 obj->idx.crc32 = input_crc32;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400356 return data;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400357}
358
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400359static void *get_data_from_pack(struct object_entry *obj)
Nicolas Pitre2d477052006-10-20 14:45:21 -0400360{
Nicolas Pitrea91ef6e2007-11-10 23:29:10 -0500361 off_t from = obj[0].idx.offset + obj[0].hdr_size;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400362 unsigned long len = obj[1].idx.offset - from;
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500363 unsigned long rdy = 0;
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500364 unsigned char *src, *data;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400365 z_stream stream;
366 int st;
367
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500368 src = xmalloc(len);
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500369 data = src;
370 do {
371 ssize_t n = pread(pack_fd, data + rdy, len - rdy, from + rdy);
Samuel Tardieufb742432008-10-06 19:28:41 +0200372 if (n < 0)
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500373 die("cannot pread pack file: %s", strerror(errno));
Samuel Tardieufb742432008-10-06 19:28:41 +0200374 if (!n)
375 die("premature end of pack file, %lu bytes missing",
376 len - rdy);
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500377 rdy += n;
378 } while (rdy < len);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400379 data = xmalloc(obj->size);
380 memset(&stream, 0, sizeof(stream));
381 stream.next_out = data;
382 stream.avail_out = obj->size;
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500383 stream.next_in = src;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400384 stream.avail_in = len;
385 inflateInit(&stream);
386 while ((st = inflate(&stream, Z_FINISH)) == Z_OK);
387 inflateEnd(&stream);
388 if (st != Z_STREAM_END || stream.total_out != obj->size)
389 die("serious inflate inconsistency");
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500390 free(src);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700391 return data;
392}
393
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400394static int find_delta(const union delta_base *base)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700395{
396 int first = 0, last = nr_deltas;
397
398 while (first < last) {
399 int next = (first + last) / 2;
400 struct delta_entry *delta = &deltas[next];
401 int cmp;
402
Nicolas Pitre3c552872006-10-17 16:23:26 -0400403 cmp = memcmp(base, &delta->base, UNION_BASE_SZ);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700404 if (!cmp)
405 return next;
406 if (cmp < 0) {
407 last = next;
408 continue;
409 }
410 first = next+1;
411 }
412 return -first-1;
413}
414
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400415static void find_delta_children(const union delta_base *base,
416 int *first_index, int *last_index)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700417{
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400418 int first = find_delta(base);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700419 int last = first;
420 int end = nr_deltas - 1;
421
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400422 if (first < 0) {
423 *first_index = 0;
424 *last_index = -1;
425 return;
426 }
Nicolas Pitre3c552872006-10-17 16:23:26 -0400427 while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700428 --first;
Nicolas Pitre3c552872006-10-17 16:23:26 -0400429 while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700430 ++last;
431 *first_index = first;
432 *last_index = last;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700433}
434
435static void sha1_object(const void *data, unsigned long size,
Nicolas Pitre9096c662007-03-20 17:07:48 -0400436 enum object_type type, unsigned char *sha1)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700437{
Nicolas Pitrece9fbf12007-03-20 16:02:09 -0400438 hash_sha1_file(data, size, typename(type), sha1);
Nicolas Pitre9096c662007-03-20 17:07:48 -0400439 if (has_sha1_file(sha1)) {
Nicolas Pitre8685da42007-03-20 15:32:35 -0400440 void *has_data;
441 enum object_type has_type;
442 unsigned long has_size;
443 has_data = read_sha1_file(sha1, &has_type, &has_size);
444 if (!has_data)
445 die("cannot read existing object %s", sha1_to_hex(sha1));
446 if (size != has_size || type != has_type ||
447 memcmp(data, has_data, size) != 0)
448 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1));
Nicolas Pitrebbf4b412007-04-03 12:33:46 -0400449 free(has_data);
Nicolas Pitre8685da42007-03-20 15:32:35 -0400450 }
Martin Koegler0153be02008-02-25 22:46:12 +0100451 if (strict) {
452 if (type == OBJ_BLOB) {
453 struct blob *blob = lookup_blob(sha1);
454 if (blob)
455 blob->object.flags |= FLAG_CHECKED;
456 else
457 die("invalid blob object %s", sha1_to_hex(sha1));
458 } else {
459 struct object *obj;
460 int eaten;
461 void *buf = (void *) data;
462
463 /*
464 * we do not need to free the memory here, as the
465 * buf is deleted by the caller.
466 */
467 obj = parse_object_buffer(sha1, type, size, buf, &eaten);
468 if (!obj)
469 die("invalid %s", typename(type));
470 if (fsck_object(obj, 1, fsck_error_function))
471 die("Error in object");
472 if (fsck_walk(obj, mark_link, 0))
473 die("Not all child objects of %s are reachable", sha1_to_hex(obj->sha1));
474
475 if (obj->type == OBJ_TREE) {
476 struct tree *item = (struct tree *) obj;
477 item->buffer = NULL;
478 }
479 if (obj->type == OBJ_COMMIT) {
480 struct commit *commit = (struct commit *) obj;
481 commit->buffer = NULL;
482 }
483 obj->flags |= FLAG_CHECKED;
484 }
485 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700486}
487
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000488static void *get_base_data(struct base_data *c)
489{
490 if (!c->data) {
491 struct object_entry *obj = c->obj;
492
493 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
494 void *base = get_base_data(c->base);
495 void *raw = get_data_from_pack(obj);
496 c->data = patch_delta(
497 base, c->base->size,
498 raw, obj->size,
499 &c->size);
500 free(raw);
501 if (!c->data)
502 bad_object(obj->idx.offset, "failed to apply delta");
Nicolas Pitre9441b612008-10-17 15:57:57 -0400503 } else {
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000504 c->data = get_data_from_pack(obj);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400505 c->size = obj->size;
506 }
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000507
508 base_cache_used += c->size;
509 prune_base_data(c);
510 }
511 return c->data;
512}
513
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400514static void resolve_delta(struct object_entry *delta_obj,
Nicolas Pitre9441b612008-10-17 15:57:57 -0400515 struct base_data *base, struct base_data *result)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700516{
Nicolas Pitrece3f6dc2008-10-20 16:46:19 -0400517 void *base_data, *delta_data;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700518
Nicolas Pitrece3f6dc2008-10-20 16:46:19 -0400519 delta_obj->real_type = base->obj->real_type;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400520 delta_data = get_data_from_pack(delta_obj);
Nicolas Pitrece3f6dc2008-10-20 16:46:19 -0400521 base_data = get_base_data(base);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400522 result->obj = delta_obj;
Nicolas Pitrece3f6dc2008-10-20 16:46:19 -0400523 result->data = patch_delta(base_data, base->size,
524 delta_data, delta_obj->size, &result->size);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700525 free(delta_data);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400526 if (!result->data)
Geert Boschaa7e44b2007-06-01 15:18:05 -0400527 bad_object(delta_obj->idx.offset, "failed to apply delta");
Nicolas Pitre9441b612008-10-17 15:57:57 -0400528 sha1_object(result->data, result->size, delta_obj->real_type,
529 delta_obj->idx.sha1);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400530 nr_resolved_deltas++;
Nicolas Pitre9441b612008-10-17 15:57:57 -0400531}
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400532
Nicolas Pitre9441b612008-10-17 15:57:57 -0400533static void find_unresolved_deltas(struct base_data *base,
534 struct base_data *prev_base)
535{
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400536 int i, ref_first, ref_last, ofs_first, ofs_last;
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400537
Nicolas Pitre9441b612008-10-17 15:57:57 -0400538 /*
539 * This is a recursive function. Those brackets should help reducing
540 * stack usage by limiting the scope of the delta_base union.
541 */
542 {
543 union delta_base base_spec;
544
545 hashcpy(base_spec.sha1, base->obj->idx.sha1);
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400546 find_delta_children(&base_spec, &ref_first, &ref_last);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400547
548 memset(&base_spec, 0, sizeof(base_spec));
549 base_spec.offset = base->obj->idx.offset;
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400550 find_delta_children(&base_spec, &ofs_first, &ofs_last);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400551 }
552
Nicolas Pitre9ed19612008-10-23 15:05:59 -0400553 if (ref_last == -1 && ofs_last == -1) {
554 free(base->data);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400555 return;
Nicolas Pitre9ed19612008-10-23 15:05:59 -0400556 }
Nicolas Pitre9441b612008-10-17 15:57:57 -0400557
558 link_base_data(prev_base, base);
559
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400560 for (i = ref_first; i <= ref_last; i++) {
561 struct object_entry *child = objects + deltas[i].obj_no;
562 if (child->real_type == OBJ_REF_DELTA) {
563 struct base_data result;
564 resolve_delta(child, base, &result);
565 if (i == ref_last && ofs_last == -1)
566 free_base_data(base);
567 find_unresolved_deltas(&result, base);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400568 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700569 }
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400570
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400571 for (i = ofs_first; i <= ofs_last; i++) {
572 struct object_entry *child = objects + deltas[i].obj_no;
573 if (child->real_type == OBJ_OFS_DELTA) {
574 struct base_data result;
575 resolve_delta(child, base, &result);
576 if (i == ofs_last)
577 free_base_data(base);
578 find_unresolved_deltas(&result, base);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400579 }
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400580 }
581
Nicolas Pitre9441b612008-10-17 15:57:57 -0400582 unlink_base_data(base);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700583}
584
585static int compare_delta_entry(const void *a, const void *b)
586{
587 const struct delta_entry *delta_a = a;
588 const struct delta_entry *delta_b = b;
Nicolas Pitre3c552872006-10-17 16:23:26 -0400589 return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700590}
591
Nicolas Pitre2d477052006-10-20 14:45:21 -0400592/* Parse all objects and return the pack content SHA1 hash */
593static void parse_pack_objects(unsigned char *sha1)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700594{
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400595 int i;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400596 struct delta_entry *delta = deltas;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400597 struct stat st;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700598
599 /*
600 * First pass:
601 * - find locations of all objects;
602 * - calculate SHA1 of all non-delta objects;
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400603 * - remember base (SHA1 or offset) for all deltas.
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700604 */
Nicolas Pitre13aaf142007-04-20 14:10:07 -0400605 if (verbose)
Nicolas Pitre29e63ed2007-10-30 14:57:35 -0400606 progress = start_progress(
607 from_stdin ? "Receiving objects" : "Indexing objects",
608 nr_objects);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700609 for (i = 0; i < nr_objects; i++) {
610 struct object_entry *obj = &objects[i];
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400611 void *data = unpack_raw_entry(obj, &delta->base);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700612 obj->real_type = obj->type;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400613 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
614 nr_deltas++;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400615 delta->obj_no = i;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400616 delta++;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700617 } else
Geert Boschaa7e44b2007-06-01 15:18:05 -0400618 sha1_object(data, obj->size, obj->type, obj->idx.sha1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700619 free(data);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400620 display_progress(progress, i+1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700621 }
Geert Boschaa7e44b2007-06-01 15:18:05 -0400622 objects[i].idx.offset = consumed_bytes;
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400623 stop_progress(&progress);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400624
625 /* Check pack integrity */
Nicolas Pitre636171c2006-10-25 23:28:17 -0400626 flush();
Nicolas Pitre9126f002008-10-01 14:05:20 -0400627 git_SHA1_Final(sha1, &input_ctx);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400628 if (hashcmp(fill(20), sha1))
Nicolas Pitree42797f2006-10-23 14:50:18 -0400629 die("pack is corrupted (SHA1 mismatch)");
Nicolas Pitre9bee2472006-10-25 23:31:53 -0400630 use(20);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400631
632 /* If input_fd is a file, we should have reached its end now. */
633 if (fstat(input_fd, &st))
Nicolas Pitree42797f2006-10-23 14:50:18 -0400634 die("cannot fstat packfile: %s", strerror(errno));
Johannes Schindelinfa257b02007-02-22 19:14:14 +0100635 if (S_ISREG(st.st_mode) &&
636 lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
Nicolas Pitree42797f2006-10-23 14:50:18 -0400637 die("pack has junk at the end");
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700638
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400639 if (!nr_deltas)
640 return;
641
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400642 /* Sort deltas by base SHA1/offset for fast searching */
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700643 qsort(deltas, nr_deltas, sizeof(struct delta_entry),
644 compare_delta_entry);
645
646 /*
647 * Second pass:
648 * - for all non-delta objects, look if it is used as a base for
649 * deltas;
650 * - if used as a base, uncompress the object and apply all deltas,
651 * recursively checking if the resulting object is used as a base
652 * for some more deltas.
653 */
Nicolas Pitre13aaf142007-04-20 14:10:07 -0400654 if (verbose)
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400655 progress = start_progress("Resolving deltas", nr_deltas);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700656 for (i = 0; i < nr_objects; i++) {
657 struct object_entry *obj = &objects[i];
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400658 struct base_data base_obj;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700659
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400660 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700661 continue;
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400662 base_obj.obj = obj;
Nicolas Pitre9441b612008-10-17 15:57:57 -0400663 base_obj.data = NULL;
664 find_unresolved_deltas(&base_obj, NULL);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400665 display_progress(progress, nr_resolved_deltas);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700666 }
Nicolas Pitre636171c2006-10-25 23:28:17 -0400667}
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700668
Nicolas Pitre85221482008-08-29 16:08:01 -0400669static int write_compressed(struct sha1file *f, void *in, unsigned int size)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400670{
671 z_stream stream;
672 unsigned long maxsize;
673 void *out;
674
675 memset(&stream, 0, sizeof(stream));
676 deflateInit(&stream, zlib_compression_level);
677 maxsize = deflateBound(&stream, size);
678 out = xmalloc(maxsize);
679
680 /* Compress it */
681 stream.next_in = in;
682 stream.avail_in = size;
683 stream.next_out = out;
684 stream.avail_out = maxsize;
685 while (deflate(&stream, Z_FINISH) == Z_OK);
686 deflateEnd(&stream);
687
688 size = stream.total_out;
Nicolas Pitre85221482008-08-29 16:08:01 -0400689 sha1write(f, out, size);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400690 free(out);
691 return size;
692}
693
Nicolas Pitre85221482008-08-29 16:08:01 -0400694static struct object_entry *append_obj_to_pack(struct sha1file *f,
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400695 const unsigned char *sha1, void *buf,
Nicolas Pitre636171c2006-10-25 23:28:17 -0400696 unsigned long size, enum object_type type)
697{
698 struct object_entry *obj = &objects[nr_objects++];
699 unsigned char header[10];
700 unsigned long s = size;
701 int n = 0;
702 unsigned char c = (type << 4) | (s & 15);
703 s >>= 4;
704 while (s) {
705 header[n++] = c | 0x80;
706 c = s & 0x7f;
707 s >>= 7;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700708 }
Nicolas Pitre636171c2006-10-25 23:28:17 -0400709 header[n++] = c;
Nicolas Pitre85221482008-08-29 16:08:01 -0400710 crc32_begin(f);
711 sha1write(f, header, n);
Björn Steinbrink72de2882008-07-24 18:32:00 +0100712 obj[0].size = size;
713 obj[0].hdr_size = n;
714 obj[0].type = type;
715 obj[0].real_type = type;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400716 obj[1].idx.offset = obj[0].idx.offset + n;
Nicolas Pitre85221482008-08-29 16:08:01 -0400717 obj[1].idx.offset += write_compressed(f, buf, size);
718 obj[0].idx.crc32 = crc32_end(f);
Nicolas Pitre838cd342008-10-09 22:08:51 -0400719 sha1flush(f);
Geert Boschaa7e44b2007-06-01 15:18:05 -0400720 hashcpy(obj->idx.sha1, sha1);
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400721 return obj;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400722}
723
724static int delta_pos_compare(const void *_a, const void *_b)
725{
726 struct delta_entry *a = *(struct delta_entry **)_a;
727 struct delta_entry *b = *(struct delta_entry **)_b;
728 return a->obj_no - b->obj_no;
729}
730
Nicolas Pitre85221482008-08-29 16:08:01 -0400731static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400732{
733 struct delta_entry **sorted_by_pos;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400734 int i, n = 0;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400735
736 /*
737 * Since many unresolved deltas may well be themselves base objects
738 * for more unresolved deltas, we really want to include the
739 * smallest number of base objects that would cover as much delta
740 * as possible by picking the
741 * trunc deltas first, allowing for other deltas to resolve without
742 * additional base objects. Since most base objects are to be found
743 * before deltas depending on them, a good heuristic is to start
744 * resolving deltas in the same order as their position in the pack.
745 */
746 sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
747 for (i = 0; i < nr_deltas; i++) {
748 if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
749 continue;
750 sorted_by_pos[n++] = &deltas[i];
751 }
752 qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
753
754 for (i = 0; i < n; i++) {
755 struct delta_entry *d = sorted_by_pos[i];
Nicolas Pitre21666f12007-02-26 14:55:59 -0500756 enum object_type type;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400757 struct base_data base_obj;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400758
759 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
760 continue;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400761 base_obj.data = read_sha1_file(d->base.sha1, &type, &base_obj.size);
762 if (!base_obj.data)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400763 continue;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400764
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400765 if (check_sha1_signature(d->base.sha1, base_obj.data,
766 base_obj.size, typename(type)))
767 die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
Nicolas Pitre85221482008-08-29 16:08:01 -0400768 base_obj.obj = append_obj_to_pack(f, d->base.sha1,
769 base_obj.data, base_obj.size, type);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400770 find_unresolved_deltas(&base_obj, NULL);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400771 display_progress(progress, nr_resolved_deltas);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400772 }
773 free(sorted_by_pos);
774}
775
Nicolas Pitree42797f2006-10-23 14:50:18 -0400776static void final(const char *final_pack_name, const char *curr_pack_name,
777 const char *final_index_name, const char *curr_index_name,
Shawn Pearceb8077702006-10-29 04:41:59 -0500778 const char *keep_name, const char *keep_msg,
Nicolas Pitree42797f2006-10-23 14:50:18 -0400779 unsigned char *sha1)
780{
Shawn O. Pearce3a556022007-03-06 20:44:17 -0500781 const char *report = "pack";
Nicolas Pitree42797f2006-10-23 14:50:18 -0400782 char name[PATH_MAX];
783 int err;
784
785 if (!from_stdin) {
786 close(input_fd);
787 } else {
Linus Torvalds4c81b032008-05-30 08:42:16 -0700788 fsync_or_die(output_fd, curr_pack_name);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400789 err = close(output_fd);
790 if (err)
791 die("error while closing pack file: %s", strerror(errno));
Nicolas Pitree42797f2006-10-23 14:50:18 -0400792 }
793
Shawn Pearceb8077702006-10-29 04:41:59 -0500794 if (keep_msg) {
795 int keep_fd, keep_msg_len = strlen(keep_msg);
796 if (!keep_name) {
797 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
798 get_object_directory(), sha1_to_hex(sha1));
799 keep_name = name;
800 }
Nicolas Pitre9ca4a202006-11-01 17:06:24 -0500801 keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
802 if (keep_fd < 0) {
803 if (errno != EEXIST)
804 die("cannot write keep file");
805 } else {
806 if (keep_msg_len > 0) {
807 write_or_die(keep_fd, keep_msg, keep_msg_len);
808 write_or_die(keep_fd, "\n", 1);
809 }
Jim Meyering91c8d592007-06-24 21:20:41 +0200810 if (close(keep_fd) != 0)
811 die("cannot write keep file");
Nicolas Pitre576162a2006-11-01 17:06:25 -0500812 report = "keep";
Shawn Pearceb8077702006-10-29 04:41:59 -0500813 }
Shawn Pearceb8077702006-10-29 04:41:59 -0500814 }
815
Nicolas Pitree42797f2006-10-23 14:50:18 -0400816 if (final_pack_name != curr_pack_name) {
817 if (!final_pack_name) {
818 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
819 get_object_directory(), sha1_to_hex(sha1));
820 final_pack_name = name;
821 }
822 if (move_temp_to_file(curr_pack_name, final_pack_name))
823 die("cannot store pack file");
824 }
Petr Baudis33b65032008-10-03 12:20:43 +0200825 if (from_stdin)
826 chmod(final_pack_name, 0444);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400827
Nicolas Pitree42797f2006-10-23 14:50:18 -0400828 if (final_index_name != curr_index_name) {
829 if (!final_index_name) {
830 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
831 get_object_directory(), sha1_to_hex(sha1));
832 final_index_name = name;
833 }
834 if (move_temp_to_file(curr_index_name, final_index_name))
835 die("cannot store index file");
836 }
Petr Baudis33b65032008-10-03 12:20:43 +0200837 chmod(final_index_name, 0444);
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];
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700882
Nicolas Pitrea672ea62008-10-20 21:17:07 -0400883 /*
884 * We wish to read the repository's config file if any, and
885 * for that it is necessary to call setup_git_directory_gently().
886 * However if the cwd was inside .git/objects/pack/ then we need
887 * to go back there or all the pack name arguments will be wrong.
888 * And in that case we cannot rely on any prefix returned by
889 * setup_git_directory_gently() either.
890 */
891 {
892 char cwd[PATH_MAX+1];
893 int nongit;
894
895 if (!getcwd(cwd, sizeof(cwd)-1))
896 die("Unable to get current working directory");
897 setup_git_directory_gently(&nongit);
898 git_config(git_index_pack_config, NULL);
899 if (chdir(cwd))
900 die("Cannot come back to cwd");
901 }
Nicolas Pitre4d00bda2007-11-01 23:26:04 -0400902
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700903 for (i = 1; i < argc; i++) {
Nicolas Pitre4049b9c2007-10-16 21:55:49 -0400904 char *arg = argv[i];
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700905
906 if (*arg == '-') {
Nicolas Pitree42797f2006-10-23 14:50:18 -0400907 if (!strcmp(arg, "--stdin")) {
908 from_stdin = 1;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400909 } else if (!strcmp(arg, "--fix-thin")) {
910 fix_thin_pack = 1;
Martin Koegler0153be02008-02-25 22:46:12 +0100911 } else if (!strcmp(arg, "--strict")) {
912 strict = 1;
Shawn Pearceb8077702006-10-29 04:41:59 -0500913 } else if (!strcmp(arg, "--keep")) {
914 keep_msg = "";
Junio C Hamanocc44c762007-02-20 01:53:29 -0800915 } else if (!prefixcmp(arg, "--keep=")) {
Shawn Pearceb8077702006-10-29 04:41:59 -0500916 keep_msg = arg + 7;
Junio C Hamanocc44c762007-02-20 01:53:29 -0800917 } else if (!prefixcmp(arg, "--pack_header=")) {
Nicolas Pitrebed006f2006-11-01 17:06:20 -0500918 struct pack_header *hdr;
919 char *c;
920
921 hdr = (struct pack_header *)input_buffer;
922 hdr->hdr_signature = htonl(PACK_SIGNATURE);
923 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
924 if (*c != ',')
925 die("bad %s", arg);
926 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
927 if (*c)
928 die("bad %s", arg);
929 input_len = sizeof(*hdr);
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400930 } else if (!strcmp(arg, "-v")) {
931 verbose = 1;
Nicolas Pitree42797f2006-10-23 14:50:18 -0400932 } else if (!strcmp(arg, "-o")) {
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700933 if (index_name || (i+1) >= argc)
934 usage(index_pack_usage);
935 index_name = argv[++i];
Nicolas Pitre4ba7d712007-04-09 17:32:03 -0400936 } else if (!prefixcmp(arg, "--index-version=")) {
937 char *c;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400938 pack_idx_default_version = strtoul(arg + 16, &c, 10);
939 if (pack_idx_default_version > 2)
Nicolas Pitre4ba7d712007-04-09 17:32:03 -0400940 die("bad %s", arg);
941 if (*c == ',')
Geert Boschaa7e44b2007-06-01 15:18:05 -0400942 pack_idx_off32_limit = strtoul(c+1, &c, 0);
943 if (*c || pack_idx_off32_limit & 0x80000000)
Nicolas Pitre4ba7d712007-04-09 17:32:03 -0400944 die("bad %s", arg);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700945 } else
946 usage(index_pack_usage);
947 continue;
948 }
949
950 if (pack_name)
951 usage(index_pack_usage);
952 pack_name = arg;
953 }
954
Nicolas Pitree42797f2006-10-23 14:50:18 -0400955 if (!pack_name && !from_stdin)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700956 usage(index_pack_usage);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400957 if (fix_thin_pack && !from_stdin)
958 die("--fix-thin cannot be used without --stdin");
Nicolas Pitree42797f2006-10-23 14:50:18 -0400959 if (!index_name && pack_name) {
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700960 int len = strlen(pack_name);
Rene Scharfe5bb1cda2006-08-11 14:01:45 +0200961 if (!has_extension(pack_name, ".pack"))
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700962 die("packfile name '%s' does not end with '.pack'",
963 pack_name);
Pavel Roskin6689f082005-12-21 15:35:48 -0500964 index_name_buf = xmalloc(len);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700965 memcpy(index_name_buf, pack_name, len - 5);
966 strcpy(index_name_buf + len - 5, ".idx");
967 index_name = index_name_buf;
968 }
Shawn Pearceb8077702006-10-29 04:41:59 -0500969 if (keep_msg && !keep_name && pack_name) {
970 int len = strlen(pack_name);
971 if (!has_extension(pack_name, ".pack"))
972 die("packfile name '%s' does not end with '.pack'",
973 pack_name);
974 keep_name_buf = xmalloc(len);
975 memcpy(keep_name_buf, pack_name, len - 5);
976 strcpy(keep_name_buf + len - 5, ".keep");
977 keep_name = keep_name_buf;
978 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700979
Nicolas Pitree42797f2006-10-23 14:50:18 -0400980 curr_pack = open_pack_file(pack_name);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700981 parse_pack_header();
Nicolas Pitre636171c2006-10-25 23:28:17 -0400982 objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
983 deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
Nicolas Pitre85221482008-08-29 16:08:01 -0400984 parse_pack_objects(pack_sha1);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400985 if (nr_deltas == nr_resolved_deltas) {
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400986 stop_progress(&progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400987 /* Flush remaining pack final 20-byte SHA1. */
988 flush();
989 } else {
Nicolas Pitre636171c2006-10-25 23:28:17 -0400990 if (fix_thin_pack) {
Nicolas Pitre85221482008-08-29 16:08:01 -0400991 struct sha1file *f;
992 unsigned char read_sha1[20], tail_sha1[20];
Nicolas Pitrea984a062007-11-08 15:45:41 -0500993 char msg[48];
Nicolas Pitre636171c2006-10-25 23:28:17 -0400994 int nr_unresolved = nr_deltas - nr_resolved_deltas;
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400995 int nr_objects_initial = nr_objects;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400996 if (nr_unresolved <= 0)
997 die("confusion beyond insanity");
998 objects = xrealloc(objects,
999 (nr_objects + nr_unresolved + 1)
1000 * sizeof(*objects));
Nicolas Pitre85221482008-08-29 16:08:01 -04001001 f = sha1fd(output_fd, curr_pack);
1002 fix_unresolved_deltas(f, nr_unresolved);
Nicolas Pitrea984a062007-11-08 15:45:41 -05001003 sprintf(msg, "completed with %d local objects",
1004 nr_objects - nr_objects_initial);
1005 stop_progress_msg(&progress, msg);
Nicolas Pitre85221482008-08-29 16:08:01 -04001006 sha1close(f, tail_sha1, 0);
1007 hashcpy(read_sha1, pack_sha1);
1008 fixup_pack_header_footer(output_fd, pack_sha1,
Nicolas Pitreabeb40e2008-08-29 16:07:59 -04001009 curr_pack, nr_objects,
Nicolas Pitre85221482008-08-29 16:08:01 -04001010 read_sha1, consumed_bytes-20);
1011 if (hashcmp(read_sha1, tail_sha1) != 0)
1012 die("Unexpected tail checksum for %s "
1013 "(disk corruption?)", curr_pack);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001014 }
1015 if (nr_deltas != nr_resolved_deltas)
1016 die("pack has %d unresolved deltas",
1017 nr_deltas - nr_resolved_deltas);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001018 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001019 free(deltas);
Martin Koegler0153be02008-02-25 22:46:12 +01001020 if (strict)
1021 check_objects();
Geert Boschaa7e44b2007-06-01 15:18:05 -04001022
1023 idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
1024 for (i = 0; i < nr_objects; i++)
1025 idx_objects[i] = &objects[i].idx;
Nicolas Pitre85221482008-08-29 16:08:01 -04001026 curr_index = write_idx_file(index_name, idx_objects, nr_objects, pack_sha1);
Geert Boschaa7e44b2007-06-01 15:18:05 -04001027 free(idx_objects);
1028
Shawn Pearceb8077702006-10-29 04:41:59 -05001029 final(pack_name, curr_pack,
1030 index_name, curr_index,
1031 keep_name, keep_msg,
Nicolas Pitre85221482008-08-29 16:08:01 -04001032 pack_sha1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001033 free(objects);
1034 free(index_name_buf);
Shawn Pearceb8077702006-10-29 04:41:59 -05001035 free(keep_name_buf);
Nicolas Pitrec85228e2007-10-16 21:55:50 -04001036 if (pack_name == NULL)
1037 free(curr_pack);
1038 if (index_name == NULL)
1039 free(curr_index);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001040
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001041 return 0;
1042}