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