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, 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))
66 static int read_tree_recursive(void *buffer, unsigned long size,
67 const char *base, int baselen,
68 int stage, char **match)
71 int len = strlen(buffer)+1;
72 unsigned char *sha1 = buffer + len;
73 char *path = strchr(buffer, ' ')+1;
76 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
82 if (!match_tree_entry(base, baselen, path, mode, match))
87 int pathlen = strlen(path);
91 unsigned long eltsize;
93 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
94 if (!eltbuf || strcmp(elttype, "tree")) {
95 if (eltbuf) free(eltbuf);
98 newbase = xmalloc(baselen + 1 + pathlen);
99 memcpy(newbase, base, baselen);
100 memcpy(newbase + baselen, path, pathlen);
101 newbase[baselen + pathlen] = '/';
102 retval = read_tree_recursive(eltbuf, eltsize,
104 baselen + pathlen + 1,
112 if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
118 int read_tree(void *buffer, unsigned long size, int stage, char **match)
120 return read_tree_recursive(buffer, size, "", 0, stage, match);
123 struct tree *lookup_tree(const unsigned char *sha1)
125 struct object *obj = lookup_object(sha1);
127 struct tree *ret = xmalloc(sizeof(struct tree));
128 memset(ret, 0, sizeof(struct tree));
129 created_object(sha1, &ret->object);
130 ret->object.type = tree_type;
134 obj->type = tree_type;
135 if (obj->type != tree_type) {
136 error("Object %s is a %s, not a tree",
137 sha1_to_hex(sha1), obj->type);
140 return (struct tree *) obj;
143 int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
145 void *bufptr = buffer;
146 struct tree_entry_list **list_p;
148 if (item->object.parsed)
150 item->object.parsed = 1;
151 list_p = &item->entries;
154 struct tree_entry_list *entry;
155 int len = 1+strlen(bufptr);
156 unsigned char *file_sha1 = bufptr + len;
157 char *path = strchr(bufptr, ' ');
159 if (size < len + 20 || !path ||
160 sscanf(bufptr, "%o", &mode) != 1)
163 entry = xmalloc(sizeof(struct tree_entry_list));
164 entry->name = strdup(path + 1);
165 entry->directory = S_ISDIR(mode) != 0;
166 entry->executable = (mode & S_IXUSR) != 0;
167 entry->symlink = S_ISLNK(mode) != 0;
174 if (entry->directory) {
175 entry->item.tree = lookup_tree(file_sha1);
176 obj = &entry->item.tree->object;
178 entry->item.blob = lookup_blob(file_sha1);
179 obj = &entry->item.blob->object;
182 add_ref(&item->object, obj);
183 entry->parent = NULL; /* needs to be filled by the user */
185 list_p = &entry->next;
190 int parse_tree(struct tree *item)
197 if (item->object.parsed)
199 buffer = read_sha1_file(item->object.sha1, type, &size);
201 return error("Could not read %s",
202 sha1_to_hex(item->object.sha1));
203 if (strcmp(type, tree_type)) {
205 return error("Object %s not a tree",
206 sha1_to_hex(item->object.sha1));
208 ret = parse_tree_buffer(item, buffer, size);