tree.con commit t0002: add test for enter_repo(), non-strict mode (3104120)
   1#include "cache.h"
   2#include "cache-tree.h"
   3#include "tree.h"
   4#include "blob.h"
   5#include "commit.h"
   6#include "tag.h"
   7#include "tree-walk.h"
   8
   9const char *tree_type = "tree";
  10
  11static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt)
  12{
  13        int len;
  14        unsigned int size;
  15        struct cache_entry *ce;
  16
  17        if (S_ISDIR(mode))
  18                return READ_TREE_RECURSIVE;
  19
  20        len = strlen(pathname);
  21        size = cache_entry_size(baselen + len);
  22        ce = xcalloc(1, size);
  23
  24        ce->ce_mode = create_ce_mode(mode);
  25        ce->ce_flags = create_ce_flags(stage);
  26        ce->ce_namelen = baselen + len;
  27        memcpy(ce->name, base, baselen);
  28        memcpy(ce->name + baselen, pathname, len+1);
  29        hashcpy(ce->sha1, sha1);
  30        return add_cache_entry(ce, opt);
  31}
  32
  33static int read_one_entry(const unsigned char *sha1, struct strbuf *base,
  34                          const char *pathname, unsigned mode, int stage,
  35                          void *context)
  36{
  37        return read_one_entry_opt(sha1, base->buf, base->len, pathname,
  38                                  mode, stage,
  39                                  ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
  40}
  41
  42/*
  43 * This is used when the caller knows there is no existing entries at
  44 * the stage that will conflict with the entry being added.
  45 */
  46static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base,
  47                                const char *pathname, unsigned mode, int stage,
  48                                void *context)
  49{
  50        return read_one_entry_opt(sha1, base->buf, base->len, pathname,
  51                                  mode, stage,
  52                                  ADD_CACHE_JUST_APPEND);
  53}
  54
  55static int read_tree_1(struct tree *tree, struct strbuf *base,
  56                       int stage, const struct pathspec *pathspec,
  57                       read_tree_fn_t fn, void *context)
  58{
  59        struct tree_desc desc;
  60        struct name_entry entry;
  61        unsigned char sha1[20];
  62        int len, oldlen = base->len;
  63        enum interesting retval = entry_not_interesting;
  64
  65        if (parse_tree(tree))
  66                return -1;
  67
  68        init_tree_desc(&desc, tree->buffer, tree->size);
  69
  70        while (tree_entry(&desc, &entry)) {
  71                if (retval != all_entries_interesting) {
  72                        retval = tree_entry_interesting(&entry, base, 0, pathspec);
  73                        if (retval == all_entries_not_interesting)
  74                                break;
  75                        if (retval == entry_not_interesting)
  76                                continue;
  77                }
  78
  79                switch (fn(entry.sha1, base,
  80                           entry.path, entry.mode, stage, context)) {
  81                case 0:
  82                        continue;
  83                case READ_TREE_RECURSIVE:
  84                        break;
  85                default:
  86                        return -1;
  87                }
  88
  89                if (S_ISDIR(entry.mode))
  90                        hashcpy(sha1, entry.sha1);
  91                else if (S_ISGITLINK(entry.mode)) {
  92                        struct commit *commit;
  93
  94                        commit = lookup_commit(entry.sha1);
  95                        if (!commit)
  96                                die("Commit %s in submodule path %s%s not found",
  97                                    sha1_to_hex(entry.sha1),
  98                                    base->buf, entry.path);
  99
 100                        if (parse_commit(commit))
 101                                die("Invalid commit %s in submodule path %s%s",
 102                                    sha1_to_hex(entry.sha1),
 103                                    base->buf, entry.path);
 104
 105                        hashcpy(sha1, commit->tree->object.sha1);
 106                }
 107                else
 108                        continue;
 109
 110                len = tree_entry_len(&entry);
 111                strbuf_add(base, entry.path, len);
 112                strbuf_addch(base, '/');
 113                retval = read_tree_1(lookup_tree(sha1),
 114                                     base, stage, pathspec,
 115                                     fn, context);
 116                strbuf_setlen(base, oldlen);
 117                if (retval)
 118                        return -1;
 119        }
 120        return 0;
 121}
 122
 123int read_tree_recursive(struct tree *tree,
 124                        const char *base, int baselen,
 125                        int stage, const struct pathspec *pathspec,
 126                        read_tree_fn_t fn, void *context)
 127{
 128        struct strbuf sb = STRBUF_INIT;
 129        int ret;
 130
 131        strbuf_add(&sb, base, baselen);
 132        ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
 133        strbuf_release(&sb);
 134        return ret;
 135}
 136
 137static int cmp_cache_name_compare(const void *a_, const void *b_)
 138{
 139        const struct cache_entry *ce1, *ce2;
 140
 141        ce1 = *((const struct cache_entry **)a_);
 142        ce2 = *((const struct cache_entry **)b_);
 143        return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
 144                                  ce2->name, ce2->ce_namelen, ce_stage(ce2));
 145}
 146
 147int read_tree(struct tree *tree, int stage, struct pathspec *match)
 148{
 149        read_tree_fn_t fn = NULL;
 150        int i, err;
 151
 152        /*
 153         * Currently the only existing callers of this function all
 154         * call it with stage=1 and after making sure there is nothing
 155         * at that stage; we could always use read_one_entry_quick().
 156         *
 157         * But when we decide to straighten out git-read-tree not to
 158         * use unpack_trees() in some cases, this will probably start
 159         * to matter.
 160         */
 161
 162        /*
 163         * See if we have cache entry at the stage.  If so,
 164         * do it the original slow way, otherwise, append and then
 165         * sort at the end.
 166         */
 167        for (i = 0; !fn && i < active_nr; i++) {
 168                const struct cache_entry *ce = active_cache[i];
 169                if (ce_stage(ce) == stage)
 170                        fn = read_one_entry;
 171        }
 172
 173        if (!fn)
 174                fn = read_one_entry_quick;
 175        err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
 176        if (fn == read_one_entry || err)
 177                return err;
 178
 179        /*
 180         * Sort the cache entry -- we need to nuke the cache tree, though.
 181         */
 182        cache_tree_free(&active_cache_tree);
 183        qsort(active_cache, active_nr, sizeof(active_cache[0]),
 184              cmp_cache_name_compare);
 185        return 0;
 186}
 187
 188struct tree *lookup_tree(const unsigned char *sha1)
 189{
 190        struct object *obj = lookup_object(sha1);
 191        if (!obj)
 192                return create_object(sha1, alloc_tree_node());
 193        return object_as_type(obj, OBJ_TREE, 0);
 194}
 195
 196int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
 197{
 198        if (item->object.parsed)
 199                return 0;
 200        item->object.parsed = 1;
 201        item->buffer = buffer;
 202        item->size = size;
 203
 204        return 0;
 205}
 206
 207int parse_tree_gently(struct tree *item, int quiet_on_missing)
 208{
 209         enum object_type type;
 210         void *buffer;
 211         unsigned long size;
 212
 213        if (item->object.parsed)
 214                return 0;
 215        buffer = read_sha1_file(item->object.sha1, &type, &size);
 216        if (!buffer)
 217                return quiet_on_missing ? -1 :
 218                        error("Could not read %s",
 219                             sha1_to_hex(item->object.sha1));
 220        if (type != OBJ_TREE) {
 221                free(buffer);
 222                return error("Object %s not a tree",
 223                             sha1_to_hex(item->object.sha1));
 224        }
 225        return parse_tree_buffer(item, buffer, size);
 226}
 227
 228void free_tree_buffer(struct tree *tree)
 229{
 230        free(tree->buffer);
 231        tree->buffer = NULL;
 232        tree->size = 0;
 233        tree->object.parsed = 0;
 234}
 235
 236struct tree *parse_tree_indirect(const unsigned char *sha1)
 237{
 238        struct object *obj = parse_object(sha1);
 239        do {
 240                if (!obj)
 241                        return NULL;
 242                if (obj->type == OBJ_TREE)
 243                        return (struct tree *) obj;
 244                else if (obj->type == OBJ_COMMIT)
 245                        obj = &(((struct commit *) obj)->tree->object);
 246                else if (obj->type == OBJ_TAG)
 247                        obj = ((struct tag *) obj)->tagged;
 248                else
 249                        return NULL;
 250                if (!obj->parsed)
 251                        parse_object(obj->sha1);
 252        } while (1);
 253}