]>
Commit | Line | Data |
---|---|---|
bad50dc8 | 1 | #include "cache.h" |
74536958 | 2 | #include "object.h" |
8ee378a0 | 3 | #include "delta.h" |
a733cb60 | 4 | #include "pack.h" |
bad50dc8 | 5 | |
d36f7b80 LT |
6 | #include <sys/time.h> |
7 | ||
8 | static int dry_run, quiet; | |
87b7b841 | 9 | static const char unpack_usage[] = "git-unpack-objects [-q] < pack-file"; |
bad50dc8 | 10 | |
67e5a5ec LT |
11 | /* We always read in 4kB chunks. */ |
12 | static unsigned char buffer[4096]; | |
13 | static unsigned long offset, len, eof; | |
14 | static SHA_CTX ctx; | |
bad50dc8 | 15 | |
67e5a5ec LT |
16 | /* |
17 | * Make sure at least "min" bytes are available in the buffer, and | |
18 | * return the pointer to the buffer. | |
19 | */ | |
20 | static void * fill(int min) | |
21 | { | |
22 | if (min <= len) | |
23 | return buffer + offset; | |
24 | if (eof) | |
25 | die("unable to fill input"); | |
26 | if (min > sizeof(buffer)) | |
27 | die("cannot fill %d bytes", min); | |
28 | if (offset) { | |
29 | SHA1_Update(&ctx, buffer, offset); | |
30 | memcpy(buffer, buffer + offset, len); | |
31 | offset = 0; | |
32 | } | |
33 | do { | |
34 | int ret = read(0, buffer + len, sizeof(buffer) - len); | |
35 | if (ret <= 0) { | |
36 | if (!ret) | |
37 | die("early EOF"); | |
38 | if (errno == EAGAIN || errno == EINTR) | |
39 | continue; | |
40 | die("read error on input: %s", strerror(errno)); | |
41 | } | |
42 | len += ret; | |
43 | } while (len < min); | |
44 | return buffer; | |
45 | } | |
74536958 | 46 | |
67e5a5ec LT |
47 | static void use(int bytes) |
48 | { | |
49 | if (bytes > len) | |
50 | die("used more bytes than were available"); | |
51 | len -= bytes; | |
52 | offset += bytes; | |
53 | } | |
bad50dc8 | 54 | |
67e5a5ec | 55 | static void *get_data(unsigned long size) |
bad50dc8 | 56 | { |
67e5a5ec LT |
57 | z_stream stream; |
58 | void *buf = xmalloc(size); | |
59 | ||
67e5a5ec LT |
60 | memset(&stream, 0, sizeof(stream)); |
61 | ||
62 | stream.next_out = buf; | |
63 | stream.avail_out = size; | |
64 | stream.next_in = fill(1); | |
65 | stream.avail_in = len; | |
66 | inflateInit(&stream); | |
67 | ||
68 | for (;;) { | |
69 | int ret = inflate(&stream, 0); | |
70 | use(len - stream.avail_in); | |
71 | if (stream.total_out == size && ret == Z_STREAM_END) | |
72 | break; | |
73 | if (ret != Z_OK) | |
74 | die("inflate returned %d\n", ret); | |
75 | stream.next_in = fill(1); | |
76 | stream.avail_in = len; | |
77 | } | |
ee639140 | 78 | inflateEnd(&stream); |
67e5a5ec | 79 | return buf; |
bad50dc8 LT |
80 | } |
81 | ||
67e5a5ec LT |
82 | struct delta_info { |
83 | unsigned char base_sha1[20]; | |
84 | unsigned long size; | |
85 | void *delta; | |
86 | struct delta_info *next; | |
87 | }; | |
88 | ||
89 | static struct delta_info *delta_list; | |
90 | ||
91 | static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size) | |
bad50dc8 | 92 | { |
67e5a5ec | 93 | struct delta_info *info = xmalloc(sizeof(*info)); |
bad50dc8 | 94 | |
67e5a5ec LT |
95 | memcpy(info->base_sha1, base_sha1, 20); |
96 | info->size = size; | |
97 | info->delta = delta; | |
98 | info->next = delta_list; | |
99 | delta_list = info; | |
bad50dc8 LT |
100 | } |
101 | ||
cca7081a | 102 | static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size); |
67e5a5ec | 103 | |
cca7081a LT |
104 | static void write_object(void *buf, unsigned long size, const char *type) |
105 | { | |
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); | |
110 | } | |
111 | ||
112 | static int resolve_delta(const char *type, | |
67e5a5ec LT |
113 | void *base, unsigned long base_size, |
114 | void *delta, unsigned long delta_size) | |
bad50dc8 | 115 | { |
67e5a5ec LT |
116 | void *result; |
117 | unsigned long result_size; | |
bad50dc8 | 118 | |
67e5a5ec LT |
119 | result = patch_delta(base, base_size, |
120 | delta, delta_size, | |
121 | &result_size); | |
122 | if (!result) | |
123 | die("failed to apply delta"); | |
124 | free(delta); | |
cca7081a | 125 | write_object(result, result_size, type); |
67e5a5ec | 126 | free(result); |
bad50dc8 LT |
127 | return 0; |
128 | } | |
129 | ||
cca7081a | 130 | static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size) |
74536958 | 131 | { |
67e5a5ec LT |
132 | struct delta_info **p = &delta_list; |
133 | struct delta_info *info; | |
134 | ||
135 | while ((info = *p) != NULL) { | |
136 | if (!memcmp(info->base_sha1, sha1, 20)) { | |
137 | *p = info->next; | |
138 | p = &delta_list; | |
139 | resolve_delta(type, data, size, info->delta, info->size); | |
140 | free(info); | |
141 | continue; | |
142 | } | |
143 | p = &info->next; | |
144 | } | |
145 | } | |
146 | ||
147 | static int unpack_non_delta_entry(enum object_type kind, unsigned long size) | |
148 | { | |
149 | void *buf = get_data(size); | |
cca7081a | 150 | const char *type; |
8ee378a0 | 151 | |
8ee378a0 | 152 | switch (kind) { |
a733cb60 LT |
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; | |
67e5a5ec | 157 | default: die("bad type %d", kind); |
8ee378a0 | 158 | } |
dddafffe LT |
159 | if (!dry_run) |
160 | write_object(buf, size, type); | |
67e5a5ec | 161 | free(buf); |
8ee378a0 JH |
162 | return 0; |
163 | } | |
164 | ||
67e5a5ec | 165 | static int unpack_delta_entry(unsigned long delta_size) |
8ee378a0 | 166 | { |
67e5a5ec LT |
167 | void *delta_data, *base; |
168 | unsigned long base_size; | |
8ee378a0 | 169 | char type[20]; |
67e5a5ec | 170 | unsigned char base_sha1[20]; |
ee639140 | 171 | int result; |
8ee378a0 | 172 | |
67e5a5ec LT |
173 | memcpy(base_sha1, fill(20), 20); |
174 | use(20); | |
c4fb06c0 | 175 | |
67e5a5ec | 176 | delta_data = get_data(delta_size); |
dddafffe LT |
177 | if (dry_run) { |
178 | free(delta_data); | |
179 | return 0; | |
180 | } | |
8ee378a0 | 181 | |
c4fb06c0 | 182 | if (!has_sha1_file(base_sha1)) { |
67e5a5ec LT |
183 | add_delta_to_list(base_sha1, delta_data, delta_size); |
184 | return 0; | |
8ee378a0 | 185 | } |
c4fb06c0 | 186 | base = read_sha1_file(base_sha1, type, &base_size); |
8ee378a0 | 187 | if (!base) |
c4fb06c0 | 188 | die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1)); |
ee639140 SV |
189 | result = resolve_delta(type, base, base_size, delta_data, delta_size); |
190 | free(base); | |
191 | return result; | |
8ee378a0 JH |
192 | } |
193 | ||
d36f7b80 | 194 | static void unpack_one(unsigned nr, unsigned total) |
8ee378a0 | 195 | { |
01247d87 | 196 | unsigned shift; |
a733cb60 | 197 | unsigned char *pack, c; |
67e5a5ec LT |
198 | unsigned long size; |
199 | enum object_type type; | |
74536958 | 200 | |
67e5a5ec LT |
201 | pack = fill(1); |
202 | c = *pack; | |
203 | use(1); | |
a733cb60 LT |
204 | type = (c >> 4) & 7; |
205 | size = (c & 15); | |
01247d87 | 206 | shift = 4; |
a733cb60 | 207 | while (c & 0x80) { |
67e5a5ec | 208 | pack = fill(1); |
a733cb60 | 209 | c = *pack++; |
67e5a5ec | 210 | use(1); |
01247d87 LT |
211 | size += (c & 0x7f) << shift; |
212 | shift += 7; | |
a733cb60 | 213 | } |
d36f7b80 LT |
214 | if (!quiet) { |
215 | static unsigned long last_sec; | |
216 | static unsigned last_percent; | |
217 | struct timeval now; | |
cf219196 | 218 | unsigned percentage = (nr * 100) / total; |
d36f7b80 LT |
219 | |
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); | |
225 | } | |
226 | } | |
a733cb60 LT |
227 | switch (type) { |
228 | case OBJ_COMMIT: | |
229 | case OBJ_TREE: | |
230 | case OBJ_BLOB: | |
231 | case OBJ_TAG: | |
67e5a5ec | 232 | unpack_non_delta_entry(type, size); |
a733cb60 LT |
233 | return; |
234 | case OBJ_DELTA: | |
67e5a5ec | 235 | unpack_delta_entry(size); |
a733cb60 | 236 | return; |
67e5a5ec LT |
237 | default: |
238 | die("bad object type %d", type); | |
74536958 LT |
239 | } |
240 | } | |
241 | ||
242 | /* | |
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.. | |
246 | */ | |
247 | static void unpack_all(void) | |
248 | { | |
67e5a5ec LT |
249 | int i; |
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); | |
253 | ||
254 | if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE) | |
255 | die("bad pack file"); | |
01247d87 | 256 | if (version != PACK_VERSION) |
67e5a5ec LT |
257 | die("unable to handle pack file version %d", version); |
258 | fprintf(stderr, "Unpacking %d objects\n", nr_objects); | |
259 | ||
260 | use(sizeof(struct pack_header)); | |
261 | for (i = 0; i < nr_objects; i++) | |
cf219196 | 262 | unpack_one(i+1, nr_objects); |
67e5a5ec LT |
263 | if (delta_list) |
264 | die("unresolved deltas left after unpacking"); | |
74536958 LT |
265 | } |
266 | ||
bad50dc8 LT |
267 | int main(int argc, char **argv) |
268 | { | |
269 | int i; | |
67e5a5ec | 270 | unsigned char sha1[20]; |
bad50dc8 LT |
271 | |
272 | for (i = 1 ; i < argc; i++) { | |
273 | const char *arg = argv[i]; | |
274 | ||
275 | if (*arg == '-') { | |
74536958 LT |
276 | if (!strcmp(arg, "-n")) { |
277 | dry_run = 1; | |
278 | continue; | |
279 | } | |
d36f7b80 LT |
280 | if (!strcmp(arg, "-q")) { |
281 | quiet = 1; | |
282 | continue; | |
283 | } | |
bad50dc8 LT |
284 | usage(unpack_usage); |
285 | } | |
67e5a5ec LT |
286 | |
287 | /* We don't take any non-flag arguments now.. Maybe some day */ | |
bad50dc8 | 288 | usage(unpack_usage); |
67e5a5ec LT |
289 | } |
290 | SHA1_Init(&ctx); | |
74536958 | 291 | unpack_all(); |
67e5a5ec LT |
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"); | |
296 | use(20); | |
297 | ||
298 | /* Write the last part of the buffer to stdout */ | |
299 | while (len) { | |
300 | int ret = write(1, buffer + offset, len); | |
301 | if (!ret) | |
302 | break; | |
303 | if (ret < 0) { | |
304 | if (errno == EAGAIN || errno == EINTR) | |
305 | continue; | |
306 | break; | |
307 | } | |
308 | len -= ret; | |
309 | offset += ret; | |
310 | } | |
311 | ||
312 | /* All done */ | |
d36f7b80 LT |
313 | if (!quiet) |
314 | fprintf(stderr, "\n"); | |
bad50dc8 LT |
315 | return 0; |
316 | } |