blob: 23d6c9671904cfe273fc16de8a4d2db56b91c3b4 [file] [log] [blame]
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08001#include "cache.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07002#include "object.h"
Daniel Barkalowe9eefa62005-04-28 07:46:33 -07003#include "blob.h"
4#include "tree.h"
5#include "commit.h"
Daniel Barkalowe9eefa62005-04-28 07:46:33 -07006#include "tag.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07007
Linus Torvalds0556a112006-06-30 11:20:33 -07008static struct object **obj_hash;
9static int nr_objs, obj_hash_size;
Linus Torvaldsfc046a72006-06-29 21:38:55 -070010
11unsigned int get_max_object_index(void)
12{
Linus Torvalds0556a112006-06-30 11:20:33 -070013 return obj_hash_size;
Linus Torvaldsfc046a72006-06-29 21:38:55 -070014}
15
16struct object *get_indexed_object(unsigned int idx)
17{
Linus Torvalds0556a112006-06-30 11:20:33 -070018 return obj_hash[idx];
Linus Torvaldsfc046a72006-06-29 21:38:55 -070019}
Daniel Barkalow175785e2005-04-18 11:39:48 -070020
Nicolas Pitredf843662007-02-26 14:55:58 -050021static 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 */
Linus Torvalds885a86a2006-06-14 16:45:13 -070027};
28
Nicolas Pitredf843662007-02-26 14:55:58 -050029const 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
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020036int type_from_string_gently(const char *str, ssize_t len, int gentle)
Nicolas Pitredf843662007-02-26 14:55:58 -050037{
38 int i;
39
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020040 if (len < 0)
41 len = strlen(str);
42
Nicolas Pitredf843662007-02-26 14:55:58 -050043 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020044 if (!strncmp(str, object_type_strings[i], len))
Nicolas Pitredf843662007-02-26 14:55:58 -050045 return i;
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020046
47 if (gentle)
48 return -1;
49
Nicolas Pitredf843662007-02-26 14:55:58 -050050 die("invalid object type \"%s\"", str);
51}
52
Michael Haggerty33bef7e2014-02-28 17:29:17 +010053/*
54 * Return a numerical hash value between 0 and n-1 for the object with
55 * the specified sha1. n must be a power of 2. Please note that the
56 * return value is *not* consistent across computer architectures.
57 */
Nicolas Pitre9f36c9b2013-09-10 18:17:12 -040058static unsigned int hash_obj(const unsigned char *sha1, unsigned int n)
Linus Torvalds0556a112006-06-30 11:20:33 -070059{
Karsten Blees039dc712014-07-03 00:20:20 +020060 return sha1hash(sha1) & (n - 1);
Linus Torvalds0556a112006-06-30 11:20:33 -070061}
62
Michael Haggerty33bef7e2014-02-28 17:29:17 +010063/*
64 * Insert obj into the hash table hash, which has length size (which
65 * must be a power of 2). On collisions, simply overflow to the next
66 * empty bucket.
67 */
Linus Torvalds0556a112006-06-30 11:20:33 -070068static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
69{
Nicolas Pitre9f36c9b2013-09-10 18:17:12 -040070 unsigned int j = hash_obj(obj->sha1, size);
Linus Torvalds0556a112006-06-30 11:20:33 -070071
72 while (hash[j]) {
73 j++;
74 if (j >= size)
75 j = 0;
76 }
77 hash[j] = obj;
78}
79
Michael Haggerty33bef7e2014-02-28 17:29:17 +010080/*
81 * Look up the record for the given sha1 in the hash map stored in
82 * obj_hash. Return NULL if it was not found.
83 */
Jason McMullan5d6ccf52005-06-03 11:05:39 -040084struct object *lookup_object(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070085{
Jeff King9a414482013-05-01 16:34:50 -040086 unsigned int i, first;
Linus Torvalds0556a112006-06-30 11:20:33 -070087 struct object *obj;
88
89 if (!obj_hash)
90 return NULL;
91
Nicolas Pitre9f36c9b2013-09-10 18:17:12 -040092 first = i = hash_obj(sha1, obj_hash_size);
Linus Torvalds0556a112006-06-30 11:20:33 -070093 while ((obj = obj_hash[i]) != NULL) {
David Rientjesa89fccd2006-08-17 11:54:57 -070094 if (!hashcmp(sha1, obj->sha1))
Linus Torvalds0556a112006-06-30 11:20:33 -070095 break;
96 i++;
97 if (i == obj_hash_size)
98 i = 0;
99 }
Jeff King9a414482013-05-01 16:34:50 -0400100 if (obj && i != first) {
101 /*
102 * Move object to where we started to look for it so
103 * that we do not need to walk the hash table the next
104 * time we look for it.
105 */
106 struct object *tmp = obj_hash[i];
107 obj_hash[i] = obj_hash[first];
108 obj_hash[first] = tmp;
109 }
Linus Torvalds0556a112006-06-30 11:20:33 -0700110 return obj;
111}
112
Michael Haggerty33bef7e2014-02-28 17:29:17 +0100113/*
114 * Increase the size of the hash map stored in obj_hash to the next
115 * power of 2 (but at least 32). Copy the existing values to the new
116 * hash map.
117 */
Linus Torvalds0556a112006-06-30 11:20:33 -0700118static void grow_object_hash(void)
119{
120 int i;
Nicolas Pitre9f36c9b2013-09-10 18:17:12 -0400121 /*
122 * Note that this size must always be power-of-2 to match hash_obj
123 * above.
124 */
Linus Torvalds0556a112006-06-30 11:20:33 -0700125 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
126 struct object **new_hash;
127
Jonas Fonsecab3c952f2006-08-28 02:26:07 +0200128 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
Linus Torvalds0556a112006-06-30 11:20:33 -0700129 for (i = 0; i < obj_hash_size; i++) {
130 struct object *obj = obj_hash[i];
131 if (!obj)
132 continue;
133 insert_obj_hash(obj, new_hash, new_hash_size);
134 }
135 free(obj_hash);
136 obj_hash = new_hash;
137 obj_hash_size = new_hash_size;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700138}
139
Jeff Kingd36f51c2014-07-13 02:41:55 -0400140void *create_object(const unsigned char *sha1, void *o)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700141{
Linus Torvalds100c5f32007-04-16 22:11:43 -0700142 struct object *obj = o;
143
Daniel Barkalow175785e2005-04-18 11:39:48 -0700144 obj->parsed = 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700145 obj->used = 0;
Linus Torvalds0556a112006-06-30 11:20:33 -0700146 obj->flags = 0;
Shawn Pearcee7024962006-08-23 02:49:00 -0400147 hashcpy(obj->sha1, sha1);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700148
Linus Torvalds0556a112006-06-30 11:20:33 -0700149 if (obj_hash_size - 1 <= nr_objs * 2)
150 grow_object_hash();
Johannes Schindelin070879c2006-02-12 02:57:57 +0100151
Linus Torvalds0556a112006-06-30 11:20:33 -0700152 insert_obj_hash(obj, obj_hash, obj_hash_size);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700153 nr_objs++;
Linus Torvalds100c5f32007-04-16 22:11:43 -0700154 return obj;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700155}
156
Jeff King8ff226a2014-07-13 02:42:03 -0400157void *object_as_type(struct object *obj, enum object_type type, int quiet)
158{
159 if (obj->type == type)
160 return obj;
161 else if (obj->type == OBJ_NONE) {
Jeff Kingd66bebc2014-07-13 02:42:12 -0400162 if (type == OBJ_COMMIT)
163 ((struct commit *)obj)->index = alloc_commit_index();
Jeff King8ff226a2014-07-13 02:42:03 -0400164 obj->type = type;
165 return obj;
166 }
167 else {
168 if (!quiet)
169 error("object %s is a %s, not a %s",
170 sha1_to_hex(obj->sha1),
171 typename(obj->type), typename(type));
172 return NULL;
173 }
174}
175
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400176struct object *lookup_unknown_object(const unsigned char *sha1)
177{
178 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700179 if (!obj)
Jeff Kingd36f51c2014-07-13 02:41:55 -0400180 obj = create_object(sha1, alloc_object_node());
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400181 return obj;
182}
183
Nicolas Pitre21666f12007-02-26 14:55:59 -0500184struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700185{
186 struct object *obj;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200187 *eaten_p = 0;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700188
Jim Meyeringcc216822007-12-21 11:56:32 +0100189 obj = NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500190 if (type == OBJ_BLOB) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700191 struct blob *blob = lookup_blob(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100192 if (blob) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100193 if (parse_blob_buffer(blob, buffer, size))
194 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100195 obj = &blob->object;
196 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500197 } else if (type == OBJ_TREE) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700198 struct tree *tree = lookup_tree(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100199 if (tree) {
200 obj = &tree->object;
Junio C Hamano68be2fe2011-11-16 22:04:13 -0800201 if (!tree->buffer)
202 tree->object.parsed = 0;
Jim Meyeringcc216822007-12-21 11:56:32 +0100203 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100204 if (parse_tree_buffer(tree, buffer, size))
205 return NULL;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200206 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100207 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700208 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500209 } else if (type == OBJ_COMMIT) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700210 struct commit *commit = lookup_commit(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100211 if (commit) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100212 if (parse_commit_buffer(commit, buffer, size))
213 return NULL;
Jeff King8597ea32014-06-10 17:44:13 -0400214 if (!get_cached_commit_buffer(commit, NULL)) {
215 set_commit_buffer(commit, buffer, size);
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200216 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100217 }
218 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700219 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500220 } else if (type == OBJ_TAG) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700221 struct tag *tag = lookup_tag(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100222 if (tag) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100223 if (parse_tag_buffer(tag, buffer, size))
224 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100225 obj = &tag->object;
226 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700227 } else {
Pete Wyckoff82247e92012-04-29 20:28:45 -0400228 warning("object %s has unknown type id %d", sha1_to_hex(sha1), type);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700229 obj = NULL;
230 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700231 return obj;
232}
233
Jeff King75a95492013-03-17 04:22:36 -0400234struct object *parse_object_or_die(const unsigned char *sha1,
235 const char *name)
236{
237 struct object *o = parse_object(sha1);
238 if (o)
239 return o;
240
241 die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1));
242}
243
Jason McMullan5d6ccf52005-06-03 11:05:39 -0400244struct object *parse_object(const unsigned char *sha1)
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700245{
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700246 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500247 enum object_type type;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700248 int eaten;
Junio C Hamano4bbf5a22011-05-15 12:54:52 -0700249 const unsigned char *repl = lookup_replace_object(sha1);
Jeff Kingccdc6032012-01-05 16:00:01 -0500250 void *buffer;
251 struct object *obj;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700252
Jeff Kingccdc6032012-01-05 16:00:01 -0500253 obj = lookup_object(sha1);
254 if (obj && obj->parsed)
255 return obj;
256
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700257 if ((obj && obj->type == OBJ_BLOB) ||
258 (!obj && has_sha1_file(sha1) &&
259 sha1_object_info(sha1, NULL) == OBJ_BLOB)) {
260 if (check_sha1_signature(repl, NULL, 0, NULL) < 0) {
Pete Wyckoff82247e92012-04-29 20:28:45 -0400261 error("sha1 mismatch %s", sha1_to_hex(repl));
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700262 return NULL;
263 }
264 parse_blob_buffer(lookup_blob(sha1), NULL, 0);
265 return lookup_object(sha1);
266 }
267
Jeff Kingccdc6032012-01-05 16:00:01 -0500268 buffer = read_sha1_file(sha1, &type, &size);
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700269 if (buffer) {
Christian Couder0e87c362009-01-23 10:07:10 +0100270 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
Carlos Rica0b1f1132007-05-25 03:46:22 +0200271 free(buffer);
Pete Wyckoff82247e92012-04-29 20:28:45 -0400272 error("sha1 mismatch %s", sha1_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 10:05:20 -0700273 return NULL;
274 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700275
Nguyễn Thái Ngọc Duy2e3400c2010-09-03 22:51:53 +0200276 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700277 if (!eaten)
278 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400279 return obj;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700280 }
281 return NULL;
282}
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400283
284struct object_list *object_list_insert(struct object *item,
285 struct object_list **list_p)
286{
287 struct object_list *new_list = xmalloc(sizeof(struct object_list));
Jared Hance55b4e9e2010-09-05 15:36:33 -0400288 new_list->item = item;
289 new_list->next = *list_p;
290 *list_p = new_list;
291 return new_list;
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400292}
293
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400294int object_list_contains(struct object_list *list, struct object *obj)
295{
296 while (list) {
297 if (list->item == obj)
298 return 1;
299 list = list->next;
300 }
301 return 0;
302}
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700303
Michael Haggerty31faeb22013-05-25 11:08:14 +0200304/*
305 * A zero-length string to which object_array_entry::name can be
306 * initialized without requiring a malloc/free.
307 */
308static char object_array_slopbuf[1];
309
Jeff King9e0c3c42014-10-15 18:42:57 -0400310void add_object_array_with_path(struct object *obj, const char *name,
311 struct object_array *array,
312 unsigned mode, const char *path)
Martin Koeglere5709a42007-04-22 18:43:58 +0200313{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700314 unsigned nr = array->nr;
315 unsigned alloc = array->alloc;
316 struct object_array_entry *objects = array->objects;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200317 struct object_array_entry *entry;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700318
319 if (nr >= alloc) {
320 alloc = (alloc + 32) * 2;
René Scharfe2756ca42014-09-16 20:56:57 +0200321 REALLOC_ARRAY(objects, alloc);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700322 array->alloc = alloc;
323 array->objects = objects;
324 }
Michael Haggerty31faeb22013-05-25 11:08:14 +0200325 entry = &objects[nr];
326 entry->item = obj;
327 if (!name)
328 entry->name = NULL;
329 else if (!*name)
330 /* Use our own empty string instead of allocating one: */
331 entry->name = object_array_slopbuf;
332 else
333 entry->name = xstrdup(name);
334 entry->mode = mode;
Jeff King9e0c3c42014-10-15 18:42:57 -0400335 if (path)
336 entry->path = xstrdup(path);
337 else
338 entry->path = NULL;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700339 array->nr = ++nr;
340}
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800341
Michael J Gruberafa15f32013-05-10 17:10:16 +0200342void add_object_array(struct object *obj, const char *name, struct object_array *array)
343{
Jeff King189a1222014-10-18 22:03:19 -0400344 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
Michael J Gruberafa15f32013-05-10 17:10:16 +0200345}
346
Jeff King68f49232014-10-15 18:34:19 -0400347/*
348 * Free all memory associated with an entry; the result is
349 * in an unspecified state and should not be examined.
350 */
351static void object_array_release_entry(struct object_array_entry *ent)
352{
353 if (ent->name != object_array_slopbuf)
354 free(ent->name);
Jeff King9e0c3c42014-10-15 18:42:57 -0400355 free(ent->path);
Jeff King68f49232014-10-15 18:34:19 -0400356}
357
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200358void object_array_filter(struct object_array *array,
359 object_array_each_func_t want, void *cb_data)
360{
361 unsigned nr = array->nr, src, dst;
362 struct object_array_entry *objects = array->objects;
363
364 for (src = dst = 0; src < nr; src++) {
365 if (want(&objects[src], cb_data)) {
366 if (src != dst)
367 objects[dst] = objects[src];
368 dst++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200369 } else {
Jeff King68f49232014-10-15 18:34:19 -0400370 object_array_release_entry(&objects[src]);
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200371 }
372 }
373 array->nr = dst;
374}
375
Jeff King46be8232014-10-15 18:34:34 -0400376void object_array_clear(struct object_array *array)
377{
378 int i;
379 for (i = 0; i < array->nr; i++)
380 object_array_release_entry(&array->objects[i]);
381 free(array->objects);
382 array->objects = NULL;
383 array->nr = array->alloc = 0;
384}
385
Michael Haggerty15065102013-05-25 11:08:10 +0200386/*
387 * Return true iff array already contains an entry with name.
388 */
389static int contains_name(struct object_array *array, const char *name)
390{
391 unsigned nr = array->nr, i;
392 struct object_array_entry *object = array->objects;
393
394 for (i = 0; i < nr; i++, object++)
395 if (!strcmp(object->name, name))
396 return 1;
397 return 0;
398}
399
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800400void object_array_remove_duplicates(struct object_array *array)
401{
Michael Haggerty15065102013-05-25 11:08:10 +0200402 unsigned nr = array->nr, src;
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800403 struct object_array_entry *objects = array->objects;
404
Michael Haggerty15065102013-05-25 11:08:10 +0200405 array->nr = 0;
406 for (src = 0; src < nr; src++) {
407 if (!contains_name(array, objects[src].name)) {
408 if (src != array->nr)
409 objects[array->nr] = objects[src];
410 array->nr++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200411 } else {
Jeff King68f49232014-10-15 18:34:19 -0400412 object_array_release_entry(&objects[src]);
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800413 }
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800414 }
415}
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200416
417void clear_object_flags(unsigned flags)
418{
419 int i;
420
421 for (i=0; i < obj_hash_size; i++) {
422 struct object *obj = obj_hash[i];
423 if (obj)
424 obj->flags &= ~flags;
425 }
426}