diff.c 34.8 KB
Newer Older
1 2 3 4 5
/*
 * Copyright (C) 2005 Junio C Hamano
 */
#include <sys/types.h>
#include <sys/wait.h>
6
#include <signal.h>
7
#include "cache.h"
8
#include "quote.h"
9
#include "diff.h"
10
#include "diffcore.h"
11

J
Junio C Hamano 已提交
12
static const char *diff_opts = "-pu";
13

14
static int use_size_cache;
15

16 17
int diff_rename_limit_default = -1;

18 19 20 21 22 23 24 25 26 27
int git_diff_config(const char *var, const char *value)
{
	if (!strcmp(var, "diff.renamelimit")) {
		diff_rename_limit_default = git_config_int(var, value);
		return 0;
	}

	return git_default_config(var, value);
}

28 29 30 31 32 33 34 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
static char *quote_one(const char *str)
{
	int needlen;
	char *xp;

	if (!str)
		return NULL;
	needlen = quote_c_style(str, NULL, NULL, 0);
	if (!needlen)
		return strdup(str);
	xp = xmalloc(needlen + 1);
	quote_c_style(str, xp, NULL, 0);
	return xp;
}

static char *quote_two(const char *one, const char *two)
{
	int need_one = quote_c_style(one, NULL, NULL, 1);
	int need_two = quote_c_style(two, NULL, NULL, 1);
	char *xp;

	if (need_one + need_two) {
		if (!need_one) need_one = strlen(one);
		if (!need_two) need_one = strlen(two);

		xp = xmalloc(need_one + need_two + 3);
		xp[0] = '"';
		quote_c_style(one, xp + 1, NULL, 1);
		quote_c_style(two, xp + need_one + 1, NULL, 1);
		strcpy(xp + need_one + need_two + 1, "\"");
		return xp;
	}
	need_one = strlen(one);
	need_two = strlen(two);
	xp = xmalloc(need_one + need_two + 1);
	strcpy(xp, one);
	strcpy(xp + need_one, two);
	return xp;
}

68
static const char *external_diff(void)
69
{
J
Junio C Hamano 已提交
70
	static const char *external_diff_cmd = NULL;
71
	static int done_preparing = 0;
J
Jason Riedy 已提交
72
	const char *env_diff_opts;
73 74 75 76

	if (done_preparing)
		return external_diff_cmd;

77 78 79 80 81 82 83 84
	/*
	 * Default values above are meant to match the
	 * Linux kernel development style.  Examples of
	 * alternative styles you can specify via environment
	 * variables are:
	 *
	 * GIT_DIFF_OPTS="-c";
	 */
85
	external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
86 87

	/* In case external diff fails... */
88
	env_diff_opts = getenv("GIT_DIFF_OPTS");
J
Jason Riedy 已提交
89
	if (env_diff_opts) diff_opts = env_diff_opts;
90 91 92

	done_preparing = 1;
	return external_diff_cmd;
93 94
}

95 96
#define TEMPFILE_PATH_LEN		50

97
static struct diff_tempfile {
98
	const char *name; /* filename external diff should read from */
99 100
	char hex[41];
	char mode[10];
101
	char tmp_path[TEMPFILE_PATH_LEN];
102 103
} diff_temp[2];

J
Junio C Hamano 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180
static int count_lines(const char *filename)
{
	FILE *in;
	int count, ch, completely_empty = 1, nl_just_seen = 0;
	in = fopen(filename, "r");
	count = 0;
	while ((ch = fgetc(in)) != EOF)
		if (ch == '\n') {
			count++;
			nl_just_seen = 1;
			completely_empty = 0;
		}
		else {
			nl_just_seen = 0;
			completely_empty = 0;
		}
	fclose(in);
	if (completely_empty)
		return 0;
	if (!nl_just_seen)
		count++; /* no trailing newline */
	return count;
}

static void print_line_count(int count)
{
	switch (count) {
	case 0:
		printf("0,0");
		break;
	case 1:
		printf("1");
		break;
	default:
		printf("1,%d", count);
		break;
	}
}

static void copy_file(int prefix, const char *filename)
{
	FILE *in;
	int ch, nl_just_seen = 1;
	in = fopen(filename, "r");
	while ((ch = fgetc(in)) != EOF) {
		if (nl_just_seen)
			putchar(prefix);
		putchar(ch);
		if (ch == '\n')
			nl_just_seen = 1;
		else
			nl_just_seen = 0;
	}
	fclose(in);
	if (!nl_just_seen)
		printf("\n\\ No newline at end of file\n");
}

static void emit_rewrite_diff(const char *name_a,
			      const char *name_b,
			      struct diff_tempfile *temp)
{
	/* Use temp[i].name as input, name_a and name_b as labels */
	int lc_a, lc_b;
	lc_a = count_lines(temp[0].name);
	lc_b = count_lines(temp[1].name);
	printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
	print_line_count(lc_a);
	printf(" +");
	print_line_count(lc_b);
	printf(" @@\n");
	if (lc_a)
		copy_file('-', temp[0].name);
	if (lc_b)
		copy_file('+', temp[1].name);
}

J
Junio C Hamano 已提交
181 182
static void builtin_diff(const char *name_a,
			 const char *name_b,
J
Junio C Hamano 已提交
183
			 struct diff_tempfile *temp,
J
Junio C Hamano 已提交
184 185
			 const char *xfrm_msg,
			 int complete_rewrite)
186
{
187
	int i, next_at, cmd_size;
188
	const char *const diff_cmd = "diff -L%s -L%s";
189
	const char *const diff_arg  = "-- %s %s||:"; /* "||:" is to return 0 */
190
	const char *input_name_sq[2];
191
	const char *label_path[2];
192
	char *cmd;
J
Junio C Hamano 已提交
193

194 195
	/* diff_cmd and diff_arg have 4 %s in total which makes
	 * the sum of these strings 8 bytes larger than required.
196
	 * we use 2 spaces around diff-opts, and we need to count
197 198 199
	 * terminating NUL; we used to subtract 5 here, but we do not
	 * care about small leaks in this subprocess that is about
	 * to exec "diff" anymore.
200
	 */
201 202 203
	cmd_size = (strlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg)
		    + 128);

204
	for (i = 0; i < 2; i++) {
205
		input_name_sq[i] = sq_quote(temp[i].name);
206 207 208 209 210 211 212
		if (!strcmp(temp[i].name, "/dev/null"))
			label_path[i] = "/dev/null";
		else if (!i)
			label_path[i] = sq_quote(quote_two("a/", name_a));
		else
			label_path[i] = sq_quote(quote_two("b/", name_b));
		cmd_size += (strlen(label_path[i]) + strlen(input_name_sq[i]));
213
	}
214

215 216 217
	cmd = xmalloc(cmd_size);

	next_at = 0;
218
	next_at += snprintf(cmd+next_at, cmd_size-next_at,
219
			    diff_cmd, label_path[0], label_path[1]);
220 221 222
	next_at += snprintf(cmd+next_at, cmd_size-next_at,
			    " %s ", diff_opts);
	next_at += snprintf(cmd+next_at, cmd_size-next_at,
223 224
			    diff_arg, input_name_sq[0], input_name_sq[1]);

225 226 227 228
	printf("diff --git %s %s\n",
	       quote_two("a/", name_a), quote_two("b/", name_b));
	if (label_path[0][0] == '/') {
		/* dev/null */
229
		printf("new file mode %s\n", temp[1].mode);
230 231 232
		if (xfrm_msg && xfrm_msg[0])
			puts(xfrm_msg);
	}
233
	else if (label_path[1][0] == '/') {
234
		printf("deleted file mode %s\n", temp[0].mode);
235 236 237
		if (xfrm_msg && xfrm_msg[0])
			puts(xfrm_msg);
	}
238
	else {
239 240 241 242
		if (strcmp(temp[0].mode, temp[1].mode)) {
			printf("old mode %s\n", temp[0].mode);
			printf("new mode %s\n", temp[1].mode);
		}
243
		if (xfrm_msg && xfrm_msg[0])
244
			puts(xfrm_msg);
245 246 247 248
		if (strncmp(temp[0].mode, temp[1].mode, 3))
			/* we do not run diff between different kind
			 * of objects.
			 */
249
			exit(0);
J
Junio C Hamano 已提交
250 251 252 253 254
		if (complete_rewrite) {
			fflush(NULL);
			emit_rewrite_diff(name_a, name_b, temp);
			exit(0);
		}
255
	}
256
	fflush(NULL);
257
	execlp("/bin/sh","sh", "-c", cmd, NULL);
258 259
}

260 261 262 263
struct diff_filespec *alloc_filespec(const char *path)
{
	int namelen = strlen(path);
	struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
264 265

	memset(spec, 0, sizeof(*spec));
266
	spec->path = (char *)(spec + 1);
267
	memcpy(spec->path, path, namelen+1);
268 269 270 271 272 273
	return spec;
}

void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
		   unsigned short mode)
{
274 275
	if (mode) {
		spec->mode = DIFF_FILE_CANON_MODE(mode);
276 277 278
		memcpy(spec->sha1, sha1, 20);
		spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
	}
279 280
}

J
Junio C Hamano 已提交
281 282 283 284 285 286 287 288 289 290
/*
 * Given a name and sha1 pair, if the dircache tells us the file in
 * the work tree has that object contents, return true, so that
 * prepare_temp_file() does not have to inflate and extract.
 */
static int work_tree_matches(const char *name, const unsigned char *sha1)
{
	struct cache_entry *ce;
	struct stat st;
	int pos, len;
291

J
Junio C Hamano 已提交
292 293 294 295
	/* We do not read the cache ourselves here, because the
	 * benchmark with my previous version that always reads cache
	 * shows that it makes things worse for diff-tree comparing
	 * two linux-2.6 kernel trees in an already checked out work
J
Junio C Hamano 已提交
296
	 * tree.  This is because most diff-tree comparisons deal with
J
Junio C Hamano 已提交
297 298 299 300 301 302
	 * only a small number of files, while reading the cache is
	 * expensive for a large project, and its cost outweighs the
	 * savings we get by not inflating the object to a temporary
	 * file.  Practically, this code only helps when we are used
	 * by diff-cache --cached, which does read the cache before
	 * calling us.
J
Junio C Hamano 已提交
303
	 */
J
Junio C Hamano 已提交
304 305 306 307 308 309 310 311
	if (!active_cache)
		return 0;

	len = strlen(name);
	pos = cache_name_pos(name, len);
	if (pos < 0)
		return 0;
	ce = active_cache[pos];
312
	if ((lstat(name, &st) < 0) ||
313
	    !S_ISREG(st.st_mode) || /* careful! */
314
	    ce_match_stat(ce, &st) ||
J
Junio C Hamano 已提交
315 316
	    memcmp(sha1, ce->sha1, 20))
		return 0;
317 318 319 320 321
	/* we return 1 only when we can stat, it is a regular file,
	 * stat information matches, and sha1 recorded in the cache
	 * matches.  I.e. we know the file in the work tree really is
	 * the same as the <name, sha1> pair.
	 */
J
Junio C Hamano 已提交
322 323 324
	return 1;
}

325 326 327 328 329 330 331
static struct sha1_size_cache {
	unsigned char sha1[20];
	unsigned long size;
} **sha1_size_cache;
static int sha1_size_cache_nr, sha1_size_cache_alloc;

static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
332
						 int find_only,
333 334 335 336 337 338 339 340
						 unsigned long size)
{
	int first, last;
	struct sha1_size_cache *e;

	first = 0;
	last = sha1_size_cache_nr;
	while (last > first) {
341
		int cmp, next = (last + first) >> 1;
342
		e = sha1_size_cache[next];
343
		cmp = memcmp(e->sha1, sha1, 20);
344 345 346 347 348 349 350 351 352
		if (!cmp)
			return e;
		if (cmp < 0) {
			last = next;
			continue;
		}
		first = next+1;
	}
	/* not found */
353
	if (find_only)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
		return NULL;
	/* insert to make it at "first" */
	if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
		sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
		sha1_size_cache = xrealloc(sha1_size_cache,
					   sha1_size_cache_alloc *
					   sizeof(*sha1_size_cache));
	}
	sha1_size_cache_nr++;
	if (first < sha1_size_cache_nr)
		memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
			(sha1_size_cache_nr - first - 1) *
			sizeof(*sha1_size_cache));
	e = xmalloc(sizeof(struct sha1_size_cache));
	sha1_size_cache[first] = e;
	memcpy(e->sha1, sha1, 20);
	e->size = size;
	return e;
}

374 375 376 377 378
/*
 * While doing rename detection and pickaxe operation, we may need to
 * grab the data for the blob (or file) for our own in-core comparison.
 * diff_filespec has data and size fields for this purpose.
 */
379
int diff_populate_filespec(struct diff_filespec *s, int size_only)
380 381
{
	int err = 0;
382
	if (!DIFF_FILE_VALID(s))
383 384 385 386
		die("internal error: asking to populate invalid file.");
	if (S_ISDIR(s->mode))
		return -1;

387 388 389
	if (!use_size_cache)
		size_only = 0;

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	if (s->data)
		return err;
	if (!s->sha1_valid ||
	    work_tree_matches(s->path, s->sha1)) {
		struct stat st;
		int fd;
		if (lstat(s->path, &st) < 0) {
			if (errno == ENOENT) {
			err_empty:
				err = -1;
			empty:
				s->data = "";
				s->size = 0;
				return err;
			}
		}
		s->size = st.st_size;
		if (!s->size)
			goto empty;
409 410
		if (size_only)
			return 0;
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
		if (S_ISLNK(st.st_mode)) {
			int ret;
			s->data = xmalloc(s->size);
			s->should_free = 1;
			ret = readlink(s->path, s->data, s->size);
			if (ret < 0) {
				free(s->data);
				goto err_empty;
			}
			return 0;
		}
		fd = open(s->path, O_RDONLY);
		if (fd < 0)
			goto err_empty;
		s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
		close(fd);
P
Pavel Roskin 已提交
427 428 429
		if (s->data == MAP_FAILED)
			goto err_empty;
		s->should_munmap = 1;
430 431 432
	}
	else {
		char type[20];
433 434 435
		struct sha1_size_cache *e;

		if (size_only) {
436
			e = locate_size_cache(s->sha1, 1, 0);
437 438 439 440
			if (e) {
				s->size = e->size;
				return 0;
			}
441
			if (!sha1_object_info(s->sha1, type, &s->size))
442
				locate_size_cache(s->sha1, 0, s->size);
443 444 445 446
		}
		else {
			s->data = read_sha1_file(s->sha1, type, &s->size);
			s->should_free = 1;
447
		}
448 449 450 451
	}
	return 0;
}

452
void diff_free_filespec_data(struct diff_filespec *s)
453 454 455 456 457
{
	if (s->should_free)
		free(s->data);
	else if (s->should_munmap)
		munmap(s->data, s->size);
458 459
	s->should_free = s->should_munmap = 0;
	s->data = NULL;
460 461
}

462 463 464
static void prep_temp_blob(struct diff_tempfile *temp,
			   void *blob,
			   unsigned long size,
J
Junio C Hamano 已提交
465
			   const unsigned char *sha1,
466 467 468 469
			   int mode)
{
	int fd;

470
	fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
471 472 473 474 475 476 477 478 479 480 481
	if (fd < 0)
		die("unable to create temp-file");
	if (write(fd, blob, size) != size)
		die("unable to write temp-file");
	close(fd);
	temp->name = temp->tmp_path;
	strcpy(temp->hex, sha1_to_hex(sha1));
	temp->hex[40] = 0;
	sprintf(temp->mode, "%06o", mode);
}

482 483
static void prepare_temp_file(const char *name,
			      struct diff_tempfile *temp,
484
			      struct diff_filespec *one)
485
{
486
	if (!DIFF_FILE_VALID(one)) {
487
	not_a_valid_file:
488 489 490
		/* A '-' entry produces this for file-2, and
		 * a '+' entry produces this for file-1.
		 */
491 492 493
		temp->name = "/dev/null";
		strcpy(temp->hex, ".");
		strcpy(temp->mode, ".");
494 495
		return;
	}
496

497
	if (!one->sha1_valid ||
498
	    work_tree_matches(name, one->sha1)) {
499
		struct stat st;
500
		if (lstat(name, &st) < 0) {
501 502
			if (errno == ENOENT)
				goto not_a_valid_file;
503
			die("stat(%s): %s", name, strerror(errno));
504
		}
505 506 507 508 509 510 511 512 513 514
		if (S_ISLNK(st.st_mode)) {
			int ret;
			char *buf, buf_[1024];
			buf = ((sizeof(buf_) < st.st_size) ?
			       xmalloc(st.st_size) : buf_);
			ret = readlink(name, buf, st.st_size);
			if (ret < 0)
				die("readlink(%s)", name);
			prep_temp_blob(temp, buf, st.st_size,
				       (one->sha1_valid ?
515
					one->sha1 : null_sha1),
516 517 518 519
				       (one->sha1_valid ?
					one->mode : S_IFLNK));
		}
		else {
520 521
			/* we can borrow from the file in the work tree */
			temp->name = name;
522 523 524
			if (!one->sha1_valid)
				strcpy(temp->hex, sha1_to_hex(null_sha1));
			else
525
				strcpy(temp->hex, sha1_to_hex(one->sha1));
J
Junio C Hamano 已提交
526 527 528 529 530 531 532
			/* Even though we may sometimes borrow the
			 * contents from the work tree, we always want
			 * one->mode.  mode is trustworthy even when
			 * !(one->sha1_valid), as long as
			 * DIFF_FILE_VALID(one).
			 */
			sprintf(temp->mode, "%06o", one->mode);
533 534
		}
		return;
535 536
	}
	else {
537
		if (diff_populate_filespec(one, 0))
538 539 540
			die("cannot read data blob for %s", one->path);
		prep_temp_blob(temp, one->data, one->size,
			       one->sha1, one->mode);
541 542 543 544 545 546 547 548 549 550 551 552 553 554
	}
}

static void remove_tempfile(void)
{
	int i;

	for (i = 0; i < 2; i++)
		if (diff_temp[i].name == diff_temp[i].tmp_path) {
			unlink(diff_temp[i].name);
			diff_temp[i].name = NULL;
		}
}

555 556 557 558 559
static void remove_tempfile_on_signal(int signo)
{
	remove_tempfile();
}

560 561 562
/* An external diff command takes:
 *
 * diff-cmd name infile1 infile1-sha1 infile1-mode \
563
 *               infile2 infile2-sha1 infile2-mode [ rename-to ]
564 565
 *
 */
566 567
static void run_external_diff(const char *pgm,
			      const char *name,
568
			      const char *other,
569 570
			      struct diff_filespec *one,
			      struct diff_filespec *two,
J
Junio C Hamano 已提交
571 572
			      const char *xfrm_msg,
			      int complete_rewrite)
573 574
{
	struct diff_tempfile *temp = diff_temp;
575 576
	pid_t pid;
	int status;
577
	static int atexit_asked = 0;
J
Jason Riedy 已提交
578
	const char *othername;
579

J
Jason Riedy 已提交
580
	othername = (other? other : name);
581 582
	if (one && two) {
		prepare_temp_file(name, &temp[0], one);
J
Jason Riedy 已提交
583
		prepare_temp_file(othername, &temp[1], two);
584 585 586 587 588 589
		if (! atexit_asked &&
		    (temp[0].name == temp[0].tmp_path ||
		     temp[1].name == temp[1].tmp_path)) {
			atexit_asked = 1;
			atexit(remove_tempfile);
		}
590
		signal(SIGINT, remove_tempfile_on_signal);
591 592 593 594 595 596 597
	}

	fflush(NULL);
	pid = fork();
	if (pid < 0)
		die("unable to fork");
	if (!pid) {
598 599
		if (pgm) {
			if (one && two) {
600
				const char *exec_arg[10];
601 602 603 604 605 606 607 608 609
				const char **arg = &exec_arg[0];
				*arg++ = pgm;
				*arg++ = name;
				*arg++ = temp[0].name;
				*arg++ = temp[0].hex;
				*arg++ = temp[0].mode;
				*arg++ = temp[1].name;
				*arg++ = temp[1].hex;
				*arg++ = temp[1].mode;
610
				if (other) {
611
					*arg++ = other;
612 613
					*arg++ = xfrm_msg;
				}
L
Linus Torvalds 已提交
614
				*arg = NULL;
615 616
				execvp(pgm, (char *const*) exec_arg);
			}
617 618 619
			else
				execlp(pgm, pgm, name, NULL);
		}
620 621 622
		/*
		 * otherwise we use the built-in one.
		 */
623
		if (one && two)
J
Jason Riedy 已提交
624
			builtin_diff(name, othername, temp, xfrm_msg,
J
Junio C Hamano 已提交
625
				     complete_rewrite);
626 627
		else
			printf("* Unmerged path %s\n", name);
628 629
		exit(0);
	}
630 631 632
	if (waitpid(pid, &status, 0) < 0 ||
	    !WIFEXITED(status) || WEXITSTATUS(status)) {
		/* Earlier we did not check the exit status because
633
		 * diff exits non-zero if files are different, and
634 635 636 637 638 639
		 * we are not interested in knowing that.  It was a
		 * mistake which made it harder to quit a diff-*
		 * session that uses the git-apply-patch-script as
		 * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
		 * should also exit non-zero only when it wants to
		 * abort the entire diff-* session.
640 641
		 */
		remove_tempfile();
642 643
		fprintf(stderr, "external diff died, stopping at %s.\n", name);
		exit(1);
644
	}
645 646 647
	remove_tempfile();
}

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
static void diff_fill_sha1_info(struct diff_filespec *one)
{
	if (DIFF_FILE_VALID(one)) {
		if (!one->sha1_valid) {
			struct stat st;
			if (stat(one->path, &st) < 0)
				die("stat %s", one->path);
			if (index_path(one->sha1, one->path, &st, 0))
				die("cannot hash %s\n", one->path);
		}
	}
	else
		memset(one->sha1, 0, 20);
}

J
Junio C Hamano 已提交
663
static void run_diff(struct diff_filepair *p, struct diff_options *o)
664 665
{
	const char *pgm = external_diff();
666
	char msg[PATH_MAX*2+300], *xfrm_msg;
J
Junio C Hamano 已提交
667 668 669 670
	struct diff_filespec *one;
	struct diff_filespec *two;
	const char *name;
	const char *other;
671
	char *name_munged, *other_munged;
J
Junio C Hamano 已提交
672
	int complete_rewrite = 0;
673
	int len;
J
Junio C Hamano 已提交
674 675 676 677 678 679 680 681 682 683

	if (DIFF_PAIR_UNMERGED(p)) {
		/* unmerged */
		run_external_diff(pgm, p->one->path, NULL, NULL, NULL, NULL,
				  0);
		return;
	}

	name = p->one->path;
	other = (strcmp(name, p->two->path) ? p->two->path : NULL);
684 685
	name_munged = quote_one(name);
	other_munged = quote_one(other);
J
Junio C Hamano 已提交
686
	one = p->one; two = p->two;
687 688 689 690 691

	diff_fill_sha1_info(one);
	diff_fill_sha1_info(two);

	len = 0;
J
Junio C Hamano 已提交
692
	switch (p->status) {
693
	case DIFF_STATUS_COPIED:
694 695 696 697 698
		len += snprintf(msg + len, sizeof(msg) - len,
				"similarity index %d%%\n"
				"copy from %s\n"
				"copy to %s\n",
				(int)(0.5 + p->score * 100.0/MAX_SCORE),
699
				name_munged, other_munged);
J
Junio C Hamano 已提交
700
		break;
701
	case DIFF_STATUS_RENAMED:
702 703 704 705 706
		len += snprintf(msg + len, sizeof(msg) - len,
				"similarity index %d%%\n"
				"rename from %s\n"
				"rename to %s\n",
				(int)(0.5 + p->score * 100.0/MAX_SCORE),
707
				name_munged, other_munged);
J
Junio C Hamano 已提交
708
		break;
709
	case DIFF_STATUS_MODIFIED:
J
Junio C Hamano 已提交
710
		if (p->score) {
711 712 713 714
			len += snprintf(msg + len, sizeof(msg) - len,
					"dissimilarity index %d%%\n",
					(int)(0.5 + p->score *
					      100.0/MAX_SCORE));
J
Junio C Hamano 已提交
715 716 717 718 719
			complete_rewrite = 1;
			break;
		}
		/* fallthru */
	default:
720 721
		/* nothing */
		;
J
Junio C Hamano 已提交
722 723
	}

724 725
	if (memcmp(one->sha1, two->sha1, 20)) {
		char one_sha1[41];
J
Junio C Hamano 已提交
726
		int abbrev = o->full_index ? 40 : DIFF_DEFAULT_INDEX_ABBREV;
727 728 729
		memcpy(one_sha1, sha1_to_hex(one->sha1), 41);

		len += snprintf(msg + len, sizeof(msg) - len,
J
Junio C Hamano 已提交
730 731 732
				"index %.*s..%.*s",
				abbrev, one_sha1, abbrev,
				sha1_to_hex(two->sha1));
733 734 735 736 737 738 739 740 741 742
		if (one->mode == two->mode)
			len += snprintf(msg + len, sizeof(msg) - len,
					" %06o", one->mode);
		len += snprintf(msg + len, sizeof(msg) - len, "\n");
	}

	if (len)
		msg[--len] = 0;
	xfrm_msg = len ? msg : NULL;

743 744 745 746 747 748 749
	if (!pgm &&
	    DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
	    (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
		/* a filepair that changes between file and symlink
		 * needs to be split into deletion and creation.
		 */
		struct diff_filespec *null = alloc_filespec(two->path);
J
Junio C Hamano 已提交
750
		run_external_diff(NULL, name, other, one, null, xfrm_msg, 0);
751 752
		free(null);
		null = alloc_filespec(one->path);
J
Junio C Hamano 已提交
753
		run_external_diff(NULL, name, other, null, two, xfrm_msg, 0);
754 755 756
		free(null);
	}
	else
J
Junio C Hamano 已提交
757 758
		run_external_diff(pgm, name, other, one, two, xfrm_msg,
				  complete_rewrite);
759 760 761

	free(name_munged);
	free(other_munged);
762 763
}

J
Junio C Hamano 已提交
764
void diff_setup(struct diff_options *options)
765
{
J
Junio C Hamano 已提交
766 767 768 769
	memset(options, 0, sizeof(*options));
	options->output_format = DIFF_FORMAT_RAW;
	options->line_termination = '\n';
	options->break_opt = -1;
770
	options->rename_limit = -1;
771 772 773

	options->change = diff_change;
	options->add_remove = diff_addremove;
J
Junio C Hamano 已提交
774 775 776 777
}

int diff_setup_done(struct diff_options *options)
{
778 779 780
	if ((options->find_copies_harder &&
	     options->detect_rename != DIFF_DETECT_COPY) ||
	    (0 <= options->rename_limit && !options->detect_rename))
J
Junio C Hamano 已提交
781
		return -1;
782 783
	if (options->detect_rename && options->rename_limit < 0)
		options->rename_limit = diff_rename_limit_default;
J
Junio C Hamano 已提交
784
	if (options->setup & DIFF_SETUP_USE_CACHE) {
785 786 787 788 789 790 791 792 793
		if (!active_cache)
			/* read-cache does not die even when it fails
			 * so it is safe for us to do this here.  Also
			 * it does not smudge active_cache or active_nr
			 * when it fails, so we do not have to worry about
			 * cleaning it up oufselves either.
			 */
			read_cache();
	}
J
Junio C Hamano 已提交
794
	if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
795
		use_size_cache = 1;
J
Junio C Hamano 已提交
796 797
	if (options->abbrev <= 0 || 40 < options->abbrev)
		options->abbrev = 40; /* full */
J
Junio C Hamano 已提交
798 799 800 801 802 803 804 805 806 807 808

	return 0;
}

int diff_opt_parse(struct diff_options *options, const char **av, int ac)
{
	const char *arg = av[0];
	if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
		options->output_format = DIFF_FORMAT_PATCH;
	else if (!strcmp(arg, "-z"))
		options->line_termination = 0;
809 810
	else if (!strncmp(arg, "-l", 2))
		options->rename_limit = strtoul(arg+2, NULL, 10);
J
Junio C Hamano 已提交
811 812
	else if (!strcmp(arg, "--full-index"))
		options->full_index = 1;
J
Junio C Hamano 已提交
813 814
	else if (!strcmp(arg, "--name-only"))
		options->output_format = DIFF_FORMAT_NAME;
815 816
	else if (!strcmp(arg, "--name-status"))
		options->output_format = DIFF_FORMAT_NAME_STATUS;
J
Junio C Hamano 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
	else if (!strcmp(arg, "-R"))
		options->reverse_diff = 1;
	else if (!strncmp(arg, "-S", 2))
		options->pickaxe = arg + 2;
	else if (!strcmp(arg, "-s"))
		options->output_format = DIFF_FORMAT_NO_OUTPUT;
	else if (!strncmp(arg, "-O", 2))
		options->orderfile = arg + 2;
	else if (!strncmp(arg, "--diff-filter=", 14))
		options->filter = arg + 14;
	else if (!strcmp(arg, "--pickaxe-all"))
		options->pickaxe_opts = DIFF_PICKAXE_ALL;
	else if (!strncmp(arg, "-B", 2)) {
		if ((options->break_opt =
		     diff_scoreopt_parse(arg)) == -1)
			return -1;
	}
	else if (!strncmp(arg, "-M", 2)) {
		if ((options->rename_score =
		     diff_scoreopt_parse(arg)) == -1)
			return -1;
		options->detect_rename = DIFF_DETECT_RENAME;
	}
	else if (!strncmp(arg, "-C", 2)) {
		if ((options->rename_score =
		     diff_scoreopt_parse(arg)) == -1)
			return -1;
		options->detect_rename = DIFF_DETECT_COPY;
	}
	else if (!strcmp(arg, "--find-copies-harder"))
		options->find_copies_harder = 1;
J
Junio C Hamano 已提交
848 849 850 851
	else if (!strcmp(arg, "--abbrev"))
		options->abbrev = DIFF_DEFAULT_ABBREV;
	else if (!strncmp(arg, "--abbrev=", 9))
		options->abbrev = strtoul(arg + 9, NULL, 10);
J
Junio C Hamano 已提交
852 853 854
	else
		return 0;
	return 1;
855 856
}

857 858
static int parse_num(const char **cp_p)
{
859 860
	unsigned long num, scale;
	int ch, dot;
861 862
	const char *cp = *cp_p;

863
	num = 0;
864
	scale = 1;
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
	dot = 0;
	for(;;) {
		ch = *cp;
		if ( !dot && ch == '.' ) {
			scale = 1;
			dot = 1;
		} else if ( ch == '%' ) {
			scale = dot ? scale*100 : 100;
			cp++;	/* % is always at the end */
			break;
		} else if ( ch >= '0' && ch <= '9' ) {
			if ( scale < 100000 ) {
				scale *= 10;
				num = (num*10) + (ch-'0');
			}
		} else {
			break;
882
		}
J
Junio C Hamano 已提交
883
		cp++;
884 885 886 887 888 889
	}
	*cp_p = cp;

	/* user says num divided by scale and we say internally that
	 * is MAX_SCORE * num / scale.
	 */
890
	return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
891 892 893 894
}

int diff_scoreopt_parse(const char *opt)
{
895
	int opt1, opt2, cmd;
896 897 898 899 900 901 902 903

	if (*opt++ != '-')
		return -1;
	cmd = *opt++;
	if (cmd != 'M' && cmd != 'C' && cmd != 'B')
		return -1; /* that is not a -M, -C nor -B option */

	opt1 = parse_num(&opt);
904 905 906 907 908 909 910 911 912 913 914 915
	if (cmd != 'B')
		opt2 = 0;
	else {
		if (*opt == 0)
			opt2 = 0;
		else if (*opt != '/')
			return -1; /* we expect -B80/99 or -B80 */
		else {
			opt++;
			opt2 = parse_num(&opt);
		}
	}
916 917
	if (*opt != 0)
		return -1;
918
	return opt1 | (opt2 << 16);
919 920
}

J
Junio C Hamano 已提交
921 922 923
struct diff_queue_struct diff_queued_diff;

void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
924
{
J
Junio C Hamano 已提交
925 926 927 928
	if (queue->alloc <= queue->nr) {
		queue->alloc = alloc_nr(queue->alloc);
		queue->queue = xrealloc(queue->queue,
					sizeof(dp) * queue->alloc);
929
	}
J
Junio C Hamano 已提交
930
	queue->queue[queue->nr++] = dp;
931 932
}

933
struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
J
Junio C Hamano 已提交
934 935
				 struct diff_filespec *one,
				 struct diff_filespec *two)
936
{
937
	struct diff_filepair *dp = xmalloc(sizeof(*dp));
938 939
	dp->one = one;
	dp->two = two;
940
	dp->score = 0;
941
	dp->status = 0;
942
	dp->source_stays = 0;
943
	dp->broken_pair = 0;
J
Junio C Hamano 已提交
944 945
	if (queue)
		diff_q(queue, dp);
946
	return dp;
947 948
}

949 950
void diff_free_filepair(struct diff_filepair *p)
{
951 952
	diff_free_filespec_data(p->one);
	diff_free_filespec_data(p->two);
J
Junio C Hamano 已提交
953 954
	free(p->one);
	free(p->two);
955 956 957
	free(p);
}

J
Junio C Hamano 已提交
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
/* This is different from find_unique_abbrev() in that
 * it needs to deal with 0{40} SHA1.
 */
const char *diff_unique_abbrev(const unsigned char *sha1, int len)
{
	int abblen;
	const char *abbrev;
	if (len == 40)
		return sha1_to_hex(sha1);

	abbrev = find_unique_abbrev(sha1, len);
	if (!abbrev) {
		if (!memcmp(sha1, null_sha1, 20)) {
			char *buf = sha1_to_hex(null_sha1);
			if (len < 37)
				strcpy(buf + len, "...");
			return buf;
		}
		else 
			return sha1_to_hex(sha1);
	}
	abblen = strlen(abbrev);
	if (abblen < 37) {
		static char hex[41];
		if (len < abblen && abblen <= len + 2)
			sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
		else
			sprintf(hex, "%s...", abbrev);
		return hex;
	}
	return sha1_to_hex(sha1);
}

991 992
static void diff_flush_raw(struct diff_filepair *p,
			   int line_termination,
993
			   int inter_name_termination,
J
Junio C Hamano 已提交
994
			   struct diff_options *options)
995
{
996 997
	int two_paths;
	char status[10];
J
Junio C Hamano 已提交
998
	int abbrev = options->abbrev;
999
	const char *path_one, *path_two;
J
Junio C Hamano 已提交
1000
	int output_format = options->output_format;
1001

1002 1003
	path_one = p->one->path;
	path_two = p->two->path;
1004
	if (line_termination) {
1005 1006
		path_one = quote_one(path_one);
		path_two = quote_one(path_two);
1007 1008
	}

J
Junio C Hamano 已提交
1009 1010 1011 1012 1013 1014 1015
	if (p->score)
		sprintf(status, "%c%03d", p->status,
			(int)(0.5 + p->score * 100.0/MAX_SCORE));
	else {
		status[0] = p->status;
		status[1] = 0;
	}
1016
	switch (p->status) {
1017 1018
	case DIFF_STATUS_COPIED:
	case DIFF_STATUS_RENAMED:
1019 1020
		two_paths = 1;
		break;
1021 1022
	case DIFF_STATUS_ADDED:
	case DIFF_STATUS_DELETED:
1023 1024
		two_paths = 0;
		break;
1025 1026 1027
	default:
		two_paths = 0;
		break;
J
Junio C Hamano 已提交
1028
	}
1029 1030
	if (output_format != DIFF_FORMAT_NAME_STATUS) {
		printf(":%06o %06o %s ",
J
Junio C Hamano 已提交
1031 1032 1033 1034
		       p->one->mode, p->two->mode,
		       diff_unique_abbrev(p->one->sha1, abbrev));
		printf("%s ",
		       diff_unique_abbrev(p->two->sha1, abbrev));
1035
	}
1036
	printf("%s%c%s", status, inter_name_termination, path_one);
1037
	if (two_paths)
1038
		printf("%c%s", inter_name_termination, path_two);
1039
	putchar(line_termination);
1040 1041 1042 1043
	if (path_one != p->one->path)
		free((void*)path_one);
	if (path_two != p->two->path)
		free((void*)path_two);
1044 1045
}

1046
static void diff_flush_name(struct diff_filepair *p,
1047
			    int inter_name_termination,
1048 1049
			    int line_termination)
{
1050 1051 1052 1053 1054 1055 1056 1057 1058
	char *path = p->two->path;

	if (line_termination)
		path = quote_one(p->two->path);
	else
		path = p->two->path;
	printf("%s%c", path, line_termination);
	if (p->two->path != path)
		free(path);
1059 1060
}

1061
int diff_unmodified_pair(struct diff_filepair *p)
1062
{
1063 1064
	/* This function is written stricter than necessary to support
	 * the currently implemented transformers, but the idea is to
1065
	 * let transformers to produce diff_filepairs any way they want,
1066
	 * and filter and clean them up here before producing the output.
1067
	 */
J
Junio C Hamano 已提交
1068 1069 1070 1071
	struct diff_filespec *one, *two;

	if (DIFF_PAIR_UNMERGED(p))
		return 0; /* unmerged is interesting */
1072

J
Junio C Hamano 已提交
1073 1074
	one = p->one;
	two = p->two;
1075

1076 1077 1078
	/* deletion, addition, mode or type change
	 * and rename are all interesting.
	 */
1079
	if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1080
	    DIFF_PAIR_MODE_CHANGED(p) ||
1081 1082
	    strcmp(one->path, two->path))
		return 0;
J
Junio C Hamano 已提交
1083

1084 1085
	/* both are valid and point at the same path.  that is, we are
	 * dealing with a change.
J
Junio C Hamano 已提交
1086
	 */
1087 1088 1089 1090 1091 1092
	if (one->sha1_valid && two->sha1_valid &&
	    !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
		return 1; /* no change */
	if (!one->sha1_valid && !two->sha1_valid)
		return 1; /* both look at the same file on the filesystem. */
	return 0;
J
Junio C Hamano 已提交
1093 1094
}

J
Junio C Hamano 已提交
1095
static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1096 1097 1098 1099 1100 1101 1102 1103
{
	if (diff_unmodified_pair(p))
		return;

	if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
	    (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
		return; /* no tree diffs in patch format */ 

J
Junio C Hamano 已提交
1104
	run_diff(p, o);
1105 1106
}

1107
int diff_queue_is_empty(void)
1108
{
1109
	struct diff_queue_struct *q = &diff_queued_diff;
1110 1111 1112 1113 1114
	int i;
	for (i = 0; i < q->nr; i++)
		if (!diff_unmodified_pair(q->queue[i]))
			return 0;
	return 1;
1115 1116
}

1117 1118 1119 1120
#if DIFF_DEBUG
void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
{
	fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
J
Jason Riedy 已提交
1121
		x, one ? one : "",
1122 1123 1124 1125 1126
		s->path,
		DIFF_FILE_VALID(s) ? "valid" : "invalid",
		s->mode,
		s->sha1_valid ? sha1_to_hex(s->sha1) : "");
	fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
J
Jason Riedy 已提交
1127
		x, one ? one : "",
1128 1129 1130 1131 1132 1133 1134
		s->size, s->xfrm_flags);
}

void diff_debug_filepair(const struct diff_filepair *p, int i)
{
	diff_debug_filespec(p->one, i, "one");
	diff_debug_filespec(p->two, i, "two");
1135
	fprintf(stderr, "score %d, status %c stays %d broken %d\n",
J
Jason Riedy 已提交
1136
		p->score, p->status ? p->status : '?',
1137
		p->source_stays, p->broken_pair);
1138 1139 1140
}

void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
J
Junio C Hamano 已提交
1141 1142
{
	int i;
1143 1144 1145
	if (msg)
		fprintf(stderr, "%s\n", msg);
	fprintf(stderr, "q->nr = %d\n", q->nr);
J
Junio C Hamano 已提交
1146 1147
	for (i = 0; i < q->nr; i++) {
		struct diff_filepair *p = q->queue[i];
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
		diff_debug_filepair(p, i);
	}
}
#endif

static void diff_resolve_rename_copy(void)
{
	int i, j;
	struct diff_filepair *p, *pp;
	struct diff_queue_struct *q = &diff_queued_diff;

	diff_debug_queue("resolve-rename-copy", q);

	for (i = 0; i < q->nr; i++) {
		p = q->queue[i];
1163
		p->status = 0; /* undecided */
1164
		if (DIFF_PAIR_UNMERGED(p))
1165
			p->status = DIFF_STATUS_UNMERGED;
1166
		else if (!DIFF_FILE_VALID(p->one))
1167
			p->status = DIFF_STATUS_ADDED;
1168
		else if (!DIFF_FILE_VALID(p->two))
1169
			p->status = DIFF_STATUS_DELETED;
1170
		else if (DIFF_PAIR_TYPE_CHANGED(p))
1171
			p->status = DIFF_STATUS_TYPE_CHANGED;
1172 1173 1174 1175 1176

		/* from this point on, we are dealing with a pair
		 * whose both sides are valid and of the same type, i.e.
		 * either in-place edit or rename/copy edit.
		 */
1177
		else if (DIFF_PAIR_RENAME(p)) {
1178
			if (p->source_stays) {
1179
				p->status = DIFF_STATUS_COPIED;
1180 1181 1182 1183
				continue;
			}
			/* See if there is some other filepair that
			 * copies from the same source as us.  If so
1184 1185 1186
			 * we are a copy.  Otherwise we are either a
			 * copy if the path stays, or a rename if it
			 * does not, but we already handled "stays" case.
1187
			 */
1188
			for (j = i + 1; j < q->nr; j++) {
1189 1190
				pp = q->queue[j];
				if (strcmp(pp->one->path, p->one->path))
1191
					continue; /* not us */
1192
				if (!DIFF_PAIR_RENAME(pp))
1193 1194
					continue; /* not a rename/copy */
				/* pp is a rename/copy from the same source */
1195
				p->status = DIFF_STATUS_COPIED;
1196
				break;
1197 1198
			}
			if (!p->status)
1199
				p->status = DIFF_STATUS_RENAMED;
1200
		}
1201 1202
		else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
			 p->one->mode != p->two->mode)
1203
			p->status = DIFF_STATUS_MODIFIED;
J
Junio C Hamano 已提交
1204 1205 1206
		else {
			/* This is a "no-change" entry and should not
			 * happen anymore, but prepare for broken callers.
1207
			 */
J
Junio C Hamano 已提交
1208 1209
			error("feeding unmodified %s to diffcore",
			      p->one->path);
1210
			p->status = DIFF_STATUS_UNKNOWN;
J
Junio C Hamano 已提交
1211
		}
J
Junio C Hamano 已提交
1212
	}
1213
	diff_debug_queue("resolve-rename-copy done", q);
1214 1215
}

J
Junio C Hamano 已提交
1216
void diff_flush(struct diff_options *options)
1217 1218 1219
{
	struct diff_queue_struct *q = &diff_queued_diff;
	int i;
1220
	int inter_name_termination = '\t';
J
Junio C Hamano 已提交
1221 1222
	int diff_output_format = options->output_format;
	int line_termination = options->line_termination;
1223

1224 1225
	if (!line_termination)
		inter_name_termination = 0;
1226

1227 1228
	for (i = 0; i < q->nr; i++) {
		struct diff_filepair *p = q->queue[i];
J
Junio C Hamano 已提交
1229
		if ((diff_output_format == DIFF_FORMAT_NO_OUTPUT) ||
1230
		    (p->status == DIFF_STATUS_UNKNOWN))
1231
			continue;
1232
		if (p->status == 0)
1233
			die("internal error in diff-resolve-rename-copy");
J
Junio C Hamano 已提交
1234
		switch (diff_output_format) {
1235
		case DIFF_FORMAT_PATCH:
J
Junio C Hamano 已提交
1236
			diff_flush_patch(p, options);
1237
			break;
1238
		case DIFF_FORMAT_RAW:
1239
		case DIFF_FORMAT_NAME_STATUS:
1240
			diff_flush_raw(p, line_termination,
1241
				       inter_name_termination,
J
Junio C Hamano 已提交
1242
				       options);
1243
			break;
1244
		case DIFF_FORMAT_NAME:
1245 1246 1247
			diff_flush_name(p,
					inter_name_termination,
					line_termination);
1248
			break;
1249
		}
1250
		diff_free_filepair(q->queue[i]);
1251
	}
1252 1253 1254
	free(q->queue);
	q->queue = NULL;
	q->nr = q->alloc = 0;
1255 1256
}

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
static void diffcore_apply_filter(const char *filter)
{
	int i;
	struct diff_queue_struct *q = &diff_queued_diff;
	struct diff_queue_struct outq;
	outq.queue = NULL;
	outq.nr = outq.alloc = 0;

	if (!filter)
		return;

1268
	if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
1269 1270 1271
		int found;
		for (i = found = 0; !found && i < q->nr; i++) {
			struct diff_filepair *p = q->queue[i];
1272 1273 1274 1275 1276 1277 1278
			if (((p->status == DIFF_STATUS_MODIFIED) &&
			     ((p->score &&
			       strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
			      (!p->score &&
			       strchr(filter, DIFF_STATUS_MODIFIED)))) ||
			    ((p->status != DIFF_STATUS_MODIFIED) &&
			     strchr(filter, p->status)))
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
				found++;
		}
		if (found)
			return;

		/* otherwise we will clear the whole queue
		 * by copying the empty outq at the end of this
		 * function, but first clear the current entries
		 * in the queue.
		 */
		for (i = 0; i < q->nr; i++)
			diff_free_filepair(q->queue[i]);
	}
	else {
		/* Only the matching ones */
		for (i = 0; i < q->nr; i++) {
			struct diff_filepair *p = q->queue[i];
1296 1297 1298 1299 1300 1301 1302 1303

			if (((p->status == DIFF_STATUS_MODIFIED) &&
			     ((p->score &&
			       strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
			      (!p->score &&
			       strchr(filter, DIFF_STATUS_MODIFIED)))) ||
			    ((p->status != DIFF_STATUS_MODIFIED) &&
			     strchr(filter, p->status)))
1304 1305 1306 1307 1308 1309 1310 1311 1312
				diff_q(&outq, p);
			else
				diff_free_filepair(p);
		}
	}
	free(q->queue);
	*q = outq;
}

J
Junio C Hamano 已提交
1313
void diffcore_std(struct diff_options *options)
1314
{
J
Junio C Hamano 已提交
1315 1316 1317 1318 1319
	if (options->paths && options->paths[0])
		diffcore_pathspec(options->paths);
	if (options->break_opt != -1)
		diffcore_break(options->break_opt);
	if (options->detect_rename)
1320
		diffcore_rename(options);
J
Junio C Hamano 已提交
1321
	if (options->break_opt != -1)
1322
		diffcore_merge_broken();
J
Junio C Hamano 已提交
1323 1324 1325 1326
	if (options->pickaxe)
		diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
	if (options->orderfile)
		diffcore_order(options->orderfile);
1327
	diff_resolve_rename_copy();
J
Junio C Hamano 已提交
1328
	diffcore_apply_filter(options->filter);
1329 1330 1331
}


J
Junio C Hamano 已提交
1332
void diffcore_std_no_resolve(struct diff_options *options)
1333
{
J
Junio C Hamano 已提交
1334 1335 1336 1337 1338
	if (options->pickaxe)
		diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
	if (options->orderfile)
		diffcore_order(options->orderfile);
	diffcore_apply_filter(options->filter);
1339 1340
}

J
Junio C Hamano 已提交
1341 1342
void diff_addremove(struct diff_options *options,
		    int addremove, unsigned mode,
1343 1344
		    const unsigned char *sha1,
		    const char *base, const char *path)
1345
{
1346
	char concatpath[PATH_MAX];
1347 1348 1349 1350 1351 1352 1353 1354 1355
	struct diff_filespec *one, *two;

	/* This may look odd, but it is a preparation for
	 * feeding "there are unchanged files which should
	 * not produce diffs, but when you are doing copy
	 * detection you would need them, so here they are"
	 * entries to the diff-core.  They will be prefixed
	 * with something like '=' or '*' (I haven't decided
	 * which but should not make any difference).
1356
	 * Feeding the same new and old to diff_change() 
1357 1358 1359
	 * also has the same effect.
	 * Before the final output happens, they are pruned after
	 * merged into rename/copy pairs as appropriate.
1360
	 */
J
Junio C Hamano 已提交
1361
	if (options->reverse_diff)
1362 1363
		addremove = (addremove == '+' ? '-' :
			     addremove == '-' ? '+' : addremove);
1364

1365 1366 1367 1368
	if (!path) path = "";
	sprintf(concatpath, "%s%s", base, path);
	one = alloc_filespec(concatpath);
	two = alloc_filespec(concatpath);
1369

1370 1371 1372 1373
	if (addremove != '+')
		fill_filespec(one, sha1, mode);
	if (addremove != '-')
		fill_filespec(two, sha1, mode);
1374

1375
	diff_queue(&diff_queued_diff, one, two);
1376 1377
}

J
Junio C Hamano 已提交
1378 1379
void diff_change(struct diff_options *options,
		 unsigned old_mode, unsigned new_mode,
1380 1381
		 const unsigned char *old_sha1,
		 const unsigned char *new_sha1,
1382 1383
		 const char *base, const char *path) 
{
1384
	char concatpath[PATH_MAX];
1385
	struct diff_filespec *one, *two;
1386

J
Junio C Hamano 已提交
1387
	if (options->reverse_diff) {
1388 1389 1390 1391 1392
		unsigned tmp;
		const unsigned char *tmp_c;
		tmp = old_mode; old_mode = new_mode; new_mode = tmp;
		tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
	}
1393 1394 1395 1396 1397 1398 1399
	if (!path) path = "";
	sprintf(concatpath, "%s%s", base, path);
	one = alloc_filespec(concatpath);
	two = alloc_filespec(concatpath);
	fill_filespec(one, old_sha1, old_mode);
	fill_filespec(two, new_sha1, new_mode);

1400
	diff_queue(&diff_queued_diff, one, two);
1401
}
1402

J
Junio C Hamano 已提交
1403 1404
void diff_unmerge(struct diff_options *options,
		  const char *path)
1405
{
J
Junio C Hamano 已提交
1406 1407 1408 1409
	struct diff_filespec *one, *two;
	one = alloc_filespec(path);
	two = alloc_filespec(path);
	diff_queue(&diff_queued_diff, one, two);
1410
}