3 * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
5 * This file is licensed under the GPL v2.
11 static const char pack_redundant_usage
[] =
12 "git-pack-redundant [ --verbose ] [ --alt-odb ] < --all | <.pack filename> ...>";
14 int load_all_packs
= 0, verbose
= 0, alt_odb
= 0;
17 struct llist_item
*next
;
21 struct llist_item
*front
;
22 struct llist_item
*back
;
24 } *all_objects
; /* all objects which must be present in local packfiles */
27 struct pack_list
*next
;
28 struct packed_git
*pack
;
29 struct llist
*unique_objects
;
30 struct llist
*all_objects
;
31 } *local_packs
= NULL
, *altodb_packs
= NULL
;
38 inline void llist_free(struct llist
*list
)
40 while((list
->back
= list
->front
)) {
41 list
->front
= list
->front
->next
;
47 inline void llist_init(struct llist
**list
)
49 *list
= xmalloc(sizeof(struct llist
));
50 (*list
)->front
= (*list
)->back
= NULL
;
54 struct llist
* llist_copy(struct llist
*list
)
57 struct llist_item
*new, *old
, *prev
;
61 if ((ret
->size
= list
->size
) == 0)
64 new = ret
->front
= xmalloc(sizeof(struct llist_item
));
65 new->sha1
= list
->front
->sha1
;
67 old
= list
->front
->next
;
70 new = xmalloc(sizeof(struct llist_item
));
72 new->sha1
= old
->sha1
;
81 inline struct llist_item
* llist_insert(struct llist
*list
,
82 struct llist_item
*after
, char *sha1
)
84 struct llist_item
*new = xmalloc(sizeof(struct llist_item
));
89 new->next
= after
->next
;
91 if (after
== list
->back
)
93 } else {/* insert in front */
97 new->next
= list
->front
;
104 inline struct llist_item
* llist_insert_back(struct llist
*list
, char *sha1
)
106 return llist_insert(list
, list
->back
, sha1
);
109 inline struct llist_item
* llist_insert_sorted_unique(struct llist
*list
,
110 char *sha1
, struct llist_item
*hint
)
112 struct llist_item
*prev
= NULL
, *l
;
114 l
= (hint
== NULL
) ? list
->front
: hint
;
116 int cmp
= memcmp(l
->sha1
, sha1
, 20);
117 if (cmp
> 0) { /* we insert before this entry */
118 return llist_insert(list
, prev
, sha1
);
120 if(!cmp
) { /* already exists */
126 /* insert at the end */
127 return llist_insert_back(list
, sha1
);
131 void llist_sorted_difference_inplace(struct llist
*A
,
134 struct llist_item
*prev
, *a
, *b
, *x
;
139 while (a
!= NULL
&& b
!= NULL
) {
140 int cmp
= memcmp(a
->sha1
, b
->sha1
, 20);
145 a
= prev
->next
= a
->next
;
147 if (a
== NULL
) /* end of list */
162 /* returns a pointer to an item in front of sha1 */
163 inline struct llist_item
* llist_sorted_remove(struct llist
*list
, char *sha1
,
164 struct llist_item
*hint
)
166 struct llist_item
*prev
, *l
;
169 l
= (hint
== NULL
) ? list
->front
: hint
;
172 int cmp
= memcmp(l
->sha1
, sha1
, 20);
173 if (cmp
> 0) /* not in list, since sorted */
175 if(!cmp
) { /* found */
177 if (hint
!= NULL
&& hint
!= list
->front
) {
178 /* we don't know the previous element */
180 goto redo_from_start
;
182 list
->front
= l
->next
;
184 prev
->next
= l
->next
;
197 inline struct pack_list
* pack_list_insert(struct pack_list
**pl
,
198 struct pack_list
*entry
)
200 struct pack_list
*p
= xmalloc(sizeof(struct pack_list
));
201 memcpy(p
, entry
, sizeof(struct pack_list
));
207 inline size_t pack_list_size(struct pack_list
*pl
)
217 struct pack_list
* pack_list_difference(struct pack_list
*A
,
220 struct pack_list
*ret
, *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 void cmp_two_packs(struct pack_list
*p1
, struct pack_list
*p2
)
240 void *p1_base
, *p2_base
;
241 struct llist_item
*p1_hint
= NULL
, *p2_hint
= NULL
;
243 p1_off
= p2_off
= 256 * 4 + 4;
244 p1_base
= (void *)p1
->pack
->index_base
;
245 p2_base
= (void *)p2
->pack
->index_base
;
247 while (p1_off
<= p1
->pack
->index_size
- 3 * 20 &&
248 p2_off
<= p2
->pack
->index_size
- 3 * 20)
250 int cmp
= memcmp(p1_base
+ p1_off
, p2_base
+ p2_off
, 20);
253 p1_hint
= llist_sorted_remove(p1
->unique_objects
,
254 p1_base
+ p1_off
, p1_hint
);
255 p2_hint
= llist_sorted_remove(p2
->unique_objects
,
256 p1_base
+ p1_off
, p2_hint
);
261 if (cmp
< 0) { /* p1 has the object, p2 doesn't */
263 } else { /* p2 has the object, p1 doesn't */
269 /* all the permutations have to be free()d at the same time,
270 * since they refer to each other
272 struct pll
* get_all_permutations(struct pack_list
*list
)
274 struct pll
*subset
, *pll
, *new_pll
= NULL
; /*silence warning*/
279 if (list
->next
== NULL
) {
280 new_pll
= xmalloc(sizeof(struct pll
));
281 new_pll
->next
= NULL
;
286 pll
= subset
= get_all_permutations(list
->next
);
288 new_pll
= xmalloc(sizeof(struct pll
));
289 new_pll
->next
= pll
->next
;
292 new_pll
->pl
= xmalloc(sizeof(struct pack_list
));
293 memcpy(new_pll
->pl
, list
, sizeof(struct pack_list
));
294 new_pll
->pl
->next
= pll
->pl
;
298 /* add ourself to the end */
299 new_pll
->next
= xmalloc(sizeof(struct pll
));
300 new_pll
->next
->pl
= xmalloc(sizeof(struct pack_list
));
301 new_pll
->next
->next
= NULL
;
302 memcpy(new_pll
->next
->pl
, list
, sizeof(struct pack_list
));
303 new_pll
->next
->pl
->next
= NULL
;
308 int is_superset(struct pack_list
*pl
, struct llist
*list
)
312 diff
= llist_copy(list
);
315 llist_sorted_difference_inplace(diff
,
317 if (diff
->size
== 0) { /* we're done */
327 size_t sizeof_union(struct packed_git
*p1
, struct packed_git
*p2
)
331 void *p1_base
, *p2_base
;
333 p1_off
= p2_off
= 256 * 4 + 4;
334 p1_base
= (void *)p1
->index_base
;
335 p2_base
= (void *)p2
->index_base
;
337 while (p1_off
<= p1
->index_size
- 3 * 20 &&
338 p2_off
<= p2
->index_size
- 3 * 20)
340 int cmp
= memcmp(p1_base
+ p1_off
, p2_base
+ p2_off
, 20);
348 if (cmp
< 0) { /* p1 has the object, p2 doesn't */
350 } else { /* p2 has the object, p1 doesn't */
357 /* another O(n^2) function ... */
358 size_t get_pack_redundancy(struct pack_list
*pl
)
360 struct pack_list
*subset
;
366 while ((subset
= pl
->next
)) {
368 ret
+= sizeof_union(pl
->pack
, subset
->pack
);
369 subset
= subset
->next
;
376 inline size_t pack_set_bytecount(struct pack_list
*pl
)
380 ret
+= pl
->pack
->pack_size
;
381 ret
+= pl
->pack
->index_size
;
387 void minimize(struct pack_list
**min
)
389 struct pack_list
*pl
, *unique
= NULL
,
390 *non_unique
= NULL
, *min_perm
= NULL
;
391 struct pll
*perm
, *perm_all
, *perm_ok
= NULL
, *new_perm
;
392 struct llist
*missing
;
393 size_t min_perm_size
= (size_t)-1, perm_size
;
397 if(pl
->unique_objects
->size
)
398 pack_list_insert(&unique
, pl
);
400 pack_list_insert(&non_unique
, pl
);
403 /* find out which objects are missing from the set of unique packs */
404 missing
= llist_copy(all_objects
);
407 llist_sorted_difference_inplace(missing
,
412 /* return if there are no objects missing from the unique set */
413 if (missing
->size
== 0) {
418 /* find the permutations which contain all missing objects */
419 perm_all
= perm
= get_all_permutations(non_unique
);
421 if (is_superset(perm
->pl
, missing
)) {
422 new_perm
= xmalloc(sizeof(struct pll
));
423 new_perm
->pl
= perm
->pl
;
424 new_perm
->next
= perm_ok
;
431 die("Internal error: No complete sets found!\n");
433 /* find the permutation with the smallest size */
436 perm_size
= pack_set_bytecount(perm
->pl
);
437 if (min_perm_size
> perm_size
) {
438 min_perm_size
= perm_size
;
444 /* add the unique packs to the list */
447 pack_list_insert(min
, pl
);
452 void load_all_objects()
454 struct pack_list
*pl
= local_packs
;
455 struct llist_item
*hint
, *l
;
458 llist_init(&all_objects
);
463 l
= pl
->all_objects
->front
;
465 hint
= llist_insert_sorted_unique(all_objects
,
471 /* remove objects present in remote packs */
474 llist_sorted_difference_inplace(all_objects
, pl
->all_objects
);
479 /* this scales like O(n^2) */
482 struct pack_list
*subset
, *pl
= local_packs
;
484 while ((subset
= pl
)) {
485 while((subset
= subset
->next
))
486 cmp_two_packs(pl
, subset
);
492 subset
= local_packs
;
494 llist_sorted_difference_inplace(subset
->unique_objects
,
496 subset
= subset
->next
;
502 struct pack_list
* add_pack(struct packed_git
*p
)
509 llist_init(&l
.all_objects
);
512 base
= (void *)p
->index_base
;
513 while (off
<= p
->index_size
- 3 * 20) {
514 llist_insert_back(l
.all_objects
, base
+ off
);
517 /* this list will be pruned in cmp_two_packs later */
518 l
.unique_objects
= llist_copy(l
.all_objects
);
520 return pack_list_insert(&local_packs
, &l
);
522 return alt_odb
? pack_list_insert(&altodb_packs
, &l
) : NULL
;
525 struct pack_list
* add_pack_file(char *filename
)
527 struct packed_git
*p
= packed_git
;
529 if (strlen(filename
) < 40)
530 die("Bad pack filename: %s\n", filename
);
533 if (strstr(p
->pack_name
, filename
))
537 die("Filename %s not found in packed_git\n", filename
);
542 struct packed_git
*p
= packed_git
;
550 int main(int argc
, char **argv
)
553 struct pack_list
*min
, *red
, *pl
;
555 for (i
= 1; i
< argc
; i
++) {
556 const char *arg
= argv
[i
];
557 if(!strcmp(arg
, "--")) {
561 if(!strcmp(arg
, "--all")) {
565 if(!strcmp(arg
, "--verbose")) {
569 if(!strcmp(arg
, "--alt-odb")) {
574 usage(pack_redundant_usage
);
579 prepare_packed_git();
584 while (*(argv
+ i
) != NULL
)
585 add_pack_file(*(argv
+ i
++));
587 if (local_packs
== NULL
)
588 die("Zero packs found!\n");
596 fprintf(stderr
, "There are %ld packs available in alt-odbs.\n",
597 pack_list_size(altodb_packs
));
598 fprintf(stderr
, "The smallest (bytewise) set of packs is:\n");
601 fprintf(stderr
, "\t%s\n", pl
->pack
->pack_name
);
604 fprintf(stderr
, "containing %ld duplicate objects "
605 "with a total size of %ldkb.\n",
606 get_pack_redundancy(min
), pack_set_bytecount(min
)/1024);
607 fprintf(stderr
, "A total of %ld unique objects were considered.\n",
609 fprintf(stderr
, "Redundant packs (with indexes):\n");
611 pl
= red
= pack_list_difference(local_packs
, min
);
614 sha1_pack_index_name(pl
->pack
->sha1
),
615 pl
->pack
->pack_name
);