8 static const char pack_usage
[] = "git-pack-objects [--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 struct object_entry
**sorted_by_sha
, **sorted_by_type
;
22 static struct object_entry
*objects
= NULL
;
23 static int nr_objects
= 0, nr_alloc
= 0;
24 static const char *base_name
;
25 static unsigned char pack_file_sha1
[20];
27 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
29 unsigned long othersize
, delta_size
;
31 void *otherbuf
= read_sha1_file(entry
->delta
->sha1
, type
, &othersize
);
35 die("unable to read %s", sha1_to_hex(entry
->delta
->sha1
));
36 delta_buf
= diff_delta(otherbuf
, othersize
,
37 buf
, size
, &delta_size
, ~0UL);
38 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
39 die("delta size changed");
46 * The per-object header is a pretty dense thing, which is
47 * - first byte: low four bits are "size", then three bits of "type",
48 * and the high bit is "size continues".
49 * - each byte afterwards: low seven bits are size continuation,
50 * with the high bit being "size continues"
52 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
57 if (type
< OBJ_COMMIT
|| type
> OBJ_DELTA
)
58 die("bad type %d", type
);
61 * Shift the size up by 7 bits at a time,
62 * until you get bits in the "high four".
63 * That will be our beginning. We'll have
64 * four size bits in 28..31, then groups
65 * of seven in 21..27, 14..20, 7..13 and
70 while (!(size
& 0xfe000000)) {
75 c
= (type
<< 4) | (size
>> 28);
76 for (i
= 1; i
< n
; i
++) {
78 c
= (size
>> 21) & 0x7f;
85 static unsigned long write_object(struct sha1file
*f
, struct object_entry
*entry
)
89 void *buf
= read_sha1_file(entry
->sha1
, type
, &size
);
90 unsigned char header
[10];
91 unsigned hdrlen
, datalen
;
92 enum object_type obj_type
;
95 die("unable to read %s", sha1_to_hex(entry
->sha1
));
96 if (size
!= entry
->size
)
97 die("object %s size inconsistency (%lu vs %lu)", sha1_to_hex(entry
->sha1
), size
, entry
->size
);
100 * The object header is a byte of 'type' followed by zero or
101 * more bytes of length. For deltas, the 20 bytes of delta sha1
104 obj_type
= entry
->type
;
106 buf
= delta_against(buf
, size
, entry
);
107 size
= entry
->delta_size
;
108 obj_type
= OBJ_DELTA
;
110 hdrlen
= encode_header(obj_type
, size
, header
);
111 sha1write(f
, header
, hdrlen
);
113 sha1write(f
, entry
->delta
, 20);
116 datalen
= sha1write_compressed(f
, buf
, size
);
118 return hdrlen
+ datalen
;
121 static unsigned long write_one(struct sha1file
*f
,
122 struct object_entry
*e
,
123 unsigned long offset
)
126 /* offset starts from header size and cannot be zero
127 * if it is written already.
131 offset
+= write_object(f
, e
);
132 /* if we are delitified, write out its base object. */
134 offset
= write_one(f
, e
->delta
, offset
);
138 static void write_pack_file(void)
142 unsigned long offset
;
144 struct pack_header hdr
;
147 f
= sha1fd(1, "<stdout>");
149 f
= sha1create("%s.%s", base_name
, "pack");
150 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
151 hdr
.hdr_version
= htonl(1);
152 hdr
.hdr_entries
= htonl(nr_objects
);
153 sha1write(f
, &hdr
, sizeof(hdr
));
154 offset
= sizeof(hdr
);
155 for (i
= 0; i
< nr_objects
; i
++)
156 offset
= write_one(f
, objects
+ i
, offset
);
158 sha1close(f
, pack_file_sha1
, 1);
163 static void write_index_file(void)
166 struct sha1file
*f
= sha1create("%s.%s", base_name
, "idx");
167 struct object_entry
**list
= sorted_by_sha
;
168 struct object_entry
**last
= list
+ nr_objects
;
169 unsigned int array
[256];
172 * Write the first-level table (the list is sorted,
173 * but we use a 256-entry lookup to be able to avoid
174 * having to do eight extra binary search iterations).
176 for (i
= 0; i
< 256; i
++) {
177 struct object_entry
**next
= list
;
178 while (next
< last
) {
179 struct object_entry
*entry
= *next
;
180 if (entry
->sha1
[0] != i
)
184 array
[i
] = htonl(next
- sorted_by_sha
);
187 sha1write(f
, array
, 256 * sizeof(int));
190 * Write the actual SHA1 entries..
192 list
= sorted_by_sha
;
193 for (i
= 0; i
< nr_objects
; i
++) {
194 struct object_entry
*entry
= *list
++;
195 unsigned int offset
= htonl(entry
->offset
);
196 sha1write(f
, &offset
, 4);
197 sha1write(f
, entry
->sha1
, 20);
199 sha1write(f
, pack_file_sha1
, 20);
200 sha1close(f
, NULL
, 1);
203 static void add_object_entry(unsigned char *sha1
, unsigned int hash
)
205 unsigned int idx
= nr_objects
;
206 struct object_entry
*entry
;
208 if (idx
>= nr_alloc
) {
209 unsigned int needed
= (idx
+ 1024) * 3 / 2;
210 objects
= xrealloc(objects
, needed
* sizeof(*entry
));
213 entry
= objects
+ idx
;
214 memset(entry
, 0, sizeof(*entry
));
215 memcpy(entry
->sha1
, sha1
, 20);
220 static void check_object(struct object_entry
*entry
)
224 if (!sha1_object_info(entry
->sha1
, type
, &entry
->size
)) {
225 if (!strcmp(type
, "commit")) {
226 entry
->type
= OBJ_COMMIT
;
227 } else if (!strcmp(type
, "tree")) {
228 entry
->type
= OBJ_TREE
;
229 } else if (!strcmp(type
, "blob")) {
230 entry
->type
= OBJ_BLOB
;
231 } else if (!strcmp(type
, "tag")) {
232 entry
->type
= OBJ_TAG
;
234 die("unable to pack object %s of type %s",
235 sha1_to_hex(entry
->sha1
), type
);
238 die("unable to get type of object %s",
239 sha1_to_hex(entry
->sha1
));
242 static void get_object_details(void)
245 struct object_entry
*entry
= objects
;
247 for (i
= 0; i
< nr_objects
; i
++)
248 check_object(entry
++);
251 typedef int (*entry_sort_t
)(const struct object_entry
*, const struct object_entry
*);
253 static entry_sort_t current_sort
;
255 static int sort_comparator(const void *_a
, const void *_b
)
257 struct object_entry
*a
= *(struct object_entry
**)_a
;
258 struct object_entry
*b
= *(struct object_entry
**)_b
;
259 return current_sort(a
,b
);
262 static struct object_entry
**create_sorted_list(entry_sort_t sort
)
264 struct object_entry
**list
= xmalloc(nr_objects
* sizeof(struct object_entry
*));
267 for (i
= 0; i
< nr_objects
; i
++)
268 list
[i
] = objects
+ i
;
270 qsort(list
, nr_objects
, sizeof(struct object_entry
*), sort_comparator
);
274 static int sha1_sort(const struct object_entry
*a
, const struct object_entry
*b
)
276 return memcmp(a
->sha1
, b
->sha1
, 20);
279 static int type_size_sort(const struct object_entry
*a
, const struct object_entry
*b
)
281 if (a
->type
< b
->type
)
283 if (a
->type
> b
->type
)
285 if (a
->hash
< b
->hash
)
287 if (a
->hash
> b
->hash
)
289 if (a
->size
< b
->size
)
291 if (a
->size
> b
->size
)
293 return a
< b
? -1 : (a
> b
);
297 struct object_entry
*entry
;
302 * We search for deltas _backwards_ in a list sorted by type and
303 * by size, so that we see progressively smaller and smaller files.
304 * That's because we prefer deltas to be from the bigger file
305 * to the smaller - deletes are potentially cheaper, but perhaps
306 * more importantly, the bigger file is likely the more recent
309 static int try_delta(struct unpacked
*cur
, struct unpacked
*old
, unsigned max_depth
)
311 struct object_entry
*cur_entry
= cur
->entry
;
312 struct object_entry
*old_entry
= old
->entry
;
313 unsigned long size
, oldsize
, delta_size
, sizediff
;
317 /* Don't bother doing diffs between different types */
318 if (cur_entry
->type
!= old_entry
->type
)
321 size
= cur_entry
->size
;
324 oldsize
= old_entry
->size
;
325 sizediff
= oldsize
> size
? oldsize
- size
: size
- oldsize
;
326 if (sizediff
> size
/ 8)
328 if (old_entry
->depth
>= max_depth
)
334 * We always delta from the bigger to the smaller, since that's
335 * more space-efficient (deletes don't have to say _what_ they
338 max_size
= size
/ 2 - 20;
339 if (cur_entry
->delta
)
340 max_size
= cur_entry
->delta_size
-1;
341 if (sizediff
>= max_size
)
343 delta_buf
= diff_delta(old
->data
, oldsize
,
344 cur
->data
, size
, &delta_size
, max_size
);
347 cur_entry
->delta
= old_entry
;
348 cur_entry
->delta_size
= delta_size
;
349 cur_entry
->depth
= old_entry
->depth
+ 1;
354 static void find_deltas(struct object_entry
**list
, int window
, int depth
)
357 unsigned int array_size
= window
* sizeof(struct unpacked
);
358 struct unpacked
*array
= xmalloc(array_size
);
360 memset(array
, 0, array_size
);
364 struct object_entry
*entry
= list
[i
];
365 struct unpacked
*n
= array
+ idx
;
372 n
->data
= read_sha1_file(entry
->sha1
, type
, &size
);
373 if (size
!= entry
->size
)
374 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry
->sha1
), size
, entry
->size
);
377 unsigned int other_idx
= idx
+ j
;
379 if (other_idx
>= window
)
381 m
= array
+ other_idx
;
384 if (try_delta(n
, m
, depth
) < 0)
393 int main(int argc
, char **argv
)
395 char line
[PATH_MAX
+ 20];
396 int window
= 10, depth
= 10, pack_to_stdout
= 0;
399 for (i
= 1; i
< argc
; i
++) {
400 const char *arg
= argv
[i
];
403 if (!strncmp("--window=", arg
, 9)) {
405 window
= strtoul(arg
+9, &end
, 0);
410 if (!strncmp("--depth=", arg
, 8)) {
412 depth
= strtoul(arg
+8, &end
, 0);
417 if (!strcmp("--stdout", arg
)) {
428 if (pack_to_stdout
!= !base_name
)
431 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
434 unsigned char sha1
[20];
436 if (get_sha1_hex(line
, sha1
))
437 die("expected sha1, got garbage");
441 unsigned char c
= *p
++;
444 hash
= hash
* 11 + c
;
446 add_object_entry(sha1
, hash
);
448 get_object_details();
450 fprintf(stderr
, "Packing %d objects\n", nr_objects
);
452 sorted_by_sha
= create_sorted_list(sha1_sort
);
453 sorted_by_type
= create_sorted_list(type_size_sort
);
455 find_deltas(sorted_by_type
, window
+1, depth
);