git-unpack-objects: start removing debug output
[git/git.git] / unpack-objects.c
CommitLineData
bad50dc8 1#include "cache.h"
74536958 2#include "object.h"
8ee378a0 3#include "delta.h"
bad50dc8 4
74536958 5static int dry_run;
bad50dc8
LT
6static int nr_entries;
7static const char *base_name;
8static const char unpack_usage[] = "git-unpack-objects basename";
9
10struct pack_entry {
8ee378a0 11 unsigned int offset; /* network byte order */
bad50dc8
LT
12 unsigned char sha1[20];
13};
14
74536958
LT
15static void *pack_base;
16static unsigned long pack_size;
8ee378a0
JH
17static void *index_base;
18static unsigned long index_size;
74536958 19
bad50dc8
LT
20static struct pack_entry **pack_list;
21
22static void *map_file(const char *suffix, unsigned long *sizep)
23{
24 static char pathname[PATH_MAX];
25 unsigned long len;
26 int fd;
27 struct stat st;
28 void *map;
29
30 len = snprintf(pathname, PATH_MAX, "%s.%s", base_name, suffix);
31 if (len >= PATH_MAX)
32 die("bad pack base-name");
33 fd = open(pathname, O_RDONLY);
34 if (fd < 0 || fstat(fd, &st))
35 die("unable to open '%s'", pathname);
36 len = st.st_size;
37 if (!len)
38 die("bad pack file '%s'", pathname);
39 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
40 if (-1 == (int)(long)map)
41 die("unable to mmap '%s'", pathname);
42 close(fd);
43 *sizep = len;
44 return map;
45}
46
47static int sort_by_offset(const void *_a, const void *_b)
48{
49 struct pack_entry *a = *(struct pack_entry **)_a;
50 struct pack_entry *b = *(struct pack_entry **)_b;
51 unsigned int o1, o2;
52
53 o1 = ntohl(a->offset);
54 o2 = ntohl(b->offset);
55 return o1 < o2 ? -1 : 1;
56}
57
8ee378a0 58static int check_index(void)
bad50dc8 59{
8ee378a0 60 unsigned int *array = index_base;
bad50dc8
LT
61 unsigned int nr;
62 int i;
63
8ee378a0 64 if (index_size < 4*256)
bad50dc8
LT
65 return error("index file too small");
66 nr = 0;
67 for (i = 0; i < 256; i++) {
68 unsigned int n = ntohl(array[i]);
69 if (n < nr)
70 return error("non-monotonic index");
71 nr = n;
72 }
8ee378a0
JH
73 if (index_size != 4*256 + nr * 24) {
74 printf("index_size=%lu, expected %u (%u)\n",
75 index_size, 4*256 + nr * 24, nr);
bad50dc8
LT
76 return error("wrong index file size");
77 }
78
79 nr_entries = nr;
80 pack_list = xmalloc(nr * sizeof(struct pack_entry *));
81 for (i = 0; i < nr; i++)
8ee378a0 82 pack_list[i] = index_base + 4*256 + i*24;
bad50dc8
LT
83
84 qsort(pack_list, nr, sizeof(*pack_list), sort_by_offset);
85
86 printf("%d entries\n", nr);
87 return 0;
88}
89
8ee378a0 90static int unpack_non_delta_entry(struct pack_entry *entry,
c4fb06c0
LT
91 int kind,
92 unsigned char *data,
93 unsigned long size,
94 unsigned long left)
74536958 95{
c4fb06c0 96 int st;
8ee378a0
JH
97 z_stream stream;
98 char *buffer;
99 unsigned char sha1[20];
100 char *type_s;
8ee378a0 101
8ee378a0 102 printf("%s %c %lu\n", sha1_to_hex(entry->sha1), kind, size);
c4fb06c0
LT
103 if (dry_run)
104 return 0;
8ee378a0
JH
105
106 buffer = xmalloc(size + 1);
107 buffer[size] = 0;
108 memset(&stream, 0, sizeof(stream));
c4fb06c0
LT
109 stream.next_in = data;
110 stream.avail_in = left;
8ee378a0
JH
111 stream.next_out = buffer;
112 stream.avail_out = size;
113
114 inflateInit(&stream);
115 st = inflate(&stream, Z_FINISH);
116 inflateEnd(&stream);
117 if ((st != Z_STREAM_END) || stream.total_out != size)
118 goto err_finish;
119 switch (kind) {
120 case 'C': type_s = "commit"; break;
121 case 'T': type_s = "tree"; break;
122 case 'B': type_s = "blob"; break;
123 default: goto err_finish;
124 }
125 if (write_sha1_file(buffer, size, type_s, sha1) < 0)
126 die("failed to write %s (%s)",
127 sha1_to_hex(entry->sha1), type_s);
128 printf("%s %s\n", sha1_to_hex(sha1), type_s);
129 if (memcmp(sha1, entry->sha1, 20))
130 die("resulting %s have wrong SHA1", type_s);
131
132 finish:
133 st = 0;
134 free(buffer);
135 return st;
136 err_finish:
137 st = -1;
138 goto finish;
139}
140
141static int find_pack_entry(unsigned char *sha1, struct pack_entry **ent)
142{
143 int *level1_ofs = index_base;
144 int hi = ntohl(level1_ofs[*sha1]);
145 int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
146 void *index = index_base + 4*256;
147
148 do {
149 int mi = (lo + hi) / 2;
150 int cmp = memcmp(index + 24 * mi + 4, sha1, 20);
151 if (!cmp) {
152 *ent = index + 24 * mi;
153 return 1;
154 }
c4fb06c0 155 if (cmp > 0)
8ee378a0
JH
156 hi = mi;
157 else
c4fb06c0 158 lo = mi+1;
8ee378a0
JH
159 } while (lo < hi);
160 return 0;
161}
162
163/* forward declaration for a mutually recursive function */
164static void unpack_entry(struct pack_entry *);
165
c4fb06c0
LT
166static int unpack_delta_entry(struct pack_entry *entry,
167 unsigned char *base_sha1,
168 unsigned long delta_size,
169 unsigned long left)
8ee378a0 170{
c4fb06c0
LT
171 void *data, *delta_data, *result, *base;
172 unsigned long data_size, result_size, base_size;
8ee378a0
JH
173 z_stream stream;
174 int st;
175 char type[20];
176 unsigned char sha1[20];
177
c4fb06c0
LT
178 if (left < 20)
179 die("truncated pack file");
180 data = base_sha1 + 20;
181 data_size = left - 20;
182 printf("%s D %lu", sha1_to_hex(entry->sha1), delta_size);
183 printf(" %s\n", sha1_to_hex(base_sha1));
184
185 if (dry_run)
186 return 0;
8ee378a0 187
c4fb06c0 188 /* pack+5 is the base sha1, unless we have it, we need to
8ee378a0
JH
189 * unpack it first.
190 */
c4fb06c0 191 if (!has_sha1_file(base_sha1)) {
8ee378a0 192 struct pack_entry *base;
c4fb06c0 193 if (!find_pack_entry(base_sha1, &base))
8ee378a0
JH
194 die("cannot find delta-pack base object");
195 unpack_entry(base);
196 }
c4fb06c0 197 delta_data = xmalloc(delta_size);
8ee378a0
JH
198
199 memset(&stream, 0, sizeof(stream));
200
c4fb06c0
LT
201 stream.next_in = data;
202 stream.avail_in = data_size;
8ee378a0 203 stream.next_out = delta_data;
c4fb06c0 204 stream.avail_out = delta_size;
8ee378a0
JH
205
206 inflateInit(&stream);
c4fb06c0 207 st = inflate(&stream, Z_FINISH);
8ee378a0 208 inflateEnd(&stream);
c4fb06c0
LT
209 if ((st != Z_STREAM_END) || stream.total_out != delta_size)
210 die("delta data unpack failed");
8ee378a0 211
c4fb06c0 212 base = read_sha1_file(base_sha1, type, &base_size);
8ee378a0 213 if (!base)
c4fb06c0 214 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
8ee378a0
JH
215 result = patch_delta(base, base_size,
216 delta_data, delta_size,
217 &result_size);
218 if (!result)
219 die("failed to apply delta");
220 free(delta_data);
221
222 if (write_sha1_file(result, result_size, type, sha1) < 0)
223 die("failed to write %s (%s)",
224 sha1_to_hex(entry->sha1), type);
225 free(result);
226 printf("%s %s\n", sha1_to_hex(sha1), type);
227 if (memcmp(sha1, entry->sha1, 20))
228 die("resulting %s have wrong SHA1", type);
229 return 0;
230}
231
232static void unpack_entry(struct pack_entry *entry)
233{
c4fb06c0 234 unsigned long offset, size, left;
74536958
LT
235 unsigned char *pack;
236
237 /* Have we done this one already due to deltas based on it? */
238 if (lookup_object(entry->sha1))
239 return;
240
241 offset = ntohl(entry->offset);
242 if (offset > pack_size - 5)
243 die("object offset outside of pack file");
244 pack = pack_base + offset;
c4fb06c0
LT
245 size = (pack[1] << 24) + (pack[2] << 16) + (pack[3] << 8) + pack[4];
246 left = pack_size - offset - 5;
74536958
LT
247 switch (*pack) {
248 case 'C': case 'T': case 'B':
c4fb06c0 249 unpack_non_delta_entry(entry, *pack, pack+5, size, left);
74536958
LT
250 break;
251 case 'D':
c4fb06c0 252 unpack_delta_entry(entry, pack+5, size, left);
74536958
LT
253 break;
254 default:
255 die("corrupted pack file");
256 }
257}
258
259/*
260 * We unpack from the end, older files first. Now, usually
261 * there are deltas etc, so we'll not actually write the
262 * objects in that order, but we might as well try..
263 */
264static void unpack_all(void)
265{
266 int i = nr_entries;
267
268 while (--i >= 0) {
269 struct pack_entry *entry = pack_list[i];
270 unpack_entry(entry);
271 }
272}
273
bad50dc8
LT
274int main(int argc, char **argv)
275{
276 int i;
bad50dc8
LT
277
278 for (i = 1 ; i < argc; i++) {
279 const char *arg = argv[i];
280
281 if (*arg == '-') {
74536958
LT
282 if (!strcmp(arg, "-n")) {
283 dry_run = 1;
284 continue;
285 }
bad50dc8
LT
286 usage(unpack_usage);
287 }
288 if (base_name)
289 usage(unpack_usage);
290 base_name = arg;
291 }
292 if (!base_name)
293 usage(unpack_usage);
8ee378a0 294 index_base = map_file("idx", &index_size);
74536958 295 pack_base = map_file("pack", &pack_size);
8ee378a0 296 if (check_index() < 0)
bad50dc8 297 die("bad index file");
74536958 298 unpack_all();
bad50dc8
LT
299 return 0;
300}