blob: 3824ee33acb2f984419ab79c2f3ecc306d3e5ea5 [file] [log] [blame]
Matthias Kestenholz5d4a6002006-08-03 17:24:36 +02001#include "builtin.h"
Linus Torvaldsc323ac72005-06-25 14:42:43 -07002#include "cache.h"
3#include "object.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02004#include "blob.h"
5#include "commit.h"
6#include "tag.h"
7#include "tree.h"
Linus Torvaldsc323ac72005-06-25 14:42:43 -07008#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -07009#include "pack.h"
Linus Torvaldsc38138c2005-06-26 20:27:56 -070010#include "csum-file.h"
Junio C Hamano1b0c7172006-03-29 22:55:43 -080011#include "tree-walk.h"
Junio C Hamanob5d97e62006-09-04 23:47:39 -070012#include "diff.h"
13#include "revision.h"
14#include "list-objects.h"
Linus Torvaldsc323ac72005-06-25 14:42:43 -070015
Nicolas Pitre231f2402006-11-07 10:51:23 -050016static const char pack_usage[] = "\
17git-pack-objects [{ -q | --progress | --all-progress }] \n\
18 [--local] [--incremental] [--window=N] [--depth=N] \n\
19 [--no-reuse-delta] [--delta-base-offset] [--non-empty] \n\
Junio C Hamano63049292006-12-18 17:25:28 -080020 [--revs [--unpacked | --all]*] [--reflog] [--stdout | base-name] \n\
Nicolas Pitre231f2402006-11-07 10:51:23 -050021 [<ref-list | <object-list]";
Linus Torvaldsc323ac72005-06-25 14:42:43 -070022
Linus Torvaldsc323ac72005-06-25 14:42:43 -070023struct object_entry {
24 unsigned char sha1[20];
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -080025 unsigned long size; /* uncompressed size */
Junio C Hamano15b4d572006-02-17 20:58:45 -080026 unsigned long offset; /* offset into the final pack file;
27 * nonzero if already written.
28 */
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -080029 unsigned int depth; /* delta depth */
Junio C Hamano15b4d572006-02-17 20:58:45 -080030 unsigned int delta_limit; /* base adjustment for in-pack delta */
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -080031 unsigned int hash; /* name hint hash */
Linus Torvaldsa733cb62005-06-28 14:21:02 -070032 enum object_type type;
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -080033 enum object_type in_pack_type; /* could be delta */
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -080034 unsigned long delta_size; /* delta data size (uncompressed) */
Nicolas Pitre780e6e72006-09-22 21:25:04 -040035#define in_pack_header_size delta_size /* only when reusing pack data */
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -080036 struct object_entry *delta; /* delta base object */
37 struct packed_git *in_pack; /* already in pack */
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -080038 unsigned int in_pack_offset;
Pavel Roskin82e5a822006-07-10 01:50:18 -040039 struct object_entry *delta_child; /* deltified objects who bases me */
Junio C Hamano15b4d572006-02-17 20:58:45 -080040 struct object_entry *delta_sibling; /* other deltified objects who
41 * uses the same base as me
42 */
Junio C Hamano7a979d92006-02-19 14:47:21 -080043 int preferred_base; /* we do not pack this, but is encouraged to
44 * be used as the base objectto delta huge
45 * objects against.
46 */
Linus Torvaldsc323ac72005-06-25 14:42:43 -070047};
48
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -080049/*
Pavel Roskin82e5a822006-07-10 01:50:18 -040050 * Objects we are going to pack are collected in objects array (dynamically
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -080051 * expanded). nr_objects & nr_alloc controls this array. They are stored
52 * in the order we see -- typically rev-list --objects order that gives us
53 * nice "minimum seek" order.
54 *
55 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
56 * elements in the objects array. The former is used to build the pack
57 * index (lists object names in the ascending order to help offset lookup),
58 * and the latter is used to group similar things together by try_delta()
59 * heuristics.
60 */
61
Linus Torvalds5f3de582005-07-03 15:34:04 -070062static unsigned char object_list_sha1[20];
David Rientjes96f1e582006-08-15 10:23:48 -070063static int non_empty;
64static int no_reuse_delta;
65static int local;
66static int incremental;
Nicolas Pitrebe6b1912006-09-21 00:09:44 -040067static int allow_ofs_delta;
68
Linus Torvaldsc323ac72005-06-25 14:42:43 -070069static struct object_entry **sorted_by_sha, **sorted_by_type;
David Rientjes96f1e582006-08-15 10:23:48 -070070static struct object_entry *objects;
71static int nr_objects, nr_alloc, nr_result;
Linus Torvaldsc323ac72005-06-25 14:42:43 -070072static const char *base_name;
Linus Torvaldse1808842005-06-26 22:01:46 -070073static unsigned char pack_file_sha1[20];
Junio C Hamano024701f2006-02-12 13:01:54 -080074static int progress = 1;
David Rientjes96f1e582006-08-15 10:23:48 -070075static volatile sig_atomic_t progress_update;
Jeff King4812a932006-07-23 01:50:30 -040076static int window = 10;
Junio C Hamanodf6d6102006-09-01 15:05:12 -070077static int pack_to_stdout;
Junio C Hamano8d1d8f82006-09-06 01:42:23 -070078static int num_preferred_base;
Linus Torvaldsc323ac72005-06-25 14:42:43 -070079
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -080080/*
81 * The object names in objects array are hashed with this hashtable,
82 * to help looking up the entry by object name. Binary search from
83 * sorted_by_sha is also possible but this was easier to code and faster.
84 * This hashtable is built after all the objects are seen.
85 */
David Rientjes96f1e582006-08-15 10:23:48 -070086static int *object_ix;
87static int object_ix_hashsz;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -080088
89/*
90 * Pack index for existing packs give us easy access to the offsets into
91 * corresponding pack file where each object's data starts, but the entries
92 * do not store the size of the compressed representation (uncompressed
Nicolas Pitre780e6e72006-09-22 21:25:04 -040093 * size is easily available by examining the pack entry header). It is
94 * also rather expensive to find the sha1 for an object given its offset.
95 *
96 * We build a hashtable of existing packs (pack_revindex), and keep reverse
97 * index here -- pack index file is sorted by object name mapping to offset;
98 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
99 * ordered by offset, so if you know the offset of an object, next offset
100 * is where its packed representation ends and the index_nr can be used to
101 * get the object sha1 from the main index.
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800102 */
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400103struct revindex_entry {
104 unsigned int offset;
105 unsigned int nr;
106};
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800107struct pack_revindex {
108 struct packed_git *p;
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400109 struct revindex_entry *revindex;
110};
111static struct pack_revindex *pack_revindex;
David Rientjes96f1e582006-08-15 10:23:48 -0700112static int pack_revindex_hashsz;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800113
114/*
115 * stats
116 */
David Rientjes96f1e582006-08-15 10:23:48 -0700117static int written;
118static int written_delta;
119static int reused;
120static int reused_delta;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800121
122static int pack_revindex_ix(struct packed_git *p)
123{
Luck, Tony2b74cff2006-03-01 15:01:53 -0800124 unsigned long ui = (unsigned long)p;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800125 int i;
126
127 ui = ui ^ (ui >> 16); /* defeat structure alignment */
128 i = (int)(ui % pack_revindex_hashsz);
129 while (pack_revindex[i].p) {
130 if (pack_revindex[i].p == p)
131 return i;
132 if (++i == pack_revindex_hashsz)
133 i = 0;
134 }
135 return -1 - i;
136}
137
138static void prepare_pack_ix(void)
139{
140 int num;
141 struct packed_git *p;
142 for (num = 0, p = packed_git; p; p = p->next)
143 num++;
144 if (!num)
145 return;
146 pack_revindex_hashsz = num * 11;
147 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
148 for (p = packed_git; p; p = p->next) {
149 num = pack_revindex_ix(p);
150 num = - 1 - num;
151 pack_revindex[num].p = p;
152 }
153 /* revindex elements are lazily initialized */
154}
155
156static int cmp_offset(const void *a_, const void *b_)
157{
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400158 const struct revindex_entry *a = a_;
159 const struct revindex_entry *b = b_;
160 return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800161}
162
163/*
164 * Ordered list of offsets of objects in the pack.
165 */
166static void prepare_pack_revindex(struct pack_revindex *rix)
167{
168 struct packed_git *p = rix->p;
169 int num_ent = num_packed_objects(p);
170 int i;
171 void *index = p->index_base + 256;
172
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400173 rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800174 for (i = 0; i < num_ent; i++) {
Florian Forster1d7f1712006-06-18 17:18:09 +0200175 unsigned int hl = *((unsigned int *)((char *) index + 24*i));
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400176 rix->revindex[i].offset = ntohl(hl);
177 rix->revindex[i].nr = i;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800178 }
179 /* This knows the pack format -- the 20-byte trailer
180 * follows immediately after the last object data.
181 */
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400182 rix->revindex[num_ent].offset = p->pack_size - 20;
183 rix->revindex[num_ent].nr = -1;
184 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800185}
186
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400187static struct revindex_entry * find_packed_object(struct packed_git *p,
188 unsigned int ofs)
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800189{
190 int num;
191 int lo, hi;
192 struct pack_revindex *rix;
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400193 struct revindex_entry *revindex;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800194 num = pack_revindex_ix(p);
195 if (num < 0)
196 die("internal error: pack revindex uninitialized");
197 rix = &pack_revindex[num];
198 if (!rix->revindex)
199 prepare_pack_revindex(rix);
200 revindex = rix->revindex;
201 lo = 0;
202 hi = num_packed_objects(p) + 1;
203 do {
204 int mi = (lo + hi) / 2;
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400205 if (revindex[mi].offset == ofs) {
206 return revindex + mi;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800207 }
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400208 else if (ofs < revindex[mi].offset)
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800209 hi = mi;
210 else
211 lo = mi + 1;
212 } while (lo < hi);
213 die("internal error: pack revindex corrupt");
214}
215
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400216static unsigned long find_packed_object_size(struct packed_git *p,
217 unsigned long ofs)
218{
219 struct revindex_entry *entry = find_packed_object(p, ofs);
220 return entry[1].offset - ofs;
221}
222
223static unsigned char *find_packed_object_name(struct packed_git *p,
224 unsigned long ofs)
225{
226 struct revindex_entry *entry = find_packed_object(p, ofs);
227 return (unsigned char *)(p->index_base + 256) + 24 * entry->nr + 4;
228}
229
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700230static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
231{
232 unsigned long othersize, delta_size;
233 char type[10];
234 void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
235 void *delta_buf;
236
237 if (!otherbuf)
238 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700239 delta_buf = diff_delta(otherbuf, othersize,
Nicolas Pitredcde55b2005-06-29 02:49:56 -0400240 buf, size, &delta_size, 0);
Linus Torvalds75c42d82005-06-25 19:30:20 -0700241 if (!delta_buf || delta_size != entry->delta_size)
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700242 die("delta size changed");
243 free(buf);
244 free(otherbuf);
245 return delta_buf;
246}
247
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700248/*
249 * The per-object header is a pretty dense thing, which is
250 * - first byte: low four bits are "size", then three bits of "type",
251 * and the high bit is "size continues".
252 * - each byte afterwards: low seven bits are size continuation,
253 * with the high bit being "size continues"
254 */
255static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
256{
Linus Torvalds01247d82005-06-28 22:15:57 -0700257 int n = 1;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700258 unsigned char c;
259
Nicolas Pitreeb32d232006-09-21 00:06:49 -0400260 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700261 die("bad type %d", type);
262
Linus Torvalds01247d82005-06-28 22:15:57 -0700263 c = (type << 4) | (size & 15);
264 size >>= 4;
265 while (size) {
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700266 *hdr++ = c | 0x80;
Linus Torvalds01247d82005-06-28 22:15:57 -0700267 c = size & 0x7f;
268 size >>= 7;
269 n++;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700270 }
271 *hdr = c;
272 return n;
273}
274
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400275/*
276 * we are going to reuse the existing object data as is. make
277 * sure it is not corrupt.
278 */
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500279static int check_pack_inflate(struct packed_git *p,
280 struct pack_window **w_curs,
281 unsigned long offset,
282 unsigned long len,
283 unsigned long expect)
284{
285 z_stream stream;
286 unsigned char fakebuf[4096], *in;
287 int st;
288
289 memset(&stream, 0, sizeof(stream));
290 inflateInit(&stream);
291 do {
292 in = use_pack(p, w_curs, offset, &stream.avail_in);
293 stream.next_in = in;
294 stream.next_out = fakebuf;
295 stream.avail_out = sizeof(fakebuf);
296 st = inflate(&stream, Z_FINISH);
297 offset += stream.next_in - in;
298 } while (st == Z_OK || st == Z_BUF_ERROR);
299 inflateEnd(&stream);
300 return (st == Z_STREAM_END &&
301 stream.total_out == expect &&
302 stream.total_in == len) ? 0 : -1;
303}
304
305static void copy_pack_data(struct sha1file *f,
306 struct packed_git *p,
307 struct pack_window **w_curs,
308 unsigned long offset,
309 unsigned long len)
310{
311 unsigned char *in;
312 unsigned int avail;
313
314 while (len) {
315 in = use_pack(p, w_curs, offset, &avail);
316 if (avail > len)
317 avail = len;
318 sha1write(f, in, avail);
319 offset += avail;
320 len -= avail;
321 }
322}
323
324static int check_loose_inflate(unsigned char *data, unsigned long len, unsigned long expect)
Junio C Hamanodf6d6102006-09-01 15:05:12 -0700325{
Junio C Hamano72518e92006-09-03 21:09:18 -0700326 z_stream stream;
327 unsigned char fakebuf[4096];
328 int st;
329
330 memset(&stream, 0, sizeof(stream));
331 stream.next_in = data;
332 stream.avail_in = len;
333 stream.next_out = fakebuf;
334 stream.avail_out = sizeof(fakebuf);
335 inflateInit(&stream);
336
337 while (1) {
338 st = inflate(&stream, Z_FINISH);
339 if (st == Z_STREAM_END || st == Z_OK) {
340 st = (stream.total_out == expect &&
341 stream.total_in == len) ? 0 : -1;
342 break;
343 }
344 if (st != Z_BUF_ERROR) {
345 st = -1;
346 break;
347 }
348 stream.next_out = fakebuf;
349 stream.avail_out = sizeof(fakebuf);
350 }
351 inflateEnd(&stream);
352 return st;
Junio C Hamanodf6d6102006-09-01 15:05:12 -0700353}
354
Junio C Hamanodf6d6102006-09-01 15:05:12 -0700355static int revalidate_loose_object(struct object_entry *entry,
356 unsigned char *map,
357 unsigned long mapsize)
358{
359 /* we already know this is a loose object with new type header. */
Junio C Hamano72518e92006-09-03 21:09:18 -0700360 enum object_type type;
361 unsigned long size, used;
Junio C Hamanodf6d6102006-09-01 15:05:12 -0700362
363 if (pack_to_stdout)
364 return 0;
365
Junio C Hamano72518e92006-09-03 21:09:18 -0700366 used = unpack_object_header_gently(map, mapsize, &type, &size);
367 if (!used)
368 return -1;
369 map += used;
370 mapsize -= used;
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500371 return check_loose_inflate(map, mapsize, size);
Junio C Hamanodf6d6102006-09-01 15:05:12 -0700372}
373
Junio C Hamano7a979d92006-02-19 14:47:21 -0800374static unsigned long write_object(struct sha1file *f,
375 struct object_entry *entry)
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700376{
377 unsigned long size;
378 char type[10];
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800379 void *buf;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700380 unsigned char header[10];
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700381 unsigned hdrlen, datalen;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700382 enum object_type obj_type;
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800383 int to_reuse = 0;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700384
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700385 obj_type = entry->type;
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800386 if (! entry->in_pack)
387 to_reuse = 0; /* can't reuse what we don't have */
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400388 else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800389 to_reuse = 1; /* check_object() decided it for us */
390 else if (obj_type != entry->in_pack_type)
391 to_reuse = 0; /* pack has delta which is unusable */
392 else if (entry->delta)
393 to_reuse = 0; /* we want to pack afresh */
394 else
395 to_reuse = 1; /* we have it in-pack undeltified,
396 * and we do not need to deltify it.
397 */
398
Junio C Hamanoceec1362006-07-17 15:06:23 -0700399 if (!entry->in_pack && !entry->delta) {
400 unsigned char *map;
401 unsigned long mapsize;
402 map = map_sha1_file(entry->sha1, &mapsize);
403 if (map && !legacy_loose_object(map)) {
404 /* We can copy straight into the pack file */
Junio C Hamanodf6d6102006-09-01 15:05:12 -0700405 if (revalidate_loose_object(entry, map, mapsize))
406 die("corrupt loose object %s",
407 sha1_to_hex(entry->sha1));
Junio C Hamanoceec1362006-07-17 15:06:23 -0700408 sha1write(f, map, mapsize);
409 munmap(map, mapsize);
410 written++;
411 reused++;
412 return mapsize;
413 }
414 if (map)
415 munmap(map, mapsize);
416 }
417
Junio C Hamanodf6d6102006-09-01 15:05:12 -0700418 if (!to_reuse) {
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800419 buf = read_sha1_file(entry->sha1, type, &size);
420 if (!buf)
421 die("unable to read %s", sha1_to_hex(entry->sha1));
422 if (size != entry->size)
423 die("object %s size inconsistency (%lu vs %lu)",
424 sha1_to_hex(entry->sha1), size, entry->size);
425 if (entry->delta) {
426 buf = delta_against(buf, size, entry);
427 size = entry->delta_size;
Nicolas Pitrebe6b1912006-09-21 00:09:44 -0400428 obj_type = (allow_ofs_delta && entry->delta->offset) ?
429 OBJ_OFS_DELTA : OBJ_REF_DELTA;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800430 }
431 /*
432 * The object header is a byte of 'type' followed by zero or
Nicolas Pitrebe6b1912006-09-21 00:09:44 -0400433 * more bytes of length.
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800434 */
435 hdrlen = encode_header(obj_type, size, header);
436 sha1write(f, header, hdrlen);
437
Nicolas Pitrebe6b1912006-09-21 00:09:44 -0400438 if (obj_type == OBJ_OFS_DELTA) {
439 /*
440 * Deltas with relative base contain an additional
441 * encoding of the relative offset for the delta
442 * base from this object's position in the pack.
443 */
444 unsigned long ofs = entry->offset - entry->delta->offset;
445 unsigned pos = sizeof(header) - 1;
446 header[pos] = ofs & 127;
447 while (ofs >>= 7)
448 header[--pos] = 128 | (--ofs & 127);
449 sha1write(f, header + pos, sizeof(header) - pos);
450 hdrlen += sizeof(header) - pos;
451 } else if (obj_type == OBJ_REF_DELTA) {
452 /*
453 * Deltas with a base reference contain
454 * an additional 20 bytes for the base sha1.
455 */
456 sha1write(f, entry->delta->sha1, 20);
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800457 hdrlen += 20;
458 }
459 datalen = sha1write_compressed(f, buf, size);
460 free(buf);
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700461 }
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800462 else {
463 struct packed_git *p = entry->in_pack;
Shawn O. Pearce03e79c82006-12-23 02:34:08 -0500464 struct pack_window *w_curs = NULL;
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500465 unsigned long offset;
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400466
467 if (entry->delta) {
468 obj_type = (allow_ofs_delta && entry->delta->offset) ?
469 OBJ_OFS_DELTA : OBJ_REF_DELTA;
470 reused_delta++;
471 }
472 hdrlen = encode_header(obj_type, entry->size, header);
473 sha1write(f, header, hdrlen);
474 if (obj_type == OBJ_OFS_DELTA) {
475 unsigned long ofs = entry->offset - entry->delta->offset;
476 unsigned pos = sizeof(header) - 1;
477 header[pos] = ofs & 127;
478 while (ofs >>= 7)
479 header[--pos] = 128 | (--ofs & 127);
480 sha1write(f, header + pos, sizeof(header) - pos);
481 hdrlen += sizeof(header) - pos;
482 } else if (obj_type == OBJ_REF_DELTA) {
483 sha1write(f, entry->delta->sha1, 20);
484 hdrlen += 20;
485 }
486
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500487 offset = entry->in_pack_offset + entry->in_pack_header_size;
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400488 datalen = find_packed_object_size(p, entry->in_pack_offset)
489 - entry->in_pack_header_size;
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500490 if (!pack_to_stdout && check_pack_inflate(p, &w_curs,
491 offset, datalen, entry->size))
Junio C Hamanodf6d6102006-09-01 15:05:12 -0700492 die("corrupt delta in pack %s", sha1_to_hex(entry->sha1));
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500493 copy_pack_data(f, p, &w_curs, offset, datalen);
Shawn O. Pearce03e79c82006-12-23 02:34:08 -0500494 unuse_pack(&w_curs);
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800495 reused++;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700496 }
Nicolas Pitrebe6b1912006-09-21 00:09:44 -0400497 if (entry->delta)
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800498 written_delta++;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800499 written++;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700500 return hdrlen + datalen;
501}
502
Junio C Hamano9d5ab962005-06-28 17:49:27 -0700503static unsigned long write_one(struct sha1file *f,
504 struct object_entry *e,
505 unsigned long offset)
506{
Nicolas Pitrebe6b1912006-09-21 00:09:44 -0400507 if (e->offset || e->preferred_base)
Junio C Hamano9d5ab962005-06-28 17:49:27 -0700508 /* offset starts from header size and cannot be zero
509 * if it is written already.
510 */
511 return offset;
Nicolas Pitrebe6b1912006-09-21 00:09:44 -0400512 /* if we are deltified, write out its base object first. */
Junio C Hamano9d5ab962005-06-28 17:49:27 -0700513 if (e->delta)
514 offset = write_one(f, e->delta, offset);
Nicolas Pitrebe6b1912006-09-21 00:09:44 -0400515 e->offset = offset;
516 return offset + write_object(f, e);
Junio C Hamano9d5ab962005-06-28 17:49:27 -0700517}
518
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700519static void write_pack_file(void)
520{
521 int i;
Linus Torvaldsd22b9292005-06-28 11:10:48 -0700522 struct sha1file *f;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700523 unsigned long offset;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700524 struct pack_header hdr;
Junio C Hamano183bdb22006-02-22 16:02:59 -0800525 unsigned last_percent = 999;
Nicolas Pitrefa438a22006-10-31 16:58:32 -0500526 int do_progress = progress;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700527
Nicolas Pitrefa438a22006-10-31 16:58:32 -0500528 if (!base_name) {
Linus Torvaldsd22b9292005-06-28 11:10:48 -0700529 f = sha1fd(1, "<stdout>");
Nicolas Pitrefa438a22006-10-31 16:58:32 -0500530 do_progress >>= 1;
531 }
532 else
Junio C Hamano183bdb22006-02-22 16:02:59 -0800533 f = sha1create("%s-%s.%s", base_name,
534 sha1_to_hex(object_list_sha1), "pack");
Junio C Hamano183bdb22006-02-22 16:02:59 -0800535 if (do_progress)
Junio C Hamanof0b0af12006-02-24 21:55:23 -0800536 fprintf(stderr, "Writing %d objects.\n", nr_result);
Junio C Hamano183bdb22006-02-22 16:02:59 -0800537
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700538 hdr.hdr_signature = htonl(PACK_SIGNATURE);
Linus Torvalds01247d82005-06-28 22:15:57 -0700539 hdr.hdr_version = htonl(PACK_VERSION);
Junio C Hamano7a979d92006-02-19 14:47:21 -0800540 hdr.hdr_entries = htonl(nr_result);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700541 sha1write(f, &hdr, sizeof(hdr));
542 offset = sizeof(hdr);
Junio C Hamanof0b0af12006-02-24 21:55:23 -0800543 if (!nr_result)
544 goto done;
Nicolas Pitre5e8dc752006-02-22 17:41:32 -0500545 for (i = 0; i < nr_objects; i++) {
Junio C Hamano9d5ab962005-06-28 17:49:27 -0700546 offset = write_one(f, objects + i, offset);
Junio C Hamano183bdb22006-02-22 16:02:59 -0800547 if (do_progress) {
Junio C Hamanof0b0af12006-02-24 21:55:23 -0800548 unsigned percent = written * 100 / nr_result;
Junio C Hamano183bdb22006-02-22 16:02:59 -0800549 if (progress_update || percent != last_percent) {
550 fprintf(stderr, "%4u%% (%u/%u) done\r",
Junio C Hamanof0b0af12006-02-24 21:55:23 -0800551 percent, written, nr_result);
Junio C Hamano183bdb22006-02-22 16:02:59 -0800552 progress_update = 0;
553 last_percent = percent;
554 }
Nicolas Pitre5e8dc752006-02-22 17:41:32 -0500555 }
556 }
Junio C Hamano183bdb22006-02-22 16:02:59 -0800557 if (do_progress)
558 fputc('\n', stderr);
Junio C Hamanof0b0af12006-02-24 21:55:23 -0800559 done:
Nicolas Pitre67c08ce2006-11-29 17:15:48 -0500560 if (written != nr_result)
561 die("wrote %d objects while expecting %d", written, nr_result);
Linus Torvaldse1808842005-06-26 22:01:46 -0700562 sha1close(f, pack_file_sha1, 1);
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700563}
564
565static void write_index_file(void)
566{
567 int i;
Junio C Hamano7a979d92006-02-19 14:47:21 -0800568 struct sha1file *f = sha1create("%s-%s.%s", base_name,
569 sha1_to_hex(object_list_sha1), "idx");
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700570 struct object_entry **list = sorted_by_sha;
Junio C Hamano7a979d92006-02-19 14:47:21 -0800571 struct object_entry **last = list + nr_result;
Junio C Hamanob18b00a2007-01-17 23:17:28 -0800572 uint32_t array[256];
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700573
574 /*
575 * Write the first-level table (the list is sorted,
576 * but we use a 256-entry lookup to be able to avoid
Linus Torvaldse1808842005-06-26 22:01:46 -0700577 * having to do eight extra binary search iterations).
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700578 */
579 for (i = 0; i < 256; i++) {
580 struct object_entry **next = list;
581 while (next < last) {
582 struct object_entry *entry = *next;
583 if (entry->sha1[0] != i)
584 break;
585 next++;
586 }
587 array[i] = htonl(next - sorted_by_sha);
588 list = next;
589 }
Junio C Hamanob18b00a2007-01-17 23:17:28 -0800590 sha1write(f, array, 256 * 4);
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700591
592 /*
593 * Write the actual SHA1 entries..
594 */
595 list = sorted_by_sha;
Junio C Hamano7a979d92006-02-19 14:47:21 -0800596 for (i = 0; i < nr_result; i++) {
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700597 struct object_entry *entry = *list++;
Junio C Hamanob18b00a2007-01-17 23:17:28 -0800598 uint32_t offset = htonl(entry->offset);
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700599 sha1write(f, &offset, 4);
600 sha1write(f, entry->sha1, 20);
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700601 }
Linus Torvaldse1808842005-06-26 22:01:46 -0700602 sha1write(f, pack_file_sha1, 20);
603 sha1close(f, NULL, 1);
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700604}
605
Junio C Hamano7a979d92006-02-19 14:47:21 -0800606static int locate_object_entry_hash(const unsigned char *sha1)
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800607{
608 int i;
609 unsigned int ui;
610 memcpy(&ui, sha1, sizeof(unsigned int));
611 i = ui % object_ix_hashsz;
612 while (0 < object_ix[i]) {
David Rientjesa89fccd2006-08-17 11:54:57 -0700613 if (!hashcmp(sha1, objects[object_ix[i] - 1].sha1))
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800614 return i;
615 if (++i == object_ix_hashsz)
616 i = 0;
617 }
618 return -1 - i;
619}
620
Junio C Hamano7a979d92006-02-19 14:47:21 -0800621static struct object_entry *locate_object_entry(const unsigned char *sha1)
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800622{
Junio C Hamano7a979d92006-02-19 14:47:21 -0800623 int i;
624
625 if (!object_ix_hashsz)
626 return NULL;
627
628 i = locate_object_entry_hash(sha1);
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800629 if (0 <= i)
630 return &objects[object_ix[i]-1];
631 return NULL;
632}
633
Junio C Hamano7a979d92006-02-19 14:47:21 -0800634static void rehash_objects(void)
635{
636 int i;
637 struct object_entry *oe;
638
639 object_ix_hashsz = nr_objects * 3;
640 if (object_ix_hashsz < 1024)
641 object_ix_hashsz = 1024;
642 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700643 memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
Junio C Hamano7a979d92006-02-19 14:47:21 -0800644 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
645 int ix = locate_object_entry_hash(oe->sha1);
646 if (0 <= ix)
647 continue;
648 ix = -1 - ix;
649 object_ix[ix] = i + 1;
650 }
651}
652
Linus Torvaldsce0bd642006-06-05 12:03:31 -0700653static unsigned name_hash(const char *name)
Junio C Hamano7a979d92006-02-19 14:47:21 -0800654{
Linus Torvaldsce0bd642006-06-05 12:03:31 -0700655 unsigned char c;
656 unsigned hash = 0;
Junio C Hamano1d6b38c2006-02-22 22:10:24 -0800657
Junio C Hamanoeeef7132006-02-23 23:27:49 -0800658 /*
Linus Torvaldsce0bd642006-06-05 12:03:31 -0700659 * This effectively just creates a sortable number from the
660 * last sixteen non-whitespace characters. Last characters
661 * count "most", so things that end in ".c" sort together.
Junio C Hamanoeeef7132006-02-23 23:27:49 -0800662 */
Linus Torvaldsce0bd642006-06-05 12:03:31 -0700663 while ((c = *name++) != 0) {
664 if (isspace(c))
665 continue;
666 hash = (hash >> 2) + (c << 24);
667 }
Junio C Hamano1d6b38c2006-02-22 22:10:24 -0800668 return hash;
669}
670
671static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700672{
673 unsigned int idx = nr_objects;
674 struct object_entry *entry;
Junio C Hamano7a979d92006-02-19 14:47:21 -0800675 struct packed_git *p;
676 unsigned int found_offset = 0;
677 struct packed_git *found_pack = NULL;
Junio C Hamanob9254102006-02-22 21:45:45 -0800678 int ix, status = 0;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700679
Junio C Hamano7a979d92006-02-19 14:47:21 -0800680 if (!exclude) {
Linus Torvalds64560372005-10-13 15:38:28 -0700681 for (p = packed_git; p; p = p->next) {
Nicolas Pitre43057302006-09-21 00:05:37 -0400682 unsigned long offset = find_pack_entry_one(sha1, p);
683 if (offset) {
Linus Torvalds64560372005-10-13 15:38:28 -0700684 if (incremental)
685 return 0;
686 if (local && !p->pack_local)
687 return 0;
Junio C Hamano7a979d92006-02-19 14:47:21 -0800688 if (!found_pack) {
Nicolas Pitre43057302006-09-21 00:05:37 -0400689 found_offset = offset;
690 found_pack = p;
Junio C Hamano7a979d92006-02-19 14:47:21 -0800691 }
Linus Torvalds64560372005-10-13 15:38:28 -0700692 }
693 }
694 }
Junio C Hamano7a979d92006-02-19 14:47:21 -0800695 if ((entry = locate_object_entry(sha1)) != NULL)
696 goto already_added;
Linus Torvaldseb019372005-07-03 13:08:40 -0700697
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700698 if (idx >= nr_alloc) {
699 unsigned int needed = (idx + 1024) * 3 / 2;
700 objects = xrealloc(objects, needed * sizeof(*entry));
701 nr_alloc = needed;
702 }
703 entry = objects + idx;
Junio C Hamano7a979d92006-02-19 14:47:21 -0800704 nr_objects = idx + 1;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700705 memset(entry, 0, sizeof(*entry));
Shawn Pearcee7024962006-08-23 02:49:00 -0400706 hashcpy(entry->sha1, sha1);
Linus Torvalds27225f22005-06-26 15:27:28 -0700707 entry->hash = hash;
Junio C Hamano7a979d92006-02-19 14:47:21 -0800708
709 if (object_ix_hashsz * 3 <= nr_objects * 4)
710 rehash_objects();
711 else {
712 ix = locate_object_entry_hash(entry->sha1);
713 if (0 <= ix)
714 die("internal error in object hashing.");
715 object_ix[-1 - ix] = idx + 1;
716 }
Junio C Hamanob9254102006-02-22 21:45:45 -0800717 status = 1;
Junio C Hamano7a979d92006-02-19 14:47:21 -0800718
719 already_added:
Junio C Hamanof0b0af12006-02-24 21:55:23 -0800720 if (progress_update) {
721 fprintf(stderr, "Counting objects...%d\r", nr_objects);
722 progress_update = 0;
723 }
Junio C Hamano7a979d92006-02-19 14:47:21 -0800724 if (exclude)
725 entry->preferred_base = 1;
726 else {
727 if (found_pack) {
728 entry->in_pack = found_pack;
729 entry->in_pack_offset = found_offset;
730 }
731 }
Junio C Hamanob9254102006-02-22 21:45:45 -0800732 return status;
Junio C Hamano7a979d92006-02-19 14:47:21 -0800733}
734
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700735struct pbase_tree_cache {
736 unsigned char sha1[20];
737 int ref;
738 int temporary;
739 void *tree_data;
740 unsigned long tree_size;
741};
742
743static struct pbase_tree_cache *(pbase_tree_cache[256]);
744static int pbase_tree_cache_ix(const unsigned char *sha1)
745{
746 return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
747}
748static int pbase_tree_cache_ix_incr(int ix)
749{
750 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
751}
752
753static struct pbase_tree {
754 struct pbase_tree *next;
755 /* This is a phony "cache" entry; we are not
756 * going to evict it nor find it through _get()
757 * mechanism -- this is for the toplevel node that
758 * would almost always change with any commit.
759 */
760 struct pbase_tree_cache pcache;
761} *pbase_tree;
762
763static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
764{
765 struct pbase_tree_cache *ent, *nent;
766 void *data;
767 unsigned long size;
768 char type[20];
769 int neigh;
770 int my_ix = pbase_tree_cache_ix(sha1);
771 int available_ix = -1;
772
773 /* pbase-tree-cache acts as a limited hashtable.
774 * your object will be found at your index or within a few
775 * slots after that slot if it is cached.
776 */
777 for (neigh = 0; neigh < 8; neigh++) {
778 ent = pbase_tree_cache[my_ix];
David Rientjesa89fccd2006-08-17 11:54:57 -0700779 if (ent && !hashcmp(ent->sha1, sha1)) {
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700780 ent->ref++;
781 return ent;
782 }
783 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
784 ((0 <= available_ix) &&
785 (!ent && pbase_tree_cache[available_ix])))
786 available_ix = my_ix;
787 if (!ent)
788 break;
789 my_ix = pbase_tree_cache_ix_incr(my_ix);
790 }
791
792 /* Did not find one. Either we got a bogus request or
793 * we need to read and perhaps cache.
794 */
795 data = read_sha1_file(sha1, type, &size);
796 if (!data)
797 return NULL;
798 if (strcmp(type, tree_type)) {
799 free(data);
800 return NULL;
801 }
802
803 /* We need to either cache or return a throwaway copy */
804
805 if (available_ix < 0)
806 ent = NULL;
807 else {
808 ent = pbase_tree_cache[available_ix];
809 my_ix = available_ix;
810 }
811
812 if (!ent) {
813 nent = xmalloc(sizeof(*nent));
814 nent->temporary = (available_ix < 0);
815 }
816 else {
817 /* evict and reuse */
818 free(ent->tree_data);
819 nent = ent;
820 }
Shawn Pearcee7024962006-08-23 02:49:00 -0400821 hashcpy(nent->sha1, sha1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700822 nent->tree_data = data;
823 nent->tree_size = size;
824 nent->ref = 1;
825 if (!nent->temporary)
826 pbase_tree_cache[my_ix] = nent;
827 return nent;
828}
829
830static void pbase_tree_put(struct pbase_tree_cache *cache)
831{
832 if (!cache->temporary) {
833 cache->ref--;
834 return;
835 }
836 free(cache->tree_data);
837 free(cache);
838}
839
840static int name_cmp_len(const char *name)
841{
842 int i;
843 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
844 ;
845 return i;
846}
847
848static void add_pbase_object(struct tree_desc *tree,
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700849 const char *name,
Linus Torvaldsce0bd642006-06-05 12:03:31 -0700850 int cmplen,
851 const char *fullname)
Junio C Hamano7a979d92006-02-19 14:47:21 -0800852{
Linus Torvalds4c068a92006-05-30 09:45:45 -0700853 struct name_entry entry;
854
855 while (tree_entry(tree,&entry)) {
Junio C Hamano7a979d92006-02-19 14:47:21 -0800856 unsigned long size;
857 char type[20];
858
Linus Torvalds4c068a92006-05-30 09:45:45 -0700859 if (entry.pathlen != cmplen ||
860 memcmp(entry.path, name, cmplen) ||
861 !has_sha1_file(entry.sha1) ||
862 sha1_object_info(entry.sha1, type, &size))
Junio C Hamano7a979d92006-02-19 14:47:21 -0800863 continue;
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700864 if (name[cmplen] != '/') {
Linus Torvaldsce0bd642006-06-05 12:03:31 -0700865 unsigned hash = name_hash(fullname);
Linus Torvalds4c068a92006-05-30 09:45:45 -0700866 add_object_entry(entry.sha1, hash, 1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700867 return;
868 }
Peter Eriksen8e440252006-04-02 14:44:09 +0200869 if (!strcmp(type, tree_type)) {
Junio C Hamano7a979d92006-02-19 14:47:21 -0800870 struct tree_desc sub;
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700871 struct pbase_tree_cache *tree;
872 const char *down = name+cmplen+1;
873 int downlen = name_cmp_len(down);
Junio C Hamano1d6b38c2006-02-22 22:10:24 -0800874
Linus Torvalds4c068a92006-05-30 09:45:45 -0700875 tree = pbase_tree_get(entry.sha1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700876 if (!tree)
877 return;
878 sub.buf = tree->tree_data;
879 sub.size = tree->tree_size;
880
Linus Torvaldsce0bd642006-06-05 12:03:31 -0700881 add_pbase_object(&sub, down, downlen, fullname);
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700882 pbase_tree_put(tree);
883 }
884 }
885}
886
887static unsigned *done_pbase_paths;
888static int done_pbase_paths_num;
889static int done_pbase_paths_alloc;
890static int done_pbase_path_pos(unsigned hash)
891{
892 int lo = 0;
893 int hi = done_pbase_paths_num;
894 while (lo < hi) {
895 int mi = (hi + lo) / 2;
896 if (done_pbase_paths[mi] == hash)
897 return mi;
898 if (done_pbase_paths[mi] < hash)
899 hi = mi;
900 else
901 lo = mi + 1;
902 }
903 return -lo-1;
904}
905
906static int check_pbase_path(unsigned hash)
907{
908 int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
909 if (0 <= pos)
910 return 1;
911 pos = -pos - 1;
912 if (done_pbase_paths_alloc <= done_pbase_paths_num) {
913 done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
914 done_pbase_paths = xrealloc(done_pbase_paths,
915 done_pbase_paths_alloc *
916 sizeof(unsigned));
917 }
918 done_pbase_paths_num++;
919 if (pos < done_pbase_paths_num)
920 memmove(done_pbase_paths + pos + 1,
921 done_pbase_paths + pos,
922 (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
923 done_pbase_paths[pos] = hash;
924 return 0;
925}
926
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700927static void add_preferred_base_object(const char *name, unsigned hash)
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700928{
929 struct pbase_tree *it;
930 int cmplen = name_cmp_len(name);
931
932 if (check_pbase_path(hash))
933 return;
934
935 for (it = pbase_tree; it; it = it->next) {
936 if (cmplen == 0) {
Linus Torvaldsce0bd642006-06-05 12:03:31 -0700937 hash = name_hash("");
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700938 add_object_entry(it->pcache.sha1, hash, 1);
939 }
940 else {
941 struct tree_desc tree;
942 tree.buf = it->pcache.tree_data;
943 tree.size = it->pcache.tree_size;
Linus Torvaldsce0bd642006-06-05 12:03:31 -0700944 add_pbase_object(&tree, name, cmplen, name);
Junio C Hamano7a979d92006-02-19 14:47:21 -0800945 }
946 }
947}
948
949static void add_preferred_base(unsigned char *sha1)
950{
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700951 struct pbase_tree *it;
952 void *data;
953 unsigned long size;
954 unsigned char tree_sha1[20];
Junio C Hamano1d6b38c2006-02-22 22:10:24 -0800955
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700956 if (window <= num_preferred_base++)
957 return;
958
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700959 data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
960 if (!data)
Junio C Hamano7a979d92006-02-19 14:47:21 -0800961 return;
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700962
963 for (it = pbase_tree; it; it = it->next) {
David Rientjesa89fccd2006-08-17 11:54:57 -0700964 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700965 free(data);
966 return;
967 }
968 }
969
970 it = xcalloc(1, sizeof(*it));
971 it->next = pbase_tree;
972 pbase_tree = it;
973
Shawn Pearcee7024962006-08-23 02:49:00 -0400974 hashcpy(it->pcache.sha1, tree_sha1);
Junio C Hamano5379a5c2006-04-05 23:24:57 -0700975 it->pcache.tree_data = data;
976 it->pcache.tree_size = size;
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700977}
978
979static void check_object(struct object_entry *entry)
980{
Junio C Hamano36e4d742005-06-27 03:34:06 -0700981 char type[20];
Linus Torvaldsc323ac72005-06-25 14:42:43 -0700982
Junio C Hamano7a979d92006-02-19 14:47:21 -0800983 if (entry->in_pack && !entry->preferred_base) {
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400984 struct packed_git *p = entry->in_pack;
Shawn O. Pearce03e79c82006-12-23 02:34:08 -0500985 struct pack_window *w_curs = NULL;
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400986 unsigned long left = p->pack_size - entry->in_pack_offset;
987 unsigned long size, used;
988 unsigned char *buf;
989 struct object_entry *base_entry = NULL;
990
Shawn O. Pearce03e79c82006-12-23 02:34:08 -0500991 buf = use_pack(p, &w_curs, entry->in_pack_offset, NULL);
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800992
993 /* We want in_pack_type even if we do not reuse delta.
994 * There is no point not reusing non-delta representations.
995 */
Nicolas Pitre780e6e72006-09-22 21:25:04 -0400996 used = unpack_object_header_gently(buf, left,
997 &entry->in_pack_type, &size);
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -0800998
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -0800999 /* Check if it is delta, and the base is also an object
1000 * we are going to pack. If so we will reuse the existing
1001 * delta.
1002 */
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001003 if (!no_reuse_delta) {
1004 unsigned char c, *base_name;
1005 unsigned long ofs;
Junio C Hamano9c18df12006-12-28 22:29:06 -08001006 unsigned long used_0;
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001007 /* there is at least 20 bytes left in the pack */
1008 switch (entry->in_pack_type) {
1009 case OBJ_REF_DELTA:
Shawn O. Pearcef5b1b5a2006-12-27 02:46:23 -05001010 base_name = use_pack(p, &w_curs,
1011 entry->in_pack_offset + used, NULL);
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001012 used += 20;
1013 break;
1014 case OBJ_OFS_DELTA:
Shawn O. Pearcef5b1b5a2006-12-27 02:46:23 -05001015 buf = use_pack(p, &w_curs,
1016 entry->in_pack_offset + used, NULL);
Junio C Hamano9c18df12006-12-28 22:29:06 -08001017 used_0 = 0;
1018 c = buf[used_0++];
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001019 ofs = c & 127;
1020 while (c & 128) {
1021 ofs += 1;
1022 if (!ofs || ofs & ~(~0UL >> 7))
1023 die("delta base offset overflow in pack for %s",
1024 sha1_to_hex(entry->sha1));
Junio C Hamano9c18df12006-12-28 22:29:06 -08001025 c = buf[used_0++];
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001026 ofs = (ofs << 7) + (c & 127);
1027 }
1028 if (ofs >= entry->in_pack_offset)
1029 die("delta base offset out of bound for %s",
1030 sha1_to_hex(entry->sha1));
1031 ofs = entry->in_pack_offset - ofs;
1032 base_name = find_packed_object_name(p, ofs);
Junio C Hamano9c18df12006-12-28 22:29:06 -08001033 used += used_0;
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001034 break;
1035 default:
1036 base_name = NULL;
1037 }
1038 if (base_name)
1039 base_entry = locate_object_entry(base_name);
1040 }
Shawn O. Pearce03e79c82006-12-23 02:34:08 -05001041 unuse_pack(&w_curs);
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001042 entry->in_pack_header_size = used;
1043
Nicolas Pitrea2700692006-09-27 15:42:16 -04001044 if (base_entry) {
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001045
1046 /* Depth value does not matter - find_deltas()
1047 * will never consider reused delta as the
1048 * base object to deltify other objects
1049 * against, in order to avoid circular deltas.
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001050 */
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001051
1052 /* uncompressed size of the delta data */
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001053 entry->size = size;
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001054 entry->delta = base_entry;
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001055 entry->type = entry->in_pack_type;
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001056
Junio C Hamano15b4d572006-02-17 20:58:45 -08001057 entry->delta_sibling = base_entry->delta_child;
1058 base_entry->delta_child = entry;
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001059
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001060 return;
1061 }
1062 /* Otherwise we would do the usual */
Junio C Hamano36e4d742005-06-27 03:34:06 -07001063 }
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001064
1065 if (sha1_object_info(entry->sha1, type, &entry->size))
Junio C Hamano36e4d742005-06-27 03:34:06 -07001066 die("unable to get type of object %s",
1067 sha1_to_hex(entry->sha1));
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001068
Peter Eriksen8e440252006-04-02 14:44:09 +02001069 if (!strcmp(type, commit_type)) {
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001070 entry->type = OBJ_COMMIT;
Peter Eriksen8e440252006-04-02 14:44:09 +02001071 } else if (!strcmp(type, tree_type)) {
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001072 entry->type = OBJ_TREE;
Peter Eriksen8e440252006-04-02 14:44:09 +02001073 } else if (!strcmp(type, blob_type)) {
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001074 entry->type = OBJ_BLOB;
Peter Eriksen8e440252006-04-02 14:44:09 +02001075 } else if (!strcmp(type, tag_type)) {
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001076 entry->type = OBJ_TAG;
1077 } else
1078 die("unable to pack object %s of type %s",
1079 sha1_to_hex(entry->sha1), type);
1080}
1081
Junio C Hamano15b4d572006-02-17 20:58:45 -08001082static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1083{
1084 struct object_entry *child = me->delta_child;
1085 unsigned int m = n;
1086 while (child) {
1087 unsigned int c = check_delta_limit(child, n + 1);
1088 if (m < c)
1089 m = c;
1090 child = child->delta_sibling;
1091 }
1092 return m;
1093}
1094
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001095static void get_object_details(void)
1096{
1097 int i;
Junio C Hamano15b4d572006-02-17 20:58:45 -08001098 struct object_entry *entry;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001099
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001100 prepare_pack_ix();
Junio C Hamano15b4d572006-02-17 20:58:45 -08001101 for (i = 0, entry = objects; i < nr_objects; i++, entry++)
1102 check_object(entry);
Junio C Hamanob76f6b62006-02-23 23:04:52 -08001103
1104 if (nr_objects == nr_result) {
1105 /*
1106 * Depth of objects that depend on the entry -- this
1107 * is subtracted from depth-max to break too deep
1108 * delta chain because of delta data reusing.
1109 * However, we loosen this restriction when we know we
1110 * are creating a thin pack -- it will have to be
1111 * expanded on the other end anyway, so do not
1112 * artificially cut the delta chain and let it go as
1113 * deep as it wants.
1114 */
1115 for (i = 0, entry = objects; i < nr_objects; i++, entry++)
1116 if (!entry->delta && entry->delta_child)
1117 entry->delta_limit =
1118 check_delta_limit(entry, 1);
1119 }
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001120}
1121
1122typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
1123
1124static entry_sort_t current_sort;
1125
1126static int sort_comparator(const void *_a, const void *_b)
1127{
1128 struct object_entry *a = *(struct object_entry **)_a;
1129 struct object_entry *b = *(struct object_entry **)_b;
1130 return current_sort(a,b);
1131}
1132
1133static struct object_entry **create_sorted_list(entry_sort_t sort)
1134{
1135 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
1136 int i;
1137
1138 for (i = 0; i < nr_objects; i++)
1139 list[i] = objects + i;
1140 current_sort = sort;
1141 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
1142 return list;
1143}
1144
1145static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
1146{
David Rientjesa89fccd2006-08-17 11:54:57 -07001147 return hashcmp(a->sha1, b->sha1);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001148}
1149
Timo Hirvonen962554c2006-02-26 17:13:46 +02001150static struct object_entry **create_final_object_list(void)
Junio C Hamano7a979d92006-02-19 14:47:21 -08001151{
1152 struct object_entry **list;
1153 int i, j;
1154
1155 for (i = nr_result = 0; i < nr_objects; i++)
1156 if (!objects[i].preferred_base)
1157 nr_result++;
1158 list = xmalloc(nr_result * sizeof(struct object_entry *));
1159 for (i = j = 0; i < nr_objects; i++) {
1160 if (!objects[i].preferred_base)
1161 list[j++] = objects + i;
1162 }
1163 current_sort = sha1_sort;
1164 qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator);
1165 return list;
1166}
1167
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001168static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
1169{
1170 if (a->type < b->type)
1171 return -1;
1172 if (a->type > b->type)
1173 return 1;
Linus Torvalds27225f22005-06-26 15:27:28 -07001174 if (a->hash < b->hash)
1175 return -1;
1176 if (a->hash > b->hash)
1177 return 1;
Junio C Hamano7a979d92006-02-19 14:47:21 -08001178 if (a->preferred_base < b->preferred_base)
1179 return -1;
1180 if (a->preferred_base > b->preferred_base)
1181 return 1;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001182 if (a->size < b->size)
1183 return -1;
1184 if (a->size > b->size)
1185 return 1;
1186 return a < b ? -1 : (a > b);
1187}
1188
1189struct unpacked {
1190 struct object_entry *entry;
1191 void *data;
Nicolas Pitref6c70812006-04-26 23:58:00 -04001192 struct delta_index *index;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001193};
1194
1195/*
Linus Torvalds521a4f42005-06-26 13:43:41 -07001196 * We search for deltas _backwards_ in a list sorted by type and
1197 * by size, so that we see progressively smaller and smaller files.
1198 * That's because we prefer deltas to be from the bigger file
1199 * to the smaller - deletes are potentially cheaper, but perhaps
1200 * more importantly, the bigger file is likely the more recent
1201 * one.
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001202 */
Nicolas Pitref6c70812006-04-26 23:58:00 -04001203static int try_delta(struct unpacked *trg, struct unpacked *src,
Nicolas Pitre560b25a2006-06-30 22:55:30 -04001204 unsigned max_depth)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001205{
Nicolas Pitref6c70812006-04-26 23:58:00 -04001206 struct object_entry *trg_entry = trg->entry;
1207 struct object_entry *src_entry = src->entry;
Nicolas Pitre560b25a2006-06-30 22:55:30 -04001208 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1209 char type[10];
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001210 void *delta_buf;
1211
1212 /* Don't bother doing diffs between different types */
Nicolas Pitref6c70812006-04-26 23:58:00 -04001213 if (trg_entry->type != src_entry->type)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001214 return -1;
1215
Junio C Hamano7a979d92006-02-19 14:47:21 -08001216 /* We do not compute delta to *create* objects we are not
1217 * going to pack.
1218 */
Nicolas Pitref6c70812006-04-26 23:58:00 -04001219 if (trg_entry->preferred_base)
Linus Torvalds75c42d82005-06-25 19:30:20 -07001220 return -1;
Junio C Hamano7a979d92006-02-19 14:47:21 -08001221
Nicolas Pitref6c70812006-04-26 23:58:00 -04001222 /*
Linus Torvalds51d1e832006-06-29 14:04:01 -07001223 * We do not bother to try a delta that we discarded
Nicolas Pitre8dbbd142006-06-29 23:44:52 -04001224 * on an earlier try, but only when reusing delta data.
Linus Torvalds51d1e832006-06-29 14:04:01 -07001225 */
Nicolas Pitre8dbbd142006-06-29 23:44:52 -04001226 if (!no_reuse_delta && trg_entry->in_pack &&
Junio C Hamanoe9195b52006-11-14 22:18:31 -08001227 trg_entry->in_pack == src_entry->in_pack &&
1228 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1229 trg_entry->in_pack_type != OBJ_OFS_DELTA)
Linus Torvalds51d1e832006-06-29 14:04:01 -07001230 return 0;
1231
1232 /*
Nicolas Pitref6c70812006-04-26 23:58:00 -04001233 * If the current object is at pack edge, take the depth the
Junio C Hamano7a979d92006-02-19 14:47:21 -08001234 * objects that depend on the current object into account --
1235 * otherwise they would become too deep.
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001236 */
Nicolas Pitref6c70812006-04-26 23:58:00 -04001237 if (trg_entry->delta_child) {
1238 if (max_depth <= trg_entry->delta_limit)
Junio C Hamano15b4d572006-02-17 20:58:45 -08001239 return 0;
Nicolas Pitref6c70812006-04-26 23:58:00 -04001240 max_depth -= trg_entry->delta_limit;
Junio C Hamano15b4d572006-02-17 20:58:45 -08001241 }
Nicolas Pitref6c70812006-04-26 23:58:00 -04001242 if (src_entry->depth >= max_depth)
Linus Torvaldsd116a452005-06-25 20:17:59 -07001243 return 0;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001244
Nicolas Pitrec3b06a62006-05-16 16:29:14 -04001245 /* Now some size filtering heuristics. */
Nicolas Pitre560b25a2006-06-30 22:55:30 -04001246 trg_size = trg_entry->size;
1247 max_size = trg_size/2 - 20;
Nicolas Pitrec3b06a62006-05-16 16:29:14 -04001248 max_size = max_size * (max_depth - src_entry->depth) / max_depth;
1249 if (max_size == 0)
1250 return 0;
Nicolas Pitre4e8da192006-05-15 11:40:05 -04001251 if (trg_entry->delta && trg_entry->delta_size <= max_size)
Nicolas Pitref6c70812006-04-26 23:58:00 -04001252 max_size = trg_entry->delta_size-1;
1253 src_size = src_entry->size;
Nicolas Pitre560b25a2006-06-30 22:55:30 -04001254 sizediff = src_size < trg_size ? trg_size - src_size : 0;
Linus Torvalds27225f22005-06-26 15:27:28 -07001255 if (sizediff >= max_size)
Junio C Hamanof527cb82006-04-20 23:36:22 -07001256 return 0;
Nicolas Pitref6c70812006-04-26 23:58:00 -04001257
Nicolas Pitre560b25a2006-06-30 22:55:30 -04001258 /* Load data if not already done */
1259 if (!trg->data) {
1260 trg->data = read_sha1_file(trg_entry->sha1, type, &sz);
1261 if (sz != trg_size)
1262 die("object %s inconsistent object length (%lu vs %lu)",
1263 sha1_to_hex(trg_entry->sha1), sz, trg_size);
1264 }
1265 if (!src->data) {
1266 src->data = read_sha1_file(src_entry->sha1, type, &sz);
1267 if (sz != src_size)
1268 die("object %s inconsistent object length (%lu vs %lu)",
1269 sha1_to_hex(src_entry->sha1), sz, src_size);
1270 }
1271 if (!src->index) {
1272 src->index = create_delta_index(src->data, src_size);
1273 if (!src->index)
1274 die("out of memory");
1275 }
1276
1277 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001278 if (!delta_buf)
Linus Torvalds75c42d82005-06-25 19:30:20 -07001279 return 0;
Nicolas Pitref6c70812006-04-26 23:58:00 -04001280
1281 trg_entry->delta = src_entry;
1282 trg_entry->delta_size = delta_size;
1283 trg_entry->depth = src_entry->depth + 1;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001284 free(delta_buf);
Nicolas Pitref6c70812006-04-26 23:58:00 -04001285 return 1;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001286}
1287
Nicolas Pitreb2504a02006-02-22 16:00:08 -05001288static void progress_interval(int signum)
1289{
Nicolas Pitreb2504a02006-02-22 16:00:08 -05001290 progress_update = 1;
1291}
1292
Linus Torvaldsd116a452005-06-25 20:17:59 -07001293static void find_deltas(struct object_entry **list, int window, int depth)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001294{
Linus Torvalds521a4f42005-06-26 13:43:41 -07001295 int i, idx;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001296 unsigned int array_size = window * sizeof(struct unpacked);
1297 struct unpacked *array = xmalloc(array_size);
Junio C Hamano183bdb22006-02-22 16:02:59 -08001298 unsigned processed = 0;
1299 unsigned last_percent = 999;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001300
1301 memset(array, 0, array_size);
Linus Torvalds521a4f42005-06-26 13:43:41 -07001302 i = nr_objects;
1303 idx = 0;
Junio C Hamano183bdb22006-02-22 16:02:59 -08001304 if (progress)
Junio C Hamanof0b0af12006-02-24 21:55:23 -08001305 fprintf(stderr, "Deltifying %d objects.\n", nr_result);
Junio C Hamano21fcd1b2006-02-11 17:54:18 -08001306
Linus Torvalds521a4f42005-06-26 13:43:41 -07001307 while (--i >= 0) {
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001308 struct object_entry *entry = list[i];
1309 struct unpacked *n = array + idx;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001310 int j;
1311
Junio C Hamanof0b0af12006-02-24 21:55:23 -08001312 if (!entry->preferred_base)
1313 processed++;
1314
Junio C Hamano183bdb22006-02-22 16:02:59 -08001315 if (progress) {
Junio C Hamanof0b0af12006-02-24 21:55:23 -08001316 unsigned percent = processed * 100 / nr_result;
Junio C Hamano183bdb22006-02-22 16:02:59 -08001317 if (percent != last_percent || progress_update) {
1318 fprintf(stderr, "%4u%% (%u/%u) done\r",
Junio C Hamanof0b0af12006-02-24 21:55:23 -08001319 percent, processed, nr_result);
Junio C Hamano183bdb22006-02-22 16:02:59 -08001320 progress_update = 0;
1321 last_percent = percent;
1322 }
Junio C Hamano21fcd1b2006-02-11 17:54:18 -08001323 }
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001324
1325 if (entry->delta)
1326 /* This happens if we decided to reuse existing
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001327 * delta from a pack. "!no_reuse_delta &&" is implied.
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001328 */
1329 continue;
1330
Junio C Hamano9a8b6a02006-04-27 19:31:46 -07001331 if (entry->size < 50)
1332 continue;
Nicolas Pitreff457152006-05-15 13:47:16 -04001333 free_delta_index(n->index);
1334 n->index = NULL;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001335 free(n->data);
Nicolas Pitre560b25a2006-06-30 22:55:30 -04001336 n->data = NULL;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001337 n->entry = entry;
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001338
Linus Torvalds78817c12005-06-25 18:29:23 -07001339 j = window;
1340 while (--j > 0) {
1341 unsigned int other_idx = idx + j;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001342 struct unpacked *m;
Linus Torvalds78817c12005-06-25 18:29:23 -07001343 if (other_idx >= window)
1344 other_idx -= window;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001345 m = array + other_idx;
1346 if (!m->entry)
1347 break;
Nicolas Pitre560b25a2006-06-30 22:55:30 -04001348 if (try_delta(n, m, depth) < 0)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001349 break;
1350 }
Junio C Hamano70ca1a32006-03-05 11:22:57 -08001351 /* if we made n a delta, and if n is already at max
1352 * depth, leaving it in the window is pointless. we
1353 * should evict it first.
Junio C Hamano70ca1a32006-03-05 11:22:57 -08001354 */
1355 if (entry->delta && depth <= entry->depth)
1356 continue;
Nicolas Pitreff457152006-05-15 13:47:16 -04001357
Linus Torvalds521a4f42005-06-26 13:43:41 -07001358 idx++;
1359 if (idx >= window)
1360 idx = 0;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001361 }
Sergey Vlasovadee7bd2005-08-08 22:46:58 +04001362
Nicolas Pitreb2504a02006-02-22 16:00:08 -05001363 if (progress)
1364 fputc('\n', stderr);
1365
Nicolas Pitref6c70812006-04-26 23:58:00 -04001366 for (i = 0; i < window; ++i) {
Nicolas Pitreff457152006-05-15 13:47:16 -04001367 free_delta_index(array[i].index);
Sergey Vlasovadee7bd2005-08-08 22:46:58 +04001368 free(array[i].data);
Nicolas Pitref6c70812006-04-26 23:58:00 -04001369 }
Sergey Vlasovadee7bd2005-08-08 22:46:58 +04001370 free(array);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001371}
1372
Junio C Hamanof3123c42005-10-22 01:28:13 -07001373static void prepare_pack(int window, int depth)
1374{
Junio C Hamano3f9ac8d2006-02-15 17:34:29 -08001375 get_object_details();
Junio C Hamanof3123c42005-10-22 01:28:13 -07001376 sorted_by_type = create_sorted_list(type_size_sort);
1377 if (window && depth)
1378 find_deltas(sorted_by_type, window+1, depth);
Junio C Hamanof3123c42005-10-22 01:28:13 -07001379}
1380
Junio C Hamanodf6d6102006-09-01 15:05:12 -07001381static int reuse_cached_pack(unsigned char *sha1)
Junio C Hamanof3123c42005-10-22 01:28:13 -07001382{
1383 static const char cache[] = "pack-cache/pack-%s.%s";
1384 char *cached_pack, *cached_idx;
1385 int ifd, ofd, ifd_ix = -1;
1386
1387 cached_pack = git_path(cache, sha1_to_hex(sha1), "pack");
1388 ifd = open(cached_pack, O_RDONLY);
1389 if (ifd < 0)
1390 return 0;
1391
1392 if (!pack_to_stdout) {
1393 cached_idx = git_path(cache, sha1_to_hex(sha1), "idx");
1394 ifd_ix = open(cached_idx, O_RDONLY);
1395 if (ifd_ix < 0) {
1396 close(ifd);
1397 return 0;
1398 }
1399 }
1400
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001401 if (progress)
1402 fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
1403 sha1_to_hex(sha1));
Junio C Hamanof3123c42005-10-22 01:28:13 -07001404
1405 if (pack_to_stdout) {
1406 if (copy_fd(ifd, 1))
1407 exit(1);
1408 close(ifd);
1409 }
1410 else {
1411 char name[PATH_MAX];
1412 snprintf(name, sizeof(name),
1413 "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack");
1414 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
1415 if (ofd < 0)
1416 die("unable to open %s (%s)", name, strerror(errno));
1417 if (copy_fd(ifd, ofd))
1418 exit(1);
1419 close(ifd);
1420
1421 snprintf(name, sizeof(name),
1422 "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx");
1423 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
1424 if (ofd < 0)
1425 die("unable to open %s (%s)", name, strerror(errno));
1426 if (copy_fd(ifd_ix, ofd))
1427 exit(1);
1428 close(ifd_ix);
1429 puts(sha1_to_hex(sha1));
1430 }
1431
1432 return 1;
1433}
1434
Linus Torvaldsfb7a6532006-04-02 13:28:27 -07001435static void setup_progress_signal(void)
1436{
1437 struct sigaction sa;
1438 struct itimerval v;
1439
1440 memset(&sa, 0, sizeof(sa));
1441 sa.sa_handler = progress_interval;
1442 sigemptyset(&sa.sa_mask);
1443 sa.sa_flags = SA_RESTART;
1444 sigaction(SIGALRM, &sa, NULL);
1445
1446 v.it_interval.tv_sec = 1;
1447 v.it_interval.tv_usec = 0;
1448 v.it_value = v.it_interval;
1449 setitimer(ITIMER_REAL, &v, NULL);
1450}
1451
Jeff King4812a932006-07-23 01:50:30 -04001452static int git_pack_config(const char *k, const char *v)
1453{
1454 if(!strcmp(k, "pack.window")) {
1455 window = git_config_int(k, v);
1456 return 0;
1457 }
1458 return git_default_config(k, v);
1459}
1460
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001461static void read_object_list_from_stdin(void)
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001462{
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001463 char line[40 + 1 + PATH_MAX + 2];
1464 unsigned char sha1[20];
1465 unsigned hash;
Nicolas Pitreb2504a02006-02-22 16:00:08 -05001466
Linus Torvaldsda93d122006-04-02 13:31:54 -07001467 for (;;) {
Linus Torvaldsda93d122006-04-02 13:31:54 -07001468 if (!fgets(line, sizeof(line), stdin)) {
1469 if (feof(stdin))
1470 break;
1471 if (!ferror(stdin))
1472 die("fgets returned NULL, not EOF, not error!");
Junio C Hamano687dd752006-04-03 23:41:09 -07001473 if (errno != EINTR)
1474 die("fgets: %s", strerror(errno));
1475 clearerr(stdin);
1476 continue;
Linus Torvaldsda93d122006-04-02 13:31:54 -07001477 }
Junio C Hamano7a979d92006-02-19 14:47:21 -08001478 if (line[0] == '-') {
1479 if (get_sha1_hex(line+1, sha1))
1480 die("expected edge sha1, got garbage:\n %s",
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001481 line);
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001482 add_preferred_base(sha1);
Junio C Hamano7a979d92006-02-19 14:47:21 -08001483 continue;
Junio C Hamano21fcd1b2006-02-11 17:54:18 -08001484 }
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001485 if (get_sha1_hex(line, sha1))
Linus Torvaldsef076182005-11-21 12:38:31 -08001486 die("expected sha1, got garbage:\n %s", line);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001487
Linus Torvaldsce0bd642006-06-05 12:03:31 -07001488 hash = name_hash(line+41);
Junio C Hamano5379a5c2006-04-05 23:24:57 -07001489 add_preferred_base_object(line+41, hash);
1490 add_object_entry(sha1, hash, 0);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001491 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001492}
1493
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001494static void show_commit(struct commit *commit)
1495{
1496 unsigned hash = name_hash("");
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001497 add_preferred_base_object("", hash);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001498 add_object_entry(commit->object.sha1, hash, 0);
1499}
1500
1501static void show_object(struct object_array_entry *p)
1502{
1503 unsigned hash = name_hash(p->name);
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001504 add_preferred_base_object(p->name, hash);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001505 add_object_entry(p->item->sha1, hash, 0);
1506}
1507
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001508static void show_edge(struct commit *commit)
1509{
1510 add_preferred_base(commit->object.sha1);
1511}
1512
1513static void get_object_list(int ac, const char **av)
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001514{
1515 struct rev_info revs;
1516 char line[1000];
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001517 int flags = 0;
1518
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001519 init_revisions(&revs, NULL);
1520 save_commit_buffer = 0;
1521 track_object_refs = 0;
1522 setup_revisions(ac, av, &revs, NULL);
1523
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001524 while (fgets(line, sizeof(line), stdin) != NULL) {
1525 int len = strlen(line);
1526 if (line[len - 1] == '\n')
1527 line[--len] = 0;
1528 if (!len)
1529 break;
1530 if (*line == '-') {
1531 if (!strcmp(line, "--not")) {
1532 flags ^= UNINTERESTING;
1533 continue;
1534 }
1535 die("not a rev '%s'", line);
1536 }
1537 if (handle_revision_arg(line, &revs, flags, 1))
1538 die("bad revision '%s'", line);
1539 }
1540
1541 prepare_revision_walk(&revs);
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001542 mark_edges_uninteresting(revs.commits, &revs, show_edge);
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001543 traverse_commit_list(&revs, show_commit, show_object);
1544}
1545
1546int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1547{
1548 SHA_CTX ctx;
1549 int depth = 10;
1550 struct object_entry **list;
1551 int use_internal_rev_list = 0;
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001552 int thin = 0;
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001553 int i;
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001554 const char *rp_av[64];
1555 int rp_ac;
1556
1557 rp_av[0] = "pack-objects";
1558 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1559 rp_ac = 2;
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001560
1561 git_config(git_pack_config);
1562
1563 progress = isatty(2);
1564 for (i = 1; i < argc; i++) {
1565 const char *arg = argv[i];
1566
1567 if (*arg != '-')
1568 break;
1569
1570 if (!strcmp("--non-empty", arg)) {
1571 non_empty = 1;
1572 continue;
1573 }
1574 if (!strcmp("--local", arg)) {
1575 local = 1;
1576 continue;
1577 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001578 if (!strcmp("--incremental", arg)) {
1579 incremental = 1;
1580 continue;
1581 }
1582 if (!strncmp("--window=", arg, 9)) {
1583 char *end;
1584 window = strtoul(arg+9, &end, 0);
1585 if (!arg[9] || *end)
1586 usage(pack_usage);
1587 continue;
1588 }
1589 if (!strncmp("--depth=", arg, 8)) {
1590 char *end;
1591 depth = strtoul(arg+8, &end, 0);
1592 if (!arg[8] || *end)
1593 usage(pack_usage);
1594 continue;
1595 }
1596 if (!strcmp("--progress", arg)) {
1597 progress = 1;
1598 continue;
1599 }
Nicolas Pitre231f2402006-11-07 10:51:23 -05001600 if (!strcmp("--all-progress", arg)) {
1601 progress = 2;
1602 continue;
1603 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001604 if (!strcmp("-q", arg)) {
1605 progress = 0;
1606 continue;
1607 }
1608 if (!strcmp("--no-reuse-delta", arg)) {
1609 no_reuse_delta = 1;
1610 continue;
1611 }
Nicolas Pitrebe6b1912006-09-21 00:09:44 -04001612 if (!strcmp("--delta-base-offset", arg)) {
Nicolas Pitre780e6e72006-09-22 21:25:04 -04001613 allow_ofs_delta = 1;
Nicolas Pitrebe6b1912006-09-21 00:09:44 -04001614 continue;
1615 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001616 if (!strcmp("--stdout", arg)) {
1617 pack_to_stdout = 1;
1618 continue;
1619 }
1620 if (!strcmp("--revs", arg)) {
1621 use_internal_rev_list = 1;
1622 continue;
1623 }
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001624 if (!strcmp("--unpacked", arg) ||
1625 !strncmp("--unpacked=", arg, 11) ||
Junio C Hamano63049292006-12-18 17:25:28 -08001626 !strcmp("--reflog", arg) ||
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001627 !strcmp("--all", arg)) {
1628 use_internal_rev_list = 1;
1629 if (ARRAY_SIZE(rp_av) - 1 <= rp_ac)
1630 die("too many internal rev-list options");
1631 rp_av[rp_ac++] = arg;
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001632 continue;
1633 }
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001634 if (!strcmp("--thin", arg)) {
1635 use_internal_rev_list = 1;
1636 thin = 1;
1637 rp_av[1] = "--objects-edge";
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001638 continue;
1639 }
1640 usage(pack_usage);
1641 }
1642
1643 /* Traditionally "pack-objects [options] base extra" failed;
1644 * we would however want to take refs parameter that would
1645 * have been given to upstream rev-list ourselves, which means
1646 * we somehow want to say what the base name is. So the
1647 * syntax would be:
1648 *
1649 * pack-objects [options] base <refs...>
1650 *
1651 * in other words, we would treat the first non-option as the
1652 * base_name and send everything else to the internal revision
1653 * walker.
1654 */
1655
1656 if (!pack_to_stdout)
1657 base_name = argv[i++];
1658
1659 if (pack_to_stdout != !base_name)
1660 usage(pack_usage);
1661
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001662 if (!pack_to_stdout && thin)
1663 die("--thin cannot be used to build an indexable pack.");
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001664
1665 prepare_packed_git();
1666
1667 if (progress) {
1668 fprintf(stderr, "Generating pack...\n");
1669 setup_progress_signal();
1670 }
1671
1672 if (!use_internal_rev_list)
1673 read_object_list_from_stdin();
Junio C Hamano8d1d8f82006-09-06 01:42:23 -07001674 else {
1675 rp_av[rp_ac] = NULL;
1676 get_object_list(rp_ac, rp_av);
1677 }
Junio C Hamanob5d97e62006-09-04 23:47:39 -07001678
Junio C Hamano21fcd1b2006-02-11 17:54:18 -08001679 if (progress)
1680 fprintf(stderr, "Done counting %d objects.\n", nr_objects);
Junio C Hamanof0b0af12006-02-24 21:55:23 -08001681 sorted_by_sha = create_final_object_list();
1682 if (non_empty && !nr_result)
Linus Torvalds1c4a2912005-07-03 13:36:58 -07001683 return 0;
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001684
Junio C Hamano84c8d8a2005-10-12 16:54:19 -07001685 SHA1_Init(&ctx);
1686 list = sorted_by_sha;
Junio C Hamano7a979d92006-02-19 14:47:21 -08001687 for (i = 0; i < nr_result; i++) {
Junio C Hamano84c8d8a2005-10-12 16:54:19 -07001688 struct object_entry *entry = *list++;
1689 SHA1_Update(&ctx, entry->sha1, 20);
1690 }
1691 SHA1_Final(object_list_sha1, &ctx);
Junio C Hamano7a979d92006-02-19 14:47:21 -08001692 if (progress && (nr_objects != nr_result))
1693 fprintf(stderr, "Result has %d objects.\n", nr_result);
Junio C Hamano84c8d8a2005-10-12 16:54:19 -07001694
Junio C Hamanodf6d6102006-09-01 15:05:12 -07001695 if (reuse_cached_pack(object_list_sha1))
Junio C Hamanof3123c42005-10-22 01:28:13 -07001696 ;
1697 else {
Junio C Hamanof0b0af12006-02-24 21:55:23 -08001698 if (nr_result)
1699 prepare_pack(window, depth);
Nicolas Pitrefa438a22006-10-31 16:58:32 -05001700 if (progress == 1 && pack_to_stdout) {
Nicolas Pitre5e8dc752006-02-22 17:41:32 -05001701 /* the other end usually displays progress itself */
1702 struct itimerval v = {{0,},};
1703 setitimer(ITIMER_REAL, &v, NULL);
1704 signal(SIGALRM, SIG_IGN );
1705 progress_update = 0;
1706 }
1707 write_pack_file();
Junio C Hamanof3123c42005-10-22 01:28:13 -07001708 if (!pack_to_stdout) {
1709 write_index_file();
1710 puts(sha1_to_hex(object_list_sha1));
1711 }
Linus Torvalds5f3de582005-07-03 15:34:04 -07001712 }
Junio C Hamanoab7cd7b2006-02-16 11:55:51 -08001713 if (progress)
Nicolas Pitre67c08ce2006-11-29 17:15:48 -05001714 fprintf(stderr, "Total %d (delta %d), reused %d (delta %d)\n",
1715 written, written_delta, reused, reused_delta);
Linus Torvaldsc323ac72005-06-25 14:42:43 -07001716 return 0;
1717}