8 #include "cache-tree.h"
11 #define REACHABLE 0x0001
16 static int show_unreachable
;
17 static int check_full
;
18 static int check_strict
;
19 static int keep_cache_objects
;
20 static unsigned char head_sha1
[20];
22 #ifdef NO_D_INO_IN_DIRENT
24 #define DIRENT_SORT_HINT(de) 0
27 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
30 static void objreport(struct object
*obj
, const char *severity
,
31 const char *err
, va_list params
)
33 fprintf(stderr
, "%s in %s %s: ",
34 severity
, typename(obj
->type
), sha1_to_hex(obj
->sha1
));
35 vfprintf(stderr
, err
, params
);
39 static int objerror(struct object
*obj
, const char *err
, ...)
42 va_start(params
, err
);
43 objreport(obj
, "error", err
, params
);
48 static int objwarning(struct object
*obj
, const char *err
, ...)
51 va_start(params
, err
);
52 objreport(obj
, "warning", err
, params
);
58 * Check a single reachable object
60 static void check_reachable_object(struct object
*obj
)
62 const struct object_refs
*refs
;
65 * We obviously want the object to be parsed,
66 * except if it was in a pack-file and we didn't
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
));
77 * Check that everything that we try to reference is also good.
79 refs
= lookup_object_refs(obj
);
82 for (j
= 0; j
< refs
->count
; j
++) {
83 struct object
*ref
= refs
->ref
[j
];
85 (has_sha1_file(ref
->sha1
)))
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
));
96 * Check a single unreachable object
98 static void check_unreachable_object(struct object
*obj
)
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
110 * Unreachable object that exists? Show it if asked to,
111 * since this is something that is prunable.
113 if (show_unreachable
) {
114 printf("unreachable %s %s\n", typename(obj
->type
), sha1_to_hex(obj
->sha1
));
119 * "!used" means that nothing at all points to it, including
120 * other unreachable objects. In other words, it's the "tip"
121 * of some set of unreachable objects, usually a commit that
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.
131 printf("dangling %s %s\n", typename(obj
->type
),
132 sha1_to_hex(obj
->sha1
));
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.
143 static void check_object(struct object
*obj
)
145 if (obj
->flags
& REACHABLE
)
146 check_reachable_object(obj
);
148 check_unreachable_object(obj
);
151 static void check_connectivity(void)
155 /* Look up all the requirements, warn about missing objects.. */
156 max
= get_max_object_index();
157 for (i
= 0; i
< max
; i
++) {
158 struct object
*obj
= get_indexed_object(i
);
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.
170 * So a directory called "a" is ordered _after_ a file
171 * called "a.c", because "a/" sorts after "a.c".
173 #define TREE_UNORDERED (-1)
174 #define TREE_HAS_DUPS (-2)
176 static int verify_ordered(unsigned mode1
, const char *name1
, unsigned mode2
, const char *name2
)
178 int len1
= strlen(name1
);
179 int len2
= strlen(name2
);
180 int len
= len1
< len2
? len1
: len2
;
181 unsigned char c1
, c2
;
184 cmp
= memcmp(name1
, name2
, len
);
188 return TREE_UNORDERED
;
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.
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.
203 return TREE_HAS_DUPS
;
204 if (!c1
&& S_ISDIR(mode1
))
206 if (!c2
&& S_ISDIR(mode2
))
208 return c1
< c2
? 0 : TREE_UNORDERED
;
211 static int fsck_tree(struct tree
*item
)
214 int has_full_path
= 0;
215 int has_zero_pad
= 0;
216 int has_bad_modes
= 0;
217 int has_dup_entries
= 0;
218 int not_properly_sorted
= 0;
219 struct tree_desc desc
;
222 const unsigned char *o_sha1
;
224 desc
.buf
= item
->buffer
;
225 desc
.size
= item
->size
;
233 const unsigned char *sha1
;
235 sha1
= tree_entry_extract(&desc
, &name
, &mode
);
237 if (strchr(name
, '/'))
239 has_zero_pad
|= *(char *)desc
.buf
== '0';
240 update_tree_entry(&desc
);
252 * This is nonstandard, but we had a few of these
253 * early on when we honored the full set of mode
264 switch (verify_ordered(o_mode
, o_name
, mode
, name
)) {
266 not_properly_sorted
= 1;
285 objwarning(&item
->object
, "contains full pathnames");
288 objwarning(&item
->object
, "contains zero-padded file modes");
291 objwarning(&item
->object
, "contains bad file modes");
293 if (has_dup_entries
) {
294 retval
= objerror(&item
->object
, "contains duplicate file entries");
296 if (not_properly_sorted
) {
297 retval
= objerror(&item
->object
, "not properly sorted");
302 static int fsck_commit(struct commit
*commit
)
304 char *buffer
= commit
->buffer
;
305 unsigned char tree_sha1
[20], sha1
[20];
307 if (memcmp(buffer
, "tree ", 5))
308 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");
312 while (!memcmp(buffer
, "parent ", 7)) {
313 if (get_sha1_hex(buffer
+7, sha1
) || buffer
[47] != '\n')
314 return objerror(&commit
->object
, "invalid 'parent' line format - bad sha1");
317 if (memcmp(buffer
, "author ", 7))
318 return objerror(&commit
->object
, "invalid format - expected 'author' line");
319 free(commit
->buffer
);
320 commit
->buffer
= NULL
;
322 return objerror(&commit
->object
, "could not load commit's tree %s", tree_sha1
);
323 if (!commit
->parents
&& show_root
)
324 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
326 printf("bad commit date in %s\n",
327 sha1_to_hex(commit
->object
.sha1
));
331 static int fsck_tag(struct tag
*tag
)
333 struct object
*tagged
= tag
->tagged
;
336 return objerror(&tag
->object
, "could not load tagged object");
341 printf("tagged %s %s", typename(tagged
->type
), sha1_to_hex(tagged
->sha1
));
342 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
346 static int fsck_sha1(unsigned char *sha1
)
348 struct object
*obj
= parse_object(sha1
);
350 return error("%s: object corrupt or missing", sha1_to_hex(sha1
));
351 if (obj
->flags
& SEEN
)
354 if (obj
->type
== OBJ_BLOB
)
356 if (obj
->type
== OBJ_TREE
)
357 return fsck_tree((struct tree
*) obj
);
358 if (obj
->type
== OBJ_COMMIT
)
359 return fsck_commit((struct commit
*) obj
);
360 if (obj
->type
== OBJ_TAG
)
361 return fsck_tag((struct tag
*) obj
);
362 /* By now, parse_object() would've returned NULL instead. */
363 return objerror(obj
, "unknown type '%d' (internal fsck error)", obj
->type
);
367 * This is the sorting chunk size: make it reasonably
368 * big so that we can sort well..
370 #define MAX_SHA1_ENTRIES (1024)
374 unsigned char sha1
[20];
379 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
382 static int ino_compare(const void *_a
, const void *_b
)
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;
389 static void fsck_sha1_list(void)
391 int i
, nr
= sha1_list
.nr
;
394 qsort(sha1_list
.entry
, nr
,
395 sizeof(struct sha1_entry
*), ino_compare
);
396 for (i
= 0; i
< nr
; i
++) {
397 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
398 unsigned char *sha1
= entry
->sha1
;
400 sha1_list
.entry
[i
] = NULL
;
407 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
409 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
413 hashcpy(entry
->sha1
, sha1
);
415 if (nr
== MAX_SHA1_ENTRIES
) {
419 sha1_list
.entry
[nr
] = entry
;
423 static void fsck_dir(int i
, char *path
)
425 DIR *dir
= opendir(path
);
431 while ((de
= readdir(dir
)) != NULL
) {
433 unsigned char sha1
[20];
434 int len
= strlen(de
->d_name
);
438 if (de
->d_name
[1] != '.')
441 if (de
->d_name
[0] != '.')
445 sprintf(name
, "%02x", i
);
446 memcpy(name
+2, de
->d_name
, len
+1);
447 if (get_sha1_hex(name
, sha1
) < 0)
449 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
452 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
457 static int default_refs
;
459 static 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
)
465 if (!is_null_sha1(osha1
)) {
466 obj
= lookup_object(osha1
);
469 mark_reachable(obj
, REACHABLE
);
472 obj
= lookup_object(nsha1
);
475 mark_reachable(obj
, REACHABLE
);
480 static int fsck_handle_reflog(const char *logname
, const unsigned char *sha1
, int flag
, void *cb_data
)
482 for_each_reflog_ent(logname
, fsck_handle_reflog_ent
, NULL
);
486 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
490 obj
= lookup_object(sha1
);
492 if (has_sha1_file(sha1
)) {
494 return 0; /* it is in a pack */
496 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
497 /* We'll continue with the rest despite the error.. */
502 mark_reachable(obj
, REACHABLE
);
507 static void get_default_heads(void)
509 for_each_ref(fsck_handle_ref
, NULL
);
510 for_each_reflog(fsck_handle_reflog
, NULL
);
513 * Not having any default heads isn't really fatal, but
514 * it does mean that "--unreachable" no longer makes any
515 * sense (since in this case everything will obviously
516 * be unreachable by definition.
518 * Showing dangling objects is valid, though (as those
519 * dangling objects are likely lost heads).
521 * So we just print a warning about it, and clear the
522 * "show_unreachable" flag.
525 error("No default references");
526 show_unreachable
= 0;
530 static void fsck_object_dir(const char *path
)
533 for (i
= 0; i
< 256; i
++) {
534 static char dir
[4096];
535 sprintf(dir
, "%s/%02x", path
, i
);
541 static int fsck_head_link(void)
543 unsigned char sha1
[20];
545 const char *head_points_at
= resolve_ref("HEAD", sha1
, 1, &flag
);
547 if (!head_points_at
|| !(flag
& REF_ISSYMREF
))
548 return error("HEAD is not a symbolic ref");
549 if (strncmp(head_points_at
, "refs/heads/", 11))
550 return error("HEAD points to something strange (%s)",
552 if (is_null_sha1(sha1
))
553 return error("HEAD: not a valid git pointer");
557 static int fsck_cache_tree(struct cache_tree
*it
)
562 if (0 <= it
->entry_count
) {
563 struct object
*obj
= parse_object(it
->sha1
);
565 error("%s: invalid sha1 pointer in cache-tree",
566 sha1_to_hex(it
->sha1
));
569 mark_reachable(obj
, REACHABLE
);
571 if (obj
->type
!= OBJ_TREE
)
572 err
|= objerror(obj
, "non-tree in cache-tree");
574 for (i
= 0; i
< it
->subtree_nr
; i
++)
575 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
579 int cmd_fsck(int argc
, char **argv
, const char *prefix
)
583 track_object_refs
= 1;
585 for (i
= 1; i
< argc
; i
++) {
586 const char *arg
= argv
[i
];
588 if (!strcmp(arg
, "--unreachable")) {
589 show_unreachable
= 1;
592 if (!strcmp(arg
, "--tags")) {
596 if (!strcmp(arg
, "--root")) {
600 if (!strcmp(arg
, "--cache")) {
601 keep_cache_objects
= 1;
604 if (!strcmp(arg
, "--full")) {
608 if (!strcmp(arg
, "--strict")) {
613 usage("git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]");
617 fsck_object_dir(get_object_directory());
619 struct alternate_object_database
*alt
;
620 struct packed_git
*p
;
622 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
623 char namebuf
[PATH_MAX
];
624 int namelen
= alt
->name
- alt
->base
;
625 memcpy(namebuf
, alt
->base
, namelen
);
626 namebuf
[namelen
- 1] = 0;
627 fsck_object_dir(namebuf
);
629 prepare_packed_git();
630 for (p
= packed_git
; p
; p
= p
->next
)
631 /* verify gives error messages itself */
634 for (p
= packed_git
; p
; p
= p
->next
) {
635 int num
= num_packed_objects(p
);
636 for (i
= 0; i
< num
; i
++) {
637 unsigned char sha1
[20];
638 nth_packed_object_sha1(p
, i
, sha1
);
645 for (i
= 1; i
< argc
; i
++) {
646 const char *arg
= argv
[i
];
651 if (!get_sha1(arg
, head_sha1
)) {
652 struct object
*obj
= lookup_object(head_sha1
);
654 /* Error is printed by lookup_object(). */
659 mark_reachable(obj
, REACHABLE
);
663 error("invalid parameter: expected sha1, got '%s'", arg
);
667 * If we've not been given any explicit head information, do the
668 * default ones from .git/refs. We also consider the index file
669 * in this case (ie this implies --cache).
673 keep_cache_objects
= 1;
676 if (keep_cache_objects
) {
679 for (i
= 0; i
< active_nr
; i
++) {
680 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
686 mark_reachable(obj
, REACHABLE
);
688 if (active_cache_tree
)
689 fsck_cache_tree(active_cache_tree
);
692 check_connectivity();