Fix cvsimport warning when called without --no-cvs-direct
[git/gitweb.git] / object.c
blob1fdebe012ba8a6db0b6eae443e40a7f74ae2d597
1 #include "object.h"
2 #include "blob.h"
3 #include "tree.h"
4 #include "commit.h"
5 #include "cache.h"
6 #include "tag.h"
8 struct object **objs;
9 int nr_objs;
10 static int obj_allocs;
12 int track_object_refs = 1;
14 static int find_object(const unsigned char *sha1)
16 int first = 0, last = nr_objs;
18 while (first < last) {
19 int next = (first + last) / 2;
20 struct object *obj = objs[next];
21 int cmp;
23 cmp = memcmp(sha1, obj->sha1, 20);
24 if (!cmp)
25 return next;
26 if (cmp < 0) {
27 last = next;
28 continue;
30 first = next+1;
32 return -first-1;
35 struct object *lookup_object(const unsigned char *sha1)
37 int pos = find_object(sha1);
38 if (pos >= 0)
39 return objs[pos];
40 return NULL;
43 void created_object(const unsigned char *sha1, struct object *obj)
45 int pos = find_object(sha1);
47 obj->parsed = 0;
48 memcpy(obj->sha1, sha1, 20);
49 obj->type = NULL;
50 obj->refs = NULL;
51 obj->used = 0;
53 if (pos >= 0)
54 die("Inserting %s twice\n", sha1_to_hex(sha1));
55 pos = -pos-1;
57 if (obj_allocs == nr_objs) {
58 obj_allocs = alloc_nr(obj_allocs);
59 objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
62 /* Insert it into the right place */
63 memmove(objs + pos + 1, objs + pos, (nr_objs - pos) *
64 sizeof(struct object *));
66 objs[pos] = obj;
67 nr_objs++;
70 void add_ref(struct object *refer, struct object *target)
72 struct object_list **pp, *p;
74 if (!track_object_refs)
75 return;
77 pp = &refer->refs;
78 while ((p = *pp) != NULL) {
79 if (p->item == target)
80 return;
81 pp = &p->next;
84 target->used = 1;
85 p = xmalloc(sizeof(*p));
86 p->item = target;
87 p->next = NULL;
88 *pp = p;
91 void mark_reachable(struct object *obj, unsigned int mask)
93 struct object_list *p = obj->refs;
95 if (!track_object_refs)
96 die("cannot do reachability with object refs turned off");
97 /* If we've been here already, don't bother */
98 if (obj->flags & mask)
99 return;
100 obj->flags |= mask;
101 while (p) {
102 mark_reachable(p->item, mask);
103 p = p->next;
107 struct object *lookup_object_type(const unsigned char *sha1, const char *type)
109 if (!type) {
110 return lookup_unknown_object(sha1);
111 } else if (!strcmp(type, blob_type)) {
112 return &lookup_blob(sha1)->object;
113 } else if (!strcmp(type, tree_type)) {
114 return &lookup_tree(sha1)->object;
115 } else if (!strcmp(type, commit_type)) {
116 return &lookup_commit(sha1)->object;
117 } else if (!strcmp(type, tag_type)) {
118 return &lookup_tag(sha1)->object;
119 } else {
120 error("Unknown type %s", type);
121 return NULL;
125 union any_object {
126 struct object object;
127 struct commit commit;
128 struct tree tree;
129 struct blob blob;
130 struct tag tag;
133 struct object *lookup_unknown_object(const unsigned char *sha1)
135 struct object *obj = lookup_object(sha1);
136 if (!obj) {
137 union any_object *ret = xmalloc(sizeof(*ret));
138 memset(ret, 0, sizeof(*ret));
139 created_object(sha1, &ret->object);
140 ret->object.type = NULL;
141 return &ret->object;
143 return obj;
146 struct object *parse_object(const unsigned char *sha1)
148 unsigned long size;
149 char type[20];
150 void *buffer = read_sha1_file(sha1, type, &size);
151 if (buffer) {
152 struct object *obj;
153 if (check_sha1_signature(sha1, buffer, size, type) < 0)
154 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
155 if (!strcmp(type, "blob")) {
156 struct blob *blob = lookup_blob(sha1);
157 parse_blob_buffer(blob, buffer, size);
158 obj = &blob->object;
159 } else if (!strcmp(type, "tree")) {
160 struct tree *tree = lookup_tree(sha1);
161 parse_tree_buffer(tree, buffer, size);
162 obj = &tree->object;
163 } else if (!strcmp(type, "commit")) {
164 struct commit *commit = lookup_commit(sha1);
165 parse_commit_buffer(commit, buffer, size);
166 if (!commit->buffer) {
167 commit->buffer = buffer;
168 buffer = NULL;
170 obj = &commit->object;
171 } else if (!strcmp(type, "tag")) {
172 struct tag *tag = lookup_tag(sha1);
173 parse_tag_buffer(tag, buffer, size);
174 obj = &tag->object;
175 } else {
176 obj = NULL;
178 free(buffer);
179 return obj;
181 return NULL;
184 struct object_list *object_list_insert(struct object *item,
185 struct object_list **list_p)
187 struct object_list *new_list = xmalloc(sizeof(struct object_list));
188 new_list->item = item;
189 new_list->next = *list_p;
190 *list_p = new_list;
191 return new_list;
194 void object_list_append(struct object *item,
195 struct object_list **list_p)
197 while (*list_p) {
198 list_p = &((*list_p)->next);
200 *list_p = xmalloc(sizeof(struct object_list));
201 (*list_p)->next = NULL;
202 (*list_p)->item = item;
205 unsigned object_list_length(struct object_list *list)
207 unsigned ret = 0;
208 while (list) {
209 list = list->next;
210 ret++;
212 return ret;
215 int object_list_contains(struct object_list *list, struct object *obj)
217 while (list) {
218 if (list->item == obj)
219 return 1;
220 list = list->next;
222 return 0;