]> git.ipfire.org Git - thirdparty/git.git/blame - tree.c
Use symbolic name SHORT_NAME_AMBIGUOUS as error return value
[thirdparty/git.git] / tree.c
CommitLineData
8f1d2e6f 1#include "cache.h"
175785e5
DB
2#include "tree.h"
3#include "blob.h"
77675e2a
DB
4#include "commit.h"
5#include "tag.h"
175785e5
DB
6#include <stdlib.h>
7
8const char *tree_type = "tree";
9
94537c78
LT
10static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
11{
3c5e8468
LT
12 int len;
13 unsigned int size;
14 struct cache_entry *ce;
15
16 if (S_ISDIR(mode))
17 return READ_TREE_RECURSIVE;
18
19 len = strlen(pathname);
20 size = cache_entry_size(baselen + len);
21 ce = xmalloc(size);
94537c78
LT
22
23 memset(ce, 0, size);
24
25 ce->ce_mode = create_ce_mode(mode);
26 ce->ce_flags = create_ce_flags(baselen + len, stage);
27 memcpy(ce->name, base, baselen);
28 memcpy(ce->name + baselen, pathname, len+1);
29 memcpy(ce->sha1, sha1, 20);
b155725d 30 return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
94537c78
LT
31}
32
3e587635 33static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
0ca14a57 34{
3e587635 35 const char *match;
0ca14a57
LT
36 int pathlen;
37
38 if (!paths)
39 return 1;
40 pathlen = strlen(path);
41 while ((match = *paths++) != NULL) {
42 int matchlen = strlen(match);
43
44 if (baselen >= matchlen) {
45 /* If it doesn't match, move along... */
46 if (strncmp(base, match, matchlen))
47 continue;
48 /* The base is a subdirectory of a path which was specified. */
49 return 1;
50 }
51
52 /* Does the base match? */
53 if (strncmp(base, match, baselen))
54 continue;
55
56 match += baselen;
57 matchlen -= baselen;
58
59 if (pathlen > matchlen)
60 continue;
61
62 if (matchlen > pathlen) {
63 if (match[pathlen] != '/')
64 continue;
65 if (!S_ISDIR(mode))
66 continue;
67 }
68
69 if (strncmp(path, match, pathlen))
70 continue;
3e587635
LT
71
72 return 1;
0ca14a57
LT
73 }
74 return 0;
75}
76
521698b1 77int read_tree_recursive(struct tree *tree,
3c5e8468
LT
78 const char *base, int baselen,
79 int stage, const char **match,
80 read_tree_fn_t fn)
94537c78 81{
521698b1
DB
82 struct tree_entry_list *list;
83 if (parse_tree(tree))
84 return -1;
85 list = tree->entries;
86 while (list) {
87 struct tree_entry_list *current = list;
88 list = list->next;
89 if (!match_tree_entry(base, baselen, current->name,
90 current->mode, match))
0ca14a57
LT
91 continue;
92
521698b1
DB
93 switch (fn(current->item.any->sha1, base, baselen,
94 current->name, current->mode, stage)) {
3c5e8468
LT
95 case 0:
96 continue;
97 case READ_TREE_RECURSIVE:
98 break;;
99 default:
100 return -1;
101 }
521698b1 102 if (current->directory) {
94537c78 103 int retval;
521698b1 104 int pathlen = strlen(current->name);
1c9da46d 105 char *newbase;
94537c78 106
1c9da46d 107 newbase = xmalloc(baselen + 1 + pathlen);
94537c78 108 memcpy(newbase, base, baselen);
521698b1 109 memcpy(newbase + baselen, current->name, pathlen);
94537c78 110 newbase[baselen + pathlen] = '/';
521698b1 111 retval = read_tree_recursive(current->item.tree,
94537c78 112 newbase,
0ca14a57 113 baselen + pathlen + 1,
3c5e8468 114 stage, match, fn);
94537c78
LT
115 free(newbase);
116 if (retval)
117 return -1;
118 continue;
119 }
94537c78
LT
120 }
121 return 0;
122}
123
521698b1 124int read_tree(struct tree *tree, int stage, const char **match)
94537c78 125{
521698b1 126 return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
94537c78
LT
127}
128
5d6ccf5c 129struct tree *lookup_tree(const unsigned char *sha1)
175785e5
DB
130{
131 struct object *obj = lookup_object(sha1);
132 if (!obj) {
812666c8 133 struct tree *ret = xmalloc(sizeof(struct tree));
175785e5
DB
134 memset(ret, 0, sizeof(struct tree));
135 created_object(sha1, &ret->object);
d32987be 136 ret->object.type = tree_type;
175785e5
DB
137 return ret;
138 }
d1af002d
NP
139 if (!obj->type)
140 obj->type = tree_type;
c35dfe85 141 if (obj->type != tree_type) {
175785e5
DB
142 error("Object %s is a %s, not a tree",
143 sha1_to_hex(sha1), obj->type);
144 return NULL;
145 }
146 return (struct tree *) obj;
147}
148
bd2c39f5 149int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
175785e5 150{
bd2c39f5 151 void *bufptr = buffer;
08692164 152 struct tree_entry_list **list_p;
4a4e6fd7 153 int n_refs = 0;
bd2c39f5 154
175785e5
DB
155 if (item->object.parsed)
156 return 0;
157 item->object.parsed = 1;
08692164 158 list_p = &item->entries;
175785e5
DB
159 while (size) {
160 struct object *obj;
08692164 161 struct tree_entry_list *entry;
175785e5
DB
162 int len = 1+strlen(bufptr);
163 unsigned char *file_sha1 = bufptr + len;
164 char *path = strchr(bufptr, ' ');
165 unsigned int mode;
166 if (size < len + 20 || !path ||
bd2c39f5 167 sscanf(bufptr, "%o", &mode) != 1)
175785e5
DB
168 return -1;
169
812666c8 170 entry = xmalloc(sizeof(struct tree_entry_list));
08692164 171 entry->name = strdup(path + 1);
42ea9cb2
LT
172 entry->directory = S_ISDIR(mode) != 0;
173 entry->executable = (mode & S_IXUSR) != 0;
174 entry->symlink = S_ISLNK(mode) != 0;
64071805 175 entry->zeropad = *(char *)bufptr == '0';
42ea9cb2 176 entry->mode = mode;
08692164
DB
177 entry->next = NULL;
178
175785e5
DB
179 bufptr += len + 20;
180 size -= len + 20;
181
08692164
DB
182 if (entry->directory) {
183 entry->item.tree = lookup_tree(file_sha1);
184 obj = &entry->item.tree->object;
175785e5 185 } else {
08692164
DB
186 entry->item.blob = lookup_blob(file_sha1);
187 obj = &entry->item.blob->object;
175785e5 188 }
235ac407 189 if (obj)
4a4e6fd7 190 n_refs++;
08692164
DB
191 *list_p = entry;
192 list_p = &entry->next;
175785e5 193 }
4a4e6fd7
SV
194
195 if (track_object_refs) {
196 struct tree_entry_list *entry;
197 unsigned i = 0;
198 struct object_refs *refs = alloc_object_refs(n_refs);
199 for (entry = item->entries; entry; entry = entry->next)
200 refs->ref[i++] = entry->item.any;
201 set_object_refs(&item->object, refs);
202 }
203
175785e5
DB
204 return 0;
205}
bd2c39f5
NP
206
207int parse_tree(struct tree *item)
208{
209 char type[20];
210 void *buffer;
211 unsigned long size;
212 int ret;
213
214 if (item->object.parsed)
215 return 0;
216 buffer = read_sha1_file(item->object.sha1, type, &size);
217 if (!buffer)
218 return error("Could not read %s",
219 sha1_to_hex(item->object.sha1));
220 if (strcmp(type, tree_type)) {
221 free(buffer);
222 return error("Object %s not a tree",
223 sha1_to_hex(item->object.sha1));
224 }
225 ret = parse_tree_buffer(item, buffer, size);
226 free(buffer);
227 return ret;
228}
77675e2a
DB
229
230struct tree *parse_tree_indirect(const unsigned char *sha1)
231{
232 struct object *obj = parse_object(sha1);
233 do {
234 if (!obj)
235 return NULL;
236 if (obj->type == tree_type)
237 return (struct tree *) obj;
238 else if (obj->type == commit_type)
239 obj = &(((struct commit *) obj)->tree->object);
240 else if (obj->type == tag_type)
241 obj = ((struct tag *) obj)->tagged;
242 else
243 return NULL;
244 if (!obj->parsed)
245 parse_object(obj->sha1);
246 } while (1);
247}