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