Linus Torvalds | 4b18242 | 2005-04-30 09:59:31 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Daniel Barkalow | ff5ebe3 | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 2 | #include "commit.h" |
| 3 | #include "tree.h" |
| 4 | #include "blob.h" |
Daniel Barkalow | c418eda | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 5 | #include "tag.h" |
Linus Torvalds | 944d858 | 2005-07-03 10:01:38 -0700 | [diff] [blame] | 6 | #include "refs.h" |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 7 | #include "pack.h" |
Junio C Hamano | 53dc3f3 | 2006-04-25 16:37:08 -0700 | [diff] [blame] | 8 | #include "cache-tree.h" |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 9 | #include "tree-walk.h" |
Daniel Barkalow | ff5ebe3 | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 10 | |
| 11 | #define REACHABLE 0x0001 |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 12 | #define SEEN 0x0002 |
Linus Torvalds | d9839e0 | 2005-04-13 09:57:30 -0700 | [diff] [blame] | 13 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 14 | static int show_root; |
| 15 | static int show_tags; |
| 16 | static int show_unreachable; |
| 17 | static int check_full; |
| 18 | static int check_strict; |
| 19 | static int keep_cache_objects; |
Linus Torvalds | d9839e0 | 2005-04-13 09:57:30 -0700 | [diff] [blame] | 20 | static unsigned char head_sha1[20]; |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 21 | static int errors_found; |
| 22 | #define ERROR_OBJECT 01 |
| 23 | #define ERROR_REACHABLE 02 |
Linus Torvalds | d9839e0 | 2005-04-13 09:57:30 -0700 | [diff] [blame] | 24 | |
Timo Hirvonen | 962554c | 2006-02-26 17:13:46 +0200 | [diff] [blame] | 25 | #ifdef NO_D_INO_IN_DIRENT |
Junio C Hamano | 35a730f | 2006-01-19 17:13:51 -0800 | [diff] [blame] | 26 | #define SORT_DIRENT 0 |
| 27 | #define DIRENT_SORT_HINT(de) 0 |
| 28 | #else |
| 29 | #define SORT_DIRENT 1 |
| 30 | #define DIRENT_SORT_HINT(de) ((de)->d_ino) |
| 31 | #endif |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 32 | |
| 33 | static void objreport(struct object *obj, const char *severity, |
| 34 | const char *err, va_list params) |
| 35 | { |
| 36 | fprintf(stderr, "%s in %s %s: ", |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 37 | severity, typename(obj->type), sha1_to_hex(obj->sha1)); |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 38 | vfprintf(stderr, err, params); |
| 39 | fputs("\n", stderr); |
| 40 | } |
| 41 | |
Peter Hagervall | a7928f8 | 2005-09-28 14:04:54 +0200 | [diff] [blame] | 42 | static int objerror(struct object *obj, const char *err, ...) |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 43 | { |
| 44 | va_list params; |
| 45 | va_start(params, err); |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 46 | errors_found |= ERROR_OBJECT; |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 47 | objreport(obj, "error", err, params); |
| 48 | va_end(params); |
| 49 | return -1; |
| 50 | } |
| 51 | |
Peter Hagervall | a7928f8 | 2005-09-28 14:04:54 +0200 | [diff] [blame] | 52 | static int objwarning(struct object *obj, const char *err, ...) |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 53 | { |
| 54 | va_list params; |
| 55 | va_start(params, err); |
| 56 | objreport(obj, "warning", err, params); |
| 57 | va_end(params); |
| 58 | return -1; |
| 59 | } |
| 60 | |
Linus Torvalds | 18af29f | 2007-01-21 22:26:41 -0800 | [diff] [blame] | 61 | /* |
| 62 | * Check a single reachable object |
| 63 | */ |
| 64 | static void check_reachable_object(struct object *obj) |
| 65 | { |
| 66 | const struct object_refs *refs; |
| 67 | |
| 68 | /* |
| 69 | * We obviously want the object to be parsed, |
| 70 | * except if it was in a pack-file and we didn't |
| 71 | * do a full fsck |
| 72 | */ |
| 73 | if (!obj->parsed) { |
Junio C Hamano | efec43c | 2007-03-05 00:21:24 -0800 | [diff] [blame] | 74 | if (has_sha1_pack(obj->sha1, NULL)) |
Linus Torvalds | 18af29f | 2007-01-21 22:26:41 -0800 | [diff] [blame] | 75 | return; /* it is in pack - forget about it */ |
| 76 | printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1)); |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 77 | errors_found |= ERROR_REACHABLE; |
Linus Torvalds | 18af29f | 2007-01-21 22:26:41 -0800 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Check that everything that we try to reference is also good. |
| 83 | */ |
| 84 | refs = lookup_object_refs(obj); |
| 85 | if (refs) { |
| 86 | unsigned j; |
| 87 | for (j = 0; j < refs->count; j++) { |
| 88 | struct object *ref = refs->ref[j]; |
| 89 | if (ref->parsed || |
| 90 | (has_sha1_file(ref->sha1))) |
| 91 | continue; |
| 92 | printf("broken link from %7s %s\n", |
| 93 | typename(obj->type), sha1_to_hex(obj->sha1)); |
| 94 | printf(" to %7s %s\n", |
| 95 | typename(ref->type), sha1_to_hex(ref->sha1)); |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 96 | errors_found |= ERROR_REACHABLE; |
Linus Torvalds | 18af29f | 2007-01-21 22:26:41 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Check a single unreachable object |
| 103 | */ |
| 104 | static void check_unreachable_object(struct object *obj) |
| 105 | { |
| 106 | /* |
| 107 | * Missing unreachable object? Ignore it. It's not like |
| 108 | * we miss it (since it can't be reached), nor do we want |
| 109 | * to complain about it being unreachable (since it does |
| 110 | * not exist). |
| 111 | */ |
| 112 | if (!obj->parsed) |
| 113 | return; |
| 114 | |
| 115 | /* |
| 116 | * Unreachable object that exists? Show it if asked to, |
| 117 | * since this is something that is prunable. |
| 118 | */ |
| 119 | if (show_unreachable) { |
| 120 | printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1)); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * "!used" means that nothing at all points to it, including |
Pavel Roskin | 3dff537 | 2007-02-03 23:49:16 -0500 | [diff] [blame] | 126 | * other unreachable objects. In other words, it's the "tip" |
Linus Torvalds | 18af29f | 2007-01-21 22:26:41 -0800 | [diff] [blame] | 127 | * of some set of unreachable objects, usually a commit that |
| 128 | * got dropped. |
| 129 | * |
| 130 | * Such starting points are more interesting than some random |
| 131 | * set of unreachable objects, so we show them even if the user |
| 132 | * hasn't asked for _all_ unreachable objects. If you have |
| 133 | * deleted a branch by mistake, this is a prime candidate to |
| 134 | * start looking at, for example. |
| 135 | */ |
| 136 | if (!obj->used) { |
| 137 | printf("dangling %s %s\n", typename(obj->type), |
| 138 | sha1_to_hex(obj->sha1)); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Otherwise? It's there, it's unreachable, and some other unreachable |
| 144 | * object points to it. Ignore it - it's not interesting, and we showed |
| 145 | * all the interesting cases above. |
| 146 | */ |
| 147 | } |
| 148 | |
| 149 | static void check_object(struct object *obj) |
| 150 | { |
| 151 | if (obj->flags & REACHABLE) |
| 152 | check_reachable_object(obj); |
| 153 | else |
| 154 | check_unreachable_object(obj); |
| 155 | } |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 156 | |
Linus Torvalds | 8ba0bbb | 2005-04-10 23:13:09 -0700 | [diff] [blame] | 157 | static void check_connectivity(void) |
| 158 | { |
Linus Torvalds | fc046a7 | 2006-06-29 21:38:55 -0700 | [diff] [blame] | 159 | int i, max; |
Linus Torvalds | 8ba0bbb | 2005-04-10 23:13:09 -0700 | [diff] [blame] | 160 | |
Linus Torvalds | 8ba0bbb | 2005-04-10 23:13:09 -0700 | [diff] [blame] | 161 | /* Look up all the requirements, warn about missing objects.. */ |
Linus Torvalds | fc046a7 | 2006-06-29 21:38:55 -0700 | [diff] [blame] | 162 | max = get_max_object_index(); |
| 163 | for (i = 0; i < max; i++) { |
Linus Torvalds | fc046a7 | 2006-06-29 21:38:55 -0700 | [diff] [blame] | 164 | struct object *obj = get_indexed_object(i); |
Linus Torvalds | 8ba0bbb | 2005-04-10 23:13:09 -0700 | [diff] [blame] | 165 | |
Linus Torvalds | 18af29f | 2007-01-21 22:26:41 -0800 | [diff] [blame] | 166 | if (obj) |
| 167 | check_object(obj); |
Linus Torvalds | 8ba0bbb | 2005-04-10 23:13:09 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 171 | /* |
| 172 | * The entries in a tree are ordered in the _path_ order, |
| 173 | * which means that a directory entry is ordered by adding |
| 174 | * a slash to the end of it. |
| 175 | * |
| 176 | * So a directory called "a" is ordered _after_ a file |
| 177 | * called "a.c", because "a/" sorts after "a.c". |
| 178 | */ |
Junio C Hamano | a4f35a2 | 2005-05-07 14:43:32 -0700 | [diff] [blame] | 179 | #define TREE_UNORDERED (-1) |
| 180 | #define TREE_HAS_DUPS (-2) |
| 181 | |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 182 | static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2) |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 183 | { |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 184 | int len1 = strlen(name1); |
| 185 | int len2 = strlen(name2); |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 186 | int len = len1 < len2 ? len1 : len2; |
| 187 | unsigned char c1, c2; |
| 188 | int cmp; |
| 189 | |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 190 | cmp = memcmp(name1, name2, len); |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 191 | if (cmp < 0) |
| 192 | return 0; |
| 193 | if (cmp > 0) |
Junio C Hamano | a4f35a2 | 2005-05-07 14:43:32 -0700 | [diff] [blame] | 194 | return TREE_UNORDERED; |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 195 | |
| 196 | /* |
| 197 | * Ok, the first <len> characters are the same. |
| 198 | * Now we need to order the next one, but turn |
| 199 | * a '\0' into a '/' for a directory entry. |
| 200 | */ |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 201 | c1 = name1[len]; |
| 202 | c2 = name2[len]; |
Junio C Hamano | a4f35a2 | 2005-05-07 14:43:32 -0700 | [diff] [blame] | 203 | if (!c1 && !c2) |
| 204 | /* |
| 205 | * git-write-tree used to write out a nonsense tree that has |
| 206 | * entries with the same name, one blob and one tree. Make |
| 207 | * sure we do not have duplicate entries. |
| 208 | */ |
| 209 | return TREE_HAS_DUPS; |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 210 | if (!c1 && S_ISDIR(mode1)) |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 211 | c1 = '/'; |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 212 | if (!c2 && S_ISDIR(mode2)) |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 213 | c2 = '/'; |
Junio C Hamano | a4f35a2 | 2005-05-07 14:43:32 -0700 | [diff] [blame] | 214 | return c1 < c2 ? 0 : TREE_UNORDERED; |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Daniel Barkalow | c418eda | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 217 | static int fsck_tree(struct tree *item) |
Linus Torvalds | 1ea34e3 | 2005-04-08 17:11:14 -0700 | [diff] [blame] | 218 | { |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 219 | int retval; |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 220 | int has_full_path = 0; |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 221 | int has_zero_pad = 0; |
| 222 | int has_bad_modes = 0; |
| 223 | int has_dup_entries = 0; |
| 224 | int not_properly_sorted = 0; |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 225 | struct tree_desc desc; |
| 226 | unsigned o_mode; |
| 227 | const char *o_name; |
| 228 | const unsigned char *o_sha1; |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 229 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 230 | init_tree_desc(&desc, item->buffer, item->size); |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 231 | |
| 232 | o_mode = 0; |
| 233 | o_name = NULL; |
| 234 | o_sha1 = NULL; |
| 235 | while (desc.size) { |
| 236 | unsigned mode; |
| 237 | const char *name; |
| 238 | const unsigned char *sha1; |
| 239 | |
| 240 | sha1 = tree_entry_extract(&desc, &name, &mode); |
| 241 | |
| 242 | if (strchr(name, '/')) |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 243 | has_full_path = 1; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 244 | has_zero_pad |= *(char *)desc.buffer == '0'; |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 245 | update_tree_entry(&desc); |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 246 | |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 247 | switch (mode) { |
Linus Torvalds | 42ea9cb | 2005-05-05 16:18:48 -0700 | [diff] [blame] | 248 | /* |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 249 | * Standard modes.. |
Linus Torvalds | 42ea9cb | 2005-05-05 16:18:48 -0700 | [diff] [blame] | 250 | */ |
| 251 | case S_IFREG | 0755: |
| 252 | case S_IFREG | 0644: |
| 253 | case S_IFLNK: |
| 254 | case S_IFDIR: |
| 255 | break; |
| 256 | /* |
| 257 | * This is nonstandard, but we had a few of these |
| 258 | * early on when we honored the full set of mode |
| 259 | * bits.. |
| 260 | */ |
| 261 | case S_IFREG | 0664: |
Linus Torvalds | de2eb7f | 2005-07-27 15:16:03 -0700 | [diff] [blame] | 262 | if (!check_strict) |
| 263 | break; |
Linus Torvalds | 42ea9cb | 2005-05-05 16:18:48 -0700 | [diff] [blame] | 264 | default: |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 265 | has_bad_modes = 1; |
Linus Torvalds | 42ea9cb | 2005-05-05 16:18:48 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 268 | if (o_name) { |
| 269 | switch (verify_ordered(o_mode, o_name, mode, name)) { |
Junio C Hamano | a4f35a2 | 2005-05-07 14:43:32 -0700 | [diff] [blame] | 270 | case TREE_UNORDERED: |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 271 | not_properly_sorted = 1; |
| 272 | break; |
Junio C Hamano | a4f35a2 | 2005-05-07 14:43:32 -0700 | [diff] [blame] | 273 | case TREE_HAS_DUPS: |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 274 | has_dup_entries = 1; |
| 275 | break; |
Junio C Hamano | a4f35a2 | 2005-05-07 14:43:32 -0700 | [diff] [blame] | 276 | default: |
| 277 | break; |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Linus Torvalds | e9a95be | 2006-05-29 12:19:02 -0700 | [diff] [blame] | 281 | o_mode = mode; |
| 282 | o_name = name; |
| 283 | o_sha1 = sha1; |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 284 | } |
Linus Torvalds | 136f2e5 | 2006-05-29 12:16:12 -0700 | [diff] [blame] | 285 | free(item->buffer); |
| 286 | item->buffer = NULL; |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 287 | |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 288 | retval = 0; |
Linus Torvalds | 8500349 | 2005-05-02 16:13:18 -0700 | [diff] [blame] | 289 | if (has_full_path) { |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 290 | objwarning(&item->object, "contains full pathnames"); |
Linus Torvalds | 1ea34e3 | 2005-04-08 17:11:14 -0700 | [diff] [blame] | 291 | } |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 292 | if (has_zero_pad) { |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 293 | objwarning(&item->object, "contains zero-padded file modes"); |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 294 | } |
| 295 | if (has_bad_modes) { |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 296 | objwarning(&item->object, "contains bad file modes"); |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 297 | } |
| 298 | if (has_dup_entries) { |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 299 | retval = objerror(&item->object, "contains duplicate file entries"); |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 300 | } |
| 301 | if (not_properly_sorted) { |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 302 | retval = objerror(&item->object, "not properly sorted"); |
Linus Torvalds | 6407180 | 2005-07-27 16:08:43 -0700 | [diff] [blame] | 303 | } |
| 304 | return retval; |
Linus Torvalds | 1ea34e3 | 2005-04-08 17:11:14 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Daniel Barkalow | c418eda | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 307 | static int fsck_commit(struct commit *commit) |
Linus Torvalds | 1ea34e3 | 2005-04-08 17:11:14 -0700 | [diff] [blame] | 308 | { |
Linus Torvalds | de2eb7f | 2005-07-27 15:16:03 -0700 | [diff] [blame] | 309 | char *buffer = commit->buffer; |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 310 | unsigned char tree_sha1[20], sha1[20]; |
Linus Torvalds | de2eb7f | 2005-07-27 15:16:03 -0700 | [diff] [blame] | 311 | |
| 312 | if (memcmp(buffer, "tree ", 5)) |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 313 | return objerror(&commit->object, "invalid format - expected 'tree' line"); |
| 314 | if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n') |
| 315 | return objerror(&commit->object, "invalid 'tree' line format - bad sha1"); |
Linus Torvalds | de2eb7f | 2005-07-27 15:16:03 -0700 | [diff] [blame] | 316 | buffer += 46; |
| 317 | while (!memcmp(buffer, "parent ", 7)) { |
| 318 | if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n') |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 319 | return objerror(&commit->object, "invalid 'parent' line format - bad sha1"); |
Linus Torvalds | de2eb7f | 2005-07-27 15:16:03 -0700 | [diff] [blame] | 320 | buffer += 48; |
| 321 | } |
| 322 | if (memcmp(buffer, "author ", 7)) |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 323 | return objerror(&commit->object, "invalid format - expected 'author' line"); |
Linus Torvalds | bd1e17e | 2005-05-25 19:26:28 -0700 | [diff] [blame] | 324 | free(commit->buffer); |
| 325 | commit->buffer = NULL; |
Daniel Barkalow | ff5ebe3 | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 326 | if (!commit->tree) |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 327 | return objerror(&commit->object, "could not load commit's tree %s", tree_sha1); |
Linus Torvalds | ab7df18 | 2005-04-25 16:34:13 -0700 | [diff] [blame] | 328 | if (!commit->parents && show_root) |
Daniel Barkalow | c418eda | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 329 | printf("root %s\n", sha1_to_hex(commit->object.sha1)); |
Linus Torvalds | e6948b6 | 2005-04-24 16:20:53 -0700 | [diff] [blame] | 330 | if (!commit->date) |
Daniel Barkalow | c418eda | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 331 | printf("bad commit date in %s\n", |
| 332 | sha1_to_hex(commit->object.sha1)); |
Linus Torvalds | 59c1e24 | 2005-04-09 00:25:22 -0700 | [diff] [blame] | 333 | return 0; |
Linus Torvalds | 1ea34e3 | 2005-04-08 17:11:14 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Daniel Barkalow | c418eda | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 336 | static int fsck_tag(struct tag *tag) |
Linus Torvalds | 4728b86 | 2005-04-24 14:10:55 -0700 | [diff] [blame] | 337 | { |
Linus Torvalds | 92d4c85 | 2005-05-03 07:57:56 -0700 | [diff] [blame] | 338 | struct object *tagged = tag->tagged; |
| 339 | |
| 340 | if (!tagged) { |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 341 | return objerror(&tag->object, "could not load tagged object"); |
Linus Torvalds | 92d4c85 | 2005-05-03 07:57:56 -0700 | [diff] [blame] | 342 | } |
Linus Torvalds | 889262e | 2005-04-25 16:31:13 -0700 | [diff] [blame] | 343 | if (!show_tags) |
| 344 | return 0; |
| 345 | |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 346 | printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1)); |
Linus Torvalds | 92d4c85 | 2005-05-03 07:57:56 -0700 | [diff] [blame] | 347 | printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1)); |
Daniel Barkalow | ff5ebe3 | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 348 | return 0; |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame^] | 351 | static int fsck_sha1(const unsigned char *sha1) |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 352 | { |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 353 | struct object *obj = parse_object(sha1); |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 354 | if (!obj) { |
| 355 | errors_found |= ERROR_OBJECT; |
| 356 | return error("%s: object corrupt or missing", |
| 357 | sha1_to_hex(sha1)); |
| 358 | } |
Linus Torvalds | 2d9c58c | 2006-05-29 12:18:33 -0700 | [diff] [blame] | 359 | if (obj->flags & SEEN) |
| 360 | return 0; |
| 361 | obj->flags |= SEEN; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 362 | if (obj->type == OBJ_BLOB) |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 363 | return 0; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 364 | if (obj->type == OBJ_TREE) |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 365 | return fsck_tree((struct tree *) obj); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 366 | if (obj->type == OBJ_COMMIT) |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 367 | return fsck_commit((struct commit *) obj); |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 368 | if (obj->type == OBJ_TAG) |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 369 | return fsck_tag((struct tag *) obj); |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 370 | |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 371 | /* By now, parse_object() would've returned NULL instead. */ |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 372 | return objerror(obj, "unknown type '%d' (internal fsck error)", |
| 373 | obj->type); |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 376 | /* |
| 377 | * This is the sorting chunk size: make it reasonably |
| 378 | * big so that we can sort well.. |
| 379 | */ |
| 380 | #define MAX_SHA1_ENTRIES (1024) |
| 381 | |
| 382 | struct sha1_entry { |
| 383 | unsigned long ino; |
| 384 | unsigned char sha1[20]; |
| 385 | }; |
| 386 | |
| 387 | static struct { |
| 388 | unsigned long nr; |
| 389 | struct sha1_entry *entry[MAX_SHA1_ENTRIES]; |
| 390 | } sha1_list; |
| 391 | |
| 392 | static int ino_compare(const void *_a, const void *_b) |
| 393 | { |
| 394 | const struct sha1_entry *a = _a, *b = _b; |
| 395 | unsigned long ino1 = a->ino, ino2 = b->ino; |
| 396 | return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0; |
| 397 | } |
| 398 | |
| 399 | static void fsck_sha1_list(void) |
| 400 | { |
| 401 | int i, nr = sha1_list.nr; |
| 402 | |
Junio C Hamano | 35a730f | 2006-01-19 17:13:51 -0800 | [diff] [blame] | 403 | if (SORT_DIRENT) |
| 404 | qsort(sha1_list.entry, nr, |
| 405 | sizeof(struct sha1_entry *), ino_compare); |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 406 | for (i = 0; i < nr; i++) { |
| 407 | struct sha1_entry *entry = sha1_list.entry[i]; |
| 408 | unsigned char *sha1 = entry->sha1; |
| 409 | |
| 410 | sha1_list.entry[i] = NULL; |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 411 | fsck_sha1(sha1); |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 412 | free(entry); |
| 413 | } |
| 414 | sha1_list.nr = 0; |
| 415 | } |
| 416 | |
| 417 | static void add_sha1_list(unsigned char *sha1, unsigned long ino) |
| 418 | { |
| 419 | struct sha1_entry *entry = xmalloc(sizeof(*entry)); |
| 420 | int nr; |
| 421 | |
| 422 | entry->ino = ino; |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 423 | hashcpy(entry->sha1, sha1); |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 424 | nr = sha1_list.nr; |
| 425 | if (nr == MAX_SHA1_ENTRIES) { |
| 426 | fsck_sha1_list(); |
| 427 | nr = 0; |
| 428 | } |
| 429 | sha1_list.entry[nr] = entry; |
| 430 | sha1_list.nr = ++nr; |
| 431 | } |
| 432 | |
David Rientjes | b5524c8 | 2006-08-14 13:36:18 -0700 | [diff] [blame] | 433 | static void fsck_dir(int i, char *path) |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 434 | { |
| 435 | DIR *dir = opendir(path); |
| 436 | struct dirent *de; |
| 437 | |
Linus Torvalds | 230f132 | 2005-10-08 15:54:01 -0700 | [diff] [blame] | 438 | if (!dir) |
David Rientjes | b5524c8 | 2006-08-14 13:36:18 -0700 | [diff] [blame] | 439 | return; |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 440 | |
| 441 | while ((de = readdir(dir)) != NULL) { |
| 442 | char name[100]; |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 443 | unsigned char sha1[20]; |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 444 | int len = strlen(de->d_name); |
| 445 | |
| 446 | switch (len) { |
| 447 | case 2: |
| 448 | if (de->d_name[1] != '.') |
| 449 | break; |
| 450 | case 1: |
| 451 | if (de->d_name[0] != '.') |
| 452 | break; |
| 453 | continue; |
| 454 | case 38: |
| 455 | sprintf(name, "%02x", i); |
| 456 | memcpy(name+2, de->d_name, len+1); |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 457 | if (get_sha1_hex(name, sha1) < 0) |
| 458 | break; |
Junio C Hamano | 35a730f | 2006-01-19 17:13:51 -0800 | [diff] [blame] | 459 | add_sha1_list(sha1, DIRENT_SORT_HINT(de)); |
Linus Torvalds | 7e8c174 | 2005-05-02 09:06:33 -0700 | [diff] [blame] | 460 | continue; |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 461 | } |
| 462 | fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name); |
| 463 | } |
| 464 | closedir(dir); |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 465 | } |
| 466 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 467 | static int default_refs; |
Linus Torvalds | 944d858 | 2005-07-03 10:01:38 -0700 | [diff] [blame] | 468 | |
Johannes Schindelin | 883d60f | 2007-01-08 01:59:54 +0100 | [diff] [blame] | 469 | static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
| 470 | const char *email, unsigned long timestamp, int tz, |
| 471 | const char *message, void *cb_data) |
Junio C Hamano | 55dd552 | 2006-12-18 01:36:16 -0800 | [diff] [blame] | 472 | { |
| 473 | struct object *obj; |
| 474 | |
| 475 | if (!is_null_sha1(osha1)) { |
| 476 | obj = lookup_object(osha1); |
| 477 | if (obj) { |
| 478 | obj->used = 1; |
| 479 | mark_reachable(obj, REACHABLE); |
| 480 | } |
| 481 | } |
| 482 | obj = lookup_object(nsha1); |
| 483 | if (obj) { |
| 484 | obj->used = 1; |
| 485 | mark_reachable(obj, REACHABLE); |
| 486 | } |
| 487 | return 0; |
| 488 | } |
| 489 | |
Nicolas Pitre | eb8381c | 2007-02-03 13:25:43 -0500 | [diff] [blame] | 490 | static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data) |
| 491 | { |
| 492 | for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL); |
| 493 | return 0; |
| 494 | } |
| 495 | |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 496 | static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
Linus Torvalds | 1024932 | 2005-05-18 10:16:14 -0700 | [diff] [blame] | 497 | { |
Linus Torvalds | 1024932 | 2005-05-18 10:16:14 -0700 | [diff] [blame] | 498 | struct object *obj; |
| 499 | |
Linus Torvalds | 1024932 | 2005-05-18 10:16:14 -0700 | [diff] [blame] | 500 | obj = lookup_object(sha1); |
Junio C Hamano | 8a498a0 | 2005-06-28 14:58:33 -0700 | [diff] [blame] | 501 | if (!obj) { |
Junio C Hamano | 7aaa715 | 2006-03-09 01:44:19 -0800 | [diff] [blame] | 502 | if (has_sha1_file(sha1)) { |
Linus Torvalds | 659cacf | 2005-07-07 17:05:41 -0700 | [diff] [blame] | 503 | default_refs++; |
Linus Torvalds | 944d858 | 2005-07-03 10:01:38 -0700 | [diff] [blame] | 504 | return 0; /* it is in a pack */ |
Linus Torvalds | 659cacf | 2005-07-07 17:05:41 -0700 | [diff] [blame] | 505 | } |
Linus Torvalds | 944d858 | 2005-07-03 10:01:38 -0700 | [diff] [blame] | 506 | error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1)); |
| 507 | /* We'll continue with the rest despite the error.. */ |
| 508 | return 0; |
Junio C Hamano | 8a498a0 | 2005-06-28 14:58:33 -0700 | [diff] [blame] | 509 | } |
Linus Torvalds | 944d858 | 2005-07-03 10:01:38 -0700 | [diff] [blame] | 510 | default_refs++; |
Linus Torvalds | 1024932 | 2005-05-18 10:16:14 -0700 | [diff] [blame] | 511 | obj->used = 1; |
| 512 | mark_reachable(obj, REACHABLE); |
Junio C Hamano | 55dd552 | 2006-12-18 01:36:16 -0800 | [diff] [blame] | 513 | |
Linus Torvalds | 7c4d07c | 2005-05-20 07:49:17 -0700 | [diff] [blame] | 514 | return 0; |
Linus Torvalds | 1024932 | 2005-05-18 10:16:14 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Linus Torvalds | 1024932 | 2005-05-18 10:16:14 -0700 | [diff] [blame] | 517 | static void get_default_heads(void) |
| 518 | { |
Junio C Hamano | cb5d709 | 2006-09-20 21:47:42 -0700 | [diff] [blame] | 519 | for_each_ref(fsck_handle_ref, NULL); |
Nicolas Pitre | eb8381c | 2007-02-03 13:25:43 -0500 | [diff] [blame] | 520 | for_each_reflog(fsck_handle_reflog, NULL); |
Linus Torvalds | 071fa89 | 2006-08-29 11:47:30 -0700 | [diff] [blame] | 521 | |
| 522 | /* |
| 523 | * Not having any default heads isn't really fatal, but |
| 524 | * it does mean that "--unreachable" no longer makes any |
| 525 | * sense (since in this case everything will obviously |
| 526 | * be unreachable by definition. |
| 527 | * |
| 528 | * Showing dangling objects is valid, though (as those |
| 529 | * dangling objects are likely lost heads). |
| 530 | * |
| 531 | * So we just print a warning about it, and clear the |
| 532 | * "show_unreachable" flag. |
| 533 | */ |
| 534 | if (!default_refs) { |
| 535 | error("No default references"); |
| 536 | show_unreachable = 0; |
| 537 | } |
Linus Torvalds | 1024932 | 2005-05-18 10:16:14 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Junio C Hamano | 8a498a0 | 2005-06-28 14:58:33 -0700 | [diff] [blame] | 540 | static void fsck_object_dir(const char *path) |
| 541 | { |
| 542 | int i; |
| 543 | for (i = 0; i < 256; i++) { |
| 544 | static char dir[4096]; |
| 545 | sprintf(dir, "%s/%02x", path, i); |
| 546 | fsck_dir(i, dir); |
| 547 | } |
| 548 | fsck_sha1_list(); |
| 549 | } |
| 550 | |
Linus Torvalds | c333038 | 2005-07-03 10:40:38 -0700 | [diff] [blame] | 551 | static int fsck_head_link(void) |
| 552 | { |
Linus Torvalds | c333038 | 2005-07-03 10:40:38 -0700 | [diff] [blame] | 553 | unsigned char sha1[20]; |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 554 | int flag; |
| 555 | const char *head_points_at = resolve_ref("HEAD", sha1, 1, &flag); |
Linus Torvalds | c333038 | 2005-07-03 10:40:38 -0700 | [diff] [blame] | 556 | |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 557 | if (!head_points_at || !(flag & REF_ISSYMREF)) |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 558 | return error("HEAD is not a symbolic ref"); |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 559 | if (prefixcmp(head_points_at, "refs/heads/")) |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 560 | return error("HEAD points to something strange (%s)", |
Junio C Hamano | 5b10b09 | 2006-09-18 01:08:00 -0700 | [diff] [blame] | 561 | head_points_at); |
David Rientjes | 0bef57e | 2006-08-15 13:37:19 -0700 | [diff] [blame] | 562 | if (is_null_sha1(sha1)) |
Linus Torvalds | c333038 | 2005-07-03 10:40:38 -0700 | [diff] [blame] | 563 | return error("HEAD: not a valid git pointer"); |
| 564 | return 0; |
| 565 | } |
| 566 | |
Junio C Hamano | 53dc3f3 | 2006-04-25 16:37:08 -0700 | [diff] [blame] | 567 | static int fsck_cache_tree(struct cache_tree *it) |
| 568 | { |
| 569 | int i; |
| 570 | int err = 0; |
| 571 | |
| 572 | if (0 <= it->entry_count) { |
| 573 | struct object *obj = parse_object(it->sha1); |
Junio C Hamano | 6d60bbe | 2006-05-03 21:17:45 -0700 | [diff] [blame] | 574 | if (!obj) { |
| 575 | error("%s: invalid sha1 pointer in cache-tree", |
| 576 | sha1_to_hex(it->sha1)); |
| 577 | return 1; |
| 578 | } |
Junio C Hamano | cdc08b3 | 2006-05-01 22:15:54 -0700 | [diff] [blame] | 579 | mark_reachable(obj, REACHABLE); |
| 580 | obj->used = 1; |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 581 | if (obj->type != OBJ_TREE) |
Junio C Hamano | 53dc3f3 | 2006-04-25 16:37:08 -0700 | [diff] [blame] | 582 | err |= objerror(obj, "non-tree in cache-tree"); |
| 583 | } |
| 584 | for (i = 0; i < it->subtree_nr; i++) |
| 585 | err |= fsck_cache_tree(it->down[i]->cache_tree); |
| 586 | return err; |
| 587 | } |
| 588 | |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 589 | static const char fsck_usage[] = |
| 590 | "git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] " |
| 591 | "[--strict] <head-sha1>*]"; |
| 592 | |
Mark Wooding | b4dfefe | 2007-01-29 15:48:06 +0000 | [diff] [blame] | 593 | int cmd_fsck(int argc, char **argv, const char *prefix) |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 594 | { |
Linus Torvalds | bcee6fd | 2005-04-13 16:42:09 -0700 | [diff] [blame] | 595 | int i, heads; |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 596 | |
Linus Torvalds | 3a7c352 | 2006-05-29 12:16:46 -0700 | [diff] [blame] | 597 | track_object_refs = 1; |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 598 | errors_found = 0; |
Junio C Hamano | 61e2b01 | 2005-11-25 23:52:04 -0800 | [diff] [blame] | 599 | |
Linus Torvalds | 889262e | 2005-04-25 16:31:13 -0700 | [diff] [blame] | 600 | for (i = 1; i < argc; i++) { |
| 601 | const char *arg = argv[i]; |
| 602 | |
| 603 | if (!strcmp(arg, "--unreachable")) { |
| 604 | show_unreachable = 1; |
| 605 | continue; |
| 606 | } |
| 607 | if (!strcmp(arg, "--tags")) { |
| 608 | show_tags = 1; |
| 609 | continue; |
| 610 | } |
Linus Torvalds | ab7df18 | 2005-04-25 16:34:13 -0700 | [diff] [blame] | 611 | if (!strcmp(arg, "--root")) { |
| 612 | show_root = 1; |
| 613 | continue; |
| 614 | } |
Junio C Hamano | ae7c0c9 | 2005-05-04 01:33:33 -0700 | [diff] [blame] | 615 | if (!strcmp(arg, "--cache")) { |
| 616 | keep_cache_objects = 1; |
| 617 | continue; |
| 618 | } |
Junio C Hamano | 8a498a0 | 2005-06-28 14:58:33 -0700 | [diff] [blame] | 619 | if (!strcmp(arg, "--full")) { |
| 620 | check_full = 1; |
| 621 | continue; |
| 622 | } |
Linus Torvalds | de2eb7f | 2005-07-27 15:16:03 -0700 | [diff] [blame] | 623 | if (!strcmp(arg, "--strict")) { |
| 624 | check_strict = 1; |
| 625 | continue; |
| 626 | } |
Linus Torvalds | 889262e | 2005-04-25 16:31:13 -0700 | [diff] [blame] | 627 | if (*arg == '-') |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 628 | usage(fsck_usage); |
Linus Torvalds | 889262e | 2005-04-25 16:31:13 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Linus Torvalds | c333038 | 2005-07-03 10:40:38 -0700 | [diff] [blame] | 631 | fsck_head_link(); |
Junio C Hamano | 8a498a0 | 2005-06-28 14:58:33 -0700 | [diff] [blame] | 632 | fsck_object_dir(get_object_directory()); |
| 633 | if (check_full) { |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 634 | struct alternate_object_database *alt; |
Junio C Hamano | 8a498a0 | 2005-06-28 14:58:33 -0700 | [diff] [blame] | 635 | struct packed_git *p; |
| 636 | prepare_alt_odb(); |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 637 | for (alt = alt_odb_list; alt; alt = alt->next) { |
Junio C Hamano | a3eb250 | 2005-07-10 15:40:43 -0700 | [diff] [blame] | 638 | char namebuf[PATH_MAX]; |
Junio C Hamano | d5a63b9 | 2005-08-14 17:25:57 -0700 | [diff] [blame] | 639 | int namelen = alt->name - alt->base; |
| 640 | memcpy(namebuf, alt->base, namelen); |
Junio C Hamano | a3eb250 | 2005-07-10 15:40:43 -0700 | [diff] [blame] | 641 | namebuf[namelen - 1] = 0; |
| 642 | fsck_object_dir(namebuf); |
Junio C Hamano | 8a498a0 | 2005-06-28 14:58:33 -0700 | [diff] [blame] | 643 | } |
| 644 | prepare_packed_git(); |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 645 | for (p = packed_git; p; p = p->next) |
| 646 | /* verify gives error messages itself */ |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 647 | verify_pack(p, 0); |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 648 | |
Junio C Hamano | 8a498a0 | 2005-06-28 14:58:33 -0700 | [diff] [blame] | 649 | for (p = packed_git; p; p = p->next) { |
Shawn O. Pearce | 326bf39 | 2007-03-06 20:44:19 -0500 | [diff] [blame] | 650 | uint32_t i, num = num_packed_objects(p); |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame^] | 651 | for (i = 0; i < num; i++) |
| 652 | fsck_sha1(nth_packed_object_sha1(p, i)); |
Junio C Hamano | 8a498a0 | 2005-06-28 14:58:33 -0700 | [diff] [blame] | 653 | } |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 654 | } |
Linus Torvalds | bcee6fd | 2005-04-13 16:42:09 -0700 | [diff] [blame] | 655 | |
| 656 | heads = 0; |
| 657 | for (i = 1; i < argc; i++) { |
Linus Torvalds | 889262e | 2005-04-25 16:31:13 -0700 | [diff] [blame] | 658 | const char *arg = argv[i]; |
| 659 | |
| 660 | if (*arg == '-') |
Linus Torvalds | bcee6fd | 2005-04-13 16:42:09 -0700 | [diff] [blame] | 661 | continue; |
Linus Torvalds | 889262e | 2005-04-25 16:31:13 -0700 | [diff] [blame] | 662 | |
Linus Torvalds | 3c249c9 | 2005-05-01 16:36:56 -0700 | [diff] [blame] | 663 | if (!get_sha1(arg, head_sha1)) { |
Linus Torvalds | 770896e | 2005-05-04 17:03:09 -0700 | [diff] [blame] | 664 | struct object *obj = lookup_object(head_sha1); |
Jonas Fonseca | e1a1388 | 2005-04-29 20:00:40 -0700 | [diff] [blame] | 665 | |
Linus Torvalds | 770896e | 2005-05-04 17:03:09 -0700 | [diff] [blame] | 666 | /* Error is printed by lookup_object(). */ |
| 667 | if (!obj) |
Jonas Fonseca | e1a1388 | 2005-04-29 20:00:40 -0700 | [diff] [blame] | 668 | continue; |
| 669 | |
Daniel Barkalow | ff5ebe3 | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 670 | obj->used = 1; |
| 671 | mark_reachable(obj, REACHABLE); |
Linus Torvalds | bcee6fd | 2005-04-13 16:42:09 -0700 | [diff] [blame] | 672 | heads++; |
| 673 | continue; |
| 674 | } |
Petr Baudis | f1f0d08 | 2005-09-20 20:56:05 +0200 | [diff] [blame] | 675 | error("invalid parameter: expected sha1, got '%s'", arg); |
Linus Torvalds | bcee6fd | 2005-04-13 16:42:09 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Linus Torvalds | 1024932 | 2005-05-18 10:16:14 -0700 | [diff] [blame] | 678 | /* |
Nicolas Pitre | d1af002 | 2005-05-20 16:59:17 -0400 | [diff] [blame] | 679 | * If we've not been given any explicit head information, do the |
Linus Torvalds | e7bd907 | 2005-05-18 10:19:59 -0700 | [diff] [blame] | 680 | * default ones from .git/refs. We also consider the index file |
| 681 | * in this case (ie this implies --cache). |
Linus Torvalds | 1024932 | 2005-05-18 10:16:14 -0700 | [diff] [blame] | 682 | */ |
Linus Torvalds | e7bd907 | 2005-05-18 10:19:59 -0700 | [diff] [blame] | 683 | if (!heads) { |
Linus Torvalds | 1024932 | 2005-05-18 10:16:14 -0700 | [diff] [blame] | 684 | get_default_heads(); |
| 685 | keep_cache_objects = 1; |
| 686 | } |
| 687 | |
Junio C Hamano | ae7c0c9 | 2005-05-04 01:33:33 -0700 | [diff] [blame] | 688 | if (keep_cache_objects) { |
| 689 | int i; |
| 690 | read_cache(); |
| 691 | for (i = 0; i < active_nr; i++) { |
| 692 | struct blob *blob = lookup_blob(active_cache[i]->sha1); |
| 693 | struct object *obj; |
| 694 | if (!blob) |
| 695 | continue; |
| 696 | obj = &blob->object; |
| 697 | obj->used = 1; |
| 698 | mark_reachable(obj, REACHABLE); |
| 699 | } |
Junio C Hamano | 53dc3f3 | 2006-04-25 16:37:08 -0700 | [diff] [blame] | 700 | if (active_cache_tree) |
| 701 | fsck_cache_tree(active_cache_tree); |
Junio C Hamano | ae7c0c9 | 2005-05-04 01:33:33 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Linus Torvalds | 8ba0bbb | 2005-04-10 23:13:09 -0700 | [diff] [blame] | 704 | check_connectivity(); |
Junio C Hamano | e2b4f63 | 2007-03-05 00:22:06 -0800 | [diff] [blame] | 705 | return errors_found; |
Linus Torvalds | 2022211 | 2005-04-08 15:02:42 -0700 | [diff] [blame] | 706 | } |