]> git.ipfire.org Git - thirdparty/git.git/blame - unpack-objects.c
[PATCH] Updates for cvs-migration.txt
[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;
67e5a5ec 9static const char unpack_usage[] = "git-unpack-objects < 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 {
34 int ret = read(0, buffer + len, sizeof(buffer) - len);
35 if (ret <= 0) {
36 if (!ret)
37 die("early EOF");
38 if (errno == EAGAIN || errno == EINTR)
39 continue;
40 die("read error on input: %s", strerror(errno));
41 }
42 len += ret;
43 } while (len < min);
44 return buffer;
45}
74536958 46
67e5a5ec
LT
47static void use(int bytes)
48{
49 if (bytes > len)
50 die("used more bytes than were available");
51 len -= bytes;
52 offset += bytes;
53}
bad50dc8 54
67e5a5ec 55static void *get_data(unsigned long size)
bad50dc8 56{
67e5a5ec
LT
57 z_stream stream;
58 void *buf = xmalloc(size);
59
67e5a5ec
LT
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;
bad50dc8
LT
79}
80
67e5a5ec
LT
81struct delta_info {
82 unsigned char base_sha1[20];
83 unsigned long size;
84 void *delta;
85 struct delta_info *next;
86};
87
88static struct delta_info *delta_list;
89
90static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
bad50dc8 91{
67e5a5ec 92 struct delta_info *info = xmalloc(sizeof(*info));
bad50dc8 93
67e5a5ec
LT
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;
bad50dc8
LT
99}
100
cca7081a 101static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
67e5a5ec 102
cca7081a
LT
103static 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
111static int resolve_delta(const char *type,
67e5a5ec
LT
112 void *base, unsigned long base_size,
113 void *delta, unsigned long delta_size)
bad50dc8 114{
67e5a5ec
LT
115 void *result;
116 unsigned long result_size;
bad50dc8 117
67e5a5ec
LT
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);
cca7081a 124 write_object(result, result_size, type);
67e5a5ec 125 free(result);
bad50dc8
LT
126 return 0;
127}
128
cca7081a 129static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
74536958 130{
67e5a5ec
LT
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
146static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
147{
148 void *buf = get_data(size);
cca7081a 149 const char *type;
8ee378a0 150
8ee378a0 151 switch (kind) {
a733cb60
LT
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;
67e5a5ec 156 default: die("bad type %d", kind);
8ee378a0 157 }
dddafffe
LT
158 if (!dry_run)
159 write_object(buf, size, type);
67e5a5ec 160 free(buf);
8ee378a0
JH
161 return 0;
162}
163
67e5a5ec 164static int unpack_delta_entry(unsigned long delta_size)
8ee378a0 165{
67e5a5ec
LT
166 void *delta_data, *base;
167 unsigned long base_size;
8ee378a0 168 char type[20];
67e5a5ec 169 unsigned char base_sha1[20];
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));
67e5a5ec 187 return resolve_delta(type, base, base_size, delta_data, delta_size);
8ee378a0
JH
188}
189
d36f7b80 190static void unpack_one(unsigned nr, unsigned total)
8ee378a0 191{
01247d87 192 unsigned shift;
a733cb60 193 unsigned char *pack, c;
67e5a5ec
LT
194 unsigned long size;
195 enum object_type type;
74536958 196
67e5a5ec
LT
197 pack = fill(1);
198 c = *pack;
199 use(1);
a733cb60
LT
200 type = (c >> 4) & 7;
201 size = (c & 15);
01247d87 202 shift = 4;
a733cb60 203 while (c & 0x80) {
67e5a5ec 204 pack = fill(1);
a733cb60 205 c = *pack++;
67e5a5ec 206 use(1);
01247d87
LT
207 size += (c & 0x7f) << shift;
208 shift += 7;
a733cb60 209 }
d36f7b80
LT
210 if (!quiet) {
211 static unsigned long last_sec;
212 static unsigned last_percent;
213 struct timeval now;
cf219196 214 unsigned percentage = (nr * 100) / total;
d36f7b80
LT
215
216 gettimeofday(&now, NULL);
217 if (percentage != last_percent || now.tv_sec != last_sec) {
218 last_sec = now.tv_sec;
219 last_percent = percentage;
220 fprintf(stderr, "%4u%% (%u/%u) done\r", percentage, nr, total);
221 }
222 }
a733cb60
LT
223 switch (type) {
224 case OBJ_COMMIT:
225 case OBJ_TREE:
226 case OBJ_BLOB:
227 case OBJ_TAG:
67e5a5ec 228 unpack_non_delta_entry(type, size);
a733cb60
LT
229 return;
230 case OBJ_DELTA:
67e5a5ec 231 unpack_delta_entry(size);
a733cb60 232 return;
67e5a5ec
LT
233 default:
234 die("bad object type %d", type);
74536958
LT
235 }
236}
237
238/*
239 * We unpack from the end, older files first. Now, usually
240 * there are deltas etc, so we'll not actually write the
241 * objects in that order, but we might as well try..
242 */
243static void unpack_all(void)
244{
67e5a5ec
LT
245 int i;
246 struct pack_header *hdr = fill(sizeof(struct pack_header));
247 unsigned version = ntohl(hdr->hdr_version);
248 unsigned nr_objects = ntohl(hdr->hdr_entries);
249
250 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
251 die("bad pack file");
01247d87 252 if (version != PACK_VERSION)
67e5a5ec
LT
253 die("unable to handle pack file version %d", version);
254 fprintf(stderr, "Unpacking %d objects\n", nr_objects);
255
256 use(sizeof(struct pack_header));
257 for (i = 0; i < nr_objects; i++)
cf219196 258 unpack_one(i+1, nr_objects);
67e5a5ec
LT
259 if (delta_list)
260 die("unresolved deltas left after unpacking");
74536958
LT
261}
262
bad50dc8
LT
263int main(int argc, char **argv)
264{
265 int i;
67e5a5ec 266 unsigned char sha1[20];
bad50dc8
LT
267
268 for (i = 1 ; i < argc; i++) {
269 const char *arg = argv[i];
270
271 if (*arg == '-') {
74536958
LT
272 if (!strcmp(arg, "-n")) {
273 dry_run = 1;
274 continue;
275 }
d36f7b80
LT
276 if (!strcmp(arg, "-q")) {
277 quiet = 1;
278 continue;
279 }
bad50dc8
LT
280 usage(unpack_usage);
281 }
67e5a5ec
LT
282
283 /* We don't take any non-flag arguments now.. Maybe some day */
bad50dc8 284 usage(unpack_usage);
67e5a5ec
LT
285 }
286 SHA1_Init(&ctx);
74536958 287 unpack_all();
67e5a5ec
LT
288 SHA1_Update(&ctx, buffer, offset);
289 SHA1_Final(sha1, &ctx);
290 if (memcmp(fill(20), sha1, 20))
291 die("final sha1 did not match");
292 use(20);
293
294 /* Write the last part of the buffer to stdout */
295 while (len) {
296 int ret = write(1, buffer + offset, len);
297 if (!ret)
298 break;
299 if (ret < 0) {
300 if (errno == EAGAIN || errno == EINTR)
301 continue;
302 break;
303 }
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}