Clean up git-unpack-objects a bit
[git.git] / unpack-objects.c
1 #include "cache.h"
2 #include "object.h"
3 #include "delta.h"
4 #include "pack.h"
5
6 static int dry_run;
7 static const char unpack_usage[] = "git-unpack-objects < pack-file";
8
9 /* We always read in 4kB chunks. */
10 static unsigned char buffer[4096];
11 static unsigned long offset, len, eof;
12 static SHA_CTX ctx;
13
14 /*
15  * Make sure at least "min" bytes are available in the buffer, and
16  * return the pointer to the buffer.
17  */
18 static void * fill(int min)
19 {
20         if (min <= len)
21                 return buffer + offset;
22         if (eof)
23                 die("unable to fill input");
24         if (min > sizeof(buffer))
25                 die("cannot fill %d bytes", min);
26         if (offset) {
27                 SHA1_Update(&ctx, buffer, offset);
28                 memcpy(buffer, buffer + offset, len);
29                 offset = 0;
30         }
31         do {
32                 int ret = read(0, buffer + len, sizeof(buffer) - len);
33                 if (ret <= 0) {
34                         if (!ret)
35                                 die("early EOF");
36                         if (errno == EAGAIN || errno == EINTR)
37                                 continue;
38                         die("read error on input: %s", strerror(errno));
39                 }
40                 len += ret;
41         } while (len < min);
42         return buffer;
43 }
44
45 static void use(int bytes)
46 {
47         if (bytes > len)
48                 die("used more bytes than were available");
49         len -= bytes;
50         offset += bytes;
51 }
52
53 static void *get_data(unsigned long size)
54 {
55         z_stream stream;
56         void *buf = xmalloc(size);
57
58         if (!size)
59                 return buf;
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         }
78         return buf;
79 }
80
81 struct delta_info {
82         unsigned char base_sha1[20];
83         unsigned long size;
84         void *delta;
85         struct delta_info *next;
86 };
87
88 static struct delta_info *delta_list;
89
90 static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
91 {
92         struct delta_info *info = xmalloc(sizeof(*info));
93
94         memcpy(info->base_sha1, base_sha1, 20);
95         info->size = size;
96         info->delta = delta;
97         info->next = delta_list;
98         delta_list = info;
99 }
100
101 static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
102
103 static void write_object(void *buf, unsigned long size, const char *type)
104 {
105         unsigned char sha1[20];
106         if (write_sha1_file(buf, size, type, sha1) < 0)
107                 die("failed to write object");
108         added_object(sha1, type, buf, size);
109 }
110
111 static int resolve_delta(const char *type,
112         void *base, unsigned long base_size, 
113         void *delta, unsigned long delta_size)
114 {
115         void *result;
116         unsigned long result_size;
117
118         result = patch_delta(base, base_size,
119                              delta, delta_size,
120                              &result_size);
121         if (!result)
122                 die("failed to apply delta");
123         free(delta);
124         write_object(result, result_size, type);
125         free(result);
126         return 0;
127 }
128
129 static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
130 {
131         struct delta_info **p = &delta_list;
132         struct delta_info *info;
133
134         while ((info = *p) != NULL) {
135                 if (!memcmp(info->base_sha1, sha1, 20)) {
136                         *p = info->next;
137                         p = &delta_list;
138                         resolve_delta(type, data, size, info->delta, info->size);
139                         free(info);
140                         continue;
141                 }
142                 p = &info->next;
143         }
144 }
145
146 static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
147 {
148         void *buf = get_data(size);
149         const char *type;
150
151         switch (kind) {
152         case OBJ_COMMIT: type = "commit"; break;
153         case OBJ_TREE:   type = "tree"; break;
154         case OBJ_BLOB:   type = "blob"; break;
155         case OBJ_TAG:    type = "tag"; break;
156         default: die("bad type %d", kind);
157         }
158         write_object(buf, size, type);
159         free(buf);
160         return 0;
161 }
162
163 static int unpack_delta_entry(unsigned long delta_size)
164 {
165         void *delta_data, *base;
166         unsigned long base_size;
167         char type[20];
168         unsigned char base_sha1[20];
169
170         memcpy(base_sha1, fill(20), 20);
171         use(20);
172
173         delta_data = get_data(delta_size);
174
175         if (!has_sha1_file(base_sha1)) {
176                 add_delta_to_list(base_sha1, delta_data, delta_size);
177                 return 0;
178         }
179         base = read_sha1_file(base_sha1, type, &base_size);
180         if (!base)
181                 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
182         return resolve_delta(type, base, base_size, delta_data, delta_size);
183 }
184
185 static void unpack_one(void)
186 {
187         unsigned shift;
188         unsigned char *pack, c;
189         unsigned long size;
190         enum object_type type;
191
192         pack = fill(1);
193         c = *pack;
194         use(1);
195         type = (c >> 4) & 7;
196         size = (c & 15);
197         shift = 4;
198         while (c & 0x80) {
199                 pack = fill(1);
200                 c = *pack++;
201                 use(1);
202                 size += (c & 0x7f) << shift;
203                 shift += 7;
204         }
205         switch (type) {
206         case OBJ_COMMIT:
207         case OBJ_TREE:
208         case OBJ_BLOB:
209         case OBJ_TAG:
210                 unpack_non_delta_entry(type, size);
211                 return;
212         case OBJ_DELTA:
213                 unpack_delta_entry(size);
214                 return;
215         default:
216                 die("bad object type %d", type);
217         }
218 }
219
220 /*
221  * We unpack from the end, older files first. Now, usually
222  * there are deltas etc, so we'll not actually write the
223  * objects in that order, but we might as well try..
224  */
225 static void unpack_all(void)
226 {
227         int i;
228         struct pack_header *hdr = fill(sizeof(struct pack_header));
229         unsigned version = ntohl(hdr->hdr_version);
230         unsigned nr_objects = ntohl(hdr->hdr_entries);
231
232         if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
233                 die("bad pack file");
234         if (version != PACK_VERSION)
235                 die("unable to handle pack file version %d", version);
236         fprintf(stderr, "Unpacking %d objects\n", nr_objects);
237
238         use(sizeof(struct pack_header));
239         for (i = 0; i < nr_objects; i++)
240                 unpack_one();
241         if (delta_list)
242                 die("unresolved deltas left after unpacking");
243 }
244
245 int main(int argc, char **argv)
246 {
247         int i;
248         unsigned char sha1[20];
249
250         for (i = 1 ; i < argc; i++) {
251                 const char *arg = argv[i];
252
253                 if (*arg == '-') {
254                         if (!strcmp(arg, "-n")) {
255                                 dry_run = 1;
256                                 continue;
257                         }
258                         usage(unpack_usage);
259                 }
260
261                 /* We don't take any non-flag arguments now.. Maybe some day */
262                 usage(unpack_usage);
263         }
264         SHA1_Init(&ctx);
265         unpack_all();
266         SHA1_Update(&ctx, buffer, offset);
267         SHA1_Final(sha1, &ctx);
268         if (memcmp(fill(20), sha1, 20))
269                 die("final sha1 did not match");
270         use(20);
271
272         /* Write the last part of the buffer to stdout */
273         while (len) {
274                 int ret = write(1, buffer + offset, len);
275                 if (!ret)
276                         break;
277                 if (ret < 0) {
278                         if (errno == EAGAIN || errno == EINTR)
279                                 continue;
280                         break;
281                 }
282                 len -= ret;
283                 offset += ret;
284         }
285
286         /* All done */
287         return 0;
288 }