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