11 static int obj_allocs
;
13 static int find_object(unsigned char *sha1
)
15 int first
= 0, last
= nr_objs
;
17 while (first
< last
) {
18 int next
= (first
+ last
) / 2;
19 struct object
*obj
= objs
[next
];
22 cmp
= memcmp(sha1
, obj
->sha1
, 20);
34 struct object
*lookup_object(unsigned char *sha1
)
36 int pos
= find_object(sha1
);
42 void created_object(unsigned char *sha1
, struct object
*obj
)
44 int pos
= find_object(sha1
);
47 memcpy(obj
->sha1
, sha1
, 20);
53 die("Inserting %s twice\n", sha1_to_hex(sha1
));
56 if (obj_allocs
== nr_objs
) {
57 obj_allocs
= alloc_nr(obj_allocs
);
58 objs
= xrealloc(objs
, obj_allocs
* sizeof(struct object
*));
61 /* Insert it into the right place */
62 memmove(objs
+ pos
+ 1, objs
+ pos
, (nr_objs
- pos
) *
63 sizeof(struct object
*));
69 void add_ref(struct object
*refer
, struct object
*target
)
71 struct object_list
**pp
= &refer
->refs
;
72 struct object_list
*p
;
74 while ((p
= *pp
) != NULL
) {
75 if (p
->item
== target
)
81 p
= xmalloc(sizeof(*p
));
87 void mark_reachable(struct object
*obj
, unsigned int mask
)
89 struct object_list
*p
= obj
->refs
;
91 /* If we've been here already, don't bother */
92 if (obj
->flags
& mask
)
96 mark_reachable(p
->item
, mask
);
101 struct object
*parse_object(unsigned char *sha1
)
103 unsigned long mapsize
;
104 void *map
= map_sha1_file(sha1
, &mapsize
);
110 void *buffer
= unpack_sha1_file(map
, mapsize
, type
, &size
);
111 munmap(map
, mapsize
);
114 is_delta
= !strcmp(type
, "delta");
115 if (!is_delta
&& check_sha1_signature(sha1
, buffer
, size
, type
) < 0)
116 printf("sha1 mismatch %s\n", sha1_to_hex(sha1
));
118 struct delta
*delta
= lookup_delta(sha1
);
119 parse_delta_buffer(delta
, buffer
, size
);
120 obj
= (struct object
*) delta
;
121 } else if (!strcmp(type
, "blob")) {
122 struct blob
*blob
= lookup_blob(sha1
);
123 parse_blob_buffer(blob
, buffer
, size
);
125 } else if (!strcmp(type
, "tree")) {
126 struct tree
*tree
= lookup_tree(sha1
);
127 parse_tree_buffer(tree
, buffer
, size
);
129 } else if (!strcmp(type
, "commit")) {
130 struct commit
*commit
= lookup_commit(sha1
);
131 parse_commit_buffer(commit
, buffer
, size
);
132 if (!commit
->buffer
) {
133 commit
->buffer
= buffer
;
136 obj
= &commit
->object
;
137 } else if (!strcmp(type
, "tag")) {
138 struct tag
*tag
= lookup_tag(sha1
);
139 parse_tag_buffer(tag
, buffer
, size
);