Add raw tree buffer info to "struct tree"
[git.git] / fsck-objects.c
1 #include <sys/types.h>
2 #include <dirent.h>
3
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"
11
12 #define REACHABLE 0x0001
13
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];
21
22 #ifdef NO_D_INO_IN_DIRENT
23 #define SORT_DIRENT 0
24 #define DIRENT_SORT_HINT(de) 0
25 #else
26 #define SORT_DIRENT 1
27 #define DIRENT_SORT_HINT(de) ((de)->d_ino)
28 #endif
29
30 static void objreport(struct object *obj, const char *severity,
31                       const char *err, va_list params)
32 {
33         fprintf(stderr, "%s in %s %s: ",
34                 severity, obj->type, sha1_to_hex(obj->sha1));
35         vfprintf(stderr, err, params);
36         fputs("\n", stderr);
37 }
38
39 static int objerror(struct object *obj, const char *err, ...)
40 {
41         va_list params;
42         va_start(params, err);
43         objreport(obj, "error", err, params);
44         va_end(params);
45         return -1;
46 }
47
48 static int objwarning(struct object *obj, const char *err, ...)
49 {
50         va_list params;
51         va_start(params, err);
52         objreport(obj, "warning", err, params);
53         va_end(params);
54         return -1;
55 }
56
57
58 static void check_connectivity(void)
59 {
60         int i;
61
62         /* Look up all the requirements, warn about missing objects.. */
63         for (i = 0; i < obj_allocs; i++) {
64                 struct object *obj = objs[i];
65
66                 if (!obj)
67                         continue;
68
69                 if (!obj->parsed) {
70                         if (has_sha1_file(obj->sha1))
71                                 ; /* it is in pack */
72                         else
73                                 printf("missing %s %s\n",
74                                        obj->type, sha1_to_hex(obj->sha1));
75                         continue;
76                 }
77
78                 if (obj->refs) {
79                         const struct object_refs *refs = obj->refs;
80                         unsigned j;
81                         for (j = 0; j < refs->count; j++) {
82                                 struct object *ref = refs->ref[j];
83                                 if (ref->parsed ||
84                                     (has_sha1_file(ref->sha1)))
85                                         continue;
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));
90                         }
91                 }
92
93                 if (show_unreachable && !(obj->flags & REACHABLE)) {
94                         printf("unreachable %s %s\n",
95                                obj->type, sha1_to_hex(obj->sha1));
96                         continue;
97                 }
98
99                 if (!obj->used) {
100                         printf("dangling %s %s\n", obj->type, 
101                                sha1_to_hex(obj->sha1));
102                 }
103         }
104 }
105
106 /*
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.
110  *
111  * So a directory called "a" is ordered _after_ a file
112  * called "a.c", because "a/" sorts after "a.c".
113  */
114 #define TREE_UNORDERED (-1)
115 #define TREE_HAS_DUPS  (-2)
116
117 static int verify_ordered(struct tree_entry_list *a, struct tree_entry_list *b)
118 {
119         int len1 = strlen(a->name);
120         int len2 = strlen(b->name);
121         int len = len1 < len2 ? len1 : len2;
122         unsigned char c1, c2;
123         int cmp;
124
125         cmp = memcmp(a->name, b->name, len);
126         if (cmp < 0)
127                 return 0;
128         if (cmp > 0)
129                 return TREE_UNORDERED;
130
131         /*
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.
135          */
136         c1 = a->name[len];
137         c2 = b->name[len];
138         if (!c1 && !c2)
139                 /*
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.
143                  */
144                 return TREE_HAS_DUPS;
145         if (!c1 && a->directory)
146                 c1 = '/';
147         if (!c2 && b->directory)
148                 c2 = '/';
149         return c1 < c2 ? 0 : TREE_UNORDERED;
150 }
151
152 static int fsck_tree(struct tree *item)
153 {
154         int retval;
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;
161
162         last = NULL;
163         for (entry = item->entries; entry; entry = entry->next) {
164                 if (strchr(entry->name, '/'))
165                         has_full_path = 1;
166                 has_zero_pad |= entry->zeropad;
167
168                 switch (entry->mode) {
169                 /*
170                  * Standard modes.. 
171                  */
172                 case S_IFREG | 0755:
173                 case S_IFREG | 0644:
174                 case S_IFLNK:
175                 case S_IFDIR:
176                         break;
177                 /*
178                  * This is nonstandard, but we had a few of these
179                  * early on when we honored the full set of mode
180                  * bits..
181                  */
182                 case S_IFREG | 0664:
183                         if (!check_strict)
184                                 break;
185                 default:
186                         has_bad_modes = 1;
187                 }
188
189                 if (last) {
190                         switch (verify_ordered(last, entry)) {
191                         case TREE_UNORDERED:
192                                 not_properly_sorted = 1;
193                                 break;
194                         case TREE_HAS_DUPS:
195                                 has_dup_entries = 1;
196                                 break;
197                         default:
198                                 break;
199                         }
200                         free(last);
201                 }
202
203                 last = entry;
204         }
205         if (last)
206                 free(last);
207         item->entries = NULL;
208         free(item->buffer);
209         item->buffer = NULL;
210
211         retval = 0;
212         if (has_full_path) {
213                 objwarning(&item->object, "contains full pathnames");
214         }
215         if (has_zero_pad) {
216                 objwarning(&item->object, "contains zero-padded file modes");
217         }
218         if (has_bad_modes) {
219                 objwarning(&item->object, "contains bad file modes");
220         }
221         if (has_dup_entries) {
222                 retval = objerror(&item->object, "contains duplicate file entries");
223         }
224         if (not_properly_sorted) {
225                 retval = objerror(&item->object, "not properly sorted");
226         }
227         return retval;
228 }
229
230 static int fsck_commit(struct commit *commit)
231 {
232         char *buffer = commit->buffer;
233         unsigned char tree_sha1[20], sha1[20];
234
235         if (memcmp(buffer, "tree ", 5))
236                 return objerror(&commit->object, "invalid format - expected 'tree' line");
237         if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
238                 return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
239         buffer += 46;
240         while (!memcmp(buffer, "parent ", 7)) {
241                 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
242                         return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
243                 buffer += 48;
244         }
245         if (memcmp(buffer, "author ", 7))
246                 return objerror(&commit->object, "invalid format - expected 'author' line");
247         free(commit->buffer);
248         commit->buffer = NULL;
249         if (!commit->tree)
250                 return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
251         if (!commit->parents && show_root)
252                 printf("root %s\n", sha1_to_hex(commit->object.sha1));
253         if (!commit->date)
254                 printf("bad commit date in %s\n", 
255                        sha1_to_hex(commit->object.sha1));
256         return 0;
257 }
258
259 static int fsck_tag(struct tag *tag)
260 {
261         struct object *tagged = tag->tagged;
262
263         if (!tagged) {
264                 return objerror(&tag->object, "could not load tagged object");
265         }
266         if (!show_tags)
267                 return 0;
268
269         printf("tagged %s %s", tagged->type, sha1_to_hex(tagged->sha1));
270         printf(" (%s) in %s\n", tag->tag, sha1_to_hex(tag->object.sha1));
271         return 0;
272 }
273
274 static int fsck_sha1(unsigned char *sha1)
275 {
276         struct object *obj = parse_object(sha1);
277         if (!obj)
278                 return error("%s: object not found", sha1_to_hex(sha1));
279         if (obj->type == blob_type)
280                 return 0;
281         if (obj->type == tree_type)
282                 return fsck_tree((struct tree *) obj);
283         if (obj->type == commit_type)
284                 return fsck_commit((struct commit *) obj);
285         if (obj->type == tag_type)
286                 return fsck_tag((struct tag *) obj);
287         /* By now, parse_object() would've returned NULL instead. */
288         return objerror(obj, "unknown type '%s' (internal fsck error)", obj->type);
289 }
290
291 /*
292  * This is the sorting chunk size: make it reasonably
293  * big so that we can sort well..
294  */
295 #define MAX_SHA1_ENTRIES (1024)
296
297 struct sha1_entry {
298         unsigned long ino;
299         unsigned char sha1[20];
300 };
301
302 static struct {
303         unsigned long nr;
304         struct sha1_entry *entry[MAX_SHA1_ENTRIES];
305 } sha1_list;
306
307 static int ino_compare(const void *_a, const void *_b)
308 {
309         const struct sha1_entry *a = _a, *b = _b;
310         unsigned long ino1 = a->ino, ino2 = b->ino;
311         return ino1 < ino2 ? -1 : ino1 > ino2 ? 1 : 0;
312 }
313
314 static void fsck_sha1_list(void)
315 {
316         int i, nr = sha1_list.nr;
317
318         if (SORT_DIRENT)
319                 qsort(sha1_list.entry, nr,
320                       sizeof(struct sha1_entry *), ino_compare);
321         for (i = 0; i < nr; i++) {
322                 struct sha1_entry *entry = sha1_list.entry[i];
323                 unsigned char *sha1 = entry->sha1;
324
325                 sha1_list.entry[i] = NULL;
326                 fsck_sha1(sha1);
327                 free(entry);
328         }
329         sha1_list.nr = 0;
330 }
331
332 static void add_sha1_list(unsigned char *sha1, unsigned long ino)
333 {
334         struct sha1_entry *entry = xmalloc(sizeof(*entry));
335         int nr;
336
337         entry->ino = ino;
338         memcpy(entry->sha1, sha1, 20);
339         nr = sha1_list.nr;
340         if (nr == MAX_SHA1_ENTRIES) {
341                 fsck_sha1_list();
342                 nr = 0;
343         }
344         sha1_list.entry[nr] = entry;
345         sha1_list.nr = ++nr;
346 }
347
348 static int fsck_dir(int i, char *path)
349 {
350         DIR *dir = opendir(path);
351         struct dirent *de;
352
353         if (!dir)
354                 return 0;
355
356         while ((de = readdir(dir)) != NULL) {
357                 char name[100];
358                 unsigned char sha1[20];
359                 int len = strlen(de->d_name);
360
361                 switch (len) {
362                 case 2:
363                         if (de->d_name[1] != '.')
364                                 break;
365                 case 1:
366                         if (de->d_name[0] != '.')
367                                 break;
368                         continue;
369                 case 38:
370                         sprintf(name, "%02x", i);
371                         memcpy(name+2, de->d_name, len+1);
372                         if (get_sha1_hex(name, sha1) < 0)
373                                 break;
374                         add_sha1_list(sha1, DIRENT_SORT_HINT(de));
375                         continue;
376                 }
377                 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
378         }
379         closedir(dir);
380         return 0;
381 }
382
383 static int default_refs = 0;
384
385 static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
386 {
387         struct object *obj;
388
389         obj = lookup_object(sha1);
390         if (!obj) {
391                 if (has_sha1_file(sha1)) {
392                         default_refs++;
393                         return 0; /* it is in a pack */
394                 }
395                 error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
396                 /* We'll continue with the rest despite the error.. */
397                 return 0;
398         }
399         default_refs++;
400         obj->used = 1;
401         mark_reachable(obj, REACHABLE);
402         return 0;
403 }
404
405 static void get_default_heads(void)
406 {
407         for_each_ref(fsck_handle_ref);
408         if (!default_refs)
409                 die("No default references");
410 }
411
412 static void fsck_object_dir(const char *path)
413 {
414         int i;
415         for (i = 0; i < 256; i++) {
416                 static char dir[4096];
417                 sprintf(dir, "%s/%02x", path, i);
418                 fsck_dir(i, dir);
419         }
420         fsck_sha1_list();
421 }
422
423 static int fsck_head_link(void)
424 {
425         unsigned char sha1[20];
426         const char *git_HEAD = strdup(git_path("HEAD"));
427         const char *git_refs_heads_master = resolve_ref(git_HEAD, sha1, 1);
428         int pfxlen = strlen(git_HEAD) - 4; /* strip .../.git/ part */
429
430         if (!git_refs_heads_master)
431                 return error("HEAD is not a symbolic ref");
432         if (strncmp(git_refs_heads_master + pfxlen, "refs/heads/", 11))
433                 return error("HEAD points to something strange (%s)",
434                              git_refs_heads_master + pfxlen);
435         if (!memcmp(null_sha1, sha1, 20))
436                 return error("HEAD: not a valid git pointer");
437         return 0;
438 }
439
440 int main(int argc, char **argv)
441 {
442         int i, heads;
443
444         setup_git_directory();
445
446         for (i = 1; i < argc; i++) {
447                 const char *arg = argv[i];
448
449                 if (!strcmp(arg, "--unreachable")) {
450                         show_unreachable = 1;
451                         continue;
452                 }
453                 if (!strcmp(arg, "--tags")) {
454                         show_tags = 1;
455                         continue;
456                 }
457                 if (!strcmp(arg, "--root")) {
458                         show_root = 1;
459                         continue;
460                 }
461                 if (!strcmp(arg, "--cache")) {
462                         keep_cache_objects = 1;
463                         continue;
464                 }
465                 if (!strcmp(arg, "--full")) {
466                         check_full = 1;
467                         continue;
468                 }
469                 if (!strcmp(arg, "--strict")) {
470                         check_strict = 1;
471                         continue;
472                 }
473                 if (*arg == '-')
474                         usage("git-fsck-objects [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]");
475         }
476
477         fsck_head_link();
478         fsck_object_dir(get_object_directory());
479         if (check_full) {
480                 struct alternate_object_database *alt;
481                 struct packed_git *p;
482                 prepare_alt_odb();
483                 for (alt = alt_odb_list; alt; alt = alt->next) {
484                         char namebuf[PATH_MAX];
485                         int namelen = alt->name - alt->base;
486                         memcpy(namebuf, alt->base, namelen);
487                         namebuf[namelen - 1] = 0;
488                         fsck_object_dir(namebuf);
489                 }
490                 prepare_packed_git();
491                 for (p = packed_git; p; p = p->next)
492                         /* verify gives error messages itself */
493                         verify_pack(p, 0);
494
495                 for (p = packed_git; p; p = p->next) {
496                         int num = num_packed_objects(p);
497                         for (i = 0; i < num; i++) {
498                                 unsigned char sha1[20];
499                                 nth_packed_object_sha1(p, i, sha1);
500                                 fsck_sha1(sha1);
501                         }
502                 }
503         }
504
505         heads = 0;
506         for (i = 1; i < argc; i++) {
507                 const char *arg = argv[i]; 
508
509                 if (*arg == '-')
510                         continue;
511
512                 if (!get_sha1(arg, head_sha1)) {
513                         struct object *obj = lookup_object(head_sha1);
514
515                         /* Error is printed by lookup_object(). */
516                         if (!obj)
517                                 continue;
518
519                         obj->used = 1;
520                         mark_reachable(obj, REACHABLE);
521                         heads++;
522                         continue;
523                 }
524                 error("invalid parameter: expected sha1, got '%s'", arg);
525         }
526
527         /*
528          * If we've not been given any explicit head information, do the
529          * default ones from .git/refs. We also consider the index file
530          * in this case (ie this implies --cache).
531          */
532         if (!heads) {
533                 get_default_heads();
534                 keep_cache_objects = 1;
535         }
536
537         if (keep_cache_objects) {
538                 int i;
539                 read_cache();
540                 for (i = 0; i < active_nr; i++) {
541                         struct blob *blob = lookup_blob(active_cache[i]->sha1);
542                         struct object *obj;
543                         if (!blob)
544                                 continue;
545                         obj = &blob->object;
546                         obj->used = 1;
547                         mark_reachable(obj, REACHABLE);
548                 }
549         }
550
551         check_connectivity();
552         return 0;
553 }