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

const char *tree_type = "tree";

11
static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
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 28

	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);
	memcpy(ce->sha1, sha1, 20);
29
	return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
30 31
}

32
static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
33
{
34
	const char *match;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	int pathlen;

	if (!paths)
		return 1;
	pathlen = strlen(path);
	while ((match = *paths++) != NULL) {
		int matchlen = strlen(match);

		if (baselen >= matchlen) {
			/* If it doesn't match, move along... */
			if (strncmp(base, match, matchlen))
				continue;
			/* The base is a subdirectory of a path which was specified. */
			return 1;
		}

		/* Does the base match? */
		if (strncmp(base, match, baselen))
			continue;

		match += baselen;
		matchlen -= baselen;

		if (pathlen > matchlen)
			continue;

		if (matchlen > pathlen) {
			if (match[pathlen] != '/')
				continue;
			if (!S_ISDIR(mode))
				continue;
		}

		if (strncmp(path, match, pathlen))
			continue;
70 71

		return 1;
72 73 74 75
	}
	return 0;
}

76
int read_tree_recursive(struct tree *tree,
77 78 79
			const char *base, int baselen,
			int stage, const char **match,
			read_tree_fn_t fn)
80
{
81 82
	struct tree_desc desc;

83 84
	if (parse_tree(tree))
		return -1;
85 86 87 88 89 90 91 92 93 94 95 96 97

	desc.buf = tree->buffer;
	desc.size = tree->size;

	while (desc.size) {
		unsigned mode;
		const char *name;
		const unsigned char *sha1;

		sha1 = tree_entry_extract(&desc, &name, &mode);
		update_tree_entry(&desc);

		if (!match_tree_entry(base, baselen, name, mode, match))
98 99
			continue;

100
		switch (fn(sha1, base, baselen, name, mode, stage)) {
101 102 103 104 105 106 107
		case 0:
			continue;
		case READ_TREE_RECURSIVE:
			break;;
		default:
			return -1;
		}
108
		if (S_ISDIR(mode)) {
109
			int retval;
110
			int pathlen = strlen(name);
111
			char *newbase;
112

113
			newbase = xmalloc(baselen + 1 + pathlen);
114
			memcpy(newbase, base, baselen);
115
			memcpy(newbase + baselen, name, pathlen);
116
			newbase[baselen + pathlen] = '/';
117
			retval = read_tree_recursive(lookup_tree(sha1),
118
						     newbase,
119
						     baselen + pathlen + 1,
120
						     stage, match, fn);
121 122 123 124 125 126 127 128 129
			free(newbase);
			if (retval)
				return -1;
			continue;
		}
	}
	return 0;
}

130
int read_tree(struct tree *tree, int stage, const char **match)
131
{
132
	return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
133 134
}

135
struct tree *lookup_tree(const unsigned char *sha1)
136 137 138
{
	struct object *obj = lookup_object(sha1);
	if (!obj) {
139
		struct tree *ret = xcalloc(1, sizeof(struct tree));
140
		created_object(sha1, &ret->object);
141
		ret->object.type = tree_type;
142 143
		return ret;
	}
Nicolas Pitre's avatar
Nicolas Pitre committed
144 145
	if (!obj->type)
		obj->type = tree_type;
146
	if (obj->type != tree_type) {
147 148 149 150 151 152 153
		error("Object %s is a %s, not a tree", 
		      sha1_to_hex(sha1), obj->type);
		return NULL;
	}
	return (struct tree *) obj;
}

154
int parse_tree_buffer(struct tree *item, void *buffer, unsigned long size)
155
{
156
	struct tree_desc desc;
157
	struct tree_entry_list **list_p;
158
	int n_refs = 0;
159

160 161 162
	if (item->object.parsed)
		return 0;
	item->object.parsed = 1;
163 164 165 166 167 168
	item->buffer = buffer;
	item->size = size;

	desc.buf = buffer;
	desc.size = size;

169
	list_p = &item->entries;
170 171 172 173
	while (desc.size) {
		unsigned mode;
		const char *path;
		const unsigned char *sha1;
174
		struct tree_entry_list *entry;
175 176

		sha1 = tree_entry_extract(&desc, &path, &mode);
177

178
		entry = xmalloc(sizeof(struct tree_entry_list));
179
		entry->name = path;
180
		entry->sha1 = sha1;
181
		entry->mode = mode;
182 183 184
		entry->directory = S_ISDIR(mode) != 0;
		entry->executable = (mode & S_IXUSR) != 0;
		entry->symlink = S_ISLNK(mode) != 0;
185
		entry->zeropad = *(const char *)(desc.buf) == '0';
186 187
		entry->next = NULL;

188 189
		update_tree_entry(&desc);
		n_refs++;
190 191
		*list_p = entry;
		list_p = &entry->next;
192
	}
193 194 195 196 197

	if (track_object_refs) {
		struct tree_entry_list *entry;
		unsigned i = 0;
		struct object_refs *refs = alloc_object_refs(n_refs);
198 199 200 201 202 203 204 205 206 207
		for (entry = item->entries; entry; entry = entry->next) {
			struct object *obj;

			if (entry->directory)
				obj = &lookup_tree(entry->sha1)->object;
			else
				obj = &lookup_blob(entry->sha1)->object;
			refs->ref[i++] = obj;
		}

208 209 210
		set_object_refs(&item->object, refs);
	}

211 212
	return 0;
}
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

int parse_tree(struct tree *item)
{
	 char type[20];
	 void *buffer;
	 unsigned long size;

	if (item->object.parsed)
		return 0;
	buffer = read_sha1_file(item->object.sha1, type, &size);
	if (!buffer)
		return error("Could not read %s",
			     sha1_to_hex(item->object.sha1));
	if (strcmp(type, tree_type)) {
		free(buffer);
		return error("Object %s not a tree",
			     sha1_to_hex(item->object.sha1));
	}
231
	return parse_tree_buffer(item, buffer, size);
232
}
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

struct tree *parse_tree_indirect(const unsigned char *sha1)
{
	struct object *obj = parse_object(sha1);
	do {
		if (!obj)
			return NULL;
		if (obj->type == tree_type)
			return (struct tree *) obj;
		else if (obj->type == commit_type)
			obj = &(((struct commit *) obj)->tree->object);
		else if (obj->type == tag_type)
			obj = ((struct tag *) obj)->tagged;
		else
			return NULL;
		if (!obj->parsed)
			parse_object(obj->sha1);
	} while (1);
}