Fix errno usage in connect.c
[git/dscho.git] / object.c
blob31c77ea03a2083ce9ed7c71a2b4d90fb6b944c78
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 **objs;
9 static int nr_objs, obj_allocs;
11 unsigned int get_max_object_index(void)
13 return obj_allocs;
16 struct object *get_indexed_object(unsigned int idx)
18 return objs[idx];
21 const char *type_names[] = {
22 "none", "blob", "tree", "commit", "bad"
25 static int hashtable_index(const unsigned char *sha1)
27 unsigned int i;
28 memcpy(&i, sha1, sizeof(unsigned int));
29 return (int)(i % obj_allocs);
32 static int find_object(const unsigned char *sha1)
34 int i;
36 if (!objs)
37 return -1;
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;
47 return -1 - i;
50 struct object *lookup_object(const unsigned char *sha1)
52 int pos = find_object(sha1);
53 if (pos >= 0)
54 return objs[pos];
55 return NULL;
58 void created_object(const unsigned char *sha1, struct object *obj)
60 int pos;
62 obj->parsed = 0;
63 memcpy(obj->sha1, sha1, 20);
64 obj->type = TYPE_NONE;
65 obj->used = 0;
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;
84 pos = find_object(sha1);
85 if (pos >= 0)
86 die("Inserting %s twice\n", sha1_to_hex(sha1));
87 pos = -pos-1;
89 objs[pos] = obj;
90 nr_objs++;
93 struct object *lookup_object_type(const unsigned char *sha1, const char *type)
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;
111 union any_object {
112 struct object object;
113 struct commit commit;
114 struct tree tree;
115 struct blob blob;
116 struct tag tag;
119 struct object *lookup_unknown_object(const unsigned char *sha1)
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;
128 return obj;
131 struct object *parse_object(const unsigned char *sha1)
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;
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;
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;
166 free(buffer);
167 return obj;
169 return NULL;
172 struct object_list *object_list_insert(struct object *item,
173 struct object_list **list_p)
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;
182 void object_list_append(struct object *item,
183 struct object_list **list_p)
185 while (*list_p) {
186 list_p = &((*list_p)->next);
188 *list_p = xmalloc(sizeof(struct object_list));
189 (*list_p)->next = NULL;
190 (*list_p)->item = item;
193 unsigned object_list_length(struct object_list *list)
195 unsigned ret = 0;
196 while (list) {
197 list = list->next;
198 ret++;
200 return ret;
203 int object_list_contains(struct object_list *list, struct object *obj)
205 while (list) {
206 if (list->item == obj)
207 return 1;
208 list = list->next;
210 return 0;
213 void add_object_array(struct object *obj, const char *name, struct object_array *array)
215 unsigned nr = array->nr;
216 unsigned alloc = array->alloc;
217 struct object_array_entry *objects = array->objects;
219 if (nr >= alloc) {
220 alloc = (alloc + 32) * 2;
221 objects = xrealloc(objects, alloc * sizeof(*objects));
222 array->alloc = alloc;
223 array->objects = objects;
225 objects[nr].item = obj;
226 objects[nr].name = name;
227 array->nr = ++nr;