blob: 1fdebe012ba8a6db0b6eae443e40a7f74ae2d597 [file] [log] [blame]
Daniel Barkalow175785e2005-04-18 11:39:48 -07001#include "object.h"
Daniel Barkalowe9eefa62005-04-28 07:46:33 -07002#include "blob.h"
3#include "tree.h"
4#include "commit.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07005#include "cache.h"
Daniel Barkalowe9eefa62005-04-28 07:46:33 -07006#include "tag.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07007
8struct object **objs;
9int nr_objs;
10static int obj_allocs;
11
Linus Torvalds8805cca2005-09-16 14:55:33 -070012int track_object_refs = 1;
13
Jason McMullan5d6ccf52005-06-03 11:05:39 -040014static int find_object(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070015{
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
Jason McMullan5d6ccf52005-06-03 11:05:39 -040035struct object *lookup_object(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070036{
37 int pos = find_object(sha1);
38 if (pos >= 0)
39 return objs[pos];
40 return NULL;
41}
42
Jason McMullan5d6ccf52005-06-03 11:05:39 -040043void created_object(const unsigned char *sha1, struct object *obj)
Daniel Barkalow175785e2005-04-18 11:39:48 -070044{
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);
Christopher Li812666c2005-04-26 12:00:58 -070059 objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
Daniel Barkalow175785e2005-04-18 11:39:48 -070060 }
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
70void add_ref(struct object *refer, struct object *target)
71{
Linus Torvalds8805cca2005-09-16 14:55:33 -070072 struct object_list **pp, *p;
73
74 if (!track_object_refs)
75 return;
76
77 pp = &refer->refs;
Daniel Barkalow175785e2005-04-18 11:39:48 -070078 while ((p = *pp) != NULL) {
79 if (p->item == target)
80 return;
81 pp = &p->next;
82 }
83
84 target->used = 1;
Christopher Li812666c2005-04-26 12:00:58 -070085 p = xmalloc(sizeof(*p));
Daniel Barkalow175785e2005-04-18 11:39:48 -070086 p->item = target;
87 p->next = NULL;
88 *pp = p;
89}
90
91void mark_reachable(struct object *obj, unsigned int mask)
92{
93 struct object_list *p = obj->refs;
94
Linus Torvalds8805cca2005-09-16 14:55:33 -070095 if (!track_object_refs)
96 die("cannot do reachability with object refs turned off");
Daniel Barkalow175785e2005-04-18 11:39:48 -070097 /* 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;
104 }
105}
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700106
Daniel Barkalow89e42022005-06-21 20:35:10 -0400107struct object *lookup_object_type(const unsigned char *sha1, const char *type)
108{
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400109 if (!type) {
110 return lookup_unknown_object(sha1);
111 } else if (!strcmp(type, blob_type)) {
Daniel Barkalow89e42022005-06-21 20:35:10 -0400112 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;
122 }
123}
124
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400125union any_object {
126 struct object object;
127 struct commit commit;
128 struct tree tree;
129 struct blob blob;
130 struct tag tag;
131};
132
133struct object *lookup_unknown_object(const unsigned char *sha1)
134{
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;
142 }
143 return obj;
144}
145
Jason McMullan5d6ccf52005-06-03 11:05:39 -0400146struct object *parse_object(const unsigned char *sha1)
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700147{
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700148 unsigned long size;
149 char type[20];
150 void *buffer = read_sha1_file(sha1, type, &size);
151 if (buffer) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400152 struct object *obj;
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700153 if (check_sha1_signature(sha1, buffer, size, type) < 0)
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700154 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700155 if (!strcmp(type, "blob")) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400156 struct blob *blob = lookup_blob(sha1);
157 parse_blob_buffer(blob, buffer, size);
158 obj = &blob->object;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700159 } else if (!strcmp(type, "tree")) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400160 struct tree *tree = lookup_tree(sha1);
161 parse_tree_buffer(tree, buffer, size);
162 obj = &tree->object;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700163 } else if (!strcmp(type, "commit")) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400164 struct commit *commit = lookup_commit(sha1);
165 parse_commit_buffer(commit, buffer, size);
Linus Torvaldsbd1e17e2005-05-25 19:26:28 -0700166 if (!commit->buffer) {
167 commit->buffer = buffer;
168 buffer = NULL;
169 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400170 obj = &commit->object;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700171 } else if (!strcmp(type, "tag")) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400172 struct tag *tag = lookup_tag(sha1);
173 parse_tag_buffer(tag, buffer, size);
174 obj = &tag->object;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700175 } else {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400176 obj = NULL;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700177 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400178 free(buffer);
179 return obj;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700180 }
181 return NULL;
182}
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400183
184struct object_list *object_list_insert(struct object *item,
185 struct object_list **list_p)
186{
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;
192}
193
Daniel Barkalow680bab32005-09-05 02:04:18 -0400194void object_list_append(struct object *item,
195 struct object_list **list_p)
196{
197 while (*list_p) {
198 list_p = &((*list_p)->next);
199 }
200 *list_p = xmalloc(sizeof(struct object_list));
201 (*list_p)->next = NULL;
202 (*list_p)->item = item;
203}
204
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400205unsigned object_list_length(struct object_list *list)
206{
207 unsigned ret = 0;
208 while (list) {
209 list = list->next;
210 ret++;
211 }
212 return ret;
213}
214
215int object_list_contains(struct object_list *list, struct object *obj)
216{
217 while (list) {
218 if (list->item == obj)
219 return 1;
220 list = list->next;
221 }
222 return 0;
223}