]> git.ipfire.org Git - thirdparty/git.git/blame - tree.c
Merge branch 'jt/upload-pack-deepen-relative-proto-v2'
[thirdparty/git.git] / tree.c
CommitLineData
85ab50f9 1#define NO_THE_INDEX_COMPATIBILITY_MACROS
8f1d2e6f 2#include "cache.h"
af3785dc 3#include "cache-tree.h"
175785e5 4#include "tree.h"
cbd53a21 5#include "object-store.h"
175785e5 6#include "blob.h"
77675e2a
DB
7#include "commit.h"
8#include "tag.h"
14ba97f8 9#include "alloc.h"
136f2e54 10#include "tree-walk.h"
109cd76d 11#include "repository.h"
175785e5
DB
12
13const char *tree_type = "tree";
14
85ab50f9 15static int read_one_entry_opt(struct index_state *istate,
df46d77e 16 const struct object_id *oid,
85ab50f9
BW
17 const char *base, int baselen,
18 const char *pathname,
19 unsigned mode, int stage, int opt)
94537c78 20{
3c5e8468 21 int len;
3c5e8468
LT
22 struct cache_entry *ce;
23
24 if (S_ISDIR(mode))
25 return READ_TREE_RECURSIVE;
26
27 len = strlen(pathname);
a849735b 28 ce = make_empty_cache_entry(istate, baselen + len);
94537c78
LT
29
30 ce->ce_mode = create_ce_mode(mode);
b60e188c
TG
31 ce->ce_flags = create_ce_flags(stage);
32 ce->ce_namelen = baselen + len;
94537c78
LT
33 memcpy(ce->name, base, baselen);
34 memcpy(ce->name + baselen, pathname, len+1);
df46d77e 35 oidcpy(&ce->oid, oid);
85ab50f9 36 return add_index_entry(istate, ce, opt);
af3785dc
JH
37}
38
df46d77e 39static int read_one_entry(const struct object_id *oid, struct strbuf *base,
6a0b0b6d
NTND
40 const char *pathname, unsigned mode, int stage,
41 void *context)
af3785dc 42{
85ab50f9 43 struct index_state *istate = context;
df46d77e 44 return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
6a0b0b6d 45 mode, stage,
af3785dc
JH
46 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
47}
48
49/*
50 * This is used when the caller knows there is no existing entries at
51 * the stage that will conflict with the entry being added.
52 */
df46d77e 53static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
6a0b0b6d
NTND
54 const char *pathname, unsigned mode, int stage,
55 void *context)
af3785dc 56{
85ab50f9 57 struct index_state *istate = context;
df46d77e 58 return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
6a0b0b6d 59 mode, stage,
af3785dc 60 ADD_CACHE_JUST_APPEND);
94537c78
LT
61}
62
e092073d
NTND
63static int read_tree_1(struct repository *r,
64 struct tree *tree, struct strbuf *base,
18e4f405 65 int stage, const struct pathspec *pathspec,
ffd31f66 66 read_tree_fn_t fn, void *context)
94537c78 67{
0790a42a 68 struct tree_desc desc;
4c068a98 69 struct name_entry entry;
f26efc58 70 struct object_id oid;
d688cf07
NTND
71 int len, oldlen = base->len;
72 enum interesting retval = entry_not_interesting;
0790a42a 73
521698b1
DB
74 if (parse_tree(tree))
75 return -1;
0790a42a 76
6fda5e51 77 init_tree_desc(&desc, tree->buffer, tree->size);
0790a42a 78
4c068a98 79 while (tree_entry(&desc, &entry)) {
d688cf07 80 if (retval != all_entries_interesting) {
67022e02
NTND
81 retval = tree_entry_interesting(r->index, &entry,
82 base, 0, pathspec);
d688cf07 83 if (retval == all_entries_not_interesting)
ffd31f66 84 break;
d688cf07 85 if (retval == entry_not_interesting)
ffd31f66
NTND
86 continue;
87 }
0ca14a57 88
df46d77e 89 switch (fn(entry.oid, base,
ffd31f66 90 entry.path, entry.mode, stage, context)) {
3c5e8468
LT
91 case 0:
92 continue;
93 case READ_TREE_RECURSIVE:
ba19a808 94 break;
3c5e8468
LT
95 default:
96 return -1;
97 }
d3bee161 98
ffd31f66 99 if (S_ISDIR(entry.mode))
f26efc58 100 oidcpy(&oid, entry.oid);
ffd31f66
NTND
101 else if (S_ISGITLINK(entry.mode)) {
102 struct commit *commit;
d3bee161 103
e092073d 104 commit = lookup_commit(r, entry.oid);
d3bee161 105 if (!commit)
ffd31f66 106 die("Commit %s in submodule path %s%s not found",
7d924c91 107 oid_to_hex(entry.oid),
ffd31f66 108 base->buf, entry.path);
d3bee161
LH
109
110 if (parse_commit(commit))
ffd31f66 111 die("Invalid commit %s in submodule path %s%s",
7d924c91 112 oid_to_hex(entry.oid),
ffd31f66
NTND
113 base->buf, entry.path);
114
2e27bd77 115 oidcpy(&oid, get_commit_tree_oid(commit));
94537c78 116 }
ffd31f66
NTND
117 else
118 continue;
119
0de16337 120 len = tree_entry_len(&entry);
ffd31f66
NTND
121 strbuf_add(base, entry.path, len);
122 strbuf_addch(base, '/');
e092073d 123 retval = read_tree_1(r, lookup_tree(r, &oid),
ffd31f66
NTND
124 base, stage, pathspec,
125 fn, context);
126 strbuf_setlen(base, oldlen);
127 if (retval)
128 return -1;
94537c78
LT
129 }
130 return 0;
131}
132
e092073d
NTND
133int read_tree_recursive(struct repository *r,
134 struct tree *tree,
ffd31f66 135 const char *base, int baselen,
18e4f405 136 int stage, const struct pathspec *pathspec,
ffd31f66
NTND
137 read_tree_fn_t fn, void *context)
138{
139 struct strbuf sb = STRBUF_INIT;
f0096c06 140 int ret;
ffd31f66 141
ffd31f66 142 strbuf_add(&sb, base, baselen);
e092073d 143 ret = read_tree_1(r, tree, &sb, stage, pathspec, fn, context);
ffd31f66 144 strbuf_release(&sb);
ffd31f66
NTND
145 return ret;
146}
147
af3785dc
JH
148static int cmp_cache_name_compare(const void *a_, const void *b_)
149{
150 const struct cache_entry *ce1, *ce2;
151
152 ce1 = *((const struct cache_entry **)a_);
153 ce2 = *((const struct cache_entry **)b_);
b60e188c
TG
154 return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
155 ce2->name, ce2->ce_namelen, ce_stage(ce2));
af3785dc
JH
156}
157
e092073d
NTND
158int read_tree(struct repository *r, struct tree *tree, int stage,
159 struct pathspec *match, struct index_state *istate)
94537c78 160{
af3785dc
JH
161 read_tree_fn_t fn = NULL;
162 int i, err;
163
164 /*
165 * Currently the only existing callers of this function all
166 * call it with stage=1 and after making sure there is nothing
167 * at that stage; we could always use read_one_entry_quick().
168 *
169 * But when we decide to straighten out git-read-tree not to
170 * use unpack_trees() in some cases, this will probably start
171 * to matter.
172 */
173
174 /*
175 * See if we have cache entry at the stage. If so,
176 * do it the original slow way, otherwise, append and then
177 * sort at the end.
178 */
85ab50f9
BW
179 for (i = 0; !fn && i < istate->cache_nr; i++) {
180 const struct cache_entry *ce = istate->cache[i];
af3785dc
JH
181 if (ce_stage(ce) == stage)
182 fn = read_one_entry;
183 }
184
185 if (!fn)
186 fn = read_one_entry_quick;
e092073d 187 err = read_tree_recursive(r, tree, "", 0, stage, match, fn, istate);
af3785dc
JH
188 if (fn == read_one_entry || err)
189 return err;
190
191 /*
192 * Sort the cache entry -- we need to nuke the cache tree, though.
193 */
85ab50f9
BW
194 cache_tree_free(&istate->cache_tree);
195 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
af3785dc 196 return 0;
94537c78
LT
197}
198
f58a6cb6 199struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
175785e5 200{
f58a6cb6 201 struct object *obj = lookup_object(r, oid->hash);
100c5f3b 202 if (!obj)
f58a6cb6
SB
203 return create_object(r, oid->hash,
204 alloc_tree_node(r));
205 return object_as_type(r, obj, OBJ_TREE, 0);
175785e5
DB
206}
207
2d9c58c6
LT
208int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
209{
175785e5
DB
210 if (item->object.parsed)
211 return 0;
212 item->object.parsed = 1;
136f2e54
LT
213 item->buffer = buffer;
214 item->size = size;
215
2d9c58c6
LT
216 return 0;
217}
218
9cc2b07a 219int parse_tree_gently(struct tree *item, int quiet_on_missing)
bd2c39f5 220{
21666f1a 221 enum object_type type;
bd2c39f5
NP
222 void *buffer;
223 unsigned long size;
bd2c39f5
NP
224
225 if (item->object.parsed)
226 return 0;
b4f5aca4 227 buffer = read_object_file(&item->object.oid, &type, &size);
bd2c39f5 228 if (!buffer)
9cc2b07a
JK
229 return quiet_on_missing ? -1 :
230 error("Could not read %s",
f2fd0760 231 oid_to_hex(&item->object.oid));
21666f1a 232 if (type != OBJ_TREE) {
bd2c39f5
NP
233 free(buffer);
234 return error("Object %s not a tree",
f2fd0760 235 oid_to_hex(&item->object.oid));
bd2c39f5 236 }
136f2e54 237 return parse_tree_buffer(item, buffer, size);
bd2c39f5 238}
77675e2a 239
6e454b9a
JK
240void free_tree_buffer(struct tree *tree)
241{
6a83d902 242 FREE_AND_NULL(tree->buffer);
6e454b9a
JK
243 tree->size = 0;
244 tree->object.parsed = 0;
245}
246
a9dbc179 247struct tree *parse_tree_indirect(const struct object_id *oid)
77675e2a 248{
109cd76d 249 struct object *obj = parse_object(the_repository, oid);
77675e2a
DB
250 do {
251 if (!obj)
252 return NULL;
1974632c 253 if (obj->type == OBJ_TREE)
77675e2a 254 return (struct tree *) obj;
1974632c 255 else if (obj->type == OBJ_COMMIT)
2e27bd77 256 obj = &(get_commit_tree(((struct commit *)obj))->object);
1974632c 257 else if (obj->type == OBJ_TAG)
77675e2a
DB
258 obj = ((struct tag *) obj)->tagged;
259 else
260 return NULL;
261 if (!obj->parsed)
109cd76d 262 parse_object(the_repository, &obj->oid);
77675e2a
DB
263 } while (1);
264}