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)
12 int len = strlen(pathname);
13 unsigned int size = cache_entry_size(baselen + len);
14 struct cache_entry *ce = xmalloc(size);
18 ce->ce_mode = create_ce_mode(mode);
19 ce->ce_flags = create_ce_flags(baselen + len, stage);
20 memcpy(ce->name, base, baselen);
21 memcpy(ce->name + baselen, pathname, len+1);
22 memcpy(ce->sha1, sha1, 20);
23 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
26 static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
33 pathlen = strlen(path);
34 while ((match = *paths++) != NULL) {
35 int matchlen = strlen(match);
37 if (baselen >= matchlen) {
38 /* If it doesn't match, move along... */
39 if (strncmp(base, match, matchlen))
41 /* The base is a subdirectory of a path which was specified. */
45 /* Does the base match? */
46 if (strncmp(base, match, baselen))
52 if (pathlen > matchlen)
55 if (matchlen > pathlen) {
56 if (match[pathlen] != '/')
62 if (strncmp(path, match, pathlen))
70 static int read_tree_recursive(void *buffer, unsigned long size,
71 const char *base, int baselen,
72 int stage, const char **match)
75 int len = strlen(buffer)+1;
76 unsigned char *sha1 = buffer + len;
77 char *path = strchr(buffer, ' ')+1;
80 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
86 if (!match_tree_entry(base, baselen, path, mode, match))
91 int pathlen = strlen(path);
95 unsigned long eltsize;
97 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
98 if (!eltbuf || strcmp(elttype, "tree")) {
99 if (eltbuf) free(eltbuf);
102 newbase = xmalloc(baselen + 1 + pathlen);
103 memcpy(newbase, base, baselen);
104 memcpy(newbase + baselen, path, pathlen);
105 newbase[baselen + pathlen] = '/';
106 retval = read_tree_recursive(eltbuf, eltsize,
108 baselen + pathlen + 1,
116 if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
122 int read_tree(void *buffer, unsigned long size, int stage, const char **match)
124 return read_tree_recursive(buffer, size, "", 0, stage, match);
127 struct tree *lookup_tree(const unsigned char *sha1)
129 struct object *obj = lookup_object(sha1);
131 struct tree *ret = xmalloc(sizeof(struct tree));
132 memset(ret, 0, sizeof(struct tree));
133 created_object(sha1, &ret->object);
134 ret->object.type = tree_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);
144 return (struct tree *) obj;
147 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
149 void *bufptr = buffer;
150 struct tree_entry_list **list_p;
153 if (item->object.parsed)
155 item->object.parsed = 1;
156 list_p = &item->entries;
159 struct tree_entry_list *entry;
160 int len = 1+strlen(bufptr);
161 unsigned char *file_sha1 = bufptr + len;
162 char *path = strchr(bufptr, ' ');
164 if (size < len + 20 || !path ||
165 sscanf(bufptr, "%o", &mode) != 1)
168 entry = xmalloc(sizeof(struct tree_entry_list));
169 entry->name = strdup(path + 1);
170 entry->directory = S_ISDIR(mode) != 0;
171 entry->executable = (mode & S_IXUSR) != 0;
172 entry->symlink = S_ISLNK(mode) != 0;
173 entry->zeropad = *(char *)bufptr == '0';
180 if (entry->directory) {
181 entry->item.tree = lookup_tree(file_sha1);
182 obj = &entry->item.tree->object;
184 entry->item.blob = lookup_blob(file_sha1);
185 obj = &entry->item.blob->object;
189 entry->parent = NULL; /* needs to be filled by the user */
191 list_p = &entry->next;
194 if (track_object_refs) {
195 struct tree_entry_list *entry;
197 struct object_refs *refs = alloc_object_refs(n_refs);
198 for (entry = item->entries; entry; entry = entry->next)
199 refs->ref[i++] = entry->item.any;
200 set_object_refs(&item->object, refs);
206 int parse_tree(struct tree *item)
213 if (item->object.parsed)
215 buffer = read_sha1_file(item->object.sha1, type, &size);
217 return error("Could not read %s",
218 sha1_to_hex(item->object.sha1));
219 if (strcmp(type, tree_type)) {
221 return error("Object %s not a tree",
222 sha1_to_hex(item->object.sha1));
224 ret = parse_tree_buffer(item, buffer, size);
229 struct tree *parse_tree_indirect(const unsigned char *sha1)
231 struct object *obj = parse_object(sha1);
235 if (obj->type == tree_type)
236 return (struct tree *) obj;
237 else if (obj->type == commit_type)
238 obj = &(((struct commit *) obj)->tree->object);
239 else if (obj->type == tag_type)
240 obj = ((struct tag *) obj)->tagged;
244 parse_object(obj->sha1);