8 static const char pack_usage
[] = "git-pack-objects [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
11 unsigned char sha1
[20];
16 enum object_type type
;
17 unsigned long delta_size
;
18 struct object_entry
*delta
;
21 static unsigned char object_list_sha1
[20];
22 static int non_empty
= 0;
23 static int incremental
= 0;
24 static struct object_entry
**sorted_by_sha
, **sorted_by_type
;
25 static struct object_entry
*objects
= NULL
;
26 static int nr_objects
= 0, nr_alloc
= 0;
27 static const char *base_name
;
28 static unsigned char pack_file_sha1
[20];
30 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
32 unsigned long othersize
, delta_size
;
34 void *otherbuf
= read_sha1_file(entry
->delta
->sha1
, type
, &othersize
);
38 die("unable to read %s", sha1_to_hex(entry
->delta
->sha1
));
39 delta_buf
= diff_delta(otherbuf
, othersize
,
40 buf
, size
, &delta_size
, 0);
41 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
42 die("delta size changed");
49 * The per-object header is a pretty dense thing, which is
50 * - first byte: low four bits are "size", then three bits of "type",
51 * and the high bit is "size continues".
52 * - each byte afterwards: low seven bits are size continuation,
53 * with the high bit being "size continues"
55 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
60 if (type
< OBJ_COMMIT
|| type
> OBJ_DELTA
)
61 die("bad type %d", type
);
63 c
= (type
<< 4) | (size
& 15);
75 static unsigned long write_object(struct sha1file
*f
, struct object_entry
*entry
)
79 void *buf
= read_sha1_file(entry
->sha1
, type
, &size
);
80 unsigned char header
[10];
81 unsigned hdrlen
, datalen
;
82 enum object_type obj_type
;
85 die("unable to read %s", sha1_to_hex(entry
->sha1
));
86 if (size
!= entry
->size
)
87 die("object %s size inconsistency (%lu vs %lu)", sha1_to_hex(entry
->sha1
), size
, entry
->size
);
90 * The object header is a byte of 'type' followed by zero or
91 * more bytes of length. For deltas, the 20 bytes of delta sha1
94 obj_type
= entry
->type
;
96 buf
= delta_against(buf
, size
, entry
);
97 size
= entry
->delta_size
;
100 hdrlen
= encode_header(obj_type
, size
, header
);
101 sha1write(f
, header
, hdrlen
);
103 sha1write(f
, entry
->delta
, 20);
106 datalen
= sha1write_compressed(f
, buf
, size
);
108 return hdrlen
+ datalen
;
111 static unsigned long write_one(struct sha1file
*f
,
112 struct object_entry
*e
,
113 unsigned long offset
)
116 /* offset starts from header size and cannot be zero
117 * if it is written already.
121 offset
+= write_object(f
, e
);
122 /* if we are delitified, write out its base object. */
124 offset
= write_one(f
, e
->delta
, offset
);
128 static void write_pack_file(void)
132 unsigned long offset
;
134 struct pack_header hdr
;
137 f
= sha1fd(1, "<stdout>");
139 f
= sha1create("%s-%s.%s", base_name
, sha1_to_hex(object_list_sha1
), "pack");
140 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
141 hdr
.hdr_version
= htonl(PACK_VERSION
);
142 hdr
.hdr_entries
= htonl(nr_objects
);
143 sha1write(f
, &hdr
, sizeof(hdr
));
144 offset
= sizeof(hdr
);
145 for (i
= 0; i
< nr_objects
; i
++)
146 offset
= write_one(f
, objects
+ i
, offset
);
148 sha1close(f
, pack_file_sha1
, 1);
153 static void write_index_file(void)
156 struct sha1file
*f
= sha1create("%s-%s.%s", base_name
, sha1_to_hex(object_list_sha1
), "idx");
157 struct object_entry
**list
= sorted_by_sha
;
158 struct object_entry
**last
= list
+ nr_objects
;
159 unsigned int array
[256];
162 * Write the first-level table (the list is sorted,
163 * but we use a 256-entry lookup to be able to avoid
164 * having to do eight extra binary search iterations).
166 for (i
= 0; i
< 256; i
++) {
167 struct object_entry
**next
= list
;
168 while (next
< last
) {
169 struct object_entry
*entry
= *next
;
170 if (entry
->sha1
[0] != i
)
174 array
[i
] = htonl(next
- sorted_by_sha
);
177 sha1write(f
, array
, 256 * sizeof(int));
180 * Write the actual SHA1 entries..
182 list
= sorted_by_sha
;
183 for (i
= 0; i
< nr_objects
; i
++) {
184 struct object_entry
*entry
= *list
++;
185 unsigned int offset
= htonl(entry
->offset
);
186 sha1write(f
, &offset
, 4);
187 sha1write(f
, entry
->sha1
, 20);
189 sha1write(f
, pack_file_sha1
, 20);
190 sha1close(f
, NULL
, 1);
193 static int add_object_entry(unsigned char *sha1
, unsigned int hash
)
195 unsigned int idx
= nr_objects
;
196 struct object_entry
*entry
;
198 if (incremental
&& has_sha1_pack(sha1
))
201 if (idx
>= nr_alloc
) {
202 unsigned int needed
= (idx
+ 1024) * 3 / 2;
203 objects
= xrealloc(objects
, needed
* sizeof(*entry
));
206 entry
= objects
+ idx
;
207 memset(entry
, 0, sizeof(*entry
));
208 memcpy(entry
->sha1
, sha1
, 20);
214 static void check_object(struct object_entry
*entry
)
218 if (!sha1_object_info(entry
->sha1
, type
, &entry
->size
)) {
219 if (!strcmp(type
, "commit")) {
220 entry
->type
= OBJ_COMMIT
;
221 } else if (!strcmp(type
, "tree")) {
222 entry
->type
= OBJ_TREE
;
223 } else if (!strcmp(type
, "blob")) {
224 entry
->type
= OBJ_BLOB
;
225 } else if (!strcmp(type
, "tag")) {
226 entry
->type
= OBJ_TAG
;
228 die("unable to pack object %s of type %s",
229 sha1_to_hex(entry
->sha1
), type
);
232 die("unable to get type of object %s",
233 sha1_to_hex(entry
->sha1
));
236 static void get_object_details(void)
239 struct object_entry
*entry
= objects
;
241 for (i
= 0; i
< nr_objects
; i
++)
242 check_object(entry
++);
245 typedef int (*entry_sort_t
)(const struct object_entry
*, const struct object_entry
*);
247 static entry_sort_t current_sort
;
249 static int sort_comparator(const void *_a
, const void *_b
)
251 struct object_entry
*a
= *(struct object_entry
**)_a
;
252 struct object_entry
*b
= *(struct object_entry
**)_b
;
253 return current_sort(a
,b
);
256 static struct object_entry
**create_sorted_list(entry_sort_t sort
)
258 struct object_entry
**list
= xmalloc(nr_objects
* sizeof(struct object_entry
*));
261 for (i
= 0; i
< nr_objects
; i
++)
262 list
[i
] = objects
+ i
;
264 qsort(list
, nr_objects
, sizeof(struct object_entry
*), sort_comparator
);
268 static int sha1_sort(const struct object_entry
*a
, const struct object_entry
*b
)
270 return memcmp(a
->sha1
, b
->sha1
, 20);
273 static int type_size_sort(const struct object_entry
*a
, const struct object_entry
*b
)
275 if (a
->type
< b
->type
)
277 if (a
->type
> b
->type
)
279 if (a
->hash
< b
->hash
)
281 if (a
->hash
> b
->hash
)
283 if (a
->size
< b
->size
)
285 if (a
->size
> b
->size
)
287 return a
< b
? -1 : (a
> b
);
291 struct object_entry
*entry
;
296 * We search for deltas _backwards_ in a list sorted by type and
297 * by size, so that we see progressively smaller and smaller files.
298 * That's because we prefer deltas to be from the bigger file
299 * to the smaller - deletes are potentially cheaper, but perhaps
300 * more importantly, the bigger file is likely the more recent
303 static int try_delta(struct unpacked
*cur
, struct unpacked
*old
, unsigned max_depth
)
305 struct object_entry
*cur_entry
= cur
->entry
;
306 struct object_entry
*old_entry
= old
->entry
;
307 unsigned long size
, oldsize
, delta_size
, sizediff
;
311 /* Don't bother doing diffs between different types */
312 if (cur_entry
->type
!= old_entry
->type
)
315 size
= cur_entry
->size
;
318 oldsize
= old_entry
->size
;
319 sizediff
= oldsize
> size
? oldsize
- size
: size
- oldsize
;
320 if (sizediff
> size
/ 8)
322 if (old_entry
->depth
>= max_depth
)
328 * We always delta from the bigger to the smaller, since that's
329 * more space-efficient (deletes don't have to say _what_ they
332 max_size
= size
/ 2 - 20;
333 if (cur_entry
->delta
)
334 max_size
= cur_entry
->delta_size
-1;
335 if (sizediff
>= max_size
)
337 delta_buf
= diff_delta(old
->data
, oldsize
,
338 cur
->data
, size
, &delta_size
, max_size
);
341 cur_entry
->delta
= old_entry
;
342 cur_entry
->delta_size
= delta_size
;
343 cur_entry
->depth
= old_entry
->depth
+ 1;
348 static void find_deltas(struct object_entry
**list
, int window
, int depth
)
351 unsigned int array_size
= window
* sizeof(struct unpacked
);
352 struct unpacked
*array
= xmalloc(array_size
);
354 memset(array
, 0, array_size
);
358 struct object_entry
*entry
= list
[i
];
359 struct unpacked
*n
= array
+ idx
;
366 n
->data
= read_sha1_file(entry
->sha1
, type
, &size
);
367 if (size
!= entry
->size
)
368 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry
->sha1
), size
, entry
->size
);
371 unsigned int other_idx
= idx
+ j
;
373 if (other_idx
>= window
)
375 m
= array
+ other_idx
;
378 if (try_delta(n
, m
, depth
) < 0)
386 for (i
= 0; i
< window
; ++i
)
391 int main(int argc
, char **argv
)
394 char line
[PATH_MAX
+ 20];
395 int window
= 10, depth
= 10, pack_to_stdout
= 0;
396 struct object_entry
**list
;
399 for (i
= 1; i
< argc
; i
++) {
400 const char *arg
= argv
[i
];
403 if (!strcmp("--non-empty", arg
)) {
407 if (!strcmp("--incremental", arg
)) {
411 if (!strncmp("--window=", arg
, 9)) {
413 window
= strtoul(arg
+9, &end
, 0);
418 if (!strncmp("--depth=", arg
, 8)) {
420 depth
= strtoul(arg
+8, &end
, 0);
425 if (!strcmp("--stdout", arg
)) {
436 if (pack_to_stdout
!= !base_name
)
439 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
442 unsigned char sha1
[20];
444 if (get_sha1_hex(line
, sha1
))
445 die("expected sha1, got garbage");
449 unsigned char c
= *p
++;
452 hash
= hash
* 11 + c
;
454 add_object_entry(sha1
, hash
);
456 if (non_empty
&& !nr_objects
)
458 get_object_details();
460 fprintf(stderr
, "Packing %d objects\n", nr_objects
);
462 sorted_by_sha
= create_sorted_list(sha1_sort
);
464 list
= sorted_by_sha
;
465 for (i
= 0; i
< nr_objects
; i
++) {
466 struct object_entry
*entry
= *list
++;
467 SHA1_Update(&ctx
, entry
->sha1
, 20);
469 SHA1_Final(object_list_sha1
, &ctx
);
471 sorted_by_type
= create_sorted_list(type_size_sort
);
473 find_deltas(sorted_by_type
, window
+1, depth
);
476 if (!pack_to_stdout
) {
478 puts(sha1_to_hex(object_list_sha1
));