]>
Commit | Line | Data |
---|---|---|
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 | |
8 | struct object **objs; | |
9 | int nr_objs; | |
10 | static int obj_allocs; | |
11 | ||
8805ccac LT |
12 | int track_object_refs = 1; |
13 | ||
5d6ccf5c | 14 | static 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 | 35 | struct 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 | 43 | void 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 | ||
70 | void add_ref(struct object *refer, struct object *target) | |
71 | { | |
8805ccac LT |
72 | struct object_list **pp, *p; |
73 | ||
74 | if (!track_object_refs) | |
75 | return; | |
76 | ||
77 | pp = &refer->refs; | |
175785e5 DB |
78 | while ((p = *pp) != NULL) { |
79 | if (p->item == target) | |
80 | return; | |
81 | pp = &p->next; | |
82 | } | |
83 | ||
84 | target->used = 1; | |
812666c8 | 85 | p = xmalloc(sizeof(*p)); |
175785e5 DB |
86 | p->item = target; |
87 | p->next = NULL; | |
88 | *pp = p; | |
89 | } | |
90 | ||
91 | void mark_reachable(struct object *obj, unsigned int mask) | |
92 | { | |
93 | struct object_list *p = obj->refs; | |
94 | ||
8805ccac LT |
95 | if (!track_object_refs) |
96 | die("cannot do reachability with object refs turned off"); | |
175785e5 DB |
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; | |
104 | } | |
105 | } | |
e9eefa67 | 106 | |
89e4202f DB |
107 | struct object *lookup_object_type(const unsigned char *sha1, const char *type) |
108 | { | |
66e481b0 DB |
109 | if (!type) { |
110 | return lookup_unknown_object(sha1); | |
111 | } else if (!strcmp(type, blob_type)) { | |
89e4202f DB |
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; | |
122 | } | |
123 | } | |
124 | ||
66e481b0 DB |
125 | union any_object { |
126 | struct object object; | |
127 | struct commit commit; | |
128 | struct tree tree; | |
129 | struct blob blob; | |
130 | struct tag tag; | |
131 | }; | |
132 | ||
133 | struct 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 | ||
5d6ccf5c | 146 | struct object *parse_object(const unsigned char *sha1) |
e9eefa67 | 147 | { |
c4584ae3 JH |
148 | unsigned long size; |
149 | char type[20]; | |
150 | void *buffer = read_sha1_file(sha1, type, &size); | |
151 | if (buffer) { | |
bd2c39f5 | 152 | struct object *obj; |
c4584ae3 | 153 | if (check_sha1_signature(sha1, buffer, size, type) < 0) |
e9eefa67 | 154 | printf("sha1 mismatch %s\n", sha1_to_hex(sha1)); |
c4584ae3 | 155 | if (!strcmp(type, "blob")) { |
bd2c39f5 NP |
156 | struct blob *blob = lookup_blob(sha1); |
157 | parse_blob_buffer(blob, buffer, size); | |
158 | obj = &blob->object; | |
e9eefa67 | 159 | } else if (!strcmp(type, "tree")) { |
bd2c39f5 NP |
160 | struct tree *tree = lookup_tree(sha1); |
161 | parse_tree_buffer(tree, buffer, size); | |
162 | obj = &tree->object; | |
e9eefa67 | 163 | } else if (!strcmp(type, "commit")) { |
bd2c39f5 NP |
164 | struct commit *commit = lookup_commit(sha1); |
165 | parse_commit_buffer(commit, buffer, size); | |
bd1e17e2 LT |
166 | if (!commit->buffer) { |
167 | commit->buffer = buffer; | |
168 | buffer = NULL; | |
169 | } | |
bd2c39f5 | 170 | obj = &commit->object; |
e9eefa67 | 171 | } else if (!strcmp(type, "tag")) { |
bd2c39f5 NP |
172 | struct tag *tag = lookup_tag(sha1); |
173 | parse_tag_buffer(tag, buffer, size); | |
174 | obj = &tag->object; | |
e9eefa67 | 175 | } else { |
bd2c39f5 | 176 | obj = NULL; |
e9eefa67 | 177 | } |
bd2c39f5 NP |
178 | free(buffer); |
179 | return obj; | |
e9eefa67 DB |
180 | } |
181 | return NULL; | |
182 | } | |
66e481b0 DB |
183 | |
184 | struct 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 | ||
680bab3d DB |
194 | void 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 | ||
66e481b0 DB |
205 | unsigned 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 | ||
215 | int 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 | } |