Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 1 | /* |
| 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 Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 10 | #include "exec_cmd.h" |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 11 | |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 12 | #define BLKSIZE 512 |
| 13 | |
Lukas_Sandström | 9bc0f32 | 2005-11-10 00:16:13 +0100 | [diff] [blame] | 14 | static const char pack_redundant_usage[] = |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 15 | "git pack-redundant [ --verbose ] [ --alt-odb ] < --all | <.pack filename> ...>"; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 16 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 17 | static int load_all_packs, verbose, alt_odb; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 18 | |
| 19 | struct llist_item { |
| 20 | struct llist_item *next; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 21 | const unsigned char *sha1; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 22 | }; |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 23 | static struct llist { |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 24 | struct llist_item *front; |
| 25 | struct llist_item *back; |
| 26 | size_t size; |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 27 | } *all_objects; /* all objects which must be present in local packfiles */ |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 28 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 29 | static struct pack_list { |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 30 | struct pack_list *next; |
| 31 | struct packed_git *pack; |
| 32 | struct llist *unique_objects; |
| 33 | struct llist *all_objects; |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 34 | } *local_packs = NULL, *altodb_packs = NULL; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 35 | |
| 36 | struct pll { |
| 37 | struct pll *next; |
| 38 | struct pack_list *pl; |
| 39 | }; |
| 40 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 41 | static struct llist_item *free_nodes; |
Alex Riesen | 60435f6 | 2005-11-22 15:56:35 +0100 | [diff] [blame] | 42 | |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 43 | static inline void llist_item_put(struct llist_item *item) |
| 44 | { |
| 45 | item->next = free_nodes; |
| 46 | free_nodes = item; |
| 47 | } |
| 48 | |
Timo Hirvonen | 962554c | 2006-02-26 17:13:46 +0200 | [diff] [blame] | 49 | static inline struct llist_item *llist_item_get(void) |
Alex Riesen | 60435f6 | 2005-11-22 15:56:35 +0100 | [diff] [blame] | 50 | { |
| 51 | struct llist_item *new; |
| 52 | if ( free_nodes ) { |
| 53 | new = free_nodes; |
| 54 | free_nodes = free_nodes->next; |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 55 | } else { |
| 56 | int i = 1; |
| 57 | new = xmalloc(sizeof(struct llist_item) * BLKSIZE); |
| 58 | for(;i < BLKSIZE; i++) { |
| 59 | llist_item_put(&new[i]); |
| 60 | } |
| 61 | } |
Alex Riesen | 60435f6 | 2005-11-22 15:56:35 +0100 | [diff] [blame] | 62 | return new; |
| 63 | } |
| 64 | |
Alex Riesen | 60435f6 | 2005-11-22 15:56:35 +0100 | [diff] [blame] | 65 | static void llist_free(struct llist *list) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 66 | { |
| 67 | while((list->back = list->front)) { |
| 68 | list->front = list->front->next; |
Alex Riesen | 60435f6 | 2005-11-22 15:56:35 +0100 | [diff] [blame] | 69 | llist_item_put(list->back); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 70 | } |
| 71 | free(list); |
| 72 | } |
| 73 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 74 | static inline void llist_init(struct llist **list) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 75 | { |
| 76 | *list = xmalloc(sizeof(struct llist)); |
| 77 | (*list)->front = (*list)->back = NULL; |
| 78 | (*list)->size = 0; |
| 79 | } |
| 80 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 81 | static struct llist * llist_copy(struct llist *list) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 82 | { |
| 83 | struct llist *ret; |
| 84 | struct llist_item *new, *old, *prev; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 85 | |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 86 | llist_init(&ret); |
| 87 | |
| 88 | if ((ret->size = list->size) == 0) |
| 89 | return ret; |
| 90 | |
Alex Riesen | 60435f6 | 2005-11-22 15:56:35 +0100 | [diff] [blame] | 91 | new = ret->front = llist_item_get(); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 92 | new->sha1 = list->front->sha1; |
| 93 | |
| 94 | old = list->front->next; |
| 95 | while (old) { |
| 96 | prev = new; |
Alex Riesen | 60435f6 | 2005-11-22 15:56:35 +0100 | [diff] [blame] | 97 | new = llist_item_get(); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 98 | prev->next = new; |
| 99 | new->sha1 = old->sha1; |
| 100 | old = old->next; |
| 101 | } |
| 102 | new->next = NULL; |
| 103 | ret->back = new; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 104 | |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 105 | return ret; |
| 106 | } |
| 107 | |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 108 | static inline struct llist_item *llist_insert(struct llist *list, |
| 109 | struct llist_item *after, |
| 110 | const unsigned char *sha1) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 111 | { |
Alex Riesen | 60435f6 | 2005-11-22 15:56:35 +0100 | [diff] [blame] | 112 | struct llist_item *new = llist_item_get(); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 113 | new->sha1 = sha1; |
| 114 | new->next = NULL; |
| 115 | |
| 116 | if (after != NULL) { |
| 117 | new->next = after->next; |
| 118 | after->next = new; |
| 119 | if (after == list->back) |
| 120 | list->back = new; |
| 121 | } else {/* insert in front */ |
| 122 | if (list->size == 0) |
| 123 | list->back = new; |
| 124 | else |
| 125 | new->next = list->front; |
| 126 | list->front = new; |
| 127 | } |
| 128 | list->size++; |
| 129 | return new; |
| 130 | } |
| 131 | |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 132 | static inline struct llist_item *llist_insert_back(struct llist *list, |
| 133 | const unsigned char *sha1) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 134 | { |
| 135 | return llist_insert(list, list->back, sha1); |
| 136 | } |
| 137 | |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 138 | static inline struct llist_item *llist_insert_sorted_unique(struct llist *list, |
| 139 | const unsigned char *sha1, struct llist_item *hint) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 140 | { |
| 141 | struct llist_item *prev = NULL, *l; |
| 142 | |
| 143 | l = (hint == NULL) ? list->front : hint; |
| 144 | while (l) { |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 145 | int cmp = hashcmp(l->sha1, sha1); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 146 | if (cmp > 0) { /* we insert before this entry */ |
| 147 | return llist_insert(list, prev, sha1); |
| 148 | } |
| 149 | if(!cmp) { /* already exists */ |
| 150 | return l; |
| 151 | } |
| 152 | prev = l; |
| 153 | l = l->next; |
| 154 | } |
| 155 | /* insert at the end */ |
| 156 | return llist_insert_back(list, sha1); |
| 157 | } |
| 158 | |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 159 | /* returns a pointer to an item in front of sha1 */ |
Junio C Hamano | b99a394 | 2005-11-23 16:08:36 -0800 | [diff] [blame] | 160 | static inline struct llist_item * llist_sorted_remove(struct llist *list, const unsigned char *sha1, struct llist_item *hint) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 161 | { |
| 162 | struct llist_item *prev, *l; |
| 163 | |
| 164 | redo_from_start: |
| 165 | l = (hint == NULL) ? list->front : hint; |
| 166 | prev = NULL; |
| 167 | while (l) { |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 168 | int cmp = hashcmp(l->sha1, sha1); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 169 | if (cmp > 0) /* not in list, since sorted */ |
| 170 | return prev; |
| 171 | if(!cmp) { /* found */ |
| 172 | if (prev == NULL) { |
| 173 | if (hint != NULL && hint != list->front) { |
| 174 | /* we don't know the previous element */ |
| 175 | hint = NULL; |
| 176 | goto redo_from_start; |
| 177 | } |
| 178 | list->front = l->next; |
| 179 | } else |
| 180 | prev->next = l->next; |
| 181 | if (l == list->back) |
| 182 | list->back = prev; |
Alex Riesen | 60435f6 | 2005-11-22 15:56:35 +0100 | [diff] [blame] | 183 | llist_item_put(l); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 184 | list->size--; |
| 185 | return prev; |
| 186 | } |
| 187 | prev = l; |
| 188 | l = l->next; |
| 189 | } |
| 190 | return prev; |
| 191 | } |
| 192 | |
Lukas Sandström | 1a41e74 | 2005-11-15 22:24:02 +0100 | [diff] [blame] | 193 | /* computes A\B */ |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 194 | static void llist_sorted_difference_inplace(struct llist *A, |
Lukas Sandström | 1a41e74 | 2005-11-15 22:24:02 +0100 | [diff] [blame] | 195 | struct llist *B) |
| 196 | { |
| 197 | struct llist_item *hint, *b; |
| 198 | |
| 199 | hint = NULL; |
| 200 | b = B->front; |
| 201 | |
| 202 | while (b) { |
| 203 | hint = llist_sorted_remove(A, b->sha1, hint); |
| 204 | b = b->next; |
| 205 | } |
| 206 | } |
| 207 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 208 | static inline struct pack_list * pack_list_insert(struct pack_list **pl, |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 209 | struct pack_list *entry) |
| 210 | { |
| 211 | struct pack_list *p = xmalloc(sizeof(struct pack_list)); |
| 212 | memcpy(p, entry, sizeof(struct pack_list)); |
| 213 | p->next = *pl; |
| 214 | *pl = p; |
| 215 | return p; |
| 216 | } |
| 217 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 218 | static inline size_t pack_list_size(struct pack_list *pl) |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 219 | { |
| 220 | size_t ret = 0; |
| 221 | while(pl) { |
| 222 | ret++; |
| 223 | pl = pl->next; |
| 224 | } |
| 225 | return ret; |
| 226 | } |
| 227 | |
Alex Riesen | d1ab157 | 2005-11-22 15:59:22 +0100 | [diff] [blame] | 228 | static struct pack_list * pack_list_difference(const struct pack_list *A, |
| 229 | const struct pack_list *B) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 230 | { |
Alex Riesen | d1ab157 | 2005-11-22 15:59:22 +0100 | [diff] [blame] | 231 | struct pack_list *ret; |
| 232 | const struct pack_list *pl; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 233 | |
| 234 | if (A == NULL) |
| 235 | return NULL; |
| 236 | |
| 237 | pl = B; |
| 238 | while (pl != NULL) { |
| 239 | if (A->pack == pl->pack) |
| 240 | return pack_list_difference(A->next, B); |
| 241 | pl = pl->next; |
| 242 | } |
| 243 | ret = xmalloc(sizeof(struct pack_list)); |
| 244 | memcpy(ret, A, sizeof(struct pack_list)); |
| 245 | ret->next = pack_list_difference(A->next, B); |
| 246 | return ret; |
| 247 | } |
| 248 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 249 | static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 250 | { |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 251 | unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 252 | const unsigned char *p1_base, *p2_base; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 253 | struct llist_item *p1_hint = NULL, *p2_hint = NULL; |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 254 | |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 255 | p1_base = p1->pack->index_data; |
| 256 | p2_base = p2->pack->index_data; |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 257 | p1_base += 256 * 4 + ((p1->pack->index_version < 2) ? 4 : 8); |
| 258 | p2_base += 256 * 4 + ((p2->pack->index_version < 2) ? 4 : 8); |
| 259 | p1_step = (p1->pack->index_version < 2) ? 24 : 20; |
| 260 | p2_step = (p2->pack->index_version < 2) ? 24 : 20; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 261 | |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 262 | while (p1_off < p1->pack->num_objects * p1_step && |
| 263 | p2_off < p2->pack->num_objects * p2_step) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 264 | { |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 265 | int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 266 | /* cmp ~ p1 - p2 */ |
| 267 | if (cmp == 0) { |
| 268 | p1_hint = llist_sorted_remove(p1->unique_objects, |
| 269 | p1_base + p1_off, p1_hint); |
| 270 | p2_hint = llist_sorted_remove(p2->unique_objects, |
| 271 | p1_base + p1_off, p2_hint); |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 272 | p1_off += p1_step; |
| 273 | p2_off += p2_step; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 274 | continue; |
| 275 | } |
| 276 | if (cmp < 0) { /* p1 has the object, p2 doesn't */ |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 277 | p1_off += p1_step; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 278 | } else { /* p2 has the object, p1 doesn't */ |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 279 | p2_off += p2_step; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Timo Hirvonen | 962554c | 2006-02-26 17:13:46 +0200 | [diff] [blame] | 284 | static void pll_free(struct pll *l) |
Lukas Sandström | 751a71e | 2005-11-17 14:11:56 +0100 | [diff] [blame] | 285 | { |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 286 | struct pll *old; |
| 287 | struct pack_list *opl; |
Lukas Sandström | 751a71e | 2005-11-17 14:11:56 +0100 | [diff] [blame] | 288 | |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 289 | while (l) { |
| 290 | old = l; |
| 291 | while (l->pl) { |
| 292 | opl = l->pl; |
| 293 | l->pl = opl->next; |
| 294 | free(opl); |
Lukas Sandström | 751a71e | 2005-11-17 14:11:56 +0100 | [diff] [blame] | 295 | } |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 296 | l = l->next; |
| 297 | free(old); |
Lukas Sandström | 751a71e | 2005-11-17 14:11:56 +0100 | [diff] [blame] | 298 | } |
Lukas Sandström | 751a71e | 2005-11-17 14:11:56 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 301 | /* all the permutations have to be free()d at the same time, |
| 302 | * since they refer to each other |
| 303 | */ |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 304 | static struct pll * get_permutations(struct pack_list *list, int n) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 305 | { |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 306 | struct pll *subset, *ret = NULL, *new_pll = NULL, *pll; |
| 307 | |
| 308 | if (list == NULL || pack_list_size(list) < n || n == 0) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 309 | return NULL; |
| 310 | |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 311 | if (n == 1) { |
| 312 | while (list) { |
| 313 | new_pll = xmalloc(sizeof(pll)); |
| 314 | new_pll->pl = NULL; |
| 315 | pack_list_insert(&new_pll->pl, list); |
| 316 | new_pll->next = ret; |
| 317 | ret = new_pll; |
| 318 | list = list->next; |
Lukas Sandström | 751a71e | 2005-11-17 14:11:56 +0100 | [diff] [blame] | 319 | } |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 320 | return ret; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 321 | } |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 322 | |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 323 | while (list->next) { |
| 324 | subset = get_permutations(list->next, n - 1); |
| 325 | while (subset) { |
| 326 | new_pll = xmalloc(sizeof(pll)); |
| 327 | new_pll->pl = subset->pl; |
| 328 | pack_list_insert(&new_pll->pl, list); |
| 329 | new_pll->next = ret; |
| 330 | ret = new_pll; |
| 331 | subset = subset->next; |
| 332 | } |
| 333 | list = list->next; |
| 334 | } |
| 335 | return ret; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 338 | static int is_superset(struct pack_list *pl, struct llist *list) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 339 | { |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 340 | struct llist *diff; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 341 | |
| 342 | diff = llist_copy(list); |
| 343 | |
| 344 | while (pl) { |
Alex Riesen | d1ab157 | 2005-11-22 15:59:22 +0100 | [diff] [blame] | 345 | llist_sorted_difference_inplace(diff, pl->all_objects); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 346 | if (diff->size == 0) { /* we're done */ |
| 347 | llist_free(diff); |
| 348 | return 1; |
| 349 | } |
| 350 | pl = pl->next; |
| 351 | } |
| 352 | llist_free(diff); |
| 353 | return 0; |
| 354 | } |
| 355 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 356 | static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 357 | { |
| 358 | size_t ret = 0; |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 359 | unsigned long p1_off = 0, p2_off = 0, p1_step, p2_step; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 360 | const unsigned char *p1_base, *p2_base; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 361 | |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 362 | p1_base = p1->index_data; |
| 363 | p2_base = p2->index_data; |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 364 | p1_base += 256 * 4 + ((p1->index_version < 2) ? 4 : 8); |
| 365 | p2_base += 256 * 4 + ((p2->index_version < 2) ? 4 : 8); |
| 366 | p1_step = (p1->index_version < 2) ? 24 : 20; |
| 367 | p2_step = (p2->index_version < 2) ? 24 : 20; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 368 | |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 369 | while (p1_off < p1->num_objects * p1_step && |
| 370 | p2_off < p2->num_objects * p2_step) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 371 | { |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 372 | int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 373 | /* cmp ~ p1 - p2 */ |
| 374 | if (cmp == 0) { |
| 375 | ret++; |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 376 | p1_off += p1_step; |
| 377 | p2_off += p2_step; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 378 | continue; |
| 379 | } |
| 380 | if (cmp < 0) { /* p1 has the object, p2 doesn't */ |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 381 | p1_off += p1_step; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 382 | } else { /* p2 has the object, p1 doesn't */ |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 383 | p2_off += p2_step; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | /* another O(n^2) function ... */ |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 390 | static size_t get_pack_redundancy(struct pack_list *pl) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 391 | { |
| 392 | struct pack_list *subset; |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 393 | size_t ret = 0; |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 394 | |
| 395 | if (pl == NULL) |
| 396 | return 0; |
| 397 | |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 398 | while ((subset = pl->next)) { |
| 399 | while(subset) { |
| 400 | ret += sizeof_union(pl->pack, subset->pack); |
| 401 | subset = subset->next; |
| 402 | } |
| 403 | pl = pl->next; |
| 404 | } |
| 405 | return ret; |
| 406 | } |
| 407 | |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 408 | static inline off_t pack_set_bytecount(struct pack_list *pl) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 409 | { |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 410 | off_t ret = 0; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 411 | while (pl) { |
| 412 | ret += pl->pack->pack_size; |
| 413 | ret += pl->pack->index_size; |
| 414 | pl = pl->next; |
| 415 | } |
| 416 | return ret; |
| 417 | } |
| 418 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 419 | static void minimize(struct pack_list **min) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 420 | { |
| 421 | struct pack_list *pl, *unique = NULL, |
| 422 | *non_unique = NULL, *min_perm = NULL; |
| 423 | struct pll *perm, *perm_all, *perm_ok = NULL, *new_perm; |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 424 | struct llist *missing; |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 425 | off_t min_perm_size = 0, perm_size; |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 426 | int n; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 427 | |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 428 | pl = local_packs; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 429 | while (pl) { |
| 430 | if(pl->unique_objects->size) |
| 431 | pack_list_insert(&unique, pl); |
| 432 | else |
| 433 | pack_list_insert(&non_unique, pl); |
| 434 | pl = pl->next; |
| 435 | } |
| 436 | /* find out which objects are missing from the set of unique packs */ |
| 437 | missing = llist_copy(all_objects); |
| 438 | pl = unique; |
| 439 | while (pl) { |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 440 | llist_sorted_difference_inplace(missing, pl->all_objects); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 441 | pl = pl->next; |
| 442 | } |
| 443 | |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 444 | /* return if there are no objects missing from the unique set */ |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 445 | if (missing->size == 0) { |
| 446 | *min = unique; |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | /* find the permutations which contain all missing objects */ |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 451 | for (n = 1; n <= pack_list_size(non_unique) && !perm_ok; n++) { |
| 452 | perm_all = perm = get_permutations(non_unique, n); |
| 453 | while (perm) { |
| 454 | if (is_superset(perm->pl, missing)) { |
| 455 | new_perm = xmalloc(sizeof(struct pll)); |
| 456 | memcpy(new_perm, perm, sizeof(struct pll)); |
| 457 | new_perm->next = perm_ok; |
| 458 | perm_ok = new_perm; |
| 459 | } |
| 460 | perm = perm->next; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 461 | } |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 462 | if (perm_ok) |
| 463 | break; |
| 464 | pll_free(perm_all); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 465 | } |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 466 | if (perm_ok == NULL) |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 467 | die("Internal error: No complete sets found!"); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 468 | |
| 469 | /* find the permutation with the smallest size */ |
| 470 | perm = perm_ok; |
| 471 | while (perm) { |
| 472 | perm_size = pack_set_bytecount(perm->pl); |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 473 | if (!min_perm_size || min_perm_size > perm_size) { |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 474 | min_perm_size = perm_size; |
| 475 | min_perm = perm->pl; |
| 476 | } |
| 477 | perm = perm->next; |
| 478 | } |
| 479 | *min = min_perm; |
| 480 | /* add the unique packs to the list */ |
| 481 | pl = unique; |
| 482 | while(pl) { |
| 483 | pack_list_insert(min, pl); |
| 484 | pl = pl->next; |
| 485 | } |
| 486 | } |
| 487 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 488 | static void load_all_objects(void) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 489 | { |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 490 | struct pack_list *pl = local_packs; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 491 | struct llist_item *hint, *l; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 492 | |
| 493 | llist_init(&all_objects); |
| 494 | |
| 495 | while (pl) { |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 496 | hint = NULL; |
| 497 | l = pl->all_objects->front; |
| 498 | while (l) { |
| 499 | hint = llist_insert_sorted_unique(all_objects, |
| 500 | l->sha1, hint); |
| 501 | l = l->next; |
| 502 | } |
| 503 | pl = pl->next; |
| 504 | } |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 505 | /* remove objects present in remote packs */ |
| 506 | pl = altodb_packs; |
| 507 | while (pl) { |
| 508 | llist_sorted_difference_inplace(all_objects, pl->all_objects); |
| 509 | pl = pl->next; |
| 510 | } |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* this scales like O(n^2) */ |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 514 | static void cmp_local_packs(void) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 515 | { |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 516 | struct pack_list *subset, *pl = local_packs; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 517 | |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 518 | while ((subset = pl)) { |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 519 | while((subset = subset->next)) |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 520 | cmp_two_packs(pl, subset); |
| 521 | pl = pl->next; |
| 522 | } |
Lukas Sandström | 06a45c8 | 2005-11-18 23:00:55 +0100 | [diff] [blame] | 523 | } |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 524 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 525 | static void scan_alt_odb_packs(void) |
Lukas Sandström | 06a45c8 | 2005-11-18 23:00:55 +0100 | [diff] [blame] | 526 | { |
| 527 | struct pack_list *local, *alt; |
| 528 | |
| 529 | alt = altodb_packs; |
| 530 | while (alt) { |
| 531 | local = local_packs; |
| 532 | while (local) { |
| 533 | llist_sorted_difference_inplace(local->unique_objects, |
| 534 | alt->all_objects); |
| 535 | local = local->next; |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 536 | } |
Lukas Sandström | 6d016c9 | 2005-12-20 21:05:54 +0100 | [diff] [blame] | 537 | llist_sorted_difference_inplace(all_objects, alt->all_objects); |
Lukas Sandström | 06a45c8 | 2005-11-18 23:00:55 +0100 | [diff] [blame] | 538 | alt = alt->next; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 542 | static struct pack_list * add_pack(struct packed_git *p) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 543 | { |
| 544 | struct pack_list l; |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 545 | unsigned long off = 0, step; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 546 | const unsigned char *base; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 547 | |
Lukas Sandström | 06a45c8 | 2005-11-18 23:00:55 +0100 | [diff] [blame] | 548 | if (!p->pack_local && !(alt_odb || verbose)) |
| 549 | return NULL; |
| 550 | |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 551 | l.pack = p; |
| 552 | llist_init(&l.all_objects); |
| 553 | |
Shawn O. Pearce | eaa8677 | 2007-05-30 02:12:28 -0400 | [diff] [blame] | 554 | if (open_pack_index(p)) |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 555 | return NULL; |
| 556 | |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 557 | base = p->index_data; |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 558 | base += 256 * 4 + ((p->index_version < 2) ? 4 : 8); |
| 559 | step = (p->index_version < 2) ? 24 : 20; |
| 560 | while (off < p->num_objects * step) { |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 561 | llist_insert_back(l.all_objects, base + off); |
Nicolas Pitre | 8c681e0 | 2007-04-09 01:06:37 -0400 | [diff] [blame] | 562 | off += step; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 563 | } |
| 564 | /* this list will be pruned in cmp_two_packs later */ |
| 565 | l.unique_objects = llist_copy(l.all_objects); |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 566 | if (p->pack_local) |
| 567 | return pack_list_insert(&local_packs, &l); |
| 568 | else |
Lukas Sandström | 06a45c8 | 2005-11-18 23:00:55 +0100 | [diff] [blame] | 569 | return pack_list_insert(&altodb_packs, &l); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 572 | static struct pack_list * add_pack_file(char *filename) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 573 | { |
| 574 | struct packed_git *p = packed_git; |
| 575 | |
| 576 | if (strlen(filename) < 40) |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 577 | die("Bad pack filename: %s", filename); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 578 | |
| 579 | while (p) { |
| 580 | if (strstr(p->pack_name, filename)) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 581 | return add_pack(p); |
| 582 | p = p->next; |
| 583 | } |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 584 | die("Filename %s not found in packed_git", filename); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Timo Hirvonen | bd22c90 | 2005-11-21 02:52:52 +0200 | [diff] [blame] | 587 | static void load_all(void) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 588 | { |
| 589 | struct packed_git *p = packed_git; |
| 590 | |
| 591 | while (p) { |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 592 | add_pack(p); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 593 | p = p->next; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | int main(int argc, char **argv) |
| 598 | { |
| 599 | int i; |
| 600 | struct pack_list *min, *red, *pl; |
Lukas Sandström | bb931cf | 2005-11-18 23:17:50 +0100 | [diff] [blame] | 601 | struct llist *ignore; |
Junio C Hamano | b99a394 | 2005-11-23 16:08:36 -0800 | [diff] [blame] | 602 | unsigned char *sha1; |
| 603 | char buf[42]; /* 40 byte sha1 + \n + \0 */ |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 604 | |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 605 | git_extract_argv0_path(argv[0]); |
| 606 | |
Junio C Hamano | 53228a5 | 2005-11-26 00:50:02 -0800 | [diff] [blame] | 607 | setup_git_directory(); |
| 608 | |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 609 | for (i = 1; i < argc; i++) { |
| 610 | const char *arg = argv[i]; |
Lukas_Sandström | 9bc0f32 | 2005-11-10 00:16:13 +0100 | [diff] [blame] | 611 | if(!strcmp(arg, "--")) { |
| 612 | i++; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 613 | break; |
Lukas_Sandström | 9bc0f32 | 2005-11-10 00:16:13 +0100 | [diff] [blame] | 614 | } |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 615 | if(!strcmp(arg, "--all")) { |
| 616 | load_all_packs = 1; |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 617 | continue; |
| 618 | } |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 619 | if(!strcmp(arg, "--verbose")) { |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 620 | verbose = 1; |
| 621 | continue; |
| 622 | } |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 623 | if(!strcmp(arg, "--alt-odb")) { |
| 624 | alt_odb = 1; |
| 625 | continue; |
| 626 | } |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 627 | if(*arg == '-') |
Lukas_Sandström | 9bc0f32 | 2005-11-10 00:16:13 +0100 | [diff] [blame] | 628 | usage(pack_redundant_usage); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 629 | else |
| 630 | break; |
| 631 | } |
| 632 | |
| 633 | prepare_packed_git(); |
| 634 | |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 635 | if (load_all_packs) |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 636 | load_all(); |
| 637 | else |
| 638 | while (*(argv + i) != NULL) |
| 639 | add_pack_file(*(argv + i++)); |
| 640 | |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 641 | if (local_packs == NULL) |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 642 | die("Zero packs found!"); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 643 | |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 644 | load_all_objects(); |
| 645 | |
Lukas Sandström | 06a45c8 | 2005-11-18 23:00:55 +0100 | [diff] [blame] | 646 | cmp_local_packs(); |
| 647 | if (alt_odb) |
| 648 | scan_alt_odb_packs(); |
| 649 | |
Lukas Sandström | bb931cf | 2005-11-18 23:17:50 +0100 | [diff] [blame] | 650 | /* ignore objects given on stdin */ |
| 651 | llist_init(&ignore); |
| 652 | if (!isatty(0)) { |
| 653 | while (fgets(buf, sizeof(buf), stdin)) { |
| 654 | sha1 = xmalloc(20); |
| 655 | if (get_sha1_hex(buf, sha1)) |
| 656 | die("Bad sha1 on stdin: %s", buf); |
| 657 | llist_insert_sorted_unique(ignore, sha1, NULL); |
| 658 | } |
| 659 | } |
| 660 | llist_sorted_difference_inplace(all_objects, ignore); |
| 661 | pl = local_packs; |
| 662 | while (pl) { |
| 663 | llist_sorted_difference_inplace(pl->unique_objects, ignore); |
| 664 | pl = pl->next; |
| 665 | } |
| 666 | |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 667 | minimize(&min); |
Lukas Sandström | 06a45c8 | 2005-11-18 23:00:55 +0100 | [diff] [blame] | 668 | |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 669 | if (verbose) { |
Kai Ruemmler | abacbe4 | 2005-11-12 17:33:24 +0100 | [diff] [blame] | 670 | fprintf(stderr, "There are %lu packs available in alt-odbs.\n", |
| 671 | (unsigned long)pack_list_size(altodb_packs)); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 672 | fprintf(stderr, "The smallest (bytewise) set of packs is:\n"); |
| 673 | pl = min; |
| 674 | while (pl) { |
| 675 | fprintf(stderr, "\t%s\n", pl->pack->pack_name); |
| 676 | pl = pl->next; |
| 677 | } |
Kai Ruemmler | abacbe4 | 2005-11-12 17:33:24 +0100 | [diff] [blame] | 678 | fprintf(stderr, "containing %lu duplicate objects " |
| 679 | "with a total size of %lukb.\n", |
| 680 | (unsigned long)get_pack_redundancy(min), |
| 681 | (unsigned long)pack_set_bytecount(min)/1024); |
| 682 | fprintf(stderr, "A total of %lu unique objects were considered.\n", |
| 683 | (unsigned long)all_objects->size); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 684 | fprintf(stderr, "Redundant packs (with indexes):\n"); |
| 685 | } |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 686 | pl = red = pack_list_difference(local_packs, min); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 687 | while (pl) { |
| 688 | printf("%s\n%s\n", |
Lukas_Sandström | 1c3039e | 2005-11-11 01:25:04 +0100 | [diff] [blame] | 689 | sha1_pack_index_name(pl->pack->sha1), |
| 690 | pl->pack->pack_name); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 691 | pl = pl->next; |
| 692 | } |
Lukas Sandström | bb931cf | 2005-11-18 23:17:50 +0100 | [diff] [blame] | 693 | if (verbose) |
Junio C Hamano | b99a394 | 2005-11-23 16:08:36 -0800 | [diff] [blame] | 694 | fprintf(stderr, "%luMB of redundant packs in total.\n", |
| 695 | (unsigned long)pack_set_bytecount(red)/(1024*1024)); |
Lukas_Sandström | c283ab2 | 2005-11-09 02:22:40 +0100 | [diff] [blame] | 696 | |
| 697 | return 0; |
| 698 | } |