]> git.ipfire.org Git - thirdparty/git.git/blame - object.c
replace-object: move replace_map to object store
[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"
90c62155 7#include "object-store.h"
d0b59866 8#include "packfile.h"
175785e5 9
0556a11a
LT
10static struct object **obj_hash;
11static int nr_objs, obj_hash_size;
fc046a75
LT
12
13unsigned int get_max_object_index(void)
14{
0556a11a 15 return obj_hash_size;
fc046a75
LT
16}
17
18struct object *get_indexed_object(unsigned int idx)
19{
0556a11a 20 return obj_hash[idx];
fc046a75 21}
175785e5 22
df843662
NP
23static const char *object_type_strings[] = {
24 NULL, /* OBJ_NONE = 0 */
25 "commit", /* OBJ_COMMIT = 1 */
26 "tree", /* OBJ_TREE = 2 */
27 "blob", /* OBJ_BLOB = 3 */
28 "tag", /* OBJ_TAG = 4 */
885a86ab
LT
29};
30
debca9d2 31const char *type_name(unsigned int type)
df843662
NP
32{
33 if (type >= ARRAY_SIZE(object_type_strings))
34 return NULL;
35 return object_type_strings[type];
36}
37
fe8e3b71 38int type_from_string_gently(const char *str, ssize_t len, int gentle)
df843662
NP
39{
40 int i;
41
fe8e3b71
JS
42 if (len < 0)
43 len = strlen(str);
44
df843662 45 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
b7994af0
JK
46 if (!strncmp(str, object_type_strings[i], len) &&
47 object_type_strings[i][len] == '\0')
df843662 48 return i;
fe8e3b71
JS
49
50 if (gentle)
51 return -1;
52
df843662
NP
53 die("invalid object type \"%s\"", str);
54}
55
33bef7ea
MH
56/*
57 * Return a numerical hash value between 0 and n-1 for the object with
58 * the specified sha1. n must be a power of 2. Please note that the
59 * return value is *not* consistent across computer architectures.
60 */
9f36c9b7 61static unsigned int hash_obj(const unsigned char *sha1, unsigned int n)
0556a11a 62{
039dc71a 63 return sha1hash(sha1) & (n - 1);
0556a11a
LT
64}
65
33bef7ea
MH
66/*
67 * Insert obj into the hash table hash, which has length size (which
68 * must be a power of 2). On collisions, simply overflow to the next
69 * empty bucket.
70 */
0556a11a
LT
71static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
72{
ed1c9977 73 unsigned int j = hash_obj(obj->oid.hash, size);
0556a11a
LT
74
75 while (hash[j]) {
76 j++;
77 if (j >= size)
78 j = 0;
79 }
80 hash[j] = obj;
81}
82
33bef7ea
MH
83/*
84 * Look up the record for the given sha1 in the hash map stored in
85 * obj_hash. Return NULL if it was not found.
86 */
0556a11a 87struct object *lookup_object(const unsigned char *sha1)
175785e5 88{
9a414486 89 unsigned int i, first;
0556a11a 90 struct object *obj;
175785e5 91
0556a11a
LT
92 if (!obj_hash)
93 return NULL;
175785e5 94
9f36c9b7 95 first = i = hash_obj(sha1, obj_hash_size);
0556a11a 96 while ((obj = obj_hash[i]) != NULL) {
ed1c9977 97 if (!hashcmp(sha1, obj->oid.hash))
0556a11a 98 break;
070879ca 99 i++;
0556a11a 100 if (i == obj_hash_size)
070879ca
JS
101 i = 0;
102 }
9a414486
JK
103 if (obj && i != first) {
104 /*
105 * Move object to where we started to look for it so
106 * that we do not need to walk the hash table the next
107 * time we look for it.
108 */
35d803bc 109 SWAP(obj_hash[i], obj_hash[first]);
9a414486 110 }
0556a11a 111 return obj;
175785e5
DB
112}
113
33bef7ea
MH
114/*
115 * Increase the size of the hash map stored in obj_hash to the next
116 * power of 2 (but at least 32). Copy the existing values to the new
117 * hash map.
118 */
0556a11a 119static void grow_object_hash(void)
175785e5 120{
0556a11a 121 int i;
9f36c9b7
NP
122 /*
123 * Note that this size must always be power-of-2 to match hash_obj
124 * above.
125 */
0556a11a
LT
126 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
127 struct object **new_hash;
128
b3c952f8 129 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
0556a11a
LT
130 for (i = 0; i < obj_hash_size; i++) {
131 struct object *obj = obj_hash[i];
132 if (!obj)
133 continue;
134 insert_obj_hash(obj, new_hash, new_hash_size);
135 }
136 free(obj_hash);
137 obj_hash = new_hash;
138 obj_hash_size = new_hash_size;
175785e5
DB
139}
140
d36f51c1 141void *create_object(const unsigned char *sha1, void *o)
175785e5 142{
100c5f3b
LT
143 struct object *obj = o;
144
175785e5 145 obj->parsed = 0;
0556a11a 146 obj->flags = 0;
ed1c9977 147 hashcpy(obj->oid.hash, sha1);
175785e5 148
0556a11a
LT
149 if (obj_hash_size - 1 <= nr_objs * 2)
150 grow_object_hash();
175785e5 151
0556a11a 152 insert_obj_hash(obj, obj_hash, obj_hash_size);
175785e5 153 nr_objs++;
100c5f3b 154 return obj;
175785e5
DB
155}
156
8ff226a9
JK
157void *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) {
d66bebcb
JK
162 if (type == OBJ_COMMIT)
163 ((struct commit *)obj)->index = alloc_commit_index();
8ff226a9
JK
164 obj->type = type;
165 return obj;
166 }
167 else {
168 if (!quiet)
169 error("object %s is a %s, not a %s",
f2fd0760 170 oid_to_hex(&obj->oid),
debca9d2 171 type_name(obj->type), type_name(type));
8ff226a9
JK
172 return NULL;
173 }
174}
175
66e481b0
DB
176struct object *lookup_unknown_object(const unsigned char *sha1)
177{
178 struct object *obj = lookup_object(sha1);
100c5f3b 179 if (!obj)
d36f51c1 180 obj = create_object(sha1, alloc_object_node());
66e481b0
DB
181 return obj;
182}
183
c251c83d 184struct object *parse_object_buffer(const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
9f613ddd
JH
185{
186 struct object *obj;
8e92e8f2 187 *eaten_p = 0;
9f613ddd 188
cc216827 189 obj = NULL;
21666f1a 190 if (type == OBJ_BLOB) {
c251c83d 191 struct blob *blob = lookup_blob(oid);
cc216827 192 if (blob) {
d0b8c9e5
MK
193 if (parse_blob_buffer(blob, buffer, size))
194 return NULL;
cc216827
JM
195 obj = &blob->object;
196 }
21666f1a 197 } else if (type == OBJ_TREE) {
c251c83d 198 struct tree *tree = lookup_tree(oid);
cc216827
JM
199 if (tree) {
200 obj = &tree->object;
68be2fea
JH
201 if (!tree->buffer)
202 tree->object.parsed = 0;
cc216827 203 if (!tree->object.parsed) {
d0b8c9e5
MK
204 if (parse_tree_buffer(tree, buffer, size))
205 return NULL;
8e92e8f2 206 *eaten_p = 1;
cc216827 207 }
9f613ddd 208 }
21666f1a 209 } else if (type == OBJ_COMMIT) {
c251c83d 210 struct commit *commit = lookup_commit(oid);
cc216827 211 if (commit) {
d0b8c9e5
MK
212 if (parse_commit_buffer(commit, buffer, size))
213 return NULL;
8597ea3a
JK
214 if (!get_cached_commit_buffer(commit, NULL)) {
215 set_commit_buffer(commit, buffer, size);
8e92e8f2 216 *eaten_p = 1;
cc216827
JM
217 }
218 obj = &commit->object;
9f613ddd 219 }
21666f1a 220 } else if (type == OBJ_TAG) {
c251c83d 221 struct tag *tag = lookup_tag(oid);
cc216827 222 if (tag) {
d0b8c9e5
MK
223 if (parse_tag_buffer(tag, buffer, size))
224 return NULL;
cc216827
JM
225 obj = &tag->object;
226 }
9f613ddd 227 } else {
c251c83d 228 warning("object %s has unknown type id %d", oid_to_hex(oid), type);
9f613ddd
JH
229 obj = NULL;
230 }
9f613ddd
JH
231 return obj;
232}
233
c251c83d 234struct object *parse_object_or_die(const struct object_id *oid,
75a95490
JK
235 const char *name)
236{
c251c83d 237 struct object *o = parse_object(oid);
75a95490
JK
238 if (o)
239 return o;
240
c251c83d 241 die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
75a95490
JK
242}
243
c251c83d 244struct object *parse_object(const struct object_id *oid)
e9eefa67 245{
c4584ae3 246 unsigned long size;
21666f1a 247 enum object_type type;
9f613ddd 248 int eaten;
b383a13c 249 const struct object_id *repl = lookup_replace_object(oid);
ccdc6037
JK
250 void *buffer;
251 struct object *obj;
252
c251c83d 253 obj = lookup_object(oid->hash);
ccdc6037
JK
254 if (obj && obj->parsed)
255 return obj;
9f613ddd 256
df11e196 257 if ((obj && obj->type == OBJ_BLOB && has_object_file(oid)) ||
c251c83d 258 (!obj && has_object_file(oid) &&
abef9020 259 oid_object_info(oid, NULL) == OBJ_BLOB)) {
b383a13c 260 if (check_object_signature(repl, NULL, 0, NULL) < 0) {
c251c83d 261 error("sha1 mismatch %s", oid_to_hex(oid));
090ea126
NTND
262 return NULL;
263 }
c251c83d 264 parse_blob_buffer(lookup_blob(oid), NULL, 0);
265 return lookup_object(oid->hash);
090ea126
NTND
266 }
267
b4f5aca4 268 buffer = read_object_file(oid, &type, &size);
c4584ae3 269 if (buffer) {
b383a13c 270 if (check_object_signature(repl, buffer, size, type_name(type)) < 0) {
0b1f1130 271 free(buffer);
b383a13c 272 error("sha1 mismatch %s", oid_to_hex(repl));
acdeec62
LT
273 return NULL;
274 }
9f613ddd 275
c251c83d 276 obj = parse_object_buffer(oid, type, size, buffer, &eaten);
9f613ddd
JH
277 if (!eaten)
278 free(buffer);
bd2c39f5 279 return obj;
e9eefa67
DB
280 }
281 return NULL;
282}
66e481b0
DB
283
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));
55b4e9e4
JH
288 new_list->item = item;
289 new_list->next = *list_p;
290 *list_p = new_list;
291 return new_list;
66e481b0
DB
292}
293
66e481b0
DB
294int 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}
1f1e895f 303
31faeb20
MH
304/*
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
9e0c3c4f
JK
310void add_object_array_with_path(struct object *obj, const char *name,
311 struct object_array *array,
312 unsigned mode, const char *path)
1f1e895f
LT
313{
314 unsigned nr = array->nr;
315 unsigned alloc = array->alloc;
316 struct object_array_entry *objects = array->objects;
31faeb20 317 struct object_array_entry *entry;
1f1e895f
LT
318
319 if (nr >= alloc) {
320 alloc = (alloc + 32) * 2;
2756ca43 321 REALLOC_ARRAY(objects, alloc);
1f1e895f
LT
322 array->alloc = alloc;
323 array->objects = objects;
324 }
31faeb20
MH
325 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;
9e0c3c4f
JK
335 if (path)
336 entry->path = xstrdup(path);
337 else
338 entry->path = NULL;
1f1e895f
LT
339 array->nr = ++nr;
340}
b2a6d1c6 341
afa15f3c
MG
342void add_object_array(struct object *obj, const char *name, struct object_array *array)
343{
189a1222 344 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
afa15f3c
MG
345}
346
68f49235
JK
347/*
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);
9e0c3c4f 355 free(ent->path);
68f49235
JK
356}
357
71992039
358struct object *object_array_pop(struct object_array *array)
359{
360 struct object *ret;
361
362 if (!array->nr)
363 return NULL;
364
365 ret = array->objects[array->nr - 1].item;
366 object_array_release_entry(&array->objects[array->nr - 1]);
367 array->nr--;
368 return ret;
369}
370
aeb4a51e
MH
371void object_array_filter(struct object_array *array,
372 object_array_each_func_t want, void *cb_data)
b2a6d1c6 373{
aeb4a51e 374 unsigned nr = array->nr, src, dst;
b2a6d1c6
JH
375 struct object_array_entry *objects = array->objects;
376
aeb4a51e
MH
377 for (src = dst = 0; src < nr; src++) {
378 if (want(&objects[src], cb_data)) {
b2a6d1c6
JH
379 if (src != dst)
380 objects[dst] = objects[src];
381 dst++;
31faeb20 382 } else {
68f49235 383 object_array_release_entry(&objects[src]);
aeb4a51e
MH
384 }
385 }
386 array->nr = dst;
387}
388
46be8231
JK
389void object_array_clear(struct object_array *array)
390{
391 int i;
392 for (i = 0; i < array->nr; i++)
393 object_array_release_entry(&array->objects[i]);
6a83d902 394 FREE_AND_NULL(array->objects);
46be8231
JK
395 array->nr = array->alloc = 0;
396}
397
1506510c
MH
398/*
399 * Return true iff array already contains an entry with name.
400 */
401static int contains_name(struct object_array *array, const char *name)
402{
403 unsigned nr = array->nr, i;
404 struct object_array_entry *object = array->objects;
405
406 for (i = 0; i < nr; i++, object++)
407 if (!strcmp(object->name, name))
408 return 1;
409 return 0;
410}
411
b2a6d1c6
JH
412void object_array_remove_duplicates(struct object_array *array)
413{
1506510c 414 unsigned nr = array->nr, src;
b2a6d1c6
JH
415 struct object_array_entry *objects = array->objects;
416
1506510c
MH
417 array->nr = 0;
418 for (src = 0; src < nr; src++) {
419 if (!contains_name(array, objects[src].name)) {
420 if (src != array->nr)
421 objects[array->nr] = objects[src];
422 array->nr++;
31faeb20 423 } else {
68f49235 424 object_array_release_entry(&objects[src]);
b2a6d1c6 425 }
b2a6d1c6
JH
426 }
427}
bcc0a3ea
HV
428
429void clear_object_flags(unsigned flags)
430{
431 int i;
432
433 for (i=0; i < obj_hash_size; i++) {
434 struct object *obj = obj_hash[i];
435 if (obj)
436 obj->flags &= ~flags;
437 }
438}
4ad315fc
RS
439
440void clear_commit_marks_all(unsigned int flags)
441{
442 int i;
443
444 for (i = 0; i < obj_hash_size; i++) {
445 struct object *obj = obj_hash[i];
446 if (obj && obj->type == OBJ_COMMIT)
447 obj->flags &= ~flags;
448 }
449}
90c62155
SB
450
451struct raw_object_store *raw_object_store_new(void)
452{
453 struct raw_object_store *o = xmalloc(sizeof(*o));
454
455 memset(o, 0, sizeof(*o));
a80d72db 456 INIT_LIST_HEAD(&o->packed_git_mru);
90c62155
SB
457 return o;
458}
97501e93
SB
459
460static void free_alt_odb(struct alternate_object_database *alt)
461{
462 strbuf_release(&alt->scratch);
463 oid_array_clear(&alt->loose_objects_cache);
464 free(alt);
465}
466
467static void free_alt_odbs(struct raw_object_store *o)
468{
469 while (o->alt_odb_list) {
470 struct alternate_object_database *next;
471
472 next = o->alt_odb_list->next;
473 free_alt_odb(o->alt_odb_list);
474 o->alt_odb_list = next;
475 }
476}
477
90c62155
SB
478void raw_object_store_clear(struct raw_object_store *o)
479{
480 FREE_AND_NULL(o->objectdir);
481 FREE_AND_NULL(o->alternate_db);
97501e93
SB
482
483 free_alt_odbs(o);
484 o->alt_odb_tail = NULL;
a80d72db
SB
485
486 INIT_LIST_HEAD(&o->packed_git_mru);
d0b59866
SB
487 close_all_packs(o);
488 o->packed_git = NULL;
90c62155 489}