8 const char *tree_type = "tree";
10 static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
14 struct cache_entry *ce;
17 return READ_TREE_RECURSIVE;
19 len = strlen(pathname);
20 size = cache_entry_size(baselen + len);
25 ce->ce_mode = create_ce_mode(mode);
26 ce->ce_flags = create_ce_flags(baselen + len, stage);
27 memcpy(ce->name, base, baselen);
28 memcpy(ce->name + baselen, pathname, len+1);
29 memcpy(ce->sha1, sha1, 20);
30 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
33 static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
40 pathlen = strlen(path);
41 while ((match = *paths++) != NULL) {
42 int matchlen = strlen(match);
44 if (baselen >= matchlen) {
45 /* If it doesn't match, move along... */
46 if (strncmp(base, match, matchlen))
48 /* The base is a subdirectory of a path which was specified. */
52 /* Does the base match? */
53 if (strncmp(base, match, baselen))
59 if (pathlen > matchlen)
62 if (matchlen > pathlen) {
63 if (match[pathlen] != '/')
69 if (strncmp(path, match, pathlen))
77 int read_tree_recursive(struct tree *tree,
78 const char *base, int baselen,
79 int stage, const char **match,
82 struct tree_entry_list *list;
87 struct tree_entry_list *current = list;
89 if (!match_tree_entry(base, baselen, current->name,
90 current->mode, match))
93 switch (fn(current->item.any->sha1, base, baselen,
94 current->name, current->mode, stage)) {
97 case READ_TREE_RECURSIVE:
102 if (current->directory) {
104 int pathlen = strlen(current->name);
107 newbase = xmalloc(baselen + 1 + pathlen);
108 memcpy(newbase, base, baselen);
109 memcpy(newbase + baselen, current->name, pathlen);
110 newbase[baselen + pathlen] = '/';
111 retval = read_tree_recursive(current->item.tree,
113 baselen + pathlen + 1,
124 int read_tree(struct tree *tree, int stage, const char **match)
126 return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
129 struct tree *lookup_tree(const unsigned char *sha1)
131 struct object *obj = lookup_object(sha1);
133 struct tree *ret = xmalloc(sizeof(struct tree));
134 memset(ret, 0, sizeof(struct tree));
135 created_object(sha1, &ret->object);
136 ret->object.type = tree_type;
140 obj->type = tree_type;
141 if (obj->type != tree_type) {
142 error("Object %s is a %s, not a tree",
143 sha1_to_hex(sha1), obj->type);
146 return (struct tree *) obj;
149 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
151 void *bufptr = buffer;
152 struct tree_entry_list **list_p;
155 if (item->object.parsed)
157 item->object.parsed = 1;
158 list_p = &item->entries;
161 struct tree_entry_list *entry;
162 int len = 1+strlen(bufptr);
163 unsigned char *file_sha1 = bufptr + len;
164 char *path = strchr(bufptr, ' ');
166 if (size < len + 20 || !path ||
167 sscanf(bufptr, "%o", &mode) != 1)
170 entry = xmalloc(sizeof(struct tree_entry_list));
171 entry->name = strdup(path + 1);
172 entry->directory = S_ISDIR(mode) != 0;
173 entry->executable = (mode & S_IXUSR) != 0;
174 entry->symlink = S_ISLNK(mode) != 0;
175 entry->zeropad = *(char *)bufptr == '0';
182 if (entry->directory) {
183 entry->item.tree = lookup_tree(file_sha1);
184 obj = &entry->item.tree->object;
186 entry->item.blob = lookup_blob(file_sha1);
187 obj = &entry->item.blob->object;
192 list_p = &entry->next;
195 if (track_object_refs) {
196 struct tree_entry_list *entry;
198 struct object_refs *refs = alloc_object_refs(n_refs);
199 for (entry = item->entries; entry; entry = entry->next)
200 refs->ref[i++] = entry->item.any;
201 set_object_refs(&item->object, refs);
207 int parse_tree(struct tree *item)
214 if (item->object.parsed)
216 buffer = read_sha1_file(item->object.sha1, type, &size);
218 return error("Could not read %s",
219 sha1_to_hex(item->object.sha1));
220 if (strcmp(type, tree_type)) {
222 return error("Object %s not a tree",
223 sha1_to_hex(item->object.sha1));
225 ret = parse_tree_buffer(item, buffer, size);
230 struct tree *parse_tree_indirect(const unsigned char *sha1)
232 struct object *obj = parse_object(sha1);
236 if (obj->type == tree_type)
237 return (struct tree *) obj;
238 else if (obj->type == commit_type)
239 obj = &(((struct commit *) obj)->tree->object);
240 else if (obj->type == tag_type)
241 obj = ((struct tag *) obj)->tagged;
245 parse_object(obj->sha1);