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
;
23 #define ERROR_OBJECT 01
24 #define ERROR_REACHABLE 02
26 #ifdef NO_D_INO_IN_DIRENT
28 #define DIRENT_SORT_HINT(de) 0
31 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
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
);
43 static int objerror(struct object
*obj
, const char *err
, ...)
46 va_start(params
, err
);
47 errors_found
|= ERROR_OBJECT
;
48 objreport(obj
, "error", err
, params
);
53 static int objwarning(struct object
*obj
, const char *err
, ...)
56 va_start(params
, err
);
57 objreport(obj
, "warning", err
, params
);
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
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
;
83 * Check that everything that we try to reference is also good.
85 refs
= lookup_object_refs(obj
);
88 for (j
= 0; j
< refs
->count
; j
++) {
89 struct object
*ref
= refs
->ref
[j
];
91 (has_sha1_file(ref
->sha1
)))
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
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
));
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
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.
138 printf("dangling %s %s\n", typename(obj
->type
),
139 sha1_to_hex(obj
->sha1
));
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
);
155 check_unreachable_object(obj
);
158 static void check_connectivity(void)
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
);
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
;
191 cmp
= memcmp(name1
, name2
, len
);
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.
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
))
213 if (!c2
&& S_ISDIR(mode2
))
215 return c1
< c2
? 0 : TREE_UNORDERED
;
218 static int fsck_tree(struct tree
*item
)
221 int has_full_path
= 0;
222 int has_empty_name
= 0;
223 int has_zero_pad
= 0;
224 int has_bad_modes
= 0;
225 int has_dup_entries
= 0;
226 int not_properly_sorted
= 0;
227 struct tree_desc desc
;
230 const unsigned char *o_sha1
;
232 init_tree_desc(&desc
, item
->buffer
, item
->size
);
240 const unsigned char *sha1
;
242 sha1
= tree_entry_extract(&desc
, &name
, &mode
);
244 if (strchr(name
, '/'))
248 has_zero_pad
|= *(char *)desc
.buffer
== '0';
249 update_tree_entry(&desc
);
262 * This is nonstandard, but we had a few of these
263 * early on when we honored the full set of mode
274 switch (verify_ordered(o_mode
, o_name
, mode
, name
)) {
276 not_properly_sorted
= 1;
295 objwarning(&item
->object
, "contains full pathnames");
297 if (has_empty_name
) {
298 objwarning(&item
->object
, "contains empty pathname");
301 objwarning(&item
->object
, "contains zero-padded file modes");
304 objwarning(&item
->object
, "contains bad file modes");
306 if (has_dup_entries
) {
307 retval
= objerror(&item
->object
, "contains duplicate file entries");
309 if (not_properly_sorted
) {
310 retval
= objerror(&item
->object
, "not properly sorted");
315 static int fsck_commit(struct commit
*commit
)
317 char *buffer
= commit
->buffer
;
318 unsigned char tree_sha1
[20], sha1
[20];
320 if (memcmp(buffer
, "tree ", 5))
321 return objerror(&commit
->object
, "invalid format - expected 'tree' line");
322 if (get_sha1_hex(buffer
+5, tree_sha1
) || buffer
[45] != '\n')
323 return objerror(&commit
->object
, "invalid 'tree' line format - bad sha1");
325 while (!memcmp(buffer
, "parent ", 7)) {
326 if (get_sha1_hex(buffer
+7, sha1
) || buffer
[47] != '\n')
327 return objerror(&commit
->object
, "invalid 'parent' line format - bad sha1");
330 if (memcmp(buffer
, "author ", 7))
331 return objerror(&commit
->object
, "invalid format - expected 'author' line");
332 free(commit
->buffer
);
333 commit
->buffer
= NULL
;
335 return objerror(&commit
->object
, "could not load commit's tree %s", tree_sha1
);
336 if (!commit
->parents
&& show_root
)
337 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
339 printf("bad commit date in %s\n",
340 sha1_to_hex(commit
->object
.sha1
));
344 static int fsck_tag(struct tag
*tag
)
346 struct object
*tagged
= tag
->tagged
;
349 return objerror(&tag
->object
, "could not load tagged object");
354 printf("tagged %s %s", typename(tagged
->type
), sha1_to_hex(tagged
->sha1
));
355 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
359 static int fsck_sha1(const unsigned char *sha1
)
361 struct object
*obj
= parse_object(sha1
);
363 errors_found
|= ERROR_OBJECT
;
364 return error("%s: object corrupt or missing",
367 if (obj
->flags
& SEEN
)
370 if (obj
->type
== OBJ_BLOB
)
372 if (obj
->type
== OBJ_TREE
)
373 return fsck_tree((struct tree
*) obj
);
374 if (obj
->type
== OBJ_COMMIT
)
375 return fsck_commit((struct commit
*) obj
);
376 if (obj
->type
== OBJ_TAG
)
377 return fsck_tag((struct tag
*) obj
);
379 /* By now, parse_object() would've returned NULL instead. */
380 return objerror(obj
, "unknown type '%d' (internal fsck error)",
385 * This is the sorting chunk size: make it reasonably
386 * big so that we can sort well..
388 #define MAX_SHA1_ENTRIES (1024)
392 unsigned char sha1
[20];
397 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
400 static int ino_compare(const void *_a
, const void *_b
)
402 const struct sha1_entry
*a
= _a
, *b
= _b
;
403 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
404 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
407 static void fsck_sha1_list(void)
409 int i
, nr
= sha1_list
.nr
;
412 qsort(sha1_list
.entry
, nr
,
413 sizeof(struct sha1_entry
*), ino_compare
);
414 for (i
= 0; i
< nr
; i
++) {
415 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
416 unsigned char *sha1
= entry
->sha1
;
418 sha1_list
.entry
[i
] = NULL
;
425 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
427 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
431 hashcpy(entry
->sha1
, sha1
);
433 if (nr
== MAX_SHA1_ENTRIES
) {
437 sha1_list
.entry
[nr
] = entry
;
441 static void fsck_dir(int i
, char *path
)
443 DIR *dir
= opendir(path
);
449 while ((de
= readdir(dir
)) != NULL
) {
451 unsigned char sha1
[20];
452 int len
= strlen(de
->d_name
);
456 if (de
->d_name
[1] != '.')
459 if (de
->d_name
[0] != '.')
463 sprintf(name
, "%02x", i
);
464 memcpy(name
+2, de
->d_name
, len
+1);
465 if (get_sha1_hex(name
, sha1
) < 0)
467 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
470 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
475 static int default_refs
;
477 static int fsck_handle_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
478 const char *email
, unsigned long timestamp
, int tz
,
479 const char *message
, void *cb_data
)
483 if (!is_null_sha1(osha1
)) {
484 obj
= lookup_object(osha1
);
487 mark_reachable(obj
, REACHABLE
);
490 obj
= lookup_object(nsha1
);
493 mark_reachable(obj
, REACHABLE
);
498 static int fsck_handle_reflog(const char *logname
, const unsigned char *sha1
, int flag
, void *cb_data
)
500 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, NULL
);
504 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
508 obj
= lookup_object(sha1
);
510 if (has_sha1_file(sha1
)) {
512 return 0; /* it is in a pack */
514 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
515 /* We'll continue with the rest despite the error.. */
520 mark_reachable(obj
, REACHABLE
);
525 static void get_default_heads(void)
527 for_each_ref(fsck_handle_ref
, NULL
);
529 for_each_reflog(fsck_handle_reflog
, NULL
);
532 * Not having any default heads isn't really fatal, but
533 * it does mean that "--unreachable" no longer makes any
534 * sense (since in this case everything will obviously
535 * be unreachable by definition.
537 * Showing dangling objects is valid, though (as those
538 * dangling objects are likely lost heads).
540 * So we just print a warning about it, and clear the
541 * "show_unreachable" flag.
544 fprintf(stderr
, "notice: No default references\n");
545 show_unreachable
= 0;
549 static void fsck_object_dir(const char *path
)
552 for (i
= 0; i
< 256; i
++) {
553 static char dir
[4096];
554 sprintf(dir
, "%s/%02x", path
, i
);
560 static int fsck_head_link(void)
562 unsigned char sha1
[20];
564 int null_is_error
= 0;
565 const char *head_points_at
= resolve_ref("HEAD", sha1
, 0, &flag
);
568 return error("Invalid HEAD");
569 if (!strcmp(head_points_at
, "HEAD"))
572 else if (prefixcmp(head_points_at
, "refs/heads/"))
573 return error("HEAD points to something strange (%s)",
575 if (is_null_sha1(sha1
)) {
577 return error("HEAD: detached HEAD points at nothing");
578 fprintf(stderr
, "notice: HEAD points to an unborn branch (%s)\n",
579 head_points_at
+ 11);
584 static int fsck_cache_tree(struct cache_tree
*it
)
589 if (0 <= it
->entry_count
) {
590 struct object
*obj
= parse_object(it
->sha1
);
592 error("%s: invalid sha1 pointer in cache-tree",
593 sha1_to_hex(it
->sha1
));
596 mark_reachable(obj
, REACHABLE
);
598 if (obj
->type
!= OBJ_TREE
)
599 err
|= objerror(obj
, "non-tree in cache-tree");
601 for (i
= 0; i
< it
->subtree_nr
; i
++)
602 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
606 static const char fsck_usage
[] =
607 "git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
608 "[--strict] <head-sha1>*]";
610 int cmd_fsck(int argc
, char **argv
, const char *prefix
)
614 track_object_refs
= 1;
617 for (i
= 1; i
< argc
; i
++) {
618 const char *arg
= argv
[i
];
620 if (!strcmp(arg
, "--unreachable")) {
621 show_unreachable
= 1;
624 if (!strcmp(arg
, "--tags")) {
628 if (!strcmp(arg
, "--root")) {
632 if (!strcmp(arg
, "--cache")) {
633 keep_cache_objects
= 1;
636 if (!strcmp(arg
, "--no-reflogs")) {
640 if (!strcmp(arg
, "--full")) {
644 if (!strcmp(arg
, "--strict")) {
653 fsck_object_dir(get_object_directory());
655 struct alternate_object_database
*alt
;
656 struct packed_git
*p
;
658 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
659 char namebuf
[PATH_MAX
];
660 int namelen
= alt
->name
- alt
->base
;
661 memcpy(namebuf
, alt
->base
, namelen
);
662 namebuf
[namelen
- 1] = 0;
663 fsck_object_dir(namebuf
);
665 prepare_packed_git();
666 for (p
= packed_git
; p
; p
= p
->next
)
667 /* verify gives error messages itself */
670 for (p
= packed_git
; p
; p
= p
->next
) {
671 uint32_t i
, num
= p
->num_objects
;
672 for (i
= 0; i
< num
; i
++)
673 fsck_sha1(nth_packed_object_sha1(p
, i
));
678 for (i
= 1; i
< argc
; i
++) {
679 const char *arg
= argv
[i
];
684 if (!get_sha1(arg
, head_sha1
)) {
685 struct object
*obj
= lookup_object(head_sha1
);
687 /* Error is printed by lookup_object(). */
692 mark_reachable(obj
, REACHABLE
);
696 error("invalid parameter: expected sha1, got '%s'", arg
);
700 * If we've not been given any explicit head information, do the
701 * default ones from .git/refs. We also consider the index file
702 * in this case (ie this implies --cache).
706 keep_cache_objects
= 1;
709 if (keep_cache_objects
) {
712 for (i
= 0; i
< active_nr
; i
++) {
717 mode
= ntohl(active_cache
[i
]->ce_mode
);
718 if (S_ISDIRLNK(mode
))
720 blob
= lookup_blob(active_cache
[i
]->sha1
);
725 mark_reachable(obj
, REACHABLE
);
727 if (active_cache_tree
)
728 fsck_cache_tree(active_cache_tree
);
731 check_connectivity();