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_rename_limit_default
= 400;
32 static int diff_suppress_blank_empty
;
33 static int diff_use_color_default
= -1;
34 static int diff_context_default
= 3;
35 static const char *diff_word_regex_cfg
;
36 static const char *external_diff_cmd_cfg
;
37 static const char *diff_order_file_cfg
;
38 int diff_auto_refresh_index
= 1;
39 static int diff_mnemonic_prefix
;
40 static int diff_no_prefix
;
41 static int diff_stat_graph_width
;
42 static int diff_dirstat_permille_default
= 30;
43 static struct diff_options default_diff_options
;
44 static long diff_algorithm
;
45 static unsigned ws_error_highlight_default
= WSEH_NEW
;
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 NORETURN
void die_want_option(const char *option_name
)
61 die(_("option '%s' requires a value"), option_name
);
64 static int parse_diff_color_slot(const char *var
)
66 if (!strcasecmp(var
, "context") || !strcasecmp(var
, "plain"))
68 if (!strcasecmp(var
, "meta"))
70 if (!strcasecmp(var
, "frag"))
72 if (!strcasecmp(var
, "old"))
74 if (!strcasecmp(var
, "new"))
76 if (!strcasecmp(var
, "commit"))
78 if (!strcasecmp(var
, "whitespace"))
79 return DIFF_WHITESPACE
;
80 if (!strcasecmp(var
, "func"))
85 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
86 struct strbuf
*errmsg
)
88 char *params_copy
= xstrdup(params_string
);
89 struct string_list params
= STRING_LIST_INIT_NODUP
;
94 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
95 for (i
= 0; i
< params
.nr
; i
++) {
96 const char *p
= params
.items
[i
].string
;
97 if (!strcmp(p
, "changes")) {
98 DIFF_OPT_CLR(options
, DIRSTAT_BY_LINE
);
99 DIFF_OPT_CLR(options
, DIRSTAT_BY_FILE
);
100 } else if (!strcmp(p
, "lines")) {
101 DIFF_OPT_SET(options
, DIRSTAT_BY_LINE
);
102 DIFF_OPT_CLR(options
, DIRSTAT_BY_FILE
);
103 } else if (!strcmp(p
, "files")) {
104 DIFF_OPT_CLR(options
, DIRSTAT_BY_LINE
);
105 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
106 } else if (!strcmp(p
, "noncumulative")) {
107 DIFF_OPT_CLR(options
, DIRSTAT_CUMULATIVE
);
108 } else if (!strcmp(p
, "cumulative")) {
109 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
110 } else if (isdigit(*p
)) {
112 int permille
= strtoul(p
, &end
, 10) * 10;
113 if (*end
== '.' && isdigit(*++end
)) {
114 /* only use first digit */
115 permille
+= *end
- '0';
116 /* .. and ignore any further digits */
117 while (isdigit(*++end
))
121 options
->dirstat_permille
= permille
;
123 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
128 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
133 string_list_clear(¶ms
, 0);
138 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
140 if (!strcmp(value
, "log"))
141 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
142 else if (!strcmp(value
, "short"))
143 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
144 else if (!strcmp(value
, "diff"))
145 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
151 static int git_config_rename(const char *var
, const char *value
)
154 return DIFF_DETECT_RENAME
;
155 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
156 return DIFF_DETECT_COPY
;
157 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
160 long parse_algorithm_value(const char *value
)
164 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
166 else if (!strcasecmp(value
, "minimal"))
167 return XDF_NEED_MINIMAL
;
168 else if (!strcasecmp(value
, "patience"))
169 return XDF_PATIENCE_DIFF
;
170 else if (!strcasecmp(value
, "histogram"))
171 return XDF_HISTOGRAM_DIFF
;
175 static int parse_one_token(const char **arg
, const char *token
)
178 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
185 static int parse_ws_error_highlight(const char *arg
)
187 const char *orig_arg
= arg
;
191 if (parse_one_token(&arg
, "none"))
193 else if (parse_one_token(&arg
, "default"))
195 else if (parse_one_token(&arg
, "all"))
196 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
197 else if (parse_one_token(&arg
, "new"))
199 else if (parse_one_token(&arg
, "old"))
201 else if (parse_one_token(&arg
, "context"))
204 return -1 - (int)(arg
- orig_arg
);
213 * These are to give UI layer defaults.
214 * The core-level commands such as git-diff-files should
215 * never be affected by the setting of diff.renames
216 * the user happens to have in the configuration file.
218 void init_diff_ui_defaults(void)
220 diff_detect_rename_default
= 1;
223 int git_diff_heuristic_config(const char *var
, const char *value
, void *cb
)
225 if (!strcmp(var
, "diff.indentheuristic"))
226 diff_indent_heuristic
= git_config_bool(var
, value
);
230 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
232 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
233 diff_use_color_default
= git_config_colorbool(var
, value
);
236 if (!strcmp(var
, "diff.context")) {
237 diff_context_default
= git_config_int(var
, value
);
238 if (diff_context_default
< 0)
242 if (!strcmp(var
, "diff.renames")) {
243 diff_detect_rename_default
= git_config_rename(var
, value
);
246 if (!strcmp(var
, "diff.autorefreshindex")) {
247 diff_auto_refresh_index
= git_config_bool(var
, value
);
250 if (!strcmp(var
, "diff.mnemonicprefix")) {
251 diff_mnemonic_prefix
= git_config_bool(var
, value
);
254 if (!strcmp(var
, "diff.noprefix")) {
255 diff_no_prefix
= git_config_bool(var
, value
);
258 if (!strcmp(var
, "diff.statgraphwidth")) {
259 diff_stat_graph_width
= git_config_int(var
, value
);
262 if (!strcmp(var
, "diff.external"))
263 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
264 if (!strcmp(var
, "diff.wordregex"))
265 return git_config_string(&diff_word_regex_cfg
, var
, value
);
266 if (!strcmp(var
, "diff.orderfile"))
267 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
269 if (!strcmp(var
, "diff.ignoresubmodules"))
270 handle_ignore_submodules_arg(&default_diff_options
, value
);
272 if (!strcmp(var
, "diff.submodule")) {
273 if (parse_submodule_params(&default_diff_options
, value
))
274 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
279 if (!strcmp(var
, "diff.algorithm")) {
280 diff_algorithm
= parse_algorithm_value(value
);
281 if (diff_algorithm
< 0)
286 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
289 if (!strcmp(var
, "diff.wserrorhighlight")) {
290 int val
= parse_ws_error_highlight(value
);
293 ws_error_highlight_default
= val
;
297 if (git_color_config(var
, value
, cb
) < 0)
300 return git_diff_basic_config(var
, value
, cb
);
303 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
307 if (!strcmp(var
, "diff.renamelimit")) {
308 diff_rename_limit_default
= git_config_int(var
, value
);
312 if (userdiff_config(var
, value
) < 0)
315 if (skip_prefix(var
, "diff.color.", &name
) ||
316 skip_prefix(var
, "color.diff.", &name
)) {
317 int slot
= parse_diff_color_slot(name
);
321 return config_error_nonbool(var
);
322 return color_parse(value
, diff_colors
[slot
]);
325 /* like GNU diff's --suppress-blank-empty option */
326 if (!strcmp(var
, "diff.suppressblankempty") ||
327 /* for backwards compatibility */
328 !strcmp(var
, "diff.suppress-blank-empty")) {
329 diff_suppress_blank_empty
= git_config_bool(var
, value
);
333 if (!strcmp(var
, "diff.dirstat")) {
334 struct strbuf errmsg
= STRBUF_INIT
;
335 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
336 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
337 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
339 strbuf_release(&errmsg
);
340 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
344 if (starts_with(var
, "submodule."))
345 return parse_submodule_config_option(var
, value
);
347 return git_default_config(var
, value
, cb
);
350 static char *quote_two(const char *one
, const char *two
)
352 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
353 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
354 struct strbuf res
= STRBUF_INIT
;
356 if (need_one
+ need_two
) {
357 strbuf_addch(&res
, '"');
358 quote_c_style(one
, &res
, NULL
, 1);
359 quote_c_style(two
, &res
, NULL
, 1);
360 strbuf_addch(&res
, '"');
362 strbuf_addstr(&res
, one
);
363 strbuf_addstr(&res
, two
);
365 return strbuf_detach(&res
, NULL
);
368 static const char *external_diff(void)
370 static const char *external_diff_cmd
= NULL
;
371 static int done_preparing
= 0;
374 return external_diff_cmd
;
375 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
376 if (!external_diff_cmd
)
377 external_diff_cmd
= external_diff_cmd_cfg
;
379 return external_diff_cmd
;
383 * Keep track of files used for diffing. Sometimes such an entry
384 * refers to a temporary file, sometimes to an existing file, and
385 * sometimes to "/dev/null".
387 static struct diff_tempfile
{
389 * filename external diff should read from, or NULL if this
390 * entry is currently not in use:
394 char hex
[GIT_SHA1_HEXSZ
+ 1];
398 * If this diff_tempfile instance refers to a temporary file,
399 * this tempfile object is used to manage its lifetime.
401 struct tempfile tempfile
;
404 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
406 struct emit_callback
{
409 int blank_at_eof_in_preimage
;
410 int blank_at_eof_in_postimage
;
412 int lno_in_postimage
;
413 sane_truncate_fn truncate
;
414 const char **label_path
;
415 struct diff_words_data
*diff_words
;
416 struct diff_options
*opt
;
417 struct strbuf
*header
;
420 static int count_lines(const char *data
, int size
)
422 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
429 completely_empty
= 0;
433 completely_empty
= 0;
436 if (completely_empty
)
439 count
++; /* no trailing newline */
443 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
445 if (!DIFF_FILE_VALID(one
)) {
446 mf
->ptr
= (char *)""; /* does not matter */
450 else if (diff_populate_filespec(one
, 0))
454 mf
->size
= one
->size
;
458 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
459 static unsigned long diff_filespec_size(struct diff_filespec
*one
)
461 if (!DIFF_FILE_VALID(one
))
463 diff_populate_filespec(one
, CHECK_SIZE_ONLY
);
467 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
470 long size
= mf
->size
;
475 ptr
+= size
- 1; /* pointing at the very end */
477 ; /* incomplete line */
479 ptr
--; /* skip the last LF */
480 while (mf
->ptr
< ptr
) {
482 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
483 if (*prev_eol
== '\n')
485 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
493 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
494 struct emit_callback
*ecbdata
)
497 unsigned ws_rule
= ecbdata
->ws_rule
;
498 l1
= count_trailing_blank(mf1
, ws_rule
);
499 l2
= count_trailing_blank(mf2
, ws_rule
);
501 ecbdata
->blank_at_eof_in_preimage
= 0;
502 ecbdata
->blank_at_eof_in_postimage
= 0;
505 at
= count_lines(mf1
->ptr
, mf1
->size
);
506 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
508 at
= count_lines(mf2
->ptr
, mf2
->size
);
509 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
512 static void emit_line_0(struct diff_options
*o
, const char *set
, const char *reset
,
513 int first
, const char *line
, int len
)
515 int has_trailing_newline
, has_trailing_carriage_return
;
517 FILE *file
= o
->file
;
519 fputs(diff_line_prefix(o
), file
);
522 has_trailing_newline
= (first
== '\n');
523 has_trailing_carriage_return
= (!has_trailing_newline
&&
525 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
527 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
528 if (has_trailing_newline
)
530 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
531 if (has_trailing_carriage_return
)
536 if (len
|| !nofirst
) {
540 fwrite(line
, len
, 1, file
);
543 if (has_trailing_carriage_return
)
545 if (has_trailing_newline
)
549 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
550 const char *line
, int len
)
552 emit_line_0(o
, set
, reset
, line
[0], line
+1, len
-1);
555 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
557 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
558 ecbdata
->blank_at_eof_in_preimage
&&
559 ecbdata
->blank_at_eof_in_postimage
&&
560 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
561 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
563 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
566 static void emit_line_checked(const char *reset
,
567 struct emit_callback
*ecbdata
,
568 const char *line
, int len
,
569 enum color_diff color
,
570 unsigned ws_error_highlight
,
573 const char *set
= diff_get_color(ecbdata
->color_diff
, color
);
574 const char *ws
= NULL
;
576 if (ecbdata
->opt
->ws_error_highlight
& ws_error_highlight
) {
577 ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
583 emit_line_0(ecbdata
->opt
, set
, reset
, sign
, line
, len
);
584 else if (sign
== '+' && new_blank_line_at_eof(ecbdata
, line
, len
))
585 /* Blank line at EOF - paint '+' as well */
586 emit_line_0(ecbdata
->opt
, ws
, reset
, sign
, line
, len
);
588 /* Emit just the prefix, then the rest. */
589 emit_line_0(ecbdata
->opt
, set
, reset
, sign
, "", 0);
590 ws_check_emit(line
, len
, ecbdata
->ws_rule
,
591 ecbdata
->opt
->file
, set
, reset
, ws
);
595 static void emit_add_line(const char *reset
,
596 struct emit_callback
*ecbdata
,
597 const char *line
, int len
)
599 emit_line_checked(reset
, ecbdata
, line
, len
,
600 DIFF_FILE_NEW
, WSEH_NEW
, '+');
603 static void emit_del_line(const char *reset
,
604 struct emit_callback
*ecbdata
,
605 const char *line
, int len
)
607 emit_line_checked(reset
, ecbdata
, line
, len
,
608 DIFF_FILE_OLD
, WSEH_OLD
, '-');
611 static void emit_context_line(const char *reset
,
612 struct emit_callback
*ecbdata
,
613 const char *line
, int len
)
615 emit_line_checked(reset
, ecbdata
, line
, len
,
616 DIFF_CONTEXT
, WSEH_CONTEXT
, ' ');
619 static void emit_hunk_header(struct emit_callback
*ecbdata
,
620 const char *line
, int len
)
622 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
623 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
624 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
625 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
626 static const char atat
[2] = { '@', '@' };
628 struct strbuf msgbuf
= STRBUF_INIT
;
633 * As a hunk header must begin with "@@ -<old>, +<new> @@",
634 * it always is at least 10 bytes long.
637 memcmp(line
, atat
, 2) ||
638 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
639 emit_line(ecbdata
->opt
, context
, reset
, line
, len
);
642 ep
+= 2; /* skip over @@ */
644 /* The hunk header in fraginfo color */
645 strbuf_addstr(&msgbuf
, frag
);
646 strbuf_add(&msgbuf
, line
, ep
- line
);
647 strbuf_addstr(&msgbuf
, reset
);
653 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
656 /* blank before the func header */
657 for (cp
= ep
; ep
- line
< len
; ep
++)
658 if (*ep
!= ' ' && *ep
!= '\t')
661 strbuf_addstr(&msgbuf
, context
);
662 strbuf_add(&msgbuf
, cp
, ep
- cp
);
663 strbuf_addstr(&msgbuf
, reset
);
666 if (ep
< line
+ len
) {
667 strbuf_addstr(&msgbuf
, func
);
668 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
669 strbuf_addstr(&msgbuf
, reset
);
672 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
673 emit_line(ecbdata
->opt
, "", "", msgbuf
.buf
, msgbuf
.len
);
674 strbuf_release(&msgbuf
);
677 static struct diff_tempfile
*claim_diff_tempfile(void) {
679 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
680 if (!diff_temp
[i
].name
)
681 return diff_temp
+ i
;
682 die("BUG: diff is failing to clean up its tempfiles");
685 static void remove_tempfile(void)
688 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
689 if (is_tempfile_active(&diff_temp
[i
].tempfile
))
690 delete_tempfile(&diff_temp
[i
].tempfile
);
691 diff_temp
[i
].name
= NULL
;
695 static void print_line_count(FILE *file
, int count
)
699 fprintf(file
, "0,0");
705 fprintf(file
, "1,%d", count
);
710 static void emit_rewrite_lines(struct emit_callback
*ecb
,
711 int prefix
, const char *data
, int size
)
713 const char *endp
= NULL
;
714 static const char *nneof
= " No newline at end of file\n";
715 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
720 endp
= memchr(data
, '\n', size
);
721 len
= endp
? (endp
- data
+ 1) : size
;
723 ecb
->lno_in_preimage
++;
724 emit_del_line(reset
, ecb
, data
, len
);
726 ecb
->lno_in_postimage
++;
727 emit_add_line(reset
, ecb
, data
, len
);
733 const char *context
= diff_get_color(ecb
->color_diff
,
735 putc('\n', ecb
->opt
->file
);
736 emit_line_0(ecb
->opt
, context
, reset
, '\\',
737 nneof
, strlen(nneof
));
741 static void emit_rewrite_diff(const char *name_a
,
743 struct diff_filespec
*one
,
744 struct diff_filespec
*two
,
745 struct userdiff_driver
*textconv_one
,
746 struct userdiff_driver
*textconv_two
,
747 struct diff_options
*o
)
750 const char *name_a_tab
, *name_b_tab
;
751 const char *metainfo
= diff_get_color(o
->use_color
, DIFF_METAINFO
);
752 const char *fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
753 const char *reset
= diff_get_color(o
->use_color
, DIFF_RESET
);
754 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
755 const char *a_prefix
, *b_prefix
;
756 char *data_one
, *data_two
;
757 size_t size_one
, size_two
;
758 struct emit_callback ecbdata
;
759 const char *line_prefix
= diff_line_prefix(o
);
761 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
762 a_prefix
= o
->b_prefix
;
763 b_prefix
= o
->a_prefix
;
765 a_prefix
= o
->a_prefix
;
766 b_prefix
= o
->b_prefix
;
769 name_a
+= (*name_a
== '/');
770 name_b
+= (*name_b
== '/');
771 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
772 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
774 strbuf_reset(&a_name
);
775 strbuf_reset(&b_name
);
776 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
777 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
779 size_one
= fill_textconv(textconv_one
, one
, &data_one
);
780 size_two
= fill_textconv(textconv_two
, two
, &data_two
);
782 memset(&ecbdata
, 0, sizeof(ecbdata
));
783 ecbdata
.color_diff
= want_color(o
->use_color
);
784 ecbdata
.ws_rule
= whitespace_rule(name_b
);
786 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
788 mf1
.ptr
= (char *)data_one
;
789 mf2
.ptr
= (char *)data_two
;
792 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
794 ecbdata
.lno_in_preimage
= 1;
795 ecbdata
.lno_in_postimage
= 1;
797 lc_a
= count_lines(data_one
, size_one
);
798 lc_b
= count_lines(data_two
, size_two
);
800 "%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -",
801 line_prefix
, metainfo
, a_name
.buf
, name_a_tab
, reset
,
802 line_prefix
, metainfo
, b_name
.buf
, name_b_tab
, reset
,
803 line_prefix
, fraginfo
);
804 if (!o
->irreversible_delete
)
805 print_line_count(o
->file
, lc_a
);
807 fprintf(o
->file
, "?,?");
808 fprintf(o
->file
, " +");
809 print_line_count(o
->file
, lc_b
);
810 fprintf(o
->file
, " @@%s\n", reset
);
811 if (lc_a
&& !o
->irreversible_delete
)
812 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
814 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
816 free((char *)data_one
);
818 free((char *)data_two
);
821 struct diff_words_buffer
{
824 struct diff_words_orig
{
825 const char *begin
, *end
;
827 int orig_nr
, orig_alloc
;
830 static void diff_words_append(char *line
, unsigned long len
,
831 struct diff_words_buffer
*buffer
)
833 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
836 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
837 buffer
->text
.size
+= len
;
838 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
841 struct diff_words_style_elem
{
844 const char *color
; /* NULL; filled in by the setup code if
845 * color is enabled */
848 struct diff_words_style
{
849 enum diff_words_type type
;
850 struct diff_words_style_elem
new, old
, ctx
;
854 static struct diff_words_style diff_words_styles
[] = {
855 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
856 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
857 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
860 struct diff_words_data
{
861 struct diff_words_buffer minus
, plus
;
862 const char *current_plus
;
864 struct diff_options
*opt
;
866 enum diff_words_type type
;
867 struct diff_words_style
*style
;
870 static int fn_out_diff_words_write_helper(FILE *fp
,
871 struct diff_words_style_elem
*st_el
,
873 size_t count
, const char *buf
,
874 const char *line_prefix
)
879 char *p
= memchr(buf
, '\n', count
);
881 fputs(line_prefix
, fp
);
883 if (st_el
->color
&& fputs(st_el
->color
, fp
) < 0)
885 if (fputs(st_el
->prefix
, fp
) < 0 ||
886 fwrite(buf
, p
? p
- buf
: count
, 1, fp
) != 1 ||
887 fputs(st_el
->suffix
, fp
) < 0)
889 if (st_el
->color
&& *st_el
->color
890 && fputs(GIT_COLOR_RESET
, fp
) < 0)
895 if (fputs(newline
, fp
) < 0)
897 count
-= p
+ 1 - buf
;
905 * '--color-words' algorithm can be described as:
907 * 1. collect a the minus/plus lines of a diff hunk, divided into
908 * minus-lines and plus-lines;
910 * 2. break both minus-lines and plus-lines into words and
911 * place them into two mmfile_t with one word for each line;
913 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
915 * And for the common parts of the both file, we output the plus side text.
916 * diff_words->current_plus is used to trace the current position of the plus file
917 * which printed. diff_words->last_minus is used to trace the last minus word
920 * For '--graph' to work with '--color-words', we need to output the graph prefix
921 * on each line of color words output. Generally, there are two conditions on
922 * which we should output the prefix.
924 * 1. diff_words->last_minus == 0 &&
925 * diff_words->current_plus == diff_words->plus.text.ptr
927 * that is: the plus text must start as a new line, and if there is no minus
928 * word printed, a graph prefix must be printed.
930 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
931 * *(diff_words->current_plus - 1) == '\n'
933 * that is: a graph prefix must be printed following a '\n'
935 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
937 if ((diff_words
->last_minus
== 0 &&
938 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
939 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
940 *(diff_words
->current_plus
- 1) == '\n')) {
947 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
949 struct diff_words_data
*diff_words
= priv
;
950 struct diff_words_style
*style
= diff_words
->style
;
951 int minus_first
, minus_len
, plus_first
, plus_len
;
952 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
953 struct diff_options
*opt
= diff_words
->opt
;
954 const char *line_prefix
;
956 if (line
[0] != '@' || parse_hunk_header(line
, len
,
957 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
961 line_prefix
= diff_line_prefix(opt
);
963 /* POSIX requires that first be decremented by one if len == 0... */
965 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
967 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
969 minus_begin
= minus_end
=
970 diff_words
->minus
.orig
[minus_first
].end
;
973 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
974 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
976 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
978 if (color_words_output_graph_prefix(diff_words
)) {
979 fputs(line_prefix
, diff_words
->opt
->file
);
981 if (diff_words
->current_plus
!= plus_begin
) {
982 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
983 &style
->ctx
, style
->newline
,
984 plus_begin
- diff_words
->current_plus
,
985 diff_words
->current_plus
, line_prefix
);
986 if (*(plus_begin
- 1) == '\n')
987 fputs(line_prefix
, diff_words
->opt
->file
);
989 if (minus_begin
!= minus_end
) {
990 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
991 &style
->old
, style
->newline
,
992 minus_end
- minus_begin
, minus_begin
,
995 if (plus_begin
!= plus_end
) {
996 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
997 &style
->new, style
->newline
,
998 plus_end
- plus_begin
, plus_begin
,
1002 diff_words
->current_plus
= plus_end
;
1003 diff_words
->last_minus
= minus_first
;
1006 /* This function starts looking at *begin, and returns 0 iff a word was found. */
1007 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
1008 int *begin
, int *end
)
1010 if (word_regex
&& *begin
< buffer
->size
) {
1011 regmatch_t match
[1];
1012 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
1013 buffer
->size
- *begin
, 1, match
, 0)) {
1014 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
1015 '\n', match
[0].rm_eo
- match
[0].rm_so
);
1016 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
1017 *begin
+= match
[0].rm_so
;
1018 return *begin
>= *end
;
1023 /* find the next word */
1024 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
1026 if (*begin
>= buffer
->size
)
1029 /* find the end of the word */
1031 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
1038 * This function splits the words in buffer->text, stores the list with
1039 * newline separator into out, and saves the offsets of the original words
1042 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
1043 regex_t
*word_regex
)
1051 /* fake an empty "0th" word */
1052 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
1053 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
1054 buffer
->orig_nr
= 1;
1056 for (i
= 0; i
< buffer
->text
.size
; i
++) {
1057 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
1060 /* store original boundaries */
1061 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
1062 buffer
->orig_alloc
);
1063 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
1064 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
1067 /* store one word */
1068 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
1069 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
1070 out
->ptr
[out
->size
+ j
- i
] = '\n';
1071 out
->size
+= j
- i
+ 1;
1077 /* this executes the word diff on the accumulated buffers */
1078 static void diff_words_show(struct diff_words_data
*diff_words
)
1082 mmfile_t minus
, plus
;
1083 struct diff_words_style
*style
= diff_words
->style
;
1085 struct diff_options
*opt
= diff_words
->opt
;
1086 const char *line_prefix
;
1089 line_prefix
= diff_line_prefix(opt
);
1091 /* special case: only removal */
1092 if (!diff_words
->plus
.text
.size
) {
1093 fputs(line_prefix
, diff_words
->opt
->file
);
1094 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
1095 &style
->old
, style
->newline
,
1096 diff_words
->minus
.text
.size
,
1097 diff_words
->minus
.text
.ptr
, line_prefix
);
1098 diff_words
->minus
.text
.size
= 0;
1102 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
1103 diff_words
->last_minus
= 0;
1105 memset(&xpp
, 0, sizeof(xpp
));
1106 memset(&xecfg
, 0, sizeof(xecfg
));
1107 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
1108 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
1110 /* as only the hunk header will be parsed, we need a 0-context */
1112 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
1114 die("unable to generate word diff");
1117 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
1118 diff_words
->plus
.text
.size
) {
1119 if (color_words_output_graph_prefix(diff_words
))
1120 fputs(line_prefix
, diff_words
->opt
->file
);
1121 fn_out_diff_words_write_helper(diff_words
->opt
->file
,
1122 &style
->ctx
, style
->newline
,
1123 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
1124 - diff_words
->current_plus
, diff_words
->current_plus
,
1127 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
1130 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
1131 static void diff_words_flush(struct emit_callback
*ecbdata
)
1133 if (ecbdata
->diff_words
->minus
.text
.size
||
1134 ecbdata
->diff_words
->plus
.text
.size
)
1135 diff_words_show(ecbdata
->diff_words
);
1138 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1140 /* Use already-loaded driver */
1144 if (S_ISREG(one
->mode
))
1145 one
->driver
= userdiff_find_by_path(one
->path
);
1147 /* Fallback to default settings */
1149 one
->driver
= userdiff_find_by_name("default");
1152 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1154 diff_filespec_load_driver(one
);
1155 return one
->driver
->word_regex
;
1158 static void init_diff_words_data(struct emit_callback
*ecbdata
,
1159 struct diff_options
*orig_opts
,
1160 struct diff_filespec
*one
,
1161 struct diff_filespec
*two
)
1164 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
1165 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
1167 ecbdata
->diff_words
=
1168 xcalloc(1, sizeof(struct diff_words_data
));
1169 ecbdata
->diff_words
->type
= o
->word_diff
;
1170 ecbdata
->diff_words
->opt
= o
;
1172 o
->word_regex
= userdiff_word_regex(one
);
1174 o
->word_regex
= userdiff_word_regex(two
);
1176 o
->word_regex
= diff_word_regex_cfg
;
1177 if (o
->word_regex
) {
1178 ecbdata
->diff_words
->word_regex
= (regex_t
*)
1179 xmalloc(sizeof(regex_t
));
1180 if (regcomp(ecbdata
->diff_words
->word_regex
,
1182 REG_EXTENDED
| REG_NEWLINE
))
1183 die ("Invalid regular expression: %s",
1186 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
1187 if (o
->word_diff
== diff_words_styles
[i
].type
) {
1188 ecbdata
->diff_words
->style
=
1189 &diff_words_styles
[i
];
1193 if (want_color(o
->use_color
)) {
1194 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
1195 st
->old
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1196 st
->new.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1197 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1201 static void free_diff_words_data(struct emit_callback
*ecbdata
)
1203 if (ecbdata
->diff_words
) {
1204 diff_words_flush(ecbdata
);
1205 free (ecbdata
->diff_words
->opt
);
1206 free (ecbdata
->diff_words
->minus
.text
.ptr
);
1207 free (ecbdata
->diff_words
->minus
.orig
);
1208 free (ecbdata
->diff_words
->plus
.text
.ptr
);
1209 free (ecbdata
->diff_words
->plus
.orig
);
1210 if (ecbdata
->diff_words
->word_regex
) {
1211 regfree(ecbdata
->diff_words
->word_regex
);
1212 free(ecbdata
->diff_words
->word_regex
);
1214 free(ecbdata
->diff_words
);
1215 ecbdata
->diff_words
= NULL
;
1219 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
1221 if (want_color(diff_use_color
))
1222 return diff_colors
[ix
];
1226 const char *diff_line_prefix(struct diff_options
*opt
)
1228 struct strbuf
*msgbuf
;
1229 if (!opt
->output_prefix
)
1232 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1236 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
1239 unsigned long allot
;
1243 return ecb
->truncate(line
, len
);
1247 (void) utf8_width(&cp
, &l
);
1249 break; /* truncated in the middle? */
1254 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
1257 ecbdata
->lno_in_preimage
= 0;
1258 ecbdata
->lno_in_postimage
= 0;
1259 p
= strchr(line
, '-');
1261 return; /* cannot happen */
1262 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
1265 return; /* cannot happen */
1266 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
1269 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
1271 struct emit_callback
*ecbdata
= priv
;
1272 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
1273 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1274 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1275 struct diff_options
*o
= ecbdata
->opt
;
1276 const char *line_prefix
= diff_line_prefix(o
);
1278 o
->found_changes
= 1;
1280 if (ecbdata
->header
) {
1281 fprintf(o
->file
, "%s", ecbdata
->header
->buf
);
1282 strbuf_reset(ecbdata
->header
);
1283 ecbdata
->header
= NULL
;
1286 if (ecbdata
->label_path
[0]) {
1287 const char *name_a_tab
, *name_b_tab
;
1289 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
1290 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
1292 fprintf(o
->file
, "%s%s--- %s%s%s\n",
1293 line_prefix
, meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
1294 fprintf(o
->file
, "%s%s+++ %s%s%s\n",
1295 line_prefix
, meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
1296 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
1299 if (diff_suppress_blank_empty
1300 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
1305 if (line
[0] == '@') {
1306 if (ecbdata
->diff_words
)
1307 diff_words_flush(ecbdata
);
1308 len
= sane_truncate_line(ecbdata
, line
, len
);
1309 find_lno(line
, ecbdata
);
1310 emit_hunk_header(ecbdata
, line
, len
);
1311 if (line
[len
-1] != '\n')
1312 putc('\n', o
->file
);
1316 if (ecbdata
->diff_words
) {
1317 if (line
[0] == '-') {
1318 diff_words_append(line
, len
,
1319 &ecbdata
->diff_words
->minus
);
1321 } else if (line
[0] == '+') {
1322 diff_words_append(line
, len
,
1323 &ecbdata
->diff_words
->plus
);
1325 } else if (starts_with(line
, "\\ ")) {
1327 * Eat the "no newline at eof" marker as if we
1328 * saw a "+" or "-" line with nothing on it,
1329 * and return without diff_words_flush() to
1330 * defer processing. If this is the end of
1331 * preimage, more "+" lines may come after it.
1335 diff_words_flush(ecbdata
);
1336 if (ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
) {
1337 emit_line(o
, context
, reset
, line
, len
);
1338 fputs("~\n", o
->file
);
1341 * Skip the prefix character, if any. With
1342 * diff_suppress_blank_empty, there may be
1345 if (line
[0] != '\n') {
1349 emit_line(o
, context
, reset
, line
, len
);
1356 ecbdata
->lno_in_postimage
++;
1357 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
1360 ecbdata
->lno_in_preimage
++;
1361 emit_del_line(reset
, ecbdata
, line
+ 1, len
- 1);
1364 ecbdata
->lno_in_postimage
++;
1365 ecbdata
->lno_in_preimage
++;
1366 emit_context_line(reset
, ecbdata
, line
+ 1, len
- 1);
1369 /* incomplete line at the end */
1370 ecbdata
->lno_in_preimage
++;
1371 emit_line(o
, diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
),
1377 static char *pprint_rename(const char *a
, const char *b
)
1379 const char *old
= a
;
1380 const char *new = b
;
1381 struct strbuf name
= STRBUF_INIT
;
1382 int pfx_length
, sfx_length
;
1383 int pfx_adjust_for_slash
;
1384 int len_a
= strlen(a
);
1385 int len_b
= strlen(b
);
1386 int a_midlen
, b_midlen
;
1387 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
1388 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
1390 if (qlen_a
|| qlen_b
) {
1391 quote_c_style(a
, &name
, NULL
, 0);
1392 strbuf_addstr(&name
, " => ");
1393 quote_c_style(b
, &name
, NULL
, 0);
1394 return strbuf_detach(&name
, NULL
);
1397 /* Find common prefix */
1399 while (*old
&& *new && *old
== *new) {
1401 pfx_length
= old
- a
+ 1;
1406 /* Find common suffix */
1411 * If there is a common prefix, it must end in a slash. In
1412 * that case we let this loop run 1 into the prefix to see the
1415 * If there is no common prefix, we cannot do this as it would
1416 * underrun the input strings.
1418 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
1419 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old
&&
1420 b
+ pfx_length
- pfx_adjust_for_slash
<= new &&
1423 sfx_length
= len_a
- (old
- a
);
1429 * pfx{mid-a => mid-b}sfx
1430 * {pfx-a => pfx-b}sfx
1431 * pfx{sfx-a => sfx-b}
1434 a_midlen
= len_a
- pfx_length
- sfx_length
;
1435 b_midlen
= len_b
- pfx_length
- sfx_length
;
1441 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
1442 if (pfx_length
+ sfx_length
) {
1443 strbuf_add(&name
, a
, pfx_length
);
1444 strbuf_addch(&name
, '{');
1446 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
1447 strbuf_addstr(&name
, " => ");
1448 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
1449 if (pfx_length
+ sfx_length
) {
1450 strbuf_addch(&name
, '}');
1451 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
1453 return strbuf_detach(&name
, NULL
);
1459 struct diffstat_file
{
1463 unsigned is_unmerged
:1;
1464 unsigned is_binary
:1;
1465 unsigned is_renamed
:1;
1466 unsigned is_interesting
:1;
1467 uintmax_t added
, deleted
;
1471 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
1475 struct diffstat_file
*x
;
1476 x
= xcalloc(1, sizeof(*x
));
1477 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
1478 diffstat
->files
[diffstat
->nr
++] = x
;
1480 x
->from_name
= xstrdup(name_a
);
1481 x
->name
= xstrdup(name_b
);
1485 x
->from_name
= NULL
;
1486 x
->name
= xstrdup(name_a
);
1491 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
1493 struct diffstat_t
*diffstat
= priv
;
1494 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
1498 else if (line
[0] == '-')
1502 const char mime_boundary_leader
[] = "------------";
1504 static int scale_linear(int it
, int width
, int max_change
)
1509 * make sure that at least one '-' or '+' is printed if
1510 * there is any change to this path. The easiest way is to
1511 * scale linearly as if the alloted width is one column shorter
1512 * than it is, and then add 1 to the result.
1514 return 1 + (it
* (width
- 1) / max_change
);
1517 static void show_name(FILE *file
,
1518 const char *prefix
, const char *name
, int len
)
1520 fprintf(file
, " %s%-*s |", prefix
, len
, name
);
1523 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
1527 fprintf(file
, "%s", set
);
1530 fprintf(file
, "%s", reset
);
1533 static void fill_print_name(struct diffstat_file
*file
)
1537 if (file
->print_name
)
1540 if (!file
->is_renamed
) {
1541 struct strbuf buf
= STRBUF_INIT
;
1542 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
1543 pname
= strbuf_detach(&buf
, NULL
);
1546 strbuf_release(&buf
);
1549 pname
= pprint_rename(file
->from_name
, file
->name
);
1551 file
->print_name
= pname
;
1554 int print_stat_summary(FILE *fp
, int files
, int insertions
, int deletions
)
1556 struct strbuf sb
= STRBUF_INIT
;
1560 assert(insertions
== 0 && deletions
== 0);
1561 return fprintf(fp
, "%s\n", " 0 files changed");
1565 (files
== 1) ? " %d file changed" : " %d files changed",
1569 * For binary diff, the caller may want to print "x files
1570 * changed" with insertions == 0 && deletions == 0.
1572 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
1573 * is probably less confusing (i.e skip over "2 files changed
1574 * but nothing about added/removed lines? Is this a bug in Git?").
1576 if (insertions
|| deletions
== 0) {
1578 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
1582 if (deletions
|| insertions
== 0) {
1584 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
1587 strbuf_addch(&sb
, '\n');
1588 ret
= fputs(sb
.buf
, fp
);
1589 strbuf_release(&sb
);
1593 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
1595 int i
, len
, add
, del
, adds
= 0, dels
= 0;
1596 uintmax_t max_change
= 0, max_len
= 0;
1597 int total_files
= data
->nr
, count
;
1598 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
1599 const char *reset
, *add_c
, *del_c
;
1600 const char *line_prefix
= "";
1601 int extra_shown
= 0;
1606 line_prefix
= diff_line_prefix(options
);
1607 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
1609 reset
= diff_get_color_opt(options
, DIFF_RESET
);
1610 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
1611 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
1614 * Find the longest filename and max number of changes
1616 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
1617 struct diffstat_file
*file
= data
->files
[i
];
1618 uintmax_t change
= file
->added
+ file
->deleted
;
1620 if (!file
->is_interesting
&& (change
== 0)) {
1621 count
++; /* not shown == room for one more */
1624 fill_print_name(file
);
1625 len
= strlen(file
->print_name
);
1629 if (file
->is_unmerged
) {
1630 /* "Unmerged" is 8 characters */
1631 bin_width
= bin_width
< 8 ? 8 : bin_width
;
1634 if (file
->is_binary
) {
1635 /* "Bin XXX -> YYY bytes" */
1636 int w
= 14 + decimal_width(file
->added
)
1637 + decimal_width(file
->deleted
);
1638 bin_width
= bin_width
< w
? w
: bin_width
;
1639 /* Display change counts aligned with "Bin" */
1644 if (max_change
< change
)
1645 max_change
= change
;
1647 count
= i
; /* where we can stop scanning in data->files[] */
1650 * We have width = stat_width or term_columns() columns total.
1651 * We want a maximum of min(max_len, stat_name_width) for the name part.
1652 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
1653 * We also need 1 for " " and 4 + decimal_width(max_change)
1654 * for " | NNNN " and one the empty column at the end, altogether
1655 * 6 + decimal_width(max_change).
1657 * If there's not enough space, we will use the smaller of
1658 * stat_name_width (if set) and 5/8*width for the filename,
1659 * and the rest for constant elements + graph part, but no more
1660 * than stat_graph_width for the graph part.
1661 * (5/8 gives 50 for filename and 30 for the constant parts + graph
1662 * for the standard terminal size).
1664 * In other words: stat_width limits the maximum width, and
1665 * stat_name_width fixes the maximum width of the filename,
1666 * and is also used to divide available columns if there
1669 * Binary files are displayed with "Bin XXX -> YYY bytes"
1670 * instead of the change count and graph. This part is treated
1671 * similarly to the graph part, except that it is not
1672 * "scaled". If total width is too small to accommodate the
1673 * guaranteed minimum width of the filename part and the
1674 * separators and this message, this message will "overflow"
1675 * making the line longer than the maximum width.
1678 if (options
->stat_width
== -1)
1679 width
= term_columns() - strlen(line_prefix
);
1681 width
= options
->stat_width
? options
->stat_width
: 80;
1682 number_width
= decimal_width(max_change
) > number_width
?
1683 decimal_width(max_change
) : number_width
;
1685 if (options
->stat_graph_width
== -1)
1686 options
->stat_graph_width
= diff_stat_graph_width
;
1689 * Guarantee 3/8*16==6 for the graph part
1690 * and 5/8*16==10 for the filename part
1692 if (width
< 16 + 6 + number_width
)
1693 width
= 16 + 6 + number_width
;
1696 * First assign sizes that are wanted, ignoring available width.
1697 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
1698 * starting from "XXX" should fit in graph_width.
1700 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
1701 if (options
->stat_graph_width
&&
1702 options
->stat_graph_width
< graph_width
)
1703 graph_width
= options
->stat_graph_width
;
1705 name_width
= (options
->stat_name_width
> 0 &&
1706 options
->stat_name_width
< max_len
) ?
1707 options
->stat_name_width
: max_len
;
1710 * Adjust adjustable widths not to exceed maximum width
1712 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
1713 if (graph_width
> width
* 3/8 - number_width
- 6) {
1714 graph_width
= width
* 3/8 - number_width
- 6;
1715 if (graph_width
< 6)
1719 if (options
->stat_graph_width
&&
1720 graph_width
> options
->stat_graph_width
)
1721 graph_width
= options
->stat_graph_width
;
1722 if (name_width
> width
- number_width
- 6 - graph_width
)
1723 name_width
= width
- number_width
- 6 - graph_width
;
1725 graph_width
= width
- number_width
- 6 - name_width
;
1729 * From here name_width is the width of the name area,
1730 * and graph_width is the width of the graph area.
1731 * max_change is used to scale graph properly.
1733 for (i
= 0; i
< count
; i
++) {
1734 const char *prefix
= "";
1735 struct diffstat_file
*file
= data
->files
[i
];
1736 char *name
= file
->print_name
;
1737 uintmax_t added
= file
->added
;
1738 uintmax_t deleted
= file
->deleted
;
1741 if (!file
->is_interesting
&& (added
+ deleted
== 0))
1745 * "scale" the filename
1748 name_len
= strlen(name
);
1749 if (name_width
< name_len
) {
1753 name
+= name_len
- len
;
1754 slash
= strchr(name
, '/');
1759 if (file
->is_binary
) {
1760 fprintf(options
->file
, "%s", line_prefix
);
1761 show_name(options
->file
, prefix
, name
, len
);
1762 fprintf(options
->file
, " %*s", number_width
, "Bin");
1763 if (!added
&& !deleted
) {
1764 putc('\n', options
->file
);
1767 fprintf(options
->file
, " %s%"PRIuMAX
"%s",
1768 del_c
, deleted
, reset
);
1769 fprintf(options
->file
, " -> ");
1770 fprintf(options
->file
, "%s%"PRIuMAX
"%s",
1771 add_c
, added
, reset
);
1772 fprintf(options
->file
, " bytes");
1773 fprintf(options
->file
, "\n");
1776 else if (file
->is_unmerged
) {
1777 fprintf(options
->file
, "%s", line_prefix
);
1778 show_name(options
->file
, prefix
, name
, len
);
1779 fprintf(options
->file
, " Unmerged\n");
1784 * scale the add/delete
1789 if (graph_width
<= max_change
) {
1790 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
1791 if (total
< 2 && add
&& del
)
1792 /* width >= 2 due to the sanity check */
1795 add
= scale_linear(add
, graph_width
, max_change
);
1798 del
= scale_linear(del
, graph_width
, max_change
);
1802 fprintf(options
->file
, "%s", line_prefix
);
1803 show_name(options
->file
, prefix
, name
, len
);
1804 fprintf(options
->file
, " %*"PRIuMAX
"%s",
1805 number_width
, added
+ deleted
,
1806 added
+ deleted
? " " : "");
1807 show_graph(options
->file
, '+', add
, add_c
, reset
);
1808 show_graph(options
->file
, '-', del
, del_c
, reset
);
1809 fprintf(options
->file
, "\n");
1812 for (i
= 0; i
< data
->nr
; i
++) {
1813 struct diffstat_file
*file
= data
->files
[i
];
1814 uintmax_t added
= file
->added
;
1815 uintmax_t deleted
= file
->deleted
;
1817 if (file
->is_unmerged
||
1818 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
1823 if (!file
->is_binary
) {
1830 fprintf(options
->file
, "%s ...\n", line_prefix
);
1833 fprintf(options
->file
, "%s", line_prefix
);
1834 print_stat_summary(options
->file
, total_files
, adds
, dels
);
1837 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
1839 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
1844 for (i
= 0; i
< data
->nr
; i
++) {
1845 int added
= data
->files
[i
]->added
;
1846 int deleted
= data
->files
[i
]->deleted
;
1848 if (data
->files
[i
]->is_unmerged
||
1849 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
1851 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
1856 fprintf(options
->file
, "%s", diff_line_prefix(options
));
1857 print_stat_summary(options
->file
, total_files
, adds
, dels
);
1860 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
1867 for (i
= 0; i
< data
->nr
; i
++) {
1868 struct diffstat_file
*file
= data
->files
[i
];
1870 fprintf(options
->file
, "%s", diff_line_prefix(options
));
1872 if (file
->is_binary
)
1873 fprintf(options
->file
, "-\t-\t");
1875 fprintf(options
->file
,
1876 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
1877 file
->added
, file
->deleted
);
1878 if (options
->line_termination
) {
1879 fill_print_name(file
);
1880 if (!file
->is_renamed
)
1881 write_name_quoted(file
->name
, options
->file
,
1882 options
->line_termination
);
1884 fputs(file
->print_name
, options
->file
);
1885 putc(options
->line_termination
, options
->file
);
1888 if (file
->is_renamed
) {
1889 putc('\0', options
->file
);
1890 write_name_quoted(file
->from_name
, options
->file
, '\0');
1892 write_name_quoted(file
->name
, options
->file
, '\0');
1897 struct dirstat_file
{
1899 unsigned long changed
;
1902 struct dirstat_dir
{
1903 struct dirstat_file
*files
;
1904 int alloc
, nr
, permille
, cumulative
;
1907 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
1908 unsigned long changed
, const char *base
, int baselen
)
1910 unsigned long this_dir
= 0;
1911 unsigned int sources
= 0;
1912 const char *line_prefix
= diff_line_prefix(opt
);
1915 struct dirstat_file
*f
= dir
->files
;
1916 int namelen
= strlen(f
->name
);
1920 if (namelen
< baselen
)
1922 if (memcmp(f
->name
, base
, baselen
))
1924 slash
= strchr(f
->name
+ baselen
, '/');
1926 int newbaselen
= slash
+ 1 - f
->name
;
1927 this = gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
1939 * We don't report dirstat's for
1941 * - or cases where everything came from a single directory
1942 * under this directory (sources == 1).
1944 if (baselen
&& sources
!= 1) {
1946 int permille
= this_dir
* 1000 / changed
;
1947 if (permille
>= dir
->permille
) {
1948 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
1949 permille
/ 10, permille
% 10, baselen
, base
);
1950 if (!dir
->cumulative
)
1958 static int dirstat_compare(const void *_a
, const void *_b
)
1960 const struct dirstat_file
*a
= _a
;
1961 const struct dirstat_file
*b
= _b
;
1962 return strcmp(a
->name
, b
->name
);
1965 static void show_dirstat(struct diff_options
*options
)
1968 unsigned long changed
;
1969 struct dirstat_dir dir
;
1970 struct diff_queue_struct
*q
= &diff_queued_diff
;
1975 dir
.permille
= options
->dirstat_permille
;
1976 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1979 for (i
= 0; i
< q
->nr
; i
++) {
1980 struct diff_filepair
*p
= q
->queue
[i
];
1982 unsigned long copied
, added
, damage
;
1983 int content_changed
;
1985 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
1987 if (p
->one
->oid_valid
&& p
->two
->oid_valid
)
1988 content_changed
= oidcmp(&p
->one
->oid
, &p
->two
->oid
);
1990 content_changed
= 1;
1992 if (!content_changed
) {
1994 * The SHA1 has not changed, so pre-/post-content is
1995 * identical. We can therefore skip looking at the
1996 * file contents altogether.
2002 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
)) {
2004 * In --dirstat-by-file mode, we don't really need to
2005 * look at the actual file contents at all.
2006 * The fact that the SHA1 changed is enough for us to
2007 * add this file to the list of results
2008 * (with each file contributing equal damage).
2014 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
2015 diff_populate_filespec(p
->one
, 0);
2016 diff_populate_filespec(p
->two
, 0);
2017 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
,
2019 diff_free_filespec_data(p
->one
);
2020 diff_free_filespec_data(p
->two
);
2021 } else if (DIFF_FILE_VALID(p
->one
)) {
2022 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
);
2024 diff_free_filespec_data(p
->one
);
2025 } else if (DIFF_FILE_VALID(p
->two
)) {
2026 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
);
2028 added
= p
->two
->size
;
2029 diff_free_filespec_data(p
->two
);
2034 * Original minus copied is the removed material,
2035 * added is the new material. They are both damages
2036 * made to the preimage.
2037 * If the resulting damage is zero, we know that
2038 * diffcore_count_changes() considers the two entries to
2039 * be identical, but since content_changed is true, we
2040 * know that there must have been _some_ kind of change,
2041 * so we force all entries to have damage > 0.
2043 damage
= (p
->one
->size
- copied
) + added
;
2048 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2049 dir
.files
[dir
.nr
].name
= 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
, dirstat_compare
);
2061 gather_dirstat(options
, &dir
, changed
, "", 0);
2064 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
2067 unsigned long changed
;
2068 struct dirstat_dir dir
;
2076 dir
.permille
= options
->dirstat_permille
;
2077 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
2080 for (i
= 0; i
< data
->nr
; i
++) {
2081 struct diffstat_file
*file
= data
->files
[i
];
2082 unsigned long damage
= file
->added
+ file
->deleted
;
2083 if (file
->is_binary
)
2085 * binary files counts bytes, not lines. Must find some
2086 * way to normalize binary bytes vs. textual lines.
2087 * The following heuristic assumes that there are 64
2089 * This is stupid and ugly, but very cheap...
2091 damage
= (damage
+ 63) / 64;
2092 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2093 dir
.files
[dir
.nr
].name
= file
->name
;
2094 dir
.files
[dir
.nr
].changed
= damage
;
2099 /* This can happen even with many files, if everything was renames */
2103 /* Show all directories with more than x% of the changes */
2104 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
2105 gather_dirstat(options
, &dir
, changed
, "", 0);
2108 static void free_diffstat_info(struct diffstat_t
*diffstat
)
2111 for (i
= 0; i
< diffstat
->nr
; i
++) {
2112 struct diffstat_file
*f
= diffstat
->files
[i
];
2113 if (f
->name
!= f
->print_name
)
2114 free(f
->print_name
);
2119 free(diffstat
->files
);
2122 struct checkdiff_t
{
2123 const char *filename
;
2125 int conflict_marker_size
;
2126 struct diff_options
*o
;
2131 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
2136 if (len
< marker_size
+ 1)
2138 firstchar
= line
[0];
2139 switch (firstchar
) {
2140 case '=': case '>': case '<': case '|':
2145 for (cnt
= 1; cnt
< marker_size
; cnt
++)
2146 if (line
[cnt
] != firstchar
)
2148 /* line[1] thru line[marker_size-1] are same as firstchar */
2149 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
2154 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
2156 struct checkdiff_t
*data
= priv
;
2157 int marker_size
= data
->conflict_marker_size
;
2158 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
2159 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
2160 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
2162 const char *line_prefix
;
2165 line_prefix
= diff_line_prefix(data
->o
);
2167 if (line
[0] == '+') {
2170 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
2172 fprintf(data
->o
->file
,
2173 "%s%s:%d: leftover conflict marker\n",
2174 line_prefix
, data
->filename
, data
->lineno
);
2176 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
2179 data
->status
|= bad
;
2180 err
= whitespace_error_string(bad
);
2181 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
2182 line_prefix
, data
->filename
, data
->lineno
, err
);
2184 emit_line(data
->o
, set
, reset
, line
, 1);
2185 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
2186 data
->o
->file
, set
, reset
, ws
);
2187 } else if (line
[0] == ' ') {
2189 } else if (line
[0] == '@') {
2190 char *plus
= strchr(line
, '+');
2192 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
2194 die("invalid diff");
2198 static unsigned char *deflate_it(char *data
,
2200 unsigned long *result_size
)
2203 unsigned char *deflated
;
2206 git_deflate_init(&stream
, zlib_compression_level
);
2207 bound
= git_deflate_bound(&stream
, size
);
2208 deflated
= xmalloc(bound
);
2209 stream
.next_out
= deflated
;
2210 stream
.avail_out
= bound
;
2212 stream
.next_in
= (unsigned char *)data
;
2213 stream
.avail_in
= size
;
2214 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
2216 git_deflate_end(&stream
);
2217 *result_size
= stream
.total_out
;
2221 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
,
2228 unsigned long orig_size
;
2229 unsigned long delta_size
;
2230 unsigned long deflate_size
;
2231 unsigned long data_size
;
2233 /* We could do deflated delta, or we could do just deflated two,
2234 * whichever is smaller.
2237 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
2238 if (one
->size
&& two
->size
) {
2239 delta
= diff_delta(one
->ptr
, one
->size
,
2240 two
->ptr
, two
->size
,
2241 &delta_size
, deflate_size
);
2243 void *to_free
= delta
;
2244 orig_size
= delta_size
;
2245 delta
= deflate_it(delta
, delta_size
, &delta_size
);
2250 if (delta
&& delta_size
< deflate_size
) {
2251 fprintf(file
, "%sdelta %lu\n", prefix
, orig_size
);
2254 data_size
= delta_size
;
2257 fprintf(file
, "%sliteral %lu\n", prefix
, two
->size
);
2260 data_size
= deflate_size
;
2263 /* emit data encoded in base85 */
2266 int bytes
= (52 < data_size
) ? 52 : data_size
;
2270 line
[0] = bytes
+ 'A' - 1;
2272 line
[0] = bytes
- 26 + 'a' - 1;
2273 encode_85(line
+ 1, cp
, bytes
);
2274 cp
= (char *) cp
+ bytes
;
2275 fprintf(file
, "%s", prefix
);
2279 fprintf(file
, "%s\n", prefix
);
2283 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
,
2286 fprintf(file
, "%sGIT binary patch\n", prefix
);
2287 emit_binary_diff_body(file
, one
, two
, prefix
);
2288 emit_binary_diff_body(file
, two
, one
, prefix
);
2291 int diff_filespec_is_binary(struct diff_filespec
*one
)
2293 if (one
->is_binary
== -1) {
2294 diff_filespec_load_driver(one
);
2295 if (one
->driver
->binary
!= -1)
2296 one
->is_binary
= one
->driver
->binary
;
2298 if (!one
->data
&& DIFF_FILE_VALID(one
))
2299 diff_populate_filespec(one
, CHECK_BINARY
);
2300 if (one
->is_binary
== -1 && one
->data
)
2301 one
->is_binary
= buffer_is_binary(one
->data
,
2303 if (one
->is_binary
== -1)
2307 return one
->is_binary
;
2310 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
2312 diff_filespec_load_driver(one
);
2313 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
2316 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
2318 if (!options
->a_prefix
)
2319 options
->a_prefix
= a
;
2320 if (!options
->b_prefix
)
2321 options
->b_prefix
= b
;
2324 struct userdiff_driver
*get_textconv(struct diff_filespec
*one
)
2326 if (!DIFF_FILE_VALID(one
))
2329 diff_filespec_load_driver(one
);
2330 return userdiff_get_textconv(one
->driver
);
2333 static void builtin_diff(const char *name_a
,
2335 struct diff_filespec
*one
,
2336 struct diff_filespec
*two
,
2337 const char *xfrm_msg
,
2338 int must_show_header
,
2339 struct diff_options
*o
,
2340 int complete_rewrite
)
2344 char *a_one
, *b_two
;
2345 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
2346 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
2347 const char *a_prefix
, *b_prefix
;
2348 struct userdiff_driver
*textconv_one
= NULL
;
2349 struct userdiff_driver
*textconv_two
= NULL
;
2350 struct strbuf header
= STRBUF_INIT
;
2351 const char *line_prefix
= diff_line_prefix(o
);
2353 diff_set_mnemonic_prefix(o
, "a/", "b/");
2354 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
2355 a_prefix
= o
->b_prefix
;
2356 b_prefix
= o
->a_prefix
;
2358 a_prefix
= o
->a_prefix
;
2359 b_prefix
= o
->b_prefix
;
2362 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
2363 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
2364 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
2365 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2366 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2367 show_submodule_summary(o
->file
, one
->path
? one
->path
: two
->path
,
2369 &one
->oid
, &two
->oid
,
2370 two
->dirty_submodule
,
2371 meta
, del
, add
, reset
);
2373 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
2374 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
2375 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
2376 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2377 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2378 show_submodule_inline_diff(o
->file
, one
->path
? one
->path
: two
->path
,
2380 &one
->oid
, &two
->oid
,
2381 two
->dirty_submodule
,
2382 meta
, del
, add
, reset
, o
);
2386 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
2387 textconv_one
= get_textconv(one
);
2388 textconv_two
= get_textconv(two
);
2391 /* Never use a non-valid filename anywhere if at all possible */
2392 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
2393 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
2395 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
2396 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
2397 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
2398 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
2399 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
2400 if (lbl
[0][0] == '/') {
2402 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
2404 strbuf_addstr(&header
, xfrm_msg
);
2405 must_show_header
= 1;
2407 else if (lbl
[1][0] == '/') {
2408 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
2410 strbuf_addstr(&header
, xfrm_msg
);
2411 must_show_header
= 1;
2414 if (one
->mode
!= two
->mode
) {
2415 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
2416 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
2417 must_show_header
= 1;
2420 strbuf_addstr(&header
, xfrm_msg
);
2423 * we do not run diff between different kind
2426 if ((one
->mode
^ two
->mode
) & S_IFMT
)
2427 goto free_ab_and_return
;
2428 if (complete_rewrite
&&
2429 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
2430 (textconv_two
|| !diff_filespec_is_binary(two
))) {
2431 fprintf(o
->file
, "%s", header
.buf
);
2432 strbuf_reset(&header
);
2433 emit_rewrite_diff(name_a
, name_b
, one
, two
,
2434 textconv_one
, textconv_two
, o
);
2435 o
->found_changes
= 1;
2436 goto free_ab_and_return
;
2440 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
2441 fprintf(o
->file
, "%s", header
.buf
);
2442 strbuf_reset(&header
);
2443 goto free_ab_and_return
;
2444 } else if (!DIFF_OPT_TST(o
, TEXT
) &&
2445 ( (!textconv_one
&& diff_filespec_is_binary(one
)) ||
2446 (!textconv_two
&& diff_filespec_is_binary(two
)) )) {
2447 if (!one
->data
&& !two
->data
&&
2448 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
2449 !DIFF_OPT_TST(o
, BINARY
)) {
2450 if (!oidcmp(&one
->oid
, &two
->oid
)) {
2451 if (must_show_header
)
2452 fprintf(o
->file
, "%s", header
.buf
);
2453 goto free_ab_and_return
;
2455 fprintf(o
->file
, "%s", header
.buf
);
2456 fprintf(o
->file
, "%sBinary files %s and %s differ\n",
2457 line_prefix
, lbl
[0], lbl
[1]);
2458 goto free_ab_and_return
;
2460 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
2461 die("unable to read files to diff");
2462 /* Quite common confusing case */
2463 if (mf1
.size
== mf2
.size
&&
2464 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
2465 if (must_show_header
)
2466 fprintf(o
->file
, "%s", header
.buf
);
2467 goto free_ab_and_return
;
2469 fprintf(o
->file
, "%s", header
.buf
);
2470 strbuf_reset(&header
);
2471 if (DIFF_OPT_TST(o
, BINARY
))
2472 emit_binary_diff(o
->file
, &mf1
, &mf2
, line_prefix
);
2474 fprintf(o
->file
, "%sBinary files %s and %s differ\n",
2475 line_prefix
, lbl
[0], lbl
[1]);
2476 o
->found_changes
= 1;
2478 /* Crazy xdl interfaces.. */
2479 const char *diffopts
= getenv("GIT_DIFF_OPTS");
2483 struct emit_callback ecbdata
;
2484 const struct userdiff_funcname
*pe
;
2486 if (must_show_header
) {
2487 fprintf(o
->file
, "%s", header
.buf
);
2488 strbuf_reset(&header
);
2491 mf1
.size
= fill_textconv(textconv_one
, one
, &mf1
.ptr
);
2492 mf2
.size
= fill_textconv(textconv_two
, two
, &mf2
.ptr
);
2494 pe
= diff_funcname_pattern(one
);
2496 pe
= diff_funcname_pattern(two
);
2498 memset(&xpp
, 0, sizeof(xpp
));
2499 memset(&xecfg
, 0, sizeof(xecfg
));
2500 memset(&ecbdata
, 0, sizeof(ecbdata
));
2501 ecbdata
.label_path
= lbl
;
2502 ecbdata
.color_diff
= want_color(o
->use_color
);
2503 ecbdata
.ws_rule
= whitespace_rule(name_b
);
2504 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
2505 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
2507 ecbdata
.header
= header
.len
? &header
: NULL
;
2508 xpp
.flags
= o
->xdl_opts
;
2509 xecfg
.ctxlen
= o
->context
;
2510 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
2511 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2512 if (DIFF_OPT_TST(o
, FUNCCONTEXT
))
2513 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
2515 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
2518 else if (skip_prefix(diffopts
, "--unified=", &v
))
2519 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
2520 else if (skip_prefix(diffopts
, "-u", &v
))
2521 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
2523 init_diff_words_data(&ecbdata
, o
, one
, two
);
2524 if (xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
2526 die("unable to generate diff for %s", one
->path
);
2528 free_diff_words_data(&ecbdata
);
2533 xdiff_clear_find_func(&xecfg
);
2537 strbuf_release(&header
);
2538 diff_free_filespec_data(one
);
2539 diff_free_filespec_data(two
);
2545 static void builtin_diffstat(const char *name_a
, const char *name_b
,
2546 struct diff_filespec
*one
,
2547 struct diff_filespec
*two
,
2548 struct diffstat_t
*diffstat
,
2549 struct diff_options
*o
,
2550 struct diff_filepair
*p
)
2553 struct diffstat_file
*data
;
2555 int complete_rewrite
= 0;
2557 if (!DIFF_PAIR_UNMERGED(p
)) {
2558 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2559 complete_rewrite
= 1;
2562 data
= diffstat_add(diffstat
, name_a
, name_b
);
2563 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
2566 data
->is_unmerged
= 1;
2570 same_contents
= !oidcmp(&one
->oid
, &two
->oid
);
2572 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
2573 data
->is_binary
= 1;
2574 if (same_contents
) {
2578 data
->added
= diff_filespec_size(two
);
2579 data
->deleted
= diff_filespec_size(one
);
2583 else if (complete_rewrite
) {
2584 diff_populate_filespec(one
, 0);
2585 diff_populate_filespec(two
, 0);
2586 data
->deleted
= count_lines(one
->data
, one
->size
);
2587 data
->added
= count_lines(two
->data
, two
->size
);
2590 else if (!same_contents
) {
2591 /* Crazy xdl interfaces.. */
2595 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
2596 die("unable to read files to diff");
2598 memset(&xpp
, 0, sizeof(xpp
));
2599 memset(&xecfg
, 0, sizeof(xecfg
));
2600 xpp
.flags
= o
->xdl_opts
;
2601 xecfg
.ctxlen
= o
->context
;
2602 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
2603 if (xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
2605 die("unable to generate diffstat for %s", one
->path
);
2608 diff_free_filespec_data(one
);
2609 diff_free_filespec_data(two
);
2612 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
2613 const char *attr_path
,
2614 struct diff_filespec
*one
,
2615 struct diff_filespec
*two
,
2616 struct diff_options
*o
)
2619 struct checkdiff_t data
;
2624 memset(&data
, 0, sizeof(data
));
2625 data
.filename
= name_b
? name_b
: name_a
;
2628 data
.ws_rule
= whitespace_rule(attr_path
);
2629 data
.conflict_marker_size
= ll_merge_marker_size(attr_path
);
2631 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
2632 die("unable to read files to diff");
2635 * All the other codepaths check both sides, but not checking
2636 * the "old" side here is deliberate. We are checking the newly
2637 * introduced changes, and as long as the "new" side is text, we
2638 * can and should check what it introduces.
2640 if (diff_filespec_is_binary(two
))
2641 goto free_and_return
;
2643 /* Crazy xdl interfaces.. */
2647 memset(&xpp
, 0, sizeof(xpp
));
2648 memset(&xecfg
, 0, sizeof(xecfg
));
2649 xecfg
.ctxlen
= 1; /* at least one context line */
2651 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
2653 die("unable to generate checkdiff for %s", one
->path
);
2655 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
2656 struct emit_callback ecbdata
;
2659 ecbdata
.ws_rule
= data
.ws_rule
;
2660 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
2661 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
2666 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
2667 fprintf(o
->file
, "%s:%d: %s.\n",
2668 data
.filename
, blank_at_eof
, err
);
2669 data
.status
= 1; /* report errors */
2674 diff_free_filespec_data(one
);
2675 diff_free_filespec_data(two
);
2677 DIFF_OPT_SET(o
, CHECK_FAILED
);
2680 struct diff_filespec
*alloc_filespec(const char *path
)
2682 struct diff_filespec
*spec
;
2684 FLEXPTR_ALLOC_STR(spec
, path
, path
);
2686 spec
->is_binary
= -1;
2690 void free_filespec(struct diff_filespec
*spec
)
2692 if (!--spec
->count
) {
2693 diff_free_filespec_data(spec
);
2698 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
2699 int sha1_valid
, unsigned short mode
)
2702 spec
->mode
= canon_mode(mode
);
2703 hashcpy(spec
->oid
.hash
, sha1
);
2704 spec
->oid_valid
= sha1_valid
;
2709 * Given a name and sha1 pair, if the index tells us the file in
2710 * the work tree has that object contents, return true, so that
2711 * prepare_temp_file() does not have to inflate and extract.
2713 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
2715 const struct cache_entry
*ce
;
2720 * We do not read the cache ourselves here, because the
2721 * benchmark with my previous version that always reads cache
2722 * shows that it makes things worse for diff-tree comparing
2723 * two linux-2.6 kernel trees in an already checked out work
2724 * tree. This is because most diff-tree comparisons deal with
2725 * only a small number of files, while reading the cache is
2726 * expensive for a large project, and its cost outweighs the
2727 * savings we get by not inflating the object to a temporary
2728 * file. Practically, this code only helps when we are used
2729 * by diff-cache --cached, which does read the cache before
2735 /* We want to avoid the working directory if our caller
2736 * doesn't need the data in a normal file, this system
2737 * is rather slow with its stat/open/mmap/close syscalls,
2738 * and the object is contained in a pack file. The pack
2739 * is probably already open and will be faster to obtain
2740 * the data through than the working directory. Loose
2741 * objects however would tend to be slower as they need
2742 * to be individually opened and inflated.
2744 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
))
2748 * Similarly, if we'd have to convert the file contents anyway, that
2749 * makes the optimization not worthwhile.
2751 if (!want_file
&& would_convert_to_git(name
))
2755 pos
= cache_name_pos(name
, len
);
2758 ce
= active_cache
[pos
];
2761 * This is not the sha1 we are looking for, or
2762 * unreusable because it is not a regular file.
2764 if (hashcmp(sha1
, ce
->oid
.hash
) || !S_ISREG(ce
->ce_mode
))
2768 * If ce is marked as "assume unchanged", there is no
2769 * guarantee that work tree matches what we are looking for.
2771 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2775 * If ce matches the file in the work tree, we can reuse it.
2777 if (ce_uptodate(ce
) ||
2778 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
2784 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
2786 struct strbuf buf
= STRBUF_INIT
;
2789 /* Are we looking at the work tree? */
2790 if (s
->dirty_submodule
)
2793 strbuf_addf(&buf
, "Subproject commit %s%s\n",
2794 oid_to_hex(&s
->oid
), dirty
);
2798 strbuf_release(&buf
);
2800 s
->data
= strbuf_detach(&buf
, NULL
);
2807 * While doing rename detection and pickaxe operation, we may need to
2808 * grab the data for the blob (or file) for our own in-core comparison.
2809 * diff_filespec has data and size fields for this purpose.
2811 int diff_populate_filespec(struct diff_filespec
*s
, unsigned int flags
)
2813 int size_only
= flags
& CHECK_SIZE_ONLY
;
2816 * demote FAIL to WARN to allow inspecting the situation
2817 * instead of refusing.
2819 enum safe_crlf crlf_warn
= (safe_crlf
== SAFE_CRLF_FAIL
2823 if (!DIFF_FILE_VALID(s
))
2824 die("internal error: asking to populate invalid file.");
2825 if (S_ISDIR(s
->mode
))
2831 if (size_only
&& 0 < s
->size
)
2834 if (S_ISGITLINK(s
->mode
))
2835 return diff_populate_gitlink(s
, size_only
);
2837 if (!s
->oid_valid
||
2838 reuse_worktree_file(s
->path
, s
->oid
.hash
, 0)) {
2839 struct strbuf buf
= STRBUF_INIT
;
2843 if (lstat(s
->path
, &st
) < 0) {
2844 if (errno
== ENOENT
) {
2848 s
->data
= (char *)"";
2853 s
->size
= xsize_t(st
.st_size
);
2856 if (S_ISLNK(st
.st_mode
)) {
2857 struct strbuf sb
= STRBUF_INIT
;
2859 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
2862 s
->data
= strbuf_detach(&sb
, NULL
);
2868 if ((flags
& CHECK_BINARY
) &&
2869 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
2873 fd
= open(s
->path
, O_RDONLY
);
2876 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2878 s
->should_munmap
= 1;
2881 * Convert from working tree format to canonical git format
2883 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, crlf_warn
)) {
2885 munmap(s
->data
, s
->size
);
2886 s
->should_munmap
= 0;
2887 s
->data
= strbuf_detach(&buf
, &size
);
2893 enum object_type type
;
2894 if (size_only
|| (flags
& CHECK_BINARY
)) {
2895 type
= sha1_object_info(s
->oid
.hash
, &s
->size
);
2897 die("unable to read %s",
2898 oid_to_hex(&s
->oid
));
2901 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
2906 s
->data
= read_sha1_file(s
->oid
.hash
, &type
, &s
->size
);
2908 die("unable to read %s", oid_to_hex(&s
->oid
));
2914 void diff_free_filespec_blob(struct diff_filespec
*s
)
2918 else if (s
->should_munmap
)
2919 munmap(s
->data
, s
->size
);
2921 if (s
->should_free
|| s
->should_munmap
) {
2922 s
->should_free
= s
->should_munmap
= 0;
2927 void diff_free_filespec_data(struct diff_filespec
*s
)
2929 diff_free_filespec_blob(s
);
2934 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
2937 const struct object_id
*oid
,
2941 struct strbuf buf
= STRBUF_INIT
;
2942 struct strbuf
template = STRBUF_INIT
;
2943 char *path_dup
= xstrdup(path
);
2944 const char *base
= basename(path_dup
);
2946 /* Generate "XXXXXX_basename.ext" */
2947 strbuf_addstr(&template, "XXXXXX_");
2948 strbuf_addstr(&template, base
);
2950 fd
= mks_tempfile_ts(&temp
->tempfile
, template.buf
, strlen(base
) + 1);
2952 die_errno("unable to create temp-file");
2953 if (convert_to_working_tree(path
,
2954 (const char *)blob
, (size_t)size
, &buf
)) {
2958 if (write_in_full(fd
, blob
, size
) != size
)
2959 die_errno("unable to write temp-file");
2960 close_tempfile(&temp
->tempfile
);
2961 temp
->name
= get_tempfile_path(&temp
->tempfile
);
2962 oid_to_hex_r(temp
->hex
, oid
);
2963 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
2964 strbuf_release(&buf
);
2965 strbuf_release(&template);
2969 static struct diff_tempfile
*prepare_temp_file(const char *name
,
2970 struct diff_filespec
*one
)
2972 struct diff_tempfile
*temp
= claim_diff_tempfile();
2974 if (!DIFF_FILE_VALID(one
)) {
2976 /* A '-' entry produces this for file-2, and
2977 * a '+' entry produces this for file-1.
2979 temp
->name
= "/dev/null";
2980 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
2981 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
2985 if (!S_ISGITLINK(one
->mode
) &&
2987 reuse_worktree_file(name
, one
->oid
.hash
, 1))) {
2989 if (lstat(name
, &st
) < 0) {
2990 if (errno
== ENOENT
)
2991 goto not_a_valid_file
;
2992 die_errno("stat(%s)", name
);
2994 if (S_ISLNK(st
.st_mode
)) {
2995 struct strbuf sb
= STRBUF_INIT
;
2996 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
2997 die_errno("readlink(%s)", name
);
2998 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
3000 &one
->oid
: &null_oid
),
3002 one
->mode
: S_IFLNK
));
3003 strbuf_release(&sb
);
3006 /* we can borrow from the file in the work tree */
3008 if (!one
->oid_valid
)
3009 sha1_to_hex_r(temp
->hex
, null_sha1
);
3011 sha1_to_hex_r(temp
->hex
, one
->oid
.hash
);
3012 /* Even though we may sometimes borrow the
3013 * contents from the work tree, we always want
3014 * one->mode. mode is trustworthy even when
3015 * !(one->sha1_valid), as long as
3016 * DIFF_FILE_VALID(one).
3018 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
3023 if (diff_populate_filespec(one
, 0))
3024 die("cannot read data blob for %s", one
->path
);
3025 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
3026 &one
->oid
, one
->mode
);
3031 static void add_external_diff_name(struct argv_array
*argv
,
3033 struct diff_filespec
*df
)
3035 struct diff_tempfile
*temp
= prepare_temp_file(name
, df
);
3036 argv_array_push(argv
, temp
->name
);
3037 argv_array_push(argv
, temp
->hex
);
3038 argv_array_push(argv
, temp
->mode
);
3041 /* An external diff command takes:
3043 * diff-cmd name infile1 infile1-sha1 infile1-mode \
3044 * infile2 infile2-sha1 infile2-mode [ rename-to ]
3047 static void run_external_diff(const char *pgm
,
3050 struct diff_filespec
*one
,
3051 struct diff_filespec
*two
,
3052 const char *xfrm_msg
,
3053 int complete_rewrite
,
3054 struct diff_options
*o
)
3056 struct argv_array argv
= ARGV_ARRAY_INIT
;
3057 struct argv_array env
= ARGV_ARRAY_INIT
;
3058 struct diff_queue_struct
*q
= &diff_queued_diff
;
3060 argv_array_push(&argv
, pgm
);
3061 argv_array_push(&argv
, name
);
3064 add_external_diff_name(&argv
, name
, one
);
3066 add_external_diff_name(&argv
, name
, two
);
3068 add_external_diff_name(&argv
, other
, two
);
3069 argv_array_push(&argv
, other
);
3070 argv_array_push(&argv
, xfrm_msg
);
3074 argv_array_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
3075 argv_array_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
3077 if (run_command_v_opt_cd_env(argv
.argv
, RUN_USING_SHELL
, NULL
, env
.argv
))
3078 die(_("external diff died, stopping at %s"), name
);
3081 argv_array_clear(&argv
);
3082 argv_array_clear(&env
);
3085 static int similarity_index(struct diff_filepair
*p
)
3087 return p
->score
* 100 / MAX_SCORE
;
3090 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
3092 if (startup_info
->have_repository
)
3093 return find_unique_abbrev(oid
->hash
, abbrev
);
3095 char *hex
= oid_to_hex(oid
);
3097 abbrev
= FALLBACK_DEFAULT_ABBREV
;
3098 if (abbrev
> GIT_SHA1_HEXSZ
)
3099 die("BUG: oid abbreviation out of range: %d", abbrev
);
3106 static void fill_metainfo(struct strbuf
*msg
,
3109 struct diff_filespec
*one
,
3110 struct diff_filespec
*two
,
3111 struct diff_options
*o
,
3112 struct diff_filepair
*p
,
3113 int *must_show_header
,
3116 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
3117 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
3118 const char *line_prefix
= diff_line_prefix(o
);
3120 *must_show_header
= 1;
3121 strbuf_init(msg
, PATH_MAX
* 2 + 300);
3122 switch (p
->status
) {
3123 case DIFF_STATUS_COPIED
:
3124 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3125 line_prefix
, set
, similarity_index(p
));
3126 strbuf_addf(msg
, "%s\n%s%scopy from ",
3127 reset
, line_prefix
, set
);
3128 quote_c_style(name
, msg
, NULL
, 0);
3129 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
3130 quote_c_style(other
, msg
, NULL
, 0);
3131 strbuf_addf(msg
, "%s\n", reset
);
3133 case DIFF_STATUS_RENAMED
:
3134 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3135 line_prefix
, set
, similarity_index(p
));
3136 strbuf_addf(msg
, "%s\n%s%srename from ",
3137 reset
, line_prefix
, set
);
3138 quote_c_style(name
, msg
, NULL
, 0);
3139 strbuf_addf(msg
, "%s\n%s%srename to ",
3140 reset
, line_prefix
, set
);
3141 quote_c_style(other
, msg
, NULL
, 0);
3142 strbuf_addf(msg
, "%s\n", reset
);
3144 case DIFF_STATUS_MODIFIED
:
3146 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
3148 set
, similarity_index(p
), reset
);
3153 *must_show_header
= 0;
3155 if (one
&& two
&& oidcmp(&one
->oid
, &two
->oid
)) {
3156 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
3158 if (DIFF_OPT_TST(o
, BINARY
)) {
3160 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
3161 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
3164 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
3165 diff_abbrev_oid(&one
->oid
, abbrev
),
3166 diff_abbrev_oid(&two
->oid
, abbrev
));
3167 if (one
->mode
== two
->mode
)
3168 strbuf_addf(msg
, " %06o", one
->mode
);
3169 strbuf_addf(msg
, "%s\n", reset
);
3173 static void run_diff_cmd(const char *pgm
,
3176 const char *attr_path
,
3177 struct diff_filespec
*one
,
3178 struct diff_filespec
*two
,
3180 struct diff_options
*o
,
3181 struct diff_filepair
*p
)
3183 const char *xfrm_msg
= NULL
;
3184 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
3185 int must_show_header
= 0;
3188 if (DIFF_OPT_TST(o
, ALLOW_EXTERNAL
)) {
3189 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
3190 if (drv
&& drv
->external
)
3191 pgm
= drv
->external
;
3196 * don't use colors when the header is intended for an
3197 * external diff driver
3199 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
3201 want_color(o
->use_color
) && !pgm
);
3202 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
3206 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
3207 complete_rewrite
, o
);
3211 builtin_diff(name
, other
? other
: name
,
3212 one
, two
, xfrm_msg
, must_show_header
,
3213 o
, complete_rewrite
);
3215 fprintf(o
->file
, "* Unmerged path %s\n", name
);
3218 static void diff_fill_sha1_info(struct diff_filespec
*one
)
3220 if (DIFF_FILE_VALID(one
)) {
3221 if (!one
->oid_valid
) {
3223 if (one
->is_stdin
) {
3227 if (lstat(one
->path
, &st
) < 0)
3228 die_errno("stat '%s'", one
->path
);
3229 if (index_path(one
->oid
.hash
, one
->path
, &st
, 0))
3230 die("cannot hash %s", one
->path
);
3237 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
3239 /* Strip the prefix but do not molest /dev/null and absolute paths */
3240 if (*namep
&& **namep
!= '/') {
3241 *namep
+= prefix_length
;
3245 if (*otherp
&& **otherp
!= '/') {
3246 *otherp
+= prefix_length
;
3247 if (**otherp
== '/')
3252 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
3254 const char *pgm
= external_diff();
3256 struct diff_filespec
*one
= p
->one
;
3257 struct diff_filespec
*two
= p
->two
;
3260 const char *attr_path
;
3262 name
= p
->one
->path
;
3263 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
3265 if (o
->prefix_length
)
3266 strip_prefix(o
->prefix_length
, &name
, &other
);
3268 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
3271 if (DIFF_PAIR_UNMERGED(p
)) {
3272 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
3273 NULL
, NULL
, NULL
, o
, p
);
3277 diff_fill_sha1_info(one
);
3278 diff_fill_sha1_info(two
);
3281 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
3282 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
3284 * a filepair that changes between file and symlink
3285 * needs to be split into deletion and creation.
3287 struct diff_filespec
*null
= alloc_filespec(two
->path
);
3288 run_diff_cmd(NULL
, name
, other
, attr_path
,
3289 one
, null
, &msg
, o
, p
);
3291 strbuf_release(&msg
);
3293 null
= alloc_filespec(one
->path
);
3294 run_diff_cmd(NULL
, name
, other
, attr_path
,
3295 null
, two
, &msg
, o
, p
);
3299 run_diff_cmd(pgm
, name
, other
, attr_path
,
3300 one
, two
, &msg
, o
, p
);
3302 strbuf_release(&msg
);
3305 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
3306 struct diffstat_t
*diffstat
)
3311 if (DIFF_PAIR_UNMERGED(p
)) {
3313 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, p
);
3317 name
= p
->one
->path
;
3318 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
3320 if (o
->prefix_length
)
3321 strip_prefix(o
->prefix_length
, &name
, &other
);
3323 diff_fill_sha1_info(p
->one
);
3324 diff_fill_sha1_info(p
->two
);
3326 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, p
);
3329 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
3333 const char *attr_path
;
3335 if (DIFF_PAIR_UNMERGED(p
)) {
3340 name
= p
->one
->path
;
3341 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
3342 attr_path
= other
? other
: name
;
3344 if (o
->prefix_length
)
3345 strip_prefix(o
->prefix_length
, &name
, &other
);
3347 diff_fill_sha1_info(p
->one
);
3348 diff_fill_sha1_info(p
->two
);
3350 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
3353 void diff_setup(struct diff_options
*options
)
3355 memcpy(options
, &default_diff_options
, sizeof(*options
));
3357 options
->file
= stdout
;
3359 options
->abbrev
= DEFAULT_ABBREV
;
3360 options
->line_termination
= '\n';
3361 options
->break_opt
= -1;
3362 options
->rename_limit
= -1;
3363 options
->dirstat_permille
= diff_dirstat_permille_default
;
3364 options
->context
= diff_context_default
;
3365 options
->ws_error_highlight
= ws_error_highlight_default
;
3366 DIFF_OPT_SET(options
, RENAME_EMPTY
);
3368 /* pathchange left =NULL by default */
3369 options
->change
= diff_change
;
3370 options
->add_remove
= diff_addremove
;
3371 options
->use_color
= diff_use_color_default
;
3372 options
->detect_rename
= diff_detect_rename_default
;
3373 options
->xdl_opts
|= diff_algorithm
;
3374 if (diff_indent_heuristic
)
3375 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
3377 options
->orderfile
= diff_order_file_cfg
;
3379 if (diff_no_prefix
) {
3380 options
->a_prefix
= options
->b_prefix
= "";
3381 } else if (!diff_mnemonic_prefix
) {
3382 options
->a_prefix
= "a/";
3383 options
->b_prefix
= "b/";
3387 void diff_setup_done(struct diff_options
*options
)
3391 if (options
->set_default
)
3392 options
->set_default(options
);
3394 if (options
->output_format
& DIFF_FORMAT_NAME
)
3396 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
3398 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
3400 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
3403 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
3406 * Most of the time we can say "there are changes"
3407 * only by checking if there are changed paths, but
3408 * --ignore-whitespace* options force us to look
3412 if (DIFF_XDL_TST(options
, IGNORE_WHITESPACE
) ||
3413 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_CHANGE
) ||
3414 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_AT_EOL
))
3415 DIFF_OPT_SET(options
, DIFF_FROM_CONTENTS
);
3417 DIFF_OPT_CLR(options
, DIFF_FROM_CONTENTS
);
3419 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
3420 options
->detect_rename
= DIFF_DETECT_COPY
;
3422 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
3423 options
->prefix
= NULL
;
3424 if (options
->prefix
)
3425 options
->prefix_length
= strlen(options
->prefix
);
3427 options
->prefix_length
= 0;
3429 if (options
->output_format
& (DIFF_FORMAT_NAME
|
3430 DIFF_FORMAT_NAME_STATUS
|
3431 DIFF_FORMAT_CHECKDIFF
|
3432 DIFF_FORMAT_NO_OUTPUT
))
3433 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
3434 DIFF_FORMAT_NUMSTAT
|
3435 DIFF_FORMAT_DIFFSTAT
|
3436 DIFF_FORMAT_SHORTSTAT
|
3437 DIFF_FORMAT_DIRSTAT
|
3438 DIFF_FORMAT_SUMMARY
|
3442 * These cases always need recursive; we do not drop caller-supplied
3443 * recursive bits for other formats here.
3445 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
3446 DIFF_FORMAT_NUMSTAT
|
3447 DIFF_FORMAT_DIFFSTAT
|
3448 DIFF_FORMAT_SHORTSTAT
|
3449 DIFF_FORMAT_DIRSTAT
|
3450 DIFF_FORMAT_SUMMARY
|
3451 DIFF_FORMAT_CHECKDIFF
))
3452 DIFF_OPT_SET(options
, RECURSIVE
);
3454 * Also pickaxe would not work very well if you do not say recursive
3456 if (options
->pickaxe
)
3457 DIFF_OPT_SET(options
, RECURSIVE
);
3459 * When patches are generated, submodules diffed against the work tree
3460 * must be checked for dirtiness too so it can be shown in the output
3462 if (options
->output_format
& DIFF_FORMAT_PATCH
)
3463 DIFF_OPT_SET(options
, DIRTY_SUBMODULES
);
3465 if (options
->detect_rename
&& options
->rename_limit
< 0)
3466 options
->rename_limit
= diff_rename_limit_default
;
3467 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
3469 /* read-cache does not die even when it fails
3470 * so it is safe for us to do this here. Also
3471 * it does not smudge active_cache or active_nr
3472 * when it fails, so we do not have to worry about
3473 * cleaning it up ourselves either.
3477 if (40 < options
->abbrev
)
3478 options
->abbrev
= 40; /* full */
3481 * It does not make sense to show the first hit we happened
3482 * to have found. It does not make sense not to return with
3483 * exit code in such a case either.
3485 if (DIFF_OPT_TST(options
, QUICK
)) {
3486 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
3487 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
3490 options
->diff_path_counter
= 0;
3492 if (DIFF_OPT_TST(options
, FOLLOW_RENAMES
) && options
->pathspec
.nr
!= 1)
3493 die(_("--follow requires exactly one pathspec"));
3496 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
3506 if (c
== arg_short
) {
3510 if (val
&& isdigit(c
)) {
3512 int n
= strtoul(arg
, &end
, 10);
3523 eq
= strchrnul(arg
, '=');
3525 if (!len
|| strncmp(arg
, arg_long
, len
))
3530 if (!isdigit(*++eq
))
3532 n
= strtoul(eq
, &end
, 10);
3540 static int diff_scoreopt_parse(const char *opt
);
3542 static inline int short_opt(char opt
, const char **argv
,
3543 const char **optarg
)
3545 const char *arg
= argv
[0];
3546 if (arg
[0] != '-' || arg
[1] != opt
)
3548 if (arg
[2] != '\0') {
3553 die("Option '%c' requires a value", opt
);
3558 int parse_long_opt(const char *opt
, const char **argv
,
3559 const char **optarg
)
3561 const char *arg
= argv
[0];
3562 if (!skip_prefix(arg
, "--", &arg
))
3564 if (!skip_prefix(arg
, opt
, &arg
))
3566 if (*arg
== '=') { /* stuck form: --option=value */
3572 /* separate form: --option value */
3574 die("Option '--%s' requires a value", opt
);
3579 static int stat_opt(struct diff_options
*options
, const char **av
)
3581 const char *arg
= av
[0];
3583 int width
= options
->stat_width
;
3584 int name_width
= options
->stat_name_width
;
3585 int graph_width
= options
->stat_graph_width
;
3586 int count
= options
->stat_count
;
3589 if (!skip_prefix(arg
, "--stat", &arg
))
3590 die("BUG: stat option does not begin with --stat: %s", arg
);
3595 if (skip_prefix(arg
, "-width", &arg
)) {
3597 width
= strtoul(arg
+ 1, &end
, 10);
3598 else if (!*arg
&& !av
[1])
3599 die_want_option("--stat-width");
3601 width
= strtoul(av
[1], &end
, 10);
3604 } else if (skip_prefix(arg
, "-name-width", &arg
)) {
3606 name_width
= strtoul(arg
+ 1, &end
, 10);
3607 else if (!*arg
&& !av
[1])
3608 die_want_option("--stat-name-width");
3610 name_width
= strtoul(av
[1], &end
, 10);
3613 } else if (skip_prefix(arg
, "-graph-width", &arg
)) {
3615 graph_width
= strtoul(arg
+ 1, &end
, 10);
3616 else if (!*arg
&& !av
[1])
3617 die_want_option("--stat-graph-width");
3619 graph_width
= strtoul(av
[1], &end
, 10);
3622 } else if (skip_prefix(arg
, "-count", &arg
)) {
3624 count
= strtoul(arg
+ 1, &end
, 10);
3625 else if (!*arg
&& !av
[1])
3626 die_want_option("--stat-count");
3628 count
= strtoul(av
[1], &end
, 10);
3634 width
= strtoul(arg
+1, &end
, 10);
3636 name_width
= strtoul(end
+1, &end
, 10);
3638 count
= strtoul(end
+1, &end
, 10);
3641 /* Important! This checks all the error cases! */
3644 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
3645 options
->stat_name_width
= name_width
;
3646 options
->stat_graph_width
= graph_width
;
3647 options
->stat_width
= width
;
3648 options
->stat_count
= count
;
3652 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
3654 struct strbuf errmsg
= STRBUF_INIT
;
3655 if (parse_dirstat_params(options
, params
, &errmsg
))
3656 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
3658 strbuf_release(&errmsg
);
3660 * The caller knows a dirstat-related option is given from the command
3661 * line; allow it to say "return this_function();"
3663 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
3667 static int parse_submodule_opt(struct diff_options
*options
, const char *value
)
3669 if (parse_submodule_params(options
, value
))
3670 die(_("Failed to parse --submodule option parameter: '%s'"),
3675 static const char diff_status_letters
[] = {
3678 DIFF_STATUS_DELETED
,
3679 DIFF_STATUS_MODIFIED
,
3680 DIFF_STATUS_RENAMED
,
3681 DIFF_STATUS_TYPE_CHANGED
,
3682 DIFF_STATUS_UNKNOWN
,
3683 DIFF_STATUS_UNMERGED
,
3684 DIFF_STATUS_FILTER_AON
,
3685 DIFF_STATUS_FILTER_BROKEN
,
3689 static unsigned int filter_bit
['Z' + 1];
3691 static void prepare_filter_bits(void)
3695 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
3696 for (i
= 0; diff_status_letters
[i
]; i
++)
3697 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
3701 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
3703 return opt
->filter
& filter_bit
[(int) status
];
3706 static int parse_diff_filter_opt(const char *optarg
, struct diff_options
*opt
)
3710 prepare_filter_bits();
3713 * If there is a negation e.g. 'd' in the input, and we haven't
3714 * initialized the filter field with another --diff-filter, start
3715 * from full set of bits, except for AON.
3718 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
3719 if (optch
< 'a' || 'z' < optch
)
3721 opt
->filter
= (1 << (ARRAY_SIZE(diff_status_letters
) - 1)) - 1;
3722 opt
->filter
&= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
3727 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
3731 if ('a' <= optch
&& optch
<= 'z') {
3733 optch
= toupper(optch
);
3738 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
3742 opt
->filter
&= ~bit
;
3749 static void enable_patch_output(int *fmt
) {
3750 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
3751 *fmt
|= DIFF_FORMAT_PATCH
;
3754 static int parse_ws_error_highlight_opt(struct diff_options
*opt
, const char *arg
)
3756 int val
= parse_ws_error_highlight(arg
);
3759 error("unknown value after ws-error-highlight=%.*s",
3763 opt
->ws_error_highlight
= val
;
3767 int diff_opt_parse(struct diff_options
*options
,
3768 const char **av
, int ac
, const char *prefix
)
3770 const char *arg
= av
[0];
3777 /* Output format options */
3778 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u") || !strcmp(arg
, "--patch")
3779 || opt_arg(arg
, 'U', "unified", &options
->context
))
3780 enable_patch_output(&options
->output_format
);
3781 else if (!strcmp(arg
, "--raw"))
3782 options
->output_format
|= DIFF_FORMAT_RAW
;
3783 else if (!strcmp(arg
, "--patch-with-raw")) {
3784 enable_patch_output(&options
->output_format
);
3785 options
->output_format
|= DIFF_FORMAT_RAW
;
3786 } else if (!strcmp(arg
, "--numstat"))
3787 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
3788 else if (!strcmp(arg
, "--shortstat"))
3789 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
3790 else if (!strcmp(arg
, "-X") || !strcmp(arg
, "--dirstat"))
3791 return parse_dirstat_opt(options
, "");
3792 else if (skip_prefix(arg
, "-X", &arg
))
3793 return parse_dirstat_opt(options
, arg
);
3794 else if (skip_prefix(arg
, "--dirstat=", &arg
))
3795 return parse_dirstat_opt(options
, arg
);
3796 else if (!strcmp(arg
, "--cumulative"))
3797 return parse_dirstat_opt(options
, "cumulative");
3798 else if (!strcmp(arg
, "--dirstat-by-file"))
3799 return parse_dirstat_opt(options
, "files");
3800 else if (skip_prefix(arg
, "--dirstat-by-file=", &arg
)) {
3801 parse_dirstat_opt(options
, "files");
3802 return parse_dirstat_opt(options
, arg
);
3804 else if (!strcmp(arg
, "--check"))
3805 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
3806 else if (!strcmp(arg
, "--summary"))
3807 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
3808 else if (!strcmp(arg
, "--patch-with-stat")) {
3809 enable_patch_output(&options
->output_format
);
3810 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
3811 } else if (!strcmp(arg
, "--name-only"))
3812 options
->output_format
|= DIFF_FORMAT_NAME
;
3813 else if (!strcmp(arg
, "--name-status"))
3814 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
3815 else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--no-patch"))
3816 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
3817 else if (starts_with(arg
, "--stat"))
3818 /* --stat, --stat-width, --stat-name-width, or --stat-count */
3819 return stat_opt(options
, av
);
3821 /* renames options */
3822 else if (starts_with(arg
, "-B") || starts_with(arg
, "--break-rewrites=") ||
3823 !strcmp(arg
, "--break-rewrites")) {
3824 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
3825 return error("invalid argument to -B: %s", arg
+2);
3827 else if (starts_with(arg
, "-M") || starts_with(arg
, "--find-renames=") ||
3828 !strcmp(arg
, "--find-renames")) {
3829 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
3830 return error("invalid argument to -M: %s", arg
+2);
3831 options
->detect_rename
= DIFF_DETECT_RENAME
;
3833 else if (!strcmp(arg
, "-D") || !strcmp(arg
, "--irreversible-delete")) {
3834 options
->irreversible_delete
= 1;
3836 else if (starts_with(arg
, "-C") || starts_with(arg
, "--find-copies=") ||
3837 !strcmp(arg
, "--find-copies")) {
3838 if (options
->detect_rename
== DIFF_DETECT_COPY
)
3839 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
3840 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
3841 return error("invalid argument to -C: %s", arg
+2);
3842 options
->detect_rename
= DIFF_DETECT_COPY
;
3844 else if (!strcmp(arg
, "--no-renames"))
3845 options
->detect_rename
= 0;
3846 else if (!strcmp(arg
, "--rename-empty"))
3847 DIFF_OPT_SET(options
, RENAME_EMPTY
);
3848 else if (!strcmp(arg
, "--no-rename-empty"))
3849 DIFF_OPT_CLR(options
, RENAME_EMPTY
);
3850 else if (!strcmp(arg
, "--relative"))
3851 DIFF_OPT_SET(options
, RELATIVE_NAME
);
3852 else if (skip_prefix(arg
, "--relative=", &arg
)) {
3853 DIFF_OPT_SET(options
, RELATIVE_NAME
);
3854 options
->prefix
= arg
;
3858 else if (!strcmp(arg
, "--minimal"))
3859 DIFF_XDL_SET(options
, NEED_MINIMAL
);
3860 else if (!strcmp(arg
, "--no-minimal"))
3861 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
3862 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
3863 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
3864 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
3865 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
3866 else if (!strcmp(arg
, "--ignore-space-at-eol"))
3867 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
3868 else if (!strcmp(arg
, "--ignore-blank-lines"))
3869 DIFF_XDL_SET(options
, IGNORE_BLANK_LINES
);
3870 else if (!strcmp(arg
, "--indent-heuristic"))
3871 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
3872 else if (!strcmp(arg
, "--no-indent-heuristic"))
3873 DIFF_XDL_CLR(options
, INDENT_HEURISTIC
);
3874 else if (!strcmp(arg
, "--patience"))
3875 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
3876 else if (!strcmp(arg
, "--histogram"))
3877 options
->xdl_opts
= DIFF_WITH_ALG(options
, HISTOGRAM_DIFF
);
3878 else if ((argcount
= parse_long_opt("diff-algorithm", av
, &optarg
))) {
3879 long value
= parse_algorithm_value(optarg
);
3881 return error("option diff-algorithm accepts \"myers\", "
3882 "\"minimal\", \"patience\" and \"histogram\"");
3883 /* clear out previous settings */
3884 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
3885 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3886 options
->xdl_opts
|= value
;
3891 else if (!strcmp(arg
, "--binary")) {
3892 enable_patch_output(&options
->output_format
);
3893 DIFF_OPT_SET(options
, BINARY
);
3895 else if (!strcmp(arg
, "--full-index"))
3896 DIFF_OPT_SET(options
, FULL_INDEX
);
3897 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
3898 DIFF_OPT_SET(options
, TEXT
);
3899 else if (!strcmp(arg
, "-R"))
3900 DIFF_OPT_SET(options
, REVERSE_DIFF
);
3901 else if (!strcmp(arg
, "--find-copies-harder"))
3902 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
3903 else if (!strcmp(arg
, "--follow"))
3904 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
3905 else if (!strcmp(arg
, "--no-follow")) {
3906 DIFF_OPT_CLR(options
, FOLLOW_RENAMES
);
3907 DIFF_OPT_CLR(options
, DEFAULT_FOLLOW_RENAMES
);
3908 } else if (!strcmp(arg
, "--color"))
3909 options
->use_color
= 1;
3910 else if (skip_prefix(arg
, "--color=", &arg
)) {
3911 int value
= git_config_colorbool(NULL
, arg
);
3913 return error("option `color' expects \"always\", \"auto\", or \"never\"");
3914 options
->use_color
= value
;
3916 else if (!strcmp(arg
, "--no-color"))
3917 options
->use_color
= 0;
3918 else if (!strcmp(arg
, "--color-words")) {
3919 options
->use_color
= 1;
3920 options
->word_diff
= DIFF_WORDS_COLOR
;
3922 else if (skip_prefix(arg
, "--color-words=", &arg
)) {
3923 options
->use_color
= 1;
3924 options
->word_diff
= DIFF_WORDS_COLOR
;
3925 options
->word_regex
= arg
;
3927 else if (!strcmp(arg
, "--word-diff")) {
3928 if (options
->word_diff
== DIFF_WORDS_NONE
)
3929 options
->word_diff
= DIFF_WORDS_PLAIN
;
3931 else if (skip_prefix(arg
, "--word-diff=", &arg
)) {
3932 if (!strcmp(arg
, "plain"))
3933 options
->word_diff
= DIFF_WORDS_PLAIN
;
3934 else if (!strcmp(arg
, "color")) {
3935 options
->use_color
= 1;
3936 options
->word_diff
= DIFF_WORDS_COLOR
;
3938 else if (!strcmp(arg
, "porcelain"))
3939 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
3940 else if (!strcmp(arg
, "none"))
3941 options
->word_diff
= DIFF_WORDS_NONE
;
3943 die("bad --word-diff argument: %s", arg
);
3945 else if ((argcount
= parse_long_opt("word-diff-regex", av
, &optarg
))) {
3946 if (options
->word_diff
== DIFF_WORDS_NONE
)
3947 options
->word_diff
= DIFF_WORDS_PLAIN
;
3948 options
->word_regex
= optarg
;
3951 else if (!strcmp(arg
, "--exit-code"))
3952 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
3953 else if (!strcmp(arg
, "--quiet"))
3954 DIFF_OPT_SET(options
, QUICK
);
3955 else if (!strcmp(arg
, "--ext-diff"))
3956 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
3957 else if (!strcmp(arg
, "--no-ext-diff"))
3958 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
3959 else if (!strcmp(arg
, "--textconv"))
3960 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
3961 else if (!strcmp(arg
, "--no-textconv"))
3962 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
3963 else if (!strcmp(arg
, "--ignore-submodules")) {
3964 DIFF_OPT_SET(options
, OVERRIDE_SUBMODULE_CONFIG
);
3965 handle_ignore_submodules_arg(options
, "all");
3966 } else if (skip_prefix(arg
, "--ignore-submodules=", &arg
)) {
3967 DIFF_OPT_SET(options
, OVERRIDE_SUBMODULE_CONFIG
);
3968 handle_ignore_submodules_arg(options
, arg
);
3969 } else if (!strcmp(arg
, "--submodule"))
3970 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
3971 else if (skip_prefix(arg
, "--submodule=", &arg
))
3972 return parse_submodule_opt(options
, arg
);
3973 else if (skip_prefix(arg
, "--ws-error-highlight=", &arg
))
3974 return parse_ws_error_highlight_opt(options
, arg
);
3975 else if (!strcmp(arg
, "--ita-invisible-in-index"))
3976 options
->ita_invisible_in_index
= 1;
3977 else if (!strcmp(arg
, "--ita-visible-in-index"))
3978 options
->ita_invisible_in_index
= 0;
3981 else if (!strcmp(arg
, "-z"))
3982 options
->line_termination
= 0;
3983 else if ((argcount
= short_opt('l', av
, &optarg
))) {
3984 options
->rename_limit
= strtoul(optarg
, NULL
, 10);
3987 else if ((argcount
= short_opt('S', av
, &optarg
))) {
3988 options
->pickaxe
= optarg
;
3989 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
3991 } else if ((argcount
= short_opt('G', av
, &optarg
))) {
3992 options
->pickaxe
= optarg
;
3993 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
3996 else if (!strcmp(arg
, "--pickaxe-all"))
3997 options
->pickaxe_opts
|= DIFF_PICKAXE_ALL
;
3998 else if (!strcmp(arg
, "--pickaxe-regex"))
3999 options
->pickaxe_opts
|= DIFF_PICKAXE_REGEX
;
4000 else if ((argcount
= short_opt('O', av
, &optarg
))) {
4001 const char *path
= prefix_filename(prefix
, strlen(prefix
), optarg
);
4002 options
->orderfile
= xstrdup(path
);
4005 else if ((argcount
= parse_long_opt("diff-filter", av
, &optarg
))) {
4006 int offending
= parse_diff_filter_opt(optarg
, options
);
4008 die("unknown change class '%c' in --diff-filter=%s",
4012 else if (!strcmp(arg
, "--no-abbrev"))
4013 options
->abbrev
= 0;
4014 else if (!strcmp(arg
, "--abbrev"))
4015 options
->abbrev
= DEFAULT_ABBREV
;
4016 else if (skip_prefix(arg
, "--abbrev=", &arg
)) {
4017 options
->abbrev
= strtoul(arg
, NULL
, 10);
4018 if (options
->abbrev
< MINIMUM_ABBREV
)
4019 options
->abbrev
= MINIMUM_ABBREV
;
4020 else if (40 < options
->abbrev
)
4021 options
->abbrev
= 40;
4023 else if ((argcount
= parse_long_opt("src-prefix", av
, &optarg
))) {
4024 options
->a_prefix
= optarg
;
4027 else if ((argcount
= parse_long_opt("line-prefix", av
, &optarg
))) {
4028 options
->line_prefix
= optarg
;
4029 options
->line_prefix_length
= strlen(options
->line_prefix
);
4030 graph_setup_line_prefix(options
);
4033 else if ((argcount
= parse_long_opt("dst-prefix", av
, &optarg
))) {
4034 options
->b_prefix
= optarg
;
4037 else if (!strcmp(arg
, "--no-prefix"))
4038 options
->a_prefix
= options
->b_prefix
= "";
4039 else if (opt_arg(arg
, '\0', "inter-hunk-context",
4040 &options
->interhunkcontext
))
4042 else if (!strcmp(arg
, "-W"))
4043 DIFF_OPT_SET(options
, FUNCCONTEXT
);
4044 else if (!strcmp(arg
, "--function-context"))
4045 DIFF_OPT_SET(options
, FUNCCONTEXT
);
4046 else if (!strcmp(arg
, "--no-function-context"))
4047 DIFF_OPT_CLR(options
, FUNCCONTEXT
);
4048 else if ((argcount
= parse_long_opt("output", av
, &optarg
))) {
4049 const char *path
= prefix_filename(prefix
, strlen(prefix
), optarg
);
4050 options
->file
= fopen(path
, "w");
4052 die_errno("Could not open '%s'", path
);
4053 options
->close_file
= 1;
4054 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
4055 options
->use_color
= GIT_COLOR_NEVER
;
4062 int parse_rename_score(const char **cp_p
)
4064 unsigned long num
, scale
;
4066 const char *cp
= *cp_p
;
4073 if ( !dot
&& ch
== '.' ) {
4076 } else if ( ch
== '%' ) {
4077 scale
= dot
? scale
*100 : 100;
4078 cp
++; /* % is always at the end */
4080 } else if ( ch
>= '0' && ch
<= '9' ) {
4081 if ( scale
< 100000 ) {
4083 num
= (num
*10) + (ch
-'0');
4092 /* user says num divided by scale and we say internally that
4093 * is MAX_SCORE * num / scale.
4095 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
4098 static int diff_scoreopt_parse(const char *opt
)
4100 int opt1
, opt2
, cmd
;
4106 /* convert the long-form arguments into short-form versions */
4107 if (skip_prefix(opt
, "break-rewrites", &opt
)) {
4108 if (*opt
== 0 || *opt
++ == '=')
4110 } else if (skip_prefix(opt
, "find-copies", &opt
)) {
4111 if (*opt
== 0 || *opt
++ == '=')
4113 } else if (skip_prefix(opt
, "find-renames", &opt
)) {
4114 if (*opt
== 0 || *opt
++ == '=')
4118 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
4119 return -1; /* that is not a -M, -C, or -B option */
4121 opt1
= parse_rename_score(&opt
);
4127 else if (*opt
!= '/')
4128 return -1; /* we expect -B80/99 or -B80 */
4131 opt2
= parse_rename_score(&opt
);
4136 return opt1
| (opt2
<< 16);
4139 struct diff_queue_struct diff_queued_diff
;
4141 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
4143 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
4144 queue
->queue
[queue
->nr
++] = dp
;
4147 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
4148 struct diff_filespec
*one
,
4149 struct diff_filespec
*two
)
4151 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
4159 void diff_free_filepair(struct diff_filepair
*p
)
4161 free_filespec(p
->one
);
4162 free_filespec(p
->two
);
4166 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
4171 if (len
== GIT_SHA1_HEXSZ
)
4172 return oid_to_hex(oid
);
4174 abbrev
= diff_abbrev_oid(oid
, len
);
4175 abblen
= strlen(abbrev
);
4178 * In well-behaved cases, where the abbbreviated result is the
4179 * same as the requested length, append three dots after the
4180 * abbreviation (hence the whole logic is limited to the case
4181 * where abblen < 37); when the actual abbreviated result is a
4182 * bit longer than the requested length, we reduce the number
4183 * of dots so that they match the well-behaved ones. However,
4184 * if the actual abbreviation is longer than the requested
4185 * length by more than three, we give up on aligning, and add
4186 * three dots anyway, to indicate that the output is not the
4187 * full object name. Yes, this may be suboptimal, but this
4188 * appears only in "diff --raw --abbrev" output and it is not
4189 * worth the effort to change it now. Note that this would
4190 * likely to work fine when the automatic sizing of default
4191 * abbreviation length is used--we would be fed -1 in "len" in
4192 * that case, and will end up always appending three-dots, but
4193 * the automatic sizing is supposed to give abblen that ensures
4194 * uniqueness across all objects (statistically speaking).
4196 if (abblen
< GIT_SHA1_HEXSZ
- 3) {
4197 static char hex
[GIT_SHA1_HEXSZ
+ 1];
4198 if (len
< abblen
&& abblen
<= len
+ 2)
4199 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
4201 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
4205 return oid_to_hex(oid
);
4208 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
4210 int line_termination
= opt
->line_termination
;
4211 int inter_name_termination
= line_termination
? '\t' : '\0';
4213 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
4214 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
4215 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
4216 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
4217 fprintf(opt
->file
, "%s ",
4218 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
4221 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
4222 inter_name_termination
);
4224 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
4227 if (p
->status
== DIFF_STATUS_COPIED
||
4228 p
->status
== DIFF_STATUS_RENAMED
) {
4229 const char *name_a
, *name_b
;
4230 name_a
= p
->one
->path
;
4231 name_b
= p
->two
->path
;
4232 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4233 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
4234 write_name_quoted(name_b
, opt
->file
, line_termination
);
4236 const char *name_a
, *name_b
;
4237 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
4239 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4240 write_name_quoted(name_a
, opt
->file
, line_termination
);
4244 int diff_unmodified_pair(struct diff_filepair
*p
)
4246 /* This function is written stricter than necessary to support
4247 * the currently implemented transformers, but the idea is to
4248 * let transformers to produce diff_filepairs any way they want,
4249 * and filter and clean them up here before producing the output.
4251 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
4253 if (DIFF_PAIR_UNMERGED(p
))
4254 return 0; /* unmerged is interesting */
4256 /* deletion, addition, mode or type change
4257 * and rename are all interesting.
4259 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
4260 DIFF_PAIR_MODE_CHANGED(p
) ||
4261 strcmp(one
->path
, two
->path
))
4264 /* both are valid and point at the same path. that is, we are
4265 * dealing with a change.
4267 if (one
->oid_valid
&& two
->oid_valid
&&
4268 !oidcmp(&one
->oid
, &two
->oid
) &&
4269 !one
->dirty_submodule
&& !two
->dirty_submodule
)
4270 return 1; /* no change */
4271 if (!one
->oid_valid
&& !two
->oid_valid
)
4272 return 1; /* both look at the same file on the filesystem. */
4276 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
4278 if (diff_unmodified_pair(p
))
4281 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4282 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4283 return; /* no tree diffs in patch format */
4288 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
4289 struct diffstat_t
*diffstat
)
4291 if (diff_unmodified_pair(p
))
4294 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4295 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4296 return; /* no useful stat for tree diffs */
4298 run_diffstat(p
, o
, diffstat
);
4301 static void diff_flush_checkdiff(struct diff_filepair
*p
,
4302 struct diff_options
*o
)
4304 if (diff_unmodified_pair(p
))
4307 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4308 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4309 return; /* nothing to check in tree diffs */
4311 run_checkdiff(p
, o
);
4314 int diff_queue_is_empty(void)
4316 struct diff_queue_struct
*q
= &diff_queued_diff
;
4318 for (i
= 0; i
< q
->nr
; i
++)
4319 if (!diff_unmodified_pair(q
->queue
[i
]))
4325 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
4327 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
4330 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
4332 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
4333 fprintf(stderr
, "queue[%d] %s size %lu\n",
4338 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
4340 diff_debug_filespec(p
->one
, i
, "one");
4341 diff_debug_filespec(p
->two
, i
, "two");
4342 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
4343 p
->score
, p
->status
? p
->status
: '?',
4344 p
->one
->rename_used
, p
->broken_pair
);
4347 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
4351 fprintf(stderr
, "%s\n", msg
);
4352 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
4353 for (i
= 0; i
< q
->nr
; i
++) {
4354 struct diff_filepair
*p
= q
->queue
[i
];
4355 diff_debug_filepair(p
, i
);
4360 static void diff_resolve_rename_copy(void)
4363 struct diff_filepair
*p
;
4364 struct diff_queue_struct
*q
= &diff_queued_diff
;
4366 diff_debug_queue("resolve-rename-copy", q
);
4368 for (i
= 0; i
< q
->nr
; i
++) {
4370 p
->status
= 0; /* undecided */
4371 if (DIFF_PAIR_UNMERGED(p
))
4372 p
->status
= DIFF_STATUS_UNMERGED
;
4373 else if (!DIFF_FILE_VALID(p
->one
))
4374 p
->status
= DIFF_STATUS_ADDED
;
4375 else if (!DIFF_FILE_VALID(p
->two
))
4376 p
->status
= DIFF_STATUS_DELETED
;
4377 else if (DIFF_PAIR_TYPE_CHANGED(p
))
4378 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
4380 /* from this point on, we are dealing with a pair
4381 * whose both sides are valid and of the same type, i.e.
4382 * either in-place edit or rename/copy edit.
4384 else if (DIFF_PAIR_RENAME(p
)) {
4386 * A rename might have re-connected a broken
4387 * pair up, causing the pathnames to be the
4388 * same again. If so, that's not a rename at
4389 * all, just a modification..
4391 * Otherwise, see if this source was used for
4392 * multiple renames, in which case we decrement
4393 * the count, and call it a copy.
4395 if (!strcmp(p
->one
->path
, p
->two
->path
))
4396 p
->status
= DIFF_STATUS_MODIFIED
;
4397 else if (--p
->one
->rename_used
> 0)
4398 p
->status
= DIFF_STATUS_COPIED
;
4400 p
->status
= DIFF_STATUS_RENAMED
;
4402 else if (oidcmp(&p
->one
->oid
, &p
->two
->oid
) ||
4403 p
->one
->mode
!= p
->two
->mode
||
4404 p
->one
->dirty_submodule
||
4405 p
->two
->dirty_submodule
||
4406 is_null_oid(&p
->one
->oid
))
4407 p
->status
= DIFF_STATUS_MODIFIED
;
4409 /* This is a "no-change" entry and should not
4410 * happen anymore, but prepare for broken callers.
4412 error("feeding unmodified %s to diffcore",
4414 p
->status
= DIFF_STATUS_UNKNOWN
;
4417 diff_debug_queue("resolve-rename-copy done", q
);
4420 static int check_pair_status(struct diff_filepair
*p
)
4422 switch (p
->status
) {
4423 case DIFF_STATUS_UNKNOWN
:
4426 die("internal error in diff-resolve-rename-copy");
4432 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
4434 int fmt
= opt
->output_format
;
4436 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
4437 diff_flush_checkdiff(p
, opt
);
4438 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
4439 diff_flush_raw(p
, opt
);
4440 else if (fmt
& DIFF_FORMAT_NAME
) {
4441 const char *name_a
, *name_b
;
4442 name_a
= p
->two
->path
;
4444 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
4445 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
4449 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
4452 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
4454 fprintf(file
, " %s ", newdelete
);
4455 write_name_quoted(fs
->path
, file
, '\n');
4459 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
,
4460 const char *line_prefix
)
4462 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
4463 fprintf(file
, "%s mode change %06o => %06o%c", line_prefix
, p
->one
->mode
,
4464 p
->two
->mode
, show_name
? ' ' : '\n');
4466 write_name_quoted(p
->two
->path
, file
, '\n');
4471 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
,
4472 const char *line_prefix
)
4474 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
4476 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
4478 show_mode_change(file
, p
, 0, line_prefix
);
4481 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
4483 FILE *file
= opt
->file
;
4484 const char *line_prefix
= diff_line_prefix(opt
);
4487 case DIFF_STATUS_DELETED
:
4488 fputs(line_prefix
, file
);
4489 show_file_mode_name(file
, "delete", p
->one
);
4491 case DIFF_STATUS_ADDED
:
4492 fputs(line_prefix
, file
);
4493 show_file_mode_name(file
, "create", p
->two
);
4495 case DIFF_STATUS_COPIED
:
4496 fputs(line_prefix
, file
);
4497 show_rename_copy(file
, "copy", p
, line_prefix
);
4499 case DIFF_STATUS_RENAMED
:
4500 fputs(line_prefix
, file
);
4501 show_rename_copy(file
, "rename", p
, line_prefix
);
4505 fprintf(file
, "%s rewrite ", line_prefix
);
4506 write_name_quoted(p
->two
->path
, file
, ' ');
4507 fprintf(file
, "(%d%%)\n", similarity_index(p
));
4509 show_mode_change(file
, p
, !p
->score
, line_prefix
);
4519 static int remove_space(char *line
, int len
)
4525 for (i
= 0; i
< len
; i
++)
4526 if (!isspace((c
= line
[i
])))
4532 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
4534 struct patch_id_t
*data
= priv
;
4537 /* Ignore line numbers when computing the SHA1 of the patch */
4538 if (starts_with(line
, "@@ -"))
4541 new_len
= remove_space(line
, len
);
4543 git_SHA1_Update(data
->ctx
, line
, new_len
);
4544 data
->patchlen
+= new_len
;
4547 /* returns 0 upon success, and writes result into sha1 */
4548 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
, int diff_header_only
)
4550 struct diff_queue_struct
*q
= &diff_queued_diff
;
4553 struct patch_id_t data
;
4554 char buffer
[PATH_MAX
* 4 + 20];
4556 git_SHA1_Init(&ctx
);
4557 memset(&data
, 0, sizeof(struct patch_id_t
));
4560 for (i
= 0; i
< q
->nr
; i
++) {
4564 struct diff_filepair
*p
= q
->queue
[i
];
4567 memset(&xpp
, 0, sizeof(xpp
));
4568 memset(&xecfg
, 0, sizeof(xecfg
));
4570 return error("internal diff status error");
4571 if (p
->status
== DIFF_STATUS_UNKNOWN
)
4573 if (diff_unmodified_pair(p
))
4575 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
4576 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
4578 if (DIFF_PAIR_UNMERGED(p
))
4581 diff_fill_sha1_info(p
->one
);
4582 diff_fill_sha1_info(p
->two
);
4584 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
4585 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
4586 if (p
->one
->mode
== 0)
4587 len1
= snprintf(buffer
, sizeof(buffer
),
4588 "diff--gita/%.*sb/%.*s"
4595 len2
, p
->two
->path
);
4596 else if (p
->two
->mode
== 0)
4597 len1
= snprintf(buffer
, sizeof(buffer
),
4598 "diff--gita/%.*sb/%.*s"
4599 "deletedfilemode%06o"
4605 len1
, p
->one
->path
);
4607 len1
= snprintf(buffer
, sizeof(buffer
),
4608 "diff--gita/%.*sb/%.*s"
4614 len2
, p
->two
->path
);
4615 git_SHA1_Update(&ctx
, buffer
, len1
);
4617 if (diff_header_only
)
4620 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
4621 fill_mmfile(&mf2
, p
->two
) < 0)
4622 return error("unable to read files to diff");
4624 if (diff_filespec_is_binary(p
->one
) ||
4625 diff_filespec_is_binary(p
->two
)) {
4626 git_SHA1_Update(&ctx
, oid_to_hex(&p
->one
->oid
),
4628 git_SHA1_Update(&ctx
, oid_to_hex(&p
->two
->oid
),
4636 if (xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
4638 return error("unable to generate patch-id diff for %s",
4642 git_SHA1_Final(sha1
, &ctx
);
4646 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
, int diff_header_only
)
4648 struct diff_queue_struct
*q
= &diff_queued_diff
;
4650 int result
= diff_get_patch_id(options
, sha1
, diff_header_only
);
4652 for (i
= 0; i
< q
->nr
; i
++)
4653 diff_free_filepair(q
->queue
[i
]);
4656 DIFF_QUEUE_CLEAR(q
);
4661 static int is_summary_empty(const struct diff_queue_struct
*q
)
4665 for (i
= 0; i
< q
->nr
; i
++) {
4666 const struct diff_filepair
*p
= q
->queue
[i
];
4668 switch (p
->status
) {
4669 case DIFF_STATUS_DELETED
:
4670 case DIFF_STATUS_ADDED
:
4671 case DIFF_STATUS_COPIED
:
4672 case DIFF_STATUS_RENAMED
:
4677 if (p
->one
->mode
&& p
->two
->mode
&&
4678 p
->one
->mode
!= p
->two
->mode
)
4686 static const char rename_limit_warning
[] =
4687 N_("inexact rename detection was skipped due to too many files.");
4689 static const char degrade_cc_to_c_warning
[] =
4690 N_("only found copies from modified paths due to too many files.");
4692 static const char rename_limit_advice
[] =
4693 N_("you may want to set your %s variable to at least "
4694 "%d and retry the command.");
4696 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
4699 warning(_(degrade_cc_to_c_warning
));
4701 warning(_(rename_limit_warning
));
4704 if (0 < needed
&& needed
< 32767)
4705 warning(_(rename_limit_advice
), varname
, needed
);
4708 void diff_flush(struct diff_options
*options
)
4710 struct diff_queue_struct
*q
= &diff_queued_diff
;
4711 int i
, output_format
= options
->output_format
;
4713 int dirstat_by_line
= 0;
4716 * Order: raw, stat, summary, patch
4717 * or: name/name-status/checkdiff (other bits clear)
4722 if (output_format
& (DIFF_FORMAT_RAW
|
4724 DIFF_FORMAT_NAME_STATUS
|
4725 DIFF_FORMAT_CHECKDIFF
)) {
4726 for (i
= 0; i
< q
->nr
; i
++) {
4727 struct diff_filepair
*p
= q
->queue
[i
];
4728 if (check_pair_status(p
))
4729 flush_one_pair(p
, options
);
4734 if (output_format
& DIFF_FORMAT_DIRSTAT
&& DIFF_OPT_TST(options
, DIRSTAT_BY_LINE
))
4735 dirstat_by_line
= 1;
4737 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
4739 struct diffstat_t diffstat
;
4741 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
4742 for (i
= 0; i
< q
->nr
; i
++) {
4743 struct diff_filepair
*p
= q
->queue
[i
];
4744 if (check_pair_status(p
))
4745 diff_flush_stat(p
, options
, &diffstat
);
4747 if (output_format
& DIFF_FORMAT_NUMSTAT
)
4748 show_numstat(&diffstat
, options
);
4749 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
4750 show_stats(&diffstat
, options
);
4751 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
4752 show_shortstats(&diffstat
, options
);
4753 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
4754 show_dirstat_by_line(&diffstat
, options
);
4755 free_diffstat_info(&diffstat
);
4758 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
4759 show_dirstat(options
);
4761 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
4762 for (i
= 0; i
< q
->nr
; i
++) {
4763 diff_summary(options
, q
->queue
[i
]);
4768 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
4769 DIFF_OPT_TST(options
, EXIT_WITH_STATUS
) &&
4770 DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
4772 * run diff_flush_patch for the exit status. setting
4773 * options->file to /dev/null should be safe, because we
4774 * aren't supposed to produce any output anyway.
4776 if (options
->close_file
)
4777 fclose(options
->file
);
4778 options
->file
= fopen("/dev/null", "w");
4780 die_errno("Could not open /dev/null");
4781 options
->close_file
= 1;
4782 for (i
= 0; i
< q
->nr
; i
++) {
4783 struct diff_filepair
*p
= q
->queue
[i
];
4784 if (check_pair_status(p
))
4785 diff_flush_patch(p
, options
);
4786 if (options
->found_changes
)
4791 if (output_format
& DIFF_FORMAT_PATCH
) {
4793 fprintf(options
->file
, "%s%c",
4794 diff_line_prefix(options
),
4795 options
->line_termination
);
4796 if (options
->stat_sep
) {
4797 /* attach patch instead of inline */
4798 fputs(options
->stat_sep
, options
->file
);
4802 for (i
= 0; i
< q
->nr
; i
++) {
4803 struct diff_filepair
*p
= q
->queue
[i
];
4804 if (check_pair_status(p
))
4805 diff_flush_patch(p
, options
);
4809 if (output_format
& DIFF_FORMAT_CALLBACK
)
4810 options
->format_callback(q
, options
, options
->format_callback_data
);
4812 for (i
= 0; i
< q
->nr
; i
++)
4813 diff_free_filepair(q
->queue
[i
]);
4816 DIFF_QUEUE_CLEAR(q
);
4817 if (options
->close_file
)
4818 fclose(options
->file
);
4821 * Report the content-level differences with HAS_CHANGES;
4822 * diff_addremove/diff_change does not set the bit when
4823 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
4825 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
4826 if (options
->found_changes
)
4827 DIFF_OPT_SET(options
, HAS_CHANGES
);
4829 DIFF_OPT_CLR(options
, HAS_CHANGES
);
4833 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
4835 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
4837 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
4839 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
4840 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
4841 filter_bit_tst(p
->status
, options
)));
4844 static void diffcore_apply_filter(struct diff_options
*options
)
4847 struct diff_queue_struct
*q
= &diff_queued_diff
;
4848 struct diff_queue_struct outq
;
4850 DIFF_QUEUE_CLEAR(&outq
);
4852 if (!options
->filter
)
4855 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
4857 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
4858 if (match_filter(options
, q
->queue
[i
]))
4864 /* otherwise we will clear the whole queue
4865 * by copying the empty outq at the end of this
4866 * function, but first clear the current entries
4869 for (i
= 0; i
< q
->nr
; i
++)
4870 diff_free_filepair(q
->queue
[i
]);
4873 /* Only the matching ones */
4874 for (i
= 0; i
< q
->nr
; i
++) {
4875 struct diff_filepair
*p
= q
->queue
[i
];
4876 if (match_filter(options
, p
))
4879 diff_free_filepair(p
);
4886 /* Check whether two filespecs with the same mode and size are identical */
4887 static int diff_filespec_is_identical(struct diff_filespec
*one
,
4888 struct diff_filespec
*two
)
4890 if (S_ISGITLINK(one
->mode
))
4892 if (diff_populate_filespec(one
, 0))
4894 if (diff_populate_filespec(two
, 0))
4896 return !memcmp(one
->data
, two
->data
, one
->size
);
4899 static int diff_filespec_check_stat_unmatch(struct diff_filepair
*p
)
4901 if (p
->done_skip_stat_unmatch
)
4902 return p
->skip_stat_unmatch_result
;
4904 p
->done_skip_stat_unmatch
= 1;
4905 p
->skip_stat_unmatch_result
= 0;
4907 * 1. Entries that come from stat info dirtiness
4908 * always have both sides (iow, not create/delete),
4909 * one side of the object name is unknown, with
4910 * the same mode and size. Keep the ones that
4911 * do not match these criteria. They have real
4914 * 2. At this point, the file is known to be modified,
4915 * with the same mode and size, and the object
4916 * name of one side is unknown. Need to inspect
4917 * the identical contents.
4919 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
4920 !DIFF_FILE_VALID(p
->two
) ||
4921 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
4922 (p
->one
->mode
!= p
->two
->mode
) ||
4923 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
) ||
4924 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
) ||
4925 (p
->one
->size
!= p
->two
->size
) ||
4926 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
4927 p
->skip_stat_unmatch_result
= 1;
4928 return p
->skip_stat_unmatch_result
;
4931 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
4934 struct diff_queue_struct
*q
= &diff_queued_diff
;
4935 struct diff_queue_struct outq
;
4936 DIFF_QUEUE_CLEAR(&outq
);
4938 for (i
= 0; i
< q
->nr
; i
++) {
4939 struct diff_filepair
*p
= q
->queue
[i
];
4941 if (diff_filespec_check_stat_unmatch(p
))
4945 * The caller can subtract 1 from skip_stat_unmatch
4946 * to determine how many paths were dirty only
4947 * due to stat info mismatch.
4949 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
4950 diffopt
->skip_stat_unmatch
++;
4951 diff_free_filepair(p
);
4958 static int diffnamecmp(const void *a_
, const void *b_
)
4960 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
4961 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
4962 const char *name_a
, *name_b
;
4964 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
4965 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
4966 return strcmp(name_a
, name_b
);
4969 void diffcore_fix_diff_index(struct diff_options
*options
)
4971 struct diff_queue_struct
*q
= &diff_queued_diff
;
4972 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
4975 void diffcore_std(struct diff_options
*options
)
4977 /* NOTE please keep the following in sync with diff_tree_combined() */
4978 if (options
->skip_stat_unmatch
)
4979 diffcore_skip_stat_unmatch(options
);
4980 if (!options
->found_follow
) {
4981 /* See try_to_follow_renames() in tree-diff.c */
4982 if (options
->break_opt
!= -1)
4983 diffcore_break(options
->break_opt
);
4984 if (options
->detect_rename
)
4985 diffcore_rename(options
);
4986 if (options
->break_opt
!= -1)
4987 diffcore_merge_broken();
4989 if (options
->pickaxe
)
4990 diffcore_pickaxe(options
);
4991 if (options
->orderfile
)
4992 diffcore_order(options
->orderfile
);
4993 if (!options
->found_follow
)
4994 /* See try_to_follow_renames() in tree-diff.c */
4995 diff_resolve_rename_copy();
4996 diffcore_apply_filter(options
);
4998 if (diff_queued_diff
.nr
&& !DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
4999 DIFF_OPT_SET(options
, HAS_CHANGES
);
5001 DIFF_OPT_CLR(options
, HAS_CHANGES
);
5003 options
->found_follow
= 0;
5006 int diff_result_code(struct diff_options
*opt
, int status
)
5010 diff_warn_rename_limit("diff.renameLimit",
5011 opt
->needed_rename_limit
,
5012 opt
->degraded_cc_to_c
);
5013 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
5014 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
5016 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
5017 DIFF_OPT_TST(opt
, HAS_CHANGES
))
5019 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
5020 DIFF_OPT_TST(opt
, CHECK_FAILED
))
5025 int diff_can_quit_early(struct diff_options
*opt
)
5027 return (DIFF_OPT_TST(opt
, QUICK
) &&
5029 DIFF_OPT_TST(opt
, HAS_CHANGES
));
5033 * Shall changes to this submodule be ignored?
5035 * Submodule changes can be configured to be ignored separately for each path,
5036 * but that configuration can be overridden from the command line.
5038 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
5041 unsigned orig_flags
= options
->flags
;
5042 if (!DIFF_OPT_TST(options
, OVERRIDE_SUBMODULE_CONFIG
))
5043 set_diffopt_flags_from_submodule_config(options
, path
);
5044 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
))
5046 options
->flags
= orig_flags
;
5050 void diff_addremove(struct diff_options
*options
,
5051 int addremove
, unsigned mode
,
5052 const unsigned char *sha1
,
5054 const char *concatpath
, unsigned dirty_submodule
)
5056 struct diff_filespec
*one
, *two
;
5058 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
5061 /* This may look odd, but it is a preparation for
5062 * feeding "there are unchanged files which should
5063 * not produce diffs, but when you are doing copy
5064 * detection you would need them, so here they are"
5065 * entries to the diff-core. They will be prefixed
5066 * with something like '=' or '*' (I haven't decided
5067 * which but should not make any difference).
5068 * Feeding the same new and old to diff_change()
5069 * also has the same effect.
5070 * Before the final output happens, they are pruned after
5071 * merged into rename/copy pairs as appropriate.
5073 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
5074 addremove
= (addremove
== '+' ? '-' :
5075 addremove
== '-' ? '+' : addremove
);
5077 if (options
->prefix
&&
5078 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5081 one
= alloc_filespec(concatpath
);
5082 two
= alloc_filespec(concatpath
);
5084 if (addremove
!= '+')
5085 fill_filespec(one
, sha1
, sha1_valid
, mode
);
5086 if (addremove
!= '-') {
5087 fill_filespec(two
, sha1
, sha1_valid
, mode
);
5088 two
->dirty_submodule
= dirty_submodule
;
5091 diff_queue(&diff_queued_diff
, one
, two
);
5092 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5093 DIFF_OPT_SET(options
, HAS_CHANGES
);
5096 void diff_change(struct diff_options
*options
,
5097 unsigned old_mode
, unsigned new_mode
,
5098 const unsigned char *old_sha1
,
5099 const unsigned char *new_sha1
,
5100 int old_sha1_valid
, int new_sha1_valid
,
5101 const char *concatpath
,
5102 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
5104 struct diff_filespec
*one
, *two
;
5105 struct diff_filepair
*p
;
5107 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
5108 is_submodule_ignored(concatpath
, options
))
5111 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
5113 const unsigned char *tmp_c
;
5114 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
5115 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
5116 tmp
= old_sha1_valid
; old_sha1_valid
= new_sha1_valid
;
5117 new_sha1_valid
= tmp
;
5118 tmp
= old_dirty_submodule
; old_dirty_submodule
= new_dirty_submodule
;
5119 new_dirty_submodule
= tmp
;
5122 if (options
->prefix
&&
5123 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5126 one
= alloc_filespec(concatpath
);
5127 two
= alloc_filespec(concatpath
);
5128 fill_filespec(one
, old_sha1
, old_sha1_valid
, old_mode
);
5129 fill_filespec(two
, new_sha1
, new_sha1_valid
, new_mode
);
5130 one
->dirty_submodule
= old_dirty_submodule
;
5131 two
->dirty_submodule
= new_dirty_submodule
;
5132 p
= diff_queue(&diff_queued_diff
, one
, two
);
5134 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5137 if (DIFF_OPT_TST(options
, QUICK
) && options
->skip_stat_unmatch
&&
5138 !diff_filespec_check_stat_unmatch(p
))
5141 DIFF_OPT_SET(options
, HAS_CHANGES
);
5144 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
5146 struct diff_filepair
*pair
;
5147 struct diff_filespec
*one
, *two
;
5149 if (options
->prefix
&&
5150 strncmp(path
, options
->prefix
, options
->prefix_length
))
5153 one
= alloc_filespec(path
);
5154 two
= alloc_filespec(path
);
5155 pair
= diff_queue(&diff_queued_diff
, one
, two
);
5156 pair
->is_unmerged
= 1;
5160 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
5163 struct diff_tempfile
*temp
;
5164 const char *argv
[3];
5165 const char **arg
= argv
;
5166 struct child_process child
= CHILD_PROCESS_INIT
;
5167 struct strbuf buf
= STRBUF_INIT
;
5170 temp
= prepare_temp_file(spec
->path
, spec
);
5172 *arg
++ = temp
->name
;
5175 child
.use_shell
= 1;
5178 if (start_command(&child
)) {
5183 if (strbuf_read(&buf
, child
.out
, 0) < 0)
5184 err
= error("error reading from textconv command '%s'", pgm
);
5187 if (finish_command(&child
) || err
) {
5188 strbuf_release(&buf
);
5194 return strbuf_detach(&buf
, outsize
);
5197 size_t fill_textconv(struct userdiff_driver
*driver
,
5198 struct diff_filespec
*df
,
5204 if (!DIFF_FILE_VALID(df
)) {
5208 if (diff_populate_filespec(df
, 0))
5209 die("unable to read files to diff");
5214 if (!driver
->textconv
)
5215 die("BUG: fill_textconv called with non-textconv driver");
5217 if (driver
->textconv_cache
&& df
->oid_valid
) {
5218 *outbuf
= notes_cache_get(driver
->textconv_cache
,
5225 *outbuf
= run_textconv(driver
->textconv
, df
, &size
);
5227 die("unable to read files to diff");
5229 if (driver
->textconv_cache
&& df
->oid_valid
) {
5230 /* ignore errors, as we might be in a readonly repository */
5231 notes_cache_put(driver
->textconv_cache
, df
->oid
.hash
, *outbuf
,
5234 * we could save up changes and flush them all at the end,
5235 * but we would need an extra call after all diffing is done.
5236 * Since generating a cache entry is the slow path anyway,
5237 * this extra overhead probably isn't a big deal.
5239 notes_cache_write(driver
->textconv_cache
);
5245 void setup_diff_pager(struct diff_options
*opt
)
5248 * If the user asked for our exit code, then either they want --quiet
5249 * or --exit-code. We should definitely not bother with a pager in the
5250 * former case, as we will generate no output. Since we still properly
5251 * report our exit code even when a pager is run, we _could_ run a
5252 * pager with --exit-code. But since we have not done so historically,
5253 * and because it is easy to find people oneline advising "git diff
5254 * --exit-code" in hooks and other scripts, we do not do so.
5256 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
5257 check_pager_config("diff") != 0)