GIT 1.1.6
[git/mingw/4msysgit/kblees.git] / unpack-objects.c
blob4b5b5cb3e22454fe487002f698272a19a1d8861d
1 #include "cache.h"
2 #include "object.h"
3 #include "delta.h"
4 #include "pack.h"
6 #include <sys/time.h>
8 static int dry_run, quiet;
9 static const char unpack_usage[] = "git-unpack-objects [-n] [-q] < pack-file";
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;
17 * Make sure at least "min" bytes are available in the buffer, and
18 * return the pointer to the buffer.
20 static void * fill(int min)
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;
33 do {
34 int ret = xread(0, buffer + len, sizeof(buffer) - len);
35 if (ret <= 0) {
36 if (!ret)
37 die("early EOF");
38 die("read error on input: %s", strerror(errno));
40 len += ret;
41 } while (len < min);
42 return buffer;
45 static void use(int bytes)
47 if (bytes > len)
48 die("used more bytes than were available");
49 len -= bytes;
50 offset += bytes;
53 static void *get_data(unsigned long size)
55 z_stream stream;
56 void *buf = xmalloc(size);
58 memset(&stream, 0, sizeof(stream));
60 stream.next_out = buf;
61 stream.avail_out = size;
62 stream.next_in = fill(1);
63 stream.avail_in = len;
64 inflateInit(&stream);
66 for (;;) {
67 int ret = inflate(&stream, 0);
68 use(len - stream.avail_in);
69 if (stream.total_out == size && ret == Z_STREAM_END)
70 break;
71 if (ret != Z_OK)
72 die("inflate returned %d\n", ret);
73 stream.next_in = fill(1);
74 stream.avail_in = len;
76 inflateEnd(&stream);
77 return buf;
80 struct delta_info {
81 unsigned char base_sha1[20];
82 unsigned long size;
83 void *delta;
84 struct delta_info *next;
87 static struct delta_info *delta_list;
89 static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
91 struct delta_info *info = xmalloc(sizeof(*info));
93 memcpy(info->base_sha1, base_sha1, 20);
94 info->size = size;
95 info->delta = delta;
96 info->next = delta_list;
97 delta_list = info;
100 static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
102 static void write_object(void *buf, unsigned long size, const char *type)
104 unsigned char sha1[20];
105 if (write_sha1_file(buf, size, type, sha1) < 0)
106 die("failed to write object");
107 added_object(sha1, type, buf, size);
110 static int resolve_delta(const char *type,
111 void *base, unsigned long base_size,
112 void *delta, unsigned long delta_size)
114 void *result;
115 unsigned long result_size;
117 result = patch_delta(base, base_size,
118 delta, delta_size,
119 &result_size);
120 if (!result)
121 die("failed to apply delta");
122 free(delta);
123 write_object(result, result_size, type);
124 free(result);
125 return 0;
128 static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
130 struct delta_info **p = &delta_list;
131 struct delta_info *info;
133 while ((info = *p) != NULL) {
134 if (!memcmp(info->base_sha1, sha1, 20)) {
135 *p = info->next;
136 p = &delta_list;
137 resolve_delta(type, data, size, info->delta, info->size);
138 free(info);
139 continue;
141 p = &info->next;
145 static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
147 void *buf = get_data(size);
148 const char *type;
150 switch (kind) {
151 case OBJ_COMMIT: type = "commit"; break;
152 case OBJ_TREE: type = "tree"; break;
153 case OBJ_BLOB: type = "blob"; break;
154 case OBJ_TAG: type = "tag"; break;
155 default: die("bad type %d", kind);
157 if (!dry_run)
158 write_object(buf, size, type);
159 free(buf);
160 return 0;
163 static int unpack_delta_entry(unsigned long delta_size)
165 void *delta_data, *base;
166 unsigned long base_size;
167 char type[20];
168 unsigned char base_sha1[20];
169 int result;
171 memcpy(base_sha1, fill(20), 20);
172 use(20);
174 delta_data = get_data(delta_size);
175 if (dry_run) {
176 free(delta_data);
177 return 0;
180 if (!has_sha1_file(base_sha1)) {
181 add_delta_to_list(base_sha1, delta_data, delta_size);
182 return 0;
184 base = read_sha1_file(base_sha1, type, &base_size);
185 if (!base)
186 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
187 result = resolve_delta(type, base, base_size, delta_data, delta_size);
188 free(base);
189 return result;
192 static void unpack_one(unsigned nr, unsigned total)
194 unsigned shift;
195 unsigned char *pack, c;
196 unsigned long size;
197 enum object_type type;
199 pack = fill(1);
200 c = *pack;
201 use(1);
202 type = (c >> 4) & 7;
203 size = (c & 15);
204 shift = 4;
205 while (c & 0x80) {
206 pack = fill(1);
207 c = *pack++;
208 use(1);
209 size += (c & 0x7f) << shift;
210 shift += 7;
212 if (!quiet) {
213 static unsigned long last_sec;
214 static unsigned last_percent;
215 struct timeval now;
216 unsigned percentage = (nr * 100) / total;
218 gettimeofday(&now, NULL);
219 if (percentage != last_percent || now.tv_sec != last_sec) {
220 last_sec = now.tv_sec;
221 last_percent = percentage;
222 fprintf(stderr, "%4u%% (%u/%u) done\r", percentage, nr, total);
225 switch (type) {
226 case OBJ_COMMIT:
227 case OBJ_TREE:
228 case OBJ_BLOB:
229 case OBJ_TAG:
230 unpack_non_delta_entry(type, size);
231 return;
232 case OBJ_DELTA:
233 unpack_delta_entry(size);
234 return;
235 default:
236 die("bad object type %d", type);
241 * We unpack from the end, older files first. Now, usually
242 * there are deltas etc, so we'll not actually write the
243 * objects in that order, but we might as well try..
245 static void unpack_all(void)
247 int i;
248 struct pack_header *hdr = fill(sizeof(struct pack_header));
249 unsigned version = ntohl(hdr->hdr_version);
250 unsigned nr_objects = ntohl(hdr->hdr_entries);
252 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
253 die("bad pack file");
254 if (version != PACK_VERSION)
255 die("unable to handle pack file version %d", version);
256 fprintf(stderr, "Unpacking %d objects\n", nr_objects);
258 use(sizeof(struct pack_header));
259 for (i = 0; i < nr_objects; i++)
260 unpack_one(i+1, nr_objects);
261 if (delta_list)
262 die("unresolved deltas left after unpacking");
265 int main(int argc, char **argv)
267 int i;
268 unsigned char sha1[20];
270 setup_git_directory();
272 quiet = !isatty(2);
274 for (i = 1 ; i < argc; i++) {
275 const char *arg = argv[i];
277 if (*arg == '-') {
278 if (!strcmp(arg, "-n")) {
279 dry_run = 1;
280 continue;
282 if (!strcmp(arg, "-q")) {
283 quiet = 1;
284 continue;
286 usage(unpack_usage);
289 /* We don't take any non-flag arguments now.. Maybe some day */
290 usage(unpack_usage);
292 SHA1_Init(&ctx);
293 unpack_all();
294 SHA1_Update(&ctx, buffer, offset);
295 SHA1_Final(sha1, &ctx);
296 if (memcmp(fill(20), sha1, 20))
297 die("final sha1 did not match");
298 use(20);
300 /* Write the last part of the buffer to stdout */
301 while (len) {
302 int ret = xwrite(1, buffer + offset, len);
303 if (ret <= 0)
304 break;
305 len -= ret;
306 offset += ret;
309 /* All done */
310 if (!quiet)
311 fprintf(stderr, "\n");
312 return 0;