8 static const char *base_name
;
9 static const char unpack_usage
[] = "git-unpack-objects basename";
12 unsigned int offset
; /* network byte order */
13 unsigned char sha1
[20];
16 static void *pack_base
;
17 static unsigned long pack_size
;
18 static void *index_base
;
19 static unsigned long index_size
;
21 static struct pack_entry
**pack_list
;
23 static void *map_file(const char *suffix
, unsigned long *sizep
)
25 static char pathname
[PATH_MAX
];
31 len
= snprintf(pathname
, PATH_MAX
, "%s.%s", base_name
, suffix
);
33 die("bad pack base-name");
34 fd
= open(pathname
, O_RDONLY
);
35 if (fd
< 0 || fstat(fd
, &st
))
36 die("unable to open '%s'", pathname
);
39 die("bad pack file '%s'", pathname
);
40 map
= mmap(NULL
, len
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
41 if (-1 == (int)(long)map
)
42 die("unable to mmap '%s'", pathname
);
48 static int sort_by_offset(const void *_a
, const void *_b
)
50 struct pack_entry
*a
= *(struct pack_entry
**)_a
;
51 struct pack_entry
*b
= *(struct pack_entry
**)_b
;
54 o1
= ntohl(a
->offset
);
55 o2
= ntohl(b
->offset
);
56 return o1
< o2
? -1 : 1;
59 static int check_index(void)
61 unsigned int *array
= index_base
;
65 if (index_size
< 4*256 + 20)
66 return error("index file too small");
68 for (i
= 0; i
< 256; i
++) {
69 unsigned int n
= ntohl(array
[i
]);
71 return error("non-monotonic index");
76 * - 256 index entries 4 bytes each
77 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
78 * - 20-byte SHA1 of the packfile
79 * - 20-byte SHA1 file checksum
81 if (index_size
!= 4*256 + nr
* 24 + 20 + 20)
82 return error("wrong index file size");
85 pack_list
= xmalloc(nr
* sizeof(struct pack_entry
*));
86 for (i
= 0; i
< nr
; i
++)
87 pack_list
[i
] = index_base
+ 4*256 + i
*24;
89 qsort(pack_list
, nr
, sizeof(*pack_list
), sort_by_offset
);
91 printf("%d entries\n", nr
);
95 static int unpack_non_delta_entry(struct pack_entry
*entry
,
96 enum object_type kind
,
104 unsigned char sha1
[20];
107 printf("%s %c %lu\n", sha1_to_hex(entry
->sha1
), ".CTBGD"[kind
], size
);
111 buffer
= xmalloc(size
+ 1);
113 memset(&stream
, 0, sizeof(stream
));
114 stream
.next_in
= data
;
115 stream
.avail_in
= left
;
116 stream
.next_out
= buffer
;
117 stream
.avail_out
= size
;
119 inflateInit(&stream
);
120 st
= inflate(&stream
, Z_FINISH
);
122 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
)
125 case OBJ_COMMIT
: type
= "commit"; break;
126 case OBJ_TREE
: type
= "tree"; break;
127 case OBJ_BLOB
: type
= "blob"; break;
128 case OBJ_TAG
: type
= "tag"; break;
129 default: goto err_finish
;
131 if (write_sha1_file(buffer
, size
, type
, sha1
) < 0)
132 die("failed to write %s (%s)",
133 sha1_to_hex(entry
->sha1
), type
);
134 printf("%s %s\n", sha1_to_hex(sha1
), type
);
135 if (memcmp(sha1
, entry
->sha1
, 20))
136 die("resulting %s have wrong SHA1", type
);
147 static int find_pack_entry(unsigned char *sha1
, struct pack_entry
**ent
)
149 int *level1_ofs
= index_base
;
150 int hi
= ntohl(level1_ofs
[*sha1
]);
151 int lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
152 void *index
= index_base
+ 4*256;
155 int mi
= (lo
+ hi
) / 2;
156 int cmp
= memcmp(index
+ 24 * mi
+ 4, sha1
, 20);
158 *ent
= index
+ 24 * mi
;
169 /* forward declaration for a mutually recursive function */
170 static void unpack_entry(struct pack_entry
*);
172 static int unpack_delta_entry(struct pack_entry
*entry
,
173 unsigned char *base_sha1
,
174 unsigned long delta_size
,
177 void *data
, *delta_data
, *result
, *base
;
178 unsigned long data_size
, result_size
, base_size
;
182 unsigned char sha1
[20];
185 die("truncated pack file");
186 data
= base_sha1
+ 20;
187 data_size
= left
- 20;
188 printf("%s D %lu", sha1_to_hex(entry
->sha1
), delta_size
);
189 printf(" %s\n", sha1_to_hex(base_sha1
));
194 /* pack+5 is the base sha1, unless we have it, we need to
197 if (!has_sha1_file(base_sha1
)) {
198 struct pack_entry
*base
;
199 if (!find_pack_entry(base_sha1
, &base
))
200 die("cannot find delta-pack base object");
203 delta_data
= xmalloc(delta_size
);
205 memset(&stream
, 0, sizeof(stream
));
207 stream
.next_in
= data
;
208 stream
.avail_in
= data_size
;
209 stream
.next_out
= delta_data
;
210 stream
.avail_out
= delta_size
;
212 inflateInit(&stream
);
213 st
= inflate(&stream
, Z_FINISH
);
215 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= delta_size
)
216 die("delta data unpack failed");
218 base
= read_sha1_file(base_sha1
, type
, &base_size
);
220 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1
));
221 result
= patch_delta(base
, base_size
,
222 delta_data
, delta_size
,
225 die("failed to apply delta");
228 if (write_sha1_file(result
, result_size
, type
, sha1
) < 0)
229 die("failed to write %s (%s)",
230 sha1_to_hex(entry
->sha1
), type
);
232 printf("%s %s\n", sha1_to_hex(sha1
), type
);
233 if (memcmp(sha1
, entry
->sha1
, 20))
234 die("resulting %s have wrong SHA1", type
);
238 static void unpack_entry(struct pack_entry
*entry
)
240 unsigned long offset
, size
, left
;
241 unsigned char *pack
, c
;
244 /* Have we done this one already due to deltas based on it? */
245 if (lookup_object(entry
->sha1
))
248 offset
= ntohl(entry
->offset
);
249 if (offset
>= pack_size
)
252 pack
= pack_base
+ offset
;
258 if (offset
>= pack_size
)
262 size
= (size
<< 7) + (c
& 0x7f);
265 left
= pack_size
- offset
;
271 unpack_non_delta_entry(entry
, type
, pack
, size
, left
);
274 unpack_delta_entry(entry
, pack
, size
, left
);
278 die("corrupted pack file");
282 * We unpack from the end, older files first. Now, usually
283 * there are deltas etc, so we'll not actually write the
284 * objects in that order, but we might as well try..
286 static void unpack_all(void)
291 struct pack_entry
*entry
= pack_list
[i
];
296 int main(int argc
, char **argv
)
300 for (i
= 1 ; i
< argc
; i
++) {
301 const char *arg
= argv
[i
];
304 if (!strcmp(arg
, "-n")) {
316 index_base
= map_file("idx", &index_size
);
317 pack_base
= map_file("pack", &pack_size
);
318 if (check_index() < 0)
319 die("bad index file");