fsck: git receive-pack: support excluding objects from fsck'ing
[gitweb.git] / fsck.c
diff --git a/fsck.c b/fsck.c
index 6635e15b1028ed55ce5eabf62a90a69dc38c1c1b..1523243f1b1ad110025fc71e70f18a6a0796b5cc 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -8,8 +8,15 @@
 #include "fsck.h"
 #include "refs.h"
 #include "utf8.h"
+#include "sha1-array.h"
+
+#define FSCK_FATAL -1
+#define FSCK_INFO -2
 
 #define FOREACH_MSG_ID(FUNC) \
+       /* fatal errors */ \
+       FUNC(NUL_IN_HEADER, FATAL) \
+       FUNC(UNTERMINATED_HEADER, FATAL) \
        /* errors */ \
        FUNC(BAD_DATE, ERROR) \
        FUNC(BAD_DATE_OVERFLOW, ERROR) \
        FUNC(MISSING_TYPE, ERROR) \
        FUNC(MISSING_TYPE_ENTRY, ERROR) \
        FUNC(MULTIPLE_AUTHORS, ERROR) \
-       FUNC(NUL_IN_HEADER, ERROR) \
        FUNC(TAG_OBJECT_NOT_TAG, ERROR) \
        FUNC(TREE_NOT_SORTED, ERROR) \
        FUNC(UNKNOWN_TYPE, ERROR) \
-       FUNC(UNTERMINATED_HEADER, ERROR) \
        FUNC(ZERO_PADDED_DATE, ERROR) \
        /* warnings */ \
        FUNC(BAD_FILEMODE, WARN) \
-       FUNC(BAD_TAG_NAME, WARN) \
        FUNC(EMPTY_NAME, WARN) \
        FUNC(FULL_PATHNAME, WARN) \
        FUNC(HAS_DOT, WARN) \
        FUNC(HAS_DOTDOT, WARN) \
        FUNC(HAS_DOTGIT, WARN) \
-       FUNC(MISSING_TAGGER_ENTRY, WARN) \
        FUNC(NULL_SHA1, WARN) \
-       FUNC(ZERO_PADDED_FILEMODE, WARN)
+       FUNC(ZERO_PADDED_FILEMODE, WARN) \
+       /* infos (reported as warnings, but ignored by default) */ \
+       FUNC(BAD_TAG_NAME, INFO) \
+       FUNC(MISSING_TAGGER_ENTRY, INFO)
 
 #define MSG_ID(id, msg_type) FSCK_MSG_##id,
 enum fsck_msg_id {
@@ -122,12 +128,51 @@ static int fsck_msg_type(enum fsck_msg_id msg_id,
        return msg_type;
 }
 
+static void init_skiplist(struct fsck_options *options, const char *path)
+{
+       static struct sha1_array skiplist = SHA1_ARRAY_INIT;
+       int sorted, fd;
+       char buffer[41];
+       unsigned char sha1[20];
+
+       if (options->skiplist)
+               sorted = options->skiplist->sorted;
+       else {
+               sorted = 1;
+               options->skiplist = &skiplist;
+       }
+
+       fd = open(path, O_RDONLY);
+       if (fd < 0)
+               die("Could not open skip list: %s", path);
+       for (;;) {
+               int result = read_in_full(fd, buffer, sizeof(buffer));
+               if (result < 0)
+                       die_errno("Could not read '%s'", path);
+               if (!result)
+                       break;
+               if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n')
+                       die("Invalid SHA-1: %s", buffer);
+               sha1_array_append(&skiplist, sha1);
+               if (sorted && skiplist.nr > 1 &&
+                               hashcmp(skiplist.sha1[skiplist.nr - 2],
+                                       sha1) > 0)
+                       sorted = 0;
+       }
+       close(fd);
+
+       if (sorted)
+               skiplist.sorted = 1;
+}
+
 static int parse_msg_type(const char *str)
 {
        if (!strcmp(str, "error"))
                return FSCK_ERROR;
        else if (!strcmp(str, "warn"))
                return FSCK_WARN;
+       else if (!strcmp(str, "ignore"))
+               return FSCK_IGNORE;
        else
                die("Unknown fsck message type: '%s'", str);
 }
@@ -149,6 +194,9 @@ void fsck_set_msg_type(struct fsck_options *options,
                die("Unhandled message id: %s", msg_id);
        type = parse_msg_type(msg_type);
 
+       if (type != FSCK_ERROR && msg_id_info[id].msg_type == FSCK_FATAL)
+               die("Cannot demote %s to %s", msg_id, msg_type);
+
        if (!options->msg_type) {
                int i;
                int *msg_type = xmalloc(sizeof(int) * FSCK_MSG_MAX);
@@ -181,6 +229,14 @@ void fsck_set_msg_types(struct fsck_options *options, const char *values)
                        buf[equal] = tolower(buf[equal]);
                buf[equal] = '\0';
 
+               if (!strcmp(buf, "skiplist")) {
+                       if (equal == len)
+                               die("skiplist requires a path");
+                       init_skiplist(options, buf + equal + 1);
+                       buf += len + 1;
+                       continue;
+               }
+
                if (equal == len)
                        die("Missing '=': '%s'", buf);
 
@@ -216,6 +272,18 @@ static int report(struct fsck_options *options, struct object *object,
        struct strbuf sb = STRBUF_INIT;
        int msg_type = fsck_msg_type(id, options), result;
 
+       if (msg_type == FSCK_IGNORE)
+               return 0;
+
+       if (options->skiplist && object &&
+                       sha1_array_lookup(options->skiplist, object->sha1) >= 0)
+               return 0;
+
+       if (msg_type == FSCK_FATAL)
+               msg_type = FSCK_ERROR;
+       else if (msg_type == FSCK_INFO)
+               msg_type = FSCK_WARN;
+
        append_msg_id(&sb, msg_id_info[id].id_string);
 
        va_start(ap, fmt);
@@ -673,15 +741,21 @@ static int fsck_tag_buffer(struct tag *tag, const char *data,
                goto done;
        }
        strbuf_addf(&sb, "refs/tags/%.*s", (int)(eol - buffer), buffer);
-       if (check_refname_format(sb.buf, 0))
-               report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME,
+       if (check_refname_format(sb.buf, 0)) {
+               ret = report(options, &tag->object, FSCK_MSG_BAD_TAG_NAME,
                           "invalid 'tag' name: %.*s",
                           (int)(eol - buffer), buffer);
+               if (ret)
+                       goto done;
+       }
        buffer = eol + 1;
 
-       if (!skip_prefix(buffer, "tagger ", &buffer))
+       if (!skip_prefix(buffer, "tagger ", &buffer)) {
                /* early tags do not contain 'tagger' lines; warn only */
-               report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
+               ret = report(options, &tag->object, FSCK_MSG_MISSING_TAGGER_ENTRY, "invalid format - expected 'tagger' line");
+               if (ret)
+                       goto done;
+       }
        else
                ret = fsck_ident(&buffer, &tag->object, options);