blob: b4cf8c53e0ebbee65a0e4bc0ac1afd1173d1b8e8 [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"
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +010011#include "exec_cmd.h"
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070012
13static const char index_pack_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020014"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 -070015
16struct object_entry
17{
Geert Boschaa7e44b2007-06-01 15:18:05 -040018 struct pack_idx_entry idx;
Nicolas Pitre2d477052006-10-20 14:45:21 -040019 unsigned long size;
20 unsigned int hdr_size;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070021 enum object_type type;
22 enum object_type real_type;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070023};
24
Nicolas Pitre53dda6f2006-09-21 00:08:33 -040025union delta_base {
26 unsigned char sha1[20];
Nicolas Pitred7dd0222007-04-09 01:06:30 -040027 off_t offset;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -040028};
29
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -040030struct base_data {
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -040031 struct base_data *base;
32 struct base_data *child;
Shawn O. Pearce03993e12008-07-13 22:07:46 -040033 struct object_entry *obj;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -040034 void *data;
35 unsigned long size;
36};
37
Nicolas Pitre3c552872006-10-17 16:23:26 -040038/*
39 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
40 * to memcmp() only the first 20 bytes.
41 */
42#define UNION_BASE_SZ 20
43
Martin Koegler0153be02008-02-25 22:46:12 +010044#define FLAG_LINK (1u<<20)
45#define FLAG_CHECKED (1u<<21)
46
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070047struct delta_entry
48{
Nicolas Pitre53dda6f2006-09-21 00:08:33 -040049 union delta_base base;
Nicolas Pitre636171c2006-10-25 23:28:17 -040050 int obj_no;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070051};
52
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070053static struct object_entry *objects;
54static struct delta_entry *deltas;
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -040055static struct base_data *base_cache;
Shawn O. Pearce92392b42008-07-15 04:45:34 +000056static size_t base_cache_used;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070057static int nr_objects;
58static int nr_deltas;
Nicolas Pitre636171c2006-10-25 23:28:17 -040059static int nr_resolved_deltas;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -070060
Nicolas Pitree42797f2006-10-23 14:50:18 -040061static int from_stdin;
Martin Koegler0153be02008-02-25 22:46:12 +010062static int strict;
Nicolas Pitre3c9af362006-10-25 23:32:59 -040063static int verbose;
64
Nicolas Pitredc6a0752007-10-30 14:57:32 -040065static struct progress *progress;
Nicolas Pitree42797f2006-10-23 14:50:18 -040066
Nicolas Pitre2d477052006-10-20 14:45:21 -040067/* We always read in 4kB chunks. */
68static unsigned char input_buffer[4096];
Nicolas Pitred7dd0222007-04-09 01:06:30 -040069static unsigned int input_offset, input_len;
70static off_t consumed_bytes;
Nicolas Pitre9126f002008-10-01 14:05:20 -040071static git_SHA_CTX input_ctx;
Nicolas Pitreee5743c2007-04-09 01:06:32 -040072static uint32_t input_crc32;
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -050073static int input_fd, output_fd, pack_fd;
Nicolas Pitre2d477052006-10-20 14:45:21 -040074
Martin Koegler0153be02008-02-25 22:46:12 +010075static int mark_link(struct object *obj, int type, void *data)
76{
77 if (!obj)
78 return -1;
79
80 if (type != OBJ_ANY && obj->type != type)
81 die("object type mismatch at %s", sha1_to_hex(obj->sha1));
82
83 obj->flags |= FLAG_LINK;
84 return 0;
85}
86
87/* The content of each linked object must have been checked
88 or it must be already present in the object database */
89static void check_object(struct object *obj)
90{
91 if (!obj)
92 return;
93
94 if (!(obj->flags & FLAG_LINK))
95 return;
96
97 if (!(obj->flags & FLAG_CHECKED)) {
98 unsigned long size;
99 int type = sha1_object_info(obj->sha1, &size);
100 if (type != obj->type || type <= 0)
101 die("object of unexpected type");
102 obj->flags |= FLAG_CHECKED;
103 return;
104 }
105}
106
107static void check_objects(void)
108{
109 unsigned i, max;
110
111 max = get_max_object_index();
112 for (i = 0; i < max; i++)
113 check_object(get_indexed_object(i));
114}
115
116
Nicolas Pitre636171c2006-10-25 23:28:17 -0400117/* Discard current buffer used content. */
Rene Scharfea6e8a762006-11-18 13:07:06 +0100118static void flush(void)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400119{
120 if (input_offset) {
121 if (output_fd >= 0)
122 write_or_die(output_fd, input_buffer, input_offset);
Nicolas Pitre9126f002008-10-01 14:05:20 -0400123 git_SHA1_Update(&input_ctx, input_buffer, input_offset);
Jim Meyering554a2632006-12-11 19:06:34 +0100124 memmove(input_buffer, input_buffer + input_offset, input_len);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400125 input_offset = 0;
126 }
127}
128
Nicolas Pitre2d477052006-10-20 14:45:21 -0400129/*
130 * Make sure at least "min" bytes are available in the buffer, and
131 * return the pointer to the buffer.
132 */
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400133static void *fill(int min)
Nicolas Pitre2d477052006-10-20 14:45:21 -0400134{
135 if (min <= input_len)
136 return input_buffer + input_offset;
137 if (min > sizeof(input_buffer))
138 die("cannot fill %d bytes", min);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400139 flush();
Nicolas Pitre2d477052006-10-20 14:45:21 -0400140 do {
Johan Herland8a912bc2007-05-15 14:49:22 +0200141 ssize_t ret = xread(input_fd, input_buffer + input_len,
Nicolas Pitre2d477052006-10-20 14:45:21 -0400142 sizeof(input_buffer) - input_len);
143 if (ret <= 0) {
144 if (!ret)
145 die("early EOF");
Thomas Rastd824cbb2009-06-27 17:58:46 +0200146 die_errno("read error on input");
Nicolas Pitre2d477052006-10-20 14:45:21 -0400147 }
148 input_len += ret;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500149 if (from_stdin)
150 display_throughput(progress, consumed_bytes + input_len);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400151 } while (input_len < min);
152 return input_buffer;
153}
154
155static void use(int bytes)
156{
157 if (bytes > input_len)
158 die("used more bytes than were available");
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400159 input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400160 input_len -= bytes;
161 input_offset += bytes;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400162
163 /* make sure off_t is sufficiently large not to wrap */
164 if (consumed_bytes > consumed_bytes + bytes)
165 die("pack too large for current definition of off_t");
Nicolas Pitre2d477052006-10-20 14:45:21 -0400166 consumed_bytes += bytes;
167}
168
Linus Torvalds3bb72562010-01-22 07:55:19 -0800169static const char *open_pack_file(const char *pack_name)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700170{
Nicolas Pitree42797f2006-10-23 14:50:18 -0400171 if (from_stdin) {
172 input_fd = 0;
173 if (!pack_name) {
174 static char tmpfile[PATH_MAX];
Junio C Hamano6e180cd2009-02-24 23:11:29 -0800175 output_fd = odb_mkstemp(tmpfile, sizeof(tmpfile),
176 "pack/tmp_pack_XXXXXX");
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)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200181 die_errno("unable to create '%s'", pack_name);
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)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200186 die_errno("cannot open packfile '%s'", pack_name);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400187 output_fd = -1;
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500188 pack_fd = input_fd;
Nicolas Pitree42797f2006-10-23 14:50:18 -0400189 }
Nicolas Pitre9126f002008-10-01 14:05:20 -0400190 git_SHA1_Init(&input_ctx);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400191 return pack_name;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700192}
193
194static void parse_pack_header(void)
195{
Nicolas Pitre2d477052006-10-20 14:45:21 -0400196 struct pack_header *hdr = fill(sizeof(struct pack_header));
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700197
198 /* Header consistency check */
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700199 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
Nicolas Pitree42797f2006-10-23 14:50:18 -0400200 die("pack signature mismatch");
Nicolas Pitred60fc1c2006-02-09 17:50:04 -0500201 if (!pack_version_ok(hdr->hdr_version))
Ramsay Jones6e1c2342008-07-03 16:52:09 +0100202 die("pack version %"PRIu32" unsupported",
203 ntohl(hdr->hdr_version));
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700204
205 nr_objects = ntohl(hdr->hdr_entries);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400206 use(sizeof(struct pack_header));
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700207}
208
Erik Faye-Lunda4f31312009-09-30 18:05:49 +0000209static NORETURN void bad_object(unsigned long offset, const char *format,
210 ...) __attribute__((format (printf, 2, 3)));
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700211
212static void bad_object(unsigned long offset, const char *format, ...)
213{
214 va_list params;
215 char buf[1024];
216
217 va_start(params, format);
218 vsnprintf(buf, sizeof(buf), format, params);
219 va_end(params);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400220 die("pack has bad object at offset %lu: %s", offset, buf);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700221}
222
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400223static void free_base_data(struct base_data *c)
224{
225 if (c->data) {
226 free(c->data);
227 c->data = NULL;
228 base_cache_used -= c->size;
229 }
230}
231
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000232static void prune_base_data(struct base_data *retain)
233{
Benjamin Kramer8e24cba2009-03-15 22:01:20 +0100234 struct base_data *b;
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000235 for (b = base_cache;
236 base_cache_used > delta_base_cache_limit && b;
237 b = b->child) {
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400238 if (b->data && b != retain)
239 free_base_data(b);
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000240 }
241}
242
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400243static void link_base_data(struct base_data *base, struct base_data *c)
244{
245 if (base)
246 base->child = c;
247 else
248 base_cache = c;
249
250 c->base = base;
251 c->child = NULL;
Nicolas Pitre9441b612008-10-17 15:57:57 -0400252 if (c->data)
253 base_cache_used += c->size;
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000254 prune_base_data(c);
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400255}
256
257static void unlink_base_data(struct base_data *c)
258{
259 struct base_data *base = c->base;
260 if (base)
261 base->child = NULL;
262 else
263 base_cache = NULL;
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400264 free_base_data(c);
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400265}
266
Nicolas Pitre2d477052006-10-20 14:45:21 -0400267static void *unpack_entry_data(unsigned long offset, unsigned long size)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700268{
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700269 z_stream stream;
270 void *buf = xmalloc(size);
271
272 memset(&stream, 0, sizeof(stream));
273 stream.next_out = buf;
274 stream.avail_out = size;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400275 stream.next_in = fill(1);
276 stream.avail_in = input_len;
Linus Torvalds39c68542009-01-07 19:54:47 -0800277 git_inflate_init(&stream);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700278
279 for (;;) {
Linus Torvalds39c68542009-01-07 19:54:47 -0800280 int ret = git_inflate(&stream, 0);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400281 use(input_len - stream.avail_in);
282 if (stream.total_out == size && ret == Z_STREAM_END)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700283 break;
284 if (ret != Z_OK)
285 bad_object(offset, "inflate returned %d", ret);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400286 stream.next_in = fill(1);
287 stream.avail_in = input_len;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700288 }
Linus Torvalds39c68542009-01-07 19:54:47 -0800289 git_inflate_end(&stream);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700290 return buf;
291}
292
Nicolas Pitre2d477052006-10-20 14:45:21 -0400293static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700294{
Linus Torvalds48fb7de2009-06-17 17:22:27 -0700295 unsigned char *p;
296 unsigned long size, c;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400297 off_t base_offset;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700298 unsigned shift;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400299 void *data;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700300
Geert Boschaa7e44b2007-06-01 15:18:05 -0400301 obj->idx.offset = consumed_bytes;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400302 input_crc32 = crc32(0, Z_NULL, 0);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400303
304 p = fill(1);
305 c = *p;
306 use(1);
307 obj->type = (c >> 4) & 7;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700308 size = (c & 15);
309 shift = 4;
310 while (c & 0x80) {
Nicolas Pitre2d477052006-10-20 14:45:21 -0400311 p = fill(1);
312 c = *p;
313 use(1);
Linus Torvalds48fb7de2009-06-17 17:22:27 -0700314 size += (c & 0x7f) << shift;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700315 shift += 7;
316 }
Nicolas Pitre2d477052006-10-20 14:45:21 -0400317 obj->size = size;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700318
Nicolas Pitre2d477052006-10-20 14:45:21 -0400319 switch (obj->type) {
Nicolas Pitreeb32d232006-09-21 00:06:49 -0400320 case OBJ_REF_DELTA:
Nicolas Pitre2d477052006-10-20 14:45:21 -0400321 hashcpy(delta_base->sha1, fill(20));
322 use(20);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400323 break;
324 case OBJ_OFS_DELTA:
325 memset(delta_base, 0, sizeof(*delta_base));
Nicolas Pitre2d477052006-10-20 14:45:21 -0400326 p = fill(1);
327 c = *p;
328 use(1);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400329 base_offset = c & 127;
330 while (c & 128) {
331 base_offset += 1;
Nicolas Pitre8723f212007-04-09 01:06:29 -0400332 if (!base_offset || MSB(base_offset, 7))
Geert Boschaa7e44b2007-06-01 15:18:05 -0400333 bad_object(obj->idx.offset, "offset value overflow for delta base object");
Nicolas Pitre2d477052006-10-20 14:45:21 -0400334 p = fill(1);
335 c = *p;
336 use(1);
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400337 base_offset = (base_offset << 7) + (c & 127);
338 }
Geert Boschaa7e44b2007-06-01 15:18:05 -0400339 delta_base->offset = obj->idx.offset - base_offset;
Nicolas Pitred8f32552008-10-29 19:02:45 -0400340 if (delta_base->offset <= 0 || delta_base->offset >= obj->idx.offset)
Geert Boschaa7e44b2007-06-01 15:18:05 -0400341 bad_object(obj->idx.offset, "delta base offset is out of bound");
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400342 break;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700343 case OBJ_COMMIT:
344 case OBJ_TREE:
345 case OBJ_BLOB:
346 case OBJ_TAG:
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700347 break;
348 default:
Geert Boschaa7e44b2007-06-01 15:18:05 -0400349 bad_object(obj->idx.offset, "unknown object type %d", obj->type);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700350 }
Geert Boschaa7e44b2007-06-01 15:18:05 -0400351 obj->hdr_size = consumed_bytes - obj->idx.offset;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700352
Geert Boschaa7e44b2007-06-01 15:18:05 -0400353 data = unpack_entry_data(obj->idx.offset, obj->size);
354 obj->idx.crc32 = input_crc32;
Nicolas Pitreee5743c2007-04-09 01:06:32 -0400355 return data;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400356}
357
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400358static void *get_data_from_pack(struct object_entry *obj)
Nicolas Pitre2d477052006-10-20 14:45:21 -0400359{
Nicolas Pitrea91ef6e2007-11-10 23:29:10 -0500360 off_t from = obj[0].idx.offset + obj[0].hdr_size;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400361 unsigned long len = obj[1].idx.offset - from;
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500362 unsigned long rdy = 0;
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500363 unsigned char *src, *data;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400364 z_stream stream;
365 int st;
366
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500367 src = xmalloc(len);
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500368 data = src;
369 do {
370 ssize_t n = pread(pack_fd, data + rdy, len - rdy, from + rdy);
Samuel Tardieufb742432008-10-06 19:28:41 +0200371 if (n < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200372 die_errno("cannot pread pack file");
Samuel Tardieufb742432008-10-06 19:28:41 +0200373 if (!n)
374 die("premature end of pack file, %lu bytes missing",
375 len - rdy);
Shawn O. Pearcea91d49c2007-02-27 23:47:19 -0500376 rdy += n;
377 } while (rdy < len);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400378 data = xmalloc(obj->size);
379 memset(&stream, 0, sizeof(stream));
380 stream.next_out = data;
381 stream.avail_out = obj->size;
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500382 stream.next_in = src;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400383 stream.avail_in = len;
Linus Torvalds39c68542009-01-07 19:54:47 -0800384 git_inflate_init(&stream);
385 while ((st = git_inflate(&stream, Z_FINISH)) == Z_OK);
386 git_inflate_end(&stream);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400387 if (st != Z_STREAM_END || stream.total_out != obj->size)
388 die("serious inflate inconsistency");
Nicolas Pitre6d2fa7f2006-12-19 10:53:08 -0500389 free(src);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700390 return data;
391}
392
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400393static int find_delta(const union delta_base *base)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700394{
395 int first = 0, last = nr_deltas;
396
397 while (first < last) {
398 int next = (first + last) / 2;
399 struct delta_entry *delta = &deltas[next];
400 int cmp;
401
Nicolas Pitre3c552872006-10-17 16:23:26 -0400402 cmp = memcmp(base, &delta->base, UNION_BASE_SZ);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700403 if (!cmp)
404 return next;
405 if (cmp < 0) {
406 last = next;
407 continue;
408 }
409 first = next+1;
410 }
411 return -first-1;
412}
413
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400414static void find_delta_children(const union delta_base *base,
415 int *first_index, int *last_index)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700416{
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400417 int first = find_delta(base);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700418 int last = first;
419 int end = nr_deltas - 1;
420
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400421 if (first < 0) {
422 *first_index = 0;
423 *last_index = -1;
424 return;
425 }
Nicolas Pitre3c552872006-10-17 16:23:26 -0400426 while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700427 --first;
Nicolas Pitre3c552872006-10-17 16:23:26 -0400428 while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700429 ++last;
430 *first_index = first;
431 *last_index = last;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700432}
433
434static void sha1_object(const void *data, unsigned long size,
Nicolas Pitre9096c662007-03-20 17:07:48 -0400435 enum object_type type, unsigned char *sha1)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700436{
Nicolas Pitrece9fbf12007-03-20 16:02:09 -0400437 hash_sha1_file(data, size, typename(type), sha1);
Nicolas Pitre9096c662007-03-20 17:07:48 -0400438 if (has_sha1_file(sha1)) {
Nicolas Pitre8685da42007-03-20 15:32:35 -0400439 void *has_data;
440 enum object_type has_type;
441 unsigned long has_size;
442 has_data = read_sha1_file(sha1, &has_type, &has_size);
443 if (!has_data)
444 die("cannot read existing object %s", sha1_to_hex(sha1));
445 if (size != has_size || type != has_type ||
446 memcmp(data, has_data, size) != 0)
447 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1));
Nicolas Pitrebbf4b412007-04-03 12:33:46 -0400448 free(has_data);
Nicolas Pitre8685da42007-03-20 15:32:35 -0400449 }
Martin Koegler0153be02008-02-25 22:46:12 +0100450 if (strict) {
451 if (type == OBJ_BLOB) {
452 struct blob *blob = lookup_blob(sha1);
453 if (blob)
454 blob->object.flags |= FLAG_CHECKED;
455 else
456 die("invalid blob object %s", sha1_to_hex(sha1));
457 } else {
458 struct object *obj;
459 int eaten;
460 void *buf = (void *) data;
461
462 /*
463 * we do not need to free the memory here, as the
464 * buf is deleted by the caller.
465 */
466 obj = parse_object_buffer(sha1, type, size, buf, &eaten);
467 if (!obj)
468 die("invalid %s", typename(type));
469 if (fsck_object(obj, 1, fsck_error_function))
470 die("Error in object");
Linus Torvalds2af202b2009-06-18 10:28:43 -0700471 if (fsck_walk(obj, mark_link, NULL))
Martin Koegler0153be02008-02-25 22:46:12 +0100472 die("Not all child objects of %s are reachable", sha1_to_hex(obj->sha1));
473
474 if (obj->type == OBJ_TREE) {
475 struct tree *item = (struct tree *) obj;
476 item->buffer = NULL;
477 }
478 if (obj->type == OBJ_COMMIT) {
479 struct commit *commit = (struct commit *) obj;
480 commit->buffer = NULL;
481 }
482 obj->flags |= FLAG_CHECKED;
483 }
484 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700485}
486
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000487static void *get_base_data(struct base_data *c)
488{
489 if (!c->data) {
490 struct object_entry *obj = c->obj;
491
492 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
493 void *base = get_base_data(c->base);
494 void *raw = get_data_from_pack(obj);
495 c->data = patch_delta(
496 base, c->base->size,
497 raw, obj->size,
498 &c->size);
499 free(raw);
500 if (!c->data)
501 bad_object(obj->idx.offset, "failed to apply delta");
Nicolas Pitre9441b612008-10-17 15:57:57 -0400502 } else {
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000503 c->data = get_data_from_pack(obj);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400504 c->size = obj->size;
505 }
Shawn O. Pearce92392b42008-07-15 04:45:34 +0000506
507 base_cache_used += c->size;
508 prune_base_data(c);
509 }
510 return c->data;
511}
512
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400513static void resolve_delta(struct object_entry *delta_obj,
Nicolas Pitre9441b612008-10-17 15:57:57 -0400514 struct base_data *base, struct base_data *result)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700515{
Nicolas Pitrece3f6dc2008-10-20 16:46:19 -0400516 void *base_data, *delta_data;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700517
Nicolas Pitrece3f6dc2008-10-20 16:46:19 -0400518 delta_obj->real_type = base->obj->real_type;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400519 delta_data = get_data_from_pack(delta_obj);
Nicolas Pitrece3f6dc2008-10-20 16:46:19 -0400520 base_data = get_base_data(base);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400521 result->obj = delta_obj;
Nicolas Pitrece3f6dc2008-10-20 16:46:19 -0400522 result->data = patch_delta(base_data, base->size,
523 delta_data, delta_obj->size, &result->size);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700524 free(delta_data);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400525 if (!result->data)
Geert Boschaa7e44b2007-06-01 15:18:05 -0400526 bad_object(delta_obj->idx.offset, "failed to apply delta");
Nicolas Pitre9441b612008-10-17 15:57:57 -0400527 sha1_object(result->data, result->size, delta_obj->real_type,
528 delta_obj->idx.sha1);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400529 nr_resolved_deltas++;
Nicolas Pitre9441b612008-10-17 15:57:57 -0400530}
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400531
Nicolas Pitre9441b612008-10-17 15:57:57 -0400532static void find_unresolved_deltas(struct base_data *base,
533 struct base_data *prev_base)
534{
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400535 int i, ref_first, ref_last, ofs_first, ofs_last;
Shawn O. Pearce4a438ca2008-07-13 22:07:45 -0400536
Nicolas Pitre9441b612008-10-17 15:57:57 -0400537 /*
538 * This is a recursive function. Those brackets should help reducing
539 * stack usage by limiting the scope of the delta_base union.
540 */
541 {
542 union delta_base base_spec;
543
544 hashcpy(base_spec.sha1, base->obj->idx.sha1);
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400545 find_delta_children(&base_spec, &ref_first, &ref_last);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400546
547 memset(&base_spec, 0, sizeof(base_spec));
548 base_spec.offset = base->obj->idx.offset;
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400549 find_delta_children(&base_spec, &ofs_first, &ofs_last);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400550 }
551
Nicolas Pitre9ed19612008-10-23 15:05:59 -0400552 if (ref_last == -1 && ofs_last == -1) {
553 free(base->data);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400554 return;
Nicolas Pitre9ed19612008-10-23 15:05:59 -0400555 }
Nicolas Pitre9441b612008-10-17 15:57:57 -0400556
557 link_base_data(prev_base, base);
558
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400559 for (i = ref_first; i <= ref_last; i++) {
560 struct object_entry *child = objects + deltas[i].obj_no;
561 if (child->real_type == OBJ_REF_DELTA) {
562 struct base_data result;
563 resolve_delta(child, base, &result);
564 if (i == ref_last && ofs_last == -1)
565 free_base_data(base);
566 find_unresolved_deltas(&result, base);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400567 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700568 }
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400569
Nicolas Pitre6a87ed92008-10-17 15:57:58 -0400570 for (i = ofs_first; i <= ofs_last; i++) {
571 struct object_entry *child = objects + deltas[i].obj_no;
572 if (child->real_type == OBJ_OFS_DELTA) {
573 struct base_data result;
574 resolve_delta(child, base, &result);
575 if (i == ofs_last)
576 free_base_data(base);
577 find_unresolved_deltas(&result, base);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400578 }
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400579 }
580
Nicolas Pitre9441b612008-10-17 15:57:57 -0400581 unlink_base_data(base);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700582}
583
584static int compare_delta_entry(const void *a, const void *b)
585{
586 const struct delta_entry *delta_a = a;
587 const struct delta_entry *delta_b = b;
Nicolas Pitre3c552872006-10-17 16:23:26 -0400588 return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700589}
590
Nicolas Pitre2d477052006-10-20 14:45:21 -0400591/* Parse all objects and return the pack content SHA1 hash */
592static void parse_pack_objects(unsigned char *sha1)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700593{
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400594 int i;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400595 struct delta_entry *delta = deltas;
Nicolas Pitre2d477052006-10-20 14:45:21 -0400596 struct stat st;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700597
598 /*
599 * First pass:
600 * - find locations of all objects;
601 * - calculate SHA1 of all non-delta objects;
Nicolas Pitreb89c4e92006-10-27 16:14:23 -0400602 * - remember base (SHA1 or offset) for all deltas.
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700603 */
Nicolas Pitre13aaf142007-04-20 14:10:07 -0400604 if (verbose)
Nicolas Pitre29e63ed2007-10-30 14:57:35 -0400605 progress = start_progress(
606 from_stdin ? "Receiving objects" : "Indexing objects",
607 nr_objects);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700608 for (i = 0; i < nr_objects; i++) {
609 struct object_entry *obj = &objects[i];
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400610 void *data = unpack_raw_entry(obj, &delta->base);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700611 obj->real_type = obj->type;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400612 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
613 nr_deltas++;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400614 delta->obj_no = i;
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400615 delta++;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700616 } else
Geert Boschaa7e44b2007-06-01 15:18:05 -0400617 sha1_object(data, obj->size, obj->type, obj->idx.sha1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700618 free(data);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400619 display_progress(progress, i+1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700620 }
Geert Boschaa7e44b2007-06-01 15:18:05 -0400621 objects[i].idx.offset = consumed_bytes;
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400622 stop_progress(&progress);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400623
624 /* Check pack integrity */
Nicolas Pitre636171c2006-10-25 23:28:17 -0400625 flush();
Nicolas Pitre9126f002008-10-01 14:05:20 -0400626 git_SHA1_Final(sha1, &input_ctx);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400627 if (hashcmp(fill(20), sha1))
Nicolas Pitree42797f2006-10-23 14:50:18 -0400628 die("pack is corrupted (SHA1 mismatch)");
Nicolas Pitre9bee2472006-10-25 23:31:53 -0400629 use(20);
Nicolas Pitre2d477052006-10-20 14:45:21 -0400630
631 /* If input_fd is a file, we should have reached its end now. */
632 if (fstat(input_fd, &st))
Thomas Rastd824cbb2009-06-27 17:58:46 +0200633 die_errno("cannot fstat packfile");
Johannes Schindelinfa257b02007-02-22 19:14:14 +0100634 if (S_ISREG(st.st_mode) &&
635 lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
Nicolas Pitree42797f2006-10-23 14:50:18 -0400636 die("pack has junk at the end");
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700637
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400638 if (!nr_deltas)
639 return;
640
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400641 /* Sort deltas by base SHA1/offset for fast searching */
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700642 qsort(deltas, nr_deltas, sizeof(struct delta_entry),
643 compare_delta_entry);
644
645 /*
646 * Second pass:
647 * - for all non-delta objects, look if it is used as a base for
648 * deltas;
649 * - if used as a base, uncompress the object and apply all deltas,
650 * recursively checking if the resulting object is used as a base
651 * for some more deltas.
652 */
Nicolas Pitre13aaf142007-04-20 14:10:07 -0400653 if (verbose)
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400654 progress = start_progress("Resolving deltas", nr_deltas);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700655 for (i = 0; i < nr_objects; i++) {
656 struct object_entry *obj = &objects[i];
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400657 struct base_data base_obj;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700658
Nicolas Pitre53dda6f2006-09-21 00:08:33 -0400659 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700660 continue;
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400661 base_obj.obj = obj;
Nicolas Pitre9441b612008-10-17 15:57:57 -0400662 base_obj.data = NULL;
663 find_unresolved_deltas(&base_obj, NULL);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400664 display_progress(progress, nr_resolved_deltas);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700665 }
Nicolas Pitre636171c2006-10-25 23:28:17 -0400666}
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700667
Nicolas Pitre85221482008-08-29 16:08:01 -0400668static int write_compressed(struct sha1file *f, void *in, unsigned int size)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400669{
670 z_stream stream;
671 unsigned long maxsize;
672 void *out;
673
674 memset(&stream, 0, sizeof(stream));
675 deflateInit(&stream, zlib_compression_level);
676 maxsize = deflateBound(&stream, size);
677 out = xmalloc(maxsize);
678
679 /* Compress it */
680 stream.next_in = in;
681 stream.avail_in = size;
682 stream.next_out = out;
683 stream.avail_out = maxsize;
684 while (deflate(&stream, Z_FINISH) == Z_OK);
685 deflateEnd(&stream);
686
687 size = stream.total_out;
Nicolas Pitre85221482008-08-29 16:08:01 -0400688 sha1write(f, out, size);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400689 free(out);
690 return size;
691}
692
Nicolas Pitre85221482008-08-29 16:08:01 -0400693static struct object_entry *append_obj_to_pack(struct sha1file *f,
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400694 const unsigned char *sha1, void *buf,
Nicolas Pitre636171c2006-10-25 23:28:17 -0400695 unsigned long size, enum object_type type)
696{
697 struct object_entry *obj = &objects[nr_objects++];
698 unsigned char header[10];
699 unsigned long s = size;
700 int n = 0;
701 unsigned char c = (type << 4) | (s & 15);
702 s >>= 4;
703 while (s) {
704 header[n++] = c | 0x80;
705 c = s & 0x7f;
706 s >>= 7;
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700707 }
Nicolas Pitre636171c2006-10-25 23:28:17 -0400708 header[n++] = c;
Nicolas Pitre85221482008-08-29 16:08:01 -0400709 crc32_begin(f);
710 sha1write(f, header, n);
Björn Steinbrink72de2882008-07-24 18:32:00 +0100711 obj[0].size = size;
712 obj[0].hdr_size = n;
713 obj[0].type = type;
714 obj[0].real_type = type;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400715 obj[1].idx.offset = obj[0].idx.offset + n;
Nicolas Pitre85221482008-08-29 16:08:01 -0400716 obj[1].idx.offset += write_compressed(f, buf, size);
717 obj[0].idx.crc32 = crc32_end(f);
Nicolas Pitre838cd342008-10-09 22:08:51 -0400718 sha1flush(f);
Geert Boschaa7e44b2007-06-01 15:18:05 -0400719 hashcpy(obj->idx.sha1, sha1);
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400720 return obj;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400721}
722
723static int delta_pos_compare(const void *_a, const void *_b)
724{
725 struct delta_entry *a = *(struct delta_entry **)_a;
726 struct delta_entry *b = *(struct delta_entry **)_b;
727 return a->obj_no - b->obj_no;
728}
729
Nicolas Pitre85221482008-08-29 16:08:01 -0400730static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400731{
732 struct delta_entry **sorted_by_pos;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400733 int i, n = 0;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400734
735 /*
736 * Since many unresolved deltas may well be themselves base objects
737 * for more unresolved deltas, we really want to include the
738 * smallest number of base objects that would cover as much delta
739 * as possible by picking the
740 * trunc deltas first, allowing for other deltas to resolve without
741 * additional base objects. Since most base objects are to be found
742 * before deltas depending on them, a good heuristic is to start
743 * resolving deltas in the same order as their position in the pack.
744 */
745 sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
746 for (i = 0; i < nr_deltas; i++) {
747 if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
748 continue;
749 sorted_by_pos[n++] = &deltas[i];
750 }
751 qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
752
753 for (i = 0; i < n; i++) {
754 struct delta_entry *d = sorted_by_pos[i];
Nicolas Pitre21666f12007-02-26 14:55:59 -0500755 enum object_type type;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400756 struct base_data base_obj;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400757
758 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
759 continue;
Shawn O. Pearcef41aebd2008-07-13 22:07:44 -0400760 base_obj.data = read_sha1_file(d->base.sha1, &type, &base_obj.size);
761 if (!base_obj.data)
Nicolas Pitre636171c2006-10-25 23:28:17 -0400762 continue;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400763
Shawn O. Pearce03993e12008-07-13 22:07:46 -0400764 if (check_sha1_signature(d->base.sha1, base_obj.data,
765 base_obj.size, typename(type)))
766 die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
Nicolas Pitre85221482008-08-29 16:08:01 -0400767 base_obj.obj = append_obj_to_pack(f, d->base.sha1,
768 base_obj.data, base_obj.size, type);
Nicolas Pitre9441b612008-10-17 15:57:57 -0400769 find_unresolved_deltas(&base_obj, NULL);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400770 display_progress(progress, nr_resolved_deltas);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400771 }
772 free(sorted_by_pos);
773}
774
Nicolas Pitree42797f2006-10-23 14:50:18 -0400775static void final(const char *final_pack_name, const char *curr_pack_name,
776 const char *final_index_name, const char *curr_index_name,
Shawn Pearceb8077702006-10-29 04:41:59 -0500777 const char *keep_name, const char *keep_msg,
Nicolas Pitree42797f2006-10-23 14:50:18 -0400778 unsigned char *sha1)
779{
Shawn O. Pearce3a556022007-03-06 20:44:17 -0500780 const char *report = "pack";
Nicolas Pitree42797f2006-10-23 14:50:18 -0400781 char name[PATH_MAX];
782 int err;
783
784 if (!from_stdin) {
785 close(input_fd);
786 } else {
Linus Torvalds4c81b032008-05-30 08:42:16 -0700787 fsync_or_die(output_fd, curr_pack_name);
Nicolas Pitree42797f2006-10-23 14:50:18 -0400788 err = close(output_fd);
789 if (err)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200790 die_errno("error while closing pack file");
Nicolas Pitree42797f2006-10-23 14:50:18 -0400791 }
792
Shawn Pearceb8077702006-10-29 04:41:59 -0500793 if (keep_msg) {
794 int keep_fd, keep_msg_len = strlen(keep_msg);
Junio C Hamano6e180cd2009-02-24 23:11:29 -0800795
796 if (!keep_name)
797 keep_fd = odb_pack_keep(name, sizeof(name), sha1);
798 else
799 keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
800
Nicolas Pitre9ca4a202006-11-01 17:06:24 -0500801 if (keep_fd < 0) {
802 if (errno != EEXIST)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200803 die_errno("cannot write keep file '%s'",
804 keep_name);
Nicolas Pitre9ca4a202006-11-01 17:06:24 -0500805 } 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)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200811 die_errno("cannot close written keep file '%s'",
812 keep_name);
Nicolas Pitre576162a2006-11-01 17:06:25 -0500813 report = "keep";
Shawn Pearceb8077702006-10-29 04:41:59 -0500814 }
Shawn Pearceb8077702006-10-29 04:41:59 -0500815 }
816
Nicolas Pitree42797f2006-10-23 14:50:18 -0400817 if (final_pack_name != curr_pack_name) {
818 if (!final_pack_name) {
819 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
820 get_object_directory(), sha1_to_hex(sha1));
821 final_pack_name = name;
822 }
823 if (move_temp_to_file(curr_pack_name, final_pack_name))
824 die("cannot store pack file");
Johan Herlandfb8b1932009-03-26 16:16:47 +0100825 } else if (from_stdin)
Petr Baudis33b65032008-10-03 12:20:43 +0200826 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");
Johan Herlandfb8b1932009-03-26 16:16:47 +0100836 } else
837 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
Linus Torvalds3bb72562010-01-22 07:55:19 -0800873int cmd_index_pack(int argc, const char **argv, const char *prefix)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700874{
Nicolas Pitre636171c2006-10-25 23:28:17 -0400875 int i, fix_thin_pack = 0;
Linus Torvalds3bb72562010-01-22 07:55:19 -0800876 const char *curr_pack, *curr_index;
877 const char *index_name = NULL, *pack_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
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600883 if (argc == 2 && !strcmp(argv[1], "-h"))
884 usage(index_pack_usage);
885
Nicolas Pitrea672ea62008-10-20 21:17:07 -0400886 /*
887 * We wish to read the repository's config file if any, and
888 * for that it is necessary to call setup_git_directory_gently().
889 * However if the cwd was inside .git/objects/pack/ then we need
890 * to go back there or all the pack name arguments will be wrong.
891 * And in that case we cannot rely on any prefix returned by
892 * setup_git_directory_gently() either.
893 */
894 {
895 char cwd[PATH_MAX+1];
896 int nongit;
897
898 if (!getcwd(cwd, sizeof(cwd)-1))
899 die("Unable to get current working directory");
900 setup_git_directory_gently(&nongit);
901 git_config(git_index_pack_config, NULL);
902 if (chdir(cwd))
903 die("Cannot come back to cwd");
904 }
Nicolas Pitre4d00bda2007-11-01 23:26:04 -0400905
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700906 for (i = 1; i < argc; i++) {
Linus Torvalds3bb72562010-01-22 07:55:19 -0800907 const char *arg = argv[i];
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700908
909 if (*arg == '-') {
Nicolas Pitree42797f2006-10-23 14:50:18 -0400910 if (!strcmp(arg, "--stdin")) {
911 from_stdin = 1;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400912 } else if (!strcmp(arg, "--fix-thin")) {
913 fix_thin_pack = 1;
Martin Koegler0153be02008-02-25 22:46:12 +0100914 } else if (!strcmp(arg, "--strict")) {
915 strict = 1;
Shawn Pearceb8077702006-10-29 04:41:59 -0500916 } else if (!strcmp(arg, "--keep")) {
917 keep_msg = "";
Junio C Hamanocc44c762007-02-20 01:53:29 -0800918 } else if (!prefixcmp(arg, "--keep=")) {
Shawn Pearceb8077702006-10-29 04:41:59 -0500919 keep_msg = arg + 7;
Junio C Hamanocc44c762007-02-20 01:53:29 -0800920 } else if (!prefixcmp(arg, "--pack_header=")) {
Nicolas Pitrebed006f2006-11-01 17:06:20 -0500921 struct pack_header *hdr;
922 char *c;
923
924 hdr = (struct pack_header *)input_buffer;
925 hdr->hdr_signature = htonl(PACK_SIGNATURE);
926 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
927 if (*c != ',')
928 die("bad %s", arg);
929 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
930 if (*c)
931 die("bad %s", arg);
932 input_len = sizeof(*hdr);
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400933 } else if (!strcmp(arg, "-v")) {
934 verbose = 1;
Nicolas Pitree42797f2006-10-23 14:50:18 -0400935 } else if (!strcmp(arg, "-o")) {
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700936 if (index_name || (i+1) >= argc)
937 usage(index_pack_usage);
938 index_name = argv[++i];
Nicolas Pitre4ba7d712007-04-09 17:32:03 -0400939 } else if (!prefixcmp(arg, "--index-version=")) {
940 char *c;
Geert Boschaa7e44b2007-06-01 15:18:05 -0400941 pack_idx_default_version = strtoul(arg + 16, &c, 10);
942 if (pack_idx_default_version > 2)
Nicolas Pitre4ba7d712007-04-09 17:32:03 -0400943 die("bad %s", arg);
944 if (*c == ',')
Geert Boschaa7e44b2007-06-01 15:18:05 -0400945 pack_idx_off32_limit = strtoul(c+1, &c, 0);
946 if (*c || pack_idx_off32_limit & 0x80000000)
Nicolas Pitre4ba7d712007-04-09 17:32:03 -0400947 die("bad %s", arg);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700948 } else
949 usage(index_pack_usage);
950 continue;
951 }
952
953 if (pack_name)
954 usage(index_pack_usage);
955 pack_name = arg;
956 }
957
Nicolas Pitree42797f2006-10-23 14:50:18 -0400958 if (!pack_name && !from_stdin)
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700959 usage(index_pack_usage);
Nicolas Pitre636171c2006-10-25 23:28:17 -0400960 if (fix_thin_pack && !from_stdin)
961 die("--fix-thin cannot be used without --stdin");
Nicolas Pitree42797f2006-10-23 14:50:18 -0400962 if (!index_name && pack_name) {
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700963 int len = strlen(pack_name);
Rene Scharfe5bb1cda2006-08-11 14:01:45 +0200964 if (!has_extension(pack_name, ".pack"))
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700965 die("packfile name '%s' does not end with '.pack'",
966 pack_name);
Pavel Roskin6689f082005-12-21 15:35:48 -0500967 index_name_buf = xmalloc(len);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700968 memcpy(index_name_buf, pack_name, len - 5);
969 strcpy(index_name_buf + len - 5, ".idx");
970 index_name = index_name_buf;
971 }
Shawn Pearceb8077702006-10-29 04:41:59 -0500972 if (keep_msg && !keep_name && pack_name) {
973 int len = strlen(pack_name);
974 if (!has_extension(pack_name, ".pack"))
975 die("packfile name '%s' does not end with '.pack'",
976 pack_name);
977 keep_name_buf = xmalloc(len);
978 memcpy(keep_name_buf, pack_name, len - 5);
979 strcpy(keep_name_buf + len - 5, ".keep");
980 keep_name = keep_name_buf;
981 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700982
Nicolas Pitree42797f2006-10-23 14:50:18 -0400983 curr_pack = open_pack_file(pack_name);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -0700984 parse_pack_header();
Nicolas Pitre636171c2006-10-25 23:28:17 -0400985 objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
986 deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
Nicolas Pitre85221482008-08-29 16:08:01 -0400987 parse_pack_objects(pack_sha1);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400988 if (nr_deltas == nr_resolved_deltas) {
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400989 stop_progress(&progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400990 /* Flush remaining pack final 20-byte SHA1. */
991 flush();
992 } else {
Nicolas Pitre636171c2006-10-25 23:28:17 -0400993 if (fix_thin_pack) {
Nicolas Pitre85221482008-08-29 16:08:01 -0400994 struct sha1file *f;
995 unsigned char read_sha1[20], tail_sha1[20];
Nicolas Pitrea984a062007-11-08 15:45:41 -0500996 char msg[48];
Nicolas Pitre636171c2006-10-25 23:28:17 -0400997 int nr_unresolved = nr_deltas - nr_resolved_deltas;
Nicolas Pitre3c9af362006-10-25 23:32:59 -0400998 int nr_objects_initial = nr_objects;
Nicolas Pitre636171c2006-10-25 23:28:17 -0400999 if (nr_unresolved <= 0)
1000 die("confusion beyond insanity");
1001 objects = xrealloc(objects,
1002 (nr_objects + nr_unresolved + 1)
1003 * sizeof(*objects));
Nicolas Pitre85221482008-08-29 16:08:01 -04001004 f = sha1fd(output_fd, curr_pack);
1005 fix_unresolved_deltas(f, nr_unresolved);
Nicolas Pitrea984a062007-11-08 15:45:41 -05001006 sprintf(msg, "completed with %d local objects",
1007 nr_objects - nr_objects_initial);
1008 stop_progress_msg(&progress, msg);
Nicolas Pitre85221482008-08-29 16:08:01 -04001009 sha1close(f, tail_sha1, 0);
1010 hashcpy(read_sha1, pack_sha1);
1011 fixup_pack_header_footer(output_fd, pack_sha1,
Nicolas Pitreabeb40e2008-08-29 16:07:59 -04001012 curr_pack, nr_objects,
Nicolas Pitre85221482008-08-29 16:08:01 -04001013 read_sha1, consumed_bytes-20);
1014 if (hashcmp(read_sha1, tail_sha1) != 0)
1015 die("Unexpected tail checksum for %s "
1016 "(disk corruption?)", curr_pack);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001017 }
1018 if (nr_deltas != nr_resolved_deltas)
1019 die("pack has %d unresolved deltas",
1020 nr_deltas - nr_resolved_deltas);
Nicolas Pitre636171c2006-10-25 23:28:17 -04001021 }
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001022 free(deltas);
Martin Koegler0153be02008-02-25 22:46:12 +01001023 if (strict)
1024 check_objects();
Geert Boschaa7e44b2007-06-01 15:18:05 -04001025
1026 idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
1027 for (i = 0; i < nr_objects; i++)
1028 idx_objects[i] = &objects[i].idx;
Nicolas Pitre85221482008-08-29 16:08:01 -04001029 curr_index = write_idx_file(index_name, idx_objects, nr_objects, pack_sha1);
Geert Boschaa7e44b2007-06-01 15:18:05 -04001030 free(idx_objects);
1031
Shawn Pearceb8077702006-10-29 04:41:59 -05001032 final(pack_name, curr_pack,
1033 index_name, curr_index,
1034 keep_name, keep_msg,
Nicolas Pitre85221482008-08-29 16:08:01 -04001035 pack_sha1);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001036 free(objects);
1037 free(index_name_buf);
Shawn Pearceb8077702006-10-29 04:41:59 -05001038 free(keep_name_buf);
Nicolas Pitrec85228e2007-10-16 21:55:50 -04001039 if (pack_name == NULL)
Linus Torvalds3bb72562010-01-22 07:55:19 -08001040 free((void *) curr_pack);
Nicolas Pitrec85228e2007-10-16 21:55:50 -04001041 if (index_name == NULL)
Linus Torvalds3bb72562010-01-22 07:55:19 -08001042 free((void *) curr_index);
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001043
Sergey Vlasov9cf6d332005-10-12 12:01:31 -07001044 return 0;
1045}