diff.c 165 KB
Newer Older
Junio C Hamano's avatar
Junio C Hamano committed
1 2 3 4
/*
 * Copyright (C) 2005 Junio C Hamano
 */
#include "cache.h"
5
#include "config.h"
6
#include "tempfile.h"
Junio C Hamano's avatar
Junio C Hamano committed
7 8 9
#include "quote.h"
#include "diff.h"
#include "diffcore.h"
Junio C Hamano's avatar
Junio C Hamano committed
10
#include "delta.h"
Junio C Hamano's avatar
Junio C Hamano committed
11
#include "xdiff-interface.h"
12
#include "color.h"
13
#include "attr.h"
14
#include "run-command.h"
15
#include "utf8.h"
16
#include "userdiff.h"
17
#include "submodule-config.h"
18
#include "submodule.h"
19
#include "hashmap.h"
20
#include "ll-merge.h"
21
#include "string-list.h"
22
#include "argv-array.h"
23
#include "graph.h"
24
#include "packfile.h"
Junio C Hamano's avatar
Junio C Hamano committed
25

26 27 28 29 30 31
#ifdef NO_FAST_WORKING_DIRECTORY
#define FAST_WORKING_DIRECTORY 0
#else
#define FAST_WORKING_DIRECTORY 1
#endif

32
static int diff_detect_rename_default;
33
static int diff_indent_heuristic = 1;
34
static int diff_rename_limit_default = 400;
35
static int diff_suppress_blank_empty;
36
static int diff_use_color_default = -1;
37
static int diff_color_moved_default;
38
static int diff_context_default = 3;
39
static int diff_interhunk_context_default;
40
static const char *diff_word_regex_cfg;
41
static const char *external_diff_cmd_cfg;
42
static const char *diff_order_file_cfg;
43
int diff_auto_refresh_index = 1;
44
static int diff_mnemonic_prefix;
45
static int diff_no_prefix;
46
static int diff_stat_graph_width;
47
static int diff_dirstat_permille_default = 30;
48
static struct diff_options default_diff_options;
49
static long diff_algorithm;
50
static unsigned ws_error_highlight_default = WSEH_NEW;
Junio C Hamano's avatar
Junio C Hamano committed
51

52
static char diff_colors[][COLOR_MAXLEN] = {
53
	GIT_COLOR_RESET,
54
	GIT_COLOR_NORMAL,	/* CONTEXT */
55 56 57 58 59 60
	GIT_COLOR_BOLD,		/* METAINFO */
	GIT_COLOR_CYAN,		/* FRAGINFO */
	GIT_COLOR_RED,		/* OLD */
	GIT_COLOR_GREEN,	/* NEW */
	GIT_COLOR_YELLOW,	/* COMMIT */
	GIT_COLOR_BG_RED,	/* WHITESPACE */
61
	GIT_COLOR_NORMAL,	/* FUNCINFO */
62 63 64 65 66 67 68 69
	GIT_COLOR_BOLD_MAGENTA,	/* OLD_MOVED */
	GIT_COLOR_BOLD_BLUE,	/* OLD_MOVED ALTERNATIVE */
	GIT_COLOR_FAINT,	/* OLD_MOVED_DIM */
	GIT_COLOR_FAINT_ITALIC,	/* OLD_MOVED_ALTERNATIVE_DIM */
	GIT_COLOR_BOLD_CYAN,	/* NEW_MOVED */
	GIT_COLOR_BOLD_YELLOW,	/* NEW_MOVED ALTERNATIVE */
	GIT_COLOR_FAINT,	/* NEW_MOVED_DIM */
	GIT_COLOR_FAINT_ITALIC,	/* NEW_MOVED_ALTERNATIVE_DIM */
70 71
};

72 73 74 75 76
static NORETURN void die_want_option(const char *option_name)
{
	die(_("option '%s' requires a value"), option_name);
}

77
static int parse_diff_color_slot(const char *var)
78
{
79
	if (!strcasecmp(var, "context") || !strcasecmp(var, "plain"))
80
		return DIFF_CONTEXT;
81
	if (!strcasecmp(var, "meta"))
82
		return DIFF_METAINFO;
83
	if (!strcasecmp(var, "frag"))
84
		return DIFF_FRAGINFO;
85
	if (!strcasecmp(var, "old"))
86
		return DIFF_FILE_OLD;
87
	if (!strcasecmp(var, "new"))
88
		return DIFF_FILE_NEW;
89
	if (!strcasecmp(var, "commit"))
90
		return DIFF_COMMIT;
91
	if (!strcasecmp(var, "whitespace"))
92
		return DIFF_WHITESPACE;
93
	if (!strcasecmp(var, "func"))
94
		return DIFF_FUNCINFO;
95 96 97 98
	if (!strcasecmp(var, "oldmoved"))
		return DIFF_FILE_OLD_MOVED;
	if (!strcasecmp(var, "oldmovedalternative"))
		return DIFF_FILE_OLD_MOVED_ALT;
99 100 101 102
	if (!strcasecmp(var, "oldmoveddimmed"))
		return DIFF_FILE_OLD_MOVED_DIM;
	if (!strcasecmp(var, "oldmovedalternativedimmed"))
		return DIFF_FILE_OLD_MOVED_ALT_DIM;
103 104 105 106
	if (!strcasecmp(var, "newmoved"))
		return DIFF_FILE_NEW_MOVED;
	if (!strcasecmp(var, "newmovedalternative"))
		return DIFF_FILE_NEW_MOVED_ALT;
107 108 109 110
	if (!strcasecmp(var, "newmoveddimmed"))
		return DIFF_FILE_NEW_MOVED_DIM;
	if (!strcasecmp(var, "newmovedalternativedimmed"))
		return DIFF_FILE_NEW_MOVED_ALT_DIM;
111
	return -1;
112 113
}

114
static int parse_dirstat_params(struct diff_options *options, const char *params_string,
115
				struct strbuf *errmsg)
116
{
117 118 119 120
	char *params_copy = xstrdup(params_string);
	struct string_list params = STRING_LIST_INIT_NODUP;
	int ret = 0;
	int i;
121

122 123 124 125 126
	if (*params_copy)
		string_list_split_in_place(&params, params_copy, ',', -1);
	for (i = 0; i < params.nr; i++) {
		const char *p = params.items[i].string;
		if (!strcmp(p, "changes")) {
127 128
			options->flags.dirstat_by_line = 0;
			options->flags.dirstat_by_file = 0;
129
		} else if (!strcmp(p, "lines")) {
130 131
			options->flags.dirstat_by_line = 1;
			options->flags.dirstat_by_file = 0;
132
		} else if (!strcmp(p, "files")) {
133 134
			options->flags.dirstat_by_line = 0;
			options->flags.dirstat_by_file = 1;
135
		} else if (!strcmp(p, "noncumulative")) {
136
			options->flags.dirstat_cumulative = 0;
137
		} else if (!strcmp(p, "cumulative")) {
138
			options->flags.dirstat_cumulative = 1;
139 140
		} else if (isdigit(*p)) {
			char *end;
141 142
			int permille = strtoul(p, &end, 10) * 10;
			if (*end == '.' && isdigit(*++end)) {
143
				/* only use first digit */
144
				permille += *end - '0';
145
				/* .. and ignore any further digits */
146
				while (isdigit(*++end))
147 148
					; /* nothing */
			}
149
			if (!*end)
150 151
				options->dirstat_permille = permille;
			else {
152 153
				strbuf_addf(errmsg, _("  Failed to parse dirstat cut-off percentage '%s'\n"),
					    p);
154 155 156
				ret++;
			}
		} else {
157
			strbuf_addf(errmsg, _("  Unknown dirstat parameter '%s'\n"), p);
158
			ret++;
159
		}
160

161
	}
162 163
	string_list_clear(&params, 0);
	free(params_copy);
164
	return ret;
165 166
}

167 168 169
static int parse_submodule_params(struct diff_options *options, const char *value)
{
	if (!strcmp(value, "log"))
170
		options->submodule_format = DIFF_SUBMODULE_LOG;
171
	else if (!strcmp(value, "short"))
172
		options->submodule_format = DIFF_SUBMODULE_SHORT;
173 174
	else if (!strcmp(value, "diff"))
		options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF;
175 176 177 178 179
	else
		return -1;
	return 0;
}

180 181 182 183 184 185 186 187 188
static int git_config_rename(const char *var, const char *value)
{
	if (!value)
		return DIFF_DETECT_RENAME;
	if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
		return  DIFF_DETECT_COPY;
	return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
}

189
long parse_algorithm_value(const char *value)
190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
	if (!value)
		return -1;
	else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
		return 0;
	else if (!strcasecmp(value, "minimal"))
		return XDF_NEED_MINIMAL;
	else if (!strcasecmp(value, "patience"))
		return XDF_PATIENCE_DIFF;
	else if (!strcasecmp(value, "histogram"))
		return XDF_HISTOGRAM_DIFF;
	return -1;
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
static int parse_one_token(const char **arg, const char *token)
{
	const char *rest;
	if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
		*arg = rest;
		return 1;
	}
	return 0;
}

static int parse_ws_error_highlight(const char *arg)
{
	const char *orig_arg = arg;
	unsigned val = 0;

	while (*arg) {
		if (parse_one_token(&arg, "none"))
			val = 0;
		else if (parse_one_token(&arg, "default"))
			val = WSEH_NEW;
		else if (parse_one_token(&arg, "all"))
			val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
		else if (parse_one_token(&arg, "new"))
			val |= WSEH_NEW;
		else if (parse_one_token(&arg, "old"))
			val |= WSEH_OLD;
		else if (parse_one_token(&arg, "context"))
			val |= WSEH_CONTEXT;
		else {
			return -1 - (int)(arg - orig_arg);
		}
		if (*arg)
			arg++;
	}
	return val;
}

241 242 243 244 245 246
/*
 * These are to give UI layer defaults.
 * The core-level commands such as git-diff-files should
 * never be affected by the setting of diff.renames
 * the user happens to have in the configuration file.
 */
247 248
void init_diff_ui_defaults(void)
{
249
	diff_detect_rename_default = DIFF_DETECT_RENAME;
250 251
}

252 253
int git_diff_heuristic_config(const char *var, const char *value, void *cb)
{
254
	if (!strcmp(var, "diff.indentheuristic"))
255 256 257 258
		diff_indent_heuristic = git_config_bool(var, value);
	return 0;
}

259 260 261 262 263 264 265 266 267 268 269 270 271
static int parse_color_moved(const char *arg)
{
	switch (git_parse_maybe_bool(arg)) {
	case 0:
		return COLOR_MOVED_NO;
	case 1:
		return COLOR_MOVED_DEFAULT;
	default:
		break;
	}

	if (!strcmp(arg, "no"))
		return COLOR_MOVED_NO;
272 273
	else if (!strcmp(arg, "plain"))
		return COLOR_MOVED_PLAIN;
274 275 276 277
	else if (!strcmp(arg, "zebra"))
		return COLOR_MOVED_ZEBRA;
	else if (!strcmp(arg, "default"))
		return COLOR_MOVED_DEFAULT;
278 279
	else if (!strcmp(arg, "dimmed_zebra"))
		return COLOR_MOVED_ZEBRA_DIM;
280
	else
281
		return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'dimmed_zebra', 'plain'"));
282 283
}

284
int git_diff_ui_config(const char *var, const char *value, void *cb)
285
{
286
	if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
287
		diff_use_color_default = git_config_colorbool(var, value);
288 289
		return 0;
	}
290 291 292 293 294 295 296
	if (!strcmp(var, "diff.colormoved")) {
		int cm = parse_color_moved(value);
		if (cm < 0)
			return -1;
		diff_color_moved_default = cm;
		return 0;
	}
297 298 299 300 301 302
	if (!strcmp(var, "diff.context")) {
		diff_context_default = git_config_int(var, value);
		if (diff_context_default < 0)
			return -1;
		return 0;
	}
303 304 305 306 307 308
	if (!strcmp(var, "diff.interhunkcontext")) {
		diff_interhunk_context_default = git_config_int(var, value);
		if (diff_interhunk_context_default < 0)
			return -1;
		return 0;
	}
309
	if (!strcmp(var, "diff.renames")) {
310
		diff_detect_rename_default = git_config_rename(var, value);
311 312
		return 0;
	}
313 314 315 316
	if (!strcmp(var, "diff.autorefreshindex")) {
		diff_auto_refresh_index = git_config_bool(var, value);
		return 0;
	}
317 318 319 320
	if (!strcmp(var, "diff.mnemonicprefix")) {
		diff_mnemonic_prefix = git_config_bool(var, value);
		return 0;
	}
321 322 323 324
	if (!strcmp(var, "diff.noprefix")) {
		diff_no_prefix = git_config_bool(var, value);
		return 0;
	}
325 326 327 328
	if (!strcmp(var, "diff.statgraphwidth")) {
		diff_stat_graph_width = git_config_int(var, value);
		return 0;
	}
329 330
	if (!strcmp(var, "diff.external"))
		return git_config_string(&external_diff_cmd_cfg, var, value);
331 332
	if (!strcmp(var, "diff.wordregex"))
		return git_config_string(&diff_word_regex_cfg, var, value);
333 334
	if (!strcmp(var, "diff.orderfile"))
		return git_config_pathname(&diff_order_file_cfg, var, value);
335

336 337 338
	if (!strcmp(var, "diff.ignoresubmodules"))
		handle_ignore_submodules_arg(&default_diff_options, value);

339 340 341 342 343 344 345
	if (!strcmp(var, "diff.submodule")) {
		if (parse_submodule_params(&default_diff_options, value))
			warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
				value);
		return 0;
	}

346 347 348 349 350 351 352
	if (!strcmp(var, "diff.algorithm")) {
		diff_algorithm = parse_algorithm_value(value);
		if (diff_algorithm < 0)
			return -1;
		return 0;
	}

353 354 355 356 357 358 359 360
	if (!strcmp(var, "diff.wserrorhighlight")) {
		int val = parse_ws_error_highlight(value);
		if (val < 0)
			return -1;
		ws_error_highlight_default = val;
		return 0;
	}

361 362 363
	if (git_color_config(var, value, cb) < 0)
		return -1;

364
	return git_diff_basic_config(var, value, cb);
365 366
}

367
int git_diff_basic_config(const char *var, const char *value, void *cb)
368
{
369 370
	const char *name;

371 372 373 374 375
	if (!strcmp(var, "diff.renamelimit")) {
		diff_rename_limit_default = git_config_int(var, value);
		return 0;
	}

376 377
	if (userdiff_config(var, value) < 0)
		return -1;
378

379 380 381
	if (skip_prefix(var, "diff.color.", &name) ||
	    skip_prefix(var, "color.diff.", &name)) {
		int slot = parse_diff_color_slot(name);
382 383
		if (slot < 0)
			return 0;
384 385
		if (!value)
			return config_error_nonbool(var);
386
		return color_parse(value, diff_colors[slot]);
387
	}
388

389
	/* like GNU diff's --suppress-blank-empty option  */
390 391 392
	if (!strcmp(var, "diff.suppressblankempty") ||
			/* for backwards compatibility */
			!strcmp(var, "diff.suppress-blank-empty")) {
393 394 395 396
		diff_suppress_blank_empty = git_config_bool(var, value);
		return 0;
	}

397
	if (!strcmp(var, "diff.dirstat")) {
398
		struct strbuf errmsg = STRBUF_INIT;
399
		default_diff_options.dirstat_permille = diff_dirstat_permille_default;
400
		if (parse_dirstat_params(&default_diff_options, value, &errmsg))
401
			warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
402 403
				errmsg.buf);
		strbuf_release(&errmsg);
404
		diff_dirstat_permille_default = default_diff_options.dirstat_permille;
405 406 407
		return 0;
	}

408 409 410
	if (git_diff_heuristic_config(var, value, cb) < 0)
		return -1;

411
	return git_default_config(var, value, cb);
412 413
}

Junio C Hamano's avatar
Junio C Hamano committed
414 415 416 417
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);
418
	struct strbuf res = STRBUF_INIT;
Junio C Hamano's avatar
Junio C Hamano committed
419 420

	if (need_one + need_two) {
421 422 423 424 425 426 427
		strbuf_addch(&res, '"');
		quote_c_style(one, &res, NULL, 1);
		quote_c_style(two, &res, NULL, 1);
		strbuf_addch(&res, '"');
	} else {
		strbuf_addstr(&res, one);
		strbuf_addstr(&res, two);
Junio C Hamano's avatar
Junio C Hamano committed
428
	}
429
	return strbuf_detach(&res, NULL);
Junio C Hamano's avatar
Junio C Hamano committed
430 431 432 433 434 435 436 437 438 439
}

static const char *external_diff(void)
{
	static const char *external_diff_cmd = NULL;
	static int done_preparing = 0;

	if (done_preparing)
		return external_diff_cmd;
	external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
440 441
	if (!external_diff_cmd)
		external_diff_cmd = external_diff_cmd_cfg;
Junio C Hamano's avatar
Junio C Hamano committed
442 443 444 445
	done_preparing = 1;
	return external_diff_cmd;
}

446 447 448 449 450
/*
 * Keep track of files used for diffing. Sometimes such an entry
 * refers to a temporary file, sometimes to an existing file, and
 * sometimes to "/dev/null".
 */
Junio C Hamano's avatar
Junio C Hamano committed
451
static struct diff_tempfile {
452 453 454 455 456 457
	/*
	 * filename external diff should read from, or NULL if this
	 * entry is currently not in use:
	 */
	const char *name;

458
	char hex[GIT_MAX_HEXSZ + 1];
Junio C Hamano's avatar
Junio C Hamano committed
459
	char mode[10];
460 461 462 463 464

	/*
	 * If this diff_tempfile instance refers to a temporary file,
	 * this tempfile object is used to manage its lifetime.
	 */
465
	struct tempfile *tempfile;
Junio C Hamano's avatar
Junio C Hamano committed
466 467
} diff_temp[2];

468 469 470 471 472 473 474 475 476
struct emit_callback {
	int color_diff;
	unsigned ws_rule;
	int blank_at_eof_in_preimage;
	int blank_at_eof_in_postimage;
	int lno_in_preimage;
	int lno_in_postimage;
	const char **label_path;
	struct diff_words_data *diff_words;
477
	struct diff_options *opt;
478
	struct strbuf *header;
479 480
};

Junio C Hamano's avatar
Junio C Hamano committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
static int count_lines(const char *data, int size)
{
	int count, ch, completely_empty = 1, nl_just_seen = 0;
	count = 0;
	while (0 < size--) {
		ch = *data++;
		if (ch == '\n') {
			count++;
			nl_just_seen = 1;
			completely_empty = 0;
		}
		else {
			nl_just_seen = 0;
			completely_empty = 0;
		}
	}
	if (completely_empty)
		return 0;
	if (!nl_just_seen)
		count++; /* no trailing newline */
	return count;
}

504 505 506 507 508 509 510 511 512
static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
{
	if (!DIFF_FILE_VALID(one)) {
		mf->ptr = (char *)""; /* does not matter */
		mf->size = 0;
		return 0;
	}
	else if (diff_populate_filespec(one, 0))
		return -1;
513

514 515 516 517 518
	mf->ptr = one->data;
	mf->size = one->size;
	return 0;
}

519 520 521 522 523
/* like fill_mmfile, but only for size, so we can avoid retrieving blob */
static unsigned long diff_filespec_size(struct diff_filespec *one)
{
	if (!DIFF_FILE_VALID(one))
		return 0;
524
	diff_populate_filespec(one, CHECK_SIZE_ONLY);
525 526 527
	return one->size;
}

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
{
	char *ptr = mf->ptr;
	long size = mf->size;
	int cnt = 0;

	if (!size)
		return cnt;
	ptr += size - 1; /* pointing at the very end */
	if (*ptr != '\n')
		; /* incomplete line */
	else
		ptr--; /* skip the last LF */
	while (mf->ptr < ptr) {
		char *prev_eol;
		for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
			if (*prev_eol == '\n')
				break;
		if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
			break;
		cnt++;
		ptr = prev_eol - 1;
	}
	return cnt;
}

static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
			       struct emit_callback *ecbdata)
{
	int l1, l2, at;
	unsigned ws_rule = ecbdata->ws_rule;
	l1 = count_trailing_blank(mf1, ws_rule);
	l2 = count_trailing_blank(mf2, ws_rule);
	if (l2 <= l1) {
		ecbdata->blank_at_eof_in_preimage = 0;
		ecbdata->blank_at_eof_in_postimage = 0;
		return;
	}
	at = count_lines(mf1->ptr, mf1->size);
	ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;

	at = count_lines(mf2->ptr, mf2->size);
	ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
}

573
static void emit_line_0(struct diff_options *o, const char *set, const char *reset,
574
			int first, const char *line, int len)
575 576
{
	int has_trailing_newline, has_trailing_carriage_return;
577
	int nofirst;
578 579
	FILE *file = o->file;

580
	fputs(diff_line_prefix(o), file);
581

582 583 584 585 586 587 588 589 590 591 592 593 594 595
	if (len == 0) {
		has_trailing_newline = (first == '\n');
		has_trailing_carriage_return = (!has_trailing_newline &&
						(first == '\r'));
		nofirst = has_trailing_newline || has_trailing_carriage_return;
	} else {
		has_trailing_newline = (len > 0 && line[len-1] == '\n');
		if (has_trailing_newline)
			len--;
		has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
		if (has_trailing_carriage_return)
			len--;
		nofirst = 0;
	}
596

597 598 599 600 601 602 603
	if (len || !nofirst) {
		fputs(set, file);
		if (!nofirst)
			fputc(first, file);
		fwrite(line, len, 1, file);
		fputs(reset, file);
	}
604 605 606 607 608 609
	if (has_trailing_carriage_return)
		fputc('\r', file);
	if (has_trailing_newline)
		fputc('\n', file);
}

610
static void emit_line(struct diff_options *o, const char *set, const char *reset,
611 612
		      const char *line, int len)
{
613
	emit_line_0(o, set, reset, line[0], line+1, len-1);
614 615
}

616
enum diff_symbol {
617 618 619 620 621
	DIFF_SYMBOL_BINARY_DIFF_HEADER,
	DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
	DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
	DIFF_SYMBOL_BINARY_DIFF_BODY,
	DIFF_SYMBOL_BINARY_DIFF_FOOTER,
622 623 624 625
	DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
	DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
	DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
	DIFF_SYMBOL_STATS_LINE,
626
	DIFF_SYMBOL_WORD_DIFF,
627
	DIFF_SYMBOL_STAT_SEP,
628
	DIFF_SYMBOL_SUMMARY,
629 630 631 632 633 634 635
	DIFF_SYMBOL_SUBMODULE_ADD,
	DIFF_SYMBOL_SUBMODULE_DEL,
	DIFF_SYMBOL_SUBMODULE_UNTRACKED,
	DIFF_SYMBOL_SUBMODULE_MODIFIED,
	DIFF_SYMBOL_SUBMODULE_HEADER,
	DIFF_SYMBOL_SUBMODULE_ERROR,
	DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
636
	DIFF_SYMBOL_REWRITE_DIFF,
637
	DIFF_SYMBOL_BINARY_FILES,
638
	DIFF_SYMBOL_HEADER,
639 640
	DIFF_SYMBOL_FILEPAIR_PLUS,
	DIFF_SYMBOL_FILEPAIR_MINUS,
641 642
	DIFF_SYMBOL_WORDS_PORCELAIN,
	DIFF_SYMBOL_WORDS,
643
	DIFF_SYMBOL_CONTEXT,
644
	DIFF_SYMBOL_CONTEXT_INCOMPLETE,
645 646
	DIFF_SYMBOL_PLUS,
	DIFF_SYMBOL_MINUS,
647
	DIFF_SYMBOL_NO_LF_EOF,
648
	DIFF_SYMBOL_CONTEXT_FRAGINFO,
649
	DIFF_SYMBOL_CONTEXT_MARKER,
650 651
	DIFF_SYMBOL_SEPARATOR
};
652 653 654 655 656 657
/*
 * Flags for content lines:
 * 0..12 are whitespace rules
 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
 * 16 is marking if the line is blank at EOF
 */
658 659 660
#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF	(1<<16)
#define DIFF_SYMBOL_MOVED_LINE			(1<<17)
#define DIFF_SYMBOL_MOVED_LINE_ALT		(1<<18)
661
#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING	(1<<19)
662 663
#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
/*
 * This struct is used when we need to buffer the output of the diff output.
 *
 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
 * into the pre/post image file. This pointer could be a union with the
 * line pointer. By storing an offset into the file instead of the literal line,
 * we can decrease the memory footprint for the buffered output. At first we
 * may want to only have indirection for the content lines, but we could also
 * enhance the state for emitting prefabricated lines, e.g. the similarity
 * score line or hunk/file headers would only need to store a number or path
 * and then the output can be constructed later on depending on state.
 */
struct emitted_diff_symbol {
	const char *line;
	int len;
	int flags;
	enum diff_symbol s;
};
#define EMITTED_DIFF_SYMBOL_INIT {NULL}

struct emitted_diff_symbols {
	struct emitted_diff_symbol *buf;
	int nr, alloc;
};
#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}

static void append_emitted_diff_symbol(struct diff_options *o,
				       struct emitted_diff_symbol *e)
692
{
693 694 695 696 697 698 699 700 701
	struct emitted_diff_symbol *f;

	ALLOC_GROW(o->emitted_symbols->buf,
		   o->emitted_symbols->nr + 1,
		   o->emitted_symbols->alloc);
	f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];

	memcpy(f, e, sizeof(struct emitted_diff_symbol));
	f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
702 703
}

704 705 706 707 708 709 710 711 712 713 714
struct moved_entry {
	struct hashmap_entry ent;
	const struct emitted_diff_symbol *es;
	struct moved_entry *next_line;
};

static int moved_entry_cmp(const struct diff_options *diffopt,
			   const struct moved_entry *a,
			   const struct moved_entry *b,
			   const void *keydata)
{
715 716 717
	return !xdiff_compare_lines(a->es->line, a->es->len,
				    b->es->line, b->es->len,
				    diffopt->xdl_opts);
718 719 720 721 722 723 724 725
}

static struct moved_entry *prepare_entry(struct diff_options *o,
					 int line_no)
{
	struct moved_entry *ret = xmalloc(sizeof(*ret));
	struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];

726
	ret->ent.hash = xdiff_hash_string(l->line, l->len, o->xdl_opts);
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	ret->es = l;
	ret->next_line = NULL;

	return ret;
}

static void add_lines_to_move_detection(struct diff_options *o,
					struct hashmap *add_lines,
					struct hashmap *del_lines)
{
	struct moved_entry *prev_line = NULL;

	int n;
	for (n = 0; n < o->emitted_symbols->nr; n++) {
		struct hashmap *hm;
		struct moved_entry *key;

		switch (o->emitted_symbols->buf[n].s) {
		case DIFF_SYMBOL_PLUS:
			hm = add_lines;
			break;
		case DIFF_SYMBOL_MINUS:
			hm = del_lines;
			break;
		default:
			prev_line = NULL;
			continue;
		}

		key = prepare_entry(o, n);
		if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
			prev_line->next_line = key;

		hashmap_add(hm, key);
		prev_line = key;
	}
}

static int shrink_potential_moved_blocks(struct moved_entry **pmb,
					 int pmb_nr)
{
	int lp, rp;

	/* Shrink the set of potential block to the remaining running */
	for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
		while (lp < pmb_nr && pmb[lp])
			lp++;
		/* lp points at the first NULL now */

		while (rp > -1 && !pmb[rp])
			rp--;
		/* rp points at the last non-NULL */

		if (lp < pmb_nr && rp > -1 && lp < rp) {
			pmb[lp] = pmb[rp];
			pmb[rp] = NULL;
			rp--;
			lp++;
		}
	}

	/* Remember the number of running sets */
	return rp + 1;
}

792 793 794
/*
 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
 *
795 796
 * Otherwise, if the last block has fewer alphanumeric characters than
 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
797 798 799 800
 * that block.
 *
 * The last block consists of the (n - block_length)'th line up to but not
 * including the nth line.
801 802 803
 *
 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
 * Think of a way to unify them.
804 805 806
 */
static void adjust_last_block(struct diff_options *o, int n, int block_length)
{
807 808
	int i, alnum_count = 0;
	if (o->color_moved == COLOR_MOVED_PLAIN)
809
		return;
810 811 812 813 814 815 816 817 818 819
	for (i = 1; i < block_length + 1; i++) {
		const char *c = o->emitted_symbols->buf[n - i].line;
		for (; *c; c++) {
			if (!isalnum(*c))
				continue;
			alnum_count++;
			if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
				return;
		}
	}
820 821 822 823
	for (i = 1; i < block_length + 1; i++)
		o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
}

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
/* Find blocks of moved code, delegate actual coloring decision to helper */
static void mark_color_as_moved(struct diff_options *o,
				struct hashmap *add_lines,
				struct hashmap *del_lines)
{
	struct moved_entry **pmb = NULL; /* potentially moved blocks */
	int pmb_nr = 0, pmb_alloc = 0;
	int n, flipped_block = 1, block_length = 0;


	for (n = 0; n < o->emitted_symbols->nr; n++) {
		struct hashmap *hm = NULL;
		struct moved_entry *key;
		struct moved_entry *match = NULL;
		struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
		int i;

		switch (l->s) {
		case DIFF_SYMBOL_PLUS:
			hm = del_lines;
			key = prepare_entry(o, n);
			match = hashmap_get(hm, key, o);
			free(key);
			break;
		case DIFF_SYMBOL_MINUS:
			hm = add_lines;
			key = prepare_entry(o, n);
			match = hashmap_get(hm, key, o);
			free(key);
			break;
		default:
			flipped_block = 1;
		}

		if (!match) {
859
			adjust_last_block(o, n, block_length);
860 861 862 863 864 865 866
			pmb_nr = 0;
			block_length = 0;
			continue;
		}

		l->flags |= DIFF_SYMBOL_MOVED_LINE;

867 868 869
		if (o->color_moved == COLOR_MOVED_PLAIN)
			continue;

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
		/* Check any potential block runs, advance each or nullify */
		for (i = 0; i < pmb_nr; i++) {
			struct moved_entry *p = pmb[i];
			struct moved_entry *pnext = (p && p->next_line) ?
					p->next_line : NULL;
			if (pnext && !hm->cmpfn(o, pnext, match, NULL)) {
				pmb[i] = p->next_line;
			} else {
				pmb[i] = NULL;
			}
		}

		pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);

		if (pmb_nr == 0) {
			/*
			 * The current line is the start of a new block.
			 * Setup the set of potential blocks.
			 */
			for (; match; match = hashmap_get_next(hm, match)) {
				ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
				pmb[pmb_nr++] = match;
			}

			flipped_block = (flipped_block + 1) % 2;
895 896 897

			adjust_last_block(o, n, block_length);
			block_length = 0;
898 899
		}

900 901
		block_length++;

902 903 904
		if (flipped_block)
			l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
	}
905
	adjust_last_block(o, n, block_length);
906 907 908

	free(pmb);
}
909

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
  (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
static void dim_moved_lines(struct diff_options *o)
{
	int n;
	for (n = 0; n < o->emitted_symbols->nr; n++) {
		struct emitted_diff_symbol *prev = (n != 0) ?
				&o->emitted_symbols->buf[n - 1] : NULL;
		struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
		struct emitted_diff_symbol *next =
				(n < o->emitted_symbols->nr - 1) ?
				&o->emitted_symbols->buf[n + 1] : NULL;

		/* Not a plus or minus line? */
		if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
			continue;

		/* Not a moved line? */
		if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
			continue;

		/*
		 * If prev or next are not a plus or minus line,
		 * pretend they don't exist
		 */
		if (prev && prev->s != DIFF_SYMBOL_PLUS &&
			    prev->s != DIFF_SYMBOL_MINUS)
			prev = NULL;
		if (next && next->s != DIFF_SYMBOL_PLUS &&
			    next->s != DIFF_SYMBOL_MINUS)
			next = NULL;

		/* Inside a block? */
		if ((prev &&
		    (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
		    (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
		    (next &&
		    (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
		    (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
			l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
			continue;
		}

		/* Check if we are at an interesting bound: */
		if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
		    (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
		       (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
			continue;
		if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
		    (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
		       (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
			continue;

		/*
		 * The boundary to prev and next are not interesting,
		 * so this line is not interesting as a whole
		 */
		l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
	}
}

971 972 973 974
static void emit_line_ws_markup(struct diff_options *o,
				const char *set, const char *reset,
				const char *line, int len, char sign,
				unsigned ws_rule, int blank_at_eof)
975
{
976
	const char *ws = NULL;
977

978 979
	if (o->ws_error_highlight & ws_rule) {
		ws = diff_get_color_opt(o, DIFF_WHITESPACE);
980 981 982 983 984
		if (!*ws)
			ws = NULL;
	}

	if (!ws)
985 986
		emit_line_0(o, set, reset, sign, line, len);
	else if (blank_at_eof)
987
		/* Blank line at EOF - paint '+' as well */
988
		emit_line_0(o, ws, reset, sign, line, len);
989 990
	else {
		/* Emit just the prefix, then the rest. */
991 992 993
		emit_line_0(o, set, reset, sign, "", 0);
		ws_check_emit(line, len, ws_rule,
			      o->file, set, reset, ws);
994 995 996
	}
}

997 998
static void emit_diff_symbol_from_struct(struct diff_options *o,
					 struct emitted_diff_symbol *eds)
999
{
1000
	static const char *nneof = " No newline at end of file\n";
1001
	const char *context, *reset, *set, *meta, *fraginfo;
1002
	struct strbuf sb = STRBUF_INIT;
1003 1004 1005 1006 1007 1008

	enum diff_symbol s = eds->s;
	const char *line = eds->line;
	int len = eds->len;
	unsigned flags = eds->flags;

1009
	switch (s) {
1010 1011 1012 1013 1014 1015 1016
	case DIFF_SYMBOL_NO_LF_EOF:
		context = diff_get_color_opt(o, DIFF_CONTEXT);
		reset = diff_get_color_opt(o, DIFF_RESET);
		putc('\n', o->file);
		emit_line_0(o, context, reset, '\\',
			    nneof, strlen(nneof));
		break;
1017 1018 1019
	case DIFF_SYMBOL_SUBMODULE_HEADER:
	case DIFF_SYMBOL_SUBMODULE_ERROR:
	case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
1020
	case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
1021
	case DIFF_SYMBOL_SUMMARY:
1022
	case DIFF_SYMBOL_STATS_LINE:
1023
	case DIFF_SYMBOL_BINARY_DIFF_BODY:
1024 1025 1026
	case DIFF_SYMBOL_CONTEXT_FRAGINFO:
		emit_line(o, "", "", line, len);
		break;
1027
	case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
1028 1029 1030 1031 1032
	case DIFF_SYMBOL_CONTEXT_MARKER:
		context = diff_get_color_opt(o, DIFF_CONTEXT);
		reset = diff_get_color_opt(o, DIFF_RESET);
		emit_line(o, context, reset, line, len);
		break;
1033 1034 1035 1036 1037
	case DIFF_SYMBOL_SEPARATOR:
		fprintf(o->file, "%s%c",
			diff_line_prefix(o),
			o->line_termination);
		break;
1038 1039 1040 1041 1042 1043 1044
	case DIFF_SYMBOL_CONTEXT:
		set = diff_get_color_opt(o, DIFF_CONTEXT);
		reset = diff_get_color_opt(o, DIFF_RESET);
		emit_line_ws_markup(o, set, reset, line, len, ' ',
				    flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
		break;
	case DIFF_SYMBOL_PLUS:
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
		switch (flags & (DIFF_SYMBOL_MOVED_LINE |
				 DIFF_SYMBOL_MOVED_LINE_ALT |
				 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
		case DIFF_SYMBOL_MOVED_LINE |
		     DIFF_SYMBOL_MOVED_LINE_ALT |
		     DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
			set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
			break;
		case DIFF_SYMBOL_MOVED_LINE |
		     DIFF_SYMBOL_MOVED_LINE_ALT:
1055
			set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
1056 1057 1058 1059 1060 1061
			break;
		case DIFF_SYMBOL_MOVED_LINE |
		     DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
			set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
			break;
		case DIFF_SYMBOL_MOVED_LINE:
1062
			set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
1063 1064
			break;
		default:
1065
			set = diff_get_color_opt(o, DIFF_FILE_NEW);
1066
		}
1067 1068 1069 1070 1071 1072
		reset = diff_get_color_opt(o, DIFF_RESET);
		emit_line_ws_markup(o, set, reset, line, len, '+',
				    flags & DIFF_SYMBOL_CONTENT_WS_MASK,
				    flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
		break;
	case DIFF_SYMBOL_MINUS:
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
		switch (flags & (DIFF_SYMBOL_MOVED_LINE |
				 DIFF_SYMBOL_MOVED_LINE_ALT |
				 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
		case DIFF_SYMBOL_MOVED_LINE |
		     DIFF_SYMBOL_MOVED_LINE_ALT |
		     DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
			set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
			break;
		case DIFF_SYMBOL_MOVED_LINE |
		     DIFF_SYMBOL_MOVED_LINE_ALT:
1083
			set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
1084 1085 1086 1087 1088 1089
			break;
		case DIFF_SYMBOL_MOVED_LINE |
		     DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
			set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
			break;
		case DIFF_SYMBOL_MOVED_LINE:
1090
			set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
1091 1092
			break;
		default:
1093
			set = diff_get_color_opt(o, DIFF_FILE_OLD);
1094
		}
1095 1096 1097 1098
		reset = diff_get_color_opt(o, DIFF_RESET);
		emit_line_ws_markup(o, set, reset, line, len, '-',
				    flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
		break;
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	case DIFF_SYMBOL_WORDS_PORCELAIN:
		context = diff_get_color_opt(o, DIFF_CONTEXT);
		reset = diff_get_color_opt(o, DIFF_RESET);
		emit_line(o, context, reset, line, len);
		fputs("~\n", o->file);
		break;
	case DIFF_SYMBOL_WORDS:
		context = diff_get_color_opt(o, DIFF_CONTEXT);
		reset = diff_get_color_opt(o, DIFF_RESET);
		/*
		 * Skip the prefix character, if any.  With
		 * diff_suppress_blank_empty, there may be
		 * none.
		 */
		if (line[0] != '\n') {
			line++;
			len--;
		}
		emit_line(o, context, reset, line, len);
		break;
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
	case DIFF_SYMBOL_FILEPAIR_PLUS:
		meta = diff_get_color_opt(o, DIFF_METAINFO);
		reset = diff_get_color_opt(o, DIFF_RESET);
		fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
			line, reset,
			strchr(line, ' ') ? "\t" : "");
		break;
	case DIFF_SYMBOL_FILEPAIR_MINUS:
		meta = diff_get_color_opt(o, DIFF_METAINFO);
		reset = diff_get_color_opt(o, DIFF_RESET);
		fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
			line, reset,
			strchr(line, ' ') ? "\t" : "");
		break;
1133
	case DIFF_SYMBOL_BINARY_FILES:
1134 1135 1136
	case DIFF_SYMBOL_HEADER:
		fprintf(o->file, "%s", line);
		break;
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	case DIFF_SYMBOL_BINARY_DIFF_HEADER:
		fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
		break;
	case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
		fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
		break;
	case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
		fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
		break;
	case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
		fputs(diff_line_prefix(o), o->file);
		fputc('\n', o->file);
		break;
1150 1151 1152 1153 1154
	case DIFF_SYMBOL_REWRITE_DIFF:
		fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
		reset = diff_get_color_opt(o, DIFF_RESET);
		emit_line(o, fraginfo, reset, line, len);
		break;
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	case DIFF_SYMBOL_SUBMODULE_ADD:
		set = diff_get_color_opt(o, DIFF_FILE_NEW);
		reset = diff_get_color_opt(o, DIFF_RESET);
		emit_line(o, set, reset, line, len);
		break;
	case DIFF_SYMBOL_SUBMODULE_DEL:
		set = diff_get_color_opt(o, DIFF_FILE_OLD);
		reset = diff_get_color_opt(o, DIFF_RESET);
		emit_line(o, set, reset, line, len);
		break;
	case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
		fprintf(o->file, "%sSubmodule %s contains untracked content\n",
			diff_line_prefix(o), line);
		break;
	case DIFF_SYMBOL_SUBMODULE_MODIFIED:
		fprintf(o->file, "%sSubmodule %s contains modified content\n",
			diff_line_prefix(o), line);
		break;
1173 1174 1175 1176 1177 1178 1179
	case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
		emit_line(o, "", "", " 0 files changed\n",
			  strlen(" 0 files changed\n"));
		break;
	case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
		emit_line(o, "", "", " ...\n", strlen(" ...\n"));
		break;
1180 1181 1182
	case DIFF_SYMBOL_WORD_DIFF:
		fprintf(o->file, "%.*s", len, line);
		break;
1183 1184 1185
	case DIFF_SYMBOL_STAT_SEP:
		fputs(o->stat_sep, o->file);
		break;
1186 1187 1188
	default:
		die("BUG: unknown diff symbol");
	}
1189
	strbuf_release(&sb);
1190 1191
}

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
			     const char *line, int len, unsigned flags)
{
	struct emitted_diff_symbol e = {line, len, flags, s};

	if (o->emitted_symbols)
		append_emitted_diff_symbol(o, &e);
	else
		emit_diff_symbol_from_struct(o, &e);
}

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
void diff_emit_submodule_del(struct diff_options *o, const char *line)
{
	emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
}

void diff_emit_submodule_add(struct diff_options *o, const char *line)
{
	emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
}

void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
{
	emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
			 path, strlen(path), 0);
}

void diff_emit_submodule_modified(struct diff_options *o, const char *path)
{
	emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
			 path, strlen(path), 0);
}

void diff_emit_submodule_header(struct diff_options *o, const char *header)
{
	emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
			 header, strlen(header), 0);
}

void diff_emit_submodule_error(struct diff_options *o, const char *err)
{
	emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
}

void diff_emit_submodule_pipethrough(struct diff_options *o,
				     const char *line, int len)
{
	emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
}

1242 1243 1244 1245 1246 1247 1248 1249
static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
{
	if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
	      ecbdata->blank_at_eof_in_preimage &&
	      ecbdata->blank_at_eof_in_postimage &&
	      ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
	      ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
		return 0;
1250
	return ws_blank_line(line, len, ecbdata->ws_rule);
1251 1252
}

1253
static void emit_add_line(const char *reset,
1254 1255 1256
			  struct emit_callback *ecbdata,
			  const char *line, int len)
{
1257 1258 1259 1260 1261
	unsigned flags = WSEH_NEW | ecbdata->ws_rule;
	if (new_blank_line_at_eof(ecbdata, line, len))
		flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;

	emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
1262
}
1263

1264 1265 1266 1267
static void emit_del_line(const char *reset,
			  struct emit_callback *ecbdata,
			  const char *line, int len)
{
1268 1269
	unsigned flags = WSEH_OLD | ecbdata->ws_rule;
	emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
1270 1271 1272 1273 1274 1275
}

static void emit_context_line(const char *reset,
			      struct emit_callback *ecbdata,
			      const char *line, int len)
{
1276 1277
	unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
	emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
1278 1279
}

1280 1281 1282
static void emit_hunk_header(struct emit_callback *ecbdata,
			     const char *line, int len)
{
1283
	const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
1284 1285 1286 1287 1288
	const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
	const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
	const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
	static const char atat[2] = { '@', '@' };
	const char *cp, *ep;
Bo Yang's avatar
Bo Yang committed
1289 1290 1291
	struct strbuf msgbuf = STRBUF_INIT;
	int org_len = len;
	int i = 1;
1292 1293 1294 1295 1296 1297 1298 1299

	/*
	 * As a hunk header must begin with "@@ -<old>, +<new> @@",
	 * it always is at least 10 bytes long.
	 */
	if (len < 10 ||
	    memcmp(line, atat, 2) ||
	    !(ep = memmem(line + 2, len - 2, atat, 2))) {
1300
		emit_diff_symbol(ecbdata->opt,
1301
				 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
1302 1303 1304 1305 1306
		return;
	}
	ep += 2; /* skip over @@ */

	/* The hunk header in fraginfo color */
1307
	strbuf_addstr(&msgbuf, frag);
Bo Yang's avatar
Bo Yang committed
1308
	strbuf_add(&msgbuf, line, ep - line);
1309
	strbuf_addstr(&msgbuf, reset);
Bo Yang's avatar
Bo Yang committed
1310 1311 1312 1313 1314 1315 1316

	/*
	 * trailing "\r\n"
	 */
	for ( ; i < 3; i++)
		if (line[len - i] == '\r' || line[len - i] == '\n')
			len--;
1317 1318 1319 1320 1321

	/* blank before the func header */
	for (cp = ep; ep - line < len; ep++)
		if (*ep != ' ' && *ep != '\t')
			break;
Bo Yang's avatar
Bo Yang committed
1322
	if (ep != cp) {
1323
		strbuf_addstr(&msgbuf, context);
Bo Yang's avatar
Bo Yang committed
1324
		strbuf_add(&msgbuf, cp, ep - cp);
1325
		strbuf_addstr(&msgbuf, reset);
Bo Yang's avatar
Bo Yang committed
1326 1327 1328
	}

	if (ep < line + len) {
1329
		strbuf_addstr(&msgbuf, func);
Bo Yang's avatar
Bo Yang committed
1330
		strbuf_add(&msgbuf, ep, line + len - ep);
1331
		strbuf_addstr(&msgbuf, reset);
Bo Yang's avatar
Bo Yang committed
1332
	}
1333

Bo Yang's avatar
Bo Yang committed
1334
	strbuf_add(&msgbuf, line + len, org_len - len);
1335
	strbuf_complete_line(&msgbuf);
1336
	emit_diff_symbol(ecbdata->opt,
1337
			 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
Bo Yang's avatar
Bo Yang committed
1338
	strbuf_release(&msgbuf);
1339 1340
}

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
static struct diff_tempfile *claim_diff_tempfile(void) {
	int i;
	for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
		if (!diff_temp[i].name)
			return diff_temp + i;
	die("BUG: diff is failing to clean up its tempfiles");
}

static void remove_tempfile(void)
{
	int i;
1352
	for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
1353
		if (is_tempfile_active(diff_temp[i].tempfile))
1354
			delete_tempfile(&diff_temp[i].tempfile);
1355 1356
		diff_temp[i].name = NULL;
	}
1357 1358
}

1359
static void add_line_count(struct strbuf *out, int count)
Junio C Hamano's avatar
Junio C Hamano committed
1360 1361 1362
{
	switch (count) {
	case 0:
1363
		strbuf_addstr(out, "0,0");
Junio C Hamano's avatar
Junio C Hamano committed
1364 1365
		break;
	case 1:
1366
		strbuf_addstr(out, "1");
Junio C Hamano's avatar
Junio C Hamano committed
1367 1368
		break;
	default:
1369
		strbuf_addf(out, "1,%d", count);
Junio C Hamano's avatar
Junio C Hamano committed
1370 1371 1372 1373
		break;
	}
}

1374 1375
static void emit_rewrite_lines(struct emit_callback *ecb,
			       int prefix, const char *data, int size)
Junio C Hamano's avatar
Junio C Hamano committed
1376
{
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
	const char *endp = NULL;
	const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);

	while (0 < size) {
		int len;

		endp = memchr(data, '\n', size);
		len = endp ? (endp - data + 1) : size;
		if (prefix != '+') {
			ecb->lno_in_preimage++;
1387
			emit_del_line(reset, ecb, data, len);
1388 1389 1390
		} else {
			ecb->lno_in_postimage++;
			emit_add_line(reset, ecb, data, len);
1391
		}
1392 1393 1394
		size -= len;
		data += len;
	}
1395
	if (!endp)
1396
		emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
Junio C Hamano's avatar
Junio C Hamano committed
1397 1398 1399 1400 1401
}

static void emit_rewrite_diff(const char *name_a,
			      const char *name_b,
			      struct diff_filespec *one,
1402
			      struct diff_filespec *two,
Jeff King's avatar
Jeff King committed
1403 1404
			      struct userdiff_driver *textconv_one,
			      struct userdiff_driver *textconv_two,
1405
			      struct diff_options *o)
Junio C Hamano's avatar
Junio C Hamano committed
1406 1407
{
	int lc_a, lc_b;
1408
	static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
1409
	const char *a_prefix, *b_prefix;
1410
	char *data_one, *data_two;
1411
	size_t size_one, size_two;
1412
	struct emit_callback ecbdata;
1413
	struct strbuf out = STRBUF_INIT;
1414

1415
	if (diff_mnemonic_prefix && o->flags.reverse_diff) {
1416 1417 1418 1419 1420 1421
		a_prefix = o->b_prefix;
		b_prefix = o->a_prefix;
	} else {
		a_prefix = o->a_prefix;
		b_prefix = o->b_prefix;
	}
1422

Junio C Hamano's avatar
Junio C Hamano committed
1423 1424
	name_a += (*name_a == '/');
	name_b += (*name_b == '/');
1425

1426 1427
	strbuf_reset(&a_name);
	strbuf_reset(&b_name);
1428 1429
	quote_two_c_style(&a_name, a_prefix, name_a, 0);
	quote_two_c_style(&b_name, b_prefix, name_b, 0);
1430

1431 1432
	size_one = fill_textconv(textconv_one, one, &data_one);
	size_two = fill_textconv(textconv_two, two, &data_two);
1433

1434
	memset(&ecbdata, 0, sizeof(ecbdata));
1435
	ecbdata.color_diff = want_color(o->use_color);
1436
	ecbdata.ws_rule = whitespace_rule(name_b);
1437
	ecbdata.opt = o;
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
	if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
		mmfile_t mf1, mf2;
		mf1.ptr = (char *)data_one;
		mf2.ptr = (char *)data_two;
		mf1.size = size_one;
		mf2.size = size_two;
		check_blank_at_eof(&mf1, &mf2, &ecbdata);
	}
	ecbdata.lno_in_preimage = 1;
	ecbdata.lno_in_postimage = 1;

1449 1450
	lc_a = count_lines(data_one, size_one);
	lc_b = count_lines(data_two, size_two);
1451 1452 1453 1454 1455 1456

	emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
			 a_name.buf, a_name.len, 0);
	emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
			 b_name.buf, b_name.len, 0);

1457
	strbuf_addstr(&out, "@@ -");
1458
	if (!o->irreversible_delete)
1459
		add_line_count(&out, lc_a);
1460
	else
1461 1462 1463 1464 1465 1466 1467
		strbuf_addstr(&out, "?,?");
	strbuf_addstr(&out, " +");
	add_line_count(&out, lc_b);
	strbuf_addstr(&out, " @@\n");
	emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
	strbuf_release(&out);

1468
	if (lc_a && !o->irreversible_delete)
1469
		emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
Junio C Hamano's avatar
Junio C Hamano committed
1470
	if (lc_b)
1471
		emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
1472
	if (textconv_one)
1473
		free((char *)data_one);
1474
	if (textconv_two)
1475
		free((char *)data_two);
Junio C Hamano's avatar
Junio C Hamano committed
1476 1477
}