12 int track_object_refs
= 1;
14 static int hashtable_index(const unsigned char *sha1
)
17 memcpy(&i
, sha1
, sizeof(unsigned int));
18 return (int)(i
% obj_allocs
);
21 static int find_object(const unsigned char *sha1
)
28 i
= hashtable_index(sha1
);
30 if (memcmp(sha1
, objs
[i
]->sha1
, 20) == 0)
39 struct object
*lookup_object(const unsigned char *sha1
)
41 int pos
= find_object(sha1
);
47 void created_object(const unsigned char *sha1
, struct object
*obj
)
52 memcpy(obj
->sha1
, sha1
, 20);
57 if (obj_allocs
- 1 <= nr_objs
* 2) {
58 int i
, count
= obj_allocs
;
59 obj_allocs
= (obj_allocs
< 32 ? 32 : 2 * obj_allocs
);
60 objs
= xrealloc(objs
, obj_allocs
* sizeof(struct object
*));
61 memset(objs
+ count
, 0, (obj_allocs
- count
)
62 * sizeof(struct object
*));
63 for (i
= 0; i
< obj_allocs
; i
++)
65 int j
= find_object(objs
[i
]->sha1
);
74 pos
= find_object(sha1
);
76 die("Inserting %s twice\n", sha1_to_hex(sha1
));
83 struct object_refs
*alloc_object_refs(unsigned count
)
85 struct object_refs
*refs
;
86 size_t size
= sizeof(*refs
) + count
*sizeof(struct object
*);
89 memset(refs
, 0, size
);
94 static int compare_object_pointers(const void *a
, const void *b
)
96 const struct object
* const *pa
= a
;
97 const struct object
* const *pb
= b
;
106 void set_object_refs(struct object
*obj
, struct object_refs
*refs
)
110 /* Do not install empty list of references */
111 if (refs
->count
< 1) {
116 /* Sort the list and filter out duplicates */
117 qsort(refs
->ref
, refs
->count
, sizeof(refs
->ref
[0]),
118 compare_object_pointers
);
119 for (i
= j
= 1; i
< refs
->count
; i
++) {
120 if (refs
->ref
[i
] != refs
->ref
[i
- 1])
121 refs
->ref
[j
++] = refs
->ref
[i
];
123 if (j
< refs
->count
) {
124 /* Duplicates were found - reallocate list */
125 size_t size
= sizeof(*refs
) + j
*sizeof(struct object
*);
127 refs
= xrealloc(refs
, size
);
130 for (i
= 0; i
< refs
->count
; i
++)
131 refs
->ref
[i
]->used
= 1;
135 void mark_reachable(struct object
*obj
, unsigned int mask
)
137 if (!track_object_refs
)
138 die("cannot do reachability with object refs turned off");
139 /* If we've been here already, don't bother */
140 if (obj
->flags
& mask
)
144 const struct object_refs
*refs
= obj
->refs
;
146 for (i
= 0; i
< refs
->count
; i
++)
147 mark_reachable(refs
->ref
[i
], mask
);
151 struct object
*lookup_object_type(const unsigned char *sha1
, const char *type
)
154 return lookup_unknown_object(sha1
);
155 } else if (!strcmp(type
, blob_type
)) {
156 return &lookup_blob(sha1
)->object
;
157 } else if (!strcmp(type
, tree_type
)) {
158 return &lookup_tree(sha1
)->object
;
159 } else if (!strcmp(type
, commit_type
)) {
160 return &lookup_commit(sha1
)->object
;
161 } else if (!strcmp(type
, tag_type
)) {
162 return &lookup_tag(sha1
)->object
;
164 error("Unknown type %s", type
);
170 struct object object
;
171 struct commit commit
;
177 struct object
*lookup_unknown_object(const unsigned char *sha1
)
179 struct object
*obj
= lookup_object(sha1
);
181 union any_object
*ret
= xmalloc(sizeof(*ret
));
182 memset(ret
, 0, sizeof(*ret
));
183 created_object(sha1
, &ret
->object
);
184 ret
->object
.type
= NULL
;
190 struct object
*parse_object(const unsigned char *sha1
)
194 void *buffer
= read_sha1_file(sha1
, type
, &size
);
197 if (check_sha1_signature(sha1
, buffer
, size
, type
) < 0)
198 printf("sha1 mismatch %s\n", sha1_to_hex(sha1
));
199 if (!strcmp(type
, "blob")) {
200 struct blob
*blob
= lookup_blob(sha1
);
201 parse_blob_buffer(blob
, buffer
, size
);
203 } else if (!strcmp(type
, "tree")) {
204 struct tree
*tree
= lookup_tree(sha1
);
205 parse_tree_buffer(tree
, buffer
, size
);
207 } else if (!strcmp(type
, "commit")) {
208 struct commit
*commit
= lookup_commit(sha1
);
209 parse_commit_buffer(commit
, buffer
, size
);
210 if (!commit
->buffer
) {
211 commit
->buffer
= buffer
;
214 obj
= &commit
->object
;
215 } else if (!strcmp(type
, "tag")) {
216 struct tag
*tag
= lookup_tag(sha1
);
217 parse_tag_buffer(tag
, buffer
, size
);
228 struct object_list
*object_list_insert(struct object
*item
,
229 struct object_list
**list_p
)
231 struct object_list
*new_list
= xmalloc(sizeof(struct object_list
));
232 new_list
->item
= item
;
233 new_list
->next
= *list_p
;
238 void object_list_append(struct object
*item
,
239 struct object_list
**list_p
)
242 list_p
= &((*list_p
)->next
);
244 *list_p
= xmalloc(sizeof(struct object_list
));
245 (*list_p
)->next
= NULL
;
246 (*list_p
)->item
= item
;
249 unsigned object_list_length(struct object_list
*list
)
259 int object_list_contains(struct object_list
*list
, struct object
*obj
)
262 if (list
->item
== obj
)