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 check_full
= 0;
18 static int check_strict
= 0;
19 static int keep_cache_objects
= 0;
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
, 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 static void check_connectivity(void)
62 /* Look up all the requirements, warn about missing objects.. */
63 for (i
= 0; i
< obj_allocs
; i
++) {
64 struct object
*obj
= objs
[i
];
70 if (has_sha1_file(obj
->sha1
))
73 printf("missing %s %s\n",
74 obj
->type
, sha1_to_hex(obj
->sha1
));
79 const struct object_refs
*refs
= obj
->refs
;
81 for (j
= 0; j
< refs
->count
; j
++) {
82 struct object
*ref
= refs
->ref
[j
];
84 (has_sha1_file(ref
->sha1
)))
86 printf("broken link from %7s %s\n",
87 obj
->type
, sha1_to_hex(obj
->sha1
));
88 printf(" to %7s %s\n",
89 ref
->type
, sha1_to_hex(ref
->sha1
));
93 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
94 printf("unreachable %s %s\n",
95 obj
->type
, sha1_to_hex(obj
->sha1
));
100 printf("dangling %s %s\n", obj
->type
,
101 sha1_to_hex(obj
->sha1
));
107 * The entries in a tree are ordered in the _path_ order,
108 * which means that a directory entry is ordered by adding
109 * a slash to the end of it.
111 * So a directory called "a" is ordered _after_ a file
112 * called "a.c", because "a/" sorts after "a.c".
114 #define TREE_UNORDERED (-1)
115 #define TREE_HAS_DUPS (-2)
117 static int verify_ordered(struct tree_entry_list
*a
, struct tree_entry_list
*b
)
119 int len1
= strlen(a
->name
);
120 int len2
= strlen(b
->name
);
121 int len
= len1
< len2
? len1
: len2
;
122 unsigned char c1
, c2
;
125 cmp
= memcmp(a
->name
, b
->name
, len
);
129 return TREE_UNORDERED
;
132 * Ok, the first <len> characters are the same.
133 * Now we need to order the next one, but turn
134 * a '\0' into a '/' for a directory entry.
140 * git-write-tree used to write out a nonsense tree that has
141 * entries with the same name, one blob and one tree. Make
142 * sure we do not have duplicate entries.
144 return TREE_HAS_DUPS
;
145 if (!c1
&& a
->directory
)
147 if (!c2
&& b
->directory
)
149 return c1
< c2
? 0 : TREE_UNORDERED
;
152 static int fsck_tree(struct tree
*item
)
155 int has_full_path
= 0;
156 int has_zero_pad
= 0;
157 int has_bad_modes
= 0;
158 int has_dup_entries
= 0;
159 int not_properly_sorted
= 0;
160 struct tree_entry_list
*entry
, *last
;
163 for (entry
= item
->entries
; entry
; entry
= entry
->next
) {
164 if (strchr(entry
->name
, '/'))
166 has_zero_pad
|= entry
->zeropad
;
168 switch (entry
->mode
) {
178 * This is nonstandard, but we had a few of these
179 * early on when we honored the full set of mode
190 switch (verify_ordered(last
, entry
)) {
192 not_properly_sorted
= 1;
210 item
->entries
= NULL
;
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
->type
== blob_type
)
282 if (obj
->type
== tree_type
)
283 return fsck_tree((struct tree
*) obj
);
284 if (obj
->type
== commit_type
)
285 return fsck_commit((struct commit
*) obj
);
286 if (obj
->type
== tag_type
)
287 return fsck_tag((struct tag
*) obj
);
288 /* By now, parse_object() would've returned NULL instead. */
289 return objerror(obj
, "unknown type '%s' (internal fsck error)", obj
->type
);
293 * This is the sorting chunk size: make it reasonably
294 * big so that we can sort well..
296 #define MAX_SHA1_ENTRIES (1024)
300 unsigned char sha1
[20];
305 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
308 static int ino_compare(const void *_a
, const void *_b
)
310 const struct sha1_entry
*a
= _a
, *b
= _b
;
311 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
312 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
315 static void fsck_sha1_list(void)
317 int i
, nr
= sha1_list
.nr
;
320 qsort(sha1_list
.entry
, nr
,
321 sizeof(struct sha1_entry
*), ino_compare
);
322 for (i
= 0; i
< nr
; i
++) {
323 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
324 unsigned char *sha1
= entry
->sha1
;
326 sha1_list
.entry
[i
] = NULL
;
333 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
335 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
339 memcpy(entry
->sha1
, sha1
, 20);
341 if (nr
== MAX_SHA1_ENTRIES
) {
345 sha1_list
.entry
[nr
] = entry
;
349 static int fsck_dir(int i
, char *path
)
351 DIR *dir
= opendir(path
);
357 while ((de
= readdir(dir
)) != NULL
) {
359 unsigned char sha1
[20];
360 int len
= strlen(de
->d_name
);
364 if (de
->d_name
[1] != '.')
367 if (de
->d_name
[0] != '.')
371 sprintf(name
, "%02x", i
);
372 memcpy(name
+2, de
->d_name
, len
+1);
373 if (get_sha1_hex(name
, sha1
) < 0)
375 add_sha1_list(sha1
, DIRENT_SORT_HINT(de
));
378 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
384 static int default_refs
= 0;
386 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
)
390 obj
= lookup_object(sha1
);
392 if (has_sha1_file(sha1
)) {
394 return 0; /* it is in a pack */
396 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
397 /* We'll continue with the rest despite the error.. */
402 mark_reachable(obj
, REACHABLE
);
406 static void get_default_heads(void)
408 for_each_ref(fsck_handle_ref
);
410 die("No default references");
413 static void fsck_object_dir(const char *path
)
416 for (i
= 0; i
< 256; i
++) {
417 static char dir
[4096];
418 sprintf(dir
, "%s/%02x", path
, i
);
424 static int fsck_head_link(void)
426 unsigned char sha1
[20];
427 const char *git_HEAD
= strdup(git_path("HEAD"));
428 const char *git_refs_heads_master
= resolve_ref(git_HEAD
, sha1
, 1);
429 int pfxlen
= strlen(git_HEAD
) - 4; /* strip .../.git/ part */
431 if (!git_refs_heads_master
)
432 return error("HEAD is not a symbolic ref");
433 if (strncmp(git_refs_heads_master
+ pfxlen
, "refs/heads/", 11))
434 return error("HEAD points to something strange (%s)",
435 git_refs_heads_master
+ pfxlen
);
436 if (!memcmp(null_sha1
, sha1
, 20))
437 return error("HEAD: not a valid git pointer");
441 int main(int argc
, char **argv
)
445 setup_git_directory();
447 for (i
= 1; i
< argc
; i
++) {
448 const char *arg
= argv
[i
];
450 if (!strcmp(arg
, "--unreachable")) {
451 show_unreachable
= 1;
454 if (!strcmp(arg
, "--tags")) {
458 if (!strcmp(arg
, "--root")) {
462 if (!strcmp(arg
, "--cache")) {
463 keep_cache_objects
= 1;
466 if (!strcmp(arg
, "--full")) {
470 if (!strcmp(arg
, "--strict")) {
475 usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]");
479 fsck_object_dir(get_object_directory());
481 struct alternate_object_database
*alt
;
482 struct packed_git
*p
;
484 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
485 char namebuf
[PATH_MAX
];
486 int namelen
= alt
->name
- alt
->base
;
487 memcpy(namebuf
, alt
->base
, namelen
);
488 namebuf
[namelen
- 1] = 0;
489 fsck_object_dir(namebuf
);
491 prepare_packed_git();
492 for (p
= packed_git
; p
; p
= p
->next
)
493 /* verify gives error messages itself */
496 for (p
= packed_git
; p
; p
= p
->next
) {
497 int num
= num_packed_objects(p
);
498 for (i
= 0; i
< num
; i
++) {
499 unsigned char sha1
[20];
500 nth_packed_object_sha1(p
, i
, sha1
);
507 for (i
= 1; i
< argc
; i
++) {
508 const char *arg
= argv
[i
];
513 if (!get_sha1(arg
, head_sha1
)) {
514 struct object
*obj
= lookup_object(head_sha1
);
516 /* Error is printed by lookup_object(). */
521 mark_reachable(obj
, REACHABLE
);
525 error("invalid parameter: expected sha1, got '%s'", arg
);
529 * If we've not been given any explicit head information, do the
530 * default ones from .git/refs. We also consider the index file
531 * in this case (ie this implies --cache).
535 keep_cache_objects
= 1;
538 if (keep_cache_objects
) {
541 for (i
= 0; i
< active_nr
; i
++) {
542 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
548 mark_reachable(obj
, REACHABLE
);
552 check_connectivity();