8 static const char pack_usage
[] = "git-pack-objects [--local] [--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;
24 static int incremental
= 0;
25 static struct object_entry
**sorted_by_sha
, **sorted_by_type
;
26 static struct object_entry
*objects
= NULL
;
27 static int nr_objects
= 0, nr_alloc
= 0;
28 static const char *base_name
;
29 static unsigned char pack_file_sha1
[20];
31 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
33 unsigned long othersize
, delta_size
;
35 void *otherbuf
= read_sha1_file(entry
->delta
->sha1
, type
, &othersize
);
39 die("unable to read %s", sha1_to_hex(entry
->delta
->sha1
));
40 delta_buf
= diff_delta(otherbuf
, othersize
,
41 buf
, size
, &delta_size
, 0);
42 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
43 die("delta size changed");
50 * The per-object header is a pretty dense thing, which is
51 * - first byte: low four bits are "size", then three bits of "type",
52 * and the high bit is "size continues".
53 * - each byte afterwards: low seven bits are size continuation,
54 * with the high bit being "size continues"
56 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
61 if (type
< OBJ_COMMIT
|| type
> OBJ_DELTA
)
62 die("bad type %d", type
);
64 c
= (type
<< 4) | (size
& 15);
76 static unsigned long write_object(struct sha1file
*f
, struct object_entry
*entry
)
80 void *buf
= read_sha1_file(entry
->sha1
, type
, &size
);
81 unsigned char header
[10];
82 unsigned hdrlen
, datalen
;
83 enum object_type obj_type
;
86 die("unable to read %s", sha1_to_hex(entry
->sha1
));
87 if (size
!= entry
->size
)
88 die("object %s size inconsistency (%lu vs %lu)", sha1_to_hex(entry
->sha1
), size
, entry
->size
);
91 * The object header is a byte of 'type' followed by zero or
92 * more bytes of length. For deltas, the 20 bytes of delta sha1
95 obj_type
= entry
->type
;
97 buf
= delta_against(buf
, size
, entry
);
98 size
= entry
->delta_size
;
101 hdrlen
= encode_header(obj_type
, size
, header
);
102 sha1write(f
, header
, hdrlen
);
104 sha1write(f
, entry
->delta
, 20);
107 datalen
= sha1write_compressed(f
, buf
, size
);
109 return hdrlen
+ datalen
;
112 static unsigned long write_one(struct sha1file
*f
,
113 struct object_entry
*e
,
114 unsigned long offset
)
117 /* offset starts from header size and cannot be zero
118 * if it is written already.
122 offset
+= write_object(f
, e
);
123 /* if we are delitified, write out its base object. */
125 offset
= write_one(f
, e
->delta
, offset
);
129 static void write_pack_file(void)
133 unsigned long offset
;
135 struct pack_header hdr
;
138 f
= sha1fd(1, "<stdout>");
140 f
= sha1create("%s-%s.%s", base_name
, sha1_to_hex(object_list_sha1
), "pack");
141 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
142 hdr
.hdr_version
= htonl(PACK_VERSION
);
143 hdr
.hdr_entries
= htonl(nr_objects
);
144 sha1write(f
, &hdr
, sizeof(hdr
));
145 offset
= sizeof(hdr
);
146 for (i
= 0; i
< nr_objects
; i
++)
147 offset
= write_one(f
, objects
+ i
, offset
);
149 sha1close(f
, pack_file_sha1
, 1);
154 static void write_index_file(void)
157 struct sha1file
*f
= sha1create("%s-%s.%s", base_name
, sha1_to_hex(object_list_sha1
), "idx");
158 struct object_entry
**list
= sorted_by_sha
;
159 struct object_entry
**last
= list
+ nr_objects
;
160 unsigned int array
[256];
163 * Write the first-level table (the list is sorted,
164 * but we use a 256-entry lookup to be able to avoid
165 * having to do eight extra binary search iterations).
167 for (i
= 0; i
< 256; i
++) {
168 struct object_entry
**next
= list
;
169 while (next
< last
) {
170 struct object_entry
*entry
= *next
;
171 if (entry
->sha1
[0] != i
)
175 array
[i
] = htonl(next
- sorted_by_sha
);
178 sha1write(f
, array
, 256 * sizeof(int));
181 * Write the actual SHA1 entries..
183 list
= sorted_by_sha
;
184 for (i
= 0; i
< nr_objects
; i
++) {
185 struct object_entry
*entry
= *list
++;
186 unsigned int offset
= htonl(entry
->offset
);
187 sha1write(f
, &offset
, 4);
188 sha1write(f
, entry
->sha1
, 20);
190 sha1write(f
, pack_file_sha1
, 20);
191 sha1close(f
, NULL
, 1);
194 static int add_object_entry(unsigned char *sha1
, unsigned int hash
)
196 unsigned int idx
= nr_objects
;
197 struct object_entry
*entry
;
199 if (incremental
|| local
) {
200 struct packed_git
*p
;
202 for (p
= packed_git
; p
; p
= p
->next
) {
205 if (find_pack_entry_one(sha1
, &e
, p
)) {
208 if (local
&& !p
->pack_local
)
214 if (idx
>= nr_alloc
) {
215 unsigned int needed
= (idx
+ 1024) * 3 / 2;
216 objects
= xrealloc(objects
, needed
* sizeof(*entry
));
219 entry
= objects
+ idx
;
220 memset(entry
, 0, sizeof(*entry
));
221 memcpy(entry
->sha1
, sha1
, 20);
227 static void check_object(struct object_entry
*entry
)
231 if (!sha1_object_info(entry
->sha1
, type
, &entry
->size
)) {
232 if (!strcmp(type
, "commit")) {
233 entry
->type
= OBJ_COMMIT
;
234 } else if (!strcmp(type
, "tree")) {
235 entry
->type
= OBJ_TREE
;
236 } else if (!strcmp(type
, "blob")) {
237 entry
->type
= OBJ_BLOB
;
238 } else if (!strcmp(type
, "tag")) {
239 entry
->type
= OBJ_TAG
;
241 die("unable to pack object %s of type %s",
242 sha1_to_hex(entry
->sha1
), type
);
245 die("unable to get type of object %s",
246 sha1_to_hex(entry
->sha1
));
249 static void get_object_details(void)
252 struct object_entry
*entry
= objects
;
254 for (i
= 0; i
< nr_objects
; i
++)
255 check_object(entry
++);
258 typedef int (*entry_sort_t
)(const struct object_entry
*, const struct object_entry
*);
260 static entry_sort_t current_sort
;
262 static int sort_comparator(const void *_a
, const void *_b
)
264 struct object_entry
*a
= *(struct object_entry
**)_a
;
265 struct object_entry
*b
= *(struct object_entry
**)_b
;
266 return current_sort(a
,b
);
269 static struct object_entry
**create_sorted_list(entry_sort_t sort
)
271 struct object_entry
**list
= xmalloc(nr_objects
* sizeof(struct object_entry
*));
274 for (i
= 0; i
< nr_objects
; i
++)
275 list
[i
] = objects
+ i
;
277 qsort(list
, nr_objects
, sizeof(struct object_entry
*), sort_comparator
);
281 static int sha1_sort(const struct object_entry
*a
, const struct object_entry
*b
)
283 return memcmp(a
->sha1
, b
->sha1
, 20);
286 static int type_size_sort(const struct object_entry
*a
, const struct object_entry
*b
)
288 if (a
->type
< b
->type
)
290 if (a
->type
> b
->type
)
292 if (a
->hash
< b
->hash
)
294 if (a
->hash
> b
->hash
)
296 if (a
->size
< b
->size
)
298 if (a
->size
> b
->size
)
300 return a
< b
? -1 : (a
> b
);
304 struct object_entry
*entry
;
309 * We search for deltas _backwards_ in a list sorted by type and
310 * by size, so that we see progressively smaller and smaller files.
311 * That's because we prefer deltas to be from the bigger file
312 * to the smaller - deletes are potentially cheaper, but perhaps
313 * more importantly, the bigger file is likely the more recent
316 static int try_delta(struct unpacked
*cur
, struct unpacked
*old
, unsigned max_depth
)
318 struct object_entry
*cur_entry
= cur
->entry
;
319 struct object_entry
*old_entry
= old
->entry
;
320 unsigned long size
, oldsize
, delta_size
, sizediff
;
324 /* Don't bother doing diffs between different types */
325 if (cur_entry
->type
!= old_entry
->type
)
328 size
= cur_entry
->size
;
331 oldsize
= old_entry
->size
;
332 sizediff
= oldsize
> size
? oldsize
- size
: size
- oldsize
;
333 if (sizediff
> size
/ 8)
335 if (old_entry
->depth
>= max_depth
)
341 * We always delta from the bigger to the smaller, since that's
342 * more space-efficient (deletes don't have to say _what_ they
345 max_size
= size
/ 2 - 20;
346 if (cur_entry
->delta
)
347 max_size
= cur_entry
->delta_size
-1;
348 if (sizediff
>= max_size
)
350 delta_buf
= diff_delta(old
->data
, oldsize
,
351 cur
->data
, size
, &delta_size
, max_size
);
354 cur_entry
->delta
= old_entry
;
355 cur_entry
->delta_size
= delta_size
;
356 cur_entry
->depth
= old_entry
->depth
+ 1;
361 static void find_deltas(struct object_entry
**list
, int window
, int depth
)
364 unsigned int array_size
= window
* sizeof(struct unpacked
);
365 struct unpacked
*array
= xmalloc(array_size
);
367 memset(array
, 0, array_size
);
371 struct object_entry
*entry
= list
[i
];
372 struct unpacked
*n
= array
+ idx
;
379 n
->data
= read_sha1_file(entry
->sha1
, type
, &size
);
380 if (size
!= entry
->size
)
381 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry
->sha1
), size
, entry
->size
);
384 unsigned int other_idx
= idx
+ j
;
386 if (other_idx
>= window
)
388 m
= array
+ other_idx
;
391 if (try_delta(n
, m
, depth
) < 0)
399 for (i
= 0; i
< window
; ++i
)
404 int main(int argc
, char **argv
)
407 char line
[PATH_MAX
+ 20];
408 int window
= 10, depth
= 10, pack_to_stdout
= 0;
409 struct object_entry
**list
;
412 for (i
= 1; i
< argc
; i
++) {
413 const char *arg
= argv
[i
];
416 if (!strcmp("--non-empty", arg
)) {
420 if (!strcmp("--local", arg
)) {
424 if (!strcmp("--incremental", arg
)) {
428 if (!strncmp("--window=", arg
, 9)) {
430 window
= strtoul(arg
+9, &end
, 0);
435 if (!strncmp("--depth=", arg
, 8)) {
437 depth
= strtoul(arg
+8, &end
, 0);
442 if (!strcmp("--stdout", arg
)) {
453 if (pack_to_stdout
!= !base_name
)
456 prepare_packed_git();
457 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
460 unsigned char sha1
[20];
462 if (get_sha1_hex(line
, sha1
))
463 die("expected sha1, got garbage");
467 unsigned char c
= *p
++;
470 hash
= hash
* 11 + c
;
472 add_object_entry(sha1
, hash
);
474 if (non_empty
&& !nr_objects
)
476 get_object_details();
478 fprintf(stderr
, "Packing %d objects\n", nr_objects
);
480 sorted_by_sha
= create_sorted_list(sha1_sort
);
482 list
= sorted_by_sha
;
483 for (i
= 0; i
< nr_objects
; i
++) {
484 struct object_entry
*entry
= *list
++;
485 SHA1_Update(&ctx
, entry
->sha1
, 20);
487 SHA1_Final(object_list_sha1
, &ctx
);
489 sorted_by_type
= create_sorted_list(type_size_sort
);
491 find_deltas(sorted_by_type
, window
+1, depth
);
494 if (!pack_to_stdout
) {
496 puts(sha1_to_hex(object_list_sha1
));