]>
Commit | Line | Data |
---|---|---|
5d4a6003 | 1 | #include "builtin.h" |
c323ac7d | 2 | #include "cache.h" |
a74db82e | 3 | #include "attr.h" |
c323ac7d | 4 | #include "object.h" |
8e440259 PE |
5 | #include "blob.h" |
6 | #include "commit.h" | |
7 | #include "tag.h" | |
8 | #include "tree.h" | |
c323ac7d | 9 | #include "delta.h" |
a733cb60 | 10 | #include "pack.h" |
c38138cd | 11 | #include "csum-file.h" |
1b0c7174 | 12 | #include "tree-walk.h" |
b5d97e6b JH |
13 | #include "diff.h" |
14 | #include "revision.h" | |
15 | #include "list-objects.h" | |
96a02f8f | 16 | #include "progress.h" |
c323ac7d | 17 | |
8ecce684 NP |
18 | #ifdef THREADED_DELTA_SEARCH |
19 | #include <pthread.h> | |
20 | #endif | |
21 | ||
231f240b | 22 | static const char pack_usage[] = "\ |
a97773ce BD |
23 | git-pack-objects [{ -q | --progress | --all-progress }] \n\ |
24 | [--max-pack-size=N] [--local] [--incremental] \n\ | |
25 | [--window=N] [--window-memory=N] [--depth=N] \n\ | |
fa736f72 | 26 | [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset] \n\ |
367f4a43 | 27 | [--threads=N] [--non-empty] [--revs [--unpacked | --all]*] [--reflog] \n\ |
08cdfb13 | 28 | [--stdout | base-name] [--keep-unreachable] [<ref-list | <object-list]"; |
c323ac7d | 29 | |
c323ac7d | 30 | struct object_entry { |
aa7e44bf | 31 | struct pack_idx_entry idx; |
3f9ac8d2 | 32 | unsigned long size; /* uncompressed size */ |
3f9ac8d2 | 33 | struct packed_git *in_pack; /* already in pack */ |
6777a59f | 34 | off_t in_pack_offset; |
a3fbf4df | 35 | struct object_entry *delta; /* delta base object */ |
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 | */ | |
074b2eea | 40 | void *delta_data; /* cached delta (uncompressed) */ |
a3fbf4df | 41 | unsigned long delta_size; /* delta data size (uncompressed) */ |
5a235b5e | 42 | unsigned int hash; /* name hint hash */ |
a3fbf4df NP |
43 | enum object_type type; |
44 | enum object_type in_pack_type; /* could be delta */ | |
45 | unsigned char in_pack_header_size; | |
46 | unsigned char preferred_base; /* we do not pack this, but is available | |
a74db82e | 47 | * to be used as the base object to delta |
a3fbf4df NP |
48 | * objects against. |
49 | */ | |
a74db82e | 50 | unsigned char no_try_delta; |
c323ac7d LT |
51 | }; |
52 | ||
3f9ac8d2 | 53 | /* |
82e5a82f | 54 | * Objects we are going to pack are collected in objects array (dynamically |
3f9ac8d2 JH |
55 | * expanded). nr_objects & nr_alloc controls this array. They are stored |
56 | * in the order we see -- typically rev-list --objects order that gives us | |
57 | * nice "minimum seek" order. | |
3f9ac8d2 | 58 | */ |
e4d58311 | 59 | static struct object_entry *objects; |
d01fb92f DH |
60 | static struct object_entry **written_list; |
61 | static uint32_t nr_objects, nr_alloc, nr_result, nr_written; | |
3f9ac8d2 | 62 | |
96f1e58f | 63 | static int non_empty; |
08cdfb13 | 64 | static int no_reuse_delta, no_reuse_object, keep_unreachable; |
96f1e58f DR |
65 | static int local; |
66 | static int incremental; | |
be6b1914 | 67 | static int allow_ofs_delta; |
d01fb92f | 68 | static const char *base_name; |
024701f1 | 69 | static int progress = 1; |
4812a93a | 70 | static int window = 10; |
d01fb92f | 71 | static uint32_t pack_size_limit; |
618e613a | 72 | static int depth = 50; |
367f4a43 | 73 | static int delta_search_threads = 1; |
df6d6101 | 74 | static int pack_to_stdout; |
8d1d8f83 | 75 | static int num_preferred_base; |
dc6a0757 | 76 | static struct progress *progress_state; |
960ccca6 DH |
77 | static int pack_compression_level = Z_DEFAULT_COMPRESSION; |
78 | static int pack_compression_seen; | |
c323ac7d | 79 | |
074b2eea MK |
80 | static unsigned long delta_cache_size = 0; |
81 | static unsigned long max_delta_cache_size = 0; | |
e3dfddb3 | 82 | static unsigned long cache_max_small_delta_size = 1000; |
074b2eea | 83 | |
a97773ce BD |
84 | static unsigned long window_memory_limit = 0; |
85 | ||
3f9ac8d2 JH |
86 | /* |
87 | * The object names in objects array are hashed with this hashtable, | |
e4d58311 | 88 | * to help looking up the entry by object name. |
3f9ac8d2 JH |
89 | * This hashtable is built after all the objects are seen. |
90 | */ | |
96f1e58f DR |
91 | static int *object_ix; |
92 | static int object_ix_hashsz; | |
3f9ac8d2 JH |
93 | |
94 | /* | |
95 | * Pack index for existing packs give us easy access to the offsets into | |
96 | * corresponding pack file where each object's data starts, but the entries | |
97 | * do not store the size of the compressed representation (uncompressed | |
780e6e73 NP |
98 | * size is easily available by examining the pack entry header). It is |
99 | * also rather expensive to find the sha1 for an object given its offset. | |
100 | * | |
101 | * We build a hashtable of existing packs (pack_revindex), and keep reverse | |
102 | * index here -- pack index file is sorted by object name mapping to offset; | |
103 | * this pack_revindex[].revindex array is a list of offset/index_nr pairs | |
104 | * ordered by offset, so if you know the offset of an object, next offset | |
105 | * is where its packed representation ends and the index_nr can be used to | |
106 | * get the object sha1 from the main index. | |
3f9ac8d2 | 107 | */ |
780e6e73 | 108 | struct revindex_entry { |
6777a59f | 109 | off_t offset; |
780e6e73 NP |
110 | unsigned int nr; |
111 | }; | |
3f9ac8d2 JH |
112 | struct pack_revindex { |
113 | struct packed_git *p; | |
780e6e73 NP |
114 | struct revindex_entry *revindex; |
115 | }; | |
116 | static struct pack_revindex *pack_revindex; | |
96f1e58f | 117 | static int pack_revindex_hashsz; |
3f9ac8d2 JH |
118 | |
119 | /* | |
120 | * stats | |
121 | */ | |
7cadf491 SP |
122 | static uint32_t written, written_delta; |
123 | static uint32_t reused, reused_delta; | |
3f9ac8d2 JH |
124 | |
125 | static int pack_revindex_ix(struct packed_git *p) | |
126 | { | |
2b74cffa | 127 | unsigned long ui = (unsigned long)p; |
3f9ac8d2 JH |
128 | int i; |
129 | ||
130 | ui = ui ^ (ui >> 16); /* defeat structure alignment */ | |
131 | i = (int)(ui % pack_revindex_hashsz); | |
132 | while (pack_revindex[i].p) { | |
133 | if (pack_revindex[i].p == p) | |
134 | return i; | |
135 | if (++i == pack_revindex_hashsz) | |
136 | i = 0; | |
137 | } | |
138 | return -1 - i; | |
139 | } | |
140 | ||
141 | static void prepare_pack_ix(void) | |
142 | { | |
143 | int num; | |
144 | struct packed_git *p; | |
145 | for (num = 0, p = packed_git; p; p = p->next) | |
146 | num++; | |
147 | if (!num) | |
148 | return; | |
149 | pack_revindex_hashsz = num * 11; | |
150 | pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz); | |
151 | for (p = packed_git; p; p = p->next) { | |
152 | num = pack_revindex_ix(p); | |
153 | num = - 1 - num; | |
154 | pack_revindex[num].p = p; | |
155 | } | |
156 | /* revindex elements are lazily initialized */ | |
157 | } | |
158 | ||
159 | static int cmp_offset(const void *a_, const void *b_) | |
160 | { | |
780e6e73 NP |
161 | const struct revindex_entry *a = a_; |
162 | const struct revindex_entry *b = b_; | |
163 | return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0; | |
3f9ac8d2 JH |
164 | } |
165 | ||
166 | /* | |
167 | * Ordered list of offsets of objects in the pack. | |
168 | */ | |
169 | static void prepare_pack_revindex(struct pack_revindex *rix) | |
170 | { | |
171 | struct packed_git *p = rix->p; | |
57059091 | 172 | int num_ent = p->num_objects; |
3f9ac8d2 | 173 | int i; |
42873078 | 174 | const char *index = p->index_data; |
3f9ac8d2 | 175 | |
780e6e73 | 176 | rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1)); |
c553ca25 NP |
177 | index += 4 * 256; |
178 | ||
179 | if (p->index_version > 1) { | |
180 | const uint32_t *off_32 = | |
181 | (uint32_t *)(index + 8 + p->num_objects * (20 + 4)); | |
182 | const uint32_t *off_64 = off_32 + p->num_objects; | |
183 | for (i = 0; i < num_ent; i++) { | |
184 | uint32_t off = ntohl(*off_32++); | |
185 | if (!(off & 0x80000000)) { | |
186 | rix->revindex[i].offset = off; | |
187 | } else { | |
188 | rix->revindex[i].offset = | |
189 | ((uint64_t)ntohl(*off_64++)) << 32; | |
190 | rix->revindex[i].offset |= | |
191 | ntohl(*off_64++); | |
192 | } | |
193 | rix->revindex[i].nr = i; | |
194 | } | |
195 | } else { | |
196 | for (i = 0; i < num_ent; i++) { | |
197 | uint32_t hl = *((uint32_t *)(index + 24 * i)); | |
198 | rix->revindex[i].offset = ntohl(hl); | |
199 | rix->revindex[i].nr = i; | |
200 | } | |
3f9ac8d2 | 201 | } |
c553ca25 | 202 | |
3f9ac8d2 JH |
203 | /* This knows the pack format -- the 20-byte trailer |
204 | * follows immediately after the last object data. | |
205 | */ | |
780e6e73 NP |
206 | rix->revindex[num_ent].offset = p->pack_size - 20; |
207 | rix->revindex[num_ent].nr = -1; | |
208 | qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset); | |
3f9ac8d2 JH |
209 | } |
210 | ||
780e6e73 | 211 | static struct revindex_entry * find_packed_object(struct packed_git *p, |
6777a59f | 212 | off_t ofs) |
3f9ac8d2 JH |
213 | { |
214 | int num; | |
215 | int lo, hi; | |
216 | struct pack_revindex *rix; | |
780e6e73 | 217 | struct revindex_entry *revindex; |
3f9ac8d2 JH |
218 | num = pack_revindex_ix(p); |
219 | if (num < 0) | |
220 | die("internal error: pack revindex uninitialized"); | |
221 | rix = &pack_revindex[num]; | |
222 | if (!rix->revindex) | |
223 | prepare_pack_revindex(rix); | |
224 | revindex = rix->revindex; | |
225 | lo = 0; | |
57059091 | 226 | hi = p->num_objects + 1; |
3f9ac8d2 JH |
227 | do { |
228 | int mi = (lo + hi) / 2; | |
780e6e73 NP |
229 | if (revindex[mi].offset == ofs) { |
230 | return revindex + mi; | |
3f9ac8d2 | 231 | } |
780e6e73 | 232 | else if (ofs < revindex[mi].offset) |
3f9ac8d2 JH |
233 | hi = mi; |
234 | else | |
235 | lo = mi + 1; | |
236 | } while (lo < hi); | |
237 | die("internal error: pack revindex corrupt"); | |
238 | } | |
239 | ||
42873078 NP |
240 | static const unsigned char *find_packed_object_name(struct packed_git *p, |
241 | off_t ofs) | |
780e6e73 NP |
242 | { |
243 | struct revindex_entry *entry = find_packed_object(p, ofs); | |
d72308e0 | 244 | return nth_packed_object_sha1(p, entry->nr); |
780e6e73 NP |
245 | } |
246 | ||
c323ac7d LT |
247 | static void *delta_against(void *buf, unsigned long size, struct object_entry *entry) |
248 | { | |
249 | unsigned long othersize, delta_size; | |
21666f1a | 250 | enum object_type type; |
aa7e44bf | 251 | void *otherbuf = read_sha1_file(entry->delta->idx.sha1, &type, &othersize); |
c323ac7d LT |
252 | void *delta_buf; |
253 | ||
254 | if (!otherbuf) | |
aa7e44bf | 255 | die("unable to read %s", sha1_to_hex(entry->delta->idx.sha1)); |
8ee378a0 | 256 | delta_buf = diff_delta(otherbuf, othersize, |
dcde55bc | 257 | buf, size, &delta_size, 0); |
75c42d8c | 258 | if (!delta_buf || delta_size != entry->delta_size) |
a6080a0a | 259 | die("delta size changed"); |
c323ac7d LT |
260 | free(buf); |
261 | free(otherbuf); | |
262 | return delta_buf; | |
263 | } | |
264 | ||
a733cb60 LT |
265 | /* |
266 | * The per-object header is a pretty dense thing, which is | |
267 | * - first byte: low four bits are "size", then three bits of "type", | |
268 | * and the high bit is "size continues". | |
269 | * - each byte afterwards: low seven bits are size continuation, | |
270 | * with the high bit being "size continues" | |
271 | */ | |
272 | static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr) | |
273 | { | |
01247d87 | 274 | int n = 1; |
a733cb60 LT |
275 | unsigned char c; |
276 | ||
eb32d236 | 277 | if (type < OBJ_COMMIT || type > OBJ_REF_DELTA) |
a733cb60 LT |
278 | die("bad type %d", type); |
279 | ||
01247d87 LT |
280 | c = (type << 4) | (size & 15); |
281 | size >>= 4; | |
282 | while (size) { | |
a733cb60 | 283 | *hdr++ = c | 0x80; |
01247d87 LT |
284 | c = size & 0x7f; |
285 | size >>= 7; | |
286 | n++; | |
a733cb60 LT |
287 | } |
288 | *hdr = c; | |
289 | return n; | |
290 | } | |
291 | ||
780e6e73 NP |
292 | /* |
293 | * we are going to reuse the existing object data as is. make | |
294 | * sure it is not corrupt. | |
295 | */ | |
079afb18 SP |
296 | static int check_pack_inflate(struct packed_git *p, |
297 | struct pack_window **w_curs, | |
6777a59f SP |
298 | off_t offset, |
299 | off_t len, | |
079afb18 SP |
300 | unsigned long expect) |
301 | { | |
302 | z_stream stream; | |
303 | unsigned char fakebuf[4096], *in; | |
304 | int st; | |
305 | ||
306 | memset(&stream, 0, sizeof(stream)); | |
307 | inflateInit(&stream); | |
308 | do { | |
309 | in = use_pack(p, w_curs, offset, &stream.avail_in); | |
310 | stream.next_in = in; | |
311 | stream.next_out = fakebuf; | |
312 | stream.avail_out = sizeof(fakebuf); | |
313 | st = inflate(&stream, Z_FINISH); | |
314 | offset += stream.next_in - in; | |
315 | } while (st == Z_OK || st == Z_BUF_ERROR); | |
316 | inflateEnd(&stream); | |
317 | return (st == Z_STREAM_END && | |
318 | stream.total_out == expect && | |
319 | stream.total_in == len) ? 0 : -1; | |
320 | } | |
321 | ||
91ecbeca NP |
322 | static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, |
323 | off_t offset, off_t len, unsigned int nr) | |
324 | { | |
325 | const uint32_t *index_crc; | |
326 | uint32_t data_crc = crc32(0, Z_NULL, 0); | |
327 | ||
328 | do { | |
329 | unsigned int avail; | |
330 | void *data = use_pack(p, w_curs, offset, &avail); | |
331 | if (avail > len) | |
332 | avail = len; | |
333 | data_crc = crc32(data_crc, data, avail); | |
334 | offset += avail; | |
335 | len -= avail; | |
336 | } while (len); | |
337 | ||
338 | index_crc = p->index_data; | |
339 | index_crc += 2 + 256 + p->num_objects * (20/4) + nr; | |
340 | ||
341 | return data_crc != ntohl(*index_crc); | |
342 | } | |
343 | ||
079afb18 SP |
344 | static void copy_pack_data(struct sha1file *f, |
345 | struct packed_git *p, | |
346 | struct pack_window **w_curs, | |
6777a59f SP |
347 | off_t offset, |
348 | off_t len) | |
079afb18 SP |
349 | { |
350 | unsigned char *in; | |
351 | unsigned int avail; | |
352 | ||
353 | while (len) { | |
354 | in = use_pack(p, w_curs, offset, &avail); | |
355 | if (avail > len) | |
6777a59f | 356 | avail = (unsigned int)len; |
079afb18 SP |
357 | sha1write(f, in, avail); |
358 | offset += avail; | |
359 | len -= avail; | |
360 | } | |
361 | } | |
362 | ||
d7dd0223 | 363 | static unsigned long write_object(struct sha1file *f, |
17b08f2c DH |
364 | struct object_entry *entry, |
365 | off_t write_offset) | |
c323ac7d LT |
366 | { |
367 | unsigned long size; | |
21666f1a | 368 | enum object_type type; |
3f9ac8d2 | 369 | void *buf; |
a733cb60 | 370 | unsigned char header[10]; |
17b08f2c | 371 | unsigned char dheader[10]; |
6777a59f SP |
372 | unsigned hdrlen; |
373 | off_t datalen; | |
a733cb60 | 374 | enum object_type obj_type; |
ab7cd7bb | 375 | int to_reuse = 0; |
17b08f2c DH |
376 | /* write limit if limited packsize and not first object */ |
377 | unsigned long limit = pack_size_limit && nr_written ? | |
378 | pack_size_limit - write_offset : 0; | |
379 | /* no if no delta */ | |
380 | int usable_delta = !entry->delta ? 0 : | |
381 | /* yes if unlimited packfile */ | |
382 | !pack_size_limit ? 1 : | |
383 | /* no if base written to previous pack */ | |
aa7e44bf | 384 | entry->delta->idx.offset == (off_t)-1 ? 0 : |
17b08f2c DH |
385 | /* otherwise double-check written to this |
386 | * pack, like we do below | |
387 | */ | |
aa7e44bf | 388 | entry->delta->idx.offset ? 1 : 0; |
c323ac7d | 389 | |
78d1e84f NP |
390 | if (!pack_to_stdout) |
391 | crc32_begin(f); | |
392 | ||
a733cb60 | 393 | obj_type = entry->type; |
fa736f72 NP |
394 | if (no_reuse_object) |
395 | to_reuse = 0; /* explicit */ | |
396 | else if (!entry->in_pack) | |
ab7cd7bb | 397 | to_reuse = 0; /* can't reuse what we don't have */ |
780e6e73 | 398 | else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA) |
17b08f2c DH |
399 | /* check_object() decided it for us ... */ |
400 | to_reuse = usable_delta; | |
401 | /* ... but pack split may override that */ | |
ab7cd7bb JH |
402 | else if (obj_type != entry->in_pack_type) |
403 | to_reuse = 0; /* pack has delta which is unusable */ | |
404 | else if (entry->delta) | |
405 | to_reuse = 0; /* we want to pack afresh */ | |
406 | else | |
407 | to_reuse = 1; /* we have it in-pack undeltified, | |
408 | * and we do not need to deltify it. | |
409 | */ | |
410 | ||
df6d6101 | 411 | if (!to_reuse) { |
17b08f2c DH |
412 | z_stream stream; |
413 | unsigned long maxsize; | |
414 | void *out; | |
5476a8ad | 415 | if (!usable_delta) { |
aa7e44bf | 416 | buf = read_sha1_file(entry->idx.sha1, &obj_type, &size); |
5476a8ad | 417 | if (!buf) |
aa7e44bf | 418 | die("unable to read %s", sha1_to_hex(entry->idx.sha1)); |
5476a8ad | 419 | } else if (entry->delta_data) { |
3f9ac8d2 | 420 | size = entry->delta_size; |
5476a8ad NP |
421 | buf = entry->delta_data; |
422 | entry->delta_data = NULL; | |
aa7e44bf | 423 | obj_type = (allow_ofs_delta && entry->delta->idx.offset) ? |
be6b1914 | 424 | OBJ_OFS_DELTA : OBJ_REF_DELTA; |
17b08f2c | 425 | } else { |
aa7e44bf | 426 | buf = read_sha1_file(entry->idx.sha1, &type, &size); |
074b2eea | 427 | if (!buf) |
aa7e44bf | 428 | die("unable to read %s", sha1_to_hex(entry->idx.sha1)); |
5476a8ad NP |
429 | buf = delta_against(buf, size, entry); |
430 | size = entry->delta_size; | |
aa7e44bf | 431 | obj_type = (allow_ofs_delta && entry->delta->idx.offset) ? |
5476a8ad | 432 | OBJ_OFS_DELTA : OBJ_REF_DELTA; |
3f9ac8d2 | 433 | } |
17b08f2c DH |
434 | /* compress the data to store and put compressed length in datalen */ |
435 | memset(&stream, 0, sizeof(stream)); | |
436 | deflateInit(&stream, pack_compression_level); | |
437 | maxsize = deflateBound(&stream, size); | |
438 | out = xmalloc(maxsize); | |
439 | /* Compress it */ | |
440 | stream.next_in = buf; | |
441 | stream.avail_in = size; | |
442 | stream.next_out = out; | |
443 | stream.avail_out = maxsize; | |
444 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
445 | /* nothing */; | |
446 | deflateEnd(&stream); | |
447 | datalen = stream.total_out; | |
448 | deflateEnd(&stream); | |
3f9ac8d2 JH |
449 | /* |
450 | * The object header is a byte of 'type' followed by zero or | |
be6b1914 | 451 | * more bytes of length. |
3f9ac8d2 JH |
452 | */ |
453 | hdrlen = encode_header(obj_type, size, header); | |
3f9ac8d2 | 454 | |
be6b1914 NP |
455 | if (obj_type == OBJ_OFS_DELTA) { |
456 | /* | |
457 | * Deltas with relative base contain an additional | |
458 | * encoding of the relative offset for the delta | |
459 | * base from this object's position in the pack. | |
460 | */ | |
aa7e44bf | 461 | off_t ofs = entry->idx.offset - entry->delta->idx.offset; |
17b08f2c DH |
462 | unsigned pos = sizeof(dheader) - 1; |
463 | dheader[pos] = ofs & 127; | |
be6b1914 | 464 | while (ofs >>= 7) |
17b08f2c DH |
465 | dheader[--pos] = 128 | (--ofs & 127); |
466 | if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { | |
467 | free(out); | |
468 | free(buf); | |
469 | return 0; | |
470 | } | |
471 | sha1write(f, header, hdrlen); | |
472 | sha1write(f, dheader + pos, sizeof(dheader) - pos); | |
473 | hdrlen += sizeof(dheader) - pos; | |
be6b1914 NP |
474 | } else if (obj_type == OBJ_REF_DELTA) { |
475 | /* | |
476 | * Deltas with a base reference contain | |
477 | * an additional 20 bytes for the base sha1. | |
478 | */ | |
17b08f2c DH |
479 | if (limit && hdrlen + 20 + datalen + 20 >= limit) { |
480 | free(out); | |
481 | free(buf); | |
482 | return 0; | |
483 | } | |
484 | sha1write(f, header, hdrlen); | |
aa7e44bf | 485 | sha1write(f, entry->delta->idx.sha1, 20); |
3f9ac8d2 | 486 | hdrlen += 20; |
17b08f2c DH |
487 | } else { |
488 | if (limit && hdrlen + datalen + 20 >= limit) { | |
489 | free(out); | |
490 | free(buf); | |
491 | return 0; | |
492 | } | |
493 | sha1write(f, header, hdrlen); | |
3f9ac8d2 | 494 | } |
17b08f2c DH |
495 | sha1write(f, out, datalen); |
496 | free(out); | |
3f9ac8d2 | 497 | free(buf); |
c323ac7d | 498 | } |
3f9ac8d2 JH |
499 | else { |
500 | struct packed_git *p = entry->in_pack; | |
03e79c88 | 501 | struct pack_window *w_curs = NULL; |
91ecbeca | 502 | struct revindex_entry *revidx; |
6777a59f | 503 | off_t offset; |
3f9ac8d2 | 504 | |
780e6e73 | 505 | if (entry->delta) { |
aa7e44bf | 506 | obj_type = (allow_ofs_delta && entry->delta->idx.offset) ? |
780e6e73 NP |
507 | OBJ_OFS_DELTA : OBJ_REF_DELTA; |
508 | reused_delta++; | |
509 | } | |
510 | hdrlen = encode_header(obj_type, entry->size, header); | |
91ecbeca NP |
511 | offset = entry->in_pack_offset; |
512 | revidx = find_packed_object(p, offset); | |
513 | datalen = revidx[1].offset - offset; | |
514 | if (!pack_to_stdout && p->index_version > 1 && | |
515 | check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) | |
aa7e44bf | 516 | die("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1)); |
91ecbeca NP |
517 | offset += entry->in_pack_header_size; |
518 | datalen -= entry->in_pack_header_size; | |
17b08f2c | 519 | if (obj_type == OBJ_OFS_DELTA) { |
aa7e44bf | 520 | off_t ofs = entry->idx.offset - entry->delta->idx.offset; |
17b08f2c DH |
521 | unsigned pos = sizeof(dheader) - 1; |
522 | dheader[pos] = ofs & 127; | |
523 | while (ofs >>= 7) | |
524 | dheader[--pos] = 128 | (--ofs & 127); | |
525 | if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) | |
526 | return 0; | |
527 | sha1write(f, header, hdrlen); | |
528 | sha1write(f, dheader + pos, sizeof(dheader) - pos); | |
529 | hdrlen += sizeof(dheader) - pos; | |
530 | } else if (obj_type == OBJ_REF_DELTA) { | |
531 | if (limit && hdrlen + 20 + datalen + 20 >= limit) | |
532 | return 0; | |
533 | sha1write(f, header, hdrlen); | |
aa7e44bf | 534 | sha1write(f, entry->delta->idx.sha1, 20); |
17b08f2c DH |
535 | hdrlen += 20; |
536 | } else { | |
537 | if (limit && hdrlen + datalen + 20 >= limit) | |
538 | return 0; | |
539 | sha1write(f, header, hdrlen); | |
540 | } | |
541 | ||
91ecbeca NP |
542 | if (!pack_to_stdout && p->index_version == 1 && |
543 | check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) | |
aa7e44bf | 544 | die("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1)); |
079afb18 | 545 | copy_pack_data(f, p, &w_curs, offset, datalen); |
03e79c88 | 546 | unuse_pack(&w_curs); |
3f9ac8d2 | 547 | reused++; |
a733cb60 | 548 | } |
17b08f2c | 549 | if (usable_delta) |
ab7cd7bb | 550 | written_delta++; |
3f9ac8d2 | 551 | written++; |
78d1e84f | 552 | if (!pack_to_stdout) |
aa7e44bf | 553 | entry->idx.crc32 = crc32_end(f); |
c323ac7d LT |
554 | return hdrlen + datalen; |
555 | } | |
556 | ||
6777a59f | 557 | static off_t write_one(struct sha1file *f, |
9d5ab962 | 558 | struct object_entry *e, |
6777a59f | 559 | off_t offset) |
9d5ab962 | 560 | { |
d7dd0223 NP |
561 | unsigned long size; |
562 | ||
563 | /* offset is non zero if object is written already. */ | |
aa7e44bf | 564 | if (e->idx.offset || e->preferred_base) |
9d5ab962 | 565 | return offset; |
d7dd0223 NP |
566 | |
567 | /* if we are deltified, write out base object first. */ | |
17b08f2c | 568 | if (e->delta) { |
9d5ab962 | 569 | offset = write_one(f, e->delta, offset); |
17b08f2c DH |
570 | if (!offset) |
571 | return 0; | |
572 | } | |
d7dd0223 | 573 | |
aa7e44bf | 574 | e->idx.offset = offset; |
17b08f2c DH |
575 | size = write_object(f, e, offset); |
576 | if (!size) { | |
aa7e44bf | 577 | e->idx.offset = 0; |
17b08f2c DH |
578 | return 0; |
579 | } | |
ebe27b13 | 580 | written_list[nr_written++] = e; |
d7dd0223 NP |
581 | |
582 | /* make sure off_t is sufficiently large not to wrap */ | |
583 | if (offset > offset + size) | |
584 | die("pack too large for current definition of off_t"); | |
585 | return offset + size; | |
9d5ab962 JH |
586 | } |
587 | ||
aa7e44bf | 588 | /* forward declaration for write_pack_file */ |
d01fb92f DH |
589 | static int adjust_perm(const char *path, mode_t mode); |
590 | ||
591 | static void write_pack_file(void) | |
c323ac7d | 592 | { |
ebe27b13 | 593 | uint32_t i = 0, j; |
d22b9290 | 594 | struct sha1file *f; |
ebe27b13 | 595 | off_t offset, offset_one, last_obj_offset = 0; |
a733cb60 | 596 | struct pack_header hdr; |
ebe27b13 DH |
597 | int do_progress = progress >> pack_to_stdout; |
598 | uint32_t nr_remaining = nr_result; | |
81a216a5 | 599 | |
13aaf148 | 600 | if (do_progress) |
dc6a0757 | 601 | progress_state = start_progress("Writing objects", nr_result); |
ebe27b13 | 602 | written_list = xmalloc(nr_objects * sizeof(struct object_entry *)); |
183bdb2c | 603 | |
ebe27b13 | 604 | do { |
aa7e44bf | 605 | unsigned char sha1[20]; |
7ba502c4 | 606 | char *pack_tmp_name = NULL; |
aa7e44bf | 607 | |
ebe27b13 DH |
608 | if (pack_to_stdout) { |
609 | f = sha1fd(1, "<stdout>"); | |
610 | } else { | |
7ba502c4 NP |
611 | char tmpname[PATH_MAX]; |
612 | int fd; | |
613 | snprintf(tmpname, sizeof(tmpname), | |
614 | "%s/tmp_pack_XXXXXX", get_object_directory()); | |
615 | fd = xmkstemp(tmpname); | |
ebe27b13 DH |
616 | pack_tmp_name = xstrdup(tmpname); |
617 | f = sha1fd(fd, pack_tmp_name); | |
618 | } | |
619 | ||
620 | hdr.hdr_signature = htonl(PACK_SIGNATURE); | |
621 | hdr.hdr_version = htonl(PACK_VERSION); | |
622 | hdr.hdr_entries = htonl(nr_remaining); | |
623 | sha1write(f, &hdr, sizeof(hdr)); | |
624 | offset = sizeof(hdr); | |
625 | nr_written = 0; | |
626 | for (; i < nr_objects; i++) { | |
627 | last_obj_offset = offset; | |
628 | offset_one = write_one(f, objects + i, offset); | |
629 | if (!offset_one) | |
630 | break; | |
631 | offset = offset_one; | |
4d4fcc54 | 632 | display_progress(progress_state, written); |
ebe27b13 | 633 | } |
c553ca25 | 634 | |
ebe27b13 DH |
635 | /* |
636 | * Did we write the wrong # entries in the header? | |
637 | * If so, rewrite it like in fast-import | |
638 | */ | |
639 | if (pack_to_stdout || nr_written == nr_remaining) { | |
aa7e44bf | 640 | sha1close(f, sha1, 1); |
ebe27b13 | 641 | } else { |
7ba502c4 NP |
642 | int fd = sha1close(f, NULL, 0); |
643 | fixup_pack_header_footer(fd, sha1, pack_tmp_name, nr_written); | |
644 | close(fd); | |
ebe27b13 DH |
645 | } |
646 | ||
647 | if (!pack_to_stdout) { | |
d01fb92f | 648 | mode_t mode = umask(0); |
7ba502c4 | 649 | char *idx_tmp_name, tmpname[PATH_MAX]; |
d01fb92f DH |
650 | |
651 | umask(mode); | |
652 | mode = 0444 & ~mode; | |
653 | ||
4049b9cf | 654 | idx_tmp_name = write_idx_file(NULL, |
7ba502c4 NP |
655 | (struct pack_idx_entry **) written_list, |
656 | nr_written, sha1); | |
d01fb92f | 657 | snprintf(tmpname, sizeof(tmpname), "%s-%s.pack", |
aa7e44bf | 658 | base_name, sha1_to_hex(sha1)); |
d01fb92f DH |
659 | if (adjust_perm(pack_tmp_name, mode)) |
660 | die("unable to make temporary pack file readable: %s", | |
661 | strerror(errno)); | |
662 | if (rename(pack_tmp_name, tmpname)) | |
663 | die("unable to rename temporary pack file: %s", | |
664 | strerror(errno)); | |
665 | snprintf(tmpname, sizeof(tmpname), "%s-%s.idx", | |
aa7e44bf | 666 | base_name, sha1_to_hex(sha1)); |
d01fb92f DH |
667 | if (adjust_perm(idx_tmp_name, mode)) |
668 | die("unable to make temporary index file readable: %s", | |
669 | strerror(errno)); | |
670 | if (rename(idx_tmp_name, tmpname)) | |
671 | die("unable to rename temporary index file: %s", | |
672 | strerror(errno)); | |
7ba502c4 NP |
673 | free(idx_tmp_name); |
674 | free(pack_tmp_name); | |
aa7e44bf | 675 | puts(sha1_to_hex(sha1)); |
ebe27b13 DH |
676 | } |
677 | ||
678 | /* mark written objects as written to previous pack */ | |
679 | for (j = 0; j < nr_written; j++) { | |
aa7e44bf | 680 | written_list[j]->idx.offset = (off_t)-1; |
ebe27b13 DH |
681 | } |
682 | nr_remaining -= nr_written; | |
683 | } while (nr_remaining && i < nr_objects); | |
684 | ||
685 | free(written_list); | |
4d4fcc54 | 686 | stop_progress(&progress_state); |
67c08ce1 | 687 | if (written != nr_result) |
7cadf491 | 688 | die("wrote %u objects while expecting %u", written, nr_result); |
01c12a23 DH |
689 | /* |
690 | * We have scanned through [0 ... i). Since we have written | |
691 | * the correct number of objects, the remaining [i ... nr_objects) | |
692 | * items must be either already written (due to out-of-order delta base) | |
693 | * or a preferred base. Count those which are neither and complain if any. | |
694 | */ | |
ebe27b13 DH |
695 | for (j = 0; i < nr_objects; i++) { |
696 | struct object_entry *e = objects + i; | |
aa7e44bf | 697 | j += !e->idx.offset && !e->preferred_base; |
d01fb92f | 698 | } |
ebe27b13 DH |
699 | if (j) |
700 | die("wrote %u objects as expected but %u unwritten", written, j); | |
c323ac7d LT |
701 | } |
702 | ||
7a979d99 JH |
703 | static int locate_object_entry_hash(const unsigned char *sha1) |
704 | { | |
705 | int i; | |
706 | unsigned int ui; | |
707 | memcpy(&ui, sha1, sizeof(unsigned int)); | |
708 | i = ui % object_ix_hashsz; | |
709 | while (0 < object_ix[i]) { | |
aa7e44bf | 710 | if (!hashcmp(sha1, objects[object_ix[i] - 1].idx.sha1)) |
7a979d99 JH |
711 | return i; |
712 | if (++i == object_ix_hashsz) | |
713 | i = 0; | |
714 | } | |
715 | return -1 - i; | |
716 | } | |
717 | ||
718 | static struct object_entry *locate_object_entry(const unsigned char *sha1) | |
719 | { | |
720 | int i; | |
721 | ||
722 | if (!object_ix_hashsz) | |
723 | return NULL; | |
724 | ||
725 | i = locate_object_entry_hash(sha1); | |
726 | if (0 <= i) | |
727 | return &objects[object_ix[i]-1]; | |
728 | return NULL; | |
729 | } | |
730 | ||
731 | static void rehash_objects(void) | |
c323ac7d | 732 | { |
7cadf491 | 733 | uint32_t i; |
7a979d99 JH |
734 | struct object_entry *oe; |
735 | ||
736 | object_ix_hashsz = nr_objects * 3; | |
737 | if (object_ix_hashsz < 1024) | |
738 | object_ix_hashsz = 1024; | |
739 | object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz); | |
5379a5c5 | 740 | memset(object_ix, 0, sizeof(int) * object_ix_hashsz); |
7a979d99 | 741 | for (i = 0, oe = objects; i < nr_objects; i++, oe++) { |
aa7e44bf | 742 | int ix = locate_object_entry_hash(oe->idx.sha1); |
7a979d99 JH |
743 | if (0 <= ix) |
744 | continue; | |
745 | ix = -1 - ix; | |
746 | object_ix[ix] = i + 1; | |
747 | } | |
748 | } | |
749 | ||
ce0bd642 | 750 | static unsigned name_hash(const char *name) |
1d6b38cc | 751 | { |
ce0bd642 LT |
752 | unsigned char c; |
753 | unsigned hash = 0; | |
754 | ||
bc32fed5 JH |
755 | if (!name) |
756 | return 0; | |
757 | ||
eeef7135 | 758 | /* |
ce0bd642 LT |
759 | * This effectively just creates a sortable number from the |
760 | * last sixteen non-whitespace characters. Last characters | |
761 | * count "most", so things that end in ".c" sort together. | |
eeef7135 | 762 | */ |
ce0bd642 LT |
763 | while ((c = *name++) != 0) { |
764 | if (isspace(c)) | |
765 | continue; | |
766 | hash = (hash >> 2) + (c << 24); | |
767 | } | |
1d6b38cc JH |
768 | return hash; |
769 | } | |
770 | ||
a74db82e JH |
771 | static void setup_delta_attr_check(struct git_attr_check *check) |
772 | { | |
773 | static struct git_attr *attr_delta; | |
774 | ||
775 | if (!attr_delta) | |
776 | attr_delta = git_attr("delta", 5); | |
777 | ||
778 | check[0].attr = attr_delta; | |
779 | } | |
780 | ||
781 | static int no_try_delta(const char *path) | |
782 | { | |
783 | struct git_attr_check check[1]; | |
784 | ||
785 | setup_delta_attr_check(check); | |
786 | if (git_checkattr(path, ARRAY_SIZE(check), check)) | |
787 | return 0; | |
788 | if (ATTR_FALSE(check->value)) | |
789 | return 1; | |
790 | return 0; | |
791 | } | |
792 | ||
5c49c116 | 793 | static int add_object_entry(const unsigned char *sha1, enum object_type type, |
bc32fed5 | 794 | const char *name, int exclude) |
c323ac7d | 795 | { |
c323ac7d | 796 | struct object_entry *entry; |
29b734e4 | 797 | struct packed_git *p, *found_pack = NULL; |
6777a59f | 798 | off_t found_offset = 0; |
29b734e4 | 799 | int ix; |
bc32fed5 | 800 | unsigned hash = name_hash(name); |
29b734e4 NP |
801 | |
802 | ix = nr_objects ? locate_object_entry_hash(sha1) : -1; | |
803 | if (ix >= 0) { | |
804 | if (exclude) { | |
805 | entry = objects + object_ix[ix] - 1; | |
81a216a5 NP |
806 | if (!entry->preferred_base) |
807 | nr_result--; | |
29b734e4 NP |
808 | entry->preferred_base = 1; |
809 | } | |
810 | return 0; | |
811 | } | |
c323ac7d | 812 | |
5c49c116 NP |
813 | for (p = packed_git; p; p = p->next) { |
814 | off_t offset = find_pack_entry_one(sha1, p); | |
815 | if (offset) { | |
816 | if (!found_pack) { | |
817 | found_offset = offset; | |
818 | found_pack = p; | |
64560374 | 819 | } |
5c49c116 NP |
820 | if (exclude) |
821 | break; | |
822 | if (incremental) | |
823 | return 0; | |
824 | if (local && !p->pack_local) | |
825 | return 0; | |
64560374 LT |
826 | } |
827 | } | |
eb019375 | 828 | |
29b734e4 NP |
829 | if (nr_objects >= nr_alloc) { |
830 | nr_alloc = (nr_alloc + 1024) * 3 / 2; | |
7cadf491 | 831 | objects = xrealloc(objects, nr_alloc * sizeof(*entry)); |
c323ac7d | 832 | } |
29b734e4 NP |
833 | |
834 | entry = objects + nr_objects++; | |
c323ac7d | 835 | memset(entry, 0, sizeof(*entry)); |
aa7e44bf | 836 | hashcpy(entry->idx.sha1, sha1); |
27225f2e | 837 | entry->hash = hash; |
5c49c116 NP |
838 | if (type) |
839 | entry->type = type; | |
29b734e4 NP |
840 | if (exclude) |
841 | entry->preferred_base = 1; | |
81a216a5 NP |
842 | else |
843 | nr_result++; | |
29b734e4 NP |
844 | if (found_pack) { |
845 | entry->in_pack = found_pack; | |
846 | entry->in_pack_offset = found_offset; | |
847 | } | |
7a979d99 JH |
848 | |
849 | if (object_ix_hashsz * 3 <= nr_objects * 4) | |
850 | rehash_objects(); | |
29b734e4 NP |
851 | else |
852 | object_ix[-1 - ix] = nr_objects; | |
7a979d99 | 853 | |
4d4fcc54 | 854 | display_progress(progress_state, nr_objects); |
29b734e4 | 855 | |
a74db82e JH |
856 | if (name && no_try_delta(name)) |
857 | entry->no_try_delta = 1; | |
858 | ||
29b734e4 | 859 | return 1; |
c323ac7d LT |
860 | } |
861 | ||
5379a5c5 JH |
862 | struct pbase_tree_cache { |
863 | unsigned char sha1[20]; | |
864 | int ref; | |
865 | int temporary; | |
866 | void *tree_data; | |
867 | unsigned long tree_size; | |
868 | }; | |
869 | ||
870 | static struct pbase_tree_cache *(pbase_tree_cache[256]); | |
871 | static int pbase_tree_cache_ix(const unsigned char *sha1) | |
872 | { | |
873 | return sha1[0] % ARRAY_SIZE(pbase_tree_cache); | |
874 | } | |
875 | static int pbase_tree_cache_ix_incr(int ix) | |
876 | { | |
877 | return (ix+1) % ARRAY_SIZE(pbase_tree_cache); | |
878 | } | |
879 | ||
880 | static struct pbase_tree { | |
881 | struct pbase_tree *next; | |
882 | /* This is a phony "cache" entry; we are not | |
883 | * going to evict it nor find it through _get() | |
884 | * mechanism -- this is for the toplevel node that | |
885 | * would almost always change with any commit. | |
886 | */ | |
887 | struct pbase_tree_cache pcache; | |
888 | } *pbase_tree; | |
889 | ||
890 | static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1) | |
891 | { | |
892 | struct pbase_tree_cache *ent, *nent; | |
893 | void *data; | |
894 | unsigned long size; | |
21666f1a | 895 | enum object_type type; |
5379a5c5 JH |
896 | int neigh; |
897 | int my_ix = pbase_tree_cache_ix(sha1); | |
898 | int available_ix = -1; | |
899 | ||
900 | /* pbase-tree-cache acts as a limited hashtable. | |
901 | * your object will be found at your index or within a few | |
902 | * slots after that slot if it is cached. | |
903 | */ | |
904 | for (neigh = 0; neigh < 8; neigh++) { | |
905 | ent = pbase_tree_cache[my_ix]; | |
a89fccd2 | 906 | if (ent && !hashcmp(ent->sha1, sha1)) { |
5379a5c5 JH |
907 | ent->ref++; |
908 | return ent; | |
909 | } | |
910 | else if (((available_ix < 0) && (!ent || !ent->ref)) || | |
911 | ((0 <= available_ix) && | |
912 | (!ent && pbase_tree_cache[available_ix]))) | |
913 | available_ix = my_ix; | |
914 | if (!ent) | |
915 | break; | |
916 | my_ix = pbase_tree_cache_ix_incr(my_ix); | |
917 | } | |
918 | ||
919 | /* Did not find one. Either we got a bogus request or | |
920 | * we need to read and perhaps cache. | |
921 | */ | |
21666f1a | 922 | data = read_sha1_file(sha1, &type, &size); |
5379a5c5 JH |
923 | if (!data) |
924 | return NULL; | |
21666f1a | 925 | if (type != OBJ_TREE) { |
5379a5c5 JH |
926 | free(data); |
927 | return NULL; | |
928 | } | |
929 | ||
930 | /* We need to either cache or return a throwaway copy */ | |
931 | ||
932 | if (available_ix < 0) | |
933 | ent = NULL; | |
934 | else { | |
935 | ent = pbase_tree_cache[available_ix]; | |
936 | my_ix = available_ix; | |
937 | } | |
938 | ||
939 | if (!ent) { | |
940 | nent = xmalloc(sizeof(*nent)); | |
941 | nent->temporary = (available_ix < 0); | |
942 | } | |
943 | else { | |
944 | /* evict and reuse */ | |
945 | free(ent->tree_data); | |
946 | nent = ent; | |
947 | } | |
e702496e | 948 | hashcpy(nent->sha1, sha1); |
5379a5c5 JH |
949 | nent->tree_data = data; |
950 | nent->tree_size = size; | |
951 | nent->ref = 1; | |
952 | if (!nent->temporary) | |
953 | pbase_tree_cache[my_ix] = nent; | |
954 | return nent; | |
955 | } | |
956 | ||
957 | static void pbase_tree_put(struct pbase_tree_cache *cache) | |
958 | { | |
959 | if (!cache->temporary) { | |
960 | cache->ref--; | |
961 | return; | |
962 | } | |
963 | free(cache->tree_data); | |
964 | free(cache); | |
965 | } | |
966 | ||
967 | static int name_cmp_len(const char *name) | |
968 | { | |
969 | int i; | |
970 | for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++) | |
971 | ; | |
972 | return i; | |
973 | } | |
974 | ||
975 | static void add_pbase_object(struct tree_desc *tree, | |
5379a5c5 | 976 | const char *name, |
ce0bd642 LT |
977 | int cmplen, |
978 | const char *fullname) | |
3f9ac8d2 | 979 | { |
4c068a98 | 980 | struct name_entry entry; |
8a5a8d6c | 981 | int cmp; |
4c068a98 LT |
982 | |
983 | while (tree_entry(tree,&entry)) { | |
1211be6b LT |
984 | if (S_ISGITLINK(entry.mode)) |
985 | continue; | |
8a5a8d6c NP |
986 | cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 : |
987 | memcmp(name, entry.path, cmplen); | |
988 | if (cmp > 0) | |
7a979d99 | 989 | continue; |
8a5a8d6c NP |
990 | if (cmp < 0) |
991 | return; | |
5379a5c5 | 992 | if (name[cmplen] != '/') { |
5c49c116 NP |
993 | add_object_entry(entry.sha1, |
994 | S_ISDIR(entry.mode) ? OBJ_TREE : OBJ_BLOB, | |
bc32fed5 | 995 | fullname, 1); |
5379a5c5 JH |
996 | return; |
997 | } | |
8a5a8d6c | 998 | if (S_ISDIR(entry.mode)) { |
7a979d99 | 999 | struct tree_desc sub; |
5379a5c5 JH |
1000 | struct pbase_tree_cache *tree; |
1001 | const char *down = name+cmplen+1; | |
1002 | int downlen = name_cmp_len(down); | |
1003 | ||
4c068a98 | 1004 | tree = pbase_tree_get(entry.sha1); |
5379a5c5 JH |
1005 | if (!tree) |
1006 | return; | |
6fda5e51 | 1007 | init_tree_desc(&sub, tree->tree_data, tree->tree_size); |
5379a5c5 | 1008 | |
ce0bd642 | 1009 | add_pbase_object(&sub, down, downlen, fullname); |
5379a5c5 JH |
1010 | pbase_tree_put(tree); |
1011 | } | |
1012 | } | |
1013 | } | |
1d6b38cc | 1014 | |
5379a5c5 JH |
1015 | static unsigned *done_pbase_paths; |
1016 | static int done_pbase_paths_num; | |
1017 | static int done_pbase_paths_alloc; | |
1018 | static int done_pbase_path_pos(unsigned hash) | |
1019 | { | |
1020 | int lo = 0; | |
1021 | int hi = done_pbase_paths_num; | |
1022 | while (lo < hi) { | |
1023 | int mi = (hi + lo) / 2; | |
1024 | if (done_pbase_paths[mi] == hash) | |
1025 | return mi; | |
1026 | if (done_pbase_paths[mi] < hash) | |
1027 | hi = mi; | |
1028 | else | |
1029 | lo = mi + 1; | |
1030 | } | |
1031 | return -lo-1; | |
1032 | } | |
1033 | ||
1034 | static int check_pbase_path(unsigned hash) | |
1035 | { | |
1036 | int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash); | |
1037 | if (0 <= pos) | |
1038 | return 1; | |
1039 | pos = -pos - 1; | |
1040 | if (done_pbase_paths_alloc <= done_pbase_paths_num) { | |
1041 | done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc); | |
1042 | done_pbase_paths = xrealloc(done_pbase_paths, | |
1043 | done_pbase_paths_alloc * | |
1044 | sizeof(unsigned)); | |
1045 | } | |
1046 | done_pbase_paths_num++; | |
1047 | if (pos < done_pbase_paths_num) | |
1048 | memmove(done_pbase_paths + pos + 1, | |
1049 | done_pbase_paths + pos, | |
1050 | (done_pbase_paths_num - pos - 1) * sizeof(unsigned)); | |
1051 | done_pbase_paths[pos] = hash; | |
1052 | return 0; | |
1053 | } | |
1054 | ||
bc32fed5 | 1055 | static void add_preferred_base_object(const char *name) |
5379a5c5 JH |
1056 | { |
1057 | struct pbase_tree *it; | |
8a5a8d6c | 1058 | int cmplen; |
bc32fed5 | 1059 | unsigned hash = name_hash(name); |
5379a5c5 | 1060 | |
8a5a8d6c | 1061 | if (!num_preferred_base || check_pbase_path(hash)) |
5379a5c5 JH |
1062 | return; |
1063 | ||
8a5a8d6c | 1064 | cmplen = name_cmp_len(name); |
5379a5c5 JH |
1065 | for (it = pbase_tree; it; it = it->next) { |
1066 | if (cmplen == 0) { | |
bc32fed5 | 1067 | add_object_entry(it->pcache.sha1, OBJ_TREE, NULL, 1); |
5379a5c5 JH |
1068 | } |
1069 | else { | |
1070 | struct tree_desc tree; | |
6fda5e51 | 1071 | init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size); |
ce0bd642 | 1072 | add_pbase_object(&tree, name, cmplen, name); |
7a979d99 | 1073 | } |
3f9ac8d2 | 1074 | } |
3f9ac8d2 JH |
1075 | } |
1076 | ||
7a979d99 | 1077 | static void add_preferred_base(unsigned char *sha1) |
3f9ac8d2 | 1078 | { |
5379a5c5 JH |
1079 | struct pbase_tree *it; |
1080 | void *data; | |
1081 | unsigned long size; | |
1082 | unsigned char tree_sha1[20]; | |
1d6b38cc | 1083 | |
8d1d8f83 JH |
1084 | if (window <= num_preferred_base++) |
1085 | return; | |
1086 | ||
5379a5c5 JH |
1087 | data = read_object_with_reference(sha1, tree_type, &size, tree_sha1); |
1088 | if (!data) | |
7a979d99 | 1089 | return; |
5379a5c5 JH |
1090 | |
1091 | for (it = pbase_tree; it; it = it->next) { | |
a89fccd2 | 1092 | if (!hashcmp(it->pcache.sha1, tree_sha1)) { |
5379a5c5 JH |
1093 | free(data); |
1094 | return; | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | it = xcalloc(1, sizeof(*it)); | |
1099 | it->next = pbase_tree; | |
1100 | pbase_tree = it; | |
1101 | ||
e702496e | 1102 | hashcpy(it->pcache.sha1, tree_sha1); |
5379a5c5 JH |
1103 | it->pcache.tree_data = data; |
1104 | it->pcache.tree_size = size; | |
3f9ac8d2 JH |
1105 | } |
1106 | ||
c323ac7d LT |
1107 | static void check_object(struct object_entry *entry) |
1108 | { | |
5c49c116 | 1109 | if (entry->in_pack) { |
780e6e73 | 1110 | struct packed_git *p = entry->in_pack; |
03e79c88 | 1111 | struct pack_window *w_curs = NULL; |
5c49c116 NP |
1112 | const unsigned char *base_ref = NULL; |
1113 | struct object_entry *base_entry; | |
1114 | unsigned long used, used_0; | |
6777a59f | 1115 | unsigned int avail; |
5c49c116 NP |
1116 | off_t ofs; |
1117 | unsigned char *buf, c; | |
780e6e73 | 1118 | |
6777a59f | 1119 | buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail); |
ab7cd7bb | 1120 | |
5c49c116 | 1121 | /* |
fa736f72 NP |
1122 | * We want in_pack_type even if we do not reuse delta |
1123 | * since non-delta representations could still be reused. | |
ab7cd7bb | 1124 | */ |
6777a59f | 1125 | used = unpack_object_header_gently(buf, avail, |
5c49c116 NP |
1126 | &entry->in_pack_type, |
1127 | &entry->size); | |
ab7cd7bb | 1128 | |
5c49c116 NP |
1129 | /* |
1130 | * Determine if this is a delta and if so whether we can | |
1131 | * reuse it or not. Otherwise let's find out as cheaply as | |
1132 | * possible what the actual type and size for this object is. | |
3f9ac8d2 | 1133 | */ |
5c49c116 NP |
1134 | switch (entry->in_pack_type) { |
1135 | default: | |
1136 | /* Not a delta hence we've already got all we need. */ | |
1137 | entry->type = entry->in_pack_type; | |
1138 | entry->in_pack_header_size = used; | |
1139 | unuse_pack(&w_curs); | |
1140 | return; | |
1141 | case OBJ_REF_DELTA: | |
1142 | if (!no_reuse_delta && !entry->preferred_base) | |
1143 | base_ref = use_pack(p, &w_curs, | |
1144 | entry->in_pack_offset + used, NULL); | |
1145 | entry->in_pack_header_size = used + 20; | |
1146 | break; | |
1147 | case OBJ_OFS_DELTA: | |
1148 | buf = use_pack(p, &w_curs, | |
1149 | entry->in_pack_offset + used, NULL); | |
1150 | used_0 = 0; | |
1151 | c = buf[used_0++]; | |
1152 | ofs = c & 127; | |
1153 | while (c & 128) { | |
1154 | ofs += 1; | |
1155 | if (!ofs || MSB(ofs, 7)) | |
1156 | die("delta base offset overflow in pack for %s", | |
aa7e44bf | 1157 | sha1_to_hex(entry->idx.sha1)); |
5c49c116 NP |
1158 | c = buf[used_0++]; |
1159 | ofs = (ofs << 7) + (c & 127); | |
780e6e73 | 1160 | } |
5c49c116 NP |
1161 | if (ofs >= entry->in_pack_offset) |
1162 | die("delta base offset out of bound for %s", | |
aa7e44bf | 1163 | sha1_to_hex(entry->idx.sha1)); |
5c49c116 NP |
1164 | ofs = entry->in_pack_offset - ofs; |
1165 | if (!no_reuse_delta && !entry->preferred_base) | |
1166 | base_ref = find_packed_object_name(p, ofs); | |
1167 | entry->in_pack_header_size = used + used_0; | |
1168 | break; | |
780e6e73 | 1169 | } |
780e6e73 | 1170 | |
5c49c116 NP |
1171 | if (base_ref && (base_entry = locate_object_entry(base_ref))) { |
1172 | /* | |
1173 | * If base_ref was set above that means we wish to | |
1174 | * reuse delta data, and we even found that base | |
1175 | * in the list of objects we want to pack. Goodie! | |
1176 | * | |
1177 | * Depth value does not matter - find_deltas() will | |
1178 | * never consider reused delta as the base object to | |
1179 | * deltify other objects against, in order to avoid | |
1180 | * circular deltas. | |
3f9ac8d2 | 1181 | */ |
780e6e73 | 1182 | entry->type = entry->in_pack_type; |
5c49c116 | 1183 | entry->delta = base_entry; |
15b4d577 JH |
1184 | entry->delta_sibling = base_entry->delta_child; |
1185 | base_entry->delta_child = entry; | |
5c49c116 NP |
1186 | unuse_pack(&w_curs); |
1187 | return; | |
1188 | } | |
ab7cd7bb | 1189 | |
5c49c116 NP |
1190 | if (entry->type) { |
1191 | /* | |
1192 | * This must be a delta and we already know what the | |
1193 | * final object type is. Let's extract the actual | |
1194 | * object size from the delta header. | |
1195 | */ | |
1196 | entry->size = get_size_from_delta(p, &w_curs, | |
1197 | entry->in_pack_offset + entry->in_pack_header_size); | |
1198 | unuse_pack(&w_curs); | |
3f9ac8d2 JH |
1199 | return; |
1200 | } | |
5c49c116 NP |
1201 | |
1202 | /* | |
1203 | * No choice but to fall back to the recursive delta walk | |
1204 | * with sha1_object_info() to find about the object type | |
1205 | * at this point... | |
1206 | */ | |
1207 | unuse_pack(&w_curs); | |
36e4d74a | 1208 | } |
3f9ac8d2 | 1209 | |
aa7e44bf | 1210 | entry->type = sha1_object_info(entry->idx.sha1, &entry->size); |
21666f1a | 1211 | if (entry->type < 0) |
36e4d74a | 1212 | die("unable to get type of object %s", |
aa7e44bf | 1213 | sha1_to_hex(entry->idx.sha1)); |
3f9ac8d2 JH |
1214 | } |
1215 | ||
5c49c116 NP |
1216 | static int pack_offset_sort(const void *_a, const void *_b) |
1217 | { | |
1218 | const struct object_entry *a = *(struct object_entry **)_a; | |
1219 | const struct object_entry *b = *(struct object_entry **)_b; | |
1220 | ||
1221 | /* avoid filesystem trashing with loose objects */ | |
1222 | if (!a->in_pack && !b->in_pack) | |
aa7e44bf | 1223 | return hashcmp(a->idx.sha1, b->idx.sha1); |
5c49c116 NP |
1224 | |
1225 | if (a->in_pack < b->in_pack) | |
1226 | return -1; | |
1227 | if (a->in_pack > b->in_pack) | |
1228 | return 1; | |
1229 | return a->in_pack_offset < b->in_pack_offset ? -1 : | |
1230 | (a->in_pack_offset > b->in_pack_offset); | |
1231 | } | |
1232 | ||
c323ac7d LT |
1233 | static void get_object_details(void) |
1234 | { | |
7cadf491 | 1235 | uint32_t i; |
5c49c116 NP |
1236 | struct object_entry **sorted_by_offset; |
1237 | ||
1238 | sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *)); | |
1239 | for (i = 0; i < nr_objects; i++) | |
1240 | sorted_by_offset[i] = objects + i; | |
1241 | qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort); | |
c323ac7d | 1242 | |
3f9ac8d2 | 1243 | prepare_pack_ix(); |
5c49c116 NP |
1244 | for (i = 0; i < nr_objects; i++) |
1245 | check_object(sorted_by_offset[i]); | |
1246 | free(sorted_by_offset); | |
c323ac7d LT |
1247 | } |
1248 | ||
9668cf59 | 1249 | static int type_size_sort(const void *_a, const void *_b) |
c323ac7d | 1250 | { |
9668cf59 NP |
1251 | const struct object_entry *a = *(struct object_entry **)_a; |
1252 | const struct object_entry *b = *(struct object_entry **)_b; | |
1253 | ||
c323ac7d LT |
1254 | if (a->type < b->type) |
1255 | return -1; | |
1256 | if (a->type > b->type) | |
1257 | return 1; | |
27225f2e LT |
1258 | if (a->hash < b->hash) |
1259 | return -1; | |
1260 | if (a->hash > b->hash) | |
1261 | return 1; | |
7a979d99 JH |
1262 | if (a->preferred_base < b->preferred_base) |
1263 | return -1; | |
1264 | if (a->preferred_base > b->preferred_base) | |
1265 | return 1; | |
c323ac7d LT |
1266 | if (a->size < b->size) |
1267 | return -1; | |
1268 | if (a->size > b->size) | |
1269 | return 1; | |
adcc7095 | 1270 | return a > b ? -1 : (a < b); /* newest last */ |
c323ac7d LT |
1271 | } |
1272 | ||
1273 | struct unpacked { | |
1274 | struct object_entry *entry; | |
1275 | void *data; | |
f6c7081a | 1276 | struct delta_index *index; |
5a235b5e | 1277 | unsigned depth; |
c323ac7d LT |
1278 | }; |
1279 | ||
d250626c NP |
1280 | static int delta_cacheable(unsigned long src_size, unsigned long trg_size, |
1281 | unsigned long delta_size) | |
074b2eea MK |
1282 | { |
1283 | if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size) | |
1284 | return 0; | |
1285 | ||
e3dfddb3 MK |
1286 | if (delta_size < cache_max_small_delta_size) |
1287 | return 1; | |
1288 | ||
074b2eea MK |
1289 | /* cache delta, if objects are large enough compared to delta size */ |
1290 | if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10)) | |
1291 | return 1; | |
1292 | ||
1293 | return 0; | |
1294 | } | |
1295 | ||
8ecce684 NP |
1296 | #ifdef THREADED_DELTA_SEARCH |
1297 | ||
1298 | static pthread_mutex_t read_mutex = PTHREAD_MUTEX_INITIALIZER; | |
1299 | #define read_lock() pthread_mutex_lock(&read_mutex) | |
1300 | #define read_unlock() pthread_mutex_unlock(&read_mutex) | |
1301 | ||
3c701839 NP |
1302 | static pthread_mutex_t cache_mutex = PTHREAD_MUTEX_INITIALIZER; |
1303 | #define cache_lock() pthread_mutex_lock(&cache_mutex) | |
1304 | #define cache_unlock() pthread_mutex_unlock(&cache_mutex) | |
1305 | ||
8ecce684 NP |
1306 | static pthread_mutex_t progress_mutex = PTHREAD_MUTEX_INITIALIZER; |
1307 | #define progress_lock() pthread_mutex_lock(&progress_mutex) | |
1308 | #define progress_unlock() pthread_mutex_unlock(&progress_mutex) | |
1309 | ||
1310 | #else | |
1311 | ||
e1ef8673 JH |
1312 | #define read_lock() (void)0 |
1313 | #define read_unlock() (void)0 | |
1314 | #define cache_lock() (void)0 | |
1315 | #define cache_unlock() (void)0 | |
1316 | #define progress_lock() (void)0 | |
1317 | #define progress_unlock() (void)0 | |
8ecce684 NP |
1318 | |
1319 | #endif | |
1320 | ||
c323ac7d | 1321 | /* |
521a4f4c LT |
1322 | * We search for deltas _backwards_ in a list sorted by type and |
1323 | * by size, so that we see progressively smaller and smaller files. | |
1324 | * That's because we prefer deltas to be from the bigger file | |
1325 | * to the smaller - deletes are potentially cheaper, but perhaps | |
1326 | * more importantly, the bigger file is likely the more recent | |
1327 | * one. | |
c323ac7d | 1328 | */ |
f6c7081a | 1329 | static int try_delta(struct unpacked *trg, struct unpacked *src, |
ef0316fc | 1330 | unsigned max_depth, unsigned long *mem_usage) |
c323ac7d | 1331 | { |
f6c7081a NP |
1332 | struct object_entry *trg_entry = trg->entry; |
1333 | struct object_entry *src_entry = src->entry; | |
560b25a8 | 1334 | unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz; |
c83f032e | 1335 | unsigned ref_depth; |
21666f1a | 1336 | enum object_type type; |
c323ac7d LT |
1337 | void *delta_buf; |
1338 | ||
1339 | /* Don't bother doing diffs between different types */ | |
f6c7081a | 1340 | if (trg_entry->type != src_entry->type) |
c323ac7d LT |
1341 | return -1; |
1342 | ||
51d1e83f LT |
1343 | /* |
1344 | * We do not bother to try a delta that we discarded | |
8dbbd14e | 1345 | * on an earlier try, but only when reusing delta data. |
51d1e83f | 1346 | */ |
8dbbd14e | 1347 | if (!no_reuse_delta && trg_entry->in_pack && |
e9195b58 JH |
1348 | trg_entry->in_pack == src_entry->in_pack && |
1349 | trg_entry->in_pack_type != OBJ_REF_DELTA && | |
1350 | trg_entry->in_pack_type != OBJ_OFS_DELTA) | |
51d1e83f LT |
1351 | return 0; |
1352 | ||
898b14ce | 1353 | /* Let's not bust the allowed depth. */ |
5a235b5e | 1354 | if (src->depth >= max_depth) |
d116a45a | 1355 | return 0; |
c323ac7d | 1356 | |
c3b06a69 | 1357 | /* Now some size filtering heuristics. */ |
560b25a8 | 1358 | trg_size = trg_entry->size; |
c83f032e NP |
1359 | if (!trg_entry->delta) { |
1360 | max_size = trg_size/2 - 20; | |
1361 | ref_depth = 1; | |
1362 | } else { | |
1363 | max_size = trg_entry->delta_size; | |
5a235b5e | 1364 | ref_depth = trg->depth; |
c83f032e | 1365 | } |
5a235b5e | 1366 | max_size = max_size * (max_depth - src->depth) / |
c83f032e | 1367 | (max_depth - ref_depth + 1); |
c3b06a69 NP |
1368 | if (max_size == 0) |
1369 | return 0; | |
f6c7081a | 1370 | src_size = src_entry->size; |
560b25a8 | 1371 | sizediff = src_size < trg_size ? trg_size - src_size : 0; |
27225f2e | 1372 | if (sizediff >= max_size) |
f527cb8c | 1373 | return 0; |
a1dab41a BD |
1374 | if (trg_size < src_size / 32) |
1375 | return 0; | |
f6c7081a | 1376 | |
560b25a8 NP |
1377 | /* Load data if not already done */ |
1378 | if (!trg->data) { | |
8ecce684 | 1379 | read_lock(); |
aa7e44bf | 1380 | trg->data = read_sha1_file(trg_entry->idx.sha1, &type, &sz); |
8ecce684 | 1381 | read_unlock(); |
2e3404c3 JH |
1382 | if (!trg->data) |
1383 | die("object %s cannot be read", | |
1384 | sha1_to_hex(trg_entry->idx.sha1)); | |
560b25a8 NP |
1385 | if (sz != trg_size) |
1386 | die("object %s inconsistent object length (%lu vs %lu)", | |
aa7e44bf | 1387 | sha1_to_hex(trg_entry->idx.sha1), sz, trg_size); |
ef0316fc | 1388 | *mem_usage += sz; |
560b25a8 NP |
1389 | } |
1390 | if (!src->data) { | |
8ecce684 | 1391 | read_lock(); |
aa7e44bf | 1392 | src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz); |
8ecce684 | 1393 | read_unlock(); |
2e3404c3 JH |
1394 | if (!src->data) |
1395 | die("object %s cannot be read", | |
1396 | sha1_to_hex(src_entry->idx.sha1)); | |
560b25a8 NP |
1397 | if (sz != src_size) |
1398 | die("object %s inconsistent object length (%lu vs %lu)", | |
aa7e44bf | 1399 | sha1_to_hex(src_entry->idx.sha1), sz, src_size); |
ef0316fc | 1400 | *mem_usage += sz; |
560b25a8 NP |
1401 | } |
1402 | if (!src->index) { | |
1403 | src->index = create_delta_index(src->data, src_size); | |
a588d88a MK |
1404 | if (!src->index) { |
1405 | static int warned = 0; | |
1406 | if (!warned++) | |
1407 | warning("suboptimal pack - out of memory"); | |
1408 | return 0; | |
1409 | } | |
ef0316fc | 1410 | *mem_usage += sizeof_delta_index(src->index); |
560b25a8 NP |
1411 | } |
1412 | ||
1413 | delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size); | |
c323ac7d | 1414 | if (!delta_buf) |
75c42d8c | 1415 | return 0; |
f6c7081a | 1416 | |
9e2d57a0 | 1417 | if (trg_entry->delta) { |
848d732c BD |
1418 | /* Prefer only shallower same-sized deltas. */ |
1419 | if (delta_size == trg_entry->delta_size && | |
5a235b5e | 1420 | src->depth + 1 >= trg->depth) { |
848d732c BD |
1421 | free(delta_buf); |
1422 | return 0; | |
1423 | } | |
074b2eea | 1424 | } |
9e2d57a0 | 1425 | |
f6c7081a NP |
1426 | trg_entry->delta = src_entry; |
1427 | trg_entry->delta_size = delta_size; | |
5a235b5e | 1428 | trg->depth = src->depth + 1; |
074b2eea | 1429 | |
3c701839 NP |
1430 | /* |
1431 | * Handle memory allocation outside of the cache | |
1432 | * accounting lock. Compiler will optimize the strangeness | |
1433 | * away when THREADED_DELTA_SEARCH is not defined. | |
1434 | */ | |
1435 | if (trg_entry->delta_data) | |
1436 | free(trg_entry->delta_data); | |
1437 | cache_lock(); | |
9e2d57a0 NP |
1438 | if (trg_entry->delta_data) { |
1439 | delta_cache_size -= trg_entry->delta_size; | |
9e2d57a0 NP |
1440 | trg_entry->delta_data = NULL; |
1441 | } | |
d250626c | 1442 | if (delta_cacheable(src_size, trg_size, delta_size)) { |
074b2eea | 1443 | delta_cache_size += trg_entry->delta_size; |
3c701839 NP |
1444 | cache_unlock(); |
1445 | trg_entry->delta_data = xrealloc(delta_buf, delta_size); | |
1446 | } else { | |
1447 | cache_unlock(); | |
074b2eea | 1448 | free(delta_buf); |
3c701839 NP |
1449 | } |
1450 | ||
f6c7081a | 1451 | return 1; |
c323ac7d LT |
1452 | } |
1453 | ||
898b14ce | 1454 | static unsigned int check_delta_limit(struct object_entry *me, unsigned int n) |
b2504a0d | 1455 | { |
898b14ce NP |
1456 | struct object_entry *child = me->delta_child; |
1457 | unsigned int m = n; | |
1458 | while (child) { | |
1459 | unsigned int c = check_delta_limit(child, n + 1); | |
1460 | if (m < c) | |
1461 | m = c; | |
1462 | child = child->delta_sibling; | |
1463 | } | |
1464 | return m; | |
b2504a0d NP |
1465 | } |
1466 | ||
ef0316fc | 1467 | static unsigned long free_unpacked(struct unpacked *n) |
a97773ce | 1468 | { |
ef0316fc | 1469 | unsigned long freed_mem = sizeof_delta_index(n->index); |
a97773ce BD |
1470 | free_delta_index(n->index); |
1471 | n->index = NULL; | |
1472 | if (n->data) { | |
ef0316fc | 1473 | freed_mem += n->entry->size; |
a97773ce BD |
1474 | free(n->data); |
1475 | n->data = NULL; | |
a97773ce BD |
1476 | } |
1477 | n->entry = NULL; | |
7d7baa5e | 1478 | n->depth = 0; |
ef0316fc | 1479 | return freed_mem; |
a97773ce BD |
1480 | } |
1481 | ||
75d39853 | 1482 | static void find_deltas(struct object_entry **list, unsigned list_size, |
e334977d | 1483 | int window, int depth, unsigned *processed) |
c323ac7d | 1484 | { |
e334977d | 1485 | uint32_t i = list_size, idx = 0, count = 0; |
c323ac7d | 1486 | unsigned int array_size = window * sizeof(struct unpacked); |
7cadf491 | 1487 | struct unpacked *array; |
ef0316fc | 1488 | unsigned long mem_usage = 0; |
c323ac7d | 1489 | |
7cadf491 | 1490 | array = xmalloc(array_size); |
c323ac7d | 1491 | memset(array, 0, array_size); |
21fcd1bd | 1492 | |
7cadf491 SP |
1493 | do { |
1494 | struct object_entry *entry = list[--i]; | |
c323ac7d | 1495 | struct unpacked *n = array + idx; |
ef0316fc | 1496 | int j, max_depth, best_base = -1; |
c323ac7d | 1497 | |
ef0316fc | 1498 | mem_usage -= free_unpacked(n); |
c323ac7d | 1499 | n->entry = entry; |
ab7cd7bb | 1500 | |
a97773ce | 1501 | while (window_memory_limit && |
ef0316fc | 1502 | mem_usage > window_memory_limit && |
a97773ce BD |
1503 | count > 1) { |
1504 | uint32_t tail = (idx + window - count) % window; | |
ef0316fc | 1505 | mem_usage -= free_unpacked(array + tail); |
a97773ce BD |
1506 | count--; |
1507 | } | |
1508 | ||
75d39853 NP |
1509 | /* We do not compute delta to *create* objects we are not |
1510 | * going to pack. | |
1511 | */ | |
1512 | if (entry->preferred_base) | |
1513 | goto next; | |
1514 | ||
8ecce684 | 1515 | progress_lock(); |
e334977d | 1516 | (*processed)++; |
4d4fcc54 | 1517 | display_progress(progress_state, *processed); |
8ecce684 | 1518 | progress_unlock(); |
75d39853 | 1519 | |
898b14ce NP |
1520 | /* |
1521 | * If the current object is at pack edge, take the depth the | |
1522 | * objects that depend on the current object into account | |
1523 | * otherwise they would become too deep. | |
1524 | */ | |
1525 | max_depth = depth; | |
1526 | if (entry->delta_child) { | |
1527 | max_depth -= check_delta_limit(entry, 0); | |
1528 | if (max_depth <= 0) | |
1529 | goto next; | |
1530 | } | |
1531 | ||
78817c15 LT |
1532 | j = window; |
1533 | while (--j > 0) { | |
77639870 | 1534 | int ret; |
7cadf491 | 1535 | uint32_t other_idx = idx + j; |
c323ac7d | 1536 | struct unpacked *m; |
78817c15 LT |
1537 | if (other_idx >= window) |
1538 | other_idx -= window; | |
c323ac7d LT |
1539 | m = array + other_idx; |
1540 | if (!m->entry) | |
1541 | break; | |
ef0316fc | 1542 | ret = try_delta(n, m, max_depth, &mem_usage); |
77639870 | 1543 | if (ret < 0) |
c323ac7d | 1544 | break; |
77639870 JH |
1545 | else if (ret > 0) |
1546 | best_base = other_idx; | |
c323ac7d | 1547 | } |
898b14ce | 1548 | |
70ca1a3f JH |
1549 | /* if we made n a delta, and if n is already at max |
1550 | * depth, leaving it in the window is pointless. we | |
1551 | * should evict it first. | |
70ca1a3f | 1552 | */ |
5a235b5e | 1553 | if (entry->delta && depth <= n->depth) |
70ca1a3f | 1554 | continue; |
ff45715c | 1555 | |
77639870 JH |
1556 | /* |
1557 | * Move the best delta base up in the window, after the | |
1558 | * currently deltified object, to keep it longer. It will | |
1559 | * be the first base object to be attempted next. | |
1560 | */ | |
1561 | if (entry->delta) { | |
1562 | struct unpacked swap = array[best_base]; | |
1563 | int dist = (window + idx - best_base) % window; | |
1564 | int dst = best_base; | |
1565 | while (dist--) { | |
1566 | int src = (dst + 1) % window; | |
1567 | array[dst] = array[src]; | |
1568 | dst = src; | |
1569 | } | |
1570 | array[dst] = swap; | |
1571 | } | |
1572 | ||
898b14ce | 1573 | next: |
521a4f4c | 1574 | idx++; |
a97773ce BD |
1575 | if (count + 1 < window) |
1576 | count++; | |
521a4f4c LT |
1577 | if (idx >= window) |
1578 | idx = 0; | |
7cadf491 | 1579 | } while (i > 0); |
adee7bdf | 1580 | |
f6c7081a | 1581 | for (i = 0; i < window; ++i) { |
ff45715c | 1582 | free_delta_index(array[i].index); |
adee7bdf | 1583 | free(array[i].data); |
f6c7081a | 1584 | } |
adee7bdf | 1585 | free(array); |
c323ac7d LT |
1586 | } |
1587 | ||
8ecce684 NP |
1588 | #ifdef THREADED_DELTA_SEARCH |
1589 | ||
1590 | struct thread_params { | |
1591 | pthread_t thread; | |
1592 | struct object_entry **list; | |
1593 | unsigned list_size; | |
1594 | int window; | |
1595 | int depth; | |
1596 | unsigned *processed; | |
1597 | }; | |
1598 | ||
c2a33679 NP |
1599 | static pthread_mutex_t data_request = PTHREAD_MUTEX_INITIALIZER; |
1600 | static pthread_mutex_t data_ready = PTHREAD_MUTEX_INITIALIZER; | |
1601 | static pthread_mutex_t data_provider = PTHREAD_MUTEX_INITIALIZER; | |
1602 | static struct thread_params *data_requester; | |
1603 | ||
8ecce684 NP |
1604 | static void *threaded_find_deltas(void *arg) |
1605 | { | |
c2a33679 NP |
1606 | struct thread_params *me = arg; |
1607 | ||
1608 | for (;;) { | |
1609 | pthread_mutex_lock(&data_request); | |
1610 | data_requester = me; | |
1611 | pthread_mutex_unlock(&data_provider); | |
1612 | pthread_mutex_lock(&data_ready); | |
b81d9af7 | 1613 | pthread_mutex_unlock(&data_request); |
c2a33679 NP |
1614 | |
1615 | if (!me->list_size) | |
1616 | return NULL; | |
1617 | ||
1618 | find_deltas(me->list, me->list_size, | |
1619 | me->window, me->depth, me->processed); | |
1620 | } | |
8ecce684 NP |
1621 | } |
1622 | ||
8ecce684 NP |
1623 | static void ll_find_deltas(struct object_entry **list, unsigned list_size, |
1624 | int window, int depth, unsigned *processed) | |
1625 | { | |
b81d9af7 | 1626 | struct thread_params *target, p[delta_search_threads]; |
8ecce684 | 1627 | int i, ret; |
c2a33679 NP |
1628 | unsigned chunk_size; |
1629 | ||
367f4a43 NP |
1630 | if (delta_search_threads <= 1) { |
1631 | find_deltas(list, list_size, window, depth, processed); | |
1632 | return; | |
1633 | } | |
1634 | ||
c2a33679 NP |
1635 | pthread_mutex_lock(&data_provider); |
1636 | pthread_mutex_lock(&data_ready); | |
8ecce684 | 1637 | |
367f4a43 | 1638 | for (i = 0; i < delta_search_threads; i++) { |
8ecce684 NP |
1639 | p[i].window = window; |
1640 | p[i].depth = depth; | |
1641 | p[i].processed = processed; | |
1642 | ret = pthread_create(&p[i].thread, NULL, | |
1643 | threaded_find_deltas, &p[i]); | |
1644 | if (ret) | |
1645 | die("unable to create thread: %s", strerror(ret)); | |
8ecce684 NP |
1646 | } |
1647 | ||
c2a33679 NP |
1648 | /* this should be auto-tuned somehow */ |
1649 | chunk_size = window * 1000; | |
1650 | ||
1651 | do { | |
1652 | unsigned sublist_size = chunk_size; | |
1653 | if (sublist_size > list_size) | |
1654 | sublist_size = list_size; | |
1655 | ||
59921b4b NP |
1656 | /* try to split chunks on "path" boundaries */ |
1657 | while (sublist_size < list_size && list[sublist_size]->hash && | |
1658 | list[sublist_size]->hash == list[sublist_size-1]->hash) | |
1659 | sublist_size++; | |
1660 | ||
c2a33679 | 1661 | pthread_mutex_lock(&data_provider); |
b81d9af7 NP |
1662 | target = data_requester; |
1663 | target->list = list; | |
1664 | target->list_size = sublist_size; | |
c2a33679 NP |
1665 | pthread_mutex_unlock(&data_ready); |
1666 | ||
1667 | list += sublist_size; | |
1668 | list_size -= sublist_size; | |
1669 | if (!sublist_size) { | |
b81d9af7 | 1670 | pthread_join(target->thread, NULL); |
c2a33679 NP |
1671 | i--; |
1672 | } | |
c2a33679 | 1673 | } while (i); |
8ecce684 NP |
1674 | } |
1675 | ||
1676 | #else | |
1677 | #define ll_find_deltas find_deltas | |
1678 | #endif | |
1679 | ||
f3123c4a JH |
1680 | static void prepare_pack(int window, int depth) |
1681 | { | |
9668cf59 | 1682 | struct object_entry **delta_list; |
75d39853 | 1683 | uint32_t i, n, nr_deltas; |
9668cf59 | 1684 | |
3f9ac8d2 | 1685 | get_object_details(); |
9668cf59 | 1686 | |
75d39853 | 1687 | if (!nr_objects || !window || !depth) |
9668cf59 NP |
1688 | return; |
1689 | ||
1690 | delta_list = xmalloc(nr_objects * sizeof(*delta_list)); | |
75d39853 NP |
1691 | nr_deltas = n = 0; |
1692 | ||
1693 | for (i = 0; i < nr_objects; i++) { | |
1694 | struct object_entry *entry = objects + i; | |
1695 | ||
1696 | if (entry->delta) | |
1697 | /* This happens if we decided to reuse existing | |
1698 | * delta from a pack. "!no_reuse_delta &&" is implied. | |
1699 | */ | |
1700 | continue; | |
1701 | ||
1702 | if (entry->size < 50) | |
1703 | continue; | |
1704 | ||
1705 | if (entry->no_try_delta) | |
1706 | continue; | |
1707 | ||
1708 | if (!entry->preferred_base) | |
1709 | nr_deltas++; | |
1710 | ||
1711 | delta_list[n++] = entry; | |
1712 | } | |
1713 | ||
2f8b8947 | 1714 | if (nr_deltas && n > 1) { |
e334977d NP |
1715 | unsigned nr_done = 0; |
1716 | if (progress) | |
dc6a0757 NP |
1717 | progress_state = start_progress("Compressing objects", |
1718 | nr_deltas); | |
75d39853 | 1719 | qsort(delta_list, n, sizeof(*delta_list), type_size_sort); |
8ecce684 | 1720 | ll_find_deltas(delta_list, n, window+1, depth, &nr_done); |
4d4fcc54 | 1721 | stop_progress(&progress_state); |
e334977d NP |
1722 | if (nr_done != nr_deltas) |
1723 | die("inconsistency with delta count"); | |
75d39853 | 1724 | } |
9668cf59 | 1725 | free(delta_list); |
f3123c4a JH |
1726 | } |
1727 | ||
4812a93a JK |
1728 | static int git_pack_config(const char *k, const char *v) |
1729 | { | |
1730 | if(!strcmp(k, "pack.window")) { | |
1731 | window = git_config_int(k, v); | |
1732 | return 0; | |
1733 | } | |
a97773ce BD |
1734 | if (!strcmp(k, "pack.windowmemory")) { |
1735 | window_memory_limit = git_config_ulong(k, v); | |
1736 | return 0; | |
1737 | } | |
1738 | if (!strcmp(k, "pack.depth")) { | |
842aaf93 TT |
1739 | depth = git_config_int(k, v); |
1740 | return 0; | |
1741 | } | |
960ccca6 DH |
1742 | if (!strcmp(k, "pack.compression")) { |
1743 | int level = git_config_int(k, v); | |
1744 | if (level == -1) | |
1745 | level = Z_DEFAULT_COMPRESSION; | |
1746 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
1747 | die("bad pack compression level %d", level); | |
1748 | pack_compression_level = level; | |
1749 | pack_compression_seen = 1; | |
1750 | return 0; | |
1751 | } | |
074b2eea MK |
1752 | if (!strcmp(k, "pack.deltacachesize")) { |
1753 | max_delta_cache_size = git_config_int(k, v); | |
1754 | return 0; | |
1755 | } | |
e3dfddb3 MK |
1756 | if (!strcmp(k, "pack.deltacachelimit")) { |
1757 | cache_max_small_delta_size = git_config_int(k, v); | |
1758 | return 0; | |
1759 | } | |
693b86ff NP |
1760 | if (!strcmp(k, "pack.threads")) { |
1761 | delta_search_threads = git_config_int(k, v); | |
1762 | if (delta_search_threads < 1) | |
1763 | die("invalid number of threads specified (%d)", | |
1764 | delta_search_threads); | |
1765 | #ifndef THREADED_DELTA_SEARCH | |
1766 | if (delta_search_threads > 1) | |
1767 | warning("no threads support, ignoring %s", k); | |
1768 | #endif | |
1769 | return 0; | |
1770 | } | |
4812a93a JK |
1771 | return git_default_config(k, v); |
1772 | } | |
1773 | ||
b5d97e6b | 1774 | static void read_object_list_from_stdin(void) |
c323ac7d | 1775 | { |
b5d97e6b JH |
1776 | char line[40 + 1 + PATH_MAX + 2]; |
1777 | unsigned char sha1[20]; | |
b2504a0d | 1778 | |
da93d12b | 1779 | for (;;) { |
da93d12b LT |
1780 | if (!fgets(line, sizeof(line), stdin)) { |
1781 | if (feof(stdin)) | |
1782 | break; | |
1783 | if (!ferror(stdin)) | |
1784 | die("fgets returned NULL, not EOF, not error!"); | |
687dd75c JH |
1785 | if (errno != EINTR) |
1786 | die("fgets: %s", strerror(errno)); | |
1787 | clearerr(stdin); | |
1788 | continue; | |
da93d12b | 1789 | } |
7a979d99 JH |
1790 | if (line[0] == '-') { |
1791 | if (get_sha1_hex(line+1, sha1)) | |
1792 | die("expected edge sha1, got garbage:\n %s", | |
b5d97e6b | 1793 | line); |
8d1d8f83 | 1794 | add_preferred_base(sha1); |
7a979d99 | 1795 | continue; |
21fcd1bd | 1796 | } |
c323ac7d | 1797 | if (get_sha1_hex(line, sha1)) |
ef07618f | 1798 | die("expected sha1, got garbage:\n %s", line); |
b5d97e6b | 1799 | |
bc32fed5 JH |
1800 | add_preferred_base_object(line+41); |
1801 | add_object_entry(sha1, 0, line+41, 0); | |
c323ac7d | 1802 | } |
b5d97e6b JH |
1803 | } |
1804 | ||
08cdfb13 JH |
1805 | #define OBJECT_ADDED (1u<<20) |
1806 | ||
b5d97e6b JH |
1807 | static void show_commit(struct commit *commit) |
1808 | { | |
bc32fed5 | 1809 | add_object_entry(commit->object.sha1, OBJ_COMMIT, NULL, 0); |
08cdfb13 | 1810 | commit->object.flags |= OBJECT_ADDED; |
b5d97e6b JH |
1811 | } |
1812 | ||
1813 | static void show_object(struct object_array_entry *p) | |
1814 | { | |
bc32fed5 JH |
1815 | add_preferred_base_object(p->name); |
1816 | add_object_entry(p->item->sha1, p->item->type, p->name, 0); | |
08cdfb13 | 1817 | p->item->flags |= OBJECT_ADDED; |
b5d97e6b JH |
1818 | } |
1819 | ||
8d1d8f83 JH |
1820 | static void show_edge(struct commit *commit) |
1821 | { | |
1822 | add_preferred_base(commit->object.sha1); | |
1823 | } | |
1824 | ||
08cdfb13 JH |
1825 | struct in_pack_object { |
1826 | off_t offset; | |
1827 | struct object *object; | |
1828 | }; | |
1829 | ||
1830 | struct in_pack { | |
1831 | int alloc; | |
1832 | int nr; | |
1833 | struct in_pack_object *array; | |
1834 | }; | |
1835 | ||
1836 | static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack) | |
1837 | { | |
1838 | in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->sha1, p); | |
1839 | in_pack->array[in_pack->nr].object = object; | |
1840 | in_pack->nr++; | |
1841 | } | |
1842 | ||
1843 | /* | |
1844 | * Compare the objects in the offset order, in order to emulate the | |
1845 | * "git-rev-list --objects" output that produced the pack originally. | |
1846 | */ | |
1847 | static int ofscmp(const void *a_, const void *b_) | |
1848 | { | |
1849 | struct in_pack_object *a = (struct in_pack_object *)a_; | |
1850 | struct in_pack_object *b = (struct in_pack_object *)b_; | |
1851 | ||
1852 | if (a->offset < b->offset) | |
1853 | return -1; | |
1854 | else if (a->offset > b->offset) | |
1855 | return 1; | |
1856 | else | |
1857 | return hashcmp(a->object->sha1, b->object->sha1); | |
1858 | } | |
1859 | ||
1860 | static void add_objects_in_unpacked_packs(struct rev_info *revs) | |
1861 | { | |
1862 | struct packed_git *p; | |
1863 | struct in_pack in_pack; | |
1864 | uint32_t i; | |
1865 | ||
1866 | memset(&in_pack, 0, sizeof(in_pack)); | |
1867 | ||
1868 | for (p = packed_git; p; p = p->next) { | |
1869 | const unsigned char *sha1; | |
1870 | struct object *o; | |
1871 | ||
1872 | for (i = 0; i < revs->num_ignore_packed; i++) { | |
1873 | if (matches_pack_name(p, revs->ignore_packed[i])) | |
1874 | break; | |
1875 | } | |
1876 | if (revs->num_ignore_packed <= i) | |
1877 | continue; | |
1878 | if (open_pack_index(p)) | |
1879 | die("cannot open pack index"); | |
1880 | ||
1881 | ALLOC_GROW(in_pack.array, | |
1882 | in_pack.nr + p->num_objects, | |
1883 | in_pack.alloc); | |
1884 | ||
1885 | for (i = 0; i < p->num_objects; i++) { | |
1886 | sha1 = nth_packed_object_sha1(p, i); | |
1887 | o = lookup_unknown_object(sha1); | |
1888 | if (!(o->flags & OBJECT_ADDED)) | |
1889 | mark_in_pack_object(o, p, &in_pack); | |
1890 | o->flags |= OBJECT_ADDED; | |
1891 | } | |
1892 | } | |
1893 | ||
1894 | if (in_pack.nr) { | |
1895 | qsort(in_pack.array, in_pack.nr, sizeof(in_pack.array[0]), | |
1896 | ofscmp); | |
1897 | for (i = 0; i < in_pack.nr; i++) { | |
1898 | struct object *o = in_pack.array[i].object; | |
1899 | add_object_entry(o->sha1, o->type, "", 0); | |
1900 | } | |
1901 | } | |
1902 | free(in_pack.array); | |
1903 | } | |
1904 | ||
8d1d8f83 | 1905 | static void get_object_list(int ac, const char **av) |
b5d97e6b JH |
1906 | { |
1907 | struct rev_info revs; | |
1908 | char line[1000]; | |
b5d97e6b JH |
1909 | int flags = 0; |
1910 | ||
b5d97e6b JH |
1911 | init_revisions(&revs, NULL); |
1912 | save_commit_buffer = 0; | |
1913 | track_object_refs = 0; | |
1914 | setup_revisions(ac, av, &revs, NULL); | |
1915 | ||
b5d97e6b JH |
1916 | while (fgets(line, sizeof(line), stdin) != NULL) { |
1917 | int len = strlen(line); | |
1918 | if (line[len - 1] == '\n') | |
1919 | line[--len] = 0; | |
1920 | if (!len) | |
1921 | break; | |
1922 | if (*line == '-') { | |
1923 | if (!strcmp(line, "--not")) { | |
1924 | flags ^= UNINTERESTING; | |
1925 | continue; | |
1926 | } | |
1927 | die("not a rev '%s'", line); | |
1928 | } | |
1929 | if (handle_revision_arg(line, &revs, flags, 1)) | |
1930 | die("bad revision '%s'", line); | |
1931 | } | |
1932 | ||
1933 | prepare_revision_walk(&revs); | |
8d1d8f83 | 1934 | mark_edges_uninteresting(revs.commits, &revs, show_edge); |
b5d97e6b | 1935 | traverse_commit_list(&revs, show_commit, show_object); |
08cdfb13 JH |
1936 | |
1937 | if (keep_unreachable) | |
1938 | add_objects_in_unpacked_packs(&revs); | |
b5d97e6b JH |
1939 | } |
1940 | ||
b6b32ccb JH |
1941 | static int adjust_perm(const char *path, mode_t mode) |
1942 | { | |
1943 | if (chmod(path, mode)) | |
1944 | return -1; | |
1945 | return adjust_shared_perm(path); | |
1946 | } | |
1947 | ||
b5d97e6b JH |
1948 | int cmd_pack_objects(int argc, const char **argv, const char *prefix) |
1949 | { | |
b5d97e6b | 1950 | int use_internal_rev_list = 0; |
8d1d8f83 | 1951 | int thin = 0; |
7cadf491 | 1952 | uint32_t i; |
ffa84ffb RD |
1953 | const char **rp_av; |
1954 | int rp_ac_alloc = 64; | |
8d1d8f83 JH |
1955 | int rp_ac; |
1956 | ||
ffa84ffb RD |
1957 | rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av)); |
1958 | ||
8d1d8f83 JH |
1959 | rp_av[0] = "pack-objects"; |
1960 | rp_av[1] = "--objects"; /* --thin will make it --objects-edge */ | |
1961 | rp_ac = 2; | |
b5d97e6b JH |
1962 | |
1963 | git_config(git_pack_config); | |
960ccca6 DH |
1964 | if (!pack_compression_seen && core_compression_seen) |
1965 | pack_compression_level = core_compression_level; | |
b5d97e6b JH |
1966 | |
1967 | progress = isatty(2); | |
1968 | for (i = 1; i < argc; i++) { | |
1969 | const char *arg = argv[i]; | |
1970 | ||
1971 | if (*arg != '-') | |
1972 | break; | |
1973 | ||
1974 | if (!strcmp("--non-empty", arg)) { | |
1975 | non_empty = 1; | |
1976 | continue; | |
1977 | } | |
1978 | if (!strcmp("--local", arg)) { | |
1979 | local = 1; | |
1980 | continue; | |
1981 | } | |
b5d97e6b JH |
1982 | if (!strcmp("--incremental", arg)) { |
1983 | incremental = 1; | |
1984 | continue; | |
1985 | } | |
960ccca6 DH |
1986 | if (!prefixcmp(arg, "--compression=")) { |
1987 | char *end; | |
1988 | int level = strtoul(arg+14, &end, 0); | |
1989 | if (!arg[14] || *end) | |
1990 | usage(pack_usage); | |
1991 | if (level == -1) | |
1992 | level = Z_DEFAULT_COMPRESSION; | |
1993 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
1994 | die("bad pack compression level %d", level); | |
1995 | pack_compression_level = level; | |
1996 | continue; | |
1997 | } | |
6b94b1a0 DH |
1998 | if (!prefixcmp(arg, "--max-pack-size=")) { |
1999 | char *end; | |
2000 | pack_size_limit = strtoul(arg+16, &end, 0) * 1024 * 1024; | |
2001 | if (!arg[16] || *end) | |
2002 | usage(pack_usage); | |
2003 | continue; | |
2004 | } | |
599065a3 | 2005 | if (!prefixcmp(arg, "--window=")) { |
b5d97e6b JH |
2006 | char *end; |
2007 | window = strtoul(arg+9, &end, 0); | |
2008 | if (!arg[9] || *end) | |
2009 | usage(pack_usage); | |
2010 | continue; | |
2011 | } | |
a97773ce BD |
2012 | if (!prefixcmp(arg, "--window-memory=")) { |
2013 | if (!git_parse_ulong(arg+16, &window_memory_limit)) | |
2014 | usage(pack_usage); | |
2015 | continue; | |
2016 | } | |
367f4a43 NP |
2017 | if (!prefixcmp(arg, "--threads=")) { |
2018 | char *end; | |
2019 | delta_search_threads = strtoul(arg+10, &end, 0); | |
2020 | if (!arg[10] || *end || delta_search_threads < 1) | |
2021 | usage(pack_usage); | |
2022 | #ifndef THREADED_DELTA_SEARCH | |
2023 | if (delta_search_threads > 1) | |
2024 | warning("no threads support, " | |
2025 | "ignoring %s", arg); | |
2026 | #endif | |
2027 | continue; | |
2028 | } | |
599065a3 | 2029 | if (!prefixcmp(arg, "--depth=")) { |
b5d97e6b JH |
2030 | char *end; |
2031 | depth = strtoul(arg+8, &end, 0); | |
2032 | if (!arg[8] || *end) | |
2033 | usage(pack_usage); | |
2034 | continue; | |
2035 | } | |
2036 | if (!strcmp("--progress", arg)) { | |
2037 | progress = 1; | |
2038 | continue; | |
2039 | } | |
231f240b NP |
2040 | if (!strcmp("--all-progress", arg)) { |
2041 | progress = 2; | |
2042 | continue; | |
2043 | } | |
b5d97e6b JH |
2044 | if (!strcmp("-q", arg)) { |
2045 | progress = 0; | |
2046 | continue; | |
2047 | } | |
2048 | if (!strcmp("--no-reuse-delta", arg)) { | |
2049 | no_reuse_delta = 1; | |
2050 | continue; | |
2051 | } | |
fa736f72 NP |
2052 | if (!strcmp("--no-reuse-object", arg)) { |
2053 | no_reuse_object = no_reuse_delta = 1; | |
2054 | continue; | |
2055 | } | |
be6b1914 | 2056 | if (!strcmp("--delta-base-offset", arg)) { |
780e6e73 | 2057 | allow_ofs_delta = 1; |
be6b1914 NP |
2058 | continue; |
2059 | } | |
b5d97e6b JH |
2060 | if (!strcmp("--stdout", arg)) { |
2061 | pack_to_stdout = 1; | |
2062 | continue; | |
2063 | } | |
2064 | if (!strcmp("--revs", arg)) { | |
2065 | use_internal_rev_list = 1; | |
2066 | continue; | |
2067 | } | |
08cdfb13 JH |
2068 | if (!strcmp("--keep-unreachable", arg)) { |
2069 | keep_unreachable = 1; | |
2070 | continue; | |
2071 | } | |
8d1d8f83 | 2072 | if (!strcmp("--unpacked", arg) || |
599065a3 | 2073 | !prefixcmp(arg, "--unpacked=") || |
63049292 | 2074 | !strcmp("--reflog", arg) || |
8d1d8f83 JH |
2075 | !strcmp("--all", arg)) { |
2076 | use_internal_rev_list = 1; | |
ffa84ffb RD |
2077 | if (rp_ac >= rp_ac_alloc - 1) { |
2078 | rp_ac_alloc = alloc_nr(rp_ac_alloc); | |
2079 | rp_av = xrealloc(rp_av, | |
2080 | rp_ac_alloc * sizeof(*rp_av)); | |
2081 | } | |
8d1d8f83 | 2082 | rp_av[rp_ac++] = arg; |
b5d97e6b JH |
2083 | continue; |
2084 | } | |
8d1d8f83 JH |
2085 | if (!strcmp("--thin", arg)) { |
2086 | use_internal_rev_list = 1; | |
2087 | thin = 1; | |
2088 | rp_av[1] = "--objects-edge"; | |
b5d97e6b | 2089 | continue; |
4ba7d711 NP |
2090 | } |
2091 | if (!prefixcmp(arg, "--index-version=")) { | |
2092 | char *c; | |
aa7e44bf GB |
2093 | pack_idx_default_version = strtoul(arg + 16, &c, 10); |
2094 | if (pack_idx_default_version > 2) | |
4ba7d711 NP |
2095 | die("bad %s", arg); |
2096 | if (*c == ',') | |
aa7e44bf GB |
2097 | pack_idx_off32_limit = strtoul(c+1, &c, 0); |
2098 | if (*c || pack_idx_off32_limit & 0x80000000) | |
4ba7d711 NP |
2099 | die("bad %s", arg); |
2100 | continue; | |
b5d97e6b JH |
2101 | } |
2102 | usage(pack_usage); | |
2103 | } | |
2104 | ||
2105 | /* Traditionally "pack-objects [options] base extra" failed; | |
2106 | * we would however want to take refs parameter that would | |
2107 | * have been given to upstream rev-list ourselves, which means | |
2108 | * we somehow want to say what the base name is. So the | |
2109 | * syntax would be: | |
2110 | * | |
2111 | * pack-objects [options] base <refs...> | |
2112 | * | |
2113 | * in other words, we would treat the first non-option as the | |
2114 | * base_name and send everything else to the internal revision | |
2115 | * walker. | |
2116 | */ | |
2117 | ||
2118 | if (!pack_to_stdout) | |
2119 | base_name = argv[i++]; | |
2120 | ||
2121 | if (pack_to_stdout != !base_name) | |
2122 | usage(pack_usage); | |
2123 | ||
01c12a23 DH |
2124 | if (pack_to_stdout && pack_size_limit) |
2125 | die("--max-pack-size cannot be used to build a pack for transfer."); | |
2126 | ||
8d1d8f83 JH |
2127 | if (!pack_to_stdout && thin) |
2128 | die("--thin cannot be used to build an indexable pack."); | |
b5d97e6b JH |
2129 | |
2130 | prepare_packed_git(); | |
2131 | ||
13aaf148 | 2132 | if (progress) |
dc6a0757 | 2133 | progress_state = start_progress("Counting objects", 0); |
b5d97e6b JH |
2134 | if (!use_internal_rev_list) |
2135 | read_object_list_from_stdin(); | |
8d1d8f83 JH |
2136 | else { |
2137 | rp_av[rp_ac] = NULL; | |
2138 | get_object_list(rp_ac, rp_av); | |
2139 | } | |
4d4fcc54 | 2140 | stop_progress(&progress_state); |
96a02f8f | 2141 | |
f0b0af1b | 2142 | if (non_empty && !nr_result) |
1c4a2912 | 2143 | return 0; |
f7ae6a93 NP |
2144 | if (nr_result) |
2145 | prepare_pack(window, depth); | |
d01fb92f | 2146 | write_pack_file(); |
ab7cd7bb | 2147 | if (progress) |
7cadf491 | 2148 | fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n", |
67c08ce1 | 2149 | written, written_delta, reused, reused_delta); |
c323ac7d LT |
2150 | return 0; |
2151 | } |