blob: bb9a2cd44722dc27d54aa5451278a10f512becb8 [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"
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +070014#include "progress.h"
Nguyễn Thái Ngọc Duy6f7f3be2012-03-07 17:54:20 +070015#include "streaming.h"
Daniel Barkalowff5ebe32005-04-18 11:39:48 -070016
17#define REACHABLE 0x0001
Linus Torvalds2d9c58c2006-05-29 12:18:33 -070018#define SEEN 0x0002
Linus Torvaldsd9839e02005-04-13 09:57:30 -070019
David Rientjes96f1e582006-08-15 10:23:48 -070020static int show_root;
21static int show_tags;
22static int show_unreachable;
Shawn O. Pearce566842f2007-04-04 10:46:14 -040023static int include_reflogs = 1;
Junio C Hamanof29cd392009-10-20 11:46:55 -070024static int check_full = 1;
David Rientjes96f1e582006-08-15 10:23:48 -070025static int check_strict;
26static int keep_cache_objects;
Linus Torvaldsd9839e02005-04-13 09:57:30 -070027static unsigned char head_sha1[20];
Junio C Hamano469e2eb2009-01-30 00:33:00 -080028static const char *head_points_at;
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080029static int errors_found;
Johannes Schindelin68f6c012007-07-03 01:33:54 +010030static int write_lost_and_found;
Johannes Schindelin20f1eb62007-06-05 03:44:00 +010031static int verbose;
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +070032static int show_progress = -1;
Junio C Hamanoc6a13b22012-02-28 14:55:39 -080033static int show_dangling = 1;
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080034#define ERROR_OBJECT 01
35#define ERROR_REACHABLE 02
Nguyễn Thái Ngọc Duya3ed7552011-11-07 09:59:23 +070036#define ERROR_PACK 04
Linus Torvaldsd9839e02005-04-13 09:57:30 -070037
Timo Hirvonen962554c2006-02-26 17:13:46 +020038#ifdef NO_D_INO_IN_DIRENT
Junio C Hamano35a730f2006-01-19 17:13:51 -080039#define SORT_DIRENT 0
40#define DIRENT_SORT_HINT(de) 0
41#else
42#define SORT_DIRENT 1
43#define DIRENT_SORT_HINT(de) ((de)->d_ino)
44#endif
Petr Baudisf1f0d082005-09-20 20:56:05 +020045
46static void objreport(struct object *obj, const char *severity,
47 const char *err, va_list params)
48{
49 fprintf(stderr, "%s in %s %s: ",
Linus Torvalds885a86a2006-06-14 16:45:13 -070050 severity, typename(obj->type), sha1_to_hex(obj->sha1));
Petr Baudisf1f0d082005-09-20 20:56:05 +020051 vfprintf(stderr, err, params);
52 fputs("\n", stderr);
53}
54
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080055__attribute__((format (printf, 2, 3)))
Peter Hagervalla7928f82005-09-28 14:04:54 +020056static int objerror(struct object *obj, const char *err, ...)
Petr Baudisf1f0d082005-09-20 20:56:05 +020057{
58 va_list params;
59 va_start(params, err);
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080060 errors_found |= ERROR_OBJECT;
Petr Baudisf1f0d082005-09-20 20:56:05 +020061 objreport(obj, "error", err, params);
62 va_end(params);
63 return -1;
64}
65
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080066__attribute__((format (printf, 3, 4)))
Martin Koeglerba002f32008-02-25 22:46:08 +010067static int fsck_error_func(struct object *obj, int type, const char *err, ...)
Petr Baudisf1f0d082005-09-20 20:56:05 +020068{
69 va_list params;
70 va_start(params, err);
Martin Koeglerba002f32008-02-25 22:46:08 +010071 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
Petr Baudisf1f0d082005-09-20 20:56:05 +020072 va_end(params);
Martin Koeglerba002f32008-02-25 22:46:08 +010073 return (type == FSCK_WARN) ? 0 : 1;
Petr Baudisf1f0d082005-09-20 20:56:05 +020074}
75
Linus Torvalds04d39752008-12-10 19:44:37 -080076static struct object_array pending;
77
Martin Koegler271b8d22008-02-25 22:46:05 +010078static int mark_object(struct object *obj, int type, void *data)
79{
Martin Koegler271b8d22008-02-25 22:46:05 +010080 struct object *parent = data;
Martin Koegler271b8d22008-02-25 22:46:05 +010081
Junio C Hamanoa1cdc252011-01-26 12:46:55 -080082 /*
83 * The only case data is NULL or type is OBJ_ANY is when
84 * mark_object_reachable() calls us. All the callers of
85 * that function has non-NULL obj hence ...
86 */
Martin Koegler271b8d22008-02-25 22:46:05 +010087 if (!obj) {
Junio C Hamanoa1cdc252011-01-26 12:46:55 -080088 /* ... these references to parent->fld are safe here */
Martin Koegler271b8d22008-02-25 22:46:05 +010089 printf("broken link from %7s %s\n",
90 typename(parent->type), sha1_to_hex(parent->sha1));
91 printf("broken link from %7s %s\n",
92 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
93 errors_found |= ERROR_REACHABLE;
94 return 1;
95 }
96
97 if (type != OBJ_ANY && obj->type != type)
Junio C Hamanoa1cdc252011-01-26 12:46:55 -080098 /* ... and the reference to parent is safe here */
Martin Koegler271b8d22008-02-25 22:46:05 +010099 objerror(parent, "wrong object type in link");
100
101 if (obj->flags & REACHABLE)
102 return 0;
103 obj->flags |= REACHABLE;
104 if (!obj->parsed) {
105 if (parent && !has_sha1_file(obj->sha1)) {
106 printf("broken link from %7s %s\n",
107 typename(parent->type), sha1_to_hex(parent->sha1));
108 printf(" to %7s %s\n",
109 typename(obj->type), sha1_to_hex(obj->sha1));
110 errors_found |= ERROR_REACHABLE;
111 }
112 return 1;
113 }
114
Linus Torvalds04d39752008-12-10 19:44:37 -0800115 add_object_array(obj, (void *) parent, &pending);
116 return 0;
117}
118
119static void mark_object_reachable(struct object *obj)
120{
Linus Torvalds2af202b2009-06-18 10:28:43 -0700121 mark_object(obj, OBJ_ANY, NULL);
Linus Torvalds04d39752008-12-10 19:44:37 -0800122}
123
Junio C Hamanoa1cdc252011-01-26 12:46:55 -0800124static int traverse_one_object(struct object *obj)
Linus Torvalds04d39752008-12-10 19:44:37 -0800125{
126 int result;
127 struct tree *tree = NULL;
128
Martin Koegler271b8d22008-02-25 22:46:05 +0100129 if (obj->type == OBJ_TREE) {
130 obj->parsed = 0;
131 tree = (struct tree *)obj;
132 if (parse_tree(tree) < 0)
133 return 1; /* error already displayed */
134 }
135 result = fsck_walk(obj, mark_object, obj);
136 if (tree) {
137 free(tree->buffer);
138 tree->buffer = NULL;
139 }
Martin Koegler271b8d22008-02-25 22:46:05 +0100140 return result;
141}
142
Linus Torvalds04d39752008-12-10 19:44:37 -0800143static int traverse_reachable(void)
Martin Koegler271b8d22008-02-25 22:46:05 +0100144{
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700145 struct progress *progress = NULL;
146 unsigned int nr = 0;
Linus Torvalds04d39752008-12-10 19:44:37 -0800147 int result = 0;
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700148 if (show_progress)
149 progress = start_progress_delay("Checking connectivity", 0, 0, 2);
Linus Torvalds04d39752008-12-10 19:44:37 -0800150 while (pending.nr) {
151 struct object_array_entry *entry;
Johannes Schindelinc0aa3352011-03-22 13:50:08 +0100152 struct object *obj;
Linus Torvalds04d39752008-12-10 19:44:37 -0800153
154 entry = pending.objects + --pending.nr;
155 obj = entry->item;
Junio C Hamanoa1cdc252011-01-26 12:46:55 -0800156 result |= traverse_one_object(obj);
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700157 display_progress(progress, ++nr);
Linus Torvalds04d39752008-12-10 19:44:37 -0800158 }
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700159 stop_progress(&progress);
Linus Torvalds04d39752008-12-10 19:44:37 -0800160 return !!result;
Martin Koegler271b8d22008-02-25 22:46:05 +0100161}
162
163static int mark_used(struct object *obj, int type, void *data)
164{
165 if (!obj)
166 return 1;
167 obj->used = 1;
168 return 0;
Petr Baudisf1f0d082005-09-20 20:56:05 +0200169}
170
Linus Torvalds18af29f2007-01-21 22:26:41 -0800171/*
172 * Check a single reachable object
173 */
174static void check_reachable_object(struct object *obj)
175{
Linus Torvalds18af29f2007-01-21 22:26:41 -0800176 /*
177 * We obviously want the object to be parsed,
178 * except if it was in a pack-file and we didn't
179 * do a full fsck
180 */
181 if (!obj->parsed) {
Junio C Hamanocd673c12009-02-27 23:15:53 -0800182 if (has_sha1_pack(obj->sha1))
Linus Torvalds18af29f2007-01-21 22:26:41 -0800183 return; /* it is in pack - forget about it */
184 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800185 errors_found |= ERROR_REACHABLE;
Linus Torvalds18af29f2007-01-21 22:26:41 -0800186 return;
187 }
Linus Torvalds18af29f2007-01-21 22:26:41 -0800188}
189
190/*
191 * Check a single unreachable object
192 */
193static void check_unreachable_object(struct object *obj)
194{
195 /*
196 * Missing unreachable object? Ignore it. It's not like
197 * we miss it (since it can't be reached), nor do we want
198 * to complain about it being unreachable (since it does
199 * not exist).
200 */
201 if (!obj->parsed)
202 return;
203
204 /*
205 * Unreachable object that exists? Show it if asked to,
206 * since this is something that is prunable.
207 */
208 if (show_unreachable) {
209 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
210 return;
211 }
212
213 /*
214 * "!used" means that nothing at all points to it, including
Pavel Roskin3dff5372007-02-03 23:49:16 -0500215 * other unreachable objects. In other words, it's the "tip"
Linus Torvalds18af29f2007-01-21 22:26:41 -0800216 * of some set of unreachable objects, usually a commit that
217 * got dropped.
218 *
219 * Such starting points are more interesting than some random
220 * set of unreachable objects, so we show them even if the user
221 * hasn't asked for _all_ unreachable objects. If you have
222 * deleted a branch by mistake, this is a prime candidate to
223 * start looking at, for example.
224 */
225 if (!obj->used) {
Junio C Hamanoc6a13b22012-02-28 14:55:39 -0800226 if (show_dangling)
227 printf("dangling %s %s\n", typename(obj->type),
228 sha1_to_hex(obj->sha1));
Johannes Schindelin68f6c012007-07-03 01:33:54 +0100229 if (write_lost_and_found) {
230 char *filename = git_path("lost-found/%s/%s",
231 obj->type == OBJ_COMMIT ? "commit" : "other",
232 sha1_to_hex(obj->sha1));
233 FILE *f;
234
235 if (safe_create_leading_directories(filename)) {
236 error("Could not create lost-found");
237 return;
238 }
239 if (!(f = fopen(filename, "w")))
Thomas Rast0721c312009-06-27 17:58:47 +0200240 die_errno("Could not open '%s'", filename);
Johannes Schindelin16a7fcf2007-07-22 21:20:26 +0100241 if (obj->type == OBJ_BLOB) {
Nguyễn Thái Ngọc Duy6f7f3be2012-03-07 17:54:20 +0700242 if (stream_blob_to_fd(fileno(f), obj->sha1, NULL, 1))
Junio C Hamanoeb726f22011-09-11 18:03:38 -0700243 die_errno("Could not write '%s'", filename);
Johannes Schindelin16a7fcf2007-07-22 21:20:26 +0100244 } else
245 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
Alex Riesen47d32af2008-12-05 01:35:48 +0100246 if (fclose(f))
Thomas Rastd824cbb2009-06-27 17:58:46 +0200247 die_errno("Could not finish '%s'",
248 filename);
Johannes Schindelin68f6c012007-07-03 01:33:54 +0100249 }
Linus Torvalds18af29f2007-01-21 22:26:41 -0800250 return;
251 }
252
253 /*
254 * Otherwise? It's there, it's unreachable, and some other unreachable
255 * object points to it. Ignore it - it's not interesting, and we showed
256 * all the interesting cases above.
257 */
258}
259
260static void check_object(struct object *obj)
261{
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100262 if (verbose)
263 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
264
Linus Torvalds18af29f2007-01-21 22:26:41 -0800265 if (obj->flags & REACHABLE)
266 check_reachable_object(obj);
267 else
268 check_unreachable_object(obj);
269}
Petr Baudisf1f0d082005-09-20 20:56:05 +0200270
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700271static void check_connectivity(void)
272{
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700273 int i, max;
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700274
Linus Torvalds04d39752008-12-10 19:44:37 -0800275 /* Traverse the pending reachable objects */
276 traverse_reachable();
277
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700278 /* Look up all the requirements, warn about missing objects.. */
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700279 max = get_max_object_index();
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100280 if (verbose)
281 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
282
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700283 for (i = 0; i < max; i++) {
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700284 struct object *obj = get_indexed_object(i);
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700285
Linus Torvalds18af29f2007-01-21 22:26:41 -0800286 if (obj)
287 check_object(obj);
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700288 }
289}
290
Nguyễn Thái Ngọc Duyc9486eb2011-11-07 09:59:25 +0700291static int fsck_obj(struct object *obj)
Linus Torvalds20222112005-04-08 15:02:42 -0700292{
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700293 if (obj->flags & SEEN)
294 return 0;
295 obj->flags |= SEEN;
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800296
Martin Koeglerba002f32008-02-25 22:46:08 +0100297 if (verbose)
298 fprintf(stderr, "Checking %s %s\n",
299 typename(obj->type), sha1_to_hex(obj->sha1));
300
Linus Torvalds2af202b2009-06-18 10:28:43 -0700301 if (fsck_walk(obj, mark_used, NULL))
Martin Koegler271b8d22008-02-25 22:46:05 +0100302 objerror(obj, "broken links");
Martin Koeglerba002f32008-02-25 22:46:08 +0100303 if (fsck_object(obj, check_strict, fsck_error_func))
304 return -1;
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700305
Martin Koeglerba002f32008-02-25 22:46:08 +0100306 if (obj->type == OBJ_TREE) {
307 struct tree *item = (struct tree *) obj;
308
309 free(item->buffer);
310 item->buffer = NULL;
311 }
312
313 if (obj->type == OBJ_COMMIT) {
314 struct commit *commit = (struct commit *) obj;
315
316 free(commit->buffer);
317 commit->buffer = NULL;
318
319 if (!commit->parents && show_root)
320 printf("root %s\n", sha1_to_hex(commit->object.sha1));
321 }
322
323 if (obj->type == OBJ_TAG) {
324 struct tag *tag = (struct tag *) obj;
325
326 if (show_tags && tag->tagged) {
327 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
328 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
329 }
330 }
331
332 return 0;
Linus Torvalds20222112005-04-08 15:02:42 -0700333}
334
Nguyễn Thái Ngọc Duyc9486eb2011-11-07 09:59:25 +0700335static int fsck_sha1(const unsigned char *sha1)
336{
337 struct object *obj = parse_object(sha1);
338 if (!obj) {
339 errors_found |= ERROR_OBJECT;
340 return error("%s: object corrupt or missing",
341 sha1_to_hex(sha1));
342 }
343 return fsck_obj(obj);
344}
345
346static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
347 unsigned long size, void *buffer, int *eaten)
348{
349 struct object *obj;
350 obj = parse_object_buffer(sha1, type, size, buffer, eaten);
351 if (!obj) {
352 errors_found |= ERROR_OBJECT;
353 return error("%s: object corrupt or missing", sha1_to_hex(sha1));
354 }
355 return fsck_obj(obj);
356}
357
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700358/*
359 * This is the sorting chunk size: make it reasonably
360 * big so that we can sort well..
361 */
362#define MAX_SHA1_ENTRIES (1024)
363
364struct sha1_entry {
365 unsigned long ino;
366 unsigned char sha1[20];
367};
368
369static struct {
370 unsigned long nr;
371 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
372} sha1_list;
373
374static int ino_compare(const void *_a, const void *_b)
375{
376 const struct sha1_entry *a = _a, *b = _b;
377 unsigned long ino1 = a->ino, ino2 = b->ino;
378 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
379}
380
381static void fsck_sha1_list(void)
382{
383 int i, nr = sha1_list.nr;
384
Junio C Hamano35a730f2006-01-19 17:13:51 -0800385 if (SORT_DIRENT)
386 qsort(sha1_list.entry, nr,
387 sizeof(struct sha1_entry *), ino_compare);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700388 for (i = 0; i < nr; i++) {
389 struct sha1_entry *entry = sha1_list.entry[i];
390 unsigned char *sha1 = entry->sha1;
391
392 sha1_list.entry[i] = NULL;
Petr Baudisf1f0d082005-09-20 20:56:05 +0200393 fsck_sha1(sha1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700394 free(entry);
395 }
396 sha1_list.nr = 0;
397}
398
399static void add_sha1_list(unsigned char *sha1, unsigned long ino)
400{
401 struct sha1_entry *entry = xmalloc(sizeof(*entry));
402 int nr;
403
404 entry->ino = ino;
Shawn Pearcee7024962006-08-23 02:49:00 -0400405 hashcpy(entry->sha1, sha1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700406 nr = sha1_list.nr;
407 if (nr == MAX_SHA1_ENTRIES) {
408 fsck_sha1_list();
409 nr = 0;
410 }
411 sha1_list.entry[nr] = entry;
412 sha1_list.nr = ++nr;
413}
414
Junio C Hamanoea6f0a22011-01-26 13:01:54 -0800415static inline int is_loose_object_file(struct dirent *de,
416 char *name, unsigned char *sha1)
417{
418 if (strlen(de->d_name) != 38)
419 return 0;
420 memcpy(name + 2, de->d_name, 39);
421 return !get_sha1_hex(name, sha1);
422}
423
David Rientjesb5524c82006-08-14 13:36:18 -0700424static void fsck_dir(int i, char *path)
Linus Torvalds20222112005-04-08 15:02:42 -0700425{
426 DIR *dir = opendir(path);
427 struct dirent *de;
Junio C Hamanoea6f0a22011-01-26 13:01:54 -0800428 char name[100];
Linus Torvalds20222112005-04-08 15:02:42 -0700429
Linus Torvalds230f1322005-10-08 15:54:01 -0700430 if (!dir)
David Rientjesb5524c82006-08-14 13:36:18 -0700431 return;
Linus Torvalds20222112005-04-08 15:02:42 -0700432
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100433 if (verbose)
434 fprintf(stderr, "Checking directory %s\n", path);
435
Junio C Hamanoea6f0a22011-01-26 13:01:54 -0800436 sprintf(name, "%02x", i);
Linus Torvalds20222112005-04-08 15:02:42 -0700437 while ((de = readdir(dir)) != NULL) {
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700438 unsigned char sha1[20];
Linus Torvalds20222112005-04-08 15:02:42 -0700439
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300440 if (is_dot_or_dotdot(de->d_name))
Linus Torvalds20222112005-04-08 15:02:42 -0700441 continue;
Junio C Hamanoea6f0a22011-01-26 13:01:54 -0800442 if (is_loose_object_file(de, name, sha1)) {
Junio C Hamano35a730f2006-01-19 17:13:51 -0800443 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700444 continue;
Linus Torvalds20222112005-04-08 15:02:42 -0700445 }
Brandon Casey3d32a462008-08-05 13:01:50 -0500446 if (!prefixcmp(de->d_name, "tmp_obj_"))
Shawn O. Pearcea08c53a2008-07-26 21:33:00 -0500447 continue;
Linus Torvalds20222112005-04-08 15:02:42 -0700448 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
449 }
450 closedir(dir);
Linus Torvalds20222112005-04-08 15:02:42 -0700451}
452
David Rientjes96f1e582006-08-15 10:23:48 -0700453static int default_refs;
Linus Torvalds944d8582005-07-03 10:01:38 -0700454
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100455static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
456 const char *email, unsigned long timestamp, int tz,
457 const char *message, void *cb_data)
Junio C Hamano55dd5522006-12-18 01:36:16 -0800458{
459 struct object *obj;
460
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100461 if (verbose)
462 fprintf(stderr, "Checking reflog %s->%s\n",
463 sha1_to_hex(osha1), sha1_to_hex(nsha1));
464
Junio C Hamano55dd5522006-12-18 01:36:16 -0800465 if (!is_null_sha1(osha1)) {
466 obj = lookup_object(osha1);
467 if (obj) {
468 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100469 mark_object_reachable(obj);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800470 }
471 }
472 obj = lookup_object(nsha1);
473 if (obj) {
474 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100475 mark_object_reachable(obj);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800476 }
477 return 0;
478}
479
Nicolas Pitreeb8381c2007-02-03 13:25:43 -0500480static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
481{
482 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
483 return 0;
484}
485
Linus Torvalds6232f622008-01-15 16:34:17 -0800486static int is_branch(const char *refname)
487{
488 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
489}
490
Junio C Hamano8da19772006-09-20 22:02:01 -0700491static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
Linus Torvalds10249322005-05-18 10:16:14 -0700492{
Linus Torvalds10249322005-05-18 10:16:14 -0700493 struct object *obj;
494
Linus Torvalds6232f622008-01-15 16:34:17 -0800495 obj = parse_object(sha1);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700496 if (!obj) {
Linus Torvalds944d8582005-07-03 10:01:38 -0700497 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
498 /* We'll continue with the rest despite the error.. */
499 return 0;
Junio C Hamano8a498a02005-06-28 14:58:33 -0700500 }
Linus Torvalds6232f622008-01-15 16:34:17 -0800501 if (obj->type != OBJ_COMMIT && is_branch(refname))
502 error("%s: not a commit", refname);
Linus Torvalds944d8582005-07-03 10:01:38 -0700503 default_refs++;
Linus Torvalds10249322005-05-18 10:16:14 -0700504 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100505 mark_object_reachable(obj);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800506
Linus Torvalds7c4d07c2005-05-20 07:49:17 -0700507 return 0;
Linus Torvalds10249322005-05-18 10:16:14 -0700508}
509
Linus Torvalds10249322005-05-18 10:16:14 -0700510static void get_default_heads(void)
511{
Junio C Hamano469e2eb2009-01-30 00:33:00 -0800512 if (head_points_at && !is_null_sha1(head_sha1))
513 fsck_handle_ref("HEAD", head_sha1, 0, NULL);
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700514 for_each_ref(fsck_handle_ref, NULL);
Shawn O. Pearce566842f2007-04-04 10:46:14 -0400515 if (include_reflogs)
516 for_each_reflog(fsck_handle_reflog, NULL);
Linus Torvalds071fa892006-08-29 11:47:30 -0700517
518 /*
519 * Not having any default heads isn't really fatal, but
520 * it does mean that "--unreachable" no longer makes any
521 * sense (since in this case everything will obviously
522 * be unreachable by definition.
523 *
524 * Showing dangling objects is valid, though (as those
525 * dangling objects are likely lost heads).
526 *
527 * So we just print a warning about it, and clear the
528 * "show_unreachable" flag.
529 */
530 if (!default_refs) {
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700531 fprintf(stderr, "notice: No default references\n");
Linus Torvalds071fa892006-08-29 11:47:30 -0700532 show_unreachable = 0;
533 }
Linus Torvalds10249322005-05-18 10:16:14 -0700534}
535
Junio C Hamano8a498a02005-06-28 14:58:33 -0700536static void fsck_object_dir(const char *path)
537{
538 int i;
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700539 struct progress *progress = NULL;
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100540
541 if (verbose)
542 fprintf(stderr, "Checking object directory\n");
543
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700544 if (show_progress)
545 progress = start_progress("Checking object directories", 256);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700546 for (i = 0; i < 256; i++) {
547 static char dir[4096];
548 sprintf(dir, "%s/%02x", path, i);
549 fsck_dir(i, dir);
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700550 display_progress(progress, i+1);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700551 }
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700552 stop_progress(&progress);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700553 fsck_sha1_list();
554}
555
Linus Torvaldsc3330382005-07-03 10:40:38 -0700556static int fsck_head_link(void)
557{
Junio C Hamano8da19772006-09-20 22:02:01 -0700558 int flag;
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700559 int null_is_error = 0;
Linus Torvaldsc3330382005-07-03 10:40:38 -0700560
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100561 if (verbose)
562 fprintf(stderr, "Checking HEAD link\n");
563
Nguyễn Thái Ngọc Duy8cad4742011-12-12 18:20:32 +0700564 head_points_at = resolve_ref_unsafe("HEAD", head_sha1, 0, &flag);
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700565 if (!head_points_at)
566 return error("Invalid HEAD");
567 if (!strcmp(head_points_at, "HEAD"))
568 /* detached HEAD */
569 null_is_error = 1;
570 else if (prefixcmp(head_points_at, "refs/heads/"))
Junio C Hamano8098a172005-09-30 14:26:57 -0700571 return error("HEAD points to something strange (%s)",
Junio C Hamano5b10b092006-09-18 01:08:00 -0700572 head_points_at);
Junio C Hamano469e2eb2009-01-30 00:33:00 -0800573 if (is_null_sha1(head_sha1)) {
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700574 if (null_is_error)
575 return error("HEAD: detached HEAD points at nothing");
576 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
577 head_points_at + 11);
578 }
Linus Torvaldsc3330382005-07-03 10:40:38 -0700579 return 0;
580}
581
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700582static int fsck_cache_tree(struct cache_tree *it)
583{
584 int i;
585 int err = 0;
586
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100587 if (verbose)
588 fprintf(stderr, "Checking cache tree\n");
589
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700590 if (0 <= it->entry_count) {
591 struct object *obj = parse_object(it->sha1);
Junio C Hamano6d60bbe2006-05-03 21:17:45 -0700592 if (!obj) {
593 error("%s: invalid sha1 pointer in cache-tree",
594 sha1_to_hex(it->sha1));
595 return 1;
596 }
Junio C Hamanocdc08b32006-05-01 22:15:54 -0700597 obj->used = 1;
Junio C Hamanoa1cdc252011-01-26 12:46:55 -0800598 mark_object_reachable(obj);
Linus Torvalds19746322006-07-11 20:45:31 -0700599 if (obj->type != OBJ_TREE)
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700600 err |= objerror(obj, "non-tree in cache-tree");
601 }
602 for (i = 0; i < it->subtree_nr; i++)
603 err |= fsck_cache_tree(it->down[i]->cache_tree);
604 return err;
605}
606
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200607static char const * const fsck_usage[] = {
Nguyễn Thái Ngọc Duycf8fe312012-08-20 19:32:13 +0700608 N_("git fsck [options] [<object>...]"),
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200609 NULL
610};
611
612static struct option fsck_opts[] = {
Nguyễn Thái Ngọc Duycf8fe312012-08-20 19:32:13 +0700613 OPT__VERBOSE(&verbose, N_("be verbose")),
614 OPT_BOOLEAN(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
615 OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
616 OPT_BOOLEAN(0, "tags", &show_tags, N_("report tags")),
617 OPT_BOOLEAN(0, "root", &show_root, N_("report root nodes")),
618 OPT_BOOLEAN(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
619 OPT_BOOLEAN(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
620 OPT_BOOLEAN(0, "full", &check_full, N_("also consider packs and alternate objects")),
621 OPT_BOOLEAN(0, "strict", &check_strict, N_("enable more strict checking")),
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200622 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
Nguyễn Thái Ngọc Duycf8fe312012-08-20 19:32:13 +0700623 N_("write dangling objects in .git/lost-found")),
624 OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200625 OPT_END(),
626};
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800627
Peter Hagervallbaffc0e2007-07-15 01:14:45 +0200628int cmd_fsck(int argc, const char **argv, const char *prefix)
Linus Torvalds20222112005-04-08 15:02:42 -0700629{
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700630 int i, heads;
Junio C Hamanoe15ef662009-01-30 00:50:54 -0800631 struct alternate_object_database *alt;
Linus Torvalds20222112005-04-08 15:02:42 -0700632
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800633 errors_found = 0;
Christian Couderdae556b2009-01-23 10:07:46 +0100634 read_replace_refs = 0;
Junio C Hamano61e2b012005-11-25 23:52:04 -0800635
Stephen Boyd37782922009-05-23 11:53:12 -0700636 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700637
638 if (show_progress == -1)
639 show_progress = isatty(2);
640 if (verbose)
641 show_progress = 0;
642
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200643 if (write_lost_and_found) {
644 check_full = 1;
645 include_reflogs = 0;
Linus Torvalds889262e2005-04-25 16:31:13 -0700646 }
647
Linus Torvaldsc3330382005-07-03 10:40:38 -0700648 fsck_head_link();
Junio C Hamano8a498a02005-06-28 14:58:33 -0700649 fsck_object_dir(get_object_directory());
Junio C Hamanoe15ef662009-01-30 00:50:54 -0800650
651 prepare_alt_odb();
652 for (alt = alt_odb_list; alt; alt = alt->next) {
653 char namebuf[PATH_MAX];
654 int namelen = alt->name - alt->base;
655 memcpy(namebuf, alt->base, namelen);
656 namebuf[namelen - 1] = 0;
657 fsck_object_dir(namebuf);
658 }
659
Junio C Hamano8a498a02005-06-28 14:58:33 -0700660 if (check_full) {
Junio C Hamano8a498a02005-06-28 14:58:33 -0700661 struct packed_git *p;
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700662 uint32_t total = 0, count = 0;
663 struct progress *progress = NULL;
Junio C Hamanoe15ef662009-01-30 00:50:54 -0800664
Junio C Hamano8a498a02005-06-28 14:58:33 -0700665 prepare_packed_git();
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700666
667 if (show_progress) {
668 for (p = packed_git; p; p = p->next) {
669 if (open_pack_index(p))
670 continue;
671 total += p->num_objects;
672 }
673
674 progress = start_progress("Checking objects", total);
675 }
676 for (p = packed_git; p; p = p->next) {
Junio C Hamanof9253392005-06-29 02:51:27 -0700677 /* verify gives error messages itself */
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700678 if (verify_pack(p, fsck_obj_buffer,
679 progress, count))
Nguyễn Thái Ngọc Duya3ed7552011-11-07 09:59:23 +0700680 errors_found |= ERROR_PACK;
Nguyễn Thái Ngọc Duy1e49f222011-11-07 09:59:26 +0700681 count += p->num_objects;
682 }
683 stop_progress(&progress);
Linus Torvalds20222112005-04-08 15:02:42 -0700684 }
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700685
686 heads = 0;
Christian Couder3aed2fd2009-01-18 04:46:09 +0100687 for (i = 0; i < argc; i++) {
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700688 const char *arg = argv[i];
Junio C Hamano469e2eb2009-01-30 00:33:00 -0800689 unsigned char sha1[20];
690 if (!get_sha1(arg, sha1)) {
691 struct object *obj = lookup_object(sha1);
Jonas Fonsecae1a13882005-04-29 20:00:40 -0700692
Linus Torvalds770896e2005-05-04 17:03:09 -0700693 /* Error is printed by lookup_object(). */
694 if (!obj)
Jonas Fonsecae1a13882005-04-29 20:00:40 -0700695 continue;
696
Daniel Barkalowff5ebe32005-04-18 11:39:48 -0700697 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100698 mark_object_reachable(obj);
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700699 heads++;
700 continue;
701 }
Petr Baudisf1f0d082005-09-20 20:56:05 +0200702 error("invalid parameter: expected sha1, got '%s'", arg);
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700703 }
704
Linus Torvalds10249322005-05-18 10:16:14 -0700705 /*
Nicolas Pitred1af0022005-05-20 16:59:17 -0400706 * If we've not been given any explicit head information, do the
Linus Torvaldse7bd9072005-05-18 10:19:59 -0700707 * default ones from .git/refs. We also consider the index file
708 * in this case (ie this implies --cache).
Linus Torvalds10249322005-05-18 10:16:14 -0700709 */
Linus Torvaldse7bd9072005-05-18 10:19:59 -0700710 if (!heads) {
Linus Torvalds10249322005-05-18 10:16:14 -0700711 get_default_heads();
712 keep_cache_objects = 1;
713 }
714
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700715 if (keep_cache_objects) {
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700716 read_cache();
717 for (i = 0; i < active_nr; i++) {
Linus Torvalds8d9721c2007-04-09 21:15:29 -0700718 unsigned int mode;
719 struct blob *blob;
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700720 struct object *obj;
Linus Torvalds8d9721c2007-04-09 21:15:29 -0700721
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800722 mode = active_cache[i]->ce_mode;
Martin Waitz302b9282007-05-21 22:08:28 +0200723 if (S_ISGITLINK(mode))
Linus Torvalds8d9721c2007-04-09 21:15:29 -0700724 continue;
725 blob = lookup_blob(active_cache[i]->sha1);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700726 if (!blob)
727 continue;
728 obj = &blob->object;
729 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100730 mark_object_reachable(obj);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700731 }
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700732 if (active_cache_tree)
733 fsck_cache_tree(active_cache_tree);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700734 }
735
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700736 check_connectivity();
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800737 return errors_found;
Linus Torvalds20222112005-04-08 15:02:42 -0700738}