[PATCH] delta check
[git/git.git] / object.c
CommitLineData
175785e5 1#include "object.h"
e9eefa67
DB
2#include "blob.h"
3#include "tree.h"
4#include "commit.h"
175785e5 5#include "cache.h"
e9eefa67 6#include "tag.h"
d1af002d 7#include "delta.h"
175785e5
DB
8#include <stdlib.h>
9#include <string.h>
10
11struct object **objs;
12int nr_objs;
13static int obj_allocs;
14
15static 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
36struct 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
44void 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);
812666c8 60 objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
175785e5
DB
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
71void 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;
812666c8 83 p = xmalloc(sizeof(*p));
175785e5
DB
84 p->item = target;
85 p->next = NULL;
86 *pp = p;
87}
88
89void 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}
e9eefa67
DB
102
103struct object *parse_object(unsigned char *sha1)
104{
105 unsigned long mapsize;
106 void *map = map_sha1_file(sha1, &mapsize);
107 if (map) {
d1af002d 108 int is_delta;
bd2c39f5 109 struct object *obj;
e9eefa67
DB
110 char type[100];
111 unsigned long size;
112 void *buffer = unpack_sha1_file(map, mapsize, type, &size);
13019d41 113 munmap(map, mapsize);
e9eefa67
DB
114 if (!buffer)
115 return NULL;
d1af002d
NP
116 is_delta = !strcmp(type, "delta");
117 if (!is_delta && check_sha1_signature(sha1, buffer, size, type) < 0)
e9eefa67 118 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
d1af002d
NP
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")) {
bd2c39f5
NP
124 struct blob *blob = lookup_blob(sha1);
125 parse_blob_buffer(blob, buffer, size);
126 obj = &blob->object;
e9eefa67 127 } else if (!strcmp(type, "tree")) {
bd2c39f5
NP
128 struct tree *tree = lookup_tree(sha1);
129 parse_tree_buffer(tree, buffer, size);
130 obj = &tree->object;
e9eefa67 131 } else if (!strcmp(type, "commit")) {
bd2c39f5
NP
132 struct commit *commit = lookup_commit(sha1);
133 parse_commit_buffer(commit, buffer, size);
134 obj = &commit->object;
e9eefa67 135 } else if (!strcmp(type, "tag")) {
bd2c39f5
NP
136 struct tag *tag = lookup_tag(sha1);
137 parse_tag_buffer(tag, buffer, size);
138 obj = &tag->object;
e9eefa67 139 } else {
bd2c39f5 140 obj = NULL;
e9eefa67 141 }
bd2c39f5
NP
142 free(buffer);
143 return obj;
e9eefa67
DB
144 }
145 return NULL;
146}