diff.c: add white space mode to move detection that allows indent changes
[git.git] / diff.c
blob7810a4733f8f230af54d1d8b8d96484ebea3a2bc
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"
26 #ifdef NO_FAST_WORKING_DIRECTORY
27 #define FAST_WORKING_DIRECTORY 0
28 #else
29 #define FAST_WORKING_DIRECTORY 1
30 #endif
32 static int diff_detect_rename_default;
33 static int diff_indent_heuristic = 1;
34 static int diff_rename_limit_default = 400;
35 static int diff_suppress_blank_empty;
36 static int diff_use_color_default = -1;
37 static int diff_color_moved_default;
38 static int diff_context_default = 3;
39 static int diff_interhunk_context_default;
40 static const char *diff_word_regex_cfg;
41 static const char *external_diff_cmd_cfg;
42 static const char *diff_order_file_cfg;
43 int diff_auto_refresh_index = 1;
44 static int diff_mnemonic_prefix;
45 static int diff_no_prefix;
46 static int diff_stat_graph_width;
47 static int diff_dirstat_permille_default = 30;
48 static struct diff_options default_diff_options;
49 static long diff_algorithm;
50 static unsigned ws_error_highlight_default = WSEH_NEW;
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 */
72 static NORETURN void die_want_option(const char *option_name)
74 die(_("option '%s' requires a value"), option_name);
77 static int parse_diff_color_slot(const char *var)
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;
114 static int parse_dirstat_params(struct diff_options *options, const char *params_string,
115 struct strbuf *errmsg)
117 char *params_copy = xstrdup(params_string);
118 struct string_list params = STRING_LIST_INIT_NODUP;
119 int ret = 0;
120 int i;
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 options->flags.dirstat_by_line = 0;
128 options->flags.dirstat_by_file = 0;
129 } else if (!strcmp(p, "lines")) {
130 options->flags.dirstat_by_line = 1;
131 options->flags.dirstat_by_file = 0;
132 } else if (!strcmp(p, "files")) {
133 options->flags.dirstat_by_line = 0;
134 options->flags.dirstat_by_file = 1;
135 } else if (!strcmp(p, "noncumulative")) {
136 options->flags.dirstat_cumulative = 0;
137 } else if (!strcmp(p, "cumulative")) {
138 options->flags.dirstat_cumulative = 1;
139 } 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 */
149 if (!*end)
150 options->dirstat_permille = permille;
151 else {
152 strbuf_addf(errmsg, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
154 ret++;
156 } else {
157 strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
158 ret++;
162 string_list_clear(&params, 0);
163 free(params_copy);
164 return ret;
167 static int parse_submodule_params(struct diff_options *options, const char *value)
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;
180 static int git_config_rename(const char *var, const char *value)
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;
189 long parse_algorithm_value(const char *value)
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;
204 static int parse_one_token(const char **arg, const char *token)
206 const char *rest;
207 if (skip_prefix(*arg, token, &rest) && (!*rest || *rest == ',')) {
208 *arg = rest;
209 return 1;
211 return 0;
214 static int parse_ws_error_highlight(const char *arg)
216 const char *orig_arg = arg;
217 unsigned val = 0;
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);
235 if (*arg)
236 arg++;
238 return val;
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.
247 void init_diff_ui_defaults(void)
249 diff_detect_rename_default = DIFF_DETECT_RENAME;
252 int git_diff_heuristic_config(const char *var, const char *value, void *cb)
254 if (!strcmp(var, "diff.indentheuristic"))
255 diff_indent_heuristic = git_config_bool(var, value);
256 return 0;
259 static int parse_color_moved(const char *arg)
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;
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, "blocks"))
275 return COLOR_MOVED_BLOCKS;
276 else if (!strcmp(arg, "zebra"))
277 return COLOR_MOVED_ZEBRA;
278 else if (!strcmp(arg, "default"))
279 return COLOR_MOVED_DEFAULT;
280 else if (!strcmp(arg, "dimmed_zebra"))
281 return COLOR_MOVED_ZEBRA_DIM;
282 else
283 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed_zebra', 'plain'"));
286 static int parse_color_moved_ws(const char *arg)
288 int ret = 0;
289 struct string_list l = STRING_LIST_INIT_DUP;
290 struct string_list_item *i;
292 string_list_split(&l, arg, ',', -1);
294 for_each_string_list_item(i, &l) {
295 struct strbuf sb = STRBUF_INIT;
296 strbuf_addstr(&sb, i->string);
297 strbuf_trim(&sb);
299 if (!strcmp(sb.buf, "ignore-space-change"))
300 ret |= XDF_IGNORE_WHITESPACE_CHANGE;
301 else if (!strcmp(sb.buf, "ignore-space-at-eol"))
302 ret |= XDF_IGNORE_WHITESPACE_AT_EOL;
303 else if (!strcmp(sb.buf, "ignore-all-space"))
304 ret |= XDF_IGNORE_WHITESPACE;
305 else if (!strcmp(sb.buf, "allow-indentation-change"))
306 ret |= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE;
307 else
308 error(_("ignoring unknown color-moved-ws mode '%s'"), sb.buf);
310 strbuf_release(&sb);
313 if ((ret & COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) &&
314 (ret & XDF_WHITESPACE_FLAGS))
315 die(_("color-moved-ws: allow-indentation-change cannot be combined with other white space modes"));
317 string_list_clear(&l, 0);
319 return ret;
322 int git_diff_ui_config(const char *var, const char *value, void *cb)
324 if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
325 diff_use_color_default = git_config_colorbool(var, value);
326 return 0;
328 if (!strcmp(var, "diff.colormoved")) {
329 int cm = parse_color_moved(value);
330 if (cm < 0)
331 return -1;
332 diff_color_moved_default = cm;
333 return 0;
335 if (!strcmp(var, "diff.context")) {
336 diff_context_default = git_config_int(var, value);
337 if (diff_context_default < 0)
338 return -1;
339 return 0;
341 if (!strcmp(var, "diff.interhunkcontext")) {
342 diff_interhunk_context_default = git_config_int(var, value);
343 if (diff_interhunk_context_default < 0)
344 return -1;
345 return 0;
347 if (!strcmp(var, "diff.renames")) {
348 diff_detect_rename_default = git_config_rename(var, value);
349 return 0;
351 if (!strcmp(var, "diff.autorefreshindex")) {
352 diff_auto_refresh_index = git_config_bool(var, value);
353 return 0;
355 if (!strcmp(var, "diff.mnemonicprefix")) {
356 diff_mnemonic_prefix = git_config_bool(var, value);
357 return 0;
359 if (!strcmp(var, "diff.noprefix")) {
360 diff_no_prefix = git_config_bool(var, value);
361 return 0;
363 if (!strcmp(var, "diff.statgraphwidth")) {
364 diff_stat_graph_width = git_config_int(var, value);
365 return 0;
367 if (!strcmp(var, "diff.external"))
368 return git_config_string(&external_diff_cmd_cfg, var, value);
369 if (!strcmp(var, "diff.wordregex"))
370 return git_config_string(&diff_word_regex_cfg, var, value);
371 if (!strcmp(var, "diff.orderfile"))
372 return git_config_pathname(&diff_order_file_cfg, var, value);
374 if (!strcmp(var, "diff.ignoresubmodules"))
375 handle_ignore_submodules_arg(&default_diff_options, value);
377 if (!strcmp(var, "diff.submodule")) {
378 if (parse_submodule_params(&default_diff_options, value))
379 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
380 value);
381 return 0;
384 if (!strcmp(var, "diff.algorithm")) {
385 diff_algorithm = parse_algorithm_value(value);
386 if (diff_algorithm < 0)
387 return -1;
388 return 0;
391 if (!strcmp(var, "diff.wserrorhighlight")) {
392 int val = parse_ws_error_highlight(value);
393 if (val < 0)
394 return -1;
395 ws_error_highlight_default = val;
396 return 0;
399 if (git_color_config(var, value, cb) < 0)
400 return -1;
402 return git_diff_basic_config(var, value, cb);
405 int git_diff_basic_config(const char *var, const char *value, void *cb)
407 const char *name;
409 if (!strcmp(var, "diff.renamelimit")) {
410 diff_rename_limit_default = git_config_int(var, value);
411 return 0;
414 if (userdiff_config(var, value) < 0)
415 return -1;
417 if (skip_prefix(var, "diff.color.", &name) ||
418 skip_prefix(var, "color.diff.", &name)) {
419 int slot = parse_diff_color_slot(name);
420 if (slot < 0)
421 return 0;
422 if (!value)
423 return config_error_nonbool(var);
424 return color_parse(value, diff_colors[slot]);
427 /* like GNU diff's --suppress-blank-empty option */
428 if (!strcmp(var, "diff.suppressblankempty") ||
429 /* for backwards compatibility */
430 !strcmp(var, "diff.suppress-blank-empty")) {
431 diff_suppress_blank_empty = git_config_bool(var, value);
432 return 0;
435 if (!strcmp(var, "diff.dirstat")) {
436 struct strbuf errmsg = STRBUF_INIT;
437 default_diff_options.dirstat_permille = diff_dirstat_permille_default;
438 if (parse_dirstat_params(&default_diff_options, value, &errmsg))
439 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
440 errmsg.buf);
441 strbuf_release(&errmsg);
442 diff_dirstat_permille_default = default_diff_options.dirstat_permille;
443 return 0;
446 if (git_diff_heuristic_config(var, value, cb) < 0)
447 return -1;
449 return git_default_config(var, value, cb);
452 static char *quote_two(const char *one, const char *two)
454 int need_one = quote_c_style(one, NULL, NULL, 1);
455 int need_two = quote_c_style(two, NULL, NULL, 1);
456 struct strbuf res = STRBUF_INIT;
458 if (need_one + need_two) {
459 strbuf_addch(&res, '"');
460 quote_c_style(one, &res, NULL, 1);
461 quote_c_style(two, &res, NULL, 1);
462 strbuf_addch(&res, '"');
463 } else {
464 strbuf_addstr(&res, one);
465 strbuf_addstr(&res, two);
467 return strbuf_detach(&res, NULL);
470 static const char *external_diff(void)
472 static const char *external_diff_cmd = NULL;
473 static int done_preparing = 0;
475 if (done_preparing)
476 return external_diff_cmd;
477 external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
478 if (!external_diff_cmd)
479 external_diff_cmd = external_diff_cmd_cfg;
480 done_preparing = 1;
481 return external_diff_cmd;
485 * Keep track of files used for diffing. Sometimes such an entry
486 * refers to a temporary file, sometimes to an existing file, and
487 * sometimes to "/dev/null".
489 static struct diff_tempfile {
491 * filename external diff should read from, or NULL if this
492 * entry is currently not in use:
494 const char *name;
496 char hex[GIT_MAX_HEXSZ + 1];
497 char mode[10];
500 * If this diff_tempfile instance refers to a temporary file,
501 * this tempfile object is used to manage its lifetime.
503 struct tempfile *tempfile;
504 } diff_temp[2];
506 struct emit_callback {
507 int color_diff;
508 unsigned ws_rule;
509 int blank_at_eof_in_preimage;
510 int blank_at_eof_in_postimage;
511 int lno_in_preimage;
512 int lno_in_postimage;
513 const char **label_path;
514 struct diff_words_data *diff_words;
515 struct diff_options *opt;
516 struct strbuf *header;
519 static int count_lines(const char *data, int size)
521 int count, ch, completely_empty = 1, nl_just_seen = 0;
522 count = 0;
523 while (0 < size--) {
524 ch = *data++;
525 if (ch == '\n') {
526 count++;
527 nl_just_seen = 1;
528 completely_empty = 0;
530 else {
531 nl_just_seen = 0;
532 completely_empty = 0;
535 if (completely_empty)
536 return 0;
537 if (!nl_just_seen)
538 count++; /* no trailing newline */
539 return count;
542 static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
544 if (!DIFF_FILE_VALID(one)) {
545 mf->ptr = (char *)""; /* does not matter */
546 mf->size = 0;
547 return 0;
549 else if (diff_populate_filespec(one, 0))
550 return -1;
552 mf->ptr = one->data;
553 mf->size = one->size;
554 return 0;
557 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
558 static unsigned long diff_filespec_size(struct diff_filespec *one)
560 if (!DIFF_FILE_VALID(one))
561 return 0;
562 diff_populate_filespec(one, CHECK_SIZE_ONLY);
563 return one->size;
566 static int count_trailing_blank(mmfile_t *mf, unsigned ws_rule)
568 char *ptr = mf->ptr;
569 long size = mf->size;
570 int cnt = 0;
572 if (!size)
573 return cnt;
574 ptr += size - 1; /* pointing at the very end */
575 if (*ptr != '\n')
576 ; /* incomplete line */
577 else
578 ptr--; /* skip the last LF */
579 while (mf->ptr < ptr) {
580 char *prev_eol;
581 for (prev_eol = ptr; mf->ptr <= prev_eol; prev_eol--)
582 if (*prev_eol == '\n')
583 break;
584 if (!ws_blank_line(prev_eol + 1, ptr - prev_eol, ws_rule))
585 break;
586 cnt++;
587 ptr = prev_eol - 1;
589 return cnt;
592 static void check_blank_at_eof(mmfile_t *mf1, mmfile_t *mf2,
593 struct emit_callback *ecbdata)
595 int l1, l2, at;
596 unsigned ws_rule = ecbdata->ws_rule;
597 l1 = count_trailing_blank(mf1, ws_rule);
598 l2 = count_trailing_blank(mf2, ws_rule);
599 if (l2 <= l1) {
600 ecbdata->blank_at_eof_in_preimage = 0;
601 ecbdata->blank_at_eof_in_postimage = 0;
602 return;
604 at = count_lines(mf1->ptr, mf1->size);
605 ecbdata->blank_at_eof_in_preimage = (at - l1) + 1;
607 at = count_lines(mf2->ptr, mf2->size);
608 ecbdata->blank_at_eof_in_postimage = (at - l2) + 1;
611 static void emit_line_0(struct diff_options *o, const char *set, const char *reset,
612 int first, const char *line, int len)
614 int has_trailing_newline, has_trailing_carriage_return;
615 int nofirst;
616 FILE *file = o->file;
618 fputs(diff_line_prefix(o), file);
620 if (len == 0) {
621 has_trailing_newline = (first == '\n');
622 has_trailing_carriage_return = (!has_trailing_newline &&
623 (first == '\r'));
624 nofirst = has_trailing_newline || has_trailing_carriage_return;
625 } else {
626 has_trailing_newline = (len > 0 && line[len-1] == '\n');
627 if (has_trailing_newline)
628 len--;
629 has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
630 if (has_trailing_carriage_return)
631 len--;
632 nofirst = 0;
635 if (len || !nofirst) {
636 fputs(set, file);
637 if (!nofirst)
638 fputc(first, file);
639 fwrite(line, len, 1, file);
640 fputs(reset, file);
642 if (has_trailing_carriage_return)
643 fputc('\r', file);
644 if (has_trailing_newline)
645 fputc('\n', file);
648 static void emit_line(struct diff_options *o, const char *set, const char *reset,
649 const char *line, int len)
651 emit_line_0(o, set, reset, line[0], line+1, len-1);
654 enum diff_symbol {
655 DIFF_SYMBOL_BINARY_DIFF_HEADER,
656 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
657 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
658 DIFF_SYMBOL_BINARY_DIFF_BODY,
659 DIFF_SYMBOL_BINARY_DIFF_FOOTER,
660 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
661 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
662 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
663 DIFF_SYMBOL_STATS_LINE,
664 DIFF_SYMBOL_WORD_DIFF,
665 DIFF_SYMBOL_STAT_SEP,
666 DIFF_SYMBOL_SUMMARY,
667 DIFF_SYMBOL_SUBMODULE_ADD,
668 DIFF_SYMBOL_SUBMODULE_DEL,
669 DIFF_SYMBOL_SUBMODULE_UNTRACKED,
670 DIFF_SYMBOL_SUBMODULE_MODIFIED,
671 DIFF_SYMBOL_SUBMODULE_HEADER,
672 DIFF_SYMBOL_SUBMODULE_ERROR,
673 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH,
674 DIFF_SYMBOL_REWRITE_DIFF,
675 DIFF_SYMBOL_BINARY_FILES,
676 DIFF_SYMBOL_HEADER,
677 DIFF_SYMBOL_FILEPAIR_PLUS,
678 DIFF_SYMBOL_FILEPAIR_MINUS,
679 DIFF_SYMBOL_WORDS_PORCELAIN,
680 DIFF_SYMBOL_WORDS,
681 DIFF_SYMBOL_CONTEXT,
682 DIFF_SYMBOL_CONTEXT_INCOMPLETE,
683 DIFF_SYMBOL_PLUS,
684 DIFF_SYMBOL_MINUS,
685 DIFF_SYMBOL_NO_LF_EOF,
686 DIFF_SYMBOL_CONTEXT_FRAGINFO,
687 DIFF_SYMBOL_CONTEXT_MARKER,
688 DIFF_SYMBOL_SEPARATOR
691 * Flags for content lines:
692 * 0..12 are whitespace rules
693 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
694 * 16 is marking if the line is blank at EOF
696 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
697 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
698 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
699 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
700 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
703 * This struct is used when we need to buffer the output of the diff output.
705 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
706 * into the pre/post image file. This pointer could be a union with the
707 * line pointer. By storing an offset into the file instead of the literal line,
708 * we can decrease the memory footprint for the buffered output. At first we
709 * may want to only have indirection for the content lines, but we could also
710 * enhance the state for emitting prefabricated lines, e.g. the similarity
711 * score line or hunk/file headers would only need to store a number or path
712 * and then the output can be constructed later on depending on state.
714 struct emitted_diff_symbol {
715 const char *line;
716 int len;
717 int flags;
718 enum diff_symbol s;
720 #define EMITTED_DIFF_SYMBOL_INIT {NULL}
722 struct emitted_diff_symbols {
723 struct emitted_diff_symbol *buf;
724 int nr, alloc;
726 #define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
728 static void append_emitted_diff_symbol(struct diff_options *o,
729 struct emitted_diff_symbol *e)
731 struct emitted_diff_symbol *f;
733 ALLOC_GROW(o->emitted_symbols->buf,
734 o->emitted_symbols->nr + 1,
735 o->emitted_symbols->alloc);
736 f = &o->emitted_symbols->buf[o->emitted_symbols->nr++];
738 memcpy(f, e, sizeof(struct emitted_diff_symbol));
739 f->line = e->line ? xmemdupz(e->line, e->len) : NULL;
742 struct moved_entry {
743 struct hashmap_entry ent;
744 const struct emitted_diff_symbol *es;
745 struct moved_entry *next_line;
746 struct ws_delta *wsd;
750 * The struct ws_delta holds white space differences between moved lines, i.e.
751 * between '+' and '-' lines that have been detected to be a move.
752 * The string contains the difference in leading white spaces, before the
753 * rest of the line is compared using the white space config for move
754 * coloring. The current_longer indicates if the first string in the
755 * comparision is longer than the second.
757 struct ws_delta {
758 char *string;
759 unsigned int current_longer : 1;
761 #define WS_DELTA_INIT { NULL, 0 }
763 static int compute_ws_delta(const struct emitted_diff_symbol *a,
764 const struct emitted_diff_symbol *b,
765 struct ws_delta *out)
767 const struct emitted_diff_symbol *longer = a->len > b->len ? a : b;
768 const struct emitted_diff_symbol *shorter = a->len > b->len ? b : a;
769 int d = longer->len - shorter->len;
771 out->string = xmemdupz(longer->line, d);
772 out->current_longer = (a == longer);
774 return !strncmp(longer->line + d, shorter->line, shorter->len);
777 static int cmp_in_block_with_wsd(const struct diff_options *o,
778 const struct moved_entry *cur,
779 const struct moved_entry *match,
780 struct moved_entry *pmb,
781 int n)
783 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
784 int al = cur->es->len, cl = l->len;
785 const char *a = cur->es->line,
786 *b = match->es->line,
787 *c = l->line;
789 int wslen;
792 * We need to check if 'cur' is equal to 'match'.
793 * As those are from the same (+/-) side, we do not need to adjust for
794 * indent changes. However these were found using fuzzy matching
795 * so we do have to check if they are equal.
797 if (strcmp(a, b))
798 return 1;
800 if (!pmb->wsd)
802 * No white space delta was carried forward? This can happen
803 * when we exit early in this function and do not carry
804 * forward ws.
806 return 1;
809 * The indent changes of the block are known and carried forward in
810 * pmb->wsd; however we need to check if the indent changes of the
811 * current line are still the same as before.
813 * To do so we need to compare 'l' to 'cur', adjusting the
814 * one of them for the white spaces, depending which was longer.
817 wslen = strlen(pmb->wsd->string);
818 if (pmb->wsd->current_longer) {
819 c += wslen;
820 cl -= wslen;
821 } else {
822 a += wslen;
823 al -= wslen;
826 if (strcmp(a, c))
827 return 1;
829 return 0;
832 static int moved_entry_cmp(const void *hashmap_cmp_fn_data,
833 const void *entry,
834 const void *entry_or_key,
835 const void *keydata)
837 const struct diff_options *diffopt = hashmap_cmp_fn_data;
838 const struct moved_entry *a = entry;
839 const struct moved_entry *b = entry_or_key;
840 unsigned flags = diffopt->color_moved_ws_handling
841 & XDF_WHITESPACE_FLAGS;
843 if (diffopt->color_moved_ws_handling &
844 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
846 * As there is not specific white space config given,
847 * we'd need to check for a new block, so ignore all
848 * white space. The setup of the white space
849 * configuration for the next block is done else where
851 flags |= XDF_IGNORE_WHITESPACE;
853 return !xdiff_compare_lines(a->es->line, a->es->len,
854 b->es->line, b->es->len,
855 flags);
858 static struct moved_entry *prepare_entry(struct diff_options *o,
859 int line_no)
861 struct moved_entry *ret = xmalloc(sizeof(*ret));
862 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
863 unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
865 ret->ent.hash = xdiff_hash_string(l->line, l->len, flags);
866 ret->es = l;
867 ret->next_line = NULL;
868 ret->wsd = NULL;
870 return ret;
873 static void add_lines_to_move_detection(struct diff_options *o,
874 struct hashmap *add_lines,
875 struct hashmap *del_lines)
877 struct moved_entry *prev_line = NULL;
879 int n;
880 for (n = 0; n < o->emitted_symbols->nr; n++) {
881 struct hashmap *hm;
882 struct moved_entry *key;
884 switch (o->emitted_symbols->buf[n].s) {
885 case DIFF_SYMBOL_PLUS:
886 hm = add_lines;
887 break;
888 case DIFF_SYMBOL_MINUS:
889 hm = del_lines;
890 break;
891 default:
892 prev_line = NULL;
893 continue;
896 key = prepare_entry(o, n);
897 if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
898 prev_line->next_line = key;
900 hashmap_add(hm, key);
901 prev_line = key;
905 static void pmb_advance_or_null(struct diff_options *o,
906 struct moved_entry *match,
907 struct hashmap *hm,
908 struct moved_entry **pmb,
909 int pmb_nr)
911 int i;
912 for (i = 0; i < pmb_nr; i++) {
913 struct moved_entry *prev = pmb[i];
914 struct moved_entry *cur = (prev && prev->next_line) ?
915 prev->next_line : NULL;
916 if (cur && !hm->cmpfn(o, cur, match, NULL)) {
917 pmb[i] = cur;
918 } else {
919 pmb[i] = NULL;
924 static void pmb_advance_or_null_multi_match(struct diff_options *o,
925 struct moved_entry *match,
926 struct hashmap *hm,
927 struct moved_entry **pmb,
928 int pmb_nr, int n)
930 int i;
931 char *got_match = xcalloc(1, pmb_nr);
933 for (; match; match = hashmap_get_next(hm, match)) {
934 for (i = 0; i < pmb_nr; i++) {
935 struct moved_entry *prev = pmb[i];
936 struct moved_entry *cur = (prev && prev->next_line) ?
937 prev->next_line : NULL;
938 if (!cur)
939 continue;
940 if (!cmp_in_block_with_wsd(o, cur, match, pmb[i], n))
941 got_match[i] |= 1;
945 for (i = 0; i < pmb_nr; i++) {
946 if (got_match[i]) {
947 /* Carry the white space delta forward */
948 pmb[i]->next_line->wsd = pmb[i]->wsd;
949 pmb[i] = pmb[i]->next_line;
950 } else
951 pmb[i] = NULL;
955 static int shrink_potential_moved_blocks(struct moved_entry **pmb,
956 int pmb_nr)
958 int lp, rp;
960 /* Shrink the set of potential block to the remaining running */
961 for (lp = 0, rp = pmb_nr - 1; lp <= rp;) {
962 while (lp < pmb_nr && pmb[lp])
963 lp++;
964 /* lp points at the first NULL now */
966 while (rp > -1 && !pmb[rp])
967 rp--;
968 /* rp points at the last non-NULL */
970 if (lp < pmb_nr && rp > -1 && lp < rp) {
971 pmb[lp] = pmb[rp];
972 if (pmb[rp]->wsd) {
973 free(pmb[rp]->wsd->string);
974 FREE_AND_NULL(pmb[rp]->wsd);
976 pmb[rp] = NULL;
977 rp--;
978 lp++;
982 /* Remember the number of running sets */
983 return rp + 1;
987 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
989 * Otherwise, if the last block has fewer alphanumeric characters than
990 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
991 * that block.
993 * The last block consists of the (n - block_length)'th line up to but not
994 * including the nth line.
996 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
997 * Think of a way to unify them.
999 static void adjust_last_block(struct diff_options *o, int n, int block_length)
1001 int i, alnum_count = 0;
1002 if (o->color_moved == COLOR_MOVED_PLAIN)
1003 return;
1004 for (i = 1; i < block_length + 1; i++) {
1005 const char *c = o->emitted_symbols->buf[n - i].line;
1006 for (; *c; c++) {
1007 if (!isalnum(*c))
1008 continue;
1009 alnum_count++;
1010 if (alnum_count >= COLOR_MOVED_MIN_ALNUM_COUNT)
1011 return;
1014 for (i = 1; i < block_length + 1; i++)
1015 o->emitted_symbols->buf[n - i].flags &= ~DIFF_SYMBOL_MOVED_LINE;
1018 /* Find blocks of moved code, delegate actual coloring decision to helper */
1019 static void mark_color_as_moved(struct diff_options *o,
1020 struct hashmap *add_lines,
1021 struct hashmap *del_lines)
1023 struct moved_entry **pmb = NULL; /* potentially moved blocks */
1024 int pmb_nr = 0, pmb_alloc = 0;
1025 int n, flipped_block = 1, block_length = 0;
1028 for (n = 0; n < o->emitted_symbols->nr; n++) {
1029 struct hashmap *hm = NULL;
1030 struct moved_entry *key;
1031 struct moved_entry *match = NULL;
1032 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1034 switch (l->s) {
1035 case DIFF_SYMBOL_PLUS:
1036 hm = del_lines;
1037 key = prepare_entry(o, n);
1038 match = hashmap_get(hm, key, NULL);
1039 free(key);
1040 break;
1041 case DIFF_SYMBOL_MINUS:
1042 hm = add_lines;
1043 key = prepare_entry(o, n);
1044 match = hashmap_get(hm, key, NULL);
1045 free(key);
1046 break;
1047 default:
1048 flipped_block = 1;
1051 if (!match) {
1052 adjust_last_block(o, n, block_length);
1053 pmb_nr = 0;
1054 block_length = 0;
1055 continue;
1058 l->flags |= DIFF_SYMBOL_MOVED_LINE;
1060 if (o->color_moved == COLOR_MOVED_PLAIN)
1061 continue;
1063 if (o->color_moved_ws_handling &
1064 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
1065 pmb_advance_or_null_multi_match(o, match, hm, pmb, pmb_nr, n);
1066 else
1067 pmb_advance_or_null(o, match, hm, pmb, pmb_nr);
1069 pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
1071 if (pmb_nr == 0) {
1073 * The current line is the start of a new block.
1074 * Setup the set of potential blocks.
1076 for (; match; match = hashmap_get_next(hm, match)) {
1077 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
1078 if (o->color_moved_ws_handling &
1079 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) {
1080 struct ws_delta *wsd = xmalloc(sizeof(*match->wsd));
1081 if (compute_ws_delta(l, match->es, wsd)) {
1082 match->wsd = wsd;
1083 pmb[pmb_nr++] = match;
1084 } else
1085 free(wsd);
1086 } else {
1087 pmb[pmb_nr++] = match;
1091 flipped_block = (flipped_block + 1) % 2;
1093 adjust_last_block(o, n, block_length);
1094 block_length = 0;
1097 block_length++;
1099 if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS)
1100 l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
1102 adjust_last_block(o, n, block_length);
1104 free(pmb);
1107 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1108 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1109 static void dim_moved_lines(struct diff_options *o)
1111 int n;
1112 for (n = 0; n < o->emitted_symbols->nr; n++) {
1113 struct emitted_diff_symbol *prev = (n != 0) ?
1114 &o->emitted_symbols->buf[n - 1] : NULL;
1115 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1116 struct emitted_diff_symbol *next =
1117 (n < o->emitted_symbols->nr - 1) ?
1118 &o->emitted_symbols->buf[n + 1] : NULL;
1120 /* Not a plus or minus line? */
1121 if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
1122 continue;
1124 /* Not a moved line? */
1125 if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
1126 continue;
1129 * If prev or next are not a plus or minus line,
1130 * pretend they don't exist
1132 if (prev && prev->s != DIFF_SYMBOL_PLUS &&
1133 prev->s != DIFF_SYMBOL_MINUS)
1134 prev = NULL;
1135 if (next && next->s != DIFF_SYMBOL_PLUS &&
1136 next->s != DIFF_SYMBOL_MINUS)
1137 next = NULL;
1139 /* Inside a block? */
1140 if ((prev &&
1141 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1142 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
1143 (next &&
1144 (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
1145 (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
1146 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1147 continue;
1150 /* Check if we are at an interesting bound: */
1151 if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
1152 (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1153 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1154 continue;
1155 if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
1156 (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1157 (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1158 continue;
1161 * The boundary to prev and next are not interesting,
1162 * so this line is not interesting as a whole
1164 l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1168 static void emit_line_ws_markup(struct diff_options *o,
1169 const char *set, const char *reset,
1170 const char *line, int len, char sign,
1171 unsigned ws_rule, int blank_at_eof)
1173 const char *ws = NULL;
1175 if (o->ws_error_highlight & ws_rule) {
1176 ws = diff_get_color_opt(o, DIFF_WHITESPACE);
1177 if (!*ws)
1178 ws = NULL;
1181 if (!ws)
1182 emit_line_0(o, set, reset, sign, line, len);
1183 else if (blank_at_eof)
1184 /* Blank line at EOF - paint '+' as well */
1185 emit_line_0(o, ws, reset, sign, line, len);
1186 else {
1187 /* Emit just the prefix, then the rest. */
1188 emit_line_0(o, set, reset, sign, "", 0);
1189 ws_check_emit(line, len, ws_rule,
1190 o->file, set, reset, ws);
1194 static void emit_diff_symbol_from_struct(struct diff_options *o,
1195 struct emitted_diff_symbol *eds)
1197 static const char *nneof = " No newline at end of file\n";
1198 const char *context, *reset, *set, *meta, *fraginfo;
1199 struct strbuf sb = STRBUF_INIT;
1201 enum diff_symbol s = eds->s;
1202 const char *line = eds->line;
1203 int len = eds->len;
1204 unsigned flags = eds->flags;
1206 switch (s) {
1207 case DIFF_SYMBOL_NO_LF_EOF:
1208 context = diff_get_color_opt(o, DIFF_CONTEXT);
1209 reset = diff_get_color_opt(o, DIFF_RESET);
1210 putc('\n', o->file);
1211 emit_line_0(o, context, reset, '\\',
1212 nneof, strlen(nneof));
1213 break;
1214 case DIFF_SYMBOL_SUBMODULE_HEADER:
1215 case DIFF_SYMBOL_SUBMODULE_ERROR:
1216 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH:
1217 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES:
1218 case DIFF_SYMBOL_SUMMARY:
1219 case DIFF_SYMBOL_STATS_LINE:
1220 case DIFF_SYMBOL_BINARY_DIFF_BODY:
1221 case DIFF_SYMBOL_CONTEXT_FRAGINFO:
1222 emit_line(o, "", "", line, len);
1223 break;
1224 case DIFF_SYMBOL_CONTEXT_INCOMPLETE:
1225 case DIFF_SYMBOL_CONTEXT_MARKER:
1226 context = diff_get_color_opt(o, DIFF_CONTEXT);
1227 reset = diff_get_color_opt(o, DIFF_RESET);
1228 emit_line(o, context, reset, line, len);
1229 break;
1230 case DIFF_SYMBOL_SEPARATOR:
1231 fprintf(o->file, "%s%c",
1232 diff_line_prefix(o),
1233 o->line_termination);
1234 break;
1235 case DIFF_SYMBOL_CONTEXT:
1236 set = diff_get_color_opt(o, DIFF_CONTEXT);
1237 reset = diff_get_color_opt(o, DIFF_RESET);
1238 emit_line_ws_markup(o, set, reset, line, len, ' ',
1239 flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1240 break;
1241 case DIFF_SYMBOL_PLUS:
1242 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1243 DIFF_SYMBOL_MOVED_LINE_ALT |
1244 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1245 case DIFF_SYMBOL_MOVED_LINE |
1246 DIFF_SYMBOL_MOVED_LINE_ALT |
1247 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1248 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
1249 break;
1250 case DIFF_SYMBOL_MOVED_LINE |
1251 DIFF_SYMBOL_MOVED_LINE_ALT:
1252 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
1253 break;
1254 case DIFF_SYMBOL_MOVED_LINE |
1255 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1256 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1257 break;
1258 case DIFF_SYMBOL_MOVED_LINE:
1259 set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
1260 break;
1261 default:
1262 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1264 reset = diff_get_color_opt(o, DIFF_RESET);
1265 emit_line_ws_markup(o, set, reset, line, len, '+',
1266 flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1267 flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1268 break;
1269 case DIFF_SYMBOL_MINUS:
1270 switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1271 DIFF_SYMBOL_MOVED_LINE_ALT |
1272 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1273 case DIFF_SYMBOL_MOVED_LINE |
1274 DIFF_SYMBOL_MOVED_LINE_ALT |
1275 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1276 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1277 break;
1278 case DIFF_SYMBOL_MOVED_LINE |
1279 DIFF_SYMBOL_MOVED_LINE_ALT:
1280 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
1281 break;
1282 case DIFF_SYMBOL_MOVED_LINE |
1283 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1284 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1285 break;
1286 case DIFF_SYMBOL_MOVED_LINE:
1287 set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
1288 break;
1289 default:
1290 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1292 reset = diff_get_color_opt(o, DIFF_RESET);
1293 emit_line_ws_markup(o, set, reset, line, len, '-',
1294 flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
1295 break;
1296 case DIFF_SYMBOL_WORDS_PORCELAIN:
1297 context = diff_get_color_opt(o, DIFF_CONTEXT);
1298 reset = diff_get_color_opt(o, DIFF_RESET);
1299 emit_line(o, context, reset, line, len);
1300 fputs("~\n", o->file);
1301 break;
1302 case DIFF_SYMBOL_WORDS:
1303 context = diff_get_color_opt(o, DIFF_CONTEXT);
1304 reset = diff_get_color_opt(o, DIFF_RESET);
1306 * Skip the prefix character, if any. With
1307 * diff_suppress_blank_empty, there may be
1308 * none.
1310 if (line[0] != '\n') {
1311 line++;
1312 len--;
1314 emit_line(o, context, reset, line, len);
1315 break;
1316 case DIFF_SYMBOL_FILEPAIR_PLUS:
1317 meta = diff_get_color_opt(o, DIFF_METAINFO);
1318 reset = diff_get_color_opt(o, DIFF_RESET);
1319 fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
1320 line, reset,
1321 strchr(line, ' ') ? "\t" : "");
1322 break;
1323 case DIFF_SYMBOL_FILEPAIR_MINUS:
1324 meta = diff_get_color_opt(o, DIFF_METAINFO);
1325 reset = diff_get_color_opt(o, DIFF_RESET);
1326 fprintf(o->file, "%s%s--- %s%s%s\n", diff_line_prefix(o), meta,
1327 line, reset,
1328 strchr(line, ' ') ? "\t" : "");
1329 break;
1330 case DIFF_SYMBOL_BINARY_FILES:
1331 case DIFF_SYMBOL_HEADER:
1332 fprintf(o->file, "%s", line);
1333 break;
1334 case DIFF_SYMBOL_BINARY_DIFF_HEADER:
1335 fprintf(o->file, "%sGIT binary patch\n", diff_line_prefix(o));
1336 break;
1337 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA:
1338 fprintf(o->file, "%sdelta %s\n", diff_line_prefix(o), line);
1339 break;
1340 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL:
1341 fprintf(o->file, "%sliteral %s\n", diff_line_prefix(o), line);
1342 break;
1343 case DIFF_SYMBOL_BINARY_DIFF_FOOTER:
1344 fputs(diff_line_prefix(o), o->file);
1345 fputc('\n', o->file);
1346 break;
1347 case DIFF_SYMBOL_REWRITE_DIFF:
1348 fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
1349 reset = diff_get_color_opt(o, DIFF_RESET);
1350 emit_line(o, fraginfo, reset, line, len);
1351 break;
1352 case DIFF_SYMBOL_SUBMODULE_ADD:
1353 set = diff_get_color_opt(o, DIFF_FILE_NEW);
1354 reset = diff_get_color_opt(o, DIFF_RESET);
1355 emit_line(o, set, reset, line, len);
1356 break;
1357 case DIFF_SYMBOL_SUBMODULE_DEL:
1358 set = diff_get_color_opt(o, DIFF_FILE_OLD);
1359 reset = diff_get_color_opt(o, DIFF_RESET);
1360 emit_line(o, set, reset, line, len);
1361 break;
1362 case DIFF_SYMBOL_SUBMODULE_UNTRACKED:
1363 fprintf(o->file, "%sSubmodule %s contains untracked content\n",
1364 diff_line_prefix(o), line);
1365 break;
1366 case DIFF_SYMBOL_SUBMODULE_MODIFIED:
1367 fprintf(o->file, "%sSubmodule %s contains modified content\n",
1368 diff_line_prefix(o), line);
1369 break;
1370 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES:
1371 emit_line(o, "", "", " 0 files changed\n",
1372 strlen(" 0 files changed\n"));
1373 break;
1374 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV:
1375 emit_line(o, "", "", " ...\n", strlen(" ...\n"));
1376 break;
1377 case DIFF_SYMBOL_WORD_DIFF:
1378 fprintf(o->file, "%.*s", len, line);
1379 break;
1380 case DIFF_SYMBOL_STAT_SEP:
1381 fputs(o->stat_sep, o->file);
1382 break;
1383 default:
1384 die("BUG: unknown diff symbol");
1386 strbuf_release(&sb);
1389 static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
1390 const char *line, int len, unsigned flags)
1392 struct emitted_diff_symbol e = {line, len, flags, s};
1394 if (o->emitted_symbols)
1395 append_emitted_diff_symbol(o, &e);
1396 else
1397 emit_diff_symbol_from_struct(o, &e);
1400 void diff_emit_submodule_del(struct diff_options *o, const char *line)
1402 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_DEL, line, strlen(line), 0);
1405 void diff_emit_submodule_add(struct diff_options *o, const char *line)
1407 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ADD, line, strlen(line), 0);
1410 void diff_emit_submodule_untracked(struct diff_options *o, const char *path)
1412 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_UNTRACKED,
1413 path, strlen(path), 0);
1416 void diff_emit_submodule_modified(struct diff_options *o, const char *path)
1418 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_MODIFIED,
1419 path, strlen(path), 0);
1422 void diff_emit_submodule_header(struct diff_options *o, const char *header)
1424 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_HEADER,
1425 header, strlen(header), 0);
1428 void diff_emit_submodule_error(struct diff_options *o, const char *err)
1430 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_ERROR, err, strlen(err), 0);
1433 void diff_emit_submodule_pipethrough(struct diff_options *o,
1434 const char *line, int len)
1436 emit_diff_symbol(o, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH, line, len, 0);
1439 static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line, int len)
1441 if (!((ecbdata->ws_rule & WS_BLANK_AT_EOF) &&
1442 ecbdata->blank_at_eof_in_preimage &&
1443 ecbdata->blank_at_eof_in_postimage &&
1444 ecbdata->blank_at_eof_in_preimage <= ecbdata->lno_in_preimage &&
1445 ecbdata->blank_at_eof_in_postimage <= ecbdata->lno_in_postimage))
1446 return 0;
1447 return ws_blank_line(line, len, ecbdata->ws_rule);
1450 static void emit_add_line(const char *reset,
1451 struct emit_callback *ecbdata,
1452 const char *line, int len)
1454 unsigned flags = WSEH_NEW | ecbdata->ws_rule;
1455 if (new_blank_line_at_eof(ecbdata, line, len))
1456 flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
1458 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
1461 static void emit_del_line(const char *reset,
1462 struct emit_callback *ecbdata,
1463 const char *line, int len)
1465 unsigned flags = WSEH_OLD | ecbdata->ws_rule;
1466 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
1469 static void emit_context_line(const char *reset,
1470 struct emit_callback *ecbdata,
1471 const char *line, int len)
1473 unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
1474 emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
1477 static void emit_hunk_header(struct emit_callback *ecbdata,
1478 const char *line, int len)
1480 const char *context = diff_get_color(ecbdata->color_diff, DIFF_CONTEXT);
1481 const char *frag = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
1482 const char *func = diff_get_color(ecbdata->color_diff, DIFF_FUNCINFO);
1483 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
1484 static const char atat[2] = { '@', '@' };
1485 const char *cp, *ep;
1486 struct strbuf msgbuf = STRBUF_INIT;
1487 int org_len = len;
1488 int i = 1;
1491 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1492 * it always is at least 10 bytes long.
1494 if (len < 10 ||
1495 memcmp(line, atat, 2) ||
1496 !(ep = memmem(line + 2, len - 2, atat, 2))) {
1497 emit_diff_symbol(ecbdata->opt,
1498 DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
1499 return;
1501 ep += 2; /* skip over @@ */
1503 /* The hunk header in fraginfo color */
1504 strbuf_addstr(&msgbuf, frag);
1505 strbuf_add(&msgbuf, line, ep - line);
1506 strbuf_addstr(&msgbuf, reset);
1509 * trailing "\r\n"
1511 for ( ; i < 3; i++)
1512 if (line[len - i] == '\r' || line[len - i] == '\n')
1513 len--;
1515 /* blank before the func header */
1516 for (cp = ep; ep - line < len; ep++)
1517 if (*ep != ' ' && *ep != '\t')
1518 break;
1519 if (ep != cp) {
1520 strbuf_addstr(&msgbuf, context);
1521 strbuf_add(&msgbuf, cp, ep - cp);
1522 strbuf_addstr(&msgbuf, reset);
1525 if (ep < line + len) {
1526 strbuf_addstr(&msgbuf, func);
1527 strbuf_add(&msgbuf, ep, line + len - ep);
1528 strbuf_addstr(&msgbuf, reset);
1531 strbuf_add(&msgbuf, line + len, org_len - len);
1532 strbuf_complete_line(&msgbuf);
1533 emit_diff_symbol(ecbdata->opt,
1534 DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
1535 strbuf_release(&msgbuf);
1538 static struct diff_tempfile *claim_diff_tempfile(void) {
1539 int i;
1540 for (i = 0; i < ARRAY_SIZE(diff_temp); i++)
1541 if (!diff_temp[i].name)
1542 return diff_temp + i;
1543 die("BUG: diff is failing to clean up its tempfiles");
1546 static void remove_tempfile(void)
1548 int i;
1549 for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
1550 if (is_tempfile_active(diff_temp[i].tempfile))
1551 delete_tempfile(&diff_temp[i].tempfile);
1552 diff_temp[i].name = NULL;
1556 static void add_line_count(struct strbuf *out, int count)
1558 switch (count) {
1559 case 0:
1560 strbuf_addstr(out, "0,0");
1561 break;
1562 case 1:
1563 strbuf_addstr(out, "1");
1564 break;
1565 default:
1566 strbuf_addf(out, "1,%d", count);
1567 break;
1571 static void emit_rewrite_lines(struct emit_callback *ecb,
1572 int prefix, const char *data, int size)
1574 const char *endp = NULL;
1575 const char *reset = diff_get_color(ecb->color_diff, DIFF_RESET);
1577 while (0 < size) {
1578 int len;
1580 endp = memchr(data, '\n', size);
1581 len = endp ? (endp - data + 1) : size;
1582 if (prefix != '+') {
1583 ecb->lno_in_preimage++;
1584 emit_del_line(reset, ecb, data, len);
1585 } else {
1586 ecb->lno_in_postimage++;
1587 emit_add_line(reset, ecb, data, len);
1589 size -= len;
1590 data += len;
1592 if (!endp)
1593 emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
1596 static void emit_rewrite_diff(const char *name_a,
1597 const char *name_b,
1598 struct diff_filespec *one,
1599 struct diff_filespec *two,
1600 struct userdiff_driver *textconv_one,
1601 struct userdiff_driver *textconv_two,
1602 struct diff_options *o)
1604 int lc_a, lc_b;
1605 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
1606 const char *a_prefix, *b_prefix;
1607 char *data_one, *data_two;
1608 size_t size_one, size_two;
1609 struct emit_callback ecbdata;
1610 struct strbuf out = STRBUF_INIT;
1612 if (diff_mnemonic_prefix && o->flags.reverse_diff) {
1613 a_prefix = o->b_prefix;
1614 b_prefix = o->a_prefix;
1615 } else {
1616 a_prefix = o->a_prefix;
1617 b_prefix = o->b_prefix;
1620 name_a += (*name_a == '/');
1621 name_b += (*name_b == '/');
1623 strbuf_reset(&a_name);
1624 strbuf_reset(&b_name);
1625 quote_two_c_style(&a_name, a_prefix, name_a, 0);
1626 quote_two_c_style(&b_name, b_prefix, name_b, 0);
1628 size_one = fill_textconv(textconv_one, one, &data_one);
1629 size_two = fill_textconv(textconv_two, two, &data_two);
1631 memset(&ecbdata, 0, sizeof(ecbdata));
1632 ecbdata.color_diff = want_color(o->use_color);
1633 ecbdata.ws_rule = whitespace_rule(name_b);
1634 ecbdata.opt = o;
1635 if (ecbdata.ws_rule & WS_BLANK_AT_EOF) {
1636 mmfile_t mf1, mf2;
1637 mf1.ptr = (char *)data_one;
1638 mf2.ptr = (char *)data_two;
1639 mf1.size = size_one;
1640 mf2.size = size_two;
1641 check_blank_at_eof(&mf1, &mf2, &ecbdata);
1643 ecbdata.lno_in_preimage = 1;
1644 ecbdata.lno_in_postimage = 1;
1646 lc_a = count_lines(data_one, size_one);
1647 lc_b = count_lines(data_two, size_two);
1649 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
1650 a_name.buf, a_name.len, 0);
1651 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
1652 b_name.buf, b_name.len, 0);
1654 strbuf_addstr(&out, "@@ -");
1655 if (!o->irreversible_delete)
1656 add_line_count(&out, lc_a);
1657 else
1658 strbuf_addstr(&out, "?,?");
1659 strbuf_addstr(&out, " +");
1660 add_line_count(&out, lc_b);
1661 strbuf_addstr(&out, " @@\n");
1662 emit_diff_symbol(o, DIFF_SYMBOL_REWRITE_DIFF, out.buf, out.len, 0);
1663 strbuf_release(&out);
1665 if (lc_a && !o->irreversible_delete)
1666 emit_rewrite_lines(&ecbdata, '-', data_one, size_one);
1667 if (lc_b)
1668 emit_rewrite_lines(&ecbdata, '+', data_two, size_two);
1669 if (textconv_one)
1670 free((char *)data_one);
1671 if (textconv_two)
1672 free((char *)data_two);
1675 struct diff_words_buffer {
1676 mmfile_t text;
1677 unsigned long alloc;
1678 struct diff_words_orig {
1679 const char *begin, *end;
1680 } *orig;
1681 int orig_nr, orig_alloc;
1684 static void diff_words_append(char *line, unsigned long len,
1685 struct diff_words_buffer *buffer)
1687 ALLOC_GROW(buffer->text.ptr, buffer->text.size + len, buffer->alloc);
1688 line++;
1689 len--;
1690 memcpy(buffer->text.ptr + buffer->text.size, line, len);
1691 buffer->text.size += len;
1692 buffer->text.ptr[buffer->text.size] = '\0';
1695 struct diff_words_style_elem {
1696 const char *prefix;
1697 const char *suffix;
1698 const char *color; /* NULL; filled in by the setup code if
1699 * color is enabled */
1702 struct diff_words_style {
1703 enum diff_words_type type;
1704 struct diff_words_style_elem new_word, old_word, ctx;
1705 const char *newline;
1708 static struct diff_words_style diff_words_styles[] = {
1709 { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1710 { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1711 { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
1714 struct diff_words_data {
1715 struct diff_words_buffer minus, plus;
1716 const char *current_plus;
1717 int last_minus;
1718 struct diff_options *opt;
1719 regex_t *word_regex;
1720 enum diff_words_type type;
1721 struct diff_words_style *style;
1724 static int fn_out_diff_words_write_helper(struct diff_options *o,
1725 struct diff_words_style_elem *st_el,
1726 const char *newline,
1727 size_t count, const char *buf)
1729 int print = 0;
1730 struct strbuf sb = STRBUF_INIT;
1732 while (count) {
1733 char *p = memchr(buf, '\n', count);
1734 if (print)
1735 strbuf_addstr(&sb, diff_line_prefix(o));
1737 if (p != buf) {
1738 const char *reset = st_el->color && *st_el->color ?
1739 GIT_COLOR_RESET : NULL;
1740 if (st_el->color && *st_el->color)
1741 strbuf_addstr(&sb, st_el->color);
1742 strbuf_addstr(&sb, st_el->prefix);
1743 strbuf_add(&sb, buf, p ? p - buf : count);
1744 strbuf_addstr(&sb, st_el->suffix);
1745 if (reset)
1746 strbuf_addstr(&sb, reset);
1748 if (!p)
1749 goto out;
1751 strbuf_addstr(&sb, newline);
1752 count -= p + 1 - buf;
1753 buf = p + 1;
1754 print = 1;
1755 if (count) {
1756 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1757 sb.buf, sb.len, 0);
1758 strbuf_reset(&sb);
1762 out:
1763 if (sb.len)
1764 emit_diff_symbol(o, DIFF_SYMBOL_WORD_DIFF,
1765 sb.buf, sb.len, 0);
1766 strbuf_release(&sb);
1767 return 0;
1771 * '--color-words' algorithm can be described as:
1773 * 1. collect the minus/plus lines of a diff hunk, divided into
1774 * minus-lines and plus-lines;
1776 * 2. break both minus-lines and plus-lines into words and
1777 * place them into two mmfile_t with one word for each line;
1779 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1781 * And for the common parts of the both file, we output the plus side text.
1782 * diff_words->current_plus is used to trace the current position of the plus file
1783 * which printed. diff_words->last_minus is used to trace the last minus word
1784 * printed.
1786 * For '--graph' to work with '--color-words', we need to output the graph prefix
1787 * on each line of color words output. Generally, there are two conditions on
1788 * which we should output the prefix.
1790 * 1. diff_words->last_minus == 0 &&
1791 * diff_words->current_plus == diff_words->plus.text.ptr
1793 * that is: the plus text must start as a new line, and if there is no minus
1794 * word printed, a graph prefix must be printed.
1796 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1797 * *(diff_words->current_plus - 1) == '\n'
1799 * that is: a graph prefix must be printed following a '\n'
1801 static int color_words_output_graph_prefix(struct diff_words_data *diff_words)
1803 if ((diff_words->last_minus == 0 &&
1804 diff_words->current_plus == diff_words->plus.text.ptr) ||
1805 (diff_words->current_plus > diff_words->plus.text.ptr &&
1806 *(diff_words->current_plus - 1) == '\n')) {
1807 return 1;
1808 } else {
1809 return 0;
1813 static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
1815 struct diff_words_data *diff_words = priv;
1816 struct diff_words_style *style = diff_words->style;
1817 int minus_first, minus_len, plus_first, plus_len;
1818 const char *minus_begin, *minus_end, *plus_begin, *plus_end;
1819 struct diff_options *opt = diff_words->opt;
1820 const char *line_prefix;
1822 if (line[0] != '@' || parse_hunk_header(line, len,
1823 &minus_first, &minus_len, &plus_first, &plus_len))
1824 return;
1826 assert(opt);
1827 line_prefix = diff_line_prefix(opt);
1829 /* POSIX requires that first be decremented by one if len == 0... */
1830 if (minus_len) {
1831 minus_begin = diff_words->minus.orig[minus_first].begin;
1832 minus_end =
1833 diff_words->minus.orig[minus_first + minus_len - 1].end;
1834 } else
1835 minus_begin = minus_end =
1836 diff_words->minus.orig[minus_first].end;
1838 if (plus_len) {
1839 plus_begin = diff_words->plus.orig[plus_first].begin;
1840 plus_end = diff_words->plus.orig[plus_first + plus_len - 1].end;
1841 } else
1842 plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
1844 if (color_words_output_graph_prefix(diff_words)) {
1845 fputs(line_prefix, diff_words->opt->file);
1847 if (diff_words->current_plus != plus_begin) {
1848 fn_out_diff_words_write_helper(diff_words->opt,
1849 &style->ctx, style->newline,
1850 plus_begin - diff_words->current_plus,
1851 diff_words->current_plus);
1853 if (minus_begin != minus_end) {
1854 fn_out_diff_words_write_helper(diff_words->opt,
1855 &style->old_word, style->newline,
1856 minus_end - minus_begin, minus_begin);
1858 if (plus_begin != plus_end) {
1859 fn_out_diff_words_write_helper(diff_words->opt,
1860 &style->new_word, style->newline,
1861 plus_end - plus_begin, plus_begin);
1864 diff_words->current_plus = plus_end;
1865 diff_words->last_minus = minus_first;
1868 /* This function starts looking at *begin, and returns 0 iff a word was found. */
1869 static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
1870 int *begin, int *end)
1872 if (word_regex && *begin < buffer->size) {
1873 regmatch_t match[1];
1874 if (!regexec_buf(word_regex, buffer->ptr + *begin,
1875 buffer->size - *begin, 1, match, 0)) {
1876 char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
1877 '\n', match[0].rm_eo - match[0].rm_so);
1878 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
1879 *begin += match[0].rm_so;
1880 return *begin >= *end;
1882 return -1;
1885 /* find the next word */
1886 while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
1887 (*begin)++;
1888 if (*begin >= buffer->size)
1889 return -1;
1891 /* find the end of the word */
1892 *end = *begin + 1;
1893 while (*end < buffer->size && !isspace(buffer->ptr[*end]))
1894 (*end)++;
1896 return 0;
1900 * This function splits the words in buffer->text, stores the list with
1901 * newline separator into out, and saves the offsets of the original words
1902 * in buffer->orig.
1904 static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
1905 regex_t *word_regex)
1907 int i, j;
1908 long alloc = 0;
1910 out->size = 0;
1911 out->ptr = NULL;
1913 /* fake an empty "0th" word */
1914 ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
1915 buffer->orig[0].begin = buffer->orig[0].end = buffer->text.ptr;
1916 buffer->orig_nr = 1;
1918 for (i = 0; i < buffer->text.size; i++) {
1919 if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
1920 return;
1922 /* store original boundaries */
1923 ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
1924 buffer->orig_alloc);
1925 buffer->orig[buffer->orig_nr].begin = buffer->text.ptr + i;
1926 buffer->orig[buffer->orig_nr].end = buffer->text.ptr + j;
1927 buffer->orig_nr++;
1929 /* store one word */
1930 ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
1931 memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
1932 out->ptr[out->size + j - i] = '\n';
1933 out->size += j - i + 1;
1935 i = j - 1;
1939 /* this executes the word diff on the accumulated buffers */
1940 static void diff_words_show(struct diff_words_data *diff_words)
1942 xpparam_t xpp;
1943 xdemitconf_t xecfg;
1944 mmfile_t minus, plus;
1945 struct diff_words_style *style = diff_words->style;
1947 struct diff_options *opt = diff_words->opt;
1948 const char *line_prefix;
1950 assert(opt);
1951 line_prefix = diff_line_prefix(opt);
1953 /* special case: only removal */
1954 if (!diff_words->plus.text.size) {
1955 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
1956 line_prefix, strlen(line_prefix), 0);
1957 fn_out_diff_words_write_helper(diff_words->opt,
1958 &style->old_word, style->newline,
1959 diff_words->minus.text.size,
1960 diff_words->minus.text.ptr);
1961 diff_words->minus.text.size = 0;
1962 return;
1965 diff_words->current_plus = diff_words->plus.text.ptr;
1966 diff_words->last_minus = 0;
1968 memset(&xpp, 0, sizeof(xpp));
1969 memset(&xecfg, 0, sizeof(xecfg));
1970 diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
1971 diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
1972 xpp.flags = 0;
1973 /* as only the hunk header will be parsed, we need a 0-context */
1974 xecfg.ctxlen = 0;
1975 if (xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
1976 &xpp, &xecfg))
1977 die("unable to generate word diff");
1978 free(minus.ptr);
1979 free(plus.ptr);
1980 if (diff_words->current_plus != diff_words->plus.text.ptr +
1981 diff_words->plus.text.size) {
1982 if (color_words_output_graph_prefix(diff_words))
1983 emit_diff_symbol(diff_words->opt, DIFF_SYMBOL_WORD_DIFF,
1984 line_prefix, strlen(line_prefix), 0);
1985 fn_out_diff_words_write_helper(diff_words->opt,
1986 &style->ctx, style->newline,
1987 diff_words->plus.text.ptr + diff_words->plus.text.size
1988 - diff_words->current_plus, diff_words->current_plus);
1990 diff_words->minus.text.size = diff_words->plus.text.size = 0;
1993 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
1994 static void diff_words_flush(struct emit_callback *ecbdata)
1996 struct diff_options *wo = ecbdata->diff_words->opt;
1998 if (ecbdata->diff_words->minus.text.size ||
1999 ecbdata->diff_words->plus.text.size)
2000 diff_words_show(ecbdata->diff_words);
2002 if (wo->emitted_symbols) {
2003 struct diff_options *o = ecbdata->opt;
2004 struct emitted_diff_symbols *wol = wo->emitted_symbols;
2005 int i;
2008 * NEEDSWORK:
2009 * Instead of appending each, concat all words to a line?
2011 for (i = 0; i < wol->nr; i++)
2012 append_emitted_diff_symbol(o, &wol->buf[i]);
2014 for (i = 0; i < wol->nr; i++)
2015 free((void *)wol->buf[i].line);
2017 wol->nr = 0;
2021 static void diff_filespec_load_driver(struct diff_filespec *one)
2023 /* Use already-loaded driver */
2024 if (one->driver)
2025 return;
2027 if (S_ISREG(one->mode))
2028 one->driver = userdiff_find_by_path(one->path);
2030 /* Fallback to default settings */
2031 if (!one->driver)
2032 one->driver = userdiff_find_by_name("default");
2035 static const char *userdiff_word_regex(struct diff_filespec *one)
2037 diff_filespec_load_driver(one);
2038 return one->driver->word_regex;
2041 static void init_diff_words_data(struct emit_callback *ecbdata,
2042 struct diff_options *orig_opts,
2043 struct diff_filespec *one,
2044 struct diff_filespec *two)
2046 int i;
2047 struct diff_options *o = xmalloc(sizeof(struct diff_options));
2048 memcpy(o, orig_opts, sizeof(struct diff_options));
2050 ecbdata->diff_words =
2051 xcalloc(1, sizeof(struct diff_words_data));
2052 ecbdata->diff_words->type = o->word_diff;
2053 ecbdata->diff_words->opt = o;
2055 if (orig_opts->emitted_symbols)
2056 o->emitted_symbols =
2057 xcalloc(1, sizeof(struct emitted_diff_symbols));
2059 if (!o->word_regex)
2060 o->word_regex = userdiff_word_regex(one);
2061 if (!o->word_regex)
2062 o->word_regex = userdiff_word_regex(two);
2063 if (!o->word_regex)
2064 o->word_regex = diff_word_regex_cfg;
2065 if (o->word_regex) {
2066 ecbdata->diff_words->word_regex = (regex_t *)
2067 xmalloc(sizeof(regex_t));
2068 if (regcomp(ecbdata->diff_words->word_regex,
2069 o->word_regex,
2070 REG_EXTENDED | REG_NEWLINE))
2071 die ("Invalid regular expression: %s",
2072 o->word_regex);
2074 for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
2075 if (o->word_diff == diff_words_styles[i].type) {
2076 ecbdata->diff_words->style =
2077 &diff_words_styles[i];
2078 break;
2081 if (want_color(o->use_color)) {
2082 struct diff_words_style *st = ecbdata->diff_words->style;
2083 st->old_word.color = diff_get_color_opt(o, DIFF_FILE_OLD);
2084 st->new_word.color = diff_get_color_opt(o, DIFF_FILE_NEW);
2085 st->ctx.color = diff_get_color_opt(o, DIFF_CONTEXT);
2089 static void free_diff_words_data(struct emit_callback *ecbdata)
2091 if (ecbdata->diff_words) {
2092 diff_words_flush(ecbdata);
2093 free (ecbdata->diff_words->opt->emitted_symbols);
2094 free (ecbdata->diff_words->opt);
2095 free (ecbdata->diff_words->minus.text.ptr);
2096 free (ecbdata->diff_words->minus.orig);
2097 free (ecbdata->diff_words->plus.text.ptr);
2098 free (ecbdata->diff_words->plus.orig);
2099 if (ecbdata->diff_words->word_regex) {
2100 regfree(ecbdata->diff_words->word_regex);
2101 free(ecbdata->diff_words->word_regex);
2103 FREE_AND_NULL(ecbdata->diff_words);
2107 const char *diff_get_color(int diff_use_color, enum color_diff ix)
2109 if (want_color(diff_use_color))
2110 return diff_colors[ix];
2111 return "";
2114 const char *diff_line_prefix(struct diff_options *opt)
2116 struct strbuf *msgbuf;
2117 if (!opt->output_prefix)
2118 return "";
2120 msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
2121 return msgbuf->buf;
2124 static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len)
2126 const char *cp;
2127 unsigned long allot;
2128 size_t l = len;
2130 cp = line;
2131 allot = l;
2132 while (0 < l) {
2133 (void) utf8_width(&cp, &l);
2134 if (!cp)
2135 break; /* truncated in the middle? */
2137 return allot - l;
2140 static void find_lno(const char *line, struct emit_callback *ecbdata)
2142 const char *p;
2143 ecbdata->lno_in_preimage = 0;
2144 ecbdata->lno_in_postimage = 0;
2145 p = strchr(line, '-');
2146 if (!p)
2147 return; /* cannot happen */
2148 ecbdata->lno_in_preimage = strtol(p + 1, NULL, 10);
2149 p = strchr(p, '+');
2150 if (!p)
2151 return; /* cannot happen */
2152 ecbdata->lno_in_postimage = strtol(p + 1, NULL, 10);
2155 static void fn_out_consume(void *priv, char *line, unsigned long len)
2157 struct emit_callback *ecbdata = priv;
2158 const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
2159 struct diff_options *o = ecbdata->opt;
2161 o->found_changes = 1;
2163 if (ecbdata->header) {
2164 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
2165 ecbdata->header->buf, ecbdata->header->len, 0);
2166 strbuf_reset(ecbdata->header);
2167 ecbdata->header = NULL;
2170 if (ecbdata->label_path[0]) {
2171 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_MINUS,
2172 ecbdata->label_path[0],
2173 strlen(ecbdata->label_path[0]), 0);
2174 emit_diff_symbol(o, DIFF_SYMBOL_FILEPAIR_PLUS,
2175 ecbdata->label_path[1],
2176 strlen(ecbdata->label_path[1]), 0);
2177 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
2180 if (diff_suppress_blank_empty
2181 && len == 2 && line[0] == ' ' && line[1] == '\n') {
2182 line[0] = '\n';
2183 len = 1;
2186 if (line[0] == '@') {
2187 if (ecbdata->diff_words)
2188 diff_words_flush(ecbdata);
2189 len = sane_truncate_line(ecbdata, line, len);
2190 find_lno(line, ecbdata);
2191 emit_hunk_header(ecbdata, line, len);
2192 return;
2195 if (ecbdata->diff_words) {
2196 enum diff_symbol s =
2197 ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN ?
2198 DIFF_SYMBOL_WORDS_PORCELAIN : DIFF_SYMBOL_WORDS;
2199 if (line[0] == '-') {
2200 diff_words_append(line, len,
2201 &ecbdata->diff_words->minus);
2202 return;
2203 } else if (line[0] == '+') {
2204 diff_words_append(line, len,
2205 &ecbdata->diff_words->plus);
2206 return;
2207 } else if (starts_with(line, "\\ ")) {
2209 * Eat the "no newline at eof" marker as if we
2210 * saw a "+" or "-" line with nothing on it,
2211 * and return without diff_words_flush() to
2212 * defer processing. If this is the end of
2213 * preimage, more "+" lines may come after it.
2215 return;
2217 diff_words_flush(ecbdata);
2218 emit_diff_symbol(o, s, line, len, 0);
2219 return;
2222 switch (line[0]) {
2223 case '+':
2224 ecbdata->lno_in_postimage++;
2225 emit_add_line(reset, ecbdata, line + 1, len - 1);
2226 break;
2227 case '-':
2228 ecbdata->lno_in_preimage++;
2229 emit_del_line(reset, ecbdata, line + 1, len - 1);
2230 break;
2231 case ' ':
2232 ecbdata->lno_in_postimage++;
2233 ecbdata->lno_in_preimage++;
2234 emit_context_line(reset, ecbdata, line + 1, len - 1);
2235 break;
2236 default:
2237 /* incomplete line at the end */
2238 ecbdata->lno_in_preimage++;
2239 emit_diff_symbol(o, DIFF_SYMBOL_CONTEXT_INCOMPLETE,
2240 line, len, 0);
2241 break;
2245 static void pprint_rename(struct strbuf *name, const char *a, const char *b)
2247 const char *old_name = a;
2248 const char *new_name = b;
2249 int pfx_length, sfx_length;
2250 int pfx_adjust_for_slash;
2251 int len_a = strlen(a);
2252 int len_b = strlen(b);
2253 int a_midlen, b_midlen;
2254 int qlen_a = quote_c_style(a, NULL, NULL, 0);
2255 int qlen_b = quote_c_style(b, NULL, NULL, 0);
2257 if (qlen_a || qlen_b) {
2258 quote_c_style(a, name, NULL, 0);
2259 strbuf_addstr(name, " => ");
2260 quote_c_style(b, name, NULL, 0);
2261 return;
2264 /* Find common prefix */
2265 pfx_length = 0;
2266 while (*old_name && *new_name && *old_name == *new_name) {
2267 if (*old_name == '/')
2268 pfx_length = old_name - a + 1;
2269 old_name++;
2270 new_name++;
2273 /* Find common suffix */
2274 old_name = a + len_a;
2275 new_name = b + len_b;
2276 sfx_length = 0;
2278 * If there is a common prefix, it must end in a slash. In
2279 * that case we let this loop run 1 into the prefix to see the
2280 * same slash.
2282 * If there is no common prefix, we cannot do this as it would
2283 * underrun the input strings.
2285 pfx_adjust_for_slash = (pfx_length ? 1 : 0);
2286 while (a + pfx_length - pfx_adjust_for_slash <= old_name &&
2287 b + pfx_length - pfx_adjust_for_slash <= new_name &&
2288 *old_name == *new_name) {
2289 if (*old_name == '/')
2290 sfx_length = len_a - (old_name - a);
2291 old_name--;
2292 new_name--;
2296 * pfx{mid-a => mid-b}sfx
2297 * {pfx-a => pfx-b}sfx
2298 * pfx{sfx-a => sfx-b}
2299 * name-a => name-b
2301 a_midlen = len_a - pfx_length - sfx_length;
2302 b_midlen = len_b - pfx_length - sfx_length;
2303 if (a_midlen < 0)
2304 a_midlen = 0;
2305 if (b_midlen < 0)
2306 b_midlen = 0;
2308 strbuf_grow(name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
2309 if (pfx_length + sfx_length) {
2310 strbuf_add(name, a, pfx_length);
2311 strbuf_addch(name, '{');
2313 strbuf_add(name, a + pfx_length, a_midlen);
2314 strbuf_addstr(name, " => ");
2315 strbuf_add(name, b + pfx_length, b_midlen);
2316 if (pfx_length + sfx_length) {
2317 strbuf_addch(name, '}');
2318 strbuf_add(name, a + len_a - sfx_length, sfx_length);
2322 struct diffstat_t {
2323 int nr;
2324 int alloc;
2325 struct diffstat_file {
2326 char *from_name;
2327 char *name;
2328 char *print_name;
2329 const char *comments;
2330 unsigned is_unmerged:1;
2331 unsigned is_binary:1;
2332 unsigned is_renamed:1;
2333 unsigned is_interesting:1;
2334 uintmax_t added, deleted;
2335 } **files;
2338 static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
2339 const char *name_a,
2340 const char *name_b)
2342 struct diffstat_file *x;
2343 x = xcalloc(1, sizeof(*x));
2344 ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
2345 diffstat->files[diffstat->nr++] = x;
2346 if (name_b) {
2347 x->from_name = xstrdup(name_a);
2348 x->name = xstrdup(name_b);
2349 x->is_renamed = 1;
2351 else {
2352 x->from_name = NULL;
2353 x->name = xstrdup(name_a);
2355 return x;
2358 static void diffstat_consume(void *priv, char *line, unsigned long len)
2360 struct diffstat_t *diffstat = priv;
2361 struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
2363 if (line[0] == '+')
2364 x->added++;
2365 else if (line[0] == '-')
2366 x->deleted++;
2369 const char mime_boundary_leader[] = "------------";
2371 static int scale_linear(int it, int width, int max_change)
2373 if (!it)
2374 return 0;
2376 * make sure that at least one '-' or '+' is printed if
2377 * there is any change to this path. The easiest way is to
2378 * scale linearly as if the alloted width is one column shorter
2379 * than it is, and then add 1 to the result.
2381 return 1 + (it * (width - 1) / max_change);
2384 static void show_graph(struct strbuf *out, char ch, int cnt,
2385 const char *set, const char *reset)
2387 if (cnt <= 0)
2388 return;
2389 strbuf_addstr(out, set);
2390 strbuf_addchars(out, ch, cnt);
2391 strbuf_addstr(out, reset);
2394 static void fill_print_name(struct diffstat_file *file)
2396 struct strbuf pname = STRBUF_INIT;
2398 if (file->print_name)
2399 return;
2401 if (file->is_renamed)
2402 pprint_rename(&pname, file->from_name, file->name);
2403 else
2404 quote_c_style(file->name, &pname, NULL, 0);
2406 if (file->comments)
2407 strbuf_addf(&pname, " (%s)", file->comments);
2409 file->print_name = strbuf_detach(&pname, NULL);
2412 static void print_stat_summary_inserts_deletes(struct diff_options *options,
2413 int files, int insertions, int deletions)
2415 struct strbuf sb = STRBUF_INIT;
2417 if (!files) {
2418 assert(insertions == 0 && deletions == 0);
2419 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES,
2420 NULL, 0, 0);
2421 return;
2424 strbuf_addf(&sb,
2425 (files == 1) ? " %d file changed" : " %d files changed",
2426 files);
2429 * For binary diff, the caller may want to print "x files
2430 * changed" with insertions == 0 && deletions == 0.
2432 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2433 * is probably less confusing (i.e skip over "2 files changed
2434 * but nothing about added/removed lines? Is this a bug in Git?").
2436 if (insertions || deletions == 0) {
2437 strbuf_addf(&sb,
2438 (insertions == 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2439 insertions);
2442 if (deletions || insertions == 0) {
2443 strbuf_addf(&sb,
2444 (deletions == 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2445 deletions);
2447 strbuf_addch(&sb, '\n');
2448 emit_diff_symbol(options, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES,
2449 sb.buf, sb.len, 0);
2450 strbuf_release(&sb);
2453 void print_stat_summary(FILE *fp, int files,
2454 int insertions, int deletions)
2456 struct diff_options o;
2457 memset(&o, 0, sizeof(o));
2458 o.file = fp;
2460 print_stat_summary_inserts_deletes(&o, files, insertions, deletions);
2463 static void show_stats(struct diffstat_t *data, struct diff_options *options)
2465 int i, len, add, del, adds = 0, dels = 0;
2466 uintmax_t max_change = 0, max_len = 0;
2467 int total_files = data->nr, count;
2468 int width, name_width, graph_width, number_width = 0, bin_width = 0;
2469 const char *reset, *add_c, *del_c;
2470 int extra_shown = 0;
2471 const char *line_prefix = diff_line_prefix(options);
2472 struct strbuf out = STRBUF_INIT;
2474 if (data->nr == 0)
2475 return;
2477 count = options->stat_count ? options->stat_count : data->nr;
2479 reset = diff_get_color_opt(options, DIFF_RESET);
2480 add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
2481 del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
2484 * Find the longest filename and max number of changes
2486 for (i = 0; (i < count) && (i < data->nr); i++) {
2487 struct diffstat_file *file = data->files[i];
2488 uintmax_t change = file->added + file->deleted;
2490 if (!file->is_interesting && (change == 0)) {
2491 count++; /* not shown == room for one more */
2492 continue;
2494 fill_print_name(file);
2495 len = strlen(file->print_name);
2496 if (max_len < len)
2497 max_len = len;
2499 if (file->is_unmerged) {
2500 /* "Unmerged" is 8 characters */
2501 bin_width = bin_width < 8 ? 8 : bin_width;
2502 continue;
2504 if (file->is_binary) {
2505 /* "Bin XXX -> YYY bytes" */
2506 int w = 14 + decimal_width(file->added)
2507 + decimal_width(file->deleted);
2508 bin_width = bin_width < w ? w : bin_width;
2509 /* Display change counts aligned with "Bin" */
2510 number_width = 3;
2511 continue;
2514 if (max_change < change)
2515 max_change = change;
2517 count = i; /* where we can stop scanning in data->files[] */
2520 * We have width = stat_width or term_columns() columns total.
2521 * We want a maximum of min(max_len, stat_name_width) for the name part.
2522 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2523 * We also need 1 for " " and 4 + decimal_width(max_change)
2524 * for " | NNNN " and one the empty column at the end, altogether
2525 * 6 + decimal_width(max_change).
2527 * If there's not enough space, we will use the smaller of
2528 * stat_name_width (if set) and 5/8*width for the filename,
2529 * and the rest for constant elements + graph part, but no more
2530 * than stat_graph_width for the graph part.
2531 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2532 * for the standard terminal size).
2534 * In other words: stat_width limits the maximum width, and
2535 * stat_name_width fixes the maximum width of the filename,
2536 * and is also used to divide available columns if there
2537 * aren't enough.
2539 * Binary files are displayed with "Bin XXX -> YYY bytes"
2540 * instead of the change count and graph. This part is treated
2541 * similarly to the graph part, except that it is not
2542 * "scaled". If total width is too small to accommodate the
2543 * guaranteed minimum width of the filename part and the
2544 * separators and this message, this message will "overflow"
2545 * making the line longer than the maximum width.
2548 if (options->stat_width == -1)
2549 width = term_columns() - strlen(line_prefix);
2550 else
2551 width = options->stat_width ? options->stat_width : 80;
2552 number_width = decimal_width(max_change) > number_width ?
2553 decimal_width(max_change) : number_width;
2555 if (options->stat_graph_width == -1)
2556 options->stat_graph_width = diff_stat_graph_width;
2559 * Guarantee 3/8*16==6 for the graph part
2560 * and 5/8*16==10 for the filename part
2562 if (width < 16 + 6 + number_width)
2563 width = 16 + 6 + number_width;
2566 * First assign sizes that are wanted, ignoring available width.
2567 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2568 * starting from "XXX" should fit in graph_width.
2570 graph_width = max_change + 4 > bin_width ? max_change : bin_width - 4;
2571 if (options->stat_graph_width &&
2572 options->stat_graph_width < graph_width)
2573 graph_width = options->stat_graph_width;
2575 name_width = (options->stat_name_width > 0 &&
2576 options->stat_name_width < max_len) ?
2577 options->stat_name_width : max_len;
2580 * Adjust adjustable widths not to exceed maximum width
2582 if (name_width + number_width + 6 + graph_width > width) {
2583 if (graph_width > width * 3/8 - number_width - 6) {
2584 graph_width = width * 3/8 - number_width - 6;
2585 if (graph_width < 6)
2586 graph_width = 6;
2589 if (options->stat_graph_width &&
2590 graph_width > options->stat_graph_width)
2591 graph_width = options->stat_graph_width;
2592 if (name_width > width - number_width - 6 - graph_width)
2593 name_width = width - number_width - 6 - graph_width;
2594 else
2595 graph_width = width - number_width - 6 - name_width;
2599 * From here name_width is the width of the name area,
2600 * and graph_width is the width of the graph area.
2601 * max_change is used to scale graph properly.
2603 for (i = 0; i < count; i++) {
2604 const char *prefix = "";
2605 struct diffstat_file *file = data->files[i];
2606 char *name = file->print_name;
2607 uintmax_t added = file->added;
2608 uintmax_t deleted = file->deleted;
2609 int name_len;
2611 if (!file->is_interesting && (added + deleted == 0))
2612 continue;
2615 * "scale" the filename
2617 len = name_width;
2618 name_len = strlen(name);
2619 if (name_width < name_len) {
2620 char *slash;
2621 prefix = "...";
2622 len -= 3;
2623 name += name_len - len;
2624 slash = strchr(name, '/');
2625 if (slash)
2626 name = slash;
2629 if (file->is_binary) {
2630 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2631 strbuf_addf(&out, " %*s", number_width, "Bin");
2632 if (!added && !deleted) {
2633 strbuf_addch(&out, '\n');
2634 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2635 out.buf, out.len, 0);
2636 strbuf_reset(&out);
2637 continue;
2639 strbuf_addf(&out, " %s%"PRIuMAX"%s",
2640 del_c, deleted, reset);
2641 strbuf_addstr(&out, " -> ");
2642 strbuf_addf(&out, "%s%"PRIuMAX"%s",
2643 add_c, added, reset);
2644 strbuf_addstr(&out, " bytes\n");
2645 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2646 out.buf, out.len, 0);
2647 strbuf_reset(&out);
2648 continue;
2650 else if (file->is_unmerged) {
2651 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2652 strbuf_addstr(&out, " Unmerged\n");
2653 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2654 out.buf, out.len, 0);
2655 strbuf_reset(&out);
2656 continue;
2660 * scale the add/delete
2662 add = added;
2663 del = deleted;
2665 if (graph_width <= max_change) {
2666 int total = scale_linear(add + del, graph_width, max_change);
2667 if (total < 2 && add && del)
2668 /* width >= 2 due to the sanity check */
2669 total = 2;
2670 if (add < del) {
2671 add = scale_linear(add, graph_width, max_change);
2672 del = total - add;
2673 } else {
2674 del = scale_linear(del, graph_width, max_change);
2675 add = total - del;
2678 strbuf_addf(&out, " %s%-*s |", prefix, len, name);
2679 strbuf_addf(&out, " %*"PRIuMAX"%s",
2680 number_width, added + deleted,
2681 added + deleted ? " " : "");
2682 show_graph(&out, '+', add, add_c, reset);
2683 show_graph(&out, '-', del, del_c, reset);
2684 strbuf_addch(&out, '\n');
2685 emit_diff_symbol(options, DIFF_SYMBOL_STATS_LINE,
2686 out.buf, out.len, 0);
2687 strbuf_reset(&out);
2690 for (i = 0; i < data->nr; i++) {
2691 struct diffstat_file *file = data->files[i];
2692 uintmax_t added = file->added;
2693 uintmax_t deleted = file->deleted;
2695 if (file->is_unmerged ||
2696 (!file->is_interesting && (added + deleted == 0))) {
2697 total_files--;
2698 continue;
2701 if (!file->is_binary) {
2702 adds += added;
2703 dels += deleted;
2705 if (i < count)
2706 continue;
2707 if (!extra_shown)
2708 emit_diff_symbol(options,
2709 DIFF_SYMBOL_STATS_SUMMARY_ABBREV,
2710 NULL, 0, 0);
2711 extra_shown = 1;
2714 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
2715 strbuf_release(&out);
2718 static void show_shortstats(struct diffstat_t *data, struct diff_options *options)
2720 int i, adds = 0, dels = 0, total_files = data->nr;
2722 if (data->nr == 0)
2723 return;
2725 for (i = 0; i < data->nr; i++) {
2726 int added = data->files[i]->added;
2727 int deleted = data->files[i]->deleted;
2729 if (data->files[i]->is_unmerged ||
2730 (!data->files[i]->is_interesting && (added + deleted == 0))) {
2731 total_files--;
2732 } else if (!data->files[i]->is_binary) { /* don't count bytes */
2733 adds += added;
2734 dels += deleted;
2737 print_stat_summary_inserts_deletes(options, total_files, adds, dels);
2740 static void show_numstat(struct diffstat_t *data, struct diff_options *options)
2742 int i;
2744 if (data->nr == 0)
2745 return;
2747 for (i = 0; i < data->nr; i++) {
2748 struct diffstat_file *file = data->files[i];
2750 fprintf(options->file, "%s", diff_line_prefix(options));
2752 if (file->is_binary)
2753 fprintf(options->file, "-\t-\t");
2754 else
2755 fprintf(options->file,
2756 "%"PRIuMAX"\t%"PRIuMAX"\t",
2757 file->added, file->deleted);
2758 if (options->line_termination) {
2759 fill_print_name(file);
2760 if (!file->is_renamed)
2761 write_name_quoted(file->name, options->file,
2762 options->line_termination);
2763 else {
2764 fputs(file->print_name, options->file);
2765 putc(options->line_termination, options->file);
2767 } else {
2768 if (file->is_renamed) {
2769 putc('\0', options->file);
2770 write_name_quoted(file->from_name, options->file, '\0');
2772 write_name_quoted(file->name, options->file, '\0');
2777 struct dirstat_file {
2778 const char *name;
2779 unsigned long changed;
2782 struct dirstat_dir {
2783 struct dirstat_file *files;
2784 int alloc, nr, permille, cumulative;
2787 static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
2788 unsigned long changed, const char *base, int baselen)
2790 unsigned long sum_changes = 0;
2791 unsigned int sources = 0;
2792 const char *line_prefix = diff_line_prefix(opt);
2794 while (dir->nr) {
2795 struct dirstat_file *f = dir->files;
2796 int namelen = strlen(f->name);
2797 unsigned long changes;
2798 char *slash;
2800 if (namelen < baselen)
2801 break;
2802 if (memcmp(f->name, base, baselen))
2803 break;
2804 slash = strchr(f->name + baselen, '/');
2805 if (slash) {
2806 int newbaselen = slash + 1 - f->name;
2807 changes = gather_dirstat(opt, dir, changed, f->name, newbaselen);
2808 sources++;
2809 } else {
2810 changes = f->changed;
2811 dir->files++;
2812 dir->nr--;
2813 sources += 2;
2815 sum_changes += changes;
2819 * We don't report dirstat's for
2820 * - the top level
2821 * - or cases where everything came from a single directory
2822 * under this directory (sources == 1).
2824 if (baselen && sources != 1) {
2825 if (sum_changes) {
2826 int permille = sum_changes * 1000 / changed;
2827 if (permille >= dir->permille) {
2828 fprintf(opt->file, "%s%4d.%01d%% %.*s\n", line_prefix,
2829 permille / 10, permille % 10, baselen, base);
2830 if (!dir->cumulative)
2831 return 0;
2835 return sum_changes;
2838 static int dirstat_compare(const void *_a, const void *_b)
2840 const struct dirstat_file *a = _a;
2841 const struct dirstat_file *b = _b;
2842 return strcmp(a->name, b->name);
2845 static void show_dirstat(struct diff_options *options)
2847 int i;
2848 unsigned long changed;
2849 struct dirstat_dir dir;
2850 struct diff_queue_struct *q = &diff_queued_diff;
2852 dir.files = NULL;
2853 dir.alloc = 0;
2854 dir.nr = 0;
2855 dir.permille = options->dirstat_permille;
2856 dir.cumulative = options->flags.dirstat_cumulative;
2858 changed = 0;
2859 for (i = 0; i < q->nr; i++) {
2860 struct diff_filepair *p = q->queue[i];
2861 const char *name;
2862 unsigned long copied, added, damage;
2863 int content_changed;
2865 name = p->two->path ? p->two->path : p->one->path;
2867 if (p->one->oid_valid && p->two->oid_valid)
2868 content_changed = oidcmp(&p->one->oid, &p->two->oid);
2869 else
2870 content_changed = 1;
2872 if (!content_changed) {
2874 * The SHA1 has not changed, so pre-/post-content is
2875 * identical. We can therefore skip looking at the
2876 * file contents altogether.
2878 damage = 0;
2879 goto found_damage;
2882 if (options->flags.dirstat_by_file) {
2884 * In --dirstat-by-file mode, we don't really need to
2885 * look at the actual file contents at all.
2886 * The fact that the SHA1 changed is enough for us to
2887 * add this file to the list of results
2888 * (with each file contributing equal damage).
2890 damage = 1;
2891 goto found_damage;
2894 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
2895 diff_populate_filespec(p->one, 0);
2896 diff_populate_filespec(p->two, 0);
2897 diffcore_count_changes(p->one, p->two, NULL, NULL,
2898 &copied, &added);
2899 diff_free_filespec_data(p->one);
2900 diff_free_filespec_data(p->two);
2901 } else if (DIFF_FILE_VALID(p->one)) {
2902 diff_populate_filespec(p->one, CHECK_SIZE_ONLY);
2903 copied = added = 0;
2904 diff_free_filespec_data(p->one);
2905 } else if (DIFF_FILE_VALID(p->two)) {
2906 diff_populate_filespec(p->two, CHECK_SIZE_ONLY);
2907 copied = 0;
2908 added = p->two->size;
2909 diff_free_filespec_data(p->two);
2910 } else
2911 continue;
2914 * Original minus copied is the removed material,
2915 * added is the new material. They are both damages
2916 * made to the preimage.
2917 * If the resulting damage is zero, we know that
2918 * diffcore_count_changes() considers the two entries to
2919 * be identical, but since content_changed is true, we
2920 * know that there must have been _some_ kind of change,
2921 * so we force all entries to have damage > 0.
2923 damage = (p->one->size - copied) + added;
2924 if (!damage)
2925 damage = 1;
2927 found_damage:
2928 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
2929 dir.files[dir.nr].name = name;
2930 dir.files[dir.nr].changed = damage;
2931 changed += damage;
2932 dir.nr++;
2935 /* This can happen even with many files, if everything was renames */
2936 if (!changed)
2937 return;
2939 /* Show all directories with more than x% of the changes */
2940 QSORT(dir.files, dir.nr, dirstat_compare);
2941 gather_dirstat(options, &dir, changed, "", 0);
2944 static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
2946 int i;
2947 unsigned long changed;
2948 struct dirstat_dir dir;
2950 if (data->nr == 0)
2951 return;
2953 dir.files = NULL;
2954 dir.alloc = 0;
2955 dir.nr = 0;
2956 dir.permille = options->dirstat_permille;
2957 dir.cumulative = options->flags.dirstat_cumulative;
2959 changed = 0;
2960 for (i = 0; i < data->nr; i++) {
2961 struct diffstat_file *file = data->files[i];
2962 unsigned long damage = file->added + file->deleted;
2963 if (file->is_binary)
2965 * binary files counts bytes, not lines. Must find some
2966 * way to normalize binary bytes vs. textual lines.
2967 * The following heuristic assumes that there are 64
2968 * bytes per "line".
2969 * This is stupid and ugly, but very cheap...
2971 damage = DIV_ROUND_UP(damage, 64);
2972 ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
2973 dir.files[dir.nr].name = file->name;
2974 dir.files[dir.nr].changed = damage;
2975 changed += damage;
2976 dir.nr++;
2979 /* This can happen even with many files, if everything was renames */
2980 if (!changed)
2981 return;
2983 /* Show all directories with more than x% of the changes */
2984 QSORT(dir.files, dir.nr, dirstat_compare);
2985 gather_dirstat(options, &dir, changed, "", 0);
2988 static void free_diffstat_info(struct diffstat_t *diffstat)
2990 int i;
2991 for (i = 0; i < diffstat->nr; i++) {
2992 struct diffstat_file *f = diffstat->files[i];
2993 free(f->print_name);
2994 free(f->name);
2995 free(f->from_name);
2996 free(f);
2998 free(diffstat->files);
3001 struct checkdiff_t {
3002 const char *filename;
3003 int lineno;
3004 int conflict_marker_size;
3005 struct diff_options *o;
3006 unsigned ws_rule;
3007 unsigned status;
3010 static int is_conflict_marker(const char *line, int marker_size, unsigned long len)
3012 char firstchar;
3013 int cnt;
3015 if (len < marker_size + 1)
3016 return 0;
3017 firstchar = line[0];
3018 switch (firstchar) {
3019 case '=': case '>': case '<': case '|':
3020 break;
3021 default:
3022 return 0;
3024 for (cnt = 1; cnt < marker_size; cnt++)
3025 if (line[cnt] != firstchar)
3026 return 0;
3027 /* line[1] thru line[marker_size-1] are same as firstchar */
3028 if (len < marker_size + 1 || !isspace(line[marker_size]))
3029 return 0;
3030 return 1;
3033 static void checkdiff_consume(void *priv, char *line, unsigned long len)
3035 struct checkdiff_t *data = priv;
3036 int marker_size = data->conflict_marker_size;
3037 const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
3038 const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
3039 const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
3040 char *err;
3041 const char *line_prefix;
3043 assert(data->o);
3044 line_prefix = diff_line_prefix(data->o);
3046 if (line[0] == '+') {
3047 unsigned bad;
3048 data->lineno++;
3049 if (is_conflict_marker(line + 1, marker_size, len - 1)) {
3050 data->status |= 1;
3051 fprintf(data->o->file,
3052 "%s%s:%d: leftover conflict marker\n",
3053 line_prefix, data->filename, data->lineno);
3055 bad = ws_check(line + 1, len - 1, data->ws_rule);
3056 if (!bad)
3057 return;
3058 data->status |= bad;
3059 err = whitespace_error_string(bad);
3060 fprintf(data->o->file, "%s%s:%d: %s.\n",
3061 line_prefix, data->filename, data->lineno, err);
3062 free(err);
3063 emit_line(data->o, set, reset, line, 1);
3064 ws_check_emit(line + 1, len - 1, data->ws_rule,
3065 data->o->file, set, reset, ws);
3066 } else if (line[0] == ' ') {
3067 data->lineno++;
3068 } else if (line[0] == '@') {
3069 char *plus = strchr(line, '+');
3070 if (plus)
3071 data->lineno = strtol(plus, NULL, 10) - 1;
3072 else
3073 die("invalid diff");
3077 static unsigned char *deflate_it(char *data,
3078 unsigned long size,
3079 unsigned long *result_size)
3081 int bound;
3082 unsigned char *deflated;
3083 git_zstream stream;
3085 git_deflate_init(&stream, zlib_compression_level);
3086 bound = git_deflate_bound(&stream, size);
3087 deflated = xmalloc(bound);
3088 stream.next_out = deflated;
3089 stream.avail_out = bound;
3091 stream.next_in = (unsigned char *)data;
3092 stream.avail_in = size;
3093 while (git_deflate(&stream, Z_FINISH) == Z_OK)
3094 ; /* nothing */
3095 git_deflate_end(&stream);
3096 *result_size = stream.total_out;
3097 return deflated;
3100 static void emit_binary_diff_body(struct diff_options *o,
3101 mmfile_t *one, mmfile_t *two)
3103 void *cp;
3104 void *delta;
3105 void *deflated;
3106 void *data;
3107 unsigned long orig_size;
3108 unsigned long delta_size;
3109 unsigned long deflate_size;
3110 unsigned long data_size;
3112 /* We could do deflated delta, or we could do just deflated two,
3113 * whichever is smaller.
3115 delta = NULL;
3116 deflated = deflate_it(two->ptr, two->size, &deflate_size);
3117 if (one->size && two->size) {
3118 delta = diff_delta(one->ptr, one->size,
3119 two->ptr, two->size,
3120 &delta_size, deflate_size);
3121 if (delta) {
3122 void *to_free = delta;
3123 orig_size = delta_size;
3124 delta = deflate_it(delta, delta_size, &delta_size);
3125 free(to_free);
3129 if (delta && delta_size < deflate_size) {
3130 char *s = xstrfmt("%lu", orig_size);
3131 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
3132 s, strlen(s), 0);
3133 free(s);
3134 free(deflated);
3135 data = delta;
3136 data_size = delta_size;
3137 } else {
3138 char *s = xstrfmt("%lu", two->size);
3139 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,
3140 s, strlen(s), 0);
3141 free(s);
3142 free(delta);
3143 data = deflated;
3144 data_size = deflate_size;
3147 /* emit data encoded in base85 */
3148 cp = data;
3149 while (data_size) {
3150 int len;
3151 int bytes = (52 < data_size) ? 52 : data_size;
3152 char line[71];
3153 data_size -= bytes;
3154 if (bytes <= 26)
3155 line[0] = bytes + 'A' - 1;
3156 else
3157 line[0] = bytes - 26 + 'a' - 1;
3158 encode_85(line + 1, cp, bytes);
3159 cp = (char *) cp + bytes;
3161 len = strlen(line);
3162 line[len++] = '\n';
3163 line[len] = '\0';
3165 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_BODY,
3166 line, len, 0);
3168 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_FOOTER, NULL, 0, 0);
3169 free(data);
3172 static void emit_binary_diff(struct diff_options *o,
3173 mmfile_t *one, mmfile_t *two)
3175 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER, NULL, 0, 0);
3176 emit_binary_diff_body(o, one, two);
3177 emit_binary_diff_body(o, two, one);
3180 int diff_filespec_is_binary(struct diff_filespec *one)
3182 if (one->is_binary == -1) {
3183 diff_filespec_load_driver(one);
3184 if (one->driver->binary != -1)
3185 one->is_binary = one->driver->binary;
3186 else {
3187 if (!one->data && DIFF_FILE_VALID(one))
3188 diff_populate_filespec(one, CHECK_BINARY);
3189 if (one->is_binary == -1 && one->data)
3190 one->is_binary = buffer_is_binary(one->data,
3191 one->size);
3192 if (one->is_binary == -1)
3193 one->is_binary = 0;
3196 return one->is_binary;
3199 static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one)
3201 diff_filespec_load_driver(one);
3202 return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
3205 void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
3207 if (!options->a_prefix)
3208 options->a_prefix = a;
3209 if (!options->b_prefix)
3210 options->b_prefix = b;
3213 struct userdiff_driver *get_textconv(struct diff_filespec *one)
3215 if (!DIFF_FILE_VALID(one))
3216 return NULL;
3218 diff_filespec_load_driver(one);
3219 return userdiff_get_textconv(one->driver);
3222 static void builtin_diff(const char *name_a,
3223 const char *name_b,
3224 struct diff_filespec *one,
3225 struct diff_filespec *two,
3226 const char *xfrm_msg,
3227 int must_show_header,
3228 struct diff_options *o,
3229 int complete_rewrite)
3231 mmfile_t mf1, mf2;
3232 const char *lbl[2];
3233 char *a_one, *b_two;
3234 const char *meta = diff_get_color_opt(o, DIFF_METAINFO);
3235 const char *reset = diff_get_color_opt(o, DIFF_RESET);
3236 const char *a_prefix, *b_prefix;
3237 struct userdiff_driver *textconv_one = NULL;
3238 struct userdiff_driver *textconv_two = NULL;
3239 struct strbuf header = STRBUF_INIT;
3240 const char *line_prefix = diff_line_prefix(o);
3242 diff_set_mnemonic_prefix(o, "a/", "b/");
3243 if (o->flags.reverse_diff) {
3244 a_prefix = o->b_prefix;
3245 b_prefix = o->a_prefix;
3246 } else {
3247 a_prefix = o->a_prefix;
3248 b_prefix = o->b_prefix;
3251 if (o->submodule_format == DIFF_SUBMODULE_LOG &&
3252 (!one->mode || S_ISGITLINK(one->mode)) &&
3253 (!two->mode || S_ISGITLINK(two->mode))) {
3254 show_submodule_summary(o, one->path ? one->path : two->path,
3255 &one->oid, &two->oid,
3256 two->dirty_submodule);
3257 return;
3258 } else if (o->submodule_format == DIFF_SUBMODULE_INLINE_DIFF &&
3259 (!one->mode || S_ISGITLINK(one->mode)) &&
3260 (!two->mode || S_ISGITLINK(two->mode))) {
3261 show_submodule_inline_diff(o, one->path ? one->path : two->path,
3262 &one->oid, &two->oid,
3263 two->dirty_submodule);
3264 return;
3267 if (o->flags.allow_textconv) {
3268 textconv_one = get_textconv(one);
3269 textconv_two = get_textconv(two);
3272 /* Never use a non-valid filename anywhere if at all possible */
3273 name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
3274 name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
3276 a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
3277 b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
3278 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
3279 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
3280 strbuf_addf(&header, "%s%sdiff --git %s %s%s\n", line_prefix, meta, a_one, b_two, reset);
3281 if (lbl[0][0] == '/') {
3282 /* /dev/null */
3283 strbuf_addf(&header, "%s%snew file mode %06o%s\n", line_prefix, meta, two->mode, reset);
3284 if (xfrm_msg)
3285 strbuf_addstr(&header, xfrm_msg);
3286 must_show_header = 1;
3288 else if (lbl[1][0] == '/') {
3289 strbuf_addf(&header, "%s%sdeleted file mode %06o%s\n", line_prefix, meta, one->mode, reset);
3290 if (xfrm_msg)
3291 strbuf_addstr(&header, xfrm_msg);
3292 must_show_header = 1;
3294 else {
3295 if (one->mode != two->mode) {
3296 strbuf_addf(&header, "%s%sold mode %06o%s\n", line_prefix, meta, one->mode, reset);
3297 strbuf_addf(&header, "%s%snew mode %06o%s\n", line_prefix, meta, two->mode, reset);
3298 must_show_header = 1;
3300 if (xfrm_msg)
3301 strbuf_addstr(&header, xfrm_msg);
3304 * we do not run diff between different kind
3305 * of objects.
3307 if ((one->mode ^ two->mode) & S_IFMT)
3308 goto free_ab_and_return;
3309 if (complete_rewrite &&
3310 (textconv_one || !diff_filespec_is_binary(one)) &&
3311 (textconv_two || !diff_filespec_is_binary(two))) {
3312 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3313 header.buf, header.len, 0);
3314 strbuf_reset(&header);
3315 emit_rewrite_diff(name_a, name_b, one, two,
3316 textconv_one, textconv_two, o);
3317 o->found_changes = 1;
3318 goto free_ab_and_return;
3322 if (o->irreversible_delete && lbl[1][0] == '/') {
3323 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf,
3324 header.len, 0);
3325 strbuf_reset(&header);
3326 goto free_ab_and_return;
3327 } else if (!o->flags.text &&
3328 ( (!textconv_one && diff_filespec_is_binary(one)) ||
3329 (!textconv_two && diff_filespec_is_binary(two)) )) {
3330 struct strbuf sb = STRBUF_INIT;
3331 if (!one->data && !two->data &&
3332 S_ISREG(one->mode) && S_ISREG(two->mode) &&
3333 !o->flags.binary) {
3334 if (!oidcmp(&one->oid, &two->oid)) {
3335 if (must_show_header)
3336 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3337 header.buf, header.len,
3339 goto free_ab_and_return;
3341 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3342 header.buf, header.len, 0);
3343 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3344 diff_line_prefix(o), lbl[0], lbl[1]);
3345 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3346 sb.buf, sb.len, 0);
3347 strbuf_release(&sb);
3348 goto free_ab_and_return;
3350 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3351 die("unable to read files to diff");
3352 /* Quite common confusing case */
3353 if (mf1.size == mf2.size &&
3354 !memcmp(mf1.ptr, mf2.ptr, mf1.size)) {
3355 if (must_show_header)
3356 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3357 header.buf, header.len, 0);
3358 goto free_ab_and_return;
3360 emit_diff_symbol(o, DIFF_SYMBOL_HEADER, header.buf, header.len, 0);
3361 strbuf_reset(&header);
3362 if (o->flags.binary)
3363 emit_binary_diff(o, &mf1, &mf2);
3364 else {
3365 strbuf_addf(&sb, "%sBinary files %s and %s differ\n",
3366 diff_line_prefix(o), lbl[0], lbl[1]);
3367 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_FILES,
3368 sb.buf, sb.len, 0);
3369 strbuf_release(&sb);
3371 o->found_changes = 1;
3372 } else {
3373 /* Crazy xdl interfaces.. */
3374 const char *diffopts = getenv("GIT_DIFF_OPTS");
3375 const char *v;
3376 xpparam_t xpp;
3377 xdemitconf_t xecfg;
3378 struct emit_callback ecbdata;
3379 const struct userdiff_funcname *pe;
3381 if (must_show_header) {
3382 emit_diff_symbol(o, DIFF_SYMBOL_HEADER,
3383 header.buf, header.len, 0);
3384 strbuf_reset(&header);
3387 mf1.size = fill_textconv(textconv_one, one, &mf1.ptr);
3388 mf2.size = fill_textconv(textconv_two, two, &mf2.ptr);
3390 pe = diff_funcname_pattern(one);
3391 if (!pe)
3392 pe = diff_funcname_pattern(two);
3394 memset(&xpp, 0, sizeof(xpp));
3395 memset(&xecfg, 0, sizeof(xecfg));
3396 memset(&ecbdata, 0, sizeof(ecbdata));
3397 ecbdata.label_path = lbl;
3398 ecbdata.color_diff = want_color(o->use_color);
3399 ecbdata.ws_rule = whitespace_rule(name_b);
3400 if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
3401 check_blank_at_eof(&mf1, &mf2, &ecbdata);
3402 ecbdata.opt = o;
3403 ecbdata.header = header.len ? &header : NULL;
3404 xpp.flags = o->xdl_opts;
3405 xpp.anchors = o->anchors;
3406 xpp.anchors_nr = o->anchors_nr;
3407 xecfg.ctxlen = o->context;
3408 xecfg.interhunkctxlen = o->interhunkcontext;
3409 xecfg.flags = XDL_EMIT_FUNCNAMES;
3410 if (o->flags.funccontext)
3411 xecfg.flags |= XDL_EMIT_FUNCCONTEXT;
3412 if (pe)
3413 xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
3414 if (!diffopts)
3416 else if (skip_prefix(diffopts, "--unified=", &v))
3417 xecfg.ctxlen = strtoul(v, NULL, 10);
3418 else if (skip_prefix(diffopts, "-u", &v))
3419 xecfg.ctxlen = strtoul(v, NULL, 10);
3420 if (o->word_diff)
3421 init_diff_words_data(&ecbdata, o, one, two);
3422 if (xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
3423 &xpp, &xecfg))
3424 die("unable to generate diff for %s", one->path);
3425 if (o->word_diff)
3426 free_diff_words_data(&ecbdata);
3427 if (textconv_one)
3428 free(mf1.ptr);
3429 if (textconv_two)
3430 free(mf2.ptr);
3431 xdiff_clear_find_func(&xecfg);
3434 free_ab_and_return:
3435 strbuf_release(&header);
3436 diff_free_filespec_data(one);
3437 diff_free_filespec_data(two);
3438 free(a_one);
3439 free(b_two);
3440 return;
3443 static char *get_compact_summary(const struct diff_filepair *p, int is_renamed)
3445 if (!is_renamed) {
3446 if (p->status == DIFF_STATUS_ADDED) {
3447 if (S_ISLNK(p->two->mode))
3448 return "new +l";
3449 else if ((p->two->mode & 0777) == 0755)
3450 return "new +x";
3451 else
3452 return "new";
3453 } else if (p->status == DIFF_STATUS_DELETED)
3454 return "gone";
3456 if (S_ISLNK(p->one->mode) && !S_ISLNK(p->two->mode))
3457 return "mode -l";
3458 else if (!S_ISLNK(p->one->mode) && S_ISLNK(p->two->mode))
3459 return "mode +l";
3460 else if ((p->one->mode & 0777) == 0644 &&
3461 (p->two->mode & 0777) == 0755)
3462 return "mode +x";
3463 else if ((p->one->mode & 0777) == 0755 &&
3464 (p->two->mode & 0777) == 0644)
3465 return "mode -x";
3466 return NULL;
3469 static void builtin_diffstat(const char *name_a, const char *name_b,
3470 struct diff_filespec *one,
3471 struct diff_filespec *two,
3472 struct diffstat_t *diffstat,
3473 struct diff_options *o,
3474 struct diff_filepair *p)
3476 mmfile_t mf1, mf2;
3477 struct diffstat_file *data;
3478 int same_contents;
3479 int complete_rewrite = 0;
3481 if (!DIFF_PAIR_UNMERGED(p)) {
3482 if (p->status == DIFF_STATUS_MODIFIED && p->score)
3483 complete_rewrite = 1;
3486 data = diffstat_add(diffstat, name_a, name_b);
3487 data->is_interesting = p->status != DIFF_STATUS_UNKNOWN;
3488 if (o->flags.stat_with_summary)
3489 data->comments = get_compact_summary(p, data->is_renamed);
3491 if (!one || !two) {
3492 data->is_unmerged = 1;
3493 return;
3496 same_contents = !oidcmp(&one->oid, &two->oid);
3498 if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
3499 data->is_binary = 1;
3500 if (same_contents) {
3501 data->added = 0;
3502 data->deleted = 0;
3503 } else {
3504 data->added = diff_filespec_size(two);
3505 data->deleted = diff_filespec_size(one);
3509 else if (complete_rewrite) {
3510 diff_populate_filespec(one, 0);
3511 diff_populate_filespec(two, 0);
3512 data->deleted = count_lines(one->data, one->size);
3513 data->added = count_lines(two->data, two->size);
3516 else if (!same_contents) {
3517 /* Crazy xdl interfaces.. */
3518 xpparam_t xpp;
3519 xdemitconf_t xecfg;
3521 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3522 die("unable to read files to diff");
3524 memset(&xpp, 0, sizeof(xpp));
3525 memset(&xecfg, 0, sizeof(xecfg));
3526 xpp.flags = o->xdl_opts;
3527 xpp.anchors = o->anchors;
3528 xpp.anchors_nr = o->anchors_nr;
3529 xecfg.ctxlen = o->context;
3530 xecfg.interhunkctxlen = o->interhunkcontext;
3531 if (xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
3532 &xpp, &xecfg))
3533 die("unable to generate diffstat for %s", one->path);
3536 diff_free_filespec_data(one);
3537 diff_free_filespec_data(two);
3540 static void builtin_checkdiff(const char *name_a, const char *name_b,
3541 const char *attr_path,
3542 struct diff_filespec *one,
3543 struct diff_filespec *two,
3544 struct diff_options *o)
3546 mmfile_t mf1, mf2;
3547 struct checkdiff_t data;
3549 if (!two)
3550 return;
3552 memset(&data, 0, sizeof(data));
3553 data.filename = name_b ? name_b : name_a;
3554 data.lineno = 0;
3555 data.o = o;
3556 data.ws_rule = whitespace_rule(attr_path);
3557 data.conflict_marker_size = ll_merge_marker_size(attr_path);
3559 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
3560 die("unable to read files to diff");
3563 * All the other codepaths check both sides, but not checking
3564 * the "old" side here is deliberate. We are checking the newly
3565 * introduced changes, and as long as the "new" side is text, we
3566 * can and should check what it introduces.
3568 if (diff_filespec_is_binary(two))
3569 goto free_and_return;
3570 else {
3571 /* Crazy xdl interfaces.. */
3572 xpparam_t xpp;
3573 xdemitconf_t xecfg;
3575 memset(&xpp, 0, sizeof(xpp));
3576 memset(&xecfg, 0, sizeof(xecfg));
3577 xecfg.ctxlen = 1; /* at least one context line */
3578 xpp.flags = 0;
3579 if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
3580 &xpp, &xecfg))
3581 die("unable to generate checkdiff for %s", one->path);
3583 if (data.ws_rule & WS_BLANK_AT_EOF) {
3584 struct emit_callback ecbdata;
3585 int blank_at_eof;
3587 ecbdata.ws_rule = data.ws_rule;
3588 check_blank_at_eof(&mf1, &mf2, &ecbdata);
3589 blank_at_eof = ecbdata.blank_at_eof_in_postimage;
3591 if (blank_at_eof) {
3592 static char *err;
3593 if (!err)
3594 err = whitespace_error_string(WS_BLANK_AT_EOF);
3595 fprintf(o->file, "%s:%d: %s.\n",
3596 data.filename, blank_at_eof, err);
3597 data.status = 1; /* report errors */
3601 free_and_return:
3602 diff_free_filespec_data(one);
3603 diff_free_filespec_data(two);
3604 if (data.status)
3605 o->flags.check_failed = 1;
3608 struct diff_filespec *alloc_filespec(const char *path)
3610 struct diff_filespec *spec;
3612 FLEXPTR_ALLOC_STR(spec, path, path);
3613 spec->count = 1;
3614 spec->is_binary = -1;
3615 return spec;
3618 void free_filespec(struct diff_filespec *spec)
3620 if (!--spec->count) {
3621 diff_free_filespec_data(spec);
3622 free(spec);
3626 void fill_filespec(struct diff_filespec *spec, const struct object_id *oid,
3627 int oid_valid, unsigned short mode)
3629 if (mode) {
3630 spec->mode = canon_mode(mode);
3631 oidcpy(&spec->oid, oid);
3632 spec->oid_valid = oid_valid;
3637 * Given a name and sha1 pair, if the index tells us the file in
3638 * the work tree has that object contents, return true, so that
3639 * prepare_temp_file() does not have to inflate and extract.
3641 static int reuse_worktree_file(const char *name, const struct object_id *oid, int want_file)
3643 const struct cache_entry *ce;
3644 struct stat st;
3645 int pos, len;
3648 * We do not read the cache ourselves here, because the
3649 * benchmark with my previous version that always reads cache
3650 * shows that it makes things worse for diff-tree comparing
3651 * two linux-2.6 kernel trees in an already checked out work
3652 * tree. This is because most diff-tree comparisons deal with
3653 * only a small number of files, while reading the cache is
3654 * expensive for a large project, and its cost outweighs the
3655 * savings we get by not inflating the object to a temporary
3656 * file. Practically, this code only helps when we are used
3657 * by diff-cache --cached, which does read the cache before
3658 * calling us.
3660 if (!active_cache)
3661 return 0;
3663 /* We want to avoid the working directory if our caller
3664 * doesn't need the data in a normal file, this system
3665 * is rather slow with its stat/open/mmap/close syscalls,
3666 * and the object is contained in a pack file. The pack
3667 * is probably already open and will be faster to obtain
3668 * the data through than the working directory. Loose
3669 * objects however would tend to be slower as they need
3670 * to be individually opened and inflated.
3672 if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(oid->hash))
3673 return 0;
3676 * Similarly, if we'd have to convert the file contents anyway, that
3677 * makes the optimization not worthwhile.
3679 if (!want_file && would_convert_to_git(&the_index, name))
3680 return 0;
3682 len = strlen(name);
3683 pos = cache_name_pos(name, len);
3684 if (pos < 0)
3685 return 0;
3686 ce = active_cache[pos];
3689 * This is not the sha1 we are looking for, or
3690 * unreusable because it is not a regular file.
3692 if (oidcmp(oid, &ce->oid) || !S_ISREG(ce->ce_mode))
3693 return 0;
3696 * If ce is marked as "assume unchanged", there is no
3697 * guarantee that work tree matches what we are looking for.
3699 if ((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce))
3700 return 0;
3703 * If ce matches the file in the work tree, we can reuse it.
3705 if (ce_uptodate(ce) ||
3706 (!lstat(name, &st) && !ce_match_stat(ce, &st, 0)))
3707 return 1;
3709 return 0;
3712 static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
3714 struct strbuf buf = STRBUF_INIT;
3715 char *dirty = "";
3717 /* Are we looking at the work tree? */
3718 if (s->dirty_submodule)
3719 dirty = "-dirty";
3721 strbuf_addf(&buf, "Subproject commit %s%s\n",
3722 oid_to_hex(&s->oid), dirty);
3723 s->size = buf.len;
3724 if (size_only) {
3725 s->data = NULL;
3726 strbuf_release(&buf);
3727 } else {
3728 s->data = strbuf_detach(&buf, NULL);
3729 s->should_free = 1;
3731 return 0;
3735 * While doing rename detection and pickaxe operation, we may need to
3736 * grab the data for the blob (or file) for our own in-core comparison.
3737 * diff_filespec has data and size fields for this purpose.
3739 int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
3741 int size_only = flags & CHECK_SIZE_ONLY;
3742 int err = 0;
3743 int conv_flags = global_conv_flags_eol;
3745 * demote FAIL to WARN to allow inspecting the situation
3746 * instead of refusing.
3748 if (conv_flags & CONV_EOL_RNDTRP_DIE)
3749 conv_flags = CONV_EOL_RNDTRP_WARN;
3751 if (!DIFF_FILE_VALID(s))
3752 die("internal error: asking to populate invalid file.");
3753 if (S_ISDIR(s->mode))
3754 return -1;
3756 if (s->data)
3757 return 0;
3759 if (size_only && 0 < s->size)
3760 return 0;
3762 if (S_ISGITLINK(s->mode))
3763 return diff_populate_gitlink(s, size_only);
3765 if (!s->oid_valid ||
3766 reuse_worktree_file(s->path, &s->oid, 0)) {
3767 struct strbuf buf = STRBUF_INIT;
3768 struct stat st;
3769 int fd;
3771 if (lstat(s->path, &st) < 0) {
3772 err_empty:
3773 err = -1;
3774 empty:
3775 s->data = (char *)"";
3776 s->size = 0;
3777 return err;
3779 s->size = xsize_t(st.st_size);
3780 if (!s->size)
3781 goto empty;
3782 if (S_ISLNK(st.st_mode)) {
3783 struct strbuf sb = STRBUF_INIT;
3785 if (strbuf_readlink(&sb, s->path, s->size))
3786 goto err_empty;
3787 s->size = sb.len;
3788 s->data = strbuf_detach(&sb, NULL);
3789 s->should_free = 1;
3790 return 0;
3794 * Even if the caller would be happy with getting
3795 * only the size, we cannot return early at this
3796 * point if the path requires us to run the content
3797 * conversion.
3799 if (size_only && !would_convert_to_git(&the_index, s->path))
3800 return 0;
3803 * Note: this check uses xsize_t(st.st_size) that may
3804 * not be the true size of the blob after it goes
3805 * through convert_to_git(). This may not strictly be
3806 * correct, but the whole point of big_file_threshold
3807 * and is_binary check being that we want to avoid
3808 * opening the file and inspecting the contents, this
3809 * is probably fine.
3811 if ((flags & CHECK_BINARY) &&
3812 s->size > big_file_threshold && s->is_binary == -1) {
3813 s->is_binary = 1;
3814 return 0;
3816 fd = open(s->path, O_RDONLY);
3817 if (fd < 0)
3818 goto err_empty;
3819 s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
3820 close(fd);
3821 s->should_munmap = 1;
3824 * Convert from working tree format to canonical git format
3826 if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, conv_flags)) {
3827 size_t size = 0;
3828 munmap(s->data, s->size);
3829 s->should_munmap = 0;
3830 s->data = strbuf_detach(&buf, &size);
3831 s->size = size;
3832 s->should_free = 1;
3835 else {
3836 enum object_type type;
3837 if (size_only || (flags & CHECK_BINARY)) {
3838 type = oid_object_info(&s->oid, &s->size);
3839 if (type < 0)
3840 die("unable to read %s",
3841 oid_to_hex(&s->oid));
3842 if (size_only)
3843 return 0;
3844 if (s->size > big_file_threshold && s->is_binary == -1) {
3845 s->is_binary = 1;
3846 return 0;
3849 s->data = read_object_file(&s->oid, &type, &s->size);
3850 if (!s->data)
3851 die("unable to read %s", oid_to_hex(&s->oid));
3852 s->should_free = 1;
3854 return 0;
3857 void diff_free_filespec_blob(struct diff_filespec *s)
3859 if (s->should_free)
3860 free(s->data);
3861 else if (s->should_munmap)
3862 munmap(s->data, s->size);
3864 if (s->should_free || s->should_munmap) {
3865 s->should_free = s->should_munmap = 0;
3866 s->data = NULL;
3870 void diff_free_filespec_data(struct diff_filespec *s)
3872 diff_free_filespec_blob(s);
3873 FREE_AND_NULL(s->cnt_data);
3876 static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
3877 void *blob,
3878 unsigned long size,
3879 const struct object_id *oid,
3880 int mode)
3882 struct strbuf buf = STRBUF_INIT;
3883 struct strbuf tempfile = STRBUF_INIT;
3884 char *path_dup = xstrdup(path);
3885 const char *base = basename(path_dup);
3887 /* Generate "XXXXXX_basename.ext" */
3888 strbuf_addstr(&tempfile, "XXXXXX_");
3889 strbuf_addstr(&tempfile, base);
3891 temp->tempfile = mks_tempfile_ts(tempfile.buf, strlen(base) + 1);
3892 if (!temp->tempfile)
3893 die_errno("unable to create temp-file");
3894 if (convert_to_working_tree(path,
3895 (const char *)blob, (size_t)size, &buf)) {
3896 blob = buf.buf;
3897 size = buf.len;
3899 if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
3900 close_tempfile_gently(temp->tempfile))
3901 die_errno("unable to write temp-file");
3902 temp->name = get_tempfile_path(temp->tempfile);
3903 oid_to_hex_r(temp->hex, oid);
3904 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", mode);
3905 strbuf_release(&buf);
3906 strbuf_release(&tempfile);
3907 free(path_dup);
3910 static struct diff_tempfile *prepare_temp_file(const char *name,
3911 struct diff_filespec *one)
3913 struct diff_tempfile *temp = claim_diff_tempfile();
3915 if (!DIFF_FILE_VALID(one)) {
3916 not_a_valid_file:
3917 /* A '-' entry produces this for file-2, and
3918 * a '+' entry produces this for file-1.
3920 temp->name = "/dev/null";
3921 xsnprintf(temp->hex, sizeof(temp->hex), ".");
3922 xsnprintf(temp->mode, sizeof(temp->mode), ".");
3923 return temp;
3926 if (!S_ISGITLINK(one->mode) &&
3927 (!one->oid_valid ||
3928 reuse_worktree_file(name, &one->oid, 1))) {
3929 struct stat st;
3930 if (lstat(name, &st) < 0) {
3931 if (errno == ENOENT)
3932 goto not_a_valid_file;
3933 die_errno("stat(%s)", name);
3935 if (S_ISLNK(st.st_mode)) {
3936 struct strbuf sb = STRBUF_INIT;
3937 if (strbuf_readlink(&sb, name, st.st_size) < 0)
3938 die_errno("readlink(%s)", name);
3939 prep_temp_blob(name, temp, sb.buf, sb.len,
3940 (one->oid_valid ?
3941 &one->oid : &null_oid),
3942 (one->oid_valid ?
3943 one->mode : S_IFLNK));
3944 strbuf_release(&sb);
3946 else {
3947 /* we can borrow from the file in the work tree */
3948 temp->name = name;
3949 if (!one->oid_valid)
3950 oid_to_hex_r(temp->hex, &null_oid);
3951 else
3952 oid_to_hex_r(temp->hex, &one->oid);
3953 /* Even though we may sometimes borrow the
3954 * contents from the work tree, we always want
3955 * one->mode. mode is trustworthy even when
3956 * !(one->oid_valid), as long as
3957 * DIFF_FILE_VALID(one).
3959 xsnprintf(temp->mode, sizeof(temp->mode), "%06o", one->mode);
3961 return temp;
3963 else {
3964 if (diff_populate_filespec(one, 0))
3965 die("cannot read data blob for %s", one->path);
3966 prep_temp_blob(name, temp, one->data, one->size,
3967 &one->oid, one->mode);
3969 return temp;
3972 static void add_external_diff_name(struct argv_array *argv,
3973 const char *name,
3974 struct diff_filespec *df)
3976 struct diff_tempfile *temp = prepare_temp_file(name, df);
3977 argv_array_push(argv, temp->name);
3978 argv_array_push(argv, temp->hex);
3979 argv_array_push(argv, temp->mode);
3982 /* An external diff command takes:
3984 * diff-cmd name infile1 infile1-sha1 infile1-mode \
3985 * infile2 infile2-sha1 infile2-mode [ rename-to ]
3988 static void run_external_diff(const char *pgm,
3989 const char *name,
3990 const char *other,
3991 struct diff_filespec *one,
3992 struct diff_filespec *two,
3993 const char *xfrm_msg,
3994 int complete_rewrite,
3995 struct diff_options *o)
3997 struct argv_array argv = ARGV_ARRAY_INIT;
3998 struct argv_array env = ARGV_ARRAY_INIT;
3999 struct diff_queue_struct *q = &diff_queued_diff;
4001 argv_array_push(&argv, pgm);
4002 argv_array_push(&argv, name);
4004 if (one && two) {
4005 add_external_diff_name(&argv, name, one);
4006 if (!other)
4007 add_external_diff_name(&argv, name, two);
4008 else {
4009 add_external_diff_name(&argv, other, two);
4010 argv_array_push(&argv, other);
4011 argv_array_push(&argv, xfrm_msg);
4015 argv_array_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
4016 argv_array_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
4018 if (run_command_v_opt_cd_env(argv.argv, RUN_USING_SHELL, NULL, env.argv))
4019 die(_("external diff died, stopping at %s"), name);
4021 remove_tempfile();
4022 argv_array_clear(&argv);
4023 argv_array_clear(&env);
4026 static int similarity_index(struct diff_filepair *p)
4028 return p->score * 100 / MAX_SCORE;
4031 static const char *diff_abbrev_oid(const struct object_id *oid, int abbrev)
4033 if (startup_info->have_repository)
4034 return find_unique_abbrev(oid, abbrev);
4035 else {
4036 char *hex = oid_to_hex(oid);
4037 if (abbrev < 0)
4038 abbrev = FALLBACK_DEFAULT_ABBREV;
4039 if (abbrev > GIT_SHA1_HEXSZ)
4040 die("BUG: oid abbreviation out of range: %d", abbrev);
4041 if (abbrev)
4042 hex[abbrev] = '\0';
4043 return hex;
4047 static void fill_metainfo(struct strbuf *msg,
4048 const char *name,
4049 const char *other,
4050 struct diff_filespec *one,
4051 struct diff_filespec *two,
4052 struct diff_options *o,
4053 struct diff_filepair *p,
4054 int *must_show_header,
4055 int use_color)
4057 const char *set = diff_get_color(use_color, DIFF_METAINFO);
4058 const char *reset = diff_get_color(use_color, DIFF_RESET);
4059 const char *line_prefix = diff_line_prefix(o);
4061 *must_show_header = 1;
4062 strbuf_init(msg, PATH_MAX * 2 + 300);
4063 switch (p->status) {
4064 case DIFF_STATUS_COPIED:
4065 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4066 line_prefix, set, similarity_index(p));
4067 strbuf_addf(msg, "%s\n%s%scopy from ",
4068 reset, line_prefix, set);
4069 quote_c_style(name, msg, NULL, 0);
4070 strbuf_addf(msg, "%s\n%s%scopy to ", reset, line_prefix, set);
4071 quote_c_style(other, msg, NULL, 0);
4072 strbuf_addf(msg, "%s\n", reset);
4073 break;
4074 case DIFF_STATUS_RENAMED:
4075 strbuf_addf(msg, "%s%ssimilarity index %d%%",
4076 line_prefix, set, similarity_index(p));
4077 strbuf_addf(msg, "%s\n%s%srename from ",
4078 reset, line_prefix, set);
4079 quote_c_style(name, msg, NULL, 0);
4080 strbuf_addf(msg, "%s\n%s%srename to ",
4081 reset, line_prefix, set);
4082 quote_c_style(other, msg, NULL, 0);
4083 strbuf_addf(msg, "%s\n", reset);
4084 break;
4085 case DIFF_STATUS_MODIFIED:
4086 if (p->score) {
4087 strbuf_addf(msg, "%s%sdissimilarity index %d%%%s\n",
4088 line_prefix,
4089 set, similarity_index(p), reset);
4090 break;
4092 /* fallthru */
4093 default:
4094 *must_show_header = 0;
4096 if (one && two && oidcmp(&one->oid, &two->oid)) {
4097 int abbrev = o->flags.full_index ? 40 : DEFAULT_ABBREV;
4099 if (o->flags.binary) {
4100 mmfile_t mf;
4101 if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) ||
4102 (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
4103 abbrev = 40;
4105 strbuf_addf(msg, "%s%sindex %s..%s", line_prefix, set,
4106 diff_abbrev_oid(&one->oid, abbrev),
4107 diff_abbrev_oid(&two->oid, abbrev));
4108 if (one->mode == two->mode)
4109 strbuf_addf(msg, " %06o", one->mode);
4110 strbuf_addf(msg, "%s\n", reset);
4114 static void run_diff_cmd(const char *pgm,
4115 const char *name,
4116 const char *other,
4117 const char *attr_path,
4118 struct diff_filespec *one,
4119 struct diff_filespec *two,
4120 struct strbuf *msg,
4121 struct diff_options *o,
4122 struct diff_filepair *p)
4124 const char *xfrm_msg = NULL;
4125 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
4126 int must_show_header = 0;
4129 if (o->flags.allow_external) {
4130 struct userdiff_driver *drv = userdiff_find_by_path(attr_path);
4131 if (drv && drv->external)
4132 pgm = drv->external;
4135 if (msg) {
4137 * don't use colors when the header is intended for an
4138 * external diff driver
4140 fill_metainfo(msg, name, other, one, two, o, p,
4141 &must_show_header,
4142 want_color(o->use_color) && !pgm);
4143 xfrm_msg = msg->len ? msg->buf : NULL;
4146 if (pgm) {
4147 run_external_diff(pgm, name, other, one, two, xfrm_msg,
4148 complete_rewrite, o);
4149 return;
4151 if (one && two)
4152 builtin_diff(name, other ? other : name,
4153 one, two, xfrm_msg, must_show_header,
4154 o, complete_rewrite);
4155 else
4156 fprintf(o->file, "* Unmerged path %s\n", name);
4159 static void diff_fill_oid_info(struct diff_filespec *one)
4161 if (DIFF_FILE_VALID(one)) {
4162 if (!one->oid_valid) {
4163 struct stat st;
4164 if (one->is_stdin) {
4165 oidclr(&one->oid);
4166 return;
4168 if (lstat(one->path, &st) < 0)
4169 die_errno("stat '%s'", one->path);
4170 if (index_path(&one->oid, one->path, &st, 0))
4171 die("cannot hash %s", one->path);
4174 else
4175 oidclr(&one->oid);
4178 static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
4180 /* Strip the prefix but do not molest /dev/null and absolute paths */
4181 if (*namep && **namep != '/') {
4182 *namep += prefix_length;
4183 if (**namep == '/')
4184 ++*namep;
4186 if (*otherp && **otherp != '/') {
4187 *otherp += prefix_length;
4188 if (**otherp == '/')
4189 ++*otherp;
4193 static void run_diff(struct diff_filepair *p, struct diff_options *o)
4195 const char *pgm = external_diff();
4196 struct strbuf msg;
4197 struct diff_filespec *one = p->one;
4198 struct diff_filespec *two = p->two;
4199 const char *name;
4200 const char *other;
4201 const char *attr_path;
4203 name = one->path;
4204 other = (strcmp(name, two->path) ? two->path : NULL);
4205 attr_path = name;
4206 if (o->prefix_length)
4207 strip_prefix(o->prefix_length, &name, &other);
4209 if (!o->flags.allow_external)
4210 pgm = NULL;
4212 if (DIFF_PAIR_UNMERGED(p)) {
4213 run_diff_cmd(pgm, name, NULL, attr_path,
4214 NULL, NULL, NULL, o, p);
4215 return;
4218 diff_fill_oid_info(one);
4219 diff_fill_oid_info(two);
4221 if (!pgm &&
4222 DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
4223 (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
4225 * a filepair that changes between file and symlink
4226 * needs to be split into deletion and creation.
4228 struct diff_filespec *null = alloc_filespec(two->path);
4229 run_diff_cmd(NULL, name, other, attr_path,
4230 one, null, &msg, o, p);
4231 free(null);
4232 strbuf_release(&msg);
4234 null = alloc_filespec(one->path);
4235 run_diff_cmd(NULL, name, other, attr_path,
4236 null, two, &msg, o, p);
4237 free(null);
4239 else
4240 run_diff_cmd(pgm, name, other, attr_path,
4241 one, two, &msg, o, p);
4243 strbuf_release(&msg);
4246 static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
4247 struct diffstat_t *diffstat)
4249 const char *name;
4250 const char *other;
4252 if (DIFF_PAIR_UNMERGED(p)) {
4253 /* unmerged */
4254 builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, p);
4255 return;
4258 name = p->one->path;
4259 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
4261 if (o->prefix_length)
4262 strip_prefix(o->prefix_length, &name, &other);
4264 diff_fill_oid_info(p->one);
4265 diff_fill_oid_info(p->two);
4267 builtin_diffstat(name, other, p->one, p->two, diffstat, o, p);
4270 static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
4272 const char *name;
4273 const char *other;
4274 const char *attr_path;
4276 if (DIFF_PAIR_UNMERGED(p)) {
4277 /* unmerged */
4278 return;
4281 name = p->one->path;
4282 other = (strcmp(name, p->two->path) ? p->two->path : NULL);
4283 attr_path = other ? other : name;
4285 if (o->prefix_length)
4286 strip_prefix(o->prefix_length, &name, &other);
4288 diff_fill_oid_info(p->one);
4289 diff_fill_oid_info(p->two);
4291 builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
4294 void diff_setup(struct diff_options *options)
4296 memcpy(options, &default_diff_options, sizeof(*options));
4298 options->file = stdout;
4300 options->abbrev = DEFAULT_ABBREV;
4301 options->line_termination = '\n';
4302 options->break_opt = -1;
4303 options->rename_limit = -1;
4304 options->dirstat_permille = diff_dirstat_permille_default;
4305 options->context = diff_context_default;
4306 options->interhunkcontext = diff_interhunk_context_default;
4307 options->ws_error_highlight = ws_error_highlight_default;
4308 options->flags.rename_empty = 1;
4309 options->objfind = NULL;
4311 /* pathchange left =NULL by default */
4312 options->change = diff_change;
4313 options->add_remove = diff_addremove;
4314 options->use_color = diff_use_color_default;
4315 options->detect_rename = diff_detect_rename_default;
4316 options->xdl_opts |= diff_algorithm;
4317 if (diff_indent_heuristic)
4318 DIFF_XDL_SET(options, INDENT_HEURISTIC);
4320 options->orderfile = diff_order_file_cfg;
4322 if (diff_no_prefix) {
4323 options->a_prefix = options->b_prefix = "";
4324 } else if (!diff_mnemonic_prefix) {
4325 options->a_prefix = "a/";
4326 options->b_prefix = "b/";
4329 options->color_moved = diff_color_moved_default;
4332 void diff_setup_done(struct diff_options *options)
4334 unsigned check_mask = DIFF_FORMAT_NAME |
4335 DIFF_FORMAT_NAME_STATUS |
4336 DIFF_FORMAT_CHECKDIFF |
4337 DIFF_FORMAT_NO_OUTPUT;
4339 if (options->set_default)
4340 options->set_default(options);
4342 if (HAS_MULTI_BITS(options->output_format & check_mask))
4343 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
4345 if (HAS_MULTI_BITS(options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK))
4346 die(_("-G, -S and --find-object are mutually exclusive"));
4349 * Most of the time we can say "there are changes"
4350 * only by checking if there are changed paths, but
4351 * --ignore-whitespace* options force us to look
4352 * inside contents.
4355 if ((options->xdl_opts & XDF_WHITESPACE_FLAGS))
4356 options->flags.diff_from_contents = 1;
4357 else
4358 options->flags.diff_from_contents = 0;
4360 if (options->flags.find_copies_harder)
4361 options->detect_rename = DIFF_DETECT_COPY;
4363 if (!options->flags.relative_name)
4364 options->prefix = NULL;
4365 if (options->prefix)
4366 options->prefix_length = strlen(options->prefix);
4367 else
4368 options->prefix_length = 0;
4370 if (options->output_format & (DIFF_FORMAT_NAME |
4371 DIFF_FORMAT_NAME_STATUS |
4372 DIFF_FORMAT_CHECKDIFF |
4373 DIFF_FORMAT_NO_OUTPUT))
4374 options->output_format &= ~(DIFF_FORMAT_RAW |
4375 DIFF_FORMAT_NUMSTAT |
4376 DIFF_FORMAT_DIFFSTAT |
4377 DIFF_FORMAT_SHORTSTAT |
4378 DIFF_FORMAT_DIRSTAT |
4379 DIFF_FORMAT_SUMMARY |
4380 DIFF_FORMAT_PATCH);
4383 * These cases always need recursive; we do not drop caller-supplied
4384 * recursive bits for other formats here.
4386 if (options->output_format & (DIFF_FORMAT_PATCH |
4387 DIFF_FORMAT_NUMSTAT |
4388 DIFF_FORMAT_DIFFSTAT |
4389 DIFF_FORMAT_SHORTSTAT |
4390 DIFF_FORMAT_DIRSTAT |
4391 DIFF_FORMAT_SUMMARY |
4392 DIFF_FORMAT_CHECKDIFF))
4393 options->flags.recursive = 1;
4395 * Also pickaxe would not work very well if you do not say recursive
4397 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
4398 options->flags.recursive = 1;
4400 * When patches are generated, submodules diffed against the work tree
4401 * must be checked for dirtiness too so it can be shown in the output
4403 if (options->output_format & DIFF_FORMAT_PATCH)
4404 options->flags.dirty_submodules = 1;
4406 if (options->detect_rename && options->rename_limit < 0)
4407 options->rename_limit = diff_rename_limit_default;
4408 if (options->setup & DIFF_SETUP_USE_CACHE) {
4409 if (!active_cache)
4410 /* read-cache does not die even when it fails
4411 * so it is safe for us to do this here. Also
4412 * it does not smudge active_cache or active_nr
4413 * when it fails, so we do not have to worry about
4414 * cleaning it up ourselves either.
4416 read_cache();
4418 if (40 < options->abbrev)
4419 options->abbrev = 40; /* full */
4422 * It does not make sense to show the first hit we happened
4423 * to have found. It does not make sense not to return with
4424 * exit code in such a case either.
4426 if (options->flags.quick) {
4427 options->output_format = DIFF_FORMAT_NO_OUTPUT;
4428 options->flags.exit_with_status = 1;
4431 options->diff_path_counter = 0;
4433 if (options->flags.follow_renames && options->pathspec.nr != 1)
4434 die(_("--follow requires exactly one pathspec"));
4436 if (!options->use_color || external_diff())
4437 options->color_moved = 0;
4440 static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
4442 char c, *eq;
4443 int len;
4445 if (*arg != '-')
4446 return 0;
4447 c = *++arg;
4448 if (!c)
4449 return 0;
4450 if (c == arg_short) {
4451 c = *++arg;
4452 if (!c)
4453 return 1;
4454 if (val && isdigit(c)) {
4455 char *end;
4456 int n = strtoul(arg, &end, 10);
4457 if (*end)
4458 return 0;
4459 *val = n;
4460 return 1;
4462 return 0;
4464 if (c != '-')
4465 return 0;
4466 arg++;
4467 eq = strchrnul(arg, '=');
4468 len = eq - arg;
4469 if (!len || strncmp(arg, arg_long, len))
4470 return 0;
4471 if (*eq) {
4472 int n;
4473 char *end;
4474 if (!isdigit(*++eq))
4475 return 0;
4476 n = strtoul(eq, &end, 10);
4477 if (*end)
4478 return 0;
4479 *val = n;
4481 return 1;
4484 static int diff_scoreopt_parse(const char *opt);
4486 static inline int short_opt(char opt, const char **argv,
4487 const char **optarg)
4489 const char *arg = argv[0];
4490 if (arg[0] != '-' || arg[1] != opt)
4491 return 0;
4492 if (arg[2] != '\0') {
4493 *optarg = arg + 2;
4494 return 1;
4496 if (!argv[1])
4497 die("Option '%c' requires a value", opt);
4498 *optarg = argv[1];
4499 return 2;
4502 int parse_long_opt(const char *opt, const char **argv,
4503 const char **optarg)
4505 const char *arg = argv[0];
4506 if (!skip_prefix(arg, "--", &arg))
4507 return 0;
4508 if (!skip_prefix(arg, opt, &arg))
4509 return 0;
4510 if (*arg == '=') { /* stuck form: --option=value */
4511 *optarg = arg + 1;
4512 return 1;
4514 if (*arg != '\0')
4515 return 0;
4516 /* separate form: --option value */
4517 if (!argv[1])
4518 die("Option '--%s' requires a value", opt);
4519 *optarg = argv[1];
4520 return 2;
4523 static int stat_opt(struct diff_options *options, const char **av)
4525 const char *arg = av[0];
4526 char *end;
4527 int width = options->stat_width;
4528 int name_width = options->stat_name_width;
4529 int graph_width = options->stat_graph_width;
4530 int count = options->stat_count;
4531 int argcount = 1;
4533 if (!skip_prefix(arg, "--stat", &arg))
4534 die("BUG: stat option does not begin with --stat: %s", arg);
4535 end = (char *)arg;
4537 switch (*arg) {
4538 case '-':
4539 if (skip_prefix(arg, "-width", &arg)) {
4540 if (*arg == '=')
4541 width = strtoul(arg + 1, &end, 10);
4542 else if (!*arg && !av[1])
4543 die_want_option("--stat-width");
4544 else if (!*arg) {
4545 width = strtoul(av[1], &end, 10);
4546 argcount = 2;
4548 } else if (skip_prefix(arg, "-name-width", &arg)) {
4549 if (*arg == '=')
4550 name_width = strtoul(arg + 1, &end, 10);
4551 else if (!*arg && !av[1])
4552 die_want_option("--stat-name-width");
4553 else if (!*arg) {
4554 name_width = strtoul(av[1], &end, 10);
4555 argcount = 2;
4557 } else if (skip_prefix(arg, "-graph-width", &arg)) {
4558 if (*arg == '=')
4559 graph_width = strtoul(arg + 1, &end, 10);
4560 else if (!*arg && !av[1])
4561 die_want_option("--stat-graph-width");
4562 else if (!*arg) {
4563 graph_width = strtoul(av[1], &end, 10);
4564 argcount = 2;
4566 } else if (skip_prefix(arg, "-count", &arg)) {
4567 if (*arg == '=')
4568 count = strtoul(arg + 1, &end, 10);
4569 else if (!*arg && !av[1])
4570 die_want_option("--stat-count");
4571 else if (!*arg) {
4572 count = strtoul(av[1], &end, 10);
4573 argcount = 2;
4576 break;
4577 case '=':
4578 width = strtoul(arg+1, &end, 10);
4579 if (*end == ',')
4580 name_width = strtoul(end+1, &end, 10);
4581 if (*end == ',')
4582 count = strtoul(end+1, &end, 10);
4585 /* Important! This checks all the error cases! */
4586 if (*end)
4587 return 0;
4588 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4589 options->stat_name_width = name_width;
4590 options->stat_graph_width = graph_width;
4591 options->stat_width = width;
4592 options->stat_count = count;
4593 return argcount;
4596 static int parse_dirstat_opt(struct diff_options *options, const char *params)
4598 struct strbuf errmsg = STRBUF_INIT;
4599 if (parse_dirstat_params(options, params, &errmsg))
4600 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4601 errmsg.buf);
4602 strbuf_release(&errmsg);
4604 * The caller knows a dirstat-related option is given from the command
4605 * line; allow it to say "return this_function();"
4607 options->output_format |= DIFF_FORMAT_DIRSTAT;
4608 return 1;
4611 static int parse_submodule_opt(struct diff_options *options, const char *value)
4613 if (parse_submodule_params(options, value))
4614 die(_("Failed to parse --submodule option parameter: '%s'"),
4615 value);
4616 return 1;
4619 static const char diff_status_letters[] = {
4620 DIFF_STATUS_ADDED,
4621 DIFF_STATUS_COPIED,
4622 DIFF_STATUS_DELETED,
4623 DIFF_STATUS_MODIFIED,
4624 DIFF_STATUS_RENAMED,
4625 DIFF_STATUS_TYPE_CHANGED,
4626 DIFF_STATUS_UNKNOWN,
4627 DIFF_STATUS_UNMERGED,
4628 DIFF_STATUS_FILTER_AON,
4629 DIFF_STATUS_FILTER_BROKEN,
4630 '\0',
4633 static unsigned int filter_bit['Z' + 1];
4635 static void prepare_filter_bits(void)
4637 int i;
4639 if (!filter_bit[DIFF_STATUS_ADDED]) {
4640 for (i = 0; diff_status_letters[i]; i++)
4641 filter_bit[(int) diff_status_letters[i]] = (1 << i);
4645 static unsigned filter_bit_tst(char status, const struct diff_options *opt)
4647 return opt->filter & filter_bit[(int) status];
4650 static int parse_diff_filter_opt(const char *optarg, struct diff_options *opt)
4652 int i, optch;
4654 prepare_filter_bits();
4657 * If there is a negation e.g. 'd' in the input, and we haven't
4658 * initialized the filter field with another --diff-filter, start
4659 * from full set of bits, except for AON.
4661 if (!opt->filter) {
4662 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4663 if (optch < 'a' || 'z' < optch)
4664 continue;
4665 opt->filter = (1 << (ARRAY_SIZE(diff_status_letters) - 1)) - 1;
4666 opt->filter &= ~filter_bit[DIFF_STATUS_FILTER_AON];
4667 break;
4671 for (i = 0; (optch = optarg[i]) != '\0'; i++) {
4672 unsigned int bit;
4673 int negate;
4675 if ('a' <= optch && optch <= 'z') {
4676 negate = 1;
4677 optch = toupper(optch);
4678 } else {
4679 negate = 0;
4682 bit = (0 <= optch && optch <= 'Z') ? filter_bit[optch] : 0;
4683 if (!bit)
4684 return optarg[i];
4685 if (negate)
4686 opt->filter &= ~bit;
4687 else
4688 opt->filter |= bit;
4690 return 0;
4693 static void enable_patch_output(int *fmt) {
4694 *fmt &= ~DIFF_FORMAT_NO_OUTPUT;
4695 *fmt |= DIFF_FORMAT_PATCH;
4698 static int parse_ws_error_highlight_opt(struct diff_options *opt, const char *arg)
4700 int val = parse_ws_error_highlight(arg);
4702 if (val < 0) {
4703 error("unknown value after ws-error-highlight=%.*s",
4704 -1 - val, arg);
4705 return 0;
4707 opt->ws_error_highlight = val;
4708 return 1;
4711 static int parse_objfind_opt(struct diff_options *opt, const char *arg)
4713 struct object_id oid;
4715 if (get_oid(arg, &oid))
4716 return error("unable to resolve '%s'", arg);
4718 if (!opt->objfind)
4719 opt->objfind = xcalloc(1, sizeof(*opt->objfind));
4721 opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
4722 opt->flags.recursive = 1;
4723 opt->flags.tree_in_recursive = 1;
4724 oidset_insert(opt->objfind, &oid);
4725 return 1;
4728 int diff_opt_parse(struct diff_options *options,
4729 const char **av, int ac, const char *prefix)
4731 const char *arg = av[0];
4732 const char *optarg;
4733 int argcount;
4735 if (!prefix)
4736 prefix = "";
4738 /* Output format options */
4739 if (!strcmp(arg, "-p") || !strcmp(arg, "-u") || !strcmp(arg, "--patch")
4740 || opt_arg(arg, 'U', "unified", &options->context))
4741 enable_patch_output(&options->output_format);
4742 else if (!strcmp(arg, "--raw"))
4743 options->output_format |= DIFF_FORMAT_RAW;
4744 else if (!strcmp(arg, "--patch-with-raw")) {
4745 enable_patch_output(&options->output_format);
4746 options->output_format |= DIFF_FORMAT_RAW;
4747 } else if (!strcmp(arg, "--numstat"))
4748 options->output_format |= DIFF_FORMAT_NUMSTAT;
4749 else if (!strcmp(arg, "--shortstat"))
4750 options->output_format |= DIFF_FORMAT_SHORTSTAT;
4751 else if (skip_prefix(arg, "-X", &arg) ||
4752 skip_to_optional_arg(arg, "--dirstat", &arg))
4753 return parse_dirstat_opt(options, arg);
4754 else if (!strcmp(arg, "--cumulative"))
4755 return parse_dirstat_opt(options, "cumulative");
4756 else if (skip_to_optional_arg(arg, "--dirstat-by-file", &arg)) {
4757 parse_dirstat_opt(options, "files");
4758 return parse_dirstat_opt(options, arg);
4760 else if (!strcmp(arg, "--check"))
4761 options->output_format |= DIFF_FORMAT_CHECKDIFF;
4762 else if (!strcmp(arg, "--summary"))
4763 options->output_format |= DIFF_FORMAT_SUMMARY;
4764 else if (!strcmp(arg, "--patch-with-stat")) {
4765 enable_patch_output(&options->output_format);
4766 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4767 } else if (!strcmp(arg, "--name-only"))
4768 options->output_format |= DIFF_FORMAT_NAME;
4769 else if (!strcmp(arg, "--name-status"))
4770 options->output_format |= DIFF_FORMAT_NAME_STATUS;
4771 else if (!strcmp(arg, "-s") || !strcmp(arg, "--no-patch"))
4772 options->output_format |= DIFF_FORMAT_NO_OUTPUT;
4773 else if (starts_with(arg, "--stat"))
4774 /* --stat, --stat-width, --stat-name-width, or --stat-count */
4775 return stat_opt(options, av);
4776 else if (!strcmp(arg, "--compact-summary")) {
4777 options->flags.stat_with_summary = 1;
4778 options->output_format |= DIFF_FORMAT_DIFFSTAT;
4779 } else if (!strcmp(arg, "--no-compact-summary"))
4780 options->flags.stat_with_summary = 0;
4782 /* renames options */
4783 else if (starts_with(arg, "-B") ||
4784 skip_to_optional_arg(arg, "--break-rewrites", NULL)) {
4785 if ((options->break_opt = diff_scoreopt_parse(arg)) == -1)
4786 return error("invalid argument to -B: %s", arg+2);
4788 else if (starts_with(arg, "-M") ||
4789 skip_to_optional_arg(arg, "--find-renames", NULL)) {
4790 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
4791 return error("invalid argument to -M: %s", arg+2);
4792 options->detect_rename = DIFF_DETECT_RENAME;
4794 else if (!strcmp(arg, "-D") || !strcmp(arg, "--irreversible-delete")) {
4795 options->irreversible_delete = 1;
4797 else if (starts_with(arg, "-C") ||
4798 skip_to_optional_arg(arg, "--find-copies", NULL)) {
4799 if (options->detect_rename == DIFF_DETECT_COPY)
4800 options->flags.find_copies_harder = 1;
4801 if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
4802 return error("invalid argument to -C: %s", arg+2);
4803 options->detect_rename = DIFF_DETECT_COPY;
4805 else if (!strcmp(arg, "--no-renames"))
4806 options->detect_rename = 0;
4807 else if (!strcmp(arg, "--rename-empty"))
4808 options->flags.rename_empty = 1;
4809 else if (!strcmp(arg, "--no-rename-empty"))
4810 options->flags.rename_empty = 0;
4811 else if (skip_to_optional_arg_default(arg, "--relative", &arg, NULL)) {
4812 options->flags.relative_name = 1;
4813 if (arg)
4814 options->prefix = arg;
4817 /* xdiff options */
4818 else if (!strcmp(arg, "--minimal"))
4819 DIFF_XDL_SET(options, NEED_MINIMAL);
4820 else if (!strcmp(arg, "--no-minimal"))
4821 DIFF_XDL_CLR(options, NEED_MINIMAL);
4822 else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
4823 DIFF_XDL_SET(options, IGNORE_WHITESPACE);
4824 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
4825 DIFF_XDL_SET(options, IGNORE_WHITESPACE_CHANGE);
4826 else if (!strcmp(arg, "--ignore-space-at-eol"))
4827 DIFF_XDL_SET(options, IGNORE_WHITESPACE_AT_EOL);
4828 else if (!strcmp(arg, "--ignore-cr-at-eol"))
4829 DIFF_XDL_SET(options, IGNORE_CR_AT_EOL);
4830 else if (!strcmp(arg, "--ignore-blank-lines"))
4831 DIFF_XDL_SET(options, IGNORE_BLANK_LINES);
4832 else if (!strcmp(arg, "--indent-heuristic"))
4833 DIFF_XDL_SET(options, INDENT_HEURISTIC);
4834 else if (!strcmp(arg, "--no-indent-heuristic"))
4835 DIFF_XDL_CLR(options, INDENT_HEURISTIC);
4836 else if (!strcmp(arg, "--patience")) {
4837 int i;
4838 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
4840 * Both --patience and --anchored use PATIENCE_DIFF
4841 * internally, so remove any anchors previously
4842 * specified.
4844 for (i = 0; i < options->anchors_nr; i++)
4845 free(options->anchors[i]);
4846 options->anchors_nr = 0;
4847 } else if (!strcmp(arg, "--histogram"))
4848 options->xdl_opts = DIFF_WITH_ALG(options, HISTOGRAM_DIFF);
4849 else if ((argcount = parse_long_opt("diff-algorithm", av, &optarg))) {
4850 long value = parse_algorithm_value(optarg);
4851 if (value < 0)
4852 return error("option diff-algorithm accepts \"myers\", "
4853 "\"minimal\", \"patience\" and \"histogram\"");
4854 /* clear out previous settings */
4855 DIFF_XDL_CLR(options, NEED_MINIMAL);
4856 options->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
4857 options->xdl_opts |= value;
4858 return argcount;
4859 } else if (skip_prefix(arg, "--anchored=", &arg)) {
4860 options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
4861 ALLOC_GROW(options->anchors, options->anchors_nr + 1,
4862 options->anchors_alloc);
4863 options->anchors[options->anchors_nr++] = xstrdup(arg);
4866 /* flags options */
4867 else if (!strcmp(arg, "--binary")) {
4868 enable_patch_output(&options->output_format);
4869 options->flags.binary = 1;
4871 else if (!strcmp(arg, "--full-index"))
4872 options->flags.full_index = 1;
4873 else if (!strcmp(arg, "-a") || !strcmp(arg, "--text"))
4874 options->flags.text = 1;
4875 else if (!strcmp(arg, "-R"))
4876 options->flags.reverse_diff = 1;
4877 else if (!strcmp(arg, "--find-copies-harder"))
4878 options->flags.find_copies_harder = 1;
4879 else if (!strcmp(arg, "--follow"))
4880 options->flags.follow_renames = 1;
4881 else if (!strcmp(arg, "--no-follow")) {
4882 options->flags.follow_renames = 0;
4883 options->flags.default_follow_renames = 0;
4884 } else if (skip_to_optional_arg_default(arg, "--color", &arg, "always")) {
4885 int value = git_config_colorbool(NULL, arg);
4886 if (value < 0)
4887 return error("option `color' expects \"always\", \"auto\", or \"never\"");
4888 options->use_color = value;
4890 else if (!strcmp(arg, "--no-color"))
4891 options->use_color = 0;
4892 else if (!strcmp(arg, "--color-moved")) {
4893 if (diff_color_moved_default)
4894 options->color_moved = diff_color_moved_default;
4895 if (options->color_moved == COLOR_MOVED_NO)
4896 options->color_moved = COLOR_MOVED_DEFAULT;
4897 } else if (!strcmp(arg, "--no-color-moved"))
4898 options->color_moved = COLOR_MOVED_NO;
4899 else if (skip_prefix(arg, "--color-moved=", &arg)) {
4900 int cm = parse_color_moved(arg);
4901 if (cm < 0)
4902 die("bad --color-moved argument: %s", arg);
4903 options->color_moved = cm;
4904 } else if (skip_prefix(arg, "--color-moved-ws=", &arg)) {
4905 options->color_moved_ws_handling = parse_color_moved_ws(arg);
4906 } else if (skip_to_optional_arg_default(arg, "--color-words", &options->word_regex, NULL)) {
4907 options->use_color = 1;
4908 options->word_diff = DIFF_WORDS_COLOR;
4910 else if (!strcmp(arg, "--word-diff")) {
4911 if (options->word_diff == DIFF_WORDS_NONE)
4912 options->word_diff = DIFF_WORDS_PLAIN;
4914 else if (skip_prefix(arg, "--word-diff=", &arg)) {
4915 if (!strcmp(arg, "plain"))
4916 options->word_diff = DIFF_WORDS_PLAIN;
4917 else if (!strcmp(arg, "color")) {
4918 options->use_color = 1;
4919 options->word_diff = DIFF_WORDS_COLOR;
4921 else if (!strcmp(arg, "porcelain"))
4922 options->word_diff = DIFF_WORDS_PORCELAIN;
4923 else if (!strcmp(arg, "none"))
4924 options->word_diff = DIFF_WORDS_NONE;
4925 else
4926 die("bad --word-diff argument: %s", arg);
4928 else if ((argcount = parse_long_opt("word-diff-regex", av, &optarg))) {
4929 if (options->word_diff == DIFF_WORDS_NONE)
4930 options->word_diff = DIFF_WORDS_PLAIN;
4931 options->word_regex = optarg;
4932 return argcount;
4934 else if (!strcmp(arg, "--exit-code"))
4935 options->flags.exit_with_status = 1;
4936 else if (!strcmp(arg, "--quiet"))
4937 options->flags.quick = 1;
4938 else if (!strcmp(arg, "--ext-diff"))
4939 options->flags.allow_external = 1;
4940 else if (!strcmp(arg, "--no-ext-diff"))
4941 options->flags.allow_external = 0;
4942 else if (!strcmp(arg, "--textconv")) {
4943 options->flags.allow_textconv = 1;
4944 options->flags.textconv_set_via_cmdline = 1;
4945 } else if (!strcmp(arg, "--no-textconv"))
4946 options->flags.allow_textconv = 0;
4947 else if (skip_to_optional_arg_default(arg, "--ignore-submodules", &arg, "all")) {
4948 options->flags.override_submodule_config = 1;
4949 handle_ignore_submodules_arg(options, arg);
4950 } else if (skip_to_optional_arg_default(arg, "--submodule", &arg, "log"))
4951 return parse_submodule_opt(options, arg);
4952 else if (skip_prefix(arg, "--ws-error-highlight=", &arg))
4953 return parse_ws_error_highlight_opt(options, arg);
4954 else if (!strcmp(arg, "--ita-invisible-in-index"))
4955 options->ita_invisible_in_index = 1;
4956 else if (!strcmp(arg, "--ita-visible-in-index"))
4957 options->ita_invisible_in_index = 0;
4959 /* misc options */
4960 else if (!strcmp(arg, "-z"))
4961 options->line_termination = 0;
4962 else if ((argcount = short_opt('l', av, &optarg))) {
4963 options->rename_limit = strtoul(optarg, NULL, 10);
4964 return argcount;
4966 else if ((argcount = short_opt('S', av, &optarg))) {
4967 options->pickaxe = optarg;
4968 options->pickaxe_opts |= DIFF_PICKAXE_KIND_S;
4969 return argcount;
4970 } else if ((argcount = short_opt('G', av, &optarg))) {
4971 options->pickaxe = optarg;
4972 options->pickaxe_opts |= DIFF_PICKAXE_KIND_G;
4973 return argcount;
4975 else if (!strcmp(arg, "--pickaxe-all"))
4976 options->pickaxe_opts |= DIFF_PICKAXE_ALL;
4977 else if (!strcmp(arg, "--pickaxe-regex"))
4978 options->pickaxe_opts |= DIFF_PICKAXE_REGEX;
4979 else if ((argcount = short_opt('O', av, &optarg))) {
4980 options->orderfile = prefix_filename(prefix, optarg);
4981 return argcount;
4982 } else if (skip_prefix(arg, "--find-object=", &arg))
4983 return parse_objfind_opt(options, arg);
4984 else if ((argcount = parse_long_opt("diff-filter", av, &optarg))) {
4985 int offending = parse_diff_filter_opt(optarg, options);
4986 if (offending)
4987 die("unknown change class '%c' in --diff-filter=%s",
4988 offending, optarg);
4989 return argcount;
4991 else if (!strcmp(arg, "--no-abbrev"))
4992 options->abbrev = 0;
4993 else if (!strcmp(arg, "--abbrev"))
4994 options->abbrev = DEFAULT_ABBREV;
4995 else if (skip_prefix(arg, "--abbrev=", &arg)) {
4996 options->abbrev = strtoul(arg, NULL, 10);
4997 if (options->abbrev < MINIMUM_ABBREV)
4998 options->abbrev = MINIMUM_ABBREV;
4999 else if (40 < options->abbrev)
5000 options->abbrev = 40;
5002 else if ((argcount = parse_long_opt("src-prefix", av, &optarg))) {
5003 options->a_prefix = optarg;
5004 return argcount;
5006 else if ((argcount = parse_long_opt("line-prefix", av, &optarg))) {
5007 options->line_prefix = optarg;
5008 options->line_prefix_length = strlen(options->line_prefix);
5009 graph_setup_line_prefix(options);
5010 return argcount;
5012 else if ((argcount = parse_long_opt("dst-prefix", av, &optarg))) {
5013 options->b_prefix = optarg;
5014 return argcount;
5016 else if (!strcmp(arg, "--no-prefix"))
5017 options->a_prefix = options->b_prefix = "";
5018 else if (opt_arg(arg, '\0', "inter-hunk-context",
5019 &options->interhunkcontext))
5021 else if (!strcmp(arg, "-W"))
5022 options->flags.funccontext = 1;
5023 else if (!strcmp(arg, "--function-context"))
5024 options->flags.funccontext = 1;
5025 else if (!strcmp(arg, "--no-function-context"))
5026 options->flags.funccontext = 0;
5027 else if ((argcount = parse_long_opt("output", av, &optarg))) {
5028 char *path = prefix_filename(prefix, optarg);
5029 options->file = xfopen(path, "w");
5030 options->close_file = 1;
5031 if (options->use_color != GIT_COLOR_ALWAYS)
5032 options->use_color = GIT_COLOR_NEVER;
5033 free(path);
5034 return argcount;
5035 } else
5036 return 0;
5037 return 1;
5040 int parse_rename_score(const char **cp_p)
5042 unsigned long num, scale;
5043 int ch, dot;
5044 const char *cp = *cp_p;
5046 num = 0;
5047 scale = 1;
5048 dot = 0;
5049 for (;;) {
5050 ch = *cp;
5051 if ( !dot && ch == '.' ) {
5052 scale = 1;
5053 dot = 1;
5054 } else if ( ch == '%' ) {
5055 scale = dot ? scale*100 : 100;
5056 cp++; /* % is always at the end */
5057 break;
5058 } else if ( ch >= '0' && ch <= '9' ) {
5059 if ( scale < 100000 ) {
5060 scale *= 10;
5061 num = (num*10) + (ch-'0');
5063 } else {
5064 break;
5066 cp++;
5068 *cp_p = cp;
5070 /* user says num divided by scale and we say internally that
5071 * is MAX_SCORE * num / scale.
5073 return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
5076 static int diff_scoreopt_parse(const char *opt)
5078 int opt1, opt2, cmd;
5080 if (*opt++ != '-')
5081 return -1;
5082 cmd = *opt++;
5083 if (cmd == '-') {
5084 /* convert the long-form arguments into short-form versions */
5085 if (skip_prefix(opt, "break-rewrites", &opt)) {
5086 if (*opt == 0 || *opt++ == '=')
5087 cmd = 'B';
5088 } else if (skip_prefix(opt, "find-copies", &opt)) {
5089 if (*opt == 0 || *opt++ == '=')
5090 cmd = 'C';
5091 } else if (skip_prefix(opt, "find-renames", &opt)) {
5092 if (*opt == 0 || *opt++ == '=')
5093 cmd = 'M';
5096 if (cmd != 'M' && cmd != 'C' && cmd != 'B')
5097 return -1; /* that is not a -M, -C, or -B option */
5099 opt1 = parse_rename_score(&opt);
5100 if (cmd != 'B')
5101 opt2 = 0;
5102 else {
5103 if (*opt == 0)
5104 opt2 = 0;
5105 else if (*opt != '/')
5106 return -1; /* we expect -B80/99 or -B80 */
5107 else {
5108 opt++;
5109 opt2 = parse_rename_score(&opt);
5112 if (*opt != 0)
5113 return -1;
5114 return opt1 | (opt2 << 16);
5117 struct diff_queue_struct diff_queued_diff;
5119 void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
5121 ALLOC_GROW(queue->queue, queue->nr + 1, queue->alloc);
5122 queue->queue[queue->nr++] = dp;
5125 struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
5126 struct diff_filespec *one,
5127 struct diff_filespec *two)
5129 struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
5130 dp->one = one;
5131 dp->two = two;
5132 if (queue)
5133 diff_q(queue, dp);
5134 return dp;
5137 void diff_free_filepair(struct diff_filepair *p)
5139 free_filespec(p->one);
5140 free_filespec(p->two);
5141 free(p);
5144 const char *diff_aligned_abbrev(const struct object_id *oid, int len)
5146 int abblen;
5147 const char *abbrev;
5149 /* Do we want all 40 hex characters? */
5150 if (len == GIT_SHA1_HEXSZ)
5151 return oid_to_hex(oid);
5153 /* An abbreviated value is fine, possibly followed by an ellipsis. */
5154 abbrev = diff_abbrev_oid(oid, len);
5156 if (!print_sha1_ellipsis())
5157 return abbrev;
5159 abblen = strlen(abbrev);
5162 * In well-behaved cases, where the abbreviated result is the
5163 * same as the requested length, append three dots after the
5164 * abbreviation (hence the whole logic is limited to the case
5165 * where abblen < 37); when the actual abbreviated result is a
5166 * bit longer than the requested length, we reduce the number
5167 * of dots so that they match the well-behaved ones. However,
5168 * if the actual abbreviation is longer than the requested
5169 * length by more than three, we give up on aligning, and add
5170 * three dots anyway, to indicate that the output is not the
5171 * full object name. Yes, this may be suboptimal, but this
5172 * appears only in "diff --raw --abbrev" output and it is not
5173 * worth the effort to change it now. Note that this would
5174 * likely to work fine when the automatic sizing of default
5175 * abbreviation length is used--we would be fed -1 in "len" in
5176 * that case, and will end up always appending three-dots, but
5177 * the automatic sizing is supposed to give abblen that ensures
5178 * uniqueness across all objects (statistically speaking).
5180 if (abblen < GIT_SHA1_HEXSZ - 3) {
5181 static char hex[GIT_MAX_HEXSZ + 1];
5182 if (len < abblen && abblen <= len + 2)
5183 xsnprintf(hex, sizeof(hex), "%s%.*s", abbrev, len+3-abblen, "..");
5184 else
5185 xsnprintf(hex, sizeof(hex), "%s...", abbrev);
5186 return hex;
5189 return oid_to_hex(oid);
5192 static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
5194 int line_termination = opt->line_termination;
5195 int inter_name_termination = line_termination ? '\t' : '\0';
5197 fprintf(opt->file, "%s", diff_line_prefix(opt));
5198 if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
5199 fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
5200 diff_aligned_abbrev(&p->one->oid, opt->abbrev));
5201 fprintf(opt->file, "%s ",
5202 diff_aligned_abbrev(&p->two->oid, opt->abbrev));
5204 if (p->score) {
5205 fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
5206 inter_name_termination);
5207 } else {
5208 fprintf(opt->file, "%c%c", p->status, inter_name_termination);
5211 if (p->status == DIFF_STATUS_COPIED ||
5212 p->status == DIFF_STATUS_RENAMED) {
5213 const char *name_a, *name_b;
5214 name_a = p->one->path;
5215 name_b = p->two->path;
5216 strip_prefix(opt->prefix_length, &name_a, &name_b);
5217 write_name_quoted(name_a, opt->file, inter_name_termination);
5218 write_name_quoted(name_b, opt->file, line_termination);
5219 } else {
5220 const char *name_a, *name_b;
5221 name_a = p->one->mode ? p->one->path : p->two->path;
5222 name_b = NULL;
5223 strip_prefix(opt->prefix_length, &name_a, &name_b);
5224 write_name_quoted(name_a, opt->file, line_termination);
5228 int diff_unmodified_pair(struct diff_filepair *p)
5230 /* This function is written stricter than necessary to support
5231 * the currently implemented transformers, but the idea is to
5232 * let transformers to produce diff_filepairs any way they want,
5233 * and filter and clean them up here before producing the output.
5235 struct diff_filespec *one = p->one, *two = p->two;
5237 if (DIFF_PAIR_UNMERGED(p))
5238 return 0; /* unmerged is interesting */
5240 /* deletion, addition, mode or type change
5241 * and rename are all interesting.
5243 if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
5244 DIFF_PAIR_MODE_CHANGED(p) ||
5245 strcmp(one->path, two->path))
5246 return 0;
5248 /* both are valid and point at the same path. that is, we are
5249 * dealing with a change.
5251 if (one->oid_valid && two->oid_valid &&
5252 !oidcmp(&one->oid, &two->oid) &&
5253 !one->dirty_submodule && !two->dirty_submodule)
5254 return 1; /* no change */
5255 if (!one->oid_valid && !two->oid_valid)
5256 return 1; /* both look at the same file on the filesystem. */
5257 return 0;
5260 static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
5262 if (diff_unmodified_pair(p))
5263 return;
5265 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5266 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5267 return; /* no tree diffs in patch format */
5269 run_diff(p, o);
5272 static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
5273 struct diffstat_t *diffstat)
5275 if (diff_unmodified_pair(p))
5276 return;
5278 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5279 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5280 return; /* no useful stat for tree diffs */
5282 run_diffstat(p, o, diffstat);
5285 static void diff_flush_checkdiff(struct diff_filepair *p,
5286 struct diff_options *o)
5288 if (diff_unmodified_pair(p))
5289 return;
5291 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5292 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5293 return; /* nothing to check in tree diffs */
5295 run_checkdiff(p, o);
5298 int diff_queue_is_empty(void)
5300 struct diff_queue_struct *q = &diff_queued_diff;
5301 int i;
5302 for (i = 0; i < q->nr; i++)
5303 if (!diff_unmodified_pair(q->queue[i]))
5304 return 0;
5305 return 1;
5308 #if DIFF_DEBUG
5309 void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
5311 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
5312 x, one ? one : "",
5313 s->path,
5314 DIFF_FILE_VALID(s) ? "valid" : "invalid",
5315 s->mode,
5316 s->oid_valid ? oid_to_hex(&s->oid) : "");
5317 fprintf(stderr, "queue[%d] %s size %lu\n",
5318 x, one ? one : "",
5319 s->size);
5322 void diff_debug_filepair(const struct diff_filepair *p, int i)
5324 diff_debug_filespec(p->one, i, "one");
5325 diff_debug_filespec(p->two, i, "two");
5326 fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
5327 p->score, p->status ? p->status : '?',
5328 p->one->rename_used, p->broken_pair);
5331 void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
5333 int i;
5334 if (msg)
5335 fprintf(stderr, "%s\n", msg);
5336 fprintf(stderr, "q->nr = %d\n", q->nr);
5337 for (i = 0; i < q->nr; i++) {
5338 struct diff_filepair *p = q->queue[i];
5339 diff_debug_filepair(p, i);
5342 #endif
5344 static void diff_resolve_rename_copy(void)
5346 int i;
5347 struct diff_filepair *p;
5348 struct diff_queue_struct *q = &diff_queued_diff;
5350 diff_debug_queue("resolve-rename-copy", q);
5352 for (i = 0; i < q->nr; i++) {
5353 p = q->queue[i];
5354 p->status = 0; /* undecided */
5355 if (DIFF_PAIR_UNMERGED(p))
5356 p->status = DIFF_STATUS_UNMERGED;
5357 else if (!DIFF_FILE_VALID(p->one))
5358 p->status = DIFF_STATUS_ADDED;
5359 else if (!DIFF_FILE_VALID(p->two))
5360 p->status = DIFF_STATUS_DELETED;
5361 else if (DIFF_PAIR_TYPE_CHANGED(p))
5362 p->status = DIFF_STATUS_TYPE_CHANGED;
5364 /* from this point on, we are dealing with a pair
5365 * whose both sides are valid and of the same type, i.e.
5366 * either in-place edit or rename/copy edit.
5368 else if (DIFF_PAIR_RENAME(p)) {
5370 * A rename might have re-connected a broken
5371 * pair up, causing the pathnames to be the
5372 * same again. If so, that's not a rename at
5373 * all, just a modification..
5375 * Otherwise, see if this source was used for
5376 * multiple renames, in which case we decrement
5377 * the count, and call it a copy.
5379 if (!strcmp(p->one->path, p->two->path))
5380 p->status = DIFF_STATUS_MODIFIED;
5381 else if (--p->one->rename_used > 0)
5382 p->status = DIFF_STATUS_COPIED;
5383 else
5384 p->status = DIFF_STATUS_RENAMED;
5386 else if (oidcmp(&p->one->oid, &p->two->oid) ||
5387 p->one->mode != p->two->mode ||
5388 p->one->dirty_submodule ||
5389 p->two->dirty_submodule ||
5390 is_null_oid(&p->one->oid))
5391 p->status = DIFF_STATUS_MODIFIED;
5392 else {
5393 /* This is a "no-change" entry and should not
5394 * happen anymore, but prepare for broken callers.
5396 error("feeding unmodified %s to diffcore",
5397 p->one->path);
5398 p->status = DIFF_STATUS_UNKNOWN;
5401 diff_debug_queue("resolve-rename-copy done", q);
5404 static int check_pair_status(struct diff_filepair *p)
5406 switch (p->status) {
5407 case DIFF_STATUS_UNKNOWN:
5408 return 0;
5409 case 0:
5410 die("internal error in diff-resolve-rename-copy");
5411 default:
5412 return 1;
5416 static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
5418 int fmt = opt->output_format;
5420 if (fmt & DIFF_FORMAT_CHECKDIFF)
5421 diff_flush_checkdiff(p, opt);
5422 else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
5423 diff_flush_raw(p, opt);
5424 else if (fmt & DIFF_FORMAT_NAME) {
5425 const char *name_a, *name_b;
5426 name_a = p->two->path;
5427 name_b = NULL;
5428 strip_prefix(opt->prefix_length, &name_a, &name_b);
5429 fprintf(opt->file, "%s", diff_line_prefix(opt));
5430 write_name_quoted(name_a, opt->file, opt->line_termination);
5434 static void show_file_mode_name(struct diff_options *opt, const char *newdelete, struct diff_filespec *fs)
5436 struct strbuf sb = STRBUF_INIT;
5437 if (fs->mode)
5438 strbuf_addf(&sb, " %s mode %06o ", newdelete, fs->mode);
5439 else
5440 strbuf_addf(&sb, " %s ", newdelete);
5442 quote_c_style(fs->path, &sb, NULL, 0);
5443 strbuf_addch(&sb, '\n');
5444 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5445 sb.buf, sb.len, 0);
5446 strbuf_release(&sb);
5449 static void show_mode_change(struct diff_options *opt, struct diff_filepair *p,
5450 int show_name)
5452 if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
5453 struct strbuf sb = STRBUF_INIT;
5454 strbuf_addf(&sb, " mode change %06o => %06o",
5455 p->one->mode, p->two->mode);
5456 if (show_name) {
5457 strbuf_addch(&sb, ' ');
5458 quote_c_style(p->two->path, &sb, NULL, 0);
5460 strbuf_addch(&sb, '\n');
5461 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5462 sb.buf, sb.len, 0);
5463 strbuf_release(&sb);
5467 static void show_rename_copy(struct diff_options *opt, const char *renamecopy,
5468 struct diff_filepair *p)
5470 struct strbuf sb = STRBUF_INIT;
5471 struct strbuf names = STRBUF_INIT;
5473 pprint_rename(&names, p->one->path, p->two->path);
5474 strbuf_addf(&sb, " %s %s (%d%%)\n",
5475 renamecopy, names.buf, similarity_index(p));
5476 strbuf_release(&names);
5477 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5478 sb.buf, sb.len, 0);
5479 show_mode_change(opt, p, 0);
5480 strbuf_release(&sb);
5483 static void diff_summary(struct diff_options *opt, struct diff_filepair *p)
5485 switch(p->status) {
5486 case DIFF_STATUS_DELETED:
5487 show_file_mode_name(opt, "delete", p->one);
5488 break;
5489 case DIFF_STATUS_ADDED:
5490 show_file_mode_name(opt, "create", p->two);
5491 break;
5492 case DIFF_STATUS_COPIED:
5493 show_rename_copy(opt, "copy", p);
5494 break;
5495 case DIFF_STATUS_RENAMED:
5496 show_rename_copy(opt, "rename", p);
5497 break;
5498 default:
5499 if (p->score) {
5500 struct strbuf sb = STRBUF_INIT;
5501 strbuf_addstr(&sb, " rewrite ");
5502 quote_c_style(p->two->path, &sb, NULL, 0);
5503 strbuf_addf(&sb, " (%d%%)\n", similarity_index(p));
5504 emit_diff_symbol(opt, DIFF_SYMBOL_SUMMARY,
5505 sb.buf, sb.len, 0);
5506 strbuf_release(&sb);
5508 show_mode_change(opt, p, !p->score);
5509 break;
5513 struct patch_id_t {
5514 git_SHA_CTX *ctx;
5515 int patchlen;
5518 static int remove_space(char *line, int len)
5520 int i;
5521 char *dst = line;
5522 unsigned char c;
5524 for (i = 0; i < len; i++)
5525 if (!isspace((c = line[i])))
5526 *dst++ = c;
5528 return dst - line;
5531 static void patch_id_consume(void *priv, char *line, unsigned long len)
5533 struct patch_id_t *data = priv;
5534 int new_len;
5536 /* Ignore line numbers when computing the SHA1 of the patch */
5537 if (starts_with(line, "@@ -"))
5538 return;
5540 new_len = remove_space(line, len);
5542 git_SHA1_Update(data->ctx, line, new_len);
5543 data->patchlen += new_len;
5546 static void patch_id_add_string(git_SHA_CTX *ctx, const char *str)
5548 git_SHA1_Update(ctx, str, strlen(str));
5551 static void patch_id_add_mode(git_SHA_CTX *ctx, unsigned mode)
5553 /* large enough for 2^32 in octal */
5554 char buf[12];
5555 int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
5556 git_SHA1_Update(ctx, buf, len);
5559 /* returns 0 upon success, and writes result into sha1 */
5560 static int diff_get_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
5562 struct diff_queue_struct *q = &diff_queued_diff;
5563 int i;
5564 git_SHA_CTX ctx;
5565 struct patch_id_t data;
5567 git_SHA1_Init(&ctx);
5568 memset(&data, 0, sizeof(struct patch_id_t));
5569 data.ctx = &ctx;
5571 for (i = 0; i < q->nr; i++) {
5572 xpparam_t xpp;
5573 xdemitconf_t xecfg;
5574 mmfile_t mf1, mf2;
5575 struct diff_filepair *p = q->queue[i];
5576 int len1, len2;
5578 memset(&xpp, 0, sizeof(xpp));
5579 memset(&xecfg, 0, sizeof(xecfg));
5580 if (p->status == 0)
5581 return error("internal diff status error");
5582 if (p->status == DIFF_STATUS_UNKNOWN)
5583 continue;
5584 if (diff_unmodified_pair(p))
5585 continue;
5586 if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
5587 (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
5588 continue;
5589 if (DIFF_PAIR_UNMERGED(p))
5590 continue;
5592 diff_fill_oid_info(p->one);
5593 diff_fill_oid_info(p->two);
5595 len1 = remove_space(p->one->path, strlen(p->one->path));
5596 len2 = remove_space(p->two->path, strlen(p->two->path));
5597 patch_id_add_string(&ctx, "diff--git");
5598 patch_id_add_string(&ctx, "a/");
5599 git_SHA1_Update(&ctx, p->one->path, len1);
5600 patch_id_add_string(&ctx, "b/");
5601 git_SHA1_Update(&ctx, p->two->path, len2);
5603 if (p->one->mode == 0) {
5604 patch_id_add_string(&ctx, "newfilemode");
5605 patch_id_add_mode(&ctx, p->two->mode);
5606 patch_id_add_string(&ctx, "---/dev/null");
5607 patch_id_add_string(&ctx, "+++b/");
5608 git_SHA1_Update(&ctx, p->two->path, len2);
5609 } else if (p->two->mode == 0) {
5610 patch_id_add_string(&ctx, "deletedfilemode");
5611 patch_id_add_mode(&ctx, p->one->mode);
5612 patch_id_add_string(&ctx, "---a/");
5613 git_SHA1_Update(&ctx, p->one->path, len1);
5614 patch_id_add_string(&ctx, "+++/dev/null");
5615 } else {
5616 patch_id_add_string(&ctx, "---a/");
5617 git_SHA1_Update(&ctx, p->one->path, len1);
5618 patch_id_add_string(&ctx, "+++b/");
5619 git_SHA1_Update(&ctx, p->two->path, len2);
5622 if (diff_header_only)
5623 continue;
5625 if (fill_mmfile(&mf1, p->one) < 0 ||
5626 fill_mmfile(&mf2, p->two) < 0)
5627 return error("unable to read files to diff");
5629 if (diff_filespec_is_binary(p->one) ||
5630 diff_filespec_is_binary(p->two)) {
5631 git_SHA1_Update(&ctx, oid_to_hex(&p->one->oid),
5632 GIT_SHA1_HEXSZ);
5633 git_SHA1_Update(&ctx, oid_to_hex(&p->two->oid),
5634 GIT_SHA1_HEXSZ);
5635 continue;
5638 xpp.flags = 0;
5639 xecfg.ctxlen = 3;
5640 xecfg.flags = 0;
5641 if (xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
5642 &xpp, &xecfg))
5643 return error("unable to generate patch-id diff for %s",
5644 p->one->path);
5647 git_SHA1_Final(oid->hash, &ctx);
5648 return 0;
5651 int diff_flush_patch_id(struct diff_options *options, struct object_id *oid, int diff_header_only)
5653 struct diff_queue_struct *q = &diff_queued_diff;
5654 int i;
5655 int result = diff_get_patch_id(options, oid, diff_header_only);
5657 for (i = 0; i < q->nr; i++)
5658 diff_free_filepair(q->queue[i]);
5660 free(q->queue);
5661 DIFF_QUEUE_CLEAR(q);
5663 return result;
5666 static int is_summary_empty(const struct diff_queue_struct *q)
5668 int i;
5670 for (i = 0; i < q->nr; i++) {
5671 const struct diff_filepair *p = q->queue[i];
5673 switch (p->status) {
5674 case DIFF_STATUS_DELETED:
5675 case DIFF_STATUS_ADDED:
5676 case DIFF_STATUS_COPIED:
5677 case DIFF_STATUS_RENAMED:
5678 return 0;
5679 default:
5680 if (p->score)
5681 return 0;
5682 if (p->one->mode && p->two->mode &&
5683 p->one->mode != p->two->mode)
5684 return 0;
5685 break;
5688 return 1;
5691 static const char rename_limit_warning[] =
5692 N_("inexact rename detection was skipped due to too many files.");
5694 static const char degrade_cc_to_c_warning[] =
5695 N_("only found copies from modified paths due to too many files.");
5697 static const char rename_limit_advice[] =
5698 N_("you may want to set your %s variable to at least "
5699 "%d and retry the command.");
5701 void diff_warn_rename_limit(const char *varname, int needed, int degraded_cc)
5703 fflush(stdout);
5704 if (degraded_cc)
5705 warning(_(degrade_cc_to_c_warning));
5706 else if (needed)
5707 warning(_(rename_limit_warning));
5708 else
5709 return;
5710 if (0 < needed)
5711 warning(_(rename_limit_advice), varname, needed);
5714 static void diff_flush_patch_all_file_pairs(struct diff_options *o)
5716 int i;
5717 static struct emitted_diff_symbols esm = EMITTED_DIFF_SYMBOLS_INIT;
5718 struct diff_queue_struct *q = &diff_queued_diff;
5720 if (WSEH_NEW & WS_RULE_MASK)
5721 die("BUG: WS rules bit mask overlaps with diff symbol flags");
5723 if (o->color_moved)
5724 o->emitted_symbols = &esm;
5726 for (i = 0; i < q->nr; i++) {
5727 struct diff_filepair *p = q->queue[i];
5728 if (check_pair_status(p))
5729 diff_flush_patch(p, o);
5732 if (o->emitted_symbols) {
5733 if (o->color_moved) {
5734 struct hashmap add_lines, del_lines;
5736 if (o->color_moved_ws_handling &
5737 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
5738 o->color_moved_ws_handling |= XDF_IGNORE_WHITESPACE;
5740 hashmap_init(&del_lines, moved_entry_cmp, o, 0);
5741 hashmap_init(&add_lines, moved_entry_cmp, o, 0);
5743 add_lines_to_move_detection(o, &add_lines, &del_lines);
5744 mark_color_as_moved(o, &add_lines, &del_lines);
5745 if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
5746 dim_moved_lines(o);
5748 hashmap_free(&add_lines, 0);
5749 hashmap_free(&del_lines, 0);
5752 for (i = 0; i < esm.nr; i++)
5753 emit_diff_symbol_from_struct(o, &esm.buf[i]);
5755 for (i = 0; i < esm.nr; i++)
5756 free((void *)esm.buf[i].line);
5758 esm.nr = 0;
5761 void diff_flush(struct diff_options *options)
5763 struct diff_queue_struct *q = &diff_queued_diff;
5764 int i, output_format = options->output_format;
5765 int separator = 0;
5766 int dirstat_by_line = 0;
5769 * Order: raw, stat, summary, patch
5770 * or: name/name-status/checkdiff (other bits clear)
5772 if (!q->nr)
5773 goto free_queue;
5775 if (output_format & (DIFF_FORMAT_RAW |
5776 DIFF_FORMAT_NAME |
5777 DIFF_FORMAT_NAME_STATUS |
5778 DIFF_FORMAT_CHECKDIFF)) {
5779 for (i = 0; i < q->nr; i++) {
5780 struct diff_filepair *p = q->queue[i];
5781 if (check_pair_status(p))
5782 flush_one_pair(p, options);
5784 separator++;
5787 if (output_format & DIFF_FORMAT_DIRSTAT && options->flags.dirstat_by_line)
5788 dirstat_by_line = 1;
5790 if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT) ||
5791 dirstat_by_line) {
5792 struct diffstat_t diffstat;
5794 memset(&diffstat, 0, sizeof(struct diffstat_t));
5795 for (i = 0; i < q->nr; i++) {
5796 struct diff_filepair *p = q->queue[i];
5797 if (check_pair_status(p))
5798 diff_flush_stat(p, options, &diffstat);
5800 if (output_format & DIFF_FORMAT_NUMSTAT)
5801 show_numstat(&diffstat, options);
5802 if (output_format & DIFF_FORMAT_DIFFSTAT)
5803 show_stats(&diffstat, options);
5804 if (output_format & DIFF_FORMAT_SHORTSTAT)
5805 show_shortstats(&diffstat, options);
5806 if (output_format & DIFF_FORMAT_DIRSTAT && dirstat_by_line)
5807 show_dirstat_by_line(&diffstat, options);
5808 free_diffstat_info(&diffstat);
5809 separator++;
5811 if ((output_format & DIFF_FORMAT_DIRSTAT) && !dirstat_by_line)
5812 show_dirstat(options);
5814 if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
5815 for (i = 0; i < q->nr; i++) {
5816 diff_summary(options, q->queue[i]);
5818 separator++;
5821 if (output_format & DIFF_FORMAT_NO_OUTPUT &&
5822 options->flags.exit_with_status &&
5823 options->flags.diff_from_contents) {
5825 * run diff_flush_patch for the exit status. setting
5826 * options->file to /dev/null should be safe, because we
5827 * aren't supposed to produce any output anyway.
5829 if (options->close_file)
5830 fclose(options->file);
5831 options->file = xfopen("/dev/null", "w");
5832 options->close_file = 1;
5833 options->color_moved = 0;
5834 for (i = 0; i < q->nr; i++) {
5835 struct diff_filepair *p = q->queue[i];
5836 if (check_pair_status(p))
5837 diff_flush_patch(p, options);
5838 if (options->found_changes)
5839 break;
5843 if (output_format & DIFF_FORMAT_PATCH) {
5844 if (separator) {
5845 emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
5846 if (options->stat_sep)
5847 /* attach patch instead of inline */
5848 emit_diff_symbol(options, DIFF_SYMBOL_STAT_SEP,
5849 NULL, 0, 0);
5852 diff_flush_patch_all_file_pairs(options);
5855 if (output_format & DIFF_FORMAT_CALLBACK)
5856 options->format_callback(q, options, options->format_callback_data);
5858 for (i = 0; i < q->nr; i++)
5859 diff_free_filepair(q->queue[i]);
5860 free_queue:
5861 free(q->queue);
5862 DIFF_QUEUE_CLEAR(q);
5863 if (options->close_file)
5864 fclose(options->file);
5867 * Report the content-level differences with HAS_CHANGES;
5868 * diff_addremove/diff_change does not set the bit when
5869 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
5871 if (options->flags.diff_from_contents) {
5872 if (options->found_changes)
5873 options->flags.has_changes = 1;
5874 else
5875 options->flags.has_changes = 0;
5879 static int match_filter(const struct diff_options *options, const struct diff_filepair *p)
5881 return (((p->status == DIFF_STATUS_MODIFIED) &&
5882 ((p->score &&
5883 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN, options)) ||
5884 (!p->score &&
5885 filter_bit_tst(DIFF_STATUS_MODIFIED, options)))) ||
5886 ((p->status != DIFF_STATUS_MODIFIED) &&
5887 filter_bit_tst(p->status, options)));
5890 static void diffcore_apply_filter(struct diff_options *options)
5892 int i;
5893 struct diff_queue_struct *q = &diff_queued_diff;
5894 struct diff_queue_struct outq;
5896 DIFF_QUEUE_CLEAR(&outq);
5898 if (!options->filter)
5899 return;
5901 if (filter_bit_tst(DIFF_STATUS_FILTER_AON, options)) {
5902 int found;
5903 for (i = found = 0; !found && i < q->nr; i++) {
5904 if (match_filter(options, q->queue[i]))
5905 found++;
5907 if (found)
5908 return;
5910 /* otherwise we will clear the whole queue
5911 * by copying the empty outq at the end of this
5912 * function, but first clear the current entries
5913 * in the queue.
5915 for (i = 0; i < q->nr; i++)
5916 diff_free_filepair(q->queue[i]);
5918 else {
5919 /* Only the matching ones */
5920 for (i = 0; i < q->nr; i++) {
5921 struct diff_filepair *p = q->queue[i];
5922 if (match_filter(options, p))
5923 diff_q(&outq, p);
5924 else
5925 diff_free_filepair(p);
5928 free(q->queue);
5929 *q = outq;
5932 /* Check whether two filespecs with the same mode and size are identical */
5933 static int diff_filespec_is_identical(struct diff_filespec *one,
5934 struct diff_filespec *two)
5936 if (S_ISGITLINK(one->mode))
5937 return 0;
5938 if (diff_populate_filespec(one, 0))
5939 return 0;
5940 if (diff_populate_filespec(two, 0))
5941 return 0;
5942 return !memcmp(one->data, two->data, one->size);
5945 static int diff_filespec_check_stat_unmatch(struct diff_filepair *p)
5947 if (p->done_skip_stat_unmatch)
5948 return p->skip_stat_unmatch_result;
5950 p->done_skip_stat_unmatch = 1;
5951 p->skip_stat_unmatch_result = 0;
5953 * 1. Entries that come from stat info dirtiness
5954 * always have both sides (iow, not create/delete),
5955 * one side of the object name is unknown, with
5956 * the same mode and size. Keep the ones that
5957 * do not match these criteria. They have real
5958 * differences.
5960 * 2. At this point, the file is known to be modified,
5961 * with the same mode and size, and the object
5962 * name of one side is unknown. Need to inspect
5963 * the identical contents.
5965 if (!DIFF_FILE_VALID(p->one) || /* (1) */
5966 !DIFF_FILE_VALID(p->two) ||
5967 (p->one->oid_valid && p->two->oid_valid) ||
5968 (p->one->mode != p->two->mode) ||
5969 diff_populate_filespec(p->one, CHECK_SIZE_ONLY) ||
5970 diff_populate_filespec(p->two, CHECK_SIZE_ONLY) ||
5971 (p->one->size != p->two->size) ||
5972 !diff_filespec_is_identical(p->one, p->two)) /* (2) */
5973 p->skip_stat_unmatch_result = 1;
5974 return p->skip_stat_unmatch_result;
5977 static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
5979 int i;
5980 struct diff_queue_struct *q = &diff_queued_diff;
5981 struct diff_queue_struct outq;
5982 DIFF_QUEUE_CLEAR(&outq);
5984 for (i = 0; i < q->nr; i++) {
5985 struct diff_filepair *p = q->queue[i];
5987 if (diff_filespec_check_stat_unmatch(p))
5988 diff_q(&outq, p);
5989 else {
5991 * The caller can subtract 1 from skip_stat_unmatch
5992 * to determine how many paths were dirty only
5993 * due to stat info mismatch.
5995 if (!diffopt->flags.no_index)
5996 diffopt->skip_stat_unmatch++;
5997 diff_free_filepair(p);
6000 free(q->queue);
6001 *q = outq;
6004 static int diffnamecmp(const void *a_, const void *b_)
6006 const struct diff_filepair *a = *((const struct diff_filepair **)a_);
6007 const struct diff_filepair *b = *((const struct diff_filepair **)b_);
6008 const char *name_a, *name_b;
6010 name_a = a->one ? a->one->path : a->two->path;
6011 name_b = b->one ? b->one->path : b->two->path;
6012 return strcmp(name_a, name_b);
6015 void diffcore_fix_diff_index(struct diff_options *options)
6017 struct diff_queue_struct *q = &diff_queued_diff;
6018 QSORT(q->queue, q->nr, diffnamecmp);
6021 void diffcore_std(struct diff_options *options)
6023 /* NOTE please keep the following in sync with diff_tree_combined() */
6024 if (options->skip_stat_unmatch)
6025 diffcore_skip_stat_unmatch(options);
6026 if (!options->found_follow) {
6027 /* See try_to_follow_renames() in tree-diff.c */
6028 if (options->break_opt != -1)
6029 diffcore_break(options->break_opt);
6030 if (options->detect_rename)
6031 diffcore_rename(options);
6032 if (options->break_opt != -1)
6033 diffcore_merge_broken();
6035 if (options->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK)
6036 diffcore_pickaxe(options);
6037 if (options->orderfile)
6038 diffcore_order(options->orderfile);
6039 if (!options->found_follow)
6040 /* See try_to_follow_renames() in tree-diff.c */
6041 diff_resolve_rename_copy();
6042 diffcore_apply_filter(options);
6044 if (diff_queued_diff.nr && !options->flags.diff_from_contents)
6045 options->flags.has_changes = 1;
6046 else
6047 options->flags.has_changes = 0;
6049 options->found_follow = 0;
6052 int diff_result_code(struct diff_options *opt, int status)
6054 int result = 0;
6056 diff_warn_rename_limit("diff.renameLimit",
6057 opt->needed_rename_limit,
6058 opt->degraded_cc_to_c);
6059 if (!opt->flags.exit_with_status &&
6060 !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
6061 return status;
6062 if (opt->flags.exit_with_status &&
6063 opt->flags.has_changes)
6064 result |= 01;
6065 if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
6066 opt->flags.check_failed)
6067 result |= 02;
6068 return result;
6071 int diff_can_quit_early(struct diff_options *opt)
6073 return (opt->flags.quick &&
6074 !opt->filter &&
6075 opt->flags.has_changes);
6079 * Shall changes to this submodule be ignored?
6081 * Submodule changes can be configured to be ignored separately for each path,
6082 * but that configuration can be overridden from the command line.
6084 static int is_submodule_ignored(const char *path, struct diff_options *options)
6086 int ignored = 0;
6087 struct diff_flags orig_flags = options->flags;
6088 if (!options->flags.override_submodule_config)
6089 set_diffopt_flags_from_submodule_config(options, path);
6090 if (options->flags.ignore_submodules)
6091 ignored = 1;
6092 options->flags = orig_flags;
6093 return ignored;
6096 void diff_addremove(struct diff_options *options,
6097 int addremove, unsigned mode,
6098 const struct object_id *oid,
6099 int oid_valid,
6100 const char *concatpath, unsigned dirty_submodule)
6102 struct diff_filespec *one, *two;
6104 if (S_ISGITLINK(mode) && is_submodule_ignored(concatpath, options))
6105 return;
6107 /* This may look odd, but it is a preparation for
6108 * feeding "there are unchanged files which should
6109 * not produce diffs, but when you are doing copy
6110 * detection you would need them, so here they are"
6111 * entries to the diff-core. They will be prefixed
6112 * with something like '=' or '*' (I haven't decided
6113 * which but should not make any difference).
6114 * Feeding the same new and old to diff_change()
6115 * also has the same effect.
6116 * Before the final output happens, they are pruned after
6117 * merged into rename/copy pairs as appropriate.
6119 if (options->flags.reverse_diff)
6120 addremove = (addremove == '+' ? '-' :
6121 addremove == '-' ? '+' : addremove);
6123 if (options->prefix &&
6124 strncmp(concatpath, options->prefix, options->prefix_length))
6125 return;
6127 one = alloc_filespec(concatpath);
6128 two = alloc_filespec(concatpath);
6130 if (addremove != '+')
6131 fill_filespec(one, oid, oid_valid, mode);
6132 if (addremove != '-') {
6133 fill_filespec(two, oid, oid_valid, mode);
6134 two->dirty_submodule = dirty_submodule;
6137 diff_queue(&diff_queued_diff, one, two);
6138 if (!options->flags.diff_from_contents)
6139 options->flags.has_changes = 1;
6142 void diff_change(struct diff_options *options,
6143 unsigned old_mode, unsigned new_mode,
6144 const struct object_id *old_oid,
6145 const struct object_id *new_oid,
6146 int old_oid_valid, int new_oid_valid,
6147 const char *concatpath,
6148 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
6150 struct diff_filespec *one, *two;
6151 struct diff_filepair *p;
6153 if (S_ISGITLINK(old_mode) && S_ISGITLINK(new_mode) &&
6154 is_submodule_ignored(concatpath, options))
6155 return;
6157 if (options->flags.reverse_diff) {
6158 SWAP(old_mode, new_mode);
6159 SWAP(old_oid, new_oid);
6160 SWAP(old_oid_valid, new_oid_valid);
6161 SWAP(old_dirty_submodule, new_dirty_submodule);
6164 if (options->prefix &&
6165 strncmp(concatpath, options->prefix, options->prefix_length))
6166 return;
6168 one = alloc_filespec(concatpath);
6169 two = alloc_filespec(concatpath);
6170 fill_filespec(one, old_oid, old_oid_valid, old_mode);
6171 fill_filespec(two, new_oid, new_oid_valid, new_mode);
6172 one->dirty_submodule = old_dirty_submodule;
6173 two->dirty_submodule = new_dirty_submodule;
6174 p = diff_queue(&diff_queued_diff, one, two);
6176 if (options->flags.diff_from_contents)
6177 return;
6179 if (options->flags.quick && options->skip_stat_unmatch &&
6180 !diff_filespec_check_stat_unmatch(p))
6181 return;
6183 options->flags.has_changes = 1;
6186 struct diff_filepair *diff_unmerge(struct diff_options *options, const char *path)
6188 struct diff_filepair *pair;
6189 struct diff_filespec *one, *two;
6191 if (options->prefix &&
6192 strncmp(path, options->prefix, options->prefix_length))
6193 return NULL;
6195 one = alloc_filespec(path);
6196 two = alloc_filespec(path);
6197 pair = diff_queue(&diff_queued_diff, one, two);
6198 pair->is_unmerged = 1;
6199 return pair;
6202 static char *run_textconv(const char *pgm, struct diff_filespec *spec,
6203 size_t *outsize)
6205 struct diff_tempfile *temp;
6206 const char *argv[3];
6207 const char **arg = argv;
6208 struct child_process child = CHILD_PROCESS_INIT;
6209 struct strbuf buf = STRBUF_INIT;
6210 int err = 0;
6212 temp = prepare_temp_file(spec->path, spec);
6213 *arg++ = pgm;
6214 *arg++ = temp->name;
6215 *arg = NULL;
6217 child.use_shell = 1;
6218 child.argv = argv;
6219 child.out = -1;
6220 if (start_command(&child)) {
6221 remove_tempfile();
6222 return NULL;
6225 if (strbuf_read(&buf, child.out, 0) < 0)
6226 err = error("error reading from textconv command '%s'", pgm);
6227 close(child.out);
6229 if (finish_command(&child) || err) {
6230 strbuf_release(&buf);
6231 remove_tempfile();
6232 return NULL;
6234 remove_tempfile();
6236 return strbuf_detach(&buf, outsize);
6239 size_t fill_textconv(struct userdiff_driver *driver,
6240 struct diff_filespec *df,
6241 char **outbuf)
6243 size_t size;
6245 if (!driver) {
6246 if (!DIFF_FILE_VALID(df)) {
6247 *outbuf = "";
6248 return 0;
6250 if (diff_populate_filespec(df, 0))
6251 die("unable to read files to diff");
6252 *outbuf = df->data;
6253 return df->size;
6256 if (!driver->textconv)
6257 die("BUG: fill_textconv called with non-textconv driver");
6259 if (driver->textconv_cache && df->oid_valid) {
6260 *outbuf = notes_cache_get(driver->textconv_cache,
6261 &df->oid,
6262 &size);
6263 if (*outbuf)
6264 return size;
6267 *outbuf = run_textconv(driver->textconv, df, &size);
6268 if (!*outbuf)
6269 die("unable to read files to diff");
6271 if (driver->textconv_cache && df->oid_valid) {
6272 /* ignore errors, as we might be in a readonly repository */
6273 notes_cache_put(driver->textconv_cache, &df->oid, *outbuf,
6274 size);
6276 * we could save up changes and flush them all at the end,
6277 * but we would need an extra call after all diffing is done.
6278 * Since generating a cache entry is the slow path anyway,
6279 * this extra overhead probably isn't a big deal.
6281 notes_cache_write(driver->textconv_cache);
6284 return size;
6287 int textconv_object(const char *path,
6288 unsigned mode,
6289 const struct object_id *oid,
6290 int oid_valid,
6291 char **buf,
6292 unsigned long *buf_size)
6294 struct diff_filespec *df;
6295 struct userdiff_driver *textconv;
6297 df = alloc_filespec(path);
6298 fill_filespec(df, oid, oid_valid, mode);
6299 textconv = get_textconv(df);
6300 if (!textconv) {
6301 free_filespec(df);
6302 return 0;
6305 *buf_size = fill_textconv(textconv, df, buf);
6306 free_filespec(df);
6307 return 1;
6310 void setup_diff_pager(struct diff_options *opt)
6313 * If the user asked for our exit code, then either they want --quiet
6314 * or --exit-code. We should definitely not bother with a pager in the
6315 * former case, as we will generate no output. Since we still properly
6316 * report our exit code even when a pager is run, we _could_ run a
6317 * pager with --exit-code. But since we have not done so historically,
6318 * and because it is easy to find people oneline advising "git diff
6319 * --exit-code" in hooks and other scripts, we do not do so.
6321 if (!opt->flags.exit_with_status &&
6322 check_pager_config("diff") != 0)
6323 setup_pager();