[PATCH] don't load and decompress objects twice with parse_object()
[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"
175785e5
DB
7#include <stdlib.h>
8#include <string.h>
9
10struct object **objs;
11int nr_objs;
12static int obj_allocs;
13
14static int find_object(unsigned char *sha1)
15{
16 int first = 0, last = nr_objs;
17
18 while (first < last) {
19 int next = (first + last) / 2;
20 struct object *obj = objs[next];
21 int cmp;
22
23 cmp = memcmp(sha1, obj->sha1, 20);
24 if (!cmp)
25 return next;
26 if (cmp < 0) {
27 last = next;
28 continue;
29 }
30 first = next+1;
31 }
32 return -first-1;
33}
34
35struct object *lookup_object(unsigned char *sha1)
36{
37 int pos = find_object(sha1);
38 if (pos >= 0)
39 return objs[pos];
40 return NULL;
41}
42
43void created_object(unsigned char *sha1, struct object *obj)
44{
45 int pos = find_object(sha1);
46
47 obj->parsed = 0;
48 memcpy(obj->sha1, sha1, 20);
49 obj->type = NULL;
50 obj->refs = NULL;
51 obj->used = 0;
52
53 if (pos >= 0)
54 die("Inserting %s twice\n", sha1_to_hex(sha1));
55 pos = -pos-1;
56
57 if (obj_allocs == nr_objs) {
58 obj_allocs = alloc_nr(obj_allocs);
812666c8 59 objs = xrealloc(objs, obj_allocs * sizeof(struct object *));
175785e5
DB
60 }
61
62 /* Insert it into the right place */
63 memmove(objs + pos + 1, objs + pos, (nr_objs - pos) *
64 sizeof(struct object *));
65
66 objs[pos] = obj;
67 nr_objs++;
68}
69
70void add_ref(struct object *refer, struct object *target)
71{
72 struct object_list **pp = &refer->refs;
73 struct object_list *p;
74
75 while ((p = *pp) != NULL) {
76 if (p->item == target)
77 return;
78 pp = &p->next;
79 }
80
81 target->used = 1;
812666c8 82 p = xmalloc(sizeof(*p));
175785e5
DB
83 p->item = target;
84 p->next = NULL;
85 *pp = p;
86}
87
88void mark_reachable(struct object *obj, unsigned int mask)
89{
90 struct object_list *p = obj->refs;
91
92 /* If we've been here already, don't bother */
93 if (obj->flags & mask)
94 return;
95 obj->flags |= mask;
96 while (p) {
97 mark_reachable(p->item, mask);
98 p = p->next;
99 }
100}
e9eefa67
DB
101
102struct object *parse_object(unsigned char *sha1)
103{
104 unsigned long mapsize;
105 void *map = map_sha1_file(sha1, &mapsize);
106 if (map) {
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;
114 if (check_sha1_signature(sha1, buffer, size, type) < 0)
115 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
e9eefa67 116 if (!strcmp(type, "blob")) {
bd2c39f5
NP
117 struct blob *blob = lookup_blob(sha1);
118 parse_blob_buffer(blob, buffer, size);
119 obj = &blob->object;
e9eefa67 120 } else if (!strcmp(type, "tree")) {
bd2c39f5
NP
121 struct tree *tree = lookup_tree(sha1);
122 parse_tree_buffer(tree, buffer, size);
123 obj = &tree->object;
e9eefa67 124 } else if (!strcmp(type, "commit")) {
bd2c39f5
NP
125 struct commit *commit = lookup_commit(sha1);
126 parse_commit_buffer(commit, buffer, size);
127 obj = &commit->object;
e9eefa67 128 } else if (!strcmp(type, "tag")) {
bd2c39f5
NP
129 struct tag *tag = lookup_tag(sha1);
130 parse_tag_buffer(tag, buffer, size);
131 obj = &tag->object;
e9eefa67 132 } else {
bd2c39f5 133 obj = NULL;
e9eefa67 134 }
bd2c39f5
NP
135 free(buffer);
136 return obj;
e9eefa67
DB
137 }
138 return NULL;
139}