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
];
23 cmp
= memcmp(sha1
, obj
->sha1
, 20);
35 struct object
*lookup_object(const unsigned char *sha1
)
37 int pos
= find_object(sha1
);
43 void created_object(const unsigned char *sha1
, struct object
*obj
)
45 int pos
= find_object(sha1
);
48 memcpy(obj
->sha1
, sha1
, 20);
54 die("Inserting %s twice\n", sha1_to_hex(sha1
));
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
*));
70 void add_ref(struct object
*refer
, struct object
*target
)
72 struct object_list
**pp
, *p
;
74 if (!track_object_refs
)
78 while ((p
= *pp
) != NULL
) {
79 if (p
->item
== target
)
85 p
= xmalloc(sizeof(*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
)
102 mark_reachable(p
->item
, mask
);
107 struct object
*lookup_object_type(const unsigned char *sha1
, const char *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
;
120 error("Unknown type %s", type
);
126 struct object object
;
127 struct commit commit
;
133 struct object
*lookup_unknown_object(const unsigned char *sha1
)
135 struct object
*obj
= lookup_object(sha1
);
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
;
146 struct object
*parse_object(const unsigned char *sha1
)
150 void *buffer
= read_sha1_file(sha1
, type
, &size
);
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
);
159 } else if (!strcmp(type
, "tree")) {
160 struct tree
*tree
= lookup_tree(sha1
);
161 parse_tree_buffer(tree
, buffer
, size
);
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
;
170 obj
= &commit
->object
;
171 } else if (!strcmp(type
, "tag")) {
172 struct tag
*tag
= lookup_tag(sha1
);
173 parse_tag_buffer(tag
, buffer
, size
);
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
;
194 void object_list_append(struct object
*item
,
195 struct object_list
**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
)
215 int object_list_contains(struct object_list
*list
, struct object
*obj
)
218 if (list
->item
== obj
)