Add "--depth=N" parameter to git-pack-objects to limit maximum delta depth
[git/git.git] / unpack-objects.c
CommitLineData
bad50dc8 1#include "cache.h"
74536958 2#include "object.h"
bad50dc8 3
74536958 4static int dry_run;
bad50dc8
LT
5static int nr_entries;
6static const char *base_name;
7static const char unpack_usage[] = "git-unpack-objects basename";
8
9struct pack_entry {
10 unsigned int offset;
11 unsigned char sha1[20];
12};
13
74536958
LT
14static void *pack_base;
15static unsigned long pack_size;
16
bad50dc8
LT
17static struct pack_entry **pack_list;
18
19static void *map_file(const char *suffix, unsigned long *sizep)
20{
21 static char pathname[PATH_MAX];
22 unsigned long len;
23 int fd;
24 struct stat st;
25 void *map;
26
27 len = snprintf(pathname, PATH_MAX, "%s.%s", base_name, suffix);
28 if (len >= PATH_MAX)
29 die("bad pack base-name");
30 fd = open(pathname, O_RDONLY);
31 if (fd < 0 || fstat(fd, &st))
32 die("unable to open '%s'", pathname);
33 len = st.st_size;
34 if (!len)
35 die("bad pack file '%s'", pathname);
36 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
37 if (-1 == (int)(long)map)
38 die("unable to mmap '%s'", pathname);
39 close(fd);
40 *sizep = len;
41 return map;
42}
43
44static int sort_by_offset(const void *_a, const void *_b)
45{
46 struct pack_entry *a = *(struct pack_entry **)_a;
47 struct pack_entry *b = *(struct pack_entry **)_b;
48 unsigned int o1, o2;
49
50 o1 = ntohl(a->offset);
51 o2 = ntohl(b->offset);
52 return o1 < o2 ? -1 : 1;
53}
54
55static int check_index(void *index, unsigned long idx_size)
56{
57 unsigned int *array = index;
58 unsigned int nr;
59 int i;
60
61 if (idx_size < 4*256)
62 return error("index file too small");
63 nr = 0;
64 for (i = 0; i < 256; i++) {
65 unsigned int n = ntohl(array[i]);
66 if (n < nr)
67 return error("non-monotonic index");
68 nr = n;
69 }
70 if (idx_size != 4*256 + nr * 24) {
74536958 71 printf("idx_size=%lu, expected %u (%u)\n", idx_size, 4*256 + nr * 24, nr);
bad50dc8
LT
72 return error("wrong index file size");
73 }
74
75 nr_entries = nr;
76 pack_list = xmalloc(nr * sizeof(struct pack_entry *));
77 for (i = 0; i < nr; i++)
78 pack_list[i] = index + 4*256 + i*24;
79
80 qsort(pack_list, nr, sizeof(*pack_list), sort_by_offset);
81
82 printf("%d entries\n", nr);
83 return 0;
84}
85
74536958
LT
86static void unpack_entry(struct pack_entry *entry)
87{
88 unsigned long size;
89 unsigned long offset;
90 unsigned char *pack;
91
92 /* Have we done this one already due to deltas based on it? */
93 if (lookup_object(entry->sha1))
94 return;
95
96 offset = ntohl(entry->offset);
97 if (offset > pack_size - 5)
98 die("object offset outside of pack file");
99 pack = pack_base + offset;
100 offset = pack_size - offset;
101 switch (*pack) {
102 case 'C': case 'T': case 'B':
103 size = (pack[1] << 24) + (pack[2] << 16) + (pack[3] << 8) + pack[4];
104 printf("%s %c %lu\n", sha1_to_hex(entry->sha1), *pack, size);
105 break;
106 case 'D':
107 printf("%s D", sha1_to_hex(entry->sha1));
108 printf(" %s\n", sha1_to_hex(pack+1));
109 break;
110 default:
111 die("corrupted pack file");
112 }
113}
114
115/*
116 * We unpack from the end, older files first. Now, usually
117 * there are deltas etc, so we'll not actually write the
118 * objects in that order, but we might as well try..
119 */
120static void unpack_all(void)
121{
122 int i = nr_entries;
123
124 while (--i >= 0) {
125 struct pack_entry *entry = pack_list[i];
126 unpack_entry(entry);
127 }
128}
129
bad50dc8
LT
130int main(int argc, char **argv)
131{
132 int i;
74536958
LT
133 unsigned long idx_size;
134 void *index;
bad50dc8
LT
135
136 for (i = 1 ; i < argc; i++) {
137 const char *arg = argv[i];
138
139 if (*arg == '-') {
74536958
LT
140 if (!strcmp(arg, "-n")) {
141 dry_run = 1;
142 continue;
143 }
bad50dc8
LT
144 usage(unpack_usage);
145 }
146 if (base_name)
147 usage(unpack_usage);
148 base_name = arg;
149 }
150 if (!base_name)
151 usage(unpack_usage);
152 index = map_file("idx", &idx_size);
74536958 153 pack_base = map_file("pack", &pack_size);
bad50dc8
LT
154 if (check_index(index, idx_size) < 0)
155 die("bad index file");
74536958 156 unpack_all();
bad50dc8
LT
157 return 0;
158}