3 * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
5 * This file is licensed under the GPL v2.
13 static const char pack_redundant_usage[] =
14 "git pack-redundant [--verbose] [--alt-odb] (--all | <filename.pack>...)";
16 static int load_all_packs, verbose, alt_odb;
19 struct llist_item *next;
20 const unsigned char *sha1;
23 struct llist_item *front;
24 struct llist_item *back;
26 } *all_objects; /* all objects which must be present in local packfiles */
28 static struct pack_list {
29 struct pack_list *next;
30 struct packed_git *pack;
31 struct llist *unique_objects;
32 struct llist *all_objects;
33 } *local_packs = NULL, *altodb_packs = NULL;
40 static struct llist_item *free_nodes;
42 static inline void llist_item_put(struct llist_item *item)
44 item->next = free_nodes;
48 static inline struct llist_item *llist_item_get(void)
50 struct llist_item *new;
53 free_nodes = free_nodes->next;
56 ALLOC_ARRAY(new, BLKSIZE);
57 for (; i < BLKSIZE; i++)
58 llist_item_put(&new[i]);
63 static void llist_free(struct llist *list)
65 while ((list->back = list->front)) {
66 list->front = list->front->next;
67 llist_item_put(list->back);
72 static inline void llist_init(struct llist **list)
74 *list = xmalloc(sizeof(struct llist));
75 (*list)->front = (*list)->back = NULL;
79 static struct llist * llist_copy(struct llist *list)
82 struct llist_item *new, *old, *prev;
86 if ((ret->size = list->size) == 0)
89 new = ret->front = llist_item_get();
90 new->sha1 = list->front->sha1;
92 old = list->front->next;
95 new = llist_item_get();
97 new->sha1 = old->sha1;
106 static inline struct llist_item *llist_insert(struct llist *list,
107 struct llist_item *after,
108 const unsigned char *sha1)
110 struct llist_item *new = llist_item_get();
115 new->next = after->next;
117 if (after == list->back)
119 } else {/* insert in front */
123 new->next = list->front;
130 static inline struct llist_item *llist_insert_back(struct llist *list,
131 const unsigned char *sha1)
133 return llist_insert(list, list->back, sha1);
136 static inline struct llist_item *llist_insert_sorted_unique(struct llist *list,
137 const unsigned char *sha1, struct llist_item *hint)
139 struct llist_item *prev = NULL, *l;
141 l = (hint == NULL) ? list->front : hint;
143 int cmp = hashcmp(l->sha1, sha1);
144 if (cmp > 0) { /* we insert before this entry */
145 return llist_insert(list, prev, sha1);
147 if (!cmp) { /* already exists */
153 /* insert at the end */
154 return llist_insert_back(list, sha1);
157 /* returns a pointer to an item in front of sha1 */
158 static inline struct llist_item * llist_sorted_remove(struct llist *list, const unsigned char *sha1, struct llist_item *hint)
160 struct llist_item *prev, *l;
163 l = (hint == NULL) ? list->front : hint;
166 int cmp = hashcmp(l->sha1, sha1);
167 if (cmp > 0) /* not in list, since sorted */
169 if (!cmp) { /* found */
171 if (hint != NULL && hint != list->front) {
172 /* we don't know the previous element */
174 goto redo_from_start;
176 list->front = l->next;
178 prev->next = l->next;
192 static void llist_sorted_difference_inplace(struct llist *A,
195 struct llist_item *hint, *b;
201 hint = llist_sorted_remove(A, b->sha1, hint);
206 static inline struct pack_list * pack_list_insert(struct pack_list **pl,
207 struct pack_list *entry)
209 struct pack_list *p = xmalloc(sizeof(struct pack_list));
210 memcpy(p, entry, sizeof(struct pack_list));
216 static inline size_t pack_list_size(struct pack_list *pl)
226 static struct pack_list * pack_list_difference(const struct pack_list *A,
227 const struct pack_list *B)
229 struct pack_list *ret;
230 const struct pack_list *pl;
237 if (A->pack == pl->pack)
238 return pack_list_difference(A->next, B);
241 ret = xmalloc(sizeof(struct pack_list));
242 memcpy(ret, A, sizeof(struct pack_list));
243 ret->next = pack_list_difference(A->next, B);
247 static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
249 unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step;
250 const unsigned char *p1_base, *p2_base;
251 struct llist_item *p1_hint = NULL, *p2_hint = NULL;
253 p1_base = p1->pack->index_data;
254 p2_base = p2->pack->index_data;
255 p1_base += 256 * 4 + ((p1->pack->index_version < 2) ? 4 : 8);
256 p2_base += 256 * 4 + ((p2->pack->index_version < 2) ? 4 : 8);
257 p1_step = (p1->pack->index_version < 2) ? 24 : 20;
258 p2_step = (p2->pack->index_version < 2) ? 24 : 20;
260 while (p1_off < p1->pack->num_objects * p1_step &&
261 p2_off < p2->pack->num_objects * p2_step)
263 int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
266 p1_hint = llist_sorted_remove(p1->unique_objects,
267 p1_base + p1_off, p1_hint);
268 p2_hint = llist_sorted_remove(p2->unique_objects,
269 p1_base + p1_off, p2_hint);
274 if (cmp < 0) { /* p1 has the object, p2 doesn't */
276 } else { /* p2 has the object, p1 doesn't */
282 static void pll_free(struct pll *l)
285 struct pack_list *opl;
299 /* all the permutations have to be free()d at the same time,
300 * since they refer to each other
302 static struct pll * get_permutations(struct pack_list *list, int n)
304 struct pll *subset, *ret = NULL, *new_pll = NULL;
306 if (list == NULL || pack_list_size(list) < n || n == 0)
311 new_pll = xmalloc(sizeof(*new_pll));
313 pack_list_insert(&new_pll->pl, list);
322 subset = get_permutations(list->next, n - 1);
324 new_pll = xmalloc(sizeof(*new_pll));
325 new_pll->pl = subset->pl;
326 pack_list_insert(&new_pll->pl, list);
329 subset = subset->next;
336 static int is_superset(struct pack_list *pl, struct llist *list)
340 diff = llist_copy(list);
343 llist_sorted_difference_inplace(diff, pl->all_objects);
344 if (diff->size == 0) { /* we're done */
354 static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
357 unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step;
358 const unsigned char *p1_base, *p2_base;
360 p1_base = p1->index_data;
361 p2_base = p2->index_data;
362 p1_base += 256 * 4 + ((p1->index_version < 2) ? 4 : 8);
363 p2_base += 256 * 4 + ((p2->index_version < 2) ? 4 : 8);
364 p1_step = (p1->index_version < 2) ? 24 : 20;
365 p2_step = (p2->index_version < 2) ? 24 : 20;
367 while (p1_off < p1->num_objects * p1_step &&
368 p2_off < p2->num_objects * p2_step)
370 int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
378 if (cmp < 0) { /* p1 has the object, p2 doesn't */
380 } else { /* p2 has the object, p1 doesn't */
387 /* another O(n^2) function ... */
388 static size_t get_pack_redundancy(struct pack_list *pl)
390 struct pack_list *subset;
396 while ((subset = pl->next)) {
398 ret += sizeof_union(pl->pack, subset->pack);
399 subset = subset->next;
406 static inline off_t pack_set_bytecount(struct pack_list *pl)
410 ret += pl->pack->pack_size;
411 ret += pl->pack->index_size;
417 static void minimize(struct pack_list **min)
419 struct pack_list *pl, *unique = NULL,
420 *non_unique = NULL, *min_perm = NULL;
421 struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm;
422 struct llist *missing;
423 off_t min_perm_size = 0, perm_size;
428 if (pl->unique_objects->size)
429 pack_list_insert(&unique, pl);
431 pack_list_insert(&non_unique, pl);
434 /* find out which objects are missing from the set of unique packs */
435 missing = llist_copy(all_objects);
438 llist_sorted_difference_inplace(missing, pl->all_objects);
442 /* return if there are no objects missing from the unique set */
443 if (missing->size == 0) {
448 /* find the permutations which contain all missing objects */
449 for (n = 1; n <= pack_list_size(non_unique) && !perm_ok; n++) {
450 perm_all = perm = get_permutations(non_unique, n);
452 if (is_superset(perm->pl, missing)) {
453 new_perm = xmalloc(sizeof(struct pll));
454 memcpy(new_perm, perm, sizeof(struct pll));
455 new_perm->next = perm_ok;
465 die("Internal error: No complete sets found!");
467 /* find the permutation with the smallest size */
470 perm_size = pack_set_bytecount(perm->pl);
471 if (!min_perm_size || min_perm_size > perm_size) {
472 min_perm_size = perm_size;
478 /* add the unique packs to the list */
481 pack_list_insert(min, pl);
486 static void load_all_objects(void)
488 struct pack_list *pl = local_packs;
489 struct llist_item *hint, *l;
491 llist_init(&all_objects);
495 l = pl->all_objects->front;
497 hint = llist_insert_sorted_unique(all_objects,
503 /* remove objects present in remote packs */
506 llist_sorted_difference_inplace(all_objects, pl->all_objects);
511 /* this scales like O(n^2) */
512 static void cmp_local_packs(void)
514 struct pack_list *subset, *pl = local_packs;
516 while ((subset = pl)) {
517 while ((subset = subset->next))
518 cmp_two_packs(pl, subset);
523 static void scan_alt_odb_packs(void)
525 struct pack_list *local, *alt;
531 llist_sorted_difference_inplace(local->unique_objects,
535 llist_sorted_difference_inplace(all_objects, alt->all_objects);
540 static struct pack_list * add_pack(struct packed_git *p)
543 unsigned long off = 0, step;
544 const unsigned char *base;
546 if (!p->pack_local && !(alt_odb || verbose))
550 llist_init(&l.all_objects);
552 if (open_pack_index(p))
555 base = p->index_data;
556 base += 256 * 4 + ((p->index_version < 2) ? 4 : 8);
557 step = (p->index_version < 2) ? 24 : 20;
558 while (off < p->num_objects * step) {
559 llist_insert_back(l.all_objects, base + off);
562 /* this list will be pruned in cmp_two_packs later */
563 l.unique_objects = llist_copy(l.all_objects);
565 return pack_list_insert(&local_packs, &l);
567 return pack_list_insert(&altodb_packs, &l);
570 static struct pack_list * add_pack_file(const char *filename)
572 struct packed_git *p = packed_git;
574 if (strlen(filename) < 40)
575 die("Bad pack filename: %s", filename);
578 if (strstr(p->pack_name, filename))
582 die("Filename %s not found in packed_git", filename);
585 static void load_all(void)
587 struct packed_git *p = packed_git;
595 int cmd_pack_redundant(int argc, const char **argv, const char *prefix)
598 struct pack_list *min, *red, *pl;
599 struct llist *ignore;
601 char buf[42]; /* 40 byte sha1 + \n + \0 */
603 if (argc == 2 && !strcmp(argv[1], "-h"))
604 usage(pack_redundant_usage);
606 for (i = 1; i < argc; i++) {
607 const char *arg = argv[i];
608 if (!strcmp(arg, "--")) {
612 if (!strcmp(arg, "--all")) {
616 if (!strcmp(arg, "--verbose")) {
620 if (!strcmp(arg, "--alt-odb")) {
625 usage(pack_redundant_usage);
630 prepare_packed_git();
635 while (*(argv + i) != NULL)
636 add_pack_file(*(argv + i++));
638 if (local_packs == NULL)
639 die("Zero packs found!");
645 scan_alt_odb_packs();
647 /* ignore objects given on stdin */
650 while (fgets(buf, sizeof(buf), stdin)) {
652 if (get_sha1_hex(buf, sha1))
653 die("Bad sha1 on stdin: %s", buf);
654 llist_insert_sorted_unique(ignore, sha1, NULL);
657 llist_sorted_difference_inplace(all_objects, ignore);
660 llist_sorted_difference_inplace(pl->unique_objects, ignore);
667 fprintf(stderr, "There are %lu packs available in alt-odbs.\n",
668 (unsigned long)pack_list_size(altodb_packs));
669 fprintf(stderr, "The smallest (bytewise) set of packs is:\n");
672 fprintf(stderr, "\t%s\n", pl->pack->pack_name);
675 fprintf(stderr, "containing %lu duplicate objects "
676 "with a total size of %lukb.\n",
677 (unsigned long)get_pack_redundancy(min),
678 (unsigned long)pack_set_bytecount(min)/1024);
679 fprintf(stderr, "A total of %lu unique objects were considered.\n",
680 (unsigned long)all_objects->size);
681 fprintf(stderr, "Redundant packs (with indexes):\n");
683 pl = red = pack_list_difference(local_packs, min);
686 sha1_pack_index_name(pl->pack->sha1),
687 pl->pack->pack_name);
691 fprintf(stderr, "%luMB of redundant packs in total.\n",
692 (unsigned long)pack_set_bytecount(red)/(1024*1024));