7 static const char unpack_usage
[] = "git-unpack-objects < pack-file";
9 /* We always read in 4kB chunks. */
10 static unsigned char buffer
[4096];
11 static unsigned long offset
, len
, eof
;
15 * Make sure at least "min" bytes are available in the buffer, and
16 * return the pointer to the buffer.
18 static void * fill(int min
)
21 return buffer
+ offset
;
23 die("unable to fill input");
24 if (min
> sizeof(buffer
))
25 die("cannot fill %d bytes", min
);
27 SHA1_Update(&ctx
, buffer
, offset
);
28 memcpy(buffer
, buffer
+ offset
, len
);
32 int ret
= read(0, buffer
+ len
, sizeof(buffer
) - len
);
36 if (errno
== EAGAIN
|| errno
== EINTR
)
38 die("read error on input: %s", strerror(errno
));
45 static void use(int bytes
)
48 die("used more bytes than were available");
53 static void *get_data(unsigned long size
)
56 void *buf
= xmalloc(size
);
58 memset(&stream
, 0, sizeof(stream
));
60 stream
.next_out
= buf
;
61 stream
.avail_out
= size
;
62 stream
.next_in
= fill(1);
63 stream
.avail_in
= len
;
67 int ret
= inflate(&stream
, 0);
68 use(len
- stream
.avail_in
);
69 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
72 die("inflate returned %d\n", ret
);
73 stream
.next_in
= fill(1);
74 stream
.avail_in
= len
;
80 unsigned char base_sha1
[20];
83 struct delta_info
*next
;
86 static struct delta_info
*delta_list
;
88 static void add_delta_to_list(unsigned char *base_sha1
, void *delta
, unsigned long size
)
90 struct delta_info
*info
= xmalloc(sizeof(*info
));
92 memcpy(info
->base_sha1
, base_sha1
, 20);
95 info
->next
= delta_list
;
99 static void added_object(unsigned char *sha1
, const char *type
, void *data
, unsigned long size
);
101 static void write_object(void *buf
, unsigned long size
, const char *type
)
103 unsigned char sha1
[20];
104 if (write_sha1_file(buf
, size
, type
, sha1
) < 0)
105 die("failed to write object");
106 added_object(sha1
, type
, buf
, size
);
109 static int resolve_delta(const char *type
,
110 void *base
, unsigned long base_size
,
111 void *delta
, unsigned long delta_size
)
114 unsigned long result_size
;
116 result
= patch_delta(base
, base_size
,
120 die("failed to apply delta");
122 write_object(result
, result_size
, type
);
127 static void added_object(unsigned char *sha1
, const char *type
, void *data
, unsigned long size
)
129 struct delta_info
**p
= &delta_list
;
130 struct delta_info
*info
;
132 while ((info
= *p
) != NULL
) {
133 if (!memcmp(info
->base_sha1
, sha1
, 20)) {
136 resolve_delta(type
, data
, size
, info
->delta
, info
->size
);
144 static int unpack_non_delta_entry(enum object_type kind
, unsigned long size
)
146 void *buf
= get_data(size
);
150 case OBJ_COMMIT
: type
= "commit"; break;
151 case OBJ_TREE
: type
= "tree"; break;
152 case OBJ_BLOB
: type
= "blob"; break;
153 case OBJ_TAG
: type
= "tag"; break;
154 default: die("bad type %d", kind
);
157 write_object(buf
, size
, type
);
162 static int unpack_delta_entry(unsigned long delta_size
)
164 void *delta_data
, *base
;
165 unsigned long base_size
;
167 unsigned char base_sha1
[20];
169 memcpy(base_sha1
, fill(20), 20);
172 delta_data
= get_data(delta_size
);
178 if (!has_sha1_file(base_sha1
)) {
179 add_delta_to_list(base_sha1
, delta_data
, delta_size
);
182 base
= read_sha1_file(base_sha1
, type
, &base_size
);
184 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1
));
185 return resolve_delta(type
, base
, base_size
, delta_data
, delta_size
);
188 static void unpack_one(void)
191 unsigned char *pack
, c
;
193 enum object_type type
;
205 size
+= (c
& 0x7f) << shift
;
213 unpack_non_delta_entry(type
, size
);
216 unpack_delta_entry(size
);
219 die("bad object type %d", type
);
224 * We unpack from the end, older files first. Now, usually
225 * there are deltas etc, so we'll not actually write the
226 * objects in that order, but we might as well try..
228 static void unpack_all(void)
231 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
232 unsigned version
= ntohl(hdr
->hdr_version
);
233 unsigned nr_objects
= ntohl(hdr
->hdr_entries
);
235 if (ntohl(hdr
->hdr_signature
) != PACK_SIGNATURE
)
236 die("bad pack file");
237 if (version
!= PACK_VERSION
)
238 die("unable to handle pack file version %d", version
);
239 fprintf(stderr
, "Unpacking %d objects\n", nr_objects
);
241 use(sizeof(struct pack_header
));
242 for (i
= 0; i
< nr_objects
; i
++)
245 die("unresolved deltas left after unpacking");
248 int main(int argc
, char **argv
)
251 unsigned char sha1
[20];
253 for (i
= 1 ; i
< argc
; i
++) {
254 const char *arg
= argv
[i
];
257 if (!strcmp(arg
, "-n")) {
264 /* We don't take any non-flag arguments now.. Maybe some day */
269 SHA1_Update(&ctx
, buffer
, offset
);
270 SHA1_Final(sha1
, &ctx
);
271 if (memcmp(fill(20), sha1
, 20))
272 die("final sha1 did not match");
275 /* Write the last part of the buffer to stdout */
277 int ret
= write(1, buffer
+ offset
, len
);
281 if (errno
== EAGAIN
|| errno
== EINTR
)