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