Custom low-level merge driver support.
[git/jnareb-git/bp-gitweb.git] / builtin-fsck.c
blob05d98d2cfc0c8ba5e38482a181fee8115e9bd640
1 #include "cache.h"
2 #include "commit.h"
3 #include "tree.h"
4 #include "blob.h"
5 #include "tag.h"
6 #include "refs.h"
7 #include "pack.h"
8 #include "cache-tree.h"
9 #include "tree-walk.h"
11 #define REACHABLE 0x0001
12 #define SEEN 0x0002
14 static int show_root;
15 static int show_tags;
16 static int show_unreachable;
17 static int include_reflogs = 1;
18 static int check_full;
19 static int check_strict;
20 static int keep_cache_objects;
21 static unsigned char head_sha1[20];
22 static int errors_found;
23 #define ERROR_OBJECT 01
24 #define ERROR_REACHABLE 02
26 #ifdef NO_D_INO_IN_DIRENT
27 #define SORT_DIRENT 0
28 #define DIRENT_SORT_HINT(de) 0
29 #else
30 #define SORT_DIRENT 1
31 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
32 #endif
34 static void objreport(struct object *obj, const char *severity,
35 const char *err, va_list params)
37 fprintf(stderr, "%s in %s %s: ",
38 severity, typename(obj->type), sha1_to_hex(obj->sha1));
39 vfprintf(stderr, err, params);
40 fputs("\n", stderr);
43 static int objerror(struct object *obj, const char *err, ...)
45 va_list params;
46 va_start(params, err);
47 errors_found |= ERROR_OBJECT;
48 objreport(obj, "error", err, params);
49 va_end(params);
50 return -1;
53 static int objwarning(struct object *obj, const char *err, ...)
55 va_list params;
56 va_start(params, err);
57 objreport(obj, "warning", err, params);
58 va_end(params);
59 return -1;
63 * Check a single reachable object
65 static void check_reachable_object(struct object *obj)
67 const struct object_refs *refs;
70 * We obviously want the object to be parsed,
71 * except if it was in a pack-file and we didn't
72 * do a full fsck
74 if (!obj->parsed) {
75 if (has_sha1_pack(obj->sha1, NULL))
76 return; /* it is in pack - forget about it */
77 printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
78 errors_found |= ERROR_REACHABLE;
79 return;
83 * Check that everything that we try to reference is also good.
85 refs = lookup_object_refs(obj);
86 if (refs) {
87 unsigned j;
88 for (j = 0; j < refs->count; j++) {
89 struct object *ref = refs->ref[j];
90 if (ref->parsed ||
91 (has_sha1_file(ref->sha1)))
92 continue;
93 printf("broken link from %7s %s\n",
94 typename(obj->type), sha1_to_hex(obj->sha1));
95 printf(" to %7s %s\n",
96 typename(ref->type), sha1_to_hex(ref->sha1));
97 errors_found |= ERROR_REACHABLE;
103 * Check a single unreachable object
105 static void check_unreachable_object(struct object *obj)
108 * Missing unreachable object? Ignore it. It's not like
109 * we miss it (since it can't be reached), nor do we want
110 * to complain about it being unreachable (since it does
111 * not exist).
113 if (!obj->parsed)
114 return;
117 * Unreachable object that exists? Show it if asked to,
118 * since this is something that is prunable.
120 if (show_unreachable) {
121 printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
122 return;
126 * "!used" means that nothing at all points to it, including
127 * other unreachable objects. In other words, it's the "tip"
128 * of some set of unreachable objects, usually a commit that
129 * got dropped.
131 * Such starting points are more interesting than some random
132 * set of unreachable objects, so we show them even if the user
133 * hasn't asked for _all_ unreachable objects. If you have
134 * deleted a branch by mistake, this is a prime candidate to
135 * start looking at, for example.
137 if (!obj->used) {
138 printf("dangling %s %s\n", typename(obj->type),
139 sha1_to_hex(obj->sha1));
140 return;
144 * Otherwise? It's there, it's unreachable, and some other unreachable
145 * object points to it. Ignore it - it's not interesting, and we showed
146 * all the interesting cases above.
150 static void check_object(struct object *obj)
152 if (obj->flags & REACHABLE)
153 check_reachable_object(obj);
154 else
155 check_unreachable_object(obj);
158 static void check_connectivity(void)
160 int i, max;
162 /* Look up all the requirements, warn about missing objects.. */
163 max = get_max_object_index();
164 for (i = 0; i < max; i++) {
165 struct object *obj = get_indexed_object(i);
167 if (obj)
168 check_object(obj);
173 * The entries in a tree are ordered in the _path_ order,
174 * which means that a directory entry is ordered by adding
175 * a slash to the end of it.
177 * So a directory called "a" is ordered _after_ a file
178 * called "a.c", because "a/" sorts after "a.c".
180 #define TREE_UNORDERED (-1)
181 #define TREE_HAS_DUPS (-2)
183 static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
185 int len1 = strlen(name1);
186 int len2 = strlen(name2);
187 int len = len1 < len2 ? len1 : len2;
188 unsigned char c1, c2;
189 int cmp;
191 cmp = memcmp(name1, name2, len);
192 if (cmp < 0)
193 return 0;
194 if (cmp > 0)
195 return TREE_UNORDERED;
198 * Ok, the first <len> characters are the same.
199 * Now we need to order the next one, but turn
200 * a '\0' into a '/' for a directory entry.
202 c1 = name1[len];
203 c2 = name2[len];
204 if (!c1 && !c2)
206 * git-write-tree used to write out a nonsense tree that has
207 * entries with the same name, one blob and one tree. Make
208 * sure we do not have duplicate entries.
210 return TREE_HAS_DUPS;
211 if (!c1 && S_ISDIR(mode1))
212 c1 = '/';
213 if (!c2 && S_ISDIR(mode2))
214 c2 = '/';
215 return c1 < c2 ? 0 : TREE_UNORDERED;
218 static int fsck_tree(struct tree *item)
220 int retval;
221 int has_full_path = 0;
222 int has_zero_pad = 0;
223 int has_bad_modes = 0;
224 int has_dup_entries = 0;
225 int not_properly_sorted = 0;
226 struct tree_desc desc;
227 unsigned o_mode;
228 const char *o_name;
229 const unsigned char *o_sha1;
231 init_tree_desc(&desc, item->buffer, item->size);
233 o_mode = 0;
234 o_name = NULL;
235 o_sha1 = NULL;
236 while (desc.size) {
237 unsigned mode;
238 const char *name;
239 const unsigned char *sha1;
241 sha1 = tree_entry_extract(&desc, &name, &mode);
243 if (strchr(name, '/'))
244 has_full_path = 1;
245 has_zero_pad |= *(char *)desc.buffer == '0';
246 update_tree_entry(&desc);
248 switch (mode) {
250 * Standard modes..
252 case S_IFREG | 0755:
253 case S_IFREG | 0644:
254 case S_IFLNK:
255 case S_IFDIR:
256 break;
258 * This is nonstandard, but we had a few of these
259 * early on when we honored the full set of mode
260 * bits..
262 case S_IFREG | 0664:
263 if (!check_strict)
264 break;
265 default:
266 has_bad_modes = 1;
269 if (o_name) {
270 switch (verify_ordered(o_mode, o_name, mode, name)) {
271 case TREE_UNORDERED:
272 not_properly_sorted = 1;
273 break;
274 case TREE_HAS_DUPS:
275 has_dup_entries = 1;
276 break;
277 default:
278 break;
282 o_mode = mode;
283 o_name = name;
284 o_sha1 = sha1;
286 free(item->buffer);
287 item->buffer = NULL;
289 retval = 0;
290 if (has_full_path) {
291 objwarning(&item->object, "contains full pathnames");
293 if (has_zero_pad) {
294 objwarning(&item->object, "contains zero-padded file modes");
296 if (has_bad_modes) {
297 objwarning(&item->object, "contains bad file modes");
299 if (has_dup_entries) {
300 retval = objerror(&item->object, "contains duplicate file entries");
302 if (not_properly_sorted) {
303 retval = objerror(&item->object, "not properly sorted");
305 return retval;
308 static int fsck_commit(struct commit *commit)
310 char *buffer = commit->buffer;
311 unsigned char tree_sha1[20], sha1[20];
313 if (memcmp(buffer, "tree ", 5))
314 return objerror(&commit->object, "invalid format - expected 'tree' line");
315 if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
316 return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
317 buffer += 46;
318 while (!memcmp(buffer, "parent ", 7)) {
319 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
320 return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
321 buffer += 48;
323 if (memcmp(buffer, "author ", 7))
324 return objerror(&commit->object, "invalid format - expected 'author' line");
325 free(commit->buffer);
326 commit->buffer = NULL;
327 if (!commit->tree)
328 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
329 if (!commit->parents && show_root)
330 printf("root %s\n", sha1_to_hex(commit->object.sha1));
331 if (!commit->date)
332 printf("bad commit date in %s\n",
333 sha1_to_hex(commit->object.sha1));
334 return 0;
337 static int fsck_tag(struct tag *tag)
339 struct object *tagged = tag->tagged;
341 if (!tagged) {
342 return objerror(&tag->object, "could not load tagged object");
344 if (!show_tags)
345 return 0;
347 printf("tagged %s %s", typename(tagged->type), sha1_to_hex(tagged->sha1));
348 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
349 return 0;
352 static int fsck_sha1(const unsigned char *sha1)
354 struct object *obj = parse_object(sha1);
355 if (!obj) {
356 errors_found |= ERROR_OBJECT;
357 return error("%s: object corrupt or missing",
358 sha1_to_hex(sha1));
360 if (obj->flags & SEEN)
361 return 0;
362 obj->flags |= SEEN;
363 if (obj->type == OBJ_BLOB)
364 return 0;
365 if (obj->type == OBJ_TREE)
366 return fsck_tree((struct tree *) obj);
367 if (obj->type == OBJ_COMMIT)
368 return fsck_commit((struct commit *) obj);
369 if (obj->type == OBJ_TAG)
370 return fsck_tag((struct tag *) obj);
372 /* By now, parse_object() would've returned NULL instead. */
373 return objerror(obj, "unknown type '%d' (internal fsck error)",
374 obj->type);
378 * This is the sorting chunk size: make it reasonably
379 * big so that we can sort well..
381 #define MAX_SHA1_ENTRIES (1024)
383 struct sha1_entry {
384 unsigned long ino;
385 unsigned char sha1[20];
388 static struct {
389 unsigned long nr;
390 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
391 } sha1_list;
393 static int ino_compare(const void *_a, const void *_b)
395 const struct sha1_entry *a = _a, *b = _b;
396 unsigned long ino1 = a->ino, ino2 = b->ino;
397 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
400 static void fsck_sha1_list(void)
402 int i, nr = sha1_list.nr;
404 if (SORT_DIRENT)
405 qsort(sha1_list.entry, nr,
406 sizeof(struct sha1_entry *), ino_compare);
407 for (i = 0; i < nr; i++) {
408 struct sha1_entry *entry = sha1_list.entry[i];
409 unsigned char *sha1 = entry->sha1;
411 sha1_list.entry[i] = NULL;
412 fsck_sha1(sha1);
413 free(entry);
415 sha1_list.nr = 0;
418 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
420 struct sha1_entry *entry = xmalloc(sizeof(*entry));
421 int nr;
423 entry->ino = ino;
424 hashcpy(entry->sha1, sha1);
425 nr = sha1_list.nr;
426 if (nr == MAX_SHA1_ENTRIES) {
427 fsck_sha1_list();
428 nr = 0;
430 sha1_list.entry[nr] = entry;
431 sha1_list.nr = ++nr;
434 static void fsck_dir(int i, char *path)
436 DIR *dir = opendir(path);
437 struct dirent *de;
439 if (!dir)
440 return;
442 while ((de = readdir(dir)) != NULL) {
443 char name[100];
444 unsigned char sha1[20];
445 int len = strlen(de->d_name);
447 switch (len) {
448 case 2:
449 if (de->d_name[1] != '.')
450 break;
451 case 1:
452 if (de->d_name[0] != '.')
453 break;
454 continue;
455 case 38:
456 sprintf(name, "%02x", i);
457 memcpy(name+2, de->d_name, len+1);
458 if (get_sha1_hex(name, sha1) < 0)
459 break;
460 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
461 continue;
463 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
465 closedir(dir);
468 static int default_refs;
470 static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
471 const char *email, unsigned long timestamp, int tz,
472 const char *message, void *cb_data)
474 struct object *obj;
476 if (!is_null_sha1(osha1)) {
477 obj = lookup_object(osha1);
478 if (obj) {
479 obj->used = 1;
480 mark_reachable(obj, REACHABLE);
483 obj = lookup_object(nsha1);
484 if (obj) {
485 obj->used = 1;
486 mark_reachable(obj, REACHABLE);
488 return 0;
491 static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
493 for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
494 return 0;
497 static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
499 struct object *obj;
501 obj = lookup_object(sha1);
502 if (!obj) {
503 if (has_sha1_file(sha1)) {
504 default_refs++;
505 return 0; /* it is in a pack */
507 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
508 /* We'll continue with the rest despite the error.. */
509 return 0;
511 default_refs++;
512 obj->used = 1;
513 mark_reachable(obj, REACHABLE);
515 return 0;
518 static void get_default_heads(void)
520 for_each_ref(fsck_handle_ref, NULL);
521 if (include_reflogs)
522 for_each_reflog(fsck_handle_reflog, NULL);
525 * Not having any default heads isn't really fatal, but
526 * it does mean that "--unreachable" no longer makes any
527 * sense (since in this case everything will obviously
528 * be unreachable by definition.
530 * Showing dangling objects is valid, though (as those
531 * dangling objects are likely lost heads).
533 * So we just print a warning about it, and clear the
534 * "show_unreachable" flag.
536 if (!default_refs) {
537 fprintf(stderr, "notice: No default references\n");
538 show_unreachable = 0;
542 static void fsck_object_dir(const char *path)
544 int i;
545 for (i = 0; i < 256; i++) {
546 static char dir[4096];
547 sprintf(dir, "%s/%02x", path, i);
548 fsck_dir(i, dir);
550 fsck_sha1_list();
553 static int fsck_head_link(void)
555 unsigned char sha1[20];
556 int flag;
557 int null_is_error = 0;
558 const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
560 if (!head_points_at)
561 return error("Invalid HEAD");
562 if (!strcmp(head_points_at, "HEAD"))
563 /* detached HEAD */
564 null_is_error = 1;
565 else if (prefixcmp(head_points_at, "refs/heads/"))
566 return error("HEAD points to something strange (%s)",
567 head_points_at);
568 if (is_null_sha1(sha1)) {
569 if (null_is_error)
570 return error("HEAD: detached HEAD points at nothing");
571 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
572 head_points_at + 11);
574 return 0;
577 static int fsck_cache_tree(struct cache_tree *it)
579 int i;
580 int err = 0;
582 if (0 <= it->entry_count) {
583 struct object *obj = parse_object(it->sha1);
584 if (!obj) {
585 error("%s: invalid sha1 pointer in cache-tree",
586 sha1_to_hex(it->sha1));
587 return 1;
589 mark_reachable(obj, REACHABLE);
590 obj->used = 1;
591 if (obj->type != OBJ_TREE)
592 err |= objerror(obj, "non-tree in cache-tree");
594 for (i = 0; i < it->subtree_nr; i++)
595 err |= fsck_cache_tree(it->down[i]->cache_tree);
596 return err;
599 static const char fsck_usage[] =
600 "git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
601 "[--strict] <head-sha1>*]";
603 int cmd_fsck(int argc, char **argv, const char *prefix)
605 int i, heads;
607 track_object_refs = 1;
608 errors_found = 0;
610 for (i = 1; i < argc; i++) {
611 const char *arg = argv[i];
613 if (!strcmp(arg, "--unreachable")) {
614 show_unreachable = 1;
615 continue;
617 if (!strcmp(arg, "--tags")) {
618 show_tags = 1;
619 continue;
621 if (!strcmp(arg, "--root")) {
622 show_root = 1;
623 continue;
625 if (!strcmp(arg, "--cache")) {
626 keep_cache_objects = 1;
627 continue;
629 if (!strcmp(arg, "--no-reflogs")) {
630 include_reflogs = 0;
631 continue;
633 if (!strcmp(arg, "--full")) {
634 check_full = 1;
635 continue;
637 if (!strcmp(arg, "--strict")) {
638 check_strict = 1;
639 continue;
641 if (*arg == '-')
642 usage(fsck_usage);
645 fsck_head_link();
646 fsck_object_dir(get_object_directory());
647 if (check_full) {
648 struct alternate_object_database *alt;
649 struct packed_git *p;
650 prepare_alt_odb();
651 for (alt = alt_odb_list; alt; alt = alt->next) {
652 char namebuf[PATH_MAX];
653 int namelen = alt->name - alt->base;
654 memcpy(namebuf, alt->base, namelen);
655 namebuf[namelen - 1] = 0;
656 fsck_object_dir(namebuf);
658 prepare_packed_git();
659 for (p = packed_git; p; p = p->next)
660 /* verify gives error messages itself */
661 verify_pack(p, 0);
663 for (p = packed_git; p; p = p->next) {
664 uint32_t i, num = num_packed_objects(p);
665 for (i = 0; i < num; i++)
666 fsck_sha1(nth_packed_object_sha1(p, i));
670 heads = 0;
671 for (i = 1; i < argc; i++) {
672 const char *arg = argv[i];
674 if (*arg == '-')
675 continue;
677 if (!get_sha1(arg, head_sha1)) {
678 struct object *obj = lookup_object(head_sha1);
680 /* Error is printed by lookup_object(). */
681 if (!obj)
682 continue;
684 obj->used = 1;
685 mark_reachable(obj, REACHABLE);
686 heads++;
687 continue;
689 error("invalid parameter: expected sha1, got '%s'", arg);
693 * If we've not been given any explicit head information, do the
694 * default ones from .git/refs. We also consider the index file
695 * in this case (ie this implies --cache).
697 if (!heads) {
698 get_default_heads();
699 keep_cache_objects = 1;
702 if (keep_cache_objects) {
703 int i;
704 read_cache();
705 for (i = 0; i < active_nr; i++) {
706 struct blob *blob = lookup_blob(active_cache[i]->sha1);
707 struct object *obj;
708 if (!blob)
709 continue;
710 obj = &blob->object;
711 obj->used = 1;
712 mark_reachable(obj, REACHABLE);
714 if (active_cache_tree)
715 fsck_cache_tree(active_cache_tree);
718 check_connectivity();
719 return errors_found;