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