12 const char *type_names
[] = {
13 "none", "blob", "tree", "commit", "bad"
16 static int hashtable_index(const unsigned char *sha1
)
19 memcpy(&i
, sha1
, sizeof(unsigned int));
20 return (int)(i
% obj_allocs
);
23 static int find_object(const unsigned char *sha1
)
30 i
= hashtable_index(sha1
);
32 if (memcmp(sha1
, objs
[i
]->sha1
, 20) == 0)
41 struct object
*lookup_object(const unsigned char *sha1
)
43 int pos
= find_object(sha1
);
49 void created_object(const unsigned char *sha1
, struct object
*obj
)
54 memcpy(obj
->sha1
, sha1
, 20);
55 obj
->type
= TYPE_NONE
;
58 if (obj_allocs
- 1 <= nr_objs
* 2) {
59 int i
, count
= obj_allocs
;
60 obj_allocs
= (obj_allocs
< 32 ? 32 : 2 * obj_allocs
);
61 objs
= xrealloc(objs
, obj_allocs
* sizeof(struct object
*));
62 memset(objs
+ count
, 0, (obj_allocs
- count
)
63 * sizeof(struct object
*));
64 for (i
= 0; i
< obj_allocs
; i
++)
66 int j
= find_object(objs
[i
]->sha1
);
75 pos
= find_object(sha1
);
77 die("Inserting %s twice\n", sha1_to_hex(sha1
));
84 struct object
*lookup_object_type(const unsigned char *sha1
, const char *type
)
87 return lookup_unknown_object(sha1
);
88 } else if (!strcmp(type
, blob_type
)) {
89 return &lookup_blob(sha1
)->object
;
90 } else if (!strcmp(type
, tree_type
)) {
91 return &lookup_tree(sha1
)->object
;
92 } else if (!strcmp(type
, commit_type
)) {
93 return &lookup_commit(sha1
)->object
;
94 } else if (!strcmp(type
, tag_type
)) {
95 return &lookup_tag(sha1
)->object
;
97 error("Unknown type %s", type
);
103 struct object object
;
104 struct commit commit
;
110 struct object
*lookup_unknown_object(const unsigned char *sha1
)
112 struct object
*obj
= lookup_object(sha1
);
114 union any_object
*ret
= xcalloc(1, sizeof(*ret
));
115 created_object(sha1
, &ret
->object
);
116 ret
->object
.type
= TYPE_NONE
;
122 struct object
*parse_object(const unsigned char *sha1
)
126 void *buffer
= read_sha1_file(sha1
, type
, &size
);
129 if (check_sha1_signature(sha1
, buffer
, size
, type
) < 0)
130 printf("sha1 mismatch %s\n", sha1_to_hex(sha1
));
131 if (!strcmp(type
, blob_type
)) {
132 struct blob
*blob
= lookup_blob(sha1
);
133 parse_blob_buffer(blob
, buffer
, size
);
135 } else if (!strcmp(type
, tree_type
)) {
136 struct tree
*tree
= lookup_tree(sha1
);
138 if (!tree
->object
.parsed
) {
139 parse_tree_buffer(tree
, buffer
, size
);
142 } else if (!strcmp(type
, commit_type
)) {
143 struct commit
*commit
= lookup_commit(sha1
);
144 parse_commit_buffer(commit
, buffer
, size
);
145 if (!commit
->buffer
) {
146 commit
->buffer
= buffer
;
149 obj
= &commit
->object
;
150 } else if (!strcmp(type
, tag_type
)) {
151 struct tag
*tag
= lookup_tag(sha1
);
152 parse_tag_buffer(tag
, buffer
, size
);
163 struct object_list
*object_list_insert(struct object
*item
,
164 struct object_list
**list_p
)
166 struct object_list
*new_list
= xmalloc(sizeof(struct object_list
));
167 new_list
->item
= item
;
168 new_list
->next
= *list_p
;
173 void object_list_append(struct object
*item
,
174 struct object_list
**list_p
)
177 list_p
= &((*list_p
)->next
);
179 *list_p
= xmalloc(sizeof(struct object_list
));
180 (*list_p
)->next
= NULL
;
181 (*list_p
)->item
= item
;
184 unsigned object_list_length(struct object_list
*list
)
194 int object_list_contains(struct object_list
*list
, struct object
*obj
)
197 if (list
->item
== obj
)