6 const char *tree_type = "tree";
8 static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
10 int len = strlen(pathname);
11 unsigned int size = cache_entry_size(baselen + len);
12 struct cache_entry *ce = xmalloc(size);
16 ce->ce_mode = create_ce_mode(mode);
17 ce->ce_flags = create_ce_flags(baselen + len, stage);
18 memcpy(ce->name, base, baselen);
19 memcpy(ce->name + baselen, pathname, len+1);
20 memcpy(ce->sha1, sha1, 20);
21 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
24 static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
31 pathlen = strlen(path);
32 while ((match = *paths++) != NULL) {
33 int matchlen = strlen(match);
35 if (baselen >= matchlen) {
36 /* If it doesn't match, move along... */
37 if (strncmp(base, match, matchlen))
39 /* The base is a subdirectory of a path which was specified. */
43 /* Does the base match? */
44 if (strncmp(base, match, baselen))
50 if (pathlen > matchlen)
53 if (matchlen > pathlen) {
54 if (match[pathlen] != '/')
60 if (strncmp(path, match, pathlen))
68 static int read_tree_recursive(void *buffer, unsigned long size,
69 const char *base, int baselen,
70 int stage, const char **match)
73 int len = strlen(buffer)+1;
74 unsigned char *sha1 = buffer + len;
75 char *path = strchr(buffer, ' ')+1;
78 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
84 if (!match_tree_entry(base, baselen, path, mode, match))
89 int pathlen = strlen(path);
93 unsigned long eltsize;
95 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
96 if (!eltbuf || strcmp(elttype, "tree")) {
97 if (eltbuf) free(eltbuf);
100 newbase = xmalloc(baselen + 1 + pathlen);
101 memcpy(newbase, base, baselen);
102 memcpy(newbase + baselen, path, pathlen);
103 newbase[baselen + pathlen] = '/';
104 retval = read_tree_recursive(eltbuf, eltsize,
106 baselen + pathlen + 1,
114 if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
120 int read_tree(void *buffer, unsigned long size, int stage, const char **match)
122 return read_tree_recursive(buffer, size, "", 0, stage, match);
125 struct tree *lookup_tree(const unsigned char *sha1)
127 struct object *obj = lookup_object(sha1);
129 struct tree *ret = xmalloc(sizeof(struct tree));
130 memset(ret, 0, sizeof(struct tree));
131 created_object(sha1, &ret->object);
132 ret->object.type = tree_type;
136 obj->type = tree_type;
137 if (obj->type != tree_type) {
138 error("Object %s is a %s, not a tree",
139 sha1_to_hex(sha1), obj->type);
142 return (struct tree *) obj;
145 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
147 void *bufptr = buffer;
148 struct tree_entry_list **list_p;
150 if (item->object.parsed)
152 item->object.parsed = 1;
153 list_p = &item->entries;
156 struct tree_entry_list *entry;
157 int len = 1+strlen(bufptr);
158 unsigned char *file_sha1 = bufptr + len;
159 char *path = strchr(bufptr, ' ');
161 if (size < len + 20 || !path ||
162 sscanf(bufptr, "%o", &mode) != 1)
165 entry = xmalloc(sizeof(struct tree_entry_list));
166 entry->name = strdup(path + 1);
167 entry->directory = S_ISDIR(mode) != 0;
168 entry->executable = (mode & S_IXUSR) != 0;
169 entry->symlink = S_ISLNK(mode) != 0;
176 if (entry->directory) {
177 entry->item.tree = lookup_tree(file_sha1);
178 obj = &entry->item.tree->object;
180 entry->item.blob = lookup_blob(file_sha1);
181 obj = &entry->item.blob->object;
184 add_ref(&item->object, obj);
185 entry->parent = NULL; /* needs to be filled by the user */
187 list_p = &entry->next;
192 int parse_tree(struct tree *item)
199 if (item->object.parsed)
201 buffer = read_sha1_file(item->object.sha1, type, &size);
203 return error("Could not read %s",
204 sha1_to_hex(item->object.sha1));
205 if (strcmp(type, tree_type)) {
207 return error("Object %s not a tree",
208 sha1_to_hex(item->object.sha1));
210 ret = parse_tree_buffer(item, buffer, size);