Start adding interfaces to read in partial trees
[git/jrn.git] / tree.c
blob8dc27e3b7354b8b8cc7d7491621929c78325a645
1 #include "tree.h"
2 #include "blob.h"
3 #include "cache.h"
4 #include <stdlib.h>
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);
14 memset(ce, 0, 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)
26 char *match;
27 int pathlen;
29 if (!paths)
30 return 1;
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))
38 continue;
39 /* The base is a subdirectory of a path which was specified. */
40 return 1;
43 /* Does the base match? */
44 if (strncmp(base, match, baselen))
45 continue;
47 match += baselen;
48 matchlen -= baselen;
50 if (pathlen > matchlen)
51 continue;
53 if (matchlen > pathlen) {
54 if (match[pathlen] != '/')
55 continue;
56 if (!S_ISDIR(mode))
57 continue;
60 if (strncmp(path, match, pathlen))
61 continue;
63 return 0;
66 static int read_tree_recursive(void *buffer, unsigned long size,
67 const char *base, int baselen,
68 int stage, char **match)
70 while (size) {
71 int len = strlen(buffer)+1;
72 unsigned char *sha1 = buffer + len;
73 char *path = strchr(buffer, ' ')+1;
74 unsigned int mode;
76 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
77 return -1;
79 buffer = sha1 + 20;
80 size -= len + 20;
82 if (!match_tree_entry(base, baselen, path, mode, match))
83 continue;
85 if (S_ISDIR(mode)) {
86 int retval;
87 int pathlen = strlen(path);
88 char *newbase;
89 void *eltbuf;
90 char elttype[20];
91 unsigned long eltsize;
93 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
94 if (!eltbuf || strcmp(elttype, "tree")) {
95 if (eltbuf) free(eltbuf);
96 return -1;
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,
103 newbase,
104 baselen + pathlen + 1,
105 stage, match);
106 free(eltbuf);
107 free(newbase);
108 if (retval)
109 return -1;
110 continue;
112 if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0)
113 return -1;
115 return 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);
126 if (!obj) {
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;
131 return ret;
133 if (!obj->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);
138 return NULL;
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)
149 return 0;
150 item->object.parsed = 1;
151 list_p = &item->entries;
152 while (size) {
153 struct object *obj;
154 struct tree_entry_list *entry;
155 int len = 1+strlen(bufptr);
156 unsigned char *file_sha1 = bufptr + len;
157 char *path = strchr(bufptr, ' ');
158 unsigned int mode;
159 if (size < len + 20 || !path ||
160 sscanf(bufptr, "%o", &mode) != 1)
161 return -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;
168 entry->mode = mode;
169 entry->next = NULL;
171 bufptr += len + 20;
172 size -= len + 20;
174 if (entry->directory) {
175 entry->item.tree = lookup_tree(file_sha1);
176 obj = &entry->item.tree->object;
177 } else {
178 entry->item.blob = lookup_blob(file_sha1);
179 obj = &entry->item.blob->object;
181 if (obj)
182 add_ref(&item->object, obj);
183 entry->parent = NULL; /* needs to be filled by the user */
184 *list_p = entry;
185 list_p = &entry->next;
187 return 0;
190 int parse_tree(struct tree *item)
192 char type[20];
193 void *buffer;
194 unsigned long size;
195 int ret;
197 if (item->object.parsed)
198 return 0;
199 buffer = read_sha1_file(item->object.sha1, type, &size);
200 if (!buffer)
201 return error("Could not read %s",
202 sha1_to_hex(item->object.sha1));
203 if (strcmp(type, tree_type)) {
204 free(buffer);
205 return error("Object %s not a tree",
206 sha1_to_hex(item->object.sha1));
208 ret = parse_tree_buffer(item, buffer, size);
209 free(buffer);
210 return ret;