]> git.ipfire.org Git - thirdparty/git.git/blame - tree.c
wildmatch: rename constants and update prototype
[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
ffd31f66
NTND
48static int read_tree_1(struct tree *tree, struct strbuf *base,
49 int stage, struct pathspec *pathspec,
50 read_tree_fn_t fn, void *context)
94537c78 51{
0790a42a 52 struct tree_desc desc;
4c068a98 53 struct name_entry entry;
ffd31f66 54 unsigned char sha1[20];
d688cf07
NTND
55 int len, oldlen = base->len;
56 enum interesting retval = entry_not_interesting;
0790a42a 57
521698b1
DB
58 if (parse_tree(tree))
59 return -1;
0790a42a 60
6fda5e51 61 init_tree_desc(&desc, tree->buffer, tree->size);
0790a42a 62
4c068a98 63 while (tree_entry(&desc, &entry)) {
d688cf07 64 if (retval != all_entries_interesting) {
ffd31f66 65 retval = tree_entry_interesting(&entry, base, 0, pathspec);
d688cf07 66 if (retval == all_entries_not_interesting)
ffd31f66 67 break;
d688cf07 68 if (retval == entry_not_interesting)
ffd31f66
NTND
69 continue;
70 }
0ca14a57 71
ffd31f66
NTND
72 switch (fn(entry.sha1, base->buf, base->len,
73 entry.path, entry.mode, stage, context)) {
3c5e8468
LT
74 case 0:
75 continue;
76 case READ_TREE_RECURSIVE:
ba19a808 77 break;
3c5e8468
LT
78 default:
79 return -1;
80 }
d3bee161 81
ffd31f66
NTND
82 if (S_ISDIR(entry.mode))
83 hashcpy(sha1, entry.sha1);
84 else if (S_ISGITLINK(entry.mode)) {
85 struct commit *commit;
d3bee161
LH
86
87 commit = lookup_commit(entry.sha1);
88 if (!commit)
ffd31f66
NTND
89 die("Commit %s in submodule path %s%s not found",
90 sha1_to_hex(entry.sha1),
91 base->buf, entry.path);
d3bee161
LH
92
93 if (parse_commit(commit))
ffd31f66
NTND
94 die("Invalid commit %s in submodule path %s%s",
95 sha1_to_hex(entry.sha1),
96 base->buf, entry.path);
97
98 hashcpy(sha1, commit->tree->object.sha1);
94537c78 99 }
ffd31f66
NTND
100 else
101 continue;
102
0de16337 103 len = tree_entry_len(&entry);
ffd31f66
NTND
104 strbuf_add(base, entry.path, len);
105 strbuf_addch(base, '/');
106 retval = read_tree_1(lookup_tree(sha1),
107 base, stage, pathspec,
108 fn, context);
109 strbuf_setlen(base, oldlen);
110 if (retval)
111 return -1;
94537c78
LT
112 }
113 return 0;
114}
115
ffd31f66
NTND
116int read_tree_recursive(struct tree *tree,
117 const char *base, int baselen,
f0096c06 118 int stage, struct pathspec *pathspec,
ffd31f66
NTND
119 read_tree_fn_t fn, void *context)
120{
121 struct strbuf sb = STRBUF_INIT;
f0096c06 122 int ret;
ffd31f66 123
ffd31f66 124 strbuf_add(&sb, base, baselen);
f0096c06 125 ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
ffd31f66 126 strbuf_release(&sb);
ffd31f66
NTND
127 return ret;
128}
129
af3785dc
JH
130static int cmp_cache_name_compare(const void *a_, const void *b_)
131{
132 const struct cache_entry *ce1, *ce2;
133
134 ce1 = *((const struct cache_entry **)a_);
135 ce2 = *((const struct cache_entry **)b_);
7a51ed66
LT
136 return cache_name_compare(ce1->name, ce1->ce_flags,
137 ce2->name, ce2->ce_flags);
af3785dc
JH
138}
139
f0096c06 140int read_tree(struct tree *tree, int stage, struct pathspec *match)
94537c78 141{
af3785dc
JH
142 read_tree_fn_t fn = NULL;
143 int i, err;
144
145 /*
146 * Currently the only existing callers of this function all
147 * call it with stage=1 and after making sure there is nothing
148 * at that stage; we could always use read_one_entry_quick().
149 *
150 * But when we decide to straighten out git-read-tree not to
151 * use unpack_trees() in some cases, this will probably start
152 * to matter.
153 */
154
155 /*
156 * See if we have cache entry at the stage. If so,
157 * do it the original slow way, otherwise, append and then
158 * sort at the end.
159 */
160 for (i = 0; !fn && i < active_nr; i++) {
161 struct cache_entry *ce = active_cache[i];
162 if (ce_stage(ce) == stage)
163 fn = read_one_entry;
164 }
165
166 if (!fn)
167 fn = read_one_entry_quick;
671f0707 168 err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
af3785dc
JH
169 if (fn == read_one_entry || err)
170 return err;
171
172 /*
173 * Sort the cache entry -- we need to nuke the cache tree, though.
174 */
175 cache_tree_free(&active_cache_tree);
176 qsort(active_cache, active_nr, sizeof(active_cache[0]),
177 cmp_cache_name_compare);
178 return 0;
94537c78
LT
179}
180
5d6ccf5c 181struct tree *lookup_tree(const unsigned char *sha1)
175785e5
DB
182{
183 struct object *obj = lookup_object(sha1);
100c5f3b
LT
184 if (!obj)
185 return create_object(sha1, OBJ_TREE, alloc_tree_node());
d1af002d 186 if (!obj->type)
1974632c
LT
187 obj->type = OBJ_TREE;
188 if (obj->type != OBJ_TREE) {
885a86ab
LT
189 error("Object %s is a %s, not a tree",
190 sha1_to_hex(sha1), typename(obj->type));
175785e5
DB
191 return NULL;
192 }
193 return (struct tree *) obj;
194}
195
2d9c58c6
LT
196int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
197{
175785e5
DB
198 if (item->object.parsed)
199 return 0;
200 item->object.parsed = 1;
136f2e54
LT
201 item->buffer = buffer;
202 item->size = size;
203
2d9c58c6
LT
204 return 0;
205}
206
bd2c39f5
NP
207int parse_tree(struct tree *item)
208{
21666f1a 209 enum object_type type;
bd2c39f5
NP
210 void *buffer;
211 unsigned long size;
bd2c39f5
NP
212
213 if (item->object.parsed)
214 return 0;
21666f1a 215 buffer = read_sha1_file(item->object.sha1, &type, &size);
bd2c39f5
NP
216 if (!buffer)
217 return error("Could not read %s",
218 sha1_to_hex(item->object.sha1));
21666f1a 219 if (type != OBJ_TREE) {
bd2c39f5
NP
220 free(buffer);
221 return error("Object %s not a tree",
222 sha1_to_hex(item->object.sha1));
223 }
136f2e54 224 return parse_tree_buffer(item, buffer, size);
bd2c39f5 225}
77675e2a
DB
226
227struct tree *parse_tree_indirect(const unsigned char *sha1)
228{
229 struct object *obj = parse_object(sha1);
230 do {
231 if (!obj)
232 return NULL;
1974632c 233 if (obj->type == OBJ_TREE)
77675e2a 234 return (struct tree *) obj;
1974632c 235 else if (obj->type == OBJ_COMMIT)
77675e2a 236 obj = &(((struct commit *) obj)->tree->object);
1974632c 237 else if (obj->type == OBJ_TAG)
77675e2a
DB
238 obj = ((struct tag *) obj)->tagged;
239 else
240 return NULL;
241 if (!obj->parsed)
242 parse_object(obj->sha1);
243 } while (1);
244}