12 #define REACHABLE 0x0001
14 static int show_root
= 0;
15 static int show_tags
= 0;
16 static int show_unreachable
= 0;
17 static int standalone
= 0;
18 static int check_full
= 0;
19 static int check_strict
= 0;
20 static int keep_cache_objects
= 0;
21 static unsigned char head_sha1
[20];
23 #if NO_D_INO_IN_DIRENT
25 #define DIRENT_SORT_HINT(de) 0
28 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
31 static void objreport(struct object
*obj
, const char *severity
,
32 const char *err
, va_list params
)
34 fprintf(stderr
, "%s in %s %s: ",
35 severity
, obj
->type
, sha1_to_hex(obj
->sha1
));
36 vfprintf(stderr
, err
, params
);
40 static int objerror(struct object
*obj
, const char *err
, ...)
43 va_start(params
, err
);
44 objreport(obj
, "error", err
, params
);
49 static int objwarning(struct object
*obj
, const char *err
, ...)
52 va_start(params
, err
);
53 objreport(obj
, "warning", err
, params
);
59 static void check_connectivity(void)
63 /* Look up all the requirements, warn about missing objects.. */
64 for (i
= 0; i
< nr_objs
; i
++) {
65 struct object
*obj
= objs
[i
];
68 if (!standalone
&& has_sha1_file(obj
->sha1
))
71 printf("missing %s %s\n",
72 obj
->type
, sha1_to_hex(obj
->sha1
));
77 const struct object_refs
*refs
= obj
->refs
;
79 for (j
= 0; j
< refs
->count
; j
++) {
80 struct object
*ref
= refs
->ref
[j
];
82 (!standalone
&& has_sha1_file(ref
->sha1
)))
84 printf("broken link from %7s %s\n",
85 obj
->type
, sha1_to_hex(obj
->sha1
));
86 printf(" to %7s %s\n",
87 ref
->type
, sha1_to_hex(ref
->sha1
));
91 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
92 printf("unreachable %s %s\n",
93 obj
->type
, sha1_to_hex(obj
->sha1
));
98 printf("dangling %s %s\n", obj
->type
,
99 sha1_to_hex(obj
->sha1
));
105 * The entries in a tree are ordered in the _path_ order,
106 * which means that a directory entry is ordered by adding
107 * a slash to the end of it.
109 * So a directory called "a" is ordered _after_ a file
110 * called "a.c", because "a/" sorts after "a.c".
112 #define TREE_UNORDERED (-1)
113 #define TREE_HAS_DUPS (-2)
115 static int verify_ordered(struct tree_entry_list
*a
, struct tree_entry_list
*b
)
117 int len1
= strlen(a
->name
);
118 int len2
= strlen(b
->name
);
119 int len
= len1
< len2
? len1
: len2
;
120 unsigned char c1
, c2
;
123 cmp
= memcmp(a
->name
, b
->name
, len
);
127 return TREE_UNORDERED
;
130 * Ok, the first <len> characters are the same.
131 * Now we need to order the next one, but turn
132 * a '\0' into a '/' for a directory entry.
138 * git-write-tree used to write out a nonsense tree that has
139 * entries with the same name, one blob and one tree. Make
140 * sure we do not have duplicate entries.
142 return TREE_HAS_DUPS
;
143 if (!c1
&& a
->directory
)
145 if (!c2
&& b
->directory
)
147 return c1
< c2
? 0 : TREE_UNORDERED
;
150 static int fsck_tree(struct tree
*item
)
153 int has_full_path
= 0;
154 int has_zero_pad
= 0;
155 int has_bad_modes
= 0;
156 int has_dup_entries
= 0;
157 int not_properly_sorted
= 0;
158 struct tree_entry_list
*entry
, *last
;
161 for (entry
= item
->entries
; entry
; entry
= entry
->next
) {
162 if (strchr(entry
->name
, '/'))
164 has_zero_pad
|= entry
->zeropad
;
166 switch (entry
->mode
) {
176 * This is nonstandard, but we had a few of these
177 * early on when we honored the full set of mode
188 switch (verify_ordered(last
, entry
)) {
190 not_properly_sorted
= 1;
208 item
->entries
= NULL
;
212 objwarning(&item
->object
, "contains full pathnames");
215 objwarning(&item
->object
, "contains zero-padded file modes");
218 objwarning(&item
->object
, "contains bad file modes");
220 if (has_dup_entries
) {
221 retval
= objerror(&item
->object
, "contains duplicate file entries");
223 if (not_properly_sorted
) {
224 retval
= objerror(&item
->object
, "not properly sorted");
229 static int fsck_commit(struct commit
*commit
)
231 char *buffer
= commit
->buffer
;
232 unsigned char tree_sha1
[20], sha1
[20];
234 if (memcmp(buffer
, "tree ", 5))
235 return objerror(&commit
->object
, "invalid format - expected 'tree' line");
236 if (get_sha1_hex(buffer
+5, tree_sha1
) || buffer
[45] != '\n')
237 return objerror(&commit
->object
, "invalid 'tree' line format - bad sha1");
239 while (!memcmp(buffer
, "parent ", 7)) {
240 if (get_sha1_hex(buffer
+7, sha1
) || buffer
[47] != '\n')
241 return objerror(&commit
->object
, "invalid 'parent' line format - bad sha1");
244 if (memcmp(buffer
, "author ", 7))
245 return objerror(&commit
->object
, "invalid format - expected 'author' line");
246 free(commit
->buffer
);
247 commit
->buffer
= NULL
;
249 return objerror(&commit
->object
, "could not load commit's tree %s", tree_sha1
);
250 if (!commit
->parents
&& show_root
)
251 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
253 printf("bad commit date in %s\n",
254 sha1_to_hex(commit
->object
.sha1
));
258 static int fsck_tag(struct tag
*tag
)
260 struct object
*tagged
= tag
->tagged
;
263 return objerror(&tag
->object
, "could not load tagged object");
268 printf("tagged %s %s", tagged
->type
, sha1_to_hex(tagged
->sha1
));
269 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
273 static int fsck_sha1(unsigned char *sha1
)
275 struct object
*obj
= parse_object(sha1
);
277 return error("%s: object not found", sha1_to_hex(sha1
));
278 if (obj
->type
== blob_type
)
280 if (obj
->type
== tree_type
)
281 return fsck_tree((struct tree
*) obj
);
282 if (obj
->type
== commit_type
)
283 return fsck_commit((struct commit
*) obj
);
284 if (obj
->type
== tag_type
)
285 return fsck_tag((struct tag
*) obj
);
286 /* By now, parse_object() would've returned NULL instead. */
287 return objerror(obj
, "unknown type '%s' (internal fsck error)", obj
->type
);
291 * This is the sorting chunk size: make it reasonably
292 * big so that we can sort well..
294 #define MAX_SHA1_ENTRIES (1024)
298 unsigned char sha1
[20];
303 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
306 static int ino_compare(const void *_a
, const void *_b
)
308 const struct sha1_entry
*a
= _a
, *b
= _b
;
309 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
310 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
313 static void fsck_sha1_list(void)
315 int i
, nr
= sha1_list
.nr
;
318 qsort(sha1_list
.entry
, nr
,
319 sizeof(struct sha1_entry
*), ino_compare
);
320 for (i
= 0; i
< nr
; i
++) {
321 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
322 unsigned char *sha1
= entry
->sha1
;
324 sha1_list
.entry
[i
] = NULL
;
331 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
333 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
337 memcpy(entry
->sha1
, sha1
, 20);
339 if (nr
== MAX_SHA1_ENTRIES
) {
343 sha1_list
.entry
[nr
] = entry
;
347 static int fsck_dir(int i
, char *path
)
349 DIR *dir
= opendir(path
);
355 while ((de
= readdir(dir
)) != NULL
) {
357 unsigned char sha1
[20];
358 int len
= strlen(de
->d_name
);
362 if (de
->d_name
[1] != '.')
365 if (de
->d_name
[0] != '.')
369 sprintf(name
, "%02x", i
);
370 memcpy(name
+2, de
->d_name
, len
+1);
371 if (get_sha1_hex(name
, sha1
) < 0)
373 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
376 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
382 static int default_refs
= 0;
384 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
)
388 obj
= lookup_object(sha1
);
390 if (!standalone
&& has_sha1_file(sha1
)) {
392 return 0; /* it is in a pack */
394 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
395 /* We'll continue with the rest despite the error.. */
400 mark_reachable(obj
, REACHABLE
);
404 static void get_default_heads(void)
406 for_each_ref(fsck_handle_ref
);
408 die("No default references");
411 static void fsck_object_dir(const char *path
)
414 for (i
= 0; i
< 256; i
++) {
415 static char dir
[4096];
416 sprintf(dir
, "%s/%02x", path
, i
);
422 static int fsck_head_link(void)
424 unsigned char sha1
[20];
425 const char *git_HEAD
= strdup(git_path("HEAD"));
426 const char *git_refs_heads_master
= resolve_ref(git_HEAD
, sha1
, 1);
427 int pfxlen
= strlen(git_HEAD
) - 4; /* strip .../.git/ part */
429 if (!git_refs_heads_master
)
430 return error("HEAD is not a symbolic ref");
431 if (strncmp(git_refs_heads_master
+ pfxlen
, "refs/heads/", 11))
432 return error("HEAD points to something strange (%s)",
433 git_refs_heads_master
+ pfxlen
);
434 if (!memcmp(null_sha1
, sha1
, 20))
435 return error("HEAD: not a valid git pointer");
439 int main(int argc
, char **argv
)
443 setup_git_directory();
445 for (i
= 1; i
< argc
; i
++) {
446 const char *arg
= argv
[i
];
448 if (!strcmp(arg
, "--unreachable")) {
449 show_unreachable
= 1;
452 if (!strcmp(arg
, "--tags")) {
456 if (!strcmp(arg
, "--root")) {
460 if (!strcmp(arg
, "--cache")) {
461 keep_cache_objects
= 1;
464 if (!strcmp(arg
, "--standalone")) {
468 if (!strcmp(arg
, "--full")) {
472 if (!strcmp(arg
, "--strict")) {
477 usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--standalone | --full] [--strict] <head-sha1>*]");
480 if (standalone
&& check_full
)
481 die("Only one of --standalone or --full can be used.");
483 putenv("GIT_ALTERNATE_OBJECT_DIRECTORIES=");
486 fsck_object_dir(get_object_directory());
488 struct alternate_object_database
*alt
;
489 struct packed_git
*p
;
491 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
492 char namebuf
[PATH_MAX
];
493 int namelen
= alt
->name
- alt
->base
;
494 memcpy(namebuf
, alt
->base
, namelen
);
495 namebuf
[namelen
- 1] = 0;
496 fsck_object_dir(namebuf
);
498 prepare_packed_git();
499 for (p
= packed_git
; p
; p
= p
->next
)
500 /* verify gives error messages itself */
503 for (p
= packed_git
; p
; p
= p
->next
) {
504 int num
= num_packed_objects(p
);
505 for (i
= 0; i
< num
; i
++) {
506 unsigned char sha1
[20];
507 nth_packed_object_sha1(p
, i
, sha1
);
514 for (i
= 1; i
< argc
; i
++) {
515 const char *arg
= argv
[i
];
520 if (!get_sha1(arg
, head_sha1
)) {
521 struct object
*obj
= lookup_object(head_sha1
);
523 /* Error is printed by lookup_object(). */
528 mark_reachable(obj
, REACHABLE
);
532 error("invalid parameter: expected sha1, got '%s'", arg
);
536 * If we've not been given any explicit head information, do the
537 * default ones from .git/refs. We also consider the index file
538 * in this case (ie this implies --cache).
542 keep_cache_objects
= 1;
545 if (keep_cache_objects
) {
548 for (i
= 0; i
< active_nr
; i
++) {
549 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
555 mark_reachable(obj
, REACHABLE
);
559 check_connectivity();