]> git.ipfire.org Git - thirdparty/git.git/blame - tree.c
Add specialized object allocator
[thirdparty/git.git] / tree.c
CommitLineData
8f1d2e6f 1#include "cache.h"
175785e5
DB
2#include "tree.h"
3#include "blob.h"
77675e2a
DB
4#include "commit.h"
5#include "tag.h"
136f2e54 6#include "tree-walk.h"
175785e5
DB
7#include <stdlib.h>
8
9const char *tree_type = "tree";
10
3a7c352b 11static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
94537c78 12{
3c5e8468
LT
13 int len;
14 unsigned int size;
15 struct cache_entry *ce;
16
17 if (S_ISDIR(mode))
18 return READ_TREE_RECURSIVE;
19
20 len = strlen(pathname);
21 size = cache_entry_size(baselen + len);
90321c10 22 ce = xcalloc(1, size);
94537c78
LT
23
24 ce->ce_mode = create_ce_mode(mode);
25 ce->ce_flags = create_ce_flags(baselen + len, stage);
26 memcpy(ce->name, base, baselen);
27 memcpy(ce->name + baselen, pathname, len+1);
28 memcpy(ce->sha1, sha1, 20);
b155725d 29 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
94537c78
LT
30}
31
3e587635 32static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
0ca14a57 33{
3e587635 34 const char *match;
0ca14a57
LT
35 int pathlen;
36
37 if (!paths)
38 return 1;
39 pathlen = strlen(path);
40 while ((match = *paths++) != NULL) {
41 int matchlen = strlen(match);
42
43 if (baselen >= matchlen) {
44 /* If it doesn't match, move along... */
45 if (strncmp(base, match, matchlen))
46 continue;
47 /* The base is a subdirectory of a path which was specified. */
48 return 1;
49 }
50
51 /* Does the base match? */
52 if (strncmp(base, match, baselen))
53 continue;
54
55 match += baselen;
56 matchlen -= baselen;
57
58 if (pathlen > matchlen)
59 continue;
60
61 if (matchlen > pathlen) {
62 if (match[pathlen] != '/')
63 continue;
64 if (!S_ISDIR(mode))
65 continue;
66 }
67
68 if (strncmp(path, match, pathlen))
69 continue;
3e587635
LT
70
71 return 1;
0ca14a57
LT
72 }
73 return 0;
74}
75
521698b1 76int read_tree_recursive(struct tree *tree,
3c5e8468
LT
77 const char *base, int baselen,
78 int stage, const char **match,
79 read_tree_fn_t fn)
94537c78 80{
0790a42a 81 struct tree_desc desc;
4c068a98 82 struct name_entry entry;
0790a42a 83
521698b1
DB
84 if (parse_tree(tree))
85 return -1;
0790a42a
LT
86
87 desc.buf = tree->buffer;
88 desc.size = tree->size;
89
4c068a98
LT
90 while (tree_entry(&desc, &entry)) {
91 if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
0ca14a57
LT
92 continue;
93
4c068a98 94 switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) {
3c5e8468
LT
95 case 0:
96 continue;
97 case READ_TREE_RECURSIVE:
98 break;;
99 default:
100 return -1;
101 }
4c068a98 102 if (S_ISDIR(entry.mode)) {
94537c78 103 int retval;
1c9da46d 104 char *newbase;
94537c78 105
4c068a98 106 newbase = xmalloc(baselen + 1 + entry.pathlen);
94537c78 107 memcpy(newbase, base, baselen);
4c068a98
LT
108 memcpy(newbase + baselen, entry.path, entry.pathlen);
109 newbase[baselen + entry.pathlen] = '/';
110 retval = read_tree_recursive(lookup_tree(entry.sha1),
94537c78 111 newbase,
4c068a98 112 baselen + entry.pathlen + 1,
3c5e8468 113 stage, match, fn);
94537c78
LT
114 free(newbase);
115 if (retval)
116 return -1;
117 continue;
118 }
94537c78
LT
119 }
120 return 0;
121}
122
521698b1 123int read_tree(struct tree *tree, int stage, const char **match)
94537c78 124{
521698b1 125 return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
94537c78
LT
126}
127
5d6ccf5c 128struct tree *lookup_tree(const unsigned char *sha1)
175785e5
DB
129{
130 struct object *obj = lookup_object(sha1);
131 if (!obj) {
855419f7 132 struct tree *ret = alloc_tree_node();
175785e5 133 created_object(sha1, &ret->object);
885a86ab 134 ret->object.type = TYPE_TREE;
175785e5
DB
135 return ret;
136 }
d1af002d 137 if (!obj->type)
885a86ab
LT
138 obj->type = TYPE_TREE;
139 if (obj->type != TYPE_TREE) {
140 error("Object %s is a %s, not a tree",
141 sha1_to_hex(sha1), typename(obj->type));
175785e5
DB
142 return NULL;
143 }
144 return (struct tree *) obj;
145}
146
2d9c58c6 147static int track_tree_refs(struct tree *item)
175785e5 148{
2d9c58c6
LT
149 int n_refs = 0, i;
150 struct object_refs *refs;
136f2e54 151 struct tree_desc desc;
4c068a98 152 struct name_entry entry;
bd2c39f5 153
2d9c58c6
LT
154 /* Count how many entries there are.. */
155 desc.buf = item->buffer;
156 desc.size = item->size;
157 while (desc.size) {
158 n_refs++;
159 update_tree_entry(&desc);
160 }
161
162 /* Allocate object refs and walk it again.. */
163 i = 0;
164 refs = alloc_object_refs(n_refs);
165 desc.buf = item->buffer;
166 desc.size = item->size;
4c068a98 167 while (tree_entry(&desc, &entry)) {
2d9c58c6
LT
168 struct object *obj;
169
4c068a98
LT
170 if (S_ISDIR(entry.mode))
171 obj = &lookup_tree(entry.sha1)->object;
2d9c58c6 172 else
4c068a98 173 obj = &lookup_blob(entry.sha1)->object;
2d9c58c6
LT
174 refs->ref[i++] = obj;
175 }
176 set_object_refs(&item->object, refs);
177 return 0;
178}
179
180int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
181{
175785e5
DB
182 if (item->object.parsed)
183 return 0;
184 item->object.parsed = 1;
136f2e54
LT
185 item->buffer = buffer;
186 item->size = size;
187
2d9c58c6
LT
188 if (track_object_refs)
189 track_tree_refs(item);
190 return 0;
191}
192
bd2c39f5
NP
193int parse_tree(struct tree *item)
194{
195 char type[20];
196 void *buffer;
197 unsigned long size;
bd2c39f5
NP
198
199 if (item->object.parsed)
200 return 0;
201 buffer = read_sha1_file(item->object.sha1, type, &size);
202 if (!buffer)
203 return error("Could not read %s",
204 sha1_to_hex(item->object.sha1));
205 if (strcmp(type, tree_type)) {
206 free(buffer);
207 return error("Object %s not a tree",
208 sha1_to_hex(item->object.sha1));
209 }
136f2e54 210 return parse_tree_buffer(item, buffer, size);
bd2c39f5 211}
77675e2a
DB
212
213struct tree *parse_tree_indirect(const unsigned char *sha1)
214{
215 struct object *obj = parse_object(sha1);
216 do {
217 if (!obj)
218 return NULL;
885a86ab 219 if (obj->type == TYPE_TREE)
77675e2a 220 return (struct tree *) obj;
885a86ab 221 else if (obj->type == TYPE_COMMIT)
77675e2a 222 obj = &(((struct commit *) obj)->tree->object);
885a86ab 223 else if (obj->type == TYPE_TAG)
77675e2a
DB
224 obj = ((struct tag *) obj)->tagged;
225 else
226 return NULL;
227 if (!obj->parsed)
228 parse_object(obj->sha1);
229 } while (1);
230}