]>
Commit | Line | Data |
---|---|---|
5d4a6003 | 1 | #include "builtin.h" |
c323ac7d LT |
2 | #include "cache.h" |
3 | #include "object.h" | |
8e440259 PE |
4 | #include "blob.h" |
5 | #include "commit.h" | |
6 | #include "tag.h" | |
7 | #include "tree.h" | |
c323ac7d | 8 | #include "delta.h" |
a733cb60 | 9 | #include "pack.h" |
c38138cd | 10 | #include "csum-file.h" |
1b0c7174 | 11 | #include "tree-walk.h" |
b5d97e6b JH |
12 | #include "diff.h" |
13 | #include "revision.h" | |
14 | #include "list-objects.h" | |
21fcd1bd | 15 | #include <sys/time.h> |
b2504a0d | 16 | #include <signal.h> |
c323ac7d | 17 | |
63fba759 | 18 | static const char pack_usage[] = "git-pack-objects [-q] [--no-reuse-delta] [--delta-base-offset] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] [--revs [--unpacked | --all]*] [--stdout | base-name] <ref-list | <object-list]"; |
c323ac7d | 19 | |
c323ac7d LT |
20 | struct object_entry { |
21 | unsigned char sha1[20]; | |
3f9ac8d2 | 22 | unsigned long size; /* uncompressed size */ |
15b4d577 JH |
23 | unsigned long offset; /* offset into the final pack file; |
24 | * nonzero if already written. | |
25 | */ | |
3f9ac8d2 | 26 | unsigned int depth; /* delta depth */ |
15b4d577 | 27 | unsigned int delta_limit; /* base adjustment for in-pack delta */ |
3f9ac8d2 | 28 | unsigned int hash; /* name hint hash */ |
a733cb60 | 29 | enum object_type type; |
ab7cd7bb | 30 | enum object_type in_pack_type; /* could be delta */ |
3f9ac8d2 | 31 | unsigned long delta_size; /* delta data size (uncompressed) */ |
780e6e73 | 32 | #define in_pack_header_size delta_size /* only when reusing pack data */ |
3f9ac8d2 JH |
33 | struct object_entry *delta; /* delta base object */ |
34 | struct packed_git *in_pack; /* already in pack */ | |
3f9ac8d2 | 35 | unsigned int in_pack_offset; |
82e5a82f | 36 | struct object_entry *delta_child; /* deltified objects who bases me */ |
15b4d577 JH |
37 | struct object_entry *delta_sibling; /* other deltified objects who |
38 | * uses the same base as me | |
39 | */ | |
7a979d99 JH |
40 | int preferred_base; /* we do not pack this, but is encouraged to |
41 | * be used as the base objectto delta huge | |
42 | * objects against. | |
43 | */ | |
c323ac7d LT |
44 | }; |
45 | ||
3f9ac8d2 | 46 | /* |
82e5a82f | 47 | * Objects we are going to pack are collected in objects array (dynamically |
3f9ac8d2 JH |
48 | * expanded). nr_objects & nr_alloc controls this array. They are stored |
49 | * in the order we see -- typically rev-list --objects order that gives us | |
50 | * nice "minimum seek" order. | |
51 | * | |
52 | * sorted-by-sha ans sorted-by-type are arrays of pointers that point at | |
53 | * elements in the objects array. The former is used to build the pack | |
54 | * index (lists object names in the ascending order to help offset lookup), | |
55 | * and the latter is used to group similar things together by try_delta() | |
56 | * heuristics. | |
57 | */ | |
58 | ||
5f3de58f | 59 | static unsigned char object_list_sha1[20]; |
96f1e58f DR |
60 | static int non_empty; |
61 | static int no_reuse_delta; | |
62 | static int local; | |
63 | static int incremental; | |
be6b1914 NP |
64 | static int allow_ofs_delta; |
65 | ||
c323ac7d | 66 | static struct object_entry **sorted_by_sha, **sorted_by_type; |
96f1e58f DR |
67 | static struct object_entry *objects; |
68 | static int nr_objects, nr_alloc, nr_result; | |
c323ac7d | 69 | static const char *base_name; |
e1808845 | 70 | static unsigned char pack_file_sha1[20]; |
024701f1 | 71 | static int progress = 1; |
96f1e58f | 72 | static volatile sig_atomic_t progress_update; |
4812a93a | 73 | static int window = 10; |
df6d6101 | 74 | static int pack_to_stdout; |
8d1d8f83 | 75 | static int num_preferred_base; |
c323ac7d | 76 | |
3f9ac8d2 JH |
77 | /* |
78 | * The object names in objects array are hashed with this hashtable, | |
79 | * to help looking up the entry by object name. Binary search from | |
80 | * sorted_by_sha is also possible but this was easier to code and faster. | |
81 | * This hashtable is built after all the objects are seen. | |
82 | */ | |
96f1e58f DR |
83 | static int *object_ix; |
84 | static int object_ix_hashsz; | |
3f9ac8d2 JH |
85 | |
86 | /* | |
87 | * Pack index for existing packs give us easy access to the offsets into | |
88 | * corresponding pack file where each object's data starts, but the entries | |
89 | * do not store the size of the compressed representation (uncompressed | |
780e6e73 NP |
90 | * size is easily available by examining the pack entry header). It is |
91 | * also rather expensive to find the sha1 for an object given its offset. | |
92 | * | |
93 | * We build a hashtable of existing packs (pack_revindex), and keep reverse | |
94 | * index here -- pack index file is sorted by object name mapping to offset; | |
95 | * this pack_revindex[].revindex array is a list of offset/index_nr pairs | |
96 | * ordered by offset, so if you know the offset of an object, next offset | |
97 | * is where its packed representation ends and the index_nr can be used to | |
98 | * get the object sha1 from the main index. | |
3f9ac8d2 | 99 | */ |
780e6e73 NP |
100 | struct revindex_entry { |
101 | unsigned int offset; | |
102 | unsigned int nr; | |
103 | }; | |
3f9ac8d2 JH |
104 | struct pack_revindex { |
105 | struct packed_git *p; | |
780e6e73 NP |
106 | struct revindex_entry *revindex; |
107 | }; | |
108 | static struct pack_revindex *pack_revindex; | |
96f1e58f | 109 | static int pack_revindex_hashsz; |
3f9ac8d2 JH |
110 | |
111 | /* | |
112 | * stats | |
113 | */ | |
96f1e58f DR |
114 | static int written; |
115 | static int written_delta; | |
116 | static int reused; | |
117 | static int reused_delta; | |
3f9ac8d2 JH |
118 | |
119 | static int pack_revindex_ix(struct packed_git *p) | |
120 | { | |
2b74cffa | 121 | unsigned long ui = (unsigned long)p; |
3f9ac8d2 JH |
122 | int i; |
123 | ||
124 | ui = ui ^ (ui >> 16); /* defeat structure alignment */ | |
125 | i = (int)(ui % pack_revindex_hashsz); | |
126 | while (pack_revindex[i].p) { | |
127 | if (pack_revindex[i].p == p) | |
128 | return i; | |
129 | if (++i == pack_revindex_hashsz) | |
130 | i = 0; | |
131 | } | |
132 | return -1 - i; | |
133 | } | |
134 | ||
135 | static void prepare_pack_ix(void) | |
136 | { | |
137 | int num; | |
138 | struct packed_git *p; | |
139 | for (num = 0, p = packed_git; p; p = p->next) | |
140 | num++; | |
141 | if (!num) | |
142 | return; | |
143 | pack_revindex_hashsz = num * 11; | |
144 | pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz); | |
145 | for (p = packed_git; p; p = p->next) { | |
146 | num = pack_revindex_ix(p); | |
147 | num = - 1 - num; | |
148 | pack_revindex[num].p = p; | |
149 | } | |
150 | /* revindex elements are lazily initialized */ | |
151 | } | |
152 | ||
153 | static int cmp_offset(const void *a_, const void *b_) | |
154 | { | |
780e6e73 NP |
155 | const struct revindex_entry *a = a_; |
156 | const struct revindex_entry *b = b_; | |
157 | return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0; | |
3f9ac8d2 JH |
158 | } |
159 | ||
160 | /* | |
161 | * Ordered list of offsets of objects in the pack. | |
162 | */ | |
163 | static void prepare_pack_revindex(struct pack_revindex *rix) | |
164 | { | |
165 | struct packed_git *p = rix->p; | |
166 | int num_ent = num_packed_objects(p); | |
167 | int i; | |
168 | void *index = p->index_base + 256; | |
169 | ||
780e6e73 | 170 | rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1)); |
3f9ac8d2 | 171 | for (i = 0; i < num_ent; i++) { |
1d7f171c | 172 | unsigned int hl = *((unsigned int *)((char *) index + 24*i)); |
780e6e73 NP |
173 | rix->revindex[i].offset = ntohl(hl); |
174 | rix->revindex[i].nr = i; | |
3f9ac8d2 JH |
175 | } |
176 | /* This knows the pack format -- the 20-byte trailer | |
177 | * follows immediately after the last object data. | |
178 | */ | |
780e6e73 NP |
179 | rix->revindex[num_ent].offset = p->pack_size - 20; |
180 | rix->revindex[num_ent].nr = -1; | |
181 | qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset); | |
3f9ac8d2 JH |
182 | } |
183 | ||
780e6e73 NP |
184 | static struct revindex_entry * find_packed_object(struct packed_git *p, |
185 | unsigned int ofs) | |
3f9ac8d2 JH |
186 | { |
187 | int num; | |
188 | int lo, hi; | |
189 | struct pack_revindex *rix; | |
780e6e73 | 190 | struct revindex_entry *revindex; |
3f9ac8d2 JH |
191 | num = pack_revindex_ix(p); |
192 | if (num < 0) | |
193 | die("internal error: pack revindex uninitialized"); | |
194 | rix = &pack_revindex[num]; | |
195 | if (!rix->revindex) | |
196 | prepare_pack_revindex(rix); | |
197 | revindex = rix->revindex; | |
198 | lo = 0; | |
199 | hi = num_packed_objects(p) + 1; | |
200 | do { | |
201 | int mi = (lo + hi) / 2; | |
780e6e73 NP |
202 | if (revindex[mi].offset == ofs) { |
203 | return revindex + mi; | |
3f9ac8d2 | 204 | } |
780e6e73 | 205 | else if (ofs < revindex[mi].offset) |
3f9ac8d2 JH |
206 | hi = mi; |
207 | else | |
208 | lo = mi + 1; | |
209 | } while (lo < hi); | |
210 | die("internal error: pack revindex corrupt"); | |
211 | } | |
212 | ||
780e6e73 NP |
213 | static unsigned long find_packed_object_size(struct packed_git *p, |
214 | unsigned long ofs) | |
215 | { | |
216 | struct revindex_entry *entry = find_packed_object(p, ofs); | |
217 | return entry[1].offset - ofs; | |
218 | } | |
219 | ||
220 | static unsigned char *find_packed_object_name(struct packed_git *p, | |
221 | unsigned long ofs) | |
222 | { | |
223 | struct revindex_entry *entry = find_packed_object(p, ofs); | |
224 | return (unsigned char *)(p->index_base + 256) + 24 * entry->nr + 4; | |
225 | } | |
226 | ||
c323ac7d LT |
227 | static void *delta_against(void *buf, unsigned long size, struct object_entry *entry) |
228 | { | |
229 | unsigned long othersize, delta_size; | |
230 | char type[10]; | |
231 | void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize); | |
232 | void *delta_buf; | |
233 | ||
234 | if (!otherbuf) | |
235 | die("unable to read %s", sha1_to_hex(entry->delta->sha1)); | |
8ee378a0 | 236 | delta_buf = diff_delta(otherbuf, othersize, |
dcde55bc | 237 | buf, size, &delta_size, 0); |
75c42d8c | 238 | if (!delta_buf || delta_size != entry->delta_size) |
c323ac7d LT |
239 | die("delta size changed"); |
240 | free(buf); | |
241 | free(otherbuf); | |
242 | return delta_buf; | |
243 | } | |
244 | ||
a733cb60 LT |
245 | /* |
246 | * The per-object header is a pretty dense thing, which is | |
247 | * - first byte: low four bits are "size", then three bits of "type", | |
248 | * and the high bit is "size continues". | |
249 | * - each byte afterwards: low seven bits are size continuation, | |
250 | * with the high bit being "size continues" | |
251 | */ | |
252 | static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr) | |
253 | { | |
01247d87 | 254 | int n = 1; |
a733cb60 LT |
255 | unsigned char c; |
256 | ||
eb32d236 | 257 | if (type < OBJ_COMMIT || type > OBJ_REF_DELTA) |
a733cb60 LT |
258 | die("bad type %d", type); |
259 | ||
01247d87 LT |
260 | c = (type << 4) | (size & 15); |
261 | size >>= 4; | |
262 | while (size) { | |
a733cb60 | 263 | *hdr++ = c | 0x80; |
01247d87 LT |
264 | c = size & 0x7f; |
265 | size >>= 7; | |
266 | n++; | |
a733cb60 LT |
267 | } |
268 | *hdr = c; | |
269 | return n; | |
270 | } | |
271 | ||
780e6e73 NP |
272 | /* |
273 | * we are going to reuse the existing object data as is. make | |
274 | * sure it is not corrupt. | |
275 | */ | |
72518e9c | 276 | static int check_inflate(unsigned char *data, unsigned long len, unsigned long expect) |
df6d6101 | 277 | { |
72518e9c JH |
278 | z_stream stream; |
279 | unsigned char fakebuf[4096]; | |
280 | int st; | |
281 | ||
282 | memset(&stream, 0, sizeof(stream)); | |
283 | stream.next_in = data; | |
284 | stream.avail_in = len; | |
285 | stream.next_out = fakebuf; | |
286 | stream.avail_out = sizeof(fakebuf); | |
287 | inflateInit(&stream); | |
288 | ||
289 | while (1) { | |
290 | st = inflate(&stream, Z_FINISH); | |
291 | if (st == Z_STREAM_END || st == Z_OK) { | |
292 | st = (stream.total_out == expect && | |
293 | stream.total_in == len) ? 0 : -1; | |
294 | break; | |
295 | } | |
296 | if (st != Z_BUF_ERROR) { | |
297 | st = -1; | |
298 | break; | |
299 | } | |
300 | stream.next_out = fakebuf; | |
301 | stream.avail_out = sizeof(fakebuf); | |
302 | } | |
303 | inflateEnd(&stream); | |
304 | return st; | |
df6d6101 JH |
305 | } |
306 | ||
df6d6101 JH |
307 | static int revalidate_loose_object(struct object_entry *entry, |
308 | unsigned char *map, | |
309 | unsigned long mapsize) | |
310 | { | |
311 | /* we already know this is a loose object with new type header. */ | |
72518e9c JH |
312 | enum object_type type; |
313 | unsigned long size, used; | |
df6d6101 JH |
314 | |
315 | if (pack_to_stdout) | |
316 | return 0; | |
317 | ||
72518e9c JH |
318 | used = unpack_object_header_gently(map, mapsize, &type, &size); |
319 | if (!used) | |
320 | return -1; | |
321 | map += used; | |
322 | mapsize -= used; | |
323 | return check_inflate(map, mapsize, size); | |
df6d6101 JH |
324 | } |
325 | ||
7a979d99 JH |
326 | static unsigned long write_object(struct sha1file *f, |
327 | struct object_entry *entry) | |
c323ac7d LT |
328 | { |
329 | unsigned long size; | |
330 | char type[10]; | |
3f9ac8d2 | 331 | void *buf; |
a733cb60 | 332 | unsigned char header[10]; |
c323ac7d | 333 | unsigned hdrlen, datalen; |
a733cb60 | 334 | enum object_type obj_type; |
ab7cd7bb | 335 | int to_reuse = 0; |
c323ac7d | 336 | |
a733cb60 | 337 | obj_type = entry->type; |
ab7cd7bb JH |
338 | if (! entry->in_pack) |
339 | to_reuse = 0; /* can't reuse what we don't have */ | |
780e6e73 | 340 | else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA) |
ab7cd7bb JH |
341 | to_reuse = 1; /* check_object() decided it for us */ |
342 | else if (obj_type != entry->in_pack_type) | |
343 | to_reuse = 0; /* pack has delta which is unusable */ | |
344 | else if (entry->delta) | |
345 | to_reuse = 0; /* we want to pack afresh */ | |
346 | else | |
347 | to_reuse = 1; /* we have it in-pack undeltified, | |
348 | * and we do not need to deltify it. | |
349 | */ | |
350 | ||
ceec1361 JH |
351 | if (!entry->in_pack && !entry->delta) { |
352 | unsigned char *map; | |
353 | unsigned long mapsize; | |
354 | map = map_sha1_file(entry->sha1, &mapsize); | |
355 | if (map && !legacy_loose_object(map)) { | |
356 | /* We can copy straight into the pack file */ | |
df6d6101 JH |
357 | if (revalidate_loose_object(entry, map, mapsize)) |
358 | die("corrupt loose object %s", | |
359 | sha1_to_hex(entry->sha1)); | |
ceec1361 JH |
360 | sha1write(f, map, mapsize); |
361 | munmap(map, mapsize); | |
362 | written++; | |
363 | reused++; | |
364 | return mapsize; | |
365 | } | |
366 | if (map) | |
367 | munmap(map, mapsize); | |
368 | } | |
369 | ||
df6d6101 | 370 | if (!to_reuse) { |
3f9ac8d2 JH |
371 | buf = read_sha1_file(entry->sha1, type, &size); |
372 | if (!buf) | |
373 | die("unable to read %s", sha1_to_hex(entry->sha1)); | |
374 | if (size != entry->size) | |
375 | die("object %s size inconsistency (%lu vs %lu)", | |
376 | sha1_to_hex(entry->sha1), size, entry->size); | |
377 | if (entry->delta) { | |
378 | buf = delta_against(buf, size, entry); | |
379 | size = entry->delta_size; | |
be6b1914 NP |
380 | obj_type = (allow_ofs_delta && entry->delta->offset) ? |
381 | OBJ_OFS_DELTA : OBJ_REF_DELTA; | |
3f9ac8d2 JH |
382 | } |
383 | /* | |
384 | * The object header is a byte of 'type' followed by zero or | |
be6b1914 | 385 | * more bytes of length. |
3f9ac8d2 JH |
386 | */ |
387 | hdrlen = encode_header(obj_type, size, header); | |
388 | sha1write(f, header, hdrlen); | |
389 | ||
be6b1914 NP |
390 | if (obj_type == OBJ_OFS_DELTA) { |
391 | /* | |
392 | * Deltas with relative base contain an additional | |
393 | * encoding of the relative offset for the delta | |
394 | * base from this object's position in the pack. | |
395 | */ | |
396 | unsigned long ofs = entry->offset - entry->delta->offset; | |
397 | unsigned pos = sizeof(header) - 1; | |
398 | header[pos] = ofs & 127; | |
399 | while (ofs >>= 7) | |
400 | header[--pos] = 128 | (--ofs & 127); | |
401 | sha1write(f, header + pos, sizeof(header) - pos); | |
402 | hdrlen += sizeof(header) - pos; | |
403 | } else if (obj_type == OBJ_REF_DELTA) { | |
404 | /* | |
405 | * Deltas with a base reference contain | |
406 | * an additional 20 bytes for the base sha1. | |
407 | */ | |
408 | sha1write(f, entry->delta->sha1, 20); | |
3f9ac8d2 JH |
409 | hdrlen += 20; |
410 | } | |
411 | datalen = sha1write_compressed(f, buf, size); | |
412 | free(buf); | |
c323ac7d | 413 | } |
3f9ac8d2 JH |
414 | else { |
415 | struct packed_git *p = entry->in_pack; | |
3f9ac8d2 | 416 | |
780e6e73 NP |
417 | if (entry->delta) { |
418 | obj_type = (allow_ofs_delta && entry->delta->offset) ? | |
419 | OBJ_OFS_DELTA : OBJ_REF_DELTA; | |
420 | reused_delta++; | |
421 | } | |
422 | hdrlen = encode_header(obj_type, entry->size, header); | |
423 | sha1write(f, header, hdrlen); | |
424 | if (obj_type == OBJ_OFS_DELTA) { | |
425 | unsigned long ofs = entry->offset - entry->delta->offset; | |
426 | unsigned pos = sizeof(header) - 1; | |
427 | header[pos] = ofs & 127; | |
428 | while (ofs >>= 7) | |
429 | header[--pos] = 128 | (--ofs & 127); | |
430 | sha1write(f, header + pos, sizeof(header) - pos); | |
431 | hdrlen += sizeof(header) - pos; | |
432 | } else if (obj_type == OBJ_REF_DELTA) { | |
433 | sha1write(f, entry->delta->sha1, 20); | |
434 | hdrlen += 20; | |
435 | } | |
df6d6101 | 436 | |
780e6e73 NP |
437 | use_packed_git(p); |
438 | buf = (char *) p->pack_base | |
439 | + entry->in_pack_offset | |
440 | + entry->in_pack_header_size; | |
441 | datalen = find_packed_object_size(p, entry->in_pack_offset) | |
442 | - entry->in_pack_header_size; | |
780e6e73 | 443 | if (!pack_to_stdout && check_inflate(buf, datalen, entry->size)) |
df6d6101 | 444 | die("corrupt delta in pack %s", sha1_to_hex(entry->sha1)); |
3f9ac8d2 JH |
445 | sha1write(f, buf, datalen); |
446 | unuse_packed_git(p); | |
3f9ac8d2 | 447 | reused++; |
a733cb60 | 448 | } |
be6b1914 | 449 | if (entry->delta) |
ab7cd7bb | 450 | written_delta++; |
3f9ac8d2 | 451 | written++; |
c323ac7d LT |
452 | return hdrlen + datalen; |
453 | } | |
454 | ||
9d5ab962 JH |
455 | static unsigned long write_one(struct sha1file *f, |
456 | struct object_entry *e, | |
457 | unsigned long offset) | |
458 | { | |
be6b1914 | 459 | if (e->offset || e->preferred_base) |
9d5ab962 JH |
460 | /* offset starts from header size and cannot be zero |
461 | * if it is written already. | |
462 | */ | |
463 | return offset; | |
be6b1914 | 464 | /* if we are deltified, write out its base object first. */ |
9d5ab962 JH |
465 | if (e->delta) |
466 | offset = write_one(f, e->delta, offset); | |
be6b1914 NP |
467 | e->offset = offset; |
468 | return offset + write_object(f, e); | |
9d5ab962 JH |
469 | } |
470 | ||
c323ac7d LT |
471 | static void write_pack_file(void) |
472 | { | |
473 | int i; | |
d22b9290 | 474 | struct sha1file *f; |
a733cb60 | 475 | unsigned long offset; |
a733cb60 | 476 | struct pack_header hdr; |
183bdb2c JH |
477 | unsigned last_percent = 999; |
478 | int do_progress = 0; | |
c323ac7d | 479 | |
d22b9290 LT |
480 | if (!base_name) |
481 | f = sha1fd(1, "<stdout>"); | |
183bdb2c JH |
482 | else { |
483 | f = sha1create("%s-%s.%s", base_name, | |
484 | sha1_to_hex(object_list_sha1), "pack"); | |
485 | do_progress = progress; | |
486 | } | |
487 | if (do_progress) | |
f0b0af1b | 488 | fprintf(stderr, "Writing %d objects.\n", nr_result); |
183bdb2c | 489 | |
a733cb60 | 490 | hdr.hdr_signature = htonl(PACK_SIGNATURE); |
01247d87 | 491 | hdr.hdr_version = htonl(PACK_VERSION); |
7a979d99 | 492 | hdr.hdr_entries = htonl(nr_result); |
a733cb60 LT |
493 | sha1write(f, &hdr, sizeof(hdr)); |
494 | offset = sizeof(hdr); | |
f0b0af1b JH |
495 | if (!nr_result) |
496 | goto done; | |
5e8dc750 | 497 | for (i = 0; i < nr_objects; i++) { |
9d5ab962 | 498 | offset = write_one(f, objects + i, offset); |
183bdb2c | 499 | if (do_progress) { |
f0b0af1b | 500 | unsigned percent = written * 100 / nr_result; |
183bdb2c JH |
501 | if (progress_update || percent != last_percent) { |
502 | fprintf(stderr, "%4u%% (%u/%u) done\r", | |
f0b0af1b | 503 | percent, written, nr_result); |
183bdb2c JH |
504 | progress_update = 0; |
505 | last_percent = percent; | |
506 | } | |
5e8dc750 NP |
507 | } |
508 | } | |
183bdb2c JH |
509 | if (do_progress) |
510 | fputc('\n', stderr); | |
f0b0af1b | 511 | done: |
e1808845 | 512 | sha1close(f, pack_file_sha1, 1); |
c323ac7d LT |
513 | } |
514 | ||
515 | static void write_index_file(void) | |
516 | { | |
517 | int i; | |
7a979d99 JH |
518 | struct sha1file *f = sha1create("%s-%s.%s", base_name, |
519 | sha1_to_hex(object_list_sha1), "idx"); | |
c323ac7d | 520 | struct object_entry **list = sorted_by_sha; |
7a979d99 | 521 | struct object_entry **last = list + nr_result; |
c323ac7d LT |
522 | unsigned int array[256]; |
523 | ||
524 | /* | |
525 | * Write the first-level table (the list is sorted, | |
526 | * but we use a 256-entry lookup to be able to avoid | |
e1808845 | 527 | * having to do eight extra binary search iterations). |
c323ac7d LT |
528 | */ |
529 | for (i = 0; i < 256; i++) { | |
530 | struct object_entry **next = list; | |
531 | while (next < last) { | |
532 | struct object_entry *entry = *next; | |
533 | if (entry->sha1[0] != i) | |
534 | break; | |
535 | next++; | |
536 | } | |
537 | array[i] = htonl(next - sorted_by_sha); | |
538 | list = next; | |
539 | } | |
c38138cd | 540 | sha1write(f, array, 256 * sizeof(int)); |
c323ac7d LT |
541 | |
542 | /* | |
543 | * Write the actual SHA1 entries.. | |
544 | */ | |
545 | list = sorted_by_sha; | |
7a979d99 | 546 | for (i = 0; i < nr_result; i++) { |
c323ac7d LT |
547 | struct object_entry *entry = *list++; |
548 | unsigned int offset = htonl(entry->offset); | |
c38138cd LT |
549 | sha1write(f, &offset, 4); |
550 | sha1write(f, entry->sha1, 20); | |
c323ac7d | 551 | } |
e1808845 LT |
552 | sha1write(f, pack_file_sha1, 20); |
553 | sha1close(f, NULL, 1); | |
c323ac7d LT |
554 | } |
555 | ||
7a979d99 JH |
556 | static int locate_object_entry_hash(const unsigned char *sha1) |
557 | { | |
558 | int i; | |
559 | unsigned int ui; | |
560 | memcpy(&ui, sha1, sizeof(unsigned int)); | |
561 | i = ui % object_ix_hashsz; | |
562 | while (0 < object_ix[i]) { | |
a89fccd2 | 563 | if (!hashcmp(sha1, objects[object_ix[i] - 1].sha1)) |
7a979d99 JH |
564 | return i; |
565 | if (++i == object_ix_hashsz) | |
566 | i = 0; | |
567 | } | |
568 | return -1 - i; | |
569 | } | |
570 | ||
571 | static struct object_entry *locate_object_entry(const unsigned char *sha1) | |
572 | { | |
573 | int i; | |
574 | ||
575 | if (!object_ix_hashsz) | |
576 | return NULL; | |
577 | ||
578 | i = locate_object_entry_hash(sha1); | |
579 | if (0 <= i) | |
580 | return &objects[object_ix[i]-1]; | |
581 | return NULL; | |
582 | } | |
583 | ||
584 | static void rehash_objects(void) | |
c323ac7d | 585 | { |
7a979d99 JH |
586 | int i; |
587 | struct object_entry *oe; | |
588 | ||
589 | object_ix_hashsz = nr_objects * 3; | |
590 | if (object_ix_hashsz < 1024) | |
591 | object_ix_hashsz = 1024; | |
592 | object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz); | |
5379a5c5 | 593 | memset(object_ix, 0, sizeof(int) * object_ix_hashsz); |
7a979d99 JH |
594 | for (i = 0, oe = objects; i < nr_objects; i++, oe++) { |
595 | int ix = locate_object_entry_hash(oe->sha1); | |
596 | if (0 <= ix) | |
597 | continue; | |
598 | ix = -1 - ix; | |
599 | object_ix[ix] = i + 1; | |
600 | } | |
601 | } | |
602 | ||
ce0bd642 | 603 | static unsigned name_hash(const char *name) |
1d6b38cc | 604 | { |
ce0bd642 LT |
605 | unsigned char c; |
606 | unsigned hash = 0; | |
607 | ||
eeef7135 | 608 | /* |
ce0bd642 LT |
609 | * This effectively just creates a sortable number from the |
610 | * last sixteen non-whitespace characters. Last characters | |
611 | * count "most", so things that end in ".c" sort together. | |
eeef7135 | 612 | */ |
ce0bd642 LT |
613 | while ((c = *name++) != 0) { |
614 | if (isspace(c)) | |
615 | continue; | |
616 | hash = (hash >> 2) + (c << 24); | |
617 | } | |
1d6b38cc JH |
618 | return hash; |
619 | } | |
620 | ||
621 | static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude) | |
c323ac7d LT |
622 | { |
623 | unsigned int idx = nr_objects; | |
624 | struct object_entry *entry; | |
3f9ac8d2 | 625 | struct packed_git *p; |
ab7cd7bb JH |
626 | unsigned int found_offset = 0; |
627 | struct packed_git *found_pack = NULL; | |
b925410d | 628 | int ix, status = 0; |
c323ac7d | 629 | |
7a979d99 | 630 | if (!exclude) { |
64560374 | 631 | for (p = packed_git; p; p = p->next) { |
43057304 NP |
632 | unsigned long offset = find_pack_entry_one(sha1, p); |
633 | if (offset) { | |
64560374 LT |
634 | if (incremental) |
635 | return 0; | |
636 | if (local && !p->pack_local) | |
637 | return 0; | |
7a979d99 | 638 | if (!found_pack) { |
43057304 NP |
639 | found_offset = offset; |
640 | found_pack = p; | |
7a979d99 | 641 | } |
64560374 LT |
642 | } |
643 | } | |
644 | } | |
7a979d99 JH |
645 | if ((entry = locate_object_entry(sha1)) != NULL) |
646 | goto already_added; | |
eb019375 | 647 | |
c323ac7d LT |
648 | if (idx >= nr_alloc) { |
649 | unsigned int needed = (idx + 1024) * 3 / 2; | |
650 | objects = xrealloc(objects, needed * sizeof(*entry)); | |
651 | nr_alloc = needed; | |
652 | } | |
653 | entry = objects + idx; | |
7a979d99 | 654 | nr_objects = idx + 1; |
c323ac7d | 655 | memset(entry, 0, sizeof(*entry)); |
e702496e | 656 | hashcpy(entry->sha1, sha1); |
27225f2e | 657 | entry->hash = hash; |
7a979d99 JH |
658 | |
659 | if (object_ix_hashsz * 3 <= nr_objects * 4) | |
660 | rehash_objects(); | |
661 | else { | |
662 | ix = locate_object_entry_hash(entry->sha1); | |
663 | if (0 <= ix) | |
664 | die("internal error in object hashing."); | |
665 | object_ix[-1 - ix] = idx + 1; | |
3f9ac8d2 | 666 | } |
b925410d | 667 | status = 1; |
7a979d99 JH |
668 | |
669 | already_added: | |
f0b0af1b JH |
670 | if (progress_update) { |
671 | fprintf(stderr, "Counting objects...%d\r", nr_objects); | |
672 | progress_update = 0; | |
673 | } | |
7a979d99 JH |
674 | if (exclude) |
675 | entry->preferred_base = 1; | |
676 | else { | |
677 | if (found_pack) { | |
678 | entry->in_pack = found_pack; | |
679 | entry->in_pack_offset = found_offset; | |
680 | } | |
a49dd05f | 681 | } |
b925410d | 682 | return status; |
c323ac7d LT |
683 | } |
684 | ||
5379a5c5 JH |
685 | struct pbase_tree_cache { |
686 | unsigned char sha1[20]; | |
687 | int ref; | |
688 | int temporary; | |
689 | void *tree_data; | |
690 | unsigned long tree_size; | |
691 | }; | |
692 | ||
693 | static struct pbase_tree_cache *(pbase_tree_cache[256]); | |
694 | static int pbase_tree_cache_ix(const unsigned char *sha1) | |
695 | { | |
696 | return sha1[0] % ARRAY_SIZE(pbase_tree_cache); | |
697 | } | |
698 | static int pbase_tree_cache_ix_incr(int ix) | |
699 | { | |
700 | return (ix+1) % ARRAY_SIZE(pbase_tree_cache); | |
701 | } | |
702 | ||
703 | static struct pbase_tree { | |
704 | struct pbase_tree *next; | |
705 | /* This is a phony "cache" entry; we are not | |
706 | * going to evict it nor find it through _get() | |
707 | * mechanism -- this is for the toplevel node that | |
708 | * would almost always change with any commit. | |
709 | */ | |
710 | struct pbase_tree_cache pcache; | |
711 | } *pbase_tree; | |
712 | ||
713 | static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1) | |
714 | { | |
715 | struct pbase_tree_cache *ent, *nent; | |
716 | void *data; | |
717 | unsigned long size; | |
718 | char type[20]; | |
719 | int neigh; | |
720 | int my_ix = pbase_tree_cache_ix(sha1); | |
721 | int available_ix = -1; | |
722 | ||
723 | /* pbase-tree-cache acts as a limited hashtable. | |
724 | * your object will be found at your index or within a few | |
725 | * slots after that slot if it is cached. | |
726 | */ | |
727 | for (neigh = 0; neigh < 8; neigh++) { | |
728 | ent = pbase_tree_cache[my_ix]; | |
a89fccd2 | 729 | if (ent && !hashcmp(ent->sha1, sha1)) { |
5379a5c5 JH |
730 | ent->ref++; |
731 | return ent; | |
732 | } | |
733 | else if (((available_ix < 0) && (!ent || !ent->ref)) || | |
734 | ((0 <= available_ix) && | |
735 | (!ent && pbase_tree_cache[available_ix]))) | |
736 | available_ix = my_ix; | |
737 | if (!ent) | |
738 | break; | |
739 | my_ix = pbase_tree_cache_ix_incr(my_ix); | |
740 | } | |
741 | ||
742 | /* Did not find one. Either we got a bogus request or | |
743 | * we need to read and perhaps cache. | |
744 | */ | |
745 | data = read_sha1_file(sha1, type, &size); | |
746 | if (!data) | |
747 | return NULL; | |
748 | if (strcmp(type, tree_type)) { | |
749 | free(data); | |
750 | return NULL; | |
751 | } | |
752 | ||
753 | /* We need to either cache or return a throwaway copy */ | |
754 | ||
755 | if (available_ix < 0) | |
756 | ent = NULL; | |
757 | else { | |
758 | ent = pbase_tree_cache[available_ix]; | |
759 | my_ix = available_ix; | |
760 | } | |
761 | ||
762 | if (!ent) { | |
763 | nent = xmalloc(sizeof(*nent)); | |
764 | nent->temporary = (available_ix < 0); | |
765 | } | |
766 | else { | |
767 | /* evict and reuse */ | |
768 | free(ent->tree_data); | |
769 | nent = ent; | |
770 | } | |
e702496e | 771 | hashcpy(nent->sha1, sha1); |
5379a5c5 JH |
772 | nent->tree_data = data; |
773 | nent->tree_size = size; | |
774 | nent->ref = 1; | |
775 | if (!nent->temporary) | |
776 | pbase_tree_cache[my_ix] = nent; | |
777 | return nent; | |
778 | } | |
779 | ||
780 | static void pbase_tree_put(struct pbase_tree_cache *cache) | |
781 | { | |
782 | if (!cache->temporary) { | |
783 | cache->ref--; | |
784 | return; | |
785 | } | |
786 | free(cache->tree_data); | |
787 | free(cache); | |
788 | } | |
789 | ||
790 | static int name_cmp_len(const char *name) | |
791 | { | |
792 | int i; | |
793 | for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++) | |
794 | ; | |
795 | return i; | |
796 | } | |
797 | ||
798 | static void add_pbase_object(struct tree_desc *tree, | |
5379a5c5 | 799 | const char *name, |
ce0bd642 LT |
800 | int cmplen, |
801 | const char *fullname) | |
3f9ac8d2 | 802 | { |
4c068a98 LT |
803 | struct name_entry entry; |
804 | ||
805 | while (tree_entry(tree,&entry)) { | |
7a979d99 JH |
806 | unsigned long size; |
807 | char type[20]; | |
808 | ||
4c068a98 LT |
809 | if (entry.pathlen != cmplen || |
810 | memcmp(entry.path, name, cmplen) || | |
811 | !has_sha1_file(entry.sha1) || | |
812 | sha1_object_info(entry.sha1, type, &size)) | |
7a979d99 | 813 | continue; |
5379a5c5 | 814 | if (name[cmplen] != '/') { |
ce0bd642 | 815 | unsigned hash = name_hash(fullname); |
4c068a98 | 816 | add_object_entry(entry.sha1, hash, 1); |
5379a5c5 JH |
817 | return; |
818 | } | |
8e440259 | 819 | if (!strcmp(type, tree_type)) { |
7a979d99 | 820 | struct tree_desc sub; |
5379a5c5 JH |
821 | struct pbase_tree_cache *tree; |
822 | const char *down = name+cmplen+1; | |
823 | int downlen = name_cmp_len(down); | |
824 | ||
4c068a98 | 825 | tree = pbase_tree_get(entry.sha1); |
5379a5c5 JH |
826 | if (!tree) |
827 | return; | |
828 | sub.buf = tree->tree_data; | |
829 | sub.size = tree->tree_size; | |
830 | ||
ce0bd642 | 831 | add_pbase_object(&sub, down, downlen, fullname); |
5379a5c5 JH |
832 | pbase_tree_put(tree); |
833 | } | |
834 | } | |
835 | } | |
1d6b38cc | 836 | |
5379a5c5 JH |
837 | static unsigned *done_pbase_paths; |
838 | static int done_pbase_paths_num; | |
839 | static int done_pbase_paths_alloc; | |
840 | static int done_pbase_path_pos(unsigned hash) | |
841 | { | |
842 | int lo = 0; | |
843 | int hi = done_pbase_paths_num; | |
844 | while (lo < hi) { | |
845 | int mi = (hi + lo) / 2; | |
846 | if (done_pbase_paths[mi] == hash) | |
847 | return mi; | |
848 | if (done_pbase_paths[mi] < hash) | |
849 | hi = mi; | |
850 | else | |
851 | lo = mi + 1; | |
852 | } | |
853 | return -lo-1; | |
854 | } | |
855 | ||
856 | static int check_pbase_path(unsigned hash) | |
857 | { | |
858 | int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash); | |
859 | if (0 <= pos) | |
860 | return 1; | |
861 | pos = -pos - 1; | |
862 | if (done_pbase_paths_alloc <= done_pbase_paths_num) { | |
863 | done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc); | |
864 | done_pbase_paths = xrealloc(done_pbase_paths, | |
865 | done_pbase_paths_alloc * | |
866 | sizeof(unsigned)); | |
867 | } | |
868 | done_pbase_paths_num++; | |
869 | if (pos < done_pbase_paths_num) | |
870 | memmove(done_pbase_paths + pos + 1, | |
871 | done_pbase_paths + pos, | |
872 | (done_pbase_paths_num - pos - 1) * sizeof(unsigned)); | |
873 | done_pbase_paths[pos] = hash; | |
874 | return 0; | |
875 | } | |
876 | ||
8d1d8f83 | 877 | static void add_preferred_base_object(const char *name, unsigned hash) |
5379a5c5 JH |
878 | { |
879 | struct pbase_tree *it; | |
880 | int cmplen = name_cmp_len(name); | |
881 | ||
882 | if (check_pbase_path(hash)) | |
883 | return; | |
884 | ||
885 | for (it = pbase_tree; it; it = it->next) { | |
886 | if (cmplen == 0) { | |
ce0bd642 | 887 | hash = name_hash(""); |
5379a5c5 JH |
888 | add_object_entry(it->pcache.sha1, hash, 1); |
889 | } | |
890 | else { | |
891 | struct tree_desc tree; | |
892 | tree.buf = it->pcache.tree_data; | |
893 | tree.size = it->pcache.tree_size; | |
ce0bd642 | 894 | add_pbase_object(&tree, name, cmplen, name); |
7a979d99 | 895 | } |
3f9ac8d2 | 896 | } |
3f9ac8d2 JH |
897 | } |
898 | ||
7a979d99 | 899 | static void add_preferred_base(unsigned char *sha1) |
3f9ac8d2 | 900 | { |
5379a5c5 JH |
901 | struct pbase_tree *it; |
902 | void *data; | |
903 | unsigned long size; | |
904 | unsigned char tree_sha1[20]; | |
1d6b38cc | 905 | |
8d1d8f83 JH |
906 | if (window <= num_preferred_base++) |
907 | return; | |
908 | ||
5379a5c5 JH |
909 | data = read_object_with_reference(sha1, tree_type, &size, tree_sha1); |
910 | if (!data) | |
7a979d99 | 911 | return; |
5379a5c5 JH |
912 | |
913 | for (it = pbase_tree; it; it = it->next) { | |
a89fccd2 | 914 | if (!hashcmp(it->pcache.sha1, tree_sha1)) { |
5379a5c5 JH |
915 | free(data); |
916 | return; | |
917 | } | |
918 | } | |
919 | ||
920 | it = xcalloc(1, sizeof(*it)); | |
921 | it->next = pbase_tree; | |
922 | pbase_tree = it; | |
923 | ||
e702496e | 924 | hashcpy(it->pcache.sha1, tree_sha1); |
5379a5c5 JH |
925 | it->pcache.tree_data = data; |
926 | it->pcache.tree_size = size; | |
3f9ac8d2 JH |
927 | } |
928 | ||
c323ac7d LT |
929 | static void check_object(struct object_entry *entry) |
930 | { | |
36e4d74a JH |
931 | char type[20]; |
932 | ||
7a979d99 | 933 | if (entry->in_pack && !entry->preferred_base) { |
780e6e73 NP |
934 | struct packed_git *p = entry->in_pack; |
935 | unsigned long left = p->pack_size - entry->in_pack_offset; | |
936 | unsigned long size, used; | |
937 | unsigned char *buf; | |
938 | struct object_entry *base_entry = NULL; | |
939 | ||
940 | use_packed_git(p); | |
941 | buf = p->pack_base; | |
942 | buf += entry->in_pack_offset; | |
ab7cd7bb JH |
943 | |
944 | /* We want in_pack_type even if we do not reuse delta. | |
945 | * There is no point not reusing non-delta representations. | |
946 | */ | |
780e6e73 NP |
947 | used = unpack_object_header_gently(buf, left, |
948 | &entry->in_pack_type, &size); | |
949 | if (!used || left - used <= 20) | |
950 | die("corrupt pack for %s", sha1_to_hex(entry->sha1)); | |
ab7cd7bb | 951 | |
3f9ac8d2 JH |
952 | /* Check if it is delta, and the base is also an object |
953 | * we are going to pack. If so we will reuse the existing | |
954 | * delta. | |
955 | */ | |
780e6e73 NP |
956 | if (!no_reuse_delta) { |
957 | unsigned char c, *base_name; | |
958 | unsigned long ofs; | |
959 | /* there is at least 20 bytes left in the pack */ | |
960 | switch (entry->in_pack_type) { | |
961 | case OBJ_REF_DELTA: | |
962 | base_name = buf + used; | |
963 | used += 20; | |
964 | break; | |
965 | case OBJ_OFS_DELTA: | |
966 | c = buf[used++]; | |
967 | ofs = c & 127; | |
968 | while (c & 128) { | |
969 | ofs += 1; | |
970 | if (!ofs || ofs & ~(~0UL >> 7)) | |
971 | die("delta base offset overflow in pack for %s", | |
972 | sha1_to_hex(entry->sha1)); | |
973 | c = buf[used++]; | |
974 | ofs = (ofs << 7) + (c & 127); | |
975 | } | |
976 | if (ofs >= entry->in_pack_offset) | |
977 | die("delta base offset out of bound for %s", | |
978 | sha1_to_hex(entry->sha1)); | |
979 | ofs = entry->in_pack_offset - ofs; | |
980 | base_name = find_packed_object_name(p, ofs); | |
981 | break; | |
982 | default: | |
983 | base_name = NULL; | |
984 | } | |
985 | if (base_name) | |
986 | base_entry = locate_object_entry(base_name); | |
987 | } | |
988 | unuse_packed_git(p); | |
989 | entry->in_pack_header_size = used; | |
990 | ||
a2700696 | 991 | if (base_entry) { |
ab7cd7bb JH |
992 | |
993 | /* Depth value does not matter - find_deltas() | |
994 | * will never consider reused delta as the | |
995 | * base object to deltify other objects | |
996 | * against, in order to avoid circular deltas. | |
3f9ac8d2 | 997 | */ |
ab7cd7bb JH |
998 | |
999 | /* uncompressed size of the delta data */ | |
780e6e73 | 1000 | entry->size = size; |
3f9ac8d2 | 1001 | entry->delta = base_entry; |
780e6e73 | 1002 | entry->type = entry->in_pack_type; |
ab7cd7bb | 1003 | |
15b4d577 JH |
1004 | entry->delta_sibling = base_entry->delta_child; |
1005 | base_entry->delta_child = entry; | |
ab7cd7bb | 1006 | |
3f9ac8d2 JH |
1007 | return; |
1008 | } | |
1009 | /* Otherwise we would do the usual */ | |
36e4d74a | 1010 | } |
3f9ac8d2 JH |
1011 | |
1012 | if (sha1_object_info(entry->sha1, type, &entry->size)) | |
36e4d74a JH |
1013 | die("unable to get type of object %s", |
1014 | sha1_to_hex(entry->sha1)); | |
3f9ac8d2 | 1015 | |
8e440259 | 1016 | if (!strcmp(type, commit_type)) { |
3f9ac8d2 | 1017 | entry->type = OBJ_COMMIT; |
8e440259 | 1018 | } else if (!strcmp(type, tree_type)) { |
3f9ac8d2 | 1019 | entry->type = OBJ_TREE; |
8e440259 | 1020 | } else if (!strcmp(type, blob_type)) { |
3f9ac8d2 | 1021 | entry->type = OBJ_BLOB; |
8e440259 | 1022 | } else if (!strcmp(type, tag_type)) { |
3f9ac8d2 JH |
1023 | entry->type = OBJ_TAG; |
1024 | } else | |
1025 | die("unable to pack object %s of type %s", | |
1026 | sha1_to_hex(entry->sha1), type); | |
1027 | } | |
1028 | ||
15b4d577 JH |
1029 | static unsigned int check_delta_limit(struct object_entry *me, unsigned int n) |
1030 | { | |
1031 | struct object_entry *child = me->delta_child; | |
1032 | unsigned int m = n; | |
1033 | while (child) { | |
1034 | unsigned int c = check_delta_limit(child, n + 1); | |
1035 | if (m < c) | |
1036 | m = c; | |
1037 | child = child->delta_sibling; | |
1038 | } | |
1039 | return m; | |
1040 | } | |
1041 | ||
c323ac7d LT |
1042 | static void get_object_details(void) |
1043 | { | |
1044 | int i; | |
15b4d577 | 1045 | struct object_entry *entry; |
c323ac7d | 1046 | |
3f9ac8d2 | 1047 | prepare_pack_ix(); |
15b4d577 JH |
1048 | for (i = 0, entry = objects; i < nr_objects; i++, entry++) |
1049 | check_object(entry); | |
b76f6b62 JH |
1050 | |
1051 | if (nr_objects == nr_result) { | |
1052 | /* | |
1053 | * Depth of objects that depend on the entry -- this | |
1054 | * is subtracted from depth-max to break too deep | |
1055 | * delta chain because of delta data reusing. | |
1056 | * However, we loosen this restriction when we know we | |
1057 | * are creating a thin pack -- it will have to be | |
1058 | * expanded on the other end anyway, so do not | |
1059 | * artificially cut the delta chain and let it go as | |
1060 | * deep as it wants. | |
1061 | */ | |
1062 | for (i = 0, entry = objects; i < nr_objects; i++, entry++) | |
1063 | if (!entry->delta && entry->delta_child) | |
1064 | entry->delta_limit = | |
1065 | check_delta_limit(entry, 1); | |
1066 | } | |
c323ac7d LT |
1067 | } |
1068 | ||
1069 | typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *); | |
1070 | ||
1071 | static entry_sort_t current_sort; | |
1072 | ||
1073 | static int sort_comparator(const void *_a, const void *_b) | |
1074 | { | |
1075 | struct object_entry *a = *(struct object_entry **)_a; | |
1076 | struct object_entry *b = *(struct object_entry **)_b; | |
1077 | return current_sort(a,b); | |
1078 | } | |
1079 | ||
1080 | static struct object_entry **create_sorted_list(entry_sort_t sort) | |
1081 | { | |
1082 | struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *)); | |
1083 | int i; | |
1084 | ||
1085 | for (i = 0; i < nr_objects; i++) | |
1086 | list[i] = objects + i; | |
1087 | current_sort = sort; | |
1088 | qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator); | |
1089 | return list; | |
1090 | } | |
1091 | ||
1092 | static int sha1_sort(const struct object_entry *a, const struct object_entry *b) | |
1093 | { | |
a89fccd2 | 1094 | return hashcmp(a->sha1, b->sha1); |
c323ac7d LT |
1095 | } |
1096 | ||
962554c6 | 1097 | static struct object_entry **create_final_object_list(void) |
7a979d99 JH |
1098 | { |
1099 | struct object_entry **list; | |
1100 | int i, j; | |
1101 | ||
1102 | for (i = nr_result = 0; i < nr_objects; i++) | |
1103 | if (!objects[i].preferred_base) | |
1104 | nr_result++; | |
1105 | list = xmalloc(nr_result * sizeof(struct object_entry *)); | |
1106 | for (i = j = 0; i < nr_objects; i++) { | |
1107 | if (!objects[i].preferred_base) | |
1108 | list[j++] = objects + i; | |
1109 | } | |
1110 | current_sort = sha1_sort; | |
1111 | qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator); | |
1112 | return list; | |
1113 | } | |
1114 | ||
c323ac7d LT |
1115 | static int type_size_sort(const struct object_entry *a, const struct object_entry *b) |
1116 | { | |
1117 | if (a->type < b->type) | |
1118 | return -1; | |
1119 | if (a->type > b->type) | |
1120 | return 1; | |
27225f2e LT |
1121 | if (a->hash < b->hash) |
1122 | return -1; | |
1123 | if (a->hash > b->hash) | |
1124 | return 1; | |
7a979d99 JH |
1125 | if (a->preferred_base < b->preferred_base) |
1126 | return -1; | |
1127 | if (a->preferred_base > b->preferred_base) | |
1128 | return 1; | |
c323ac7d LT |
1129 | if (a->size < b->size) |
1130 | return -1; | |
1131 | if (a->size > b->size) | |
1132 | return 1; | |
1133 | return a < b ? -1 : (a > b); | |
1134 | } | |
1135 | ||
1136 | struct unpacked { | |
1137 | struct object_entry *entry; | |
1138 | void *data; | |
f6c7081a | 1139 | struct delta_index *index; |
c323ac7d LT |
1140 | }; |
1141 | ||
1142 | /* | |
521a4f4c LT |
1143 | * We search for deltas _backwards_ in a list sorted by type and |
1144 | * by size, so that we see progressively smaller and smaller files. | |
1145 | * That's because we prefer deltas to be from the bigger file | |
1146 | * to the smaller - deletes are potentially cheaper, but perhaps | |
1147 | * more importantly, the bigger file is likely the more recent | |
1148 | * one. | |
c323ac7d | 1149 | */ |
f6c7081a | 1150 | static int try_delta(struct unpacked *trg, struct unpacked *src, |
560b25a8 | 1151 | unsigned max_depth) |
c323ac7d | 1152 | { |
f6c7081a NP |
1153 | struct object_entry *trg_entry = trg->entry; |
1154 | struct object_entry *src_entry = src->entry; | |
560b25a8 NP |
1155 | unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz; |
1156 | char type[10]; | |
c323ac7d LT |
1157 | void *delta_buf; |
1158 | ||
1159 | /* Don't bother doing diffs between different types */ | |
f6c7081a | 1160 | if (trg_entry->type != src_entry->type) |
c323ac7d LT |
1161 | return -1; |
1162 | ||
7a979d99 JH |
1163 | /* We do not compute delta to *create* objects we are not |
1164 | * going to pack. | |
1165 | */ | |
f6c7081a | 1166 | if (trg_entry->preferred_base) |
75c42d8c | 1167 | return -1; |
7a979d99 | 1168 | |
51d1e83f LT |
1169 | /* |
1170 | * We do not bother to try a delta that we discarded | |
8dbbd14e | 1171 | * on an earlier try, but only when reusing delta data. |
51d1e83f | 1172 | */ |
8dbbd14e NP |
1173 | if (!no_reuse_delta && trg_entry->in_pack && |
1174 | trg_entry->in_pack == src_entry->in_pack) | |
51d1e83f LT |
1175 | return 0; |
1176 | ||
f6c7081a NP |
1177 | /* |
1178 | * If the current object is at pack edge, take the depth the | |
7a979d99 JH |
1179 | * objects that depend on the current object into account -- |
1180 | * otherwise they would become too deep. | |
ab7cd7bb | 1181 | */ |
f6c7081a NP |
1182 | if (trg_entry->delta_child) { |
1183 | if (max_depth <= trg_entry->delta_limit) | |
15b4d577 | 1184 | return 0; |
f6c7081a | 1185 | max_depth -= trg_entry->delta_limit; |
15b4d577 | 1186 | } |
f6c7081a | 1187 | if (src_entry->depth >= max_depth) |
d116a45a | 1188 | return 0; |
c323ac7d | 1189 | |
c3b06a69 | 1190 | /* Now some size filtering heuristics. */ |
560b25a8 NP |
1191 | trg_size = trg_entry->size; |
1192 | max_size = trg_size/2 - 20; | |
c3b06a69 NP |
1193 | max_size = max_size * (max_depth - src_entry->depth) / max_depth; |
1194 | if (max_size == 0) | |
1195 | return 0; | |
4e8da195 | 1196 | if (trg_entry->delta && trg_entry->delta_size <= max_size) |
f6c7081a NP |
1197 | max_size = trg_entry->delta_size-1; |
1198 | src_size = src_entry->size; | |
560b25a8 | 1199 | sizediff = src_size < trg_size ? trg_size - src_size : 0; |
27225f2e | 1200 | if (sizediff >= max_size) |
f527cb8c | 1201 | return 0; |
f6c7081a | 1202 | |
560b25a8 NP |
1203 | /* Load data if not already done */ |
1204 | if (!trg->data) { | |
1205 | trg->data = read_sha1_file(trg_entry->sha1, type, &sz); | |
1206 | if (sz != trg_size) | |
1207 | die("object %s inconsistent object length (%lu vs %lu)", | |
1208 | sha1_to_hex(trg_entry->sha1), sz, trg_size); | |
1209 | } | |
1210 | if (!src->data) { | |
1211 | src->data = read_sha1_file(src_entry->sha1, type, &sz); | |
1212 | if (sz != src_size) | |
1213 | die("object %s inconsistent object length (%lu vs %lu)", | |
1214 | sha1_to_hex(src_entry->sha1), sz, src_size); | |
1215 | } | |
1216 | if (!src->index) { | |
1217 | src->index = create_delta_index(src->data, src_size); | |
1218 | if (!src->index) | |
1219 | die("out of memory"); | |
1220 | } | |
1221 | ||
1222 | delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size); | |
c323ac7d | 1223 | if (!delta_buf) |
75c42d8c | 1224 | return 0; |
f6c7081a NP |
1225 | |
1226 | trg_entry->delta = src_entry; | |
1227 | trg_entry->delta_size = delta_size; | |
1228 | trg_entry->depth = src_entry->depth + 1; | |
c323ac7d | 1229 | free(delta_buf); |
f6c7081a | 1230 | return 1; |
c323ac7d LT |
1231 | } |
1232 | ||
b2504a0d NP |
1233 | static void progress_interval(int signum) |
1234 | { | |
b2504a0d NP |
1235 | progress_update = 1; |
1236 | } | |
1237 | ||
d116a45a | 1238 | static void find_deltas(struct object_entry **list, int window, int depth) |
c323ac7d | 1239 | { |
521a4f4c | 1240 | int i, idx; |
c323ac7d LT |
1241 | unsigned int array_size = window * sizeof(struct unpacked); |
1242 | struct unpacked *array = xmalloc(array_size); | |
183bdb2c JH |
1243 | unsigned processed = 0; |
1244 | unsigned last_percent = 999; | |
c323ac7d LT |
1245 | |
1246 | memset(array, 0, array_size); | |
521a4f4c LT |
1247 | i = nr_objects; |
1248 | idx = 0; | |
183bdb2c | 1249 | if (progress) |
f0b0af1b | 1250 | fprintf(stderr, "Deltifying %d objects.\n", nr_result); |
21fcd1bd | 1251 | |
521a4f4c | 1252 | while (--i >= 0) { |
c323ac7d LT |
1253 | struct object_entry *entry = list[i]; |
1254 | struct unpacked *n = array + idx; | |
c323ac7d LT |
1255 | int j; |
1256 | ||
f0b0af1b JH |
1257 | if (!entry->preferred_base) |
1258 | processed++; | |
1259 | ||
183bdb2c | 1260 | if (progress) { |
f0b0af1b | 1261 | unsigned percent = processed * 100 / nr_result; |
183bdb2c JH |
1262 | if (percent != last_percent || progress_update) { |
1263 | fprintf(stderr, "%4u%% (%u/%u) done\r", | |
f0b0af1b | 1264 | percent, processed, nr_result); |
183bdb2c JH |
1265 | progress_update = 0; |
1266 | last_percent = percent; | |
1267 | } | |
21fcd1bd | 1268 | } |
3f9ac8d2 JH |
1269 | |
1270 | if (entry->delta) | |
1271 | /* This happens if we decided to reuse existing | |
ab7cd7bb | 1272 | * delta from a pack. "!no_reuse_delta &&" is implied. |
3f9ac8d2 JH |
1273 | */ |
1274 | continue; | |
1275 | ||
9a8b6a0a JH |
1276 | if (entry->size < 50) |
1277 | continue; | |
ff45715c NP |
1278 | free_delta_index(n->index); |
1279 | n->index = NULL; | |
c323ac7d | 1280 | free(n->data); |
560b25a8 | 1281 | n->data = NULL; |
c323ac7d | 1282 | n->entry = entry; |
ab7cd7bb | 1283 | |
78817c15 LT |
1284 | j = window; |
1285 | while (--j > 0) { | |
1286 | unsigned int other_idx = idx + j; | |
c323ac7d | 1287 | struct unpacked *m; |
78817c15 LT |
1288 | if (other_idx >= window) |
1289 | other_idx -= window; | |
c323ac7d LT |
1290 | m = array + other_idx; |
1291 | if (!m->entry) | |
1292 | break; | |
560b25a8 | 1293 | if (try_delta(n, m, depth) < 0) |
c323ac7d LT |
1294 | break; |
1295 | } | |
70ca1a3f JH |
1296 | /* if we made n a delta, and if n is already at max |
1297 | * depth, leaving it in the window is pointless. we | |
1298 | * should evict it first. | |
70ca1a3f JH |
1299 | */ |
1300 | if (entry->delta && depth <= entry->depth) | |
1301 | continue; | |
ff45715c | 1302 | |
521a4f4c LT |
1303 | idx++; |
1304 | if (idx >= window) | |
1305 | idx = 0; | |
c323ac7d | 1306 | } |
adee7bdf | 1307 | |
b2504a0d NP |
1308 | if (progress) |
1309 | fputc('\n', stderr); | |
1310 | ||
f6c7081a | 1311 | for (i = 0; i < window; ++i) { |
ff45715c | 1312 | free_delta_index(array[i].index); |
adee7bdf | 1313 | free(array[i].data); |
f6c7081a | 1314 | } |
adee7bdf | 1315 | free(array); |
c323ac7d LT |
1316 | } |
1317 | ||
f3123c4a JH |
1318 | static void prepare_pack(int window, int depth) |
1319 | { | |
3f9ac8d2 | 1320 | get_object_details(); |
f3123c4a JH |
1321 | sorted_by_type = create_sorted_list(type_size_sort); |
1322 | if (window && depth) | |
1323 | find_deltas(sorted_by_type, window+1, depth); | |
f3123c4a JH |
1324 | } |
1325 | ||
df6d6101 | 1326 | static int reuse_cached_pack(unsigned char *sha1) |
f3123c4a JH |
1327 | { |
1328 | static const char cache[] = "pack-cache/pack-%s.%s"; | |
1329 | char *cached_pack, *cached_idx; | |
1330 | int ifd, ofd, ifd_ix = -1; | |
1331 | ||
1332 | cached_pack = git_path(cache, sha1_to_hex(sha1), "pack"); | |
1333 | ifd = open(cached_pack, O_RDONLY); | |
1334 | if (ifd < 0) | |
1335 | return 0; | |
1336 | ||
1337 | if (!pack_to_stdout) { | |
1338 | cached_idx = git_path(cache, sha1_to_hex(sha1), "idx"); | |
1339 | ifd_ix = open(cached_idx, O_RDONLY); | |
1340 | if (ifd_ix < 0) { | |
1341 | close(ifd); | |
1342 | return 0; | |
1343 | } | |
1344 | } | |
1345 | ||
ab7cd7bb JH |
1346 | if (progress) |
1347 | fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects, | |
1348 | sha1_to_hex(sha1)); | |
f3123c4a JH |
1349 | |
1350 | if (pack_to_stdout) { | |
1351 | if (copy_fd(ifd, 1)) | |
1352 | exit(1); | |
1353 | close(ifd); | |
1354 | } | |
1355 | else { | |
1356 | char name[PATH_MAX]; | |
1357 | snprintf(name, sizeof(name), | |
1358 | "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack"); | |
1359 | ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
1360 | if (ofd < 0) | |
1361 | die("unable to open %s (%s)", name, strerror(errno)); | |
1362 | if (copy_fd(ifd, ofd)) | |
1363 | exit(1); | |
1364 | close(ifd); | |
1365 | ||
1366 | snprintf(name, sizeof(name), | |
1367 | "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx"); | |
1368 | ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
1369 | if (ofd < 0) | |
1370 | die("unable to open %s (%s)", name, strerror(errno)); | |
1371 | if (copy_fd(ifd_ix, ofd)) | |
1372 | exit(1); | |
1373 | close(ifd_ix); | |
1374 | puts(sha1_to_hex(sha1)); | |
1375 | } | |
1376 | ||
1377 | return 1; | |
1378 | } | |
1379 | ||
fb7a6531 LT |
1380 | static void setup_progress_signal(void) |
1381 | { | |
1382 | struct sigaction sa; | |
1383 | struct itimerval v; | |
1384 | ||
1385 | memset(&sa, 0, sizeof(sa)); | |
1386 | sa.sa_handler = progress_interval; | |
1387 | sigemptyset(&sa.sa_mask); | |
1388 | sa.sa_flags = SA_RESTART; | |
1389 | sigaction(SIGALRM, &sa, NULL); | |
1390 | ||
1391 | v.it_interval.tv_sec = 1; | |
1392 | v.it_interval.tv_usec = 0; | |
1393 | v.it_value = v.it_interval; | |
1394 | setitimer(ITIMER_REAL, &v, NULL); | |
1395 | } | |
1396 | ||
4812a93a JK |
1397 | static int git_pack_config(const char *k, const char *v) |
1398 | { | |
1399 | if(!strcmp(k, "pack.window")) { | |
1400 | window = git_config_int(k, v); | |
1401 | return 0; | |
1402 | } | |
1403 | return git_default_config(k, v); | |
1404 | } | |
1405 | ||
b5d97e6b | 1406 | static void read_object_list_from_stdin(void) |
c323ac7d | 1407 | { |
b5d97e6b JH |
1408 | char line[40 + 1 + PATH_MAX + 2]; |
1409 | unsigned char sha1[20]; | |
1410 | unsigned hash; | |
b2504a0d | 1411 | |
da93d12b | 1412 | for (;;) { |
da93d12b LT |
1413 | if (!fgets(line, sizeof(line), stdin)) { |
1414 | if (feof(stdin)) | |
1415 | break; | |
1416 | if (!ferror(stdin)) | |
1417 | die("fgets returned NULL, not EOF, not error!"); | |
687dd75c JH |
1418 | if (errno != EINTR) |
1419 | die("fgets: %s", strerror(errno)); | |
1420 | clearerr(stdin); | |
1421 | continue; | |
da93d12b | 1422 | } |
7a979d99 JH |
1423 | if (line[0] == '-') { |
1424 | if (get_sha1_hex(line+1, sha1)) | |
1425 | die("expected edge sha1, got garbage:\n %s", | |
b5d97e6b | 1426 | line); |
8d1d8f83 | 1427 | add_preferred_base(sha1); |
7a979d99 | 1428 | continue; |
21fcd1bd | 1429 | } |
c323ac7d | 1430 | if (get_sha1_hex(line, sha1)) |
ef07618f | 1431 | die("expected sha1, got garbage:\n %s", line); |
b5d97e6b | 1432 | |
ce0bd642 | 1433 | hash = name_hash(line+41); |
5379a5c5 JH |
1434 | add_preferred_base_object(line+41, hash); |
1435 | add_object_entry(sha1, hash, 0); | |
c323ac7d | 1436 | } |
b5d97e6b JH |
1437 | } |
1438 | ||
b5d97e6b JH |
1439 | static void show_commit(struct commit *commit) |
1440 | { | |
1441 | unsigned hash = name_hash(""); | |
8d1d8f83 | 1442 | add_preferred_base_object("", hash); |
b5d97e6b JH |
1443 | add_object_entry(commit->object.sha1, hash, 0); |
1444 | } | |
1445 | ||
1446 | static void show_object(struct object_array_entry *p) | |
1447 | { | |
1448 | unsigned hash = name_hash(p->name); | |
8d1d8f83 | 1449 | add_preferred_base_object(p->name, hash); |
b5d97e6b JH |
1450 | add_object_entry(p->item->sha1, hash, 0); |
1451 | } | |
1452 | ||
8d1d8f83 JH |
1453 | static void show_edge(struct commit *commit) |
1454 | { | |
1455 | add_preferred_base(commit->object.sha1); | |
1456 | } | |
1457 | ||
1458 | static void get_object_list(int ac, const char **av) | |
b5d97e6b JH |
1459 | { |
1460 | struct rev_info revs; | |
1461 | char line[1000]; | |
b5d97e6b JH |
1462 | int flags = 0; |
1463 | ||
b5d97e6b JH |
1464 | init_revisions(&revs, NULL); |
1465 | save_commit_buffer = 0; | |
1466 | track_object_refs = 0; | |
1467 | setup_revisions(ac, av, &revs, NULL); | |
1468 | ||
b5d97e6b JH |
1469 | while (fgets(line, sizeof(line), stdin) != NULL) { |
1470 | int len = strlen(line); | |
1471 | if (line[len - 1] == '\n') | |
1472 | line[--len] = 0; | |
1473 | if (!len) | |
1474 | break; | |
1475 | if (*line == '-') { | |
1476 | if (!strcmp(line, "--not")) { | |
1477 | flags ^= UNINTERESTING; | |
1478 | continue; | |
1479 | } | |
1480 | die("not a rev '%s'", line); | |
1481 | } | |
1482 | if (handle_revision_arg(line, &revs, flags, 1)) | |
1483 | die("bad revision '%s'", line); | |
1484 | } | |
1485 | ||
1486 | prepare_revision_walk(&revs); | |
8d1d8f83 | 1487 | mark_edges_uninteresting(revs.commits, &revs, show_edge); |
b5d97e6b JH |
1488 | traverse_commit_list(&revs, show_commit, show_object); |
1489 | } | |
1490 | ||
1491 | int cmd_pack_objects(int argc, const char **argv, const char *prefix) | |
1492 | { | |
1493 | SHA_CTX ctx; | |
1494 | int depth = 10; | |
1495 | struct object_entry **list; | |
1496 | int use_internal_rev_list = 0; | |
8d1d8f83 | 1497 | int thin = 0; |
b5d97e6b | 1498 | int i; |
8d1d8f83 JH |
1499 | const char *rp_av[64]; |
1500 | int rp_ac; | |
1501 | ||
1502 | rp_av[0] = "pack-objects"; | |
1503 | rp_av[1] = "--objects"; /* --thin will make it --objects-edge */ | |
1504 | rp_ac = 2; | |
b5d97e6b JH |
1505 | |
1506 | git_config(git_pack_config); | |
1507 | ||
1508 | progress = isatty(2); | |
1509 | for (i = 1; i < argc; i++) { | |
1510 | const char *arg = argv[i]; | |
1511 | ||
1512 | if (*arg != '-') | |
1513 | break; | |
1514 | ||
1515 | if (!strcmp("--non-empty", arg)) { | |
1516 | non_empty = 1; | |
1517 | continue; | |
1518 | } | |
1519 | if (!strcmp("--local", arg)) { | |
1520 | local = 1; | |
1521 | continue; | |
1522 | } | |
1523 | if (!strcmp("--progress", arg)) { | |
1524 | progress = 1; | |
1525 | continue; | |
1526 | } | |
1527 | if (!strcmp("--incremental", arg)) { | |
1528 | incremental = 1; | |
1529 | continue; | |
1530 | } | |
1531 | if (!strncmp("--window=", arg, 9)) { | |
1532 | char *end; | |
1533 | window = strtoul(arg+9, &end, 0); | |
1534 | if (!arg[9] || *end) | |
1535 | usage(pack_usage); | |
1536 | continue; | |
1537 | } | |
1538 | if (!strncmp("--depth=", arg, 8)) { | |
1539 | char *end; | |
1540 | depth = strtoul(arg+8, &end, 0); | |
1541 | if (!arg[8] || *end) | |
1542 | usage(pack_usage); | |
1543 | continue; | |
1544 | } | |
1545 | if (!strcmp("--progress", arg)) { | |
1546 | progress = 1; | |
1547 | continue; | |
1548 | } | |
1549 | if (!strcmp("-q", arg)) { | |
1550 | progress = 0; | |
1551 | continue; | |
1552 | } | |
1553 | if (!strcmp("--no-reuse-delta", arg)) { | |
1554 | no_reuse_delta = 1; | |
1555 | continue; | |
1556 | } | |
be6b1914 | 1557 | if (!strcmp("--delta-base-offset", arg)) { |
780e6e73 | 1558 | allow_ofs_delta = 1; |
be6b1914 NP |
1559 | continue; |
1560 | } | |
b5d97e6b JH |
1561 | if (!strcmp("--stdout", arg)) { |
1562 | pack_to_stdout = 1; | |
1563 | continue; | |
1564 | } | |
1565 | if (!strcmp("--revs", arg)) { | |
1566 | use_internal_rev_list = 1; | |
1567 | continue; | |
1568 | } | |
8d1d8f83 JH |
1569 | if (!strcmp("--unpacked", arg) || |
1570 | !strncmp("--unpacked=", arg, 11) || | |
1571 | !strcmp("--all", arg)) { | |
1572 | use_internal_rev_list = 1; | |
1573 | if (ARRAY_SIZE(rp_av) - 1 <= rp_ac) | |
1574 | die("too many internal rev-list options"); | |
1575 | rp_av[rp_ac++] = arg; | |
b5d97e6b JH |
1576 | continue; |
1577 | } | |
8d1d8f83 JH |
1578 | if (!strcmp("--thin", arg)) { |
1579 | use_internal_rev_list = 1; | |
1580 | thin = 1; | |
1581 | rp_av[1] = "--objects-edge"; | |
b5d97e6b JH |
1582 | continue; |
1583 | } | |
1584 | usage(pack_usage); | |
1585 | } | |
1586 | ||
1587 | /* Traditionally "pack-objects [options] base extra" failed; | |
1588 | * we would however want to take refs parameter that would | |
1589 | * have been given to upstream rev-list ourselves, which means | |
1590 | * we somehow want to say what the base name is. So the | |
1591 | * syntax would be: | |
1592 | * | |
1593 | * pack-objects [options] base <refs...> | |
1594 | * | |
1595 | * in other words, we would treat the first non-option as the | |
1596 | * base_name and send everything else to the internal revision | |
1597 | * walker. | |
1598 | */ | |
1599 | ||
1600 | if (!pack_to_stdout) | |
1601 | base_name = argv[i++]; | |
1602 | ||
1603 | if (pack_to_stdout != !base_name) | |
1604 | usage(pack_usage); | |
1605 | ||
8d1d8f83 JH |
1606 | if (!pack_to_stdout && thin) |
1607 | die("--thin cannot be used to build an indexable pack."); | |
b5d97e6b JH |
1608 | |
1609 | prepare_packed_git(); | |
1610 | ||
1611 | if (progress) { | |
1612 | fprintf(stderr, "Generating pack...\n"); | |
1613 | setup_progress_signal(); | |
1614 | } | |
1615 | ||
1616 | if (!use_internal_rev_list) | |
1617 | read_object_list_from_stdin(); | |
8d1d8f83 JH |
1618 | else { |
1619 | rp_av[rp_ac] = NULL; | |
1620 | get_object_list(rp_ac, rp_av); | |
1621 | } | |
b5d97e6b | 1622 | |
21fcd1bd JH |
1623 | if (progress) |
1624 | fprintf(stderr, "Done counting %d objects.\n", nr_objects); | |
f0b0af1b JH |
1625 | sorted_by_sha = create_final_object_list(); |
1626 | if (non_empty && !nr_result) | |
1c4a2912 | 1627 | return 0; |
c323ac7d | 1628 | |
84c8d8ae JH |
1629 | SHA1_Init(&ctx); |
1630 | list = sorted_by_sha; | |
7a979d99 | 1631 | for (i = 0; i < nr_result; i++) { |
84c8d8ae JH |
1632 | struct object_entry *entry = *list++; |
1633 | SHA1_Update(&ctx, entry->sha1, 20); | |
1634 | } | |
1635 | SHA1_Final(object_list_sha1, &ctx); | |
7a979d99 JH |
1636 | if (progress && (nr_objects != nr_result)) |
1637 | fprintf(stderr, "Result has %d objects.\n", nr_result); | |
84c8d8ae | 1638 | |
df6d6101 | 1639 | if (reuse_cached_pack(object_list_sha1)) |
f3123c4a JH |
1640 | ; |
1641 | else { | |
f0b0af1b JH |
1642 | if (nr_result) |
1643 | prepare_pack(window, depth); | |
5e8dc750 NP |
1644 | if (progress && pack_to_stdout) { |
1645 | /* the other end usually displays progress itself */ | |
1646 | struct itimerval v = {{0,},}; | |
1647 | setitimer(ITIMER_REAL, &v, NULL); | |
1648 | signal(SIGALRM, SIG_IGN ); | |
1649 | progress_update = 0; | |
1650 | } | |
1651 | write_pack_file(); | |
f3123c4a JH |
1652 | if (!pack_to_stdout) { |
1653 | write_index_file(); | |
1654 | puts(sha1_to_hex(object_list_sha1)); | |
1655 | } | |
5f3de58f | 1656 | } |
ab7cd7bb JH |
1657 | if (progress) |
1658 | fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n", | |
7a979d99 | 1659 | nr_result, written, written_delta, reused, reused_delta); |
c323ac7d LT |
1660 | return 0; |
1661 | } |