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