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