2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "run-command.h"
16 #include "submodule.h"
18 #ifdef NO_FAST_WORKING_DIRECTORY
19 #define FAST_WORKING_DIRECTORY 0
21 #define FAST_WORKING_DIRECTORY 1
24 static int diff_detect_rename_default
;
25 static int diff_rename_limit_default
= 200;
26 static int diff_suppress_blank_empty
;
27 int diff_use_color_default
= -1;
28 static const char *diff_word_regex_cfg
;
29 static const char *external_diff_cmd_cfg
;
30 int diff_auto_refresh_index
= 1;
31 static int diff_mnemonic_prefix
;
33 static char diff_colors
[][COLOR_MAXLEN
] = {
35 GIT_COLOR_NORMAL
, /* PLAIN */
36 GIT_COLOR_BOLD
, /* METAINFO */
37 GIT_COLOR_CYAN
, /* FRAGINFO */
38 GIT_COLOR_RED
, /* OLD */
39 GIT_COLOR_GREEN
, /* NEW */
40 GIT_COLOR_YELLOW
, /* COMMIT */
41 GIT_COLOR_BG_RED
, /* WHITESPACE */
42 GIT_COLOR_NORMAL
, /* FUNCINFO */
45 static void diff_filespec_load_driver(struct diff_filespec
*one
);
46 static char *run_textconv(const char *, struct diff_filespec
*, size_t *);
48 static int parse_diff_color_slot(const char *var
, int ofs
)
50 if (!strcasecmp(var
+ofs
, "plain"))
52 if (!strcasecmp(var
+ofs
, "meta"))
54 if (!strcasecmp(var
+ofs
, "frag"))
56 if (!strcasecmp(var
+ofs
, "old"))
58 if (!strcasecmp(var
+ofs
, "new"))
60 if (!strcasecmp(var
+ofs
, "commit"))
62 if (!strcasecmp(var
+ofs
, "whitespace"))
63 return DIFF_WHITESPACE
;
64 if (!strcasecmp(var
+ofs
, "func"))
69 static int git_config_rename(const char *var
, const char *value
)
72 return DIFF_DETECT_RENAME
;
73 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
74 return DIFF_DETECT_COPY
;
75 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
79 * These are to give UI layer defaults.
80 * The core-level commands such as git-diff-files should
81 * never be affected by the setting of diff.renames
82 * the user happens to have in the configuration file.
84 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
86 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
87 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
90 if (!strcmp(var
, "diff.renames")) {
91 diff_detect_rename_default
= git_config_rename(var
, value
);
94 if (!strcmp(var
, "diff.autorefreshindex")) {
95 diff_auto_refresh_index
= git_config_bool(var
, value
);
98 if (!strcmp(var
, "diff.mnemonicprefix")) {
99 diff_mnemonic_prefix
= git_config_bool(var
, value
);
102 if (!strcmp(var
, "diff.external"))
103 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
104 if (!strcmp(var
, "diff.wordregex"))
105 return git_config_string(&diff_word_regex_cfg
, var
, value
);
107 return git_diff_basic_config(var
, value
, cb
);
110 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
112 if (!strcmp(var
, "diff.renamelimit")) {
113 diff_rename_limit_default
= git_config_int(var
, value
);
117 switch (userdiff_config(var
, value
)) {
123 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
124 int slot
= parse_diff_color_slot(var
, 11);
128 return config_error_nonbool(var
);
129 color_parse(value
, var
, diff_colors
[slot
]);
133 /* like GNU diff's --suppress-blank-empty option */
134 if (!strcmp(var
, "diff.suppressblankempty") ||
135 /* for backwards compatibility */
136 !strcmp(var
, "diff.suppress-blank-empty")) {
137 diff_suppress_blank_empty
= git_config_bool(var
, value
);
141 return git_color_default_config(var
, value
, cb
);
144 static char *quote_two(const char *one
, const char *two
)
146 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
147 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
148 struct strbuf res
= STRBUF_INIT
;
150 if (need_one
+ need_two
) {
151 strbuf_addch(&res
, '"');
152 quote_c_style(one
, &res
, NULL
, 1);
153 quote_c_style(two
, &res
, NULL
, 1);
154 strbuf_addch(&res
, '"');
156 strbuf_addstr(&res
, one
);
157 strbuf_addstr(&res
, two
);
159 return strbuf_detach(&res
, NULL
);
162 static const char *external_diff(void)
164 static const char *external_diff_cmd
= NULL
;
165 static int done_preparing
= 0;
168 return external_diff_cmd
;
169 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
170 if (!external_diff_cmd
)
171 external_diff_cmd
= external_diff_cmd_cfg
;
173 return external_diff_cmd
;
176 static struct diff_tempfile
{
177 const char *name
; /* filename external diff should read from */
180 char tmp_path
[PATH_MAX
];
183 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
185 struct emit_callback
{
188 int blank_at_eof_in_preimage
;
189 int blank_at_eof_in_postimage
;
191 int lno_in_postimage
;
192 sane_truncate_fn truncate
;
193 const char **label_path
;
194 struct diff_words_data
*diff_words
;
197 struct strbuf
*header
;
200 static int count_lines(const char *data
, int size
)
202 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
209 completely_empty
= 0;
213 completely_empty
= 0;
216 if (completely_empty
)
219 count
++; /* no trailing newline */
223 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
225 if (!DIFF_FILE_VALID(one
)) {
226 mf
->ptr
= (char *)""; /* does not matter */
230 else if (diff_populate_filespec(one
, 0))
234 mf
->size
= one
->size
;
238 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
241 long size
= mf
->size
;
246 ptr
+= size
- 1; /* pointing at the very end */
248 ; /* incomplete line */
250 ptr
--; /* skip the last LF */
251 while (mf
->ptr
< ptr
) {
253 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
254 if (*prev_eol
== '\n')
256 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
264 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
265 struct emit_callback
*ecbdata
)
268 unsigned ws_rule
= ecbdata
->ws_rule
;
269 l1
= count_trailing_blank(mf1
, ws_rule
);
270 l2
= count_trailing_blank(mf2
, ws_rule
);
272 ecbdata
->blank_at_eof_in_preimage
= 0;
273 ecbdata
->blank_at_eof_in_postimage
= 0;
276 at
= count_lines(mf1
->ptr
, mf1
->size
);
277 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
279 at
= count_lines(mf2
->ptr
, mf2
->size
);
280 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
283 static void emit_line_0(FILE *file
, const char *set
, const char *reset
,
284 int first
, const char *line
, int len
)
286 int has_trailing_newline
, has_trailing_carriage_return
;
290 has_trailing_newline
= (first
== '\n');
291 has_trailing_carriage_return
= (!has_trailing_newline
&&
293 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
295 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
296 if (has_trailing_newline
)
298 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
299 if (has_trailing_carriage_return
)
304 if (len
|| !nofirst
) {
308 fwrite(line
, len
, 1, file
);
311 if (has_trailing_carriage_return
)
313 if (has_trailing_newline
)
317 static void emit_line(FILE *file
, const char *set
, const char *reset
,
318 const char *line
, int len
)
320 emit_line_0(file
, set
, reset
, line
[0], line
+1, len
-1);
323 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
325 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
326 ecbdata
->blank_at_eof_in_preimage
&&
327 ecbdata
->blank_at_eof_in_postimage
&&
328 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
329 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
331 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
334 static void emit_add_line(const char *reset
,
335 struct emit_callback
*ecbdata
,
336 const char *line
, int len
)
338 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
339 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
342 emit_line_0(ecbdata
->file
, set
, reset
, '+', line
, len
);
343 else if (new_blank_line_at_eof(ecbdata
, line
, len
))
344 /* Blank line at EOF - paint '+' as well */
345 emit_line_0(ecbdata
->file
, ws
, reset
, '+', line
, len
);
347 /* Emit just the prefix, then the rest. */
348 emit_line_0(ecbdata
->file
, set
, reset
, '+', "", 0);
349 ws_check_emit(line
, len
, ecbdata
->ws_rule
,
350 ecbdata
->file
, set
, reset
, ws
);
354 static void emit_hunk_header(struct emit_callback
*ecbdata
,
355 const char *line
, int len
)
357 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
358 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
359 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
360 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
361 static const char atat
[2] = { '@', '@' };
365 * As a hunk header must begin with "@@ -<old>, +<new> @@",
366 * it always is at least 10 bytes long.
369 memcmp(line
, atat
, 2) ||
370 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
371 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
374 ep
+= 2; /* skip over @@ */
376 /* The hunk header in fraginfo color */
377 emit_line(ecbdata
->file
, frag
, reset
, line
, ep
- line
);
379 /* blank before the func header */
380 for (cp
= ep
; ep
- line
< len
; ep
++)
381 if (*ep
!= ' ' && *ep
!= '\t')
384 emit_line(ecbdata
->file
, plain
, reset
, cp
, ep
- cp
);
387 emit_line(ecbdata
->file
, func
, reset
, ep
, line
+ len
- ep
);
390 static struct diff_tempfile
*claim_diff_tempfile(void) {
392 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
393 if (!diff_temp
[i
].name
)
394 return diff_temp
+ i
;
395 die("BUG: diff is failing to clean up its tempfiles");
398 static int remove_tempfile_installed
;
400 static void remove_tempfile(void)
403 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
404 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
)
405 unlink_or_warn(diff_temp
[i
].name
);
406 diff_temp
[i
].name
= NULL
;
410 static void remove_tempfile_on_signal(int signo
)
417 static void print_line_count(FILE *file
, int count
)
421 fprintf(file
, "0,0");
427 fprintf(file
, "1,%d", count
);
432 static void emit_rewrite_lines(struct emit_callback
*ecb
,
433 int prefix
, const char *data
, int size
)
435 const char *endp
= NULL
;
436 static const char *nneof
= " No newline at end of file\n";
437 const char *old
= diff_get_color(ecb
->color_diff
, DIFF_FILE_OLD
);
438 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
443 endp
= memchr(data
, '\n', size
);
444 len
= endp
? (endp
- data
+ 1) : size
;
446 ecb
->lno_in_preimage
++;
447 emit_line_0(ecb
->file
, old
, reset
, '-',
450 ecb
->lno_in_postimage
++;
451 emit_add_line(reset
, ecb
, data
, len
);
457 const char *plain
= diff_get_color(ecb
->color_diff
,
459 emit_line_0(ecb
->file
, plain
, reset
, '\\',
460 nneof
, strlen(nneof
));
464 static void emit_rewrite_diff(const char *name_a
,
466 struct diff_filespec
*one
,
467 struct diff_filespec
*two
,
468 const char *textconv_one
,
469 const char *textconv_two
,
470 struct diff_options
*o
)
473 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
474 const char *name_a_tab
, *name_b_tab
;
475 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
476 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
477 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
478 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
479 const char *a_prefix
, *b_prefix
;
480 const char *data_one
, *data_two
;
481 size_t size_one
, size_two
;
482 struct emit_callback ecbdata
;
484 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
485 a_prefix
= o
->b_prefix
;
486 b_prefix
= o
->a_prefix
;
488 a_prefix
= o
->a_prefix
;
489 b_prefix
= o
->b_prefix
;
492 name_a
+= (*name_a
== '/');
493 name_b
+= (*name_b
== '/');
494 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
495 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
497 strbuf_reset(&a_name
);
498 strbuf_reset(&b_name
);
499 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
500 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
502 diff_populate_filespec(one
, 0);
503 diff_populate_filespec(two
, 0);
505 data_one
= run_textconv(textconv_one
, one
, &size_one
);
507 die("unable to read files to diff");
510 data_one
= one
->data
;
511 size_one
= one
->size
;
514 data_two
= run_textconv(textconv_two
, two
, &size_two
);
516 die("unable to read files to diff");
519 data_two
= two
->data
;
520 size_two
= two
->size
;
523 memset(&ecbdata
, 0, sizeof(ecbdata
));
524 ecbdata
.color_diff
= color_diff
;
525 ecbdata
.found_changesp
= &o
->found_changes
;
526 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
527 ecbdata
.file
= o
->file
;
528 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
530 mf1
.ptr
= (char *)data_one
;
531 mf2
.ptr
= (char *)data_two
;
534 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
536 ecbdata
.lno_in_preimage
= 1;
537 ecbdata
.lno_in_postimage
= 1;
539 lc_a
= count_lines(data_one
, size_one
);
540 lc_b
= count_lines(data_two
, size_two
);
542 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
543 metainfo
, a_name
.buf
, name_a_tab
, reset
,
544 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
545 print_line_count(o
->file
, lc_a
);
546 fprintf(o
->file
, " +");
547 print_line_count(o
->file
, lc_b
);
548 fprintf(o
->file
, " @@%s\n", reset
);
550 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
552 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
555 struct diff_words_buffer
{
558 struct diff_words_orig
{
559 const char *begin
, *end
;
561 int orig_nr
, orig_alloc
;
564 static void diff_words_append(char *line
, unsigned long len
,
565 struct diff_words_buffer
*buffer
)
567 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
570 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
571 buffer
->text
.size
+= len
;
572 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
575 struct diff_words_data
{
576 struct diff_words_buffer minus
, plus
;
577 const char *current_plus
;
582 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
584 struct diff_words_data
*diff_words
= priv
;
585 int minus_first
, minus_len
, plus_first
, plus_len
;
586 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
588 if (line
[0] != '@' || parse_hunk_header(line
, len
,
589 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
592 /* POSIX requires that first be decremented by one if len == 0... */
594 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
596 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
598 minus_begin
= minus_end
=
599 diff_words
->minus
.orig
[minus_first
].end
;
602 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
603 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
605 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
607 if (diff_words
->current_plus
!= plus_begin
)
608 fwrite(diff_words
->current_plus
,
609 plus_begin
- diff_words
->current_plus
, 1,
611 if (minus_begin
!= minus_end
)
612 color_fwrite_lines(diff_words
->file
,
613 diff_get_color(1, DIFF_FILE_OLD
),
614 minus_end
- minus_begin
, minus_begin
);
615 if (plus_begin
!= plus_end
)
616 color_fwrite_lines(diff_words
->file
,
617 diff_get_color(1, DIFF_FILE_NEW
),
618 plus_end
- plus_begin
, plus_begin
);
620 diff_words
->current_plus
= plus_end
;
623 /* This function starts looking at *begin, and returns 0 iff a word was found. */
624 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
625 int *begin
, int *end
)
627 if (word_regex
&& *begin
< buffer
->size
) {
629 if (!regexec(word_regex
, buffer
->ptr
+ *begin
, 1, match
, 0)) {
630 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
631 '\n', match
[0].rm_eo
- match
[0].rm_so
);
632 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
633 *begin
+= match
[0].rm_so
;
634 return *begin
>= *end
;
639 /* find the next word */
640 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
642 if (*begin
>= buffer
->size
)
645 /* find the end of the word */
647 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
654 * This function splits the words in buffer->text, stores the list with
655 * newline separator into out, and saves the offsets of the original words
658 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
667 /* fake an empty "0th" word */
668 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
669 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
672 for (i
= 0; i
< buffer
->text
.size
; i
++) {
673 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
676 /* store original boundaries */
677 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
679 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
680 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
684 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
685 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
686 out
->ptr
[out
->size
+ j
- i
] = '\n';
687 out
->size
+= j
- i
+ 1;
693 /* this executes the word diff on the accumulated buffers */
694 static void diff_words_show(struct diff_words_data
*diff_words
)
699 mmfile_t minus
, plus
;
701 /* special case: only removal */
702 if (!diff_words
->plus
.text
.size
) {
703 color_fwrite_lines(diff_words
->file
,
704 diff_get_color(1, DIFF_FILE_OLD
),
705 diff_words
->minus
.text
.size
, diff_words
->minus
.text
.ptr
);
706 diff_words
->minus
.text
.size
= 0;
710 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
712 memset(&xpp
, 0, sizeof(xpp
));
713 memset(&xecfg
, 0, sizeof(xecfg
));
714 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
715 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
716 xpp
.flags
= XDF_NEED_MINIMAL
;
717 /* as only the hunk header will be parsed, we need a 0-context */
719 xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
723 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
724 diff_words
->plus
.text
.size
)
725 fwrite(diff_words
->current_plus
,
726 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
727 - diff_words
->current_plus
, 1,
729 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
732 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
733 static void diff_words_flush(struct emit_callback
*ecbdata
)
735 if (ecbdata
->diff_words
->minus
.text
.size
||
736 ecbdata
->diff_words
->plus
.text
.size
)
737 diff_words_show(ecbdata
->diff_words
);
740 static void free_diff_words_data(struct emit_callback
*ecbdata
)
742 if (ecbdata
->diff_words
) {
743 diff_words_flush(ecbdata
);
744 free (ecbdata
->diff_words
->minus
.text
.ptr
);
745 free (ecbdata
->diff_words
->minus
.orig
);
746 free (ecbdata
->diff_words
->plus
.text
.ptr
);
747 free (ecbdata
->diff_words
->plus
.orig
);
748 free(ecbdata
->diff_words
->word_regex
);
749 free(ecbdata
->diff_words
);
750 ecbdata
->diff_words
= NULL
;
754 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
757 return diff_colors
[ix
];
761 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
768 return ecb
->truncate(line
, len
);
772 (void) utf8_width(&cp
, &l
);
774 break; /* truncated in the middle? */
779 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
782 ecbdata
->lno_in_preimage
= 0;
783 ecbdata
->lno_in_postimage
= 0;
784 p
= strchr(line
, '-');
786 return; /* cannot happen */
787 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
790 return; /* cannot happen */
791 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
794 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
796 struct emit_callback
*ecbdata
= priv
;
797 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
798 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
799 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
801 if (ecbdata
->header
) {
802 fprintf(ecbdata
->file
, "%s", ecbdata
->header
->buf
);
803 strbuf_reset(ecbdata
->header
);
804 ecbdata
->header
= NULL
;
806 *(ecbdata
->found_changesp
) = 1;
808 if (ecbdata
->label_path
[0]) {
809 const char *name_a_tab
, *name_b_tab
;
811 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
812 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
814 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
815 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
816 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
817 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
818 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
821 if (diff_suppress_blank_empty
822 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
827 if (line
[0] == '@') {
828 if (ecbdata
->diff_words
)
829 diff_words_flush(ecbdata
);
830 len
= sane_truncate_line(ecbdata
, line
, len
);
831 find_lno(line
, ecbdata
);
832 emit_hunk_header(ecbdata
, line
, len
);
833 if (line
[len
-1] != '\n')
834 putc('\n', ecbdata
->file
);
839 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
843 if (ecbdata
->diff_words
) {
844 if (line
[0] == '-') {
845 diff_words_append(line
, len
,
846 &ecbdata
->diff_words
->minus
);
848 } else if (line
[0] == '+') {
849 diff_words_append(line
, len
,
850 &ecbdata
->diff_words
->plus
);
853 diff_words_flush(ecbdata
);
856 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
860 if (line
[0] != '+') {
862 diff_get_color(ecbdata
->color_diff
,
863 line
[0] == '-' ? DIFF_FILE_OLD
: DIFF_PLAIN
);
864 ecbdata
->lno_in_preimage
++;
866 ecbdata
->lno_in_postimage
++;
867 emit_line(ecbdata
->file
, color
, reset
, line
, len
);
869 ecbdata
->lno_in_postimage
++;
870 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
874 static char *pprint_rename(const char *a
, const char *b
)
878 struct strbuf name
= STRBUF_INIT
;
879 int pfx_length
, sfx_length
;
880 int len_a
= strlen(a
);
881 int len_b
= strlen(b
);
882 int a_midlen
, b_midlen
;
883 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
884 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
886 if (qlen_a
|| qlen_b
) {
887 quote_c_style(a
, &name
, NULL
, 0);
888 strbuf_addstr(&name
, " => ");
889 quote_c_style(b
, &name
, NULL
, 0);
890 return strbuf_detach(&name
, NULL
);
893 /* Find common prefix */
895 while (*old
&& *new && *old
== *new) {
897 pfx_length
= old
- a
+ 1;
902 /* Find common suffix */
906 while (a
<= old
&& b
<= new && *old
== *new) {
908 sfx_length
= len_a
- (old
- a
);
914 * pfx{mid-a => mid-b}sfx
915 * {pfx-a => pfx-b}sfx
916 * pfx{sfx-a => sfx-b}
919 a_midlen
= len_a
- pfx_length
- sfx_length
;
920 b_midlen
= len_b
- pfx_length
- sfx_length
;
926 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
927 if (pfx_length
+ sfx_length
) {
928 strbuf_add(&name
, a
, pfx_length
);
929 strbuf_addch(&name
, '{');
931 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
932 strbuf_addstr(&name
, " => ");
933 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
934 if (pfx_length
+ sfx_length
) {
935 strbuf_addch(&name
, '}');
936 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
938 return strbuf_detach(&name
, NULL
);
944 struct diffstat_file
{
948 unsigned is_unmerged
:1;
949 unsigned is_binary
:1;
950 unsigned is_renamed
:1;
951 unsigned int added
, deleted
;
955 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
959 struct diffstat_file
*x
;
960 x
= xcalloc(sizeof (*x
), 1);
961 if (diffstat
->nr
== diffstat
->alloc
) {
962 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
963 diffstat
->files
= xrealloc(diffstat
->files
,
964 diffstat
->alloc
* sizeof(x
));
966 diffstat
->files
[diffstat
->nr
++] = x
;
968 x
->from_name
= xstrdup(name_a
);
969 x
->name
= xstrdup(name_b
);
974 x
->name
= xstrdup(name_a
);
979 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
981 struct diffstat_t
*diffstat
= priv
;
982 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
986 else if (line
[0] == '-')
990 const char mime_boundary_leader
[] = "------------";
992 static int scale_linear(int it
, int width
, int max_change
)
995 * make sure that at least one '-' is printed if there were deletions,
996 * and likewise for '+'.
1000 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
1003 static void show_name(FILE *file
,
1004 const char *prefix
, const char *name
, int len
)
1006 fprintf(file
, " %s%-*s |", prefix
, len
, name
);
1009 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
1013 fprintf(file
, "%s", set
);
1016 fprintf(file
, "%s", reset
);
1019 static void fill_print_name(struct diffstat_file
*file
)
1023 if (file
->print_name
)
1026 if (!file
->is_renamed
) {
1027 struct strbuf buf
= STRBUF_INIT
;
1028 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
1029 pname
= strbuf_detach(&buf
, NULL
);
1032 strbuf_release(&buf
);
1035 pname
= pprint_rename(file
->from_name
, file
->name
);
1037 file
->print_name
= pname
;
1040 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
1042 int i
, len
, add
, del
, adds
= 0, dels
= 0;
1043 int max_change
= 0, max_len
= 0;
1044 int total_files
= data
->nr
;
1045 int width
, name_width
;
1046 const char *reset
, *set
, *add_c
, *del_c
;
1051 width
= options
->stat_width
? options
->stat_width
: 80;
1052 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
1054 /* Sanity: give at least 5 columns to the graph,
1055 * but leave at least 10 columns for the name.
1059 if (name_width
< 10)
1061 else if (width
< name_width
+ 15)
1062 name_width
= width
- 15;
1064 /* Find the longest filename and max number of changes */
1065 reset
= diff_get_color_opt(options
, DIFF_RESET
);
1066 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
1067 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
1068 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
1070 for (i
= 0; i
< data
->nr
; i
++) {
1071 struct diffstat_file
*file
= data
->files
[i
];
1072 int change
= file
->added
+ file
->deleted
;
1073 fill_print_name(file
);
1074 len
= strlen(file
->print_name
);
1078 if (file
->is_binary
|| file
->is_unmerged
)
1080 if (max_change
< change
)
1081 max_change
= change
;
1084 /* Compute the width of the graph part;
1085 * 10 is for one blank at the beginning of the line plus
1086 * " | count " between the name and the graph.
1088 * From here on, name_width is the width of the name area,
1089 * and width is the width of the graph area.
1091 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
1092 if (width
< (name_width
+ 10) + max_change
)
1093 width
= width
- (name_width
+ 10);
1097 for (i
= 0; i
< data
->nr
; i
++) {
1098 const char *prefix
= "";
1099 char *name
= data
->files
[i
]->print_name
;
1100 int added
= data
->files
[i
]->added
;
1101 int deleted
= data
->files
[i
]->deleted
;
1105 * "scale" the filename
1108 name_len
= strlen(name
);
1109 if (name_width
< name_len
) {
1113 name
+= name_len
- len
;
1114 slash
= strchr(name
, '/');
1119 if (data
->files
[i
]->is_binary
) {
1120 show_name(options
->file
, prefix
, name
, len
);
1121 fprintf(options
->file
, " Bin ");
1122 fprintf(options
->file
, "%s%d%s", del_c
, deleted
, reset
);
1123 fprintf(options
->file
, " -> ");
1124 fprintf(options
->file
, "%s%d%s", add_c
, added
, reset
);
1125 fprintf(options
->file
, " bytes");
1126 fprintf(options
->file
, "\n");
1129 else if (data
->files
[i
]->is_unmerged
) {
1130 show_name(options
->file
, prefix
, name
, len
);
1131 fprintf(options
->file
, " Unmerged\n");
1134 else if (!data
->files
[i
]->is_renamed
&&
1135 (added
+ deleted
== 0)) {
1141 * scale the add/delete
1148 if (width
<= max_change
) {
1149 add
= scale_linear(add
, width
, max_change
);
1150 del
= scale_linear(del
, width
, max_change
);
1152 show_name(options
->file
, prefix
, name
, len
);
1153 fprintf(options
->file
, "%5d%s", added
+ deleted
,
1154 added
+ deleted
? " " : "");
1155 show_graph(options
->file
, '+', add
, add_c
, reset
);
1156 show_graph(options
->file
, '-', del
, del_c
, reset
);
1157 fprintf(options
->file
, "\n");
1159 fprintf(options
->file
,
1160 " %d files changed, %d insertions(+), %d deletions(-)\n",
1161 total_files
, adds
, dels
);
1164 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
1166 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
1171 for (i
= 0; i
< data
->nr
; i
++) {
1172 if (!data
->files
[i
]->is_binary
&&
1173 !data
->files
[i
]->is_unmerged
) {
1174 int added
= data
->files
[i
]->added
;
1175 int deleted
= data
->files
[i
]->deleted
;
1176 if (!data
->files
[i
]->is_renamed
&&
1177 (added
+ deleted
== 0)) {
1185 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
1186 total_files
, adds
, dels
);
1189 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
1196 for (i
= 0; i
< data
->nr
; i
++) {
1197 struct diffstat_file
*file
= data
->files
[i
];
1199 if (file
->is_binary
)
1200 fprintf(options
->file
, "-\t-\t");
1202 fprintf(options
->file
,
1203 "%d\t%d\t", file
->added
, file
->deleted
);
1204 if (options
->line_termination
) {
1205 fill_print_name(file
);
1206 if (!file
->is_renamed
)
1207 write_name_quoted(file
->name
, options
->file
,
1208 options
->line_termination
);
1210 fputs(file
->print_name
, options
->file
);
1211 putc(options
->line_termination
, options
->file
);
1214 if (file
->is_renamed
) {
1215 putc('\0', options
->file
);
1216 write_name_quoted(file
->from_name
, options
->file
, '\0');
1218 write_name_quoted(file
->name
, options
->file
, '\0');
1223 struct dirstat_file
{
1225 unsigned long changed
;
1228 struct dirstat_dir
{
1229 struct dirstat_file
*files
;
1230 int alloc
, nr
, percent
, cumulative
;
1233 static long gather_dirstat(FILE *file
, struct dirstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1235 unsigned long this_dir
= 0;
1236 unsigned int sources
= 0;
1239 struct dirstat_file
*f
= dir
->files
;
1240 int namelen
= strlen(f
->name
);
1244 if (namelen
< baselen
)
1246 if (memcmp(f
->name
, base
, baselen
))
1248 slash
= strchr(f
->name
+ baselen
, '/');
1250 int newbaselen
= slash
+ 1 - f
->name
;
1251 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1263 * We don't report dirstat's for
1265 * - or cases where everything came from a single directory
1266 * under this directory (sources == 1).
1268 if (baselen
&& sources
!= 1) {
1269 int permille
= this_dir
* 1000 / changed
;
1271 int percent
= permille
/ 10;
1272 if (percent
>= dir
->percent
) {
1273 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1274 if (!dir
->cumulative
)
1282 static int dirstat_compare(const void *_a
, const void *_b
)
1284 const struct dirstat_file
*a
= _a
;
1285 const struct dirstat_file
*b
= _b
;
1286 return strcmp(a
->name
, b
->name
);
1289 static void show_dirstat(struct diff_options
*options
)
1292 unsigned long changed
;
1293 struct dirstat_dir dir
;
1294 struct diff_queue_struct
*q
= &diff_queued_diff
;
1299 dir
.percent
= options
->dirstat_percent
;
1300 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1303 for (i
= 0; i
< q
->nr
; i
++) {
1304 struct diff_filepair
*p
= q
->queue
[i
];
1306 unsigned long copied
, added
, damage
;
1308 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1310 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1311 diff_populate_filespec(p
->one
, 0);
1312 diff_populate_filespec(p
->two
, 0);
1313 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1315 diff_free_filespec_data(p
->one
);
1316 diff_free_filespec_data(p
->two
);
1317 } else if (DIFF_FILE_VALID(p
->one
)) {
1318 diff_populate_filespec(p
->one
, 1);
1320 diff_free_filespec_data(p
->one
);
1321 } else if (DIFF_FILE_VALID(p
->two
)) {
1322 diff_populate_filespec(p
->two
, 1);
1324 added
= p
->two
->size
;
1325 diff_free_filespec_data(p
->two
);
1330 * Original minus copied is the removed material,
1331 * added is the new material. They are both damages
1332 * made to the preimage. In --dirstat-by-file mode, count
1333 * damaged files, not damaged lines. This is done by
1334 * counting only a single damaged line per file.
1336 damage
= (p
->one
->size
- copied
) + added
;
1337 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
) && damage
> 0)
1340 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1341 dir
.files
[dir
.nr
].name
= name
;
1342 dir
.files
[dir
.nr
].changed
= damage
;
1347 /* This can happen even with many files, if everything was renames */
1351 /* Show all directories with more than x% of the changes */
1352 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
1353 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1356 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1359 for (i
= 0; i
< diffstat
->nr
; i
++) {
1360 struct diffstat_file
*f
= diffstat
->files
[i
];
1361 if (f
->name
!= f
->print_name
)
1362 free(f
->print_name
);
1367 free(diffstat
->files
);
1370 struct checkdiff_t
{
1371 const char *filename
;
1373 struct diff_options
*o
;
1378 static int is_conflict_marker(const char *line
, unsigned long len
)
1385 firstchar
= line
[0];
1386 switch (firstchar
) {
1387 case '=': case '>': case '<':
1392 for (cnt
= 1; cnt
< 7; cnt
++)
1393 if (line
[cnt
] != firstchar
)
1395 /* line[0] thru line[6] are same as firstchar */
1396 if (firstchar
== '=') {
1397 /* divider between ours and theirs? */
1398 if (len
!= 8 || line
[7] != '\n')
1400 } else if (len
< 8 || !isspace(line
[7])) {
1401 /* not divider before ours nor after theirs */
1407 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1409 struct checkdiff_t
*data
= priv
;
1410 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1411 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1412 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1413 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1416 if (line
[0] == '+') {
1419 if (is_conflict_marker(line
+ 1, len
- 1)) {
1421 fprintf(data
->o
->file
,
1422 "%s:%d: leftover conflict marker\n",
1423 data
->filename
, data
->lineno
);
1425 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1428 data
->status
|= bad
;
1429 err
= whitespace_error_string(bad
);
1430 fprintf(data
->o
->file
, "%s:%d: %s.\n",
1431 data
->filename
, data
->lineno
, err
);
1433 emit_line(data
->o
->file
, set
, reset
, line
, 1);
1434 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1435 data
->o
->file
, set
, reset
, ws
);
1436 } else if (line
[0] == ' ') {
1438 } else if (line
[0] == '@') {
1439 char *plus
= strchr(line
, '+');
1441 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1443 die("invalid diff");
1447 static unsigned char *deflate_it(char *data
,
1449 unsigned long *result_size
)
1452 unsigned char *deflated
;
1455 memset(&stream
, 0, sizeof(stream
));
1456 deflateInit(&stream
, zlib_compression_level
);
1457 bound
= deflateBound(&stream
, size
);
1458 deflated
= xmalloc(bound
);
1459 stream
.next_out
= deflated
;
1460 stream
.avail_out
= bound
;
1462 stream
.next_in
= (unsigned char *)data
;
1463 stream
.avail_in
= size
;
1464 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1466 deflateEnd(&stream
);
1467 *result_size
= stream
.total_out
;
1471 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1477 unsigned long orig_size
;
1478 unsigned long delta_size
;
1479 unsigned long deflate_size
;
1480 unsigned long data_size
;
1482 /* We could do deflated delta, or we could do just deflated two,
1483 * whichever is smaller.
1486 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1487 if (one
->size
&& two
->size
) {
1488 delta
= diff_delta(one
->ptr
, one
->size
,
1489 two
->ptr
, two
->size
,
1490 &delta_size
, deflate_size
);
1492 void *to_free
= delta
;
1493 orig_size
= delta_size
;
1494 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1499 if (delta
&& delta_size
< deflate_size
) {
1500 fprintf(file
, "delta %lu\n", orig_size
);
1503 data_size
= delta_size
;
1506 fprintf(file
, "literal %lu\n", two
->size
);
1509 data_size
= deflate_size
;
1512 /* emit data encoded in base85 */
1515 int bytes
= (52 < data_size
) ? 52 : data_size
;
1519 line
[0] = bytes
+ 'A' - 1;
1521 line
[0] = bytes
- 26 + 'a' - 1;
1522 encode_85(line
+ 1, cp
, bytes
);
1523 cp
= (char *) cp
+ bytes
;
1527 fprintf(file
, "\n");
1531 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1533 fprintf(file
, "GIT binary patch\n");
1534 emit_binary_diff_body(file
, one
, two
);
1535 emit_binary_diff_body(file
, two
, one
);
1538 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1541 one
->driver
= userdiff_find_by_path(one
->path
);
1543 one
->driver
= userdiff_find_by_name("default");
1546 int diff_filespec_is_binary(struct diff_filespec
*one
)
1548 if (one
->is_binary
== -1) {
1549 diff_filespec_load_driver(one
);
1550 if (one
->driver
->binary
!= -1)
1551 one
->is_binary
= one
->driver
->binary
;
1553 if (!one
->data
&& DIFF_FILE_VALID(one
))
1554 diff_populate_filespec(one
, 0);
1556 one
->is_binary
= buffer_is_binary(one
->data
,
1558 if (one
->is_binary
== -1)
1562 return one
->is_binary
;
1565 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
1567 diff_filespec_load_driver(one
);
1568 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
1571 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1573 diff_filespec_load_driver(one
);
1574 return one
->driver
->word_regex
;
1577 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
1579 if (!options
->a_prefix
)
1580 options
->a_prefix
= a
;
1581 if (!options
->b_prefix
)
1582 options
->b_prefix
= b
;
1585 static const char *get_textconv(struct diff_filespec
*one
)
1587 if (!DIFF_FILE_VALID(one
))
1589 if (!S_ISREG(one
->mode
))
1591 diff_filespec_load_driver(one
);
1592 return one
->driver
->textconv
;
1595 static void builtin_diff(const char *name_a
,
1597 struct diff_filespec
*one
,
1598 struct diff_filespec
*two
,
1599 const char *xfrm_msg
,
1600 struct diff_options
*o
,
1601 int complete_rewrite
)
1605 char *a_one
, *b_two
;
1606 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1607 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1608 const char *a_prefix
, *b_prefix
;
1609 const char *textconv_one
= NULL
, *textconv_two
= NULL
;
1610 struct strbuf header
= STRBUF_INIT
;
1612 if (DIFF_OPT_TST(o
, SUBMODULE_LOG
) &&
1613 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
1614 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
1615 const char *del
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1616 const char *add
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1617 show_submodule_summary(o
->file
, one
? one
->path
: two
->path
,
1618 one
->sha1
, two
->sha1
, two
->dirty_submodule
,
1623 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
1624 textconv_one
= get_textconv(one
);
1625 textconv_two
= get_textconv(two
);
1628 diff_set_mnemonic_prefix(o
, "a/", "b/");
1629 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
1630 a_prefix
= o
->b_prefix
;
1631 b_prefix
= o
->a_prefix
;
1633 a_prefix
= o
->a_prefix
;
1634 b_prefix
= o
->b_prefix
;
1637 /* Never use a non-valid filename anywhere if at all possible */
1638 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
1639 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
1641 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
1642 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
1643 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1644 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1645 strbuf_addf(&header
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1646 if (lbl
[0][0] == '/') {
1648 strbuf_addf(&header
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1649 if (xfrm_msg
&& xfrm_msg
[0])
1650 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1652 else if (lbl
[1][0] == '/') {
1653 strbuf_addf(&header
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1654 if (xfrm_msg
&& xfrm_msg
[0])
1655 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1658 if (one
->mode
!= two
->mode
) {
1659 strbuf_addf(&header
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1660 strbuf_addf(&header
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1662 if (xfrm_msg
&& xfrm_msg
[0])
1663 strbuf_addf(&header
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1666 * we do not run diff between different kind
1669 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1670 goto free_ab_and_return
;
1671 if (complete_rewrite
&&
1672 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
1673 (textconv_two
|| !diff_filespec_is_binary(two
))) {
1674 fprintf(o
->file
, "%s", header
.buf
);
1675 strbuf_reset(&header
);
1676 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1677 textconv_one
, textconv_two
, o
);
1678 o
->found_changes
= 1;
1679 goto free_ab_and_return
;
1683 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1684 die("unable to read files to diff");
1686 if (!DIFF_OPT_TST(o
, TEXT
) &&
1687 ( (diff_filespec_is_binary(one
) && !textconv_one
) ||
1688 (diff_filespec_is_binary(two
) && !textconv_two
) )) {
1689 /* Quite common confusing case */
1690 if (mf1
.size
== mf2
.size
&&
1691 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1692 goto free_ab_and_return
;
1693 fprintf(o
->file
, "%s", header
.buf
);
1694 strbuf_reset(&header
);
1695 if (DIFF_OPT_TST(o
, BINARY
))
1696 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1698 fprintf(o
->file
, "Binary files %s and %s differ\n",
1700 o
->found_changes
= 1;
1703 /* Crazy xdl interfaces.. */
1704 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1708 struct emit_callback ecbdata
;
1709 const struct userdiff_funcname
*pe
;
1711 if (!DIFF_XDL_TST(o
, WHITESPACE_FLAGS
)) {
1712 fprintf(o
->file
, "%s", header
.buf
);
1713 strbuf_reset(&header
);
1718 mf1
.ptr
= run_textconv(textconv_one
, one
, &size
);
1720 die("unable to read files to diff");
1725 mf2
.ptr
= run_textconv(textconv_two
, two
, &size
);
1727 die("unable to read files to diff");
1731 pe
= diff_funcname_pattern(one
);
1733 pe
= diff_funcname_pattern(two
);
1735 memset(&xpp
, 0, sizeof(xpp
));
1736 memset(&xecfg
, 0, sizeof(xecfg
));
1737 memset(&ecbdata
, 0, sizeof(ecbdata
));
1738 ecbdata
.label_path
= lbl
;
1739 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1740 ecbdata
.found_changesp
= &o
->found_changes
;
1741 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1742 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
1743 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1744 ecbdata
.file
= o
->file
;
1745 ecbdata
.header
= header
.len
? &header
: NULL
;
1746 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1747 xecfg
.ctxlen
= o
->context
;
1748 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
1749 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1751 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
1754 else if (!prefixcmp(diffopts
, "--unified="))
1755 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1756 else if (!prefixcmp(diffopts
, "-u"))
1757 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1758 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
)) {
1759 ecbdata
.diff_words
=
1760 xcalloc(1, sizeof(struct diff_words_data
));
1761 ecbdata
.diff_words
->file
= o
->file
;
1763 o
->word_regex
= userdiff_word_regex(one
);
1765 o
->word_regex
= userdiff_word_regex(two
);
1767 o
->word_regex
= diff_word_regex_cfg
;
1768 if (o
->word_regex
) {
1769 ecbdata
.diff_words
->word_regex
= (regex_t
*)
1770 xmalloc(sizeof(regex_t
));
1771 if (regcomp(ecbdata
.diff_words
->word_regex
,
1773 REG_EXTENDED
| REG_NEWLINE
))
1774 die ("Invalid regular expression: %s",
1778 xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
1779 &xpp
, &xecfg
, &ecb
);
1780 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
))
1781 free_diff_words_data(&ecbdata
);
1786 xdiff_clear_find_func(&xecfg
);
1790 strbuf_release(&header
);
1791 diff_free_filespec_data(one
);
1792 diff_free_filespec_data(two
);
1798 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1799 struct diff_filespec
*one
,
1800 struct diff_filespec
*two
,
1801 struct diffstat_t
*diffstat
,
1802 struct diff_options
*o
,
1803 int complete_rewrite
)
1806 struct diffstat_file
*data
;
1808 data
= diffstat_add(diffstat
, name_a
, name_b
);
1811 data
->is_unmerged
= 1;
1814 if (complete_rewrite
) {
1815 diff_populate_filespec(one
, 0);
1816 diff_populate_filespec(two
, 0);
1817 data
->deleted
= count_lines(one
->data
, one
->size
);
1818 data
->added
= count_lines(two
->data
, two
->size
);
1819 goto free_and_return
;
1821 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1822 die("unable to read files to diff");
1824 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1825 data
->is_binary
= 1;
1826 data
->added
= mf2
.size
;
1827 data
->deleted
= mf1
.size
;
1829 /* Crazy xdl interfaces.. */
1834 memset(&xpp
, 0, sizeof(xpp
));
1835 memset(&xecfg
, 0, sizeof(xecfg
));
1836 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1837 xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
1838 &xpp
, &xecfg
, &ecb
);
1842 diff_free_filespec_data(one
);
1843 diff_free_filespec_data(two
);
1846 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1847 const char *attr_path
,
1848 struct diff_filespec
*one
,
1849 struct diff_filespec
*two
,
1850 struct diff_options
*o
)
1853 struct checkdiff_t data
;
1858 memset(&data
, 0, sizeof(data
));
1859 data
.filename
= name_b
? name_b
: name_a
;
1862 data
.ws_rule
= whitespace_rule(attr_path
);
1864 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1865 die("unable to read files to diff");
1868 * All the other codepaths check both sides, but not checking
1869 * the "old" side here is deliberate. We are checking the newly
1870 * introduced changes, and as long as the "new" side is text, we
1871 * can and should check what it introduces.
1873 if (diff_filespec_is_binary(two
))
1874 goto free_and_return
;
1876 /* Crazy xdl interfaces.. */
1881 memset(&xpp
, 0, sizeof(xpp
));
1882 memset(&xecfg
, 0, sizeof(xecfg
));
1883 xecfg
.ctxlen
= 1; /* at least one context line */
1884 xpp
.flags
= XDF_NEED_MINIMAL
;
1885 xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
1886 &xpp
, &xecfg
, &ecb
);
1888 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
1889 struct emit_callback ecbdata
;
1892 ecbdata
.ws_rule
= data
.ws_rule
;
1893 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1894 blank_at_eof
= ecbdata
.blank_at_eof_in_preimage
;
1899 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
1900 fprintf(o
->file
, "%s:%d: %s.\n",
1901 data
.filename
, blank_at_eof
, err
);
1902 data
.status
= 1; /* report errors */
1907 diff_free_filespec_data(one
);
1908 diff_free_filespec_data(two
);
1910 DIFF_OPT_SET(o
, CHECK_FAILED
);
1913 struct diff_filespec
*alloc_filespec(const char *path
)
1915 int namelen
= strlen(path
);
1916 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1918 memset(spec
, 0, sizeof(*spec
));
1919 spec
->path
= (char *)(spec
+ 1);
1920 memcpy(spec
->path
, path
, namelen
+1);
1922 spec
->is_binary
= -1;
1926 void free_filespec(struct diff_filespec
*spec
)
1928 if (!--spec
->count
) {
1929 diff_free_filespec_data(spec
);
1934 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1935 unsigned short mode
)
1938 spec
->mode
= canon_mode(mode
);
1939 hashcpy(spec
->sha1
, sha1
);
1940 spec
->sha1_valid
= !is_null_sha1(sha1
);
1945 * Given a name and sha1 pair, if the index tells us the file in
1946 * the work tree has that object contents, return true, so that
1947 * prepare_temp_file() does not have to inflate and extract.
1949 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1951 struct cache_entry
*ce
;
1956 * We do not read the cache ourselves here, because the
1957 * benchmark with my previous version that always reads cache
1958 * shows that it makes things worse for diff-tree comparing
1959 * two linux-2.6 kernel trees in an already checked out work
1960 * tree. This is because most diff-tree comparisons deal with
1961 * only a small number of files, while reading the cache is
1962 * expensive for a large project, and its cost outweighs the
1963 * savings we get by not inflating the object to a temporary
1964 * file. Practically, this code only helps when we are used
1965 * by diff-cache --cached, which does read the cache before
1971 /* We want to avoid the working directory if our caller
1972 * doesn't need the data in a normal file, this system
1973 * is rather slow with its stat/open/mmap/close syscalls,
1974 * and the object is contained in a pack file. The pack
1975 * is probably already open and will be faster to obtain
1976 * the data through than the working directory. Loose
1977 * objects however would tend to be slower as they need
1978 * to be individually opened and inflated.
1980 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
))
1984 pos
= cache_name_pos(name
, len
);
1987 ce
= active_cache
[pos
];
1990 * This is not the sha1 we are looking for, or
1991 * unreusable because it is not a regular file.
1993 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
1997 * If ce is marked as "assume unchanged", there is no
1998 * guarantee that work tree matches what we are looking for.
2000 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
2004 * If ce matches the file in the work tree, we can reuse it.
2006 if (ce_uptodate(ce
) ||
2007 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
2013 static int populate_from_stdin(struct diff_filespec
*s
)
2015 struct strbuf buf
= STRBUF_INIT
;
2018 if (strbuf_read(&buf
, 0, 0) < 0)
2019 return error("error while reading from stdin %s",
2022 s
->should_munmap
= 0;
2023 s
->data
= strbuf_detach(&buf
, &size
);
2029 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
2032 char *data
= xmalloc(100), *dirty
= "";
2034 /* Are we looking at the work tree? */
2035 if (!s
->sha1_valid
&& s
->dirty_submodule
)
2038 len
= snprintf(data
, 100,
2039 "Subproject commit %s%s\n", sha1_to_hex(s
->sha1
), dirty
);
2051 * While doing rename detection and pickaxe operation, we may need to
2052 * grab the data for the blob (or file) for our own in-core comparison.
2053 * diff_filespec has data and size fields for this purpose.
2055 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
2058 if (!DIFF_FILE_VALID(s
))
2059 die("internal error: asking to populate invalid file.");
2060 if (S_ISDIR(s
->mode
))
2066 if (size_only
&& 0 < s
->size
)
2069 if (S_ISGITLINK(s
->mode
))
2070 return diff_populate_gitlink(s
, size_only
);
2072 if (!s
->sha1_valid
||
2073 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
2074 struct strbuf buf
= STRBUF_INIT
;
2078 if (!strcmp(s
->path
, "-"))
2079 return populate_from_stdin(s
);
2081 if (lstat(s
->path
, &st
) < 0) {
2082 if (errno
== ENOENT
) {
2086 s
->data
= (char *)"";
2091 s
->size
= xsize_t(st
.st_size
);
2094 if (S_ISLNK(st
.st_mode
)) {
2095 struct strbuf sb
= STRBUF_INIT
;
2097 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
2100 s
->data
= strbuf_detach(&sb
, NULL
);
2106 fd
= open(s
->path
, O_RDONLY
);
2109 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2111 s
->should_munmap
= 1;
2114 * Convert from working tree format to canonical git format
2116 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
2118 munmap(s
->data
, s
->size
);
2119 s
->should_munmap
= 0;
2120 s
->data
= strbuf_detach(&buf
, &size
);
2126 enum object_type type
;
2128 type
= sha1_object_info(s
->sha1
, &s
->size
);
2130 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
2137 void diff_free_filespec_blob(struct diff_filespec
*s
)
2141 else if (s
->should_munmap
)
2142 munmap(s
->data
, s
->size
);
2144 if (s
->should_free
|| s
->should_munmap
) {
2145 s
->should_free
= s
->should_munmap
= 0;
2150 void diff_free_filespec_data(struct diff_filespec
*s
)
2152 diff_free_filespec_blob(s
);
2157 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
2160 const unsigned char *sha1
,
2164 struct strbuf buf
= STRBUF_INIT
;
2165 struct strbuf
template = STRBUF_INIT
;
2166 char *path_dup
= xstrdup(path
);
2167 const char *base
= basename(path_dup
);
2169 /* Generate "XXXXXX_basename.ext" */
2170 strbuf_addstr(&template, "XXXXXX_");
2171 strbuf_addstr(&template, base
);
2173 fd
= git_mkstemps(temp
->tmp_path
, PATH_MAX
, template.buf
,
2176 die_errno("unable to create temp-file");
2177 if (convert_to_working_tree(path
,
2178 (const char *)blob
, (size_t)size
, &buf
)) {
2182 if (write_in_full(fd
, blob
, size
) != size
)
2183 die_errno("unable to write temp-file");
2185 temp
->name
= temp
->tmp_path
;
2186 strcpy(temp
->hex
, sha1_to_hex(sha1
));
2188 sprintf(temp
->mode
, "%06o", mode
);
2189 strbuf_release(&buf
);
2190 strbuf_release(&template);
2194 static struct diff_tempfile
*prepare_temp_file(const char *name
,
2195 struct diff_filespec
*one
)
2197 struct diff_tempfile
*temp
= claim_diff_tempfile();
2199 if (!DIFF_FILE_VALID(one
)) {
2201 /* A '-' entry produces this for file-2, and
2202 * a '+' entry produces this for file-1.
2204 temp
->name
= "/dev/null";
2205 strcpy(temp
->hex
, ".");
2206 strcpy(temp
->mode
, ".");
2210 if (!remove_tempfile_installed
) {
2211 atexit(remove_tempfile
);
2212 sigchain_push_common(remove_tempfile_on_signal
);
2213 remove_tempfile_installed
= 1;
2216 if (!one
->sha1_valid
||
2217 reuse_worktree_file(name
, one
->sha1
, 1)) {
2219 if (lstat(name
, &st
) < 0) {
2220 if (errno
== ENOENT
)
2221 goto not_a_valid_file
;
2222 die_errno("stat(%s)", name
);
2224 if (S_ISLNK(st
.st_mode
)) {
2225 struct strbuf sb
= STRBUF_INIT
;
2226 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
2227 die_errno("readlink(%s)", name
);
2228 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
2230 one
->sha1
: null_sha1
),
2232 one
->mode
: S_IFLNK
));
2233 strbuf_release(&sb
);
2236 /* we can borrow from the file in the work tree */
2238 if (!one
->sha1_valid
)
2239 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
2241 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
2242 /* Even though we may sometimes borrow the
2243 * contents from the work tree, we always want
2244 * one->mode. mode is trustworthy even when
2245 * !(one->sha1_valid), as long as
2246 * DIFF_FILE_VALID(one).
2248 sprintf(temp
->mode
, "%06o", one
->mode
);
2253 if (diff_populate_filespec(one
, 0))
2254 die("cannot read data blob for %s", one
->path
);
2255 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
2256 one
->sha1
, one
->mode
);
2261 /* An external diff command takes:
2263 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2264 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2267 static void run_external_diff(const char *pgm
,
2270 struct diff_filespec
*one
,
2271 struct diff_filespec
*two
,
2272 const char *xfrm_msg
,
2273 int complete_rewrite
)
2275 const char *spawn_arg
[10];
2277 const char **arg
= &spawn_arg
[0];
2280 struct diff_tempfile
*temp_one
, *temp_two
;
2281 const char *othername
= (other
? other
: name
);
2282 temp_one
= prepare_temp_file(name
, one
);
2283 temp_two
= prepare_temp_file(othername
, two
);
2286 *arg
++ = temp_one
->name
;
2287 *arg
++ = temp_one
->hex
;
2288 *arg
++ = temp_one
->mode
;
2289 *arg
++ = temp_two
->name
;
2290 *arg
++ = temp_two
->hex
;
2291 *arg
++ = temp_two
->mode
;
2302 retval
= run_command_v_opt(spawn_arg
, RUN_USING_SHELL
);
2305 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2310 static int similarity_index(struct diff_filepair
*p
)
2312 return p
->score
* 100 / MAX_SCORE
;
2315 static void fill_metainfo(struct strbuf
*msg
,
2318 struct diff_filespec
*one
,
2319 struct diff_filespec
*two
,
2320 struct diff_options
*o
,
2321 struct diff_filepair
*p
)
2323 strbuf_init(msg
, PATH_MAX
* 2 + 300);
2324 switch (p
->status
) {
2325 case DIFF_STATUS_COPIED
:
2326 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2327 strbuf_addstr(msg
, "\ncopy from ");
2328 quote_c_style(name
, msg
, NULL
, 0);
2329 strbuf_addstr(msg
, "\ncopy to ");
2330 quote_c_style(other
, msg
, NULL
, 0);
2331 strbuf_addch(msg
, '\n');
2333 case DIFF_STATUS_RENAMED
:
2334 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2335 strbuf_addstr(msg
, "\nrename from ");
2336 quote_c_style(name
, msg
, NULL
, 0);
2337 strbuf_addstr(msg
, "\nrename to ");
2338 quote_c_style(other
, msg
, NULL
, 0);
2339 strbuf_addch(msg
, '\n');
2341 case DIFF_STATUS_MODIFIED
:
2343 strbuf_addf(msg
, "dissimilarity index %d%%\n",
2344 similarity_index(p
));
2352 if (one
&& two
&& hashcmp(one
->sha1
, two
->sha1
)) {
2353 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2355 if (DIFF_OPT_TST(o
, BINARY
)) {
2357 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2358 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2361 strbuf_addf(msg
, "index %.*s..%.*s",
2362 abbrev
, sha1_to_hex(one
->sha1
),
2363 abbrev
, sha1_to_hex(two
->sha1
));
2364 if (one
->mode
== two
->mode
)
2365 strbuf_addf(msg
, " %06o", one
->mode
);
2366 strbuf_addch(msg
, '\n');
2369 strbuf_setlen(msg
, msg
->len
- 1);
2372 static void run_diff_cmd(const char *pgm
,
2375 const char *attr_path
,
2376 struct diff_filespec
*one
,
2377 struct diff_filespec
*two
,
2379 struct diff_options
*o
,
2380 struct diff_filepair
*p
)
2382 const char *xfrm_msg
= NULL
;
2383 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
2386 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
);
2387 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
2390 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2393 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
2394 if (drv
&& drv
->external
)
2395 pgm
= drv
->external
;
2399 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2404 builtin_diff(name
, other
? other
: name
,
2405 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2407 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2410 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2412 if (DIFF_FILE_VALID(one
)) {
2413 if (!one
->sha1_valid
) {
2415 if (!strcmp(one
->path
, "-")) {
2416 hashcpy(one
->sha1
, null_sha1
);
2419 if (lstat(one
->path
, &st
) < 0)
2420 die_errno("stat '%s'", one
->path
);
2421 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2422 die("cannot hash %s", one
->path
);
2429 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2431 /* Strip the prefix but do not molest /dev/null and absolute paths */
2432 if (*namep
&& **namep
!= '/')
2433 *namep
+= prefix_length
;
2434 if (*otherp
&& **otherp
!= '/')
2435 *otherp
+= prefix_length
;
2438 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2440 const char *pgm
= external_diff();
2442 struct diff_filespec
*one
= p
->one
;
2443 struct diff_filespec
*two
= p
->two
;
2446 const char *attr_path
;
2448 name
= p
->one
->path
;
2449 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2451 if (o
->prefix_length
)
2452 strip_prefix(o
->prefix_length
, &name
, &other
);
2454 if (DIFF_PAIR_UNMERGED(p
)) {
2455 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2456 NULL
, NULL
, NULL
, o
, p
);
2460 diff_fill_sha1_info(one
);
2461 diff_fill_sha1_info(two
);
2464 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2465 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2467 * a filepair that changes between file and symlink
2468 * needs to be split into deletion and creation.
2470 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2471 run_diff_cmd(NULL
, name
, other
, attr_path
,
2472 one
, null
, &msg
, o
, p
);
2474 strbuf_release(&msg
);
2476 null
= alloc_filespec(one
->path
);
2477 run_diff_cmd(NULL
, name
, other
, attr_path
,
2478 null
, two
, &msg
, o
, p
);
2482 run_diff_cmd(pgm
, name
, other
, attr_path
,
2483 one
, two
, &msg
, o
, p
);
2485 strbuf_release(&msg
);
2488 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2489 struct diffstat_t
*diffstat
)
2493 int complete_rewrite
= 0;
2495 if (DIFF_PAIR_UNMERGED(p
)) {
2497 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2501 name
= p
->one
->path
;
2502 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2504 if (o
->prefix_length
)
2505 strip_prefix(o
->prefix_length
, &name
, &other
);
2507 diff_fill_sha1_info(p
->one
);
2508 diff_fill_sha1_info(p
->two
);
2510 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2511 complete_rewrite
= 1;
2512 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2515 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2519 const char *attr_path
;
2521 if (DIFF_PAIR_UNMERGED(p
)) {
2526 name
= p
->one
->path
;
2527 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2528 attr_path
= other
? other
: name
;
2530 if (o
->prefix_length
)
2531 strip_prefix(o
->prefix_length
, &name
, &other
);
2533 diff_fill_sha1_info(p
->one
);
2534 diff_fill_sha1_info(p
->two
);
2536 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2539 void diff_setup(struct diff_options
*options
)
2541 memset(options
, 0, sizeof(*options
));
2543 options
->file
= stdout
;
2545 options
->line_termination
= '\n';
2546 options
->break_opt
= -1;
2547 options
->rename_limit
= -1;
2548 options
->dirstat_percent
= 3;
2549 options
->context
= 3;
2551 options
->change
= diff_change
;
2552 options
->add_remove
= diff_addremove
;
2553 if (diff_use_color_default
> 0)
2554 DIFF_OPT_SET(options
, COLOR_DIFF
);
2555 options
->detect_rename
= diff_detect_rename_default
;
2557 if (!diff_mnemonic_prefix
) {
2558 options
->a_prefix
= "a/";
2559 options
->b_prefix
= "b/";
2563 int diff_setup_done(struct diff_options
*options
)
2567 if (options
->output_format
& DIFF_FORMAT_NAME
)
2569 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2571 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2573 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2576 die("--name-only, --name-status, --check and -s are mutually exclusive");
2579 * Most of the time we can say "there are changes"
2580 * only by checking if there are changed paths, but
2581 * --ignore-whitespace* options force us to look
2585 if (DIFF_XDL_TST(options
, IGNORE_WHITESPACE
) ||
2586 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_CHANGE
) ||
2587 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_AT_EOL
))
2588 DIFF_OPT_SET(options
, DIFF_FROM_CONTENTS
);
2590 DIFF_OPT_CLR(options
, DIFF_FROM_CONTENTS
);
2592 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2593 options
->detect_rename
= DIFF_DETECT_COPY
;
2595 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2596 options
->prefix
= NULL
;
2597 if (options
->prefix
)
2598 options
->prefix_length
= strlen(options
->prefix
);
2600 options
->prefix_length
= 0;
2602 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2603 DIFF_FORMAT_NAME_STATUS
|
2604 DIFF_FORMAT_CHECKDIFF
|
2605 DIFF_FORMAT_NO_OUTPUT
))
2606 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2607 DIFF_FORMAT_NUMSTAT
|
2608 DIFF_FORMAT_DIFFSTAT
|
2609 DIFF_FORMAT_SHORTSTAT
|
2610 DIFF_FORMAT_DIRSTAT
|
2611 DIFF_FORMAT_SUMMARY
|
2615 * These cases always need recursive; we do not drop caller-supplied
2616 * recursive bits for other formats here.
2618 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2619 DIFF_FORMAT_NUMSTAT
|
2620 DIFF_FORMAT_DIFFSTAT
|
2621 DIFF_FORMAT_SHORTSTAT
|
2622 DIFF_FORMAT_DIRSTAT
|
2623 DIFF_FORMAT_SUMMARY
|
2624 DIFF_FORMAT_CHECKDIFF
))
2625 DIFF_OPT_SET(options
, RECURSIVE
);
2627 * Also pickaxe would not work very well if you do not say recursive
2629 if (options
->pickaxe
)
2630 DIFF_OPT_SET(options
, RECURSIVE
);
2632 if (options
->detect_rename
&& options
->rename_limit
< 0)
2633 options
->rename_limit
= diff_rename_limit_default
;
2634 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2636 /* read-cache does not die even when it fails
2637 * so it is safe for us to do this here. Also
2638 * it does not smudge active_cache or active_nr
2639 * when it fails, so we do not have to worry about
2640 * cleaning it up ourselves either.
2644 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2645 options
->abbrev
= 40; /* full */
2648 * It does not make sense to show the first hit we happened
2649 * to have found. It does not make sense not to return with
2650 * exit code in such a case either.
2652 if (DIFF_OPT_TST(options
, QUICK
)) {
2653 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2654 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2660 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2670 if (c
== arg_short
) {
2674 if (val
&& isdigit(c
)) {
2676 int n
= strtoul(arg
, &end
, 10);
2687 eq
= strchr(arg
, '=');
2692 if (!len
|| strncmp(arg
, arg_long
, len
))
2697 if (!isdigit(*++eq
))
2699 n
= strtoul(eq
, &end
, 10);
2707 static int diff_scoreopt_parse(const char *opt
);
2709 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2711 const char *arg
= av
[0];
2713 /* Output format options */
2714 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2715 options
->output_format
|= DIFF_FORMAT_PATCH
;
2716 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2717 options
->output_format
|= DIFF_FORMAT_PATCH
;
2718 else if (!strcmp(arg
, "--raw"))
2719 options
->output_format
|= DIFF_FORMAT_RAW
;
2720 else if (!strcmp(arg
, "--patch-with-raw"))
2721 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2722 else if (!strcmp(arg
, "--numstat"))
2723 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2724 else if (!strcmp(arg
, "--shortstat"))
2725 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2726 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2727 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2728 else if (!strcmp(arg
, "--cumulative")) {
2729 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2730 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
2731 } else if (opt_arg(arg
, 0, "dirstat-by-file",
2732 &options
->dirstat_percent
)) {
2733 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2734 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
2736 else if (!strcmp(arg
, "--check"))
2737 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2738 else if (!strcmp(arg
, "--summary"))
2739 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2740 else if (!strcmp(arg
, "--patch-with-stat"))
2741 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2742 else if (!strcmp(arg
, "--name-only"))
2743 options
->output_format
|= DIFF_FORMAT_NAME
;
2744 else if (!strcmp(arg
, "--name-status"))
2745 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2746 else if (!strcmp(arg
, "-s"))
2747 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2748 else if (!prefixcmp(arg
, "--stat")) {
2750 int width
= options
->stat_width
;
2751 int name_width
= options
->stat_name_width
;
2757 if (!prefixcmp(arg
, "-width="))
2758 width
= strtoul(arg
+ 7, &end
, 10);
2759 else if (!prefixcmp(arg
, "-name-width="))
2760 name_width
= strtoul(arg
+ 12, &end
, 10);
2763 width
= strtoul(arg
+1, &end
, 10);
2765 name_width
= strtoul(end
+1, &end
, 10);
2768 /* Important! This checks all the error cases! */
2771 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2772 options
->stat_name_width
= name_width
;
2773 options
->stat_width
= width
;
2776 /* renames options */
2777 else if (!prefixcmp(arg
, "-B")) {
2778 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2781 else if (!prefixcmp(arg
, "-M")) {
2782 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2784 options
->detect_rename
= DIFF_DETECT_RENAME
;
2786 else if (!prefixcmp(arg
, "-C")) {
2787 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2788 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2789 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2791 options
->detect_rename
= DIFF_DETECT_COPY
;
2793 else if (!strcmp(arg
, "--no-renames"))
2794 options
->detect_rename
= 0;
2795 else if (!strcmp(arg
, "--relative"))
2796 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2797 else if (!prefixcmp(arg
, "--relative=")) {
2798 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2799 options
->prefix
= arg
+ 11;
2803 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2804 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
2805 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2806 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
2807 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2808 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
2809 else if (!strcmp(arg
, "--patience"))
2810 DIFF_XDL_SET(options
, PATIENCE_DIFF
);
2813 else if (!strcmp(arg
, "--binary")) {
2814 options
->output_format
|= DIFF_FORMAT_PATCH
;
2815 DIFF_OPT_SET(options
, BINARY
);
2817 else if (!strcmp(arg
, "--full-index"))
2818 DIFF_OPT_SET(options
, FULL_INDEX
);
2819 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2820 DIFF_OPT_SET(options
, TEXT
);
2821 else if (!strcmp(arg
, "-R"))
2822 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2823 else if (!strcmp(arg
, "--find-copies-harder"))
2824 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2825 else if (!strcmp(arg
, "--follow"))
2826 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2827 else if (!strcmp(arg
, "--color"))
2828 DIFF_OPT_SET(options
, COLOR_DIFF
);
2829 else if (!prefixcmp(arg
, "--color=")) {
2830 int value
= git_config_colorbool(NULL
, arg
+8, -1);
2832 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2834 DIFF_OPT_SET(options
, COLOR_DIFF
);
2836 return error("option `color' expects \"always\", \"auto\", or \"never\"");
2838 else if (!strcmp(arg
, "--no-color"))
2839 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2840 else if (!strcmp(arg
, "--color-words")) {
2841 DIFF_OPT_SET(options
, COLOR_DIFF
);
2842 DIFF_OPT_SET(options
, COLOR_DIFF_WORDS
);
2844 else if (!prefixcmp(arg
, "--color-words=")) {
2845 DIFF_OPT_SET(options
, COLOR_DIFF
);
2846 DIFF_OPT_SET(options
, COLOR_DIFF_WORDS
);
2847 options
->word_regex
= arg
+ 14;
2849 else if (!strcmp(arg
, "--exit-code"))
2850 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2851 else if (!strcmp(arg
, "--quiet"))
2852 DIFF_OPT_SET(options
, QUICK
);
2853 else if (!strcmp(arg
, "--ext-diff"))
2854 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2855 else if (!strcmp(arg
, "--no-ext-diff"))
2856 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2857 else if (!strcmp(arg
, "--textconv"))
2858 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
2859 else if (!strcmp(arg
, "--no-textconv"))
2860 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
2861 else if (!strcmp(arg
, "--ignore-submodules"))
2862 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
2863 else if (!strcmp(arg
, "--submodule"))
2864 DIFF_OPT_SET(options
, SUBMODULE_LOG
);
2865 else if (!prefixcmp(arg
, "--submodule=")) {
2866 if (!strcmp(arg
+ 12, "log"))
2867 DIFF_OPT_SET(options
, SUBMODULE_LOG
);
2871 else if (!strcmp(arg
, "-z"))
2872 options
->line_termination
= 0;
2873 else if (!prefixcmp(arg
, "-l"))
2874 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2875 else if (!prefixcmp(arg
, "-S"))
2876 options
->pickaxe
= arg
+ 2;
2877 else if (!strcmp(arg
, "--pickaxe-all"))
2878 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2879 else if (!strcmp(arg
, "--pickaxe-regex"))
2880 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2881 else if (!prefixcmp(arg
, "-O"))
2882 options
->orderfile
= arg
+ 2;
2883 else if (!prefixcmp(arg
, "--diff-filter="))
2884 options
->filter
= arg
+ 14;
2885 else if (!strcmp(arg
, "--abbrev"))
2886 options
->abbrev
= DEFAULT_ABBREV
;
2887 else if (!prefixcmp(arg
, "--abbrev=")) {
2888 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2889 if (options
->abbrev
< MINIMUM_ABBREV
)
2890 options
->abbrev
= MINIMUM_ABBREV
;
2891 else if (40 < options
->abbrev
)
2892 options
->abbrev
= 40;
2894 else if (!prefixcmp(arg
, "--src-prefix="))
2895 options
->a_prefix
= arg
+ 13;
2896 else if (!prefixcmp(arg
, "--dst-prefix="))
2897 options
->b_prefix
= arg
+ 13;
2898 else if (!strcmp(arg
, "--no-prefix"))
2899 options
->a_prefix
= options
->b_prefix
= "";
2900 else if (opt_arg(arg
, '\0', "inter-hunk-context",
2901 &options
->interhunkcontext
))
2903 else if (!prefixcmp(arg
, "--output=")) {
2904 options
->file
= fopen(arg
+ strlen("--output="), "w");
2905 options
->close_file
= 1;
2911 static int parse_num(const char **cp_p
)
2913 unsigned long num
, scale
;
2915 const char *cp
= *cp_p
;
2922 if ( !dot
&& ch
== '.' ) {
2925 } else if ( ch
== '%' ) {
2926 scale
= dot
? scale
*100 : 100;
2927 cp
++; /* % is always at the end */
2929 } else if ( ch
>= '0' && ch
<= '9' ) {
2930 if ( scale
< 100000 ) {
2932 num
= (num
*10) + (ch
-'0');
2941 /* user says num divided by scale and we say internally that
2942 * is MAX_SCORE * num / scale.
2944 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2947 static int diff_scoreopt_parse(const char *opt
)
2949 int opt1
, opt2
, cmd
;
2954 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2955 return -1; /* that is not a -M, -C nor -B option */
2957 opt1
= parse_num(&opt
);
2963 else if (*opt
!= '/')
2964 return -1; /* we expect -B80/99 or -B80 */
2967 opt2
= parse_num(&opt
);
2972 return opt1
| (opt2
<< 16);
2975 struct diff_queue_struct diff_queued_diff
;
2977 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2979 if (queue
->alloc
<= queue
->nr
) {
2980 queue
->alloc
= alloc_nr(queue
->alloc
);
2981 queue
->queue
= xrealloc(queue
->queue
,
2982 sizeof(dp
) * queue
->alloc
);
2984 queue
->queue
[queue
->nr
++] = dp
;
2987 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2988 struct diff_filespec
*one
,
2989 struct diff_filespec
*two
)
2991 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2999 void diff_free_filepair(struct diff_filepair
*p
)
3001 free_filespec(p
->one
);
3002 free_filespec(p
->two
);
3006 /* This is different from find_unique_abbrev() in that
3007 * it stuffs the result with dots for alignment.
3009 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
3014 return sha1_to_hex(sha1
);
3016 abbrev
= find_unique_abbrev(sha1
, len
);
3017 abblen
= strlen(abbrev
);
3019 static char hex
[41];
3020 if (len
< abblen
&& abblen
<= len
+ 2)
3021 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
3023 sprintf(hex
, "%s...", abbrev
);
3026 return sha1_to_hex(sha1
);
3029 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
3031 int line_termination
= opt
->line_termination
;
3032 int inter_name_termination
= line_termination
? '\t' : '\0';
3034 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
3035 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
3036 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
3037 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
3040 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
3041 inter_name_termination
);
3043 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
3046 if (p
->status
== DIFF_STATUS_COPIED
||
3047 p
->status
== DIFF_STATUS_RENAMED
) {
3048 const char *name_a
, *name_b
;
3049 name_a
= p
->one
->path
;
3050 name_b
= p
->two
->path
;
3051 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3052 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
3053 write_name_quoted(name_b
, opt
->file
, line_termination
);
3055 const char *name_a
, *name_b
;
3056 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
3058 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3059 write_name_quoted(name_a
, opt
->file
, line_termination
);
3063 int diff_unmodified_pair(struct diff_filepair
*p
)
3065 /* This function is written stricter than necessary to support
3066 * the currently implemented transformers, but the idea is to
3067 * let transformers to produce diff_filepairs any way they want,
3068 * and filter and clean them up here before producing the output.
3070 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
3072 if (DIFF_PAIR_UNMERGED(p
))
3073 return 0; /* unmerged is interesting */
3075 /* deletion, addition, mode or type change
3076 * and rename are all interesting.
3078 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
3079 DIFF_PAIR_MODE_CHANGED(p
) ||
3080 strcmp(one
->path
, two
->path
))
3083 /* both are valid and point at the same path. that is, we are
3084 * dealing with a change.
3086 if (one
->sha1_valid
&& two
->sha1_valid
&&
3087 !hashcmp(one
->sha1
, two
->sha1
))
3088 return 1; /* no change */
3089 if (!one
->sha1_valid
&& !two
->sha1_valid
)
3090 return 1; /* both look at the same file on the filesystem. */
3094 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
3096 if (diff_unmodified_pair(p
))
3099 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3100 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3101 return; /* no tree diffs in patch format */
3106 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
3107 struct diffstat_t
*diffstat
)
3109 if (diff_unmodified_pair(p
))
3112 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3113 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3114 return; /* no tree diffs in patch format */
3116 run_diffstat(p
, o
, diffstat
);
3119 static void diff_flush_checkdiff(struct diff_filepair
*p
,
3120 struct diff_options
*o
)
3122 if (diff_unmodified_pair(p
))
3125 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3126 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3127 return; /* no tree diffs in patch format */
3129 run_checkdiff(p
, o
);
3132 int diff_queue_is_empty(void)
3134 struct diff_queue_struct
*q
= &diff_queued_diff
;
3136 for (i
= 0; i
< q
->nr
; i
++)
3137 if (!diff_unmodified_pair(q
->queue
[i
]))
3143 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
3145 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
3148 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
3150 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
3151 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
3153 s
->size
, s
->xfrm_flags
);
3156 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
3158 diff_debug_filespec(p
->one
, i
, "one");
3159 diff_debug_filespec(p
->two
, i
, "two");
3160 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
3161 p
->score
, p
->status
? p
->status
: '?',
3162 p
->one
->rename_used
, p
->broken_pair
);
3165 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
3169 fprintf(stderr
, "%s\n", msg
);
3170 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
3171 for (i
= 0; i
< q
->nr
; i
++) {
3172 struct diff_filepair
*p
= q
->queue
[i
];
3173 diff_debug_filepair(p
, i
);
3178 static void diff_resolve_rename_copy(void)
3181 struct diff_filepair
*p
;
3182 struct diff_queue_struct
*q
= &diff_queued_diff
;
3184 diff_debug_queue("resolve-rename-copy", q
);
3186 for (i
= 0; i
< q
->nr
; i
++) {
3188 p
->status
= 0; /* undecided */
3189 if (DIFF_PAIR_UNMERGED(p
))
3190 p
->status
= DIFF_STATUS_UNMERGED
;
3191 else if (!DIFF_FILE_VALID(p
->one
))
3192 p
->status
= DIFF_STATUS_ADDED
;
3193 else if (!DIFF_FILE_VALID(p
->two
))
3194 p
->status
= DIFF_STATUS_DELETED
;
3195 else if (DIFF_PAIR_TYPE_CHANGED(p
))
3196 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
3198 /* from this point on, we are dealing with a pair
3199 * whose both sides are valid and of the same type, i.e.
3200 * either in-place edit or rename/copy edit.
3202 else if (DIFF_PAIR_RENAME(p
)) {
3204 * A rename might have re-connected a broken
3205 * pair up, causing the pathnames to be the
3206 * same again. If so, that's not a rename at
3207 * all, just a modification..
3209 * Otherwise, see if this source was used for
3210 * multiple renames, in which case we decrement
3211 * the count, and call it a copy.
3213 if (!strcmp(p
->one
->path
, p
->two
->path
))
3214 p
->status
= DIFF_STATUS_MODIFIED
;
3215 else if (--p
->one
->rename_used
> 0)
3216 p
->status
= DIFF_STATUS_COPIED
;
3218 p
->status
= DIFF_STATUS_RENAMED
;
3220 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
3221 p
->one
->mode
!= p
->two
->mode
||
3222 is_null_sha1(p
->one
->sha1
))
3223 p
->status
= DIFF_STATUS_MODIFIED
;
3225 /* This is a "no-change" entry and should not
3226 * happen anymore, but prepare for broken callers.
3228 error("feeding unmodified %s to diffcore",
3230 p
->status
= DIFF_STATUS_UNKNOWN
;
3233 diff_debug_queue("resolve-rename-copy done", q
);
3236 static int check_pair_status(struct diff_filepair
*p
)
3238 switch (p
->status
) {
3239 case DIFF_STATUS_UNKNOWN
:
3242 die("internal error in diff-resolve-rename-copy");
3248 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
3250 int fmt
= opt
->output_format
;
3252 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
3253 diff_flush_checkdiff(p
, opt
);
3254 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
3255 diff_flush_raw(p
, opt
);
3256 else if (fmt
& DIFF_FORMAT_NAME
) {
3257 const char *name_a
, *name_b
;
3258 name_a
= p
->two
->path
;
3260 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3261 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
3265 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
3268 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
3270 fprintf(file
, " %s ", newdelete
);
3271 write_name_quoted(fs
->path
, file
, '\n');
3275 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
3277 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
3278 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
3279 show_name
? ' ' : '\n');
3281 write_name_quoted(p
->two
->path
, file
, '\n');
3286 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
3288 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
3290 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
3292 show_mode_change(file
, p
, 0);
3295 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
3298 case DIFF_STATUS_DELETED
:
3299 show_file_mode_name(file
, "delete", p
->one
);
3301 case DIFF_STATUS_ADDED
:
3302 show_file_mode_name(file
, "create", p
->two
);
3304 case DIFF_STATUS_COPIED
:
3305 show_rename_copy(file
, "copy", p
);
3307 case DIFF_STATUS_RENAMED
:
3308 show_rename_copy(file
, "rename", p
);
3312 fputs(" rewrite ", file
);
3313 write_name_quoted(p
->two
->path
, file
, ' ');
3314 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3316 show_mode_change(file
, p
, !p
->score
);
3326 static int remove_space(char *line
, int len
)
3332 for (i
= 0; i
< len
; i
++)
3333 if (!isspace((c
= line
[i
])))
3339 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3341 struct patch_id_t
*data
= priv
;
3344 /* Ignore line numbers when computing the SHA1 of the patch */
3345 if (!prefixcmp(line
, "@@ -"))
3348 new_len
= remove_space(line
, len
);
3350 git_SHA1_Update(data
->ctx
, line
, new_len
);
3351 data
->patchlen
+= new_len
;
3354 /* returns 0 upon success, and writes result into sha1 */
3355 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3357 struct diff_queue_struct
*q
= &diff_queued_diff
;
3360 struct patch_id_t data
;
3361 char buffer
[PATH_MAX
* 4 + 20];
3363 git_SHA1_Init(&ctx
);
3364 memset(&data
, 0, sizeof(struct patch_id_t
));
3367 for (i
= 0; i
< q
->nr
; i
++) {
3372 struct diff_filepair
*p
= q
->queue
[i
];
3375 memset(&xpp
, 0, sizeof(xpp
));
3376 memset(&xecfg
, 0, sizeof(xecfg
));
3378 return error("internal diff status error");
3379 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3381 if (diff_unmodified_pair(p
))
3383 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3384 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3386 if (DIFF_PAIR_UNMERGED(p
))
3389 diff_fill_sha1_info(p
->one
);
3390 diff_fill_sha1_info(p
->two
);
3391 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3392 fill_mmfile(&mf2
, p
->two
) < 0)
3393 return error("unable to read files to diff");
3395 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3396 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3397 if (p
->one
->mode
== 0)
3398 len1
= snprintf(buffer
, sizeof(buffer
),
3399 "diff--gita/%.*sb/%.*s"
3406 len2
, p
->two
->path
);
3407 else if (p
->two
->mode
== 0)
3408 len1
= snprintf(buffer
, sizeof(buffer
),
3409 "diff--gita/%.*sb/%.*s"
3410 "deletedfilemode%06o"
3416 len1
, p
->one
->path
);
3418 len1
= snprintf(buffer
, sizeof(buffer
),
3419 "diff--gita/%.*sb/%.*s"
3425 len2
, p
->two
->path
);
3426 git_SHA1_Update(&ctx
, buffer
, len1
);
3428 xpp
.flags
= XDF_NEED_MINIMAL
;
3430 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3431 xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
3432 &xpp
, &xecfg
, &ecb
);
3435 git_SHA1_Final(sha1
, &ctx
);
3439 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3441 struct diff_queue_struct
*q
= &diff_queued_diff
;
3443 int result
= diff_get_patch_id(options
, sha1
);
3445 for (i
= 0; i
< q
->nr
; i
++)
3446 diff_free_filepair(q
->queue
[i
]);
3450 q
->nr
= q
->alloc
= 0;
3455 static int is_summary_empty(const struct diff_queue_struct
*q
)
3459 for (i
= 0; i
< q
->nr
; i
++) {
3460 const struct diff_filepair
*p
= q
->queue
[i
];
3462 switch (p
->status
) {
3463 case DIFF_STATUS_DELETED
:
3464 case DIFF_STATUS_ADDED
:
3465 case DIFF_STATUS_COPIED
:
3466 case DIFF_STATUS_RENAMED
:
3471 if (p
->one
->mode
&& p
->two
->mode
&&
3472 p
->one
->mode
!= p
->two
->mode
)
3480 void diff_flush(struct diff_options
*options
)
3482 struct diff_queue_struct
*q
= &diff_queued_diff
;
3483 int i
, output_format
= options
->output_format
;
3487 * Order: raw, stat, summary, patch
3488 * or: name/name-status/checkdiff (other bits clear)
3493 if (output_format
& (DIFF_FORMAT_RAW
|
3495 DIFF_FORMAT_NAME_STATUS
|
3496 DIFF_FORMAT_CHECKDIFF
)) {
3497 for (i
= 0; i
< q
->nr
; i
++) {
3498 struct diff_filepair
*p
= q
->queue
[i
];
3499 if (check_pair_status(p
))
3500 flush_one_pair(p
, options
);
3505 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3506 struct diffstat_t diffstat
;
3508 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3509 for (i
= 0; i
< q
->nr
; i
++) {
3510 struct diff_filepair
*p
= q
->queue
[i
];
3511 if (check_pair_status(p
))
3512 diff_flush_stat(p
, options
, &diffstat
);
3514 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3515 show_numstat(&diffstat
, options
);
3516 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3517 show_stats(&diffstat
, options
);
3518 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3519 show_shortstats(&diffstat
, options
);
3520 free_diffstat_info(&diffstat
);
3523 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3524 show_dirstat(options
);
3526 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3527 for (i
= 0; i
< q
->nr
; i
++)
3528 diff_summary(options
->file
, q
->queue
[i
]);
3532 if (output_format
& DIFF_FORMAT_PATCH
) {
3534 putc(options
->line_termination
, options
->file
);
3535 if (options
->stat_sep
) {
3536 /* attach patch instead of inline */
3537 fputs(options
->stat_sep
, options
->file
);
3541 for (i
= 0; i
< q
->nr
; i
++) {
3542 struct diff_filepair
*p
= q
->queue
[i
];
3543 if (check_pair_status(p
))
3544 diff_flush_patch(p
, options
);
3548 if (output_format
& DIFF_FORMAT_CALLBACK
)
3549 options
->format_callback(q
, options
, options
->format_callback_data
);
3551 for (i
= 0; i
< q
->nr
; i
++)
3552 diff_free_filepair(q
->queue
[i
]);
3556 q
->nr
= q
->alloc
= 0;
3557 if (options
->close_file
)
3558 fclose(options
->file
);
3561 * Report the content-level differences with HAS_CHANGES;
3562 * diff_addremove/diff_change does not set the bit when
3563 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
3565 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
3566 if (options
->found_changes
)
3567 DIFF_OPT_SET(options
, HAS_CHANGES
);
3569 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3573 static void diffcore_apply_filter(const char *filter
)
3576 struct diff_queue_struct
*q
= &diff_queued_diff
;
3577 struct diff_queue_struct outq
;
3579 outq
.nr
= outq
.alloc
= 0;
3584 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3586 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3587 struct diff_filepair
*p
= q
->queue
[i
];
3588 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3590 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3592 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3593 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3594 strchr(filter
, p
->status
)))
3600 /* otherwise we will clear the whole queue
3601 * by copying the empty outq at the end of this
3602 * function, but first clear the current entries
3605 for (i
= 0; i
< q
->nr
; i
++)
3606 diff_free_filepair(q
->queue
[i
]);
3609 /* Only the matching ones */
3610 for (i
= 0; i
< q
->nr
; i
++) {
3611 struct diff_filepair
*p
= q
->queue
[i
];
3613 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3615 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3617 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3618 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3619 strchr(filter
, p
->status
)))
3622 diff_free_filepair(p
);
3629 /* Check whether two filespecs with the same mode and size are identical */
3630 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3631 struct diff_filespec
*two
)
3633 if (S_ISGITLINK(one
->mode
))
3635 if (diff_populate_filespec(one
, 0))
3637 if (diff_populate_filespec(two
, 0))
3639 return !memcmp(one
->data
, two
->data
, one
->size
);
3642 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3645 struct diff_queue_struct
*q
= &diff_queued_diff
;
3646 struct diff_queue_struct outq
;
3648 outq
.nr
= outq
.alloc
= 0;
3650 for (i
= 0; i
< q
->nr
; i
++) {
3651 struct diff_filepair
*p
= q
->queue
[i
];
3654 * 1. Entries that come from stat info dirtyness
3655 * always have both sides (iow, not create/delete),
3656 * one side of the object name is unknown, with
3657 * the same mode and size. Keep the ones that
3658 * do not match these criteria. They have real
3661 * 2. At this point, the file is known to be modified,
3662 * with the same mode and size, and the object
3663 * name of one side is unknown. Need to inspect
3664 * the identical contents.
3666 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3667 !DIFF_FILE_VALID(p
->two
) ||
3668 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3669 (p
->one
->mode
!= p
->two
->mode
) ||
3670 diff_populate_filespec(p
->one
, 1) ||
3671 diff_populate_filespec(p
->two
, 1) ||
3672 (p
->one
->size
!= p
->two
->size
) ||
3673 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3677 * The caller can subtract 1 from skip_stat_unmatch
3678 * to determine how many paths were dirty only
3679 * due to stat info mismatch.
3681 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3682 diffopt
->skip_stat_unmatch
++;
3683 diff_free_filepair(p
);
3690 static int diffnamecmp(const void *a_
, const void *b_
)
3692 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
3693 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
3694 const char *name_a
, *name_b
;
3696 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
3697 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
3698 return strcmp(name_a
, name_b
);
3701 void diffcore_fix_diff_index(struct diff_options
*options
)
3703 struct diff_queue_struct
*q
= &diff_queued_diff
;
3704 qsort(q
->queue
, q
->nr
, sizeof(q
->queue
[0]), diffnamecmp
);
3707 void diffcore_std(struct diff_options
*options
)
3709 if (options
->skip_stat_unmatch
)
3710 diffcore_skip_stat_unmatch(options
);
3711 if (options
->break_opt
!= -1)
3712 diffcore_break(options
->break_opt
);
3713 if (options
->detect_rename
)
3714 diffcore_rename(options
);
3715 if (options
->break_opt
!= -1)
3716 diffcore_merge_broken();
3717 if (options
->pickaxe
)
3718 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3719 if (options
->orderfile
)
3720 diffcore_order(options
->orderfile
);
3721 diff_resolve_rename_copy();
3722 diffcore_apply_filter(options
->filter
);
3724 if (diff_queued_diff
.nr
&& !DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3725 DIFF_OPT_SET(options
, HAS_CHANGES
);
3727 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3730 int diff_result_code(struct diff_options
*opt
, int status
)
3733 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3734 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3736 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3737 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3739 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3740 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3745 void diff_addremove(struct diff_options
*options
,
3746 int addremove
, unsigned mode
,
3747 const unsigned char *sha1
,
3748 const char *concatpath
, unsigned dirty_submodule
)
3750 struct diff_filespec
*one
, *two
;
3752 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
3755 /* This may look odd, but it is a preparation for
3756 * feeding "there are unchanged files which should
3757 * not produce diffs, but when you are doing copy
3758 * detection you would need them, so here they are"
3759 * entries to the diff-core. They will be prefixed
3760 * with something like '=' or '*' (I haven't decided
3761 * which but should not make any difference).
3762 * Feeding the same new and old to diff_change()
3763 * also has the same effect.
3764 * Before the final output happens, they are pruned after
3765 * merged into rename/copy pairs as appropriate.
3767 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3768 addremove
= (addremove
== '+' ? '-' :
3769 addremove
== '-' ? '+' : addremove
);
3771 if (options
->prefix
&&
3772 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3775 one
= alloc_filespec(concatpath
);
3776 two
= alloc_filespec(concatpath
);
3778 if (addremove
!= '+')
3779 fill_filespec(one
, sha1
, mode
);
3780 if (addremove
!= '-') {
3781 fill_filespec(two
, sha1
, mode
);
3782 two
->dirty_submodule
= dirty_submodule
;
3785 diff_queue(&diff_queued_diff
, one
, two
);
3786 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3787 DIFF_OPT_SET(options
, HAS_CHANGES
);
3790 void diff_change(struct diff_options
*options
,
3791 unsigned old_mode
, unsigned new_mode
,
3792 const unsigned char *old_sha1
,
3793 const unsigned char *new_sha1
,
3794 const char *concatpath
,
3795 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
3797 struct diff_filespec
*one
, *two
;
3799 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
3800 && S_ISGITLINK(new_mode
))
3803 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3805 const unsigned char *tmp_c
;
3806 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3807 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3808 tmp
= old_dirty_submodule
; old_dirty_submodule
= new_dirty_submodule
;
3809 new_dirty_submodule
= tmp
;
3812 if (options
->prefix
&&
3813 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3816 one
= alloc_filespec(concatpath
);
3817 two
= alloc_filespec(concatpath
);
3818 fill_filespec(one
, old_sha1
, old_mode
);
3819 fill_filespec(two
, new_sha1
, new_mode
);
3820 one
->dirty_submodule
= old_dirty_submodule
;
3821 two
->dirty_submodule
= new_dirty_submodule
;
3823 diff_queue(&diff_queued_diff
, one
, two
);
3824 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
3825 DIFF_OPT_SET(options
, HAS_CHANGES
);
3828 void diff_unmerge(struct diff_options
*options
,
3830 unsigned mode
, const unsigned char *sha1
)
3832 struct diff_filespec
*one
, *two
;
3834 if (options
->prefix
&&
3835 strncmp(path
, options
->prefix
, options
->prefix_length
))
3838 one
= alloc_filespec(path
);
3839 two
= alloc_filespec(path
);
3840 fill_filespec(one
, sha1
, mode
);
3841 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;
3844 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
3847 struct diff_tempfile
*temp
;
3848 const char *argv
[3];
3849 const char **arg
= argv
;
3850 struct child_process child
;
3851 struct strbuf buf
= STRBUF_INIT
;
3853 temp
= prepare_temp_file(spec
->path
, spec
);
3855 *arg
++ = temp
->name
;
3858 memset(&child
, 0, sizeof(child
));
3859 child
.use_shell
= 1;
3862 if (start_command(&child
) != 0 ||
3863 strbuf_read(&buf
, child
.out
, 0) < 0 ||
3864 finish_command(&child
) != 0) {
3866 strbuf_release(&buf
);
3868 error("error running textconv command '%s'", pgm
);
3874 return strbuf_detach(&buf
, outsize
);