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
])
87 drv
= xcalloc(1, sizeof(struct ll_diff_driver
));
88 namebuf
= xmalloc(namelen
+ 1);
89 memcpy(namebuf
, name
, namelen
);
94 user_diff_tail
= &user_diff
;
95 *user_diff_tail
= drv
;
96 user_diff_tail
= &(drv
->next
);
100 return error("%s: lacks value", var
);
101 drv
->cmd
= strdup(value
);
106 * 'diff.<what>.funcname' attribute can be specified in the configuration
107 * to define a customized regexp to find the beginning of a function to
108 * be used for hunk header lines of "diff -p" style output.
110 static struct funcname_pattern
{
113 struct funcname_pattern
*next
;
114 } *funcname_pattern_list
;
116 static int parse_funcname_pattern(const char *var
, const char *ep
, const char *value
)
120 struct funcname_pattern
*pp
;
122 name
= var
+ 5; /* "diff." */
125 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
126 if (!strncmp(pp
->name
, name
, namelen
) && !pp
->name
[namelen
])
130 pp
= xcalloc(1, sizeof(*pp
));
131 namebuf
= xmalloc(namelen
+ 1);
132 memcpy(namebuf
, name
, namelen
);
133 namebuf
[namelen
] = 0;
135 pp
->next
= funcname_pattern_list
;
136 funcname_pattern_list
= pp
;
140 pp
->pattern
= xstrdup(value
);
145 * These are to give UI layer defaults.
146 * The core-level commands such as git-diff-files should
147 * never be affected by the setting of diff.renames
148 * the user happens to have in the configuration file.
150 int git_diff_ui_config(const char *var
, const char *value
)
152 if (!strcmp(var
, "diff.renamelimit")) {
153 diff_rename_limit_default
= git_config_int(var
, value
);
156 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
157 diff_use_color_default
= git_config_colorbool(var
, value
);
160 if (!strcmp(var
, "diff.renames")) {
162 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
163 else if (!strcasecmp(value
, "copies") ||
164 !strcasecmp(value
, "copy"))
165 diff_detect_rename_default
= DIFF_DETECT_COPY
;
166 else if (git_config_bool(var
,value
))
167 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
170 if (!strcmp(var
, "diff.autorefreshindex")) {
171 diff_auto_refresh_index
= git_config_bool(var
, value
);
174 if (!prefixcmp(var
, "diff.")) {
175 const char *ep
= strrchr(var
, '.');
178 if (!strcmp(ep
, ".command"))
179 return parse_lldiff_command(var
, ep
, value
);
180 if (!strcmp(ep
, ".funcname"))
181 return parse_funcname_pattern(var
, ep
, value
);
184 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
185 int slot
= parse_diff_color_slot(var
, 11);
186 color_parse(value
, var
, diff_colors
[slot
]);
190 return git_default_config(var
, value
);
193 static char *quote_one(const char *str
)
200 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
203 xp
= xmalloc(needlen
+ 1);
204 quote_c_style(str
, xp
, NULL
, 0);
208 static char *quote_two(const char *one
, const char *two
)
210 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
211 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
214 if (need_one
+ need_two
) {
215 if (!need_one
) need_one
= strlen(one
);
216 if (!need_two
) need_one
= strlen(two
);
218 xp
= xmalloc(need_one
+ need_two
+ 3);
220 quote_c_style(one
, xp
+ 1, NULL
, 1);
221 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
222 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
225 need_one
= strlen(one
);
226 need_two
= strlen(two
);
227 xp
= xmalloc(need_one
+ need_two
+ 1);
229 strcpy(xp
+ need_one
, two
);
233 static const char *external_diff(void)
235 static const char *external_diff_cmd
= NULL
;
236 static int done_preparing
= 0;
239 return external_diff_cmd
;
240 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
242 return external_diff_cmd
;
245 static struct diff_tempfile
{
246 const char *name
; /* filename external diff should read from */
249 char tmp_path
[PATH_MAX
];
252 static int count_lines(const char *data
, int size
)
254 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
261 completely_empty
= 0;
265 completely_empty
= 0;
268 if (completely_empty
)
271 count
++; /* no trailing newline */
275 static void print_line_count(int count
)
285 printf("1,%d", count
);
290 static void copy_file(int prefix
, const char *data
, int size
,
291 const char *set
, const char *reset
)
293 int ch
, nl_just_seen
= 1;
302 fputs(reset
, stdout
);
308 printf("%s\n\\ No newline at end of file\n", reset
);
311 static void emit_rewrite_diff(const char *name_a
,
313 struct diff_filespec
*one
,
314 struct diff_filespec
*two
,
318 const char *name_a_tab
, *name_b_tab
;
319 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
320 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
321 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
322 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
323 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
325 name_a
+= (*name_a
== '/');
326 name_b
+= (*name_b
== '/');
327 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
328 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
330 diff_populate_filespec(one
, 0);
331 diff_populate_filespec(two
, 0);
332 lc_a
= count_lines(one
->data
, one
->size
);
333 lc_b
= count_lines(two
->data
, two
->size
);
334 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
335 metainfo
, name_a
, name_a_tab
, reset
,
336 metainfo
, name_b
, name_b_tab
, reset
, fraginfo
);
337 print_line_count(lc_a
);
339 print_line_count(lc_b
);
340 printf(" @@%s\n", reset
);
342 copy_file('-', one
->data
, one
->size
, old
, reset
);
344 copy_file('+', two
->data
, two
->size
, new, reset
);
347 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
349 if (!DIFF_FILE_VALID(one
)) {
350 mf
->ptr
= (char *)""; /* does not matter */
354 else if (diff_populate_filespec(one
, 0))
357 mf
->size
= one
->size
;
361 struct diff_words_buffer
{
364 long current
; /* output pointer */
365 int suppressed_newline
;
368 static void diff_words_append(char *line
, unsigned long len
,
369 struct diff_words_buffer
*buffer
)
371 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
372 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
373 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
377 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
378 buffer
->text
.size
+= len
;
381 struct diff_words_data
{
382 struct xdiff_emit_state xm
;
383 struct diff_words_buffer minus
, plus
;
386 static void print_word(struct diff_words_buffer
*buffer
, int len
, int color
,
387 int suppress_newline
)
395 ptr
= buffer
->text
.ptr
+ buffer
->current
;
396 buffer
->current
+= len
;
398 if (ptr
[len
- 1] == '\n') {
403 fputs(diff_get_color(1, color
), stdout
);
404 fwrite(ptr
, len
, 1, stdout
);
405 fputs(diff_get_color(1, DIFF_RESET
), stdout
);
408 if (suppress_newline
)
409 buffer
->suppressed_newline
= 1;
415 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
417 struct diff_words_data
*diff_words
= priv
;
419 if (diff_words
->minus
.suppressed_newline
) {
422 diff_words
->minus
.suppressed_newline
= 0;
428 print_word(&diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
431 print_word(&diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
434 print_word(&diff_words
->plus
, len
, DIFF_PLAIN
, 0);
435 diff_words
->minus
.current
+= len
;
440 /* this executes the word diff on the accumulated buffers */
441 static void diff_words_show(struct diff_words_data
*diff_words
)
446 mmfile_t minus
, plus
;
449 memset(&xecfg
, 0, sizeof(xecfg
));
450 minus
.size
= diff_words
->minus
.text
.size
;
451 minus
.ptr
= xmalloc(minus
.size
);
452 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
453 for (i
= 0; i
< minus
.size
; i
++)
454 if (isspace(minus
.ptr
[i
]))
456 diff_words
->minus
.current
= 0;
458 plus
.size
= diff_words
->plus
.text
.size
;
459 plus
.ptr
= xmalloc(plus
.size
);
460 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
461 for (i
= 0; i
< plus
.size
; i
++)
462 if (isspace(plus
.ptr
[i
]))
464 diff_words
->plus
.current
= 0;
466 xpp
.flags
= XDF_NEED_MINIMAL
;
467 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
468 ecb
.outf
= xdiff_outf
;
469 ecb
.priv
= diff_words
;
470 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
471 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
475 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
477 if (diff_words
->minus
.suppressed_newline
) {
479 diff_words
->minus
.suppressed_newline
= 0;
483 struct emit_callback
{
484 struct xdiff_emit_state xm
;
485 int nparents
, color_diff
;
486 const char **label_path
;
487 struct diff_words_data
*diff_words
;
491 static void free_diff_words_data(struct emit_callback
*ecbdata
)
493 if (ecbdata
->diff_words
) {
495 if (ecbdata
->diff_words
->minus
.text
.size
||
496 ecbdata
->diff_words
->plus
.text
.size
)
497 diff_words_show(ecbdata
->diff_words
);
499 if (ecbdata
->diff_words
->minus
.text
.ptr
)
500 free (ecbdata
->diff_words
->minus
.text
.ptr
);
501 if (ecbdata
->diff_words
->plus
.text
.ptr
)
502 free (ecbdata
->diff_words
->plus
.text
.ptr
);
503 free(ecbdata
->diff_words
);
504 ecbdata
->diff_words
= NULL
;
508 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
511 return diff_colors
[ix
];
515 static void emit_line(const char *set
, const char *reset
, const char *line
, int len
)
517 if (len
> 0 && line
[len
-1] == '\n')
520 fwrite(line
, len
, 1, stdout
);
524 static void emit_line_with_ws(int nparents
,
525 const char *set
, const char *reset
, const char *ws
,
526 const char *line
, int len
)
529 int last_tab_in_indent
= -1;
530 int last_space_in_indent
= -1;
533 int need_highlight_leading_space
= 0;
534 /* The line is a newly added line. Does it have funny leading
535 * whitespaces? In indent, SP should never precede a TAB.
537 for (i
= col0
; i
< len
; i
++) {
538 if (line
[i
] == '\t') {
539 last_tab_in_indent
= i
;
540 if (0 <= last_space_in_indent
)
541 need_highlight_leading_space
= 1;
543 else if (line
[i
] == ' ')
544 last_space_in_indent
= i
;
549 fwrite(line
, col0
, 1, stdout
);
550 fputs(reset
, stdout
);
551 if (((i
== len
) || line
[i
] == '\n') && i
!= col0
) {
552 /* The whole line was indent */
553 emit_line(ws
, reset
, line
+ col0
, len
- col0
);
557 if (need_highlight_leading_space
) {
558 while (i
< last_tab_in_indent
) {
559 if (line
[i
] == ' ') {
562 fputs(reset
, stdout
);
570 if (line
[tail
] == '\n' && i
< tail
)
573 if (!isspace(line
[tail
]))
577 if ((i
< tail
&& line
[tail
+ 1] != '\n')) {
578 /* This has whitespace between tail+1..len */
580 fwrite(line
+ i
, tail
- i
+ 1, 1, stdout
);
581 fputs(reset
, stdout
);
582 emit_line(ws
, reset
, line
+ tail
+ 1, len
- tail
- 1);
585 emit_line(set
, reset
, line
+ i
, len
- i
);
588 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
590 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
591 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
594 emit_line(set
, reset
, line
, len
);
596 emit_line_with_ws(ecbdata
->nparents
, set
, reset
, ws
,
600 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
604 struct emit_callback
*ecbdata
= priv
;
605 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
606 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
608 *(ecbdata
->found_changesp
) = 1;
610 if (ecbdata
->label_path
[0]) {
611 const char *name_a_tab
, *name_b_tab
;
613 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
614 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
616 printf("%s--- %s%s%s\n",
617 set
, ecbdata
->label_path
[0], reset
, name_a_tab
);
618 printf("%s+++ %s%s%s\n",
619 set
, ecbdata
->label_path
[1], reset
, name_b_tab
);
620 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
623 /* This is not really necessary for now because
624 * this codepath only deals with two-way diffs.
626 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
628 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
629 ecbdata
->nparents
= i
- 1;
630 emit_line(diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
635 if (len
< ecbdata
->nparents
) {
637 emit_line(reset
, reset
, line
, len
);
642 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
643 /* fall back to normal diff */
644 free_diff_words_data(ecbdata
);
645 if (ecbdata
->diff_words
) {
646 if (line
[0] == '-') {
647 diff_words_append(line
, len
,
648 &ecbdata
->diff_words
->minus
);
650 } else if (line
[0] == '+') {
651 diff_words_append(line
, len
,
652 &ecbdata
->diff_words
->plus
);
655 if (ecbdata
->diff_words
->minus
.text
.size
||
656 ecbdata
->diff_words
->plus
.text
.size
)
657 diff_words_show(ecbdata
->diff_words
);
660 emit_line(set
, reset
, line
, len
);
663 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
665 color
= DIFF_FILE_OLD
;
666 else if (line
[i
] == '+')
667 color
= DIFF_FILE_NEW
;
670 if (color
!= DIFF_FILE_NEW
) {
671 emit_line(diff_get_color(ecbdata
->color_diff
, color
),
675 emit_add_line(reset
, ecbdata
, line
, len
);
678 static char *pprint_rename(const char *a
, const char *b
)
683 int pfx_length
, sfx_length
;
684 int len_a
= strlen(a
);
685 int len_b
= strlen(b
);
686 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
687 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
689 if (qlen_a
|| qlen_b
) {
690 if (qlen_a
) len_a
= qlen_a
;
691 if (qlen_b
) len_b
= qlen_b
;
692 name
= xmalloc( len_a
+ len_b
+ 5 );
694 quote_c_style(a
, name
, NULL
, 0);
696 memcpy(name
, a
, len_a
);
697 memcpy(name
+ len_a
, " => ", 4);
699 quote_c_style(b
, name
+ len_a
+ 4, NULL
, 0);
701 memcpy(name
+ len_a
+ 4, b
, len_b
+ 1);
705 /* Find common prefix */
707 while (*old
&& *new && *old
== *new) {
709 pfx_length
= old
- a
+ 1;
714 /* Find common suffix */
718 while (a
<= old
&& b
<= new && *old
== *new) {
720 sfx_length
= len_a
- (old
- a
);
726 * pfx{mid-a => mid-b}sfx
727 * {pfx-a => pfx-b}sfx
728 * pfx{sfx-a => sfx-b}
731 if (pfx_length
+ sfx_length
) {
732 int a_midlen
= len_a
- pfx_length
- sfx_length
;
733 int b_midlen
= len_b
- pfx_length
- sfx_length
;
734 if (a_midlen
< 0) a_midlen
= 0;
735 if (b_midlen
< 0) b_midlen
= 0;
737 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
738 sprintf(name
, "%.*s{%.*s => %.*s}%s",
740 a_midlen
, a
+ pfx_length
,
741 b_midlen
, b
+ pfx_length
,
742 a
+ len_a
- sfx_length
);
745 name
= xmalloc(len_a
+ len_b
+ 5);
746 sprintf(name
, "%s => %s", a
, b
);
752 struct xdiff_emit_state xm
;
756 struct diffstat_file
{
758 unsigned is_unmerged
:1;
759 unsigned is_binary
:1;
760 unsigned is_renamed
:1;
761 unsigned int added
, deleted
;
765 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
769 struct diffstat_file
*x
;
770 x
= xcalloc(sizeof (*x
), 1);
771 if (diffstat
->nr
== diffstat
->alloc
) {
772 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
773 diffstat
->files
= xrealloc(diffstat
->files
,
774 diffstat
->alloc
* sizeof(x
));
776 diffstat
->files
[diffstat
->nr
++] = x
;
778 x
->name
= pprint_rename(name_a
, name_b
);
782 x
->name
= xstrdup(name_a
);
786 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
788 struct diffstat_t
*diffstat
= priv
;
789 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
793 else if (line
[0] == '-')
797 const char mime_boundary_leader
[] = "------------";
799 static int scale_linear(int it
, int width
, int max_change
)
802 * make sure that at least one '-' is printed if there were deletions,
803 * and likewise for '+'.
807 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
810 static void show_name(const char *prefix
, const char *name
, int len
,
811 const char *reset
, const char *set
)
813 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
816 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
826 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
828 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
829 int max_change
= 0, max_len
= 0;
830 int total_files
= data
->nr
;
831 int width
, name_width
;
832 const char *reset
, *set
, *add_c
, *del_c
;
837 width
= options
->stat_width
? options
->stat_width
: 80;
838 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
840 /* Sanity: give at least 5 columns to the graph,
841 * but leave at least 10 columns for the name.
843 if (width
< name_width
+ 15) {
844 if (name_width
<= 25)
845 width
= name_width
+ 15;
847 name_width
= width
- 15;
850 /* Find the longest filename and max number of changes */
851 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
852 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
853 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
854 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
856 for (i
= 0; i
< data
->nr
; i
++) {
857 struct diffstat_file
*file
= data
->files
[i
];
858 int change
= file
->added
+ file
->deleted
;
860 if (!file
->is_renamed
) { /* renames are already quoted by pprint_rename */
861 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
863 char *qname
= xmalloc(len
+ 1);
864 quote_c_style(file
->name
, qname
, NULL
, 0);
870 len
= strlen(file
->name
);
874 if (file
->is_binary
|| file
->is_unmerged
)
876 if (max_change
< change
)
880 /* Compute the width of the graph part;
881 * 10 is for one blank at the beginning of the line plus
882 * " | count " between the name and the graph.
884 * From here on, name_width is the width of the name area,
885 * and width is the width of the graph area.
887 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
888 if (width
< (name_width
+ 10) + max_change
)
889 width
= width
- (name_width
+ 10);
893 for (i
= 0; i
< data
->nr
; i
++) {
894 const char *prefix
= "";
895 char *name
= data
->files
[i
]->name
;
896 int added
= data
->files
[i
]->added
;
897 int deleted
= data
->files
[i
]->deleted
;
901 * "scale" the filename
904 name_len
= strlen(name
);
905 if (name_width
< name_len
) {
909 name
+= name_len
- len
;
910 slash
= strchr(name
, '/');
915 if (data
->files
[i
]->is_binary
) {
916 show_name(prefix
, name
, len
, reset
, set
);
918 printf("%s%d%s", del_c
, deleted
, reset
);
920 printf("%s%d%s", add_c
, added
, reset
);
923 goto free_diffstat_file
;
925 else if (data
->files
[i
]->is_unmerged
) {
926 show_name(prefix
, name
, len
, reset
, set
);
927 printf(" Unmerged\n");
928 goto free_diffstat_file
;
930 else if (!data
->files
[i
]->is_renamed
&&
931 (added
+ deleted
== 0)) {
933 goto free_diffstat_file
;
937 * scale the add/delete
945 if (width
<= max_change
) {
946 add
= scale_linear(add
, width
, max_change
);
947 del
= scale_linear(del
, width
, max_change
);
950 show_name(prefix
, name
, len
, reset
, set
);
951 printf("%5d ", added
+ deleted
);
952 show_graph('+', add
, add_c
, reset
);
953 show_graph('-', del
, del_c
, reset
);
956 free(data
->files
[i
]->name
);
957 free(data
->files
[i
]);
960 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
961 set
, total_files
, adds
, dels
, reset
);
964 static void show_shortstats(struct diffstat_t
* data
)
966 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
971 for (i
= 0; i
< data
->nr
; i
++) {
972 if (!data
->files
[i
]->is_binary
&&
973 !data
->files
[i
]->is_unmerged
) {
974 int added
= data
->files
[i
]->added
;
975 int deleted
= data
->files
[i
]->deleted
;
976 if (!data
->files
[i
]->is_renamed
&&
977 (added
+ deleted
== 0)) {
984 free(data
->files
[i
]->name
);
985 free(data
->files
[i
]);
989 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
990 total_files
, adds
, dels
);
993 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
997 for (i
= 0; i
< data
->nr
; i
++) {
998 struct diffstat_file
*file
= data
->files
[i
];
1000 if (file
->is_binary
)
1003 printf("%d\t%d\t", file
->added
, file
->deleted
);
1004 if (options
->line_termination
&& !file
->is_renamed
&&
1005 quote_c_style(file
->name
, NULL
, NULL
, 0))
1006 quote_c_style(file
->name
, NULL
, stdout
, 0);
1008 fputs(file
->name
, stdout
);
1009 putchar(options
->line_termination
);
1013 struct checkdiff_t
{
1014 struct xdiff_emit_state xm
;
1015 const char *filename
;
1016 int lineno
, color_diff
;
1019 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1021 struct checkdiff_t
*data
= priv
;
1022 const char *ws
= diff_get_color(data
->color_diff
, DIFF_WHITESPACE
);
1023 const char *reset
= diff_get_color(data
->color_diff
, DIFF_RESET
);
1024 const char *set
= diff_get_color(data
->color_diff
, DIFF_FILE_NEW
);
1026 if (line
[0] == '+') {
1027 int i
, spaces
= 0, space_before_tab
= 0, white_space_at_end
= 0;
1029 /* check space before tab */
1030 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
1033 if (line
[i
- 1] == '\t' && spaces
)
1034 space_before_tab
= 1;
1036 /* check white space at line end */
1037 if (line
[len
- 1] == '\n')
1039 if (isspace(line
[len
- 1]))
1040 white_space_at_end
= 1;
1042 if (space_before_tab
|| white_space_at_end
) {
1043 printf("%s:%d: %s", data
->filename
, data
->lineno
, ws
);
1044 if (space_before_tab
) {
1045 printf("space before tab");
1046 if (white_space_at_end
)
1049 if (white_space_at_end
)
1050 printf("white space at end");
1051 printf(":%s ", reset
);
1052 emit_line_with_ws(1, set
, reset
, ws
, line
, len
);
1056 } else if (line
[0] == ' ')
1058 else if (line
[0] == '@') {
1059 char *plus
= strchr(line
, '+');
1061 data
->lineno
= strtol(plus
, NULL
, 10);
1063 die("invalid diff");
1067 static unsigned char *deflate_it(char *data
,
1069 unsigned long *result_size
)
1072 unsigned char *deflated
;
1075 memset(&stream
, 0, sizeof(stream
));
1076 deflateInit(&stream
, zlib_compression_level
);
1077 bound
= deflateBound(&stream
, size
);
1078 deflated
= xmalloc(bound
);
1079 stream
.next_out
= deflated
;
1080 stream
.avail_out
= bound
;
1082 stream
.next_in
= (unsigned char *)data
;
1083 stream
.avail_in
= size
;
1084 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1086 deflateEnd(&stream
);
1087 *result_size
= stream
.total_out
;
1091 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
1097 unsigned long orig_size
;
1098 unsigned long delta_size
;
1099 unsigned long deflate_size
;
1100 unsigned long data_size
;
1102 /* We could do deflated delta, or we could do just deflated two,
1103 * whichever is smaller.
1106 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1107 if (one
->size
&& two
->size
) {
1108 delta
= diff_delta(one
->ptr
, one
->size
,
1109 two
->ptr
, two
->size
,
1110 &delta_size
, deflate_size
);
1112 void *to_free
= delta
;
1113 orig_size
= delta_size
;
1114 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1119 if (delta
&& delta_size
< deflate_size
) {
1120 printf("delta %lu\n", orig_size
);
1123 data_size
= delta_size
;
1126 printf("literal %lu\n", two
->size
);
1129 data_size
= deflate_size
;
1132 /* emit data encoded in base85 */
1135 int bytes
= (52 < data_size
) ? 52 : data_size
;
1139 line
[0] = bytes
+ 'A' - 1;
1141 line
[0] = bytes
- 26 + 'a' - 1;
1142 encode_85(line
+ 1, cp
, bytes
);
1143 cp
= (char *) cp
+ bytes
;
1150 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
1152 printf("GIT binary patch\n");
1153 emit_binary_diff_body(one
, two
);
1154 emit_binary_diff_body(two
, one
);
1157 static void setup_diff_attr_check(struct git_attr_check
*check
)
1159 static struct git_attr
*attr_diff
;
1162 attr_diff
= git_attr("diff", 4);
1164 check
[0].attr
= attr_diff
;
1167 static void diff_filespec_check_attr(struct diff_filespec
*one
)
1169 struct git_attr_check attr_diff_check
;
1170 int check_from_data
= 0;
1172 if (one
->checked_attr
)
1175 setup_diff_attr_check(&attr_diff_check
);
1177 one
->funcname_pattern_ident
= NULL
;
1179 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1183 value
= attr_diff_check
.value
;
1184 if (ATTR_TRUE(value
))
1186 else if (ATTR_FALSE(value
))
1189 check_from_data
= 1;
1191 /* funcname pattern ident */
1192 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1195 one
->funcname_pattern_ident
= value
;
1198 if (check_from_data
) {
1199 if (!one
->data
&& DIFF_FILE_VALID(one
))
1200 diff_populate_filespec(one
, 0);
1203 one
->is_binary
= buffer_is_binary(one
->data
, one
->size
);
1207 int diff_filespec_is_binary(struct diff_filespec
*one
)
1209 diff_filespec_check_attr(one
);
1210 return one
->is_binary
;
1213 static const char *funcname_pattern(const char *ident
)
1215 struct funcname_pattern
*pp
;
1217 read_config_if_needed();
1218 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
1219 if (!strcmp(ident
, pp
->name
))
1224 static struct builtin_funcname_pattern
{
1226 const char *pattern
;
1227 } builtin_funcname_pattern
[] = {
1228 { "java", "!^[ ]*\\(catch\\|do\\|for\\|if\\|instanceof\\|"
1229 "new\\|return\\|switch\\|throw\\|while\\)\n"
1231 "[A-Za-z_][A-Za-z_0-9]*\\)\\{2,\\}"
1233 { "tex", "^\\(\\\\\\(sub\\)*section{.*\\)$" },
1236 static const char *diff_funcname_pattern(struct diff_filespec
*one
)
1238 const char *ident
, *pattern
;
1241 diff_filespec_check_attr(one
);
1242 ident
= one
->funcname_pattern_ident
;
1246 * If the config file has "funcname.default" defined, that
1247 * regexp is used; otherwise NULL is returned and xemit uses
1248 * the built-in default.
1250 return funcname_pattern("default");
1252 /* Look up custom "funcname.$ident" regexp from config. */
1253 pattern
= funcname_pattern(ident
);
1258 * And define built-in fallback patterns here. Note that
1259 * these can be overriden by the user's config settings.
1261 for (i
= 0; i
< ARRAY_SIZE(builtin_funcname_pattern
); i
++)
1262 if (!strcmp(ident
, builtin_funcname_pattern
[i
].name
))
1263 return builtin_funcname_pattern
[i
].pattern
;
1268 static void builtin_diff(const char *name_a
,
1270 struct diff_filespec
*one
,
1271 struct diff_filespec
*two
,
1272 const char *xfrm_msg
,
1273 struct diff_options
*o
,
1274 int complete_rewrite
)
1278 char *a_one
, *b_two
;
1279 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
1280 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
1282 a_one
= quote_two("a/", name_a
+ (*name_a
== '/'));
1283 b_two
= quote_two("b/", name_b
+ (*name_b
== '/'));
1284 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1285 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1286 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1287 if (lbl
[0][0] == '/') {
1289 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1290 if (xfrm_msg
&& xfrm_msg
[0])
1291 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1293 else if (lbl
[1][0] == '/') {
1294 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1295 if (xfrm_msg
&& xfrm_msg
[0])
1296 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1299 if (one
->mode
!= two
->mode
) {
1300 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
1301 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
1303 if (xfrm_msg
&& xfrm_msg
[0])
1304 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1306 * we do not run diff between different kind
1309 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1310 goto free_ab_and_return
;
1311 if (complete_rewrite
) {
1312 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1314 o
->found_changes
= 1;
1315 goto free_ab_and_return
;
1319 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1320 die("unable to read files to diff");
1323 (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
))) {
1324 /* Quite common confusing case */
1325 if (mf1
.size
== mf2
.size
&&
1326 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1327 goto free_ab_and_return
;
1329 emit_binary_diff(&mf1
, &mf2
);
1331 printf("Binary files %s and %s differ\n",
1333 o
->found_changes
= 1;
1336 /* Crazy xdl interfaces.. */
1337 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1341 struct emit_callback ecbdata
;
1342 const char *funcname_pattern
;
1344 funcname_pattern
= diff_funcname_pattern(one
);
1345 if (!funcname_pattern
)
1346 funcname_pattern
= diff_funcname_pattern(two
);
1348 memset(&xecfg
, 0, sizeof(xecfg
));
1349 memset(&ecbdata
, 0, sizeof(ecbdata
));
1350 ecbdata
.label_path
= lbl
;
1351 ecbdata
.color_diff
= o
->color_diff
;
1352 ecbdata
.found_changesp
= &o
->found_changes
;
1353 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1354 xecfg
.ctxlen
= o
->context
;
1355 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1356 if (funcname_pattern
)
1357 xdiff_set_find_func(&xecfg
, funcname_pattern
);
1360 else if (!prefixcmp(diffopts
, "--unified="))
1361 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1362 else if (!prefixcmp(diffopts
, "-u"))
1363 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1364 ecb
.outf
= xdiff_outf
;
1365 ecb
.priv
= &ecbdata
;
1366 ecbdata
.xm
.consume
= fn_out_consume
;
1367 if (o
->color_diff_words
)
1368 ecbdata
.diff_words
=
1369 xcalloc(1, sizeof(struct diff_words_data
));
1370 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1371 if (o
->color_diff_words
)
1372 free_diff_words_data(&ecbdata
);
1376 diff_free_filespec_data(one
);
1377 diff_free_filespec_data(two
);
1383 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1384 struct diff_filespec
*one
,
1385 struct diff_filespec
*two
,
1386 struct diffstat_t
*diffstat
,
1387 struct diff_options
*o
,
1388 int complete_rewrite
)
1391 struct diffstat_file
*data
;
1393 data
= diffstat_add(diffstat
, name_a
, name_b
);
1396 data
->is_unmerged
= 1;
1399 if (complete_rewrite
) {
1400 diff_populate_filespec(one
, 0);
1401 diff_populate_filespec(two
, 0);
1402 data
->deleted
= count_lines(one
->data
, one
->size
);
1403 data
->added
= count_lines(two
->data
, two
->size
);
1404 goto free_and_return
;
1406 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1407 die("unable to read files to diff");
1409 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1410 data
->is_binary
= 1;
1411 data
->added
= mf2
.size
;
1412 data
->deleted
= mf1
.size
;
1414 /* Crazy xdl interfaces.. */
1419 memset(&xecfg
, 0, sizeof(xecfg
));
1420 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1421 ecb
.outf
= xdiff_outf
;
1422 ecb
.priv
= diffstat
;
1423 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1427 diff_free_filespec_data(one
);
1428 diff_free_filespec_data(two
);
1431 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1432 struct diff_filespec
*one
,
1433 struct diff_filespec
*two
, struct diff_options
*o
)
1436 struct checkdiff_t data
;
1441 memset(&data
, 0, sizeof(data
));
1442 data
.xm
.consume
= checkdiff_consume
;
1443 data
.filename
= name_b
? name_b
: name_a
;
1445 data
.color_diff
= o
->color_diff
;
1447 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1448 die("unable to read files to diff");
1450 if (diff_filespec_is_binary(two
))
1451 goto free_and_return
;
1453 /* Crazy xdl interfaces.. */
1458 memset(&xecfg
, 0, sizeof(xecfg
));
1459 xpp
.flags
= XDF_NEED_MINIMAL
;
1460 ecb
.outf
= xdiff_outf
;
1462 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1465 diff_free_filespec_data(one
);
1466 diff_free_filespec_data(two
);
1469 struct diff_filespec
*alloc_filespec(const char *path
)
1471 int namelen
= strlen(path
);
1472 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1474 memset(spec
, 0, sizeof(*spec
));
1475 spec
->path
= (char *)(spec
+ 1);
1476 memcpy(spec
->path
, path
, namelen
+1);
1480 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1481 unsigned short mode
)
1484 spec
->mode
= canon_mode(mode
);
1485 hashcpy(spec
->sha1
, sha1
);
1486 spec
->sha1_valid
= !is_null_sha1(sha1
);
1491 * Given a name and sha1 pair, if the index tells us the file in
1492 * the work tree has that object contents, return true, so that
1493 * prepare_temp_file() does not have to inflate and extract.
1495 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1497 struct cache_entry
*ce
;
1501 /* We do not read the cache ourselves here, because the
1502 * benchmark with my previous version that always reads cache
1503 * shows that it makes things worse for diff-tree comparing
1504 * two linux-2.6 kernel trees in an already checked out work
1505 * tree. This is because most diff-tree comparisons deal with
1506 * only a small number of files, while reading the cache is
1507 * expensive for a large project, and its cost outweighs the
1508 * savings we get by not inflating the object to a temporary
1509 * file. Practically, this code only helps when we are used
1510 * by diff-cache --cached, which does read the cache before
1516 /* We want to avoid the working directory if our caller
1517 * doesn't need the data in a normal file, this system
1518 * is rather slow with its stat/open/mmap/close syscalls,
1519 * and the object is contained in a pack file. The pack
1520 * is probably already open and will be faster to obtain
1521 * the data through than the working directory. Loose
1522 * objects however would tend to be slower as they need
1523 * to be individually opened and inflated.
1525 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1529 pos
= cache_name_pos(name
, len
);
1532 ce
= active_cache
[pos
];
1533 if ((lstat(name
, &st
) < 0) ||
1534 !S_ISREG(st
.st_mode
) || /* careful! */
1535 ce_match_stat(ce
, &st
, 0) ||
1536 hashcmp(sha1
, ce
->sha1
))
1538 /* we return 1 only when we can stat, it is a regular file,
1539 * stat information matches, and sha1 recorded in the cache
1540 * matches. I.e. we know the file in the work tree really is
1541 * the same as the <name, sha1> pair.
1546 static int populate_from_stdin(struct diff_filespec
*s
)
1548 #define INCREMENT 1024
1556 buf
= xrealloc(buf
, size
+ INCREMENT
);
1557 got
= xread(0, buf
+ size
, INCREMENT
);
1561 return error("error while reading from stdin %s",
1565 s
->should_munmap
= 0;
1572 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1575 char *data
= xmalloc(100);
1576 len
= snprintf(data
, 100,
1577 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1589 * While doing rename detection and pickaxe operation, we may need to
1590 * grab the data for the blob (or file) for our own in-core comparison.
1591 * diff_filespec has data and size fields for this purpose.
1593 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1596 if (!DIFF_FILE_VALID(s
))
1597 die("internal error: asking to populate invalid file.");
1598 if (S_ISDIR(s
->mode
))
1604 if (size_only
&& 0 < s
->size
)
1607 if (S_ISGITLINK(s
->mode
))
1608 return diff_populate_gitlink(s
, size_only
);
1610 if (!s
->sha1_valid
||
1611 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1617 if (!strcmp(s
->path
, "-"))
1618 return populate_from_stdin(s
);
1620 if (lstat(s
->path
, &st
) < 0) {
1621 if (errno
== ENOENT
) {
1625 s
->data
= (char *)"";
1630 s
->size
= xsize_t(st
.st_size
);
1635 if (S_ISLNK(st
.st_mode
)) {
1637 s
->data
= xmalloc(s
->size
);
1639 ret
= readlink(s
->path
, s
->data
, s
->size
);
1646 fd
= open(s
->path
, O_RDONLY
);
1649 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1651 s
->should_munmap
= 1;
1654 * Convert from working tree format to canonical git format
1657 buf
= convert_to_git(s
->path
, s
->data
, &size
);
1659 munmap(s
->data
, s
->size
);
1660 s
->should_munmap
= 0;
1667 enum object_type type
;
1669 type
= sha1_object_info(s
->sha1
, &s
->size
);
1671 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1678 void diff_free_filespec_data(struct diff_filespec
*s
)
1682 else if (s
->should_munmap
)
1683 munmap(s
->data
, s
->size
);
1685 if (s
->should_free
|| s
->should_munmap
) {
1686 s
->should_free
= s
->should_munmap
= 0;
1693 static void prep_temp_blob(struct diff_tempfile
*temp
,
1696 const unsigned char *sha1
,
1701 fd
= git_mkstemp(temp
->tmp_path
, PATH_MAX
, ".diff_XXXXXX");
1703 die("unable to create temp-file: %s", strerror(errno
));
1704 if (write_in_full(fd
, blob
, size
) != size
)
1705 die("unable to write temp-file");
1707 temp
->name
= temp
->tmp_path
;
1708 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1710 sprintf(temp
->mode
, "%06o", mode
);
1713 static void prepare_temp_file(const char *name
,
1714 struct diff_tempfile
*temp
,
1715 struct diff_filespec
*one
)
1717 if (!DIFF_FILE_VALID(one
)) {
1719 /* A '-' entry produces this for file-2, and
1720 * a '+' entry produces this for file-1.
1722 temp
->name
= "/dev/null";
1723 strcpy(temp
->hex
, ".");
1724 strcpy(temp
->mode
, ".");
1728 if (!one
->sha1_valid
||
1729 reuse_worktree_file(name
, one
->sha1
, 1)) {
1731 if (lstat(name
, &st
) < 0) {
1732 if (errno
== ENOENT
)
1733 goto not_a_valid_file
;
1734 die("stat(%s): %s", name
, strerror(errno
));
1736 if (S_ISLNK(st
.st_mode
)) {
1738 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1739 size_t sz
= xsize_t(st
.st_size
);
1740 if (sizeof(buf
) <= st
.st_size
)
1741 die("symlink too long: %s", name
);
1742 ret
= readlink(name
, buf
, sz
);
1744 die("readlink(%s)", name
);
1745 prep_temp_blob(temp
, buf
, sz
,
1747 one
->sha1
: null_sha1
),
1749 one
->mode
: S_IFLNK
));
1752 /* we can borrow from the file in the work tree */
1754 if (!one
->sha1_valid
)
1755 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1757 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1758 /* Even though we may sometimes borrow the
1759 * contents from the work tree, we always want
1760 * one->mode. mode is trustworthy even when
1761 * !(one->sha1_valid), as long as
1762 * DIFF_FILE_VALID(one).
1764 sprintf(temp
->mode
, "%06o", one
->mode
);
1769 if (diff_populate_filespec(one
, 0))
1770 die("cannot read data blob for %s", one
->path
);
1771 prep_temp_blob(temp
, one
->data
, one
->size
,
1772 one
->sha1
, one
->mode
);
1776 static void remove_tempfile(void)
1780 for (i
= 0; i
< 2; i
++)
1781 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1782 unlink(diff_temp
[i
].name
);
1783 diff_temp
[i
].name
= NULL
;
1787 static void remove_tempfile_on_signal(int signo
)
1790 signal(SIGINT
, SIG_DFL
);
1794 static int spawn_prog(const char *pgm
, const char **arg
)
1802 die("unable to fork");
1804 execvp(pgm
, (char *const*) arg
);
1808 while (waitpid(pid
, &status
, 0) < 0) {
1814 /* Earlier we did not check the exit status because
1815 * diff exits non-zero if files are different, and
1816 * we are not interested in knowing that. It was a
1817 * mistake which made it harder to quit a diff-*
1818 * session that uses the git-apply-patch-script as
1819 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1820 * should also exit non-zero only when it wants to
1821 * abort the entire diff-* session.
1823 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1828 /* An external diff command takes:
1830 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1831 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1834 static void run_external_diff(const char *pgm
,
1837 struct diff_filespec
*one
,
1838 struct diff_filespec
*two
,
1839 const char *xfrm_msg
,
1840 int complete_rewrite
)
1842 const char *spawn_arg
[10];
1843 struct diff_tempfile
*temp
= diff_temp
;
1845 static int atexit_asked
= 0;
1846 const char *othername
;
1847 const char **arg
= &spawn_arg
[0];
1849 othername
= (other
? other
: name
);
1851 prepare_temp_file(name
, &temp
[0], one
);
1852 prepare_temp_file(othername
, &temp
[1], two
);
1853 if (! atexit_asked
&&
1854 (temp
[0].name
== temp
[0].tmp_path
||
1855 temp
[1].name
== temp
[1].tmp_path
)) {
1857 atexit(remove_tempfile
);
1859 signal(SIGINT
, remove_tempfile_on_signal
);
1865 *arg
++ = temp
[0].name
;
1866 *arg
++ = temp
[0].hex
;
1867 *arg
++ = temp
[0].mode
;
1868 *arg
++ = temp
[1].name
;
1869 *arg
++ = temp
[1].hex
;
1870 *arg
++ = temp
[1].mode
;
1880 retval
= spawn_prog(pgm
, spawn_arg
);
1883 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1888 static const char *external_diff_attr(const char *name
)
1890 struct git_attr_check attr_diff_check
;
1892 setup_diff_attr_check(&attr_diff_check
);
1893 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
1894 const char *value
= attr_diff_check
.value
;
1895 if (!ATTR_TRUE(value
) &&
1896 !ATTR_FALSE(value
) &&
1897 !ATTR_UNSET(value
)) {
1898 struct ll_diff_driver
*drv
;
1900 read_config_if_needed();
1901 for (drv
= user_diff
; drv
; drv
= drv
->next
)
1902 if (!strcmp(drv
->name
, value
))
1909 static void run_diff_cmd(const char *pgm
,
1912 struct diff_filespec
*one
,
1913 struct diff_filespec
*two
,
1914 const char *xfrm_msg
,
1915 struct diff_options
*o
,
1916 int complete_rewrite
)
1918 if (!o
->allow_external
)
1921 const char *cmd
= external_diff_attr(name
);
1927 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1932 builtin_diff(name
, other
? other
: name
,
1933 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1935 printf("* Unmerged path %s\n", name
);
1938 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1940 if (DIFF_FILE_VALID(one
)) {
1941 if (!one
->sha1_valid
) {
1943 if (!strcmp(one
->path
, "-")) {
1944 hashcpy(one
->sha1
, null_sha1
);
1947 if (lstat(one
->path
, &st
) < 0)
1948 die("stat %s", one
->path
);
1949 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1950 die("cannot hash %s\n", one
->path
);
1957 static int similarity_index(struct diff_filepair
*p
)
1959 return p
->score
* 100 / MAX_SCORE
;
1962 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1964 const char *pgm
= external_diff();
1965 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1966 struct diff_filespec
*one
;
1967 struct diff_filespec
*two
;
1970 char *name_munged
, *other_munged
;
1971 int complete_rewrite
= 0;
1974 if (DIFF_PAIR_UNMERGED(p
)) {
1976 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1980 name
= p
->one
->path
;
1981 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1982 name_munged
= quote_one(name
);
1983 other_munged
= quote_one(other
);
1984 one
= p
->one
; two
= p
->two
;
1986 diff_fill_sha1_info(one
);
1987 diff_fill_sha1_info(two
);
1990 switch (p
->status
) {
1991 case DIFF_STATUS_COPIED
:
1992 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1993 "similarity index %d%%\n"
1996 similarity_index(p
), name_munged
, other_munged
);
1998 case DIFF_STATUS_RENAMED
:
1999 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2000 "similarity index %d%%\n"
2003 similarity_index(p
), name_munged
, other_munged
);
2005 case DIFF_STATUS_MODIFIED
:
2007 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2008 "dissimilarity index %d%%\n",
2009 similarity_index(p
));
2010 complete_rewrite
= 1;
2019 if (hashcmp(one
->sha1
, two
->sha1
)) {
2020 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
2024 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2025 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2028 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2030 abbrev
, sha1_to_hex(one
->sha1
),
2031 abbrev
, sha1_to_hex(two
->sha1
));
2032 if (one
->mode
== two
->mode
)
2033 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2034 " %06o", one
->mode
);
2035 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
2040 xfrm_msg
= len
? msg
: NULL
;
2043 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2044 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2045 /* a filepair that changes between file and symlink
2046 * needs to be split into deletion and creation.
2048 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2049 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
2051 null
= alloc_filespec(one
->path
);
2052 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
2056 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
2063 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2064 struct diffstat_t
*diffstat
)
2068 int complete_rewrite
= 0;
2070 if (DIFF_PAIR_UNMERGED(p
)) {
2072 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
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 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2083 complete_rewrite
= 1;
2084 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2087 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2092 if (DIFF_PAIR_UNMERGED(p
)) {
2097 name
= p
->one
->path
;
2098 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2100 diff_fill_sha1_info(p
->one
);
2101 diff_fill_sha1_info(p
->two
);
2103 builtin_checkdiff(name
, other
, p
->one
, p
->two
, o
);
2106 void diff_setup(struct diff_options
*options
)
2108 memset(options
, 0, sizeof(*options
));
2109 options
->line_termination
= '\n';
2110 options
->break_opt
= -1;
2111 options
->rename_limit
= -1;
2112 options
->context
= 3;
2113 options
->msg_sep
= "";
2115 options
->change
= diff_change
;
2116 options
->add_remove
= diff_addremove
;
2117 options
->color_diff
= diff_use_color_default
;
2118 options
->detect_rename
= diff_detect_rename_default
;
2121 int diff_setup_done(struct diff_options
*options
)
2125 if (options
->output_format
& DIFF_FORMAT_NAME
)
2127 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2129 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2131 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2134 die("--name-only, --name-status, --check and -s are mutually exclusive");
2136 if (options
->find_copies_harder
)
2137 options
->detect_rename
= DIFF_DETECT_COPY
;
2139 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2140 DIFF_FORMAT_NAME_STATUS
|
2141 DIFF_FORMAT_CHECKDIFF
|
2142 DIFF_FORMAT_NO_OUTPUT
))
2143 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2144 DIFF_FORMAT_NUMSTAT
|
2145 DIFF_FORMAT_DIFFSTAT
|
2146 DIFF_FORMAT_SHORTSTAT
|
2147 DIFF_FORMAT_SUMMARY
|
2151 * These cases always need recursive; we do not drop caller-supplied
2152 * recursive bits for other formats here.
2154 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2155 DIFF_FORMAT_NUMSTAT
|
2156 DIFF_FORMAT_DIFFSTAT
|
2157 DIFF_FORMAT_SHORTSTAT
|
2158 DIFF_FORMAT_SUMMARY
|
2159 DIFF_FORMAT_CHECKDIFF
))
2160 options
->recursive
= 1;
2162 * Also pickaxe would not work very well if you do not say recursive
2164 if (options
->pickaxe
)
2165 options
->recursive
= 1;
2167 if (options
->detect_rename
&& options
->rename_limit
< 0)
2168 options
->rename_limit
= diff_rename_limit_default
;
2169 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2171 /* read-cache does not die even when it fails
2172 * so it is safe for us to do this here. Also
2173 * it does not smudge active_cache or active_nr
2174 * when it fails, so we do not have to worry about
2175 * cleaning it up ourselves either.
2179 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2180 options
->abbrev
= 40; /* full */
2183 * It does not make sense to show the first hit we happened
2184 * to have found. It does not make sense not to return with
2185 * exit code in such a case either.
2187 if (options
->quiet
) {
2188 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2189 options
->exit_with_status
= 1;
2193 * If we postprocess in diffcore, we cannot simply return
2194 * upon the first hit. We need to run diff as usual.
2196 if (options
->pickaxe
|| options
->filter
)
2202 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2212 if (c
== arg_short
) {
2216 if (val
&& isdigit(c
)) {
2218 int n
= strtoul(arg
, &end
, 10);
2229 eq
= strchr(arg
, '=');
2234 if (!len
|| strncmp(arg
, arg_long
, len
))
2239 if (!isdigit(*++eq
))
2241 n
= strtoul(eq
, &end
, 10);
2249 static int diff_scoreopt_parse(const char *opt
);
2251 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2253 const char *arg
= av
[0];
2254 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2255 options
->output_format
|= DIFF_FORMAT_PATCH
;
2256 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2257 options
->output_format
|= DIFF_FORMAT_PATCH
;
2258 else if (!strcmp(arg
, "--raw"))
2259 options
->output_format
|= DIFF_FORMAT_RAW
;
2260 else if (!strcmp(arg
, "--patch-with-raw")) {
2261 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2263 else if (!strcmp(arg
, "--numstat")) {
2264 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2266 else if (!strcmp(arg
, "--shortstat")) {
2267 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2269 else if (!prefixcmp(arg
, "--stat")) {
2271 int width
= options
->stat_width
;
2272 int name_width
= options
->stat_name_width
;
2278 if (!prefixcmp(arg
, "-width="))
2279 width
= strtoul(arg
+ 7, &end
, 10);
2280 else if (!prefixcmp(arg
, "-name-width="))
2281 name_width
= strtoul(arg
+ 12, &end
, 10);
2284 width
= strtoul(arg
+1, &end
, 10);
2286 name_width
= strtoul(end
+1, &end
, 10);
2289 /* Important! This checks all the error cases! */
2292 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2293 options
->stat_name_width
= name_width
;
2294 options
->stat_width
= width
;
2296 else if (!strcmp(arg
, "--check"))
2297 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2298 else if (!strcmp(arg
, "--summary"))
2299 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2300 else if (!strcmp(arg
, "--patch-with-stat")) {
2301 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2303 else if (!strcmp(arg
, "-z"))
2304 options
->line_termination
= 0;
2305 else if (!prefixcmp(arg
, "-l"))
2306 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2307 else if (!strcmp(arg
, "--full-index"))
2308 options
->full_index
= 1;
2309 else if (!strcmp(arg
, "--binary")) {
2310 options
->output_format
|= DIFF_FORMAT_PATCH
;
2311 options
->binary
= 1;
2313 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
2316 else if (!strcmp(arg
, "--name-only"))
2317 options
->output_format
|= DIFF_FORMAT_NAME
;
2318 else if (!strcmp(arg
, "--name-status"))
2319 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2320 else if (!strcmp(arg
, "-R"))
2321 options
->reverse_diff
= 1;
2322 else if (!prefixcmp(arg
, "-S"))
2323 options
->pickaxe
= arg
+ 2;
2324 else if (!strcmp(arg
, "-s")) {
2325 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2327 else if (!prefixcmp(arg
, "-O"))
2328 options
->orderfile
= arg
+ 2;
2329 else if (!prefixcmp(arg
, "--diff-filter="))
2330 options
->filter
= arg
+ 14;
2331 else if (!strcmp(arg
, "--pickaxe-all"))
2332 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2333 else if (!strcmp(arg
, "--pickaxe-regex"))
2334 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2335 else if (!prefixcmp(arg
, "-B")) {
2336 if ((options
->break_opt
=
2337 diff_scoreopt_parse(arg
)) == -1)
2340 else if (!prefixcmp(arg
, "-M")) {
2341 if ((options
->rename_score
=
2342 diff_scoreopt_parse(arg
)) == -1)
2344 options
->detect_rename
= DIFF_DETECT_RENAME
;
2346 else if (!prefixcmp(arg
, "-C")) {
2347 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2348 options
->find_copies_harder
= 1;
2349 if ((options
->rename_score
=
2350 diff_scoreopt_parse(arg
)) == -1)
2352 options
->detect_rename
= DIFF_DETECT_COPY
;
2354 else if (!strcmp(arg
, "--find-copies-harder"))
2355 options
->find_copies_harder
= 1;
2356 else if (!strcmp(arg
, "--follow"))
2357 options
->follow_renames
= 1;
2358 else if (!strcmp(arg
, "--abbrev"))
2359 options
->abbrev
= DEFAULT_ABBREV
;
2360 else if (!prefixcmp(arg
, "--abbrev=")) {
2361 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2362 if (options
->abbrev
< MINIMUM_ABBREV
)
2363 options
->abbrev
= MINIMUM_ABBREV
;
2364 else if (40 < options
->abbrev
)
2365 options
->abbrev
= 40;
2367 else if (!strcmp(arg
, "--color"))
2368 options
->color_diff
= 1;
2369 else if (!strcmp(arg
, "--no-color"))
2370 options
->color_diff
= 0;
2371 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2372 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2373 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2374 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2375 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2376 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2377 else if (!strcmp(arg
, "--color-words"))
2378 options
->color_diff
= options
->color_diff_words
= 1;
2379 else if (!strcmp(arg
, "--no-renames"))
2380 options
->detect_rename
= 0;
2381 else if (!strcmp(arg
, "--exit-code"))
2382 options
->exit_with_status
= 1;
2383 else if (!strcmp(arg
, "--quiet"))
2385 else if (!strcmp(arg
, "--ext-diff"))
2386 options
->allow_external
= 1;
2387 else if (!strcmp(arg
, "--no-ext-diff"))
2388 options
->allow_external
= 0;
2394 static int parse_num(const char **cp_p
)
2396 unsigned long num
, scale
;
2398 const char *cp
= *cp_p
;
2405 if ( !dot
&& ch
== '.' ) {
2408 } else if ( ch
== '%' ) {
2409 scale
= dot
? scale
*100 : 100;
2410 cp
++; /* % is always at the end */
2412 } else if ( ch
>= '0' && ch
<= '9' ) {
2413 if ( scale
< 100000 ) {
2415 num
= (num
*10) + (ch
-'0');
2424 /* user says num divided by scale and we say internally that
2425 * is MAX_SCORE * num / scale.
2427 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2430 static int diff_scoreopt_parse(const char *opt
)
2432 int opt1
, opt2
, cmd
;
2437 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2438 return -1; /* that is not a -M, -C nor -B option */
2440 opt1
= parse_num(&opt
);
2446 else if (*opt
!= '/')
2447 return -1; /* we expect -B80/99 or -B80 */
2450 opt2
= parse_num(&opt
);
2455 return opt1
| (opt2
<< 16);
2458 struct diff_queue_struct diff_queued_diff
;
2460 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2462 if (queue
->alloc
<= queue
->nr
) {
2463 queue
->alloc
= alloc_nr(queue
->alloc
);
2464 queue
->queue
= xrealloc(queue
->queue
,
2465 sizeof(dp
) * queue
->alloc
);
2467 queue
->queue
[queue
->nr
++] = dp
;
2470 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2471 struct diff_filespec
*one
,
2472 struct diff_filespec
*two
)
2474 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2482 void diff_free_filepair(struct diff_filepair
*p
)
2484 diff_free_filespec_data(p
->one
);
2485 diff_free_filespec_data(p
->two
);
2491 /* This is different from find_unique_abbrev() in that
2492 * it stuffs the result with dots for alignment.
2494 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2499 return sha1_to_hex(sha1
);
2501 abbrev
= find_unique_abbrev(sha1
, len
);
2503 return sha1_to_hex(sha1
);
2504 abblen
= strlen(abbrev
);
2506 static char hex
[41];
2507 if (len
< abblen
&& abblen
<= len
+ 2)
2508 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2510 sprintf(hex
, "%s...", abbrev
);
2513 return sha1_to_hex(sha1
);
2516 static void diff_flush_raw(struct diff_filepair
*p
,
2517 struct diff_options
*options
)
2521 int abbrev
= options
->abbrev
;
2522 const char *path_one
, *path_two
;
2523 int inter_name_termination
= '\t';
2524 int line_termination
= options
->line_termination
;
2526 if (!line_termination
)
2527 inter_name_termination
= 0;
2529 path_one
= p
->one
->path
;
2530 path_two
= p
->two
->path
;
2531 if (line_termination
) {
2532 path_one
= quote_one(path_one
);
2533 path_two
= quote_one(path_two
);
2537 sprintf(status
, "%c%03d", p
->status
, similarity_index(p
));
2539 status
[0] = p
->status
;
2542 switch (p
->status
) {
2543 case DIFF_STATUS_COPIED
:
2544 case DIFF_STATUS_RENAMED
:
2547 case DIFF_STATUS_ADDED
:
2548 case DIFF_STATUS_DELETED
:
2555 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2556 printf(":%06o %06o %s ",
2557 p
->one
->mode
, p
->two
->mode
,
2558 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2560 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2562 printf("%s%c%s", status
, inter_name_termination
,
2563 two_paths
|| p
->one
->mode
? path_one
: path_two
);
2565 printf("%c%s", inter_name_termination
, path_two
);
2566 putchar(line_termination
);
2567 if (path_one
!= p
->one
->path
)
2568 free((void*)path_one
);
2569 if (path_two
!= p
->two
->path
)
2570 free((void*)path_two
);
2573 static void diff_flush_name(struct diff_filepair
*p
, struct diff_options
*opt
)
2575 char *path
= p
->two
->path
;
2577 if (opt
->line_termination
)
2578 path
= quote_one(p
->two
->path
);
2579 printf("%s%c", path
, opt
->line_termination
);
2580 if (p
->two
->path
!= path
)
2584 int diff_unmodified_pair(struct diff_filepair
*p
)
2586 /* This function is written stricter than necessary to support
2587 * the currently implemented transformers, but the idea is to
2588 * let transformers to produce diff_filepairs any way they want,
2589 * and filter and clean them up here before producing the output.
2591 struct diff_filespec
*one
, *two
;
2593 if (DIFF_PAIR_UNMERGED(p
))
2594 return 0; /* unmerged is interesting */
2599 /* deletion, addition, mode or type change
2600 * and rename are all interesting.
2602 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2603 DIFF_PAIR_MODE_CHANGED(p
) ||
2604 strcmp(one
->path
, two
->path
))
2607 /* both are valid and point at the same path. that is, we are
2608 * dealing with a change.
2610 if (one
->sha1_valid
&& two
->sha1_valid
&&
2611 !hashcmp(one
->sha1
, two
->sha1
))
2612 return 1; /* no change */
2613 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2614 return 1; /* both look at the same file on the filesystem. */
2618 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2620 if (diff_unmodified_pair(p
))
2623 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2624 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2625 return; /* no tree diffs in patch format */
2630 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2631 struct diffstat_t
*diffstat
)
2633 if (diff_unmodified_pair(p
))
2636 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2637 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2638 return; /* no tree diffs in patch format */
2640 run_diffstat(p
, o
, diffstat
);
2643 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2644 struct diff_options
*o
)
2646 if (diff_unmodified_pair(p
))
2649 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2650 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2651 return; /* no tree diffs in patch format */
2653 run_checkdiff(p
, o
);
2656 int diff_queue_is_empty(void)
2658 struct diff_queue_struct
*q
= &diff_queued_diff
;
2660 for (i
= 0; i
< q
->nr
; i
++)
2661 if (!diff_unmodified_pair(q
->queue
[i
]))
2667 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2669 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2672 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2674 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2675 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2677 s
->size
, s
->xfrm_flags
);
2680 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2682 diff_debug_filespec(p
->one
, i
, "one");
2683 diff_debug_filespec(p
->two
, i
, "two");
2684 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2685 p
->score
, p
->status
? p
->status
: '?',
2686 p
->source_stays
, p
->broken_pair
);
2689 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2693 fprintf(stderr
, "%s\n", msg
);
2694 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2695 for (i
= 0; i
< q
->nr
; i
++) {
2696 struct diff_filepair
*p
= q
->queue
[i
];
2697 diff_debug_filepair(p
, i
);
2702 static void diff_resolve_rename_copy(void)
2705 struct diff_filepair
*p
, *pp
;
2706 struct diff_queue_struct
*q
= &diff_queued_diff
;
2708 diff_debug_queue("resolve-rename-copy", q
);
2710 for (i
= 0; i
< q
->nr
; i
++) {
2712 p
->status
= 0; /* undecided */
2713 if (DIFF_PAIR_UNMERGED(p
))
2714 p
->status
= DIFF_STATUS_UNMERGED
;
2715 else if (!DIFF_FILE_VALID(p
->one
))
2716 p
->status
= DIFF_STATUS_ADDED
;
2717 else if (!DIFF_FILE_VALID(p
->two
))
2718 p
->status
= DIFF_STATUS_DELETED
;
2719 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2720 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2722 /* from this point on, we are dealing with a pair
2723 * whose both sides are valid and of the same type, i.e.
2724 * either in-place edit or rename/copy edit.
2726 else if (DIFF_PAIR_RENAME(p
)) {
2727 if (p
->source_stays
) {
2728 p
->status
= DIFF_STATUS_COPIED
;
2731 /* See if there is some other filepair that
2732 * copies from the same source as us. If so
2733 * we are a copy. Otherwise we are either a
2734 * copy if the path stays, or a rename if it
2735 * does not, but we already handled "stays" case.
2737 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2739 if (strcmp(pp
->one
->path
, p
->one
->path
))
2740 continue; /* not us */
2741 if (!DIFF_PAIR_RENAME(pp
))
2742 continue; /* not a rename/copy */
2743 /* pp is a rename/copy from the same source */
2744 p
->status
= DIFF_STATUS_COPIED
;
2748 p
->status
= DIFF_STATUS_RENAMED
;
2750 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2751 p
->one
->mode
!= p
->two
->mode
||
2752 is_null_sha1(p
->one
->sha1
))
2753 p
->status
= DIFF_STATUS_MODIFIED
;
2755 /* This is a "no-change" entry and should not
2756 * happen anymore, but prepare for broken callers.
2758 error("feeding unmodified %s to diffcore",
2760 p
->status
= DIFF_STATUS_UNKNOWN
;
2763 diff_debug_queue("resolve-rename-copy done", q
);
2766 static int check_pair_status(struct diff_filepair
*p
)
2768 switch (p
->status
) {
2769 case DIFF_STATUS_UNKNOWN
:
2772 die("internal error in diff-resolve-rename-copy");
2778 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2780 int fmt
= opt
->output_format
;
2782 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2783 diff_flush_checkdiff(p
, opt
);
2784 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2785 diff_flush_raw(p
, opt
);
2786 else if (fmt
& DIFF_FORMAT_NAME
)
2787 diff_flush_name(p
, opt
);
2790 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2792 char *name
= quote_one(fs
->path
);
2794 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, name
);
2796 printf(" %s %s\n", newdelete
, name
);
2801 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2803 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2805 char *name
= quote_one(p
->two
->path
);
2806 printf(" mode change %06o => %06o %s\n",
2807 p
->one
->mode
, p
->two
->mode
, name
);
2811 printf(" mode change %06o => %06o\n",
2812 p
->one
->mode
, p
->two
->mode
);
2816 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2818 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
2820 printf(" %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
2822 show_mode_change(p
, 0);
2825 static void diff_summary(struct diff_filepair
*p
)
2828 case DIFF_STATUS_DELETED
:
2829 show_file_mode_name("delete", p
->one
);
2831 case DIFF_STATUS_ADDED
:
2832 show_file_mode_name("create", p
->two
);
2834 case DIFF_STATUS_COPIED
:
2835 show_rename_copy("copy", p
);
2837 case DIFF_STATUS_RENAMED
:
2838 show_rename_copy("rename", p
);
2842 char *name
= quote_one(p
->two
->path
);
2843 printf(" rewrite %s (%d%%)\n", name
,
2844 similarity_index(p
));
2846 show_mode_change(p
, 0);
2847 } else show_mode_change(p
, 1);
2853 struct xdiff_emit_state xm
;
2858 static int remove_space(char *line
, int len
)
2864 for (i
= 0; i
< len
; i
++)
2865 if (!isspace((c
= line
[i
])))
2871 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2873 struct patch_id_t
*data
= priv
;
2876 /* Ignore line numbers when computing the SHA1 of the patch */
2877 if (!prefixcmp(line
, "@@ -"))
2880 new_len
= remove_space(line
, len
);
2882 SHA1_Update(data
->ctx
, line
, new_len
);
2883 data
->patchlen
+= new_len
;
2886 /* returns 0 upon success, and writes result into sha1 */
2887 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2889 struct diff_queue_struct
*q
= &diff_queued_diff
;
2892 struct patch_id_t data
;
2893 char buffer
[PATH_MAX
* 4 + 20];
2896 memset(&data
, 0, sizeof(struct patch_id_t
));
2898 data
.xm
.consume
= patch_id_consume
;
2900 for (i
= 0; i
< q
->nr
; i
++) {
2905 struct diff_filepair
*p
= q
->queue
[i
];
2908 memset(&xecfg
, 0, sizeof(xecfg
));
2910 return error("internal diff status error");
2911 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2913 if (diff_unmodified_pair(p
))
2915 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2916 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2918 if (DIFF_PAIR_UNMERGED(p
))
2921 diff_fill_sha1_info(p
->one
);
2922 diff_fill_sha1_info(p
->two
);
2923 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2924 fill_mmfile(&mf2
, p
->two
) < 0)
2925 return error("unable to read files to diff");
2927 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2928 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2929 if (p
->one
->mode
== 0)
2930 len1
= snprintf(buffer
, sizeof(buffer
),
2931 "diff--gita/%.*sb/%.*s"
2938 len2
, p
->two
->path
);
2939 else if (p
->two
->mode
== 0)
2940 len1
= snprintf(buffer
, sizeof(buffer
),
2941 "diff--gita/%.*sb/%.*s"
2942 "deletedfilemode%06o"
2948 len1
, p
->one
->path
);
2950 len1
= snprintf(buffer
, sizeof(buffer
),
2951 "diff--gita/%.*sb/%.*s"
2957 len2
, p
->two
->path
);
2958 SHA1_Update(&ctx
, buffer
, len1
);
2960 xpp
.flags
= XDF_NEED_MINIMAL
;
2962 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2963 ecb
.outf
= xdiff_outf
;
2965 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2968 SHA1_Final(sha1
, &ctx
);
2972 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2974 struct diff_queue_struct
*q
= &diff_queued_diff
;
2976 int result
= diff_get_patch_id(options
, sha1
);
2978 for (i
= 0; i
< q
->nr
; i
++)
2979 diff_free_filepair(q
->queue
[i
]);
2983 q
->nr
= q
->alloc
= 0;
2988 static int is_summary_empty(const struct diff_queue_struct
*q
)
2992 for (i
= 0; i
< q
->nr
; i
++) {
2993 const struct diff_filepair
*p
= q
->queue
[i
];
2995 switch (p
->status
) {
2996 case DIFF_STATUS_DELETED
:
2997 case DIFF_STATUS_ADDED
:
2998 case DIFF_STATUS_COPIED
:
2999 case DIFF_STATUS_RENAMED
:
3004 if (p
->one
->mode
&& p
->two
->mode
&&
3005 p
->one
->mode
!= p
->two
->mode
)
3013 void diff_flush(struct diff_options
*options
)
3015 struct diff_queue_struct
*q
= &diff_queued_diff
;
3016 int i
, output_format
= options
->output_format
;
3020 * Order: raw, stat, summary, patch
3021 * or: name/name-status/checkdiff (other bits clear)
3026 if (output_format
& (DIFF_FORMAT_RAW
|
3028 DIFF_FORMAT_NAME_STATUS
|
3029 DIFF_FORMAT_CHECKDIFF
)) {
3030 for (i
= 0; i
< q
->nr
; i
++) {
3031 struct diff_filepair
*p
= q
->queue
[i
];
3032 if (check_pair_status(p
))
3033 flush_one_pair(p
, options
);
3038 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3039 struct diffstat_t diffstat
;
3041 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3042 diffstat
.xm
.consume
= diffstat_consume
;
3043 for (i
= 0; i
< q
->nr
; i
++) {
3044 struct diff_filepair
*p
= q
->queue
[i
];
3045 if (check_pair_status(p
))
3046 diff_flush_stat(p
, options
, &diffstat
);
3048 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3049 show_numstat(&diffstat
, options
);
3050 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3051 show_stats(&diffstat
, options
);
3052 else if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3053 show_shortstats(&diffstat
);
3057 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3058 for (i
= 0; i
< q
->nr
; i
++)
3059 diff_summary(q
->queue
[i
]);
3063 if (output_format
& DIFF_FORMAT_PATCH
) {
3065 if (options
->stat_sep
) {
3066 /* attach patch instead of inline */
3067 fputs(options
->stat_sep
, stdout
);
3069 putchar(options
->line_termination
);
3073 for (i
= 0; i
< q
->nr
; i
++) {
3074 struct diff_filepair
*p
= q
->queue
[i
];
3075 if (check_pair_status(p
))
3076 diff_flush_patch(p
, options
);
3080 if (output_format
& DIFF_FORMAT_CALLBACK
)
3081 options
->format_callback(q
, options
, options
->format_callback_data
);
3083 for (i
= 0; i
< q
->nr
; i
++)
3084 diff_free_filepair(q
->queue
[i
]);
3088 q
->nr
= q
->alloc
= 0;
3091 static void diffcore_apply_filter(const char *filter
)
3094 struct diff_queue_struct
*q
= &diff_queued_diff
;
3095 struct diff_queue_struct outq
;
3097 outq
.nr
= outq
.alloc
= 0;
3102 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3104 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3105 struct diff_filepair
*p
= q
->queue
[i
];
3106 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3108 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3110 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3111 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3112 strchr(filter
, p
->status
)))
3118 /* otherwise we will clear the whole queue
3119 * by copying the empty outq at the end of this
3120 * function, but first clear the current entries
3123 for (i
= 0; i
< q
->nr
; i
++)
3124 diff_free_filepair(q
->queue
[i
]);
3127 /* Only the matching ones */
3128 for (i
= 0; i
< q
->nr
; i
++) {
3129 struct diff_filepair
*p
= q
->queue
[i
];
3131 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3133 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3135 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3136 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3137 strchr(filter
, p
->status
)))
3140 diff_free_filepair(p
);
3147 /* Check whether two filespecs with the same mode and size are identical */
3148 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3149 struct diff_filespec
*two
)
3151 if (S_ISGITLINK(one
->mode
)) {
3152 diff_fill_sha1_info(one
);
3153 diff_fill_sha1_info(two
);
3154 return !hashcmp(one
->sha1
, two
->sha1
);
3156 if (diff_populate_filespec(one
, 0))
3158 if (diff_populate_filespec(two
, 0))
3160 return !memcmp(one
->data
, two
->data
, one
->size
);
3163 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3166 struct diff_queue_struct
*q
= &diff_queued_diff
;
3167 struct diff_queue_struct outq
;
3169 outq
.nr
= outq
.alloc
= 0;
3171 for (i
= 0; i
< q
->nr
; i
++) {
3172 struct diff_filepair
*p
= q
->queue
[i
];
3175 * 1. Entries that come from stat info dirtyness
3176 * always have both sides (iow, not create/delete),
3177 * one side of the object name is unknown, with
3178 * the same mode and size. Keep the ones that
3179 * do not match these criteria. They have real
3182 * 2. At this point, the file is known to be modified,
3183 * with the same mode and size, and the object
3184 * name of one side is unknown. Need to inspect
3185 * the identical contents.
3187 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3188 !DIFF_FILE_VALID(p
->two
) ||
3189 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3190 (p
->one
->mode
!= p
->two
->mode
) ||
3191 diff_populate_filespec(p
->one
, 1) ||
3192 diff_populate_filespec(p
->two
, 1) ||
3193 (p
->one
->size
!= p
->two
->size
) ||
3194 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3198 * The caller can subtract 1 from skip_stat_unmatch
3199 * to determine how many paths were dirty only
3200 * due to stat info mismatch.
3202 if (!diffopt
->no_index
)
3203 diffopt
->skip_stat_unmatch
++;
3204 diff_free_filepair(p
);
3211 void diffcore_std(struct diff_options
*options
)
3216 if (options
->skip_stat_unmatch
&& !options
->find_copies_harder
)
3217 diffcore_skip_stat_unmatch(options
);
3218 if (options
->break_opt
!= -1)
3219 diffcore_break(options
->break_opt
);
3220 if (options
->detect_rename
)
3221 diffcore_rename(options
);
3222 if (options
->break_opt
!= -1)
3223 diffcore_merge_broken();
3224 if (options
->pickaxe
)
3225 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3226 if (options
->orderfile
)
3227 diffcore_order(options
->orderfile
);
3228 diff_resolve_rename_copy();
3229 diffcore_apply_filter(options
->filter
);
3231 options
->has_changes
= !!diff_queued_diff
.nr
;
3235 void diff_addremove(struct diff_options
*options
,
3236 int addremove
, unsigned mode
,
3237 const unsigned char *sha1
,
3238 const char *base
, const char *path
)
3240 char concatpath
[PATH_MAX
];
3241 struct diff_filespec
*one
, *two
;
3243 /* This may look odd, but it is a preparation for
3244 * feeding "there are unchanged files which should
3245 * not produce diffs, but when you are doing copy
3246 * detection you would need them, so here they are"
3247 * entries to the diff-core. They will be prefixed
3248 * with something like '=' or '*' (I haven't decided
3249 * which but should not make any difference).
3250 * Feeding the same new and old to diff_change()
3251 * also has the same effect.
3252 * Before the final output happens, they are pruned after
3253 * merged into rename/copy pairs as appropriate.
3255 if (options
->reverse_diff
)
3256 addremove
= (addremove
== '+' ? '-' :
3257 addremove
== '-' ? '+' : addremove
);
3259 if (!path
) path
= "";
3260 sprintf(concatpath
, "%s%s", base
, path
);
3261 one
= alloc_filespec(concatpath
);
3262 two
= alloc_filespec(concatpath
);
3264 if (addremove
!= '+')
3265 fill_filespec(one
, sha1
, mode
);
3266 if (addremove
!= '-')
3267 fill_filespec(two
, sha1
, mode
);
3269 diff_queue(&diff_queued_diff
, one
, two
);
3270 options
->has_changes
= 1;
3273 void diff_change(struct diff_options
*options
,
3274 unsigned old_mode
, unsigned new_mode
,
3275 const unsigned char *old_sha1
,
3276 const unsigned char *new_sha1
,
3277 const char *base
, const char *path
)
3279 char concatpath
[PATH_MAX
];
3280 struct diff_filespec
*one
, *two
;
3282 if (options
->reverse_diff
) {
3284 const unsigned char *tmp_c
;
3285 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3286 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3288 if (!path
) path
= "";
3289 sprintf(concatpath
, "%s%s", base
, path
);
3290 one
= alloc_filespec(concatpath
);
3291 two
= alloc_filespec(concatpath
);
3292 fill_filespec(one
, old_sha1
, old_mode
);
3293 fill_filespec(two
, new_sha1
, new_mode
);
3295 diff_queue(&diff_queued_diff
, one
, two
);
3296 options
->has_changes
= 1;
3299 void diff_unmerge(struct diff_options
*options
,
3301 unsigned mode
, const unsigned char *sha1
)
3303 struct diff_filespec
*one
, *two
;
3304 one
= alloc_filespec(path
);
3305 two
= alloc_filespec(path
);
3306 fill_filespec(one
, sha1
, mode
);
3307 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;