match-trees.c 8.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include "cache.h"
#include "tree.h"
#include "tree-walk.h"

static int score_missing(unsigned mode, const char *path)
{
	int score;

	if (S_ISDIR(mode))
		score = -1000;
	else if (S_ISLNK(mode))
		score = -500;
	else
		score = -50;
	return score;
}

static int score_differs(unsigned mode1, unsigned mode2, const char *path)
{
	int score;

	if (S_ISDIR(mode1) != S_ISDIR(mode2))
		score = -100;
	else if (S_ISLNK(mode1) != S_ISLNK(mode2))
		score = -50;
	else
		score = -5;
	return score;
}

static int score_matches(unsigned mode1, unsigned mode2, const char *path)
{
	int score;

	/* Heh, we found SHA-1 collisions between different kind of objects */
	if (S_ISDIR(mode1) != S_ISDIR(mode2))
		score = -100;
	else if (S_ISLNK(mode1) != S_ISLNK(mode2))
		score = -50;

	else if (S_ISDIR(mode1))
		score = 1000;
	else if (S_ISLNK(mode1))
		score = 500;
	else
		score = 250;
	return score;
}

50
static void *fill_tree_desc_strict(struct tree_desc *desc,
51
				   const struct object_id *hash)
52 53 54 55 56
{
	void *buffer;
	enum object_type type;
	unsigned long size;

57
	buffer = read_sha1_file(hash->hash, &type, &size);
58
	if (!buffer)
59
		die("unable to read tree (%s)", oid_to_hex(hash));
60
	if (type != OBJ_TREE)
61
		die("%s is not a tree", oid_to_hex(hash));
62 63 64 65
	init_tree_desc(desc, buffer, size);
	return buffer;
}

66 67 68 69 70 71 72
static int base_name_entries_compare(const struct name_entry *a,
				     const struct name_entry *b)
{
	return base_name_compare(a->path, tree_entry_len(a), a->mode,
				 b->path, tree_entry_len(b), b->mode);
}

73 74 75
/*
 * Inspect two trees, and give a score that tells how similar they are.
 */
76
static int score_trees(const struct object_id *hash1, const struct object_id *hash2)
77 78 79
{
	struct tree_desc one;
	struct tree_desc two;
80 81
	void *one_buf = fill_tree_desc_strict(&one, hash1);
	void *two_buf = fill_tree_desc_strict(&two, hash2);
82 83
	int score = 0;

84 85 86 87
	for (;;) {
		struct name_entry e1, e2;
		int got_entry_from_one = tree_entry(&one, &e1);
		int got_entry_from_two = tree_entry(&two, &e2);
88 89
		int cmp;

90 91 92
		if (got_entry_from_one && got_entry_from_two)
			cmp = base_name_entries_compare(&e1, &e2);
		else if (got_entry_from_one)
93
			/* two lacks this entry */
94 95 96 97 98 99 100 101
			cmp = -1;
		else if (got_entry_from_two)
			/* two has more entries */
			cmp = 1;
		else
			break;

		if (cmp < 0)
102
			/* path1 does not appear in two */
103 104
			score += score_missing(e1.mode, e1.path);
		else if (cmp > 0)
105
			/* path2 does not appear in one */
106
			score += score_missing(e2.mode, e2.path);
107
		else if (oidcmp(e1.oid, e2.oid))
108
			/* they are different */
109
			score += score_differs(e1.mode, e2.mode, e1.path);
110 111
		else
			/* same subtree or blob */
112
			score += score_matches(e1.mode, e2.mode, e1.path);
113 114 115 116 117 118 119 120 121
	}
	free(one_buf);
	free(two_buf);
	return score;
}

/*
 * Match one itself and its subtrees with two and pick the best match.
 */
122 123
static void match_trees(const struct object_id *hash1,
			const struct object_id *hash2,
124 125
			int *best_score,
			char **best_match,
126
			const char *base,
127 128 129
			int recurse_limit)
{
	struct tree_desc one;
130
	void *one_buf = fill_tree_desc_strict(&one, hash1);
131 132 133

	while (one.size) {
		const char *path;
134
		const struct object_id *elem;
135 136 137 138 139 140
		unsigned mode;
		int score;

		elem = tree_entry_extract(&one, &path, &mode);
		if (!S_ISDIR(mode))
			goto next;
141
		score = score_trees(elem, hash2);
142 143
		if (*best_score < score) {
			free(*best_match);
144
			*best_match = xstrfmt("%s%s", base, path);
145 146 147
			*best_score = score;
		}
		if (recurse_limit) {
148
			char *newbase = xstrfmt("%s%s/", base, path);
149
			match_trees(elem, hash2, best_score, best_match,
150 151 152 153 154 155 156 157 158 159 160
				    newbase, recurse_limit - 1);
			free(newbase);
		}

	next:
		update_tree_entry(&one);
	}
	free(one_buf);
}

/*
161 162
 * A tree "oid1" has a subdirectory at "prefix".  Come up with a tree object by
 * replacing it with another tree "oid2".
163
 */
164 165
static int splice_tree(const struct object_id *oid1, const char *prefix,
		       const struct object_id *oid2, struct object_id *result)
166 167 168 169 170 171
{
	char *subpath;
	int toplen;
	char *buf;
	unsigned long sz;
	struct tree_desc desc;
172 173 174
	struct object_id *rewrite_here;
	const struct object_id *rewrite_with;
	struct object_id subtree;
175 176 177
	enum object_type type;
	int status;

178 179 180
	subpath = strchrnul(prefix, '/');
	toplen = subpath - prefix;
	if (*subpath)
181 182
		subpath++;

183
	buf = read_sha1_file(oid1->hash, &type, &sz);
184
	if (!buf)
185
		die("cannot read tree %s", oid_to_hex(oid1));
186 187 188 189 190 191
	init_tree_desc(&desc, buf, sz);

	rewrite_here = NULL;
	while (desc.size) {
		const char *name;
		unsigned mode;
192
		const struct object_id *oid;
193

194
		oid = tree_entry_extract(&desc, &name, &mode);
195 196 197
		if (strlen(name) == toplen &&
		    !memcmp(name, prefix, toplen)) {
			if (!S_ISDIR(mode))
198 199 200
				die("entry %s in tree %s is not a tree", name,
				    oid_to_hex(oid1));
			rewrite_here = (struct object_id *)oid;
201 202 203 204 205
			break;
		}
		update_tree_entry(&desc);
	}
	if (!rewrite_here)
206 207
		die("entry %.*s not found in tree %s", toplen, prefix,
		    oid_to_hex(oid1));
208
	if (*subpath) {
209
		status = splice_tree(rewrite_here, subpath, oid2, &subtree);
210 211
		if (status)
			return status;
212 213 214
		rewrite_with = &subtree;
	} else {
		rewrite_with = oid2;
215
	}
216
	oidcpy(rewrite_here, rewrite_with);
217
	status = write_object_file(buf, sz, tree_type, result);
218 219 220 221 222 223 224 225 226 227 228 229
	free(buf);
	return status;
}

/*
 * We are trying to come up with a merge between one and two that
 * results in a tree shape similar to one.  The tree two might
 * correspond to a subtree of one, in which case it needs to be
 * shifted down by prefixing otherwise empty directories.  On the
 * other hand, it could cover tree one and we might need to pick a
 * subtree of it.
 */
230 231 232
void shift_tree(const struct object_id *hash1,
		const struct object_id *hash2,
		struct object_id *shifted,
233 234 235 236 237 238
		int depth_limit)
{
	char *add_prefix;
	char *del_prefix;
	int add_score, del_score;

239 240 241 242 243 244 245
	/*
	 * NEEDSWORK: this limits the recursion depth to hardcoded
	 * value '2' to avoid excessive overhead.
	 */
	if (!depth_limit)
		depth_limit = 2;

246
	add_score = del_score = score_trees(hash1, hash2);
247 248 249 250 251 252 253
	add_prefix = xcalloc(1, 1);
	del_prefix = xcalloc(1, 1);

	/*
	 * See if one's subtree resembles two; if so we need to prefix
	 * two with a few fake trees to match the prefix.
	 */
254
	match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
255 256 257 258 259

	/*
	 * See if two's subtree resembles one; if so we need to
	 * pick only subtree of two.
	 */
260
	match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
261 262

	/* Assume we do not have to do any shifting */
263
	oidcpy(shifted, hash2);
264 265 266 267 268 269 270 271

	if (add_score < del_score) {
		/* We need to pick a subtree of two */
		unsigned mode;

		if (!*del_prefix)
			return;

272
		if (get_tree_entry(hash2->hash, del_prefix, shifted->hash, &mode))
273
			die("cannot find path %s in tree %s",
274
			    del_prefix, oid_to_hex(hash2));
275 276 277 278 279 280
		return;
	}

	if (!*add_prefix)
		return;

281
	splice_tree(hash1, add_prefix, hash2, shifted);
282
}
283 284 285 286 287 288

/*
 * The user says the trees will be shifted by this much.
 * Unfortunately we cannot fundamentally tell which one to
 * be prefixed, as recursive merge can work in either direction.
 */
289 290 291
void shift_tree_by(const struct object_id *hash1,
		   const struct object_id *hash2,
		   struct object_id *shifted,
292 293
		   const char *shift_prefix)
{
294
	struct object_id sub1, sub2;
295 296 297 298
	unsigned mode1, mode2;
	unsigned candidate = 0;

	/* Can hash2 be a tree at shift_prefix in tree hash1? */
299
	if (!get_tree_entry(hash1->hash, shift_prefix, sub1.hash, &mode1) &&
300 301 302 303
	    S_ISDIR(mode1))
		candidate |= 1;

	/* Can hash1 be a tree at shift_prefix in tree hash2? */
304
	if (!get_tree_entry(hash2->hash, shift_prefix, sub2.hash, &mode2) &&
305 306 307 308 309
	    S_ISDIR(mode2))
		candidate |= 2;

	if (candidate == 3) {
		/* Both are plausible -- we need to evaluate the score */
310
		int best_score = score_trees(hash1, hash2);
311 312 313
		int score;

		candidate = 0;
314
		score = score_trees(&sub1, hash2);
315 316 317 318
		if (score > best_score) {
			candidate = 1;
			best_score = score;
		}
319
		score = score_trees(&sub2, hash1);
320 321 322 323 324 325
		if (score > best_score)
			candidate = 2;
	}

	if (!candidate) {
		/* Neither is plausible -- do not shift */
326
		oidcpy(shifted, hash2);
327 328 329 330 331 332 333 334
		return;
	}

	if (candidate == 1)
		/*
		 * shift tree2 down by adding shift_prefix above it
		 * to match tree1.
		 */
335
		splice_tree(hash1, shift_prefix, hash2, shifted);
336 337 338 339 340
	else
		/*
		 * shift tree2 up by removing shift_prefix from it
		 * to match tree1.
		 */
341
		oidcpy(shifted, &sub2);
342
}