coccinelle: add a rule to make "type" code use FREE_AND_NULL()
[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);
b60e188c
TG
25 ce->ce_flags = create_ce_flags(stage);
26 ce->ce_namelen = baselen + len;
94537c78
LT
27 memcpy(ce->name, base, baselen);
28 memcpy(ce->name + baselen, pathname, len+1);
99d1a986 29 hashcpy(ce->oid.hash, sha1);
af3785dc
JH
30 return add_cache_entry(ce, opt);
31}
32
6a0b0b6d
NTND
33static int read_one_entry(const unsigned char *sha1, struct strbuf *base,
34 const char *pathname, unsigned mode, int stage,
35 void *context)
af3785dc 36{
6a0b0b6d
NTND
37 return read_one_entry_opt(sha1, base->buf, base->len, pathname,
38 mode, stage,
af3785dc
JH
39 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
40}
41
42/*
43 * This is used when the caller knows there is no existing entries at
44 * the stage that will conflict with the entry being added.
45 */
6a0b0b6d
NTND
46static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base,
47 const char *pathname, unsigned mode, int stage,
48 void *context)
af3785dc 49{
6a0b0b6d
NTND
50 return read_one_entry_opt(sha1, base->buf, base->len, pathname,
51 mode, stage,
af3785dc 52 ADD_CACHE_JUST_APPEND);
94537c78
LT
53}
54
ffd31f66 55static int read_tree_1(struct tree *tree, struct strbuf *base,
18e4f405 56 int stage, const struct pathspec *pathspec,
ffd31f66 57 read_tree_fn_t fn, void *context)
94537c78 58{
0790a42a 59 struct tree_desc desc;
4c068a98 60 struct name_entry entry;
f26efc58 61 struct object_id oid;
d688cf07
NTND
62 int len, oldlen = base->len;
63 enum interesting retval = entry_not_interesting;
0790a42a 64
521698b1
DB
65 if (parse_tree(tree))
66 return -1;
0790a42a 67
6fda5e51 68 init_tree_desc(&desc, tree->buffer, tree->size);
0790a42a 69
4c068a98 70 while (tree_entry(&desc, &entry)) {
d688cf07 71 if (retval != all_entries_interesting) {
ffd31f66 72 retval = tree_entry_interesting(&entry, base, 0, pathspec);
d688cf07 73 if (retval == all_entries_not_interesting)
ffd31f66 74 break;
d688cf07 75 if (retval == entry_not_interesting)
ffd31f66
NTND
76 continue;
77 }
0ca14a57 78
7d924c91 79 switch (fn(entry.oid->hash, base,
ffd31f66 80 entry.path, entry.mode, stage, context)) {
3c5e8468
LT
81 case 0:
82 continue;
83 case READ_TREE_RECURSIVE:
ba19a808 84 break;
3c5e8468
LT
85 default:
86 return -1;
87 }
d3bee161 88
ffd31f66 89 if (S_ISDIR(entry.mode))
f26efc58 90 oidcpy(&oid, entry.oid);
ffd31f66
NTND
91 else if (S_ISGITLINK(entry.mode)) {
92 struct commit *commit;
d3bee161 93
bc83266a 94 commit = lookup_commit(entry.oid);
d3bee161 95 if (!commit)
ffd31f66 96 die("Commit %s in submodule path %s%s not found",
7d924c91 97 oid_to_hex(entry.oid),
ffd31f66 98 base->buf, entry.path);
d3bee161
LH
99
100 if (parse_commit(commit))
ffd31f66 101 die("Invalid commit %s in submodule path %s%s",
7d924c91 102 oid_to_hex(entry.oid),
ffd31f66
NTND
103 base->buf, entry.path);
104
f26efc58 105 oidcpy(&oid, &commit->tree->object.oid);
94537c78 106 }
ffd31f66
NTND
107 else
108 continue;
109
0de16337 110 len = tree_entry_len(&entry);
ffd31f66
NTND
111 strbuf_add(base, entry.path, len);
112 strbuf_addch(base, '/');
740ee055 113 retval = read_tree_1(lookup_tree(&oid),
ffd31f66
NTND
114 base, stage, pathspec,
115 fn, context);
116 strbuf_setlen(base, oldlen);
117 if (retval)
118 return -1;
94537c78
LT
119 }
120 return 0;
121}
122
ffd31f66
NTND
123int read_tree_recursive(struct tree *tree,
124 const char *base, int baselen,
18e4f405 125 int stage, const struct pathspec *pathspec,
ffd31f66
NTND
126 read_tree_fn_t fn, void *context)
127{
128 struct strbuf sb = STRBUF_INIT;
f0096c06 129 int ret;
ffd31f66 130
ffd31f66 131 strbuf_add(&sb, base, baselen);
f0096c06 132 ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
ffd31f66 133 strbuf_release(&sb);
ffd31f66
NTND
134 return ret;
135}
136
af3785dc
JH
137static int cmp_cache_name_compare(const void *a_, const void *b_)
138{
139 const struct cache_entry *ce1, *ce2;
140
141 ce1 = *((const struct cache_entry **)a_);
142 ce2 = *((const struct cache_entry **)b_);
b60e188c
TG
143 return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
144 ce2->name, ce2->ce_namelen, ce_stage(ce2));
af3785dc
JH
145}
146
f0096c06 147int read_tree(struct tree *tree, int stage, struct pathspec *match)
94537c78 148{
af3785dc
JH
149 read_tree_fn_t fn = NULL;
150 int i, err;
151
152 /*
153 * Currently the only existing callers of this function all
154 * call it with stage=1 and after making sure there is nothing
155 * at that stage; we could always use read_one_entry_quick().
156 *
157 * But when we decide to straighten out git-read-tree not to
158 * use unpack_trees() in some cases, this will probably start
159 * to matter.
160 */
161
162 /*
163 * See if we have cache entry at the stage. If so,
164 * do it the original slow way, otherwise, append and then
165 * sort at the end.
166 */
167 for (i = 0; !fn && i < active_nr; i++) {
9c5e6c80 168 const struct cache_entry *ce = active_cache[i];
af3785dc
JH
169 if (ce_stage(ce) == stage)
170 fn = read_one_entry;
171 }
172
173 if (!fn)
174 fn = read_one_entry_quick;
671f0707 175 err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
af3785dc
JH
176 if (fn == read_one_entry || err)
177 return err;
178
179 /*
180 * Sort the cache entry -- we need to nuke the cache tree, though.
181 */
182 cache_tree_free(&active_cache_tree);
9ed0d8d6 183 QSORT(active_cache, active_nr, cmp_cache_name_compare);
af3785dc 184 return 0;
94537c78
LT
185}
186
740ee055 187struct tree *lookup_tree(const struct object_id *oid)
175785e5 188{
740ee055 189 struct object *obj = lookup_object(oid->hash);
100c5f3b 190 if (!obj)
740ee055 191 return create_object(oid->hash, alloc_tree_node());
8ff226a9 192 return object_as_type(obj, OBJ_TREE, 0);
175785e5
DB
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
9cc2b07a 206int parse_tree_gently(struct tree *item, int quiet_on_missing)
bd2c39f5 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;
ed1c9977 214 buffer = read_sha1_file(item->object.oid.hash, &type, &size);
bd2c39f5 215 if (!buffer)
9cc2b07a
JK
216 return quiet_on_missing ? -1 :
217 error("Could not read %s",
f2fd0760 218 oid_to_hex(&item->object.oid));
21666f1a 219 if (type != OBJ_TREE) {
bd2c39f5
NP
220 free(buffer);
221 return error("Object %s not a tree",
f2fd0760 222 oid_to_hex(&item->object.oid));
bd2c39f5 223 }
136f2e54 224 return parse_tree_buffer(item, buffer, size);
bd2c39f5 225}
77675e2a 226
6e454b9a
JK
227void free_tree_buffer(struct tree *tree)
228{
229 free(tree->buffer);
230 tree->buffer = NULL;
231 tree->size = 0;
232 tree->object.parsed = 0;
233}
234
a9dbc179 235struct tree *parse_tree_indirect(const struct object_id *oid)
77675e2a 236{
c251c83d 237 struct object *obj = parse_object(oid);
77675e2a
DB
238 do {
239 if (!obj)
240 return NULL;
1974632c 241 if (obj->type == OBJ_TREE)
77675e2a 242 return (struct tree *) obj;
1974632c 243 else if (obj->type == OBJ_COMMIT)
77675e2a 244 obj = &(((struct commit *) obj)->tree->object);
1974632c 245 else if (obj->type == OBJ_TAG)
77675e2a
DB
246 obj = ((struct tag *) obj)->tagged;
247 else
248 return NULL;
249 if (!obj->parsed)
c251c83d 250 parse_object(&obj->oid);
77675e2a
DB
251 } while (1);
252}