Junio C Hamano | 8f1d2e6 | 2006-01-07 01:33:54 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 2 | #include "object.h" |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 3 | #include "blob.h" |
| 4 | #include "tree.h" |
| 5 | #include "commit.h" |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 6 | #include "tag.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 7 | |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 8 | static struct object **obj_hash; |
| 9 | static int nr_objs, obj_hash_size; |
Linus Torvalds | fc046a7 | 2006-06-29 21:38:55 -0700 | [diff] [blame] | 10 | |
| 11 | unsigned int get_max_object_index(void) |
| 12 | { |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 13 | return obj_hash_size; |
Linus Torvalds | fc046a7 | 2006-06-29 21:38:55 -0700 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | struct object *get_indexed_object(unsigned int idx) |
| 17 | { |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 18 | return obj_hash[idx]; |
Linus Torvalds | fc046a7 | 2006-06-29 21:38:55 -0700 | [diff] [blame] | 19 | } |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 20 | |
Nicolas Pitre | df84366 | 2007-02-26 14:55:58 -0500 | [diff] [blame] | 21 | static 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 Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Nicolas Pitre | df84366 | 2007-02-26 14:55:58 -0500 | [diff] [blame] | 29 | const 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 | |
| 36 | int 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 | |
Michael Haggerty | 33bef7e | 2014-02-28 17:29:17 +0100 | [diff] [blame] | 46 | /* |
| 47 | * Return a numerical hash value between 0 and n-1 for the object with |
| 48 | * the specified sha1. n must be a power of 2. Please note that the |
| 49 | * return value is *not* consistent across computer architectures. |
| 50 | */ |
Nicolas Pitre | 9f36c9b | 2013-09-10 18:17:12 -0400 | [diff] [blame] | 51 | static unsigned int hash_obj(const unsigned char *sha1, unsigned int n) |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 52 | { |
Dan McGee | b867d32 | 2009-05-11 20:17:38 -0500 | [diff] [blame] | 53 | unsigned int hash; |
Michael Haggerty | 33bef7e | 2014-02-28 17:29:17 +0100 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Since the sha1 is essentially random, we just take the |
| 57 | * required number of bits directly from the first |
| 58 | * sizeof(unsigned int) bytes of sha1. First we have to copy |
| 59 | * the bytes into a properly aligned integer. If we cared |
| 60 | * about getting consistent results across architectures, we |
| 61 | * would have to call ntohl() here, too. |
| 62 | */ |
Nicolas Pitre | 9f36c9b | 2013-09-10 18:17:12 -0400 | [diff] [blame] | 63 | memcpy(&hash, sha1, sizeof(unsigned int)); |
Nicolas Pitre | 9f36c9b | 2013-09-10 18:17:12 -0400 | [diff] [blame] | 64 | return hash & (n - 1); |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Michael Haggerty | 33bef7e | 2014-02-28 17:29:17 +0100 | [diff] [blame] | 67 | /* |
| 68 | * Insert obj into the hash table hash, which has length size (which |
| 69 | * must be a power of 2). On collisions, simply overflow to the next |
| 70 | * empty bucket. |
| 71 | */ |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 72 | static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size) |
| 73 | { |
Nicolas Pitre | 9f36c9b | 2013-09-10 18:17:12 -0400 | [diff] [blame] | 74 | unsigned int j = hash_obj(obj->sha1, size); |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 75 | |
| 76 | while (hash[j]) { |
| 77 | j++; |
| 78 | if (j >= size) |
| 79 | j = 0; |
| 80 | } |
| 81 | hash[j] = obj; |
| 82 | } |
| 83 | |
Michael Haggerty | 33bef7e | 2014-02-28 17:29:17 +0100 | [diff] [blame] | 84 | /* |
| 85 | * Look up the record for the given sha1 in the hash map stored in |
| 86 | * obj_hash. Return NULL if it was not found. |
| 87 | */ |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 88 | struct object *lookup_object(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 89 | { |
Jeff King | 9a41448 | 2013-05-01 16:34:50 -0400 | [diff] [blame] | 90 | unsigned int i, first; |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 91 | struct object *obj; |
| 92 | |
| 93 | if (!obj_hash) |
| 94 | return NULL; |
| 95 | |
Nicolas Pitre | 9f36c9b | 2013-09-10 18:17:12 -0400 | [diff] [blame] | 96 | first = i = hash_obj(sha1, obj_hash_size); |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 97 | while ((obj = obj_hash[i]) != NULL) { |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 98 | if (!hashcmp(sha1, obj->sha1)) |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 99 | break; |
| 100 | i++; |
| 101 | if (i == obj_hash_size) |
| 102 | i = 0; |
| 103 | } |
Jeff King | 9a41448 | 2013-05-01 16:34:50 -0400 | [diff] [blame] | 104 | if (obj && i != first) { |
| 105 | /* |
| 106 | * Move object to where we started to look for it so |
| 107 | * that we do not need to walk the hash table the next |
| 108 | * time we look for it. |
| 109 | */ |
| 110 | struct object *tmp = obj_hash[i]; |
| 111 | obj_hash[i] = obj_hash[first]; |
| 112 | obj_hash[first] = tmp; |
| 113 | } |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 114 | return obj; |
| 115 | } |
| 116 | |
Michael Haggerty | 33bef7e | 2014-02-28 17:29:17 +0100 | [diff] [blame] | 117 | /* |
| 118 | * Increase the size of the hash map stored in obj_hash to the next |
| 119 | * power of 2 (but at least 32). Copy the existing values to the new |
| 120 | * hash map. |
| 121 | */ |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 122 | static void grow_object_hash(void) |
| 123 | { |
| 124 | int i; |
Nicolas Pitre | 9f36c9b | 2013-09-10 18:17:12 -0400 | [diff] [blame] | 125 | /* |
| 126 | * Note that this size must always be power-of-2 to match hash_obj |
| 127 | * above. |
| 128 | */ |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 129 | int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size; |
| 130 | struct object **new_hash; |
| 131 | |
Jonas Fonseca | b3c952f | 2006-08-28 02:26:07 +0200 | [diff] [blame] | 132 | new_hash = xcalloc(new_hash_size, sizeof(struct object *)); |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 133 | for (i = 0; i < obj_hash_size; i++) { |
| 134 | struct object *obj = obj_hash[i]; |
| 135 | if (!obj) |
| 136 | continue; |
| 137 | insert_obj_hash(obj, new_hash, new_hash_size); |
| 138 | } |
| 139 | free(obj_hash); |
| 140 | obj_hash = new_hash; |
| 141 | obj_hash_size = new_hash_size; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Jeff King | fe24d39 | 2014-07-13 02:41:55 -0400 | [diff] [blame] | 144 | void *create_object(const unsigned char *sha1, void *o) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 145 | { |
Linus Torvalds | 100c5f3 | 2007-04-16 22:11:43 -0700 | [diff] [blame] | 146 | struct object *obj = o; |
| 147 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 148 | obj->parsed = 0; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 149 | obj->used = 0; |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 150 | obj->flags = 0; |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 151 | hashcpy(obj->sha1, sha1); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 152 | |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 153 | if (obj_hash_size - 1 <= nr_objs * 2) |
| 154 | grow_object_hash(); |
Johannes Schindelin | 070879c | 2006-02-12 02:57:57 +0100 | [diff] [blame] | 155 | |
Linus Torvalds | 0556a11 | 2006-06-30 11:20:33 -0700 | [diff] [blame] | 156 | insert_obj_hash(obj, obj_hash, obj_hash_size); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 157 | nr_objs++; |
Linus Torvalds | 100c5f3 | 2007-04-16 22:11:43 -0700 | [diff] [blame] | 158 | return obj; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Jeff King | c4ad00f | 2014-07-13 02:42:03 -0400 | [diff] [blame] | 161 | void *object_as_type(struct object *obj, enum object_type type, int quiet) |
| 162 | { |
| 163 | if (obj->type == type) |
| 164 | return obj; |
| 165 | else if (obj->type == OBJ_NONE) { |
Jeff King | 34dfe19 | 2014-07-13 02:42:12 -0400 | [diff] [blame] | 166 | if (type == OBJ_COMMIT) |
| 167 | ((struct commit *)obj)->index = alloc_commit_index(); |
Jeff King | c4ad00f | 2014-07-13 02:42:03 -0400 | [diff] [blame] | 168 | obj->type = type; |
| 169 | return obj; |
| 170 | } |
| 171 | else { |
| 172 | if (!quiet) |
| 173 | error("object %s is a %s, not a %s", |
| 174 | sha1_to_hex(obj->sha1), |
| 175 | typename(obj->type), typename(type)); |
| 176 | return NULL; |
| 177 | } |
| 178 | } |
| 179 | |
barkalow@iabervon.org | 66e481b | 2005-08-02 19:45:48 -0400 | [diff] [blame] | 180 | struct object *lookup_unknown_object(const unsigned char *sha1) |
| 181 | { |
| 182 | struct object *obj = lookup_object(sha1); |
Linus Torvalds | 100c5f3 | 2007-04-16 22:11:43 -0700 | [diff] [blame] | 183 | if (!obj) |
Jeff King | fe24d39 | 2014-07-13 02:41:55 -0400 | [diff] [blame] | 184 | obj = create_object(sha1, alloc_object_node()); |
barkalow@iabervon.org | 66e481b | 2005-08-02 19:45:48 -0400 | [diff] [blame] | 185 | return obj; |
| 186 | } |
| 187 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 188 | struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p) |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 189 | { |
| 190 | struct object *obj; |
Stefan Beller | 8e92e8f | 2013-07-18 00:09:42 +0200 | [diff] [blame] | 191 | *eaten_p = 0; |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 192 | |
Jim Meyering | cc21682 | 2007-12-21 11:56:32 +0100 | [diff] [blame] | 193 | obj = NULL; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 194 | if (type == OBJ_BLOB) { |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 195 | struct blob *blob = lookup_blob(sha1); |
Jim Meyering | cc21682 | 2007-12-21 11:56:32 +0100 | [diff] [blame] | 196 | if (blob) { |
Martin Koegler | d0b8c9e | 2008-02-03 22:22:39 +0100 | [diff] [blame] | 197 | if (parse_blob_buffer(blob, buffer, size)) |
| 198 | return NULL; |
Jim Meyering | cc21682 | 2007-12-21 11:56:32 +0100 | [diff] [blame] | 199 | obj = &blob->object; |
| 200 | } |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 201 | } else if (type == OBJ_TREE) { |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 202 | struct tree *tree = lookup_tree(sha1); |
Jim Meyering | cc21682 | 2007-12-21 11:56:32 +0100 | [diff] [blame] | 203 | if (tree) { |
| 204 | obj = &tree->object; |
Junio C Hamano | 68be2fe | 2011-11-16 22:04:13 -0800 | [diff] [blame] | 205 | if (!tree->buffer) |
| 206 | tree->object.parsed = 0; |
Jim Meyering | cc21682 | 2007-12-21 11:56:32 +0100 | [diff] [blame] | 207 | if (!tree->object.parsed) { |
Martin Koegler | d0b8c9e | 2008-02-03 22:22:39 +0100 | [diff] [blame] | 208 | if (parse_tree_buffer(tree, buffer, size)) |
| 209 | return NULL; |
Stefan Beller | 8e92e8f | 2013-07-18 00:09:42 +0200 | [diff] [blame] | 210 | *eaten_p = 1; |
Jim Meyering | cc21682 | 2007-12-21 11:56:32 +0100 | [diff] [blame] | 211 | } |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 212 | } |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 213 | } else if (type == OBJ_COMMIT) { |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 214 | struct commit *commit = lookup_commit(sha1); |
Jim Meyering | cc21682 | 2007-12-21 11:56:32 +0100 | [diff] [blame] | 215 | if (commit) { |
Martin Koegler | d0b8c9e | 2008-02-03 22:22:39 +0100 | [diff] [blame] | 216 | if (parse_commit_buffer(commit, buffer, size)) |
| 217 | return NULL; |
Jeff King | 8597ea3 | 2014-06-10 17:44:13 -0400 | [diff] [blame] | 218 | if (!get_cached_commit_buffer(commit, NULL)) { |
| 219 | set_commit_buffer(commit, buffer, size); |
Stefan Beller | 8e92e8f | 2013-07-18 00:09:42 +0200 | [diff] [blame] | 220 | *eaten_p = 1; |
Jim Meyering | cc21682 | 2007-12-21 11:56:32 +0100 | [diff] [blame] | 221 | } |
| 222 | obj = &commit->object; |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 223 | } |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 224 | } else if (type == OBJ_TAG) { |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 225 | struct tag *tag = lookup_tag(sha1); |
Jim Meyering | cc21682 | 2007-12-21 11:56:32 +0100 | [diff] [blame] | 226 | if (tag) { |
Martin Koegler | d0b8c9e | 2008-02-03 22:22:39 +0100 | [diff] [blame] | 227 | if (parse_tag_buffer(tag, buffer, size)) |
| 228 | return NULL; |
Jim Meyering | cc21682 | 2007-12-21 11:56:32 +0100 | [diff] [blame] | 229 | obj = &tag->object; |
| 230 | } |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 231 | } else { |
Pete Wyckoff | 82247e9 | 2012-04-29 20:28:45 -0400 | [diff] [blame] | 232 | warning("object %s has unknown type id %d", sha1_to_hex(sha1), type); |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 233 | obj = NULL; |
| 234 | } |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 235 | return obj; |
| 236 | } |
| 237 | |
Jeff King | 75a9549 | 2013-03-17 04:22:36 -0400 | [diff] [blame] | 238 | struct object *parse_object_or_die(const unsigned char *sha1, |
| 239 | const char *name) |
| 240 | { |
| 241 | struct object *o = parse_object(sha1); |
| 242 | if (o) |
| 243 | return o; |
| 244 | |
| 245 | die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1)); |
| 246 | } |
| 247 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 248 | struct object *parse_object(const unsigned char *sha1) |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 249 | { |
Junio C Hamano | c4584ae | 2005-06-27 03:33:33 -0700 | [diff] [blame] | 250 | unsigned long size; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 251 | enum object_type type; |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 252 | int eaten; |
Junio C Hamano | 4bbf5a2 | 2011-05-15 12:54:52 -0700 | [diff] [blame] | 253 | const unsigned char *repl = lookup_replace_object(sha1); |
Jeff King | ccdc603 | 2012-01-05 16:00:01 -0500 | [diff] [blame] | 254 | void *buffer; |
| 255 | struct object *obj; |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 256 | |
Jeff King | ccdc603 | 2012-01-05 16:00:01 -0500 | [diff] [blame] | 257 | obj = lookup_object(sha1); |
| 258 | if (obj && obj->parsed) |
| 259 | return obj; |
| 260 | |
Nguyễn Thái Ngọc Duy | 090ea12 | 2012-03-07 17:54:18 +0700 | [diff] [blame] | 261 | if ((obj && obj->type == OBJ_BLOB) || |
| 262 | (!obj && has_sha1_file(sha1) && |
| 263 | sha1_object_info(sha1, NULL) == OBJ_BLOB)) { |
| 264 | if (check_sha1_signature(repl, NULL, 0, NULL) < 0) { |
Pete Wyckoff | 82247e9 | 2012-04-29 20:28:45 -0400 | [diff] [blame] | 265 | error("sha1 mismatch %s", sha1_to_hex(repl)); |
Nguyễn Thái Ngọc Duy | 090ea12 | 2012-03-07 17:54:18 +0700 | [diff] [blame] | 266 | return NULL; |
| 267 | } |
| 268 | parse_blob_buffer(lookup_blob(sha1), NULL, 0); |
| 269 | return lookup_object(sha1); |
| 270 | } |
| 271 | |
Jeff King | ccdc603 | 2012-01-05 16:00:01 -0500 | [diff] [blame] | 272 | buffer = read_sha1_file(sha1, &type, &size); |
Junio C Hamano | c4584ae | 2005-06-27 03:33:33 -0700 | [diff] [blame] | 273 | if (buffer) { |
Christian Couder | 0e87c36 | 2009-01-23 10:07:10 +0100 | [diff] [blame] | 274 | if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) { |
Carlos Rica | 0b1f113 | 2007-05-25 03:46:22 +0200 | [diff] [blame] | 275 | free(buffer); |
Pete Wyckoff | 82247e9 | 2012-04-29 20:28:45 -0400 | [diff] [blame] | 276 | error("sha1 mismatch %s", sha1_to_hex(repl)); |
Linus Torvalds | acdeec6 | 2007-03-20 10:05:20 -0700 | [diff] [blame] | 277 | return NULL; |
| 278 | } |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 279 | |
Nguyễn Thái Ngọc Duy | 2e3400c | 2010-09-03 22:51:53 +0200 | [diff] [blame] | 280 | obj = parse_object_buffer(sha1, type, size, buffer, &eaten); |
Junio C Hamano | 9f613dd | 2006-09-15 13:30:02 -0700 | [diff] [blame] | 281 | if (!eaten) |
| 282 | free(buffer); |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 283 | return obj; |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 284 | } |
| 285 | return NULL; |
| 286 | } |
barkalow@iabervon.org | 66e481b | 2005-08-02 19:45:48 -0400 | [diff] [blame] | 287 | |
| 288 | struct object_list *object_list_insert(struct object *item, |
| 289 | struct object_list **list_p) |
| 290 | { |
| 291 | struct object_list *new_list = xmalloc(sizeof(struct object_list)); |
Jared Hance | 55b4e9e | 2010-09-05 15:36:33 -0400 | [diff] [blame] | 292 | new_list->item = item; |
| 293 | new_list->next = *list_p; |
| 294 | *list_p = new_list; |
| 295 | return new_list; |
barkalow@iabervon.org | 66e481b | 2005-08-02 19:45:48 -0400 | [diff] [blame] | 296 | } |
| 297 | |
barkalow@iabervon.org | 66e481b | 2005-08-02 19:45:48 -0400 | [diff] [blame] | 298 | int object_list_contains(struct object_list *list, struct object *obj) |
| 299 | { |
| 300 | while (list) { |
| 301 | if (list->item == obj) |
| 302 | return 1; |
| 303 | list = list->next; |
| 304 | } |
| 305 | return 0; |
| 306 | } |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 307 | |
Michael Haggerty | 31faeb2 | 2013-05-25 11:08:14 +0200 | [diff] [blame] | 308 | /* |
| 309 | * A zero-length string to which object_array_entry::name can be |
| 310 | * initialized without requiring a malloc/free. |
| 311 | */ |
| 312 | static char object_array_slopbuf[1]; |
| 313 | |
Junio C Hamano | 4197361 | 2013-10-23 13:21:30 -0700 | [diff] [blame] | 314 | static void add_object_array_with_mode_context(struct object *obj, const char *name, |
| 315 | struct object_array *array, |
| 316 | unsigned mode, |
| 317 | struct object_context *context) |
Martin Koegler | e5709a4 | 2007-04-22 18:43:58 +0200 | [diff] [blame] | 318 | { |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 319 | unsigned nr = array->nr; |
| 320 | unsigned alloc = array->alloc; |
| 321 | struct object_array_entry *objects = array->objects; |
Michael Haggerty | 31faeb2 | 2013-05-25 11:08:14 +0200 | [diff] [blame] | 322 | struct object_array_entry *entry; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 323 | |
| 324 | if (nr >= alloc) { |
| 325 | alloc = (alloc + 32) * 2; |
| 326 | objects = xrealloc(objects, alloc * sizeof(*objects)); |
| 327 | array->alloc = alloc; |
| 328 | array->objects = objects; |
| 329 | } |
Michael Haggerty | 31faeb2 | 2013-05-25 11:08:14 +0200 | [diff] [blame] | 330 | entry = &objects[nr]; |
| 331 | entry->item = obj; |
| 332 | if (!name) |
| 333 | entry->name = NULL; |
| 334 | else if (!*name) |
| 335 | /* Use our own empty string instead of allocating one: */ |
| 336 | entry->name = object_array_slopbuf; |
| 337 | else |
| 338 | entry->name = xstrdup(name); |
| 339 | entry->mode = mode; |
Junio C Hamano | 4197361 | 2013-10-23 13:21:30 -0700 | [diff] [blame] | 340 | entry->context = context; |
Linus Torvalds | 1f1e895 | 2006-06-19 17:42:35 -0700 | [diff] [blame] | 341 | array->nr = ++nr; |
| 342 | } |
Junio C Hamano | b2a6d1c | 2009-01-17 22:27:08 -0800 | [diff] [blame] | 343 | |
Michael J Gruber | afa15f3 | 2013-05-10 17:10:16 +0200 | [diff] [blame] | 344 | void add_object_array(struct object *obj, const char *name, struct object_array *array) |
| 345 | { |
| 346 | add_object_array_with_mode(obj, name, array, S_IFINVALID); |
| 347 | } |
| 348 | |
| 349 | void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode) |
| 350 | { |
| 351 | add_object_array_with_mode_context(obj, name, array, mode, NULL); |
| 352 | } |
| 353 | |
| 354 | void add_object_array_with_context(struct object *obj, const char *name, struct object_array *array, struct object_context *context) |
| 355 | { |
| 356 | if (context) |
| 357 | add_object_array_with_mode_context(obj, name, array, context->mode, context); |
| 358 | else |
| 359 | add_object_array_with_mode_context(obj, name, array, S_IFINVALID, context); |
| 360 | } |
| 361 | |
Michael Haggerty | aeb4a51 | 2013-05-25 11:08:08 +0200 | [diff] [blame] | 362 | void object_array_filter(struct object_array *array, |
| 363 | object_array_each_func_t want, void *cb_data) |
| 364 | { |
| 365 | unsigned nr = array->nr, src, dst; |
| 366 | struct object_array_entry *objects = array->objects; |
| 367 | |
| 368 | for (src = dst = 0; src < nr; src++) { |
| 369 | if (want(&objects[src], cb_data)) { |
| 370 | if (src != dst) |
| 371 | objects[dst] = objects[src]; |
| 372 | dst++; |
Michael Haggerty | 31faeb2 | 2013-05-25 11:08:14 +0200 | [diff] [blame] | 373 | } else { |
| 374 | if (objects[src].name != object_array_slopbuf) |
| 375 | free(objects[src].name); |
Michael Haggerty | aeb4a51 | 2013-05-25 11:08:08 +0200 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | array->nr = dst; |
| 379 | } |
| 380 | |
Michael Haggerty | 1506510 | 2013-05-25 11:08:10 +0200 | [diff] [blame] | 381 | /* |
| 382 | * Return true iff array already contains an entry with name. |
| 383 | */ |
| 384 | static int contains_name(struct object_array *array, const char *name) |
| 385 | { |
| 386 | unsigned nr = array->nr, i; |
| 387 | struct object_array_entry *object = array->objects; |
| 388 | |
| 389 | for (i = 0; i < nr; i++, object++) |
| 390 | if (!strcmp(object->name, name)) |
| 391 | return 1; |
| 392 | return 0; |
| 393 | } |
| 394 | |
Junio C Hamano | b2a6d1c | 2009-01-17 22:27:08 -0800 | [diff] [blame] | 395 | void object_array_remove_duplicates(struct object_array *array) |
| 396 | { |
Michael Haggerty | 1506510 | 2013-05-25 11:08:10 +0200 | [diff] [blame] | 397 | unsigned nr = array->nr, src; |
Junio C Hamano | b2a6d1c | 2009-01-17 22:27:08 -0800 | [diff] [blame] | 398 | struct object_array_entry *objects = array->objects; |
| 399 | |
Michael Haggerty | 1506510 | 2013-05-25 11:08:10 +0200 | [diff] [blame] | 400 | array->nr = 0; |
| 401 | for (src = 0; src < nr; src++) { |
| 402 | if (!contains_name(array, objects[src].name)) { |
| 403 | if (src != array->nr) |
| 404 | objects[array->nr] = objects[src]; |
| 405 | array->nr++; |
Michael Haggerty | 31faeb2 | 2013-05-25 11:08:14 +0200 | [diff] [blame] | 406 | } else { |
| 407 | if (objects[src].name != object_array_slopbuf) |
| 408 | free(objects[src].name); |
Junio C Hamano | b2a6d1c | 2009-01-17 22:27:08 -0800 | [diff] [blame] | 409 | } |
Junio C Hamano | b2a6d1c | 2009-01-17 22:27:08 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
Heiko Voigt | bcc0a3e | 2012-03-29 09:21:21 +0200 | [diff] [blame] | 412 | |
| 413 | void clear_object_flags(unsigned flags) |
| 414 | { |
| 415 | int i; |
| 416 | |
| 417 | for (i=0; i < obj_hash_size; i++) { |
| 418 | struct object *obj = obj_hash[i]; |
| 419 | if (obj) |
| 420 | obj->flags &= ~flags; |
| 421 | } |
| 422 | } |