12 static int dry_run
, quiet
, recover
, has_errors
;
13 static const char unpack_usage
[] = "git-unpack-objects [-n] [-q] [-r] < pack-file";
15 /* We always read in 4kB chunks. */
16 static unsigned char buffer
[4096];
17 static unsigned int offset
, len
;
18 static off_t consumed_bytes
;
22 * Make sure at least "min" bytes are available in the buffer, and
23 * return the pointer to the buffer.
25 static void *fill(int min
)
28 return buffer
+ offset
;
29 if (min
> sizeof(buffer
))
30 die("cannot fill %d bytes", min
);
32 SHA1_Update(&ctx
, buffer
, offset
);
33 memmove(buffer
, buffer
+ offset
, len
);
37 ssize_t ret
= xread(0, buffer
+ len
, sizeof(buffer
) - len
);
41 die("read error on input: %s", strerror(errno
));
48 static void use(int bytes
)
51 die("used more bytes than were available");
55 /* make sure off_t is sufficiently large not to wrap */
56 if (consumed_bytes
> consumed_bytes
+ bytes
)
57 die("pack too large for current definition of off_t");
58 consumed_bytes
+= bytes
;
61 static void *get_data(unsigned long size
)
64 void *buf
= xmalloc(size
);
66 memset(&stream
, 0, sizeof(stream
));
68 stream
.next_out
= buf
;
69 stream
.avail_out
= size
;
70 stream
.next_in
= fill(1);
71 stream
.avail_in
= len
;
75 int ret
= inflate(&stream
, 0);
76 use(len
- stream
.avail_in
);
77 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
80 error("inflate returned %d\n", ret
);
88 stream
.next_in
= fill(1);
89 stream
.avail_in
= len
;
96 unsigned char base_sha1
[20];
101 struct delta_info
*next
;
104 static struct delta_info
*delta_list
;
106 static void add_delta_to_list(unsigned nr
, unsigned const char *base_sha1
,
108 void *delta
, unsigned long size
)
110 struct delta_info
*info
= xmalloc(sizeof(*info
));
112 hashcpy(info
->base_sha1
, base_sha1
);
113 info
->base_offset
= base_offset
;
117 info
->next
= delta_list
;
123 unsigned char sha1
[20];
126 static struct obj_info
*obj_list
;
128 static void added_object(unsigned nr
, enum object_type type
,
129 void *data
, unsigned long size
);
131 static void write_object(unsigned nr
, enum object_type type
,
132 void *buf
, unsigned long size
)
134 if (write_sha1_file(buf
, size
, typename(type
), obj_list
[nr
].sha1
) < 0)
135 die("failed to write object");
136 added_object(nr
, type
, buf
, size
);
139 static void resolve_delta(unsigned nr
, enum object_type type
,
140 void *base
, unsigned long base_size
,
141 void *delta
, unsigned long delta_size
)
144 unsigned long result_size
;
146 result
= patch_delta(base
, base_size
,
150 die("failed to apply delta");
152 write_object(nr
, type
, result
, result_size
);
156 static void added_object(unsigned nr
, enum object_type type
,
157 void *data
, unsigned long size
)
159 struct delta_info
**p
= &delta_list
;
160 struct delta_info
*info
;
162 while ((info
= *p
) != NULL
) {
163 if (!hashcmp(info
->base_sha1
, obj_list
[nr
].sha1
) ||
164 info
->base_offset
== obj_list
[nr
].offset
) {
167 resolve_delta(info
->nr
, type
, data
, size
,
168 info
->delta
, info
->size
);
176 static void unpack_non_delta_entry(enum object_type type
, unsigned long size
,
179 void *buf
= get_data(size
);
182 write_object(nr
, type
, buf
, size
);
186 static void unpack_delta_entry(enum object_type type
, unsigned long delta_size
,
189 void *delta_data
, *base
;
190 unsigned long base_size
;
191 unsigned char base_sha1
[20];
193 if (type
== OBJ_REF_DELTA
) {
194 hashcpy(base_sha1
, fill(20));
196 delta_data
= get_data(delta_size
);
197 if (dry_run
|| !delta_data
) {
201 if (!has_sha1_file(base_sha1
)) {
202 hashcpy(obj_list
[nr
].sha1
, null_sha1
);
203 add_delta_to_list(nr
, base_sha1
, 0, delta_data
, delta_size
);
207 unsigned base_found
= 0;
208 unsigned char *pack
, c
;
210 unsigned lo
, mid
, hi
;
215 base_offset
= c
& 127;
218 if (!base_offset
|| MSB(base_offset
, 7))
219 die("offset value overflow for delta base object");
223 base_offset
= (base_offset
<< 7) + (c
& 127);
225 base_offset
= obj_list
[nr
].offset
- base_offset
;
227 delta_data
= get_data(delta_size
);
228 if (dry_run
|| !delta_data
) {
236 if (base_offset
< obj_list
[mid
].offset
) {
238 } else if (base_offset
> obj_list
[mid
].offset
) {
241 hashcpy(base_sha1
, obj_list
[mid
].sha1
);
242 base_found
= !is_null_sha1(base_sha1
);
247 /* The delta base object is itself a delta that
248 has not been resolved yet. */
249 hashcpy(obj_list
[nr
].sha1
, null_sha1
);
250 add_delta_to_list(nr
, null_sha1
, base_offset
, delta_data
, delta_size
);
255 base
= read_sha1_file(base_sha1
, &type
, &base_size
);
257 error("failed to read delta-pack base object %s",
258 sha1_to_hex(base_sha1
));
264 resolve_delta(nr
, type
, base
, base_size
, delta_data
, delta_size
);
268 static void unpack_one(unsigned nr
)
271 unsigned char *pack
, c
;
273 enum object_type type
;
275 obj_list
[nr
].offset
= consumed_bytes
;
287 size
+= (c
& 0x7f) << shift
;
296 unpack_non_delta_entry(type
, size
, nr
);
300 unpack_delta_entry(type
, size
, nr
);
303 error("bad object type %d", type
);
311 static void unpack_all(void)
314 struct progress progress
;
315 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
316 unsigned nr_objects
= ntohl(hdr
->hdr_entries
);
318 if (ntohl(hdr
->hdr_signature
) != PACK_SIGNATURE
)
319 die("bad pack file");
320 if (!pack_version_ok(hdr
->hdr_version
))
321 die("unknown pack file version %d", ntohl(hdr
->hdr_version
));
322 use(sizeof(struct pack_header
));
325 start_progress(&progress
, "Unpacking objects", nr_objects
);
326 obj_list
= xmalloc(nr_objects
* sizeof(*obj_list
));
327 for (i
= 0; i
< nr_objects
; i
++) {
330 display_progress(&progress
, i
+ 1);
333 stop_progress(&progress
);
336 die("unresolved deltas left after unpacking");
339 int cmd_unpack_objects(int argc
, const char **argv
, const char *prefix
)
342 unsigned char sha1
[20];
344 git_config(git_default_config
);
348 for (i
= 1 ; i
< argc
; i
++) {
349 const char *arg
= argv
[i
];
352 if (!strcmp(arg
, "-n")) {
356 if (!strcmp(arg
, "-q")) {
360 if (!strcmp(arg
, "-r")) {
364 if (!prefixcmp(arg
, "--pack_header=")) {
365 struct pack_header
*hdr
;
368 hdr
= (struct pack_header
*)buffer
;
369 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
370 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
373 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
382 /* We don't take any non-flag arguments now.. Maybe some day */
387 SHA1_Update(&ctx
, buffer
, offset
);
388 SHA1_Final(sha1
, &ctx
);
389 if (hashcmp(fill(20), sha1
))
390 die("final sha1 did not match");
393 /* Write the last part of the buffer to stdout */
395 int ret
= xwrite(1, buffer
+ offset
, len
);