2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
13 #ifdef NO_FAST_WORKING_DIRECTORY
14 #define FAST_WORKING_DIRECTORY 0
16 #define FAST_WORKING_DIRECTORY 1
19 static int diff_detect_rename_default
;
20 static int diff_rename_limit_default
= 100;
21 static int diff_use_color_default
;
22 int diff_auto_refresh_index
= 1;
24 static char diff_colors
[][COLOR_MAXLEN
] = {
26 "", /* PLAIN (normal) */
27 "\033[1m", /* METAINFO (bold) */
28 "\033[36m", /* FRAGINFO (cyan) */
29 "\033[31m", /* OLD (red) */
30 "\033[32m", /* NEW (green) */
31 "\033[33m", /* COMMIT (yellow) */
32 "\033[41m", /* WHITESPACE (red background) */
35 static int parse_diff_color_slot(const char *var
, int ofs
)
37 if (!strcasecmp(var
+ofs
, "plain"))
39 if (!strcasecmp(var
+ofs
, "meta"))
41 if (!strcasecmp(var
+ofs
, "frag"))
43 if (!strcasecmp(var
+ofs
, "old"))
45 if (!strcasecmp(var
+ofs
, "new"))
47 if (!strcasecmp(var
+ofs
, "commit"))
49 if (!strcasecmp(var
+ofs
, "whitespace"))
50 return DIFF_WHITESPACE
;
51 die("bad config variable '%s'", var
);
54 static struct ll_diff_driver
{
56 struct ll_diff_driver
*next
;
58 } *user_diff
, **user_diff_tail
;
60 static void read_config_if_needed(void)
62 if (!user_diff_tail
) {
63 user_diff_tail
= &user_diff
;
64 git_config(git_diff_ui_config
);
69 * Currently there is only "diff.<drivername>.command" variable;
70 * because there are "diff.color.<slot>" variables, we are parsing
71 * this in a bit convoluted way to allow low level diff driver
74 static int parse_lldiff_command(const char *var
, const char *ep
, const char *value
)
78 struct ll_diff_driver
*drv
;
82 for (drv
= user_diff
; drv
; drv
= drv
->next
)
83 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
86 drv
= xcalloc(1, sizeof(struct ll_diff_driver
));
87 drv
->name
= xmemdupz(name
, namelen
);
89 user_diff_tail
= &user_diff
;
90 *user_diff_tail
= drv
;
91 user_diff_tail
= &(drv
->next
);
95 return error("%s: lacks value", var
);
96 drv
->cmd
= strdup(value
);
101 * 'diff.<what>.funcname' attribute can be specified in the configuration
102 * to define a customized regexp to find the beginning of a function to
103 * be used for hunk header lines of "diff -p" style output.
105 static struct funcname_pattern
{
108 struct funcname_pattern
*next
;
109 } *funcname_pattern_list
;
111 static int parse_funcname_pattern(const char *var
, const char *ep
, const char *value
)
115 struct funcname_pattern
*pp
;
117 name
= var
+ 5; /* "diff." */
120 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
121 if (!strncmp(pp
->name
, name
, namelen
) && !pp
->name
[namelen
])
124 pp
= xcalloc(1, sizeof(*pp
));
125 pp
->name
= xmemdupz(name
, namelen
);
126 pp
->next
= funcname_pattern_list
;
127 funcname_pattern_list
= pp
;
131 pp
->pattern
= xstrdup(value
);
136 * These are to give UI layer defaults.
137 * The core-level commands such as git-diff-files should
138 * never be affected by the setting of diff.renames
139 * the user happens to have in the configuration file.
141 int git_diff_ui_config(const char *var
, const char *value
)
143 if (!strcmp(var
, "diff.renamelimit")) {
144 diff_rename_limit_default
= git_config_int(var
, value
);
147 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
148 diff_use_color_default
= git_config_colorbool(var
, value
);
151 if (!strcmp(var
, "diff.renames")) {
153 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
154 else if (!strcasecmp(value
, "copies") ||
155 !strcasecmp(value
, "copy"))
156 diff_detect_rename_default
= DIFF_DETECT_COPY
;
157 else if (git_config_bool(var
,value
))
158 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
161 if (!strcmp(var
, "diff.autorefreshindex")) {
162 diff_auto_refresh_index
= git_config_bool(var
, value
);
165 if (!prefixcmp(var
, "diff.")) {
166 const char *ep
= strrchr(var
, '.');
169 if (!strcmp(ep
, ".command"))
170 return parse_lldiff_command(var
, ep
, value
);
171 if (!strcmp(ep
, ".funcname"))
172 return parse_funcname_pattern(var
, ep
, value
);
175 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
176 int slot
= parse_diff_color_slot(var
, 11);
177 color_parse(value
, var
, diff_colors
[slot
]);
181 return git_default_config(var
, value
);
184 static char *quote_one(const char *str
)
191 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
194 xp
= xmalloc(needlen
+ 1);
195 quote_c_style(str
, xp
, NULL
, 0);
199 static char *quote_two(const char *one
, const char *two
)
201 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
202 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
205 if (need_one
+ need_two
) {
206 if (!need_one
) need_one
= strlen(one
);
207 if (!need_two
) need_one
= strlen(two
);
209 xp
= xmalloc(need_one
+ need_two
+ 3);
211 quote_c_style(one
, xp
+ 1, NULL
, 1);
212 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
213 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
216 need_one
= strlen(one
);
217 need_two
= strlen(two
);
218 xp
= xmalloc(need_one
+ need_two
+ 1);
220 strcpy(xp
+ need_one
, two
);
224 static const char *external_diff(void)
226 static const char *external_diff_cmd
= NULL
;
227 static int done_preparing
= 0;
230 return external_diff_cmd
;
231 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
233 return external_diff_cmd
;
236 static struct diff_tempfile
{
237 const char *name
; /* filename external diff should read from */
240 char tmp_path
[PATH_MAX
];
243 static int count_lines(const char *data
, int size
)
245 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
252 completely_empty
= 0;
256 completely_empty
= 0;
259 if (completely_empty
)
262 count
++; /* no trailing newline */
266 static void print_line_count(int count
)
276 printf("1,%d", count
);
281 static void copy_file(int prefix
, const char *data
, int size
,
282 const char *set
, const char *reset
)
284 int ch
, nl_just_seen
= 1;
293 fputs(reset
, stdout
);
299 printf("%s\n\\ No newline at end of file\n", reset
);
302 static void emit_rewrite_diff(const char *name_a
,
304 struct diff_filespec
*one
,
305 struct diff_filespec
*two
,
309 const char *name_a_tab
, *name_b_tab
;
310 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
311 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
312 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
313 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
314 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
316 name_a
+= (*name_a
== '/');
317 name_b
+= (*name_b
== '/');
318 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
319 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
321 diff_populate_filespec(one
, 0);
322 diff_populate_filespec(two
, 0);
323 lc_a
= count_lines(one
->data
, one
->size
);
324 lc_b
= count_lines(two
->data
, two
->size
);
325 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
326 metainfo
, name_a
, name_a_tab
, reset
,
327 metainfo
, name_b
, name_b_tab
, reset
, fraginfo
);
328 print_line_count(lc_a
);
330 print_line_count(lc_b
);
331 printf(" @@%s\n", reset
);
333 copy_file('-', one
->data
, one
->size
, old
, reset
);
335 copy_file('+', two
->data
, two
->size
, new, reset
);
338 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
340 if (!DIFF_FILE_VALID(one
)) {
341 mf
->ptr
= (char *)""; /* does not matter */
345 else if (diff_populate_filespec(one
, 0))
348 mf
->size
= one
->size
;
352 struct diff_words_buffer
{
355 long current
; /* output pointer */
356 int suppressed_newline
;
359 static void diff_words_append(char *line
, unsigned long len
,
360 struct diff_words_buffer
*buffer
)
362 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
363 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
364 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
368 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
369 buffer
->text
.size
+= len
;
372 struct diff_words_data
{
373 struct xdiff_emit_state xm
;
374 struct diff_words_buffer minus
, plus
;
377 static void print_word(struct diff_words_buffer
*buffer
, int len
, int color
,
378 int suppress_newline
)
386 ptr
= buffer
->text
.ptr
+ buffer
->current
;
387 buffer
->current
+= len
;
389 if (ptr
[len
- 1] == '\n') {
394 fputs(diff_get_color(1, color
), stdout
);
395 fwrite(ptr
, len
, 1, stdout
);
396 fputs(diff_get_color(1, DIFF_RESET
), stdout
);
399 if (suppress_newline
)
400 buffer
->suppressed_newline
= 1;
406 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
408 struct diff_words_data
*diff_words
= priv
;
410 if (diff_words
->minus
.suppressed_newline
) {
413 diff_words
->minus
.suppressed_newline
= 0;
419 print_word(&diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
422 print_word(&diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
425 print_word(&diff_words
->plus
, len
, DIFF_PLAIN
, 0);
426 diff_words
->minus
.current
+= len
;
431 /* this executes the word diff on the accumulated buffers */
432 static void diff_words_show(struct diff_words_data
*diff_words
)
437 mmfile_t minus
, plus
;
440 memset(&xecfg
, 0, sizeof(xecfg
));
441 minus
.size
= diff_words
->minus
.text
.size
;
442 minus
.ptr
= xmalloc(minus
.size
);
443 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
444 for (i
= 0; i
< minus
.size
; i
++)
445 if (isspace(minus
.ptr
[i
]))
447 diff_words
->minus
.current
= 0;
449 plus
.size
= diff_words
->plus
.text
.size
;
450 plus
.ptr
= xmalloc(plus
.size
);
451 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
452 for (i
= 0; i
< plus
.size
; i
++)
453 if (isspace(plus
.ptr
[i
]))
455 diff_words
->plus
.current
= 0;
457 xpp
.flags
= XDF_NEED_MINIMAL
;
458 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
459 ecb
.outf
= xdiff_outf
;
460 ecb
.priv
= diff_words
;
461 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
462 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
466 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
468 if (diff_words
->minus
.suppressed_newline
) {
470 diff_words
->minus
.suppressed_newline
= 0;
474 struct emit_callback
{
475 struct xdiff_emit_state xm
;
476 int nparents
, color_diff
;
477 const char **label_path
;
478 struct diff_words_data
*diff_words
;
482 static void free_diff_words_data(struct emit_callback
*ecbdata
)
484 if (ecbdata
->diff_words
) {
486 if (ecbdata
->diff_words
->minus
.text
.size
||
487 ecbdata
->diff_words
->plus
.text
.size
)
488 diff_words_show(ecbdata
->diff_words
);
490 if (ecbdata
->diff_words
->minus
.text
.ptr
)
491 free (ecbdata
->diff_words
->minus
.text
.ptr
);
492 if (ecbdata
->diff_words
->plus
.text
.ptr
)
493 free (ecbdata
->diff_words
->plus
.text
.ptr
);
494 free(ecbdata
->diff_words
);
495 ecbdata
->diff_words
= NULL
;
499 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
502 return diff_colors
[ix
];
506 static void emit_line(const char *set
, const char *reset
, const char *line
, int len
)
508 if (len
> 0 && line
[len
-1] == '\n')
511 fwrite(line
, len
, 1, stdout
);
515 static void emit_line_with_ws(int nparents
,
516 const char *set
, const char *reset
, const char *ws
,
517 const char *line
, int len
)
520 int last_tab_in_indent
= -1;
521 int last_space_in_indent
= -1;
524 int need_highlight_leading_space
= 0;
525 /* The line is a newly added line. Does it have funny leading
526 * whitespaces? In indent, SP should never precede a TAB.
528 for (i
= col0
; i
< len
; i
++) {
529 if (line
[i
] == '\t') {
530 last_tab_in_indent
= i
;
531 if (0 <= last_space_in_indent
)
532 need_highlight_leading_space
= 1;
534 else if (line
[i
] == ' ')
535 last_space_in_indent
= i
;
540 fwrite(line
, col0
, 1, stdout
);
541 fputs(reset
, stdout
);
542 if (((i
== len
) || line
[i
] == '\n') && i
!= col0
) {
543 /* The whole line was indent */
544 emit_line(ws
, reset
, line
+ col0
, len
- col0
);
548 if (need_highlight_leading_space
) {
549 while (i
< last_tab_in_indent
) {
550 if (line
[i
] == ' ') {
553 fputs(reset
, stdout
);
561 if (line
[tail
] == '\n' && i
< tail
)
564 if (!isspace(line
[tail
]))
568 if ((i
< tail
&& line
[tail
+ 1] != '\n')) {
569 /* This has whitespace between tail+1..len */
571 fwrite(line
+ i
, tail
- i
+ 1, 1, stdout
);
572 fputs(reset
, stdout
);
573 emit_line(ws
, reset
, line
+ tail
+ 1, len
- tail
- 1);
576 emit_line(set
, reset
, line
+ i
, len
- i
);
579 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
581 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
582 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
585 emit_line(set
, reset
, line
, len
);
587 emit_line_with_ws(ecbdata
->nparents
, set
, reset
, ws
,
591 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
595 struct emit_callback
*ecbdata
= priv
;
596 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
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 printf("%s--- %s%s%s\n",
608 set
, ecbdata
->label_path
[0], reset
, name_a_tab
);
609 printf("%s+++ %s%s%s\n",
610 set
, ecbdata
->label_path
[1], reset
, name_b_tab
);
611 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
614 /* This is not really necessary for now because
615 * this codepath only deals with two-way diffs.
617 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
619 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
620 ecbdata
->nparents
= i
- 1;
621 emit_line(diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
626 if (len
< ecbdata
->nparents
) {
628 emit_line(reset
, reset
, line
, len
);
633 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
634 /* fall back to normal diff */
635 free_diff_words_data(ecbdata
);
636 if (ecbdata
->diff_words
) {
637 if (line
[0] == '-') {
638 diff_words_append(line
, len
,
639 &ecbdata
->diff_words
->minus
);
641 } else if (line
[0] == '+') {
642 diff_words_append(line
, len
,
643 &ecbdata
->diff_words
->plus
);
646 if (ecbdata
->diff_words
->minus
.text
.size
||
647 ecbdata
->diff_words
->plus
.text
.size
)
648 diff_words_show(ecbdata
->diff_words
);
651 emit_line(set
, reset
, line
, len
);
654 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
656 color
= DIFF_FILE_OLD
;
657 else if (line
[i
] == '+')
658 color
= DIFF_FILE_NEW
;
661 if (color
!= DIFF_FILE_NEW
) {
662 emit_line(diff_get_color(ecbdata
->color_diff
, color
),
666 emit_add_line(reset
, ecbdata
, line
, len
);
669 static char *pprint_rename(const char *a
, const char *b
)
674 int pfx_length
, sfx_length
;
675 int len_a
= strlen(a
);
676 int len_b
= strlen(b
);
677 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
678 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
680 if (qlen_a
|| qlen_b
) {
681 if (qlen_a
) len_a
= qlen_a
;
682 if (qlen_b
) len_b
= qlen_b
;
683 name
= xmalloc( len_a
+ len_b
+ 5 );
685 quote_c_style(a
, name
, NULL
, 0);
687 memcpy(name
, a
, len_a
);
688 memcpy(name
+ len_a
, " => ", 4);
690 quote_c_style(b
, name
+ len_a
+ 4, NULL
, 0);
692 memcpy(name
+ len_a
+ 4, b
, len_b
+ 1);
696 /* Find common prefix */
698 while (*old
&& *new && *old
== *new) {
700 pfx_length
= old
- a
+ 1;
705 /* Find common suffix */
709 while (a
<= old
&& b
<= new && *old
== *new) {
711 sfx_length
= len_a
- (old
- a
);
717 * pfx{mid-a => mid-b}sfx
718 * {pfx-a => pfx-b}sfx
719 * pfx{sfx-a => sfx-b}
722 if (pfx_length
+ sfx_length
) {
723 int a_midlen
= len_a
- pfx_length
- sfx_length
;
724 int b_midlen
= len_b
- pfx_length
- sfx_length
;
725 if (a_midlen
< 0) a_midlen
= 0;
726 if (b_midlen
< 0) b_midlen
= 0;
728 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
729 sprintf(name
, "%.*s{%.*s => %.*s}%s",
731 a_midlen
, a
+ pfx_length
,
732 b_midlen
, b
+ pfx_length
,
733 a
+ len_a
- sfx_length
);
736 name
= xmalloc(len_a
+ len_b
+ 5);
737 sprintf(name
, "%s => %s", a
, b
);
743 struct xdiff_emit_state xm
;
747 struct diffstat_file
{
749 unsigned is_unmerged
:1;
750 unsigned is_binary
:1;
751 unsigned is_renamed
:1;
752 unsigned int added
, deleted
;
756 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
760 struct diffstat_file
*x
;
761 x
= xcalloc(sizeof (*x
), 1);
762 if (diffstat
->nr
== diffstat
->alloc
) {
763 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
764 diffstat
->files
= xrealloc(diffstat
->files
,
765 diffstat
->alloc
* sizeof(x
));
767 diffstat
->files
[diffstat
->nr
++] = x
;
769 x
->name
= pprint_rename(name_a
, name_b
);
773 x
->name
= xstrdup(name_a
);
777 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
779 struct diffstat_t
*diffstat
= priv
;
780 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
784 else if (line
[0] == '-')
788 const char mime_boundary_leader
[] = "------------";
790 static int scale_linear(int it
, int width
, int max_change
)
793 * make sure that at least one '-' is printed if there were deletions,
794 * and likewise for '+'.
798 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
801 static void show_name(const char *prefix
, const char *name
, int len
,
802 const char *reset
, const char *set
)
804 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
807 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
817 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
819 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
820 int max_change
= 0, max_len
= 0;
821 int total_files
= data
->nr
;
822 int width
, name_width
;
823 const char *reset
, *set
, *add_c
, *del_c
;
828 width
= options
->stat_width
? options
->stat_width
: 80;
829 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
831 /* Sanity: give at least 5 columns to the graph,
832 * but leave at least 10 columns for the name.
834 if (width
< name_width
+ 15) {
835 if (name_width
<= 25)
836 width
= name_width
+ 15;
838 name_width
= width
- 15;
841 /* Find the longest filename and max number of changes */
842 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
843 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
844 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
845 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
847 for (i
= 0; i
< data
->nr
; i
++) {
848 struct diffstat_file
*file
= data
->files
[i
];
849 int change
= file
->added
+ file
->deleted
;
851 if (!file
->is_renamed
) { /* renames are already quoted by pprint_rename */
852 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
854 char *qname
= xmalloc(len
+ 1);
855 quote_c_style(file
->name
, qname
, NULL
, 0);
861 len
= strlen(file
->name
);
865 if (file
->is_binary
|| file
->is_unmerged
)
867 if (max_change
< change
)
871 /* Compute the width of the graph part;
872 * 10 is for one blank at the beginning of the line plus
873 * " | count " between the name and the graph.
875 * From here on, name_width is the width of the name area,
876 * and width is the width of the graph area.
878 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
879 if (width
< (name_width
+ 10) + max_change
)
880 width
= width
- (name_width
+ 10);
884 for (i
= 0; i
< data
->nr
; i
++) {
885 const char *prefix
= "";
886 char *name
= data
->files
[i
]->name
;
887 int added
= data
->files
[i
]->added
;
888 int deleted
= data
->files
[i
]->deleted
;
892 * "scale" the filename
895 name_len
= strlen(name
);
896 if (name_width
< name_len
) {
900 name
+= name_len
- len
;
901 slash
= strchr(name
, '/');
906 if (data
->files
[i
]->is_binary
) {
907 show_name(prefix
, name
, len
, reset
, set
);
909 printf("%s%d%s", del_c
, deleted
, reset
);
911 printf("%s%d%s", add_c
, added
, reset
);
914 goto free_diffstat_file
;
916 else if (data
->files
[i
]->is_unmerged
) {
917 show_name(prefix
, name
, len
, reset
, set
);
918 printf(" Unmerged\n");
919 goto free_diffstat_file
;
921 else if (!data
->files
[i
]->is_renamed
&&
922 (added
+ deleted
== 0)) {
924 goto free_diffstat_file
;
928 * scale the add/delete
936 if (width
<= max_change
) {
937 add
= scale_linear(add
, width
, max_change
);
938 del
= scale_linear(del
, width
, max_change
);
941 show_name(prefix
, name
, len
, reset
, set
);
942 printf("%5d ", added
+ deleted
);
943 show_graph('+', add
, add_c
, reset
);
944 show_graph('-', del
, del_c
, reset
);
947 free(data
->files
[i
]->name
);
948 free(data
->files
[i
]);
951 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
952 set
, total_files
, adds
, dels
, reset
);
955 static void show_shortstats(struct diffstat_t
* data
)
957 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
962 for (i
= 0; i
< data
->nr
; i
++) {
963 if (!data
->files
[i
]->is_binary
&&
964 !data
->files
[i
]->is_unmerged
) {
965 int added
= data
->files
[i
]->added
;
966 int deleted
= data
->files
[i
]->deleted
;
967 if (!data
->files
[i
]->is_renamed
&&
968 (added
+ deleted
== 0)) {
975 free(data
->files
[i
]->name
);
976 free(data
->files
[i
]);
980 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
981 total_files
, adds
, dels
);
984 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
988 for (i
= 0; i
< data
->nr
; i
++) {
989 struct diffstat_file
*file
= data
->files
[i
];
994 printf("%d\t%d\t", file
->added
, file
->deleted
);
995 if (options
->line_termination
&& !file
->is_renamed
&&
996 quote_c_style(file
->name
, NULL
, NULL
, 0))
997 quote_c_style(file
->name
, NULL
, stdout
, 0);
999 fputs(file
->name
, stdout
);
1000 putchar(options
->line_termination
);
1004 struct checkdiff_t
{
1005 struct xdiff_emit_state xm
;
1006 const char *filename
;
1007 int lineno
, color_diff
;
1010 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1012 struct checkdiff_t
*data
= priv
;
1013 const char *ws
= diff_get_color(data
->color_diff
, DIFF_WHITESPACE
);
1014 const char *reset
= diff_get_color(data
->color_diff
, DIFF_RESET
);
1015 const char *set
= diff_get_color(data
->color_diff
, DIFF_FILE_NEW
);
1017 if (line
[0] == '+') {
1018 int i
, spaces
= 0, space_before_tab
= 0, white_space_at_end
= 0;
1020 /* check space before tab */
1021 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
1024 if (line
[i
- 1] == '\t' && spaces
)
1025 space_before_tab
= 1;
1027 /* check white space at line end */
1028 if (line
[len
- 1] == '\n')
1030 if (isspace(line
[len
- 1]))
1031 white_space_at_end
= 1;
1033 if (space_before_tab
|| white_space_at_end
) {
1034 printf("%s:%d: %s", data
->filename
, data
->lineno
, ws
);
1035 if (space_before_tab
) {
1036 printf("space before tab");
1037 if (white_space_at_end
)
1040 if (white_space_at_end
)
1041 printf("white space at end");
1042 printf(":%s ", reset
);
1043 emit_line_with_ws(1, set
, reset
, ws
, line
, len
);
1047 } else if (line
[0] == ' ')
1049 else if (line
[0] == '@') {
1050 char *plus
= strchr(line
, '+');
1052 data
->lineno
= strtol(plus
, NULL
, 10);
1054 die("invalid diff");
1058 static unsigned char *deflate_it(char *data
,
1060 unsigned long *result_size
)
1063 unsigned char *deflated
;
1066 memset(&stream
, 0, sizeof(stream
));
1067 deflateInit(&stream
, zlib_compression_level
);
1068 bound
= deflateBound(&stream
, size
);
1069 deflated
= xmalloc(bound
);
1070 stream
.next_out
= deflated
;
1071 stream
.avail_out
= bound
;
1073 stream
.next_in
= (unsigned char *)data
;
1074 stream
.avail_in
= size
;
1075 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1077 deflateEnd(&stream
);
1078 *result_size
= stream
.total_out
;
1082 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
1088 unsigned long orig_size
;
1089 unsigned long delta_size
;
1090 unsigned long deflate_size
;
1091 unsigned long data_size
;
1093 /* We could do deflated delta, or we could do just deflated two,
1094 * whichever is smaller.
1097 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1098 if (one
->size
&& two
->size
) {
1099 delta
= diff_delta(one
->ptr
, one
->size
,
1100 two
->ptr
, two
->size
,
1101 &delta_size
, deflate_size
);
1103 void *to_free
= delta
;
1104 orig_size
= delta_size
;
1105 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1110 if (delta
&& delta_size
< deflate_size
) {
1111 printf("delta %lu\n", orig_size
);
1114 data_size
= delta_size
;
1117 printf("literal %lu\n", two
->size
);
1120 data_size
= deflate_size
;
1123 /* emit data encoded in base85 */
1126 int bytes
= (52 < data_size
) ? 52 : data_size
;
1130 line
[0] = bytes
+ 'A' - 1;
1132 line
[0] = bytes
- 26 + 'a' - 1;
1133 encode_85(line
+ 1, cp
, bytes
);
1134 cp
= (char *) cp
+ bytes
;
1141 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
1143 printf("GIT binary patch\n");
1144 emit_binary_diff_body(one
, two
);
1145 emit_binary_diff_body(two
, one
);
1148 static void setup_diff_attr_check(struct git_attr_check
*check
)
1150 static struct git_attr
*attr_diff
;
1153 attr_diff
= git_attr("diff", 4);
1155 check
[0].attr
= attr_diff
;
1158 static void diff_filespec_check_attr(struct diff_filespec
*one
)
1160 struct git_attr_check attr_diff_check
;
1161 int check_from_data
= 0;
1163 if (one
->checked_attr
)
1166 setup_diff_attr_check(&attr_diff_check
);
1168 one
->funcname_pattern_ident
= NULL
;
1170 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1174 value
= attr_diff_check
.value
;
1175 if (ATTR_TRUE(value
))
1177 else if (ATTR_FALSE(value
))
1180 check_from_data
= 1;
1182 /* funcname pattern ident */
1183 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1186 one
->funcname_pattern_ident
= value
;
1189 if (check_from_data
) {
1190 if (!one
->data
&& DIFF_FILE_VALID(one
))
1191 diff_populate_filespec(one
, 0);
1194 one
->is_binary
= buffer_is_binary(one
->data
, one
->size
);
1198 int diff_filespec_is_binary(struct diff_filespec
*one
)
1200 diff_filespec_check_attr(one
);
1201 return one
->is_binary
;
1204 static const char *funcname_pattern(const char *ident
)
1206 struct funcname_pattern
*pp
;
1208 read_config_if_needed();
1209 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
1210 if (!strcmp(ident
, pp
->name
))
1215 static struct builtin_funcname_pattern
{
1217 const char *pattern
;
1218 } builtin_funcname_pattern
[] = {
1219 { "java", "!^[ ]*\\(catch\\|do\\|for\\|if\\|instanceof\\|"
1220 "new\\|return\\|switch\\|throw\\|while\\)\n"
1222 "[A-Za-z_][A-Za-z_0-9]*\\)\\{2,\\}"
1224 { "tex", "^\\(\\\\\\(sub\\)*section{.*\\)$" },
1227 static const char *diff_funcname_pattern(struct diff_filespec
*one
)
1229 const char *ident
, *pattern
;
1232 diff_filespec_check_attr(one
);
1233 ident
= one
->funcname_pattern_ident
;
1237 * If the config file has "funcname.default" defined, that
1238 * regexp is used; otherwise NULL is returned and xemit uses
1239 * the built-in default.
1241 return funcname_pattern("default");
1243 /* Look up custom "funcname.$ident" regexp from config. */
1244 pattern
= funcname_pattern(ident
);
1249 * And define built-in fallback patterns here. Note that
1250 * these can be overriden by the user's config settings.
1252 for (i
= 0; i
< ARRAY_SIZE(builtin_funcname_pattern
); i
++)
1253 if (!strcmp(ident
, builtin_funcname_pattern
[i
].name
))
1254 return builtin_funcname_pattern
[i
].pattern
;
1259 static void builtin_diff(const char *name_a
,
1261 struct diff_filespec
*one
,
1262 struct diff_filespec
*two
,
1263 const char *xfrm_msg
,
1264 struct diff_options
*o
,
1265 int complete_rewrite
)
1269 char *a_one
, *b_two
;
1270 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
1271 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
1273 a_one
= quote_two("a/", name_a
+ (*name_a
== '/'));
1274 b_two
= quote_two("b/", name_b
+ (*name_b
== '/'));
1275 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1276 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1277 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1278 if (lbl
[0][0] == '/') {
1280 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1281 if (xfrm_msg
&& xfrm_msg
[0])
1282 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1284 else if (lbl
[1][0] == '/') {
1285 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1286 if (xfrm_msg
&& xfrm_msg
[0])
1287 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1290 if (one
->mode
!= two
->mode
) {
1291 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
1292 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
1294 if (xfrm_msg
&& xfrm_msg
[0])
1295 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1297 * we do not run diff between different kind
1300 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1301 goto free_ab_and_return
;
1302 if (complete_rewrite
) {
1303 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1305 o
->found_changes
= 1;
1306 goto free_ab_and_return
;
1310 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1311 die("unable to read files to diff");
1314 (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
))) {
1315 /* Quite common confusing case */
1316 if (mf1
.size
== mf2
.size
&&
1317 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1318 goto free_ab_and_return
;
1320 emit_binary_diff(&mf1
, &mf2
);
1322 printf("Binary files %s and %s differ\n",
1324 o
->found_changes
= 1;
1327 /* Crazy xdl interfaces.. */
1328 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1332 struct emit_callback ecbdata
;
1333 const char *funcname_pattern
;
1335 funcname_pattern
= diff_funcname_pattern(one
);
1336 if (!funcname_pattern
)
1337 funcname_pattern
= diff_funcname_pattern(two
);
1339 memset(&xecfg
, 0, sizeof(xecfg
));
1340 memset(&ecbdata
, 0, sizeof(ecbdata
));
1341 ecbdata
.label_path
= lbl
;
1342 ecbdata
.color_diff
= o
->color_diff
;
1343 ecbdata
.found_changesp
= &o
->found_changes
;
1344 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1345 xecfg
.ctxlen
= o
->context
;
1346 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1347 if (funcname_pattern
)
1348 xdiff_set_find_func(&xecfg
, funcname_pattern
);
1351 else if (!prefixcmp(diffopts
, "--unified="))
1352 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1353 else if (!prefixcmp(diffopts
, "-u"))
1354 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1355 ecb
.outf
= xdiff_outf
;
1356 ecb
.priv
= &ecbdata
;
1357 ecbdata
.xm
.consume
= fn_out_consume
;
1358 if (o
->color_diff_words
)
1359 ecbdata
.diff_words
=
1360 xcalloc(1, sizeof(struct diff_words_data
));
1361 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1362 if (o
->color_diff_words
)
1363 free_diff_words_data(&ecbdata
);
1367 diff_free_filespec_data(one
);
1368 diff_free_filespec_data(two
);
1374 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1375 struct diff_filespec
*one
,
1376 struct diff_filespec
*two
,
1377 struct diffstat_t
*diffstat
,
1378 struct diff_options
*o
,
1379 int complete_rewrite
)
1382 struct diffstat_file
*data
;
1384 data
= diffstat_add(diffstat
, name_a
, name_b
);
1387 data
->is_unmerged
= 1;
1390 if (complete_rewrite
) {
1391 diff_populate_filespec(one
, 0);
1392 diff_populate_filespec(two
, 0);
1393 data
->deleted
= count_lines(one
->data
, one
->size
);
1394 data
->added
= count_lines(two
->data
, two
->size
);
1395 goto free_and_return
;
1397 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1398 die("unable to read files to diff");
1400 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1401 data
->is_binary
= 1;
1402 data
->added
= mf2
.size
;
1403 data
->deleted
= mf1
.size
;
1405 /* Crazy xdl interfaces.. */
1410 memset(&xecfg
, 0, sizeof(xecfg
));
1411 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1412 ecb
.outf
= xdiff_outf
;
1413 ecb
.priv
= diffstat
;
1414 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1418 diff_free_filespec_data(one
);
1419 diff_free_filespec_data(two
);
1422 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1423 struct diff_filespec
*one
,
1424 struct diff_filespec
*two
, struct diff_options
*o
)
1427 struct checkdiff_t data
;
1432 memset(&data
, 0, sizeof(data
));
1433 data
.xm
.consume
= checkdiff_consume
;
1434 data
.filename
= name_b
? name_b
: name_a
;
1436 data
.color_diff
= o
->color_diff
;
1438 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1439 die("unable to read files to diff");
1441 if (diff_filespec_is_binary(two
))
1442 goto free_and_return
;
1444 /* Crazy xdl interfaces.. */
1449 memset(&xecfg
, 0, sizeof(xecfg
));
1450 xpp
.flags
= XDF_NEED_MINIMAL
;
1451 ecb
.outf
= xdiff_outf
;
1453 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1456 diff_free_filespec_data(one
);
1457 diff_free_filespec_data(two
);
1460 struct diff_filespec
*alloc_filespec(const char *path
)
1462 int namelen
= strlen(path
);
1463 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1465 memset(spec
, 0, sizeof(*spec
));
1466 spec
->path
= (char *)(spec
+ 1);
1467 memcpy(spec
->path
, path
, namelen
+1);
1471 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1472 unsigned short mode
)
1475 spec
->mode
= canon_mode(mode
);
1476 hashcpy(spec
->sha1
, sha1
);
1477 spec
->sha1_valid
= !is_null_sha1(sha1
);
1482 * Given a name and sha1 pair, if the index tells us the file in
1483 * the work tree has that object contents, return true, so that
1484 * prepare_temp_file() does not have to inflate and extract.
1486 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1488 struct cache_entry
*ce
;
1492 /* We do not read the cache ourselves here, because the
1493 * benchmark with my previous version that always reads cache
1494 * shows that it makes things worse for diff-tree comparing
1495 * two linux-2.6 kernel trees in an already checked out work
1496 * tree. This is because most diff-tree comparisons deal with
1497 * only a small number of files, while reading the cache is
1498 * expensive for a large project, and its cost outweighs the
1499 * savings we get by not inflating the object to a temporary
1500 * file. Practically, this code only helps when we are used
1501 * by diff-cache --cached, which does read the cache before
1507 /* We want to avoid the working directory if our caller
1508 * doesn't need the data in a normal file, this system
1509 * is rather slow with its stat/open/mmap/close syscalls,
1510 * and the object is contained in a pack file. The pack
1511 * is probably already open and will be faster to obtain
1512 * the data through than the working directory. Loose
1513 * objects however would tend to be slower as they need
1514 * to be individually opened and inflated.
1516 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1520 pos
= cache_name_pos(name
, len
);
1523 ce
= active_cache
[pos
];
1524 if ((lstat(name
, &st
) < 0) ||
1525 !S_ISREG(st
.st_mode
) || /* careful! */
1526 ce_match_stat(ce
, &st
, 0) ||
1527 hashcmp(sha1
, ce
->sha1
))
1529 /* we return 1 only when we can stat, it is a regular file,
1530 * stat information matches, and sha1 recorded in the cache
1531 * matches. I.e. we know the file in the work tree really is
1532 * the same as the <name, sha1> pair.
1537 static int populate_from_stdin(struct diff_filespec
*s
)
1541 strbuf_init(&buf
, 0);
1542 if (strbuf_read(&buf
, 0, 0) < 0)
1543 return error("error while reading from stdin %s",
1546 s
->should_munmap
= 0;
1548 s
->data
= strbuf_detach(&buf
);
1553 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1556 char *data
= xmalloc(100);
1557 len
= snprintf(data
, 100,
1558 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1570 * While doing rename detection and pickaxe operation, we may need to
1571 * grab the data for the blob (or file) for our own in-core comparison.
1572 * diff_filespec has data and size fields for this purpose.
1574 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1577 if (!DIFF_FILE_VALID(s
))
1578 die("internal error: asking to populate invalid file.");
1579 if (S_ISDIR(s
->mode
))
1585 if (size_only
&& 0 < s
->size
)
1588 if (S_ISGITLINK(s
->mode
))
1589 return diff_populate_gitlink(s
, size_only
);
1591 if (!s
->sha1_valid
||
1592 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1597 if (!strcmp(s
->path
, "-"))
1598 return populate_from_stdin(s
);
1600 if (lstat(s
->path
, &st
) < 0) {
1601 if (errno
== ENOENT
) {
1605 s
->data
= (char *)"";
1610 s
->size
= xsize_t(st
.st_size
);
1615 if (S_ISLNK(st
.st_mode
)) {
1617 s
->data
= xmalloc(s
->size
);
1619 ret
= readlink(s
->path
, s
->data
, s
->size
);
1626 fd
= open(s
->path
, O_RDONLY
);
1629 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1631 s
->should_munmap
= 1;
1634 * Convert from working tree format to canonical git format
1636 strbuf_init(&buf
, 0);
1637 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
)) {
1638 munmap(s
->data
, s
->size
);
1639 s
->should_munmap
= 0;
1646 enum object_type type
;
1648 type
= sha1_object_info(s
->sha1
, &s
->size
);
1650 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1657 void diff_free_filespec_data(struct diff_filespec
*s
)
1661 else if (s
->should_munmap
)
1662 munmap(s
->data
, s
->size
);
1664 if (s
->should_free
|| s
->should_munmap
) {
1665 s
->should_free
= s
->should_munmap
= 0;
1672 static void prep_temp_blob(struct diff_tempfile
*temp
,
1675 const unsigned char *sha1
,
1680 fd
= git_mkstemp(temp
->tmp_path
, PATH_MAX
, ".diff_XXXXXX");
1682 die("unable to create temp-file: %s", strerror(errno
));
1683 if (write_in_full(fd
, blob
, size
) != size
)
1684 die("unable to write temp-file");
1686 temp
->name
= temp
->tmp_path
;
1687 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1689 sprintf(temp
->mode
, "%06o", mode
);
1692 static void prepare_temp_file(const char *name
,
1693 struct diff_tempfile
*temp
,
1694 struct diff_filespec
*one
)
1696 if (!DIFF_FILE_VALID(one
)) {
1698 /* A '-' entry produces this for file-2, and
1699 * a '+' entry produces this for file-1.
1701 temp
->name
= "/dev/null";
1702 strcpy(temp
->hex
, ".");
1703 strcpy(temp
->mode
, ".");
1707 if (!one
->sha1_valid
||
1708 reuse_worktree_file(name
, one
->sha1
, 1)) {
1710 if (lstat(name
, &st
) < 0) {
1711 if (errno
== ENOENT
)
1712 goto not_a_valid_file
;
1713 die("stat(%s): %s", name
, strerror(errno
));
1715 if (S_ISLNK(st
.st_mode
)) {
1717 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1718 size_t sz
= xsize_t(st
.st_size
);
1719 if (sizeof(buf
) <= st
.st_size
)
1720 die("symlink too long: %s", name
);
1721 ret
= readlink(name
, buf
, sz
);
1723 die("readlink(%s)", name
);
1724 prep_temp_blob(temp
, buf
, sz
,
1726 one
->sha1
: null_sha1
),
1728 one
->mode
: S_IFLNK
));
1731 /* we can borrow from the file in the work tree */
1733 if (!one
->sha1_valid
)
1734 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1736 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1737 /* Even though we may sometimes borrow the
1738 * contents from the work tree, we always want
1739 * one->mode. mode is trustworthy even when
1740 * !(one->sha1_valid), as long as
1741 * DIFF_FILE_VALID(one).
1743 sprintf(temp
->mode
, "%06o", one
->mode
);
1748 if (diff_populate_filespec(one
, 0))
1749 die("cannot read data blob for %s", one
->path
);
1750 prep_temp_blob(temp
, one
->data
, one
->size
,
1751 one
->sha1
, one
->mode
);
1755 static void remove_tempfile(void)
1759 for (i
= 0; i
< 2; i
++)
1760 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1761 unlink(diff_temp
[i
].name
);
1762 diff_temp
[i
].name
= NULL
;
1766 static void remove_tempfile_on_signal(int signo
)
1769 signal(SIGINT
, SIG_DFL
);
1773 static int spawn_prog(const char *pgm
, const char **arg
)
1781 die("unable to fork");
1783 execvp(pgm
, (char *const*) arg
);
1787 while (waitpid(pid
, &status
, 0) < 0) {
1793 /* Earlier we did not check the exit status because
1794 * diff exits non-zero if files are different, and
1795 * we are not interested in knowing that. It was a
1796 * mistake which made it harder to quit a diff-*
1797 * session that uses the git-apply-patch-script as
1798 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1799 * should also exit non-zero only when it wants to
1800 * abort the entire diff-* session.
1802 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1807 /* An external diff command takes:
1809 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1810 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1813 static void run_external_diff(const char *pgm
,
1816 struct diff_filespec
*one
,
1817 struct diff_filespec
*two
,
1818 const char *xfrm_msg
,
1819 int complete_rewrite
)
1821 const char *spawn_arg
[10];
1822 struct diff_tempfile
*temp
= diff_temp
;
1824 static int atexit_asked
= 0;
1825 const char *othername
;
1826 const char **arg
= &spawn_arg
[0];
1828 othername
= (other
? other
: name
);
1830 prepare_temp_file(name
, &temp
[0], one
);
1831 prepare_temp_file(othername
, &temp
[1], two
);
1832 if (! atexit_asked
&&
1833 (temp
[0].name
== temp
[0].tmp_path
||
1834 temp
[1].name
== temp
[1].tmp_path
)) {
1836 atexit(remove_tempfile
);
1838 signal(SIGINT
, remove_tempfile_on_signal
);
1844 *arg
++ = temp
[0].name
;
1845 *arg
++ = temp
[0].hex
;
1846 *arg
++ = temp
[0].mode
;
1847 *arg
++ = temp
[1].name
;
1848 *arg
++ = temp
[1].hex
;
1849 *arg
++ = temp
[1].mode
;
1859 retval
= spawn_prog(pgm
, spawn_arg
);
1862 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1867 static const char *external_diff_attr(const char *name
)
1869 struct git_attr_check attr_diff_check
;
1871 setup_diff_attr_check(&attr_diff_check
);
1872 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
1873 const char *value
= attr_diff_check
.value
;
1874 if (!ATTR_TRUE(value
) &&
1875 !ATTR_FALSE(value
) &&
1876 !ATTR_UNSET(value
)) {
1877 struct ll_diff_driver
*drv
;
1879 read_config_if_needed();
1880 for (drv
= user_diff
; drv
; drv
= drv
->next
)
1881 if (!strcmp(drv
->name
, value
))
1888 static void run_diff_cmd(const char *pgm
,
1891 struct diff_filespec
*one
,
1892 struct diff_filespec
*two
,
1893 const char *xfrm_msg
,
1894 struct diff_options
*o
,
1895 int complete_rewrite
)
1897 if (!o
->allow_external
)
1900 const char *cmd
= external_diff_attr(name
);
1906 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1911 builtin_diff(name
, other
? other
: name
,
1912 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1914 printf("* Unmerged path %s\n", name
);
1917 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1919 if (DIFF_FILE_VALID(one
)) {
1920 if (!one
->sha1_valid
) {
1922 if (!strcmp(one
->path
, "-")) {
1923 hashcpy(one
->sha1
, null_sha1
);
1926 if (lstat(one
->path
, &st
) < 0)
1927 die("stat %s", one
->path
);
1928 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1929 die("cannot hash %s\n", one
->path
);
1936 static int similarity_index(struct diff_filepair
*p
)
1938 return p
->score
* 100 / MAX_SCORE
;
1941 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1943 const char *pgm
= external_diff();
1944 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1945 struct diff_filespec
*one
;
1946 struct diff_filespec
*two
;
1949 char *name_munged
, *other_munged
;
1950 int complete_rewrite
= 0;
1953 if (DIFF_PAIR_UNMERGED(p
)) {
1955 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1959 name
= p
->one
->path
;
1960 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1961 name_munged
= quote_one(name
);
1962 other_munged
= quote_one(other
);
1963 one
= p
->one
; two
= p
->two
;
1965 diff_fill_sha1_info(one
);
1966 diff_fill_sha1_info(two
);
1969 switch (p
->status
) {
1970 case DIFF_STATUS_COPIED
:
1971 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1972 "similarity index %d%%\n"
1975 similarity_index(p
), name_munged
, other_munged
);
1977 case DIFF_STATUS_RENAMED
:
1978 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1979 "similarity index %d%%\n"
1982 similarity_index(p
), name_munged
, other_munged
);
1984 case DIFF_STATUS_MODIFIED
:
1986 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1987 "dissimilarity index %d%%\n",
1988 similarity_index(p
));
1989 complete_rewrite
= 1;
1998 if (hashcmp(one
->sha1
, two
->sha1
)) {
1999 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
2003 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2004 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2007 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2009 abbrev
, sha1_to_hex(one
->sha1
),
2010 abbrev
, sha1_to_hex(two
->sha1
));
2011 if (one
->mode
== two
->mode
)
2012 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2013 " %06o", one
->mode
);
2014 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
2019 xfrm_msg
= len
? msg
: NULL
;
2022 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2023 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2024 /* a filepair that changes between file and symlink
2025 * needs to be split into deletion and creation.
2027 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2028 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
2030 null
= alloc_filespec(one
->path
);
2031 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
2035 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
2042 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2043 struct diffstat_t
*diffstat
)
2047 int complete_rewrite
= 0;
2049 if (DIFF_PAIR_UNMERGED(p
)) {
2051 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2055 name
= p
->one
->path
;
2056 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2058 diff_fill_sha1_info(p
->one
);
2059 diff_fill_sha1_info(p
->two
);
2061 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2062 complete_rewrite
= 1;
2063 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2066 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2071 if (DIFF_PAIR_UNMERGED(p
)) {
2076 name
= p
->one
->path
;
2077 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2079 diff_fill_sha1_info(p
->one
);
2080 diff_fill_sha1_info(p
->two
);
2082 builtin_checkdiff(name
, other
, p
->one
, p
->two
, o
);
2085 void diff_setup(struct diff_options
*options
)
2087 memset(options
, 0, sizeof(*options
));
2088 options
->line_termination
= '\n';
2089 options
->break_opt
= -1;
2090 options
->rename_limit
= -1;
2091 options
->context
= 3;
2092 options
->msg_sep
= "";
2094 options
->change
= diff_change
;
2095 options
->add_remove
= diff_addremove
;
2096 options
->color_diff
= diff_use_color_default
;
2097 options
->detect_rename
= diff_detect_rename_default
;
2100 int diff_setup_done(struct diff_options
*options
)
2104 if (options
->output_format
& DIFF_FORMAT_NAME
)
2106 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2108 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2110 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2113 die("--name-only, --name-status, --check and -s are mutually exclusive");
2115 if (options
->find_copies_harder
)
2116 options
->detect_rename
= DIFF_DETECT_COPY
;
2118 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2119 DIFF_FORMAT_NAME_STATUS
|
2120 DIFF_FORMAT_CHECKDIFF
|
2121 DIFF_FORMAT_NO_OUTPUT
))
2122 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2123 DIFF_FORMAT_NUMSTAT
|
2124 DIFF_FORMAT_DIFFSTAT
|
2125 DIFF_FORMAT_SHORTSTAT
|
2126 DIFF_FORMAT_SUMMARY
|
2130 * These cases always need recursive; we do not drop caller-supplied
2131 * recursive bits for other formats here.
2133 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2134 DIFF_FORMAT_NUMSTAT
|
2135 DIFF_FORMAT_DIFFSTAT
|
2136 DIFF_FORMAT_SHORTSTAT
|
2137 DIFF_FORMAT_SUMMARY
|
2138 DIFF_FORMAT_CHECKDIFF
))
2139 options
->recursive
= 1;
2141 * Also pickaxe would not work very well if you do not say recursive
2143 if (options
->pickaxe
)
2144 options
->recursive
= 1;
2146 if (options
->detect_rename
&& options
->rename_limit
< 0)
2147 options
->rename_limit
= diff_rename_limit_default
;
2148 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2150 /* read-cache does not die even when it fails
2151 * so it is safe for us to do this here. Also
2152 * it does not smudge active_cache or active_nr
2153 * when it fails, so we do not have to worry about
2154 * cleaning it up ourselves either.
2158 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2159 options
->abbrev
= 40; /* full */
2162 * It does not make sense to show the first hit we happened
2163 * to have found. It does not make sense not to return with
2164 * exit code in such a case either.
2166 if (options
->quiet
) {
2167 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2168 options
->exit_with_status
= 1;
2172 * If we postprocess in diffcore, we cannot simply return
2173 * upon the first hit. We need to run diff as usual.
2175 if (options
->pickaxe
|| options
->filter
)
2181 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2191 if (c
== arg_short
) {
2195 if (val
&& isdigit(c
)) {
2197 int n
= strtoul(arg
, &end
, 10);
2208 eq
= strchr(arg
, '=');
2213 if (!len
|| strncmp(arg
, arg_long
, len
))
2218 if (!isdigit(*++eq
))
2220 n
= strtoul(eq
, &end
, 10);
2228 static int diff_scoreopt_parse(const char *opt
);
2230 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2232 const char *arg
= av
[0];
2233 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2234 options
->output_format
|= DIFF_FORMAT_PATCH
;
2235 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2236 options
->output_format
|= DIFF_FORMAT_PATCH
;
2237 else if (!strcmp(arg
, "--raw"))
2238 options
->output_format
|= DIFF_FORMAT_RAW
;
2239 else if (!strcmp(arg
, "--patch-with-raw")) {
2240 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2242 else if (!strcmp(arg
, "--numstat")) {
2243 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2245 else if (!strcmp(arg
, "--shortstat")) {
2246 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2248 else if (!prefixcmp(arg
, "--stat")) {
2250 int width
= options
->stat_width
;
2251 int name_width
= options
->stat_name_width
;
2257 if (!prefixcmp(arg
, "-width="))
2258 width
= strtoul(arg
+ 7, &end
, 10);
2259 else if (!prefixcmp(arg
, "-name-width="))
2260 name_width
= strtoul(arg
+ 12, &end
, 10);
2263 width
= strtoul(arg
+1, &end
, 10);
2265 name_width
= strtoul(end
+1, &end
, 10);
2268 /* Important! This checks all the error cases! */
2271 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2272 options
->stat_name_width
= name_width
;
2273 options
->stat_width
= width
;
2275 else if (!strcmp(arg
, "--check"))
2276 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2277 else if (!strcmp(arg
, "--summary"))
2278 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2279 else if (!strcmp(arg
, "--patch-with-stat")) {
2280 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2282 else if (!strcmp(arg
, "-z"))
2283 options
->line_termination
= 0;
2284 else if (!prefixcmp(arg
, "-l"))
2285 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2286 else if (!strcmp(arg
, "--full-index"))
2287 options
->full_index
= 1;
2288 else if (!strcmp(arg
, "--binary")) {
2289 options
->output_format
|= DIFF_FORMAT_PATCH
;
2290 options
->binary
= 1;
2292 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
2295 else if (!strcmp(arg
, "--name-only"))
2296 options
->output_format
|= DIFF_FORMAT_NAME
;
2297 else if (!strcmp(arg
, "--name-status"))
2298 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2299 else if (!strcmp(arg
, "-R"))
2300 options
->reverse_diff
= 1;
2301 else if (!prefixcmp(arg
, "-S"))
2302 options
->pickaxe
= arg
+ 2;
2303 else if (!strcmp(arg
, "-s")) {
2304 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2306 else if (!prefixcmp(arg
, "-O"))
2307 options
->orderfile
= arg
+ 2;
2308 else if (!prefixcmp(arg
, "--diff-filter="))
2309 options
->filter
= arg
+ 14;
2310 else if (!strcmp(arg
, "--pickaxe-all"))
2311 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2312 else if (!strcmp(arg
, "--pickaxe-regex"))
2313 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2314 else if (!prefixcmp(arg
, "-B")) {
2315 if ((options
->break_opt
=
2316 diff_scoreopt_parse(arg
)) == -1)
2319 else if (!prefixcmp(arg
, "-M")) {
2320 if ((options
->rename_score
=
2321 diff_scoreopt_parse(arg
)) == -1)
2323 options
->detect_rename
= DIFF_DETECT_RENAME
;
2325 else if (!prefixcmp(arg
, "-C")) {
2326 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2327 options
->find_copies_harder
= 1;
2328 if ((options
->rename_score
=
2329 diff_scoreopt_parse(arg
)) == -1)
2331 options
->detect_rename
= DIFF_DETECT_COPY
;
2333 else if (!strcmp(arg
, "--find-copies-harder"))
2334 options
->find_copies_harder
= 1;
2335 else if (!strcmp(arg
, "--follow"))
2336 options
->follow_renames
= 1;
2337 else if (!strcmp(arg
, "--abbrev"))
2338 options
->abbrev
= DEFAULT_ABBREV
;
2339 else if (!prefixcmp(arg
, "--abbrev=")) {
2340 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2341 if (options
->abbrev
< MINIMUM_ABBREV
)
2342 options
->abbrev
= MINIMUM_ABBREV
;
2343 else if (40 < options
->abbrev
)
2344 options
->abbrev
= 40;
2346 else if (!strcmp(arg
, "--color"))
2347 options
->color_diff
= 1;
2348 else if (!strcmp(arg
, "--no-color"))
2349 options
->color_diff
= 0;
2350 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2351 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2352 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2353 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2354 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2355 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2356 else if (!strcmp(arg
, "--color-words"))
2357 options
->color_diff
= options
->color_diff_words
= 1;
2358 else if (!strcmp(arg
, "--no-renames"))
2359 options
->detect_rename
= 0;
2360 else if (!strcmp(arg
, "--exit-code"))
2361 options
->exit_with_status
= 1;
2362 else if (!strcmp(arg
, "--quiet"))
2364 else if (!strcmp(arg
, "--ext-diff"))
2365 options
->allow_external
= 1;
2366 else if (!strcmp(arg
, "--no-ext-diff"))
2367 options
->allow_external
= 0;
2373 static int parse_num(const char **cp_p
)
2375 unsigned long num
, scale
;
2377 const char *cp
= *cp_p
;
2384 if ( !dot
&& ch
== '.' ) {
2387 } else if ( ch
== '%' ) {
2388 scale
= dot
? scale
*100 : 100;
2389 cp
++; /* % is always at the end */
2391 } else if ( ch
>= '0' && ch
<= '9' ) {
2392 if ( scale
< 100000 ) {
2394 num
= (num
*10) + (ch
-'0');
2403 /* user says num divided by scale and we say internally that
2404 * is MAX_SCORE * num / scale.
2406 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2409 static int diff_scoreopt_parse(const char *opt
)
2411 int opt1
, opt2
, cmd
;
2416 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2417 return -1; /* that is not a -M, -C nor -B option */
2419 opt1
= parse_num(&opt
);
2425 else if (*opt
!= '/')
2426 return -1; /* we expect -B80/99 or -B80 */
2429 opt2
= parse_num(&opt
);
2434 return opt1
| (opt2
<< 16);
2437 struct diff_queue_struct diff_queued_diff
;
2439 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2441 if (queue
->alloc
<= queue
->nr
) {
2442 queue
->alloc
= alloc_nr(queue
->alloc
);
2443 queue
->queue
= xrealloc(queue
->queue
,
2444 sizeof(dp
) * queue
->alloc
);
2446 queue
->queue
[queue
->nr
++] = dp
;
2449 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2450 struct diff_filespec
*one
,
2451 struct diff_filespec
*two
)
2453 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2461 void diff_free_filepair(struct diff_filepair
*p
)
2463 diff_free_filespec_data(p
->one
);
2464 diff_free_filespec_data(p
->two
);
2470 /* This is different from find_unique_abbrev() in that
2471 * it stuffs the result with dots for alignment.
2473 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2478 return sha1_to_hex(sha1
);
2480 abbrev
= find_unique_abbrev(sha1
, len
);
2482 return sha1_to_hex(sha1
);
2483 abblen
= strlen(abbrev
);
2485 static char hex
[41];
2486 if (len
< abblen
&& abblen
<= len
+ 2)
2487 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2489 sprintf(hex
, "%s...", abbrev
);
2492 return sha1_to_hex(sha1
);
2495 static void diff_flush_raw(struct diff_filepair
*p
,
2496 struct diff_options
*options
)
2500 int abbrev
= options
->abbrev
;
2501 const char *path_one
, *path_two
;
2502 int inter_name_termination
= '\t';
2503 int line_termination
= options
->line_termination
;
2505 if (!line_termination
)
2506 inter_name_termination
= 0;
2508 path_one
= p
->one
->path
;
2509 path_two
= p
->two
->path
;
2510 if (line_termination
) {
2511 path_one
= quote_one(path_one
);
2512 path_two
= quote_one(path_two
);
2516 sprintf(status
, "%c%03d", p
->status
, similarity_index(p
));
2518 status
[0] = p
->status
;
2521 switch (p
->status
) {
2522 case DIFF_STATUS_COPIED
:
2523 case DIFF_STATUS_RENAMED
:
2526 case DIFF_STATUS_ADDED
:
2527 case DIFF_STATUS_DELETED
:
2534 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2535 printf(":%06o %06o %s ",
2536 p
->one
->mode
, p
->two
->mode
,
2537 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2539 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2541 printf("%s%c%s", status
, inter_name_termination
,
2542 two_paths
|| p
->one
->mode
? path_one
: path_two
);
2544 printf("%c%s", inter_name_termination
, path_two
);
2545 putchar(line_termination
);
2546 if (path_one
!= p
->one
->path
)
2547 free((void*)path_one
);
2548 if (path_two
!= p
->two
->path
)
2549 free((void*)path_two
);
2552 static void diff_flush_name(struct diff_filepair
*p
, struct diff_options
*opt
)
2554 char *path
= p
->two
->path
;
2556 if (opt
->line_termination
)
2557 path
= quote_one(p
->two
->path
);
2558 printf("%s%c", path
, opt
->line_termination
);
2559 if (p
->two
->path
!= path
)
2563 int diff_unmodified_pair(struct diff_filepair
*p
)
2565 /* This function is written stricter than necessary to support
2566 * the currently implemented transformers, but the idea is to
2567 * let transformers to produce diff_filepairs any way they want,
2568 * and filter and clean them up here before producing the output.
2570 struct diff_filespec
*one
, *two
;
2572 if (DIFF_PAIR_UNMERGED(p
))
2573 return 0; /* unmerged is interesting */
2578 /* deletion, addition, mode or type change
2579 * and rename are all interesting.
2581 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2582 DIFF_PAIR_MODE_CHANGED(p
) ||
2583 strcmp(one
->path
, two
->path
))
2586 /* both are valid and point at the same path. that is, we are
2587 * dealing with a change.
2589 if (one
->sha1_valid
&& two
->sha1_valid
&&
2590 !hashcmp(one
->sha1
, two
->sha1
))
2591 return 1; /* no change */
2592 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2593 return 1; /* both look at the same file on the filesystem. */
2597 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2599 if (diff_unmodified_pair(p
))
2602 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2603 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2604 return; /* no tree diffs in patch format */
2609 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2610 struct diffstat_t
*diffstat
)
2612 if (diff_unmodified_pair(p
))
2615 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2616 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2617 return; /* no tree diffs in patch format */
2619 run_diffstat(p
, o
, diffstat
);
2622 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2623 struct diff_options
*o
)
2625 if (diff_unmodified_pair(p
))
2628 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2629 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2630 return; /* no tree diffs in patch format */
2632 run_checkdiff(p
, o
);
2635 int diff_queue_is_empty(void)
2637 struct diff_queue_struct
*q
= &diff_queued_diff
;
2639 for (i
= 0; i
< q
->nr
; i
++)
2640 if (!diff_unmodified_pair(q
->queue
[i
]))
2646 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2648 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2651 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2653 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2654 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2656 s
->size
, s
->xfrm_flags
);
2659 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2661 diff_debug_filespec(p
->one
, i
, "one");
2662 diff_debug_filespec(p
->two
, i
, "two");
2663 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2664 p
->score
, p
->status
? p
->status
: '?',
2665 p
->source_stays
, p
->broken_pair
);
2668 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2672 fprintf(stderr
, "%s\n", msg
);
2673 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2674 for (i
= 0; i
< q
->nr
; i
++) {
2675 struct diff_filepair
*p
= q
->queue
[i
];
2676 diff_debug_filepair(p
, i
);
2681 static void diff_resolve_rename_copy(void)
2684 struct diff_filepair
*p
, *pp
;
2685 struct diff_queue_struct
*q
= &diff_queued_diff
;
2687 diff_debug_queue("resolve-rename-copy", q
);
2689 for (i
= 0; i
< q
->nr
; i
++) {
2691 p
->status
= 0; /* undecided */
2692 if (DIFF_PAIR_UNMERGED(p
))
2693 p
->status
= DIFF_STATUS_UNMERGED
;
2694 else if (!DIFF_FILE_VALID(p
->one
))
2695 p
->status
= DIFF_STATUS_ADDED
;
2696 else if (!DIFF_FILE_VALID(p
->two
))
2697 p
->status
= DIFF_STATUS_DELETED
;
2698 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2699 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2701 /* from this point on, we are dealing with a pair
2702 * whose both sides are valid and of the same type, i.e.
2703 * either in-place edit or rename/copy edit.
2705 else if (DIFF_PAIR_RENAME(p
)) {
2706 if (p
->source_stays
) {
2707 p
->status
= DIFF_STATUS_COPIED
;
2710 /* See if there is some other filepair that
2711 * copies from the same source as us. If so
2712 * we are a copy. Otherwise we are either a
2713 * copy if the path stays, or a rename if it
2714 * does not, but we already handled "stays" case.
2716 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2718 if (strcmp(pp
->one
->path
, p
->one
->path
))
2719 continue; /* not us */
2720 if (!DIFF_PAIR_RENAME(pp
))
2721 continue; /* not a rename/copy */
2722 /* pp is a rename/copy from the same source */
2723 p
->status
= DIFF_STATUS_COPIED
;
2727 p
->status
= DIFF_STATUS_RENAMED
;
2729 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2730 p
->one
->mode
!= p
->two
->mode
||
2731 is_null_sha1(p
->one
->sha1
))
2732 p
->status
= DIFF_STATUS_MODIFIED
;
2734 /* This is a "no-change" entry and should not
2735 * happen anymore, but prepare for broken callers.
2737 error("feeding unmodified %s to diffcore",
2739 p
->status
= DIFF_STATUS_UNKNOWN
;
2742 diff_debug_queue("resolve-rename-copy done", q
);
2745 static int check_pair_status(struct diff_filepair
*p
)
2747 switch (p
->status
) {
2748 case DIFF_STATUS_UNKNOWN
:
2751 die("internal error in diff-resolve-rename-copy");
2757 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2759 int fmt
= opt
->output_format
;
2761 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2762 diff_flush_checkdiff(p
, opt
);
2763 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2764 diff_flush_raw(p
, opt
);
2765 else if (fmt
& DIFF_FORMAT_NAME
)
2766 diff_flush_name(p
, opt
);
2769 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2771 char *name
= quote_one(fs
->path
);
2773 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, name
);
2775 printf(" %s %s\n", newdelete
, name
);
2780 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2782 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2784 char *name
= quote_one(p
->two
->path
);
2785 printf(" mode change %06o => %06o %s\n",
2786 p
->one
->mode
, p
->two
->mode
, name
);
2790 printf(" mode change %06o => %06o\n",
2791 p
->one
->mode
, p
->two
->mode
);
2795 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2797 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
2799 printf(" %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
2801 show_mode_change(p
, 0);
2804 static void diff_summary(struct diff_filepair
*p
)
2807 case DIFF_STATUS_DELETED
:
2808 show_file_mode_name("delete", p
->one
);
2810 case DIFF_STATUS_ADDED
:
2811 show_file_mode_name("create", p
->two
);
2813 case DIFF_STATUS_COPIED
:
2814 show_rename_copy("copy", p
);
2816 case DIFF_STATUS_RENAMED
:
2817 show_rename_copy("rename", p
);
2821 char *name
= quote_one(p
->two
->path
);
2822 printf(" rewrite %s (%d%%)\n", name
,
2823 similarity_index(p
));
2825 show_mode_change(p
, 0);
2826 } else show_mode_change(p
, 1);
2832 struct xdiff_emit_state xm
;
2837 static int remove_space(char *line
, int len
)
2843 for (i
= 0; i
< len
; i
++)
2844 if (!isspace((c
= line
[i
])))
2850 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2852 struct patch_id_t
*data
= priv
;
2855 /* Ignore line numbers when computing the SHA1 of the patch */
2856 if (!prefixcmp(line
, "@@ -"))
2859 new_len
= remove_space(line
, len
);
2861 SHA1_Update(data
->ctx
, line
, new_len
);
2862 data
->patchlen
+= new_len
;
2865 /* returns 0 upon success, and writes result into sha1 */
2866 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2868 struct diff_queue_struct
*q
= &diff_queued_diff
;
2871 struct patch_id_t data
;
2872 char buffer
[PATH_MAX
* 4 + 20];
2875 memset(&data
, 0, sizeof(struct patch_id_t
));
2877 data
.xm
.consume
= patch_id_consume
;
2879 for (i
= 0; i
< q
->nr
; i
++) {
2884 struct diff_filepair
*p
= q
->queue
[i
];
2887 memset(&xecfg
, 0, sizeof(xecfg
));
2889 return error("internal diff status error");
2890 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2892 if (diff_unmodified_pair(p
))
2894 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2895 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2897 if (DIFF_PAIR_UNMERGED(p
))
2900 diff_fill_sha1_info(p
->one
);
2901 diff_fill_sha1_info(p
->two
);
2902 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2903 fill_mmfile(&mf2
, p
->two
) < 0)
2904 return error("unable to read files to diff");
2906 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2907 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2908 if (p
->one
->mode
== 0)
2909 len1
= snprintf(buffer
, sizeof(buffer
),
2910 "diff--gita/%.*sb/%.*s"
2917 len2
, p
->two
->path
);
2918 else if (p
->two
->mode
== 0)
2919 len1
= snprintf(buffer
, sizeof(buffer
),
2920 "diff--gita/%.*sb/%.*s"
2921 "deletedfilemode%06o"
2927 len1
, p
->one
->path
);
2929 len1
= snprintf(buffer
, sizeof(buffer
),
2930 "diff--gita/%.*sb/%.*s"
2936 len2
, p
->two
->path
);
2937 SHA1_Update(&ctx
, buffer
, len1
);
2939 xpp
.flags
= XDF_NEED_MINIMAL
;
2941 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2942 ecb
.outf
= xdiff_outf
;
2944 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2947 SHA1_Final(sha1
, &ctx
);
2951 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2953 struct diff_queue_struct
*q
= &diff_queued_diff
;
2955 int result
= diff_get_patch_id(options
, sha1
);
2957 for (i
= 0; i
< q
->nr
; i
++)
2958 diff_free_filepair(q
->queue
[i
]);
2962 q
->nr
= q
->alloc
= 0;
2967 static int is_summary_empty(const struct diff_queue_struct
*q
)
2971 for (i
= 0; i
< q
->nr
; i
++) {
2972 const struct diff_filepair
*p
= q
->queue
[i
];
2974 switch (p
->status
) {
2975 case DIFF_STATUS_DELETED
:
2976 case DIFF_STATUS_ADDED
:
2977 case DIFF_STATUS_COPIED
:
2978 case DIFF_STATUS_RENAMED
:
2983 if (p
->one
->mode
&& p
->two
->mode
&&
2984 p
->one
->mode
!= p
->two
->mode
)
2992 void diff_flush(struct diff_options
*options
)
2994 struct diff_queue_struct
*q
= &diff_queued_diff
;
2995 int i
, output_format
= options
->output_format
;
2999 * Order: raw, stat, summary, patch
3000 * or: name/name-status/checkdiff (other bits clear)
3005 if (output_format
& (DIFF_FORMAT_RAW
|
3007 DIFF_FORMAT_NAME_STATUS
|
3008 DIFF_FORMAT_CHECKDIFF
)) {
3009 for (i
= 0; i
< q
->nr
; i
++) {
3010 struct diff_filepair
*p
= q
->queue
[i
];
3011 if (check_pair_status(p
))
3012 flush_one_pair(p
, options
);
3017 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3018 struct diffstat_t diffstat
;
3020 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3021 diffstat
.xm
.consume
= diffstat_consume
;
3022 for (i
= 0; i
< q
->nr
; i
++) {
3023 struct diff_filepair
*p
= q
->queue
[i
];
3024 if (check_pair_status(p
))
3025 diff_flush_stat(p
, options
, &diffstat
);
3027 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3028 show_numstat(&diffstat
, options
);
3029 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3030 show_stats(&diffstat
, options
);
3031 else if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3032 show_shortstats(&diffstat
);
3036 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3037 for (i
= 0; i
< q
->nr
; i
++)
3038 diff_summary(q
->queue
[i
]);
3042 if (output_format
& DIFF_FORMAT_PATCH
) {
3044 if (options
->stat_sep
) {
3045 /* attach patch instead of inline */
3046 fputs(options
->stat_sep
, stdout
);
3048 putchar(options
->line_termination
);
3052 for (i
= 0; i
< q
->nr
; i
++) {
3053 struct diff_filepair
*p
= q
->queue
[i
];
3054 if (check_pair_status(p
))
3055 diff_flush_patch(p
, options
);
3059 if (output_format
& DIFF_FORMAT_CALLBACK
)
3060 options
->format_callback(q
, options
, options
->format_callback_data
);
3062 for (i
= 0; i
< q
->nr
; i
++)
3063 diff_free_filepair(q
->queue
[i
]);
3067 q
->nr
= q
->alloc
= 0;
3070 static void diffcore_apply_filter(const char *filter
)
3073 struct diff_queue_struct
*q
= &diff_queued_diff
;
3074 struct diff_queue_struct outq
;
3076 outq
.nr
= outq
.alloc
= 0;
3081 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3083 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3084 struct diff_filepair
*p
= q
->queue
[i
];
3085 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3087 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3089 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3090 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3091 strchr(filter
, p
->status
)))
3097 /* otherwise we will clear the whole queue
3098 * by copying the empty outq at the end of this
3099 * function, but first clear the current entries
3102 for (i
= 0; i
< q
->nr
; i
++)
3103 diff_free_filepair(q
->queue
[i
]);
3106 /* Only the matching ones */
3107 for (i
= 0; i
< q
->nr
; i
++) {
3108 struct diff_filepair
*p
= q
->queue
[i
];
3110 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3112 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3114 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3115 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3116 strchr(filter
, p
->status
)))
3119 diff_free_filepair(p
);
3126 /* Check whether two filespecs with the same mode and size are identical */
3127 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3128 struct diff_filespec
*two
)
3130 if (S_ISGITLINK(one
->mode
)) {
3131 diff_fill_sha1_info(one
);
3132 diff_fill_sha1_info(two
);
3133 return !hashcmp(one
->sha1
, two
->sha1
);
3135 if (diff_populate_filespec(one
, 0))
3137 if (diff_populate_filespec(two
, 0))
3139 return !memcmp(one
->data
, two
->data
, one
->size
);
3142 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3145 struct diff_queue_struct
*q
= &diff_queued_diff
;
3146 struct diff_queue_struct outq
;
3148 outq
.nr
= outq
.alloc
= 0;
3150 for (i
= 0; i
< q
->nr
; i
++) {
3151 struct diff_filepair
*p
= q
->queue
[i
];
3154 * 1. Entries that come from stat info dirtyness
3155 * always have both sides (iow, not create/delete),
3156 * one side of the object name is unknown, with
3157 * the same mode and size. Keep the ones that
3158 * do not match these criteria. They have real
3161 * 2. At this point, the file is known to be modified,
3162 * with the same mode and size, and the object
3163 * name of one side is unknown. Need to inspect
3164 * the identical contents.
3166 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3167 !DIFF_FILE_VALID(p
->two
) ||
3168 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3169 (p
->one
->mode
!= p
->two
->mode
) ||
3170 diff_populate_filespec(p
->one
, 1) ||
3171 diff_populate_filespec(p
->two
, 1) ||
3172 (p
->one
->size
!= p
->two
->size
) ||
3173 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3177 * The caller can subtract 1 from skip_stat_unmatch
3178 * to determine how many paths were dirty only
3179 * due to stat info mismatch.
3181 if (!diffopt
->no_index
)
3182 diffopt
->skip_stat_unmatch
++;
3183 diff_free_filepair(p
);
3190 void diffcore_std(struct diff_options
*options
)
3195 if (options
->skip_stat_unmatch
&& !options
->find_copies_harder
)
3196 diffcore_skip_stat_unmatch(options
);
3197 if (options
->break_opt
!= -1)
3198 diffcore_break(options
->break_opt
);
3199 if (options
->detect_rename
)
3200 diffcore_rename(options
);
3201 if (options
->break_opt
!= -1)
3202 diffcore_merge_broken();
3203 if (options
->pickaxe
)
3204 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3205 if (options
->orderfile
)
3206 diffcore_order(options
->orderfile
);
3207 diff_resolve_rename_copy();
3208 diffcore_apply_filter(options
->filter
);
3210 options
->has_changes
= !!diff_queued_diff
.nr
;
3214 void diff_addremove(struct diff_options
*options
,
3215 int addremove
, unsigned mode
,
3216 const unsigned char *sha1
,
3217 const char *base
, const char *path
)
3219 char concatpath
[PATH_MAX
];
3220 struct diff_filespec
*one
, *two
;
3222 /* This may look odd, but it is a preparation for
3223 * feeding "there are unchanged files which should
3224 * not produce diffs, but when you are doing copy
3225 * detection you would need them, so here they are"
3226 * entries to the diff-core. They will be prefixed
3227 * with something like '=' or '*' (I haven't decided
3228 * which but should not make any difference).
3229 * Feeding the same new and old to diff_change()
3230 * also has the same effect.
3231 * Before the final output happens, they are pruned after
3232 * merged into rename/copy pairs as appropriate.
3234 if (options
->reverse_diff
)
3235 addremove
= (addremove
== '+' ? '-' :
3236 addremove
== '-' ? '+' : addremove
);
3238 if (!path
) path
= "";
3239 sprintf(concatpath
, "%s%s", base
, path
);
3240 one
= alloc_filespec(concatpath
);
3241 two
= alloc_filespec(concatpath
);
3243 if (addremove
!= '+')
3244 fill_filespec(one
, sha1
, mode
);
3245 if (addremove
!= '-')
3246 fill_filespec(two
, sha1
, mode
);
3248 diff_queue(&diff_queued_diff
, one
, two
);
3249 options
->has_changes
= 1;
3252 void diff_change(struct diff_options
*options
,
3253 unsigned old_mode
, unsigned new_mode
,
3254 const unsigned char *old_sha1
,
3255 const unsigned char *new_sha1
,
3256 const char *base
, const char *path
)
3258 char concatpath
[PATH_MAX
];
3259 struct diff_filespec
*one
, *two
;
3261 if (options
->reverse_diff
) {
3263 const unsigned char *tmp_c
;
3264 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3265 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3267 if (!path
) path
= "";
3268 sprintf(concatpath
, "%s%s", base
, path
);
3269 one
= alloc_filespec(concatpath
);
3270 two
= alloc_filespec(concatpath
);
3271 fill_filespec(one
, old_sha1
, old_mode
);
3272 fill_filespec(two
, new_sha1
, new_mode
);
3274 diff_queue(&diff_queued_diff
, one
, two
);
3275 options
->has_changes
= 1;
3278 void diff_unmerge(struct diff_options
*options
,
3280 unsigned mode
, const unsigned char *sha1
)
3282 struct diff_filespec
*one
, *two
;
3283 one
= alloc_filespec(path
);
3284 two
= alloc_filespec(path
);
3285 fill_filespec(one
, sha1
, mode
);
3286 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;