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