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 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 */
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;
38 inline void llist_free(struct llist *list)
40 while((list->back = list->front)) {
41 list->front = list->front->next;
47 inline void llist_init(struct llist **list)
49 *list = xmalloc(sizeof(struct llist));
50 (*list)->front = (*list)->back = NULL;
54 struct llist * llist_copy(struct llist *list)
57 struct llist_item *new, *old, *prev;
61 if ((ret->size = list->size) == 0)
64 new = ret->front = xmalloc(sizeof(struct llist_item));
65 new->sha1 = list->front->sha1;
67 old = list->front->next;
70 new = xmalloc(sizeof(struct llist_item));
72 new->sha1 = old->sha1;
81 inline struct llist_item * llist_insert(struct llist *list,
82 struct llist_item *after, char *sha1)
84 struct llist_item *new = xmalloc(sizeof(struct llist_item));
89 new->next = after->next;
91 if (after == list->back)
93 } else {/* insert in front */
97 new->next = list->front;
104 inline struct llist_item * llist_insert_back(struct llist *list, char *sha1)
106 return llist_insert(list, list->back, sha1);
109 inline struct llist_item * llist_insert_sorted_unique(struct llist *list,
110 char *sha1, struct llist_item *hint)
112 struct llist_item *prev = NULL, *l;
114 l = (hint == NULL) ? list->front : hint;
116 int cmp = memcmp(l->sha1, sha1, 20);
117 if (cmp > 0) { /* we insert before this entry */
118 return llist_insert(list, prev, sha1);
120 if(!cmp) { /* already exists */
126 /* insert at the end */
127 return llist_insert_back(list, sha1);
130 /* returns a pointer to an item in front of sha1 */
131 inline struct llist_item * llist_sorted_remove(struct llist *list, char *sha1,
132 struct llist_item *hint)
134 struct llist_item *prev, *l;
137 l = (hint == NULL) ? list->front : hint;
140 int cmp = memcmp(l->sha1, sha1, 20);
141 if (cmp > 0) /* not in list, since sorted */
143 if(!cmp) { /* found */
145 if (hint != NULL && hint != list->front) {
146 /* we don't know the previous element */
148 goto redo_from_start;
150 list->front = l->next;
152 prev->next = l->next;
166 void llist_sorted_difference_inplace(struct llist *A,
169 struct llist_item *hint, *b;
175 hint = llist_sorted_remove(A, b->sha1, hint);
180 inline struct pack_list * pack_list_insert(struct pack_list **pl,
181 struct pack_list *entry)
183 struct pack_list *p = xmalloc(sizeof(struct pack_list));
184 memcpy(p, entry, sizeof(struct pack_list));
190 inline size_t pack_list_size(struct pack_list *pl)
200 struct pack_list * pack_list_difference(struct pack_list *A,
203 struct pack_list *ret, *pl;
210 if (A->pack == pl->pack)
211 return pack_list_difference(A->next, B);
214 ret = xmalloc(sizeof(struct pack_list));
215 memcpy(ret, A, sizeof(struct pack_list));
216 ret->next = pack_list_difference(A->next, B);
220 void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
223 void *p1_base, *p2_base;
224 struct llist_item *p1_hint = NULL, *p2_hint = NULL;
226 p1_off = p2_off = 256 * 4 + 4;
227 p1_base = (void *)p1->pack->index_base;
228 p2_base = (void *)p2->pack->index_base;
230 while (p1_off <= p1->pack->index_size - 3 * 20 &&
231 p2_off <= p2->pack->index_size - 3 * 20)
233 int cmp = memcmp(p1_base + p1_off, p2_base + p2_off, 20);
236 p1_hint = llist_sorted_remove(p1->unique_objects,
237 p1_base + p1_off, p1_hint);
238 p2_hint = llist_sorted_remove(p2->unique_objects,
239 p1_base + p1_off, p2_hint);
244 if (cmp < 0) { /* p1 has the object, p2 doesn't */
246 } else { /* p2 has the object, p1 doesn't */
252 /* all the permutations have to be free()d at the same time,
253 * since they refer to each other
255 struct pll * get_all_permutations(struct pack_list *list)
257 struct pll *subset, *pll, *new_pll = NULL; /*silence warning*/
262 if (list->next == NULL) {
263 new_pll = xmalloc(sizeof(struct pll));
264 new_pll->next = NULL;
269 pll = subset = get_all_permutations(list->next);
271 new_pll = xmalloc(sizeof(struct pll));
272 new_pll->next = pll->next;
275 new_pll->pl = xmalloc(sizeof(struct pack_list));
276 memcpy(new_pll->pl, list, sizeof(struct pack_list));
277 new_pll->pl->next = pll->pl;
281 /* add ourself to the end */
282 new_pll->next = xmalloc(sizeof(struct pll));
283 new_pll->next->pl = xmalloc(sizeof(struct pack_list));
284 new_pll->next->next = NULL;
285 memcpy(new_pll->next->pl, list, sizeof(struct pack_list));
286 new_pll->next->pl->next = NULL;
291 int is_superset(struct pack_list *pl, struct llist *list)
295 diff = llist_copy(list);
298 llist_sorted_difference_inplace(diff,
300 if (diff->size == 0) { /* we're done */
310 size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
314 void *p1_base, *p2_base;
316 p1_off = p2_off = 256 * 4 + 4;
317 p1_base = (void *)p1->index_base;
318 p2_base = (void *)p2->index_base;
320 while (p1_off <= p1->index_size - 3 * 20 &&
321 p2_off <= p2->index_size - 3 * 20)
323 int cmp = memcmp(p1_base + p1_off, p2_base + p2_off, 20);
331 if (cmp < 0) { /* p1 has the object, p2 doesn't */
333 } else { /* p2 has the object, p1 doesn't */
340 /* another O(n^2) function ... */
341 size_t get_pack_redundancy(struct pack_list *pl)
343 struct pack_list *subset;
349 while ((subset = pl->next)) {
351 ret += sizeof_union(pl->pack, subset->pack);
352 subset = subset->next;
359 inline size_t pack_set_bytecount(struct pack_list *pl)
363 ret += pl->pack->pack_size;
364 ret += pl->pack->index_size;
370 void minimize(struct pack_list **min)
372 struct pack_list *pl, *unique = NULL,
373 *non_unique = NULL, *min_perm = NULL;
374 struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm;
375 struct llist *missing;
376 size_t min_perm_size = (size_t)-1, perm_size;
380 if(pl->unique_objects->size)
381 pack_list_insert(&unique, pl);
383 pack_list_insert(&non_unique, pl);
386 /* find out which objects are missing from the set of unique packs */
387 missing = llist_copy(all_objects);
390 llist_sorted_difference_inplace(missing,
395 /* return if there are no objects missing from the unique set */
396 if (missing->size == 0) {
401 /* find the permutations which contain all missing objects */
402 perm_all = perm = get_all_permutations(non_unique);
404 if (is_superset(perm->pl, missing)) {
405 new_perm = xmalloc(sizeof(struct pll));
406 new_perm->pl = perm->pl;
407 new_perm->next = perm_ok;
414 die("Internal error: No complete sets found!\n");
416 /* find the permutation with the smallest size */
419 perm_size = pack_set_bytecount(perm->pl);
420 if (min_perm_size > perm_size) {
421 min_perm_size = perm_size;
427 /* add the unique packs to the list */
430 pack_list_insert(min, pl);
435 void load_all_objects()
437 struct pack_list *pl = local_packs;
438 struct llist_item *hint, *l;
441 llist_init(&all_objects);
446 l = pl->all_objects->front;
448 hint = llist_insert_sorted_unique(all_objects,
454 /* remove objects present in remote packs */
457 llist_sorted_difference_inplace(all_objects, pl->all_objects);
462 /* this scales like O(n^2) */
465 struct pack_list *subset, *pl = local_packs;
467 while ((subset = pl)) {
468 while((subset = subset->next))
469 cmp_two_packs(pl, subset);
475 subset = local_packs;
477 llist_sorted_difference_inplace(subset->unique_objects,
479 subset = subset->next;
485 struct pack_list * add_pack(struct packed_git *p)
492 llist_init(&l.all_objects);
495 base = (void *)p->index_base;
496 while (off <= p->index_size - 3 * 20) {
497 llist_insert_back(l.all_objects, base + off);
500 /* this list will be pruned in cmp_two_packs later */
501 l.unique_objects = llist_copy(l.all_objects);
503 return pack_list_insert(&local_packs, &l);
505 return alt_odb ? pack_list_insert(&altodb_packs, &l) : NULL;
508 struct pack_list * add_pack_file(char *filename)
510 struct packed_git *p = packed_git;
512 if (strlen(filename) < 40)
513 die("Bad pack filename: %s\n", filename);
516 if (strstr(p->pack_name, filename))
520 die("Filename %s not found in packed_git\n", filename);
525 struct packed_git *p = packed_git;
533 int main(int argc, char **argv)
536 struct pack_list *min, *red, *pl;
538 for (i = 1; i < argc; i++) {
539 const char *arg = argv[i];
540 if(!strcmp(arg, "--")) {
544 if(!strcmp(arg, "--all")) {
548 if(!strcmp(arg, "--verbose")) {
552 if(!strcmp(arg, "--alt-odb")) {
557 usage(pack_redundant_usage);
562 prepare_packed_git();
567 while (*(argv + i) != NULL)
568 add_pack_file(*(argv + i++));
570 if (local_packs == NULL)
571 die("Zero packs found!\n");
579 fprintf(stderr, "There are %lu packs available in alt-odbs.\n",
580 (unsigned long)pack_list_size(altodb_packs));
581 fprintf(stderr, "The smallest (bytewise) set of packs is:\n");
584 fprintf(stderr, "\t%s\n", pl->pack->pack_name);
587 fprintf(stderr, "containing %lu duplicate objects "
588 "with a total size of %lukb.\n",
589 (unsigned long)get_pack_redundancy(min),
590 (unsigned long)pack_set_bytecount(min)/1024);
591 fprintf(stderr, "A total of %lu unique objects were considered.\n",
592 (unsigned long)all_objects->size);
593 fprintf(stderr, "Redundant packs (with indexes):\n");
595 pl = red = pack_list_difference(local_packs, min);
598 sha1_pack_index_name(pl->pack->sha1),
599 pl->pack->pack_name);