12 static int dry_run
, quiet
;
13 static const char unpack_usage
[] = "git-unpack-objects [-n] [-q] < pack-file";
15 /* We always read in 4kB chunks. */
16 static unsigned char buffer
[4096];
17 static unsigned long offset
, len
, eof
;
21 * Make sure at least "min" bytes are available in the buffer, and
22 * return the pointer to the buffer.
24 static void * fill(int min
)
27 return buffer
+ offset
;
29 die("unable to fill input");
30 if (min
> sizeof(buffer
))
31 die("cannot fill %d bytes", min
);
33 SHA1_Update(&ctx
, buffer
, offset
);
34 memcpy(buffer
, buffer
+ offset
, len
);
38 int ret
= xread(0, buffer
+ len
, sizeof(buffer
) - len
);
42 die("read error on input: %s", strerror(errno
));
49 static void use(int bytes
)
52 die("used more bytes than were available");
57 static void *get_data(unsigned long size
)
60 void *buf
= xmalloc(size
);
62 memset(&stream
, 0, sizeof(stream
));
64 stream
.next_out
= buf
;
65 stream
.avail_out
= size
;
66 stream
.next_in
= fill(1);
67 stream
.avail_in
= len
;
71 int ret
= inflate(&stream
, 0);
72 use(len
- stream
.avail_in
);
73 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
76 die("inflate returned %d\n", ret
);
77 stream
.next_in
= fill(1);
78 stream
.avail_in
= len
;
85 unsigned char base_sha1
[20];
88 struct delta_info
*next
;
91 static struct delta_info
*delta_list
;
93 static void add_delta_to_list(unsigned char *base_sha1
, void *delta
, unsigned long size
)
95 struct delta_info
*info
= xmalloc(sizeof(*info
));
97 memcpy(info
->base_sha1
, base_sha1
, 20);
100 info
->next
= delta_list
;
104 static void added_object(unsigned char *sha1
, const char *type
, void *data
, unsigned long size
);
106 static void write_object(void *buf
, unsigned long size
, const char *type
)
108 unsigned char sha1
[20];
109 if (write_sha1_file(buf
, size
, type
, sha1
) < 0)
110 die("failed to write object");
111 added_object(sha1
, type
, buf
, size
);
114 static int resolve_delta(const char *type
,
115 void *base
, unsigned long base_size
,
116 void *delta
, unsigned long delta_size
)
119 unsigned long result_size
;
121 result
= patch_delta(base
, base_size
,
125 die("failed to apply delta");
127 write_object(result
, result_size
, type
);
132 static void added_object(unsigned char *sha1
, const char *type
, void *data
, unsigned long size
)
134 struct delta_info
**p
= &delta_list
;
135 struct delta_info
*info
;
137 while ((info
= *p
) != NULL
) {
138 if (!memcmp(info
->base_sha1
, sha1
, 20)) {
141 resolve_delta(type
, data
, size
, info
->delta
, info
->size
);
149 static int unpack_non_delta_entry(enum object_type kind
, unsigned long size
)
151 void *buf
= get_data(size
);
155 case OBJ_COMMIT
: type
= commit_type
; break;
156 case OBJ_TREE
: type
= tree_type
; break;
157 case OBJ_BLOB
: type
= blob_type
; break;
158 case OBJ_TAG
: type
= tag_type
; break;
159 default: die("bad type %d", kind
);
162 write_object(buf
, size
, type
);
167 static int unpack_delta_entry(unsigned long delta_size
)
169 void *delta_data
, *base
;
170 unsigned long base_size
;
172 unsigned char base_sha1
[20];
175 memcpy(base_sha1
, fill(20), 20);
178 delta_data
= get_data(delta_size
);
184 if (!has_sha1_file(base_sha1
)) {
185 add_delta_to_list(base_sha1
, delta_data
, delta_size
);
188 base
= read_sha1_file(base_sha1
, type
, &base_size
);
190 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1
));
191 result
= resolve_delta(type
, base
, base_size
, delta_data
, delta_size
);
196 static void unpack_one(unsigned nr
, unsigned total
)
199 unsigned char *pack
, c
;
201 enum object_type type
;
213 size
+= (c
& 0x7f) << shift
;
217 static unsigned long last_sec
;
218 static unsigned last_percent
;
220 unsigned percentage
= (nr
* 100) / total
;
222 gettimeofday(&now
, NULL
);
223 if (percentage
!= last_percent
|| now
.tv_sec
!= last_sec
) {
224 last_sec
= now
.tv_sec
;
225 last_percent
= percentage
;
226 fprintf(stderr
, "%4u%% (%u/%u) done\r", percentage
, nr
, total
);
234 unpack_non_delta_entry(type
, size
);
237 unpack_delta_entry(size
);
240 die("bad object type %d", type
);
244 static void unpack_all(void)
247 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
248 unsigned nr_objects
= ntohl(hdr
->hdr_entries
);
250 if (ntohl(hdr
->hdr_signature
) != PACK_SIGNATURE
)
251 die("bad pack file");
252 if (!pack_version_ok(hdr
->hdr_version
))
253 die("unknown pack file version %d", ntohl(hdr
->hdr_version
));
254 fprintf(stderr
, "Unpacking %d objects\n", nr_objects
);
256 use(sizeof(struct pack_header
));
257 for (i
= 0; i
< nr_objects
; i
++)
258 unpack_one(i
+1, nr_objects
);
260 die("unresolved deltas left after unpacking");
263 int main(int argc
, char **argv
)
266 unsigned char sha1
[20];
268 setup_git_directory();
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
= xwrite(1, buffer
+ offset
, len
);
309 fprintf(stderr
, "\n");