blob: b224115e0f4d61368560eba406a04f0259b7c4f0 [file] [log] [blame]
Brandon Williams85ab50f2017-06-12 15:13:57 -07001#define NO_THE_INDEX_COMPATIBILITY_MACROS
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08002#include "cache.h"
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07003#include "cache-tree.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07004#include "tree.h"
5#include "blob.h"
Daniel Barkalow77675e22005-09-05 02:03:51 -04006#include "commit.h"
7#include "tag.h"
Linus Torvalds136f2e52006-05-29 12:16:12 -07008#include "tree-walk.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07009
10const char *tree_type = "tree";
11
Brandon Williams85ab50f2017-06-12 15:13:57 -070012static int read_one_entry_opt(struct index_state *istate,
13 const unsigned char *sha1,
14 const char *base, int baselen,
15 const char *pathname,
16 unsigned mode, int stage, int opt)
Linus Torvalds94537c72005-04-22 16:42:37 -070017{
Linus Torvalds3c5e8462005-11-26 09:38:20 -080018 int len;
19 unsigned int size;
20 struct cache_entry *ce;
21
22 if (S_ISDIR(mode))
23 return READ_TREE_RECURSIVE;
24
25 len = strlen(pathname);
26 size = cache_entry_size(baselen + len);
Peter Eriksen90321c12006-04-03 19:30:46 +010027 ce = xcalloc(1, size);
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. carlson99d1a982016-09-05 20:07:52 +000034 hashcpy(ce->oid.hash, sha1);
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
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +070038static int read_one_entry(const unsigned char *sha1, struct strbuf *base,
39 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;
43 return read_one_entry_opt(istate, sha1, 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 */
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +070052static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base,
53 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;
57 return read_one_entry_opt(istate, sha1, 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 Duyffd31f62011-03-25 16:34:18 +070062static int read_tree_1(struct tree *tree, struct strbuf *base,
Nguyễn Thái Ngọc Duy18e4f402013-07-14 15:35:52 +070063 int stage, const struct pathspec *pathspec,
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070064 read_tree_fn_t fn, void *context)
Linus Torvalds0ca14a52005-07-14 11:26:31 -070065{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070066 struct tree_desc desc;
67 struct name_entry entry;
brian m. carlsonf26efc52017-05-06 22:10:15 +000068 struct object_id oid;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110069 int len, oldlen = base->len;
70 enum interesting retval = entry_not_interesting;
Linus Torvalds0ca14a52005-07-14 11:26:31 -070071
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070072 if (parse_tree(tree))
73 return -1;
Linus Torvalds0ca14a52005-07-14 11:26:31 -070074
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070075 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds0ca14a52005-07-14 11:26:31 -070076
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070077 while (tree_entry(&desc, &entry)) {
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110078 if (retval != all_entries_interesting) {
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070079 retval = tree_entry_interesting(&entry, base, 0, pathspec);
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110080 if (retval == all_entries_not_interesting)
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070081 break;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110082 if (retval == entry_not_interesting)
Linus Torvalds0ca14a52005-07-14 11:26:31 -070083 continue;
84 }
85
brian m. carlson7d924c92016-04-17 23:10:39 +000086 switch (fn(entry.oid->hash, base,
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070087 entry.path, entry.mode, stage, context)) {
88 case 0:
89 continue;
90 case READ_TREE_RECURSIVE:
91 break;
92 default:
93 return -1;
94 }
95
96 if (S_ISDIR(entry.mode))
brian m. carlsonf26efc52017-05-06 22:10:15 +000097 oidcpy(&oid, entry.oid);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070098 else if (S_ISGITLINK(entry.mode)) {
99 struct commit *commit;
100
brian m. carlsonbc832662017-05-06 22:10:10 +0000101 commit = lookup_commit(entry.oid);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700102 if (!commit)
103 die("Commit %s in submodule path %s%s not found",
brian m. carlson7d924c92016-04-17 23:10:39 +0000104 oid_to_hex(entry.oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700105 base->buf, entry.path);
106
107 if (parse_commit(commit))
108 die("Invalid commit %s in submodule path %s%s",
brian m. carlson7d924c92016-04-17 23:10:39 +0000109 oid_to_hex(entry.oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700110 base->buf, entry.path);
111
brian m. carlsonf26efc52017-05-06 22:10:15 +0000112 oidcpy(&oid, &commit->tree->object.oid);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700113 }
114 else
Linus Torvalds0ca14a52005-07-14 11:26:31 -0700115 continue;
Linus Torvalds3e587632005-07-14 11:39:27 -0700116
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100117 len = tree_entry_len(&entry);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700118 strbuf_add(base, entry.path, len);
119 strbuf_addch(base, '/');
brian m. carlson740ee052017-05-06 22:10:17 +0000120 retval = read_tree_1(lookup_tree(&oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700121 base, stage, pathspec,
122 fn, context);
123 strbuf_setlen(base, oldlen);
124 if (retval)
125 return -1;
Linus Torvalds0ca14a52005-07-14 11:26:31 -0700126 }
127 return 0;
128}
129
Daniel Barkalow521698b2006-01-26 01:13:36 -0500130int read_tree_recursive(struct tree *tree,
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800131 const char *base, int baselen,
Nguyễn Thái Ngọc Duy18e4f402013-07-14 15:35:52 +0700132 int stage, const struct pathspec *pathspec,
René Scharfe671f0702008-07-14 21:22:12 +0200133 read_tree_fn_t fn, void *context)
Linus Torvalds94537c72005-04-22 16:42:37 -0700134{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700135 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700136 int ret;
Linus Torvalds0790a422006-05-29 12:17:28 -0700137
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700138 strbuf_add(&sb, base, baselen);
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700139 ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700140 strbuf_release(&sb);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700141 return ret;
Linus Torvalds94537c72005-04-22 16:42:37 -0700142}
143
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700144static int cmp_cache_name_compare(const void *a_, const void *b_)
145{
146 const struct cache_entry *ce1, *ce2;
147
148 ce1 = *((const struct cache_entry **)a_);
149 ce2 = *((const struct cache_entry **)b_);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200150 return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
151 ce2->name, ce2->ce_namelen, ce_stage(ce2));
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700152}
153
Brandon Williams85ab50f2017-06-12 15:13:57 -0700154int read_tree(struct tree *tree, int stage, struct pathspec *match,
155 struct index_state *istate)
Linus Torvalds94537c72005-04-22 16:42:37 -0700156{
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700157 read_tree_fn_t fn = NULL;
158 int i, err;
159
160 /*
161 * Currently the only existing callers of this function all
162 * call it with stage=1 and after making sure there is nothing
163 * at that stage; we could always use read_one_entry_quick().
164 *
165 * But when we decide to straighten out git-read-tree not to
166 * use unpack_trees() in some cases, this will probably start
167 * to matter.
168 */
169
170 /*
171 * See if we have cache entry at the stage. If so,
172 * do it the original slow way, otherwise, append and then
173 * sort at the end.
174 */
Brandon Williams85ab50f2017-06-12 15:13:57 -0700175 for (i = 0; !fn && i < istate->cache_nr; i++) {
176 const struct cache_entry *ce = istate->cache[i];
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700177 if (ce_stage(ce) == stage)
178 fn = read_one_entry;
179 }
180
181 if (!fn)
182 fn = read_one_entry_quick;
Brandon Williams85ab50f2017-06-12 15:13:57 -0700183 err = read_tree_recursive(tree, "", 0, stage, match, fn, istate);
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700184 if (fn == read_one_entry || err)
185 return err;
186
187 /*
188 * Sort the cache entry -- we need to nuke the cache tree, though.
189 */
Brandon Williams85ab50f2017-06-12 15:13:57 -0700190 cache_tree_free(&istate->cache_tree);
191 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700192 return 0;
Linus Torvalds94537c72005-04-22 16:42:37 -0700193}
194
brian m. carlson740ee052017-05-06 22:10:17 +0000195struct tree *lookup_tree(const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700196{
brian m. carlson740ee052017-05-06 22:10:17 +0000197 struct object *obj = lookup_object(oid->hash);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700198 if (!obj)
brian m. carlson740ee052017-05-06 22:10:17 +0000199 return create_object(oid->hash, alloc_tree_node());
Jeff King8ff226a2014-07-13 02:42:03 -0400200 return object_as_type(obj, OBJ_TREE, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700201}
202
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400203int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700204{
Daniel Barkalow175785e2005-04-18 11:39:48 -0700205 if (item->object.parsed)
206 return 0;
207 item->object.parsed = 1;
Linus Torvalds136f2e52006-05-29 12:16:12 -0700208 item->buffer = buffer;
209 item->size = size;
210
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700211 return 0;
212}
Linus Torvalds136f2e52006-05-29 12:16:12 -0700213
Jeff King9cc2b072015-06-01 05:56:26 -0400214int parse_tree_gently(struct tree *item, int quiet_on_missing)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400215{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500216 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400217 void *buffer;
218 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400219
220 if (item->object.parsed)
221 return 0;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000222 buffer = read_sha1_file(item->object.oid.hash, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400223 if (!buffer)
Jeff King9cc2b072015-06-01 05:56:26 -0400224 return quiet_on_missing ? -1 :
225 error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000226 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500227 if (type != OBJ_TREE) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400228 free(buffer);
229 return error("Object %s not a tree",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000230 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400231 }
Linus Torvalds136f2e52006-05-29 12:16:12 -0700232 return parse_tree_buffer(item, buffer, size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400233}
Daniel Barkalow77675e22005-09-05 02:03:51 -0400234
Jeff King6e454b92013-06-05 18:37:39 -0400235void free_tree_buffer(struct tree *tree)
236{
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000237 FREE_AND_NULL(tree->buffer);
Jeff King6e454b92013-06-05 18:37:39 -0400238 tree->size = 0;
239 tree->object.parsed = 0;
240}
241
brian m. carlsona9dbc172017-05-06 22:10:37 +0000242struct tree *parse_tree_indirect(const struct object_id *oid)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400243{
brian m. carlsonc251c832017-05-06 22:10:38 +0000244 struct object *obj = parse_object(oid);
Daniel Barkalow77675e22005-09-05 02:03:51 -0400245 do {
246 if (!obj)
247 return NULL;
Linus Torvalds19746322006-07-11 20:45:31 -0700248 if (obj->type == OBJ_TREE)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400249 return (struct tree *) obj;
Linus Torvalds19746322006-07-11 20:45:31 -0700250 else if (obj->type == OBJ_COMMIT)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400251 obj = &(((struct commit *) obj)->tree->object);
Linus Torvalds19746322006-07-11 20:45:31 -0700252 else if (obj->type == OBJ_TAG)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400253 obj = ((struct tag *) obj)->tagged;
254 else
255 return NULL;
256 if (!obj->parsed)
brian m. carlsonc251c832017-05-06 22:10:38 +0000257 parse_object(&obj->oid);
Daniel Barkalow77675e22005-09-05 02:03:51 -0400258 } while (1);
259}