tree.c 6.17 KB
Newer Older
1
#include "cache.h"
2
#include "cache-tree.h"
3 4
#include "tree.h"
#include "blob.h"
5 6
#include "commit.h"
#include "tag.h"
7
#include "tree-walk.h"
8 9 10

const char *tree_type = "tree";

11
static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt)
12
{
13 14 15 16 17 18 19 20 21
	int len;
	unsigned int size;
	struct cache_entry *ce;

	if (S_ISDIR(mode))
		return READ_TREE_RECURSIVE;

	len = strlen(pathname);
	size = cache_entry_size(baselen + len);
22
	ce = xcalloc(1, size);
23 24 25 26 27

	ce->ce_mode = create_ce_mode(mode);
	ce->ce_flags = create_ce_flags(baselen + len, stage);
	memcpy(ce->name, base, baselen);
	memcpy(ce->name + baselen, pathname, len+1);
28
	hashcpy(ce->sha1, sha1);
29 30 31
	return add_cache_entry(ce, opt);
}

32
static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
33 34 35 36 37 38 39 40 41
{
	return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
				  ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
}

/*
 * This is used when the caller knows there is no existing entries at
 * the stage that will conflict with the entry being added.
 */
42
static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, void *context)
43 44 45
{
	return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
				  ADD_CACHE_JUST_APPEND);
46 47
}

48 49 50
static int read_tree_1(struct tree *tree, struct strbuf *base,
		       int stage, struct pathspec *pathspec,
		       read_tree_fn_t fn, void *context)
51
{
52
	struct tree_desc desc;
53
	struct name_entry entry;
54 55
	unsigned char sha1[20];
	int len, retval = 0, oldlen = base->len;
56

57 58
	if (parse_tree(tree))
		return -1;
59

60
	init_tree_desc(&desc, tree->buffer, tree->size);
61

62
	while (tree_entry(&desc, &entry)) {
63 64 65 66 67 68 69
		if (retval != 2) {
			retval = tree_entry_interesting(&entry, base, 0, pathspec);
			if (retval < 0)
				break;
			if (retval == 0)
				continue;
		}
70

71 72
		switch (fn(entry.sha1, base->buf, base->len,
			   entry.path, entry.mode, stage, context)) {
73 74 75
		case 0:
			continue;
		case READ_TREE_RECURSIVE:
Junio C Hamano's avatar
Junio C Hamano committed
76
			break;
77 78 79
		default:
			return -1;
		}
80

81 82 83 84
		if (S_ISDIR(entry.mode))
			hashcpy(sha1, entry.sha1);
		else if (S_ISGITLINK(entry.mode)) {
			struct commit *commit;
85 86 87

			commit = lookup_commit(entry.sha1);
			if (!commit)
88 89 90
				die("Commit %s in submodule path %s%s not found",
				    sha1_to_hex(entry.sha1),
				    base->buf, entry.path);
91 92

			if (parse_commit(commit))
93 94 95 96 97
				die("Invalid commit %s in submodule path %s%s",
				    sha1_to_hex(entry.sha1),
				    base->buf, entry.path);

			hashcpy(sha1, commit->tree->object.sha1);
98
		}
99 100 101 102 103 104 105 106 107 108 109 110
		else
			continue;

		len = tree_entry_len(entry.path, entry.sha1);
		strbuf_add(base, entry.path, len);
		strbuf_addch(base, '/');
		retval = read_tree_1(lookup_tree(sha1),
				     base, stage, pathspec,
				     fn, context);
		strbuf_setlen(base, oldlen);
		if (retval)
			return -1;
111 112 113 114
	}
	return 0;
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
int read_tree_recursive(struct tree *tree,
			const char *base, int baselen,
			int stage, const char **match,
			read_tree_fn_t fn, void *context)
{
	struct strbuf sb = STRBUF_INIT;
	struct pathspec pathspec;
	int i, ret;

	init_pathspec(&pathspec, match);
	for (i = 0; i < pathspec.nr; i++)
		pathspec.items[i].has_wildcard = 0;
	strbuf_add(&sb, base, baselen);
	ret = read_tree_1(tree, &sb, stage, &pathspec, fn, context);
	strbuf_release(&sb);
	free_pathspec(&pathspec);
	return ret;
}

134 135 136 137 138 139
static int cmp_cache_name_compare(const void *a_, const void *b_)
{
	const struct cache_entry *ce1, *ce2;

	ce1 = *((const struct cache_entry **)a_);
	ce2 = *((const struct cache_entry **)b_);
140 141
	return cache_name_compare(ce1->name, ce1->ce_flags,
				  ce2->name, ce2->ce_flags);
142 143
}

144
int read_tree(struct tree *tree, int stage, const char **match)
145
{
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	read_tree_fn_t fn = NULL;
	int i, err;

	/*
	 * Currently the only existing callers of this function all
	 * call it with stage=1 and after making sure there is nothing
	 * at that stage; we could always use read_one_entry_quick().
	 *
	 * But when we decide to straighten out git-read-tree not to
	 * use unpack_trees() in some cases, this will probably start
	 * to matter.
	 */

	/*
	 * See if we have cache entry at the stage.  If so,
	 * do it the original slow way, otherwise, append and then
	 * sort at the end.
	 */
	for (i = 0; !fn && i < active_nr; i++) {
		struct cache_entry *ce = active_cache[i];
		if (ce_stage(ce) == stage)
			fn = read_one_entry;
	}

	if (!fn)
		fn = read_one_entry_quick;
172
	err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
173 174 175 176 177 178 179 180 181 182
	if (fn == read_one_entry || err)
		return err;

	/*
	 * Sort the cache entry -- we need to nuke the cache tree, though.
	 */
	cache_tree_free(&active_cache_tree);
	qsort(active_cache, active_nr, sizeof(active_cache[0]),
	      cmp_cache_name_compare);
	return 0;
183 184
}

185
struct tree *lookup_tree(const unsigned char *sha1)
186 187
{
	struct object *obj = lookup_object(sha1);
188 189
	if (!obj)
		return create_object(sha1, OBJ_TREE, alloc_tree_node());
Nicolas Pitre's avatar
Nicolas Pitre committed
190
	if (!obj->type)
191 192
		obj->type = OBJ_TREE;
	if (obj->type != OBJ_TREE) {
193 194
		error("Object %s is a %s, not a tree",
		      sha1_to_hex(sha1), typename(obj->type));
195 196 197 198 199
		return NULL;
	}
	return (struct tree *) obj;
}

200 201
int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
{
202 203 204
	if (item->object.parsed)
		return 0;
	item->object.parsed = 1;
205 206 207
	item->buffer = buffer;
	item->size = size;

208 209 210
	return 0;
}

211 212
int parse_tree(struct tree *item)
{
213
	 enum object_type type;
214 215 216 217 218
	 void *buffer;
	 unsigned long size;

	if (item->object.parsed)
		return 0;
219
	buffer = read_sha1_file(item->object.sha1, &type, &size);
220 221 222
	if (!buffer)
		return error("Could not read %s",
			     sha1_to_hex(item->object.sha1));
223
	if (type != OBJ_TREE) {
224 225 226 227
		free(buffer);
		return error("Object %s not a tree",
			     sha1_to_hex(item->object.sha1));
	}
228
	return parse_tree_buffer(item, buffer, size);
229
}
230 231 232 233 234 235 236

struct tree *parse_tree_indirect(const unsigned char *sha1)
{
	struct object *obj = parse_object(sha1);
	do {
		if (!obj)
			return NULL;
237
		if (obj->type == OBJ_TREE)
238
			return (struct tree *) obj;
239
		else if (obj->type == OBJ_COMMIT)
240
			obj = &(((struct commit *) obj)->tree->object);
241
		else if (obj->type == OBJ_TAG)
242 243 244 245 246 247 248
			obj = ((struct tag *) obj)->tagged;
		else
			return NULL;
		if (!obj->parsed)
			parse_object(obj->sha1);
	} while (1);
}