Remove "pathlen" from "struct name_entry"
[git/dscho.git] / tree.c
blob705a4812558654a35f03d669431d2d962b3b1bb1
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"
8 const char *tree_type = "tree";
10 static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
12 int len;
13 unsigned int size;
14 struct cache_entry *ce;
16 if (S_ISDIR(mode))
17 return READ_TREE_RECURSIVE;
19 len = strlen(pathname);
20 size = cache_entry_size(baselen + len);
21 ce = xcalloc(1, size);
23 ce->ce_mode = create_ce_mode(mode);
24 ce->ce_flags = create_ce_flags(baselen + len, stage);
25 memcpy(ce->name, base, baselen);
26 memcpy(ce->name + baselen, pathname, len+1);
27 hashcpy(ce->sha1, sha1);
28 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
31 static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
33 const char *match;
34 int pathlen;
36 if (!paths)
37 return 1;
38 pathlen = strlen(path);
39 while ((match = *paths++) != NULL) {
40 int matchlen = strlen(match);
42 if (baselen >= matchlen) {
43 /* If it doesn't match, move along... */
44 if (strncmp(base, match, matchlen))
45 continue;
46 /* The base is a subdirectory of a path which was specified. */
47 return 1;
50 /* Does the base match? */
51 if (strncmp(base, match, baselen))
52 continue;
54 match += baselen;
55 matchlen -= baselen;
57 if (pathlen > matchlen)
58 continue;
60 if (matchlen > pathlen) {
61 if (match[pathlen] != '/')
62 continue;
63 if (!S_ISDIR(mode))
64 continue;
67 if (strncmp(path, match, pathlen))
68 continue;
70 return 1;
72 return 0;
75 int read_tree_recursive(struct tree *tree,
76 const char *base, int baselen,
77 int stage, const char **match,
78 read_tree_fn_t fn)
80 struct tree_desc desc;
81 struct name_entry entry;
83 if (parse_tree(tree))
84 return -1;
86 desc.buf = tree->buffer;
87 desc.size = tree->size;
89 while (tree_entry(&desc, &entry)) {
90 if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
91 continue;
93 switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) {
94 case 0:
95 continue;
96 case READ_TREE_RECURSIVE:
97 break;;
98 default:
99 return -1;
101 if (S_ISDIR(entry.mode)) {
102 int retval;
103 char *newbase;
104 unsigned int pathlen = tree_entry_len(entry.path, entry.sha1);
106 newbase = xmalloc(baselen + 1 + pathlen);
107 memcpy(newbase, base, baselen);
108 memcpy(newbase + baselen, entry.path, pathlen);
109 newbase[baselen + pathlen] = '/';
110 retval = read_tree_recursive(lookup_tree(entry.sha1),
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 = alloc_tree_node();
133 created_object(sha1, &ret->object);
134 ret->object.type = OBJ_TREE;
135 return ret;
137 if (!obj->type)
138 obj->type = OBJ_TREE;
139 if (obj->type != OBJ_TREE) {
140 error("Object %s is a %s, not a tree",
141 sha1_to_hex(sha1), typename(obj->type));
142 return NULL;
144 return (struct tree *) obj;
147 static void track_tree_refs(struct tree *item)
149 int n_refs = 0, i;
150 struct object_refs *refs;
151 struct tree_desc desc;
152 struct name_entry entry;
154 /* Count how many entries there are.. */
155 desc.buf = item->buffer;
156 desc.size = item->size;
157 while (tree_entry(&desc, &entry))
158 n_refs++;
160 /* Allocate object refs and walk it again.. */
161 i = 0;
162 refs = alloc_object_refs(n_refs);
163 desc.buf = item->buffer;
164 desc.size = item->size;
165 while (tree_entry(&desc, &entry)) {
166 struct object *obj;
168 if (S_ISDIR(entry.mode))
169 obj = &lookup_tree(entry.sha1)->object;
170 else
171 obj = &lookup_blob(entry.sha1)->object;
172 refs->ref[i++] = obj;
174 set_object_refs(&item->object, refs);
177 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
179 if (item->object.parsed)
180 return 0;
181 item->object.parsed = 1;
182 item->buffer = buffer;
183 item->size = size;
185 if (track_object_refs)
186 track_tree_refs(item);
187 return 0;
190 int parse_tree(struct tree *item)
192 enum object_type type;
193 void *buffer;
194 unsigned long size;
196 if (item->object.parsed)
197 return 0;
198 buffer = read_sha1_file(item->object.sha1, &type, &size);
199 if (!buffer)
200 return error("Could not read %s",
201 sha1_to_hex(item->object.sha1));
202 if (type != OBJ_TREE) {
203 free(buffer);
204 return error("Object %s not a tree",
205 sha1_to_hex(item->object.sha1));
207 return parse_tree_buffer(item, buffer, size);
210 struct tree *parse_tree_indirect(const unsigned char *sha1)
212 struct object *obj = parse_object(sha1);
213 do {
214 if (!obj)
215 return NULL;
216 if (obj->type == OBJ_TREE)
217 return (struct tree *) obj;
218 else if (obj->type == OBJ_COMMIT)
219 obj = &(((struct commit *) obj)->tree->object);
220 else if (obj->type == OBJ_TAG)
221 obj = ((struct tag *) obj)->tagged;
222 else
223 return NULL;
224 if (!obj->parsed)
225 parse_object(obj->sha1);
226 } while (1);