]> git.ipfire.org Git - thirdparty/git.git/blame - object.c
rev-list --exclude: export add/clear-ref-exclusion and ref-excluded API
[thirdparty/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 7
0556a11a
LT
8static struct object **obj_hash;
9static int nr_objs, obj_hash_size;
fc046a75
LT
10
11unsigned int get_max_object_index(void)
12{
0556a11a 13 return obj_hash_size;
fc046a75
LT
14}
15
16struct object *get_indexed_object(unsigned int idx)
17{
0556a11a 18 return obj_hash[idx];
fc046a75 19}
175785e5 20
df843662
NP
21static const char *object_type_strings[] = {
22 NULL, /* OBJ_NONE = 0 */
23 "commit", /* OBJ_COMMIT = 1 */
24 "tree", /* OBJ_TREE = 2 */
25 "blob", /* OBJ_BLOB = 3 */
26 "tag", /* OBJ_TAG = 4 */
885a86ab
LT
27};
28
df843662
NP
29const char *typename(unsigned int type)
30{
31 if (type >= ARRAY_SIZE(object_type_strings))
32 return NULL;
33 return object_type_strings[type];
34}
35
36int type_from_string(const char *str)
37{
38 int i;
39
40 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
41 if (!strcmp(str, object_type_strings[i]))
42 return i;
43 die("invalid object type \"%s\"", str);
44}
45
0556a11a
LT
46static unsigned int hash_obj(struct object *obj, unsigned int n)
47{
b867d324
DM
48 unsigned int hash;
49 memcpy(&hash, obj->sha1, sizeof(unsigned int));
0556a11a
LT
50 return hash % n;
51}
52
53static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
54{
91fe2f90 55 unsigned int j = hash_obj(obj, size);
0556a11a
LT
56
57 while (hash[j]) {
58 j++;
59 if (j >= size)
60 j = 0;
61 }
62 hash[j] = obj;
63}
64
91fe2f90 65static unsigned int hashtable_index(const unsigned char *sha1)
070879ca 66{
2b796360
JH
67 unsigned int i;
68 memcpy(&i, sha1, sizeof(unsigned int));
91fe2f90 69 return i % obj_hash_size;
070879ca
JS
70}
71
0556a11a 72struct object *lookup_object(const unsigned char *sha1)
175785e5 73{
9a414486 74 unsigned int i, first;
0556a11a 75 struct object *obj;
175785e5 76
0556a11a
LT
77 if (!obj_hash)
78 return NULL;
175785e5 79
9a414486 80 first = i = hashtable_index(sha1);
0556a11a 81 while ((obj = obj_hash[i]) != NULL) {
a89fccd2 82 if (!hashcmp(sha1, obj->sha1))
0556a11a 83 break;
070879ca 84 i++;
0556a11a 85 if (i == obj_hash_size)
070879ca
JS
86 i = 0;
87 }
9a414486
JK
88 if (obj && i != first) {
89 /*
90 * Move object to where we started to look for it so
91 * that we do not need to walk the hash table the next
92 * time we look for it.
93 */
94 struct object *tmp = obj_hash[i];
95 obj_hash[i] = obj_hash[first];
96 obj_hash[first] = tmp;
97 }
0556a11a 98 return obj;
175785e5
DB
99}
100
0556a11a 101static void grow_object_hash(void)
175785e5 102{
0556a11a
LT
103 int i;
104 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
105 struct object **new_hash;
106
b3c952f8 107 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
0556a11a
LT
108 for (i = 0; i < obj_hash_size; i++) {
109 struct object *obj = obj_hash[i];
110 if (!obj)
111 continue;
112 insert_obj_hash(obj, new_hash, new_hash_size);
113 }
114 free(obj_hash);
115 obj_hash = new_hash;
116 obj_hash_size = new_hash_size;
175785e5
DB
117}
118
100c5f3b 119void *create_object(const unsigned char *sha1, int type, void *o)
175785e5 120{
100c5f3b
LT
121 struct object *obj = o;
122
175785e5 123 obj->parsed = 0;
175785e5 124 obj->used = 0;
100c5f3b 125 obj->type = type;
0556a11a 126 obj->flags = 0;
e702496e 127 hashcpy(obj->sha1, sha1);
175785e5 128
0556a11a
LT
129 if (obj_hash_size - 1 <= nr_objs * 2)
130 grow_object_hash();
175785e5 131
0556a11a 132 insert_obj_hash(obj, obj_hash, obj_hash_size);
175785e5 133 nr_objs++;
100c5f3b 134 return obj;
175785e5
DB
135}
136
66e481b0
DB
137struct object *lookup_unknown_object(const unsigned char *sha1)
138{
139 struct object *obj = lookup_object(sha1);
100c5f3b
LT
140 if (!obj)
141 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
66e481b0
DB
142 return obj;
143}
144
21666f1a 145struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
9f613ddd
JH
146{
147 struct object *obj;
8e92e8f2 148 *eaten_p = 0;
9f613ddd 149
cc216827 150 obj = NULL;
21666f1a 151 if (type == OBJ_BLOB) {
9f613ddd 152 struct blob *blob = lookup_blob(sha1);
cc216827 153 if (blob) {
d0b8c9e5
MK
154 if (parse_blob_buffer(blob, buffer, size))
155 return NULL;
cc216827
JM
156 obj = &blob->object;
157 }
21666f1a 158 } else if (type == OBJ_TREE) {
9f613ddd 159 struct tree *tree = lookup_tree(sha1);
cc216827
JM
160 if (tree) {
161 obj = &tree->object;
68be2fea
JH
162 if (!tree->buffer)
163 tree->object.parsed = 0;
cc216827 164 if (!tree->object.parsed) {
d0b8c9e5
MK
165 if (parse_tree_buffer(tree, buffer, size))
166 return NULL;
8e92e8f2 167 *eaten_p = 1;
cc216827 168 }
9f613ddd 169 }
21666f1a 170 } else if (type == OBJ_COMMIT) {
9f613ddd 171 struct commit *commit = lookup_commit(sha1);
cc216827 172 if (commit) {
d0b8c9e5
MK
173 if (parse_commit_buffer(commit, buffer, size))
174 return NULL;
cc216827
JM
175 if (!commit->buffer) {
176 commit->buffer = buffer;
8e92e8f2 177 *eaten_p = 1;
cc216827
JM
178 }
179 obj = &commit->object;
9f613ddd 180 }
21666f1a 181 } else if (type == OBJ_TAG) {
9f613ddd 182 struct tag *tag = lookup_tag(sha1);
cc216827 183 if (tag) {
d0b8c9e5
MK
184 if (parse_tag_buffer(tag, buffer, size))
185 return NULL;
cc216827
JM
186 obj = &tag->object;
187 }
9f613ddd 188 } else {
82247e9b 189 warning("object %s has unknown type id %d", sha1_to_hex(sha1), type);
9f613ddd
JH
190 obj = NULL;
191 }
e2ac7cb5
SV
192 if (obj && obj->type == OBJ_NONE)
193 obj->type = type;
9f613ddd
JH
194 return obj;
195}
196
75a95490
JK
197struct object *parse_object_or_die(const unsigned char *sha1,
198 const char *name)
199{
200 struct object *o = parse_object(sha1);
201 if (o)
202 return o;
203
204 die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1));
205}
206
5d6ccf5c 207struct object *parse_object(const unsigned char *sha1)
e9eefa67 208{
c4584ae3 209 unsigned long size;
21666f1a 210 enum object_type type;
9f613ddd 211 int eaten;
4bbf5a26 212 const unsigned char *repl = lookup_replace_object(sha1);
ccdc6037
JK
213 void *buffer;
214 struct object *obj;
215
216 obj = lookup_object(sha1);
217 if (obj && obj->parsed)
218 return obj;
9f613ddd 219
090ea126
NTND
220 if ((obj && obj->type == OBJ_BLOB) ||
221 (!obj && has_sha1_file(sha1) &&
222 sha1_object_info(sha1, NULL) == OBJ_BLOB)) {
223 if (check_sha1_signature(repl, NULL, 0, NULL) < 0) {
82247e9b 224 error("sha1 mismatch %s", sha1_to_hex(repl));
090ea126
NTND
225 return NULL;
226 }
227 parse_blob_buffer(lookup_blob(sha1), NULL, 0);
228 return lookup_object(sha1);
229 }
230
ccdc6037 231 buffer = read_sha1_file(sha1, &type, &size);
c4584ae3 232 if (buffer) {
0e87c367 233 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
0b1f1130 234 free(buffer);
82247e9b 235 error("sha1 mismatch %s", sha1_to_hex(repl));
acdeec62
LT
236 return NULL;
237 }
9f613ddd 238
2e3400c0 239 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
9f613ddd
JH
240 if (!eaten)
241 free(buffer);
bd2c39f5 242 return obj;
e9eefa67
DB
243 }
244 return NULL;
245}
66e481b0
DB
246
247struct object_list *object_list_insert(struct object *item,
248 struct object_list **list_p)
249{
250 struct object_list *new_list = xmalloc(sizeof(struct object_list));
55b4e9e4
JH
251 new_list->item = item;
252 new_list->next = *list_p;
253 *list_p = new_list;
254 return new_list;
66e481b0
DB
255}
256
66e481b0
DB
257int object_list_contains(struct object_list *list, struct object *obj)
258{
259 while (list) {
260 if (list->item == obj)
261 return 1;
262 list = list->next;
263 }
264 return 0;
265}
1f1e895f
LT
266
267void add_object_array(struct object *obj, const char *name, struct object_array *array)
e5709a4a
MK
268{
269 add_object_array_with_mode(obj, name, array, S_IFINVALID);
270}
271
31faeb20
MH
272/*
273 * A zero-length string to which object_array_entry::name can be
274 * initialized without requiring a malloc/free.
275 */
276static char object_array_slopbuf[1];
277
e5709a4a 278void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
1f1e895f
LT
279{
280 unsigned nr = array->nr;
281 unsigned alloc = array->alloc;
282 struct object_array_entry *objects = array->objects;
31faeb20 283 struct object_array_entry *entry;
1f1e895f
LT
284
285 if (nr >= alloc) {
286 alloc = (alloc + 32) * 2;
287 objects = xrealloc(objects, alloc * sizeof(*objects));
288 array->alloc = alloc;
289 array->objects = objects;
290 }
31faeb20
MH
291 entry = &objects[nr];
292 entry->item = obj;
293 if (!name)
294 entry->name = NULL;
295 else if (!*name)
296 /* Use our own empty string instead of allocating one: */
297 entry->name = object_array_slopbuf;
298 else
299 entry->name = xstrdup(name);
300 entry->mode = mode;
1f1e895f
LT
301 array->nr = ++nr;
302}
b2a6d1c6 303
aeb4a51e
MH
304void object_array_filter(struct object_array *array,
305 object_array_each_func_t want, void *cb_data)
b2a6d1c6 306{
aeb4a51e 307 unsigned nr = array->nr, src, dst;
b2a6d1c6
JH
308 struct object_array_entry *objects = array->objects;
309
aeb4a51e
MH
310 for (src = dst = 0; src < nr; src++) {
311 if (want(&objects[src], cb_data)) {
b2a6d1c6
JH
312 if (src != dst)
313 objects[dst] = objects[src];
314 dst++;
31faeb20
MH
315 } else {
316 if (objects[src].name != object_array_slopbuf)
317 free(objects[src].name);
aeb4a51e
MH
318 }
319 }
320 array->nr = dst;
321}
322
1506510c
MH
323/*
324 * Return true iff array already contains an entry with name.
325 */
326static int contains_name(struct object_array *array, const char *name)
327{
328 unsigned nr = array->nr, i;
329 struct object_array_entry *object = array->objects;
330
331 for (i = 0; i < nr; i++, object++)
332 if (!strcmp(object->name, name))
333 return 1;
334 return 0;
335}
336
b2a6d1c6
JH
337void object_array_remove_duplicates(struct object_array *array)
338{
1506510c 339 unsigned nr = array->nr, src;
b2a6d1c6
JH
340 struct object_array_entry *objects = array->objects;
341
1506510c
MH
342 array->nr = 0;
343 for (src = 0; src < nr; src++) {
344 if (!contains_name(array, objects[src].name)) {
345 if (src != array->nr)
346 objects[array->nr] = objects[src];
347 array->nr++;
31faeb20
MH
348 } else {
349 if (objects[src].name != object_array_slopbuf)
350 free(objects[src].name);
b2a6d1c6 351 }
b2a6d1c6
JH
352 }
353}
bcc0a3ea
HV
354
355void clear_object_flags(unsigned flags)
356{
357 int i;
358
359 for (i=0; i < obj_hash_size; i++) {
360 struct object *obj = obj_hash[i];
361 if (obj)
362 obj->flags &= ~flags;
363 }
364}