3 * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
5 * This file is licensed under the GPL v2.
14 static const char pack_redundant_usage
[] =
15 "git pack-redundant [ --verbose ] [ --alt-odb ] < --all | <.pack filename> ...>";
17 static int load_all_packs
, verbose
, alt_odb
;
20 struct llist_item
*next
;
21 const unsigned char *sha1
;
24 struct llist_item
*front
;
25 struct llist_item
*back
;
27 } *all_objects
; /* all objects which must be present in local packfiles */
29 static struct pack_list
{
30 struct pack_list
*next
;
31 struct packed_git
*pack
;
32 struct llist
*unique_objects
;
33 struct llist
*all_objects
;
34 } *local_packs
= NULL
, *altodb_packs
= NULL
;
41 static struct llist_item
*free_nodes
;
43 static inline void llist_item_put(struct llist_item
*item
)
45 item
->next
= free_nodes
;
49 static inline struct llist_item
*llist_item_get(void)
51 struct llist_item
*new;
54 free_nodes
= free_nodes
->next
;
57 new = xmalloc(sizeof(struct llist_item
) * BLKSIZE
);
58 for(;i
< BLKSIZE
; i
++) {
59 llist_item_put(&new[i
]);
65 static void llist_free(struct llist
*list
)
67 while((list
->back
= list
->front
)) {
68 list
->front
= list
->front
->next
;
69 llist_item_put(list
->back
);
74 static inline void llist_init(struct llist
**list
)
76 *list
= xmalloc(sizeof(struct llist
));
77 (*list
)->front
= (*list
)->back
= NULL
;
81 static struct llist
* llist_copy(struct llist
*list
)
84 struct llist_item
*new, *old
, *prev
;
88 if ((ret
->size
= list
->size
) == 0)
91 new = ret
->front
= llist_item_get();
92 new->sha1
= list
->front
->sha1
;
94 old
= list
->front
->next
;
97 new = llist_item_get();
99 new->sha1
= old
->sha1
;
108 static inline struct llist_item
*llist_insert(struct llist
*list
,
109 struct llist_item
*after
,
110 const unsigned char *sha1
)
112 struct llist_item
*new = llist_item_get();
117 new->next
= after
->next
;
119 if (after
== list
->back
)
121 } else {/* insert in front */
125 new->next
= list
->front
;
132 static inline struct llist_item
*llist_insert_back(struct llist
*list
,
133 const unsigned char *sha1
)
135 return llist_insert(list
, list
->back
, sha1
);
138 static inline struct llist_item
*llist_insert_sorted_unique(struct llist
*list
,
139 const unsigned char *sha1
, struct llist_item
*hint
)
141 struct llist_item
*prev
= NULL
, *l
;
143 l
= (hint
== NULL
) ? list
->front
: hint
;
145 int cmp
= hashcmp(l
->sha1
, sha1
);
146 if (cmp
> 0) { /* we insert before this entry */
147 return llist_insert(list
, prev
, sha1
);
149 if(!cmp
) { /* already exists */
155 /* insert at the end */
156 return llist_insert_back(list
, sha1
);
159 /* returns a pointer to an item in front of sha1 */
160 static inline struct llist_item
* llist_sorted_remove(struct llist
*list
, const unsigned char *sha1
, struct llist_item
*hint
)
162 struct llist_item
*prev
, *l
;
165 l
= (hint
== NULL
) ? list
->front
: hint
;
168 int cmp
= hashcmp(l
->sha1
, sha1
);
169 if (cmp
> 0) /* not in list, since sorted */
171 if(!cmp
) { /* found */
173 if (hint
!= NULL
&& hint
!= list
->front
) {
174 /* we don't know the previous element */
176 goto redo_from_start
;
178 list
->front
= l
->next
;
180 prev
->next
= l
->next
;
194 static void llist_sorted_difference_inplace(struct llist
*A
,
197 struct llist_item
*hint
, *b
;
203 hint
= llist_sorted_remove(A
, b
->sha1
, hint
);
208 static inline struct pack_list
* pack_list_insert(struct pack_list
**pl
,
209 struct pack_list
*entry
)
211 struct pack_list
*p
= xmalloc(sizeof(struct pack_list
));
212 memcpy(p
, entry
, sizeof(struct pack_list
));
218 static inline size_t pack_list_size(struct pack_list
*pl
)
228 static struct pack_list
* pack_list_difference(const struct pack_list
*A
,
229 const struct pack_list
*B
)
231 struct pack_list
*ret
;
232 const struct pack_list
*pl
;
239 if (A
->pack
== pl
->pack
)
240 return pack_list_difference(A
->next
, B
);
243 ret
= xmalloc(sizeof(struct pack_list
));
244 memcpy(ret
, A
, sizeof(struct pack_list
));
245 ret
->next
= pack_list_difference(A
->next
, B
);
249 static void cmp_two_packs(struct pack_list
*p1
, struct pack_list
*p2
)
251 unsigned long p1_off
= 0, p2_off
= 0, p1_step
, p2_step
;
252 const unsigned char *p1_base
, *p2_base
;
253 struct llist_item
*p1_hint
= NULL
, *p2_hint
= NULL
;
255 p1_base
= p1
->pack
->index_data
;
256 p2_base
= p2
->pack
->index_data
;
257 p1_base
+= 256 * 4 + ((p1
->pack
->index_version
< 2) ? 4 : 8);
258 p2_base
+= 256 * 4 + ((p2
->pack
->index_version
< 2) ? 4 : 8);
259 p1_step
= (p1
->pack
->index_version
< 2) ? 24 : 20;
260 p2_step
= (p2
->pack
->index_version
< 2) ? 24 : 20;
262 while (p1_off
< p1
->pack
->num_objects
* p1_step
&&
263 p2_off
< p2
->pack
->num_objects
* p2_step
)
265 int cmp
= hashcmp(p1_base
+ p1_off
, p2_base
+ p2_off
);
268 p1_hint
= llist_sorted_remove(p1
->unique_objects
,
269 p1_base
+ p1_off
, p1_hint
);
270 p2_hint
= llist_sorted_remove(p2
->unique_objects
,
271 p1_base
+ p1_off
, p2_hint
);
276 if (cmp
< 0) { /* p1 has the object, p2 doesn't */
278 } else { /* p2 has the object, p1 doesn't */
284 static void pll_free(struct pll
*l
)
287 struct pack_list
*opl
;
301 /* all the permutations have to be free()d at the same time,
302 * since they refer to each other
304 static struct pll
* get_permutations(struct pack_list
*list
, int n
)
306 struct pll
*subset
, *ret
= NULL
, *new_pll
= NULL
, *pll
;
308 if (list
== NULL
|| pack_list_size(list
) < n
|| n
== 0)
313 new_pll
= xmalloc(sizeof(pll
));
315 pack_list_insert(&new_pll
->pl
, list
);
324 subset
= get_permutations(list
->next
, n
- 1);
326 new_pll
= xmalloc(sizeof(pll
));
327 new_pll
->pl
= subset
->pl
;
328 pack_list_insert(&new_pll
->pl
, list
);
331 subset
= subset
->next
;
338 static int is_superset(struct pack_list
*pl
, struct llist
*list
)
342 diff
= llist_copy(list
);
345 llist_sorted_difference_inplace(diff
, pl
->all_objects
);
346 if (diff
->size
== 0) { /* we're done */
356 static size_t sizeof_union(struct packed_git
*p1
, struct packed_git
*p2
)
359 unsigned long p1_off
= 0, p2_off
= 0, p1_step
, p2_step
;
360 const unsigned char *p1_base
, *p2_base
;
362 p1_base
= p1
->index_data
;
363 p2_base
= p2
->index_data
;
364 p1_base
+= 256 * 4 + ((p1
->index_version
< 2) ? 4 : 8);
365 p2_base
+= 256 * 4 + ((p2
->index_version
< 2) ? 4 : 8);
366 p1_step
= (p1
->index_version
< 2) ? 24 : 20;
367 p2_step
= (p2
->index_version
< 2) ? 24 : 20;
369 while (p1_off
< p1
->num_objects
* p1_step
&&
370 p2_off
< p2
->num_objects
* p2_step
)
372 int cmp
= hashcmp(p1_base
+ p1_off
, p2_base
+ p2_off
);
380 if (cmp
< 0) { /* p1 has the object, p2 doesn't */
382 } else { /* p2 has the object, p1 doesn't */
389 /* another O(n^2) function ... */
390 static size_t get_pack_redundancy(struct pack_list
*pl
)
392 struct pack_list
*subset
;
398 while ((subset
= pl
->next
)) {
400 ret
+= sizeof_union(pl
->pack
, subset
->pack
);
401 subset
= subset
->next
;
408 static inline off_t
pack_set_bytecount(struct pack_list
*pl
)
412 ret
+= pl
->pack
->pack_size
;
413 ret
+= pl
->pack
->index_size
;
419 static void minimize(struct pack_list
**min
)
421 struct pack_list
*pl
, *unique
= NULL
,
422 *non_unique
= NULL
, *min_perm
= NULL
;
423 struct pll
*perm
, *perm_all
, *perm_ok
= NULL
, *new_perm
;
424 struct llist
*missing
;
425 off_t min_perm_size
= 0, perm_size
;
430 if(pl
->unique_objects
->size
)
431 pack_list_insert(&unique
, pl
);
433 pack_list_insert(&non_unique
, pl
);
436 /* find out which objects are missing from the set of unique packs */
437 missing
= llist_copy(all_objects
);
440 llist_sorted_difference_inplace(missing
, pl
->all_objects
);
444 /* return if there are no objects missing from the unique set */
445 if (missing
->size
== 0) {
450 /* find the permutations which contain all missing objects */
451 for (n
= 1; n
<= pack_list_size(non_unique
) && !perm_ok
; n
++) {
452 perm_all
= perm
= get_permutations(non_unique
, n
);
454 if (is_superset(perm
->pl
, missing
)) {
455 new_perm
= xmalloc(sizeof(struct pll
));
456 memcpy(new_perm
, perm
, sizeof(struct pll
));
457 new_perm
->next
= perm_ok
;
467 die("Internal error: No complete sets found!");
469 /* find the permutation with the smallest size */
472 perm_size
= pack_set_bytecount(perm
->pl
);
473 if (!min_perm_size
|| min_perm_size
> perm_size
) {
474 min_perm_size
= perm_size
;
480 /* add the unique packs to the list */
483 pack_list_insert(min
, pl
);
488 static void load_all_objects(void)
490 struct pack_list
*pl
= local_packs
;
491 struct llist_item
*hint
, *l
;
493 llist_init(&all_objects
);
497 l
= pl
->all_objects
->front
;
499 hint
= llist_insert_sorted_unique(all_objects
,
505 /* remove objects present in remote packs */
508 llist_sorted_difference_inplace(all_objects
, pl
->all_objects
);
513 /* this scales like O(n^2) */
514 static void cmp_local_packs(void)
516 struct pack_list
*subset
, *pl
= local_packs
;
518 while ((subset
= pl
)) {
519 while((subset
= subset
->next
))
520 cmp_two_packs(pl
, subset
);
525 static void scan_alt_odb_packs(void)
527 struct pack_list
*local
, *alt
;
533 llist_sorted_difference_inplace(local
->unique_objects
,
537 llist_sorted_difference_inplace(all_objects
, alt
->all_objects
);
542 static struct pack_list
* add_pack(struct packed_git
*p
)
545 unsigned long off
= 0, step
;
546 const unsigned char *base
;
548 if (!p
->pack_local
&& !(alt_odb
|| verbose
))
552 llist_init(&l
.all_objects
);
554 if (open_pack_index(p
))
557 base
= p
->index_data
;
558 base
+= 256 * 4 + ((p
->index_version
< 2) ? 4 : 8);
559 step
= (p
->index_version
< 2) ? 24 : 20;
560 while (off
< p
->num_objects
* step
) {
561 llist_insert_back(l
.all_objects
, base
+ off
);
564 /* this list will be pruned in cmp_two_packs later */
565 l
.unique_objects
= llist_copy(l
.all_objects
);
567 return pack_list_insert(&local_packs
, &l
);
569 return pack_list_insert(&altodb_packs
, &l
);
572 static struct pack_list
* add_pack_file(char *filename
)
574 struct packed_git
*p
= packed_git
;
576 if (strlen(filename
) < 40)
577 die("Bad pack filename: %s", filename
);
580 if (strstr(p
->pack_name
, filename
))
584 die("Filename %s not found in packed_git", filename
);
587 static void load_all(void)
589 struct packed_git
*p
= packed_git
;
597 int main(int argc
, char **argv
)
600 struct pack_list
*min
, *red
, *pl
;
601 struct llist
*ignore
;
603 char buf
[42]; /* 40 byte sha1 + \n + \0 */
605 git_extract_argv0_path(argv
[0]);
607 setup_git_directory();
609 for (i
= 1; i
< argc
; i
++) {
610 const char *arg
= argv
[i
];
611 if(!strcmp(arg
, "--")) {
615 if(!strcmp(arg
, "--all")) {
619 if(!strcmp(arg
, "--verbose")) {
623 if(!strcmp(arg
, "--alt-odb")) {
628 usage(pack_redundant_usage
);
633 prepare_packed_git();
638 while (*(argv
+ i
) != NULL
)
639 add_pack_file(*(argv
+ i
++));
641 if (local_packs
== NULL
)
642 die("Zero packs found!");
648 scan_alt_odb_packs();
650 /* ignore objects given on stdin */
653 while (fgets(buf
, sizeof(buf
), stdin
)) {
655 if (get_sha1_hex(buf
, sha1
))
656 die("Bad sha1 on stdin: %s", buf
);
657 llist_insert_sorted_unique(ignore
, sha1
, NULL
);
660 llist_sorted_difference_inplace(all_objects
, ignore
);
663 llist_sorted_difference_inplace(pl
->unique_objects
, ignore
);
670 fprintf(stderr
, "There are %lu packs available in alt-odbs.\n",
671 (unsigned long)pack_list_size(altodb_packs
));
672 fprintf(stderr
, "The smallest (bytewise) set of packs is:\n");
675 fprintf(stderr
, "\t%s\n", pl
->pack
->pack_name
);
678 fprintf(stderr
, "containing %lu duplicate objects "
679 "with a total size of %lukb.\n",
680 (unsigned long)get_pack_redundancy(min
),
681 (unsigned long)pack_set_bytecount(min
)/1024);
682 fprintf(stderr
, "A total of %lu unique objects were considered.\n",
683 (unsigned long)all_objects
->size
);
684 fprintf(stderr
, "Redundant packs (with indexes):\n");
686 pl
= red
= pack_list_difference(local_packs
, min
);
689 sha1_pack_index_name(pl
->pack
->sha1
),
690 pl
->pack
->pack_name
);
694 fprintf(stderr
, "%luMB of redundant packs in total.\n",
695 (unsigned long)pack_set_bytecount(red
)/(1024*1024));