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