44e5e49948da52c9dcd54d68595ba61c238f77a3
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
10#define REACHABLE 0x0001
11
12static int show_root = 0;
13static int show_tags = 0;
14static int show_unreachable = 0;
15static unsigned char head_sha1[20];
16
17static void check_connectivity(void)
18{
19 int i;
20
21 /* Look up all the requirements, warn about missing objects.. */
22 for (i = 0; i < nr_objs; i++) {
23 struct object *obj = objs[i];
24 struct object_list *refs;
25
26 if (show_unreachable && !(obj->flags & REACHABLE)) {
27 printf("unreachable %s %s\n", obj->type, sha1_to_hex(obj->sha1));
28 continue;
29 }
30
31 if (!obj->parsed) {
32 printf("missing %s %s\n", obj->type,
33 sha1_to_hex(obj->sha1));
34 }
35 if (!obj->used) {
36 printf("dangling %s %s\n", obj->type,
37 sha1_to_hex(obj->sha1));
38 }
39 for (refs = obj->refs; refs; refs = refs->next) {
40 if (!refs->item->parsed) {
41 printf("broken link from %s\n",
42 sha1_to_hex(obj->sha1));
43 printf(" to %s\n",
44 sha1_to_hex(refs->item->sha1));
45 }
46 }
47 }
48}
49
50static int fsck_tree(struct tree *item)
51{
52 if (item->has_full_path) {
53 fprintf(stderr, "warning: fsck-cache: tree %s "
54 "has full pathnames in it\n",
55 sha1_to_hex(item->object.sha1));
56 }
57 return 0;
58}
59
60static int fsck_commit(struct commit *commit)
61{
62 if (!commit->tree)
63 return -1;
64 if (!commit->parents && show_root)
65 printf("root %s\n", sha1_to_hex(commit->object.sha1));
66 if (!commit->date)
67 printf("bad commit date in %s\n",
68 sha1_to_hex(commit->object.sha1));
69 return 0;
70}
71
72static int fsck_tag(struct tag *tag)
73{
74 if (!show_tags)
75 return 0;
76
77 printf("tagged %s %s",
78 tag->tagged->type,
79 sha1_to_hex(tag->tagged->sha1));
80 printf(" (%s) in %s\n",
81 tag->tag, sha1_to_hex(tag->object.sha1));
82 return 0;
83}
84
85static int fsck_name(char *hex)
86{
87 unsigned char sha1[20];
88 if (!get_sha1_hex(hex, sha1)) {
89 struct object *obj = parse_object(sha1);
90 if (!obj)
91 return -1;
92 if (obj->type == blob_type)
93 return 0;
94 if (obj->type == tree_type)
95 return fsck_tree((struct tree *) obj);
96 if (obj->type == commit_type)
97 return fsck_commit((struct commit *) obj);
98 if (obj->type == tag_type)
99 return fsck_tag((struct tag *) obj);
100 }
101 return -1;
102}
103
104static int fsck_dir(int i, char *path)
105{
106 DIR *dir = opendir(path);
107 struct dirent *de;
108
109 if (!dir) {
110 return error("missing sha1 directory '%s'", path);
111 }
112
113 while ((de = readdir(dir)) != NULL) {
114 char name[100];
115 int len = strlen(de->d_name);
116
117 switch (len) {
118 case 2:
119 if (de->d_name[1] != '.')
120 break;
121 case 1:
122 if (de->d_name[0] != '.')
123 break;
124 continue;
125 case 38:
126 sprintf(name, "%02x", i);
127 memcpy(name+2, de->d_name, len+1);
128 if (!fsck_name(name))
129 continue;
130 }
131 fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
132 }
133 closedir(dir);
134 return 0;
135}
136
137int main(int argc, char **argv)
138{
139 int i, heads;
140 char *sha1_dir;
141
142 for (i = 1; i < argc; i++) {
143 const char *arg = argv[i];
144
145 if (!strcmp(arg, "--unreachable")) {
146 show_unreachable = 1;
147 continue;
148 }
149 if (!strcmp(arg, "--tags")) {
150 show_tags = 1;
151 continue;
152 }
153 if (!strcmp(arg, "--root")) {
154 show_root = 1;
155 continue;
156 }
157 if (*arg == '-')
158 usage("fsck-cache [--tags] [[--unreachable] <head-sha1>*]");
159 }
160
161 sha1_dir = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;
162 for (i = 0; i < 256; i++) {
163 static char dir[4096];
164 sprintf(dir, "%s/%02x", sha1_dir, i);
165 fsck_dir(i, dir);
166 }
167
168 heads = 0;
169 for (i = 1; i < argc; i++) {
170 const char *arg = argv[i];
171
172 if (*arg == '-')
173 continue;
174
175 if (!get_sha1_hex(arg, head_sha1)) {
176 struct commit *commit = lookup_commit(head_sha1);
177 struct object *obj;
178
179 /* Error is printed by lookup_commit(). */
180 if (!commit)
181 continue;
182
183 obj = &commit->object;
184 obj->used = 1;
185 mark_reachable(obj, REACHABLE);
186 heads++;
187 continue;
188 }
189 error("expected sha1, got %s", arg);
190 }
191
192 if (!heads) {
193 if (show_unreachable) {
194 fprintf(stderr, "unable to do reachability without a head\n");
195 show_unreachable = 0;
196 }
197 fprintf(stderr, "expect dangling commits - potential heads - due to lack of head information\n");
198 }
199
200 check_connectivity();
201 return 0;
202}