tree.con commit Merge branch 'lt/gitlink' (afb5b6a)
   1#include "cache.h"
   2#include "tree.h"
   3#include "blob.h"
   4#include "commit.h"
   5#include "tag.h"
   6#include "tree-walk.h"
   7
   8const char *tree_type = "tree";
   9
  10static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
  11{
  12        int len;
  13        unsigned int size;
  14        struct cache_entry *ce;
  15
  16        if (S_ISDIR(mode))
  17                return READ_TREE_RECURSIVE;
  18
  19        len = strlen(pathname);
  20        size = cache_entry_size(baselen + len);
  21        ce = xcalloc(1, size);
  22
  23        ce->ce_mode = create_ce_mode(mode);
  24        ce->ce_flags = create_ce_flags(baselen + len, stage);
  25        memcpy(ce->name, base, baselen);
  26        memcpy(ce->name + baselen, pathname, len+1);
  27        hashcpy(ce->sha1, sha1);
  28        return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
  29}
  30
  31static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
  32{
  33        const char *match;
  34        int pathlen;
  35
  36        if (!paths)
  37                return 1;
  38        pathlen = strlen(path);
  39        while ((match = *paths++) != NULL) {
  40                int matchlen = strlen(match);
  41
  42                if (baselen >= matchlen) {
  43                        /* If it doesn't match, move along... */
  44                        if (strncmp(base, match, matchlen))
  45                                continue;
  46                        /* The base is a subdirectory of a path which was specified. */
  47                        return 1;
  48                }
  49
  50                /* Does the base match? */
  51                if (strncmp(base, match, baselen))
  52                        continue;
  53
  54                match += baselen;
  55                matchlen -= baselen;
  56
  57                if (pathlen > matchlen)
  58                        continue;
  59
  60                if (matchlen > pathlen) {
  61                        if (match[pathlen] != '/')
  62                                continue;
  63                        if (!S_ISDIR(mode))
  64                                continue;
  65                }
  66
  67                if (strncmp(path, match, pathlen))
  68                        continue;
  69
  70                return 1;
  71        }
  72        return 0;
  73}
  74
  75int read_tree_recursive(struct tree *tree,
  76                        const char *base, int baselen,
  77                        int stage, const char **match,
  78                        read_tree_fn_t fn)
  79{
  80        struct tree_desc desc;
  81        struct name_entry entry;
  82
  83        if (parse_tree(tree))
  84                return -1;
  85
  86        init_tree_desc(&desc, tree->buffer, tree->size);
  87
  88        while (tree_entry(&desc, &entry)) {
  89                if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
  90                        continue;
  91
  92                switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) {
  93                case 0:
  94                        continue;
  95                case READ_TREE_RECURSIVE:
  96                        break;;
  97                default:
  98                        return -1;
  99                }
 100                if (S_ISDIR(entry.mode)) {
 101                        int retval;
 102                        char *newbase;
 103                        unsigned int pathlen = tree_entry_len(entry.path, entry.sha1);
 104
 105                        newbase = xmalloc(baselen + 1 + pathlen);
 106                        memcpy(newbase, base, baselen);
 107                        memcpy(newbase + baselen, entry.path, pathlen);
 108                        newbase[baselen + pathlen] = '/';
 109                        retval = read_tree_recursive(lookup_tree(entry.sha1),
 110                                                     newbase,
 111                                                     baselen + pathlen + 1,
 112                                                     stage, match, fn);
 113                        free(newbase);
 114                        if (retval)
 115                                return -1;
 116                        continue;
 117                }
 118        }
 119        return 0;
 120}
 121
 122int read_tree(struct tree *tree, int stage, const char **match)
 123{
 124        return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
 125}
 126
 127struct tree *lookup_tree(const unsigned char *sha1)
 128{
 129        struct object *obj = lookup_object(sha1);
 130        if (!obj) {
 131                struct tree *ret = alloc_tree_node();
 132                created_object(sha1, &ret->object);
 133                ret->object.type = OBJ_TREE;
 134                return ret;
 135        }
 136        if (!obj->type)
 137                obj->type = OBJ_TREE;
 138        if (obj->type != OBJ_TREE) {
 139                error("Object %s is a %s, not a tree",
 140                      sha1_to_hex(sha1), typename(obj->type));
 141                return NULL;
 142        }
 143        return (struct tree *) obj;
 144}
 145
 146/*
 147 * NOTE! Tree refs to external git repositories
 148 * (ie gitlinks) do not count as real references.
 149 *
 150 * You don't have to have those repositories
 151 * available at all, much less have the objects
 152 * accessible from the current repository.
 153 */
 154static void track_tree_refs(struct tree *item)
 155{
 156        int n_refs = 0, i;
 157        struct object_refs *refs;
 158        struct tree_desc desc;
 159        struct name_entry entry;
 160
 161        /* Count how many entries there are.. */
 162        init_tree_desc(&desc, item->buffer, item->size);
 163        while (tree_entry(&desc, &entry)) {
 164                if (S_ISDIRLNK(entry.mode))
 165                        continue;
 166                n_refs++;
 167        }
 168
 169        /* Allocate object refs and walk it again.. */
 170        i = 0;
 171        refs = alloc_object_refs(n_refs);
 172        init_tree_desc(&desc, item->buffer, item->size);
 173        while (tree_entry(&desc, &entry)) {
 174                struct object *obj;
 175
 176                if (S_ISDIRLNK(entry.mode))
 177                        continue;
 178                if (S_ISDIR(entry.mode))
 179                        obj = &lookup_tree(entry.sha1)->object;
 180                else
 181                        obj = &lookup_blob(entry.sha1)->object;
 182                refs->ref[i++] = obj;
 183        }
 184        set_object_refs(&item->object, refs);
 185}
 186
 187int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
 188{
 189        if (item->object.parsed)
 190                return 0;
 191        item->object.parsed = 1;
 192        item->buffer = buffer;
 193        item->size = size;
 194
 195        if (track_object_refs)
 196                track_tree_refs(item);
 197        return 0;
 198}
 199
 200int parse_tree(struct tree *item)
 201{
 202         enum object_type type;
 203         void *buffer;
 204         unsigned long size;
 205
 206        if (item->object.parsed)
 207                return 0;
 208        buffer = read_sha1_file(item->object.sha1, &type, &size);
 209        if (!buffer)
 210                return error("Could not read %s",
 211                             sha1_to_hex(item->object.sha1));
 212        if (type != OBJ_TREE) {
 213                free(buffer);
 214                return error("Object %s not a tree",
 215                             sha1_to_hex(item->object.sha1));
 216        }
 217        return parse_tree_buffer(item, buffer, size);
 218}
 219
 220struct tree *parse_tree_indirect(const unsigned char *sha1)
 221{
 222        struct object *obj = parse_object(sha1);
 223        do {
 224                if (!obj)
 225                        return NULL;
 226                if (obj->type == OBJ_TREE)
 227                        return (struct tree *) obj;
 228                else if (obj->type == OBJ_COMMIT)
 229                        obj = &(((struct commit *) obj)->tree->object);
 230                else if (obj->type == OBJ_TAG)
 231                        obj = ((struct tag *) obj)->tagged;
 232                else
 233                        return NULL;
 234                if (!obj->parsed)
 235                        parse_object(obj->sha1);
 236        } while (1);
 237}