fsck.con commit add generic, type aware object chain walker (355885d)
   1#include "cache.h"
   2#include "object.h"
   3#include "blob.h"
   4#include "tree.h"
   5#include "tree-walk.h"
   6#include "commit.h"
   7#include "tag.h"
   8#include "fsck.h"
   9
  10static int fsck_walk_tree(struct tree *tree, fsck_walk_func walk, void *data)
  11{
  12        struct tree_desc desc;
  13        struct name_entry entry;
  14        int res = 0;
  15
  16        if (parse_tree(tree))
  17                return -1;
  18
  19        init_tree_desc(&desc, tree->buffer, tree->size);
  20        while (tree_entry(&desc, &entry)) {
  21                int result;
  22
  23                if (S_ISGITLINK(entry.mode))
  24                        continue;
  25                if (S_ISDIR(entry.mode))
  26                        result = walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data);
  27                else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
  28                        result = walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data);
  29                else {
  30                        result = error("in tree %s: entry %s has bad mode %.6o\n",
  31                                        sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
  32                }
  33                if (result < 0)
  34                        return result;
  35                if (!res)
  36                        res = result;
  37        }
  38        return res;
  39}
  40
  41static int fsck_walk_commit(struct commit *commit, fsck_walk_func walk, void *data)
  42{
  43        struct commit_list *parents;
  44        int res;
  45        int result;
  46
  47        if (parse_commit(commit))
  48                return -1;
  49
  50        result = walk((struct object *)commit->tree, OBJ_TREE, data);
  51        if (result < 0)
  52                return result;
  53        res = result;
  54
  55        parents = commit->parents;
  56        while (parents) {
  57                result = walk((struct object *)parents->item, OBJ_COMMIT, data);
  58                if (result < 0)
  59                        return result;
  60                if (!res)
  61                        res = result;
  62                parents = parents->next;
  63        }
  64        return res;
  65}
  66
  67static int fsck_walk_tag(struct tag *tag, fsck_walk_func walk, void *data)
  68{
  69        if (parse_tag(tag))
  70                return -1;
  71        return walk(tag->tagged, OBJ_ANY, data);
  72}
  73
  74int fsck_walk(struct object *obj, fsck_walk_func walk, void *data)
  75{
  76        if (!obj)
  77                return -1;
  78        switch (obj->type) {
  79        case OBJ_BLOB:
  80                return 0;
  81        case OBJ_TREE:
  82                return fsck_walk_tree((struct tree *)obj, walk, data);
  83        case OBJ_COMMIT:
  84                return fsck_walk_commit((struct commit *)obj, walk, data);
  85        case OBJ_TAG:
  86                return fsck_walk_tag((struct tag *)obj, walk, data);
  87        default:
  88                error("Unknown object type for %s", sha1_to_hex(obj->sha1));
  89                return -1;
  90        }
  91}