10 static int obj_allocs
;
12 static int find_object(const unsigned char *sha1
)
14 int first
= 0, last
= nr_objs
;
16 while (first
< last
) {
17 int next
= (first
+ last
) / 2;
18 struct object
*obj
= objs
[next
];
21 cmp
= memcmp(sha1
, obj
->sha1
, 20);
33 struct object
*lookup_object(const unsigned char *sha1
)
35 int pos
= find_object(sha1
);
41 void created_object(const unsigned char *sha1
, struct object
*obj
)
43 int pos
= find_object(sha1
);
46 memcpy(obj
->sha1
, sha1
, 20);
52 die("Inserting %s twice\n", sha1_to_hex(sha1
));
55 if (obj_allocs
== nr_objs
) {
56 obj_allocs
= alloc_nr(obj_allocs
);
57 objs
= xrealloc(objs
, obj_allocs
* sizeof(struct object
*));
60 /* Insert it into the right place */
61 memmove(objs
+ pos
+ 1, objs
+ pos
, (nr_objs
- pos
) *
62 sizeof(struct object
*));
68 void add_ref(struct object
*refer
, struct object
*target
)
70 struct object_list
**pp
= &refer
->refs
;
71 struct object_list
*p
;
73 while ((p
= *pp
) != NULL
) {
74 if (p
->item
== target
)
80 p
= xmalloc(sizeof(*p
));
86 void mark_reachable(struct object
*obj
, unsigned int mask
)
88 struct object_list
*p
= obj
->refs
;
90 /* If we've been here already, don't bother */
91 if (obj
->flags
& mask
)
95 mark_reachable(p
->item
, mask
);
100 struct object
*lookup_object_type(const unsigned char *sha1
, const char *type
)
103 return lookup_unknown_object(sha1
);
104 } else if (!strcmp(type
, blob_type
)) {
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
;
113 error("Unknown type %s", type
);
119 struct object object
;
120 struct commit commit
;
126 struct object
*lookup_unknown_object(const unsigned char *sha1
)
128 struct object
*obj
= lookup_object(sha1
);
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
;
139 struct object
*parse_object(const unsigned char *sha1
)
143 void *buffer
= read_sha1_file(sha1
, type
, &size
);
146 if (check_sha1_signature(sha1
, buffer
, size
, type
) < 0)
147 printf("sha1 mismatch %s\n", sha1_to_hex(sha1
));
148 if (!strcmp(type
, "blob")) {
149 struct blob
*blob
= lookup_blob(sha1
);
150 parse_blob_buffer(blob
, buffer
, size
);
152 } else if (!strcmp(type
, "tree")) {
153 struct tree
*tree
= lookup_tree(sha1
);
154 parse_tree_buffer(tree
, buffer
, size
);
156 } else if (!strcmp(type
, "commit")) {
157 struct commit
*commit
= lookup_commit(sha1
);
158 parse_commit_buffer(commit
, buffer
, size
);
159 if (!commit
->buffer
) {
160 commit
->buffer
= buffer
;
163 obj
= &commit
->object
;
164 } else if (!strcmp(type
, "tag")) {
165 struct tag
*tag
= lookup_tag(sha1
);
166 parse_tag_buffer(tag
, buffer
, size
);
177 struct object_list
*object_list_insert(struct object
*item
,
178 struct object_list
**list_p
)
180 struct object_list
*new_list
= xmalloc(sizeof(struct object_list
));
181 new_list
->item
= item
;
182 new_list
->next
= *list_p
;
187 void object_list_append(struct object
*item
,
188 struct object_list
**list_p
)
191 list_p
= &((*list_p
)->next
);
193 *list_p
= xmalloc(sizeof(struct object_list
));
194 (*list_p
)->next
= NULL
;
195 (*list_p
)->item
= item
;
198 unsigned object_list_length(struct object_list
*list
)
208 int object_list_contains(struct object_list
*list
, struct object
*obj
)
211 if (list
->item
== obj
)