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