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