]> git.ipfire.org Git - thirdparty/git.git/blame - object.c
[PATCH] rename git-rpush and git-rpull to git-ssh-push and git-ssh-pull
[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
13static int find_object(unsigned char *sha1)
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
34struct object *lookup_object(unsigned char *sha1)
35{
36 int pos = find_object(sha1);
37 if (pos >= 0)
38 return objs[pos];
39 return NULL;
40}
41
42void created_object(unsigned char *sha1, struct object *obj)
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
DB
100
101struct object *parse_object(unsigned char *sha1)
102{
103 unsigned long mapsize;
104 void *map = map_sha1_file(sha1, &mapsize);
105 if (map) {
d1af002d 106 int is_delta;
bd2c39f5 107 struct object *obj;
e9eefa67
DB
108 char type[100];
109 unsigned long size;
110 void *buffer = unpack_sha1_file(map, mapsize, type, &size);
13019d41 111 munmap(map, mapsize);
e9eefa67
DB
112 if (!buffer)
113 return NULL;
d1af002d
NP
114 is_delta = !strcmp(type, "delta");
115 if (!is_delta && check_sha1_signature(sha1, buffer, size, type) < 0)
e9eefa67 116 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
d1af002d
NP
117 if (is_delta) {
118 struct delta *delta = lookup_delta(sha1);
119 parse_delta_buffer(delta, buffer, size);
120 obj = (struct object *) delta;
121 } else if (!strcmp(type, "blob")) {
bd2c39f5
NP
122 struct blob *blob = lookup_blob(sha1);
123 parse_blob_buffer(blob, buffer, size);
124 obj = &blob->object;
e9eefa67 125 } else if (!strcmp(type, "tree")) {
bd2c39f5
NP
126 struct tree *tree = lookup_tree(sha1);
127 parse_tree_buffer(tree, buffer, size);
128 obj = &tree->object;
e9eefa67 129 } else if (!strcmp(type, "commit")) {
bd2c39f5
NP
130 struct commit *commit = lookup_commit(sha1);
131 parse_commit_buffer(commit, buffer, size);
bd1e17e2
LT
132 if (!commit->buffer) {
133 commit->buffer = buffer;
134 buffer = NULL;
135 }
bd2c39f5 136 obj = &commit->object;
e9eefa67 137 } else if (!strcmp(type, "tag")) {
bd2c39f5
NP
138 struct tag *tag = lookup_tag(sha1);
139 parse_tag_buffer(tag, buffer, size);
140 obj = &tag->object;
e9eefa67 141 } else {
bd2c39f5 142 obj = NULL;
e9eefa67 143 }
bd2c39f5
NP
144 free(buffer);
145 return obj;
e9eefa67
DB
146 }
147 return NULL;
148}