2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "run-command.h"
15 #ifdef NO_FAST_WORKING_DIRECTORY
16 #define FAST_WORKING_DIRECTORY 0
18 #define FAST_WORKING_DIRECTORY 1
21 static int diff_detect_rename_default
;
22 static int diff_rename_limit_default
= 200;
23 int diff_use_color_default
= -1;
24 static const char *external_diff_cmd_cfg
;
25 int diff_auto_refresh_index
= 1;
27 static char diff_colors
[][COLOR_MAXLEN
] = {
29 "", /* PLAIN (normal) */
30 "\033[1m", /* METAINFO (bold) */
31 "\033[36m", /* FRAGINFO (cyan) */
32 "\033[31m", /* OLD (red) */
33 "\033[32m", /* NEW (green) */
34 "\033[33m", /* COMMIT (yellow) */
35 "\033[41m", /* WHITESPACE (red background) */
38 static int parse_diff_color_slot(const char *var
, int ofs
)
40 if (!strcasecmp(var
+ofs
, "plain"))
42 if (!strcasecmp(var
+ofs
, "meta"))
44 if (!strcasecmp(var
+ofs
, "frag"))
46 if (!strcasecmp(var
+ofs
, "old"))
48 if (!strcasecmp(var
+ofs
, "new"))
50 if (!strcasecmp(var
+ofs
, "commit"))
52 if (!strcasecmp(var
+ofs
, "whitespace"))
53 return DIFF_WHITESPACE
;
54 die("bad config variable '%s'", var
);
57 static struct ll_diff_driver
{
59 struct ll_diff_driver
*next
;
61 } *user_diff
, **user_diff_tail
;
64 * Currently there is only "diff.<drivername>.command" variable;
65 * because there are "diff.color.<slot>" variables, we are parsing
66 * this in a bit convoluted way to allow low level diff driver
69 static int parse_lldiff_command(const char *var
, const char *ep
, const char *value
)
73 struct ll_diff_driver
*drv
;
77 for (drv
= user_diff
; drv
; drv
= drv
->next
)
78 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
81 drv
= xcalloc(1, sizeof(struct ll_diff_driver
));
82 drv
->name
= xmemdupz(name
, namelen
);
84 user_diff_tail
= &user_diff
;
85 *user_diff_tail
= drv
;
86 user_diff_tail
= &(drv
->next
);
89 return git_config_string(&(drv
->cmd
), var
, value
);
93 * 'diff.<what>.funcname' attribute can be specified in the configuration
94 * to define a customized regexp to find the beginning of a function to
95 * be used for hunk header lines of "diff -p" style output.
97 struct funcname_pattern_entry
{
102 static struct funcname_pattern_list
{
103 struct funcname_pattern_list
*next
;
104 struct funcname_pattern_entry e
;
105 } *funcname_pattern_list
;
107 static int parse_funcname_pattern(const char *var
, const char *ep
, const char *value
, int cflags
)
111 struct funcname_pattern_list
*pp
;
113 name
= var
+ 5; /* "diff." */
116 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
117 if (!strncmp(pp
->e
.name
, name
, namelen
) && !pp
->e
.name
[namelen
])
120 pp
= xcalloc(1, sizeof(*pp
));
121 pp
->e
.name
= xmemdupz(name
, namelen
);
122 pp
->next
= funcname_pattern_list
;
123 funcname_pattern_list
= pp
;
126 pp
->e
.pattern
= xstrdup(value
);
127 pp
->e
.cflags
= cflags
;
132 * These are to give UI layer defaults.
133 * The core-level commands such as git-diff-files should
134 * never be affected by the setting of diff.renames
135 * the user happens to have in the configuration file.
137 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
139 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
140 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
143 if (!strcmp(var
, "diff.renames")) {
145 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
146 else if (!strcasecmp(value
, "copies") ||
147 !strcasecmp(value
, "copy"))
148 diff_detect_rename_default
= DIFF_DETECT_COPY
;
149 else if (git_config_bool(var
,value
))
150 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
153 if (!strcmp(var
, "diff.autorefreshindex")) {
154 diff_auto_refresh_index
= git_config_bool(var
, value
);
157 if (!strcmp(var
, "diff.external"))
158 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
159 if (!prefixcmp(var
, "diff.")) {
160 const char *ep
= strrchr(var
, '.');
162 if (ep
!= var
+ 4 && !strcmp(ep
, ".command"))
163 return parse_lldiff_command(var
, ep
, value
);
166 return git_diff_basic_config(var
, value
, cb
);
169 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
171 if (!strcmp(var
, "diff.renamelimit")) {
172 diff_rename_limit_default
= git_config_int(var
, value
);
176 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
177 int slot
= parse_diff_color_slot(var
, 11);
179 return config_error_nonbool(var
);
180 color_parse(value
, var
, diff_colors
[slot
]);
184 if (!prefixcmp(var
, "diff.")) {
185 const char *ep
= strrchr(var
, '.');
187 if (!strcmp(ep
, ".funcname")) {
189 return config_error_nonbool(var
);
190 return parse_funcname_pattern(var
, ep
, value
,
192 } else if (!strcmp(ep
, ".xfuncname")) {
194 return config_error_nonbool(var
);
195 return parse_funcname_pattern(var
, ep
, value
,
201 return git_color_default_config(var
, value
, cb
);
204 static char *quote_two(const char *one
, const char *two
)
206 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
207 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
210 strbuf_init(&res
, 0);
211 if (need_one
+ need_two
) {
212 strbuf_addch(&res
, '"');
213 quote_c_style(one
, &res
, NULL
, 1);
214 quote_c_style(two
, &res
, NULL
, 1);
215 strbuf_addch(&res
, '"');
217 strbuf_addstr(&res
, one
);
218 strbuf_addstr(&res
, two
);
220 return strbuf_detach(&res
, NULL
);
223 static const char *external_diff(void)
225 static const char *external_diff_cmd
= NULL
;
226 static int done_preparing
= 0;
229 return external_diff_cmd
;
230 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
231 if (!external_diff_cmd
)
232 external_diff_cmd
= external_diff_cmd_cfg
;
234 return external_diff_cmd
;
237 static struct diff_tempfile
{
238 const char *name
; /* filename external diff should read from */
241 char tmp_path
[PATH_MAX
];
244 static int count_lines(const char *data
, int size
)
246 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
253 completely_empty
= 0;
257 completely_empty
= 0;
260 if (completely_empty
)
263 count
++; /* no trailing newline */
267 static void print_line_count(FILE *file
, int count
)
271 fprintf(file
, "0,0");
277 fprintf(file
, "1,%d", count
);
282 static void copy_file_with_prefix(FILE *file
,
283 int prefix
, const char *data
, int size
,
284 const char *set
, const char *reset
)
286 int ch
, nl_just_seen
= 1;
301 fprintf(file
, "%s\n\\ No newline at end of file\n", reset
);
304 static void emit_rewrite_diff(const char *name_a
,
306 struct diff_filespec
*one
,
307 struct diff_filespec
*two
,
308 struct diff_options
*o
)
311 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
312 const char *name_a_tab
, *name_b_tab
;
313 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
314 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
315 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
316 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
317 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
318 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
320 name_a
+= (*name_a
== '/');
321 name_b
+= (*name_b
== '/');
322 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
323 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
325 strbuf_reset(&a_name
);
326 strbuf_reset(&b_name
);
327 quote_two_c_style(&a_name
, o
->a_prefix
, name_a
, 0);
328 quote_two_c_style(&b_name
, o
->b_prefix
, name_b
, 0);
330 diff_populate_filespec(one
, 0);
331 diff_populate_filespec(two
, 0);
332 lc_a
= count_lines(one
->data
, one
->size
);
333 lc_b
= count_lines(two
->data
, two
->size
);
335 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
336 metainfo
, a_name
.buf
, name_a_tab
, reset
,
337 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
338 print_line_count(o
->file
, lc_a
);
339 fprintf(o
->file
, " +");
340 print_line_count(o
->file
, lc_b
);
341 fprintf(o
->file
, " @@%s\n", reset
);
343 copy_file_with_prefix(o
->file
, '-', one
->data
, one
->size
, old
, reset
);
345 copy_file_with_prefix(o
->file
, '+', two
->data
, two
->size
, new, reset
);
348 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
350 if (!DIFF_FILE_VALID(one
)) {
351 mf
->ptr
= (char *)""; /* does not matter */
355 else if (diff_populate_filespec(one
, 0))
358 mf
->size
= one
->size
;
362 struct diff_words_buffer
{
365 long current
; /* output pointer */
366 int suppressed_newline
;
369 static void diff_words_append(char *line
, unsigned long len
,
370 struct diff_words_buffer
*buffer
)
372 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
373 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
374 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
378 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
379 buffer
->text
.size
+= len
;
382 struct diff_words_data
{
383 struct xdiff_emit_state xm
;
384 struct diff_words_buffer minus
, plus
;
388 static void print_word(FILE *file
, struct diff_words_buffer
*buffer
, int len
, int color
,
389 int suppress_newline
)
397 ptr
= buffer
->text
.ptr
+ buffer
->current
;
398 buffer
->current
+= len
;
400 if (ptr
[len
- 1] == '\n') {
405 fputs(diff_get_color(1, color
), file
);
406 fwrite(ptr
, len
, 1, file
);
407 fputs(diff_get_color(1, DIFF_RESET
), file
);
410 if (suppress_newline
)
411 buffer
->suppressed_newline
= 1;
417 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
419 struct diff_words_data
*diff_words
= priv
;
421 if (diff_words
->minus
.suppressed_newline
) {
423 putc('\n', diff_words
->file
);
424 diff_words
->minus
.suppressed_newline
= 0;
430 print_word(diff_words
->file
,
431 &diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
434 print_word(diff_words
->file
,
435 &diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
438 print_word(diff_words
->file
,
439 &diff_words
->plus
, len
, DIFF_PLAIN
, 0);
440 diff_words
->minus
.current
+= len
;
445 /* this executes the word diff on the accumulated buffers */
446 static void diff_words_show(struct diff_words_data
*diff_words
)
451 mmfile_t minus
, plus
;
454 memset(&xecfg
, 0, sizeof(xecfg
));
455 minus
.size
= diff_words
->minus
.text
.size
;
456 minus
.ptr
= xmalloc(minus
.size
);
457 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
458 for (i
= 0; i
< minus
.size
; i
++)
459 if (isspace(minus
.ptr
[i
]))
461 diff_words
->minus
.current
= 0;
463 plus
.size
= diff_words
->plus
.text
.size
;
464 plus
.ptr
= xmalloc(plus
.size
);
465 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
466 for (i
= 0; i
< plus
.size
; i
++)
467 if (isspace(plus
.ptr
[i
]))
469 diff_words
->plus
.current
= 0;
471 xpp
.flags
= XDF_NEED_MINIMAL
;
472 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
473 ecb
.outf
= xdiff_outf
;
474 ecb
.priv
= diff_words
;
475 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
476 xdi_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
480 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
482 if (diff_words
->minus
.suppressed_newline
) {
483 putc('\n', diff_words
->file
);
484 diff_words
->minus
.suppressed_newline
= 0;
488 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
490 struct emit_callback
{
491 struct xdiff_emit_state xm
;
496 sane_truncate_fn truncate
;
497 const char **label_path
;
498 struct diff_words_data
*diff_words
;
503 static void free_diff_words_data(struct emit_callback
*ecbdata
)
505 if (ecbdata
->diff_words
) {
507 if (ecbdata
->diff_words
->minus
.text
.size
||
508 ecbdata
->diff_words
->plus
.text
.size
)
509 diff_words_show(ecbdata
->diff_words
);
511 free (ecbdata
->diff_words
->minus
.text
.ptr
);
512 free (ecbdata
->diff_words
->plus
.text
.ptr
);
513 free(ecbdata
->diff_words
);
514 ecbdata
->diff_words
= NULL
;
518 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
521 return diff_colors
[ix
];
525 static void emit_line(FILE *file
, const char *set
, const char *reset
, const char *line
, int len
)
527 int has_trailing_newline
, has_trailing_carriage_return
;
529 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
530 if (has_trailing_newline
)
532 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
533 if (has_trailing_carriage_return
)
537 fwrite(line
, len
, 1, file
);
539 if (has_trailing_carriage_return
)
541 if (has_trailing_newline
)
545 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
547 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
548 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
551 emit_line(ecbdata
->file
, set
, reset
, line
, len
);
552 else if ((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
553 ecbdata
->blank_at_eof
&&
554 (ecbdata
->blank_at_eof
<= ecbdata
->lno_in_preimage
) &&
555 ws_blank_line(line
+ 1, len
- 1, ecbdata
->ws_rule
))
556 /* Blank line at EOF */
557 emit_line(ecbdata
->file
, ws
, reset
, line
, len
);
559 /* Emit just the prefix, then the rest. */
560 emit_line(ecbdata
->file
, set
, reset
, line
, 1);
561 ws_check_emit(line
+ 1, len
- 1, ecbdata
->ws_rule
,
562 ecbdata
->file
, set
, reset
, ws
);
566 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
573 return ecb
->truncate(line
, len
);
577 (void) utf8_width(&cp
, &l
);
579 break; /* truncated in the middle? */
584 static int find_preimage_lno(const char *line
)
586 char *p
= strchr(line
, '-');
588 return 0; /* should not happen */
589 return strtol(p
+1, NULL
, 10);
592 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
594 struct emit_callback
*ecbdata
= priv
;
595 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
596 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
597 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
599 *(ecbdata
->found_changesp
) = 1;
601 if (ecbdata
->label_path
[0]) {
602 const char *name_a_tab
, *name_b_tab
;
604 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
605 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
607 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
608 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
609 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
610 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
611 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
614 if (line
[0] == '@') {
615 len
= sane_truncate_line(ecbdata
, line
, len
);
616 ecbdata
->lno_in_preimage
= find_preimage_lno(line
);
617 emit_line(ecbdata
->file
,
618 diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
620 if (line
[len
-1] != '\n')
621 putc('\n', ecbdata
->file
);
626 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
630 if (ecbdata
->diff_words
) {
631 if (line
[0] == '-') {
632 diff_words_append(line
, len
,
633 &ecbdata
->diff_words
->minus
);
635 } else if (line
[0] == '+') {
636 diff_words_append(line
, len
,
637 &ecbdata
->diff_words
->plus
);
640 if (ecbdata
->diff_words
->minus
.text
.size
||
641 ecbdata
->diff_words
->plus
.text
.size
)
642 diff_words_show(ecbdata
->diff_words
);
645 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
649 if (line
[0] != '+') {
651 diff_get_color(ecbdata
->color_diff
,
652 line
[0] == '-' ? DIFF_FILE_OLD
: DIFF_PLAIN
);
653 ecbdata
->lno_in_preimage
++;
654 emit_line(ecbdata
->file
, color
, reset
, line
, len
);
657 emit_add_line(reset
, ecbdata
, line
, len
);
660 static char *pprint_rename(const char *a
, const char *b
)
665 int pfx_length
, sfx_length
;
666 int len_a
= strlen(a
);
667 int len_b
= strlen(b
);
668 int a_midlen
, b_midlen
;
669 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
670 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
672 strbuf_init(&name
, 0);
673 if (qlen_a
|| qlen_b
) {
674 quote_c_style(a
, &name
, NULL
, 0);
675 strbuf_addstr(&name
, " => ");
676 quote_c_style(b
, &name
, NULL
, 0);
677 return strbuf_detach(&name
, NULL
);
680 /* Find common prefix */
682 while (*old
&& *new && *old
== *new) {
684 pfx_length
= old
- a
+ 1;
689 /* Find common suffix */
693 while (a
<= old
&& b
<= new && *old
== *new) {
695 sfx_length
= len_a
- (old
- a
);
701 * pfx{mid-a => mid-b}sfx
702 * {pfx-a => pfx-b}sfx
703 * pfx{sfx-a => sfx-b}
706 a_midlen
= len_a
- pfx_length
- sfx_length
;
707 b_midlen
= len_b
- pfx_length
- sfx_length
;
713 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
714 if (pfx_length
+ sfx_length
) {
715 strbuf_add(&name
, a
, pfx_length
);
716 strbuf_addch(&name
, '{');
718 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
719 strbuf_addstr(&name
, " => ");
720 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
721 if (pfx_length
+ sfx_length
) {
722 strbuf_addch(&name
, '}');
723 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
725 return strbuf_detach(&name
, NULL
);
729 struct xdiff_emit_state xm
;
733 struct diffstat_file
{
737 unsigned is_unmerged
:1;
738 unsigned is_binary
:1;
739 unsigned is_renamed
:1;
740 unsigned int added
, deleted
;
744 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
748 struct diffstat_file
*x
;
749 x
= xcalloc(sizeof (*x
), 1);
750 if (diffstat
->nr
== diffstat
->alloc
) {
751 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
752 diffstat
->files
= xrealloc(diffstat
->files
,
753 diffstat
->alloc
* sizeof(x
));
755 diffstat
->files
[diffstat
->nr
++] = x
;
757 x
->from_name
= xstrdup(name_a
);
758 x
->name
= xstrdup(name_b
);
763 x
->name
= xstrdup(name_a
);
768 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
770 struct diffstat_t
*diffstat
= priv
;
771 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
775 else if (line
[0] == '-')
779 const char mime_boundary_leader
[] = "------------";
781 static int scale_linear(int it
, int width
, int max_change
)
784 * make sure that at least one '-' is printed if there were deletions,
785 * and likewise for '+'.
789 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
792 static void show_name(FILE *file
,
793 const char *prefix
, const char *name
, int len
,
794 const char *reset
, const char *set
)
796 fprintf(file
, " %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
799 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
803 fprintf(file
, "%s", set
);
806 fprintf(file
, "%s", reset
);
809 static void fill_print_name(struct diffstat_file
*file
)
813 if (file
->print_name
)
816 if (!file
->is_renamed
) {
818 strbuf_init(&buf
, 0);
819 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
820 pname
= strbuf_detach(&buf
, NULL
);
823 strbuf_release(&buf
);
826 pname
= pprint_rename(file
->from_name
, file
->name
);
828 file
->print_name
= pname
;
831 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
833 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
834 int max_change
= 0, max_len
= 0;
835 int total_files
= data
->nr
;
836 int width
, name_width
;
837 const char *reset
, *set
, *add_c
, *del_c
;
842 width
= options
->stat_width
? options
->stat_width
: 80;
843 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
845 /* Sanity: give at least 5 columns to the graph,
846 * but leave at least 10 columns for the name.
852 else if (width
< name_width
+ 15)
853 name_width
= width
- 15;
855 /* Find the longest filename and max number of changes */
856 reset
= diff_get_color_opt(options
, DIFF_RESET
);
857 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
858 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
859 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
861 for (i
= 0; i
< data
->nr
; i
++) {
862 struct diffstat_file
*file
= data
->files
[i
];
863 int change
= file
->added
+ file
->deleted
;
864 fill_print_name(file
);
865 len
= strlen(file
->print_name
);
869 if (file
->is_binary
|| file
->is_unmerged
)
871 if (max_change
< change
)
875 /* Compute the width of the graph part;
876 * 10 is for one blank at the beginning of the line plus
877 * " | count " between the name and the graph.
879 * From here on, name_width is the width of the name area,
880 * and width is the width of the graph area.
882 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
883 if (width
< (name_width
+ 10) + max_change
)
884 width
= width
- (name_width
+ 10);
888 for (i
= 0; i
< data
->nr
; i
++) {
889 const char *prefix
= "";
890 char *name
= data
->files
[i
]->print_name
;
891 int added
= data
->files
[i
]->added
;
892 int deleted
= data
->files
[i
]->deleted
;
896 * "scale" the filename
899 name_len
= strlen(name
);
900 if (name_width
< name_len
) {
904 name
+= name_len
- len
;
905 slash
= strchr(name
, '/');
910 if (data
->files
[i
]->is_binary
) {
911 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
912 fprintf(options
->file
, " Bin ");
913 fprintf(options
->file
, "%s%d%s", del_c
, deleted
, reset
);
914 fprintf(options
->file
, " -> ");
915 fprintf(options
->file
, "%s%d%s", add_c
, added
, reset
);
916 fprintf(options
->file
, " bytes");
917 fprintf(options
->file
, "\n");
920 else if (data
->files
[i
]->is_unmerged
) {
921 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
922 fprintf(options
->file
, " Unmerged\n");
925 else if (!data
->files
[i
]->is_renamed
&&
926 (added
+ deleted
== 0)) {
932 * scale the add/delete
940 if (width
<= max_change
) {
941 add
= scale_linear(add
, width
, max_change
);
942 del
= scale_linear(del
, width
, max_change
);
945 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
946 fprintf(options
->file
, "%5d%s", added
+ deleted
,
947 added
+ deleted
? " " : "");
948 show_graph(options
->file
, '+', add
, add_c
, reset
);
949 show_graph(options
->file
, '-', del
, del_c
, reset
);
950 fprintf(options
->file
, "\n");
952 fprintf(options
->file
,
953 "%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
954 set
, total_files
, adds
, dels
, reset
);
957 static void show_shortstats(struct diffstat_t
* data
, struct diff_options
*options
)
959 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
964 for (i
= 0; i
< data
->nr
; i
++) {
965 if (!data
->files
[i
]->is_binary
&&
966 !data
->files
[i
]->is_unmerged
) {
967 int added
= data
->files
[i
]->added
;
968 int deleted
= data
->files
[i
]->deleted
;
969 if (!data
->files
[i
]->is_renamed
&&
970 (added
+ deleted
== 0)) {
978 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
979 total_files
, adds
, dels
);
982 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
989 for (i
= 0; i
< data
->nr
; i
++) {
990 struct diffstat_file
*file
= data
->files
[i
];
993 fprintf(options
->file
, "-\t-\t");
995 fprintf(options
->file
,
996 "%d\t%d\t", file
->added
, file
->deleted
);
997 if (options
->line_termination
) {
998 fill_print_name(file
);
999 if (!file
->is_renamed
)
1000 write_name_quoted(file
->name
, options
->file
,
1001 options
->line_termination
);
1003 fputs(file
->print_name
, options
->file
);
1004 putc(options
->line_termination
, options
->file
);
1007 if (file
->is_renamed
) {
1008 putc('\0', options
->file
);
1009 write_name_quoted(file
->from_name
, options
->file
, '\0');
1011 write_name_quoted(file
->name
, options
->file
, '\0');
1016 struct dirstat_file
{
1018 unsigned long changed
;
1021 struct dirstat_dir
{
1022 struct dirstat_file
*files
;
1023 int alloc
, nr
, percent
, cumulative
;
1026 static long gather_dirstat(FILE *file
, struct dirstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1028 unsigned long this_dir
= 0;
1029 unsigned int sources
= 0;
1032 struct dirstat_file
*f
= dir
->files
;
1033 int namelen
= strlen(f
->name
);
1037 if (namelen
< baselen
)
1039 if (memcmp(f
->name
, base
, baselen
))
1041 slash
= strchr(f
->name
+ baselen
, '/');
1043 int newbaselen
= slash
+ 1 - f
->name
;
1044 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1056 * We don't report dirstat's for
1058 * - or cases where everything came from a single directory
1059 * under this directory (sources == 1).
1061 if (baselen
&& sources
!= 1) {
1062 int permille
= this_dir
* 1000 / changed
;
1064 int percent
= permille
/ 10;
1065 if (percent
>= dir
->percent
) {
1066 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1067 if (!dir
->cumulative
)
1075 static int dirstat_compare(const void *_a
, const void *_b
)
1077 const struct dirstat_file
*a
= _a
;
1078 const struct dirstat_file
*b
= _b
;
1079 return strcmp(a
->name
, b
->name
);
1082 static void show_dirstat(struct diff_options
*options
)
1085 unsigned long changed
;
1086 struct dirstat_dir dir
;
1087 struct diff_queue_struct
*q
= &diff_queued_diff
;
1092 dir
.percent
= options
->dirstat_percent
;
1093 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1096 for (i
= 0; i
< q
->nr
; i
++) {
1097 struct diff_filepair
*p
= q
->queue
[i
];
1099 unsigned long copied
, added
, damage
;
1101 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1103 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1104 diff_populate_filespec(p
->one
, 0);
1105 diff_populate_filespec(p
->two
, 0);
1106 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1108 diff_free_filespec_data(p
->one
);
1109 diff_free_filespec_data(p
->two
);
1110 } else if (DIFF_FILE_VALID(p
->one
)) {
1111 diff_populate_filespec(p
->one
, 1);
1113 diff_free_filespec_data(p
->one
);
1114 } else if (DIFF_FILE_VALID(p
->two
)) {
1115 diff_populate_filespec(p
->two
, 1);
1117 added
= p
->two
->size
;
1118 diff_free_filespec_data(p
->two
);
1123 * Original minus copied is the removed material,
1124 * added is the new material. They are both damages
1125 * made to the preimage.
1127 damage
= (p
->one
->size
- copied
) + added
;
1129 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1130 dir
.files
[dir
.nr
].name
= name
;
1131 dir
.files
[dir
.nr
].changed
= damage
;
1136 /* This can happen even with many files, if everything was renames */
1140 /* Show all directories with more than x% of the changes */
1141 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
1142 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1145 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1148 for (i
= 0; i
< diffstat
->nr
; i
++) {
1149 struct diffstat_file
*f
= diffstat
->files
[i
];
1150 if (f
->name
!= f
->print_name
)
1151 free(f
->print_name
);
1156 free(diffstat
->files
);
1159 struct checkdiff_t
{
1160 struct xdiff_emit_state xm
;
1161 const char *filename
;
1163 struct diff_options
*o
;
1168 static int is_conflict_marker(const char *line
, unsigned long len
)
1175 firstchar
= line
[0];
1176 switch (firstchar
) {
1177 case '=': case '>': case '<':
1182 for (cnt
= 1; cnt
< 7; cnt
++)
1183 if (line
[cnt
] != firstchar
)
1185 /* line[0] thru line[6] are same as firstchar */
1186 if (firstchar
== '=') {
1187 /* divider between ours and theirs? */
1188 if (len
!= 8 || line
[7] != '\n')
1190 } else if (len
< 8 || !isspace(line
[7])) {
1191 /* not divider before ours nor after theirs */
1197 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1199 struct checkdiff_t
*data
= priv
;
1200 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1201 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1202 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1203 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1206 if (line
[0] == '+') {
1209 if (is_conflict_marker(line
+ 1, len
- 1)) {
1211 fprintf(data
->o
->file
,
1212 "%s:%d: leftover conflict marker\n",
1213 data
->filename
, data
->lineno
);
1215 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1218 data
->status
|= bad
;
1219 err
= whitespace_error_string(bad
);
1220 fprintf(data
->o
->file
, "%s:%d: %s.\n",
1221 data
->filename
, data
->lineno
, err
);
1223 emit_line(data
->o
->file
, set
, reset
, line
, 1);
1224 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1225 data
->o
->file
, set
, reset
, ws
);
1226 } else if (line
[0] == ' ') {
1228 } else if (line
[0] == '@') {
1229 char *plus
= strchr(line
, '+');
1231 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1233 die("invalid diff");
1237 static unsigned char *deflate_it(char *data
,
1239 unsigned long *result_size
)
1242 unsigned char *deflated
;
1245 memset(&stream
, 0, sizeof(stream
));
1246 deflateInit(&stream
, zlib_compression_level
);
1247 bound
= deflateBound(&stream
, size
);
1248 deflated
= xmalloc(bound
);
1249 stream
.next_out
= deflated
;
1250 stream
.avail_out
= bound
;
1252 stream
.next_in
= (unsigned char *)data
;
1253 stream
.avail_in
= size
;
1254 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1256 deflateEnd(&stream
);
1257 *result_size
= stream
.total_out
;
1261 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1267 unsigned long orig_size
;
1268 unsigned long delta_size
;
1269 unsigned long deflate_size
;
1270 unsigned long data_size
;
1272 /* We could do deflated delta, or we could do just deflated two,
1273 * whichever is smaller.
1276 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1277 if (one
->size
&& two
->size
) {
1278 delta
= diff_delta(one
->ptr
, one
->size
,
1279 two
->ptr
, two
->size
,
1280 &delta_size
, deflate_size
);
1282 void *to_free
= delta
;
1283 orig_size
= delta_size
;
1284 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1289 if (delta
&& delta_size
< deflate_size
) {
1290 fprintf(file
, "delta %lu\n", orig_size
);
1293 data_size
= delta_size
;
1296 fprintf(file
, "literal %lu\n", two
->size
);
1299 data_size
= deflate_size
;
1302 /* emit data encoded in base85 */
1305 int bytes
= (52 < data_size
) ? 52 : data_size
;
1309 line
[0] = bytes
+ 'A' - 1;
1311 line
[0] = bytes
- 26 + 'a' - 1;
1312 encode_85(line
+ 1, cp
, bytes
);
1313 cp
= (char *) cp
+ bytes
;
1317 fprintf(file
, "\n");
1321 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1323 fprintf(file
, "GIT binary patch\n");
1324 emit_binary_diff_body(file
, one
, two
);
1325 emit_binary_diff_body(file
, two
, one
);
1328 static void setup_diff_attr_check(struct git_attr_check
*check
)
1330 static struct git_attr
*attr_diff
;
1333 attr_diff
= git_attr("diff", 4);
1335 check
[0].attr
= attr_diff
;
1338 static void diff_filespec_check_attr(struct diff_filespec
*one
)
1340 struct git_attr_check attr_diff_check
;
1341 int check_from_data
= 0;
1343 if (one
->checked_attr
)
1346 setup_diff_attr_check(&attr_diff_check
);
1348 one
->funcname_pattern_ident
= NULL
;
1350 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1354 value
= attr_diff_check
.value
;
1355 if (ATTR_TRUE(value
))
1357 else if (ATTR_FALSE(value
))
1360 check_from_data
= 1;
1362 /* funcname pattern ident */
1363 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1366 one
->funcname_pattern_ident
= value
;
1369 if (check_from_data
) {
1370 if (!one
->data
&& DIFF_FILE_VALID(one
))
1371 diff_populate_filespec(one
, 0);
1374 one
->is_binary
= buffer_is_binary(one
->data
, one
->size
);
1378 int diff_filespec_is_binary(struct diff_filespec
*one
)
1380 diff_filespec_check_attr(one
);
1381 return one
->is_binary
;
1384 static const struct funcname_pattern_entry
*funcname_pattern(const char *ident
)
1386 struct funcname_pattern_list
*pp
;
1388 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
1389 if (!strcmp(ident
, pp
->e
.name
))
1394 static const struct funcname_pattern_entry builtin_funcname_pattern
[] = {
1396 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
1397 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$",
1400 "^((procedure|function|constructor|destructor|interface|"
1401 "implementation|initialization|finalization)[ \t]*.*)$"
1403 "^(.*=[ \t]*(class|record).*)$",
1405 { "bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
1408 "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
1410 { "ruby", "^[ \t]*((class|module|def)[ \t].*)$",
1414 static const struct funcname_pattern_entry
*diff_funcname_pattern(struct diff_filespec
*one
)
1417 const struct funcname_pattern_entry
*pe
;
1420 diff_filespec_check_attr(one
);
1421 ident
= one
->funcname_pattern_ident
;
1425 * If the config file has "funcname.default" defined, that
1426 * regexp is used; otherwise NULL is returned and xemit uses
1427 * the built-in default.
1429 return funcname_pattern("default");
1431 /* Look up custom "funcname.$ident" regexp from config. */
1432 pe
= funcname_pattern(ident
);
1437 * And define built-in fallback patterns here. Note that
1438 * these can be overridden by the user's config settings.
1440 for (i
= 0; i
< ARRAY_SIZE(builtin_funcname_pattern
); i
++)
1441 if (!strcmp(ident
, builtin_funcname_pattern
[i
].name
))
1442 return &builtin_funcname_pattern
[i
];
1447 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
1449 char *ptr
= mf
->ptr
;
1450 long size
= mf
->size
;
1455 ptr
+= size
- 1; /* pointing at the very end */
1457 ; /* incomplete line */
1459 ptr
--; /* skip the last LF */
1460 while (mf
->ptr
< ptr
) {
1462 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
1463 if (*prev_eol
== '\n')
1465 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
1473 static int adds_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
, unsigned ws_rule
)
1476 l1
= count_trailing_blank(mf1
, ws_rule
);
1477 l2
= count_trailing_blank(mf2
, ws_rule
);
1480 /* starting where? */
1481 at
= count_lines(mf1
->ptr
, mf1
->size
);
1482 return (at
- l1
) + 1; /* the line number counts from 1 */
1485 static void builtin_diff(const char *name_a
,
1487 struct diff_filespec
*one
,
1488 struct diff_filespec
*two
,
1489 const char *xfrm_msg
,
1490 struct diff_options
*o
,
1491 int complete_rewrite
)
1495 char *a_one
, *b_two
;
1496 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1497 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1499 /* Never use a non-valid filename anywhere if at all possible */
1500 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
1501 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
1503 a_one
= quote_two(o
->a_prefix
, name_a
+ (*name_a
== '/'));
1504 b_two
= quote_two(o
->b_prefix
, name_b
+ (*name_b
== '/'));
1505 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1506 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1507 fprintf(o
->file
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1508 if (lbl
[0][0] == '/') {
1510 fprintf(o
->file
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1511 if (xfrm_msg
&& xfrm_msg
[0])
1512 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1514 else if (lbl
[1][0] == '/') {
1515 fprintf(o
->file
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1516 if (xfrm_msg
&& xfrm_msg
[0])
1517 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1520 if (one
->mode
!= two
->mode
) {
1521 fprintf(o
->file
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1522 fprintf(o
->file
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1524 if (xfrm_msg
&& xfrm_msg
[0])
1525 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1527 * we do not run diff between different kind
1530 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1531 goto free_ab_and_return
;
1532 if (complete_rewrite
) {
1533 emit_rewrite_diff(name_a
, name_b
, one
, two
, o
);
1534 o
->found_changes
= 1;
1535 goto free_ab_and_return
;
1539 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1540 die("unable to read files to diff");
1542 if (!DIFF_OPT_TST(o
, TEXT
) &&
1543 (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
))) {
1544 /* Quite common confusing case */
1545 if (mf1
.size
== mf2
.size
&&
1546 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1547 goto free_ab_and_return
;
1548 if (DIFF_OPT_TST(o
, BINARY
))
1549 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1551 fprintf(o
->file
, "Binary files %s and %s differ\n",
1553 o
->found_changes
= 1;
1556 /* Crazy xdl interfaces.. */
1557 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1561 struct emit_callback ecbdata
;
1562 const struct funcname_pattern_entry
*pe
;
1564 pe
= diff_funcname_pattern(one
);
1566 pe
= diff_funcname_pattern(two
);
1568 memset(&xecfg
, 0, sizeof(xecfg
));
1569 memset(&ecbdata
, 0, sizeof(ecbdata
));
1570 ecbdata
.label_path
= lbl
;
1571 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1572 ecbdata
.found_changesp
= &o
->found_changes
;
1573 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1574 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
1575 ecbdata
.blank_at_eof
=
1576 adds_blank_at_eof(&mf1
, &mf2
, ecbdata
.ws_rule
);
1577 ecbdata
.file
= o
->file
;
1578 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1579 xecfg
.ctxlen
= o
->context
;
1580 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1582 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
1585 else if (!prefixcmp(diffopts
, "--unified="))
1586 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1587 else if (!prefixcmp(diffopts
, "-u"))
1588 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1589 ecb
.outf
= xdiff_outf
;
1590 ecb
.priv
= &ecbdata
;
1591 ecbdata
.xm
.consume
= fn_out_consume
;
1592 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
)) {
1593 ecbdata
.diff_words
=
1594 xcalloc(1, sizeof(struct diff_words_data
));
1595 ecbdata
.diff_words
->file
= o
->file
;
1597 xdi_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1598 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
))
1599 free_diff_words_data(&ecbdata
);
1603 diff_free_filespec_data(one
);
1604 diff_free_filespec_data(two
);
1610 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1611 struct diff_filespec
*one
,
1612 struct diff_filespec
*two
,
1613 struct diffstat_t
*diffstat
,
1614 struct diff_options
*o
,
1615 int complete_rewrite
)
1618 struct diffstat_file
*data
;
1620 data
= diffstat_add(diffstat
, name_a
, name_b
);
1623 data
->is_unmerged
= 1;
1626 if (complete_rewrite
) {
1627 diff_populate_filespec(one
, 0);
1628 diff_populate_filespec(two
, 0);
1629 data
->deleted
= count_lines(one
->data
, one
->size
);
1630 data
->added
= count_lines(two
->data
, two
->size
);
1631 goto free_and_return
;
1633 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1634 die("unable to read files to diff");
1636 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1637 data
->is_binary
= 1;
1638 data
->added
= mf2
.size
;
1639 data
->deleted
= mf1
.size
;
1641 /* Crazy xdl interfaces.. */
1646 memset(&xecfg
, 0, sizeof(xecfg
));
1647 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1648 ecb
.outf
= xdiff_outf
;
1649 ecb
.priv
= diffstat
;
1650 xdi_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1654 diff_free_filespec_data(one
);
1655 diff_free_filespec_data(two
);
1658 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1659 const char *attr_path
,
1660 struct diff_filespec
*one
,
1661 struct diff_filespec
*two
,
1662 struct diff_options
*o
)
1665 struct checkdiff_t data
;
1670 memset(&data
, 0, sizeof(data
));
1671 data
.xm
.consume
= checkdiff_consume
;
1672 data
.filename
= name_b
? name_b
: name_a
;
1675 data
.ws_rule
= whitespace_rule(attr_path
);
1677 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1678 die("unable to read files to diff");
1681 * All the other codepaths check both sides, but not checking
1682 * the "old" side here is deliberate. We are checking the newly
1683 * introduced changes, and as long as the "new" side is text, we
1684 * can and should check what it introduces.
1686 if (diff_filespec_is_binary(two
))
1687 goto free_and_return
;
1689 /* Crazy xdl interfaces.. */
1694 memset(&xecfg
, 0, sizeof(xecfg
));
1695 xecfg
.ctxlen
= 1; /* at least one context line */
1696 xpp
.flags
= XDF_NEED_MINIMAL
;
1697 ecb
.outf
= xdiff_outf
;
1699 xdi_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1701 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
1702 int blank_at_eof
= adds_blank_at_eof(&mf1
, &mf2
, data
.ws_rule
);
1706 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
1707 fprintf(o
->file
, "%s:%d: %s.\n",
1708 data
.filename
, blank_at_eof
, err
);
1709 data
.status
= 1; /* report errors */
1714 diff_free_filespec_data(one
);
1715 diff_free_filespec_data(two
);
1717 DIFF_OPT_SET(o
, CHECK_FAILED
);
1720 struct diff_filespec
*alloc_filespec(const char *path
)
1722 int namelen
= strlen(path
);
1723 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1725 memset(spec
, 0, sizeof(*spec
));
1726 spec
->path
= (char *)(spec
+ 1);
1727 memcpy(spec
->path
, path
, namelen
+1);
1732 void free_filespec(struct diff_filespec
*spec
)
1734 if (!--spec
->count
) {
1735 diff_free_filespec_data(spec
);
1740 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1741 unsigned short mode
)
1744 spec
->mode
= canon_mode(mode
);
1745 hashcpy(spec
->sha1
, sha1
);
1746 spec
->sha1_valid
= !is_null_sha1(sha1
);
1751 * Given a name and sha1 pair, if the index tells us the file in
1752 * the work tree has that object contents, return true, so that
1753 * prepare_temp_file() does not have to inflate and extract.
1755 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1757 struct cache_entry
*ce
;
1761 /* We do not read the cache ourselves here, because the
1762 * benchmark with my previous version that always reads cache
1763 * shows that it makes things worse for diff-tree comparing
1764 * two linux-2.6 kernel trees in an already checked out work
1765 * tree. This is because most diff-tree comparisons deal with
1766 * only a small number of files, while reading the cache is
1767 * expensive for a large project, and its cost outweighs the
1768 * savings we get by not inflating the object to a temporary
1769 * file. Practically, this code only helps when we are used
1770 * by diff-cache --cached, which does read the cache before
1776 /* We want to avoid the working directory if our caller
1777 * doesn't need the data in a normal file, this system
1778 * is rather slow with its stat/open/mmap/close syscalls,
1779 * and the object is contained in a pack file. The pack
1780 * is probably already open and will be faster to obtain
1781 * the data through than the working directory. Loose
1782 * objects however would tend to be slower as they need
1783 * to be individually opened and inflated.
1785 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1789 pos
= cache_name_pos(name
, len
);
1792 ce
= active_cache
[pos
];
1795 * This is not the sha1 we are looking for, or
1796 * unreusable because it is not a regular file.
1798 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
1802 * If ce matches the file in the work tree, we can reuse it.
1804 if (ce_uptodate(ce
) ||
1805 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
1811 static int populate_from_stdin(struct diff_filespec
*s
)
1816 strbuf_init(&buf
, 0);
1817 if (strbuf_read(&buf
, 0, 0) < 0)
1818 return error("error while reading from stdin %s",
1821 s
->should_munmap
= 0;
1822 s
->data
= strbuf_detach(&buf
, &size
);
1828 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1831 char *data
= xmalloc(100);
1832 len
= snprintf(data
, 100,
1833 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1845 * While doing rename detection and pickaxe operation, we may need to
1846 * grab the data for the blob (or file) for our own in-core comparison.
1847 * diff_filespec has data and size fields for this purpose.
1849 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1852 if (!DIFF_FILE_VALID(s
))
1853 die("internal error: asking to populate invalid file.");
1854 if (S_ISDIR(s
->mode
))
1860 if (size_only
&& 0 < s
->size
)
1863 if (S_ISGITLINK(s
->mode
))
1864 return diff_populate_gitlink(s
, size_only
);
1866 if (!s
->sha1_valid
||
1867 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1872 if (!strcmp(s
->path
, "-"))
1873 return populate_from_stdin(s
);
1875 if (lstat(s
->path
, &st
) < 0) {
1876 if (errno
== ENOENT
) {
1880 s
->data
= (char *)"";
1885 s
->size
= xsize_t(st
.st_size
);
1890 if (S_ISLNK(st
.st_mode
)) {
1892 s
->data
= xmalloc(s
->size
);
1894 ret
= readlink(s
->path
, s
->data
, s
->size
);
1901 fd
= open(s
->path
, O_RDONLY
);
1904 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1906 s
->should_munmap
= 1;
1909 * Convert from working tree format to canonical git format
1911 strbuf_init(&buf
, 0);
1912 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
1914 munmap(s
->data
, s
->size
);
1915 s
->should_munmap
= 0;
1916 s
->data
= strbuf_detach(&buf
, &size
);
1922 enum object_type type
;
1924 type
= sha1_object_info(s
->sha1
, &s
->size
);
1926 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1933 void diff_free_filespec_blob(struct diff_filespec
*s
)
1937 else if (s
->should_munmap
)
1938 munmap(s
->data
, s
->size
);
1940 if (s
->should_free
|| s
->should_munmap
) {
1941 s
->should_free
= s
->should_munmap
= 0;
1946 void diff_free_filespec_data(struct diff_filespec
*s
)
1948 diff_free_filespec_blob(s
);
1953 static void prep_temp_blob(struct diff_tempfile
*temp
,
1956 const unsigned char *sha1
,
1961 fd
= git_mkstemp(temp
->tmp_path
, PATH_MAX
, ".diff_XXXXXX");
1963 die("unable to create temp-file: %s", strerror(errno
));
1964 if (write_in_full(fd
, blob
, size
) != size
)
1965 die("unable to write temp-file");
1967 temp
->name
= temp
->tmp_path
;
1968 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1970 sprintf(temp
->mode
, "%06o", mode
);
1973 static void prepare_temp_file(const char *name
,
1974 struct diff_tempfile
*temp
,
1975 struct diff_filespec
*one
)
1977 if (!DIFF_FILE_VALID(one
)) {
1979 /* A '-' entry produces this for file-2, and
1980 * a '+' entry produces this for file-1.
1982 temp
->name
= "/dev/null";
1983 strcpy(temp
->hex
, ".");
1984 strcpy(temp
->mode
, ".");
1988 if (!one
->sha1_valid
||
1989 reuse_worktree_file(name
, one
->sha1
, 1)) {
1991 if (lstat(name
, &st
) < 0) {
1992 if (errno
== ENOENT
)
1993 goto not_a_valid_file
;
1994 die("stat(%s): %s", name
, strerror(errno
));
1996 if (S_ISLNK(st
.st_mode
)) {
1998 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1999 size_t sz
= xsize_t(st
.st_size
);
2000 if (sizeof(buf
) <= st
.st_size
)
2001 die("symlink too long: %s", name
);
2002 ret
= readlink(name
, buf
, sz
);
2004 die("readlink(%s)", name
);
2005 prep_temp_blob(temp
, buf
, sz
,
2007 one
->sha1
: null_sha1
),
2009 one
->mode
: S_IFLNK
));
2012 /* we can borrow from the file in the work tree */
2014 if (!one
->sha1_valid
)
2015 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
2017 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
2018 /* Even though we may sometimes borrow the
2019 * contents from the work tree, we always want
2020 * one->mode. mode is trustworthy even when
2021 * !(one->sha1_valid), as long as
2022 * DIFF_FILE_VALID(one).
2024 sprintf(temp
->mode
, "%06o", one
->mode
);
2029 if (diff_populate_filespec(one
, 0))
2030 die("cannot read data blob for %s", one
->path
);
2031 prep_temp_blob(temp
, one
->data
, one
->size
,
2032 one
->sha1
, one
->mode
);
2036 static void remove_tempfile(void)
2040 for (i
= 0; i
< 2; i
++)
2041 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
2042 unlink(diff_temp
[i
].name
);
2043 diff_temp
[i
].name
= NULL
;
2047 static void remove_tempfile_on_signal(int signo
)
2050 signal(SIGINT
, SIG_DFL
);
2054 /* An external diff command takes:
2056 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2057 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2060 static void run_external_diff(const char *pgm
,
2063 struct diff_filespec
*one
,
2064 struct diff_filespec
*two
,
2065 const char *xfrm_msg
,
2066 int complete_rewrite
)
2068 const char *spawn_arg
[10];
2069 struct diff_tempfile
*temp
= diff_temp
;
2071 static int atexit_asked
= 0;
2072 const char *othername
;
2073 const char **arg
= &spawn_arg
[0];
2075 othername
= (other
? other
: name
);
2077 prepare_temp_file(name
, &temp
[0], one
);
2078 prepare_temp_file(othername
, &temp
[1], two
);
2079 if (! atexit_asked
&&
2080 (temp
[0].name
== temp
[0].tmp_path
||
2081 temp
[1].name
== temp
[1].tmp_path
)) {
2083 atexit(remove_tempfile
);
2085 signal(SIGINT
, remove_tempfile_on_signal
);
2091 *arg
++ = temp
[0].name
;
2092 *arg
++ = temp
[0].hex
;
2093 *arg
++ = temp
[0].mode
;
2094 *arg
++ = temp
[1].name
;
2095 *arg
++ = temp
[1].hex
;
2096 *arg
++ = temp
[1].mode
;
2107 retval
= run_command_v_opt(spawn_arg
, 0);
2110 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2115 static const char *external_diff_attr(const char *name
)
2117 struct git_attr_check attr_diff_check
;
2122 setup_diff_attr_check(&attr_diff_check
);
2123 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
2124 const char *value
= attr_diff_check
.value
;
2125 if (!ATTR_TRUE(value
) &&
2126 !ATTR_FALSE(value
) &&
2127 !ATTR_UNSET(value
)) {
2128 struct ll_diff_driver
*drv
;
2130 for (drv
= user_diff
; drv
; drv
= drv
->next
)
2131 if (!strcmp(drv
->name
, value
))
2138 static int similarity_index(struct diff_filepair
*p
)
2140 return p
->score
* 100 / MAX_SCORE
;
2143 static void fill_metainfo(struct strbuf
*msg
,
2146 struct diff_filespec
*one
,
2147 struct diff_filespec
*two
,
2148 struct diff_options
*o
,
2149 struct diff_filepair
*p
)
2151 strbuf_init(msg
, PATH_MAX
* 2 + 300);
2152 switch (p
->status
) {
2153 case DIFF_STATUS_COPIED
:
2154 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2155 strbuf_addstr(msg
, "\ncopy from ");
2156 quote_c_style(name
, msg
, NULL
, 0);
2157 strbuf_addstr(msg
, "\ncopy to ");
2158 quote_c_style(other
, msg
, NULL
, 0);
2159 strbuf_addch(msg
, '\n');
2161 case DIFF_STATUS_RENAMED
:
2162 strbuf_addf(msg
, "similarity index %d%%", similarity_index(p
));
2163 strbuf_addstr(msg
, "\nrename from ");
2164 quote_c_style(name
, msg
, NULL
, 0);
2165 strbuf_addstr(msg
, "\nrename to ");
2166 quote_c_style(other
, msg
, NULL
, 0);
2167 strbuf_addch(msg
, '\n');
2169 case DIFF_STATUS_MODIFIED
:
2171 strbuf_addf(msg
, "dissimilarity index %d%%\n",
2172 similarity_index(p
));
2180 if (one
&& two
&& hashcmp(one
->sha1
, two
->sha1
)) {
2181 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2183 if (DIFF_OPT_TST(o
, BINARY
)) {
2185 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2186 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2189 strbuf_addf(msg
, "index %.*s..%.*s",
2190 abbrev
, sha1_to_hex(one
->sha1
),
2191 abbrev
, sha1_to_hex(two
->sha1
));
2192 if (one
->mode
== two
->mode
)
2193 strbuf_addf(msg
, " %06o", one
->mode
);
2194 strbuf_addch(msg
, '\n');
2197 strbuf_setlen(msg
, msg
->len
- 1);
2200 static void run_diff_cmd(const char *pgm
,
2203 const char *attr_path
,
2204 struct diff_filespec
*one
,
2205 struct diff_filespec
*two
,
2207 struct diff_options
*o
,
2208 struct diff_filepair
*p
)
2210 const char *xfrm_msg
= NULL
;
2211 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
2214 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
);
2215 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
2218 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2221 const char *cmd
= external_diff_attr(attr_path
);
2227 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2232 builtin_diff(name
, other
? other
: name
,
2233 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2235 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2238 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2240 if (DIFF_FILE_VALID(one
)) {
2241 if (!one
->sha1_valid
) {
2243 if (!strcmp(one
->path
, "-")) {
2244 hashcpy(one
->sha1
, null_sha1
);
2247 if (lstat(one
->path
, &st
) < 0)
2248 die("stat %s", one
->path
);
2249 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2250 die("cannot hash %s\n", one
->path
);
2257 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2259 /* Strip the prefix but do not molest /dev/null and absolute paths */
2260 if (*namep
&& **namep
!= '/')
2261 *namep
+= prefix_length
;
2262 if (*otherp
&& **otherp
!= '/')
2263 *otherp
+= prefix_length
;
2266 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2268 const char *pgm
= external_diff();
2270 struct diff_filespec
*one
= p
->one
;
2271 struct diff_filespec
*two
= p
->two
;
2274 const char *attr_path
;
2276 name
= p
->one
->path
;
2277 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2279 if (o
->prefix_length
)
2280 strip_prefix(o
->prefix_length
, &name
, &other
);
2282 if (DIFF_PAIR_UNMERGED(p
)) {
2283 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2284 NULL
, NULL
, NULL
, o
, p
);
2288 diff_fill_sha1_info(one
);
2289 diff_fill_sha1_info(two
);
2292 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2293 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2295 * a filepair that changes between file and symlink
2296 * needs to be split into deletion and creation.
2298 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2299 run_diff_cmd(NULL
, name
, other
, attr_path
,
2300 one
, null
, &msg
, o
, p
);
2302 strbuf_release(&msg
);
2304 null
= alloc_filespec(one
->path
);
2305 run_diff_cmd(NULL
, name
, other
, attr_path
,
2306 null
, two
, &msg
, o
, p
);
2310 run_diff_cmd(pgm
, name
, other
, attr_path
,
2311 one
, two
, &msg
, o
, p
);
2313 strbuf_release(&msg
);
2316 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2317 struct diffstat_t
*diffstat
)
2321 int complete_rewrite
= 0;
2323 if (DIFF_PAIR_UNMERGED(p
)) {
2325 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2329 name
= p
->one
->path
;
2330 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2332 if (o
->prefix_length
)
2333 strip_prefix(o
->prefix_length
, &name
, &other
);
2335 diff_fill_sha1_info(p
->one
);
2336 diff_fill_sha1_info(p
->two
);
2338 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2339 complete_rewrite
= 1;
2340 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2343 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2347 const char *attr_path
;
2349 if (DIFF_PAIR_UNMERGED(p
)) {
2354 name
= p
->one
->path
;
2355 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2356 attr_path
= other
? other
: name
;
2358 if (o
->prefix_length
)
2359 strip_prefix(o
->prefix_length
, &name
, &other
);
2361 diff_fill_sha1_info(p
->one
);
2362 diff_fill_sha1_info(p
->two
);
2364 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2367 void diff_setup(struct diff_options
*options
)
2369 memset(options
, 0, sizeof(*options
));
2371 options
->file
= stdout
;
2373 options
->line_termination
= '\n';
2374 options
->break_opt
= -1;
2375 options
->rename_limit
= -1;
2376 options
->dirstat_percent
= 3;
2377 DIFF_OPT_CLR(options
, DIRSTAT_CUMULATIVE
);
2378 options
->context
= 3;
2380 options
->change
= diff_change
;
2381 options
->add_remove
= diff_addremove
;
2382 if (diff_use_color_default
> 0)
2383 DIFF_OPT_SET(options
, COLOR_DIFF
);
2385 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2386 options
->detect_rename
= diff_detect_rename_default
;
2388 options
->a_prefix
= "a/";
2389 options
->b_prefix
= "b/";
2392 int diff_setup_done(struct diff_options
*options
)
2396 if (options
->output_format
& DIFF_FORMAT_NAME
)
2398 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2400 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2402 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2405 die("--name-only, --name-status, --check and -s are mutually exclusive");
2407 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2408 options
->detect_rename
= DIFF_DETECT_COPY
;
2410 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2411 options
->prefix
= NULL
;
2412 if (options
->prefix
)
2413 options
->prefix_length
= strlen(options
->prefix
);
2415 options
->prefix_length
= 0;
2417 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2418 DIFF_FORMAT_NAME_STATUS
|
2419 DIFF_FORMAT_CHECKDIFF
|
2420 DIFF_FORMAT_NO_OUTPUT
))
2421 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2422 DIFF_FORMAT_NUMSTAT
|
2423 DIFF_FORMAT_DIFFSTAT
|
2424 DIFF_FORMAT_SHORTSTAT
|
2425 DIFF_FORMAT_DIRSTAT
|
2426 DIFF_FORMAT_SUMMARY
|
2430 * These cases always need recursive; we do not drop caller-supplied
2431 * recursive bits for other formats here.
2433 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2434 DIFF_FORMAT_NUMSTAT
|
2435 DIFF_FORMAT_DIFFSTAT
|
2436 DIFF_FORMAT_SHORTSTAT
|
2437 DIFF_FORMAT_DIRSTAT
|
2438 DIFF_FORMAT_SUMMARY
|
2439 DIFF_FORMAT_CHECKDIFF
))
2440 DIFF_OPT_SET(options
, RECURSIVE
);
2442 * Also pickaxe would not work very well if you do not say recursive
2444 if (options
->pickaxe
)
2445 DIFF_OPT_SET(options
, RECURSIVE
);
2447 if (options
->detect_rename
&& options
->rename_limit
< 0)
2448 options
->rename_limit
= diff_rename_limit_default
;
2449 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2451 /* read-cache does not die even when it fails
2452 * so it is safe for us to do this here. Also
2453 * it does not smudge active_cache or active_nr
2454 * when it fails, so we do not have to worry about
2455 * cleaning it up ourselves either.
2459 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2460 options
->abbrev
= 40; /* full */
2463 * It does not make sense to show the first hit we happened
2464 * to have found. It does not make sense not to return with
2465 * exit code in such a case either.
2467 if (DIFF_OPT_TST(options
, QUIET
)) {
2468 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2469 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2475 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2485 if (c
== arg_short
) {
2489 if (val
&& isdigit(c
)) {
2491 int n
= strtoul(arg
, &end
, 10);
2502 eq
= strchr(arg
, '=');
2507 if (!len
|| strncmp(arg
, arg_long
, len
))
2512 if (!isdigit(*++eq
))
2514 n
= strtoul(eq
, &end
, 10);
2522 static int diff_scoreopt_parse(const char *opt
);
2524 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2526 const char *arg
= av
[0];
2528 /* Output format options */
2529 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2530 options
->output_format
|= DIFF_FORMAT_PATCH
;
2531 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2532 options
->output_format
|= DIFF_FORMAT_PATCH
;
2533 else if (!strcmp(arg
, "--raw"))
2534 options
->output_format
|= DIFF_FORMAT_RAW
;
2535 else if (!strcmp(arg
, "--patch-with-raw"))
2536 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2537 else if (!strcmp(arg
, "--numstat"))
2538 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2539 else if (!strcmp(arg
, "--shortstat"))
2540 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2541 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2542 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2543 else if (!strcmp(arg
, "--cumulative")) {
2544 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2545 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
2547 else if (!strcmp(arg
, "--check"))
2548 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2549 else if (!strcmp(arg
, "--summary"))
2550 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2551 else if (!strcmp(arg
, "--patch-with-stat"))
2552 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2553 else if (!strcmp(arg
, "--name-only"))
2554 options
->output_format
|= DIFF_FORMAT_NAME
;
2555 else if (!strcmp(arg
, "--name-status"))
2556 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2557 else if (!strcmp(arg
, "-s"))
2558 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2559 else if (!prefixcmp(arg
, "--stat")) {
2561 int width
= options
->stat_width
;
2562 int name_width
= options
->stat_name_width
;
2568 if (!prefixcmp(arg
, "-width="))
2569 width
= strtoul(arg
+ 7, &end
, 10);
2570 else if (!prefixcmp(arg
, "-name-width="))
2571 name_width
= strtoul(arg
+ 12, &end
, 10);
2574 width
= strtoul(arg
+1, &end
, 10);
2576 name_width
= strtoul(end
+1, &end
, 10);
2579 /* Important! This checks all the error cases! */
2582 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2583 options
->stat_name_width
= name_width
;
2584 options
->stat_width
= width
;
2587 /* renames options */
2588 else if (!prefixcmp(arg
, "-B")) {
2589 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2592 else if (!prefixcmp(arg
, "-M")) {
2593 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2595 options
->detect_rename
= DIFF_DETECT_RENAME
;
2597 else if (!prefixcmp(arg
, "-C")) {
2598 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2599 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2600 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2602 options
->detect_rename
= DIFF_DETECT_COPY
;
2604 else if (!strcmp(arg
, "--no-renames"))
2605 options
->detect_rename
= 0;
2606 else if (!strcmp(arg
, "--relative"))
2607 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2608 else if (!prefixcmp(arg
, "--relative=")) {
2609 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2610 options
->prefix
= arg
+ 11;
2614 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2615 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2616 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2617 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2618 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2619 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2622 else if (!strcmp(arg
, "--binary")) {
2623 options
->output_format
|= DIFF_FORMAT_PATCH
;
2624 DIFF_OPT_SET(options
, BINARY
);
2626 else if (!strcmp(arg
, "--full-index"))
2627 DIFF_OPT_SET(options
, FULL_INDEX
);
2628 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2629 DIFF_OPT_SET(options
, TEXT
);
2630 else if (!strcmp(arg
, "-R"))
2631 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2632 else if (!strcmp(arg
, "--find-copies-harder"))
2633 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2634 else if (!strcmp(arg
, "--follow"))
2635 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2636 else if (!strcmp(arg
, "--color"))
2637 DIFF_OPT_SET(options
, COLOR_DIFF
);
2638 else if (!strcmp(arg
, "--no-color"))
2639 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2640 else if (!strcmp(arg
, "--color-words"))
2641 options
->flags
|= DIFF_OPT_COLOR_DIFF
| DIFF_OPT_COLOR_DIFF_WORDS
;
2642 else if (!strcmp(arg
, "--exit-code"))
2643 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2644 else if (!strcmp(arg
, "--quiet"))
2645 DIFF_OPT_SET(options
, QUIET
);
2646 else if (!strcmp(arg
, "--ext-diff"))
2647 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2648 else if (!strcmp(arg
, "--no-ext-diff"))
2649 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2650 else if (!strcmp(arg
, "--ignore-submodules"))
2651 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
2654 else if (!strcmp(arg
, "-z"))
2655 options
->line_termination
= 0;
2656 else if (!prefixcmp(arg
, "-l"))
2657 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2658 else if (!prefixcmp(arg
, "-S"))
2659 options
->pickaxe
= arg
+ 2;
2660 else if (!strcmp(arg
, "--pickaxe-all"))
2661 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2662 else if (!strcmp(arg
, "--pickaxe-regex"))
2663 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2664 else if (!prefixcmp(arg
, "-O"))
2665 options
->orderfile
= arg
+ 2;
2666 else if (!prefixcmp(arg
, "--diff-filter="))
2667 options
->filter
= arg
+ 14;
2668 else if (!strcmp(arg
, "--abbrev"))
2669 options
->abbrev
= DEFAULT_ABBREV
;
2670 else if (!prefixcmp(arg
, "--abbrev=")) {
2671 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2672 if (options
->abbrev
< MINIMUM_ABBREV
)
2673 options
->abbrev
= MINIMUM_ABBREV
;
2674 else if (40 < options
->abbrev
)
2675 options
->abbrev
= 40;
2677 else if (!prefixcmp(arg
, "--src-prefix="))
2678 options
->a_prefix
= arg
+ 13;
2679 else if (!prefixcmp(arg
, "--dst-prefix="))
2680 options
->b_prefix
= arg
+ 13;
2681 else if (!strcmp(arg
, "--no-prefix"))
2682 options
->a_prefix
= options
->b_prefix
= "";
2683 else if (!prefixcmp(arg
, "--output=")) {
2684 options
->file
= fopen(arg
+ strlen("--output="), "w");
2685 options
->close_file
= 1;
2691 static int parse_num(const char **cp_p
)
2693 unsigned long num
, scale
;
2695 const char *cp
= *cp_p
;
2702 if ( !dot
&& ch
== '.' ) {
2705 } else if ( ch
== '%' ) {
2706 scale
= dot
? scale
*100 : 100;
2707 cp
++; /* % is always at the end */
2709 } else if ( ch
>= '0' && ch
<= '9' ) {
2710 if ( scale
< 100000 ) {
2712 num
= (num
*10) + (ch
-'0');
2721 /* user says num divided by scale and we say internally that
2722 * is MAX_SCORE * num / scale.
2724 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2727 static int diff_scoreopt_parse(const char *opt
)
2729 int opt1
, opt2
, cmd
;
2734 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2735 return -1; /* that is not a -M, -C nor -B option */
2737 opt1
= parse_num(&opt
);
2743 else if (*opt
!= '/')
2744 return -1; /* we expect -B80/99 or -B80 */
2747 opt2
= parse_num(&opt
);
2752 return opt1
| (opt2
<< 16);
2755 struct diff_queue_struct diff_queued_diff
;
2757 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2759 if (queue
->alloc
<= queue
->nr
) {
2760 queue
->alloc
= alloc_nr(queue
->alloc
);
2761 queue
->queue
= xrealloc(queue
->queue
,
2762 sizeof(dp
) * queue
->alloc
);
2764 queue
->queue
[queue
->nr
++] = dp
;
2767 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2768 struct diff_filespec
*one
,
2769 struct diff_filespec
*two
)
2771 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2779 void diff_free_filepair(struct diff_filepair
*p
)
2781 free_filespec(p
->one
);
2782 free_filespec(p
->two
);
2786 /* This is different from find_unique_abbrev() in that
2787 * it stuffs the result with dots for alignment.
2789 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2794 return sha1_to_hex(sha1
);
2796 abbrev
= find_unique_abbrev(sha1
, len
);
2797 abblen
= strlen(abbrev
);
2799 static char hex
[41];
2800 if (len
< abblen
&& abblen
<= len
+ 2)
2801 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2803 sprintf(hex
, "%s...", abbrev
);
2806 return sha1_to_hex(sha1
);
2809 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
2811 int line_termination
= opt
->line_termination
;
2812 int inter_name_termination
= line_termination
? '\t' : '\0';
2814 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2815 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
2816 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
2817 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
2820 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
2821 inter_name_termination
);
2823 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
2826 if (p
->status
== DIFF_STATUS_COPIED
||
2827 p
->status
== DIFF_STATUS_RENAMED
) {
2828 const char *name_a
, *name_b
;
2829 name_a
= p
->one
->path
;
2830 name_b
= p
->two
->path
;
2831 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2832 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
2833 write_name_quoted(name_b
, opt
->file
, line_termination
);
2835 const char *name_a
, *name_b
;
2836 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
2838 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2839 write_name_quoted(name_a
, opt
->file
, line_termination
);
2843 int diff_unmodified_pair(struct diff_filepair
*p
)
2845 /* This function is written stricter than necessary to support
2846 * the currently implemented transformers, but the idea is to
2847 * let transformers to produce diff_filepairs any way they want,
2848 * and filter and clean them up here before producing the output.
2850 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
2852 if (DIFF_PAIR_UNMERGED(p
))
2853 return 0; /* unmerged is interesting */
2855 /* deletion, addition, mode or type change
2856 * and rename are all interesting.
2858 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2859 DIFF_PAIR_MODE_CHANGED(p
) ||
2860 strcmp(one
->path
, two
->path
))
2863 /* both are valid and point at the same path. that is, we are
2864 * dealing with a change.
2866 if (one
->sha1_valid
&& two
->sha1_valid
&&
2867 !hashcmp(one
->sha1
, two
->sha1
))
2868 return 1; /* no change */
2869 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2870 return 1; /* both look at the same file on the filesystem. */
2874 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2876 if (diff_unmodified_pair(p
))
2879 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2880 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2881 return; /* no tree diffs in patch format */
2886 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2887 struct diffstat_t
*diffstat
)
2889 if (diff_unmodified_pair(p
))
2892 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2893 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2894 return; /* no tree diffs in patch format */
2896 run_diffstat(p
, o
, diffstat
);
2899 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2900 struct diff_options
*o
)
2902 if (diff_unmodified_pair(p
))
2905 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2906 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2907 return; /* no tree diffs in patch format */
2909 run_checkdiff(p
, o
);
2912 int diff_queue_is_empty(void)
2914 struct diff_queue_struct
*q
= &diff_queued_diff
;
2916 for (i
= 0; i
< q
->nr
; i
++)
2917 if (!diff_unmodified_pair(q
->queue
[i
]))
2923 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2925 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2928 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2930 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2931 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2933 s
->size
, s
->xfrm_flags
);
2936 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2938 diff_debug_filespec(p
->one
, i
, "one");
2939 diff_debug_filespec(p
->two
, i
, "two");
2940 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
2941 p
->score
, p
->status
? p
->status
: '?',
2942 p
->one
->rename_used
, p
->broken_pair
);
2945 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2949 fprintf(stderr
, "%s\n", msg
);
2950 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2951 for (i
= 0; i
< q
->nr
; i
++) {
2952 struct diff_filepair
*p
= q
->queue
[i
];
2953 diff_debug_filepair(p
, i
);
2958 static void diff_resolve_rename_copy(void)
2961 struct diff_filepair
*p
;
2962 struct diff_queue_struct
*q
= &diff_queued_diff
;
2964 diff_debug_queue("resolve-rename-copy", q
);
2966 for (i
= 0; i
< q
->nr
; i
++) {
2968 p
->status
= 0; /* undecided */
2969 if (DIFF_PAIR_UNMERGED(p
))
2970 p
->status
= DIFF_STATUS_UNMERGED
;
2971 else if (!DIFF_FILE_VALID(p
->one
))
2972 p
->status
= DIFF_STATUS_ADDED
;
2973 else if (!DIFF_FILE_VALID(p
->two
))
2974 p
->status
= DIFF_STATUS_DELETED
;
2975 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2976 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2978 /* from this point on, we are dealing with a pair
2979 * whose both sides are valid and of the same type, i.e.
2980 * either in-place edit or rename/copy edit.
2982 else if (DIFF_PAIR_RENAME(p
)) {
2984 * A rename might have re-connected a broken
2985 * pair up, causing the pathnames to be the
2986 * same again. If so, that's not a rename at
2987 * all, just a modification..
2989 * Otherwise, see if this source was used for
2990 * multiple renames, in which case we decrement
2991 * the count, and call it a copy.
2993 if (!strcmp(p
->one
->path
, p
->two
->path
))
2994 p
->status
= DIFF_STATUS_MODIFIED
;
2995 else if (--p
->one
->rename_used
> 0)
2996 p
->status
= DIFF_STATUS_COPIED
;
2998 p
->status
= DIFF_STATUS_RENAMED
;
3000 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
3001 p
->one
->mode
!= p
->two
->mode
||
3002 is_null_sha1(p
->one
->sha1
))
3003 p
->status
= DIFF_STATUS_MODIFIED
;
3005 /* This is a "no-change" entry and should not
3006 * happen anymore, but prepare for broken callers.
3008 error("feeding unmodified %s to diffcore",
3010 p
->status
= DIFF_STATUS_UNKNOWN
;
3013 diff_debug_queue("resolve-rename-copy done", q
);
3016 static int check_pair_status(struct diff_filepair
*p
)
3018 switch (p
->status
) {
3019 case DIFF_STATUS_UNKNOWN
:
3022 die("internal error in diff-resolve-rename-copy");
3028 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
3030 int fmt
= opt
->output_format
;
3032 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
3033 diff_flush_checkdiff(p
, opt
);
3034 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
3035 diff_flush_raw(p
, opt
);
3036 else if (fmt
& DIFF_FORMAT_NAME
) {
3037 const char *name_a
, *name_b
;
3038 name_a
= p
->two
->path
;
3040 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3041 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
3045 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
3048 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
3050 fprintf(file
, " %s ", newdelete
);
3051 write_name_quoted(fs
->path
, file
, '\n');
3055 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
3057 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
3058 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
3059 show_name
? ' ' : '\n');
3061 write_name_quoted(p
->two
->path
, file
, '\n');
3066 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
3068 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
3070 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
3072 show_mode_change(file
, p
, 0);
3075 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
3078 case DIFF_STATUS_DELETED
:
3079 show_file_mode_name(file
, "delete", p
->one
);
3081 case DIFF_STATUS_ADDED
:
3082 show_file_mode_name(file
, "create", p
->two
);
3084 case DIFF_STATUS_COPIED
:
3085 show_rename_copy(file
, "copy", p
);
3087 case DIFF_STATUS_RENAMED
:
3088 show_rename_copy(file
, "rename", p
);
3092 fputs(" rewrite ", file
);
3093 write_name_quoted(p
->two
->path
, file
, ' ');
3094 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3096 show_mode_change(file
, p
, !p
->score
);
3102 struct xdiff_emit_state xm
;
3107 static int remove_space(char *line
, int len
)
3113 for (i
= 0; i
< len
; i
++)
3114 if (!isspace((c
= line
[i
])))
3120 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3122 struct patch_id_t
*data
= priv
;
3125 /* Ignore line numbers when computing the SHA1 of the patch */
3126 if (!prefixcmp(line
, "@@ -"))
3129 new_len
= remove_space(line
, len
);
3131 SHA1_Update(data
->ctx
, line
, new_len
);
3132 data
->patchlen
+= new_len
;
3135 /* returns 0 upon success, and writes result into sha1 */
3136 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3138 struct diff_queue_struct
*q
= &diff_queued_diff
;
3141 struct patch_id_t data
;
3142 char buffer
[PATH_MAX
* 4 + 20];
3145 memset(&data
, 0, sizeof(struct patch_id_t
));
3147 data
.xm
.consume
= patch_id_consume
;
3149 for (i
= 0; i
< q
->nr
; i
++) {
3154 struct diff_filepair
*p
= q
->queue
[i
];
3157 memset(&xecfg
, 0, sizeof(xecfg
));
3159 return error("internal diff status error");
3160 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3162 if (diff_unmodified_pair(p
))
3164 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3165 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3167 if (DIFF_PAIR_UNMERGED(p
))
3170 diff_fill_sha1_info(p
->one
);
3171 diff_fill_sha1_info(p
->two
);
3172 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3173 fill_mmfile(&mf2
, p
->two
) < 0)
3174 return error("unable to read files to diff");
3176 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3177 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3178 if (p
->one
->mode
== 0)
3179 len1
= snprintf(buffer
, sizeof(buffer
),
3180 "diff--gita/%.*sb/%.*s"
3187 len2
, p
->two
->path
);
3188 else if (p
->two
->mode
== 0)
3189 len1
= snprintf(buffer
, sizeof(buffer
),
3190 "diff--gita/%.*sb/%.*s"
3191 "deletedfilemode%06o"
3197 len1
, p
->one
->path
);
3199 len1
= snprintf(buffer
, sizeof(buffer
),
3200 "diff--gita/%.*sb/%.*s"
3206 len2
, p
->two
->path
);
3207 SHA1_Update(&ctx
, buffer
, len1
);
3209 xpp
.flags
= XDF_NEED_MINIMAL
;
3211 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3212 ecb
.outf
= xdiff_outf
;
3214 xdi_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
3217 SHA1_Final(sha1
, &ctx
);
3221 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3223 struct diff_queue_struct
*q
= &diff_queued_diff
;
3225 int result
= diff_get_patch_id(options
, sha1
);
3227 for (i
= 0; i
< q
->nr
; i
++)
3228 diff_free_filepair(q
->queue
[i
]);
3232 q
->nr
= q
->alloc
= 0;
3237 static int is_summary_empty(const struct diff_queue_struct
*q
)
3241 for (i
= 0; i
< q
->nr
; i
++) {
3242 const struct diff_filepair
*p
= q
->queue
[i
];
3244 switch (p
->status
) {
3245 case DIFF_STATUS_DELETED
:
3246 case DIFF_STATUS_ADDED
:
3247 case DIFF_STATUS_COPIED
:
3248 case DIFF_STATUS_RENAMED
:
3253 if (p
->one
->mode
&& p
->two
->mode
&&
3254 p
->one
->mode
!= p
->two
->mode
)
3262 void diff_flush(struct diff_options
*options
)
3264 struct diff_queue_struct
*q
= &diff_queued_diff
;
3265 int i
, output_format
= options
->output_format
;
3269 * Order: raw, stat, summary, patch
3270 * or: name/name-status/checkdiff (other bits clear)
3275 if (output_format
& (DIFF_FORMAT_RAW
|
3277 DIFF_FORMAT_NAME_STATUS
|
3278 DIFF_FORMAT_CHECKDIFF
)) {
3279 for (i
= 0; i
< q
->nr
; i
++) {
3280 struct diff_filepair
*p
= q
->queue
[i
];
3281 if (check_pair_status(p
))
3282 flush_one_pair(p
, options
);
3287 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3288 struct diffstat_t diffstat
;
3290 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3291 diffstat
.xm
.consume
= diffstat_consume
;
3292 for (i
= 0; i
< q
->nr
; i
++) {
3293 struct diff_filepair
*p
= q
->queue
[i
];
3294 if (check_pair_status(p
))
3295 diff_flush_stat(p
, options
, &diffstat
);
3297 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3298 show_numstat(&diffstat
, options
);
3299 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3300 show_stats(&diffstat
, options
);
3301 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3302 show_shortstats(&diffstat
, options
);
3303 free_diffstat_info(&diffstat
);
3306 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3307 show_dirstat(options
);
3309 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3310 for (i
= 0; i
< q
->nr
; i
++)
3311 diff_summary(options
->file
, q
->queue
[i
]);
3315 if (output_format
& DIFF_FORMAT_PATCH
) {
3317 putc(options
->line_termination
, options
->file
);
3318 if (options
->stat_sep
) {
3319 /* attach patch instead of inline */
3320 fputs(options
->stat_sep
, options
->file
);
3324 for (i
= 0; i
< q
->nr
; i
++) {
3325 struct diff_filepair
*p
= q
->queue
[i
];
3326 if (check_pair_status(p
))
3327 diff_flush_patch(p
, options
);
3331 if (output_format
& DIFF_FORMAT_CALLBACK
)
3332 options
->format_callback(q
, options
, options
->format_callback_data
);
3334 for (i
= 0; i
< q
->nr
; i
++)
3335 diff_free_filepair(q
->queue
[i
]);
3339 q
->nr
= q
->alloc
= 0;
3340 if (options
->close_file
)
3341 fclose(options
->file
);
3344 static void diffcore_apply_filter(const char *filter
)
3347 struct diff_queue_struct
*q
= &diff_queued_diff
;
3348 struct diff_queue_struct outq
;
3350 outq
.nr
= outq
.alloc
= 0;
3355 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3357 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3358 struct diff_filepair
*p
= q
->queue
[i
];
3359 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3361 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3363 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3364 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3365 strchr(filter
, p
->status
)))
3371 /* otherwise we will clear the whole queue
3372 * by copying the empty outq at the end of this
3373 * function, but first clear the current entries
3376 for (i
= 0; i
< q
->nr
; i
++)
3377 diff_free_filepair(q
->queue
[i
]);
3380 /* Only the matching ones */
3381 for (i
= 0; i
< q
->nr
; i
++) {
3382 struct diff_filepair
*p
= q
->queue
[i
];
3384 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3386 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3388 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3389 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3390 strchr(filter
, p
->status
)))
3393 diff_free_filepair(p
);
3400 /* Check whether two filespecs with the same mode and size are identical */
3401 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3402 struct diff_filespec
*two
)
3404 if (S_ISGITLINK(one
->mode
))
3406 if (diff_populate_filespec(one
, 0))
3408 if (diff_populate_filespec(two
, 0))
3410 return !memcmp(one
->data
, two
->data
, one
->size
);
3413 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3416 struct diff_queue_struct
*q
= &diff_queued_diff
;
3417 struct diff_queue_struct outq
;
3419 outq
.nr
= outq
.alloc
= 0;
3421 for (i
= 0; i
< q
->nr
; i
++) {
3422 struct diff_filepair
*p
= q
->queue
[i
];
3425 * 1. Entries that come from stat info dirtyness
3426 * always have both sides (iow, not create/delete),
3427 * one side of the object name is unknown, with
3428 * the same mode and size. Keep the ones that
3429 * do not match these criteria. They have real
3432 * 2. At this point, the file is known to be modified,
3433 * with the same mode and size, and the object
3434 * name of one side is unknown. Need to inspect
3435 * the identical contents.
3437 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3438 !DIFF_FILE_VALID(p
->two
) ||
3439 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3440 (p
->one
->mode
!= p
->two
->mode
) ||
3441 diff_populate_filespec(p
->one
, 1) ||
3442 diff_populate_filespec(p
->two
, 1) ||
3443 (p
->one
->size
!= p
->two
->size
) ||
3444 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3448 * The caller can subtract 1 from skip_stat_unmatch
3449 * to determine how many paths were dirty only
3450 * due to stat info mismatch.
3452 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3453 diffopt
->skip_stat_unmatch
++;
3454 diff_free_filepair(p
);
3461 void diffcore_std(struct diff_options
*options
)
3463 if (options
->skip_stat_unmatch
)
3464 diffcore_skip_stat_unmatch(options
);
3465 if (options
->break_opt
!= -1)
3466 diffcore_break(options
->break_opt
);
3467 if (options
->detect_rename
)
3468 diffcore_rename(options
);
3469 if (options
->break_opt
!= -1)
3470 diffcore_merge_broken();
3471 if (options
->pickaxe
)
3472 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3473 if (options
->orderfile
)
3474 diffcore_order(options
->orderfile
);
3475 diff_resolve_rename_copy();
3476 diffcore_apply_filter(options
->filter
);
3478 if (diff_queued_diff
.nr
)
3479 DIFF_OPT_SET(options
, HAS_CHANGES
);
3481 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3484 int diff_result_code(struct diff_options
*opt
, int status
)
3487 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3488 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3490 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3491 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3493 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3494 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3499 void diff_addremove(struct diff_options
*options
,
3500 int addremove
, unsigned mode
,
3501 const unsigned char *sha1
,
3502 const char *concatpath
)
3504 struct diff_filespec
*one
, *two
;
3506 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
3509 /* This may look odd, but it is a preparation for
3510 * feeding "there are unchanged files which should
3511 * not produce diffs, but when you are doing copy
3512 * detection you would need them, so here they are"
3513 * entries to the diff-core. They will be prefixed
3514 * with something like '=' or '*' (I haven't decided
3515 * which but should not make any difference).
3516 * Feeding the same new and old to diff_change()
3517 * also has the same effect.
3518 * Before the final output happens, they are pruned after
3519 * merged into rename/copy pairs as appropriate.
3521 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3522 addremove
= (addremove
== '+' ? '-' :
3523 addremove
== '-' ? '+' : addremove
);
3525 if (options
->prefix
&&
3526 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3529 one
= alloc_filespec(concatpath
);
3530 two
= alloc_filespec(concatpath
);
3532 if (addremove
!= '+')
3533 fill_filespec(one
, sha1
, mode
);
3534 if (addremove
!= '-')
3535 fill_filespec(two
, sha1
, mode
);
3537 diff_queue(&diff_queued_diff
, one
, two
);
3538 DIFF_OPT_SET(options
, HAS_CHANGES
);
3541 void diff_change(struct diff_options
*options
,
3542 unsigned old_mode
, unsigned new_mode
,
3543 const unsigned char *old_sha1
,
3544 const unsigned char *new_sha1
,
3545 const char *concatpath
)
3547 struct diff_filespec
*one
, *two
;
3549 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
3550 && S_ISGITLINK(new_mode
))
3553 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3555 const unsigned char *tmp_c
;
3556 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3557 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3560 if (options
->prefix
&&
3561 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3564 one
= alloc_filespec(concatpath
);
3565 two
= alloc_filespec(concatpath
);
3566 fill_filespec(one
, old_sha1
, old_mode
);
3567 fill_filespec(two
, new_sha1
, new_mode
);
3569 diff_queue(&diff_queued_diff
, one
, two
);
3570 DIFF_OPT_SET(options
, HAS_CHANGES
);
3573 void diff_unmerge(struct diff_options
*options
,
3575 unsigned mode
, const unsigned char *sha1
)
3577 struct diff_filespec
*one
, *two
;
3579 if (options
->prefix
&&
3580 strncmp(path
, options
->prefix
, options
->prefix_length
))
3583 one
= alloc_filespec(path
);
3584 two
= alloc_filespec(path
);
3585 fill_filespec(one
, sha1
, mode
);
3586 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;