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