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