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