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