2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "run-command.h"
16 #include "submodule.h"
18 #ifdef NO_FAST_WORKING_DIRECTORY
19 #define FAST_WORKING_DIRECTORY 0
21 #define FAST_WORKING_DIRECTORY 1
24 static int diff_detect_rename_default
;
25 static int diff_rename_limit_default
= 200;
26 static int diff_suppress_blank_empty
;
27 int diff_use_color_default
= -1;
28 static const char *diff_word_regex_cfg
;
29 static const char *external_diff_cmd_cfg
;
30 int diff_auto_refresh_index
= 1;
31 static int diff_mnemonic_prefix
;
33 static char diff_colors
[][COLOR_MAXLEN
] = {
35 GIT_COLOR_NORMAL
, /* PLAIN */
36 GIT_COLOR_BOLD
, /* METAINFO */
37 GIT_COLOR_CYAN
, /* FRAGINFO */
38 GIT_COLOR_RED
, /* OLD */
39 GIT_COLOR_GREEN
, /* NEW */
40 GIT_COLOR_YELLOW
, /* COMMIT */
41 GIT_COLOR_BG_RED
, /* WHITESPACE */
42 GIT_COLOR_NORMAL
, /* FUNCINFO */
45 static void diff_filespec_load_driver(struct diff_filespec
*one
);
46 static char *run_textconv(const char *, struct diff_filespec
*, size_t *);
48 static int parse_diff_color_slot(const char *var
, int ofs
)
50 if (!strcasecmp(var
+ofs
, "plain"))
52 if (!strcasecmp(var
+ofs
, "meta"))
54 if (!strcasecmp(var
+ofs
, "frag"))
56 if (!strcasecmp(var
+ofs
, "old"))
58 if (!strcasecmp(var
+ofs
, "new"))
60 if (!strcasecmp(var
+ofs
, "commit"))
62 if (!strcasecmp(var
+ofs
, "whitespace"))
63 return DIFF_WHITESPACE
;
64 if (!strcasecmp(var
+ofs
, "func"))
69 static int git_config_rename(const char *var
, const char *value
)
72 return DIFF_DETECT_RENAME
;
73 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
74 return DIFF_DETECT_COPY
;
75 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
79 * These are to give UI layer defaults.
80 * The core-level commands such as git-diff-files should
81 * never be affected by the setting of diff.renames
82 * the user happens to have in the configuration file.
84 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
86 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
87 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
90 if (!strcmp(var
, "diff.renames")) {
91 diff_detect_rename_default
= git_config_rename(var
, value
);
94 if (!strcmp(var
, "diff.autorefreshindex")) {
95 diff_auto_refresh_index
= git_config_bool(var
, value
);
98 if (!strcmp(var
, "diff.mnemonicprefix")) {
99 diff_mnemonic_prefix
= git_config_bool(var
, value
);
102 if (!strcmp(var
, "diff.external"))
103 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
104 if (!strcmp(var
, "diff.wordregex"))
105 return git_config_string(&diff_word_regex_cfg
, var
, value
);
107 return git_diff_basic_config(var
, value
, cb
);
110 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
112 if (!strcmp(var
, "diff.renamelimit")) {
113 diff_rename_limit_default
= git_config_int(var
, value
);
117 switch (userdiff_config(var
, value
)) {
123 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
124 int slot
= parse_diff_color_slot(var
, 11);
128 return config_error_nonbool(var
);
129 color_parse(value
, var
, diff_colors
[slot
]);
133 /* like GNU diff's --suppress-blank-empty option */
134 if (!strcmp(var
, "diff.suppressblankempty") ||
135 /* for backwards compatibility */
136 !strcmp(var
, "diff.suppress-blank-empty")) {
137 diff_suppress_blank_empty
= git_config_bool(var
, value
);
141 return git_color_default_config(var
, value
, cb
);
144 static char *quote_two(const char *one
, const char *two
)
146 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
147 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
148 struct strbuf res
= STRBUF_INIT
;
150 if (need_one
+ need_two
) {
151 strbuf_addch(&res
, '"');
152 quote_c_style(one
, &res
, NULL
, 1);
153 quote_c_style(two
, &res
, NULL
, 1);
154 strbuf_addch(&res
, '"');
156 strbuf_addstr(&res
, one
);
157 strbuf_addstr(&res
, two
);
159 return strbuf_detach(&res
, NULL
);
162 static const char *external_diff(void)
164 static const char *external_diff_cmd
= NULL
;
165 static int done_preparing
= 0;
168 return external_diff_cmd
;
169 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
170 if (!external_diff_cmd
)
171 external_diff_cmd
= external_diff_cmd_cfg
;
173 return external_diff_cmd
;
176 static struct diff_tempfile
{
177 const char *name
; /* filename external diff should read from */
180 char tmp_path
[PATH_MAX
];
183 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
185 struct emit_callback
{
188 int blank_at_eof_in_preimage
;
189 int blank_at_eof_in_postimage
;
191 int lno_in_postimage
;
192 sane_truncate_fn truncate
;
193 const char **label_path
;
194 struct diff_words_data
*diff_words
;
197 struct strbuf
*header
;
200 static int count_lines(const char *data
, int size
)
202 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
209 completely_empty
= 0;
213 completely_empty
= 0;
216 if (completely_empty
)
219 count
++; /* no trailing newline */
223 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
225 if (!DIFF_FILE_VALID(one
)) {
226 mf
->ptr
= (char *)""; /* does not matter */
230 else if (diff_populate_filespec(one
, 0))
234 mf
->size
= one
->size
;
238 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
241 long size
= mf
->size
;
246 ptr
+= size
- 1; /* pointing at the very end */
248 ; /* incomplete line */
250 ptr
--; /* skip the last LF */
251 while (mf
->ptr
< ptr
) {
253 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
254 if (*prev_eol
== '\n')
256 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
264 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
265 struct emit_callback
*ecbdata
)
268 unsigned ws_rule
= ecbdata
->ws_rule
;
269 l1
= count_trailing_blank(mf1
, ws_rule
);
270 l2
= count_trailing_blank(mf2
, ws_rule
);
272 ecbdata
->blank_at_eof_in_preimage
= 0;
273 ecbdata
->blank_at_eof_in_postimage
= 0;
276 at
= count_lines(mf1
->ptr
, mf1
->size
);
277 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
279 at
= count_lines(mf2
->ptr
, mf2
->size
);
280 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
283 static void emit_line_0(FILE *file
, const char *set
, const char *reset
,
284 int first
, const char *line
, int len
)
286 int has_trailing_newline
, has_trailing_carriage_return
;
290 has_trailing_newline
= (first
== '\n');
291 has_trailing_carriage_return
= (!has_trailing_newline
&&
293 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
295 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
296 if (has_trailing_newline
)
298 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
299 if (has_trailing_carriage_return
)
304 if (len
|| !nofirst
) {
308 fwrite(line
, len
, 1, file
);
311 if (has_trailing_carriage_return
)
313 if (has_trailing_newline
)
317 static void emit_line(FILE *file
, const char *set
, const char *reset
,
318 const char *line
, int len
)
320 emit_line_0(file
, set
, reset
, line
[0], line
+1, len
-1);
323 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
325 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
326 ecbdata
->blank_at_eof_in_preimage
&&
327 ecbdata
->blank_at_eof_in_postimage
&&
328 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
329 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
331 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
334 static void emit_add_line(const char *reset
,
335 struct emit_callback
*ecbdata
,
336 const char *line
, int len
)
338 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
339 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
342 emit_line_0(ecbdata
->file
, set
, reset
, '+', line
, len
);
343 else if (new_blank_line_at_eof(ecbdata
, line
, len
))
344 /* Blank line at EOF - paint '+' as well */
345 emit_line_0(ecbdata
->file
, ws
, reset
, '+', line
, len
);
347 /* Emit just the prefix, then the rest. */
348 emit_line_0(ecbdata
->file
, set
, reset
, '+', "", 0);
349 ws_check_emit(line
, len
, ecbdata
->ws_rule
,
350 ecbdata
->file
, set
, reset
, ws
);
354 static void emit_hunk_header(struct emit_callback
*ecbdata
,
355 const char *line
, int len
)
357 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
358 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
359 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
360 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
361 static const char atat
[2] = { '@', '@' };
365 * As a hunk header must begin with "@@ -<old>, +<new> @@",
366 * it always is at least 10 bytes long.
369 memcmp(line
, atat
, 2) ||
370 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
371 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
374 ep
+= 2; /* skip over @@ */
376 /* The hunk header in fraginfo color */
377 emit_line(ecbdata
->file
, frag
, reset
, line
, ep
- line
);
379 /* blank before the func header */
380 for (cp
= ep
; ep
- line
< len
; ep
++)
381 if (*ep
!= ' ' && *ep
!= '\t')
384 emit_line(ecbdata
->file
, plain
, reset
, cp
, ep
- cp
);
387 emit_line(ecbdata
->file
, func
, reset
, ep
, line
+ len
- ep
);
390 static struct diff_tempfile
*claim_diff_tempfile(void) {
392 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
393 if (!diff_temp
[i
].name
)
394 return diff_temp
+ i
;
395 die("BUG: diff is failing to clean up its tempfiles");
398 static int remove_tempfile_installed
;
400 static void remove_tempfile(void)
403 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
404 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
)
405 unlink_or_warn(diff_temp
[i
].name
);
406 diff_temp
[i
].name
= NULL
;
410 static void remove_tempfile_on_signal(int signo
)
417 static void print_line_count(FILE *file
, int count
)
421 fprintf(file
, "0,0");
427 fprintf(file
, "1,%d", count
);
432 static void emit_rewrite_lines(struct emit_callback
*ecb
,
433 int prefix
, const char *data
, int size
)
435 const char *endp
= NULL
;
436 static const char *nneof
= " No newline at end of file\n";
437 const char *old
= diff_get_color(ecb
->color_diff
, DIFF_FILE_OLD
);
438 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
443 endp
= memchr(data
, '\n', size
);
444 len
= endp
? (endp
- data
+ 1) : size
;
446 ecb
->lno_in_preimage
++;
447 emit_line_0(ecb
->file
, old
, reset
, '-',
450 ecb
->lno_in_postimage
++;
451 emit_add_line(reset
, ecb
, data
, len
);
457 const char *plain
= diff_get_color(ecb
->color_diff
,
459 emit_line_0(ecb
->file
, plain
, reset
, '\\',
460 nneof
, strlen(nneof
));
464 static void emit_rewrite_diff(const char *name_a
,
466 struct diff_filespec
*one
,
467 struct diff_filespec
*two
,
468 const char *textconv_one
,
469 const char *textconv_two
,
470 struct diff_options
*o
)
473 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
474 const char *name_a_tab
, *name_b_tab
;
475 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
476 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
477 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
478 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
479 const char *a_prefix
, *b_prefix
;
480 const char *data_one
, *data_two
;
481 size_t size_one
, size_two
;
482 struct emit_callback ecbdata
;
484 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
485 a_prefix
= o
->b_prefix
;
486 b_prefix
= o
->a_prefix
;
488 a_prefix
= o
->a_prefix
;
489 b_prefix
= o
->b_prefix
;
492 name_a
+= (*name_a
== '/');
493 name_b
+= (*name_b
== '/');
494 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
495 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
497 strbuf_reset(&a_name
);
498 strbuf_reset(&b_name
);
499 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
500 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
502 diff_populate_filespec(one
, 0);
503 diff_populate_filespec(two
, 0);
505 data_one
= run_textconv(textconv_one
, one
, &size_one
);
507 die("unable to read files to diff");
510 data_one
= one
->data
;
511 size_one
= one
->size
;
514 data_two
= run_textconv(textconv_two
, two
, &size_two
);
516 die("unable to read files to diff");
519 data_two
= two
->data
;
520 size_two
= two
->size
;
523 memset(&ecbdata
, 0, sizeof(ecbdata
));
524 ecbdata
.color_diff
= color_diff
;
525 ecbdata
.found_changesp
= &o
->found_changes
;
526 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
527 ecbdata
.file
= o
->file
;
528 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
530 mf1
.ptr
= (char *)data_one
;
531 mf2
.ptr
= (char *)data_two
;
534 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
536 ecbdata
.lno_in_preimage
= 1;
537 ecbdata
.lno_in_postimage
= 1;
539 lc_a
= count_lines(data_one
, size_one
);
540 lc_b
= count_lines(data_two
, size_two
);
542 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
543 metainfo
, a_name
.buf
, name_a_tab
, reset
,
544 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
545 print_line_count(o
->file
, lc_a
);
546 fprintf(o
->file
, " +");
547 print_line_count(o
->file
, lc_b
);
548 fprintf(o
->file
, " @@%s\n", reset
);
550 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
552 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
555 struct diff_words_buffer
{
558 struct diff_words_orig
{
559 const char *begin
, *end
;
561 int orig_nr
, orig_alloc
;
564 static void diff_words_append(char *line
, unsigned long len
,
565 struct diff_words_buffer
*buffer
)
567 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
570 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
571 buffer
->text
.size
+= len
;
572 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
575 struct diff_words_style_elem
579 const char *color
; /* NULL; filled in by the setup code if
580 * color is enabled */
583 struct diff_words_style
585 enum diff_words_type type
;
586 struct diff_words_style_elem
new, old
, ctx
;
590 struct diff_words_style diff_words_styles
[] = {
591 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
592 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
593 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
596 struct diff_words_data
{
597 struct diff_words_buffer minus
, plus
;
598 const char *current_plus
;
601 enum diff_words_type type
;
602 struct diff_words_style
*style
;
605 static int fn_out_diff_words_write_helper(FILE *fp
,
606 struct diff_words_style_elem
*st_el
,
608 size_t count
, const char *buf
)
611 char *p
= memchr(buf
, '\n', count
);
613 if (st_el
->color
&& fputs(st_el
->color
, fp
) < 0)
615 if (fputs(st_el
->prefix
, fp
) < 0 ||
616 fwrite(buf
, p
? p
- buf
: count
, 1, fp
) != 1 ||
617 fputs(st_el
->suffix
, fp
) < 0)
619 if (st_el
->color
&& *st_el
->color
620 && fputs(GIT_COLOR_RESET
, fp
) < 0)
625 if (fputs(newline
, fp
) < 0)
627 count
-= p
+ 1 - buf
;
633 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
635 struct diff_words_data
*diff_words
= priv
;
636 struct diff_words_style
*style
= diff_words
->style
;
637 int minus_first
, minus_len
, plus_first
, plus_len
;
638 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
640 if (line
[0] != '@' || parse_hunk_header(line
, len
,
641 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
644 /* POSIX requires that first be decremented by one if len == 0... */
646 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
648 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
650 minus_begin
= minus_end
=
651 diff_words
->minus
.orig
[minus_first
].end
;
654 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
655 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
657 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
659 if (diff_words
->current_plus
!= plus_begin
)
660 fn_out_diff_words_write_helper(diff_words
->file
,
661 &style
->ctx
, style
->newline
,
662 plus_begin
- diff_words
->current_plus
,
663 diff_words
->current_plus
);
664 if (minus_begin
!= minus_end
)
665 fn_out_diff_words_write_helper(diff_words
->file
,
666 &style
->old
, style
->newline
,
667 minus_end
- minus_begin
, minus_begin
);
668 if (plus_begin
!= plus_end
)
669 fn_out_diff_words_write_helper(diff_words
->file
,
670 &style
->new, style
->newline
,
671 plus_end
- plus_begin
, plus_begin
);
673 diff_words
->current_plus
= plus_end
;
676 /* This function starts looking at *begin, and returns 0 iff a word was found. */
677 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
678 int *begin
, int *end
)
680 if (word_regex
&& *begin
< buffer
->size
) {
682 if (!regexec(word_regex
, buffer
->ptr
+ *begin
, 1, match
, 0)) {
683 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
684 '\n', match
[0].rm_eo
- match
[0].rm_so
);
685 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
686 *begin
+= match
[0].rm_so
;
687 return *begin
>= *end
;
692 /* find the next word */
693 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
695 if (*begin
>= buffer
->size
)
698 /* find the end of the word */
700 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
707 * This function splits the words in buffer->text, stores the list with
708 * newline separator into out, and saves the offsets of the original words
711 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
720 /* fake an empty "0th" word */
721 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
722 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
725 for (i
= 0; i
< buffer
->text
.size
; i
++) {
726 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
729 /* store original boundaries */
730 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
732 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
733 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
737 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
738 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
739 out
->ptr
[out
->size
+ j
- i
] = '\n';
740 out
->size
+= j
- i
+ 1;
746 /* this executes the word diff on the accumulated buffers */
747 static void diff_words_show(struct diff_words_data
*diff_words
)
752 mmfile_t minus
, plus
;
753 struct diff_words_style
*style
= diff_words
->style
;
755 /* special case: only removal */
756 if (!diff_words
->plus
.text
.size
) {
757 fn_out_diff_words_write_helper(diff_words
->file
,
758 &style
->old
, style
->newline
,
759 diff_words
->minus
.text
.size
, diff_words
->minus
.text
.ptr
);
760 diff_words
->minus
.text
.size
= 0;
764 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
766 memset(&xpp
, 0, sizeof(xpp
));
767 memset(&xecfg
, 0, sizeof(xecfg
));
768 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
769 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
770 xpp
.flags
= XDF_NEED_MINIMAL
;
771 /* as only the hunk header will be parsed, we need a 0-context */
773 xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
777 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
778 diff_words
->plus
.text
.size
)
779 fn_out_diff_words_write_helper(diff_words
->file
,
780 &style
->ctx
, style
->newline
,
781 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
782 - diff_words
->current_plus
, diff_words
->current_plus
);
783 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
786 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
787 static void diff_words_flush(struct emit_callback
*ecbdata
)
789 if (ecbdata
->diff_words
->minus
.text
.size
||
790 ecbdata
->diff_words
->plus
.text
.size
)
791 diff_words_show(ecbdata
->diff_words
);
794 static void free_diff_words_data(struct emit_callback
*ecbdata
)
796 if (ecbdata
->diff_words
) {
797 diff_words_flush(ecbdata
);
798 free (ecbdata
->diff_words
->minus
.text
.ptr
);
799 free (ecbdata
->diff_words
->minus
.orig
);
800 free (ecbdata
->diff_words
->plus
.text
.ptr
);
801 free (ecbdata
->diff_words
->plus
.orig
);
802 free(ecbdata
->diff_words
->word_regex
);
803 free(ecbdata
->diff_words
);
804 ecbdata
->diff_words
= NULL
;
808 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
811 return diff_colors
[ix
];
815 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
822 return ecb
->truncate(line
, len
);
826 (void) utf8_width(&cp
, &l
);
828 break; /* truncated in the middle? */
833 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
836 ecbdata
->lno_in_preimage
= 0;
837 ecbdata
->lno_in_postimage
= 0;
838 p
= strchr(line
, '-');
840 return; /* cannot happen */
841 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
844 return; /* cannot happen */
845 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
848 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
850 struct emit_callback
*ecbdata
= priv
;
851 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
852 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
853 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
855 if (ecbdata
->header
) {
856 fprintf(ecbdata
->file
, "%s", ecbdata
->header
->buf
);
857 strbuf_reset(ecbdata
->header
);
858 ecbdata
->header
= NULL
;
860 *(ecbdata
->found_changesp
) = 1;
862 if (ecbdata
->label_path
[0]) {
863 const char *name_a_tab
, *name_b_tab
;
865 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
866 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
868 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
869 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
870 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
871 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
872 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
875 if (diff_suppress_blank_empty
876 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
881 if (line
[0] == '@') {
882 if (ecbdata
->diff_words
)
883 diff_words_flush(ecbdata
);
884 len
= sane_truncate_line(ecbdata
, line
, len
);
885 find_lno(line
, ecbdata
);
886 emit_hunk_header(ecbdata
, line
, len
);
887 if (line
[len
-1] != '\n')
888 putc('\n', ecbdata
->file
);
893 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
894 if (ecbdata
->diff_words
895 && ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
)
896 fputs("~\n", ecbdata
->file
);
900 if (ecbdata
->diff_words
) {
901 if (line
[0] == '-') {
902 diff_words_append(line
, len
,
903 &ecbdata
->diff_words
->minus
);
905 } else if (line
[0] == '+') {
906 diff_words_append(line
, len
,
907 &ecbdata
->diff_words
->plus
);
910 diff_words_flush(ecbdata
);
911 if (ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
) {
912 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
913 fputs("~\n", ecbdata
->file
);
915 /* don't print the prefix character */
916 emit_line(ecbdata
->file
, plain
, reset
, line
+1, len
-1);
921 if (line
[0] != '+') {
923 diff_get_color(ecbdata
->color_diff
,
924 line
[0] == '-' ? DIFF_FILE_OLD
: DIFF_PLAIN
);
925 ecbdata
->lno_in_preimage
++;
927 ecbdata
->lno_in_postimage
++;
928 emit_line(ecbdata
->file
, color
, reset
, line
, len
);
930 ecbdata
->lno_in_postimage
++;
931 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
935 static char *pprint_rename(const char *a
, const char *b
)
939 struct strbuf name
= STRBUF_INIT
;
940 int pfx_length
, sfx_length
;
941 int len_a
= strlen(a
);
942 int len_b
= strlen(b
);
943 int a_midlen
, b_midlen
;
944 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
945 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
947 if (qlen_a
|| qlen_b
) {
948 quote_c_style(a
, &name
, NULL
, 0);
949 strbuf_addstr(&name
, " => ");
950 quote_c_style(b
, &name
, NULL
, 0);
951 return strbuf_detach(&name
, NULL
);
954 /* Find common prefix */
956 while (*old
&& *new && *old
== *new) {
958 pfx_length
= old
- a
+ 1;
963 /* Find common suffix */
967 while (a
<= old
&& b
<= new && *old
== *new) {
969 sfx_length
= len_a
- (old
- a
);
975 * pfx{mid-a => mid-b}sfx
976 * {pfx-a => pfx-b}sfx
977 * pfx{sfx-a => sfx-b}
980 a_midlen
= len_a
- pfx_length
- sfx_length
;
981 b_midlen
= len_b
- pfx_length
- sfx_length
;
987 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
988 if (pfx_length
+ sfx_length
) {
989 strbuf_add(&name
, a
, pfx_length
);
990 strbuf_addch(&name
, '{');
992 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
993 strbuf_addstr(&name
, " => ");
994 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
995 if (pfx_length
+ sfx_length
) {
996 strbuf_addch(&name
, '}');
997 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
999 return strbuf_detach(&name
, NULL
);
1005 struct diffstat_file
{
1009 unsigned is_unmerged
:1;
1010 unsigned is_binary
:1;
1011 unsigned is_renamed
:1;
1012 unsigned int added
, deleted
;
1016 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
1020 struct diffstat_file
*x
;
1021 x
= xcalloc(sizeof (*x
), 1);
1022 if (diffstat
->nr
== diffstat
->alloc
) {
1023 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
1024 diffstat
->files
= xrealloc(diffstat
->files
,
1025 diffstat
->alloc
* sizeof(x
));
1027 diffstat
->files
[diffstat
->nr
++] = x
;
1029 x
->from_name
= xstrdup(name_a
);
1030 x
->name
= xstrdup(name_b
);
1034 x
->from_name
= NULL
;
1035 x
->name
= xstrdup(name_a
);
1040 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
1042 struct diffstat_t
*diffstat
= priv
;
1043 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
1047 else if (line
[0] == '-')
1051 const char mime_boundary_leader
[] = "------------";
1053 static int scale_linear(int it
, int width
, int max_change
)
1056 * make sure that at least one '-' is printed if there were deletions,
1057 * and likewise for '+'.
1061 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
1064 static void show_name(FILE *file
,
1065 const char *prefix
, const char *name
, int len
)
1067 fprintf(file
, " %s%-*s |", prefix
, len
, name
);
1070 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
1074 fprintf(file
, "%s", set
);
1077 fprintf(file
, "%s", reset
);
1080 static void fill_print_name(struct diffstat_file
*file
)
1084 if (file
->print_name
)
1087 if (!file
->is_renamed
) {
1088 struct strbuf buf
= STRBUF_INIT
;
1089 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
1090 pname
= strbuf_detach(&buf
, NULL
);
1093 strbuf_release(&buf
);
1096 pname
= pprint_rename(file
->from_name
, file
->name
);
1098 file
->print_name
= pname
;
1101 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
1103 int i
, len
, add
, del
, adds
= 0, dels
= 0;
1104 int max_change
= 0, max_len
= 0;
1105 int total_files
= data
->nr
;
1106 int width
, name_width
;
1107 const char *reset
, *set
, *add_c
, *del_c
;
1112 width
= options
->stat_width
? options
->stat_width
: 80;
1113 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
1115 /* Sanity: give at least 5 columns to the graph,
1116 * but leave at least 10 columns for the name.
1120 if (name_width
< 10)
1122 else if (width
< name_width
+ 15)
1123 name_width
= width
- 15;
1125 /* Find the longest filename and max number of changes */
1126 reset
= diff_get_color_opt(options
, DIFF_RESET
);
1127 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
1128 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
1129 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
1131 for (i
= 0; i
< data
->nr
; i
++) {
1132 struct diffstat_file
*file
= data
->files
[i
];
1133 int change
= file
->added
+ file
->deleted
;
1134 fill_print_name(file
);
1135 len
= strlen(file
->print_name
);
1139 if (file
->is_binary
|| file
->is_unmerged
)
1141 if (max_change
< change
)
1142 max_change
= change
;
1145 /* Compute the width of the graph part;
1146 * 10 is for one blank at the beginning of the line plus
1147 * " | count " between the name and the graph.
1149 * From here on, name_width is the width of the name area,
1150 * and width is the width of the graph area.
1152 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
1153 if (width
< (name_width
+ 10) + max_change
)
1154 width
= width
- (name_width
+ 10);
1158 for (i
= 0; i
< data
->nr
; i
++) {
1159 const char *prefix
= "";
1160 char *name
= data
->files
[i
]->print_name
;
1161 int added
= data
->files
[i
]->added
;
1162 int deleted
= data
->files
[i
]->deleted
;
1166 * "scale" the filename
1169 name_len
= strlen(name
);
1170 if (name_width
< name_len
) {
1174 name
+= name_len
- len
;
1175 slash
= strchr(name
, '/');
1180 if (data
->files
[i
]->is_binary
) {
1181 show_name(options
->file
, prefix
, name
, len
);
1182 fprintf(options
->file
, " Bin ");
1183 fprintf(options
->file
, "%s%d%s", del_c
, deleted
, reset
);
1184 fprintf(options
->file
, " -> ");
1185 fprintf(options
->file
, "%s%d%s", add_c
, added
, reset
);
1186 fprintf(options
->file
, " bytes");
1187 fprintf(options
->file
, "\n");
1190 else if (data
->files
[i
]->is_unmerged
) {
1191 show_name(options
->file
, prefix
, name
, len
);
1192 fprintf(options
->file
, " Unmerged\n");
1195 else if (!data
->files
[i
]->is_renamed
&&
1196 (added
+ deleted
== 0)) {
1202 * scale the add/delete
1209 if (width
<= max_change
) {
1210 add
= scale_linear(add
, width
, max_change
);
1211 del
= scale_linear(del
, width
, max_change
);
1213 show_name(options
->file
, prefix
, name
, len
);
1214 fprintf(options
->file
, "%5d%s", added
+ deleted
,
1215 added
+ deleted
? " " : "");
1216 show_graph(options
->file
, '+', add
, add_c
, reset
);
1217 show_graph(options
->file
, '-', del
, del_c
, reset
);
1218 fprintf(options
->file
, "\n");
1220 fprintf(options
->file
,
1221 " %d files changed, %d insertions(+), %d deletions(-)\n",
1222 total_files
, adds
, dels
);
1225 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
1227 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
1232 for (i
= 0; i
< data
->nr
; i
++) {
1233 if (!data
->files
[i
]->is_binary
&&
1234 !data
->files
[i
]->is_unmerged
) {
1235 int added
= data
->files
[i
]->added
;
1236 int deleted
= data
->files
[i
]->deleted
;
1237 if (!data
->files
[i
]->is_renamed
&&
1238 (added
+ deleted
== 0)) {
1246 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
1247 total_files
, adds
, dels
);
1250 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
1257 for (i
= 0; i
< data
->nr
; i
++) {
1258 struct diffstat_file
*file
= data
->files
[i
];
1260 if (file
->is_binary
)
1261 fprintf(options
->file
, "-\t-\t");
1263 fprintf(options
->file
,
1264 "%d\t%d\t", file
->added
, file
->deleted
);
1265 if (options
->line_termination
) {
1266 fill_print_name(file
);
1267 if (!file
->is_renamed
)
1268 write_name_quoted(file
->name
, options
->file
,
1269 options
->line_termination
);
1271 fputs(file
->print_name
, options
->file
);
1272 putc(options
->line_termination
, options
->file
);
1275 if (file
->is_renamed
) {
1276 putc('\0', options
->file
);
1277 write_name_quoted(file
->from_name
, options
->file
, '\0');
1279 write_name_quoted(file
->name
, options
->file
, '\0');
1284 struct dirstat_file
{
1286 unsigned long changed
;
1289 struct dirstat_dir
{
1290 struct dirstat_file
*files
;
1291 int alloc
, nr
, percent
, cumulative
;
1294 static long gather_dirstat(FILE *file
, struct dirstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1296 unsigned long this_dir
= 0;
1297 unsigned int sources
= 0;
1300 struct dirstat_file
*f
= dir
->files
;
1301 int namelen
= strlen(f
->name
);
1305 if (namelen
< baselen
)
1307 if (memcmp(f
->name
, base
, baselen
))
1309 slash
= strchr(f
->name
+ baselen
, '/');
1311 int newbaselen
= slash
+ 1 - f
->name
;
1312 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1324 * We don't report dirstat's for
1326 * - or cases where everything came from a single directory
1327 * under this directory (sources == 1).
1329 if (baselen
&& sources
!= 1) {
1330 int permille
= this_dir
* 1000 / changed
;
1332 int percent
= permille
/ 10;
1333 if (percent
>= dir
->percent
) {
1334 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1335 if (!dir
->cumulative
)
1343 static int dirstat_compare(const void *_a
, const void *_b
)
1345 const struct dirstat_file
*a
= _a
;
1346 const struct dirstat_file
*b
= _b
;
1347 return strcmp(a
->name
, b
->name
);
1350 static void show_dirstat(struct diff_options
*options
)
1353 unsigned long changed
;
1354 struct dirstat_dir dir
;
1355 struct diff_queue_struct
*q
= &diff_queued_diff
;
1360 dir
.percent
= options
->dirstat_percent
;
1361 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1364 for (i
= 0; i
< q
->nr
; i
++) {
1365 struct diff_filepair
*p
= q
->queue
[i
];
1367 unsigned long copied
, added
, damage
;
1369 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1371 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1372 diff_populate_filespec(p
->one
, 0);
1373 diff_populate_filespec(p
->two
, 0);
1374 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1376 diff_free_filespec_data(p
->one
);
1377 diff_free_filespec_data(p
->two
);
1378 } else if (DIFF_FILE_VALID(p
->one
)) {
1379 diff_populate_filespec(p
->one
, 1);
1381 diff_free_filespec_data(p
->one
);
1382 } else if (DIFF_FILE_VALID(p
->two
)) {
1383 diff_populate_filespec(p
->two
, 1);
1385 added
= p
->two
->size
;
1386 diff_free_filespec_data(p
->two
);
1391 * Original minus copied is the removed material,
1392 * added is the new material. They are both damages
1393 * made to the preimage. In --dirstat-by-file mode, count
1394 * damaged files, not damaged lines. This is done by
1395 * counting only a single damaged line per file.
1397 damage
= (p
->one
->size
- copied
) + added
;
1398 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
) && damage
> 0)
1401 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1402 dir
.files
[dir
.nr
].name
= name
;
1403 dir
.files
[dir
.nr
].changed
= damage
;
1408 /* This can happen even with many files, if everything was renames */
1412 /* Show all directories with more than x% of the changes */
1413 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
1414 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1417 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1420 for (i
= 0; i
< diffstat
->nr
; i
++) {
1421 struct diffstat_file
*f
= diffstat
->files
[i
];
1422 if (f
->name
!= f
->print_name
)
1423 free(f
->print_name
);
1428 free(diffstat
->files
);
1431 struct checkdiff_t
{
1432 const char *filename
;
1434 struct diff_options
*o
;
1439 static int is_conflict_marker(const char *line
, unsigned long len
)
1446 firstchar
= line
[0];
1447 switch (firstchar
) {
1448 case '=': case '>': case '<':
1453 for (cnt
= 1; cnt
< 7; cnt
++)
1454 if (line
[cnt
] != firstchar
)
1456 /* line[0] thru line[6] are same as firstchar */
1457 if (firstchar
== '=') {
1458 /* divider between ours and theirs? */
1459 if (len
!= 8 || line
[7] != '\n')
1461 } else if (len
< 8 || !isspace(line
[7])) {
1462 /* not divider before ours nor after theirs */
1468 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1470 struct checkdiff_t
*data
= priv
;
1471 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1472 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1473 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1474 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1477 if (line
[0] == '+') {
1480 if (is_conflict_marker(line
+ 1, len
- 1)) {
1482 fprintf(data
->o
->file
,
1483 "%s:%d: leftover conflict marker\n",
1484 data
->filename
, data
->lineno
);
1486 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1489 data
->status
|= bad
;
1490 err
= whitespace_error_string(bad
);
1491 fprintf(data
->o
->file
, "%s:%d: %s.\n",
1492 data
->filename
, data
->lineno
, err
);
1494 emit_line(data
->o
->file
, set
, reset
, line
, 1);
1495 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1496 data
->o
->file
, set
, reset
, ws
);
1497 } else if (line
[0] == ' ') {
1499 } else if (line
[0] == '@') {
1500 char *plus
= strchr(line
, '+');
1502 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1504 die("invalid diff");
1508 static unsigned char *deflate_it(char *data
,
1510 unsigned long *result_size
)
1513 unsigned char *deflated
;
1516 memset(&stream
, 0, sizeof(stream
));
1517 deflateInit(&stream
, zlib_compression_level
);
1518 bound
= deflateBound(&stream
, size
);
1519 deflated
= xmalloc(bound
);
1520 stream
.next_out
= deflated
;
1521 stream
.avail_out
= bound
;
1523 stream
.next_in
= (unsigned char *)data
;
1524 stream
.avail_in
= size
;
1525 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1527 deflateEnd(&stream
);
1528 *result_size
= stream
.total_out
;
1532 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1538 unsigned long orig_size
;
1539 unsigned long delta_size
;
1540 unsigned long deflate_size
;
1541 unsigned long data_size
;
1543 /* We could do deflated delta, or we could do just deflated two,
1544 * whichever is smaller.
1547 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1548 if (one
->size
&& two
->size
) {
1549 delta
= diff_delta(one
->ptr
, one
->size
,
1550 two
->ptr
, two
->size
,
1551 &delta_size
, deflate_size
);
1553 void *to_free
= delta
;
1554 orig_size
= delta_size
;
1555 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1560 if (delta
&& delta_size
< deflate_size
) {
1561 fprintf(file
, "delta %lu\n", orig_size
);
1564 data_size
= delta_size
;
1567 fprintf(file
, "literal %lu\n", two
->size
);
1570 data_size
= deflate_size
;
1573 /* emit data encoded in base85 */
1576 int bytes
= (52 < data_size
) ? 52 : data_size
;
1580 line
[0] = bytes
+ 'A' - 1;
1582 line
[0] = bytes
- 26 + 'a' - 1;
1583 encode_85(line
+ 1, cp
, bytes
);
1584 cp
= (char *) cp
+ bytes
;
1588 fprintf(file
, "\n");
1592 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1594 fprintf(file
, "GIT binary patch\n");
1595 emit_binary_diff_body(file
, one
, two
);
1596 emit_binary_diff_body(file
, two
, one
);
1599 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1602 one
->driver
= userdiff_find_by_path(one
->path
);
1604 one
->driver
= userdiff_find_by_name("default");
1607 int diff_filespec_is_binary(struct diff_filespec
*one
)
1609 if (one
->is_binary
== -1) {
1610 diff_filespec_load_driver(one
);
1611 if (one
->driver
->binary
!= -1)
1612 one
->is_binary
= one
->driver
->binary
;
1614 if (!one
->data
&& DIFF_FILE_VALID(one
))
1615 diff_populate_filespec(one
, 0);
1617 one
->is_binary
= buffer_is_binary(one
->data
,
1619 if (one
->is_binary
== -1)
1623 return one
->is_binary
;
1626 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
1628 diff_filespec_load_driver(one
);
1629 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
1632 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1634 diff_filespec_load_driver(one
);
1635 return one
->driver
->word_regex
;
1638 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
1640 if (!options
->a_prefix
)
1641 options
->a_prefix
= a
;
1642 if (!options
->b_prefix
)
1643 options
->b_prefix
= b
;
1646 static const char *get_textconv(struct diff_filespec
*one
)
1648 if (!DIFF_FILE_VALID(one
))
1650 if (!S_ISREG(one
->mode
))
1652 diff_filespec_load_driver(one
);
1653 return one
->driver
->textconv
;
1656 static void builtin_diff(const char *name_a
,
1658 struct diff_filespec
*one
,
1659 struct diff_filespec
*two
,
1660 const char *xfrm_msg
,
1661 struct diff_options
*o
,
1662 int complete_rewrite
)
1666 char *a_one
, *b_two
;
1667 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1668 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1669 const char *a_prefix
, *b_prefix
;
1670 const char *textconv_one
= NULL
, *textconv_two
= NULL
;
1671 struct strbuf header
= STRBUF_INIT
;
1673 if (DIFF_OPT_TST(o
, SUBMODULE_LOG
) &&
1674 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
1675 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
1676 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1677 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1678 show_submodule_summary(o
->file
, one
? one
->path
: two
->path
,
1679 one
->sha1
, two
->sha1
, two
->dirty_submodule
,
1684 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
1685 textconv_one
= get_textconv(one
);
1686 textconv_two
= get_textconv(two
);
1689 diff_set_mnemonic_prefix(o
, "a/", "b/");
1690 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
1691 a_prefix
= o
->b_prefix
;
1692 b_prefix
= o
->a_prefix
;
1694 a_prefix
= o
->a_prefix
;
1695 b_prefix
= o
->b_prefix
;
1698 /* Never use a non-valid filename anywhere if at all possible */
1699 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
1700 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
1702 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
1703 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
1704 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1705 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1706 strbuf_addf(&header
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1707 if (lbl
[0][0] == '/') {
1709 strbuf_addf(&header
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1710 if (xfrm_msg
&& xfrm_msg
[0])
1711 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1713 else if (lbl
[1][0] == '/') {
1714 strbuf_addf(&header
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1715 if (xfrm_msg
&& xfrm_msg
[0])
1716 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1719 if (one
->mode
!= two
->mode
) {
1720 strbuf_addf(&header
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1721 strbuf_addf(&header
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1723 if (xfrm_msg
&& xfrm_msg
[0])
1724 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1727 * we do not run diff between different kind
1730 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1731 goto free_ab_and_return
;
1732 if (complete_rewrite
&&
1733 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
1734 (textconv_two
|| !diff_filespec_is_binary(two
))) {
1735 fprintf(o
->file
, "%s", header
.buf
);
1736 strbuf_reset(&header
);
1737 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1738 textconv_one
, textconv_two
, o
);
1739 o
->found_changes
= 1;
1740 goto free_ab_and_return
;
1744 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1745 die("unable to read files to diff");
1747 if (!DIFF_OPT_TST(o
, TEXT
) &&
1748 ( (diff_filespec_is_binary(one
) && !textconv_one
) ||
1749 (diff_filespec_is_binary(two
) && !textconv_two
) )) {
1750 /* Quite common confusing case */
1751 if (mf1
.size
== mf2
.size
&&
1752 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1753 goto free_ab_and_return
;
1754 fprintf(o
->file
, "%s", header
.buf
);
1755 strbuf_reset(&header
);
1756 if (DIFF_OPT_TST(o
, BINARY
))
1757 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1759 fprintf(o
->file
, "Binary files %s and %s differ\n",
1761 o
->found_changes
= 1;
1764 /* Crazy xdl interfaces.. */
1765 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1769 struct emit_callback ecbdata
;
1770 const struct userdiff_funcname
*pe
;
1772 if (!DIFF_XDL_TST(o
, WHITESPACE_FLAGS
)) {
1773 fprintf(o
->file
, "%s", header
.buf
);
1774 strbuf_reset(&header
);
1779 mf1
.ptr
= run_textconv(textconv_one
, one
, &size
);
1781 die("unable to read files to diff");
1786 mf2
.ptr
= run_textconv(textconv_two
, two
, &size
);
1788 die("unable to read files to diff");
1792 pe
= diff_funcname_pattern(one
);
1794 pe
= diff_funcname_pattern(two
);
1796 memset(&xpp
, 0, sizeof(xpp
));
1797 memset(&xecfg
, 0, sizeof(xecfg
));
1798 memset(&ecbdata
, 0, sizeof(ecbdata
));
1799 ecbdata
.label_path
= lbl
;
1800 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1801 ecbdata
.found_changesp
= &o
->found_changes
;
1802 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1803 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
1804 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1805 ecbdata
.file
= o
->file
;
1806 ecbdata
.header
= header
.len
? &header
: NULL
;
1807 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1808 xecfg
.ctxlen
= o
->context
;
1809 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
1810 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1812 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
1815 else if (!prefixcmp(diffopts
, "--unified="))
1816 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1817 else if (!prefixcmp(diffopts
, "-u"))
1818 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1822 ecbdata
.diff_words
=
1823 xcalloc(1, sizeof(struct diff_words_data
));
1824 ecbdata
.diff_words
->file
= o
->file
;
1825 ecbdata
.diff_words
->type
= o
->word_diff
;
1827 o
->word_regex
= userdiff_word_regex(one
);
1829 o
->word_regex
= userdiff_word_regex(two
);
1831 o
->word_regex
= diff_word_regex_cfg
;
1832 if (o
->word_regex
) {
1833 ecbdata
.diff_words
->word_regex
= (regex_t
*)
1834 xmalloc(sizeof(regex_t
));
1835 if (regcomp(ecbdata
.diff_words
->word_regex
,
1837 REG_EXTENDED
| REG_NEWLINE
))
1838 die ("Invalid regular expression: %s",
1841 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
1842 if (o
->word_diff
== diff_words_styles
[i
].type
) {
1843 ecbdata
.diff_words
->style
=
1844 &diff_words_styles
[i
];
1848 if (DIFF_OPT_TST(o
, COLOR_DIFF
)) {
1849 struct diff_words_style
*st
= ecbdata
.diff_words
->style
;
1850 st
->old
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1851 st
->new.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1852 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_PLAIN
);
1855 xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
1856 &xpp
, &xecfg
, &ecb
);
1858 free_diff_words_data(&ecbdata
);
1863 xdiff_clear_find_func(&xecfg
);
1867 strbuf_release(&header
);
1868 diff_free_filespec_data(one
);
1869 diff_free_filespec_data(two
);
1875 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1876 struct diff_filespec
*one
,
1877 struct diff_filespec
*two
,
1878 struct diffstat_t
*diffstat
,
1879 struct diff_options
*o
,
1880 int complete_rewrite
)
1883 struct diffstat_file
*data
;
1885 data
= diffstat_add(diffstat
, name_a
, name_b
);
1888 data
->is_unmerged
= 1;
1891 if (complete_rewrite
) {
1892 diff_populate_filespec(one
, 0);
1893 diff_populate_filespec(two
, 0);
1894 data
->deleted
= count_lines(one
->data
, one
->size
);
1895 data
->added
= count_lines(two
->data
, two
->size
);
1896 goto free_and_return
;
1898 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1899 die("unable to read files to diff");
1901 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1902 data
->is_binary
= 1;
1903 data
->added
= mf2
.size
;
1904 data
->deleted
= mf1
.size
;
1906 /* Crazy xdl interfaces.. */
1911 memset(&xpp
, 0, sizeof(xpp
));
1912 memset(&xecfg
, 0, sizeof(xecfg
));
1913 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1914 xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
1915 &xpp
, &xecfg
, &ecb
);
1919 diff_free_filespec_data(one
);
1920 diff_free_filespec_data(two
);
1923 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1924 const char *attr_path
,
1925 struct diff_filespec
*one
,
1926 struct diff_filespec
*two
,
1927 struct diff_options
*o
)
1930 struct checkdiff_t data
;
1935 memset(&data
, 0, sizeof(data
));
1936 data
.filename
= name_b
? name_b
: name_a
;
1939 data
.ws_rule
= whitespace_rule(attr_path
);
1941 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1942 die("unable to read files to diff");
1945 * All the other codepaths check both sides, but not checking
1946 * the "old" side here is deliberate. We are checking the newly
1947 * introduced changes, and as long as the "new" side is text, we
1948 * can and should check what it introduces.
1950 if (diff_filespec_is_binary(two
))
1951 goto free_and_return
;
1953 /* Crazy xdl interfaces.. */
1958 memset(&xpp
, 0, sizeof(xpp
));
1959 memset(&xecfg
, 0, sizeof(xecfg
));
1960 xecfg
.ctxlen
= 1; /* at least one context line */
1961 xpp
.flags
= XDF_NEED_MINIMAL
;
1962 xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
1963 &xpp
, &xecfg
, &ecb
);
1965 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
1966 struct emit_callback ecbdata
;
1969 ecbdata
.ws_rule
= data
.ws_rule
;
1970 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1971 blank_at_eof
= ecbdata
.blank_at_eof_in_preimage
;
1976 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
1977 fprintf(o
->file
, "%s:%d: %s.\n",
1978 data
.filename
, blank_at_eof
, err
);
1979 data
.status
= 1; /* report errors */
1984 diff_free_filespec_data(one
);
1985 diff_free_filespec_data(two
);
1987 DIFF_OPT_SET(o
, CHECK_FAILED
);
1990 struct diff_filespec
*alloc_filespec(const char *path
)
1992 int namelen
= strlen(path
);
1993 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1995 memset(spec
, 0, sizeof(*spec
));
1996 spec
->path
= (char *)(spec
+ 1);
1997 memcpy(spec
->path
, path
, namelen
+1);
1999 spec
->is_binary
= -1;
2003 void free_filespec(struct diff_filespec
*spec
)
2005 if (!--spec
->count
) {
2006 diff_free_filespec_data(spec
);
2011 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
2012 unsigned short mode
)
2015 spec
->mode
= canon_mode(mode
);
2016 hashcpy(spec
->sha1
, sha1
);
2017 spec
->sha1_valid
= !is_null_sha1(sha1
);
2022 * Given a name and sha1 pair, if the index tells us the file in
2023 * the work tree has that object contents, return true, so that
2024 * prepare_temp_file() does not have to inflate and extract.
2026 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
2028 struct cache_entry
*ce
;
2033 * We do not read the cache ourselves here, because the
2034 * benchmark with my previous version that always reads cache
2035 * shows that it makes things worse for diff-tree comparing
2036 * two linux-2.6 kernel trees in an already checked out work
2037 * tree. This is because most diff-tree comparisons deal with
2038 * only a small number of files, while reading the cache is
2039 * expensive for a large project, and its cost outweighs the
2040 * savings we get by not inflating the object to a temporary
2041 * file. Practically, this code only helps when we are used
2042 * by diff-cache --cached, which does read the cache before
2048 /* We want to avoid the working directory if our caller
2049 * doesn't need the data in a normal file, this system
2050 * is rather slow with its stat/open/mmap/close syscalls,
2051 * and the object is contained in a pack file. The pack
2052 * is probably already open and will be faster to obtain
2053 * the data through than the working directory. Loose
2054 * objects however would tend to be slower as they need
2055 * to be individually opened and inflated.
2057 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
))
2061 pos
= cache_name_pos(name
, len
);
2064 ce
= active_cache
[pos
];
2067 * This is not the sha1 we are looking for, or
2068 * unreusable because it is not a regular file.
2070 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
2074 * If ce is marked as "assume unchanged", there is no
2075 * guarantee that work tree matches what we are looking for.
2077 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2081 * If ce matches the file in the work tree, we can reuse it.
2083 if (ce_uptodate(ce
) ||
2084 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
2090 static int populate_from_stdin(struct diff_filespec
*s
)
2092 struct strbuf buf
= STRBUF_INIT
;
2095 if (strbuf_read(&buf
, 0, 0) < 0)
2096 return error("error while reading from stdin %s",
2099 s
->should_munmap
= 0;
2100 s
->data
= strbuf_detach(&buf
, &size
);
2106 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
2109 char *data
= xmalloc(100), *dirty
= "";
2111 /* Are we looking at the work tree? */
2112 if (s
->dirty_submodule
)
2115 len
= snprintf(data
, 100,
2116 "Subproject commit %s%s\n", sha1_to_hex(s
->sha1
), dirty
);
2128 * While doing rename detection and pickaxe operation, we may need to
2129 * grab the data for the blob (or file) for our own in-core comparison.
2130 * diff_filespec has data and size fields for this purpose.
2132 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
2135 if (!DIFF_FILE_VALID(s
))
2136 die("internal error: asking to populate invalid file.");
2137 if (S_ISDIR(s
->mode
))
2143 if (size_only
&& 0 < s
->size
)
2146 if (S_ISGITLINK(s
->mode
))
2147 return diff_populate_gitlink(s
, size_only
);
2149 if (!s
->sha1_valid
||
2150 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
2151 struct strbuf buf
= STRBUF_INIT
;
2155 if (!strcmp(s
->path
, "-"))
2156 return populate_from_stdin(s
);
2158 if (lstat(s
->path
, &st
) < 0) {
2159 if (errno
== ENOENT
) {
2163 s
->data
= (char *)"";
2168 s
->size
= xsize_t(st
.st_size
);
2171 if (S_ISLNK(st
.st_mode
)) {
2172 struct strbuf sb
= STRBUF_INIT
;
2174 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
2177 s
->data
= strbuf_detach(&sb
, NULL
);
2183 fd
= open(s
->path
, O_RDONLY
);
2186 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2188 s
->should_munmap
= 1;
2191 * Convert from working tree format to canonical git format
2193 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
2195 munmap(s
->data
, s
->size
);
2196 s
->should_munmap
= 0;
2197 s
->data
= strbuf_detach(&buf
, &size
);
2203 enum object_type type
;
2205 type
= sha1_object_info(s
->sha1
, &s
->size
);
2207 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
2214 void diff_free_filespec_blob(struct diff_filespec
*s
)
2218 else if (s
->should_munmap
)
2219 munmap(s
->data
, s
->size
);
2221 if (s
->should_free
|| s
->should_munmap
) {
2222 s
->should_free
= s
->should_munmap
= 0;
2227 void diff_free_filespec_data(struct diff_filespec
*s
)
2229 diff_free_filespec_blob(s
);
2234 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
2237 const unsigned char *sha1
,
2241 struct strbuf buf
= STRBUF_INIT
;
2242 struct strbuf
template = STRBUF_INIT
;
2243 char *path_dup
= xstrdup(path
);
2244 const char *base
= basename(path_dup
);
2246 /* Generate "XXXXXX_basename.ext" */
2247 strbuf_addstr(&template, "XXXXXX_");
2248 strbuf_addstr(&template, base
);
2250 fd
= git_mkstemps(temp
->tmp_path
, PATH_MAX
, template.buf
,
2253 die_errno("unable to create temp-file");
2254 if (convert_to_working_tree(path
,
2255 (const char *)blob
, (size_t)size
, &buf
)) {
2259 if (write_in_full(fd
, blob
, size
) != size
)
2260 die_errno("unable to write temp-file");
2262 temp
->name
= temp
->tmp_path
;
2263 strcpy(temp
->hex
, sha1_to_hex(sha1
));
2265 sprintf(temp
->mode
, "%06o", mode
);
2266 strbuf_release(&buf
);
2267 strbuf_release(&template);
2271 static struct diff_tempfile
*prepare_temp_file(const char *name
,
2272 struct diff_filespec
*one
)
2274 struct diff_tempfile
*temp
= claim_diff_tempfile();
2276 if (!DIFF_FILE_VALID(one
)) {
2278 /* A '-' entry produces this for file-2, and
2279 * a '+' entry produces this for file-1.
2281 temp
->name
= "/dev/null";
2282 strcpy(temp
->hex
, ".");
2283 strcpy(temp
->mode
, ".");
2287 if (!remove_tempfile_installed
) {
2288 atexit(remove_tempfile
);
2289 sigchain_push_common(remove_tempfile_on_signal
);
2290 remove_tempfile_installed
= 1;
2293 if (!one
->sha1_valid
||
2294 reuse_worktree_file(name
, one
->sha1
, 1)) {
2296 if (lstat(name
, &st
) < 0) {
2297 if (errno
== ENOENT
)
2298 goto not_a_valid_file
;
2299 die_errno("stat(%s)", name
);
2301 if (S_ISLNK(st
.st_mode
)) {
2302 struct strbuf sb
= STRBUF_INIT
;
2303 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
2304 die_errno("readlink(%s)", name
);
2305 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
2307 one
->sha1
: null_sha1
),
2309 one
->mode
: S_IFLNK
));
2310 strbuf_release(&sb
);
2313 /* we can borrow from the file in the work tree */
2315 if (!one
->sha1_valid
)
2316 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
2318 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
2319 /* Even though we may sometimes borrow the
2320 * contents from the work tree, we always want
2321 * one->mode. mode is trustworthy even when
2322 * !(one->sha1_valid), as long as
2323 * DIFF_FILE_VALID(one).
2325 sprintf(temp
->mode
, "%06o", one
->mode
);
2330 if (diff_populate_filespec(one
, 0))
2331 die("cannot read data blob for %s", one
->path
);
2332 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
2333 one
->sha1
, one
->mode
);
2338 /* An external diff command takes:
2340 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2341 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2344 static void run_external_diff(const char *pgm
,
2347 struct diff_filespec
*one
,
2348 struct diff_filespec
*two
,
2349 const char *xfrm_msg
,
2350 int complete_rewrite
)
2352 const char *spawn_arg
[10];
2354 const char **arg
= &spawn_arg
[0];
2357 struct diff_tempfile
*temp_one
, *temp_two
;
2358 const char *othername
= (other
? other
: name
);
2359 temp_one
= prepare_temp_file(name
, one
);
2360 temp_two
= prepare_temp_file(othername
, two
);
2363 *arg
++ = temp_one
->name
;
2364 *arg
++ = temp_one
->hex
;
2365 *arg
++ = temp_one
->mode
;
2366 *arg
++ = temp_two
->name
;
2367 *arg
++ = temp_two
->hex
;
2368 *arg
++ = temp_two
->mode
;
2379 retval
= run_command_v_opt(spawn_arg
, RUN_USING_SHELL
);
2382 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2387 static int similarity_index(struct diff_filepair
*p
)
2389 return p
->score
* 100 / MAX_SCORE
;
2392 static void fill_metainfo(struct strbuf
*msg
,
2395 struct diff_filespec
*one
,
2396 struct diff_filespec
*two
,
2397 struct diff_options
*o
,
2398 struct diff_filepair
*p
)
2400 strbuf_init(msg
, PATH_MAX
* 2 + 300);
2401 switch (p
->status
) {
2402 case DIFF_STATUS_COPIED
:
2403 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2404 strbuf_addstr(msg
, "\ncopy from ");
2405 quote_c_style(name
, msg
, NULL
, 0);
2406 strbuf_addstr(msg
, "\ncopy to ");
2407 quote_c_style(other
, msg
, NULL
, 0);
2408 strbuf_addch(msg
, '\n');
2410 case DIFF_STATUS_RENAMED
:
2411 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2412 strbuf_addstr(msg
, "\nrename from ");
2413 quote_c_style(name
, msg
, NULL
, 0);
2414 strbuf_addstr(msg
, "\nrename to ");
2415 quote_c_style(other
, msg
, NULL
, 0);
2416 strbuf_addch(msg
, '\n');
2418 case DIFF_STATUS_MODIFIED
:
2420 strbuf_addf(msg
, "dissimilarity index %d%%\n",
2421 similarity_index(p
));
2429 if (one
&& two
&& hashcmp(one
->sha1
, two
->sha1
)) {
2430 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2432 if (DIFF_OPT_TST(o
, BINARY
)) {
2434 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2435 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2438 strbuf_addf(msg
, "index %.*s..%.*s",
2439 abbrev
, sha1_to_hex(one
->sha1
),
2440 abbrev
, sha1_to_hex(two
->sha1
));
2441 if (one
->mode
== two
->mode
)
2442 strbuf_addf(msg
, " %06o", one
->mode
);
2443 strbuf_addch(msg
, '\n');
2446 strbuf_setlen(msg
, msg
->len
- 1);
2449 static void run_diff_cmd(const char *pgm
,
2452 const char *attr_path
,
2453 struct diff_filespec
*one
,
2454 struct diff_filespec
*two
,
2456 struct diff_options
*o
,
2457 struct diff_filepair
*p
)
2459 const char *xfrm_msg
= NULL
;
2460 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
2463 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
);
2464 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
2467 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2470 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
2471 if (drv
&& drv
->external
)
2472 pgm
= drv
->external
;
2476 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2481 builtin_diff(name
, other
? other
: name
,
2482 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2484 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2487 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2489 if (DIFF_FILE_VALID(one
)) {
2490 if (!one
->sha1_valid
) {
2492 if (!strcmp(one
->path
, "-")) {
2493 hashcpy(one
->sha1
, null_sha1
);
2496 if (lstat(one
->path
, &st
) < 0)
2497 die_errno("stat '%s'", one
->path
);
2498 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2499 die("cannot hash %s", one
->path
);
2506 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2508 /* Strip the prefix but do not molest /dev/null and absolute paths */
2509 if (*namep
&& **namep
!= '/')
2510 *namep
+= prefix_length
;
2511 if (*otherp
&& **otherp
!= '/')
2512 *otherp
+= prefix_length
;
2515 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2517 const char *pgm
= external_diff();
2519 struct diff_filespec
*one
= p
->one
;
2520 struct diff_filespec
*two
= p
->two
;
2523 const char *attr_path
;
2525 name
= p
->one
->path
;
2526 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2528 if (o
->prefix_length
)
2529 strip_prefix(o
->prefix_length
, &name
, &other
);
2531 if (DIFF_PAIR_UNMERGED(p
)) {
2532 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2533 NULL
, NULL
, NULL
, o
, p
);
2537 diff_fill_sha1_info(one
);
2538 diff_fill_sha1_info(two
);
2541 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2542 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2544 * a filepair that changes between file and symlink
2545 * needs to be split into deletion and creation.
2547 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2548 run_diff_cmd(NULL
, name
, other
, attr_path
,
2549 one
, null
, &msg
, o
, p
);
2551 strbuf_release(&msg
);
2553 null
= alloc_filespec(one
->path
);
2554 run_diff_cmd(NULL
, name
, other
, attr_path
,
2555 null
, two
, &msg
, o
, p
);
2559 run_diff_cmd(pgm
, name
, other
, attr_path
,
2560 one
, two
, &msg
, o
, p
);
2562 strbuf_release(&msg
);
2565 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2566 struct diffstat_t
*diffstat
)
2570 int complete_rewrite
= 0;
2572 if (DIFF_PAIR_UNMERGED(p
)) {
2574 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2578 name
= p
->one
->path
;
2579 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2581 if (o
->prefix_length
)
2582 strip_prefix(o
->prefix_length
, &name
, &other
);
2584 diff_fill_sha1_info(p
->one
);
2585 diff_fill_sha1_info(p
->two
);
2587 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2588 complete_rewrite
= 1;
2589 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2592 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2596 const char *attr_path
;
2598 if (DIFF_PAIR_UNMERGED(p
)) {
2603 name
= p
->one
->path
;
2604 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2605 attr_path
= other
? other
: name
;
2607 if (o
->prefix_length
)
2608 strip_prefix(o
->prefix_length
, &name
, &other
);
2610 diff_fill_sha1_info(p
->one
);
2611 diff_fill_sha1_info(p
->two
);
2613 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2616 void diff_setup(struct diff_options
*options
)
2618 memset(options
, 0, sizeof(*options
));
2620 options
->file
= stdout
;
2622 options
->line_termination
= '\n';
2623 options
->break_opt
= -1;
2624 options
->rename_limit
= -1;
2625 options
->dirstat_percent
= 3;
2626 options
->context
= 3;
2628 options
->change
= diff_change
;
2629 options
->add_remove
= diff_addremove
;
2630 if (diff_use_color_default
> 0)
2631 DIFF_OPT_SET(options
, COLOR_DIFF
);
2632 options
->detect_rename
= diff_detect_rename_default
;
2634 if (!diff_mnemonic_prefix
) {
2635 options
->a_prefix
= "a/";
2636 options
->b_prefix
= "b/";
2640 int diff_setup_done(struct diff_options
*options
)
2644 if (options
->output_format
& DIFF_FORMAT_NAME
)
2646 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2648 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2650 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2653 die("--name-only, --name-status, --check and -s are mutually exclusive");
2656 * Most of the time we can say "there are changes"
2657 * only by checking if there are changed paths, but
2658 * --ignore-whitespace* options force us to look
2662 if (DIFF_XDL_TST(options
, IGNORE_WHITESPACE
) ||
2663 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_CHANGE
) ||
2664 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_AT_EOL
))
2665 DIFF_OPT_SET(options
, DIFF_FROM_CONTENTS
);
2667 DIFF_OPT_CLR(options
, DIFF_FROM_CONTENTS
);
2669 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2670 options
->detect_rename
= DIFF_DETECT_COPY
;
2672 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2673 options
->prefix
= NULL
;
2674 if (options
->prefix
)
2675 options
->prefix_length
= strlen(options
->prefix
);
2677 options
->prefix_length
= 0;
2679 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2680 DIFF_FORMAT_NAME_STATUS
|
2681 DIFF_FORMAT_CHECKDIFF
|
2682 DIFF_FORMAT_NO_OUTPUT
))
2683 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2684 DIFF_FORMAT_NUMSTAT
|
2685 DIFF_FORMAT_DIFFSTAT
|
2686 DIFF_FORMAT_SHORTSTAT
|
2687 DIFF_FORMAT_DIRSTAT
|
2688 DIFF_FORMAT_SUMMARY
|
2692 * These cases always need recursive; we do not drop caller-supplied
2693 * recursive bits for other formats here.
2695 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2696 DIFF_FORMAT_NUMSTAT
|
2697 DIFF_FORMAT_DIFFSTAT
|
2698 DIFF_FORMAT_SHORTSTAT
|
2699 DIFF_FORMAT_DIRSTAT
|
2700 DIFF_FORMAT_SUMMARY
|
2701 DIFF_FORMAT_CHECKDIFF
))
2702 DIFF_OPT_SET(options
, RECURSIVE
);
2704 * Also pickaxe would not work very well if you do not say recursive
2706 if (options
->pickaxe
)
2707 DIFF_OPT_SET(options
, RECURSIVE
);
2709 * When patches are generated, submodules diffed against the work tree
2710 * must be checked for dirtiness too so it can be shown in the output
2712 if (options
->output_format
& DIFF_FORMAT_PATCH
)
2713 DIFF_OPT_SET(options
, DIRTY_SUBMODULES
);
2715 if (options
->detect_rename
&& options
->rename_limit
< 0)
2716 options
->rename_limit
= diff_rename_limit_default
;
2717 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2719 /* read-cache does not die even when it fails
2720 * so it is safe for us to do this here. Also
2721 * it does not smudge active_cache or active_nr
2722 * when it fails, so we do not have to worry about
2723 * cleaning it up ourselves either.
2727 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2728 options
->abbrev
= 40; /* full */
2731 * It does not make sense to show the first hit we happened
2732 * to have found. It does not make sense not to return with
2733 * exit code in such a case either.
2735 if (DIFF_OPT_TST(options
, QUICK
)) {
2736 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2737 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2743 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2753 if (c
== arg_short
) {
2757 if (val
&& isdigit(c
)) {
2759 int n
= strtoul(arg
, &end
, 10);
2770 eq
= strchr(arg
, '=');
2775 if (!len
|| strncmp(arg
, arg_long
, len
))
2780 if (!isdigit(*++eq
))
2782 n
= strtoul(eq
, &end
, 10);
2790 static int diff_scoreopt_parse(const char *opt
);
2792 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2794 const char *arg
= av
[0];
2796 /* Output format options */
2797 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2798 options
->output_format
|= DIFF_FORMAT_PATCH
;
2799 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2800 options
->output_format
|= DIFF_FORMAT_PATCH
;
2801 else if (!strcmp(arg
, "--raw"))
2802 options
->output_format
|= DIFF_FORMAT_RAW
;
2803 else if (!strcmp(arg
, "--patch-with-raw"))
2804 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2805 else if (!strcmp(arg
, "--numstat"))
2806 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2807 else if (!strcmp(arg
, "--shortstat"))
2808 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2809 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2810 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2811 else if (!strcmp(arg
, "--cumulative")) {
2812 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2813 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
2814 } else if (opt_arg(arg
, 0, "dirstat-by-file",
2815 &options
->dirstat_percent
)) {
2816 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2817 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
2819 else if (!strcmp(arg
, "--check"))
2820 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2821 else if (!strcmp(arg
, "--summary"))
2822 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2823 else if (!strcmp(arg
, "--patch-with-stat"))
2824 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2825 else if (!strcmp(arg
, "--name-only"))
2826 options
->output_format
|= DIFF_FORMAT_NAME
;
2827 else if (!strcmp(arg
, "--name-status"))
2828 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2829 else if (!strcmp(arg
, "-s"))
2830 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2831 else if (!prefixcmp(arg
, "--stat")) {
2833 int width
= options
->stat_width
;
2834 int name_width
= options
->stat_name_width
;
2840 if (!prefixcmp(arg
, "-width="))
2841 width
= strtoul(arg
+ 7, &end
, 10);
2842 else if (!prefixcmp(arg
, "-name-width="))
2843 name_width
= strtoul(arg
+ 12, &end
, 10);
2846 width
= strtoul(arg
+1, &end
, 10);
2848 name_width
= strtoul(end
+1, &end
, 10);
2851 /* Important! This checks all the error cases! */
2854 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2855 options
->stat_name_width
= name_width
;
2856 options
->stat_width
= width
;
2859 /* renames options */
2860 else if (!prefixcmp(arg
, "-B")) {
2861 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2864 else if (!prefixcmp(arg
, "-M")) {
2865 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2867 options
->detect_rename
= DIFF_DETECT_RENAME
;
2869 else if (!prefixcmp(arg
, "-C")) {
2870 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2871 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2872 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2874 options
->detect_rename
= DIFF_DETECT_COPY
;
2876 else if (!strcmp(arg
, "--no-renames"))
2877 options
->detect_rename
= 0;
2878 else if (!strcmp(arg
, "--relative"))
2879 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2880 else if (!prefixcmp(arg
, "--relative=")) {
2881 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2882 options
->prefix
= arg
+ 11;
2886 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2887 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
2888 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2889 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
2890 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2891 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
2892 else if (!strcmp(arg
, "--patience"))
2893 DIFF_XDL_SET(options
, PATIENCE_DIFF
);
2896 else if (!strcmp(arg
, "--binary")) {
2897 options
->output_format
|= DIFF_FORMAT_PATCH
;
2898 DIFF_OPT_SET(options
, BINARY
);
2900 else if (!strcmp(arg
, "--full-index"))
2901 DIFF_OPT_SET(options
, FULL_INDEX
);
2902 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2903 DIFF_OPT_SET(options
, TEXT
);
2904 else if (!strcmp(arg
, "-R"))
2905 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2906 else if (!strcmp(arg
, "--find-copies-harder"))
2907 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2908 else if (!strcmp(arg
, "--follow"))
2909 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2910 else if (!strcmp(arg
, "--color"))
2911 DIFF_OPT_SET(options
, COLOR_DIFF
);
2912 else if (!prefixcmp(arg
, "--color=")) {
2913 int value
= git_config_colorbool(NULL
, arg
+8, -1);
2915 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2917 DIFF_OPT_SET(options
, COLOR_DIFF
);
2919 return error("option `color' expects \"always\", \"auto\", or \"never\"");
2921 else if (!strcmp(arg
, "--no-color"))
2922 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2923 else if (!strcmp(arg
, "--color-words")) {
2924 DIFF_OPT_SET(options
, COLOR_DIFF
);
2925 options
->word_diff
= DIFF_WORDS_COLOR
;
2927 else if (!prefixcmp(arg
, "--color-words=")) {
2928 DIFF_OPT_SET(options
, COLOR_DIFF
);
2929 options
->word_diff
= DIFF_WORDS_COLOR
;
2930 options
->word_regex
= arg
+ 14;
2932 else if (!strcmp(arg
, "--word-diff")) {
2933 if (options
->word_diff
== DIFF_WORDS_NONE
)
2934 options
->word_diff
= DIFF_WORDS_PLAIN
;
2936 else if (!prefixcmp(arg
, "--word-diff=")) {
2937 const char *type
= arg
+ 12;
2938 if (!strcmp(type
, "plain"))
2939 options
->word_diff
= DIFF_WORDS_PLAIN
;
2940 else if (!strcmp(type
, "color")) {
2941 DIFF_OPT_SET(options
, COLOR_DIFF
);
2942 options
->word_diff
= DIFF_WORDS_COLOR
;
2944 else if (!strcmp(type
, "porcelain"))
2945 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
2946 else if (!strcmp(type
, "none"))
2947 options
->word_diff
= DIFF_WORDS_NONE
;
2949 die("bad --word-diff argument: %s", type
);
2951 else if (!prefixcmp(arg
, "--word-diff-regex=")) {
2952 if (options
->word_diff
== DIFF_WORDS_NONE
)
2953 options
->word_diff
= DIFF_WORDS_PLAIN
;
2954 options
->word_regex
= arg
+ 18;
2956 else if (!strcmp(arg
, "--exit-code"))
2957 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2958 else if (!strcmp(arg
, "--quiet"))
2959 DIFF_OPT_SET(options
, QUICK
);
2960 else if (!strcmp(arg
, "--ext-diff"))
2961 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2962 else if (!strcmp(arg
, "--no-ext-diff"))
2963 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2964 else if (!strcmp(arg
, "--textconv"))
2965 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
2966 else if (!strcmp(arg
, "--no-textconv"))
2967 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
2968 else if (!strcmp(arg
, "--ignore-submodules"))
2969 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
2970 else if (!strcmp(arg
, "--submodule"))
2971 DIFF_OPT_SET(options
, SUBMODULE_LOG
);
2972 else if (!prefixcmp(arg
, "--submodule=")) {
2973 if (!strcmp(arg
+ 12, "log"))
2974 DIFF_OPT_SET(options
, SUBMODULE_LOG
);
2978 else if (!strcmp(arg
, "-z"))
2979 options
->line_termination
= 0;
2980 else if (!prefixcmp(arg
, "-l"))
2981 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2982 else if (!prefixcmp(arg
, "-S"))
2983 options
->pickaxe
= arg
+ 2;
2984 else if (!strcmp(arg
, "--pickaxe-all"))
2985 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2986 else if (!strcmp(arg
, "--pickaxe-regex"))
2987 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2988 else if (!prefixcmp(arg
, "-O"))
2989 options
->orderfile
= arg
+ 2;
2990 else if (!prefixcmp(arg
, "--diff-filter="))
2991 options
->filter
= arg
+ 14;
2992 else if (!strcmp(arg
, "--abbrev"))
2993 options
->abbrev
= DEFAULT_ABBREV
;
2994 else if (!prefixcmp(arg
, "--abbrev=")) {
2995 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2996 if (options
->abbrev
< MINIMUM_ABBREV
)
2997 options
->abbrev
= MINIMUM_ABBREV
;
2998 else if (40 < options
->abbrev
)
2999 options
->abbrev
= 40;
3001 else if (!prefixcmp(arg
, "--src-prefix="))
3002 options
->a_prefix
= arg
+ 13;
3003 else if (!prefixcmp(arg
, "--dst-prefix="))
3004 options
->b_prefix
= arg
+ 13;
3005 else if (!strcmp(arg
, "--no-prefix"))
3006 options
->a_prefix
= options
->b_prefix
= "";
3007 else if (opt_arg(arg
, '\0', "inter-hunk-context",
3008 &options
->interhunkcontext
))
3010 else if (!prefixcmp(arg
, "--output=")) {
3011 options
->file
= fopen(arg
+ strlen("--output="), "w");
3013 die_errno("Could not open '%s'", arg
+ strlen("--output="));
3014 options
->close_file
= 1;
3020 static int parse_num(const char **cp_p
)
3022 unsigned long num
, scale
;
3024 const char *cp
= *cp_p
;
3031 if ( !dot
&& ch
== '.' ) {
3034 } else if ( ch
== '%' ) {
3035 scale
= dot
? scale
*100 : 100;
3036 cp
++; /* % is always at the end */
3038 } else if ( ch
>= '0' && ch
<= '9' ) {
3039 if ( scale
< 100000 ) {
3041 num
= (num
*10) + (ch
-'0');
3050 /* user says num divided by scale and we say internally that
3051 * is MAX_SCORE * num / scale.
3053 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
3056 static int diff_scoreopt_parse(const char *opt
)
3058 int opt1
, opt2
, cmd
;
3063 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
3064 return -1; /* that is not a -M, -C nor -B option */
3066 opt1
= parse_num(&opt
);
3072 else if (*opt
!= '/')
3073 return -1; /* we expect -B80/99 or -B80 */
3076 opt2
= parse_num(&opt
);
3081 return opt1
| (opt2
<< 16);
3084 struct diff_queue_struct diff_queued_diff
;
3086 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
3088 if (queue
->alloc
<= queue
->nr
) {
3089 queue
->alloc
= alloc_nr(queue
->alloc
);
3090 queue
->queue
= xrealloc(queue
->queue
,
3091 sizeof(dp
) * queue
->alloc
);
3093 queue
->queue
[queue
->nr
++] = dp
;
3096 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
3097 struct diff_filespec
*one
,
3098 struct diff_filespec
*two
)
3100 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
3108 void diff_free_filepair(struct diff_filepair
*p
)
3110 free_filespec(p
->one
);
3111 free_filespec(p
->two
);
3115 /* This is different from find_unique_abbrev() in that
3116 * it stuffs the result with dots for alignment.
3118 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
3123 return sha1_to_hex(sha1
);
3125 abbrev
= find_unique_abbrev(sha1
, len
);
3126 abblen
= strlen(abbrev
);
3128 static char hex
[41];
3129 if (len
< abblen
&& abblen
<= len
+ 2)
3130 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
3132 sprintf(hex
, "%s...", abbrev
);
3135 return sha1_to_hex(sha1
);
3138 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
3140 int line_termination
= opt
->line_termination
;
3141 int inter_name_termination
= line_termination
? '\t' : '\0';
3143 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
3144 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
3145 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
3146 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
3149 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
3150 inter_name_termination
);
3152 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
3155 if (p
->status
== DIFF_STATUS_COPIED
||
3156 p
->status
== DIFF_STATUS_RENAMED
) {
3157 const char *name_a
, *name_b
;
3158 name_a
= p
->one
->path
;
3159 name_b
= p
->two
->path
;
3160 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3161 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
3162 write_name_quoted(name_b
, opt
->file
, line_termination
);
3164 const char *name_a
, *name_b
;
3165 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
3167 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3168 write_name_quoted(name_a
, opt
->file
, line_termination
);
3172 int diff_unmodified_pair(struct diff_filepair
*p
)
3174 /* This function is written stricter than necessary to support
3175 * the currently implemented transformers, but the idea is to
3176 * let transformers to produce diff_filepairs any way they want,
3177 * and filter and clean them up here before producing the output.
3179 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
3181 if (DIFF_PAIR_UNMERGED(p
))
3182 return 0; /* unmerged is interesting */
3184 /* deletion, addition, mode or type change
3185 * and rename are all interesting.
3187 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
3188 DIFF_PAIR_MODE_CHANGED(p
) ||
3189 strcmp(one
->path
, two
->path
))
3192 /* both are valid and point at the same path. that is, we are
3193 * dealing with a change.
3195 if (one
->sha1_valid
&& two
->sha1_valid
&&
3196 !hashcmp(one
->sha1
, two
->sha1
) &&
3197 !one
->dirty_submodule
&& !two
->dirty_submodule
)
3198 return 1; /* no change */
3199 if (!one
->sha1_valid
&& !two
->sha1_valid
)
3200 return 1; /* both look at the same file on the filesystem. */
3204 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
3206 if (diff_unmodified_pair(p
))
3209 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3210 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3211 return; /* no tree diffs in patch format */
3216 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
3217 struct diffstat_t
*diffstat
)
3219 if (diff_unmodified_pair(p
))
3222 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3223 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3224 return; /* no tree diffs in patch format */
3226 run_diffstat(p
, o
, diffstat
);
3229 static void diff_flush_checkdiff(struct diff_filepair
*p
,
3230 struct diff_options
*o
)
3232 if (diff_unmodified_pair(p
))
3235 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3236 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3237 return; /* no tree diffs in patch format */
3239 run_checkdiff(p
, o
);
3242 int diff_queue_is_empty(void)
3244 struct diff_queue_struct
*q
= &diff_queued_diff
;
3246 for (i
= 0; i
< q
->nr
; i
++)
3247 if (!diff_unmodified_pair(q
->queue
[i
]))
3253 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
3255 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
3258 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
3260 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
3261 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
3263 s
->size
, s
->xfrm_flags
);
3266 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
3268 diff_debug_filespec(p
->one
, i
, "one");
3269 diff_debug_filespec(p
->two
, i
, "two");
3270 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
3271 p
->score
, p
->status
? p
->status
: '?',
3272 p
->one
->rename_used
, p
->broken_pair
);
3275 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
3279 fprintf(stderr
, "%s\n", msg
);
3280 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
3281 for (i
= 0; i
< q
->nr
; i
++) {
3282 struct diff_filepair
*p
= q
->queue
[i
];
3283 diff_debug_filepair(p
, i
);
3288 static void diff_resolve_rename_copy(void)
3291 struct diff_filepair
*p
;
3292 struct diff_queue_struct
*q
= &diff_queued_diff
;
3294 diff_debug_queue("resolve-rename-copy", q
);
3296 for (i
= 0; i
< q
->nr
; i
++) {
3298 p
->status
= 0; /* undecided */
3299 if (DIFF_PAIR_UNMERGED(p
))
3300 p
->status
= DIFF_STATUS_UNMERGED
;
3301 else if (!DIFF_FILE_VALID(p
->one
))
3302 p
->status
= DIFF_STATUS_ADDED
;
3303 else if (!DIFF_FILE_VALID(p
->two
))
3304 p
->status
= DIFF_STATUS_DELETED
;
3305 else if (DIFF_PAIR_TYPE_CHANGED(p
))
3306 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
3308 /* from this point on, we are dealing with a pair
3309 * whose both sides are valid and of the same type, i.e.
3310 * either in-place edit or rename/copy edit.
3312 else if (DIFF_PAIR_RENAME(p
)) {
3314 * A rename might have re-connected a broken
3315 * pair up, causing the pathnames to be the
3316 * same again. If so, that's not a rename at
3317 * all, just a modification..
3319 * Otherwise, see if this source was used for
3320 * multiple renames, in which case we decrement
3321 * the count, and call it a copy.
3323 if (!strcmp(p
->one
->path
, p
->two
->path
))
3324 p
->status
= DIFF_STATUS_MODIFIED
;
3325 else if (--p
->one
->rename_used
> 0)
3326 p
->status
= DIFF_STATUS_COPIED
;
3328 p
->status
= DIFF_STATUS_RENAMED
;
3330 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
3331 p
->one
->mode
!= p
->two
->mode
||
3332 p
->one
->dirty_submodule
||
3333 p
->two
->dirty_submodule
||
3334 is_null_sha1(p
->one
->sha1
))
3335 p
->status
= DIFF_STATUS_MODIFIED
;
3337 /* This is a "no-change" entry and should not
3338 * happen anymore, but prepare for broken callers.
3340 error("feeding unmodified %s to diffcore",
3342 p
->status
= DIFF_STATUS_UNKNOWN
;
3345 diff_debug_queue("resolve-rename-copy done", q
);
3348 static int check_pair_status(struct diff_filepair
*p
)
3350 switch (p
->status
) {
3351 case DIFF_STATUS_UNKNOWN
:
3354 die("internal error in diff-resolve-rename-copy");
3360 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
3362 int fmt
= opt
->output_format
;
3364 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
3365 diff_flush_checkdiff(p
, opt
);
3366 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
3367 diff_flush_raw(p
, opt
);
3368 else if (fmt
& DIFF_FORMAT_NAME
) {
3369 const char *name_a
, *name_b
;
3370 name_a
= p
->two
->path
;
3372 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3373 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
3377 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
3380 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
3382 fprintf(file
, " %s ", newdelete
);
3383 write_name_quoted(fs
->path
, file
, '\n');
3387 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
3389 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
3390 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
3391 show_name
? ' ' : '\n');
3393 write_name_quoted(p
->two
->path
, file
, '\n');
3398 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
3400 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
3402 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
3404 show_mode_change(file
, p
, 0);
3407 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
3410 case DIFF_STATUS_DELETED
:
3411 show_file_mode_name(file
, "delete", p
->one
);
3413 case DIFF_STATUS_ADDED
:
3414 show_file_mode_name(file
, "create", p
->two
);
3416 case DIFF_STATUS_COPIED
:
3417 show_rename_copy(file
, "copy", p
);
3419 case DIFF_STATUS_RENAMED
:
3420 show_rename_copy(file
, "rename", p
);
3424 fputs(" rewrite ", file
);
3425 write_name_quoted(p
->two
->path
, file
, ' ');
3426 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3428 show_mode_change(file
, p
, !p
->score
);
3438 static int remove_space(char *line
, int len
)
3444 for (i
= 0; i
< len
; i
++)
3445 if (!isspace((c
= line
[i
])))
3451 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3453 struct patch_id_t
*data
= priv
;
3456 /* Ignore line numbers when computing the SHA1 of the patch */
3457 if (!prefixcmp(line
, "@@ -"))
3460 new_len
= remove_space(line
, len
);
3462 git_SHA1_Update(data
->ctx
, line
, new_len
);
3463 data
->patchlen
+= new_len
;
3466 /* returns 0 upon success, and writes result into sha1 */
3467 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3469 struct diff_queue_struct
*q
= &diff_queued_diff
;
3472 struct patch_id_t data
;
3473 char buffer
[PATH_MAX
* 4 + 20];
3475 git_SHA1_Init(&ctx
);
3476 memset(&data
, 0, sizeof(struct patch_id_t
));
3479 for (i
= 0; i
< q
->nr
; i
++) {
3484 struct diff_filepair
*p
= q
->queue
[i
];
3487 memset(&xpp
, 0, sizeof(xpp
));
3488 memset(&xecfg
, 0, sizeof(xecfg
));
3490 return error("internal diff status error");
3491 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3493 if (diff_unmodified_pair(p
))
3495 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3496 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3498 if (DIFF_PAIR_UNMERGED(p
))
3501 diff_fill_sha1_info(p
->one
);
3502 diff_fill_sha1_info(p
->two
);
3503 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3504 fill_mmfile(&mf2
, p
->two
) < 0)
3505 return error("unable to read files to diff");
3507 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3508 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3509 if (p
->one
->mode
== 0)
3510 len1
= snprintf(buffer
, sizeof(buffer
),
3511 "diff--gita/%.*sb/%.*s"
3518 len2
, p
->two
->path
);
3519 else if (p
->two
->mode
== 0)
3520 len1
= snprintf(buffer
, sizeof(buffer
),
3521 "diff--gita/%.*sb/%.*s"
3522 "deletedfilemode%06o"
3528 len1
, p
->one
->path
);
3530 len1
= snprintf(buffer
, sizeof(buffer
),
3531 "diff--gita/%.*sb/%.*s"
3537 len2
, p
->two
->path
);
3538 git_SHA1_Update(&ctx
, buffer
, len1
);
3540 xpp
.flags
= XDF_NEED_MINIMAL
;
3542 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3543 xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
3544 &xpp
, &xecfg
, &ecb
);
3547 git_SHA1_Final(sha1
, &ctx
);
3551 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3553 struct diff_queue_struct
*q
= &diff_queued_diff
;
3555 int result
= diff_get_patch_id(options
, sha1
);
3557 for (i
= 0; i
< q
->nr
; i
++)
3558 diff_free_filepair(q
->queue
[i
]);
3562 q
->nr
= q
->alloc
= 0;
3567 static int is_summary_empty(const struct diff_queue_struct
*q
)
3571 for (i
= 0; i
< q
->nr
; i
++) {
3572 const struct diff_filepair
*p
= q
->queue
[i
];
3574 switch (p
->status
) {
3575 case DIFF_STATUS_DELETED
:
3576 case DIFF_STATUS_ADDED
:
3577 case DIFF_STATUS_COPIED
:
3578 case DIFF_STATUS_RENAMED
:
3583 if (p
->one
->mode
&& p
->two
->mode
&&
3584 p
->one
->mode
!= p
->two
->mode
)
3592 void diff_flush(struct diff_options
*options
)
3594 struct diff_queue_struct
*q
= &diff_queued_diff
;
3595 int i
, output_format
= options
->output_format
;
3599 * Order: raw, stat, summary, patch
3600 * or: name/name-status/checkdiff (other bits clear)
3605 if (output_format
& (DIFF_FORMAT_RAW
|
3607 DIFF_FORMAT_NAME_STATUS
|
3608 DIFF_FORMAT_CHECKDIFF
)) {
3609 for (i
= 0; i
< q
->nr
; i
++) {
3610 struct diff_filepair
*p
= q
->queue
[i
];
3611 if (check_pair_status(p
))
3612 flush_one_pair(p
, options
);
3617 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3618 struct diffstat_t diffstat
;
3620 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3621 for (i
= 0; i
< q
->nr
; i
++) {
3622 struct diff_filepair
*p
= q
->queue
[i
];
3623 if (check_pair_status(p
))
3624 diff_flush_stat(p
, options
, &diffstat
);
3626 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3627 show_numstat(&diffstat
, options
);
3628 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3629 show_stats(&diffstat
, options
);
3630 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3631 show_shortstats(&diffstat
, options
);
3632 free_diffstat_info(&diffstat
);
3635 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3636 show_dirstat(options
);
3638 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3639 for (i
= 0; i
< q
->nr
; i
++)
3640 diff_summary(options
->file
, q
->queue
[i
]);
3644 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
3645 DIFF_OPT_TST(options
, EXIT_WITH_STATUS
) &&
3646 DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
3648 * run diff_flush_patch for the exit status. setting
3649 * options->file to /dev/null should be safe, becaue we
3650 * aren't supposed to produce any output anyway.
3652 if (options
->close_file
)
3653 fclose(options
->file
);
3654 options
->file
= fopen("/dev/null", "w");
3656 die_errno("Could not open /dev/null");
3657 options
->close_file
= 1;
3658 for (i
= 0; i
< q
->nr
; i
++) {
3659 struct diff_filepair
*p
= q
->queue
[i
];
3660 if (check_pair_status(p
))
3661 diff_flush_patch(p
, options
);
3662 if (options
->found_changes
)
3667 if (output_format
& DIFF_FORMAT_PATCH
) {
3669 putc(options
->line_termination
, options
->file
);
3670 if (options
->stat_sep
) {
3671 /* attach patch instead of inline */
3672 fputs(options
->stat_sep
, options
->file
);
3676 for (i
= 0; i
< q
->nr
; i
++) {
3677 struct diff_filepair
*p
= q
->queue
[i
];
3678 if (check_pair_status(p
))
3679 diff_flush_patch(p
, options
);
3683 if (output_format
& DIFF_FORMAT_CALLBACK
)
3684 options
->format_callback(q
, options
, options
->format_callback_data
);
3686 for (i
= 0; i
< q
->nr
; i
++)
3687 diff_free_filepair(q
->queue
[i
]);
3691 q
->nr
= q
->alloc
= 0;
3692 if (options
->close_file
)
3693 fclose(options
->file
);
3696 * Report the content-level differences with HAS_CHANGES;
3697 * diff_addremove/diff_change does not set the bit when
3698 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
3700 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
3701 if (options
->found_changes
)
3702 DIFF_OPT_SET(options
, HAS_CHANGES
);
3704 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3708 static void diffcore_apply_filter(const char *filter
)
3711 struct diff_queue_struct
*q
= &diff_queued_diff
;
3712 struct diff_queue_struct outq
;
3714 outq
.nr
= outq
.alloc
= 0;
3719 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3721 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3722 struct diff_filepair
*p
= q
->queue
[i
];
3723 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3725 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3727 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3728 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3729 strchr(filter
, p
->status
)))
3735 /* otherwise we will clear the whole queue
3736 * by copying the empty outq at the end of this
3737 * function, but first clear the current entries
3740 for (i
= 0; i
< q
->nr
; i
++)
3741 diff_free_filepair(q
->queue
[i
]);
3744 /* Only the matching ones */
3745 for (i
= 0; i
< q
->nr
; i
++) {
3746 struct diff_filepair
*p
= q
->queue
[i
];
3748 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3750 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3752 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3753 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3754 strchr(filter
, p
->status
)))
3757 diff_free_filepair(p
);
3764 /* Check whether two filespecs with the same mode and size are identical */
3765 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3766 struct diff_filespec
*two
)
3768 if (S_ISGITLINK(one
->mode
))
3770 if (diff_populate_filespec(one
, 0))
3772 if (diff_populate_filespec(two
, 0))
3774 return !memcmp(one
->data
, two
->data
, one
->size
);
3777 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3780 struct diff_queue_struct
*q
= &diff_queued_diff
;
3781 struct diff_queue_struct outq
;
3783 outq
.nr
= outq
.alloc
= 0;
3785 for (i
= 0; i
< q
->nr
; i
++) {
3786 struct diff_filepair
*p
= q
->queue
[i
];
3789 * 1. Entries that come from stat info dirtiness
3790 * always have both sides (iow, not create/delete),
3791 * one side of the object name is unknown, with
3792 * the same mode and size. Keep the ones that
3793 * do not match these criteria. They have real
3796 * 2. At this point, the file is known to be modified,
3797 * with the same mode and size, and the object
3798 * name of one side is unknown. Need to inspect
3799 * the identical contents.
3801 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3802 !DIFF_FILE_VALID(p
->two
) ||
3803 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3804 (p
->one
->mode
!= p
->two
->mode
) ||
3805 diff_populate_filespec(p
->one
, 1) ||
3806 diff_populate_filespec(p
->two
, 1) ||
3807 (p
->one
->size
!= p
->two
->size
) ||
3808 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3812 * The caller can subtract 1 from skip_stat_unmatch
3813 * to determine how many paths were dirty only
3814 * due to stat info mismatch.
3816 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3817 diffopt
->skip_stat_unmatch
++;
3818 diff_free_filepair(p
);
3825 static int diffnamecmp(const void *a_
, const void *b_
)
3827 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
3828 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
3829 const char *name_a
, *name_b
;
3831 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
3832 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
3833 return strcmp(name_a
, name_b
);
3836 void diffcore_fix_diff_index(struct diff_options
*options
)
3838 struct diff_queue_struct
*q
= &diff_queued_diff
;
3839 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), diffnamecmp
);
3842 void diffcore_std(struct diff_options
*options
)
3844 if (options
->skip_stat_unmatch
)
3845 diffcore_skip_stat_unmatch(options
);
3846 if (options
->break_opt
!= -1)
3847 diffcore_break(options
->break_opt
);
3848 if (options
->detect_rename
)
3849 diffcore_rename(options
);
3850 if (options
->break_opt
!= -1)
3851 diffcore_merge_broken();
3852 if (options
->pickaxe
)
3853 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3854 if (options
->orderfile
)
3855 diffcore_order(options
->orderfile
);
3856 diff_resolve_rename_copy();
3857 diffcore_apply_filter(options
->filter
);
3859 if (diff_queued_diff
.nr
&& !DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3860 DIFF_OPT_SET(options
, HAS_CHANGES
);
3862 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3865 int diff_result_code(struct diff_options
*opt
, int status
)
3868 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3869 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3871 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3872 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3874 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3875 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3880 void diff_addremove(struct diff_options
*options
,
3881 int addremove
, unsigned mode
,
3882 const unsigned char *sha1
,
3883 const char *concatpath
, unsigned dirty_submodule
)
3885 struct diff_filespec
*one
, *two
;
3887 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
3890 /* This may look odd, but it is a preparation for
3891 * feeding "there are unchanged files which should
3892 * not produce diffs, but when you are doing copy
3893 * detection you would need them, so here they are"
3894 * entries to the diff-core. They will be prefixed
3895 * with something like '=' or '*' (I haven't decided
3896 * which but should not make any difference).
3897 * Feeding the same new and old to diff_change()
3898 * also has the same effect.
3899 * Before the final output happens, they are pruned after
3900 * merged into rename/copy pairs as appropriate.
3902 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3903 addremove
= (addremove
== '+' ? '-' :
3904 addremove
== '-' ? '+' : addremove
);
3906 if (options
->prefix
&&
3907 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3910 one
= alloc_filespec(concatpath
);
3911 two
= alloc_filespec(concatpath
);
3913 if (addremove
!= '+')
3914 fill_filespec(one
, sha1
, mode
);
3915 if (addremove
!= '-') {
3916 fill_filespec(two
, sha1
, mode
);
3917 two
->dirty_submodule
= dirty_submodule
;
3920 diff_queue(&diff_queued_diff
, one
, two
);
3921 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3922 DIFF_OPT_SET(options
, HAS_CHANGES
);
3925 void diff_change(struct diff_options
*options
,
3926 unsigned old_mode
, unsigned new_mode
,
3927 const unsigned char *old_sha1
,
3928 const unsigned char *new_sha1
,
3929 const char *concatpath
,
3930 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
3932 struct diff_filespec
*one
, *two
;
3934 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
3935 && S_ISGITLINK(new_mode
))
3938 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3940 const unsigned char *tmp_c
;
3941 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3942 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3943 tmp
= old_dirty_submodule
; old_dirty_submodule
= new_dirty_submodule
;
3944 new_dirty_submodule
= tmp
;
3947 if (options
->prefix
&&
3948 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3951 one
= alloc_filespec(concatpath
);
3952 two
= alloc_filespec(concatpath
);
3953 fill_filespec(one
, old_sha1
, old_mode
);
3954 fill_filespec(two
, new_sha1
, new_mode
);
3955 one
->dirty_submodule
= old_dirty_submodule
;
3956 two
->dirty_submodule
= new_dirty_submodule
;
3958 diff_queue(&diff_queued_diff
, one
, two
);
3959 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3960 DIFF_OPT_SET(options
, HAS_CHANGES
);
3963 void diff_unmerge(struct diff_options
*options
,
3965 unsigned mode
, const unsigned char *sha1
)
3967 struct diff_filespec
*one
, *two
;
3969 if (options
->prefix
&&
3970 strncmp(path
, options
->prefix
, options
->prefix_length
))
3973 one
= alloc_filespec(path
);
3974 two
= alloc_filespec(path
);
3975 fill_filespec(one
, sha1
, mode
);
3976 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;
3979 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
3982 struct diff_tempfile
*temp
;
3983 const char *argv
[3];
3984 const char **arg
= argv
;
3985 struct child_process child
;
3986 struct strbuf buf
= STRBUF_INIT
;
3989 temp
= prepare_temp_file(spec
->path
, spec
);
3991 *arg
++ = temp
->name
;
3994 memset(&child
, 0, sizeof(child
));
3995 child
.use_shell
= 1;
3998 if (start_command(&child
)) {
4003 if (strbuf_read(&buf
, child
.out
, 0) < 0)
4004 err
= error("error reading from textconv command '%s'", pgm
);
4007 if (finish_command(&child
) || err
) {
4008 strbuf_release(&buf
);
4014 return strbuf_detach(&buf
, outsize
);