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
;
197 struct diff_options
*opt
;
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(struct diff_options
*o
, const char *set
, const char *reset
,
286 int first
, const char *line
, int len
)
288 int has_trailing_newline
, has_trailing_carriage_return
;
290 FILE *file
= o
->file
;
292 if (o
->output_prefix
) {
293 struct strbuf
*msg
= NULL
;
294 msg
= o
->output_prefix(o
, o
->output_prefix_data
);
296 fwrite(msg
->buf
, msg
->len
, 1, file
);
300 has_trailing_newline
= (first
== '\n');
301 has_trailing_carriage_return
= (!has_trailing_newline
&&
303 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
305 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
306 if (has_trailing_newline
)
308 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
309 if (has_trailing_carriage_return
)
314 if (len
|| !nofirst
) {
318 fwrite(line
, len
, 1, file
);
321 if (has_trailing_carriage_return
)
323 if (has_trailing_newline
)
327 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
328 const char *line
, int len
)
330 emit_line_0(o
, set
, reset
, line
[0], line
+1, len
-1);
333 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
335 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
336 ecbdata
->blank_at_eof_in_preimage
&&
337 ecbdata
->blank_at_eof_in_postimage
&&
338 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
339 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
341 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
344 static void emit_add_line(const char *reset
,
345 struct emit_callback
*ecbdata
,
346 const char *line
, int len
)
348 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
349 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
352 emit_line_0(ecbdata
->opt
, set
, reset
, '+', line
, len
);
353 else if (new_blank_line_at_eof(ecbdata
, line
, len
))
354 /* Blank line at EOF - paint '+' as well */
355 emit_line_0(ecbdata
->opt
, ws
, reset
, '+', line
, len
);
357 /* Emit just the prefix, then the rest. */
358 emit_line_0(ecbdata
->opt
, set
, reset
, '+', "", 0);
359 ws_check_emit(line
, len
, ecbdata
->ws_rule
,
360 ecbdata
->opt
->file
, set
, reset
, ws
);
364 static void emit_hunk_header(struct emit_callback
*ecbdata
,
365 const char *line
, int len
)
367 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
368 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
369 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
370 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
371 static const char atat
[2] = { '@', '@' };
373 struct strbuf msgbuf
= STRBUF_INIT
;
378 * As a hunk header must begin with "@@ -<old>, +<new> @@",
379 * it always is at least 10 bytes long.
382 memcmp(line
, atat
, 2) ||
383 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
384 emit_line(ecbdata
->opt
, plain
, reset
, line
, len
);
387 ep
+= 2; /* skip over @@ */
389 /* The hunk header in fraginfo color */
390 strbuf_add(&msgbuf
, frag
, strlen(frag
));
391 strbuf_add(&msgbuf
, line
, ep
- line
);
392 strbuf_add(&msgbuf
, reset
, strlen(reset
));
398 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
401 /* blank before the func header */
402 for (cp
= ep
; ep
- line
< len
; ep
++)
403 if (*ep
!= ' ' && *ep
!= '\t')
406 strbuf_add(&msgbuf
, plain
, strlen(plain
));
407 strbuf_add(&msgbuf
, cp
, ep
- cp
);
408 strbuf_add(&msgbuf
, reset
, strlen(reset
));
411 if (ep
< line
+ len
) {
412 strbuf_add(&msgbuf
, func
, strlen(func
));
413 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
414 strbuf_add(&msgbuf
, reset
, strlen(reset
));
417 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
418 emit_line(ecbdata
->opt
, "", "", msgbuf
.buf
, msgbuf
.len
);
419 strbuf_release(&msgbuf
);
422 static struct diff_tempfile
*claim_diff_tempfile(void) {
424 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
425 if (!diff_temp
[i
].name
)
426 return diff_temp
+ i
;
427 die("BUG: diff is failing to clean up its tempfiles");
430 static int remove_tempfile_installed
;
432 static void remove_tempfile(void)
435 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
436 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
)
437 unlink_or_warn(diff_temp
[i
].name
);
438 diff_temp
[i
].name
= NULL
;
442 static void remove_tempfile_on_signal(int signo
)
449 static void print_line_count(FILE *file
, int count
)
453 fprintf(file
, "0,0");
459 fprintf(file
, "1,%d", count
);
464 static void emit_rewrite_lines(struct emit_callback
*ecb
,
465 int prefix
, const char *data
, int size
)
467 const char *endp
= NULL
;
468 static const char *nneof
= " No newline at end of file\n";
469 const char *old
= diff_get_color(ecb
->color_diff
, DIFF_FILE_OLD
);
470 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
475 endp
= memchr(data
, '\n', size
);
476 len
= endp
? (endp
- data
+ 1) : size
;
478 ecb
->lno_in_preimage
++;
479 emit_line_0(ecb
->opt
, old
, reset
, '-',
482 ecb
->lno_in_postimage
++;
483 emit_add_line(reset
, ecb
, data
, len
);
489 const char *plain
= diff_get_color(ecb
->color_diff
,
491 emit_line_0(ecb
->opt
, plain
, reset
, '\\',
492 nneof
, strlen(nneof
));
496 static void emit_rewrite_diff(const char *name_a
,
498 struct diff_filespec
*one
,
499 struct diff_filespec
*two
,
500 struct userdiff_driver
*textconv_one
,
501 struct userdiff_driver
*textconv_two
,
502 struct diff_options
*o
)
505 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
506 const char *name_a_tab
, *name_b_tab
;
507 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
508 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
509 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
510 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
511 const char *a_prefix
, *b_prefix
;
512 char *data_one
, *data_two
;
513 size_t size_one
, size_two
;
514 struct emit_callback ecbdata
;
515 char *line_prefix
= "";
516 struct strbuf
*msgbuf
;
518 if (o
&& o
->output_prefix
) {
519 msgbuf
= o
->output_prefix(o
, o
->output_prefix_data
);
520 line_prefix
= msgbuf
->buf
;
523 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
524 a_prefix
= o
->b_prefix
;
525 b_prefix
= o
->a_prefix
;
527 a_prefix
= o
->a_prefix
;
528 b_prefix
= o
->b_prefix
;
531 name_a
+= (*name_a
== '/');
532 name_b
+= (*name_b
== '/');
533 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
534 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
536 strbuf_reset(&a_name
);
537 strbuf_reset(&b_name
);
538 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
539 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
541 size_one
= fill_textconv(textconv_one
, one
, &data_one
);
542 size_two
= fill_textconv(textconv_two
, two
, &data_two
);
544 memset(&ecbdata
, 0, sizeof(ecbdata
));
545 ecbdata
.color_diff
= color_diff
;
546 ecbdata
.found_changesp
= &o
->found_changes
;
547 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
549 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
551 mf1
.ptr
= (char *)data_one
;
552 mf2
.ptr
= (char *)data_two
;
555 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
557 ecbdata
.lno_in_preimage
= 1;
558 ecbdata
.lno_in_postimage
= 1;
560 lc_a
= count_lines(data_one
, size_one
);
561 lc_b
= count_lines(data_two
, size_two
);
563 "%s%s--- %s%s%s\n%s%s+++ %s%s%s\n%s%s@@ -",
564 line_prefix
, metainfo
, a_name
.buf
, name_a_tab
, reset
,
565 line_prefix
, metainfo
, b_name
.buf
, name_b_tab
, reset
,
566 line_prefix
, fraginfo
);
567 print_line_count(o
->file
, lc_a
);
568 fprintf(o
->file
, " +");
569 print_line_count(o
->file
, lc_b
);
570 fprintf(o
->file
, " @@%s\n", reset
);
572 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
574 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
576 free((char *)data_one
);
578 free((char *)data_two
);
581 struct diff_words_buffer
{
584 struct diff_words_orig
{
585 const char *begin
, *end
;
587 int orig_nr
, orig_alloc
;
590 static void diff_words_append(char *line
, unsigned long len
,
591 struct diff_words_buffer
*buffer
)
593 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
596 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
597 buffer
->text
.size
+= len
;
598 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
601 struct diff_words_style_elem
605 const char *color
; /* NULL; filled in by the setup code if
606 * color is enabled */
609 struct diff_words_style
611 enum diff_words_type type
;
612 struct diff_words_style_elem
new, old
, ctx
;
616 struct diff_words_style diff_words_styles
[] = {
617 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
618 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
619 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
622 struct diff_words_data
{
623 struct diff_words_buffer minus
, plus
;
624 const char *current_plus
;
627 enum diff_words_type type
;
628 struct diff_words_style
*style
;
631 static int fn_out_diff_words_write_helper(FILE *fp
,
632 struct diff_words_style_elem
*st_el
,
634 size_t count
, const char *buf
)
637 char *p
= memchr(buf
, '\n', count
);
639 if (st_el
->color
&& fputs(st_el
->color
, fp
) < 0)
641 if (fputs(st_el
->prefix
, fp
) < 0 ||
642 fwrite(buf
, p
? p
- buf
: count
, 1, fp
) != 1 ||
643 fputs(st_el
->suffix
, fp
) < 0)
645 if (st_el
->color
&& *st_el
->color
646 && fputs(GIT_COLOR_RESET
, fp
) < 0)
651 if (fputs(newline
, fp
) < 0)
653 count
-= p
+ 1 - buf
;
659 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
661 struct diff_words_data
*diff_words
= priv
;
662 struct diff_words_style
*style
= diff_words
->style
;
663 int minus_first
, minus_len
, plus_first
, plus_len
;
664 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
666 if (line
[0] != '@' || parse_hunk_header(line
, len
,
667 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
670 /* POSIX requires that first be decremented by one if len == 0... */
672 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
674 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
676 minus_begin
= minus_end
=
677 diff_words
->minus
.orig
[minus_first
].end
;
680 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
681 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
683 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
685 if (diff_words
->current_plus
!= plus_begin
)
686 fn_out_diff_words_write_helper(diff_words
->file
,
687 &style
->ctx
, style
->newline
,
688 plus_begin
- diff_words
->current_plus
,
689 diff_words
->current_plus
);
690 if (minus_begin
!= minus_end
)
691 fn_out_diff_words_write_helper(diff_words
->file
,
692 &style
->old
, style
->newline
,
693 minus_end
- minus_begin
, minus_begin
);
694 if (plus_begin
!= plus_end
)
695 fn_out_diff_words_write_helper(diff_words
->file
,
696 &style
->new, style
->newline
,
697 plus_end
- plus_begin
, plus_begin
);
699 diff_words
->current_plus
= plus_end
;
702 /* This function starts looking at *begin, and returns 0 iff a word was found. */
703 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
704 int *begin
, int *end
)
706 if (word_regex
&& *begin
< buffer
->size
) {
708 if (!regexec(word_regex
, buffer
->ptr
+ *begin
, 1, match
, 0)) {
709 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
710 '\n', match
[0].rm_eo
- match
[0].rm_so
);
711 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
712 *begin
+= match
[0].rm_so
;
713 return *begin
>= *end
;
718 /* find the next word */
719 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
721 if (*begin
>= buffer
->size
)
724 /* find the end of the word */
726 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
733 * This function splits the words in buffer->text, stores the list with
734 * newline separator into out, and saves the offsets of the original words
737 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
746 /* fake an empty "0th" word */
747 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
748 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
751 for (i
= 0; i
< buffer
->text
.size
; i
++) {
752 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
755 /* store original boundaries */
756 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
758 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
759 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
763 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
764 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
765 out
->ptr
[out
->size
+ j
- i
] = '\n';
766 out
->size
+= j
- i
+ 1;
772 /* this executes the word diff on the accumulated buffers */
773 static void diff_words_show(struct diff_words_data
*diff_words
)
777 mmfile_t minus
, plus
;
778 struct diff_words_style
*style
= diff_words
->style
;
780 /* special case: only removal */
781 if (!diff_words
->plus
.text
.size
) {
782 fn_out_diff_words_write_helper(diff_words
->file
,
783 &style
->old
, style
->newline
,
784 diff_words
->minus
.text
.size
, diff_words
->minus
.text
.ptr
);
785 diff_words
->minus
.text
.size
= 0;
789 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
791 memset(&xpp
, 0, sizeof(xpp
));
792 memset(&xecfg
, 0, sizeof(xecfg
));
793 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
794 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
795 xpp
.flags
= XDF_NEED_MINIMAL
;
796 /* as only the hunk header will be parsed, we need a 0-context */
798 xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
802 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
803 diff_words
->plus
.text
.size
)
804 fn_out_diff_words_write_helper(diff_words
->file
,
805 &style
->ctx
, style
->newline
,
806 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
807 - diff_words
->current_plus
, diff_words
->current_plus
);
808 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
811 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
812 static void diff_words_flush(struct emit_callback
*ecbdata
)
814 if (ecbdata
->diff_words
->minus
.text
.size
||
815 ecbdata
->diff_words
->plus
.text
.size
)
816 diff_words_show(ecbdata
->diff_words
);
819 static void free_diff_words_data(struct emit_callback
*ecbdata
)
821 if (ecbdata
->diff_words
) {
822 diff_words_flush(ecbdata
);
823 free (ecbdata
->diff_words
->minus
.text
.ptr
);
824 free (ecbdata
->diff_words
->minus
.orig
);
825 free (ecbdata
->diff_words
->plus
.text
.ptr
);
826 free (ecbdata
->diff_words
->plus
.orig
);
827 free(ecbdata
->diff_words
->word_regex
);
828 free(ecbdata
->diff_words
);
829 ecbdata
->diff_words
= NULL
;
833 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
836 return diff_colors
[ix
];
840 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
847 return ecb
->truncate(line
, len
);
851 (void) utf8_width(&cp
, &l
);
853 break; /* truncated in the middle? */
858 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
861 ecbdata
->lno_in_preimage
= 0;
862 ecbdata
->lno_in_postimage
= 0;
863 p
= strchr(line
, '-');
865 return; /* cannot happen */
866 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
869 return; /* cannot happen */
870 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
873 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
875 struct emit_callback
*ecbdata
= priv
;
876 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
877 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
878 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
879 struct diff_options
*o
= ecbdata
->opt
;
880 char *line_prefix
= "";
881 struct strbuf
*msgbuf
;
883 if (o
&& o
->output_prefix
) {
884 msgbuf
= o
->output_prefix(o
, o
->output_prefix_data
);
885 line_prefix
= msgbuf
->buf
;
888 if (ecbdata
->header
) {
889 fprintf(ecbdata
->opt
->file
, "%s", ecbdata
->header
->buf
);
890 strbuf_reset(ecbdata
->header
);
891 ecbdata
->header
= NULL
;
893 *(ecbdata
->found_changesp
) = 1;
895 if (ecbdata
->label_path
[0]) {
896 const char *name_a_tab
, *name_b_tab
;
898 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
899 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
901 fprintf(ecbdata
->opt
->file
, "%s%s--- %s%s%s\n",
902 line_prefix
, meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
903 fprintf(ecbdata
->opt
->file
, "%s%s+++ %s%s%s\n",
904 line_prefix
, meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
905 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
908 if (diff_suppress_blank_empty
909 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
914 if (line
[0] == '@') {
915 if (ecbdata
->diff_words
)
916 diff_words_flush(ecbdata
);
917 len
= sane_truncate_line(ecbdata
, line
, len
);
918 find_lno(line
, ecbdata
);
919 emit_hunk_header(ecbdata
, line
, len
);
920 if (line
[len
-1] != '\n')
921 putc('\n', ecbdata
->opt
->file
);
926 emit_line(ecbdata
->opt
, reset
, reset
, line
, len
);
927 if (ecbdata
->diff_words
928 && ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
)
929 fputs("~\n", ecbdata
->opt
->file
);
933 if (ecbdata
->diff_words
) {
934 if (line
[0] == '-') {
935 diff_words_append(line
, len
,
936 &ecbdata
->diff_words
->minus
);
938 } else if (line
[0] == '+') {
939 diff_words_append(line
, len
,
940 &ecbdata
->diff_words
->plus
);
943 diff_words_flush(ecbdata
);
944 if (ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
) {
945 emit_line(ecbdata
->opt
, plain
, reset
, line
, len
);
946 fputs("~\n", ecbdata
->opt
->file
);
948 /* don't print the prefix character */
949 emit_line(ecbdata
->opt
, plain
, reset
, line
+1, len
-1);
954 if (line
[0] != '+') {
956 diff_get_color(ecbdata
->color_diff
,
957 line
[0] == '-' ? DIFF_FILE_OLD
: DIFF_PLAIN
);
958 ecbdata
->lno_in_preimage
++;
960 ecbdata
->lno_in_postimage
++;
961 emit_line(ecbdata
->opt
, color
, reset
, line
, len
);
963 ecbdata
->lno_in_postimage
++;
964 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
968 static char *pprint_rename(const char *a
, const char *b
)
972 struct strbuf name
= STRBUF_INIT
;
973 int pfx_length
, sfx_length
;
974 int len_a
= strlen(a
);
975 int len_b
= strlen(b
);
976 int a_midlen
, b_midlen
;
977 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
978 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
980 if (qlen_a
|| qlen_b
) {
981 quote_c_style(a
, &name
, NULL
, 0);
982 strbuf_addstr(&name
, " => ");
983 quote_c_style(b
, &name
, NULL
, 0);
984 return strbuf_detach(&name
, NULL
);
987 /* Find common prefix */
989 while (*old
&& *new && *old
== *new) {
991 pfx_length
= old
- a
+ 1;
996 /* Find common suffix */
1000 while (a
<= old
&& b
<= new && *old
== *new) {
1002 sfx_length
= len_a
- (old
- a
);
1008 * pfx{mid-a => mid-b}sfx
1009 * {pfx-a => pfx-b}sfx
1010 * pfx{sfx-a => sfx-b}
1013 a_midlen
= len_a
- pfx_length
- sfx_length
;
1014 b_midlen
= len_b
- pfx_length
- sfx_length
;
1020 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
1021 if (pfx_length
+ sfx_length
) {
1022 strbuf_add(&name
, a
, pfx_length
);
1023 strbuf_addch(&name
, '{');
1025 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
1026 strbuf_addstr(&name
, " => ");
1027 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
1028 if (pfx_length
+ sfx_length
) {
1029 strbuf_addch(&name
, '}');
1030 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
1032 return strbuf_detach(&name
, NULL
);
1038 struct diffstat_file
{
1042 unsigned is_unmerged
:1;
1043 unsigned is_binary
:1;
1044 unsigned is_renamed
:1;
1045 uintmax_t added
, deleted
;
1049 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
1053 struct diffstat_file
*x
;
1054 x
= xcalloc(sizeof (*x
), 1);
1055 if (diffstat
->nr
== diffstat
->alloc
) {
1056 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
1057 diffstat
->files
= xrealloc(diffstat
->files
,
1058 diffstat
->alloc
* sizeof(x
));
1060 diffstat
->files
[diffstat
->nr
++] = x
;
1062 x
->from_name
= xstrdup(name_a
);
1063 x
->name
= xstrdup(name_b
);
1067 x
->from_name
= NULL
;
1068 x
->name
= xstrdup(name_a
);
1073 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
1075 struct diffstat_t
*diffstat
= priv
;
1076 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
1080 else if (line
[0] == '-')
1084 const char mime_boundary_leader
[] = "------------";
1086 static int scale_linear(int it
, int width
, int max_change
)
1089 * make sure that at least one '-' is printed if there were deletions,
1090 * and likewise for '+'.
1094 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
1097 static void show_name(FILE *file
,
1098 const char *prefix
, const char *name
, int len
)
1100 fprintf(file
, " %s%-*s |", prefix
, len
, name
);
1103 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
1107 fprintf(file
, "%s", set
);
1110 fprintf(file
, "%s", reset
);
1113 static void fill_print_name(struct diffstat_file
*file
)
1117 if (file
->print_name
)
1120 if (!file
->is_renamed
) {
1121 struct strbuf buf
= STRBUF_INIT
;
1122 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
1123 pname
= strbuf_detach(&buf
, NULL
);
1126 strbuf_release(&buf
);
1129 pname
= pprint_rename(file
->from_name
, file
->name
);
1131 file
->print_name
= pname
;
1134 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
1136 int i
, len
, add
, del
, adds
= 0, dels
= 0;
1137 uintmax_t max_change
= 0, max_len
= 0;
1138 int total_files
= data
->nr
;
1139 int width
, name_width
;
1140 const char *reset
, *set
, *add_c
, *del_c
;
1141 const char *line_prefix
= "";
1142 struct strbuf
*msg
= NULL
;
1147 if (options
->output_prefix
) {
1148 msg
= options
->output_prefix(options
, options
->output_prefix_data
);
1149 line_prefix
= msg
->buf
;
1152 width
= options
->stat_width
? options
->stat_width
: 80;
1153 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
1155 /* Sanity: give at least 5 columns to the graph,
1156 * but leave at least 10 columns for the name.
1160 if (name_width
< 10)
1162 else if (width
< name_width
+ 15)
1163 name_width
= width
- 15;
1165 /* Find the longest filename and max number of changes */
1166 reset
= diff_get_color_opt(options
, DIFF_RESET
);
1167 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
1168 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
1169 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
1171 for (i
= 0; i
< data
->nr
; i
++) {
1172 struct diffstat_file
*file
= data
->files
[i
];
1173 uintmax_t change
= file
->added
+ file
->deleted
;
1174 fill_print_name(file
);
1175 len
= strlen(file
->print_name
);
1179 if (file
->is_binary
|| file
->is_unmerged
)
1181 if (max_change
< change
)
1182 max_change
= change
;
1185 /* Compute the width of the graph part;
1186 * 10 is for one blank at the beginning of the line plus
1187 * " | count " between the name and the graph.
1189 * From here on, name_width is the width of the name area,
1190 * and width is the width of the graph area.
1192 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
1193 if (width
< (name_width
+ 10) + max_change
)
1194 width
= width
- (name_width
+ 10);
1198 for (i
= 0; i
< data
->nr
; i
++) {
1199 const char *prefix
= "";
1200 char *name
= data
->files
[i
]->print_name
;
1201 uintmax_t added
= data
->files
[i
]->added
;
1202 uintmax_t deleted
= data
->files
[i
]->deleted
;
1206 * "scale" the filename
1209 name_len
= strlen(name
);
1210 if (name_width
< name_len
) {
1214 name
+= name_len
- len
;
1215 slash
= strchr(name
, '/');
1220 if (data
->files
[i
]->is_binary
) {
1221 fprintf(options
->file
, "%s", line_prefix
);
1222 show_name(options
->file
, prefix
, name
, len
);
1223 fprintf(options
->file
, " Bin ");
1224 fprintf(options
->file
, "%s%"PRIuMAX
"%s",
1225 del_c
, deleted
, reset
);
1226 fprintf(options
->file
, " -> ");
1227 fprintf(options
->file
, "%s%"PRIuMAX
"%s",
1228 add_c
, added
, reset
);
1229 fprintf(options
->file
, " bytes");
1230 fprintf(options
->file
, "\n");
1233 else if (data
->files
[i
]->is_unmerged
) {
1234 fprintf(options
->file
, "%s", line_prefix
);
1235 show_name(options
->file
, prefix
, name
, len
);
1236 fprintf(options
->file
, " Unmerged\n");
1239 else if (!data
->files
[i
]->is_renamed
&&
1240 (added
+ deleted
== 0)) {
1246 * scale the add/delete
1253 if (width
<= max_change
) {
1254 add
= scale_linear(add
, width
, max_change
);
1255 del
= scale_linear(del
, width
, max_change
);
1257 fprintf(options
->file
, "%s", line_prefix
);
1258 show_name(options
->file
, prefix
, name
, len
);
1259 fprintf(options
->file
, "%5"PRIuMAX
"%s", added
+ deleted
,
1260 added
+ deleted
? " " : "");
1261 show_graph(options
->file
, '+', add
, add_c
, reset
);
1262 show_graph(options
->file
, '-', del
, del_c
, reset
);
1263 fprintf(options
->file
, "\n");
1265 fprintf(options
->file
, "%s", line_prefix
);
1266 fprintf(options
->file
,
1267 " %d files changed, %d insertions(+), %d deletions(-)\n",
1268 total_files
, adds
, dels
);
1271 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
1273 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
1278 for (i
= 0; i
< data
->nr
; i
++) {
1279 if (!data
->files
[i
]->is_binary
&&
1280 !data
->files
[i
]->is_unmerged
) {
1281 int added
= data
->files
[i
]->added
;
1282 int deleted
= data
->files
[i
]->deleted
;
1283 if (!data
->files
[i
]->is_renamed
&&
1284 (added
+ deleted
== 0)) {
1292 if (options
->output_prefix
) {
1293 struct strbuf
*msg
= NULL
;
1294 msg
= options
->output_prefix(options
,
1295 options
->output_prefix_data
);
1296 fprintf(options
->file
, "%s", msg
->buf
);
1298 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
1299 total_files
, adds
, dels
);
1302 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
1309 for (i
= 0; i
< data
->nr
; i
++) {
1310 struct diffstat_file
*file
= data
->files
[i
];
1312 if (options
->output_prefix
) {
1313 struct strbuf
*msg
= NULL
;
1314 msg
= options
->output_prefix(options
,
1315 options
->output_prefix_data
);
1316 fprintf(options
->file
, "%s", msg
->buf
);
1319 if (file
->is_binary
)
1320 fprintf(options
->file
, "-\t-\t");
1322 fprintf(options
->file
,
1323 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
1324 file
->added
, file
->deleted
);
1325 if (options
->line_termination
) {
1326 fill_print_name(file
);
1327 if (!file
->is_renamed
)
1328 write_name_quoted(file
->name
, options
->file
,
1329 options
->line_termination
);
1331 fputs(file
->print_name
, options
->file
);
1332 putc(options
->line_termination
, options
->file
);
1335 if (file
->is_renamed
) {
1336 putc('\0', options
->file
);
1337 write_name_quoted(file
->from_name
, options
->file
, '\0');
1339 write_name_quoted(file
->name
, options
->file
, '\0');
1344 struct dirstat_file
{
1346 unsigned long changed
;
1349 struct dirstat_dir
{
1350 struct dirstat_file
*files
;
1351 int alloc
, nr
, percent
, cumulative
;
1354 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
1355 unsigned long changed
, const char *base
, int baselen
)
1357 unsigned long this_dir
= 0;
1358 unsigned int sources
= 0;
1359 const char *line_prefix
= "";
1360 struct strbuf
*msg
= NULL
;
1362 if (opt
->output_prefix
) {
1363 msg
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1364 line_prefix
= msg
->buf
;
1368 struct dirstat_file
*f
= dir
->files
;
1369 int namelen
= strlen(f
->name
);
1373 if (namelen
< baselen
)
1375 if (memcmp(f
->name
, base
, baselen
))
1377 slash
= strchr(f
->name
+ baselen
, '/');
1379 int newbaselen
= slash
+ 1 - f
->name
;
1380 this = gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
1392 * We don't report dirstat's for
1394 * - or cases where everything came from a single directory
1395 * under this directory (sources == 1).
1397 if (baselen
&& sources
!= 1) {
1398 int permille
= this_dir
* 1000 / changed
;
1400 int percent
= permille
/ 10;
1401 if (percent
>= dir
->percent
) {
1402 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
1403 percent
, permille
% 10, baselen
, base
);
1404 if (!dir
->cumulative
)
1412 static int dirstat_compare(const void *_a
, const void *_b
)
1414 const struct dirstat_file
*a
= _a
;
1415 const struct dirstat_file
*b
= _b
;
1416 return strcmp(a
->name
, b
->name
);
1419 static void show_dirstat(struct diff_options
*options
)
1422 unsigned long changed
;
1423 struct dirstat_dir dir
;
1424 struct diff_queue_struct
*q
= &diff_queued_diff
;
1429 dir
.percent
= options
->dirstat_percent
;
1430 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1433 for (i
= 0; i
< q
->nr
; i
++) {
1434 struct diff_filepair
*p
= q
->queue
[i
];
1436 unsigned long copied
, added
, damage
;
1438 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1440 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1441 diff_populate_filespec(p
->one
, 0);
1442 diff_populate_filespec(p
->two
, 0);
1443 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1445 diff_free_filespec_data(p
->one
);
1446 diff_free_filespec_data(p
->two
);
1447 } else if (DIFF_FILE_VALID(p
->one
)) {
1448 diff_populate_filespec(p
->one
, 1);
1450 diff_free_filespec_data(p
->one
);
1451 } else if (DIFF_FILE_VALID(p
->two
)) {
1452 diff_populate_filespec(p
->two
, 1);
1454 added
= p
->two
->size
;
1455 diff_free_filespec_data(p
->two
);
1460 * Original minus copied is the removed material,
1461 * added is the new material. They are both damages
1462 * made to the preimage. In --dirstat-by-file mode, count
1463 * damaged files, not damaged lines. This is done by
1464 * counting only a single damaged line per file.
1466 damage
= (p
->one
->size
- copied
) + added
;
1467 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
) && damage
> 0)
1470 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1471 dir
.files
[dir
.nr
].name
= name
;
1472 dir
.files
[dir
.nr
].changed
= damage
;
1477 /* This can happen even with many files, if everything was renames */
1481 /* Show all directories with more than x% of the changes */
1482 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
1483 gather_dirstat(options
, &dir
, changed
, "", 0);
1486 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1489 for (i
= 0; i
< diffstat
->nr
; i
++) {
1490 struct diffstat_file
*f
= diffstat
->files
[i
];
1491 if (f
->name
!= f
->print_name
)
1492 free(f
->print_name
);
1497 free(diffstat
->files
);
1500 struct checkdiff_t
{
1501 const char *filename
;
1503 int conflict_marker_size
;
1504 struct diff_options
*o
;
1509 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
1514 if (len
< marker_size
+ 1)
1516 firstchar
= line
[0];
1517 switch (firstchar
) {
1518 case '=': case '>': case '<': case '|':
1523 for (cnt
= 1; cnt
< marker_size
; cnt
++)
1524 if (line
[cnt
] != firstchar
)
1526 /* line[1] thru line[marker_size-1] are same as firstchar */
1527 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
1532 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1534 struct checkdiff_t
*data
= priv
;
1535 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1536 int marker_size
= data
->conflict_marker_size
;
1537 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1538 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1539 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1541 char *line_prefix
= "";
1542 struct strbuf
*msgbuf
;
1545 if (data
->o
->output_prefix
) {
1546 msgbuf
= data
->o
->output_prefix(data
->o
,
1547 data
->o
->output_prefix_data
);
1548 line_prefix
= msgbuf
->buf
;
1551 if (line
[0] == '+') {
1554 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
1556 fprintf(data
->o
->file
,
1557 "%s%s:%d: leftover conflict marker\n",
1558 line_prefix
, data
->filename
, data
->lineno
);
1560 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1563 data
->status
|= bad
;
1564 err
= whitespace_error_string(bad
);
1565 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
1566 line_prefix
, data
->filename
, data
->lineno
, err
);
1568 emit_line(data
->o
, set
, reset
, line
, 1);
1569 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1570 data
->o
->file
, set
, reset
, ws
);
1571 } else if (line
[0] == ' ') {
1573 } else if (line
[0] == '@') {
1574 char *plus
= strchr(line
, '+');
1576 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1578 die("invalid diff");
1582 static unsigned char *deflate_it(char *data
,
1584 unsigned long *result_size
)
1587 unsigned char *deflated
;
1590 memset(&stream
, 0, sizeof(stream
));
1591 deflateInit(&stream
, zlib_compression_level
);
1592 bound
= deflateBound(&stream
, size
);
1593 deflated
= xmalloc(bound
);
1594 stream
.next_out
= deflated
;
1595 stream
.avail_out
= bound
;
1597 stream
.next_in
= (unsigned char *)data
;
1598 stream
.avail_in
= size
;
1599 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1601 deflateEnd(&stream
);
1602 *result_size
= stream
.total_out
;
1606 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
, char *prefix
)
1612 unsigned long orig_size
;
1613 unsigned long delta_size
;
1614 unsigned long deflate_size
;
1615 unsigned long data_size
;
1617 /* We could do deflated delta, or we could do just deflated two,
1618 * whichever is smaller.
1621 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1622 if (one
->size
&& two
->size
) {
1623 delta
= diff_delta(one
->ptr
, one
->size
,
1624 two
->ptr
, two
->size
,
1625 &delta_size
, deflate_size
);
1627 void *to_free
= delta
;
1628 orig_size
= delta_size
;
1629 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1634 if (delta
&& delta_size
< deflate_size
) {
1635 fprintf(file
, "%sdelta %lu\n", prefix
, orig_size
);
1638 data_size
= delta_size
;
1641 fprintf(file
, "%sliteral %lu\n", prefix
, two
->size
);
1644 data_size
= deflate_size
;
1647 /* emit data encoded in base85 */
1650 int bytes
= (52 < data_size
) ? 52 : data_size
;
1654 line
[0] = bytes
+ 'A' - 1;
1656 line
[0] = bytes
- 26 + 'a' - 1;
1657 encode_85(line
+ 1, cp
, bytes
);
1658 cp
= (char *) cp
+ bytes
;
1659 fprintf(file
, "%s", prefix
);
1663 fprintf(file
, "%s\n", prefix
);
1667 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
, char *prefix
)
1669 fprintf(file
, "%sGIT binary patch\n", prefix
);
1670 emit_binary_diff_body(file
, one
, two
, prefix
);
1671 emit_binary_diff_body(file
, two
, one
, prefix
);
1674 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1677 one
->driver
= userdiff_find_by_path(one
->path
);
1679 one
->driver
= userdiff_find_by_name("default");
1682 int diff_filespec_is_binary(struct diff_filespec
*one
)
1684 if (one
->is_binary
== -1) {
1685 diff_filespec_load_driver(one
);
1686 if (one
->driver
->binary
!= -1)
1687 one
->is_binary
= one
->driver
->binary
;
1689 if (!one
->data
&& DIFF_FILE_VALID(one
))
1690 diff_populate_filespec(one
, 0);
1692 one
->is_binary
= buffer_is_binary(one
->data
,
1694 if (one
->is_binary
== -1)
1698 return one
->is_binary
;
1701 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
1703 diff_filespec_load_driver(one
);
1704 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
1707 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1709 diff_filespec_load_driver(one
);
1710 return one
->driver
->word_regex
;
1713 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
1715 if (!options
->a_prefix
)
1716 options
->a_prefix
= a
;
1717 if (!options
->b_prefix
)
1718 options
->b_prefix
= b
;
1721 static struct userdiff_driver
*get_textconv(struct diff_filespec
*one
)
1723 if (!DIFF_FILE_VALID(one
))
1725 if (!S_ISREG(one
->mode
))
1727 diff_filespec_load_driver(one
);
1728 if (!one
->driver
->textconv
)
1731 if (one
->driver
->textconv_want_cache
&& !one
->driver
->textconv_cache
) {
1732 struct notes_cache
*c
= xmalloc(sizeof(*c
));
1733 struct strbuf name
= STRBUF_INIT
;
1735 strbuf_addf(&name
, "textconv/%s", one
->driver
->name
);
1736 notes_cache_init(c
, name
.buf
, one
->driver
->textconv
);
1737 one
->driver
->textconv_cache
= c
;
1743 static void builtin_diff(const char *name_a
,
1745 struct diff_filespec
*one
,
1746 struct diff_filespec
*two
,
1747 const char *xfrm_msg
,
1748 struct diff_options
*o
,
1749 int complete_rewrite
)
1753 char *a_one
, *b_two
;
1754 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1755 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1756 const char *a_prefix
, *b_prefix
;
1757 struct userdiff_driver
*textconv_one
= NULL
;
1758 struct userdiff_driver
*textconv_two
= NULL
;
1759 struct strbuf header
= STRBUF_INIT
;
1760 struct strbuf
*msgbuf
;
1761 char *line_prefix
= "";
1763 if (o
->output_prefix
) {
1764 msgbuf
= o
->output_prefix(o
, o
->output_prefix_data
);
1765 line_prefix
= msgbuf
->buf
;
1768 if (DIFF_OPT_TST(o
, SUBMODULE_LOG
) &&
1769 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
1770 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
1771 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1772 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1773 show_submodule_summary(o
->file
, one
? one
->path
: two
->path
,
1774 one
->sha1
, two
->sha1
, two
->dirty_submodule
,
1779 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
1780 textconv_one
= get_textconv(one
);
1781 textconv_two
= get_textconv(two
);
1784 diff_set_mnemonic_prefix(o
, "a/", "b/");
1785 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
1786 a_prefix
= o
->b_prefix
;
1787 b_prefix
= o
->a_prefix
;
1789 a_prefix
= o
->a_prefix
;
1790 b_prefix
= o
->b_prefix
;
1793 /* Never use a non-valid filename anywhere if at all possible */
1794 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
1795 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
1797 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
1798 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
1799 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1800 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1801 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, set
, a_one
, b_two
, reset
);
1802 if (lbl
[0][0] == '/') {
1804 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, set
, two
->mode
, reset
);
1805 if (xfrm_msg
&& xfrm_msg
[0])
1806 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1808 else if (lbl
[1][0] == '/') {
1809 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, set
, one
->mode
, reset
);
1810 if (xfrm_msg
&& xfrm_msg
[0])
1811 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1814 if (one
->mode
!= two
->mode
) {
1815 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, set
, one
->mode
, reset
);
1816 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, set
, two
->mode
, reset
);
1818 if (xfrm_msg
&& xfrm_msg
[0])
1819 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1822 * we do not run diff between different kind
1825 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1826 goto free_ab_and_return
;
1827 if (complete_rewrite
&&
1828 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
1829 (textconv_two
|| !diff_filespec_is_binary(two
))) {
1830 fprintf(o
->file
, "%s", header
.buf
);
1831 strbuf_reset(&header
);
1832 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1833 textconv_one
, textconv_two
, o
);
1834 o
->found_changes
= 1;
1835 goto free_ab_and_return
;
1839 if (!DIFF_OPT_TST(o
, TEXT
) &&
1840 ( (!textconv_one
&& diff_filespec_is_binary(one
)) ||
1841 (!textconv_two
&& diff_filespec_is_binary(two
)) )) {
1842 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1843 die("unable to read files to diff");
1844 /* Quite common confusing case */
1845 if (mf1
.size
== mf2
.size
&&
1846 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1847 goto free_ab_and_return
;
1848 fprintf(o
->file
, "%s", header
.buf
);
1849 strbuf_reset(&header
);
1850 if (DIFF_OPT_TST(o
, BINARY
))
1851 emit_binary_diff(o
->file
, &mf1
, &mf2
, line_prefix
);
1853 fprintf(o
->file
, "%sBinary files %s and %s differ\n",
1854 line_prefix
, lbl
[0], lbl
[1]);
1855 o
->found_changes
= 1;
1858 /* Crazy xdl interfaces.. */
1859 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1862 struct emit_callback ecbdata
;
1863 const struct userdiff_funcname
*pe
;
1865 if (!DIFF_XDL_TST(o
, WHITESPACE_FLAGS
)) {
1866 fprintf(o
->file
, "%s", header
.buf
);
1867 strbuf_reset(&header
);
1870 mf1
.size
= fill_textconv(textconv_one
, one
, &mf1
.ptr
);
1871 mf2
.size
= fill_textconv(textconv_two
, two
, &mf2
.ptr
);
1873 pe
= diff_funcname_pattern(one
);
1875 pe
= diff_funcname_pattern(two
);
1877 memset(&xpp
, 0, sizeof(xpp
));
1878 memset(&xecfg
, 0, sizeof(xecfg
));
1879 memset(&ecbdata
, 0, sizeof(ecbdata
));
1880 ecbdata
.label_path
= lbl
;
1881 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1882 ecbdata
.found_changesp
= &o
->found_changes
;
1883 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1884 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
1885 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1887 ecbdata
.header
= header
.len
? &header
: NULL
;
1888 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1889 xecfg
.ctxlen
= o
->context
;
1890 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
1891 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1893 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
1896 else if (!prefixcmp(diffopts
, "--unified="))
1897 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1898 else if (!prefixcmp(diffopts
, "-u"))
1899 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1903 ecbdata
.diff_words
=
1904 xcalloc(1, sizeof(struct diff_words_data
));
1905 ecbdata
.diff_words
->file
= o
->file
;
1906 ecbdata
.diff_words
->type
= o
->word_diff
;
1908 o
->word_regex
= userdiff_word_regex(one
);
1910 o
->word_regex
= userdiff_word_regex(two
);
1912 o
->word_regex
= diff_word_regex_cfg
;
1913 if (o
->word_regex
) {
1914 ecbdata
.diff_words
->word_regex
= (regex_t
*)
1915 xmalloc(sizeof(regex_t
));
1916 if (regcomp(ecbdata
.diff_words
->word_regex
,
1918 REG_EXTENDED
| REG_NEWLINE
))
1919 die ("Invalid regular expression: %s",
1922 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
1923 if (o
->word_diff
== diff_words_styles
[i
].type
) {
1924 ecbdata
.diff_words
->style
=
1925 &diff_words_styles
[i
];
1929 if (DIFF_OPT_TST(o
, COLOR_DIFF
)) {
1930 struct diff_words_style
*st
= ecbdata
.diff_words
->style
;
1931 st
->old
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1932 st
->new.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1933 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_PLAIN
);
1936 xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
1939 free_diff_words_data(&ecbdata
);
1944 xdiff_clear_find_func(&xecfg
);
1948 strbuf_release(&header
);
1949 diff_free_filespec_data(one
);
1950 diff_free_filespec_data(two
);
1956 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1957 struct diff_filespec
*one
,
1958 struct diff_filespec
*two
,
1959 struct diffstat_t
*diffstat
,
1960 struct diff_options
*o
,
1961 int complete_rewrite
)
1964 struct diffstat_file
*data
;
1966 data
= diffstat_add(diffstat
, name_a
, name_b
);
1969 data
->is_unmerged
= 1;
1972 if (complete_rewrite
) {
1973 diff_populate_filespec(one
, 0);
1974 diff_populate_filespec(two
, 0);
1975 data
->deleted
= count_lines(one
->data
, one
->size
);
1976 data
->added
= count_lines(two
->data
, two
->size
);
1977 goto free_and_return
;
1979 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1980 die("unable to read files to diff");
1982 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1983 data
->is_binary
= 1;
1984 data
->added
= mf2
.size
;
1985 data
->deleted
= mf1
.size
;
1987 /* Crazy xdl interfaces.. */
1991 memset(&xpp
, 0, sizeof(xpp
));
1992 memset(&xecfg
, 0, sizeof(xecfg
));
1993 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1994 xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
1999 diff_free_filespec_data(one
);
2000 diff_free_filespec_data(two
);
2003 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
2004 const char *attr_path
,
2005 struct diff_filespec
*one
,
2006 struct diff_filespec
*two
,
2007 struct diff_options
*o
)
2010 struct checkdiff_t data
;
2015 memset(&data
, 0, sizeof(data
));
2016 data
.filename
= name_b
? name_b
: name_a
;
2019 data
.ws_rule
= whitespace_rule(attr_path
);
2020 data
.conflict_marker_size
= ll_merge_marker_size(attr_path
);
2022 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
2023 die("unable to read files to diff");
2026 * All the other codepaths check both sides, but not checking
2027 * the "old" side here is deliberate. We are checking the newly
2028 * introduced changes, and as long as the "new" side is text, we
2029 * can and should check what it introduces.
2031 if (diff_filespec_is_binary(two
))
2032 goto free_and_return
;
2034 /* Crazy xdl interfaces.. */
2038 memset(&xpp
, 0, sizeof(xpp
));
2039 memset(&xecfg
, 0, sizeof(xecfg
));
2040 xecfg
.ctxlen
= 1; /* at least one context line */
2041 xpp
.flags
= XDF_NEED_MINIMAL
;
2042 xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
2045 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
2046 struct emit_callback ecbdata
;
2049 ecbdata
.ws_rule
= data
.ws_rule
;
2050 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
2051 blank_at_eof
= ecbdata
.blank_at_eof_in_preimage
;
2056 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
2057 fprintf(o
->file
, "%s:%d: %s.\n",
2058 data
.filename
, blank_at_eof
, err
);
2059 data
.status
= 1; /* report errors */
2064 diff_free_filespec_data(one
);
2065 diff_free_filespec_data(two
);
2067 DIFF_OPT_SET(o
, CHECK_FAILED
);
2070 struct diff_filespec
*alloc_filespec(const char *path
)
2072 int namelen
= strlen(path
);
2073 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
2075 memset(spec
, 0, sizeof(*spec
));
2076 spec
->path
= (char *)(spec
+ 1);
2077 memcpy(spec
->path
, path
, namelen
+1);
2079 spec
->is_binary
= -1;
2083 void free_filespec(struct diff_filespec
*spec
)
2085 if (!--spec
->count
) {
2086 diff_free_filespec_data(spec
);
2091 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
2092 unsigned short mode
)
2095 spec
->mode
= canon_mode(mode
);
2096 hashcpy(spec
->sha1
, sha1
);
2097 spec
->sha1_valid
= !is_null_sha1(sha1
);
2102 * Given a name and sha1 pair, if the index tells us the file in
2103 * the work tree has that object contents, return true, so that
2104 * prepare_temp_file() does not have to inflate and extract.
2106 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
2108 struct cache_entry
*ce
;
2113 * We do not read the cache ourselves here, because the
2114 * benchmark with my previous version that always reads cache
2115 * shows that it makes things worse for diff-tree comparing
2116 * two linux-2.6 kernel trees in an already checked out work
2117 * tree. This is because most diff-tree comparisons deal with
2118 * only a small number of files, while reading the cache is
2119 * expensive for a large project, and its cost outweighs the
2120 * savings we get by not inflating the object to a temporary
2121 * file. Practically, this code only helps when we are used
2122 * by diff-cache --cached, which does read the cache before
2128 /* We want to avoid the working directory if our caller
2129 * doesn't need the data in a normal file, this system
2130 * is rather slow with its stat/open/mmap/close syscalls,
2131 * and the object is contained in a pack file. The pack
2132 * is probably already open and will be faster to obtain
2133 * the data through than the working directory. Loose
2134 * objects however would tend to be slower as they need
2135 * to be individually opened and inflated.
2137 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
))
2141 pos
= cache_name_pos(name
, len
);
2144 ce
= active_cache
[pos
];
2147 * This is not the sha1 we are looking for, or
2148 * unreusable because it is not a regular file.
2150 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
2154 * If ce is marked as "assume unchanged", there is no
2155 * guarantee that work tree matches what we are looking for.
2157 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2161 * If ce matches the file in the work tree, we can reuse it.
2163 if (ce_uptodate(ce
) ||
2164 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
2170 static int populate_from_stdin(struct diff_filespec
*s
)
2172 struct strbuf buf
= STRBUF_INIT
;
2175 if (strbuf_read(&buf
, 0, 0) < 0)
2176 return error("error while reading from stdin %s",
2179 s
->should_munmap
= 0;
2180 s
->data
= strbuf_detach(&buf
, &size
);
2186 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
2189 char *data
= xmalloc(100), *dirty
= "";
2191 /* Are we looking at the work tree? */
2192 if (s
->dirty_submodule
)
2195 len
= snprintf(data
, 100,
2196 "Subproject commit %s%s\n", sha1_to_hex(s
->sha1
), dirty
);
2208 * While doing rename detection and pickaxe operation, we may need to
2209 * grab the data for the blob (or file) for our own in-core comparison.
2210 * diff_filespec has data and size fields for this purpose.
2212 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
2215 if (!DIFF_FILE_VALID(s
))
2216 die("internal error: asking to populate invalid file.");
2217 if (S_ISDIR(s
->mode
))
2223 if (size_only
&& 0 < s
->size
)
2226 if (S_ISGITLINK(s
->mode
))
2227 return diff_populate_gitlink(s
, size_only
);
2229 if (!s
->sha1_valid
||
2230 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
2231 struct strbuf buf
= STRBUF_INIT
;
2235 if (!strcmp(s
->path
, "-"))
2236 return populate_from_stdin(s
);
2238 if (lstat(s
->path
, &st
) < 0) {
2239 if (errno
== ENOENT
) {
2243 s
->data
= (char *)"";
2248 s
->size
= xsize_t(st
.st_size
);
2251 if (S_ISLNK(st
.st_mode
)) {
2252 struct strbuf sb
= STRBUF_INIT
;
2254 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
2257 s
->data
= strbuf_detach(&sb
, NULL
);
2263 fd
= open(s
->path
, O_RDONLY
);
2266 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2268 s
->should_munmap
= 1;
2271 * Convert from working tree format to canonical git format
2273 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
2275 munmap(s
->data
, s
->size
);
2276 s
->should_munmap
= 0;
2277 s
->data
= strbuf_detach(&buf
, &size
);
2283 enum object_type type
;
2285 type
= sha1_object_info(s
->sha1
, &s
->size
);
2287 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
2294 void diff_free_filespec_blob(struct diff_filespec
*s
)
2298 else if (s
->should_munmap
)
2299 munmap(s
->data
, s
->size
);
2301 if (s
->should_free
|| s
->should_munmap
) {
2302 s
->should_free
= s
->should_munmap
= 0;
2307 void diff_free_filespec_data(struct diff_filespec
*s
)
2309 diff_free_filespec_blob(s
);
2314 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
2317 const unsigned char *sha1
,
2321 struct strbuf buf
= STRBUF_INIT
;
2322 struct strbuf
template = STRBUF_INIT
;
2323 char *path_dup
= xstrdup(path
);
2324 const char *base
= basename(path_dup
);
2326 /* Generate "XXXXXX_basename.ext" */
2327 strbuf_addstr(&template, "XXXXXX_");
2328 strbuf_addstr(&template, base
);
2330 fd
= git_mkstemps(temp
->tmp_path
, PATH_MAX
, template.buf
,
2333 die_errno("unable to create temp-file");
2334 if (convert_to_working_tree(path
,
2335 (const char *)blob
, (size_t)size
, &buf
)) {
2339 if (write_in_full(fd
, blob
, size
) != size
)
2340 die_errno("unable to write temp-file");
2342 temp
->name
= temp
->tmp_path
;
2343 strcpy(temp
->hex
, sha1_to_hex(sha1
));
2345 sprintf(temp
->mode
, "%06o", mode
);
2346 strbuf_release(&buf
);
2347 strbuf_release(&template);
2351 static struct diff_tempfile
*prepare_temp_file(const char *name
,
2352 struct diff_filespec
*one
)
2354 struct diff_tempfile
*temp
= claim_diff_tempfile();
2356 if (!DIFF_FILE_VALID(one
)) {
2358 /* A '-' entry produces this for file-2, and
2359 * a '+' entry produces this for file-1.
2361 temp
->name
= "/dev/null";
2362 strcpy(temp
->hex
, ".");
2363 strcpy(temp
->mode
, ".");
2367 if (!remove_tempfile_installed
) {
2368 atexit(remove_tempfile
);
2369 sigchain_push_common(remove_tempfile_on_signal
);
2370 remove_tempfile_installed
= 1;
2373 if (!one
->sha1_valid
||
2374 reuse_worktree_file(name
, one
->sha1
, 1)) {
2376 if (lstat(name
, &st
) < 0) {
2377 if (errno
== ENOENT
)
2378 goto not_a_valid_file
;
2379 die_errno("stat(%s)", name
);
2381 if (S_ISLNK(st
.st_mode
)) {
2382 struct strbuf sb
= STRBUF_INIT
;
2383 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
2384 die_errno("readlink(%s)", name
);
2385 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
2387 one
->sha1
: null_sha1
),
2389 one
->mode
: S_IFLNK
));
2390 strbuf_release(&sb
);
2393 /* we can borrow from the file in the work tree */
2395 if (!one
->sha1_valid
)
2396 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
2398 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
2399 /* Even though we may sometimes borrow the
2400 * contents from the work tree, we always want
2401 * one->mode. mode is trustworthy even when
2402 * !(one->sha1_valid), as long as
2403 * DIFF_FILE_VALID(one).
2405 sprintf(temp
->mode
, "%06o", one
->mode
);
2410 if (diff_populate_filespec(one
, 0))
2411 die("cannot read data blob for %s", one
->path
);
2412 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
2413 one
->sha1
, one
->mode
);
2418 /* An external diff command takes:
2420 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2421 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2424 static void run_external_diff(const char *pgm
,
2427 struct diff_filespec
*one
,
2428 struct diff_filespec
*two
,
2429 const char *xfrm_msg
,
2430 int complete_rewrite
)
2432 const char *spawn_arg
[10];
2434 const char **arg
= &spawn_arg
[0];
2437 struct diff_tempfile
*temp_one
, *temp_two
;
2438 const char *othername
= (other
? other
: name
);
2439 temp_one
= prepare_temp_file(name
, one
);
2440 temp_two
= prepare_temp_file(othername
, two
);
2443 *arg
++ = temp_one
->name
;
2444 *arg
++ = temp_one
->hex
;
2445 *arg
++ = temp_one
->mode
;
2446 *arg
++ = temp_two
->name
;
2447 *arg
++ = temp_two
->hex
;
2448 *arg
++ = temp_two
->mode
;
2459 retval
= run_command_v_opt(spawn_arg
, RUN_USING_SHELL
);
2462 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2467 static int similarity_index(struct diff_filepair
*p
)
2469 return p
->score
* 100 / MAX_SCORE
;
2472 static void fill_metainfo(struct strbuf
*msg
,
2475 struct diff_filespec
*one
,
2476 struct diff_filespec
*two
,
2477 struct diff_options
*o
,
2478 struct diff_filepair
*p
)
2480 struct strbuf
*msgbuf
;
2481 char *line_prefix
= "";
2483 if (o
->output_prefix
) {
2484 msgbuf
= o
->output_prefix(o
, o
->output_prefix_data
);
2485 line_prefix
= msgbuf
->buf
;
2488 strbuf_init(msg
, PATH_MAX
* 2 + 300);
2489 switch (p
->status
) {
2490 case DIFF_STATUS_COPIED
:
2491 strbuf_addf(msg
, "%ssimilarity index %d%%", line_prefix
, similarity_index(p
));
2492 strbuf_addf(msg
, "\n%scopy from ", line_prefix
);
2493 quote_c_style(name
, msg
, NULL
, 0);
2494 strbuf_addf(msg
, "\n%scopy to ", line_prefix
);
2495 quote_c_style(other
, msg
, NULL
, 0);
2496 strbuf_addch(msg
, '\n');
2498 case DIFF_STATUS_RENAMED
:
2499 strbuf_addf(msg
, "%ssimilarity index %d%%", line_prefix
, similarity_index(p
));
2500 strbuf_addf(msg
, "\n%srename from ", line_prefix
);
2501 quote_c_style(name
, msg
, NULL
, 0);
2502 strbuf_addf(msg
, "\n%srename to ", line_prefix
);
2503 quote_c_style(other
, msg
, NULL
, 0);
2504 strbuf_addch(msg
, '\n');
2506 case DIFF_STATUS_MODIFIED
:
2508 strbuf_addf(msg
, "%sdissimilarity index %d%%\n",
2509 line_prefix
, similarity_index(p
));
2517 if (one
&& two
&& hashcmp(one
->sha1
, two
->sha1
)) {
2518 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2520 if (DIFF_OPT_TST(o
, BINARY
)) {
2522 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2523 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2526 strbuf_addf(msg
, "%sindex %.*s..%.*s",
2527 line_prefix
, abbrev
, sha1_to_hex(one
->sha1
),
2528 abbrev
, sha1_to_hex(two
->sha1
));
2529 if (one
->mode
== two
->mode
)
2530 strbuf_addf(msg
, " %06o", one
->mode
);
2531 strbuf_addch(msg
, '\n');
2534 strbuf_setlen(msg
, msg
->len
- 1);
2537 static void run_diff_cmd(const char *pgm
,
2540 const char *attr_path
,
2541 struct diff_filespec
*one
,
2542 struct diff_filespec
*two
,
2544 struct diff_options
*o
,
2545 struct diff_filepair
*p
)
2547 const char *xfrm_msg
= NULL
;
2548 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
2551 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
);
2552 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
2555 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2558 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
2559 if (drv
&& drv
->external
)
2560 pgm
= drv
->external
;
2564 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2569 builtin_diff(name
, other
? other
: name
,
2570 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2572 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2575 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2577 if (DIFF_FILE_VALID(one
)) {
2578 if (!one
->sha1_valid
) {
2580 if (!strcmp(one
->path
, "-")) {
2581 hashcpy(one
->sha1
, null_sha1
);
2584 if (lstat(one
->path
, &st
) < 0)
2585 die_errno("stat '%s'", one
->path
);
2586 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2587 die("cannot hash %s", one
->path
);
2594 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2596 /* Strip the prefix but do not molest /dev/null and absolute paths */
2597 if (*namep
&& **namep
!= '/')
2598 *namep
+= prefix_length
;
2599 if (*otherp
&& **otherp
!= '/')
2600 *otherp
+= prefix_length
;
2603 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2605 const char *pgm
= external_diff();
2607 struct diff_filespec
*one
= p
->one
;
2608 struct diff_filespec
*two
= p
->two
;
2611 const char *attr_path
;
2613 name
= p
->one
->path
;
2614 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2616 if (o
->prefix_length
)
2617 strip_prefix(o
->prefix_length
, &name
, &other
);
2619 if (DIFF_PAIR_UNMERGED(p
)) {
2620 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2621 NULL
, NULL
, NULL
, o
, p
);
2625 diff_fill_sha1_info(one
);
2626 diff_fill_sha1_info(two
);
2629 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2630 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2632 * a filepair that changes between file and symlink
2633 * needs to be split into deletion and creation.
2635 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2636 run_diff_cmd(NULL
, name
, other
, attr_path
,
2637 one
, null
, &msg
, o
, p
);
2639 strbuf_release(&msg
);
2641 null
= alloc_filespec(one
->path
);
2642 run_diff_cmd(NULL
, name
, other
, attr_path
,
2643 null
, two
, &msg
, o
, p
);
2647 run_diff_cmd(pgm
, name
, other
, attr_path
,
2648 one
, two
, &msg
, o
, p
);
2650 strbuf_release(&msg
);
2653 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2654 struct diffstat_t
*diffstat
)
2658 int complete_rewrite
= 0;
2660 if (DIFF_PAIR_UNMERGED(p
)) {
2662 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2666 name
= p
->one
->path
;
2667 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2669 if (o
->prefix_length
)
2670 strip_prefix(o
->prefix_length
, &name
, &other
);
2672 diff_fill_sha1_info(p
->one
);
2673 diff_fill_sha1_info(p
->two
);
2675 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2676 complete_rewrite
= 1;
2677 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2680 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2684 const char *attr_path
;
2686 if (DIFF_PAIR_UNMERGED(p
)) {
2691 name
= p
->one
->path
;
2692 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2693 attr_path
= other
? other
: name
;
2695 if (o
->prefix_length
)
2696 strip_prefix(o
->prefix_length
, &name
, &other
);
2698 diff_fill_sha1_info(p
->one
);
2699 diff_fill_sha1_info(p
->two
);
2701 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2704 void diff_setup(struct diff_options
*options
)
2706 memset(options
, 0, sizeof(*options
));
2707 memset(&diff_queued_diff
, 0, sizeof(diff_queued_diff
));
2709 options
->file
= stdout
;
2711 options
->line_termination
= '\n';
2712 options
->break_opt
= -1;
2713 options
->rename_limit
= -1;
2714 options
->dirstat_percent
= 3;
2715 options
->context
= 3;
2717 options
->change
= diff_change
;
2718 options
->add_remove
= diff_addremove
;
2719 if (diff_use_color_default
> 0)
2720 DIFF_OPT_SET(options
, COLOR_DIFF
);
2721 options
->detect_rename
= diff_detect_rename_default
;
2723 if (!diff_mnemonic_prefix
) {
2724 options
->a_prefix
= "a/";
2725 options
->b_prefix
= "b/";
2729 int diff_setup_done(struct diff_options
*options
)
2733 if (options
->output_format
& DIFF_FORMAT_NAME
)
2735 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2737 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2739 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2742 die("--name-only, --name-status, --check and -s are mutually exclusive");
2745 * Most of the time we can say "there are changes"
2746 * only by checking if there are changed paths, but
2747 * --ignore-whitespace* options force us to look
2751 if (DIFF_XDL_TST(options
, IGNORE_WHITESPACE
) ||
2752 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_CHANGE
) ||
2753 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_AT_EOL
))
2754 DIFF_OPT_SET(options
, DIFF_FROM_CONTENTS
);
2756 DIFF_OPT_CLR(options
, DIFF_FROM_CONTENTS
);
2758 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2759 options
->detect_rename
= DIFF_DETECT_COPY
;
2761 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2762 options
->prefix
= NULL
;
2763 if (options
->prefix
)
2764 options
->prefix_length
= strlen(options
->prefix
);
2766 options
->prefix_length
= 0;
2768 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2769 DIFF_FORMAT_NAME_STATUS
|
2770 DIFF_FORMAT_CHECKDIFF
|
2771 DIFF_FORMAT_NO_OUTPUT
))
2772 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2773 DIFF_FORMAT_NUMSTAT
|
2774 DIFF_FORMAT_DIFFSTAT
|
2775 DIFF_FORMAT_SHORTSTAT
|
2776 DIFF_FORMAT_DIRSTAT
|
2777 DIFF_FORMAT_SUMMARY
|
2781 * These cases always need recursive; we do not drop caller-supplied
2782 * recursive bits for other formats here.
2784 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2785 DIFF_FORMAT_NUMSTAT
|
2786 DIFF_FORMAT_DIFFSTAT
|
2787 DIFF_FORMAT_SHORTSTAT
|
2788 DIFF_FORMAT_DIRSTAT
|
2789 DIFF_FORMAT_SUMMARY
|
2790 DIFF_FORMAT_CHECKDIFF
))
2791 DIFF_OPT_SET(options
, RECURSIVE
);
2793 * Also pickaxe would not work very well if you do not say recursive
2795 if (options
->pickaxe
)
2796 DIFF_OPT_SET(options
, RECURSIVE
);
2798 * When patches are generated, submodules diffed against the work tree
2799 * must be checked for dirtiness too so it can be shown in the output
2801 if (options
->output_format
& DIFF_FORMAT_PATCH
)
2802 DIFF_OPT_SET(options
, DIRTY_SUBMODULES
);
2804 if (options
->detect_rename
&& options
->rename_limit
< 0)
2805 options
->rename_limit
= diff_rename_limit_default
;
2806 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2808 /* read-cache does not die even when it fails
2809 * so it is safe for us to do this here. Also
2810 * it does not smudge active_cache or active_nr
2811 * when it fails, so we do not have to worry about
2812 * cleaning it up ourselves either.
2816 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2817 options
->abbrev
= 40; /* full */
2820 * It does not make sense to show the first hit we happened
2821 * to have found. It does not make sense not to return with
2822 * exit code in such a case either.
2824 if (DIFF_OPT_TST(options
, QUICK
)) {
2825 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2826 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2832 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2842 if (c
== arg_short
) {
2846 if (val
&& isdigit(c
)) {
2848 int n
= strtoul(arg
, &end
, 10);
2859 eq
= strchr(arg
, '=');
2864 if (!len
|| strncmp(arg
, arg_long
, len
))
2869 if (!isdigit(*++eq
))
2871 n
= strtoul(eq
, &end
, 10);
2879 static int diff_scoreopt_parse(const char *opt
);
2881 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2883 const char *arg
= av
[0];
2885 /* Output format options */
2886 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u") || !strcmp(arg
, "--patch"))
2887 options
->output_format
|= DIFF_FORMAT_PATCH
;
2888 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2889 options
->output_format
|= DIFF_FORMAT_PATCH
;
2890 else if (!strcmp(arg
, "--raw"))
2891 options
->output_format
|= DIFF_FORMAT_RAW
;
2892 else if (!strcmp(arg
, "--patch-with-raw"))
2893 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2894 else if (!strcmp(arg
, "--numstat"))
2895 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2896 else if (!strcmp(arg
, "--shortstat"))
2897 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2898 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2899 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2900 else if (!strcmp(arg
, "--cumulative")) {
2901 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2902 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
2903 } else if (opt_arg(arg
, 0, "dirstat-by-file",
2904 &options
->dirstat_percent
)) {
2905 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2906 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
2908 else if (!strcmp(arg
, "--check"))
2909 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2910 else if (!strcmp(arg
, "--summary"))
2911 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2912 else if (!strcmp(arg
, "--patch-with-stat"))
2913 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2914 else if (!strcmp(arg
, "--name-only"))
2915 options
->output_format
|= DIFF_FORMAT_NAME
;
2916 else if (!strcmp(arg
, "--name-status"))
2917 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2918 else if (!strcmp(arg
, "-s"))
2919 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2920 else if (!prefixcmp(arg
, "--stat")) {
2922 int width
= options
->stat_width
;
2923 int name_width
= options
->stat_name_width
;
2929 if (!prefixcmp(arg
, "-width="))
2930 width
= strtoul(arg
+ 7, &end
, 10);
2931 else if (!prefixcmp(arg
, "-name-width="))
2932 name_width
= strtoul(arg
+ 12, &end
, 10);
2935 width
= strtoul(arg
+1, &end
, 10);
2937 name_width
= strtoul(end
+1, &end
, 10);
2940 /* Important! This checks all the error cases! */
2943 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2944 options
->stat_name_width
= name_width
;
2945 options
->stat_width
= width
;
2948 /* renames options */
2949 else if (!prefixcmp(arg
, "-B")) {
2950 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2953 else if (!prefixcmp(arg
, "-M")) {
2954 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2956 options
->detect_rename
= DIFF_DETECT_RENAME
;
2958 else if (!prefixcmp(arg
, "-C")) {
2959 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2960 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2961 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2963 options
->detect_rename
= DIFF_DETECT_COPY
;
2965 else if (!strcmp(arg
, "--no-renames"))
2966 options
->detect_rename
= 0;
2967 else if (!strcmp(arg
, "--relative"))
2968 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2969 else if (!prefixcmp(arg
, "--relative=")) {
2970 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2971 options
->prefix
= arg
+ 11;
2975 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2976 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
2977 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2978 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
2979 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2980 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
2981 else if (!strcmp(arg
, "--patience"))
2982 DIFF_XDL_SET(options
, PATIENCE_DIFF
);
2985 else if (!strcmp(arg
, "--binary")) {
2986 options
->output_format
|= DIFF_FORMAT_PATCH
;
2987 DIFF_OPT_SET(options
, BINARY
);
2989 else if (!strcmp(arg
, "--full-index"))
2990 DIFF_OPT_SET(options
, FULL_INDEX
);
2991 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2992 DIFF_OPT_SET(options
, TEXT
);
2993 else if (!strcmp(arg
, "-R"))
2994 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2995 else if (!strcmp(arg
, "--find-copies-harder"))
2996 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2997 else if (!strcmp(arg
, "--follow"))
2998 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2999 else if (!strcmp(arg
, "--color"))
3000 DIFF_OPT_SET(options
, COLOR_DIFF
);
3001 else if (!prefixcmp(arg
, "--color=")) {
3002 int value
= git_config_colorbool(NULL
, arg
+8, -1);
3004 DIFF_OPT_CLR(options
, COLOR_DIFF
);
3006 DIFF_OPT_SET(options
, COLOR_DIFF
);
3008 return error("option `color' expects \"always\", \"auto\", or \"never\"");
3010 else if (!strcmp(arg
, "--no-color"))
3011 DIFF_OPT_CLR(options
, COLOR_DIFF
);
3012 else if (!strcmp(arg
, "--color-words")) {
3013 DIFF_OPT_SET(options
, COLOR_DIFF
);
3014 options
->word_diff
= DIFF_WORDS_COLOR
;
3016 else if (!prefixcmp(arg
, "--color-words=")) {
3017 DIFF_OPT_SET(options
, COLOR_DIFF
);
3018 options
->word_diff
= DIFF_WORDS_COLOR
;
3019 options
->word_regex
= arg
+ 14;
3021 else if (!strcmp(arg
, "--word-diff")) {
3022 if (options
->word_diff
== DIFF_WORDS_NONE
)
3023 options
->word_diff
= DIFF_WORDS_PLAIN
;
3025 else if (!prefixcmp(arg
, "--word-diff=")) {
3026 const char *type
= arg
+ 12;
3027 if (!strcmp(type
, "plain"))
3028 options
->word_diff
= DIFF_WORDS_PLAIN
;
3029 else if (!strcmp(type
, "color")) {
3030 DIFF_OPT_SET(options
, COLOR_DIFF
);
3031 options
->word_diff
= DIFF_WORDS_COLOR
;
3033 else if (!strcmp(type
, "porcelain"))
3034 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
3035 else if (!strcmp(type
, "none"))
3036 options
->word_diff
= DIFF_WORDS_NONE
;
3038 die("bad --word-diff argument: %s", type
);
3040 else if (!prefixcmp(arg
, "--word-diff-regex=")) {
3041 if (options
->word_diff
== DIFF_WORDS_NONE
)
3042 options
->word_diff
= DIFF_WORDS_PLAIN
;
3043 options
->word_regex
= arg
+ 18;
3045 else if (!strcmp(arg
, "--exit-code"))
3046 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
3047 else if (!strcmp(arg
, "--quiet"))
3048 DIFF_OPT_SET(options
, QUICK
);
3049 else if (!strcmp(arg
, "--ext-diff"))
3050 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
3051 else if (!strcmp(arg
, "--no-ext-diff"))
3052 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
3053 else if (!strcmp(arg
, "--textconv"))
3054 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
3055 else if (!strcmp(arg
, "--no-textconv"))
3056 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
3057 else if (!strcmp(arg
, "--ignore-submodules"))
3058 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
3059 else if (!strcmp(arg
, "--submodule"))
3060 DIFF_OPT_SET(options
, SUBMODULE_LOG
);
3061 else if (!prefixcmp(arg
, "--submodule=")) {
3062 if (!strcmp(arg
+ 12, "log"))
3063 DIFF_OPT_SET(options
, SUBMODULE_LOG
);
3067 else if (!strcmp(arg
, "-z"))
3068 options
->line_termination
= 0;
3069 else if (!prefixcmp(arg
, "-l"))
3070 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
3071 else if (!prefixcmp(arg
, "-S"))
3072 options
->pickaxe
= arg
+ 2;
3073 else if (!strcmp(arg
, "--pickaxe-all"))
3074 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
3075 else if (!strcmp(arg
, "--pickaxe-regex"))
3076 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
3077 else if (!prefixcmp(arg
, "-O"))
3078 options
->orderfile
= arg
+ 2;
3079 else if (!prefixcmp(arg
, "--diff-filter="))
3080 options
->filter
= arg
+ 14;
3081 else if (!strcmp(arg
, "--abbrev"))
3082 options
->abbrev
= DEFAULT_ABBREV
;
3083 else if (!prefixcmp(arg
, "--abbrev=")) {
3084 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
3085 if (options
->abbrev
< MINIMUM_ABBREV
)
3086 options
->abbrev
= MINIMUM_ABBREV
;
3087 else if (40 < options
->abbrev
)
3088 options
->abbrev
= 40;
3090 else if (!prefixcmp(arg
, "--src-prefix="))
3091 options
->a_prefix
= arg
+ 13;
3092 else if (!prefixcmp(arg
, "--dst-prefix="))
3093 options
->b_prefix
= arg
+ 13;
3094 else if (!strcmp(arg
, "--no-prefix"))
3095 options
->a_prefix
= options
->b_prefix
= "";
3096 else if (opt_arg(arg
, '\0', "inter-hunk-context",
3097 &options
->interhunkcontext
))
3099 else if (!prefixcmp(arg
, "--output=")) {
3100 options
->file
= fopen(arg
+ strlen("--output="), "w");
3102 die_errno("Could not open '%s'", arg
+ strlen("--output="));
3103 options
->close_file
= 1;
3109 static int parse_num(const char **cp_p
)
3111 unsigned long num
, scale
;
3113 const char *cp
= *cp_p
;
3120 if ( !dot
&& ch
== '.' ) {
3123 } else if ( ch
== '%' ) {
3124 scale
= dot
? scale
*100 : 100;
3125 cp
++; /* % is always at the end */
3127 } else if ( ch
>= '0' && ch
<= '9' ) {
3128 if ( scale
< 100000 ) {
3130 num
= (num
*10) + (ch
-'0');
3139 /* user says num divided by scale and we say internally that
3140 * is MAX_SCORE * num / scale.
3142 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
3145 static int diff_scoreopt_parse(const char *opt
)
3147 int opt1
, opt2
, cmd
;
3152 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
3153 return -1; /* that is not a -M, -C nor -B option */
3155 opt1
= parse_num(&opt
);
3161 else if (*opt
!= '/')
3162 return -1; /* we expect -B80/99 or -B80 */
3165 opt2
= parse_num(&opt
);
3170 return opt1
| (opt2
<< 16);
3173 struct diff_queue_struct diff_queued_diff
;
3175 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
3177 if (queue
->alloc
<= queue
->nr
) {
3178 queue
->alloc
= alloc_nr(queue
->alloc
);
3179 queue
->queue
= xrealloc(queue
->queue
,
3180 sizeof(dp
) * queue
->alloc
);
3182 queue
->queue
[queue
->nr
++] = dp
;
3185 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
3186 struct diff_filespec
*one
,
3187 struct diff_filespec
*two
)
3189 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
3197 void diff_free_filepair(struct diff_filepair
*p
)
3199 free_filespec(p
->one
);
3200 free_filespec(p
->two
);
3204 /* This is different from find_unique_abbrev() in that
3205 * it stuffs the result with dots for alignment.
3207 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
3212 return sha1_to_hex(sha1
);
3214 abbrev
= find_unique_abbrev(sha1
, len
);
3215 abblen
= strlen(abbrev
);
3217 static char hex
[41];
3218 if (len
< abblen
&& abblen
<= len
+ 2)
3219 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
3221 sprintf(hex
, "%s...", abbrev
);
3224 return sha1_to_hex(sha1
);
3227 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
3229 int line_termination
= opt
->line_termination
;
3230 int inter_name_termination
= line_termination
? '\t' : '\0';
3231 if (opt
->output_prefix
) {
3232 struct strbuf
*msg
= NULL
;
3233 msg
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
3234 fprintf(opt
->file
, "%s", msg
->buf
);
3237 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
3238 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
3239 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
3240 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
3243 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
3244 inter_name_termination
);
3246 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
3249 if (p
->status
== DIFF_STATUS_COPIED
||
3250 p
->status
== DIFF_STATUS_RENAMED
) {
3251 const char *name_a
, *name_b
;
3252 name_a
= p
->one
->path
;
3253 name_b
= p
->two
->path
;
3254 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3255 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
3256 write_name_quoted(name_b
, opt
->file
, line_termination
);
3258 const char *name_a
, *name_b
;
3259 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
3261 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3262 write_name_quoted(name_a
, opt
->file
, line_termination
);
3266 int diff_unmodified_pair(struct diff_filepair
*p
)
3268 /* This function is written stricter than necessary to support
3269 * the currently implemented transformers, but the idea is to
3270 * let transformers to produce diff_filepairs any way they want,
3271 * and filter and clean them up here before producing the output.
3273 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
3275 if (DIFF_PAIR_UNMERGED(p
))
3276 return 0; /* unmerged is interesting */
3278 /* deletion, addition, mode or type change
3279 * and rename are all interesting.
3281 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
3282 DIFF_PAIR_MODE_CHANGED(p
) ||
3283 strcmp(one
->path
, two
->path
))
3286 /* both are valid and point at the same path. that is, we are
3287 * dealing with a change.
3289 if (one
->sha1_valid
&& two
->sha1_valid
&&
3290 !hashcmp(one
->sha1
, two
->sha1
) &&
3291 !one
->dirty_submodule
&& !two
->dirty_submodule
)
3292 return 1; /* no change */
3293 if (!one
->sha1_valid
&& !two
->sha1_valid
)
3294 return 1; /* both look at the same file on the filesystem. */
3298 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
3300 if (diff_unmodified_pair(p
))
3303 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3304 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3305 return; /* no tree diffs in patch format */
3310 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
3311 struct diffstat_t
*diffstat
)
3313 if (diff_unmodified_pair(p
))
3316 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3317 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3318 return; /* no tree diffs in patch format */
3320 run_diffstat(p
, o
, diffstat
);
3323 static void diff_flush_checkdiff(struct diff_filepair
*p
,
3324 struct diff_options
*o
)
3326 if (diff_unmodified_pair(p
))
3329 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3330 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3331 return; /* no tree diffs in patch format */
3333 run_checkdiff(p
, o
);
3336 int diff_queue_is_empty(void)
3338 struct diff_queue_struct
*q
= &diff_queued_diff
;
3340 for (i
= 0; i
< q
->nr
; i
++)
3341 if (!diff_unmodified_pair(q
->queue
[i
]))
3347 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
3349 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
3352 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
3354 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
3355 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
3357 s
->size
, s
->xfrm_flags
);
3360 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
3362 diff_debug_filespec(p
->one
, i
, "one");
3363 diff_debug_filespec(p
->two
, i
, "two");
3364 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
3365 p
->score
, p
->status
? p
->status
: '?',
3366 p
->one
->rename_used
, p
->broken_pair
);
3369 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
3373 fprintf(stderr
, "%s\n", msg
);
3374 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
3375 for (i
= 0; i
< q
->nr
; i
++) {
3376 struct diff_filepair
*p
= q
->queue
[i
];
3377 diff_debug_filepair(p
, i
);
3382 static void diff_resolve_rename_copy(void)
3385 struct diff_filepair
*p
;
3386 struct diff_queue_struct
*q
= &diff_queued_diff
;
3388 diff_debug_queue("resolve-rename-copy", q
);
3390 for (i
= 0; i
< q
->nr
; i
++) {
3392 p
->status
= 0; /* undecided */
3393 if (DIFF_PAIR_UNMERGED(p
))
3394 p
->status
= DIFF_STATUS_UNMERGED
;
3395 else if (!DIFF_FILE_VALID(p
->one
))
3396 p
->status
= DIFF_STATUS_ADDED
;
3397 else if (!DIFF_FILE_VALID(p
->two
))
3398 p
->status
= DIFF_STATUS_DELETED
;
3399 else if (DIFF_PAIR_TYPE_CHANGED(p
))
3400 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
3402 /* from this point on, we are dealing with a pair
3403 * whose both sides are valid and of the same type, i.e.
3404 * either in-place edit or rename/copy edit.
3406 else if (DIFF_PAIR_RENAME(p
)) {
3408 * A rename might have re-connected a broken
3409 * pair up, causing the pathnames to be the
3410 * same again. If so, that's not a rename at
3411 * all, just a modification..
3413 * Otherwise, see if this source was used for
3414 * multiple renames, in which case we decrement
3415 * the count, and call it a copy.
3417 if (!strcmp(p
->one
->path
, p
->two
->path
))
3418 p
->status
= DIFF_STATUS_MODIFIED
;
3419 else if (--p
->one
->rename_used
> 0)
3420 p
->status
= DIFF_STATUS_COPIED
;
3422 p
->status
= DIFF_STATUS_RENAMED
;
3424 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
3425 p
->one
->mode
!= p
->two
->mode
||
3426 p
->one
->dirty_submodule
||
3427 p
->two
->dirty_submodule
||
3428 is_null_sha1(p
->one
->sha1
))
3429 p
->status
= DIFF_STATUS_MODIFIED
;
3431 /* This is a "no-change" entry and should not
3432 * happen anymore, but prepare for broken callers.
3434 error("feeding unmodified %s to diffcore",
3436 p
->status
= DIFF_STATUS_UNKNOWN
;
3439 diff_debug_queue("resolve-rename-copy done", q
);
3442 static int check_pair_status(struct diff_filepair
*p
)
3444 switch (p
->status
) {
3445 case DIFF_STATUS_UNKNOWN
:
3448 die("internal error in diff-resolve-rename-copy");
3454 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
3456 int fmt
= opt
->output_format
;
3458 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
3459 diff_flush_checkdiff(p
, opt
);
3460 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
3461 diff_flush_raw(p
, opt
);
3462 else if (fmt
& DIFF_FORMAT_NAME
) {
3463 const char *name_a
, *name_b
;
3464 name_a
= p
->two
->path
;
3466 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3467 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
3471 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
3474 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
3476 fprintf(file
, " %s ", newdelete
);
3477 write_name_quoted(fs
->path
, file
, '\n');
3481 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
,
3482 const char *line_prefix
)
3484 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
3485 fprintf(file
, "%s mode change %06o => %06o%c", line_prefix
, p
->one
->mode
,
3486 p
->two
->mode
, show_name
? ' ' : '\n');
3488 write_name_quoted(p
->two
->path
, file
, '\n');
3493 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
,
3494 const char *line_prefix
)
3496 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
3498 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
3500 show_mode_change(file
, p
, 0, line_prefix
);
3503 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
3505 FILE *file
= opt
->file
;
3506 char *line_prefix
= "";
3508 if (opt
->output_prefix
) {
3509 struct strbuf
*buf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
3510 line_prefix
= buf
->buf
;
3514 case DIFF_STATUS_DELETED
:
3515 fputs(line_prefix
, file
);
3516 show_file_mode_name(file
, "delete", p
->one
);
3518 case DIFF_STATUS_ADDED
:
3519 fputs(line_prefix
, file
);
3520 show_file_mode_name(file
, "create", p
->two
);
3522 case DIFF_STATUS_COPIED
:
3523 fputs(line_prefix
, file
);
3524 show_rename_copy(file
, "copy", p
, line_prefix
);
3526 case DIFF_STATUS_RENAMED
:
3527 fputs(line_prefix
, file
);
3528 show_rename_copy(file
, "rename", p
, line_prefix
);
3532 fprintf(file
, "%s rewrite ", line_prefix
);
3533 write_name_quoted(p
->two
->path
, file
, ' ');
3534 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3536 show_mode_change(file
, p
, !p
->score
, line_prefix
);
3546 static int remove_space(char *line
, int len
)
3552 for (i
= 0; i
< len
; i
++)
3553 if (!isspace((c
= line
[i
])))
3559 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3561 struct patch_id_t
*data
= priv
;
3564 /* Ignore line numbers when computing the SHA1 of the patch */
3565 if (!prefixcmp(line
, "@@ -"))
3568 new_len
= remove_space(line
, len
);
3570 git_SHA1_Update(data
->ctx
, line
, new_len
);
3571 data
->patchlen
+= new_len
;
3574 /* returns 0 upon success, and writes result into sha1 */
3575 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3577 struct diff_queue_struct
*q
= &diff_queued_diff
;
3580 struct patch_id_t data
;
3581 char buffer
[PATH_MAX
* 4 + 20];
3583 git_SHA1_Init(&ctx
);
3584 memset(&data
, 0, sizeof(struct patch_id_t
));
3587 for (i
= 0; i
< q
->nr
; i
++) {
3591 struct diff_filepair
*p
= q
->queue
[i
];
3594 memset(&xpp
, 0, sizeof(xpp
));
3595 memset(&xecfg
, 0, sizeof(xecfg
));
3597 return error("internal diff status error");
3598 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3600 if (diff_unmodified_pair(p
))
3602 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3603 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3605 if (DIFF_PAIR_UNMERGED(p
))
3608 diff_fill_sha1_info(p
->one
);
3609 diff_fill_sha1_info(p
->two
);
3610 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3611 fill_mmfile(&mf2
, p
->two
) < 0)
3612 return error("unable to read files to diff");
3614 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3615 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3616 if (p
->one
->mode
== 0)
3617 len1
= snprintf(buffer
, sizeof(buffer
),
3618 "diff--gita/%.*sb/%.*s"
3625 len2
, p
->two
->path
);
3626 else if (p
->two
->mode
== 0)
3627 len1
= snprintf(buffer
, sizeof(buffer
),
3628 "diff--gita/%.*sb/%.*s"
3629 "deletedfilemode%06o"
3635 len1
, p
->one
->path
);
3637 len1
= snprintf(buffer
, sizeof(buffer
),
3638 "diff--gita/%.*sb/%.*s"
3644 len2
, p
->two
->path
);
3645 git_SHA1_Update(&ctx
, buffer
, len1
);
3647 xpp
.flags
= XDF_NEED_MINIMAL
;
3649 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3650 xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
3654 git_SHA1_Final(sha1
, &ctx
);
3658 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3660 struct diff_queue_struct
*q
= &diff_queued_diff
;
3662 int result
= diff_get_patch_id(options
, sha1
);
3664 for (i
= 0; i
< q
->nr
; i
++)
3665 diff_free_filepair(q
->queue
[i
]);
3668 DIFF_QUEUE_CLEAR(q
);
3673 static int is_summary_empty(const struct diff_queue_struct
*q
)
3677 for (i
= 0; i
< q
->nr
; i
++) {
3678 const struct diff_filepair
*p
= q
->queue
[i
];
3680 switch (p
->status
) {
3681 case DIFF_STATUS_DELETED
:
3682 case DIFF_STATUS_ADDED
:
3683 case DIFF_STATUS_COPIED
:
3684 case DIFF_STATUS_RENAMED
:
3689 if (p
->one
->mode
&& p
->two
->mode
&&
3690 p
->one
->mode
!= p
->two
->mode
)
3698 void diff_flush(struct diff_options
*options
)
3700 struct diff_queue_struct
*q
= &diff_queued_diff
;
3701 int i
, output_format
= options
->output_format
;
3705 * Order: raw, stat, summary, patch
3706 * or: name/name-status/checkdiff (other bits clear)
3711 if (output_format
& (DIFF_FORMAT_RAW
|
3713 DIFF_FORMAT_NAME_STATUS
|
3714 DIFF_FORMAT_CHECKDIFF
)) {
3715 for (i
= 0; i
< q
->nr
; i
++) {
3716 struct diff_filepair
*p
= q
->queue
[i
];
3717 if (check_pair_status(p
))
3718 flush_one_pair(p
, options
);
3723 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3724 struct diffstat_t diffstat
;
3726 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3727 for (i
= 0; i
< q
->nr
; i
++) {
3728 struct diff_filepair
*p
= q
->queue
[i
];
3729 if (check_pair_status(p
))
3730 diff_flush_stat(p
, options
, &diffstat
);
3732 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3733 show_numstat(&diffstat
, options
);
3734 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3735 show_stats(&diffstat
, options
);
3736 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3737 show_shortstats(&diffstat
, options
);
3738 free_diffstat_info(&diffstat
);
3741 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3742 show_dirstat(options
);
3744 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3745 for (i
= 0; i
< q
->nr
; i
++) {
3746 diff_summary(options
, q
->queue
[i
]);
3751 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
3752 DIFF_OPT_TST(options
, EXIT_WITH_STATUS
) &&
3753 DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
3755 * run diff_flush_patch for the exit status. setting
3756 * options->file to /dev/null should be safe, becaue we
3757 * aren't supposed to produce any output anyway.
3759 if (options
->close_file
)
3760 fclose(options
->file
);
3761 options
->file
= fopen("/dev/null", "w");
3763 die_errno("Could not open /dev/null");
3764 options
->close_file
= 1;
3765 for (i
= 0; i
< q
->nr
; i
++) {
3766 struct diff_filepair
*p
= q
->queue
[i
];
3767 if (check_pair_status(p
))
3768 diff_flush_patch(p
, options
);
3769 if (options
->found_changes
)
3774 if (output_format
& DIFF_FORMAT_PATCH
) {
3776 putc(options
->line_termination
, options
->file
);
3777 if (options
->stat_sep
) {
3778 /* attach patch instead of inline */
3779 fputs(options
->stat_sep
, options
->file
);
3783 for (i
= 0; i
< q
->nr
; i
++) {
3784 struct diff_filepair
*p
= q
->queue
[i
];
3785 if (check_pair_status(p
))
3786 diff_flush_patch(p
, options
);
3790 if (output_format
& DIFF_FORMAT_CALLBACK
)
3791 options
->format_callback(q
, options
, options
->format_callback_data
);
3793 for (i
= 0; i
< q
->nr
; i
++)
3794 diff_free_filepair(q
->queue
[i
]);
3797 DIFF_QUEUE_CLEAR(q
);
3798 if (options
->close_file
)
3799 fclose(options
->file
);
3802 * Report the content-level differences with HAS_CHANGES;
3803 * diff_addremove/diff_change does not set the bit when
3804 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
3806 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
3807 if (options
->found_changes
)
3808 DIFF_OPT_SET(options
, HAS_CHANGES
);
3810 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3814 static void diffcore_apply_filter(const char *filter
)
3817 struct diff_queue_struct
*q
= &diff_queued_diff
;
3818 struct diff_queue_struct outq
;
3819 DIFF_QUEUE_CLEAR(&outq
);
3824 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3826 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3827 struct diff_filepair
*p
= q
->queue
[i
];
3828 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3830 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3832 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3833 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3834 strchr(filter
, p
->status
)))
3840 /* otherwise we will clear the whole queue
3841 * by copying the empty outq at the end of this
3842 * function, but first clear the current entries
3845 for (i
= 0; i
< q
->nr
; i
++)
3846 diff_free_filepair(q
->queue
[i
]);
3849 /* Only the matching ones */
3850 for (i
= 0; i
< q
->nr
; i
++) {
3851 struct diff_filepair
*p
= q
->queue
[i
];
3853 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3855 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3857 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3858 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3859 strchr(filter
, p
->status
)))
3862 diff_free_filepair(p
);
3869 /* Check whether two filespecs with the same mode and size are identical */
3870 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3871 struct diff_filespec
*two
)
3873 if (S_ISGITLINK(one
->mode
))
3875 if (diff_populate_filespec(one
, 0))
3877 if (diff_populate_filespec(two
, 0))
3879 return !memcmp(one
->data
, two
->data
, one
->size
);
3882 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3885 struct diff_queue_struct
*q
= &diff_queued_diff
;
3886 struct diff_queue_struct outq
;
3887 DIFF_QUEUE_CLEAR(&outq
);
3889 for (i
= 0; i
< q
->nr
; i
++) {
3890 struct diff_filepair
*p
= q
->queue
[i
];
3893 * 1. Entries that come from stat info dirtiness
3894 * always have both sides (iow, not create/delete),
3895 * one side of the object name is unknown, with
3896 * the same mode and size. Keep the ones that
3897 * do not match these criteria. They have real
3900 * 2. At this point, the file is known to be modified,
3901 * with the same mode and size, and the object
3902 * name of one side is unknown. Need to inspect
3903 * the identical contents.
3905 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3906 !DIFF_FILE_VALID(p
->two
) ||
3907 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3908 (p
->one
->mode
!= p
->two
->mode
) ||
3909 diff_populate_filespec(p
->one
, 1) ||
3910 diff_populate_filespec(p
->two
, 1) ||
3911 (p
->one
->size
!= p
->two
->size
) ||
3912 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3916 * The caller can subtract 1 from skip_stat_unmatch
3917 * to determine how many paths were dirty only
3918 * due to stat info mismatch.
3920 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3921 diffopt
->skip_stat_unmatch
++;
3922 diff_free_filepair(p
);
3929 static int diffnamecmp(const void *a_
, const void *b_
)
3931 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
3932 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
3933 const char *name_a
, *name_b
;
3935 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
3936 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
3937 return strcmp(name_a
, name_b
);
3940 void diffcore_fix_diff_index(struct diff_options
*options
)
3942 struct diff_queue_struct
*q
= &diff_queued_diff
;
3943 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), diffnamecmp
);
3946 void diffcore_std(struct diff_options
*options
)
3948 /* We never run this function more than one time, because the
3949 * rename/copy detection logic can only run once.
3951 if (diff_queued_diff
.run
)
3954 if (options
->skip_stat_unmatch
)
3955 diffcore_skip_stat_unmatch(options
);
3956 if (options
->break_opt
!= -1)
3957 diffcore_break(options
->break_opt
);
3958 if (options
->detect_rename
)
3959 diffcore_rename(options
);
3960 if (options
->break_opt
!= -1)
3961 diffcore_merge_broken();
3962 if (options
->pickaxe
)
3963 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3964 if (options
->orderfile
)
3965 diffcore_order(options
->orderfile
);
3966 diff_resolve_rename_copy();
3967 diffcore_apply_filter(options
->filter
);
3969 if (diff_queued_diff
.nr
&& !DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3970 DIFF_OPT_SET(options
, HAS_CHANGES
);
3972 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3974 diff_queued_diff
.run
= 1;
3977 int diff_result_code(struct diff_options
*opt
, int status
)
3980 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3981 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3983 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3984 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3986 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3987 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3992 void diff_addremove(struct diff_options
*options
,
3993 int addremove
, unsigned mode
,
3994 const unsigned char *sha1
,
3995 const char *concatpath
, unsigned dirty_submodule
)
3997 struct diff_filespec
*one
, *two
;
3999 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
4002 /* This may look odd, but it is a preparation for
4003 * feeding "there are unchanged files which should
4004 * not produce diffs, but when you are doing copy
4005 * detection you would need them, so here they are"
4006 * entries to the diff-core. They will be prefixed
4007 * with something like '=' or '*' (I haven't decided
4008 * which but should not make any difference).
4009 * Feeding the same new and old to diff_change()
4010 * also has the same effect.
4011 * Before the final output happens, they are pruned after
4012 * merged into rename/copy pairs as appropriate.
4014 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
4015 addremove
= (addremove
== '+' ? '-' :
4016 addremove
== '-' ? '+' : addremove
);
4018 if (options
->prefix
&&
4019 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
4022 one
= alloc_filespec(concatpath
);
4023 two
= alloc_filespec(concatpath
);
4025 if (addremove
!= '+')
4026 fill_filespec(one
, sha1
, mode
);
4027 if (addremove
!= '-') {
4028 fill_filespec(two
, sha1
, mode
);
4029 two
->dirty_submodule
= dirty_submodule
;
4032 diff_queue(&diff_queued_diff
, one
, two
);
4033 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
4034 DIFF_OPT_SET(options
, HAS_CHANGES
);
4037 void diff_change(struct diff_options
*options
,
4038 unsigned old_mode
, unsigned new_mode
,
4039 const unsigned char *old_sha1
,
4040 const unsigned char *new_sha1
,
4041 const char *concatpath
,
4042 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
4044 struct diff_filespec
*one
, *two
;
4046 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
4047 && S_ISGITLINK(new_mode
))
4050 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
4052 const unsigned char *tmp_c
;
4053 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
4054 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
4055 tmp
= old_dirty_submodule
; old_dirty_submodule
= new_dirty_submodule
;
4056 new_dirty_submodule
= tmp
;
4059 if (options
->prefix
&&
4060 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
4063 one
= alloc_filespec(concatpath
);
4064 two
= alloc_filespec(concatpath
);
4065 fill_filespec(one
, old_sha1
, old_mode
);
4066 fill_filespec(two
, new_sha1
, new_mode
);
4067 one
->dirty_submodule
= old_dirty_submodule
;
4068 two
->dirty_submodule
= new_dirty_submodule
;
4070 diff_queue(&diff_queued_diff
, one
, two
);
4071 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
4072 DIFF_OPT_SET(options
, HAS_CHANGES
);
4075 void diff_unmerge(struct diff_options
*options
,
4077 unsigned mode
, const unsigned char *sha1
)
4079 struct diff_filespec
*one
, *two
;
4081 if (options
->prefix
&&
4082 strncmp(path
, options
->prefix
, options
->prefix_length
))
4085 one
= alloc_filespec(path
);
4086 two
= alloc_filespec(path
);
4087 fill_filespec(one
, sha1
, mode
);
4088 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;
4091 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
4094 struct diff_tempfile
*temp
;
4095 const char *argv
[3];
4096 const char **arg
= argv
;
4097 struct child_process child
;
4098 struct strbuf buf
= STRBUF_INIT
;
4101 temp
= prepare_temp_file(spec
->path
, spec
);
4103 *arg
++ = temp
->name
;
4106 memset(&child
, 0, sizeof(child
));
4107 child
.use_shell
= 1;
4110 if (start_command(&child
)) {
4115 if (strbuf_read(&buf
, child
.out
, 0) < 0)
4116 err
= error("error reading from textconv command '%s'", pgm
);
4119 if (finish_command(&child
) || err
) {
4120 strbuf_release(&buf
);
4126 return strbuf_detach(&buf
, outsize
);
4129 static size_t fill_textconv(struct userdiff_driver
*driver
,
4130 struct diff_filespec
*df
,
4135 if (!driver
|| !driver
->textconv
) {
4136 if (!DIFF_FILE_VALID(df
)) {
4140 if (diff_populate_filespec(df
, 0))
4141 die("unable to read files to diff");
4146 if (driver
->textconv_cache
) {
4147 *outbuf
= notes_cache_get(driver
->textconv_cache
, df
->sha1
,
4153 *outbuf
= run_textconv(driver
->textconv
, df
, &size
);
4155 die("unable to read files to diff");
4157 if (driver
->textconv_cache
) {
4158 /* ignore errors, as we might be in a readonly repository */
4159 notes_cache_put(driver
->textconv_cache
, df
->sha1
, *outbuf
,
4162 * we could save up changes and flush them all at the end,
4163 * but we would need an extra call after all diffing is done.
4164 * Since generating a cache entry is the slow path anyway,
4165 * this extra overhead probably isn't a big deal.
4167 notes_cache_write(driver
->textconv_cache
);