[PATCH] delta check
[git/git.git] / object.c
1 #include "object.h"
2 #include "blob.h"
3 #include "tree.h"
4 #include "commit.h"
5 #include "cache.h"
6 #include "tag.h"
7 #include "delta.h"
8 #include <stdlib.h>
9 #include <string.h>
10
11 struct object **objs;
12 int nr_objs;
13 static int obj_allocs;
14
15 static int find_object(unsigned char *sha1)
16 {
17 int first = 0, last = nr_objs;
18
19 while (first < last) {
20 int next = (first + last) / 2;
21 struct object *obj = objs[next];
22 int cmp;
23
24 cmp = memcmp(sha1, obj->sha1, 20);
25 if (!cmp)
26 return next;
27 if (cmp < 0) {
28 last = next;
29 continue;
30 }
31 first = next+1;
32 }
33 return -first-1;
34 }
35
36 struct object *lookup_object(unsigned char *sha1)
37 {
38 int pos = find_object(sha1);
39 if (pos >= 0)
40 return objs[pos];
41 return NULL;
42 }
43
44 void created_object(unsigned char *sha1, struct object *obj)
45 {
46 int pos = find_object(sha1);
47
48 obj->parsed = 0;
49 memcpy(obj->sha1, sha1, 20);
50 obj->type = NULL;
51 obj->refs = NULL;
52 obj->used = 0;
53
54 if (pos >= 0)
55 die("Inserting %s twice\n", sha1_to_hex(sha1));
56 pos = -pos-1;
57
58 if (obj_allocs == nr_objs) {
59 obj_allocs = alloc_nr(obj_allocs);
60 objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
61 }
62
63 /* Insert it into the right place */
64 memmove(objs + pos + 1, objs + pos, (nr_objs - pos) *
65 sizeof(struct object *));
66
67 objs[pos] = obj;
68 nr_objs++;
69 }
70
71 void add_ref(struct object *refer, struct object *target)
72 {
73 struct object_list **pp = &refer->refs;
74 struct object_list *p;
75
76 while ((p = *pp) != NULL) {
77 if (p->item == target)
78 return;
79 pp = &p->next;
80 }
81
82 target->used = 1;
83 p = xmalloc(sizeof(*p));
84 p->item = target;
85 p->next = NULL;
86 *pp = p;
87 }
88
89 void mark_reachable(struct object *obj, unsigned int mask)
90 {
91 struct object_list *p = obj->refs;
92
93 /* If we've been here already, don't bother */
94 if (obj->flags & mask)
95 return;
96 obj->flags |= mask;
97 while (p) {
98 mark_reachable(p->item, mask);
99 p = p->next;
100 }
101 }
102
103 struct object *parse_object(unsigned char *sha1)
104 {
105 unsigned long mapsize;
106 void *map = map_sha1_file(sha1, &mapsize);
107 if (map) {
108 int is_delta;
109 struct object *obj;
110 char type[100];
111 unsigned long size;
112 void *buffer = unpack_sha1_file(map, mapsize, type, &size);
113 munmap(map, mapsize);
114 if (!buffer)
115 return NULL;
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));
119 if (is_delta) {
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);
126 obj = &blob->object;
127 } else if (!strcmp(type, "tree")) {
128 struct tree *tree = lookup_tree(sha1);
129 parse_tree_buffer(tree, buffer, size);
130 obj = &tree->object;
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);
138 obj = &tag->object;
139 } else {
140 obj = NULL;
141 }
142 free(buffer);
143 return obj;
144 }
145 return NULL;
146 }