8 static struct object **obj_hash;
 
   9 static int nr_objs, obj_hash_size;
 
  11 unsigned int get_max_object_index(void)
 
  16 struct object *get_indexed_object(unsigned int idx)
 
  21 static 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 */
 
  29 const char *typename(unsigned int type)
 
  31         if (type >= ARRAY_SIZE(object_type_strings))
 
  33         return object_type_strings[type];
 
  36 int type_from_string(const char *str)
 
  40         for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
 
  41                 if (!strcmp(str, object_type_strings[i]))
 
  43         die("invalid object type \"%s\"", str);
 
  46 static unsigned int hash_obj(struct object *obj, unsigned int n)
 
  49         memcpy(&hash, obj->sha1, sizeof(unsigned int));
 
  53 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
 
  55         unsigned int j = hash_obj(obj, size);
 
  65 static unsigned int hashtable_index(const unsigned char *sha1)
 
  68         memcpy(&i, sha1, sizeof(unsigned int));
 
  69         return i % obj_hash_size;
 
  72 struct object *lookup_object(const unsigned char *sha1)
 
  80         i = hashtable_index(sha1);
 
  81         while ((obj = obj_hash[i]) != NULL) {
 
  82                 if (!hashcmp(sha1, obj->sha1))
 
  85                 if (i == obj_hash_size)
 
  91 static void grow_object_hash(void)
 
  94         int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
 
  95         struct object **new_hash;
 
  97         new_hash = xcalloc(new_hash_size, sizeof(struct object *));
 
  98         for (i = 0; i < obj_hash_size; i++) {
 
  99                 struct object *obj = obj_hash[i];
 
 102                 insert_obj_hash(obj, new_hash, new_hash_size);
 
 106         obj_hash_size = new_hash_size;
 
 109 void *create_object(const unsigned char *sha1, int type, void *o)
 
 111         struct object *obj = o;
 
 117         hashcpy(obj->sha1, sha1);
 
 119         if (obj_hash_size - 1 <= nr_objs * 2)
 
 122         insert_obj_hash(obj, obj_hash, obj_hash_size);
 
 127 struct object *lookup_unknown_object(const unsigned char *sha1)
 
 129         struct object *obj = lookup_object(sha1);
 
 131                 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
 
 135 struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
 
 141         if (type == OBJ_BLOB) {
 
 142                 struct blob *blob = lookup_blob(sha1);
 
 144                         if (parse_blob_buffer(blob, buffer, size))
 
 148         } else if (type == OBJ_TREE) {
 
 149                 struct tree *tree = lookup_tree(sha1);
 
 153                                 tree->object.parsed = 0;
 
 154                         if (!tree->object.parsed) {
 
 155                                 if (parse_tree_buffer(tree, buffer, size))
 
 160         } else if (type == OBJ_COMMIT) {
 
 161                 struct commit *commit = lookup_commit(sha1);
 
 163                         if (parse_commit_buffer(commit, buffer, size))
 
 165                         if (!commit->buffer) {
 
 166                                 commit->buffer = buffer;
 
 169                         obj = &commit->object;
 
 171         } else if (type == OBJ_TAG) {
 
 172                 struct tag *tag = lookup_tag(sha1);
 
 174                         if (parse_tag_buffer(tag, buffer, size))
 
 179                 warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type);
 
 182         if (obj && obj->type == OBJ_NONE)
 
 188 struct object *parse_object(const unsigned char *sha1)
 
 191         enum object_type type;
 
 193         const unsigned char *repl = lookup_replace_object(sha1);
 
 194         void *buffer = read_sha1_file(sha1, &type, &size);
 
 198                 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
 
 200                         error("sha1 mismatch %s\n", sha1_to_hex(repl));
 
 204                 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
 
 212 struct object_list *object_list_insert(struct object *item,
 
 213                                        struct object_list **list_p)
 
 215         struct object_list *new_list = xmalloc(sizeof(struct object_list));
 
 216         new_list->item = item;
 
 217         new_list->next = *list_p;
 
 222 int object_list_contains(struct object_list *list, struct object *obj)
 
 225                 if (list->item == obj)
 
 232 void add_object_array(struct object *obj, const char *name, struct object_array *array)
 
 234         add_object_array_with_mode(obj, name, array, S_IFINVALID);
 
 237 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
 
 239         unsigned nr = array->nr;
 
 240         unsigned alloc = array->alloc;
 
 241         struct object_array_entry *objects = array->objects;
 
 244                 alloc = (alloc + 32) * 2;
 
 245                 objects = xrealloc(objects, alloc * sizeof(*objects));
 
 246                 array->alloc = alloc;
 
 247                 array->objects = objects;
 
 249         objects[nr].item = obj;
 
 250         objects[nr].name = name;
 
 251         objects[nr].mode = mode;
 
 255 void object_array_remove_duplicates(struct object_array *array)
 
 257         unsigned int ref, src, dst;
 
 258         struct object_array_entry *objects = array->objects;
 
 260         for (ref = 0; ref + 1 < array->nr; ref++) {
 
 261                 for (src = ref + 1, dst = src;
 
 264                         if (!strcmp(objects[ref].name, objects[src].name))
 
 267                                 objects[dst] = objects[src];