blob: 62fed632d8a1ade389dd645b5887155c21ae25ba [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);
Thomas Gummererb60e1882012-07-11 11:22:37 +020025 ce->ce_flags = create_ce_flags(stage);
26 ce->ce_namelen = baselen + len;
Linus Torvalds94537c72005-04-22 16:42:37 -070027 memcpy(ce->name, base, baselen);
28 memcpy(ce->name + baselen, pathname, len+1);
Shawn Pearcee7024962006-08-23 02:49:00 -040029 hashcpy(ce->sha1, sha1);
Junio C Hamanoaf3785d2007-08-09 13:42:50 -070030 return add_cache_entry(ce, opt);
31}
32
René Scharfe671f0702008-07-14 21:22:12 +020033static 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 -070034{
35 return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
36 ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
37}
38
39/*
40 * This is used when the caller knows there is no existing entries at
41 * the stage that will conflict with the entry being added.
42 */
René Scharfe671f0702008-07-14 21:22:12 +020043static 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 -070044{
45 return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
46 ADD_CACHE_JUST_APPEND);
Linus Torvalds94537c72005-04-22 16:42:37 -070047}
48
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070049static int read_tree_1(struct tree *tree, struct strbuf *base,
50 int stage, struct pathspec *pathspec,
51 read_tree_fn_t fn, void *context)
Linus Torvalds0ca14a52005-07-14 11:26:31 -070052{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070053 struct tree_desc desc;
54 struct name_entry entry;
55 unsigned char sha1[20];
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110056 int len, oldlen = base->len;
57 enum interesting retval = entry_not_interesting;
Linus Torvalds0ca14a52005-07-14 11:26:31 -070058
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070059 if (parse_tree(tree))
60 return -1;
Linus Torvalds0ca14a52005-07-14 11:26:31 -070061
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070062 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds0ca14a52005-07-14 11:26:31 -070063
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070064 while (tree_entry(&desc, &entry)) {
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110065 if (retval != all_entries_interesting) {
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070066 retval = tree_entry_interesting(&entry, base, 0, pathspec);
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110067 if (retval == all_entries_not_interesting)
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070068 break;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +110069 if (retval == entry_not_interesting)
Linus Torvalds0ca14a52005-07-14 11:26:31 -070070 continue;
71 }
72
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +070073 switch (fn(entry.sha1, base->buf, base->len,
74 entry.path, entry.mode, stage, context)) {
75 case 0:
76 continue;
77 case READ_TREE_RECURSIVE:
78 break;
79 default:
80 return -1;
81 }
82
83 if (S_ISDIR(entry.mode))
84 hashcpy(sha1, entry.sha1);
85 else if (S_ISGITLINK(entry.mode)) {
86 struct commit *commit;
87
88 commit = lookup_commit(entry.sha1);
89 if (!commit)
90 die("Commit %s in submodule path %s%s not found",
91 sha1_to_hex(entry.sha1),
92 base->buf, entry.path);
93
94 if (parse_commit(commit))
95 die("Invalid commit %s in submodule path %s%s",
96 sha1_to_hex(entry.sha1),
97 base->buf, entry.path);
98
99 hashcpy(sha1, commit->tree->object.sha1);
100 }
101 else
Linus Torvalds0ca14a52005-07-14 11:26:31 -0700102 continue;
Linus Torvalds3e587632005-07-14 11:39:27 -0700103
Nguyễn Thái Ngọc Duy0de16332011-10-24 17:36:09 +1100104 len = tree_entry_len(&entry);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700105 strbuf_add(base, entry.path, len);
106 strbuf_addch(base, '/');
107 retval = read_tree_1(lookup_tree(sha1),
108 base, stage, pathspec,
109 fn, context);
110 strbuf_setlen(base, oldlen);
111 if (retval)
112 return -1;
Linus Torvalds0ca14a52005-07-14 11:26:31 -0700113 }
114 return 0;
115}
116
Daniel Barkalow521698b2006-01-26 01:13:36 -0500117int read_tree_recursive(struct tree *tree,
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800118 const char *base, int baselen,
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700119 int stage, struct pathspec *pathspec,
René Scharfe671f0702008-07-14 21:22:12 +0200120 read_tree_fn_t fn, void *context)
Linus Torvalds94537c72005-04-22 16:42:37 -0700121{
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700122 struct strbuf sb = STRBUF_INIT;
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700123 int ret;
Linus Torvalds0790a422006-05-29 12:17:28 -0700124
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700125 strbuf_add(&sb, base, baselen);
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700126 ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700127 strbuf_release(&sb);
Nguyễn Thái Ngọc Duyffd31f62011-03-25 16:34:18 +0700128 return ret;
Linus Torvalds94537c72005-04-22 16:42:37 -0700129}
130
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700131static int cmp_cache_name_compare(const void *a_, const void *b_)
132{
133 const struct cache_entry *ce1, *ce2;
134
135 ce1 = *((const struct cache_entry **)a_);
136 ce2 = *((const struct cache_entry **)b_);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200137 return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
138 ce2->name, ce2->ce_namelen, ce_stage(ce2));
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700139}
140
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700141int read_tree(struct tree *tree, int stage, struct pathspec *match)
Linus Torvalds94537c72005-04-22 16:42:37 -0700142{
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700143 read_tree_fn_t fn = NULL;
144 int i, err;
145
146 /*
147 * Currently the only existing callers of this function all
148 * call it with stage=1 and after making sure there is nothing
149 * at that stage; we could always use read_one_entry_quick().
150 *
151 * But when we decide to straighten out git-read-tree not to
152 * use unpack_trees() in some cases, this will probably start
153 * to matter.
154 */
155
156 /*
157 * See if we have cache entry at the stage. If so,
158 * do it the original slow way, otherwise, append and then
159 * sort at the end.
160 */
161 for (i = 0; !fn && i < active_nr; i++) {
162 struct cache_entry *ce = active_cache[i];
163 if (ce_stage(ce) == stage)
164 fn = read_one_entry;
165 }
166
167 if (!fn)
168 fn = read_one_entry_quick;
René Scharfe671f0702008-07-14 21:22:12 +0200169 err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700170 if (fn == read_one_entry || err)
171 return err;
172
173 /*
174 * Sort the cache entry -- we need to nuke the cache tree, though.
175 */
176 cache_tree_free(&active_cache_tree);
177 qsort(active_cache, active_nr, sizeof(active_cache[0]),
178 cmp_cache_name_compare);
179 return 0;
Linus Torvalds94537c72005-04-22 16:42:37 -0700180}
181
Jason McMullan5d6ccf52005-06-03 11:05:39 -0400182struct tree *lookup_tree(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700183{
184 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700185 if (!obj)
186 return create_object(sha1, OBJ_TREE, alloc_tree_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -0400187 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -0700188 obj->type = OBJ_TREE;
189 if (obj->type != OBJ_TREE) {
Linus Torvalds885a86a2006-06-14 16:45:13 -0700190 error("Object %s is a %s, not a tree",
191 sha1_to_hex(sha1), typename(obj->type));
Daniel Barkalow175785e2005-04-18 11:39:48 -0700192 return NULL;
193 }
194 return (struct tree *) obj;
195}
196
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400197int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700198{
Daniel Barkalow175785e2005-04-18 11:39:48 -0700199 if (item->object.parsed)
200 return 0;
201 item->object.parsed = 1;
Linus Torvalds136f2e52006-05-29 12:16:12 -0700202 item->buffer = buffer;
203 item->size = size;
204
Linus Torvalds2d9c58c2006-05-29 12:18:33 -0700205 return 0;
206}
Linus Torvalds136f2e52006-05-29 12:16:12 -0700207
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400208int parse_tree(struct tree *item)
209{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500210 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400211 void *buffer;
212 unsigned long size;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400213
214 if (item->object.parsed)
215 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500216 buffer = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400217 if (!buffer)
218 return error("Could not read %s",
219 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500220 if (type != OBJ_TREE) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400221 free(buffer);
222 return error("Object %s not a tree",
223 sha1_to_hex(item->object.sha1));
224 }
Linus Torvalds136f2e52006-05-29 12:16:12 -0700225 return parse_tree_buffer(item, buffer, size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400226}
Daniel Barkalow77675e22005-09-05 02:03:51 -0400227
228struct tree *parse_tree_indirect(const unsigned char *sha1)
229{
230 struct object *obj = parse_object(sha1);
231 do {
232 if (!obj)
233 return NULL;
Linus Torvalds19746322006-07-11 20:45:31 -0700234 if (obj->type == OBJ_TREE)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400235 return (struct tree *) obj;
Linus Torvalds19746322006-07-11 20:45:31 -0700236 else if (obj->type == OBJ_COMMIT)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400237 obj = &(((struct commit *) obj)->tree->object);
Linus Torvalds19746322006-07-11 20:45:31 -0700238 else if (obj->type == OBJ_TAG)
Daniel Barkalow77675e22005-09-05 02:03:51 -0400239 obj = ((struct tag *) obj)->tagged;
240 else
241 return NULL;
242 if (!obj->parsed)
243 parse_object(obj->sha1);
244 } while (1);
245}