tag.c: use 'ref-filter' APIs
[gitweb.git] / fsck.c
diff --git a/fsck.c b/fsck.c
index 7a614bba821174416be4117e53d4588caa2cb26c..e41e753d6dcbb577148260a1392210d50fb0b325 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -8,6 +8,7 @@
 #include "fsck.h"
 #include "refs.h"
 #include "utf8.h"
+#include "sha1-array.h"
 
 #define FSCK_FATAL -1
 #define FSCK_INFO -2
@@ -127,6 +128,43 @@ 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"))
@@ -191,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);
 
@@ -229,6 +275,10 @@ static int report(struct fsck_options *options, struct object *object,
        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)
@@ -477,8 +527,8 @@ static int fsck_tree(struct tree *item, struct fsck_options *options)
        return retval;
 }
 
-static int require_end_of_header(const void *data, unsigned long size,
-       struct object *obj, struct fsck_options *options)
+static int verify_headers(const void *data, unsigned long size,
+                         struct object *obj, struct fsck_options *options)
 {
        const char *buffer = (const char *)data;
        unsigned long i;
@@ -495,6 +545,15 @@ static int require_end_of_header(const void *data, unsigned long size,
                }
        }
 
+       /*
+        * We did not find double-LF that separates the header
+        * and the body.  Not having a body is not a crime but
+        * we do want to see the terminating LF for the last header
+        * line.
+        */
+       if (size && buffer[size - 1] == '\n')
+               return 0;
+
        return report(options, obj,
                FSCK_MSG_UNTERMINATED_HEADER, "unterminated header");
 }
@@ -551,7 +610,7 @@ static int fsck_commit_buffer(struct commit *commit, const char *buffer,
        unsigned parent_count, parent_line_count = 0, author_count;
        int err;
 
-       if (require_end_of_header(buffer, size, &commit->object, options))
+       if (verify_headers(buffer, size, &commit->object, options))
                return -1;
 
        if (!skip_prefix(buffer, "tree ", &buffer))
@@ -652,7 +711,7 @@ static int fsck_tag_buffer(struct tag *tag, const char *data,
                }
        }
 
-       if (require_end_of_header(buffer, size, &tag->object, options))
+       if (verify_headers(buffer, size, &tag->object, options))
                goto done;
 
        if (!skip_prefix(buffer, "object ", &buffer)) {