tree.c 6.01 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
		else
			continue;

102
		len = tree_entry_len(&entry);
103 104 105 106 107 108 109 110
		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
int read_tree_recursive(struct tree *tree,
			const char *base, int baselen,
117
			int stage, struct pathspec *pathspec,
118 119 120
			read_tree_fn_t fn, void *context)
{
	struct strbuf sb = STRBUF_INIT;
121
	int ret;
122 123

	strbuf_add(&sb, base, baselen);
124
	ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
125 126 127 128
	strbuf_release(&sb);
	return ret;
}

129 130 131 132 133 134
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_);
135 136
	return cache_name_compare(ce1->name, ce1->ce_flags,
				  ce2->name, ce2->ce_flags);
137 138
}

139
int read_tree(struct tree *tree, int stage, struct pathspec *match)
140
{
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	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;
167
	err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
168 169 170 171 172 173 174 175 176 177
	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;
178 179
}

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

195 196
int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
{
197 198 199
	if (item->object.parsed)
		return 0;
	item->object.parsed = 1;
200 201 202
	item->buffer = buffer;
	item->size = size;

203 204 205
	return 0;
}

206 207
int parse_tree(struct tree *item)
{
208
	 enum object_type type;
209 210 211 212 213
	 void *buffer;
	 unsigned long size;

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

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