10 #include "tree-walk.h"
15 static int dry_run
, quiet
, recover
, has_errors
, strict
;
16 static const char unpack_usage
[] = "git-unpack-objects [-n] [-q] [-r] [--strict] < pack-file";
18 /* We always read in 4kB chunks. */
19 static unsigned char buffer
[4096];
20 static unsigned int offset
, len
;
21 static off_t consumed_bytes
;
29 static struct decoration obj_decorate
;
31 static struct obj_buffer
*lookup_object_buffer(struct object
*base
)
33 return lookup_decoration(&obj_decorate
, base
);
36 static void add_object_buffer(struct object
*object
, char *buffer
, unsigned long size
)
38 struct obj_buffer
*obj
;
39 obj
= xcalloc(1, sizeof(struct obj_buffer
));
42 if (add_decoration(&obj_decorate
, object
, obj
))
43 die("object %s tried to add buffer twice!", sha1_to_hex(object
->sha1
));
47 * Make sure at least "min" bytes are available in the buffer, and
48 * return the pointer to the buffer.
50 static void *fill(int min
)
53 return buffer
+ offset
;
54 if (min
> sizeof(buffer
))
55 die("cannot fill %d bytes", min
);
57 SHA1_Update(&ctx
, buffer
, offset
);
58 memmove(buffer
, buffer
+ offset
, len
);
62 ssize_t ret
= xread(0, buffer
+ len
, sizeof(buffer
) - len
);
66 die("read error on input: %s", strerror(errno
));
73 static void use(int bytes
)
76 die("used more bytes than were available");
80 /* make sure off_t is sufficiently large not to wrap */
81 if (consumed_bytes
> consumed_bytes
+ bytes
)
82 die("pack too large for current definition of off_t");
83 consumed_bytes
+= bytes
;
86 static void *get_data(unsigned long size
)
89 void *buf
= xmalloc(size
);
91 memset(&stream
, 0, sizeof(stream
));
93 stream
.next_out
= buf
;
94 stream
.avail_out
= size
;
95 stream
.next_in
= fill(1);
96 stream
.avail_in
= len
;
100 int ret
= inflate(&stream
, 0);
101 use(len
- stream
.avail_in
);
102 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
105 error("inflate returned %d\n", ret
);
113 stream
.next_in
= fill(1);
114 stream
.avail_in
= len
;
121 unsigned char base_sha1
[20];
126 struct delta_info
*next
;
129 static struct delta_info
*delta_list
;
131 static void add_delta_to_list(unsigned nr
, unsigned const char *base_sha1
,
133 void *delta
, unsigned long size
)
135 struct delta_info
*info
= xmalloc(sizeof(*info
));
137 hashcpy(info
->base_sha1
, base_sha1
);
138 info
->base_offset
= base_offset
;
142 info
->next
= delta_list
;
148 unsigned char sha1
[20];
152 #define FLAG_OPEN (1u<<20)
153 #define FLAG_WRITTEN (1u<<21)
155 static struct obj_info
*obj_list
;
158 static void write_cached_object(struct object
*obj
)
160 unsigned char sha1
[20];
161 struct obj_buffer
*obj_buf
= lookup_object_buffer(obj
);
162 if (write_sha1_file(obj_buf
->buffer
, obj_buf
->size
, typename(obj
->type
), sha1
) < 0)
163 die("failed to write object %s", sha1_to_hex(obj
->sha1
));
164 obj
->flags
|= FLAG_WRITTEN
;
167 static int check_object(struct object
*obj
, int type
, void *data
)
172 if (obj
->flags
& FLAG_WRITTEN
)
175 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
176 die("object type mismatch");
178 if (!(obj
->flags
& FLAG_OPEN
)) {
180 int type
= sha1_object_info(obj
->sha1
, &size
);
181 if (type
!= obj
->type
|| type
<= 0)
182 die("object of unexpected type");
183 obj
->flags
|= FLAG_WRITTEN
;
187 if (fsck_object(obj
, 1, fsck_error_function
))
188 die("Error in object");
189 if (!fsck_walk(obj
, check_object
, 0))
190 die("Error on reachable objects of %s", sha1_to_hex(obj
->sha1
));
191 write_cached_object(obj
);
195 static void write_rest(void)
198 for (i
= 0; i
< nr_objects
; i
++)
199 check_object(obj_list
[i
].obj
, OBJ_ANY
, 0);
202 static void added_object(unsigned nr
, enum object_type type
,
203 void *data
, unsigned long size
);
205 static void write_object(unsigned nr
, enum object_type type
,
206 void *buf
, unsigned long size
)
208 added_object(nr
, type
, buf
, size
);
210 if (write_sha1_file(buf
, size
, typename(type
), obj_list
[nr
].sha1
) < 0)
211 die("failed to write object");
213 obj_list
[nr
].obj
= 0;
214 } else if (type
== OBJ_BLOB
) {
216 if (write_sha1_file(buf
, size
, typename(type
), obj_list
[nr
].sha1
) < 0)
217 die("failed to write object");
220 blob
= lookup_blob(obj_list
[nr
].sha1
);
222 blob
->object
.flags
|= FLAG_WRITTEN
;
224 die("invalid blob object");
225 obj_list
[nr
].obj
= 0;
229 hash_sha1_file(buf
, size
, typename(type
), obj_list
[nr
].sha1
);
230 obj
= parse_object_buffer(obj_list
[nr
].sha1
, type
, size
, buf
, &eaten
);
232 die("invalid %s", typename(type
));
233 /* buf is stored via add_object_buffer and in obj, if its a tree or commit */
234 add_object_buffer(obj
, buf
, size
);
235 obj
->flags
|= FLAG_OPEN
;
236 obj_list
[nr
].obj
= obj
;
240 static void resolve_delta(unsigned nr
, enum object_type type
,
241 void *base
, unsigned long base_size
,
242 void *delta
, unsigned long delta_size
)
245 unsigned long result_size
;
247 result
= patch_delta(base
, base_size
,
251 die("failed to apply delta");
253 write_object(nr
, type
, result
, result_size
);
256 static void added_object(unsigned nr
, enum object_type type
,
257 void *data
, unsigned long size
)
259 struct delta_info
**p
= &delta_list
;
260 struct delta_info
*info
;
262 while ((info
= *p
) != NULL
) {
263 if (!hashcmp(info
->base_sha1
, obj_list
[nr
].sha1
) ||
264 info
->base_offset
== obj_list
[nr
].offset
) {
267 resolve_delta(info
->nr
, type
, data
, size
,
268 info
->delta
, info
->size
);
276 static void unpack_non_delta_entry(enum object_type type
, unsigned long size
,
279 void *buf
= get_data(size
);
282 write_object(nr
, type
, buf
, size
);
287 static void unpack_delta_entry(enum object_type type
, unsigned long delta_size
,
290 void *delta_data
, *base
;
291 unsigned long base_size
;
292 unsigned char base_sha1
[20];
295 if (type
== OBJ_REF_DELTA
) {
296 hashcpy(base_sha1
, fill(20));
298 delta_data
= get_data(delta_size
);
299 if (dry_run
|| !delta_data
) {
303 if (!has_sha1_file(base_sha1
)) {
304 hashcpy(obj_list
[nr
].sha1
, null_sha1
);
305 add_delta_to_list(nr
, base_sha1
, 0, delta_data
, delta_size
);
309 unsigned base_found
= 0;
310 unsigned char *pack
, c
;
312 unsigned lo
, mid
, hi
;
317 base_offset
= c
& 127;
320 if (!base_offset
|| MSB(base_offset
, 7))
321 die("offset value overflow for delta base object");
325 base_offset
= (base_offset
<< 7) + (c
& 127);
327 base_offset
= obj_list
[nr
].offset
- base_offset
;
329 delta_data
= get_data(delta_size
);
330 if (dry_run
|| !delta_data
) {
338 if (base_offset
< obj_list
[mid
].offset
) {
340 } else if (base_offset
> obj_list
[mid
].offset
) {
343 hashcpy(base_sha1
, obj_list
[mid
].sha1
);
344 base_found
= !is_null_sha1(base_sha1
);
349 /* The delta base object is itself a delta that
350 has not been resolved yet. */
351 hashcpy(obj_list
[nr
].sha1
, null_sha1
);
352 add_delta_to_list(nr
, null_sha1
, base_offset
, delta_data
, delta_size
);
357 obj
= lookup_object(base_sha1
);
359 struct obj_buffer
*obj_buf
= lookup_object_buffer(obj
);
361 resolve_delta(nr
, obj
->type
, obj_buf
->buffer
, obj_buf
->size
, delta_data
, delta_size
);
366 base
= read_sha1_file(base_sha1
, &type
, &base_size
);
368 error("failed to read delta-pack base object %s",
369 sha1_to_hex(base_sha1
));
375 resolve_delta(nr
, type
, base
, base_size
, delta_data
, delta_size
);
379 static void unpack_one(unsigned nr
)
382 unsigned char *pack
, c
;
384 enum object_type type
;
386 obj_list
[nr
].offset
= consumed_bytes
;
398 size
+= (c
& 0x7f) << shift
;
407 unpack_non_delta_entry(type
, size
, nr
);
411 unpack_delta_entry(type
, size
, nr
);
414 error("bad object type %d", type
);
422 static void unpack_all(void)
425 struct progress
*progress
= NULL
;
426 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
428 nr_objects
= ntohl(hdr
->hdr_entries
);
430 if (ntohl(hdr
->hdr_signature
) != PACK_SIGNATURE
)
431 die("bad pack file");
432 if (!pack_version_ok(hdr
->hdr_version
))
433 die("unknown pack file version %d", ntohl(hdr
->hdr_version
));
434 use(sizeof(struct pack_header
));
437 progress
= start_progress("Unpacking objects", nr_objects
);
438 obj_list
= xmalloc(nr_objects
* sizeof(*obj_list
));
439 memset(obj_list
, 0, nr_objects
* sizeof(*obj_list
));
440 for (i
= 0; i
< nr_objects
; i
++) {
442 display_progress(progress
, i
+ 1);
444 stop_progress(&progress
);
447 die("unresolved deltas left after unpacking");
450 int cmd_unpack_objects(int argc
, const char **argv
, const char *prefix
)
453 unsigned char sha1
[20];
455 git_config(git_default_config
);
459 for (i
= 1 ; i
< argc
; i
++) {
460 const char *arg
= argv
[i
];
463 if (!strcmp(arg
, "-n")) {
467 if (!strcmp(arg
, "-q")) {
471 if (!strcmp(arg
, "-r")) {
475 if (!strcmp(arg
, "--strict")) {
479 if (!prefixcmp(arg
, "--pack_header=")) {
480 struct pack_header
*hdr
;
483 hdr
= (struct pack_header
*)buffer
;
484 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
485 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
488 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
497 /* We don't take any non-flag arguments now.. Maybe some day */
502 SHA1_Update(&ctx
, buffer
, offset
);
503 SHA1_Final(sha1
, &ctx
);
506 if (hashcmp(fill(20), sha1
))
507 die("final sha1 did not match");
510 /* Write the last part of the buffer to stdout */
512 int ret
= xwrite(1, buffer
+ offset
, len
);