tree.con commit Remove unused "zeropad" entry from tree_list_entry (3bc1eca)
   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#include <stdlib.h>
   8
   9const char *tree_type = "tree";
  10
  11static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
  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        memcpy(ce->sha1, sha1, 20);
  29        return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
  30}
  31
  32static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
  33{
  34        const char *match;
  35        int pathlen;
  36
  37        if (!paths)
  38                return 1;
  39        pathlen = strlen(path);
  40        while ((match = *paths++) != NULL) {
  41                int matchlen = strlen(match);
  42
  43                if (baselen >= matchlen) {
  44                        /* If it doesn't match, move along... */
  45                        if (strncmp(base, match, matchlen))
  46                                continue;
  47                        /* The base is a subdirectory of a path which was specified. */
  48                        return 1;
  49                }
  50
  51                /* Does the base match? */
  52                if (strncmp(base, match, baselen))
  53                        continue;
  54
  55                match += baselen;
  56                matchlen -= baselen;
  57
  58                if (pathlen > matchlen)
  59                        continue;
  60
  61                if (matchlen > pathlen) {
  62                        if (match[pathlen] != '/')
  63                                continue;
  64                        if (!S_ISDIR(mode))
  65                                continue;
  66                }
  67
  68                if (strncmp(path, match, pathlen))
  69                        continue;
  70
  71                return 1;
  72        }
  73        return 0;
  74}
  75
  76int read_tree_recursive(struct tree *tree,
  77                        const char *base, int baselen,
  78                        int stage, const char **match,
  79                        read_tree_fn_t fn)
  80{
  81        struct tree_desc desc;
  82
  83        if (parse_tree(tree))
  84                return -1;
  85
  86        desc.buf = tree->buffer;
  87        desc.size = tree->size;
  88
  89        while (desc.size) {
  90                unsigned mode;
  91                const char *name;
  92                const unsigned char *sha1;
  93
  94                sha1 = tree_entry_extract(&desc, &name, &mode);
  95                update_tree_entry(&desc);
  96
  97                if (!match_tree_entry(base, baselen, name, mode, match))
  98                        continue;
  99
 100                switch (fn(sha1, base, baselen, name, mode, stage)) {
 101                case 0:
 102                        continue;
 103                case READ_TREE_RECURSIVE:
 104                        break;;
 105                default:
 106                        return -1;
 107                }
 108                if (S_ISDIR(mode)) {
 109                        int retval;
 110                        int pathlen = strlen(name);
 111                        char *newbase;
 112
 113                        newbase = xmalloc(baselen + 1 + pathlen);
 114                        memcpy(newbase, base, baselen);
 115                        memcpy(newbase + baselen, name, pathlen);
 116                        newbase[baselen + pathlen] = '/';
 117                        retval = read_tree_recursive(lookup_tree(sha1),
 118                                                     newbase,
 119                                                     baselen + pathlen + 1,
 120                                                     stage, match, fn);
 121                        free(newbase);
 122                        if (retval)
 123                                return -1;
 124                        continue;
 125                }
 126        }
 127        return 0;
 128}
 129
 130int read_tree(struct tree *tree, int stage, const char **match)
 131{
 132        return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
 133}
 134
 135struct tree *lookup_tree(const unsigned char *sha1)
 136{
 137        struct object *obj = lookup_object(sha1);
 138        if (!obj) {
 139                struct tree *ret = xcalloc(1, sizeof(struct tree));
 140                created_object(sha1, &ret->object);
 141                ret->object.type = tree_type;
 142                return ret;
 143        }
 144        if (!obj->type)
 145                obj->type = tree_type;
 146        if (obj->type != tree_type) {
 147                error("Object %s is a %s, not a tree", 
 148                      sha1_to_hex(sha1), obj->type);
 149                return NULL;
 150        }
 151        return (struct tree *) obj;
 152}
 153
 154static int track_tree_refs(struct tree *item)
 155{
 156        int n_refs = 0, i;
 157        struct object_refs *refs;
 158        struct tree_desc desc;
 159
 160        /* Count how many entries there are.. */
 161        desc.buf = item->buffer;
 162        desc.size = item->size;
 163        while (desc.size) {
 164                n_refs++;
 165                update_tree_entry(&desc);
 166        }
 167
 168        /* Allocate object refs and walk it again.. */
 169        i = 0;
 170        refs = alloc_object_refs(n_refs);
 171        desc.buf = item->buffer;
 172        desc.size = item->size;
 173        while (desc.size) {
 174                unsigned mode;
 175                const char *name;
 176                const unsigned char *sha1;
 177                struct object *obj;
 178
 179                sha1 = tree_entry_extract(&desc, &name, &mode);
 180                update_tree_entry(&desc);
 181                if (S_ISDIR(mode))
 182                        obj = &lookup_tree(sha1)->object;
 183                else
 184                        obj = &lookup_blob(sha1)->object;
 185                refs->ref[i++] = obj;
 186        }
 187        set_object_refs(&item->object, refs);
 188        return 0;
 189}
 190
 191int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
 192{
 193        if (item->object.parsed)
 194                return 0;
 195        item->object.parsed = 1;
 196        item->buffer = buffer;
 197        item->size = size;
 198
 199        if (track_object_refs)
 200                track_tree_refs(item);
 201        return 0;
 202}
 203
 204struct tree_entry_list *create_tree_entry_list(struct tree *tree)
 205{
 206        struct tree_desc desc;
 207        struct tree_entry_list *ret = NULL;
 208        struct tree_entry_list **list_p = &ret;
 209
 210        desc.buf = tree->buffer;
 211        desc.size = tree->size;
 212
 213        while (desc.size) {
 214                unsigned mode;
 215                const char *path;
 216                const unsigned char *sha1;
 217                struct tree_entry_list *entry;
 218
 219                sha1 = tree_entry_extract(&desc, &path, &mode);
 220                update_tree_entry(&desc);
 221
 222                entry = xmalloc(sizeof(struct tree_entry_list));
 223                entry->name = path;
 224                entry->sha1 = sha1;
 225                entry->mode = mode;
 226                entry->directory = S_ISDIR(mode) != 0;
 227                entry->executable = (mode & S_IXUSR) != 0;
 228                entry->symlink = S_ISLNK(mode) != 0;
 229                entry->next = NULL;
 230
 231                *list_p = entry;
 232                list_p = &entry->next;
 233        }
 234        return ret;
 235}
 236
 237void free_tree_entry_list(struct tree_entry_list *list)
 238{
 239        while (list) {
 240                struct tree_entry_list *next = list->next;
 241                free(list);
 242                list = next;
 243        }
 244}
 245
 246int parse_tree(struct tree *item)
 247{
 248         char type[20];
 249         void *buffer;
 250         unsigned long size;
 251
 252        if (item->object.parsed)
 253                return 0;
 254        buffer = read_sha1_file(item->object.sha1, type, &size);
 255        if (!buffer)
 256                return error("Could not read %s",
 257                             sha1_to_hex(item->object.sha1));
 258        if (strcmp(type, tree_type)) {
 259                free(buffer);
 260                return error("Object %s not a tree",
 261                             sha1_to_hex(item->object.sha1));
 262        }
 263        return parse_tree_buffer(item, buffer, size);
 264}
 265
 266struct tree *parse_tree_indirect(const unsigned char *sha1)
 267{
 268        struct object *obj = parse_object(sha1);
 269        do {
 270                if (!obj)
 271                        return NULL;
 272                if (obj->type == tree_type)
 273                        return (struct tree *) obj;
 274                else if (obj->type == commit_type)
 275                        obj = &(((struct commit *) obj)->tree->object);
 276                else if (obj->type == tag_type)
 277                        obj = ((struct tag *) obj)->tagged;
 278                else
 279                        return NULL;
 280                if (!obj->parsed)
 281                        parse_object(obj->sha1);
 282        } while (1);
 283}