2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "run-command.h"
16 #include "submodule.h"
19 #ifdef NO_FAST_WORKING_DIRECTORY
20 #define FAST_WORKING_DIRECTORY 0
22 #define FAST_WORKING_DIRECTORY 1
25 static int diff_detect_rename_default
;
26 static int diff_rename_limit_default
= 200;
27 static int diff_suppress_blank_empty
;
28 int diff_use_color_default
= -1;
29 static const char *diff_word_regex_cfg
;
30 static const char *external_diff_cmd_cfg
;
31 int diff_auto_refresh_index
= 1;
32 static int diff_mnemonic_prefix
;
34 static char diff_colors
[][COLOR_MAXLEN
] = {
36 GIT_COLOR_NORMAL
, /* PLAIN */
37 GIT_COLOR_BOLD
, /* METAINFO */
38 GIT_COLOR_CYAN
, /* FRAGINFO */
39 GIT_COLOR_RED
, /* OLD */
40 GIT_COLOR_GREEN
, /* NEW */
41 GIT_COLOR_YELLOW
, /* COMMIT */
42 GIT_COLOR_BG_RED
, /* WHITESPACE */
43 GIT_COLOR_NORMAL
, /* FUNCINFO */
46 static void diff_filespec_load_driver(struct diff_filespec
*one
);
47 static char *run_textconv(const char *, struct diff_filespec
*, size_t *);
49 static int parse_diff_color_slot(const char *var
, int ofs
)
51 if (!strcasecmp(var
+ofs
, "plain"))
53 if (!strcasecmp(var
+ofs
, "meta"))
55 if (!strcasecmp(var
+ofs
, "frag"))
57 if (!strcasecmp(var
+ofs
, "old"))
59 if (!strcasecmp(var
+ofs
, "new"))
61 if (!strcasecmp(var
+ofs
, "commit"))
63 if (!strcasecmp(var
+ofs
, "whitespace"))
64 return DIFF_WHITESPACE
;
65 if (!strcasecmp(var
+ofs
, "func"))
70 static int git_config_rename(const char *var
, const char *value
)
73 return DIFF_DETECT_RENAME
;
74 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
75 return DIFF_DETECT_COPY
;
76 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
80 * These are to give UI layer defaults.
81 * The core-level commands such as git-diff-files should
82 * never be affected by the setting of diff.renames
83 * the user happens to have in the configuration file.
85 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
87 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
88 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
91 if (!strcmp(var
, "diff.renames")) {
92 diff_detect_rename_default
= git_config_rename(var
, value
);
95 if (!strcmp(var
, "diff.autorefreshindex")) {
96 diff_auto_refresh_index
= git_config_bool(var
, value
);
99 if (!strcmp(var
, "diff.mnemonicprefix")) {
100 diff_mnemonic_prefix
= git_config_bool(var
, value
);
103 if (!strcmp(var
, "diff.external"))
104 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
105 if (!strcmp(var
, "diff.wordregex"))
106 return git_config_string(&diff_word_regex_cfg
, var
, value
);
108 return git_diff_basic_config(var
, value
, cb
);
111 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
113 if (!strcmp(var
, "diff.renamelimit")) {
114 diff_rename_limit_default
= git_config_int(var
, value
);
118 switch (userdiff_config(var
, value
)) {
124 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
125 int slot
= parse_diff_color_slot(var
, 11);
129 return config_error_nonbool(var
);
130 color_parse(value
, var
, diff_colors
[slot
]);
134 /* like GNU diff's --suppress-blank-empty option */
135 if (!strcmp(var
, "diff.suppressblankempty") ||
136 /* for backwards compatibility */
137 !strcmp(var
, "diff.suppress-blank-empty")) {
138 diff_suppress_blank_empty
= git_config_bool(var
, value
);
142 return git_color_default_config(var
, value
, cb
);
145 static char *quote_two(const char *one
, const char *two
)
147 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
148 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
149 struct strbuf res
= STRBUF_INIT
;
151 if (need_one
+ need_two
) {
152 strbuf_addch(&res
, '"');
153 quote_c_style(one
, &res
, NULL
, 1);
154 quote_c_style(two
, &res
, NULL
, 1);
155 strbuf_addch(&res
, '"');
157 strbuf_addstr(&res
, one
);
158 strbuf_addstr(&res
, two
);
160 return strbuf_detach(&res
, NULL
);
163 static const char *external_diff(void)
165 static const char *external_diff_cmd
= NULL
;
166 static int done_preparing
= 0;
169 return external_diff_cmd
;
170 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
171 if (!external_diff_cmd
)
172 external_diff_cmd
= external_diff_cmd_cfg
;
174 return external_diff_cmd
;
177 static struct diff_tempfile
{
178 const char *name
; /* filename external diff should read from */
181 char tmp_path
[PATH_MAX
];
184 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
186 struct emit_callback
{
189 int blank_at_eof_in_preimage
;
190 int blank_at_eof_in_postimage
;
192 int lno_in_postimage
;
193 sane_truncate_fn truncate
;
194 const char **label_path
;
195 struct diff_words_data
*diff_words
;
198 struct strbuf
*header
;
201 static int count_lines(const char *data
, int size
)
203 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
210 completely_empty
= 0;
214 completely_empty
= 0;
217 if (completely_empty
)
220 count
++; /* no trailing newline */
224 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
226 if (!DIFF_FILE_VALID(one
)) {
227 mf
->ptr
= (char *)""; /* does not matter */
231 else if (diff_populate_filespec(one
, 0))
235 mf
->size
= one
->size
;
239 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
242 long size
= mf
->size
;
247 ptr
+= size
- 1; /* pointing at the very end */
249 ; /* incomplete line */
251 ptr
--; /* skip the last LF */
252 while (mf
->ptr
< ptr
) {
254 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
255 if (*prev_eol
== '\n')
257 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
265 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
266 struct emit_callback
*ecbdata
)
269 unsigned ws_rule
= ecbdata
->ws_rule
;
270 l1
= count_trailing_blank(mf1
, ws_rule
);
271 l2
= count_trailing_blank(mf2
, ws_rule
);
273 ecbdata
->blank_at_eof_in_preimage
= 0;
274 ecbdata
->blank_at_eof_in_postimage
= 0;
277 at
= count_lines(mf1
->ptr
, mf1
->size
);
278 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
280 at
= count_lines(mf2
->ptr
, mf2
->size
);
281 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
284 static void emit_line_0(FILE *file
, const char *set
, const char *reset
,
285 int first
, const char *line
, int len
)
287 int has_trailing_newline
, has_trailing_carriage_return
;
291 has_trailing_newline
= (first
== '\n');
292 has_trailing_carriage_return
= (!has_trailing_newline
&&
294 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
296 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
297 if (has_trailing_newline
)
299 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
300 if (has_trailing_carriage_return
)
305 if (len
|| !nofirst
) {
309 fwrite(line
, len
, 1, file
);
312 if (has_trailing_carriage_return
)
314 if (has_trailing_newline
)
318 static void emit_line(FILE *file
, const char *set
, const char *reset
,
319 const char *line
, int len
)
321 emit_line_0(file
, set
, reset
, line
[0], line
+1, len
-1);
324 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
326 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
327 ecbdata
->blank_at_eof_in_preimage
&&
328 ecbdata
->blank_at_eof_in_postimage
&&
329 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
330 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
332 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
335 static void emit_add_line(const char *reset
,
336 struct emit_callback
*ecbdata
,
337 const char *line
, int len
)
339 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
340 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
343 emit_line_0(ecbdata
->file
, set
, reset
, '+', line
, len
);
344 else if (new_blank_line_at_eof(ecbdata
, line
, len
))
345 /* Blank line at EOF - paint '+' as well */
346 emit_line_0(ecbdata
->file
, ws
, reset
, '+', line
, len
);
348 /* Emit just the prefix, then the rest. */
349 emit_line_0(ecbdata
->file
, set
, reset
, '+', "", 0);
350 ws_check_emit(line
, len
, ecbdata
->ws_rule
,
351 ecbdata
->file
, set
, reset
, ws
);
355 static void emit_hunk_header(struct emit_callback
*ecbdata
,
356 const char *line
, int len
)
358 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
359 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
360 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
361 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
362 static const char atat
[2] = { '@', '@' };
366 * As a hunk header must begin with "@@ -<old>, +<new> @@",
367 * it always is at least 10 bytes long.
370 memcmp(line
, atat
, 2) ||
371 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
372 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
375 ep
+= 2; /* skip over @@ */
377 /* The hunk header in fraginfo color */
378 emit_line(ecbdata
->file
, frag
, reset
, line
, ep
- line
);
380 /* blank before the func header */
381 for (cp
= ep
; ep
- line
< len
; ep
++)
382 if (*ep
!= ' ' && *ep
!= '\t')
385 emit_line(ecbdata
->file
, plain
, reset
, cp
, ep
- cp
);
388 emit_line(ecbdata
->file
, func
, reset
, ep
, line
+ len
- ep
);
391 static struct diff_tempfile
*claim_diff_tempfile(void) {
393 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
394 if (!diff_temp
[i
].name
)
395 return diff_temp
+ i
;
396 die("BUG: diff is failing to clean up its tempfiles");
399 static int remove_tempfile_installed
;
401 static void remove_tempfile(void)
404 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
405 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
)
406 unlink_or_warn(diff_temp
[i
].name
);
407 diff_temp
[i
].name
= NULL
;
411 static void remove_tempfile_on_signal(int signo
)
418 static void print_line_count(FILE *file
, int count
)
422 fprintf(file
, "0,0");
428 fprintf(file
, "1,%d", count
);
433 static void emit_rewrite_lines(struct emit_callback
*ecb
,
434 int prefix
, const char *data
, int size
)
436 const char *endp
= NULL
;
437 static const char *nneof
= " No newline at end of file\n";
438 const char *old
= diff_get_color(ecb
->color_diff
, DIFF_FILE_OLD
);
439 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
444 endp
= memchr(data
, '\n', size
);
445 len
= endp
? (endp
- data
+ 1) : size
;
447 ecb
->lno_in_preimage
++;
448 emit_line_0(ecb
->file
, old
, reset
, '-',
451 ecb
->lno_in_postimage
++;
452 emit_add_line(reset
, ecb
, data
, len
);
458 const char *plain
= diff_get_color(ecb
->color_diff
,
460 emit_line_0(ecb
->file
, plain
, reset
, '\\',
461 nneof
, strlen(nneof
));
465 static void emit_rewrite_diff(const char *name_a
,
467 struct diff_filespec
*one
,
468 struct diff_filespec
*two
,
469 const char *textconv_one
,
470 const char *textconv_two
,
471 struct diff_options
*o
)
474 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
475 const char *name_a_tab
, *name_b_tab
;
476 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
477 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
478 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
479 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
480 const char *a_prefix
, *b_prefix
;
481 const char *data_one
, *data_two
;
482 size_t size_one
, size_two
;
483 struct emit_callback ecbdata
;
485 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
486 a_prefix
= o
->b_prefix
;
487 b_prefix
= o
->a_prefix
;
489 a_prefix
= o
->a_prefix
;
490 b_prefix
= o
->b_prefix
;
493 name_a
+= (*name_a
== '/');
494 name_b
+= (*name_b
== '/');
495 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
496 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
498 strbuf_reset(&a_name
);
499 strbuf_reset(&b_name
);
500 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
501 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
503 diff_populate_filespec(one
, 0);
504 diff_populate_filespec(two
, 0);
506 data_one
= run_textconv(textconv_one
, one
, &size_one
);
508 die("unable to read files to diff");
511 data_one
= one
->data
;
512 size_one
= one
->size
;
515 data_two
= run_textconv(textconv_two
, two
, &size_two
);
517 die("unable to read files to diff");
520 data_two
= two
->data
;
521 size_two
= two
->size
;
524 memset(&ecbdata
, 0, sizeof(ecbdata
));
525 ecbdata
.color_diff
= color_diff
;
526 ecbdata
.found_changesp
= &o
->found_changes
;
527 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
528 ecbdata
.file
= o
->file
;
529 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
531 mf1
.ptr
= (char *)data_one
;
532 mf2
.ptr
= (char *)data_two
;
535 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
537 ecbdata
.lno_in_preimage
= 1;
538 ecbdata
.lno_in_postimage
= 1;
540 lc_a
= count_lines(data_one
, size_one
);
541 lc_b
= count_lines(data_two
, size_two
);
543 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
544 metainfo
, a_name
.buf
, name_a_tab
, reset
,
545 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
546 print_line_count(o
->file
, lc_a
);
547 fprintf(o
->file
, " +");
548 print_line_count(o
->file
, lc_b
);
549 fprintf(o
->file
, " @@%s\n", reset
);
551 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
553 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
555 free((char *)data_one
);
557 free((char *)data_two
);
560 struct diff_words_buffer
{
563 struct diff_words_orig
{
564 const char *begin
, *end
;
566 int orig_nr
, orig_alloc
;
569 static void diff_words_append(char *line
, unsigned long len
,
570 struct diff_words_buffer
*buffer
)
572 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
575 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
576 buffer
->text
.size
+= len
;
577 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
580 struct diff_words_data
{
581 struct diff_words_buffer minus
, plus
;
582 const char *current_plus
;
587 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
589 struct diff_words_data
*diff_words
= priv
;
590 int minus_first
, minus_len
, plus_first
, plus_len
;
591 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
593 if (line
[0] != '@' || parse_hunk_header(line
, len
,
594 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
597 /* POSIX requires that first be decremented by one if len == 0... */
599 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
601 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
603 minus_begin
= minus_end
=
604 diff_words
->minus
.orig
[minus_first
].end
;
607 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
608 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
610 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
612 if (diff_words
->current_plus
!= plus_begin
)
613 fwrite(diff_words
->current_plus
,
614 plus_begin
- diff_words
->current_plus
, 1,
616 if (minus_begin
!= minus_end
)
617 color_fwrite_lines(diff_words
->file
,
618 diff_get_color(1, DIFF_FILE_OLD
),
619 minus_end
- minus_begin
, minus_begin
);
620 if (plus_begin
!= plus_end
)
621 color_fwrite_lines(diff_words
->file
,
622 diff_get_color(1, DIFF_FILE_NEW
),
623 plus_end
- plus_begin
, plus_begin
);
625 diff_words
->current_plus
= plus_end
;
628 /* This function starts looking at *begin, and returns 0 iff a word was found. */
629 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
630 int *begin
, int *end
)
632 if (word_regex
&& *begin
< buffer
->size
) {
634 if (!regexec(word_regex
, buffer
->ptr
+ *begin
, 1, match
, 0)) {
635 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
636 '\n', match
[0].rm_eo
- match
[0].rm_so
);
637 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
638 *begin
+= match
[0].rm_so
;
639 return *begin
>= *end
;
644 /* find the next word */
645 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
647 if (*begin
>= buffer
->size
)
650 /* find the end of the word */
652 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
659 * This function splits the words in buffer->text, stores the list with
660 * newline separator into out, and saves the offsets of the original words
663 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
672 /* fake an empty "0th" word */
673 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
674 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
677 for (i
= 0; i
< buffer
->text
.size
; i
++) {
678 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
681 /* store original boundaries */
682 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
684 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
685 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
689 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
690 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
691 out
->ptr
[out
->size
+ j
- i
] = '\n';
692 out
->size
+= j
- i
+ 1;
698 /* this executes the word diff on the accumulated buffers */
699 static void diff_words_show(struct diff_words_data
*diff_words
)
704 mmfile_t minus
, plus
;
706 /* special case: only removal */
707 if (!diff_words
->plus
.text
.size
) {
708 color_fwrite_lines(diff_words
->file
,
709 diff_get_color(1, DIFF_FILE_OLD
),
710 diff_words
->minus
.text
.size
, diff_words
->minus
.text
.ptr
);
711 diff_words
->minus
.text
.size
= 0;
715 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
717 memset(&xpp
, 0, sizeof(xpp
));
718 memset(&xecfg
, 0, sizeof(xecfg
));
719 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
720 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
721 xpp
.flags
= XDF_NEED_MINIMAL
;
722 /* as only the hunk header will be parsed, we need a 0-context */
724 xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
728 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
729 diff_words
->plus
.text
.size
)
730 fwrite(diff_words
->current_plus
,
731 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
732 - diff_words
->current_plus
, 1,
734 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
737 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
738 static void diff_words_flush(struct emit_callback
*ecbdata
)
740 if (ecbdata
->diff_words
->minus
.text
.size
||
741 ecbdata
->diff_words
->plus
.text
.size
)
742 diff_words_show(ecbdata
->diff_words
);
745 static void free_diff_words_data(struct emit_callback
*ecbdata
)
747 if (ecbdata
->diff_words
) {
748 diff_words_flush(ecbdata
);
749 free (ecbdata
->diff_words
->minus
.text
.ptr
);
750 free (ecbdata
->diff_words
->minus
.orig
);
751 free (ecbdata
->diff_words
->plus
.text
.ptr
);
752 free (ecbdata
->diff_words
->plus
.orig
);
753 free(ecbdata
->diff_words
->word_regex
);
754 free(ecbdata
->diff_words
);
755 ecbdata
->diff_words
= NULL
;
759 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
762 return diff_colors
[ix
];
766 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
773 return ecb
->truncate(line
, len
);
777 (void) utf8_width(&cp
, &l
);
779 break; /* truncated in the middle? */
784 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
787 ecbdata
->lno_in_preimage
= 0;
788 ecbdata
->lno_in_postimage
= 0;
789 p
= strchr(line
, '-');
791 return; /* cannot happen */
792 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
795 return; /* cannot happen */
796 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
799 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
801 struct emit_callback
*ecbdata
= priv
;
802 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
803 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
804 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
806 if (ecbdata
->header
) {
807 fprintf(ecbdata
->file
, "%s", ecbdata
->header
->buf
);
808 strbuf_reset(ecbdata
->header
);
809 ecbdata
->header
= NULL
;
811 *(ecbdata
->found_changesp
) = 1;
813 if (ecbdata
->label_path
[0]) {
814 const char *name_a_tab
, *name_b_tab
;
816 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
817 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
819 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
820 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
821 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
822 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
823 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
826 if (diff_suppress_blank_empty
827 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
832 if (line
[0] == '@') {
833 if (ecbdata
->diff_words
)
834 diff_words_flush(ecbdata
);
835 len
= sane_truncate_line(ecbdata
, line
, len
);
836 find_lno(line
, ecbdata
);
837 emit_hunk_header(ecbdata
, line
, len
);
838 if (line
[len
-1] != '\n')
839 putc('\n', ecbdata
->file
);
844 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
848 if (ecbdata
->diff_words
) {
849 if (line
[0] == '-') {
850 diff_words_append(line
, len
,
851 &ecbdata
->diff_words
->minus
);
853 } else if (line
[0] == '+') {
854 diff_words_append(line
, len
,
855 &ecbdata
->diff_words
->plus
);
858 diff_words_flush(ecbdata
);
861 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
865 if (line
[0] != '+') {
867 diff_get_color(ecbdata
->color_diff
,
868 line
[0] == '-' ? DIFF_FILE_OLD
: DIFF_PLAIN
);
869 ecbdata
->lno_in_preimage
++;
871 ecbdata
->lno_in_postimage
++;
872 emit_line(ecbdata
->file
, color
, reset
, line
, len
);
874 ecbdata
->lno_in_postimage
++;
875 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
879 static char *pprint_rename(const char *a
, const char *b
)
883 struct strbuf name
= STRBUF_INIT
;
884 int pfx_length
, sfx_length
;
885 int len_a
= strlen(a
);
886 int len_b
= strlen(b
);
887 int a_midlen
, b_midlen
;
888 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
889 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
891 if (qlen_a
|| qlen_b
) {
892 quote_c_style(a
, &name
, NULL
, 0);
893 strbuf_addstr(&name
, " => ");
894 quote_c_style(b
, &name
, NULL
, 0);
895 return strbuf_detach(&name
, NULL
);
898 /* Find common prefix */
900 while (*old
&& *new && *old
== *new) {
902 pfx_length
= old
- a
+ 1;
907 /* Find common suffix */
911 while (a
<= old
&& b
<= new && *old
== *new) {
913 sfx_length
= len_a
- (old
- a
);
919 * pfx{mid-a => mid-b}sfx
920 * {pfx-a => pfx-b}sfx
921 * pfx{sfx-a => sfx-b}
924 a_midlen
= len_a
- pfx_length
- sfx_length
;
925 b_midlen
= len_b
- pfx_length
- sfx_length
;
931 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
932 if (pfx_length
+ sfx_length
) {
933 strbuf_add(&name
, a
, pfx_length
);
934 strbuf_addch(&name
, '{');
936 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
937 strbuf_addstr(&name
, " => ");
938 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
939 if (pfx_length
+ sfx_length
) {
940 strbuf_addch(&name
, '}');
941 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
943 return strbuf_detach(&name
, NULL
);
949 struct diffstat_file
{
953 unsigned is_unmerged
:1;
954 unsigned is_binary
:1;
955 unsigned is_renamed
:1;
956 uintmax_t added
, deleted
;
960 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
964 struct diffstat_file
*x
;
965 x
= xcalloc(sizeof (*x
), 1);
966 if (diffstat
->nr
== diffstat
->alloc
) {
967 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
968 diffstat
->files
= xrealloc(diffstat
->files
,
969 diffstat
->alloc
* sizeof(x
));
971 diffstat
->files
[diffstat
->nr
++] = x
;
973 x
->from_name
= xstrdup(name_a
);
974 x
->name
= xstrdup(name_b
);
979 x
->name
= xstrdup(name_a
);
984 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
986 struct diffstat_t
*diffstat
= priv
;
987 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
991 else if (line
[0] == '-')
995 const char mime_boundary_leader
[] = "------------";
997 static int scale_linear(int it
, int width
, int max_change
)
1000 * make sure that at least one '-' is printed if there were deletions,
1001 * and likewise for '+'.
1005 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
1008 static void show_name(FILE *file
,
1009 const char *prefix
, const char *name
, int len
)
1011 fprintf(file
, " %s%-*s |", prefix
, len
, name
);
1014 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
1018 fprintf(file
, "%s", set
);
1021 fprintf(file
, "%s", reset
);
1024 static void fill_print_name(struct diffstat_file
*file
)
1028 if (file
->print_name
)
1031 if (!file
->is_renamed
) {
1032 struct strbuf buf
= STRBUF_INIT
;
1033 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
1034 pname
= strbuf_detach(&buf
, NULL
);
1037 strbuf_release(&buf
);
1040 pname
= pprint_rename(file
->from_name
, file
->name
);
1042 file
->print_name
= pname
;
1045 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
1047 int i
, len
, add
, del
, adds
= 0, dels
= 0;
1048 uintmax_t max_change
= 0, max_len
= 0;
1049 int total_files
= data
->nr
;
1050 int width
, name_width
;
1051 const char *reset
, *set
, *add_c
, *del_c
;
1056 width
= options
->stat_width
? options
->stat_width
: 80;
1057 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
1059 /* Sanity: give at least 5 columns to the graph,
1060 * but leave at least 10 columns for the name.
1064 if (name_width
< 10)
1066 else if (width
< name_width
+ 15)
1067 name_width
= width
- 15;
1069 /* Find the longest filename and max number of changes */
1070 reset
= diff_get_color_opt(options
, DIFF_RESET
);
1071 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
1072 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
1073 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
1075 for (i
= 0; i
< data
->nr
; i
++) {
1076 struct diffstat_file
*file
= data
->files
[i
];
1077 uintmax_t change
= file
->added
+ file
->deleted
;
1078 fill_print_name(file
);
1079 len
= strlen(file
->print_name
);
1083 if (file
->is_binary
|| file
->is_unmerged
)
1085 if (max_change
< change
)
1086 max_change
= change
;
1089 /* Compute the width of the graph part;
1090 * 10 is for one blank at the beginning of the line plus
1091 * " | count " between the name and the graph.
1093 * From here on, name_width is the width of the name area,
1094 * and width is the width of the graph area.
1096 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
1097 if (width
< (name_width
+ 10) + max_change
)
1098 width
= width
- (name_width
+ 10);
1102 for (i
= 0; i
< data
->nr
; i
++) {
1103 const char *prefix
= "";
1104 char *name
= data
->files
[i
]->print_name
;
1105 uintmax_t added
= data
->files
[i
]->added
;
1106 uintmax_t deleted
= data
->files
[i
]->deleted
;
1110 * "scale" the filename
1113 name_len
= strlen(name
);
1114 if (name_width
< name_len
) {
1118 name
+= name_len
- len
;
1119 slash
= strchr(name
, '/');
1124 if (data
->files
[i
]->is_binary
) {
1125 show_name(options
->file
, prefix
, name
, len
);
1126 fprintf(options
->file
, " Bin ");
1127 fprintf(options
->file
, "%s%"PRIuMAX
"%s",
1128 del_c
, deleted
, reset
);
1129 fprintf(options
->file
, " -> ");
1130 fprintf(options
->file
, "%s%"PRIuMAX
"%s",
1131 add_c
, added
, reset
);
1132 fprintf(options
->file
, " bytes");
1133 fprintf(options
->file
, "\n");
1136 else if (data
->files
[i
]->is_unmerged
) {
1137 show_name(options
->file
, prefix
, name
, len
);
1138 fprintf(options
->file
, " Unmerged\n");
1141 else if (!data
->files
[i
]->is_renamed
&&
1142 (added
+ deleted
== 0)) {
1148 * scale the add/delete
1155 if (width
<= max_change
) {
1156 add
= scale_linear(add
, width
, max_change
);
1157 del
= scale_linear(del
, width
, max_change
);
1159 show_name(options
->file
, prefix
, name
, len
);
1160 fprintf(options
->file
, "%5"PRIuMAX
"%s", added
+ deleted
,
1161 added
+ deleted
? " " : "");
1162 show_graph(options
->file
, '+', add
, add_c
, reset
);
1163 show_graph(options
->file
, '-', del
, del_c
, reset
);
1164 fprintf(options
->file
, "\n");
1166 fprintf(options
->file
,
1167 " %d files changed, %d insertions(+), %d deletions(-)\n",
1168 total_files
, adds
, dels
);
1171 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
1173 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
1178 for (i
= 0; i
< data
->nr
; i
++) {
1179 if (!data
->files
[i
]->is_binary
&&
1180 !data
->files
[i
]->is_unmerged
) {
1181 int added
= data
->files
[i
]->added
;
1182 int deleted
= data
->files
[i
]->deleted
;
1183 if (!data
->files
[i
]->is_renamed
&&
1184 (added
+ deleted
== 0)) {
1192 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
1193 total_files
, adds
, dels
);
1196 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
1203 for (i
= 0; i
< data
->nr
; i
++) {
1204 struct diffstat_file
*file
= data
->files
[i
];
1206 if (file
->is_binary
)
1207 fprintf(options
->file
, "-\t-\t");
1209 fprintf(options
->file
,
1210 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
1211 file
->added
, file
->deleted
);
1212 if (options
->line_termination
) {
1213 fill_print_name(file
);
1214 if (!file
->is_renamed
)
1215 write_name_quoted(file
->name
, options
->file
,
1216 options
->line_termination
);
1218 fputs(file
->print_name
, options
->file
);
1219 putc(options
->line_termination
, options
->file
);
1222 if (file
->is_renamed
) {
1223 putc('\0', options
->file
);
1224 write_name_quoted(file
->from_name
, options
->file
, '\0');
1226 write_name_quoted(file
->name
, options
->file
, '\0');
1231 struct dirstat_file
{
1233 unsigned long changed
;
1236 struct dirstat_dir
{
1237 struct dirstat_file
*files
;
1238 int alloc
, nr
, percent
, cumulative
;
1241 static long gather_dirstat(FILE *file
, struct dirstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1243 unsigned long this_dir
= 0;
1244 unsigned int sources
= 0;
1247 struct dirstat_file
*f
= dir
->files
;
1248 int namelen
= strlen(f
->name
);
1252 if (namelen
< baselen
)
1254 if (memcmp(f
->name
, base
, baselen
))
1256 slash
= strchr(f
->name
+ baselen
, '/');
1258 int newbaselen
= slash
+ 1 - f
->name
;
1259 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1271 * We don't report dirstat's for
1273 * - or cases where everything came from a single directory
1274 * under this directory (sources == 1).
1276 if (baselen
&& sources
!= 1) {
1277 int permille
= this_dir
* 1000 / changed
;
1279 int percent
= permille
/ 10;
1280 if (percent
>= dir
->percent
) {
1281 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1282 if (!dir
->cumulative
)
1290 static int dirstat_compare(const void *_a
, const void *_b
)
1292 const struct dirstat_file
*a
= _a
;
1293 const struct dirstat_file
*b
= _b
;
1294 return strcmp(a
->name
, b
->name
);
1297 static void show_dirstat(struct diff_options
*options
)
1300 unsigned long changed
;
1301 struct dirstat_dir dir
;
1302 struct diff_queue_struct
*q
= &diff_queued_diff
;
1307 dir
.percent
= options
->dirstat_percent
;
1308 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1311 for (i
= 0; i
< q
->nr
; i
++) {
1312 struct diff_filepair
*p
= q
->queue
[i
];
1314 unsigned long copied
, added
, damage
;
1316 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1318 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1319 diff_populate_filespec(p
->one
, 0);
1320 diff_populate_filespec(p
->two
, 0);
1321 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1323 diff_free_filespec_data(p
->one
);
1324 diff_free_filespec_data(p
->two
);
1325 } else if (DIFF_FILE_VALID(p
->one
)) {
1326 diff_populate_filespec(p
->one
, 1);
1328 diff_free_filespec_data(p
->one
);
1329 } else if (DIFF_FILE_VALID(p
->two
)) {
1330 diff_populate_filespec(p
->two
, 1);
1332 added
= p
->two
->size
;
1333 diff_free_filespec_data(p
->two
);
1338 * Original minus copied is the removed material,
1339 * added is the new material. They are both damages
1340 * made to the preimage. In --dirstat-by-file mode, count
1341 * damaged files, not damaged lines. This is done by
1342 * counting only a single damaged line per file.
1344 damage
= (p
->one
->size
- copied
) + added
;
1345 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
) && damage
> 0)
1348 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1349 dir
.files
[dir
.nr
].name
= name
;
1350 dir
.files
[dir
.nr
].changed
= damage
;
1355 /* This can happen even with many files, if everything was renames */
1359 /* Show all directories with more than x% of the changes */
1360 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
1361 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1364 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1367 for (i
= 0; i
< diffstat
->nr
; i
++) {
1368 struct diffstat_file
*f
= diffstat
->files
[i
];
1369 if (f
->name
!= f
->print_name
)
1370 free(f
->print_name
);
1375 free(diffstat
->files
);
1378 struct checkdiff_t
{
1379 const char *filename
;
1381 int conflict_marker_size
;
1382 struct diff_options
*o
;
1387 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
1392 if (len
< marker_size
+ 1)
1394 firstchar
= line
[0];
1395 switch (firstchar
) {
1396 case '=': case '>': case '<': case '|':
1401 for (cnt
= 1; cnt
< marker_size
; cnt
++)
1402 if (line
[cnt
] != firstchar
)
1404 /* line[1] thru line[marker_size-1] are same as firstchar */
1405 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
1410 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1412 struct checkdiff_t
*data
= priv
;
1413 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1414 int marker_size
= data
->conflict_marker_size
;
1415 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1416 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1417 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1420 if (line
[0] == '+') {
1423 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
1425 fprintf(data
->o
->file
,
1426 "%s:%d: leftover conflict marker\n",
1427 data
->filename
, data
->lineno
);
1429 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1432 data
->status
|= bad
;
1433 err
= whitespace_error_string(bad
);
1434 fprintf(data
->o
->file
, "%s:%d: %s.\n",
1435 data
->filename
, data
->lineno
, err
);
1437 emit_line(data
->o
->file
, set
, reset
, line
, 1);
1438 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1439 data
->o
->file
, set
, reset
, ws
);
1440 } else if (line
[0] == ' ') {
1442 } else if (line
[0] == '@') {
1443 char *plus
= strchr(line
, '+');
1445 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1447 die("invalid diff");
1451 static unsigned char *deflate_it(char *data
,
1453 unsigned long *result_size
)
1456 unsigned char *deflated
;
1459 memset(&stream
, 0, sizeof(stream
));
1460 deflateInit(&stream
, zlib_compression_level
);
1461 bound
= deflateBound(&stream
, size
);
1462 deflated
= xmalloc(bound
);
1463 stream
.next_out
= deflated
;
1464 stream
.avail_out
= bound
;
1466 stream
.next_in
= (unsigned char *)data
;
1467 stream
.avail_in
= size
;
1468 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1470 deflateEnd(&stream
);
1471 *result_size
= stream
.total_out
;
1475 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1481 unsigned long orig_size
;
1482 unsigned long delta_size
;
1483 unsigned long deflate_size
;
1484 unsigned long data_size
;
1486 /* We could do deflated delta, or we could do just deflated two,
1487 * whichever is smaller.
1490 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1491 if (one
->size
&& two
->size
) {
1492 delta
= diff_delta(one
->ptr
, one
->size
,
1493 two
->ptr
, two
->size
,
1494 &delta_size
, deflate_size
);
1496 void *to_free
= delta
;
1497 orig_size
= delta_size
;
1498 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1503 if (delta
&& delta_size
< deflate_size
) {
1504 fprintf(file
, "delta %lu\n", orig_size
);
1507 data_size
= delta_size
;
1510 fprintf(file
, "literal %lu\n", two
->size
);
1513 data_size
= deflate_size
;
1516 /* emit data encoded in base85 */
1519 int bytes
= (52 < data_size
) ? 52 : data_size
;
1523 line
[0] = bytes
+ 'A' - 1;
1525 line
[0] = bytes
- 26 + 'a' - 1;
1526 encode_85(line
+ 1, cp
, bytes
);
1527 cp
= (char *) cp
+ bytes
;
1531 fprintf(file
, "\n");
1535 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1537 fprintf(file
, "GIT binary patch\n");
1538 emit_binary_diff_body(file
, one
, two
);
1539 emit_binary_diff_body(file
, two
, one
);
1542 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1545 one
->driver
= userdiff_find_by_path(one
->path
);
1547 one
->driver
= userdiff_find_by_name("default");
1550 int diff_filespec_is_binary(struct diff_filespec
*one
)
1552 if (one
->is_binary
== -1) {
1553 diff_filespec_load_driver(one
);
1554 if (one
->driver
->binary
!= -1)
1555 one
->is_binary
= one
->driver
->binary
;
1557 if (!one
->data
&& DIFF_FILE_VALID(one
))
1558 diff_populate_filespec(one
, 0);
1560 one
->is_binary
= buffer_is_binary(one
->data
,
1562 if (one
->is_binary
== -1)
1566 return one
->is_binary
;
1569 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
1571 diff_filespec_load_driver(one
);
1572 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
1575 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1577 diff_filespec_load_driver(one
);
1578 return one
->driver
->word_regex
;
1581 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
1583 if (!options
->a_prefix
)
1584 options
->a_prefix
= a
;
1585 if (!options
->b_prefix
)
1586 options
->b_prefix
= b
;
1589 static const char *get_textconv(struct diff_filespec
*one
)
1591 if (!DIFF_FILE_VALID(one
))
1593 if (!S_ISREG(one
->mode
))
1595 diff_filespec_load_driver(one
);
1596 return one
->driver
->textconv
;
1599 static void builtin_diff(const char *name_a
,
1601 struct diff_filespec
*one
,
1602 struct diff_filespec
*two
,
1603 const char *xfrm_msg
,
1604 struct diff_options
*o
,
1605 int complete_rewrite
)
1609 char *a_one
, *b_two
;
1610 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1611 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1612 const char *a_prefix
, *b_prefix
;
1613 const char *textconv_one
= NULL
, *textconv_two
= NULL
;
1614 struct strbuf header
= STRBUF_INIT
;
1616 if (DIFF_OPT_TST(o
, SUBMODULE_LOG
) &&
1617 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
1618 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
1619 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1620 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1621 show_submodule_summary(o
->file
, one
? one
->path
: two
->path
,
1622 one
->sha1
, two
->sha1
, two
->dirty_submodule
,
1627 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
1628 textconv_one
= get_textconv(one
);
1629 textconv_two
= get_textconv(two
);
1632 diff_set_mnemonic_prefix(o
, "a/", "b/");
1633 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
1634 a_prefix
= o
->b_prefix
;
1635 b_prefix
= o
->a_prefix
;
1637 a_prefix
= o
->a_prefix
;
1638 b_prefix
= o
->b_prefix
;
1641 /* Never use a non-valid filename anywhere if at all possible */
1642 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
1643 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
1645 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
1646 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
1647 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1648 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1649 strbuf_addf(&header
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1650 if (lbl
[0][0] == '/') {
1652 strbuf_addf(&header
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1654 strbuf_addstr(&header
, xfrm_msg
);
1656 else if (lbl
[1][0] == '/') {
1657 strbuf_addf(&header
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1659 strbuf_addstr(&header
, xfrm_msg
);
1662 if (one
->mode
!= two
->mode
) {
1663 strbuf_addf(&header
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1664 strbuf_addf(&header
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1667 strbuf_addstr(&header
, xfrm_msg
);
1670 * we do not run diff between different kind
1673 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1674 goto free_ab_and_return
;
1675 if (complete_rewrite
&&
1676 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
1677 (textconv_two
|| !diff_filespec_is_binary(two
))) {
1678 fprintf(o
->file
, "%s", header
.buf
);
1679 strbuf_reset(&header
);
1680 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1681 textconv_one
, textconv_two
, o
);
1682 o
->found_changes
= 1;
1683 goto free_ab_and_return
;
1687 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1688 die("unable to read files to diff");
1690 if (!DIFF_OPT_TST(o
, TEXT
) &&
1691 ( (diff_filespec_is_binary(one
) && !textconv_one
) ||
1692 (diff_filespec_is_binary(two
) && !textconv_two
) )) {
1693 /* Quite common confusing case */
1694 if (mf1
.size
== mf2
.size
&&
1695 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1696 goto free_ab_and_return
;
1697 fprintf(o
->file
, "%s", header
.buf
);
1698 strbuf_reset(&header
);
1699 if (DIFF_OPT_TST(o
, BINARY
))
1700 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1702 fprintf(o
->file
, "Binary files %s and %s differ\n",
1704 o
->found_changes
= 1;
1707 /* Crazy xdl interfaces.. */
1708 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1712 struct emit_callback ecbdata
;
1713 const struct userdiff_funcname
*pe
;
1715 if (!DIFF_XDL_TST(o
, WHITESPACE_FLAGS
)) {
1716 fprintf(o
->file
, "%s", header
.buf
);
1717 strbuf_reset(&header
);
1722 mf1
.ptr
= run_textconv(textconv_one
, one
, &size
);
1724 die("unable to read files to diff");
1729 mf2
.ptr
= run_textconv(textconv_two
, two
, &size
);
1731 die("unable to read files to diff");
1735 pe
= diff_funcname_pattern(one
);
1737 pe
= diff_funcname_pattern(two
);
1739 memset(&xpp
, 0, sizeof(xpp
));
1740 memset(&xecfg
, 0, sizeof(xecfg
));
1741 memset(&ecbdata
, 0, sizeof(ecbdata
));
1742 ecbdata
.label_path
= lbl
;
1743 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1744 ecbdata
.found_changesp
= &o
->found_changes
;
1745 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1746 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
1747 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1748 ecbdata
.file
= o
->file
;
1749 ecbdata
.header
= header
.len
? &header
: NULL
;
1750 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1751 xecfg
.ctxlen
= o
->context
;
1752 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
1753 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1755 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
1758 else if (!prefixcmp(diffopts
, "--unified="))
1759 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1760 else if (!prefixcmp(diffopts
, "-u"))
1761 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1762 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
)) {
1763 ecbdata
.diff_words
=
1764 xcalloc(1, sizeof(struct diff_words_data
));
1765 ecbdata
.diff_words
->file
= o
->file
;
1767 o
->word_regex
= userdiff_word_regex(one
);
1769 o
->word_regex
= userdiff_word_regex(two
);
1771 o
->word_regex
= diff_word_regex_cfg
;
1772 if (o
->word_regex
) {
1773 ecbdata
.diff_words
->word_regex
= (regex_t
*)
1774 xmalloc(sizeof(regex_t
));
1775 if (regcomp(ecbdata
.diff_words
->word_regex
,
1777 REG_EXTENDED
| REG_NEWLINE
))
1778 die ("Invalid regular expression: %s",
1782 xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
1783 &xpp
, &xecfg
, &ecb
);
1784 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
))
1785 free_diff_words_data(&ecbdata
);
1790 xdiff_clear_find_func(&xecfg
);
1794 strbuf_release(&header
);
1795 diff_free_filespec_data(one
);
1796 diff_free_filespec_data(two
);
1802 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1803 struct diff_filespec
*one
,
1804 struct diff_filespec
*two
,
1805 struct diffstat_t
*diffstat
,
1806 struct diff_options
*o
,
1807 int complete_rewrite
)
1810 struct diffstat_file
*data
;
1812 data
= diffstat_add(diffstat
, name_a
, name_b
);
1815 data
->is_unmerged
= 1;
1818 if (complete_rewrite
) {
1819 diff_populate_filespec(one
, 0);
1820 diff_populate_filespec(two
, 0);
1821 data
->deleted
= count_lines(one
->data
, one
->size
);
1822 data
->added
= count_lines(two
->data
, two
->size
);
1823 goto free_and_return
;
1825 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1826 die("unable to read files to diff");
1828 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1829 data
->is_binary
= 1;
1830 data
->added
= mf2
.size
;
1831 data
->deleted
= mf1
.size
;
1833 /* Crazy xdl interfaces.. */
1838 memset(&xpp
, 0, sizeof(xpp
));
1839 memset(&xecfg
, 0, sizeof(xecfg
));
1840 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1841 xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
1842 &xpp
, &xecfg
, &ecb
);
1846 diff_free_filespec_data(one
);
1847 diff_free_filespec_data(two
);
1850 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1851 const char *attr_path
,
1852 struct diff_filespec
*one
,
1853 struct diff_filespec
*two
,
1854 struct diff_options
*o
)
1857 struct checkdiff_t data
;
1862 memset(&data
, 0, sizeof(data
));
1863 data
.filename
= name_b
? name_b
: name_a
;
1866 data
.ws_rule
= whitespace_rule(attr_path
);
1867 data
.conflict_marker_size
= ll_merge_marker_size(attr_path
);
1869 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1870 die("unable to read files to diff");
1873 * All the other codepaths check both sides, but not checking
1874 * the "old" side here is deliberate. We are checking the newly
1875 * introduced changes, and as long as the "new" side is text, we
1876 * can and should check what it introduces.
1878 if (diff_filespec_is_binary(two
))
1879 goto free_and_return
;
1881 /* Crazy xdl interfaces.. */
1886 memset(&xpp
, 0, sizeof(xpp
));
1887 memset(&xecfg
, 0, sizeof(xecfg
));
1888 xecfg
.ctxlen
= 1; /* at least one context line */
1889 xpp
.flags
= XDF_NEED_MINIMAL
;
1890 xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
1891 &xpp
, &xecfg
, &ecb
);
1893 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
1894 struct emit_callback ecbdata
;
1897 ecbdata
.ws_rule
= data
.ws_rule
;
1898 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1899 blank_at_eof
= ecbdata
.blank_at_eof_in_preimage
;
1904 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
1905 fprintf(o
->file
, "%s:%d: %s.\n",
1906 data
.filename
, blank_at_eof
, err
);
1907 data
.status
= 1; /* report errors */
1912 diff_free_filespec_data(one
);
1913 diff_free_filespec_data(two
);
1915 DIFF_OPT_SET(o
, CHECK_FAILED
);
1918 struct diff_filespec
*alloc_filespec(const char *path
)
1920 int namelen
= strlen(path
);
1921 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1923 memset(spec
, 0, sizeof(*spec
));
1924 spec
->path
= (char *)(spec
+ 1);
1925 memcpy(spec
->path
, path
, namelen
+1);
1927 spec
->is_binary
= -1;
1931 void free_filespec(struct diff_filespec
*spec
)
1933 if (!--spec
->count
) {
1934 diff_free_filespec_data(spec
);
1939 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1940 unsigned short mode
)
1943 spec
->mode
= canon_mode(mode
);
1944 hashcpy(spec
->sha1
, sha1
);
1945 spec
->sha1_valid
= !is_null_sha1(sha1
);
1950 * Given a name and sha1 pair, if the index tells us the file in
1951 * the work tree has that object contents, return true, so that
1952 * prepare_temp_file() does not have to inflate and extract.
1954 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1956 struct cache_entry
*ce
;
1961 * We do not read the cache ourselves here, because the
1962 * benchmark with my previous version that always reads cache
1963 * shows that it makes things worse for diff-tree comparing
1964 * two linux-2.6 kernel trees in an already checked out work
1965 * tree. This is because most diff-tree comparisons deal with
1966 * only a small number of files, while reading the cache is
1967 * expensive for a large project, and its cost outweighs the
1968 * savings we get by not inflating the object to a temporary
1969 * file. Practically, this code only helps when we are used
1970 * by diff-cache --cached, which does read the cache before
1976 /* We want to avoid the working directory if our caller
1977 * doesn't need the data in a normal file, this system
1978 * is rather slow with its stat/open/mmap/close syscalls,
1979 * and the object is contained in a pack file. The pack
1980 * is probably already open and will be faster to obtain
1981 * the data through than the working directory. Loose
1982 * objects however would tend to be slower as they need
1983 * to be individually opened and inflated.
1985 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
))
1989 pos
= cache_name_pos(name
, len
);
1992 ce
= active_cache
[pos
];
1995 * This is not the sha1 we are looking for, or
1996 * unreusable because it is not a regular file.
1998 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
2002 * If ce is marked as "assume unchanged", there is no
2003 * guarantee that work tree matches what we are looking for.
2005 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2009 * If ce matches the file in the work tree, we can reuse it.
2011 if (ce_uptodate(ce
) ||
2012 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
2018 static int populate_from_stdin(struct diff_filespec
*s
)
2020 struct strbuf buf
= STRBUF_INIT
;
2023 if (strbuf_read(&buf
, 0, 0) < 0)
2024 return error("error while reading from stdin %s",
2027 s
->should_munmap
= 0;
2028 s
->data
= strbuf_detach(&buf
, &size
);
2034 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
2037 char *data
= xmalloc(100), *dirty
= "";
2039 /* Are we looking at the work tree? */
2040 if (s
->dirty_submodule
)
2043 len
= snprintf(data
, 100,
2044 "Subproject commit %s%s\n", sha1_to_hex(s
->sha1
), dirty
);
2056 * While doing rename detection and pickaxe operation, we may need to
2057 * grab the data for the blob (or file) for our own in-core comparison.
2058 * diff_filespec has data and size fields for this purpose.
2060 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
2063 if (!DIFF_FILE_VALID(s
))
2064 die("internal error: asking to populate invalid file.");
2065 if (S_ISDIR(s
->mode
))
2071 if (size_only
&& 0 < s
->size
)
2074 if (S_ISGITLINK(s
->mode
))
2075 return diff_populate_gitlink(s
, size_only
);
2077 if (!s
->sha1_valid
||
2078 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
2079 struct strbuf buf
= STRBUF_INIT
;
2083 if (!strcmp(s
->path
, "-"))
2084 return populate_from_stdin(s
);
2086 if (lstat(s
->path
, &st
) < 0) {
2087 if (errno
== ENOENT
) {
2091 s
->data
= (char *)"";
2096 s
->size
= xsize_t(st
.st_size
);
2099 if (S_ISLNK(st
.st_mode
)) {
2100 struct strbuf sb
= STRBUF_INIT
;
2102 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
2105 s
->data
= strbuf_detach(&sb
, NULL
);
2111 fd
= open(s
->path
, O_RDONLY
);
2114 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2116 s
->should_munmap
= 1;
2119 * Convert from working tree format to canonical git format
2121 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
2123 munmap(s
->data
, s
->size
);
2124 s
->should_munmap
= 0;
2125 s
->data
= strbuf_detach(&buf
, &size
);
2131 enum object_type type
;
2133 type
= sha1_object_info(s
->sha1
, &s
->size
);
2135 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
2142 void diff_free_filespec_blob(struct diff_filespec
*s
)
2146 else if (s
->should_munmap
)
2147 munmap(s
->data
, s
->size
);
2149 if (s
->should_free
|| s
->should_munmap
) {
2150 s
->should_free
= s
->should_munmap
= 0;
2155 void diff_free_filespec_data(struct diff_filespec
*s
)
2157 diff_free_filespec_blob(s
);
2162 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
2165 const unsigned char *sha1
,
2169 struct strbuf buf
= STRBUF_INIT
;
2170 struct strbuf
template = STRBUF_INIT
;
2171 char *path_dup
= xstrdup(path
);
2172 const char *base
= basename(path_dup
);
2174 /* Generate "XXXXXX_basename.ext" */
2175 strbuf_addstr(&template, "XXXXXX_");
2176 strbuf_addstr(&template, base
);
2178 fd
= git_mkstemps(temp
->tmp_path
, PATH_MAX
, template.buf
,
2181 die_errno("unable to create temp-file");
2182 if (convert_to_working_tree(path
,
2183 (const char *)blob
, (size_t)size
, &buf
)) {
2187 if (write_in_full(fd
, blob
, size
) != size
)
2188 die_errno("unable to write temp-file");
2190 temp
->name
= temp
->tmp_path
;
2191 strcpy(temp
->hex
, sha1_to_hex(sha1
));
2193 sprintf(temp
->mode
, "%06o", mode
);
2194 strbuf_release(&buf
);
2195 strbuf_release(&template);
2199 static struct diff_tempfile
*prepare_temp_file(const char *name
,
2200 struct diff_filespec
*one
)
2202 struct diff_tempfile
*temp
= claim_diff_tempfile();
2204 if (!DIFF_FILE_VALID(one
)) {
2206 /* A '-' entry produces this for file-2, and
2207 * a '+' entry produces this for file-1.
2209 temp
->name
= "/dev/null";
2210 strcpy(temp
->hex
, ".");
2211 strcpy(temp
->mode
, ".");
2215 if (!remove_tempfile_installed
) {
2216 atexit(remove_tempfile
);
2217 sigchain_push_common(remove_tempfile_on_signal
);
2218 remove_tempfile_installed
= 1;
2221 if (!one
->sha1_valid
||
2222 reuse_worktree_file(name
, one
->sha1
, 1)) {
2224 if (lstat(name
, &st
) < 0) {
2225 if (errno
== ENOENT
)
2226 goto not_a_valid_file
;
2227 die_errno("stat(%s)", name
);
2229 if (S_ISLNK(st
.st_mode
)) {
2230 struct strbuf sb
= STRBUF_INIT
;
2231 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
2232 die_errno("readlink(%s)", name
);
2233 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
2235 one
->sha1
: null_sha1
),
2237 one
->mode
: S_IFLNK
));
2238 strbuf_release(&sb
);
2241 /* we can borrow from the file in the work tree */
2243 if (!one
->sha1_valid
)
2244 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
2246 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
2247 /* Even though we may sometimes borrow the
2248 * contents from the work tree, we always want
2249 * one->mode. mode is trustworthy even when
2250 * !(one->sha1_valid), as long as
2251 * DIFF_FILE_VALID(one).
2253 sprintf(temp
->mode
, "%06o", one
->mode
);
2258 if (diff_populate_filespec(one
, 0))
2259 die("cannot read data blob for %s", one
->path
);
2260 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
2261 one
->sha1
, one
->mode
);
2266 /* An external diff command takes:
2268 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2269 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2272 static void run_external_diff(const char *pgm
,
2275 struct diff_filespec
*one
,
2276 struct diff_filespec
*two
,
2277 const char *xfrm_msg
,
2278 int complete_rewrite
)
2280 const char *spawn_arg
[10];
2282 const char **arg
= &spawn_arg
[0];
2285 struct diff_tempfile
*temp_one
, *temp_two
;
2286 const char *othername
= (other
? other
: name
);
2287 temp_one
= prepare_temp_file(name
, one
);
2288 temp_two
= prepare_temp_file(othername
, two
);
2291 *arg
++ = temp_one
->name
;
2292 *arg
++ = temp_one
->hex
;
2293 *arg
++ = temp_one
->mode
;
2294 *arg
++ = temp_two
->name
;
2295 *arg
++ = temp_two
->hex
;
2296 *arg
++ = temp_two
->mode
;
2307 retval
= run_command_v_opt(spawn_arg
, RUN_USING_SHELL
);
2310 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2315 static int similarity_index(struct diff_filepair
*p
)
2317 return p
->score
* 100 / MAX_SCORE
;
2320 static void fill_metainfo(struct strbuf
*msg
,
2323 struct diff_filespec
*one
,
2324 struct diff_filespec
*two
,
2325 struct diff_options
*o
,
2326 struct diff_filepair
*p
,
2329 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
2330 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
2332 strbuf_init(msg
, PATH_MAX
* 2 + 300);
2333 switch (p
->status
) {
2334 case DIFF_STATUS_COPIED
:
2335 strbuf_addf(msg
, "%ssimilarity index %d%%",
2336 set
, similarity_index(p
));
2337 strbuf_addf(msg
, "%s\n%scopy from ", reset
, set
);
2338 quote_c_style(name
, msg
, NULL
, 0);
2339 strbuf_addf(msg
, "%s\n%scopy to ", reset
, set
);
2340 quote_c_style(other
, msg
, NULL
, 0);
2341 strbuf_addf(msg
, "%s\n", reset
);
2343 case DIFF_STATUS_RENAMED
:
2344 strbuf_addf(msg
, "%ssimilarity index %d%%",
2345 set
, similarity_index(p
));
2346 strbuf_addf(msg
, "%s\n%srename from ", reset
, set
);
2347 quote_c_style(name
, msg
, NULL
, 0);
2348 strbuf_addf(msg
, "%s\n%srename to ", reset
, set
);
2349 quote_c_style(other
, msg
, NULL
, 0);
2350 strbuf_addf(msg
, "%s\n", reset
);
2352 case DIFF_STATUS_MODIFIED
:
2354 strbuf_addf(msg
, "%sdissimilarity index %d%%%s\n",
2355 set
, similarity_index(p
), reset
);
2363 if (one
&& two
&& hashcmp(one
->sha1
, two
->sha1
)) {
2364 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2366 if (DIFF_OPT_TST(o
, BINARY
)) {
2368 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2369 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2372 strbuf_addf(msg
, "%sindex %.*s..%.*s", set
,
2373 abbrev
, sha1_to_hex(one
->sha1
),
2374 abbrev
, sha1_to_hex(two
->sha1
));
2375 if (one
->mode
== two
->mode
)
2376 strbuf_addf(msg
, " %06o", one
->mode
);
2377 strbuf_addf(msg
, "%s\n", reset
);
2381 static void run_diff_cmd(const char *pgm
,
2384 const char *attr_path
,
2385 struct diff_filespec
*one
,
2386 struct diff_filespec
*two
,
2388 struct diff_options
*o
,
2389 struct diff_filepair
*p
)
2391 const char *xfrm_msg
= NULL
;
2392 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
2394 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2397 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
2398 if (drv
&& drv
->external
)
2399 pgm
= drv
->external
;
2404 * don't use colors when the header is intended for an
2405 * external diff driver
2407 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
2408 DIFF_OPT_TST(o
, COLOR_DIFF
) && !pgm
);
2409 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
2413 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2418 builtin_diff(name
, other
? other
: name
,
2419 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2421 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2424 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2426 if (DIFF_FILE_VALID(one
)) {
2427 if (!one
->sha1_valid
) {
2429 if (!strcmp(one
->path
, "-")) {
2430 hashcpy(one
->sha1
, null_sha1
);
2433 if (lstat(one
->path
, &st
) < 0)
2434 die_errno("stat '%s'", one
->path
);
2435 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2436 die("cannot hash %s", one
->path
);
2443 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2445 /* Strip the prefix but do not molest /dev/null and absolute paths */
2446 if (*namep
&& **namep
!= '/')
2447 *namep
+= prefix_length
;
2448 if (*otherp
&& **otherp
!= '/')
2449 *otherp
+= prefix_length
;
2452 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2454 const char *pgm
= external_diff();
2456 struct diff_filespec
*one
= p
->one
;
2457 struct diff_filespec
*two
= p
->two
;
2460 const char *attr_path
;
2462 name
= p
->one
->path
;
2463 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2465 if (o
->prefix_length
)
2466 strip_prefix(o
->prefix_length
, &name
, &other
);
2468 if (DIFF_PAIR_UNMERGED(p
)) {
2469 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2470 NULL
, NULL
, NULL
, o
, p
);
2474 diff_fill_sha1_info(one
);
2475 diff_fill_sha1_info(two
);
2478 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2479 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2481 * a filepair that changes between file and symlink
2482 * needs to be split into deletion and creation.
2484 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2485 run_diff_cmd(NULL
, name
, other
, attr_path
,
2486 one
, null
, &msg
, o
, p
);
2488 strbuf_release(&msg
);
2490 null
= alloc_filespec(one
->path
);
2491 run_diff_cmd(NULL
, name
, other
, attr_path
,
2492 null
, two
, &msg
, o
, p
);
2496 run_diff_cmd(pgm
, name
, other
, attr_path
,
2497 one
, two
, &msg
, o
, p
);
2499 strbuf_release(&msg
);
2502 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2503 struct diffstat_t
*diffstat
)
2507 int complete_rewrite
= 0;
2509 if (DIFF_PAIR_UNMERGED(p
)) {
2511 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2515 name
= p
->one
->path
;
2516 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2518 if (o
->prefix_length
)
2519 strip_prefix(o
->prefix_length
, &name
, &other
);
2521 diff_fill_sha1_info(p
->one
);
2522 diff_fill_sha1_info(p
->two
);
2524 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2525 complete_rewrite
= 1;
2526 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2529 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2533 const char *attr_path
;
2535 if (DIFF_PAIR_UNMERGED(p
)) {
2540 name
= p
->one
->path
;
2541 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2542 attr_path
= other
? other
: name
;
2544 if (o
->prefix_length
)
2545 strip_prefix(o
->prefix_length
, &name
, &other
);
2547 diff_fill_sha1_info(p
->one
);
2548 diff_fill_sha1_info(p
->two
);
2550 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2553 void diff_setup(struct diff_options
*options
)
2555 memset(options
, 0, sizeof(*options
));
2557 options
->file
= stdout
;
2559 options
->line_termination
= '\n';
2560 options
->break_opt
= -1;
2561 options
->rename_limit
= -1;
2562 options
->dirstat_percent
= 3;
2563 options
->context
= 3;
2565 options
->change
= diff_change
;
2566 options
->add_remove
= diff_addremove
;
2567 if (diff_use_color_default
> 0)
2568 DIFF_OPT_SET(options
, COLOR_DIFF
);
2569 options
->detect_rename
= diff_detect_rename_default
;
2571 if (!diff_mnemonic_prefix
) {
2572 options
->a_prefix
= "a/";
2573 options
->b_prefix
= "b/";
2577 int diff_setup_done(struct diff_options
*options
)
2581 if (options
->output_format
& DIFF_FORMAT_NAME
)
2583 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2585 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2587 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2590 die("--name-only, --name-status, --check and -s are mutually exclusive");
2593 * Most of the time we can say "there are changes"
2594 * only by checking if there are changed paths, but
2595 * --ignore-whitespace* options force us to look
2599 if (DIFF_XDL_TST(options
, IGNORE_WHITESPACE
) ||
2600 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_CHANGE
) ||
2601 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_AT_EOL
))
2602 DIFF_OPT_SET(options
, DIFF_FROM_CONTENTS
);
2604 DIFF_OPT_CLR(options
, DIFF_FROM_CONTENTS
);
2606 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2607 options
->detect_rename
= DIFF_DETECT_COPY
;
2609 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2610 options
->prefix
= NULL
;
2611 if (options
->prefix
)
2612 options
->prefix_length
= strlen(options
->prefix
);
2614 options
->prefix_length
= 0;
2616 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2617 DIFF_FORMAT_NAME_STATUS
|
2618 DIFF_FORMAT_CHECKDIFF
|
2619 DIFF_FORMAT_NO_OUTPUT
))
2620 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2621 DIFF_FORMAT_NUMSTAT
|
2622 DIFF_FORMAT_DIFFSTAT
|
2623 DIFF_FORMAT_SHORTSTAT
|
2624 DIFF_FORMAT_DIRSTAT
|
2625 DIFF_FORMAT_SUMMARY
|
2629 * These cases always need recursive; we do not drop caller-supplied
2630 * recursive bits for other formats here.
2632 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2633 DIFF_FORMAT_NUMSTAT
|
2634 DIFF_FORMAT_DIFFSTAT
|
2635 DIFF_FORMAT_SHORTSTAT
|
2636 DIFF_FORMAT_DIRSTAT
|
2637 DIFF_FORMAT_SUMMARY
|
2638 DIFF_FORMAT_CHECKDIFF
))
2639 DIFF_OPT_SET(options
, RECURSIVE
);
2641 * Also pickaxe would not work very well if you do not say recursive
2643 if (options
->pickaxe
)
2644 DIFF_OPT_SET(options
, RECURSIVE
);
2646 * When patches are generated, submodules diffed against the work tree
2647 * must be checked for dirtiness too so it can be shown in the output
2649 if (options
->output_format
& DIFF_FORMAT_PATCH
)
2650 DIFF_OPT_SET(options
, DIRTY_SUBMODULES
);
2652 if (options
->detect_rename
&& options
->rename_limit
< 0)
2653 options
->rename_limit
= diff_rename_limit_default
;
2654 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2656 /* read-cache does not die even when it fails
2657 * so it is safe for us to do this here. Also
2658 * it does not smudge active_cache or active_nr
2659 * when it fails, so we do not have to worry about
2660 * cleaning it up ourselves either.
2664 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2665 options
->abbrev
= 40; /* full */
2668 * It does not make sense to show the first hit we happened
2669 * to have found. It does not make sense not to return with
2670 * exit code in such a case either.
2672 if (DIFF_OPT_TST(options
, QUICK
)) {
2673 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2674 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2680 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2690 if (c
== arg_short
) {
2694 if (val
&& isdigit(c
)) {
2696 int n
= strtoul(arg
, &end
, 10);
2707 eq
= strchr(arg
, '=');
2712 if (!len
|| strncmp(arg
, arg_long
, len
))
2717 if (!isdigit(*++eq
))
2719 n
= strtoul(eq
, &end
, 10);
2727 static int diff_scoreopt_parse(const char *opt
);
2729 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2731 const char *arg
= av
[0];
2733 /* Output format options */
2734 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2735 options
->output_format
|= DIFF_FORMAT_PATCH
;
2736 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2737 options
->output_format
|= DIFF_FORMAT_PATCH
;
2738 else if (!strcmp(arg
, "--raw"))
2739 options
->output_format
|= DIFF_FORMAT_RAW
;
2740 else if (!strcmp(arg
, "--patch-with-raw"))
2741 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2742 else if (!strcmp(arg
, "--numstat"))
2743 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2744 else if (!strcmp(arg
, "--shortstat"))
2745 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2746 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2747 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2748 else if (!strcmp(arg
, "--cumulative")) {
2749 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2750 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
2751 } else if (opt_arg(arg
, 0, "dirstat-by-file",
2752 &options
->dirstat_percent
)) {
2753 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2754 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
2756 else if (!strcmp(arg
, "--check"))
2757 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2758 else if (!strcmp(arg
, "--summary"))
2759 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2760 else if (!strcmp(arg
, "--patch-with-stat"))
2761 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2762 else if (!strcmp(arg
, "--name-only"))
2763 options
->output_format
|= DIFF_FORMAT_NAME
;
2764 else if (!strcmp(arg
, "--name-status"))
2765 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2766 else if (!strcmp(arg
, "-s"))
2767 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2768 else if (!prefixcmp(arg
, "--stat")) {
2770 int width
= options
->stat_width
;
2771 int name_width
= options
->stat_name_width
;
2777 if (!prefixcmp(arg
, "-width="))
2778 width
= strtoul(arg
+ 7, &end
, 10);
2779 else if (!prefixcmp(arg
, "-name-width="))
2780 name_width
= strtoul(arg
+ 12, &end
, 10);
2783 width
= strtoul(arg
+1, &end
, 10);
2785 name_width
= strtoul(end
+1, &end
, 10);
2788 /* Important! This checks all the error cases! */
2791 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2792 options
->stat_name_width
= name_width
;
2793 options
->stat_width
= width
;
2796 /* renames options */
2797 else if (!prefixcmp(arg
, "-B")) {
2798 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2801 else if (!prefixcmp(arg
, "-M")) {
2802 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2804 options
->detect_rename
= DIFF_DETECT_RENAME
;
2806 else if (!prefixcmp(arg
, "-C")) {
2807 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2808 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2809 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2811 options
->detect_rename
= DIFF_DETECT_COPY
;
2813 else if (!strcmp(arg
, "--no-renames"))
2814 options
->detect_rename
= 0;
2815 else if (!strcmp(arg
, "--relative"))
2816 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2817 else if (!prefixcmp(arg
, "--relative=")) {
2818 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2819 options
->prefix
= arg
+ 11;
2823 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2824 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
2825 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2826 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
2827 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2828 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
2829 else if (!strcmp(arg
, "--patience"))
2830 DIFF_XDL_SET(options
, PATIENCE_DIFF
);
2833 else if (!strcmp(arg
, "--binary")) {
2834 options
->output_format
|= DIFF_FORMAT_PATCH
;
2835 DIFF_OPT_SET(options
, BINARY
);
2837 else if (!strcmp(arg
, "--full-index"))
2838 DIFF_OPT_SET(options
, FULL_INDEX
);
2839 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2840 DIFF_OPT_SET(options
, TEXT
);
2841 else if (!strcmp(arg
, "-R"))
2842 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2843 else if (!strcmp(arg
, "--find-copies-harder"))
2844 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2845 else if (!strcmp(arg
, "--follow"))
2846 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2847 else if (!strcmp(arg
, "--color"))
2848 DIFF_OPT_SET(options
, COLOR_DIFF
);
2849 else if (!prefixcmp(arg
, "--color=")) {
2850 int value
= git_config_colorbool(NULL
, arg
+8, -1);
2852 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2854 DIFF_OPT_SET(options
, COLOR_DIFF
);
2856 return error("option `color' expects \"always\", \"auto\", or \"never\"");
2858 else if (!strcmp(arg
, "--no-color"))
2859 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2860 else if (!strcmp(arg
, "--color-words")) {
2861 DIFF_OPT_SET(options
, COLOR_DIFF
);
2862 DIFF_OPT_SET(options
, COLOR_DIFF_WORDS
);
2864 else if (!prefixcmp(arg
, "--color-words=")) {
2865 DIFF_OPT_SET(options
, COLOR_DIFF
);
2866 DIFF_OPT_SET(options
, COLOR_DIFF_WORDS
);
2867 options
->word_regex
= arg
+ 14;
2869 else if (!strcmp(arg
, "--exit-code"))
2870 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2871 else if (!strcmp(arg
, "--quiet"))
2872 DIFF_OPT_SET(options
, QUICK
);
2873 else if (!strcmp(arg
, "--ext-diff"))
2874 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2875 else if (!strcmp(arg
, "--no-ext-diff"))
2876 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2877 else if (!strcmp(arg
, "--textconv"))
2878 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
2879 else if (!strcmp(arg
, "--no-textconv"))
2880 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
2881 else if (!strcmp(arg
, "--ignore-submodules"))
2882 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
2883 else if (!strcmp(arg
, "--submodule"))
2884 DIFF_OPT_SET(options
, SUBMODULE_LOG
);
2885 else if (!prefixcmp(arg
, "--submodule=")) {
2886 if (!strcmp(arg
+ 12, "log"))
2887 DIFF_OPT_SET(options
, SUBMODULE_LOG
);
2891 else if (!strcmp(arg
, "-z"))
2892 options
->line_termination
= 0;
2893 else if (!prefixcmp(arg
, "-l"))
2894 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2895 else if (!prefixcmp(arg
, "-S"))
2896 options
->pickaxe
= arg
+ 2;
2897 else if (!strcmp(arg
, "--pickaxe-all"))
2898 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2899 else if (!strcmp(arg
, "--pickaxe-regex"))
2900 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2901 else if (!prefixcmp(arg
, "-O"))
2902 options
->orderfile
= arg
+ 2;
2903 else if (!prefixcmp(arg
, "--diff-filter="))
2904 options
->filter
= arg
+ 14;
2905 else if (!strcmp(arg
, "--abbrev"))
2906 options
->abbrev
= DEFAULT_ABBREV
;
2907 else if (!prefixcmp(arg
, "--abbrev=")) {
2908 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2909 if (options
->abbrev
< MINIMUM_ABBREV
)
2910 options
->abbrev
= MINIMUM_ABBREV
;
2911 else if (40 < options
->abbrev
)
2912 options
->abbrev
= 40;
2914 else if (!prefixcmp(arg
, "--src-prefix="))
2915 options
->a_prefix
= arg
+ 13;
2916 else if (!prefixcmp(arg
, "--dst-prefix="))
2917 options
->b_prefix
= arg
+ 13;
2918 else if (!strcmp(arg
, "--no-prefix"))
2919 options
->a_prefix
= options
->b_prefix
= "";
2920 else if (opt_arg(arg
, '\0', "inter-hunk-context",
2921 &options
->interhunkcontext
))
2923 else if (!prefixcmp(arg
, "--output=")) {
2924 options
->file
= fopen(arg
+ strlen("--output="), "w");
2926 die_errno("Could not open '%s'", arg
+ strlen("--output="));
2927 options
->close_file
= 1;
2933 static int parse_num(const char **cp_p
)
2935 unsigned long num
, scale
;
2937 const char *cp
= *cp_p
;
2944 if ( !dot
&& ch
== '.' ) {
2947 } else if ( ch
== '%' ) {
2948 scale
= dot
? scale
*100 : 100;
2949 cp
++; /* % is always at the end */
2951 } else if ( ch
>= '0' && ch
<= '9' ) {
2952 if ( scale
< 100000 ) {
2954 num
= (num
*10) + (ch
-'0');
2963 /* user says num divided by scale and we say internally that
2964 * is MAX_SCORE * num / scale.
2966 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2969 static int diff_scoreopt_parse(const char *opt
)
2971 int opt1
, opt2
, cmd
;
2976 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2977 return -1; /* that is not a -M, -C nor -B option */
2979 opt1
= parse_num(&opt
);
2985 else if (*opt
!= '/')
2986 return -1; /* we expect -B80/99 or -B80 */
2989 opt2
= parse_num(&opt
);
2994 return opt1
| (opt2
<< 16);
2997 struct diff_queue_struct diff_queued_diff
;
2999 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
3001 if (queue
->alloc
<= queue
->nr
) {
3002 queue
->alloc
= alloc_nr(queue
->alloc
);
3003 queue
->queue
= xrealloc(queue
->queue
,
3004 sizeof(dp
) * queue
->alloc
);
3006 queue
->queue
[queue
->nr
++] = dp
;
3009 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
3010 struct diff_filespec
*one
,
3011 struct diff_filespec
*two
)
3013 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
3021 void diff_free_filepair(struct diff_filepair
*p
)
3023 free_filespec(p
->one
);
3024 free_filespec(p
->two
);
3028 /* This is different from find_unique_abbrev() in that
3029 * it stuffs the result with dots for alignment.
3031 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
3036 return sha1_to_hex(sha1
);
3038 abbrev
= find_unique_abbrev(sha1
, len
);
3039 abblen
= strlen(abbrev
);
3041 static char hex
[41];
3042 if (len
< abblen
&& abblen
<= len
+ 2)
3043 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
3045 sprintf(hex
, "%s...", abbrev
);
3048 return sha1_to_hex(sha1
);
3051 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
3053 int line_termination
= opt
->line_termination
;
3054 int inter_name_termination
= line_termination
? '\t' : '\0';
3056 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
3057 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
3058 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
3059 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
3062 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
3063 inter_name_termination
);
3065 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
3068 if (p
->status
== DIFF_STATUS_COPIED
||
3069 p
->status
== DIFF_STATUS_RENAMED
) {
3070 const char *name_a
, *name_b
;
3071 name_a
= p
->one
->path
;
3072 name_b
= p
->two
->path
;
3073 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3074 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
3075 write_name_quoted(name_b
, opt
->file
, line_termination
);
3077 const char *name_a
, *name_b
;
3078 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
3080 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3081 write_name_quoted(name_a
, opt
->file
, line_termination
);
3085 int diff_unmodified_pair(struct diff_filepair
*p
)
3087 /* This function is written stricter than necessary to support
3088 * the currently implemented transformers, but the idea is to
3089 * let transformers to produce diff_filepairs any way they want,
3090 * and filter and clean them up here before producing the output.
3092 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
3094 if (DIFF_PAIR_UNMERGED(p
))
3095 return 0; /* unmerged is interesting */
3097 /* deletion, addition, mode or type change
3098 * and rename are all interesting.
3100 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
3101 DIFF_PAIR_MODE_CHANGED(p
) ||
3102 strcmp(one
->path
, two
->path
))
3105 /* both are valid and point at the same path. that is, we are
3106 * dealing with a change.
3108 if (one
->sha1_valid
&& two
->sha1_valid
&&
3109 !hashcmp(one
->sha1
, two
->sha1
) &&
3110 !one
->dirty_submodule
&& !two
->dirty_submodule
)
3111 return 1; /* no change */
3112 if (!one
->sha1_valid
&& !two
->sha1_valid
)
3113 return 1; /* both look at the same file on the filesystem. */
3117 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
3119 if (diff_unmodified_pair(p
))
3122 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3123 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3124 return; /* no tree diffs in patch format */
3129 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
3130 struct diffstat_t
*diffstat
)
3132 if (diff_unmodified_pair(p
))
3135 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3136 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3137 return; /* no tree diffs in patch format */
3139 run_diffstat(p
, o
, diffstat
);
3142 static void diff_flush_checkdiff(struct diff_filepair
*p
,
3143 struct diff_options
*o
)
3145 if (diff_unmodified_pair(p
))
3148 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3149 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3150 return; /* no tree diffs in patch format */
3152 run_checkdiff(p
, o
);
3155 int diff_queue_is_empty(void)
3157 struct diff_queue_struct
*q
= &diff_queued_diff
;
3159 for (i
= 0; i
< q
->nr
; i
++)
3160 if (!diff_unmodified_pair(q
->queue
[i
]))
3166 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
3168 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
3171 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
3173 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
3174 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
3176 s
->size
, s
->xfrm_flags
);
3179 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
3181 diff_debug_filespec(p
->one
, i
, "one");
3182 diff_debug_filespec(p
->two
, i
, "two");
3183 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
3184 p
->score
, p
->status
? p
->status
: '?',
3185 p
->one
->rename_used
, p
->broken_pair
);
3188 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
3192 fprintf(stderr
, "%s\n", msg
);
3193 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
3194 for (i
= 0; i
< q
->nr
; i
++) {
3195 struct diff_filepair
*p
= q
->queue
[i
];
3196 diff_debug_filepair(p
, i
);
3201 static void diff_resolve_rename_copy(void)
3204 struct diff_filepair
*p
;
3205 struct diff_queue_struct
*q
= &diff_queued_diff
;
3207 diff_debug_queue("resolve-rename-copy", q
);
3209 for (i
= 0; i
< q
->nr
; i
++) {
3211 p
->status
= 0; /* undecided */
3212 if (DIFF_PAIR_UNMERGED(p
))
3213 p
->status
= DIFF_STATUS_UNMERGED
;
3214 else if (!DIFF_FILE_VALID(p
->one
))
3215 p
->status
= DIFF_STATUS_ADDED
;
3216 else if (!DIFF_FILE_VALID(p
->two
))
3217 p
->status
= DIFF_STATUS_DELETED
;
3218 else if (DIFF_PAIR_TYPE_CHANGED(p
))
3219 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
3221 /* from this point on, we are dealing with a pair
3222 * whose both sides are valid and of the same type, i.e.
3223 * either in-place edit or rename/copy edit.
3225 else if (DIFF_PAIR_RENAME(p
)) {
3227 * A rename might have re-connected a broken
3228 * pair up, causing the pathnames to be the
3229 * same again. If so, that's not a rename at
3230 * all, just a modification..
3232 * Otherwise, see if this source was used for
3233 * multiple renames, in which case we decrement
3234 * the count, and call it a copy.
3236 if (!strcmp(p
->one
->path
, p
->two
->path
))
3237 p
->status
= DIFF_STATUS_MODIFIED
;
3238 else if (--p
->one
->rename_used
> 0)
3239 p
->status
= DIFF_STATUS_COPIED
;
3241 p
->status
= DIFF_STATUS_RENAMED
;
3243 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
3244 p
->one
->mode
!= p
->two
->mode
||
3245 p
->one
->dirty_submodule
||
3246 p
->two
->dirty_submodule
||
3247 is_null_sha1(p
->one
->sha1
))
3248 p
->status
= DIFF_STATUS_MODIFIED
;
3250 /* This is a "no-change" entry and should not
3251 * happen anymore, but prepare for broken callers.
3253 error("feeding unmodified %s to diffcore",
3255 p
->status
= DIFF_STATUS_UNKNOWN
;
3258 diff_debug_queue("resolve-rename-copy done", q
);
3261 static int check_pair_status(struct diff_filepair
*p
)
3263 switch (p
->status
) {
3264 case DIFF_STATUS_UNKNOWN
:
3267 die("internal error in diff-resolve-rename-copy");
3273 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
3275 int fmt
= opt
->output_format
;
3277 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
3278 diff_flush_checkdiff(p
, opt
);
3279 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
3280 diff_flush_raw(p
, opt
);
3281 else if (fmt
& DIFF_FORMAT_NAME
) {
3282 const char *name_a
, *name_b
;
3283 name_a
= p
->two
->path
;
3285 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3286 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
3290 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
3293 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
3295 fprintf(file
, " %s ", newdelete
);
3296 write_name_quoted(fs
->path
, file
, '\n');
3300 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
3302 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
3303 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
3304 show_name
? ' ' : '\n');
3306 write_name_quoted(p
->two
->path
, file
, '\n');
3311 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
3313 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
3315 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
3317 show_mode_change(file
, p
, 0);
3320 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
3323 case DIFF_STATUS_DELETED
:
3324 show_file_mode_name(file
, "delete", p
->one
);
3326 case DIFF_STATUS_ADDED
:
3327 show_file_mode_name(file
, "create", p
->two
);
3329 case DIFF_STATUS_COPIED
:
3330 show_rename_copy(file
, "copy", p
);
3332 case DIFF_STATUS_RENAMED
:
3333 show_rename_copy(file
, "rename", p
);
3337 fputs(" rewrite ", file
);
3338 write_name_quoted(p
->two
->path
, file
, ' ');
3339 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3341 show_mode_change(file
, p
, !p
->score
);
3351 static int remove_space(char *line
, int len
)
3357 for (i
= 0; i
< len
; i
++)
3358 if (!isspace((c
= line
[i
])))
3364 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3366 struct patch_id_t
*data
= priv
;
3369 /* Ignore line numbers when computing the SHA1 of the patch */
3370 if (!prefixcmp(line
, "@@ -"))
3373 new_len
= remove_space(line
, len
);
3375 git_SHA1_Update(data
->ctx
, line
, new_len
);
3376 data
->patchlen
+= new_len
;
3379 /* returns 0 upon success, and writes result into sha1 */
3380 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3382 struct diff_queue_struct
*q
= &diff_queued_diff
;
3385 struct patch_id_t data
;
3386 char buffer
[PATH_MAX
* 4 + 20];
3388 git_SHA1_Init(&ctx
);
3389 memset(&data
, 0, sizeof(struct patch_id_t
));
3392 for (i
= 0; i
< q
->nr
; i
++) {
3397 struct diff_filepair
*p
= q
->queue
[i
];
3400 memset(&xpp
, 0, sizeof(xpp
));
3401 memset(&xecfg
, 0, sizeof(xecfg
));
3403 return error("internal diff status error");
3404 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3406 if (diff_unmodified_pair(p
))
3408 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3409 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3411 if (DIFF_PAIR_UNMERGED(p
))
3414 diff_fill_sha1_info(p
->one
);
3415 diff_fill_sha1_info(p
->two
);
3416 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3417 fill_mmfile(&mf2
, p
->two
) < 0)
3418 return error("unable to read files to diff");
3420 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3421 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3422 if (p
->one
->mode
== 0)
3423 len1
= snprintf(buffer
, sizeof(buffer
),
3424 "diff--gita/%.*sb/%.*s"
3431 len2
, p
->two
->path
);
3432 else if (p
->two
->mode
== 0)
3433 len1
= snprintf(buffer
, sizeof(buffer
),
3434 "diff--gita/%.*sb/%.*s"
3435 "deletedfilemode%06o"
3441 len1
, p
->one
->path
);
3443 len1
= snprintf(buffer
, sizeof(buffer
),
3444 "diff--gita/%.*sb/%.*s"
3450 len2
, p
->two
->path
);
3451 git_SHA1_Update(&ctx
, buffer
, len1
);
3453 xpp
.flags
= XDF_NEED_MINIMAL
;
3455 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3456 xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
3457 &xpp
, &xecfg
, &ecb
);
3460 git_SHA1_Final(sha1
, &ctx
);
3464 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3466 struct diff_queue_struct
*q
= &diff_queued_diff
;
3468 int result
= diff_get_patch_id(options
, sha1
);
3470 for (i
= 0; i
< q
->nr
; i
++)
3471 diff_free_filepair(q
->queue
[i
]);
3475 q
->nr
= q
->alloc
= 0;
3480 static int is_summary_empty(const struct diff_queue_struct
*q
)
3484 for (i
= 0; i
< q
->nr
; i
++) {
3485 const struct diff_filepair
*p
= q
->queue
[i
];
3487 switch (p
->status
) {
3488 case DIFF_STATUS_DELETED
:
3489 case DIFF_STATUS_ADDED
:
3490 case DIFF_STATUS_COPIED
:
3491 case DIFF_STATUS_RENAMED
:
3496 if (p
->one
->mode
&& p
->two
->mode
&&
3497 p
->one
->mode
!= p
->two
->mode
)
3505 void diff_flush(struct diff_options
*options
)
3507 struct diff_queue_struct
*q
= &diff_queued_diff
;
3508 int i
, output_format
= options
->output_format
;
3512 * Order: raw, stat, summary, patch
3513 * or: name/name-status/checkdiff (other bits clear)
3518 if (output_format
& (DIFF_FORMAT_RAW
|
3520 DIFF_FORMAT_NAME_STATUS
|
3521 DIFF_FORMAT_CHECKDIFF
)) {
3522 for (i
= 0; i
< q
->nr
; i
++) {
3523 struct diff_filepair
*p
= q
->queue
[i
];
3524 if (check_pair_status(p
))
3525 flush_one_pair(p
, options
);
3530 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3531 struct diffstat_t diffstat
;
3533 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3534 for (i
= 0; i
< q
->nr
; i
++) {
3535 struct diff_filepair
*p
= q
->queue
[i
];
3536 if (check_pair_status(p
))
3537 diff_flush_stat(p
, options
, &diffstat
);
3539 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3540 show_numstat(&diffstat
, options
);
3541 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3542 show_stats(&diffstat
, options
);
3543 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3544 show_shortstats(&diffstat
, options
);
3545 free_diffstat_info(&diffstat
);
3548 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3549 show_dirstat(options
);
3551 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3552 for (i
= 0; i
< q
->nr
; i
++)
3553 diff_summary(options
->file
, q
->queue
[i
]);
3557 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
3558 DIFF_OPT_TST(options
, EXIT_WITH_STATUS
) &&
3559 DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
3561 * run diff_flush_patch for the exit status. setting
3562 * options->file to /dev/null should be safe, becaue we
3563 * aren't supposed to produce any output anyway.
3565 if (options
->close_file
)
3566 fclose(options
->file
);
3567 options
->file
= fopen("/dev/null", "w");
3569 die_errno("Could not open /dev/null");
3570 options
->close_file
= 1;
3571 for (i
= 0; i
< q
->nr
; i
++) {
3572 struct diff_filepair
*p
= q
->queue
[i
];
3573 if (check_pair_status(p
))
3574 diff_flush_patch(p
, options
);
3575 if (options
->found_changes
)
3580 if (output_format
& DIFF_FORMAT_PATCH
) {
3582 putc(options
->line_termination
, options
->file
);
3583 if (options
->stat_sep
) {
3584 /* attach patch instead of inline */
3585 fputs(options
->stat_sep
, options
->file
);
3589 for (i
= 0; i
< q
->nr
; i
++) {
3590 struct diff_filepair
*p
= q
->queue
[i
];
3591 if (check_pair_status(p
))
3592 diff_flush_patch(p
, options
);
3596 if (output_format
& DIFF_FORMAT_CALLBACK
)
3597 options
->format_callback(q
, options
, options
->format_callback_data
);
3599 for (i
= 0; i
< q
->nr
; i
++)
3600 diff_free_filepair(q
->queue
[i
]);
3604 q
->nr
= q
->alloc
= 0;
3605 if (options
->close_file
)
3606 fclose(options
->file
);
3609 * Report the content-level differences with HAS_CHANGES;
3610 * diff_addremove/diff_change does not set the bit when
3611 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
3613 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
3614 if (options
->found_changes
)
3615 DIFF_OPT_SET(options
, HAS_CHANGES
);
3617 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3621 static void diffcore_apply_filter(const char *filter
)
3624 struct diff_queue_struct
*q
= &diff_queued_diff
;
3625 struct diff_queue_struct outq
;
3627 outq
.nr
= outq
.alloc
= 0;
3632 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3634 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3635 struct diff_filepair
*p
= q
->queue
[i
];
3636 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3638 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3640 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3641 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3642 strchr(filter
, p
->status
)))
3648 /* otherwise we will clear the whole queue
3649 * by copying the empty outq at the end of this
3650 * function, but first clear the current entries
3653 for (i
= 0; i
< q
->nr
; i
++)
3654 diff_free_filepair(q
->queue
[i
]);
3657 /* Only the matching ones */
3658 for (i
= 0; i
< q
->nr
; i
++) {
3659 struct diff_filepair
*p
= q
->queue
[i
];
3661 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3663 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3665 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3666 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3667 strchr(filter
, p
->status
)))
3670 diff_free_filepair(p
);
3677 /* Check whether two filespecs with the same mode and size are identical */
3678 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3679 struct diff_filespec
*two
)
3681 if (S_ISGITLINK(one
->mode
))
3683 if (diff_populate_filespec(one
, 0))
3685 if (diff_populate_filespec(two
, 0))
3687 return !memcmp(one
->data
, two
->data
, one
->size
);
3690 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3693 struct diff_queue_struct
*q
= &diff_queued_diff
;
3694 struct diff_queue_struct outq
;
3696 outq
.nr
= outq
.alloc
= 0;
3698 for (i
= 0; i
< q
->nr
; i
++) {
3699 struct diff_filepair
*p
= q
->queue
[i
];
3702 * 1. Entries that come from stat info dirtiness
3703 * always have both sides (iow, not create/delete),
3704 * one side of the object name is unknown, with
3705 * the same mode and size. Keep the ones that
3706 * do not match these criteria. They have real
3709 * 2. At this point, the file is known to be modified,
3710 * with the same mode and size, and the object
3711 * name of one side is unknown. Need to inspect
3712 * the identical contents.
3714 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3715 !DIFF_FILE_VALID(p
->two
) ||
3716 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3717 (p
->one
->mode
!= p
->two
->mode
) ||
3718 diff_populate_filespec(p
->one
, 1) ||
3719 diff_populate_filespec(p
->two
, 1) ||
3720 (p
->one
->size
!= p
->two
->size
) ||
3721 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3725 * The caller can subtract 1 from skip_stat_unmatch
3726 * to determine how many paths were dirty only
3727 * due to stat info mismatch.
3729 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3730 diffopt
->skip_stat_unmatch
++;
3731 diff_free_filepair(p
);
3738 static int diffnamecmp(const void *a_
, const void *b_
)
3740 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
3741 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
3742 const char *name_a
, *name_b
;
3744 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
3745 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
3746 return strcmp(name_a
, name_b
);
3749 void diffcore_fix_diff_index(struct diff_options
*options
)
3751 struct diff_queue_struct
*q
= &diff_queued_diff
;
3752 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), diffnamecmp
);
3755 void diffcore_std(struct diff_options
*options
)
3757 if (options
->skip_stat_unmatch
)
3758 diffcore_skip_stat_unmatch(options
);
3759 if (options
->break_opt
!= -1)
3760 diffcore_break(options
->break_opt
);
3761 if (options
->detect_rename
)
3762 diffcore_rename(options
);
3763 if (options
->break_opt
!= -1)
3764 diffcore_merge_broken();
3765 if (options
->pickaxe
)
3766 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3767 if (options
->orderfile
)
3768 diffcore_order(options
->orderfile
);
3769 diff_resolve_rename_copy();
3770 diffcore_apply_filter(options
->filter
);
3772 if (diff_queued_diff
.nr
&& !DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3773 DIFF_OPT_SET(options
, HAS_CHANGES
);
3775 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3778 int diff_result_code(struct diff_options
*opt
, int status
)
3781 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3782 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3784 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3785 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3787 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3788 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3793 void diff_addremove(struct diff_options
*options
,
3794 int addremove
, unsigned mode
,
3795 const unsigned char *sha1
,
3796 const char *concatpath
, unsigned dirty_submodule
)
3798 struct diff_filespec
*one
, *two
;
3800 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
3803 /* This may look odd, but it is a preparation for
3804 * feeding "there are unchanged files which should
3805 * not produce diffs, but when you are doing copy
3806 * detection you would need them, so here they are"
3807 * entries to the diff-core. They will be prefixed
3808 * with something like '=' or '*' (I haven't decided
3809 * which but should not make any difference).
3810 * Feeding the same new and old to diff_change()
3811 * also has the same effect.
3812 * Before the final output happens, they are pruned after
3813 * merged into rename/copy pairs as appropriate.
3815 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3816 addremove
= (addremove
== '+' ? '-' :
3817 addremove
== '-' ? '+' : addremove
);
3819 if (options
->prefix
&&
3820 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3823 one
= alloc_filespec(concatpath
);
3824 two
= alloc_filespec(concatpath
);
3826 if (addremove
!= '+')
3827 fill_filespec(one
, sha1
, mode
);
3828 if (addremove
!= '-') {
3829 fill_filespec(two
, sha1
, mode
);
3830 two
->dirty_submodule
= dirty_submodule
;
3833 diff_queue(&diff_queued_diff
, one
, two
);
3834 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3835 DIFF_OPT_SET(options
, HAS_CHANGES
);
3838 void diff_change(struct diff_options
*options
,
3839 unsigned old_mode
, unsigned new_mode
,
3840 const unsigned char *old_sha1
,
3841 const unsigned char *new_sha1
,
3842 const char *concatpath
,
3843 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
3845 struct diff_filespec
*one
, *two
;
3847 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
3848 && S_ISGITLINK(new_mode
))
3851 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3853 const unsigned char *tmp_c
;
3854 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3855 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3856 tmp
= old_dirty_submodule
; old_dirty_submodule
= new_dirty_submodule
;
3857 new_dirty_submodule
= tmp
;
3860 if (options
->prefix
&&
3861 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3864 one
= alloc_filespec(concatpath
);
3865 two
= alloc_filespec(concatpath
);
3866 fill_filespec(one
, old_sha1
, old_mode
);
3867 fill_filespec(two
, new_sha1
, new_mode
);
3868 one
->dirty_submodule
= old_dirty_submodule
;
3869 two
->dirty_submodule
= new_dirty_submodule
;
3871 diff_queue(&diff_queued_diff
, one
, two
);
3872 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3873 DIFF_OPT_SET(options
, HAS_CHANGES
);
3876 void diff_unmerge(struct diff_options
*options
,
3878 unsigned mode
, const unsigned char *sha1
)
3880 struct diff_filespec
*one
, *two
;
3882 if (options
->prefix
&&
3883 strncmp(path
, options
->prefix
, options
->prefix_length
))
3886 one
= alloc_filespec(path
);
3887 two
= alloc_filespec(path
);
3888 fill_filespec(one
, sha1
, mode
);
3889 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;
3892 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
3895 struct diff_tempfile
*temp
;
3896 const char *argv
[3];
3897 const char **arg
= argv
;
3898 struct child_process child
;
3899 struct strbuf buf
= STRBUF_INIT
;
3902 temp
= prepare_temp_file(spec
->path
, spec
);
3904 *arg
++ = temp
->name
;
3907 memset(&child
, 0, sizeof(child
));
3908 child
.use_shell
= 1;
3911 if (start_command(&child
)) {
3916 if (strbuf_read(&buf
, child
.out
, 0) < 0)
3917 err
= error("error reading from textconv command '%s'", pgm
);
3920 if (finish_command(&child
) || err
) {
3921 strbuf_release(&buf
);
3927 return strbuf_detach(&buf
, outsize
);