9 #include "cache-tree.h"
10 #include "tree-walk.h"
12 #define REACHABLE 0x0001
17 static int show_unreachable
;
18 static int include_reflogs
= 1;
19 static int check_full
;
20 static int check_strict
;
21 static int keep_cache_objects
;
22 static unsigned char head_sha1
[20];
23 static int errors_found
;
24 static int write_lost_and_found
;
26 #define ERROR_OBJECT 01
27 #define ERROR_REACHABLE 02
29 #ifdef NO_D_INO_IN_DIRENT
31 #define DIRENT_SORT_HINT(de) 0
34 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
37 static void objreport(struct object
*obj
, const char *severity
,
38 const char *err
, va_list params
)
40 fprintf(stderr
, "%s in %s %s: ",
41 severity
, typename(obj
->type
), sha1_to_hex(obj
->sha1
));
42 vfprintf(stderr
, err
, params
);
46 static int objerror(struct object
*obj
, const char *err
, ...)
49 va_start(params
, err
);
50 errors_found
|= ERROR_OBJECT
;
51 objreport(obj
, "error", err
, params
);
56 static int objwarning(struct object
*obj
, const char *err
, ...)
59 va_start(params
, err
);
60 objreport(obj
, "warning", err
, params
);
66 * Check a single reachable object
68 static void check_reachable_object(struct object
*obj
)
70 const struct object_refs
*refs
;
73 * We obviously want the object to be parsed,
74 * except if it was in a pack-file and we didn't
78 if (has_sha1_pack(obj
->sha1
, NULL
))
79 return; /* it is in pack - forget about it */
80 printf("missing %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
81 errors_found
|= ERROR_REACHABLE
;
86 * Check that everything that we try to reference is also good.
88 refs
= lookup_object_refs(obj
);
91 for (j
= 0; j
< refs
->count
; j
++) {
92 struct object
*ref
= refs
->ref
[j
];
94 (has_sha1_file(ref
->sha1
)))
96 printf("broken link from %7s %s\n",
97 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
98 printf(" to %7s %s\n",
99 typename(ref
->type
), sha1_to_hex(ref
->sha1
));
100 errors_found
|= ERROR_REACHABLE
;
106 * Check a single unreachable object
108 static void check_unreachable_object(struct object
*obj
)
111 * Missing unreachable object? Ignore it. It's not like
112 * we miss it (since it can't be reached), nor do we want
113 * to complain about it being unreachable (since it does
120 * Unreachable object that exists? Show it if asked to,
121 * since this is something that is prunable.
123 if (show_unreachable
) {
124 printf("unreachable %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
129 * "!used" means that nothing at all points to it, including
130 * other unreachable objects. In other words, it's the "tip"
131 * of some set of unreachable objects, usually a commit that
134 * Such starting points are more interesting than some random
135 * set of unreachable objects, so we show them even if the user
136 * hasn't asked for _all_ unreachable objects. If you have
137 * deleted a branch by mistake, this is a prime candidate to
138 * start looking at, for example.
141 printf("dangling %s %s\n", typename(obj
->type
),
142 sha1_to_hex(obj
->sha1
));
143 if (write_lost_and_found
) {
144 char *filename
= git_path("lost-found/%s/%s",
145 obj
->type
== OBJ_COMMIT
? "commit" : "other",
146 sha1_to_hex(obj
->sha1
));
149 if (safe_create_leading_directories(filename
)) {
150 error("Could not create lost-found");
153 if (!(f
= fopen(filename
, "w")))
154 die("Could not open %s", filename
);
155 if (obj
->type
== OBJ_BLOB
) {
156 enum object_type type
;
158 char *buf
= read_sha1_file(obj
->sha1
,
161 fwrite(buf
, size
, 1, f
);
165 fprintf(f
, "%s\n", sha1_to_hex(obj
->sha1
));
172 * Otherwise? It's there, it's unreachable, and some other unreachable
173 * object points to it. Ignore it - it's not interesting, and we showed
174 * all the interesting cases above.
178 static void check_object(struct object
*obj
)
181 fprintf(stderr
, "Checking %s\n", sha1_to_hex(obj
->sha1
));
183 if (obj
->flags
& REACHABLE
)
184 check_reachable_object(obj
);
186 check_unreachable_object(obj
);
189 static void check_connectivity(void)
193 /* Look up all the requirements, warn about missing objects.. */
194 max
= get_max_object_index();
196 fprintf(stderr
, "Checking connectivity (%d objects)\n", max
);
198 for (i
= 0; i
< max
; i
++) {
199 struct object
*obj
= get_indexed_object(i
);
207 * The entries in a tree are ordered in the _path_ order,
208 * which means that a directory entry is ordered by adding
209 * a slash to the end of it.
211 * So a directory called "a" is ordered _after_ a file
212 * called "a.c", because "a/" sorts after "a.c".
214 #define TREE_UNORDERED (-1)
215 #define TREE_HAS_DUPS (-2)
217 static int verify_ordered(unsigned mode1
, const char *name1
, unsigned mode2
, const char *name2
)
219 int len1
= strlen(name1
);
220 int len2
= strlen(name2
);
221 int len
= len1
< len2
? len1
: len2
;
222 unsigned char c1
, c2
;
225 cmp
= memcmp(name1
, name2
, len
);
229 return TREE_UNORDERED
;
232 * Ok, the first <len> characters are the same.
233 * Now we need to order the next one, but turn
234 * a '\0' into a '/' for a directory entry.
240 * git-write-tree used to write out a nonsense tree that has
241 * entries with the same name, one blob and one tree. Make
242 * sure we do not have duplicate entries.
244 return TREE_HAS_DUPS
;
245 if (!c1
&& S_ISDIR(mode1
))
247 if (!c2
&& S_ISDIR(mode2
))
249 return c1
< c2
? 0 : TREE_UNORDERED
;
252 static int fsck_tree(struct tree
*item
)
255 int has_full_path
= 0;
256 int has_empty_name
= 0;
257 int has_zero_pad
= 0;
258 int has_bad_modes
= 0;
259 int has_dup_entries
= 0;
260 int not_properly_sorted
= 0;
261 struct tree_desc desc
;
264 const unsigned char *o_sha1
;
267 fprintf(stderr
, "Checking tree %s\n",
268 sha1_to_hex(item
->object
.sha1
));
270 init_tree_desc(&desc
, item
->buffer
, item
->size
);
278 const unsigned char *sha1
;
280 sha1
= tree_entry_extract(&desc
, &name
, &mode
);
282 if (strchr(name
, '/'))
286 has_zero_pad
|= *(char *)desc
.buffer
== '0';
287 update_tree_entry(&desc
);
300 * This is nonstandard, but we had a few of these
301 * early on when we honored the full set of mode
312 switch (verify_ordered(o_mode
, o_name
, mode
, name
)) {
314 not_properly_sorted
= 1;
333 objwarning(&item
->object
, "contains full pathnames");
335 if (has_empty_name
) {
336 objwarning(&item
->object
, "contains empty pathname");
339 objwarning(&item
->object
, "contains zero-padded file modes");
342 objwarning(&item
->object
, "contains bad file modes");
344 if (has_dup_entries
) {
345 retval
= objerror(&item
->object
, "contains duplicate file entries");
347 if (not_properly_sorted
) {
348 retval
= objerror(&item
->object
, "not properly sorted");
353 static int fsck_commit(struct commit
*commit
)
355 char *buffer
= commit
->buffer
;
356 unsigned char tree_sha1
[20], sha1
[20];
359 fprintf(stderr
, "Checking commit %s\n",
360 sha1_to_hex(commit
->object
.sha1
));
362 if (memcmp(buffer
, "tree ", 5))
363 return objerror(&commit
->object
, "invalid format - expected 'tree' line");
364 if (get_sha1_hex(buffer
+5, tree_sha1
) || buffer
[45] != '\n')
365 return objerror(&commit
->object
, "invalid 'tree' line format - bad sha1");
367 while (!memcmp(buffer
, "parent ", 7)) {
368 if (get_sha1_hex(buffer
+7, sha1
) || buffer
[47] != '\n')
369 return objerror(&commit
->object
, "invalid 'parent' line format - bad sha1");
372 if (memcmp(buffer
, "author ", 7))
373 return objerror(&commit
->object
, "invalid format - expected 'author' line");
374 free(commit
->buffer
);
375 commit
->buffer
= NULL
;
377 return objerror(&commit
->object
, "could not load commit's tree %s", tree_sha1
);
378 if (!commit
->parents
&& show_root
)
379 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
381 printf("bad commit date in %s\n",
382 sha1_to_hex(commit
->object
.sha1
));
386 static int fsck_tag(struct tag
*tag
)
388 struct object
*tagged
= tag
->tagged
;
391 fprintf(stderr
, "Checking tag %s\n",
392 sha1_to_hex(tag
->object
.sha1
));
395 return objerror(&tag
->object
, "could not load tagged object");
400 printf("tagged %s %s", typename(tagged
->type
), sha1_to_hex(tagged
->sha1
));
401 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
405 static int fsck_sha1(const unsigned char *sha1
)
407 struct object
*obj
= parse_object(sha1
);
409 errors_found
|= ERROR_OBJECT
;
410 return error("%s: object corrupt or missing",
413 if (obj
->flags
& SEEN
)
416 if (obj
->type
== OBJ_BLOB
)
418 if (obj
->type
== OBJ_TREE
)
419 return fsck_tree((struct tree
*) obj
);
420 if (obj
->type
== OBJ_COMMIT
)
421 return fsck_commit((struct commit
*) obj
);
422 if (obj
->type
== OBJ_TAG
)
423 return fsck_tag((struct tag
*) obj
);
425 /* By now, parse_object() would've returned NULL instead. */
426 return objerror(obj
, "unknown type '%d' (internal fsck error)",
431 * This is the sorting chunk size: make it reasonably
432 * big so that we can sort well..
434 #define MAX_SHA1_ENTRIES (1024)
438 unsigned char sha1
[20];
443 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
446 static int ino_compare(const void *_a
, const void *_b
)
448 const struct sha1_entry
*a
= _a
, *b
= _b
;
449 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
450 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
453 static void fsck_sha1_list(void)
455 int i
, nr
= sha1_list
.nr
;
458 qsort(sha1_list
.entry
, nr
,
459 sizeof(struct sha1_entry
*), ino_compare
);
460 for (i
= 0; i
< nr
; i
++) {
461 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
462 unsigned char *sha1
= entry
->sha1
;
464 sha1_list
.entry
[i
] = NULL
;
471 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
473 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
477 hashcpy(entry
->sha1
, sha1
);
479 if (nr
== MAX_SHA1_ENTRIES
) {
483 sha1_list
.entry
[nr
] = entry
;
487 static void fsck_dir(int i
, char *path
)
489 DIR *dir
= opendir(path
);
496 fprintf(stderr
, "Checking directory %s\n", path
);
498 while ((de
= readdir(dir
)) != NULL
) {
500 unsigned char sha1
[20];
501 int len
= strlen(de
->d_name
);
505 if (de
->d_name
[1] != '.')
508 if (de
->d_name
[0] != '.')
512 sprintf(name
, "%02x", i
);
513 memcpy(name
+2, de
->d_name
, len
+1);
514 if (get_sha1_hex(name
, sha1
) < 0)
516 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
519 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
524 static int default_refs
;
526 static int fsck_handle_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
527 const char *email
, unsigned long timestamp
, int tz
,
528 const char *message
, void *cb_data
)
533 fprintf(stderr
, "Checking reflog %s->%s\n",
534 sha1_to_hex(osha1
), sha1_to_hex(nsha1
));
536 if (!is_null_sha1(osha1
)) {
537 obj
= lookup_object(osha1
);
540 mark_reachable(obj
, REACHABLE
);
543 obj
= lookup_object(nsha1
);
546 mark_reachable(obj
, REACHABLE
);
551 static int fsck_handle_reflog(const char *logname
, const unsigned char *sha1
, int flag
, void *cb_data
)
553 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, NULL
);
557 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
561 obj
= lookup_object(sha1
);
563 if (has_sha1_file(sha1
)) {
565 return 0; /* it is in a pack */
567 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
568 /* We'll continue with the rest despite the error.. */
573 mark_reachable(obj
, REACHABLE
);
578 static void get_default_heads(void)
580 for_each_ref(fsck_handle_ref
, NULL
);
582 for_each_reflog(fsck_handle_reflog
, NULL
);
585 * Not having any default heads isn't really fatal, but
586 * it does mean that "--unreachable" no longer makes any
587 * sense (since in this case everything will obviously
588 * be unreachable by definition.
590 * Showing dangling objects is valid, though (as those
591 * dangling objects are likely lost heads).
593 * So we just print a warning about it, and clear the
594 * "show_unreachable" flag.
597 fprintf(stderr
, "notice: No default references\n");
598 show_unreachable
= 0;
602 static void fsck_object_dir(const char *path
)
607 fprintf(stderr
, "Checking object directory\n");
609 for (i
= 0; i
< 256; i
++) {
610 static char dir
[4096];
611 sprintf(dir
, "%s/%02x", path
, i
);
617 static int fsck_head_link(void)
619 unsigned char sha1
[20];
621 int null_is_error
= 0;
622 const char *head_points_at
= resolve_ref("HEAD", sha1
, 0, &flag
);
625 fprintf(stderr
, "Checking HEAD link\n");
628 return error("Invalid HEAD");
629 if (!strcmp(head_points_at
, "HEAD"))
632 else if (prefixcmp(head_points_at
, "refs/heads/"))
633 return error("HEAD points to something strange (%s)",
635 if (is_null_sha1(sha1
)) {
637 return error("HEAD: detached HEAD points at nothing");
638 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
639 head_points_at
+ 11);
644 static int fsck_cache_tree(struct cache_tree
*it
)
650 fprintf(stderr
, "Checking cache tree\n");
652 if (0 <= it
->entry_count
) {
653 struct object
*obj
= parse_object(it
->sha1
);
655 error("%s: invalid sha1 pointer in cache-tree",
656 sha1_to_hex(it
->sha1
));
659 mark_reachable(obj
, REACHABLE
);
661 if (obj
->type
!= OBJ_TREE
)
662 err
|= objerror(obj
, "non-tree in cache-tree");
664 for (i
= 0; i
< it
->subtree_nr
; i
++)
665 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
669 static const char fsck_usage
[] =
670 "git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
671 "[--strict] [--verbose] <head-sha1>*]";
673 int cmd_fsck(int argc
, const char **argv
, const char *prefix
)
677 track_object_refs
= 1;
680 for (i
= 1; i
< argc
; i
++) {
681 const char *arg
= argv
[i
];
683 if (!strcmp(arg
, "--unreachable")) {
684 show_unreachable
= 1;
687 if (!strcmp(arg
, "--tags")) {
691 if (!strcmp(arg
, "--root")) {
695 if (!strcmp(arg
, "--cache")) {
696 keep_cache_objects
= 1;
699 if (!strcmp(arg
, "--no-reflogs")) {
703 if (!strcmp(arg
, "--full")) {
707 if (!strcmp(arg
, "--strict")) {
711 if (!strcmp(arg
, "--verbose")) {
715 if (!strcmp(arg
, "--lost-found")) {
718 write_lost_and_found
= 1;
726 fsck_object_dir(get_object_directory());
728 struct alternate_object_database
*alt
;
729 struct packed_git
*p
;
731 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
732 char namebuf
[PATH_MAX
];
733 int namelen
= alt
->name
- alt
->base
;
734 memcpy(namebuf
, alt
->base
, namelen
);
735 namebuf
[namelen
- 1] = 0;
736 fsck_object_dir(namebuf
);
738 prepare_packed_git();
739 for (p
= packed_git
; p
; p
= p
->next
)
740 /* verify gives error messages itself */
743 for (p
= packed_git
; p
; p
= p
->next
) {
745 if (open_pack_index(p
))
747 num
= p
->num_objects
;
748 for (i
= 0; i
< num
; i
++)
749 fsck_sha1(nth_packed_object_sha1(p
, i
));
754 for (i
= 1; i
< argc
; i
++) {
755 const char *arg
= argv
[i
];
760 if (!get_sha1(arg
, head_sha1
)) {
761 struct object
*obj
= lookup_object(head_sha1
);
763 /* Error is printed by lookup_object(). */
768 mark_reachable(obj
, REACHABLE
);
772 error("invalid parameter: expected sha1, got '%s'", arg
);
776 * If we've not been given any explicit head information, do the
777 * default ones from .git/refs. We also consider the index file
778 * in this case (ie this implies --cache).
782 keep_cache_objects
= 1;
785 if (keep_cache_objects
) {
788 for (i
= 0; i
< active_nr
; i
++) {
793 mode
= ntohl(active_cache
[i
]->ce_mode
);
794 if (S_ISGITLINK(mode
))
796 blob
= lookup_blob(active_cache
[i
]->sha1
);
801 mark_reachable(obj
, REACHABLE
);
803 if (active_cache_tree
)
804 fsck_cache_tree(active_cache_tree
);
807 check_connectivity();