Shrink "struct object" a bit
[git/git.git] / object.c
CommitLineData
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
DB
7
8struct object **objs;
070879ca
JS
9static int nr_objs;
10int obj_allocs;
175785e5 11
885a86ab
LT
12const char *type_names[] = {
13 "none", "blob", "tree", "commit", "bad"
14};
15
3a7c352b 16int track_object_refs = 0;
8805ccac 17
070879ca
JS
18static int hashtable_index(const unsigned char *sha1)
19{
2b796360
JH
20 unsigned int i;
21 memcpy(&i, sha1, sizeof(unsigned int));
070879ca
JS
22 return (int)(i % obj_allocs);
23}
24
5d6ccf5c 25static int find_object(const unsigned char *sha1)
175785e5 26{
2b796360 27 int i;
175785e5 28
070879ca
JS
29 if (!objs)
30 return -1;
175785e5 31
2b796360 32 i = hashtable_index(sha1);
070879ca
JS
33 while (objs[i]) {
34 if (memcmp(sha1, objs[i]->sha1, 20) == 0)
35 return i;
36 i++;
37 if (i == obj_allocs)
38 i = 0;
39 }
40 return -1 - i;
175785e5
DB
41}
42
5d6ccf5c 43struct object *lookup_object(const unsigned char *sha1)
175785e5
DB
44{
45 int pos = find_object(sha1);
46 if (pos >= 0)
47 return objs[pos];
48 return NULL;
49}
50
5d6ccf5c 51void created_object(const unsigned char *sha1, struct object *obj)
175785e5 52{
070879ca 53 int pos;
175785e5
DB
54
55 obj->parsed = 0;
56 memcpy(obj->sha1, sha1, 20);
885a86ab 57 obj->type = TYPE_NONE;
175785e5
DB
58 obj->refs = NULL;
59 obj->used = 0;
60
070879ca
JS
61 if (obj_allocs - 1 <= nr_objs * 2) {
62 int i, count = obj_allocs;
63 obj_allocs = (obj_allocs < 32 ? 32 : 2 * obj_allocs);
812666c8 64 objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
070879ca
JS
65 memset(objs + count, 0, (obj_allocs - count)
66 * sizeof(struct object *));
d7ee090d 67 for (i = 0; i < obj_allocs; i++)
070879ca
JS
68 if (objs[i]) {
69 int j = find_object(objs[i]->sha1);
70 if (j != i) {
71 j = -1 - j;
72 objs[j] = objs[i];
73 objs[i] = NULL;
74 }
75 }
175785e5
DB
76 }
77
070879ca
JS
78 pos = find_object(sha1);
79 if (pos >= 0)
80 die("Inserting %s twice\n", sha1_to_hex(sha1));
81 pos = -pos-1;
175785e5
DB
82
83 objs[pos] = obj;
84 nr_objs++;
85}
86
4a4e6fd7 87struct object_refs *alloc_object_refs(unsigned count)
175785e5 88{
4a4e6fd7
SV
89 struct object_refs *refs;
90 size_t size = sizeof(*refs) + count*sizeof(struct object *);
8805ccac 91
90321c10 92 refs = xcalloc(1, size);
4a4e6fd7
SV
93 refs->count = count;
94 return refs;
95}
96
97static int compare_object_pointers(const void *a, const void *b)
98{
99 const struct object * const *pa = a;
100 const struct object * const *pb = b;
e23eff8b
JH
101 if (*pa == *pb)
102 return 0;
103 else if (*pa < *pb)
104 return -1;
105 else
106 return 1;
4a4e6fd7
SV
107}
108
109void set_object_refs(struct object *obj, struct object_refs *refs)
110{
111 unsigned int i, j;
112
113 /* Do not install empty list of references */
114 if (refs->count < 1) {
115 free(refs);
8805ccac 116 return;
4a4e6fd7 117 }
8805ccac 118
4a4e6fd7
SV
119 /* Sort the list and filter out duplicates */
120 qsort(refs->ref, refs->count, sizeof(refs->ref[0]),
121 compare_object_pointers);
122 for (i = j = 1; i < refs->count; i++) {
123 if (refs->ref[i] != refs->ref[i - 1])
124 refs->ref[j++] = refs->ref[i];
125 }
126 if (j < refs->count) {
127 /* Duplicates were found - reallocate list */
128 size_t size = sizeof(*refs) + j*sizeof(struct object *);
129 refs->count = j;
130 refs = xrealloc(refs, size);
175785e5
DB
131 }
132
4a4e6fd7
SV
133 for (i = 0; i < refs->count; i++)
134 refs->ref[i]->used = 1;
135 obj->refs = refs;
175785e5
DB
136}
137
138void mark_reachable(struct object *obj, unsigned int mask)
139{
8805ccac
LT
140 if (!track_object_refs)
141 die("cannot do reachability with object refs turned off");
175785e5
DB
142 /* If we've been here already, don't bother */
143 if (obj->flags & mask)
144 return;
145 obj->flags |= mask;
4a4e6fd7
SV
146 if (obj->refs) {
147 const struct object_refs *refs = obj->refs;
148 unsigned i;
149 for (i = 0; i < refs->count; i++)
150 mark_reachable(refs->ref[i], mask);
175785e5
DB
151 }
152}
e9eefa67 153
89e4202f
DB
154struct object *lookup_object_type(const unsigned char *sha1, const char *type)
155{
66e481b0
DB
156 if (!type) {
157 return lookup_unknown_object(sha1);
158 } else if (!strcmp(type, blob_type)) {
89e4202f
DB
159 return &lookup_blob(sha1)->object;
160 } else if (!strcmp(type, tree_type)) {
161 return &lookup_tree(sha1)->object;
162 } else if (!strcmp(type, commit_type)) {
163 return &lookup_commit(sha1)->object;
164 } else if (!strcmp(type, tag_type)) {
165 return &lookup_tag(sha1)->object;
166 } else {
167 error("Unknown type %s", type);
168 return NULL;
169 }
170}
171
66e481b0
DB
172union any_object {
173 struct object object;
174 struct commit commit;
175 struct tree tree;
176 struct blob blob;
177 struct tag tag;
178};
179
180struct object *lookup_unknown_object(const unsigned char *sha1)
181{
182 struct object *obj = lookup_object(sha1);
183 if (!obj) {
90321c10 184 union any_object *ret = xcalloc(1, sizeof(*ret));
66e481b0 185 created_object(sha1, &ret->object);
885a86ab 186 ret->object.type = TYPE_NONE;
66e481b0
DB
187 return &ret->object;
188 }
189 return obj;
190}
191
5d6ccf5c 192struct object *parse_object(const unsigned char *sha1)
e9eefa67 193{
c4584ae3
JH
194 unsigned long size;
195 char type[20];
196 void *buffer = read_sha1_file(sha1, type, &size);
197 if (buffer) {
bd2c39f5 198 struct object *obj;
c4584ae3 199 if (check_sha1_signature(sha1, buffer, size, type) < 0)
e9eefa67 200 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
8e440259 201 if (!strcmp(type, blob_type)) {
bd2c39f5
NP
202 struct blob *blob = lookup_blob(sha1);
203 parse_blob_buffer(blob, buffer, size);
204 obj = &blob->object;
8e440259 205 } else if (!strcmp(type, tree_type)) {
bd2c39f5 206 struct tree *tree = lookup_tree(sha1);
bd2c39f5 207 obj = &tree->object;
136f2e54
LT
208 if (!tree->object.parsed) {
209 parse_tree_buffer(tree, buffer, size);
210 buffer = NULL;
211 }
8e440259 212 } else if (!strcmp(type, commit_type)) {
bd2c39f5
NP
213 struct commit *commit = lookup_commit(sha1);
214 parse_commit_buffer(commit, buffer, size);
bd1e17e2
LT
215 if (!commit->buffer) {
216 commit->buffer = buffer;
217 buffer = NULL;
218 }
bd2c39f5 219 obj = &commit->object;
8e440259 220 } else if (!strcmp(type, tag_type)) {
bd2c39f5
NP
221 struct tag *tag = lookup_tag(sha1);
222 parse_tag_buffer(tag, buffer, size);
223 obj = &tag->object;
e9eefa67 224 } else {
bd2c39f5 225 obj = NULL;
e9eefa67 226 }
bd2c39f5
NP
227 free(buffer);
228 return obj;
e9eefa67
DB
229 }
230 return NULL;
231}
66e481b0
DB
232
233struct object_list *object_list_insert(struct object *item,
234 struct object_list **list_p)
235{
236 struct object_list *new_list = xmalloc(sizeof(struct object_list));
237 new_list->item = item;
238 new_list->next = *list_p;
239 *list_p = new_list;
240 return new_list;
241}
242
680bab3d
DB
243void object_list_append(struct object *item,
244 struct object_list **list_p)
245{
246 while (*list_p) {
247 list_p = &((*list_p)->next);
248 }
249 *list_p = xmalloc(sizeof(struct object_list));
250 (*list_p)->next = NULL;
251 (*list_p)->item = item;
252}
253
66e481b0
DB
254unsigned object_list_length(struct object_list *list)
255{
256 unsigned ret = 0;
257 while (list) {
258 list = list->next;
259 ret++;
260 }
261 return ret;
262}
263
264int object_list_contains(struct object_list *list, struct object *obj)
265{
266 while (list) {
267 if (list->item == obj)
268 return 1;
269 list = list->next;
270 }
271 return 0;
272}