blob: 5ae0366bc8cfe52f226c2af151a66d39b9df02aa [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;
Junio C Hamanof29cd392009-10-20 11:46:55 -070022static int check_full = 1;
David Rientjes96f1e582006-08-15 10:23:48 -070023static int check_strict;
24static int keep_cache_objects;
Linus Torvaldsd9839e02005-04-13 09:57:30 -070025static unsigned char head_sha1[20];
Junio C Hamano469e2eb2009-01-30 00:33:00 -080026static const char *head_points_at;
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080027static int errors_found;
Johannes Schindelin68f6c012007-07-03 01:33:54 +010028static int write_lost_and_found;
Johannes Schindelin20f1eb62007-06-05 03:44:00 +010029static int verbose;
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080030#define ERROR_OBJECT 01
31#define ERROR_REACHABLE 02
Linus Torvaldsd9839e02005-04-13 09:57:30 -070032
Timo Hirvonen962554c2006-02-26 17:13:46 +020033#ifdef NO_D_INO_IN_DIRENT
Junio C Hamano35a730f2006-01-19 17:13:51 -080034#define SORT_DIRENT 0
35#define DIRENT_SORT_HINT(de) 0
36#else
37#define SORT_DIRENT 1
38#define DIRENT_SORT_HINT(de) ((de)->d_ino)
39#endif
Petr Baudisf1f0d082005-09-20 20:56:05 +020040
41static void objreport(struct object *obj, const char *severity,
42 const char *err, va_list params)
43{
44 fprintf(stderr, "%s in %s %s: ",
Linus Torvalds885a86a2006-06-14 16:45:13 -070045 severity, typename(obj->type), sha1_to_hex(obj->sha1));
Petr Baudisf1f0d082005-09-20 20:56:05 +020046 vfprintf(stderr, err, params);
47 fputs("\n", stderr);
48}
49
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080050__attribute__((format (printf, 2, 3)))
Peter Hagervalla7928f82005-09-28 14:04:54 +020051static int objerror(struct object *obj, const char *err, ...)
Petr Baudisf1f0d082005-09-20 20:56:05 +020052{
53 va_list params;
54 va_start(params, err);
Junio C Hamanoe2b4f632007-03-05 00:22:06 -080055 errors_found |= ERROR_OBJECT;
Petr Baudisf1f0d082005-09-20 20:56:05 +020056 objreport(obj, "error", err, params);
57 va_end(params);
58 return -1;
59}
60
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080061__attribute__((format (printf, 3, 4)))
Martin Koeglerba002f32008-02-25 22:46:08 +010062static int fsck_error_func(struct object *obj, int type, const char *err, ...)
Petr Baudisf1f0d082005-09-20 20:56:05 +020063{
64 va_list params;
65 va_start(params, err);
Martin Koeglerba002f32008-02-25 22:46:08 +010066 objreport(obj, (type == FSCK_WARN) ? "warning" : "error", err, params);
Petr Baudisf1f0d082005-09-20 20:56:05 +020067 va_end(params);
Martin Koeglerba002f32008-02-25 22:46:08 +010068 return (type == FSCK_WARN) ? 0 : 1;
Petr Baudisf1f0d082005-09-20 20:56:05 +020069}
70
Linus Torvalds04d39752008-12-10 19:44:37 -080071static struct object_array pending;
72
Martin Koegler271b8d22008-02-25 22:46:05 +010073static int mark_object(struct object *obj, int type, void *data)
74{
Martin Koegler271b8d22008-02-25 22:46:05 +010075 struct object *parent = data;
Martin Koegler271b8d22008-02-25 22:46:05 +010076
Junio C Hamanoa1cdc252011-01-26 12:46:55 -080077 /*
78 * The only case data is NULL or type is OBJ_ANY is when
79 * mark_object_reachable() calls us. All the callers of
80 * that function has non-NULL obj hence ...
81 */
Martin Koegler271b8d22008-02-25 22:46:05 +010082 if (!obj) {
Junio C Hamanoa1cdc252011-01-26 12:46:55 -080083 /* ... these references to parent->fld are safe here */
Martin Koegler271b8d22008-02-25 22:46:05 +010084 printf("broken link from %7s %s\n",
85 typename(parent->type), sha1_to_hex(parent->sha1));
86 printf("broken link from %7s %s\n",
87 (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
88 errors_found |= ERROR_REACHABLE;
89 return 1;
90 }
91
92 if (type != OBJ_ANY && obj->type != type)
Junio C Hamanoa1cdc252011-01-26 12:46:55 -080093 /* ... and the reference to parent is safe here */
Martin Koegler271b8d22008-02-25 22:46:05 +010094 objerror(parent, "wrong object type in link");
95
96 if (obj->flags & REACHABLE)
97 return 0;
98 obj->flags |= REACHABLE;
99 if (!obj->parsed) {
100 if (parent && !has_sha1_file(obj->sha1)) {
101 printf("broken link from %7s %s\n",
102 typename(parent->type), sha1_to_hex(parent->sha1));
103 printf(" to %7s %s\n",
104 typename(obj->type), sha1_to_hex(obj->sha1));
105 errors_found |= ERROR_REACHABLE;
106 }
107 return 1;
108 }
109
Linus Torvalds04d39752008-12-10 19:44:37 -0800110 add_object_array(obj, (void *) parent, &pending);
111 return 0;
112}
113
114static void mark_object_reachable(struct object *obj)
115{
Linus Torvalds2af202b2009-06-18 10:28:43 -0700116 mark_object(obj, OBJ_ANY, NULL);
Linus Torvalds04d39752008-12-10 19:44:37 -0800117}
118
Junio C Hamanoa1cdc252011-01-26 12:46:55 -0800119static int traverse_one_object(struct object *obj)
Linus Torvalds04d39752008-12-10 19:44:37 -0800120{
121 int result;
122 struct tree *tree = NULL;
123
Martin Koegler271b8d22008-02-25 22:46:05 +0100124 if (obj->type == OBJ_TREE) {
125 obj->parsed = 0;
126 tree = (struct tree *)obj;
127 if (parse_tree(tree) < 0)
128 return 1; /* error already displayed */
129 }
130 result = fsck_walk(obj, mark_object, obj);
131 if (tree) {
132 free(tree->buffer);
133 tree->buffer = NULL;
134 }
Martin Koegler271b8d22008-02-25 22:46:05 +0100135 return result;
136}
137
Linus Torvalds04d39752008-12-10 19:44:37 -0800138static int traverse_reachable(void)
Martin Koegler271b8d22008-02-25 22:46:05 +0100139{
Linus Torvalds04d39752008-12-10 19:44:37 -0800140 int result = 0;
141 while (pending.nr) {
142 struct object_array_entry *entry;
Johannes Schindelinc0aa3352011-03-22 13:50:08 +0100143 struct object *obj;
Linus Torvalds04d39752008-12-10 19:44:37 -0800144
145 entry = pending.objects + --pending.nr;
146 obj = entry->item;
Junio C Hamanoa1cdc252011-01-26 12:46:55 -0800147 result |= traverse_one_object(obj);
Linus Torvalds04d39752008-12-10 19:44:37 -0800148 }
149 return !!result;
Martin Koegler271b8d22008-02-25 22:46:05 +0100150}
151
152static int mark_used(struct object *obj, int type, void *data)
153{
154 if (!obj)
155 return 1;
156 obj->used = 1;
157 return 0;
Petr Baudisf1f0d082005-09-20 20:56:05 +0200158}
159
Linus Torvalds18af29f2007-01-21 22:26:41 -0800160/*
161 * Check a single reachable object
162 */
163static void check_reachable_object(struct object *obj)
164{
Linus Torvalds18af29f2007-01-21 22:26:41 -0800165 /*
166 * We obviously want the object to be parsed,
167 * except if it was in a pack-file and we didn't
168 * do a full fsck
169 */
170 if (!obj->parsed) {
Junio C Hamanocd673c12009-02-27 23:15:53 -0800171 if (has_sha1_pack(obj->sha1))
Linus Torvalds18af29f2007-01-21 22:26:41 -0800172 return; /* it is in pack - forget about it */
173 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800174 errors_found |= ERROR_REACHABLE;
Linus Torvalds18af29f2007-01-21 22:26:41 -0800175 return;
176 }
Linus Torvalds18af29f2007-01-21 22:26:41 -0800177}
178
179/*
180 * Check a single unreachable object
181 */
182static void check_unreachable_object(struct object *obj)
183{
184 /*
185 * Missing unreachable object? Ignore it. It's not like
186 * we miss it (since it can't be reached), nor do we want
187 * to complain about it being unreachable (since it does
188 * not exist).
189 */
190 if (!obj->parsed)
191 return;
192
193 /*
194 * Unreachable object that exists? Show it if asked to,
195 * since this is something that is prunable.
196 */
197 if (show_unreachable) {
198 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
199 return;
200 }
201
202 /*
203 * "!used" means that nothing at all points to it, including
Pavel Roskin3dff5372007-02-03 23:49:16 -0500204 * other unreachable objects. In other words, it's the "tip"
Linus Torvalds18af29f2007-01-21 22:26:41 -0800205 * of some set of unreachable objects, usually a commit that
206 * got dropped.
207 *
208 * Such starting points are more interesting than some random
209 * set of unreachable objects, so we show them even if the user
210 * hasn't asked for _all_ unreachable objects. If you have
211 * deleted a branch by mistake, this is a prime candidate to
212 * start looking at, for example.
213 */
214 if (!obj->used) {
215 printf("dangling %s %s\n", typename(obj->type),
216 sha1_to_hex(obj->sha1));
Johannes Schindelin68f6c012007-07-03 01:33:54 +0100217 if (write_lost_and_found) {
218 char *filename = git_path("lost-found/%s/%s",
219 obj->type == OBJ_COMMIT ? "commit" : "other",
220 sha1_to_hex(obj->sha1));
221 FILE *f;
222
223 if (safe_create_leading_directories(filename)) {
224 error("Could not create lost-found");
225 return;
226 }
227 if (!(f = fopen(filename, "w")))
Thomas Rast0721c312009-06-27 17:58:47 +0200228 die_errno("Could not open '%s'", filename);
Johannes Schindelin16a7fcf2007-07-22 21:20:26 +0100229 if (obj->type == OBJ_BLOB) {
230 enum object_type type;
231 unsigned long size;
232 char *buf = read_sha1_file(obj->sha1,
233 &type, &size);
234 if (buf) {
Alex Riesen47d32af2008-12-05 01:35:48 +0100235 if (fwrite(buf, size, 1, f) != 1)
Thomas Rastd824cbb2009-06-27 17:58:46 +0200236 die_errno("Could not write '%s'",
237 filename);
Johannes Schindelin16a7fcf2007-07-22 21:20:26 +0100238 free(buf);
239 }
240 } else
241 fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
Alex Riesen47d32af2008-12-05 01:35:48 +0100242 if (fclose(f))
Thomas Rastd824cbb2009-06-27 17:58:46 +0200243 die_errno("Could not finish '%s'",
244 filename);
Johannes Schindelin68f6c012007-07-03 01:33:54 +0100245 }
Linus Torvalds18af29f2007-01-21 22:26:41 -0800246 return;
247 }
248
249 /*
250 * Otherwise? It's there, it's unreachable, and some other unreachable
251 * object points to it. Ignore it - it's not interesting, and we showed
252 * all the interesting cases above.
253 */
254}
255
256static void check_object(struct object *obj)
257{
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100258 if (verbose)
259 fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
260
Linus Torvalds18af29f2007-01-21 22:26:41 -0800261 if (obj->flags & REACHABLE)
262 check_reachable_object(obj);
263 else
264 check_unreachable_object(obj);
265}
Petr Baudisf1f0d082005-09-20 20:56:05 +0200266
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700267static void check_connectivity(void)
268{
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700269 int i, max;
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700270
Linus Torvalds04d39752008-12-10 19:44:37 -0800271 /* Traverse the pending reachable objects */
272 traverse_reachable();
273
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700274 /* Look up all the requirements, warn about missing objects.. */
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700275 max = get_max_object_index();
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100276 if (verbose)
277 fprintf(stderr, "Checking connectivity (%d objects)\n", max);
278
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700279 for (i = 0; i < max; i++) {
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700280 struct object *obj = get_indexed_object(i);
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700281
Linus Torvalds18af29f2007-01-21 22:26:41 -0800282 if (obj)
283 check_object(obj);
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700284 }
285}
286
Nicolas Pitred72308e2007-04-04 16:49:04 -0400287static int fsck_sha1(const unsigned char *sha1)
Linus Torvalds20222112005-04-08 15:02:42 -0700288{
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700289 struct object *obj = parse_object(sha1);
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800290 if (!obj) {
291 errors_found |= ERROR_OBJECT;
292 return error("%s: object corrupt or missing",
293 sha1_to_hex(sha1));
294 }
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700295 if (obj->flags & SEEN)
296 return 0;
297 obj->flags |= SEEN;
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800298
Martin Koeglerba002f32008-02-25 22:46:08 +0100299 if (verbose)
300 fprintf(stderr, "Checking %s %s\n",
301 typename(obj->type), sha1_to_hex(obj->sha1));
302
Linus Torvalds2af202b2009-06-18 10:28:43 -0700303 if (fsck_walk(obj, mark_used, NULL))
Martin Koegler271b8d22008-02-25 22:46:05 +0100304 objerror(obj, "broken links");
Martin Koeglerba002f32008-02-25 22:46:08 +0100305 if (fsck_object(obj, check_strict, fsck_error_func))
306 return -1;
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700307
Martin Koeglerba002f32008-02-25 22:46:08 +0100308 if (obj->type == OBJ_TREE) {
309 struct tree *item = (struct tree *) obj;
310
311 free(item->buffer);
312 item->buffer = NULL;
313 }
314
315 if (obj->type == OBJ_COMMIT) {
316 struct commit *commit = (struct commit *) obj;
317
318 free(commit->buffer);
319 commit->buffer = NULL;
320
321 if (!commit->parents && show_root)
322 printf("root %s\n", sha1_to_hex(commit->object.sha1));
323 }
324
325 if (obj->type == OBJ_TAG) {
326 struct tag *tag = (struct tag *) obj;
327
328 if (show_tags && tag->tagged) {
329 printf("tagged %s %s", typename(tag->tagged->type), sha1_to_hex(tag->tagged->sha1));
330 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
331 }
332 }
333
334 return 0;
Linus Torvalds20222112005-04-08 15:02:42 -0700335}
336
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700337/*
338 * This is the sorting chunk size: make it reasonably
339 * big so that we can sort well..
340 */
341#define MAX_SHA1_ENTRIES (1024)
342
343struct sha1_entry {
344 unsigned long ino;
345 unsigned char sha1[20];
346};
347
348static struct {
349 unsigned long nr;
350 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
351} sha1_list;
352
353static int ino_compare(const void *_a, const void *_b)
354{
355 const struct sha1_entry *a = _a, *b = _b;
356 unsigned long ino1 = a->ino, ino2 = b->ino;
357 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
358}
359
360static void fsck_sha1_list(void)
361{
362 int i, nr = sha1_list.nr;
363
Junio C Hamano35a730f2006-01-19 17:13:51 -0800364 if (SORT_DIRENT)
365 qsort(sha1_list.entry, nr,
366 sizeof(struct sha1_entry *), ino_compare);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700367 for (i = 0; i < nr; i++) {
368 struct sha1_entry *entry = sha1_list.entry[i];
369 unsigned char *sha1 = entry->sha1;
370
371 sha1_list.entry[i] = NULL;
Petr Baudisf1f0d082005-09-20 20:56:05 +0200372 fsck_sha1(sha1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700373 free(entry);
374 }
375 sha1_list.nr = 0;
376}
377
378static void add_sha1_list(unsigned char *sha1, unsigned long ino)
379{
380 struct sha1_entry *entry = xmalloc(sizeof(*entry));
381 int nr;
382
383 entry->ino = ino;
Shawn Pearcee7024962006-08-23 02:49:00 -0400384 hashcpy(entry->sha1, sha1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700385 nr = sha1_list.nr;
386 if (nr == MAX_SHA1_ENTRIES) {
387 fsck_sha1_list();
388 nr = 0;
389 }
390 sha1_list.entry[nr] = entry;
391 sha1_list.nr = ++nr;
392}
393
Junio C Hamanoea6f0a22011-01-26 13:01:54 -0800394static inline int is_loose_object_file(struct dirent *de,
395 char *name, unsigned char *sha1)
396{
397 if (strlen(de->d_name) != 38)
398 return 0;
399 memcpy(name + 2, de->d_name, 39);
400 return !get_sha1_hex(name, sha1);
401}
402
David Rientjesb5524c82006-08-14 13:36:18 -0700403static void fsck_dir(int i, char *path)
Linus Torvalds20222112005-04-08 15:02:42 -0700404{
405 DIR *dir = opendir(path);
406 struct dirent *de;
Junio C Hamanoea6f0a22011-01-26 13:01:54 -0800407 char name[100];
Linus Torvalds20222112005-04-08 15:02:42 -0700408
Linus Torvalds230f1322005-10-08 15:54:01 -0700409 if (!dir)
David Rientjesb5524c82006-08-14 13:36:18 -0700410 return;
Linus Torvalds20222112005-04-08 15:02:42 -0700411
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100412 if (verbose)
413 fprintf(stderr, "Checking directory %s\n", path);
414
Junio C Hamanoea6f0a22011-01-26 13:01:54 -0800415 sprintf(name, "%02x", i);
Linus Torvalds20222112005-04-08 15:02:42 -0700416 while ((de = readdir(dir)) != NULL) {
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700417 unsigned char sha1[20];
Linus Torvalds20222112005-04-08 15:02:42 -0700418
Alexander Potashev8ca12c02009-01-10 15:07:50 +0300419 if (is_dot_or_dotdot(de->d_name))
Linus Torvalds20222112005-04-08 15:02:42 -0700420 continue;
Junio C Hamanoea6f0a22011-01-26 13:01:54 -0800421 if (is_loose_object_file(de, name, sha1)) {
Junio C Hamano35a730f2006-01-19 17:13:51 -0800422 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700423 continue;
Linus Torvalds20222112005-04-08 15:02:42 -0700424 }
Brandon Casey3d32a462008-08-05 13:01:50 -0500425 if (!prefixcmp(de->d_name, "tmp_obj_"))
Shawn O. Pearcea08c53a2008-07-26 21:33:00 -0500426 continue;
Linus Torvalds20222112005-04-08 15:02:42 -0700427 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
428 }
429 closedir(dir);
Linus Torvalds20222112005-04-08 15:02:42 -0700430}
431
David Rientjes96f1e582006-08-15 10:23:48 -0700432static int default_refs;
Linus Torvalds944d8582005-07-03 10:01:38 -0700433
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100434static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
435 const char *email, unsigned long timestamp, int tz,
436 const char *message, void *cb_data)
Junio C Hamano55dd5522006-12-18 01:36:16 -0800437{
438 struct object *obj;
439
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100440 if (verbose)
441 fprintf(stderr, "Checking reflog %s->%s\n",
442 sha1_to_hex(osha1), sha1_to_hex(nsha1));
443
Junio C Hamano55dd5522006-12-18 01:36:16 -0800444 if (!is_null_sha1(osha1)) {
445 obj = lookup_object(osha1);
446 if (obj) {
447 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100448 mark_object_reachable(obj);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800449 }
450 }
451 obj = lookup_object(nsha1);
452 if (obj) {
453 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100454 mark_object_reachable(obj);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800455 }
456 return 0;
457}
458
Nicolas Pitreeb8381c2007-02-03 13:25:43 -0500459static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
460{
461 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
462 return 0;
463}
464
Linus Torvalds6232f622008-01-15 16:34:17 -0800465static int is_branch(const char *refname)
466{
467 return !strcmp(refname, "HEAD") || !prefixcmp(refname, "refs/heads/");
468}
469
Junio C Hamano8da19772006-09-20 22:02:01 -0700470static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
Linus Torvalds10249322005-05-18 10:16:14 -0700471{
Linus Torvalds10249322005-05-18 10:16:14 -0700472 struct object *obj;
473
Linus Torvalds6232f622008-01-15 16:34:17 -0800474 obj = parse_object(sha1);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700475 if (!obj) {
Linus Torvalds944d8582005-07-03 10:01:38 -0700476 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
477 /* We'll continue with the rest despite the error.. */
478 return 0;
Junio C Hamano8a498a02005-06-28 14:58:33 -0700479 }
Linus Torvalds6232f622008-01-15 16:34:17 -0800480 if (obj->type != OBJ_COMMIT && is_branch(refname))
481 error("%s: not a commit", refname);
Linus Torvalds944d8582005-07-03 10:01:38 -0700482 default_refs++;
Linus Torvalds10249322005-05-18 10:16:14 -0700483 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100484 mark_object_reachable(obj);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800485
Linus Torvalds7c4d07c2005-05-20 07:49:17 -0700486 return 0;
Linus Torvalds10249322005-05-18 10:16:14 -0700487}
488
Linus Torvalds10249322005-05-18 10:16:14 -0700489static void get_default_heads(void)
490{
Junio C Hamano469e2eb2009-01-30 00:33:00 -0800491 if (head_points_at && !is_null_sha1(head_sha1))
492 fsck_handle_ref("HEAD", head_sha1, 0, NULL);
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700493 for_each_ref(fsck_handle_ref, NULL);
Shawn O. Pearce566842f2007-04-04 10:46:14 -0400494 if (include_reflogs)
495 for_each_reflog(fsck_handle_reflog, NULL);
Linus Torvalds071fa892006-08-29 11:47:30 -0700496
497 /*
498 * Not having any default heads isn't really fatal, but
499 * it does mean that "--unreachable" no longer makes any
500 * sense (since in this case everything will obviously
501 * be unreachable by definition.
502 *
503 * Showing dangling objects is valid, though (as those
504 * dangling objects are likely lost heads).
505 *
506 * So we just print a warning about it, and clear the
507 * "show_unreachable" flag.
508 */
509 if (!default_refs) {
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700510 fprintf(stderr, "notice: No default references\n");
Linus Torvalds071fa892006-08-29 11:47:30 -0700511 show_unreachable = 0;
512 }
Linus Torvalds10249322005-05-18 10:16:14 -0700513}
514
Junio C Hamano8a498a02005-06-28 14:58:33 -0700515static void fsck_object_dir(const char *path)
516{
517 int i;
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100518
519 if (verbose)
520 fprintf(stderr, "Checking object directory\n");
521
Junio C Hamano8a498a02005-06-28 14:58:33 -0700522 for (i = 0; i < 256; i++) {
523 static char dir[4096];
524 sprintf(dir, "%s/%02x", path, i);
525 fsck_dir(i, dir);
526 }
527 fsck_sha1_list();
528}
529
Linus Torvaldsc3330382005-07-03 10:40:38 -0700530static int fsck_head_link(void)
531{
Junio C Hamano8da19772006-09-20 22:02:01 -0700532 int flag;
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700533 int null_is_error = 0;
Linus Torvaldsc3330382005-07-03 10:40:38 -0700534
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100535 if (verbose)
536 fprintf(stderr, "Checking HEAD link\n");
537
Junio C Hamano469e2eb2009-01-30 00:33:00 -0800538 head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag);
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700539 if (!head_points_at)
540 return error("Invalid HEAD");
541 if (!strcmp(head_points_at, "HEAD"))
542 /* detached HEAD */
543 null_is_error = 1;
544 else if (prefixcmp(head_points_at, "refs/heads/"))
Junio C Hamano8098a172005-09-30 14:26:57 -0700545 return error("HEAD points to something strange (%s)",
Junio C Hamano5b10b092006-09-18 01:08:00 -0700546 head_points_at);
Junio C Hamano469e2eb2009-01-30 00:33:00 -0800547 if (is_null_sha1(head_sha1)) {
Junio C Hamano8eb2d0b2007-04-11 01:28:43 -0700548 if (null_is_error)
549 return error("HEAD: detached HEAD points at nothing");
550 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
551 head_points_at + 11);
552 }
Linus Torvaldsc3330382005-07-03 10:40:38 -0700553 return 0;
554}
555
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700556static int fsck_cache_tree(struct cache_tree *it)
557{
558 int i;
559 int err = 0;
560
Johannes Schindelin20f1eb62007-06-05 03:44:00 +0100561 if (verbose)
562 fprintf(stderr, "Checking cache tree\n");
563
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700564 if (0 <= it->entry_count) {
565 struct object *obj = parse_object(it->sha1);
Junio C Hamano6d60bbe2006-05-03 21:17:45 -0700566 if (!obj) {
567 error("%s: invalid sha1 pointer in cache-tree",
568 sha1_to_hex(it->sha1));
569 return 1;
570 }
Junio C Hamanocdc08b32006-05-01 22:15:54 -0700571 obj->used = 1;
Junio C Hamanoa1cdc252011-01-26 12:46:55 -0800572 mark_object_reachable(obj);
Linus Torvalds19746322006-07-11 20:45:31 -0700573 if (obj->type != OBJ_TREE)
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700574 err |= objerror(obj, "non-tree in cache-tree");
575 }
576 for (i = 0; i < it->subtree_nr; i++)
577 err |= fsck_cache_tree(it->down[i]->cache_tree);
578 return err;
579}
580
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200581static char const * const fsck_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200582 "git fsck [options] [<object>...]",
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200583 NULL
584};
585
586static struct option fsck_opts[] = {
René Scharfefd038812010-11-08 18:56:39 +0100587 OPT__VERBOSE(&verbose, "be verbose"),
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200588 OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
589 OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
590 OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
591 OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
592 OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
Wesley J. Landaker2b26e0e2010-02-05 12:58:50 -0700593 OPT_BOOLEAN(0, "full", &check_full, "also consider packs and alternate objects"),
Emil Medvedd46b9b2007-10-30 14:15:21 -0500594 OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200595 OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
596 "write dangling objects in .git/lost-found"),
597 OPT_END(),
598};
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800599
Peter Hagervallbaffc0e2007-07-15 01:14:45 +0200600int cmd_fsck(int argc, const char **argv, const char *prefix)
Linus Torvalds20222112005-04-08 15:02:42 -0700601{
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700602 int i, heads;
Junio C Hamanoe15ef662009-01-30 00:50:54 -0800603 struct alternate_object_database *alt;
Linus Torvalds20222112005-04-08 15:02:42 -0700604
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800605 errors_found = 0;
Christian Couderdae556b2009-01-23 10:07:46 +0100606 read_replace_refs = 0;
Junio C Hamano61e2b012005-11-25 23:52:04 -0800607
Stephen Boyd37782922009-05-23 11:53:12 -0700608 argc = parse_options(argc, argv, prefix, fsck_opts, fsck_usage, 0);
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200609 if (write_lost_and_found) {
610 check_full = 1;
611 include_reflogs = 0;
Linus Torvalds889262e2005-04-25 16:31:13 -0700612 }
613
Linus Torvaldsc3330382005-07-03 10:40:38 -0700614 fsck_head_link();
Junio C Hamano8a498a02005-06-28 14:58:33 -0700615 fsck_object_dir(get_object_directory());
Junio C Hamanoe15ef662009-01-30 00:50:54 -0800616
617 prepare_alt_odb();
618 for (alt = alt_odb_list; alt; alt = alt->next) {
619 char namebuf[PATH_MAX];
620 int namelen = alt->name - alt->base;
621 memcpy(namebuf, alt->base, namelen);
622 namebuf[namelen - 1] = 0;
623 fsck_object_dir(namebuf);
624 }
625
Junio C Hamano8a498a02005-06-28 14:58:33 -0700626 if (check_full) {
Junio C Hamano8a498a02005-06-28 14:58:33 -0700627 struct packed_git *p;
Junio C Hamanoe15ef662009-01-30 00:50:54 -0800628
Junio C Hamano8a498a02005-06-28 14:58:33 -0700629 prepare_packed_git();
Junio C Hamanof9253392005-06-29 02:51:27 -0700630 for (p = packed_git; p; p = p->next)
631 /* verify gives error messages itself */
Nicolas Pitre77d3ece2008-06-24 23:18:17 -0400632 verify_pack(p);
Junio C Hamanof9253392005-06-29 02:51:27 -0700633
Junio C Hamano8a498a02005-06-28 14:58:33 -0700634 for (p = packed_git; p; p = p->next) {
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200635 uint32_t j, num;
Shawn O. Pearceb77ffe82007-05-30 02:13:14 -0400636 if (open_pack_index(p))
637 continue;
638 num = p->num_objects;
Pierre Habouzit5ac0a202007-10-15 22:34:05 +0200639 for (j = 0; j < num; j++)
640 fsck_sha1(nth_packed_object_sha1(p, j));
Junio C Hamano8a498a02005-06-28 14:58:33 -0700641 }
Linus Torvalds20222112005-04-08 15:02:42 -0700642 }
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700643
644 heads = 0;
Christian Couder3aed2fd2009-01-18 04:46:09 +0100645 for (i = 0; i < argc; i++) {
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700646 const char *arg = argv[i];
Junio C Hamano469e2eb2009-01-30 00:33:00 -0800647 unsigned char sha1[20];
648 if (!get_sha1(arg, sha1)) {
649 struct object *obj = lookup_object(sha1);
Jonas Fonsecae1a13882005-04-29 20:00:40 -0700650
Linus Torvalds770896e2005-05-04 17:03:09 -0700651 /* Error is printed by lookup_object(). */
652 if (!obj)
Jonas Fonsecae1a13882005-04-29 20:00:40 -0700653 continue;
654
Daniel Barkalowff5ebe32005-04-18 11:39:48 -0700655 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100656 mark_object_reachable(obj);
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700657 heads++;
658 continue;
659 }
Petr Baudisf1f0d082005-09-20 20:56:05 +0200660 error("invalid parameter: expected sha1, got '%s'", arg);
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700661 }
662
Linus Torvalds10249322005-05-18 10:16:14 -0700663 /*
Nicolas Pitred1af0022005-05-20 16:59:17 -0400664 * If we've not been given any explicit head information, do the
Linus Torvaldse7bd9072005-05-18 10:19:59 -0700665 * default ones from .git/refs. We also consider the index file
666 * in this case (ie this implies --cache).
Linus Torvalds10249322005-05-18 10:16:14 -0700667 */
Linus Torvaldse7bd9072005-05-18 10:19:59 -0700668 if (!heads) {
Linus Torvalds10249322005-05-18 10:16:14 -0700669 get_default_heads();
670 keep_cache_objects = 1;
671 }
672
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700673 if (keep_cache_objects) {
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700674 read_cache();
675 for (i = 0; i < active_nr; i++) {
Linus Torvalds8d9721c2007-04-09 21:15:29 -0700676 unsigned int mode;
677 struct blob *blob;
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700678 struct object *obj;
Linus Torvalds8d9721c2007-04-09 21:15:29 -0700679
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800680 mode = active_cache[i]->ce_mode;
Martin Waitz302b9282007-05-21 22:08:28 +0200681 if (S_ISGITLINK(mode))
Linus Torvalds8d9721c2007-04-09 21:15:29 -0700682 continue;
683 blob = lookup_blob(active_cache[i]->sha1);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700684 if (!blob)
685 continue;
686 obj = &blob->object;
687 obj->used = 1;
Martin Koegler271b8d22008-02-25 22:46:05 +0100688 mark_object_reachable(obj);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700689 }
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700690 if (active_cache_tree)
691 fsck_cache_tree(active_cache_tree);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700692 }
693
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700694 check_connectivity();
Junio C Hamanoe2b4f632007-03-05 00:22:06 -0800695 return errors_found;
Linus Torvalds20222112005-04-08 15:02:42 -0700696}