3 * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
5 * This file is licensed under the GPL v2.
13 static const char pack_redundant_usage
[] =
14 "git-pack-redundant [ --verbose ] [ --alt-odb ] < --all | <.pack filename> ...>";
16 static int load_all_packs
= 0, verbose
= 0, alt_odb
= 0;
19 struct llist_item
*next
;
23 struct llist_item
*front
;
24 struct llist_item
*back
;
26 } *all_objects
; /* all objects which must be present in local packfiles */
28 static struct pack_list
{
29 struct pack_list
*next
;
30 struct packed_git
*pack
;
31 struct llist
*unique_objects
;
32 struct llist
*all_objects
;
33 } *local_packs
= NULL
, *altodb_packs
= NULL
;
40 static struct llist_item
*free_nodes
= NULL
;
42 static inline void llist_item_put(struct llist_item
*item
)
44 item
->next
= free_nodes
;
48 static inline struct llist_item
*llist_item_get()
50 struct llist_item
*new;
53 free_nodes
= free_nodes
->next
;
56 new = xmalloc(sizeof(struct llist_item
) * BLKSIZE
);
57 for(;i
< BLKSIZE
; i
++) {
58 llist_item_put(&new[i
]);
64 static void llist_free(struct llist
*list
)
66 while((list
->back
= list
->front
)) {
67 list
->front
= list
->front
->next
;
68 llist_item_put(list
->back
);
73 static inline void llist_init(struct llist
**list
)
75 *list
= xmalloc(sizeof(struct llist
));
76 (*list
)->front
= (*list
)->back
= NULL
;
80 static struct llist
* llist_copy(struct llist
*list
)
83 struct llist_item
*new, *old
, *prev
;
87 if ((ret
->size
= list
->size
) == 0)
90 new = ret
->front
= llist_item_get();
91 new->sha1
= list
->front
->sha1
;
93 old
= list
->front
->next
;
96 new = llist_item_get();
98 new->sha1
= old
->sha1
;
107 static inline struct llist_item
* llist_insert(struct llist
*list
,
108 struct llist_item
*after
,
111 struct llist_item
*new = llist_item_get();
116 new->next
= after
->next
;
118 if (after
== list
->back
)
120 } else {/* insert in front */
124 new->next
= list
->front
;
131 static inline struct llist_item
*llist_insert_back(struct llist
*list
, unsigned char *sha1
)
133 return llist_insert(list
, list
->back
, sha1
);
136 static inline struct llist_item
*llist_insert_sorted_unique(struct llist
*list
, unsigned char *sha1
, struct llist_item
*hint
)
138 struct llist_item
*prev
= NULL
, *l
;
140 l
= (hint
== NULL
) ? list
->front
: hint
;
142 int cmp
= memcmp(l
->sha1
, sha1
, 20);
143 if (cmp
> 0) { /* we insert before this entry */
144 return llist_insert(list
, prev
, sha1
);
146 if(!cmp
) { /* already exists */
152 /* insert at the end */
153 return llist_insert_back(list
, sha1
);
156 /* returns a pointer to an item in front of sha1 */
157 static inline struct llist_item
* llist_sorted_remove(struct llist
*list
, const unsigned char *sha1
, struct llist_item
*hint
)
159 struct llist_item
*prev
, *l
;
162 l
= (hint
== NULL
) ? list
->front
: hint
;
165 int cmp
= memcmp(l
->sha1
, sha1
, 20);
166 if (cmp
> 0) /* not in list, since sorted */
168 if(!cmp
) { /* found */
170 if (hint
!= NULL
&& hint
!= list
->front
) {
171 /* we don't know the previous element */
173 goto redo_from_start
;
175 list
->front
= l
->next
;
177 prev
->next
= l
->next
;
191 static void llist_sorted_difference_inplace(struct llist
*A
,
194 struct llist_item
*hint
, *b
;
200 hint
= llist_sorted_remove(A
, b
->sha1
, hint
);
205 static inline struct pack_list
* pack_list_insert(struct pack_list
**pl
,
206 struct pack_list
*entry
)
208 struct pack_list
*p
= xmalloc(sizeof(struct pack_list
));
209 memcpy(p
, entry
, sizeof(struct pack_list
));
215 static inline size_t pack_list_size(struct pack_list
*pl
)
225 static struct pack_list
* pack_list_difference(const struct pack_list
*A
,
226 const struct pack_list
*B
)
228 struct pack_list
*ret
;
229 const struct pack_list
*pl
;
236 if (A
->pack
== pl
->pack
)
237 return pack_list_difference(A
->next
, B
);
240 ret
= xmalloc(sizeof(struct pack_list
));
241 memcpy(ret
, A
, sizeof(struct pack_list
));
242 ret
->next
= pack_list_difference(A
->next
, B
);
246 static void cmp_two_packs(struct pack_list
*p1
, struct pack_list
*p2
)
249 void *p1_base
, *p2_base
;
250 struct llist_item
*p1_hint
= NULL
, *p2_hint
= NULL
;
252 p1_off
= p2_off
= 256 * 4 + 4;
253 p1_base
= (void *)p1
->pack
->index_base
;
254 p2_base
= (void *)p2
->pack
->index_base
;
256 while (p1_off
<= p1
->pack
->index_size
- 3 * 20 &&
257 p2_off
<= p2
->pack
->index_size
- 3 * 20)
259 int cmp
= memcmp(p1_base
+ p1_off
, p2_base
+ p2_off
, 20);
262 p1_hint
= llist_sorted_remove(p1
->unique_objects
,
263 p1_base
+ p1_off
, p1_hint
);
264 p2_hint
= llist_sorted_remove(p2
->unique_objects
,
265 p1_base
+ p1_off
, p2_hint
);
270 if (cmp
< 0) { /* p1 has the object, p2 doesn't */
272 } else { /* p2 has the object, p1 doesn't */
278 void pll_free(struct pll
*l
)
281 struct pack_list
*opl
;
295 /* all the permutations have to be free()d at the same time,
296 * since they refer to each other
298 static struct pll
* get_permutations(struct pack_list
*list
, int n
)
300 struct pll
*subset
, *ret
= NULL
, *new_pll
= NULL
, *pll
;
302 if (list
== NULL
|| pack_list_size(list
) < n
|| n
== 0)
307 new_pll
= xmalloc(sizeof(pll
));
309 pack_list_insert(&new_pll
->pl
, list
);
318 subset
= get_permutations(list
->next
, n
- 1);
320 new_pll
= xmalloc(sizeof(pll
));
321 new_pll
->pl
= subset
->pl
;
322 pack_list_insert(&new_pll
->pl
, list
);
325 subset
= subset
->next
;
332 static int is_superset(struct pack_list
*pl
, struct llist
*list
)
336 diff
= llist_copy(list
);
339 llist_sorted_difference_inplace(diff
, pl
->all_objects
);
340 if (diff
->size
== 0) { /* we're done */
350 static size_t sizeof_union(struct packed_git
*p1
, struct packed_git
*p2
)
354 void *p1_base
, *p2_base
;
356 p1_off
= p2_off
= 256 * 4 + 4;
357 p1_base
= (void *)p1
->index_base
;
358 p2_base
= (void *)p2
->index_base
;
360 while (p1_off
<= p1
->index_size
- 3 * 20 &&
361 p2_off
<= p2
->index_size
- 3 * 20)
363 int cmp
= memcmp(p1_base
+ p1_off
, p2_base
+ p2_off
, 20);
371 if (cmp
< 0) { /* p1 has the object, p2 doesn't */
373 } else { /* p2 has the object, p1 doesn't */
380 /* another O(n^2) function ... */
381 static size_t get_pack_redundancy(struct pack_list
*pl
)
383 struct pack_list
*subset
;
389 while ((subset
= pl
->next
)) {
391 ret
+= sizeof_union(pl
->pack
, subset
->pack
);
392 subset
= subset
->next
;
399 static inline size_t pack_set_bytecount(struct pack_list
*pl
)
403 ret
+= pl
->pack
->pack_size
;
404 ret
+= pl
->pack
->index_size
;
410 static void minimize(struct pack_list
**min
)
412 struct pack_list
*pl
, *unique
= NULL
,
413 *non_unique
= NULL
, *min_perm
= NULL
;
414 struct pll
*perm
, *perm_all
, *perm_ok
= NULL
, *new_perm
;
415 struct llist
*missing
;
416 size_t min_perm_size
= (size_t)-1, perm_size
;
421 if(pl
->unique_objects
->size
)
422 pack_list_insert(&unique
, pl
);
424 pack_list_insert(&non_unique
, pl
);
427 /* find out which objects are missing from the set of unique packs */
428 missing
= llist_copy(all_objects
);
431 llist_sorted_difference_inplace(missing
, pl
->all_objects
);
435 /* return if there are no objects missing from the unique set */
436 if (missing
->size
== 0) {
441 /* find the permutations which contain all missing objects */
442 for (n
= 1; n
<= pack_list_size(non_unique
) && !perm_ok
; n
++) {
443 perm_all
= perm
= get_permutations(non_unique
, n
);
445 if (is_superset(perm
->pl
, missing
)) {
446 new_perm
= xmalloc(sizeof(struct pll
));
447 memcpy(new_perm
, perm
, sizeof(struct pll
));
448 new_perm
->next
= perm_ok
;
458 die("Internal error: No complete sets found!\n");
460 /* find the permutation with the smallest size */
463 perm_size
= pack_set_bytecount(perm
->pl
);
464 if (min_perm_size
> perm_size
) {
465 min_perm_size
= perm_size
;
471 /* add the unique packs to the list */
474 pack_list_insert(min
, pl
);
479 static void load_all_objects(void)
481 struct pack_list
*pl
= local_packs
;
482 struct llist_item
*hint
, *l
;
484 llist_init(&all_objects
);
488 l
= pl
->all_objects
->front
;
490 hint
= llist_insert_sorted_unique(all_objects
,
496 /* remove objects present in remote packs */
499 llist_sorted_difference_inplace(all_objects
, pl
->all_objects
);
504 /* this scales like O(n^2) */
505 static void cmp_local_packs(void)
507 struct pack_list
*subset
, *pl
= local_packs
;
509 while ((subset
= pl
)) {
510 while((subset
= subset
->next
))
511 cmp_two_packs(pl
, subset
);
516 static void scan_alt_odb_packs(void)
518 struct pack_list
*local
, *alt
;
524 llist_sorted_difference_inplace(local
->unique_objects
,
528 llist_sorted_difference_inplace(all_objects
, alt
->all_objects
);
533 static struct pack_list
* add_pack(struct packed_git
*p
)
539 if (!p
->pack_local
&& !(alt_odb
|| verbose
))
543 llist_init(&l
.all_objects
);
546 base
= (void *)p
->index_base
;
547 while (off
<= p
->index_size
- 3 * 20) {
548 llist_insert_back(l
.all_objects
, base
+ off
);
551 /* this list will be pruned in cmp_two_packs later */
552 l
.unique_objects
= llist_copy(l
.all_objects
);
554 return pack_list_insert(&local_packs
, &l
);
556 return pack_list_insert(&altodb_packs
, &l
);
559 static struct pack_list
* add_pack_file(char *filename
)
561 struct packed_git
*p
= packed_git
;
563 if (strlen(filename
) < 40)
564 die("Bad pack filename: %s\n", filename
);
567 if (strstr(p
->pack_name
, filename
))
571 die("Filename %s not found in packed_git\n", filename
);
574 static void load_all(void)
576 struct packed_git
*p
= packed_git
;
584 int main(int argc
, char **argv
)
587 struct pack_list
*min
, *red
, *pl
;
588 struct llist
*ignore
;
590 char buf
[42]; /* 40 byte sha1 + \n + \0 */
592 setup_git_directory();
594 for (i
= 1; i
< argc
; i
++) {
595 const char *arg
= argv
[i
];
596 if(!strcmp(arg
, "--")) {
600 if(!strcmp(arg
, "--all")) {
604 if(!strcmp(arg
, "--verbose")) {
608 if(!strcmp(arg
, "--alt-odb")) {
613 usage(pack_redundant_usage
);
618 prepare_packed_git();
623 while (*(argv
+ i
) != NULL
)
624 add_pack_file(*(argv
+ i
++));
626 if (local_packs
== NULL
)
627 die("Zero packs found!\n");
633 scan_alt_odb_packs();
635 /* ignore objects given on stdin */
638 while (fgets(buf
, sizeof(buf
), stdin
)) {
640 if (get_sha1_hex(buf
, sha1
))
641 die("Bad sha1 on stdin: %s", buf
);
642 llist_insert_sorted_unique(ignore
, sha1
, NULL
);
645 llist_sorted_difference_inplace(all_objects
, ignore
);
648 llist_sorted_difference_inplace(pl
->unique_objects
, ignore
);
655 fprintf(stderr
, "There are %lu packs available in alt-odbs.\n",
656 (unsigned long)pack_list_size(altodb_packs
));
657 fprintf(stderr
, "The smallest (bytewise) set of packs is:\n");
660 fprintf(stderr
, "\t%s\n", pl
->pack
->pack_name
);
663 fprintf(stderr
, "containing %lu duplicate objects "
664 "with a total size of %lukb.\n",
665 (unsigned long)get_pack_redundancy(min
),
666 (unsigned long)pack_set_bytecount(min
)/1024);
667 fprintf(stderr
, "A total of %lu unique objects were considered.\n",
668 (unsigned long)all_objects
->size
);
669 fprintf(stderr
, "Redundant packs (with indexes):\n");
671 pl
= red
= pack_list_difference(local_packs
, min
);
674 sha1_pack_index_name(pl
->pack
->sha1
),
675 pl
->pack
->pack_name
);
679 fprintf(stderr
, "%luMB of redundant packs in total.\n",
680 (unsigned long)pack_set_bytecount(red
)/(1024*1024));