Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 278 lines (243 sloc) 6.033 kb
8f1d2e6 [PATCH] Compilation: zero-length array declaration.
Junio C Hamano authored
1 #include "cache.h"
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
2 #include "object.h"
e9eefa6 [PATCH] Add function to parse an object of unspecified type (take 2)
Daniel Barkalow authored
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "tag.h"
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
7
0556a11 git object hash cleanups
Linus Torvalds authored
8 static struct object **obj_hash;
9 static int nr_objs, obj_hash_size;
fc046a7 Abstract out accesses to object hash array
Linus Torvalds authored
10
11 unsigned int get_max_object_index(void)
12 {
0556a11 git object hash cleanups
Linus Torvalds authored
13 return obj_hash_size;
fc046a7 Abstract out accesses to object hash array
Linus Torvalds authored
14 }
15
16 struct object *get_indexed_object(unsigned int idx)
17 {
0556a11 git object hash cleanups
Linus Torvalds authored
18 return obj_hash[idx];
fc046a7 Abstract out accesses to object hash array
Linus Torvalds authored
19 }
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
20
df84366 formalize typename(), and add its reverse type_from_string()
Nicolas Pitre authored
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 */
885a86a Shrink "struct object" a bit
Linus Torvalds authored
27 };
28
df84366 formalize typename(), and add its reverse type_from_string()
Nicolas Pitre authored
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
0556a11 git object hash cleanups
Linus Torvalds authored
46 static unsigned int hash_obj(struct object *obj, unsigned int n)
47 {
b867d32 @toofishes Fix type-punning issues
toofishes authored
48 unsigned int hash;
49 memcpy(&hash, obj->sha1, sizeof(unsigned int));
0556a11 git object hash cleanups
Linus Torvalds authored
50 return hash % n;
51 }
52
53 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
54 {
91fe2f9 @toofishes Unify signedness in hashing calls
toofishes authored
55 unsigned int j = hash_obj(obj, size);
0556a11 git object hash cleanups
Linus Torvalds authored
56
57 while (hash[j]) {
58 j++;
59 if (j >= size)
60 j = 0;
61 }
62 hash[j] = obj;
63 }
64
91fe2f9 @toofishes Unify signedness in hashing calls
toofishes authored
65 static unsigned int hashtable_index(const unsigned char *sha1)
070879c @dscho Use a hashtable for objects instead of a sorted list
dscho authored
66 {
2b79636 hashtable-based objects: minimum fixups.
Junio C Hamano authored
67 unsigned int i;
68 memcpy(&i, sha1, sizeof(unsigned int));
91fe2f9 @toofishes Unify signedness in hashing calls
toofishes authored
69 return i % obj_hash_size;
070879c @dscho Use a hashtable for objects instead of a sorted list
dscho authored
70 }
71
0556a11 git object hash cleanups
Linus Torvalds authored
72 struct object *lookup_object(const unsigned char *sha1)
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
73 {
91fe2f9 @toofishes Unify signedness in hashing calls
toofishes authored
74 unsigned int i;
0556a11 git object hash cleanups
Linus Torvalds authored
75 struct object *obj;
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
76
0556a11 git object hash cleanups
Linus Torvalds authored
77 if (!obj_hash)
78 return NULL;
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
79
2b79636 hashtable-based objects: minimum fixups.
Junio C Hamano authored
80 i = hashtable_index(sha1);
0556a11 git object hash cleanups
Linus Torvalds authored
81 while ((obj = obj_hash[i]) != NULL) {
a89fccd Do not use memcmp(sha1_1, sha1_2, 20) with hardcoded length.
David Rientjes authored
82 if (!hashcmp(sha1, obj->sha1))
0556a11 git object hash cleanups
Linus Torvalds authored
83 break;
070879c @dscho Use a hashtable for objects instead of a sorted list
dscho authored
84 i++;
0556a11 git object hash cleanups
Linus Torvalds authored
85 if (i == obj_hash_size)
070879c @dscho Use a hashtable for objects instead of a sorted list
dscho authored
86 i = 0;
87 }
0556a11 git object hash cleanups
Linus Torvalds authored
88 return obj;
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
89 }
90
0556a11 git object hash cleanups
Linus Torvalds authored
91 static void grow_object_hash(void)
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
92 {
0556a11 git object hash cleanups
Linus Torvalds authored
93 int i;
94 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
95 struct object **new_hash;
96
b3c952f @jonas Use xcalloc instead of calloc
jonas authored
97 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
0556a11 git object hash cleanups
Linus Torvalds authored
98 for (i = 0; i < obj_hash_size; i++) {
99 struct object *obj = obj_hash[i];
100 if (!obj)
101 continue;
102 insert_obj_hash(obj, new_hash, new_hash_size);
103 }
104 free(obj_hash);
105 obj_hash = new_hash;
106 obj_hash_size = new_hash_size;
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
107 }
108
100c5f3 @torvalds Clean up object creation to use more common code
torvalds authored
109 void *create_object(const unsigned char *sha1, int type, void *o)
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
110 {
100c5f3 @torvalds Clean up object creation to use more common code
torvalds authored
111 struct object *obj = o;
112
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
113 obj->parsed = 0;
114 obj->used = 0;
100c5f3 @torvalds Clean up object creation to use more common code
torvalds authored
115 obj->type = type;
0556a11 git object hash cleanups
Linus Torvalds authored
116 obj->flags = 0;
e702496 @spearce Convert memcpy(a,b,20) to hashcpy(a,b).
spearce authored
117 hashcpy(obj->sha1, sha1);
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
118
0556a11 git object hash cleanups
Linus Torvalds authored
119 if (obj_hash_size - 1 <= nr_objs * 2)
120 grow_object_hash();
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
121
0556a11 git object hash cleanups
Linus Torvalds authored
122 insert_obj_hash(obj, obj_hash, obj_hash_size);
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
123 nr_objs++;
100c5f3 @torvalds Clean up object creation to use more common code
torvalds authored
124 return obj;
175785e [PATCH] Implementations of parsing functions
Daniel Barkalow authored
125 }
126
66e481b [PATCH] Object library enhancements
barkalow@iabervon.org authored
127 struct object *lookup_unknown_object(const unsigned char *sha1)
128 {
129 struct object *obj = lookup_object(sha1);
100c5f3 @torvalds Clean up object creation to use more common code
torvalds authored
130 if (!obj)
131 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
66e481b [PATCH] Object library enhancements
barkalow@iabervon.org authored
132 return obj;
133 }
134
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
135 struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
136 {
137 struct object *obj;
138 int eaten = 0;
139
cc21682 Don't dereference NULL upon lookup failure.
Jim Meyering authored
140 obj = NULL;
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
141 if (type == OBJ_BLOB) {
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
142 struct blob *blob = lookup_blob(sha1);
cc21682 Don't dereference NULL upon lookup failure.
Jim Meyering authored
143 if (blob) {
d0b8c9e parse_object_buffer: don't ignore errors from the object specific parsin...
Martin Koegler authored
144 if (parse_blob_buffer(blob, buffer, size))
145 return NULL;
cc21682 Don't dereference NULL upon lookup failure.
Jim Meyering authored
146 obj = &blob->object;
147 }
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
148 } else if (type == OBJ_TREE) {
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
149 struct tree *tree = lookup_tree(sha1);
cc21682 Don't dereference NULL upon lookup failure.
Jim Meyering authored
150 if (tree) {
151 obj = &tree->object;
68be2fe @gitster receive-pack, fetch-pack: reject bogus pack that records objects twice
authored
152 if (!tree->buffer)
153 tree->object.parsed = 0;
cc21682 Don't dereference NULL upon lookup failure.
Jim Meyering authored
154 if (!tree->object.parsed) {
d0b8c9e parse_object_buffer: don't ignore errors from the object specific parsin...
Martin Koegler authored
155 if (parse_tree_buffer(tree, buffer, size))
156 return NULL;
cc21682 Don't dereference NULL upon lookup failure.
Jim Meyering authored
157 eaten = 1;
158 }
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
159 }
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
160 } else if (type == OBJ_COMMIT) {
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
161 struct commit *commit = lookup_commit(sha1);
cc21682 Don't dereference NULL upon lookup failure.
Jim Meyering authored
162 if (commit) {
d0b8c9e parse_object_buffer: don't ignore errors from the object specific parsin...
Martin Koegler authored
163 if (parse_commit_buffer(commit, buffer, size))
164 return NULL;
cc21682 Don't dereference NULL upon lookup failure.
Jim Meyering authored
165 if (!commit->buffer) {
166 commit->buffer = buffer;
167 eaten = 1;
168 }
169 obj = &commit->object;
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
170 }
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
171 } else if (type == OBJ_TAG) {
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
172 struct tag *tag = lookup_tag(sha1);
cc21682 Don't dereference NULL upon lookup failure.
Jim Meyering authored
173 if (tag) {
d0b8c9e parse_object_buffer: don't ignore errors from the object specific parsin...
Martin Koegler authored
174 if (parse_tag_buffer(tag, buffer, size))
175 return NULL;
cc21682 Don't dereference NULL upon lookup failure.
Jim Meyering authored
176 obj = &tag->object;
177 }
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
178 } else {
e2ac7cb Don't assume tree entries that are not dirs are blobs
Sam Vilain authored
179 warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type);
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
180 obj = NULL;
181 }
e2ac7cb Don't assume tree entries that are not dirs are blobs
Sam Vilain authored
182 if (obj && obj->type == OBJ_NONE)
183 obj->type = type;
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
184 *eaten_p = eaten;
185 return obj;
186 }
187
5d6ccf5 [PATCH] Anal retentive 'const unsigned char *sha1'
Jason McMullan authored
188 struct object *parse_object(const unsigned char *sha1)
e9eefa6 [PATCH] Add function to parse an object of unspecified type (take 2)
Daniel Barkalow authored
189 {
c4584ae [PATCH] Remove "delta" object representation.
Junio C Hamano authored
190 unsigned long size;
21666f1 convert object type handling from a string to a number
Nicolas Pitre authored
191 enum object_type type;
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
192 int eaten;
4bbf5a2 @gitster read_sha1_file(): get rid of read_sha1_file_repl() madness
authored
193 const unsigned char *repl = lookup_replace_object(sha1);
ccdc603 @peff parse_object: try internal cache before reading object db
peff authored
194 void *buffer;
195 struct object *obj;
196
197 obj = lookup_object(sha1);
198 if (obj && obj->parsed)
199 return obj;
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
200
ccdc603 @peff parse_object: try internal cache before reading object db
peff authored
201 buffer = read_sha1_file(sha1, &type, &size);
c4584ae [PATCH] Remove "delta" object representation.
Junio C Hamano authored
202 if (buffer) {
0e87c36 @chriscool object: call "check_sha1_signature" with the replacement sha1
chriscool authored
203 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
0b1f113 @jasampler fix memory leak in parse_object when check_sha1_signature fails
jasampler authored
204 free(buffer);
0e87c36 @chriscool object: call "check_sha1_signature" with the replacement sha1
chriscool authored
205 error("sha1 mismatch %s\n", sha1_to_hex(repl));
acdeec6 @torvalds Don't ever return corrupt objects from "parse_object()"
torvalds authored
206 return NULL;
207 }
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
208
2e3400c @pclouds parse_object: pass on the original sha1, not the replaced one
pclouds authored
209 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
9f613dd Add git-for-each-ref: helper for language bindings
Junio C Hamano authored
210 if (!eaten)
211 free(buffer);
bd2c39f [PATCH] don't load and decompress objects twice with parse_object()
Nicolas Pitre authored
212 return obj;
e9eefa6 [PATCH] Add function to parse an object of unspecified type (take 2)
Daniel Barkalow authored
213 }
214 return NULL;
215 }
66e481b [PATCH] Object library enhancements
barkalow@iabervon.org authored
216
217 struct object_list *object_list_insert(struct object *item,
218 struct object_list **list_p)
219 {
220 struct object_list *new_list = xmalloc(sizeof(struct object_list));
55b4e9e @jhance Fix whitespace issue in object.c
jhance authored
221 new_list->item = item;
222 new_list->next = *list_p;
223 *list_p = new_list;
224 return new_list;
66e481b [PATCH] Object library enhancements
barkalow@iabervon.org authored
225 }
226
227 int object_list_contains(struct object_list *list, struct object *obj)
228 {
229 while (list) {
230 if (list->item == obj)
231 return 1;
232 list = list->next;
233 }
234 return 0;
235 }
1f1e895 Add "named object array" concept
Linus Torvalds authored
236
237 void add_object_array(struct object *obj, const char *name, struct object_array *array)
238 {
e5709a4 add add_object_array_with_mode
Martin Koegler authored
239 add_object_array_with_mode(obj, name, array, S_IFINVALID);
240 }
241
242 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
243 {
1f1e895 Add "named object array" concept
Linus Torvalds authored
244 unsigned nr = array->nr;
245 unsigned alloc = array->alloc;
246 struct object_array_entry *objects = array->objects;
247
248 if (nr >= alloc) {
249 alloc = (alloc + 32) * 2;
250 objects = xrealloc(objects, alloc * sizeof(*objects));
251 array->alloc = alloc;
252 array->objects = objects;
253 }
254 objects[nr].item = obj;
255 objects[nr].name = name;
e5709a4 add add_object_array_with_mode
Martin Koegler authored
256 objects[nr].mode = mode;
1f1e895 Add "named object array" concept
Linus Torvalds authored
257 array->nr = ++nr;
258 }
b2a6d1c @gitster bundle: allow the same ref to be given more than once
authored
259
260 void object_array_remove_duplicates(struct object_array *array)
261 {
97a20ee @jrn fix "bundle --stdin" segfault
jrn authored
262 unsigned int ref, src, dst;
b2a6d1c @gitster bundle: allow the same ref to be given more than once
authored
263 struct object_array_entry *objects = array->objects;
264
97a20ee @jrn fix "bundle --stdin" segfault
jrn authored
265 for (ref = 0; ref + 1 < array->nr; ref++) {
b2a6d1c @gitster bundle: allow the same ref to be given more than once
authored
266 for (src = ref + 1, dst = src;
267 src < array->nr;
268 src++) {
269 if (!strcmp(objects[ref].name, objects[src].name))
270 continue;
271 if (src != dst)
272 objects[dst] = objects[src];
273 dst++;
274 }
275 array->nr = dst;
276 }
277 }
Something went wrong with that request. Please try again.