11 #include "cache-tree.h"
13 #define REACHABLE 0x0001
16 static int show_root = 0;
17 static int show_tags = 0;
18 static int show_unreachable = 0;
19 static int check_full = 0;
20 static int check_strict = 0;
21 static int keep_cache_objects = 0;
22 static unsigned char head_sha1[20];
24 #ifdef NO_D_INO_IN_DIRENT
26 #define DIRENT_SORT_HINT(de) 0
29 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
32 static void objreport(struct object *obj, const char *severity,
33 const char *err, va_list params)
35 fprintf(stderr, "%s in %s %s: ",
36 severity, obj->type, sha1_to_hex(obj->sha1));
37 vfprintf(stderr, err, params);
41 static int objerror(struct object *obj, const char *err, ...)
44 va_start(params, err);
45 objreport(obj, "error", err, params);
50 static int objwarning(struct object *obj, const char *err, ...)
53 va_start(params, err);
54 objreport(obj, "warning", err, params);
60 static void check_connectivity(void)
64 /* Look up all the requirements, warn about missing objects.. */
65 for (i = 0; i < obj_allocs; i++) {
66 struct object *obj = objs[i];
72 if (has_sha1_file(obj->sha1))
75 printf("missing %s %s\n",
76 obj->type, sha1_to_hex(obj->sha1));
81 const struct object_refs *refs = obj->refs;
83 for (j = 0; j < refs->count; j++) {
84 struct object *ref = refs->ref[j];
86 (has_sha1_file(ref->sha1)))
88 printf("broken link from %7s %s\n",
89 obj->type, sha1_to_hex(obj->sha1));
90 printf(" to %7s %s\n",
91 ref->type, sha1_to_hex(ref->sha1));
95 if (show_unreachable && !(obj->flags & REACHABLE)) {
96 printf("unreachable %s %s\n",
97 obj->type, sha1_to_hex(obj->sha1));
102 printf("dangling %s %s\n", obj->type,
103 sha1_to_hex(obj->sha1));
109 * The entries in a tree are ordered in the _path_ order,
110 * which means that a directory entry is ordered by adding
111 * a slash to the end of it.
113 * So a directory called "a" is ordered _after_ a file
114 * called "a.c", because "a/" sorts after "a.c".
116 #define TREE_UNORDERED (-1)
117 #define TREE_HAS_DUPS (-2)
119 static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
121 int len1 = strlen(a->name);
122 int len2 = strlen(b->name);
123 int len = len1 < len2 ? len1 : len2;
124 unsigned char c1, c2;
127 cmp = memcmp(a->name, b->name, len);
131 return TREE_UNORDERED;
134 * Ok, the first <len> characters are the same.
135 * Now we need to order the next one, but turn
136 * a '\0' into a '/' for a directory entry.
142 * git-write-tree used to write out a nonsense tree that has
143 * entries with the same name, one blob and one tree. Make
144 * sure we do not have duplicate entries.
146 return TREE_HAS_DUPS;
147 if (!c1 && a->directory)
149 if (!c2 && b->directory)
151 return c1 < c2 ? 0 : TREE_UNORDERED;
154 static int fsck_tree(struct tree *item)
157 int has_full_path = 0;
158 int has_zero_pad = 0;
159 int has_bad_modes = 0;
160 int has_dup_entries = 0;
161 int not_properly_sorted = 0;
162 struct tree_entry_list *entry, *last;
165 for (entry = create_tree_entry_list(item); entry; entry = entry->next) {
166 if (strchr(entry->name, '/'))
168 has_zero_pad |= entry->zeropad;
170 switch (entry->mode) {
180 * This is nonstandard, but we had a few of these
181 * early on when we honored the full set of mode
192 switch (verify_ordered(last, entry)) {
194 not_properly_sorted = 1;
214 objwarning(&item->object, "contains full pathnames");
217 objwarning(&item->object, "contains zero-padded file modes");
220 objwarning(&item->object, "contains bad file modes");
222 if (has_dup_entries) {
223 retval = objerror(&item->object, "contains duplicate file entries");
225 if (not_properly_sorted) {
226 retval = objerror(&item->object, "not properly sorted");
231 static int fsck_commit(struct commit *commit)
233 char *buffer = commit->buffer;
234 unsigned char tree_sha1[20], sha1[20];
236 if (memcmp(buffer, "tree ", 5))
237 return objerror(&commit->object, "invalid format - expected 'tree' line");
238 if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
239 return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
241 while (!memcmp(buffer, "parent ", 7)) {
242 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
243 return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
246 if (memcmp(buffer, "author ", 7))
247 return objerror(&commit->object, "invalid format - expected 'author' line");
248 free(commit->buffer);
249 commit->buffer = NULL;
251 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
252 if (!commit->parents && show_root)
253 printf("root %s\n", sha1_to_hex(commit->object.sha1));
255 printf("bad commit date in %s\n",
256 sha1_to_hex(commit->object.sha1));
260 static int fsck_tag(struct tag *tag)
262 struct object *tagged = tag->tagged;
265 return objerror(&tag->object, "could not load tagged object");
270 printf("tagged %s %s", tagged->type, sha1_to_hex(tagged->sha1));
271 printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
275 static int fsck_sha1(unsigned char *sha1)
277 struct object *obj = parse_object(sha1);
279 return error("%s: object not found", sha1_to_hex(sha1));
280 if (obj->flags & SEEN)
283 if (obj->type == blob_type)
285 if (obj->type == tree_type)
286 return fsck_tree((struct tree *) obj);
287 if (obj->type == commit_type)
288 return fsck_commit((struct commit *) obj);
289 if (obj->type == tag_type)
290 return fsck_tag((struct tag *) obj);
291 /* By now, parse_object() would've returned NULL instead. */
292 return objerror(obj, "unknown type '%s' (internal fsck error)", obj->type);
296 * This is the sorting chunk size: make it reasonably
297 * big so that we can sort well..
299 #define MAX_SHA1_ENTRIES (1024)
303 unsigned char sha1[20];
308 struct sha1_entry *entry[MAX_SHA1_ENTRIES];
311 static int ino_compare(const void *_a, const void *_b)
313 const struct sha1_entry *a = _a, *b = _b;
314 unsigned long ino1 = a->ino, ino2 = b->ino;
315 return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
318 static void fsck_sha1_list(void)
320 int i, nr = sha1_list.nr;
323 qsort(sha1_list.entry, nr,
324 sizeof(struct sha1_entry *), ino_compare);
325 for (i = 0; i < nr; i++) {
326 struct sha1_entry *entry = sha1_list.entry[i];
327 unsigned char *sha1 = entry->sha1;
329 sha1_list.entry[i] = NULL;
336 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
338 struct sha1_entry *entry = xmalloc(sizeof(*entry));
342 memcpy(entry->sha1, sha1, 20);
344 if (nr == MAX_SHA1_ENTRIES) {
348 sha1_list.entry[nr] = entry;
352 static int fsck_dir(int i, char *path)
354 DIR *dir = opendir(path);
360 while ((de = readdir(dir)) != NULL) {
362 unsigned char sha1[20];
363 int len = strlen(de->d_name);
367 if (de->d_name[1] != '.')
370 if (de->d_name[0] != '.')
374 sprintf(name, "%02x", i);
375 memcpy(name+2, de->d_name, len+1);
376 if (get_sha1_hex(name, sha1) < 0)
378 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
381 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
387 static int default_refs = 0;
389 static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
393 obj = lookup_object(sha1);
395 if (has_sha1_file(sha1)) {
397 return 0; /* it is in a pack */
399 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
400 /* We'll continue with the rest despite the error.. */
405 mark_reachable(obj, REACHABLE);
409 static void get_default_heads(void)
411 for_each_ref(fsck_handle_ref);
413 die("No default references");
416 static void fsck_object_dir(const char *path)
419 for (i = 0; i < 256; i++) {
420 static char dir[4096];
421 sprintf(dir, "%s/%02x", path, i);
427 static int fsck_head_link(void)
429 unsigned char sha1[20];
430 const char *git_HEAD = strdup(git_path("HEAD"));
431 const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 1);
432 int pfxlen = strlen(git_HEAD) - 4; /* strip .../.git/ part */
434 if (!git_refs_heads_master)
435 return error("HEAD is not a symbolic ref");
436 if (strncmp(git_refs_heads_master + pfxlen, "refs/heads/", 11))
437 return error("HEAD points to something strange (%s)",
438 git_refs_heads_master + pfxlen);
439 if (!memcmp(null_sha1, sha1, 20))
440 return error("HEAD: not a valid git pointer");
444 static int fsck_cache_tree(struct cache_tree *it)
449 if (0 <= it->entry_count) {
450 struct object *obj = parse_object(it->sha1);
452 error("%s: invalid sha1 pointer in cache-tree",
453 sha1_to_hex(it->sha1));
456 mark_reachable(obj, REACHABLE);
458 if (obj->type != tree_type)
459 err |= objerror(obj, "non-tree in cache-tree");
461 for (i = 0; i < it->subtree_nr; i++)
462 err |= fsck_cache_tree(it->down[i]->cache_tree);
466 int main(int argc, char **argv)
470 track_object_refs = 1;
471 setup_git_directory();
473 for (i = 1; i < argc; i++) {
474 const char *arg = argv[i];
476 if (!strcmp(arg, "--unreachable")) {
477 show_unreachable = 1;
480 if (!strcmp(arg, "--tags")) {
484 if (!strcmp(arg, "--root")) {
488 if (!strcmp(arg, "--cache")) {
489 keep_cache_objects = 1;
492 if (!strcmp(arg, "--full")) {
496 if (!strcmp(arg, "--strict")) {
501 usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]");
505 fsck_object_dir(get_object_directory());
507 struct alternate_object_database *alt;
508 struct packed_git *p;
510 for (alt = alt_odb_list; alt; alt = alt->next) {
511 char namebuf[PATH_MAX];
512 int namelen = alt->name - alt->base;
513 memcpy(namebuf, alt->base, namelen);
514 namebuf[namelen - 1] = 0;
515 fsck_object_dir(namebuf);
517 prepare_packed_git();
518 for (p = packed_git; p; p = p->next)
519 /* verify gives error messages itself */
522 for (p = packed_git; p; p = p->next) {
523 int num = num_packed_objects(p);
524 for (i = 0; i < num; i++) {
525 unsigned char sha1[20];
526 nth_packed_object_sha1(p, i, sha1);
533 for (i = 1; i < argc; i++) {
534 const char *arg = argv[i];
539 if (!get_sha1(arg, head_sha1)) {
540 struct object *obj = lookup_object(head_sha1);
542 /* Error is printed by lookup_object(). */
547 mark_reachable(obj, REACHABLE);
551 error("invalid parameter: expected sha1, got '%s'", arg);
555 * If we've not been given any explicit head information, do the
556 * default ones from .git/refs. We also consider the index file
557 * in this case (ie this implies --cache).
561 keep_cache_objects = 1;
564 if (keep_cache_objects) {
567 for (i = 0; i < active_nr; i++) {
568 struct blob *blob = lookup_blob(active_cache[i]->sha1);
574 mark_reachable(obj, REACHABLE);
576 if (active_cache_tree)
577 fsck_cache_tree(active_cache_tree);
580 check_connectivity();