3 * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
5 * This file is licensed under the GPL v2.
11 static const char pack_redundant_usage[] =
12 "git-pack-redundant [ --verbose ] [ --alt-odb ] < --all | <.pack filename> ...>";
14 static int load_all_packs = 0, verbose = 0, alt_odb = 0;
17 struct llist_item *next;
21 struct llist_item *front;
22 struct llist_item *back;
24 } *all_objects; /* all objects which must be present in local packfiles */
26 static struct pack_list {
27 struct pack_list *next;
28 struct packed_git *pack;
29 struct llist *unique_objects;
30 struct llist *all_objects;
31 } *local_packs = NULL, *altodb_packs = NULL;
39 static struct llist_item *free_nodes = NULL;
41 static inline struct llist_item *llist_item_get()
43 struct llist_item *new;
46 free_nodes = free_nodes->next;
48 new = xmalloc(sizeof(struct llist_item));
53 static inline void llist_item_put(struct llist_item *item)
55 item->next = free_nodes;
59 static void llist_free(struct llist *list)
61 while((list->back = list->front)) {
62 list->front = list->front->next;
63 llist_item_put(list->back);
68 static inline void llist_init(struct llist **list)
70 *list = xmalloc(sizeof(struct llist));
71 (*list)->front = (*list)->back = NULL;
75 static struct llist * llist_copy(struct llist *list)
78 struct llist_item *new, *old, *prev;
82 if ((ret->size = list->size) == 0)
85 new = ret->front = llist_item_get();
86 new->sha1 = list->front->sha1;
88 old = list->front->next;
91 new = llist_item_get();
93 new->sha1 = old->sha1;
102 static inline struct llist_item * llist_insert(struct llist *list,
103 struct llist_item *after, char *sha1)
105 struct llist_item *new = llist_item_get();
110 new->next = after->next;
112 if (after == list->back)
114 } else {/* insert in front */
118 new->next = list->front;
125 static inline struct llist_item * llist_insert_back(struct llist *list, char *sha1)
127 return llist_insert(list, list->back, sha1);
130 static inline struct llist_item * llist_insert_sorted_unique(struct llist *list,
131 char *sha1, struct llist_item *hint)
133 struct llist_item *prev = NULL, *l;
135 l = (hint == NULL) ? list->front : hint;
137 int cmp = memcmp(l->sha1, sha1, 20);
138 if (cmp > 0) { /* we insert before this entry */
139 return llist_insert(list, prev, sha1);
141 if(!cmp) { /* already exists */
147 /* insert at the end */
148 return llist_insert_back(list, sha1);
151 /* returns a pointer to an item in front of sha1 */
152 static inline struct llist_item * llist_sorted_remove(struct llist *list, char *sha1,
153 struct llist_item *hint)
155 struct llist_item *prev, *l;
158 l = (hint == NULL) ? list->front : hint;
161 int cmp = memcmp(l->sha1, sha1, 20);
162 if (cmp > 0) /* not in list, since sorted */
164 if(!cmp) { /* found */
166 if (hint != NULL && hint != list->front) {
167 /* we don't know the previous element */
169 goto redo_from_start;
171 list->front = l->next;
173 prev->next = l->next;
187 static void llist_sorted_difference_inplace(struct llist *A,
190 struct llist_item *hint, *b;
196 hint = llist_sorted_remove(A, b->sha1, hint);
201 static inline struct pack_list * pack_list_insert(struct pack_list **pl,
202 struct pack_list *entry)
204 struct pack_list *p = xmalloc(sizeof(struct pack_list));
205 memcpy(p, entry, sizeof(struct pack_list));
211 static inline size_t pack_list_size(struct pack_list *pl)
221 static struct pack_list * pack_list_difference(struct pack_list *A,
224 struct pack_list *ret, *pl;
231 if (A->pack == pl->pack)
232 return pack_list_difference(A->next, B);
235 ret = xmalloc(sizeof(struct pack_list));
236 memcpy(ret, A, sizeof(struct pack_list));
237 ret->next = pack_list_difference(A->next, B);
241 static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
244 void *p1_base, *p2_base;
245 struct llist_item *p1_hint = NULL, *p2_hint = NULL;
247 p1_off = p2_off = 256 * 4 + 4;
248 p1_base = (void *)p1->pack->index_base;
249 p2_base = (void *)p2->pack->index_base;
251 while (p1_off <= p1->pack->index_size - 3 * 20 &&
252 p2_off <= p2->pack->index_size - 3 * 20)
254 int cmp = memcmp(p1_base + p1_off, p2_base + p2_off, 20);
257 p1_hint = llist_sorted_remove(p1->unique_objects,
258 p1_base + p1_off, p1_hint);
259 p2_hint = llist_sorted_remove(p2->unique_objects,
260 p1_base + p1_off, p2_hint);
265 if (cmp < 0) { /* p1 has the object, p2 doesn't */
267 } else { /* p2 has the object, p1 doesn't */
273 static void pll_insert(struct pll **pll, struct pll **hint_table)
276 int i = (*pll)->pl_size - 1;
278 if (hint_table[i] == NULL) {
279 hint_table[i--] = *pll;
280 for (; i >= 0; --i) {
281 if (hint_table[i] != NULL)
284 if (hint_table[i] == NULL) /* no elements in list */
285 die("Why did this happen?");
288 prev = hint_table[i];
289 while (prev->next && prev->next->pl_size < (*pll)->pl_size)
292 (*pll)->next = prev->next;
296 /* all the permutations have to be free()d at the same time,
297 * since they refer to each other
299 static struct pll * get_all_permutations(struct pack_list *list)
301 struct pll *subset, *pll, *new_pll = NULL; /*silence warning*/
302 static struct pll **hint = NULL;
304 hint = xcalloc(pack_list_size(list), sizeof(struct pll *));
309 if (list->next == NULL) {
310 new_pll = xmalloc(sizeof(struct pll));
312 new_pll->next = NULL;
314 new_pll->pl_size = 1;
318 pll = subset = get_all_permutations(list->next);
320 if (pll->pl->pack == list->pack) {
324 new_pll = xmalloc(sizeof(struct pll));
326 new_pll->pl = xmalloc(sizeof(struct pack_list));
327 memcpy(new_pll->pl, list, sizeof(struct pack_list));
328 new_pll->pl->next = pll->pl;
329 new_pll->pl_size = pll->pl_size + 1;
331 pll_insert(&new_pll, hint);
336 new_pll = xmalloc(sizeof(struct pll));
337 new_pll->pl = xmalloc(sizeof(struct pack_list));
338 memcpy(new_pll->pl, list, sizeof(struct pack_list));
339 new_pll->pl->next = NULL;
340 new_pll->pl_size = 1;
341 pll_insert(&new_pll, hint);
346 static int is_superset(struct pack_list *pl, struct llist *list)
350 diff = llist_copy(list);
353 llist_sorted_difference_inplace(diff,
355 if (diff->size == 0) { /* we're done */
365 static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
369 void *p1_base, *p2_base;
371 p1_off = p2_off = 256 * 4 + 4;
372 p1_base = (void *)p1->index_base;
373 p2_base = (void *)p2->index_base;
375 while (p1_off <= p1->index_size - 3 * 20 &&
376 p2_off <= p2->index_size - 3 * 20)
378 int cmp = memcmp(p1_base + p1_off, p2_base + p2_off, 20);
386 if (cmp < 0) { /* p1 has the object, p2 doesn't */
388 } else { /* p2 has the object, p1 doesn't */
395 /* another O(n^2) function ... */
396 static size_t get_pack_redundancy(struct pack_list *pl)
398 struct pack_list *subset;
404 while ((subset = pl->next)) {
406 ret += sizeof_union(pl->pack, subset->pack);
407 subset = subset->next;
414 static inline size_t pack_set_bytecount(struct pack_list *pl)
418 ret += pl->pack->pack_size;
419 ret += pl->pack->index_size;
425 static void minimize(struct pack_list **min)
427 struct pack_list *pl, *unique = NULL,
428 *non_unique = NULL, *min_perm = NULL;
429 struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm;
430 struct llist *missing;
431 size_t min_perm_size = (size_t)-1, perm_size;
435 if(pl->unique_objects->size)
436 pack_list_insert(&unique, pl);
438 pack_list_insert(&non_unique, pl);
441 /* find out which objects are missing from the set of unique packs */
442 missing = llist_copy(all_objects);
445 llist_sorted_difference_inplace(missing,
450 /* return if there are no objects missing from the unique set */
451 if (missing->size == 0) {
456 /* find the permutations which contain all missing objects */
457 perm_all = perm = get_all_permutations(non_unique);
459 if (perm_ok && perm->pl_size > perm_ok->pl_size)
460 break; /* ignore all larger permutations */
461 if (is_superset(perm->pl, missing)) {
462 new_perm = xmalloc(sizeof(struct pll));
463 memcpy(new_perm, perm, sizeof(struct pll));
464 new_perm->next = perm_ok;
471 die("Internal error: No complete sets found!\n");
473 /* find the permutation with the smallest size */
476 perm_size = pack_set_bytecount(perm->pl);
477 if (min_perm_size > perm_size) {
478 min_perm_size = perm_size;
484 /* add the unique packs to the list */
487 pack_list_insert(min, pl);
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);
501 l = pl->all_objects->front;
503 hint = llist_insert_sorted_unique(all_objects,
509 /* remove objects present in remote packs */
512 llist_sorted_difference_inplace(all_objects, pl->all_objects);
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);
529 static void scan_alt_odb_packs(void)
531 struct pack_list *local, *alt;
537 llist_sorted_difference_inplace(local->unique_objects,
545 static struct pack_list * add_pack(struct packed_git *p)
551 if (!p->pack_local && !(alt_odb || verbose))
555 llist_init(&l.all_objects);
558 base = (void *)p->index_base;
559 while (off <= p->index_size - 3 * 20) {
560 llist_insert_back(l.all_objects, base + off);
563 /* this list will be pruned in cmp_two_packs later */
564 l.unique_objects = llist_copy(l.all_objects);
566 return pack_list_insert(&local_packs, &l);
568 return pack_list_insert(&altodb_packs, &l);
571 static struct pack_list * add_pack_file(char *filename)
573 struct packed_git *p = packed_git;
575 if (strlen(filename) < 40)
576 die("Bad pack filename: %s\n", filename);
579 if (strstr(p->pack_name, filename))
583 die("Filename %s not found in packed_git\n", filename);
586 static void load_all(void)
588 struct packed_git *p = packed_git;
596 int main(int argc, char **argv)
599 struct pack_list *min, *red, *pl;
600 struct llist *ignore;
601 char *sha1, buf[42]; /* 40 byte sha1 + \n + \0 */
603 for (i = 1; i < argc; i++) {
604 const char *arg = argv[i];
605 if(!strcmp(arg, "--")) {
609 if(!strcmp(arg, "--all")) {
613 if(!strcmp(arg, "--verbose")) {
617 if(!strcmp(arg, "--alt-odb")) {
622 usage(pack_redundant_usage);
627 prepare_packed_git();
632 while (*(argv + i) != NULL)
633 add_pack_file(*(argv + i++));
635 if (local_packs == NULL)
636 die("Zero packs found!\n");
642 scan_alt_odb_packs();
644 /* ignore objects given on stdin */
647 while (fgets(buf, sizeof(buf), stdin)) {
649 if (get_sha1_hex(buf, sha1))
650 die("Bad sha1 on stdin: %s", buf);
651 llist_insert_sorted_unique(ignore, sha1, NULL);
654 llist_sorted_difference_inplace(all_objects, ignore);
657 llist_sorted_difference_inplace(pl->unique_objects, ignore);
664 fprintf(stderr, "There are %lu packs available in alt-odbs.\n",
665 (unsigned long)pack_list_size(altodb_packs));
666 fprintf(stderr, "The smallest (bytewise) set of packs is:\n");
669 fprintf(stderr, "\t%s\n", pl->pack->pack_name);
672 fprintf(stderr, "containing %lu duplicate objects "
673 "with a total size of %lukb.\n",
674 (unsigned long)get_pack_redundancy(min),
675 (unsigned long)pack_set_bytecount(min)/1024);
676 fprintf(stderr, "A total of %lu unique objects were considered.\n",
677 (unsigned long)all_objects->size);
678 fprintf(stderr, "Redundant packs (with indexes):\n");
680 pl = red = pack_list_difference(local_packs, min);
683 sha1_pack_index_name(pl->pack->sha1),
684 pl->pack->pack_name);
688 fprintf(stderr, "%luMB of redundant packs in total.\n", pack_set_bytecount(red)/(1024*1024));