2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "run-command.h"
17 #ifdef NO_FAST_WORKING_DIRECTORY
18 #define FAST_WORKING_DIRECTORY 0
20 #define FAST_WORKING_DIRECTORY 1
23 static int diff_detect_rename_default
;
24 static int diff_rename_limit_default
= 200;
25 static int diff_suppress_blank_empty
;
26 int diff_use_color_default
= -1;
27 static const char *diff_word_regex_cfg
;
28 static const char *external_diff_cmd_cfg
;
29 int diff_auto_refresh_index
= 1;
30 static int diff_mnemonic_prefix
;
32 static char diff_colors
[][COLOR_MAXLEN
] = {
34 GIT_COLOR_NORMAL
, /* PLAIN */
35 GIT_COLOR_BOLD
, /* METAINFO */
36 GIT_COLOR_CYAN
, /* FRAGINFO */
37 GIT_COLOR_RED
, /* OLD */
38 GIT_COLOR_GREEN
, /* NEW */
39 GIT_COLOR_YELLOW
, /* COMMIT */
40 GIT_COLOR_BG_RED
, /* WHITESPACE */
43 static void diff_filespec_load_driver(struct diff_filespec
*one
);
44 static char *run_textconv(const char *, struct diff_filespec
*, size_t *);
46 static int parse_diff_color_slot(const char *var
, int ofs
)
48 if (!strcasecmp(var
+ofs
, "plain"))
50 if (!strcasecmp(var
+ofs
, "meta"))
52 if (!strcasecmp(var
+ofs
, "frag"))
54 if (!strcasecmp(var
+ofs
, "old"))
56 if (!strcasecmp(var
+ofs
, "new"))
58 if (!strcasecmp(var
+ofs
, "commit"))
60 if (!strcasecmp(var
+ofs
, "whitespace"))
61 return DIFF_WHITESPACE
;
62 die("bad config variable '%s'", var
);
65 static int git_config_rename(const char *var
, const char *value
)
68 return DIFF_DETECT_RENAME
;
69 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
70 return DIFF_DETECT_COPY
;
71 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
75 * These are to give UI layer defaults.
76 * The core-level commands such as git-diff-files should
77 * never be affected by the setting of diff.renames
78 * the user happens to have in the configuration file.
80 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
82 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
83 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
86 if (!strcmp(var
, "diff.renames")) {
87 diff_detect_rename_default
= git_config_rename(var
, value
);
90 if (!strcmp(var
, "diff.autorefreshindex")) {
91 diff_auto_refresh_index
= git_config_bool(var
, value
);
94 if (!strcmp(var
, "diff.mnemonicprefix")) {
95 diff_mnemonic_prefix
= git_config_bool(var
, value
);
98 if (!strcmp(var
, "diff.external"))
99 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
100 if (!strcmp(var
, "diff.wordregex"))
101 return git_config_string(&diff_word_regex_cfg
, var
, value
);
103 return git_diff_basic_config(var
, value
, cb
);
106 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
108 if (!strcmp(var
, "diff.renamelimit")) {
109 diff_rename_limit_default
= git_config_int(var
, value
);
113 switch (userdiff_config(var
, value
)) {
119 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
120 int slot
= parse_diff_color_slot(var
, 11);
122 return config_error_nonbool(var
);
123 color_parse(value
, var
, diff_colors
[slot
]);
127 /* like GNU diff's --suppress-blank-empty option */
128 if (!strcmp(var
, "diff.suppressblankempty") ||
129 /* for backwards compatibility */
130 !strcmp(var
, "diff.suppress-blank-empty")) {
131 diff_suppress_blank_empty
= git_config_bool(var
, value
);
135 return git_color_default_config(var
, value
, cb
);
138 static char *quote_two(const char *one
, const char *two
)
140 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
141 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
142 struct strbuf res
= STRBUF_INIT
;
144 if (need_one
+ need_two
) {
145 strbuf_addch(&res
, '"');
146 quote_c_style(one
, &res
, NULL
, 1);
147 quote_c_style(two
, &res
, NULL
, 1);
148 strbuf_addch(&res
, '"');
150 strbuf_addstr(&res
, one
);
151 strbuf_addstr(&res
, two
);
153 return strbuf_detach(&res
, NULL
);
156 static const char *external_diff(void)
158 static const char *external_diff_cmd
= NULL
;
159 static int done_preparing
= 0;
162 return external_diff_cmd
;
163 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
164 if (!external_diff_cmd
)
165 external_diff_cmd
= external_diff_cmd_cfg
;
167 return external_diff_cmd
;
170 static struct diff_tempfile
{
171 const char *name
; /* filename external diff should read from */
174 char tmp_path
[PATH_MAX
];
177 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
179 struct emit_callback
{
182 int blank_at_eof_in_preimage
;
183 int blank_at_eof_in_postimage
;
185 int lno_in_postimage
;
186 sane_truncate_fn truncate
;
187 const char **label_path
;
188 struct diff_words_data
*diff_words
;
193 static int count_lines(const char *data
, int size
)
195 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
202 completely_empty
= 0;
206 completely_empty
= 0;
209 if (completely_empty
)
212 count
++; /* no trailing newline */
216 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
218 if (!DIFF_FILE_VALID(one
)) {
219 mf
->ptr
= (char *)""; /* does not matter */
223 else if (diff_populate_filespec(one
, 0))
227 mf
->size
= one
->size
;
231 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
234 long size
= mf
->size
;
239 ptr
+= size
- 1; /* pointing at the very end */
241 ; /* incomplete line */
243 ptr
--; /* skip the last LF */
244 while (mf
->ptr
< ptr
) {
246 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
247 if (*prev_eol
== '\n')
249 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
257 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
258 struct emit_callback
*ecbdata
)
261 unsigned ws_rule
= ecbdata
->ws_rule
;
262 l1
= count_trailing_blank(mf1
, ws_rule
);
263 l2
= count_trailing_blank(mf2
, ws_rule
);
265 ecbdata
->blank_at_eof_in_preimage
= 0;
266 ecbdata
->blank_at_eof_in_postimage
= 0;
269 at
= count_lines(mf1
->ptr
, mf1
->size
);
270 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
272 at
= count_lines(mf2
->ptr
, mf2
->size
);
273 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
276 static void emit_line_0(FILE *file
, const char *set
, const char *reset
,
277 int first
, const char *line
, int len
)
279 int has_trailing_newline
, has_trailing_carriage_return
;
283 has_trailing_newline
= (first
== '\n');
284 has_trailing_carriage_return
= (!has_trailing_newline
&&
286 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
288 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
289 if (has_trailing_newline
)
291 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
292 if (has_trailing_carriage_return
)
301 fwrite(line
, len
, 1, file
);
303 if (has_trailing_carriage_return
)
305 if (has_trailing_newline
)
309 static void emit_line(FILE *file
, const char *set
, const char *reset
,
310 const char *line
, int len
)
312 emit_line_0(file
, set
, reset
, line
[0], line
+1, len
-1);
315 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
317 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
318 ecbdata
->blank_at_eof_in_preimage
&&
319 ecbdata
->blank_at_eof_in_postimage
&&
320 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
321 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
323 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
326 static void emit_add_line(const char *reset
,
327 struct emit_callback
*ecbdata
,
328 const char *line
, int len
)
330 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
331 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
334 emit_line_0(ecbdata
->file
, set
, reset
, '+', line
, len
);
335 else if (new_blank_line_at_eof(ecbdata
, line
, len
))
336 /* Blank line at EOF - paint '+' as well */
337 emit_line_0(ecbdata
->file
, ws
, reset
, '+', line
, len
);
339 /* Emit just the prefix, then the rest. */
340 emit_line_0(ecbdata
->file
, set
, reset
, '+', "", 0);
341 ws_check_emit(line
, len
, ecbdata
->ws_rule
,
342 ecbdata
->file
, set
, reset
, ws
);
346 static struct diff_tempfile
*claim_diff_tempfile(void) {
348 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
349 if (!diff_temp
[i
].name
)
350 return diff_temp
+ i
;
351 die("BUG: diff is failing to clean up its tempfiles");
354 static int remove_tempfile_installed
;
356 static void remove_tempfile(void)
359 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
360 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
)
361 unlink_or_warn(diff_temp
[i
].name
);
362 diff_temp
[i
].name
= NULL
;
366 static void remove_tempfile_on_signal(int signo
)
373 static void print_line_count(FILE *file
, int count
)
377 fprintf(file
, "0,0");
383 fprintf(file
, "1,%d", count
);
388 static void copy_file_with_prefix(FILE *file
,
389 int prefix
, const char *data
, int size
,
390 const char *set
, const char *reset
)
392 int ch
, nl_just_seen
= 1;
407 fprintf(file
, "%s\n\\ No newline at end of file\n", reset
);
410 static void emit_rewrite_diff(const char *name_a
,
412 struct diff_filespec
*one
,
413 struct diff_filespec
*two
,
414 const char *textconv_one
,
415 const char *textconv_two
,
416 struct diff_options
*o
)
419 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
420 const char *name_a_tab
, *name_b_tab
;
421 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
422 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
423 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
424 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
425 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
426 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
427 const char *a_prefix
, *b_prefix
;
428 const char *data_one
, *data_two
;
429 size_t size_one
, size_two
;
431 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
432 a_prefix
= o
->b_prefix
;
433 b_prefix
= o
->a_prefix
;
435 a_prefix
= o
->a_prefix
;
436 b_prefix
= o
->b_prefix
;
439 name_a
+= (*name_a
== '/');
440 name_b
+= (*name_b
== '/');
441 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
442 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
444 strbuf_reset(&a_name
);
445 strbuf_reset(&b_name
);
446 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
447 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
449 diff_populate_filespec(one
, 0);
450 diff_populate_filespec(two
, 0);
452 data_one
= run_textconv(textconv_one
, one
, &size_one
);
454 die("unable to read files to diff");
457 data_one
= one
->data
;
458 size_one
= one
->size
;
461 data_two
= run_textconv(textconv_two
, two
, &size_two
);
463 die("unable to read files to diff");
466 data_two
= two
->data
;
467 size_two
= two
->size
;
470 lc_a
= count_lines(data_one
, size_one
);
471 lc_b
= count_lines(data_two
, size_two
);
473 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
474 metainfo
, a_name
.buf
, name_a_tab
, reset
,
475 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
476 print_line_count(o
->file
, lc_a
);
477 fprintf(o
->file
, " +");
478 print_line_count(o
->file
, lc_b
);
479 fprintf(o
->file
, " @@%s\n", reset
);
481 copy_file_with_prefix(o
->file
, '-', data_one
, size_one
, old
, reset
);
483 copy_file_with_prefix(o
->file
, '+', data_two
, size_two
, new, reset
);
486 struct diff_words_buffer
{
489 struct diff_words_orig
{
490 const char *begin
, *end
;
492 int orig_nr
, orig_alloc
;
495 static void diff_words_append(char *line
, unsigned long len
,
496 struct diff_words_buffer
*buffer
)
498 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
501 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
502 buffer
->text
.size
+= len
;
503 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
506 struct diff_words_data
{
507 struct diff_words_buffer minus
, plus
;
508 const char *current_plus
;
513 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
515 struct diff_words_data
*diff_words
= priv
;
516 int minus_first
, minus_len
, plus_first
, plus_len
;
517 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
519 if (line
[0] != '@' || parse_hunk_header(line
, len
,
520 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
523 /* POSIX requires that first be decremented by one if len == 0... */
525 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
527 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
529 minus_begin
= minus_end
=
530 diff_words
->minus
.orig
[minus_first
].end
;
533 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
534 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
536 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
538 if (diff_words
->current_plus
!= plus_begin
)
539 fwrite(diff_words
->current_plus
,
540 plus_begin
- diff_words
->current_plus
, 1,
542 if (minus_begin
!= minus_end
)
543 color_fwrite_lines(diff_words
->file
,
544 diff_get_color(1, DIFF_FILE_OLD
),
545 minus_end
- minus_begin
, minus_begin
);
546 if (plus_begin
!= plus_end
)
547 color_fwrite_lines(diff_words
->file
,
548 diff_get_color(1, DIFF_FILE_NEW
),
549 plus_end
- plus_begin
, plus_begin
);
551 diff_words
->current_plus
= plus_end
;
554 /* This function starts looking at *begin, and returns 0 iff a word was found. */
555 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
556 int *begin
, int *end
)
558 if (word_regex
&& *begin
< buffer
->size
) {
560 if (!regexec(word_regex
, buffer
->ptr
+ *begin
, 1, match
, 0)) {
561 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
562 '\n', match
[0].rm_eo
- match
[0].rm_so
);
563 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
564 *begin
+= match
[0].rm_so
;
565 return *begin
>= *end
;
570 /* find the next word */
571 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
573 if (*begin
>= buffer
->size
)
576 /* find the end of the word */
578 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
585 * This function splits the words in buffer->text, stores the list with
586 * newline separator into out, and saves the offsets of the original words
589 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
598 /* fake an empty "0th" word */
599 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
600 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
603 for (i
= 0; i
< buffer
->text
.size
; i
++) {
604 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
607 /* store original boundaries */
608 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
610 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
611 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
615 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
616 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
617 out
->ptr
[out
->size
+ j
- i
] = '\n';
618 out
->size
+= j
- i
+ 1;
624 /* this executes the word diff on the accumulated buffers */
625 static void diff_words_show(struct diff_words_data
*diff_words
)
630 mmfile_t minus
, plus
;
632 /* special case: only removal */
633 if (!diff_words
->plus
.text
.size
) {
634 color_fwrite_lines(diff_words
->file
,
635 diff_get_color(1, DIFF_FILE_OLD
),
636 diff_words
->minus
.text
.size
, diff_words
->minus
.text
.ptr
);
637 diff_words
->minus
.text
.size
= 0;
641 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
643 memset(&xpp
, 0, sizeof(xpp
));
644 memset(&xecfg
, 0, sizeof(xecfg
));
645 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
646 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
647 xpp
.flags
= XDF_NEED_MINIMAL
;
648 /* as only the hunk header will be parsed, we need a 0-context */
650 xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
654 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
655 diff_words
->plus
.text
.size
)
656 fwrite(diff_words
->current_plus
,
657 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
658 - diff_words
->current_plus
, 1,
660 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
663 static void free_diff_words_data(struct emit_callback
*ecbdata
)
665 if (ecbdata
->diff_words
) {
667 if (ecbdata
->diff_words
->minus
.text
.size
||
668 ecbdata
->diff_words
->plus
.text
.size
)
669 diff_words_show(ecbdata
->diff_words
);
671 free (ecbdata
->diff_words
->minus
.text
.ptr
);
672 free (ecbdata
->diff_words
->minus
.orig
);
673 free (ecbdata
->diff_words
->plus
.text
.ptr
);
674 free (ecbdata
->diff_words
->plus
.orig
);
675 free(ecbdata
->diff_words
->word_regex
);
676 free(ecbdata
->diff_words
);
677 ecbdata
->diff_words
= NULL
;
681 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
684 return diff_colors
[ix
];
688 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
695 return ecb
->truncate(line
, len
);
699 (void) utf8_width(&cp
, &l
);
701 break; /* truncated in the middle? */
706 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
709 ecbdata
->lno_in_preimage
= 0;
710 ecbdata
->lno_in_postimage
= 0;
711 p
= strchr(line
, '-');
713 return; /* cannot happen */
714 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
717 return; /* cannot happen */
718 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
721 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
723 struct emit_callback
*ecbdata
= priv
;
724 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
725 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
726 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
728 *(ecbdata
->found_changesp
) = 1;
730 if (ecbdata
->label_path
[0]) {
731 const char *name_a_tab
, *name_b_tab
;
733 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
734 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
736 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
737 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
738 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
739 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
740 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
743 if (diff_suppress_blank_empty
744 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
749 if (line
[0] == '@') {
750 len
= sane_truncate_line(ecbdata
, line
, len
);
751 find_lno(line
, ecbdata
);
752 emit_line(ecbdata
->file
,
753 diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
755 if (line
[len
-1] != '\n')
756 putc('\n', ecbdata
->file
);
761 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
765 if (ecbdata
->diff_words
) {
766 if (line
[0] == '-') {
767 diff_words_append(line
, len
,
768 &ecbdata
->diff_words
->minus
);
770 } else if (line
[0] == '+') {
771 diff_words_append(line
, len
,
772 &ecbdata
->diff_words
->plus
);
775 if (ecbdata
->diff_words
->minus
.text
.size
||
776 ecbdata
->diff_words
->plus
.text
.size
)
777 diff_words_show(ecbdata
->diff_words
);
780 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
784 if (line
[0] != '+') {
786 diff_get_color(ecbdata
->color_diff
,
787 line
[0] == '-' ? DIFF_FILE_OLD
: DIFF_PLAIN
);
788 ecbdata
->lno_in_preimage
++;
790 ecbdata
->lno_in_postimage
++;
791 emit_line(ecbdata
->file
, color
, reset
, line
, len
);
793 ecbdata
->lno_in_postimage
++;
794 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
798 static char *pprint_rename(const char *a
, const char *b
)
802 struct strbuf name
= STRBUF_INIT
;
803 int pfx_length
, sfx_length
;
804 int len_a
= strlen(a
);
805 int len_b
= strlen(b
);
806 int a_midlen
, b_midlen
;
807 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
808 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
810 if (qlen_a
|| qlen_b
) {
811 quote_c_style(a
, &name
, NULL
, 0);
812 strbuf_addstr(&name
, " => ");
813 quote_c_style(b
, &name
, NULL
, 0);
814 return strbuf_detach(&name
, NULL
);
817 /* Find common prefix */
819 while (*old
&& *new && *old
== *new) {
821 pfx_length
= old
- a
+ 1;
826 /* Find common suffix */
830 while (a
<= old
&& b
<= new && *old
== *new) {
832 sfx_length
= len_a
- (old
- a
);
838 * pfx{mid-a => mid-b}sfx
839 * {pfx-a => pfx-b}sfx
840 * pfx{sfx-a => sfx-b}
843 a_midlen
= len_a
- pfx_length
- sfx_length
;
844 b_midlen
= len_b
- pfx_length
- sfx_length
;
850 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
851 if (pfx_length
+ sfx_length
) {
852 strbuf_add(&name
, a
, pfx_length
);
853 strbuf_addch(&name
, '{');
855 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
856 strbuf_addstr(&name
, " => ");
857 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
858 if (pfx_length
+ sfx_length
) {
859 strbuf_addch(&name
, '}');
860 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
862 return strbuf_detach(&name
, NULL
);
868 struct diffstat_file
{
872 unsigned is_unmerged
:1;
873 unsigned is_binary
:1;
874 unsigned is_renamed
:1;
875 unsigned int added
, deleted
;
879 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
883 struct diffstat_file
*x
;
884 x
= xcalloc(sizeof (*x
), 1);
885 if (diffstat
->nr
== diffstat
->alloc
) {
886 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
887 diffstat
->files
= xrealloc(diffstat
->files
,
888 diffstat
->alloc
* sizeof(x
));
890 diffstat
->files
[diffstat
->nr
++] = x
;
892 x
->from_name
= xstrdup(name_a
);
893 x
->name
= xstrdup(name_b
);
898 x
->name
= xstrdup(name_a
);
903 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
905 struct diffstat_t
*diffstat
= priv
;
906 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
910 else if (line
[0] == '-')
914 const char mime_boundary_leader
[] = "------------";
916 static int scale_linear(int it
, int width
, int max_change
)
919 * make sure that at least one '-' is printed if there were deletions,
920 * and likewise for '+'.
924 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
927 static void show_name(FILE *file
,
928 const char *prefix
, const char *name
, int len
)
930 fprintf(file
, " %s%-*s |", prefix
, len
, name
);
933 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
937 fprintf(file
, "%s", set
);
940 fprintf(file
, "%s", reset
);
943 static void fill_print_name(struct diffstat_file
*file
)
947 if (file
->print_name
)
950 if (!file
->is_renamed
) {
951 struct strbuf buf
= STRBUF_INIT
;
952 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
953 pname
= strbuf_detach(&buf
, NULL
);
956 strbuf_release(&buf
);
959 pname
= pprint_rename(file
->from_name
, file
->name
);
961 file
->print_name
= pname
;
964 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
966 int i
, len
, add
, del
, adds
= 0, dels
= 0;
967 int max_change
= 0, max_len
= 0;
968 int total_files
= data
->nr
;
969 int width
, name_width
;
970 const char *reset
, *set
, *add_c
, *del_c
;
975 width
= options
->stat_width
? options
->stat_width
: 80;
976 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
978 /* Sanity: give at least 5 columns to the graph,
979 * but leave at least 10 columns for the name.
985 else if (width
< name_width
+ 15)
986 name_width
= width
- 15;
988 /* Find the longest filename and max number of changes */
989 reset
= diff_get_color_opt(options
, DIFF_RESET
);
990 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
991 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
992 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
994 for (i
= 0; i
< data
->nr
; i
++) {
995 struct diffstat_file
*file
= data
->files
[i
];
996 int change
= file
->added
+ file
->deleted
;
997 fill_print_name(file
);
998 len
= strlen(file
->print_name
);
1002 if (file
->is_binary
|| file
->is_unmerged
)
1004 if (max_change
< change
)
1005 max_change
= change
;
1008 /* Compute the width of the graph part;
1009 * 10 is for one blank at the beginning of the line plus
1010 * " | count " between the name and the graph.
1012 * From here on, name_width is the width of the name area,
1013 * and width is the width of the graph area.
1015 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
1016 if (width
< (name_width
+ 10) + max_change
)
1017 width
= width
- (name_width
+ 10);
1021 for (i
= 0; i
< data
->nr
; i
++) {
1022 const char *prefix
= "";
1023 char *name
= data
->files
[i
]->print_name
;
1024 int added
= data
->files
[i
]->added
;
1025 int deleted
= data
->files
[i
]->deleted
;
1029 * "scale" the filename
1032 name_len
= strlen(name
);
1033 if (name_width
< name_len
) {
1037 name
+= name_len
- len
;
1038 slash
= strchr(name
, '/');
1043 if (data
->files
[i
]->is_binary
) {
1044 show_name(options
->file
, prefix
, name
, len
);
1045 fprintf(options
->file
, " Bin ");
1046 fprintf(options
->file
, "%s%d%s", del_c
, deleted
, reset
);
1047 fprintf(options
->file
, " -> ");
1048 fprintf(options
->file
, "%s%d%s", add_c
, added
, reset
);
1049 fprintf(options
->file
, " bytes");
1050 fprintf(options
->file
, "\n");
1053 else if (data
->files
[i
]->is_unmerged
) {
1054 show_name(options
->file
, prefix
, name
, len
);
1055 fprintf(options
->file
, " Unmerged\n");
1058 else if (!data
->files
[i
]->is_renamed
&&
1059 (added
+ deleted
== 0)) {
1065 * scale the add/delete
1072 if (width
<= max_change
) {
1073 add
= scale_linear(add
, width
, max_change
);
1074 del
= scale_linear(del
, width
, max_change
);
1076 show_name(options
->file
, prefix
, name
, len
);
1077 fprintf(options
->file
, "%5d%s", added
+ deleted
,
1078 added
+ deleted
? " " : "");
1079 show_graph(options
->file
, '+', add
, add_c
, reset
);
1080 show_graph(options
->file
, '-', del
, del_c
, reset
);
1081 fprintf(options
->file
, "\n");
1083 fprintf(options
->file
,
1084 " %d files changed, %d insertions(+), %d deletions(-)\n",
1085 total_files
, adds
, dels
);
1088 static void show_shortstats(struct diffstat_t
* data
, struct diff_options
*options
)
1090 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
1095 for (i
= 0; i
< data
->nr
; i
++) {
1096 if (!data
->files
[i
]->is_binary
&&
1097 !data
->files
[i
]->is_unmerged
) {
1098 int added
= data
->files
[i
]->added
;
1099 int deleted
= data
->files
[i
]->deleted
;
1100 if (!data
->files
[i
]->is_renamed
&&
1101 (added
+ deleted
== 0)) {
1109 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
1110 total_files
, adds
, dels
);
1113 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
1120 for (i
= 0; i
< data
->nr
; i
++) {
1121 struct diffstat_file
*file
= data
->files
[i
];
1123 if (file
->is_binary
)
1124 fprintf(options
->file
, "-\t-\t");
1126 fprintf(options
->file
,
1127 "%d\t%d\t", file
->added
, file
->deleted
);
1128 if (options
->line_termination
) {
1129 fill_print_name(file
);
1130 if (!file
->is_renamed
)
1131 write_name_quoted(file
->name
, options
->file
,
1132 options
->line_termination
);
1134 fputs(file
->print_name
, options
->file
);
1135 putc(options
->line_termination
, options
->file
);
1138 if (file
->is_renamed
) {
1139 putc('\0', options
->file
);
1140 write_name_quoted(file
->from_name
, options
->file
, '\0');
1142 write_name_quoted(file
->name
, options
->file
, '\0');
1147 struct dirstat_file
{
1149 unsigned long changed
;
1152 struct dirstat_dir
{
1153 struct dirstat_file
*files
;
1154 int alloc
, nr
, percent
, cumulative
;
1157 static long gather_dirstat(FILE *file
, struct dirstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1159 unsigned long this_dir
= 0;
1160 unsigned int sources
= 0;
1163 struct dirstat_file
*f
= dir
->files
;
1164 int namelen
= strlen(f
->name
);
1168 if (namelen
< baselen
)
1170 if (memcmp(f
->name
, base
, baselen
))
1172 slash
= strchr(f
->name
+ baselen
, '/');
1174 int newbaselen
= slash
+ 1 - f
->name
;
1175 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1187 * We don't report dirstat's for
1189 * - or cases where everything came from a single directory
1190 * under this directory (sources == 1).
1192 if (baselen
&& sources
!= 1) {
1193 int permille
= this_dir
* 1000 / changed
;
1195 int percent
= permille
/ 10;
1196 if (percent
>= dir
->percent
) {
1197 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1198 if (!dir
->cumulative
)
1206 static int dirstat_compare(const void *_a
, const void *_b
)
1208 const struct dirstat_file
*a
= _a
;
1209 const struct dirstat_file
*b
= _b
;
1210 return strcmp(a
->name
, b
->name
);
1213 static void show_dirstat(struct diff_options
*options
)
1216 unsigned long changed
;
1217 struct dirstat_dir dir
;
1218 struct diff_queue_struct
*q
= &diff_queued_diff
;
1223 dir
.percent
= options
->dirstat_percent
;
1224 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1227 for (i
= 0; i
< q
->nr
; i
++) {
1228 struct diff_filepair
*p
= q
->queue
[i
];
1230 unsigned long copied
, added
, damage
;
1232 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1234 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1235 diff_populate_filespec(p
->one
, 0);
1236 diff_populate_filespec(p
->two
, 0);
1237 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1239 diff_free_filespec_data(p
->one
);
1240 diff_free_filespec_data(p
->two
);
1241 } else if (DIFF_FILE_VALID(p
->one
)) {
1242 diff_populate_filespec(p
->one
, 1);
1244 diff_free_filespec_data(p
->one
);
1245 } else if (DIFF_FILE_VALID(p
->two
)) {
1246 diff_populate_filespec(p
->two
, 1);
1248 added
= p
->two
->size
;
1249 diff_free_filespec_data(p
->two
);
1254 * Original minus copied is the removed material,
1255 * added is the new material. They are both damages
1256 * made to the preimage. In --dirstat-by-file mode, count
1257 * damaged files, not damaged lines. This is done by
1258 * counting only a single damaged line per file.
1260 damage
= (p
->one
->size
- copied
) + added
;
1261 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
) && damage
> 0)
1264 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1265 dir
.files
[dir
.nr
].name
= name
;
1266 dir
.files
[dir
.nr
].changed
= damage
;
1271 /* This can happen even with many files, if everything was renames */
1275 /* Show all directories with more than x% of the changes */
1276 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
1277 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1280 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1283 for (i
= 0; i
< diffstat
->nr
; i
++) {
1284 struct diffstat_file
*f
= diffstat
->files
[i
];
1285 if (f
->name
!= f
->print_name
)
1286 free(f
->print_name
);
1291 free(diffstat
->files
);
1294 struct checkdiff_t
{
1295 const char *filename
;
1297 struct diff_options
*o
;
1302 static int is_conflict_marker(const char *line
, unsigned long len
)
1309 firstchar
= line
[0];
1310 switch (firstchar
) {
1311 case '=': case '>': case '<':
1316 for (cnt
= 1; cnt
< 7; cnt
++)
1317 if (line
[cnt
] != firstchar
)
1319 /* line[0] thru line[6] are same as firstchar */
1320 if (firstchar
== '=') {
1321 /* divider between ours and theirs? */
1322 if (len
!= 8 || line
[7] != '\n')
1324 } else if (len
< 8 || !isspace(line
[7])) {
1325 /* not divider before ours nor after theirs */
1331 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1333 struct checkdiff_t
*data
= priv
;
1334 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1335 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1336 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1337 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1340 if (line
[0] == '+') {
1343 if (is_conflict_marker(line
+ 1, len
- 1)) {
1345 fprintf(data
->o
->file
,
1346 "%s:%d: leftover conflict marker\n",
1347 data
->filename
, data
->lineno
);
1349 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1352 data
->status
|= bad
;
1353 err
= whitespace_error_string(bad
);
1354 fprintf(data
->o
->file
, "%s:%d: %s.\n",
1355 data
->filename
, data
->lineno
, err
);
1357 emit_line(data
->o
->file
, set
, reset
, line
, 1);
1358 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1359 data
->o
->file
, set
, reset
, ws
);
1360 } else if (line
[0] == ' ') {
1362 } else if (line
[0] == '@') {
1363 char *plus
= strchr(line
, '+');
1365 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1367 die("invalid diff");
1371 static unsigned char *deflate_it(char *data
,
1373 unsigned long *result_size
)
1376 unsigned char *deflated
;
1379 memset(&stream
, 0, sizeof(stream
));
1380 deflateInit(&stream
, zlib_compression_level
);
1381 bound
= deflateBound(&stream
, size
);
1382 deflated
= xmalloc(bound
);
1383 stream
.next_out
= deflated
;
1384 stream
.avail_out
= bound
;
1386 stream
.next_in
= (unsigned char *)data
;
1387 stream
.avail_in
= size
;
1388 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1390 deflateEnd(&stream
);
1391 *result_size
= stream
.total_out
;
1395 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1401 unsigned long orig_size
;
1402 unsigned long delta_size
;
1403 unsigned long deflate_size
;
1404 unsigned long data_size
;
1406 /* We could do deflated delta, or we could do just deflated two,
1407 * whichever is smaller.
1410 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1411 if (one
->size
&& two
->size
) {
1412 delta
= diff_delta(one
->ptr
, one
->size
,
1413 two
->ptr
, two
->size
,
1414 &delta_size
, deflate_size
);
1416 void *to_free
= delta
;
1417 orig_size
= delta_size
;
1418 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1423 if (delta
&& delta_size
< deflate_size
) {
1424 fprintf(file
, "delta %lu\n", orig_size
);
1427 data_size
= delta_size
;
1430 fprintf(file
, "literal %lu\n", two
->size
);
1433 data_size
= deflate_size
;
1436 /* emit data encoded in base85 */
1439 int bytes
= (52 < data_size
) ? 52 : data_size
;
1443 line
[0] = bytes
+ 'A' - 1;
1445 line
[0] = bytes
- 26 + 'a' - 1;
1446 encode_85(line
+ 1, cp
, bytes
);
1447 cp
= (char *) cp
+ bytes
;
1451 fprintf(file
, "\n");
1455 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1457 fprintf(file
, "GIT binary patch\n");
1458 emit_binary_diff_body(file
, one
, two
);
1459 emit_binary_diff_body(file
, two
, one
);
1462 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1465 one
->driver
= userdiff_find_by_path(one
->path
);
1467 one
->driver
= userdiff_find_by_name("default");
1470 int diff_filespec_is_binary(struct diff_filespec
*one
)
1472 if (one
->is_binary
== -1) {
1473 diff_filespec_load_driver(one
);
1474 if (one
->driver
->binary
!= -1)
1475 one
->is_binary
= one
->driver
->binary
;
1477 if (!one
->data
&& DIFF_FILE_VALID(one
))
1478 diff_populate_filespec(one
, 0);
1480 one
->is_binary
= buffer_is_binary(one
->data
,
1482 if (one
->is_binary
== -1)
1486 return one
->is_binary
;
1489 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
1491 diff_filespec_load_driver(one
);
1492 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
1495 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1497 diff_filespec_load_driver(one
);
1498 return one
->driver
->word_regex
;
1501 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
1503 if (!options
->a_prefix
)
1504 options
->a_prefix
= a
;
1505 if (!options
->b_prefix
)
1506 options
->b_prefix
= b
;
1509 static const char *get_textconv(struct diff_filespec
*one
)
1511 if (!DIFF_FILE_VALID(one
))
1513 if (!S_ISREG(one
->mode
))
1515 diff_filespec_load_driver(one
);
1516 return one
->driver
->textconv
;
1519 static void builtin_diff(const char *name_a
,
1521 struct diff_filespec
*one
,
1522 struct diff_filespec
*two
,
1523 const char *xfrm_msg
,
1524 struct diff_options
*o
,
1525 int complete_rewrite
)
1529 char *a_one
, *b_two
;
1530 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1531 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1532 const char *a_prefix
, *b_prefix
;
1533 const char *textconv_one
= NULL
, *textconv_two
= NULL
;
1535 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
1536 textconv_one
= get_textconv(one
);
1537 textconv_two
= get_textconv(two
);
1540 diff_set_mnemonic_prefix(o
, "a/", "b/");
1541 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
1542 a_prefix
= o
->b_prefix
;
1543 b_prefix
= o
->a_prefix
;
1545 a_prefix
= o
->a_prefix
;
1546 b_prefix
= o
->b_prefix
;
1549 /* Never use a non-valid filename anywhere if at all possible */
1550 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
1551 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
1553 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
1554 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
1555 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1556 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1557 fprintf(o
->file
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1558 if (lbl
[0][0] == '/') {
1560 fprintf(o
->file
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1561 if (xfrm_msg
&& xfrm_msg
[0])
1562 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1564 else if (lbl
[1][0] == '/') {
1565 fprintf(o
->file
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1566 if (xfrm_msg
&& xfrm_msg
[0])
1567 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1570 if (one
->mode
!= two
->mode
) {
1571 fprintf(o
->file
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1572 fprintf(o
->file
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1574 if (xfrm_msg
&& xfrm_msg
[0])
1575 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1577 * we do not run diff between different kind
1580 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1581 goto free_ab_and_return
;
1582 if (complete_rewrite
&&
1583 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
1584 (textconv_two
|| !diff_filespec_is_binary(two
))) {
1585 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1586 textconv_one
, textconv_two
, o
);
1587 o
->found_changes
= 1;
1588 goto free_ab_and_return
;
1592 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1593 die("unable to read files to diff");
1595 if (!DIFF_OPT_TST(o
, TEXT
) &&
1596 ( (diff_filespec_is_binary(one
) && !textconv_one
) ||
1597 (diff_filespec_is_binary(two
) && !textconv_two
) )) {
1598 /* Quite common confusing case */
1599 if (mf1
.size
== mf2
.size
&&
1600 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1601 goto free_ab_and_return
;
1602 if (DIFF_OPT_TST(o
, BINARY
))
1603 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1605 fprintf(o
->file
, "Binary files %s and %s differ\n",
1607 o
->found_changes
= 1;
1610 /* Crazy xdl interfaces.. */
1611 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1615 struct emit_callback ecbdata
;
1616 const struct userdiff_funcname
*pe
;
1620 mf1
.ptr
= run_textconv(textconv_one
, one
, &size
);
1622 die("unable to read files to diff");
1627 mf2
.ptr
= run_textconv(textconv_two
, two
, &size
);
1629 die("unable to read files to diff");
1633 pe
= diff_funcname_pattern(one
);
1635 pe
= diff_funcname_pattern(two
);
1637 memset(&xpp
, 0, sizeof(xpp
));
1638 memset(&xecfg
, 0, sizeof(xecfg
));
1639 memset(&ecbdata
, 0, sizeof(ecbdata
));
1640 ecbdata
.label_path
= lbl
;
1641 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1642 ecbdata
.found_changesp
= &o
->found_changes
;
1643 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1644 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
1645 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1646 ecbdata
.file
= o
->file
;
1647 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1648 xecfg
.ctxlen
= o
->context
;
1649 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
1650 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1652 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
1655 else if (!prefixcmp(diffopts
, "--unified="))
1656 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1657 else if (!prefixcmp(diffopts
, "-u"))
1658 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1659 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
)) {
1660 ecbdata
.diff_words
=
1661 xcalloc(1, sizeof(struct diff_words_data
));
1662 ecbdata
.diff_words
->file
= o
->file
;
1664 o
->word_regex
= userdiff_word_regex(one
);
1666 o
->word_regex
= userdiff_word_regex(two
);
1668 o
->word_regex
= diff_word_regex_cfg
;
1669 if (o
->word_regex
) {
1670 ecbdata
.diff_words
->word_regex
= (regex_t
*)
1671 xmalloc(sizeof(regex_t
));
1672 if (regcomp(ecbdata
.diff_words
->word_regex
,
1674 REG_EXTENDED
| REG_NEWLINE
))
1675 die ("Invalid regular expression: %s",
1679 xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
1680 &xpp
, &xecfg
, &ecb
);
1681 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
))
1682 free_diff_words_data(&ecbdata
);
1687 xdiff_clear_find_func(&xecfg
);
1691 diff_free_filespec_data(one
);
1692 diff_free_filespec_data(two
);
1698 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1699 struct diff_filespec
*one
,
1700 struct diff_filespec
*two
,
1701 struct diffstat_t
*diffstat
,
1702 struct diff_options
*o
,
1703 int complete_rewrite
)
1706 struct diffstat_file
*data
;
1708 data
= diffstat_add(diffstat
, name_a
, name_b
);
1711 data
->is_unmerged
= 1;
1714 if (complete_rewrite
) {
1715 diff_populate_filespec(one
, 0);
1716 diff_populate_filespec(two
, 0);
1717 data
->deleted
= count_lines(one
->data
, one
->size
);
1718 data
->added
= count_lines(two
->data
, two
->size
);
1719 goto free_and_return
;
1721 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1722 die("unable to read files to diff");
1724 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1725 data
->is_binary
= 1;
1726 data
->added
= mf2
.size
;
1727 data
->deleted
= mf1
.size
;
1729 /* Crazy xdl interfaces.. */
1734 memset(&xpp
, 0, sizeof(xpp
));
1735 memset(&xecfg
, 0, sizeof(xecfg
));
1736 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1737 xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
1738 &xpp
, &xecfg
, &ecb
);
1742 diff_free_filespec_data(one
);
1743 diff_free_filespec_data(two
);
1746 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1747 const char *attr_path
,
1748 struct diff_filespec
*one
,
1749 struct diff_filespec
*two
,
1750 struct diff_options
*o
)
1753 struct checkdiff_t data
;
1758 memset(&data
, 0, sizeof(data
));
1759 data
.filename
= name_b
? name_b
: name_a
;
1762 data
.ws_rule
= whitespace_rule(attr_path
);
1764 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1765 die("unable to read files to diff");
1768 * All the other codepaths check both sides, but not checking
1769 * the "old" side here is deliberate. We are checking the newly
1770 * introduced changes, and as long as the "new" side is text, we
1771 * can and should check what it introduces.
1773 if (diff_filespec_is_binary(two
))
1774 goto free_and_return
;
1776 /* Crazy xdl interfaces.. */
1781 memset(&xpp
, 0, sizeof(xpp
));
1782 memset(&xecfg
, 0, sizeof(xecfg
));
1783 xecfg
.ctxlen
= 1; /* at least one context line */
1784 xpp
.flags
= XDF_NEED_MINIMAL
;
1785 xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
1786 &xpp
, &xecfg
, &ecb
);
1788 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
1789 struct emit_callback ecbdata
;
1792 ecbdata
.ws_rule
= data
.ws_rule
;
1793 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1794 blank_at_eof
= ecbdata
.blank_at_eof_in_preimage
;
1799 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
1800 fprintf(o
->file
, "%s:%d: %s.\n",
1801 data
.filename
, blank_at_eof
, err
);
1802 data
.status
= 1; /* report errors */
1807 diff_free_filespec_data(one
);
1808 diff_free_filespec_data(two
);
1810 DIFF_OPT_SET(o
, CHECK_FAILED
);
1813 struct diff_filespec
*alloc_filespec(const char *path
)
1815 int namelen
= strlen(path
);
1816 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1818 memset(spec
, 0, sizeof(*spec
));
1819 spec
->path
= (char *)(spec
+ 1);
1820 memcpy(spec
->path
, path
, namelen
+1);
1822 spec
->is_binary
= -1;
1826 void free_filespec(struct diff_filespec
*spec
)
1828 if (!--spec
->count
) {
1829 diff_free_filespec_data(spec
);
1834 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1835 unsigned short mode
)
1838 spec
->mode
= canon_mode(mode
);
1839 hashcpy(spec
->sha1
, sha1
);
1840 spec
->sha1_valid
= !is_null_sha1(sha1
);
1845 * Given a name and sha1 pair, if the index tells us the file in
1846 * the work tree has that object contents, return true, so that
1847 * prepare_temp_file() does not have to inflate and extract.
1849 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1851 struct cache_entry
*ce
;
1856 * We do not read the cache ourselves here, because the
1857 * benchmark with my previous version that always reads cache
1858 * shows that it makes things worse for diff-tree comparing
1859 * two linux-2.6 kernel trees in an already checked out work
1860 * tree. This is because most diff-tree comparisons deal with
1861 * only a small number of files, while reading the cache is
1862 * expensive for a large project, and its cost outweighs the
1863 * savings we get by not inflating the object to a temporary
1864 * file. Practically, this code only helps when we are used
1865 * by diff-cache --cached, which does read the cache before
1871 /* We want to avoid the working directory if our caller
1872 * doesn't need the data in a normal file, this system
1873 * is rather slow with its stat/open/mmap/close syscalls,
1874 * and the object is contained in a pack file. The pack
1875 * is probably already open and will be faster to obtain
1876 * the data through than the working directory. Loose
1877 * objects however would tend to be slower as they need
1878 * to be individually opened and inflated.
1880 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
))
1884 pos
= cache_name_pos(name
, len
);
1887 ce
= active_cache
[pos
];
1890 * This is not the sha1 we are looking for, or
1891 * unreusable because it is not a regular file.
1893 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
1897 * If ce is marked as "assume unchanged", there is no
1898 * guarantee that work tree matches what we are looking for.
1900 if (ce
->ce_flags
& CE_VALID
)
1904 * If ce matches the file in the work tree, we can reuse it.
1906 if (ce_uptodate(ce
) ||
1907 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
1913 static int populate_from_stdin(struct diff_filespec
*s
)
1915 struct strbuf buf
= STRBUF_INIT
;
1918 if (strbuf_read(&buf
, 0, 0) < 0)
1919 return error("error while reading from stdin %s",
1922 s
->should_munmap
= 0;
1923 s
->data
= strbuf_detach(&buf
, &size
);
1929 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1932 char *data
= xmalloc(100);
1933 len
= snprintf(data
, 100,
1934 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1946 * While doing rename detection and pickaxe operation, we may need to
1947 * grab the data for the blob (or file) for our own in-core comparison.
1948 * diff_filespec has data and size fields for this purpose.
1950 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1953 if (!DIFF_FILE_VALID(s
))
1954 die("internal error: asking to populate invalid file.");
1955 if (S_ISDIR(s
->mode
))
1961 if (size_only
&& 0 < s
->size
)
1964 if (S_ISGITLINK(s
->mode
))
1965 return diff_populate_gitlink(s
, size_only
);
1967 if (!s
->sha1_valid
||
1968 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1969 struct strbuf buf
= STRBUF_INIT
;
1973 if (!strcmp(s
->path
, "-"))
1974 return populate_from_stdin(s
);
1976 if (lstat(s
->path
, &st
) < 0) {
1977 if (errno
== ENOENT
) {
1981 s
->data
= (char *)"";
1986 s
->size
= xsize_t(st
.st_size
);
1989 if (S_ISLNK(st
.st_mode
)) {
1990 struct strbuf sb
= STRBUF_INIT
;
1992 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
1995 s
->data
= strbuf_detach(&sb
, NULL
);
2001 fd
= open(s
->path
, O_RDONLY
);
2004 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2006 s
->should_munmap
= 1;
2009 * Convert from working tree format to canonical git format
2011 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
2013 munmap(s
->data
, s
->size
);
2014 s
->should_munmap
= 0;
2015 s
->data
= strbuf_detach(&buf
, &size
);
2021 enum object_type type
;
2023 type
= sha1_object_info(s
->sha1
, &s
->size
);
2025 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
2032 void diff_free_filespec_blob(struct diff_filespec
*s
)
2036 else if (s
->should_munmap
)
2037 munmap(s
->data
, s
->size
);
2039 if (s
->should_free
|| s
->should_munmap
) {
2040 s
->should_free
= s
->should_munmap
= 0;
2045 void diff_free_filespec_data(struct diff_filespec
*s
)
2047 diff_free_filespec_blob(s
);
2052 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
2055 const unsigned char *sha1
,
2059 struct strbuf buf
= STRBUF_INIT
;
2060 struct strbuf
template = STRBUF_INIT
;
2061 char *path_dup
= xstrdup(path
);
2062 const char *base
= basename(path_dup
);
2064 /* Generate "XXXXXX_basename.ext" */
2065 strbuf_addstr(&template, "XXXXXX_");
2066 strbuf_addstr(&template, base
);
2068 fd
= git_mkstemps(temp
->tmp_path
, PATH_MAX
, template.buf
,
2071 die_errno("unable to create temp-file");
2072 if (convert_to_working_tree(path
,
2073 (const char *)blob
, (size_t)size
, &buf
)) {
2077 if (write_in_full(fd
, blob
, size
) != size
)
2078 die_errno("unable to write temp-file");
2080 temp
->name
= temp
->tmp_path
;
2081 strcpy(temp
->hex
, sha1_to_hex(sha1
));
2083 sprintf(temp
->mode
, "%06o", mode
);
2084 strbuf_release(&buf
);
2085 strbuf_release(&template);
2089 static struct diff_tempfile
*prepare_temp_file(const char *name
,
2090 struct diff_filespec
*one
)
2092 struct diff_tempfile
*temp
= claim_diff_tempfile();
2094 if (!DIFF_FILE_VALID(one
)) {
2096 /* A '-' entry produces this for file-2, and
2097 * a '+' entry produces this for file-1.
2099 temp
->name
= "/dev/null";
2100 strcpy(temp
->hex
, ".");
2101 strcpy(temp
->mode
, ".");
2105 if (!remove_tempfile_installed
) {
2106 atexit(remove_tempfile
);
2107 sigchain_push_common(remove_tempfile_on_signal
);
2108 remove_tempfile_installed
= 1;
2111 if (!one
->sha1_valid
||
2112 reuse_worktree_file(name
, one
->sha1
, 1)) {
2114 if (lstat(name
, &st
) < 0) {
2115 if (errno
== ENOENT
)
2116 goto not_a_valid_file
;
2117 die_errno("stat(%s)", name
);
2119 if (S_ISLNK(st
.st_mode
)) {
2120 struct strbuf sb
= STRBUF_INIT
;
2121 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
2122 die_errno("readlink(%s)", name
);
2123 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
2125 one
->sha1
: null_sha1
),
2127 one
->mode
: S_IFLNK
));
2128 strbuf_release(&sb
);
2131 /* we can borrow from the file in the work tree */
2133 if (!one
->sha1_valid
)
2134 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
2136 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
2137 /* Even though we may sometimes borrow the
2138 * contents from the work tree, we always want
2139 * one->mode. mode is trustworthy even when
2140 * !(one->sha1_valid), as long as
2141 * DIFF_FILE_VALID(one).
2143 sprintf(temp
->mode
, "%06o", one
->mode
);
2148 if (diff_populate_filespec(one
, 0))
2149 die("cannot read data blob for %s", one
->path
);
2150 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
2151 one
->sha1
, one
->mode
);
2156 /* An external diff command takes:
2158 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2159 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2162 static void run_external_diff(const char *pgm
,
2165 struct diff_filespec
*one
,
2166 struct diff_filespec
*two
,
2167 const char *xfrm_msg
,
2168 int complete_rewrite
)
2170 const char *spawn_arg
[10];
2172 const char **arg
= &spawn_arg
[0];
2175 struct diff_tempfile
*temp_one
, *temp_two
;
2176 const char *othername
= (other
? other
: name
);
2177 temp_one
= prepare_temp_file(name
, one
);
2178 temp_two
= prepare_temp_file(othername
, two
);
2181 *arg
++ = temp_one
->name
;
2182 *arg
++ = temp_one
->hex
;
2183 *arg
++ = temp_one
->mode
;
2184 *arg
++ = temp_two
->name
;
2185 *arg
++ = temp_two
->hex
;
2186 *arg
++ = temp_two
->mode
;
2197 retval
= run_command_v_opt(spawn_arg
, 0);
2200 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2205 static int similarity_index(struct diff_filepair
*p
)
2207 return p
->score
* 100 / MAX_SCORE
;
2210 static void fill_metainfo(struct strbuf
*msg
,
2213 struct diff_filespec
*one
,
2214 struct diff_filespec
*two
,
2215 struct diff_options
*o
,
2216 struct diff_filepair
*p
)
2218 strbuf_init(msg
, PATH_MAX
* 2 + 300);
2219 switch (p
->status
) {
2220 case DIFF_STATUS_COPIED
:
2221 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2222 strbuf_addstr(msg
, "\ncopy from ");
2223 quote_c_style(name
, msg
, NULL
, 0);
2224 strbuf_addstr(msg
, "\ncopy to ");
2225 quote_c_style(other
, msg
, NULL
, 0);
2226 strbuf_addch(msg
, '\n');
2228 case DIFF_STATUS_RENAMED
:
2229 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2230 strbuf_addstr(msg
, "\nrename from ");
2231 quote_c_style(name
, msg
, NULL
, 0);
2232 strbuf_addstr(msg
, "\nrename to ");
2233 quote_c_style(other
, msg
, NULL
, 0);
2234 strbuf_addch(msg
, '\n');
2236 case DIFF_STATUS_MODIFIED
:
2238 strbuf_addf(msg
, "dissimilarity index %d%%\n",
2239 similarity_index(p
));
2247 if (one
&& two
&& hashcmp(one
->sha1
, two
->sha1
)) {
2248 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2250 if (DIFF_OPT_TST(o
, BINARY
)) {
2252 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2253 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2256 strbuf_addf(msg
, "index %.*s..%.*s",
2257 abbrev
, sha1_to_hex(one
->sha1
),
2258 abbrev
, sha1_to_hex(two
->sha1
));
2259 if (one
->mode
== two
->mode
)
2260 strbuf_addf(msg
, " %06o", one
->mode
);
2261 strbuf_addch(msg
, '\n');
2264 strbuf_setlen(msg
, msg
->len
- 1);
2267 static void run_diff_cmd(const char *pgm
,
2270 const char *attr_path
,
2271 struct diff_filespec
*one
,
2272 struct diff_filespec
*two
,
2274 struct diff_options
*o
,
2275 struct diff_filepair
*p
)
2277 const char *xfrm_msg
= NULL
;
2278 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
2281 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
);
2282 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
2285 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2288 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
2289 if (drv
&& drv
->external
)
2290 pgm
= drv
->external
;
2294 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2299 builtin_diff(name
, other
? other
: name
,
2300 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2302 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2305 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2307 if (DIFF_FILE_VALID(one
)) {
2308 if (!one
->sha1_valid
) {
2310 if (!strcmp(one
->path
, "-")) {
2311 hashcpy(one
->sha1
, null_sha1
);
2314 if (lstat(one
->path
, &st
) < 0)
2315 die_errno("stat '%s'", one
->path
);
2316 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2317 die("cannot hash %s", one
->path
);
2324 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2326 /* Strip the prefix but do not molest /dev/null and absolute paths */
2327 if (*namep
&& **namep
!= '/')
2328 *namep
+= prefix_length
;
2329 if (*otherp
&& **otherp
!= '/')
2330 *otherp
+= prefix_length
;
2333 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2335 const char *pgm
= external_diff();
2337 struct diff_filespec
*one
= p
->one
;
2338 struct diff_filespec
*two
= p
->two
;
2341 const char *attr_path
;
2343 name
= p
->one
->path
;
2344 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2346 if (o
->prefix_length
)
2347 strip_prefix(o
->prefix_length
, &name
, &other
);
2349 if (DIFF_PAIR_UNMERGED(p
)) {
2350 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2351 NULL
, NULL
, NULL
, o
, p
);
2355 diff_fill_sha1_info(one
);
2356 diff_fill_sha1_info(two
);
2359 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2360 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2362 * a filepair that changes between file and symlink
2363 * needs to be split into deletion and creation.
2365 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2366 run_diff_cmd(NULL
, name
, other
, attr_path
,
2367 one
, null
, &msg
, o
, p
);
2369 strbuf_release(&msg
);
2371 null
= alloc_filespec(one
->path
);
2372 run_diff_cmd(NULL
, name
, other
, attr_path
,
2373 null
, two
, &msg
, o
, p
);
2377 run_diff_cmd(pgm
, name
, other
, attr_path
,
2378 one
, two
, &msg
, o
, p
);
2380 strbuf_release(&msg
);
2383 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2384 struct diffstat_t
*diffstat
)
2388 int complete_rewrite
= 0;
2390 if (DIFF_PAIR_UNMERGED(p
)) {
2392 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2396 name
= p
->one
->path
;
2397 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2399 if (o
->prefix_length
)
2400 strip_prefix(o
->prefix_length
, &name
, &other
);
2402 diff_fill_sha1_info(p
->one
);
2403 diff_fill_sha1_info(p
->two
);
2405 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2406 complete_rewrite
= 1;
2407 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2410 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2414 const char *attr_path
;
2416 if (DIFF_PAIR_UNMERGED(p
)) {
2421 name
= p
->one
->path
;
2422 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2423 attr_path
= other
? other
: name
;
2425 if (o
->prefix_length
)
2426 strip_prefix(o
->prefix_length
, &name
, &other
);
2428 diff_fill_sha1_info(p
->one
);
2429 diff_fill_sha1_info(p
->two
);
2431 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2434 void diff_setup(struct diff_options
*options
)
2436 memset(options
, 0, sizeof(*options
));
2438 options
->file
= stdout
;
2440 options
->line_termination
= '\n';
2441 options
->break_opt
= -1;
2442 options
->rename_limit
= -1;
2443 options
->dirstat_percent
= 3;
2444 options
->context
= 3;
2446 options
->change
= diff_change
;
2447 options
->add_remove
= diff_addremove
;
2448 if (diff_use_color_default
> 0)
2449 DIFF_OPT_SET(options
, COLOR_DIFF
);
2450 options
->detect_rename
= diff_detect_rename_default
;
2452 if (!diff_mnemonic_prefix
) {
2453 options
->a_prefix
= "a/";
2454 options
->b_prefix
= "b/";
2458 int diff_setup_done(struct diff_options
*options
)
2462 if (options
->output_format
& DIFF_FORMAT_NAME
)
2464 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2466 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2468 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2471 die("--name-only, --name-status, --check and -s are mutually exclusive");
2473 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2474 options
->detect_rename
= DIFF_DETECT_COPY
;
2476 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2477 options
->prefix
= NULL
;
2478 if (options
->prefix
)
2479 options
->prefix_length
= strlen(options
->prefix
);
2481 options
->prefix_length
= 0;
2483 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2484 DIFF_FORMAT_NAME_STATUS
|
2485 DIFF_FORMAT_CHECKDIFF
|
2486 DIFF_FORMAT_NO_OUTPUT
))
2487 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2488 DIFF_FORMAT_NUMSTAT
|
2489 DIFF_FORMAT_DIFFSTAT
|
2490 DIFF_FORMAT_SHORTSTAT
|
2491 DIFF_FORMAT_DIRSTAT
|
2492 DIFF_FORMAT_SUMMARY
|
2496 * These cases always need recursive; we do not drop caller-supplied
2497 * recursive bits for other formats here.
2499 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2500 DIFF_FORMAT_NUMSTAT
|
2501 DIFF_FORMAT_DIFFSTAT
|
2502 DIFF_FORMAT_SHORTSTAT
|
2503 DIFF_FORMAT_DIRSTAT
|
2504 DIFF_FORMAT_SUMMARY
|
2505 DIFF_FORMAT_CHECKDIFF
))
2506 DIFF_OPT_SET(options
, RECURSIVE
);
2508 * Also pickaxe would not work very well if you do not say recursive
2510 if (options
->pickaxe
)
2511 DIFF_OPT_SET(options
, RECURSIVE
);
2513 if (options
->detect_rename
&& options
->rename_limit
< 0)
2514 options
->rename_limit
= diff_rename_limit_default
;
2515 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2517 /* read-cache does not die even when it fails
2518 * so it is safe for us to do this here. Also
2519 * it does not smudge active_cache or active_nr
2520 * when it fails, so we do not have to worry about
2521 * cleaning it up ourselves either.
2525 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2526 options
->abbrev
= 40; /* full */
2529 * It does not make sense to show the first hit we happened
2530 * to have found. It does not make sense not to return with
2531 * exit code in such a case either.
2533 if (DIFF_OPT_TST(options
, QUIET
)) {
2534 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2535 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2541 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2551 if (c
== arg_short
) {
2555 if (val
&& isdigit(c
)) {
2557 int n
= strtoul(arg
, &end
, 10);
2568 eq
= strchr(arg
, '=');
2573 if (!len
|| strncmp(arg
, arg_long
, len
))
2578 if (!isdigit(*++eq
))
2580 n
= strtoul(eq
, &end
, 10);
2588 static int diff_scoreopt_parse(const char *opt
);
2590 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2592 const char *arg
= av
[0];
2594 /* Output format options */
2595 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2596 options
->output_format
|= DIFF_FORMAT_PATCH
;
2597 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2598 options
->output_format
|= DIFF_FORMAT_PATCH
;
2599 else if (!strcmp(arg
, "--raw"))
2600 options
->output_format
|= DIFF_FORMAT_RAW
;
2601 else if (!strcmp(arg
, "--patch-with-raw"))
2602 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2603 else if (!strcmp(arg
, "--numstat"))
2604 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2605 else if (!strcmp(arg
, "--shortstat"))
2606 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2607 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2608 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2609 else if (!strcmp(arg
, "--cumulative")) {
2610 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2611 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
2612 } else if (opt_arg(arg
, 0, "dirstat-by-file",
2613 &options
->dirstat_percent
)) {
2614 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2615 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
2617 else if (!strcmp(arg
, "--check"))
2618 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2619 else if (!strcmp(arg
, "--summary"))
2620 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2621 else if (!strcmp(arg
, "--patch-with-stat"))
2622 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2623 else if (!strcmp(arg
, "--name-only"))
2624 options
->output_format
|= DIFF_FORMAT_NAME
;
2625 else if (!strcmp(arg
, "--name-status"))
2626 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2627 else if (!strcmp(arg
, "-s"))
2628 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2629 else if (!prefixcmp(arg
, "--stat")) {
2631 int width
= options
->stat_width
;
2632 int name_width
= options
->stat_name_width
;
2638 if (!prefixcmp(arg
, "-width="))
2639 width
= strtoul(arg
+ 7, &end
, 10);
2640 else if (!prefixcmp(arg
, "-name-width="))
2641 name_width
= strtoul(arg
+ 12, &end
, 10);
2644 width
= strtoul(arg
+1, &end
, 10);
2646 name_width
= strtoul(end
+1, &end
, 10);
2649 /* Important! This checks all the error cases! */
2652 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2653 options
->stat_name_width
= name_width
;
2654 options
->stat_width
= width
;
2657 /* renames options */
2658 else if (!prefixcmp(arg
, "-B")) {
2659 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2662 else if (!prefixcmp(arg
, "-M")) {
2663 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2665 options
->detect_rename
= DIFF_DETECT_RENAME
;
2667 else if (!prefixcmp(arg
, "-C")) {
2668 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2669 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2670 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2672 options
->detect_rename
= DIFF_DETECT_COPY
;
2674 else if (!strcmp(arg
, "--no-renames"))
2675 options
->detect_rename
= 0;
2676 else if (!strcmp(arg
, "--relative"))
2677 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2678 else if (!prefixcmp(arg
, "--relative=")) {
2679 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2680 options
->prefix
= arg
+ 11;
2684 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2685 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
2686 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2687 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
2688 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2689 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
2690 else if (!strcmp(arg
, "--patience"))
2691 DIFF_XDL_SET(options
, PATIENCE_DIFF
);
2694 else if (!strcmp(arg
, "--binary")) {
2695 options
->output_format
|= DIFF_FORMAT_PATCH
;
2696 DIFF_OPT_SET(options
, BINARY
);
2698 else if (!strcmp(arg
, "--full-index"))
2699 DIFF_OPT_SET(options
, FULL_INDEX
);
2700 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2701 DIFF_OPT_SET(options
, TEXT
);
2702 else if (!strcmp(arg
, "-R"))
2703 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2704 else if (!strcmp(arg
, "--find-copies-harder"))
2705 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2706 else if (!strcmp(arg
, "--follow"))
2707 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2708 else if (!strcmp(arg
, "--color"))
2709 DIFF_OPT_SET(options
, COLOR_DIFF
);
2710 else if (!strcmp(arg
, "--no-color"))
2711 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2712 else if (!strcmp(arg
, "--color-words")) {
2713 DIFF_OPT_SET(options
, COLOR_DIFF
);
2714 DIFF_OPT_SET(options
, COLOR_DIFF_WORDS
);
2716 else if (!prefixcmp(arg
, "--color-words=")) {
2717 DIFF_OPT_SET(options
, COLOR_DIFF
);
2718 DIFF_OPT_SET(options
, COLOR_DIFF_WORDS
);
2719 options
->word_regex
= arg
+ 14;
2721 else if (!strcmp(arg
, "--exit-code"))
2722 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2723 else if (!strcmp(arg
, "--quiet"))
2724 DIFF_OPT_SET(options
, QUIET
);
2725 else if (!strcmp(arg
, "--ext-diff"))
2726 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2727 else if (!strcmp(arg
, "--no-ext-diff"))
2728 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2729 else if (!strcmp(arg
, "--textconv"))
2730 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
2731 else if (!strcmp(arg
, "--no-textconv"))
2732 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
2733 else if (!strcmp(arg
, "--ignore-submodules"))
2734 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
2737 else if (!strcmp(arg
, "-z"))
2738 options
->line_termination
= 0;
2739 else if (!prefixcmp(arg
, "-l"))
2740 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2741 else if (!prefixcmp(arg
, "-S"))
2742 options
->pickaxe
= arg
+ 2;
2743 else if (!strcmp(arg
, "--pickaxe-all"))
2744 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2745 else if (!strcmp(arg
, "--pickaxe-regex"))
2746 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2747 else if (!prefixcmp(arg
, "-O"))
2748 options
->orderfile
= arg
+ 2;
2749 else if (!prefixcmp(arg
, "--diff-filter="))
2750 options
->filter
= arg
+ 14;
2751 else if (!strcmp(arg
, "--abbrev"))
2752 options
->abbrev
= DEFAULT_ABBREV
;
2753 else if (!prefixcmp(arg
, "--abbrev=")) {
2754 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2755 if (options
->abbrev
< MINIMUM_ABBREV
)
2756 options
->abbrev
= MINIMUM_ABBREV
;
2757 else if (40 < options
->abbrev
)
2758 options
->abbrev
= 40;
2760 else if (!prefixcmp(arg
, "--src-prefix="))
2761 options
->a_prefix
= arg
+ 13;
2762 else if (!prefixcmp(arg
, "--dst-prefix="))
2763 options
->b_prefix
= arg
+ 13;
2764 else if (!strcmp(arg
, "--no-prefix"))
2765 options
->a_prefix
= options
->b_prefix
= "";
2766 else if (opt_arg(arg
, '\0', "inter-hunk-context",
2767 &options
->interhunkcontext
))
2769 else if (!prefixcmp(arg
, "--output=")) {
2770 options
->file
= fopen(arg
+ strlen("--output="), "w");
2771 options
->close_file
= 1;
2777 static int parse_num(const char **cp_p
)
2779 unsigned long num
, scale
;
2781 const char *cp
= *cp_p
;
2788 if ( !dot
&& ch
== '.' ) {
2791 } else if ( ch
== '%' ) {
2792 scale
= dot
? scale
*100 : 100;
2793 cp
++; /* % is always at the end */
2795 } else if ( ch
>= '0' && ch
<= '9' ) {
2796 if ( scale
< 100000 ) {
2798 num
= (num
*10) + (ch
-'0');
2807 /* user says num divided by scale and we say internally that
2808 * is MAX_SCORE * num / scale.
2810 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2813 static int diff_scoreopt_parse(const char *opt
)
2815 int opt1
, opt2
, cmd
;
2820 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2821 return -1; /* that is not a -M, -C nor -B option */
2823 opt1
= parse_num(&opt
);
2829 else if (*opt
!= '/')
2830 return -1; /* we expect -B80/99 or -B80 */
2833 opt2
= parse_num(&opt
);
2838 return opt1
| (opt2
<< 16);
2841 struct diff_queue_struct diff_queued_diff
;
2843 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2845 if (queue
->alloc
<= queue
->nr
) {
2846 queue
->alloc
= alloc_nr(queue
->alloc
);
2847 queue
->queue
= xrealloc(queue
->queue
,
2848 sizeof(dp
) * queue
->alloc
);
2850 queue
->queue
[queue
->nr
++] = dp
;
2853 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2854 struct diff_filespec
*one
,
2855 struct diff_filespec
*two
)
2857 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2865 void diff_free_filepair(struct diff_filepair
*p
)
2867 free_filespec(p
->one
);
2868 free_filespec(p
->two
);
2872 /* This is different from find_unique_abbrev() in that
2873 * it stuffs the result with dots for alignment.
2875 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2880 return sha1_to_hex(sha1
);
2882 abbrev
= find_unique_abbrev(sha1
, len
);
2883 abblen
= strlen(abbrev
);
2885 static char hex
[41];
2886 if (len
< abblen
&& abblen
<= len
+ 2)
2887 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2889 sprintf(hex
, "%s...", abbrev
);
2892 return sha1_to_hex(sha1
);
2895 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
2897 int line_termination
= opt
->line_termination
;
2898 int inter_name_termination
= line_termination
? '\t' : '\0';
2900 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2901 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
2902 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
2903 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
2906 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
2907 inter_name_termination
);
2909 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
2912 if (p
->status
== DIFF_STATUS_COPIED
||
2913 p
->status
== DIFF_STATUS_RENAMED
) {
2914 const char *name_a
, *name_b
;
2915 name_a
= p
->one
->path
;
2916 name_b
= p
->two
->path
;
2917 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2918 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
2919 write_name_quoted(name_b
, opt
->file
, line_termination
);
2921 const char *name_a
, *name_b
;
2922 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
2924 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2925 write_name_quoted(name_a
, opt
->file
, line_termination
);
2929 int diff_unmodified_pair(struct diff_filepair
*p
)
2931 /* This function is written stricter than necessary to support
2932 * the currently implemented transformers, but the idea is to
2933 * let transformers to produce diff_filepairs any way they want,
2934 * and filter and clean them up here before producing the output.
2936 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
2938 if (DIFF_PAIR_UNMERGED(p
))
2939 return 0; /* unmerged is interesting */
2941 /* deletion, addition, mode or type change
2942 * and rename are all interesting.
2944 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2945 DIFF_PAIR_MODE_CHANGED(p
) ||
2946 strcmp(one
->path
, two
->path
))
2949 /* both are valid and point at the same path. that is, we are
2950 * dealing with a change.
2952 if (one
->sha1_valid
&& two
->sha1_valid
&&
2953 !hashcmp(one
->sha1
, two
->sha1
))
2954 return 1; /* no change */
2955 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2956 return 1; /* both look at the same file on the filesystem. */
2960 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2962 if (diff_unmodified_pair(p
))
2965 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2966 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2967 return; /* no tree diffs in patch format */
2972 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2973 struct diffstat_t
*diffstat
)
2975 if (diff_unmodified_pair(p
))
2978 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2979 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2980 return; /* no tree diffs in patch format */
2982 run_diffstat(p
, o
, diffstat
);
2985 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2986 struct diff_options
*o
)
2988 if (diff_unmodified_pair(p
))
2991 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2992 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2993 return; /* no tree diffs in patch format */
2995 run_checkdiff(p
, o
);
2998 int diff_queue_is_empty(void)
3000 struct diff_queue_struct
*q
= &diff_queued_diff
;
3002 for (i
= 0; i
< q
->nr
; i
++)
3003 if (!diff_unmodified_pair(q
->queue
[i
]))
3009 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
3011 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
3014 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
3016 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
3017 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
3019 s
->size
, s
->xfrm_flags
);
3022 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
3024 diff_debug_filespec(p
->one
, i
, "one");
3025 diff_debug_filespec(p
->two
, i
, "two");
3026 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
3027 p
->score
, p
->status
? p
->status
: '?',
3028 p
->one
->rename_used
, p
->broken_pair
);
3031 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
3035 fprintf(stderr
, "%s\n", msg
);
3036 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
3037 for (i
= 0; i
< q
->nr
; i
++) {
3038 struct diff_filepair
*p
= q
->queue
[i
];
3039 diff_debug_filepair(p
, i
);
3044 static void diff_resolve_rename_copy(void)
3047 struct diff_filepair
*p
;
3048 struct diff_queue_struct
*q
= &diff_queued_diff
;
3050 diff_debug_queue("resolve-rename-copy", q
);
3052 for (i
= 0; i
< q
->nr
; i
++) {
3054 p
->status
= 0; /* undecided */
3055 if (DIFF_PAIR_UNMERGED(p
))
3056 p
->status
= DIFF_STATUS_UNMERGED
;
3057 else if (!DIFF_FILE_VALID(p
->one
))
3058 p
->status
= DIFF_STATUS_ADDED
;
3059 else if (!DIFF_FILE_VALID(p
->two
))
3060 p
->status
= DIFF_STATUS_DELETED
;
3061 else if (DIFF_PAIR_TYPE_CHANGED(p
))
3062 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
3064 /* from this point on, we are dealing with a pair
3065 * whose both sides are valid and of the same type, i.e.
3066 * either in-place edit or rename/copy edit.
3068 else if (DIFF_PAIR_RENAME(p
)) {
3070 * A rename might have re-connected a broken
3071 * pair up, causing the pathnames to be the
3072 * same again. If so, that's not a rename at
3073 * all, just a modification..
3075 * Otherwise, see if this source was used for
3076 * multiple renames, in which case we decrement
3077 * the count, and call it a copy.
3079 if (!strcmp(p
->one
->path
, p
->two
->path
))
3080 p
->status
= DIFF_STATUS_MODIFIED
;
3081 else if (--p
->one
->rename_used
> 0)
3082 p
->status
= DIFF_STATUS_COPIED
;
3084 p
->status
= DIFF_STATUS_RENAMED
;
3086 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
3087 p
->one
->mode
!= p
->two
->mode
||
3088 is_null_sha1(p
->one
->sha1
))
3089 p
->status
= DIFF_STATUS_MODIFIED
;
3091 /* This is a "no-change" entry and should not
3092 * happen anymore, but prepare for broken callers.
3094 error("feeding unmodified %s to diffcore",
3096 p
->status
= DIFF_STATUS_UNKNOWN
;
3099 diff_debug_queue("resolve-rename-copy done", q
);
3102 static int check_pair_status(struct diff_filepair
*p
)
3104 switch (p
->status
) {
3105 case DIFF_STATUS_UNKNOWN
:
3108 die("internal error in diff-resolve-rename-copy");
3114 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
3116 int fmt
= opt
->output_format
;
3118 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
3119 diff_flush_checkdiff(p
, opt
);
3120 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
3121 diff_flush_raw(p
, opt
);
3122 else if (fmt
& DIFF_FORMAT_NAME
) {
3123 const char *name_a
, *name_b
;
3124 name_a
= p
->two
->path
;
3126 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3127 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
3131 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
3134 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
3136 fprintf(file
, " %s ", newdelete
);
3137 write_name_quoted(fs
->path
, file
, '\n');
3141 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
3143 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
3144 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
3145 show_name
? ' ' : '\n');
3147 write_name_quoted(p
->two
->path
, file
, '\n');
3152 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
3154 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
3156 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
3158 show_mode_change(file
, p
, 0);
3161 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
3164 case DIFF_STATUS_DELETED
:
3165 show_file_mode_name(file
, "delete", p
->one
);
3167 case DIFF_STATUS_ADDED
:
3168 show_file_mode_name(file
, "create", p
->two
);
3170 case DIFF_STATUS_COPIED
:
3171 show_rename_copy(file
, "copy", p
);
3173 case DIFF_STATUS_RENAMED
:
3174 show_rename_copy(file
, "rename", p
);
3178 fputs(" rewrite ", file
);
3179 write_name_quoted(p
->two
->path
, file
, ' ');
3180 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3182 show_mode_change(file
, p
, !p
->score
);
3192 static int remove_space(char *line
, int len
)
3198 for (i
= 0; i
< len
; i
++)
3199 if (!isspace((c
= line
[i
])))
3205 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3207 struct patch_id_t
*data
= priv
;
3210 /* Ignore line numbers when computing the SHA1 of the patch */
3211 if (!prefixcmp(line
, "@@ -"))
3214 new_len
= remove_space(line
, len
);
3216 git_SHA1_Update(data
->ctx
, line
, new_len
);
3217 data
->patchlen
+= new_len
;
3220 /* returns 0 upon success, and writes result into sha1 */
3221 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3223 struct diff_queue_struct
*q
= &diff_queued_diff
;
3226 struct patch_id_t data
;
3227 char buffer
[PATH_MAX
* 4 + 20];
3229 git_SHA1_Init(&ctx
);
3230 memset(&data
, 0, sizeof(struct patch_id_t
));
3233 for (i
= 0; i
< q
->nr
; i
++) {
3238 struct diff_filepair
*p
= q
->queue
[i
];
3241 memset(&xpp
, 0, sizeof(xpp
));
3242 memset(&xecfg
, 0, sizeof(xecfg
));
3244 return error("internal diff status error");
3245 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3247 if (diff_unmodified_pair(p
))
3249 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3250 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3252 if (DIFF_PAIR_UNMERGED(p
))
3255 diff_fill_sha1_info(p
->one
);
3256 diff_fill_sha1_info(p
->two
);
3257 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3258 fill_mmfile(&mf2
, p
->two
) < 0)
3259 return error("unable to read files to diff");
3261 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3262 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3263 if (p
->one
->mode
== 0)
3264 len1
= snprintf(buffer
, sizeof(buffer
),
3265 "diff--gita/%.*sb/%.*s"
3272 len2
, p
->two
->path
);
3273 else if (p
->two
->mode
== 0)
3274 len1
= snprintf(buffer
, sizeof(buffer
),
3275 "diff--gita/%.*sb/%.*s"
3276 "deletedfilemode%06o"
3282 len1
, p
->one
->path
);
3284 len1
= snprintf(buffer
, sizeof(buffer
),
3285 "diff--gita/%.*sb/%.*s"
3291 len2
, p
->two
->path
);
3292 git_SHA1_Update(&ctx
, buffer
, len1
);
3294 xpp
.flags
= XDF_NEED_MINIMAL
;
3296 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3297 xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
3298 &xpp
, &xecfg
, &ecb
);
3301 git_SHA1_Final(sha1
, &ctx
);
3305 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3307 struct diff_queue_struct
*q
= &diff_queued_diff
;
3309 int result
= diff_get_patch_id(options
, sha1
);
3311 for (i
= 0; i
< q
->nr
; i
++)
3312 diff_free_filepair(q
->queue
[i
]);
3316 q
->nr
= q
->alloc
= 0;
3321 static int is_summary_empty(const struct diff_queue_struct
*q
)
3325 for (i
= 0; i
< q
->nr
; i
++) {
3326 const struct diff_filepair
*p
= q
->queue
[i
];
3328 switch (p
->status
) {
3329 case DIFF_STATUS_DELETED
:
3330 case DIFF_STATUS_ADDED
:
3331 case DIFF_STATUS_COPIED
:
3332 case DIFF_STATUS_RENAMED
:
3337 if (p
->one
->mode
&& p
->two
->mode
&&
3338 p
->one
->mode
!= p
->two
->mode
)
3346 void diff_flush(struct diff_options
*options
)
3348 struct diff_queue_struct
*q
= &diff_queued_diff
;
3349 int i
, output_format
= options
->output_format
;
3353 * Order: raw, stat, summary, patch
3354 * or: name/name-status/checkdiff (other bits clear)
3359 if (output_format
& (DIFF_FORMAT_RAW
|
3361 DIFF_FORMAT_NAME_STATUS
|
3362 DIFF_FORMAT_CHECKDIFF
)) {
3363 for (i
= 0; i
< q
->nr
; i
++) {
3364 struct diff_filepair
*p
= q
->queue
[i
];
3365 if (check_pair_status(p
))
3366 flush_one_pair(p
, options
);
3371 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3372 struct diffstat_t diffstat
;
3374 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3375 for (i
= 0; i
< q
->nr
; i
++) {
3376 struct diff_filepair
*p
= q
->queue
[i
];
3377 if (check_pair_status(p
))
3378 diff_flush_stat(p
, options
, &diffstat
);
3380 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3381 show_numstat(&diffstat
, options
);
3382 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3383 show_stats(&diffstat
, options
);
3384 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3385 show_shortstats(&diffstat
, options
);
3386 free_diffstat_info(&diffstat
);
3389 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3390 show_dirstat(options
);
3392 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3393 for (i
= 0; i
< q
->nr
; i
++)
3394 diff_summary(options
->file
, q
->queue
[i
]);
3398 if (output_format
& DIFF_FORMAT_PATCH
) {
3400 putc(options
->line_termination
, options
->file
);
3401 if (options
->stat_sep
) {
3402 /* attach patch instead of inline */
3403 fputs(options
->stat_sep
, options
->file
);
3407 for (i
= 0; i
< q
->nr
; i
++) {
3408 struct diff_filepair
*p
= q
->queue
[i
];
3409 if (check_pair_status(p
))
3410 diff_flush_patch(p
, options
);
3414 if (output_format
& DIFF_FORMAT_CALLBACK
)
3415 options
->format_callback(q
, options
, options
->format_callback_data
);
3417 for (i
= 0; i
< q
->nr
; i
++)
3418 diff_free_filepair(q
->queue
[i
]);
3422 q
->nr
= q
->alloc
= 0;
3423 if (options
->close_file
)
3424 fclose(options
->file
);
3427 static void diffcore_apply_filter(const char *filter
)
3430 struct diff_queue_struct
*q
= &diff_queued_diff
;
3431 struct diff_queue_struct outq
;
3433 outq
.nr
= outq
.alloc
= 0;
3438 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3440 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3441 struct diff_filepair
*p
= q
->queue
[i
];
3442 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3444 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3446 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3447 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3448 strchr(filter
, p
->status
)))
3454 /* otherwise we will clear the whole queue
3455 * by copying the empty outq at the end of this
3456 * function, but first clear the current entries
3459 for (i
= 0; i
< q
->nr
; i
++)
3460 diff_free_filepair(q
->queue
[i
]);
3463 /* Only the matching ones */
3464 for (i
= 0; i
< q
->nr
; i
++) {
3465 struct diff_filepair
*p
= q
->queue
[i
];
3467 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3469 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3471 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3472 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3473 strchr(filter
, p
->status
)))
3476 diff_free_filepair(p
);
3483 /* Check whether two filespecs with the same mode and size are identical */
3484 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3485 struct diff_filespec
*two
)
3487 if (S_ISGITLINK(one
->mode
))
3489 if (diff_populate_filespec(one
, 0))
3491 if (diff_populate_filespec(two
, 0))
3493 return !memcmp(one
->data
, two
->data
, one
->size
);
3496 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3499 struct diff_queue_struct
*q
= &diff_queued_diff
;
3500 struct diff_queue_struct outq
;
3502 outq
.nr
= outq
.alloc
= 0;
3504 for (i
= 0; i
< q
->nr
; i
++) {
3505 struct diff_filepair
*p
= q
->queue
[i
];
3508 * 1. Entries that come from stat info dirtyness
3509 * always have both sides (iow, not create/delete),
3510 * one side of the object name is unknown, with
3511 * the same mode and size. Keep the ones that
3512 * do not match these criteria. They have real
3515 * 2. At this point, the file is known to be modified,
3516 * with the same mode and size, and the object
3517 * name of one side is unknown. Need to inspect
3518 * the identical contents.
3520 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3521 !DIFF_FILE_VALID(p
->two
) ||
3522 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3523 (p
->one
->mode
!= p
->two
->mode
) ||
3524 diff_populate_filespec(p
->one
, 1) ||
3525 diff_populate_filespec(p
->two
, 1) ||
3526 (p
->one
->size
!= p
->two
->size
) ||
3527 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3531 * The caller can subtract 1 from skip_stat_unmatch
3532 * to determine how many paths were dirty only
3533 * due to stat info mismatch.
3535 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3536 diffopt
->skip_stat_unmatch
++;
3537 diff_free_filepair(p
);
3544 void diffcore_std(struct diff_options
*options
)
3546 if (options
->skip_stat_unmatch
)
3547 diffcore_skip_stat_unmatch(options
);
3548 if (options
->break_opt
!= -1)
3549 diffcore_break(options
->break_opt
);
3550 if (options
->detect_rename
)
3551 diffcore_rename(options
);
3552 if (options
->break_opt
!= -1)
3553 diffcore_merge_broken();
3554 if (options
->pickaxe
)
3555 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3556 if (options
->orderfile
)
3557 diffcore_order(options
->orderfile
);
3558 diff_resolve_rename_copy();
3559 diffcore_apply_filter(options
->filter
);
3561 if (diff_queued_diff
.nr
)
3562 DIFF_OPT_SET(options
, HAS_CHANGES
);
3564 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3567 int diff_result_code(struct diff_options
*opt
, int status
)
3570 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3571 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3573 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3574 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3576 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3577 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3582 void diff_addremove(struct diff_options
*options
,
3583 int addremove
, unsigned mode
,
3584 const unsigned char *sha1
,
3585 const char *concatpath
)
3587 struct diff_filespec
*one
, *two
;
3589 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
3592 /* This may look odd, but it is a preparation for
3593 * feeding "there are unchanged files which should
3594 * not produce diffs, but when you are doing copy
3595 * detection you would need them, so here they are"
3596 * entries to the diff-core. They will be prefixed
3597 * with something like '=' or '*' (I haven't decided
3598 * which but should not make any difference).
3599 * Feeding the same new and old to diff_change()
3600 * also has the same effect.
3601 * Before the final output happens, they are pruned after
3602 * merged into rename/copy pairs as appropriate.
3604 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3605 addremove
= (addremove
== '+' ? '-' :
3606 addremove
== '-' ? '+' : addremove
);
3608 if (options
->prefix
&&
3609 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3612 one
= alloc_filespec(concatpath
);
3613 two
= alloc_filespec(concatpath
);
3615 if (addremove
!= '+')
3616 fill_filespec(one
, sha1
, mode
);
3617 if (addremove
!= '-')
3618 fill_filespec(two
, sha1
, mode
);
3620 diff_queue(&diff_queued_diff
, one
, two
);
3621 DIFF_OPT_SET(options
, HAS_CHANGES
);
3624 void diff_change(struct diff_options
*options
,
3625 unsigned old_mode
, unsigned new_mode
,
3626 const unsigned char *old_sha1
,
3627 const unsigned char *new_sha1
,
3628 const char *concatpath
)
3630 struct diff_filespec
*one
, *two
;
3632 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
3633 && S_ISGITLINK(new_mode
))
3636 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3638 const unsigned char *tmp_c
;
3639 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3640 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3643 if (options
->prefix
&&
3644 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3647 one
= alloc_filespec(concatpath
);
3648 two
= alloc_filespec(concatpath
);
3649 fill_filespec(one
, old_sha1
, old_mode
);
3650 fill_filespec(two
, new_sha1
, new_mode
);
3652 diff_queue(&diff_queued_diff
, one
, two
);
3653 DIFF_OPT_SET(options
, HAS_CHANGES
);
3656 void diff_unmerge(struct diff_options
*options
,
3658 unsigned mode
, const unsigned char *sha1
)
3660 struct diff_filespec
*one
, *two
;
3662 if (options
->prefix
&&
3663 strncmp(path
, options
->prefix
, options
->prefix_length
))
3666 one
= alloc_filespec(path
);
3667 two
= alloc_filespec(path
);
3668 fill_filespec(one
, sha1
, mode
);
3669 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;
3672 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
3675 struct diff_tempfile
*temp
;
3676 const char *argv
[3];
3677 const char **arg
= argv
;
3678 struct child_process child
;
3679 struct strbuf buf
= STRBUF_INIT
;
3681 temp
= prepare_temp_file(spec
->path
, spec
);
3683 *arg
++ = temp
->name
;
3686 memset(&child
, 0, sizeof(child
));
3689 if (start_command(&child
) != 0 ||
3690 strbuf_read(&buf
, child
.out
, 0) < 0 ||
3691 finish_command(&child
) != 0) {
3692 strbuf_release(&buf
);
3694 error("error running textconv command '%s'", pgm
);
3699 return strbuf_detach(&buf
, outsize
);