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