13 static int dry_run
, quiet
;
14 static const char unpack_usage
[] = "git-unpack-objects [-n] [-q] < pack-file";
16 /* We always read in 4kB chunks. */
17 static unsigned char buffer
[4096];
18 static unsigned long offset
, len
, eof
;
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
;
30 die("unable to fill input");
31 if (min
> sizeof(buffer
))
32 die("cannot fill %d bytes", min
);
34 SHA1_Update(&ctx
, buffer
, offset
);
35 memcpy(buffer
, buffer
+ offset
, len
);
39 int ret
= xread(0, buffer
+ len
, sizeof(buffer
) - len
);
43 die("read error on input: %s", strerror(errno
));
50 static void use(int bytes
)
53 die("used more bytes than were available");
58 static void *get_data(unsigned long size
)
61 void *buf
= xmalloc(size
);
63 memset(&stream
, 0, sizeof(stream
));
65 stream
.next_out
= buf
;
66 stream
.avail_out
= size
;
67 stream
.next_in
= fill(1);
68 stream
.avail_in
= len
;
72 int ret
= inflate(&stream
, 0);
73 use(len
- stream
.avail_in
);
74 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
77 die("inflate returned %d\n", ret
);
78 stream
.next_in
= fill(1);
79 stream
.avail_in
= len
;
86 unsigned char base_sha1
[20];
89 struct delta_info
*next
;
92 static struct delta_info
*delta_list
;
94 static void add_delta_to_list(unsigned char *base_sha1
, void *delta
, unsigned long size
)
96 struct delta_info
*info
= xmalloc(sizeof(*info
));
98 hashcpy(info
->base_sha1
, base_sha1
);
101 info
->next
= delta_list
;
105 static void added_object(unsigned char *sha1
, const char *type
, void *data
, unsigned long size
);
107 static void write_object(void *buf
, unsigned long size
, const char *type
)
109 unsigned char sha1
[20];
110 if (write_sha1_file(buf
, size
, type
, sha1
) < 0)
111 die("failed to write object");
112 added_object(sha1
, type
, buf
, size
);
115 static int resolve_delta(const char *type
,
116 void *base
, unsigned long base_size
,
117 void *delta
, unsigned long delta_size
)
120 unsigned long result_size
;
122 result
= patch_delta(base
, base_size
,
126 die("failed to apply delta");
128 write_object(result
, result_size
, type
);
133 static void added_object(unsigned char *sha1
, const char *type
, void *data
, unsigned long size
)
135 struct delta_info
**p
= &delta_list
;
136 struct delta_info
*info
;
138 while ((info
= *p
) != NULL
) {
139 if (!hashcmp(info
->base_sha1
, sha1
)) {
142 resolve_delta(type
, data
, size
, info
->delta
, info
->size
);
150 static int unpack_non_delta_entry(enum object_type kind
, unsigned long size
)
152 void *buf
= get_data(size
);
156 case OBJ_COMMIT
: type
= commit_type
; break;
157 case OBJ_TREE
: type
= tree_type
; break;
158 case OBJ_BLOB
: type
= blob_type
; break;
159 case OBJ_TAG
: type
= tag_type
; break;
160 default: die("bad type %d", kind
);
163 write_object(buf
, size
, type
);
168 static int unpack_delta_entry(unsigned long delta_size
)
170 void *delta_data
, *base
;
171 unsigned long base_size
;
173 unsigned char base_sha1
[20];
176 hashcpy(base_sha1
, fill(20));
179 delta_data
= get_data(delta_size
);
185 if (!has_sha1_file(base_sha1
)) {
186 add_delta_to_list(base_sha1
, delta_data
, delta_size
);
189 base
= read_sha1_file(base_sha1
, type
, &base_size
);
191 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1
));
192 result
= resolve_delta(type
, base
, base_size
, delta_data
, delta_size
);
197 static void unpack_one(unsigned nr
, unsigned total
)
200 unsigned char *pack
, c
;
202 enum object_type type
;
214 size
+= (c
& 0x7f) << shift
;
218 static unsigned long last_sec
;
219 static unsigned last_percent
;
221 unsigned percentage
= (nr
* 100) / total
;
223 gettimeofday(&now
, NULL
);
224 if (percentage
!= last_percent
|| now
.tv_sec
!= last_sec
) {
225 last_sec
= now
.tv_sec
;
226 last_percent
= percentage
;
227 fprintf(stderr
, "%4u%% (%u/%u) done\r", percentage
, nr
, total
);
235 unpack_non_delta_entry(type
, size
);
238 unpack_delta_entry(size
);
241 die("bad object type %d", type
);
245 static void unpack_all(void)
248 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
249 unsigned nr_objects
= ntohl(hdr
->hdr_entries
);
251 if (ntohl(hdr
->hdr_signature
) != PACK_SIGNATURE
)
252 die("bad pack file");
253 if (!pack_version_ok(hdr
->hdr_version
))
254 die("unknown pack file version %d", ntohl(hdr
->hdr_version
));
255 fprintf(stderr
, "Unpacking %d objects\n", nr_objects
);
257 use(sizeof(struct pack_header
));
258 for (i
= 0; i
< nr_objects
; i
++)
259 unpack_one(i
+1, nr_objects
);
261 die("unresolved deltas left after unpacking");
264 int cmd_unpack_objects(int argc
, const char **argv
, const char *prefix
)
267 unsigned char sha1
[20];
269 git_config(git_default_config
);
273 for (i
= 1 ; i
< argc
; i
++) {
274 const char *arg
= argv
[i
];
277 if (!strcmp(arg
, "-n")) {
281 if (!strcmp(arg
, "-q")) {
288 /* We don't take any non-flag arguments now.. Maybe some day */
293 SHA1_Update(&ctx
, buffer
, offset
);
294 SHA1_Final(sha1
, &ctx
);
295 if (hashcmp(fill(20), sha1
))
296 die("final sha1 did not match");
299 /* Write the last part of the buffer to stdout */
301 int ret
= xwrite(1, buffer
+ offset
, len
);
310 fprintf(stderr
, "\n");