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