Rename git-pack-intersect to git-pack-redundant
[git/dscho.git] / pack-redundant.c
blobdb3dcde22a3b290bd60762fc0ad81165c02f1fbb
1 /*
3 * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
5 * This file is licensed under the GPL v2.
7 */
9 #include "cache.h"
11 static const char pack_redundant_usage[] =
12 "git-pack-redundant [ -v ] < -a | <.pack filename> ...>";
14 int all = 0, verbose = 0;
16 struct llist_item {
17 struct llist_item *next;
18 char *sha1;
20 struct llist {
21 struct llist_item *front;
22 struct llist_item *back;
23 size_t size;
24 } *all_objects;
26 struct pack_list {
27 struct pack_list *next;
28 struct packed_git *pack;
29 struct llist *unique_objects;
30 struct llist *all_objects;
31 } *pack_list;
33 struct pll {
34 struct pll *next;
35 struct pack_list *pl;
38 inline void llist_free(struct llist *list)
40 while((list->back = list->front)) {
41 list->front = list->front->next;
42 free(list->back);
44 free(list);
47 inline void llist_init(struct llist **list)
49 *list = xmalloc(sizeof(struct llist));
50 (*list)->front = (*list)->back = NULL;
51 (*list)->size = 0;
54 struct llist * llist_copy(struct llist *list)
56 struct llist *ret;
57 struct llist_item *new, *old, *prev;
59 llist_init(&ret);
61 if ((ret->size = list->size) == 0)
62 return ret;
64 new = ret->front = xmalloc(sizeof(struct llist_item));
65 new->sha1 = list->front->sha1;
67 old = list->front->next;
68 while (old) {
69 prev = new;
70 new = xmalloc(sizeof(struct llist_item));
71 prev->next = new;
72 new->sha1 = old->sha1;
73 old = old->next;
75 new->next = NULL;
76 ret->back = new;
78 return ret;
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));
85 new->sha1 = sha1;
86 new->next = NULL;
88 if (after != NULL) {
89 new->next = after->next;
90 after->next = new;
91 if (after == list->back)
92 list->back = new;
93 } else {/* insert in front */
94 if (list->size == 0)
95 list->back = new;
96 else
97 new->next = list->front;
98 list->front = new;
100 list->size++;
101 return new;
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;
115 while (l) {
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 */
121 return l;
123 prev = l;
124 l = l->next;
126 /* insert at the end */
127 return llist_insert_back(list, sha1);
130 /* computes A\B */
131 struct llist * llist_sorted_difference(struct llist_item *A,
132 struct llist_item *B)
134 struct llist *ret;
135 llist_init(&ret);
137 while (A != NULL && B != NULL) {
138 int cmp = memcmp(A->sha1, B->sha1, 20);
139 if (!cmp) {
140 A = A->next;
141 B = B->next;
142 continue;
144 if(cmp > 0) { /* we'll never find this B */
145 B = B->next;
146 continue;
148 /* A has the object, B doesn't */
149 llist_insert_back(ret, A->sha1);
150 A = A->next;
152 while (A != NULL) {
153 llist_insert_back(ret, A->sha1);
154 A = A->next;
156 return ret;
159 /* returns a pointer to an item in front of sha1 */
160 inline struct llist_item * llist_sorted_remove(struct llist *list, char *sha1,
161 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 = memcmp(l->sha1, sha1, 20);
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 free(l);
185 list->size--;
186 return prev;
188 prev = l;
189 l = l->next;
191 return prev;
194 inline struct pack_list * pack_list_insert(struct pack_list **pl,
195 struct pack_list *entry)
197 struct pack_list *p = xmalloc(sizeof(struct pack_list));
198 memcpy(p, entry, sizeof(struct pack_list));
199 p->next = *pl;
200 *pl = p;
201 return p;
204 struct pack_list * pack_list_difference(struct pack_list *A,
205 struct pack_list *B)
207 struct pack_list *ret, *pl;
209 if (A == NULL)
210 return NULL;
212 pl = B;
213 while (pl != NULL) {
214 if (A->pack == pl->pack)
215 return pack_list_difference(A->next, B);
216 pl = pl->next;
218 ret = xmalloc(sizeof(struct pack_list));
219 memcpy(ret, A, sizeof(struct pack_list));
220 ret->next = pack_list_difference(A->next, B);
221 return ret;
224 void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
226 int p1_off, p2_off;
227 void *p1_base, *p2_base;
228 struct llist_item *p1_hint = NULL, *p2_hint = NULL;
230 p1_off = p2_off = 256 * 4 + 4;
231 p1_base = (void *)p1->pack->index_base;
232 p2_base = (void *)p2->pack->index_base;
234 while (p1_off <= p1->pack->index_size - 3 * 20 &&
235 p2_off <= p2->pack->index_size - 3 * 20)
237 int cmp = memcmp(p1_base + p1_off, p2_base + p2_off, 20);
238 /* cmp ~ p1 - p2 */
239 if (cmp == 0) {
240 p1_hint = llist_sorted_remove(p1->unique_objects,
241 p1_base + p1_off, p1_hint);
242 p2_hint = llist_sorted_remove(p2->unique_objects,
243 p1_base + p1_off, p2_hint);
244 p1_off+=24;
245 p2_off+=24;
246 continue;
248 if (cmp < 0) { /* p1 has the object, p2 doesn't */
249 p1_off+=24;
250 } else { /* p2 has the object, p1 doesn't */
251 p2_off+=24;
256 /* all the permutations have to be free()d at the same time,
257 * since they refer to each other
259 struct pll * get_all_permutations(struct pack_list *list)
261 struct pll *subset, *pll, *new_pll = NULL; /*silence warning*/
263 if (list == NULL)
264 return NULL;
266 if (list->next == NULL) {
267 new_pll = xmalloc(sizeof(struct pll));
268 new_pll->next = NULL;
269 new_pll->pl = list;
270 return new_pll;
273 pll = subset = get_all_permutations(list->next);
274 while (pll) {
275 new_pll = xmalloc(sizeof(struct pll));
276 new_pll->next = pll->next;
277 pll->next = new_pll;
279 new_pll->pl = xmalloc(sizeof(struct pack_list));
280 memcpy(new_pll->pl, list, sizeof(struct pack_list));
281 new_pll->pl->next = pll->pl;
283 pll = new_pll->next;
285 /* add ourself to the end */
286 new_pll->next = xmalloc(sizeof(struct pll));
287 new_pll->next->pl = xmalloc(sizeof(struct pack_list));
288 new_pll->next->next = NULL;
289 memcpy(new_pll->next->pl, list, sizeof(struct pack_list));
290 new_pll->next->pl->next = NULL;
292 return subset;
295 int is_superset(struct pack_list *pl, struct llist *list)
297 struct llist *diff, *old;
299 diff = llist_copy(list);
301 while (pl) {
302 old = diff;
303 diff = llist_sorted_difference(diff->front,
304 pl->all_objects->front);
305 llist_free(old);
306 if (diff->size == 0) { /* we're done */
307 llist_free(diff);
308 return 1;
310 pl = pl->next;
312 llist_free(diff);
313 return 0;
316 size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
318 size_t ret = 0;
319 int p1_off, p2_off;
320 void *p1_base, *p2_base;
322 p1_off = p2_off = 256 * 4 + 4;
323 p1_base = (void *)p1->index_base;
324 p2_base = (void *)p2->index_base;
326 while (p1_off <= p1->index_size - 3 * 20 &&
327 p2_off <= p2->index_size - 3 * 20)
329 int cmp = memcmp(p1_base + p1_off, p2_base + p2_off, 20);
330 /* cmp ~ p1 - p2 */
331 if (cmp == 0) {
332 ret++;
333 p1_off+=24;
334 p2_off+=24;
335 continue;
337 if (cmp < 0) { /* p1 has the object, p2 doesn't */
338 p1_off+=24;
339 } else { /* p2 has the object, p1 doesn't */
340 p2_off+=24;
343 return ret;
346 /* another O(n^2) function ... */
347 size_t get_pack_redundancy(struct pack_list *pl)
349 struct pack_list *subset;
350 size_t ret = 0;
351 while ((subset = pl->next)) {
352 while(subset) {
353 ret += sizeof_union(pl->pack, subset->pack);
354 subset = subset->next;
356 pl = pl->next;
358 return ret;
361 inline size_t pack_set_bytecount(struct pack_list *pl)
363 size_t ret = 0;
364 while (pl) {
365 ret += pl->pack->pack_size;
366 ret += pl->pack->index_size;
367 pl = pl->next;
369 return ret;
372 void minimize(struct pack_list **min)
374 struct pack_list *pl, *unique = NULL,
375 *non_unique = NULL, *min_perm = NULL;
376 struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm;
377 struct llist *missing, *old;
378 size_t min_perm_size = (size_t)-1, perm_size;
380 pl = pack_list;
381 while (pl) {
382 if(pl->unique_objects->size)
383 pack_list_insert(&unique, pl);
384 else
385 pack_list_insert(&non_unique, pl);
386 pl = pl->next;
388 /* find out which objects are missing from the set of unique packs */
389 missing = llist_copy(all_objects);
390 pl = unique;
391 while (pl) {
392 old = missing;
393 missing = llist_sorted_difference(missing->front,
394 pl->all_objects->front);
395 llist_free(old);
396 pl = pl->next;
399 if (missing->size == 0) {
400 *min = unique;
401 return;
404 /* find the permutations which contain all missing objects */
405 perm_all = perm = get_all_permutations(non_unique);
406 while (perm) {
407 if (is_superset(perm->pl, missing)) {
408 new_perm = xmalloc(sizeof(struct pll));
409 new_perm->pl = perm->pl;
410 new_perm->next = perm_ok;
411 perm_ok = new_perm;
413 perm = perm->next;
416 if (perm_ok == NULL)
417 die("Internal error: No complete sets found!\n");
419 /* find the permutation with the smallest size */
420 perm = perm_ok;
421 while (perm) {
422 perm_size = pack_set_bytecount(perm->pl);
423 if (min_perm_size > perm_size) {
424 min_perm_size = perm_size;
425 min_perm = perm->pl;
427 perm = perm->next;
429 *min = min_perm;
430 /* add the unique packs to the list */
431 pl = unique;
432 while(pl) {
433 pack_list_insert(min, pl);
434 pl = pl->next;
438 void load_all_objects()
440 struct pack_list *pl = pack_list;
441 struct llist_item *hint, *l;
442 int i;
444 llist_init(&all_objects);
446 while (pl) {
447 i = 0;
448 hint = NULL;
449 l = pl->all_objects->front;
450 while (l) {
451 hint = llist_insert_sorted_unique(all_objects,
452 l->sha1, hint);
453 l = l->next;
455 pl = pl->next;
459 /* this scales like O(n^2) */
460 void cmp_packs()
462 struct pack_list *subset, *curr = pack_list;
464 while ((subset = curr)) {
465 while((subset = subset->next))
466 cmp_two_packs(curr, subset);
467 curr = curr->next;
471 struct pack_list * add_pack(struct packed_git *p)
473 struct pack_list l;
474 size_t off;
475 void *base;
477 l.pack = p;
478 llist_init(&l.all_objects);
480 off = 256 * 4 + 4;
481 base = (void *)p->index_base;
482 while (off <= p->index_size - 3 * 20) {
483 llist_insert_back(l.all_objects, base + off);
484 off+=24;
486 /* this list will be pruned in cmp_two_packs later */
487 l.unique_objects = llist_copy(l.all_objects);
488 return pack_list_insert(&pack_list, &l);
491 struct pack_list * add_pack_file(char *filename)
493 struct packed_git *p = packed_git;
495 if (strlen(filename) < 40)
496 die("Bad pack filename: %s\n", filename);
498 while (p) {
499 if (strstr(p->pack_name, filename))
500 /* this will silently ignore packs in alt-odb */
501 return add_pack(p);
502 p = p->next;
504 die("Filename %s not found in packed_git\n", filename);
507 void load_all()
509 struct packed_git *p = packed_git;
511 while (p) {
512 if (p->pack_local) /* ignore alt-odb for now */
513 add_pack(p);
514 p = p->next;
518 int main(int argc, char **argv)
520 int i;
521 struct pack_list *min, *red, *pl;
523 for (i = 1; i < argc; i++) {
524 const char *arg = argv[i];
525 if(!strcmp(arg, "--")) {
526 i++;
527 break;
529 if(!strcmp(arg, "-a")) {
530 all = 1;
531 continue;
533 if(!strcmp(arg, "-v")) {
534 verbose = 1;
535 continue;
537 if(*arg == '-')
538 usage(pack_redundant_usage);
539 else
540 break;
543 prepare_packed_git();
545 if(all)
546 load_all();
547 else
548 while (*(argv + i) != NULL)
549 add_pack_file(*(argv + i++));
551 if (pack_list == NULL)
552 die("Zero packs found!\n");
554 cmp_packs();
556 load_all_objects();
558 minimize(&min);
559 if (verbose) {
560 fprintf(stderr, "The smallest (bytewise) set of packs is:\n");
561 pl = min;
562 while (pl) {
563 fprintf(stderr, "\t%s\n", pl->pack->pack_name);
564 pl = pl->next;
566 fprintf(stderr, "containing %ld duplicate objects "
567 "with a total size of %ldkb.\n",
568 get_pack_redundancy(min), pack_set_bytecount(min)/1024);
569 fprintf(stderr, "Redundant packs (with indexes):\n");
571 pl = red = pack_list_difference(pack_list, min);
572 while (pl) {
573 printf("%s\n%s\n",
574 sha1_pack_index_name(pl->pack->sha1), pl->pack->pack_name);
575 pl = pl->next;
578 return 0;