]>
Commit | Line | Data |
---|---|---|
c283ab21 LS |
1 | /* |
2 | * | |
3 | * Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se> | |
4 | * | |
5 | * This file is licensed under the GPL v2. | |
6 | * | |
7 | */ | |
8 | ||
c2e86add | 9 | #include "builtin.h" |
41771fa4 | 10 | #include "hex.h" |
a80d72db | 11 | #include "repository.h" |
4f39cd82 | 12 | #include "packfile.h" |
a80d72db | 13 | #include "object-store.h" |
c283ab21 | 14 | |
6d016c9c LS |
15 | #define BLKSIZE 512 |
16 | ||
9bc0f32c | 17 | static const char pack_redundant_usage[] = |
f6a8ef07 | 18 | "git pack-redundant [--verbose] [--alt-odb] (--all | <pack-filename>...)"; |
c283ab21 | 19 | |
96f1e58f | 20 | static int load_all_packs, verbose, alt_odb; |
c283ab21 LS |
21 | |
22 | struct llist_item { | |
23 | struct llist_item *next; | |
0e5e2284 | 24 | struct object_id oid; |
c283ab21 | 25 | }; |
bd22c904 | 26 | static struct llist { |
c283ab21 LS |
27 | struct llist_item *front; |
28 | struct llist_item *back; | |
29 | size_t size; | |
1c3039e8 | 30 | } *all_objects; /* all objects which must be present in local packfiles */ |
c283ab21 | 31 | |
bd22c904 | 32 | static struct pack_list { |
c283ab21 LS |
33 | struct pack_list *next; |
34 | struct packed_git *pack; | |
35 | struct llist *unique_objects; | |
4bc0cc12 | 36 | struct llist *remaining_objects; |
0e37abd2 | 37 | size_t all_objects_size; |
1c3039e8 | 38 | } *local_packs = NULL, *altodb_packs = NULL; |
c283ab21 | 39 | |
96f1e58f | 40 | static struct llist_item *free_nodes; |
60435f68 | 41 | |
6d016c9c LS |
42 | static inline void llist_item_put(struct llist_item *item) |
43 | { | |
44 | item->next = free_nodes; | |
45 | free_nodes = item; | |
46 | } | |
47 | ||
962554c6 | 48 | static inline struct llist_item *llist_item_get(void) |
60435f68 | 49 | { |
b2e4bafb | 50 | struct llist_item *new_item; |
60435f68 | 51 | if ( free_nodes ) { |
b2e4bafb | 52 | new_item = free_nodes; |
60435f68 | 53 | free_nodes = free_nodes->next; |
6d016c9c LS |
54 | } else { |
55 | int i = 1; | |
b2e4bafb | 56 | ALLOC_ARRAY(new_item, BLKSIZE); |
eeefa7c9 | 57 | for (; i < BLKSIZE; i++) |
b2e4bafb | 58 | llist_item_put(&new_item[i]); |
6d016c9c | 59 | } |
b2e4bafb | 60 | return new_item; |
60435f68 AR |
61 | } |
62 | ||
bd22c904 | 63 | static inline void llist_init(struct llist **list) |
c283ab21 LS |
64 | { |
65 | *list = xmalloc(sizeof(struct llist)); | |
66 | (*list)->front = (*list)->back = NULL; | |
67 | (*list)->size = 0; | |
68 | } | |
69 | ||
bd22c904 | 70 | static struct llist * llist_copy(struct llist *list) |
c283ab21 LS |
71 | { |
72 | struct llist *ret; | |
b2e4bafb | 73 | struct llist_item *new_item, *old_item, *prev; |
a6080a0a | 74 | |
c283ab21 LS |
75 | llist_init(&ret); |
76 | ||
77 | if ((ret->size = list->size) == 0) | |
78 | return ret; | |
79 | ||
b2e4bafb | 80 | new_item = ret->front = llist_item_get(); |
6390fe20 | 81 | new_item->oid = list->front->oid; |
c283ab21 | 82 | |
b2e4bafb BW |
83 | old_item = list->front->next; |
84 | while (old_item) { | |
85 | prev = new_item; | |
86 | new_item = llist_item_get(); | |
87 | prev->next = new_item; | |
6390fe20 | 88 | new_item->oid = old_item->oid; |
b2e4bafb | 89 | old_item = old_item->next; |
c283ab21 | 90 | } |
b2e4bafb BW |
91 | new_item->next = NULL; |
92 | ret->back = new_item; | |
a6080a0a | 93 | |
c283ab21 LS |
94 | return ret; |
95 | } | |
96 | ||
42873078 NP |
97 | static inline struct llist_item *llist_insert(struct llist *list, |
98 | struct llist_item *after, | |
0e5e2284 | 99 | const unsigned char *oid) |
c283ab21 | 100 | { |
b2e4bafb | 101 | struct llist_item *new_item = llist_item_get(); |
0e5e2284 | 102 | oidread(&new_item->oid, oid); |
b2e4bafb | 103 | new_item->next = NULL; |
c283ab21 | 104 | |
afe8a907 | 105 | if (after) { |
b2e4bafb BW |
106 | new_item->next = after->next; |
107 | after->next = new_item; | |
c283ab21 | 108 | if (after == list->back) |
b2e4bafb | 109 | list->back = new_item; |
c283ab21 LS |
110 | } else {/* insert in front */ |
111 | if (list->size == 0) | |
b2e4bafb | 112 | list->back = new_item; |
c283ab21 | 113 | else |
b2e4bafb BW |
114 | new_item->next = list->front; |
115 | list->front = new_item; | |
c283ab21 LS |
116 | } |
117 | list->size++; | |
b2e4bafb | 118 | return new_item; |
c283ab21 LS |
119 | } |
120 | ||
42873078 | 121 | static inline struct llist_item *llist_insert_back(struct llist *list, |
0e5e2284 | 122 | const unsigned char *oid) |
c283ab21 | 123 | { |
6390fe20 | 124 | return llist_insert(list, list->back, oid); |
c283ab21 LS |
125 | } |
126 | ||
42873078 | 127 | static inline struct llist_item *llist_insert_sorted_unique(struct llist *list, |
6390fe20 | 128 | const struct object_id *oid, struct llist_item *hint) |
c283ab21 LS |
129 | { |
130 | struct llist_item *prev = NULL, *l; | |
131 | ||
132 | l = (hint == NULL) ? list->front : hint; | |
133 | while (l) { | |
0e5e2284 | 134 | int cmp = oidcmp(&l->oid, oid); |
c283ab21 | 135 | if (cmp > 0) { /* we insert before this entry */ |
0e5e2284 | 136 | return llist_insert(list, prev, oid->hash); |
c283ab21 | 137 | } |
eeefa7c9 | 138 | if (!cmp) { /* already exists */ |
c283ab21 LS |
139 | return l; |
140 | } | |
141 | prev = l; | |
142 | l = l->next; | |
143 | } | |
144 | /* insert at the end */ | |
0e5e2284 | 145 | return llist_insert_back(list, oid->hash); |
c283ab21 LS |
146 | } |
147 | ||
c283ab21 | 148 | /* returns a pointer to an item in front of sha1 */ |
0e5e2284 | 149 | static inline struct llist_item * llist_sorted_remove(struct llist *list, const unsigned char *oid, struct llist_item *hint) |
c283ab21 LS |
150 | { |
151 | struct llist_item *prev, *l; | |
152 | ||
153 | redo_from_start: | |
154 | l = (hint == NULL) ? list->front : hint; | |
155 | prev = NULL; | |
156 | while (l) { | |
0e5e2284 | 157 | const int cmp = hashcmp(l->oid.hash, oid); |
c283ab21 LS |
158 | if (cmp > 0) /* not in list, since sorted */ |
159 | return prev; | |
eeefa7c9 | 160 | if (!cmp) { /* found */ |
afe8a907 | 161 | if (!prev) { |
c283ab21 LS |
162 | if (hint != NULL && hint != list->front) { |
163 | /* we don't know the previous element */ | |
164 | hint = NULL; | |
165 | goto redo_from_start; | |
166 | } | |
167 | list->front = l->next; | |
168 | } else | |
169 | prev->next = l->next; | |
170 | if (l == list->back) | |
171 | list->back = prev; | |
60435f68 | 172 | llist_item_put(l); |
c283ab21 LS |
173 | list->size--; |
174 | return prev; | |
175 | } | |
176 | prev = l; | |
177 | l = l->next; | |
178 | } | |
179 | return prev; | |
180 | } | |
181 | ||
1a41e743 | 182 | /* computes A\B */ |
bd22c904 | 183 | static void llist_sorted_difference_inplace(struct llist *A, |
1a41e743 LS |
184 | struct llist *B) |
185 | { | |
186 | struct llist_item *hint, *b; | |
187 | ||
188 | hint = NULL; | |
189 | b = B->front; | |
190 | ||
191 | while (b) { | |
0e5e2284 | 192 | hint = llist_sorted_remove(A, b->oid.hash, hint); |
1a41e743 LS |
193 | b = b->next; |
194 | } | |
195 | } | |
196 | ||
bd22c904 | 197 | static inline struct pack_list * pack_list_insert(struct pack_list **pl, |
c283ab21 LS |
198 | struct pack_list *entry) |
199 | { | |
200 | struct pack_list *p = xmalloc(sizeof(struct pack_list)); | |
201 | memcpy(p, entry, sizeof(struct pack_list)); | |
202 | p->next = *pl; | |
203 | *pl = p; | |
204 | return p; | |
205 | } | |
206 | ||
bd22c904 | 207 | static inline size_t pack_list_size(struct pack_list *pl) |
1c3039e8 LS |
208 | { |
209 | size_t ret = 0; | |
eeefa7c9 | 210 | while (pl) { |
1c3039e8 LS |
211 | ret++; |
212 | pl = pl->next; | |
213 | } | |
214 | return ret; | |
215 | } | |
216 | ||
d1ab1577 AR |
217 | static struct pack_list * pack_list_difference(const struct pack_list *A, |
218 | const struct pack_list *B) | |
c283ab21 | 219 | { |
d1ab1577 AR |
220 | struct pack_list *ret; |
221 | const struct pack_list *pl; | |
c283ab21 | 222 | |
afe8a907 | 223 | if (!A) |
c283ab21 LS |
224 | return NULL; |
225 | ||
226 | pl = B; | |
227 | while (pl != NULL) { | |
228 | if (A->pack == pl->pack) | |
229 | return pack_list_difference(A->next, B); | |
230 | pl = pl->next; | |
231 | } | |
232 | ret = xmalloc(sizeof(struct pack_list)); | |
233 | memcpy(ret, A, sizeof(struct pack_list)); | |
234 | ret->next = pack_list_difference(A->next, B); | |
235 | return ret; | |
236 | } | |
237 | ||
bd22c904 | 238 | static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2) |
c283ab21 | 239 | { |
a9bc372e | 240 | size_t p1_off = 0, p2_off = 0, p1_step, p2_step; |
42873078 | 241 | const unsigned char *p1_base, *p2_base; |
c283ab21 | 242 | struct llist_item *p1_hint = NULL, *p2_hint = NULL; |
00de6063 | 243 | const unsigned int hashsz = the_hash_algo->rawsz; |
1d7f171c | 244 | |
30111776 | 245 | if (!p1->unique_objects) |
4bc0cc12 | 246 | p1->unique_objects = llist_copy(p1->remaining_objects); |
30111776 | 247 | if (!p2->unique_objects) |
4bc0cc12 | 248 | p2->unique_objects = llist_copy(p2->remaining_objects); |
30111776 | 249 | |
42873078 NP |
250 | p1_base = p1->pack->index_data; |
251 | p2_base = p2->pack->index_data; | |
8c681e07 NP |
252 | p1_base += 256 * 4 + ((p1->pack->index_version < 2) ? 4 : 8); |
253 | p2_base += 256 * 4 + ((p2->pack->index_version < 2) ? 4 : 8); | |
00de6063 | 254 | p1_step = hashsz + ((p1->pack->index_version < 2) ? 4 : 0); |
255 | p2_step = hashsz + ((p2->pack->index_version < 2) ? 4 : 0); | |
c283ab21 | 256 | |
8c681e07 NP |
257 | while (p1_off < p1->pack->num_objects * p1_step && |
258 | p2_off < p2->pack->num_objects * p2_step) | |
c283ab21 | 259 | { |
33de80b1 | 260 | const int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off); |
c283ab21 LS |
261 | /* cmp ~ p1 - p2 */ |
262 | if (cmp == 0) { | |
263 | p1_hint = llist_sorted_remove(p1->unique_objects, | |
0e5e2284 | 264 | p1_base + p1_off, |
6390fe20 | 265 | p1_hint); |
c283ab21 | 266 | p2_hint = llist_sorted_remove(p2->unique_objects, |
0e5e2284 | 267 | p1_base + p1_off, |
6390fe20 | 268 | p2_hint); |
8c681e07 NP |
269 | p1_off += p1_step; |
270 | p2_off += p2_step; | |
c283ab21 LS |
271 | continue; |
272 | } | |
273 | if (cmp < 0) { /* p1 has the object, p2 doesn't */ | |
8c681e07 | 274 | p1_off += p1_step; |
c283ab21 | 275 | } else { /* p2 has the object, p1 doesn't */ |
8c681e07 | 276 | p2_off += p2_step; |
c283ab21 LS |
277 | } |
278 | } | |
279 | } | |
280 | ||
bd22c904 | 281 | static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2) |
c283ab21 LS |
282 | { |
283 | size_t ret = 0; | |
a9bc372e | 284 | size_t p1_off = 0, p2_off = 0, p1_step, p2_step; |
42873078 | 285 | const unsigned char *p1_base, *p2_base; |
00de6063 | 286 | const unsigned int hashsz = the_hash_algo->rawsz; |
c283ab21 | 287 | |
42873078 NP |
288 | p1_base = p1->index_data; |
289 | p2_base = p2->index_data; | |
8c681e07 NP |
290 | p1_base += 256 * 4 + ((p1->index_version < 2) ? 4 : 8); |
291 | p2_base += 256 * 4 + ((p2->index_version < 2) ? 4 : 8); | |
00de6063 | 292 | p1_step = hashsz + ((p1->index_version < 2) ? 4 : 0); |
293 | p2_step = hashsz + ((p2->index_version < 2) ? 4 : 0); | |
c283ab21 | 294 | |
8c681e07 NP |
295 | while (p1_off < p1->num_objects * p1_step && |
296 | p2_off < p2->num_objects * p2_step) | |
c283ab21 | 297 | { |
a89fccd2 | 298 | int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off); |
c283ab21 LS |
299 | /* cmp ~ p1 - p2 */ |
300 | if (cmp == 0) { | |
301 | ret++; | |
8c681e07 NP |
302 | p1_off += p1_step; |
303 | p2_off += p2_step; | |
c283ab21 LS |
304 | continue; |
305 | } | |
306 | if (cmp < 0) { /* p1 has the object, p2 doesn't */ | |
8c681e07 | 307 | p1_off += p1_step; |
c283ab21 | 308 | } else { /* p2 has the object, p1 doesn't */ |
8c681e07 | 309 | p2_off += p2_step; |
c283ab21 LS |
310 | } |
311 | } | |
312 | return ret; | |
313 | } | |
314 | ||
315 | /* another O(n^2) function ... */ | |
bd22c904 | 316 | static size_t get_pack_redundancy(struct pack_list *pl) |
c283ab21 LS |
317 | { |
318 | struct pack_list *subset; | |
bd22c904 | 319 | size_t ret = 0; |
1c3039e8 | 320 | |
afe8a907 | 321 | if (!pl) |
1c3039e8 LS |
322 | return 0; |
323 | ||
c283ab21 | 324 | while ((subset = pl->next)) { |
eeefa7c9 | 325 | while (subset) { |
c283ab21 LS |
326 | ret += sizeof_union(pl->pack, subset->pack); |
327 | subset = subset->next; | |
328 | } | |
329 | pl = pl->next; | |
330 | } | |
331 | return ret; | |
332 | } | |
333 | ||
c4001d92 | 334 | static inline off_t pack_set_bytecount(struct pack_list *pl) |
c283ab21 | 335 | { |
c4001d92 | 336 | off_t ret = 0; |
c283ab21 LS |
337 | while (pl) { |
338 | ret += pl->pack->pack_size; | |
339 | ret += pl->pack->index_size; | |
340 | pl = pl->next; | |
341 | } | |
342 | return ret; | |
343 | } | |
344 | ||
0e37abd2 | 345 | static int cmp_remaining_objects(const void *a, const void *b) |
3084a01e SC |
346 | { |
347 | struct pack_list *pl_a = *((struct pack_list **)a); | |
348 | struct pack_list *pl_b = *((struct pack_list **)b); | |
3084a01e | 349 | |
0e37abd2 JX |
350 | if (pl_a->remaining_objects->size == pl_b->remaining_objects->size) { |
351 | /* have the same remaining_objects, big pack first */ | |
352 | if (pl_a->all_objects_size == pl_b->all_objects_size) | |
353 | return 0; | |
354 | else if (pl_a->all_objects_size < pl_b->all_objects_size) | |
355 | return 1; | |
356 | else | |
357 | return -1; | |
358 | } else if (pl_a->remaining_objects->size < pl_b->remaining_objects->size) { | |
359 | /* sort by remaining objects, more objects first */ | |
3084a01e | 360 | return 1; |
0e37abd2 | 361 | } else { |
3084a01e | 362 | return -1; |
0e37abd2 | 363 | } |
3084a01e SC |
364 | } |
365 | ||
4bc0cc12 | 366 | /* Sort pack_list, greater size of remaining_objects first */ |
3084a01e SC |
367 | static void sort_pack_list(struct pack_list **pl) |
368 | { | |
369 | struct pack_list **ary, *p; | |
370 | int i; | |
371 | size_t n = pack_list_size(*pl); | |
372 | ||
373 | if (n < 2) | |
374 | return; | |
375 | ||
376 | /* prepare an array of packed_list for easier sorting */ | |
ca56dadb | 377 | CALLOC_ARRAY(ary, n); |
3084a01e SC |
378 | for (n = 0, p = *pl; p; p = p->next) |
379 | ary[n++] = p; | |
380 | ||
0e37abd2 | 381 | QSORT(ary, n, cmp_remaining_objects); |
3084a01e SC |
382 | |
383 | /* link them back again */ | |
384 | for (i = 0; i < n - 1; i++) | |
385 | ary[i]->next = ary[i + 1]; | |
386 | ary[n - 1]->next = NULL; | |
387 | *pl = ary[0]; | |
388 | ||
389 | free(ary); | |
390 | } | |
391 | ||
392 | ||
bd22c904 | 393 | static void minimize(struct pack_list **min) |
c283ab21 | 394 | { |
3084a01e SC |
395 | struct pack_list *pl, *unique = NULL, *non_unique = NULL; |
396 | struct llist *missing, *unique_pack_objects; | |
c283ab21 | 397 | |
1c3039e8 | 398 | pl = local_packs; |
c283ab21 | 399 | while (pl) { |
eeefa7c9 | 400 | if (pl->unique_objects->size) |
c283ab21 LS |
401 | pack_list_insert(&unique, pl); |
402 | else | |
403 | pack_list_insert(&non_unique, pl); | |
404 | pl = pl->next; | |
405 | } | |
406 | /* find out which objects are missing from the set of unique packs */ | |
407 | missing = llist_copy(all_objects); | |
408 | pl = unique; | |
409 | while (pl) { | |
4bc0cc12 | 410 | llist_sorted_difference_inplace(missing, pl->remaining_objects); |
c283ab21 LS |
411 | pl = pl->next; |
412 | } | |
413 | ||
3084a01e SC |
414 | *min = unique; |
415 | ||
1c3039e8 | 416 | /* return if there are no objects missing from the unique set */ |
c283ab21 | 417 | if (missing->size == 0) { |
077a34e0 | 418 | free(missing); |
c283ab21 LS |
419 | return; |
420 | } | |
421 | ||
3084a01e SC |
422 | unique_pack_objects = llist_copy(all_objects); |
423 | llist_sorted_difference_inplace(unique_pack_objects, missing); | |
424 | ||
425 | /* remove unique pack objects from the non_unique packs */ | |
426 | pl = non_unique; | |
eeefa7c9 | 427 | while (pl) { |
4bc0cc12 | 428 | llist_sorted_difference_inplace(pl->remaining_objects, unique_pack_objects); |
c283ab21 LS |
429 | pl = pl->next; |
430 | } | |
3084a01e SC |
431 | |
432 | while (non_unique) { | |
4bc0cc12 | 433 | /* sort the non_unique packs, greater size of remaining_objects first */ |
3084a01e | 434 | sort_pack_list(&non_unique); |
4bc0cc12 | 435 | if (non_unique->remaining_objects->size == 0) |
3084a01e SC |
436 | break; |
437 | ||
438 | pack_list_insert(min, non_unique); | |
439 | ||
4bc0cc12 JX |
440 | for (pl = non_unique->next; pl && pl->remaining_objects->size > 0; pl = pl->next) |
441 | llist_sorted_difference_inplace(pl->remaining_objects, non_unique->remaining_objects); | |
3084a01e SC |
442 | |
443 | non_unique = non_unique->next; | |
444 | } | |
c283ab21 LS |
445 | } |
446 | ||
bd22c904 | 447 | static void load_all_objects(void) |
c283ab21 | 448 | { |
1c3039e8 | 449 | struct pack_list *pl = local_packs; |
c283ab21 | 450 | struct llist_item *hint, *l; |
c283ab21 LS |
451 | |
452 | llist_init(&all_objects); | |
453 | ||
454 | while (pl) { | |
c283ab21 | 455 | hint = NULL; |
4bc0cc12 | 456 | l = pl->remaining_objects->front; |
c283ab21 LS |
457 | while (l) { |
458 | hint = llist_insert_sorted_unique(all_objects, | |
0e5e2284 | 459 | &l->oid, hint); |
c283ab21 LS |
460 | l = l->next; |
461 | } | |
462 | pl = pl->next; | |
463 | } | |
1c3039e8 LS |
464 | /* remove objects present in remote packs */ |
465 | pl = altodb_packs; | |
466 | while (pl) { | |
4bc0cc12 | 467 | llist_sorted_difference_inplace(all_objects, pl->remaining_objects); |
1c3039e8 LS |
468 | pl = pl->next; |
469 | } | |
c283ab21 LS |
470 | } |
471 | ||
472 | /* this scales like O(n^2) */ | |
bd22c904 | 473 | static void cmp_local_packs(void) |
c283ab21 | 474 | { |
1c3039e8 | 475 | struct pack_list *subset, *pl = local_packs; |
c283ab21 | 476 | |
06962323 JX |
477 | /* only one packfile */ |
478 | if (!pl->next) { | |
479 | llist_init(&pl->unique_objects); | |
480 | return; | |
481 | } | |
482 | ||
1c3039e8 | 483 | while ((subset = pl)) { |
eeefa7c9 | 484 | while ((subset = subset->next)) |
1c3039e8 LS |
485 | cmp_two_packs(pl, subset); |
486 | pl = pl->next; | |
487 | } | |
06a45c8c | 488 | } |
1c3039e8 | 489 | |
bd22c904 | 490 | static void scan_alt_odb_packs(void) |
06a45c8c LS |
491 | { |
492 | struct pack_list *local, *alt; | |
493 | ||
494 | alt = altodb_packs; | |
495 | while (alt) { | |
496 | local = local_packs; | |
497 | while (local) { | |
4bc0cc12 JX |
498 | llist_sorted_difference_inplace(local->remaining_objects, |
499 | alt->remaining_objects); | |
06a45c8c | 500 | local = local->next; |
1c3039e8 | 501 | } |
06a45c8c | 502 | alt = alt->next; |
c283ab21 LS |
503 | } |
504 | } | |
505 | ||
bd22c904 | 506 | static struct pack_list * add_pack(struct packed_git *p) |
c283ab21 LS |
507 | { |
508 | struct pack_list l; | |
a9bc372e | 509 | size_t off = 0, step; |
42873078 | 510 | const unsigned char *base; |
c283ab21 | 511 | |
06a45c8c LS |
512 | if (!p->pack_local && !(alt_odb || verbose)) |
513 | return NULL; | |
514 | ||
c283ab21 | 515 | l.pack = p; |
4bc0cc12 | 516 | llist_init(&l.remaining_objects); |
c283ab21 | 517 | |
eaa86770 | 518 | if (open_pack_index(p)) |
d079837e SP |
519 | return NULL; |
520 | ||
42873078 | 521 | base = p->index_data; |
8c681e07 | 522 | base += 256 * 4 + ((p->index_version < 2) ? 4 : 8); |
00de6063 | 523 | step = the_hash_algo->rawsz + ((p->index_version < 2) ? 4 : 0); |
8c681e07 | 524 | while (off < p->num_objects * step) { |
0e5e2284 | 525 | llist_insert_back(l.remaining_objects, base + off); |
8c681e07 | 526 | off += step; |
c283ab21 | 527 | } |
0e37abd2 | 528 | l.all_objects_size = l.remaining_objects->size; |
30111776 | 529 | l.unique_objects = NULL; |
1c3039e8 LS |
530 | if (p->pack_local) |
531 | return pack_list_insert(&local_packs, &l); | |
532 | else | |
06a45c8c | 533 | return pack_list_insert(&altodb_packs, &l); |
c283ab21 LS |
534 | } |
535 | ||
377d0276 | 536 | static struct pack_list * add_pack_file(const char *filename) |
c283ab21 | 537 | { |
454ea2e4 | 538 | struct packed_git *p = get_all_packs(the_repository); |
c283ab21 LS |
539 | |
540 | if (strlen(filename) < 40) | |
d7530708 | 541 | die("Bad pack filename: %s", filename); |
c283ab21 LS |
542 | |
543 | while (p) { | |
544 | if (strstr(p->pack_name, filename)) | |
c283ab21 LS |
545 | return add_pack(p); |
546 | p = p->next; | |
547 | } | |
d7530708 | 548 | die("Filename %s not found in packed_git", filename); |
c283ab21 LS |
549 | } |
550 | ||
bd22c904 | 551 | static void load_all(void) |
c283ab21 | 552 | { |
454ea2e4 | 553 | struct packed_git *p = get_all_packs(the_repository); |
c283ab21 LS |
554 | |
555 | while (p) { | |
1c3039e8 | 556 | add_pack(p); |
c283ab21 LS |
557 | p = p->next; |
558 | } | |
559 | } | |
560 | ||
377d0276 | 561 | int cmd_pack_redundant(int argc, const char **argv, const char *prefix) |
c283ab21 LS |
562 | { |
563 | int i; | |
c3b58472 | 564 | int i_still_use_this = 0; |
3084a01e | 565 | struct pack_list *min = NULL, *red, *pl; |
bb931cf9 | 566 | struct llist *ignore; |
6390fe20 | 567 | struct object_id *oid; |
568 | char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */ | |
c283ab21 | 569 | |
99caeed0 JN |
570 | if (argc == 2 && !strcmp(argv[1], "-h")) |
571 | usage(pack_redundant_usage); | |
572 | ||
c283ab21 LS |
573 | for (i = 1; i < argc; i++) { |
574 | const char *arg = argv[i]; | |
eeefa7c9 | 575 | if (!strcmp(arg, "--")) { |
9bc0f32c | 576 | i++; |
c283ab21 | 577 | break; |
9bc0f32c | 578 | } |
eeefa7c9 | 579 | if (!strcmp(arg, "--all")) { |
1c3039e8 | 580 | load_all_packs = 1; |
c283ab21 LS |
581 | continue; |
582 | } | |
eeefa7c9 | 583 | if (!strcmp(arg, "--verbose")) { |
c283ab21 LS |
584 | verbose = 1; |
585 | continue; | |
586 | } | |
eeefa7c9 | 587 | if (!strcmp(arg, "--alt-odb")) { |
1c3039e8 LS |
588 | alt_odb = 1; |
589 | continue; | |
590 | } | |
c3b58472 JH |
591 | if (!strcmp(arg, "--i-still-use-this")) { |
592 | i_still_use_this = 1; | |
593 | continue; | |
594 | } | |
eeefa7c9 | 595 | if (*arg == '-') |
9bc0f32c | 596 | usage(pack_redundant_usage); |
c283ab21 LS |
597 | else |
598 | break; | |
599 | } | |
600 | ||
c3b58472 JH |
601 | if (!i_still_use_this) { |
602 | fputs(_("'git pack-redundant' is nominated for removal.\n" | |
603 | "If you still use this command, please add an extra\n" | |
604 | "option, '--i-still-use-this', on the command line\n" | |
605 | "and let us know you still use it by sending an e-mail\n" | |
606 | "to <git@vger.kernel.org>. Thanks.\n"), stderr); | |
607 | } | |
608 | ||
1c3039e8 | 609 | if (load_all_packs) |
c283ab21 LS |
610 | load_all(); |
611 | else | |
612 | while (*(argv + i) != NULL) | |
613 | add_pack_file(*(argv + i++)); | |
614 | ||
afe8a907 | 615 | if (!local_packs) |
d7530708 | 616 | die("Zero packs found!"); |
c283ab21 | 617 | |
c283ab21 LS |
618 | load_all_objects(); |
619 | ||
06a45c8c LS |
620 | if (alt_odb) |
621 | scan_alt_odb_packs(); | |
622 | ||
bb931cf9 LS |
623 | /* ignore objects given on stdin */ |
624 | llist_init(&ignore); | |
625 | if (!isatty(0)) { | |
626 | while (fgets(buf, sizeof(buf), stdin)) { | |
6390fe20 | 627 | oid = xmalloc(sizeof(*oid)); |
628 | if (get_oid_hex(buf, oid)) | |
629 | die("Bad object ID on stdin: %s", buf); | |
630 | llist_insert_sorted_unique(ignore, oid, NULL); | |
bb931cf9 LS |
631 | } |
632 | } | |
633 | llist_sorted_difference_inplace(all_objects, ignore); | |
634 | pl = local_packs; | |
635 | while (pl) { | |
4bc0cc12 | 636 | llist_sorted_difference_inplace(pl->remaining_objects, ignore); |
bb931cf9 LS |
637 | pl = pl->next; |
638 | } | |
639 | ||
30111776 JX |
640 | cmp_local_packs(); |
641 | ||
c283ab21 | 642 | minimize(&min); |
06a45c8c | 643 | |
c283ab21 | 644 | if (verbose) { |
abacbe41 KR |
645 | fprintf(stderr, "There are %lu packs available in alt-odbs.\n", |
646 | (unsigned long)pack_list_size(altodb_packs)); | |
c283ab21 LS |
647 | fprintf(stderr, "The smallest (bytewise) set of packs is:\n"); |
648 | pl = min; | |
649 | while (pl) { | |
650 | fprintf(stderr, "\t%s\n", pl->pack->pack_name); | |
651 | pl = pl->next; | |
652 | } | |
abacbe41 KR |
653 | fprintf(stderr, "containing %lu duplicate objects " |
654 | "with a total size of %lukb.\n", | |
655 | (unsigned long)get_pack_redundancy(min), | |
656 | (unsigned long)pack_set_bytecount(min)/1024); | |
657 | fprintf(stderr, "A total of %lu unique objects were considered.\n", | |
658 | (unsigned long)all_objects->size); | |
c283ab21 LS |
659 | fprintf(stderr, "Redundant packs (with indexes):\n"); |
660 | } | |
1c3039e8 | 661 | pl = red = pack_list_difference(local_packs, min); |
c283ab21 LS |
662 | while (pl) { |
663 | printf("%s\n%s\n", | |
538b1523 | 664 | sha1_pack_index_name(pl->pack->hash), |
1c3039e8 | 665 | pl->pack->pack_name); |
c283ab21 LS |
666 | pl = pl->next; |
667 | } | |
bb931cf9 | 668 | if (verbose) |
b99a394c JH |
669 | fprintf(stderr, "%luMB of redundant packs in total.\n", |
670 | (unsigned long)pack_set_bytecount(red)/(1024*1024)); | |
c283ab21 LS |
671 | |
672 | return 0; | |
673 | } |