tree-diff.c 8.6 KB
Newer Older
1 2 3 4 5
/*
 * Helper functions for tree diff generation
 */
#include "cache.h"
#include "diff.h"
6
#include "diffcore.h"
7
#include "tree.h"
8

9 10
static void show_entry(struct diff_options *opt, const char *prefix,
		       struct tree_desc *desc, struct strbuf *base);
11

12 13
static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
			      struct strbuf *base, struct diff_options *opt)
14 15 16 17 18
{
	unsigned mode1, mode2;
	const char *path1, *path2;
	const unsigned char *sha1, *sha2;
	int cmp, pathlen1, pathlen2;
19
	int old_baselen = base->len;
20

21 22
	sha1 = tree_entry_extract(t1, &path1, &mode1);
	sha2 = tree_entry_extract(t2, &path2, &mode2);
23

24 25
	pathlen1 = tree_entry_len(&t1->entry);
	pathlen2 = tree_entry_len(&t2->entry);
26 27
	cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
	if (cmp < 0) {
28
		show_entry(opt, "-", t1, base);
29 30 31
		return -1;
	}
	if (cmp > 0) {
32
		show_entry(opt, "+", t2, base);
33 34
		return 1;
	}
35
	if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
36 37 38 39 40 41 42
		return 0;

	/*
	 * If the filemode has changed to/from a directory from/to a regular
	 * file, we need to consider it a remove and an add.
	 */
	if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
43 44
		show_entry(opt, "-", t1, base);
		show_entry(opt, "+", t2, base);
45 46 47
		return 0;
	}

48
	strbuf_add(base, path1, pathlen1);
49
	if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
D
Dmitry Potapov 已提交
50
		if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
51
			opt->change(opt, mode1, mode2,
52
				    sha1, sha2, 1, 1, base->buf, 0, 0);
D
Dmitry Potapov 已提交
53
		}
54
		strbuf_addch(base, '/');
J
Johannes Schindelin 已提交
55
		diff_tree_sha1(sha1, sha2, base->buf, opt);
56
	} else {
57
		opt->change(opt, mode1, mode2, sha1, sha2, 1, 1, base->buf, 0, 0);
58
	}
59
	strbuf_setlen(base, old_baselen);
60 61 62 63
	return 0;
}

/* A whole sub-tree went away or appeared */
64 65
static void show_tree(struct diff_options *opt, const char *prefix,
		      struct tree_desc *desc, struct strbuf *base)
66
{
67
	enum interesting match = entry_not_interesting;
68
	for (; desc->size; update_tree_entry(desc)) {
69
		if (match != all_entries_interesting) {
70 71
			match = tree_entry_interesting(&desc->entry, base, 0,
						       &opt->pathspec);
72
			if (match == all_entries_not_interesting)
73
				break;
74
			if (match == entry_not_interesting)
75
				continue;
76
		}
77
		show_entry(opt, prefix, desc, base);
78 79 80 81
	}
}

/* A file entry went away or appeared */
82 83
static void show_entry(struct diff_options *opt, const char *prefix,
		       struct tree_desc *desc, struct strbuf *base)
84 85 86
{
	unsigned mode;
	const char *path;
87
	const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
88
	int pathlen = tree_entry_len(&desc->entry);
89
	int old_baselen = base->len;
90

91
	strbuf_add(base, path, pathlen);
92
	if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
93
		enum object_type type;
94 95
		struct tree_desc inner;
		void *tree;
96
		unsigned long size;
97

98
		tree = read_sha1_file(sha1, &type, &size);
99
		if (!tree || type != OBJ_TREE)
100 101
			die("corrupt tree sha %s", sha1_to_hex(sha1));

102
		if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE))
103
			opt->add_remove(opt, *prefix, mode, sha1, 1, base->buf, 0);
104

105
		strbuf_addch(base, '/');
106

107 108
		init_tree_desc(&inner, tree, size);
		show_tree(opt, prefix, &inner, base);
109
		free(tree);
110
	} else
111
		opt->add_remove(opt, prefix[0], mode, sha1, 1, base->buf, 0);
112 113

	strbuf_setlen(base, old_baselen);
114 115
}

116
static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
117 118
			       struct diff_options *opt,
			       enum interesting *match)
119 120
{
	while (t->size) {
121 122
		*match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
		if (*match) {
123
			if (*match == all_entries_not_interesting)
124 125
				t->size = 0;
			break;
126
		}
127
		update_tree_entry(t);
128 129 130
	}
}

131 132
int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
	      const char *base_str, struct diff_options *opt)
133
{
134 135
	struct strbuf base;
	int baselen = strlen(base_str);
136 137
	enum interesting t1_match = entry_not_interesting;
	enum interesting t2_match = entry_not_interesting;
138

139 140 141 142
	/* Enable recursion indefinitely */
	opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
	opt->pathspec.max_depth = -1;

143 144 145
	strbuf_init(&base, PATH_MAX);
	strbuf_add(&base, base_str, baselen);

146
	for (;;) {
147
		if (diff_can_quit_early(opt))
J
Junio C Hamano 已提交
148
			break;
149
		if (opt->pathspec.nr) {
150 151
			skip_uninteresting(t1, &base, opt, &t1_match);
			skip_uninteresting(t2, &base, opt, &t2_match);
152 153
		}
		if (!t1->size) {
154 155
			if (!t2->size)
				break;
156
			show_entry(opt, "+", t2, &base);
157 158 159 160
			update_tree_entry(t2);
			continue;
		}
		if (!t2->size) {
161
			show_entry(opt, "-", t1, &base);
162 163 164
			update_tree_entry(t1);
			continue;
		}
165
		switch (compare_tree_entry(t1, t2, &base, opt)) {
166 167 168 169 170 171 172 173 174 175
		case -1:
			update_tree_entry(t1);
			continue;
		case 0:
			update_tree_entry(t1);
			/* Fallthrough */
		case 1:
			update_tree_entry(t2);
			continue;
		}
176
		die("git diff-tree: internal error");
177
	}
178 179

	strbuf_release(&base);
180 181 182
	return 0;
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196
/*
 * Does it look like the resulting diff might be due to a rename?
 *  - single entry
 *  - not a valid previous file
 */
static inline int diff_might_be_rename(void)
{
	return diff_queued_diff.nr == 1 &&
		!DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
}

static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
{
	struct diff_options diff_opts;
197 198 199
	struct diff_queue_struct *q = &diff_queued_diff;
	struct diff_filepair *choice;
	const char *paths[1];
200 201
	int i;

202 203 204 205
	/* Remove the file creation entry from the diff queue, and remember it */
	choice = q->queue[0];
	q->nr = 0;

206
	diff_setup(&diff_opts);
207
	DIFF_OPT_SET(&diff_opts, RECURSIVE);
208
	DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
209
	diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
210
	diff_opts.single_follow = opt->pathspec.raw[0];
L
Linus Torvalds 已提交
211
	diff_opts.break_opt = opt->break_opt;
212
	diff_opts.rename_score = opt->rename_score;
213 214 215 216 217 218
	paths[0] = NULL;
	diff_tree_setup_paths(paths, &diff_opts);
	if (diff_setup_done(&diff_opts) < 0)
		die("unable to set up diff options to follow renames");
	diff_tree(t1, t2, base, &diff_opts);
	diffcore_std(&diff_opts);
219
	diff_tree_release_paths(&diff_opts);
220

221
	/* Go through the new set of filepairing, and see if we find a more interesting one */
222
	opt->found_follow = 0;
223 224
	for (i = 0; i < q->nr; i++) {
		struct diff_filepair *p = q->queue[i];
225 226 227

		/*
		 * Found a source? Not only do we use that for the new
228
		 * diff_queued_diff, we will also use that as the path in
229 230
		 * the future!
		 */
231 232
		if ((p->status == 'R' || p->status == 'C') &&
		    !strcmp(p->two->path, opt->pathspec.raw[0])) {
233 234 235 236 237
			/* Switch the file-pairs around */
			q->queue[i] = choice;
			choice = p;

			/* Update the path we use from now on.. */
238
			diff_tree_release_paths(opt);
239 240
			opt->pathspec.raw[0] = xstrdup(p->one->path);
			diff_tree_setup_paths(opt->pathspec.raw, opt);
241 242 243 244 245 246 247 248 249 250

			/*
			 * The caller expects us to return a set of vanilla
			 * filepairs to let a later call to diffcore_std()
			 * it makes to sort the renames out (among other
			 * things), but we already have found renames
			 * ourselves; signal diffcore_std() not to muck with
			 * rename information.
			 */
			opt->found_follow = 1;
251 252 253 254 255
			break;
		}
	}

	/*
M
Mike Ralphson 已提交
256
	 * Then, discard all the non-relevant file pairs...
257 258 259 260 261 262 263 264 265
	 */
	for (i = 0; i < q->nr; i++) {
		struct diff_filepair *p = q->queue[i];
		diff_free_filepair(p);
	}

	/*
	 * .. and re-instate the one we want (which might be either the
	 * original one, or the rename/copy we found)
266
	 */
267 268
	q->queue[0] = choice;
	q->nr = 1;
269 270
}

271 272 273 274
int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
{
	void *tree1, *tree2;
	struct tree_desc t1, t2;
275
	unsigned long size1, size2;
276 277
	int retval;

278
	tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
279 280
	if (!tree1)
		die("unable to read source tree (%s)", sha1_to_hex(old));
281
	tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
282 283
	if (!tree2)
		die("unable to read destination tree (%s)", sha1_to_hex(new));
284 285
	init_tree_desc(&t1, tree1, size1);
	init_tree_desc(&t2, tree2, size2);
286
	retval = diff_tree(&t1, &t2, base, opt);
287
	if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
288 289 290 291
		init_tree_desc(&t1, tree1, size1);
		init_tree_desc(&t2, tree2, size2);
		try_to_follow_renames(&t1, &t2, base, opt);
	}
292 293 294 295 296
	free(tree1);
	free(tree2);
	return retval;
}

R
Rene Scharfe 已提交
297 298 299 300
int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
{
	int retval;
	void *tree;
301
	unsigned long size;
R
Rene Scharfe 已提交
302 303
	struct tree_desc empty, real;

304
	tree = read_object_with_reference(new, tree_type, &size, NULL);
R
Rene Scharfe 已提交
305 306
	if (!tree)
		die("unable to read root tree (%s)", sha1_to_hex(new));
307
	init_tree_desc(&real, tree, size);
R
Rene Scharfe 已提交
308

309
	init_tree_desc(&empty, "", 0);
R
Rene Scharfe 已提交
310 311 312 313 314
	retval = diff_tree(&empty, &real, base, opt);
	free(tree);
	return retval;
}

315
void diff_tree_release_paths(struct diff_options *opt)
316
{
317
	free_pathspec(&opt->pathspec);
318 319 320 321
}

void diff_tree_setup_paths(const char **p, struct diff_options *opt)
{
322
	init_pathspec(&opt->pathspec, p);
323
}