]> git.ipfire.org Git - thirdparty/git.git/blame - tree.c
prune: do not show error from pack-redundant when no packs are found.
[thirdparty/git.git] / tree.c
CommitLineData
175785e5
DB
1#include "tree.h"
2#include "blob.h"
77675e2a
DB
3#include "commit.h"
4#include "tag.h"
175785e5
DB
5#include "cache.h"
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
3c5e8468
LT
77int read_tree_recursive(void *buffer, unsigned long size,
78 const char *base, int baselen,
79 int stage, const char **match,
80 read_tree_fn_t fn)
94537c78
LT
81{
82 while (size) {
83 int len = strlen(buffer)+1;
84 unsigned char *sha1 = buffer + len;
85 char *path = strchr(buffer, ' ')+1;
86 unsigned int mode;
87
88 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
89 return -1;
90
91 buffer = sha1 + 20;
92 size -= len + 20;
93
0ca14a57
LT
94 if (!match_tree_entry(base, baselen, path, mode, match))
95 continue;
96
3c5e8468
LT
97 switch (fn(sha1, base, baselen, path, mode, stage)) {
98 case 0:
99 continue;
100 case READ_TREE_RECURSIVE:
101 break;;
102 default:
103 return -1;
104 }
94537c78
LT
105 if (S_ISDIR(mode)) {
106 int retval;
107 int pathlen = strlen(path);
1c9da46d 108 char *newbase;
94537c78
LT
109 void *eltbuf;
110 char elttype[20];
111 unsigned long eltsize;
112
113 eltbuf = read_sha1_file(sha1, elttype, &eltsize);
1c9da46d
JF
114 if (!eltbuf || strcmp(elttype, "tree")) {
115 if (eltbuf) free(eltbuf);
94537c78 116 return -1;
1c9da46d
JF
117 }
118 newbase = xmalloc(baselen + 1 + pathlen);
94537c78
LT
119 memcpy(newbase, base, baselen);
120 memcpy(newbase + baselen, path, pathlen);
121 newbase[baselen + pathlen] = '/';
122 retval = read_tree_recursive(eltbuf, eltsize,
123 newbase,
0ca14a57 124 baselen + pathlen + 1,
3c5e8468 125 stage, match, fn);
94537c78
LT
126 free(eltbuf);
127 free(newbase);
128 if (retval)
129 return -1;
130 continue;
131 }
94537c78
LT
132 }
133 return 0;
134}
135
3e587635 136int read_tree(void *buffer, unsigned long size, int stage, const char **match)
94537c78 137{
3c5e8468 138 return read_tree_recursive(buffer, size, "", 0, stage, match, read_one_entry);
94537c78
LT
139}
140
5d6ccf5c 141struct tree *lookup_tree(const unsigned char *sha1)
175785e5
DB
142{
143 struct object *obj = lookup_object(sha1);
144 if (!obj) {
812666c8 145 struct tree *ret = xmalloc(sizeof(struct tree));
175785e5
DB
146 memset(ret, 0, sizeof(struct tree));
147 created_object(sha1, &ret->object);
d32987be 148 ret->object.type = tree_type;
175785e5
DB
149 return ret;
150 }
d1af002d
NP
151 if (!obj->type)
152 obj->type = tree_type;
c35dfe85 153 if (obj->type != tree_type) {
175785e5
DB
154 error("Object %s is a %s, not a tree",
155 sha1_to_hex(sha1), obj->type);
156 return NULL;
157 }
158 return (struct tree *) obj;
159}
160
bd2c39f5 161int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
175785e5 162{
bd2c39f5 163 void *bufptr = buffer;
08692164 164 struct tree_entry_list **list_p;
4a4e6fd7 165 int n_refs = 0;
bd2c39f5 166
175785e5
DB
167 if (item->object.parsed)
168 return 0;
169 item->object.parsed = 1;
08692164 170 list_p = &item->entries;
175785e5
DB
171 while (size) {
172 struct object *obj;
08692164 173 struct tree_entry_list *entry;
175785e5
DB
174 int len = 1+strlen(bufptr);
175 unsigned char *file_sha1 = bufptr + len;
176 char *path = strchr(bufptr, ' ');
177 unsigned int mode;
178 if (size < len + 20 || !path ||
bd2c39f5 179 sscanf(bufptr, "%o", &mode) != 1)
175785e5
DB
180 return -1;
181
812666c8 182 entry = xmalloc(sizeof(struct tree_entry_list));
08692164 183 entry->name = strdup(path + 1);
42ea9cb2
LT
184 entry->directory = S_ISDIR(mode) != 0;
185 entry->executable = (mode & S_IXUSR) != 0;
186 entry->symlink = S_ISLNK(mode) != 0;
64071805 187 entry->zeropad = *(char *)bufptr == '0';
42ea9cb2 188 entry->mode = mode;
08692164
DB
189 entry->next = NULL;
190
175785e5
DB
191 bufptr += len + 20;
192 size -= len + 20;
193
08692164
DB
194 if (entry->directory) {
195 entry->item.tree = lookup_tree(file_sha1);
196 obj = &entry->item.tree->object;
175785e5 197 } else {
08692164
DB
198 entry->item.blob = lookup_blob(file_sha1);
199 obj = &entry->item.blob->object;
175785e5 200 }
235ac407 201 if (obj)
4a4e6fd7 202 n_refs++;
08692164
DB
203 *list_p = entry;
204 list_p = &entry->next;
175785e5 205 }
4a4e6fd7
SV
206
207 if (track_object_refs) {
208 struct tree_entry_list *entry;
209 unsigned i = 0;
210 struct object_refs *refs = alloc_object_refs(n_refs);
211 for (entry = item->entries; entry; entry = entry->next)
212 refs->ref[i++] = entry->item.any;
213 set_object_refs(&item->object, refs);
214 }
215
175785e5
DB
216 return 0;
217}
bd2c39f5
NP
218
219int parse_tree(struct tree *item)
220{
221 char type[20];
222 void *buffer;
223 unsigned long size;
224 int ret;
225
226 if (item->object.parsed)
227 return 0;
228 buffer = read_sha1_file(item->object.sha1, type, &size);
229 if (!buffer)
230 return error("Could not read %s",
231 sha1_to_hex(item->object.sha1));
232 if (strcmp(type, tree_type)) {
233 free(buffer);
234 return error("Object %s not a tree",
235 sha1_to_hex(item->object.sha1));
236 }
237 ret = parse_tree_buffer(item, buffer, size);
238 free(buffer);
239 return ret;
240}
77675e2a
DB
241
242struct tree *parse_tree_indirect(const unsigned char *sha1)
243{
244 struct object *obj = parse_object(sha1);
245 do {
246 if (!obj)
247 return NULL;
248 if (obj->type == tree_type)
249 return (struct tree *) obj;
250 else if (obj->type == commit_type)
251 obj = &(((struct commit *) obj)->tree->object);
252 else if (obj->type == tag_type)
253 obj = ((struct tag *) obj)->tagged;
254 else
255 return NULL;
256 if (!obj->parsed)
257 parse_object(obj->sha1);
258 } while (1);
259}