archive: handle commits with an empty tree
[git/jnareb-git.git] / object.c
blob4af3451bf89471a0ab7a148aad4924a8ac8ae053
1 #include "cache.h"
2 #include "object.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "tag.h"
8 static struct object **obj_hash;
9 static int nr_objs, obj_hash_size;
11 unsigned int get_max_object_index(void)
13 return obj_hash_size;
16 struct object *get_indexed_object(unsigned int idx)
18 return obj_hash[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))
32 return NULL;
33 return object_type_strings[type];
36 int type_from_string(const char *str)
38 int i;
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);
46 static unsigned int hash_obj(struct object *obj, unsigned int n)
48 unsigned int hash;
49 memcpy(&hash, obj->sha1, sizeof(unsigned int));
50 return hash % n;
53 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
55 unsigned int j = hash_obj(obj, size);
57 while (hash[j]) {
58 j++;
59 if (j >= size)
60 j = 0;
62 hash[j] = obj;
65 static unsigned int hashtable_index(const unsigned char *sha1)
67 unsigned int i;
68 memcpy(&i, sha1, sizeof(unsigned int));
69 return i % obj_hash_size;
72 struct object *lookup_object(const unsigned char *sha1)
74 unsigned int i;
75 struct object *obj;
77 if (!obj_hash)
78 return NULL;
80 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;
88 return obj;
91 static void grow_object_hash(void)
93 int i;
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];
100 if (!obj)
101 continue;
102 insert_obj_hash(obj, new_hash, new_hash_size);
104 free(obj_hash);
105 obj_hash = new_hash;
106 obj_hash_size = new_hash_size;
109 void *create_object(const unsigned char *sha1, int type, void *o)
111 struct object *obj = o;
113 obj->parsed = 0;
114 obj->used = 0;
115 obj->type = type;
116 obj->flags = 0;
117 hashcpy(obj->sha1, sha1);
119 if (obj_hash_size - 1 <= nr_objs * 2)
120 grow_object_hash();
122 insert_obj_hash(obj, obj_hash, obj_hash_size);
123 nr_objs++;
124 return obj;
127 struct object *lookup_unknown_object(const unsigned char *sha1)
129 struct object *obj = lookup_object(sha1);
130 if (!obj)
131 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
132 return obj;
135 struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
137 struct object *obj;
138 int eaten = 0;
140 obj = NULL;
141 if (type == OBJ_BLOB) {
142 struct blob *blob = lookup_blob(sha1);
143 if (blob) {
144 if (parse_blob_buffer(blob, buffer, size))
145 return NULL;
146 obj = &blob->object;
148 } else if (type == OBJ_TREE) {
149 struct tree *tree = lookup_tree(sha1);
150 if (tree) {
151 obj = &tree->object;
152 if (!tree->buffer)
153 tree->object.parsed = 0;
154 if (!tree->object.parsed) {
155 if (parse_tree_buffer(tree, buffer, size))
156 return NULL;
157 eaten = 1;
160 } else if (type == OBJ_COMMIT) {
161 struct commit *commit = lookup_commit(sha1);
162 if (commit) {
163 if (parse_commit_buffer(commit, buffer, size))
164 return NULL;
165 if (!commit->buffer) {
166 commit->buffer = buffer;
167 eaten = 1;
169 obj = &commit->object;
171 } else if (type == OBJ_TAG) {
172 struct tag *tag = lookup_tag(sha1);
173 if (tag) {
174 if (parse_tag_buffer(tag, buffer, size))
175 return NULL;
176 obj = &tag->object;
178 } else {
179 warning("object %s has unknown type id %d", sha1_to_hex(sha1), type);
180 obj = NULL;
182 if (obj && obj->type == OBJ_NONE)
183 obj->type = type;
184 *eaten_p = eaten;
185 return obj;
188 struct object *parse_object(const unsigned char *sha1)
190 unsigned long size;
191 enum object_type type;
192 int eaten;
193 const unsigned char *repl = lookup_replace_object(sha1);
194 void *buffer;
195 struct object *obj;
197 obj = lookup_object(sha1);
198 if (obj && obj->parsed)
199 return obj;
201 if ((obj && obj->type == OBJ_BLOB) ||
202 (!obj && has_sha1_file(sha1) &&
203 sha1_object_info(sha1, NULL) == OBJ_BLOB)) {
204 if (check_sha1_signature(repl, NULL, 0, NULL) < 0) {
205 error("sha1 mismatch %s", sha1_to_hex(repl));
206 return NULL;
208 parse_blob_buffer(lookup_blob(sha1), NULL, 0);
209 return lookup_object(sha1);
212 buffer = read_sha1_file(sha1, &type, &size);
213 if (buffer) {
214 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
215 free(buffer);
216 error("sha1 mismatch %s", sha1_to_hex(repl));
217 return NULL;
220 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
221 if (!eaten)
222 free(buffer);
223 return obj;
225 return NULL;
228 struct object_list *object_list_insert(struct object *item,
229 struct object_list **list_p)
231 struct object_list *new_list = xmalloc(sizeof(struct object_list));
232 new_list->item = item;
233 new_list->next = *list_p;
234 *list_p = new_list;
235 return new_list;
238 int object_list_contains(struct object_list *list, struct object *obj)
240 while (list) {
241 if (list->item == obj)
242 return 1;
243 list = list->next;
245 return 0;
248 void add_object_array(struct object *obj, const char *name, struct object_array *array)
250 add_object_array_with_mode(obj, name, array, S_IFINVALID);
253 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
255 unsigned nr = array->nr;
256 unsigned alloc = array->alloc;
257 struct object_array_entry *objects = array->objects;
259 if (nr >= alloc) {
260 alloc = (alloc + 32) * 2;
261 objects = xrealloc(objects, alloc * sizeof(*objects));
262 array->alloc = alloc;
263 array->objects = objects;
265 objects[nr].item = obj;
266 objects[nr].name = name;
267 objects[nr].mode = mode;
268 array->nr = ++nr;
271 void object_array_remove_duplicates(struct object_array *array)
273 unsigned int ref, src, dst;
274 struct object_array_entry *objects = array->objects;
276 for (ref = 0; ref + 1 < array->nr; ref++) {
277 for (src = ref + 1, dst = src;
278 src < array->nr;
279 src++) {
280 if (!strcmp(objects[ref].name, objects[src].name))
281 continue;
282 if (src != dst)
283 objects[dst] = objects[src];
284 dst++;
286 array->nr = dst;
290 void clear_object_flags(unsigned flags)
292 int i;
294 for (i=0; i < obj_hash_size; i++) {
295 struct object *obj = obj_hash[i];
296 if (obj)
297 obj->flags &= ~flags;