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