Add raw tree buffer info to "struct tree"
[git/dkf.git] / tree.c
blob1e76d9cc11dcbda655e40f19fbd82d34212383b2
1 #include "cache.h"
2 #include "tree.h"
3 #include "blob.h"
4 #include "commit.h"
5 #include "tag.h"
6 #include "tree-walk.h"
7 #include <stdlib.h>
9 const char *tree_type = "tree";
11 static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
13 int len;
14 unsigned int size;
15 struct cache_entry *ce;
17 if (S_ISDIR(mode))
18 return READ_TREE_RECURSIVE;
20 len = strlen(pathname);
21 size = cache_entry_size(baselen + len);
22 ce = xcalloc(1, size);
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);
29 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
32 static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
34 const char *match;
35 int pathlen;
37 if (!paths)
38 return 1;
39 pathlen = strlen(path);
40 while ((match = *paths++) != NULL) {
41 int matchlen = strlen(match);
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;
51 /* Does the base match? */
52 if (strncmp(base, match, baselen))
53 continue;
55 match += baselen;
56 matchlen -= baselen;
58 if (pathlen > matchlen)
59 continue;
61 if (matchlen > pathlen) {
62 if (match[pathlen] != '/')
63 continue;
64 if (!S_ISDIR(mode))
65 continue;
68 if (strncmp(path, match, pathlen))
69 continue;
71 return 1;
73 return 0;
76 int read_tree_recursive(struct tree *tree,
77 const char *base, int baselen,
78 int stage, const char **match,
79 read_tree_fn_t fn)
81 struct tree_entry_list *list;
82 if (parse_tree(tree))
83 return -1;
84 list = tree->entries;
85 while (list) {
86 struct tree_entry_list *current = list;
87 list = list->next;
88 if (!match_tree_entry(base, baselen, current->name,
89 current->mode, match))
90 continue;
92 switch (fn(current->item.any->sha1, base, baselen,
93 current->name, current->mode, stage)) {
94 case 0:
95 continue;
96 case READ_TREE_RECURSIVE:
97 break;;
98 default:
99 return -1;
101 if (current->directory) {
102 int retval;
103 int pathlen = strlen(current->name);
104 char *newbase;
106 newbase = xmalloc(baselen + 1 + pathlen);
107 memcpy(newbase, base, baselen);
108 memcpy(newbase + baselen, current->name, pathlen);
109 newbase[baselen + pathlen] = '/';
110 retval = read_tree_recursive(current->item.tree,
111 newbase,
112 baselen + pathlen + 1,
113 stage, match, fn);
114 free(newbase);
115 if (retval)
116 return -1;
117 continue;
120 return 0;
123 int read_tree(struct tree *tree, int stage, const char **match)
125 return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
128 struct tree *lookup_tree(const unsigned char *sha1)
130 struct object *obj = lookup_object(sha1);
131 if (!obj) {
132 struct tree *ret = xcalloc(1, sizeof(struct tree));
133 created_object(sha1, &ret->object);
134 ret->object.type = tree_type;
135 return ret;
137 if (!obj->type)
138 obj->type = tree_type;
139 if (obj->type != tree_type) {
140 error("Object %s is a %s, not a tree",
141 sha1_to_hex(sha1), obj->type);
142 return NULL;
144 return (struct tree *) obj;
147 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
149 struct tree_desc desc;
150 struct tree_entry_list **list_p;
151 int n_refs = 0;
153 if (item->object.parsed)
154 return 0;
155 item->object.parsed = 1;
156 item->buffer = buffer;
157 item->size = size;
159 desc.buf = buffer;
160 desc.size = size;
162 list_p = &item->entries;
163 while (desc.size) {
164 unsigned mode;
165 const char *path;
166 const unsigned char *sha1;
167 struct tree_entry_list *entry;
169 sha1 = tree_entry_extract(&desc, &path, &mode);
171 entry = xmalloc(sizeof(struct tree_entry_list));
172 entry->name = path;
173 entry->mode = mode;
174 entry->directory = S_ISDIR(mode) != 0;
175 entry->executable = (mode & S_IXUSR) != 0;
176 entry->symlink = S_ISLNK(mode) != 0;
177 entry->zeropad = *(const char *)(desc.buf) == '0';
178 entry->next = NULL;
180 update_tree_entry(&desc);
182 if (entry->directory) {
183 entry->item.tree = lookup_tree(sha1);
184 } else {
185 entry->item.blob = lookup_blob(sha1);
187 n_refs++;
188 *list_p = entry;
189 list_p = &entry->next;
192 if (track_object_refs) {
193 struct tree_entry_list *entry;
194 unsigned i = 0;
195 struct object_refs *refs = alloc_object_refs(n_refs);
196 for (entry = item->entries; entry; entry = entry->next)
197 refs->ref[i++] = entry->item.any;
198 set_object_refs(&item->object, refs);
201 return 0;
204 int parse_tree(struct tree *item)
206 char type[20];
207 void *buffer;
208 unsigned long size;
210 if (item->object.parsed)
211 return 0;
212 buffer = read_sha1_file(item->object.sha1, type, &size);
213 if (!buffer)
214 return error("Could not read %s",
215 sha1_to_hex(item->object.sha1));
216 if (strcmp(type, tree_type)) {
217 free(buffer);
218 return error("Object %s not a tree",
219 sha1_to_hex(item->object.sha1));
221 return parse_tree_buffer(item, buffer, size);
224 struct tree *parse_tree_indirect(const unsigned char *sha1)
226 struct object *obj = parse_object(sha1);
227 do {
228 if (!obj)
229 return NULL;
230 if (obj->type == tree_type)
231 return (struct tree *) obj;
232 else if (obj->type == commit_type)
233 obj = &(((struct commit *) obj)->tree->object);
234 else if (obj->type == tag_type)
235 obj = ((struct tag *) obj)->tagged;
236 else
237 return NULL;
238 if (!obj->parsed)
239 parse_object(obj->sha1);
240 } while (1);