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];
24 static void objreport(struct object
*obj
, const char *severity
,
25 const char *err
, va_list params
)
27 fprintf(stderr
, "%s in %s %s: ",
28 severity
, obj
->type
, sha1_to_hex(obj
->sha1
));
29 vfprintf(stderr
, err
, params
);
33 static int objerror(struct object
*obj
, const char *err
, ...)
36 va_start(params
, err
);
37 objreport(obj
, "error", err
, params
);
42 static int objwarning(struct object
*obj
, const char *err
, ...)
45 va_start(params
, err
);
46 objreport(obj
, "warning", err
, params
);
52 static void check_connectivity(void)
56 /* Look up all the requirements, warn about missing objects.. */
57 for (i
= 0; i
< nr_objs
; i
++) {
58 struct object
*obj
= objs
[i
];
59 struct object_list
*refs
;
62 if (!standalone
&& has_sha1_file(obj
->sha1
))
65 printf("missing %s %s\n",
66 obj
->type
, sha1_to_hex(obj
->sha1
));
70 for (refs
= obj
->refs
; refs
; refs
= refs
->next
) {
71 if (refs
->item
->parsed
||
72 (!standalone
&& has_sha1_file(refs
->item
->sha1
)))
74 printf("broken link from %7s %s\n",
75 obj
->type
, sha1_to_hex(obj
->sha1
));
76 printf(" to %7s %s\n",
77 refs
->item
->type
, sha1_to_hex(refs
->item
->sha1
));
80 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
81 printf("unreachable %s %s\n",
82 obj
->type
, sha1_to_hex(obj
->sha1
));
87 printf("dangling %s %s\n", obj
->type
,
88 sha1_to_hex(obj
->sha1
));
94 * The entries in a tree are ordered in the _path_ order,
95 * which means that a directory entry is ordered by adding
96 * a slash to the end of it.
98 * So a directory called "a" is ordered _after_ a file
99 * called "a.c", because "a/" sorts after "a.c".
101 #define TREE_UNORDERED (-1)
102 #define TREE_HAS_DUPS (-2)
104 static int verify_ordered(struct tree_entry_list
*a
, struct tree_entry_list
*b
)
106 int len1
= strlen(a
->name
);
107 int len2
= strlen(b
->name
);
108 int len
= len1
< len2
? len1
: len2
;
109 unsigned char c1
, c2
;
112 cmp
= memcmp(a
->name
, b
->name
, len
);
116 return TREE_UNORDERED
;
119 * Ok, the first <len> characters are the same.
120 * Now we need to order the next one, but turn
121 * a '\0' into a '/' for a directory entry.
127 * git-write-tree used to write out a nonsense tree that has
128 * entries with the same name, one blob and one tree. Make
129 * sure we do not have duplicate entries.
131 return TREE_HAS_DUPS
;
132 if (!c1
&& a
->directory
)
134 if (!c2
&& b
->directory
)
136 return c1
< c2
? 0 : TREE_UNORDERED
;
139 static int fsck_tree(struct tree
*item
)
142 int has_full_path
= 0;
143 int has_zero_pad
= 0;
144 int has_bad_modes
= 0;
145 int has_dup_entries
= 0;
146 int not_properly_sorted
= 0;
147 struct tree_entry_list
*entry
, *last
;
150 for (entry
= item
->entries
; entry
; entry
= entry
->next
) {
151 if (strchr(entry
->name
, '/'))
153 has_zero_pad
|= entry
->zeropad
;
155 switch (entry
->mode
) {
165 * This is nonstandard, but we had a few of these
166 * early on when we honored the full set of mode
177 switch (verify_ordered(last
, entry
)) {
179 not_properly_sorted
= 1;
194 objwarning(&item
->object
, "contains full pathnames");
197 objwarning(&item
->object
, "contains zero-padded file modes");
200 objwarning(&item
->object
, "contains bad file modes");
202 if (has_dup_entries
) {
203 retval
= objerror(&item
->object
, "contains duplicate file entries");
205 if (not_properly_sorted
) {
206 retval
= objerror(&item
->object
, "not properly sorted");
211 static int fsck_commit(struct commit
*commit
)
213 char *buffer
= commit
->buffer
;
214 unsigned char tree_sha1
[20], sha1
[20];
216 if (memcmp(buffer
, "tree ", 5))
217 return objerror(&commit
->object
, "invalid format - expected 'tree' line");
218 if (get_sha1_hex(buffer
+5, tree_sha1
) || buffer
[45] != '\n')
219 return objerror(&commit
->object
, "invalid 'tree' line format - bad sha1");
221 while (!memcmp(buffer
, "parent ", 7)) {
222 if (get_sha1_hex(buffer
+7, sha1
) || buffer
[47] != '\n')
223 return objerror(&commit
->object
, "invalid 'parent' line format - bad sha1");
226 if (memcmp(buffer
, "author ", 7))
227 return objerror(&commit
->object
, "invalid format - expected 'author' line");
228 free(commit
->buffer
);
229 commit
->buffer
= NULL
;
231 return objerror(&commit
->object
, "could not load commit's tree %s", tree_sha1
);
232 if (!commit
->parents
&& show_root
)
233 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
235 printf("bad commit date in %s\n",
236 sha1_to_hex(commit
->object
.sha1
));
240 static int fsck_tag(struct tag
*tag
)
242 struct object
*tagged
= tag
->tagged
;
245 return objerror(&tag
->object
, "could not load tagged object");
250 printf("tagged %s %s", tagged
->type
, sha1_to_hex(tagged
->sha1
));
251 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
255 static int fsck_sha1(unsigned char *sha1
)
257 struct object
*obj
= parse_object(sha1
);
259 return error("%s: object not found", sha1_to_hex(sha1
));
260 if (obj
->type
== blob_type
)
262 if (obj
->type
== tree_type
)
263 return fsck_tree((struct tree
*) obj
);
264 if (obj
->type
== commit_type
)
265 return fsck_commit((struct commit
*) obj
);
266 if (obj
->type
== tag_type
)
267 return fsck_tag((struct tag
*) obj
);
268 /* By now, parse_object() would've returned NULL instead. */
269 return objerror(obj
, "unknown type '%s' (internal fsck error)", obj
->type
);
273 * This is the sorting chunk size: make it reasonably
274 * big so that we can sort well..
276 #define MAX_SHA1_ENTRIES (1024)
280 unsigned char sha1
[20];
285 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
288 static int ino_compare(const void *_a
, const void *_b
)
290 const struct sha1_entry
*a
= _a
, *b
= _b
;
291 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
292 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
295 static void fsck_sha1_list(void)
297 int i
, nr
= sha1_list
.nr
;
299 qsort(sha1_list
.entry
, nr
, sizeof(struct sha1_entry
*), ino_compare
);
300 for (i
= 0; i
< nr
; i
++) {
301 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
302 unsigned char *sha1
= entry
->sha1
;
304 sha1_list
.entry
[i
] = NULL
;
311 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
313 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
317 memcpy(entry
->sha1
, sha1
, 20);
319 if (nr
== MAX_SHA1_ENTRIES
) {
323 sha1_list
.entry
[nr
] = entry
;
327 static int fsck_dir(int i
, char *path
)
329 DIR *dir
= opendir(path
);
333 return error("missing sha1 directory '%s'", path
);
336 while ((de
= readdir(dir
)) != NULL
) {
338 unsigned char sha1
[20];
339 int len
= strlen(de
->d_name
);
343 if (de
->d_name
[1] != '.')
346 if (de
->d_name
[0] != '.')
350 sprintf(name
, "%02x", i
);
351 memcpy(name
+2, de
->d_name
, len
+1);
352 if (get_sha1_hex(name
, sha1
) < 0)
354 add_sha1_list(sha1
, de
->d_ino
);
357 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
363 static int default_refs
= 0;
365 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
)
369 obj
= lookup_object(sha1
);
371 if (!standalone
&& has_sha1_file(sha1
)) {
373 return 0; /* it is in a pack */
375 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
376 /* We'll continue with the rest despite the error.. */
381 mark_reachable(obj
, REACHABLE
);
385 static void get_default_heads(void)
387 for_each_ref(fsck_handle_ref
);
389 die("No default references");
392 static void fsck_object_dir(const char *path
)
395 for (i
= 0; i
< 256; i
++) {
396 static char dir
[4096];
397 sprintf(dir
, "%s/%02x", path
, i
);
403 static int fsck_head_link(void)
407 unsigned char sha1
[20];
408 static char path
[PATH_MAX
], link
[PATH_MAX
];
409 const char *git_dir
= get_git_dir();
411 snprintf(path
, sizeof(path
), "%s/HEAD", git_dir
);
412 if (readlink(path
, link
, sizeof(link
)) < 0)
413 return error("HEAD is not a symlink");
414 if (strncmp("refs/heads/", link
, 11))
415 return error("HEAD points to something strange (%s)", link
);
416 fd
= open(path
, O_RDONLY
);
418 return error("HEAD: %s", strerror(errno
));
419 count
= read(fd
, hex
, sizeof(hex
));
422 return error("HEAD: %s", strerror(errno
));
423 if (count
< 40 || get_sha1_hex(hex
, sha1
))
424 return error("HEAD: not a valid git pointer");
428 int main(int argc
, char **argv
)
432 for (i
= 1; i
< argc
; i
++) {
433 const char *arg
= argv
[i
];
435 if (!strcmp(arg
, "--unreachable")) {
436 show_unreachable
= 1;
439 if (!strcmp(arg
, "--tags")) {
443 if (!strcmp(arg
, "--root")) {
447 if (!strcmp(arg
, "--cache")) {
448 keep_cache_objects
= 1;
451 if (!strcmp(arg
, "--standalone")) {
455 if (!strcmp(arg
, "--full")) {
459 if (!strcmp(arg
, "--strict")) {
464 usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--standalone | --full] [--strict] <head-sha1>*]");
467 if (standalone
&& check_full
)
468 die("Only one of --standalone or --full can be used.");
470 putenv("GIT_ALTERNATE_OBJECT_DIRECTORIES=");
473 fsck_object_dir(get_object_directory());
475 struct alternate_object_database
*alt
;
476 struct packed_git
*p
;
478 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
479 char namebuf
[PATH_MAX
];
480 int namelen
= alt
->name
- alt
->base
;
481 memcpy(namebuf
, alt
->base
, namelen
);
482 namebuf
[namelen
- 1] = 0;
483 fsck_object_dir(namebuf
);
485 prepare_packed_git();
486 for (p
= packed_git
; p
; p
= p
->next
)
487 /* verify gives error messages itself */
490 for (p
= packed_git
; p
; p
= p
->next
) {
491 int num
= num_packed_objects(p
);
492 for (i
= 0; i
< num
; i
++) {
493 unsigned char sha1
[20];
494 nth_packed_object_sha1(p
, i
, sha1
);
501 for (i
= 1; i
< argc
; i
++) {
502 const char *arg
= argv
[i
];
507 if (!get_sha1(arg
, head_sha1
)) {
508 struct object
*obj
= lookup_object(head_sha1
);
510 /* Error is printed by lookup_object(). */
515 mark_reachable(obj
, REACHABLE
);
519 error("invalid parameter: expected sha1, got '%s'", arg
);
523 * If we've not been given any explicit head information, do the
524 * default ones from .git/refs. We also consider the index file
525 * in this case (ie this implies --cache).
529 keep_cache_objects
= 1;
532 if (keep_cache_objects
) {
535 for (i
= 0; i
< active_nr
; i
++) {
536 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
542 mark_reachable(obj
, REACHABLE
);
546 check_connectivity();