13 static int obj_allocs
;
15 static int find_object(unsigned char *sha1
)
17 int first
= 0, last
= nr_objs
;
19 while (first
< last
) {
20 int next
= (first
+ last
) / 2;
21 struct object
*obj
= objs
[next
];
24 cmp
= memcmp(sha1
, obj
->sha1
, 20);
36 struct object
*lookup_object(unsigned char *sha1
)
38 int pos
= find_object(sha1
);
44 void created_object(unsigned char *sha1
, struct object
*obj
)
46 int pos
= find_object(sha1
);
49 memcpy(obj
->sha1
, sha1
, 20);
55 die("Inserting %s twice\n", sha1_to_hex(sha1
));
58 if (obj_allocs
== nr_objs
) {
59 obj_allocs
= alloc_nr(obj_allocs
);
60 objs
= xrealloc(objs
, obj_allocs
* sizeof(struct object
*));
63 /* Insert it into the right place */
64 memmove(objs
+ pos
+ 1, objs
+ pos
, (nr_objs
- pos
) *
65 sizeof(struct object
*));
71 void add_ref(struct object
*refer
, struct object
*target
)
73 struct object_list
**pp
= &refer
->refs
;
74 struct object_list
*p
;
76 while ((p
= *pp
) != NULL
) {
77 if (p
->item
== target
)
83 p
= xmalloc(sizeof(*p
));
89 void mark_reachable(struct object
*obj
, unsigned int mask
)
91 struct object_list
*p
= obj
->refs
;
93 /* If we've been here already, don't bother */
94 if (obj
->flags
& mask
)
98 mark_reachable(p
->item
, mask
);
103 struct object
*parse_object(unsigned char *sha1
)
105 unsigned long mapsize
;
106 void *map
= map_sha1_file(sha1
, &mapsize
);
112 void *buffer
= unpack_sha1_file(map
, mapsize
, type
, &size
);
113 munmap(map
, mapsize
);
116 is_delta
= !strcmp(type
, "delta");
117 if (!is_delta
&& check_sha1_signature(sha1
, buffer
, size
, type
) < 0)
118 printf("sha1 mismatch %s\n", sha1_to_hex(sha1
));
120 struct delta
*delta
= lookup_delta(sha1
);
121 parse_delta_buffer(delta
, buffer
, size
);
122 obj
= (struct object
*) delta
;
123 } else if (!strcmp(type
, "blob")) {
124 struct blob
*blob
= lookup_blob(sha1
);
125 parse_blob_buffer(blob
, buffer
, size
);
127 } else if (!strcmp(type
, "tree")) {
128 struct tree
*tree
= lookup_tree(sha1
);
129 parse_tree_buffer(tree
, buffer
, size
);
131 } else if (!strcmp(type
, "commit")) {
132 struct commit
*commit
= lookup_commit(sha1
);
133 parse_commit_buffer(commit
, buffer
, size
);
134 obj
= &commit
->object
;
135 } else if (!strcmp(type
, "tag")) {
136 struct tag
*tag
= lookup_tag(sha1
);
137 parse_tag_buffer(tag
, buffer
, size
);