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
];
61 if (!standalone
&& has_sha1_file(obj
->sha1
))
64 printf("missing %s %s\n",
65 obj
->type
, sha1_to_hex(obj
->sha1
));
70 const struct object_refs
*refs
= obj
->refs
;
72 for (j
= 0; j
< refs
->count
; j
++) {
73 struct object
*ref
= refs
->ref
[j
];
75 (!standalone
&& has_sha1_file(ref
->sha1
)))
77 printf("broken link from %7s %s\n",
78 obj
->type
, sha1_to_hex(obj
->sha1
));
79 printf(" to %7s %s\n",
80 ref
->type
, sha1_to_hex(ref
->sha1
));
84 if (show_unreachable
&& !(obj
->flags
& REACHABLE
)) {
85 printf("unreachable %s %s\n",
86 obj
->type
, sha1_to_hex(obj
->sha1
));
91 printf("dangling %s %s\n", obj
->type
,
92 sha1_to_hex(obj
->sha1
));
98 * The entries in a tree are ordered in the _path_ order,
99 * which means that a directory entry is ordered by adding
100 * a slash to the end of it.
102 * So a directory called "a" is ordered _after_ a file
103 * called "a.c", because "a/" sorts after "a.c".
105 #define TREE_UNORDERED (-1)
106 #define TREE_HAS_DUPS (-2)
108 static int verify_ordered(struct tree_entry_list
*a
, struct tree_entry_list
*b
)
110 int len1
= strlen(a
->name
);
111 int len2
= strlen(b
->name
);
112 int len
= len1
< len2
? len1
: len2
;
113 unsigned char c1
, c2
;
116 cmp
= memcmp(a
->name
, b
->name
, len
);
120 return TREE_UNORDERED
;
123 * Ok, the first <len> characters are the same.
124 * Now we need to order the next one, but turn
125 * a '\0' into a '/' for a directory entry.
131 * git-write-tree used to write out a nonsense tree that has
132 * entries with the same name, one blob and one tree. Make
133 * sure we do not have duplicate entries.
135 return TREE_HAS_DUPS
;
136 if (!c1
&& a
->directory
)
138 if (!c2
&& b
->directory
)
140 return c1
< c2
? 0 : TREE_UNORDERED
;
143 static int fsck_tree(struct tree
*item
)
146 int has_full_path
= 0;
147 int has_zero_pad
= 0;
148 int has_bad_modes
= 0;
149 int has_dup_entries
= 0;
150 int not_properly_sorted
= 0;
151 struct tree_entry_list
*entry
, *last
;
154 for (entry
= item
->entries
; entry
; entry
= entry
->next
) {
155 if (strchr(entry
->name
, '/'))
157 has_zero_pad
|= entry
->zeropad
;
159 switch (entry
->mode
) {
169 * This is nonstandard, but we had a few of these
170 * early on when we honored the full set of mode
181 switch (verify_ordered(last
, entry
)) {
183 not_properly_sorted
= 1;
201 item
->entries
= NULL
;
205 objwarning(&item
->object
, "contains full pathnames");
208 objwarning(&item
->object
, "contains zero-padded file modes");
211 objwarning(&item
->object
, "contains bad file modes");
213 if (has_dup_entries
) {
214 retval
= objerror(&item
->object
, "contains duplicate file entries");
216 if (not_properly_sorted
) {
217 retval
= objerror(&item
->object
, "not properly sorted");
222 static int fsck_commit(struct commit
*commit
)
224 char *buffer
= commit
->buffer
;
225 unsigned char tree_sha1
[20], sha1
[20];
227 if (memcmp(buffer
, "tree ", 5))
228 return objerror(&commit
->object
, "invalid format - expected 'tree' line");
229 if (get_sha1_hex(buffer
+5, tree_sha1
) || buffer
[45] != '\n')
230 return objerror(&commit
->object
, "invalid 'tree' line format - bad sha1");
232 while (!memcmp(buffer
, "parent ", 7)) {
233 if (get_sha1_hex(buffer
+7, sha1
) || buffer
[47] != '\n')
234 return objerror(&commit
->object
, "invalid 'parent' line format - bad sha1");
237 if (memcmp(buffer
, "author ", 7))
238 return objerror(&commit
->object
, "invalid format - expected 'author' line");
239 free(commit
->buffer
);
240 commit
->buffer
= NULL
;
242 return objerror(&commit
->object
, "could not load commit's tree %s", tree_sha1
);
243 if (!commit
->parents
&& show_root
)
244 printf("root %s\n", sha1_to_hex(commit
->object
.sha1
));
246 printf("bad commit date in %s\n",
247 sha1_to_hex(commit
->object
.sha1
));
251 static int fsck_tag(struct tag
*tag
)
253 struct object
*tagged
= tag
->tagged
;
256 return objerror(&tag
->object
, "could not load tagged object");
261 printf("tagged %s %s", tagged
->type
, sha1_to_hex(tagged
->sha1
));
262 printf(" (%s) in %s\n", tag
->tag
, sha1_to_hex(tag
->object
.sha1
));
266 static int fsck_sha1(unsigned char *sha1
)
268 struct object
*obj
= parse_object(sha1
);
270 return error("%s: object not found", sha1_to_hex(sha1
));
271 if (obj
->type
== blob_type
)
273 if (obj
->type
== tree_type
)
274 return fsck_tree((struct tree
*) obj
);
275 if (obj
->type
== commit_type
)
276 return fsck_commit((struct commit
*) obj
);
277 if (obj
->type
== tag_type
)
278 return fsck_tag((struct tag
*) obj
);
279 /* By now, parse_object() would've returned NULL instead. */
280 return objerror(obj
, "unknown type '%s' (internal fsck error)", obj
->type
);
284 * This is the sorting chunk size: make it reasonably
285 * big so that we can sort well..
287 #define MAX_SHA1_ENTRIES (1024)
291 unsigned char sha1
[20];
296 struct sha1_entry
*entry
[MAX_SHA1_ENTRIES
];
299 static int ino_compare(const void *_a
, const void *_b
)
301 const struct sha1_entry
*a
= _a
, *b
= _b
;
302 unsigned long ino1
= a
->ino
, ino2
= b
->ino
;
303 return ino1
< ino2
? -1 : ino1
> ino2
? 1 : 0;
306 static void fsck_sha1_list(void)
308 int i
, nr
= sha1_list
.nr
;
310 qsort(sha1_list
.entry
, nr
, sizeof(struct sha1_entry
*), ino_compare
);
311 for (i
= 0; i
< nr
; i
++) {
312 struct sha1_entry
*entry
= sha1_list
.entry
[i
];
313 unsigned char *sha1
= entry
->sha1
;
315 sha1_list
.entry
[i
] = NULL
;
322 static void add_sha1_list(unsigned char *sha1
, unsigned long ino
)
324 struct sha1_entry
*entry
= xmalloc(sizeof(*entry
));
328 memcpy(entry
->sha1
, sha1
, 20);
330 if (nr
== MAX_SHA1_ENTRIES
) {
334 sha1_list
.entry
[nr
] = entry
;
338 static int fsck_dir(int i
, char *path
)
340 DIR *dir
= opendir(path
);
346 while ((de
= readdir(dir
)) != NULL
) {
348 unsigned char sha1
[20];
349 int len
= strlen(de
->d_name
);
353 if (de
->d_name
[1] != '.')
356 if (de
->d_name
[0] != '.')
360 sprintf(name
, "%02x", i
);
361 memcpy(name
+2, de
->d_name
, len
+1);
362 if (get_sha1_hex(name
, sha1
) < 0)
364 add_sha1_list(sha1
, de
->d_ino
);
367 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
373 static int default_refs
= 0;
375 static int fsck_handle_ref(const char *refname
, const unsigned char *sha1
)
379 obj
= lookup_object(sha1
);
381 if (!standalone
&& has_sha1_file(sha1
)) {
383 return 0; /* it is in a pack */
385 error("%s: invalid sha1 pointer %s", refname
, sha1_to_hex(sha1
));
386 /* We'll continue with the rest despite the error.. */
391 mark_reachable(obj
, REACHABLE
);
395 static void get_default_heads(void)
397 for_each_ref(fsck_handle_ref
);
399 die("No default references");
402 static void fsck_object_dir(const char *path
)
405 for (i
= 0; i
< 256; i
++) {
406 static char dir
[4096];
407 sprintf(dir
, "%s/%02x", path
, i
);
413 static int fsck_head_link(void)
415 unsigned char sha1
[20];
416 const char *git_HEAD
= strdup(git_path("HEAD"));
417 const char *git_refs_heads_master
= resolve_ref(git_HEAD
, sha1
, 1);
418 int pfxlen
= strlen(git_HEAD
) - 4; /* strip .../.git/ part */
420 if (!git_refs_heads_master
)
421 return error("HEAD is not a symbolic ref");
422 if (strncmp(git_refs_heads_master
+ pfxlen
, "refs/heads/", 11))
423 return error("HEAD points to something strange (%s)",
424 git_refs_heads_master
+ pfxlen
);
425 if (!memcmp(null_sha1
, sha1
, 20))
426 return error("HEAD: not a valid git pointer");
430 int main(int argc
, char **argv
)
434 setup_git_directory();
436 for (i
= 1; i
< argc
; i
++) {
437 const char *arg
= argv
[i
];
439 if (!strcmp(arg
, "--unreachable")) {
440 show_unreachable
= 1;
443 if (!strcmp(arg
, "--tags")) {
447 if (!strcmp(arg
, "--root")) {
451 if (!strcmp(arg
, "--cache")) {
452 keep_cache_objects
= 1;
455 if (!strcmp(arg
, "--standalone")) {
459 if (!strcmp(arg
, "--full")) {
463 if (!strcmp(arg
, "--strict")) {
468 usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--standalone | --full] [--strict] <head-sha1>*]");
471 if (standalone
&& check_full
)
472 die("Only one of --standalone or --full can be used.");
474 putenv("GIT_ALTERNATE_OBJECT_DIRECTORIES=");
477 fsck_object_dir(get_object_directory());
479 struct alternate_object_database
*alt
;
480 struct packed_git
*p
;
482 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
483 char namebuf
[PATH_MAX
];
484 int namelen
= alt
->name
- alt
->base
;
485 memcpy(namebuf
, alt
->base
, namelen
);
486 namebuf
[namelen
- 1] = 0;
487 fsck_object_dir(namebuf
);
489 prepare_packed_git();
490 for (p
= packed_git
; p
; p
= p
->next
)
491 /* verify gives error messages itself */
494 for (p
= packed_git
; p
; p
= p
->next
) {
495 int num
= num_packed_objects(p
);
496 for (i
= 0; i
< num
; i
++) {
497 unsigned char sha1
[20];
498 nth_packed_object_sha1(p
, i
, sha1
);
505 for (i
= 1; i
< argc
; i
++) {
506 const char *arg
= argv
[i
];
511 if (!get_sha1(arg
, head_sha1
)) {
512 struct object
*obj
= lookup_object(head_sha1
);
514 /* Error is printed by lookup_object(). */
519 mark_reachable(obj
, REACHABLE
);
523 error("invalid parameter: expected sha1, got '%s'", arg
);
527 * If we've not been given any explicit head information, do the
528 * default ones from .git/refs. We also consider the index file
529 * in this case (ie this implies --cache).
533 keep_cache_objects
= 1;
536 if (keep_cache_objects
) {
539 for (i
= 0; i
< active_nr
; i
++) {
540 struct blob
*blob
= lookup_blob(active_cache
[i
]->sha1
);
546 mark_reachable(obj
, REACHABLE
);
550 check_connectivity();