Move 'unpack_trees()' over to 'traverse_trees()' interface
[git/git.git] / tree-walk.c
CommitLineData
1b0c7174
JH
1#include "cache.h"
2#include "tree-walk.h"
8e440259 3#include "tree.h"
1b0c7174 4
4651ece8
LT
5static const char *get_mode(const char *str, unsigned int *modep)
6{
7 unsigned char c;
8 unsigned int mode = 0;
9
64cc1c09
MK
10 if (*str == ' ')
11 return NULL;
12
4651ece8
LT
13 while ((c = *str++) != ' ') {
14 if (c < '0' || c > '7')
15 return NULL;
16 mode = (mode << 3) + (c - '0');
17 }
18 *modep = mode;
19 return str;
20}
21
64cc1c09 22static void decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size)
4651ece8
LT
23{
24 const char *path;
25 unsigned int mode, len;
26
64cc1c09
MK
27 if (size < 24 || buf[size - 21])
28 die("corrupt tree file");
29
4651ece8 30 path = get_mode(buf, &mode);
64cc1c09 31 if (!path || !*path)
4651ece8
LT
32 die("corrupt tree file");
33 len = strlen(path) + 1;
34
35 /* Initialize the descriptor entry */
36 desc->entry.path = path;
37 desc->entry.mode = mode;
38 desc->entry.sha1 = (const unsigned char *)(path + len);
39}
40
6fda5e51
LT
41void init_tree_desc(struct tree_desc *desc, const void *buffer, unsigned long size)
42{
43 desc->buffer = buffer;
44 desc->size = size;
4651ece8
LT
45 if (size)
46 decode_tree_entry(desc, buffer, size);
6fda5e51
LT
47}
48
1b0c7174
JH
49void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
50{
51 unsigned long size = 0;
52 void *buf = NULL;
53
54 if (sha1) {
8e440259 55 buf = read_object_with_reference(sha1, tree_type, &size, NULL);
1b0c7174
JH
56 if (!buf)
57 die("unable to read tree %s", sha1_to_hex(sha1));
58 }
6fda5e51 59 init_tree_desc(desc, buf, size);
1b0c7174
JH
60 return buf;
61}
62
63static int entry_compare(struct name_entry *a, struct name_entry *b)
64{
91e4f036 65 return df_name_compare(
a8c40471
LT
66 a->path, tree_entry_len(a->path, a->sha1), a->mode,
67 b->path, tree_entry_len(b->path, b->sha1), b->mode);
1b0c7174
JH
68}
69
70static void entry_clear(struct name_entry *a)
71{
72 memset(a, 0, sizeof(*a));
73}
74
75static void entry_extract(struct tree_desc *t, struct name_entry *a)
76{
4651ece8 77 *a = t->entry;
1b0c7174
JH
78}
79
80void update_tree_entry(struct tree_desc *desc)
81{
6fda5e51 82 const void *buf = desc->buffer;
4651ece8 83 const unsigned char *end = desc->entry.sha1 + 20;
1b0c7174 84 unsigned long size = desc->size;
4651ece8 85 unsigned long len = end - (const unsigned char *)buf;
1b0c7174
JH
86
87 if (size < len)
88 die("corrupt tree file");
4651ece8
LT
89 buf = end;
90 size -= len;
91 desc->buffer = buf;
92 desc->size = size;
93 if (size)
94 decode_tree_entry(desc, buf, size);
1b0c7174
JH
95}
96
4c068a98
LT
97int tree_entry(struct tree_desc *desc, struct name_entry *entry)
98{
4651ece8 99 if (!desc->size)
4c068a98
LT
100 return 0;
101
4651ece8
LT
102 *entry = desc->entry;
103 update_tree_entry(desc);
4c068a98
LT
104 return 1;
105}
106
40d934df
LT
107void setup_traverse_info(struct traverse_info *info, const char *base)
108{
109 int pathlen = strlen(base);
110
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);
117}
118
119char *make_traverse_path(char *path, const struct traverse_info *info, const struct name_entry *n)
120{
121 int len = tree_entry_len(n->path, n->sha1);
122 int pathlen = info->pathlen;
123
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;
134 }
135 return path;
136}
137
5803c6f8 138int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
1b0c7174 139{
5803c6f8 140 int ret = 0;
1b0c7174
JH
141 struct name_entry *entry = xmalloc(n*sizeof(*entry));
142
143 for (;;) {
1b0c7174 144 unsigned long mask = 0;
91e4f036 145 unsigned long dirmask = 0;
1b0c7174
JH
146 int i, last;
147
148 last = -1;
149 for (i = 0; i < n; i++) {
150 if (!t[i].size)
151 continue;
152 entry_extract(t+i, entry+i);
153 if (last >= 0) {
154 int cmp = entry_compare(entry+i, entry+last);
155
156 /*
157 * Is the new name bigger than the old one?
158 * Ignore it
159 */
160 if (cmp > 0)
161 continue;
162 /*
163 * Is the new name smaller than the old one?
164 * Ignore all old ones
165 */
166 if (cmp < 0)
167 mask = 0;
168 }
169 mask |= 1ul << i;
91e4f036
LT
170 if (S_ISDIR(entry[i].mode))
171 dirmask |= 1ul << i;
1b0c7174
JH
172 last = i;
173 }
174 if (!mask)
175 break;
91e4f036 176 dirmask &= mask;
1b0c7174
JH
177
178 /*
5803c6f8 179 * Clear all the unused name-entries.
1b0c7174
JH
180 */
181 for (i = 0; i < n; i++) {
5803c6f8 182 if (mask & (1ul << i))
1b0c7174 183 continue;
1b0c7174
JH
184 entry_clear(entry + i);
185 }
91e4f036 186 ret = info->fn(n, mask, dirmask, entry, info);
5803c6f8
LT
187 if (ret < 0)
188 break;
189 if (ret)
190 mask &= ret;
191 ret = 0;
192 for (i = 0; i < n; i++) {
193 if (mask & (1ul << i))
194 update_tree_entry(t + i);
195 }
1b0c7174
JH
196 }
197 free(entry);
5803c6f8 198 return ret;
1b0c7174
JH
199}
200
4dcff634
JH
201static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode)
202{
203 int namelen = strlen(name);
204 while (t->size) {
205 const char *entry;
206 const unsigned char *sha1;
207 int entrylen, cmp;
208
209 sha1 = tree_entry_extract(t, &entry, mode);
210 update_tree_entry(t);
304de2d2 211 entrylen = tree_entry_len(entry, sha1);
4dcff634
JH
212 if (entrylen > namelen)
213 continue;
214 cmp = memcmp(name, entry, entrylen);
215 if (cmp > 0)
216 continue;
217 if (cmp < 0)
218 break;
219 if (entrylen == namelen) {
e702496e 220 hashcpy(result, sha1);
4dcff634
JH
221 return 0;
222 }
223 if (name[entrylen] != '/')
224 continue;
225 if (!S_ISDIR(*mode))
226 break;
227 if (++entrylen == namelen) {
e702496e 228 hashcpy(result, sha1);
4dcff634
JH
229 return 0;
230 }
231 return get_tree_entry(sha1, name + entrylen, result, mode);
232 }
233 return -1;
234}
235
236int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned char *sha1, unsigned *mode)
237{
238 int retval;
239 void *tree;
6fda5e51 240 unsigned long size;
4dcff634 241 struct tree_desc t;
d93b7d1c 242 unsigned char root[20];
4dcff634 243
6fda5e51 244 tree = read_object_with_reference(tree_sha1, tree_type, &size, root);
4dcff634
JH
245 if (!tree)
246 return -1;
d93b7d1c
JK
247
248 if (name[0] == '\0') {
249 hashcpy(sha1, root);
250 return 0;
251 }
252
6fda5e51 253 init_tree_desc(&t, tree, size);
4dcff634
JH
254 retval = find_tree_entry(&t, name, sha1, mode);
255 free(tree);
256 return retval;
257}