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