Add return value to 'traverse_tree()' callback
[git/git-p4.git] / tree-walk.c
blob7170e375b3473aaced9b639d915c93ff931be859
1 #include "cache.h"
2 #include "tree-walk.h"
3 #include "tree.h"
5 static const char *get_mode(const char *str, unsigned int *modep)
7 unsigned char c;
8 unsigned int mode = 0;
10 if (*str == ' ')
11 return NULL;
13 while ((c = *str++) != ' ') {
14 if (c < '0' || c > '7')
15 return NULL;
16 mode = (mode << 3) + (c - '0');
18 *modep = mode;
19 return str;
22 static void decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size)
24 const char *path;
25 unsigned int mode, len;
27 if (size < 24 || buf[size - 21])
28 die("corrupt tree file");
30 path = get_mode(buf, &mode);
31 if (!path || !*path)
32 die("corrupt tree file");
33 len = strlen(path) + 1;
35 /* Initialize the descriptor entry */
36 desc->entry.path = path;
37 desc->entry.mode = mode;
38 desc->entry.sha1 = (const unsigned char *)(path + len);
41 void init_tree_desc(struct tree_desc *desc, const void *buffer, unsigned long size)
43 desc->buffer = buffer;
44 desc->size = size;
45 if (size)
46 decode_tree_entry(desc, buffer, size);
49 void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
51 unsigned long size = 0;
52 void *buf = NULL;
54 if (sha1) {
55 buf = read_object_with_reference(sha1, tree_type, &size, NULL);
56 if (!buf)
57 die("unable to read tree %s", sha1_to_hex(sha1));
59 init_tree_desc(desc, buf, size);
60 return buf;
63 static int entry_compare(struct name_entry *a, struct name_entry *b)
65 return base_name_compare(
66 a->path, tree_entry_len(a->path, a->sha1), a->mode,
67 b->path, tree_entry_len(b->path, b->sha1), b->mode);
70 static void entry_clear(struct name_entry *a)
72 memset(a, 0, sizeof(*a));
75 static void entry_extract(struct tree_desc *t, struct name_entry *a)
77 *a = t->entry;
80 void update_tree_entry(struct tree_desc *desc)
82 const void *buf = desc->buffer;
83 const unsigned char *end = desc->entry.sha1 + 20;
84 unsigned long size = desc->size;
85 unsigned long len = end - (const unsigned char *)buf;
87 if (size < len)
88 die("corrupt tree file");
89 buf = end;
90 size -= len;
91 desc->buffer = buf;
92 desc->size = size;
93 if (size)
94 decode_tree_entry(desc, buf, size);
97 int tree_entry(struct tree_desc *desc, struct name_entry *entry)
99 if (!desc->size)
100 return 0;
102 *entry = desc->entry;
103 update_tree_entry(desc);
104 return 1;
107 void setup_traverse_info(struct traverse_info *info, const char *base)
109 int pathlen = strlen(base);
111 memset(info, 0, sizeof(*info));
112 if (pathlen && base[pathlen-1] == '/')
113 pathlen--;
114 info->pathlen = pathlen ? pathlen + 1 : 0;
115 info->name.path = base;
116 info->name.sha1 = (void *)(base + pathlen + 1);
119 char *make_traverse_path(char *path, const struct traverse_info *info, const struct name_entry *n)
121 int len = tree_entry_len(n->path, n->sha1);
122 int pathlen = info->pathlen;
124 path[pathlen + len] = 0;
125 for (;;) {
126 memcpy(path + pathlen, n->path, len);
127 if (!pathlen)
128 break;
129 path[--pathlen] = '/';
130 n = &info->name;
131 len = tree_entry_len(n->path, n->sha1);
132 info = info->prev;
133 pathlen -= len;
135 return path;
138 int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
140 int ret = 0;
141 struct name_entry *entry = xmalloc(n*sizeof(*entry));
143 for (;;) {
144 unsigned long mask = 0;
145 int i, last;
147 last = -1;
148 for (i = 0; i < n; i++) {
149 if (!t[i].size)
150 continue;
151 entry_extract(t+i, entry+i);
152 if (last >= 0) {
153 int cmp = entry_compare(entry+i, entry+last);
156 * Is the new name bigger than the old one?
157 * Ignore it
159 if (cmp > 0)
160 continue;
162 * Is the new name smaller than the old one?
163 * Ignore all old ones
165 if (cmp < 0)
166 mask = 0;
168 mask |= 1ul << i;
169 last = i;
171 if (!mask)
172 break;
175 * Clear all the unused name-entries.
177 for (i = 0; i < n; i++) {
178 if (mask & (1ul << i))
179 continue;
180 entry_clear(entry + i);
182 ret = info->fn(n, mask, entry, info);
183 if (ret < 0)
184 break;
185 if (ret)
186 mask &= ret;
187 ret = 0;
188 for (i = 0; i < n; i++) {
189 if (mask & (1ul << i))
190 update_tree_entry(t + i);
193 free(entry);
194 return ret;
197 static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode)
199 int namelen = strlen(name);
200 while (t->size) {
201 const char *entry;
202 const unsigned char *sha1;
203 int entrylen, cmp;
205 sha1 = tree_entry_extract(t, &entry, mode);
206 update_tree_entry(t);
207 entrylen = tree_entry_len(entry, sha1);
208 if (entrylen > namelen)
209 continue;
210 cmp = memcmp(name, entry, entrylen);
211 if (cmp > 0)
212 continue;
213 if (cmp < 0)
214 break;
215 if (entrylen == namelen) {
216 hashcpy(result, sha1);
217 return 0;
219 if (name[entrylen] != '/')
220 continue;
221 if (!S_ISDIR(*mode))
222 break;
223 if (++entrylen == namelen) {
224 hashcpy(result, sha1);
225 return 0;
227 return get_tree_entry(sha1, name + entrylen, result, mode);
229 return -1;
232 int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned char *sha1, unsigned *mode)
234 int retval;
235 void *tree;
236 unsigned long size;
237 struct tree_desc t;
238 unsigned char root[20];
240 tree = read_object_with_reference(tree_sha1, tree_type, &size, root);
241 if (!tree)
242 return -1;
244 if (name[0] == '\0') {
245 hashcpy(sha1, root);
246 return 0;
249 init_tree_desc(&t, tree, size);
250 retval = find_tree_entry(&t, name, sha1, mode);
251 free(tree);
252 return retval;