]> git.ipfire.org Git - thirdparty/git.git/commitdiff
tree-walk: add a mechanism for getting non-canonicalized modes
authorJeff King <peff@peff.net>
Wed, 10 Aug 2022 21:01:17 +0000 (17:01 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 10 Aug 2022 21:26:25 +0000 (14:26 -0700)
When using init_tree_desc() and tree_entry() to iterate over a tree, we
always canonicalize the modes coming out of the tree. This is a good
thing to prevent bugs or oddities in normal code paths, but it's
counter-productive for tools like fsck that want to see the exact
contents.

We can address this by adding an option to avoid the extra
canonicalization. A few notes on the implementation:

  - I've attached the new option to the tree_desc struct itself. The
    actual code change is in decode_tree_entry(), which is in turn
    called by the public update_tree_entry(), tree_entry(), and
    init_tree_desc() functions, plus their "gently" counterparts.

    By letting it ride along in the struct, we can avoid changing the
    signature of those functions, which are called many times. Plus it's
    conceptually simpler: you really want a particular iteration of a
    tree to be "raw" or not, rather than individual calls.

  - We still have to set the new option somewhere. The struct is
    initialized by init_tree_desc(). I added the new flags field only to
    the "gently" version. That avoids disturbing the much more numerous
    non-gentle callers, and it makes sense that anybody being careful
    about looking at raw modes would also be careful about bogus trees
    (i.e., the caller will be something like fsck in the first place).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
fsck.c
packfile.c
tree-walk.c
tree-walk.h

diff --git a/fsck.c b/fsck.c
index dd4822ba1be7fe2de830613757110e6d55bfb566..5acc982a7c784fcc9c4c10b66d30fd9f8071eb1a 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -308,7 +308,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 +578,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, 0)) {
                retval += report(options, tree_oid, OBJ_TREE,
                                 FSCK_MSG_BAD_TREE,
                                 "cannot be parsed as a tree");
index 8e812a84a3c3395de964bae782d9fe74ae169737..e5b1d0ed7670a72a664ea72dbd5d37e49f15b46e 100644 (file)
@@ -2243,7 +2243,7 @@ static int add_promisor_object(const struct object_id *oid,
                struct tree *tree = (struct tree *)obj;
                struct tree_desc desc;
                struct name_entry entry;
-               if (init_tree_desc_gently(&desc, tree->buffer, tree->size))
+               if (init_tree_desc_gently(&desc, tree->buffer, tree->size, 0))
                        /*
                         * Error messages are given when packs are
                         * verified, so do not print any here.
index 506234b4b8138894d6516fc80e9fc42753f78f00..74f4d710e8f0f28410fae49a6b9c31bf1045e0ca 100644 (file)
@@ -47,17 +47,20 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned l
 
        /* Initialize the descriptor entry */
        desc->entry.path = path;
-       desc->entry.mode = canon_mode(mode);
+       desc->entry.mode = (desc->flags & TREE_DESC_RAW_MODES) ? mode : canon_mode(mode);
        desc->entry.pathlen = len - 1;
        oidread(&desc->entry.oid, (const unsigned char *)path + len);
 
        return 0;
 }
 
-static int init_tree_desc_internal(struct tree_desc *desc, const void *buffer, unsigned long size, struct strbuf *err)
+static int init_tree_desc_internal(struct tree_desc *desc, const void *buffer,
+                                  unsigned long size, struct strbuf *err,
+                                  enum tree_desc_flags flags)
 {
        desc->buffer = buffer;
        desc->size = size;
+       desc->flags = flags;
        if (size)
                return decode_tree_entry(desc, buffer, size, err);
        return 0;
@@ -66,15 +69,16 @@ static int init_tree_desc_internal(struct tree_desc *desc, const void *buffer, u
 void init_tree_desc(struct tree_desc *desc, const void *buffer, unsigned long size)
 {
        struct strbuf err = STRBUF_INIT;
-       if (init_tree_desc_internal(desc, buffer, size, &err))
+       if (init_tree_desc_internal(desc, buffer, size, &err, 0))
                die("%s", err.buf);
        strbuf_release(&err);
 }
 
-int init_tree_desc_gently(struct tree_desc *desc, const void *buffer, unsigned long size)
+int init_tree_desc_gently(struct tree_desc *desc, const void *buffer, unsigned long size,
+                         enum tree_desc_flags flags)
 {
        struct strbuf err = STRBUF_INIT;
-       int result = init_tree_desc_internal(desc, buffer, size, &err);
+       int result = init_tree_desc_internal(desc, buffer, size, &err, flags);
        if (result)
                error("%s", err.buf);
        strbuf_release(&err);
index a5058469e9b3a83fdd58a04f2af72690742d08bd..6305d531503f25cd0b2632914d9aa7ddea55ff8e 100644 (file)
@@ -34,6 +34,11 @@ struct tree_desc {
 
        /* counts the number of bytes left in the `buffer`. */
        unsigned int size;
+
+       /* option flags passed via init_tree_desc_gently() */
+       enum tree_desc_flags {
+               TREE_DESC_RAW_MODES = (1 << 0),
+       } flags;
 };
 
 /**
@@ -79,7 +84,8 @@ int update_tree_entry_gently(struct tree_desc *);
  */
 void init_tree_desc(struct tree_desc *desc, const void *buf, unsigned long size);
 
-int init_tree_desc_gently(struct tree_desc *desc, const void *buf, unsigned long size);
+int init_tree_desc_gently(struct tree_desc *desc, const void *buf, unsigned long size,
+                         enum tree_desc_flags flags);
 
 /*
  * Visit the next entry in a tree. Returns 1 when there are more entries