git_dir holds pointers to local strings, hence MUST be const.
[git/kirr.git] / builtin-unpack-objects.c
blobf0ae5c987af79bc5254df7cd3587ed1852e63209
1 #include "builtin.h"
2 #include "cache.h"
3 #include "object.h"
4 #include "delta.h"
5 #include "pack.h"
6 #include "blob.h"
7 #include "commit.h"
8 #include "tag.h"
9 #include "tree.h"
11 #include <sys/time.h>
13 static int dry_run, quiet;
14 static const char unpack_usage[] = "git-unpack-objects [-n] [-q] < pack-file";
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;
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)
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;
38 do {
39 int ret = xread(0, buffer + len, sizeof(buffer) - len);
40 if (ret <= 0) {
41 if (!ret)
42 die("early EOF");
43 die("read error on input: %s", strerror(errno));
45 len += ret;
46 } while (len < min);
47 return buffer;
50 static void use(int bytes)
52 if (bytes > len)
53 die("used more bytes than were available");
54 len -= bytes;
55 offset += bytes;
58 static void *get_data(unsigned long size)
60 z_stream stream;
61 void *buf = xmalloc(size);
63 memset(&stream, 0, sizeof(stream));
65 stream.next_out = buf;
66 stream.avail_out = size;
67 stream.next_in = fill(1);
68 stream.avail_in = len;
69 inflateInit(&stream);
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;
81 inflateEnd(&stream);
82 return buf;
85 struct delta_info {
86 unsigned char base_sha1[20];
87 unsigned long size;
88 void *delta;
89 struct delta_info *next;
92 static struct delta_info *delta_list;
94 static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
96 struct delta_info *info = xmalloc(sizeof(*info));
98 memcpy(info->base_sha1, base_sha1, 20);
99 info->size = size;
100 info->delta = delta;
101 info->next = delta_list;
102 delta_list = info;
105 static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
107 static void write_object(void *buf, unsigned long size, const char *type)
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);
115 static int resolve_delta(const char *type,
116 void *base, unsigned long base_size,
117 void *delta, unsigned long delta_size)
119 void *result;
120 unsigned long result_size;
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);
128 write_object(result, result_size, type);
129 free(result);
130 return 0;
133 static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
135 struct delta_info **p = &delta_list;
136 struct delta_info *info;
138 while ((info = *p) != NULL) {
139 if (!hashcmp(info->base_sha1, sha1)) {
140 *p = info->next;
141 p = &delta_list;
142 resolve_delta(type, data, size, info->delta, info->size);
143 free(info);
144 continue;
146 p = &info->next;
150 static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
152 void *buf = get_data(size);
153 const char *type;
155 switch (kind) {
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;
160 default: die("bad type %d", kind);
162 if (!dry_run)
163 write_object(buf, size, type);
164 free(buf);
165 return 0;
168 static int unpack_delta_entry(unsigned long delta_size)
170 void *delta_data, *base;
171 unsigned long base_size;
172 char type[20];
173 unsigned char base_sha1[20];
174 int result;
176 memcpy(base_sha1, fill(20), 20);
177 use(20);
179 delta_data = get_data(delta_size);
180 if (dry_run) {
181 free(delta_data);
182 return 0;
185 if (!has_sha1_file(base_sha1)) {
186 add_delta_to_list(base_sha1, delta_data, delta_size);
187 return 0;
189 base = read_sha1_file(base_sha1, type, &base_size);
190 if (!base)
191 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
192 result = resolve_delta(type, base, base_size, delta_data, delta_size);
193 free(base);
194 return result;
197 static void unpack_one(unsigned nr, unsigned total)
199 unsigned shift;
200 unsigned char *pack, c;
201 unsigned long size;
202 enum object_type type;
204 pack = fill(1);
205 c = *pack;
206 use(1);
207 type = (c >> 4) & 7;
208 size = (c & 15);
209 shift = 4;
210 while (c & 0x80) {
211 pack = fill(1);
212 c = *pack++;
213 use(1);
214 size += (c & 0x7f) << shift;
215 shift += 7;
217 if (!quiet) {
218 static unsigned long last_sec;
219 static unsigned last_percent;
220 struct timeval now;
221 unsigned percentage = (nr * 100) / total;
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);
230 switch (type) {
231 case OBJ_COMMIT:
232 case OBJ_TREE:
233 case OBJ_BLOB:
234 case OBJ_TAG:
235 unpack_non_delta_entry(type, size);
236 return;
237 case OBJ_DELTA:
238 unpack_delta_entry(size);
239 return;
240 default:
241 die("bad object type %d", type);
245 static void unpack_all(void)
247 int i;
248 struct pack_header *hdr = fill(sizeof(struct pack_header));
249 unsigned nr_objects = ntohl(hdr->hdr_entries);
251 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
252 die("bad pack file");
253 if (!pack_version_ok(hdr->hdr_version))
254 die("unknown pack file version %d", ntohl(hdr->hdr_version));
255 fprintf(stderr, "Unpacking %d objects\n", nr_objects);
257 use(sizeof(struct pack_header));
258 for (i = 0; i < nr_objects; i++)
259 unpack_one(i+1, nr_objects);
260 if (delta_list)
261 die("unresolved deltas left after unpacking");
264 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
266 int i;
267 unsigned char sha1[20];
269 git_config(git_default_config);
271 quiet = !isatty(2);
273 for (i = 1 ; i < argc; i++) {
274 const char *arg = argv[i];
276 if (*arg == '-') {
277 if (!strcmp(arg, "-n")) {
278 dry_run = 1;
279 continue;
281 if (!strcmp(arg, "-q")) {
282 quiet = 1;
283 continue;
285 usage(unpack_usage);
288 /* We don't take any non-flag arguments now.. Maybe some day */
289 usage(unpack_usage);
291 SHA1_Init(&ctx);
292 unpack_all();
293 SHA1_Update(&ctx, buffer, offset);
294 SHA1_Final(sha1, &ctx);
295 if (hashcmp(fill(20), sha1))
296 die("final sha1 did not match");
297 use(20);
299 /* Write the last part of the buffer to stdout */
300 while (len) {
301 int ret = xwrite(1, buffer + offset, len);
302 if (ret <= 0)
303 break;
304 len -= ret;
305 offset += ret;
308 /* All done */
309 if (!quiet)
310 fprintf(stderr, "\n");
311 return 0;