diff: fix whitespace-skipping with --color-moved
[git/git.git] / diff.c
CommitLineData
6973dcae
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
6973dcae 4#include "cache.h"
b2141fc1 5#include "config.h"
284098f1 6#include "tempfile.h"
6973dcae
JH
7#include "quote.h"
8#include "diff.h"
9#include "diffcore.h"
051308f6 10#include "delta.h"
6973dcae 11#include "xdiff-interface.h"
7c92fe0e 12#include "color.h"
8c701249 13#include "attr.h"
d5535ec7 14#include "run-command.h"
23707811 15#include "utf8.h"
be58e70d 16#include "userdiff.h"
851e18c3 17#include "submodule-config.h"
752c0c24 18#include "submodule.h"
2e2d5ac1 19#include "hashmap.h"
a757c646 20#include "ll-merge.h"
02e8ca0e 21#include "string-list.h"
82fbf269 22#include "argv-array.h"
660e113c 23#include "graph.h"
150e3001 24#include "packfile.h"
6973dcae 25
1510fea7
SP
26#ifdef NO_FAST_WORKING_DIRECTORY
27#define FAST_WORKING_DIRECTORY 0
28#else
29#define FAST_WORKING_DIRECTORY 1
30#endif
31
96f1e58f 32static int diff_detect_rename_default;
33de7163 33static int diff_indent_heuristic = 1;
92c57e5c 34static int diff_rename_limit_default = 400;
a624eaa7 35static int diff_suppress_blank_empty;
d2aea137 36static int diff_use_color_default = -1;
2e2d5ac1 37static int diff_color_moved_default;
6468a4e5 38static int diff_context_default = 3;
c4888677 39static int diff_interhunk_context_default;
98a4d87b 40static const char *diff_word_regex_cfg;
cbe02100 41static const char *external_diff_cmd_cfg;
6d8940b5 42static const char *diff_order_file_cfg;
aecbf914 43int diff_auto_refresh_index = 1;
a5a818ee 44static int diff_mnemonic_prefix;
f89504dd 45static int diff_no_prefix;
df44483a 46static int diff_stat_graph_width;
712d2c7d 47static int diff_dirstat_permille_default = 30;
be4f2b40 48static struct diff_options default_diff_options;
07ab4dec 49static long diff_algorithm;
a17505f2 50static unsigned ws_error_highlight_default = WSEH_NEW;
6973dcae 51
7c92fe0e 52static char diff_colors[][COLOR_MAXLEN] = {
dc6ebd4c 53 GIT_COLOR_RESET,
8dbf3eb6 54 GIT_COLOR_NORMAL, /* CONTEXT */
dc6ebd4c
AL
55 GIT_COLOR_BOLD, /* METAINFO */
56 GIT_COLOR_CYAN, /* FRAGINFO */
57 GIT_COLOR_RED, /* OLD */
58 GIT_COLOR_GREEN, /* NEW */
59 GIT_COLOR_YELLOW, /* COMMIT */
60 GIT_COLOR_BG_RED, /* WHITESPACE */
89cb73a1 61 GIT_COLOR_NORMAL, /* FUNCINFO */
86b452e2
SB
62 GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
63 GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
64 GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
65 GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
66 GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
67 GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
68 GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
69 GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
cd112cef
JS
70};
71
a2f05c94
JNA
72static NORETURN void die_want_option(const char *option_name)
73{
74 die(_("option '%s' requires a value"), option_name);
75}
76
9e1a5ebe 77static int parse_diff_color_slot(const char *var)
801235c5 78{
74b15bfb 79 if (!strcasecmp(var, "context") || !strcasecmp(var, "plain"))
8dbf3eb6 80 return DIFF_CONTEXT;
9e1a5ebe 81 if (!strcasecmp(var, "meta"))
801235c5 82 return DIFF_METAINFO;
9e1a5ebe 83 if (!strcasecmp(var, "frag"))
801235c5 84 return DIFF_FRAGINFO;
9e1a5ebe 85 if (!strcasecmp(var, "old"))
801235c5 86 return DIFF_FILE_OLD;
9e1a5ebe 87 if (!strcasecmp(var, "new"))
801235c5 88 return DIFF_FILE_NEW;
9e1a5ebe 89 if (!strcasecmp(var, "commit"))
ce436973 90 return DIFF_COMMIT;
9e1a5ebe 91 if (!strcasecmp(var, "whitespace"))
448c3ef1 92 return DIFF_WHITESPACE;
9e1a5ebe 93 if (!strcasecmp(var, "func"))
89cb73a1 94 return DIFF_FUNCINFO;
2e2d5ac1
SB
95 if (!strcasecmp(var, "oldmoved"))
96 return DIFF_FILE_OLD_MOVED;
97 if (!strcasecmp(var, "oldmovedalternative"))
98 return DIFF_FILE_OLD_MOVED_ALT;
86b452e2
SB
99 if (!strcasecmp(var, "oldmoveddimmed"))
100 return DIFF_FILE_OLD_MOVED_DIM;
101 if (!strcasecmp(var, "oldmovedalternativedimmed"))
102 return DIFF_FILE_OLD_MOVED_ALT_DIM;
2e2d5ac1
SB
103 if (!strcasecmp(var, "newmoved"))
104 return DIFF_FILE_NEW_MOVED;
105 if (!strcasecmp(var, "newmovedalternative"))
106 return DIFF_FILE_NEW_MOVED_ALT;
86b452e2
SB
107 if (!strcasecmp(var, "newmoveddimmed"))
108 return DIFF_FILE_NEW_MOVED_DIM;
109 if (!strcasecmp(var, "newmovedalternativedimmed"))
110 return DIFF_FILE_NEW_MOVED_ALT_DIM;
8b8e8624 111 return -1;
801235c5
JH
112}
113
02e8ca0e 114static int parse_dirstat_params(struct diff_options *options, const char *params_string,
51670fc8 115 struct strbuf *errmsg)
333f3fb0 116{
02e8ca0e
MH
117 char *params_copy = xstrdup(params_string);
118 struct string_list params = STRING_LIST_INIT_NODUP;
119 int ret = 0;
120 int i;
51670fc8 121
02e8ca0e
MH
122 if (*params_copy)
123 string_list_split_in_place(&params, params_copy, ',', -1);
124 for (i = 0; i < params.nr; i++) {
125 const char *p = params.items[i].string;
126 if (!strcmp(p, "changes")) {
1c57a627
JH
127 DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
128 DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
02e8ca0e 129 } else if (!strcmp(p, "lines")) {
1c57a627 130 DIFF_OPT_SET(options, DIRSTAT_BY_LINE);
333f3fb0 131 DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
02e8ca0e 132 } else if (!strcmp(p, "files")) {
1c57a627 133 DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
333f3fb0 134 DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
02e8ca0e 135 } else if (!strcmp(p, "noncumulative")) {
333f3fb0 136 DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
02e8ca0e 137 } else if (!strcmp(p, "cumulative")) {
333f3fb0
JH
138 DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
139 } else if (isdigit(*p)) {
140 char *end;
51670fc8
JH
141 int permille = strtoul(p, &end, 10) * 10;
142 if (*end == '.' && isdigit(*++end)) {
712d2c7d 143 /* only use first digit */
51670fc8 144 permille += *end - '0';
712d2c7d 145 /* .. and ignore any further digits */
51670fc8 146 while (isdigit(*++end))
712d2c7d
JH
147 ; /* nothing */
148 }
02e8ca0e 149 if (!*end)
51670fc8
JH
150 options->dirstat_permille = permille;
151 else {
02e8ca0e
MH
152 strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
153 p);
51670fc8
JH
154 ret++;
155 }
156 } else {
02e8ca0e 157 strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
51670fc8 158 ret++;
333f3fb0 159 }
51670fc8 160
333f3fb0 161 }
02e8ca0e
MH
162 string_list_clear(&params, 0);
163 free(params_copy);
51670fc8 164 return ret;
333f3fb0
JH
165}
166
c47ef57c
RR
167static int parse_submodule_params(struct diff_options *options, const char *value)
168{
169 if (!strcmp(value, "log"))
61cfbc05 170 options->submodule_format = DIFF_SUBMODULE_LOG;
c47ef57c 171 else if (!strcmp(value, "short"))
61cfbc05 172 options->submodule_format = DIFF_SUBMODULE_SHORT;
fd47ae6a
JK
173 else if (!strcmp(value, "diff"))
174 options->submodule_format = DIFF_SUBMODULE_INLINE_DIFF;
c47ef57c
RR
175 else
176 return -1;
177 return 0;
178}
179
cced5fbc
LT
180static int git_config_rename(const char *var, const char *value)
181{
182 if (!value)
183 return DIFF_DETECT_RENAME;
184 if (!strcasecmp(value, "copies") || !strcasecmp(value, "copy"))
185 return DIFF_DETECT_COPY;
186 return git_config_bool(var,value) ? DIFF_DETECT_RENAME : 0;
187}
188
07924d4d 189long parse_algorithm_value(const char *value)
07ab4dec
MP
190{
191 if (!value)
192 return -1;
193 else if (!strcasecmp(value, "myers") || !strcasecmp(value, "default"))
194 return 0;
195 else if (!strcasecmp(value, "minimal"))
196 return XDF_NEED_MINIMAL;
197 else if (!strcasecmp(value, "patience"))
198 return XDF_PATIENCE_DIFF;
199 else if (!strcasecmp(value, "histogram"))
200 return XDF_HISTOGRAM_DIFF;
201 return -1;
202}
203
0b4b42e7
JH
204static int parse_one_token(const char **arg, const char *token)
205{
206 const char *rest;
207 if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
208 *arg = rest;
209 return 1;
210 }
211 return 0;
212}
213
214static int parse_ws_error_highlight(const char *arg)
215{
216 const char *orig_arg = arg;
217 unsigned val = 0;
218
219 while (*arg) {
220 if (parse_one_token(&arg, "none"))
221 val = 0;
222 else if (parse_one_token(&arg, "default"))
223 val = WSEH_NEW;
224 else if (parse_one_token(&arg, "all"))
225 val = WSEH_NEW | WSEH_OLD | WSEH_CONTEXT;
226 else if (parse_one_token(&arg, "new"))
227 val |= WSEH_NEW;
228 else if (parse_one_token(&arg, "old"))
229 val |= WSEH_OLD;
230 else if (parse_one_token(&arg, "context"))
231 val |= WSEH_CONTEXT;
232 else {
233 return -1 - (int)(arg - orig_arg);
234 }
235 if (*arg)
236 arg++;
237 }
238 return val;
239}
240
83ad63cf
JH
241/*
242 * These are to give UI layer defaults.
243 * The core-level commands such as git-diff-files should
244 * never be affected by the setting of diff.renames
245 * the user happens to have in the configuration file.
246 */
5404c116
MM
247void init_diff_ui_defaults(void)
248{
249 diff_detect_rename_default = 1;
250}
251
5b162879
MH
252int git_diff_heuristic_config(const char *var, const char *value, void *cb)
253{
3cde4e02 254 if (!strcmp(var, "diff.indentheuristic"))
5b162879 255 diff_indent_heuristic = git_config_bool(var, value);
5b162879
MH
256 return 0;
257}
258
2e2d5ac1
SB
259static int parse_color_moved(const char *arg)
260{
261 switch (git_parse_maybe_bool(arg)) {
262 case 0:
263 return COLOR_MOVED_NO;
264 case 1:
265 return COLOR_MOVED_DEFAULT;
266 default:
267 break;
268 }
269
270 if (!strcmp(arg, "no"))
271 return COLOR_MOVED_NO;
176841f0
SB
272 else if (!strcmp(arg, "plain"))
273 return COLOR_MOVED_PLAIN;
2e2d5ac1
SB
274 else if (!strcmp(arg, "zebra"))
275 return COLOR_MOVED_ZEBRA;
276 else if (!strcmp(arg, "default"))
277 return COLOR_MOVED_DEFAULT;
86b452e2
SB
278 else if (!strcmp(arg, "dimmed_zebra"))
279 return COLOR_MOVED_ZEBRA_DIM;
2e2d5ac1 280 else
86b452e2 281 return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'dimmed_zebra', 'plain'"));
2e2d5ac1
SB
282}
283
ef90d6d4 284int git_diff_ui_config(const char *var, const char *value, void *cb)
801235c5 285{
a159ca0c 286 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
e269eb79 287 diff_use_color_default = git_config_colorbool(var, value);
801235c5
JH
288 return 0;
289 }
2e2d5ac1
SB
290 if (!strcmp(var, "diff.colormoved")) {
291 int cm = parse_color_moved(value);
292 if (cm < 0)
293 return -1;
294 diff_color_moved_default = cm;
295 return 0;
296 }
6468a4e5
JM
297 if (!strcmp(var, "diff.context")) {
298 diff_context_default = git_config_int(var, value);
299 if (diff_context_default < 0)
300 return -1;
301 return 0;
302 }
c4888677
VN
303 if (!strcmp(var, "diff.interhunkcontext")) {
304 diff_interhunk_context_default = git_config_int(var, value);
305 if (diff_interhunk_context_default < 0)
306 return -1;
307 return 0;
308 }
b68ea12e 309 if (!strcmp(var, "diff.renames")) {
cced5fbc 310 diff_detect_rename_default = git_config_rename(var, value);
b68ea12e
EW
311 return 0;
312 }
aecbf914
JH
313 if (!strcmp(var, "diff.autorefreshindex")) {
314 diff_auto_refresh_index = git_config_bool(var, value);
315 return 0;
316 }
a5a818ee
JH
317 if (!strcmp(var, "diff.mnemonicprefix")) {
318 diff_mnemonic_prefix = git_config_bool(var, value);
319 return 0;
320 }
f89504dd
EC
321 if (!strcmp(var, "diff.noprefix")) {
322 diff_no_prefix = git_config_bool(var, value);
323 return 0;
324 }
df44483a
ZJS
325 if (!strcmp(var, "diff.statgraphwidth")) {
326 diff_stat_graph_width = git_config_int(var, value);
327 return 0;
328 }
daec808c
BH
329 if (!strcmp(var, "diff.external"))
330 return git_config_string(&external_diff_cmd_cfg, var, value);
98a4d87b
BSSJ
331 if (!strcmp(var, "diff.wordregex"))
332 return git_config_string(&diff_word_regex_cfg, var, value);
6d8940b5
SB
333 if (!strcmp(var, "diff.orderfile"))
334 return git_config_pathname(&diff_order_file_cfg, var, value);
f1af60bd 335
be4f2b40
JS
336 if (!strcmp(var, "diff.ignoresubmodules"))
337 handle_ignore_submodules_arg(&default_diff_options, value);
338
c47ef57c
RR
339 if (!strcmp(var, "diff.submodule")) {
340 if (parse_submodule_params(&default_diff_options, value))
341 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
342 value);
343 return 0;
344 }
345
07ab4dec
MP
346 if (!strcmp(var, "diff.algorithm")) {
347 diff_algorithm = parse_algorithm_value(value);
348 if (diff_algorithm < 0)
349 return -1;
350 return 0;
351 }
352
a17505f2
JH
353 if (!strcmp(var, "diff.wserrorhighlight")) {
354 int val = parse_ws_error_highlight(value);
355 if (val < 0)
356 return -1;
357 ws_error_highlight_default = val;
358 return 0;
359 }
360
33c643bb
JK
361 if (git_color_config(var, value, cb) < 0)
362 return -1;
363
ef90d6d4 364 return git_diff_basic_config(var, value, cb);
9a1805a8
JK
365}
366
ef90d6d4 367int git_diff_basic_config(const char *var, const char *value, void *cb)
9a1805a8 368{
ae021d87
JK
369 const char *name;
370
2b6ca6df
LT
371 if (!strcmp(var, "diff.renamelimit")) {
372 diff_rename_limit_default = git_config_int(var, value);
373 return 0;
374 }
375
6680a087
JK
376 if (userdiff_config(var, value) < 0)
377 return -1;
c7534ef4 378
ae021d87
JK
379 if (skip_prefix(var, "diff.color.", &name) ||
380 skip_prefix(var, "color.diff.", &name)) {
381 int slot = parse_diff_color_slot(name);
8b8e8624
JK
382 if (slot < 0)
383 return 0;
64f30e94
JH
384 if (!value)
385 return config_error_nonbool(var);
f6c5a296 386 return color_parse(value, diff_colors[slot]);
801235c5 387 }
f1af60bd 388
a624eaa7 389 /* like GNU diff's --suppress-blank-empty option */
950db879
JS
390 if (!strcmp(var, "diff.suppressblankempty") ||
391 /* for backwards compatibility */
392 !strcmp(var, "diff.suppress-blank-empty")) {
a624eaa7
JM
393 diff_suppress_blank_empty = git_config_bool(var, value);
394 return 0;
395 }
396
2d174951 397 if (!strcmp(var, "diff.dirstat")) {
51670fc8 398 struct strbuf errmsg = STRBUF_INIT;
712d2c7d 399 default_diff_options.dirstat_permille = diff_dirstat_permille_default;
51670fc8 400 if (parse_dirstat_params(&default_diff_options, value, &errmsg))
7478ac57 401 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
51670fc8
JH
402 errmsg.buf);
403 strbuf_release(&errmsg);
712d2c7d 404 diff_dirstat_permille_default = default_diff_options.dirstat_permille;
2d174951
JH
405 return 0;
406 }
407
cf5e7722
MB
408 if (git_diff_heuristic_config(var, value, cb) < 0)
409 return -1;
410
3e1dd17a 411 return git_default_config(var, value, cb);
801235c5
JH
412}
413
6973dcae
JH
414static char *quote_two(const char *one, const char *two)
415{
416 int need_one = quote_c_style(one, NULL, NULL, 1);
417 int need_two = quote_c_style(two, NULL, NULL, 1);
f285a2d7 418 struct strbuf res = STRBUF_INIT;
6973dcae
JH
419
420 if (need_one + need_two) {
663af342
PH
421 strbuf_addch(&res, '"');
422 quote_c_style(one, &res, NULL, 1);
423 quote_c_style(two, &res, NULL, 1);
424 strbuf_addch(&res, '"');
425 } else {
426 strbuf_addstr(&res, one);
427 strbuf_addstr(&res, two);
6973dcae 428 }
b315c5c0 429 return strbuf_detach(&res, NULL);
6973dcae
JH
430}
431
432static const char *external_diff(void)
433{
434 static const char *external_diff_cmd = NULL;
435 static int done_preparing = 0;
436
437 if (done_preparing)
438 return external_diff_cmd;
439 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
cbe02100
JS
440 if (!external_diff_cmd)
441 external_diff_cmd = external_diff_cmd_cfg;
6973dcae
JH
442 done_preparing = 1;
443 return external_diff_cmd;
444}
445
284098f1
MH
446/*
447 * Keep track of files used for diffing. Sometimes such an entry
448 * refers to a temporary file, sometimes to an existing file, and
449 * sometimes to "/dev/null".
450 */
6973dcae 451static struct diff_tempfile {
284098f1
MH
452 /*
453 * filename external diff should read from, or NULL if this
454 * entry is currently not in use:
455 */
456 const char *name;
457
dc01505f 458 char hex[GIT_MAX_HEXSZ + 1];
6973dcae 459 char mode[10];
284098f1
MH
460
461 /*
462 * If this diff_tempfile instance refers to a temporary file,
463 * this tempfile object is used to manage its lifetime.
464 */
076aa2cb 465 struct tempfile *tempfile;
6973dcae
JH
466} diff_temp[2];
467
6957eb9a 468struct emit_callback {
6957eb9a
JH
469 int color_diff;
470 unsigned ws_rule;
471 int blank_at_eof_in_preimage;
472 int blank_at_eof_in_postimage;
473 int lno_in_preimage;
474 int lno_in_postimage;
6957eb9a
JH
475 const char **label_path;
476 struct diff_words_data *diff_words;
a3c158d4 477 struct diff_options *opt;
3e97c7c6 478 struct strbuf *header;
6957eb9a
JH
479};
480
6973dcae
JH
481static int count_lines(const char *data, int size)
482{
483 int count, ch, completely_empty = 1, nl_just_seen = 0;
484 count = 0;
485 while (0 < size--) {
486 ch = *data++;
487 if (ch == '\n') {
488 count++;
489 nl_just_seen = 1;
490 completely_empty = 0;
491 }
492 else {
493 nl_just_seen = 0;
494 completely_empty = 0;
495 }
496 }
497 if (completely_empty)
498 return 0;
499 if (!nl_just_seen)
500 count++; /* no trailing newline */
501 return count;
502}
503
6957eb9a
JH
504static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
505{
506 if (!DIFF_FILE_VALID(one)) {
507 mf->ptr = (char *)""; /* does not matter */
508 mf->size = 0;
509 return 0;
510 }
511 else if (diff_populate_filespec(one, 0))
512 return -1;
bb35fefb 513
6957eb9a
JH
514 mf->ptr = one->data;
515 mf->size = one->size;
516 return 0;
517}
518
abb371a1
JK
519/* like fill_mmfile, but only for size, so we can avoid retrieving blob */
520static unsigned long diff_filespec_size(struct diff_filespec *one)
521{
522 if (!DIFF_FILE_VALID(one))
523 return 0;
8e5dd3d6 524 diff_populate_filespec(one, CHECK_SIZE_ONLY);
abb371a1
JK
525 return one->size;
526}
527
6957eb9a
JH
528static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
529{
530 char *ptr = mf->ptr;
531 long size = mf->size;
532 int cnt = 0;
533
534 if (!size)
535 return cnt;
536 ptr += size - 1; /* pointing at the very end */
537 if (*ptr != '\n')
538 ; /* incomplete line */
539 else
540 ptr--; /* skip the last LF */
541 while (mf->ptr < ptr) {
542 char *prev_eol;
543 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
544 if (*prev_eol == '\n')
545 break;
546 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
547 break;
548 cnt++;
549 ptr = prev_eol - 1;
550 }
551 return cnt;
552}
553
554static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
555 struct emit_callback *ecbdata)
556{
557 int l1, l2, at;
558 unsigned ws_rule = ecbdata->ws_rule;
559 l1 = count_trailing_blank(mf1, ws_rule);
560 l2 = count_trailing_blank(mf2, ws_rule);
561 if (l2 <= l1) {
562 ecbdata->blank_at_eof_in_preimage = 0;
563 ecbdata->blank_at_eof_in_postimage = 0;
564 return;
565 }
566 at = count_lines(mf1->ptr, mf1->size);
567 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
568
569 at = count_lines(mf2->ptr, mf2->size);
570 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
571}
572
a3c158d4 573static void emit_line_0(struct diff_options *o, const char *set, const char *reset,
250f7993 574 int first, const char *line, int len)
6957eb9a
JH
575{
576 int has_trailing_newline, has_trailing_carriage_return;
250f7993 577 int nofirst;
a3c158d4
BY
578 FILE *file = o->file;
579
30997bb8 580 fputs(diff_line_prefix(o), file);
6957eb9a 581
250f7993
JH
582 if (len == 0) {
583 has_trailing_newline = (first == '\n');
584 has_trailing_carriage_return = (!has_trailing_newline &&
585 (first == '\r'));
586 nofirst = has_trailing_newline || has_trailing_carriage_return;
587 } else {
588 has_trailing_newline = (len > 0 && line[len-1] == '\n');
589 if (has_trailing_newline)
590 len--;
591 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
592 if (has_trailing_carriage_return)
593 len--;
594 nofirst = 0;
595 }
6957eb9a 596
06a47552
JH
597 if (len || !nofirst) {
598 fputs(set, file);
599 if (!nofirst)
600 fputc(first, file);
601 fwrite(line, len, 1, file);
602 fputs(reset, file);
603 }
6957eb9a
JH
604 if (has_trailing_carriage_return)
605 fputc('\r', file);
606 if (has_trailing_newline)
607 fputc('\n', file);
608}
609
a3c158d4 610static void emit_line(struct diff_options *o, const char *set, const char *reset,
250f7993
JH
611 const char *line, int len)
612{
a3c158d4 613 emit_line_0(o, set, reset, line[0], line+1, len-1);
250f7993
JH
614}
615
36a4cefd 616enum diff_symbol {
4eed0ebd
SB
617 DIFF_SYMBOL_BINARY_DIFF_HEADER,
618 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
619 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
620 DIFF_SYMBOL_BINARY_DIFF_BODY,
621 DIFF_SYMBOL_BINARY_DIFF_FOOTER,
0911c475
SB
622 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
623 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
624 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
625 DIFF_SYMBOL_STATS_LINE,
bd033291 626 DIFF_SYMBOL_WORD_DIFF,
30b7e1e7 627 DIFF_SYMBOL_STAT_SEP,
146fdb0d 628 DIFF_SYMBOL_SUMMARY,
f3597138
SB
629 DIFF_SYMBOL_SUBMODULE_ADD,
630 DIFF_SYMBOL_SUBMODULE_DEL,
631 DIFF_SYMBOL_SUBMODULE_UNTRACKED,
632 DIFF_SYMBOL_SUBMODULE_MODIFIED,
633 DIFF_SYMBOL_SUBMODULE_HEADER,
634 DIFF_SYMBOL_SUBMODULE_ERROR,
635 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
5af6ea95 636 DIFF_SYMBOL_REWRITE_DIFF,
4acaaa7a 637 DIFF_SYMBOL_BINARY_FILES,
a29b0a13 638 DIFF_SYMBOL_HEADER,
3ee8b7bf
SB
639 DIFF_SYMBOL_FILEPAIR_PLUS,
640 DIFF_SYMBOL_FILEPAIR_MINUS,
ff958679
SB
641 DIFF_SYMBOL_WORDS_PORCELAIN,
642 DIFF_SYMBOL_WORDS,
091f8e28 643 DIFF_SYMBOL_CONTEXT,
f2bb1218 644 DIFF_SYMBOL_CONTEXT_INCOMPLETE,
091f8e28
SB
645 DIFF_SYMBOL_PLUS,
646 DIFF_SYMBOL_MINUS,
b9cbfde6 647 DIFF_SYMBOL_NO_LF_EOF,
68abc6f1 648 DIFF_SYMBOL_CONTEXT_FRAGINFO,
c64b420b 649 DIFF_SYMBOL_CONTEXT_MARKER,
36a4cefd
SB
650 DIFF_SYMBOL_SEPARATOR
651};
091f8e28
SB
652/*
653 * Flags for content lines:
654 * 0..12 are whitespace rules
655 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
656 * 16 is marking if the line is blank at EOF
657 */
2e2d5ac1
SB
658#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
659#define DIFF_SYMBOL_MOVED_LINE (1<<17)
660#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
86b452e2 661#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
091f8e28
SB
662#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
663
e6e045f8
SB
664/*
665 * This struct is used when we need to buffer the output of the diff output.
666 *
667 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
668 * into the pre/post image file. This pointer could be a union with the
669 * line pointer. By storing an offset into the file instead of the literal line,
670 * we can decrease the memory footprint for the buffered output. At first we
671 * may want to only have indirection for the content lines, but we could also
672 * enhance the state for emitting prefabricated lines, e.g. the similarity
673 * score line or hunk/file headers would only need to store a number or path
674 * and then the output can be constructed later on depending on state.
675 */
676struct emitted_diff_symbol {
677 const char *line;
678 int len;
679 int flags;
680 enum diff_symbol s;
681};
682#define EMITTED_DIFF_SYMBOL_INIT {NULL}
683
684struct emitted_diff_symbols {
685 struct emitted_diff_symbol *buf;
686 int nr, alloc;
687};
688#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
689
690static void append_emitted_diff_symbol(struct diff_options *o,
691 struct emitted_diff_symbol *e)
6957eb9a 692{
e6e045f8
SB
693 struct emitted_diff_symbol *f;
694
695 ALLOC_GROW(o->emitted_symbols->buf,
696 o->emitted_symbols->nr + 1,
697 o->emitted_symbols->alloc);
698 f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
699
700 memcpy(f, e, sizeof(struct emitted_diff_symbol));
701 f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
6957eb9a
JH
702}
703
2e2d5ac1
SB
704struct moved_entry {
705 struct hashmap_entry ent;
706 const struct emitted_diff_symbol *es;
707 struct moved_entry *next_line;
708};
709
710static int next_byte(const char **cp, const char **endp,
711 const struct diff_options *diffopt)
712{
713 int retval;
714
da58318e 715 if (*cp >= *endp)
2e2d5ac1
SB
716 return -1;
717
fa5ba2c1
JK
718 if (isspace(**cp)) {
719 if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_CHANGE)) {
720 while (*cp < *endp && isspace(**cp))
721 (*cp)++;
722 /*
723 * After skipping a couple of whitespaces,
724 * we still have to account for one space.
725 */
726 return (int)' ';
727 }
2e2d5ac1 728
fa5ba2c1
JK
729 if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE)) {
730 while (*cp < *endp && isspace(**cp))
731 (*cp)++;
da58318e
JK
732 /*
733 * return the first non-ws character via the usual
734 * below, unless we ate all of the bytes
735 */
736 if (*cp >= *endp)
737 return -1;
fa5ba2c1 738 }
2e2d5ac1
SB
739 }
740
741 retval = (unsigned char)(**cp);
742 (*cp)++;
743 return retval;
744}
745
746static int moved_entry_cmp(const struct diff_options *diffopt,
747 const struct moved_entry *a,
748 const struct moved_entry *b,
749 const void *keydata)
750{
751 const char *ap = a->es->line, *ae = a->es->line + a->es->len;
752 const char *bp = b->es->line, *be = b->es->line + b->es->len;
753
754 if (!(diffopt->xdl_opts & XDF_WHITESPACE_FLAGS))
755 return a->es->len != b->es->len || memcmp(ap, bp, a->es->len);
756
757 if (DIFF_XDL_TST(diffopt, IGNORE_WHITESPACE_AT_EOL)) {
da58318e 758 while (ae > ap && isspace(ae[-1]))
2e2d5ac1 759 ae--;
da58318e 760 while (be > bp && isspace(be[-1]))
2e2d5ac1
SB
761 be--;
762 }
763
764 while (1) {
765 int ca, cb;
766 ca = next_byte(&ap, &ae, diffopt);
767 cb = next_byte(&bp, &be, diffopt);
768 if (ca != cb)
769 return 1;
770 if (ca < 0)
771 return 0;
772 }
773}
774
775static unsigned get_string_hash(struct emitted_diff_symbol *es, struct diff_options *o)
776{
777 if (o->xdl_opts & XDF_WHITESPACE_FLAGS) {
778 static struct strbuf sb = STRBUF_INIT;
779 const char *ap = es->line, *ae = es->line + es->len;
780 int c;
781
782 strbuf_reset(&sb);
da58318e 783 while (ae > ap && isspace(ae[-1]))
2e2d5ac1
SB
784 ae--;
785 while ((c = next_byte(&ap, &ae, o)) > 0)
786 strbuf_addch(&sb, c);
787
788 return memhash(sb.buf, sb.len);
789 } else {
790 return memhash(es->line, es->len);
791 }
792}
793
794static struct moved_entry *prepare_entry(struct diff_options *o,
795 int line_no)
796{
797 struct moved_entry *ret = xmalloc(sizeof(*ret));
798 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
799
800 ret->ent.hash = get_string_hash(l, o);
801 ret->es = l;
802 ret->next_line = NULL;
803
804 return ret;
805}
806
807static void add_lines_to_move_detection(struct diff_options *o,
808 struct hashmap *add_lines,
809 struct hashmap *del_lines)
810{
811 struct moved_entry *prev_line = NULL;
812
813 int n;
814 for (n = 0; n < o->emitted_symbols->nr; n++) {
815 struct hashmap *hm;
816 struct moved_entry *key;
817
818 switch (o->emitted_symbols->buf[n].s) {
819 case DIFF_SYMBOL_PLUS:
820 hm = add_lines;
821 break;
822 case DIFF_SYMBOL_MINUS:
823 hm = del_lines;
824 break;
825 default:
826 prev_line = NULL;
827 continue;
828 }
829
830 key = prepare_entry(o, n);
831 if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
832 prev_line->next_line = key;
833
834 hashmap_add(hm, key);
835 prev_line = key;
836 }
837}
838
839static int shrink_potential_moved_blocks(struct moved_entry **pmb,
840 int pmb_nr)
841{
842 int lp, rp;
843
844 /* Shrink the set of potential block to the remaining running */
845 for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
846 while (lp < pmb_nr && pmb[lp])
847 lp++;
848 /* lp points at the first NULL now */
849
850 while (rp > -1 && !pmb[rp])
851 rp--;
852 /* rp points at the last non-NULL */
853
854 if (lp < pmb_nr && rp > -1 && lp < rp) {
855 pmb[lp] = pmb[rp];
856 pmb[rp] = NULL;
857 rp--;
858 lp++;
859 }
860 }
861
862 /* Remember the number of running sets */
863 return rp + 1;
864}
865
09153277
JT
866/*
867 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
868 *
f0b8fb6e
JT
869 * Otherwise, if the last block has fewer alphanumeric characters than
870 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
09153277
JT
871 * that block.
872 *
873 * The last block consists of the (n - block_length)'th line up to but not
874 * including the nth line.
f0b8fb6e
JT
875 *
876 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
877 * Think of a way to unify them.
09153277
JT
878 */
879static void adjust_last_block(struct diff_options *o, int n, int block_length)
880{
f0b8fb6e
JT
881 int i, alnum_count = 0;
882 if (o->color_moved == COLOR_MOVED_PLAIN)
09153277 883 return;
f0b8fb6e
JT
884 for (i = 1; i < block_length + 1; i++) {
885 const char *c = o->emitted_symbols->buf[n - i].line;
886 for (; *c; c++) {
887 if (!isalnum(*c))
888 continue;
889 alnum_count++;
890 if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
891 return;
892 }
893 }
09153277
JT
894 for (i = 1; i < block_length + 1; i++)
895 o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
896}
897
2e2d5ac1
SB
898/* Find blocks of moved code, delegate actual coloring decision to helper */
899static void mark_color_as_moved(struct diff_options *o,
900 struct hashmap *add_lines,
901 struct hashmap *del_lines)
902{
903 struct moved_entry **pmb = NULL; /* potentially moved blocks */
904 int pmb_nr = 0, pmb_alloc = 0;
905 int n, flipped_block = 1, block_length = 0;
906
907
908 for (n = 0; n < o->emitted_symbols->nr; n++) {
909 struct hashmap *hm = NULL;
910 struct moved_entry *key;
911 struct moved_entry *match = NULL;
912 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
913 int i;
914
915 switch (l->s) {
916 case DIFF_SYMBOL_PLUS:
917 hm = del_lines;
918 key = prepare_entry(o, n);
919 match = hashmap_get(hm, key, o);
920 free(key);
921 break;
922 case DIFF_SYMBOL_MINUS:
923 hm = add_lines;
924 key = prepare_entry(o, n);
925 match = hashmap_get(hm, key, o);
926 free(key);
927 break;
928 default:
929 flipped_block = 1;
930 }
931
932 if (!match) {
09153277 933 adjust_last_block(o, n, block_length);
2e2d5ac1
SB
934 pmb_nr = 0;
935 block_length = 0;
936 continue;
937 }
938
939 l->flags |= DIFF_SYMBOL_MOVED_LINE;
2e2d5ac1 940
176841f0
SB
941 if (o->color_moved == COLOR_MOVED_PLAIN)
942 continue;
943
2e2d5ac1
SB
944 /* Check any potential block runs, advance each or nullify */
945 for (i = 0; i < pmb_nr; i++) {
946 struct moved_entry *p = pmb[i];
947 struct moved_entry *pnext = (p && p->next_line) ?
948 p->next_line : NULL;
949 if (pnext && !hm->cmpfn(o, pnext, match, NULL)) {
950 pmb[i] = p->next_line;
951 } else {
952 pmb[i] = NULL;
953 }
954 }
955
956 pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
957
958 if (pmb_nr == 0) {
959 /*
960 * The current line is the start of a new block.
961 * Setup the set of potential blocks.
962 */
963 for (; match; match = hashmap_get_next(hm, match)) {
964 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
965 pmb[pmb_nr++] = match;
966 }
967
968 flipped_block = (flipped_block + 1) % 2;
f0b8fb6e
JT
969
970 adjust_last_block(o, n, block_length);
971 block_length = 0;
2e2d5ac1
SB
972 }
973
f0b8fb6e
JT
974 block_length++;
975
2e2d5ac1
SB
976 if (flipped_block)
977 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
978 }
09153277 979 adjust_last_block(o, n, block_length);
2e2d5ac1
SB
980
981 free(pmb);
982}
e6e045f8 983
86b452e2
SB
984#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
985 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
986static void dim_moved_lines(struct diff_options *o)
987{
988 int n;
989 for (n = 0; n < o->emitted_symbols->nr; n++) {
990 struct emitted_diff_symbol *prev = (n != 0) ?
991 &o->emitted_symbols->buf[n - 1] : NULL;
992 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
993 struct emitted_diff_symbol *next =
994 (n < o->emitted_symbols->nr - 1) ?
995 &o->emitted_symbols->buf[n + 1] : NULL;
996
997 /* Not a plus or minus line? */
998 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
999 continue;
1000
1001 /* Not a moved line? */
1002 if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
1003 continue;
1004
1005 /*
1006 * If prev or next are not a plus or minus line,
1007 * pretend they don't exist
1008 */
1009 if (prev && prev->s != DIFF_SYMBOL_PLUS &&
1010 prev->s != DIFF_SYMBOL_MINUS)
1011 prev = NULL;
1012 if (next && next->s != DIFF_SYMBOL_PLUS &&
1013 next->s != DIFF_SYMBOL_MINUS)
1014 next = NULL;
1015
1016 /* Inside a block? */
1017 if ((prev &&
1018 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1019 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
1020 (next &&
1021 (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1022 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
1023 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1024 continue;
1025 }
1026
1027 /* Check if we are at an interesting bound: */
1028 if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
1029 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1030 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1031 continue;
1032 if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
1033 (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1034 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1035 continue;
1036
1037 /*
1038 * The boundary to prev and next are not interesting,
1039 * so this line is not interesting as a whole
1040 */
1041 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1042 }
1043}
1044
091f8e28
SB
1045static void emit_line_ws_markup(struct diff_options *o,
1046 const char *set, const char *reset,
1047 const char *line, int len, char sign,
1048 unsigned ws_rule, int blank_at_eof)
6957eb9a 1049{
b8767f79 1050 const char *ws = NULL;
6957eb9a 1051
091f8e28
SB
1052 if (o->ws_error_highlight & ws_rule) {
1053 ws = diff_get_color_opt(o, DIFF_WHITESPACE);
b8767f79
JH
1054 if (!*ws)
1055 ws = NULL;
1056 }
1057
1058 if (!ws)
091f8e28
SB
1059 emit_line_0(o, set, reset, sign, line, len);
1060 else if (blank_at_eof)
6957eb9a 1061 /* Blank line at EOF - paint '+' as well */
091f8e28 1062 emit_line_0(o, ws, reset, sign, line, len);
6957eb9a
JH
1063 else {
1064 /* Emit just the prefix, then the rest. */
091f8e28
SB
1065 emit_line_0(o, set, reset, sign, "", 0);
1066 ws_check_emit(line, len, ws_rule,
1067 o->file, set, reset, ws);
6957eb9a
JH
1068 }
1069}
1070
e6e045f8
SB
1071static void emit_diff_symbol_from_struct(struct diff_options *o,
1072 struct emitted_diff_symbol *eds)
36a4cefd 1073{
b9cbfde6 1074 static const char *nneof = " No newline at end of file\n";
5af6ea95 1075 const char *context, *reset, *set, *meta, *fraginfo;
0911c475 1076 struct strbuf sb = STRBUF_INIT;
e6e045f8
SB
1077
1078 enum diff_symbol s = eds->s;
1079 const char *line = eds->line;
1080 int len = eds->len;
1081 unsigned flags = eds->flags;
1082
36a4cefd 1083 switch (s) {
b9cbfde6
SB
1084 case DIFF_SYMBOL_NO_LF_EOF:
1085 context = diff_get_color_opt(o, DIFF_CONTEXT);
1086 reset = diff_get_color_opt(o, DIFF_RESET);
1087 putc('\n', o->file);
1088 emit_line_0(o, context, reset, '\\',
1089 nneof, strlen(nneof));
1090 break;
f3597138
SB
1091 case DIFF_SYMBOL_SUBMODULE_HEADER:
1092 case DIFF_SYMBOL_SUBMODULE_ERROR:
1093 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
0911c475 1094 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
146fdb0d 1095 case DIFF_SYMBOL_SUMMARY:
0911c475 1096 case DIFF_SYMBOL_STATS_LINE:
4eed0ebd 1097 case DIFF_SYMBOL_BINARY_DIFF_BODY:
68abc6f1
SB
1098 case DIFF_SYMBOL_CONTEXT_FRAGINFO:
1099 emit_line(o, "", "", line, len);
1100 break;
f2bb1218 1101 case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
c64b420b
SB
1102 case DIFF_SYMBOL_CONTEXT_MARKER:
1103 context = diff_get_color_opt(o, DIFF_CONTEXT);
1104 reset = diff_get_color_opt(o, DIFF_RESET);
1105 emit_line(o, context, reset, line, len);
1106 break;
36a4cefd
SB
1107 case DIFF_SYMBOL_SEPARATOR:
1108 fprintf(o->file, "%s%c",
1109 diff_line_prefix(o),
1110 o->line_termination);
1111 break;
091f8e28
SB
1112 case DIFF_SYMBOL_CONTEXT:
1113 set = diff_get_color_opt(o, DIFF_CONTEXT);
1114 reset = diff_get_color_opt(o, DIFF_RESET);
1115 emit_line_ws_markup(o, set, reset, line, len, ' ',
1116 flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1117 break;
1118 case DIFF_SYMBOL_PLUS:
86b452e2
SB
1119 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1120 DIFF_SYMBOL_MOVED_LINE_ALT |
1121 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1122 case DIFF_SYMBOL_MOVED_LINE |
1123 DIFF_SYMBOL_MOVED_LINE_ALT |
1124 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1125 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
1126 break;
1127 case DIFF_SYMBOL_MOVED_LINE |
1128 DIFF_SYMBOL_MOVED_LINE_ALT:
2e2d5ac1 1129 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
86b452e2
SB
1130 break;
1131 case DIFF_SYMBOL_MOVED_LINE |
1132 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1133 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1134 break;
1135 case DIFF_SYMBOL_MOVED_LINE:
2e2d5ac1 1136 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
86b452e2
SB
1137 break;
1138 default:
2e2d5ac1 1139 set = diff_get_color_opt(o, DIFF_FILE_NEW);
86b452e2 1140 }
091f8e28
SB
1141 reset = diff_get_color_opt(o, DIFF_RESET);
1142 emit_line_ws_markup(o, set, reset, line, len, '+',
1143 flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1144 flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1145 break;
1146 case DIFF_SYMBOL_MINUS:
86b452e2
SB
1147 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1148 DIFF_SYMBOL_MOVED_LINE_ALT |
1149 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1150 case DIFF_SYMBOL_MOVED_LINE |
1151 DIFF_SYMBOL_MOVED_LINE_ALT |
1152 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1153 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1154 break;
1155 case DIFF_SYMBOL_MOVED_LINE |
1156 DIFF_SYMBOL_MOVED_LINE_ALT:
2e2d5ac1 1157 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
86b452e2
SB
1158 break;
1159 case DIFF_SYMBOL_MOVED_LINE |
1160 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1161 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1162 break;
1163 case DIFF_SYMBOL_MOVED_LINE:
2e2d5ac1 1164 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
86b452e2
SB
1165 break;
1166 default:
2e2d5ac1 1167 set = diff_get_color_opt(o, DIFF_FILE_OLD);
86b452e2 1168 }
091f8e28
SB
1169 reset = diff_get_color_opt(o, DIFF_RESET);
1170 emit_line_ws_markup(o, set, reset, line, len, '-',
1171 flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
1172 break;
ff958679
SB
1173 case DIFF_SYMBOL_WORDS_PORCELAIN:
1174 context = diff_get_color_opt(o, DIFF_CONTEXT);
1175 reset = diff_get_color_opt(o, DIFF_RESET);
1176 emit_line(o, context, reset, line, len);
1177 fputs("~\n", o->file);
1178 break;
1179 case DIFF_SYMBOL_WORDS:
1180 context = diff_get_color_opt(o, DIFF_CONTEXT);
1181 reset = diff_get_color_opt(o, DIFF_RESET);
1182 /*
1183 * Skip the prefix character, if any. With
1184 * diff_suppress_blank_empty, there may be
1185 * none.
1186 */
1187 if (line[0] != '\n') {
1188 line++;
1189 len--;
1190 }
1191 emit_line(o, context, reset, line, len);
1192 break;
3ee8b7bf
SB
1193 case DIFF_SYMBOL_FILEPAIR_PLUS:
1194 meta = diff_get_color_opt(o, DIFF_METAINFO);
1195 reset = diff_get_color_opt(o, DIFF_RESET);
1196 fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
1197 line, reset,
1198 strchr(line, ' ') ? "\t" : "");
1199 break;
1200 case DIFF_SYMBOL_FILEPAIR_MINUS:
1201 meta = diff_get_color_opt(o, DIFF_METAINFO);
1202 reset = diff_get_color_opt(o, DIFF_RESET);
1203 fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
1204 line, reset,
1205 strchr(line, ' ') ? "\t" : "");
1206 break;
4acaaa7a 1207 case DIFF_SYMBOL_BINARY_FILES:
a29b0a13
SB
1208 case DIFF_SYMBOL_HEADER:
1209 fprintf(o->file, "%s", line);
1210 break;
4eed0ebd
SB
1211 case DIFF_SYMBOL_BINARY_DIFF_HEADER:
1212 fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
1213 break;
1214 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
1215 fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
1216 break;
1217 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
1218 fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
1219 break;
1220 case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
1221 fputs(diff_line_prefix(o), o->file);
1222 fputc('\n', o->file);
1223 break;
5af6ea95
SB
1224 case DIFF_SYMBOL_REWRITE_DIFF:
1225 fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
1226 reset = diff_get_color_opt(o, DIFF_RESET);
1227 emit_line(o, fraginfo, reset, line, len);
1228 break;
f3597138
SB
1229 case DIFF_SYMBOL_SUBMODULE_ADD:
1230 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1231 reset = diff_get_color_opt(o, DIFF_RESET);
1232 emit_line(o, set, reset, line, len);
1233 break;
1234 case DIFF_SYMBOL_SUBMODULE_DEL:
1235 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1236 reset = diff_get_color_opt(o, DIFF_RESET);
1237 emit_line(o, set, reset, line, len);
1238 break;
1239 case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
1240 fprintf(o->file, "%sSubmodule %s contains untracked content\n",
1241 diff_line_prefix(o), line);
1242 break;
1243 case DIFF_SYMBOL_SUBMODULE_MODIFIED:
1244 fprintf(o->file, "%sSubmodule %s contains modified content\n",
1245 diff_line_prefix(o), line);
1246 break;
0911c475
SB
1247 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
1248 emit_line(o, "", "", " 0 files changed\n",
1249 strlen(" 0 files changed\n"));
1250 break;
1251 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
1252 emit_line(o, "", "", " ...\n", strlen(" ...\n"));
1253 break;
bd033291
SB
1254 case DIFF_SYMBOL_WORD_DIFF:
1255 fprintf(o->file, "%.*s", len, line);
1256 break;
30b7e1e7
SB
1257 case DIFF_SYMBOL_STAT_SEP:
1258 fputs(o->stat_sep, o->file);
1259 break;
36a4cefd
SB
1260 default:
1261 die("BUG: unknown diff symbol");
1262 }
0911c475 1263 strbuf_release(&sb);
36a4cefd
SB
1264}
1265
e6e045f8
SB
1266static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
1267 const char *line, int len, unsigned flags)
1268{
1269 struct emitted_diff_symbol e = {line, len, flags, s};
1270
1271 if (o->emitted_symbols)
1272 append_emitted_diff_symbol(o, &e);
1273 else
1274 emit_diff_symbol_from_struct(o, &e);
1275}
1276
f3597138
SB
1277void diff_emit_submodule_del(struct diff_options *o, const char *line)
1278{
1279 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
1280}
1281
1282void diff_emit_submodule_add(struct diff_options *o, const char *line)
1283{
1284 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
1285}
1286
1287void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
1288{
1289 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
1290 path, strlen(path), 0);
1291}
1292
1293void diff_emit_submodule_modified(struct diff_options *o, const char *path)
1294{
1295 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
1296 path, strlen(path), 0);
1297}
1298
1299void diff_emit_submodule_header(struct diff_options *o, const char *header)
1300{
1301 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
1302 header, strlen(header), 0);
1303}
1304
1305void diff_emit_submodule_error(struct diff_options *o, const char *err)
1306{
1307 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
1308}
1309
1310void diff_emit_submodule_pipethrough(struct diff_options *o,
1311 const char *line, int len)
1312{
1313 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
1314}
1315
6957eb9a
JH
1316static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
1317{
1318 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
1319 ecbdata->blank_at_eof_in_preimage &&
1320 ecbdata->blank_at_eof_in_postimage &&
1321 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
1322 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
1323 return 0;
018cff70 1324 return ws_blank_line(line, len, ecbdata->ws_rule);
6957eb9a
JH
1325}
1326
b8767f79 1327static void emit_add_line(const char *reset,
0e383e18
JH
1328 struct emit_callback *ecbdata,
1329 const char *line, int len)
1330{
091f8e28
SB
1331 unsigned flags = WSEH_NEW | ecbdata->ws_rule;
1332 if (new_blank_line_at_eof(ecbdata, line, len))
1333 flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
1334
1335 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
b8767f79 1336}
0e383e18 1337
b8767f79
JH
1338static void emit_del_line(const char *reset,
1339 struct emit_callback *ecbdata,
1340 const char *line, int len)
1341{
091f8e28
SB
1342 unsigned flags = WSEH_OLD | ecbdata->ws_rule;
1343 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
0e383e18
JH
1344}
1345
1346static void emit_context_line(const char *reset,
1347 struct emit_callback *ecbdata,
1348 const char *line, int len)
1349{
091f8e28
SB
1350 unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
1351 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
0e383e18
JH
1352}
1353
89cb73a1
BW
1354static void emit_hunk_header(struct emit_callback *ecbdata,
1355 const char *line, int len)
1356{
8dbf3eb6 1357 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
89cb73a1
BW
1358 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1359 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1360 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
1361 static const char atat[2] = { '@', '@' };
1362 const char *cp, *ep;
2efcc977
BY
1363 struct strbuf msgbuf = STRBUF_INIT;
1364 int org_len = len;
1365 int i = 1;
89cb73a1
BW
1366
1367 /*
1368 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1369 * it always is at least 10 bytes long.
1370 */
1371 if (len < 10 ||
1372 memcmp(line, atat, 2) ||
1373 !(ep = memmem(line + 2, len - 2, atat, 2))) {
c64b420b 1374 emit_diff_symbol(ecbdata->opt,
091f8e28 1375 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
89cb73a1
BW
1376 return;
1377 }
1378 ep += 2; /* skip over @@ */
1379
1380 /* The hunk header in fraginfo color */
cedc61a9 1381 strbuf_addstr(&msgbuf, frag);
2efcc977 1382 strbuf_add(&msgbuf, line, ep - line);
cedc61a9 1383 strbuf_addstr(&msgbuf, reset);
2efcc977
BY
1384
1385 /*
1386 * trailing "\r\n"
1387 */
1388 for ( ; i < 3; i++)
1389 if (line[len - i] == '\r' || line[len - i] == '\n')
1390 len--;
89cb73a1
BW
1391
1392 /* blank before the func header */
1393 for (cp = ep; ep - line < len; ep++)
1394 if (*ep != ' ' && *ep != '\t')
1395 break;
2efcc977 1396 if (ep != cp) {
8dbf3eb6 1397 strbuf_addstr(&msgbuf, context);
2efcc977 1398 strbuf_add(&msgbuf, cp, ep - cp);
cedc61a9 1399 strbuf_addstr(&msgbuf, reset);
2efcc977
BY
1400 }
1401
1402 if (ep < line + len) {
cedc61a9 1403 strbuf_addstr(&msgbuf, func);
2efcc977 1404 strbuf_add(&msgbuf, ep, line + len - ep);
cedc61a9 1405 strbuf_addstr(&msgbuf, reset);
2efcc977 1406 }
89cb73a1 1407
2efcc977 1408 strbuf_add(&msgbuf, line + len, org_len - len);
dfb7728f 1409 strbuf_complete_line(&msgbuf);
68abc6f1 1410 emit_diff_symbol(ecbdata->opt,
091f8e28 1411 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
2efcc977 1412 strbuf_release(&msgbuf);
89cb73a1
BW
1413}
1414
479b0ae8
JK
1415static struct diff_tempfile *claim_diff_tempfile(void) {
1416 int i;
1417 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
1418 if (!diff_temp[i].name)
1419 return diff_temp + i;
1420 die("BUG: diff is failing to clean up its tempfiles");
1421}
1422
479b0ae8
JK
1423static void remove_tempfile(void)
1424{
1425 int i;
a8344abe 1426 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
076aa2cb 1427 if (is_tempfile_active(diff_temp[i].tempfile))
284098f1 1428 delete_tempfile(&diff_temp[i].tempfile);
a8344abe
NR
1429 diff_temp[i].name = NULL;
1430 }
479b0ae8
JK
1431}
1432
5af6ea95 1433static void add_line_count(struct strbuf *out, int count)
6973dcae
JH
1434{
1435 switch (count) {
1436 case 0:
5af6ea95 1437 strbuf_addstr(out, "0,0");
6973dcae
JH
1438 break;
1439 case 1:
5af6ea95 1440 strbuf_addstr(out, "1");
6973dcae
JH
1441 break;
1442 default:
5af6ea95 1443 strbuf_addf(out, "1,%d", count);
6973dcae
JH
1444 break;
1445 }
1446}
1447
7f7ee2ff
JH
1448static void emit_rewrite_lines(struct emit_callback *ecb,
1449 int prefix, const char *data, int size)
6973dcae 1450{
7f7ee2ff 1451 const char *endp = NULL;
7f7ee2ff
JH
1452 const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);
1453
1454 while (0 < size) {
1455 int len;
1456
1457 endp = memchr(data, '\n', size);
1458 len = endp ? (endp - data + 1) : size;
1459 if (prefix != '+') {
1460 ecb->lno_in_preimage++;
0e383e18 1461 emit_del_line(reset, ecb, data, len);
7f7ee2ff
JH
1462 } else {
1463 ecb->lno_in_postimage++;
1464 emit_add_line(reset, ecb, data, len);
13e36ec5 1465 }
7f7ee2ff
JH
1466 size -= len;
1467 data += len;
1468 }
b9cbfde6 1469 if (!endp)
091f8e28 1470 emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
6973dcae
JH
1471}
1472
1473static void emit_rewrite_diff(const char *name_a,
1474 const char *name_b,
1475 struct diff_filespec *one,
13e36ec5 1476 struct diff_filespec *two,
d9bae1a1
JK
1477 struct userdiff_driver *textconv_one,
1478 struct userdiff_driver *textconv_two,
eab9a40b 1479 struct diff_options *o)
6973dcae
JH
1480{
1481 int lc_a, lc_b;
d5625091 1482 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
a5a818ee 1483 const char *a_prefix, *b_prefix;
840383b2 1484 char *data_one, *data_two;
3aa1f7ca 1485 size_t size_one, size_two;
7f7ee2ff 1486 struct emit_callback ecbdata;
5af6ea95 1487 struct strbuf out = STRBUF_INIT;
a5a818ee
JH
1488
1489 if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
1490 a_prefix = o->b_prefix;
1491 b_prefix = o->a_prefix;
1492 } else {
1493 a_prefix = o->a_prefix;
1494 b_prefix = o->b_prefix;
1495 }
1a9eb3b9 1496
8a13becc
JH
1497 name_a += (*name_a == '/');
1498 name_b += (*name_b == '/');
1a9eb3b9 1499
d5625091
JH
1500 strbuf_reset(&a_name);
1501 strbuf_reset(&b_name);
a5a818ee
JH
1502 quote_two_c_style(&a_name, a_prefix, name_a, 0);
1503 quote_two_c_style(&b_name, b_prefix, name_b, 0);
d5625091 1504
840383b2
JK
1505 size_one = fill_textconv(textconv_one, one, &data_one);
1506 size_two = fill_textconv(textconv_two, two, &data_two);
3aa1f7ca 1507
d91ba8fa 1508 memset(&ecbdata, 0, sizeof(ecbdata));
daa0c3d9 1509 ecbdata.color_diff = want_color(o->use_color);
c189c4f2 1510 ecbdata.ws_rule = whitespace_rule(name_b);
a3c158d4 1511 ecbdata.opt = o;
d91ba8fa
JH
1512 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
1513 mmfile_t mf1, mf2;
1514 mf1.ptr = (char *)data_one;
1515 mf2.ptr = (char *)data_two;
1516 mf1.size = size_one;
1517 mf2.size = size_two;
1518 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1519 }
1520 ecbdata.lno_in_preimage = 1;
1521 ecbdata.lno_in_postimage = 1;
1522
3aa1f7ca
JK
1523 lc_a = count_lines(data_one, size_one);
1524 lc_b = count_lines(data_two, size_two);
3ee8b7bf
SB
1525
1526 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1527 a_name.buf, a_name.len, 0);
1528 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1529 b_name.buf, b_name.len, 0);
1530
5af6ea95 1531 strbuf_addstr(&out, "@@ -");
467ddc14 1532 if (!o->irreversible_delete)
5af6ea95 1533 add_line_count(&out, lc_a);
467ddc14 1534 else
5af6ea95
SB
1535 strbuf_addstr(&out, "?,?");
1536 strbuf_addstr(&out, " +");
1537 add_line_count(&out, lc_b);
1538 strbuf_addstr(&out, " @@\n");
1539 emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
1540 strbuf_release(&out);
1541
467ddc14 1542 if (lc_a && !o->irreversible_delete)
d91ba8fa 1543 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
6973dcae 1544 if (lc_b)
d91ba8fa 1545 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
b76c056b 1546 if (textconv_one)
aed6ca52 1547 free((char *)data_one);
b76c056b 1548 if (textconv_two)
aed6ca52 1549 free((char *)data_two);
6973dcae
JH
1550}
1551
f59a59e2
JS
1552struct diff_words_buffer {
1553 mmfile_t text;
071bcaab 1554 unsigned long alloc;
2e5d2003
JS
1555 struct diff_words_orig {
1556 const char *begin, *end;
1557 } *orig;
1558 int orig_nr, orig_alloc;
f59a59e2
JS
1559};
1560
1561static void diff_words_append(char *line, unsigned long len,
1562 struct diff_words_buffer *buffer)
1563{
23c1575f 1564 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
f59a59e2
JS
1565 line++;
1566 len--;
1567 memcpy(buffer->text.ptr + buffer->text.size, line, len);
1568 buffer->text.size += len;
2b6a5417 1569 buffer->text.ptr[buffer->text.size] = '\0';
f59a59e2
JS
1570}
1571
9cba13ca 1572struct diff_words_style_elem {
882749a0
TR
1573 const char *prefix;
1574 const char *suffix;
1575 const char *color; /* NULL; filled in by the setup code if
1576 * color is enabled */
1577};
1578
9cba13ca 1579struct diff_words_style {
882749a0
TR
1580 enum diff_words_type type;
1581 struct diff_words_style_elem new, old, ctx;
1582 const char *newline;
1583};
1584
c2e86add 1585static struct diff_words_style diff_words_styles[] = {
882749a0
TR
1586 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1587 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1588 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
1589};
1590
f59a59e2 1591struct diff_words_data {
f59a59e2 1592 struct diff_words_buffer minus, plus;
2e5d2003 1593 const char *current_plus;
4297c0ae
BY
1594 int last_minus;
1595 struct diff_options *opt;
2b6a5417 1596 regex_t *word_regex;
882749a0
TR
1597 enum diff_words_type type;
1598 struct diff_words_style *style;
f59a59e2
JS
1599};
1600
bd033291 1601static int fn_out_diff_words_write_helper(struct diff_options *o,
882749a0
TR
1602 struct diff_words_style_elem *st_el,
1603 const char *newline,
bd033291 1604 size_t count, const char *buf)
882749a0 1605{
4297c0ae 1606 int print = 0;
bd033291 1607 struct strbuf sb = STRBUF_INIT;
4297c0ae 1608
882749a0
TR
1609 while (count) {
1610 char *p = memchr(buf, '\n', count);
4297c0ae 1611 if (print)
bd033291
SB
1612 strbuf_addstr(&sb, diff_line_prefix(o));
1613
882749a0 1614 if (p != buf) {
bd033291
SB
1615 const char *reset = st_el->color && *st_el->color ?
1616 GIT_COLOR_RESET : NULL;
1617 if (st_el->color && *st_el->color)
1618 strbuf_addstr(&sb, st_el->color);
1619 strbuf_addstr(&sb, st_el->prefix);
1620 strbuf_add(&sb, buf, p ? p - buf : count);
1621 strbuf_addstr(&sb, st_el->suffix);
1622 if (reset)
1623 strbuf_addstr(&sb, reset);
882749a0
TR
1624 }
1625 if (!p)
bd033291
SB
1626 goto out;
1627
1628 strbuf_addstr(&sb, newline);
882749a0
TR
1629 count -= p + 1 - buf;
1630 buf = p + 1;
4297c0ae 1631 print = 1;
bd033291
SB
1632 if (count) {
1633 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1634 sb.buf, sb.len, 0);
1635 strbuf_reset(&sb);
1636 }
882749a0 1637 }
bd033291
SB
1638
1639out:
1640 if (sb.len)
1641 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1642 sb.buf, sb.len, 0);
1643 strbuf_release(&sb);
882749a0
TR
1644 return 0;
1645}
1646
4297c0ae
BY
1647/*
1648 * '--color-words' algorithm can be described as:
1649 *
5621760f 1650 * 1. collect the minus/plus lines of a diff hunk, divided into
4297c0ae
BY
1651 * minus-lines and plus-lines;
1652 *
1653 * 2. break both minus-lines and plus-lines into words and
1654 * place them into two mmfile_t with one word for each line;
1655 *
1656 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1657 *
1658 * And for the common parts of the both file, we output the plus side text.
1659 * diff_words->current_plus is used to trace the current position of the plus file
1660 * which printed. diff_words->last_minus is used to trace the last minus word
1661 * printed.
1662 *
1663 * For '--graph' to work with '--color-words', we need to output the graph prefix
1664 * on each line of color words output. Generally, there are two conditions on
1665 * which we should output the prefix.
1666 *
1667 * 1. diff_words->last_minus == 0 &&
1668 * diff_words->current_plus == diff_words->plus.text.ptr
1669 *
1670 * that is: the plus text must start as a new line, and if there is no minus
1671 * word printed, a graph prefix must be printed.
1672 *
1673 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1674 * *(diff_words->current_plus - 1) == '\n'
1675 *
1676 * that is: a graph prefix must be printed following a '\n'
1677 */
1678static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
1679{
1680 if ((diff_words->last_minus == 0 &&
1681 diff_words->current_plus == diff_words->plus.text.ptr) ||
1682 (diff_words->current_plus > diff_words->plus.text.ptr &&
1683 *(diff_words->current_plus - 1) == '\n')) {
1684 return 1;
1685 } else {
1686 return 0;
1687 }
1688}
1689
f59a59e2 1690static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
f59a59e2 1691{
f59a59e2 1692 struct diff_words_data *diff_words = priv;
882749a0 1693 struct diff_words_style *style = diff_words->style;
2e5d2003
JS
1694 int minus_first, minus_len, plus_first, plus_len;
1695 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
4297c0ae 1696 struct diff_options *opt = diff_words->opt;
30997bb8 1697 const char *line_prefix;
f59a59e2 1698
2e5d2003
JS
1699 if (line[0] != '@' || parse_hunk_header(line, len,
1700 &minus_first, &minus_len, &plus_first, &plus_len))
f59a59e2
JS
1701 return;
1702
4297c0ae 1703 assert(opt);
30997bb8 1704 line_prefix = diff_line_prefix(opt);
4297c0ae 1705
2e5d2003
JS
1706 /* POSIX requires that first be decremented by one if len == 0... */
1707 if (minus_len) {
1708 minus_begin = diff_words->minus.orig[minus_first].begin;
1709 minus_end =
1710 diff_words->minus.orig[minus_first + minus_len - 1].end;
1711 } else
1712 minus_begin = minus_end =
1713 diff_words->minus.orig[minus_first].end;
1714
1715 if (plus_len) {
1716 plus_begin = diff_words->plus.orig[plus_first].begin;
1717 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
1718 } else
1719 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
1720
4297c0ae
BY
1721 if (color_words_output_graph_prefix(diff_words)) {
1722 fputs(line_prefix, diff_words->opt->file);
1723 }
1724 if (diff_words->current_plus != plus_begin) {
bd033291 1725 fn_out_diff_words_write_helper(diff_words->opt,
882749a0
TR
1726 &style->ctx, style->newline,
1727 plus_begin - diff_words->current_plus,
bd033291 1728 diff_words->current_plus);
4297c0ae
BY
1729 }
1730 if (minus_begin != minus_end) {
bd033291 1731 fn_out_diff_words_write_helper(diff_words->opt,
882749a0 1732 &style->old, style->newline,
bd033291 1733 minus_end - minus_begin, minus_begin);
4297c0ae
BY
1734 }
1735 if (plus_begin != plus_end) {
bd033291 1736 fn_out_diff_words_write_helper(diff_words->opt,
882749a0 1737 &style->new, style->newline,
bd033291 1738 plus_end - plus_begin, plus_begin);
4297c0ae 1739 }
2e5d2003
JS
1740
1741 diff_words->current_plus = plus_end;
4297c0ae 1742 diff_words->last_minus = minus_first;
f59a59e2
JS
1743}
1744
2b6a5417
JS
1745/* This function starts looking at *begin, and returns 0 iff a word was found. */
1746static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
1747 int *begin, int *end)
1748{
1749 if (word_regex && *begin < buffer->size) {
1750 regmatch_t match[1];
b7d36ffc
JS
1751 if (!regexec_buf(word_regex, buffer->ptr + *begin,
1752 buffer->size - *begin, 1, match, 0)) {
2b6a5417
JS
1753 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
1754 '\n', match[0].rm_eo - match[0].rm_so);
1755 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
1756 *begin += match[0].rm_so;
1757 return *begin >= *end;
1758 }
1759 return -1;
f59a59e2
JS
1760 }
1761
2b6a5417
JS
1762 /* find the next word */
1763 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
1764 (*begin)++;
1765 if (*begin >= buffer->size)
1766 return -1;
f59a59e2 1767
2b6a5417
JS
1768 /* find the end of the word */
1769 *end = *begin + 1;
1770 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
1771 (*end)++;
1772
1773 return 0;
f59a59e2
JS
1774}
1775
23c1575f 1776/*
2e5d2003
JS
1777 * This function splits the words in buffer->text, stores the list with
1778 * newline separator into out, and saves the offsets of the original words
1779 * in buffer->orig.
23c1575f 1780 */
2b6a5417
JS
1781static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
1782 regex_t *word_regex)
f59a59e2 1783{
2e5d2003 1784 int i, j;
2b6a5417 1785 long alloc = 0;
f59a59e2 1786
2e5d2003 1787 out->size = 0;
2b6a5417 1788 out->ptr = NULL;
f59a59e2 1789
2e5d2003
JS
1790 /* fake an empty "0th" word */
1791 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
1792 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
1793 buffer->orig_nr = 1;
1794
1795 for (i = 0; i < buffer->text.size; i++) {
2b6a5417
JS
1796 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
1797 return;
2e5d2003
JS
1798
1799 /* store original boundaries */
1800 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
1801 buffer->orig_alloc);
1802 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
1803 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
1804 buffer->orig_nr++;
1805
1806 /* store one word */
2b6a5417 1807 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
2e5d2003
JS
1808 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
1809 out->ptr[out->size + j - i] = '\n';
1810 out->size += j - i + 1;
1811
1812 i = j - 1;
f59a59e2
JS
1813 }
1814}
1815
1816/* this executes the word diff on the accumulated buffers */
1817static void diff_words_show(struct diff_words_data *diff_words)
1818{
1819 xpparam_t xpp;
1820 xdemitconf_t xecfg;
f59a59e2 1821 mmfile_t minus, plus;
882749a0 1822 struct diff_words_style *style = diff_words->style;
f59a59e2 1823
4297c0ae 1824 struct diff_options *opt = diff_words->opt;
30997bb8 1825 const char *line_prefix;
4297c0ae
BY
1826
1827 assert(opt);
30997bb8 1828 line_prefix = diff_line_prefix(opt);
4297c0ae 1829
2e5d2003
JS
1830 /* special case: only removal */
1831 if (!diff_words->plus.text.size) {
bd033291
SB
1832 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
1833 line_prefix, strlen(line_prefix), 0);
1834 fn_out_diff_words_write_helper(diff_words->opt,
882749a0 1835 &style->old, style->newline,
4297c0ae 1836 diff_words->minus.text.size,
bd033291 1837 diff_words->minus.text.ptr);
2e5d2003
JS
1838 diff_words->minus.text.size = 0;
1839 return;
1840 }
1841
1842 diff_words->current_plus = diff_words->plus.text.ptr;
4297c0ae 1843 diff_words->last_minus = 0;
f59a59e2 1844
9ccd0a88 1845 memset(&xpp, 0, sizeof(xpp));
30b25010 1846 memset(&xecfg, 0, sizeof(xecfg));
2b6a5417
JS
1847 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
1848 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
582aa00b 1849 xpp.flags = 0;
2b6a5417 1850 /* as only the hunk header will be parsed, we need a 0-context */
2e5d2003 1851 xecfg.ctxlen = 0;
3efb9880
JK
1852 if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
1853 &xpp, &xecfg))
1854 die("unable to generate word diff");
f59a59e2
JS
1855 free(minus.ptr);
1856 free(plus.ptr);
2e5d2003 1857 if (diff_words->current_plus != diff_words->plus.text.ptr +
4297c0ae
BY
1858 diff_words->plus.text.size) {
1859 if (color_words_output_graph_prefix(diff_words))
bd033291
SB
1860 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
1861 line_prefix, strlen(line_prefix), 0);
1862 fn_out_diff_words_write_helper(diff_words->opt,
882749a0 1863 &style->ctx, style->newline,
2e5d2003 1864 diff_words->plus.text.ptr + diff_words->plus.text.size
bd033291 1865 - diff_words->current_plus, diff_words->current_plus);
4297c0ae 1866 }
f59a59e2 1867 diff_words->minus.text.size = diff_words->plus.text.size = 0;
f59a59e2
JS
1868}
1869
76fd2828
JH
1870/* In "color-words" mode, show word-diff of words accumulated in the buffer */
1871static void diff_words_flush(struct emit_callback *ecbdata)
1872{
e6e045f8
SB
1873 struct diff_options *wo = ecbdata->diff_words->opt;
1874
76fd2828
JH
1875 if (ecbdata->diff_words->minus.text.size ||
1876 ecbdata->diff_words->plus.text.size)
1877 diff_words_show(ecbdata->diff_words);
e6e045f8
SB
1878
1879 if (wo->emitted_symbols) {
1880 struct diff_options *o = ecbdata->opt;
1881 struct emitted_diff_symbols *wol = wo->emitted_symbols;
1882 int i;
1883
1884 /*
1885 * NEEDSWORK:
1886 * Instead of appending each, concat all words to a line?
1887 */
1888 for (i = 0; i < wol->nr; i++)
1889 append_emitted_diff_symbol(o, &wol->buf[i]);
1890
1891 for (i = 0; i < wol->nr; i++)
1892 free((void *)wol->buf[i].line);
1893
1894 wol->nr = 0;
1895 }
76fd2828
JH
1896}
1897
77d1a520
TR
1898static void diff_filespec_load_driver(struct diff_filespec *one)
1899{
1900 /* Use already-loaded driver */
1901 if (one->driver)
1902 return;
1903
1904 if (S_ISREG(one->mode))
1905 one->driver = userdiff_find_by_path(one->path);
1906
1907 /* Fallback to default settings */
1908 if (!one->driver)
1909 one->driver = userdiff_find_by_name("default");
1910}
1911
1912static const char *userdiff_word_regex(struct diff_filespec *one)
1913{
1914 diff_filespec_load_driver(one);
1915 return one->driver->word_regex;
1916}
1917
1918static void init_diff_words_data(struct emit_callback *ecbdata,
6440d341 1919 struct diff_options *orig_opts,
77d1a520
TR
1920 struct diff_filespec *one,
1921 struct diff_filespec *two)
1922{
1923 int i;
6440d341
TR
1924 struct diff_options *o = xmalloc(sizeof(struct diff_options));
1925 memcpy(o, orig_opts, sizeof(struct diff_options));
77d1a520
TR
1926
1927 ecbdata->diff_words =
1928 xcalloc(1, sizeof(struct diff_words_data));
1929 ecbdata->diff_words->type = o->word_diff;
1930 ecbdata->diff_words->opt = o;
e6e045f8
SB
1931
1932 if (orig_opts->emitted_symbols)
1933 o->emitted_symbols =
1934 xcalloc(1, sizeof(struct emitted_diff_symbols));
1935
77d1a520
TR
1936 if (!o->word_regex)
1937 o->word_regex = userdiff_word_regex(one);
1938 if (!o->word_regex)
1939 o->word_regex = userdiff_word_regex(two);
1940 if (!o->word_regex)
1941 o->word_regex = diff_word_regex_cfg;
1942 if (o->word_regex) {
1943 ecbdata->diff_words->word_regex = (regex_t *)
1944 xmalloc(sizeof(regex_t));
1945 if (regcomp(ecbdata->diff_words->word_regex,
1946 o->word_regex,
1947 REG_EXTENDED | REG_NEWLINE))
1948 die ("Invalid regular expression: %s",
1949 o->word_regex);
1950 }
1951 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
1952 if (o->word_diff == diff_words_styles[i].type) {
1953 ecbdata->diff_words->style =
1954 &diff_words_styles[i];
1955 break;
1956 }
1957 }
1958 if (want_color(o->use_color)) {
1959 struct diff_words_style *st = ecbdata->diff_words->style;
1960 st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
1961 st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
8dbf3eb6 1962 st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
77d1a520
TR
1963 }
1964}
1965
f59a59e2
JS
1966static void free_diff_words_data(struct emit_callback *ecbdata)
1967{
1968 if (ecbdata->diff_words) {
76fd2828 1969 diff_words_flush(ecbdata);
e6e045f8 1970 free (ecbdata->diff_words->opt->emitted_symbols);
6440d341 1971 free (ecbdata->diff_words->opt);
8e0f7003 1972 free (ecbdata->diff_words->minus.text.ptr);
2e5d2003 1973 free (ecbdata->diff_words->minus.orig);
8e0f7003 1974 free (ecbdata->diff_words->plus.text.ptr);
2e5d2003 1975 free (ecbdata->diff_words->plus.orig);
ef5644ea
BC
1976 if (ecbdata->diff_words->word_regex) {
1977 regfree(ecbdata->diff_words->word_regex);
1978 free(ecbdata->diff_words->word_regex);
1979 }
6a83d902 1980 FREE_AND_NULL(ecbdata->diff_words);
f59a59e2
JS
1981 }
1982}
1983
ce436973 1984const char *diff_get_color(int diff_use_color, enum color_diff ix)
cd112cef 1985{
daa0c3d9 1986 if (want_color(diff_use_color))
50f575fc
LT
1987 return diff_colors[ix];
1988 return "";
cd112cef
JS
1989}
1990
f1922234
JK
1991const char *diff_line_prefix(struct diff_options *opt)
1992{
1993 struct strbuf *msgbuf;
1994 if (!opt->output_prefix)
1995 return "";
1996
1997 msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
1998 return msgbuf->buf;
1999}
2000
23707811
JH
2001static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len)
2002{
2003 const char *cp;
2004 unsigned long allot;
2005 size_t l = len;
2006
23707811
JH
2007 cp = line;
2008 allot = l;
2009 while (0 < l) {
2010 (void) utf8_width(&cp, &l);
2011 if (!cp)
2012 break; /* truncated in the middle? */
2013 }
2014 return allot - l;
2015}
2016
d68fe26f 2017static void find_lno(const char *line, struct emit_callback *ecbdata)
690ed843 2018{
d68fe26f
JH
2019 const char *p;
2020 ecbdata->lno_in_preimage = 0;
2021 ecbdata->lno_in_postimage = 0;
2022 p = strchr(line, '-');
690ed843 2023 if (!p)
d68fe26f
JH
2024 return; /* cannot happen */
2025 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
2026 p = strchr(p, '+');
2027 if (!p)
2028 return; /* cannot happen */
2029 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
690ed843
JH
2030}
2031
cd112cef 2032static void fn_out_consume(void *priv, char *line, unsigned long len)
6973dcae 2033{
6973dcae 2034 struct emit_callback *ecbdata = priv;
ce436973 2035 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
7be57610 2036 struct diff_options *o = ecbdata->opt;
6973dcae 2037
ba16233c
SB
2038 o->found_changes = 1;
2039
3e97c7c6 2040 if (ecbdata->header) {
a29b0a13
SB
2041 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
2042 ecbdata->header->buf, ecbdata->header->len, 0);
3e97c7c6
GB
2043 strbuf_reset(ecbdata->header);
2044 ecbdata->header = NULL;
2045 }
34a5e1a2 2046
6973dcae 2047 if (ecbdata->label_path[0]) {
3ee8b7bf
SB
2048 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
2049 ecbdata->label_path[0],
2050 strlen(ecbdata->label_path[0]), 0);
2051 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
2052 ecbdata->label_path[1],
2053 strlen(ecbdata->label_path[1]), 0);
6973dcae
JH
2054 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
2055 }
cd112cef 2056
a624eaa7
JM
2057 if (diff_suppress_blank_empty
2058 && len == 2 && line[0] == ' ' && line[1] == '\n') {
2059 line[0] = '\n';
2060 len = 1;
2061 }
2062
b8d9c1a6 2063 if (line[0] == '@') {
76fd2828
JH
2064 if (ecbdata->diff_words)
2065 diff_words_flush(ecbdata);
23707811 2066 len = sane_truncate_line(ecbdata, line, len);
d68fe26f 2067 find_lno(line, ecbdata);
89cb73a1 2068 emit_hunk_header(ecbdata, line, len);
448c3ef1 2069 return;
cd112cef 2070 }
448c3ef1 2071
448c3ef1 2072 if (ecbdata->diff_words) {
ff958679
SB
2073 enum diff_symbol s =
2074 ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
2075 DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
448c3ef1
JH
2076 if (line[0] == '-') {
2077 diff_words_append(line, len,
2078 &ecbdata->diff_words->minus);
2079 return;
2080 } else if (line[0] == '+') {
2081 diff_words_append(line, len,
2082 &ecbdata->diff_words->plus);
2083 return;
59556548 2084 } else if (starts_with(line, "\\ ")) {
c7c2bc0a
TR
2085 /*
2086 * Eat the "no newline at eof" marker as if we
2087 * saw a "+" or "-" line with nothing on it,
2088 * and return without diff_words_flush() to
2089 * defer processing. If this is the end of
2090 * preimage, more "+" lines may come after it.
2091 */
2092 return;
448c3ef1 2093 }
76fd2828 2094 diff_words_flush(ecbdata);
ff958679 2095 emit_diff_symbol(o, s, line, len, 0);
448c3ef1
JH
2096 return;
2097 }
448c3ef1 2098
0e383e18
JH
2099 switch (line[0]) {
2100 case '+':
d68fe26f 2101 ecbdata->lno_in_postimage++;
018cff70 2102 emit_add_line(reset, ecbdata, line + 1, len - 1);
0e383e18
JH
2103 break;
2104 case '-':
2105 ecbdata->lno_in_preimage++;
2106 emit_del_line(reset, ecbdata, line + 1, len - 1);
2107 break;
2108 case ' ':
2109 ecbdata->lno_in_postimage++;
2110 ecbdata->lno_in_preimage++;
2111 emit_context_line(reset, ecbdata, line + 1, len - 1);
2112 break;
2113 default:
2114 /* incomplete line at the end */
2115 ecbdata->lno_in_preimage++;
f2bb1218
SB
2116 emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
2117 line, len, 0);
0e383e18 2118 break;
448c3ef1 2119 }
6973dcae
JH
2120}
2121
2122static char *pprint_rename(const char *a, const char *b)
2123{
2124 const char *old = a;
2125 const char *new = b;
f285a2d7 2126 struct strbuf name = STRBUF_INIT;
6973dcae 2127 int pfx_length, sfx_length;
dd281f09 2128 int pfx_adjust_for_slash;
6973dcae
JH
2129 int len_a = strlen(a);
2130 int len_b = strlen(b);
663af342 2131 int a_midlen, b_midlen;
e5bfbf9b
AJ
2132 int qlen_a = quote_c_style(a, NULL, NULL, 0);
2133 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2134
2135 if (qlen_a || qlen_b) {
663af342
PH
2136 quote_c_style(a, &name, NULL, 0);
2137 strbuf_addstr(&name, " => ");
2138 quote_c_style(b, &name, NULL, 0);
b315c5c0 2139 return strbuf_detach(&name, NULL);
e5bfbf9b 2140 }
6973dcae
JH
2141
2142 /* Find common prefix */
2143 pfx_length = 0;
2144 while (*old && *new && *old == *new) {
2145 if (*old == '/')
2146 pfx_length = old - a + 1;
2147 old++;
2148 new++;
2149 }
2150
2151 /* Find common suffix */
2152 old = a + len_a;
2153 new = b + len_b;
2154 sfx_length = 0;
d020e27f 2155 /*
dd281f09
TR
2156 * If there is a common prefix, it must end in a slash. In
2157 * that case we let this loop run 1 into the prefix to see the
2158 * same slash.
2159 *
2160 * If there is no common prefix, we cannot do this as it would
2161 * underrun the input strings.
d020e27f 2162 */
dd281f09
TR
2163 pfx_adjust_for_slash = (pfx_length ? 1 : 0);
2164 while (a + pfx_length - pfx_adjust_for_slash <= old &&
2165 b + pfx_length - pfx_adjust_for_slash <= new &&
d020e27f 2166 *old == *new) {
6973dcae
JH
2167 if (*old == '/')
2168 sfx_length = len_a - (old - a);
2169 old--;
2170 new--;
2171 }
2172
2173 /*
2174 * pfx{mid-a => mid-b}sfx
2175 * {pfx-a => pfx-b}sfx
2176 * pfx{sfx-a => sfx-b}
2177 * name-a => name-b
2178 */
663af342
PH
2179 a_midlen = len_a - pfx_length - sfx_length;
2180 b_midlen = len_b - pfx_length - sfx_length;
2181 if (a_midlen < 0)
2182 a_midlen = 0;
2183 if (b_midlen < 0)
2184 b_midlen = 0;
2185
2186 strbuf_grow(&name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
6973dcae 2187 if (pfx_length + sfx_length) {
663af342
PH
2188 strbuf_add(&name, a, pfx_length);
2189 strbuf_addch(&name, '{');
6973dcae 2190 }
663af342
PH
2191 strbuf_add(&name, a + pfx_length, a_midlen);
2192 strbuf_addstr(&name, " => ");
2193 strbuf_add(&name, b + pfx_length, b_midlen);
2194 if (pfx_length + sfx_length) {
2195 strbuf_addch(&name, '}');
2196 strbuf_add(&name, a + len_a - sfx_length, sfx_length);
6973dcae 2197 }
b315c5c0 2198 return strbuf_detach(&name, NULL);
6973dcae
JH
2199}
2200
2201struct diffstat_t {
6973dcae
JH
2202 int nr;
2203 int alloc;
2204 struct diffstat_file {
f604652e 2205 char *from_name;
6973dcae 2206 char *name;
f604652e 2207 char *print_name;
6973dcae
JH
2208 unsigned is_unmerged:1;
2209 unsigned is_binary:1;
2210 unsigned is_renamed:1;
74faaa16 2211 unsigned is_interesting:1;
0974c117 2212 uintmax_t added, deleted;
6973dcae
JH
2213 } **files;
2214};
2215
2216static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2217 const char *name_a,
2218 const char *name_b)
2219{
2220 struct diffstat_file *x;
1a4927c5 2221 x = xcalloc(1, sizeof(*x));
4c960a43 2222 ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
6973dcae
JH
2223 diffstat->files[diffstat->nr++] = x;
2224 if (name_b) {
f604652e
JH
2225 x->from_name = xstrdup(name_a);
2226 x->name = xstrdup(name_b);
6973dcae
JH
2227 x->is_renamed = 1;
2228 }
f604652e
JH
2229 else {
2230 x->from_name = NULL;
9befac47 2231 x->name = xstrdup(name_a);
f604652e 2232 }
6973dcae
JH
2233 return x;
2234}
2235
2236static void diffstat_consume(void *priv, char *line, unsigned long len)
2237{
2238 struct diffstat_t *diffstat = priv;
2239 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
2240
2241 if (line[0] == '+')
2242 x->added++;
2243 else if (line[0] == '-')
2244 x->deleted++;
2245}
2246
698ce6f8 2247const char mime_boundary_leader[] = "------------";
6973dcae 2248
a2540023
JH
2249static int scale_linear(int it, int width, int max_change)
2250{
2eeeef24
JH
2251 if (!it)
2252 return 0;
a2540023 2253 /*
2eeeef24
JH
2254 * make sure that at least one '-' or '+' is printed if
2255 * there is any change to this path. The easiest way is to
2256 * scale linearly as if the alloted width is one column shorter
2257 * than it is, and then add 1 to the result.
a2540023 2258 */
2eeeef24 2259 return 1 + (it * (width - 1) / max_change);
a2540023
JH
2260}
2261
0911c475
SB
2262static void show_graph(struct strbuf *out, char ch, int cnt,
2263 const char *set, const char *reset)
a2540023
JH
2264{
2265 if (cnt <= 0)
2266 return;
0911c475
SB
2267 strbuf_addstr(out, set);
2268 strbuf_addchars(out, ch, cnt);
2269 strbuf_addstr(out, reset);
a2540023
JH
2270}
2271
f604652e
JH
2272static void fill_print_name(struct diffstat_file *file)
2273{
2274 char *pname;
2275
2276 if (file->print_name)
2277 return;
2278
2279 if (!file->is_renamed) {
f285a2d7 2280 struct strbuf buf = STRBUF_INIT;
f604652e
JH
2281 if (quote_c_style(file->name, &buf, NULL, 0)) {
2282 pname = strbuf_detach(&buf, NULL);
2283 } else {
2284 pname = file->name;
2285 strbuf_release(&buf);
2286 }
2287 } else {
2288 pname = pprint_rename(file->from_name, file->name);
2289 }
2290 file->print_name = pname;
2291}
2292
0911c475
SB
2293static void print_stat_summary_inserts_deletes(struct diff_options *options,
2294 int files, int insertions, int deletions)
7f814632
NTND
2295{
2296 struct strbuf sb = STRBUF_INIT;
7f814632
NTND
2297
2298 if (!files) {
2299 assert(insertions == 0 && deletions == 0);
0911c475
SB
2300 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
2301 NULL, 0, 0);
2302 return;
7f814632
NTND
2303 }
2304
2305 strbuf_addf(&sb,
218adaaa 2306 (files == 1) ? " %d file changed" : " %d files changed",
7f814632
NTND
2307 files);
2308
2309 /*
2310 * For binary diff, the caller may want to print "x files
2311 * changed" with insertions == 0 && deletions == 0.
2312 *
2313 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2314 * is probably less confusing (i.e skip over "2 files changed
2315 * but nothing about added/removed lines? Is this a bug in Git?").
2316 */
2317 if (insertions || deletions == 0) {
7f814632 2318 strbuf_addf(&sb,
218adaaa 2319 (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
7f814632
NTND
2320 insertions);
2321 }
2322
2323 if (deletions || insertions == 0) {
7f814632 2324 strbuf_addf(&sb,
218adaaa 2325 (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
7f814632
NTND
2326 deletions);
2327 }
2328 strbuf_addch(&sb, '\n');
0911c475
SB
2329 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
2330 sb.buf, sb.len, 0);
7f814632 2331 strbuf_release(&sb);
0911c475
SB
2332}
2333
2334void print_stat_summary(FILE *fp, int files,
2335 int insertions, int deletions)
2336{
2337 struct diff_options o;
2338 memset(&o, 0, sizeof(o));
2339 o.file = fp;
2340
2341 print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
7f814632
NTND
2342}
2343
4b25d091 2344static void show_stats(struct diffstat_t *data, struct diff_options *options)
6973dcae 2345{
eb3a9dd3 2346 int i, len, add, del, adds = 0, dels = 0;
0974c117 2347 uintmax_t max_change = 0, max_len = 0;
dc801e71
ZJS
2348 int total_files = data->nr, count;
2349 int width, name_width, graph_width, number_width = 0, bin_width = 0;
c0aa335c 2350 const char *reset, *add_c, *del_c;
e5f85df8 2351 int extra_shown = 0;
0911c475
SB
2352 const char *line_prefix = diff_line_prefix(options);
2353 struct strbuf out = STRBUF_INIT;
6973dcae
JH
2354
2355 if (data->nr == 0)
2356 return;
2357
808e1db2 2358 count = options->stat_count ? options->stat_count : data->nr;
a2540023 2359
8f67f8ae 2360 reset = diff_get_color_opt(options, DIFF_RESET);
8f67f8ae
PH
2361 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
2362 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
785f7432 2363
1b058bc3
ZJS
2364 /*
2365 * Find the longest filename and max number of changes
2366 */
808e1db2 2367 for (i = 0; (i < count) && (i < data->nr); i++) {
6973dcae 2368 struct diffstat_file *file = data->files[i];
0974c117 2369 uintmax_t change = file->added + file->deleted;
af0ed819
JH
2370
2371 if (!file->is_interesting && (change == 0)) {
808e1db2 2372 count++; /* not shown == room for one more */
358e460e
MG
2373 continue;
2374 }
f604652e
JH
2375 fill_print_name(file);
2376 len = strlen(file->print_name);
6973dcae
JH
2377 if (max_len < len)
2378 max_len = len;
2379
dc801e71
ZJS
2380 if (file->is_unmerged) {
2381 /* "Unmerged" is 8 characters */
2382 bin_width = bin_width < 8 ? 8 : bin_width;
6973dcae 2383 continue;
dc801e71
ZJS
2384 }
2385 if (file->is_binary) {
2386 /* "Bin XXX -> YYY bytes" */
2387 int w = 14 + decimal_width(file->added)
2388 + decimal_width(file->deleted);
2389 bin_width = bin_width < w ? w : bin_width;
2390 /* Display change counts aligned with "Bin" */
2391 number_width = 3;
2392 continue;
2393 }
2394
a2540023
JH
2395 if (max_change < change)
2396 max_change = change;
6973dcae 2397 }
a20d3c0d 2398 count = i; /* where we can stop scanning in data->files[] */
6973dcae 2399
1b058bc3
ZJS
2400 /*
2401 * We have width = stat_width or term_columns() columns total.
2402 * We want a maximum of min(max_len, stat_name_width) for the name part.
969fe57b 2403 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
1b058bc3
ZJS
2404 * We also need 1 for " " and 4 + decimal_width(max_change)
2405 * for " | NNNN " and one the empty column at the end, altogether
2406 * 6 + decimal_width(max_change).
2407 *
2408 * If there's not enough space, we will use the smaller of
2409 * stat_name_width (if set) and 5/8*width for the filename,
969fe57b
ZJS
2410 * and the rest for constant elements + graph part, but no more
2411 * than stat_graph_width for the graph part.
1b058bc3
ZJS
2412 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2413 * for the standard terminal size).
a2540023 2414 *
1b058bc3
ZJS
2415 * In other words: stat_width limits the maximum width, and
2416 * stat_name_width fixes the maximum width of the filename,
2417 * and is also used to divide available columns if there
2418 * aren't enough.
dc801e71
ZJS
2419 *
2420 * Binary files are displayed with "Bin XXX -> YYY bytes"
2421 * instead of the change count and graph. This part is treated
2422 * similarly to the graph part, except that it is not
41ccfdd9 2423 * "scaled". If total width is too small to accommodate the
dc801e71
ZJS
2424 * guaranteed minimum width of the filename part and the
2425 * separators and this message, this message will "overflow"
2426 * making the line longer than the maximum width.
a2540023 2427 */
1b058bc3
ZJS
2428
2429 if (options->stat_width == -1)
cd48dadb 2430 width = term_columns() - strlen(line_prefix);
a2540023 2431 else
1b058bc3 2432 width = options->stat_width ? options->stat_width : 80;
dc801e71
ZJS
2433 number_width = decimal_width(max_change) > number_width ?
2434 decimal_width(max_change) : number_width;
a2540023 2435
df44483a
ZJS
2436 if (options->stat_graph_width == -1)
2437 options->stat_graph_width = diff_stat_graph_width;
a2540023 2438
1b058bc3
ZJS
2439 /*
2440 * Guarantee 3/8*16==6 for the graph part
2441 * and 5/8*16==10 for the filename part
2442 */
2443 if (width < 16 + 6 + number_width)
2444 width = 16 + 6 + number_width;
2445
2446 /*
2447 * First assign sizes that are wanted, ignoring available width.
dc801e71
ZJS
2448 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2449 * starting from "XXX" should fit in graph_width.
1b058bc3 2450 */
dc801e71
ZJS
2451 graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
2452 if (options->stat_graph_width &&
2453 options->stat_graph_width < graph_width)
2454 graph_width = options->stat_graph_width;
2455
1b058bc3
ZJS
2456 name_width = (options->stat_name_width > 0 &&
2457 options->stat_name_width < max_len) ?
2458 options->stat_name_width : max_len;
2459
2460 /*
2461 * Adjust adjustable widths not to exceed maximum width
2462 */
2463 if (name_width + number_width + 6 + graph_width > width) {
678c5741 2464 if (graph_width > width * 3/8 - number_width - 6) {
1b058bc3 2465 graph_width = width * 3/8 - number_width - 6;
678c5741
LP
2466 if (graph_width < 6)
2467 graph_width = 6;
2468 }
2469
969fe57b
ZJS
2470 if (options->stat_graph_width &&
2471 graph_width > options->stat_graph_width)
2472 graph_width = options->stat_graph_width;
1b058bc3
ZJS
2473 if (name_width > width - number_width - 6 - graph_width)
2474 name_width = width - number_width - 6 - graph_width;
2475 else
2476 graph_width = width - number_width - 6 - name_width;
2477 }
2478
2479 /*
2480 * From here name_width is the width of the name area,
2481 * and graph_width is the width of the graph area.
2482 * max_change is used to scale graph properly.
2483 */
808e1db2 2484 for (i = 0; i < count; i++) {
d2543b8e 2485 const char *prefix = "";
af0ed819
JH
2486 struct diffstat_file *file = data->files[i];
2487 char *name = file->print_name;
2488 uintmax_t added = file->added;
2489 uintmax_t deleted = file->deleted;
a2540023 2490 int name_len;
6973dcae 2491
a20d3c0d 2492 if (!file->is_interesting && (added + deleted == 0))
358e460e 2493 continue;
a20d3c0d 2494
6973dcae
JH
2495 /*
2496 * "scale" the filename
2497 */
a2540023
JH
2498 len = name_width;
2499 name_len = strlen(name);
2500 if (name_width < name_len) {
6973dcae
JH
2501 char *slash;
2502 prefix = "...";
a2540023
JH
2503 len -= 3;
2504 name += name_len - len;
6973dcae
JH
2505 slash = strchr(name, '/');
2506 if (slash)
2507 name = slash;
2508 }
6973dcae 2509
af0ed819 2510 if (file->is_binary) {
0911c475
SB
2511 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2512 strbuf_addf(&out, " %*s", number_width, "Bin");
e18872b2 2513 if (!added && !deleted) {
0911c475
SB
2514 strbuf_addch(&out, '\n');
2515 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2516 out.buf, out.len, 0);
2517 strbuf_reset(&out);
e18872b2
ZJS
2518 continue;
2519 }
0911c475 2520 strbuf_addf(&out, " %s%"PRIuMAX"%s",
0974c117 2521 del_c, deleted, reset);
0911c475
SB
2522 strbuf_addstr(&out, " -> ");
2523 strbuf_addf(&out, "%s%"PRIuMAX"%s",
0974c117 2524 add_c, added, reset);
0911c475
SB
2525 strbuf_addstr(&out, " bytes\n");
2526 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2527 out.buf, out.len, 0);
2528 strbuf_reset(&out);
f604652e 2529 continue;
6973dcae 2530 }
af0ed819 2531 else if (file->is_unmerged) {
0911c475
SB
2532 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2533 strbuf_addstr(&out, " Unmerged\n");
2534 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2535 out.buf, out.len, 0);
2536 strbuf_reset(&out);
f604652e 2537 continue;
6973dcae 2538 }
6973dcae 2539
a2540023
JH
2540 /*
2541 * scale the add/delete
2542 */
6973dcae
JH
2543 add = added;
2544 del = deleted;
6973dcae 2545
1b058bc3 2546 if (graph_width <= max_change) {
d3c9cf32 2547 int total = scale_linear(add + del, graph_width, max_change);
2eeeef24
JH
2548 if (total < 2 && add && del)
2549 /* width >= 2 due to the sanity check */
2550 total = 2;
2551 if (add < del) {
1b058bc3 2552 add = scale_linear(add, graph_width, max_change);
2eeeef24
JH
2553 del = total - add;
2554 } else {
1b058bc3 2555 del = scale_linear(del, graph_width, max_change);
2eeeef24
JH
2556 add = total - del;
2557 }
6973dcae 2558 }
0911c475
SB
2559 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2560 strbuf_addf(&out, " %*"PRIuMAX"%s",
dc801e71
ZJS
2561 number_width, added + deleted,
2562 added + deleted ? " " : "");
0911c475
SB
2563 show_graph(&out, '+', add, add_c, reset);
2564 show_graph(&out, '-', del, del_c, reset);
2565 strbuf_addch(&out, '\n');
2566 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2567 out.buf, out.len, 0);
2568 strbuf_reset(&out);
c0c77734 2569 }
a20d3c0d
JH
2570
2571 for (i = 0; i < data->nr; i++) {
af0ed819
JH
2572 struct diffstat_file *file = data->files[i];
2573 uintmax_t added = file->added;
2574 uintmax_t deleted = file->deleted;
82dfc2c4
JH
2575
2576 if (file->is_unmerged ||
2577 (!file->is_interesting && (added + deleted == 0))) {
808e1db2
MG
2578 total_files--;
2579 continue;
2580 }
a20d3c0d 2581
82dfc2c4 2582 if (!file->is_binary) {
a20d3c0d
JH
2583 adds += added;
2584 dels += deleted;
2585 }
2586 if (i < count)
2587 continue;
e5f85df8 2588 if (!extra_shown)
0911c475
SB
2589 emit_diff_symbol(options,
2590 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
2591 NULL, 0, 0);
e5f85df8 2592 extra_shown = 1;
808e1db2 2593 }
0911c475
SB
2594
2595 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
5a612017 2596 strbuf_release(&out);
6973dcae
JH
2597}
2598
2775d92c 2599static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
ebd124c6
NP
2600{
2601 int i, adds = 0, dels = 0, total_files = data->nr;
2602
2603 if (data->nr == 0)
2604 return;
2605
2606 for (i = 0; i < data->nr; i++) {
e18872b2 2607 int added = data->files[i]->added;
0911c475 2608 int deleted = data->files[i]->deleted;
e18872b2 2609
20c8cde4
JH
2610 if (data->files[i]->is_unmerged ||
2611 (!data->files[i]->is_interesting && (added + deleted == 0))) {
e18872b2 2612 total_files--;
de9658b5 2613 } else if (!data->files[i]->is_binary) { /* don't count bytes */
e18872b2
ZJS
2614 adds += added;
2615 dels += deleted;
ebd124c6 2616 }
ebd124c6 2617 }
0911c475 2618 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
ebd124c6
NP
2619}
2620
4b25d091 2621static void show_numstat(struct diffstat_t *data, struct diff_options *options)
74e2abe5
JH
2622{
2623 int i;
2624
f604652e
JH
2625 if (data->nr == 0)
2626 return;
2627
74e2abe5
JH
2628 for (i = 0; i < data->nr; i++) {
2629 struct diffstat_file *file = data->files[i];
2630
30997bb8 2631 fprintf(options->file, "%s", diff_line_prefix(options));
7be57610 2632
bfddbc5e 2633 if (file->is_binary)
c0c77734 2634 fprintf(options->file, "-\t-\t");
bfddbc5e 2635 else
c0c77734 2636 fprintf(options->file,
0974c117
JK
2637 "%"PRIuMAX"\t%"PRIuMAX"\t",
2638 file->added, file->deleted);
f604652e
JH
2639 if (options->line_termination) {
2640 fill_print_name(file);
2641 if (!file->is_renamed)
c0c77734 2642 write_name_quoted(file->name, options->file,
f604652e
JH
2643 options->line_termination);
2644 else {
c0c77734
DB
2645 fputs(file->print_name, options->file);
2646 putc(options->line_termination, options->file);
f604652e 2647 }
663af342 2648 } else {
f604652e 2649 if (file->is_renamed) {
c0c77734
DB
2650 putc('\0', options->file);
2651 write_name_quoted(file->from_name, options->file, '\0');
f604652e 2652 }
c0c77734 2653 write_name_quoted(file->name, options->file, '\0');
663af342 2654 }
74e2abe5
JH
2655 }
2656}
2657
c04a7155
JH
2658struct dirstat_file {
2659 const char *name;
2660 unsigned long changed;
7df7c019
LT
2661};
2662
c04a7155
JH
2663struct dirstat_dir {
2664 struct dirstat_file *files;
712d2c7d 2665 int alloc, nr, permille, cumulative;
c04a7155
JH
2666};
2667
7be57610
BY
2668static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
2669 unsigned long changed, const char *base, int baselen)
7df7c019
LT
2670{
2671 unsigned long this_dir = 0;
2672 unsigned int sources = 0;
30997bb8 2673 const char *line_prefix = diff_line_prefix(opt);
7df7c019
LT
2674
2675 while (dir->nr) {
c04a7155 2676 struct dirstat_file *f = dir->files;
7df7c019
LT
2677 int namelen = strlen(f->name);
2678 unsigned long this;
2679 char *slash;
2680
2681 if (namelen < baselen)
2682 break;
2683 if (memcmp(f->name, base, baselen))
2684 break;
2685 slash = strchr(f->name + baselen, '/');
2686 if (slash) {
2687 int newbaselen = slash + 1 - f->name;
7be57610 2688 this = gather_dirstat(opt, dir, changed, f->name, newbaselen);
7df7c019
LT
2689 sources++;
2690 } else {
c04a7155 2691 this = f->changed;
7df7c019
LT
2692 dir->files++;
2693 dir->nr--;
2694 sources += 2;
2695 }
2696 this_dir += this;
2697 }
2698
2699 /*
2700 * We don't report dirstat's for
2701 * - the top level
2702 * - or cases where everything came from a single directory
2703 * under this directory (sources == 1).
2704 */
2705 if (baselen && sources != 1) {
58a8756a
JH
2706 if (this_dir) {
2707 int permille = this_dir * 1000 / changed;
712d2c7d 2708 if (permille >= dir->permille) {
7be57610 2709 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
712d2c7d 2710 permille / 10, permille % 10, baselen, base);
7df7c019
LT
2711 if (!dir->cumulative)
2712 return 0;
2713 }
2714 }
2715 }
2716 return this_dir;
2717}
2718
441bca0b
LT
2719static int dirstat_compare(const void *_a, const void *_b)
2720{
2721 const struct dirstat_file *a = _a;
2722 const struct dirstat_file *b = _b;
2723 return strcmp(a->name, b->name);
2724}
2725
c04a7155 2726static void show_dirstat(struct diff_options *options)
7df7c019
LT
2727{
2728 int i;
2729 unsigned long changed;
c04a7155
JH
2730 struct dirstat_dir dir;
2731 struct diff_queue_struct *q = &diff_queued_diff;
2732
2733 dir.files = NULL;
2734 dir.alloc = 0;
2735 dir.nr = 0;
712d2c7d 2736 dir.permille = options->dirstat_permille;
f88d225f 2737 dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
7df7c019 2738
7df7c019 2739 changed = 0;
c04a7155
JH
2740 for (i = 0; i < q->nr; i++) {
2741 struct diff_filepair *p = q->queue[i];
2742 const char *name;
2743 unsigned long copied, added, damage;
0133dab7 2744 int content_changed;
c04a7155 2745
2ca86714 2746 name = p->two->path ? p->two->path : p->one->path;
c04a7155 2747
41c9560e 2748 if (p->one->oid_valid && p->two->oid_valid)
a0d12c44 2749 content_changed = oidcmp(&p->one->oid, &p->two->oid);
0133dab7
JH
2750 else
2751 content_changed = 1;
2752
2ff3a803
JH
2753 if (!content_changed) {
2754 /*
2755 * The SHA1 has not changed, so pre-/post-content is
2756 * identical. We can therefore skip looking at the
2757 * file contents altogether.
2758 */
2759 damage = 0;
2760 goto found_damage;
2761 }
2762
0133dab7
JH
2763 if (DIFF_OPT_TST(options, DIRSTAT_BY_FILE)) {
2764 /*
2765 * In --dirstat-by-file mode, we don't really need to
2766 * look at the actual file contents at all.
2767 * The fact that the SHA1 changed is enough for us to
2768 * add this file to the list of results
2769 * (with each file contributing equal damage).
2770 */
2ff3a803 2771 damage = 1;
0133dab7
JH
2772 goto found_damage;
2773 }
c04a7155
JH
2774
2775 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
2776 diff_populate_filespec(p->one, 0);
2777 diff_populate_filespec(p->two, 0);
974e0044 2778 diffcore_count_changes(p->one, p->two, NULL, NULL,
c04a7155
JH
2779 &copied, &added);
2780 diff_free_filespec_data(p->one);
2781 diff_free_filespec_data(p->two);
2782 } else if (DIFF_FILE_VALID(p->one)) {
8e5dd3d6 2783 diff_populate_filespec(p->one, CHECK_SIZE_ONLY);
c04a7155
JH
2784 copied = added = 0;
2785 diff_free_filespec_data(p->one);
2786 } else if (DIFF_FILE_VALID(p->two)) {
8e5dd3d6 2787 diff_populate_filespec(p->two, CHECK_SIZE_ONLY);
c04a7155
JH
2788 copied = 0;
2789 added = p->two->size;
2790 diff_free_filespec_data(p->two);
2791 } else
2b0b551d 2792 continue;
c04a7155
JH
2793
2794 /*
2795 * Original minus copied is the removed material,
2796 * added is the new material. They are both damages
0133dab7 2797 * made to the preimage.
2ff3a803
JH
2798 * If the resulting damage is zero, we know that
2799 * diffcore_count_changes() considers the two entries to
2800 * be identical, but since content_changed is true, we
2801 * know that there must have been _some_ kind of change,
2802 * so we force all entries to have damage > 0.
c04a7155
JH
2803 */
2804 damage = (p->one->size - copied) + added;
2ff3a803 2805 if (!damage)
fd33777b 2806 damage = 1;
c04a7155 2807
0133dab7 2808found_damage:
c04a7155
JH
2809 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
2810 dir.files[dir.nr].name = name;
2811 dir.files[dir.nr].changed = damage;
2812 changed += damage;
2813 dir.nr++;
7df7c019
LT
2814 }
2815
2816 /* This can happen even with many files, if everything was renames */
2817 if (!changed)
2818 return;
2819
2820 /* Show all directories with more than x% of the changes */
9ed0d8d6 2821 QSORT(dir.files, dir.nr, dirstat_compare);
7be57610 2822 gather_dirstat(options, &dir, changed, "", 0);
7df7c019
LT
2823}
2824
1c57a627
JH
2825static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
2826{
2827 int i;
2828 unsigned long changed;
2829 struct dirstat_dir dir;
2830
2831 if (data->nr == 0)
2832 return;
2833
2834 dir.files = NULL;
2835 dir.alloc = 0;
2836 dir.nr = 0;
2837 dir.permille = options->dirstat_permille;
2838 dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
2839
2840 changed = 0;
2841 for (i = 0; i < data->nr; i++) {
2842 struct diffstat_file *file = data->files[i];
2843 unsigned long damage = file->added + file->deleted;
2844 if (file->is_binary)
2845 /*
2846 * binary files counts bytes, not lines. Must find some
2847 * way to normalize binary bytes vs. textual lines.
2848 * The following heuristic assumes that there are 64
2849 * bytes per "line".
2850 * This is stupid and ugly, but very cheap...
2851 */
42c78a21 2852 damage = DIV_ROUND_UP(damage, 64);
1c57a627
JH
2853 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
2854 dir.files[dir.nr].name = file->name;
2855 dir.files[dir.nr].changed = damage;
2856 changed += damage;
2857 dir.nr++;
2858 }
2859
2860 /* This can happen even with many files, if everything was renames */
2861 if (!changed)
2862 return;
2863
2864 /* Show all directories with more than x% of the changes */
9ed0d8d6 2865 QSORT(dir.files, dir.nr, dirstat_compare);
1c57a627
JH
2866 gather_dirstat(options, &dir, changed, "", 0);
2867}
2868
f604652e
JH
2869static void free_diffstat_info(struct diffstat_t *diffstat)
2870{
2871 int i;
2872 for (i = 0; i < diffstat->nr; i++) {
2873 struct diffstat_file *f = diffstat->files[i];
2874 if (f->name != f->print_name)
2875 free(f->print_name);
2876 free(f->name);
2877 free(f->from_name);
2878 free(f);
2879 }
2880 free(diffstat->files);
2881}
2882
88246898 2883struct checkdiff_t {
88246898 2884 const char *filename;
1ba111d1 2885 int lineno;
a757c646 2886 int conflict_marker_size;
1ba111d1 2887 struct diff_options *o;
cf1b7869 2888 unsigned ws_rule;
62c64895 2889 unsigned status;
88246898
JS
2890};
2891
a757c646 2892static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
04954043
JH
2893{
2894 char firstchar;
2895 int cnt;
2896
a757c646 2897 if (len < marker_size + 1)
04954043
JH
2898 return 0;
2899 firstchar = line[0];
2900 switch (firstchar) {
a757c646 2901 case '=': case '>': case '<': case '|':
04954043
JH
2902 break;
2903 default:
2904 return 0;
2905 }
a757c646 2906 for (cnt = 1; cnt < marker_size; cnt++)
04954043
JH
2907 if (line[cnt] != firstchar)
2908 return 0;
a757c646
JH
2909 /* line[1] thru line[marker_size-1] are same as firstchar */
2910 if (len < marker_size + 1 || !isspace(line[marker_size]))
04954043 2911 return 0;
04954043
JH
2912 return 1;
2913}
2914
88246898
JS
2915static void checkdiff_consume(void *priv, char *line, unsigned long len)
2916{
2917 struct checkdiff_t *data = priv;
a757c646 2918 int marker_size = data->conflict_marker_size;
f1c96261
JK
2919 const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
2920 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
2921 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
c1795bb0 2922 char *err;
30997bb8 2923 const char *line_prefix;
7be57610
BY
2924
2925 assert(data->o);
30997bb8 2926 line_prefix = diff_line_prefix(data->o);
88246898
JS
2927
2928 if (line[0] == '+') {
18374e58 2929 unsigned bad;
0ef617f4 2930 data->lineno++;
a757c646 2931 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
04954043
JH
2932 data->status |= 1;
2933 fprintf(data->o->file,
7be57610
BY
2934 "%s%s:%d: leftover conflict marker\n",
2935 line_prefix, data->filename, data->lineno);
04954043 2936 }
8f8841e9 2937 bad = ws_check(line + 1, len - 1, data->ws_rule);
18374e58 2938 if (!bad)
c1795bb0 2939 return;
18374e58
JH
2940 data->status |= bad;
2941 err = whitespace_error_string(bad);
7be57610
BY
2942 fprintf(data->o->file, "%s%s:%d: %s.\n",
2943 line_prefix, data->filename, data->lineno, err);
c1795bb0 2944 free(err);
a3c158d4 2945 emit_line(data->o, set, reset, line, 1);
8f8841e9 2946 ws_check_emit(line + 1, len - 1, data->ws_rule,
1ba111d1 2947 data->o->file, set, reset, ws);
877f23cc 2948 } else if (line[0] == ' ') {
88246898 2949 data->lineno++;
877f23cc 2950 } else if (line[0] == '@') {
88246898
JS
2951 char *plus = strchr(line, '+');
2952 if (plus)
0ef617f4 2953 data->lineno = strtol(plus, NULL, 10) - 1;
88246898
JS
2954 else
2955 die("invalid diff");
2956 }
2957}
2958
0660626c
JH
2959static unsigned char *deflate_it(char *data,
2960 unsigned long size,
2961 unsigned long *result_size)
051308f6 2962{
0660626c
JH
2963 int bound;
2964 unsigned char *deflated;
ef49a7a0 2965 git_zstream stream;
0660626c 2966
55bb5c91 2967 git_deflate_init(&stream, zlib_compression_level);
225a6f10 2968 bound = git_deflate_bound(&stream, size);
0660626c
JH
2969 deflated = xmalloc(bound);
2970 stream.next_out = deflated;
2971 stream.avail_out = bound;
2972
2973 stream.next_in = (unsigned char *)data;
2974 stream.avail_in = size;
55bb5c91 2975 while (git_deflate(&stream, Z_FINISH) == Z_OK)
0660626c 2976 ; /* nothing */
55bb5c91 2977 git_deflate_end(&stream);
0660626c
JH
2978 *result_size = stream.total_out;
2979 return deflated;
051308f6
JH
2980}
2981
4eed0ebd
SB
2982static void emit_binary_diff_body(struct diff_options *o,
2983 mmfile_t *one, mmfile_t *two)
051308f6 2984{
0660626c
JH
2985 void *cp;
2986 void *delta;
2987 void *deflated;
2988 void *data;
2989 unsigned long orig_size;
2990 unsigned long delta_size;
2991 unsigned long deflate_size;
2992 unsigned long data_size;
051308f6 2993
0660626c
JH
2994 /* We could do deflated delta, or we could do just deflated two,
2995 * whichever is smaller.
051308f6 2996 */
0660626c
JH
2997 delta = NULL;
2998 deflated = deflate_it(two->ptr, two->size, &deflate_size);
2999 if (one->size && two->size) {
3000 delta = diff_delta(one->ptr, one->size,
3001 two->ptr, two->size,
3002 &delta_size, deflate_size);
3003 if (delta) {
3004 void *to_free = delta;
3005 orig_size = delta_size;
3006 delta = deflate_it(delta, delta_size, &delta_size);
3007 free(to_free);
051308f6
JH
3008 }
3009 }
051308f6 3010
0660626c 3011 if (delta && delta_size < deflate_size) {
4eed0ebd
SB
3012 char *s = xstrfmt("%lu", orig_size);
3013 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
3014 s, strlen(s), 0);
3015 free(s);
0660626c
JH
3016 free(deflated);
3017 data = delta;
3018 data_size = delta_size;
4eed0ebd
SB
3019 } else {
3020 char *s = xstrfmt("%lu", two->size);
3021 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
3022 s, strlen(s), 0);
3023 free(s);
0660626c
JH
3024 free(delta);
3025 data = deflated;
3026 data_size = deflate_size;
3027 }
051308f6 3028
0660626c
JH
3029 /* emit data encoded in base85 */
3030 cp = data;
3031 while (data_size) {
4eed0ebd 3032 int len;
0660626c 3033 int bytes = (52 < data_size) ? 52 : data_size;
4eed0ebd 3034 char line[71];
0660626c 3035 data_size -= bytes;
051308f6
JH
3036 if (bytes <= 26)
3037 line[0] = bytes + 'A' - 1;
3038 else
3039 line[0] = bytes - 26 + 'a' - 1;
3040 encode_85(line + 1, cp, bytes);
1d7f171c 3041 cp = (char *) cp + bytes;
4eed0ebd
SB
3042
3043 len = strlen(line);
3044 line[len++] = '\n';
3045 line[len] = '\0';
3046
3047 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
3048 line, len, 0);
051308f6 3049 }
4eed0ebd 3050 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
0660626c 3051 free(data);
051308f6
JH
3052}
3053
4eed0ebd
SB
3054static void emit_binary_diff(struct diff_options *o,
3055 mmfile_t *one, mmfile_t *two)
d4c452f0 3056{
4eed0ebd
SB
3057 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
3058 emit_binary_diff_body(o, one, two);
3059 emit_binary_diff_body(o, two, one);
d4c452f0
JH
3060}
3061
29a3eefd
JH
3062int diff_filespec_is_binary(struct diff_filespec *one)
3063{
122aa6f9
JK
3064 if (one->is_binary == -1) {
3065 diff_filespec_load_driver(one);
3066 if (one->driver->binary != -1)
3067 one->is_binary = one->driver->binary;
3068 else {
3069 if (!one->data && DIFF_FILE_VALID(one))
6bf3b813
NTND
3070 diff_populate_filespec(one, CHECK_BINARY);
3071 if (one->is_binary == -1 && one->data)
122aa6f9
JK
3072 one->is_binary = buffer_is_binary(one->data,
3073 one->size);
3074 if (one->is_binary == -1)
3075 one->is_binary = 0;
3076 }
3077 }
29a3eefd 3078 return one->is_binary;
6973dcae
JH
3079}
3080
be58e70d 3081static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one)
f258475a 3082{
122aa6f9
JK
3083 diff_filespec_load_driver(one);
3084 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
f258475a
JH
3085}
3086
a5a818ee
JH
3087void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
3088{
3089 if (!options->a_prefix)
3090 options->a_prefix = a;
3091 if (!options->b_prefix)
3092 options->b_prefix = b;
3093}
3094
a788d7d5 3095struct userdiff_driver *get_textconv(struct diff_filespec *one)
04427ac8
JK
3096{
3097 if (!DIFF_FILE_VALID(one))
3098 return NULL;
d391c0ff 3099
04427ac8 3100 diff_filespec_load_driver(one);
3813e690 3101 return userdiff_get_textconv(one->driver);
04427ac8
JK
3102}
3103
6973dcae
JH
3104static void builtin_diff(const char *name_a,
3105 const char *name_b,
3106 struct diff_filespec *one,
3107 struct diff_filespec *two,
3108 const char *xfrm_msg,
296c6bb2 3109 int must_show_header,
051308f6 3110 struct diff_options *o,
6973dcae
JH
3111 int complete_rewrite)
3112{
3113 mmfile_t mf1, mf2;
3114 const char *lbl[2];
3115 char *a_one, *b_two;
d9c552f1 3116 const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
8f67f8ae 3117 const char *reset = diff_get_color_opt(o, DIFF_RESET);
a5a818ee 3118 const char *a_prefix, *b_prefix;
d9bae1a1
JK
3119 struct userdiff_driver *textconv_one = NULL;
3120 struct userdiff_driver *textconv_two = NULL;
3e97c7c6 3121 struct strbuf header = STRBUF_INIT;
30997bb8 3122 const char *line_prefix = diff_line_prefix(o);
a5a818ee 3123
fd47ae6a
JK
3124 diff_set_mnemonic_prefix(o, "a/", "b/");
3125 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
3126 a_prefix = o->b_prefix;
3127 b_prefix = o->a_prefix;
3128 } else {
3129 a_prefix = o->a_prefix;
3130 b_prefix = o->b_prefix;
3131 }
3132
61cfbc05
JK
3133 if (o->submodule_format == DIFF_SUBMODULE_LOG &&
3134 (!one->mode || S_ISGITLINK(one->mode)) &&
3135 (!two->mode || S_ISGITLINK(two->mode))) {
f3597138 3136 show_submodule_summary(o, one->path ? one->path : two->path,
602a283a 3137 &one->oid, &two->oid,
f3597138 3138 two->dirty_submodule);
752c0c24 3139 return;
fd47ae6a
JK
3140 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3141 (!one->mode || S_ISGITLINK(one->mode)) &&
3142 (!two->mode || S_ISGITLINK(two->mode))) {
f3597138 3143 show_submodule_inline_diff(o, one->path ? one->path : two->path,
fd47ae6a 3144 &one->oid, &two->oid,
f3597138 3145 two->dirty_submodule);
fd47ae6a 3146 return;
752c0c24
JS
3147 }
3148
3aa1f7ca
JK
3149 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
3150 textconv_one = get_textconv(one);
3151 textconv_two = get_textconv(two);
3152 }
3153
71b989e7
LT
3154 /* Never use a non-valid filename anywhere if at all possible */
3155 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
3156 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
3157
a5a818ee
JH
3158 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
3159 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
6973dcae
JH
3160 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
3161 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
d9c552f1 3162 strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset);
6973dcae
JH
3163 if (lbl[0][0] == '/') {
3164 /* /dev/null */
d9c552f1 3165 strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
37466447
BW
3166 if (xfrm_msg)
3167 strbuf_addstr(&header, xfrm_msg);
296c6bb2 3168 must_show_header = 1;
6973dcae
JH
3169 }
3170 else if (lbl[1][0] == '/') {
d9c552f1 3171 strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
37466447
BW
3172 if (xfrm_msg)
3173 strbuf_addstr(&header, xfrm_msg);
296c6bb2 3174 must_show_header = 1;
6973dcae
JH
3175 }
3176 else {
3177 if (one->mode != two->mode) {
d9c552f1
JK
3178 strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
3179 strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
296c6bb2 3180 must_show_header = 1;
cd112cef 3181 }
37466447
BW
3182 if (xfrm_msg)
3183 strbuf_addstr(&header, xfrm_msg);
3e97c7c6 3184
6973dcae
JH
3185 /*
3186 * we do not run diff between different kind
3187 * of objects.
3188 */
3189 if ((one->mode ^ two->mode) & S_IFMT)
3190 goto free_ab_and_return;
0c01857d 3191 if (complete_rewrite &&
3aa1f7ca
JK
3192 (textconv_one || !diff_filespec_is_binary(one)) &&
3193 (textconv_two || !diff_filespec_is_binary(two))) {
a29b0a13
SB
3194 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3195 header.buf, header.len, 0);
3e97c7c6 3196 strbuf_reset(&header);
3aa1f7ca
JK
3197 emit_rewrite_diff(name_a, name_b, one, two,
3198 textconv_one, textconv_two, o);
34a5e1a2 3199 o->found_changes = 1;
6973dcae
JH
3200 goto free_ab_and_return;
3201 }
3202 }
3203
467ddc14 3204 if (o->irreversible_delete && lbl[1][0] == '/') {
a29b0a13
SB
3205 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
3206 header.len, 0);
467ddc14
JH
3207 strbuf_reset(&header);
3208 goto free_ab_and_return;
3209 } else if (!DIFF_OPT_TST(o, TEXT) &&
b3373982
JK
3210 ( (!textconv_one && diff_filespec_is_binary(one)) ||
3211 (!textconv_two && diff_filespec_is_binary(two)) )) {
4acaaa7a 3212 struct strbuf sb = STRBUF_INIT;
1aaf69e6
NTND
3213 if (!one->data && !two->data &&
3214 S_ISREG(one->mode) && S_ISREG(two->mode) &&
3215 !DIFF_OPT_TST(o, BINARY)) {
a0d12c44 3216 if (!oidcmp(&one->oid, &two->oid)) {
1aaf69e6 3217 if (must_show_header)
a29b0a13
SB
3218 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3219 header.buf, header.len,
3220 0);
1aaf69e6
NTND
3221 goto free_ab_and_return;
3222 }
a29b0a13
SB
3223 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3224 header.buf, header.len, 0);
4acaaa7a
SB
3225 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3226 diff_line_prefix(o), lbl[0], lbl[1]);
3227 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3228 sb.buf, sb.len, 0);
3229 strbuf_release(&sb);
1aaf69e6
NTND
3230 goto free_ab_and_return;
3231 }
b3373982
JK
3232 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3233 die("unable to read files to diff");
0660626c
JH
3234 /* Quite common confusing case */
3235 if (mf1.size == mf2.size &&
296c6bb2
CC
3236 !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
3237 if (must_show_header)
a29b0a13
SB
3238 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3239 header.buf, header.len, 0);
0660626c 3240 goto free_ab_and_return;
296c6bb2 3241 }
a29b0a13 3242 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
3e97c7c6 3243 strbuf_reset(&header);
8f67f8ae 3244 if (DIFF_OPT_TST(o, BINARY))
4eed0ebd 3245 emit_binary_diff(o, &mf1, &mf2);
4acaaa7a
SB
3246 else {
3247 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3248 diff_line_prefix(o), lbl[0], lbl[1]);
3249 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3250 sb.buf, sb.len, 0);
3251 strbuf_release(&sb);
3252 }
34a5e1a2 3253 o->found_changes = 1;
467ddc14 3254 } else {
6973dcae
JH
3255 /* Crazy xdl interfaces.. */
3256 const char *diffopts = getenv("GIT_DIFF_OPTS");
ae021d87 3257 const char *v;
6973dcae
JH
3258 xpparam_t xpp;
3259 xdemitconf_t xecfg;
6973dcae 3260 struct emit_callback ecbdata;
be58e70d 3261 const struct userdiff_funcname *pe;
f258475a 3262
b3f01ff2 3263 if (must_show_header) {
a29b0a13
SB
3264 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3265 header.buf, header.len, 0);
3e97c7c6
GB
3266 strbuf_reset(&header);
3267 }
3268
840383b2
JK
3269 mf1.size = fill_textconv(textconv_one, one, &mf1.ptr);
3270 mf2.size = fill_textconv(textconv_two, two, &mf2.ptr);
04427ac8 3271
45e7ca0f
BC
3272 pe = diff_funcname_pattern(one);
3273 if (!pe)
3274 pe = diff_funcname_pattern(two);
6973dcae 3275
9ccd0a88 3276 memset(&xpp, 0, sizeof(xpp));
30b25010 3277 memset(&xecfg, 0, sizeof(xecfg));
cd112cef 3278 memset(&ecbdata, 0, sizeof(ecbdata));
6973dcae 3279 ecbdata.label_path = lbl;
daa0c3d9 3280 ecbdata.color_diff = want_color(o->use_color);
c189c4f2 3281 ecbdata.ws_rule = whitespace_rule(name_b);
690ed843 3282 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
d68fe26f 3283 check_blank_at_eof(&mf1, &mf2, &ecbdata);
a3c158d4 3284 ecbdata.opt = o;
3e97c7c6 3285 ecbdata.header = header.len ? &header : NULL;
582aa00b 3286 xpp.flags = o->xdl_opts;
ee1e5412 3287 xecfg.ctxlen = o->context;
6d0e674a 3288 xecfg.interhunkctxlen = o->interhunkcontext;
6973dcae 3289 xecfg.flags = XDL_EMIT_FUNCNAMES;
14937c2c
RS
3290 if (DIFF_OPT_TST(o, FUNCCONTEXT))
3291 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
45e7ca0f 3292 if (pe)
a013585b 3293 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
6973dcae
JH
3294 if (!diffopts)
3295 ;
ae021d87
JK
3296 else if (skip_prefix(diffopts, "--unified=", &v))
3297 xecfg.ctxlen = strtoul(v, NULL, 10);
3298 else if (skip_prefix(diffopts, "-u", &v))
3299 xecfg.ctxlen = strtoul(v, NULL, 10);
77d1a520
TR
3300 if (o->word_diff)
3301 init_diff_words_data(&ecbdata, o, one, two);
3efb9880
JK
3302 if (xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
3303 &xpp, &xecfg))
3304 die("unable to generate diff for %s", one->path);
882749a0 3305 if (o->word_diff)
f59a59e2 3306 free_diff_words_data(&ecbdata);
04427ac8
JK
3307 if (textconv_one)
3308 free(mf1.ptr);
3309 if (textconv_two)
3310 free(mf2.ptr);
8cfe5f1c 3311 xdiff_clear_find_func(&xecfg);
6973dcae
JH
3312 }
3313
3314 free_ab_and_return:
3e97c7c6 3315 strbuf_release(&header);
fc3abdf5
JH
3316 diff_free_filespec_data(one);
3317 diff_free_filespec_data(two);
6973dcae
JH
3318 free(a_one);
3319 free(b_two);
3320 return;
3321}
3322
3323static void builtin_diffstat(const char *name_a, const char *name_b,
3324 struct diff_filespec *one,
3325 struct diff_filespec *two,
710158e3 3326 struct diffstat_t *diffstat,
0d21efa5 3327 struct diff_options *o,
74faaa16 3328 struct diff_filepair *p)
6973dcae
JH
3329{
3330 mmfile_t mf1, mf2;
3331 struct diffstat_file *data;
352ca4e1 3332 int same_contents;
74faaa16
LT
3333 int complete_rewrite = 0;
3334
3335 if (!DIFF_PAIR_UNMERGED(p)) {
3336 if (p->status == DIFF_STATUS_MODIFIED && p->score)
3337 complete_rewrite = 1;
3338 }
6973dcae
JH
3339
3340 data = diffstat_add(diffstat, name_a, name_b);
99bfd407 3341 data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
6973dcae
JH
3342
3343 if (!one || !two) {
3344 data->is_unmerged = 1;
3345 return;
3346 }
ded0abc7 3347
a0d12c44 3348 same_contents = !oidcmp(&one->oid, &two->oid);
352ca4e1 3349
ded0abc7 3350 if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
ded0abc7 3351 data->is_binary = 1;
352ca4e1 3352 if (same_contents) {
e18872b2
ZJS
3353 data->added = 0;
3354 data->deleted = 0;
3355 } else {
3356 data->added = diff_filespec_size(two);
3357 data->deleted = diff_filespec_size(one);
3358 }
ded0abc7
JK
3359 }
3360
3361 else if (complete_rewrite) {
710158e3
JH
3362 diff_populate_filespec(one, 0);
3363 diff_populate_filespec(two, 0);
3364 data->deleted = count_lines(one->data, one->size);
3365 data->added = count_lines(two->data, two->size);
710158e3 3366 }
6973dcae 3367
352ca4e1 3368 else if (!same_contents) {
6973dcae
JH
3369 /* Crazy xdl interfaces.. */
3370 xpparam_t xpp;
3371 xdemitconf_t xecfg;
6973dcae 3372
ded0abc7
JK
3373 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3374 die("unable to read files to diff");
3375
9ccd0a88 3376 memset(&xpp, 0, sizeof(xpp));
30b25010 3377 memset(&xecfg, 0, sizeof(xecfg));
582aa00b 3378 xpp.flags = o->xdl_opts;
f01cae91
JH
3379 xecfg.ctxlen = o->context;
3380 xecfg.interhunkctxlen = o->interhunkcontext;
3efb9880
JK
3381 if (xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
3382 &xpp, &xecfg))
3383 die("unable to generate diffstat for %s", one->path);
6973dcae 3384 }
fc3abdf5 3385
fc3abdf5
JH
3386 diff_free_filespec_data(one);
3387 diff_free_filespec_data(two);
6973dcae
JH
3388}
3389
88246898 3390static void builtin_checkdiff(const char *name_a, const char *name_b,
cd676a51 3391 const char *attr_path,
5ff10dd6
JH
3392 struct diff_filespec *one,
3393 struct diff_filespec *two,
3394 struct diff_options *o)
88246898
JS
3395{
3396 mmfile_t mf1, mf2;
3397 struct checkdiff_t data;
3398
3399 if (!two)
3400 return;
3401
3402 memset(&data, 0, sizeof(data));
88246898
JS
3403 data.filename = name_b ? name_b : name_a;
3404 data.lineno = 0;
1ba111d1 3405 data.o = o;
cd676a51 3406 data.ws_rule = whitespace_rule(attr_path);
a757c646 3407 data.conflict_marker_size = ll_merge_marker_size(attr_path);
88246898
JS
3408
3409 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3410 die("unable to read files to diff");
3411
5ff10dd6
JH
3412 /*
3413 * All the other codepaths check both sides, but not checking
3414 * the "old" side here is deliberate. We are checking the newly
3415 * introduced changes, and as long as the "new" side is text, we
3416 * can and should check what it introduces.
3417 */
29a3eefd 3418 if (diff_filespec_is_binary(two))
fc3abdf5 3419 goto free_and_return;
88246898
JS
3420 else {
3421 /* Crazy xdl interfaces.. */
3422 xpparam_t xpp;
3423 xdemitconf_t xecfg;
88246898 3424
9ccd0a88 3425 memset(&xpp, 0, sizeof(xpp));
30b25010 3426 memset(&xecfg, 0, sizeof(xecfg));
c35539eb 3427 xecfg.ctxlen = 1; /* at least one context line */
582aa00b 3428 xpp.flags = 0;
3efb9880
JK
3429 if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
3430 &xpp, &xecfg))
3431 die("unable to generate checkdiff for %s", one->path);
877f23cc 3432
467babf8 3433 if (data.ws_rule & WS_BLANK_AT_EOF) {
d68fe26f
JH
3434 struct emit_callback ecbdata;
3435 int blank_at_eof;
3436
3437 ecbdata.ws_rule = data.ws_rule;
3438 check_blank_at_eof(&mf1, &mf2, &ecbdata);
8837d335 3439 blank_at_eof = ecbdata.blank_at_eof_in_postimage;
d68fe26f 3440
467babf8
JH
3441 if (blank_at_eof) {
3442 static char *err;
3443 if (!err)
3444 err = whitespace_error_string(WS_BLANK_AT_EOF);
3445 fprintf(o->file, "%s:%d: %s.\n",
3446 data.filename, blank_at_eof, err);
3447 data.status = 1; /* report errors */
3448 }
877f23cc 3449 }
88246898 3450 }
fc3abdf5
JH
3451 free_and_return:
3452 diff_free_filespec_data(one);
3453 diff_free_filespec_data(two);
62c64895
WC
3454 if (data.status)
3455 DIFF_OPT_SET(o, CHECK_FAILED);
88246898
JS
3456}
3457
6973dcae
JH
3458struct diff_filespec *alloc_filespec(const char *path)
3459{
96ffc06f 3460 struct diff_filespec *spec;
6973dcae 3461
96ffc06f 3462 FLEXPTR_ALLOC_STR(spec, path, path);
9fb88419 3463 spec->count = 1;
122aa6f9 3464 spec->is_binary = -1;
6973dcae
JH
3465 return spec;
3466}
3467
9fb88419
LT
3468void free_filespec(struct diff_filespec *spec)
3469{
3470 if (!--spec->count) {
3471 diff_free_filespec_data(spec);
3472 free(spec);
3473 }
3474}
3475
f9704c2d
BW
3476void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
3477 int oid_valid, unsigned short mode)
6973dcae
JH
3478{
3479 if (mode) {
3480 spec->mode = canon_mode(mode);
f9704c2d
BW
3481 oidcpy(&spec->oid, oid);
3482 spec->oid_valid = oid_valid;
6973dcae
JH
3483 }
3484}
3485
3486/*
5adf317b 3487 * Given a name and sha1 pair, if the index tells us the file in
6973dcae
JH
3488 * the work tree has that object contents, return true, so that
3489 * prepare_temp_file() does not have to inflate and extract.
3490 */
fb4a1c0d 3491static int reuse_worktree_file(const char *name, const struct object_id *oid, int want_file)
6973dcae 3492{
9c5e6c80 3493 const struct cache_entry *ce;
6973dcae
JH
3494 struct stat st;
3495 int pos, len;
3496
150115ad
JH
3497 /*
3498 * We do not read the cache ourselves here, because the
6973dcae
JH
3499 * benchmark with my previous version that always reads cache
3500 * shows that it makes things worse for diff-tree comparing
3501 * two linux-2.6 kernel trees in an already checked out work
3502 * tree. This is because most diff-tree comparisons deal with
3503 * only a small number of files, while reading the cache is
3504 * expensive for a large project, and its cost outweighs the
3505 * savings we get by not inflating the object to a temporary
3506 * file. Practically, this code only helps when we are used
3507 * by diff-cache --cached, which does read the cache before
3508 * calling us.
3509 */
3510 if (!active_cache)
3511 return 0;
3512
1510fea7
SP
3513 /* We want to avoid the working directory if our caller
3514 * doesn't need the data in a normal file, this system
3515 * is rather slow with its stat/open/mmap/close syscalls,
3516 * and the object is contained in a pack file. The pack
3517 * is probably already open and will be faster to obtain
3518 * the data through than the working directory. Loose
3519 * objects however would tend to be slower as they need
3520 * to be individually opened and inflated.
3521 */
fb4a1c0d 3522 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(oid->hash))
1510fea7
SP
3523 return 0;
3524
06dec439
JK
3525 /*
3526 * Similarly, if we'd have to convert the file contents anyway, that
3527 * makes the optimization not worthwhile.
3528 */
82b474e0 3529 if (!want_file && would_convert_to_git(&the_index, name))
06dec439
JK
3530 return 0;
3531
6973dcae
JH
3532 len = strlen(name);
3533 pos = cache_name_pos(name, len);
3534 if (pos < 0)
3535 return 0;
3536 ce = active_cache[pos];
eadb5831
JH
3537
3538 /*
3539 * This is not the sha1 we are looking for, or
3540 * unreusable because it is not a regular file.
3541 */
fb4a1c0d 3542 if (oidcmp(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
6973dcae 3543 return 0;
eadb5831 3544
150115ad
JH
3545 /*
3546 * If ce is marked as "assume unchanged", there is no
3547 * guarantee that work tree matches what we are looking for.
3548 */
b4d1690d 3549 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
150115ad
JH
3550 return 0;
3551
eadb5831
JH
3552 /*
3553 * If ce matches the file in the work tree, we can reuse it.
6973dcae 3554 */
eadb5831
JH
3555 if (ce_uptodate(ce) ||
3556 (!lstat(name, &st) && !ce_match_stat(ce, &st, 0)))
3557 return 1;
3558
3559 return 0;
6973dcae
JH
3560}
3561
04786756
LT
3562static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
3563{
b1ddfb91
JK
3564 struct strbuf buf = STRBUF_INIT;
3565 char *dirty = "";
8e08b419
JH
3566
3567 /* Are we looking at the work tree? */
85adbf2f 3568 if (s->dirty_submodule)
8e08b419
JH
3569 dirty = "-dirty";
3570
a0d12c44 3571 strbuf_addf(&buf, "Subproject commit %s%s\n",
3572 oid_to_hex(&s->oid), dirty);
b1ddfb91 3573 s->size = buf.len;
04786756
LT
3574 if (size_only) {
3575 s->data = NULL;
b1ddfb91
JK
3576 strbuf_release(&buf);
3577 } else {
3578 s->data = strbuf_detach(&buf, NULL);
3579 s->should_free = 1;
04786756
LT
3580 }
3581 return 0;
3582}
3583
6973dcae
JH
3584/*
3585 * While doing rename detection and pickaxe operation, we may need to
3586 * grab the data for the blob (or file) for our own in-core comparison.
3587 * diff_filespec has data and size fields for this purpose.
3588 */
8e5dd3d6 3589int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
6973dcae 3590{
8e5dd3d6 3591 int size_only = flags & CHECK_SIZE_ONLY;
6973dcae 3592 int err = 0;
5430bb28
JH
3593 /*
3594 * demote FAIL to WARN to allow inspecting the situation
3595 * instead of refusing.
3596 */
3597 enum safe_crlf crlf_warn = (safe_crlf == SAFE_CRLF_FAIL
3598 ? SAFE_CRLF_WARN
3599 : safe_crlf);
3600
6973dcae
JH
3601 if (!DIFF_FILE_VALID(s))
3602 die("internal error: asking to populate invalid file.");
3603 if (S_ISDIR(s->mode))
3604 return -1;
3605
6973dcae 3606 if (s->data)
fc3abdf5 3607 return 0;
04786756 3608
6e0b8ed6
JH
3609 if (size_only && 0 < s->size)
3610 return 0;
3611
302b9282 3612 if (S_ISGITLINK(s->mode))
04786756
LT
3613 return diff_populate_gitlink(s, size_only);
3614
41c9560e