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
= -1;
21 static int diff_use_color_default
;
23 static char diff_colors
[][COLOR_MAXLEN
] = {
25 "", /* PLAIN (normal) */
26 "\033[1m", /* METAINFO (bold) */
27 "\033[36m", /* FRAGINFO (cyan) */
28 "\033[31m", /* OLD (red) */
29 "\033[32m", /* NEW (green) */
30 "\033[33m", /* COMMIT (yellow) */
31 "\033[41m", /* WHITESPACE (red background) */
34 static int parse_diff_color_slot(const char *var
, int ofs
)
36 if (!strcasecmp(var
+ofs
, "plain"))
38 if (!strcasecmp(var
+ofs
, "meta"))
40 if (!strcasecmp(var
+ofs
, "frag"))
42 if (!strcasecmp(var
+ofs
, "old"))
44 if (!strcasecmp(var
+ofs
, "new"))
46 if (!strcasecmp(var
+ofs
, "commit"))
48 if (!strcasecmp(var
+ofs
, "whitespace"))
49 return DIFF_WHITESPACE
;
50 die("bad config variable '%s'", var
);
53 static struct ll_diff_driver
{
55 struct ll_diff_driver
*next
;
57 } *user_diff
, **user_diff_tail
;
59 static void read_config_if_needed(void)
61 if (!user_diff_tail
) {
62 user_diff_tail
= &user_diff
;
63 git_config(git_diff_ui_config
);
68 * Currently there is only "diff.<drivername>.command" variable;
69 * because there are "diff.color.<slot>" variables, we are parsing
70 * this in a bit convoluted way to allow low level diff driver
73 static int parse_lldiff_command(const char *var
, const char *ep
, const char *value
)
77 struct ll_diff_driver
*drv
;
81 for (drv
= user_diff
; drv
; drv
= drv
->next
)
82 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
86 drv
= xcalloc(1, sizeof(struct ll_diff_driver
));
87 namebuf
= xmalloc(namelen
+ 1);
88 memcpy(namebuf
, name
, namelen
);
93 user_diff_tail
= &user_diff
;
94 *user_diff_tail
= drv
;
95 user_diff_tail
= &(drv
->next
);
99 return error("%s: lacks value", var
);
100 drv
->cmd
= strdup(value
);
105 * 'diff.<what>.funcname' attribute can be specified in the configuration
106 * to define a customized regexp to find the beginning of a function to
107 * be used for hunk header lines of "diff -p" style output.
109 static struct funcname_pattern
{
112 struct funcname_pattern
*next
;
113 } *funcname_pattern_list
;
115 static int parse_funcname_pattern(const char *var
, const char *ep
, const char *value
)
119 struct funcname_pattern
*pp
;
121 name
= var
+ 5; /* "diff." */
124 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
125 if (!strncmp(pp
->name
, name
, namelen
) && !pp
->name
[namelen
])
129 pp
= xcalloc(1, sizeof(*pp
));
130 namebuf
= xmalloc(namelen
+ 1);
131 memcpy(namebuf
, name
, namelen
);
132 namebuf
[namelen
] = 0;
134 pp
->next
= funcname_pattern_list
;
135 funcname_pattern_list
= pp
;
139 pp
->pattern
= xstrdup(value
);
144 * These are to give UI layer defaults.
145 * The core-level commands such as git-diff-files should
146 * never be affected by the setting of diff.renames
147 * the user happens to have in the configuration file.
149 int git_diff_ui_config(const char *var
, const char *value
)
151 if (!strcmp(var
, "diff.renamelimit")) {
152 diff_rename_limit_default
= git_config_int(var
, value
);
155 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
156 diff_use_color_default
= git_config_colorbool(var
, value
);
159 if (!strcmp(var
, "diff.renames")) {
161 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
162 else if (!strcasecmp(value
, "copies") ||
163 !strcasecmp(value
, "copy"))
164 diff_detect_rename_default
= DIFF_DETECT_COPY
;
165 else if (git_config_bool(var
,value
))
166 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
169 if (!prefixcmp(var
, "diff.")) {
170 const char *ep
= strrchr(var
, '.');
173 if (!strcmp(ep
, ".command"))
174 return parse_lldiff_command(var
, ep
, value
);
175 if (!strcmp(ep
, ".funcname"))
176 return parse_funcname_pattern(var
, ep
, value
);
179 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
180 int slot
= parse_diff_color_slot(var
, 11);
181 color_parse(value
, var
, diff_colors
[slot
]);
185 return git_default_config(var
, value
);
188 static char *quote_one(const char *str
)
195 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
198 xp
= xmalloc(needlen
+ 1);
199 quote_c_style(str
, xp
, NULL
, 0);
203 static char *quote_two(const char *one
, const char *two
)
205 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
206 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
209 if (need_one
+ need_two
) {
210 if (!need_one
) need_one
= strlen(one
);
211 if (!need_two
) need_one
= strlen(two
);
213 xp
= xmalloc(need_one
+ need_two
+ 3);
215 quote_c_style(one
, xp
+ 1, NULL
, 1);
216 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
217 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
220 need_one
= strlen(one
);
221 need_two
= strlen(two
);
222 xp
= xmalloc(need_one
+ need_two
+ 1);
224 strcpy(xp
+ need_one
, two
);
228 static const char *external_diff(void)
230 static const char *external_diff_cmd
= NULL
;
231 static int done_preparing
= 0;
234 return external_diff_cmd
;
235 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
237 return external_diff_cmd
;
240 static struct diff_tempfile
{
241 const char *name
; /* filename external diff should read from */
244 char tmp_path
[PATH_MAX
];
247 static int count_lines(const char *data
, int size
)
249 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
256 completely_empty
= 0;
260 completely_empty
= 0;
263 if (completely_empty
)
266 count
++; /* no trailing newline */
270 static void print_line_count(int count
)
280 printf("1,%d", count
);
285 static void copy_file(int prefix
, const char *data
, int size
,
286 const char *set
, const char *reset
)
288 int ch
, nl_just_seen
= 1;
297 fputs(reset
, stdout
);
303 printf("%s\n\\ No newline at end of file\n", reset
);
306 static void emit_rewrite_diff(const char *name_a
,
308 struct diff_filespec
*one
,
309 struct diff_filespec
*two
,
313 const char *name_a_tab
, *name_b_tab
;
314 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
315 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
316 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
317 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
318 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
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 diff_populate_filespec(one
, 0);
326 diff_populate_filespec(two
, 0);
327 lc_a
= count_lines(one
->data
, one
->size
);
328 lc_b
= count_lines(two
->data
, two
->size
);
329 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
330 metainfo
, name_a
, name_a_tab
, reset
,
331 metainfo
, name_b
, name_b_tab
, reset
, fraginfo
);
332 print_line_count(lc_a
);
334 print_line_count(lc_b
);
335 printf(" @@%s\n", reset
);
337 copy_file('-', one
->data
, one
->size
, old
, reset
);
339 copy_file('+', two
->data
, two
->size
, new, reset
);
342 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
344 if (!DIFF_FILE_VALID(one
)) {
345 mf
->ptr
= (char *)""; /* does not matter */
349 else if (diff_populate_filespec(one
, 0))
352 mf
->size
= one
->size
;
356 struct diff_words_buffer
{
359 long current
; /* output pointer */
360 int suppressed_newline
;
363 static void diff_words_append(char *line
, unsigned long len
,
364 struct diff_words_buffer
*buffer
)
366 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
367 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
368 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
372 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
373 buffer
->text
.size
+= len
;
376 struct diff_words_data
{
377 struct xdiff_emit_state xm
;
378 struct diff_words_buffer minus
, plus
;
381 static void print_word(struct diff_words_buffer
*buffer
, int len
, int color
,
382 int suppress_newline
)
390 ptr
= buffer
->text
.ptr
+ buffer
->current
;
391 buffer
->current
+= len
;
393 if (ptr
[len
- 1] == '\n') {
398 fputs(diff_get_color(1, color
), stdout
);
399 fwrite(ptr
, len
, 1, stdout
);
400 fputs(diff_get_color(1, DIFF_RESET
), stdout
);
403 if (suppress_newline
)
404 buffer
->suppressed_newline
= 1;
410 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
412 struct diff_words_data
*diff_words
= priv
;
414 if (diff_words
->minus
.suppressed_newline
) {
417 diff_words
->minus
.suppressed_newline
= 0;
423 print_word(&diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
426 print_word(&diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
429 print_word(&diff_words
->plus
, len
, DIFF_PLAIN
, 0);
430 diff_words
->minus
.current
+= len
;
435 /* this executes the word diff on the accumulated buffers */
436 static void diff_words_show(struct diff_words_data
*diff_words
)
441 mmfile_t minus
, plus
;
444 memset(&xecfg
, 0, sizeof(xecfg
));
445 minus
.size
= diff_words
->minus
.text
.size
;
446 minus
.ptr
= xmalloc(minus
.size
);
447 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
448 for (i
= 0; i
< minus
.size
; i
++)
449 if (isspace(minus
.ptr
[i
]))
451 diff_words
->minus
.current
= 0;
453 plus
.size
= diff_words
->plus
.text
.size
;
454 plus
.ptr
= xmalloc(plus
.size
);
455 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
456 for (i
= 0; i
< plus
.size
; i
++)
457 if (isspace(plus
.ptr
[i
]))
459 diff_words
->plus
.current
= 0;
461 xpp
.flags
= XDF_NEED_MINIMAL
;
462 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
463 ecb
.outf
= xdiff_outf
;
464 ecb
.priv
= diff_words
;
465 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
466 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
470 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
472 if (diff_words
->minus
.suppressed_newline
) {
474 diff_words
->minus
.suppressed_newline
= 0;
478 struct emit_callback
{
479 struct xdiff_emit_state xm
;
480 int nparents
, color_diff
;
481 const char **label_path
;
482 struct diff_words_data
*diff_words
;
486 static void free_diff_words_data(struct emit_callback
*ecbdata
)
488 if (ecbdata
->diff_words
) {
490 if (ecbdata
->diff_words
->minus
.text
.size
||
491 ecbdata
->diff_words
->plus
.text
.size
)
492 diff_words_show(ecbdata
->diff_words
);
494 if (ecbdata
->diff_words
->minus
.text
.ptr
)
495 free (ecbdata
->diff_words
->minus
.text
.ptr
);
496 if (ecbdata
->diff_words
->plus
.text
.ptr
)
497 free (ecbdata
->diff_words
->plus
.text
.ptr
);
498 free(ecbdata
->diff_words
);
499 ecbdata
->diff_words
= NULL
;
503 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
506 return diff_colors
[ix
];
510 static void emit_line(const char *set
, const char *reset
, const char *line
, int len
)
512 if (len
> 0 && line
[len
-1] == '\n')
515 fwrite(line
, len
, 1, stdout
);
519 static void emit_line_with_ws(int nparents
,
520 const char *set
, const char *reset
, const char *ws
,
521 const char *line
, int len
)
524 int last_tab_in_indent
= -1;
525 int last_space_in_indent
= -1;
528 int need_highlight_leading_space
= 0;
529 /* The line is a newly added line. Does it have funny leading
530 * whitespaces? In indent, SP should never precede a TAB.
532 for (i
= col0
; i
< len
; i
++) {
533 if (line
[i
] == '\t') {
534 last_tab_in_indent
= i
;
535 if (0 <= last_space_in_indent
)
536 need_highlight_leading_space
= 1;
538 else if (line
[i
] == ' ')
539 last_space_in_indent
= i
;
544 fwrite(line
, col0
, 1, stdout
);
545 fputs(reset
, stdout
);
546 if (((i
== len
) || line
[i
] == '\n') && i
!= col0
) {
547 /* The whole line was indent */
548 emit_line(ws
, reset
, line
+ col0
, len
- col0
);
552 if (need_highlight_leading_space
) {
553 while (i
< last_tab_in_indent
) {
554 if (line
[i
] == ' ') {
557 fputs(reset
, stdout
);
565 if (line
[tail
] == '\n' && i
< tail
)
568 if (!isspace(line
[tail
]))
572 if ((i
< tail
&& line
[tail
+ 1] != '\n')) {
573 /* This has whitespace between tail+1..len */
575 fwrite(line
+ i
, tail
- i
+ 1, 1, stdout
);
576 fputs(reset
, stdout
);
577 emit_line(ws
, reset
, line
+ tail
+ 1, len
- tail
- 1);
580 emit_line(set
, reset
, line
+ i
, len
- i
);
583 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
585 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
586 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
589 emit_line(set
, reset
, line
, len
);
591 emit_line_with_ws(ecbdata
->nparents
, set
, reset
, ws
,
595 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
599 struct emit_callback
*ecbdata
= priv
;
600 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
601 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
603 *(ecbdata
->found_changesp
) = 1;
605 if (ecbdata
->label_path
[0]) {
606 const char *name_a_tab
, *name_b_tab
;
608 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
609 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
611 printf("%s--- %s%s%s\n",
612 set
, ecbdata
->label_path
[0], reset
, name_a_tab
);
613 printf("%s+++ %s%s%s\n",
614 set
, ecbdata
->label_path
[1], reset
, name_b_tab
);
615 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
618 /* This is not really necessary for now because
619 * this codepath only deals with two-way diffs.
621 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
623 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
624 ecbdata
->nparents
= i
- 1;
625 emit_line(diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
630 if (len
< ecbdata
->nparents
) {
632 emit_line(reset
, reset
, line
, len
);
637 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
638 /* fall back to normal diff */
639 free_diff_words_data(ecbdata
);
640 if (ecbdata
->diff_words
) {
641 if (line
[0] == '-') {
642 diff_words_append(line
, len
,
643 &ecbdata
->diff_words
->minus
);
645 } else if (line
[0] == '+') {
646 diff_words_append(line
, len
,
647 &ecbdata
->diff_words
->plus
);
650 if (ecbdata
->diff_words
->minus
.text
.size
||
651 ecbdata
->diff_words
->plus
.text
.size
)
652 diff_words_show(ecbdata
->diff_words
);
655 emit_line(set
, reset
, line
, len
);
658 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
660 color
= DIFF_FILE_OLD
;
661 else if (line
[i
] == '+')
662 color
= DIFF_FILE_NEW
;
665 if (color
!= DIFF_FILE_NEW
) {
666 emit_line(diff_get_color(ecbdata
->color_diff
, color
),
670 emit_add_line(reset
, ecbdata
, line
, len
);
673 static char *pprint_rename(const char *a
, const char *b
)
678 int pfx_length
, sfx_length
;
679 int len_a
= strlen(a
);
680 int len_b
= strlen(b
);
681 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
682 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
684 if (qlen_a
|| qlen_b
) {
685 if (qlen_a
) len_a
= qlen_a
;
686 if (qlen_b
) len_b
= qlen_b
;
687 name
= xmalloc( len_a
+ len_b
+ 5 );
689 quote_c_style(a
, name
, NULL
, 0);
691 memcpy(name
, a
, len_a
);
692 memcpy(name
+ len_a
, " => ", 4);
694 quote_c_style(b
, name
+ len_a
+ 4, NULL
, 0);
696 memcpy(name
+ len_a
+ 4, b
, len_b
+ 1);
700 /* Find common prefix */
702 while (*old
&& *new && *old
== *new) {
704 pfx_length
= old
- a
+ 1;
709 /* Find common suffix */
713 while (a
<= old
&& b
<= new && *old
== *new) {
715 sfx_length
= len_a
- (old
- a
);
721 * pfx{mid-a => mid-b}sfx
722 * {pfx-a => pfx-b}sfx
723 * pfx{sfx-a => sfx-b}
726 if (pfx_length
+ sfx_length
) {
727 int a_midlen
= len_a
- pfx_length
- sfx_length
;
728 int b_midlen
= len_b
- pfx_length
- sfx_length
;
729 if (a_midlen
< 0) a_midlen
= 0;
730 if (b_midlen
< 0) b_midlen
= 0;
732 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
733 sprintf(name
, "%.*s{%.*s => %.*s}%s",
735 a_midlen
, a
+ pfx_length
,
736 b_midlen
, b
+ pfx_length
,
737 a
+ len_a
- sfx_length
);
740 name
= xmalloc(len_a
+ len_b
+ 5);
741 sprintf(name
, "%s => %s", a
, b
);
747 struct xdiff_emit_state xm
;
751 struct diffstat_file
{
753 unsigned is_unmerged
:1;
754 unsigned is_binary
:1;
755 unsigned is_renamed
:1;
756 unsigned int added
, deleted
;
760 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
764 struct diffstat_file
*x
;
765 x
= xcalloc(sizeof (*x
), 1);
766 if (diffstat
->nr
== diffstat
->alloc
) {
767 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
768 diffstat
->files
= xrealloc(diffstat
->files
,
769 diffstat
->alloc
* sizeof(x
));
771 diffstat
->files
[diffstat
->nr
++] = x
;
773 x
->name
= pprint_rename(name_a
, name_b
);
777 x
->name
= xstrdup(name_a
);
781 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
783 struct diffstat_t
*diffstat
= priv
;
784 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
788 else if (line
[0] == '-')
792 const char mime_boundary_leader
[] = "------------";
794 static int scale_linear(int it
, int width
, int max_change
)
797 * make sure that at least one '-' is printed if there were deletions,
798 * and likewise for '+'.
802 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
805 static void show_name(const char *prefix
, const char *name
, int len
,
806 const char *reset
, const char *set
)
808 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
811 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
821 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
823 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
824 int max_change
= 0, max_len
= 0;
825 int total_files
= data
->nr
;
826 int width
, name_width
;
827 const char *reset
, *set
, *add_c
, *del_c
;
832 width
= options
->stat_width
? options
->stat_width
: 80;
833 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
835 /* Sanity: give at least 5 columns to the graph,
836 * but leave at least 10 columns for the name.
838 if (width
< name_width
+ 15) {
839 if (name_width
<= 25)
840 width
= name_width
+ 15;
842 name_width
= width
- 15;
845 /* Find the longest filename and max number of changes */
846 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
847 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
848 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
849 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
851 for (i
= 0; i
< data
->nr
; i
++) {
852 struct diffstat_file
*file
= data
->files
[i
];
853 int change
= file
->added
+ file
->deleted
;
855 if (!file
->is_renamed
) { /* renames are already quoted by pprint_rename */
856 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
858 char *qname
= xmalloc(len
+ 1);
859 quote_c_style(file
->name
, qname
, NULL
, 0);
865 len
= strlen(file
->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
]->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(prefix
, name
, len
, reset
, set
);
913 printf("%s%d%s", del_c
, deleted
, reset
);
915 printf("%s%d%s", add_c
, added
, reset
);
918 goto free_diffstat_file
;
920 else if (data
->files
[i
]->is_unmerged
) {
921 show_name(prefix
, name
, len
, reset
, set
);
922 printf(" Unmerged\n");
923 goto free_diffstat_file
;
925 else if (!data
->files
[i
]->is_renamed
&&
926 (added
+ deleted
== 0)) {
928 goto free_diffstat_file
;
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(prefix
, name
, len
, reset
, set
);
946 printf("%5d ", added
+ deleted
);
947 show_graph('+', add
, add_c
, reset
);
948 show_graph('-', del
, del_c
, reset
);
951 free(data
->files
[i
]->name
);
952 free(data
->files
[i
]);
955 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
956 set
, total_files
, adds
, dels
, reset
);
959 static void show_shortstats(struct diffstat_t
* data
)
961 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
966 for (i
= 0; i
< data
->nr
; i
++) {
967 if (!data
->files
[i
]->is_binary
&&
968 !data
->files
[i
]->is_unmerged
) {
969 int added
= data
->files
[i
]->added
;
970 int deleted
= data
->files
[i
]->deleted
;
971 if (!data
->files
[i
]->is_renamed
&&
972 (added
+ deleted
== 0)) {
979 free(data
->files
[i
]->name
);
980 free(data
->files
[i
]);
984 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
985 total_files
, adds
, dels
);
988 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
992 for (i
= 0; i
< data
->nr
; i
++) {
993 struct diffstat_file
*file
= data
->files
[i
];
998 printf("%d\t%d\t", file
->added
, file
->deleted
);
999 if (options
->line_termination
&& !file
->is_renamed
&&
1000 quote_c_style(file
->name
, NULL
, NULL
, 0))
1001 quote_c_style(file
->name
, NULL
, stdout
, 0);
1003 fputs(file
->name
, stdout
);
1004 putchar(options
->line_termination
);
1008 struct checkdiff_t
{
1009 struct xdiff_emit_state xm
;
1010 const char *filename
;
1011 int lineno
, color_diff
;
1014 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1016 struct checkdiff_t
*data
= priv
;
1017 const char *ws
= diff_get_color(data
->color_diff
, DIFF_WHITESPACE
);
1018 const char *reset
= diff_get_color(data
->color_diff
, DIFF_RESET
);
1019 const char *set
= diff_get_color(data
->color_diff
, DIFF_FILE_NEW
);
1021 if (line
[0] == '+') {
1022 int i
, spaces
= 0, space_before_tab
= 0, white_space_at_end
= 0;
1024 /* check space before tab */
1025 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
1028 if (line
[i
- 1] == '\t' && spaces
)
1029 space_before_tab
= 1;
1031 /* check white space at line end */
1032 if (line
[len
- 1] == '\n')
1034 if (isspace(line
[len
- 1]))
1035 white_space_at_end
= 1;
1037 if (space_before_tab
|| white_space_at_end
) {
1038 printf("%s:%d: %s", data
->filename
, data
->lineno
, ws
);
1039 if (space_before_tab
) {
1040 printf("space before tab");
1041 if (white_space_at_end
)
1044 if (white_space_at_end
)
1045 printf("white space at end");
1046 printf(":%s ", reset
);
1047 emit_line_with_ws(1, set
, reset
, ws
, line
, len
);
1051 } else if (line
[0] == ' ')
1053 else if (line
[0] == '@') {
1054 char *plus
= strchr(line
, '+');
1056 data
->lineno
= strtol(plus
, NULL
, 10);
1058 die("invalid diff");
1062 static unsigned char *deflate_it(char *data
,
1064 unsigned long *result_size
)
1067 unsigned char *deflated
;
1070 memset(&stream
, 0, sizeof(stream
));
1071 deflateInit(&stream
, zlib_compression_level
);
1072 bound
= deflateBound(&stream
, size
);
1073 deflated
= xmalloc(bound
);
1074 stream
.next_out
= deflated
;
1075 stream
.avail_out
= bound
;
1077 stream
.next_in
= (unsigned char *)data
;
1078 stream
.avail_in
= size
;
1079 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1081 deflateEnd(&stream
);
1082 *result_size
= stream
.total_out
;
1086 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
1092 unsigned long orig_size
;
1093 unsigned long delta_size
;
1094 unsigned long deflate_size
;
1095 unsigned long data_size
;
1097 /* We could do deflated delta, or we could do just deflated two,
1098 * whichever is smaller.
1101 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1102 if (one
->size
&& two
->size
) {
1103 delta
= diff_delta(one
->ptr
, one
->size
,
1104 two
->ptr
, two
->size
,
1105 &delta_size
, deflate_size
);
1107 void *to_free
= delta
;
1108 orig_size
= delta_size
;
1109 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1114 if (delta
&& delta_size
< deflate_size
) {
1115 printf("delta %lu\n", orig_size
);
1118 data_size
= delta_size
;
1121 printf("literal %lu\n", two
->size
);
1124 data_size
= deflate_size
;
1127 /* emit data encoded in base85 */
1130 int bytes
= (52 < data_size
) ? 52 : data_size
;
1134 line
[0] = bytes
+ 'A' - 1;
1136 line
[0] = bytes
- 26 + 'a' - 1;
1137 encode_85(line
+ 1, cp
, bytes
);
1138 cp
= (char *) cp
+ bytes
;
1145 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
1147 printf("GIT binary patch\n");
1148 emit_binary_diff_body(one
, two
);
1149 emit_binary_diff_body(two
, one
);
1152 static void setup_diff_attr_check(struct git_attr_check
*check
)
1154 static struct git_attr
*attr_diff
;
1157 attr_diff
= git_attr("diff", 4);
1159 check
[0].attr
= attr_diff
;
1162 static void diff_filespec_check_attr(struct diff_filespec
*one
)
1164 struct git_attr_check attr_diff_check
;
1165 int check_from_data
= 0;
1167 if (one
->checked_attr
)
1170 setup_diff_attr_check(&attr_diff_check
);
1172 one
->funcname_pattern_ident
= NULL
;
1174 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1178 value
= attr_diff_check
.value
;
1179 if (ATTR_TRUE(value
))
1181 else if (ATTR_FALSE(value
))
1184 check_from_data
= 1;
1186 /* funcname pattern ident */
1187 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1190 one
->funcname_pattern_ident
= value
;
1193 if (check_from_data
) {
1194 if (!one
->data
&& DIFF_FILE_VALID(one
))
1195 diff_populate_filespec(one
, 0);
1198 one
->is_binary
= buffer_is_binary(one
->data
, one
->size
);
1202 int diff_filespec_is_binary(struct diff_filespec
*one
)
1204 diff_filespec_check_attr(one
);
1205 return one
->is_binary
;
1208 static const char *funcname_pattern(const char *ident
)
1210 struct funcname_pattern
*pp
;
1212 read_config_if_needed();
1213 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
1214 if (!strcmp(ident
, pp
->name
))
1219 static const char *diff_funcname_pattern(struct diff_filespec
*one
)
1221 const char *ident
, *pattern
;
1223 diff_filespec_check_attr(one
);
1224 ident
= one
->funcname_pattern_ident
;
1228 * If the config file has "funcname.default" defined, that
1229 * regexp is used; otherwise NULL is returned and xemit uses
1230 * the built-in default.
1232 return funcname_pattern("default");
1234 /* Look up custom "funcname.$ident" regexp from config. */
1235 pattern
= funcname_pattern(ident
);
1240 * And define built-in fallback patterns here. Note that
1241 * these can be overriden by the user's config settings.
1243 if (!strcmp(ident
, "java"))
1244 return "!^[ ]*\\(catch\\|do\\|for\\|if\\|instanceof\\|"
1245 "new\\|return\\|switch\\|throw\\|while\\)\n"
1247 "[A-Za-z_][A-Za-z_0-9]*\\)\\{2,\\}"
1253 static void builtin_diff(const char *name_a
,
1255 struct diff_filespec
*one
,
1256 struct diff_filespec
*two
,
1257 const char *xfrm_msg
,
1258 struct diff_options
*o
,
1259 int complete_rewrite
)
1263 char *a_one
, *b_two
;
1264 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
1265 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
1267 a_one
= quote_two("a/", name_a
+ (*name_a
== '/'));
1268 b_two
= quote_two("b/", name_b
+ (*name_b
== '/'));
1269 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1270 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1271 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1272 if (lbl
[0][0] == '/') {
1274 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1275 if (xfrm_msg
&& xfrm_msg
[0])
1276 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1278 else if (lbl
[1][0] == '/') {
1279 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1280 if (xfrm_msg
&& xfrm_msg
[0])
1281 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1284 if (one
->mode
!= two
->mode
) {
1285 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
1286 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
1288 if (xfrm_msg
&& xfrm_msg
[0])
1289 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1291 * we do not run diff between different kind
1294 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1295 goto free_ab_and_return
;
1296 if (complete_rewrite
) {
1297 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1299 o
->found_changes
= 1;
1300 goto free_ab_and_return
;
1304 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1305 die("unable to read files to diff");
1308 (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
))) {
1309 /* Quite common confusing case */
1310 if (mf1
.size
== mf2
.size
&&
1311 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1312 goto free_ab_and_return
;
1314 emit_binary_diff(&mf1
, &mf2
);
1316 printf("Binary files %s and %s differ\n",
1318 o
->found_changes
= 1;
1321 /* Crazy xdl interfaces.. */
1322 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1326 struct emit_callback ecbdata
;
1327 const char *funcname_pattern
;
1329 funcname_pattern
= diff_funcname_pattern(one
);
1330 if (!funcname_pattern
)
1331 funcname_pattern
= diff_funcname_pattern(two
);
1333 memset(&xecfg
, 0, sizeof(xecfg
));
1334 memset(&ecbdata
, 0, sizeof(ecbdata
));
1335 ecbdata
.label_path
= lbl
;
1336 ecbdata
.color_diff
= o
->color_diff
;
1337 ecbdata
.found_changesp
= &o
->found_changes
;
1338 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1339 xecfg
.ctxlen
= o
->context
;
1340 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1341 if (funcname_pattern
)
1342 xdiff_set_find_func(&xecfg
, funcname_pattern
);
1345 else if (!prefixcmp(diffopts
, "--unified="))
1346 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1347 else if (!prefixcmp(diffopts
, "-u"))
1348 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1349 ecb
.outf
= xdiff_outf
;
1350 ecb
.priv
= &ecbdata
;
1351 ecbdata
.xm
.consume
= fn_out_consume
;
1352 if (o
->color_diff_words
)
1353 ecbdata
.diff_words
=
1354 xcalloc(1, sizeof(struct diff_words_data
));
1355 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1356 if (o
->color_diff_words
)
1357 free_diff_words_data(&ecbdata
);
1361 diff_free_filespec_data(one
);
1362 diff_free_filespec_data(two
);
1368 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1369 struct diff_filespec
*one
,
1370 struct diff_filespec
*two
,
1371 struct diffstat_t
*diffstat
,
1372 struct diff_options
*o
,
1373 int complete_rewrite
)
1376 struct diffstat_file
*data
;
1378 data
= diffstat_add(diffstat
, name_a
, name_b
);
1381 data
->is_unmerged
= 1;
1384 if (complete_rewrite
) {
1385 diff_populate_filespec(one
, 0);
1386 diff_populate_filespec(two
, 0);
1387 data
->deleted
= count_lines(one
->data
, one
->size
);
1388 data
->added
= count_lines(two
->data
, two
->size
);
1389 goto free_and_return
;
1391 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1392 die("unable to read files to diff");
1394 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1395 data
->is_binary
= 1;
1396 data
->added
= mf2
.size
;
1397 data
->deleted
= mf1
.size
;
1399 /* Crazy xdl interfaces.. */
1404 memset(&xecfg
, 0, sizeof(xecfg
));
1405 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1406 ecb
.outf
= xdiff_outf
;
1407 ecb
.priv
= diffstat
;
1408 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1412 diff_free_filespec_data(one
);
1413 diff_free_filespec_data(two
);
1416 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1417 struct diff_filespec
*one
,
1418 struct diff_filespec
*two
, struct diff_options
*o
)
1421 struct checkdiff_t data
;
1426 memset(&data
, 0, sizeof(data
));
1427 data
.xm
.consume
= checkdiff_consume
;
1428 data
.filename
= name_b
? name_b
: name_a
;
1430 data
.color_diff
= o
->color_diff
;
1432 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1433 die("unable to read files to diff");
1435 if (diff_filespec_is_binary(two
))
1436 goto free_and_return
;
1438 /* Crazy xdl interfaces.. */
1443 memset(&xecfg
, 0, sizeof(xecfg
));
1444 xpp
.flags
= XDF_NEED_MINIMAL
;
1445 ecb
.outf
= xdiff_outf
;
1447 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1450 diff_free_filespec_data(one
);
1451 diff_free_filespec_data(two
);
1454 struct diff_filespec
*alloc_filespec(const char *path
)
1456 int namelen
= strlen(path
);
1457 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1459 memset(spec
, 0, sizeof(*spec
));
1460 spec
->path
= (char *)(spec
+ 1);
1461 memcpy(spec
->path
, path
, namelen
+1);
1465 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1466 unsigned short mode
)
1469 spec
->mode
= canon_mode(mode
);
1470 hashcpy(spec
->sha1
, sha1
);
1471 spec
->sha1_valid
= !is_null_sha1(sha1
);
1476 * Given a name and sha1 pair, if the index tells us the file in
1477 * the work tree has that object contents, return true, so that
1478 * prepare_temp_file() does not have to inflate and extract.
1480 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1482 struct cache_entry
*ce
;
1486 /* We do not read the cache ourselves here, because the
1487 * benchmark with my previous version that always reads cache
1488 * shows that it makes things worse for diff-tree comparing
1489 * two linux-2.6 kernel trees in an already checked out work
1490 * tree. This is because most diff-tree comparisons deal with
1491 * only a small number of files, while reading the cache is
1492 * expensive for a large project, and its cost outweighs the
1493 * savings we get by not inflating the object to a temporary
1494 * file. Practically, this code only helps when we are used
1495 * by diff-cache --cached, which does read the cache before
1501 /* We want to avoid the working directory if our caller
1502 * doesn't need the data in a normal file, this system
1503 * is rather slow with its stat/open/mmap/close syscalls,
1504 * and the object is contained in a pack file. The pack
1505 * is probably already open and will be faster to obtain
1506 * the data through than the working directory. Loose
1507 * objects however would tend to be slower as they need
1508 * to be individually opened and inflated.
1510 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1514 pos
= cache_name_pos(name
, len
);
1517 ce
= active_cache
[pos
];
1518 if ((lstat(name
, &st
) < 0) ||
1519 !S_ISREG(st
.st_mode
) || /* careful! */
1520 ce_match_stat(ce
, &st
, 0) ||
1521 hashcmp(sha1
, ce
->sha1
))
1523 /* we return 1 only when we can stat, it is a regular file,
1524 * stat information matches, and sha1 recorded in the cache
1525 * matches. I.e. we know the file in the work tree really is
1526 * the same as the <name, sha1> pair.
1531 static int populate_from_stdin(struct diff_filespec
*s
)
1533 #define INCREMENT 1024
1541 buf
= xrealloc(buf
, size
+ INCREMENT
);
1542 got
= xread(0, buf
+ size
, INCREMENT
);
1546 return error("error while reading from stdin %s",
1550 s
->should_munmap
= 0;
1557 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1560 char *data
= xmalloc(100);
1561 len
= snprintf(data
, 100,
1562 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1574 * While doing rename detection and pickaxe operation, we may need to
1575 * grab the data for the blob (or file) for our own in-core comparison.
1576 * diff_filespec has data and size fields for this purpose.
1578 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1581 if (!DIFF_FILE_VALID(s
))
1582 die("internal error: asking to populate invalid file.");
1583 if (S_ISDIR(s
->mode
))
1589 if (size_only
&& 0 < s
->size
)
1592 if (S_ISGITLINK(s
->mode
))
1593 return diff_populate_gitlink(s
, size_only
);
1595 if (!s
->sha1_valid
||
1596 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1602 if (!strcmp(s
->path
, "-"))
1603 return populate_from_stdin(s
);
1605 if (lstat(s
->path
, &st
) < 0) {
1606 if (errno
== ENOENT
) {
1610 s
->data
= (char *)"";
1615 s
->size
= xsize_t(st
.st_size
);
1620 if (S_ISLNK(st
.st_mode
)) {
1622 s
->data
= xmalloc(s
->size
);
1624 ret
= readlink(s
->path
, s
->data
, s
->size
);
1631 fd
= open(s
->path
, O_RDONLY
);
1634 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1636 s
->should_munmap
= 1;
1639 * Convert from working tree format to canonical git format
1642 buf
= convert_to_git(s
->path
, s
->data
, &size
);
1644 munmap(s
->data
, s
->size
);
1645 s
->should_munmap
= 0;
1652 enum object_type type
;
1654 type
= sha1_object_info(s
->sha1
, &s
->size
);
1656 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1663 void diff_free_filespec_data(struct diff_filespec
*s
)
1667 else if (s
->should_munmap
)
1668 munmap(s
->data
, s
->size
);
1670 if (s
->should_free
|| s
->should_munmap
) {
1671 s
->should_free
= s
->should_munmap
= 0;
1678 static void prep_temp_blob(struct diff_tempfile
*temp
,
1681 const unsigned char *sha1
,
1686 fd
= git_mkstemp(temp
->tmp_path
, PATH_MAX
, ".diff_XXXXXX");
1688 die("unable to create temp-file");
1689 if (write_in_full(fd
, blob
, size
) != size
)
1690 die("unable to write temp-file");
1692 temp
->name
= temp
->tmp_path
;
1693 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1695 sprintf(temp
->mode
, "%06o", mode
);
1698 static void prepare_temp_file(const char *name
,
1699 struct diff_tempfile
*temp
,
1700 struct diff_filespec
*one
)
1702 if (!DIFF_FILE_VALID(one
)) {
1704 /* A '-' entry produces this for file-2, and
1705 * a '+' entry produces this for file-1.
1707 temp
->name
= "/dev/null";
1708 strcpy(temp
->hex
, ".");
1709 strcpy(temp
->mode
, ".");
1713 if (!one
->sha1_valid
||
1714 reuse_worktree_file(name
, one
->sha1
, 1)) {
1716 if (lstat(name
, &st
) < 0) {
1717 if (errno
== ENOENT
)
1718 goto not_a_valid_file
;
1719 die("stat(%s): %s", name
, strerror(errno
));
1721 if (S_ISLNK(st
.st_mode
)) {
1723 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1724 size_t sz
= xsize_t(st
.st_size
);
1725 if (sizeof(buf
) <= st
.st_size
)
1726 die("symlink too long: %s", name
);
1727 ret
= readlink(name
, buf
, sz
);
1729 die("readlink(%s)", name
);
1730 prep_temp_blob(temp
, buf
, sz
,
1732 one
->sha1
: null_sha1
),
1734 one
->mode
: S_IFLNK
));
1737 /* we can borrow from the file in the work tree */
1739 if (!one
->sha1_valid
)
1740 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1742 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1743 /* Even though we may sometimes borrow the
1744 * contents from the work tree, we always want
1745 * one->mode. mode is trustworthy even when
1746 * !(one->sha1_valid), as long as
1747 * DIFF_FILE_VALID(one).
1749 sprintf(temp
->mode
, "%06o", one
->mode
);
1754 if (diff_populate_filespec(one
, 0))
1755 die("cannot read data blob for %s", one
->path
);
1756 prep_temp_blob(temp
, one
->data
, one
->size
,
1757 one
->sha1
, one
->mode
);
1761 static void remove_tempfile(void)
1765 for (i
= 0; i
< 2; i
++)
1766 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1767 unlink(diff_temp
[i
].name
);
1768 diff_temp
[i
].name
= NULL
;
1772 static void remove_tempfile_on_signal(int signo
)
1775 signal(SIGINT
, SIG_DFL
);
1779 static int spawn_prog(const char *pgm
, const char **arg
)
1787 die("unable to fork");
1789 execvp(pgm
, (char *const*) arg
);
1793 while (waitpid(pid
, &status
, 0) < 0) {
1799 /* Earlier we did not check the exit status because
1800 * diff exits non-zero if files are different, and
1801 * we are not interested in knowing that. It was a
1802 * mistake which made it harder to quit a diff-*
1803 * session that uses the git-apply-patch-script as
1804 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1805 * should also exit non-zero only when it wants to
1806 * abort the entire diff-* session.
1808 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1813 /* An external diff command takes:
1815 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1816 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1819 static void run_external_diff(const char *pgm
,
1822 struct diff_filespec
*one
,
1823 struct diff_filespec
*two
,
1824 const char *xfrm_msg
,
1825 int complete_rewrite
)
1827 const char *spawn_arg
[10];
1828 struct diff_tempfile
*temp
= diff_temp
;
1830 static int atexit_asked
= 0;
1831 const char *othername
;
1832 const char **arg
= &spawn_arg
[0];
1834 othername
= (other
? other
: name
);
1836 prepare_temp_file(name
, &temp
[0], one
);
1837 prepare_temp_file(othername
, &temp
[1], two
);
1838 if (! atexit_asked
&&
1839 (temp
[0].name
== temp
[0].tmp_path
||
1840 temp
[1].name
== temp
[1].tmp_path
)) {
1842 atexit(remove_tempfile
);
1844 signal(SIGINT
, remove_tempfile_on_signal
);
1850 *arg
++ = temp
[0].name
;
1851 *arg
++ = temp
[0].hex
;
1852 *arg
++ = temp
[0].mode
;
1853 *arg
++ = temp
[1].name
;
1854 *arg
++ = temp
[1].hex
;
1855 *arg
++ = temp
[1].mode
;
1865 retval
= spawn_prog(pgm
, spawn_arg
);
1868 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1873 static const char *external_diff_attr(const char *name
)
1875 struct git_attr_check attr_diff_check
;
1877 setup_diff_attr_check(&attr_diff_check
);
1878 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
1879 const char *value
= attr_diff_check
.value
;
1880 if (!ATTR_TRUE(value
) &&
1881 !ATTR_FALSE(value
) &&
1882 !ATTR_UNSET(value
)) {
1883 struct ll_diff_driver
*drv
;
1885 read_config_if_needed();
1886 for (drv
= user_diff
; drv
; drv
= drv
->next
)
1887 if (!strcmp(drv
->name
, value
))
1894 static void run_diff_cmd(const char *pgm
,
1897 struct diff_filespec
*one
,
1898 struct diff_filespec
*two
,
1899 const char *xfrm_msg
,
1900 struct diff_options
*o
,
1901 int complete_rewrite
)
1903 if (!o
->allow_external
)
1906 const char *cmd
= external_diff_attr(name
);
1912 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1917 builtin_diff(name
, other
? other
: name
,
1918 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1920 printf("* Unmerged path %s\n", name
);
1923 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1925 if (DIFF_FILE_VALID(one
)) {
1926 if (!one
->sha1_valid
) {
1928 if (!strcmp(one
->path
, "-")) {
1929 hashcpy(one
->sha1
, null_sha1
);
1932 if (lstat(one
->path
, &st
) < 0)
1933 die("stat %s", one
->path
);
1934 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1935 die("cannot hash %s\n", one
->path
);
1942 static int similarity_index(struct diff_filepair
*p
)
1944 return p
->score
* 100 / MAX_SCORE
;
1947 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1949 const char *pgm
= external_diff();
1950 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1951 struct diff_filespec
*one
;
1952 struct diff_filespec
*two
;
1955 char *name_munged
, *other_munged
;
1956 int complete_rewrite
= 0;
1959 if (DIFF_PAIR_UNMERGED(p
)) {
1961 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1965 name
= p
->one
->path
;
1966 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1967 name_munged
= quote_one(name
);
1968 other_munged
= quote_one(other
);
1969 one
= p
->one
; two
= p
->two
;
1971 diff_fill_sha1_info(one
);
1972 diff_fill_sha1_info(two
);
1975 switch (p
->status
) {
1976 case DIFF_STATUS_COPIED
:
1977 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1978 "similarity index %d%%\n"
1981 similarity_index(p
), name_munged
, other_munged
);
1983 case DIFF_STATUS_RENAMED
:
1984 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1985 "similarity index %d%%\n"
1988 similarity_index(p
), name_munged
, other_munged
);
1990 case DIFF_STATUS_MODIFIED
:
1992 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1993 "dissimilarity index %d%%\n",
1994 similarity_index(p
));
1995 complete_rewrite
= 1;
2004 if (hashcmp(one
->sha1
, two
->sha1
)) {
2005 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
2009 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2010 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2013 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2015 abbrev
, sha1_to_hex(one
->sha1
),
2016 abbrev
, sha1_to_hex(two
->sha1
));
2017 if (one
->mode
== two
->mode
)
2018 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2019 " %06o", one
->mode
);
2020 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
2025 xfrm_msg
= len
? msg
: NULL
;
2028 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2029 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2030 /* a filepair that changes between file and symlink
2031 * needs to be split into deletion and creation.
2033 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2034 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
2036 null
= alloc_filespec(one
->path
);
2037 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
2041 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
2048 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2049 struct diffstat_t
*diffstat
)
2053 int complete_rewrite
= 0;
2055 if (DIFF_PAIR_UNMERGED(p
)) {
2057 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2061 name
= p
->one
->path
;
2062 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2064 diff_fill_sha1_info(p
->one
);
2065 diff_fill_sha1_info(p
->two
);
2067 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2068 complete_rewrite
= 1;
2069 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2072 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2077 if (DIFF_PAIR_UNMERGED(p
)) {
2082 name
= p
->one
->path
;
2083 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2085 diff_fill_sha1_info(p
->one
);
2086 diff_fill_sha1_info(p
->two
);
2088 builtin_checkdiff(name
, other
, p
->one
, p
->two
, o
);
2091 void diff_setup(struct diff_options
*options
)
2093 memset(options
, 0, sizeof(*options
));
2094 options
->line_termination
= '\n';
2095 options
->break_opt
= -1;
2096 options
->rename_limit
= -1;
2097 options
->context
= 3;
2098 options
->msg_sep
= "";
2100 options
->change
= diff_change
;
2101 options
->add_remove
= diff_addremove
;
2102 options
->color_diff
= diff_use_color_default
;
2103 options
->detect_rename
= diff_detect_rename_default
;
2106 int diff_setup_done(struct diff_options
*options
)
2110 if (options
->output_format
& DIFF_FORMAT_NAME
)
2112 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2114 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2116 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2119 die("--name-only, --name-status, --check and -s are mutually exclusive");
2121 if (options
->find_copies_harder
)
2122 options
->detect_rename
= DIFF_DETECT_COPY
;
2124 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2125 DIFF_FORMAT_NAME_STATUS
|
2126 DIFF_FORMAT_CHECKDIFF
|
2127 DIFF_FORMAT_NO_OUTPUT
))
2128 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2129 DIFF_FORMAT_NUMSTAT
|
2130 DIFF_FORMAT_DIFFSTAT
|
2131 DIFF_FORMAT_SHORTSTAT
|
2132 DIFF_FORMAT_SUMMARY
|
2136 * These cases always need recursive; we do not drop caller-supplied
2137 * recursive bits for other formats here.
2139 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2140 DIFF_FORMAT_NUMSTAT
|
2141 DIFF_FORMAT_DIFFSTAT
|
2142 DIFF_FORMAT_SHORTSTAT
|
2143 DIFF_FORMAT_SUMMARY
|
2144 DIFF_FORMAT_CHECKDIFF
))
2145 options
->recursive
= 1;
2147 * Also pickaxe would not work very well if you do not say recursive
2149 if (options
->pickaxe
)
2150 options
->recursive
= 1;
2152 if (options
->detect_rename
&& options
->rename_limit
< 0)
2153 options
->rename_limit
= diff_rename_limit_default
;
2154 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2156 /* read-cache does not die even when it fails
2157 * so it is safe for us to do this here. Also
2158 * it does not smudge active_cache or active_nr
2159 * when it fails, so we do not have to worry about
2160 * cleaning it up ourselves either.
2164 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2165 options
->abbrev
= 40; /* full */
2168 * It does not make sense to show the first hit we happened
2169 * to have found. It does not make sense not to return with
2170 * exit code in such a case either.
2172 if (options
->quiet
) {
2173 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2174 options
->exit_with_status
= 1;
2178 * If we postprocess in diffcore, we cannot simply return
2179 * upon the first hit. We need to run diff as usual.
2181 if (options
->pickaxe
|| options
->filter
)
2187 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2197 if (c
== arg_short
) {
2201 if (val
&& isdigit(c
)) {
2203 int n
= strtoul(arg
, &end
, 10);
2214 eq
= strchr(arg
, '=');
2219 if (!len
|| strncmp(arg
, arg_long
, len
))
2224 if (!isdigit(*++eq
))
2226 n
= strtoul(eq
, &end
, 10);
2234 static int diff_scoreopt_parse(const char *opt
);
2236 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2238 const char *arg
= av
[0];
2239 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2240 options
->output_format
|= DIFF_FORMAT_PATCH
;
2241 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2242 options
->output_format
|= DIFF_FORMAT_PATCH
;
2243 else if (!strcmp(arg
, "--raw"))
2244 options
->output_format
|= DIFF_FORMAT_RAW
;
2245 else if (!strcmp(arg
, "--patch-with-raw")) {
2246 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2248 else if (!strcmp(arg
, "--numstat")) {
2249 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2251 else if (!strcmp(arg
, "--shortstat")) {
2252 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2254 else if (!prefixcmp(arg
, "--stat")) {
2256 int width
= options
->stat_width
;
2257 int name_width
= options
->stat_name_width
;
2263 if (!prefixcmp(arg
, "-width="))
2264 width
= strtoul(arg
+ 7, &end
, 10);
2265 else if (!prefixcmp(arg
, "-name-width="))
2266 name_width
= strtoul(arg
+ 12, &end
, 10);
2269 width
= strtoul(arg
+1, &end
, 10);
2271 name_width
= strtoul(end
+1, &end
, 10);
2274 /* Important! This checks all the error cases! */
2277 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2278 options
->stat_name_width
= name_width
;
2279 options
->stat_width
= width
;
2281 else if (!strcmp(arg
, "--check"))
2282 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2283 else if (!strcmp(arg
, "--summary"))
2284 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2285 else if (!strcmp(arg
, "--patch-with-stat")) {
2286 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2288 else if (!strcmp(arg
, "-z"))
2289 options
->line_termination
= 0;
2290 else if (!prefixcmp(arg
, "-l"))
2291 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2292 else if (!strcmp(arg
, "--full-index"))
2293 options
->full_index
= 1;
2294 else if (!strcmp(arg
, "--binary")) {
2295 options
->output_format
|= DIFF_FORMAT_PATCH
;
2296 options
->binary
= 1;
2298 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
2301 else if (!strcmp(arg
, "--name-only"))
2302 options
->output_format
|= DIFF_FORMAT_NAME
;
2303 else if (!strcmp(arg
, "--name-status"))
2304 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2305 else if (!strcmp(arg
, "-R"))
2306 options
->reverse_diff
= 1;
2307 else if (!prefixcmp(arg
, "-S"))
2308 options
->pickaxe
= arg
+ 2;
2309 else if (!strcmp(arg
, "-s")) {
2310 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2312 else if (!prefixcmp(arg
, "-O"))
2313 options
->orderfile
= arg
+ 2;
2314 else if (!prefixcmp(arg
, "--diff-filter="))
2315 options
->filter
= arg
+ 14;
2316 else if (!strcmp(arg
, "--pickaxe-all"))
2317 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2318 else if (!strcmp(arg
, "--pickaxe-regex"))
2319 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2320 else if (!prefixcmp(arg
, "-B")) {
2321 if ((options
->break_opt
=
2322 diff_scoreopt_parse(arg
)) == -1)
2325 else if (!prefixcmp(arg
, "-M")) {
2326 if ((options
->rename_score
=
2327 diff_scoreopt_parse(arg
)) == -1)
2329 options
->detect_rename
= DIFF_DETECT_RENAME
;
2331 else if (!prefixcmp(arg
, "-C")) {
2332 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2333 options
->find_copies_harder
= 1;
2334 if ((options
->rename_score
=
2335 diff_scoreopt_parse(arg
)) == -1)
2337 options
->detect_rename
= DIFF_DETECT_COPY
;
2339 else if (!strcmp(arg
, "--find-copies-harder"))
2340 options
->find_copies_harder
= 1;
2341 else if (!strcmp(arg
, "--follow"))
2342 options
->follow_renames
= 1;
2343 else if (!strcmp(arg
, "--abbrev"))
2344 options
->abbrev
= DEFAULT_ABBREV
;
2345 else if (!prefixcmp(arg
, "--abbrev=")) {
2346 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2347 if (options
->abbrev
< MINIMUM_ABBREV
)
2348 options
->abbrev
= MINIMUM_ABBREV
;
2349 else if (40 < options
->abbrev
)
2350 options
->abbrev
= 40;
2352 else if (!strcmp(arg
, "--color"))
2353 options
->color_diff
= 1;
2354 else if (!strcmp(arg
, "--no-color"))
2355 options
->color_diff
= 0;
2356 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2357 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2358 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2359 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2360 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2361 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2362 else if (!strcmp(arg
, "--color-words"))
2363 options
->color_diff
= options
->color_diff_words
= 1;
2364 else if (!strcmp(arg
, "--no-renames"))
2365 options
->detect_rename
= 0;
2366 else if (!strcmp(arg
, "--exit-code"))
2367 options
->exit_with_status
= 1;
2368 else if (!strcmp(arg
, "--quiet"))
2370 else if (!strcmp(arg
, "--ext-diff"))
2371 options
->allow_external
= 1;
2372 else if (!strcmp(arg
, "--no-ext-diff"))
2373 options
->allow_external
= 0;
2379 static int parse_num(const char **cp_p
)
2381 unsigned long num
, scale
;
2383 const char *cp
= *cp_p
;
2390 if ( !dot
&& ch
== '.' ) {
2393 } else if ( ch
== '%' ) {
2394 scale
= dot
? scale
*100 : 100;
2395 cp
++; /* % is always at the end */
2397 } else if ( ch
>= '0' && ch
<= '9' ) {
2398 if ( scale
< 100000 ) {
2400 num
= (num
*10) + (ch
-'0');
2409 /* user says num divided by scale and we say internally that
2410 * is MAX_SCORE * num / scale.
2412 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2415 static int diff_scoreopt_parse(const char *opt
)
2417 int opt1
, opt2
, cmd
;
2422 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2423 return -1; /* that is not a -M, -C nor -B option */
2425 opt1
= parse_num(&opt
);
2431 else if (*opt
!= '/')
2432 return -1; /* we expect -B80/99 or -B80 */
2435 opt2
= parse_num(&opt
);
2440 return opt1
| (opt2
<< 16);
2443 struct diff_queue_struct diff_queued_diff
;
2445 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2447 if (queue
->alloc
<= queue
->nr
) {
2448 queue
->alloc
= alloc_nr(queue
->alloc
);
2449 queue
->queue
= xrealloc(queue
->queue
,
2450 sizeof(dp
) * queue
->alloc
);
2452 queue
->queue
[queue
->nr
++] = dp
;
2455 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2456 struct diff_filespec
*one
,
2457 struct diff_filespec
*two
)
2459 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2467 void diff_free_filepair(struct diff_filepair
*p
)
2469 diff_free_filespec_data(p
->one
);
2470 diff_free_filespec_data(p
->two
);
2476 /* This is different from find_unique_abbrev() in that
2477 * it stuffs the result with dots for alignment.
2479 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2484 return sha1_to_hex(sha1
);
2486 abbrev
= find_unique_abbrev(sha1
, len
);
2488 return sha1_to_hex(sha1
);
2489 abblen
= strlen(abbrev
);
2491 static char hex
[41];
2492 if (len
< abblen
&& abblen
<= len
+ 2)
2493 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2495 sprintf(hex
, "%s...", abbrev
);
2498 return sha1_to_hex(sha1
);
2501 static void diff_flush_raw(struct diff_filepair
*p
,
2502 struct diff_options
*options
)
2506 int abbrev
= options
->abbrev
;
2507 const char *path_one
, *path_two
;
2508 int inter_name_termination
= '\t';
2509 int line_termination
= options
->line_termination
;
2511 if (!line_termination
)
2512 inter_name_termination
= 0;
2514 path_one
= p
->one
->path
;
2515 path_two
= p
->two
->path
;
2516 if (line_termination
) {
2517 path_one
= quote_one(path_one
);
2518 path_two
= quote_one(path_two
);
2522 sprintf(status
, "%c%03d", p
->status
, similarity_index(p
));
2524 status
[0] = p
->status
;
2527 switch (p
->status
) {
2528 case DIFF_STATUS_COPIED
:
2529 case DIFF_STATUS_RENAMED
:
2532 case DIFF_STATUS_ADDED
:
2533 case DIFF_STATUS_DELETED
:
2540 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2541 printf(":%06o %06o %s ",
2542 p
->one
->mode
, p
->two
->mode
,
2543 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2545 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2547 printf("%s%c%s", status
, inter_name_termination
,
2548 two_paths
|| p
->one
->mode
? path_one
: path_two
);
2550 printf("%c%s", inter_name_termination
, path_two
);
2551 putchar(line_termination
);
2552 if (path_one
!= p
->one
->path
)
2553 free((void*)path_one
);
2554 if (path_two
!= p
->two
->path
)
2555 free((void*)path_two
);
2558 static void diff_flush_name(struct diff_filepair
*p
, struct diff_options
*opt
)
2560 char *path
= p
->two
->path
;
2562 if (opt
->line_termination
)
2563 path
= quote_one(p
->two
->path
);
2564 printf("%s%c", path
, opt
->line_termination
);
2565 if (p
->two
->path
!= path
)
2569 int diff_unmodified_pair(struct diff_filepair
*p
)
2571 /* This function is written stricter than necessary to support
2572 * the currently implemented transformers, but the idea is to
2573 * let transformers to produce diff_filepairs any way they want,
2574 * and filter and clean them up here before producing the output.
2576 struct diff_filespec
*one
, *two
;
2578 if (DIFF_PAIR_UNMERGED(p
))
2579 return 0; /* unmerged is interesting */
2584 /* deletion, addition, mode or type change
2585 * and rename are all interesting.
2587 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2588 DIFF_PAIR_MODE_CHANGED(p
) ||
2589 strcmp(one
->path
, two
->path
))
2592 /* both are valid and point at the same path. that is, we are
2593 * dealing with a change.
2595 if (one
->sha1_valid
&& two
->sha1_valid
&&
2596 !hashcmp(one
->sha1
, two
->sha1
))
2597 return 1; /* no change */
2598 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2599 return 1; /* both look at the same file on the filesystem. */
2603 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2605 if (diff_unmodified_pair(p
))
2608 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2609 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2610 return; /* no tree diffs in patch format */
2615 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2616 struct diffstat_t
*diffstat
)
2618 if (diff_unmodified_pair(p
))
2621 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2622 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2623 return; /* no tree diffs in patch format */
2625 run_diffstat(p
, o
, diffstat
);
2628 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2629 struct diff_options
*o
)
2631 if (diff_unmodified_pair(p
))
2634 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2635 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2636 return; /* no tree diffs in patch format */
2638 run_checkdiff(p
, o
);
2641 int diff_queue_is_empty(void)
2643 struct diff_queue_struct
*q
= &diff_queued_diff
;
2645 for (i
= 0; i
< q
->nr
; i
++)
2646 if (!diff_unmodified_pair(q
->queue
[i
]))
2652 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2654 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2657 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2659 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2660 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2662 s
->size
, s
->xfrm_flags
);
2665 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2667 diff_debug_filespec(p
->one
, i
, "one");
2668 diff_debug_filespec(p
->two
, i
, "two");
2669 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2670 p
->score
, p
->status
? p
->status
: '?',
2671 p
->source_stays
, p
->broken_pair
);
2674 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2678 fprintf(stderr
, "%s\n", msg
);
2679 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2680 for (i
= 0; i
< q
->nr
; i
++) {
2681 struct diff_filepair
*p
= q
->queue
[i
];
2682 diff_debug_filepair(p
, i
);
2687 static void diff_resolve_rename_copy(void)
2690 struct diff_filepair
*p
, *pp
;
2691 struct diff_queue_struct
*q
= &diff_queued_diff
;
2693 diff_debug_queue("resolve-rename-copy", q
);
2695 for (i
= 0; i
< q
->nr
; i
++) {
2697 p
->status
= 0; /* undecided */
2698 if (DIFF_PAIR_UNMERGED(p
))
2699 p
->status
= DIFF_STATUS_UNMERGED
;
2700 else if (!DIFF_FILE_VALID(p
->one
))
2701 p
->status
= DIFF_STATUS_ADDED
;
2702 else if (!DIFF_FILE_VALID(p
->two
))
2703 p
->status
= DIFF_STATUS_DELETED
;
2704 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2705 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2707 /* from this point on, we are dealing with a pair
2708 * whose both sides are valid and of the same type, i.e.
2709 * either in-place edit or rename/copy edit.
2711 else if (DIFF_PAIR_RENAME(p
)) {
2712 if (p
->source_stays
) {
2713 p
->status
= DIFF_STATUS_COPIED
;
2716 /* See if there is some other filepair that
2717 * copies from the same source as us. If so
2718 * we are a copy. Otherwise we are either a
2719 * copy if the path stays, or a rename if it
2720 * does not, but we already handled "stays" case.
2722 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2724 if (strcmp(pp
->one
->path
, p
->one
->path
))
2725 continue; /* not us */
2726 if (!DIFF_PAIR_RENAME(pp
))
2727 continue; /* not a rename/copy */
2728 /* pp is a rename/copy from the same source */
2729 p
->status
= DIFF_STATUS_COPIED
;
2733 p
->status
= DIFF_STATUS_RENAMED
;
2735 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2736 p
->one
->mode
!= p
->two
->mode
||
2737 is_null_sha1(p
->one
->sha1
))
2738 p
->status
= DIFF_STATUS_MODIFIED
;
2740 /* This is a "no-change" entry and should not
2741 * happen anymore, but prepare for broken callers.
2743 error("feeding unmodified %s to diffcore",
2745 p
->status
= DIFF_STATUS_UNKNOWN
;
2748 diff_debug_queue("resolve-rename-copy done", q
);
2751 static int check_pair_status(struct diff_filepair
*p
)
2753 switch (p
->status
) {
2754 case DIFF_STATUS_UNKNOWN
:
2757 die("internal error in diff-resolve-rename-copy");
2763 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2765 int fmt
= opt
->output_format
;
2767 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2768 diff_flush_checkdiff(p
, opt
);
2769 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2770 diff_flush_raw(p
, opt
);
2771 else if (fmt
& DIFF_FORMAT_NAME
)
2772 diff_flush_name(p
, opt
);
2775 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2777 char *name
= quote_one(fs
->path
);
2779 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, name
);
2781 printf(" %s %s\n", newdelete
, name
);
2786 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2788 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2790 char *name
= quote_one(p
->two
->path
);
2791 printf(" mode change %06o => %06o %s\n",
2792 p
->one
->mode
, p
->two
->mode
, name
);
2796 printf(" mode change %06o => %06o\n",
2797 p
->one
->mode
, p
->two
->mode
);
2801 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2803 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
2805 printf(" %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
2807 show_mode_change(p
, 0);
2810 static void diff_summary(struct diff_filepair
*p
)
2813 case DIFF_STATUS_DELETED
:
2814 show_file_mode_name("delete", p
->one
);
2816 case DIFF_STATUS_ADDED
:
2817 show_file_mode_name("create", p
->two
);
2819 case DIFF_STATUS_COPIED
:
2820 show_rename_copy("copy", p
);
2822 case DIFF_STATUS_RENAMED
:
2823 show_rename_copy("rename", p
);
2827 char *name
= quote_one(p
->two
->path
);
2828 printf(" rewrite %s (%d%%)\n", name
,
2829 similarity_index(p
));
2831 show_mode_change(p
, 0);
2832 } else show_mode_change(p
, 1);
2838 struct xdiff_emit_state xm
;
2843 static int remove_space(char *line
, int len
)
2849 for (i
= 0; i
< len
; i
++)
2850 if (!isspace((c
= line
[i
])))
2856 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2858 struct patch_id_t
*data
= priv
;
2861 /* Ignore line numbers when computing the SHA1 of the patch */
2862 if (!prefixcmp(line
, "@@ -"))
2865 new_len
= remove_space(line
, len
);
2867 SHA1_Update(data
->ctx
, line
, new_len
);
2868 data
->patchlen
+= new_len
;
2871 /* returns 0 upon success, and writes result into sha1 */
2872 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2874 struct diff_queue_struct
*q
= &diff_queued_diff
;
2877 struct patch_id_t data
;
2878 char buffer
[PATH_MAX
* 4 + 20];
2881 memset(&data
, 0, sizeof(struct patch_id_t
));
2883 data
.xm
.consume
= patch_id_consume
;
2885 for (i
= 0; i
< q
->nr
; i
++) {
2890 struct diff_filepair
*p
= q
->queue
[i
];
2893 memset(&xecfg
, 0, sizeof(xecfg
));
2895 return error("internal diff status error");
2896 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2898 if (diff_unmodified_pair(p
))
2900 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2901 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2903 if (DIFF_PAIR_UNMERGED(p
))
2906 diff_fill_sha1_info(p
->one
);
2907 diff_fill_sha1_info(p
->two
);
2908 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2909 fill_mmfile(&mf2
, p
->two
) < 0)
2910 return error("unable to read files to diff");
2912 /* Maybe hash p->two? into the patch id? */
2913 if (diff_filespec_is_binary(p
->two
))
2916 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2917 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2918 if (p
->one
->mode
== 0)
2919 len1
= snprintf(buffer
, sizeof(buffer
),
2920 "diff--gita/%.*sb/%.*s"
2927 len2
, p
->two
->path
);
2928 else if (p
->two
->mode
== 0)
2929 len1
= snprintf(buffer
, sizeof(buffer
),
2930 "diff--gita/%.*sb/%.*s"
2931 "deletedfilemode%06o"
2937 len1
, p
->one
->path
);
2939 len1
= snprintf(buffer
, sizeof(buffer
),
2940 "diff--gita/%.*sb/%.*s"
2946 len2
, p
->two
->path
);
2947 SHA1_Update(&ctx
, buffer
, len1
);
2949 xpp
.flags
= XDF_NEED_MINIMAL
;
2951 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2952 ecb
.outf
= xdiff_outf
;
2954 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2957 SHA1_Final(sha1
, &ctx
);
2961 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2963 struct diff_queue_struct
*q
= &diff_queued_diff
;
2965 int result
= diff_get_patch_id(options
, sha1
);
2967 for (i
= 0; i
< q
->nr
; i
++)
2968 diff_free_filepair(q
->queue
[i
]);
2972 q
->nr
= q
->alloc
= 0;
2977 static int is_summary_empty(const struct diff_queue_struct
*q
)
2981 for (i
= 0; i
< q
->nr
; i
++) {
2982 const struct diff_filepair
*p
= q
->queue
[i
];
2984 switch (p
->status
) {
2985 case DIFF_STATUS_DELETED
:
2986 case DIFF_STATUS_ADDED
:
2987 case DIFF_STATUS_COPIED
:
2988 case DIFF_STATUS_RENAMED
:
2993 if (p
->one
->mode
&& p
->two
->mode
&&
2994 p
->one
->mode
!= p
->two
->mode
)
3002 void diff_flush(struct diff_options
*options
)
3004 struct diff_queue_struct
*q
= &diff_queued_diff
;
3005 int i
, output_format
= options
->output_format
;
3009 * Order: raw, stat, summary, patch
3010 * or: name/name-status/checkdiff (other bits clear)
3015 if (output_format
& (DIFF_FORMAT_RAW
|
3017 DIFF_FORMAT_NAME_STATUS
|
3018 DIFF_FORMAT_CHECKDIFF
)) {
3019 for (i
= 0; i
< q
->nr
; i
++) {
3020 struct diff_filepair
*p
= q
->queue
[i
];
3021 if (check_pair_status(p
))
3022 flush_one_pair(p
, options
);
3027 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3028 struct diffstat_t diffstat
;
3030 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3031 diffstat
.xm
.consume
= diffstat_consume
;
3032 for (i
= 0; i
< q
->nr
; i
++) {
3033 struct diff_filepair
*p
= q
->queue
[i
];
3034 if (check_pair_status(p
))
3035 diff_flush_stat(p
, options
, &diffstat
);
3037 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3038 show_numstat(&diffstat
, options
);
3039 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3040 show_stats(&diffstat
, options
);
3041 else if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3042 show_shortstats(&diffstat
);
3046 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3047 for (i
= 0; i
< q
->nr
; i
++)
3048 diff_summary(q
->queue
[i
]);
3052 if (output_format
& DIFF_FORMAT_PATCH
) {
3054 if (options
->stat_sep
) {
3055 /* attach patch instead of inline */
3056 fputs(options
->stat_sep
, stdout
);
3058 putchar(options
->line_termination
);
3062 for (i
= 0; i
< q
->nr
; i
++) {
3063 struct diff_filepair
*p
= q
->queue
[i
];
3064 if (check_pair_status(p
))
3065 diff_flush_patch(p
, options
);
3069 if (output_format
& DIFF_FORMAT_CALLBACK
)
3070 options
->format_callback(q
, options
, options
->format_callback_data
);
3072 for (i
= 0; i
< q
->nr
; i
++)
3073 diff_free_filepair(q
->queue
[i
]);
3077 q
->nr
= q
->alloc
= 0;
3080 static void diffcore_apply_filter(const char *filter
)
3083 struct diff_queue_struct
*q
= &diff_queued_diff
;
3084 struct diff_queue_struct outq
;
3086 outq
.nr
= outq
.alloc
= 0;
3091 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3093 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3094 struct diff_filepair
*p
= q
->queue
[i
];
3095 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3097 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3099 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3100 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3101 strchr(filter
, p
->status
)))
3107 /* otherwise we will clear the whole queue
3108 * by copying the empty outq at the end of this
3109 * function, but first clear the current entries
3112 for (i
= 0; i
< q
->nr
; i
++)
3113 diff_free_filepair(q
->queue
[i
]);
3116 /* Only the matching ones */
3117 for (i
= 0; i
< q
->nr
; i
++) {
3118 struct diff_filepair
*p
= q
->queue
[i
];
3120 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3122 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3124 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3125 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3126 strchr(filter
, p
->status
)))
3129 diff_free_filepair(p
);
3136 void diffcore_std(struct diff_options
*options
)
3141 if (options
->break_opt
!= -1)
3142 diffcore_break(options
->break_opt
);
3143 if (options
->detect_rename
)
3144 diffcore_rename(options
);
3145 if (options
->break_opt
!= -1)
3146 diffcore_merge_broken();
3147 if (options
->pickaxe
)
3148 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3149 if (options
->orderfile
)
3150 diffcore_order(options
->orderfile
);
3151 diff_resolve_rename_copy();
3152 diffcore_apply_filter(options
->filter
);
3154 options
->has_changes
= !!diff_queued_diff
.nr
;
3158 void diff_addremove(struct diff_options
*options
,
3159 int addremove
, unsigned mode
,
3160 const unsigned char *sha1
,
3161 const char *base
, const char *path
)
3163 char concatpath
[PATH_MAX
];
3164 struct diff_filespec
*one
, *two
;
3166 /* This may look odd, but it is a preparation for
3167 * feeding "there are unchanged files which should
3168 * not produce diffs, but when you are doing copy
3169 * detection you would need them, so here they are"
3170 * entries to the diff-core. They will be prefixed
3171 * with something like '=' or '*' (I haven't decided
3172 * which but should not make any difference).
3173 * Feeding the same new and old to diff_change()
3174 * also has the same effect.
3175 * Before the final output happens, they are pruned after
3176 * merged into rename/copy pairs as appropriate.
3178 if (options
->reverse_diff
)
3179 addremove
= (addremove
== '+' ? '-' :
3180 addremove
== '-' ? '+' : addremove
);
3182 if (!path
) path
= "";
3183 sprintf(concatpath
, "%s%s", base
, path
);
3184 one
= alloc_filespec(concatpath
);
3185 two
= alloc_filespec(concatpath
);
3187 if (addremove
!= '+')
3188 fill_filespec(one
, sha1
, mode
);
3189 if (addremove
!= '-')
3190 fill_filespec(two
, sha1
, mode
);
3192 diff_queue(&diff_queued_diff
, one
, two
);
3193 options
->has_changes
= 1;
3196 void diff_change(struct diff_options
*options
,
3197 unsigned old_mode
, unsigned new_mode
,
3198 const unsigned char *old_sha1
,
3199 const unsigned char *new_sha1
,
3200 const char *base
, const char *path
)
3202 char concatpath
[PATH_MAX
];
3203 struct diff_filespec
*one
, *two
;
3205 if (options
->reverse_diff
) {
3207 const unsigned char *tmp_c
;
3208 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3209 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3211 if (!path
) path
= "";
3212 sprintf(concatpath
, "%s%s", base
, path
);
3213 one
= alloc_filespec(concatpath
);
3214 two
= alloc_filespec(concatpath
);
3215 fill_filespec(one
, old_sha1
, old_mode
);
3216 fill_filespec(two
, new_sha1
, new_mode
);
3218 diff_queue(&diff_queued_diff
, one
, two
);
3219 options
->has_changes
= 1;
3222 void diff_unmerge(struct diff_options
*options
,
3224 unsigned mode
, const unsigned char *sha1
)
3226 struct diff_filespec
*one
, *two
;
3227 one
= alloc_filespec(path
);
3228 two
= alloc_filespec(path
);
3229 fill_filespec(one
, sha1
, mode
);
3230 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;