object.con commit git-svn: Added 'find-rev' command (26e6016)
   1#include "cache.h"
   2#include "object.h"
   3#include "blob.h"
   4#include "tree.h"
   5#include "commit.h"
   6#include "tag.h"
   7
   8static struct object **obj_hash;
   9static int nr_objs, obj_hash_size;
  10
  11unsigned int get_max_object_index(void)
  12{
  13        return obj_hash_size;
  14}
  15
  16struct object *get_indexed_object(unsigned int idx)
  17{
  18        return obj_hash[idx];
  19}
  20
  21static const char *object_type_strings[] = {
  22        NULL,           /* OBJ_NONE = 0 */
  23        "commit",       /* OBJ_COMMIT = 1 */
  24        "tree",         /* OBJ_TREE = 2 */
  25        "blob",         /* OBJ_BLOB = 3 */
  26        "tag",          /* OBJ_TAG = 4 */
  27};
  28
  29const char *typename(unsigned int type)
  30{
  31        if (type >= ARRAY_SIZE(object_type_strings))
  32                return NULL;
  33        return object_type_strings[type];
  34}
  35
  36int type_from_string(const char *str)
  37{
  38        int i;
  39
  40        for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
  41                if (!strcmp(str, object_type_strings[i]))
  42                        return i;
  43        die("invalid object type \"%s\"", str);
  44}
  45
  46static unsigned int hash_obj(struct object *obj, unsigned int n)
  47{
  48        unsigned int hash = *(unsigned int *)obj->sha1;
  49        return hash % n;
  50}
  51
  52static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
  53{
  54        int j = hash_obj(obj, size);
  55
  56        while (hash[j]) {
  57                j++;
  58                if (j >= size)
  59                        j = 0;
  60        }
  61        hash[j] = obj;
  62}
  63
  64static int hashtable_index(const unsigned char *sha1)
  65{
  66        unsigned int i;
  67        memcpy(&i, sha1, sizeof(unsigned int));
  68        return (int)(i % obj_hash_size);
  69}
  70
  71struct object *lookup_object(const unsigned char *sha1)
  72{
  73        int i;
  74        struct object *obj;
  75
  76        if (!obj_hash)
  77                return NULL;
  78
  79        i = hashtable_index(sha1);
  80        while ((obj = obj_hash[i]) != NULL) {
  81                if (!hashcmp(sha1, obj->sha1))
  82                        break;
  83                i++;
  84                if (i == obj_hash_size)
  85                        i = 0;
  86        }
  87        return obj;
  88}
  89
  90static void grow_object_hash(void)
  91{
  92        int i;
  93        int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
  94        struct object **new_hash;
  95
  96        new_hash = xcalloc(new_hash_size, sizeof(struct object *));
  97        for (i = 0; i < obj_hash_size; i++) {
  98                struct object *obj = obj_hash[i];
  99                if (!obj)
 100                        continue;
 101                insert_obj_hash(obj, new_hash, new_hash_size);
 102        }
 103        free(obj_hash);
 104        obj_hash = new_hash;
 105        obj_hash_size = new_hash_size;
 106}
 107
 108void created_object(const unsigned char *sha1, struct object *obj)
 109{
 110        obj->parsed = 0;
 111        obj->used = 0;
 112        obj->type = OBJ_NONE;
 113        obj->flags = 0;
 114        hashcpy(obj->sha1, sha1);
 115
 116        if (obj_hash_size - 1 <= nr_objs * 2)
 117                grow_object_hash();
 118
 119        insert_obj_hash(obj, obj_hash, obj_hash_size);
 120        nr_objs++;
 121}
 122
 123union any_object {
 124        struct object object;
 125        struct commit commit;
 126        struct tree tree;
 127        struct blob blob;
 128        struct tag tag;
 129};
 130
 131struct object *lookup_unknown_object(const unsigned char *sha1)
 132{
 133        struct object *obj = lookup_object(sha1);
 134        if (!obj) {
 135                union any_object *ret = xcalloc(1, sizeof(*ret));
 136                created_object(sha1, &ret->object);
 137                ret->object.type = OBJ_NONE;
 138                return &ret->object;
 139        }
 140        return obj;
 141}
 142
 143struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
 144{
 145        struct object *obj;
 146        int eaten = 0;
 147
 148        if (type == OBJ_BLOB) {
 149                struct blob *blob = lookup_blob(sha1);
 150                parse_blob_buffer(blob, buffer, size);
 151                obj = &blob->object;
 152        } else if (type == OBJ_TREE) {
 153                struct tree *tree = lookup_tree(sha1);
 154                obj = &tree->object;
 155                if (!tree->object.parsed) {
 156                        parse_tree_buffer(tree, buffer, size);
 157                        eaten = 1;
 158                }
 159        } else if (type == OBJ_COMMIT) {
 160                struct commit *commit = lookup_commit(sha1);
 161                parse_commit_buffer(commit, buffer, size);
 162                if (!commit->buffer) {
 163                        commit->buffer = buffer;
 164                        eaten = 1;
 165                }
 166                obj = &commit->object;
 167        } else if (type == OBJ_TAG) {
 168                struct tag *tag = lookup_tag(sha1);
 169                parse_tag_buffer(tag, buffer, size);
 170                obj = &tag->object;
 171        } else {
 172                obj = NULL;
 173        }
 174        *eaten_p = eaten;
 175        return obj;
 176}
 177
 178struct object *parse_object(const unsigned char *sha1)
 179{
 180        unsigned long size;
 181        enum object_type type;
 182        int eaten;
 183        void *buffer = read_sha1_file(sha1, &type, &size);
 184
 185        if (buffer) {
 186                struct object *obj;
 187                if (check_sha1_signature(sha1, buffer, size, typename(type)) < 0) {
 188                        error("sha1 mismatch %s\n", sha1_to_hex(sha1));
 189                        return NULL;
 190                }
 191
 192                obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
 193                if (!eaten)
 194                        free(buffer);
 195                return obj;
 196        }
 197        return NULL;
 198}
 199
 200struct object_list *object_list_insert(struct object *item,
 201                                       struct object_list **list_p)
 202{
 203        struct object_list *new_list = xmalloc(sizeof(struct object_list));
 204        new_list->item = item;
 205        new_list->next = *list_p;
 206        *list_p = new_list;
 207        return new_list;
 208}
 209
 210void object_list_append(struct object *item,
 211                        struct object_list **list_p)
 212{
 213        while (*list_p) {
 214                list_p = &((*list_p)->next);
 215        }
 216        *list_p = xmalloc(sizeof(struct object_list));
 217        (*list_p)->next = NULL;
 218        (*list_p)->item = item;
 219}
 220
 221unsigned object_list_length(struct object_list *list)
 222{
 223        unsigned ret = 0;
 224        while (list) {
 225                list = list->next;
 226                ret++;
 227        }
 228        return ret;
 229}
 230
 231int object_list_contains(struct object_list *list, struct object *obj)
 232{
 233        while (list) {
 234                if (list->item == obj)
 235                        return 1;
 236                list = list->next;
 237        }
 238        return 0;
 239}
 240
 241void add_object_array(struct object *obj, const char *name, struct object_array *array)
 242{
 243        unsigned nr = array->nr;
 244        unsigned alloc = array->alloc;
 245        struct object_array_entry *objects = array->objects;
 246
 247        if (nr >= alloc) {
 248                alloc = (alloc + 32) * 2;
 249                objects = xrealloc(objects, alloc * sizeof(*objects));
 250                array->alloc = alloc;
 251                array->objects = objects;
 252        }
 253        objects[nr].item = obj;
 254        objects[nr].name = name;
 255        array->nr = ++nr;
 256}