object.con commit contrib: contacts: add ability to parse from committish (8e7c4a8)
   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;
  49        memcpy(&hash, obj->sha1, sizeof(unsigned int));
  50        return hash % n;
  51}
  52
  53static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
  54{
  55        unsigned int j = hash_obj(obj, size);
  56
  57        while (hash[j]) {
  58                j++;
  59                if (j >= size)
  60                        j = 0;
  61        }
  62        hash[j] = obj;
  63}
  64
  65static unsigned int hashtable_index(const unsigned char *sha1)
  66{
  67        unsigned int i;
  68        memcpy(&i, sha1, sizeof(unsigned int));
  69        return i % obj_hash_size;
  70}
  71
  72struct object *lookup_object(const unsigned char *sha1)
  73{
  74        unsigned int i, first;
  75        struct object *obj;
  76
  77        if (!obj_hash)
  78                return NULL;
  79
  80        first = i = hashtable_index(sha1);
  81        while ((obj = obj_hash[i]) != NULL) {
  82                if (!hashcmp(sha1, obj->sha1))
  83                        break;
  84                i++;
  85                if (i == obj_hash_size)
  86                        i = 0;
  87        }
  88        if (obj && i != first) {
  89                /*
  90                 * Move object to where we started to look for it so
  91                 * that we do not need to walk the hash table the next
  92                 * time we look for it.
  93                 */
  94                struct object *tmp = obj_hash[i];
  95                obj_hash[i] = obj_hash[first];
  96                obj_hash[first] = tmp;
  97        }
  98        return obj;
  99}
 100
 101static void grow_object_hash(void)
 102{
 103        int i;
 104        int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
 105        struct object **new_hash;
 106
 107        new_hash = xcalloc(new_hash_size, sizeof(struct object *));
 108        for (i = 0; i < obj_hash_size; i++) {
 109                struct object *obj = obj_hash[i];
 110                if (!obj)
 111                        continue;
 112                insert_obj_hash(obj, new_hash, new_hash_size);
 113        }
 114        free(obj_hash);
 115        obj_hash = new_hash;
 116        obj_hash_size = new_hash_size;
 117}
 118
 119void *create_object(const unsigned char *sha1, int type, void *o)
 120{
 121        struct object *obj = o;
 122
 123        obj->parsed = 0;
 124        obj->used = 0;
 125        obj->type = type;
 126        obj->flags = 0;
 127        hashcpy(obj->sha1, sha1);
 128
 129        if (obj_hash_size - 1 <= nr_objs * 2)
 130                grow_object_hash();
 131
 132        insert_obj_hash(obj, obj_hash, obj_hash_size);
 133        nr_objs++;
 134        return obj;
 135}
 136
 137struct object *lookup_unknown_object(const unsigned char *sha1)
 138{
 139        struct object *obj = lookup_object(sha1);
 140        if (!obj)
 141                obj = create_object(sha1, OBJ_NONE, alloc_object_node());
 142        return obj;
 143}
 144
 145struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
 146{
 147        struct object *obj;
 148        int eaten = 0;
 149
 150        obj = NULL;
 151        if (type == OBJ_BLOB) {
 152                struct blob *blob = lookup_blob(sha1);
 153                if (blob) {
 154                        if (parse_blob_buffer(blob, buffer, size))
 155                                return NULL;
 156                        obj = &blob->object;
 157                }
 158        } else if (type == OBJ_TREE) {
 159                struct tree *tree = lookup_tree(sha1);
 160                if (tree) {
 161                        obj = &tree->object;
 162                        if (!tree->buffer)
 163                                tree->object.parsed = 0;
 164                        if (!tree->object.parsed) {
 165                                if (parse_tree_buffer(tree, buffer, size))
 166                                        return NULL;
 167                                eaten = 1;
 168                        }
 169                }
 170        } else if (type == OBJ_COMMIT) {
 171                struct commit *commit = lookup_commit(sha1);
 172                if (commit) {
 173                        if (parse_commit_buffer(commit, buffer, size))
 174                                return NULL;
 175                        if (!commit->buffer) {
 176                                commit->buffer = buffer;
 177                                eaten = 1;
 178                        }
 179                        obj = &commit->object;
 180                }
 181        } else if (type == OBJ_TAG) {
 182                struct tag *tag = lookup_tag(sha1);
 183                if (tag) {
 184                        if (parse_tag_buffer(tag, buffer, size))
 185                               return NULL;
 186                        obj = &tag->object;
 187                }
 188        } else {
 189                warning("object %s has unknown type id %d", sha1_to_hex(sha1), type);
 190                obj = NULL;
 191        }
 192        if (obj && obj->type == OBJ_NONE)
 193                obj->type = type;
 194        *eaten_p = eaten;
 195        return obj;
 196}
 197
 198struct object *parse_object_or_die(const unsigned char *sha1,
 199                                   const char *name)
 200{
 201        struct object *o = parse_object(sha1);
 202        if (o)
 203                return o;
 204
 205        die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1));
 206}
 207
 208struct object *parse_object(const unsigned char *sha1)
 209{
 210        unsigned long size;
 211        enum object_type type;
 212        int eaten;
 213        const unsigned char *repl = lookup_replace_object(sha1);
 214        void *buffer;
 215        struct object *obj;
 216
 217        obj = lookup_object(sha1);
 218        if (obj && obj->parsed)
 219                return obj;
 220
 221        if ((obj && obj->type == OBJ_BLOB) ||
 222            (!obj && has_sha1_file(sha1) &&
 223             sha1_object_info(sha1, NULL) == OBJ_BLOB)) {
 224                if (check_sha1_signature(repl, NULL, 0, NULL) < 0) {
 225                        error("sha1 mismatch %s", sha1_to_hex(repl));
 226                        return NULL;
 227                }
 228                parse_blob_buffer(lookup_blob(sha1), NULL, 0);
 229                return lookup_object(sha1);
 230        }
 231
 232        buffer = read_sha1_file(sha1, &type, &size);
 233        if (buffer) {
 234                if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
 235                        free(buffer);
 236                        error("sha1 mismatch %s", sha1_to_hex(repl));
 237                        return NULL;
 238                }
 239
 240                obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
 241                if (!eaten)
 242                        free(buffer);
 243                return obj;
 244        }
 245        return NULL;
 246}
 247
 248struct object_list *object_list_insert(struct object *item,
 249                                       struct object_list **list_p)
 250{
 251        struct object_list *new_list = xmalloc(sizeof(struct object_list));
 252        new_list->item = item;
 253        new_list->next = *list_p;
 254        *list_p = new_list;
 255        return new_list;
 256}
 257
 258int object_list_contains(struct object_list *list, struct object *obj)
 259{
 260        while (list) {
 261                if (list->item == obj)
 262                        return 1;
 263                list = list->next;
 264        }
 265        return 0;
 266}
 267
 268void add_object_array(struct object *obj, const char *name, struct object_array *array)
 269{
 270        add_object_array_with_mode(obj, name, array, S_IFINVALID);
 271}
 272
 273/*
 274 * A zero-length string to which object_array_entry::name can be
 275 * initialized without requiring a malloc/free.
 276 */
 277static char object_array_slopbuf[1];
 278
 279void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
 280{
 281        unsigned nr = array->nr;
 282        unsigned alloc = array->alloc;
 283        struct object_array_entry *objects = array->objects;
 284        struct object_array_entry *entry;
 285
 286        if (nr >= alloc) {
 287                alloc = (alloc + 32) * 2;
 288                objects = xrealloc(objects, alloc * sizeof(*objects));
 289                array->alloc = alloc;
 290                array->objects = objects;
 291        }
 292        entry = &objects[nr];
 293        entry->item = obj;
 294        if (!name)
 295                entry->name = NULL;
 296        else if (!*name)
 297                /* Use our own empty string instead of allocating one: */
 298                entry->name = object_array_slopbuf;
 299        else
 300                entry->name = xstrdup(name);
 301        entry->mode = mode;
 302        array->nr = ++nr;
 303}
 304
 305void object_array_filter(struct object_array *array,
 306                         object_array_each_func_t want, void *cb_data)
 307{
 308        unsigned nr = array->nr, src, dst;
 309        struct object_array_entry *objects = array->objects;
 310
 311        for (src = dst = 0; src < nr; src++) {
 312                if (want(&objects[src], cb_data)) {
 313                        if (src != dst)
 314                                objects[dst] = objects[src];
 315                        dst++;
 316                } else {
 317                        if (objects[src].name != object_array_slopbuf)
 318                                free(objects[src].name);
 319                }
 320        }
 321        array->nr = dst;
 322}
 323
 324/*
 325 * Return true iff array already contains an entry with name.
 326 */
 327static int contains_name(struct object_array *array, const char *name)
 328{
 329        unsigned nr = array->nr, i;
 330        struct object_array_entry *object = array->objects;
 331
 332        for (i = 0; i < nr; i++, object++)
 333                if (!strcmp(object->name, name))
 334                        return 1;
 335        return 0;
 336}
 337
 338void object_array_remove_duplicates(struct object_array *array)
 339{
 340        unsigned nr = array->nr, src;
 341        struct object_array_entry *objects = array->objects;
 342
 343        array->nr = 0;
 344        for (src = 0; src < nr; src++) {
 345                if (!contains_name(array, objects[src].name)) {
 346                        if (src != array->nr)
 347                                objects[array->nr] = objects[src];
 348                        array->nr++;
 349                } else {
 350                        if (objects[src].name != object_array_slopbuf)
 351                                free(objects[src].name);
 352                }
 353        }
 354}
 355
 356void clear_object_flags(unsigned flags)
 357{
 358        int i;
 359
 360        for (i=0; i < obj_hash_size; i++) {
 361                struct object *obj = obj_hash[i];
 362                if (obj)
 363                        obj->flags &= ~flags;
 364        }
 365}