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