]> git.ipfire.org Git - thirdparty/git.git/blame - object.c
[PATCH] git-ssh-pull: commit-id consistency
[thirdparty/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
9struct object **objs;
10int nr_objs;
11static int obj_allocs;
12
5d6ccf5c 13static int find_object(const unsigned char *sha1)
175785e5
DB
14{
15 int first = 0, last = nr_objs;
16
17 while (first < last) {
18 int next = (first + last) / 2;
19 struct object *obj = objs[next];
20 int cmp;
21
22 cmp = memcmp(sha1, obj->sha1, 20);
23 if (!cmp)
24 return next;
25 if (cmp < 0) {
26 last = next;
27 continue;
28 }
29 first = next+1;
30 }
31 return -first-1;
32}
33
5d6ccf5c 34struct object *lookup_object(const unsigned char *sha1)
175785e5
DB
35{
36 int pos = find_object(sha1);
37 if (pos >= 0)
38 return objs[pos];
39 return NULL;
40}
41
5d6ccf5c 42void created_object(const unsigned char *sha1, struct object *obj)
175785e5
DB
43{
44 int pos = find_object(sha1);
45
46 obj->parsed = 0;
47 memcpy(obj->sha1, sha1, 20);
48 obj->type = NULL;
49 obj->refs = NULL;
50 obj->used = 0;
51
52 if (pos >= 0)
53 die("Inserting %s twice\n", sha1_to_hex(sha1));
54 pos = -pos-1;
55
56 if (obj_allocs == nr_objs) {
57 obj_allocs = alloc_nr(obj_allocs);
812666c8 58 objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
175785e5
DB
59 }
60
61 /* Insert it into the right place */
62 memmove(objs + pos + 1, objs + pos, (nr_objs - pos) *
63 sizeof(struct object *));
64
65 objs[pos] = obj;
66 nr_objs++;
67}
68
69void add_ref(struct object *refer, struct object *target)
70{
71 struct object_list **pp = &refer->refs;
72 struct object_list *p;
73
74 while ((p = *pp) != NULL) {
75 if (p->item == target)
76 return;
77 pp = &p->next;
78 }
79
80 target->used = 1;
812666c8 81 p = xmalloc(sizeof(*p));
175785e5
DB
82 p->item = target;
83 p->next = NULL;
84 *pp = p;
85}
86
87void mark_reachable(struct object *obj, unsigned int mask)
88{
89 struct object_list *p = obj->refs;
90
91 /* If we've been here already, don't bother */
92 if (obj->flags & mask)
93 return;
94 obj->flags |= mask;
95 while (p) {
96 mark_reachable(p->item, mask);
97 p = p->next;
98 }
99}
e9eefa67 100
89e4202f
DB
101struct object *lookup_object_type(const unsigned char *sha1, const char *type)
102{
103 if (!strcmp(type, blob_type)) {
104 return &lookup_blob(sha1)->object;
105 } else if (!strcmp(type, tree_type)) {
106 return &lookup_tree(sha1)->object;
107 } else if (!strcmp(type, commit_type)) {
108 return &lookup_commit(sha1)->object;
109 } else if (!strcmp(type, tag_type)) {
110 return &lookup_tag(sha1)->object;
111 } else {
112 error("Unknown type %s", type);
113 return NULL;
114 }
115}
116
5d6ccf5c 117struct object *parse_object(const unsigned char *sha1)
e9eefa67
DB
118{
119 unsigned long mapsize;
120 void *map = map_sha1_file(sha1, &mapsize);
121 if (map) {
d1af002d 122 int is_delta;
bd2c39f5 123 struct object *obj;
e9eefa67
DB
124 char type[100];
125 unsigned long size;
126 void *buffer = unpack_sha1_file(map, mapsize, type, &size);
13019d41 127 munmap(map, mapsize);
e9eefa67
DB
128 if (!buffer)
129 return NULL;
d1af002d
NP
130 is_delta = !strcmp(type, "delta");
131 if (!is_delta && check_sha1_signature(sha1, buffer, size, type) < 0)
e9eefa67 132 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
d1af002d
NP
133 if (is_delta) {
134 struct delta *delta = lookup_delta(sha1);
135 parse_delta_buffer(delta, buffer, size);
136 obj = (struct object *) delta;
137 } else if (!strcmp(type, "blob")) {
bd2c39f5
NP
138 struct blob *blob = lookup_blob(sha1);
139 parse_blob_buffer(blob, buffer, size);
140 obj = &blob->object;
e9eefa67 141 } else if (!strcmp(type, "tree")) {
bd2c39f5
NP
142 struct tree *tree = lookup_tree(sha1);
143 parse_tree_buffer(tree, buffer, size);
144 obj = &tree->object;
e9eefa67 145 } else if (!strcmp(type, "commit")) {
bd2c39f5
NP
146 struct commit *commit = lookup_commit(sha1);
147 parse_commit_buffer(commit, buffer, size);
bd1e17e2
LT
148 if (!commit->buffer) {
149 commit->buffer = buffer;
150 buffer = NULL;
151 }
bd2c39f5 152 obj = &commit->object;
e9eefa67 153 } else if (!strcmp(type, "tag")) {
bd2c39f5
NP
154 struct tag *tag = lookup_tag(sha1);
155 parse_tag_buffer(tag, buffer, size);
156 obj = &tag->object;
e9eefa67 157 } else {
bd2c39f5 158 obj = NULL;
e9eefa67 159 }
bd2c39f5
NP
160 free(buffer);
161 return obj;
e9eefa67
DB
162 }
163 return NULL;
164}