blob: 4e5aa33dfb9921e4be98ea893cea34fc66e35735 [file] [log] [blame]
Linus Torvalds4b182422005-04-30 09:59:31 -07001#include "cache.h"
Daniel Barkalowff5ebe32005-04-18 11:39:48 -07002#include "commit.h"
3#include "tree.h"
4#include "blob.h"
Daniel Barkalowc418eda2005-04-28 07:46:33 -07005#include "tag.h"
Linus Torvalds944d8582005-07-03 10:01:38 -07006#include "refs.h"
Junio C Hamanof9253392005-06-29 02:51:27 -07007#include "pack.h"
Junio C Hamano53dc3f32006-04-25 16:37:08 -07008#include "cache-tree.h"
Linus Torvaldse9a95be2006-05-29 12:19:02 -07009#include "tree-walk.h"
Daniel Barkalowff5ebe32005-04-18 11:39:48 -070010
11#define REACHABLE 0x0001
Linus Torvalds2d9c58c2006-05-29 12:18:33 -070012#define SEEN 0x0002
Linus Torvaldsd9839e02005-04-13 09:57:30 -070013
David Rientjes96f1e582006-08-15 10:23:48 -070014static int show_root;
15static int show_tags;
16static int show_unreachable;
17static int check_full;
18static int check_strict;
19static int keep_cache_objects;
Linus Torvaldsd9839e02005-04-13 09:57:30 -070020static unsigned char head_sha1[20];
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080021static int errors_found;
22#define ERROR_OBJECT 01
23#define ERROR_REACHABLE 02
Linus Torvaldsd9839e02005-04-13 09:57:30 -070024
Timo Hirvonen962554c2006-02-26 17:13:46 +020025#ifdef NO_D_INO_IN_DIRENT
Junio C Hamano35a730f2006-01-19 17:13:51 -080026#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 Baudisf1f0d082005-09-20 20:56:05 +020032
33static void objreport(struct object *obj, const char *severity,
34 const char *err, va_list params)
35{
36 fprintf(stderr, "%s in %s %s: ",
Linus Torvalds885a86a2006-06-14 16:45:13 -070037 severity, typename(obj->type), sha1_to_hex(obj->sha1));
Petr Baudisf1f0d082005-09-20 20:56:05 +020038 vfprintf(stderr, err, params);
39 fputs("\n", stderr);
40}
41
Peter Hagervalla7928f82005-09-28 14:04:54 +020042static int objerror(struct object *obj, const char *err, ...)
Petr Baudisf1f0d082005-09-20 20:56:05 +020043{
44 va_list params;
45 va_start(params, err);
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080046 errors_found |= ERROR_OBJECT;
Petr Baudisf1f0d082005-09-20 20:56:05 +020047 objreport(obj, "error", err, params);
48 va_end(params);
49 return -1;
50}
51
Peter Hagervalla7928f82005-09-28 14:04:54 +020052static int objwarning(struct object *obj, const char *err, ...)
Petr Baudisf1f0d082005-09-20 20:56:05 +020053{
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 Torvalds18af29f2007-01-21 22:26:41 -080061/*
62 * Check a single reachable object
63 */
64static 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 Hamanoefec43c2007-03-05 00:21:24 -080074 if (has_sha1_pack(obj->sha1, NULL))
Linus Torvalds18af29f2007-01-21 22:26:41 -080075 return; /* it is in pack - forget about it */
76 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080077 errors_found |= ERROR_REACHABLE;
Linus Torvalds18af29f2007-01-21 22:26:41 -080078 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 Hamanoe2b4f632007-03-05 00:22:06 -080096 errors_found |= ERROR_REACHABLE;
Linus Torvalds18af29f2007-01-21 22:26:41 -080097 }
98 }
99}
100
101/*
102 * Check a single unreachable object
103 */
104static 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 Roskin3dff5372007-02-03 23:49:16 -0500126 * other unreachable objects. In other words, it's the "tip"
Linus Torvalds18af29f2007-01-21 22:26:41 -0800127 * 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
149static 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 Baudisf1f0d082005-09-20 20:56:05 +0200156
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700157static void check_connectivity(void)
158{
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700159 int i, max;
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700160
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700161 /* Look up all the requirements, warn about missing objects.. */
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700162 max = get_max_object_index();
163 for (i = 0; i < max; i++) {
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700164 struct object *obj = get_indexed_object(i);
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700165
Linus Torvalds18af29f2007-01-21 22:26:41 -0800166 if (obj)
167 check_object(obj);
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700168 }
169}
170
Linus Torvalds85003492005-05-02 16:13:18 -0700171/*
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 Hamanoa4f35a22005-05-07 14:43:32 -0700179#define TREE_UNORDERED (-1)
180#define TREE_HAS_DUPS (-2)
181
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700182static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
Linus Torvalds85003492005-05-02 16:13:18 -0700183{
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700184 int len1 = strlen(name1);
185 int len2 = strlen(name2);
Linus Torvalds85003492005-05-02 16:13:18 -0700186 int len = len1 < len2 ? len1 : len2;
187 unsigned char c1, c2;
188 int cmp;
189
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700190 cmp = memcmp(name1, name2, len);
Linus Torvalds85003492005-05-02 16:13:18 -0700191 if (cmp < 0)
192 return 0;
193 if (cmp > 0)
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700194 return TREE_UNORDERED;
Linus Torvalds85003492005-05-02 16:13:18 -0700195
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 Torvaldse9a95be2006-05-29 12:19:02 -0700201 c1 = name1[len];
202 c2 = name2[len];
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700203 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 Torvaldse9a95be2006-05-29 12:19:02 -0700210 if (!c1 && S_ISDIR(mode1))
Linus Torvalds85003492005-05-02 16:13:18 -0700211 c1 = '/';
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700212 if (!c2 && S_ISDIR(mode2))
Linus Torvalds85003492005-05-02 16:13:18 -0700213 c2 = '/';
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700214 return c1 < c2 ? 0 : TREE_UNORDERED;
Linus Torvalds85003492005-05-02 16:13:18 -0700215}
216
Daniel Barkalowc418eda2005-04-28 07:46:33 -0700217static int fsck_tree(struct tree *item)
Linus Torvalds1ea34e32005-04-08 17:11:14 -0700218{
Linus Torvalds64071802005-07-27 16:08:43 -0700219 int retval;
Linus Torvalds85003492005-05-02 16:13:18 -0700220 int has_full_path = 0;
Linus Torvalds64071802005-07-27 16:08:43 -0700221 int has_zero_pad = 0;
222 int has_bad_modes = 0;
223 int has_dup_entries = 0;
224 int not_properly_sorted = 0;
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700225 struct tree_desc desc;
226 unsigned o_mode;
227 const char *o_name;
228 const unsigned char *o_sha1;
Linus Torvalds85003492005-05-02 16:13:18 -0700229
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700230 init_tree_desc(&desc, item->buffer, item->size);
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700231
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 Torvalds85003492005-05-02 16:13:18 -0700243 has_full_path = 1;
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700244 has_zero_pad |= *(char *)desc.buffer == '0';
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700245 update_tree_entry(&desc);
Linus Torvalds85003492005-05-02 16:13:18 -0700246
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700247 switch (mode) {
Linus Torvalds42ea9cb2005-05-05 16:18:48 -0700248 /*
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700249 * Standard modes..
Linus Torvalds42ea9cb2005-05-05 16:18:48 -0700250 */
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 Torvaldsde2eb7f2005-07-27 15:16:03 -0700262 if (!check_strict)
263 break;
Linus Torvalds42ea9cb2005-05-05 16:18:48 -0700264 default:
Linus Torvalds64071802005-07-27 16:08:43 -0700265 has_bad_modes = 1;
Linus Torvalds42ea9cb2005-05-05 16:18:48 -0700266 }
267
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700268 if (o_name) {
269 switch (verify_ordered(o_mode, o_name, mode, name)) {
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700270 case TREE_UNORDERED:
Linus Torvalds64071802005-07-27 16:08:43 -0700271 not_properly_sorted = 1;
272 break;
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700273 case TREE_HAS_DUPS:
Linus Torvalds64071802005-07-27 16:08:43 -0700274 has_dup_entries = 1;
275 break;
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700276 default:
277 break;
Linus Torvalds85003492005-05-02 16:13:18 -0700278 }
279 }
280
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700281 o_mode = mode;
282 o_name = name;
283 o_sha1 = sha1;
Linus Torvalds85003492005-05-02 16:13:18 -0700284 }
Linus Torvalds136f2e52006-05-29 12:16:12 -0700285 free(item->buffer);
286 item->buffer = NULL;
Linus Torvalds85003492005-05-02 16:13:18 -0700287
Linus Torvalds64071802005-07-27 16:08:43 -0700288 retval = 0;
Linus Torvalds85003492005-05-02 16:13:18 -0700289 if (has_full_path) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200290 objwarning(&item->object, "contains full pathnames");
Linus Torvalds1ea34e32005-04-08 17:11:14 -0700291 }
Linus Torvalds64071802005-07-27 16:08:43 -0700292 if (has_zero_pad) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200293 objwarning(&item->object, "contains zero-padded file modes");
Linus Torvalds64071802005-07-27 16:08:43 -0700294 }
295 if (has_bad_modes) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200296 objwarning(&item->object, "contains bad file modes");
Linus Torvalds64071802005-07-27 16:08:43 -0700297 }
298 if (has_dup_entries) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200299 retval = objerror(&item->object, "contains duplicate file entries");
Linus Torvalds64071802005-07-27 16:08:43 -0700300 }
301 if (not_properly_sorted) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200302 retval = objerror(&item->object, "not properly sorted");
Linus Torvalds64071802005-07-27 16:08:43 -0700303 }
304 return retval;
Linus Torvalds1ea34e32005-04-08 17:11:14 -0700305}
306
Daniel Barkalowc418eda2005-04-28 07:46:33 -0700307static int fsck_commit(struct commit *commit)
Linus Torvalds1ea34e32005-04-08 17:11:14 -0700308{
Linus Torvaldsde2eb7f2005-07-27 15:16:03 -0700309 char *buffer = commit->buffer;
Petr Baudisf1f0d082005-09-20 20:56:05 +0200310 unsigned char tree_sha1[20], sha1[20];
Linus Torvaldsde2eb7f2005-07-27 15:16:03 -0700311
312 if (memcmp(buffer, "tree ", 5))
Petr Baudisf1f0d082005-09-20 20:56:05 +0200313 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 Torvaldsde2eb7f2005-07-27 15:16:03 -0700316 buffer += 46;
317 while (!memcmp(buffer, "parent ", 7)) {
318 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
Petr Baudisf1f0d082005-09-20 20:56:05 +0200319 return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
Linus Torvaldsde2eb7f2005-07-27 15:16:03 -0700320 buffer += 48;
321 }
322 if (memcmp(buffer, "author ", 7))
Petr Baudisf1f0d082005-09-20 20:56:05 +0200323 return objerror(&commit->object, "invalid format - expected 'author' line");
Linus Torvaldsbd1e17e2005-05-25 19:26:28 -0700324 free(commit->buffer);
325 commit->buffer = NULL;
Daniel Barkalowff5ebe32005-04-18 11:39:48 -0700326 if (!commit->tree)
Petr Baudisf1f0d082005-09-20 20:56:05 +0200327 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
Linus Torvaldsab7df182005-04-25 16:34:13 -0700328 if (!commit->parents && show_root)
Daniel Barkalowc418eda2005-04-28 07:46:33 -0700329 printf("root %s\n", sha1_to_hex(commit->object.sha1));
Linus Torvaldse6948b62005-04-24 16:20:53 -0700330 if (!commit->date)
Daniel Barkalowc418eda2005-04-28 07:46:33 -0700331 printf("bad commit date in %s\n",
332 sha1_to_hex(commit->object.sha1));
Linus Torvalds59c1e242005-04-09 00:25:22 -0700333 return 0;
Linus Torvalds1ea34e32005-04-08 17:11:14 -0700334}
335
Daniel Barkalowc418eda2005-04-28 07:46:33 -0700336static int fsck_tag(struct tag *tag)
Linus Torvalds4728b862005-04-24 14:10:55 -0700337{
Linus Torvalds92d4c852005-05-03 07:57:56 -0700338 struct object *tagged = tag->tagged;
339
340 if (!tagged) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200341 return objerror(&tag->object, "could not load tagged object");
Linus Torvalds92d4c852005-05-03 07:57:56 -0700342 }
Linus Torvalds889262e2005-04-25 16:31:13 -0700343 if (!show_tags)
344 return 0;
345
Linus Torvalds885a86a2006-06-14 16:45:13 -0700346 printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1));
Linus Torvalds92d4c852005-05-03 07:57:56 -0700347 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
Daniel Barkalowff5ebe32005-04-18 11:39:48 -0700348 return 0;
Linus Torvalds20222112005-04-08 15:02:42 -0700349}
350
Nicolas Pitred72308e2007-04-04 16:49:04 -0400351static int fsck_sha1(const unsigned char *sha1)
Linus Torvalds20222112005-04-08 15:02:42 -0700352{
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700353 struct object *obj = parse_object(sha1);
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800354 if (!obj) {
355 errors_found |= ERROR_OBJECT;
356 return error("%s: object corrupt or missing",
357 sha1_to_hex(sha1));
358 }
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700359 if (obj->flags & SEEN)
360 return 0;
361 obj->flags |= SEEN;
Linus Torvalds19746322006-07-11 20:45:31 -0700362 if (obj->type == OBJ_BLOB)
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700363 return 0;
Linus Torvalds19746322006-07-11 20:45:31 -0700364 if (obj->type == OBJ_TREE)
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700365 return fsck_tree((struct tree *) obj);
Linus Torvalds19746322006-07-11 20:45:31 -0700366 if (obj->type == OBJ_COMMIT)
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700367 return fsck_commit((struct commit *) obj);
Linus Torvalds19746322006-07-11 20:45:31 -0700368 if (obj->type == OBJ_TAG)
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700369 return fsck_tag((struct tag *) obj);
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800370
Petr Baudisf1f0d082005-09-20 20:56:05 +0200371 /* By now, parse_object() would've returned NULL instead. */
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800372 return objerror(obj, "unknown type '%d' (internal fsck error)",
373 obj->type);
Linus Torvalds20222112005-04-08 15:02:42 -0700374}
375
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700376/*
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
382struct sha1_entry {
383 unsigned long ino;
384 unsigned char sha1[20];
385};
386
387static struct {
388 unsigned long nr;
389 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
390} sha1_list;
391
392static 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
399static void fsck_sha1_list(void)
400{
401 int i, nr = sha1_list.nr;
402
Junio C Hamano35a730f2006-01-19 17:13:51 -0800403 if (SORT_DIRENT)
404 qsort(sha1_list.entry, nr,
405 sizeof(struct sha1_entry *), ino_compare);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700406 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 Baudisf1f0d082005-09-20 20:56:05 +0200411 fsck_sha1(sha1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700412 free(entry);
413 }
414 sha1_list.nr = 0;
415}
416
417static 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 Pearcee7024962006-08-23 02:49:00 -0400423 hashcpy(entry->sha1, sha1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700424 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 Rientjesb5524c82006-08-14 13:36:18 -0700433static void fsck_dir(int i, char *path)
Linus Torvalds20222112005-04-08 15:02:42 -0700434{
435 DIR *dir = opendir(path);
436 struct dirent *de;
437
Linus Torvalds230f1322005-10-08 15:54:01 -0700438 if (!dir)
David Rientjesb5524c82006-08-14 13:36:18 -0700439 return;
Linus Torvalds20222112005-04-08 15:02:42 -0700440
441 while ((de = readdir(dir)) != NULL) {
442 char name[100];
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700443 unsigned char sha1[20];
Linus Torvalds20222112005-04-08 15:02:42 -0700444 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 Torvalds7e8c1742005-05-02 09:06:33 -0700457 if (get_sha1_hex(name, sha1) < 0)
458 break;
Junio C Hamano35a730f2006-01-19 17:13:51 -0800459 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700460 continue;
Linus Torvalds20222112005-04-08 15:02:42 -0700461 }
462 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
463 }
464 closedir(dir);
Linus Torvalds20222112005-04-08 15:02:42 -0700465}
466
David Rientjes96f1e582006-08-15 10:23:48 -0700467static int default_refs;
Linus Torvalds944d8582005-07-03 10:01:38 -0700468
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100469static 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 Hamano55dd5522006-12-18 01:36:16 -0800472{
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 Pitreeb8381c2007-02-03 13:25:43 -0500490static 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 Hamano8da19772006-09-20 22:02:01 -0700496static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
Linus Torvalds10249322005-05-18 10:16:14 -0700497{
Linus Torvalds10249322005-05-18 10:16:14 -0700498 struct object *obj;
499
Linus Torvalds10249322005-05-18 10:16:14 -0700500 obj = lookup_object(sha1);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700501 if (!obj) {
Junio C Hamano7aaa7152006-03-09 01:44:19 -0800502 if (has_sha1_file(sha1)) {
Linus Torvalds659cacf2005-07-07 17:05:41 -0700503 default_refs++;
Linus Torvalds944d8582005-07-03 10:01:38 -0700504 return 0; /* it is in a pack */
Linus Torvalds659cacf2005-07-07 17:05:41 -0700505 }
Linus Torvalds944d8582005-07-03 10:01:38 -0700506 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 Hamano8a498a02005-06-28 14:58:33 -0700509 }
Linus Torvalds944d8582005-07-03 10:01:38 -0700510 default_refs++;
Linus Torvalds10249322005-05-18 10:16:14 -0700511 obj->used = 1;
512 mark_reachable(obj, REACHABLE);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800513
Linus Torvalds7c4d07c2005-05-20 07:49:17 -0700514 return 0;
Linus Torvalds10249322005-05-18 10:16:14 -0700515}
516
Linus Torvalds10249322005-05-18 10:16:14 -0700517static void get_default_heads(void)
518{
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700519 for_each_ref(fsck_handle_ref, NULL);
Nicolas Pitreeb8381c2007-02-03 13:25:43 -0500520 for_each_reflog(fsck_handle_reflog, NULL);
Linus Torvalds071fa892006-08-29 11:47:30 -0700521
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 Torvalds10249322005-05-18 10:16:14 -0700538}
539
Junio C Hamano8a498a02005-06-28 14:58:33 -0700540static 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 Torvaldsc3330382005-07-03 10:40:38 -0700551static int fsck_head_link(void)
552{
Linus Torvaldsc3330382005-07-03 10:40:38 -0700553 unsigned char sha1[20];
Junio C Hamano8da19772006-09-20 22:02:01 -0700554 int flag;
555 const char *head_points_at = resolve_ref("HEAD", sha1, 1, &flag);
Linus Torvaldsc3330382005-07-03 10:40:38 -0700556
Junio C Hamano8da19772006-09-20 22:02:01 -0700557 if (!head_points_at || !(flag & REF_ISSYMREF))
Junio C Hamano8098a172005-09-30 14:26:57 -0700558 return error("HEAD is not a symbolic ref");
Junio C Hamanocc44c762007-02-20 01:53:29 -0800559 if (prefixcmp(head_points_at, "refs/heads/"))
Junio C Hamano8098a172005-09-30 14:26:57 -0700560 return error("HEAD points to something strange (%s)",
Junio C Hamano5b10b092006-09-18 01:08:00 -0700561 head_points_at);
David Rientjes0bef57e2006-08-15 13:37:19 -0700562 if (is_null_sha1(sha1))
Linus Torvaldsc3330382005-07-03 10:40:38 -0700563 return error("HEAD: not a valid git pointer");
564 return 0;
565}
566
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700567static 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 Hamano6d60bbe2006-05-03 21:17:45 -0700574 if (!obj) {
575 error("%s: invalid sha1 pointer in cache-tree",
576 sha1_to_hex(it->sha1));
577 return 1;
578 }
Junio C Hamanocdc08b32006-05-01 22:15:54 -0700579 mark_reachable(obj, REACHABLE);
580 obj->used = 1;
Linus Torvalds19746322006-07-11 20:45:31 -0700581 if (obj->type != OBJ_TREE)
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700582 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 Hamanoe2b4f632007-03-05 00:22:06 -0800589static const char fsck_usage[] =
590"git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
591"[--strict] <head-sha1>*]";
592
Mark Woodingb4dfefe2007-01-29 15:48:06 +0000593int cmd_fsck(int argc, char **argv, const char *prefix)
Linus Torvalds20222112005-04-08 15:02:42 -0700594{
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700595 int i, heads;
Linus Torvalds20222112005-04-08 15:02:42 -0700596
Linus Torvalds3a7c3522006-05-29 12:16:46 -0700597 track_object_refs = 1;
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800598 errors_found = 0;
Junio C Hamano61e2b012005-11-25 23:52:04 -0800599
Linus Torvalds889262e2005-04-25 16:31:13 -0700600 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 Torvaldsab7df182005-04-25 16:34:13 -0700611 if (!strcmp(arg, "--root")) {
612 show_root = 1;
613 continue;
614 }
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700615 if (!strcmp(arg, "--cache")) {
616 keep_cache_objects = 1;
617 continue;
618 }
Junio C Hamano8a498a02005-06-28 14:58:33 -0700619 if (!strcmp(arg, "--full")) {
620 check_full = 1;
621 continue;
622 }
Linus Torvaldsde2eb7f2005-07-27 15:16:03 -0700623 if (!strcmp(arg, "--strict")) {
624 check_strict = 1;
625 continue;
626 }
Linus Torvalds889262e2005-04-25 16:31:13 -0700627 if (*arg == '-')
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800628 usage(fsck_usage);
Linus Torvalds889262e2005-04-25 16:31:13 -0700629 }
630
Linus Torvaldsc3330382005-07-03 10:40:38 -0700631 fsck_head_link();
Junio C Hamano8a498a02005-06-28 14:58:33 -0700632 fsck_object_dir(get_object_directory());
633 if (check_full) {
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700634 struct alternate_object_database *alt;
Junio C Hamano8a498a02005-06-28 14:58:33 -0700635 struct packed_git *p;
636 prepare_alt_odb();
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700637 for (alt = alt_odb_list; alt; alt = alt->next) {
Junio C Hamanoa3eb2502005-07-10 15:40:43 -0700638 char namebuf[PATH_MAX];
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700639 int namelen = alt->name - alt->base;
640 memcpy(namebuf, alt->base, namelen);
Junio C Hamanoa3eb2502005-07-10 15:40:43 -0700641 namebuf[namelen - 1] = 0;
642 fsck_object_dir(namebuf);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700643 }
644 prepare_packed_git();
Junio C Hamanof9253392005-06-29 02:51:27 -0700645 for (p = packed_git; p; p = p->next)
646 /* verify gives error messages itself */
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700647 verify_pack(p, 0);
Junio C Hamanof9253392005-06-29 02:51:27 -0700648
Junio C Hamano8a498a02005-06-28 14:58:33 -0700649 for (p = packed_git; p; p = p->next) {
Shawn O. Pearce326bf392007-03-06 20:44:19 -0500650 uint32_t i, num = num_packed_objects(p);
Nicolas Pitred72308e2007-04-04 16:49:04 -0400651 for (i = 0; i < num; i++)
652 fsck_sha1(nth_packed_object_sha1(p, i));
Junio C Hamano8a498a02005-06-28 14:58:33 -0700653 }
Linus Torvalds20222112005-04-08 15:02:42 -0700654 }
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700655
656 heads = 0;
657 for (i = 1; i < argc; i++) {
Linus Torvalds889262e2005-04-25 16:31:13 -0700658 const char *arg = argv[i];
659
660 if (*arg == '-')
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700661 continue;
Linus Torvalds889262e2005-04-25 16:31:13 -0700662
Linus Torvalds3c249c92005-05-01 16:36:56 -0700663 if (!get_sha1(arg, head_sha1)) {
Linus Torvalds770896e2005-05-04 17:03:09 -0700664 struct object *obj = lookup_object(head_sha1);
Jonas Fonsecae1a13882005-04-29 20:00:40 -0700665
Linus Torvalds770896e2005-05-04 17:03:09 -0700666 /* Error is printed by lookup_object(). */
667 if (!obj)
Jonas Fonsecae1a13882005-04-29 20:00:40 -0700668 continue;
669
Daniel Barkalowff5ebe32005-04-18 11:39:48 -0700670 obj->used = 1;
671 mark_reachable(obj, REACHABLE);
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700672 heads++;
673 continue;
674 }
Petr Baudisf1f0d082005-09-20 20:56:05 +0200675 error("invalid parameter: expected sha1, got '%s'", arg);
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700676 }
677
Linus Torvalds10249322005-05-18 10:16:14 -0700678 /*
Nicolas Pitred1af0022005-05-20 16:59:17 -0400679 * If we've not been given any explicit head information, do the
Linus Torvaldse7bd9072005-05-18 10:19:59 -0700680 * default ones from .git/refs. We also consider the index file
681 * in this case (ie this implies --cache).
Linus Torvalds10249322005-05-18 10:16:14 -0700682 */
Linus Torvaldse7bd9072005-05-18 10:19:59 -0700683 if (!heads) {
Linus Torvalds10249322005-05-18 10:16:14 -0700684 get_default_heads();
685 keep_cache_objects = 1;
686 }
687
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700688 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 Hamano53dc3f32006-04-25 16:37:08 -0700700 if (active_cache_tree)
701 fsck_cache_tree(active_cache_tree);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700702 }
703
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700704 check_connectivity();
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800705 return errors_found;
Linus Torvalds20222112005-04-08 15:02:42 -0700706}