🌐 AI搜索 & 代理 主页
blob: e76517f6b180e4703a032706f6d388d491df22a5 [file] [log] [blame]
Junio C Hamano8f1d2e62006-01-07 09:33:541#include "cache.h"
Junio C Hamanoaf3785d2007-08-09 20:42:502#include "cache-tree.h"
Daniel Barkalow175785e2005-04-18 18:39:483#include "tree.h"
Stefan Bellercbd53a22018-05-15 23:42:154#include "object-store.h"
Daniel Barkalow175785e2005-04-18 18:39:485#include "blob.h"
Daniel Barkalow77675e22005-09-05 06:03:516#include "commit.h"
7#include "tag.h"
Stefan Beller14ba97f2018-05-15 21:48:428#include "alloc.h"
Linus Torvalds136f2e52006-05-29 19:16:129#include "tree-walk.h"
Stefan Beller109cd762018-06-29 01:21:5110#include "repository.h"
Daniel Barkalow175785e2005-04-18 18:39:4811
12const char *tree_type = "tree";
13
Brandon Williams85ab50f2017-06-12 22:13:5714static int read_one_entry_opt(struct index_state *istate,
brian m. carlsondf46d772018-03-12 02:27:2615 const struct object_id *oid,
Brandon Williams85ab50f2017-06-12 22:13:5716 const char *base, int baselen,
17 const char *pathname,
18 unsigned mode, int stage, int opt)
Linus Torvalds94537c72005-04-22 23:42:3719{
Linus Torvalds3c5e8462005-11-26 17:38:2020 int len;
Linus Torvalds3c5e8462005-11-26 17:38:2021 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:3127 ce = make_empty_cache_entry(istate, baselen + len);
Linus Torvalds94537c72005-04-22 23:42:3728
29 ce->ce_mode = create_ce_mode(mode);
Thomas Gummererb60e1882012-07-11 09:22:3730 ce->ce_flags = create_ce_flags(stage);
31 ce->ce_namelen = baselen + len;
Linus Torvalds94537c72005-04-22 23:42:3732 memcpy(ce->name, base, baselen);
33 memcpy(ce->name + baselen, pathname, len+1);
brian m. carlsondf46d772018-03-12 02:27:2634 oidcpy(&ce->oid, oid);
Brandon Williams85ab50f2017-06-12 22:13:5735 return add_index_entry(istate, ce, opt);
Junio C Hamanoaf3785d2007-08-09 20:42:5036}
37
brian m. carlsondf46d772018-03-12 02:27:2638static int read_one_entry(const struct object_id *oid, struct strbuf *base,
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 09:05:0039 const char *pathname, unsigned mode, int stage,
40 void *context)
Junio C Hamanoaf3785d2007-08-09 20:42:5041{
Brandon Williams85ab50f2017-06-12 22:13:5742 struct index_state *istate = context;
brian m. carlsondf46d772018-03-12 02:27:2643 return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 09:05:0044 mode, stage,
Junio C Hamanoaf3785d2007-08-09 20:42:5045 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:2652static int read_one_entry_quick(const struct object_id *oid, struct strbuf *base,
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 09:05:0053 const char *pathname, unsigned mode, int stage,
54 void *context)
Junio C Hamanoaf3785d2007-08-09 20:42:5055{
Brandon Williams85ab50f2017-06-12 22:13:5756 struct index_state *istate = context;
brian m. carlsondf46d772018-03-12 02:27:2657 return read_one_entry_opt(istate, oid, base->buf, base->len, pathname,
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 09:05:0058 mode, stage,
Junio C Hamanoaf3785d2007-08-09 20:42:5059 ADD_CACHE_JUST_APPEND);
Linus Torvalds94537c72005-04-22 23:42:3760}
61
Nguyễn Thái Ngọc Duye0920732018-11-18 16:47:5662static int read_tree_1(struct repository *r,
63 struct tree *tree, struct strbuf *base,
Nguyễn Thái Ngọc Duy18e4f402013-07-14 08:35:5264 int stage, const struct pathspec *pathspec,
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1865 read_tree_fn_t fn, void *context)
Linus Torvalds0ca14a52005-07-14 18:26:3166{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1867 struct tree_desc desc;
68 struct name_entry entry;
brian m. carlsonf26efc52017-05-06 22:10:1569 struct object_id oid;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 06:36:1070 int len, oldlen = base->len;
71 enum interesting retval = entry_not_interesting;
Linus Torvalds0ca14a52005-07-14 18:26:3172
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1873 if (parse_tree(tree))
74 return -1;
Linus Torvalds0ca14a52005-07-14 18:26:3175
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1876 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds0ca14a52005-07-14 18:26:3177
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1878 while (tree_entry(&desc, &entry)) {
Nguyễn Thái Ngọc Duyd688cf02011-10-24 06:36:1079 if (retval != all_entries_interesting) {
Nguyễn Thái Ngọc Duy67022e02018-11-18 16:47:5780 retval = tree_entry_interesting(r->index, &entry,
81 base, 0, pathspec);
Nguyễn Thái Ngọc Duyd688cf02011-10-24 06:36:1082 if (retval == all_entries_not_interesting)
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1883 break;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 06:36:1084 if (retval == entry_not_interesting)
Linus Torvalds0ca14a52005-07-14 18:26:3185 continue;
86 }
87
brian m. carlsonea82b2a2019-01-15 00:39:4488 switch (fn(&entry.oid, base,
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1889 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:4499 oidcpy(&oid, &entry.oid);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18100 else if (S_ISGITLINK(entry.mode)) {
101 struct commit *commit;
102
Junio C Hamano371820d2019-01-29 20:47:56103 commit = lookup_commit(r, &entry.oid);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18104 if (!commit)
105 die("Commit %s in submodule path %s%s not found",
brian m. carlsonea82b2a2019-01-15 00:39:44106 oid_to_hex(&entry.oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18107 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:44111 oid_to_hex(&entry.oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18112 base->buf, entry.path);
113
Derrick Stolee2e27bd72018-04-06 19:09:38114 oidcpy(&oid, get_commit_tree_oid(commit));
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18115 }
116 else
Linus Torvalds0ca14a52005-07-14 18:26:31117 continue;
Linus Torvalds3e587632005-07-14 18:39:27118
Nguyễn Thái Ngọc Duy0de16332011-10-24 06:36:09119 len = tree_entry_len(&entry);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18120 strbuf_add(base, entry.path, len);
121 strbuf_addch(base, '/');
Nguyễn Thái Ngọc Duye0920732018-11-18 16:47:56122 retval = read_tree_1(r, lookup_tree(r, &oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18123 base, stage, pathspec,
124 fn, context);
125 strbuf_setlen(base, oldlen);
126 if (retval)
127 return -1;
Linus Torvalds0ca14a52005-07-14 18:26:31128 }
129 return 0;
130}
131
Nguyễn Thái Ngọc Duye0920732018-11-18 16:47:56132int read_tree_recursive(struct repository *r,
133 struct tree *tree,
Linus Torvalds3c5e8462005-11-26 17:38:20134 const char *base, int baselen,
Nguyễn Thái Ngọc Duy18e4f402013-07-14 08:35:52135 int stage, const struct pathspec *pathspec,
René Scharfe671f0702008-07-14 19:22:12136 read_tree_fn_t fn, void *context)
Linus Torvalds94537c72005-04-22 23:42:37137{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18138 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyf0096c02011-03-25 09:34:19139 int ret;
Linus Torvalds0790a422006-05-29 19:17:28140
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18141 strbuf_add(&sb, base, baselen);
Nguyễn Thái Ngọc Duye0920732018-11-18 16:47:56142 ret = read_tree_1(r, tree, &sb, stage, pathspec, fn, context);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18143 strbuf_release(&sb);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18144 return ret;
Linus Torvalds94537c72005-04-22 23:42:37145}
146
Junio C Hamanoaf3785d2007-08-09 20:42:50147static 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 09:22:37153 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 20:42:50155}
156
Nguyễn Thái Ngọc Duye0920732018-11-18 16:47:56157int read_tree(struct repository *r, struct tree *tree, int stage,
158 struct pathspec *match, struct index_state *istate)
Linus Torvalds94537c72005-04-22 23:42:37159{
Junio C Hamanoaf3785d2007-08-09 20:42:50160 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 22:13:57178 for (i = 0; !fn && i < istate->cache_nr; i++) {
179 const struct cache_entry *ce = istate->cache[i];
Junio C Hamanoaf3785d2007-08-09 20:42:50180 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 16:47:56186 err = read_tree_recursive(r, tree, "", 0, stage, match, fn, istate);
Junio C Hamanoaf3785d2007-08-09 20:42:50187 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 22:13:57193 cache_tree_free(&istate->cache_tree);
194 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
Junio C Hamanoaf3785d2007-08-09 20:42:50195 return 0;
Linus Torvalds94537c72005-04-22 23:42:37196}
197
Stefan Bellerf58a6cb2018-06-29 01:22:09198struct tree *lookup_tree(struct repository *r, const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 18:39:48199{
Jeff Kingd0229ab2019-06-20 07:41:14200 struct object *obj = lookup_object(r, oid);
Linus Torvalds100c5f32007-04-17 05:11:43201 if (!obj)
Jeff Kinga3785092019-06-20 07:41:21202 return create_object(r, oid, alloc_tree_node(r));
Abhishek Kumar6da43d92020-06-17 09:14:08203 return object_as_type(obj, OBJ_TREE, 0);
Daniel Barkalow175785e2005-04-18 18:39:48204}
205
Nicolas Pitrebd2c39f2005-05-06 17:48:34206int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 18:39:48207{
Daniel Barkalow175785e2005-04-18 18:39:48208 if (item->object.parsed)
209 return 0;
210 item->object.parsed = 1;
Linus Torvalds136f2e52006-05-29 19:16:12211 item->buffer = buffer;
212 item->size = size;
213
Linus Torvalds2d9c58c2006-05-29 19:18:33214 return 0;
215}
Linus Torvalds136f2e52006-05-29 19:16:12216
Jeff King9cc2b072015-06-01 09:56:26217int parse_tree_gently(struct tree *item, int quiet_on_missing)
Nicolas Pitrebd2c39f2005-05-06 17:48:34218{
Nicolas Pitre21666f12007-02-26 19:55:59219 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 17:48:34220 void *buffer;
221 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 17:48:34222
223 if (item->object.parsed)
224 return 0;
brian m. carlsonb4f5aca2018-03-12 02:27:53225 buffer = read_object_file(&item->object.oid, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34226 if (!buffer)
Jeff King9cc2b072015-06-01 09:56:26227 return quiet_on_missing ? -1 :
228 error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28229 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 19:55:59230 if (type != OBJ_TREE) {
Nicolas Pitrebd2c39f2005-05-06 17:48:34231 free(buffer);
232 return error("Object %s not a tree",
brian m. carlsonf2fd0762015-11-10 02:22:28233 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 17:48:34234 }
Linus Torvalds136f2e52006-05-29 19:16:12235 return parse_tree_buffer(item, buffer, size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34236}
Daniel Barkalow77675e22005-09-05 06:03:51237
Jeff King6e454b92013-06-05 22:37:39238void free_tree_buffer(struct tree *tree)
239{
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46240 FREE_AND_NULL(tree->buffer);
Jeff King6e454b92013-06-05 22:37:39241 tree->size = 0;
242 tree->object.parsed = 0;
243}
244
brian m. carlsona9dbc172017-05-06 22:10:37245struct tree *parse_tree_indirect(const struct object_id *oid)
Daniel Barkalow77675e22005-09-05 06:03:51246{
René Scharfe1577dc02019-08-29 19:06:22247 struct repository *r = the_repository;
248 struct object *obj = parse_object(r, oid);
249 return (struct tree *)repo_peel_to_type(r, NULL, 0, obj, OBJ_TREE);
Daniel Barkalow77675e22005-09-05 06:03:51250}