11 #include "cache-tree.h"
12 #include "tree-walk.h"
14 #define REACHABLE 0x0001
17 static int show_root
= 0;
18 static int show_tags
= 0;
19 static int show_unreachable
= 0;
20 static int check_full
= 0;
21 static int check_strict
= 0;
22 static int keep_cache_objects
= 0;
23 static unsigned char head_sha1
[20];
25 #ifdef NO_D_INO_IN_DIRENT
27 #define DIRENT_SORT_HINT(de) 0
30 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
33 static void objreport(struct object
*obj
, const char *severity
,
34 const char *err
, va_list params
)
36 fprintf(stderr
, "%s in %s %s: ",
37 severity
, typename(obj
->type
), sha1_to_hex(obj
->sha1
));
38 vfprintf(stderr
, err
, params
);
42 static int objerror(struct object
*obj
, const char *err
, ...)
45 va_start(params
, err
);
46 objreport(obj
, "error", err
, params
);
51 static int objwarning(struct object
*obj
, const char *err
, ...)
54 va_start(params
, err
);
55 objreport(obj
, "warning", err
, params
);
61 static void check_connectivity(void)
65 /* Look up all the requirements, warn about missing objects.. */
66 for (i
= 0; i
< obj_allocs
; i
++) {
67 struct object
*obj
= objs
[i
];
73 if (has_sha1_file(obj
->sha1
))
76 printf("missing %s %s\n",
77 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
82 const struct object_refs
*refs
= obj
->refs
;
84 for (j
= 0; j
< refs
->count
; j
++) {
85 struct object
*ref
= refs
->ref
[j
];
87 (has_sha1_file(ref
->sha1
)))
89 printf("broken link from %7s %s\n",
90 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
91 printf(" to %7s %s\n",
92 typename(ref
->type
), sha1_to_hex(ref
->sha1
));
96 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
97 printf("unreachable %s %s\n",
98 typename(obj
->type
), sha1_to_hex(obj
->sha1
));
103 printf("dangling %s %s\n", typename(obj
->type
),
104 sha1_to_hex(obj
->sha1
));
110 * The entries in a tree are ordered in the _path_ order,
111 * which means that a directory entry is ordered by adding
112 * a slash to the end of it.
114 * So a directory called "a" is ordered _after_ a file
115 * called "a.c", because "a/" sorts after "a.c".
117 #define TREE_UNORDERED (-1)
118 #define TREE_HAS_DUPS (-2)
120 static int verify_ordered(unsigned mode1
, const char *name1
, unsigned mode2
, const char *name2
)
122 int len1
= strlen(name1
);
123 int len2
= strlen(name2
);
124 int len
= len1
< len2
? len1
: len2
;
125 unsigned char c1
, c2
;
128 cmp
= memcmp(name1
, name2
, len
);
132 return TREE_UNORDERED
;
135 * Ok, the first <len> characters are the same.
136 * Now we need to order the next one, but turn
137 * a '\0' into a '/' for a directory entry.
143 * git-write-tree used to write out a nonsense tree that has
144 * entries with the same name, one blob and one tree. Make
145 * sure we do not have duplicate entries.
147 return TREE_HAS_DUPS
;
148 if (!c1
&& S_ISDIR(mode1
))
150 if (!c2
&& S_ISDIR(mode2
))
152 return c1
< c2
? 0 : TREE_UNORDERED
;
155 static int fsck_tree(struct tree
*item
)
158 int has_full_path
= 0;
159 int has_zero_pad
= 0;
160 int has_bad_modes
= 0;
161 int has_dup_entries
= 0;
162 int not_properly_sorted
= 0;
163 struct tree_desc desc
;
166 const unsigned char *o_sha1
;
168 desc
.buf
= item
->buffer
;
169 desc
.size
= item
->size
;
177 const unsigned char *sha1
;
179 sha1
= tree_entry_extract(&desc
, &name
, &mode
);
181 if (strchr(name
, '/'))
183 has_zero_pad
|= *(char *)desc
.buf
== '0';
184 update_tree_entry(&desc
);
196 * This is nonstandard, but we had a few of these
197 * early on when we honored the full set of mode
208 switch (verify_ordered(o_mode
, o_name
, mode
, name
)) {
210 not_properly_sorted
= 1;
229 objwarning(&item
->object
, "contains full pathnames");
232 objwarning(&item
->object
, "contains zero-padded file modes");
235 objwarning(&item
->object
, "contains bad file modes");
237 if (has_dup_entries
) {
238 retval
= objerror(&item
->object
, "contains duplicate file entries");
240 if (not_properly_sorted
) {
241 retval
= objerror(&item
->object
, "not properly sorted");
246 static int fsck_commit(struct commit
*commit
)
248 char *buffer
= commit
->buffer
;
249 unsigned char tree_sha1
[20], sha1
[20];
251 if (memcmp(buffer
, "tree ", 5))
252 return objerror(&commit
->object
, "invalid format - expected 'tree' line");
253 if (get_sha1_hex(buffer
+5, tree_sha1
) || buffer
[45] != '\n')
254 return objerror(&commit
->object
, "invalid 'tree' line format - bad sha1");
256 while (!memcmp(buffer
, "parent ", 7)) {
257 if (get_sha1_hex(buffer
+7, sha1
) || buffer
[47] != '\n')
258 return objerror(&commit
->object
, "invalid 'parent' line format - bad sha1");
261 if (memcmp(buffer
, "author ", 7))
262 return objerror(&commit
->object
, "invalid format - expected 'author' line");
263 free(commit
->buffer
);
264 commit
->buffer
= NULL
;
266 return objerror(&commit
->object
, "could not load commit's tree %s", tree_sha1
);
267 if (!commit
->parents
&& show_root
)
268 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
270 printf("bad commit date in %s\n",
271 sha1_to_hex(commit
->object
.sha1
));
275 static int fsck_tag(struct tag
*tag
)
277 struct object
*tagged
= tag
->tagged
;
280 return objerror(&tag
->object
, "could not load tagged object");
285 printf("tagged %s %s", typename(tagged
->type
), sha1_to_hex(tagged
->sha1
));
286 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
290 static int fsck_sha1(unsigned char *sha1
)
292 struct object
*obj
= parse_object(sha1
);
294 return error("%s: object not found", sha1_to_hex(sha1
));
295 if (obj
->flags
& SEEN
)
298 if (obj
->type
== TYPE_BLOB
)
300 if (obj
->type
== TYPE_TREE
)
301 return fsck_tree((struct tree
*) obj
);
302 if (obj
->type
== TYPE_COMMIT
)
303 return fsck_commit((struct commit
*) obj
);
304 if (obj
->type
== TYPE_TAG
)
305 return fsck_tag((struct tag
*) obj
);
306 /* By now, parse_object() would've returned NULL instead. */
307 return objerror(obj
, "unknown type '%d' (internal fsck error)", obj
->type
);
311 * This is the sorting chunk size: make it reasonably
312 * big so that we can sort well..
314 #define MAX_SHA1_ENTRIES (1024)
318 unsigned char sha1
[20];
323 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
326 static int ino_compare(const void *_a
, const void *_b
)
328 const struct sha1_entry
*a
= _a
, *b
= _b
;
329 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
330 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
333 static void fsck_sha1_list(void)
335 int i
, nr
= sha1_list
.nr
;
338 qsort(sha1_list
.entry
, nr
,
339 sizeof(struct sha1_entry
*), ino_compare
);
340 for (i
= 0; i
< nr
; i
++) {
341 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
342 unsigned char *sha1
= entry
->sha1
;
344 sha1_list
.entry
[i
] = NULL
;
351 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
353 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
357 memcpy(entry
->sha1
, sha1
, 20);
359 if (nr
== MAX_SHA1_ENTRIES
) {
363 sha1_list
.entry
[nr
] = entry
;
367 static int fsck_dir(int i
, char *path
)
369 DIR *dir
= opendir(path
);
375 while ((de
= readdir(dir
)) != NULL
) {
377 unsigned char sha1
[20];
378 int len
= strlen(de
->d_name
);
382 if (de
->d_name
[1] != '.')
385 if (de
->d_name
[0] != '.')
389 sprintf(name
, "%02x", i
);
390 memcpy(name
+2, de
->d_name
, len
+1);
391 if (get_sha1_hex(name
, sha1
) < 0)
393 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
396 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
402 static int default_refs
= 0;
404 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
)
408 obj
= lookup_object(sha1
);
410 if (has_sha1_file(sha1
)) {
412 return 0; /* it is in a pack */
414 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
415 /* We'll continue with the rest despite the error.. */
420 mark_reachable(obj
, REACHABLE
);
424 static void get_default_heads(void)
426 for_each_ref(fsck_handle_ref
);
428 die("No default references");
431 static void fsck_object_dir(const char *path
)
434 for (i
= 0; i
< 256; i
++) {
435 static char dir
[4096];
436 sprintf(dir
, "%s/%02x", path
, i
);
442 static int fsck_head_link(void)
444 unsigned char sha1
[20];
445 const char *git_HEAD
= strdup(git_path("HEAD"));
446 const char *git_refs_heads_master
= resolve_ref(git_HEAD
, sha1
, 1);
447 int pfxlen
= strlen(git_HEAD
) - 4; /* strip .../.git/ part */
449 if (!git_refs_heads_master
)
450 return error("HEAD is not a symbolic ref");
451 if (strncmp(git_refs_heads_master
+ pfxlen
, "refs/heads/", 11))
452 return error("HEAD points to something strange (%s)",
453 git_refs_heads_master
+ pfxlen
);
454 if (!memcmp(null_sha1
, sha1
, 20))
455 return error("HEAD: not a valid git pointer");
459 static int fsck_cache_tree(struct cache_tree
*it
)
464 if (0 <= it
->entry_count
) {
465 struct object
*obj
= parse_object(it
->sha1
);
467 error("%s: invalid sha1 pointer in cache-tree",
468 sha1_to_hex(it
->sha1
));
471 mark_reachable(obj
, REACHABLE
);
473 if (obj
->type
!= TYPE_TREE
)
474 err
|= objerror(obj
, "non-tree in cache-tree");
476 for (i
= 0; i
< it
->subtree_nr
; i
++)
477 err
|= fsck_cache_tree(it
->down
[i
]->cache_tree
);
481 int main(int argc
, char **argv
)
485 track_object_refs
= 1;
486 setup_git_directory();
488 for (i
= 1; i
< argc
; i
++) {
489 const char *arg
= argv
[i
];
491 if (!strcmp(arg
, "--unreachable")) {
492 show_unreachable
= 1;
495 if (!strcmp(arg
, "--tags")) {
499 if (!strcmp(arg
, "--root")) {
503 if (!strcmp(arg
, "--cache")) {
504 keep_cache_objects
= 1;
507 if (!strcmp(arg
, "--full")) {
511 if (!strcmp(arg
, "--strict")) {
516 usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]");
520 fsck_object_dir(get_object_directory());
522 struct alternate_object_database
*alt
;
523 struct packed_git
*p
;
525 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
526 char namebuf
[PATH_MAX
];
527 int namelen
= alt
->name
- alt
->base
;
528 memcpy(namebuf
, alt
->base
, namelen
);
529 namebuf
[namelen
- 1] = 0;
530 fsck_object_dir(namebuf
);
532 prepare_packed_git();
533 for (p
= packed_git
; p
; p
= p
->next
)
534 /* verify gives error messages itself */
537 for (p
= packed_git
; p
; p
= p
->next
) {
538 int num
= num_packed_objects(p
);
539 for (i
= 0; i
< num
; i
++) {
540 unsigned char sha1
[20];
541 nth_packed_object_sha1(p
, i
, sha1
);
548 for (i
= 1; i
< argc
; i
++) {
549 const char *arg
= argv
[i
];
554 if (!get_sha1(arg
, head_sha1
)) {
555 struct object
*obj
= lookup_object(head_sha1
);
557 /* Error is printed by lookup_object(). */
562 mark_reachable(obj
, REACHABLE
);
566 error("invalid parameter: expected sha1, got '%s'", arg
);
570 * If we've not been given any explicit head information, do the
571 * default ones from .git/refs. We also consider the index file
572 * in this case (ie this implies --cache).
576 keep_cache_objects
= 1;
579 if (keep_cache_objects
) {
582 for (i
= 0; i
< active_nr
; i
++) {
583 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
589 mark_reachable(obj
, REACHABLE
);
591 if (active_cache_tree
)
592 fsck_cache_tree(active_cache_tree
);
595 check_connectivity();