]>
Commit | Line | Data |
---|---|---|
c283ab21 LS |
1 | /* |
2 | * | |
3 | * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se> | |
4 | * | |
5 | * This file is licensed under the GPL v2. | |
6 | * | |
7 | */ | |
8 | ||
c2e86add | 9 | #include "builtin.h" |
a80d72db | 10 | #include "repository.h" |
4f39cd82 | 11 | #include "packfile.h" |
a80d72db | 12 | #include "object-store.h" |
c283ab21 | 13 | |
6d016c9c LS |
14 | #define BLKSIZE 512 |
15 | ||
9bc0f32c | 16 | static const char pack_redundant_usage[] = |
9c9b4f2f | 17 | "git pack-redundant [--verbose] [--alt-odb] (--all | <filename.pack>...)"; |
c283ab21 | 18 | |
96f1e58f | 19 | static int load_all_packs, verbose, alt_odb; |
c283ab21 LS |
20 | |
21 | struct llist_item { | |
22 | struct llist_item *next; | |
6390fe20 | 23 | const struct object_id *oid; |
c283ab21 | 24 | }; |
bd22c904 | 25 | static struct llist { |
c283ab21 LS |
26 | struct llist_item *front; |
27 | struct llist_item *back; | |
28 | size_t size; | |
1c3039e8 | 29 | } *all_objects; /* all objects which must be present in local packfiles */ |
c283ab21 | 30 | |
bd22c904 | 31 | static struct pack_list { |
c283ab21 LS |
32 | struct pack_list *next; |
33 | struct packed_git *pack; | |
34 | struct llist *unique_objects; | |
35 | struct llist *all_objects; | |
1c3039e8 | 36 | } *local_packs = NULL, *altodb_packs = NULL; |
c283ab21 LS |
37 | |
38 | struct pll { | |
39 | struct pll *next; | |
40 | struct pack_list *pl; | |
41 | }; | |
42 | ||
96f1e58f | 43 | static struct llist_item *free_nodes; |
60435f68 | 44 | |
6d016c9c LS |
45 | static inline void llist_item_put(struct llist_item *item) |
46 | { | |
47 | item->next = free_nodes; | |
48 | free_nodes = item; | |
49 | } | |
50 | ||
962554c6 | 51 | static inline struct llist_item *llist_item_get(void) |
60435f68 | 52 | { |
b2e4bafb | 53 | struct llist_item *new_item; |
60435f68 | 54 | if ( free_nodes ) { |
b2e4bafb | 55 | new_item = free_nodes; |
60435f68 | 56 | free_nodes = free_nodes->next; |
6d016c9c LS |
57 | } else { |
58 | int i = 1; | |
b2e4bafb | 59 | ALLOC_ARRAY(new_item, BLKSIZE); |
eeefa7c9 | 60 | for (; i < BLKSIZE; i++) |
b2e4bafb | 61 | llist_item_put(&new_item[i]); |
6d016c9c | 62 | } |
b2e4bafb | 63 | return new_item; |
60435f68 AR |
64 | } |
65 | ||
60435f68 | 66 | static void llist_free(struct llist *list) |
c283ab21 | 67 | { |
eeefa7c9 | 68 | while ((list->back = list->front)) { |
c283ab21 | 69 | list->front = list->front->next; |
60435f68 | 70 | llist_item_put(list->back); |
c283ab21 LS |
71 | } |
72 | free(list); | |
73 | } | |
74 | ||
bd22c904 | 75 | static inline void llist_init(struct llist **list) |
c283ab21 LS |
76 | { |
77 | *list = xmalloc(sizeof(struct llist)); | |
78 | (*list)->front = (*list)->back = NULL; | |
79 | (*list)->size = 0; | |
80 | } | |
81 | ||
bd22c904 | 82 | static struct llist * llist_copy(struct llist *list) |
c283ab21 LS |
83 | { |
84 | struct llist *ret; | |
b2e4bafb | 85 | struct llist_item *new_item, *old_item, *prev; |
a6080a0a | 86 | |
c283ab21 LS |
87 | llist_init(&ret); |
88 | ||
89 | if ((ret->size = list->size) == 0) | |
90 | return ret; | |
91 | ||
b2e4bafb | 92 | new_item = ret->front = llist_item_get(); |
6390fe20 | 93 | new_item->oid = list->front->oid; |
c283ab21 | 94 | |
b2e4bafb BW |
95 | old_item = list->front->next; |
96 | while (old_item) { | |
97 | prev = new_item; | |
98 | new_item = llist_item_get(); | |
99 | prev->next = new_item; | |
6390fe20 | 100 | new_item->oid = old_item->oid; |
b2e4bafb | 101 | old_item = old_item->next; |
c283ab21 | 102 | } |
b2e4bafb BW |
103 | new_item->next = NULL; |
104 | ret->back = new_item; | |
a6080a0a | 105 | |
c283ab21 LS |
106 | return ret; |
107 | } | |
108 | ||
42873078 NP |
109 | static inline struct llist_item *llist_insert(struct llist *list, |
110 | struct llist_item *after, | |
6390fe20 | 111 | const struct object_id *oid) |
c283ab21 | 112 | { |
b2e4bafb | 113 | struct llist_item *new_item = llist_item_get(); |
6390fe20 | 114 | new_item->oid = oid; |
b2e4bafb | 115 | new_item->next = NULL; |
c283ab21 LS |
116 | |
117 | if (after != NULL) { | |
b2e4bafb BW |
118 | new_item->next = after->next; |
119 | after->next = new_item; | |
c283ab21 | 120 | if (after == list->back) |
b2e4bafb | 121 | list->back = new_item; |
c283ab21 LS |
122 | } else {/* insert in front */ |
123 | if (list->size == 0) | |
b2e4bafb | 124 | list->back = new_item; |
c283ab21 | 125 | else |
b2e4bafb BW |
126 | new_item->next = list->front; |
127 | list->front = new_item; | |
c283ab21 LS |
128 | } |
129 | list->size++; | |
b2e4bafb | 130 | return new_item; |
c283ab21 LS |
131 | } |
132 | ||
42873078 | 133 | static inline struct llist_item *llist_insert_back(struct llist *list, |
6390fe20 | 134 | const struct object_id *oid) |
c283ab21 | 135 | { |
6390fe20 | 136 | return llist_insert(list, list->back, oid); |
c283ab21 LS |
137 | } |
138 | ||
42873078 | 139 | static inline struct llist_item *llist_insert_sorted_unique(struct llist *list, |
6390fe20 | 140 | const struct object_id *oid, struct llist_item *hint) |
c283ab21 LS |
141 | { |
142 | struct llist_item *prev = NULL, *l; | |
143 | ||
144 | l = (hint == NULL) ? list->front : hint; | |
145 | while (l) { | |
6390fe20 | 146 | int cmp = oidcmp(l->oid, oid); |
c283ab21 | 147 | if (cmp > 0) { /* we insert before this entry */ |
6390fe20 | 148 | return llist_insert(list, prev, oid); |
c283ab21 | 149 | } |
eeefa7c9 | 150 | if (!cmp) { /* already exists */ |
c283ab21 LS |
151 | return l; |
152 | } | |
153 | prev = l; | |
154 | l = l->next; | |
155 | } | |
156 | /* insert at the end */ | |
6390fe20 | 157 | return llist_insert_back(list, oid); |
c283ab21 LS |
158 | } |
159 | ||
c283ab21 | 160 | /* returns a pointer to an item in front of sha1 */ |
6390fe20 | 161 | static inline struct llist_item * llist_sorted_remove(struct llist *list, const struct object_id *oid, struct llist_item *hint) |
c283ab21 LS |
162 | { |
163 | struct llist_item *prev, *l; | |
164 | ||
165 | redo_from_start: | |
166 | l = (hint == NULL) ? list->front : hint; | |
167 | prev = NULL; | |
168 | while (l) { | |
6390fe20 | 169 | int cmp = oidcmp(l->oid, oid); |
c283ab21 LS |
170 | if (cmp > 0) /* not in list, since sorted */ |
171 | return prev; | |
eeefa7c9 | 172 | if (!cmp) { /* found */ |
c283ab21 LS |
173 | if (prev == NULL) { |
174 | if (hint != NULL && hint != list->front) { | |
175 | /* we don't know the previous element */ | |
176 | hint = NULL; | |
177 | goto redo_from_start; | |
178 | } | |
179 | list->front = l->next; | |
180 | } else | |
181 | prev->next = l->next; | |
182 | if (l == list->back) | |
183 | list->back = prev; | |
60435f68 | 184 | llist_item_put(l); |
c283ab21 LS |
185 | list->size--; |
186 | return prev; | |
187 | } | |
188 | prev = l; | |
189 | l = l->next; | |
190 | } | |
191 | return prev; | |
192 | } | |
193 | ||
1a41e743 | 194 | /* computes A\B */ |
bd22c904 | 195 | static void llist_sorted_difference_inplace(struct llist *A, |
1a41e743 LS |
196 | struct llist *B) |
197 | { | |
198 | struct llist_item *hint, *b; | |
199 | ||
200 | hint = NULL; | |
201 | b = B->front; | |
202 | ||
203 | while (b) { | |
6390fe20 | 204 | hint = llist_sorted_remove(A, b->oid, hint); |
1a41e743 LS |
205 | b = b->next; |
206 | } | |
207 | } | |
208 | ||
bd22c904 | 209 | static inline struct pack_list * pack_list_insert(struct pack_list **pl, |
c283ab21 LS |
210 | struct pack_list *entry) |
211 | { | |
212 | struct pack_list *p = xmalloc(sizeof(struct pack_list)); | |
213 | memcpy(p, entry, sizeof(struct pack_list)); | |
214 | p->next = *pl; | |
215 | *pl = p; | |
216 | return p; | |
217 | } | |
218 | ||
bd22c904 | 219 | static inline size_t pack_list_size(struct pack_list *pl) |
1c3039e8 LS |
220 | { |
221 | size_t ret = 0; | |
eeefa7c9 | 222 | while (pl) { |
1c3039e8 LS |
223 | ret++; |
224 | pl = pl->next; | |
225 | } | |
226 | return ret; | |
227 | } | |
228 | ||
d1ab1577 AR |
229 | static struct pack_list * pack_list_difference(const struct pack_list *A, |
230 | const struct pack_list *B) | |
c283ab21 | 231 | { |
d1ab1577 AR |
232 | struct pack_list *ret; |
233 | const struct pack_list *pl; | |
c283ab21 LS |
234 | |
235 | if (A == NULL) | |
236 | return NULL; | |
237 | ||
238 | pl = B; | |
239 | while (pl != NULL) { | |
240 | if (A->pack == pl->pack) | |
241 | return pack_list_difference(A->next, B); | |
242 | pl = pl->next; | |
243 | } | |
244 | ret = xmalloc(sizeof(struct pack_list)); | |
245 | memcpy(ret, A, sizeof(struct pack_list)); | |
246 | ret->next = pack_list_difference(A->next, B); | |
247 | return ret; | |
248 | } | |
249 | ||
bd22c904 | 250 | static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2) |
c283ab21 | 251 | { |
8c681e07 | 252 | unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step; |
42873078 | 253 | const unsigned char *p1_base, *p2_base; |
c283ab21 | 254 | struct llist_item *p1_hint = NULL, *p2_hint = NULL; |
00de6063 | 255 | const unsigned int hashsz = the_hash_algo->rawsz; |
1d7f171c | 256 | |
42873078 NP |
257 | p1_base = p1->pack->index_data; |
258 | p2_base = p2->pack->index_data; | |
8c681e07 NP |
259 | p1_base += 256 * 4 + ((p1->pack->index_version < 2) ? 4 : 8); |
260 | p2_base += 256 * 4 + ((p2->pack->index_version < 2) ? 4 : 8); | |
00de6063 | 261 | p1_step = hashsz + ((p1->pack->index_version < 2) ? 4 : 0); |
262 | p2_step = hashsz + ((p2->pack->index_version < 2) ? 4 : 0); | |
c283ab21 | 263 | |
8c681e07 NP |
264 | while (p1_off < p1->pack->num_objects * p1_step && |
265 | p2_off < p2->pack->num_objects * p2_step) | |
c283ab21 | 266 | { |
a89fccd2 | 267 | int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off); |
c283ab21 LS |
268 | /* cmp ~ p1 - p2 */ |
269 | if (cmp == 0) { | |
270 | p1_hint = llist_sorted_remove(p1->unique_objects, | |
6390fe20 | 271 | (const struct object_id *)(p1_base + p1_off), |
272 | p1_hint); | |
c283ab21 | 273 | p2_hint = llist_sorted_remove(p2->unique_objects, |
6390fe20 | 274 | (const struct object_id *)(p1_base + p1_off), |
275 | p2_hint); | |
8c681e07 NP |
276 | p1_off += p1_step; |
277 | p2_off += p2_step; | |
c283ab21 LS |
278 | continue; |
279 | } | |
280 | if (cmp < 0) { /* p1 has the object, p2 doesn't */ | |
8c681e07 | 281 | p1_off += p1_step; |
c283ab21 | 282 | } else { /* p2 has the object, p1 doesn't */ |
8c681e07 | 283 | p2_off += p2_step; |
c283ab21 LS |
284 | } |
285 | } | |
286 | } | |
287 | ||
962554c6 | 288 | static void pll_free(struct pll *l) |
751a71e2 | 289 | { |
6d016c9c LS |
290 | struct pll *old; |
291 | struct pack_list *opl; | |
292 | ||
293 | while (l) { | |
294 | old = l; | |
295 | while (l->pl) { | |
296 | opl = l->pl; | |
297 | l->pl = opl->next; | |
298 | free(opl); | |
751a71e2 | 299 | } |
6d016c9c LS |
300 | l = l->next; |
301 | free(old); | |
751a71e2 | 302 | } |
751a71e2 LS |
303 | } |
304 | ||
c283ab21 LS |
305 | /* all the permutations have to be free()d at the same time, |
306 | * since they refer to each other | |
307 | */ | |
6d016c9c | 308 | static struct pll * get_permutations(struct pack_list *list, int n) |
c283ab21 | 309 | { |
bdd478d6 | 310 | struct pll *subset, *ret = NULL, *new_pll = NULL; |
6d016c9c LS |
311 | |
312 | if (list == NULL || pack_list_size(list) < n || n == 0) | |
c283ab21 LS |
313 | return NULL; |
314 | ||
6d016c9c LS |
315 | if (n == 1) { |
316 | while (list) { | |
bdd478d6 | 317 | new_pll = xmalloc(sizeof(*new_pll)); |
6d016c9c LS |
318 | new_pll->pl = NULL; |
319 | pack_list_insert(&new_pll->pl, list); | |
320 | new_pll->next = ret; | |
321 | ret = new_pll; | |
322 | list = list->next; | |
323 | } | |
324 | return ret; | |
c283ab21 LS |
325 | } |
326 | ||
6d016c9c LS |
327 | while (list->next) { |
328 | subset = get_permutations(list->next, n - 1); | |
329 | while (subset) { | |
bdd478d6 | 330 | new_pll = xmalloc(sizeof(*new_pll)); |
6d016c9c LS |
331 | new_pll->pl = subset->pl; |
332 | pack_list_insert(&new_pll->pl, list); | |
333 | new_pll->next = ret; | |
334 | ret = new_pll; | |
335 | subset = subset->next; | |
751a71e2 | 336 | } |
6d016c9c | 337 | list = list->next; |
c283ab21 | 338 | } |
6d016c9c | 339 | return ret; |
c283ab21 LS |
340 | } |
341 | ||
bd22c904 | 342 | static int is_superset(struct pack_list *pl, struct llist *list) |
c283ab21 | 343 | { |
1c3039e8 | 344 | struct llist *diff; |
c283ab21 LS |
345 | |
346 | diff = llist_copy(list); | |
347 | ||
348 | while (pl) { | |
d1ab1577 | 349 | llist_sorted_difference_inplace(diff, pl->all_objects); |
c283ab21 LS |
350 | if (diff->size == 0) { /* we're done */ |
351 | llist_free(diff); | |
352 | return 1; | |
353 | } | |
354 | pl = pl->next; | |
355 | } | |
356 | llist_free(diff); | |
357 | return 0; | |
358 | } | |
359 | ||
bd22c904 | 360 | static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2) |
c283ab21 LS |
361 | { |
362 | size_t ret = 0; | |
8c681e07 | 363 | unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step; |
42873078 | 364 | const unsigned char *p1_base, *p2_base; |
00de6063 | 365 | const unsigned int hashsz = the_hash_algo->rawsz; |
c283ab21 | 366 | |
42873078 NP |
367 | p1_base = p1->index_data; |
368 | p2_base = p2->index_data; | |
8c681e07 NP |
369 | p1_base += 256 * 4 + ((p1->index_version < 2) ? 4 : 8); |
370 | p2_base += 256 * 4 + ((p2->index_version < 2) ? 4 : 8); | |
00de6063 | 371 | p1_step = hashsz + ((p1->index_version < 2) ? 4 : 0); |
372 | p2_step = hashsz + ((p2->index_version < 2) ? 4 : 0); | |
c283ab21 | 373 | |
8c681e07 NP |
374 | while (p1_off < p1->num_objects * p1_step && |
375 | p2_off < p2->num_objects * p2_step) | |
c283ab21 | 376 | { |
a89fccd2 | 377 | int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off); |
c283ab21 LS |
378 | /* cmp ~ p1 - p2 */ |
379 | if (cmp == 0) { | |
380 | ret++; | |
8c681e07 NP |
381 | p1_off += p1_step; |
382 | p2_off += p2_step; | |
c283ab21 LS |
383 | continue; |
384 | } | |
385 | if (cmp < 0) { /* p1 has the object, p2 doesn't */ | |
8c681e07 | 386 | p1_off += p1_step; |
c283ab21 | 387 | } else { /* p2 has the object, p1 doesn't */ |
8c681e07 | 388 | p2_off += p2_step; |
c283ab21 LS |
389 | } |
390 | } | |
391 | return ret; | |
392 | } | |
393 | ||
394 | /* another O(n^2) function ... */ | |
bd22c904 | 395 | static size_t get_pack_redundancy(struct pack_list *pl) |
c283ab21 LS |
396 | { |
397 | struct pack_list *subset; | |
bd22c904 | 398 | size_t ret = 0; |
1c3039e8 LS |
399 | |
400 | if (pl == NULL) | |
401 | return 0; | |
402 | ||
c283ab21 | 403 | while ((subset = pl->next)) { |
eeefa7c9 | 404 | while (subset) { |
c283ab21 LS |
405 | ret += sizeof_union(pl->pack, subset->pack); |
406 | subset = subset->next; | |
407 | } | |
408 | pl = pl->next; | |
409 | } | |
410 | return ret; | |
411 | } | |
412 | ||
c4001d92 | 413 | static inline off_t pack_set_bytecount(struct pack_list *pl) |
c283ab21 | 414 | { |
c4001d92 | 415 | off_t ret = 0; |
c283ab21 LS |
416 | while (pl) { |
417 | ret += pl->pack->pack_size; | |
418 | ret += pl->pack->index_size; | |
419 | pl = pl->next; | |
420 | } | |
421 | return ret; | |
422 | } | |
423 | ||
bd22c904 | 424 | static void minimize(struct pack_list **min) |
c283ab21 LS |
425 | { |
426 | struct pack_list *pl, *unique = NULL, | |
427 | *non_unique = NULL, *min_perm = NULL; | |
428 | struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm; | |
1c3039e8 | 429 | struct llist *missing; |
c4001d92 | 430 | off_t min_perm_size = 0, perm_size; |
6d016c9c | 431 | int n; |
c283ab21 | 432 | |
1c3039e8 | 433 | pl = local_packs; |
c283ab21 | 434 | while (pl) { |
eeefa7c9 | 435 | if (pl->unique_objects->size) |
c283ab21 LS |
436 | pack_list_insert(&unique, pl); |
437 | else | |
438 | pack_list_insert(&non_unique, pl); | |
439 | pl = pl->next; | |
440 | } | |
441 | /* find out which objects are missing from the set of unique packs */ | |
442 | missing = llist_copy(all_objects); | |
443 | pl = unique; | |
444 | while (pl) { | |
6d016c9c | 445 | llist_sorted_difference_inplace(missing, pl->all_objects); |
c283ab21 LS |
446 | pl = pl->next; |
447 | } | |
448 | ||
1c3039e8 | 449 | /* return if there are no objects missing from the unique set */ |
c283ab21 LS |
450 | if (missing->size == 0) { |
451 | *min = unique; | |
077a34e0 | 452 | free(missing); |
c283ab21 LS |
453 | return; |
454 | } | |
455 | ||
456 | /* find the permutations which contain all missing objects */ | |
6d016c9c LS |
457 | for (n = 1; n <= pack_list_size(non_unique) && !perm_ok; n++) { |
458 | perm_all = perm = get_permutations(non_unique, n); | |
459 | while (perm) { | |
460 | if (is_superset(perm->pl, missing)) { | |
461 | new_perm = xmalloc(sizeof(struct pll)); | |
462 | memcpy(new_perm, perm, sizeof(struct pll)); | |
463 | new_perm->next = perm_ok; | |
464 | perm_ok = new_perm; | |
465 | } | |
466 | perm = perm->next; | |
c283ab21 | 467 | } |
6d016c9c LS |
468 | if (perm_ok) |
469 | break; | |
470 | pll_free(perm_all); | |
c283ab21 | 471 | } |
c283ab21 | 472 | if (perm_ok == NULL) |
d7530708 | 473 | die("Internal error: No complete sets found!"); |
c283ab21 LS |
474 | |
475 | /* find the permutation with the smallest size */ | |
476 | perm = perm_ok; | |
477 | while (perm) { | |
478 | perm_size = pack_set_bytecount(perm->pl); | |
c4001d92 | 479 | if (!min_perm_size || min_perm_size > perm_size) { |
c283ab21 LS |
480 | min_perm_size = perm_size; |
481 | min_perm = perm->pl; | |
482 | } | |
483 | perm = perm->next; | |
484 | } | |
485 | *min = min_perm; | |
486 | /* add the unique packs to the list */ | |
487 | pl = unique; | |
eeefa7c9 | 488 | while (pl) { |
c283ab21 LS |
489 | pack_list_insert(min, pl); |
490 | pl = pl->next; | |
491 | } | |
492 | } | |
493 | ||
bd22c904 | 494 | static void load_all_objects(void) |
c283ab21 | 495 | { |
1c3039e8 | 496 | struct pack_list *pl = local_packs; |
c283ab21 | 497 | struct llist_item *hint, *l; |
c283ab21 LS |
498 | |
499 | llist_init(&all_objects); | |
500 | ||
501 | while (pl) { | |
c283ab21 LS |
502 | hint = NULL; |
503 | l = pl->all_objects->front; | |
504 | while (l) { | |
505 | hint = llist_insert_sorted_unique(all_objects, | |
6390fe20 | 506 | l->oid, hint); |
c283ab21 LS |
507 | l = l->next; |
508 | } | |
509 | pl = pl->next; | |
510 | } | |
1c3039e8 LS |
511 | /* remove objects present in remote packs */ |
512 | pl = altodb_packs; | |
513 | while (pl) { | |
514 | llist_sorted_difference_inplace(all_objects, pl->all_objects); | |
515 | pl = pl->next; | |
516 | } | |
c283ab21 LS |
517 | } |
518 | ||
519 | /* this scales like O(n^2) */ | |
bd22c904 | 520 | static void cmp_local_packs(void) |
c283ab21 | 521 | { |
1c3039e8 | 522 | struct pack_list *subset, *pl = local_packs; |
c283ab21 | 523 | |
1c3039e8 | 524 | while ((subset = pl)) { |
eeefa7c9 | 525 | while ((subset = subset->next)) |
1c3039e8 LS |
526 | cmp_two_packs(pl, subset); |
527 | pl = pl->next; | |
528 | } | |
06a45c8c | 529 | } |
1c3039e8 | 530 | |
bd22c904 | 531 | static void scan_alt_odb_packs(void) |
06a45c8c LS |
532 | { |
533 | struct pack_list *local, *alt; | |
534 | ||
535 | alt = altodb_packs; | |
536 | while (alt) { | |
537 | local = local_packs; | |
538 | while (local) { | |
539 | llist_sorted_difference_inplace(local->unique_objects, | |
540 | alt->all_objects); | |
541 | local = local->next; | |
1c3039e8 | 542 | } |
6d016c9c | 543 | llist_sorted_difference_inplace(all_objects, alt->all_objects); |
06a45c8c | 544 | alt = alt->next; |
c283ab21 LS |
545 | } |
546 | } | |
547 | ||
bd22c904 | 548 | static struct pack_list * add_pack(struct packed_git *p) |
c283ab21 LS |
549 | { |
550 | struct pack_list l; | |
8c681e07 | 551 | unsigned long off = 0, step; |
42873078 | 552 | const unsigned char *base; |
c283ab21 | 553 | |
06a45c8c LS |
554 | if (!p->pack_local && !(alt_odb || verbose)) |
555 | return NULL; | |
556 | ||
c283ab21 LS |
557 | l.pack = p; |
558 | llist_init(&l.all_objects); | |
559 | ||
eaa86770 | 560 | if (open_pack_index(p)) |
d079837e SP |
561 | return NULL; |
562 | ||
42873078 | 563 | base = p->index_data; |
8c681e07 | 564 | base += 256 * 4 + ((p->index_version < 2) ? 4 : 8); |
00de6063 | 565 | step = the_hash_algo->rawsz + ((p->index_version < 2) ? 4 : 0); |
8c681e07 | 566 | while (off < p->num_objects * step) { |
6390fe20 | 567 | llist_insert_back(l.all_objects, (const struct object_id *)(base + off)); |
8c681e07 | 568 | off += step; |
c283ab21 LS |
569 | } |
570 | /* this list will be pruned in cmp_two_packs later */ | |
571 | l.unique_objects = llist_copy(l.all_objects); | |
1c3039e8 LS |
572 | if (p->pack_local) |
573 | return pack_list_insert(&local_packs, &l); | |
574 | else | |
06a45c8c | 575 | return pack_list_insert(&altodb_packs, &l); |
c283ab21 LS |
576 | } |
577 | ||
377d0276 | 578 | static struct pack_list * add_pack_file(const char *filename) |
c283ab21 | 579 | { |
a80d72db | 580 | struct packed_git *p = get_packed_git(the_repository); |
c283ab21 LS |
581 | |
582 | if (strlen(filename) < 40) | |
d7530708 | 583 | die("Bad pack filename: %s", filename); |
c283ab21 LS |
584 | |
585 | while (p) { | |
586 | if (strstr(p->pack_name, filename)) | |
c283ab21 LS |
587 | return add_pack(p); |
588 | p = p->next; | |
589 | } | |
d7530708 | 590 | die("Filename %s not found in packed_git", filename); |
c283ab21 LS |
591 | } |
592 | ||
bd22c904 | 593 | static void load_all(void) |
c283ab21 | 594 | { |
a80d72db | 595 | struct packed_git *p = get_packed_git(the_repository); |
c283ab21 LS |
596 | |
597 | while (p) { | |
1c3039e8 | 598 | add_pack(p); |
c283ab21 LS |
599 | p = p->next; |
600 | } | |
601 | } | |
602 | ||
377d0276 | 603 | int cmd_pack_redundant(int argc, const char **argv, const char *prefix) |
c283ab21 LS |
604 | { |
605 | int i; | |
606 | struct pack_list *min, *red, *pl; | |
bb931cf9 | 607 | struct llist *ignore; |
6390fe20 | 608 | struct object_id *oid; |
609 | char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */ | |
c283ab21 | 610 | |
99caeed0 JN |
611 | if (argc == 2 && !strcmp(argv[1], "-h")) |
612 | usage(pack_redundant_usage); | |
613 | ||
c283ab21 LS |
614 | for (i = 1; i < argc; i++) { |
615 | const char *arg = argv[i]; | |
eeefa7c9 | 616 | if (!strcmp(arg, "--")) { |
9bc0f32c | 617 | i++; |
c283ab21 | 618 | break; |
9bc0f32c | 619 | } |
eeefa7c9 | 620 | if (!strcmp(arg, "--all")) { |
1c3039e8 | 621 | load_all_packs = 1; |
c283ab21 LS |
622 | continue; |
623 | } | |
eeefa7c9 | 624 | if (!strcmp(arg, "--verbose")) { |
c283ab21 LS |
625 | verbose = 1; |
626 | continue; | |
627 | } | |
eeefa7c9 | 628 | if (!strcmp(arg, "--alt-odb")) { |
1c3039e8 LS |
629 | alt_odb = 1; |
630 | continue; | |
631 | } | |
eeefa7c9 | 632 | if (*arg == '-') |
9bc0f32c | 633 | usage(pack_redundant_usage); |
c283ab21 LS |
634 | else |
635 | break; | |
636 | } | |
637 | ||
1c3039e8 | 638 | if (load_all_packs) |
c283ab21 LS |
639 | load_all(); |
640 | else | |
641 | while (*(argv + i) != NULL) | |
642 | add_pack_file(*(argv + i++)); | |
643 | ||
1c3039e8 | 644 | if (local_packs == NULL) |
d7530708 | 645 | die("Zero packs found!"); |
c283ab21 | 646 | |
c283ab21 LS |
647 | load_all_objects(); |
648 | ||
06a45c8c LS |
649 | cmp_local_packs(); |
650 | if (alt_odb) | |
651 | scan_alt_odb_packs(); | |
652 | ||
bb931cf9 LS |
653 | /* ignore objects given on stdin */ |
654 | llist_init(&ignore); | |
655 | if (!isatty(0)) { | |
656 | while (fgets(buf, sizeof(buf), stdin)) { | |
6390fe20 | 657 | oid = xmalloc(sizeof(*oid)); |
658 | if (get_oid_hex(buf, oid)) | |
659 | die("Bad object ID on stdin: %s", buf); | |
660 | llist_insert_sorted_unique(ignore, oid, NULL); | |
bb931cf9 LS |
661 | } |
662 | } | |
663 | llist_sorted_difference_inplace(all_objects, ignore); | |
664 | pl = local_packs; | |
665 | while (pl) { | |
666 | llist_sorted_difference_inplace(pl->unique_objects, ignore); | |
667 | pl = pl->next; | |
668 | } | |
669 | ||
c283ab21 | 670 | minimize(&min); |
06a45c8c | 671 | |
c283ab21 | 672 | if (verbose) { |
abacbe41 KR |
673 | fprintf(stderr, "There are %lu packs available in alt-odbs.\n", |
674 | (unsigned long)pack_list_size(altodb_packs)); | |
c283ab21 LS |
675 | fprintf(stderr, "The smallest (bytewise) set of packs is:\n"); |
676 | pl = min; | |
677 | while (pl) { | |
678 | fprintf(stderr, "\t%s\n", pl->pack->pack_name); | |
679 | pl = pl->next; | |
680 | } | |
abacbe41 KR |
681 | fprintf(stderr, "containing %lu duplicate objects " |
682 | "with a total size of %lukb.\n", | |
683 | (unsigned long)get_pack_redundancy(min), | |
684 | (unsigned long)pack_set_bytecount(min)/1024); | |
685 | fprintf(stderr, "A total of %lu unique objects were considered.\n", | |
686 | (unsigned long)all_objects->size); | |
c283ab21 LS |
687 | fprintf(stderr, "Redundant packs (with indexes):\n"); |
688 | } | |
1c3039e8 | 689 | pl = red = pack_list_difference(local_packs, min); |
c283ab21 LS |
690 | while (pl) { |
691 | printf("%s\n%s\n", | |
1c3039e8 LS |
692 | sha1_pack_index_name(pl->pack->sha1), |
693 | pl->pack->pack_name); | |
c283ab21 LS |
694 | pl = pl->next; |
695 | } | |
bb931cf9 | 696 | if (verbose) |
b99a394c JH |
697 | fprintf(stderr, "%luMB of redundant packs in total.\n", |
698 | (unsigned long)pack_set_bytecount(red)/(1024*1024)); | |
c283ab21 LS |
699 | |
700 | return 0; | |
701 | } |