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