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);
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 memcpy(ce->sha1, sha1, 20);
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)
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))
46 /* The base is a subdirectory of a path which was specified. */
50 /* Does the base match? */
51 if (strncmp(base, match, baselen))
57 if (pathlen > matchlen)
60 if (matchlen > pathlen) {
61 if (match[pathlen] != '/')
67 if (strncmp(path, match, pathlen))
75 int read_tree_recursive(struct tree *tree,
76 const char *base, int baselen,
77 int stage, const char **match,
80 struct tree_entry_list *list;
85 struct tree_entry_list *current = list;
87 if (!match_tree_entry(base, baselen, current->name,
88 current->mode, match))
91 switch (fn(current->item.any->sha1, base, baselen,
92 current->name, current->mode, stage)) {
95 case READ_TREE_RECURSIVE:
100 if (current->directory) {
102 int pathlen = strlen(current->name);
105 newbase = xmalloc(baselen + 1 + pathlen);
106 memcpy(newbase, base, baselen);
107 memcpy(newbase + baselen, current->name, pathlen);
108 newbase[baselen + pathlen] = '/';
109 retval = read_tree_recursive(current->item.tree,
111 baselen + pathlen + 1,
122 int read_tree(struct tree *tree, int stage, const char **match)
124 return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
127 struct tree *lookup_tree(const unsigned char *sha1)
129 struct object *obj = lookup_object(sha1);
131 struct tree *ret = xcalloc(1, sizeof(struct tree));
132 created_object(sha1, &ret->object);
133 ret->object.type = tree_type;
137 obj->type = tree_type;
138 if (obj->type != tree_type) {
139 error("Object %s is a %s, not a tree",
140 sha1_to_hex(sha1), obj->type);
143 return (struct tree *) obj;
146 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
148 void *bufptr = buffer;
149 struct tree_entry_list **list_p;
152 if (item->object.parsed)
154 item->object.parsed = 1;
155 list_p = &item->entries;
158 struct tree_entry_list *entry;
159 int len = 1+strlen(bufptr);
160 unsigned char *file_sha1 = bufptr + len;
161 char *path = strchr(bufptr, ' ');
163 if (size < len + 20 || !path ||
164 sscanf(bufptr, "%o", &mode) != 1)
167 entry = xmalloc(sizeof(struct tree_entry_list));
168 entry->name = strdup(path + 1);
169 entry->directory = S_ISDIR(mode) != 0;
170 entry->executable = (mode & S_IXUSR) != 0;
171 entry->symlink = S_ISLNK(mode) != 0;
172 entry->zeropad = *(char *)bufptr == '0';
179 if (entry->directory) {
180 entry->item.tree = lookup_tree(file_sha1);
181 obj = &entry->item.tree->object;
183 entry->item.blob = lookup_blob(file_sha1);
184 obj = &entry->item.blob->object;
189 list_p = &entry->next;
192 if (track_object_refs) {
193 struct tree_entry_list *entry;
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);
204 int parse_tree(struct tree *item)
211 if (item->object.parsed)
213 buffer = read_sha1_file(item->object.sha1, type, &size);
215 return error("Could not read %s",
216 sha1_to_hex(item->object.sha1));
217 if (strcmp(type, tree_type)) {
219 return error("Object %s not a tree",
220 sha1_to_hex(item->object.sha1));
222 ret = parse_tree_buffer(item, buffer, size);
227 struct tree *parse_tree_indirect(const unsigned char *sha1)
229 struct object *obj = parse_object(sha1);
233 if (obj->type == tree_type)
234 return (struct tree *) obj;
235 else if (obj->type == commit_type)
236 obj = &(((struct commit *) obj)->tree->object);
237 else if (obj->type == tag_type)
238 obj = ((struct tag *) obj)->tagged;
242 parse_object(obj->sha1);