🌐 AI搜索 & 代理 主页
blob: b224115e0f4d61368560eba406a04f0259b7c4f0 [file] [log] [blame]
Brandon Williams85ab50f2017-06-12 22:13:571#define NO_THE_INDEX_COMPATIBILITY_MACROS
Junio C Hamano8f1d2e62006-01-07 09:33:542#include "cache.h"
Junio C Hamanoaf3785d2007-08-09 20:42:503#include "cache-tree.h"
Daniel Barkalow175785e2005-04-18 18:39:484#include "tree.h"
5#include "blob.h"
Daniel Barkalow77675e22005-09-05 06:03:516#include "commit.h"
7#include "tag.h"
Linus Torvalds136f2e52006-05-29 19:16:128#include "tree-walk.h"
Daniel Barkalow175785e2005-04-18 18:39:489
10const char *tree_type = "tree";
11
Brandon Williams85ab50f2017-06-12 22:13:5712static 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 23:42:3717{
Linus Torvalds3c5e8462005-11-26 17:38:2018 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 18:30:4627 ce = xcalloc(1, size);
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. carlson99d1a982016-09-05 20:07:5234 hashcpy(ce->oid.hash, sha1);
Brandon Williams85ab50f2017-06-12 22:13:5735 return add_index_entry(istate, ce, opt);
Junio C Hamanoaf3785d2007-08-09 20:42:5036}
37
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 09:05:0038static 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 20:42:5041{
Brandon Williams85ab50f2017-06-12 22:13:5742 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 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 */
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 09:05:0052static 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 20:42:5055{
Brandon Williams85ab50f2017-06-12 22:13:5756 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 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 Duyffd31f62011-03-25 09:34:1862static int read_tree_1(struct tree *tree, struct strbuf *base,
Nguyễn Thái Ngọc Duy18e4f402013-07-14 08:35:5263 int stage, const struct pathspec *pathspec,
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1864 read_tree_fn_t fn, void *context)
Linus Torvalds0ca14a52005-07-14 18:26:3165{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1866 struct tree_desc desc;
67 struct name_entry entry;
brian m. carlsonf26efc52017-05-06 22:10:1568 struct object_id oid;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 06:36:1069 int len, oldlen = base->len;
70 enum interesting retval = entry_not_interesting;
Linus Torvalds0ca14a52005-07-14 18:26:3171
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1872 if (parse_tree(tree))
73 return -1;
Linus Torvalds0ca14a52005-07-14 18:26:3174
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1875 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds0ca14a52005-07-14 18:26:3176
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1877 while (tree_entry(&desc, &entry)) {
Nguyễn Thái Ngọc Duyd688cf02011-10-24 06:36:1078 if (retval != all_entries_interesting) {
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1879 retval = tree_entry_interesting(&entry, base, 0, pathspec);
Nguyễn Thái Ngọc Duyd688cf02011-10-24 06:36:1080 if (retval == all_entries_not_interesting)
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1881 break;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 06:36:1082 if (retval == entry_not_interesting)
Linus Torvalds0ca14a52005-07-14 18:26:3183 continue;
84 }
85
brian m. carlson7d924c92016-04-17 23:10:3986 switch (fn(entry.oid->hash, base,
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1887 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:1597 oidcpy(&oid, entry.oid);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:1898 else if (S_ISGITLINK(entry.mode)) {
99 struct commit *commit;
100
brian m. carlsonbc832662017-05-06 22:10:10101 commit = lookup_commit(entry.oid);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18102 if (!commit)
103 die("Commit %s in submodule path %s%s not found",
brian m. carlson7d924c92016-04-17 23:10:39104 oid_to_hex(entry.oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18105 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:39109 oid_to_hex(entry.oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18110 base->buf, entry.path);
111
brian m. carlsonf26efc52017-05-06 22:10:15112 oidcpy(&oid, &commit->tree->object.oid);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18113 }
114 else
Linus Torvalds0ca14a52005-07-14 18:26:31115 continue;
Linus Torvalds3e587632005-07-14 18:39:27116
Nguyễn Thái Ngọc Duy0de16332011-10-24 06:36:09117 len = tree_entry_len(&entry);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18118 strbuf_add(base, entry.path, len);
119 strbuf_addch(base, '/');
brian m. carlson740ee052017-05-06 22:10:17120 retval = read_tree_1(lookup_tree(&oid),
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18121 base, stage, pathspec,
122 fn, context);
123 strbuf_setlen(base, oldlen);
124 if (retval)
125 return -1;
Linus Torvalds0ca14a52005-07-14 18:26:31126 }
127 return 0;
128}
129
Daniel Barkalow521698b2006-01-26 06:13:36130int read_tree_recursive(struct tree *tree,
Linus Torvalds3c5e8462005-11-26 17:38:20131 const char *base, int baselen,
Nguyễn Thái Ngọc Duy18e4f402013-07-14 08:35:52132 int stage, const struct pathspec *pathspec,
René Scharfe671f0702008-07-14 19:22:12133 read_tree_fn_t fn, void *context)
Linus Torvalds94537c72005-04-22 23:42:37134{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18135 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyf0096c02011-03-25 09:34:19136 int ret;
Linus Torvalds0790a422006-05-29 19:17:28137
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18138 strbuf_add(&sb, base, baselen);
Nguyễn Thái Ngọc Duyf0096c02011-03-25 09:34:19139 ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18140 strbuf_release(&sb);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 09:34:18141 return ret;
Linus Torvalds94537c72005-04-22 23:42:37142}
143
Junio C Hamanoaf3785d2007-08-09 20:42:50144static 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 09:22:37150 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 20:42:50152}
153
Brandon Williams85ab50f2017-06-12 22:13:57154int read_tree(struct tree *tree, int stage, struct pathspec *match,
155 struct index_state *istate)
Linus Torvalds94537c72005-04-22 23:42:37156{
Junio C Hamanoaf3785d2007-08-09 20:42:50157 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 22:13:57175 for (i = 0; !fn && i < istate->cache_nr; i++) {
176 const struct cache_entry *ce = istate->cache[i];
Junio C Hamanoaf3785d2007-08-09 20:42:50177 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 22:13:57183 err = read_tree_recursive(tree, "", 0, stage, match, fn, istate);
Junio C Hamanoaf3785d2007-08-09 20:42:50184 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 22:13:57190 cache_tree_free(&istate->cache_tree);
191 QSORT(istate->cache, istate->cache_nr, cmp_cache_name_compare);
Junio C Hamanoaf3785d2007-08-09 20:42:50192 return 0;
Linus Torvalds94537c72005-04-22 23:42:37193}
194
brian m. carlson740ee052017-05-06 22:10:17195struct tree *lookup_tree(const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 18:39:48196{
brian m. carlson740ee052017-05-06 22:10:17197 struct object *obj = lookup_object(oid->hash);
Linus Torvalds100c5f32007-04-17 05:11:43198 if (!obj)
brian m. carlson740ee052017-05-06 22:10:17199 return create_object(oid->hash, alloc_tree_node());
Jeff King8ff226a2014-07-13 06:42:03200 return object_as_type(obj, OBJ_TREE, 0);
Daniel Barkalow175785e2005-04-18 18:39:48201}
202
Nicolas Pitrebd2c39f2005-05-06 17:48:34203int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 18:39:48204{
Daniel Barkalow175785e2005-04-18 18:39:48205 if (item->object.parsed)
206 return 0;
207 item->object.parsed = 1;
Linus Torvalds136f2e52006-05-29 19:16:12208 item->buffer = buffer;
209 item->size = size;
210
Linus Torvalds2d9c58c2006-05-29 19:18:33211 return 0;
212}
Linus Torvalds136f2e52006-05-29 19:16:12213
Jeff King9cc2b072015-06-01 09:56:26214int parse_tree_gently(struct tree *item, int quiet_on_missing)
Nicolas Pitrebd2c39f2005-05-06 17:48:34215{
Nicolas Pitre21666f12007-02-26 19:55:59216 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 17:48:34217 void *buffer;
218 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 17:48:34219
220 if (item->object.parsed)
221 return 0;
brian m. carlsoned1c9972015-11-10 02:22:29222 buffer = read_sha1_file(item->object.oid.hash, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34223 if (!buffer)
Jeff King9cc2b072015-06-01 09:56:26224 return quiet_on_missing ? -1 :
225 error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28226 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 19:55:59227 if (type != OBJ_TREE) {
Nicolas Pitrebd2c39f2005-05-06 17:48:34228 free(buffer);
229 return error("Object %s not a tree",
brian m. carlsonf2fd0762015-11-10 02:22:28230 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 17:48:34231 }
Linus Torvalds136f2e52006-05-29 19:16:12232 return parse_tree_buffer(item, buffer, size);
Nicolas Pitrebd2c39f2005-05-06 17:48:34233}
Daniel Barkalow77675e22005-09-05 06:03:51234
Jeff King6e454b92013-06-05 22:37:39235void free_tree_buffer(struct tree *tree)
236{
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46237 FREE_AND_NULL(tree->buffer);
Jeff King6e454b92013-06-05 22:37:39238 tree->size = 0;
239 tree->object.parsed = 0;
240}
241
brian m. carlsona9dbc172017-05-06 22:10:37242struct tree *parse_tree_indirect(const struct object_id *oid)
Daniel Barkalow77675e22005-09-05 06:03:51243{
brian m. carlsonc251c832017-05-06 22:10:38244 struct object *obj = parse_object(oid);
Daniel Barkalow77675e22005-09-05 06:03:51245 do {
246 if (!obj)
247 return NULL;
Linus Torvalds19746322006-07-12 03:45:31248 if (obj->type == OBJ_TREE)
Daniel Barkalow77675e22005-09-05 06:03:51249 return (struct tree *) obj;
Linus Torvalds19746322006-07-12 03:45:31250 else if (obj->type == OBJ_COMMIT)
Daniel Barkalow77675e22005-09-05 06:03:51251 obj = &(((struct commit *) obj)->tree->object);
Linus Torvalds19746322006-07-12 03:45:31252 else if (obj->type == OBJ_TAG)
Daniel Barkalow77675e22005-09-05 06:03:51253 obj = ((struct tag *) obj)->tagged;
254 else
255 return NULL;
256 if (!obj->parsed)
brian m. carlsonc251c832017-05-06 22:10:38257 parse_object(&obj->oid);
Daniel Barkalow77675e22005-09-05 06:03:51258 } while (1);
259}