blob: e17730e9da94053f39f1610e8cd81ab80bbf8a9f [file] [log] [blame]
Martin Koegler355885d2008-02-25 22:46:04 +01001#ifndef GIT_FSCK_H
2#define GIT_FSCK_H
3
Elijah Newrena64215b2023-02-24 00:09:30 +00004#include "object.h"
René Scharfe3b41fb02018-09-03 14:49:27 +00005#include "oidset.h"
6
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +02007enum fsck_msg_type {
Ævar Arnfjörð Bjarmason30cf6182021-03-28 15:15:41 +02008 /* for internal use only */
9 FSCK_IGNORE,
10 FSCK_INFO,
11 FSCK_FATAL,
12 /* "public", fed to e.g. error_func callbacks */
13 FSCK_ERROR,
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +020014 FSCK_WARN,
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +020015};
Martin Koeglerba002f32008-02-25 22:46:08 +010016
John Caif6534db2022-10-25 15:42:23 -070017/*
18 * Documentation/fsck-msgids.txt documents these; when
19 * modifying this list in any way, make sure to keep the
20 * two in sync.
21 */
22
Ævar Arnfjörð Bjarmason44e07da2021-03-28 15:15:45 +020023#define FOREACH_FSCK_MSG_ID(FUNC) \
24 /* fatal errors */ \
25 FUNC(NUL_IN_HEADER, FATAL) \
26 FUNC(UNTERMINATED_HEADER, FATAL) \
27 /* errors */ \
28 FUNC(BAD_DATE, ERROR) \
29 FUNC(BAD_DATE_OVERFLOW, ERROR) \
30 FUNC(BAD_EMAIL, ERROR) \
31 FUNC(BAD_NAME, ERROR) \
32 FUNC(BAD_OBJECT_SHA1, ERROR) \
33 FUNC(BAD_PARENT_SHA1, ERROR) \
Ævar Arnfjörð Bjarmason44e07da2021-03-28 15:15:45 +020034 FUNC(BAD_TIMEZONE, ERROR) \
35 FUNC(BAD_TREE, ERROR) \
36 FUNC(BAD_TREE_SHA1, ERROR) \
37 FUNC(BAD_TYPE, ERROR) \
38 FUNC(DUPLICATE_ENTRIES, ERROR) \
39 FUNC(MISSING_AUTHOR, ERROR) \
40 FUNC(MISSING_COMMITTER, ERROR) \
41 FUNC(MISSING_EMAIL, ERROR) \
42 FUNC(MISSING_NAME_BEFORE_EMAIL, ERROR) \
43 FUNC(MISSING_OBJECT, ERROR) \
44 FUNC(MISSING_SPACE_BEFORE_DATE, ERROR) \
45 FUNC(MISSING_SPACE_BEFORE_EMAIL, ERROR) \
46 FUNC(MISSING_TAG, ERROR) \
47 FUNC(MISSING_TAG_ENTRY, ERROR) \
48 FUNC(MISSING_TREE, ERROR) \
Ævar Arnfjörð Bjarmason44e07da2021-03-28 15:15:45 +020049 FUNC(MISSING_TYPE, ERROR) \
50 FUNC(MISSING_TYPE_ENTRY, ERROR) \
51 FUNC(MULTIPLE_AUTHORS, ERROR) \
52 FUNC(TREE_NOT_SORTED, ERROR) \
53 FUNC(UNKNOWN_TYPE, ERROR) \
54 FUNC(ZERO_PADDED_DATE, ERROR) \
55 FUNC(GITMODULES_MISSING, ERROR) \
56 FUNC(GITMODULES_BLOB, ERROR) \
57 FUNC(GITMODULES_LARGE, ERROR) \
58 FUNC(GITMODULES_NAME, ERROR) \
59 FUNC(GITMODULES_SYMLINK, ERROR) \
60 FUNC(GITMODULES_URL, ERROR) \
61 FUNC(GITMODULES_PATH, ERROR) \
62 FUNC(GITMODULES_UPDATE, ERROR) \
Patrick Steinhardt27ab4782022-12-01 15:46:09 +010063 FUNC(GITATTRIBUTES_MISSING, ERROR) \
64 FUNC(GITATTRIBUTES_LARGE, ERROR) \
65 FUNC(GITATTRIBUTES_LINE_LENGTH, ERROR) \
66 FUNC(GITATTRIBUTES_BLOB, ERROR) \
Ævar Arnfjörð Bjarmason44e07da2021-03-28 15:15:45 +020067 /* warnings */ \
Ævar Arnfjörð Bjarmason44e07da2021-03-28 15:15:45 +020068 FUNC(EMPTY_NAME, WARN) \
69 FUNC(FULL_PATHNAME, WARN) \
70 FUNC(HAS_DOT, WARN) \
71 FUNC(HAS_DOTDOT, WARN) \
72 FUNC(HAS_DOTGIT, WARN) \
73 FUNC(NULL_SHA1, WARN) \
74 FUNC(ZERO_PADDED_FILEMODE, WARN) \
75 FUNC(NUL_IN_COMMIT, WARN) \
76 /* infos (reported as warnings, but ignored by default) */ \
Jeff King4dd3b042022-08-10 17:04:07 -040077 FUNC(BAD_FILEMODE, INFO) \
Ævar Arnfjörð Bjarmason44e07da2021-03-28 15:15:45 +020078 FUNC(GITMODULES_PARSE, INFO) \
Jeff Kingbb6832d2021-05-03 16:43:25 -040079 FUNC(GITIGNORE_SYMLINK, INFO) \
80 FUNC(GITATTRIBUTES_SYMLINK, INFO) \
81 FUNC(MAILMAP_SYMLINK, INFO) \
Ævar Arnfjörð Bjarmason44e07da2021-03-28 15:15:45 +020082 FUNC(BAD_TAG_NAME, INFO) \
83 FUNC(MISSING_TAGGER_ENTRY, INFO) \
84 /* ignored (elevated when requested) */ \
85 FUNC(EXTRA_HEADER_ENTRY, IGNORE)
86
87#define MSG_ID(id, msg_type) FSCK_MSG_##id,
88enum fsck_msg_id {
89 FOREACH_FSCK_MSG_ID(MSG_ID)
90 FSCK_MSG_MAX
91};
92#undef MSG_ID
93
Johannes Schindelin22410542015-06-22 17:25:00 +020094struct fsck_options;
Elijah Newrenef3ca952018-08-15 10:54:05 -070095struct object;
Johannes Schindelin22410542015-06-22 17:25:00 +020096
Ævar Arnfjörð Bjarmason53692df2021-03-28 15:15:47 +020097void fsck_set_msg_type_from_ids(struct fsck_options *options,
98 enum fsck_msg_id msg_id,
99 enum fsck_msg_type msg_type);
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200100void fsck_set_msg_type(struct fsck_options *options,
Ævar Arnfjörð Bjarmasonf1abc2d2021-03-28 15:15:36 +0200101 const char *msg_id, const char *msg_type);
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200102void fsck_set_msg_types(struct fsck_options *options, const char *values);
Johannes Schindelin5d477a32015-06-22 17:25:31 +0200103int is_valid_msg_type(const char *msg_id, const char *msg_type);
Johannes Schindelin0282f4d2015-06-22 17:25:25 +0200104
Martin Koegler355885d2008-02-25 22:46:04 +0100105/*
106 * callback function for fsck_walk
107 * type is the expected type of the object or OBJ_ANY
108 * the return value is:
109 * 0 everything OK
110 * <0 error signaled and abort
111 * >0 error signaled and do not abort
112 */
Ævar Arnfjörð Bjarmasona1aad712021-03-28 15:15:35 +0200113typedef int (*fsck_walk_func)(struct object *obj, enum object_type object_type,
114 void *data, struct fsck_options *options);
Martin Koegler355885d2008-02-25 22:46:04 +0100115
Martin Koeglerba002f32008-02-25 22:46:08 +0100116/* callback for fsck_object, type is FSCK_ERROR or FSCK_WARN */
Johannes Schindelin1cd772c2016-07-17 12:59:57 +0200117typedef int (*fsck_error)(struct fsck_options *o,
Jeff King5afc4b12019-10-18 00:58:40 -0400118 const struct object_id *oid, enum object_type object_type,
Ævar Arnfjörð Bjarmason394d5d32021-03-28 15:15:46 +0200119 enum fsck_msg_type msg_type, enum fsck_msg_id msg_id,
120 const char *message);
Martin Koeglerba002f32008-02-25 22:46:08 +0100121
Johannes Schindelin1cd772c2016-07-17 12:59:57 +0200122int fsck_error_function(struct fsck_options *o,
Jeff King5afc4b12019-10-18 00:58:40 -0400123 const struct object_id *oid, enum object_type object_type,
Ævar Arnfjörð Bjarmason394d5d32021-03-28 15:15:46 +0200124 enum fsck_msg_type msg_type, enum fsck_msg_id msg_id,
125 const char *message);
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +0200126int fsck_error_cb_print_missing_gitmodules(struct fsck_options *o,
127 const struct object_id *oid,
128 enum object_type object_type,
129 enum fsck_msg_type msg_type,
130 enum fsck_msg_id msg_id,
131 const char *message);
Martin Koeglerd6ffc8d2008-02-25 22:46:09 +0100132
Johannes Schindelin22410542015-06-22 17:25:00 +0200133struct fsck_options {
134 fsck_walk_func walk;
135 fsck_error error_func;
136 unsigned strict:1;
Ævar Arnfjörð Bjarmason1b32b592021-03-28 15:15:40 +0200137 enum fsck_msg_type *msg_type;
René Scharfe3b41fb02018-09-03 14:49:27 +0000138 struct oidset skiplist;
Ævar Arnfjörð Bjarmasonc15087d2021-03-28 15:15:48 +0200139 struct oidset gitmodules_found;
140 struct oidset gitmodules_done;
Patrick Steinhardt27ab4782022-12-01 15:46:09 +0100141 struct oidset gitattributes_found;
142 struct oidset gitattributes_done;
Jeff King73390292019-10-18 00:57:37 -0400143 kh_oid_map_t *object_names;
Johannes Schindelin22410542015-06-22 17:25:00 +0200144};
145
Ævar Arnfjörð Bjarmasond3857842021-03-28 15:15:34 +0200146#define FSCK_OPTIONS_DEFAULT { \
147 .skiplist = OIDSET_INIT, \
Ævar Arnfjörð Bjarmasonc15087d2021-03-28 15:15:48 +0200148 .gitmodules_found = OIDSET_INIT, \
149 .gitmodules_done = OIDSET_INIT, \
Patrick Steinhardt27ab4782022-12-01 15:46:09 +0100150 .gitattributes_found = OIDSET_INIT, \
151 .gitattributes_done = OIDSET_INIT, \
Ævar Arnfjörð Bjarmasond3857842021-03-28 15:15:34 +0200152 .error_func = fsck_error_function \
153}
154#define FSCK_OPTIONS_STRICT { \
155 .strict = 1, \
Ævar Arnfjörð Bjarmasonc15087d2021-03-28 15:15:48 +0200156 .gitmodules_found = OIDSET_INIT, \
157 .gitmodules_done = OIDSET_INIT, \
Patrick Steinhardt27ab4782022-12-01 15:46:09 +0100158 .gitattributes_found = OIDSET_INIT, \
159 .gitattributes_done = OIDSET_INIT, \
Ævar Arnfjörð Bjarmasond3857842021-03-28 15:15:34 +0200160 .error_func = fsck_error_function, \
161}
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +0200162#define FSCK_OPTIONS_MISSING_GITMODULES { \
163 .strict = 1, \
164 .gitmodules_found = OIDSET_INIT, \
165 .gitmodules_done = OIDSET_INIT, \
Patrick Steinhardt27ab4782022-12-01 15:46:09 +0100166 .gitattributes_found = OIDSET_INIT, \
167 .gitattributes_done = OIDSET_INIT, \
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +0200168 .error_func = fsck_error_cb_print_missing_gitmodules, \
169}
Johannes Schindelin22410542015-06-22 17:25:00 +0200170
Martin Koegler355885d2008-02-25 22:46:04 +0100171/* descend in all linked child objects
172 * the return value is:
173 * -1 error in processing the object
174 * <0 return value of the callback, which lead to an abort
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100175 * >0 return value of the first signaled error >0 (in the case of no other errors)
Martin Koegler355885d2008-02-25 22:46:04 +0100176 * 0 everything OK
177 */
Johannes Schindelin22410542015-06-22 17:25:00 +0200178int fsck_walk(struct object *obj, void *data, struct fsck_options *options);
Jeff King23a173a2019-10-18 00:54:12 -0400179
180/*
181 * Blob objects my pass a NULL data pointer, which indicates they are too large
182 * to fit in memory. All other types must pass a real buffer.
183 */
Johannes Schindelin90a398b2014-09-10 15:52:51 +0200184int fsck_object(struct object *obj, void *data, unsigned long size,
Johannes Schindelin22410542015-06-22 17:25:00 +0200185 struct fsck_options *options);
Martin Koegler355885d2008-02-25 22:46:04 +0100186
Jeff King159e7b02018-05-02 17:20:08 -0400187/*
Jeff King35ff3272023-01-18 15:43:53 -0500188 * Same as fsck_object(), but for when the caller doesn't have an object
189 * struct.
190 */
191int fsck_buffer(const struct object_id *oid, enum object_type,
192 void *data, unsigned long size,
193 struct fsck_options *options);
194
195/*
Ævar Arnfjörð Bjarmasonacf9de42021-01-05 20:42:46 +0100196 * fsck a tag, and pass info about it back to the caller. This is
197 * exposed fsck_object() internals for git-mktag(1).
198 */
199int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
200 unsigned long size, struct fsck_options *options,
201 struct object_id *tagged_oid,
202 int *tag_type);
203
204/*
Jeff King159e7b02018-05-02 17:20:08 -0400205 * Some fsck checks are context-dependent, and may end up queued; run this
206 * after completing all fsck_object() calls in order to resolve any remaining
207 * checks.
208 */
209int fsck_finish(struct fsck_options *options);
210
Jeff Kinga59cfb32019-10-18 00:56:13 -0400211/*
212 * Subsystem for storing human-readable names for each object.
213 *
214 * If fsck_enable_object_names() has not been called, all other functions are
215 * noops.
216 *
217 * Use fsck_put_object_name() to seed initial names (e.g. from refnames); the
218 * fsck code will extend that while walking trees, etc.
219 *
220 * Use fsck_get_object_name() to get a single name (or NULL if none). Or the
221 * more convenient describe_object(), which always produces an output string
222 * with the oid combined with the name (if any). Note that the return value
223 * points to a rotating array of static buffers, and may be invalidated by a
224 * subsequent call.
225 */
226void fsck_enable_object_names(struct fsck_options *options);
227const char *fsck_get_object_name(struct fsck_options *options,
Jeff King73390292019-10-18 00:57:37 -0400228 const struct object_id *oid);
Jeff Kinga59cfb32019-10-18 00:56:13 -0400229__attribute__((format (printf,3,4)))
Jeff King73390292019-10-18 00:57:37 -0400230void fsck_put_object_name(struct fsck_options *options,
231 const struct object_id *oid,
Jeff Kinga59cfb32019-10-18 00:56:13 -0400232 const char *fmt, ...);
233const char *fsck_describe_object(struct fsck_options *options,
Jeff King73390292019-10-18 00:57:37 -0400234 const struct object_id *oid);
Jeff Kinga59cfb32019-10-18 00:56:13 -0400235
Ævar Arnfjörð Bjarmason1f3299f2021-01-05 20:42:47 +0100236/*
237 * git_config() callback for use by fsck-y tools that want to support
238 * fsck.<msg> fsck.skipList etc.
239 */
Ævar Arnfjörð Bjarmasonfb79f5b2021-03-17 19:20:36 +0100240int git_fsck_config(const char *var, const char *value, void *cb);
Ævar Arnfjörð Bjarmason1f3299f2021-01-05 20:42:47 +0100241
Martin Koegler355885d2008-02-25 22:46:04 +0100242#endif