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_indent_heuristic
; /* experimental */
31 static int diff_compaction_heuristic
; /* experimental */
32 static int diff_rename_limit_default
= 400;
33 static int diff_suppress_blank_empty
;
34 static int diff_use_color_default
= -1;
35 static int diff_context_default
= 3;
36 static const char *diff_word_regex_cfg
;
37 static const char *external_diff_cmd_cfg
;
38 static const char *diff_order_file_cfg
;
39 int diff_auto_refresh_index
= 1;
40 static int diff_mnemonic_prefix
;
41 static int diff_no_prefix
;
42 static int diff_stat_graph_width
;
43 static int diff_dirstat_permille_default
= 30;
44 static struct diff_options default_diff_options
;
45 static long diff_algorithm
;
47 static char diff_colors
[][COLOR_MAXLEN
] = {
49 GIT_COLOR_NORMAL
, /* CONTEXT */
50 GIT_COLOR_BOLD
, /* METAINFO */
51 GIT_COLOR_CYAN
, /* FRAGINFO */
52 GIT_COLOR_RED
, /* OLD */
53 GIT_COLOR_GREEN
, /* NEW */
54 GIT_COLOR_YELLOW
, /* COMMIT */
55 GIT_COLOR_BG_RED
, /* WHITESPACE */
56 GIT_COLOR_NORMAL
, /* FUNCINFO */
59 static int parse_diff_color_slot(const char *var
)
61 if (!strcasecmp(var
, "context") || !strcasecmp(var
, "plain"))
63 if (!strcasecmp(var
, "meta"))
65 if (!strcasecmp(var
, "frag"))
67 if (!strcasecmp(var
, "old"))
69 if (!strcasecmp(var
, "new"))
71 if (!strcasecmp(var
, "commit"))
73 if (!strcasecmp(var
, "whitespace"))
74 return DIFF_WHITESPACE
;
75 if (!strcasecmp(var
, "func"))
80 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
81 struct strbuf
*errmsg
)
83 char *params_copy
= xstrdup(params_string
);
84 struct string_list params
= STRING_LIST_INIT_NODUP
;
89 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
90 for (i
= 0; i
< params
.nr
; i
++) {
91 const char *p
= params
.items
[i
].string
;
92 if (!strcmp(p
, "changes")) {
93 DIFF_OPT_CLR(options
, DIRSTAT_BY_LINE
);
94 DIFF_OPT_CLR(options
, DIRSTAT_BY_FILE
);
95 } else if (!strcmp(p
, "lines")) {
96 DIFF_OPT_SET(options
, DIRSTAT_BY_LINE
);
97 DIFF_OPT_CLR(options
, DIRSTAT_BY_FILE
);
98 } else if (!strcmp(p
, "files")) {
99 DIFF_OPT_CLR(options
, DIRSTAT_BY_LINE
);
100 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
101 } else if (!strcmp(p
, "noncumulative")) {
102 DIFF_OPT_CLR(options
, DIRSTAT_CUMULATIVE
);
103 } else if (!strcmp(p
, "cumulative")) {
104 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
105 } else if (isdigit(*p
)) {
107 int permille
= strtoul(p
, &end
, 10) * 10;
108 if (*end
== '.' && isdigit(*++end
)) {
109 /* only use first digit */
110 permille
+= *end
- '0';
111 /* .. and ignore any further digits */
112 while (isdigit(*++end
))
116 options
->dirstat_permille
= permille
;
118 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
123 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
128 string_list_clear(¶ms
, 0);
133 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
135 if (!strcmp(value
, "log"))
136 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
137 else if (!strcmp(value
, "short"))
138 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
139 else if (!strcmp(value
, "diff"))
140 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
146 static int git_config_rename(const char *var
, const char *value
)
149 return DIFF_DETECT_RENAME
;
150 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
151 return DIFF_DETECT_COPY
;
152 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
155 long parse_algorithm_value(const char *value
)
159 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
161 else if (!strcasecmp(value
, "minimal"))
162 return XDF_NEED_MINIMAL
;
163 else if (!strcasecmp(value
, "patience"))
164 return XDF_PATIENCE_DIFF
;
165 else if (!strcasecmp(value
, "histogram"))
166 return XDF_HISTOGRAM_DIFF
;
171 * These are to give UI layer defaults.
172 * The core-level commands such as git-diff-files should
173 * never be affected by the setting of diff.renames
174 * the user happens to have in the configuration file.
176 void init_diff_ui_defaults(void)
178 diff_detect_rename_default
= 1;
181 int git_diff_heuristic_config(const char *var
, const char *value
, void *cb
)
183 if (!strcmp(var
, "diff.indentheuristic")) {
184 diff_indent_heuristic
= git_config_bool(var
, value
);
185 if (diff_indent_heuristic
)
186 diff_compaction_heuristic
= 0;
188 if (!strcmp(var
, "diff.compactionheuristic")) {
189 diff_compaction_heuristic
= git_config_bool(var
, value
);
190 if (diff_compaction_heuristic
)
191 diff_indent_heuristic
= 0;
196 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
198 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
199 diff_use_color_default
= git_config_colorbool(var
, value
);
202 if (!strcmp(var
, "diff.context")) {
203 diff_context_default
= git_config_int(var
, value
);
204 if (diff_context_default
< 0)
208 if (!strcmp(var
, "diff.renames")) {
209 diff_detect_rename_default
= git_config_rename(var
, value
);
212 if (!strcmp(var
, "diff.autorefreshindex")) {
213 diff_auto_refresh_index
= git_config_bool(var
, value
);
216 if (!strcmp(var
, "diff.mnemonicprefix")) {
217 diff_mnemonic_prefix
= git_config_bool(var
, value
);
220 if (!strcmp(var
, "diff.noprefix")) {
221 diff_no_prefix
= git_config_bool(var
, value
);
224 if (!strcmp(var
, "diff.statgraphwidth")) {
225 diff_stat_graph_width
= git_config_int(var
, value
);
228 if (!strcmp(var
, "diff.external"))
229 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
230 if (!strcmp(var
, "diff.wordregex"))
231 return git_config_string(&diff_word_regex_cfg
, var
, value
);
232 if (!strcmp(var
, "diff.orderfile"))
233 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
235 if (!strcmp(var
, "diff.ignoresubmodules"))
236 handle_ignore_submodules_arg(&default_diff_options
, value
);
238 if (!strcmp(var
, "diff.submodule")) {
239 if (parse_submodule_params(&default_diff_options
, value
))
240 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
245 if (!strcmp(var
, "diff.algorithm")) {
246 diff_algorithm
= parse_algorithm_value(value
);
247 if (diff_algorithm
< 0)
252 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
254 if (git_color_config(var
, value
, cb
) < 0)
257 return git_diff_basic_config(var
, value
, cb
);
260 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
264 if (!strcmp(var
, "diff.renamelimit")) {
265 diff_rename_limit_default
= git_config_int(var
, value
);
269 if (userdiff_config(var
, value
) < 0)
272 if (skip_prefix(var
, "diff.color.", &name
) ||
273 skip_prefix(var
, "color.diff.", &name
)) {
274 int slot
= parse_diff_color_slot(name
);
278 return config_error_nonbool(var
);
279 return color_parse(value
, diff_colors
[slot
]);
282 /* like GNU diff's --suppress-blank-empty option */
283 if (!strcmp(var
, "diff.suppressblankempty") ||
284 /* for backwards compatibility */
285 !strcmp(var
, "diff.suppress-blank-empty")) {
286 diff_suppress_blank_empty
= git_config_bool(var
, value
);
290 if (!strcmp(var
, "diff.dirstat")) {
291 struct strbuf errmsg
= STRBUF_INIT
;
292 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
293 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
294 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
296 strbuf_release(&errmsg
);
297 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
301 if (starts_with(var
, "submodule."))
302 return parse_submodule_config_option(var
, value
);
304 return git_default_config(var
, value
, cb
);
307 static char *quote_two(const char *one
, const char *two
)
309 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
310 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
311 struct strbuf res
= STRBUF_INIT
;
313 if (need_one
+ need_two
) {
314 strbuf_addch(&res
, '"');
315 quote_c_style(one
, &res
, NULL
, 1);
316 quote_c_style(two
, &res
, NULL
, 1);
317 strbuf_addch(&res
, '"');
319 strbuf_addstr(&res
, one
);
320 strbuf_addstr(&res
, two
);
322 return strbuf_detach(&res
, NULL
);
325 static const char *external_diff(void)
327 static const char *external_diff_cmd
= NULL
;
328 static int done_preparing
= 0;
331 return external_diff_cmd
;
332 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
333 if (!external_diff_cmd
)
334 external_diff_cmd
= external_diff_cmd_cfg
;
336 return external_diff_cmd
;
340 * Keep track of files used for diffing. Sometimes such an entry
341 * refers to a temporary file, sometimes to an existing file, and
342 * sometimes to "/dev/null".
344 static struct diff_tempfile
{
346 * filename external diff should read from, or NULL if this
347 * entry is currently not in use:
351 char hex
[GIT_SHA1_HEXSZ
+ 1];
355 * If this diff_tempfile instance refers to a temporary file,
356 * this tempfile object is used to manage its lifetime.
358 struct tempfile tempfile
;
361 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
363 struct emit_callback
{
366 int blank_at_eof_in_preimage
;
367 int blank_at_eof_in_postimage
;
369 int lno_in_postimage
;
370 sane_truncate_fn truncate
;
371 const char **label_path
;
372 struct diff_words_data
*diff_words
;
373 struct diff_options
*opt
;
374 struct strbuf
*header
;
377 static int count_lines(const char *data
, int size
)
379 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
386 completely_empty
= 0;
390 completely_empty
= 0;
393 if (completely_empty
)
396 count
++; /* no trailing newline */
400 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
402 if (!DIFF_FILE_VALID(one
)) {
403 mf
->ptr
= (char *)""; /* does not matter */
407 else if (diff_populate_filespec(one
, 0))
411 mf
->size
= one
->size
;
415 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
416 static unsigned long diff_filespec_size(struct diff_filespec
*one
)
418 if (!DIFF_FILE_VALID(one
))
420 diff_populate_filespec(one
, CHECK_SIZE_ONLY
);
424 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
427 long size
= mf
->size
;
432 ptr
+= size
- 1; /* pointing at the very end */
434 ; /* incomplete line */
436 ptr
--; /* skip the last LF */
437 while (mf
->ptr
< ptr
) {
439 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
440 if (*prev_eol
== '\n')
442 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
450 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
451 struct emit_callback
*ecbdata
)
454 unsigned ws_rule
= ecbdata
->ws_rule
;
455 l1
= count_trailing_blank(mf1
, ws_rule
);
456 l2
= count_trailing_blank(mf2
, ws_rule
);
458 ecbdata
->blank_at_eof_in_preimage
= 0;
459 ecbdata
->blank_at_eof_in_postimage
= 0;
462 at
= count_lines(mf1
->ptr
, mf1
->size
);
463 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
465 at
= count_lines(mf2
->ptr
, mf2
->size
);
466 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
469 static void emit_line_0(struct diff_options
*o
, const char *set
, const char *reset
,
470 int first
, const char *line
, int len
)
472 int has_trailing_newline
, has_trailing_carriage_return
;
474 FILE *file
= o
->file
;
476 fputs(diff_line_prefix(o
), file
);
479 has_trailing_newline
= (first
== '\n');
480 has_trailing_carriage_return
= (!has_trailing_newline
&&
482 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
484 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
485 if (has_trailing_newline
)
487 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
488 if (has_trailing_carriage_return
)
493 if (len
|| !nofirst
) {
497 fwrite(line
, len
, 1, file
);
500 if (has_trailing_carriage_return
)
502 if (has_trailing_newline
)
506 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
507 const char *line
, int len
)
509 emit_line_0(o
, set
, reset
, line
[0], line
+1, len
-1);
512 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
514 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
515 ecbdata
->blank_at_eof_in_preimage
&&
516 ecbdata
->blank_at_eof_in_postimage
&&
517 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
518 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
520 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
523 static void emit_line_checked(const char *reset
,
524 struct emit_callback
*ecbdata
,
525 const char *line
, int len
,
526 enum color_diff color
,
527 unsigned ws_error_highlight
,
530 const char *set
= diff_get_color(ecbdata
->color_diff
, color
);
531 const char *ws
= NULL
;
533 if (ecbdata
->opt
->ws_error_highlight
& ws_error_highlight
) {
534 ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
540 emit_line_0(ecbdata
->opt
, set
, reset
, sign
, line
, len
);
541 else if (sign
== '+' && new_blank_line_at_eof(ecbdata
, line
, len
))
542 /* Blank line at EOF - paint '+' as well */
543 emit_line_0(ecbdata
->opt
, ws
, reset
, sign
, line
, len
);
545 /* Emit just the prefix, then the rest. */
546 emit_line_0(ecbdata
->opt
, set
, reset
, sign
, "", 0);
547 ws_check_emit(line
, len
, ecbdata
->ws_rule
,
548 ecbdata
->opt
->file
, set
, reset
, ws
);
552 static void emit_add_line(const char *reset
,
553 struct emit_callback
*ecbdata
,
554 const char *line
, int len
)
556 emit_line_checked(reset
, ecbdata
, line
, len
,
557 DIFF_FILE_NEW
, WSEH_NEW
, '+');
560 static void emit_del_line(const char *reset
,
561 struct emit_callback
*ecbdata
,
562 const char *line
, int len
)
564 emit_line_checked(reset
, ecbdata
, line
, len
,
565 DIFF_FILE_OLD
, WSEH_OLD
, '-');
568 static void emit_context_line(const char *reset
,
569 struct emit_callback
*ecbdata
,
570 const char *line
, int len
)
572 emit_line_checked(reset
, ecbdata
, line
, len
,
573 DIFF_CONTEXT
, WSEH_CONTEXT
, ' ');
576 static void emit_hunk_header(struct emit_callback
*ecbdata
,
577 const char *line
, int len
)
579 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
580 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
581 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
582 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
583 static const char atat
[2] = { '@', '@' };
585 struct strbuf msgbuf
= STRBUF_INIT
;
590 * As a hunk header must begin with "@@ -<old>, +<new> @@",
591 * it always is at least 10 bytes long.
594 memcmp(line
, atat
, 2) ||
595 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
596 emit_line(ecbdata
->opt
, context
, reset
, line
, len
);
599 ep
+= 2; /* skip over @@ */
601 /* The hunk header in fraginfo color */
602 strbuf_addstr(&msgbuf
, frag
);
603 strbuf_add(&msgbuf
, line
, ep
- line
);
604 strbuf_addstr(&msgbuf
, reset
);
610 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
613 /* blank before the func header */
614 for (cp
= ep
; ep
- line
< len
; ep
++)
615 if (*ep
!= ' ' && *ep
!= '\t')
618 strbuf_addstr(&msgbuf
, context
);
619 strbuf_add(&msgbuf
, cp
, ep
- cp
);
620 strbuf_addstr(&msgbuf
, reset
);
623 if (ep
< line
+ len
) {
624 strbuf_addstr(&msgbuf
, func
);
625 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
626 strbuf_addstr(&msgbuf
, reset
);
629 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
630 emit_line(ecbdata
->opt
, "", "", msgbuf
.buf
, msgbuf
.len
);
631 strbuf_release(&msgbuf
);
634 static struct diff_tempfile
*claim_diff_tempfile(void) {
636 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
637 if (!diff_temp
[i
].name
)
638 return diff_temp
+ i
;
639 die("BUG: diff is failing to clean up its tempfiles");
642 static void remove_tempfile(void)
645 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
646 if (is_tempfile_active(&diff_temp
[i
].tempfile
))
647 delete_tempfile(&diff_temp
[i
].tempfile
);
648 diff_temp
[i
].name
= NULL
;
652 static void print_line_count(FILE *file
, int count
)
656 fprintf(file
, "0,0");
662 fprintf(file
, "1,%d", count
);
667 static void emit_rewrite_lines(struct emit_callback
*ecb
,
668 int prefix
, const char *data
, int size
)
670 const char *endp
= NULL
;
671 static const char *nneof
= " No newline at end of file\n";
672 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
677 endp
= memchr(data
, '\n', size
);
678 len
= endp
? (endp
- data
+ 1) : size
;
680 ecb
->lno_in_preimage
++;
681 emit_del_line(reset
, ecb
, data
, len
);
683 ecb
->lno_in_postimage
++;
684 emit_add_line(reset
, ecb
, data
, len
);
690 const char *context
= diff_get_color(ecb
->color_diff
,
692 putc('\n', ecb
->opt
->file
);
693 emit_line_0(ecb
->opt
, context
, reset
, '\\',
694 nneof
, strlen(nneof
));
698 static void emit_rewrite_diff(const char *name_a
,
700 struct diff_filespec
*one
,
701 struct diff_filespec
*two
,
702 struct userdiff_driver
*textconv_one
,
703 struct userdiff_driver
*textconv_two
,
704 struct diff_options
*o
)
707 const char *name_a_tab
, *name_b_tab
;
708 const char *metainfo
= diff_get_color(o
->use_color
, DIFF_METAINFO
);
709 const char *fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
710 const char *reset
= diff_get_color(o
->use_color
, DIFF_RESET
);
711 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
712 const char *a_prefix
, *b_prefix
;
713 char *data_one
, *data_two
;
714 size_t size_one
, size_two
;
715 struct emit_callback ecbdata
;
716 const char *line_prefix
= diff_line_prefix(o
);
718 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
719 a_prefix
= o
->b_prefix
;
720 b_prefix
= o
->a_prefix
;
722 a_prefix
= o
->a_prefix
;
723 b_prefix
= o
->b_prefix
;
726 name_a
+= (*name_a
== '/');
727 name_b
+= (*name_b
== '/');
728 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
729 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
731 strbuf_reset(&a_name
);
732 strbuf_reset(&b_name
);
733 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
734 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
736 size_one
= fill_textconv(textconv_one
, one
, &data_one
);
737 size_two
= fill_textconv(textconv_two
, two
, &data_two
);
739 memset(&ecbdata
, 0, sizeof(ecbdata
));
740 ecbdata
.color_diff
= want_color(o
->use_color
);
741 ecbdata
.ws_rule
= whitespace_rule(name_b
);
743 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
745 mf1
.ptr
= (char *)data_one
;
746 mf2
.ptr
= (char *)data_two
;
749 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
751 ecbdata
.lno_in_preimage
= 1;
752 ecbdata
.lno_in_postimage
= 1;
754 lc_a
= count_lines(data_one
, size_one
);
755 lc_b
= count_lines(data_two
, size_two
);
757 "%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -",
758 line_prefix
, metainfo
, a_name
.buf
, name_a_tab
, reset
,
759 line_prefix
, metainfo
, b_name
.buf
, name_b_tab
, reset
,
760 line_prefix
, fraginfo
);
761 if (!o
->irreversible_delete
)
762 print_line_count(o
->file
, lc_a
);
764 fprintf(o
->file
, "?,?");
765 fprintf(o
->file
, " +");
766 print_line_count(o
->file
, lc_b
);
767 fprintf(o
->file
, " @@%s\n", reset
);
768 if (lc_a
&& !o
->irreversible_delete
)
769 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
771 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
773 free((char *)data_one
);
775 free((char *)data_two
);
778 struct diff_words_buffer
{
781 struct diff_words_orig
{
782 const char *begin
, *end
;
784 int orig_nr
, orig_alloc
;
787 static void diff_words_append(char *line
, unsigned long len
,
788 struct diff_words_buffer
*buffer
)
790 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
793 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
794 buffer
->text
.size
+= len
;
795 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
798 struct diff_words_style_elem
{
801 const char *color
; /* NULL; filled in by the setup code if
802 * color is enabled */
805 struct diff_words_style
{
806 enum diff_words_type type
;
807 struct diff_words_style_elem
new, old
, ctx
;
811 static struct diff_words_style diff_words_styles
[] = {
812 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
813 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
814 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
817 struct diff_words_data
{
818 struct diff_words_buffer minus
, plus
;
819 const char *current_plus
;
821 struct diff_options
*opt
;
823 enum diff_words_type type
;
824 struct diff_words_style
*style
;
827 static int fn_out_diff_words_write_helper(FILE *fp
,
828 struct diff_words_style_elem
*st_el
,
830 size_t count
, const char *buf
,
831 const char *line_prefix
)
836 char *p
= memchr(buf
, '\n', count
);
838 fputs(line_prefix
, fp
);
840 if (st_el
->color
&& fputs(st_el
->color
, fp
) < 0)
842 if (fputs(st_el
->prefix
, fp
) < 0 ||
843 fwrite(buf
, p
? p
- buf
: count
, 1, fp
) != 1 ||
844 fputs(st_el
->suffix
, fp
) < 0)
846 if (st_el
->color
&& *st_el
->color
847 && fputs(GIT_COLOR_RESET
, fp
) < 0)
852 if (fputs(newline
, fp
) < 0)
854 count
-= p
+ 1 - buf
;
862 * '--color-words' algorithm can be described as:
864 * 1. collect a the minus/plus lines of a diff hunk, divided into
865 * minus-lines and plus-lines;
867 * 2. break both minus-lines and plus-lines into words and
868 * place them into two mmfile_t with one word for each line;
870 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
872 * And for the common parts of the both file, we output the plus side text.
873 * diff_words->current_plus is used to trace the current position of the plus file
874 * which printed. diff_words->last_minus is used to trace the last minus word
877 * For '--graph' to work with '--color-words', we need to output the graph prefix
878 * on each line of color words output. Generally, there are two conditions on
879 * which we should output the prefix.
881 * 1. diff_words->last_minus == 0 &&
882 * diff_words->current_plus == diff_words->plus.text.ptr
884 * that is: the plus text must start as a new line, and if there is no minus
885 * word printed, a graph prefix must be printed.
887 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
888 * *(diff_words->current_plus - 1) == '\n'
890 * that is: a graph prefix must be printed following a '\n'
892 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
894 if ((diff_words
->last_minus
== 0 &&
895 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
896 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
897 *(diff_words
->current_plus
- 1) == '\n')) {
904 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
906 struct diff_words_data
*diff_words
= priv
;
907 struct diff_words_style
*style
= diff_words
->style
;
908 int minus_first
, minus_len
, plus_first
, plus_len
;
909 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
910 struct diff_options
*opt
= diff_words
->opt
;
911 const char *line_prefix
;
913 if (line
[0] != '@' || parse_hunk_header(line
, len
,
914 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
918 line_prefix
= diff_line_prefix(opt
);
920 /* POSIX requires that first be decremented by one if len == 0... */
922 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
924 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
926 minus_begin
= minus_end
=
927 diff_words
->minus
.orig
[minus_first
].end
;
930 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
931 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
933 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
935 if (color_words_output_graph_prefix(diff_words
)) {
936 fputs(line_prefix
, diff_words
->opt
->file
);
938 if (diff_words
->current_plus
!= plus_begin
) {
939 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
940 &style
->ctx
, style
->newline
,
941 plus_begin
- diff_words
->current_plus
,
942 diff_words
->current_plus
, line_prefix
);
943 if (*(plus_begin
- 1) == '\n')
944 fputs(line_prefix
, diff_words
->opt
->file
);
946 if (minus_begin
!= minus_end
) {
947 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
948 &style
->old
, style
->newline
,
949 minus_end
- minus_begin
, minus_begin
,
952 if (plus_begin
!= plus_end
) {
953 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
954 &style
->new, style
->newline
,
955 plus_end
- plus_begin
, plus_begin
,
959 diff_words
->current_plus
= plus_end
;
960 diff_words
->last_minus
= minus_first
;
963 /* This function starts looking at *begin, and returns 0 iff a word was found. */
964 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
965 int *begin
, int *end
)
967 if (word_regex
&& *begin
< buffer
->size
) {
969 if (!regexec(word_regex
, buffer
->ptr
+ *begin
, 1, match
, 0)) {
970 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
971 '\n', match
[0].rm_eo
- match
[0].rm_so
);
972 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
973 *begin
+= match
[0].rm_so
;
974 return *begin
>= *end
;
979 /* find the next word */
980 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
982 if (*begin
>= buffer
->size
)
985 /* find the end of the word */
987 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
994 * This function splits the words in buffer->text, stores the list with
995 * newline separator into out, and saves the offsets of the original words
998 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
1007 /* fake an empty "0th" word */
1008 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
1009 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
1010 buffer
->orig_nr
= 1;
1012 for (i
= 0; i
< buffer
->text
.size
; i
++) {
1013 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
1016 /* store original boundaries */
1017 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
1018 buffer
->orig_alloc
);
1019 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
1020 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
1023 /* store one word */
1024 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
1025 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
1026 out
->ptr
[out
->size
+ j
- i
] = '\n';
1027 out
->size
+= j
- i
+ 1;
1033 /* this executes the word diff on the accumulated buffers */
1034 static void diff_words_show(struct diff_words_data
*diff_words
)
1038 mmfile_t minus
, plus
;
1039 struct diff_words_style
*style
= diff_words
->style
;
1041 struct diff_options
*opt
= diff_words
->opt
;
1042 const char *line_prefix
;
1045 line_prefix
= diff_line_prefix(opt
);
1047 /* special case: only removal */
1048 if (!diff_words
->plus
.text
.size
) {
1049 fputs(line_prefix
, diff_words
->opt
->file
);
1050 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
1051 &style
->old
, style
->newline
,
1052 diff_words
->minus
.text
.size
,
1053 diff_words
->minus
.text
.ptr
, line_prefix
);
1054 diff_words
->minus
.text
.size
= 0;
1058 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
1059 diff_words
->last_minus
= 0;
1061 memset(&xpp
, 0, sizeof(xpp
));
1062 memset(&xecfg
, 0, sizeof(xecfg
));
1063 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
1064 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
1066 /* as only the hunk header will be parsed, we need a 0-context */
1068 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
1070 die("unable to generate word diff");
1073 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
1074 diff_words
->plus
.text
.size
) {
1075 if (color_words_output_graph_prefix(diff_words
))
1076 fputs(line_prefix
, diff_words
->opt
->file
);
1077 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
1078 &style
->ctx
, style
->newline
,
1079 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
1080 - diff_words
->current_plus
, diff_words
->current_plus
,
1083 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
1086 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
1087 static void diff_words_flush(struct emit_callback
*ecbdata
)
1089 if (ecbdata
->diff_words
->minus
.text
.size
||
1090 ecbdata
->diff_words
->plus
.text
.size
)
1091 diff_words_show(ecbdata
->diff_words
);
1094 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1096 /* Use already-loaded driver */
1100 if (S_ISREG(one
->mode
))
1101 one
->driver
= userdiff_find_by_path(one
->path
);
1103 /* Fallback to default settings */
1105 one
->driver
= userdiff_find_by_name("default");
1108 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1110 diff_filespec_load_driver(one
);
1111 return one
->driver
->word_regex
;
1114 static void init_diff_words_data(struct emit_callback
*ecbdata
,
1115 struct diff_options
*orig_opts
,
1116 struct diff_filespec
*one
,
1117 struct diff_filespec
*two
)
1120 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
1121 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
1123 ecbdata
->diff_words
=
1124 xcalloc(1, sizeof(struct diff_words_data
));
1125 ecbdata
->diff_words
->type
= o
->word_diff
;
1126 ecbdata
->diff_words
->opt
= o
;
1128 o
->word_regex
= userdiff_word_regex(one
);
1130 o
->word_regex
= userdiff_word_regex(two
);
1132 o
->word_regex
= diff_word_regex_cfg
;
1133 if (o
->word_regex
) {
1134 ecbdata
->diff_words
->word_regex
= (regex_t
*)
1135 xmalloc(sizeof(regex_t
));
1136 if (regcomp(ecbdata
->diff_words
->word_regex
,
1138 REG_EXTENDED
| REG_NEWLINE
))
1139 die ("Invalid regular expression: %s",
1142 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
1143 if (o
->word_diff
== diff_words_styles
[i
].type
) {
1144 ecbdata
->diff_words
->style
=
1145 &diff_words_styles
[i
];
1149 if (want_color(o
->use_color
)) {
1150 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
1151 st
->old
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1152 st
->new.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1153 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1157 static void free_diff_words_data(struct emit_callback
*ecbdata
)
1159 if (ecbdata
->diff_words
) {
1160 diff_words_flush(ecbdata
);
1161 free (ecbdata
->diff_words
->opt
);
1162 free (ecbdata
->diff_words
->minus
.text
.ptr
);
1163 free (ecbdata
->diff_words
->minus
.orig
);
1164 free (ecbdata
->diff_words
->plus
.text
.ptr
);
1165 free (ecbdata
->diff_words
->plus
.orig
);
1166 if (ecbdata
->diff_words
->word_regex
) {
1167 regfree(ecbdata
->diff_words
->word_regex
);
1168 free(ecbdata
->diff_words
->word_regex
);
1170 free(ecbdata
->diff_words
);
1171 ecbdata
->diff_words
= NULL
;
1175 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
1177 if (want_color(diff_use_color
))
1178 return diff_colors
[ix
];
1182 const char *diff_line_prefix(struct diff_options
*opt
)
1184 struct strbuf
*msgbuf
;
1185 if (!opt
->output_prefix
)
1188 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1192 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
1195 unsigned long allot
;
1199 return ecb
->truncate(line
, len
);
1203 (void) utf8_width(&cp
, &l
);
1205 break; /* truncated in the middle? */
1210 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
1213 ecbdata
->lno_in_preimage
= 0;
1214 ecbdata
->lno_in_postimage
= 0;
1215 p
= strchr(line
, '-');
1217 return; /* cannot happen */
1218 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
1221 return; /* cannot happen */
1222 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
1225 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
1227 struct emit_callback
*ecbdata
= priv
;
1228 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
1229 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1230 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1231 struct diff_options
*o
= ecbdata
->opt
;
1232 const char *line_prefix
= diff_line_prefix(o
);
1234 o
->found_changes
= 1;
1236 if (ecbdata
->header
) {
1237 fprintf(o
->file
, "%s", ecbdata
->header
->buf
);
1238 strbuf_reset(ecbdata
->header
);
1239 ecbdata
->header
= NULL
;
1242 if (ecbdata
->label_path
[0]) {
1243 const char *name_a_tab
, *name_b_tab
;
1245 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
1246 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
1248 fprintf(o
->file
, "%s%s--- %s%s%s\n",
1249 line_prefix
, meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
1250 fprintf(o
->file
, "%s%s+++ %s%s%s\n",
1251 line_prefix
, meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
1252 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
1255 if (diff_suppress_blank_empty
1256 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
1261 if (line
[0] == '@') {
1262 if (ecbdata
->diff_words
)
1263 diff_words_flush(ecbdata
);
1264 len
= sane_truncate_line(ecbdata
, line
, len
);
1265 find_lno(line
, ecbdata
);
1266 emit_hunk_header(ecbdata
, line
, len
);
1267 if (line
[len
-1] != '\n')
1268 putc('\n', o
->file
);
1272 if (ecbdata
->diff_words
) {
1273 if (line
[0] == '-') {
1274 diff_words_append(line
, len
,
1275 &ecbdata
->diff_words
->minus
);
1277 } else if (line
[0] == '+') {
1278 diff_words_append(line
, len
,
1279 &ecbdata
->diff_words
->plus
);
1281 } else if (starts_with(line
, "\\ ")) {
1283 * Eat the "no newline at eof" marker as if we
1284 * saw a "+" or "-" line with nothing on it,
1285 * and return without diff_words_flush() to
1286 * defer processing. If this is the end of
1287 * preimage, more "+" lines may come after it.
1291 diff_words_flush(ecbdata
);
1292 if (ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
) {
1293 emit_line(o
, context
, reset
, line
, len
);
1294 fputs("~\n", o
->file
);
1297 * Skip the prefix character, if any. With
1298 * diff_suppress_blank_empty, there may be
1301 if (line
[0] != '\n') {
1305 emit_line(o
, context
, reset
, line
, len
);
1312 ecbdata
->lno_in_postimage
++;
1313 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
1316 ecbdata
->lno_in_preimage
++;
1317 emit_del_line(reset
, ecbdata
, line
+ 1, len
- 1);
1320 ecbdata
->lno_in_postimage
++;
1321 ecbdata
->lno_in_preimage
++;
1322 emit_context_line(reset
, ecbdata
, line
+ 1, len
- 1);
1325 /* incomplete line at the end */
1326 ecbdata
->lno_in_preimage
++;
1327 emit_line(o
, diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
),
1333 static char *pprint_rename(const char *a
, const char *b
)
1335 const char *old
= a
;
1336 const char *new = b
;
1337 struct strbuf name
= STRBUF_INIT
;
1338 int pfx_length
, sfx_length
;
1339 int pfx_adjust_for_slash
;
1340 int len_a
= strlen(a
);
1341 int len_b
= strlen(b
);
1342 int a_midlen
, b_midlen
;
1343 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
1344 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
1346 if (qlen_a
|| qlen_b
) {
1347 quote_c_style(a
, &name
, NULL
, 0);
1348 strbuf_addstr(&name
, " => ");
1349 quote_c_style(b
, &name
, NULL
, 0);
1350 return strbuf_detach(&name
, NULL
);
1353 /* Find common prefix */
1355 while (*old
&& *new && *old
== *new) {
1357 pfx_length
= old
- a
+ 1;
1362 /* Find common suffix */
1367 * If there is a common prefix, it must end in a slash. In
1368 * that case we let this loop run 1 into the prefix to see the
1371 * If there is no common prefix, we cannot do this as it would
1372 * underrun the input strings.
1374 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
1375 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old
&&
1376 b
+ pfx_length
- pfx_adjust_for_slash
<= new &&
1379 sfx_length
= len_a
- (old
- a
);
1385 * pfx{mid-a => mid-b}sfx
1386 * {pfx-a => pfx-b}sfx
1387 * pfx{sfx-a => sfx-b}
1390 a_midlen
= len_a
- pfx_length
- sfx_length
;
1391 b_midlen
= len_b
- pfx_length
- sfx_length
;
1397 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
1398 if (pfx_length
+ sfx_length
) {
1399 strbuf_add(&name
, a
, pfx_length
);
1400 strbuf_addch(&name
, '{');
1402 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
1403 strbuf_addstr(&name
, " => ");
1404 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
1405 if (pfx_length
+ sfx_length
) {
1406 strbuf_addch(&name
, '}');
1407 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
1409 return strbuf_detach(&name
, NULL
);
1415 struct diffstat_file
{
1419 unsigned is_unmerged
:1;
1420 unsigned is_binary
:1;
1421 unsigned is_renamed
:1;
1422 unsigned is_interesting
:1;
1423 uintmax_t added
, deleted
;
1427 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
1431 struct diffstat_file
*x
;
1432 x
= xcalloc(1, sizeof(*x
));
1433 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
1434 diffstat
->files
[diffstat
->nr
++] = x
;
1436 x
->from_name
= xstrdup(name_a
);
1437 x
->name
= xstrdup(name_b
);
1441 x
->from_name
= NULL
;
1442 x
->name
= xstrdup(name_a
);
1447 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
1449 struct diffstat_t
*diffstat
= priv
;
1450 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
1454 else if (line
[0] == '-')
1458 const char mime_boundary_leader
[] = "------------";
1460 static int scale_linear(int it
, int width
, int max_change
)
1465 * make sure that at least one '-' or '+' is printed if
1466 * there is any change to this path. The easiest way is to
1467 * scale linearly as if the alloted width is one column shorter
1468 * than it is, and then add 1 to the result.
1470 return 1 + (it
* (width
- 1) / max_change
);
1473 static void show_name(FILE *file
,
1474 const char *prefix
, const char *name
, int len
)
1476 fprintf(file
, " %s%-*s |", prefix
, len
, name
);
1479 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
1483 fprintf(file
, "%s", set
);
1486 fprintf(file
, "%s", reset
);
1489 static void fill_print_name(struct diffstat_file
*file
)
1493 if (file
->print_name
)
1496 if (!file
->is_renamed
) {
1497 struct strbuf buf
= STRBUF_INIT
;
1498 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
1499 pname
= strbuf_detach(&buf
, NULL
);
1502 strbuf_release(&buf
);
1505 pname
= pprint_rename(file
->from_name
, file
->name
);
1507 file
->print_name
= pname
;
1510 int print_stat_summary(FILE *fp
, int files
, int insertions
, int deletions
)
1512 struct strbuf sb
= STRBUF_INIT
;
1516 assert(insertions
== 0 && deletions
== 0);
1517 return fprintf(fp
, "%s\n", " 0 files changed");
1521 (files
== 1) ? " %d file changed" : " %d files changed",
1525 * For binary diff, the caller may want to print "x files
1526 * changed" with insertions == 0 && deletions == 0.
1528 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
1529 * is probably less confusing (i.e skip over "2 files changed
1530 * but nothing about added/removed lines? Is this a bug in Git?").
1532 if (insertions
|| deletions
== 0) {
1534 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
1538 if (deletions
|| insertions
== 0) {
1540 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
1543 strbuf_addch(&sb
, '\n');
1544 ret
= fputs(sb
.buf
, fp
);
1545 strbuf_release(&sb
);
1549 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
1551 int i
, len
, add
, del
, adds
= 0, dels
= 0;
1552 uintmax_t max_change
= 0, max_len
= 0;
1553 int total_files
= data
->nr
, count
;
1554 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
1555 const char *reset
, *add_c
, *del_c
;
1556 const char *line_prefix
= "";
1557 int extra_shown
= 0;
1562 line_prefix
= diff_line_prefix(options
);
1563 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
1565 reset
= diff_get_color_opt(options
, DIFF_RESET
);
1566 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
1567 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
1570 * Find the longest filename and max number of changes
1572 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
1573 struct diffstat_file
*file
= data
->files
[i
];
1574 uintmax_t change
= file
->added
+ file
->deleted
;
1576 if (!file
->is_interesting
&& (change
== 0)) {
1577 count
++; /* not shown == room for one more */
1580 fill_print_name(file
);
1581 len
= strlen(file
->print_name
);
1585 if (file
->is_unmerged
) {
1586 /* "Unmerged" is 8 characters */
1587 bin_width
= bin_width
< 8 ? 8 : bin_width
;
1590 if (file
->is_binary
) {
1591 /* "Bin XXX -> YYY bytes" */
1592 int w
= 14 + decimal_width(file
->added
)
1593 + decimal_width(file
->deleted
);
1594 bin_width
= bin_width
< w
? w
: bin_width
;
1595 /* Display change counts aligned with "Bin" */
1600 if (max_change
< change
)
1601 max_change
= change
;
1603 count
= i
; /* where we can stop scanning in data->files[] */
1606 * We have width = stat_width or term_columns() columns total.
1607 * We want a maximum of min(max_len, stat_name_width) for the name part.
1608 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
1609 * We also need 1 for " " and 4 + decimal_width(max_change)
1610 * for " | NNNN " and one the empty column at the end, altogether
1611 * 6 + decimal_width(max_change).
1613 * If there's not enough space, we will use the smaller of
1614 * stat_name_width (if set) and 5/8*width for the filename,
1615 * and the rest for constant elements + graph part, but no more
1616 * than stat_graph_width for the graph part.
1617 * (5/8 gives 50 for filename and 30 for the constant parts + graph
1618 * for the standard terminal size).
1620 * In other words: stat_width limits the maximum width, and
1621 * stat_name_width fixes the maximum width of the filename,
1622 * and is also used to divide available columns if there
1625 * Binary files are displayed with "Bin XXX -> YYY bytes"
1626 * instead of the change count and graph. This part is treated
1627 * similarly to the graph part, except that it is not
1628 * "scaled". If total width is too small to accommodate the
1629 * guaranteed minimum width of the filename part and the
1630 * separators and this message, this message will "overflow"
1631 * making the line longer than the maximum width.
1634 if (options
->stat_width
== -1)
1635 width
= term_columns() - strlen(line_prefix
);
1637 width
= options
->stat_width
? options
->stat_width
: 80;
1638 number_width
= decimal_width(max_change
) > number_width
?
1639 decimal_width(max_change
) : number_width
;
1641 if (options
->stat_graph_width
== -1)
1642 options
->stat_graph_width
= diff_stat_graph_width
;
1645 * Guarantee 3/8*16==6 for the graph part
1646 * and 5/8*16==10 for the filename part
1648 if (width
< 16 + 6 + number_width
)
1649 width
= 16 + 6 + number_width
;
1652 * First assign sizes that are wanted, ignoring available width.
1653 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
1654 * starting from "XXX" should fit in graph_width.
1656 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
1657 if (options
->stat_graph_width
&&
1658 options
->stat_graph_width
< graph_width
)
1659 graph_width
= options
->stat_graph_width
;
1661 name_width
= (options
->stat_name_width
> 0 &&
1662 options
->stat_name_width
< max_len
) ?
1663 options
->stat_name_width
: max_len
;
1666 * Adjust adjustable widths not to exceed maximum width
1668 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
1669 if (graph_width
> width
* 3/8 - number_width
- 6) {
1670 graph_width
= width
* 3/8 - number_width
- 6;
1671 if (graph_width
< 6)
1675 if (options
->stat_graph_width
&&
1676 graph_width
> options
->stat_graph_width
)
1677 graph_width
= options
->stat_graph_width
;
1678 if (name_width
> width
- number_width
- 6 - graph_width
)
1679 name_width
= width
- number_width
- 6 - graph_width
;
1681 graph_width
= width
- number_width
- 6 - name_width
;
1685 * From here name_width is the width of the name area,
1686 * and graph_width is the width of the graph area.
1687 * max_change is used to scale graph properly.
1689 for (i
= 0; i
< count
; i
++) {
1690 const char *prefix
= "";
1691 struct diffstat_file
*file
= data
->files
[i
];
1692 char *name
= file
->print_name
;
1693 uintmax_t added
= file
->added
;
1694 uintmax_t deleted
= file
->deleted
;
1697 if (!file
->is_interesting
&& (added
+ deleted
== 0))
1701 * "scale" the filename
1704 name_len
= strlen(name
);
1705 if (name_width
< name_len
) {
1709 name
+= name_len
- len
;
1710 slash
= strchr(name
, '/');
1715 if (file
->is_binary
) {
1716 fprintf(options
->file
, "%s", line_prefix
);
1717 show_name(options
->file
, prefix
, name
, len
);
1718 fprintf(options
->file
, " %*s", number_width
, "Bin");
1719 if (!added
&& !deleted
) {
1720 putc('\n', options
->file
);
1723 fprintf(options
->file
, " %s%"PRIuMAX
"%s",
1724 del_c
, deleted
, reset
);
1725 fprintf(options
->file
, " -> ");
1726 fprintf(options
->file
, "%s%"PRIuMAX
"%s",
1727 add_c
, added
, reset
);
1728 fprintf(options
->file
, " bytes");
1729 fprintf(options
->file
, "\n");
1732 else if (file
->is_unmerged
) {
1733 fprintf(options
->file
, "%s", line_prefix
);
1734 show_name(options
->file
, prefix
, name
, len
);
1735 fprintf(options
->file
, " Unmerged\n");
1740 * scale the add/delete
1745 if (graph_width
<= max_change
) {
1746 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
1747 if (total
< 2 && add
&& del
)
1748 /* width >= 2 due to the sanity check */
1751 add
= scale_linear(add
, graph_width
, max_change
);
1754 del
= scale_linear(del
, graph_width
, max_change
);
1758 fprintf(options
->file
, "%s", line_prefix
);
1759 show_name(options
->file
, prefix
, name
, len
);
1760 fprintf(options
->file
, " %*"PRIuMAX
"%s",
1761 number_width
, added
+ deleted
,
1762 added
+ deleted
? " " : "");
1763 show_graph(options
->file
, '+', add
, add_c
, reset
);
1764 show_graph(options
->file
, '-', del
, del_c
, reset
);
1765 fprintf(options
->file
, "\n");
1768 for (i
= 0; i
< data
->nr
; i
++) {
1769 struct diffstat_file
*file
= data
->files
[i
];
1770 uintmax_t added
= file
->added
;
1771 uintmax_t deleted
= file
->deleted
;
1773 if (file
->is_unmerged
||
1774 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
1779 if (!file
->is_binary
) {
1786 fprintf(options
->file
, "%s ...\n", line_prefix
);
1789 fprintf(options
->file
, "%s", line_prefix
);
1790 print_stat_summary(options
->file
, total_files
, adds
, dels
);
1793 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
1795 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
1800 for (i
= 0; i
< data
->nr
; i
++) {
1801 int added
= data
->files
[i
]->added
;
1802 int deleted
= data
->files
[i
]->deleted
;
1804 if (data
->files
[i
]->is_unmerged
||
1805 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
1807 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
1812 fprintf(options
->file
, "%s", diff_line_prefix(options
));
1813 print_stat_summary(options
->file
, total_files
, adds
, dels
);
1816 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
1823 for (i
= 0; i
< data
->nr
; i
++) {
1824 struct diffstat_file
*file
= data
->files
[i
];
1826 fprintf(options
->file
, "%s", diff_line_prefix(options
));
1828 if (file
->is_binary
)
1829 fprintf(options
->file
, "-\t-\t");
1831 fprintf(options
->file
,
1832 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
1833 file
->added
, file
->deleted
);
1834 if (options
->line_termination
) {
1835 fill_print_name(file
);
1836 if (!file
->is_renamed
)
1837 write_name_quoted(file
->name
, options
->file
,
1838 options
->line_termination
);
1840 fputs(file
->print_name
, options
->file
);
1841 putc(options
->line_termination
, options
->file
);
1844 if (file
->is_renamed
) {
1845 putc('\0', options
->file
);
1846 write_name_quoted(file
->from_name
, options
->file
, '\0');
1848 write_name_quoted(file
->name
, options
->file
, '\0');
1853 struct dirstat_file
{
1855 unsigned long changed
;
1858 struct dirstat_dir
{
1859 struct dirstat_file
*files
;
1860 int alloc
, nr
, permille
, cumulative
;
1863 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
1864 unsigned long changed
, const char *base
, int baselen
)
1866 unsigned long this_dir
= 0;
1867 unsigned int sources
= 0;
1868 const char *line_prefix
= diff_line_prefix(opt
);
1871 struct dirstat_file
*f
= dir
->files
;
1872 int namelen
= strlen(f
->name
);
1876 if (namelen
< baselen
)
1878 if (memcmp(f
->name
, base
, baselen
))
1880 slash
= strchr(f
->name
+ baselen
, '/');
1882 int newbaselen
= slash
+ 1 - f
->name
;
1883 this = gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
1895 * We don't report dirstat's for
1897 * - or cases where everything came from a single directory
1898 * under this directory (sources == 1).
1900 if (baselen
&& sources
!= 1) {
1902 int permille
= this_dir
* 1000 / changed
;
1903 if (permille
>= dir
->permille
) {
1904 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
1905 permille
/ 10, permille
% 10, baselen
, base
);
1906 if (!dir
->cumulative
)
1914 static int dirstat_compare(const void *_a
, const void *_b
)
1916 const struct dirstat_file
*a
= _a
;
1917 const struct dirstat_file
*b
= _b
;
1918 return strcmp(a
->name
, b
->name
);
1921 static void show_dirstat(struct diff_options
*options
)
1924 unsigned long changed
;
1925 struct dirstat_dir dir
;
1926 struct diff_queue_struct
*q
= &diff_queued_diff
;
1931 dir
.permille
= options
->dirstat_permille
;
1932 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1935 for (i
= 0; i
< q
->nr
; i
++) {
1936 struct diff_filepair
*p
= q
->queue
[i
];
1938 unsigned long copied
, added
, damage
;
1939 int content_changed
;
1941 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
1943 if (p
->one
->oid_valid
&& p
->two
->oid_valid
)
1944 content_changed
= oidcmp(&p
->one
->oid
, &p
->two
->oid
);
1946 content_changed
= 1;
1948 if (!content_changed
) {
1950 * The SHA1 has not changed, so pre-/post-content is
1951 * identical. We can therefore skip looking at the
1952 * file contents altogether.
1958 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
)) {
1960 * In --dirstat-by-file mode, we don't really need to
1961 * look at the actual file contents at all.
1962 * The fact that the SHA1 changed is enough for us to
1963 * add this file to the list of results
1964 * (with each file contributing equal damage).
1970 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1971 diff_populate_filespec(p
->one
, 0);
1972 diff_populate_filespec(p
->two
, 0);
1973 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1975 diff_free_filespec_data(p
->one
);
1976 diff_free_filespec_data(p
->two
);
1977 } else if (DIFF_FILE_VALID(p
->one
)) {
1978 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
);
1980 diff_free_filespec_data(p
->one
);
1981 } else if (DIFF_FILE_VALID(p
->two
)) {
1982 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
);
1984 added
= p
->two
->size
;
1985 diff_free_filespec_data(p
->two
);
1990 * Original minus copied is the removed material,
1991 * added is the new material. They are both damages
1992 * made to the preimage.
1993 * If the resulting damage is zero, we know that
1994 * diffcore_count_changes() considers the two entries to
1995 * be identical, but since content_changed is true, we
1996 * know that there must have been _some_ kind of change,
1997 * so we force all entries to have damage > 0.
1999 damage
= (p
->one
->size
- copied
) + added
;
2004 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2005 dir
.files
[dir
.nr
].name
= name
;
2006 dir
.files
[dir
.nr
].changed
= damage
;
2011 /* This can happen even with many files, if everything was renames */
2015 /* Show all directories with more than x% of the changes */
2016 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
2017 gather_dirstat(options
, &dir
, changed
, "", 0);
2020 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
2023 unsigned long changed
;
2024 struct dirstat_dir dir
;
2032 dir
.permille
= options
->dirstat_permille
;
2033 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
2036 for (i
= 0; i
< data
->nr
; i
++) {
2037 struct diffstat_file
*file
= data
->files
[i
];
2038 unsigned long damage
= file
->added
+ file
->deleted
;
2039 if (file
->is_binary
)
2041 * binary files counts bytes, not lines. Must find some
2042 * way to normalize binary bytes vs. textual lines.
2043 * The following heuristic assumes that there are 64
2045 * This is stupid and ugly, but very cheap...
2047 damage
= (damage
+ 63) / 64;
2048 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2049 dir
.files
[dir
.nr
].name
= file
->name
;
2050 dir
.files
[dir
.nr
].changed
= damage
;
2055 /* This can happen even with many files, if everything was renames */
2059 /* Show all directories with more than x% of the changes */
2060 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
2061 gather_dirstat(options
, &dir
, changed
, "", 0);
2064 static void free_diffstat_info(struct diffstat_t
*diffstat
)
2067 for (i
= 0; i
< diffstat
->nr
; i
++) {
2068 struct diffstat_file
*f
= diffstat
->files
[i
];
2069 if (f
->name
!= f
->print_name
)
2070 free(f
->print_name
);
2075 free(diffstat
->files
);
2078 struct checkdiff_t
{
2079 const char *filename
;
2081 int conflict_marker_size
;
2082 struct diff_options
*o
;
2087 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
2092 if (len
< marker_size
+ 1)
2094 firstchar
= line
[0];
2095 switch (firstchar
) {
2096 case '=': case '>': case '<': case '|':
2101 for (cnt
= 1; cnt
< marker_size
; cnt
++)
2102 if (line
[cnt
] != firstchar
)
2104 /* line[1] thru line[marker_size-1] are same as firstchar */
2105 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
2110 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
2112 struct checkdiff_t
*data
= priv
;
2113 int marker_size
= data
->conflict_marker_size
;
2114 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
2115 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
2116 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
2118 const char *line_prefix
;
2121 line_prefix
= diff_line_prefix(data
->o
);
2123 if (line
[0] == '+') {
2126 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
2128 fprintf(data
->o
->file
,
2129 "%s%s:%d: leftover conflict marker\n",
2130 line_prefix
, data
->filename
, data
->lineno
);
2132 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
2135 data
->status
|= bad
;
2136 err
= whitespace_error_string(bad
);
2137 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
2138 line_prefix
, data
->filename
, data
->lineno
, err
);
2140 emit_line(data
->o
, set
, reset
, line
, 1);
2141 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
2142 data
->o
->file
, set
, reset
, ws
);
2143 } else if (line
[0] == ' ') {
2145 } else if (line
[0] == '@') {
2146 char *plus
= strchr(line
, '+');
2148 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
2150 die("invalid diff");
2154 static unsigned char *deflate_it(char *data
,
2156 unsigned long *result_size
)
2159 unsigned char *deflated
;
2162 git_deflate_init(&stream
, zlib_compression_level
);
2163 bound
= git_deflate_bound(&stream
, size
);
2164 deflated
= xmalloc(bound
);
2165 stream
.next_out
= deflated
;
2166 stream
.avail_out
= bound
;
2168 stream
.next_in
= (unsigned char *)data
;
2169 stream
.avail_in
= size
;
2170 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
2172 git_deflate_end(&stream
);
2173 *result_size
= stream
.total_out
;
2177 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
,
2184 unsigned long orig_size
;
2185 unsigned long delta_size
;
2186 unsigned long deflate_size
;
2187 unsigned long data_size
;
2189 /* We could do deflated delta, or we could do just deflated two,
2190 * whichever is smaller.
2193 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
2194 if (one
->size
&& two
->size
) {
2195 delta
= diff_delta(one
->ptr
, one
->size
,
2196 two
->ptr
, two
->size
,
2197 &delta_size
, deflate_size
);
2199 void *to_free
= delta
;
2200 orig_size
= delta_size
;
2201 delta
= deflate_it(delta
, delta_size
, &delta_size
);
2206 if (delta
&& delta_size
< deflate_size
) {
2207 fprintf(file
, "%sdelta %lu\n", prefix
, orig_size
);
2210 data_size
= delta_size
;
2213 fprintf(file
, "%sliteral %lu\n", prefix
, two
->size
);
2216 data_size
= deflate_size
;
2219 /* emit data encoded in base85 */
2222 int bytes
= (52 < data_size
) ? 52 : data_size
;
2226 line
[0] = bytes
+ 'A' - 1;
2228 line
[0] = bytes
- 26 + 'a' - 1;
2229 encode_85(line
+ 1, cp
, bytes
);
2230 cp
= (char *) cp
+ bytes
;
2231 fprintf(file
, "%s", prefix
);
2235 fprintf(file
, "%s\n", prefix
);
2239 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
,
2242 fprintf(file
, "%sGIT binary patch\n", prefix
);
2243 emit_binary_diff_body(file
, one
, two
, prefix
);
2244 emit_binary_diff_body(file
, two
, one
, prefix
);
2247 int diff_filespec_is_binary(struct diff_filespec
*one
)
2249 if (one
->is_binary
== -1) {
2250 diff_filespec_load_driver(one
);
2251 if (one
->driver
->binary
!= -1)
2252 one
->is_binary
= one
->driver
->binary
;
2254 if (!one
->data
&& DIFF_FILE_VALID(one
))
2255 diff_populate_filespec(one
, CHECK_BINARY
);
2256 if (one
->is_binary
== -1 && one
->data
)
2257 one
->is_binary
= buffer_is_binary(one
->data
,
2259 if (one
->is_binary
== -1)
2263 return one
->is_binary
;
2266 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
2268 diff_filespec_load_driver(one
);
2269 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
2272 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
2274 if (!options
->a_prefix
)
2275 options
->a_prefix
= a
;
2276 if (!options
->b_prefix
)
2277 options
->b_prefix
= b
;
2280 struct userdiff_driver
*get_textconv(struct diff_filespec
*one
)
2282 if (!DIFF_FILE_VALID(one
))
2285 diff_filespec_load_driver(one
);
2286 return userdiff_get_textconv(one
->driver
);
2289 static void builtin_diff(const char *name_a
,
2291 struct diff_filespec
*one
,
2292 struct diff_filespec
*two
,
2293 const char *xfrm_msg
,
2294 int must_show_header
,
2295 struct diff_options
*o
,
2296 int complete_rewrite
)
2300 char *a_one
, *b_two
;
2301 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
2302 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
2303 const char *a_prefix
, *b_prefix
;
2304 struct userdiff_driver
*textconv_one
= NULL
;
2305 struct userdiff_driver
*textconv_two
= NULL
;
2306 struct strbuf header
= STRBUF_INIT
;
2307 const char *line_prefix
= diff_line_prefix(o
);
2309 diff_set_mnemonic_prefix(o
, "a/", "b/");
2310 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
2311 a_prefix
= o
->b_prefix
;
2312 b_prefix
= o
->a_prefix
;
2314 a_prefix
= o
->a_prefix
;
2315 b_prefix
= o
->b_prefix
;
2318 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
2319 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
2320 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
2321 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2322 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2323 show_submodule_summary(o
->file
, one
->path
? one
->path
: two
->path
,
2325 &one
->oid
, &two
->oid
,
2326 two
->dirty_submodule
,
2327 meta
, del
, add
, reset
);
2329 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
2330 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
2331 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
2332 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2333 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2334 show_submodule_inline_diff(o
->file
, one
->path
? one
->path
: two
->path
,
2336 &one
->oid
, &two
->oid
,
2337 two
->dirty_submodule
,
2338 meta
, del
, add
, reset
, o
);
2342 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
2343 textconv_one
= get_textconv(one
);
2344 textconv_two
= get_textconv(two
);
2347 /* Never use a non-valid filename anywhere if at all possible */
2348 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
2349 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
2351 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
2352 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
2353 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
2354 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
2355 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
2356 if (lbl
[0][0] == '/') {
2358 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
2360 strbuf_addstr(&header
, xfrm_msg
);
2361 must_show_header
= 1;
2363 else if (lbl
[1][0] == '/') {
2364 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
2366 strbuf_addstr(&header
, xfrm_msg
);
2367 must_show_header
= 1;
2370 if (one
->mode
!= two
->mode
) {
2371 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
2372 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
2373 must_show_header
= 1;
2376 strbuf_addstr(&header
, xfrm_msg
);
2379 * we do not run diff between different kind
2382 if ((one
->mode
^ two
->mode
) & S_IFMT
)
2383 goto free_ab_and_return
;
2384 if (complete_rewrite
&&
2385 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
2386 (textconv_two
|| !diff_filespec_is_binary(two
))) {
2387 fprintf(o
->file
, "%s", header
.buf
);
2388 strbuf_reset(&header
);
2389 emit_rewrite_diff(name_a
, name_b
, one
, two
,
2390 textconv_one
, textconv_two
, o
);
2391 o
->found_changes
= 1;
2392 goto free_ab_and_return
;
2396 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
2397 fprintf(o
->file
, "%s", header
.buf
);
2398 strbuf_reset(&header
);
2399 goto free_ab_and_return
;
2400 } else if (!DIFF_OPT_TST(o
, TEXT
) &&
2401 ( (!textconv_one
&& diff_filespec_is_binary(one
)) ||
2402 (!textconv_two
&& diff_filespec_is_binary(two
)) )) {
2403 if (!one
->data
&& !two
->data
&&
2404 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
2405 !DIFF_OPT_TST(o
, BINARY
)) {
2406 if (!oidcmp(&one
->oid
, &two
->oid
)) {
2407 if (must_show_header
)
2408 fprintf(o
->file
, "%s", header
.buf
);
2409 goto free_ab_and_return
;
2411 fprintf(o
->file
, "%s", header
.buf
);
2412 fprintf(o
->file
, "%sBinary files %s and %s differ\n",
2413 line_prefix
, lbl
[0], lbl
[1]);
2414 goto free_ab_and_return
;
2416 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
2417 die("unable to read files to diff");
2418 /* Quite common confusing case */
2419 if (mf1
.size
== mf2
.size
&&
2420 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
2421 if (must_show_header
)
2422 fprintf(o
->file
, "%s", header
.buf
);
2423 goto free_ab_and_return
;
2425 fprintf(o
->file
, "%s", header
.buf
);
2426 strbuf_reset(&header
);
2427 if (DIFF_OPT_TST(o
, BINARY
))
2428 emit_binary_diff(o
->file
, &mf1
, &mf2
, line_prefix
);
2430 fprintf(o
->file
, "%sBinary files %s and %s differ\n",
2431 line_prefix
, lbl
[0], lbl
[1]);
2432 o
->found_changes
= 1;
2434 /* Crazy xdl interfaces.. */
2435 const char *diffopts
= getenv("GIT_DIFF_OPTS");
2439 struct emit_callback ecbdata
;
2440 const struct userdiff_funcname
*pe
;
2442 if (must_show_header
) {
2443 fprintf(o
->file
, "%s", header
.buf
);
2444 strbuf_reset(&header
);
2447 mf1
.size
= fill_textconv(textconv_one
, one
, &mf1
.ptr
);
2448 mf2
.size
= fill_textconv(textconv_two
, two
, &mf2
.ptr
);
2450 pe
= diff_funcname_pattern(one
);
2452 pe
= diff_funcname_pattern(two
);
2454 memset(&xpp
, 0, sizeof(xpp
));
2455 memset(&xecfg
, 0, sizeof(xecfg
));
2456 memset(&ecbdata
, 0, sizeof(ecbdata
));
2457 ecbdata
.label_path
= lbl
;
2458 ecbdata
.color_diff
= want_color(o
->use_color
);
2459 ecbdata
.ws_rule
= whitespace_rule(name_b
);
2460 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
2461 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
2463 ecbdata
.header
= header
.len
? &header
: NULL
;
2464 xpp
.flags
= o
->xdl_opts
;
2465 xecfg
.ctxlen
= o
->context
;
2466 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
2467 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2468 if (DIFF_OPT_TST(o
, FUNCCONTEXT
))
2469 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
2471 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
2474 else if (skip_prefix(diffopts
, "--unified=", &v
))
2475 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
2476 else if (skip_prefix(diffopts
, "-u", &v
))
2477 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
2479 init_diff_words_data(&ecbdata
, o
, one
, two
);
2480 if (xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
2482 die("unable to generate diff for %s", one
->path
);
2484 free_diff_words_data(&ecbdata
);
2489 xdiff_clear_find_func(&xecfg
);
2493 strbuf_release(&header
);
2494 diff_free_filespec_data(one
);
2495 diff_free_filespec_data(two
);
2501 static void builtin_diffstat(const char *name_a
, const char *name_b
,
2502 struct diff_filespec
*one
,
2503 struct diff_filespec
*two
,
2504 struct diffstat_t
*diffstat
,
2505 struct diff_options
*o
,
2506 struct diff_filepair
*p
)
2509 struct diffstat_file
*data
;
2511 int complete_rewrite
= 0;
2513 if (!DIFF_PAIR_UNMERGED(p
)) {
2514 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2515 complete_rewrite
= 1;
2518 data
= diffstat_add(diffstat
, name_a
, name_b
);
2519 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
2522 data
->is_unmerged
= 1;
2526 same_contents
= !oidcmp(&one
->oid
, &two
->oid
);
2528 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
2529 data
->is_binary
= 1;
2530 if (same_contents
) {
2534 data
->added
= diff_filespec_size(two
);
2535 data
->deleted
= diff_filespec_size(one
);
2539 else if (complete_rewrite
) {
2540 diff_populate_filespec(one
, 0);
2541 diff_populate_filespec(two
, 0);
2542 data
->deleted
= count_lines(one
->data
, one
->size
);
2543 data
->added
= count_lines(two
->data
, two
->size
);
2546 else if (!same_contents
) {
2547 /* Crazy xdl interfaces.. */
2551 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
2552 die("unable to read files to diff");
2554 memset(&xpp
, 0, sizeof(xpp
));
2555 memset(&xecfg
, 0, sizeof(xecfg
));
2556 xpp
.flags
= o
->xdl_opts
;
2557 xecfg
.ctxlen
= o
->context
;
2558 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
2559 if (xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
2561 die("unable to generate diffstat for %s", one
->path
);
2564 diff_free_filespec_data(one
);
2565 diff_free_filespec_data(two
);
2568 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
2569 const char *attr_path
,
2570 struct diff_filespec
*one
,
2571 struct diff_filespec
*two
,
2572 struct diff_options
*o
)
2575 struct checkdiff_t data
;
2580 memset(&data
, 0, sizeof(data
));
2581 data
.filename
= name_b
? name_b
: name_a
;
2584 data
.ws_rule
= whitespace_rule(attr_path
);
2585 data
.conflict_marker_size
= ll_merge_marker_size(attr_path
);
2587 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
2588 die("unable to read files to diff");
2591 * All the other codepaths check both sides, but not checking
2592 * the "old" side here is deliberate. We are checking the newly
2593 * introduced changes, and as long as the "new" side is text, we
2594 * can and should check what it introduces.
2596 if (diff_filespec_is_binary(two
))
2597 goto free_and_return
;
2599 /* Crazy xdl interfaces.. */
2603 memset(&xpp
, 0, sizeof(xpp
));
2604 memset(&xecfg
, 0, sizeof(xecfg
));
2605 xecfg
.ctxlen
= 1; /* at least one context line */
2607 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
2609 die("unable to generate checkdiff for %s", one
->path
);
2611 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
2612 struct emit_callback ecbdata
;
2615 ecbdata
.ws_rule
= data
.ws_rule
;
2616 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
2617 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
2622 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
2623 fprintf(o
->file
, "%s:%d: %s.\n",
2624 data
.filename
, blank_at_eof
, err
);
2625 data
.status
= 1; /* report errors */
2630 diff_free_filespec_data(one
);
2631 diff_free_filespec_data(two
);
2633 DIFF_OPT_SET(o
, CHECK_FAILED
);
2636 struct diff_filespec
*alloc_filespec(const char *path
)
2638 struct diff_filespec
*spec
;
2640 FLEXPTR_ALLOC_STR(spec
, path
, path
);
2642 spec
->is_binary
= -1;
2646 void free_filespec(struct diff_filespec
*spec
)
2648 if (!--spec
->count
) {
2649 diff_free_filespec_data(spec
);
2654 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
2655 int sha1_valid
, unsigned short mode
)
2658 spec
->mode
= canon_mode(mode
);
2659 hashcpy(spec
->oid
.hash
, sha1
);
2660 spec
->oid_valid
= sha1_valid
;
2665 * Given a name and sha1 pair, if the index tells us the file in
2666 * the work tree has that object contents, return true, so that
2667 * prepare_temp_file() does not have to inflate and extract.
2669 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
2671 const struct cache_entry
*ce
;
2676 * We do not read the cache ourselves here, because the
2677 * benchmark with my previous version that always reads cache
2678 * shows that it makes things worse for diff-tree comparing
2679 * two linux-2.6 kernel trees in an already checked out work
2680 * tree. This is because most diff-tree comparisons deal with
2681 * only a small number of files, while reading the cache is
2682 * expensive for a large project, and its cost outweighs the
2683 * savings we get by not inflating the object to a temporary
2684 * file. Practically, this code only helps when we are used
2685 * by diff-cache --cached, which does read the cache before
2691 /* We want to avoid the working directory if our caller
2692 * doesn't need the data in a normal file, this system
2693 * is rather slow with its stat/open/mmap/close syscalls,
2694 * and the object is contained in a pack file. The pack
2695 * is probably already open and will be faster to obtain
2696 * the data through than the working directory. Loose
2697 * objects however would tend to be slower as they need
2698 * to be individually opened and inflated.
2700 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
))
2704 * Similarly, if we'd have to convert the file contents anyway, that
2705 * makes the optimization not worthwhile.
2707 if (!want_file
&& would_convert_to_git(name
))
2711 pos
= cache_name_pos(name
, len
);
2714 ce
= active_cache
[pos
];
2717 * This is not the sha1 we are looking for, or
2718 * unreusable because it is not a regular file.
2720 if (hashcmp(sha1
, ce
->oid
.hash
) || !S_ISREG(ce
->ce_mode
))
2724 * If ce is marked as "assume unchanged", there is no
2725 * guarantee that work tree matches what we are looking for.
2727 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2731 * If ce matches the file in the work tree, we can reuse it.
2733 if (ce_uptodate(ce
) ||
2734 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
2740 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
2742 struct strbuf buf
= STRBUF_INIT
;
2745 /* Are we looking at the work tree? */
2746 if (s
->dirty_submodule
)
2749 strbuf_addf(&buf
, "Subproject commit %s%s\n",
2750 oid_to_hex(&s
->oid
), dirty
);
2754 strbuf_release(&buf
);
2756 s
->data
= strbuf_detach(&buf
, NULL
);
2763 * While doing rename detection and pickaxe operation, we may need to
2764 * grab the data for the blob (or file) for our own in-core comparison.
2765 * diff_filespec has data and size fields for this purpose.
2767 int diff_populate_filespec(struct diff_filespec
*s
, unsigned int flags
)
2769 int size_only
= flags
& CHECK_SIZE_ONLY
;
2772 * demote FAIL to WARN to allow inspecting the situation
2773 * instead of refusing.
2775 enum safe_crlf crlf_warn
= (safe_crlf
== SAFE_CRLF_FAIL
2779 if (!DIFF_FILE_VALID(s
))
2780 die("internal error: asking to populate invalid file.");
2781 if (S_ISDIR(s
->mode
))
2787 if (size_only
&& 0 < s
->size
)
2790 if (S_ISGITLINK(s
->mode
))
2791 return diff_populate_gitlink(s
, size_only
);
2793 if (!s
->oid_valid
||
2794 reuse_worktree_file(s
->path
, s
->oid
.hash
, 0)) {
2795 struct strbuf buf
= STRBUF_INIT
;
2799 if (lstat(s
->path
, &st
) < 0) {
2800 if (errno
== ENOENT
) {
2804 s
->data
= (char *)"";
2809 s
->size
= xsize_t(st
.st_size
);
2812 if (S_ISLNK(st
.st_mode
)) {
2813 struct strbuf sb
= STRBUF_INIT
;
2815 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
2818 s
->data
= strbuf_detach(&sb
, NULL
);
2824 if ((flags
& CHECK_BINARY
) &&
2825 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
2829 fd
= open(s
->path
, O_RDONLY
);
2832 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2834 s
->should_munmap
= 1;
2837 * Convert from working tree format to canonical git format
2839 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, crlf_warn
)) {
2841 munmap(s
->data
, s
->size
);
2842 s
->should_munmap
= 0;
2843 s
->data
= strbuf_detach(&buf
, &size
);
2849 enum object_type type
;
2850 if (size_only
|| (flags
& CHECK_BINARY
)) {
2851 type
= sha1_object_info(s
->oid
.hash
, &s
->size
);
2853 die("unable to read %s",
2854 oid_to_hex(&s
->oid
));
2857 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
2862 s
->data
= read_sha1_file(s
->oid
.hash
, &type
, &s
->size
);
2864 die("unable to read %s", oid_to_hex(&s
->oid
));
2870 void diff_free_filespec_blob(struct diff_filespec
*s
)
2874 else if (s
->should_munmap
)
2875 munmap(s
->data
, s
->size
);
2877 if (s
->should_free
|| s
->should_munmap
) {
2878 s
->should_free
= s
->should_munmap
= 0;
2883 void diff_free_filespec_data(struct diff_filespec
*s
)
2885 diff_free_filespec_blob(s
);
2890 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
2893 const struct object_id
*oid
,
2897 struct strbuf buf
= STRBUF_INIT
;
2898 struct strbuf
template = STRBUF_INIT
;
2899 char *path_dup
= xstrdup(path
);
2900 const char *base
= basename(path_dup
);
2902 /* Generate "XXXXXX_basename.ext" */
2903 strbuf_addstr(&template, "XXXXXX_");
2904 strbuf_addstr(&template, base
);
2906 fd
= mks_tempfile_ts(&temp
->tempfile
, template.buf
, strlen(base
) + 1);
2908 die_errno("unable to create temp-file");
2909 if (convert_to_working_tree(path
,
2910 (const char *)blob
, (size_t)size
, &buf
)) {
2914 if (write_in_full(fd
, blob
, size
) != size
)
2915 die_errno("unable to write temp-file");
2916 close_tempfile(&temp
->tempfile
);
2917 temp
->name
= get_tempfile_path(&temp
->tempfile
);
2918 oid_to_hex_r(temp
->hex
, oid
);
2919 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
2920 strbuf_release(&buf
);
2921 strbuf_release(&template);
2925 static struct diff_tempfile
*prepare_temp_file(const char *name
,
2926 struct diff_filespec
*one
)
2928 struct diff_tempfile
*temp
= claim_diff_tempfile();
2930 if (!DIFF_FILE_VALID(one
)) {
2932 /* A '-' entry produces this for file-2, and
2933 * a '+' entry produces this for file-1.
2935 temp
->name
= "/dev/null";
2936 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
2937 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
2941 if (!S_ISGITLINK(one
->mode
) &&
2943 reuse_worktree_file(name
, one
->oid
.hash
, 1))) {
2945 if (lstat(name
, &st
) < 0) {
2946 if (errno
== ENOENT
)
2947 goto not_a_valid_file
;
2948 die_errno("stat(%s)", name
);
2950 if (S_ISLNK(st
.st_mode
)) {
2951 struct strbuf sb
= STRBUF_INIT
;
2952 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
2953 die_errno("readlink(%s)", name
);
2954 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
2956 &one
->oid
: &null_oid
),
2958 one
->mode
: S_IFLNK
));
2959 strbuf_release(&sb
);
2962 /* we can borrow from the file in the work tree */
2964 if (!one
->oid_valid
)
2965 sha1_to_hex_r(temp
->hex
, null_sha1
);
2967 sha1_to_hex_r(temp
->hex
, one
->oid
.hash
);
2968 /* Even though we may sometimes borrow the
2969 * contents from the work tree, we always want
2970 * one->mode. mode is trustworthy even when
2971 * !(one->sha1_valid), as long as
2972 * DIFF_FILE_VALID(one).
2974 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
2979 if (diff_populate_filespec(one
, 0))
2980 die("cannot read data blob for %s", one
->path
);
2981 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
2982 &one
->oid
, one
->mode
);
2987 static void add_external_diff_name(struct argv_array
*argv
,
2989 struct diff_filespec
*df
)
2991 struct diff_tempfile
*temp
= prepare_temp_file(name
, df
);
2992 argv_array_push(argv
, temp
->name
);
2993 argv_array_push(argv
, temp
->hex
);
2994 argv_array_push(argv
, temp
->mode
);
2997 /* An external diff command takes:
2999 * diff-cmd name infile1 infile1-sha1 infile1-mode \
3000 * infile2 infile2-sha1 infile2-mode [ rename-to ]
3003 static void run_external_diff(const char *pgm
,
3006 struct diff_filespec
*one
,
3007 struct diff_filespec
*two
,
3008 const char *xfrm_msg
,
3009 int complete_rewrite
,
3010 struct diff_options
*o
)
3012 struct argv_array argv
= ARGV_ARRAY_INIT
;
3013 struct argv_array env
= ARGV_ARRAY_INIT
;
3014 struct diff_queue_struct
*q
= &diff_queued_diff
;
3016 argv_array_push(&argv
, pgm
);
3017 argv_array_push(&argv
, name
);
3020 add_external_diff_name(&argv
, name
, one
);
3022 add_external_diff_name(&argv
, name
, two
);
3024 add_external_diff_name(&argv
, other
, two
);
3025 argv_array_push(&argv
, other
);
3026 argv_array_push(&argv
, xfrm_msg
);
3030 argv_array_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
3031 argv_array_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
3033 if (run_command_v_opt_cd_env(argv
.argv
, RUN_USING_SHELL
, NULL
, env
.argv
))
3034 die(_("external diff died, stopping at %s"), name
);
3037 argv_array_clear(&argv
);
3038 argv_array_clear(&env
);
3041 static int similarity_index(struct diff_filepair
*p
)
3043 return p
->score
* 100 / MAX_SCORE
;
3046 static void fill_metainfo(struct strbuf
*msg
,
3049 struct diff_filespec
*one
,
3050 struct diff_filespec
*two
,
3051 struct diff_options
*o
,
3052 struct diff_filepair
*p
,
3053 int *must_show_header
,
3056 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
3057 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
3058 const char *line_prefix
= diff_line_prefix(o
);
3060 *must_show_header
= 1;
3061 strbuf_init(msg
, PATH_MAX
* 2 + 300);
3062 switch (p
->status
) {
3063 case DIFF_STATUS_COPIED
:
3064 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3065 line_prefix
, set
, similarity_index(p
));
3066 strbuf_addf(msg
, "%s\n%s%scopy from ",
3067 reset
, line_prefix
, set
);
3068 quote_c_style(name
, msg
, NULL
, 0);
3069 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
3070 quote_c_style(other
, msg
, NULL
, 0);
3071 strbuf_addf(msg
, "%s\n", reset
);
3073 case DIFF_STATUS_RENAMED
:
3074 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3075 line_prefix
, set
, similarity_index(p
));
3076 strbuf_addf(msg
, "%s\n%s%srename from ",
3077 reset
, line_prefix
, set
);
3078 quote_c_style(name
, msg
, NULL
, 0);
3079 strbuf_addf(msg
, "%s\n%s%srename to ",
3080 reset
, line_prefix
, set
);
3081 quote_c_style(other
, msg
, NULL
, 0);
3082 strbuf_addf(msg
, "%s\n", reset
);
3084 case DIFF_STATUS_MODIFIED
:
3086 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
3088 set
, similarity_index(p
), reset
);
3093 *must_show_header
= 0;
3095 if (one
&& two
&& oidcmp(&one
->oid
, &two
->oid
)) {
3096 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
3098 if (DIFF_OPT_TST(o
, BINARY
)) {
3100 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
3101 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
3104 strbuf_addf(msg
, "%s%sindex %s..", line_prefix
, set
,
3105 find_unique_abbrev(one
->oid
.hash
, abbrev
));
3106 strbuf_addstr(msg
, find_unique_abbrev(two
->oid
.hash
, abbrev
));
3107 if (one
->mode
== two
->mode
)
3108 strbuf_addf(msg
, " %06o", one
->mode
);
3109 strbuf_addf(msg
, "%s\n", reset
);
3113 static void run_diff_cmd(const char *pgm
,
3116 const char *attr_path
,
3117 struct diff_filespec
*one
,
3118 struct diff_filespec
*two
,
3120 struct diff_options
*o
,
3121 struct diff_filepair
*p
)
3123 const char *xfrm_msg
= NULL
;
3124 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
3125 int must_show_header
= 0;
3128 if (DIFF_OPT_TST(o
, ALLOW_EXTERNAL
)) {
3129 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
3130 if (drv
&& drv
->external
)
3131 pgm
= drv
->external
;
3136 * don't use colors when the header is intended for an
3137 * external diff driver
3139 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
3141 want_color(o
->use_color
) && !pgm
);
3142 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
3146 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
3147 complete_rewrite
, o
);
3151 builtin_diff(name
, other
? other
: name
,
3152 one
, two
, xfrm_msg
, must_show_header
,
3153 o
, complete_rewrite
);
3155 fprintf(o
->file
, "* Unmerged path %s\n", name
);
3158 static void diff_fill_sha1_info(struct diff_filespec
*one
)
3160 if (DIFF_FILE_VALID(one
)) {
3161 if (!one
->oid_valid
) {
3163 if (one
->is_stdin
) {
3167 if (lstat(one
->path
, &st
) < 0)
3168 die_errno("stat '%s'", one
->path
);
3169 if (index_path(one
->oid
.hash
, one
->path
, &st
, 0))
3170 die("cannot hash %s", one
->path
);
3177 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
3179 /* Strip the prefix but do not molest /dev/null and absolute paths */
3180 if (*namep
&& **namep
!= '/') {
3181 *namep
+= prefix_length
;
3185 if (*otherp
&& **otherp
!= '/') {
3186 *otherp
+= prefix_length
;
3187 if (**otherp
== '/')
3192 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
3194 const char *pgm
= external_diff();
3196 struct diff_filespec
*one
= p
->one
;
3197 struct diff_filespec
*two
= p
->two
;
3200 const char *attr_path
;
3202 name
= p
->one
->path
;
3203 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
3205 if (o
->prefix_length
)
3206 strip_prefix(o
->prefix_length
, &name
, &other
);
3208 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
3211 if (DIFF_PAIR_UNMERGED(p
)) {
3212 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
3213 NULL
, NULL
, NULL
, o
, p
);
3217 diff_fill_sha1_info(one
);
3218 diff_fill_sha1_info(two
);
3221 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
3222 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
3224 * a filepair that changes between file and symlink
3225 * needs to be split into deletion and creation.
3227 struct diff_filespec
*null
= alloc_filespec(two
->path
);
3228 run_diff_cmd(NULL
, name
, other
, attr_path
,
3229 one
, null
, &msg
, o
, p
);
3231 strbuf_release(&msg
);
3233 null
= alloc_filespec(one
->path
);
3234 run_diff_cmd(NULL
, name
, other
, attr_path
,
3235 null
, two
, &msg
, o
, p
);
3239 run_diff_cmd(pgm
, name
, other
, attr_path
,
3240 one
, two
, &msg
, o
, p
);
3242 strbuf_release(&msg
);
3245 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
3246 struct diffstat_t
*diffstat
)
3251 if (DIFF_PAIR_UNMERGED(p
)) {
3253 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, p
);
3257 name
= p
->one
->path
;
3258 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
3260 if (o
->prefix_length
)
3261 strip_prefix(o
->prefix_length
, &name
, &other
);
3263 diff_fill_sha1_info(p
->one
);
3264 diff_fill_sha1_info(p
->two
);
3266 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, p
);
3269 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
3273 const char *attr_path
;
3275 if (DIFF_PAIR_UNMERGED(p
)) {
3280 name
= p
->one
->path
;
3281 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
3282 attr_path
= other
? other
: name
;
3284 if (o
->prefix_length
)
3285 strip_prefix(o
->prefix_length
, &name
, &other
);
3287 diff_fill_sha1_info(p
->one
);
3288 diff_fill_sha1_info(p
->two
);
3290 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
3293 void diff_setup(struct diff_options
*options
)
3295 memcpy(options
, &default_diff_options
, sizeof(*options
));
3297 options
->file
= stdout
;
3299 options
->line_termination
= '\n';
3300 options
->break_opt
= -1;
3301 options
->rename_limit
= -1;
3302 options
->dirstat_permille
= diff_dirstat_permille_default
;
3303 options
->context
= diff_context_default
;
3304 options
->ws_error_highlight
= WSEH_NEW
;
3305 DIFF_OPT_SET(options
, RENAME_EMPTY
);
3307 /* pathchange left =NULL by default */
3308 options
->change
= diff_change
;
3309 options
->add_remove
= diff_addremove
;
3310 options
->use_color
= diff_use_color_default
;
3311 options
->detect_rename
= diff_detect_rename_default
;
3312 options
->xdl_opts
|= diff_algorithm
;
3313 if (diff_indent_heuristic
)
3314 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
3315 else if (diff_compaction_heuristic
)
3316 DIFF_XDL_SET(options
, COMPACTION_HEURISTIC
);
3318 options
->orderfile
= diff_order_file_cfg
;
3320 if (diff_no_prefix
) {
3321 options
->a_prefix
= options
->b_prefix
= "";
3322 } else if (!diff_mnemonic_prefix
) {
3323 options
->a_prefix
= "a/";
3324 options
->b_prefix
= "b/";
3328 void diff_setup_done(struct diff_options
*options
)
3332 if (options
->set_default
)
3333 options
->set_default(options
);
3335 if (options
->output_format
& DIFF_FORMAT_NAME
)
3337 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
3339 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
3341 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
3344 die("--name-only, --name-status, --check and -s are mutually exclusive");
3347 * Most of the time we can say "there are changes"
3348 * only by checking if there are changed paths, but
3349 * --ignore-whitespace* options force us to look
3353 if (DIFF_XDL_TST(options
, IGNORE_WHITESPACE
) ||
3354 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_CHANGE
) ||
3355 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_AT_EOL
))
3356 DIFF_OPT_SET(options
, DIFF_FROM_CONTENTS
);
3358 DIFF_OPT_CLR(options
, DIFF_FROM_CONTENTS
);
3360 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
3361 options
->detect_rename
= DIFF_DETECT_COPY
;
3363 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
3364 options
->prefix
= NULL
;
3365 if (options
->prefix
)
3366 options
->prefix_length
= strlen(options
->prefix
);
3368 options
->prefix_length
= 0;
3370 if (options
->output_format
& (DIFF_FORMAT_NAME
|
3371 DIFF_FORMAT_NAME_STATUS
|
3372 DIFF_FORMAT_CHECKDIFF
|
3373 DIFF_FORMAT_NO_OUTPUT
))
3374 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
3375 DIFF_FORMAT_NUMSTAT
|
3376 DIFF_FORMAT_DIFFSTAT
|
3377 DIFF_FORMAT_SHORTSTAT
|
3378 DIFF_FORMAT_DIRSTAT
|
3379 DIFF_FORMAT_SUMMARY
|
3383 * These cases always need recursive; we do not drop caller-supplied
3384 * recursive bits for other formats here.
3386 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
3387 DIFF_FORMAT_NUMSTAT
|
3388 DIFF_FORMAT_DIFFSTAT
|
3389 DIFF_FORMAT_SHORTSTAT
|
3390 DIFF_FORMAT_DIRSTAT
|
3391 DIFF_FORMAT_SUMMARY
|
3392 DIFF_FORMAT_CHECKDIFF
))
3393 DIFF_OPT_SET(options
, RECURSIVE
);
3395 * Also pickaxe would not work very well if you do not say recursive
3397 if (options
->pickaxe
)
3398 DIFF_OPT_SET(options
, RECURSIVE
);
3400 * When patches are generated, submodules diffed against the work tree
3401 * must be checked for dirtiness too so it can be shown in the output
3403 if (options
->output_format
& DIFF_FORMAT_PATCH
)
3404 DIFF_OPT_SET(options
, DIRTY_SUBMODULES
);
3406 if (options
->detect_rename
&& options
->rename_limit
< 0)
3407 options
->rename_limit
= diff_rename_limit_default
;
3408 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
3410 /* read-cache does not die even when it fails
3411 * so it is safe for us to do this here. Also
3412 * it does not smudge active_cache or active_nr
3413 * when it fails, so we do not have to worry about
3414 * cleaning it up ourselves either.
3418 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
3419 options
->abbrev
= 40; /* full */
3422 * It does not make sense to show the first hit we happened
3423 * to have found. It does not make sense not to return with
3424 * exit code in such a case either.
3426 if (DIFF_OPT_TST(options
, QUICK
)) {
3427 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
3428 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
3431 options
->diff_path_counter
= 0;
3433 if (DIFF_OPT_TST(options
, FOLLOW_RENAMES
) && options
->pathspec
.nr
!= 1)
3434 die(_("--follow requires exactly one pathspec"));
3437 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
3447 if (c
== arg_short
) {
3451 if (val
&& isdigit(c
)) {
3453 int n
= strtoul(arg
, &end
, 10);
3464 eq
= strchrnul(arg
, '=');
3466 if (!len
|| strncmp(arg
, arg_long
, len
))
3471 if (!isdigit(*++eq
))
3473 n
= strtoul(eq
, &end
, 10);
3481 static int diff_scoreopt_parse(const char *opt
);
3483 static inline int short_opt(char opt
, const char **argv
,
3484 const char **optarg
)
3486 const char *arg
= argv
[0];
3487 if (arg
[0] != '-' || arg
[1] != opt
)
3489 if (arg
[2] != '\0') {
3494 die("Option '%c' requires a value", opt
);
3499 int parse_long_opt(const char *opt
, const char **argv
,
3500 const char **optarg
)
3502 const char *arg
= argv
[0];
3503 if (!skip_prefix(arg
, "--", &arg
))
3505 if (!skip_prefix(arg
, opt
, &arg
))
3507 if (*arg
== '=') { /* stuck form: --option=value */
3513 /* separate form: --option value */
3515 die("Option '--%s' requires a value", opt
);
3520 static int stat_opt(struct diff_options
*options
, const char **av
)
3522 const char *arg
= av
[0];
3524 int width
= options
->stat_width
;
3525 int name_width
= options
->stat_name_width
;
3526 int graph_width
= options
->stat_graph_width
;
3527 int count
= options
->stat_count
;
3530 if (!skip_prefix(arg
, "--stat", &arg
))
3531 die("BUG: stat option does not begin with --stat: %s", arg
);
3536 if (skip_prefix(arg
, "-width", &arg
)) {
3538 width
= strtoul(arg
+ 1, &end
, 10);
3539 else if (!*arg
&& !av
[1])
3540 die("Option '--stat-width' requires a value");
3542 width
= strtoul(av
[1], &end
, 10);
3545 } else if (skip_prefix(arg
, "-name-width", &arg
)) {
3547 name_width
= strtoul(arg
+ 1, &end
, 10);
3548 else if (!*arg
&& !av
[1])
3549 die("Option '--stat-name-width' requires a value");
3551 name_width
= strtoul(av
[1], &end
, 10);
3554 } else if (skip_prefix(arg
, "-graph-width", &arg
)) {
3556 graph_width
= strtoul(arg
+ 1, &end
, 10);
3557 else if (!*arg
&& !av
[1])
3558 die("Option '--stat-graph-width' requires a value");
3560 graph_width
= strtoul(av
[1], &end
, 10);
3563 } else if (skip_prefix(arg
, "-count", &arg
)) {
3565 count
= strtoul(arg
+ 1, &end
, 10);
3566 else if (!*arg
&& !av
[1])
3567 die("Option '--stat-count' requires a value");
3569 count
= strtoul(av
[1], &end
, 10);
3575 width
= strtoul(arg
+1, &end
, 10);
3577 name_width
= strtoul(end
+1, &end
, 10);
3579 count
= strtoul(end
+1, &end
, 10);
3582 /* Important! This checks all the error cases! */
3585 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
3586 options
->stat_name_width
= name_width
;
3587 options
->stat_graph_width
= graph_width
;
3588 options
->stat_width
= width
;
3589 options
->stat_count
= count
;
3593 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
3595 struct strbuf errmsg
= STRBUF_INIT
;
3596 if (parse_dirstat_params(options
, params
, &errmsg
))
3597 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
3599 strbuf_release(&errmsg
);
3601 * The caller knows a dirstat-related option is given from the command
3602 * line; allow it to say "return this_function();"
3604 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
3608 static int parse_submodule_opt(struct diff_options
*options
, const char *value
)
3610 if (parse_submodule_params(options
, value
))
3611 die(_("Failed to parse --submodule option parameter: '%s'"),
3616 static const char diff_status_letters
[] = {
3619 DIFF_STATUS_DELETED
,
3620 DIFF_STATUS_MODIFIED
,
3621 DIFF_STATUS_RENAMED
,
3622 DIFF_STATUS_TYPE_CHANGED
,
3623 DIFF_STATUS_UNKNOWN
,
3624 DIFF_STATUS_UNMERGED
,
3625 DIFF_STATUS_FILTER_AON
,
3626 DIFF_STATUS_FILTER_BROKEN
,
3630 static unsigned int filter_bit
['Z' + 1];
3632 static void prepare_filter_bits(void)
3636 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
3637 for (i
= 0; diff_status_letters
[i
]; i
++)
3638 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
3642 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
3644 return opt
->filter
& filter_bit
[(int) status
];
3647 static int parse_diff_filter_opt(const char *optarg
, struct diff_options
*opt
)
3651 prepare_filter_bits();
3654 * If there is a negation e.g. 'd' in the input, and we haven't
3655 * initialized the filter field with another --diff-filter, start
3656 * from full set of bits, except for AON.
3659 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
3660 if (optch
< 'a' || 'z' < optch
)
3662 opt
->filter
= (1 << (ARRAY_SIZE(diff_status_letters
) - 1)) - 1;
3663 opt
->filter
&= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
3668 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
3672 if ('a' <= optch
&& optch
<= 'z') {
3674 optch
= toupper(optch
);
3679 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
3683 opt
->filter
&= ~bit
;
3690 static void enable_patch_output(int *fmt
) {
3691 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
3692 *fmt
|= DIFF_FORMAT_PATCH
;
3695 static int parse_one_token(const char **arg
, const char *token
)
3698 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
3705 static int parse_ws_error_highlight(struct diff_options
*opt
, const char *arg
)
3707 const char *orig_arg
= arg
;
3710 if (parse_one_token(&arg
, "none"))
3712 else if (parse_one_token(&arg
, "default"))
3714 else if (parse_one_token(&arg
, "all"))
3715 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
3716 else if (parse_one_token(&arg
, "new"))
3718 else if (parse_one_token(&arg
, "old"))
3720 else if (parse_one_token(&arg
, "context"))
3721 val
|= WSEH_CONTEXT
;
3723 error("unknown value after ws-error-highlight=%.*s",
3724 (int)(arg
- orig_arg
), orig_arg
);
3730 opt
->ws_error_highlight
= val
;
3734 int diff_opt_parse(struct diff_options
*options
,
3735 const char **av
, int ac
, const char *prefix
)
3737 const char *arg
= av
[0];
3744 /* Output format options */
3745 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u") || !strcmp(arg
, "--patch")
3746 || opt_arg(arg
, 'U', "unified", &options
->context
))
3747 enable_patch_output(&options
->output_format
);
3748 else if (!strcmp(arg
, "--raw"))
3749 options
->output_format
|= DIFF_FORMAT_RAW
;
3750 else if (!strcmp(arg
, "--patch-with-raw")) {
3751 enable_patch_output(&options
->output_format
);
3752 options
->output_format
|= DIFF_FORMAT_RAW
;
3753 } else if (!strcmp(arg
, "--numstat"))
3754 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
3755 else if (!strcmp(arg
, "--shortstat"))
3756 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
3757 else if (!strcmp(arg
, "-X") || !strcmp(arg
, "--dirstat"))
3758 return parse_dirstat_opt(options
, "");
3759 else if (skip_prefix(arg
, "-X", &arg
))
3760 return parse_dirstat_opt(options
, arg
);
3761 else if (skip_prefix(arg
, "--dirstat=", &arg
))
3762 return parse_dirstat_opt(options
, arg
);
3763 else if (!strcmp(arg
, "--cumulative"))
3764 return parse_dirstat_opt(options
, "cumulative");
3765 else if (!strcmp(arg
, "--dirstat-by-file"))
3766 return parse_dirstat_opt(options
, "files");
3767 else if (skip_prefix(arg
, "--dirstat-by-file=", &arg
)) {
3768 parse_dirstat_opt(options
, "files");
3769 return parse_dirstat_opt(options
, arg
);
3771 else if (!strcmp(arg
, "--check"))
3772 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
3773 else if (!strcmp(arg
, "--summary"))
3774 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
3775 else if (!strcmp(arg
, "--patch-with-stat")) {
3776 enable_patch_output(&options
->output_format
);
3777 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
3778 } else if (!strcmp(arg
, "--name-only"))
3779 options
->output_format
|= DIFF_FORMAT_NAME
;
3780 else if (!strcmp(arg
, "--name-status"))
3781 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
3782 else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--no-patch"))
3783 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
3784 else if (starts_with(arg
, "--stat"))
3785 /* --stat, --stat-width, --stat-name-width, or --stat-count */
3786 return stat_opt(options
, av
);
3788 /* renames options */
3789 else if (starts_with(arg
, "-B") || starts_with(arg
, "--break-rewrites=") ||
3790 !strcmp(arg
, "--break-rewrites")) {
3791 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
3792 return error("invalid argument to -B: %s", arg
+2);
3794 else if (starts_with(arg
, "-M") || starts_with(arg
, "--find-renames=") ||
3795 !strcmp(arg
, "--find-renames")) {
3796 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
3797 return error("invalid argument to -M: %s", arg
+2);
3798 options
->detect_rename
= DIFF_DETECT_RENAME
;
3800 else if (!strcmp(arg
, "-D") || !strcmp(arg
, "--irreversible-delete")) {
3801 options
->irreversible_delete
= 1;
3803 else if (starts_with(arg
, "-C") || starts_with(arg
, "--find-copies=") ||
3804 !strcmp(arg
, "--find-copies")) {
3805 if (options
->detect_rename
== DIFF_DETECT_COPY
)
3806 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
3807 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
3808 return error("invalid argument to -C: %s", arg
+2);
3809 options
->detect_rename
= DIFF_DETECT_COPY
;
3811 else if (!strcmp(arg
, "--no-renames"))
3812 options
->detect_rename
= 0;
3813 else if (!strcmp(arg
, "--rename-empty"))
3814 DIFF_OPT_SET(options
, RENAME_EMPTY
);
3815 else if (!strcmp(arg
, "--no-rename-empty"))
3816 DIFF_OPT_CLR(options
, RENAME_EMPTY
);
3817 else if (!strcmp(arg
, "--relative"))
3818 DIFF_OPT_SET(options
, RELATIVE_NAME
);
3819 else if (skip_prefix(arg
, "--relative=", &arg
)) {
3820 DIFF_OPT_SET(options
, RELATIVE_NAME
);
3821 options
->prefix
= arg
;
3825 else if (!strcmp(arg
, "--minimal"))
3826 DIFF_XDL_SET(options
, NEED_MINIMAL
);
3827 else if (!strcmp(arg
, "--no-minimal"))
3828 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
3829 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
3830 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
3831 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
3832 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
3833 else if (!strcmp(arg
, "--ignore-space-at-eol"))
3834 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
3835 else if (!strcmp(arg
, "--ignore-blank-lines"))
3836 DIFF_XDL_SET(options
, IGNORE_BLANK_LINES
);
3837 else if (!strcmp(arg
, "--indent-heuristic")) {
3838 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
3839 DIFF_XDL_CLR(options
, COMPACTION_HEURISTIC
);
3840 } else if (!strcmp(arg
, "--no-indent-heuristic"))
3841 DIFF_XDL_CLR(options
, INDENT_HEURISTIC
);
3842 else if (!strcmp(arg
, "--compaction-heuristic")) {
3843 DIFF_XDL_SET(options
, COMPACTION_HEURISTIC
);
3844 DIFF_XDL_CLR(options
, INDENT_HEURISTIC
);
3845 } else if (!strcmp(arg
, "--no-compaction-heuristic"))
3846 DIFF_XDL_CLR(options
, COMPACTION_HEURISTIC
);
3847 else if (!strcmp(arg
, "--patience"))
3848 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
3849 else if (!strcmp(arg
, "--histogram"))
3850 options
->xdl_opts
= DIFF_WITH_ALG(options
, HISTOGRAM_DIFF
);
3851 else if ((argcount
= parse_long_opt("diff-algorithm", av
, &optarg
))) {
3852 long value
= parse_algorithm_value(optarg
);
3854 return error("option diff-algorithm accepts \"myers\", "
3855 "\"minimal\", \"patience\" and \"histogram\"");
3856 /* clear out previous settings */
3857 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
3858 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3859 options
->xdl_opts
|= value
;
3864 else if (!strcmp(arg
, "--binary")) {
3865 enable_patch_output(&options
->output_format
);
3866 DIFF_OPT_SET(options
, BINARY
);
3868 else if (!strcmp(arg
, "--full-index"))
3869 DIFF_OPT_SET(options
, FULL_INDEX
);
3870 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
3871 DIFF_OPT_SET(options
, TEXT
);
3872 else if (!strcmp(arg
, "-R"))
3873 DIFF_OPT_SET(options
, REVERSE_DIFF
);
3874 else if (!strcmp(arg
, "--find-copies-harder"))
3875 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
3876 else if (!strcmp(arg
, "--follow"))
3877 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
3878 else if (!strcmp(arg
, "--no-follow")) {
3879 DIFF_OPT_CLR(options
, FOLLOW_RENAMES
);
3880 DIFF_OPT_CLR(options
, DEFAULT_FOLLOW_RENAMES
);
3881 } else if (!strcmp(arg
, "--color"))
3882 options
->use_color
= 1;
3883 else if (skip_prefix(arg
, "--color=", &arg
)) {
3884 int value
= git_config_colorbool(NULL
, arg
);
3886 return error("option `color' expects \"always\", \"auto\", or \"never\"");
3887 options
->use_color
= value
;
3889 else if (!strcmp(arg
, "--no-color"))
3890 options
->use_color
= 0;
3891 else if (!strcmp(arg
, "--color-words")) {
3892 options
->use_color
= 1;
3893 options
->word_diff
= DIFF_WORDS_COLOR
;
3895 else if (skip_prefix(arg
, "--color-words=", &arg
)) {
3896 options
->use_color
= 1;
3897 options
->word_diff
= DIFF_WORDS_COLOR
;
3898 options
->word_regex
= arg
;
3900 else if (!strcmp(arg
, "--word-diff")) {
3901 if (options
->word_diff
== DIFF_WORDS_NONE
)
3902 options
->word_diff
= DIFF_WORDS_PLAIN
;
3904 else if (skip_prefix(arg
, "--word-diff=", &arg
)) {
3905 if (!strcmp(arg
, "plain"))
3906 options
->word_diff
= DIFF_WORDS_PLAIN
;
3907 else if (!strcmp(arg
, "color")) {
3908 options
->use_color
= 1;
3909 options
->word_diff
= DIFF_WORDS_COLOR
;
3911 else if (!strcmp(arg
, "porcelain"))
3912 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
3913 else if (!strcmp(arg
, "none"))
3914 options
->word_diff
= DIFF_WORDS_NONE
;
3916 die("bad --word-diff argument: %s", arg
);
3918 else if ((argcount
= parse_long_opt("word-diff-regex", av
, &optarg
))) {
3919 if (options
->word_diff
== DIFF_WORDS_NONE
)
3920 options
->word_diff
= DIFF_WORDS_PLAIN
;
3921 options
->word_regex
= optarg
;
3924 else if (!strcmp(arg
, "--exit-code"))
3925 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
3926 else if (!strcmp(arg
, "--quiet"))
3927 DIFF_OPT_SET(options
, QUICK
);
3928 else if (!strcmp(arg
, "--ext-diff"))
3929 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
3930 else if (!strcmp(arg
, "--no-ext-diff"))
3931 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
3932 else if (!strcmp(arg
, "--textconv"))
3933 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
3934 else if (!strcmp(arg
, "--no-textconv"))
3935 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
3936 else if (!strcmp(arg
, "--ignore-submodules")) {
3937 DIFF_OPT_SET(options
, OVERRIDE_SUBMODULE_CONFIG
);
3938 handle_ignore_submodules_arg(options
, "all");
3939 } else if (skip_prefix(arg
, "--ignore-submodules=", &arg
)) {
3940 DIFF_OPT_SET(options
, OVERRIDE_SUBMODULE_CONFIG
);
3941 handle_ignore_submodules_arg(options
, arg
);
3942 } else if (!strcmp(arg
, "--submodule"))
3943 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
3944 else if (skip_prefix(arg
, "--submodule=", &arg
))
3945 return parse_submodule_opt(options
, arg
);
3946 else if (skip_prefix(arg
, "--ws-error-highlight=", &arg
))
3947 return parse_ws_error_highlight(options
, arg
);
3950 else if (!strcmp(arg
, "-z"))
3951 options
->line_termination
= 0;
3952 else if ((argcount
= short_opt('l', av
, &optarg
))) {
3953 options
->rename_limit
= strtoul(optarg
, NULL
, 10);
3956 else if ((argcount
= short_opt('S', av
, &optarg
))) {
3957 options
->pickaxe
= optarg
;
3958 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
3960 } else if ((argcount
= short_opt('G', av
, &optarg
))) {
3961 options
->pickaxe
= optarg
;
3962 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
3965 else if (!strcmp(arg
, "--pickaxe-all"))
3966 options
->pickaxe_opts
|= DIFF_PICKAXE_ALL
;
3967 else if (!strcmp(arg
, "--pickaxe-regex"))
3968 options
->pickaxe_opts
|= DIFF_PICKAXE_REGEX
;
3969 else if ((argcount
= short_opt('O', av
, &optarg
))) {
3970 const char *path
= prefix_filename(prefix
, strlen(prefix
), optarg
);
3971 options
->orderfile
= xstrdup(path
);
3974 else if ((argcount
= parse_long_opt("diff-filter", av
, &optarg
))) {
3975 int offending
= parse_diff_filter_opt(optarg
, options
);
3977 die("unknown change class '%c' in --diff-filter=%s",
3981 else if (!strcmp(arg
, "--abbrev"))
3982 options
->abbrev
= DEFAULT_ABBREV
;
3983 else if (skip_prefix(arg
, "--abbrev=", &arg
)) {
3984 options
->abbrev
= strtoul(arg
, NULL
, 10);
3985 if (options
->abbrev
< MINIMUM_ABBREV
)
3986 options
->abbrev
= MINIMUM_ABBREV
;
3987 else if (40 < options
->abbrev
)
3988 options
->abbrev
= 40;
3990 else if ((argcount
= parse_long_opt("src-prefix", av
, &optarg
))) {
3991 options
->a_prefix
= optarg
;
3994 else if ((argcount
= parse_long_opt("line-prefix", av
, &optarg
))) {
3995 options
->line_prefix
= optarg
;
3996 options
->line_prefix_length
= strlen(options
->line_prefix
);
3997 graph_setup_line_prefix(options
);
4000 else if ((argcount
= parse_long_opt("dst-prefix", av
, &optarg
))) {
4001 options
->b_prefix
= optarg
;
4004 else if (!strcmp(arg
, "--no-prefix"))
4005 options
->a_prefix
= options
->b_prefix
= "";
4006 else if (opt_arg(arg
, '\0', "inter-hunk-context",
4007 &options
->interhunkcontext
))
4009 else if (!strcmp(arg
, "-W"))
4010 DIFF_OPT_SET(options
, FUNCCONTEXT
);
4011 else if (!strcmp(arg
, "--function-context"))
4012 DIFF_OPT_SET(options
, FUNCCONTEXT
);
4013 else if (!strcmp(arg
, "--no-function-context"))
4014 DIFF_OPT_CLR(options
, FUNCCONTEXT
);
4015 else if ((argcount
= parse_long_opt("output", av
, &optarg
))) {
4016 const char *path
= prefix_filename(prefix
, strlen(prefix
), optarg
);
4017 options
->file
= fopen(path
, "w");
4019 die_errno("Could not open '%s'", path
);
4020 options
->close_file
= 1;
4021 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
4022 options
->use_color
= GIT_COLOR_NEVER
;
4029 int parse_rename_score(const char **cp_p
)
4031 unsigned long num
, scale
;
4033 const char *cp
= *cp_p
;
4040 if ( !dot
&& ch
== '.' ) {
4043 } else if ( ch
== '%' ) {
4044 scale
= dot
? scale
*100 : 100;
4045 cp
++; /* % is always at the end */
4047 } else if ( ch
>= '0' && ch
<= '9' ) {
4048 if ( scale
< 100000 ) {
4050 num
= (num
*10) + (ch
-'0');
4059 /* user says num divided by scale and we say internally that
4060 * is MAX_SCORE * num / scale.
4062 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
4065 static int diff_scoreopt_parse(const char *opt
)
4067 int opt1
, opt2
, cmd
;
4073 /* convert the long-form arguments into short-form versions */
4074 if (skip_prefix(opt
, "break-rewrites", &opt
)) {
4075 if (*opt
== 0 || *opt
++ == '=')
4077 } else if (skip_prefix(opt
, "find-copies", &opt
)) {
4078 if (*opt
== 0 || *opt
++ == '=')
4080 } else if (skip_prefix(opt
, "find-renames", &opt
)) {
4081 if (*opt
== 0 || *opt
++ == '=')
4085 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
4086 return -1; /* that is not a -M, -C, or -B option */
4088 opt1
= parse_rename_score(&opt
);
4094 else if (*opt
!= '/')
4095 return -1; /* we expect -B80/99 or -B80 */
4098 opt2
= parse_rename_score(&opt
);
4103 return opt1
| (opt2
<< 16);
4106 struct diff_queue_struct diff_queued_diff
;
4108 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
4110 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
4111 queue
->queue
[queue
->nr
++] = dp
;
4114 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
4115 struct diff_filespec
*one
,
4116 struct diff_filespec
*two
)
4118 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
4126 void diff_free_filepair(struct diff_filepair
*p
)
4128 free_filespec(p
->one
);
4129 free_filespec(p
->two
);
4133 /* This is different from find_unique_abbrev() in that
4134 * it stuffs the result with dots for alignment.
4136 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
4141 return sha1_to_hex(sha1
);
4143 abbrev
= find_unique_abbrev(sha1
, len
);
4144 abblen
= strlen(abbrev
);
4146 static char hex
[41];
4147 if (len
< abblen
&& abblen
<= len
+ 2)
4148 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
4150 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
4153 return sha1_to_hex(sha1
);
4156 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
4158 int line_termination
= opt
->line_termination
;
4159 int inter_name_termination
= line_termination
? '\t' : '\0';
4161 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
4162 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
4163 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
4164 diff_unique_abbrev(p
->one
->oid
.hash
, opt
->abbrev
));
4165 fprintf(opt
->file
, "%s ",
4166 diff_unique_abbrev(p
->two
->oid
.hash
, opt
->abbrev
));
4169 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
4170 inter_name_termination
);
4172 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
4175 if (p
->status
== DIFF_STATUS_COPIED
||
4176 p
->status
== DIFF_STATUS_RENAMED
) {
4177 const char *name_a
, *name_b
;
4178 name_a
= p
->one
->path
;
4179 name_b
= p
->two
->path
;
4180 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4181 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
4182 write_name_quoted(name_b
, opt
->file
, line_termination
);
4184 const char *name_a
, *name_b
;
4185 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
4187 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4188 write_name_quoted(name_a
, opt
->file
, line_termination
);
4192 int diff_unmodified_pair(struct diff_filepair
*p
)
4194 /* This function is written stricter than necessary to support
4195 * the currently implemented transformers, but the idea is to
4196 * let transformers to produce diff_filepairs any way they want,
4197 * and filter and clean them up here before producing the output.
4199 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
4201 if (DIFF_PAIR_UNMERGED(p
))
4202 return 0; /* unmerged is interesting */
4204 /* deletion, addition, mode or type change
4205 * and rename are all interesting.
4207 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
4208 DIFF_PAIR_MODE_CHANGED(p
) ||
4209 strcmp(one
->path
, two
->path
))
4212 /* both are valid and point at the same path. that is, we are
4213 * dealing with a change.
4215 if (one
->oid_valid
&& two
->oid_valid
&&
4216 !oidcmp(&one
->oid
, &two
->oid
) &&
4217 !one
->dirty_submodule
&& !two
->dirty_submodule
)
4218 return 1; /* no change */
4219 if (!one
->oid_valid
&& !two
->oid_valid
)
4220 return 1; /* both look at the same file on the filesystem. */
4224 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
4226 if (diff_unmodified_pair(p
))
4229 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4230 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4231 return; /* no tree diffs in patch format */
4236 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
4237 struct diffstat_t
*diffstat
)
4239 if (diff_unmodified_pair(p
))
4242 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4243 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4244 return; /* no useful stat for tree diffs */
4246 run_diffstat(p
, o
, diffstat
);
4249 static void diff_flush_checkdiff(struct diff_filepair
*p
,
4250 struct diff_options
*o
)
4252 if (diff_unmodified_pair(p
))
4255 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4256 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4257 return; /* nothing to check in tree diffs */
4259 run_checkdiff(p
, o
);
4262 int diff_queue_is_empty(void)
4264 struct diff_queue_struct
*q
= &diff_queued_diff
;
4266 for (i
= 0; i
< q
->nr
; i
++)
4267 if (!diff_unmodified_pair(q
->queue
[i
]))
4273 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
4275 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
4278 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
4280 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
4281 fprintf(stderr
, "queue[%d] %s size %lu\n",
4286 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
4288 diff_debug_filespec(p
->one
, i
, "one");
4289 diff_debug_filespec(p
->two
, i
, "two");
4290 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
4291 p
->score
, p
->status
? p
->status
: '?',
4292 p
->one
->rename_used
, p
->broken_pair
);
4295 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
4299 fprintf(stderr
, "%s\n", msg
);
4300 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
4301 for (i
= 0; i
< q
->nr
; i
++) {
4302 struct diff_filepair
*p
= q
->queue
[i
];
4303 diff_debug_filepair(p
, i
);
4308 static void diff_resolve_rename_copy(void)
4311 struct diff_filepair
*p
;
4312 struct diff_queue_struct
*q
= &diff_queued_diff
;
4314 diff_debug_queue("resolve-rename-copy", q
);
4316 for (i
= 0; i
< q
->nr
; i
++) {
4318 p
->status
= 0; /* undecided */
4319 if (DIFF_PAIR_UNMERGED(p
))
4320 p
->status
= DIFF_STATUS_UNMERGED
;
4321 else if (!DIFF_FILE_VALID(p
->one
))
4322 p
->status
= DIFF_STATUS_ADDED
;
4323 else if (!DIFF_FILE_VALID(p
->two
))
4324 p
->status
= DIFF_STATUS_DELETED
;
4325 else if (DIFF_PAIR_TYPE_CHANGED(p
))
4326 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
4328 /* from this point on, we are dealing with a pair
4329 * whose both sides are valid and of the same type, i.e.
4330 * either in-place edit or rename/copy edit.
4332 else if (DIFF_PAIR_RENAME(p
)) {
4334 * A rename might have re-connected a broken
4335 * pair up, causing the pathnames to be the
4336 * same again. If so, that's not a rename at
4337 * all, just a modification..
4339 * Otherwise, see if this source was used for
4340 * multiple renames, in which case we decrement
4341 * the count, and call it a copy.
4343 if (!strcmp(p
->one
->path
, p
->two
->path
))
4344 p
->status
= DIFF_STATUS_MODIFIED
;
4345 else if (--p
->one
->rename_used
> 0)
4346 p
->status
= DIFF_STATUS_COPIED
;
4348 p
->status
= DIFF_STATUS_RENAMED
;
4350 else if (oidcmp(&p
->one
->oid
, &p
->two
->oid
) ||
4351 p
->one
->mode
!= p
->two
->mode
||
4352 p
->one
->dirty_submodule
||
4353 p
->two
->dirty_submodule
||
4354 is_null_oid(&p
->one
->oid
))
4355 p
->status
= DIFF_STATUS_MODIFIED
;
4357 /* This is a "no-change" entry and should not
4358 * happen anymore, but prepare for broken callers.
4360 error("feeding unmodified %s to diffcore",
4362 p
->status
= DIFF_STATUS_UNKNOWN
;
4365 diff_debug_queue("resolve-rename-copy done", q
);
4368 static int check_pair_status(struct diff_filepair
*p
)
4370 switch (p
->status
) {
4371 case DIFF_STATUS_UNKNOWN
:
4374 die("internal error in diff-resolve-rename-copy");
4380 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
4382 int fmt
= opt
->output_format
;
4384 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
4385 diff_flush_checkdiff(p
, opt
);
4386 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
4387 diff_flush_raw(p
, opt
);
4388 else if (fmt
& DIFF_FORMAT_NAME
) {
4389 const char *name_a
, *name_b
;
4390 name_a
= p
->two
->path
;
4392 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4393 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
4397 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
4400 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
4402 fprintf(file
, " %s ", newdelete
);
4403 write_name_quoted(fs
->path
, file
, '\n');
4407 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
,
4408 const char *line_prefix
)
4410 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
4411 fprintf(file
, "%s mode change %06o => %06o%c", line_prefix
, p
->one
->mode
,
4412 p
->two
->mode
, show_name
? ' ' : '\n');
4414 write_name_quoted(p
->two
->path
, file
, '\n');
4419 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
,
4420 const char *line_prefix
)
4422 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
4424 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
4426 show_mode_change(file
, p
, 0, line_prefix
);
4429 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
4431 FILE *file
= opt
->file
;
4432 const char *line_prefix
= diff_line_prefix(opt
);
4435 case DIFF_STATUS_DELETED
:
4436 fputs(line_prefix
, file
);
4437 show_file_mode_name(file
, "delete", p
->one
);
4439 case DIFF_STATUS_ADDED
:
4440 fputs(line_prefix
, file
);
4441 show_file_mode_name(file
, "create", p
->two
);
4443 case DIFF_STATUS_COPIED
:
4444 fputs(line_prefix
, file
);
4445 show_rename_copy(file
, "copy", p
, line_prefix
);
4447 case DIFF_STATUS_RENAMED
:
4448 fputs(line_prefix
, file
);
4449 show_rename_copy(file
, "rename", p
, line_prefix
);
4453 fprintf(file
, "%s rewrite ", line_prefix
);
4454 write_name_quoted(p
->two
->path
, file
, ' ');
4455 fprintf(file
, "(%d%%)\n", similarity_index(p
));
4457 show_mode_change(file
, p
, !p
->score
, line_prefix
);
4467 static int remove_space(char *line
, int len
)
4473 for (i
= 0; i
< len
; i
++)
4474 if (!isspace((c
= line
[i
])))
4480 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
4482 struct patch_id_t
*data
= priv
;
4485 /* Ignore line numbers when computing the SHA1 of the patch */
4486 if (starts_with(line
, "@@ -"))
4489 new_len
= remove_space(line
, len
);
4491 git_SHA1_Update(data
->ctx
, line
, new_len
);
4492 data
->patchlen
+= new_len
;
4495 /* returns 0 upon success, and writes result into sha1 */
4496 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
, int diff_header_only
)
4498 struct diff_queue_struct
*q
= &diff_queued_diff
;
4501 struct patch_id_t data
;
4502 char buffer
[PATH_MAX
* 4 + 20];
4504 git_SHA1_Init(&ctx
);
4505 memset(&data
, 0, sizeof(struct patch_id_t
));
4508 for (i
= 0; i
< q
->nr
; i
++) {
4512 struct diff_filepair
*p
= q
->queue
[i
];
4515 memset(&xpp
, 0, sizeof(xpp
));
4516 memset(&xecfg
, 0, sizeof(xecfg
));
4518 return error("internal diff status error");
4519 if (p
->status
== DIFF_STATUS_UNKNOWN
)
4521 if (diff_unmodified_pair(p
))
4523 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4524 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4526 if (DIFF_PAIR_UNMERGED(p
))
4529 diff_fill_sha1_info(p
->one
);
4530 diff_fill_sha1_info(p
->two
);
4532 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
4533 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
4534 if (p
->one
->mode
== 0)
4535 len1
= snprintf(buffer
, sizeof(buffer
),
4536 "diff--gita/%.*sb/%.*s"
4543 len2
, p
->two
->path
);
4544 else if (p
->two
->mode
== 0)
4545 len1
= snprintf(buffer
, sizeof(buffer
),
4546 "diff--gita/%.*sb/%.*s"
4547 "deletedfilemode%06o"
4553 len1
, p
->one
->path
);
4555 len1
= snprintf(buffer
, sizeof(buffer
),
4556 "diff--gita/%.*sb/%.*s"
4562 len2
, p
->two
->path
);
4563 git_SHA1_Update(&ctx
, buffer
, len1
);
4565 if (diff_header_only
)
4568 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
4569 fill_mmfile(&mf2
, p
->two
) < 0)
4570 return error("unable to read files to diff");
4572 if (diff_filespec_is_binary(p
->one
) ||
4573 diff_filespec_is_binary(p
->two
)) {
4574 git_SHA1_Update(&ctx
, oid_to_hex(&p
->one
->oid
),
4576 git_SHA1_Update(&ctx
, oid_to_hex(&p
->two
->oid
),
4584 if (xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
4586 return error("unable to generate patch-id diff for %s",
4590 git_SHA1_Final(sha1
, &ctx
);
4594 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
, int diff_header_only
)
4596 struct diff_queue_struct
*q
= &diff_queued_diff
;
4598 int result
= diff_get_patch_id(options
, sha1
, diff_header_only
);
4600 for (i
= 0; i
< q
->nr
; i
++)
4601 diff_free_filepair(q
->queue
[i
]);
4604 DIFF_QUEUE_CLEAR(q
);
4609 static int is_summary_empty(const struct diff_queue_struct
*q
)
4613 for (i
= 0; i
< q
->nr
; i
++) {
4614 const struct diff_filepair
*p
= q
->queue
[i
];
4616 switch (p
->status
) {
4617 case DIFF_STATUS_DELETED
:
4618 case DIFF_STATUS_ADDED
:
4619 case DIFF_STATUS_COPIED
:
4620 case DIFF_STATUS_RENAMED
:
4625 if (p
->one
->mode
&& p
->two
->mode
&&
4626 p
->one
->mode
!= p
->two
->mode
)
4634 static const char rename_limit_warning
[] =
4635 "inexact rename detection was skipped due to too many files.";
4637 static const char degrade_cc_to_c_warning
[] =
4638 "only found copies from modified paths due to too many files.";
4640 static const char rename_limit_advice
[] =
4641 "you may want to set your %s variable to at least "
4642 "%d and retry the command.";
4644 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
4647 warning(degrade_cc_to_c_warning
);
4649 warning(rename_limit_warning
);
4652 if (0 < needed
&& needed
< 32767)
4653 warning(rename_limit_advice
, varname
, needed
);
4656 void diff_flush(struct diff_options
*options
)
4658 struct diff_queue_struct
*q
= &diff_queued_diff
;
4659 int i
, output_format
= options
->output_format
;
4661 int dirstat_by_line
= 0;
4664 * Order: raw, stat, summary, patch
4665 * or: name/name-status/checkdiff (other bits clear)
4670 if (output_format
& (DIFF_FORMAT_RAW
|
4672 DIFF_FORMAT_NAME_STATUS
|
4673 DIFF_FORMAT_CHECKDIFF
)) {
4674 for (i
= 0; i
< q
->nr
; i
++) {
4675 struct diff_filepair
*p
= q
->queue
[i
];
4676 if (check_pair_status(p
))
4677 flush_one_pair(p
, options
);
4682 if (output_format
& DIFF_FORMAT_DIRSTAT
&& DIFF_OPT_TST(options
, DIRSTAT_BY_LINE
))
4683 dirstat_by_line
= 1;
4685 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
4687 struct diffstat_t diffstat
;
4689 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
4690 for (i
= 0; i
< q
->nr
; i
++) {
4691 struct diff_filepair
*p
= q
->queue
[i
];
4692 if (check_pair_status(p
))
4693 diff_flush_stat(p
, options
, &diffstat
);
4695 if (output_format
& DIFF_FORMAT_NUMSTAT
)
4696 show_numstat(&diffstat
, options
);
4697 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
4698 show_stats(&diffstat
, options
);
4699 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
4700 show_shortstats(&diffstat
, options
);
4701 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
4702 show_dirstat_by_line(&diffstat
, options
);
4703 free_diffstat_info(&diffstat
);
4706 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
4707 show_dirstat(options
);
4709 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
4710 for (i
= 0; i
< q
->nr
; i
++) {
4711 diff_summary(options
, q
->queue
[i
]);
4716 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
4717 DIFF_OPT_TST(options
, EXIT_WITH_STATUS
) &&
4718 DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
4720 * run diff_flush_patch for the exit status. setting
4721 * options->file to /dev/null should be safe, because we
4722 * aren't supposed to produce any output anyway.
4724 if (options
->close_file
)
4725 fclose(options
->file
);
4726 options
->file
= fopen("/dev/null", "w");
4728 die_errno("Could not open /dev/null");
4729 options
->close_file
= 1;
4730 for (i
= 0; i
< q
->nr
; i
++) {
4731 struct diff_filepair
*p
= q
->queue
[i
];
4732 if (check_pair_status(p
))
4733 diff_flush_patch(p
, options
);
4734 if (options
->found_changes
)
4739 if (output_format
& DIFF_FORMAT_PATCH
) {
4741 fprintf(options
->file
, "%s%c",
4742 diff_line_prefix(options
),
4743 options
->line_termination
);
4744 if (options
->stat_sep
) {
4745 /* attach patch instead of inline */
4746 fputs(options
->stat_sep
, options
->file
);
4750 for (i
= 0; i
< q
->nr
; i
++) {
4751 struct diff_filepair
*p
= q
->queue
[i
];
4752 if (check_pair_status(p
))
4753 diff_flush_patch(p
, options
);
4757 if (output_format
& DIFF_FORMAT_CALLBACK
)
4758 options
->format_callback(q
, options
, options
->format_callback_data
);
4760 for (i
= 0; i
< q
->nr
; i
++)
4761 diff_free_filepair(q
->queue
[i
]);
4764 DIFF_QUEUE_CLEAR(q
);
4765 if (options
->close_file
)
4766 fclose(options
->file
);
4769 * Report the content-level differences with HAS_CHANGES;
4770 * diff_addremove/diff_change does not set the bit when
4771 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
4773 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
4774 if (options
->found_changes
)
4775 DIFF_OPT_SET(options
, HAS_CHANGES
);
4777 DIFF_OPT_CLR(options
, HAS_CHANGES
);
4781 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
4783 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
4785 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
4787 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
4788 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
4789 filter_bit_tst(p
->status
, options
)));
4792 static void diffcore_apply_filter(struct diff_options
*options
)
4795 struct diff_queue_struct
*q
= &diff_queued_diff
;
4796 struct diff_queue_struct outq
;
4798 DIFF_QUEUE_CLEAR(&outq
);
4800 if (!options
->filter
)
4803 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
4805 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
4806 if (match_filter(options
, q
->queue
[i
]))
4812 /* otherwise we will clear the whole queue
4813 * by copying the empty outq at the end of this
4814 * function, but first clear the current entries
4817 for (i
= 0; i
< q
->nr
; i
++)
4818 diff_free_filepair(q
->queue
[i
]);
4821 /* Only the matching ones */
4822 for (i
= 0; i
< q
->nr
; i
++) {
4823 struct diff_filepair
*p
= q
->queue
[i
];
4824 if (match_filter(options
, p
))
4827 diff_free_filepair(p
);
4834 /* Check whether two filespecs with the same mode and size are identical */
4835 static int diff_filespec_is_identical(struct diff_filespec
*one
,
4836 struct diff_filespec
*two
)
4838 if (S_ISGITLINK(one
->mode
))
4840 if (diff_populate_filespec(one
, 0))
4842 if (diff_populate_filespec(two
, 0))
4844 return !memcmp(one
->data
, two
->data
, one
->size
);
4847 static int diff_filespec_check_stat_unmatch(struct diff_filepair
*p
)
4849 if (p
->done_skip_stat_unmatch
)
4850 return p
->skip_stat_unmatch_result
;
4852 p
->done_skip_stat_unmatch
= 1;
4853 p
->skip_stat_unmatch_result
= 0;
4855 * 1. Entries that come from stat info dirtiness
4856 * always have both sides (iow, not create/delete),
4857 * one side of the object name is unknown, with
4858 * the same mode and size. Keep the ones that
4859 * do not match these criteria. They have real
4862 * 2. At this point, the file is known to be modified,
4863 * with the same mode and size, and the object
4864 * name of one side is unknown. Need to inspect
4865 * the identical contents.
4867 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
4868 !DIFF_FILE_VALID(p
->two
) ||
4869 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
4870 (p
->one
->mode
!= p
->two
->mode
) ||
4871 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
) ||
4872 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
) ||
4873 (p
->one
->size
!= p
->two
->size
) ||
4874 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
4875 p
->skip_stat_unmatch_result
= 1;
4876 return p
->skip_stat_unmatch_result
;
4879 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
4882 struct diff_queue_struct
*q
= &diff_queued_diff
;
4883 struct diff_queue_struct outq
;
4884 DIFF_QUEUE_CLEAR(&outq
);
4886 for (i
= 0; i
< q
->nr
; i
++) {
4887 struct diff_filepair
*p
= q
->queue
[i
];
4889 if (diff_filespec_check_stat_unmatch(p
))
4893 * The caller can subtract 1 from skip_stat_unmatch
4894 * to determine how many paths were dirty only
4895 * due to stat info mismatch.
4897 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
4898 diffopt
->skip_stat_unmatch
++;
4899 diff_free_filepair(p
);
4906 static int diffnamecmp(const void *a_
, const void *b_
)
4908 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
4909 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
4910 const char *name_a
, *name_b
;
4912 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
4913 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
4914 return strcmp(name_a
, name_b
);
4917 void diffcore_fix_diff_index(struct diff_options
*options
)
4919 struct diff_queue_struct
*q
= &diff_queued_diff
;
4920 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), diffnamecmp
);
4923 void diffcore_std(struct diff_options
*options
)
4925 /* NOTE please keep the following in sync with diff_tree_combined() */
4926 if (options
->skip_stat_unmatch
)
4927 diffcore_skip_stat_unmatch(options
);
4928 if (!options
->found_follow
) {
4929 /* See try_to_follow_renames() in tree-diff.c */
4930 if (options
->break_opt
!= -1)
4931 diffcore_break(options
->break_opt
);
4932 if (options
->detect_rename
)
4933 diffcore_rename(options
);
4934 if (options
->break_opt
!= -1)
4935 diffcore_merge_broken();
4937 if (options
->pickaxe
)
4938 diffcore_pickaxe(options
);
4939 if (options
->orderfile
)
4940 diffcore_order(options
->orderfile
);
4941 if (!options
->found_follow
)
4942 /* See try_to_follow_renames() in tree-diff.c */
4943 diff_resolve_rename_copy();
4944 diffcore_apply_filter(options
);
4946 if (diff_queued_diff
.nr
&& !DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
4947 DIFF_OPT_SET(options
, HAS_CHANGES
);
4949 DIFF_OPT_CLR(options
, HAS_CHANGES
);
4951 options
->found_follow
= 0;
4954 int diff_result_code(struct diff_options
*opt
, int status
)
4958 diff_warn_rename_limit("diff.renameLimit",
4959 opt
->needed_rename_limit
,
4960 opt
->degraded_cc_to_c
);
4961 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
4962 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
4964 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
4965 DIFF_OPT_TST(opt
, HAS_CHANGES
))
4967 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
4968 DIFF_OPT_TST(opt
, CHECK_FAILED
))
4973 int diff_can_quit_early(struct diff_options
*opt
)
4975 return (DIFF_OPT_TST(opt
, QUICK
) &&
4977 DIFF_OPT_TST(opt
, HAS_CHANGES
));
4981 * Shall changes to this submodule be ignored?
4983 * Submodule changes can be configured to be ignored separately for each path,
4984 * but that configuration can be overridden from the command line.
4986 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
4989 unsigned orig_flags
= options
->flags
;
4990 if (!DIFF_OPT_TST(options
, OVERRIDE_SUBMODULE_CONFIG
))
4991 set_diffopt_flags_from_submodule_config(options
, path
);
4992 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
))
4994 options
->flags
= orig_flags
;
4998 void diff_addremove(struct diff_options
*options
,
4999 int addremove
, unsigned mode
,
5000 const unsigned char *sha1
,
5002 const char *concatpath
, unsigned dirty_submodule
)
5004 struct diff_filespec
*one
, *two
;
5006 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
5009 /* This may look odd, but it is a preparation for
5010 * feeding "there are unchanged files which should
5011 * not produce diffs, but when you are doing copy
5012 * detection you would need them, so here they are"
5013 * entries to the diff-core. They will be prefixed
5014 * with something like '=' or '*' (I haven't decided
5015 * which but should not make any difference).
5016 * Feeding the same new and old to diff_change()
5017 * also has the same effect.
5018 * Before the final output happens, they are pruned after
5019 * merged into rename/copy pairs as appropriate.
5021 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
5022 addremove
= (addremove
== '+' ? '-' :
5023 addremove
== '-' ? '+' : addremove
);
5025 if (options
->prefix
&&
5026 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5029 one
= alloc_filespec(concatpath
);
5030 two
= alloc_filespec(concatpath
);
5032 if (addremove
!= '+')
5033 fill_filespec(one
, sha1
, sha1_valid
, mode
);
5034 if (addremove
!= '-') {
5035 fill_filespec(two
, sha1
, sha1_valid
, mode
);
5036 two
->dirty_submodule
= dirty_submodule
;
5039 diff_queue(&diff_queued_diff
, one
, two
);
5040 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5041 DIFF_OPT_SET(options
, HAS_CHANGES
);
5044 void diff_change(struct diff_options
*options
,
5045 unsigned old_mode
, unsigned new_mode
,
5046 const unsigned char *old_sha1
,
5047 const unsigned char *new_sha1
,
5048 int old_sha1_valid
, int new_sha1_valid
,
5049 const char *concatpath
,
5050 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
5052 struct diff_filespec
*one
, *two
;
5053 struct diff_filepair
*p
;
5055 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
5056 is_submodule_ignored(concatpath
, options
))
5059 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
5061 const unsigned char *tmp_c
;
5062 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
5063 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
5064 tmp
= old_sha1_valid
; old_sha1_valid
= new_sha1_valid
;
5065 new_sha1_valid
= tmp
;
5066 tmp
= old_dirty_submodule
; old_dirty_submodule
= new_dirty_submodule
;
5067 new_dirty_submodule
= tmp
;
5070 if (options
->prefix
&&
5071 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5074 one
= alloc_filespec(concatpath
);
5075 two
= alloc_filespec(concatpath
);
5076 fill_filespec(one
, old_sha1
, old_sha1_valid
, old_mode
);
5077 fill_filespec(two
, new_sha1
, new_sha1_valid
, new_mode
);
5078 one
->dirty_submodule
= old_dirty_submodule
;
5079 two
->dirty_submodule
= new_dirty_submodule
;
5080 p
= diff_queue(&diff_queued_diff
, one
, two
);
5082 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5085 if (DIFF_OPT_TST(options
, QUICK
) && options
->skip_stat_unmatch
&&
5086 !diff_filespec_check_stat_unmatch(p
))
5089 DIFF_OPT_SET(options
, HAS_CHANGES
);
5092 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
5094 struct diff_filepair
*pair
;
5095 struct diff_filespec
*one
, *two
;
5097 if (options
->prefix
&&
5098 strncmp(path
, options
->prefix
, options
->prefix_length
))
5101 one
= alloc_filespec(path
);
5102 two
= alloc_filespec(path
);
5103 pair
= diff_queue(&diff_queued_diff
, one
, two
);
5104 pair
->is_unmerged
= 1;
5108 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
5111 struct diff_tempfile
*temp
;
5112 const char *argv
[3];
5113 const char **arg
= argv
;
5114 struct child_process child
= CHILD_PROCESS_INIT
;
5115 struct strbuf buf
= STRBUF_INIT
;
5118 temp
= prepare_temp_file(spec
->path
, spec
);
5120 *arg
++ = temp
->name
;
5123 child
.use_shell
= 1;
5126 if (start_command(&child
)) {
5131 if (strbuf_read(&buf
, child
.out
, 0) < 0)
5132 err
= error("error reading from textconv command '%s'", pgm
);
5135 if (finish_command(&child
) || err
) {
5136 strbuf_release(&buf
);
5142 return strbuf_detach(&buf
, outsize
);
5145 size_t fill_textconv(struct userdiff_driver
*driver
,
5146 struct diff_filespec
*df
,
5152 if (!DIFF_FILE_VALID(df
)) {
5156 if (diff_populate_filespec(df
, 0))
5157 die("unable to read files to diff");
5162 if (!driver
->textconv
)
5163 die("BUG: fill_textconv called with non-textconv driver");
5165 if (driver
->textconv_cache
&& df
->oid_valid
) {
5166 *outbuf
= notes_cache_get(driver
->textconv_cache
,
5173 *outbuf
= run_textconv(driver
->textconv
, df
, &size
);
5175 die("unable to read files to diff");
5177 if (driver
->textconv_cache
&& df
->oid_valid
) {
5178 /* ignore errors, as we might be in a readonly repository */
5179 notes_cache_put(driver
->textconv_cache
, df
->oid
.hash
, *outbuf
,
5182 * we could save up changes and flush them all at the end,
5183 * but we would need an extra call after all diffing is done.
5184 * Since generating a cache entry is the slow path anyway,
5185 * this extra overhead probably isn't a big deal.
5187 notes_cache_write(driver
->textconv_cache
);
5193 void setup_diff_pager(struct diff_options
*opt
)
5196 * If the user asked for our exit code, then either they want --quiet
5197 * or --exit-code. We should definitely not bother with a pager in the
5198 * former case, as we will generate no output. Since we still properly
5199 * report our exit code even when a pager is run, we _could_ run a
5200 * pager with --exit-code. But since we have not done so historically,
5201 * and because it is easy to find people oneline advising "git diff
5202 * --exit-code" in hooks and other scripts, we do not do so.
5204 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
5205 check_pager_config("diff") != 0)