]> git.ipfire.org Git - thirdparty/git.git/blame - tree.c
Merge branch 'maint-1.6.0' into maint-1.6.1
[thirdparty/git.git] / tree.c
CommitLineData
8f1d2e6f 1#include "cache.h"
af3785dc 2#include "cache-tree.h"
175785e5
DB
3#include "tree.h"
4#include "blob.h"
77675e2a
DB
5#include "commit.h"
6#include "tag.h"
136f2e54 7#include "tree-walk.h"
175785e5
DB
8
9const char *tree_type = "tree";
10
af3785dc 11static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt)
94537c78 12{
3c5e8468
LT
13 int len;
14 unsigned int size;
15 struct cache_entry *ce;
16
17 if (S_ISDIR(mode))
18 return READ_TREE_RECURSIVE;
19
20 len = strlen(pathname);
21 size = cache_entry_size(baselen + len);
90321c10 22 ce = xcalloc(1, size);
94537c78
LT
23
24 ce->ce_mode = create_ce_mode(mode);
25 ce->ce_flags = create_ce_flags(baselen + len, stage);
26 memcpy(ce->name, base, baselen);
27 memcpy(ce->name + baselen, pathname, len+1);
e702496e 28 hashcpy(ce->sha1, sha1);
af3785dc
JH
29 return add_cache_entry(ce, opt);
30}
31
671f0707 32static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
af3785dc
JH
33{
34 return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
35 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
36}
37
38/*
39 * This is used when the caller knows there is no existing entries at
40 * the stage that will conflict with the entry being added.
41 */
671f0707 42static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
af3785dc
JH
43{
44 return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
45 ADD_CACHE_JUST_APPEND);
94537c78
LT
46}
47
3e587635 48static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
0ca14a57 49{
3e587635 50 const char *match;
0ca14a57
LT
51 int pathlen;
52
53 if (!paths)
54 return 1;
55 pathlen = strlen(path);
56 while ((match = *paths++) != NULL) {
57 int matchlen = strlen(match);
58
59 if (baselen >= matchlen) {
60 /* If it doesn't match, move along... */
61 if (strncmp(base, match, matchlen))
62 continue;
8092bfb6
JH
63 /* pathspecs match only at the directory boundaries */
64 if (!matchlen ||
65 base[matchlen] == '/' ||
66 match[matchlen - 1] == '/')
67 return 1;
68 continue;
0ca14a57
LT
69 }
70
71 /* Does the base match? */
72 if (strncmp(base, match, baselen))
73 continue;
74
75 match += baselen;
76 matchlen -= baselen;
77
78 if (pathlen > matchlen)
79 continue;
80
81 if (matchlen > pathlen) {
82 if (match[pathlen] != '/')
83 continue;
84 if (!S_ISDIR(mode))
85 continue;
86 }
87
88 if (strncmp(path, match, pathlen))
89 continue;
3e587635
LT
90
91 return 1;
0ca14a57
LT
92 }
93 return 0;
94}
95
521698b1 96int read_tree_recursive(struct tree *tree,
3c5e8468
LT
97 const char *base, int baselen,
98 int stage, const char **match,
671f0707 99 read_tree_fn_t fn, void *context)
94537c78 100{
0790a42a 101 struct tree_desc desc;
4c068a98 102 struct name_entry entry;
0790a42a 103
521698b1
DB
104 if (parse_tree(tree))
105 return -1;
0790a42a 106
6fda5e51 107 init_tree_desc(&desc, tree->buffer, tree->size);
0790a42a 108
4c068a98
LT
109 while (tree_entry(&desc, &entry)) {
110 if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
0ca14a57
LT
111 continue;
112
671f0707 113 switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage, context)) {
3c5e8468
LT
114 case 0:
115 continue;
116 case READ_TREE_RECURSIVE:
117 break;;
118 default:
119 return -1;
120 }
4c068a98 121 if (S_ISDIR(entry.mode)) {
94537c78 122 int retval;
1c9da46d 123 char *newbase;
a8c40471 124 unsigned int pathlen = tree_entry_len(entry.path, entry.sha1);
94537c78 125
a8c40471 126 newbase = xmalloc(baselen + 1 + pathlen);
94537c78 127 memcpy(newbase, base, baselen);
a8c40471
LT
128 memcpy(newbase + baselen, entry.path, pathlen);
129 newbase[baselen + pathlen] = '/';
4c068a98 130 retval = read_tree_recursive(lookup_tree(entry.sha1),
94537c78 131 newbase,
a8c40471 132 baselen + pathlen + 1,
671f0707 133 stage, match, fn, context);
94537c78
LT
134 free(newbase);
135 if (retval)
136 return -1;
137 continue;
138 }
94537c78
LT
139 }
140 return 0;
141}
142
af3785dc
JH
143static int cmp_cache_name_compare(const void *a_, const void *b_)
144{
145 const struct cache_entry *ce1, *ce2;
146
147 ce1 = *((const struct cache_entry **)a_);
148 ce2 = *((const struct cache_entry **)b_);
7a51ed66
LT
149 return cache_name_compare(ce1->name, ce1->ce_flags,
150 ce2->name, ce2->ce_flags);
af3785dc
JH
151}
152
521698b1 153int read_tree(struct tree *tree, int stage, const char **match)
94537c78 154{
af3785dc
JH
155 read_tree_fn_t fn = NULL;
156 int i, err;
157
158 /*
159 * Currently the only existing callers of this function all
160 * call it with stage=1 and after making sure there is nothing
161 * at that stage; we could always use read_one_entry_quick().
162 *
163 * But when we decide to straighten out git-read-tree not to
164 * use unpack_trees() in some cases, this will probably start
165 * to matter.
166 */
167
168 /*
169 * See if we have cache entry at the stage. If so,
170 * do it the original slow way, otherwise, append and then
171 * sort at the end.
172 */
173 for (i = 0; !fn && i < active_nr; i++) {
174 struct cache_entry *ce = active_cache[i];
175 if (ce_stage(ce) == stage)
176 fn = read_one_entry;
177 }
178
179 if (!fn)
180 fn = read_one_entry_quick;
671f0707 181 err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
af3785dc
JH
182 if (fn == read_one_entry || err)
183 return err;
184
185 /*
186 * Sort the cache entry -- we need to nuke the cache tree, though.
187 */
188 cache_tree_free(&active_cache_tree);
189 qsort(active_cache, active_nr, sizeof(active_cache[0]),
190 cmp_cache_name_compare);
191 return 0;
94537c78
LT
192}
193
5d6ccf5c 194struct tree *lookup_tree(const unsigned char *sha1)
175785e5
DB
195{
196 struct object *obj = lookup_object(sha1);
100c5f3b
LT
197 if (!obj)
198 return create_object(sha1, OBJ_TREE, alloc_tree_node());
d1af002d 199 if (!obj->type)
1974632c
LT
200 obj->type = OBJ_TREE;
201 if (obj->type != OBJ_TREE) {
885a86ab
LT
202 error("Object %s is a %s, not a tree",
203 sha1_to_hex(sha1), typename(obj->type));
175785e5
DB
204 return NULL;
205 }
206 return (struct tree *) obj;
207}
208
2d9c58c6
LT
209int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
210{
175785e5
DB
211 if (item->object.parsed)
212 return 0;
213 item->object.parsed = 1;
136f2e54
LT
214 item->buffer = buffer;
215 item->size = size;
216
2d9c58c6
LT
217 return 0;
218}
219
bd2c39f5
NP
220int parse_tree(struct tree *item)
221{
21666f1a 222 enum object_type type;
bd2c39f5
NP
223 void *buffer;
224 unsigned long size;
bd2c39f5
NP
225
226 if (item->object.parsed)
227 return 0;
21666f1a 228 buffer = read_sha1_file(item->object.sha1, &type, &size);
bd2c39f5
NP
229 if (!buffer)
230 return error("Could not read %s",
231 sha1_to_hex(item->object.sha1));
21666f1a 232 if (type != OBJ_TREE) {
bd2c39f5
NP
233 free(buffer);
234 return error("Object %s not a tree",
235 sha1_to_hex(item->object.sha1));
236 }
136f2e54 237 return parse_tree_buffer(item, buffer, size);
bd2c39f5 238}
77675e2a
DB
239
240struct tree *parse_tree_indirect(const unsigned char *sha1)
241{
242 struct object *obj = parse_object(sha1);
243 do {
244 if (!obj)
245 return NULL;
1974632c 246 if (obj->type == OBJ_TREE)
77675e2a 247 return (struct tree *) obj;
1974632c 248 else if (obj->type == OBJ_COMMIT)
77675e2a 249 obj = &(((struct commit *) obj)->tree->object);
1974632c 250 else if (obj->type == OBJ_TAG)
77675e2a
DB
251 obj = ((struct tag *) obj)->tagged;
252 else
253 return NULL;
254 if (!obj->parsed)
255 parse_object(obj->sha1);
256 } while (1);
257}