8 static int dry_run
, quiet
;
9 static const char unpack_usage
[] = "git-unpack-objects [-q] < pack-file";
11 /* We always read in 4kB chunks. */
12 static unsigned char buffer
[4096];
13 static unsigned long offset
, len
, eof
;
17 * Make sure at least "min" bytes are available in the buffer, and
18 * return the pointer to the buffer.
20 static void * fill(int min
)
23 return buffer
+ offset
;
25 die("unable to fill input");
26 if (min
> sizeof(buffer
))
27 die("cannot fill %d bytes", min
);
29 SHA1_Update(&ctx
, buffer
, offset
);
30 memcpy(buffer
, buffer
+ offset
, len
);
34 int ret
= read(0, buffer
+ len
, sizeof(buffer
) - len
);
38 if (errno
== EAGAIN
|| errno
== EINTR
)
40 die("read error on input: %s", strerror(errno
));
47 static void use(int bytes
)
50 die("used more bytes than were available");
55 static void *get_data(unsigned long size
)
58 void *buf
= xmalloc(size
);
60 memset(&stream
, 0, sizeof(stream
));
62 stream
.next_out
= buf
;
63 stream
.avail_out
= size
;
64 stream
.next_in
= fill(1);
65 stream
.avail_in
= len
;
69 int ret
= inflate(&stream
, 0);
70 use(len
- stream
.avail_in
);
71 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
74 die("inflate returned %d\n", ret
);
75 stream
.next_in
= fill(1);
76 stream
.avail_in
= len
;
83 unsigned char base_sha1
[20];
86 struct delta_info
*next
;
89 static struct delta_info
*delta_list
;
91 static void add_delta_to_list(unsigned char *base_sha1
, void *delta
, unsigned long size
)
93 struct delta_info
*info
= xmalloc(sizeof(*info
));
95 memcpy(info
->base_sha1
, base_sha1
, 20);
98 info
->next
= delta_list
;
102 static void added_object(unsigned char *sha1
, const char *type
, void *data
, unsigned long size
);
104 static void write_object(void *buf
, unsigned long size
, const char *type
)
106 unsigned char sha1
[20];
107 if (write_sha1_file(buf
, size
, type
, sha1
) < 0)
108 die("failed to write object");
109 added_object(sha1
, type
, buf
, size
);
112 static int resolve_delta(const char *type
,
113 void *base
, unsigned long base_size
,
114 void *delta
, unsigned long delta_size
)
117 unsigned long result_size
;
119 result
= patch_delta(base
, base_size
,
123 die("failed to apply delta");
125 write_object(result
, result_size
, type
);
130 static void added_object(unsigned char *sha1
, const char *type
, void *data
, unsigned long size
)
132 struct delta_info
**p
= &delta_list
;
133 struct delta_info
*info
;
135 while ((info
= *p
) != NULL
) {
136 if (!memcmp(info
->base_sha1
, sha1
, 20)) {
139 resolve_delta(type
, data
, size
, info
->delta
, info
->size
);
147 static int unpack_non_delta_entry(enum object_type kind
, unsigned long size
)
149 void *buf
= get_data(size
);
153 case OBJ_COMMIT
: type
= "commit"; break;
154 case OBJ_TREE
: type
= "tree"; break;
155 case OBJ_BLOB
: type
= "blob"; break;
156 case OBJ_TAG
: type
= "tag"; break;
157 default: die("bad type %d", kind
);
160 write_object(buf
, size
, type
);
165 static int unpack_delta_entry(unsigned long delta_size
)
167 void *delta_data
, *base
;
168 unsigned long base_size
;
170 unsigned char base_sha1
[20];
173 memcpy(base_sha1
, fill(20), 20);
176 delta_data
= get_data(delta_size
);
182 if (!has_sha1_file(base_sha1
)) {
183 add_delta_to_list(base_sha1
, delta_data
, delta_size
);
186 base
= read_sha1_file(base_sha1
, type
, &base_size
);
188 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1
));
189 result
= resolve_delta(type
, base
, base_size
, delta_data
, delta_size
);
194 static void unpack_one(unsigned nr
, unsigned total
)
197 unsigned char *pack
, c
;
199 enum object_type type
;
211 size
+= (c
& 0x7f) << shift
;
215 static unsigned long last_sec
;
216 static unsigned last_percent
;
218 unsigned percentage
= (nr
* 100) / total
;
220 gettimeofday(&now
, NULL
);
221 if (percentage
!= last_percent
|| now
.tv_sec
!= last_sec
) {
222 last_sec
= now
.tv_sec
;
223 last_percent
= percentage
;
224 fprintf(stderr
, "%4u%% (%u/%u) done\r", percentage
, nr
, total
);
232 unpack_non_delta_entry(type
, size
);
235 unpack_delta_entry(size
);
238 die("bad object type %d", type
);
243 * We unpack from the end, older files first. Now, usually
244 * there are deltas etc, so we'll not actually write the
245 * objects in that order, but we might as well try..
247 static void unpack_all(void)
250 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
251 unsigned version
= ntohl(hdr
->hdr_version
);
252 unsigned nr_objects
= ntohl(hdr
->hdr_entries
);
254 if (ntohl(hdr
->hdr_signature
) != PACK_SIGNATURE
)
255 die("bad pack file");
256 if (version
!= PACK_VERSION
)
257 die("unable to handle pack file version %d", version
);
258 fprintf(stderr
, "Unpacking %d objects\n", nr_objects
);
260 use(sizeof(struct pack_header
));
261 for (i
= 0; i
< nr_objects
; i
++)
262 unpack_one(i
+1, nr_objects
);
264 die("unresolved deltas left after unpacking");
267 int main(int argc
, char **argv
)
270 unsigned char sha1
[20];
272 for (i
= 1 ; i
< argc
; i
++) {
273 const char *arg
= argv
[i
];
276 if (!strcmp(arg
, "-n")) {
280 if (!strcmp(arg
, "-q")) {
287 /* We don't take any non-flag arguments now.. Maybe some day */
292 SHA1_Update(&ctx
, buffer
, offset
);
293 SHA1_Final(sha1
, &ctx
);
294 if (memcmp(fill(20), sha1
, 20))
295 die("final sha1 did not match");
298 /* Write the last part of the buffer to stdout */
300 int ret
= write(1, buffer
+ offset
, len
);
304 if (errno
== EAGAIN
|| errno
== EINTR
)
314 fprintf(stderr
, "\n");