pack-redundant: abstract away hash algorithm
[git/raj.git] / builtin / pack-redundant.c
blob0fe1ff3cb702cc6ab9e1b6d666c7b9d2f94d0c47
1 /*
3 * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
5 * This file is licensed under the GPL v2.
7 */
9 #include "builtin.h"
10 #include "repository.h"
11 #include "packfile.h"
12 #include "object-store.h"
14 #define BLKSIZE 512
16 static const char pack_redundant_usage[] =
17 "git pack-redundant [--verbose] [--alt-odb] (--all | <filename.pack>...)";
19 static int load_all_packs, verbose, alt_odb;
21 struct llist_item {
22 struct llist_item *next;
23 const unsigned char *sha1;
25 static struct llist {
26 struct llist_item *front;
27 struct llist_item *back;
28 size_t size;
29 } *all_objects; /* all objects which must be present in local packfiles */
31 static struct pack_list {
32 struct pack_list *next;
33 struct packed_git *pack;
34 struct llist *unique_objects;
35 struct llist *all_objects;
36 } *local_packs = NULL, *altodb_packs = NULL;
38 struct pll {
39 struct pll *next;
40 struct pack_list *pl;
43 static struct llist_item *free_nodes;
45 static inline void llist_item_put(struct llist_item *item)
47 item->next = free_nodes;
48 free_nodes = item;
51 static inline struct llist_item *llist_item_get(void)
53 struct llist_item *new_item;
54 if ( free_nodes ) {
55 new_item = free_nodes;
56 free_nodes = free_nodes->next;
57 } else {
58 int i = 1;
59 ALLOC_ARRAY(new_item, BLKSIZE);
60 for (; i < BLKSIZE; i++)
61 llist_item_put(&new_item[i]);
63 return new_item;
66 static void llist_free(struct llist *list)
68 while ((list->back = list->front)) {
69 list->front = list->front->next;
70 llist_item_put(list->back);
72 free(list);
75 static inline void llist_init(struct llist **list)
77 *list = xmalloc(sizeof(struct llist));
78 (*list)->front = (*list)->back = NULL;
79 (*list)->size = 0;
82 static struct llist * llist_copy(struct llist *list)
84 struct llist *ret;
85 struct llist_item *new_item, *old_item, *prev;
87 llist_init(&ret);
89 if ((ret->size = list->size) == 0)
90 return ret;
92 new_item = ret->front = llist_item_get();
93 new_item->sha1 = list->front->sha1;
95 old_item = list->front->next;
96 while (old_item) {
97 prev = new_item;
98 new_item = llist_item_get();
99 prev->next = new_item;
100 new_item->sha1 = old_item->sha1;
101 old_item = old_item->next;
103 new_item->next = NULL;
104 ret->back = new_item;
106 return ret;
109 static inline struct llist_item *llist_insert(struct llist *list,
110 struct llist_item *after,
111 const unsigned char *sha1)
113 struct llist_item *new_item = llist_item_get();
114 new_item->sha1 = sha1;
115 new_item->next = NULL;
117 if (after != NULL) {
118 new_item->next = after->next;
119 after->next = new_item;
120 if (after == list->back)
121 list->back = new_item;
122 } else {/* insert in front */
123 if (list->size == 0)
124 list->back = new_item;
125 else
126 new_item->next = list->front;
127 list->front = new_item;
129 list->size++;
130 return new_item;
133 static inline struct llist_item *llist_insert_back(struct llist *list,
134 const unsigned char *sha1)
136 return llist_insert(list, list->back, sha1);
139 static inline struct llist_item *llist_insert_sorted_unique(struct llist *list,
140 const unsigned char *sha1, struct llist_item *hint)
142 struct llist_item *prev = NULL, *l;
144 l = (hint == NULL) ? list->front : hint;
145 while (l) {
146 int cmp = hashcmp(l->sha1, sha1);
147 if (cmp > 0) { /* we insert before this entry */
148 return llist_insert(list, prev, sha1);
150 if (!cmp) { /* already exists */
151 return l;
153 prev = l;
154 l = l->next;
156 /* insert at the end */
157 return llist_insert_back(list, sha1);
160 /* returns a pointer to an item in front of sha1 */
161 static inline struct llist_item * llist_sorted_remove(struct llist *list, const unsigned char *sha1, struct llist_item *hint)
163 struct llist_item *prev, *l;
165 redo_from_start:
166 l = (hint == NULL) ? list->front : hint;
167 prev = NULL;
168 while (l) {
169 int cmp = hashcmp(l->sha1, sha1);
170 if (cmp > 0) /* not in list, since sorted */
171 return prev;
172 if (!cmp) { /* found */
173 if (prev == NULL) {
174 if (hint != NULL && hint != list->front) {
175 /* we don't know the previous element */
176 hint = NULL;
177 goto redo_from_start;
179 list->front = l->next;
180 } else
181 prev->next = l->next;
182 if (l == list->back)
183 list->back = prev;
184 llist_item_put(l);
185 list->size--;
186 return prev;
188 prev = l;
189 l = l->next;
191 return prev;
194 /* computes A\B */
195 static void llist_sorted_difference_inplace(struct llist *A,
196 struct llist *B)
198 struct llist_item *hint, *b;
200 hint = NULL;
201 b = B->front;
203 while (b) {
204 hint = llist_sorted_remove(A, b->sha1, hint);
205 b = b->next;
209 static inline struct pack_list * pack_list_insert(struct pack_list **pl,
210 struct pack_list *entry)
212 struct pack_list *p = xmalloc(sizeof(struct pack_list));
213 memcpy(p, entry, sizeof(struct pack_list));
214 p->next = *pl;
215 *pl = p;
216 return p;
219 static inline size_t pack_list_size(struct pack_list *pl)
221 size_t ret = 0;
222 while (pl) {
223 ret++;
224 pl = pl->next;
226 return ret;
229 static struct pack_list * pack_list_difference(const struct pack_list *A,
230 const struct pack_list *B)
232 struct pack_list *ret;
233 const struct pack_list *pl;
235 if (A == NULL)
236 return NULL;
238 pl = B;
239 while (pl != NULL) {
240 if (A->pack == pl->pack)
241 return pack_list_difference(A->next, B);
242 pl = pl->next;
244 ret = xmalloc(sizeof(struct pack_list));
245 memcpy(ret, A, sizeof(struct pack_list));
246 ret->next = pack_list_difference(A->next, B);
247 return ret;
250 static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
252 unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step;
253 const unsigned char *p1_base, *p2_base;
254 struct llist_item *p1_hint = NULL, *p2_hint = NULL;
255 const unsigned int hashsz = the_hash_algo->rawsz;
257 p1_base = p1->pack->index_data;
258 p2_base = p2->pack->index_data;
259 p1_base += 256 * 4 + ((p1->pack->index_version < 2) ? 4 : 8);
260 p2_base += 256 * 4 + ((p2->pack->index_version < 2) ? 4 : 8);
261 p1_step = hashsz + ((p1->pack->index_version < 2) ? 4 : 0);
262 p2_step = hashsz + ((p2->pack->index_version < 2) ? 4 : 0);
264 while (p1_off < p1->pack->num_objects * p1_step &&
265 p2_off < p2->pack->num_objects * p2_step)
267 int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
268 /* cmp ~ p1 - p2 */
269 if (cmp == 0) {
270 p1_hint = llist_sorted_remove(p1->unique_objects,
271 p1_base + p1_off, p1_hint);
272 p2_hint = llist_sorted_remove(p2->unique_objects,
273 p1_base + p1_off, p2_hint);
274 p1_off += p1_step;
275 p2_off += p2_step;
276 continue;
278 if (cmp < 0) { /* p1 has the object, p2 doesn't */
279 p1_off += p1_step;
280 } else { /* p2 has the object, p1 doesn't */
281 p2_off += p2_step;
286 static void pll_free(struct pll *l)
288 struct pll *old;
289 struct pack_list *opl;
291 while (l) {
292 old = l;
293 while (l->pl) {
294 opl = l->pl;
295 l->pl = opl->next;
296 free(opl);
298 l = l->next;
299 free(old);
303 /* all the permutations have to be free()d at the same time,
304 * since they refer to each other
306 static struct pll * get_permutations(struct pack_list *list, int n)
308 struct pll *subset, *ret = NULL, *new_pll = NULL;
310 if (list == NULL || pack_list_size(list) < n || n == 0)
311 return NULL;
313 if (n == 1) {
314 while (list) {
315 new_pll = xmalloc(sizeof(*new_pll));
316 new_pll->pl = NULL;
317 pack_list_insert(&new_pll->pl, list);
318 new_pll->next = ret;
319 ret = new_pll;
320 list = list->next;
322 return ret;
325 while (list->next) {
326 subset = get_permutations(list->next, n - 1);
327 while (subset) {
328 new_pll = xmalloc(sizeof(*new_pll));
329 new_pll->pl = subset->pl;
330 pack_list_insert(&new_pll->pl, list);
331 new_pll->next = ret;
332 ret = new_pll;
333 subset = subset->next;
335 list = list->next;
337 return ret;
340 static int is_superset(struct pack_list *pl, struct llist *list)
342 struct llist *diff;
344 diff = llist_copy(list);
346 while (pl) {
347 llist_sorted_difference_inplace(diff, pl->all_objects);
348 if (diff->size == 0) { /* we're done */
349 llist_free(diff);
350 return 1;
352 pl = pl->next;
354 llist_free(diff);
355 return 0;
358 static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
360 size_t ret = 0;
361 unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step;
362 const unsigned char *p1_base, *p2_base;
363 const unsigned int hashsz = the_hash_algo->rawsz;
365 p1_base = p1->index_data;
366 p2_base = p2->index_data;
367 p1_base += 256 * 4 + ((p1->index_version < 2) ? 4 : 8);
368 p2_base += 256 * 4 + ((p2->index_version < 2) ? 4 : 8);
369 p1_step = hashsz + ((p1->index_version < 2) ? 4 : 0);
370 p2_step = hashsz + ((p2->index_version < 2) ? 4 : 0);
372 while (p1_off < p1->num_objects * p1_step &&
373 p2_off < p2->num_objects * p2_step)
375 int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
376 /* cmp ~ p1 - p2 */
377 if (cmp == 0) {
378 ret++;
379 p1_off += p1_step;
380 p2_off += p2_step;
381 continue;
383 if (cmp < 0) { /* p1 has the object, p2 doesn't */
384 p1_off += p1_step;
385 } else { /* p2 has the object, p1 doesn't */
386 p2_off += p2_step;
389 return ret;
392 /* another O(n^2) function ... */
393 static size_t get_pack_redundancy(struct pack_list *pl)
395 struct pack_list *subset;
396 size_t ret = 0;
398 if (pl == NULL)
399 return 0;
401 while ((subset = pl->next)) {
402 while (subset) {
403 ret += sizeof_union(pl->pack, subset->pack);
404 subset = subset->next;
406 pl = pl->next;
408 return ret;
411 static inline off_t pack_set_bytecount(struct pack_list *pl)
413 off_t ret = 0;
414 while (pl) {
415 ret += pl->pack->pack_size;
416 ret += pl->pack->index_size;
417 pl = pl->next;
419 return ret;
422 static void minimize(struct pack_list **min)
424 struct pack_list *pl, *unique = NULL,
425 *non_unique = NULL, *min_perm = NULL;
426 struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm;
427 struct llist *missing;
428 off_t min_perm_size = 0, perm_size;
429 int n;
431 pl = local_packs;
432 while (pl) {
433 if (pl->unique_objects->size)
434 pack_list_insert(&unique, pl);
435 else
436 pack_list_insert(&non_unique, pl);
437 pl = pl->next;
439 /* find out which objects are missing from the set of unique packs */
440 missing = llist_copy(all_objects);
441 pl = unique;
442 while (pl) {
443 llist_sorted_difference_inplace(missing, pl->all_objects);
444 pl = pl->next;
447 /* return if there are no objects missing from the unique set */
448 if (missing->size == 0) {
449 *min = unique;
450 free(missing);
451 return;
454 /* find the permutations which contain all missing objects */
455 for (n = 1; n <= pack_list_size(non_unique) && !perm_ok; n++) {
456 perm_all = perm = get_permutations(non_unique, n);
457 while (perm) {
458 if (is_superset(perm->pl, missing)) {
459 new_perm = xmalloc(sizeof(struct pll));
460 memcpy(new_perm, perm, sizeof(struct pll));
461 new_perm->next = perm_ok;
462 perm_ok = new_perm;
464 perm = perm->next;
466 if (perm_ok)
467 break;
468 pll_free(perm_all);
470 if (perm_ok == NULL)
471 die("Internal error: No complete sets found!");
473 /* find the permutation with the smallest size */
474 perm = perm_ok;
475 while (perm) {
476 perm_size = pack_set_bytecount(perm->pl);
477 if (!min_perm_size || min_perm_size > perm_size) {
478 min_perm_size = perm_size;
479 min_perm = perm->pl;
481 perm = perm->next;
483 *min = min_perm;
484 /* add the unique packs to the list */
485 pl = unique;
486 while (pl) {
487 pack_list_insert(min, pl);
488 pl = pl->next;
492 static void load_all_objects(void)
494 struct pack_list *pl = local_packs;
495 struct llist_item *hint, *l;
497 llist_init(&all_objects);
499 while (pl) {
500 hint = NULL;
501 l = pl->all_objects->front;
502 while (l) {
503 hint = llist_insert_sorted_unique(all_objects,
504 l->sha1, hint);
505 l = l->next;
507 pl = pl->next;
509 /* remove objects present in remote packs */
510 pl = altodb_packs;
511 while (pl) {
512 llist_sorted_difference_inplace(all_objects, pl->all_objects);
513 pl = pl->next;
517 /* this scales like O(n^2) */
518 static void cmp_local_packs(void)
520 struct pack_list *subset, *pl = local_packs;
522 while ((subset = pl)) {
523 while ((subset = subset->next))
524 cmp_two_packs(pl, subset);
525 pl = pl->next;
529 static void scan_alt_odb_packs(void)
531 struct pack_list *local, *alt;
533 alt = altodb_packs;
534 while (alt) {
535 local = local_packs;
536 while (local) {
537 llist_sorted_difference_inplace(local->unique_objects,
538 alt->all_objects);
539 local = local->next;
541 llist_sorted_difference_inplace(all_objects, alt->all_objects);
542 alt = alt->next;
546 static struct pack_list * add_pack(struct packed_git *p)
548 struct pack_list l;
549 unsigned long off = 0, step;
550 const unsigned char *base;
552 if (!p->pack_local && !(alt_odb || verbose))
553 return NULL;
555 l.pack = p;
556 llist_init(&l.all_objects);
558 if (open_pack_index(p))
559 return NULL;
561 base = p->index_data;
562 base += 256 * 4 + ((p->index_version < 2) ? 4 : 8);
563 step = the_hash_algo->rawsz + ((p->index_version < 2) ? 4 : 0);
564 while (off < p->num_objects * step) {
565 llist_insert_back(l.all_objects, base + off);
566 off += step;
568 /* this list will be pruned in cmp_two_packs later */
569 l.unique_objects = llist_copy(l.all_objects);
570 if (p->pack_local)
571 return pack_list_insert(&local_packs, &l);
572 else
573 return pack_list_insert(&altodb_packs, &l);
576 static struct pack_list * add_pack_file(const char *filename)
578 struct packed_git *p = get_packed_git(the_repository);
580 if (strlen(filename) < 40)
581 die("Bad pack filename: %s", filename);
583 while (p) {
584 if (strstr(p->pack_name, filename))
585 return add_pack(p);
586 p = p->next;
588 die("Filename %s not found in packed_git", filename);
591 static void load_all(void)
593 struct packed_git *p = get_packed_git(the_repository);
595 while (p) {
596 add_pack(p);
597 p = p->next;
601 int cmd_pack_redundant(int argc, const char **argv, const char *prefix)
603 int i;
604 struct pack_list *min, *red, *pl;
605 struct llist *ignore;
606 unsigned char *sha1;
607 char buf[42]; /* 40 byte sha1 + \n + \0 */
609 if (argc == 2 && !strcmp(argv[1], "-h"))
610 usage(pack_redundant_usage);
612 for (i = 1; i < argc; i++) {
613 const char *arg = argv[i];
614 if (!strcmp(arg, "--")) {
615 i++;
616 break;
618 if (!strcmp(arg, "--all")) {
619 load_all_packs = 1;
620 continue;
622 if (!strcmp(arg, "--verbose")) {
623 verbose = 1;
624 continue;
626 if (!strcmp(arg, "--alt-odb")) {
627 alt_odb = 1;
628 continue;
630 if (*arg == '-')
631 usage(pack_redundant_usage);
632 else
633 break;
636 if (load_all_packs)
637 load_all();
638 else
639 while (*(argv + i) != NULL)
640 add_pack_file(*(argv + i++));
642 if (local_packs == NULL)
643 die("Zero packs found!");
645 load_all_objects();
647 cmp_local_packs();
648 if (alt_odb)
649 scan_alt_odb_packs();
651 /* ignore objects given on stdin */
652 llist_init(&ignore);
653 if (!isatty(0)) {
654 while (fgets(buf, sizeof(buf), stdin)) {
655 sha1 = xmalloc(20);
656 if (get_sha1_hex(buf, sha1))
657 die("Bad sha1 on stdin: %s", buf);
658 llist_insert_sorted_unique(ignore, sha1, NULL);
661 llist_sorted_difference_inplace(all_objects, ignore);
662 pl = local_packs;
663 while (pl) {
664 llist_sorted_difference_inplace(pl->unique_objects, ignore);
665 pl = pl->next;
668 minimize(&min);
670 if (verbose) {
671 fprintf(stderr, "There are %lu packs available in alt-odbs.\n",
672 (unsigned long)pack_list_size(altodb_packs));
673 fprintf(stderr, "The smallest (bytewise) set of packs is:\n");
674 pl = min;
675 while (pl) {
676 fprintf(stderr, "\t%s\n", pl->pack->pack_name);
677 pl = pl->next;
679 fprintf(stderr, "containing %lu duplicate objects "
680 "with a total size of %lukb.\n",
681 (unsigned long)get_pack_redundancy(min),
682 (unsigned long)pack_set_bytecount(min)/1024);
683 fprintf(stderr, "A total of %lu unique objects were considered.\n",
684 (unsigned long)all_objects->size);
685 fprintf(stderr, "Redundant packs (with indexes):\n");
687 pl = red = pack_list_difference(local_packs, min);
688 while (pl) {
689 printf("%s\n%s\n",
690 sha1_pack_index_name(pl->pack->sha1),
691 pl->pack->pack_name);
692 pl = pl->next;
694 if (verbose)
695 fprintf(stderr, "%luMB of redundant packs in total.\n",
696 (unsigned long)pack_set_bytecount(red)/(1024*1024));
698 return 0;