blob: 41e1615a28d772d1677c172ff2f570f31de4026f [file] [log] [blame]
Lukas_Sandströmc283ab22005-11-09 02:22:40 +01001/*
2*
3* Copyright 2005, Lukas Sandstrom <lukass@etek.chalmers.se>
4*
5* This file is licensed under the GPL v2.
6*
7*/
8
9#include "cache.h"
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +010010#include "exec_cmd.h"
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010011
Lukas Sandström6d016c92005-12-20 21:05:54 +010012#define BLKSIZE 512
13
Lukas_Sandström9bc0f322005-11-10 00:16:13 +010014static const char pack_redundant_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020015"git pack-redundant [ --verbose ] [ --alt-odb ] < --all | <.pack filename> ...>";
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010016
David Rientjes96f1e582006-08-15 10:23:48 -070017static int load_all_packs, verbose, alt_odb;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010018
19struct llist_item {
20 struct llist_item *next;
Nicolas Pitre42873072007-03-16 16:42:50 -040021 const unsigned char *sha1;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010022};
Timo Hirvonenbd22c902005-11-21 02:52:52 +020023static struct llist {
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010024 struct llist_item *front;
25 struct llist_item *back;
26 size_t size;
Lukas_Sandström1c3039e2005-11-11 01:25:04 +010027} *all_objects; /* all objects which must be present in local packfiles */
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010028
Timo Hirvonenbd22c902005-11-21 02:52:52 +020029static struct pack_list {
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010030 struct pack_list *next;
31 struct packed_git *pack;
32 struct llist *unique_objects;
33 struct llist *all_objects;
Lukas_Sandström1c3039e2005-11-11 01:25:04 +010034} *local_packs = NULL, *altodb_packs = NULL;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010035
36struct pll {
37 struct pll *next;
38 struct pack_list *pl;
39};
40
David Rientjes96f1e582006-08-15 10:23:48 -070041static struct llist_item *free_nodes;
Alex Riesen60435f62005-11-22 15:56:35 +010042
Lukas Sandström6d016c92005-12-20 21:05:54 +010043static inline void llist_item_put(struct llist_item *item)
44{
45 item->next = free_nodes;
46 free_nodes = item;
47}
48
Timo Hirvonen962554c2006-02-26 17:13:46 +020049static inline struct llist_item *llist_item_get(void)
Alex Riesen60435f62005-11-22 15:56:35 +010050{
51 struct llist_item *new;
52 if ( free_nodes ) {
53 new = free_nodes;
54 free_nodes = free_nodes->next;
Lukas Sandström6d016c92005-12-20 21:05:54 +010055 } else {
56 int i = 1;
57 new = xmalloc(sizeof(struct llist_item) * BLKSIZE);
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -040058 for (; i < BLKSIZE; i++)
Lukas Sandström6d016c92005-12-20 21:05:54 +010059 llist_item_put(&new[i]);
Lukas Sandström6d016c92005-12-20 21:05:54 +010060 }
Alex Riesen60435f62005-11-22 15:56:35 +010061 return new;
62}
63
Alex Riesen60435f62005-11-22 15:56:35 +010064static void llist_free(struct llist *list)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010065{
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -040066 while ((list->back = list->front)) {
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010067 list->front = list->front->next;
Alex Riesen60435f62005-11-22 15:56:35 +010068 llist_item_put(list->back);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010069 }
70 free(list);
71}
72
Timo Hirvonenbd22c902005-11-21 02:52:52 +020073static inline void llist_init(struct llist **list)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010074{
75 *list = xmalloc(sizeof(struct llist));
76 (*list)->front = (*list)->back = NULL;
77 (*list)->size = 0;
78}
79
Timo Hirvonenbd22c902005-11-21 02:52:52 +020080static struct llist * llist_copy(struct llist *list)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010081{
82 struct llist *ret;
83 struct llist_item *new, *old, *prev;
Junio C Hamanoa6080a02007-06-07 00:04:01 -070084
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010085 llist_init(&ret);
86
87 if ((ret->size = list->size) == 0)
88 return ret;
89
Alex Riesen60435f62005-11-22 15:56:35 +010090 new = ret->front = llist_item_get();
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010091 new->sha1 = list->front->sha1;
92
93 old = list->front->next;
94 while (old) {
95 prev = new;
Alex Riesen60435f62005-11-22 15:56:35 +010096 new = llist_item_get();
Lukas_Sandströmc283ab22005-11-09 02:22:40 +010097 prev->next = new;
98 new->sha1 = old->sha1;
99 old = old->next;
100 }
101 new->next = NULL;
102 ret->back = new;
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700103
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100104 return ret;
105}
106
Nicolas Pitre42873072007-03-16 16:42:50 -0400107static inline struct llist_item *llist_insert(struct llist *list,
108 struct llist_item *after,
109 const unsigned char *sha1)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100110{
Alex Riesen60435f62005-11-22 15:56:35 +0100111 struct llist_item *new = llist_item_get();
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100112 new->sha1 = sha1;
113 new->next = NULL;
114
115 if (after != NULL) {
116 new->next = after->next;
117 after->next = new;
118 if (after == list->back)
119 list->back = new;
120 } else {/* insert in front */
121 if (list->size == 0)
122 list->back = new;
123 else
124 new->next = list->front;
125 list->front = new;
126 }
127 list->size++;
128 return new;
129}
130
Nicolas Pitre42873072007-03-16 16:42:50 -0400131static inline struct llist_item *llist_insert_back(struct llist *list,
132 const unsigned char *sha1)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100133{
134 return llist_insert(list, list->back, sha1);
135}
136
Nicolas Pitre42873072007-03-16 16:42:50 -0400137static inline struct llist_item *llist_insert_sorted_unique(struct llist *list,
138 const unsigned char *sha1, struct llist_item *hint)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100139{
140 struct llist_item *prev = NULL, *l;
141
142 l = (hint == NULL) ? list->front : hint;
143 while (l) {
David Rientjesa89fccd2006-08-17 11:54:57 -0700144 int cmp = hashcmp(l->sha1, sha1);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100145 if (cmp > 0) { /* we insert before this entry */
146 return llist_insert(list, prev, sha1);
147 }
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400148 if (!cmp) { /* already exists */
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100149 return l;
150 }
151 prev = l;
152 l = l->next;
153 }
154 /* insert at the end */
155 return llist_insert_back(list, sha1);
156}
157
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100158/* returns a pointer to an item in front of sha1 */
Junio C Hamanob99a3942005-11-23 16:08:36 -0800159static inline struct llist_item * llist_sorted_remove(struct llist *list, const unsigned char *sha1, struct llist_item *hint)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100160{
161 struct llist_item *prev, *l;
162
163redo_from_start:
164 l = (hint == NULL) ? list->front : hint;
165 prev = NULL;
166 while (l) {
David Rientjesa89fccd2006-08-17 11:54:57 -0700167 int cmp = hashcmp(l->sha1, sha1);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100168 if (cmp > 0) /* not in list, since sorted */
169 return prev;
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400170 if (!cmp) { /* found */
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100171 if (prev == NULL) {
172 if (hint != NULL && hint != list->front) {
173 /* we don't know the previous element */
174 hint = NULL;
175 goto redo_from_start;
176 }
177 list->front = l->next;
178 } else
179 prev->next = l->next;
180 if (l == list->back)
181 list->back = prev;
Alex Riesen60435f62005-11-22 15:56:35 +0100182 llist_item_put(l);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100183 list->size--;
184 return prev;
185 }
186 prev = l;
187 l = l->next;
188 }
189 return prev;
190}
191
Lukas Sandström1a41e742005-11-15 22:24:02 +0100192/* computes A\B */
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200193static void llist_sorted_difference_inplace(struct llist *A,
Lukas Sandström1a41e742005-11-15 22:24:02 +0100194 struct llist *B)
195{
196 struct llist_item *hint, *b;
197
198 hint = NULL;
199 b = B->front;
200
201 while (b) {
202 hint = llist_sorted_remove(A, b->sha1, hint);
203 b = b->next;
204 }
205}
206
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200207static inline struct pack_list * pack_list_insert(struct pack_list **pl,
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100208 struct pack_list *entry)
209{
210 struct pack_list *p = xmalloc(sizeof(struct pack_list));
211 memcpy(p, entry, sizeof(struct pack_list));
212 p->next = *pl;
213 *pl = p;
214 return p;
215}
216
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200217static inline size_t pack_list_size(struct pack_list *pl)
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100218{
219 size_t ret = 0;
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400220 while (pl) {
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100221 ret++;
222 pl = pl->next;
223 }
224 return ret;
225}
226
Alex Riesend1ab1572005-11-22 15:59:22 +0100227static struct pack_list * pack_list_difference(const struct pack_list *A,
228 const struct pack_list *B)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100229{
Alex Riesend1ab1572005-11-22 15:59:22 +0100230 struct pack_list *ret;
231 const struct pack_list *pl;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100232
233 if (A == NULL)
234 return NULL;
235
236 pl = B;
237 while (pl != NULL) {
238 if (A->pack == pl->pack)
239 return pack_list_difference(A->next, B);
240 pl = pl->next;
241 }
242 ret = xmalloc(sizeof(struct pack_list));
243 memcpy(ret, A, sizeof(struct pack_list));
244 ret->next = pack_list_difference(A->next, B);
245 return ret;
246}
247
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200248static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100249{
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400250 unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step;
Nicolas Pitre42873072007-03-16 16:42:50 -0400251 const unsigned char *p1_base, *p2_base;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100252 struct llist_item *p1_hint = NULL, *p2_hint = NULL;
Florian Forster1d7f1712006-06-18 17:18:09 +0200253
Nicolas Pitre42873072007-03-16 16:42:50 -0400254 p1_base = p1->pack->index_data;
255 p2_base = p2->pack->index_data;
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400256 p1_base += 256 * 4 + ((p1->pack->index_version < 2) ? 4 : 8);
257 p2_base += 256 * 4 + ((p2->pack->index_version < 2) ? 4 : 8);
258 p1_step = (p1->pack->index_version < 2) ? 24 : 20;
259 p2_step = (p2->pack->index_version < 2) ? 24 : 20;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100260
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400261 while (p1_off < p1->pack->num_objects * p1_step &&
262 p2_off < p2->pack->num_objects * p2_step)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100263 {
David Rientjesa89fccd2006-08-17 11:54:57 -0700264 int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100265 /* cmp ~ p1 - p2 */
266 if (cmp == 0) {
267 p1_hint = llist_sorted_remove(p1->unique_objects,
268 p1_base + p1_off, p1_hint);
269 p2_hint = llist_sorted_remove(p2->unique_objects,
270 p1_base + p1_off, p2_hint);
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400271 p1_off += p1_step;
272 p2_off += p2_step;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100273 continue;
274 }
275 if (cmp < 0) { /* p1 has the object, p2 doesn't */
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400276 p1_off += p1_step;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100277 } else { /* p2 has the object, p1 doesn't */
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400278 p2_off += p2_step;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100279 }
280 }
281}
282
Timo Hirvonen962554c2006-02-26 17:13:46 +0200283static void pll_free(struct pll *l)
Lukas Sandström751a71e2005-11-17 14:11:56 +0100284{
Lukas Sandström6d016c92005-12-20 21:05:54 +0100285 struct pll *old;
286 struct pack_list *opl;
Lukas Sandström751a71e2005-11-17 14:11:56 +0100287
Lukas Sandström6d016c92005-12-20 21:05:54 +0100288 while (l) {
289 old = l;
290 while (l->pl) {
291 opl = l->pl;
292 l->pl = opl->next;
293 free(opl);
Lukas Sandström751a71e2005-11-17 14:11:56 +0100294 }
Lukas Sandström6d016c92005-12-20 21:05:54 +0100295 l = l->next;
296 free(old);
Lukas Sandström751a71e2005-11-17 14:11:56 +0100297 }
Lukas Sandström751a71e2005-11-17 14:11:56 +0100298}
299
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100300/* all the permutations have to be free()d at the same time,
301 * since they refer to each other
302 */
Lukas Sandström6d016c92005-12-20 21:05:54 +0100303static struct pll * get_permutations(struct pack_list *list, int n)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100304{
Lukas Sandström6d016c92005-12-20 21:05:54 +0100305 struct pll *subset, *ret = NULL, *new_pll = NULL, *pll;
306
307 if (list == NULL || pack_list_size(list) < n || n == 0)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100308 return NULL;
309
Lukas Sandström6d016c92005-12-20 21:05:54 +0100310 if (n == 1) {
311 while (list) {
312 new_pll = xmalloc(sizeof(pll));
313 new_pll->pl = NULL;
314 pack_list_insert(&new_pll->pl, list);
315 new_pll->next = ret;
316 ret = new_pll;
317 list = list->next;
Lukas Sandström751a71e2005-11-17 14:11:56 +0100318 }
Lukas Sandström6d016c92005-12-20 21:05:54 +0100319 return ret;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100320 }
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100321
Lukas Sandström6d016c92005-12-20 21:05:54 +0100322 while (list->next) {
323 subset = get_permutations(list->next, n - 1);
324 while (subset) {
325 new_pll = xmalloc(sizeof(pll));
326 new_pll->pl = subset->pl;
327 pack_list_insert(&new_pll->pl, list);
328 new_pll->next = ret;
329 ret = new_pll;
330 subset = subset->next;
331 }
332 list = list->next;
333 }
334 return ret;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100335}
336
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200337static int is_superset(struct pack_list *pl, struct llist *list)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100338{
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100339 struct llist *diff;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100340
341 diff = llist_copy(list);
342
343 while (pl) {
Alex Riesend1ab1572005-11-22 15:59:22 +0100344 llist_sorted_difference_inplace(diff, pl->all_objects);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100345 if (diff->size == 0) { /* we're done */
346 llist_free(diff);
347 return 1;
348 }
349 pl = pl->next;
350 }
351 llist_free(diff);
352 return 0;
353}
354
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200355static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100356{
357 size_t ret = 0;
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400358 unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step;
Nicolas Pitre42873072007-03-16 16:42:50 -0400359 const unsigned char *p1_base, *p2_base;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100360
Nicolas Pitre42873072007-03-16 16:42:50 -0400361 p1_base = p1->index_data;
362 p2_base = p2->index_data;
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400363 p1_base += 256 * 4 + ((p1->index_version < 2) ? 4 : 8);
364 p2_base += 256 * 4 + ((p2->index_version < 2) ? 4 : 8);
365 p1_step = (p1->index_version < 2) ? 24 : 20;
366 p2_step = (p2->index_version < 2) ? 24 : 20;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100367
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400368 while (p1_off < p1->num_objects * p1_step &&
369 p2_off < p2->num_objects * p2_step)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100370 {
David Rientjesa89fccd2006-08-17 11:54:57 -0700371 int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100372 /* cmp ~ p1 - p2 */
373 if (cmp == 0) {
374 ret++;
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400375 p1_off += p1_step;
376 p2_off += p2_step;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100377 continue;
378 }
379 if (cmp < 0) { /* p1 has the object, p2 doesn't */
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400380 p1_off += p1_step;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100381 } else { /* p2 has the object, p1 doesn't */
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400382 p2_off += p2_step;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100383 }
384 }
385 return ret;
386}
387
388/* another O(n^2) function ... */
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200389static size_t get_pack_redundancy(struct pack_list *pl)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100390{
391 struct pack_list *subset;
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200392 size_t ret = 0;
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100393
394 if (pl == NULL)
395 return 0;
396
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100397 while ((subset = pl->next)) {
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400398 while (subset) {
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100399 ret += sizeof_union(pl->pack, subset->pack);
400 subset = subset->next;
401 }
402 pl = pl->next;
403 }
404 return ret;
405}
406
Shawn O. Pearcec4001d92007-03-06 20:44:30 -0500407static inline off_t pack_set_bytecount(struct pack_list *pl)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100408{
Shawn O. Pearcec4001d92007-03-06 20:44:30 -0500409 off_t ret = 0;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100410 while (pl) {
411 ret += pl->pack->pack_size;
412 ret += pl->pack->index_size;
413 pl = pl->next;
414 }
415 return ret;
416}
417
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200418static void minimize(struct pack_list **min)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100419{
420 struct pack_list *pl, *unique = NULL,
421 *non_unique = NULL, *min_perm = NULL;
422 struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm;
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100423 struct llist *missing;
Shawn O. Pearcec4001d92007-03-06 20:44:30 -0500424 off_t min_perm_size = 0, perm_size;
Lukas Sandström6d016c92005-12-20 21:05:54 +0100425 int n;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100426
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100427 pl = local_packs;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100428 while (pl) {
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400429 if (pl->unique_objects->size)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100430 pack_list_insert(&unique, pl);
431 else
432 pack_list_insert(&non_unique, pl);
433 pl = pl->next;
434 }
435 /* find out which objects are missing from the set of unique packs */
436 missing = llist_copy(all_objects);
437 pl = unique;
438 while (pl) {
Lukas Sandström6d016c92005-12-20 21:05:54 +0100439 llist_sorted_difference_inplace(missing, pl->all_objects);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100440 pl = pl->next;
441 }
442
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100443 /* return if there are no objects missing from the unique set */
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100444 if (missing->size == 0) {
445 *min = unique;
446 return;
447 }
448
449 /* find the permutations which contain all missing objects */
Lukas Sandström6d016c92005-12-20 21:05:54 +0100450 for (n = 1; n <= pack_list_size(non_unique) && !perm_ok; n++) {
451 perm_all = perm = get_permutations(non_unique, n);
452 while (perm) {
453 if (is_superset(perm->pl, missing)) {
454 new_perm = xmalloc(sizeof(struct pll));
455 memcpy(new_perm, perm, sizeof(struct pll));
456 new_perm->next = perm_ok;
457 perm_ok = new_perm;
458 }
459 perm = perm->next;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100460 }
Lukas Sandström6d016c92005-12-20 21:05:54 +0100461 if (perm_ok)
462 break;
463 pll_free(perm_all);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100464 }
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100465 if (perm_ok == NULL)
Alexander Potashevd7530702009-01-04 21:38:41 +0300466 die("Internal error: No complete sets found!");
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100467
468 /* find the permutation with the smallest size */
469 perm = perm_ok;
470 while (perm) {
471 perm_size = pack_set_bytecount(perm->pl);
Shawn O. Pearcec4001d92007-03-06 20:44:30 -0500472 if (!min_perm_size || min_perm_size > perm_size) {
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100473 min_perm_size = perm_size;
474 min_perm = perm->pl;
475 }
476 perm = perm->next;
477 }
478 *min = min_perm;
479 /* add the unique packs to the list */
480 pl = unique;
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400481 while (pl) {
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100482 pack_list_insert(min, pl);
483 pl = pl->next;
484 }
485}
486
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200487static void load_all_objects(void)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100488{
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100489 struct pack_list *pl = local_packs;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100490 struct llist_item *hint, *l;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100491
492 llist_init(&all_objects);
493
494 while (pl) {
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100495 hint = NULL;
496 l = pl->all_objects->front;
497 while (l) {
498 hint = llist_insert_sorted_unique(all_objects,
499 l->sha1, hint);
500 l = l->next;
501 }
502 pl = pl->next;
503 }
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100504 /* remove objects present in remote packs */
505 pl = altodb_packs;
506 while (pl) {
507 llist_sorted_difference_inplace(all_objects, pl->all_objects);
508 pl = pl->next;
509 }
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100510}
511
512/* this scales like O(n^2) */
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200513static void cmp_local_packs(void)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100514{
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100515 struct pack_list *subset, *pl = local_packs;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100516
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100517 while ((subset = pl)) {
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400518 while ((subset = subset->next))
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100519 cmp_two_packs(pl, subset);
520 pl = pl->next;
521 }
Lukas Sandström06a45c82005-11-18 23:00:55 +0100522}
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100523
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200524static void scan_alt_odb_packs(void)
Lukas Sandström06a45c82005-11-18 23:00:55 +0100525{
526 struct pack_list *local, *alt;
527
528 alt = altodb_packs;
529 while (alt) {
530 local = local_packs;
531 while (local) {
532 llist_sorted_difference_inplace(local->unique_objects,
533 alt->all_objects);
534 local = local->next;
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100535 }
Lukas Sandström6d016c92005-12-20 21:05:54 +0100536 llist_sorted_difference_inplace(all_objects, alt->all_objects);
Lukas Sandström06a45c82005-11-18 23:00:55 +0100537 alt = alt->next;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100538 }
539}
540
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200541static struct pack_list * add_pack(struct packed_git *p)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100542{
543 struct pack_list l;
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400544 unsigned long off = 0, step;
Nicolas Pitre42873072007-03-16 16:42:50 -0400545 const unsigned char *base;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100546
Lukas Sandström06a45c82005-11-18 23:00:55 +0100547 if (!p->pack_local && !(alt_odb || verbose))
548 return NULL;
549
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100550 l.pack = p;
551 llist_init(&l.all_objects);
552
Shawn O. Pearceeaa86772007-05-30 02:12:28 -0400553 if (open_pack_index(p))
Shawn O. Pearced0798372007-05-26 01:24:19 -0400554 return NULL;
555
Nicolas Pitre42873072007-03-16 16:42:50 -0400556 base = p->index_data;
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400557 base += 256 * 4 + ((p->index_version < 2) ? 4 : 8);
558 step = (p->index_version < 2) ? 24 : 20;
559 while (off < p->num_objects * step) {
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100560 llist_insert_back(l.all_objects, base + off);
Nicolas Pitre8c681e02007-04-09 01:06:37 -0400561 off += step;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100562 }
563 /* this list will be pruned in cmp_two_packs later */
564 l.unique_objects = llist_copy(l.all_objects);
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100565 if (p->pack_local)
566 return pack_list_insert(&local_packs, &l);
567 else
Lukas Sandström06a45c82005-11-18 23:00:55 +0100568 return pack_list_insert(&altodb_packs, &l);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100569}
570
Linus Torvalds377d0272010-01-22 07:42:14 -0800571static struct pack_list * add_pack_file(const char *filename)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100572{
573 struct packed_git *p = packed_git;
574
575 if (strlen(filename) < 40)
Alexander Potashevd7530702009-01-04 21:38:41 +0300576 die("Bad pack filename: %s", filename);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100577
578 while (p) {
579 if (strstr(p->pack_name, filename))
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100580 return add_pack(p);
581 p = p->next;
582 }
Alexander Potashevd7530702009-01-04 21:38:41 +0300583 die("Filename %s not found in packed_git", filename);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100584}
585
Timo Hirvonenbd22c902005-11-21 02:52:52 +0200586static void load_all(void)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100587{
588 struct packed_git *p = packed_git;
589
590 while (p) {
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100591 add_pack(p);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100592 p = p->next;
593 }
594}
595
Linus Torvalds377d0272010-01-22 07:42:14 -0800596int cmd_pack_redundant(int argc, const char **argv, const char *prefix)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100597{
598 int i;
599 struct pack_list *min, *red, *pl;
Lukas Sandströmbb931cf2005-11-18 23:17:50 +0100600 struct llist *ignore;
Junio C Hamanob99a3942005-11-23 16:08:36 -0800601 unsigned char *sha1;
602 char buf[42]; /* 40 byte sha1 + \n + \0 */
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100603
Jonathan Nieder99caeed2009-11-09 09:05:01 -0600604 if (argc == 2 && !strcmp(argv[1], "-h"))
605 usage(pack_redundant_usage);
606
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100607 for (i = 1; i < argc; i++) {
608 const char *arg = argv[i];
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400609 if (!strcmp(arg, "--")) {
Lukas_Sandström9bc0f322005-11-10 00:16:13 +0100610 i++;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100611 break;
Lukas_Sandström9bc0f322005-11-10 00:16:13 +0100612 }
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400613 if (!strcmp(arg, "--all")) {
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100614 load_all_packs = 1;
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100615 continue;
616 }
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400617 if (!strcmp(arg, "--verbose")) {
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100618 verbose = 1;
619 continue;
620 }
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400621 if (!strcmp(arg, "--alt-odb")) {
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100622 alt_odb = 1;
623 continue;
624 }
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400625 if (*arg == '-')
Lukas_Sandström9bc0f322005-11-10 00:16:13 +0100626 usage(pack_redundant_usage);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100627 else
628 break;
629 }
630
631 prepare_packed_git();
632
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100633 if (load_all_packs)
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100634 load_all();
635 else
636 while (*(argv + i) != NULL)
637 add_pack_file(*(argv + i++));
638
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100639 if (local_packs == NULL)
Alexander Potashevd7530702009-01-04 21:38:41 +0300640 die("Zero packs found!");
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100641
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100642 load_all_objects();
643
Lukas Sandström06a45c82005-11-18 23:00:55 +0100644 cmp_local_packs();
645 if (alt_odb)
646 scan_alt_odb_packs();
647
Lukas Sandströmbb931cf2005-11-18 23:17:50 +0100648 /* ignore objects given on stdin */
649 llist_init(&ignore);
650 if (!isatty(0)) {
651 while (fgets(buf, sizeof(buf), stdin)) {
652 sha1 = xmalloc(20);
653 if (get_sha1_hex(buf, sha1))
654 die("Bad sha1 on stdin: %s", buf);
655 llist_insert_sorted_unique(ignore, sha1, NULL);
656 }
657 }
658 llist_sorted_difference_inplace(all_objects, ignore);
659 pl = local_packs;
660 while (pl) {
661 llist_sorted_difference_inplace(pl->unique_objects, ignore);
662 pl = pl->next;
663 }
664
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100665 minimize(&min);
Lukas Sandström06a45c82005-11-18 23:00:55 +0100666
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100667 if (verbose) {
Kai Ruemmlerabacbe42005-11-12 17:33:24 +0100668 fprintf(stderr, "There are %lu packs available in alt-odbs.\n",
669 (unsigned long)pack_list_size(altodb_packs));
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100670 fprintf(stderr, "The smallest (bytewise) set of packs is:\n");
671 pl = min;
672 while (pl) {
673 fprintf(stderr, "\t%s\n", pl->pack->pack_name);
674 pl = pl->next;
675 }
Kai Ruemmlerabacbe42005-11-12 17:33:24 +0100676 fprintf(stderr, "containing %lu duplicate objects "
677 "with a total size of %lukb.\n",
678 (unsigned long)get_pack_redundancy(min),
679 (unsigned long)pack_set_bytecount(min)/1024);
680 fprintf(stderr, "A total of %lu unique objects were considered.\n",
681 (unsigned long)all_objects->size);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100682 fprintf(stderr, "Redundant packs (with indexes):\n");
683 }
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100684 pl = red = pack_list_difference(local_packs, min);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100685 while (pl) {
686 printf("%s\n%s\n",
Lukas_Sandström1c3039e2005-11-11 01:25:04 +0100687 sha1_pack_index_name(pl->pack->sha1),
688 pl->pack->pack_name);
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100689 pl = pl->next;
690 }
Lukas Sandströmbb931cf2005-11-18 23:17:50 +0100691 if (verbose)
Junio C Hamanob99a3942005-11-23 16:08:36 -0800692 fprintf(stderr, "%luMB of redundant packs in total.\n",
693 (unsigned long)pack_set_bytecount(red)/(1024*1024));
Lukas_Sandströmc283ab22005-11-09 02:22:40 +0100694
695 return 0;
696}