blob: f416afc57d784f8f63ba66ec8c7424ef1e4fcaa1 [file] [log] [blame]
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08001#include "cache.h"
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07002#include "cache-tree.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07003#include "tree.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07005#include "blob.h"
Daniel Barkalow77675e22005-09-05 02:03:51 -04006#include "commit.h"
7#include "tag.h"
Stefan Beller14ba97f2018-05-15 14:48:42 -07008#include "alloc.h"
Linus Torvalds136f2e52006-05-29 12:16:12 -07009#include "tree-walk.h"
Stefan Beller109cd762018-06-28 18:21:51 -070010#include "repository.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -070011
12const char *tree_type = "tree";
13
Brandon Williams85ab50f2017-06-12 15:13:57 -070014static int read_one_entry_opt(struct index_state *istate,
brian m. carlsondf46d772018-03-12 02:27:26 +000015 const struct object_id *oid,
Brandon Williams85ab50f2017-06-12 15:13:57 -070016 const char *base, int baselen,
17 const char *pathname,
18 unsigned mode, int stage, int opt)
Linus Torvalds94537c72005-04-22 16:42:37 -070019{
Linus Torvalds3c5e8462005-11-26 09:38:20 -080020 int len;
Linus Torvalds3c5e8462005-11-26 09:38:20 -080021 struct cache_entry *ce;
22
23 if (S_ISDIR(mode))
24 return READ_TREE_RECURSIVE;
25
26 len = strlen(pathname);
Jameson Millera8497352018-07-02 19:49:31 +000027 ce = make_empty_cache_entry(istate, baselen + len);
Linus Torvalds94537c72005-04-22 16:42:37 -070028
29 ce->ce_mode = create_ce_mode(mode);
Thomas Gummererb60e1882012-07-11 11:22:37 +020030 ce->ce_flags = create_ce_flags(stage);
31 ce->ce_namelen = baselen + len;
Linus Torvalds94537c72005-04-22 16:42:37 -070032 memcpy(ce->name, base, baselen);
33 memcpy(ce->name + baselen, pathname, len+1);
brian m. carlsondf46d772018-03-12 02:27:26 +000034 oidcpy(&ce->oid, oid);
Brandon Williams85ab50f2017-06-12 15:13:57 -070035 return add_index_entry(istate, ce, opt);
Junio C Hamanoaf3785d2007-08-09 13:42:50 -070036}
37
brian m. carlsondf46d772018-03-12 02:27:26 +000038static int read_one_entry(const struct object_id *oid, struct strbuf *base,
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +070039 const char *pathname, unsigned mode, int stage,
40 void *context)
Junio C Hamanoaf3785d2007-08-09 13:42:50 -070041{
Brandon Williams85ab50f2017-06-12 15:13:57 -070042 struct index_state *istate = context;
brian m. carlsondf46d772018-03-12 02:27:26 +000043 return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +070044 mode, stage,
Junio C Hamanoaf3785d2007-08-09 13:42:50 -070045 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
46}
47
48/*
49 * This is used when the caller knows there is no existing entries at
50 * the stage that will conflict with the entry being added.
51 */
brian m. carlsondf46d772018-03-12 02:27:26 +000052static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +070053 const char *pathname, unsigned mode, int stage,
54 void *context)
Junio C Hamanoaf3785d2007-08-09 13:42:50 -070055{
Brandon Williams85ab50f2017-06-12 15:13:57 -070056 struct index_state *istate = context;
brian m. carlsondf46d772018-03-12 02:27:26 +000057 return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +070058 mode, stage,
Junio C Hamanoaf3785d2007-08-09 13:42:50 -070059 ADD_CACHE_JUST_APPEND);
Linus Torvalds94537c72005-04-22 16:42:37 -070060}
61
Nguyễn Thái Ngọc Duye0920732018-11-18 17:47:56 +010062static int read_tree_1(struct repository *r,
63 struct tree *tree, struct strbuf *base,
Nguyễn Thái Ngọc Duy18e4f402013-07-14 15:35:52 +070064 int stage, const struct pathspec *pathspec,
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070065 read_tree_fn_t fn, void *context)
Linus Torvalds0ca14a52005-07-14 11:26:31 -070066{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070067 struct tree_desc desc;
68 struct name_entry entry;
brian m. carlsonf26efc52017-05-06 22:10:15 +000069 struct object_id oid;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110070 int len, oldlen = base->len;
71 enum interesting retval = entry_not_interesting;
Linus Torvalds0ca14a52005-07-14 11:26:31 -070072
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070073 if (parse_tree(tree))
74 return -1;
Linus Torvalds0ca14a52005-07-14 11:26:31 -070075
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070076 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds0ca14a52005-07-14 11:26:31 -070077
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070078 while (tree_entry(&desc, &entry)) {
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110079 if (retval != all_entries_interesting) {
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +010080 retval = tree_entry_interesting(r->index, &entry,
81 base, 0, pathspec);
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110082 if (retval == all_entries_not_interesting)
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070083 break;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110084 if (retval == entry_not_interesting)
Linus Torvalds0ca14a52005-07-14 11:26:31 -070085 continue;
86 }
87
brian m. carlsonea82b2a2019-01-15 00:39:44 +000088 switch (fn(&entry.oid, base,
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070089 entry.path, entry.mode, stage, context)) {
90 case 0:
91 continue;
92 case READ_TREE_RECURSIVE:
93 break;
94 default:
95 return -1;
96 }
97
98 if (S_ISDIR(entry.mode))
brian m. carlsonea82b2a2019-01-15 00:39:44 +000099 oidcpy(&oid, &entry.oid);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700100 else if (S_ISGITLINK(entry.mode)) {
101 struct commit *commit;
102
Junio C Hamano371820d2019-01-29 12:47:56 -0800103 commit = lookup_commit(r, &entry.oid);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700104 if (!commit)
105 die("Commit %s in submodule path %s%s not found",
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000106 oid_to_hex(&entry.oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700107 base->buf, entry.path);
108
109 if (parse_commit(commit))
110 die("Invalid commit %s in submodule path %s%s",
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000111 oid_to_hex(&entry.oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700112 base->buf, entry.path);
113
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000114 oidcpy(&oid, get_commit_tree_oid(commit));
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700115 }
116 else
Linus Torvalds0ca14a52005-07-14 11:26:31 -0700117 continue;
Linus Torvalds3e587632005-07-14 11:39:27 -0700118
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100119 len = tree_entry_len(&entry);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700120 strbuf_add(base, entry.path, len);
121 strbuf_addch(base, '/');
Nguyễn Thái Ngọc Duye0920732018-11-18 17:47:56 +0100122 retval = read_tree_1(r, lookup_tree(r, &oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700123 base, stage, pathspec,
124 fn, context);
125 strbuf_setlen(base, oldlen);
126 if (retval)
127 return -1;
Linus Torvalds0ca14a52005-07-14 11:26:31 -0700128 }
129 return 0;
130}
131
Nguyễn Thái Ngọc Duye0920732018-11-18 17:47:56 +0100132int read_tree_recursive(struct repository *r,
133 struct tree *tree,
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800134 const char *base, int baselen,
Nguyễn Thái Ngọc Duy18e4f402013-07-14 15:35:52 +0700135 int stage, const struct pathspec *pathspec,
René Scharfe671f0702008-07-14 21:22:12 +0200136 read_tree_fn_t fn, void *context)
Linus Torvalds94537c72005-04-22 16:42:37 -0700137{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700138 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700139 int ret;
Linus Torvalds0790a422006-05-29 12:17:28 -0700140
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700141 strbuf_add(&sb, base, baselen);
Nguyễn Thái Ngọc Duye0920732018-11-18 17:47:56 +0100142 ret = read_tree_1(r, tree, &sb, stage, pathspec, fn, context);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700143 strbuf_release(&sb);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700144 return ret;
Linus Torvalds94537c72005-04-22 16:42:37 -0700145}
146
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700147static int cmp_cache_name_compare(const void *a_, const void *b_)
148{
149 const struct cache_entry *ce1, *ce2;
150
151 ce1 = *((const struct cache_entry **)a_);
152 ce2 = *((const struct cache_entry **)b_);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200153 return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
154 ce2->name, ce2->ce_namelen, ce_stage(ce2));
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700155}
156
Nguyễn Thái Ngọc Duye0920732018-11-18 17:47:56 +0100157int read_tree(struct repository *r, struct tree *tree, int stage,
158 struct pathspec *match, struct index_state *istate)
Linus Torvalds94537c72005-04-22 16:42:37 -0700159{
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700160 read_tree_fn_t fn = NULL;
161 int i, err;
162
163 /*
164 * Currently the only existing callers of this function all
165 * call it with stage=1 and after making sure there is nothing
166 * at that stage; we could always use read_one_entry_quick().
167 *
168 * But when we decide to straighten out git-read-tree not to
169 * use unpack_trees() in some cases, this will probably start
170 * to matter.
171 */
172
173 /*
174 * See if we have cache entry at the stage. If so,
175 * do it the original slow way, otherwise, append and then
176 * sort at the end.
177 */
Brandon Williams85ab50f2017-06-12 15:13:57 -0700178 for (i = 0; !fn && i < istate->cache_nr; i++) {
179 const struct cache_entry *ce = istate->cache[i];
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700180 if (ce_stage(ce) == stage)
181 fn = read_one_entry;
182 }
183
184 if (!fn)
185 fn = read_one_entry_quick;
Nguyễn Thái Ngọc Duye0920732018-11-18 17:47:56 +0100186 err = read_tree_recursive(r, tree, "", 0, stage, match, fn, istate);
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700187 if (fn == read_one_entry || err)
188 return err;
189
190 /*
191 * Sort the cache entry -- we need to nuke the cache tree, though.
192 */
Brandon Williams85ab50f2017-06-12 15:13:57 -0700193 cache_tree_free(&istate->cache_tree);
194 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700195 return 0;
Linus Torvalds94537c72005-04-22 16:42:37 -0700196}
197
Stefan Bellerf58a6cb2018-06-28 18:22:09 -0700198struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700199{
Stefan Bellerf58a6cb2018-06-28 18:22:09 -0700200 struct object *obj = lookup_object(r, oid->hash);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700201 if (!obj)
Stefan Bellerf58a6cb2018-06-28 18:22:09 -0700202 return create_object(r, oid->hash,
203 alloc_tree_node(r));
204 return object_as_type(r, obj, OBJ_TREE, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700205}
206
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400207int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700208{
Daniel Barkalow175785e2005-04-18 11:39:48 -0700209 if (item->object.parsed)
210 return 0;
211 item->object.parsed = 1;
Linus Torvalds136f2e52006-05-29 12:16:12 -0700212 item->buffer = buffer;
213 item->size = size;
214
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700215 return 0;
216}
Linus Torvalds136f2e52006-05-29 12:16:12 -0700217
Jeff King9cc2b072015-06-01 05:56:26 -0400218int parse_tree_gently(struct tree *item, int quiet_on_missing)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400219{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500220 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400221 void *buffer;
222 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400223
224 if (item->object.parsed)
225 return 0;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000226 buffer = read_object_file(&item->object.oid, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400227 if (!buffer)
Jeff King9cc2b072015-06-01 05:56:26 -0400228 return quiet_on_missing ? -1 :
229 error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000230 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500231 if (type != OBJ_TREE) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400232 free(buffer);
233 return error("Object %s not a tree",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000234 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400235 }
Linus Torvalds136f2e52006-05-29 12:16:12 -0700236 return parse_tree_buffer(item, buffer, size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400237}
Daniel Barkalow77675e22005-09-05 02:03:51 -0400238
Jeff King6e454b92013-06-05 18:37:39 -0400239void free_tree_buffer(struct tree *tree)
240{
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000241 FREE_AND_NULL(tree->buffer);
Jeff King6e454b92013-06-05 18:37:39 -0400242 tree->size = 0;
243 tree->object.parsed = 0;
244}
245
brian m. carlsona9dbc172017-05-06 22:10:37 +0000246struct tree *parse_tree_indirect(const struct object_id *oid)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400247{
Stefan Beller109cd762018-06-28 18:21:51 -0700248 struct object *obj = parse_object(the_repository, oid);
Daniel Barkalow77675e22005-09-05 02:03:51 -0400249 do {
250 if (!obj)
251 return NULL;
Linus Torvalds19746322006-07-11 20:45:31 -0700252 if (obj->type == OBJ_TREE)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400253 return (struct tree *) obj;
Linus Torvalds19746322006-07-11 20:45:31 -0700254 else if (obj->type == OBJ_COMMIT)
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000255 obj = &(get_commit_tree(((struct commit *)obj))->object);
Linus Torvalds19746322006-07-11 20:45:31 -0700256 else if (obj->type == OBJ_TAG)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400257 obj = ((struct tag *) obj)->tagged;
258 else
259 return NULL;
260 if (!obj->parsed)
Stefan Beller109cd762018-06-28 18:21:51 -0700261 parse_object(the_repository, &obj->oid);
Daniel Barkalow77675e22005-09-05 02:03:51 -0400262 } while (1);
263}