[PATCH] Add function to append to an object_list.
[git/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
5d6ccf5c 12static int find_object(const unsigned char *sha1)
175785e5
DB
13{
14 int first = 0, last = nr_objs;
15
16 while (first < last) {
17 int next = (first + last) / 2;
18 struct object *obj = objs[next];
19 int cmp;
20
21 cmp = memcmp(sha1, obj->sha1, 20);
22 if (!cmp)
23 return next;
24 if (cmp < 0) {
25 last = next;
26 continue;
27 }
28 first = next+1;
29 }
30 return -first-1;
31}
32
5d6ccf5c 33struct object *lookup_object(const unsigned char *sha1)
175785e5
DB
34{
35 int pos = find_object(sha1);
36 if (pos >= 0)
37 return objs[pos];
38 return NULL;
39}
40
5d6ccf5c 41void created_object(const unsigned char *sha1, struct object *obj)
175785e5
DB
42{
43 int pos = find_object(sha1);
44
45 obj->parsed = 0;
46 memcpy(obj->sha1, sha1, 20);
47 obj->type = NULL;
48 obj->refs = NULL;
49 obj->used = 0;
50
51 if (pos >= 0)
52 die("Inserting %s twice\n", sha1_to_hex(sha1));
53 pos = -pos-1;
54
55 if (obj_allocs == nr_objs) {
56 obj_allocs = alloc_nr(obj_allocs);
812666c8 57 objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
175785e5
DB
58 }
59
60 /* Insert it into the right place */
61 memmove(objs + pos + 1, objs + pos, (nr_objs - pos) *
62 sizeof(struct object *));
63
64 objs[pos] = obj;
65 nr_objs++;
66}
67
68void add_ref(struct object *refer, struct object *target)
69{
70 struct object_list **pp = &refer->refs;
71 struct object_list *p;
72
73 while ((p = *pp) != NULL) {
74 if (p->item == target)
75 return;
76 pp = &p->next;
77 }
78
79 target->used = 1;
812666c8 80 p = xmalloc(sizeof(*p));
175785e5
DB
81 p->item = target;
82 p->next = NULL;
83 *pp = p;
84}
85
86void mark_reachable(struct object *obj, unsigned int mask)
87{
88 struct object_list *p = obj->refs;
89
90 /* If we've been here already, don't bother */
91 if (obj->flags & mask)
92 return;
93 obj->flags |= mask;
94 while (p) {
95 mark_reachable(p->item, mask);
96 p = p->next;
97 }
98}
e9eefa67 99
89e4202f
DB
100struct object *lookup_object_type(const unsigned char *sha1, const char *type)
101{
66e481b0
DB
102 if (!type) {
103 return lookup_unknown_object(sha1);
104 } else if (!strcmp(type, blob_type)) {
89e4202f
DB
105 return &lookup_blob(sha1)->object;
106 } else if (!strcmp(type, tree_type)) {
107 return &lookup_tree(sha1)->object;
108 } else if (!strcmp(type, commit_type)) {
109 return &lookup_commit(sha1)->object;
110 } else if (!strcmp(type, tag_type)) {
111 return &lookup_tag(sha1)->object;
112 } else {
113 error("Unknown type %s", type);
114 return NULL;
115 }
116}
117
66e481b0
DB
118union any_object {
119 struct object object;
120 struct commit commit;
121 struct tree tree;
122 struct blob blob;
123 struct tag tag;
124};
125
126struct object *lookup_unknown_object(const unsigned char *sha1)
127{
128 struct object *obj = lookup_object(sha1);
129 if (!obj) {
130 union any_object *ret = xmalloc(sizeof(*ret));
131 memset(ret, 0, sizeof(*ret));
132 created_object(sha1, &ret->object);
133 ret->object.type = NULL;
134 return &ret->object;
135 }
136 return obj;
137}
138
5d6ccf5c 139struct object *parse_object(const unsigned char *sha1)
e9eefa67 140{
c4584ae3
JH
141 unsigned long size;
142 char type[20];
143 void *buffer = read_sha1_file(sha1, type, &size);
144 if (buffer) {
bd2c39f5 145 struct object *obj;
c4584ae3 146 if (check_sha1_signature(sha1, buffer, size, type) < 0)
e9eefa67 147 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
c4584ae3 148 if (!strcmp(type, "blob")) {
bd2c39f5
NP
149 struct blob *blob = lookup_blob(sha1);
150 parse_blob_buffer(blob, buffer, size);
151 obj = &blob->object;
e9eefa67 152 } else if (!strcmp(type, "tree")) {
bd2c39f5
NP
153 struct tree *tree = lookup_tree(sha1);
154 parse_tree_buffer(tree, buffer, size);
155 obj = &tree->object;
e9eefa67 156 } else if (!strcmp(type, "commit")) {
bd2c39f5
NP
157 struct commit *commit = lookup_commit(sha1);
158 parse_commit_buffer(commit, buffer, size);
bd1e17e2
LT
159 if (!commit->buffer) {
160 commit->buffer = buffer;
161 buffer = NULL;
162 }
bd2c39f5 163 obj = &commit->object;
e9eefa67 164 } else if (!strcmp(type, "tag")) {
bd2c39f5
NP
165 struct tag *tag = lookup_tag(sha1);
166 parse_tag_buffer(tag, buffer, size);
167 obj = &tag->object;
e9eefa67 168 } else {
bd2c39f5 169 obj = NULL;
e9eefa67 170 }
bd2c39f5
NP
171 free(buffer);
172 return obj;
e9eefa67
DB
173 }
174 return NULL;
175}
66e481b0
DB
176
177struct object_list *object_list_insert(struct object *item,
178 struct object_list **list_p)
179{
180 struct object_list *new_list = xmalloc(sizeof(struct object_list));
181 new_list->item = item;
182 new_list->next = *list_p;
183 *list_p = new_list;
184 return new_list;
185}
186
680bab3d
DB
187void object_list_append(struct object *item,
188 struct object_list **list_p)
189{
190 while (*list_p) {
191 list_p = &((*list_p)->next);
192 }
193 *list_p = xmalloc(sizeof(struct object_list));
194 (*list_p)->next = NULL;
195 (*list_p)->item = item;
196}
197
66e481b0
DB
198unsigned object_list_length(struct object_list *list)
199{
200 unsigned ret = 0;
201 while (list) {
202 list = list->next;
203 ret++;
204 }
205 return ret;
206}
207
208int object_list_contains(struct object_list *list, struct object *obj)
209{
210 while (list) {
211 if (list->item == obj)
212 return 1;
213 list = list->next;
214 }
215 return 0;
216}