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