2 * Copyright (C) 2005 Junio C Hamano
10 #include "xdiff-interface.h"
13 #include "run-command.h"
16 #include "submodule-config.h"
17 #include "submodule.h"
19 #include "string-list.h"
20 #include "argv-array.h"
23 #ifdef NO_FAST_WORKING_DIRECTORY
24 #define FAST_WORKING_DIRECTORY 0
26 #define FAST_WORKING_DIRECTORY 1
29 static int diff_detect_rename_default
;
30 static int diff_compaction_heuristic
; /* experimental */
31 static int diff_rename_limit_default
= 400;
32 static int diff_suppress_blank_empty
;
33 static int diff_use_color_default
= -1;
34 static int diff_context_default
= 3;
35 static const char *diff_word_regex_cfg
;
36 static const char *external_diff_cmd_cfg
;
37 static const char *diff_order_file_cfg
;
38 int diff_auto_refresh_index
= 1;
39 static int diff_mnemonic_prefix
;
40 static int diff_no_prefix
;
41 static int diff_stat_graph_width
;
42 static int diff_dirstat_permille_default
= 30;
43 static struct diff_options default_diff_options
;
44 static long diff_algorithm
;
46 static char diff_colors
[][COLOR_MAXLEN
] = {
48 GIT_COLOR_NORMAL
, /* CONTEXT */
49 GIT_COLOR_BOLD
, /* METAINFO */
50 GIT_COLOR_CYAN
, /* FRAGINFO */
51 GIT_COLOR_RED
, /* OLD */
52 GIT_COLOR_GREEN
, /* NEW */
53 GIT_COLOR_YELLOW
, /* COMMIT */
54 GIT_COLOR_BG_RED
, /* WHITESPACE */
55 GIT_COLOR_NORMAL
, /* FUNCINFO */
58 static int parse_diff_color_slot(const char *var
)
60 if (!strcasecmp(var
, "context") || !strcasecmp(var
, "plain"))
62 if (!strcasecmp(var
, "meta"))
64 if (!strcasecmp(var
, "frag"))
66 if (!strcasecmp(var
, "old"))
68 if (!strcasecmp(var
, "new"))
70 if (!strcasecmp(var
, "commit"))
72 if (!strcasecmp(var
, "whitespace"))
73 return DIFF_WHITESPACE
;
74 if (!strcasecmp(var
, "func"))
79 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
80 struct strbuf
*errmsg
)
82 char *params_copy
= xstrdup(params_string
);
83 struct string_list params
= STRING_LIST_INIT_NODUP
;
88 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
89 for (i
= 0; i
< params
.nr
; i
++) {
90 const char *p
= params
.items
[i
].string
;
91 if (!strcmp(p
, "changes")) {
92 DIFF_OPT_CLR(options
, DIRSTAT_BY_LINE
);
93 DIFF_OPT_CLR(options
, DIRSTAT_BY_FILE
);
94 } else if (!strcmp(p
, "lines")) {
95 DIFF_OPT_SET(options
, DIRSTAT_BY_LINE
);
96 DIFF_OPT_CLR(options
, DIRSTAT_BY_FILE
);
97 } else if (!strcmp(p
, "files")) {
98 DIFF_OPT_CLR(options
, DIRSTAT_BY_LINE
);
99 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
100 } else if (!strcmp(p
, "noncumulative")) {
101 DIFF_OPT_CLR(options
, DIRSTAT_CUMULATIVE
);
102 } else if (!strcmp(p
, "cumulative")) {
103 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
104 } else if (isdigit(*p
)) {
106 int permille
= strtoul(p
, &end
, 10) * 10;
107 if (*end
== '.' && isdigit(*++end
)) {
108 /* only use first digit */
109 permille
+= *end
- '0';
110 /* .. and ignore any further digits */
111 while (isdigit(*++end
))
115 options
->dirstat_permille
= permille
;
117 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
122 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
127 string_list_clear(¶ms
, 0);
132 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
134 if (!strcmp(value
, "log"))
135 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
136 else if (!strcmp(value
, "short"))
137 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
138 else if (!strcmp(value
, "diff"))
139 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
145 static int git_config_rename(const char *var
, const char *value
)
148 return DIFF_DETECT_RENAME
;
149 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
150 return DIFF_DETECT_COPY
;
151 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
154 long parse_algorithm_value(const char *value
)
158 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
160 else if (!strcasecmp(value
, "minimal"))
161 return XDF_NEED_MINIMAL
;
162 else if (!strcasecmp(value
, "patience"))
163 return XDF_PATIENCE_DIFF
;
164 else if (!strcasecmp(value
, "histogram"))
165 return XDF_HISTOGRAM_DIFF
;
170 * These are to give UI layer defaults.
171 * The core-level commands such as git-diff-files should
172 * never be affected by the setting of diff.renames
173 * the user happens to have in the configuration file.
175 void init_diff_ui_defaults(void)
177 diff_detect_rename_default
= 1;
180 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
182 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
183 diff_use_color_default
= git_config_colorbool(var
, value
);
186 if (!strcmp(var
, "diff.context")) {
187 diff_context_default
= git_config_int(var
, value
);
188 if (diff_context_default
< 0)
192 if (!strcmp(var
, "diff.renames")) {
193 diff_detect_rename_default
= git_config_rename(var
, value
);
196 if (!strcmp(var
, "diff.compactionheuristic")) {
197 diff_compaction_heuristic
= git_config_bool(var
, value
);
200 if (!strcmp(var
, "diff.autorefreshindex")) {
201 diff_auto_refresh_index
= git_config_bool(var
, value
);
204 if (!strcmp(var
, "diff.mnemonicprefix")) {
205 diff_mnemonic_prefix
= git_config_bool(var
, value
);
208 if (!strcmp(var
, "diff.noprefix")) {
209 diff_no_prefix
= git_config_bool(var
, value
);
212 if (!strcmp(var
, "diff.statgraphwidth")) {
213 diff_stat_graph_width
= git_config_int(var
, value
);
216 if (!strcmp(var
, "diff.external"))
217 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
218 if (!strcmp(var
, "diff.wordregex"))
219 return git_config_string(&diff_word_regex_cfg
, var
, value
);
220 if (!strcmp(var
, "diff.orderfile"))
221 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
223 if (!strcmp(var
, "diff.ignoresubmodules"))
224 handle_ignore_submodules_arg(&default_diff_options
, value
);
226 if (!strcmp(var
, "diff.submodule")) {
227 if (parse_submodule_params(&default_diff_options
, value
))
228 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
233 if (!strcmp(var
, "diff.algorithm")) {
234 diff_algorithm
= parse_algorithm_value(value
);
235 if (diff_algorithm
< 0)
240 if (git_color_config(var
, value
, cb
) < 0)
243 return git_diff_basic_config(var
, value
, cb
);
246 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
250 if (!strcmp(var
, "diff.renamelimit")) {
251 diff_rename_limit_default
= git_config_int(var
, value
);
255 if (userdiff_config(var
, value
) < 0)
258 if (skip_prefix(var
, "diff.color.", &name
) ||
259 skip_prefix(var
, "color.diff.", &name
)) {
260 int slot
= parse_diff_color_slot(name
);
264 return config_error_nonbool(var
);
265 return color_parse(value
, diff_colors
[slot
]);
268 /* like GNU diff's --suppress-blank-empty option */
269 if (!strcmp(var
, "diff.suppressblankempty") ||
270 /* for backwards compatibility */
271 !strcmp(var
, "diff.suppress-blank-empty")) {
272 diff_suppress_blank_empty
= git_config_bool(var
, value
);
276 if (!strcmp(var
, "diff.dirstat")) {
277 struct strbuf errmsg
= STRBUF_INIT
;
278 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
279 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
280 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
282 strbuf_release(&errmsg
);
283 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
287 if (starts_with(var
, "submodule."))
288 return parse_submodule_config_option(var
, value
);
290 return git_default_config(var
, value
, cb
);
293 static char *quote_two(const char *one
, const char *two
)
295 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
296 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
297 struct strbuf res
= STRBUF_INIT
;
299 if (need_one
+ need_two
) {
300 strbuf_addch(&res
, '"');
301 quote_c_style(one
, &res
, NULL
, 1);
302 quote_c_style(two
, &res
, NULL
, 1);
303 strbuf_addch(&res
, '"');
305 strbuf_addstr(&res
, one
);
306 strbuf_addstr(&res
, two
);
308 return strbuf_detach(&res
, NULL
);
311 static const char *external_diff(void)
313 static const char *external_diff_cmd
= NULL
;
314 static int done_preparing
= 0;
317 return external_diff_cmd
;
318 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
319 if (!external_diff_cmd
)
320 external_diff_cmd
= external_diff_cmd_cfg
;
322 return external_diff_cmd
;
326 * Keep track of files used for diffing. Sometimes such an entry
327 * refers to a temporary file, sometimes to an existing file, and
328 * sometimes to "/dev/null".
330 static struct diff_tempfile
{
332 * filename external diff should read from, or NULL if this
333 * entry is currently not in use:
337 char hex
[GIT_SHA1_HEXSZ
+ 1];
341 * If this diff_tempfile instance refers to a temporary file,
342 * this tempfile object is used to manage its lifetime.
344 struct tempfile tempfile
;
347 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
349 struct emit_callback
{
352 int blank_at_eof_in_preimage
;
353 int blank_at_eof_in_postimage
;
355 int lno_in_postimage
;
356 sane_truncate_fn truncate
;
357 const char **label_path
;
358 struct diff_words_data
*diff_words
;
359 struct diff_options
*opt
;
361 struct strbuf
*header
;
364 static int count_lines(const char *data
, int size
)
366 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
373 completely_empty
= 0;
377 completely_empty
= 0;
380 if (completely_empty
)
383 count
++; /* no trailing newline */
387 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
389 if (!DIFF_FILE_VALID(one
)) {
390 mf
->ptr
= (char *)""; /* does not matter */
394 else if (diff_populate_filespec(one
, 0))
398 mf
->size
= one
->size
;
402 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
403 static unsigned long diff_filespec_size(struct diff_filespec
*one
)
405 if (!DIFF_FILE_VALID(one
))
407 diff_populate_filespec(one
, CHECK_SIZE_ONLY
);
411 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
414 long size
= mf
->size
;
419 ptr
+= size
- 1; /* pointing at the very end */
421 ; /* incomplete line */
423 ptr
--; /* skip the last LF */
424 while (mf
->ptr
< ptr
) {
426 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
427 if (*prev_eol
== '\n')
429 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
437 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
438 struct emit_callback
*ecbdata
)
441 unsigned ws_rule
= ecbdata
->ws_rule
;
442 l1
= count_trailing_blank(mf1
, ws_rule
);
443 l2
= count_trailing_blank(mf2
, ws_rule
);
445 ecbdata
->blank_at_eof_in_preimage
= 0;
446 ecbdata
->blank_at_eof_in_postimage
= 0;
449 at
= count_lines(mf1
->ptr
, mf1
->size
);
450 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
452 at
= count_lines(mf2
->ptr
, mf2
->size
);
453 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
456 static void emit_line_0(struct diff_options
*o
, const char *set
, const char *reset
,
457 int first
, const char *line
, int len
)
459 int has_trailing_newline
, has_trailing_carriage_return
;
461 FILE *file
= o
->file
;
463 fputs(diff_line_prefix(o
), file
);
466 has_trailing_newline
= (first
== '\n');
467 has_trailing_carriage_return
= (!has_trailing_newline
&&
469 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
471 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
472 if (has_trailing_newline
)
474 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
475 if (has_trailing_carriage_return
)
480 if (len
|| !nofirst
) {
484 fwrite(line
, len
, 1, file
);
487 if (has_trailing_carriage_return
)
489 if (has_trailing_newline
)
493 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
494 const char *line
, int len
)
496 emit_line_0(o
, set
, reset
, line
[0], line
+1, len
-1);
499 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
501 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
502 ecbdata
->blank_at_eof_in_preimage
&&
503 ecbdata
->blank_at_eof_in_postimage
&&
504 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
505 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
507 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
510 static void emit_line_checked(const char *reset
,
511 struct emit_callback
*ecbdata
,
512 const char *line
, int len
,
513 enum color_diff color
,
514 unsigned ws_error_highlight
,
517 const char *set
= diff_get_color(ecbdata
->color_diff
, color
);
518 const char *ws
= NULL
;
520 if (ecbdata
->opt
->ws_error_highlight
& ws_error_highlight
) {
521 ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
527 emit_line_0(ecbdata
->opt
, set
, reset
, sign
, line
, len
);
528 else if (sign
== '+' && new_blank_line_at_eof(ecbdata
, line
, len
))
529 /* Blank line at EOF - paint '+' as well */
530 emit_line_0(ecbdata
->opt
, ws
, reset
, sign
, line
, len
);
532 /* Emit just the prefix, then the rest. */
533 emit_line_0(ecbdata
->opt
, set
, reset
, sign
, "", 0);
534 ws_check_emit(line
, len
, ecbdata
->ws_rule
,
535 ecbdata
->opt
->file
, set
, reset
, ws
);
539 static void emit_add_line(const char *reset
,
540 struct emit_callback
*ecbdata
,
541 const char *line
, int len
)
543 emit_line_checked(reset
, ecbdata
, line
, len
,
544 DIFF_FILE_NEW
, WSEH_NEW
, '+');
547 static void emit_del_line(const char *reset
,
548 struct emit_callback
*ecbdata
,
549 const char *line
, int len
)
551 emit_line_checked(reset
, ecbdata
, line
, len
,
552 DIFF_FILE_OLD
, WSEH_OLD
, '-');
555 static void emit_context_line(const char *reset
,
556 struct emit_callback
*ecbdata
,
557 const char *line
, int len
)
559 emit_line_checked(reset
, ecbdata
, line
, len
,
560 DIFF_CONTEXT
, WSEH_CONTEXT
, ' ');
563 static void emit_hunk_header(struct emit_callback
*ecbdata
,
564 const char *line
, int len
)
566 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
567 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
568 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
569 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
570 static const char atat
[2] = { '@', '@' };
572 struct strbuf msgbuf
= STRBUF_INIT
;
577 * As a hunk header must begin with "@@ -<old>, +<new> @@",
578 * it always is at least 10 bytes long.
581 memcmp(line
, atat
, 2) ||
582 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
583 emit_line(ecbdata
->opt
, context
, reset
, line
, len
);
586 ep
+= 2; /* skip over @@ */
588 /* The hunk header in fraginfo color */
589 strbuf_addstr(&msgbuf
, frag
);
590 strbuf_add(&msgbuf
, line
, ep
- line
);
591 strbuf_addstr(&msgbuf
, reset
);
597 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
600 /* blank before the func header */
601 for (cp
= ep
; ep
- line
< len
; ep
++)
602 if (*ep
!= ' ' && *ep
!= '\t')
605 strbuf_addstr(&msgbuf
, context
);
606 strbuf_add(&msgbuf
, cp
, ep
- cp
);
607 strbuf_addstr(&msgbuf
, reset
);
610 if (ep
< line
+ len
) {
611 strbuf_addstr(&msgbuf
, func
);
612 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
613 strbuf_addstr(&msgbuf
, reset
);
616 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
617 emit_line(ecbdata
->opt
, "", "", msgbuf
.buf
, msgbuf
.len
);
618 strbuf_release(&msgbuf
);
621 static struct diff_tempfile
*claim_diff_tempfile(void) {
623 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
624 if (!diff_temp
[i
].name
)
625 return diff_temp
+ i
;
626 die("BUG: diff is failing to clean up its tempfiles");
629 static void remove_tempfile(void)
632 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
633 if (is_tempfile_active(&diff_temp
[i
].tempfile
))
634 delete_tempfile(&diff_temp
[i
].tempfile
);
635 diff_temp
[i
].name
= NULL
;
639 static void print_line_count(FILE *file
, int count
)
643 fprintf(file
, "0,0");
649 fprintf(file
, "1,%d", count
);
654 static void emit_rewrite_lines(struct emit_callback
*ecb
,
655 int prefix
, const char *data
, int size
)
657 const char *endp
= NULL
;
658 static const char *nneof
= " No newline at end of file\n";
659 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
664 endp
= memchr(data
, '\n', size
);
665 len
= endp
? (endp
- data
+ 1) : size
;
667 ecb
->lno_in_preimage
++;
668 emit_del_line(reset
, ecb
, data
, len
);
670 ecb
->lno_in_postimage
++;
671 emit_add_line(reset
, ecb
, data
, len
);
677 const char *context
= diff_get_color(ecb
->color_diff
,
679 putc('\n', ecb
->opt
->file
);
680 emit_line_0(ecb
->opt
, context
, reset
, '\\',
681 nneof
, strlen(nneof
));
685 static void emit_rewrite_diff(const char *name_a
,
687 struct diff_filespec
*one
,
688 struct diff_filespec
*two
,
689 struct userdiff_driver
*textconv_one
,
690 struct userdiff_driver
*textconv_two
,
691 struct diff_options
*o
)
694 const char *name_a_tab
, *name_b_tab
;
695 const char *metainfo
= diff_get_color(o
->use_color
, DIFF_METAINFO
);
696 const char *fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
697 const char *reset
= diff_get_color(o
->use_color
, DIFF_RESET
);
698 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
699 const char *a_prefix
, *b_prefix
;
700 char *data_one
, *data_two
;
701 size_t size_one
, size_two
;
702 struct emit_callback ecbdata
;
703 const char *line_prefix
= diff_line_prefix(o
);
705 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
706 a_prefix
= o
->b_prefix
;
707 b_prefix
= o
->a_prefix
;
709 a_prefix
= o
->a_prefix
;
710 b_prefix
= o
->b_prefix
;
713 name_a
+= (*name_a
== '/');
714 name_b
+= (*name_b
== '/');
715 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
716 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
718 strbuf_reset(&a_name
);
719 strbuf_reset(&b_name
);
720 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
721 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
723 size_one
= fill_textconv(textconv_one
, one
, &data_one
);
724 size_two
= fill_textconv(textconv_two
, two
, &data_two
);
726 memset(&ecbdata
, 0, sizeof(ecbdata
));
727 ecbdata
.color_diff
= want_color(o
->use_color
);
728 ecbdata
.found_changesp
= &o
->found_changes
;
729 ecbdata
.ws_rule
= whitespace_rule(name_b
);
731 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
733 mf1
.ptr
= (char *)data_one
;
734 mf2
.ptr
= (char *)data_two
;
737 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
739 ecbdata
.lno_in_preimage
= 1;
740 ecbdata
.lno_in_postimage
= 1;
742 lc_a
= count_lines(data_one
, size_one
);
743 lc_b
= count_lines(data_two
, size_two
);
745 "%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -",
746 line_prefix
, metainfo
, a_name
.buf
, name_a_tab
, reset
,
747 line_prefix
, metainfo
, b_name
.buf
, name_b_tab
, reset
,
748 line_prefix
, fraginfo
);
749 if (!o
->irreversible_delete
)
750 print_line_count(o
->file
, lc_a
);
752 fprintf(o
->file
, "?,?");
753 fprintf(o
->file
, " +");
754 print_line_count(o
->file
, lc_b
);
755 fprintf(o
->file
, " @@%s\n", reset
);
756 if (lc_a
&& !o
->irreversible_delete
)
757 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
759 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
761 free((char *)data_one
);
763 free((char *)data_two
);
766 struct diff_words_buffer
{
769 struct diff_words_orig
{
770 const char *begin
, *end
;
772 int orig_nr
, orig_alloc
;
775 static void diff_words_append(char *line
, unsigned long len
,
776 struct diff_words_buffer
*buffer
)
778 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
781 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
782 buffer
->text
.size
+= len
;
783 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
786 struct diff_words_style_elem
{
789 const char *color
; /* NULL; filled in by the setup code if
790 * color is enabled */
793 struct diff_words_style
{
794 enum diff_words_type type
;
795 struct diff_words_style_elem
new, old
, ctx
;
799 static struct diff_words_style diff_words_styles
[] = {
800 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
801 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
802 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
805 struct diff_words_data
{
806 struct diff_words_buffer minus
, plus
;
807 const char *current_plus
;
809 struct diff_options
*opt
;
811 enum diff_words_type type
;
812 struct diff_words_style
*style
;
815 static int fn_out_diff_words_write_helper(FILE *fp
,
816 struct diff_words_style_elem
*st_el
,
818 size_t count
, const char *buf
,
819 const char *line_prefix
)
824 char *p
= memchr(buf
, '\n', count
);
826 fputs(line_prefix
, fp
);
828 if (st_el
->color
&& fputs(st_el
->color
, fp
) < 0)
830 if (fputs(st_el
->prefix
, fp
) < 0 ||
831 fwrite(buf
, p
? p
- buf
: count
, 1, fp
) != 1 ||
832 fputs(st_el
->suffix
, fp
) < 0)
834 if (st_el
->color
&& *st_el
->color
835 && fputs(GIT_COLOR_RESET
, fp
) < 0)
840 if (fputs(newline
, fp
) < 0)
842 count
-= p
+ 1 - buf
;
850 * '--color-words' algorithm can be described as:
852 * 1. collect a the minus/plus lines of a diff hunk, divided into
853 * minus-lines and plus-lines;
855 * 2. break both minus-lines and plus-lines into words and
856 * place them into two mmfile_t with one word for each line;
858 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
860 * And for the common parts of the both file, we output the plus side text.
861 * diff_words->current_plus is used to trace the current position of the plus file
862 * which printed. diff_words->last_minus is used to trace the last minus word
865 * For '--graph' to work with '--color-words', we need to output the graph prefix
866 * on each line of color words output. Generally, there are two conditions on
867 * which we should output the prefix.
869 * 1. diff_words->last_minus == 0 &&
870 * diff_words->current_plus == diff_words->plus.text.ptr
872 * that is: the plus text must start as a new line, and if there is no minus
873 * word printed, a graph prefix must be printed.
875 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
876 * *(diff_words->current_plus - 1) == '\n'
878 * that is: a graph prefix must be printed following a '\n'
880 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
882 if ((diff_words
->last_minus
== 0 &&
883 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
884 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
885 *(diff_words
->current_plus
- 1) == '\n')) {
892 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
894 struct diff_words_data
*diff_words
= priv
;
895 struct diff_words_style
*style
= diff_words
->style
;
896 int minus_first
, minus_len
, plus_first
, plus_len
;
897 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
898 struct diff_options
*opt
= diff_words
->opt
;
899 const char *line_prefix
;
901 if (line
[0] != '@' || parse_hunk_header(line
, len
,
902 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
906 line_prefix
= diff_line_prefix(opt
);
908 /* POSIX requires that first be decremented by one if len == 0... */
910 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
912 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
914 minus_begin
= minus_end
=
915 diff_words
->minus
.orig
[minus_first
].end
;
918 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
919 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
921 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
923 if (color_words_output_graph_prefix(diff_words
)) {
924 fputs(line_prefix
, diff_words
->opt
->file
);
926 if (diff_words
->current_plus
!= plus_begin
) {
927 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
928 &style
->ctx
, style
->newline
,
929 plus_begin
- diff_words
->current_plus
,
930 diff_words
->current_plus
, line_prefix
);
931 if (*(plus_begin
- 1) == '\n')
932 fputs(line_prefix
, diff_words
->opt
->file
);
934 if (minus_begin
!= minus_end
) {
935 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
936 &style
->old
, style
->newline
,
937 minus_end
- minus_begin
, minus_begin
,
940 if (plus_begin
!= plus_end
) {
941 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
942 &style
->new, style
->newline
,
943 plus_end
- plus_begin
, plus_begin
,
947 diff_words
->current_plus
= plus_end
;
948 diff_words
->last_minus
= minus_first
;
951 /* This function starts looking at *begin, and returns 0 iff a word was found. */
952 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
953 int *begin
, int *end
)
955 if (word_regex
&& *begin
< buffer
->size
) {
957 if (!regexec(word_regex
, buffer
->ptr
+ *begin
, 1, match
, 0)) {
958 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
959 '\n', match
[0].rm_eo
- match
[0].rm_so
);
960 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
961 *begin
+= match
[0].rm_so
;
962 return *begin
>= *end
;
967 /* find the next word */
968 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
970 if (*begin
>= buffer
->size
)
973 /* find the end of the word */
975 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
982 * This function splits the words in buffer->text, stores the list with
983 * newline separator into out, and saves the offsets of the original words
986 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
995 /* fake an empty "0th" word */
996 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
997 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
1000 for (i
= 0; i
< buffer
->text
.size
; i
++) {
1001 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
1004 /* store original boundaries */
1005 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
1006 buffer
->orig_alloc
);
1007 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
1008 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
1011 /* store one word */
1012 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
1013 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
1014 out
->ptr
[out
->size
+ j
- i
] = '\n';
1015 out
->size
+= j
- i
+ 1;
1021 /* this executes the word diff on the accumulated buffers */
1022 static void diff_words_show(struct diff_words_data
*diff_words
)
1026 mmfile_t minus
, plus
;
1027 struct diff_words_style
*style
= diff_words
->style
;
1029 struct diff_options
*opt
= diff_words
->opt
;
1030 const char *line_prefix
;
1033 line_prefix
= diff_line_prefix(opt
);
1035 /* special case: only removal */
1036 if (!diff_words
->plus
.text
.size
) {
1037 fputs(line_prefix
, diff_words
->opt
->file
);
1038 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
1039 &style
->old
, style
->newline
,
1040 diff_words
->minus
.text
.size
,
1041 diff_words
->minus
.text
.ptr
, line_prefix
);
1042 diff_words
->minus
.text
.size
= 0;
1046 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
1047 diff_words
->last_minus
= 0;
1049 memset(&xpp
, 0, sizeof(xpp
));
1050 memset(&xecfg
, 0, sizeof(xecfg
));
1051 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
1052 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
1054 /* as only the hunk header will be parsed, we need a 0-context */
1056 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
1058 die("unable to generate word diff");
1061 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
1062 diff_words
->plus
.text
.size
) {
1063 if (color_words_output_graph_prefix(diff_words
))
1064 fputs(line_prefix
, diff_words
->opt
->file
);
1065 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
1066 &style
->ctx
, style
->newline
,
1067 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
1068 - diff_words
->current_plus
, diff_words
->current_plus
,
1071 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
1074 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
1075 static void diff_words_flush(struct emit_callback
*ecbdata
)
1077 if (ecbdata
->diff_words
->minus
.text
.size
||
1078 ecbdata
->diff_words
->plus
.text
.size
)
1079 diff_words_show(ecbdata
->diff_words
);
1082 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1084 /* Use already-loaded driver */
1088 if (S_ISREG(one
->mode
))
1089 one
->driver
= userdiff_find_by_path(one
->path
);
1091 /* Fallback to default settings */
1093 one
->driver
= userdiff_find_by_name("default");
1096 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1098 diff_filespec_load_driver(one
);
1099 return one
->driver
->word_regex
;
1102 static void init_diff_words_data(struct emit_callback
*ecbdata
,
1103 struct diff_options
*orig_opts
,
1104 struct diff_filespec
*one
,
1105 struct diff_filespec
*two
)
1108 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
1109 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
1111 ecbdata
->diff_words
=
1112 xcalloc(1, sizeof(struct diff_words_data
));
1113 ecbdata
->diff_words
->type
= o
->word_diff
;
1114 ecbdata
->diff_words
->opt
= o
;
1116 o
->word_regex
= userdiff_word_regex(one
);
1118 o
->word_regex
= userdiff_word_regex(two
);
1120 o
->word_regex
= diff_word_regex_cfg
;
1121 if (o
->word_regex
) {
1122 ecbdata
->diff_words
->word_regex
= (regex_t
*)
1123 xmalloc(sizeof(regex_t
));
1124 if (regcomp(ecbdata
->diff_words
->word_regex
,
1126 REG_EXTENDED
| REG_NEWLINE
))
1127 die ("Invalid regular expression: %s",
1130 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
1131 if (o
->word_diff
== diff_words_styles
[i
].type
) {
1132 ecbdata
->diff_words
->style
=
1133 &diff_words_styles
[i
];
1137 if (want_color(o
->use_color
)) {
1138 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
1139 st
->old
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1140 st
->new.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1141 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1145 static void free_diff_words_data(struct emit_callback
*ecbdata
)
1147 if (ecbdata
->diff_words
) {
1148 diff_words_flush(ecbdata
);
1149 free (ecbdata
->diff_words
->opt
);
1150 free (ecbdata
->diff_words
->minus
.text
.ptr
);
1151 free (ecbdata
->diff_words
->minus
.orig
);
1152 free (ecbdata
->diff_words
->plus
.text
.ptr
);
1153 free (ecbdata
->diff_words
->plus
.orig
);
1154 if (ecbdata
->diff_words
->word_regex
) {
1155 regfree(ecbdata
->diff_words
->word_regex
);
1156 free(ecbdata
->diff_words
->word_regex
);
1158 free(ecbdata
->diff_words
);
1159 ecbdata
->diff_words
= NULL
;
1163 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
1165 if (want_color(diff_use_color
))
1166 return diff_colors
[ix
];
1170 const char *diff_line_prefix(struct diff_options
*opt
)
1172 struct strbuf
*msgbuf
;
1173 if (!opt
->output_prefix
)
1176 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1180 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
1183 unsigned long allot
;
1187 return ecb
->truncate(line
, len
);
1191 (void) utf8_width(&cp
, &l
);
1193 break; /* truncated in the middle? */
1198 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
1201 ecbdata
->lno_in_preimage
= 0;
1202 ecbdata
->lno_in_postimage
= 0;
1203 p
= strchr(line
, '-');
1205 return; /* cannot happen */
1206 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
1209 return; /* cannot happen */
1210 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
1213 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
1215 struct emit_callback
*ecbdata
= priv
;
1216 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
1217 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1218 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1219 struct diff_options
*o
= ecbdata
->opt
;
1220 const char *line_prefix
= diff_line_prefix(o
);
1222 if (ecbdata
->header
) {
1223 fprintf(ecbdata
->opt
->file
, "%s", ecbdata
->header
->buf
);
1224 strbuf_reset(ecbdata
->header
);
1225 ecbdata
->header
= NULL
;
1227 *(ecbdata
->found_changesp
) = 1;
1229 if (ecbdata
->label_path
[0]) {
1230 const char *name_a_tab
, *name_b_tab
;
1232 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
1233 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
1235 fprintf(ecbdata
->opt
->file
, "%s%s--- %s%s%s\n",
1236 line_prefix
, meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
1237 fprintf(ecbdata
->opt
->file
, "%s%s+++ %s%s%s\n",
1238 line_prefix
, meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
1239 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
1242 if (diff_suppress_blank_empty
1243 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
1248 if (line
[0] == '@') {
1249 if (ecbdata
->diff_words
)
1250 diff_words_flush(ecbdata
);
1251 len
= sane_truncate_line(ecbdata
, line
, len
);
1252 find_lno(line
, ecbdata
);
1253 emit_hunk_header(ecbdata
, line
, len
);
1254 if (line
[len
-1] != '\n')
1255 putc('\n', ecbdata
->opt
->file
);
1260 emit_line(ecbdata
->opt
, reset
, reset
, line
, len
);
1261 if (ecbdata
->diff_words
1262 && ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
)
1263 fputs("~\n", ecbdata
->opt
->file
);
1267 if (ecbdata
->diff_words
) {
1268 if (line
[0] == '-') {
1269 diff_words_append(line
, len
,
1270 &ecbdata
->diff_words
->minus
);
1272 } else if (line
[0] == '+') {
1273 diff_words_append(line
, len
,
1274 &ecbdata
->diff_words
->plus
);
1276 } else if (starts_with(line
, "\\ ")) {
1278 * Eat the "no newline at eof" marker as if we
1279 * saw a "+" or "-" line with nothing on it,
1280 * and return without diff_words_flush() to
1281 * defer processing. If this is the end of
1282 * preimage, more "+" lines may come after it.
1286 diff_words_flush(ecbdata
);
1287 if (ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
) {
1288 emit_line(ecbdata
->opt
, context
, reset
, line
, len
);
1289 fputs("~\n", ecbdata
->opt
->file
);
1292 * Skip the prefix character, if any. With
1293 * diff_suppress_blank_empty, there may be
1296 if (line
[0] != '\n') {
1300 emit_line(ecbdata
->opt
, context
, reset
, line
, len
);
1307 ecbdata
->lno_in_postimage
++;
1308 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
1311 ecbdata
->lno_in_preimage
++;
1312 emit_del_line(reset
, ecbdata
, line
+ 1, len
- 1);
1315 ecbdata
->lno_in_postimage
++;
1316 ecbdata
->lno_in_preimage
++;
1317 emit_context_line(reset
, ecbdata
, line
+ 1, len
- 1);
1320 /* incomplete line at the end */
1321 ecbdata
->lno_in_preimage
++;
1322 emit_line(ecbdata
->opt
,
1323 diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
),
1329 static char *pprint_rename(const char *a
, const char *b
)
1331 const char *old
= a
;
1332 const char *new = b
;
1333 struct strbuf name
= STRBUF_INIT
;
1334 int pfx_length
, sfx_length
;
1335 int pfx_adjust_for_slash
;
1336 int len_a
= strlen(a
);
1337 int len_b
= strlen(b
);
1338 int a_midlen
, b_midlen
;
1339 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
1340 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
1342 if (qlen_a
|| qlen_b
) {
1343 quote_c_style(a
, &name
, NULL
, 0);
1344 strbuf_addstr(&name
, " => ");
1345 quote_c_style(b
, &name
, NULL
, 0);
1346 return strbuf_detach(&name
, NULL
);
1349 /* Find common prefix */
1351 while (*old
&& *new && *old
== *new) {
1353 pfx_length
= old
- a
+ 1;
1358 /* Find common suffix */
1363 * If there is a common prefix, it must end in a slash. In
1364 * that case we let this loop run 1 into the prefix to see the
1367 * If there is no common prefix, we cannot do this as it would
1368 * underrun the input strings.
1370 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
1371 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old
&&
1372 b
+ pfx_length
- pfx_adjust_for_slash
<= new &&
1375 sfx_length
= len_a
- (old
- a
);
1381 * pfx{mid-a => mid-b}sfx
1382 * {pfx-a => pfx-b}sfx
1383 * pfx{sfx-a => sfx-b}
1386 a_midlen
= len_a
- pfx_length
- sfx_length
;
1387 b_midlen
= len_b
- pfx_length
- sfx_length
;
1393 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
1394 if (pfx_length
+ sfx_length
) {
1395 strbuf_add(&name
, a
, pfx_length
);
1396 strbuf_addch(&name
, '{');
1398 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
1399 strbuf_addstr(&name
, " => ");
1400 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
1401 if (pfx_length
+ sfx_length
) {
1402 strbuf_addch(&name
, '}');
1403 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
1405 return strbuf_detach(&name
, NULL
);
1411 struct diffstat_file
{
1415 unsigned is_unmerged
:1;
1416 unsigned is_binary
:1;
1417 unsigned is_renamed
:1;
1418 unsigned is_interesting
:1;
1419 uintmax_t added
, deleted
;
1423 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
1427 struct diffstat_file
*x
;
1428 x
= xcalloc(1, sizeof(*x
));
1429 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
1430 diffstat
->files
[diffstat
->nr
++] = x
;
1432 x
->from_name
= xstrdup(name_a
);
1433 x
->name
= xstrdup(name_b
);
1437 x
->from_name
= NULL
;
1438 x
->name
= xstrdup(name_a
);
1443 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
1445 struct diffstat_t
*diffstat
= priv
;
1446 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
1450 else if (line
[0] == '-')
1454 const char mime_boundary_leader
[] = "------------";
1456 static int scale_linear(int it
, int width
, int max_change
)
1461 * make sure that at least one '-' or '+' is printed if
1462 * there is any change to this path. The easiest way is to
1463 * scale linearly as if the alloted width is one column shorter
1464 * than it is, and then add 1 to the result.
1466 return 1 + (it
* (width
- 1) / max_change
);
1469 static void show_name(FILE *file
,
1470 const char *prefix
, const char *name
, int len
)
1472 fprintf(file
, " %s%-*s |", prefix
, len
, name
);
1475 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
1479 fprintf(file
, "%s", set
);
1482 fprintf(file
, "%s", reset
);
1485 static void fill_print_name(struct diffstat_file
*file
)
1489 if (file
->print_name
)
1492 if (!file
->is_renamed
) {
1493 struct strbuf buf
= STRBUF_INIT
;
1494 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
1495 pname
= strbuf_detach(&buf
, NULL
);
1498 strbuf_release(&buf
);
1501 pname
= pprint_rename(file
->from_name
, file
->name
);
1503 file
->print_name
= pname
;
1506 int print_stat_summary(FILE *fp
, int files
, int insertions
, int deletions
)
1508 struct strbuf sb
= STRBUF_INIT
;
1512 assert(insertions
== 0 && deletions
== 0);
1513 return fprintf(fp
, "%s\n", " 0 files changed");
1517 (files
== 1) ? " %d file changed" : " %d files changed",
1521 * For binary diff, the caller may want to print "x files
1522 * changed" with insertions == 0 && deletions == 0.
1524 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
1525 * is probably less confusing (i.e skip over "2 files changed
1526 * but nothing about added/removed lines? Is this a bug in Git?").
1528 if (insertions
|| deletions
== 0) {
1530 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
1534 if (deletions
|| insertions
== 0) {
1536 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
1539 strbuf_addch(&sb
, '\n');
1540 ret
= fputs(sb
.buf
, fp
);
1541 strbuf_release(&sb
);
1545 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
1547 int i
, len
, add
, del
, adds
= 0, dels
= 0;
1548 uintmax_t max_change
= 0, max_len
= 0;
1549 int total_files
= data
->nr
, count
;
1550 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
1551 const char *reset
, *add_c
, *del_c
;
1552 const char *line_prefix
= "";
1553 int extra_shown
= 0;
1558 line_prefix
= diff_line_prefix(options
);
1559 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
1561 reset
= diff_get_color_opt(options
, DIFF_RESET
);
1562 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
1563 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
1566 * Find the longest filename and max number of changes
1568 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
1569 struct diffstat_file
*file
= data
->files
[i
];
1570 uintmax_t change
= file
->added
+ file
->deleted
;
1572 if (!file
->is_interesting
&& (change
== 0)) {
1573 count
++; /* not shown == room for one more */
1576 fill_print_name(file
);
1577 len
= strlen(file
->print_name
);
1581 if (file
->is_unmerged
) {
1582 /* "Unmerged" is 8 characters */
1583 bin_width
= bin_width
< 8 ? 8 : bin_width
;
1586 if (file
->is_binary
) {
1587 /* "Bin XXX -> YYY bytes" */
1588 int w
= 14 + decimal_width(file
->added
)
1589 + decimal_width(file
->deleted
);
1590 bin_width
= bin_width
< w
? w
: bin_width
;
1591 /* Display change counts aligned with "Bin" */
1596 if (max_change
< change
)
1597 max_change
= change
;
1599 count
= i
; /* where we can stop scanning in data->files[] */
1602 * We have width = stat_width or term_columns() columns total.
1603 * We want a maximum of min(max_len, stat_name_width) for the name part.
1604 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
1605 * We also need 1 for " " and 4 + decimal_width(max_change)
1606 * for " | NNNN " and one the empty column at the end, altogether
1607 * 6 + decimal_width(max_change).
1609 * If there's not enough space, we will use the smaller of
1610 * stat_name_width (if set) and 5/8*width for the filename,
1611 * and the rest for constant elements + graph part, but no more
1612 * than stat_graph_width for the graph part.
1613 * (5/8 gives 50 for filename and 30 for the constant parts + graph
1614 * for the standard terminal size).
1616 * In other words: stat_width limits the maximum width, and
1617 * stat_name_width fixes the maximum width of the filename,
1618 * and is also used to divide available columns if there
1621 * Binary files are displayed with "Bin XXX -> YYY bytes"
1622 * instead of the change count and graph. This part is treated
1623 * similarly to the graph part, except that it is not
1624 * "scaled". If total width is too small to accommodate the
1625 * guaranteed minimum width of the filename part and the
1626 * separators and this message, this message will "overflow"
1627 * making the line longer than the maximum width.
1630 if (options
->stat_width
== -1)
1631 width
= term_columns() - strlen(line_prefix
);
1633 width
= options
->stat_width
? options
->stat_width
: 80;
1634 number_width
= decimal_width(max_change
) > number_width
?
1635 decimal_width(max_change
) : number_width
;
1637 if (options
->stat_graph_width
== -1)
1638 options
->stat_graph_width
= diff_stat_graph_width
;
1641 * Guarantee 3/8*16==6 for the graph part
1642 * and 5/8*16==10 for the filename part
1644 if (width
< 16 + 6 + number_width
)
1645 width
= 16 + 6 + number_width
;
1648 * First assign sizes that are wanted, ignoring available width.
1649 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
1650 * starting from "XXX" should fit in graph_width.
1652 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
1653 if (options
->stat_graph_width
&&
1654 options
->stat_graph_width
< graph_width
)
1655 graph_width
= options
->stat_graph_width
;
1657 name_width
= (options
->stat_name_width
> 0 &&
1658 options
->stat_name_width
< max_len
) ?
1659 options
->stat_name_width
: max_len
;
1662 * Adjust adjustable widths not to exceed maximum width
1664 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
1665 if (graph_width
> width
* 3/8 - number_width
- 6) {
1666 graph_width
= width
* 3/8 - number_width
- 6;
1667 if (graph_width
< 6)
1671 if (options
->stat_graph_width
&&
1672 graph_width
> options
->stat_graph_width
)
1673 graph_width
= options
->stat_graph_width
;
1674 if (name_width
> width
- number_width
- 6 - graph_width
)
1675 name_width
= width
- number_width
- 6 - graph_width
;
1677 graph_width
= width
- number_width
- 6 - name_width
;
1681 * From here name_width is the width of the name area,
1682 * and graph_width is the width of the graph area.
1683 * max_change is used to scale graph properly.
1685 for (i
= 0; i
< count
; i
++) {
1686 const char *prefix
= "";
1687 struct diffstat_file
*file
= data
->files
[i
];
1688 char *name
= file
->print_name
;
1689 uintmax_t added
= file
->added
;
1690 uintmax_t deleted
= file
->deleted
;
1693 if (!file
->is_interesting
&& (added
+ deleted
== 0))
1697 * "scale" the filename
1700 name_len
= strlen(name
);
1701 if (name_width
< name_len
) {
1705 name
+= name_len
- len
;
1706 slash
= strchr(name
, '/');
1711 if (file
->is_binary
) {
1712 fprintf(options
->file
, "%s", line_prefix
);
1713 show_name(options
->file
, prefix
, name
, len
);
1714 fprintf(options
->file
, " %*s", number_width
, "Bin");
1715 if (!added
&& !deleted
) {
1716 putc('\n', options
->file
);
1719 fprintf(options
->file
, " %s%"PRIuMAX
"%s",
1720 del_c
, deleted
, reset
);
1721 fprintf(options
->file
, " -> ");
1722 fprintf(options
->file
, "%s%"PRIuMAX
"%s",
1723 add_c
, added
, reset
);
1724 fprintf(options
->file
, " bytes");
1725 fprintf(options
->file
, "\n");
1728 else if (file
->is_unmerged
) {
1729 fprintf(options
->file
, "%s", line_prefix
);
1730 show_name(options
->file
, prefix
, name
, len
);
1731 fprintf(options
->file
, " Unmerged\n");
1736 * scale the add/delete
1741 if (graph_width
<= max_change
) {
1742 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
1743 if (total
< 2 && add
&& del
)
1744 /* width >= 2 due to the sanity check */
1747 add
= scale_linear(add
, graph_width
, max_change
);
1750 del
= scale_linear(del
, graph_width
, max_change
);
1754 fprintf(options
->file
, "%s", line_prefix
);
1755 show_name(options
->file
, prefix
, name
, len
);
1756 fprintf(options
->file
, " %*"PRIuMAX
"%s",
1757 number_width
, added
+ deleted
,
1758 added
+ deleted
? " " : "");
1759 show_graph(options
->file
, '+', add
, add_c
, reset
);
1760 show_graph(options
->file
, '-', del
, del_c
, reset
);
1761 fprintf(options
->file
, "\n");
1764 for (i
= 0; i
< data
->nr
; i
++) {
1765 struct diffstat_file
*file
= data
->files
[i
];
1766 uintmax_t added
= file
->added
;
1767 uintmax_t deleted
= file
->deleted
;
1769 if (file
->is_unmerged
||
1770 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
1775 if (!file
->is_binary
) {
1782 fprintf(options
->file
, "%s ...\n", line_prefix
);
1785 fprintf(options
->file
, "%s", line_prefix
);
1786 print_stat_summary(options
->file
, total_files
, adds
, dels
);
1789 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
1791 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
1796 for (i
= 0; i
< data
->nr
; i
++) {
1797 int added
= data
->files
[i
]->added
;
1798 int deleted
= data
->files
[i
]->deleted
;
1800 if (data
->files
[i
]->is_unmerged
||
1801 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
1803 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
1808 fprintf(options
->file
, "%s", diff_line_prefix(options
));
1809 print_stat_summary(options
->file
, total_files
, adds
, dels
);
1812 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
1819 for (i
= 0; i
< data
->nr
; i
++) {
1820 struct diffstat_file
*file
= data
->files
[i
];
1822 fprintf(options
->file
, "%s", diff_line_prefix(options
));
1824 if (file
->is_binary
)
1825 fprintf(options
->file
, "-\t-\t");
1827 fprintf(options
->file
,
1828 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
1829 file
->added
, file
->deleted
);
1830 if (options
->line_termination
) {
1831 fill_print_name(file
);
1832 if (!file
->is_renamed
)
1833 write_name_quoted(file
->name
, options
->file
,
1834 options
->line_termination
);
1836 fputs(file
->print_name
, options
->file
);
1837 putc(options
->line_termination
, options
->file
);
1840 if (file
->is_renamed
) {
1841 putc('\0', options
->file
);
1842 write_name_quoted(file
->from_name
, options
->file
, '\0');
1844 write_name_quoted(file
->name
, options
->file
, '\0');
1849 struct dirstat_file
{
1851 unsigned long changed
;
1854 struct dirstat_dir
{
1855 struct dirstat_file
*files
;
1856 int alloc
, nr
, permille
, cumulative
;
1859 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
1860 unsigned long changed
, const char *base
, int baselen
)
1862 unsigned long this_dir
= 0;
1863 unsigned int sources
= 0;
1864 const char *line_prefix
= diff_line_prefix(opt
);
1867 struct dirstat_file
*f
= dir
->files
;
1868 int namelen
= strlen(f
->name
);
1872 if (namelen
< baselen
)
1874 if (memcmp(f
->name
, base
, baselen
))
1876 slash
= strchr(f
->name
+ baselen
, '/');
1878 int newbaselen
= slash
+ 1 - f
->name
;
1879 this = gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
1891 * We don't report dirstat's for
1893 * - or cases where everything came from a single directory
1894 * under this directory (sources == 1).
1896 if (baselen
&& sources
!= 1) {
1898 int permille
= this_dir
* 1000 / changed
;
1899 if (permille
>= dir
->permille
) {
1900 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
1901 permille
/ 10, permille
% 10, baselen
, base
);
1902 if (!dir
->cumulative
)
1910 static int dirstat_compare(const void *_a
, const void *_b
)
1912 const struct dirstat_file
*a
= _a
;
1913 const struct dirstat_file
*b
= _b
;
1914 return strcmp(a
->name
, b
->name
);
1917 static void show_dirstat(struct diff_options
*options
)
1920 unsigned long changed
;
1921 struct dirstat_dir dir
;
1922 struct diff_queue_struct
*q
= &diff_queued_diff
;
1927 dir
.permille
= options
->dirstat_permille
;
1928 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1931 for (i
= 0; i
< q
->nr
; i
++) {
1932 struct diff_filepair
*p
= q
->queue
[i
];
1934 unsigned long copied
, added
, damage
;
1935 int content_changed
;
1937 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
1939 if (p
->one
->oid_valid
&& p
->two
->oid_valid
)
1940 content_changed
= oidcmp(&p
->one
->oid
, &p
->two
->oid
);
1942 content_changed
= 1;
1944 if (!content_changed
) {
1946 * The SHA1 has not changed, so pre-/post-content is
1947 * identical. We can therefore skip looking at the
1948 * file contents altogether.
1954 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
)) {
1956 * In --dirstat-by-file mode, we don't really need to
1957 * look at the actual file contents at all.
1958 * The fact that the SHA1 changed is enough for us to
1959 * add this file to the list of results
1960 * (with each file contributing equal damage).
1966 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1967 diff_populate_filespec(p
->one
, 0);
1968 diff_populate_filespec(p
->two
, 0);
1969 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1971 diff_free_filespec_data(p
->one
);
1972 diff_free_filespec_data(p
->two
);
1973 } else if (DIFF_FILE_VALID(p
->one
)) {
1974 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
);
1976 diff_free_filespec_data(p
->one
);
1977 } else if (DIFF_FILE_VALID(p
->two
)) {
1978 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
);
1980 added
= p
->two
->size
;
1981 diff_free_filespec_data(p
->two
);
1986 * Original minus copied is the removed material,
1987 * added is the new material. They are both damages
1988 * made to the preimage.
1989 * If the resulting damage is zero, we know that
1990 * diffcore_count_changes() considers the two entries to
1991 * be identical, but since content_changed is true, we
1992 * know that there must have been _some_ kind of change,
1993 * so we force all entries to have damage > 0.
1995 damage
= (p
->one
->size
- copied
) + added
;
2000 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2001 dir
.files
[dir
.nr
].name
= name
;
2002 dir
.files
[dir
.nr
].changed
= damage
;
2007 /* This can happen even with many files, if everything was renames */
2011 /* Show all directories with more than x% of the changes */
2012 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
2013 gather_dirstat(options
, &dir
, changed
, "", 0);
2016 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
2019 unsigned long changed
;
2020 struct dirstat_dir dir
;
2028 dir
.permille
= options
->dirstat_permille
;
2029 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
2032 for (i
= 0; i
< data
->nr
; i
++) {
2033 struct diffstat_file
*file
= data
->files
[i
];
2034 unsigned long damage
= file
->added
+ file
->deleted
;
2035 if (file
->is_binary
)
2037 * binary files counts bytes, not lines. Must find some
2038 * way to normalize binary bytes vs. textual lines.
2039 * The following heuristic assumes that there are 64
2041 * This is stupid and ugly, but very cheap...
2043 damage
= (damage
+ 63) / 64;
2044 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2045 dir
.files
[dir
.nr
].name
= file
->name
;
2046 dir
.files
[dir
.nr
].changed
= damage
;
2051 /* This can happen even with many files, if everything was renames */
2055 /* Show all directories with more than x% of the changes */
2056 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
2057 gather_dirstat(options
, &dir
, changed
, "", 0);
2060 static void free_diffstat_info(struct diffstat_t
*diffstat
)
2063 for (i
= 0; i
< diffstat
->nr
; i
++) {
2064 struct diffstat_file
*f
= diffstat
->files
[i
];
2065 if (f
->name
!= f
->print_name
)
2066 free(f
->print_name
);
2071 free(diffstat
->files
);
2074 struct checkdiff_t
{
2075 const char *filename
;
2077 int conflict_marker_size
;
2078 struct diff_options
*o
;
2083 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
2088 if (len
< marker_size
+ 1)
2090 firstchar
= line
[0];
2091 switch (firstchar
) {
2092 case '=': case '>': case '<': case '|':
2097 for (cnt
= 1; cnt
< marker_size
; cnt
++)
2098 if (line
[cnt
] != firstchar
)
2100 /* line[1] thru line[marker_size-1] are same as firstchar */
2101 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
2106 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
2108 struct checkdiff_t
*data
= priv
;
2109 int marker_size
= data
->conflict_marker_size
;
2110 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
2111 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
2112 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
2114 const char *line_prefix
;
2117 line_prefix
= diff_line_prefix(data
->o
);
2119 if (line
[0] == '+') {
2122 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
2124 fprintf(data
->o
->file
,
2125 "%s%s:%d: leftover conflict marker\n",
2126 line_prefix
, data
->filename
, data
->lineno
);
2128 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
2131 data
->status
|= bad
;
2132 err
= whitespace_error_string(bad
);
2133 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
2134 line_prefix
, data
->filename
, data
->lineno
, err
);
2136 emit_line(data
->o
, set
, reset
, line
, 1);
2137 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
2138 data
->o
->file
, set
, reset
, ws
);
2139 } else if (line
[0] == ' ') {
2141 } else if (line
[0] == '@') {
2142 char *plus
= strchr(line
, '+');
2144 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
2146 die("invalid diff");
2150 static unsigned char *deflate_it(char *data
,
2152 unsigned long *result_size
)
2155 unsigned char *deflated
;
2158 git_deflate_init(&stream
, zlib_compression_level
);
2159 bound
= git_deflate_bound(&stream
, size
);
2160 deflated
= xmalloc(bound
);
2161 stream
.next_out
= deflated
;
2162 stream
.avail_out
= bound
;
2164 stream
.next_in
= (unsigned char *)data
;
2165 stream
.avail_in
= size
;
2166 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
2168 git_deflate_end(&stream
);
2169 *result_size
= stream
.total_out
;
2173 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
,
2180 unsigned long orig_size
;
2181 unsigned long delta_size
;
2182 unsigned long deflate_size
;
2183 unsigned long data_size
;
2185 /* We could do deflated delta, or we could do just deflated two,
2186 * whichever is smaller.
2189 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
2190 if (one
->size
&& two
->size
) {
2191 delta
= diff_delta(one
->ptr
, one
->size
,
2192 two
->ptr
, two
->size
,
2193 &delta_size
, deflate_size
);
2195 void *to_free
= delta
;
2196 orig_size
= delta_size
;
2197 delta
= deflate_it(delta
, delta_size
, &delta_size
);
2202 if (delta
&& delta_size
< deflate_size
) {
2203 fprintf(file
, "%sdelta %lu\n", prefix
, orig_size
);
2206 data_size
= delta_size
;
2209 fprintf(file
, "%sliteral %lu\n", prefix
, two
->size
);
2212 data_size
= deflate_size
;
2215 /* emit data encoded in base85 */
2218 int bytes
= (52 < data_size
) ? 52 : data_size
;
2222 line
[0] = bytes
+ 'A' - 1;
2224 line
[0] = bytes
- 26 + 'a' - 1;
2225 encode_85(line
+ 1, cp
, bytes
);
2226 cp
= (char *) cp
+ bytes
;
2227 fprintf(file
, "%s", prefix
);
2231 fprintf(file
, "%s\n", prefix
);
2235 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
,
2238 fprintf(file
, "%sGIT binary patch\n", prefix
);
2239 emit_binary_diff_body(file
, one
, two
, prefix
);
2240 emit_binary_diff_body(file
, two
, one
, prefix
);
2243 int diff_filespec_is_binary(struct diff_filespec
*one
)
2245 if (one
->is_binary
== -1) {
2246 diff_filespec_load_driver(one
);
2247 if (one
->driver
->binary
!= -1)
2248 one
->is_binary
= one
->driver
->binary
;
2250 if (!one
->data
&& DIFF_FILE_VALID(one
))
2251 diff_populate_filespec(one
, CHECK_BINARY
);
2252 if (one
->is_binary
== -1 && one
->data
)
2253 one
->is_binary
= buffer_is_binary(one
->data
,
2255 if (one
->is_binary
== -1)
2259 return one
->is_binary
;
2262 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
2264 diff_filespec_load_driver(one
);
2265 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
2268 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
2270 if (!options
->a_prefix
)
2271 options
->a_prefix
= a
;
2272 if (!options
->b_prefix
)
2273 options
->b_prefix
= b
;
2276 struct userdiff_driver
*get_textconv(struct diff_filespec
*one
)
2278 if (!DIFF_FILE_VALID(one
))
2281 diff_filespec_load_driver(one
);
2282 return userdiff_get_textconv(one
->driver
);
2285 static void builtin_diff(const char *name_a
,
2287 struct diff_filespec
*one
,
2288 struct diff_filespec
*two
,
2289 const char *xfrm_msg
,
2290 int must_show_header
,
2291 struct diff_options
*o
,
2292 int complete_rewrite
)
2296 char *a_one
, *b_two
;
2297 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
2298 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
2299 const char *a_prefix
, *b_prefix
;
2300 struct userdiff_driver
*textconv_one
= NULL
;
2301 struct userdiff_driver
*textconv_two
= NULL
;
2302 struct strbuf header
= STRBUF_INIT
;
2303 const char *line_prefix
= diff_line_prefix(o
);
2305 diff_set_mnemonic_prefix(o
, "a/", "b/");
2306 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
2307 a_prefix
= o
->b_prefix
;
2308 b_prefix
= o
->a_prefix
;
2310 a_prefix
= o
->a_prefix
;
2311 b_prefix
= o
->b_prefix
;
2314 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
2315 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
2316 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
2317 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2318 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2319 show_submodule_summary(o
->file
, one
->path
? one
->path
: two
->path
,
2321 &one
->oid
, &two
->oid
,
2322 two
->dirty_submodule
,
2323 meta
, del
, add
, reset
);
2325 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
2326 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
2327 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
2328 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2329 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2330 show_submodule_inline_diff(o
->file
, one
->path
? one
->path
: two
->path
,
2332 &one
->oid
, &two
->oid
,
2333 two
->dirty_submodule
,
2334 meta
, del
, add
, reset
, o
);
2338 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
2339 textconv_one
= get_textconv(one
);
2340 textconv_two
= get_textconv(two
);
2343 /* Never use a non-valid filename anywhere if at all possible */
2344 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
2345 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
2347 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
2348 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
2349 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
2350 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
2351 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
2352 if (lbl
[0][0] == '/') {
2354 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
2356 strbuf_addstr(&header
, xfrm_msg
);
2357 must_show_header
= 1;
2359 else if (lbl
[1][0] == '/') {
2360 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
2362 strbuf_addstr(&header
, xfrm_msg
);
2363 must_show_header
= 1;
2366 if (one
->mode
!= two
->mode
) {
2367 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
2368 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
2369 must_show_header
= 1;
2372 strbuf_addstr(&header
, xfrm_msg
);
2375 * we do not run diff between different kind
2378 if ((one
->mode
^ two
->mode
) & S_IFMT
)
2379 goto free_ab_and_return
;
2380 if (complete_rewrite
&&
2381 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
2382 (textconv_two
|| !diff_filespec_is_binary(two
))) {
2383 fprintf(o
->file
, "%s", header
.buf
);
2384 strbuf_reset(&header
);
2385 emit_rewrite_diff(name_a
, name_b
, one
, two
,
2386 textconv_one
, textconv_two
, o
);
2387 o
->found_changes
= 1;
2388 goto free_ab_and_return
;
2392 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
2393 fprintf(o
->file
, "%s", header
.buf
);
2394 strbuf_reset(&header
);
2395 goto free_ab_and_return
;
2396 } else if (!DIFF_OPT_TST(o
, TEXT
) &&
2397 ( (!textconv_one
&& diff_filespec_is_binary(one
)) ||
2398 (!textconv_two
&& diff_filespec_is_binary(two
)) )) {
2399 if (!one
->data
&& !two
->data
&&
2400 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
2401 !DIFF_OPT_TST(o
, BINARY
)) {
2402 if (!oidcmp(&one
->oid
, &two
->oid
)) {
2403 if (must_show_header
)
2404 fprintf(o
->file
, "%s", header
.buf
);
2405 goto free_ab_and_return
;
2407 fprintf(o
->file
, "%s", header
.buf
);
2408 fprintf(o
->file
, "%sBinary files %s and %s differ\n",
2409 line_prefix
, lbl
[0], lbl
[1]);
2410 goto free_ab_and_return
;
2412 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
2413 die("unable to read files to diff");
2414 /* Quite common confusing case */
2415 if (mf1
.size
== mf2
.size
&&
2416 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
2417 if (must_show_header
)
2418 fprintf(o
->file
, "%s", header
.buf
);
2419 goto free_ab_and_return
;
2421 fprintf(o
->file
, "%s", header
.buf
);
2422 strbuf_reset(&header
);
2423 if (DIFF_OPT_TST(o
, BINARY
))
2424 emit_binary_diff(o
->file
, &mf1
, &mf2
, line_prefix
);
2426 fprintf(o
->file
, "%sBinary files %s and %s differ\n",
2427 line_prefix
, lbl
[0], lbl
[1]);
2428 o
->found_changes
= 1;
2430 /* Crazy xdl interfaces.. */
2431 const char *diffopts
= getenv("GIT_DIFF_OPTS");
2435 struct emit_callback ecbdata
;
2436 const struct userdiff_funcname
*pe
;
2438 if (must_show_header
) {
2439 fprintf(o
->file
, "%s", header
.buf
);
2440 strbuf_reset(&header
);
2443 mf1
.size
= fill_textconv(textconv_one
, one
, &mf1
.ptr
);
2444 mf2
.size
= fill_textconv(textconv_two
, two
, &mf2
.ptr
);
2446 pe
= diff_funcname_pattern(one
);
2448 pe
= diff_funcname_pattern(two
);
2450 memset(&xpp
, 0, sizeof(xpp
));
2451 memset(&xecfg
, 0, sizeof(xecfg
));
2452 memset(&ecbdata
, 0, sizeof(ecbdata
));
2453 ecbdata
.label_path
= lbl
;
2454 ecbdata
.color_diff
= want_color(o
->use_color
);
2455 ecbdata
.found_changesp
= &o
->found_changes
;
2456 ecbdata
.ws_rule
= whitespace_rule(name_b
);
2457 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
2458 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
2460 ecbdata
.header
= header
.len
? &header
: NULL
;
2461 xpp
.flags
= o
->xdl_opts
;
2462 xecfg
.ctxlen
= o
->context
;
2463 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
2464 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2465 if (DIFF_OPT_TST(o
, FUNCCONTEXT
))
2466 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
2468 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
2471 else if (skip_prefix(diffopts
, "--unified=", &v
))
2472 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
2473 else if (skip_prefix(diffopts
, "-u", &v
))
2474 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
2476 init_diff_words_data(&ecbdata
, o
, one
, two
);
2477 if (xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
2479 die("unable to generate diff for %s", one
->path
);
2481 free_diff_words_data(&ecbdata
);
2486 xdiff_clear_find_func(&xecfg
);
2490 strbuf_release(&header
);
2491 diff_free_filespec_data(one
);
2492 diff_free_filespec_data(two
);
2498 static void builtin_diffstat(const char *name_a
, const char *name_b
,
2499 struct diff_filespec
*one
,
2500 struct diff_filespec
*two
,
2501 struct diffstat_t
*diffstat
,
2502 struct diff_options
*o
,
2503 struct diff_filepair
*p
)
2506 struct diffstat_file
*data
;
2508 int complete_rewrite
= 0;
2510 if (!DIFF_PAIR_UNMERGED(p
)) {
2511 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2512 complete_rewrite
= 1;
2515 data
= diffstat_add(diffstat
, name_a
, name_b
);
2516 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
2519 data
->is_unmerged
= 1;
2523 same_contents
= !oidcmp(&one
->oid
, &two
->oid
);
2525 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
2526 data
->is_binary
= 1;
2527 if (same_contents
) {
2531 data
->added
= diff_filespec_size(two
);
2532 data
->deleted
= diff_filespec_size(one
);
2536 else if (complete_rewrite
) {
2537 diff_populate_filespec(one
, 0);
2538 diff_populate_filespec(two
, 0);
2539 data
->deleted
= count_lines(one
->data
, one
->size
);
2540 data
->added
= count_lines(two
->data
, two
->size
);
2543 else if (!same_contents
) {
2544 /* Crazy xdl interfaces.. */
2548 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
2549 die("unable to read files to diff");
2551 memset(&xpp
, 0, sizeof(xpp
));
2552 memset(&xecfg
, 0, sizeof(xecfg
));
2553 xpp
.flags
= o
->xdl_opts
;
2554 xecfg
.ctxlen
= o
->context
;
2555 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
2556 if (xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
2558 die("unable to generate diffstat for %s", one
->path
);
2561 diff_free_filespec_data(one
);
2562 diff_free_filespec_data(two
);
2565 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
2566 const char *attr_path
,
2567 struct diff_filespec
*one
,
2568 struct diff_filespec
*two
,
2569 struct diff_options
*o
)
2572 struct checkdiff_t data
;
2577 memset(&data
, 0, sizeof(data
));
2578 data
.filename
= name_b
? name_b
: name_a
;
2581 data
.ws_rule
= whitespace_rule(attr_path
);
2582 data
.conflict_marker_size
= ll_merge_marker_size(attr_path
);
2584 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
2585 die("unable to read files to diff");
2588 * All the other codepaths check both sides, but not checking
2589 * the "old" side here is deliberate. We are checking the newly
2590 * introduced changes, and as long as the "new" side is text, we
2591 * can and should check what it introduces.
2593 if (diff_filespec_is_binary(two
))
2594 goto free_and_return
;
2596 /* Crazy xdl interfaces.. */
2600 memset(&xpp
, 0, sizeof(xpp
));
2601 memset(&xecfg
, 0, sizeof(xecfg
));
2602 xecfg
.ctxlen
= 1; /* at least one context line */
2604 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
2606 die("unable to generate checkdiff for %s", one
->path
);
2608 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
2609 struct emit_callback ecbdata
;
2612 ecbdata
.ws_rule
= data
.ws_rule
;
2613 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
2614 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
2619 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
2620 fprintf(o
->file
, "%s:%d: %s.\n",
2621 data
.filename
, blank_at_eof
, err
);
2622 data
.status
= 1; /* report errors */
2627 diff_free_filespec_data(one
);
2628 diff_free_filespec_data(two
);
2630 DIFF_OPT_SET(o
, CHECK_FAILED
);
2633 struct diff_filespec
*alloc_filespec(const char *path
)
2635 struct diff_filespec
*spec
;
2637 FLEXPTR_ALLOC_STR(spec
, path
, path
);
2639 spec
->is_binary
= -1;
2643 void free_filespec(struct diff_filespec
*spec
)
2645 if (!--spec
->count
) {
2646 diff_free_filespec_data(spec
);
2651 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
2652 int sha1_valid
, unsigned short mode
)
2655 spec
->mode
= canon_mode(mode
);
2656 hashcpy(spec
->oid
.hash
, sha1
);
2657 spec
->oid_valid
= sha1_valid
;
2662 * Given a name and sha1 pair, if the index tells us the file in
2663 * the work tree has that object contents, return true, so that
2664 * prepare_temp_file() does not have to inflate and extract.
2666 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
2668 const struct cache_entry
*ce
;
2673 * We do not read the cache ourselves here, because the
2674 * benchmark with my previous version that always reads cache
2675 * shows that it makes things worse for diff-tree comparing
2676 * two linux-2.6 kernel trees in an already checked out work
2677 * tree. This is because most diff-tree comparisons deal with
2678 * only a small number of files, while reading the cache is
2679 * expensive for a large project, and its cost outweighs the
2680 * savings we get by not inflating the object to a temporary
2681 * file. Practically, this code only helps when we are used
2682 * by diff-cache --cached, which does read the cache before
2688 /* We want to avoid the working directory if our caller
2689 * doesn't need the data in a normal file, this system
2690 * is rather slow with its stat/open/mmap/close syscalls,
2691 * and the object is contained in a pack file. The pack
2692 * is probably already open and will be faster to obtain
2693 * the data through than the working directory. Loose
2694 * objects however would tend to be slower as they need
2695 * to be individually opened and inflated.
2697 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
))
2701 * Similarly, if we'd have to convert the file contents anyway, that
2702 * makes the optimization not worthwhile.
2704 if (!want_file
&& would_convert_to_git(name
))
2708 pos
= cache_name_pos(name
, len
);
2711 ce
= active_cache
[pos
];
2714 * This is not the sha1 we are looking for, or
2715 * unreusable because it is not a regular file.
2717 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
2721 * If ce is marked as "assume unchanged", there is no
2722 * guarantee that work tree matches what we are looking for.
2724 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2728 * If ce matches the file in the work tree, we can reuse it.
2730 if (ce_uptodate(ce
) ||
2731 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
2737 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
2739 struct strbuf buf
= STRBUF_INIT
;
2742 /* Are we looking at the work tree? */
2743 if (s
->dirty_submodule
)
2746 strbuf_addf(&buf
, "Subproject commit %s%s\n",
2747 oid_to_hex(&s
->oid
), dirty
);
2751 strbuf_release(&buf
);
2753 s
->data
= strbuf_detach(&buf
, NULL
);
2760 * While doing rename detection and pickaxe operation, we may need to
2761 * grab the data for the blob (or file) for our own in-core comparison.
2762 * diff_filespec has data and size fields for this purpose.
2764 int diff_populate_filespec(struct diff_filespec
*s
, unsigned int flags
)
2766 int size_only
= flags
& CHECK_SIZE_ONLY
;
2769 * demote FAIL to WARN to allow inspecting the situation
2770 * instead of refusing.
2772 enum safe_crlf crlf_warn
= (safe_crlf
== SAFE_CRLF_FAIL
2776 if (!DIFF_FILE_VALID(s
))
2777 die("internal error: asking to populate invalid file.");
2778 if (S_ISDIR(s
->mode
))
2784 if (size_only
&& 0 < s
->size
)
2787 if (S_ISGITLINK(s
->mode
))
2788 return diff_populate_gitlink(s
, size_only
);
2790 if (!s
->oid_valid
||
2791 reuse_worktree_file(s
->path
, s
->oid
.hash
, 0)) {
2792 struct strbuf buf
= STRBUF_INIT
;
2796 if (lstat(s
->path
, &st
) < 0) {
2797 if (errno
== ENOENT
) {
2801 s
->data
= (char *)"";
2806 s
->size
= xsize_t(st
.st_size
);
2809 if (S_ISLNK(st
.st_mode
)) {
2810 struct strbuf sb
= STRBUF_INIT
;
2812 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
2815 s
->data
= strbuf_detach(&sb
, NULL
);
2821 if ((flags
& CHECK_BINARY
) &&
2822 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
2826 fd
= open(s
->path
, O_RDONLY
);
2829 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2831 s
->should_munmap
= 1;
2834 * Convert from working tree format to canonical git format
2836 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, crlf_warn
)) {
2838 munmap(s
->data
, s
->size
);
2839 s
->should_munmap
= 0;
2840 s
->data
= strbuf_detach(&buf
, &size
);
2846 enum object_type type
;
2847 if (size_only
|| (flags
& CHECK_BINARY
)) {
2848 type
= sha1_object_info(s
->oid
.hash
, &s
->size
);
2850 die("unable to read %s",
2851 oid_to_hex(&s
->oid
));
2854 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
2859 s
->data
= read_sha1_file(s
->oid
.hash
, &type
, &s
->size
);
2861 die("unable to read %s", oid_to_hex(&s
->oid
));
2867 void diff_free_filespec_blob(struct diff_filespec
*s
)
2871 else if (s
->should_munmap
)
2872 munmap(s
->data
, s
->size
);
2874 if (s
->should_free
|| s
->should_munmap
) {
2875 s
->should_free
= s
->should_munmap
= 0;
2880 void diff_free_filespec_data(struct diff_filespec
*s
)
2882 diff_free_filespec_blob(s
);
2887 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
2890 const struct object_id
*oid
,
2894 struct strbuf buf
= STRBUF_INIT
;
2895 struct strbuf
template = STRBUF_INIT
;
2896 char *path_dup
= xstrdup(path
);
2897 const char *base
= basename(path_dup
);
2899 /* Generate "XXXXXX_basename.ext" */
2900 strbuf_addstr(&template, "XXXXXX_");
2901 strbuf_addstr(&template, base
);
2903 fd
= mks_tempfile_ts(&temp
->tempfile
, template.buf
, strlen(base
) + 1);
2905 die_errno("unable to create temp-file");
2906 if (convert_to_working_tree(path
,
2907 (const char *)blob
, (size_t)size
, &buf
)) {
2911 if (write_in_full(fd
, blob
, size
) != size
)
2912 die_errno("unable to write temp-file");
2913 close_tempfile(&temp
->tempfile
);
2914 temp
->name
= get_tempfile_path(&temp
->tempfile
);
2915 oid_to_hex_r(temp
->hex
, oid
);
2916 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
2917 strbuf_release(&buf
);
2918 strbuf_release(&template);
2922 static struct diff_tempfile
*prepare_temp_file(const char *name
,
2923 struct diff_filespec
*one
)
2925 struct diff_tempfile
*temp
= claim_diff_tempfile();
2927 if (!DIFF_FILE_VALID(one
)) {
2929 /* A '-' entry produces this for file-2, and
2930 * a '+' entry produces this for file-1.
2932 temp
->name
= "/dev/null";
2933 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
2934 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
2938 if (!S_ISGITLINK(one
->mode
) &&
2940 reuse_worktree_file(name
, one
->oid
.hash
, 1))) {
2942 if (lstat(name
, &st
) < 0) {
2943 if (errno
== ENOENT
)
2944 goto not_a_valid_file
;
2945 die_errno("stat(%s)", name
);
2947 if (S_ISLNK(st
.st_mode
)) {
2948 struct strbuf sb
= STRBUF_INIT
;
2949 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
2950 die_errno("readlink(%s)", name
);
2951 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
2953 &one
->oid
: &null_oid
),
2955 one
->mode
: S_IFLNK
));
2956 strbuf_release(&sb
);
2959 /* we can borrow from the file in the work tree */
2961 if (!one
->oid_valid
)
2962 sha1_to_hex_r(temp
->hex
, null_sha1
);
2964 sha1_to_hex_r(temp
->hex
, one
->oid
.hash
);
2965 /* Even though we may sometimes borrow the
2966 * contents from the work tree, we always want
2967 * one->mode. mode is trustworthy even when
2968 * !(one->sha1_valid), as long as
2969 * DIFF_FILE_VALID(one).
2971 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
2976 if (diff_populate_filespec(one
, 0))
2977 die("cannot read data blob for %s", one
->path
);
2978 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
2979 &one
->oid
, one
->mode
);
2984 static void add_external_diff_name(struct argv_array
*argv
,
2986 struct diff_filespec
*df
)
2988 struct diff_tempfile
*temp
= prepare_temp_file(name
, df
);
2989 argv_array_push(argv
, temp
->name
);
2990 argv_array_push(argv
, temp
->hex
);
2991 argv_array_push(argv
, temp
->mode
);
2994 /* An external diff command takes:
2996 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2997 * infile2 infile2-sha1 infile2-mode [ rename-to ]
3000 static void run_external_diff(const char *pgm
,
3003 struct diff_filespec
*one
,
3004 struct diff_filespec
*two
,
3005 const char *xfrm_msg
,
3006 int complete_rewrite
,
3007 struct diff_options
*o
)
3009 struct argv_array argv
= ARGV_ARRAY_INIT
;
3010 struct argv_array env
= ARGV_ARRAY_INIT
;
3011 struct diff_queue_struct
*q
= &diff_queued_diff
;
3013 argv_array_push(&argv
, pgm
);
3014 argv_array_push(&argv
, name
);
3017 add_external_diff_name(&argv
, name
, one
);
3019 add_external_diff_name(&argv
, name
, two
);
3021 add_external_diff_name(&argv
, other
, two
);
3022 argv_array_push(&argv
, other
);
3023 argv_array_push(&argv
, xfrm_msg
);
3027 argv_array_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
3028 argv_array_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
3030 if (run_command_v_opt_cd_env(argv
.argv
, RUN_USING_SHELL
, NULL
, env
.argv
))
3031 die(_("external diff died, stopping at %s"), name
);
3034 argv_array_clear(&argv
);
3035 argv_array_clear(&env
);
3038 static int similarity_index(struct diff_filepair
*p
)
3040 return p
->score
* 100 / MAX_SCORE
;
3043 static void fill_metainfo(struct strbuf
*msg
,
3046 struct diff_filespec
*one
,
3047 struct diff_filespec
*two
,
3048 struct diff_options
*o
,
3049 struct diff_filepair
*p
,
3050 int *must_show_header
,
3053 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
3054 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
3055 const char *line_prefix
= diff_line_prefix(o
);
3057 *must_show_header
= 1;
3058 strbuf_init(msg
, PATH_MAX
* 2 + 300);
3059 switch (p
->status
) {
3060 case DIFF_STATUS_COPIED
:
3061 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3062 line_prefix
, set
, similarity_index(p
));
3063 strbuf_addf(msg
, "%s\n%s%scopy from ",
3064 reset
, line_prefix
, set
);
3065 quote_c_style(name
, msg
, NULL
, 0);
3066 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
3067 quote_c_style(other
, msg
, NULL
, 0);
3068 strbuf_addf(msg
, "%s\n", reset
);
3070 case DIFF_STATUS_RENAMED
:
3071 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3072 line_prefix
, set
, similarity_index(p
));
3073 strbuf_addf(msg
, "%s\n%s%srename from ",
3074 reset
, line_prefix
, set
);
3075 quote_c_style(name
, msg
, NULL
, 0);
3076 strbuf_addf(msg
, "%s\n%s%srename to ",
3077 reset
, line_prefix
, set
);
3078 quote_c_style(other
, msg
, NULL
, 0);
3079 strbuf_addf(msg
, "%s\n", reset
);
3081 case DIFF_STATUS_MODIFIED
:
3083 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
3085 set
, similarity_index(p
), reset
);
3090 *must_show_header
= 0;
3092 if (one
&& two
&& oidcmp(&one
->oid
, &two
->oid
)) {
3093 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
3095 if (DIFF_OPT_TST(o
, BINARY
)) {
3097 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
3098 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
3101 strbuf_addf(msg
, "%s%sindex %s..", line_prefix
, set
,
3102 find_unique_abbrev(one
->oid
.hash
, abbrev
));
3103 strbuf_addstr(msg
, find_unique_abbrev(two
->oid
.hash
, abbrev
));
3104 if (one
->mode
== two
->mode
)
3105 strbuf_addf(msg
, " %06o", one
->mode
);
3106 strbuf_addf(msg
, "%s\n", reset
);
3110 static void run_diff_cmd(const char *pgm
,
3113 const char *attr_path
,
3114 struct diff_filespec
*one
,
3115 struct diff_filespec
*two
,
3117 struct diff_options
*o
,
3118 struct diff_filepair
*p
)
3120 const char *xfrm_msg
= NULL
;
3121 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
3122 int must_show_header
= 0;
3125 if (DIFF_OPT_TST(o
, ALLOW_EXTERNAL
)) {
3126 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
3127 if (drv
&& drv
->external
)
3128 pgm
= drv
->external
;
3133 * don't use colors when the header is intended for an
3134 * external diff driver
3136 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
3138 want_color(o
->use_color
) && !pgm
);
3139 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
3143 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
3144 complete_rewrite
, o
);
3148 builtin_diff(name
, other
? other
: name
,
3149 one
, two
, xfrm_msg
, must_show_header
,
3150 o
, complete_rewrite
);
3152 fprintf(o
->file
, "* Unmerged path %s\n", name
);
3155 static void diff_fill_sha1_info(struct diff_filespec
*one
)
3157 if (DIFF_FILE_VALID(one
)) {
3158 if (!one
->oid_valid
) {
3160 if (one
->is_stdin
) {
3164 if (lstat(one
->path
, &st
) < 0)
3165 die_errno("stat '%s'", one
->path
);
3166 if (index_path(one
->oid
.hash
, one
->path
, &st
, 0))
3167 die("cannot hash %s", one
->path
);
3174 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
3176 /* Strip the prefix but do not molest /dev/null and absolute paths */
3177 if (*namep
&& **namep
!= '/') {
3178 *namep
+= prefix_length
;
3182 if (*otherp
&& **otherp
!= '/') {
3183 *otherp
+= prefix_length
;
3184 if (**otherp
== '/')
3189 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
3191 const char *pgm
= external_diff();
3193 struct diff_filespec
*one
= p
->one
;
3194 struct diff_filespec
*two
= p
->two
;
3197 const char *attr_path
;
3199 name
= p
->one
->path
;
3200 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
3202 if (o
->prefix_length
)
3203 strip_prefix(o
->prefix_length
, &name
, &other
);
3205 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
3208 if (DIFF_PAIR_UNMERGED(p
)) {
3209 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
3210 NULL
, NULL
, NULL
, o
, p
);
3214 diff_fill_sha1_info(one
);
3215 diff_fill_sha1_info(two
);
3218 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
3219 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
3221 * a filepair that changes between file and symlink
3222 * needs to be split into deletion and creation.
3224 struct diff_filespec
*null
= alloc_filespec(two
->path
);
3225 run_diff_cmd(NULL
, name
, other
, attr_path
,
3226 one
, null
, &msg
, o
, p
);
3228 strbuf_release(&msg
);
3230 null
= alloc_filespec(one
->path
);
3231 run_diff_cmd(NULL
, name
, other
, attr_path
,
3232 null
, two
, &msg
, o
, p
);
3236 run_diff_cmd(pgm
, name
, other
, attr_path
,
3237 one
, two
, &msg
, o
, p
);
3239 strbuf_release(&msg
);
3242 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
3243 struct diffstat_t
*diffstat
)
3248 if (DIFF_PAIR_UNMERGED(p
)) {
3250 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, p
);
3254 name
= p
->one
->path
;
3255 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
3257 if (o
->prefix_length
)
3258 strip_prefix(o
->prefix_length
, &name
, &other
);
3260 diff_fill_sha1_info(p
->one
);
3261 diff_fill_sha1_info(p
->two
);
3263 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, p
);
3266 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
3270 const char *attr_path
;
3272 if (DIFF_PAIR_UNMERGED(p
)) {
3277 name
= p
->one
->path
;
3278 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
3279 attr_path
= other
? other
: name
;
3281 if (o
->prefix_length
)
3282 strip_prefix(o
->prefix_length
, &name
, &other
);
3284 diff_fill_sha1_info(p
->one
);
3285 diff_fill_sha1_info(p
->two
);
3287 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
3290 void diff_setup(struct diff_options
*options
)
3292 memcpy(options
, &default_diff_options
, sizeof(*options
));
3294 options
->file
= stdout
;
3296 options
->line_termination
= '\n';
3297 options
->break_opt
= -1;
3298 options
->rename_limit
= -1;
3299 options
->dirstat_permille
= diff_dirstat_permille_default
;
3300 options
->context
= diff_context_default
;
3301 options
->ws_error_highlight
= WSEH_NEW
;
3302 DIFF_OPT_SET(options
, RENAME_EMPTY
);
3304 /* pathchange left =NULL by default */
3305 options
->change
= diff_change
;
3306 options
->add_remove
= diff_addremove
;
3307 options
->use_color
= diff_use_color_default
;
3308 options
->detect_rename
= diff_detect_rename_default
;
3309 options
->xdl_opts
|= diff_algorithm
;
3310 if (diff_compaction_heuristic
)
3311 DIFF_XDL_SET(options
, COMPACTION_HEURISTIC
);
3313 options
->orderfile
= diff_order_file_cfg
;
3315 if (diff_no_prefix
) {
3316 options
->a_prefix
= options
->b_prefix
= "";
3317 } else if (!diff_mnemonic_prefix
) {
3318 options
->a_prefix
= "a/";
3319 options
->b_prefix
= "b/";
3323 void diff_setup_done(struct diff_options
*options
)
3327 if (options
->set_default
)
3328 options
->set_default(options
);
3330 if (options
->output_format
& DIFF_FORMAT_NAME
)
3332 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
3334 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
3336 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
3339 die("--name-only, --name-status, --check and -s are mutually exclusive");
3342 * Most of the time we can say "there are changes"
3343 * only by checking if there are changed paths, but
3344 * --ignore-whitespace* options force us to look
3348 if (DIFF_XDL_TST(options
, IGNORE_WHITESPACE
) ||
3349 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_CHANGE
) ||
3350 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_AT_EOL
))
3351 DIFF_OPT_SET(options
, DIFF_FROM_CONTENTS
);
3353 DIFF_OPT_CLR(options
, DIFF_FROM_CONTENTS
);
3355 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
3356 options
->detect_rename
= DIFF_DETECT_COPY
;
3358 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
3359 options
->prefix
= NULL
;
3360 if (options
->prefix
)
3361 options
->prefix_length
= strlen(options
->prefix
);
3363 options
->prefix_length
= 0;
3365 if (options
->output_format
& (DIFF_FORMAT_NAME
|
3366 DIFF_FORMAT_NAME_STATUS
|
3367 DIFF_FORMAT_CHECKDIFF
|
3368 DIFF_FORMAT_NO_OUTPUT
))
3369 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
3370 DIFF_FORMAT_NUMSTAT
|
3371 DIFF_FORMAT_DIFFSTAT
|
3372 DIFF_FORMAT_SHORTSTAT
|
3373 DIFF_FORMAT_DIRSTAT
|
3374 DIFF_FORMAT_SUMMARY
|
3378 * These cases always need recursive; we do not drop caller-supplied
3379 * recursive bits for other formats here.
3381 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
3382 DIFF_FORMAT_NUMSTAT
|
3383 DIFF_FORMAT_DIFFSTAT
|
3384 DIFF_FORMAT_SHORTSTAT
|
3385 DIFF_FORMAT_DIRSTAT
|
3386 DIFF_FORMAT_SUMMARY
|
3387 DIFF_FORMAT_CHECKDIFF
))
3388 DIFF_OPT_SET(options
, RECURSIVE
);
3390 * Also pickaxe would not work very well if you do not say recursive
3392 if (options
->pickaxe
)
3393 DIFF_OPT_SET(options
, RECURSIVE
);
3395 * When patches are generated, submodules diffed against the work tree
3396 * must be checked for dirtiness too so it can be shown in the output
3398 if (options
->output_format
& DIFF_FORMAT_PATCH
)
3399 DIFF_OPT_SET(options
, DIRTY_SUBMODULES
);
3401 if (options
->detect_rename
&& options
->rename_limit
< 0)
3402 options
->rename_limit
= diff_rename_limit_default
;
3403 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
3405 /* read-cache does not die even when it fails
3406 * so it is safe for us to do this here. Also
3407 * it does not smudge active_cache or active_nr
3408 * when it fails, so we do not have to worry about
3409 * cleaning it up ourselves either.
3413 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
3414 options
->abbrev
= 40; /* full */
3417 * It does not make sense to show the first hit we happened
3418 * to have found. It does not make sense not to return with
3419 * exit code in such a case either.
3421 if (DIFF_OPT_TST(options
, QUICK
)) {
3422 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
3423 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
3426 options
->diff_path_counter
= 0;
3428 if (DIFF_OPT_TST(options
, FOLLOW_RENAMES
) && options
->pathspec
.nr
!= 1)
3429 die(_("--follow requires exactly one pathspec"));
3432 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
3442 if (c
== arg_short
) {
3446 if (val
&& isdigit(c
)) {
3448 int n
= strtoul(arg
, &end
, 10);
3459 eq
= strchrnul(arg
, '=');
3461 if (!len
|| strncmp(arg
, arg_long
, len
))
3466 if (!isdigit(*++eq
))
3468 n
= strtoul(eq
, &end
, 10);
3476 static int diff_scoreopt_parse(const char *opt
);
3478 static inline int short_opt(char opt
, const char **argv
,
3479 const char **optarg
)
3481 const char *arg
= argv
[0];
3482 if (arg
[0] != '-' || arg
[1] != opt
)
3484 if (arg
[2] != '\0') {
3489 die("Option '%c' requires a value", opt
);
3494 int parse_long_opt(const char *opt
, const char **argv
,
3495 const char **optarg
)
3497 const char *arg
= argv
[0];
3498 if (!skip_prefix(arg
, "--", &arg
))
3500 if (!skip_prefix(arg
, opt
, &arg
))
3502 if (*arg
== '=') { /* stuck form: --option=value */
3508 /* separate form: --option value */
3510 die("Option '--%s' requires a value", opt
);
3515 static int stat_opt(struct diff_options
*options
, const char **av
)
3517 const char *arg
= av
[0];
3519 int width
= options
->stat_width
;
3520 int name_width
= options
->stat_name_width
;
3521 int graph_width
= options
->stat_graph_width
;
3522 int count
= options
->stat_count
;
3525 if (!skip_prefix(arg
, "--stat", &arg
))
3526 die("BUG: stat option does not begin with --stat: %s", arg
);
3531 if (skip_prefix(arg
, "-width", &arg
)) {
3533 width
= strtoul(arg
+ 1, &end
, 10);
3534 else if (!*arg
&& !av
[1])
3535 die("Option '--stat-width' requires a value");
3537 width
= strtoul(av
[1], &end
, 10);
3540 } else if (skip_prefix(arg
, "-name-width", &arg
)) {
3542 name_width
= strtoul(arg
+ 1, &end
, 10);
3543 else if (!*arg
&& !av
[1])
3544 die("Option '--stat-name-width' requires a value");
3546 name_width
= strtoul(av
[1], &end
, 10);
3549 } else if (skip_prefix(arg
, "-graph-width", &arg
)) {
3551 graph_width
= strtoul(arg
+ 1, &end
, 10);
3552 else if (!*arg
&& !av
[1])
3553 die("Option '--stat-graph-width' requires a value");
3555 graph_width
= strtoul(av
[1], &end
, 10);
3558 } else if (skip_prefix(arg
, "-count", &arg
)) {
3560 count
= strtoul(arg
+ 1, &end
, 10);
3561 else if (!*arg
&& !av
[1])
3562 die("Option '--stat-count' requires a value");
3564 count
= strtoul(av
[1], &end
, 10);
3570 width
= strtoul(arg
+1, &end
, 10);
3572 name_width
= strtoul(end
+1, &end
, 10);
3574 count
= strtoul(end
+1, &end
, 10);
3577 /* Important! This checks all the error cases! */
3580 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
3581 options
->stat_name_width
= name_width
;
3582 options
->stat_graph_width
= graph_width
;
3583 options
->stat_width
= width
;
3584 options
->stat_count
= count
;
3588 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
3590 struct strbuf errmsg
= STRBUF_INIT
;
3591 if (parse_dirstat_params(options
, params
, &errmsg
))
3592 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
3594 strbuf_release(&errmsg
);
3596 * The caller knows a dirstat-related option is given from the command
3597 * line; allow it to say "return this_function();"
3599 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
3603 static int parse_submodule_opt(struct diff_options
*options
, const char *value
)
3605 if (parse_submodule_params(options
, value
))
3606 die(_("Failed to parse --submodule option parameter: '%s'"),
3611 static const char diff_status_letters
[] = {
3614 DIFF_STATUS_DELETED
,
3615 DIFF_STATUS_MODIFIED
,
3616 DIFF_STATUS_RENAMED
,
3617 DIFF_STATUS_TYPE_CHANGED
,
3618 DIFF_STATUS_UNKNOWN
,
3619 DIFF_STATUS_UNMERGED
,
3620 DIFF_STATUS_FILTER_AON
,
3621 DIFF_STATUS_FILTER_BROKEN
,
3625 static unsigned int filter_bit
['Z' + 1];
3627 static void prepare_filter_bits(void)
3631 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
3632 for (i
= 0; diff_status_letters
[i
]; i
++)
3633 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
3637 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
3639 return opt
->filter
& filter_bit
[(int) status
];
3642 static int parse_diff_filter_opt(const char *optarg
, struct diff_options
*opt
)
3646 prepare_filter_bits();
3649 * If there is a negation e.g. 'd' in the input, and we haven't
3650 * initialized the filter field with another --diff-filter, start
3651 * from full set of bits, except for AON.
3654 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
3655 if (optch
< 'a' || 'z' < optch
)
3657 opt
->filter
= (1 << (ARRAY_SIZE(diff_status_letters
) - 1)) - 1;
3658 opt
->filter
&= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
3663 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
3667 if ('a' <= optch
&& optch
<= 'z') {
3669 optch
= toupper(optch
);
3674 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
3678 opt
->filter
&= ~bit
;
3685 static void enable_patch_output(int *fmt
) {
3686 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
3687 *fmt
|= DIFF_FORMAT_PATCH
;
3690 static int parse_one_token(const char **arg
, const char *token
)
3693 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
3700 static int parse_ws_error_highlight(struct diff_options
*opt
, const char *arg
)
3702 const char *orig_arg
= arg
;
3705 if (parse_one_token(&arg
, "none"))
3707 else if (parse_one_token(&arg
, "default"))
3709 else if (parse_one_token(&arg
, "all"))
3710 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
3711 else if (parse_one_token(&arg
, "new"))
3713 else if (parse_one_token(&arg
, "old"))
3715 else if (parse_one_token(&arg
, "context"))
3716 val
|= WSEH_CONTEXT
;
3718 error("unknown value after ws-error-highlight=%.*s",
3719 (int)(arg
- orig_arg
), orig_arg
);
3725 opt
->ws_error_highlight
= val
;
3729 int diff_opt_parse(struct diff_options
*options
,
3730 const char **av
, int ac
, const char *prefix
)
3732 const char *arg
= av
[0];
3739 /* Output format options */
3740 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u") || !strcmp(arg
, "--patch")
3741 || opt_arg(arg
, 'U', "unified", &options
->context
))
3742 enable_patch_output(&options
->output_format
);
3743 else if (!strcmp(arg
, "--raw"))
3744 options
->output_format
|= DIFF_FORMAT_RAW
;
3745 else if (!strcmp(arg
, "--patch-with-raw")) {
3746 enable_patch_output(&options
->output_format
);
3747 options
->output_format
|= DIFF_FORMAT_RAW
;
3748 } else if (!strcmp(arg
, "--numstat"))
3749 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
3750 else if (!strcmp(arg
, "--shortstat"))
3751 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
3752 else if (!strcmp(arg
, "-X") || !strcmp(arg
, "--dirstat"))
3753 return parse_dirstat_opt(options
, "");
3754 else if (skip_prefix(arg
, "-X", &arg
))
3755 return parse_dirstat_opt(options
, arg
);
3756 else if (skip_prefix(arg
, "--dirstat=", &arg
))
3757 return parse_dirstat_opt(options
, arg
);
3758 else if (!strcmp(arg
, "--cumulative"))
3759 return parse_dirstat_opt(options
, "cumulative");
3760 else if (!strcmp(arg
, "--dirstat-by-file"))
3761 return parse_dirstat_opt(options
, "files");
3762 else if (skip_prefix(arg
, "--dirstat-by-file=", &arg
)) {
3763 parse_dirstat_opt(options
, "files");
3764 return parse_dirstat_opt(options
, arg
);
3766 else if (!strcmp(arg
, "--check"))
3767 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
3768 else if (!strcmp(arg
, "--summary"))
3769 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
3770 else if (!strcmp(arg
, "--patch-with-stat")) {
3771 enable_patch_output(&options
->output_format
);
3772 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
3773 } else if (!strcmp(arg
, "--name-only"))
3774 options
->output_format
|= DIFF_FORMAT_NAME
;
3775 else if (!strcmp(arg
, "--name-status"))
3776 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
3777 else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--no-patch"))
3778 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
3779 else if (starts_with(arg
, "--stat"))
3780 /* --stat, --stat-width, --stat-name-width, or --stat-count */
3781 return stat_opt(options
, av
);
3783 /* renames options */
3784 else if (starts_with(arg
, "-B") || starts_with(arg
, "--break-rewrites=") ||
3785 !strcmp(arg
, "--break-rewrites")) {
3786 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
3787 return error("invalid argument to -B: %s", arg
+2);
3789 else if (starts_with(arg
, "-M") || starts_with(arg
, "--find-renames=") ||
3790 !strcmp(arg
, "--find-renames")) {
3791 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
3792 return error("invalid argument to -M: %s", arg
+2);
3793 options
->detect_rename
= DIFF_DETECT_RENAME
;
3795 else if (!strcmp(arg
, "-D") || !strcmp(arg
, "--irreversible-delete")) {
3796 options
->irreversible_delete
= 1;
3798 else if (starts_with(arg
, "-C") || starts_with(arg
, "--find-copies=") ||
3799 !strcmp(arg
, "--find-copies")) {
3800 if (options
->detect_rename
== DIFF_DETECT_COPY
)
3801 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
3802 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
3803 return error("invalid argument to -C: %s", arg
+2);
3804 options
->detect_rename
= DIFF_DETECT_COPY
;
3806 else if (!strcmp(arg
, "--no-renames"))
3807 options
->detect_rename
= 0;
3808 else if (!strcmp(arg
, "--rename-empty"))
3809 DIFF_OPT_SET(options
, RENAME_EMPTY
);
3810 else if (!strcmp(arg
, "--no-rename-empty"))
3811 DIFF_OPT_CLR(options
, RENAME_EMPTY
);
3812 else if (!strcmp(arg
, "--relative"))
3813 DIFF_OPT_SET(options
, RELATIVE_NAME
);
3814 else if (skip_prefix(arg
, "--relative=", &arg
)) {
3815 DIFF_OPT_SET(options
, RELATIVE_NAME
);
3816 options
->prefix
= arg
;
3820 else if (!strcmp(arg
, "--minimal"))
3821 DIFF_XDL_SET(options
, NEED_MINIMAL
);
3822 else if (!strcmp(arg
, "--no-minimal"))
3823 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
3824 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
3825 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
3826 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
3827 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
3828 else if (!strcmp(arg
, "--ignore-space-at-eol"))
3829 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
3830 else if (!strcmp(arg
, "--ignore-blank-lines"))
3831 DIFF_XDL_SET(options
, IGNORE_BLANK_LINES
);
3832 else if (!strcmp(arg
, "--compaction-heuristic"))
3833 DIFF_XDL_SET(options
, COMPACTION_HEURISTIC
);
3834 else if (!strcmp(arg
, "--no-compaction-heuristic"))
3835 DIFF_XDL_CLR(options
, COMPACTION_HEURISTIC
);
3836 else if (!strcmp(arg
, "--patience"))
3837 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
3838 else if (!strcmp(arg
, "--histogram"))
3839 options
->xdl_opts
= DIFF_WITH_ALG(options
, HISTOGRAM_DIFF
);
3840 else if ((argcount
= parse_long_opt("diff-algorithm", av
, &optarg
))) {
3841 long value
= parse_algorithm_value(optarg
);
3843 return error("option diff-algorithm accepts \"myers\", "
3844 "\"minimal\", \"patience\" and \"histogram\"");
3845 /* clear out previous settings */
3846 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
3847 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3848 options
->xdl_opts
|= value
;
3853 else if (!strcmp(arg
, "--binary")) {
3854 enable_patch_output(&options
->output_format
);
3855 DIFF_OPT_SET(options
, BINARY
);
3857 else if (!strcmp(arg
, "--full-index"))
3858 DIFF_OPT_SET(options
, FULL_INDEX
);
3859 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
3860 DIFF_OPT_SET(options
, TEXT
);
3861 else if (!strcmp(arg
, "-R"))
3862 DIFF_OPT_SET(options
, REVERSE_DIFF
);
3863 else if (!strcmp(arg
, "--find-copies-harder"))
3864 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
3865 else if (!strcmp(arg
, "--follow"))
3866 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
3867 else if (!strcmp(arg
, "--no-follow")) {
3868 DIFF_OPT_CLR(options
, FOLLOW_RENAMES
);
3869 DIFF_OPT_CLR(options
, DEFAULT_FOLLOW_RENAMES
);
3870 } else if (!strcmp(arg
, "--color"))
3871 options
->use_color
= 1;
3872 else if (skip_prefix(arg
, "--color=", &arg
)) {
3873 int value
= git_config_colorbool(NULL
, arg
);
3875 return error("option `color' expects \"always\", \"auto\", or \"never\"");
3876 options
->use_color
= value
;
3878 else if (!strcmp(arg
, "--no-color"))
3879 options
->use_color
= 0;
3880 else if (!strcmp(arg
, "--color-words")) {
3881 options
->use_color
= 1;
3882 options
->word_diff
= DIFF_WORDS_COLOR
;
3884 else if (skip_prefix(arg
, "--color-words=", &arg
)) {
3885 options
->use_color
= 1;
3886 options
->word_diff
= DIFF_WORDS_COLOR
;
3887 options
->word_regex
= arg
;
3889 else if (!strcmp(arg
, "--word-diff")) {
3890 if (options
->word_diff
== DIFF_WORDS_NONE
)
3891 options
->word_diff
= DIFF_WORDS_PLAIN
;
3893 else if (skip_prefix(arg
, "--word-diff=", &arg
)) {
3894 if (!strcmp(arg
, "plain"))
3895 options
->word_diff
= DIFF_WORDS_PLAIN
;
3896 else if (!strcmp(arg
, "color")) {
3897 options
->use_color
= 1;
3898 options
->word_diff
= DIFF_WORDS_COLOR
;
3900 else if (!strcmp(arg
, "porcelain"))
3901 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
3902 else if (!strcmp(arg
, "none"))
3903 options
->word_diff
= DIFF_WORDS_NONE
;
3905 die("bad --word-diff argument: %s", arg
);
3907 else if ((argcount
= parse_long_opt("word-diff-regex", av
, &optarg
))) {
3908 if (options
->word_diff
== DIFF_WORDS_NONE
)
3909 options
->word_diff
= DIFF_WORDS_PLAIN
;
3910 options
->word_regex
= optarg
;
3913 else if (!strcmp(arg
, "--exit-code"))
3914 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
3915 else if (!strcmp(arg
, "--quiet"))
3916 DIFF_OPT_SET(options
, QUICK
);
3917 else if (!strcmp(arg
, "--ext-diff"))
3918 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
3919 else if (!strcmp(arg
, "--no-ext-diff"))
3920 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
3921 else if (!strcmp(arg
, "--textconv"))
3922 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
3923 else if (!strcmp(arg
, "--no-textconv"))
3924 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
3925 else if (!strcmp(arg
, "--ignore-submodules")) {
3926 DIFF_OPT_SET(options
, OVERRIDE_SUBMODULE_CONFIG
);
3927 handle_ignore_submodules_arg(options
, "all");
3928 } else if (skip_prefix(arg
, "--ignore-submodules=", &arg
)) {
3929 DIFF_OPT_SET(options
, OVERRIDE_SUBMODULE_CONFIG
);
3930 handle_ignore_submodules_arg(options
, arg
);
3931 } else if (!strcmp(arg
, "--submodule"))
3932 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
3933 else if (skip_prefix(arg
, "--submodule=", &arg
))
3934 return parse_submodule_opt(options
, arg
);
3935 else if (skip_prefix(arg
, "--ws-error-highlight=", &arg
))
3936 return parse_ws_error_highlight(options
, arg
);
3939 else if (!strcmp(arg
, "-z"))
3940 options
->line_termination
= 0;
3941 else if ((argcount
= short_opt('l', av
, &optarg
))) {
3942 options
->rename_limit
= strtoul(optarg
, NULL
, 10);
3945 else if ((argcount
= short_opt('S', av
, &optarg
))) {
3946 options
->pickaxe
= optarg
;
3947 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
3949 } else if ((argcount
= short_opt('G', av
, &optarg
))) {
3950 options
->pickaxe
= optarg
;
3951 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
3954 else if (!strcmp(arg
, "--pickaxe-all"))
3955 options
->pickaxe_opts
|= DIFF_PICKAXE_ALL
;
3956 else if (!strcmp(arg
, "--pickaxe-regex"))
3957 options
->pickaxe_opts
|= DIFF_PICKAXE_REGEX
;
3958 else if ((argcount
= short_opt('O', av
, &optarg
))) {
3959 const char *path
= prefix_filename(prefix
, strlen(prefix
), optarg
);
3960 options
->orderfile
= xstrdup(path
);
3963 else if ((argcount
= parse_long_opt("diff-filter", av
, &optarg
))) {
3964 int offending
= parse_diff_filter_opt(optarg
, options
);
3966 die("unknown change class '%c' in --diff-filter=%s",
3970 else if (!strcmp(arg
, "--abbrev"))
3971 options
->abbrev
= DEFAULT_ABBREV
;
3972 else if (skip_prefix(arg
, "--abbrev=", &arg
)) {
3973 options
->abbrev
= strtoul(arg
, NULL
, 10);
3974 if (options
->abbrev
< MINIMUM_ABBREV
)
3975 options
->abbrev
= MINIMUM_ABBREV
;
3976 else if (40 < options
->abbrev
)
3977 options
->abbrev
= 40;
3979 else if ((argcount
= parse_long_opt("src-prefix", av
, &optarg
))) {
3980 options
->a_prefix
= optarg
;
3983 else if ((argcount
= parse_long_opt("line-prefix", av
, &optarg
))) {
3984 options
->line_prefix
= optarg
;
3985 options
->line_prefix_length
= strlen(options
->line_prefix
);
3986 graph_setup_line_prefix(options
);
3989 else if ((argcount
= parse_long_opt("dst-prefix", av
, &optarg
))) {
3990 options
->b_prefix
= optarg
;
3993 else if (!strcmp(arg
, "--no-prefix"))
3994 options
->a_prefix
= options
->b_prefix
= "";
3995 else if (opt_arg(arg
, '\0', "inter-hunk-context",
3996 &options
->interhunkcontext
))
3998 else if (!strcmp(arg
, "-W"))
3999 DIFF_OPT_SET(options
, FUNCCONTEXT
);
4000 else if (!strcmp(arg
, "--function-context"))
4001 DIFF_OPT_SET(options
, FUNCCONTEXT
);
4002 else if (!strcmp(arg
, "--no-function-context"))
4003 DIFF_OPT_CLR(options
, FUNCCONTEXT
);
4004 else if ((argcount
= parse_long_opt("output", av
, &optarg
))) {
4005 const char *path
= prefix_filename(prefix
, strlen(prefix
), optarg
);
4006 options
->file
= fopen(path
, "w");
4008 die_errno("Could not open '%s'", path
);
4009 options
->close_file
= 1;
4010 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
4011 options
->use_color
= GIT_COLOR_NEVER
;
4018 int parse_rename_score(const char **cp_p
)
4020 unsigned long num
, scale
;
4022 const char *cp
= *cp_p
;
4029 if ( !dot
&& ch
== '.' ) {
4032 } else if ( ch
== '%' ) {
4033 scale
= dot
? scale
*100 : 100;
4034 cp
++; /* % is always at the end */
4036 } else if ( ch
>= '0' && ch
<= '9' ) {
4037 if ( scale
< 100000 ) {
4039 num
= (num
*10) + (ch
-'0');
4048 /* user says num divided by scale and we say internally that
4049 * is MAX_SCORE * num / scale.
4051 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
4054 static int diff_scoreopt_parse(const char *opt
)
4056 int opt1
, opt2
, cmd
;
4062 /* convert the long-form arguments into short-form versions */
4063 if (skip_prefix(opt
, "break-rewrites", &opt
)) {
4064 if (*opt
== 0 || *opt
++ == '=')
4066 } else if (skip_prefix(opt
, "find-copies", &opt
)) {
4067 if (*opt
== 0 || *opt
++ == '=')
4069 } else if (skip_prefix(opt
, "find-renames", &opt
)) {
4070 if (*opt
== 0 || *opt
++ == '=')
4074 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
4075 return -1; /* that is not a -M, -C, or -B option */
4077 opt1
= parse_rename_score(&opt
);
4083 else if (*opt
!= '/')
4084 return -1; /* we expect -B80/99 or -B80 */
4087 opt2
= parse_rename_score(&opt
);
4092 return opt1
| (opt2
<< 16);
4095 struct diff_queue_struct diff_queued_diff
;
4097 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
4099 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
4100 queue
->queue
[queue
->nr
++] = dp
;
4103 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
4104 struct diff_filespec
*one
,
4105 struct diff_filespec
*two
)
4107 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
4115 void diff_free_filepair(struct diff_filepair
*p
)
4117 free_filespec(p
->one
);
4118 free_filespec(p
->two
);
4122 /* This is different from find_unique_abbrev() in that
4123 * it stuffs the result with dots for alignment.
4125 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
4130 return sha1_to_hex(sha1
);
4132 abbrev
= find_unique_abbrev(sha1
, len
);
4133 abblen
= strlen(abbrev
);
4135 static char hex
[41];
4136 if (len
< abblen
&& abblen
<= len
+ 2)
4137 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
4139 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
4142 return sha1_to_hex(sha1
);
4145 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
4147 int line_termination
= opt
->line_termination
;
4148 int inter_name_termination
= line_termination
? '\t' : '\0';
4150 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
4151 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
4152 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
4153 diff_unique_abbrev(p
->one
->oid
.hash
, opt
->abbrev
));
4154 fprintf(opt
->file
, "%s ",
4155 diff_unique_abbrev(p
->two
->oid
.hash
, opt
->abbrev
));
4158 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
4159 inter_name_termination
);
4161 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
4164 if (p
->status
== DIFF_STATUS_COPIED
||
4165 p
->status
== DIFF_STATUS_RENAMED
) {
4166 const char *name_a
, *name_b
;
4167 name_a
= p
->one
->path
;
4168 name_b
= p
->two
->path
;
4169 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4170 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
4171 write_name_quoted(name_b
, opt
->file
, line_termination
);
4173 const char *name_a
, *name_b
;
4174 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
4176 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4177 write_name_quoted(name_a
, opt
->file
, line_termination
);
4181 int diff_unmodified_pair(struct diff_filepair
*p
)
4183 /* This function is written stricter than necessary to support
4184 * the currently implemented transformers, but the idea is to
4185 * let transformers to produce diff_filepairs any way they want,
4186 * and filter and clean them up here before producing the output.
4188 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
4190 if (DIFF_PAIR_UNMERGED(p
))
4191 return 0; /* unmerged is interesting */
4193 /* deletion, addition, mode or type change
4194 * and rename are all interesting.
4196 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
4197 DIFF_PAIR_MODE_CHANGED(p
) ||
4198 strcmp(one
->path
, two
->path
))
4201 /* both are valid and point at the same path. that is, we are
4202 * dealing with a change.
4204 if (one
->oid_valid
&& two
->oid_valid
&&
4205 !oidcmp(&one
->oid
, &two
->oid
) &&
4206 !one
->dirty_submodule
&& !two
->dirty_submodule
)
4207 return 1; /* no change */
4208 if (!one
->oid_valid
&& !two
->oid_valid
)
4209 return 1; /* both look at the same file on the filesystem. */
4213 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
4215 if (diff_unmodified_pair(p
))
4218 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4219 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4220 return; /* no tree diffs in patch format */
4225 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
4226 struct diffstat_t
*diffstat
)
4228 if (diff_unmodified_pair(p
))
4231 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4232 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4233 return; /* no useful stat for tree diffs */
4235 run_diffstat(p
, o
, diffstat
);
4238 static void diff_flush_checkdiff(struct diff_filepair
*p
,
4239 struct diff_options
*o
)
4241 if (diff_unmodified_pair(p
))
4244 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4245 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4246 return; /* nothing to check in tree diffs */
4248 run_checkdiff(p
, o
);
4251 int diff_queue_is_empty(void)
4253 struct diff_queue_struct
*q
= &diff_queued_diff
;
4255 for (i
= 0; i
< q
->nr
; i
++)
4256 if (!diff_unmodified_pair(q
->queue
[i
]))
4262 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
4264 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
4267 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
4269 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
4270 fprintf(stderr
, "queue[%d] %s size %lu\n",
4275 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
4277 diff_debug_filespec(p
->one
, i
, "one");
4278 diff_debug_filespec(p
->two
, i
, "two");
4279 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
4280 p
->score
, p
->status
? p
->status
: '?',
4281 p
->one
->rename_used
, p
->broken_pair
);
4284 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
4288 fprintf(stderr
, "%s\n", msg
);
4289 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
4290 for (i
= 0; i
< q
->nr
; i
++) {
4291 struct diff_filepair
*p
= q
->queue
[i
];
4292 diff_debug_filepair(p
, i
);
4297 static void diff_resolve_rename_copy(void)
4300 struct diff_filepair
*p
;
4301 struct diff_queue_struct
*q
= &diff_queued_diff
;
4303 diff_debug_queue("resolve-rename-copy", q
);
4305 for (i
= 0; i
< q
->nr
; i
++) {
4307 p
->status
= 0; /* undecided */
4308 if (DIFF_PAIR_UNMERGED(p
))
4309 p
->status
= DIFF_STATUS_UNMERGED
;
4310 else if (!DIFF_FILE_VALID(p
->one
))
4311 p
->status
= DIFF_STATUS_ADDED
;
4312 else if (!DIFF_FILE_VALID(p
->two
))
4313 p
->status
= DIFF_STATUS_DELETED
;
4314 else if (DIFF_PAIR_TYPE_CHANGED(p
))
4315 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
4317 /* from this point on, we are dealing with a pair
4318 * whose both sides are valid and of the same type, i.e.
4319 * either in-place edit or rename/copy edit.
4321 else if (DIFF_PAIR_RENAME(p
)) {
4323 * A rename might have re-connected a broken
4324 * pair up, causing the pathnames to be the
4325 * same again. If so, that's not a rename at
4326 * all, just a modification..
4328 * Otherwise, see if this source was used for
4329 * multiple renames, in which case we decrement
4330 * the count, and call it a copy.
4332 if (!strcmp(p
->one
->path
, p
->two
->path
))
4333 p
->status
= DIFF_STATUS_MODIFIED
;
4334 else if (--p
->one
->rename_used
> 0)
4335 p
->status
= DIFF_STATUS_COPIED
;
4337 p
->status
= DIFF_STATUS_RENAMED
;
4339 else if (oidcmp(&p
->one
->oid
, &p
->two
->oid
) ||
4340 p
->one
->mode
!= p
->two
->mode
||
4341 p
->one
->dirty_submodule
||
4342 p
->two
->dirty_submodule
||
4343 is_null_oid(&p
->one
->oid
))
4344 p
->status
= DIFF_STATUS_MODIFIED
;
4346 /* This is a "no-change" entry and should not
4347 * happen anymore, but prepare for broken callers.
4349 error("feeding unmodified %s to diffcore",
4351 p
->status
= DIFF_STATUS_UNKNOWN
;
4354 diff_debug_queue("resolve-rename-copy done", q
);
4357 static int check_pair_status(struct diff_filepair
*p
)
4359 switch (p
->status
) {
4360 case DIFF_STATUS_UNKNOWN
:
4363 die("internal error in diff-resolve-rename-copy");
4369 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
4371 int fmt
= opt
->output_format
;
4373 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
4374 diff_flush_checkdiff(p
, opt
);
4375 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
4376 diff_flush_raw(p
, opt
);
4377 else if (fmt
& DIFF_FORMAT_NAME
) {
4378 const char *name_a
, *name_b
;
4379 name_a
= p
->two
->path
;
4381 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4382 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
4386 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
4389 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
4391 fprintf(file
, " %s ", newdelete
);
4392 write_name_quoted(fs
->path
, file
, '\n');
4396 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
,
4397 const char *line_prefix
)
4399 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
4400 fprintf(file
, "%s mode change %06o => %06o%c", line_prefix
, p
->one
->mode
,
4401 p
->two
->mode
, show_name
? ' ' : '\n');
4403 write_name_quoted(p
->two
->path
, file
, '\n');
4408 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
,
4409 const char *line_prefix
)
4411 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
4413 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
4415 show_mode_change(file
, p
, 0, line_prefix
);
4418 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
4420 FILE *file
= opt
->file
;
4421 const char *line_prefix
= diff_line_prefix(opt
);
4424 case DIFF_STATUS_DELETED
:
4425 fputs(line_prefix
, file
);
4426 show_file_mode_name(file
, "delete", p
->one
);
4428 case DIFF_STATUS_ADDED
:
4429 fputs(line_prefix
, file
);
4430 show_file_mode_name(file
, "create", p
->two
);
4432 case DIFF_STATUS_COPIED
:
4433 fputs(line_prefix
, file
);
4434 show_rename_copy(file
, "copy", p
, line_prefix
);
4436 case DIFF_STATUS_RENAMED
:
4437 fputs(line_prefix
, file
);
4438 show_rename_copy(file
, "rename", p
, line_prefix
);
4442 fprintf(file
, "%s rewrite ", line_prefix
);
4443 write_name_quoted(p
->two
->path
, file
, ' ');
4444 fprintf(file
, "(%d%%)\n", similarity_index(p
));
4446 show_mode_change(file
, p
, !p
->score
, line_prefix
);
4456 static int remove_space(char *line
, int len
)
4462 for (i
= 0; i
< len
; i
++)
4463 if (!isspace((c
= line
[i
])))
4469 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
4471 struct patch_id_t
*data
= priv
;
4474 /* Ignore line numbers when computing the SHA1 of the patch */
4475 if (starts_with(line
, "@@ -"))
4478 new_len
= remove_space(line
, len
);
4480 git_SHA1_Update(data
->ctx
, line
, new_len
);
4481 data
->patchlen
+= new_len
;
4484 /* returns 0 upon success, and writes result into sha1 */
4485 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
4487 struct diff_queue_struct
*q
= &diff_queued_diff
;
4490 struct patch_id_t data
;
4491 char buffer
[PATH_MAX
* 4 + 20];
4493 git_SHA1_Init(&ctx
);
4494 memset(&data
, 0, sizeof(struct patch_id_t
));
4497 for (i
= 0; i
< q
->nr
; i
++) {
4501 struct diff_filepair
*p
= q
->queue
[i
];
4504 memset(&xpp
, 0, sizeof(xpp
));
4505 memset(&xecfg
, 0, sizeof(xecfg
));
4507 return error("internal diff status error");
4508 if (p
->status
== DIFF_STATUS_UNKNOWN
)
4510 if (diff_unmodified_pair(p
))
4512 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4513 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4515 if (DIFF_PAIR_UNMERGED(p
))
4518 diff_fill_sha1_info(p
->one
);
4519 diff_fill_sha1_info(p
->two
);
4520 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
4521 fill_mmfile(&mf2
, p
->two
) < 0)
4522 return error("unable to read files to diff");
4524 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
4525 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
4526 if (p
->one
->mode
== 0)
4527 len1
= snprintf(buffer
, sizeof(buffer
),
4528 "diff--gita/%.*sb/%.*s"
4535 len2
, p
->two
->path
);
4536 else if (p
->two
->mode
== 0)
4537 len1
= snprintf(buffer
, sizeof(buffer
),
4538 "diff--gita/%.*sb/%.*s"
4539 "deletedfilemode%06o"
4545 len1
, p
->one
->path
);
4547 len1
= snprintf(buffer
, sizeof(buffer
),
4548 "diff--gita/%.*sb/%.*s"
4554 len2
, p
->two
->path
);
4555 git_SHA1_Update(&ctx
, buffer
, len1
);
4557 if (diff_filespec_is_binary(p
->one
) ||
4558 diff_filespec_is_binary(p
->two
)) {
4559 git_SHA1_Update(&ctx
, oid_to_hex(&p
->one
->oid
),
4561 git_SHA1_Update(&ctx
, oid_to_hex(&p
->two
->oid
),
4569 if (xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
4571 return error("unable to generate patch-id diff for %s",
4575 git_SHA1_Final(sha1
, &ctx
);
4579 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
4581 struct diff_queue_struct
*q
= &diff_queued_diff
;
4583 int result
= diff_get_patch_id(options
, sha1
);
4585 for (i
= 0; i
< q
->nr
; i
++)
4586 diff_free_filepair(q
->queue
[i
]);
4589 DIFF_QUEUE_CLEAR(q
);
4594 static int is_summary_empty(const struct diff_queue_struct
*q
)
4598 for (i
= 0; i
< q
->nr
; i
++) {
4599 const struct diff_filepair
*p
= q
->queue
[i
];
4601 switch (p
->status
) {
4602 case DIFF_STATUS_DELETED
:
4603 case DIFF_STATUS_ADDED
:
4604 case DIFF_STATUS_COPIED
:
4605 case DIFF_STATUS_RENAMED
:
4610 if (p
->one
->mode
&& p
->two
->mode
&&
4611 p
->one
->mode
!= p
->two
->mode
)
4619 static const char rename_limit_warning
[] =
4620 "inexact rename detection was skipped due to too many files.";
4622 static const char degrade_cc_to_c_warning
[] =
4623 "only found copies from modified paths due to too many files.";
4625 static const char rename_limit_advice
[] =
4626 "you may want to set your %s variable to at least "
4627 "%d and retry the command.";
4629 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
4632 warning(degrade_cc_to_c_warning
);
4634 warning(rename_limit_warning
);
4637 if (0 < needed
&& needed
< 32767)
4638 warning(rename_limit_advice
, varname
, needed
);
4641 void diff_flush(struct diff_options
*options
)
4643 struct diff_queue_struct
*q
= &diff_queued_diff
;
4644 int i
, output_format
= options
->output_format
;
4646 int dirstat_by_line
= 0;
4649 * Order: raw, stat, summary, patch
4650 * or: name/name-status/checkdiff (other bits clear)
4655 if (output_format
& (DIFF_FORMAT_RAW
|
4657 DIFF_FORMAT_NAME_STATUS
|
4658 DIFF_FORMAT_CHECKDIFF
)) {
4659 for (i
= 0; i
< q
->nr
; i
++) {
4660 struct diff_filepair
*p
= q
->queue
[i
];
4661 if (check_pair_status(p
))
4662 flush_one_pair(p
, options
);
4667 if (output_format
& DIFF_FORMAT_DIRSTAT
&& DIFF_OPT_TST(options
, DIRSTAT_BY_LINE
))
4668 dirstat_by_line
= 1;
4670 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
4672 struct diffstat_t diffstat
;
4674 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
4675 for (i
= 0; i
< q
->nr
; i
++) {
4676 struct diff_filepair
*p
= q
->queue
[i
];
4677 if (check_pair_status(p
))
4678 diff_flush_stat(p
, options
, &diffstat
);
4680 if (output_format
& DIFF_FORMAT_NUMSTAT
)
4681 show_numstat(&diffstat
, options
);
4682 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
4683 show_stats(&diffstat
, options
);
4684 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
4685 show_shortstats(&diffstat
, options
);
4686 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
4687 show_dirstat_by_line(&diffstat
, options
);
4688 free_diffstat_info(&diffstat
);
4691 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
4692 show_dirstat(options
);
4694 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
4695 for (i
= 0; i
< q
->nr
; i
++) {
4696 diff_summary(options
, q
->queue
[i
]);
4701 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
4702 DIFF_OPT_TST(options
, EXIT_WITH_STATUS
) &&
4703 DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
4705 * run diff_flush_patch for the exit status. setting
4706 * options->file to /dev/null should be safe, because we
4707 * aren't supposed to produce any output anyway.
4709 if (options
->close_file
)
4710 fclose(options
->file
);
4711 options
->file
= fopen("/dev/null", "w");
4713 die_errno("Could not open /dev/null");
4714 options
->close_file
= 1;
4715 for (i
= 0; i
< q
->nr
; i
++) {
4716 struct diff_filepair
*p
= q
->queue
[i
];
4717 if (check_pair_status(p
))
4718 diff_flush_patch(p
, options
);
4719 if (options
->found_changes
)
4724 if (output_format
& DIFF_FORMAT_PATCH
) {
4726 fprintf(options
->file
, "%s%c",
4727 diff_line_prefix(options
),
4728 options
->line_termination
);
4729 if (options
->stat_sep
) {
4730 /* attach patch instead of inline */
4731 fputs(options
->stat_sep
, options
->file
);
4735 for (i
= 0; i
< q
->nr
; i
++) {
4736 struct diff_filepair
*p
= q
->queue
[i
];
4737 if (check_pair_status(p
))
4738 diff_flush_patch(p
, options
);
4742 if (output_format
& DIFF_FORMAT_CALLBACK
)
4743 options
->format_callback(q
, options
, options
->format_callback_data
);
4745 for (i
= 0; i
< q
->nr
; i
++)
4746 diff_free_filepair(q
->queue
[i
]);
4749 DIFF_QUEUE_CLEAR(q
);
4750 if (options
->close_file
)
4751 fclose(options
->file
);
4754 * Report the content-level differences with HAS_CHANGES;
4755 * diff_addremove/diff_change does not set the bit when
4756 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
4758 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
4759 if (options
->found_changes
)
4760 DIFF_OPT_SET(options
, HAS_CHANGES
);
4762 DIFF_OPT_CLR(options
, HAS_CHANGES
);
4766 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
4768 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
4770 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
4772 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
4773 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
4774 filter_bit_tst(p
->status
, options
)));
4777 static void diffcore_apply_filter(struct diff_options
*options
)
4780 struct diff_queue_struct
*q
= &diff_queued_diff
;
4781 struct diff_queue_struct outq
;
4783 DIFF_QUEUE_CLEAR(&outq
);
4785 if (!options
->filter
)
4788 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
4790 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
4791 if (match_filter(options
, q
->queue
[i
]))
4797 /* otherwise we will clear the whole queue
4798 * by copying the empty outq at the end of this
4799 * function, but first clear the current entries
4802 for (i
= 0; i
< q
->nr
; i
++)
4803 diff_free_filepair(q
->queue
[i
]);
4806 /* Only the matching ones */
4807 for (i
= 0; i
< q
->nr
; i
++) {
4808 struct diff_filepair
*p
= q
->queue
[i
];
4809 if (match_filter(options
, p
))
4812 diff_free_filepair(p
);
4819 /* Check whether two filespecs with the same mode and size are identical */
4820 static int diff_filespec_is_identical(struct diff_filespec
*one
,
4821 struct diff_filespec
*two
)
4823 if (S_ISGITLINK(one
->mode
))
4825 if (diff_populate_filespec(one
, 0))
4827 if (diff_populate_filespec(two
, 0))
4829 return !memcmp(one
->data
, two
->data
, one
->size
);
4832 static int diff_filespec_check_stat_unmatch(struct diff_filepair
*p
)
4834 if (p
->done_skip_stat_unmatch
)
4835 return p
->skip_stat_unmatch_result
;
4837 p
->done_skip_stat_unmatch
= 1;
4838 p
->skip_stat_unmatch_result
= 0;
4840 * 1. Entries that come from stat info dirtiness
4841 * always have both sides (iow, not create/delete),
4842 * one side of the object name is unknown, with
4843 * the same mode and size. Keep the ones that
4844 * do not match these criteria. They have real
4847 * 2. At this point, the file is known to be modified,
4848 * with the same mode and size, and the object
4849 * name of one side is unknown. Need to inspect
4850 * the identical contents.
4852 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
4853 !DIFF_FILE_VALID(p
->two
) ||
4854 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
4855 (p
->one
->mode
!= p
->two
->mode
) ||
4856 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
) ||
4857 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
) ||
4858 (p
->one
->size
!= p
->two
->size
) ||
4859 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
4860 p
->skip_stat_unmatch_result
= 1;
4861 return p
->skip_stat_unmatch_result
;
4864 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
4867 struct diff_queue_struct
*q
= &diff_queued_diff
;
4868 struct diff_queue_struct outq
;
4869 DIFF_QUEUE_CLEAR(&outq
);
4871 for (i
= 0; i
< q
->nr
; i
++) {
4872 struct diff_filepair
*p
= q
->queue
[i
];
4874 if (diff_filespec_check_stat_unmatch(p
))
4878 * The caller can subtract 1 from skip_stat_unmatch
4879 * to determine how many paths were dirty only
4880 * due to stat info mismatch.
4882 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
4883 diffopt
->skip_stat_unmatch
++;
4884 diff_free_filepair(p
);
4891 static int diffnamecmp(const void *a_
, const void *b_
)
4893 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
4894 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
4895 const char *name_a
, *name_b
;
4897 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
4898 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
4899 return strcmp(name_a
, name_b
);
4902 void diffcore_fix_diff_index(struct diff_options
*options
)
4904 struct diff_queue_struct
*q
= &diff_queued_diff
;
4905 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), diffnamecmp
);
4908 void diffcore_std(struct diff_options
*options
)
4910 /* NOTE please keep the following in sync with diff_tree_combined() */
4911 if (options
->skip_stat_unmatch
)
4912 diffcore_skip_stat_unmatch(options
);
4913 if (!options
->found_follow
) {
4914 /* See try_to_follow_renames() in tree-diff.c */
4915 if (options
->break_opt
!= -1)
4916 diffcore_break(options
->break_opt
);
4917 if (options
->detect_rename
)
4918 diffcore_rename(options
);
4919 if (options
->break_opt
!= -1)
4920 diffcore_merge_broken();
4922 if (options
->pickaxe
)
4923 diffcore_pickaxe(options
);
4924 if (options
->orderfile
)
4925 diffcore_order(options
->orderfile
);
4926 if (!options
->found_follow
)
4927 /* See try_to_follow_renames() in tree-diff.c */
4928 diff_resolve_rename_copy();
4929 diffcore_apply_filter(options
);
4931 if (diff_queued_diff
.nr
&& !DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
4932 DIFF_OPT_SET(options
, HAS_CHANGES
);
4934 DIFF_OPT_CLR(options
, HAS_CHANGES
);
4936 options
->found_follow
= 0;
4939 int diff_result_code(struct diff_options
*opt
, int status
)
4943 diff_warn_rename_limit("diff.renameLimit",
4944 opt
->needed_rename_limit
,
4945 opt
->degraded_cc_to_c
);
4946 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
4947 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
4949 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
4950 DIFF_OPT_TST(opt
, HAS_CHANGES
))
4952 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
4953 DIFF_OPT_TST(opt
, CHECK_FAILED
))
4958 int diff_can_quit_early(struct diff_options
*opt
)
4960 return (DIFF_OPT_TST(opt
, QUICK
) &&
4962 DIFF_OPT_TST(opt
, HAS_CHANGES
));
4966 * Shall changes to this submodule be ignored?
4968 * Submodule changes can be configured to be ignored separately for each path,
4969 * but that configuration can be overridden from the command line.
4971 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
4974 unsigned orig_flags
= options
->flags
;
4975 if (!DIFF_OPT_TST(options
, OVERRIDE_SUBMODULE_CONFIG
))
4976 set_diffopt_flags_from_submodule_config(options
, path
);
4977 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
))
4979 options
->flags
= orig_flags
;
4983 void diff_addremove(struct diff_options
*options
,
4984 int addremove
, unsigned mode
,
4985 const unsigned char *sha1
,
4987 const char *concatpath
, unsigned dirty_submodule
)
4989 struct diff_filespec
*one
, *two
;
4991 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
4994 /* This may look odd, but it is a preparation for
4995 * feeding "there are unchanged files which should
4996 * not produce diffs, but when you are doing copy
4997 * detection you would need them, so here they are"
4998 * entries to the diff-core. They will be prefixed
4999 * with something like '=' or '*' (I haven't decided
5000 * which but should not make any difference).
5001 * Feeding the same new and old to diff_change()
5002 * also has the same effect.
5003 * Before the final output happens, they are pruned after
5004 * merged into rename/copy pairs as appropriate.
5006 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
5007 addremove
= (addremove
== '+' ? '-' :
5008 addremove
== '-' ? '+' : addremove
);
5010 if (options
->prefix
&&
5011 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5014 one
= alloc_filespec(concatpath
);
5015 two
= alloc_filespec(concatpath
);
5017 if (addremove
!= '+')
5018 fill_filespec(one
, sha1
, sha1_valid
, mode
);
5019 if (addremove
!= '-') {
5020 fill_filespec(two
, sha1
, sha1_valid
, mode
);
5021 two
->dirty_submodule
= dirty_submodule
;
5024 diff_queue(&diff_queued_diff
, one
, two
);
5025 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5026 DIFF_OPT_SET(options
, HAS_CHANGES
);
5029 void diff_change(struct diff_options
*options
,
5030 unsigned old_mode
, unsigned new_mode
,
5031 const unsigned char *old_sha1
,
5032 const unsigned char *new_sha1
,
5033 int old_sha1_valid
, int new_sha1_valid
,
5034 const char *concatpath
,
5035 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
5037 struct diff_filespec
*one
, *two
;
5038 struct diff_filepair
*p
;
5040 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
5041 is_submodule_ignored(concatpath
, options
))
5044 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
5046 const unsigned char *tmp_c
;
5047 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
5048 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
5049 tmp
= old_sha1_valid
; old_sha1_valid
= new_sha1_valid
;
5050 new_sha1_valid
= tmp
;
5051 tmp
= old_dirty_submodule
; old_dirty_submodule
= new_dirty_submodule
;
5052 new_dirty_submodule
= tmp
;
5055 if (options
->prefix
&&
5056 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5059 one
= alloc_filespec(concatpath
);
5060 two
= alloc_filespec(concatpath
);
5061 fill_filespec(one
, old_sha1
, old_sha1_valid
, old_mode
);
5062 fill_filespec(two
, new_sha1
, new_sha1_valid
, new_mode
);
5063 one
->dirty_submodule
= old_dirty_submodule
;
5064 two
->dirty_submodule
= new_dirty_submodule
;
5065 p
= diff_queue(&diff_queued_diff
, one
, two
);
5067 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5070 if (DIFF_OPT_TST(options
, QUICK
) && options
->skip_stat_unmatch
&&
5071 !diff_filespec_check_stat_unmatch(p
))
5074 DIFF_OPT_SET(options
, HAS_CHANGES
);
5077 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
5079 struct diff_filepair
*pair
;
5080 struct diff_filespec
*one
, *two
;
5082 if (options
->prefix
&&
5083 strncmp(path
, options
->prefix
, options
->prefix_length
))
5086 one
= alloc_filespec(path
);
5087 two
= alloc_filespec(path
);
5088 pair
= diff_queue(&diff_queued_diff
, one
, two
);
5089 pair
->is_unmerged
= 1;
5093 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
5096 struct diff_tempfile
*temp
;
5097 const char *argv
[3];
5098 const char **arg
= argv
;
5099 struct child_process child
= CHILD_PROCESS_INIT
;
5100 struct strbuf buf
= STRBUF_INIT
;
5103 temp
= prepare_temp_file(spec
->path
, spec
);
5105 *arg
++ = temp
->name
;
5108 child
.use_shell
= 1;
5111 if (start_command(&child
)) {
5116 if (strbuf_read(&buf
, child
.out
, 0) < 0)
5117 err
= error("error reading from textconv command '%s'", pgm
);
5120 if (finish_command(&child
) || err
) {
5121 strbuf_release(&buf
);
5127 return strbuf_detach(&buf
, outsize
);
5130 size_t fill_textconv(struct userdiff_driver
*driver
,
5131 struct diff_filespec
*df
,
5137 if (!DIFF_FILE_VALID(df
)) {
5141 if (diff_populate_filespec(df
, 0))
5142 die("unable to read files to diff");
5147 if (!driver
->textconv
)
5148 die("BUG: fill_textconv called with non-textconv driver");
5150 if (driver
->textconv_cache
&& df
->oid_valid
) {
5151 *outbuf
= notes_cache_get(driver
->textconv_cache
,
5158 *outbuf
= run_textconv(driver
->textconv
, df
, &size
);
5160 die("unable to read files to diff");
5162 if (driver
->textconv_cache
&& df
->oid_valid
) {
5163 /* ignore errors, as we might be in a readonly repository */
5164 notes_cache_put(driver
->textconv_cache
, df
->oid
.hash
, *outbuf
,
5167 * we could save up changes and flush them all at the end,
5168 * but we would need an extra call after all diffing is done.
5169 * Since generating a cache entry is the slow path anyway,
5170 * this extra overhead probably isn't a big deal.
5172 notes_cache_write(driver
->textconv_cache
);
5178 void setup_diff_pager(struct diff_options
*opt
)
5181 * If the user asked for our exit code, then either they want --quiet
5182 * or --exit-code. We should definitely not bother with a pager in the
5183 * former case, as we will generate no output. Since we still properly
5184 * report our exit code even when a pager is run, we _could_ run a
5185 * pager with --exit-code. But since we have not done so historically,
5186 * and because it is easy to find people oneline advising "git diff
5187 * --exit-code" in hooks and other scripts, we do not do so.
5189 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
5190 check_pager_config("diff") != 0)