]> git.ipfire.org Git - thirdparty/git.git/blame - object.c
pager: config variable pager.color
[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
885a86ab 21const char *type_names[] = {
1974632c
LT
22 "none", "commit", "tree", "blob", "tag",
23 "bad type 5", "bad type 6", "delta", "bad",
885a86ab
LT
24};
25
0556a11a
LT
26static unsigned int hash_obj(struct object *obj, unsigned int n)
27{
28 unsigned int hash = *(unsigned int *)obj->sha1;
29 return hash % n;
30}
31
32static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
33{
34 int j = hash_obj(obj, size);
35
36 while (hash[j]) {
37 j++;
38 if (j >= size)
39 j = 0;
40 }
41 hash[j] = obj;
42}
43
070879ca
JS
44static int hashtable_index(const unsigned char *sha1)
45{
2b796360
JH
46 unsigned int i;
47 memcpy(&i, sha1, sizeof(unsigned int));
0556a11a 48 return (int)(i % obj_hash_size);
070879ca
JS
49}
50
0556a11a 51struct object *lookup_object(const unsigned char *sha1)
175785e5 52{
2b796360 53 int i;
0556a11a 54 struct object *obj;
175785e5 55
0556a11a
LT
56 if (!obj_hash)
57 return NULL;
175785e5 58
2b796360 59 i = hashtable_index(sha1);
0556a11a
LT
60 while ((obj = obj_hash[i]) != NULL) {
61 if (!memcmp(sha1, obj->sha1, 20))
62 break;
070879ca 63 i++;
0556a11a 64 if (i == obj_hash_size)
070879ca
JS
65 i = 0;
66 }
0556a11a 67 return obj;
175785e5
DB
68}
69
0556a11a 70static void grow_object_hash(void)
175785e5 71{
0556a11a
LT
72 int i;
73 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
74 struct object **new_hash;
75
76 new_hash = calloc(new_hash_size, sizeof(struct object *));
77 for (i = 0; i < obj_hash_size; i++) {
78 struct object *obj = obj_hash[i];
79 if (!obj)
80 continue;
81 insert_obj_hash(obj, new_hash, new_hash_size);
82 }
83 free(obj_hash);
84 obj_hash = new_hash;
85 obj_hash_size = new_hash_size;
175785e5
DB
86}
87
5d6ccf5c 88void created_object(const unsigned char *sha1, struct object *obj)
175785e5 89{
175785e5 90 obj->parsed = 0;
175785e5 91 obj->used = 0;
1974632c 92 obj->type = OBJ_NONE;
0556a11a
LT
93 obj->flags = 0;
94 memcpy(obj->sha1, sha1, 20);
175785e5 95
0556a11a
LT
96 if (obj_hash_size - 1 <= nr_objs * 2)
97 grow_object_hash();
175785e5 98
0556a11a 99 insert_obj_hash(obj, obj_hash, obj_hash_size);
175785e5
DB
100 nr_objs++;
101}
102
89e4202f
DB
103struct object *lookup_object_type(const unsigned char *sha1, const char *type)
104{
66e481b0
DB
105 if (!type) {
106 return lookup_unknown_object(sha1);
107 } else if (!strcmp(type, blob_type)) {
89e4202f
DB
108 return &lookup_blob(sha1)->object;
109 } else if (!strcmp(type, tree_type)) {
110 return &lookup_tree(sha1)->object;
111 } else if (!strcmp(type, commit_type)) {
112 return &lookup_commit(sha1)->object;
113 } else if (!strcmp(type, tag_type)) {
114 return &lookup_tag(sha1)->object;
115 } else {
116 error("Unknown type %s", type);
117 return NULL;
118 }
119}
120
66e481b0
DB
121union any_object {
122 struct object object;
123 struct commit commit;
124 struct tree tree;
125 struct blob blob;
126 struct tag tag;
127};
128
129struct object *lookup_unknown_object(const unsigned char *sha1)
130{
131 struct object *obj = lookup_object(sha1);
132 if (!obj) {
90321c10 133 union any_object *ret = xcalloc(1, sizeof(*ret));
66e481b0 134 created_object(sha1, &ret->object);
1974632c 135 ret->object.type = OBJ_NONE;
66e481b0
DB
136 return &ret->object;
137 }
138 return obj;
139}
140
5d6ccf5c 141struct object *parse_object(const unsigned char *sha1)
e9eefa67 142{
c4584ae3
JH
143 unsigned long size;
144 char type[20];
145 void *buffer = read_sha1_file(sha1, type, &size);
146 if (buffer) {
bd2c39f5 147 struct object *obj;
c4584ae3 148 if (check_sha1_signature(sha1, buffer, size, type) < 0)
e9eefa67 149 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
8e440259 150 if (!strcmp(type, blob_type)) {
bd2c39f5
NP
151 struct blob *blob = lookup_blob(sha1);
152 parse_blob_buffer(blob, buffer, size);
153 obj = &blob->object;
8e440259 154 } else if (!strcmp(type, tree_type)) {
bd2c39f5 155 struct tree *tree = lookup_tree(sha1);
bd2c39f5 156 obj = &tree->object;
136f2e54
LT
157 if (!tree->object.parsed) {
158 parse_tree_buffer(tree, buffer, size);
159 buffer = NULL;
160 }
8e440259 161 } else if (!strcmp(type, commit_type)) {
bd2c39f5
NP
162 struct commit *commit = lookup_commit(sha1);
163 parse_commit_buffer(commit, buffer, size);
bd1e17e2
LT
164 if (!commit->buffer) {
165 commit->buffer = buffer;
166 buffer = NULL;
167 }
bd2c39f5 168 obj = &commit->object;
8e440259 169 } else if (!strcmp(type, tag_type)) {
bd2c39f5
NP
170 struct tag *tag = lookup_tag(sha1);
171 parse_tag_buffer(tag, buffer, size);
172 obj = &tag->object;
e9eefa67 173 } else {
bd2c39f5 174 obj = NULL;
e9eefa67 175 }
bd2c39f5
NP
176 free(buffer);
177 return obj;
e9eefa67
DB
178 }
179 return NULL;
180}
66e481b0
DB
181
182struct object_list *object_list_insert(struct object *item,
183 struct object_list **list_p)
184{
185 struct object_list *new_list = xmalloc(sizeof(struct object_list));
186 new_list->item = item;
187 new_list->next = *list_p;
188 *list_p = new_list;
189 return new_list;
190}
191
680bab3d
DB
192void object_list_append(struct object *item,
193 struct object_list **list_p)
194{
195 while (*list_p) {
196 list_p = &((*list_p)->next);
197 }
198 *list_p = xmalloc(sizeof(struct object_list));
199 (*list_p)->next = NULL;
200 (*list_p)->item = item;
201}
202
66e481b0
DB
203unsigned object_list_length(struct object_list *list)
204{
205 unsigned ret = 0;
206 while (list) {
207 list = list->next;
208 ret++;
209 }
210 return ret;
211}
212
213int object_list_contains(struct object_list *list, struct object *obj)
214{
215 while (list) {
216 if (list->item == obj)
217 return 1;
218 list = list->next;
219 }
220 return 0;
221}
1f1e895f
LT
222
223void add_object_array(struct object *obj, const char *name, struct object_array *array)
224{
225 unsigned nr = array->nr;
226 unsigned alloc = array->alloc;
227 struct object_array_entry *objects = array->objects;
228
229 if (nr >= alloc) {
230 alloc = (alloc + 32) * 2;
231 objects = xrealloc(objects, alloc * sizeof(*objects));
232 array->alloc = alloc;
233 array->objects = objects;
234 }
235 objects[nr].item = obj;
236 objects[nr].name = name;
237 array->nr = ++nr;
238}