Code

Documentation: stdout of update-hook is connected to /dev/null
[git.git] / fsck-objects.c
1 #include <sys/types.h>
2 #include <dirent.h>
4 #include "cache.h"
5 #include "commit.h"
6 #include "tree.h"
7 #include "blob.h"
8 #include "tag.h"
9 #include "refs.h"
10 #include "pack.h"
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)
26 {
27         fprintf(stderr, "%s in %s %s: ",
28                 severity, obj->type, sha1_to_hex(obj->sha1));
29         vfprintf(stderr, err, params);
30         fputs("\n", stderr);
31 }
33 static int objerror(struct object *obj, const char *err, ...)
34 {
35         va_list params;
36         va_start(params, err);
37         objreport(obj, "error", err, params);
38         va_end(params);
39         return -1;
40 }
42 static int objwarning(struct object *obj, const char *err, ...)
43 {
44         va_list params;
45         va_start(params, err);
46         objreport(obj, "warning", err, params);
47         va_end(params);
48         return -1;
49 }
52 static void check_connectivity(void)
53 {
54         int i;
56         /* Look up all the requirements, warn about missing objects.. */
57         for (i = 0; i < nr_objs; i++) {
58                 struct object *obj = objs[i];
60                 if (!obj->parsed) {
61                         if (!standalone && has_sha1_file(obj->sha1))
62                                 ; /* it is in pack */
63                         else
64                                 printf("missing %s %s\n",
65                                        obj->type, sha1_to_hex(obj->sha1));
66                         continue;
67                 }
69                 if (obj->refs) {
70                         const struct object_refs *refs = obj->refs;
71                         unsigned j;
72                         for (j = 0; j < refs->count; j++) {
73                                 struct object *ref = refs->ref[j];
74                                 if (ref->parsed ||
75                                     (!standalone && has_sha1_file(ref->sha1)))
76                                         continue;
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));
81                         }
82                 }
84                 if (show_unreachable && !(obj->flags & REACHABLE)) {
85                         printf("unreachable %s %s\n",
86                                obj->type, sha1_to_hex(obj->sha1));
87                         continue;
88                 }
90                 if (!obj->used) {
91                         printf("dangling %s %s\n", obj->type, 
92                                sha1_to_hex(obj->sha1));
93                 }
94         }
95 }
97 /*
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.
101  *
102  * So a directory called "a" is ordered _after_ a file
103  * called "a.c", because "a/" sorts after "a.c".
104  */
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;
114         int cmp;
116         cmp = memcmp(a->name, b->name, len);
117         if (cmp < 0)
118                 return 0;
119         if (cmp > 0)
120                 return TREE_UNORDERED;
122         /*
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.
126          */
127         c1 = a->name[len];
128         c2 = b->name[len];
129         if (!c1 && !c2)
130                 /*
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.
134                  */
135                 return TREE_HAS_DUPS;
136         if (!c1 && a->directory)
137                 c1 = '/';
138         if (!c2 && b->directory)
139                 c2 = '/';
140         return c1 < c2 ? 0 : TREE_UNORDERED;
143 static int fsck_tree(struct tree *item)
145         int retval;
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;
153         last = NULL;
154         for (entry = item->entries; entry; entry = entry->next) {
155                 if (strchr(entry->name, '/'))
156                         has_full_path = 1;
157                 has_zero_pad |= entry->zeropad;
159                 switch (entry->mode) {
160                 /*
161                  * Standard modes.. 
162                  */
163                 case S_IFREG | 0755:
164                 case S_IFREG | 0644:
165                 case S_IFLNK:
166                 case S_IFDIR:
167                         break;
168                 /*
169                  * This is nonstandard, but we had a few of these
170                  * early on when we honored the full set of mode
171                  * bits..
172                  */
173                 case S_IFREG | 0664:
174                         if (!check_strict)
175                                 break;
176                 default:
177                         has_bad_modes = 1;
178                 }
180                 if (last) {
181                         switch (verify_ordered(last, entry)) {
182                         case TREE_UNORDERED:
183                                 not_properly_sorted = 1;
184                                 break;
185                         case TREE_HAS_DUPS:
186                                 has_dup_entries = 1;
187                                 break;
188                         default:
189                                 break;
190                         }
191                         free(last->name);
192                         free(last);
193                 }
195                 last = entry;
196         }
197         if (last) {
198                 free(last->name);
199                 free(last);
200         }
201         item->entries = NULL;
203         retval = 0;
204         if (has_full_path) {
205                 objwarning(&item->object, "contains full pathnames");
206         }
207         if (has_zero_pad) {
208                 objwarning(&item->object, "contains zero-padded file modes");
209         }
210         if (has_bad_modes) {
211                 objwarning(&item->object, "contains bad file modes");
212         }
213         if (has_dup_entries) {
214                 retval = objerror(&item->object, "contains duplicate file entries");
215         }
216         if (not_properly_sorted) {
217                 retval = objerror(&item->object, "not properly sorted");
218         }
219         return retval;
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");
231         buffer += 46;
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");
235                 buffer += 48;
236         }
237         if (memcmp(buffer, "author ", 7))
238                 return objerror(&commit->object, "invalid format - expected 'author' line");
239         free(commit->buffer);
240         commit->buffer = NULL;
241         if (!commit->tree)
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));
245         if (!commit->date)
246                 printf("bad commit date in %s\n", 
247                        sha1_to_hex(commit->object.sha1));
248         return 0;
251 static int fsck_tag(struct tag *tag)
253         struct object *tagged = tag->tagged;
255         if (!tagged) {
256                 return objerror(&tag->object, "could not load tagged object");
257         }
258         if (!show_tags)
259                 return 0;
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));
263         return 0;
266 static int fsck_sha1(unsigned char *sha1)
268         struct object *obj = parse_object(sha1);
269         if (!obj)
270                 return error("%s: object not found", sha1_to_hex(sha1));
271         if (obj->type == blob_type)
272                 return 0;
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);
283 /*
284  * This is the sorting chunk size: make it reasonably
285  * big so that we can sort well..
286  */
287 #define MAX_SHA1_ENTRIES (1024)
289 struct sha1_entry {
290         unsigned long ino;
291         unsigned char sha1[20];
292 };
294 static struct {
295         unsigned long nr;
296         struct sha1_entry *entry[MAX_SHA1_ENTRIES];
297 } sha1_list;
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;
316                 fsck_sha1(sha1);
317                 free(entry);
318         }
319         sha1_list.nr = 0;
322 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
324         struct sha1_entry *entry = xmalloc(sizeof(*entry));
325         int nr;
327         entry->ino = ino;
328         memcpy(entry->sha1, sha1, 20);
329         nr = sha1_list.nr;
330         if (nr == MAX_SHA1_ENTRIES) {
331                 fsck_sha1_list();
332                 nr = 0;
333         }
334         sha1_list.entry[nr] = entry;
335         sha1_list.nr = ++nr;
338 static int fsck_dir(int i, char *path)
340         DIR *dir = opendir(path);
341         struct dirent *de;
343         if (!dir)
344                 return 0;
346         while ((de = readdir(dir)) != NULL) {
347                 char name[100];
348                 unsigned char sha1[20];
349                 int len = strlen(de->d_name);
351                 switch (len) {
352                 case 2:
353                         if (de->d_name[1] != '.')
354                                 break;
355                 case 1:
356                         if (de->d_name[0] != '.')
357                                 break;
358                         continue;
359                 case 38:
360                         sprintf(name, "%02x", i);
361                         memcpy(name+2, de->d_name, len+1);
362                         if (get_sha1_hex(name, sha1) < 0)
363                                 break;
364                         add_sha1_list(sha1, de->d_ino);
365                         continue;
366                 }
367                 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
368         }
369         closedir(dir);
370         return 0;
373 static int default_refs = 0;
375 static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
377         struct object *obj;
379         obj = lookup_object(sha1);
380         if (!obj) {
381                 if (!standalone && has_sha1_file(sha1)) {
382                         default_refs++;
383                         return 0; /* it is in a pack */
384                 }
385                 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
386                 /* We'll continue with the rest despite the error.. */
387                 return 0;
388         }
389         default_refs++;
390         obj->used = 1;
391         mark_reachable(obj, REACHABLE);
392         return 0;
395 static void get_default_heads(void)
397         for_each_ref(fsck_handle_ref);
398         if (!default_refs)
399                 die("No default references");
402 static void fsck_object_dir(const char *path)
404         int i;
405         for (i = 0; i < 256; i++) {
406                 static char dir[4096];
407                 sprintf(dir, "%s/%02x", path, i);
408                 fsck_dir(i, dir);
409         }
410         fsck_sha1_list();
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");
427         return 0;
430 int main(int argc, char **argv)
432         int i, heads;
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;
441                         continue;
442                 }
443                 if (!strcmp(arg, "--tags")) {
444                         show_tags = 1;
445                         continue;
446                 }
447                 if (!strcmp(arg, "--root")) {
448                         show_root = 1;
449                         continue;
450                 }
451                 if (!strcmp(arg, "--cache")) {
452                         keep_cache_objects = 1;
453                         continue;
454                 }
455                 if (!strcmp(arg, "--standalone")) {
456                         standalone = 1;
457                         continue;
458                 }
459                 if (!strcmp(arg, "--full")) {
460                         check_full = 1;
461                         continue;
462                 }
463                 if (!strcmp(arg, "--strict")) {
464                         check_strict = 1;
465                         continue;
466                 }
467                 if (*arg == '-')
468                         usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--standalone | --full] [--strict] <head-sha1>*]");
469         }
471         if (standalone && check_full)
472                 die("Only one of --standalone or --full can be used.");
473         if (standalone)
474                 putenv("GIT_ALTERNATE_OBJECT_DIRECTORIES=");
476         fsck_head_link();
477         fsck_object_dir(get_object_directory());
478         if (check_full) {
479                 struct alternate_object_database *alt;
480                 struct packed_git *p;
481                 prepare_alt_odb();
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);
488                 }
489                 prepare_packed_git();
490                 for (p = packed_git; p; p = p->next)
491                         /* verify gives error messages itself */
492                         verify_pack(p, 0);
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);
499                                 fsck_sha1(sha1);
500                         }
501                 }
502         }
504         heads = 0;
505         for (i = 1; i < argc; i++) {
506                 const char *arg = argv[i]; 
508                 if (*arg == '-')
509                         continue;
511                 if (!get_sha1(arg, head_sha1)) {
512                         struct object *obj = lookup_object(head_sha1);
514                         /* Error is printed by lookup_object(). */
515                         if (!obj)
516                                 continue;
518                         obj->used = 1;
519                         mark_reachable(obj, REACHABLE);
520                         heads++;
521                         continue;
522                 }
523                 error("invalid parameter: expected sha1, got '%s'", arg);
524         }
526         /*
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).
530          */
531         if (!heads) {
532                 get_default_heads();
533                 keep_cache_objects = 1;
534         }
536         if (keep_cache_objects) {
537                 int i;
538                 read_cache();
539                 for (i = 0; i < active_nr; i++) {
540                         struct blob *blob = lookup_blob(active_cache[i]->sha1);
541                         struct object *obj;
542                         if (!blob)
543                                 continue;
544                         obj = &blob->object;
545                         obj->used = 1;
546                         mark_reachable(obj, REACHABLE);
547                 }
548         }
550         check_connectivity();
551         return 0;