]> git.ipfire.org Git - thirdparty/git.git/blobdiff - fsck.c
Merge branch 'maint-2.36' into maint-2.37
[thirdparty/git.git] / fsck.c
diff --git a/fsck.c b/fsck.c
index ddcb2d264cd9574834523c957c9654a8981c7325..47eaeedd7076ba60a621e072abb405127b3c33fe 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -2,6 +2,7 @@
 #include "object-store.h"
 #include "repository.h"
 #include "object.h"
+#include "attr.h"
 #include "blob.h"
 #include "tree.h"
 #include "tree-walk.h"
@@ -308,7 +309,7 @@ static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *op
                return -1;
 
        name = fsck_get_object_name(options, &tree->object.oid);
-       if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
+       if (init_tree_desc_gently(&desc, tree->buffer, tree->size, 0))
                return -1;
        while (tree_entry_gently(&desc, &entry)) {
                struct object *obj;
@@ -578,7 +579,7 @@ static int fsck_tree(const struct object_id *tree_oid,
        const char *o_name;
        struct name_stack df_dup_candidates = { NULL };
 
-       if (init_tree_desc_gently(&desc, buffer, size)) {
+       if (init_tree_desc_gently(&desc, buffer, size, TREE_DESC_RAW_MODES)) {
                retval += report(options, tree_oid, OBJ_TREE,
                                 FSCK_MSG_BAD_TREE,
                                 "cannot be parsed as a tree");
@@ -614,17 +615,22 @@ static int fsck_tree(const struct object_id *tree_oid,
                                                 ".gitmodules is a symbolic link");
                }
 
+               if (is_hfs_dotgitattributes(name) || is_ntfs_dotgitattributes(name)) {
+                       if (!S_ISLNK(mode))
+                               oidset_insert(&options->gitattributes_found,
+                                             entry_oid);
+                       else
+                               retval += report(options, tree_oid, OBJ_TREE,
+                                                FSCK_MSG_GITATTRIBUTES_SYMLINK,
+                                                ".gitattributes is a symlink");
+               }
+
                if (S_ISLNK(mode)) {
                        if (is_hfs_dotgitignore(name) ||
                            is_ntfs_dotgitignore(name))
                                retval += report(options, tree_oid, OBJ_TREE,
                                                 FSCK_MSG_GITIGNORE_SYMLINK,
                                                 ".gitignore is a symlink");
-                       if (is_hfs_dotgitattributes(name) ||
-                           is_ntfs_dotgitattributes(name))
-                               retval += report(options, tree_oid, OBJ_TREE,
-                                                FSCK_MSG_GITATTRIBUTES_SYMLINK,
-                                                ".gitattributes is a symlink");
                        if (is_hfs_dotmailmap(name) ||
                            is_ntfs_dotmailmap(name))
                                retval += report(options, tree_oid, OBJ_TREE,
@@ -975,27 +981,16 @@ done:
        return ret;
 }
 
-/*
- * Like builtin/submodule--helper.c's starts_with_dot_slash, but without
- * relying on the platform-dependent is_dir_sep helper.
- *
- * This is for use in checking whether a submodule URL is interpreted as
- * relative to the current directory on any platform, since \ is a
- * directory separator on Windows but not on other platforms.
- */
-static int starts_with_dot_slash(const char *str)
+static int starts_with_dot_slash(const char *const path)
 {
-       return str[0] == '.' && (str[1] == '/' || str[1] == '\\');
+       return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
+                               PATH_MATCH_XPLATFORM);
 }
 
-/*
- * Like starts_with_dot_slash, this is a variant of submodule--helper's
- * helper of the same name with the twist that it accepts backslash as a
- * directory separator even on non-Windows platforms.
- */
-static int starts_with_dot_dot_slash(const char *str)
+static int starts_with_dot_dot_slash(const char *const path)
 {
-       return str[0] == '.' && starts_with_dot_slash(str + 1);
+       return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
+                               PATH_MATCH_XPLATFORM);
 }
 
 static int submodule_url_is_relative(const char *url)
@@ -1204,6 +1199,35 @@ static int fsck_blob(const struct object_id *oid, const char *buf,
                ret |= data.ret;
        }
 
+       if (oidset_contains(&options->gitattributes_found, oid)) {
+               const char *ptr;
+
+               oidset_insert(&options->gitattributes_done, oid);
+
+               if (!buf || size > ATTR_MAX_FILE_SIZE) {
+                       /*
+                        * A missing buffer here is a sign that the caller found the
+                        * blob too gigantic to load into memory. Let's just consider
+                        * that an error.
+                        */
+                       return report(options, oid, OBJ_BLOB,
+                                       FSCK_MSG_GITATTRIBUTES_LARGE,
+                                       ".gitattributes too large to parse");
+               }
+
+               for (ptr = buf; *ptr; ) {
+                       const char *eol = strchrnul(ptr, '\n');
+                       if (eol - ptr >= ATTR_MAX_LINE_LENGTH) {
+                               ret |= report(options, oid, OBJ_BLOB,
+                                             FSCK_MSG_GITATTRIBUTES_LINE_LENGTH,
+                                             ".gitattributes has too long lines to parse");
+                               break;
+                       }
+
+                       ptr = *eol ? eol + 1 : eol;
+               }
+       }
+
        return ret;
 }
 
@@ -1243,19 +1267,21 @@ int fsck_error_function(struct fsck_options *o,
        return 1;
 }
 
-int fsck_finish(struct fsck_options *options)
+static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done,
+                     enum fsck_msg_id msg_missing, enum fsck_msg_id msg_type,
+                     struct fsck_options *options, const char *blob_type)
 {
        int ret = 0;
        struct oidset_iter iter;
        const struct object_id *oid;
 
-       oidset_iter_init(&options->gitmodules_found, &iter);
+       oidset_iter_init(blobs_found, &iter);
        while ((oid = oidset_iter_next(&iter))) {
                enum object_type type;
                unsigned long size;
                char *buf;
 
-               if (oidset_contains(&options->gitmodules_done, oid))
+               if (oidset_contains(blobs_done, oid))
                        continue;
 
                buf = read_object_file(oid, &type, &size);
@@ -1263,25 +1289,36 @@ int fsck_finish(struct fsck_options *options)
                        if (is_promisor_object(oid))
                                continue;
                        ret |= report(options,
-                                     oid, OBJ_BLOB,
-                                     FSCK_MSG_GITMODULES_MISSING,
-                                     "unable to read .gitmodules blob");
+                                     oid, OBJ_BLOB, msg_missing,
+                                     "unable to read %s blob", blob_type);
                        continue;
                }
 
                if (type == OBJ_BLOB)
                        ret |= fsck_blob(oid, buf, size, options);
                else
-                       ret |= report(options,
-                                     oid, type,
-                                     FSCK_MSG_GITMODULES_BLOB,
-                                     "non-blob found at .gitmodules");
+                       ret |= report(options, oid, type, msg_type,
+                                     "non-blob found at %s", blob_type);
                free(buf);
        }
 
+       oidset_clear(blobs_found);
+       oidset_clear(blobs_done);
+
+       return ret;
+}
+
+int fsck_finish(struct fsck_options *options)
+{
+       int ret = 0;
+
+       ret |= fsck_blobs(&options->gitmodules_found, &options->gitmodules_done,
+                         FSCK_MSG_GITMODULES_MISSING, FSCK_MSG_GITMODULES_BLOB,
+                         options, ".gitmodules");
+       ret |= fsck_blobs(&options->gitattributes_found, &options->gitattributes_done,
+                         FSCK_MSG_GITATTRIBUTES_MISSING, FSCK_MSG_GITATTRIBUTES_BLOB,
+                         options, ".gitattributes");
 
-       oidset_clear(&options->gitmodules_found);
-       oidset_clear(&options->gitmodules_done);
        return ret;
 }