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