tree.c 6.19 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

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

33 34 35
static int read_one_entry(const unsigned char *sha1, struct strbuf *base,
			  const char *pathname, unsigned mode, int stage,
			  void *context)
36
{
37 38
	return read_one_entry_opt(sha1, base->buf, base->len, pathname,
				  mode, stage,
39 40 41 42 43 44 45
				  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.
 */
46 47 48
static int read_one_entry_quick(const unsigned char *sha1, struct strbuf *base,
				const char *pathname, unsigned mode, int stage,
				void *context)
49
{
50 51
	return read_one_entry_opt(sha1, base->buf, base->len, pathname,
				  mode, stage,
52
				  ADD_CACHE_JUST_APPEND);
53 54
}

55
static int read_tree_1(struct tree *tree, struct strbuf *base,
56
		       int stage, const struct pathspec *pathspec,
57
		       read_tree_fn_t fn, void *context)
58
{
59
	struct tree_desc desc;
60
	struct name_entry entry;
61
	struct object_id oid;
62 63
	int len, oldlen = base->len;
	enum interesting retval = entry_not_interesting;
64

65 66
	if (parse_tree(tree))
		return -1;
67

68
	init_tree_desc(&desc, tree->buffer, tree->size);
69

70
	while (tree_entry(&desc, &entry)) {
71
		if (retval != all_entries_interesting) {
72
			retval = tree_entry_interesting(&entry, base, 0, pathspec);
73
			if (retval == all_entries_not_interesting)
74
				break;
75
			if (retval == entry_not_interesting)
76 77
				continue;
		}
78

79
		switch (fn(entry.oid->hash, base,
80
			   entry.path, entry.mode, stage, context)) {
81 82 83
		case 0:
			continue;
		case READ_TREE_RECURSIVE:
Junio C Hamano's avatar
Junio C Hamano committed
84
			break;
85 86 87
		default:
			return -1;
		}
88

89
		if (S_ISDIR(entry.mode))
90
			oidcpy(&oid, entry.oid);
91 92
		else if (S_ISGITLINK(entry.mode)) {
			struct commit *commit;
93

94
			commit = lookup_commit(entry.oid);
95
			if (!commit)
96
				die("Commit %s in submodule path %s%s not found",
97
				    oid_to_hex(entry.oid),
98
				    base->buf, entry.path);
99 100

			if (parse_commit(commit))
101
				die("Invalid commit %s in submodule path %s%s",
102
				    oid_to_hex(entry.oid),
103 104
				    base->buf, entry.path);

105
			oidcpy(&oid, &commit->tree->object.oid);
106
		}
107 108 109
		else
			continue;

110
		len = tree_entry_len(&entry);
111 112
		strbuf_add(base, entry.path, len);
		strbuf_addch(base, '/');
113
		retval = read_tree_1(lookup_tree(&oid),
114 115 116 117 118
				     base, stage, pathspec,
				     fn, context);
		strbuf_setlen(base, oldlen);
		if (retval)
			return -1;
119 120 121 122
	}
	return 0;
}

123 124
int read_tree_recursive(struct tree *tree,
			const char *base, int baselen,
125
			int stage, const struct pathspec *pathspec,
126 127 128
			read_tree_fn_t fn, void *context)
{
	struct strbuf sb = STRBUF_INIT;
129
	int ret;
130 131

	strbuf_add(&sb, base, baselen);
132
	ret = read_tree_1(tree, &sb, stage, pathspec, fn, context);
133 134 135 136
	strbuf_release(&sb);
	return ret;
}

137 138 139 140 141 142
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_);
143 144
	return cache_name_stage_compare(ce1->name, ce1->ce_namelen, ce_stage(ce1),
				  ce2->name, ce2->ce_namelen, ce_stage(ce2));
145 146
}

147
int read_tree(struct tree *tree, int stage, struct pathspec *match)
148
{
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	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++) {
168
		const struct cache_entry *ce = active_cache[i];
169 170 171 172 173 174
		if (ce_stage(ce) == stage)
			fn = read_one_entry;
	}

	if (!fn)
		fn = read_one_entry_quick;
175
	err = read_tree_recursive(tree, "", 0, stage, match, fn, NULL);
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);
René Scharfe's avatar
René Scharfe committed
183
	QSORT(active_cache, active_nr, cmp_cache_name_compare);
184
	return 0;
185 186
}

187
struct tree *lookup_tree(const struct object_id *oid)
188
{
189
	struct object *obj = lookup_object(oid->hash);
190
	if (!obj)
191
		return create_object(oid->hash, alloc_tree_node());
192
	return object_as_type(obj, OBJ_TREE, 0);
193 194
}

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
int parse_tree_gently(struct tree *item, int quiet_on_missing)
207
{
208
	 enum object_type type;
209 210 211 212 213
	 void *buffer;
	 unsigned long size;

	if (item->object.parsed)
		return 0;
brian m. carlson's avatar
brian m. carlson committed
214
	buffer = read_sha1_file(item->object.oid.hash, &type, &size);
215
	if (!buffer)
216 217
		return quiet_on_missing ? -1 :
			error("Could not read %s",
218
			     oid_to_hex(&item->object.oid));
219
	if (type != OBJ_TREE) {
220 221
		free(buffer);
		return error("Object %s not a tree",
222
			     oid_to_hex(&item->object.oid));
223
	}
224
	return parse_tree_buffer(item, buffer, size);
225
}
226

227 228 229 230 231 232 233 234
void free_tree_buffer(struct tree *tree)
{
	free(tree->buffer);
	tree->buffer = NULL;
	tree->size = 0;
	tree->object.parsed = 0;
}

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