13 static int dry_run
, quiet
, recover
, has_errors
;
14 static const char unpack_usage
[] = "git-unpack-objects [-n] [-q] [-r] < pack-file";
16 /* We always read in 4kB chunks. */
17 static unsigned char buffer
[4096];
18 static unsigned long offset
, len
;
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 memcpy(buffer
, buffer
+ offset
, len
);
37 int 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");
56 static void *get_data(unsigned long size
)
59 void *buf
= xmalloc(size
);
61 memset(&stream
, 0, sizeof(stream
));
63 stream
.next_out
= buf
;
64 stream
.avail_out
= size
;
65 stream
.next_in
= fill(1);
66 stream
.avail_in
= len
;
70 int ret
= inflate(&stream
, 0);
71 use(len
- stream
.avail_in
);
72 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
75 error("inflate returned %d\n", ret
);
83 stream
.next_in
= fill(1);
84 stream
.avail_in
= len
;
91 unsigned char base_sha1
[20];
94 struct delta_info
*next
;
97 static struct delta_info
*delta_list
;
99 static void add_delta_to_list(unsigned char *base_sha1
, void *delta
, unsigned long size
)
101 struct delta_info
*info
= xmalloc(sizeof(*info
));
103 hashcpy(info
->base_sha1
, base_sha1
);
106 info
->next
= delta_list
;
110 static void added_object(unsigned char *sha1
, const char *type
, void *data
, unsigned long size
);
112 static void write_object(void *buf
, unsigned long size
, const char *type
)
114 unsigned char sha1
[20];
115 if (write_sha1_file(buf
, size
, type
, sha1
) < 0)
116 die("failed to write object");
117 added_object(sha1
, type
, buf
, size
);
120 static void resolve_delta(const char *type
,
121 void *base
, unsigned long base_size
,
122 void *delta
, unsigned long delta_size
)
125 unsigned long result_size
;
127 result
= patch_delta(base
, base_size
,
131 die("failed to apply delta");
133 write_object(result
, result_size
, type
);
137 static void added_object(unsigned char *sha1
, const char *type
, void *data
, unsigned long size
)
139 struct delta_info
**p
= &delta_list
;
140 struct delta_info
*info
;
142 while ((info
= *p
) != NULL
) {
143 if (!hashcmp(info
->base_sha1
, sha1
)) {
146 resolve_delta(type
, data
, size
, info
->delta
, info
->size
);
154 static void unpack_non_delta_entry(enum object_type kind
, unsigned long size
)
156 void *buf
= get_data(size
);
160 case OBJ_COMMIT
: type
= commit_type
; break;
161 case OBJ_TREE
: type
= tree_type
; break;
162 case OBJ_BLOB
: type
= blob_type
; break;
163 case OBJ_TAG
: type
= tag_type
; break;
164 default: die("bad type %d", kind
);
167 write_object(buf
, size
, type
);
171 static void unpack_delta_entry(unsigned long delta_size
)
173 void *delta_data
, *base
;
174 unsigned long base_size
;
176 unsigned char base_sha1
[20];
178 hashcpy(base_sha1
, fill(20));
181 delta_data
= get_data(delta_size
);
182 if (dry_run
|| !delta_data
) {
187 if (!has_sha1_file(base_sha1
)) {
188 add_delta_to_list(base_sha1
, delta_data
, delta_size
);
191 base
= read_sha1_file(base_sha1
, type
, &base_size
);
193 error("failed to read delta-pack base object %s",
194 sha1_to_hex(base_sha1
));
200 resolve_delta(type
, base
, base_size
, delta_data
, delta_size
);
204 static void unpack_one(unsigned nr
, unsigned total
)
207 unsigned char *pack
, c
;
209 enum object_type type
;
221 size
+= (c
& 0x7f) << shift
;
225 static unsigned long last_sec
;
226 static unsigned last_percent
;
228 unsigned percentage
= (nr
* 100) / total
;
230 gettimeofday(&now
, NULL
);
231 if (percentage
!= last_percent
|| now
.tv_sec
!= last_sec
) {
232 last_sec
= now
.tv_sec
;
233 last_percent
= percentage
;
234 fprintf(stderr
, "%4u%% (%u/%u) done\r", percentage
, nr
, total
);
242 unpack_non_delta_entry(type
, size
);
245 unpack_delta_entry(size
);
248 error("bad object type %d", type
);
256 static void unpack_all(void)
259 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
260 unsigned nr_objects
= ntohl(hdr
->hdr_entries
);
262 if (ntohl(hdr
->hdr_signature
) != PACK_SIGNATURE
)
263 die("bad pack file");
264 if (!pack_version_ok(hdr
->hdr_version
))
265 die("unknown pack file version %d", ntohl(hdr
->hdr_version
));
266 fprintf(stderr
, "Unpacking %d objects\n", nr_objects
);
268 use(sizeof(struct pack_header
));
269 for (i
= 0; i
< nr_objects
; i
++)
270 unpack_one(i
+1, nr_objects
);
272 die("unresolved deltas left after unpacking");
275 int cmd_unpack_objects(int argc
, const char **argv
, const char *prefix
)
278 unsigned char sha1
[20];
280 git_config(git_default_config
);
284 for (i
= 1 ; i
< argc
; i
++) {
285 const char *arg
= argv
[i
];
288 if (!strcmp(arg
, "-n")) {
292 if (!strcmp(arg
, "-q")) {
296 if (!strcmp(arg
, "-r")) {
303 /* We don't take any non-flag arguments now.. Maybe some day */
308 SHA1_Update(&ctx
, buffer
, offset
);
309 SHA1_Final(sha1
, &ctx
);
310 if (hashcmp(fill(20), sha1
))
311 die("final sha1 did not match");
314 /* Write the last part of the buffer to stdout */
316 int ret
= xwrite(1, buffer
+ offset
, len
);
325 fprintf(stderr
, "\n");