2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "run-command.h"
17 #ifdef NO_FAST_WORKING_DIRECTORY
18 #define FAST_WORKING_DIRECTORY 0
20 #define FAST_WORKING_DIRECTORY 1
23 static int diff_detect_rename_default
;
24 static int diff_rename_limit_default
= 200;
25 static int diff_suppress_blank_empty
;
26 int diff_use_color_default
= -1;
27 static const char *diff_word_regex_cfg
;
28 static const char *external_diff_cmd_cfg
;
29 int diff_auto_refresh_index
= 1;
30 static int diff_mnemonic_prefix
;
32 static char diff_colors
[][COLOR_MAXLEN
] = {
34 GIT_COLOR_NORMAL
, /* PLAIN */
35 GIT_COLOR_BOLD
, /* METAINFO */
36 GIT_COLOR_CYAN
, /* FRAGINFO */
37 GIT_COLOR_RED
, /* OLD */
38 GIT_COLOR_GREEN
, /* NEW */
39 GIT_COLOR_YELLOW
, /* COMMIT */
40 GIT_COLOR_BG_RED
, /* WHITESPACE */
43 static void diff_filespec_load_driver(struct diff_filespec
*one
);
44 static char *run_textconv(const char *, struct diff_filespec
*, size_t *);
46 static int parse_diff_color_slot(const char *var
, int ofs
)
48 if (!strcasecmp(var
+ofs
, "plain"))
50 if (!strcasecmp(var
+ofs
, "meta"))
52 if (!strcasecmp(var
+ofs
, "frag"))
54 if (!strcasecmp(var
+ofs
, "old"))
56 if (!strcasecmp(var
+ofs
, "new"))
58 if (!strcasecmp(var
+ofs
, "commit"))
60 if (!strcasecmp(var
+ofs
, "whitespace"))
61 return DIFF_WHITESPACE
;
62 die("bad config variable '%s'", var
);
65 static int git_config_rename(const char *var
, const char *value
)
68 return DIFF_DETECT_RENAME
;
69 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
70 return DIFF_DETECT_COPY
;
71 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
75 * These are to give UI layer defaults.
76 * The core-level commands such as git-diff-files should
77 * never be affected by the setting of diff.renames
78 * the user happens to have in the configuration file.
80 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
82 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
83 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
86 if (!strcmp(var
, "diff.renames")) {
87 diff_detect_rename_default
= git_config_rename(var
, value
);
90 if (!strcmp(var
, "diff.autorefreshindex")) {
91 diff_auto_refresh_index
= git_config_bool(var
, value
);
94 if (!strcmp(var
, "diff.mnemonicprefix")) {
95 diff_mnemonic_prefix
= git_config_bool(var
, value
);
98 if (!strcmp(var
, "diff.external"))
99 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
100 if (!strcmp(var
, "diff.wordregex"))
101 return git_config_string(&diff_word_regex_cfg
, var
, value
);
103 return git_diff_basic_config(var
, value
, cb
);
106 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
108 if (!strcmp(var
, "diff.renamelimit")) {
109 diff_rename_limit_default
= git_config_int(var
, value
);
113 switch (userdiff_config(var
, value
)) {
119 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
120 int slot
= parse_diff_color_slot(var
, 11);
122 return config_error_nonbool(var
);
123 color_parse(value
, var
, diff_colors
[slot
]);
127 /* like GNU diff's --suppress-blank-empty option */
128 if (!strcmp(var
, "diff.suppressblankempty") ||
129 /* for backwards compatibility */
130 !strcmp(var
, "diff.suppress-blank-empty")) {
131 diff_suppress_blank_empty
= git_config_bool(var
, value
);
135 return git_color_default_config(var
, value
, cb
);
138 static char *quote_two(const char *one
, const char *two
)
140 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
141 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
142 struct strbuf res
= STRBUF_INIT
;
144 if (need_one
+ need_two
) {
145 strbuf_addch(&res
, '"');
146 quote_c_style(one
, &res
, NULL
, 1);
147 quote_c_style(two
, &res
, NULL
, 1);
148 strbuf_addch(&res
, '"');
150 strbuf_addstr(&res
, one
);
151 strbuf_addstr(&res
, two
);
153 return strbuf_detach(&res
, NULL
);
156 static const char *external_diff(void)
158 static const char *external_diff_cmd
= NULL
;
159 static int done_preparing
= 0;
162 return external_diff_cmd
;
163 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
164 if (!external_diff_cmd
)
165 external_diff_cmd
= external_diff_cmd_cfg
;
167 return external_diff_cmd
;
170 static struct diff_tempfile
{
171 const char *name
; /* filename external diff should read from */
174 char tmp_path
[PATH_MAX
];
177 static struct diff_tempfile
*claim_diff_tempfile(void) {
179 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
180 if (!diff_temp
[i
].name
)
181 return diff_temp
+ i
;
182 die("BUG: diff is failing to clean up its tempfiles");
185 static int remove_tempfile_installed
;
187 static void remove_tempfile(void)
190 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
191 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
)
192 unlink_or_warn(diff_temp
[i
].name
);
193 diff_temp
[i
].name
= NULL
;
197 static void remove_tempfile_on_signal(int signo
)
204 static int count_lines(const char *data
, int size
)
206 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
213 completely_empty
= 0;
217 completely_empty
= 0;
220 if (completely_empty
)
223 count
++; /* no trailing newline */
227 static void print_line_count(FILE *file
, int count
)
231 fprintf(file
, "0,0");
237 fprintf(file
, "1,%d", count
);
242 static void copy_file_with_prefix(FILE *file
,
243 int prefix
, const char *data
, int size
,
244 const char *set
, const char *reset
)
246 int ch
, nl_just_seen
= 1;
261 fprintf(file
, "%s\n\\ No newline at end of file\n", reset
);
264 static void emit_rewrite_diff(const char *name_a
,
266 struct diff_filespec
*one
,
267 struct diff_filespec
*two
,
268 const char *textconv_one
,
269 const char *textconv_two
,
270 struct diff_options
*o
)
273 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
274 const char *name_a_tab
, *name_b_tab
;
275 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
276 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
277 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
278 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
279 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
280 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
281 const char *a_prefix
, *b_prefix
;
282 const char *data_one
, *data_two
;
283 size_t size_one
, size_two
;
285 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
286 a_prefix
= o
->b_prefix
;
287 b_prefix
= o
->a_prefix
;
289 a_prefix
= o
->a_prefix
;
290 b_prefix
= o
->b_prefix
;
293 name_a
+= (*name_a
== '/');
294 name_b
+= (*name_b
== '/');
295 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
296 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
298 strbuf_reset(&a_name
);
299 strbuf_reset(&b_name
);
300 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
301 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
303 diff_populate_filespec(one
, 0);
304 diff_populate_filespec(two
, 0);
306 data_one
= run_textconv(textconv_one
, one
, &size_one
);
308 die("unable to read files to diff");
311 data_one
= one
->data
;
312 size_one
= one
->size
;
315 data_two
= run_textconv(textconv_two
, two
, &size_two
);
317 die("unable to read files to diff");
320 data_two
= two
->data
;
321 size_two
= two
->size
;
324 lc_a
= count_lines(data_one
, size_one
);
325 lc_b
= count_lines(data_two
, size_two
);
327 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
328 metainfo
, a_name
.buf
, name_a_tab
, reset
,
329 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
330 print_line_count(o
->file
, lc_a
);
331 fprintf(o
->file
, " +");
332 print_line_count(o
->file
, lc_b
);
333 fprintf(o
->file
, " @@%s\n", reset
);
335 copy_file_with_prefix(o
->file
, '-', data_one
, size_one
, old
, reset
);
337 copy_file_with_prefix(o
->file
, '+', data_two
, size_two
, new, reset
);
340 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
342 if (!DIFF_FILE_VALID(one
)) {
343 mf
->ptr
= (char *)""; /* does not matter */
347 else if (diff_populate_filespec(one
, 0))
351 mf
->size
= one
->size
;
355 struct diff_words_buffer
{
358 struct diff_words_orig
{
359 const char *begin
, *end
;
361 int orig_nr
, orig_alloc
;
364 static void diff_words_append(char *line
, unsigned long len
,
365 struct diff_words_buffer
*buffer
)
367 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
370 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
371 buffer
->text
.size
+= len
;
372 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
375 struct diff_words_data
{
376 struct diff_words_buffer minus
, plus
;
377 const char *current_plus
;
382 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
384 struct diff_words_data
*diff_words
= priv
;
385 int minus_first
, minus_len
, plus_first
, plus_len
;
386 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
388 if (line
[0] != '@' || parse_hunk_header(line
, len
,
389 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
392 /* POSIX requires that first be decremented by one if len == 0... */
394 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
396 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
398 minus_begin
= minus_end
=
399 diff_words
->minus
.orig
[minus_first
].end
;
402 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
403 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
405 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
407 if (diff_words
->current_plus
!= plus_begin
)
408 fwrite(diff_words
->current_plus
,
409 plus_begin
- diff_words
->current_plus
, 1,
411 if (minus_begin
!= minus_end
)
412 color_fwrite_lines(diff_words
->file
,
413 diff_get_color(1, DIFF_FILE_OLD
),
414 minus_end
- minus_begin
, minus_begin
);
415 if (plus_begin
!= plus_end
)
416 color_fwrite_lines(diff_words
->file
,
417 diff_get_color(1, DIFF_FILE_NEW
),
418 plus_end
- plus_begin
, plus_begin
);
420 diff_words
->current_plus
= plus_end
;
423 /* This function starts looking at *begin, and returns 0 iff a word was found. */
424 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
425 int *begin
, int *end
)
427 if (word_regex
&& *begin
< buffer
->size
) {
429 if (!regexec(word_regex
, buffer
->ptr
+ *begin
, 1, match
, 0)) {
430 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
431 '\n', match
[0].rm_eo
- match
[0].rm_so
);
432 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
433 *begin
+= match
[0].rm_so
;
434 return *begin
>= *end
;
439 /* find the next word */
440 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
442 if (*begin
>= buffer
->size
)
445 /* find the end of the word */
447 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
454 * This function splits the words in buffer->text, stores the list with
455 * newline separator into out, and saves the offsets of the original words
458 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
467 /* fake an empty "0th" word */
468 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
469 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
472 for (i
= 0; i
< buffer
->text
.size
; i
++) {
473 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
476 /* store original boundaries */
477 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
479 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
480 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
484 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
485 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
486 out
->ptr
[out
->size
+ j
- i
] = '\n';
487 out
->size
+= j
- i
+ 1;
493 /* this executes the word diff on the accumulated buffers */
494 static void diff_words_show(struct diff_words_data
*diff_words
)
499 mmfile_t minus
, plus
;
501 /* special case: only removal */
502 if (!diff_words
->plus
.text
.size
) {
503 color_fwrite_lines(diff_words
->file
,
504 diff_get_color(1, DIFF_FILE_OLD
),
505 diff_words
->minus
.text
.size
, diff_words
->minus
.text
.ptr
);
506 diff_words
->minus
.text
.size
= 0;
510 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
512 memset(&xpp
, 0, sizeof(xpp
));
513 memset(&xecfg
, 0, sizeof(xecfg
));
514 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
515 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
516 xpp
.flags
= XDF_NEED_MINIMAL
;
517 /* as only the hunk header will be parsed, we need a 0-context */
519 xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
523 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
524 diff_words
->plus
.text
.size
)
525 fwrite(diff_words
->current_plus
,
526 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
527 - diff_words
->current_plus
, 1,
529 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
532 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
534 struct emit_callback
{
535 int nparents
, color_diff
;
537 sane_truncate_fn truncate
;
538 const char **label_path
;
539 struct diff_words_data
*diff_words
;
544 static void free_diff_words_data(struct emit_callback
*ecbdata
)
546 if (ecbdata
->diff_words
) {
548 if (ecbdata
->diff_words
->minus
.text
.size
||
549 ecbdata
->diff_words
->plus
.text
.size
)
550 diff_words_show(ecbdata
->diff_words
);
552 free (ecbdata
->diff_words
->minus
.text
.ptr
);
553 free (ecbdata
->diff_words
->minus
.orig
);
554 free (ecbdata
->diff_words
->plus
.text
.ptr
);
555 free (ecbdata
->diff_words
->plus
.orig
);
556 free(ecbdata
->diff_words
->word_regex
);
557 free(ecbdata
->diff_words
);
558 ecbdata
->diff_words
= NULL
;
562 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
565 return diff_colors
[ix
];
569 static void emit_line(FILE *file
, const char *set
, const char *reset
, const char *line
, int len
)
571 int has_trailing_newline
, has_trailing_carriage_return
;
573 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
574 if (has_trailing_newline
)
576 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
577 if (has_trailing_carriage_return
)
581 fwrite(line
, len
, 1, file
);
583 if (has_trailing_carriage_return
)
585 if (has_trailing_newline
)
589 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
591 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
592 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
595 emit_line(ecbdata
->file
, set
, reset
, line
, len
);
597 /* Emit just the prefix, then the rest. */
598 emit_line(ecbdata
->file
, set
, reset
, line
, ecbdata
->nparents
);
599 ws_check_emit(line
+ ecbdata
->nparents
,
600 len
- ecbdata
->nparents
, ecbdata
->ws_rule
,
601 ecbdata
->file
, set
, reset
, ws
);
605 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
612 return ecb
->truncate(line
, len
);
616 (void) utf8_width(&cp
, &l
);
618 break; /* truncated in the middle? */
623 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
627 struct emit_callback
*ecbdata
= priv
;
628 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
629 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
630 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
632 *(ecbdata
->found_changesp
) = 1;
634 if (ecbdata
->label_path
[0]) {
635 const char *name_a_tab
, *name_b_tab
;
637 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
638 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
640 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
641 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
642 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
643 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
644 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
647 if (diff_suppress_blank_empty
648 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
653 /* This is not really necessary for now because
654 * this codepath only deals with two-way diffs.
656 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
658 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
659 ecbdata
->nparents
= i
- 1;
660 len
= sane_truncate_line(ecbdata
, line
, len
);
661 emit_line(ecbdata
->file
,
662 diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
664 if (line
[len
-1] != '\n')
665 putc('\n', ecbdata
->file
);
669 if (len
< ecbdata
->nparents
) {
670 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
675 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
676 /* fall back to normal diff */
677 free_diff_words_data(ecbdata
);
678 if (ecbdata
->diff_words
) {
679 if (line
[0] == '-') {
680 diff_words_append(line
, len
,
681 &ecbdata
->diff_words
->minus
);
683 } else if (line
[0] == '+') {
684 diff_words_append(line
, len
,
685 &ecbdata
->diff_words
->plus
);
688 if (ecbdata
->diff_words
->minus
.text
.size
||
689 ecbdata
->diff_words
->plus
.text
.size
)
690 diff_words_show(ecbdata
->diff_words
);
693 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
696 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
698 color
= DIFF_FILE_OLD
;
699 else if (line
[i
] == '+')
700 color
= DIFF_FILE_NEW
;
703 if (color
!= DIFF_FILE_NEW
) {
704 emit_line(ecbdata
->file
,
705 diff_get_color(ecbdata
->color_diff
, color
),
709 emit_add_line(reset
, ecbdata
, line
, len
);
712 static char *pprint_rename(const char *a
, const char *b
)
716 struct strbuf name
= STRBUF_INIT
;
717 int pfx_length
, sfx_length
;
718 int len_a
= strlen(a
);
719 int len_b
= strlen(b
);
720 int a_midlen
, b_midlen
;
721 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
722 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
724 if (qlen_a
|| qlen_b
) {
725 quote_c_style(a
, &name
, NULL
, 0);
726 strbuf_addstr(&name
, " => ");
727 quote_c_style(b
, &name
, NULL
, 0);
728 return strbuf_detach(&name
, NULL
);
731 /* Find common prefix */
733 while (*old
&& *new && *old
== *new) {
735 pfx_length
= old
- a
+ 1;
740 /* Find common suffix */
744 while (a
<= old
&& b
<= new && *old
== *new) {
746 sfx_length
= len_a
- (old
- a
);
752 * pfx{mid-a => mid-b}sfx
753 * {pfx-a => pfx-b}sfx
754 * pfx{sfx-a => sfx-b}
757 a_midlen
= len_a
- pfx_length
- sfx_length
;
758 b_midlen
= len_b
- pfx_length
- sfx_length
;
764 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
765 if (pfx_length
+ sfx_length
) {
766 strbuf_add(&name
, a
, pfx_length
);
767 strbuf_addch(&name
, '{');
769 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
770 strbuf_addstr(&name
, " => ");
771 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
772 if (pfx_length
+ sfx_length
) {
773 strbuf_addch(&name
, '}');
774 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
776 return strbuf_detach(&name
, NULL
);
782 struct diffstat_file
{
786 unsigned is_unmerged
:1;
787 unsigned is_binary
:1;
788 unsigned is_renamed
:1;
789 unsigned int added
, deleted
;
793 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
797 struct diffstat_file
*x
;
798 x
= xcalloc(sizeof (*x
), 1);
799 if (diffstat
->nr
== diffstat
->alloc
) {
800 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
801 diffstat
->files
= xrealloc(diffstat
->files
,
802 diffstat
->alloc
* sizeof(x
));
804 diffstat
->files
[diffstat
->nr
++] = x
;
806 x
->from_name
= xstrdup(name_a
);
807 x
->name
= xstrdup(name_b
);
812 x
->name
= xstrdup(name_a
);
817 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
819 struct diffstat_t
*diffstat
= priv
;
820 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
824 else if (line
[0] == '-')
828 const char mime_boundary_leader
[] = "------------";
830 static int scale_linear(int it
, int width
, int max_change
)
833 * make sure that at least one '-' is printed if there were deletions,
834 * and likewise for '+'.
838 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
841 static void show_name(FILE *file
,
842 const char *prefix
, const char *name
, int len
)
844 fprintf(file
, " %s%-*s |", prefix
, len
, name
);
847 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
851 fprintf(file
, "%s", set
);
854 fprintf(file
, "%s", reset
);
857 static void fill_print_name(struct diffstat_file
*file
)
861 if (file
->print_name
)
864 if (!file
->is_renamed
) {
865 struct strbuf buf
= STRBUF_INIT
;
866 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
867 pname
= strbuf_detach(&buf
, NULL
);
870 strbuf_release(&buf
);
873 pname
= pprint_rename(file
->from_name
, file
->name
);
875 file
->print_name
= pname
;
878 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
880 int i
, len
, add
, del
, adds
= 0, dels
= 0;
881 int max_change
= 0, max_len
= 0;
882 int total_files
= data
->nr
;
883 int width
, name_width
;
884 const char *reset
, *set
, *add_c
, *del_c
;
889 width
= options
->stat_width
? options
->stat_width
: 80;
890 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
892 /* Sanity: give at least 5 columns to the graph,
893 * but leave at least 10 columns for the name.
899 else if (width
< name_width
+ 15)
900 name_width
= width
- 15;
902 /* Find the longest filename and max number of changes */
903 reset
= diff_get_color_opt(options
, DIFF_RESET
);
904 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
905 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
906 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
908 for (i
= 0; i
< data
->nr
; i
++) {
909 struct diffstat_file
*file
= data
->files
[i
];
910 int change
= file
->added
+ file
->deleted
;
911 fill_print_name(file
);
912 len
= strlen(file
->print_name
);
916 if (file
->is_binary
|| file
->is_unmerged
)
918 if (max_change
< change
)
922 /* Compute the width of the graph part;
923 * 10 is for one blank at the beginning of the line plus
924 * " | count " between the name and the graph.
926 * From here on, name_width is the width of the name area,
927 * and width is the width of the graph area.
929 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
930 if (width
< (name_width
+ 10) + max_change
)
931 width
= width
- (name_width
+ 10);
935 for (i
= 0; i
< data
->nr
; i
++) {
936 const char *prefix
= "";
937 char *name
= data
->files
[i
]->print_name
;
938 int added
= data
->files
[i
]->added
;
939 int deleted
= data
->files
[i
]->deleted
;
943 * "scale" the filename
946 name_len
= strlen(name
);
947 if (name_width
< name_len
) {
951 name
+= name_len
- len
;
952 slash
= strchr(name
, '/');
957 if (data
->files
[i
]->is_binary
) {
958 show_name(options
->file
, prefix
, name
, len
);
959 fprintf(options
->file
, " Bin ");
960 fprintf(options
->file
, "%s%d%s", del_c
, deleted
, reset
);
961 fprintf(options
->file
, " -> ");
962 fprintf(options
->file
, "%s%d%s", add_c
, added
, reset
);
963 fprintf(options
->file
, " bytes");
964 fprintf(options
->file
, "\n");
967 else if (data
->files
[i
]->is_unmerged
) {
968 show_name(options
->file
, prefix
, name
, len
);
969 fprintf(options
->file
, " Unmerged\n");
972 else if (!data
->files
[i
]->is_renamed
&&
973 (added
+ deleted
== 0)) {
979 * scale the add/delete
986 if (width
<= max_change
) {
987 add
= scale_linear(add
, width
, max_change
);
988 del
= scale_linear(del
, width
, max_change
);
990 show_name(options
->file
, prefix
, name
, len
);
991 fprintf(options
->file
, "%5d%s", added
+ deleted
,
992 added
+ deleted
? " " : "");
993 show_graph(options
->file
, '+', add
, add_c
, reset
);
994 show_graph(options
->file
, '-', del
, del_c
, reset
);
995 fprintf(options
->file
, "\n");
997 fprintf(options
->file
,
998 " %d files changed, %d insertions(+), %d deletions(-)\n",
999 total_files
, adds
, dels
);
1002 static void show_shortstats(struct diffstat_t
* data
, struct diff_options
*options
)
1004 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
1009 for (i
= 0; i
< data
->nr
; i
++) {
1010 if (!data
->files
[i
]->is_binary
&&
1011 !data
->files
[i
]->is_unmerged
) {
1012 int added
= data
->files
[i
]->added
;
1013 int deleted
= data
->files
[i
]->deleted
;
1014 if (!data
->files
[i
]->is_renamed
&&
1015 (added
+ deleted
== 0)) {
1023 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
1024 total_files
, adds
, dels
);
1027 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
1034 for (i
= 0; i
< data
->nr
; i
++) {
1035 struct diffstat_file
*file
= data
->files
[i
];
1037 if (file
->is_binary
)
1038 fprintf(options
->file
, "-\t-\t");
1040 fprintf(options
->file
,
1041 "%d\t%d\t", file
->added
, file
->deleted
);
1042 if (options
->line_termination
) {
1043 fill_print_name(file
);
1044 if (!file
->is_renamed
)
1045 write_name_quoted(file
->name
, options
->file
,
1046 options
->line_termination
);
1048 fputs(file
->print_name
, options
->file
);
1049 putc(options
->line_termination
, options
->file
);
1052 if (file
->is_renamed
) {
1053 putc('\0', options
->file
);
1054 write_name_quoted(file
->from_name
, options
->file
, '\0');
1056 write_name_quoted(file
->name
, options
->file
, '\0');
1061 struct dirstat_file
{
1063 unsigned long changed
;
1066 struct dirstat_dir
{
1067 struct dirstat_file
*files
;
1068 int alloc
, nr
, percent
, cumulative
;
1071 static long gather_dirstat(FILE *file
, struct dirstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1073 unsigned long this_dir
= 0;
1074 unsigned int sources
= 0;
1077 struct dirstat_file
*f
= dir
->files
;
1078 int namelen
= strlen(f
->name
);
1082 if (namelen
< baselen
)
1084 if (memcmp(f
->name
, base
, baselen
))
1086 slash
= strchr(f
->name
+ baselen
, '/');
1088 int newbaselen
= slash
+ 1 - f
->name
;
1089 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1101 * We don't report dirstat's for
1103 * - or cases where everything came from a single directory
1104 * under this directory (sources == 1).
1106 if (baselen
&& sources
!= 1) {
1107 int permille
= this_dir
* 1000 / changed
;
1109 int percent
= permille
/ 10;
1110 if (percent
>= dir
->percent
) {
1111 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1112 if (!dir
->cumulative
)
1120 static int dirstat_compare(const void *_a
, const void *_b
)
1122 const struct dirstat_file
*a
= _a
;
1123 const struct dirstat_file
*b
= _b
;
1124 return strcmp(a
->name
, b
->name
);
1127 static void show_dirstat(struct diff_options
*options
)
1130 unsigned long changed
;
1131 struct dirstat_dir dir
;
1132 struct diff_queue_struct
*q
= &diff_queued_diff
;
1137 dir
.percent
= options
->dirstat_percent
;
1138 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1141 for (i
= 0; i
< q
->nr
; i
++) {
1142 struct diff_filepair
*p
= q
->queue
[i
];
1144 unsigned long copied
, added
, damage
;
1146 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1148 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1149 diff_populate_filespec(p
->one
, 0);
1150 diff_populate_filespec(p
->two
, 0);
1151 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1153 diff_free_filespec_data(p
->one
);
1154 diff_free_filespec_data(p
->two
);
1155 } else if (DIFF_FILE_VALID(p
->one
)) {
1156 diff_populate_filespec(p
->one
, 1);
1158 diff_free_filespec_data(p
->one
);
1159 } else if (DIFF_FILE_VALID(p
->two
)) {
1160 diff_populate_filespec(p
->two
, 1);
1162 added
= p
->two
->size
;
1163 diff_free_filespec_data(p
->two
);
1168 * Original minus copied is the removed material,
1169 * added is the new material. They are both damages
1170 * made to the preimage. In --dirstat-by-file mode, count
1171 * damaged files, not damaged lines. This is done by
1172 * counting only a single damaged line per file.
1174 damage
= (p
->one
->size
- copied
) + added
;
1175 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
) && damage
> 0)
1178 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1179 dir
.files
[dir
.nr
].name
= name
;
1180 dir
.files
[dir
.nr
].changed
= damage
;
1185 /* This can happen even with many files, if everything was renames */
1189 /* Show all directories with more than x% of the changes */
1190 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
1191 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1194 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1197 for (i
= 0; i
< diffstat
->nr
; i
++) {
1198 struct diffstat_file
*f
= diffstat
->files
[i
];
1199 if (f
->name
!= f
->print_name
)
1200 free(f
->print_name
);
1205 free(diffstat
->files
);
1208 struct checkdiff_t
{
1209 const char *filename
;
1211 struct diff_options
*o
;
1214 int trailing_blanks_start
;
1217 static int is_conflict_marker(const char *line
, unsigned long len
)
1224 firstchar
= line
[0];
1225 switch (firstchar
) {
1226 case '=': case '>': case '<':
1231 for (cnt
= 1; cnt
< 7; cnt
++)
1232 if (line
[cnt
] != firstchar
)
1234 /* line[0] thru line[6] are same as firstchar */
1235 if (firstchar
== '=') {
1236 /* divider between ours and theirs? */
1237 if (len
!= 8 || line
[7] != '\n')
1239 } else if (len
< 8 || !isspace(line
[7])) {
1240 /* not divider before ours nor after theirs */
1246 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1248 struct checkdiff_t
*data
= priv
;
1249 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1250 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1251 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1252 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1255 if (line
[0] == '+') {
1258 if (!ws_blank_line(line
+ 1, len
- 1, data
->ws_rule
))
1259 data
->trailing_blanks_start
= 0;
1260 else if (!data
->trailing_blanks_start
)
1261 data
->trailing_blanks_start
= data
->lineno
;
1262 if (is_conflict_marker(line
+ 1, len
- 1)) {
1264 fprintf(data
->o
->file
,
1265 "%s:%d: leftover conflict marker\n",
1266 data
->filename
, data
->lineno
);
1268 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1271 data
->status
|= bad
;
1272 err
= whitespace_error_string(bad
);
1273 fprintf(data
->o
->file
, "%s:%d: %s.\n",
1274 data
->filename
, data
->lineno
, err
);
1276 emit_line(data
->o
->file
, set
, reset
, line
, 1);
1277 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1278 data
->o
->file
, set
, reset
, ws
);
1279 } else if (line
[0] == ' ') {
1281 data
->trailing_blanks_start
= 0;
1282 } else if (line
[0] == '@') {
1283 char *plus
= strchr(line
, '+');
1285 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1287 die("invalid diff");
1288 data
->trailing_blanks_start
= 0;
1292 static unsigned char *deflate_it(char *data
,
1294 unsigned long *result_size
)
1297 unsigned char *deflated
;
1300 memset(&stream
, 0, sizeof(stream
));
1301 deflateInit(&stream
, zlib_compression_level
);
1302 bound
= deflateBound(&stream
, size
);
1303 deflated
= xmalloc(bound
);
1304 stream
.next_out
= deflated
;
1305 stream
.avail_out
= bound
;
1307 stream
.next_in
= (unsigned char *)data
;
1308 stream
.avail_in
= size
;
1309 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1311 deflateEnd(&stream
);
1312 *result_size
= stream
.total_out
;
1316 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1322 unsigned long orig_size
;
1323 unsigned long delta_size
;
1324 unsigned long deflate_size
;
1325 unsigned long data_size
;
1327 /* We could do deflated delta, or we could do just deflated two,
1328 * whichever is smaller.
1331 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1332 if (one
->size
&& two
->size
) {
1333 delta
= diff_delta(one
->ptr
, one
->size
,
1334 two
->ptr
, two
->size
,
1335 &delta_size
, deflate_size
);
1337 void *to_free
= delta
;
1338 orig_size
= delta_size
;
1339 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1344 if (delta
&& delta_size
< deflate_size
) {
1345 fprintf(file
, "delta %lu\n", orig_size
);
1348 data_size
= delta_size
;
1351 fprintf(file
, "literal %lu\n", two
->size
);
1354 data_size
= deflate_size
;
1357 /* emit data encoded in base85 */
1360 int bytes
= (52 < data_size
) ? 52 : data_size
;
1364 line
[0] = bytes
+ 'A' - 1;
1366 line
[0] = bytes
- 26 + 'a' - 1;
1367 encode_85(line
+ 1, cp
, bytes
);
1368 cp
= (char *) cp
+ bytes
;
1372 fprintf(file
, "\n");
1376 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1378 fprintf(file
, "GIT binary patch\n");
1379 emit_binary_diff_body(file
, one
, two
);
1380 emit_binary_diff_body(file
, two
, one
);
1383 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1386 one
->driver
= userdiff_find_by_path(one
->path
);
1388 one
->driver
= userdiff_find_by_name("default");
1391 int diff_filespec_is_binary(struct diff_filespec
*one
)
1393 if (one
->is_binary
== -1) {
1394 diff_filespec_load_driver(one
);
1395 if (one
->driver
->binary
!= -1)
1396 one
->is_binary
= one
->driver
->binary
;
1398 if (!one
->data
&& DIFF_FILE_VALID(one
))
1399 diff_populate_filespec(one
, 0);
1401 one
->is_binary
= buffer_is_binary(one
->data
,
1403 if (one
->is_binary
== -1)
1407 return one
->is_binary
;
1410 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
1412 diff_filespec_load_driver(one
);
1413 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
1416 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1418 diff_filespec_load_driver(one
);
1419 return one
->driver
->word_regex
;
1422 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
1424 if (!options
->a_prefix
)
1425 options
->a_prefix
= a
;
1426 if (!options
->b_prefix
)
1427 options
->b_prefix
= b
;
1430 static const char *get_textconv(struct diff_filespec
*one
)
1432 if (!DIFF_FILE_VALID(one
))
1434 if (!S_ISREG(one
->mode
))
1436 diff_filespec_load_driver(one
);
1437 return one
->driver
->textconv
;
1440 static void builtin_diff(const char *name_a
,
1442 struct diff_filespec
*one
,
1443 struct diff_filespec
*two
,
1444 const char *xfrm_msg
,
1445 struct diff_options
*o
,
1446 int complete_rewrite
)
1450 char *a_one
, *b_two
;
1451 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1452 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1453 const char *a_prefix
, *b_prefix
;
1454 const char *textconv_one
= NULL
, *textconv_two
= NULL
;
1456 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
1457 textconv_one
= get_textconv(one
);
1458 textconv_two
= get_textconv(two
);
1461 diff_set_mnemonic_prefix(o
, "a/", "b/");
1462 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
1463 a_prefix
= o
->b_prefix
;
1464 b_prefix
= o
->a_prefix
;
1466 a_prefix
= o
->a_prefix
;
1467 b_prefix
= o
->b_prefix
;
1470 /* Never use a non-valid filename anywhere if at all possible */
1471 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
1472 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
1474 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
1475 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
1476 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1477 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1478 fprintf(o
->file
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1479 if (lbl
[0][0] == '/') {
1481 fprintf(o
->file
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1482 if (xfrm_msg
&& xfrm_msg
[0])
1483 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1485 else if (lbl
[1][0] == '/') {
1486 fprintf(o
->file
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1487 if (xfrm_msg
&& xfrm_msg
[0])
1488 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1491 if (one
->mode
!= two
->mode
) {
1492 fprintf(o
->file
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1493 fprintf(o
->file
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1495 if (xfrm_msg
&& xfrm_msg
[0])
1496 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1498 * we do not run diff between different kind
1501 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1502 goto free_ab_and_return
;
1503 if (complete_rewrite
&&
1504 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
1505 (textconv_two
|| !diff_filespec_is_binary(two
))) {
1506 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1507 textconv_one
, textconv_two
, o
);
1508 o
->found_changes
= 1;
1509 goto free_ab_and_return
;
1513 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1514 die("unable to read files to diff");
1516 if (!DIFF_OPT_TST(o
, TEXT
) &&
1517 ( (diff_filespec_is_binary(one
) && !textconv_one
) ||
1518 (diff_filespec_is_binary(two
) && !textconv_two
) )) {
1519 /* Quite common confusing case */
1520 if (mf1
.size
== mf2
.size
&&
1521 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1522 goto free_ab_and_return
;
1523 if (DIFF_OPT_TST(o
, BINARY
))
1524 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1526 fprintf(o
->file
, "Binary files %s and %s differ\n",
1528 o
->found_changes
= 1;
1531 /* Crazy xdl interfaces.. */
1532 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1536 struct emit_callback ecbdata
;
1537 const struct userdiff_funcname
*pe
;
1541 mf1
.ptr
= run_textconv(textconv_one
, one
, &size
);
1543 die("unable to read files to diff");
1548 mf2
.ptr
= run_textconv(textconv_two
, two
, &size
);
1550 die("unable to read files to diff");
1554 pe
= diff_funcname_pattern(one
);
1556 pe
= diff_funcname_pattern(two
);
1558 memset(&xpp
, 0, sizeof(xpp
));
1559 memset(&xecfg
, 0, sizeof(xecfg
));
1560 memset(&ecbdata
, 0, sizeof(ecbdata
));
1561 ecbdata
.label_path
= lbl
;
1562 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1563 ecbdata
.found_changesp
= &o
->found_changes
;
1564 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1565 ecbdata
.file
= o
->file
;
1566 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1567 xecfg
.ctxlen
= o
->context
;
1568 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
1569 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1571 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
1574 else if (!prefixcmp(diffopts
, "--unified="))
1575 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1576 else if (!prefixcmp(diffopts
, "-u"))
1577 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1578 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
)) {
1579 ecbdata
.diff_words
=
1580 xcalloc(1, sizeof(struct diff_words_data
));
1581 ecbdata
.diff_words
->file
= o
->file
;
1583 o
->word_regex
= userdiff_word_regex(one
);
1585 o
->word_regex
= userdiff_word_regex(two
);
1587 o
->word_regex
= diff_word_regex_cfg
;
1588 if (o
->word_regex
) {
1589 ecbdata
.diff_words
->word_regex
= (regex_t
*)
1590 xmalloc(sizeof(regex_t
));
1591 if (regcomp(ecbdata
.diff_words
->word_regex
,
1593 REG_EXTENDED
| REG_NEWLINE
))
1594 die ("Invalid regular expression: %s",
1598 xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
1599 &xpp
, &xecfg
, &ecb
);
1600 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
))
1601 free_diff_words_data(&ecbdata
);
1606 xdiff_clear_find_func(&xecfg
);
1610 diff_free_filespec_data(one
);
1611 diff_free_filespec_data(two
);
1617 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1618 struct diff_filespec
*one
,
1619 struct diff_filespec
*two
,
1620 struct diffstat_t
*diffstat
,
1621 struct diff_options
*o
,
1622 int complete_rewrite
)
1625 struct diffstat_file
*data
;
1627 data
= diffstat_add(diffstat
, name_a
, name_b
);
1630 data
->is_unmerged
= 1;
1633 if (complete_rewrite
) {
1634 diff_populate_filespec(one
, 0);
1635 diff_populate_filespec(two
, 0);
1636 data
->deleted
= count_lines(one
->data
, one
->size
);
1637 data
->added
= count_lines(two
->data
, two
->size
);
1638 goto free_and_return
;
1640 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1641 die("unable to read files to diff");
1643 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1644 data
->is_binary
= 1;
1645 data
->added
= mf2
.size
;
1646 data
->deleted
= mf1
.size
;
1648 /* Crazy xdl interfaces.. */
1653 memset(&xpp
, 0, sizeof(xpp
));
1654 memset(&xecfg
, 0, sizeof(xecfg
));
1655 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1656 xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
1657 &xpp
, &xecfg
, &ecb
);
1661 diff_free_filespec_data(one
);
1662 diff_free_filespec_data(two
);
1665 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1666 const char *attr_path
,
1667 struct diff_filespec
*one
,
1668 struct diff_filespec
*two
,
1669 struct diff_options
*o
)
1672 struct checkdiff_t data
;
1677 memset(&data
, 0, sizeof(data
));
1678 data
.filename
= name_b
? name_b
: name_a
;
1681 data
.ws_rule
= whitespace_rule(attr_path
);
1683 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1684 die("unable to read files to diff");
1687 * All the other codepaths check both sides, but not checking
1688 * the "old" side here is deliberate. We are checking the newly
1689 * introduced changes, and as long as the "new" side is text, we
1690 * can and should check what it introduces.
1692 if (diff_filespec_is_binary(two
))
1693 goto free_and_return
;
1695 /* Crazy xdl interfaces.. */
1700 memset(&xpp
, 0, sizeof(xpp
));
1701 memset(&xecfg
, 0, sizeof(xecfg
));
1702 xecfg
.ctxlen
= 1; /* at least one context line */
1703 xpp
.flags
= XDF_NEED_MINIMAL
;
1704 xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
1705 &xpp
, &xecfg
, &ecb
);
1707 if ((data
.ws_rule
& WS_TRAILING_SPACE
) &&
1708 data
.trailing_blanks_start
) {
1709 fprintf(o
->file
, "%s:%d: ends with blank lines.\n",
1710 data
.filename
, data
.trailing_blanks_start
);
1711 data
.status
= 1; /* report errors */
1715 diff_free_filespec_data(one
);
1716 diff_free_filespec_data(two
);
1718 DIFF_OPT_SET(o
, CHECK_FAILED
);
1721 struct diff_filespec
*alloc_filespec(const char *path
)
1723 int namelen
= strlen(path
);
1724 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1726 memset(spec
, 0, sizeof(*spec
));
1727 spec
->path
= (char *)(spec
+ 1);
1728 memcpy(spec
->path
, path
, namelen
+1);
1730 spec
->is_binary
= -1;
1734 void free_filespec(struct diff_filespec
*spec
)
1736 if (!--spec
->count
) {
1737 diff_free_filespec_data(spec
);
1742 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1743 unsigned short mode
)
1746 spec
->mode
= canon_mode(mode
);
1747 hashcpy(spec
->sha1
, sha1
);
1748 spec
->sha1_valid
= !is_null_sha1(sha1
);
1753 * Given a name and sha1 pair, if the index tells us the file in
1754 * the work tree has that object contents, return true, so that
1755 * prepare_temp_file() does not have to inflate and extract.
1757 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1759 struct cache_entry
*ce
;
1764 * We do not read the cache ourselves here, because the
1765 * benchmark with my previous version that always reads cache
1766 * shows that it makes things worse for diff-tree comparing
1767 * two linux-2.6 kernel trees in an already checked out work
1768 * tree. This is because most diff-tree comparisons deal with
1769 * only a small number of files, while reading the cache is
1770 * expensive for a large project, and its cost outweighs the
1771 * savings we get by not inflating the object to a temporary
1772 * file. Practically, this code only helps when we are used
1773 * by diff-cache --cached, which does read the cache before
1779 /* We want to avoid the working directory if our caller
1780 * doesn't need the data in a normal file, this system
1781 * is rather slow with its stat/open/mmap/close syscalls,
1782 * and the object is contained in a pack file. The pack
1783 * is probably already open and will be faster to obtain
1784 * the data through than the working directory. Loose
1785 * objects however would tend to be slower as they need
1786 * to be individually opened and inflated.
1788 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
))
1792 pos
= cache_name_pos(name
, len
);
1795 ce
= active_cache
[pos
];
1798 * This is not the sha1 we are looking for, or
1799 * unreusable because it is not a regular file.
1801 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
1805 * If ce is marked as "assume unchanged", there is no
1806 * guarantee that work tree matches what we are looking for.
1808 if (ce
->ce_flags
& CE_VALID
)
1812 * If ce matches the file in the work tree, we can reuse it.
1814 if (ce_uptodate(ce
) ||
1815 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
1821 static int populate_from_stdin(struct diff_filespec
*s
)
1823 struct strbuf buf
= STRBUF_INIT
;
1826 if (strbuf_read(&buf
, 0, 0) < 0)
1827 return error("error while reading from stdin %s",
1830 s
->should_munmap
= 0;
1831 s
->data
= strbuf_detach(&buf
, &size
);
1837 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1840 char *data
= xmalloc(100);
1841 len
= snprintf(data
, 100,
1842 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1854 * While doing rename detection and pickaxe operation, we may need to
1855 * grab the data for the blob (or file) for our own in-core comparison.
1856 * diff_filespec has data and size fields for this purpose.
1858 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1861 if (!DIFF_FILE_VALID(s
))
1862 die("internal error: asking to populate invalid file.");
1863 if (S_ISDIR(s
->mode
))
1869 if (size_only
&& 0 < s
->size
)
1872 if (S_ISGITLINK(s
->mode
))
1873 return diff_populate_gitlink(s
, size_only
);
1875 if (!s
->sha1_valid
||
1876 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1877 struct strbuf buf
= STRBUF_INIT
;
1881 if (!strcmp(s
->path
, "-"))
1882 return populate_from_stdin(s
);
1884 if (lstat(s
->path
, &st
) < 0) {
1885 if (errno
== ENOENT
) {
1889 s
->data
= (char *)"";
1894 s
->size
= xsize_t(st
.st_size
);
1897 if (S_ISLNK(st
.st_mode
)) {
1898 struct strbuf sb
= STRBUF_INIT
;
1900 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
1903 s
->data
= strbuf_detach(&sb
, NULL
);
1909 fd
= open(s
->path
, O_RDONLY
);
1912 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1914 s
->should_munmap
= 1;
1917 * Convert from working tree format to canonical git format
1919 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
1921 munmap(s
->data
, s
->size
);
1922 s
->should_munmap
= 0;
1923 s
->data
= strbuf_detach(&buf
, &size
);
1929 enum object_type type
;
1931 type
= sha1_object_info(s
->sha1
, &s
->size
);
1933 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1940 void diff_free_filespec_blob(struct diff_filespec
*s
)
1944 else if (s
->should_munmap
)
1945 munmap(s
->data
, s
->size
);
1947 if (s
->should_free
|| s
->should_munmap
) {
1948 s
->should_free
= s
->should_munmap
= 0;
1953 void diff_free_filespec_data(struct diff_filespec
*s
)
1955 diff_free_filespec_blob(s
);
1960 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
1963 const unsigned char *sha1
,
1967 struct strbuf buf
= STRBUF_INIT
;
1968 struct strbuf
template = STRBUF_INIT
;
1969 char *path_dup
= xstrdup(path
);
1970 const char *base
= basename(path_dup
);
1972 /* Generate "XXXXXX_basename.ext" */
1973 strbuf_addstr(&template, "XXXXXX_");
1974 strbuf_addstr(&template, base
);
1976 fd
= git_mkstemps(temp
->tmp_path
, PATH_MAX
, template.buf
,
1979 die_errno("unable to create temp-file");
1980 if (convert_to_working_tree(path
,
1981 (const char *)blob
, (size_t)size
, &buf
)) {
1985 if (write_in_full(fd
, blob
, size
) != size
)
1986 die_errno("unable to write temp-file");
1988 temp
->name
= temp
->tmp_path
;
1989 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1991 sprintf(temp
->mode
, "%06o", mode
);
1992 strbuf_release(&buf
);
1993 strbuf_release(&template);
1997 static struct diff_tempfile
*prepare_temp_file(const char *name
,
1998 struct diff_filespec
*one
)
2000 struct diff_tempfile
*temp
= claim_diff_tempfile();
2002 if (!DIFF_FILE_VALID(one
)) {
2004 /* A '-' entry produces this for file-2, and
2005 * a '+' entry produces this for file-1.
2007 temp
->name
= "/dev/null";
2008 strcpy(temp
->hex
, ".");
2009 strcpy(temp
->mode
, ".");
2013 if (!remove_tempfile_installed
) {
2014 atexit(remove_tempfile
);
2015 sigchain_push_common(remove_tempfile_on_signal
);
2016 remove_tempfile_installed
= 1;
2019 if (!one
->sha1_valid
||
2020 reuse_worktree_file(name
, one
->sha1
, 1)) {
2022 if (lstat(name
, &st
) < 0) {
2023 if (errno
== ENOENT
)
2024 goto not_a_valid_file
;
2025 die_errno("stat(%s)", name
);
2027 if (S_ISLNK(st
.st_mode
)) {
2028 struct strbuf sb
= STRBUF_INIT
;
2029 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
2030 die_errno("readlink(%s)", name
);
2031 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
2033 one
->sha1
: null_sha1
),
2035 one
->mode
: S_IFLNK
));
2036 strbuf_release(&sb
);
2039 /* we can borrow from the file in the work tree */
2041 if (!one
->sha1_valid
)
2042 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
2044 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
2045 /* Even though we may sometimes borrow the
2046 * contents from the work tree, we always want
2047 * one->mode. mode is trustworthy even when
2048 * !(one->sha1_valid), as long as
2049 * DIFF_FILE_VALID(one).
2051 sprintf(temp
->mode
, "%06o", one
->mode
);
2056 if (diff_populate_filespec(one
, 0))
2057 die("cannot read data blob for %s", one
->path
);
2058 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
2059 one
->sha1
, one
->mode
);
2064 /* An external diff command takes:
2066 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2067 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2070 static void run_external_diff(const char *pgm
,
2073 struct diff_filespec
*one
,
2074 struct diff_filespec
*two
,
2075 const char *xfrm_msg
,
2076 int complete_rewrite
)
2078 const char *spawn_arg
[10];
2080 const char **arg
= &spawn_arg
[0];
2083 struct diff_tempfile
*temp_one
, *temp_two
;
2084 const char *othername
= (other
? other
: name
);
2085 temp_one
= prepare_temp_file(name
, one
);
2086 temp_two
= prepare_temp_file(othername
, two
);
2089 *arg
++ = temp_one
->name
;
2090 *arg
++ = temp_one
->hex
;
2091 *arg
++ = temp_one
->mode
;
2092 *arg
++ = temp_two
->name
;
2093 *arg
++ = temp_two
->hex
;
2094 *arg
++ = temp_two
->mode
;
2105 retval
= run_command_v_opt(spawn_arg
, 0);
2108 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2113 static int similarity_index(struct diff_filepair
*p
)
2115 return p
->score
* 100 / MAX_SCORE
;
2118 static void fill_metainfo(struct strbuf
*msg
,
2121 struct diff_filespec
*one
,
2122 struct diff_filespec
*two
,
2123 struct diff_options
*o
,
2124 struct diff_filepair
*p
)
2126 strbuf_init(msg
, PATH_MAX
* 2 + 300);
2127 switch (p
->status
) {
2128 case DIFF_STATUS_COPIED
:
2129 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2130 strbuf_addstr(msg
, "\ncopy from ");
2131 quote_c_style(name
, msg
, NULL
, 0);
2132 strbuf_addstr(msg
, "\ncopy to ");
2133 quote_c_style(other
, msg
, NULL
, 0);
2134 strbuf_addch(msg
, '\n');
2136 case DIFF_STATUS_RENAMED
:
2137 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2138 strbuf_addstr(msg
, "\nrename from ");
2139 quote_c_style(name
, msg
, NULL
, 0);
2140 strbuf_addstr(msg
, "\nrename to ");
2141 quote_c_style(other
, msg
, NULL
, 0);
2142 strbuf_addch(msg
, '\n');
2144 case DIFF_STATUS_MODIFIED
:
2146 strbuf_addf(msg
, "dissimilarity index %d%%\n",
2147 similarity_index(p
));
2155 if (one
&& two
&& hashcmp(one
->sha1
, two
->sha1
)) {
2156 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2158 if (DIFF_OPT_TST(o
, BINARY
)) {
2160 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2161 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2164 strbuf_addf(msg
, "index %.*s..%.*s",
2165 abbrev
, sha1_to_hex(one
->sha1
),
2166 abbrev
, sha1_to_hex(two
->sha1
));
2167 if (one
->mode
== two
->mode
)
2168 strbuf_addf(msg
, " %06o", one
->mode
);
2169 strbuf_addch(msg
, '\n');
2172 strbuf_setlen(msg
, msg
->len
- 1);
2175 static void run_diff_cmd(const char *pgm
,
2178 const char *attr_path
,
2179 struct diff_filespec
*one
,
2180 struct diff_filespec
*two
,
2182 struct diff_options
*o
,
2183 struct diff_filepair
*p
)
2185 const char *xfrm_msg
= NULL
;
2186 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
2189 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
);
2190 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
2193 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2196 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
2197 if (drv
&& drv
->external
)
2198 pgm
= drv
->external
;
2202 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2207 builtin_diff(name
, other
? other
: name
,
2208 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2210 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2213 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2215 if (DIFF_FILE_VALID(one
)) {
2216 if (!one
->sha1_valid
) {
2218 if (!strcmp(one
->path
, "-")) {
2219 hashcpy(one
->sha1
, null_sha1
);
2222 if (lstat(one
->path
, &st
) < 0)
2223 die_errno("stat '%s'", one
->path
);
2224 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2225 die("cannot hash %s", one
->path
);
2232 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2234 /* Strip the prefix but do not molest /dev/null and absolute paths */
2235 if (*namep
&& **namep
!= '/')
2236 *namep
+= prefix_length
;
2237 if (*otherp
&& **otherp
!= '/')
2238 *otherp
+= prefix_length
;
2241 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2243 const char *pgm
= external_diff();
2245 struct diff_filespec
*one
= p
->one
;
2246 struct diff_filespec
*two
= p
->two
;
2249 const char *attr_path
;
2251 name
= p
->one
->path
;
2252 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2254 if (o
->prefix_length
)
2255 strip_prefix(o
->prefix_length
, &name
, &other
);
2257 if (DIFF_PAIR_UNMERGED(p
)) {
2258 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2259 NULL
, NULL
, NULL
, o
, p
);
2263 diff_fill_sha1_info(one
);
2264 diff_fill_sha1_info(two
);
2267 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2268 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2270 * a filepair that changes between file and symlink
2271 * needs to be split into deletion and creation.
2273 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2274 run_diff_cmd(NULL
, name
, other
, attr_path
,
2275 one
, null
, &msg
, o
, p
);
2277 strbuf_release(&msg
);
2279 null
= alloc_filespec(one
->path
);
2280 run_diff_cmd(NULL
, name
, other
, attr_path
,
2281 null
, two
, &msg
, o
, p
);
2285 run_diff_cmd(pgm
, name
, other
, attr_path
,
2286 one
, two
, &msg
, o
, p
);
2288 strbuf_release(&msg
);
2291 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2292 struct diffstat_t
*diffstat
)
2296 int complete_rewrite
= 0;
2298 if (DIFF_PAIR_UNMERGED(p
)) {
2300 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2304 name
= p
->one
->path
;
2305 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2307 if (o
->prefix_length
)
2308 strip_prefix(o
->prefix_length
, &name
, &other
);
2310 diff_fill_sha1_info(p
->one
);
2311 diff_fill_sha1_info(p
->two
);
2313 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2314 complete_rewrite
= 1;
2315 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2318 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2322 const char *attr_path
;
2324 if (DIFF_PAIR_UNMERGED(p
)) {
2329 name
= p
->one
->path
;
2330 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2331 attr_path
= other
? other
: name
;
2333 if (o
->prefix_length
)
2334 strip_prefix(o
->prefix_length
, &name
, &other
);
2336 diff_fill_sha1_info(p
->one
);
2337 diff_fill_sha1_info(p
->two
);
2339 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2342 void diff_setup(struct diff_options
*options
)
2344 memset(options
, 0, sizeof(*options
));
2346 options
->file
= stdout
;
2348 options
->line_termination
= '\n';
2349 options
->break_opt
= -1;
2350 options
->rename_limit
= -1;
2351 options
->dirstat_percent
= 3;
2352 options
->context
= 3;
2354 options
->change
= diff_change
;
2355 options
->add_remove
= diff_addremove
;
2356 if (diff_use_color_default
> 0)
2357 DIFF_OPT_SET(options
, COLOR_DIFF
);
2358 options
->detect_rename
= diff_detect_rename_default
;
2360 if (!diff_mnemonic_prefix
) {
2361 options
->a_prefix
= "a/";
2362 options
->b_prefix
= "b/";
2366 int diff_setup_done(struct diff_options
*options
)
2370 if (options
->output_format
& DIFF_FORMAT_NAME
)
2372 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2374 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2376 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2379 die("--name-only, --name-status, --check and -s are mutually exclusive");
2381 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2382 options
->detect_rename
= DIFF_DETECT_COPY
;
2384 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2385 options
->prefix
= NULL
;
2386 if (options
->prefix
)
2387 options
->prefix_length
= strlen(options
->prefix
);
2389 options
->prefix_length
= 0;
2391 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2392 DIFF_FORMAT_NAME_STATUS
|
2393 DIFF_FORMAT_CHECKDIFF
|
2394 DIFF_FORMAT_NO_OUTPUT
))
2395 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2396 DIFF_FORMAT_NUMSTAT
|
2397 DIFF_FORMAT_DIFFSTAT
|
2398 DIFF_FORMAT_SHORTSTAT
|
2399 DIFF_FORMAT_DIRSTAT
|
2400 DIFF_FORMAT_SUMMARY
|
2404 * These cases always need recursive; we do not drop caller-supplied
2405 * recursive bits for other formats here.
2407 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2408 DIFF_FORMAT_NUMSTAT
|
2409 DIFF_FORMAT_DIFFSTAT
|
2410 DIFF_FORMAT_SHORTSTAT
|
2411 DIFF_FORMAT_DIRSTAT
|
2412 DIFF_FORMAT_SUMMARY
|
2413 DIFF_FORMAT_CHECKDIFF
))
2414 DIFF_OPT_SET(options
, RECURSIVE
);
2416 * Also pickaxe would not work very well if you do not say recursive
2418 if (options
->pickaxe
)
2419 DIFF_OPT_SET(options
, RECURSIVE
);
2421 if (options
->detect_rename
&& options
->rename_limit
< 0)
2422 options
->rename_limit
= diff_rename_limit_default
;
2423 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2425 /* read-cache does not die even when it fails
2426 * so it is safe for us to do this here. Also
2427 * it does not smudge active_cache or active_nr
2428 * when it fails, so we do not have to worry about
2429 * cleaning it up ourselves either.
2433 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2434 options
->abbrev
= 40; /* full */
2437 * It does not make sense to show the first hit we happened
2438 * to have found. It does not make sense not to return with
2439 * exit code in such a case either.
2441 if (DIFF_OPT_TST(options
, QUIET
)) {
2442 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2443 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2449 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2459 if (c
== arg_short
) {
2463 if (val
&& isdigit(c
)) {
2465 int n
= strtoul(arg
, &end
, 10);
2476 eq
= strchr(arg
, '=');
2481 if (!len
|| strncmp(arg
, arg_long
, len
))
2486 if (!isdigit(*++eq
))
2488 n
= strtoul(eq
, &end
, 10);
2496 static int diff_scoreopt_parse(const char *opt
);
2498 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2500 const char *arg
= av
[0];
2502 /* Output format options */
2503 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2504 options
->output_format
|= DIFF_FORMAT_PATCH
;
2505 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2506 options
->output_format
|= DIFF_FORMAT_PATCH
;
2507 else if (!strcmp(arg
, "--raw"))
2508 options
->output_format
|= DIFF_FORMAT_RAW
;
2509 else if (!strcmp(arg
, "--patch-with-raw"))
2510 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2511 else if (!strcmp(arg
, "--numstat"))
2512 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2513 else if (!strcmp(arg
, "--shortstat"))
2514 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2515 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2516 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2517 else if (!strcmp(arg
, "--cumulative")) {
2518 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2519 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
2520 } else if (opt_arg(arg
, 0, "dirstat-by-file",
2521 &options
->dirstat_percent
)) {
2522 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2523 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
2525 else if (!strcmp(arg
, "--check"))
2526 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2527 else if (!strcmp(arg
, "--summary"))
2528 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2529 else if (!strcmp(arg
, "--patch-with-stat"))
2530 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2531 else if (!strcmp(arg
, "--name-only"))
2532 options
->output_format
|= DIFF_FORMAT_NAME
;
2533 else if (!strcmp(arg
, "--name-status"))
2534 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2535 else if (!strcmp(arg
, "-s"))
2536 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2537 else if (!prefixcmp(arg
, "--stat")) {
2539 int width
= options
->stat_width
;
2540 int name_width
= options
->stat_name_width
;
2546 if (!prefixcmp(arg
, "-width="))
2547 width
= strtoul(arg
+ 7, &end
, 10);
2548 else if (!prefixcmp(arg
, "-name-width="))
2549 name_width
= strtoul(arg
+ 12, &end
, 10);
2552 width
= strtoul(arg
+1, &end
, 10);
2554 name_width
= strtoul(end
+1, &end
, 10);
2557 /* Important! This checks all the error cases! */
2560 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2561 options
->stat_name_width
= name_width
;
2562 options
->stat_width
= width
;
2565 /* renames options */
2566 else if (!prefixcmp(arg
, "-B")) {
2567 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2570 else if (!prefixcmp(arg
, "-M")) {
2571 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2573 options
->detect_rename
= DIFF_DETECT_RENAME
;
2575 else if (!prefixcmp(arg
, "-C")) {
2576 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2577 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2578 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2580 options
->detect_rename
= DIFF_DETECT_COPY
;
2582 else if (!strcmp(arg
, "--no-renames"))
2583 options
->detect_rename
= 0;
2584 else if (!strcmp(arg
, "--relative"))
2585 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2586 else if (!prefixcmp(arg
, "--relative=")) {
2587 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2588 options
->prefix
= arg
+ 11;
2592 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2593 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
2594 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2595 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
2596 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2597 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
2598 else if (!strcmp(arg
, "--patience"))
2599 DIFF_XDL_SET(options
, PATIENCE_DIFF
);
2602 else if (!strcmp(arg
, "--binary")) {
2603 options
->output_format
|= DIFF_FORMAT_PATCH
;
2604 DIFF_OPT_SET(options
, BINARY
);
2606 else if (!strcmp(arg
, "--full-index"))
2607 DIFF_OPT_SET(options
, FULL_INDEX
);
2608 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2609 DIFF_OPT_SET(options
, TEXT
);
2610 else if (!strcmp(arg
, "-R"))
2611 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2612 else if (!strcmp(arg
, "--find-copies-harder"))
2613 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2614 else if (!strcmp(arg
, "--follow"))
2615 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2616 else if (!strcmp(arg
, "--color"))
2617 DIFF_OPT_SET(options
, COLOR_DIFF
);
2618 else if (!strcmp(arg
, "--no-color"))
2619 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2620 else if (!strcmp(arg
, "--color-words")) {
2621 DIFF_OPT_SET(options
, COLOR_DIFF
);
2622 DIFF_OPT_SET(options
, COLOR_DIFF_WORDS
);
2624 else if (!prefixcmp(arg
, "--color-words=")) {
2625 DIFF_OPT_SET(options
, COLOR_DIFF
);
2626 DIFF_OPT_SET(options
, COLOR_DIFF_WORDS
);
2627 options
->word_regex
= arg
+ 14;
2629 else if (!strcmp(arg
, "--exit-code"))
2630 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2631 else if (!strcmp(arg
, "--quiet"))
2632 DIFF_OPT_SET(options
, QUIET
);
2633 else if (!strcmp(arg
, "--ext-diff"))
2634 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2635 else if (!strcmp(arg
, "--no-ext-diff"))
2636 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2637 else if (!strcmp(arg
, "--textconv"))
2638 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
2639 else if (!strcmp(arg
, "--no-textconv"))
2640 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
2641 else if (!strcmp(arg
, "--ignore-submodules"))
2642 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
2645 else if (!strcmp(arg
, "-z"))
2646 options
->line_termination
= 0;
2647 else if (!prefixcmp(arg
, "-l"))
2648 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2649 else if (!prefixcmp(arg
, "-S"))
2650 options
->pickaxe
= arg
+ 2;
2651 else if (!strcmp(arg
, "--pickaxe-all"))
2652 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2653 else if (!strcmp(arg
, "--pickaxe-regex"))
2654 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2655 else if (!prefixcmp(arg
, "-O"))
2656 options
->orderfile
= arg
+ 2;
2657 else if (!prefixcmp(arg
, "--diff-filter="))
2658 options
->filter
= arg
+ 14;
2659 else if (!strcmp(arg
, "--abbrev"))
2660 options
->abbrev
= DEFAULT_ABBREV
;
2661 else if (!prefixcmp(arg
, "--abbrev=")) {
2662 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2663 if (options
->abbrev
< MINIMUM_ABBREV
)
2664 options
->abbrev
= MINIMUM_ABBREV
;
2665 else if (40 < options
->abbrev
)
2666 options
->abbrev
= 40;
2668 else if (!prefixcmp(arg
, "--src-prefix="))
2669 options
->a_prefix
= arg
+ 13;
2670 else if (!prefixcmp(arg
, "--dst-prefix="))
2671 options
->b_prefix
= arg
+ 13;
2672 else if (!strcmp(arg
, "--no-prefix"))
2673 options
->a_prefix
= options
->b_prefix
= "";
2674 else if (opt_arg(arg
, '\0', "inter-hunk-context",
2675 &options
->interhunkcontext
))
2677 else if (!prefixcmp(arg
, "--output=")) {
2678 options
->file
= fopen(arg
+ strlen("--output="), "w");
2679 options
->close_file
= 1;
2685 static int parse_num(const char **cp_p
)
2687 unsigned long num
, scale
;
2689 const char *cp
= *cp_p
;
2696 if ( !dot
&& ch
== '.' ) {
2699 } else if ( ch
== '%' ) {
2700 scale
= dot
? scale
*100 : 100;
2701 cp
++; /* % is always at the end */
2703 } else if ( ch
>= '0' && ch
<= '9' ) {
2704 if ( scale
< 100000 ) {
2706 num
= (num
*10) + (ch
-'0');
2715 /* user says num divided by scale and we say internally that
2716 * is MAX_SCORE * num / scale.
2718 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2721 static int diff_scoreopt_parse(const char *opt
)
2723 int opt1
, opt2
, cmd
;
2728 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2729 return -1; /* that is not a -M, -C nor -B option */
2731 opt1
= parse_num(&opt
);
2737 else if (*opt
!= '/')
2738 return -1; /* we expect -B80/99 or -B80 */
2741 opt2
= parse_num(&opt
);
2746 return opt1
| (opt2
<< 16);
2749 struct diff_queue_struct diff_queued_diff
;
2751 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2753 if (queue
->alloc
<= queue
->nr
) {
2754 queue
->alloc
= alloc_nr(queue
->alloc
);
2755 queue
->queue
= xrealloc(queue
->queue
,
2756 sizeof(dp
) * queue
->alloc
);
2758 queue
->queue
[queue
->nr
++] = dp
;
2761 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2762 struct diff_filespec
*one
,
2763 struct diff_filespec
*two
)
2765 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2773 void diff_free_filepair(struct diff_filepair
*p
)
2775 free_filespec(p
->one
);
2776 free_filespec(p
->two
);
2780 /* This is different from find_unique_abbrev() in that
2781 * it stuffs the result with dots for alignment.
2783 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2788 return sha1_to_hex(sha1
);
2790 abbrev
= find_unique_abbrev(sha1
, len
);
2791 abblen
= strlen(abbrev
);
2793 static char hex
[41];
2794 if (len
< abblen
&& abblen
<= len
+ 2)
2795 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2797 sprintf(hex
, "%s...", abbrev
);
2800 return sha1_to_hex(sha1
);
2803 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
2805 int line_termination
= opt
->line_termination
;
2806 int inter_name_termination
= line_termination
? '\t' : '\0';
2808 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2809 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
2810 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
2811 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
2814 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
2815 inter_name_termination
);
2817 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
2820 if (p
->status
== DIFF_STATUS_COPIED
||
2821 p
->status
== DIFF_STATUS_RENAMED
) {
2822 const char *name_a
, *name_b
;
2823 name_a
= p
->one
->path
;
2824 name_b
= p
->two
->path
;
2825 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2826 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
2827 write_name_quoted(name_b
, opt
->file
, line_termination
);
2829 const char *name_a
, *name_b
;
2830 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
2832 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2833 write_name_quoted(name_a
, opt
->file
, line_termination
);
2837 int diff_unmodified_pair(struct diff_filepair
*p
)
2839 /* This function is written stricter than necessary to support
2840 * the currently implemented transformers, but the idea is to
2841 * let transformers to produce diff_filepairs any way they want,
2842 * and filter and clean them up here before producing the output.
2844 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
2846 if (DIFF_PAIR_UNMERGED(p
))
2847 return 0; /* unmerged is interesting */
2849 /* deletion, addition, mode or type change
2850 * and rename are all interesting.
2852 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2853 DIFF_PAIR_MODE_CHANGED(p
) ||
2854 strcmp(one
->path
, two
->path
))
2857 /* both are valid and point at the same path. that is, we are
2858 * dealing with a change.
2860 if (one
->sha1_valid
&& two
->sha1_valid
&&
2861 !hashcmp(one
->sha1
, two
->sha1
))
2862 return 1; /* no change */
2863 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2864 return 1; /* both look at the same file on the filesystem. */
2868 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2870 if (diff_unmodified_pair(p
))
2873 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2874 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2875 return; /* no tree diffs in patch format */
2880 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2881 struct diffstat_t
*diffstat
)
2883 if (diff_unmodified_pair(p
))
2886 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2887 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2888 return; /* no tree diffs in patch format */
2890 run_diffstat(p
, o
, diffstat
);
2893 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2894 struct diff_options
*o
)
2896 if (diff_unmodified_pair(p
))
2899 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2900 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2901 return; /* no tree diffs in patch format */
2903 run_checkdiff(p
, o
);
2906 int diff_queue_is_empty(void)
2908 struct diff_queue_struct
*q
= &diff_queued_diff
;
2910 for (i
= 0; i
< q
->nr
; i
++)
2911 if (!diff_unmodified_pair(q
->queue
[i
]))
2917 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2919 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2922 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2924 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2925 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2927 s
->size
, s
->xfrm_flags
);
2930 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2932 diff_debug_filespec(p
->one
, i
, "one");
2933 diff_debug_filespec(p
->two
, i
, "two");
2934 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
2935 p
->score
, p
->status
? p
->status
: '?',
2936 p
->one
->rename_used
, p
->broken_pair
);
2939 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2943 fprintf(stderr
, "%s\n", msg
);
2944 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2945 for (i
= 0; i
< q
->nr
; i
++) {
2946 struct diff_filepair
*p
= q
->queue
[i
];
2947 diff_debug_filepair(p
, i
);
2952 static void diff_resolve_rename_copy(void)
2955 struct diff_filepair
*p
;
2956 struct diff_queue_struct
*q
= &diff_queued_diff
;
2958 diff_debug_queue("resolve-rename-copy", q
);
2960 for (i
= 0; i
< q
->nr
; i
++) {
2962 p
->status
= 0; /* undecided */
2963 if (DIFF_PAIR_UNMERGED(p
))
2964 p
->status
= DIFF_STATUS_UNMERGED
;
2965 else if (!DIFF_FILE_VALID(p
->one
))
2966 p
->status
= DIFF_STATUS_ADDED
;
2967 else if (!DIFF_FILE_VALID(p
->two
))
2968 p
->status
= DIFF_STATUS_DELETED
;
2969 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2970 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2972 /* from this point on, we are dealing with a pair
2973 * whose both sides are valid and of the same type, i.e.
2974 * either in-place edit or rename/copy edit.
2976 else if (DIFF_PAIR_RENAME(p
)) {
2978 * A rename might have re-connected a broken
2979 * pair up, causing the pathnames to be the
2980 * same again. If so, that's not a rename at
2981 * all, just a modification..
2983 * Otherwise, see if this source was used for
2984 * multiple renames, in which case we decrement
2985 * the count, and call it a copy.
2987 if (!strcmp(p
->one
->path
, p
->two
->path
))
2988 p
->status
= DIFF_STATUS_MODIFIED
;
2989 else if (--p
->one
->rename_used
> 0)
2990 p
->status
= DIFF_STATUS_COPIED
;
2992 p
->status
= DIFF_STATUS_RENAMED
;
2994 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2995 p
->one
->mode
!= p
->two
->mode
||
2996 is_null_sha1(p
->one
->sha1
))
2997 p
->status
= DIFF_STATUS_MODIFIED
;
2999 /* This is a "no-change" entry and should not
3000 * happen anymore, but prepare for broken callers.
3002 error("feeding unmodified %s to diffcore",
3004 p
->status
= DIFF_STATUS_UNKNOWN
;
3007 diff_debug_queue("resolve-rename-copy done", q
);
3010 static int check_pair_status(struct diff_filepair
*p
)
3012 switch (p
->status
) {
3013 case DIFF_STATUS_UNKNOWN
:
3016 die("internal error in diff-resolve-rename-copy");
3022 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
3024 int fmt
= opt
->output_format
;
3026 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
3027 diff_flush_checkdiff(p
, opt
);
3028 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
3029 diff_flush_raw(p
, opt
);
3030 else if (fmt
& DIFF_FORMAT_NAME
) {
3031 const char *name_a
, *name_b
;
3032 name_a
= p
->two
->path
;
3034 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3035 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
3039 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
3042 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
3044 fprintf(file
, " %s ", newdelete
);
3045 write_name_quoted(fs
->path
, file
, '\n');
3049 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
3051 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
3052 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
3053 show_name
? ' ' : '\n');
3055 write_name_quoted(p
->two
->path
, file
, '\n');
3060 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
3062 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
3064 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
3066 show_mode_change(file
, p
, 0);
3069 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
3072 case DIFF_STATUS_DELETED
:
3073 show_file_mode_name(file
, "delete", p
->one
);
3075 case DIFF_STATUS_ADDED
:
3076 show_file_mode_name(file
, "create", p
->two
);
3078 case DIFF_STATUS_COPIED
:
3079 show_rename_copy(file
, "copy", p
);
3081 case DIFF_STATUS_RENAMED
:
3082 show_rename_copy(file
, "rename", p
);
3086 fputs(" rewrite ", file
);
3087 write_name_quoted(p
->two
->path
, file
, ' ');
3088 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3090 show_mode_change(file
, p
, !p
->score
);
3100 static int remove_space(char *line
, int len
)
3106 for (i
= 0; i
< len
; i
++)
3107 if (!isspace((c
= line
[i
])))
3113 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3115 struct patch_id_t
*data
= priv
;
3118 /* Ignore line numbers when computing the SHA1 of the patch */
3119 if (!prefixcmp(line
, "@@ -"))
3122 new_len
= remove_space(line
, len
);
3124 git_SHA1_Update(data
->ctx
, line
, new_len
);
3125 data
->patchlen
+= new_len
;
3128 /* returns 0 upon success, and writes result into sha1 */
3129 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3131 struct diff_queue_struct
*q
= &diff_queued_diff
;
3134 struct patch_id_t data
;
3135 char buffer
[PATH_MAX
* 4 + 20];
3137 git_SHA1_Init(&ctx
);
3138 memset(&data
, 0, sizeof(struct patch_id_t
));
3141 for (i
= 0; i
< q
->nr
; i
++) {
3146 struct diff_filepair
*p
= q
->queue
[i
];
3149 memset(&xpp
, 0, sizeof(xpp
));
3150 memset(&xecfg
, 0, sizeof(xecfg
));
3152 return error("internal diff status error");
3153 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3155 if (diff_unmodified_pair(p
))
3157 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3158 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3160 if (DIFF_PAIR_UNMERGED(p
))
3163 diff_fill_sha1_info(p
->one
);
3164 diff_fill_sha1_info(p
->two
);
3165 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3166 fill_mmfile(&mf2
, p
->two
) < 0)
3167 return error("unable to read files to diff");
3169 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3170 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3171 if (p
->one
->mode
== 0)
3172 len1
= snprintf(buffer
, sizeof(buffer
),
3173 "diff--gita/%.*sb/%.*s"
3180 len2
, p
->two
->path
);
3181 else if (p
->two
->mode
== 0)
3182 len1
= snprintf(buffer
, sizeof(buffer
),
3183 "diff--gita/%.*sb/%.*s"
3184 "deletedfilemode%06o"
3190 len1
, p
->one
->path
);
3192 len1
= snprintf(buffer
, sizeof(buffer
),
3193 "diff--gita/%.*sb/%.*s"
3199 len2
, p
->two
->path
);
3200 git_SHA1_Update(&ctx
, buffer
, len1
);
3202 xpp
.flags
= XDF_NEED_MINIMAL
;
3204 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3205 xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
3206 &xpp
, &xecfg
, &ecb
);
3209 git_SHA1_Final(sha1
, &ctx
);
3213 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3215 struct diff_queue_struct
*q
= &diff_queued_diff
;
3217 int result
= diff_get_patch_id(options
, sha1
);
3219 for (i
= 0; i
< q
->nr
; i
++)
3220 diff_free_filepair(q
->queue
[i
]);
3224 q
->nr
= q
->alloc
= 0;
3229 static int is_summary_empty(const struct diff_queue_struct
*q
)
3233 for (i
= 0; i
< q
->nr
; i
++) {
3234 const struct diff_filepair
*p
= q
->queue
[i
];
3236 switch (p
->status
) {
3237 case DIFF_STATUS_DELETED
:
3238 case DIFF_STATUS_ADDED
:
3239 case DIFF_STATUS_COPIED
:
3240 case DIFF_STATUS_RENAMED
:
3245 if (p
->one
->mode
&& p
->two
->mode
&&
3246 p
->one
->mode
!= p
->two
->mode
)
3254 void diff_flush(struct diff_options
*options
)
3256 struct diff_queue_struct
*q
= &diff_queued_diff
;
3257 int i
, output_format
= options
->output_format
;
3261 * Order: raw, stat, summary, patch
3262 * or: name/name-status/checkdiff (other bits clear)
3267 if (output_format
& (DIFF_FORMAT_RAW
|
3269 DIFF_FORMAT_NAME_STATUS
|
3270 DIFF_FORMAT_CHECKDIFF
)) {
3271 for (i
= 0; i
< q
->nr
; i
++) {
3272 struct diff_filepair
*p
= q
->queue
[i
];
3273 if (check_pair_status(p
))
3274 flush_one_pair(p
, options
);
3279 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3280 struct diffstat_t diffstat
;
3282 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3283 for (i
= 0; i
< q
->nr
; i
++) {
3284 struct diff_filepair
*p
= q
->queue
[i
];
3285 if (check_pair_status(p
))
3286 diff_flush_stat(p
, options
, &diffstat
);
3288 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3289 show_numstat(&diffstat
, options
);
3290 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3291 show_stats(&diffstat
, options
);
3292 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3293 show_shortstats(&diffstat
, options
);
3294 free_diffstat_info(&diffstat
);
3297 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3298 show_dirstat(options
);
3300 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3301 for (i
= 0; i
< q
->nr
; i
++)
3302 diff_summary(options
->file
, q
->queue
[i
]);
3306 if (output_format
& DIFF_FORMAT_PATCH
) {
3308 putc(options
->line_termination
, options
->file
);
3309 if (options
->stat_sep
) {
3310 /* attach patch instead of inline */
3311 fputs(options
->stat_sep
, options
->file
);
3315 for (i
= 0; i
< q
->nr
; i
++) {
3316 struct diff_filepair
*p
= q
->queue
[i
];
3317 if (check_pair_status(p
))
3318 diff_flush_patch(p
, options
);
3322 if (output_format
& DIFF_FORMAT_CALLBACK
)
3323 options
->format_callback(q
, options
, options
->format_callback_data
);
3325 for (i
= 0; i
< q
->nr
; i
++)
3326 diff_free_filepair(q
->queue
[i
]);
3330 q
->nr
= q
->alloc
= 0;
3331 if (options
->close_file
)
3332 fclose(options
->file
);
3335 static void diffcore_apply_filter(const char *filter
)
3338 struct diff_queue_struct
*q
= &diff_queued_diff
;
3339 struct diff_queue_struct outq
;
3341 outq
.nr
= outq
.alloc
= 0;
3346 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3348 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3349 struct diff_filepair
*p
= q
->queue
[i
];
3350 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3352 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3354 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3355 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3356 strchr(filter
, p
->status
)))
3362 /* otherwise we will clear the whole queue
3363 * by copying the empty outq at the end of this
3364 * function, but first clear the current entries
3367 for (i
= 0; i
< q
->nr
; i
++)
3368 diff_free_filepair(q
->queue
[i
]);
3371 /* Only the matching ones */
3372 for (i
= 0; i
< q
->nr
; i
++) {
3373 struct diff_filepair
*p
= q
->queue
[i
];
3375 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3377 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3379 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3380 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3381 strchr(filter
, p
->status
)))
3384 diff_free_filepair(p
);
3391 /* Check whether two filespecs with the same mode and size are identical */
3392 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3393 struct diff_filespec
*two
)
3395 if (S_ISGITLINK(one
->mode
))
3397 if (diff_populate_filespec(one
, 0))
3399 if (diff_populate_filespec(two
, 0))
3401 return !memcmp(one
->data
, two
->data
, one
->size
);
3404 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3407 struct diff_queue_struct
*q
= &diff_queued_diff
;
3408 struct diff_queue_struct outq
;
3410 outq
.nr
= outq
.alloc
= 0;
3412 for (i
= 0; i
< q
->nr
; i
++) {
3413 struct diff_filepair
*p
= q
->queue
[i
];
3416 * 1. Entries that come from stat info dirtyness
3417 * always have both sides (iow, not create/delete),
3418 * one side of the object name is unknown, with
3419 * the same mode and size. Keep the ones that
3420 * do not match these criteria. They have real
3423 * 2. At this point, the file is known to be modified,
3424 * with the same mode and size, and the object
3425 * name of one side is unknown. Need to inspect
3426 * the identical contents.
3428 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3429 !DIFF_FILE_VALID(p
->two
) ||
3430 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3431 (p
->one
->mode
!= p
->two
->mode
) ||
3432 diff_populate_filespec(p
->one
, 1) ||
3433 diff_populate_filespec(p
->two
, 1) ||
3434 (p
->one
->size
!= p
->two
->size
) ||
3435 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3439 * The caller can subtract 1 from skip_stat_unmatch
3440 * to determine how many paths were dirty only
3441 * due to stat info mismatch.
3443 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3444 diffopt
->skip_stat_unmatch
++;
3445 diff_free_filepair(p
);
3452 void diffcore_std(struct diff_options
*options
)
3454 if (options
->skip_stat_unmatch
)
3455 diffcore_skip_stat_unmatch(options
);
3456 if (options
->break_opt
!= -1)
3457 diffcore_break(options
->break_opt
);
3458 if (options
->detect_rename
)
3459 diffcore_rename(options
);
3460 if (options
->break_opt
!= -1)
3461 diffcore_merge_broken();
3462 if (options
->pickaxe
)
3463 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3464 if (options
->orderfile
)
3465 diffcore_order(options
->orderfile
);
3466 diff_resolve_rename_copy();
3467 diffcore_apply_filter(options
->filter
);
3469 if (diff_queued_diff
.nr
)
3470 DIFF_OPT_SET(options
, HAS_CHANGES
);
3472 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3475 int diff_result_code(struct diff_options
*opt
, int status
)
3478 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3479 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3481 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3482 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3484 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3485 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3490 void diff_addremove(struct diff_options
*options
,
3491 int addremove
, unsigned mode
,
3492 const unsigned char *sha1
,
3493 const char *concatpath
)
3495 struct diff_filespec
*one
, *two
;
3497 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
3500 /* This may look odd, but it is a preparation for
3501 * feeding "there are unchanged files which should
3502 * not produce diffs, but when you are doing copy
3503 * detection you would need them, so here they are"
3504 * entries to the diff-core. They will be prefixed
3505 * with something like '=' or '*' (I haven't decided
3506 * which but should not make any difference).
3507 * Feeding the same new and old to diff_change()
3508 * also has the same effect.
3509 * Before the final output happens, they are pruned after
3510 * merged into rename/copy pairs as appropriate.
3512 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3513 addremove
= (addremove
== '+' ? '-' :
3514 addremove
== '-' ? '+' : addremove
);
3516 if (options
->prefix
&&
3517 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3520 one
= alloc_filespec(concatpath
);
3521 two
= alloc_filespec(concatpath
);
3523 if (addremove
!= '+')
3524 fill_filespec(one
, sha1
, mode
);
3525 if (addremove
!= '-')
3526 fill_filespec(two
, sha1
, mode
);
3528 diff_queue(&diff_queued_diff
, one
, two
);
3529 DIFF_OPT_SET(options
, HAS_CHANGES
);
3532 void diff_change(struct diff_options
*options
,
3533 unsigned old_mode
, unsigned new_mode
,
3534 const unsigned char *old_sha1
,
3535 const unsigned char *new_sha1
,
3536 const char *concatpath
)
3538 struct diff_filespec
*one
, *two
;
3540 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
3541 && S_ISGITLINK(new_mode
))
3544 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3546 const unsigned char *tmp_c
;
3547 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3548 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3551 if (options
->prefix
&&
3552 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3555 one
= alloc_filespec(concatpath
);
3556 two
= alloc_filespec(concatpath
);
3557 fill_filespec(one
, old_sha1
, old_mode
);
3558 fill_filespec(two
, new_sha1
, new_mode
);
3560 diff_queue(&diff_queued_diff
, one
, two
);
3561 DIFF_OPT_SET(options
, HAS_CHANGES
);
3564 void diff_unmerge(struct diff_options
*options
,
3566 unsigned mode
, const unsigned char *sha1
)
3568 struct diff_filespec
*one
, *two
;
3570 if (options
->prefix
&&
3571 strncmp(path
, options
->prefix
, options
->prefix_length
))
3574 one
= alloc_filespec(path
);
3575 two
= alloc_filespec(path
);
3576 fill_filespec(one
, sha1
, mode
);
3577 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;
3580 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
3583 struct diff_tempfile
*temp
;
3584 const char *argv
[3];
3585 const char **arg
= argv
;
3586 struct child_process child
;
3587 struct strbuf buf
= STRBUF_INIT
;
3589 temp
= prepare_temp_file(spec
->path
, spec
);
3591 *arg
++ = temp
->name
;
3594 memset(&child
, 0, sizeof(child
));
3597 if (start_command(&child
) != 0 ||
3598 strbuf_read(&buf
, child
.out
, 0) < 0 ||
3599 finish_command(&child
) != 0) {
3600 strbuf_release(&buf
);
3602 error("error running textconv command '%s'", pgm
);
3607 return strbuf_detach(&buf
, outsize
);