3 * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
5 * This file is licensed under the GPL v2.
10 #include "repository.h"
12 #include "object-store.h"
16 static const char pack_redundant_usage
[] =
17 "git pack-redundant [--verbose] [--alt-odb] (--all | <filename.pack>...)";
19 static int load_all_packs
, verbose
, alt_odb
;
22 struct llist_item
*next
;
23 const struct object_id
*oid
;
26 struct llist_item
*front
;
27 struct llist_item
*back
;
29 } *all_objects
; /* all objects which must be present in local packfiles */
31 static struct pack_list
{
32 struct pack_list
*next
;
33 struct packed_git
*pack
;
34 struct llist
*unique_objects
;
35 struct llist
*remaining_objects
;
36 size_t all_objects_size
;
37 } *local_packs
= NULL
, *altodb_packs
= NULL
;
39 static struct llist_item
*free_nodes
;
41 static inline void llist_item_put(struct llist_item
*item
)
43 item
->next
= free_nodes
;
47 static inline struct llist_item
*llist_item_get(void)
49 struct llist_item
*new_item
;
51 new_item
= free_nodes
;
52 free_nodes
= free_nodes
->next
;
55 ALLOC_ARRAY(new_item
, BLKSIZE
);
56 for (; i
< BLKSIZE
; i
++)
57 llist_item_put(&new_item
[i
]);
62 static inline void llist_init(struct llist
**list
)
64 *list
= xmalloc(sizeof(struct llist
));
65 (*list
)->front
= (*list
)->back
= NULL
;
69 static struct llist
* llist_copy(struct llist
*list
)
72 struct llist_item
*new_item
, *old_item
, *prev
;
76 if ((ret
->size
= list
->size
) == 0)
79 new_item
= ret
->front
= llist_item_get();
80 new_item
->oid
= list
->front
->oid
;
82 old_item
= list
->front
->next
;
85 new_item
= llist_item_get();
86 prev
->next
= new_item
;
87 new_item
->oid
= old_item
->oid
;
88 old_item
= old_item
->next
;
90 new_item
->next
= NULL
;
96 static inline struct llist_item
*llist_insert(struct llist
*list
,
97 struct llist_item
*after
,
98 const struct object_id
*oid
)
100 struct llist_item
*new_item
= llist_item_get();
102 new_item
->next
= NULL
;
105 new_item
->next
= after
->next
;
106 after
->next
= new_item
;
107 if (after
== list
->back
)
108 list
->back
= new_item
;
109 } else {/* insert in front */
111 list
->back
= new_item
;
113 new_item
->next
= list
->front
;
114 list
->front
= new_item
;
120 static inline struct llist_item
*llist_insert_back(struct llist
*list
,
121 const struct object_id
*oid
)
123 return llist_insert(list
, list
->back
, oid
);
126 static inline struct llist_item
*llist_insert_sorted_unique(struct llist
*list
,
127 const struct object_id
*oid
, struct llist_item
*hint
)
129 struct llist_item
*prev
= NULL
, *l
;
131 l
= (hint
== NULL
) ? list
->front
: hint
;
133 int cmp
= oidcmp(l
->oid
, oid
);
134 if (cmp
> 0) { /* we insert before this entry */
135 return llist_insert(list
, prev
, oid
);
137 if (!cmp
) { /* already exists */
143 /* insert at the end */
144 return llist_insert_back(list
, oid
);
147 /* returns a pointer to an item in front of sha1 */
148 static inline struct llist_item
* llist_sorted_remove(struct llist
*list
, const struct object_id
*oid
, struct llist_item
*hint
)
150 struct llist_item
*prev
, *l
;
153 l
= (hint
== NULL
) ? list
->front
: hint
;
156 const int cmp
= oidcmp(l
->oid
, oid
);
157 if (cmp
> 0) /* not in list, since sorted */
159 if (!cmp
) { /* found */
161 if (hint
!= NULL
&& hint
!= list
->front
) {
162 /* we don't know the previous element */
164 goto redo_from_start
;
166 list
->front
= l
->next
;
168 prev
->next
= l
->next
;
182 static void llist_sorted_difference_inplace(struct llist
*A
,
185 struct llist_item
*hint
, *b
;
191 hint
= llist_sorted_remove(A
, b
->oid
, hint
);
196 static inline struct pack_list
* pack_list_insert(struct pack_list
**pl
,
197 struct pack_list
*entry
)
199 struct pack_list
*p
= xmalloc(sizeof(struct pack_list
));
200 memcpy(p
, entry
, sizeof(struct pack_list
));
206 static inline size_t pack_list_size(struct pack_list
*pl
)
216 static struct pack_list
* pack_list_difference(const struct pack_list
*A
,
217 const struct pack_list
*B
)
219 struct pack_list
*ret
;
220 const struct pack_list
*pl
;
227 if (A
->pack
== pl
->pack
)
228 return pack_list_difference(A
->next
, B
);
231 ret
= xmalloc(sizeof(struct pack_list
));
232 memcpy(ret
, A
, sizeof(struct pack_list
));
233 ret
->next
= pack_list_difference(A
->next
, B
);
237 static void cmp_two_packs(struct pack_list
*p1
, struct pack_list
*p2
)
239 size_t p1_off
= 0, p2_off
= 0, p1_step
, p2_step
;
240 const unsigned char *p1_base
, *p2_base
;
241 struct llist_item
*p1_hint
= NULL
, *p2_hint
= NULL
;
242 const unsigned int hashsz
= the_hash_algo
->rawsz
;
244 if (!p1
->unique_objects
)
245 p1
->unique_objects
= llist_copy(p1
->remaining_objects
);
246 if (!p2
->unique_objects
)
247 p2
->unique_objects
= llist_copy(p2
->remaining_objects
);
249 p1_base
= p1
->pack
->index_data
;
250 p2_base
= p2
->pack
->index_data
;
251 p1_base
+= 256 * 4 + ((p1
->pack
->index_version
< 2) ? 4 : 8);
252 p2_base
+= 256 * 4 + ((p2
->pack
->index_version
< 2) ? 4 : 8);
253 p1_step
= hashsz
+ ((p1
->pack
->index_version
< 2) ? 4 : 0);
254 p2_step
= hashsz
+ ((p2
->pack
->index_version
< 2) ? 4 : 0);
256 while (p1_off
< p1
->pack
->num_objects
* p1_step
&&
257 p2_off
< p2
->pack
->num_objects
* p2_step
)
259 const int cmp
= hashcmp(p1_base
+ p1_off
, p2_base
+ p2_off
);
262 p1_hint
= llist_sorted_remove(p1
->unique_objects
,
263 (const struct object_id
*)(p1_base
+ p1_off
),
265 p2_hint
= llist_sorted_remove(p2
->unique_objects
,
266 (const struct object_id
*)(p1_base
+ p1_off
),
272 if (cmp
< 0) { /* p1 has the object, p2 doesn't */
274 } else { /* p2 has the object, p1 doesn't */
280 static size_t sizeof_union(struct packed_git
*p1
, struct packed_git
*p2
)
283 size_t p1_off
= 0, p2_off
= 0, p1_step
, p2_step
;
284 const unsigned char *p1_base
, *p2_base
;
285 const unsigned int hashsz
= the_hash_algo
->rawsz
;
287 p1_base
= p1
->index_data
;
288 p2_base
= p2
->index_data
;
289 p1_base
+= 256 * 4 + ((p1
->index_version
< 2) ? 4 : 8);
290 p2_base
+= 256 * 4 + ((p2
->index_version
< 2) ? 4 : 8);
291 p1_step
= hashsz
+ ((p1
->index_version
< 2) ? 4 : 0);
292 p2_step
= hashsz
+ ((p2
->index_version
< 2) ? 4 : 0);
294 while (p1_off
< p1
->num_objects
* p1_step
&&
295 p2_off
< p2
->num_objects
* p2_step
)
297 int cmp
= hashcmp(p1_base
+ p1_off
, p2_base
+ p2_off
);
305 if (cmp
< 0) { /* p1 has the object, p2 doesn't */
307 } else { /* p2 has the object, p1 doesn't */
314 /* another O(n^2) function ... */
315 static size_t get_pack_redundancy(struct pack_list
*pl
)
317 struct pack_list
*subset
;
323 while ((subset
= pl
->next
)) {
325 ret
+= sizeof_union(pl
->pack
, subset
->pack
);
326 subset
= subset
->next
;
333 static inline off_t
pack_set_bytecount(struct pack_list
*pl
)
337 ret
+= pl
->pack
->pack_size
;
338 ret
+= pl
->pack
->index_size
;
344 static int cmp_remaining_objects(const void *a
, const void *b
)
346 struct pack_list
*pl_a
= *((struct pack_list
**)a
);
347 struct pack_list
*pl_b
= *((struct pack_list
**)b
);
349 if (pl_a
->remaining_objects
->size
== pl_b
->remaining_objects
->size
) {
350 /* have the same remaining_objects, big pack first */
351 if (pl_a
->all_objects_size
== pl_b
->all_objects_size
)
353 else if (pl_a
->all_objects_size
< pl_b
->all_objects_size
)
357 } else if (pl_a
->remaining_objects
->size
< pl_b
->remaining_objects
->size
) {
358 /* sort by remaining objects, more objects first */
365 /* Sort pack_list, greater size of remaining_objects first */
366 static void sort_pack_list(struct pack_list
**pl
)
368 struct pack_list
**ary
, *p
;
370 size_t n
= pack_list_size(*pl
);
375 /* prepare an array of packed_list for easier sorting */
376 ary
= xcalloc(n
, sizeof(struct pack_list
*));
377 for (n
= 0, p
= *pl
; p
; p
= p
->next
)
380 QSORT(ary
, n
, cmp_remaining_objects
);
382 /* link them back again */
383 for (i
= 0; i
< n
- 1; i
++)
384 ary
[i
]->next
= ary
[i
+ 1];
385 ary
[n
- 1]->next
= NULL
;
392 static void minimize(struct pack_list
**min
)
394 struct pack_list
*pl
, *unique
= NULL
, *non_unique
= NULL
;
395 struct llist
*missing
, *unique_pack_objects
;
399 if (pl
->unique_objects
->size
)
400 pack_list_insert(&unique
, pl
);
402 pack_list_insert(&non_unique
, pl
);
405 /* find out which objects are missing from the set of unique packs */
406 missing
= llist_copy(all_objects
);
409 llist_sorted_difference_inplace(missing
, pl
->remaining_objects
);
415 /* return if there are no objects missing from the unique set */
416 if (missing
->size
== 0) {
421 unique_pack_objects
= llist_copy(all_objects
);
422 llist_sorted_difference_inplace(unique_pack_objects
, missing
);
424 /* remove unique pack objects from the non_unique packs */
427 llist_sorted_difference_inplace(pl
->remaining_objects
, unique_pack_objects
);
432 /* sort the non_unique packs, greater size of remaining_objects first */
433 sort_pack_list(&non_unique
);
434 if (non_unique
->remaining_objects
->size
== 0)
437 pack_list_insert(min
, non_unique
);
439 for (pl
= non_unique
->next
; pl
&& pl
->remaining_objects
->size
> 0; pl
= pl
->next
)
440 llist_sorted_difference_inplace(pl
->remaining_objects
, non_unique
->remaining_objects
);
442 non_unique
= non_unique
->next
;
446 static void load_all_objects(void)
448 struct pack_list
*pl
= local_packs
;
449 struct llist_item
*hint
, *l
;
451 llist_init(&all_objects
);
455 l
= pl
->remaining_objects
->front
;
457 hint
= llist_insert_sorted_unique(all_objects
,
463 /* remove objects present in remote packs */
466 llist_sorted_difference_inplace(all_objects
, pl
->remaining_objects
);
471 /* this scales like O(n^2) */
472 static void cmp_local_packs(void)
474 struct pack_list
*subset
, *pl
= local_packs
;
476 /* only one packfile */
478 llist_init(&pl
->unique_objects
);
482 while ((subset
= pl
)) {
483 while ((subset
= subset
->next
))
484 cmp_two_packs(pl
, subset
);
489 static void scan_alt_odb_packs(void)
491 struct pack_list
*local
, *alt
;
497 llist_sorted_difference_inplace(local
->remaining_objects
,
498 alt
->remaining_objects
);
505 static struct pack_list
* add_pack(struct packed_git
*p
)
508 size_t off
= 0, step
;
509 const unsigned char *base
;
511 if (!p
->pack_local
&& !(alt_odb
|| verbose
))
515 llist_init(&l
.remaining_objects
);
517 if (open_pack_index(p
))
520 base
= p
->index_data
;
521 base
+= 256 * 4 + ((p
->index_version
< 2) ? 4 : 8);
522 step
= the_hash_algo
->rawsz
+ ((p
->index_version
< 2) ? 4 : 0);
523 while (off
< p
->num_objects
* step
) {
524 llist_insert_back(l
.remaining_objects
, (const struct object_id
*)(base
+ off
));
527 l
.all_objects_size
= l
.remaining_objects
->size
;
528 l
.unique_objects
= NULL
;
530 return pack_list_insert(&local_packs
, &l
);
532 return pack_list_insert(&altodb_packs
, &l
);
535 static struct pack_list
* add_pack_file(const char *filename
)
537 struct packed_git
*p
= get_all_packs(the_repository
);
539 if (strlen(filename
) < 40)
540 die("Bad pack filename: %s", filename
);
543 if (strstr(p
->pack_name
, filename
))
547 die("Filename %s not found in packed_git", filename
);
550 static void load_all(void)
552 struct packed_git
*p
= get_all_packs(the_repository
);
560 int cmd_pack_redundant(int argc
, const char **argv
, const char *prefix
)
563 int i_still_use_this
= 0;
564 struct pack_list
*min
= NULL
, *red
, *pl
;
565 struct llist
*ignore
;
566 struct object_id
*oid
;
567 char buf
[GIT_MAX_HEXSZ
+ 2]; /* hex hash + \n + \0 */
569 if (argc
== 2 && !strcmp(argv
[1], "-h"))
570 usage(pack_redundant_usage
);
572 for (i
= 1; i
< argc
; i
++) {
573 const char *arg
= argv
[i
];
574 if (!strcmp(arg
, "--")) {
578 if (!strcmp(arg
, "--all")) {
582 if (!strcmp(arg
, "--verbose")) {
586 if (!strcmp(arg
, "--alt-odb")) {
590 if (!strcmp(arg
, "--i-still-use-this")) {
591 i_still_use_this
= 1;
595 usage(pack_redundant_usage
);
600 if (!i_still_use_this
) {
601 fputs(_("'git pack-redundant' is nominated for removal.\n"
602 "If you still use this command, please add an extra\n"
603 "option, '--i-still-use-this', on the command line\n"
604 "and let us know you still use it by sending an e-mail\n"
605 "to <git@vger.kernel.org>. Thanks.\n"), stderr
);
611 while (*(argv
+ i
) != NULL
)
612 add_pack_file(*(argv
+ i
++));
614 if (local_packs
== NULL
)
615 die("Zero packs found!");
620 scan_alt_odb_packs();
622 /* ignore objects given on stdin */
625 while (fgets(buf
, sizeof(buf
), stdin
)) {
626 oid
= xmalloc(sizeof(*oid
));
627 if (get_oid_hex(buf
, oid
))
628 die("Bad object ID on stdin: %s", buf
);
629 llist_insert_sorted_unique(ignore
, oid
, NULL
);
632 llist_sorted_difference_inplace(all_objects
, ignore
);
635 llist_sorted_difference_inplace(pl
->remaining_objects
, ignore
);
644 fprintf(stderr
, "There are %lu packs available in alt-odbs.\n",
645 (unsigned long)pack_list_size(altodb_packs
));
646 fprintf(stderr
, "The smallest (bytewise) set of packs is:\n");
649 fprintf(stderr
, "\t%s\n", pl
->pack
->pack_name
);
652 fprintf(stderr
, "containing %lu duplicate objects "
653 "with a total size of %lukb.\n",
654 (unsigned long)get_pack_redundancy(min
),
655 (unsigned long)pack_set_bytecount(min
)/1024);
656 fprintf(stderr
, "A total of %lu unique objects were considered.\n",
657 (unsigned long)all_objects
->size
);
658 fprintf(stderr
, "Redundant packs (with indexes):\n");
660 pl
= red
= pack_list_difference(local_packs
, min
);
663 sha1_pack_index_name(pl
->pack
->hash
),
664 pl
->pack
->pack_name
);
668 fprintf(stderr
, "%luMB of redundant packs in total.\n",
669 (unsigned long)pack_set_bytecount(red
)/(1024*1024));