[PATCH] delta creation
[git/dscho.git] / object.c
blobdeb683076d7e1a91e913398ff905085230014a4f
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>
11 struct object **objs;
12 int nr_objs;
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];
22 int cmp;
24 cmp = memcmp(sha1, obj->sha1, 20);
25 if (!cmp)
26 return next;
27 if (cmp < 0) {
28 last = next;
29 continue;
31 first = next+1;
33 return -first-1;
36 struct object *lookup_object(unsigned char *sha1)
38 int pos = find_object(sha1);
39 if (pos >= 0)
40 return objs[pos];
41 return NULL;
44 void created_object(unsigned char *sha1, struct object *obj)
46 int pos = find_object(sha1);
48 obj->parsed = 0;
49 memcpy(obj->sha1, sha1, 20);
50 obj->type = NULL;
51 obj->refs = NULL;
52 obj->used = 0;
54 if (pos >= 0)
55 die("Inserting %s twice\n", sha1_to_hex(sha1));
56 pos = -pos-1;
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 *));
67 objs[pos] = obj;
68 nr_objs++;
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)
78 return;
79 pp = &p->next;
82 target->used = 1;
83 p = xmalloc(sizeof(*p));
84 p->item = target;
85 p->next = NULL;
86 *pp = 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)
95 return;
96 obj->flags |= mask;
97 while (p) {
98 mark_reachable(p->item, mask);
99 p = p->next;
103 struct object *parse_object(unsigned char *sha1)
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;
142 free(buffer);
143 return obj;
145 return NULL;