]> git.ipfire.org Git - thirdparty/git.git/blame - unpack-objects.c
Merge branch 'ml/cvsserver'
[thirdparty/git.git] / unpack-objects.c
CommitLineData
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
8static int dry_run, quiet;
b2309b70 9static const char unpack_usage[] = "git-unpack-objects [-n] [-q] < pack-file";
bad50dc8 10
67e5a5ec
LT
11/* We always read in 4kB chunks. */
12static unsigned char buffer[4096];
13static unsigned long offset, len, eof;
14static 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 */
20static 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 {
1c15afb9 34 int ret = xread(0, buffer + len, sizeof(buffer) - len);
67e5a5ec
LT
35 if (ret <= 0) {
36 if (!ret)
37 die("early EOF");
67e5a5ec
LT
38 die("read error on input: %s", strerror(errno));
39 }
40 len += ret;
41 } while (len < min);
42 return buffer;
43}
74536958 44
67e5a5ec
LT
45static void use(int bytes)
46{
47 if (bytes > len)
48 die("used more bytes than were available");
49 len -= bytes;
50 offset += bytes;
51}
bad50dc8 52
67e5a5ec 53static void *get_data(unsigned long size)
bad50dc8 54{
67e5a5ec
LT
55 z_stream stream;
56 void *buf = xmalloc(size);
57
67e5a5ec
LT
58 memset(&stream, 0, sizeof(stream));
59
60 stream.next_out = buf;
61 stream.avail_out = size;
62 stream.next_in = fill(1);
63 stream.avail_in = len;
64 inflateInit(&stream);
65
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;
75 }
ee639140 76 inflateEnd(&stream);
67e5a5ec 77 return buf;
bad50dc8
LT
78}
79
67e5a5ec
LT
80struct delta_info {
81 unsigned char base_sha1[20];
82 unsigned long size;
83 void *delta;
84 struct delta_info *next;
85};
86
87static struct delta_info *delta_list;
88
89static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
bad50dc8 90{
67e5a5ec 91 struct delta_info *info = xmalloc(sizeof(*info));
bad50dc8 92
67e5a5ec
LT
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;
bad50dc8
LT
98}
99
cca7081a 100static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
67e5a5ec 101
cca7081a
LT
102static void write_object(void *buf, unsigned long size, const char *type)
103{
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);
108}
109
110static int resolve_delta(const char *type,
67e5a5ec
LT
111 void *base, unsigned long base_size,
112 void *delta, unsigned long delta_size)
bad50dc8 113{
67e5a5ec
LT
114 void *result;
115 unsigned long result_size;
bad50dc8 116
67e5a5ec
LT
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);
cca7081a 123 write_object(result, result_size, type);
67e5a5ec 124 free(result);
bad50dc8
LT
125 return 0;
126}
127
cca7081a 128static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
74536958 129{
67e5a5ec
LT
130 struct delta_info **p = &delta_list;
131 struct delta_info *info;
132
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;
140 }
141 p = &info->next;
142 }
143}
144
145static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
146{
147 void *buf = get_data(size);
cca7081a 148 const char *type;
8ee378a0 149
8ee378a0 150 switch (kind) {
a733cb60
LT
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;
67e5a5ec 155 default: die("bad type %d", kind);
8ee378a0 156 }
dddafffe
LT
157 if (!dry_run)
158 write_object(buf, size, type);
67e5a5ec 159 free(buf);
8ee378a0
JH
160 return 0;
161}
162
67e5a5ec 163static int unpack_delta_entry(unsigned long delta_size)
8ee378a0 164{
67e5a5ec
LT
165 void *delta_data, *base;
166 unsigned long base_size;
8ee378a0 167 char type[20];
67e5a5ec 168 unsigned char base_sha1[20];
ee639140 169 int result;
8ee378a0 170
67e5a5ec
LT
171 memcpy(base_sha1, fill(20), 20);
172 use(20);
c4fb06c0 173
67e5a5ec 174 delta_data = get_data(delta_size);
dddafffe
LT
175 if (dry_run) {
176 free(delta_data);
177 return 0;
178 }
8ee378a0 179
c4fb06c0 180 if (!has_sha1_file(base_sha1)) {
67e5a5ec
LT
181 add_delta_to_list(base_sha1, delta_data, delta_size);
182 return 0;
8ee378a0 183 }
c4fb06c0 184 base = read_sha1_file(base_sha1, type, &base_size);
8ee378a0 185 if (!base)
c4fb06c0 186 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
ee639140
SV
187 result = resolve_delta(type, base, base_size, delta_data, delta_size);
188 free(base);
189 return result;
8ee378a0
JH
190}
191
d36f7b80 192static void unpack_one(unsigned nr, unsigned total)
8ee378a0 193{
01247d87 194 unsigned shift;
a733cb60 195 unsigned char *pack, c;
67e5a5ec
LT
196 unsigned long size;
197 enum object_type type;
74536958 198
67e5a5ec
LT
199 pack = fill(1);
200 c = *pack;
201 use(1);
a733cb60
LT
202 type = (c >> 4) & 7;
203 size = (c & 15);
01247d87 204 shift = 4;
a733cb60 205 while (c & 0x80) {
67e5a5ec 206 pack = fill(1);
a733cb60 207 c = *pack++;
67e5a5ec 208 use(1);
01247d87
LT
209 size += (c & 0x7f) << shift;
210 shift += 7;
a733cb60 211 }
d36f7b80
LT
212 if (!quiet) {
213 static unsigned long last_sec;
214 static unsigned last_percent;
215 struct timeval now;
cf219196 216 unsigned percentage = (nr * 100) / total;
d36f7b80
LT
217
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);
223 }
224 }
a733cb60
LT
225 switch (type) {
226 case OBJ_COMMIT:
227 case OBJ_TREE:
228 case OBJ_BLOB:
229 case OBJ_TAG:
67e5a5ec 230 unpack_non_delta_entry(type, size);
a733cb60
LT
231 return;
232 case OBJ_DELTA:
67e5a5ec 233 unpack_delta_entry(size);
a733cb60 234 return;
67e5a5ec
LT
235 default:
236 die("bad object type %d", type);
74536958
LT
237 }
238}
239
240/*
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..
244 */
245static 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
bad50dc8
LT
264int main(int argc, char **argv)
265{
266 int i;
67e5a5ec 267 unsigned char sha1[20];
bad50dc8 268
53228a5f
JH
269 setup_git_directory();
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);
295 if (memcmp(fill(20), sha1, 20))
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}