blob: aecc8280a0aedb37437ccce446f3382ccba08829 [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Linus Torvalds4b182422005-04-30 09:59:31 -07002#include "cache.h"
Daniel Barkalowff5ebe32005-04-18 11:39:48 -07003#include "commit.h"
4#include "tree.h"
5#include "blob.h"
Daniel Barkalowc418eda2005-04-28 07:46:33 -07006#include "tag.h"
Linus Torvalds944d8582005-07-03 10:01:38 -07007#include "refs.h"
Junio C Hamanof9253392005-06-29 02:51:27 -07008#include "pack.h"
Junio C Hamano53dc3f32006-04-25 16:37:08 -07009#include "cache-tree.h"
Linus Torvaldse9a95be2006-05-29 12:19:02 -070010#include "tree-walk.h"
Martin Koegler271b8d22008-02-25 22:46:05 +010011#include "fsck.h"
Pierre Habouzit5ac0a202007-10-15 22:34:05 +020012#include "parse-options.h"
Alexander Potashev8ca12c02009-01-10 15:07:50 +030013#include "dir.h"
Daniel Barkalowff5ebe32005-04-18 11:39:48 -070014
15#define REACHABLE 0x0001
Linus Torvalds2d9c58c2006-05-29 12:18:33 -070016#define SEEN 0x0002
Linus Torvaldsd9839e02005-04-13 09:57:30 -070017
David Rientjes96f1e582006-08-15 10:23:48 -070018static int show_root;
19static int show_tags;
20static int show_unreachable;
Shawn O. Pearce566842f2007-04-04 10:46:14 -040021static int include_reflogs = 1;
David Rientjes96f1e582006-08-15 10:23:48 -070022static int check_full;
23static int check_strict;
24static int keep_cache_objects;
Linus Torvaldsd9839e02005-04-13 09:57:30 -070025static unsigned char head_sha1[20];
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080026static int errors_found;
Johannes Schindelin68f6c012007-07-03 01:33:54 +010027static int write_lost_and_found;
Johannes Schindelin20f1eb62007-06-05 03:44:00 +010028static int verbose;
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080029#define ERROR_OBJECT 01
30#define ERROR_REACHABLE 02
Linus Torvaldsd9839e02005-04-13 09:57:30 -070031
Timo Hirvonen962554c2006-02-26 17:13:46 +020032#ifdef NO_D_INO_IN_DIRENT
Junio C Hamano35a730f2006-01-19 17:13:51 -080033#define SORT_DIRENT 0
34#define DIRENT_SORT_HINT(de) 0
35#else
36#define SORT_DIRENT 1
37#define DIRENT_SORT_HINT(de) ((de)->d_ino)
38#endif
Petr Baudisf1f0d082005-09-20 20:56:05 +020039
40static void objreport(struct object *obj, const char *severity,
41 const char *err, va_list params)
42{
43 fprintf(stderr, "%s in %s %s: ",
Linus Torvalds885a86a2006-06-14 16:45:13 -070044 severity, typename(obj->type), sha1_to_hex(obj->sha1));
Petr Baudisf1f0d082005-09-20 20:56:05 +020045 vfprintf(stderr, err, params);
46 fputs("\n", stderr);
47}
48
Peter Hagervalla7928f82005-09-28 14:04:54 +020049static int objerror(struct object *obj, const char *err, ...)
Petr Baudisf1f0d082005-09-20 20:56:05 +020050{
51 va_list params;
52 va_start(params, err);
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080053 errors_found |= ERROR_OBJECT;
Petr Baudisf1f0d082005-09-20 20:56:05 +020054 objreport(obj, "error", err, params);
55 va_end(params);
56 return -1;
57}
58
Martin Koeglerba002f32008-02-25 22:46:08 +010059static int fsck_error_func(struct object *obj, int type, const char *err, ...)
Petr Baudisf1f0d082005-09-20 20:56:05 +020060{
61 va_list params;
62 va_start(params, err);
Martin Koeglerba002f32008-02-25 22:46:08 +010063 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
Petr Baudisf1f0d082005-09-20 20:56:05 +020064 va_end(params);
Martin Koeglerba002f32008-02-25 22:46:08 +010065 return (type == FSCK_WARN) ? 0 : 1;
Petr Baudisf1f0d082005-09-20 20:56:05 +020066}
67
Linus Torvalds04d39752008-12-10 19:44:37 -080068static struct object_array pending;
69
Martin Koegler271b8d22008-02-25 22:46:05 +010070static int mark_object(struct object *obj, int type, void *data)
71{
Martin Koegler271b8d22008-02-25 22:46:05 +010072 struct object *parent = data;
Martin Koegler271b8d22008-02-25 22:46:05 +010073
74 if (!obj) {
75 printf("broken link from %7s %s\n",
76 typename(parent->type), sha1_to_hex(parent->sha1));
77 printf("broken link from %7s %s\n",
78 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
79 errors_found |= ERROR_REACHABLE;
80 return 1;
81 }
82
83 if (type != OBJ_ANY && obj->type != type)
84 objerror(parent, "wrong object type in link");
85
86 if (obj->flags & REACHABLE)
87 return 0;
88 obj->flags |= REACHABLE;
89 if (!obj->parsed) {
90 if (parent && !has_sha1_file(obj->sha1)) {
91 printf("broken link from %7s %s\n",
92 typename(parent->type), sha1_to_hex(parent->sha1));
93 printf(" to %7s %s\n",
94 typename(obj->type), sha1_to_hex(obj->sha1));
95 errors_found |= ERROR_REACHABLE;
96 }
97 return 1;
98 }
99
Linus Torvalds04d39752008-12-10 19:44:37 -0800100 add_object_array(obj, (void *) parent, &pending);
101 return 0;
102}
103
104static void mark_object_reachable(struct object *obj)
105{
106 mark_object(obj, OBJ_ANY, 0);
107}
108
109static int traverse_one_object(struct object *obj, struct object *parent)
110{
111 int result;
112 struct tree *tree = NULL;
113
Martin Koegler271b8d22008-02-25 22:46:05 +0100114 if (obj->type == OBJ_TREE) {
115 obj->parsed = 0;
116 tree = (struct tree *)obj;
117 if (parse_tree(tree) < 0)
118 return 1; /* error already displayed */
119 }
120 result = fsck_walk(obj, mark_object, obj);
121 if (tree) {
122 free(tree->buffer);
123 tree->buffer = NULL;
124 }
Martin Koegler271b8d22008-02-25 22:46:05 +0100125 return result;
126}
127
Linus Torvalds04d39752008-12-10 19:44:37 -0800128static int traverse_reachable(void)
Martin Koegler271b8d22008-02-25 22:46:05 +0100129{
Linus Torvalds04d39752008-12-10 19:44:37 -0800130 int result = 0;
131 while (pending.nr) {
132 struct object_array_entry *entry;
133 struct object *obj, *parent;
134
135 entry = pending.objects + --pending.nr;
136 obj = entry->item;
137 parent = (struct object *) entry->name;
138 result |= traverse_one_object(obj, parent);
139 }
140 return !!result;
Martin Koegler271b8d22008-02-25 22:46:05 +0100141}
142
143static int mark_used(struct object *obj, int type, void *data)
144{
145 if (!obj)
146 return 1;
147 obj->used = 1;
148 return 0;
Petr Baudisf1f0d082005-09-20 20:56:05 +0200149}
150
Linus Torvalds18af29f2007-01-21 22:26:41 -0800151/*
152 * Check a single reachable object
153 */
154static void check_reachable_object(struct object *obj)
155{
Linus Torvalds18af29f2007-01-21 22:26:41 -0800156 /*
157 * We obviously want the object to be parsed,
158 * except if it was in a pack-file and we didn't
159 * do a full fsck
160 */
161 if (!obj->parsed) {
Junio C Hamanoefec43c2007-03-05 00:21:24 -0800162 if (has_sha1_pack(obj->sha1, NULL))
Linus Torvalds18af29f2007-01-21 22:26:41 -0800163 return; /* it is in pack - forget about it */
164 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800165 errors_found |= ERROR_REACHABLE;
Linus Torvalds18af29f2007-01-21 22:26:41 -0800166 return;
167 }
Linus Torvalds18af29f2007-01-21 22:26:41 -0800168}
169
170/*
171 * Check a single unreachable object
172 */
173static void check_unreachable_object(struct object *obj)
174{
175 /*
176 * Missing unreachable object? Ignore it. It's not like
177 * we miss it (since it can't be reached), nor do we want
178 * to complain about it being unreachable (since it does
179 * not exist).
180 */
181 if (!obj->parsed)
182 return;
183
184 /*
185 * Unreachable object that exists? Show it if asked to,
186 * since this is something that is prunable.
187 */
188 if (show_unreachable) {
189 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
190 return;
191 }
192
193 /*
194 * "!used" means that nothing at all points to it, including
Pavel Roskin3dff5372007-02-03 23:49:16 -0500195 * other unreachable objects. In other words, it's the "tip"
Linus Torvalds18af29f2007-01-21 22:26:41 -0800196 * of some set of unreachable objects, usually a commit that
197 * got dropped.
198 *
199 * Such starting points are more interesting than some random
200 * set of unreachable objects, so we show them even if the user
201 * hasn't asked for _all_ unreachable objects. If you have
202 * deleted a branch by mistake, this is a prime candidate to
203 * start looking at, for example.
204 */
205 if (!obj->used) {
206 printf("dangling %s %s\n", typename(obj->type),
207 sha1_to_hex(obj->sha1));
Johannes Schindelin68f6c012007-07-03 01:33:54 +0100208 if (write_lost_and_found) {
209 char *filename = git_path("lost-found/%s/%s",
210 obj->type == OBJ_COMMIT ? "commit" : "other",
211 sha1_to_hex(obj->sha1));
212 FILE *f;
213
214 if (safe_create_leading_directories(filename)) {
215 error("Could not create lost-found");
216 return;
217 }
218 if (!(f = fopen(filename, "w")))
219 die("Could not open %s", filename);
Johannes Schindelin16a7fcf2007-07-22 21:20:26 +0100220 if (obj->type == OBJ_BLOB) {
221 enum object_type type;
222 unsigned long size;
223 char *buf = read_sha1_file(obj->sha1,
224 &type, &size);
225 if (buf) {
Alex Riesen47d32af2008-12-05 01:35:48 +0100226 if (fwrite(buf, size, 1, f) != 1)
227 die("Could not write %s: %s",
228 filename, strerror(errno));
Johannes Schindelin16a7fcf2007-07-22 21:20:26 +0100229 free(buf);
230 }
231 } else
232 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
Alex Riesen47d32af2008-12-05 01:35:48 +0100233 if (fclose(f))
234 die("Could not finish %s: %s",
235 filename, strerror(errno));
Johannes Schindelin68f6c012007-07-03 01:33:54 +0100236 }
Linus Torvalds18af29f2007-01-21 22:26:41 -0800237 return;
238 }
239
240 /*
241 * Otherwise? It's there, it's unreachable, and some other unreachable
242 * object points to it. Ignore it - it's not interesting, and we showed
243 * all the interesting cases above.
244 */
245}
246
247static void check_object(struct object *obj)
248{
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100249 if (verbose)
250 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
251
Linus Torvalds18af29f2007-01-21 22:26:41 -0800252 if (obj->flags & REACHABLE)
253 check_reachable_object(obj);
254 else
255 check_unreachable_object(obj);
256}
Petr Baudisf1f0d082005-09-20 20:56:05 +0200257
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700258static void check_connectivity(void)
259{
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700260 int i, max;
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700261
Linus Torvalds04d39752008-12-10 19:44:37 -0800262 /* Traverse the pending reachable objects */
263 traverse_reachable();
264
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700265 /* Look up all the requirements, warn about missing objects.. */
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700266 max = get_max_object_index();
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100267 if (verbose)
268 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
269
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700270 for (i = 0; i < max; i++) {
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700271 struct object *obj = get_indexed_object(i);
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700272
Linus Torvalds18af29f2007-01-21 22:26:41 -0800273 if (obj)
274 check_object(obj);
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700275 }
276}
277
Nicolas Pitred72308e2007-04-04 16:49:04 -0400278static int fsck_sha1(const unsigned char *sha1)
Linus Torvalds20222112005-04-08 15:02:42 -0700279{
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700280 struct object *obj = parse_object(sha1);
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800281 if (!obj) {
282 errors_found |= ERROR_OBJECT;
283 return error("%s: object corrupt or missing",
284 sha1_to_hex(sha1));
285 }
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700286 if (obj->flags & SEEN)
287 return 0;
288 obj->flags |= SEEN;
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800289
Martin Koeglerba002f32008-02-25 22:46:08 +0100290 if (verbose)
291 fprintf(stderr, "Checking %s %s\n",
292 typename(obj->type), sha1_to_hex(obj->sha1));
293
Martin Koegler271b8d22008-02-25 22:46:05 +0100294 if (fsck_walk(obj, mark_used, 0))
295 objerror(obj, "broken links");
Martin Koeglerba002f32008-02-25 22:46:08 +0100296 if (fsck_object(obj, check_strict, fsck_error_func))
297 return -1;
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700298
Martin Koeglerba002f32008-02-25 22:46:08 +0100299 if (obj->type == OBJ_TREE) {
300 struct tree *item = (struct tree *) obj;
301
302 free(item->buffer);
303 item->buffer = NULL;
304 }
305
306 if (obj->type == OBJ_COMMIT) {
307 struct commit *commit = (struct commit *) obj;
308
309 free(commit->buffer);
310 commit->buffer = NULL;
311
312 if (!commit->parents && show_root)
313 printf("root %s\n", sha1_to_hex(commit->object.sha1));
314 }
315
316 if (obj->type == OBJ_TAG) {
317 struct tag *tag = (struct tag *) obj;
318
319 if (show_tags && tag->tagged) {
320 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
321 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
322 }
323 }
324
325 return 0;
Linus Torvalds20222112005-04-08 15:02:42 -0700326}
327
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700328/*
329 * This is the sorting chunk size: make it reasonably
330 * big so that we can sort well..
331 */
332#define MAX_SHA1_ENTRIES (1024)
333
334struct sha1_entry {
335 unsigned long ino;
336 unsigned char sha1[20];
337};
338
339static struct {
340 unsigned long nr;
341 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
342} sha1_list;
343
344static int ino_compare(const void *_a, const void *_b)
345{
346 const struct sha1_entry *a = _a, *b = _b;
347 unsigned long ino1 = a->ino, ino2 = b->ino;
348 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
349}
350
351static void fsck_sha1_list(void)
352{
353 int i, nr = sha1_list.nr;
354
Junio C Hamano35a730f2006-01-19 17:13:51 -0800355 if (SORT_DIRENT)
356 qsort(sha1_list.entry, nr,
357 sizeof(struct sha1_entry *), ino_compare);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700358 for (i = 0; i < nr; i++) {
359 struct sha1_entry *entry = sha1_list.entry[i];
360 unsigned char *sha1 = entry->sha1;
361
362 sha1_list.entry[i] = NULL;
Petr Baudisf1f0d082005-09-20 20:56:05 +0200363 fsck_sha1(sha1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700364 free(entry);
365 }
366 sha1_list.nr = 0;
367}
368
369static void add_sha1_list(unsigned char *sha1, unsigned long ino)
370{
371 struct sha1_entry *entry = xmalloc(sizeof(*entry));
372 int nr;
373
374 entry->ino = ino;
Shawn Pearcee7024962006-08-23 02:49:00 -0400375 hashcpy(entry->sha1, sha1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700376 nr = sha1_list.nr;
377 if (nr == MAX_SHA1_ENTRIES) {
378 fsck_sha1_list();
379 nr = 0;
380 }
381 sha1_list.entry[nr] = entry;
382 sha1_list.nr = ++nr;
383}
384
David Rientjesb5524c82006-08-14 13:36:18 -0700385static void fsck_dir(int i, char *path)
Linus Torvalds20222112005-04-08 15:02:42 -0700386{
387 DIR *dir = opendir(path);
388 struct dirent *de;
389
Linus Torvalds230f1322005-10-08 15:54:01 -0700390 if (!dir)
David Rientjesb5524c82006-08-14 13:36:18 -0700391 return;
Linus Torvalds20222112005-04-08 15:02:42 -0700392
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100393 if (verbose)
394 fprintf(stderr, "Checking directory %s\n", path);
395
Linus Torvalds20222112005-04-08 15:02:42 -0700396 while ((de = readdir(dir)) != NULL) {
397 char name[100];
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700398 unsigned char sha1[20];
Linus Torvalds20222112005-04-08 15:02:42 -0700399
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300400 if (is_dot_or_dotdot(de->d_name))
Linus Torvalds20222112005-04-08 15:02:42 -0700401 continue;
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300402 if (strlen(de->d_name) == 38) {
Linus Torvalds20222112005-04-08 15:02:42 -0700403 sprintf(name, "%02x", i);
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300404 memcpy(name+2, de->d_name, 39);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700405 if (get_sha1_hex(name, sha1) < 0)
406 break;
Junio C Hamano35a730f2006-01-19 17:13:51 -0800407 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700408 continue;
Linus Torvalds20222112005-04-08 15:02:42 -0700409 }
Brandon Casey3d32a462008-08-05 13:01:50 -0500410 if (!prefixcmp(de->d_name, "tmp_obj_"))
Shawn O. Pearcea08c53a2008-07-26 21:33:00 -0500411 continue;
Linus Torvalds20222112005-04-08 15:02:42 -0700412 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
413 }
414 closedir(dir);
Linus Torvalds20222112005-04-08 15:02:42 -0700415}
416
David Rientjes96f1e582006-08-15 10:23:48 -0700417static int default_refs;
Linus Torvalds944d8582005-07-03 10:01:38 -0700418
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100419static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
420 const char *email, unsigned long timestamp, int tz,
421 const char *message, void *cb_data)
Junio C Hamano55dd5522006-12-18 01:36:16 -0800422{
423 struct object *obj;
424
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100425 if (verbose)
426 fprintf(stderr, "Checking reflog %s->%s\n",
427 sha1_to_hex(osha1), sha1_to_hex(nsha1));
428
Junio C Hamano55dd5522006-12-18 01:36:16 -0800429 if (!is_null_sha1(osha1)) {
430 obj = lookup_object(osha1);
431 if (obj) {
432 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100433 mark_object_reachable(obj);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800434 }
435 }
436 obj = lookup_object(nsha1);
437 if (obj) {
438 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100439 mark_object_reachable(obj);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800440 }
441 return 0;
442}
443
Nicolas Pitreeb8381c2007-02-03 13:25:43 -0500444static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
445{
446 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
447 return 0;
448}
449
Linus Torvalds6232f622008-01-15 16:34:17 -0800450static int is_branch(const char *refname)
451{
452 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
453}
454
Junio C Hamano8da19772006-09-20 22:02:01 -0700455static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
Linus Torvalds10249322005-05-18 10:16:14 -0700456{
Linus Torvalds10249322005-05-18 10:16:14 -0700457 struct object *obj;
458
Linus Torvalds6232f622008-01-15 16:34:17 -0800459 obj = parse_object(sha1);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700460 if (!obj) {
Linus Torvalds944d8582005-07-03 10:01:38 -0700461 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
462 /* We'll continue with the rest despite the error.. */
463 return 0;
Junio C Hamano8a498a02005-06-28 14:58:33 -0700464 }
Linus Torvalds6232f622008-01-15 16:34:17 -0800465 if (obj->type != OBJ_COMMIT && is_branch(refname))
466 error("%s: not a commit", refname);
Linus Torvalds944d8582005-07-03 10:01:38 -0700467 default_refs++;
Linus Torvalds10249322005-05-18 10:16:14 -0700468 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100469 mark_object_reachable(obj);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800470
Linus Torvalds7c4d07c2005-05-20 07:49:17 -0700471 return 0;
Linus Torvalds10249322005-05-18 10:16:14 -0700472}
473
Linus Torvalds10249322005-05-18 10:16:14 -0700474static void get_default_heads(void)
475{
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700476 for_each_ref(fsck_handle_ref, NULL);
Shawn O. Pearce566842f2007-04-04 10:46:14 -0400477 if (include_reflogs)
478 for_each_reflog(fsck_handle_reflog, NULL);
Linus Torvalds071fa892006-08-29 11:47:30 -0700479
480 /*
481 * Not having any default heads isn't really fatal, but
482 * it does mean that "--unreachable" no longer makes any
483 * sense (since in this case everything will obviously
484 * be unreachable by definition.
485 *
486 * Showing dangling objects is valid, though (as those
487 * dangling objects are likely lost heads).
488 *
489 * So we just print a warning about it, and clear the
490 * "show_unreachable" flag.
491 */
492 if (!default_refs) {
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700493 fprintf(stderr, "notice: No default references\n");
Linus Torvalds071fa892006-08-29 11:47:30 -0700494 show_unreachable = 0;
495 }
Linus Torvalds10249322005-05-18 10:16:14 -0700496}
497
Junio C Hamano8a498a02005-06-28 14:58:33 -0700498static void fsck_object_dir(const char *path)
499{
500 int i;
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100501
502 if (verbose)
503 fprintf(stderr, "Checking object directory\n");
504
Junio C Hamano8a498a02005-06-28 14:58:33 -0700505 for (i = 0; i < 256; i++) {
506 static char dir[4096];
507 sprintf(dir, "%s/%02x", path, i);
508 fsck_dir(i, dir);
509 }
510 fsck_sha1_list();
511}
512
Linus Torvaldsc3330382005-07-03 10:40:38 -0700513static int fsck_head_link(void)
514{
Linus Torvaldsc3330382005-07-03 10:40:38 -0700515 unsigned char sha1[20];
Junio C Hamano8da19772006-09-20 22:02:01 -0700516 int flag;
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700517 int null_is_error = 0;
518 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
Linus Torvaldsc3330382005-07-03 10:40:38 -0700519
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100520 if (verbose)
521 fprintf(stderr, "Checking HEAD link\n");
522
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700523 if (!head_points_at)
524 return error("Invalid HEAD");
525 if (!strcmp(head_points_at, "HEAD"))
526 /* detached HEAD */
527 null_is_error = 1;
528 else if (prefixcmp(head_points_at, "refs/heads/"))
Junio C Hamano8098a172005-09-30 14:26:57 -0700529 return error("HEAD points to something strange (%s)",
Junio C Hamano5b10b092006-09-18 01:08:00 -0700530 head_points_at);
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700531 if (is_null_sha1(sha1)) {
532 if (null_is_error)
533 return error("HEAD: detached HEAD points at nothing");
534 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
535 head_points_at + 11);
536 }
Linus Torvaldsc3330382005-07-03 10:40:38 -0700537 return 0;
538}
539
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700540static int fsck_cache_tree(struct cache_tree *it)
541{
542 int i;
543 int err = 0;
544
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100545 if (verbose)
546 fprintf(stderr, "Checking cache tree\n");
547
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700548 if (0 <= it->entry_count) {
549 struct object *obj = parse_object(it->sha1);
Junio C Hamano6d60bbe2006-05-03 21:17:45 -0700550 if (!obj) {
551 error("%s: invalid sha1 pointer in cache-tree",
552 sha1_to_hex(it->sha1));
553 return 1;
554 }
Martin Koegler271b8d22008-02-25 22:46:05 +0100555 mark_object_reachable(obj);
Junio C Hamanocdc08b32006-05-01 22:15:54 -0700556 obj->used = 1;
Linus Torvalds19746322006-07-11 20:45:31 -0700557 if (obj->type != OBJ_TREE)
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700558 err |= objerror(obj, "non-tree in cache-tree");
559 }
560 for (i = 0; i < it->subtree_nr; i++)
561 err |= fsck_cache_tree(it->down[i]->cache_tree);
562 return err;
563}
564
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200565static char const * const fsck_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200566 "git fsck [options] [<object>...]",
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200567 NULL
568};
569
570static struct option fsck_opts[] = {
571 OPT__VERBOSE(&verbose),
572 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
573 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
574 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
575 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
576 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
577 OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"),
Emil Medvedd46b9b2007-10-30 14:15:21 -0500578 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200579 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
580 "write dangling objects in .git/lost-found"),
581 OPT_END(),
582};
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800583
Peter Hagervallbaffc0e2007-07-15 01:14:45 +0200584int cmd_fsck(int argc, const char **argv, const char *prefix)
Linus Torvalds20222112005-04-08 15:02:42 -0700585{
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700586 int i, heads;
Linus Torvalds20222112005-04-08 15:02:42 -0700587
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800588 errors_found = 0;
Junio C Hamano61e2b012005-11-25 23:52:04 -0800589
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200590 argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0);
591 if (write_lost_and_found) {
592 check_full = 1;
593 include_reflogs = 0;
Linus Torvalds889262e2005-04-25 16:31:13 -0700594 }
595
Linus Torvaldsc3330382005-07-03 10:40:38 -0700596 fsck_head_link();
Junio C Hamano8a498a02005-06-28 14:58:33 -0700597 fsck_object_dir(get_object_directory());
598 if (check_full) {
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700599 struct alternate_object_database *alt;
Junio C Hamano8a498a02005-06-28 14:58:33 -0700600 struct packed_git *p;
601 prepare_alt_odb();
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700602 for (alt = alt_odb_list; alt; alt = alt->next) {
Junio C Hamanoa3eb2502005-07-10 15:40:43 -0700603 char namebuf[PATH_MAX];
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700604 int namelen = alt->name - alt->base;
605 memcpy(namebuf, alt->base, namelen);
Junio C Hamanoa3eb2502005-07-10 15:40:43 -0700606 namebuf[namelen - 1] = 0;
607 fsck_object_dir(namebuf);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700608 }
609 prepare_packed_git();
Junio C Hamanof9253392005-06-29 02:51:27 -0700610 for (p = packed_git; p; p = p->next)
611 /* verify gives error messages itself */
Nicolas Pitre77d3ece2008-06-24 23:18:17 -0400612 verify_pack(p);
Junio C Hamanof9253392005-06-29 02:51:27 -0700613
Junio C Hamano8a498a02005-06-28 14:58:33 -0700614 for (p = packed_git; p; p = p->next) {
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200615 uint32_t j, num;
Shawn O. Pearceb77ffe82007-05-30 02:13:14 -0400616 if (open_pack_index(p))
617 continue;
618 num = p->num_objects;
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200619 for (j = 0; j < num; j++)
620 fsck_sha1(nth_packed_object_sha1(p, j));
Junio C Hamano8a498a02005-06-28 14:58:33 -0700621 }
Linus Torvalds20222112005-04-08 15:02:42 -0700622 }
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700623
624 heads = 0;
Christian Couder3aed2fd2009-01-18 04:46:09 +0100625 for (i = 0; i < argc; i++) {
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700626 const char *arg = argv[i];
Linus Torvalds3c249c92005-05-01 16:36:56 -0700627 if (!get_sha1(arg, head_sha1)) {
Linus Torvalds770896e2005-05-04 17:03:09 -0700628 struct object *obj = lookup_object(head_sha1);
Jonas Fonsecae1a13882005-04-29 20:00:40 -0700629
Linus Torvalds770896e2005-05-04 17:03:09 -0700630 /* Error is printed by lookup_object(). */
631 if (!obj)
Jonas Fonsecae1a13882005-04-29 20:00:40 -0700632 continue;
633
Daniel Barkalowff5ebe32005-04-18 11:39:48 -0700634 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100635 mark_object_reachable(obj);
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700636 heads++;
637 continue;
638 }
Petr Baudisf1f0d082005-09-20 20:56:05 +0200639 error("invalid parameter: expected sha1, got '%s'", arg);
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700640 }
641
Linus Torvalds10249322005-05-18 10:16:14 -0700642 /*
Nicolas Pitred1af0022005-05-20 16:59:17 -0400643 * If we've not been given any explicit head information, do the
Linus Torvaldse7bd9072005-05-18 10:19:59 -0700644 * default ones from .git/refs. We also consider the index file
645 * in this case (ie this implies --cache).
Linus Torvalds10249322005-05-18 10:16:14 -0700646 */
Linus Torvaldse7bd9072005-05-18 10:19:59 -0700647 if (!heads) {
Linus Torvalds10249322005-05-18 10:16:14 -0700648 get_default_heads();
649 keep_cache_objects = 1;
650 }
651
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700652 if (keep_cache_objects) {
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700653 read_cache();
654 for (i = 0; i < active_nr; i++) {
Linus Torvalds8d9721c2007-04-09 21:15:29 -0700655 unsigned int mode;
656 struct blob *blob;
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700657 struct object *obj;
Linus Torvalds8d9721c2007-04-09 21:15:29 -0700658
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800659 mode = active_cache[i]->ce_mode;
Martin Waitz302b9282007-05-21 22:08:28 +0200660 if (S_ISGITLINK(mode))
Linus Torvalds8d9721c2007-04-09 21:15:29 -0700661 continue;
662 blob = lookup_blob(active_cache[i]->sha1);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700663 if (!blob)
664 continue;
665 obj = &blob->object;
666 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100667 mark_object_reachable(obj);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700668 }
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700669 if (active_cache_tree)
670 fsck_cache_tree(active_cache_tree);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700671 }
672
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700673 check_connectivity();
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800674 return errors_found;
Linus Torvalds20222112005-04-08 15:02:42 -0700675}