]> git.ipfire.org Git - thirdparty/git.git/blame - object.c
[PATCH] Initial AIX portability fixes.
[thirdparty/git.git] / object.c
CommitLineData
175785e5 1#include "object.h"
e9eefa67
DB
2#include "blob.h"
3#include "tree.h"
4#include "commit.h"
175785e5 5#include "cache.h"
e9eefa67 6#include "tag.h"
175785e5
DB
7
8struct object **objs;
9int nr_objs;
10static int obj_allocs;
11
8805ccac
LT
12int track_object_refs = 1;
13
5d6ccf5c 14static int find_object(const unsigned char *sha1)
175785e5
DB
15{
16 int first = 0, last = nr_objs;
17
18 while (first < last) {
19 int next = (first + last) / 2;
20 struct object *obj = objs[next];
21 int cmp;
22
23 cmp = memcmp(sha1, obj->sha1, 20);
24 if (!cmp)
25 return next;
26 if (cmp < 0) {
27 last = next;
28 continue;
29 }
30 first = next+1;
31 }
32 return -first-1;
33}
34
5d6ccf5c 35struct object *lookup_object(const unsigned char *sha1)
175785e5
DB
36{
37 int pos = find_object(sha1);
38 if (pos >= 0)
39 return objs[pos];
40 return NULL;
41}
42
5d6ccf5c 43void created_object(const unsigned char *sha1, struct object *obj)
175785e5
DB
44{
45 int pos = find_object(sha1);
46
47 obj->parsed = 0;
48 memcpy(obj->sha1, sha1, 20);
49 obj->type = NULL;
50 obj->refs = NULL;
51 obj->used = 0;
52
53 if (pos >= 0)
54 die("Inserting %s twice\n", sha1_to_hex(sha1));
55 pos = -pos-1;
56
57 if (obj_allocs == nr_objs) {
58 obj_allocs = alloc_nr(obj_allocs);
812666c8 59 objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
175785e5
DB
60 }
61
62 /* Insert it into the right place */
63 memmove(objs + pos + 1, objs + pos, (nr_objs - pos) *
64 sizeof(struct object *));
65
66 objs[pos] = obj;
67 nr_objs++;
68}
69
4a4e6fd7 70struct object_refs *alloc_object_refs(unsigned count)
175785e5 71{
4a4e6fd7
SV
72 struct object_refs *refs;
73 size_t size = sizeof(*refs) + count*sizeof(struct object *);
8805ccac 74
4a4e6fd7
SV
75 refs = xmalloc(size);
76 memset(refs, 0, size);
77 refs->count = count;
78 return refs;
79}
80
81static int compare_object_pointers(const void *a, const void *b)
82{
83 const struct object * const *pa = a;
84 const struct object * const *pb = b;
85 return *pa - *pb;
86}
87
88void set_object_refs(struct object *obj, struct object_refs *refs)
89{
90 unsigned int i, j;
91
92 /* Do not install empty list of references */
93 if (refs->count < 1) {
94 free(refs);
8805ccac 95 return;
4a4e6fd7 96 }
8805ccac 97
4a4e6fd7
SV
98 /* Sort the list and filter out duplicates */
99 qsort(refs->ref, refs->count, sizeof(refs->ref[0]),
100 compare_object_pointers);
101 for (i = j = 1; i < refs->count; i++) {
102 if (refs->ref[i] != refs->ref[i - 1])
103 refs->ref[j++] = refs->ref[i];
104 }
105 if (j < refs->count) {
106 /* Duplicates were found - reallocate list */
107 size_t size = sizeof(*refs) + j*sizeof(struct object *);
108 refs->count = j;
109 refs = xrealloc(refs, size);
175785e5
DB
110 }
111
4a4e6fd7
SV
112 for (i = 0; i < refs->count; i++)
113 refs->ref[i]->used = 1;
114 obj->refs = refs;
175785e5
DB
115}
116
117void mark_reachable(struct object *obj, unsigned int mask)
118{
8805ccac
LT
119 if (!track_object_refs)
120 die("cannot do reachability with object refs turned off");
175785e5
DB
121 /* If we've been here already, don't bother */
122 if (obj->flags & mask)
123 return;
124 obj->flags |= mask;
4a4e6fd7
SV
125 if (obj->refs) {
126 const struct object_refs *refs = obj->refs;
127 unsigned i;
128 for (i = 0; i < refs->count; i++)
129 mark_reachable(refs->ref[i], mask);
175785e5
DB
130 }
131}
e9eefa67 132
89e4202f
DB
133struct object *lookup_object_type(const unsigned char *sha1, const char *type)
134{
66e481b0
DB
135 if (!type) {
136 return lookup_unknown_object(sha1);
137 } else if (!strcmp(type, blob_type)) {
89e4202f
DB
138 return &lookup_blob(sha1)->object;
139 } else if (!strcmp(type, tree_type)) {
140 return &lookup_tree(sha1)->object;
141 } else if (!strcmp(type, commit_type)) {
142 return &lookup_commit(sha1)->object;
143 } else if (!strcmp(type, tag_type)) {
144 return &lookup_tag(sha1)->object;
145 } else {
146 error("Unknown type %s", type);
147 return NULL;
148 }
149}
150
66e481b0
DB
151union any_object {
152 struct object object;
153 struct commit commit;
154 struct tree tree;
155 struct blob blob;
156 struct tag tag;
157};
158
159struct object *lookup_unknown_object(const unsigned char *sha1)
160{
161 struct object *obj = lookup_object(sha1);
162 if (!obj) {
163 union any_object *ret = xmalloc(sizeof(*ret));
164 memset(ret, 0, sizeof(*ret));
165 created_object(sha1, &ret->object);
166 ret->object.type = NULL;
167 return &ret->object;
168 }
169 return obj;
170}
171
5d6ccf5c 172struct object *parse_object(const unsigned char *sha1)
e9eefa67 173{
c4584ae3
JH
174 unsigned long size;
175 char type[20];
176 void *buffer = read_sha1_file(sha1, type, &size);
177 if (buffer) {
bd2c39f5 178 struct object *obj;
c4584ae3 179 if (check_sha1_signature(sha1, buffer, size, type) < 0)
e9eefa67 180 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
c4584ae3 181 if (!strcmp(type, "blob")) {
bd2c39f5
NP
182 struct blob *blob = lookup_blob(sha1);
183 parse_blob_buffer(blob, buffer, size);
184 obj = &blob->object;
e9eefa67 185 } else if (!strcmp(type, "tree")) {
bd2c39f5
NP
186 struct tree *tree = lookup_tree(sha1);
187 parse_tree_buffer(tree, buffer, size);
188 obj = &tree->object;
e9eefa67 189 } else if (!strcmp(type, "commit")) {
bd2c39f5
NP
190 struct commit *commit = lookup_commit(sha1);
191 parse_commit_buffer(commit, buffer, size);
bd1e17e2
LT
192 if (!commit->buffer) {
193 commit->buffer = buffer;
194 buffer = NULL;
195 }
bd2c39f5 196 obj = &commit->object;
e9eefa67 197 } else if (!strcmp(type, "tag")) {
bd2c39f5
NP
198 struct tag *tag = lookup_tag(sha1);
199 parse_tag_buffer(tag, buffer, size);
200 obj = &tag->object;
e9eefa67 201 } else {
bd2c39f5 202 obj = NULL;
e9eefa67 203 }
bd2c39f5
NP
204 free(buffer);
205 return obj;
e9eefa67
DB
206 }
207 return NULL;
208}
66e481b0
DB
209
210struct object_list *object_list_insert(struct object *item,
211 struct object_list **list_p)
212{
213 struct object_list *new_list = xmalloc(sizeof(struct object_list));
214 new_list->item = item;
215 new_list->next = *list_p;
216 *list_p = new_list;
217 return new_list;
218}
219
680bab3d
DB
220void object_list_append(struct object *item,
221 struct object_list **list_p)
222{
223 while (*list_p) {
224 list_p = &((*list_p)->next);
225 }
226 *list_p = xmalloc(sizeof(struct object_list));
227 (*list_p)->next = NULL;
228 (*list_p)->item = item;
229}
230
66e481b0
DB
231unsigned object_list_length(struct object_list *list)
232{
233 unsigned ret = 0;
234 while (list) {
235 list = list->next;
236 ret++;
237 }
238 return ret;
239}
240
241int object_list_contains(struct object_list *list, struct object *obj)
242{
243 while (list) {
244 if (list->item == obj)
245 return 1;
246 list = list->next;
247 }
248 return 0;
249}