2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "run-command.h"
16 #include "submodule.h"
19 #ifdef NO_FAST_WORKING_DIRECTORY
20 #define FAST_WORKING_DIRECTORY 0
22 #define FAST_WORKING_DIRECTORY 1
25 static int diff_detect_rename_default
;
26 static int diff_rename_limit_default
= 200;
27 static int diff_suppress_blank_empty
;
28 int diff_use_color_default
= -1;
29 static const char *diff_word_regex_cfg
;
30 static const char *external_diff_cmd_cfg
;
31 int diff_auto_refresh_index
= 1;
32 static int diff_mnemonic_prefix
;
34 static char diff_colors
[][COLOR_MAXLEN
] = {
36 GIT_COLOR_NORMAL
, /* PLAIN */
37 GIT_COLOR_BOLD
, /* METAINFO */
38 GIT_COLOR_CYAN
, /* FRAGINFO */
39 GIT_COLOR_RED
, /* OLD */
40 GIT_COLOR_GREEN
, /* NEW */
41 GIT_COLOR_YELLOW
, /* COMMIT */
42 GIT_COLOR_BG_RED
, /* WHITESPACE */
43 GIT_COLOR_NORMAL
, /* FUNCINFO */
46 static void diff_filespec_load_driver(struct diff_filespec
*one
);
47 static size_t fill_textconv(struct userdiff_driver
*driver
,
48 struct diff_filespec
*df
, char **outbuf
);
50 static int parse_diff_color_slot(const char *var
, int ofs
)
52 if (!strcasecmp(var
+ofs
, "plain"))
54 if (!strcasecmp(var
+ofs
, "meta"))
56 if (!strcasecmp(var
+ofs
, "frag"))
58 if (!strcasecmp(var
+ofs
, "old"))
60 if (!strcasecmp(var
+ofs
, "new"))
62 if (!strcasecmp(var
+ofs
, "commit"))
64 if (!strcasecmp(var
+ofs
, "whitespace"))
65 return DIFF_WHITESPACE
;
66 if (!strcasecmp(var
+ofs
, "func"))
71 static int git_config_rename(const char *var
, const char *value
)
74 return DIFF_DETECT_RENAME
;
75 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
76 return DIFF_DETECT_COPY
;
77 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
81 * These are to give UI layer defaults.
82 * The core-level commands such as git-diff-files should
83 * never be affected by the setting of diff.renames
84 * the user happens to have in the configuration file.
86 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
88 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
89 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
92 if (!strcmp(var
, "diff.renames")) {
93 diff_detect_rename_default
= git_config_rename(var
, value
);
96 if (!strcmp(var
, "diff.autorefreshindex")) {
97 diff_auto_refresh_index
= git_config_bool(var
, value
);
100 if (!strcmp(var
, "diff.mnemonicprefix")) {
101 diff_mnemonic_prefix
= git_config_bool(var
, value
);
104 if (!strcmp(var
, "diff.external"))
105 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
106 if (!strcmp(var
, "diff.wordregex"))
107 return git_config_string(&diff_word_regex_cfg
, var
, value
);
109 return git_diff_basic_config(var
, value
, cb
);
112 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
114 if (!strcmp(var
, "diff.renamelimit")) {
115 diff_rename_limit_default
= git_config_int(var
, value
);
119 switch (userdiff_config(var
, value
)) {
125 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
126 int slot
= parse_diff_color_slot(var
, 11);
130 return config_error_nonbool(var
);
131 color_parse(value
, var
, diff_colors
[slot
]);
135 /* like GNU diff's --suppress-blank-empty option */
136 if (!strcmp(var
, "diff.suppressblankempty") ||
137 /* for backwards compatibility */
138 !strcmp(var
, "diff.suppress-blank-empty")) {
139 diff_suppress_blank_empty
= git_config_bool(var
, value
);
143 return git_color_default_config(var
, value
, cb
);
146 static char *quote_two(const char *one
, const char *two
)
148 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
149 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
150 struct strbuf res
= STRBUF_INIT
;
152 if (need_one
+ need_two
) {
153 strbuf_addch(&res
, '"');
154 quote_c_style(one
, &res
, NULL
, 1);
155 quote_c_style(two
, &res
, NULL
, 1);
156 strbuf_addch(&res
, '"');
158 strbuf_addstr(&res
, one
);
159 strbuf_addstr(&res
, two
);
161 return strbuf_detach(&res
, NULL
);
164 static const char *external_diff(void)
166 static const char *external_diff_cmd
= NULL
;
167 static int done_preparing
= 0;
170 return external_diff_cmd
;
171 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
172 if (!external_diff_cmd
)
173 external_diff_cmd
= external_diff_cmd_cfg
;
175 return external_diff_cmd
;
178 static struct diff_tempfile
{
179 const char *name
; /* filename external diff should read from */
182 char tmp_path
[PATH_MAX
];
185 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
187 struct emit_callback
{
190 int blank_at_eof_in_preimage
;
191 int blank_at_eof_in_postimage
;
193 int lno_in_postimage
;
194 sane_truncate_fn truncate
;
195 const char **label_path
;
196 struct diff_words_data
*diff_words
;
199 struct strbuf
*header
;
202 static int count_lines(const char *data
, int size
)
204 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
211 completely_empty
= 0;
215 completely_empty
= 0;
218 if (completely_empty
)
221 count
++; /* no trailing newline */
225 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
227 if (!DIFF_FILE_VALID(one
)) {
228 mf
->ptr
= (char *)""; /* does not matter */
232 else if (diff_populate_filespec(one
, 0))
236 mf
->size
= one
->size
;
240 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
243 long size
= mf
->size
;
248 ptr
+= size
- 1; /* pointing at the very end */
250 ; /* incomplete line */
252 ptr
--; /* skip the last LF */
253 while (mf
->ptr
< ptr
) {
255 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
256 if (*prev_eol
== '\n')
258 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
266 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
267 struct emit_callback
*ecbdata
)
270 unsigned ws_rule
= ecbdata
->ws_rule
;
271 l1
= count_trailing_blank(mf1
, ws_rule
);
272 l2
= count_trailing_blank(mf2
, ws_rule
);
274 ecbdata
->blank_at_eof_in_preimage
= 0;
275 ecbdata
->blank_at_eof_in_postimage
= 0;
278 at
= count_lines(mf1
->ptr
, mf1
->size
);
279 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
281 at
= count_lines(mf2
->ptr
, mf2
->size
);
282 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
285 static void emit_line_0(FILE *file
, const char *set
, const char *reset
,
286 int first
, const char *line
, int len
)
288 int has_trailing_newline
, has_trailing_carriage_return
;
292 has_trailing_newline
= (first
== '\n');
293 has_trailing_carriage_return
= (!has_trailing_newline
&&
295 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
297 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
298 if (has_trailing_newline
)
300 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
301 if (has_trailing_carriage_return
)
306 if (len
|| !nofirst
) {
310 fwrite(line
, len
, 1, file
);
313 if (has_trailing_carriage_return
)
315 if (has_trailing_newline
)
319 static void emit_line(FILE *file
, const char *set
, const char *reset
,
320 const char *line
, int len
)
322 emit_line_0(file
, set
, reset
, line
[0], line
+1, len
-1);
325 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
327 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
328 ecbdata
->blank_at_eof_in_preimage
&&
329 ecbdata
->blank_at_eof_in_postimage
&&
330 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
331 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
333 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
336 static void emit_add_line(const char *reset
,
337 struct emit_callback
*ecbdata
,
338 const char *line
, int len
)
340 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
341 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
344 emit_line_0(ecbdata
->file
, set
, reset
, '+', line
, len
);
345 else if (new_blank_line_at_eof(ecbdata
, line
, len
))
346 /* Blank line at EOF - paint '+' as well */
347 emit_line_0(ecbdata
->file
, ws
, reset
, '+', line
, len
);
349 /* Emit just the prefix, then the rest. */
350 emit_line_0(ecbdata
->file
, set
, reset
, '+', "", 0);
351 ws_check_emit(line
, len
, ecbdata
->ws_rule
,
352 ecbdata
->file
, set
, reset
, ws
);
356 static void emit_hunk_header(struct emit_callback
*ecbdata
,
357 const char *line
, int len
)
359 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
360 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
361 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
362 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
363 static const char atat
[2] = { '@', '@' };
367 * As a hunk header must begin with "@@ -<old>, +<new> @@",
368 * it always is at least 10 bytes long.
371 memcmp(line
, atat
, 2) ||
372 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
373 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
376 ep
+= 2; /* skip over @@ */
378 /* The hunk header in fraginfo color */
379 emit_line(ecbdata
->file
, frag
, reset
, line
, ep
- line
);
381 /* blank before the func header */
382 for (cp
= ep
; ep
- line
< len
; ep
++)
383 if (*ep
!= ' ' && *ep
!= '\t')
386 emit_line(ecbdata
->file
, plain
, reset
, cp
, ep
- cp
);
389 emit_line(ecbdata
->file
, func
, reset
, ep
, line
+ len
- ep
);
392 static struct diff_tempfile
*claim_diff_tempfile(void) {
394 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
395 if (!diff_temp
[i
].name
)
396 return diff_temp
+ i
;
397 die("BUG: diff is failing to clean up its tempfiles");
400 static int remove_tempfile_installed
;
402 static void remove_tempfile(void)
405 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
406 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
)
407 unlink_or_warn(diff_temp
[i
].name
);
408 diff_temp
[i
].name
= NULL
;
412 static void remove_tempfile_on_signal(int signo
)
419 static void print_line_count(FILE *file
, int count
)
423 fprintf(file
, "0,0");
429 fprintf(file
, "1,%d", count
);
434 static void emit_rewrite_lines(struct emit_callback
*ecb
,
435 int prefix
, const char *data
, int size
)
437 const char *endp
= NULL
;
438 static const char *nneof
= " No newline at end of file\n";
439 const char *old
= diff_get_color(ecb
->color_diff
, DIFF_FILE_OLD
);
440 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
445 endp
= memchr(data
, '\n', size
);
446 len
= endp
? (endp
- data
+ 1) : size
;
448 ecb
->lno_in_preimage
++;
449 emit_line_0(ecb
->file
, old
, reset
, '-',
452 ecb
->lno_in_postimage
++;
453 emit_add_line(reset
, ecb
, data
, len
);
459 const char *plain
= diff_get_color(ecb
->color_diff
,
461 emit_line_0(ecb
->file
, plain
, reset
, '\\',
462 nneof
, strlen(nneof
));
466 static void emit_rewrite_diff(const char *name_a
,
468 struct diff_filespec
*one
,
469 struct diff_filespec
*two
,
470 struct userdiff_driver
*textconv_one
,
471 struct userdiff_driver
*textconv_two
,
472 struct diff_options
*o
)
475 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
476 const char *name_a_tab
, *name_b_tab
;
477 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
478 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
479 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
480 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
481 const char *a_prefix
, *b_prefix
;
482 char *data_one
, *data_two
;
483 size_t size_one
, size_two
;
484 struct emit_callback ecbdata
;
486 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
487 a_prefix
= o
->b_prefix
;
488 b_prefix
= o
->a_prefix
;
490 a_prefix
= o
->a_prefix
;
491 b_prefix
= o
->b_prefix
;
494 name_a
+= (*name_a
== '/');
495 name_b
+= (*name_b
== '/');
496 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
497 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
499 strbuf_reset(&a_name
);
500 strbuf_reset(&b_name
);
501 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
502 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
504 size_one
= fill_textconv(textconv_one
, one
, &data_one
);
505 size_two
= fill_textconv(textconv_two
, two
, &data_two
);
507 memset(&ecbdata
, 0, sizeof(ecbdata
));
508 ecbdata
.color_diff
= color_diff
;
509 ecbdata
.found_changesp
= &o
->found_changes
;
510 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
511 ecbdata
.file
= o
->file
;
512 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
514 mf1
.ptr
= (char *)data_one
;
515 mf2
.ptr
= (char *)data_two
;
518 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
520 ecbdata
.lno_in_preimage
= 1;
521 ecbdata
.lno_in_postimage
= 1;
523 lc_a
= count_lines(data_one
, size_one
);
524 lc_b
= count_lines(data_two
, size_two
);
526 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
527 metainfo
, a_name
.buf
, name_a_tab
, reset
,
528 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
529 print_line_count(o
->file
, lc_a
);
530 fprintf(o
->file
, " +");
531 print_line_count(o
->file
, lc_b
);
532 fprintf(o
->file
, " @@%s\n", reset
);
534 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
536 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
538 free((char *)data_one
);
540 free((char *)data_two
);
543 struct diff_words_buffer
{
546 struct diff_words_orig
{
547 const char *begin
, *end
;
549 int orig_nr
, orig_alloc
;
552 static void diff_words_append(char *line
, unsigned long len
,
553 struct diff_words_buffer
*buffer
)
555 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
558 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
559 buffer
->text
.size
+= len
;
560 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
563 struct diff_words_style_elem
567 const char *color
; /* NULL; filled in by the setup code if
568 * color is enabled */
571 struct diff_words_style
573 enum diff_words_type type
;
574 struct diff_words_style_elem
new, old
, ctx
;
578 struct diff_words_style diff_words_styles
[] = {
579 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
580 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
581 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
584 struct diff_words_data
{
585 struct diff_words_buffer minus
, plus
;
586 const char *current_plus
;
589 enum diff_words_type type
;
590 struct diff_words_style
*style
;
593 static int fn_out_diff_words_write_helper(FILE *fp
,
594 struct diff_words_style_elem
*st_el
,
596 size_t count
, const char *buf
)
599 char *p
= memchr(buf
, '\n', count
);
601 if (st_el
->color
&& fputs(st_el
->color
, fp
) < 0)
603 if (fputs(st_el
->prefix
, fp
) < 0 ||
604 fwrite(buf
, p
? p
- buf
: count
, 1, fp
) != 1 ||
605 fputs(st_el
->suffix
, fp
) < 0)
607 if (st_el
->color
&& *st_el
->color
608 && fputs(GIT_COLOR_RESET
, fp
) < 0)
613 if (fputs(newline
, fp
) < 0)
615 count
-= p
+ 1 - buf
;
621 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
623 struct diff_words_data
*diff_words
= priv
;
624 struct diff_words_style
*style
= diff_words
->style
;
625 int minus_first
, minus_len
, plus_first
, plus_len
;
626 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
628 if (line
[0] != '@' || parse_hunk_header(line
, len
,
629 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
632 /* POSIX requires that first be decremented by one if len == 0... */
634 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
636 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
638 minus_begin
= minus_end
=
639 diff_words
->minus
.orig
[minus_first
].end
;
642 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
643 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
645 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
647 if (diff_words
->current_plus
!= plus_begin
)
648 fn_out_diff_words_write_helper(diff_words
->file
,
649 &style
->ctx
, style
->newline
,
650 plus_begin
- diff_words
->current_plus
,
651 diff_words
->current_plus
);
652 if (minus_begin
!= minus_end
)
653 fn_out_diff_words_write_helper(diff_words
->file
,
654 &style
->old
, style
->newline
,
655 minus_end
- minus_begin
, minus_begin
);
656 if (plus_begin
!= plus_end
)
657 fn_out_diff_words_write_helper(diff_words
->file
,
658 &style
->new, style
->newline
,
659 plus_end
- plus_begin
, plus_begin
);
661 diff_words
->current_plus
= plus_end
;
664 /* This function starts looking at *begin, and returns 0 iff a word was found. */
665 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
666 int *begin
, int *end
)
668 if (word_regex
&& *begin
< buffer
->size
) {
670 if (!regexec(word_regex
, buffer
->ptr
+ *begin
, 1, match
, 0)) {
671 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
672 '\n', match
[0].rm_eo
- match
[0].rm_so
);
673 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
674 *begin
+= match
[0].rm_so
;
675 return *begin
>= *end
;
680 /* find the next word */
681 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
683 if (*begin
>= buffer
->size
)
686 /* find the end of the word */
688 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
695 * This function splits the words in buffer->text, stores the list with
696 * newline separator into out, and saves the offsets of the original words
699 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
708 /* fake an empty "0th" word */
709 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
710 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
713 for (i
= 0; i
< buffer
->text
.size
; i
++) {
714 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
717 /* store original boundaries */
718 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
720 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
721 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
725 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
726 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
727 out
->ptr
[out
->size
+ j
- i
] = '\n';
728 out
->size
+= j
- i
+ 1;
734 /* this executes the word diff on the accumulated buffers */
735 static void diff_words_show(struct diff_words_data
*diff_words
)
739 mmfile_t minus
, plus
;
740 struct diff_words_style
*style
= diff_words
->style
;
742 /* special case: only removal */
743 if (!diff_words
->plus
.text
.size
) {
744 fn_out_diff_words_write_helper(diff_words
->file
,
745 &style
->old
, style
->newline
,
746 diff_words
->minus
.text
.size
, diff_words
->minus
.text
.ptr
);
747 diff_words
->minus
.text
.size
= 0;
751 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
753 memset(&xpp
, 0, sizeof(xpp
));
754 memset(&xecfg
, 0, sizeof(xecfg
));
755 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
756 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
757 xpp
.flags
= XDF_NEED_MINIMAL
;
758 /* as only the hunk header will be parsed, we need a 0-context */
760 xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
764 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
765 diff_words
->plus
.text
.size
)
766 fn_out_diff_words_write_helper(diff_words
->file
,
767 &style
->ctx
, style
->newline
,
768 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
769 - diff_words
->current_plus
, diff_words
->current_plus
);
770 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
773 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
774 static void diff_words_flush(struct emit_callback
*ecbdata
)
776 if (ecbdata
->diff_words
->minus
.text
.size
||
777 ecbdata
->diff_words
->plus
.text
.size
)
778 diff_words_show(ecbdata
->diff_words
);
781 static void free_diff_words_data(struct emit_callback
*ecbdata
)
783 if (ecbdata
->diff_words
) {
784 diff_words_flush(ecbdata
);
785 free (ecbdata
->diff_words
->minus
.text
.ptr
);
786 free (ecbdata
->diff_words
->minus
.orig
);
787 free (ecbdata
->diff_words
->plus
.text
.ptr
);
788 free (ecbdata
->diff_words
->plus
.orig
);
789 free(ecbdata
->diff_words
->word_regex
);
790 free(ecbdata
->diff_words
);
791 ecbdata
->diff_words
= NULL
;
795 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
798 return diff_colors
[ix
];
802 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
809 return ecb
->truncate(line
, len
);
813 (void) utf8_width(&cp
, &l
);
815 break; /* truncated in the middle? */
820 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
823 ecbdata
->lno_in_preimage
= 0;
824 ecbdata
->lno_in_postimage
= 0;
825 p
= strchr(line
, '-');
827 return; /* cannot happen */
828 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
831 return; /* cannot happen */
832 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
835 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
837 struct emit_callback
*ecbdata
= priv
;
838 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
839 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
840 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
842 if (ecbdata
->header
) {
843 fprintf(ecbdata
->file
, "%s", ecbdata
->header
->buf
);
844 strbuf_reset(ecbdata
->header
);
845 ecbdata
->header
= NULL
;
847 *(ecbdata
->found_changesp
) = 1;
849 if (ecbdata
->label_path
[0]) {
850 const char *name_a_tab
, *name_b_tab
;
852 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
853 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
855 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
856 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
857 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
858 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
859 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
862 if (diff_suppress_blank_empty
863 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
868 if (line
[0] == '@') {
869 if (ecbdata
->diff_words
)
870 diff_words_flush(ecbdata
);
871 len
= sane_truncate_line(ecbdata
, line
, len
);
872 find_lno(line
, ecbdata
);
873 emit_hunk_header(ecbdata
, line
, len
);
874 if (line
[len
-1] != '\n')
875 putc('\n', ecbdata
->file
);
880 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
881 if (ecbdata
->diff_words
882 && ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
)
883 fputs("~\n", ecbdata
->file
);
887 if (ecbdata
->diff_words
) {
888 if (line
[0] == '-') {
889 diff_words_append(line
, len
,
890 &ecbdata
->diff_words
->minus
);
892 } else if (line
[0] == '+') {
893 diff_words_append(line
, len
,
894 &ecbdata
->diff_words
->plus
);
897 diff_words_flush(ecbdata
);
898 if (ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
) {
899 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
900 fputs("~\n", ecbdata
->file
);
902 /* don't print the prefix character */
903 emit_line(ecbdata
->file
, plain
, reset
, line
+1, len
-1);
908 if (line
[0] != '+') {
910 diff_get_color(ecbdata
->color_diff
,
911 line
[0] == '-' ? DIFF_FILE_OLD
: DIFF_PLAIN
);
912 ecbdata
->lno_in_preimage
++;
914 ecbdata
->lno_in_postimage
++;
915 emit_line(ecbdata
->file
, color
, reset
, line
, len
);
917 ecbdata
->lno_in_postimage
++;
918 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
922 static char *pprint_rename(const char *a
, const char *b
)
926 struct strbuf name
= STRBUF_INIT
;
927 int pfx_length
, sfx_length
;
928 int len_a
= strlen(a
);
929 int len_b
= strlen(b
);
930 int a_midlen
, b_midlen
;
931 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
932 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
934 if (qlen_a
|| qlen_b
) {
935 quote_c_style(a
, &name
, NULL
, 0);
936 strbuf_addstr(&name
, " => ");
937 quote_c_style(b
, &name
, NULL
, 0);
938 return strbuf_detach(&name
, NULL
);
941 /* Find common prefix */
943 while (*old
&& *new && *old
== *new) {
945 pfx_length
= old
- a
+ 1;
950 /* Find common suffix */
954 while (a
<= old
&& b
<= new && *old
== *new) {
956 sfx_length
= len_a
- (old
- a
);
962 * pfx{mid-a => mid-b}sfx
963 * {pfx-a => pfx-b}sfx
964 * pfx{sfx-a => sfx-b}
967 a_midlen
= len_a
- pfx_length
- sfx_length
;
968 b_midlen
= len_b
- pfx_length
- sfx_length
;
974 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
975 if (pfx_length
+ sfx_length
) {
976 strbuf_add(&name
, a
, pfx_length
);
977 strbuf_addch(&name
, '{');
979 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
980 strbuf_addstr(&name
, " => ");
981 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
982 if (pfx_length
+ sfx_length
) {
983 strbuf_addch(&name
, '}');
984 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
986 return strbuf_detach(&name
, NULL
);
992 struct diffstat_file
{
996 unsigned is_unmerged
:1;
997 unsigned is_binary
:1;
998 unsigned is_renamed
:1;
999 uintmax_t added
, deleted
;
1003 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
1007 struct diffstat_file
*x
;
1008 x
= xcalloc(sizeof (*x
), 1);
1009 if (diffstat
->nr
== diffstat
->alloc
) {
1010 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
1011 diffstat
->files
= xrealloc(diffstat
->files
,
1012 diffstat
->alloc
* sizeof(x
));
1014 diffstat
->files
[diffstat
->nr
++] = x
;
1016 x
->from_name
= xstrdup(name_a
);
1017 x
->name
= xstrdup(name_b
);
1021 x
->from_name
= NULL
;
1022 x
->name
= xstrdup(name_a
);
1027 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
1029 struct diffstat_t
*diffstat
= priv
;
1030 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
1034 else if (line
[0] == '-')
1038 const char mime_boundary_leader
[] = "------------";
1040 static int scale_linear(int it
, int width
, int max_change
)
1043 * make sure that at least one '-' is printed if there were deletions,
1044 * and likewise for '+'.
1048 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
1051 static void show_name(FILE *file
,
1052 const char *prefix
, const char *name
, int len
)
1054 fprintf(file
, " %s%-*s |", prefix
, len
, name
);
1057 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
1061 fprintf(file
, "%s", set
);
1064 fprintf(file
, "%s", reset
);
1067 static void fill_print_name(struct diffstat_file
*file
)
1071 if (file
->print_name
)
1074 if (!file
->is_renamed
) {
1075 struct strbuf buf
= STRBUF_INIT
;
1076 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
1077 pname
= strbuf_detach(&buf
, NULL
);
1080 strbuf_release(&buf
);
1083 pname
= pprint_rename(file
->from_name
, file
->name
);
1085 file
->print_name
= pname
;
1088 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
1090 int i
, len
, add
, del
, adds
= 0, dels
= 0;
1091 uintmax_t max_change
= 0, max_len
= 0;
1092 int total_files
= data
->nr
;
1093 int width
, name_width
;
1094 const char *reset
, *set
, *add_c
, *del_c
;
1099 width
= options
->stat_width
? options
->stat_width
: 80;
1100 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
1102 /* Sanity: give at least 5 columns to the graph,
1103 * but leave at least 10 columns for the name.
1107 if (name_width
< 10)
1109 else if (width
< name_width
+ 15)
1110 name_width
= width
- 15;
1112 /* Find the longest filename and max number of changes */
1113 reset
= diff_get_color_opt(options
, DIFF_RESET
);
1114 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
1115 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
1116 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
1118 for (i
= 0; i
< data
->nr
; i
++) {
1119 struct diffstat_file
*file
= data
->files
[i
];
1120 uintmax_t change
= file
->added
+ file
->deleted
;
1121 fill_print_name(file
);
1122 len
= strlen(file
->print_name
);
1126 if (file
->is_binary
|| file
->is_unmerged
)
1128 if (max_change
< change
)
1129 max_change
= change
;
1132 /* Compute the width of the graph part;
1133 * 10 is for one blank at the beginning of the line plus
1134 * " | count " between the name and the graph.
1136 * From here on, name_width is the width of the name area,
1137 * and width is the width of the graph area.
1139 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
1140 if (width
< (name_width
+ 10) + max_change
)
1141 width
= width
- (name_width
+ 10);
1145 for (i
= 0; i
< data
->nr
; i
++) {
1146 const char *prefix
= "";
1147 char *name
= data
->files
[i
]->print_name
;
1148 uintmax_t added
= data
->files
[i
]->added
;
1149 uintmax_t deleted
= data
->files
[i
]->deleted
;
1153 * "scale" the filename
1156 name_len
= strlen(name
);
1157 if (name_width
< name_len
) {
1161 name
+= name_len
- len
;
1162 slash
= strchr(name
, '/');
1167 if (data
->files
[i
]->is_binary
) {
1168 show_name(options
->file
, prefix
, name
, len
);
1169 fprintf(options
->file
, " Bin ");
1170 fprintf(options
->file
, "%s%"PRIuMAX
"%s",
1171 del_c
, deleted
, reset
);
1172 fprintf(options
->file
, " -> ");
1173 fprintf(options
->file
, "%s%"PRIuMAX
"%s",
1174 add_c
, added
, reset
);
1175 fprintf(options
->file
, " bytes");
1176 fprintf(options
->file
, "\n");
1179 else if (data
->files
[i
]->is_unmerged
) {
1180 show_name(options
->file
, prefix
, name
, len
);
1181 fprintf(options
->file
, " Unmerged\n");
1184 else if (!data
->files
[i
]->is_renamed
&&
1185 (added
+ deleted
== 0)) {
1191 * scale the add/delete
1198 if (width
<= max_change
) {
1199 add
= scale_linear(add
, width
, max_change
);
1200 del
= scale_linear(del
, width
, max_change
);
1202 show_name(options
->file
, prefix
, name
, len
);
1203 fprintf(options
->file
, "%5"PRIuMAX
"%s", added
+ deleted
,
1204 added
+ deleted
? " " : "");
1205 show_graph(options
->file
, '+', add
, add_c
, reset
);
1206 show_graph(options
->file
, '-', del
, del_c
, reset
);
1207 fprintf(options
->file
, "\n");
1209 fprintf(options
->file
,
1210 " %d files changed, %d insertions(+), %d deletions(-)\n",
1211 total_files
, adds
, dels
);
1214 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
1216 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
1221 for (i
= 0; i
< data
->nr
; i
++) {
1222 if (!data
->files
[i
]->is_binary
&&
1223 !data
->files
[i
]->is_unmerged
) {
1224 int added
= data
->files
[i
]->added
;
1225 int deleted
= data
->files
[i
]->deleted
;
1226 if (!data
->files
[i
]->is_renamed
&&
1227 (added
+ deleted
== 0)) {
1235 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
1236 total_files
, adds
, dels
);
1239 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
1246 for (i
= 0; i
< data
->nr
; i
++) {
1247 struct diffstat_file
*file
= data
->files
[i
];
1249 if (file
->is_binary
)
1250 fprintf(options
->file
, "-\t-\t");
1252 fprintf(options
->file
,
1253 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
1254 file
->added
, file
->deleted
);
1255 if (options
->line_termination
) {
1256 fill_print_name(file
);
1257 if (!file
->is_renamed
)
1258 write_name_quoted(file
->name
, options
->file
,
1259 options
->line_termination
);
1261 fputs(file
->print_name
, options
->file
);
1262 putc(options
->line_termination
, options
->file
);
1265 if (file
->is_renamed
) {
1266 putc('\0', options
->file
);
1267 write_name_quoted(file
->from_name
, options
->file
, '\0');
1269 write_name_quoted(file
->name
, options
->file
, '\0');
1274 struct dirstat_file
{
1276 unsigned long changed
;
1279 struct dirstat_dir
{
1280 struct dirstat_file
*files
;
1281 int alloc
, nr
, percent
, cumulative
;
1284 static long gather_dirstat(FILE *file
, struct dirstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1286 unsigned long this_dir
= 0;
1287 unsigned int sources
= 0;
1290 struct dirstat_file
*f
= dir
->files
;
1291 int namelen
= strlen(f
->name
);
1295 if (namelen
< baselen
)
1297 if (memcmp(f
->name
, base
, baselen
))
1299 slash
= strchr(f
->name
+ baselen
, '/');
1301 int newbaselen
= slash
+ 1 - f
->name
;
1302 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1314 * We don't report dirstat's for
1316 * - or cases where everything came from a single directory
1317 * under this directory (sources == 1).
1319 if (baselen
&& sources
!= 1) {
1320 int permille
= this_dir
* 1000 / changed
;
1322 int percent
= permille
/ 10;
1323 if (percent
>= dir
->percent
) {
1324 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1325 if (!dir
->cumulative
)
1333 static int dirstat_compare(const void *_a
, const void *_b
)
1335 const struct dirstat_file
*a
= _a
;
1336 const struct dirstat_file
*b
= _b
;
1337 return strcmp(a
->name
, b
->name
);
1340 static void show_dirstat(struct diff_options
*options
)
1343 unsigned long changed
;
1344 struct dirstat_dir dir
;
1345 struct diff_queue_struct
*q
= &diff_queued_diff
;
1350 dir
.percent
= options
->dirstat_percent
;
1351 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1354 for (i
= 0; i
< q
->nr
; i
++) {
1355 struct diff_filepair
*p
= q
->queue
[i
];
1357 unsigned long copied
, added
, damage
;
1359 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1361 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1362 diff_populate_filespec(p
->one
, 0);
1363 diff_populate_filespec(p
->two
, 0);
1364 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1366 diff_free_filespec_data(p
->one
);
1367 diff_free_filespec_data(p
->two
);
1368 } else if (DIFF_FILE_VALID(p
->one
)) {
1369 diff_populate_filespec(p
->one
, 1);
1371 diff_free_filespec_data(p
->one
);
1372 } else if (DIFF_FILE_VALID(p
->two
)) {
1373 diff_populate_filespec(p
->two
, 1);
1375 added
= p
->two
->size
;
1376 diff_free_filespec_data(p
->two
);
1381 * Original minus copied is the removed material,
1382 * added is the new material. They are both damages
1383 * made to the preimage. In --dirstat-by-file mode, count
1384 * damaged files, not damaged lines. This is done by
1385 * counting only a single damaged line per file.
1387 damage
= (p
->one
->size
- copied
) + added
;
1388 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
) && damage
> 0)
1391 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1392 dir
.files
[dir
.nr
].name
= name
;
1393 dir
.files
[dir
.nr
].changed
= damage
;
1398 /* This can happen even with many files, if everything was renames */
1402 /* Show all directories with more than x% of the changes */
1403 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
1404 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1407 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1410 for (i
= 0; i
< diffstat
->nr
; i
++) {
1411 struct diffstat_file
*f
= diffstat
->files
[i
];
1412 if (f
->name
!= f
->print_name
)
1413 free(f
->print_name
);
1418 free(diffstat
->files
);
1421 struct checkdiff_t
{
1422 const char *filename
;
1424 int conflict_marker_size
;
1425 struct diff_options
*o
;
1430 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
1435 if (len
< marker_size
+ 1)
1437 firstchar
= line
[0];
1438 switch (firstchar
) {
1439 case '=': case '>': case '<': case '|':
1444 for (cnt
= 1; cnt
< marker_size
; cnt
++)
1445 if (line
[cnt
] != firstchar
)
1447 /* line[1] thru line[marker_size-1] are same as firstchar */
1448 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
1453 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1455 struct checkdiff_t
*data
= priv
;
1456 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1457 int marker_size
= data
->conflict_marker_size
;
1458 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1459 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1460 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1463 if (line
[0] == '+') {
1466 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
1468 fprintf(data
->o
->file
,
1469 "%s:%d: leftover conflict marker\n",
1470 data
->filename
, data
->lineno
);
1472 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1475 data
->status
|= bad
;
1476 err
= whitespace_error_string(bad
);
1477 fprintf(data
->o
->file
, "%s:%d: %s.\n",
1478 data
->filename
, data
->lineno
, err
);
1480 emit_line(data
->o
->file
, set
, reset
, line
, 1);
1481 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1482 data
->o
->file
, set
, reset
, ws
);
1483 } else if (line
[0] == ' ') {
1485 } else if (line
[0] == '@') {
1486 char *plus
= strchr(line
, '+');
1488 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1490 die("invalid diff");
1494 static unsigned char *deflate_it(char *data
,
1496 unsigned long *result_size
)
1499 unsigned char *deflated
;
1502 memset(&stream
, 0, sizeof(stream
));
1503 deflateInit(&stream
, zlib_compression_level
);
1504 bound
= deflateBound(&stream
, size
);
1505 deflated
= xmalloc(bound
);
1506 stream
.next_out
= deflated
;
1507 stream
.avail_out
= bound
;
1509 stream
.next_in
= (unsigned char *)data
;
1510 stream
.avail_in
= size
;
1511 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1513 deflateEnd(&stream
);
1514 *result_size
= stream
.total_out
;
1518 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1524 unsigned long orig_size
;
1525 unsigned long delta_size
;
1526 unsigned long deflate_size
;
1527 unsigned long data_size
;
1529 /* We could do deflated delta, or we could do just deflated two,
1530 * whichever is smaller.
1533 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1534 if (one
->size
&& two
->size
) {
1535 delta
= diff_delta(one
->ptr
, one
->size
,
1536 two
->ptr
, two
->size
,
1537 &delta_size
, deflate_size
);
1539 void *to_free
= delta
;
1540 orig_size
= delta_size
;
1541 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1546 if (delta
&& delta_size
< deflate_size
) {
1547 fprintf(file
, "delta %lu\n", orig_size
);
1550 data_size
= delta_size
;
1553 fprintf(file
, "literal %lu\n", two
->size
);
1556 data_size
= deflate_size
;
1559 /* emit data encoded in base85 */
1562 int bytes
= (52 < data_size
) ? 52 : data_size
;
1566 line
[0] = bytes
+ 'A' - 1;
1568 line
[0] = bytes
- 26 + 'a' - 1;
1569 encode_85(line
+ 1, cp
, bytes
);
1570 cp
= (char *) cp
+ bytes
;
1574 fprintf(file
, "\n");
1578 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1580 fprintf(file
, "GIT binary patch\n");
1581 emit_binary_diff_body(file
, one
, two
);
1582 emit_binary_diff_body(file
, two
, one
);
1585 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1588 one
->driver
= userdiff_find_by_path(one
->path
);
1590 one
->driver
= userdiff_find_by_name("default");
1593 int diff_filespec_is_binary(struct diff_filespec
*one
)
1595 if (one
->is_binary
== -1) {
1596 diff_filespec_load_driver(one
);
1597 if (one
->driver
->binary
!= -1)
1598 one
->is_binary
= one
->driver
->binary
;
1600 if (!one
->data
&& DIFF_FILE_VALID(one
))
1601 diff_populate_filespec(one
, 0);
1603 one
->is_binary
= buffer_is_binary(one
->data
,
1605 if (one
->is_binary
== -1)
1609 return one
->is_binary
;
1612 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
1614 diff_filespec_load_driver(one
);
1615 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
1618 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1620 diff_filespec_load_driver(one
);
1621 return one
->driver
->word_regex
;
1624 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
1626 if (!options
->a_prefix
)
1627 options
->a_prefix
= a
;
1628 if (!options
->b_prefix
)
1629 options
->b_prefix
= b
;
1632 static struct userdiff_driver
*get_textconv(struct diff_filespec
*one
)
1634 if (!DIFF_FILE_VALID(one
))
1636 if (!S_ISREG(one
->mode
))
1638 diff_filespec_load_driver(one
);
1639 if (!one
->driver
->textconv
)
1642 if (one
->driver
->textconv_want_cache
&& !one
->driver
->textconv_cache
) {
1643 struct notes_cache
*c
= xmalloc(sizeof(*c
));
1644 struct strbuf name
= STRBUF_INIT
;
1646 strbuf_addf(&name
, "textconv/%s", one
->driver
->name
);
1647 notes_cache_init(c
, name
.buf
, one
->driver
->textconv
);
1648 one
->driver
->textconv_cache
= c
;
1654 static void builtin_diff(const char *name_a
,
1656 struct diff_filespec
*one
,
1657 struct diff_filespec
*two
,
1658 const char *xfrm_msg
,
1659 struct diff_options
*o
,
1660 int complete_rewrite
)
1664 char *a_one
, *b_two
;
1665 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1666 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1667 const char *a_prefix
, *b_prefix
;
1668 struct userdiff_driver
*textconv_one
= NULL
;
1669 struct userdiff_driver
*textconv_two
= NULL
;
1670 struct strbuf header
= STRBUF_INIT
;
1672 if (DIFF_OPT_TST(o
, SUBMODULE_LOG
) &&
1673 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
1674 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
1675 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1676 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1677 show_submodule_summary(o
->file
, one
? one
->path
: two
->path
,
1678 one
->sha1
, two
->sha1
, two
->dirty_submodule
,
1683 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
1684 textconv_one
= get_textconv(one
);
1685 textconv_two
= get_textconv(two
);
1688 diff_set_mnemonic_prefix(o
, "a/", "b/");
1689 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
1690 a_prefix
= o
->b_prefix
;
1691 b_prefix
= o
->a_prefix
;
1693 a_prefix
= o
->a_prefix
;
1694 b_prefix
= o
->b_prefix
;
1697 /* Never use a non-valid filename anywhere if at all possible */
1698 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
1699 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
1701 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
1702 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
1703 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1704 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1705 strbuf_addf(&header
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1706 if (lbl
[0][0] == '/') {
1708 strbuf_addf(&header
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1709 if (xfrm_msg
&& xfrm_msg
[0])
1710 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1712 else if (lbl
[1][0] == '/') {
1713 strbuf_addf(&header
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1714 if (xfrm_msg
&& xfrm_msg
[0])
1715 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1718 if (one
->mode
!= two
->mode
) {
1719 strbuf_addf(&header
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1720 strbuf_addf(&header
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1722 if (xfrm_msg
&& xfrm_msg
[0])
1723 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1726 * we do not run diff between different kind
1729 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1730 goto free_ab_and_return
;
1731 if (complete_rewrite
&&
1732 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
1733 (textconv_two
|| !diff_filespec_is_binary(two
))) {
1734 fprintf(o
->file
, "%s", header
.buf
);
1735 strbuf_reset(&header
);
1736 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1737 textconv_one
, textconv_two
, o
);
1738 o
->found_changes
= 1;
1739 goto free_ab_and_return
;
1743 if (!DIFF_OPT_TST(o
, TEXT
) &&
1744 ( (!textconv_one
&& diff_filespec_is_binary(one
)) ||
1745 (!textconv_two
&& diff_filespec_is_binary(two
)) )) {
1746 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1747 die("unable to read files to diff");
1748 /* Quite common confusing case */
1749 if (mf1
.size
== mf2
.size
&&
1750 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1751 goto free_ab_and_return
;
1752 fprintf(o
->file
, "%s", header
.buf
);
1753 strbuf_reset(&header
);
1754 if (DIFF_OPT_TST(o
, BINARY
))
1755 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1757 fprintf(o
->file
, "Binary files %s and %s differ\n",
1759 o
->found_changes
= 1;
1762 /* Crazy xdl interfaces.. */
1763 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1766 struct emit_callback ecbdata
;
1767 const struct userdiff_funcname
*pe
;
1769 if (!DIFF_XDL_TST(o
, WHITESPACE_FLAGS
)) {
1770 fprintf(o
->file
, "%s", header
.buf
);
1771 strbuf_reset(&header
);
1774 mf1
.size
= fill_textconv(textconv_one
, one
, &mf1
.ptr
);
1775 mf2
.size
= fill_textconv(textconv_two
, two
, &mf2
.ptr
);
1777 pe
= diff_funcname_pattern(one
);
1779 pe
= diff_funcname_pattern(two
);
1781 memset(&xpp
, 0, sizeof(xpp
));
1782 memset(&xecfg
, 0, sizeof(xecfg
));
1783 memset(&ecbdata
, 0, sizeof(ecbdata
));
1784 ecbdata
.label_path
= lbl
;
1785 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1786 ecbdata
.found_changesp
= &o
->found_changes
;
1787 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1788 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
1789 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1790 ecbdata
.file
= o
->file
;
1791 ecbdata
.header
= header
.len
? &header
: NULL
;
1792 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1793 xecfg
.ctxlen
= o
->context
;
1794 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
1795 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1797 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
1800 else if (!prefixcmp(diffopts
, "--unified="))
1801 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1802 else if (!prefixcmp(diffopts
, "-u"))
1803 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1807 ecbdata
.diff_words
=
1808 xcalloc(1, sizeof(struct diff_words_data
));
1809 ecbdata
.diff_words
->file
= o
->file
;
1810 ecbdata
.diff_words
->type
= o
->word_diff
;
1812 o
->word_regex
= userdiff_word_regex(one
);
1814 o
->word_regex
= userdiff_word_regex(two
);
1816 o
->word_regex
= diff_word_regex_cfg
;
1817 if (o
->word_regex
) {
1818 ecbdata
.diff_words
->word_regex
= (regex_t
*)
1819 xmalloc(sizeof(regex_t
));
1820 if (regcomp(ecbdata
.diff_words
->word_regex
,
1822 REG_EXTENDED
| REG_NEWLINE
))
1823 die ("Invalid regular expression: %s",
1826 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
1827 if (o
->word_diff
== diff_words_styles
[i
].type
) {
1828 ecbdata
.diff_words
->style
=
1829 &diff_words_styles
[i
];
1833 if (DIFF_OPT_TST(o
, COLOR_DIFF
)) {
1834 struct diff_words_style
*st
= ecbdata
.diff_words
->style
;
1835 st
->old
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1836 st
->new.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1837 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_PLAIN
);
1840 xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
1843 free_diff_words_data(&ecbdata
);
1848 xdiff_clear_find_func(&xecfg
);
1852 strbuf_release(&header
);
1853 diff_free_filespec_data(one
);
1854 diff_free_filespec_data(two
);
1860 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1861 struct diff_filespec
*one
,
1862 struct diff_filespec
*two
,
1863 struct diffstat_t
*diffstat
,
1864 struct diff_options
*o
,
1865 int complete_rewrite
)
1868 struct diffstat_file
*data
;
1870 data
= diffstat_add(diffstat
, name_a
, name_b
);
1873 data
->is_unmerged
= 1;
1876 if (complete_rewrite
) {
1877 diff_populate_filespec(one
, 0);
1878 diff_populate_filespec(two
, 0);
1879 data
->deleted
= count_lines(one
->data
, one
->size
);
1880 data
->added
= count_lines(two
->data
, two
->size
);
1881 goto free_and_return
;
1883 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1884 die("unable to read files to diff");
1886 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1887 data
->is_binary
= 1;
1888 data
->added
= mf2
.size
;
1889 data
->deleted
= mf1
.size
;
1891 /* Crazy xdl interfaces.. */
1895 memset(&xpp
, 0, sizeof(xpp
));
1896 memset(&xecfg
, 0, sizeof(xecfg
));
1897 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1898 xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
1903 diff_free_filespec_data(one
);
1904 diff_free_filespec_data(two
);
1907 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1908 const char *attr_path
,
1909 struct diff_filespec
*one
,
1910 struct diff_filespec
*two
,
1911 struct diff_options
*o
)
1914 struct checkdiff_t data
;
1919 memset(&data
, 0, sizeof(data
));
1920 data
.filename
= name_b
? name_b
: name_a
;
1923 data
.ws_rule
= whitespace_rule(attr_path
);
1924 data
.conflict_marker_size
= ll_merge_marker_size(attr_path
);
1926 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1927 die("unable to read files to diff");
1930 * All the other codepaths check both sides, but not checking
1931 * the "old" side here is deliberate. We are checking the newly
1932 * introduced changes, and as long as the "new" side is text, we
1933 * can and should check what it introduces.
1935 if (diff_filespec_is_binary(two
))
1936 goto free_and_return
;
1938 /* Crazy xdl interfaces.. */
1942 memset(&xpp
, 0, sizeof(xpp
));
1943 memset(&xecfg
, 0, sizeof(xecfg
));
1944 xecfg
.ctxlen
= 1; /* at least one context line */
1945 xpp
.flags
= XDF_NEED_MINIMAL
;
1946 xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
1949 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
1950 struct emit_callback ecbdata
;
1953 ecbdata
.ws_rule
= data
.ws_rule
;
1954 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1955 blank_at_eof
= ecbdata
.blank_at_eof_in_preimage
;
1960 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
1961 fprintf(o
->file
, "%s:%d: %s.\n",
1962 data
.filename
, blank_at_eof
, err
);
1963 data
.status
= 1; /* report errors */
1968 diff_free_filespec_data(one
);
1969 diff_free_filespec_data(two
);
1971 DIFF_OPT_SET(o
, CHECK_FAILED
);
1974 struct diff_filespec
*alloc_filespec(const char *path
)
1976 int namelen
= strlen(path
);
1977 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1979 memset(spec
, 0, sizeof(*spec
));
1980 spec
->path
= (char *)(spec
+ 1);
1981 memcpy(spec
->path
, path
, namelen
+1);
1983 spec
->is_binary
= -1;
1987 void free_filespec(struct diff_filespec
*spec
)
1989 if (!--spec
->count
) {
1990 diff_free_filespec_data(spec
);
1995 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1996 unsigned short mode
)
1999 spec
->mode
= canon_mode(mode
);
2000 hashcpy(spec
->sha1
, sha1
);
2001 spec
->sha1_valid
= !is_null_sha1(sha1
);
2006 * Given a name and sha1 pair, if the index tells us the file in
2007 * the work tree has that object contents, return true, so that
2008 * prepare_temp_file() does not have to inflate and extract.
2010 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
2012 struct cache_entry
*ce
;
2017 * We do not read the cache ourselves here, because the
2018 * benchmark with my previous version that always reads cache
2019 * shows that it makes things worse for diff-tree comparing
2020 * two linux-2.6 kernel trees in an already checked out work
2021 * tree. This is because most diff-tree comparisons deal with
2022 * only a small number of files, while reading the cache is
2023 * expensive for a large project, and its cost outweighs the
2024 * savings we get by not inflating the object to a temporary
2025 * file. Practically, this code only helps when we are used
2026 * by diff-cache --cached, which does read the cache before
2032 /* We want to avoid the working directory if our caller
2033 * doesn't need the data in a normal file, this system
2034 * is rather slow with its stat/open/mmap/close syscalls,
2035 * and the object is contained in a pack file. The pack
2036 * is probably already open and will be faster to obtain
2037 * the data through than the working directory. Loose
2038 * objects however would tend to be slower as they need
2039 * to be individually opened and inflated.
2041 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
))
2045 pos
= cache_name_pos(name
, len
);
2048 ce
= active_cache
[pos
];
2051 * This is not the sha1 we are looking for, or
2052 * unreusable because it is not a regular file.
2054 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
2058 * If ce is marked as "assume unchanged", there is no
2059 * guarantee that work tree matches what we are looking for.
2061 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2065 * If ce matches the file in the work tree, we can reuse it.
2067 if (ce_uptodate(ce
) ||
2068 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
2074 static int populate_from_stdin(struct diff_filespec
*s
)
2076 struct strbuf buf
= STRBUF_INIT
;
2079 if (strbuf_read(&buf
, 0, 0) < 0)
2080 return error("error while reading from stdin %s",
2083 s
->should_munmap
= 0;
2084 s
->data
= strbuf_detach(&buf
, &size
);
2090 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
2093 char *data
= xmalloc(100), *dirty
= "";
2095 /* Are we looking at the work tree? */
2096 if (s
->dirty_submodule
)
2099 len
= snprintf(data
, 100,
2100 "Subproject commit %s%s\n", sha1_to_hex(s
->sha1
), dirty
);
2112 * While doing rename detection and pickaxe operation, we may need to
2113 * grab the data for the blob (or file) for our own in-core comparison.
2114 * diff_filespec has data and size fields for this purpose.
2116 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
2119 if (!DIFF_FILE_VALID(s
))
2120 die("internal error: asking to populate invalid file.");
2121 if (S_ISDIR(s
->mode
))
2127 if (size_only
&& 0 < s
->size
)
2130 if (S_ISGITLINK(s
->mode
))
2131 return diff_populate_gitlink(s
, size_only
);
2133 if (!s
->sha1_valid
||
2134 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
2135 struct strbuf buf
= STRBUF_INIT
;
2139 if (!strcmp(s
->path
, "-"))
2140 return populate_from_stdin(s
);
2142 if (lstat(s
->path
, &st
) < 0) {
2143 if (errno
== ENOENT
) {
2147 s
->data
= (char *)"";
2152 s
->size
= xsize_t(st
.st_size
);
2155 if (S_ISLNK(st
.st_mode
)) {
2156 struct strbuf sb
= STRBUF_INIT
;
2158 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
2161 s
->data
= strbuf_detach(&sb
, NULL
);
2167 fd
= open(s
->path
, O_RDONLY
);
2170 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2172 s
->should_munmap
= 1;
2175 * Convert from working tree format to canonical git format
2177 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
2179 munmap(s
->data
, s
->size
);
2180 s
->should_munmap
= 0;
2181 s
->data
= strbuf_detach(&buf
, &size
);
2187 enum object_type type
;
2189 type
= sha1_object_info(s
->sha1
, &s
->size
);
2191 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
2198 void diff_free_filespec_blob(struct diff_filespec
*s
)
2202 else if (s
->should_munmap
)
2203 munmap(s
->data
, s
->size
);
2205 if (s
->should_free
|| s
->should_munmap
) {
2206 s
->should_free
= s
->should_munmap
= 0;
2211 void diff_free_filespec_data(struct diff_filespec
*s
)
2213 diff_free_filespec_blob(s
);
2218 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
2221 const unsigned char *sha1
,
2225 struct strbuf buf
= STRBUF_INIT
;
2226 struct strbuf
template = STRBUF_INIT
;
2227 char *path_dup
= xstrdup(path
);
2228 const char *base
= basename(path_dup
);
2230 /* Generate "XXXXXX_basename.ext" */
2231 strbuf_addstr(&template, "XXXXXX_");
2232 strbuf_addstr(&template, base
);
2234 fd
= git_mkstemps(temp
->tmp_path
, PATH_MAX
, template.buf
,
2237 die_errno("unable to create temp-file");
2238 if (convert_to_working_tree(path
,
2239 (const char *)blob
, (size_t)size
, &buf
)) {
2243 if (write_in_full(fd
, blob
, size
) != size
)
2244 die_errno("unable to write temp-file");
2246 temp
->name
= temp
->tmp_path
;
2247 strcpy(temp
->hex
, sha1_to_hex(sha1
));
2249 sprintf(temp
->mode
, "%06o", mode
);
2250 strbuf_release(&buf
);
2251 strbuf_release(&template);
2255 static struct diff_tempfile
*prepare_temp_file(const char *name
,
2256 struct diff_filespec
*one
)
2258 struct diff_tempfile
*temp
= claim_diff_tempfile();
2260 if (!DIFF_FILE_VALID(one
)) {
2262 /* A '-' entry produces this for file-2, and
2263 * a '+' entry produces this for file-1.
2265 temp
->name
= "/dev/null";
2266 strcpy(temp
->hex
, ".");
2267 strcpy(temp
->mode
, ".");
2271 if (!remove_tempfile_installed
) {
2272 atexit(remove_tempfile
);
2273 sigchain_push_common(remove_tempfile_on_signal
);
2274 remove_tempfile_installed
= 1;
2277 if (!one
->sha1_valid
||
2278 reuse_worktree_file(name
, one
->sha1
, 1)) {
2280 if (lstat(name
, &st
) < 0) {
2281 if (errno
== ENOENT
)
2282 goto not_a_valid_file
;
2283 die_errno("stat(%s)", name
);
2285 if (S_ISLNK(st
.st_mode
)) {
2286 struct strbuf sb
= STRBUF_INIT
;
2287 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
2288 die_errno("readlink(%s)", name
);
2289 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
2291 one
->sha1
: null_sha1
),
2293 one
->mode
: S_IFLNK
));
2294 strbuf_release(&sb
);
2297 /* we can borrow from the file in the work tree */
2299 if (!one
->sha1_valid
)
2300 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
2302 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
2303 /* Even though we may sometimes borrow the
2304 * contents from the work tree, we always want
2305 * one->mode. mode is trustworthy even when
2306 * !(one->sha1_valid), as long as
2307 * DIFF_FILE_VALID(one).
2309 sprintf(temp
->mode
, "%06o", one
->mode
);
2314 if (diff_populate_filespec(one
, 0))
2315 die("cannot read data blob for %s", one
->path
);
2316 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
2317 one
->sha1
, one
->mode
);
2322 /* An external diff command takes:
2324 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2325 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2328 static void run_external_diff(const char *pgm
,
2331 struct diff_filespec
*one
,
2332 struct diff_filespec
*two
,
2333 const char *xfrm_msg
,
2334 int complete_rewrite
)
2336 const char *spawn_arg
[10];
2338 const char **arg
= &spawn_arg
[0];
2341 struct diff_tempfile
*temp_one
, *temp_two
;
2342 const char *othername
= (other
? other
: name
);
2343 temp_one
= prepare_temp_file(name
, one
);
2344 temp_two
= prepare_temp_file(othername
, two
);
2347 *arg
++ = temp_one
->name
;
2348 *arg
++ = temp_one
->hex
;
2349 *arg
++ = temp_one
->mode
;
2350 *arg
++ = temp_two
->name
;
2351 *arg
++ = temp_two
->hex
;
2352 *arg
++ = temp_two
->mode
;
2363 retval
= run_command_v_opt(spawn_arg
, RUN_USING_SHELL
);
2366 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2371 static int similarity_index(struct diff_filepair
*p
)
2373 return p
->score
* 100 / MAX_SCORE
;
2376 static void fill_metainfo(struct strbuf
*msg
,
2379 struct diff_filespec
*one
,
2380 struct diff_filespec
*two
,
2381 struct diff_options
*o
,
2382 struct diff_filepair
*p
)
2384 strbuf_init(msg
, PATH_MAX
* 2 + 300);
2385 switch (p
->status
) {
2386 case DIFF_STATUS_COPIED
:
2387 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2388 strbuf_addstr(msg
, "\ncopy from ");
2389 quote_c_style(name
, msg
, NULL
, 0);
2390 strbuf_addstr(msg
, "\ncopy to ");
2391 quote_c_style(other
, msg
, NULL
, 0);
2392 strbuf_addch(msg
, '\n');
2394 case DIFF_STATUS_RENAMED
:
2395 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2396 strbuf_addstr(msg
, "\nrename from ");
2397 quote_c_style(name
, msg
, NULL
, 0);
2398 strbuf_addstr(msg
, "\nrename to ");
2399 quote_c_style(other
, msg
, NULL
, 0);
2400 strbuf_addch(msg
, '\n');
2402 case DIFF_STATUS_MODIFIED
:
2404 strbuf_addf(msg
, "dissimilarity index %d%%\n",
2405 similarity_index(p
));
2413 if (one
&& two
&& hashcmp(one
->sha1
, two
->sha1
)) {
2414 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2416 if (DIFF_OPT_TST(o
, BINARY
)) {
2418 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2419 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2422 strbuf_addf(msg
, "index %.*s..%.*s",
2423 abbrev
, sha1_to_hex(one
->sha1
),
2424 abbrev
, sha1_to_hex(two
->sha1
));
2425 if (one
->mode
== two
->mode
)
2426 strbuf_addf(msg
, " %06o", one
->mode
);
2427 strbuf_addch(msg
, '\n');
2430 strbuf_setlen(msg
, msg
->len
- 1);
2433 static void run_diff_cmd(const char *pgm
,
2436 const char *attr_path
,
2437 struct diff_filespec
*one
,
2438 struct diff_filespec
*two
,
2440 struct diff_options
*o
,
2441 struct diff_filepair
*p
)
2443 const char *xfrm_msg
= NULL
;
2444 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
2447 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
);
2448 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
2451 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2454 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
2455 if (drv
&& drv
->external
)
2456 pgm
= drv
->external
;
2460 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2465 builtin_diff(name
, other
? other
: name
,
2466 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2468 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2471 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2473 if (DIFF_FILE_VALID(one
)) {
2474 if (!one
->sha1_valid
) {
2476 if (!strcmp(one
->path
, "-")) {
2477 hashcpy(one
->sha1
, null_sha1
);
2480 if (lstat(one
->path
, &st
) < 0)
2481 die_errno("stat '%s'", one
->path
);
2482 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2483 die("cannot hash %s", one
->path
);
2490 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2492 /* Strip the prefix but do not molest /dev/null and absolute paths */
2493 if (*namep
&& **namep
!= '/')
2494 *namep
+= prefix_length
;
2495 if (*otherp
&& **otherp
!= '/')
2496 *otherp
+= prefix_length
;
2499 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2501 const char *pgm
= external_diff();
2503 struct diff_filespec
*one
= p
->one
;
2504 struct diff_filespec
*two
= p
->two
;
2507 const char *attr_path
;
2509 name
= p
->one
->path
;
2510 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2512 if (o
->prefix_length
)
2513 strip_prefix(o
->prefix_length
, &name
, &other
);
2515 if (DIFF_PAIR_UNMERGED(p
)) {
2516 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2517 NULL
, NULL
, NULL
, o
, p
);
2521 diff_fill_sha1_info(one
);
2522 diff_fill_sha1_info(two
);
2525 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2526 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2528 * a filepair that changes between file and symlink
2529 * needs to be split into deletion and creation.
2531 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2532 run_diff_cmd(NULL
, name
, other
, attr_path
,
2533 one
, null
, &msg
, o
, p
);
2535 strbuf_release(&msg
);
2537 null
= alloc_filespec(one
->path
);
2538 run_diff_cmd(NULL
, name
, other
, attr_path
,
2539 null
, two
, &msg
, o
, p
);
2543 run_diff_cmd(pgm
, name
, other
, attr_path
,
2544 one
, two
, &msg
, o
, p
);
2546 strbuf_release(&msg
);
2549 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2550 struct diffstat_t
*diffstat
)
2554 int complete_rewrite
= 0;
2556 if (DIFF_PAIR_UNMERGED(p
)) {
2558 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2562 name
= p
->one
->path
;
2563 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2565 if (o
->prefix_length
)
2566 strip_prefix(o
->prefix_length
, &name
, &other
);
2568 diff_fill_sha1_info(p
->one
);
2569 diff_fill_sha1_info(p
->two
);
2571 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2572 complete_rewrite
= 1;
2573 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2576 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2580 const char *attr_path
;
2582 if (DIFF_PAIR_UNMERGED(p
)) {
2587 name
= p
->one
->path
;
2588 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2589 attr_path
= other
? other
: name
;
2591 if (o
->prefix_length
)
2592 strip_prefix(o
->prefix_length
, &name
, &other
);
2594 diff_fill_sha1_info(p
->one
);
2595 diff_fill_sha1_info(p
->two
);
2597 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2600 void diff_setup(struct diff_options
*options
)
2602 memset(options
, 0, sizeof(*options
));
2604 options
->file
= stdout
;
2606 options
->line_termination
= '\n';
2607 options
->break_opt
= -1;
2608 options
->rename_limit
= -1;
2609 options
->dirstat_percent
= 3;
2610 options
->context
= 3;
2612 options
->change
= diff_change
;
2613 options
->add_remove
= diff_addremove
;
2614 if (diff_use_color_default
> 0)
2615 DIFF_OPT_SET(options
, COLOR_DIFF
);
2616 options
->detect_rename
= diff_detect_rename_default
;
2618 if (!diff_mnemonic_prefix
) {
2619 options
->a_prefix
= "a/";
2620 options
->b_prefix
= "b/";
2624 int diff_setup_done(struct diff_options
*options
)
2628 if (options
->output_format
& DIFF_FORMAT_NAME
)
2630 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2632 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2634 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2637 die("--name-only, --name-status, --check and -s are mutually exclusive");
2640 * Most of the time we can say "there are changes"
2641 * only by checking if there are changed paths, but
2642 * --ignore-whitespace* options force us to look
2646 if (DIFF_XDL_TST(options
, IGNORE_WHITESPACE
) ||
2647 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_CHANGE
) ||
2648 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_AT_EOL
))
2649 DIFF_OPT_SET(options
, DIFF_FROM_CONTENTS
);
2651 DIFF_OPT_CLR(options
, DIFF_FROM_CONTENTS
);
2653 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2654 options
->detect_rename
= DIFF_DETECT_COPY
;
2656 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2657 options
->prefix
= NULL
;
2658 if (options
->prefix
)
2659 options
->prefix_length
= strlen(options
->prefix
);
2661 options
->prefix_length
= 0;
2663 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2664 DIFF_FORMAT_NAME_STATUS
|
2665 DIFF_FORMAT_CHECKDIFF
|
2666 DIFF_FORMAT_NO_OUTPUT
))
2667 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2668 DIFF_FORMAT_NUMSTAT
|
2669 DIFF_FORMAT_DIFFSTAT
|
2670 DIFF_FORMAT_SHORTSTAT
|
2671 DIFF_FORMAT_DIRSTAT
|
2672 DIFF_FORMAT_SUMMARY
|
2676 * These cases always need recursive; we do not drop caller-supplied
2677 * recursive bits for other formats here.
2679 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2680 DIFF_FORMAT_NUMSTAT
|
2681 DIFF_FORMAT_DIFFSTAT
|
2682 DIFF_FORMAT_SHORTSTAT
|
2683 DIFF_FORMAT_DIRSTAT
|
2684 DIFF_FORMAT_SUMMARY
|
2685 DIFF_FORMAT_CHECKDIFF
))
2686 DIFF_OPT_SET(options
, RECURSIVE
);
2688 * Also pickaxe would not work very well if you do not say recursive
2690 if (options
->pickaxe
)
2691 DIFF_OPT_SET(options
, RECURSIVE
);
2693 * When patches are generated, submodules diffed against the work tree
2694 * must be checked for dirtiness too so it can be shown in the output
2696 if (options
->output_format
& DIFF_FORMAT_PATCH
)
2697 DIFF_OPT_SET(options
, DIRTY_SUBMODULES
);
2699 if (options
->detect_rename
&& options
->rename_limit
< 0)
2700 options
->rename_limit
= diff_rename_limit_default
;
2701 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2703 /* read-cache does not die even when it fails
2704 * so it is safe for us to do this here. Also
2705 * it does not smudge active_cache or active_nr
2706 * when it fails, so we do not have to worry about
2707 * cleaning it up ourselves either.
2711 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2712 options
->abbrev
= 40; /* full */
2715 * It does not make sense to show the first hit we happened
2716 * to have found. It does not make sense not to return with
2717 * exit code in such a case either.
2719 if (DIFF_OPT_TST(options
, QUICK
)) {
2720 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2721 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2727 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2737 if (c
== arg_short
) {
2741 if (val
&& isdigit(c
)) {
2743 int n
= strtoul(arg
, &end
, 10);
2754 eq
= strchr(arg
, '=');
2759 if (!len
|| strncmp(arg
, arg_long
, len
))
2764 if (!isdigit(*++eq
))
2766 n
= strtoul(eq
, &end
, 10);
2774 static int diff_scoreopt_parse(const char *opt
);
2776 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2778 const char *arg
= av
[0];
2780 /* Output format options */
2781 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u") || !strcmp(arg
, "--patch"))
2782 options
->output_format
|= DIFF_FORMAT_PATCH
;
2783 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2784 options
->output_format
|= DIFF_FORMAT_PATCH
;
2785 else if (!strcmp(arg
, "--raw"))
2786 options
->output_format
|= DIFF_FORMAT_RAW
;
2787 else if (!strcmp(arg
, "--patch-with-raw"))
2788 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2789 else if (!strcmp(arg
, "--numstat"))
2790 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2791 else if (!strcmp(arg
, "--shortstat"))
2792 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2793 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2794 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2795 else if (!strcmp(arg
, "--cumulative")) {
2796 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2797 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
2798 } else if (opt_arg(arg
, 0, "dirstat-by-file",
2799 &options
->dirstat_percent
)) {
2800 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2801 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
2803 else if (!strcmp(arg
, "--check"))
2804 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2805 else if (!strcmp(arg
, "--summary"))
2806 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2807 else if (!strcmp(arg
, "--patch-with-stat"))
2808 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2809 else if (!strcmp(arg
, "--name-only"))
2810 options
->output_format
|= DIFF_FORMAT_NAME
;
2811 else if (!strcmp(arg
, "--name-status"))
2812 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2813 else if (!strcmp(arg
, "-s"))
2814 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2815 else if (!prefixcmp(arg
, "--stat")) {
2817 int width
= options
->stat_width
;
2818 int name_width
= options
->stat_name_width
;
2824 if (!prefixcmp(arg
, "-width="))
2825 width
= strtoul(arg
+ 7, &end
, 10);
2826 else if (!prefixcmp(arg
, "-name-width="))
2827 name_width
= strtoul(arg
+ 12, &end
, 10);
2830 width
= strtoul(arg
+1, &end
, 10);
2832 name_width
= strtoul(end
+1, &end
, 10);
2835 /* Important! This checks all the error cases! */
2838 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2839 options
->stat_name_width
= name_width
;
2840 options
->stat_width
= width
;
2843 /* renames options */
2844 else if (!prefixcmp(arg
, "-B")) {
2845 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2848 else if (!prefixcmp(arg
, "-M")) {
2849 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2851 options
->detect_rename
= DIFF_DETECT_RENAME
;
2853 else if (!prefixcmp(arg
, "-C")) {
2854 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2855 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2856 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2858 options
->detect_rename
= DIFF_DETECT_COPY
;
2860 else if (!strcmp(arg
, "--no-renames"))
2861 options
->detect_rename
= 0;
2862 else if (!strcmp(arg
, "--relative"))
2863 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2864 else if (!prefixcmp(arg
, "--relative=")) {
2865 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2866 options
->prefix
= arg
+ 11;
2870 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2871 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
2872 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2873 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
2874 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2875 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
2876 else if (!strcmp(arg
, "--patience"))
2877 DIFF_XDL_SET(options
, PATIENCE_DIFF
);
2880 else if (!strcmp(arg
, "--binary")) {
2881 options
->output_format
|= DIFF_FORMAT_PATCH
;
2882 DIFF_OPT_SET(options
, BINARY
);
2884 else if (!strcmp(arg
, "--full-index"))
2885 DIFF_OPT_SET(options
, FULL_INDEX
);
2886 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2887 DIFF_OPT_SET(options
, TEXT
);
2888 else if (!strcmp(arg
, "-R"))
2889 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2890 else if (!strcmp(arg
, "--find-copies-harder"))
2891 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2892 else if (!strcmp(arg
, "--follow"))
2893 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2894 else if (!strcmp(arg
, "--color"))
2895 DIFF_OPT_SET(options
, COLOR_DIFF
);
2896 else if (!prefixcmp(arg
, "--color=")) {
2897 int value
= git_config_colorbool(NULL
, arg
+8, -1);
2899 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2901 DIFF_OPT_SET(options
, COLOR_DIFF
);
2903 return error("option `color' expects \"always\", \"auto\", or \"never\"");
2905 else if (!strcmp(arg
, "--no-color"))
2906 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2907 else if (!strcmp(arg
, "--color-words")) {
2908 DIFF_OPT_SET(options
, COLOR_DIFF
);
2909 options
->word_diff
= DIFF_WORDS_COLOR
;
2911 else if (!prefixcmp(arg
, "--color-words=")) {
2912 DIFF_OPT_SET(options
, COLOR_DIFF
);
2913 options
->word_diff
= DIFF_WORDS_COLOR
;
2914 options
->word_regex
= arg
+ 14;
2916 else if (!strcmp(arg
, "--word-diff")) {
2917 if (options
->word_diff
== DIFF_WORDS_NONE
)
2918 options
->word_diff
= DIFF_WORDS_PLAIN
;
2920 else if (!prefixcmp(arg
, "--word-diff=")) {
2921 const char *type
= arg
+ 12;
2922 if (!strcmp(type
, "plain"))
2923 options
->word_diff
= DIFF_WORDS_PLAIN
;
2924 else if (!strcmp(type
, "color")) {
2925 DIFF_OPT_SET(options
, COLOR_DIFF
);
2926 options
->word_diff
= DIFF_WORDS_COLOR
;
2928 else if (!strcmp(type
, "porcelain"))
2929 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
2930 else if (!strcmp(type
, "none"))
2931 options
->word_diff
= DIFF_WORDS_NONE
;
2933 die("bad --word-diff argument: %s", type
);
2935 else if (!prefixcmp(arg
, "--word-diff-regex=")) {
2936 if (options
->word_diff
== DIFF_WORDS_NONE
)
2937 options
->word_diff
= DIFF_WORDS_PLAIN
;
2938 options
->word_regex
= arg
+ 18;
2940 else if (!strcmp(arg
, "--exit-code"))
2941 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2942 else if (!strcmp(arg
, "--quiet"))
2943 DIFF_OPT_SET(options
, QUICK
);
2944 else if (!strcmp(arg
, "--ext-diff"))
2945 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2946 else if (!strcmp(arg
, "--no-ext-diff"))
2947 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2948 else if (!strcmp(arg
, "--textconv"))
2949 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
2950 else if (!strcmp(arg
, "--no-textconv"))
2951 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
2952 else if (!strcmp(arg
, "--ignore-submodules"))
2953 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
2954 else if (!strcmp(arg
, "--submodule"))
2955 DIFF_OPT_SET(options
, SUBMODULE_LOG
);
2956 else if (!prefixcmp(arg
, "--submodule=")) {
2957 if (!strcmp(arg
+ 12, "log"))
2958 DIFF_OPT_SET(options
, SUBMODULE_LOG
);
2962 else if (!strcmp(arg
, "-z"))
2963 options
->line_termination
= 0;
2964 else if (!prefixcmp(arg
, "-l"))
2965 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2966 else if (!prefixcmp(arg
, "-S"))
2967 options
->pickaxe
= arg
+ 2;
2968 else if (!strcmp(arg
, "--pickaxe-all"))
2969 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2970 else if (!strcmp(arg
, "--pickaxe-regex"))
2971 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2972 else if (!prefixcmp(arg
, "-O"))
2973 options
->orderfile
= arg
+ 2;
2974 else if (!prefixcmp(arg
, "--diff-filter="))
2975 options
->filter
= arg
+ 14;
2976 else if (!strcmp(arg
, "--abbrev"))
2977 options
->abbrev
= DEFAULT_ABBREV
;
2978 else if (!prefixcmp(arg
, "--abbrev=")) {
2979 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2980 if (options
->abbrev
< MINIMUM_ABBREV
)
2981 options
->abbrev
= MINIMUM_ABBREV
;
2982 else if (40 < options
->abbrev
)
2983 options
->abbrev
= 40;
2985 else if (!prefixcmp(arg
, "--src-prefix="))
2986 options
->a_prefix
= arg
+ 13;
2987 else if (!prefixcmp(arg
, "--dst-prefix="))
2988 options
->b_prefix
= arg
+ 13;
2989 else if (!strcmp(arg
, "--no-prefix"))
2990 options
->a_prefix
= options
->b_prefix
= "";
2991 else if (opt_arg(arg
, '\0', "inter-hunk-context",
2992 &options
->interhunkcontext
))
2994 else if (!prefixcmp(arg
, "--output=")) {
2995 options
->file
= fopen(arg
+ strlen("--output="), "w");
2997 die_errno("Could not open '%s'", arg
+ strlen("--output="));
2998 options
->close_file
= 1;
3004 static int parse_num(const char **cp_p
)
3006 unsigned long num
, scale
;
3008 const char *cp
= *cp_p
;
3015 if ( !dot
&& ch
== '.' ) {
3018 } else if ( ch
== '%' ) {
3019 scale
= dot
? scale
*100 : 100;
3020 cp
++; /* % is always at the end */
3022 } else if ( ch
>= '0' && ch
<= '9' ) {
3023 if ( scale
< 100000 ) {
3025 num
= (num
*10) + (ch
-'0');
3034 /* user says num divided by scale and we say internally that
3035 * is MAX_SCORE * num / scale.
3037 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
3040 static int diff_scoreopt_parse(const char *opt
)
3042 int opt1
, opt2
, cmd
;
3047 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
3048 return -1; /* that is not a -M, -C nor -B option */
3050 opt1
= parse_num(&opt
);
3056 else if (*opt
!= '/')
3057 return -1; /* we expect -B80/99 or -B80 */
3060 opt2
= parse_num(&opt
);
3065 return opt1
| (opt2
<< 16);
3068 struct diff_queue_struct diff_queued_diff
;
3070 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
3072 if (queue
->alloc
<= queue
->nr
) {
3073 queue
->alloc
= alloc_nr(queue
->alloc
);
3074 queue
->queue
= xrealloc(queue
->queue
,
3075 sizeof(dp
) * queue
->alloc
);
3077 queue
->queue
[queue
->nr
++] = dp
;
3080 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
3081 struct diff_filespec
*one
,
3082 struct diff_filespec
*two
)
3084 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
3092 void diff_free_filepair(struct diff_filepair
*p
)
3094 free_filespec(p
->one
);
3095 free_filespec(p
->two
);
3099 /* This is different from find_unique_abbrev() in that
3100 * it stuffs the result with dots for alignment.
3102 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
3107 return sha1_to_hex(sha1
);
3109 abbrev
= find_unique_abbrev(sha1
, len
);
3110 abblen
= strlen(abbrev
);
3112 static char hex
[41];
3113 if (len
< abblen
&& abblen
<= len
+ 2)
3114 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
3116 sprintf(hex
, "%s...", abbrev
);
3119 return sha1_to_hex(sha1
);
3122 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
3124 int line_termination
= opt
->line_termination
;
3125 int inter_name_termination
= line_termination
? '\t' : '\0';
3127 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
3128 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
3129 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
3130 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
3133 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
3134 inter_name_termination
);
3136 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
3139 if (p
->status
== DIFF_STATUS_COPIED
||
3140 p
->status
== DIFF_STATUS_RENAMED
) {
3141 const char *name_a
, *name_b
;
3142 name_a
= p
->one
->path
;
3143 name_b
= p
->two
->path
;
3144 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3145 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
3146 write_name_quoted(name_b
, opt
->file
, line_termination
);
3148 const char *name_a
, *name_b
;
3149 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
3151 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3152 write_name_quoted(name_a
, opt
->file
, line_termination
);
3156 int diff_unmodified_pair(struct diff_filepair
*p
)
3158 /* This function is written stricter than necessary to support
3159 * the currently implemented transformers, but the idea is to
3160 * let transformers to produce diff_filepairs any way they want,
3161 * and filter and clean them up here before producing the output.
3163 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
3165 if (DIFF_PAIR_UNMERGED(p
))
3166 return 0; /* unmerged is interesting */
3168 /* deletion, addition, mode or type change
3169 * and rename are all interesting.
3171 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
3172 DIFF_PAIR_MODE_CHANGED(p
) ||
3173 strcmp(one
->path
, two
->path
))
3176 /* both are valid and point at the same path. that is, we are
3177 * dealing with a change.
3179 if (one
->sha1_valid
&& two
->sha1_valid
&&
3180 !hashcmp(one
->sha1
, two
->sha1
) &&
3181 !one
->dirty_submodule
&& !two
->dirty_submodule
)
3182 return 1; /* no change */
3183 if (!one
->sha1_valid
&& !two
->sha1_valid
)
3184 return 1; /* both look at the same file on the filesystem. */
3188 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
3190 if (diff_unmodified_pair(p
))
3193 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3194 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3195 return; /* no tree diffs in patch format */
3200 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
3201 struct diffstat_t
*diffstat
)
3203 if (diff_unmodified_pair(p
))
3206 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3207 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3208 return; /* no tree diffs in patch format */
3210 run_diffstat(p
, o
, diffstat
);
3213 static void diff_flush_checkdiff(struct diff_filepair
*p
,
3214 struct diff_options
*o
)
3216 if (diff_unmodified_pair(p
))
3219 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3220 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3221 return; /* no tree diffs in patch format */
3223 run_checkdiff(p
, o
);
3226 int diff_queue_is_empty(void)
3228 struct diff_queue_struct
*q
= &diff_queued_diff
;
3230 for (i
= 0; i
< q
->nr
; i
++)
3231 if (!diff_unmodified_pair(q
->queue
[i
]))
3237 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
3239 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
3242 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
3244 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
3245 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
3247 s
->size
, s
->xfrm_flags
);
3250 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
3252 diff_debug_filespec(p
->one
, i
, "one");
3253 diff_debug_filespec(p
->two
, i
, "two");
3254 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
3255 p
->score
, p
->status
? p
->status
: '?',
3256 p
->one
->rename_used
, p
->broken_pair
);
3259 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
3263 fprintf(stderr
, "%s\n", msg
);
3264 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
3265 for (i
= 0; i
< q
->nr
; i
++) {
3266 struct diff_filepair
*p
= q
->queue
[i
];
3267 diff_debug_filepair(p
, i
);
3272 static void diff_resolve_rename_copy(void)
3275 struct diff_filepair
*p
;
3276 struct diff_queue_struct
*q
= &diff_queued_diff
;
3278 diff_debug_queue("resolve-rename-copy", q
);
3280 for (i
= 0; i
< q
->nr
; i
++) {
3282 p
->status
= 0; /* undecided */
3283 if (DIFF_PAIR_UNMERGED(p
))
3284 p
->status
= DIFF_STATUS_UNMERGED
;
3285 else if (!DIFF_FILE_VALID(p
->one
))
3286 p
->status
= DIFF_STATUS_ADDED
;
3287 else if (!DIFF_FILE_VALID(p
->two
))
3288 p
->status
= DIFF_STATUS_DELETED
;
3289 else if (DIFF_PAIR_TYPE_CHANGED(p
))
3290 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
3292 /* from this point on, we are dealing with a pair
3293 * whose both sides are valid and of the same type, i.e.
3294 * either in-place edit or rename/copy edit.
3296 else if (DIFF_PAIR_RENAME(p
)) {
3298 * A rename might have re-connected a broken
3299 * pair up, causing the pathnames to be the
3300 * same again. If so, that's not a rename at
3301 * all, just a modification..
3303 * Otherwise, see if this source was used for
3304 * multiple renames, in which case we decrement
3305 * the count, and call it a copy.
3307 if (!strcmp(p
->one
->path
, p
->two
->path
))
3308 p
->status
= DIFF_STATUS_MODIFIED
;
3309 else if (--p
->one
->rename_used
> 0)
3310 p
->status
= DIFF_STATUS_COPIED
;
3312 p
->status
= DIFF_STATUS_RENAMED
;
3314 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
3315 p
->one
->mode
!= p
->two
->mode
||
3316 p
->one
->dirty_submodule
||
3317 p
->two
->dirty_submodule
||
3318 is_null_sha1(p
->one
->sha1
))
3319 p
->status
= DIFF_STATUS_MODIFIED
;
3321 /* This is a "no-change" entry and should not
3322 * happen anymore, but prepare for broken callers.
3324 error("feeding unmodified %s to diffcore",
3326 p
->status
= DIFF_STATUS_UNKNOWN
;
3329 diff_debug_queue("resolve-rename-copy done", q
);
3332 static int check_pair_status(struct diff_filepair
*p
)
3334 switch (p
->status
) {
3335 case DIFF_STATUS_UNKNOWN
:
3338 die("internal error in diff-resolve-rename-copy");
3344 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
3346 int fmt
= opt
->output_format
;
3348 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
3349 diff_flush_checkdiff(p
, opt
);
3350 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
3351 diff_flush_raw(p
, opt
);
3352 else if (fmt
& DIFF_FORMAT_NAME
) {
3353 const char *name_a
, *name_b
;
3354 name_a
= p
->two
->path
;
3356 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3357 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
3361 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
3364 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
3366 fprintf(file
, " %s ", newdelete
);
3367 write_name_quoted(fs
->path
, file
, '\n');
3371 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
3373 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
3374 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
3375 show_name
? ' ' : '\n');
3377 write_name_quoted(p
->two
->path
, file
, '\n');
3382 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
3384 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
3386 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
3388 show_mode_change(file
, p
, 0);
3391 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
3394 case DIFF_STATUS_DELETED
:
3395 show_file_mode_name(file
, "delete", p
->one
);
3397 case DIFF_STATUS_ADDED
:
3398 show_file_mode_name(file
, "create", p
->two
);
3400 case DIFF_STATUS_COPIED
:
3401 show_rename_copy(file
, "copy", p
);
3403 case DIFF_STATUS_RENAMED
:
3404 show_rename_copy(file
, "rename", p
);
3408 fputs(" rewrite ", file
);
3409 write_name_quoted(p
->two
->path
, file
, ' ');
3410 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3412 show_mode_change(file
, p
, !p
->score
);
3422 static int remove_space(char *line
, int len
)
3428 for (i
= 0; i
< len
; i
++)
3429 if (!isspace((c
= line
[i
])))
3435 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3437 struct patch_id_t
*data
= priv
;
3440 /* Ignore line numbers when computing the SHA1 of the patch */
3441 if (!prefixcmp(line
, "@@ -"))
3444 new_len
= remove_space(line
, len
);
3446 git_SHA1_Update(data
->ctx
, line
, new_len
);
3447 data
->patchlen
+= new_len
;
3450 /* returns 0 upon success, and writes result into sha1 */
3451 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3453 struct diff_queue_struct
*q
= &diff_queued_diff
;
3456 struct patch_id_t data
;
3457 char buffer
[PATH_MAX
* 4 + 20];
3459 git_SHA1_Init(&ctx
);
3460 memset(&data
, 0, sizeof(struct patch_id_t
));
3463 for (i
= 0; i
< q
->nr
; i
++) {
3467 struct diff_filepair
*p
= q
->queue
[i
];
3470 memset(&xpp
, 0, sizeof(xpp
));
3471 memset(&xecfg
, 0, sizeof(xecfg
));
3473 return error("internal diff status error");
3474 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3476 if (diff_unmodified_pair(p
))
3478 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3479 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3481 if (DIFF_PAIR_UNMERGED(p
))
3484 diff_fill_sha1_info(p
->one
);
3485 diff_fill_sha1_info(p
->two
);
3486 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3487 fill_mmfile(&mf2
, p
->two
) < 0)
3488 return error("unable to read files to diff");
3490 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3491 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3492 if (p
->one
->mode
== 0)
3493 len1
= snprintf(buffer
, sizeof(buffer
),
3494 "diff--gita/%.*sb/%.*s"
3501 len2
, p
->two
->path
);
3502 else if (p
->two
->mode
== 0)
3503 len1
= snprintf(buffer
, sizeof(buffer
),
3504 "diff--gita/%.*sb/%.*s"
3505 "deletedfilemode%06o"
3511 len1
, p
->one
->path
);
3513 len1
= snprintf(buffer
, sizeof(buffer
),
3514 "diff--gita/%.*sb/%.*s"
3520 len2
, p
->two
->path
);
3521 git_SHA1_Update(&ctx
, buffer
, len1
);
3523 xpp
.flags
= XDF_NEED_MINIMAL
;
3525 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3526 xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
3530 git_SHA1_Final(sha1
, &ctx
);
3534 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3536 struct diff_queue_struct
*q
= &diff_queued_diff
;
3538 int result
= diff_get_patch_id(options
, sha1
);
3540 for (i
= 0; i
< q
->nr
; i
++)
3541 diff_free_filepair(q
->queue
[i
]);
3545 q
->nr
= q
->alloc
= 0;
3550 static int is_summary_empty(const struct diff_queue_struct
*q
)
3554 for (i
= 0; i
< q
->nr
; i
++) {
3555 const struct diff_filepair
*p
= q
->queue
[i
];
3557 switch (p
->status
) {
3558 case DIFF_STATUS_DELETED
:
3559 case DIFF_STATUS_ADDED
:
3560 case DIFF_STATUS_COPIED
:
3561 case DIFF_STATUS_RENAMED
:
3566 if (p
->one
->mode
&& p
->two
->mode
&&
3567 p
->one
->mode
!= p
->two
->mode
)
3575 void diff_flush(struct diff_options
*options
)
3577 struct diff_queue_struct
*q
= &diff_queued_diff
;
3578 int i
, output_format
= options
->output_format
;
3582 * Order: raw, stat, summary, patch
3583 * or: name/name-status/checkdiff (other bits clear)
3588 if (output_format
& (DIFF_FORMAT_RAW
|
3590 DIFF_FORMAT_NAME_STATUS
|
3591 DIFF_FORMAT_CHECKDIFF
)) {
3592 for (i
= 0; i
< q
->nr
; i
++) {
3593 struct diff_filepair
*p
= q
->queue
[i
];
3594 if (check_pair_status(p
))
3595 flush_one_pair(p
, options
);
3600 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3601 struct diffstat_t diffstat
;
3603 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3604 for (i
= 0; i
< q
->nr
; i
++) {
3605 struct diff_filepair
*p
= q
->queue
[i
];
3606 if (check_pair_status(p
))
3607 diff_flush_stat(p
, options
, &diffstat
);
3609 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3610 show_numstat(&diffstat
, options
);
3611 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3612 show_stats(&diffstat
, options
);
3613 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3614 show_shortstats(&diffstat
, options
);
3615 free_diffstat_info(&diffstat
);
3618 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3619 show_dirstat(options
);
3621 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3622 for (i
= 0; i
< q
->nr
; i
++)
3623 diff_summary(options
->file
, q
->queue
[i
]);
3627 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
3628 DIFF_OPT_TST(options
, EXIT_WITH_STATUS
) &&
3629 DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
3631 * run diff_flush_patch for the exit status. setting
3632 * options->file to /dev/null should be safe, becaue we
3633 * aren't supposed to produce any output anyway.
3635 if (options
->close_file
)
3636 fclose(options
->file
);
3637 options
->file
= fopen("/dev/null", "w");
3639 die_errno("Could not open /dev/null");
3640 options
->close_file
= 1;
3641 for (i
= 0; i
< q
->nr
; i
++) {
3642 struct diff_filepair
*p
= q
->queue
[i
];
3643 if (check_pair_status(p
))
3644 diff_flush_patch(p
, options
);
3645 if (options
->found_changes
)
3650 if (output_format
& DIFF_FORMAT_PATCH
) {
3652 putc(options
->line_termination
, options
->file
);
3653 if (options
->stat_sep
) {
3654 /* attach patch instead of inline */
3655 fputs(options
->stat_sep
, options
->file
);
3659 for (i
= 0; i
< q
->nr
; i
++) {
3660 struct diff_filepair
*p
= q
->queue
[i
];
3661 if (check_pair_status(p
))
3662 diff_flush_patch(p
, options
);
3666 if (output_format
& DIFF_FORMAT_CALLBACK
)
3667 options
->format_callback(q
, options
, options
->format_callback_data
);
3669 for (i
= 0; i
< q
->nr
; i
++)
3670 diff_free_filepair(q
->queue
[i
]);
3674 q
->nr
= q
->alloc
= 0;
3675 if (options
->close_file
)
3676 fclose(options
->file
);
3679 * Report the content-level differences with HAS_CHANGES;
3680 * diff_addremove/diff_change does not set the bit when
3681 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
3683 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
3684 if (options
->found_changes
)
3685 DIFF_OPT_SET(options
, HAS_CHANGES
);
3687 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3691 static void diffcore_apply_filter(const char *filter
)
3694 struct diff_queue_struct
*q
= &diff_queued_diff
;
3695 struct diff_queue_struct outq
;
3697 outq
.nr
= outq
.alloc
= 0;
3702 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3704 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3705 struct diff_filepair
*p
= q
->queue
[i
];
3706 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3708 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3710 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3711 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3712 strchr(filter
, p
->status
)))
3718 /* otherwise we will clear the whole queue
3719 * by copying the empty outq at the end of this
3720 * function, but first clear the current entries
3723 for (i
= 0; i
< q
->nr
; i
++)
3724 diff_free_filepair(q
->queue
[i
]);
3727 /* Only the matching ones */
3728 for (i
= 0; i
< q
->nr
; i
++) {
3729 struct diff_filepair
*p
= q
->queue
[i
];
3731 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3733 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3735 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3736 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3737 strchr(filter
, p
->status
)))
3740 diff_free_filepair(p
);
3747 /* Check whether two filespecs with the same mode and size are identical */
3748 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3749 struct diff_filespec
*two
)
3751 if (S_ISGITLINK(one
->mode
))
3753 if (diff_populate_filespec(one
, 0))
3755 if (diff_populate_filespec(two
, 0))
3757 return !memcmp(one
->data
, two
->data
, one
->size
);
3760 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3763 struct diff_queue_struct
*q
= &diff_queued_diff
;
3764 struct diff_queue_struct outq
;
3766 outq
.nr
= outq
.alloc
= 0;
3768 for (i
= 0; i
< q
->nr
; i
++) {
3769 struct diff_filepair
*p
= q
->queue
[i
];
3772 * 1. Entries that come from stat info dirtiness
3773 * always have both sides (iow, not create/delete),
3774 * one side of the object name is unknown, with
3775 * the same mode and size. Keep the ones that
3776 * do not match these criteria. They have real
3779 * 2. At this point, the file is known to be modified,
3780 * with the same mode and size, and the object
3781 * name of one side is unknown. Need to inspect
3782 * the identical contents.
3784 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3785 !DIFF_FILE_VALID(p
->two
) ||
3786 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3787 (p
->one
->mode
!= p
->two
->mode
) ||
3788 diff_populate_filespec(p
->one
, 1) ||
3789 diff_populate_filespec(p
->two
, 1) ||
3790 (p
->one
->size
!= p
->two
->size
) ||
3791 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3795 * The caller can subtract 1 from skip_stat_unmatch
3796 * to determine how many paths were dirty only
3797 * due to stat info mismatch.
3799 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3800 diffopt
->skip_stat_unmatch
++;
3801 diff_free_filepair(p
);
3808 static int diffnamecmp(const void *a_
, const void *b_
)
3810 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
3811 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
3812 const char *name_a
, *name_b
;
3814 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
3815 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
3816 return strcmp(name_a
, name_b
);
3819 void diffcore_fix_diff_index(struct diff_options
*options
)
3821 struct diff_queue_struct
*q
= &diff_queued_diff
;
3822 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), diffnamecmp
);
3825 void diffcore_std(struct diff_options
*options
)
3827 if (options
->skip_stat_unmatch
)
3828 diffcore_skip_stat_unmatch(options
);
3829 if (options
->break_opt
!= -1)
3830 diffcore_break(options
->break_opt
);
3831 if (options
->detect_rename
)
3832 diffcore_rename(options
);
3833 if (options
->break_opt
!= -1)
3834 diffcore_merge_broken();
3835 if (options
->pickaxe
)
3836 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3837 if (options
->orderfile
)
3838 diffcore_order(options
->orderfile
);
3839 diff_resolve_rename_copy();
3840 diffcore_apply_filter(options
->filter
);
3842 if (diff_queued_diff
.nr
&& !DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3843 DIFF_OPT_SET(options
, HAS_CHANGES
);
3845 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3848 int diff_result_code(struct diff_options
*opt
, int status
)
3851 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3852 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3854 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3855 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3857 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3858 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3863 void diff_addremove(struct diff_options
*options
,
3864 int addremove
, unsigned mode
,
3865 const unsigned char *sha1
,
3866 const char *concatpath
, unsigned dirty_submodule
)
3868 struct diff_filespec
*one
, *two
;
3870 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
3873 /* This may look odd, but it is a preparation for
3874 * feeding "there are unchanged files which should
3875 * not produce diffs, but when you are doing copy
3876 * detection you would need them, so here they are"
3877 * entries to the diff-core. They will be prefixed
3878 * with something like '=' or '*' (I haven't decided
3879 * which but should not make any difference).
3880 * Feeding the same new and old to diff_change()
3881 * also has the same effect.
3882 * Before the final output happens, they are pruned after
3883 * merged into rename/copy pairs as appropriate.
3885 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3886 addremove
= (addremove
== '+' ? '-' :
3887 addremove
== '-' ? '+' : addremove
);
3889 if (options
->prefix
&&
3890 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3893 one
= alloc_filespec(concatpath
);
3894 two
= alloc_filespec(concatpath
);
3896 if (addremove
!= '+')
3897 fill_filespec(one
, sha1
, mode
);
3898 if (addremove
!= '-') {
3899 fill_filespec(two
, sha1
, mode
);
3900 two
->dirty_submodule
= dirty_submodule
;
3903 diff_queue(&diff_queued_diff
, one
, two
);
3904 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3905 DIFF_OPT_SET(options
, HAS_CHANGES
);
3908 void diff_change(struct diff_options
*options
,
3909 unsigned old_mode
, unsigned new_mode
,
3910 const unsigned char *old_sha1
,
3911 const unsigned char *new_sha1
,
3912 const char *concatpath
,
3913 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
3915 struct diff_filespec
*one
, *two
;
3917 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
3918 && S_ISGITLINK(new_mode
))
3921 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3923 const unsigned char *tmp_c
;
3924 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3925 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3926 tmp
= old_dirty_submodule
; old_dirty_submodule
= new_dirty_submodule
;
3927 new_dirty_submodule
= tmp
;
3930 if (options
->prefix
&&
3931 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3934 one
= alloc_filespec(concatpath
);
3935 two
= alloc_filespec(concatpath
);
3936 fill_filespec(one
, old_sha1
, old_mode
);
3937 fill_filespec(two
, new_sha1
, new_mode
);
3938 one
->dirty_submodule
= old_dirty_submodule
;
3939 two
->dirty_submodule
= new_dirty_submodule
;
3941 diff_queue(&diff_queued_diff
, one
, two
);
3942 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3943 DIFF_OPT_SET(options
, HAS_CHANGES
);
3946 void diff_unmerge(struct diff_options
*options
,
3948 unsigned mode
, const unsigned char *sha1
)
3950 struct diff_filespec
*one
, *two
;
3952 if (options
->prefix
&&
3953 strncmp(path
, options
->prefix
, options
->prefix_length
))
3956 one
= alloc_filespec(path
);
3957 two
= alloc_filespec(path
);
3958 fill_filespec(one
, sha1
, mode
);
3959 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;
3962 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
3965 struct diff_tempfile
*temp
;
3966 const char *argv
[3];
3967 const char **arg
= argv
;
3968 struct child_process child
;
3969 struct strbuf buf
= STRBUF_INIT
;
3972 temp
= prepare_temp_file(spec
->path
, spec
);
3974 *arg
++ = temp
->name
;
3977 memset(&child
, 0, sizeof(child
));
3978 child
.use_shell
= 1;
3981 if (start_command(&child
)) {
3986 if (strbuf_read(&buf
, child
.out
, 0) < 0)
3987 err
= error("error reading from textconv command '%s'", pgm
);
3990 if (finish_command(&child
) || err
) {
3991 strbuf_release(&buf
);
3997 return strbuf_detach(&buf
, outsize
);
4000 static size_t fill_textconv(struct userdiff_driver
*driver
,
4001 struct diff_filespec
*df
,
4006 if (!driver
|| !driver
->textconv
) {
4007 if (!DIFF_FILE_VALID(df
)) {
4011 if (diff_populate_filespec(df
, 0))
4012 die("unable to read files to diff");
4017 if (driver
->textconv_cache
) {
4018 *outbuf
= notes_cache_get(driver
->textconv_cache
, df
->sha1
,
4024 *outbuf
= run_textconv(driver
->textconv
, df
, &size
);
4026 die("unable to read files to diff");
4028 if (driver
->textconv_cache
) {
4029 /* ignore errors, as we might be in a readonly repository */
4030 notes_cache_put(driver
->textconv_cache
, df
->sha1
, *outbuf
,
4033 * we could save up changes and flush them all at the end,
4034 * but we would need an extra call after all diffing is done.
4035 * Since generating a cache entry is the slow path anyway,
4036 * this extra overhead probably isn't a big deal.
4038 notes_cache_write(driver
->textconv_cache
);