object.con commit autoconf: Checks for libraries (ebdf532)
   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 **objs;
   9static int nr_objs, obj_allocs;
  10
  11unsigned int get_max_object_index(void)
  12{
  13        return obj_allocs;
  14}
  15
  16struct object *get_indexed_object(unsigned int idx)
  17{
  18        return objs[idx];
  19}
  20
  21const char *type_names[] = {
  22        "none", "blob", "tree", "commit", "bad"
  23};
  24
  25static int hashtable_index(const unsigned char *sha1)
  26{
  27        unsigned int i;
  28        memcpy(&i, sha1, sizeof(unsigned int));
  29        return (int)(i % obj_allocs);
  30}
  31
  32static int find_object(const unsigned char *sha1)
  33{
  34        int i;
  35
  36        if (!objs)
  37                return -1;
  38
  39        i = hashtable_index(sha1);
  40        while (objs[i]) {
  41                if (memcmp(sha1, objs[i]->sha1, 20) == 0)
  42                        return i;
  43                i++;
  44                if (i == obj_allocs)
  45                        i = 0;
  46        }
  47        return -1 - i;
  48}
  49
  50struct object *lookup_object(const unsigned char *sha1)
  51{
  52        int pos = find_object(sha1);
  53        if (pos >= 0)
  54                return objs[pos];
  55        return NULL;
  56}
  57
  58void created_object(const unsigned char *sha1, struct object *obj)
  59{
  60        int pos;
  61
  62        obj->parsed = 0;
  63        memcpy(obj->sha1, sha1, 20);
  64        obj->type = TYPE_NONE;
  65        obj->used = 0;
  66
  67        if (obj_allocs - 1 <= nr_objs * 2) {
  68                int i, count = obj_allocs;
  69                obj_allocs = (obj_allocs < 32 ? 32 : 2 * obj_allocs);
  70                objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
  71                memset(objs + count, 0, (obj_allocs - count)
  72                                * sizeof(struct object *));
  73                for (i = 0; i < obj_allocs; i++)
  74                        if (objs[i]) {
  75                                int j = find_object(objs[i]->sha1);
  76                                if (j != i) {
  77                                        j = -1 - j;
  78                                        objs[j] = objs[i];
  79                                        objs[i] = NULL;
  80                                }
  81                        }
  82        }
  83
  84        pos = find_object(sha1);
  85        if (pos >= 0)
  86                die("Inserting %s twice\n", sha1_to_hex(sha1));
  87        pos = -pos-1;
  88
  89        objs[pos] = obj;
  90        nr_objs++;
  91}
  92
  93struct object *lookup_object_type(const unsigned char *sha1, const char *type)
  94{
  95        if (!type) {
  96                return lookup_unknown_object(sha1);
  97        } else if (!strcmp(type, blob_type)) {
  98                return &lookup_blob(sha1)->object;
  99        } else if (!strcmp(type, tree_type)) {
 100                return &lookup_tree(sha1)->object;
 101        } else if (!strcmp(type, commit_type)) {
 102                return &lookup_commit(sha1)->object;
 103        } else if (!strcmp(type, tag_type)) {
 104                return &lookup_tag(sha1)->object;
 105        } else {
 106                error("Unknown type %s", type);
 107                return NULL;
 108        }
 109}
 110
 111union any_object {
 112        struct object object;
 113        struct commit commit;
 114        struct tree tree;
 115        struct blob blob;
 116        struct tag tag;
 117};
 118
 119struct object *lookup_unknown_object(const unsigned char *sha1)
 120{
 121        struct object *obj = lookup_object(sha1);
 122        if (!obj) {
 123                union any_object *ret = xcalloc(1, sizeof(*ret));
 124                created_object(sha1, &ret->object);
 125                ret->object.type = TYPE_NONE;
 126                return &ret->object;
 127        }
 128        return obj;
 129}
 130
 131struct object *parse_object(const unsigned char *sha1)
 132{
 133        unsigned long size;
 134        char type[20];
 135        void *buffer = read_sha1_file(sha1, type, &size);
 136        if (buffer) {
 137                struct object *obj;
 138                if (check_sha1_signature(sha1, buffer, size, type) < 0)
 139                        printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
 140                if (!strcmp(type, blob_type)) {
 141                        struct blob *blob = lookup_blob(sha1);
 142                        parse_blob_buffer(blob, buffer, size);
 143                        obj = &blob->object;
 144                } else if (!strcmp(type, tree_type)) {
 145                        struct tree *tree = lookup_tree(sha1);
 146                        obj = &tree->object;
 147                        if (!tree->object.parsed) {
 148                                parse_tree_buffer(tree, buffer, size);
 149                                buffer = NULL;
 150                        }
 151                } else if (!strcmp(type, commit_type)) {
 152                        struct commit *commit = lookup_commit(sha1);
 153                        parse_commit_buffer(commit, buffer, size);
 154                        if (!commit->buffer) {
 155                                commit->buffer = buffer;
 156                                buffer = NULL;
 157                        }
 158                        obj = &commit->object;
 159                } else if (!strcmp(type, tag_type)) {
 160                        struct tag *tag = lookup_tag(sha1);
 161                        parse_tag_buffer(tag, buffer, size);
 162                        obj = &tag->object;
 163                } else {
 164                        obj = NULL;
 165                }
 166                free(buffer);
 167                return obj;
 168        }
 169        return NULL;
 170}
 171
 172struct object_list *object_list_insert(struct object *item,
 173                                       struct object_list **list_p)
 174{
 175        struct object_list *new_list = xmalloc(sizeof(struct object_list));
 176        new_list->item = item;
 177        new_list->next = *list_p;
 178        *list_p = new_list;
 179        return new_list;
 180}
 181
 182void object_list_append(struct object *item,
 183                        struct object_list **list_p)
 184{
 185        while (*list_p) {
 186                list_p = &((*list_p)->next);
 187        }
 188        *list_p = xmalloc(sizeof(struct object_list));
 189        (*list_p)->next = NULL;
 190        (*list_p)->item = item;
 191}
 192
 193unsigned object_list_length(struct object_list *list)
 194{
 195        unsigned ret = 0;
 196        while (list) {
 197                list = list->next;
 198                ret++;
 199        }
 200        return ret;
 201}
 202
 203int object_list_contains(struct object_list *list, struct object *obj)
 204{
 205        while (list) {
 206                if (list->item == obj)
 207                        return 1;
 208                list = list->next;
 209        }
 210        return 0;
 211}
 212
 213void add_object_array(struct object *obj, const char *name, struct object_array *array)
 214{
 215        unsigned nr = array->nr;
 216        unsigned alloc = array->alloc;
 217        struct object_array_entry *objects = array->objects;
 218
 219        if (nr >= alloc) {
 220                alloc = (alloc + 32) * 2;
 221                objects = xrealloc(objects, alloc * sizeof(*objects));
 222                array->alloc = alloc;
 223                array->objects = objects;
 224        }
 225        objects[nr].item = obj;
 226        objects[nr].name = name;
 227        array->nr = ++nr;
 228}