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 [ -v ] < -a | <.pack filename> ...>";
14 int all
= 0, verbose
= 0;
17 struct llist_item
*next
;
21 struct llist_item
*front
;
22 struct llist_item
*back
;
27 struct pack_list
*next
;
28 struct packed_git
*pack
;
29 struct llist
*unique_objects
;
30 struct llist
*all_objects
;
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 struct llist
* llist_sorted_difference(struct llist_item
*A
,
132 struct llist_item
*B
)
137 while (A
!= NULL
&& B
!= NULL
) {
138 int cmp
= memcmp(A
->sha1
, B
->sha1
, 20);
144 if(cmp
> 0) { /* we'll never find this B */
148 /* A has the object, B doesn't */
149 llist_insert_back(ret
, A
->sha1
);
153 llist_insert_back(ret
, A
->sha1
);
159 /* returns a pointer to an item in front of sha1 */
160 inline struct llist_item
* llist_sorted_remove(struct llist
*list
, char *sha1
,
161 struct llist_item
*hint
)
163 struct llist_item
*prev
, *l
;
166 l
= (hint
== NULL
) ? list
->front
: hint
;
169 int cmp
= memcmp(l
->sha1
, sha1
, 20);
170 if (cmp
> 0) /* not in list, since sorted */
172 if(!cmp
) { /* found */
174 if (hint
!= NULL
&& hint
!= list
->front
) {
175 /* we don't know the previous element */
177 goto redo_from_start
;
179 list
->front
= l
->next
;
181 prev
->next
= l
->next
;
194 inline struct pack_list
* pack_list_insert(struct pack_list
**pl
,
195 struct pack_list
*entry
)
197 struct pack_list
*p
= xmalloc(sizeof(struct pack_list
));
198 memcpy(p
, entry
, sizeof(struct pack_list
));
204 struct pack_list
* pack_list_difference(struct pack_list
*A
,
207 struct pack_list
*ret
, *pl
;
214 if (A
->pack
== pl
->pack
)
215 return pack_list_difference(A
->next
, B
);
218 ret
= xmalloc(sizeof(struct pack_list
));
219 memcpy(ret
, A
, sizeof(struct pack_list
));
220 ret
->next
= pack_list_difference(A
->next
, B
);
224 void cmp_two_packs(struct pack_list
*p1
, struct pack_list
*p2
)
227 void *p1_base
, *p2_base
;
228 struct llist_item
*p1_hint
= NULL
, *p2_hint
= NULL
;
230 p1_off
= p2_off
= 256 * 4 + 4;
231 p1_base
= (void *)p1
->pack
->index_base
;
232 p2_base
= (void *)p2
->pack
->index_base
;
234 while (p1_off
<= p1
->pack
->index_size
- 3 * 20 &&
235 p2_off
<= p2
->pack
->index_size
- 3 * 20)
237 int cmp
= memcmp(p1_base
+ p1_off
, p2_base
+ p2_off
, 20);
240 p1_hint
= llist_sorted_remove(p1
->unique_objects
,
241 p1_base
+ p1_off
, p1_hint
);
242 p2_hint
= llist_sorted_remove(p2
->unique_objects
,
243 p1_base
+ p1_off
, p2_hint
);
248 if (cmp
< 0) { /* p1 has the object, p2 doesn't */
250 } else { /* p2 has the object, p1 doesn't */
256 /* all the permutations have to be free()d at the same time,
257 * since they refer to each other
259 struct pll
* get_all_permutations(struct pack_list
*list
)
261 struct pll
*subset
, *pll
, *new_pll
= NULL
; /*silence warning*/
266 if (list
->next
== NULL
) {
267 new_pll
= xmalloc(sizeof(struct pll
));
268 new_pll
->next
= NULL
;
273 pll
= subset
= get_all_permutations(list
->next
);
275 new_pll
= xmalloc(sizeof(struct pll
));
276 new_pll
->next
= pll
->next
;
279 new_pll
->pl
= xmalloc(sizeof(struct pack_list
));
280 memcpy(new_pll
->pl
, list
, sizeof(struct pack_list
));
281 new_pll
->pl
->next
= pll
->pl
;
285 /* add ourself to the end */
286 new_pll
->next
= xmalloc(sizeof(struct pll
));
287 new_pll
->next
->pl
= xmalloc(sizeof(struct pack_list
));
288 new_pll
->next
->next
= NULL
;
289 memcpy(new_pll
->next
->pl
, list
, sizeof(struct pack_list
));
290 new_pll
->next
->pl
->next
= NULL
;
295 int is_superset(struct pack_list
*pl
, struct llist
*list
)
297 struct llist
*diff
, *old
;
299 diff
= llist_copy(list
);
303 diff
= llist_sorted_difference(diff
->front
,
304 pl
->all_objects
->front
);
306 if (diff
->size
== 0) { /* we're done */
316 size_t sizeof_union(struct packed_git
*p1
, struct packed_git
*p2
)
320 void *p1_base
, *p2_base
;
322 p1_off
= p2_off
= 256 * 4 + 4;
323 p1_base
= (void *)p1
->index_base
;
324 p2_base
= (void *)p2
->index_base
;
326 while (p1_off
<= p1
->index_size
- 3 * 20 &&
327 p2_off
<= p2
->index_size
- 3 * 20)
329 int cmp
= memcmp(p1_base
+ p1_off
, p2_base
+ p2_off
, 20);
337 if (cmp
< 0) { /* p1 has the object, p2 doesn't */
339 } else { /* p2 has the object, p1 doesn't */
346 /* another O(n^2) function ... */
347 size_t get_pack_redundancy(struct pack_list
*pl
)
349 struct pack_list
*subset
;
351 while ((subset
= pl
->next
)) {
353 ret
+= sizeof_union(pl
->pack
, subset
->pack
);
354 subset
= subset
->next
;
361 inline size_t pack_set_bytecount(struct pack_list
*pl
)
365 ret
+= pl
->pack
->pack_size
;
366 ret
+= pl
->pack
->index_size
;
372 void minimize(struct pack_list
**min
)
374 struct pack_list
*pl
, *unique
= NULL
,
375 *non_unique
= NULL
, *min_perm
= NULL
;
376 struct pll
*perm
, *perm_all
, *perm_ok
= NULL
, *new_perm
;
377 struct llist
*missing
, *old
;
378 size_t min_perm_size
= (size_t)-1, perm_size
;
382 if(pl
->unique_objects
->size
)
383 pack_list_insert(&unique
, pl
);
385 pack_list_insert(&non_unique
, pl
);
388 /* find out which objects are missing from the set of unique packs */
389 missing
= llist_copy(all_objects
);
393 missing
= llist_sorted_difference(missing
->front
,
394 pl
->all_objects
->front
);
399 if (missing
->size
== 0) {
404 /* find the permutations which contain all missing objects */
405 perm_all
= perm
= get_all_permutations(non_unique
);
407 if (is_superset(perm
->pl
, missing
)) {
408 new_perm
= xmalloc(sizeof(struct pll
));
409 new_perm
->pl
= perm
->pl
;
410 new_perm
->next
= perm_ok
;
417 die("Internal error: No complete sets found!\n");
419 /* find the permutation with the smallest size */
422 perm_size
= pack_set_bytecount(perm
->pl
);
423 if (min_perm_size
> perm_size
) {
424 min_perm_size
= perm_size
;
430 /* add the unique packs to the list */
433 pack_list_insert(min
, pl
);
438 void load_all_objects()
440 struct pack_list
*pl
= pack_list
;
441 struct llist_item
*hint
, *l
;
444 llist_init(&all_objects
);
449 l
= pl
->all_objects
->front
;
451 hint
= llist_insert_sorted_unique(all_objects
,
459 /* this scales like O(n^2) */
462 struct pack_list
*subset
, *curr
= pack_list
;
464 while ((subset
= curr
)) {
465 while((subset
= subset
->next
))
466 cmp_two_packs(curr
, subset
);
471 struct pack_list
* add_pack(struct packed_git
*p
)
478 llist_init(&l
.all_objects
);
481 base
= (void *)p
->index_base
;
482 while (off
<= p
->index_size
- 3 * 20) {
483 llist_insert_back(l
.all_objects
, base
+ off
);
486 /* this list will be pruned in cmp_two_packs later */
487 l
.unique_objects
= llist_copy(l
.all_objects
);
488 return pack_list_insert(&pack_list
, &l
);
491 struct pack_list
* add_pack_file(char *filename
)
493 struct packed_git
*p
= packed_git
;
495 if (strlen(filename
) < 40)
496 die("Bad pack filename: %s\n", filename
);
499 if (strstr(p
->pack_name
, filename
))
500 /* this will silently ignore packs in alt-odb */
504 die("Filename %s not found in packed_git\n", filename
);
509 struct packed_git
*p
= packed_git
;
512 if (p
->pack_local
) /* ignore alt-odb for now */
518 int main(int argc
, char **argv
)
521 struct pack_list
*min
, *red
, *pl
;
523 for (i
= 1; i
< argc
; i
++) {
524 const char *arg
= argv
[i
];
525 if(!strcmp(arg
, "--")) {
529 if(!strcmp(arg
, "-a")) {
533 if(!strcmp(arg
, "-v")) {
538 usage(pack_redundant_usage
);
543 prepare_packed_git();
548 while (*(argv
+ i
) != NULL
)
549 add_pack_file(*(argv
+ i
++));
551 if (pack_list
== NULL
)
552 die("Zero packs found!\n");
560 fprintf(stderr
, "The smallest (bytewise) set of packs is:\n");
563 fprintf(stderr
, "\t%s\n", pl
->pack
->pack_name
);
566 fprintf(stderr
, "containing %ld duplicate objects "
567 "with a total size of %ldkb.\n",
568 get_pack_redundancy(min
), pack_set_bytecount(min
)/1024);
569 fprintf(stderr
, "Redundant packs (with indexes):\n");
571 pl
= red
= pack_list_difference(pack_list
, min
);
574 sha1_pack_index_name(pl
->pack
->sha1
), pl
->pack
->pack_name
);