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