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