blob: ecfb014fffe1c13dedcd6973b4328abeef02413d [file] [log] [blame]
Linus Torvalds4b182422005-04-30 09:59:31 -07001#include "cache.h"
Daniel Barkalowff5ebe32005-04-18 11:39:48 -07002#include "commit.h"
3#include "tree.h"
4#include "blob.h"
Daniel Barkalowc418eda2005-04-28 07:46:33 -07005#include "tag.h"
Linus Torvalds944d8582005-07-03 10:01:38 -07006#include "refs.h"
Junio C Hamanof9253392005-06-29 02:51:27 -07007#include "pack.h"
Junio C Hamano53dc3f32006-04-25 16:37:08 -07008#include "cache-tree.h"
Linus Torvaldse9a95be2006-05-29 12:19:02 -07009#include "tree-walk.h"
Daniel Barkalowff5ebe32005-04-18 11:39:48 -070010
11#define REACHABLE 0x0001
Linus Torvalds2d9c58c2006-05-29 12:18:33 -070012#define SEEN 0x0002
Linus Torvaldsd9839e02005-04-13 09:57:30 -070013
David Rientjes96f1e582006-08-15 10:23:48 -070014static int show_root;
15static int show_tags;
16static int show_unreachable;
17static int check_full;
18static int check_strict;
19static int keep_cache_objects;
Linus Torvaldsd9839e02005-04-13 09:57:30 -070020static unsigned char head_sha1[20];
21
Timo Hirvonen962554c2006-02-26 17:13:46 +020022#ifdef NO_D_INO_IN_DIRENT
Junio C Hamano35a730f2006-01-19 17:13:51 -080023#define SORT_DIRENT 0
24#define DIRENT_SORT_HINT(de) 0
25#else
26#define SORT_DIRENT 1
27#define DIRENT_SORT_HINT(de) ((de)->d_ino)
28#endif
Petr Baudisf1f0d082005-09-20 20:56:05 +020029
30static void objreport(struct object *obj, const char *severity,
31 const char *err, va_list params)
32{
33 fprintf(stderr, "%s in %s %s: ",
Linus Torvalds885a86a2006-06-14 16:45:13 -070034 severity, typename(obj->type), sha1_to_hex(obj->sha1));
Petr Baudisf1f0d082005-09-20 20:56:05 +020035 vfprintf(stderr, err, params);
36 fputs("\n", stderr);
37}
38
Peter Hagervalla7928f82005-09-28 14:04:54 +020039static int objerror(struct object *obj, const char *err, ...)
Petr Baudisf1f0d082005-09-20 20:56:05 +020040{
41 va_list params;
42 va_start(params, err);
43 objreport(obj, "error", err, params);
44 va_end(params);
45 return -1;
46}
47
Peter Hagervalla7928f82005-09-28 14:04:54 +020048static int objwarning(struct object *obj, const char *err, ...)
Petr Baudisf1f0d082005-09-20 20:56:05 +020049{
50 va_list params;
51 va_start(params, err);
52 objreport(obj, "warning", err, params);
53 va_end(params);
54 return -1;
55}
56
Linus Torvalds18af29f2007-01-21 22:26:41 -080057/*
58 * Check a single reachable object
59 */
60static void check_reachable_object(struct object *obj)
61{
62 const struct object_refs *refs;
63
64 /*
65 * We obviously want the object to be parsed,
66 * except if it was in a pack-file and we didn't
67 * do a full fsck
68 */
69 if (!obj->parsed) {
70 if (has_sha1_file(obj->sha1))
71 return; /* it is in pack - forget about it */
72 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
73 return;
74 }
75
76 /*
77 * Check that everything that we try to reference is also good.
78 */
79 refs = lookup_object_refs(obj);
80 if (refs) {
81 unsigned j;
82 for (j = 0; j < refs->count; j++) {
83 struct object *ref = refs->ref[j];
84 if (ref->parsed ||
85 (has_sha1_file(ref->sha1)))
86 continue;
87 printf("broken link from %7s %s\n",
88 typename(obj->type), sha1_to_hex(obj->sha1));
89 printf(" to %7s %s\n",
90 typename(ref->type), sha1_to_hex(ref->sha1));
91 }
92 }
93}
94
95/*
96 * Check a single unreachable object
97 */
98static void check_unreachable_object(struct object *obj)
99{
100 /*
101 * Missing unreachable object? Ignore it. It's not like
102 * we miss it (since it can't be reached), nor do we want
103 * to complain about it being unreachable (since it does
104 * not exist).
105 */
106 if (!obj->parsed)
107 return;
108
109 /*
110 * Unreachable object that exists? Show it if asked to,
111 * since this is something that is prunable.
112 */
113 if (show_unreachable) {
114 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
115 return;
116 }
117
118 /*
119 * "!used" means that nothing at all points to it, including
120 * other unreacahble objects. In other words, it's the "tip"
121 * of some set of unreachable objects, usually a commit that
122 * got dropped.
123 *
124 * Such starting points are more interesting than some random
125 * set of unreachable objects, so we show them even if the user
126 * hasn't asked for _all_ unreachable objects. If you have
127 * deleted a branch by mistake, this is a prime candidate to
128 * start looking at, for example.
129 */
130 if (!obj->used) {
131 printf("dangling %s %s\n", typename(obj->type),
132 sha1_to_hex(obj->sha1));
133 return;
134 }
135
136 /*
137 * Otherwise? It's there, it's unreachable, and some other unreachable
138 * object points to it. Ignore it - it's not interesting, and we showed
139 * all the interesting cases above.
140 */
141}
142
143static void check_object(struct object *obj)
144{
145 if (obj->flags & REACHABLE)
146 check_reachable_object(obj);
147 else
148 check_unreachable_object(obj);
149}
Petr Baudisf1f0d082005-09-20 20:56:05 +0200150
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700151static void check_connectivity(void)
152{
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700153 int i, max;
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700154
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700155 /* Look up all the requirements, warn about missing objects.. */
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700156 max = get_max_object_index();
157 for (i = 0; i < max; i++) {
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700158 struct object *obj = get_indexed_object(i);
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700159
Linus Torvalds18af29f2007-01-21 22:26:41 -0800160 if (obj)
161 check_object(obj);
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700162 }
163}
164
Linus Torvalds85003492005-05-02 16:13:18 -0700165/*
166 * The entries in a tree are ordered in the _path_ order,
167 * which means that a directory entry is ordered by adding
168 * a slash to the end of it.
169 *
170 * So a directory called "a" is ordered _after_ a file
171 * called "a.c", because "a/" sorts after "a.c".
172 */
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700173#define TREE_UNORDERED (-1)
174#define TREE_HAS_DUPS (-2)
175
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700176static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
Linus Torvalds85003492005-05-02 16:13:18 -0700177{
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700178 int len1 = strlen(name1);
179 int len2 = strlen(name2);
Linus Torvalds85003492005-05-02 16:13:18 -0700180 int len = len1 < len2 ? len1 : len2;
181 unsigned char c1, c2;
182 int cmp;
183
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700184 cmp = memcmp(name1, name2, len);
Linus Torvalds85003492005-05-02 16:13:18 -0700185 if (cmp < 0)
186 return 0;
187 if (cmp > 0)
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700188 return TREE_UNORDERED;
Linus Torvalds85003492005-05-02 16:13:18 -0700189
190 /*
191 * Ok, the first <len> characters are the same.
192 * Now we need to order the next one, but turn
193 * a '\0' into a '/' for a directory entry.
194 */
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700195 c1 = name1[len];
196 c2 = name2[len];
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700197 if (!c1 && !c2)
198 /*
199 * git-write-tree used to write out a nonsense tree that has
200 * entries with the same name, one blob and one tree. Make
201 * sure we do not have duplicate entries.
202 */
203 return TREE_HAS_DUPS;
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700204 if (!c1 && S_ISDIR(mode1))
Linus Torvalds85003492005-05-02 16:13:18 -0700205 c1 = '/';
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700206 if (!c2 && S_ISDIR(mode2))
Linus Torvalds85003492005-05-02 16:13:18 -0700207 c2 = '/';
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700208 return c1 < c2 ? 0 : TREE_UNORDERED;
Linus Torvalds85003492005-05-02 16:13:18 -0700209}
210
Daniel Barkalowc418eda2005-04-28 07:46:33 -0700211static int fsck_tree(struct tree *item)
Linus Torvalds1ea34e32005-04-08 17:11:14 -0700212{
Linus Torvalds64071802005-07-27 16:08:43 -0700213 int retval;
Linus Torvalds85003492005-05-02 16:13:18 -0700214 int has_full_path = 0;
Linus Torvalds64071802005-07-27 16:08:43 -0700215 int has_zero_pad = 0;
216 int has_bad_modes = 0;
217 int has_dup_entries = 0;
218 int not_properly_sorted = 0;
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700219 struct tree_desc desc;
220 unsigned o_mode;
221 const char *o_name;
222 const unsigned char *o_sha1;
Linus Torvalds85003492005-05-02 16:13:18 -0700223
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700224 desc.buf = item->buffer;
225 desc.size = item->size;
226
227 o_mode = 0;
228 o_name = NULL;
229 o_sha1 = NULL;
230 while (desc.size) {
231 unsigned mode;
232 const char *name;
233 const unsigned char *sha1;
234
235 sha1 = tree_entry_extract(&desc, &name, &mode);
236
237 if (strchr(name, '/'))
Linus Torvalds85003492005-05-02 16:13:18 -0700238 has_full_path = 1;
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700239 has_zero_pad |= *(char *)desc.buf == '0';
240 update_tree_entry(&desc);
Linus Torvalds85003492005-05-02 16:13:18 -0700241
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700242 switch (mode) {
Linus Torvalds42ea9cb2005-05-05 16:18:48 -0700243 /*
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700244 * Standard modes..
Linus Torvalds42ea9cb2005-05-05 16:18:48 -0700245 */
246 case S_IFREG | 0755:
247 case S_IFREG | 0644:
248 case S_IFLNK:
249 case S_IFDIR:
250 break;
251 /*
252 * This is nonstandard, but we had a few of these
253 * early on when we honored the full set of mode
254 * bits..
255 */
256 case S_IFREG | 0664:
Linus Torvaldsde2eb7f2005-07-27 15:16:03 -0700257 if (!check_strict)
258 break;
Linus Torvalds42ea9cb2005-05-05 16:18:48 -0700259 default:
Linus Torvalds64071802005-07-27 16:08:43 -0700260 has_bad_modes = 1;
Linus Torvalds42ea9cb2005-05-05 16:18:48 -0700261 }
262
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700263 if (o_name) {
264 switch (verify_ordered(o_mode, o_name, mode, name)) {
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700265 case TREE_UNORDERED:
Linus Torvalds64071802005-07-27 16:08:43 -0700266 not_properly_sorted = 1;
267 break;
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700268 case TREE_HAS_DUPS:
Linus Torvalds64071802005-07-27 16:08:43 -0700269 has_dup_entries = 1;
270 break;
Junio C Hamanoa4f35a22005-05-07 14:43:32 -0700271 default:
272 break;
Linus Torvalds85003492005-05-02 16:13:18 -0700273 }
274 }
275
Linus Torvaldse9a95be2006-05-29 12:19:02 -0700276 o_mode = mode;
277 o_name = name;
278 o_sha1 = sha1;
Linus Torvalds85003492005-05-02 16:13:18 -0700279 }
Linus Torvalds136f2e52006-05-29 12:16:12 -0700280 free(item->buffer);
281 item->buffer = NULL;
Linus Torvalds85003492005-05-02 16:13:18 -0700282
Linus Torvalds64071802005-07-27 16:08:43 -0700283 retval = 0;
Linus Torvalds85003492005-05-02 16:13:18 -0700284 if (has_full_path) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200285 objwarning(&item->object, "contains full pathnames");
Linus Torvalds1ea34e32005-04-08 17:11:14 -0700286 }
Linus Torvalds64071802005-07-27 16:08:43 -0700287 if (has_zero_pad) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200288 objwarning(&item->object, "contains zero-padded file modes");
Linus Torvalds64071802005-07-27 16:08:43 -0700289 }
290 if (has_bad_modes) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200291 objwarning(&item->object, "contains bad file modes");
Linus Torvalds64071802005-07-27 16:08:43 -0700292 }
293 if (has_dup_entries) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200294 retval = objerror(&item->object, "contains duplicate file entries");
Linus Torvalds64071802005-07-27 16:08:43 -0700295 }
296 if (not_properly_sorted) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200297 retval = objerror(&item->object, "not properly sorted");
Linus Torvalds64071802005-07-27 16:08:43 -0700298 }
299 return retval;
Linus Torvalds1ea34e32005-04-08 17:11:14 -0700300}
301
Daniel Barkalowc418eda2005-04-28 07:46:33 -0700302static int fsck_commit(struct commit *commit)
Linus Torvalds1ea34e32005-04-08 17:11:14 -0700303{
Linus Torvaldsde2eb7f2005-07-27 15:16:03 -0700304 char *buffer = commit->buffer;
Petr Baudisf1f0d082005-09-20 20:56:05 +0200305 unsigned char tree_sha1[20], sha1[20];
Linus Torvaldsde2eb7f2005-07-27 15:16:03 -0700306
307 if (memcmp(buffer, "tree ", 5))
Petr Baudisf1f0d082005-09-20 20:56:05 +0200308 return objerror(&commit->object, "invalid format - expected 'tree' line");
309 if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
310 return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
Linus Torvaldsde2eb7f2005-07-27 15:16:03 -0700311 buffer += 46;
312 while (!memcmp(buffer, "parent ", 7)) {
313 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
Petr Baudisf1f0d082005-09-20 20:56:05 +0200314 return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
Linus Torvaldsde2eb7f2005-07-27 15:16:03 -0700315 buffer += 48;
316 }
317 if (memcmp(buffer, "author ", 7))
Petr Baudisf1f0d082005-09-20 20:56:05 +0200318 return objerror(&commit->object, "invalid format - expected 'author' line");
Linus Torvaldsbd1e17e2005-05-25 19:26:28 -0700319 free(commit->buffer);
320 commit->buffer = NULL;
Daniel Barkalowff5ebe32005-04-18 11:39:48 -0700321 if (!commit->tree)
Petr Baudisf1f0d082005-09-20 20:56:05 +0200322 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
Linus Torvaldsab7df182005-04-25 16:34:13 -0700323 if (!commit->parents && show_root)
Daniel Barkalowc418eda2005-04-28 07:46:33 -0700324 printf("root %s\n", sha1_to_hex(commit->object.sha1));
Linus Torvaldse6948b62005-04-24 16:20:53 -0700325 if (!commit->date)
Daniel Barkalowc418eda2005-04-28 07:46:33 -0700326 printf("bad commit date in %s\n",
327 sha1_to_hex(commit->object.sha1));
Linus Torvalds59c1e242005-04-09 00:25:22 -0700328 return 0;
Linus Torvalds1ea34e32005-04-08 17:11:14 -0700329}
330
Daniel Barkalowc418eda2005-04-28 07:46:33 -0700331static int fsck_tag(struct tag *tag)
Linus Torvalds4728b862005-04-24 14:10:55 -0700332{
Linus Torvalds92d4c852005-05-03 07:57:56 -0700333 struct object *tagged = tag->tagged;
334
335 if (!tagged) {
Petr Baudisf1f0d082005-09-20 20:56:05 +0200336 return objerror(&tag->object, "could not load tagged object");
Linus Torvalds92d4c852005-05-03 07:57:56 -0700337 }
Linus Torvalds889262e2005-04-25 16:31:13 -0700338 if (!show_tags)
339 return 0;
340
Linus Torvalds885a86a2006-06-14 16:45:13 -0700341 printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1));
Linus Torvalds92d4c852005-05-03 07:57:56 -0700342 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
Daniel Barkalowff5ebe32005-04-18 11:39:48 -0700343 return 0;
Linus Torvalds20222112005-04-08 15:02:42 -0700344}
345
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700346static int fsck_sha1(unsigned char *sha1)
Linus Torvalds20222112005-04-08 15:02:42 -0700347{
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700348 struct object *obj = parse_object(sha1);
349 if (!obj)
Linus Torvalds9130ac12007-01-11 14:09:31 -0800350 return error("%s: object corrupt or missing", sha1_to_hex(sha1));
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700351 if (obj->flags & SEEN)
352 return 0;
353 obj->flags |= SEEN;
Linus Torvalds19746322006-07-11 20:45:31 -0700354 if (obj->type == OBJ_BLOB)
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700355 return 0;
Linus Torvalds19746322006-07-11 20:45:31 -0700356 if (obj->type == OBJ_TREE)
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700357 return fsck_tree((struct tree *) obj);
Linus Torvalds19746322006-07-11 20:45:31 -0700358 if (obj->type == OBJ_COMMIT)
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700359 return fsck_commit((struct commit *) obj);
Linus Torvalds19746322006-07-11 20:45:31 -0700360 if (obj->type == OBJ_TAG)
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700361 return fsck_tag((struct tag *) obj);
Petr Baudisf1f0d082005-09-20 20:56:05 +0200362 /* By now, parse_object() would've returned NULL instead. */
Linus Torvalds885a86a2006-06-14 16:45:13 -0700363 return objerror(obj, "unknown type '%d' (internal fsck error)", obj->type);
Linus Torvalds20222112005-04-08 15:02:42 -0700364}
365
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700366/*
367 * This is the sorting chunk size: make it reasonably
368 * big so that we can sort well..
369 */
370#define MAX_SHA1_ENTRIES (1024)
371
372struct sha1_entry {
373 unsigned long ino;
374 unsigned char sha1[20];
375};
376
377static struct {
378 unsigned long nr;
379 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
380} sha1_list;
381
382static int ino_compare(const void *_a, const void *_b)
383{
384 const struct sha1_entry *a = _a, *b = _b;
385 unsigned long ino1 = a->ino, ino2 = b->ino;
386 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
387}
388
389static void fsck_sha1_list(void)
390{
391 int i, nr = sha1_list.nr;
392
Junio C Hamano35a730f2006-01-19 17:13:51 -0800393 if (SORT_DIRENT)
394 qsort(sha1_list.entry, nr,
395 sizeof(struct sha1_entry *), ino_compare);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700396 for (i = 0; i < nr; i++) {
397 struct sha1_entry *entry = sha1_list.entry[i];
398 unsigned char *sha1 = entry->sha1;
399
400 sha1_list.entry[i] = NULL;
Petr Baudisf1f0d082005-09-20 20:56:05 +0200401 fsck_sha1(sha1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700402 free(entry);
403 }
404 sha1_list.nr = 0;
405}
406
407static void add_sha1_list(unsigned char *sha1, unsigned long ino)
408{
409 struct sha1_entry *entry = xmalloc(sizeof(*entry));
410 int nr;
411
412 entry->ino = ino;
Shawn Pearcee7024962006-08-23 02:49:00 -0400413 hashcpy(entry->sha1, sha1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700414 nr = sha1_list.nr;
415 if (nr == MAX_SHA1_ENTRIES) {
416 fsck_sha1_list();
417 nr = 0;
418 }
419 sha1_list.entry[nr] = entry;
420 sha1_list.nr = ++nr;
421}
422
David Rientjesb5524c82006-08-14 13:36:18 -0700423static void fsck_dir(int i, char *path)
Linus Torvalds20222112005-04-08 15:02:42 -0700424{
425 DIR *dir = opendir(path);
426 struct dirent *de;
427
Linus Torvalds230f1322005-10-08 15:54:01 -0700428 if (!dir)
David Rientjesb5524c82006-08-14 13:36:18 -0700429 return;
Linus Torvalds20222112005-04-08 15:02:42 -0700430
431 while ((de = readdir(dir)) != NULL) {
432 char name[100];
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700433 unsigned char sha1[20];
Linus Torvalds20222112005-04-08 15:02:42 -0700434 int len = strlen(de->d_name);
435
436 switch (len) {
437 case 2:
438 if (de->d_name[1] != '.')
439 break;
440 case 1:
441 if (de->d_name[0] != '.')
442 break;
443 continue;
444 case 38:
445 sprintf(name, "%02x", i);
446 memcpy(name+2, de->d_name, len+1);
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700447 if (get_sha1_hex(name, sha1) < 0)
448 break;
Junio C Hamano35a730f2006-01-19 17:13:51 -0800449 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
Linus Torvalds7e8c1742005-05-02 09:06:33 -0700450 continue;
Linus Torvalds20222112005-04-08 15:02:42 -0700451 }
452 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
453 }
454 closedir(dir);
Linus Torvalds20222112005-04-08 15:02:42 -0700455}
456
David Rientjes96f1e582006-08-15 10:23:48 -0700457static int default_refs;
Linus Torvalds944d8582005-07-03 10:01:38 -0700458
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100459static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
460 const char *email, unsigned long timestamp, int tz,
461 const char *message, void *cb_data)
Junio C Hamano55dd5522006-12-18 01:36:16 -0800462{
463 struct object *obj;
464
465 if (!is_null_sha1(osha1)) {
466 obj = lookup_object(osha1);
467 if (obj) {
468 obj->used = 1;
469 mark_reachable(obj, REACHABLE);
470 }
471 }
472 obj = lookup_object(nsha1);
473 if (obj) {
474 obj->used = 1;
475 mark_reachable(obj, REACHABLE);
476 }
477 return 0;
478}
479
Junio C Hamano8da19772006-09-20 22:02:01 -0700480static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
Linus Torvalds10249322005-05-18 10:16:14 -0700481{
Linus Torvalds10249322005-05-18 10:16:14 -0700482 struct object *obj;
483
Linus Torvalds10249322005-05-18 10:16:14 -0700484 obj = lookup_object(sha1);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700485 if (!obj) {
Junio C Hamano7aaa7152006-03-09 01:44:19 -0800486 if (has_sha1_file(sha1)) {
Linus Torvalds659cacf2005-07-07 17:05:41 -0700487 default_refs++;
Linus Torvalds944d8582005-07-03 10:01:38 -0700488 return 0; /* it is in a pack */
Linus Torvalds659cacf2005-07-07 17:05:41 -0700489 }
Linus Torvalds944d8582005-07-03 10:01:38 -0700490 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
491 /* We'll continue with the rest despite the error.. */
492 return 0;
Junio C Hamano8a498a02005-06-28 14:58:33 -0700493 }
Linus Torvalds944d8582005-07-03 10:01:38 -0700494 default_refs++;
Linus Torvalds10249322005-05-18 10:16:14 -0700495 obj->used = 1;
496 mark_reachable(obj, REACHABLE);
Junio C Hamano55dd5522006-12-18 01:36:16 -0800497
498 for_each_reflog_ent(refname, fsck_handle_reflog_ent, NULL);
499
Linus Torvalds7c4d07c2005-05-20 07:49:17 -0700500 return 0;
Linus Torvalds10249322005-05-18 10:16:14 -0700501}
502
Linus Torvalds10249322005-05-18 10:16:14 -0700503static void get_default_heads(void)
504{
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700505 for_each_ref(fsck_handle_ref, NULL);
Linus Torvalds071fa892006-08-29 11:47:30 -0700506
507 /*
508 * Not having any default heads isn't really fatal, but
509 * it does mean that "--unreachable" no longer makes any
510 * sense (since in this case everything will obviously
511 * be unreachable by definition.
512 *
513 * Showing dangling objects is valid, though (as those
514 * dangling objects are likely lost heads).
515 *
516 * So we just print a warning about it, and clear the
517 * "show_unreachable" flag.
518 */
519 if (!default_refs) {
520 error("No default references");
521 show_unreachable = 0;
522 }
Linus Torvalds10249322005-05-18 10:16:14 -0700523}
524
Junio C Hamano8a498a02005-06-28 14:58:33 -0700525static void fsck_object_dir(const char *path)
526{
527 int i;
528 for (i = 0; i < 256; i++) {
529 static char dir[4096];
530 sprintf(dir, "%s/%02x", path, i);
531 fsck_dir(i, dir);
532 }
533 fsck_sha1_list();
534}
535
Linus Torvaldsc3330382005-07-03 10:40:38 -0700536static int fsck_head_link(void)
537{
Linus Torvaldsc3330382005-07-03 10:40:38 -0700538 unsigned char sha1[20];
Junio C Hamano8da19772006-09-20 22:02:01 -0700539 int flag;
540 const char *head_points_at = resolve_ref("HEAD", sha1, 1, &flag);
Linus Torvaldsc3330382005-07-03 10:40:38 -0700541
Junio C Hamano8da19772006-09-20 22:02:01 -0700542 if (!head_points_at || !(flag & REF_ISSYMREF))
Junio C Hamano8098a172005-09-30 14:26:57 -0700543 return error("HEAD is not a symbolic ref");
Junio C Hamano5b10b092006-09-18 01:08:00 -0700544 if (strncmp(head_points_at, "refs/heads/", 11))
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);
David Rientjes0bef57e2006-08-15 13:37:19 -0700547 if (is_null_sha1(sha1))
Linus Torvaldsc3330382005-07-03 10:40:38 -0700548 return error("HEAD: not a valid git pointer");
549 return 0;
550}
551
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700552static int fsck_cache_tree(struct cache_tree *it)
553{
554 int i;
555 int err = 0;
556
557 if (0 <= it->entry_count) {
558 struct object *obj = parse_object(it->sha1);
Junio C Hamano6d60bbe2006-05-03 21:17:45 -0700559 if (!obj) {
560 error("%s: invalid sha1 pointer in cache-tree",
561 sha1_to_hex(it->sha1));
562 return 1;
563 }
Junio C Hamanocdc08b32006-05-01 22:15:54 -0700564 mark_reachable(obj, REACHABLE);
565 obj->used = 1;
Linus Torvalds19746322006-07-11 20:45:31 -0700566 if (obj->type != OBJ_TREE)
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700567 err |= objerror(obj, "non-tree in cache-tree");
568 }
569 for (i = 0; i < it->subtree_nr; i++)
570 err |= fsck_cache_tree(it->down[i]->cache_tree);
571 return err;
572}
573
Linus Torvalds20222112005-04-08 15:02:42 -0700574int main(int argc, char **argv)
575{
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700576 int i, heads;
Linus Torvalds20222112005-04-08 15:02:42 -0700577
Linus Torvalds3a7c3522006-05-29 12:16:46 -0700578 track_object_refs = 1;
Junio C Hamano61e2b012005-11-25 23:52:04 -0800579 setup_git_directory();
580
Linus Torvalds889262e2005-04-25 16:31:13 -0700581 for (i = 1; i < argc; i++) {
582 const char *arg = argv[i];
583
584 if (!strcmp(arg, "--unreachable")) {
585 show_unreachable = 1;
586 continue;
587 }
588 if (!strcmp(arg, "--tags")) {
589 show_tags = 1;
590 continue;
591 }
Linus Torvaldsab7df182005-04-25 16:34:13 -0700592 if (!strcmp(arg, "--root")) {
593 show_root = 1;
594 continue;
595 }
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700596 if (!strcmp(arg, "--cache")) {
597 keep_cache_objects = 1;
598 continue;
599 }
Junio C Hamano8a498a02005-06-28 14:58:33 -0700600 if (!strcmp(arg, "--full")) {
601 check_full = 1;
602 continue;
603 }
Linus Torvaldsde2eb7f2005-07-27 15:16:03 -0700604 if (!strcmp(arg, "--strict")) {
605 check_strict = 1;
606 continue;
607 }
Linus Torvalds889262e2005-04-25 16:31:13 -0700608 if (*arg == '-')
Junio C Hamano7aaa7152006-03-09 01:44:19 -0800609 usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]");
Linus Torvalds889262e2005-04-25 16:31:13 -0700610 }
611
Linus Torvaldsc3330382005-07-03 10:40:38 -0700612 fsck_head_link();
Junio C Hamano8a498a02005-06-28 14:58:33 -0700613 fsck_object_dir(get_object_directory());
614 if (check_full) {
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700615 struct alternate_object_database *alt;
Junio C Hamano8a498a02005-06-28 14:58:33 -0700616 struct packed_git *p;
617 prepare_alt_odb();
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700618 for (alt = alt_odb_list; alt; alt = alt->next) {
Junio C Hamanoa3eb2502005-07-10 15:40:43 -0700619 char namebuf[PATH_MAX];
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700620 int namelen = alt->name - alt->base;
621 memcpy(namebuf, alt->base, namelen);
Junio C Hamanoa3eb2502005-07-10 15:40:43 -0700622 namebuf[namelen - 1] = 0;
623 fsck_object_dir(namebuf);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700624 }
625 prepare_packed_git();
Junio C Hamanof9253392005-06-29 02:51:27 -0700626 for (p = packed_git; p; p = p->next)
627 /* verify gives error messages itself */
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700628 verify_pack(p, 0);
Junio C Hamanof9253392005-06-29 02:51:27 -0700629
Junio C Hamano8a498a02005-06-28 14:58:33 -0700630 for (p = packed_git; p; p = p->next) {
631 int num = num_packed_objects(p);
632 for (i = 0; i < num; i++) {
633 unsigned char sha1[20];
634 nth_packed_object_sha1(p, i, sha1);
Petr Baudisf1f0d082005-09-20 20:56:05 +0200635 fsck_sha1(sha1);
Junio C Hamano8a498a02005-06-28 14:58:33 -0700636 }
637 }
Linus Torvalds20222112005-04-08 15:02:42 -0700638 }
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700639
640 heads = 0;
641 for (i = 1; i < argc; i++) {
Linus Torvalds889262e2005-04-25 16:31:13 -0700642 const char *arg = argv[i];
643
644 if (*arg == '-')
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700645 continue;
Linus Torvalds889262e2005-04-25 16:31:13 -0700646
Linus Torvalds3c249c92005-05-01 16:36:56 -0700647 if (!get_sha1(arg, head_sha1)) {
Linus Torvalds770896e2005-05-04 17:03:09 -0700648 struct object *obj = lookup_object(head_sha1);
Jonas Fonsecae1a13882005-04-29 20:00:40 -0700649
Linus Torvalds770896e2005-05-04 17:03:09 -0700650 /* Error is printed by lookup_object(). */
651 if (!obj)
Jonas Fonsecae1a13882005-04-29 20:00:40 -0700652 continue;
653
Daniel Barkalowff5ebe32005-04-18 11:39:48 -0700654 obj->used = 1;
655 mark_reachable(obj, REACHABLE);
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700656 heads++;
657 continue;
658 }
Petr Baudisf1f0d082005-09-20 20:56:05 +0200659 error("invalid parameter: expected sha1, got '%s'", arg);
Linus Torvaldsbcee6fd2005-04-13 16:42:09 -0700660 }
661
Linus Torvalds10249322005-05-18 10:16:14 -0700662 /*
Nicolas Pitred1af0022005-05-20 16:59:17 -0400663 * If we've not been given any explicit head information, do the
Linus Torvaldse7bd9072005-05-18 10:19:59 -0700664 * default ones from .git/refs. We also consider the index file
665 * in this case (ie this implies --cache).
Linus Torvalds10249322005-05-18 10:16:14 -0700666 */
Linus Torvaldse7bd9072005-05-18 10:19:59 -0700667 if (!heads) {
Linus Torvalds10249322005-05-18 10:16:14 -0700668 get_default_heads();
669 keep_cache_objects = 1;
670 }
671
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700672 if (keep_cache_objects) {
673 int i;
674 read_cache();
675 for (i = 0; i < active_nr; i++) {
676 struct blob *blob = lookup_blob(active_cache[i]->sha1);
677 struct object *obj;
678 if (!blob)
679 continue;
680 obj = &blob->object;
681 obj->used = 1;
682 mark_reachable(obj, REACHABLE);
683 }
Junio C Hamano53dc3f32006-04-25 16:37:08 -0700684 if (active_cache_tree)
685 fsck_cache_tree(active_cache_tree);
Junio C Hamanoae7c0c92005-05-04 01:33:33 -0700686 }
687
Linus Torvalds8ba0bbb2005-04-10 23:13:09 -0700688 check_connectivity();
Linus Torvalds20222112005-04-08 15:02:42 -0700689 return 0;
690}