blob: 676e9f710ca8d5a568e0c6ea2fa88132da81b48c [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"
4#include "blob.h"
Daniel Barkalow77675e22005-09-05 02:03:51 -04005#include "commit.h"
6#include "tag.h"
Linus Torvalds136f2e52006-05-29 12:16:12 -07007#include "tree-walk.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07008
9const char *tree_type = "tree";
10
Junio C Hamanoaf3785d2007-08-09 13:42:50 -070011static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt)
Linus Torvalds94537c72005-04-22 16:42:37 -070012{
Linus Torvalds3c5e8462005-11-26 09:38:20 -080013 int len;
14 unsigned int size;
15 struct cache_entry *ce;
16
17 if (S_ISDIR(mode))
18 return READ_TREE_RECURSIVE;
19
20 len = strlen(pathname);
21 size = cache_entry_size(baselen + len);
Peter Eriksen90321c12006-04-03 19:30:46 +010022 ce = xcalloc(1, size);
Linus Torvalds94537c72005-04-22 16:42:37 -070023
24 ce->ce_mode = create_ce_mode(mode);
25 ce->ce_flags = create_ce_flags(baselen + len, stage);
26 memcpy(ce->name, base, baselen);
27 memcpy(ce->name + baselen, pathname, len+1);
Shawn Pearcee7024962006-08-23 02:49:00 -040028 hashcpy(ce->sha1, sha1);
Junio C Hamanoaf3785d2007-08-09 13:42:50 -070029 return add_cache_entry(ce, opt);
30}
31
René Scharfe671f0702008-07-14 21:22:12 +020032static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
Junio C Hamanoaf3785d2007-08-09 13:42:50 -070033{
34 return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
35 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
36}
37
38/*
39 * This is used when the caller knows there is no existing entries at
40 * the stage that will conflict with the entry being added.
41 */
René Scharfe671f0702008-07-14 21:22:12 +020042static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
Junio C Hamanoaf3785d2007-08-09 13:42:50 -070043{
44 return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
45 ADD_CACHE_JUST_APPEND);
Linus Torvalds94537c72005-04-22 16:42:37 -070046}
47
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070048static int read_tree_1(struct tree *tree, struct strbuf *base,
49 int stage, struct pathspec *pathspec,
50 read_tree_fn_t fn, void *context)
Linus Torvalds0ca14a52005-07-14 11:26:31 -070051{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070052 struct tree_desc desc;
53 struct name_entry entry;
54 unsigned char sha1[20];
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110055 int len, oldlen = base->len;
56 enum interesting retval = entry_not_interesting;
Linus Torvalds0ca14a52005-07-14 11:26:31 -070057
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070058 if (parse_tree(tree))
59 return -1;
Linus Torvalds0ca14a52005-07-14 11:26:31 -070060
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070061 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds0ca14a52005-07-14 11:26:31 -070062
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070063 while (tree_entry(&desc, &entry)) {
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110064 if (retval != all_entries_interesting) {
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070065 retval = tree_entry_interesting(&entry, base, 0, pathspec);
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110066 if (retval == all_entries_not_interesting)
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070067 break;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110068 if (retval == entry_not_interesting)
Linus Torvalds0ca14a52005-07-14 11:26:31 -070069 continue;
70 }
71
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070072 switch (fn(entry.sha1, base->buf, base->len,
73 entry.path, entry.mode, stage, context)) {
74 case 0:
75 continue;
76 case READ_TREE_RECURSIVE:
77 break;
78 default:
79 return -1;
80 }
81
82 if (S_ISDIR(entry.mode))
83 hashcpy(sha1, entry.sha1);
84 else if (S_ISGITLINK(entry.mode)) {
85 struct commit *commit;
86
87 commit = lookup_commit(entry.sha1);
88 if (!commit)
89 die("Commit %s in submodule path %s%s not found",
90 sha1_to_hex(entry.sha1),
91 base->buf, entry.path);
92
93 if (parse_commit(commit))
94 die("Invalid commit %s in submodule path %s%s",
95 sha1_to_hex(entry.sha1),
96 base->buf, entry.path);
97
98 hashcpy(sha1, commit->tree->object.sha1);
99 }
100 else
Linus Torvalds0ca14a52005-07-14 11:26:31 -0700101 continue;
Linus Torvalds3e587632005-07-14 11:39:27 -0700102
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100103 len = tree_entry_len(&entry);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700104 strbuf_add(base, entry.path, len);
105 strbuf_addch(base, '/');
106 retval = read_tree_1(lookup_tree(sha1),
107 base, stage, pathspec,
108 fn, context);
109 strbuf_setlen(base, oldlen);
110 if (retval)
111 return -1;
Linus Torvalds0ca14a52005-07-14 11:26:31 -0700112 }
113 return 0;
114}
115
Daniel Barkalow521698b2006-01-26 01:13:36 -0500116int read_tree_recursive(struct tree *tree,
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800117 const char *base, int baselen,
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700118 int stage, struct pathspec *pathspec,
René Scharfe671f0702008-07-14 21:22:12 +0200119 read_tree_fn_t fn, void *context)
Linus Torvalds94537c72005-04-22 16:42:37 -0700120{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700121 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700122 int ret;
Linus Torvalds0790a422006-05-29 12:17:28 -0700123
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700124 strbuf_add(&sb, base, baselen);
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700125 ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700126 strbuf_release(&sb);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700127 return ret;
Linus Torvalds94537c72005-04-22 16:42:37 -0700128}
129
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700130static int cmp_cache_name_compare(const void *a_, const void *b_)
131{
132 const struct cache_entry *ce1, *ce2;
133
134 ce1 = *((const struct cache_entry **)a_);
135 ce2 = *((const struct cache_entry **)b_);
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800136 return cache_name_compare(ce1->name, ce1->ce_flags,
137 ce2->name, ce2->ce_flags);
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700138}
139
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700140int read_tree(struct tree *tree, int stage, struct pathspec *match)
Linus Torvalds94537c72005-04-22 16:42:37 -0700141{
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700142 read_tree_fn_t fn = NULL;
143 int i, err;
144
145 /*
146 * Currently the only existing callers of this function all
147 * call it with stage=1 and after making sure there is nothing
148 * at that stage; we could always use read_one_entry_quick().
149 *
150 * But when we decide to straighten out git-read-tree not to
151 * use unpack_trees() in some cases, this will probably start
152 * to matter.
153 */
154
155 /*
156 * See if we have cache entry at the stage. If so,
157 * do it the original slow way, otherwise, append and then
158 * sort at the end.
159 */
160 for (i = 0; !fn && i < active_nr; i++) {
161 struct cache_entry *ce = active_cache[i];
162 if (ce_stage(ce) == stage)
163 fn = read_one_entry;
164 }
165
166 if (!fn)
167 fn = read_one_entry_quick;
René Scharfe671f0702008-07-14 21:22:12 +0200168 err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700169 if (fn == read_one_entry || err)
170 return err;
171
172 /*
173 * Sort the cache entry -- we need to nuke the cache tree, though.
174 */
175 cache_tree_free(&active_cache_tree);
176 qsort(active_cache, active_nr, sizeof(active_cache[0]),
177 cmp_cache_name_compare);
178 return 0;
Linus Torvalds94537c72005-04-22 16:42:37 -0700179}
180
Jason McMullan5d6ccf52005-06-03 11:05:39 -0400181struct tree *lookup_tree(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700182{
183 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700184 if (!obj)
185 return create_object(sha1, OBJ_TREE, alloc_tree_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -0400186 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -0700187 obj->type = OBJ_TREE;
188 if (obj->type != OBJ_TREE) {
Linus Torvalds885a86a2006-06-14 16:45:13 -0700189 error("Object %s is a %s, not a tree",
190 sha1_to_hex(sha1), typename(obj->type));
Daniel Barkalow175785e2005-04-18 11:39:48 -0700191 return NULL;
192 }
193 return (struct tree *) obj;
194}
195
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400196int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700197{
Daniel Barkalow175785e2005-04-18 11:39:48 -0700198 if (item->object.parsed)
199 return 0;
200 item->object.parsed = 1;
Linus Torvalds136f2e52006-05-29 12:16:12 -0700201 item->buffer = buffer;
202 item->size = size;
203
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700204 return 0;
205}
Linus Torvalds136f2e52006-05-29 12:16:12 -0700206
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400207int parse_tree(struct tree *item)
208{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500209 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400210 void *buffer;
211 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400212
213 if (item->object.parsed)
214 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500215 buffer = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400216 if (!buffer)
217 return error("Could not read %s",
218 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500219 if (type != OBJ_TREE) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400220 free(buffer);
221 return error("Object %s not a tree",
222 sha1_to_hex(item->object.sha1));
223 }
Linus Torvalds136f2e52006-05-29 12:16:12 -0700224 return parse_tree_buffer(item, buffer, size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400225}
Daniel Barkalow77675e22005-09-05 02:03:51 -0400226
227struct tree *parse_tree_indirect(const unsigned char *sha1)
228{
229 struct object *obj = parse_object(sha1);
230 do {
231 if (!obj)
232 return NULL;
Linus Torvalds19746322006-07-11 20:45:31 -0700233 if (obj->type == OBJ_TREE)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400234 return (struct tree *) obj;
Linus Torvalds19746322006-07-11 20:45:31 -0700235 else if (obj->type == OBJ_COMMIT)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400236 obj = &(((struct commit *) obj)->tree->object);
Linus Torvalds19746322006-07-11 20:45:31 -0700237 else if (obj->type == OBJ_TAG)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400238 obj = ((struct tag *) obj)->tagged;
239 else
240 return NULL;
241 if (!obj->parsed)
242 parse_object(obj->sha1);
243 } while (1);
244}