]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/pack-redundant.c
Merge branch 'jk/bundle-progress'
[thirdparty/git.git] / builtin / pack-redundant.c
1 /*
2 *
3 * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
4 *
5 * This file is licensed under the GPL v2.
6 *
7 */
8
9 #include "builtin.h"
10 #include "hex.h"
11 #include "repository.h"
12 #include "packfile.h"
13 #include "object-store.h"
14
15 #define BLKSIZE 512
16
17 static const char pack_redundant_usage[] =
18 "git pack-redundant [--verbose] [--alt-odb] (--all | <pack-filename>...)";
19
20 static int load_all_packs, verbose, alt_odb;
21
22 struct llist_item {
23 struct llist_item *next;
24 struct object_id oid;
25 };
26 static struct llist {
27 struct llist_item *front;
28 struct llist_item *back;
29 size_t size;
30 } *all_objects; /* all objects which must be present in local packfiles */
31
32 static struct pack_list {
33 struct pack_list *next;
34 struct packed_git *pack;
35 struct llist *unique_objects;
36 struct llist *remaining_objects;
37 size_t all_objects_size;
38 } *local_packs = NULL, *altodb_packs = NULL;
39
40 static struct llist_item *free_nodes;
41
42 static inline void llist_item_put(struct llist_item *item)
43 {
44 item->next = free_nodes;
45 free_nodes = item;
46 }
47
48 static inline struct llist_item *llist_item_get(void)
49 {
50 struct llist_item *new_item;
51 if ( free_nodes ) {
52 new_item = free_nodes;
53 free_nodes = free_nodes->next;
54 } else {
55 int i = 1;
56 ALLOC_ARRAY(new_item, BLKSIZE);
57 for (; i < BLKSIZE; i++)
58 llist_item_put(&new_item[i]);
59 }
60 return new_item;
61 }
62
63 static inline void llist_init(struct llist **list)
64 {
65 *list = xmalloc(sizeof(struct llist));
66 (*list)->front = (*list)->back = NULL;
67 (*list)->size = 0;
68 }
69
70 static struct llist * llist_copy(struct llist *list)
71 {
72 struct llist *ret;
73 struct llist_item *new_item, *old_item, *prev;
74
75 llist_init(&ret);
76
77 if ((ret->size = list->size) == 0)
78 return ret;
79
80 new_item = ret->front = llist_item_get();
81 new_item->oid = list->front->oid;
82
83 old_item = list->front->next;
84 while (old_item) {
85 prev = new_item;
86 new_item = llist_item_get();
87 prev->next = new_item;
88 new_item->oid = old_item->oid;
89 old_item = old_item->next;
90 }
91 new_item->next = NULL;
92 ret->back = new_item;
93
94 return ret;
95 }
96
97 static inline struct llist_item *llist_insert(struct llist *list,
98 struct llist_item *after,
99 const unsigned char *oid)
100 {
101 struct llist_item *new_item = llist_item_get();
102 oidread(&new_item->oid, oid);
103 new_item->next = NULL;
104
105 if (after) {
106 new_item->next = after->next;
107 after->next = new_item;
108 if (after == list->back)
109 list->back = new_item;
110 } else {/* insert in front */
111 if (list->size == 0)
112 list->back = new_item;
113 else
114 new_item->next = list->front;
115 list->front = new_item;
116 }
117 list->size++;
118 return new_item;
119 }
120
121 static inline struct llist_item *llist_insert_back(struct llist *list,
122 const unsigned char *oid)
123 {
124 return llist_insert(list, list->back, oid);
125 }
126
127 static inline struct llist_item *llist_insert_sorted_unique(struct llist *list,
128 const struct object_id *oid, struct llist_item *hint)
129 {
130 struct llist_item *prev = NULL, *l;
131
132 l = (hint == NULL) ? list->front : hint;
133 while (l) {
134 int cmp = oidcmp(&l->oid, oid);
135 if (cmp > 0) { /* we insert before this entry */
136 return llist_insert(list, prev, oid->hash);
137 }
138 if (!cmp) { /* already exists */
139 return l;
140 }
141 prev = l;
142 l = l->next;
143 }
144 /* insert at the end */
145 return llist_insert_back(list, oid->hash);
146 }
147
148 /* returns a pointer to an item in front of sha1 */
149 static inline struct llist_item * llist_sorted_remove(struct llist *list, const unsigned char *oid, struct llist_item *hint)
150 {
151 struct llist_item *prev, *l;
152
153 redo_from_start:
154 l = (hint == NULL) ? list->front : hint;
155 prev = NULL;
156 while (l) {
157 const int cmp = hashcmp(l->oid.hash, oid);
158 if (cmp > 0) /* not in list, since sorted */
159 return prev;
160 if (!cmp) { /* found */
161 if (!prev) {
162 if (hint != NULL && hint != list->front) {
163 /* we don't know the previous element */
164 hint = NULL;
165 goto redo_from_start;
166 }
167 list->front = l->next;
168 } else
169 prev->next = l->next;
170 if (l == list->back)
171 list->back = prev;
172 llist_item_put(l);
173 list->size--;
174 return prev;
175 }
176 prev = l;
177 l = l->next;
178 }
179 return prev;
180 }
181
182 /* computes A\B */
183 static void llist_sorted_difference_inplace(struct llist *A,
184 struct llist *B)
185 {
186 struct llist_item *hint, *b;
187
188 hint = NULL;
189 b = B->front;
190
191 while (b) {
192 hint = llist_sorted_remove(A, b->oid.hash, hint);
193 b = b->next;
194 }
195 }
196
197 static inline struct pack_list * pack_list_insert(struct pack_list **pl,
198 struct pack_list *entry)
199 {
200 struct pack_list *p = xmalloc(sizeof(struct pack_list));
201 memcpy(p, entry, sizeof(struct pack_list));
202 p->next = *pl;
203 *pl = p;
204 return p;
205 }
206
207 static inline size_t pack_list_size(struct pack_list *pl)
208 {
209 size_t ret = 0;
210 while (pl) {
211 ret++;
212 pl = pl->next;
213 }
214 return ret;
215 }
216
217 static struct pack_list * pack_list_difference(const struct pack_list *A,
218 const struct pack_list *B)
219 {
220 struct pack_list *ret;
221 const struct pack_list *pl;
222
223 if (!A)
224 return NULL;
225
226 pl = B;
227 while (pl != NULL) {
228 if (A->pack == pl->pack)
229 return pack_list_difference(A->next, B);
230 pl = pl->next;
231 }
232 ret = xmalloc(sizeof(struct pack_list));
233 memcpy(ret, A, sizeof(struct pack_list));
234 ret->next = pack_list_difference(A->next, B);
235 return ret;
236 }
237
238 static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
239 {
240 size_t p1_off = 0, p2_off = 0, p1_step, p2_step;
241 const unsigned char *p1_base, *p2_base;
242 struct llist_item *p1_hint = NULL, *p2_hint = NULL;
243 const unsigned int hashsz = the_hash_algo->rawsz;
244
245 if (!p1->unique_objects)
246 p1->unique_objects = llist_copy(p1->remaining_objects);
247 if (!p2->unique_objects)
248 p2->unique_objects = llist_copy(p2->remaining_objects);
249
250 p1_base = p1->pack->index_data;
251 p2_base = p2->pack->index_data;
252 p1_base += 256 * 4 + ((p1->pack->index_version < 2) ? 4 : 8);
253 p2_base += 256 * 4 + ((p2->pack->index_version < 2) ? 4 : 8);
254 p1_step = hashsz + ((p1->pack->index_version < 2) ? 4 : 0);
255 p2_step = hashsz + ((p2->pack->index_version < 2) ? 4 : 0);
256
257 while (p1_off < p1->pack->num_objects * p1_step &&
258 p2_off < p2->pack->num_objects * p2_step)
259 {
260 const int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
261 /* cmp ~ p1 - p2 */
262 if (cmp == 0) {
263 p1_hint = llist_sorted_remove(p1->unique_objects,
264 p1_base + p1_off,
265 p1_hint);
266 p2_hint = llist_sorted_remove(p2->unique_objects,
267 p1_base + p1_off,
268 p2_hint);
269 p1_off += p1_step;
270 p2_off += p2_step;
271 continue;
272 }
273 if (cmp < 0) { /* p1 has the object, p2 doesn't */
274 p1_off += p1_step;
275 } else { /* p2 has the object, p1 doesn't */
276 p2_off += p2_step;
277 }
278 }
279 }
280
281 static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
282 {
283 size_t ret = 0;
284 size_t p1_off = 0, p2_off = 0, p1_step, p2_step;
285 const unsigned char *p1_base, *p2_base;
286 const unsigned int hashsz = the_hash_algo->rawsz;
287
288 p1_base = p1->index_data;
289 p2_base = p2->index_data;
290 p1_base += 256 * 4 + ((p1->index_version < 2) ? 4 : 8);
291 p2_base += 256 * 4 + ((p2->index_version < 2) ? 4 : 8);
292 p1_step = hashsz + ((p1->index_version < 2) ? 4 : 0);
293 p2_step = hashsz + ((p2->index_version < 2) ? 4 : 0);
294
295 while (p1_off < p1->num_objects * p1_step &&
296 p2_off < p2->num_objects * p2_step)
297 {
298 int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
299 /* cmp ~ p1 - p2 */
300 if (cmp == 0) {
301 ret++;
302 p1_off += p1_step;
303 p2_off += p2_step;
304 continue;
305 }
306 if (cmp < 0) { /* p1 has the object, p2 doesn't */
307 p1_off += p1_step;
308 } else { /* p2 has the object, p1 doesn't */
309 p2_off += p2_step;
310 }
311 }
312 return ret;
313 }
314
315 /* another O(n^2) function ... */
316 static size_t get_pack_redundancy(struct pack_list *pl)
317 {
318 struct pack_list *subset;
319 size_t ret = 0;
320
321 if (!pl)
322 return 0;
323
324 while ((subset = pl->next)) {
325 while (subset) {
326 ret += sizeof_union(pl->pack, subset->pack);
327 subset = subset->next;
328 }
329 pl = pl->next;
330 }
331 return ret;
332 }
333
334 static inline off_t pack_set_bytecount(struct pack_list *pl)
335 {
336 off_t ret = 0;
337 while (pl) {
338 ret += pl->pack->pack_size;
339 ret += pl->pack->index_size;
340 pl = pl->next;
341 }
342 return ret;
343 }
344
345 static int cmp_remaining_objects(const void *a, const void *b)
346 {
347 struct pack_list *pl_a = *((struct pack_list **)a);
348 struct pack_list *pl_b = *((struct pack_list **)b);
349
350 if (pl_a->remaining_objects->size == pl_b->remaining_objects->size) {
351 /* have the same remaining_objects, big pack first */
352 if (pl_a->all_objects_size == pl_b->all_objects_size)
353 return 0;
354 else if (pl_a->all_objects_size < pl_b->all_objects_size)
355 return 1;
356 else
357 return -1;
358 } else if (pl_a->remaining_objects->size < pl_b->remaining_objects->size) {
359 /* sort by remaining objects, more objects first */
360 return 1;
361 } else {
362 return -1;
363 }
364 }
365
366 /* Sort pack_list, greater size of remaining_objects first */
367 static void sort_pack_list(struct pack_list **pl)
368 {
369 struct pack_list **ary, *p;
370 int i;
371 size_t n = pack_list_size(*pl);
372
373 if (n < 2)
374 return;
375
376 /* prepare an array of packed_list for easier sorting */
377 CALLOC_ARRAY(ary, n);
378 for (n = 0, p = *pl; p; p = p->next)
379 ary[n++] = p;
380
381 QSORT(ary, n, cmp_remaining_objects);
382
383 /* link them back again */
384 for (i = 0; i < n - 1; i++)
385 ary[i]->next = ary[i + 1];
386 ary[n - 1]->next = NULL;
387 *pl = ary[0];
388
389 free(ary);
390 }
391
392
393 static void minimize(struct pack_list **min)
394 {
395 struct pack_list *pl, *unique = NULL, *non_unique = NULL;
396 struct llist *missing, *unique_pack_objects;
397
398 pl = local_packs;
399 while (pl) {
400 if (pl->unique_objects->size)
401 pack_list_insert(&unique, pl);
402 else
403 pack_list_insert(&non_unique, pl);
404 pl = pl->next;
405 }
406 /* find out which objects are missing from the set of unique packs */
407 missing = llist_copy(all_objects);
408 pl = unique;
409 while (pl) {
410 llist_sorted_difference_inplace(missing, pl->remaining_objects);
411 pl = pl->next;
412 }
413
414 *min = unique;
415
416 /* return if there are no objects missing from the unique set */
417 if (missing->size == 0) {
418 free(missing);
419 return;
420 }
421
422 unique_pack_objects = llist_copy(all_objects);
423 llist_sorted_difference_inplace(unique_pack_objects, missing);
424
425 /* remove unique pack objects from the non_unique packs */
426 pl = non_unique;
427 while (pl) {
428 llist_sorted_difference_inplace(pl->remaining_objects, unique_pack_objects);
429 pl = pl->next;
430 }
431
432 while (non_unique) {
433 /* sort the non_unique packs, greater size of remaining_objects first */
434 sort_pack_list(&non_unique);
435 if (non_unique->remaining_objects->size == 0)
436 break;
437
438 pack_list_insert(min, non_unique);
439
440 for (pl = non_unique->next; pl && pl->remaining_objects->size > 0; pl = pl->next)
441 llist_sorted_difference_inplace(pl->remaining_objects, non_unique->remaining_objects);
442
443 non_unique = non_unique->next;
444 }
445 }
446
447 static void load_all_objects(void)
448 {
449 struct pack_list *pl = local_packs;
450 struct llist_item *hint, *l;
451
452 llist_init(&all_objects);
453
454 while (pl) {
455 hint = NULL;
456 l = pl->remaining_objects->front;
457 while (l) {
458 hint = llist_insert_sorted_unique(all_objects,
459 &l->oid, hint);
460 l = l->next;
461 }
462 pl = pl->next;
463 }
464 /* remove objects present in remote packs */
465 pl = altodb_packs;
466 while (pl) {
467 llist_sorted_difference_inplace(all_objects, pl->remaining_objects);
468 pl = pl->next;
469 }
470 }
471
472 /* this scales like O(n^2) */
473 static void cmp_local_packs(void)
474 {
475 struct pack_list *subset, *pl = local_packs;
476
477 /* only one packfile */
478 if (!pl->next) {
479 llist_init(&pl->unique_objects);
480 return;
481 }
482
483 while ((subset = pl)) {
484 while ((subset = subset->next))
485 cmp_two_packs(pl, subset);
486 pl = pl->next;
487 }
488 }
489
490 static void scan_alt_odb_packs(void)
491 {
492 struct pack_list *local, *alt;
493
494 alt = altodb_packs;
495 while (alt) {
496 local = local_packs;
497 while (local) {
498 llist_sorted_difference_inplace(local->remaining_objects,
499 alt->remaining_objects);
500 local = local->next;
501 }
502 alt = alt->next;
503 }
504 }
505
506 static struct pack_list * add_pack(struct packed_git *p)
507 {
508 struct pack_list l;
509 size_t off = 0, step;
510 const unsigned char *base;
511
512 if (!p->pack_local && !(alt_odb || verbose))
513 return NULL;
514
515 l.pack = p;
516 llist_init(&l.remaining_objects);
517
518 if (open_pack_index(p))
519 return NULL;
520
521 base = p->index_data;
522 base += 256 * 4 + ((p->index_version < 2) ? 4 : 8);
523 step = the_hash_algo->rawsz + ((p->index_version < 2) ? 4 : 0);
524 while (off < p->num_objects * step) {
525 llist_insert_back(l.remaining_objects, base + off);
526 off += step;
527 }
528 l.all_objects_size = l.remaining_objects->size;
529 l.unique_objects = NULL;
530 if (p->pack_local)
531 return pack_list_insert(&local_packs, &l);
532 else
533 return pack_list_insert(&altodb_packs, &l);
534 }
535
536 static struct pack_list * add_pack_file(const char *filename)
537 {
538 struct packed_git *p = get_all_packs(the_repository);
539
540 if (strlen(filename) < 40)
541 die("Bad pack filename: %s", filename);
542
543 while (p) {
544 if (strstr(p->pack_name, filename))
545 return add_pack(p);
546 p = p->next;
547 }
548 die("Filename %s not found in packed_git", filename);
549 }
550
551 static void load_all(void)
552 {
553 struct packed_git *p = get_all_packs(the_repository);
554
555 while (p) {
556 add_pack(p);
557 p = p->next;
558 }
559 }
560
561 int cmd_pack_redundant(int argc, const char **argv, const char *prefix)
562 {
563 int i;
564 int i_still_use_this = 0;
565 struct pack_list *min = NULL, *red, *pl;
566 struct llist *ignore;
567 struct object_id *oid;
568 char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */
569
570 if (argc == 2 && !strcmp(argv[1], "-h"))
571 usage(pack_redundant_usage);
572
573 for (i = 1; i < argc; i++) {
574 const char *arg = argv[i];
575 if (!strcmp(arg, "--")) {
576 i++;
577 break;
578 }
579 if (!strcmp(arg, "--all")) {
580 load_all_packs = 1;
581 continue;
582 }
583 if (!strcmp(arg, "--verbose")) {
584 verbose = 1;
585 continue;
586 }
587 if (!strcmp(arg, "--alt-odb")) {
588 alt_odb = 1;
589 continue;
590 }
591 if (!strcmp(arg, "--i-still-use-this")) {
592 i_still_use_this = 1;
593 continue;
594 }
595 if (*arg == '-')
596 usage(pack_redundant_usage);
597 else
598 break;
599 }
600
601 if (!i_still_use_this) {
602 fputs(_("'git pack-redundant' is nominated for removal.\n"
603 "If you still use this command, please add an extra\n"
604 "option, '--i-still-use-this', on the command line\n"
605 "and let us know you still use it by sending an e-mail\n"
606 "to <git@vger.kernel.org>. Thanks.\n"), stderr);
607 }
608
609 if (load_all_packs)
610 load_all();
611 else
612 while (*(argv + i) != NULL)
613 add_pack_file(*(argv + i++));
614
615 if (!local_packs)
616 die("Zero packs found!");
617
618 load_all_objects();
619
620 if (alt_odb)
621 scan_alt_odb_packs();
622
623 /* ignore objects given on stdin */
624 llist_init(&ignore);
625 if (!isatty(0)) {
626 while (fgets(buf, sizeof(buf), stdin)) {
627 oid = xmalloc(sizeof(*oid));
628 if (get_oid_hex(buf, oid))
629 die("Bad object ID on stdin: %s", buf);
630 llist_insert_sorted_unique(ignore, oid, NULL);
631 }
632 }
633 llist_sorted_difference_inplace(all_objects, ignore);
634 pl = local_packs;
635 while (pl) {
636 llist_sorted_difference_inplace(pl->remaining_objects, ignore);
637 pl = pl->next;
638 }
639
640 cmp_local_packs();
641
642 minimize(&min);
643
644 if (verbose) {
645 fprintf(stderr, "There are %lu packs available in alt-odbs.\n",
646 (unsigned long)pack_list_size(altodb_packs));
647 fprintf(stderr, "The smallest (bytewise) set of packs is:\n");
648 pl = min;
649 while (pl) {
650 fprintf(stderr, "\t%s\n", pl->pack->pack_name);
651 pl = pl->next;
652 }
653 fprintf(stderr, "containing %lu duplicate objects "
654 "with a total size of %lukb.\n",
655 (unsigned long)get_pack_redundancy(min),
656 (unsigned long)pack_set_bytecount(min)/1024);
657 fprintf(stderr, "A total of %lu unique objects were considered.\n",
658 (unsigned long)all_objects->size);
659 fprintf(stderr, "Redundant packs (with indexes):\n");
660 }
661 pl = red = pack_list_difference(local_packs, min);
662 while (pl) {
663 printf("%s\n%s\n",
664 sha1_pack_index_name(pl->pack->hash),
665 pl->pack->pack_name);
666 pl = pl->next;
667 }
668 if (verbose)
669 fprintf(stderr, "%luMB of redundant packs in total.\n",
670 (unsigned long)pack_set_bytecount(red)/(1024*1024));
671
672 return 0;
673 }