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