8 #include "cache-tree.h"
11 #define REACHABLE 0x0001
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
;
24 #define ERROR_OBJECT 01
25 #define ERROR_REACHABLE 02
27 #ifdef NO_D_INO_IN_DIRENT
29 #define DIRENT_SORT_HINT(de) 0
32 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
35 static void objreport(struct object
*obj
, const char *severity
,
36 const char *err
, va_list params
)
38 fprintf(stderr
, "%s in %s %s: ",
39 severity
, typename(obj
->type
), sha1_to_hex(obj
->sha1
));
40 vfprintf(stderr
, err
, params
);
44 static int objerror(struct object
*obj
, const char *err
, ...)
47 va_start(params
, err
);
48 errors_found
|= ERROR_OBJECT
;
49 objreport(obj
, "error", err
, params
);
54 static int objwarning(struct object
*obj
, const char *err
, ...)
57 va_start(params
, err
);
58 objreport(obj
, "warning", err
, params
);
64 * Check a single reachable object
66 static void check_reachable_object(struct object
*obj
)
68 const struct object_refs
*refs
;
71 * We obviously want the object to be parsed,
72 * except if it was in a pack-file and we didn't
76 if (has_sha1_pack(obj
->sha1
, NULL
))
77 return; /* it is in pack - forget about it */
78 printf("missing %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
79 errors_found
|= ERROR_REACHABLE
;
84 * Check that everything that we try to reference is also good.
86 refs
= lookup_object_refs(obj
);
89 for (j
= 0; j
< refs
->count
; j
++) {
90 struct object
*ref
= refs
->ref
[j
];
92 (has_sha1_file(ref
->sha1
)))
94 printf("broken link from %7s %s\n",
95 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
96 printf(" to %7s %s\n",
97 typename(ref
->type
), sha1_to_hex(ref
->sha1
));
98 errors_found
|= ERROR_REACHABLE
;
104 * Check a single unreachable object
106 static void check_unreachable_object(struct object
*obj
)
109 * Missing unreachable object? Ignore it. It's not like
110 * we miss it (since it can't be reached), nor do we want
111 * to complain about it being unreachable (since it does
118 * Unreachable object that exists? Show it if asked to,
119 * since this is something that is prunable.
121 if (show_unreachable
) {
122 printf("unreachable %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
127 * "!used" means that nothing at all points to it, including
128 * other unreachable objects. In other words, it's the "tip"
129 * of some set of unreachable objects, usually a commit that
132 * Such starting points are more interesting than some random
133 * set of unreachable objects, so we show them even if the user
134 * hasn't asked for _all_ unreachable objects. If you have
135 * deleted a branch by mistake, this is a prime candidate to
136 * start looking at, for example.
139 printf("dangling %s %s\n", typename(obj
->type
),
140 sha1_to_hex(obj
->sha1
));
145 * Otherwise? It's there, it's unreachable, and some other unreachable
146 * object points to it. Ignore it - it's not interesting, and we showed
147 * all the interesting cases above.
151 static void check_object(struct object
*obj
)
154 fprintf(stderr
, "Checking %s\n", sha1_to_hex(obj
->sha1
));
156 if (obj
->flags
& REACHABLE
)
157 check_reachable_object(obj
);
159 check_unreachable_object(obj
);
162 static void check_connectivity(void)
166 /* Look up all the requirements, warn about missing objects.. */
167 max
= get_max_object_index();
169 fprintf(stderr
, "Checking connectivity (%d objects)\n", max
);
171 for (i
= 0; i
< max
; i
++) {
172 struct object
*obj
= get_indexed_object(i
);
180 * The entries in a tree are ordered in the _path_ order,
181 * which means that a directory entry is ordered by adding
182 * a slash to the end of it.
184 * So a directory called "a" is ordered _after_ a file
185 * called "a.c", because "a/" sorts after "a.c".
187 #define TREE_UNORDERED (-1)
188 #define TREE_HAS_DUPS (-2)
190 static int verify_ordered(unsigned mode1
, const char *name1
, unsigned mode2
, const char *name2
)
192 int len1
= strlen(name1
);
193 int len2
= strlen(name2
);
194 int len
= len1
< len2
? len1
: len2
;
195 unsigned char c1
, c2
;
198 cmp
= memcmp(name1
, name2
, len
);
202 return TREE_UNORDERED
;
205 * Ok, the first <len> characters are the same.
206 * Now we need to order the next one, but turn
207 * a '\0' into a '/' for a directory entry.
213 * git-write-tree used to write out a nonsense tree that has
214 * entries with the same name, one blob and one tree. Make
215 * sure we do not have duplicate entries.
217 return TREE_HAS_DUPS
;
218 if (!c1
&& S_ISDIR(mode1
))
220 if (!c2
&& S_ISDIR(mode2
))
222 return c1
< c2
? 0 : TREE_UNORDERED
;
225 static int fsck_tree(struct tree
*item
)
228 int has_full_path
= 0;
229 int has_empty_name
= 0;
230 int has_zero_pad
= 0;
231 int has_bad_modes
= 0;
232 int has_dup_entries
= 0;
233 int not_properly_sorted
= 0;
234 struct tree_desc desc
;
237 const unsigned char *o_sha1
;
240 fprintf(stderr
, "Checking tree %s\n",
241 sha1_to_hex(item
->object
.sha1
));
243 init_tree_desc(&desc
, item
->buffer
, item
->size
);
251 const unsigned char *sha1
;
253 sha1
= tree_entry_extract(&desc
, &name
, &mode
);
255 if (strchr(name
, '/'))
259 has_zero_pad
|= *(char *)desc
.buffer
== '0';
260 update_tree_entry(&desc
);
273 * This is nonstandard, but we had a few of these
274 * early on when we honored the full set of mode
285 switch (verify_ordered(o_mode
, o_name
, mode
, name
)) {
287 not_properly_sorted
= 1;
306 objwarning(&item
->object
, "contains full pathnames");
308 if (has_empty_name
) {
309 objwarning(&item
->object
, "contains empty pathname");
312 objwarning(&item
->object
, "contains zero-padded file modes");
315 objwarning(&item
->object
, "contains bad file modes");
317 if (has_dup_entries
) {
318 retval
= objerror(&item
->object
, "contains duplicate file entries");
320 if (not_properly_sorted
) {
321 retval
= objerror(&item
->object
, "not properly sorted");
326 static int fsck_commit(struct commit
*commit
)
328 char *buffer
= commit
->buffer
;
329 unsigned char tree_sha1
[20], sha1
[20];
332 fprintf(stderr
, "Checking commit %s\n",
333 sha1_to_hex(commit
->object
.sha1
));
335 if (memcmp(buffer
, "tree ", 5))
336 return objerror(&commit
->object
, "invalid format - expected 'tree' line");
337 if (get_sha1_hex(buffer
+5, tree_sha1
) || buffer
[45] != '\n')
338 return objerror(&commit
->object
, "invalid 'tree' line format - bad sha1");
340 while (!memcmp(buffer
, "parent ", 7)) {
341 if (get_sha1_hex(buffer
+7, sha1
) || buffer
[47] != '\n')
342 return objerror(&commit
->object
, "invalid 'parent' line format - bad sha1");
345 if (memcmp(buffer
, "author ", 7))
346 return objerror(&commit
->object
, "invalid format - expected 'author' line");
347 free(commit
->buffer
);
348 commit
->buffer
= NULL
;
350 return objerror(&commit
->object
, "could not load commit's tree %s", tree_sha1
);
351 if (!commit
->parents
&& show_root
)
352 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
354 printf("bad commit date in %s\n",
355 sha1_to_hex(commit
->object
.sha1
));
359 static int fsck_tag(struct tag
*tag
)
361 struct object
*tagged
= tag
->tagged
;
364 fprintf(stderr
, "Checking tag %s\n",
365 sha1_to_hex(tag
->object
.sha1
));
368 return objerror(&tag
->object
, "could not load tagged object");
373 printf("tagged %s %s", typename(tagged
->type
), sha1_to_hex(tagged
->sha1
));
374 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
378 static int fsck_sha1(const unsigned char *sha1
)
380 struct object
*obj
= parse_object(sha1
);
382 errors_found
|= ERROR_OBJECT
;
383 return error("%s: object corrupt or missing",
386 if (obj
->flags
& SEEN
)
389 if (obj
->type
== OBJ_BLOB
)
391 if (obj
->type
== OBJ_TREE
)
392 return fsck_tree((struct tree
*) obj
);
393 if (obj
->type
== OBJ_COMMIT
)
394 return fsck_commit((struct commit
*) obj
);
395 if (obj
->type
== OBJ_TAG
)
396 return fsck_tag((struct tag
*) obj
);
398 /* By now, parse_object() would've returned NULL instead. */
399 return objerror(obj
, "unknown type '%d' (internal fsck error)",
404 * This is the sorting chunk size: make it reasonably
405 * big so that we can sort well..
407 #define MAX_SHA1_ENTRIES (1024)
411 unsigned char sha1
[20];
416 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
419 static int ino_compare(const void *_a
, const void *_b
)
421 const struct sha1_entry
*a
= _a
, *b
= _b
;
422 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
423 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
426 static void fsck_sha1_list(void)
428 int i
, nr
= sha1_list
.nr
;
431 qsort(sha1_list
.entry
, nr
,
432 sizeof(struct sha1_entry
*), ino_compare
);
433 for (i
= 0; i
< nr
; i
++) {
434 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
435 unsigned char *sha1
= entry
->sha1
;
437 sha1_list
.entry
[i
] = NULL
;
444 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
446 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
450 hashcpy(entry
->sha1
, sha1
);
452 if (nr
== MAX_SHA1_ENTRIES
) {
456 sha1_list
.entry
[nr
] = entry
;
460 static void fsck_dir(int i
, char *path
)
462 DIR *dir
= opendir(path
);
469 fprintf(stderr
, "Checking directory %s\n", path
);
471 while ((de
= readdir(dir
)) != NULL
) {
473 unsigned char sha1
[20];
474 int len
= strlen(de
->d_name
);
478 if (de
->d_name
[1] != '.')
481 if (de
->d_name
[0] != '.')
485 sprintf(name
, "%02x", i
);
486 memcpy(name
+2, de
->d_name
, len
+1);
487 if (get_sha1_hex(name
, sha1
) < 0)
489 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
492 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
497 static int default_refs
;
499 static int fsck_handle_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
500 const char *email
, unsigned long timestamp
, int tz
,
501 const char *message
, void *cb_data
)
506 fprintf(stderr
, "Checking reflog %s->%s\n",
507 sha1_to_hex(osha1
), sha1_to_hex(nsha1
));
509 if (!is_null_sha1(osha1
)) {
510 obj
= lookup_object(osha1
);
513 mark_reachable(obj
, REACHABLE
);
516 obj
= lookup_object(nsha1
);
519 mark_reachable(obj
, REACHABLE
);
524 static int fsck_handle_reflog(const char *logname
, const unsigned char *sha1
, int flag
, void *cb_data
)
526 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, NULL
);
530 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
534 obj
= lookup_object(sha1
);
536 if (has_sha1_file(sha1
)) {
538 return 0; /* it is in a pack */
540 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
541 /* We'll continue with the rest despite the error.. */
546 mark_reachable(obj
, REACHABLE
);
551 static void get_default_heads(void)
553 for_each_ref(fsck_handle_ref
, NULL
);
555 for_each_reflog(fsck_handle_reflog
, NULL
);
558 * Not having any default heads isn't really fatal, but
559 * it does mean that "--unreachable" no longer makes any
560 * sense (since in this case everything will obviously
561 * be unreachable by definition.
563 * Showing dangling objects is valid, though (as those
564 * dangling objects are likely lost heads).
566 * So we just print a warning about it, and clear the
567 * "show_unreachable" flag.
570 fprintf(stderr
, "notice: No default references\n");
571 show_unreachable
= 0;
575 static void fsck_object_dir(const char *path
)
580 fprintf(stderr
, "Checking object directory\n");
582 for (i
= 0; i
< 256; i
++) {
583 static char dir
[4096];
584 sprintf(dir
, "%s/%02x", path
, i
);
590 static int fsck_head_link(void)
592 unsigned char sha1
[20];
594 int null_is_error
= 0;
595 const char *head_points_at
= resolve_ref("HEAD", sha1
, 0, &flag
);
598 fprintf(stderr
, "Checking HEAD link\n");
601 return error("Invalid HEAD");
602 if (!strcmp(head_points_at
, "HEAD"))
605 else if (prefixcmp(head_points_at
, "refs/heads/"))
606 return error("HEAD points to something strange (%s)",
608 if (is_null_sha1(sha1
)) {
610 return error("HEAD: detached HEAD points at nothing");
611 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
612 head_points_at
+ 11);
617 static int fsck_cache_tree(struct cache_tree
*it
)
623 fprintf(stderr
, "Checking cache tree\n");
625 if (0 <= it
->entry_count
) {
626 struct object
*obj
= parse_object(it
->sha1
);
628 error("%s: invalid sha1 pointer in cache-tree",
629 sha1_to_hex(it
->sha1
));
632 mark_reachable(obj
, REACHABLE
);
634 if (obj
->type
!= OBJ_TREE
)
635 err
|= objerror(obj
, "non-tree in cache-tree");
637 for (i
= 0; i
< it
->subtree_nr
; i
++)
638 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
642 static const char fsck_usage
[] =
643 "git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
644 "[--strict] [--verbose] <head-sha1>*]";
646 int cmd_fsck(int argc
, char **argv
, const char *prefix
)
650 track_object_refs
= 1;
653 for (i
= 1; i
< argc
; i
++) {
654 const char *arg
= argv
[i
];
656 if (!strcmp(arg
, "--unreachable")) {
657 show_unreachable
= 1;
660 if (!strcmp(arg
, "--tags")) {
664 if (!strcmp(arg
, "--root")) {
668 if (!strcmp(arg
, "--cache")) {
669 keep_cache_objects
= 1;
672 if (!strcmp(arg
, "--no-reflogs")) {
676 if (!strcmp(arg
, "--full")) {
680 if (!strcmp(arg
, "--strict")) {
684 if (!strcmp(arg
, "--verbose")) {
693 fsck_object_dir(get_object_directory());
695 struct alternate_object_database
*alt
;
696 struct packed_git
*p
;
698 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
699 char namebuf
[PATH_MAX
];
700 int namelen
= alt
->name
- alt
->base
;
701 memcpy(namebuf
, alt
->base
, namelen
);
702 namebuf
[namelen
- 1] = 0;
703 fsck_object_dir(namebuf
);
705 prepare_packed_git();
706 for (p
= packed_git
; p
; p
= p
->next
)
707 /* verify gives error messages itself */
710 for (p
= packed_git
; p
; p
= p
->next
) {
712 if (open_pack_index(p
))
714 num
= p
->num_objects
;
715 for (i
= 0; i
< num
; i
++)
716 fsck_sha1(nth_packed_object_sha1(p
, i
));
721 for (i
= 1; i
< argc
; i
++) {
722 const char *arg
= argv
[i
];
727 if (!get_sha1(arg
, head_sha1
)) {
728 struct object
*obj
= lookup_object(head_sha1
);
730 /* Error is printed by lookup_object(). */
735 mark_reachable(obj
, REACHABLE
);
739 error("invalid parameter: expected sha1, got '%s'", arg
);
743 * If we've not been given any explicit head information, do the
744 * default ones from .git/refs. We also consider the index file
745 * in this case (ie this implies --cache).
749 keep_cache_objects
= 1;
752 if (keep_cache_objects
) {
755 for (i
= 0; i
< active_nr
; i
++) {
760 mode
= ntohl(active_cache
[i
]->ce_mode
);
761 if (S_ISGITLINK(mode
))
763 blob
= lookup_blob(active_cache
[i
]->sha1
);
768 mark_reachable(obj
, REACHABLE
);
770 if (active_cache_tree
)
771 fsck_cache_tree(active_cache_tree
);
774 check_connectivity();