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 use_size_cache
;
21 static int diff_detect_rename_default
;
22 static int diff_rename_limit_default
= -1;
23 static int diff_use_color_default
;
25 static char diff_colors
[][COLOR_MAXLEN
] = {
27 "", /* PLAIN (normal) */
28 "\033[1m", /* METAINFO (bold) */
29 "\033[36m", /* FRAGINFO (cyan) */
30 "\033[31m", /* OLD (red) */
31 "\033[32m", /* NEW (green) */
32 "\033[33m", /* COMMIT (yellow) */
33 "\033[41m", /* WHITESPACE (red background) */
36 static int parse_diff_color_slot(const char *var
, int ofs
)
38 if (!strcasecmp(var
+ofs
, "plain"))
40 if (!strcasecmp(var
+ofs
, "meta"))
42 if (!strcasecmp(var
+ofs
, "frag"))
44 if (!strcasecmp(var
+ofs
, "old"))
46 if (!strcasecmp(var
+ofs
, "new"))
48 if (!strcasecmp(var
+ofs
, "commit"))
50 if (!strcasecmp(var
+ofs
, "whitespace"))
51 return DIFF_WHITESPACE
;
52 die("bad config variable '%s'", var
);
55 static struct ll_diff_driver
{
57 struct ll_diff_driver
*next
;
59 } *user_diff
, **user_diff_tail
;
62 * Currently there is only "diff.<drivername>.command" variable;
63 * because there are "diff.color.<slot>" variables, we are parsing
64 * this in a bit convoluted way to allow low level diff driver
67 static int parse_lldiff_command(const char *var
, const char *ep
, const char *value
)
71 struct ll_diff_driver
*drv
;
75 for (drv
= user_diff
; drv
; drv
= drv
->next
)
76 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
80 drv
= xcalloc(1, sizeof(struct ll_diff_driver
));
81 namebuf
= xmalloc(namelen
+ 1);
82 memcpy(namebuf
, name
, namelen
);
87 user_diff_tail
= &user_diff
;
88 *user_diff_tail
= drv
;
89 user_diff_tail
= &(drv
->next
);
93 return error("%s: lacks value", var
);
94 drv
->cmd
= strdup(value
);
99 * These are to give UI layer defaults.
100 * The core-level commands such as git-diff-files should
101 * never be affected by the setting of diff.renames
102 * the user happens to have in the configuration file.
104 int git_diff_ui_config(const char *var
, const char *value
)
106 if (!strcmp(var
, "diff.renamelimit")) {
107 diff_rename_limit_default
= git_config_int(var
, value
);
110 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
111 diff_use_color_default
= git_config_colorbool(var
, value
);
114 if (!strcmp(var
, "diff.renames")) {
116 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
117 else if (!strcasecmp(value
, "copies") ||
118 !strcasecmp(value
, "copy"))
119 diff_detect_rename_default
= DIFF_DETECT_COPY
;
120 else if (git_config_bool(var
,value
))
121 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
124 if (!prefixcmp(var
, "diff.")) {
125 const char *ep
= strrchr(var
, '.');
127 if (ep
!= var
+ 4 && !strcmp(ep
, ".command"))
128 return parse_lldiff_command(var
, ep
, value
);
130 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
131 int slot
= parse_diff_color_slot(var
, 11);
132 color_parse(value
, var
, diff_colors
[slot
]);
136 return git_default_config(var
, value
);
139 static char *quote_one(const char *str
)
146 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
149 xp
= xmalloc(needlen
+ 1);
150 quote_c_style(str
, xp
, NULL
, 0);
154 static char *quote_two(const char *one
, const char *two
)
156 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
157 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
160 if (need_one
+ need_two
) {
161 if (!need_one
) need_one
= strlen(one
);
162 if (!need_two
) need_one
= strlen(two
);
164 xp
= xmalloc(need_one
+ need_two
+ 3);
166 quote_c_style(one
, xp
+ 1, NULL
, 1);
167 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
168 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
171 need_one
= strlen(one
);
172 need_two
= strlen(two
);
173 xp
= xmalloc(need_one
+ need_two
+ 1);
175 strcpy(xp
+ need_one
, two
);
179 static const char *external_diff(void)
181 static const char *external_diff_cmd
= NULL
;
182 static int done_preparing
= 0;
185 return external_diff_cmd
;
186 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
188 return external_diff_cmd
;
191 #define TEMPFILE_PATH_LEN 50
193 static struct diff_tempfile
{
194 const char *name
; /* filename external diff should read from */
197 char tmp_path
[TEMPFILE_PATH_LEN
];
200 static int count_lines(const char *data
, int size
)
202 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
209 completely_empty
= 0;
213 completely_empty
= 0;
216 if (completely_empty
)
219 count
++; /* no trailing newline */
223 static void print_line_count(int count
)
233 printf("1,%d", count
);
238 static void copy_file(int prefix
, const char *data
, int size
,
239 const char *set
, const char *reset
)
241 int ch
, nl_just_seen
= 1;
250 fputs(reset
, stdout
);
256 printf("%s\n\\ No newline at end of file\n", reset
);
259 static void emit_rewrite_diff(const char *name_a
,
261 struct diff_filespec
*one
,
262 struct diff_filespec
*two
,
266 const char *name_a_tab
, *name_b_tab
;
267 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
268 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
269 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
270 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
271 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
273 name_a
+= (*name_a
== '/');
274 name_b
+= (*name_b
== '/');
275 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
276 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
278 diff_populate_filespec(one
, 0);
279 diff_populate_filespec(two
, 0);
280 lc_a
= count_lines(one
->data
, one
->size
);
281 lc_b
= count_lines(two
->data
, two
->size
);
282 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
283 metainfo
, name_a
, name_a_tab
, reset
,
284 metainfo
, name_b
, name_b_tab
, reset
, fraginfo
);
285 print_line_count(lc_a
);
287 print_line_count(lc_b
);
288 printf(" @@%s\n", reset
);
290 copy_file('-', one
->data
, one
->size
, old
, reset
);
292 copy_file('+', two
->data
, two
->size
, new, reset
);
295 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
297 if (!DIFF_FILE_VALID(one
)) {
298 mf
->ptr
= (char *)""; /* does not matter */
302 else if (diff_populate_filespec(one
, 0))
305 mf
->size
= one
->size
;
309 struct diff_words_buffer
{
312 long current
; /* output pointer */
313 int suppressed_newline
;
316 static void diff_words_append(char *line
, unsigned long len
,
317 struct diff_words_buffer
*buffer
)
319 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
320 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
321 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
325 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
326 buffer
->text
.size
+= len
;
329 struct diff_words_data
{
330 struct xdiff_emit_state xm
;
331 struct diff_words_buffer minus
, plus
;
334 static void print_word(struct diff_words_buffer
*buffer
, int len
, int color
,
335 int suppress_newline
)
343 ptr
= buffer
->text
.ptr
+ buffer
->current
;
344 buffer
->current
+= len
;
346 if (ptr
[len
- 1] == '\n') {
351 fputs(diff_get_color(1, color
), stdout
);
352 fwrite(ptr
, len
, 1, stdout
);
353 fputs(diff_get_color(1, DIFF_RESET
), stdout
);
356 if (suppress_newline
)
357 buffer
->suppressed_newline
= 1;
363 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
365 struct diff_words_data
*diff_words
= priv
;
367 if (diff_words
->minus
.suppressed_newline
) {
370 diff_words
->minus
.suppressed_newline
= 0;
376 print_word(&diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
379 print_word(&diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
382 print_word(&diff_words
->plus
, len
, DIFF_PLAIN
, 0);
383 diff_words
->minus
.current
+= len
;
388 /* this executes the word diff on the accumulated buffers */
389 static void diff_words_show(struct diff_words_data
*diff_words
)
394 mmfile_t minus
, plus
;
397 minus
.size
= diff_words
->minus
.text
.size
;
398 minus
.ptr
= xmalloc(minus
.size
);
399 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
400 for (i
= 0; i
< minus
.size
; i
++)
401 if (isspace(minus
.ptr
[i
]))
403 diff_words
->minus
.current
= 0;
405 plus
.size
= diff_words
->plus
.text
.size
;
406 plus
.ptr
= xmalloc(plus
.size
);
407 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
408 for (i
= 0; i
< plus
.size
; i
++)
409 if (isspace(plus
.ptr
[i
]))
411 diff_words
->plus
.current
= 0;
413 xpp
.flags
= XDF_NEED_MINIMAL
;
414 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
416 ecb
.outf
= xdiff_outf
;
417 ecb
.priv
= diff_words
;
418 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
419 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
423 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
425 if (diff_words
->minus
.suppressed_newline
) {
427 diff_words
->minus
.suppressed_newline
= 0;
431 struct emit_callback
{
432 struct xdiff_emit_state xm
;
433 int nparents
, color_diff
;
434 const char **label_path
;
435 struct diff_words_data
*diff_words
;
439 static void free_diff_words_data(struct emit_callback
*ecbdata
)
441 if (ecbdata
->diff_words
) {
443 if (ecbdata
->diff_words
->minus
.text
.size
||
444 ecbdata
->diff_words
->plus
.text
.size
)
445 diff_words_show(ecbdata
->diff_words
);
447 if (ecbdata
->diff_words
->minus
.text
.ptr
)
448 free (ecbdata
->diff_words
->minus
.text
.ptr
);
449 if (ecbdata
->diff_words
->plus
.text
.ptr
)
450 free (ecbdata
->diff_words
->plus
.text
.ptr
);
451 free(ecbdata
->diff_words
);
452 ecbdata
->diff_words
= NULL
;
456 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
459 return diff_colors
[ix
];
463 static void emit_line(const char *set
, const char *reset
, const char *line
, int len
)
465 if (len
> 0 && line
[len
-1] == '\n')
468 fwrite(line
, len
, 1, stdout
);
472 static void emit_line_with_ws(int nparents
,
473 const char *set
, const char *reset
, const char *ws
,
474 const char *line
, int len
)
477 int last_tab_in_indent
= -1;
478 int last_space_in_indent
= -1;
481 int need_highlight_leading_space
= 0;
482 /* The line is a newly added line. Does it have funny leading
483 * whitespaces? In indent, SP should never precede a TAB.
485 for (i
= col0
; i
< len
; i
++) {
486 if (line
[i
] == '\t') {
487 last_tab_in_indent
= i
;
488 if (0 <= last_space_in_indent
)
489 need_highlight_leading_space
= 1;
491 else if (line
[i
] == ' ')
492 last_space_in_indent
= i
;
497 fwrite(line
, col0
, 1, stdout
);
498 fputs(reset
, stdout
);
499 if (((i
== len
) || line
[i
] == '\n') && i
!= col0
) {
500 /* The whole line was indent */
501 emit_line(ws
, reset
, line
+ col0
, len
- col0
);
505 if (need_highlight_leading_space
) {
506 while (i
< last_tab_in_indent
) {
507 if (line
[i
] == ' ') {
510 fputs(reset
, stdout
);
518 if (line
[tail
] == '\n' && i
< tail
)
521 if (!isspace(line
[tail
]))
525 if ((i
< tail
&& line
[tail
+ 1] != '\n')) {
526 /* This has whitespace between tail+1..len */
528 fwrite(line
+ i
, tail
- i
+ 1, 1, stdout
);
529 fputs(reset
, stdout
);
530 emit_line(ws
, reset
, line
+ tail
+ 1, len
- tail
- 1);
533 emit_line(set
, reset
, line
+ i
, len
- i
);
536 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
538 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
539 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
542 emit_line(set
, reset
, line
, len
);
544 emit_line_with_ws(ecbdata
->nparents
, set
, reset
, ws
,
548 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
552 struct emit_callback
*ecbdata
= priv
;
553 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
554 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
556 *(ecbdata
->found_changesp
) = 1;
558 if (ecbdata
->label_path
[0]) {
559 const char *name_a_tab
, *name_b_tab
;
561 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
562 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
564 printf("%s--- %s%s%s\n",
565 set
, ecbdata
->label_path
[0], reset
, name_a_tab
);
566 printf("%s+++ %s%s%s\n",
567 set
, ecbdata
->label_path
[1], reset
, name_b_tab
);
568 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
571 /* This is not really necessary for now because
572 * this codepath only deals with two-way diffs.
574 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
576 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
577 ecbdata
->nparents
= i
- 1;
578 emit_line(diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
583 if (len
< ecbdata
->nparents
) {
585 emit_line(reset
, reset
, line
, len
);
590 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
591 /* fall back to normal diff */
592 free_diff_words_data(ecbdata
);
593 if (ecbdata
->diff_words
) {
594 if (line
[0] == '-') {
595 diff_words_append(line
, len
,
596 &ecbdata
->diff_words
->minus
);
598 } else if (line
[0] == '+') {
599 diff_words_append(line
, len
,
600 &ecbdata
->diff_words
->plus
);
603 if (ecbdata
->diff_words
->minus
.text
.size
||
604 ecbdata
->diff_words
->plus
.text
.size
)
605 diff_words_show(ecbdata
->diff_words
);
608 emit_line(set
, reset
, line
, len
);
611 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
613 color
= DIFF_FILE_OLD
;
614 else if (line
[i
] == '+')
615 color
= DIFF_FILE_NEW
;
618 if (color
!= DIFF_FILE_NEW
) {
619 emit_line(diff_get_color(ecbdata
->color_diff
, color
),
623 emit_add_line(reset
, ecbdata
, line
, len
);
626 static char *pprint_rename(const char *a
, const char *b
)
631 int pfx_length
, sfx_length
;
632 int len_a
= strlen(a
);
633 int len_b
= strlen(b
);
634 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
635 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
637 if (qlen_a
|| qlen_b
) {
638 if (qlen_a
) len_a
= qlen_a
;
639 if (qlen_b
) len_b
= qlen_b
;
640 name
= xmalloc( len_a
+ len_b
+ 5 );
642 quote_c_style(a
, name
, NULL
, 0);
644 memcpy(name
, a
, len_a
);
645 memcpy(name
+ len_a
, " => ", 4);
647 quote_c_style(b
, name
+ len_a
+ 4, NULL
, 0);
649 memcpy(name
+ len_a
+ 4, b
, len_b
+ 1);
653 /* Find common prefix */
655 while (*old
&& *new && *old
== *new) {
657 pfx_length
= old
- a
+ 1;
662 /* Find common suffix */
666 while (a
<= old
&& b
<= new && *old
== *new) {
668 sfx_length
= len_a
- (old
- a
);
674 * pfx{mid-a => mid-b}sfx
675 * {pfx-a => pfx-b}sfx
676 * pfx{sfx-a => sfx-b}
679 if (pfx_length
+ sfx_length
) {
680 int a_midlen
= len_a
- pfx_length
- sfx_length
;
681 int b_midlen
= len_b
- pfx_length
- sfx_length
;
682 if (a_midlen
< 0) a_midlen
= 0;
683 if (b_midlen
< 0) b_midlen
= 0;
685 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
686 sprintf(name
, "%.*s{%.*s => %.*s}%s",
688 a_midlen
, a
+ pfx_length
,
689 b_midlen
, b
+ pfx_length
,
690 a
+ len_a
- sfx_length
);
693 name
= xmalloc(len_a
+ len_b
+ 5);
694 sprintf(name
, "%s => %s", a
, b
);
700 struct xdiff_emit_state xm
;
704 struct diffstat_file
{
706 unsigned is_unmerged
:1;
707 unsigned is_binary
:1;
708 unsigned is_renamed
:1;
709 unsigned int added
, deleted
;
713 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
717 struct diffstat_file
*x
;
718 x
= xcalloc(sizeof (*x
), 1);
719 if (diffstat
->nr
== diffstat
->alloc
) {
720 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
721 diffstat
->files
= xrealloc(diffstat
->files
,
722 diffstat
->alloc
* sizeof(x
));
724 diffstat
->files
[diffstat
->nr
++] = x
;
726 x
->name
= pprint_rename(name_a
, name_b
);
730 x
->name
= xstrdup(name_a
);
734 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
736 struct diffstat_t
*diffstat
= priv
;
737 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
741 else if (line
[0] == '-')
745 const char mime_boundary_leader
[] = "------------";
747 static int scale_linear(int it
, int width
, int max_change
)
750 * make sure that at least one '-' is printed if there were deletions,
751 * and likewise for '+'.
755 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
758 static void show_name(const char *prefix
, const char *name
, int len
,
759 const char *reset
, const char *set
)
761 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
764 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
774 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
776 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
777 int max_change
= 0, max_len
= 0;
778 int total_files
= data
->nr
;
779 int width
, name_width
;
780 const char *reset
, *set
, *add_c
, *del_c
;
785 width
= options
->stat_width
? options
->stat_width
: 80;
786 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
788 /* Sanity: give at least 5 columns to the graph,
789 * but leave at least 10 columns for the name.
791 if (width
< name_width
+ 15) {
792 if (name_width
<= 25)
793 width
= name_width
+ 15;
795 name_width
= width
- 15;
798 /* Find the longest filename and max number of changes */
799 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
800 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
801 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
802 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
804 for (i
= 0; i
< data
->nr
; i
++) {
805 struct diffstat_file
*file
= data
->files
[i
];
806 int change
= file
->added
+ file
->deleted
;
808 if (!file
->is_renamed
) { /* renames are already quoted by pprint_rename */
809 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
811 char *qname
= xmalloc(len
+ 1);
812 quote_c_style(file
->name
, qname
, NULL
, 0);
818 len
= strlen(file
->name
);
822 if (file
->is_binary
|| file
->is_unmerged
)
824 if (max_change
< change
)
828 /* Compute the width of the graph part;
829 * 10 is for one blank at the beginning of the line plus
830 * " | count " between the name and the graph.
832 * From here on, name_width is the width of the name area,
833 * and width is the width of the graph area.
835 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
836 if (width
< (name_width
+ 10) + max_change
)
837 width
= width
- (name_width
+ 10);
841 for (i
= 0; i
< data
->nr
; i
++) {
842 const char *prefix
= "";
843 char *name
= data
->files
[i
]->name
;
844 int added
= data
->files
[i
]->added
;
845 int deleted
= data
->files
[i
]->deleted
;
849 * "scale" the filename
852 name_len
= strlen(name
);
853 if (name_width
< name_len
) {
857 name
+= name_len
- len
;
858 slash
= strchr(name
, '/');
863 if (data
->files
[i
]->is_binary
) {
864 show_name(prefix
, name
, len
, reset
, set
);
866 printf("%s%d%s", del_c
, deleted
, reset
);
868 printf("%s%d%s", add_c
, added
, reset
);
871 goto free_diffstat_file
;
873 else if (data
->files
[i
]->is_unmerged
) {
874 show_name(prefix
, name
, len
, reset
, set
);
875 printf(" Unmerged\n");
876 goto free_diffstat_file
;
878 else if (!data
->files
[i
]->is_renamed
&&
879 (added
+ deleted
== 0)) {
881 goto free_diffstat_file
;
885 * scale the add/delete
893 if (width
<= max_change
) {
894 add
= scale_linear(add
, width
, max_change
);
895 del
= scale_linear(del
, width
, max_change
);
898 show_name(prefix
, name
, len
, reset
, set
);
899 printf("%5d ", added
+ deleted
);
900 show_graph('+', add
, add_c
, reset
);
901 show_graph('-', del
, del_c
, reset
);
904 free(data
->files
[i
]->name
);
905 free(data
->files
[i
]);
908 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
909 set
, total_files
, adds
, dels
, reset
);
912 static void show_shortstats(struct diffstat_t
* data
)
914 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
919 for (i
= 0; i
< data
->nr
; i
++) {
920 if (!data
->files
[i
]->is_binary
&&
921 !data
->files
[i
]->is_unmerged
) {
922 int added
= data
->files
[i
]->added
;
923 int deleted
= data
->files
[i
]->deleted
;
924 if (!data
->files
[i
]->is_renamed
&&
925 (added
+ deleted
== 0)) {
932 free(data
->files
[i
]->name
);
933 free(data
->files
[i
]);
937 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
938 total_files
, adds
, dels
);
941 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
945 for (i
= 0; i
< data
->nr
; i
++) {
946 struct diffstat_file
*file
= data
->files
[i
];
951 printf("%d\t%d\t", file
->added
, file
->deleted
);
952 if (options
->line_termination
&& !file
->is_renamed
&&
953 quote_c_style(file
->name
, NULL
, NULL
, 0))
954 quote_c_style(file
->name
, NULL
, stdout
, 0);
956 fputs(file
->name
, stdout
);
957 putchar(options
->line_termination
);
962 struct xdiff_emit_state xm
;
963 const char *filename
;
964 int lineno
, color_diff
;
967 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
969 struct checkdiff_t
*data
= priv
;
970 const char *ws
= diff_get_color(data
->color_diff
, DIFF_WHITESPACE
);
971 const char *reset
= diff_get_color(data
->color_diff
, DIFF_RESET
);
972 const char *set
= diff_get_color(data
->color_diff
, DIFF_FILE_NEW
);
974 if (line
[0] == '+') {
975 int i
, spaces
= 0, space_before_tab
= 0, white_space_at_end
= 0;
977 /* check space before tab */
978 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
981 if (line
[i
- 1] == '\t' && spaces
)
982 space_before_tab
= 1;
984 /* check white space at line end */
985 if (line
[len
- 1] == '\n')
987 if (isspace(line
[len
- 1]))
988 white_space_at_end
= 1;
990 if (space_before_tab
|| white_space_at_end
) {
991 printf("%s:%d: %s", data
->filename
, data
->lineno
, ws
);
992 if (space_before_tab
) {
993 printf("space before tab");
994 if (white_space_at_end
)
997 if (white_space_at_end
)
998 printf("white space at end");
999 printf(":%s ", reset
);
1000 emit_line_with_ws(1, set
, reset
, ws
, line
, len
);
1004 } else if (line
[0] == ' ')
1006 else if (line
[0] == '@') {
1007 char *plus
= strchr(line
, '+');
1009 data
->lineno
= strtol(plus
, NULL
, 10);
1011 die("invalid diff");
1015 static unsigned char *deflate_it(char *data
,
1017 unsigned long *result_size
)
1020 unsigned char *deflated
;
1023 memset(&stream
, 0, sizeof(stream
));
1024 deflateInit(&stream
, zlib_compression_level
);
1025 bound
= deflateBound(&stream
, size
);
1026 deflated
= xmalloc(bound
);
1027 stream
.next_out
= deflated
;
1028 stream
.avail_out
= bound
;
1030 stream
.next_in
= (unsigned char *)data
;
1031 stream
.avail_in
= size
;
1032 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1034 deflateEnd(&stream
);
1035 *result_size
= stream
.total_out
;
1039 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
1045 unsigned long orig_size
;
1046 unsigned long delta_size
;
1047 unsigned long deflate_size
;
1048 unsigned long data_size
;
1050 /* We could do deflated delta, or we could do just deflated two,
1051 * whichever is smaller.
1054 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1055 if (one
->size
&& two
->size
) {
1056 delta
= diff_delta(one
->ptr
, one
->size
,
1057 two
->ptr
, two
->size
,
1058 &delta_size
, deflate_size
);
1060 void *to_free
= delta
;
1061 orig_size
= delta_size
;
1062 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1067 if (delta
&& delta_size
< deflate_size
) {
1068 printf("delta %lu\n", orig_size
);
1071 data_size
= delta_size
;
1074 printf("literal %lu\n", two
->size
);
1077 data_size
= deflate_size
;
1080 /* emit data encoded in base85 */
1083 int bytes
= (52 < data_size
) ? 52 : data_size
;
1087 line
[0] = bytes
+ 'A' - 1;
1089 line
[0] = bytes
- 26 + 'a' - 1;
1090 encode_85(line
+ 1, cp
, bytes
);
1091 cp
= (char *) cp
+ bytes
;
1098 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
1100 printf("GIT binary patch\n");
1101 emit_binary_diff_body(one
, two
);
1102 emit_binary_diff_body(two
, one
);
1105 static void setup_diff_attr_check(struct git_attr_check
*check
)
1107 static struct git_attr
*attr_diff
;
1110 attr_diff
= git_attr("diff", 4);
1111 check
->attr
= attr_diff
;
1114 #define FIRST_FEW_BYTES 8000
1115 static int file_is_binary(struct diff_filespec
*one
)
1118 struct git_attr_check attr_diff_check
;
1120 setup_diff_attr_check(&attr_diff_check
);
1121 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1122 const char *value
= attr_diff_check
.value
;
1123 if (ATTR_TRUE(value
))
1125 else if (ATTR_FALSE(value
))
1130 if (!DIFF_FILE_VALID(one
))
1132 diff_populate_filespec(one
, 0);
1135 if (FIRST_FEW_BYTES
< sz
)
1136 sz
= FIRST_FEW_BYTES
;
1137 return !!memchr(one
->data
, 0, sz
);
1140 static void builtin_diff(const char *name_a
,
1142 struct diff_filespec
*one
,
1143 struct diff_filespec
*two
,
1144 const char *xfrm_msg
,
1145 struct diff_options
*o
,
1146 int complete_rewrite
)
1150 char *a_one
, *b_two
;
1151 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
1152 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
1154 a_one
= quote_two("a/", name_a
+ (*name_a
== '/'));
1155 b_two
= quote_two("b/", name_b
+ (*name_b
== '/'));
1156 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1157 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1158 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1159 if (lbl
[0][0] == '/') {
1161 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1162 if (xfrm_msg
&& xfrm_msg
[0])
1163 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1165 else if (lbl
[1][0] == '/') {
1166 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1167 if (xfrm_msg
&& xfrm_msg
[0])
1168 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1171 if (one
->mode
!= two
->mode
) {
1172 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
1173 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
1175 if (xfrm_msg
&& xfrm_msg
[0])
1176 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1178 * we do not run diff between different kind
1181 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1182 goto free_ab_and_return
;
1183 if (complete_rewrite
) {
1184 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1186 o
->found_changes
= 1;
1187 goto free_ab_and_return
;
1191 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1192 die("unable to read files to diff");
1194 if (!o
->text
&& (file_is_binary(one
) || file_is_binary(two
))) {
1195 /* Quite common confusing case */
1196 if (mf1
.size
== mf2
.size
&&
1197 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1198 goto free_ab_and_return
;
1200 emit_binary_diff(&mf1
, &mf2
);
1202 printf("Binary files %s and %s differ\n",
1204 o
->found_changes
= 1;
1207 /* Crazy xdl interfaces.. */
1208 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1212 struct emit_callback ecbdata
;
1214 memset(&ecbdata
, 0, sizeof(ecbdata
));
1215 ecbdata
.label_path
= lbl
;
1216 ecbdata
.color_diff
= o
->color_diff
;
1217 ecbdata
.found_changesp
= &o
->found_changes
;
1218 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1219 xecfg
.ctxlen
= o
->context
;
1220 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1223 else if (!prefixcmp(diffopts
, "--unified="))
1224 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1225 else if (!prefixcmp(diffopts
, "-u"))
1226 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1227 ecb
.outf
= xdiff_outf
;
1228 ecb
.priv
= &ecbdata
;
1229 ecbdata
.xm
.consume
= fn_out_consume
;
1230 if (o
->color_diff_words
)
1231 ecbdata
.diff_words
=
1232 xcalloc(1, sizeof(struct diff_words_data
));
1233 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1234 if (o
->color_diff_words
)
1235 free_diff_words_data(&ecbdata
);
1244 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1245 struct diff_filespec
*one
,
1246 struct diff_filespec
*two
,
1247 struct diffstat_t
*diffstat
,
1248 struct diff_options
*o
,
1249 int complete_rewrite
)
1252 struct diffstat_file
*data
;
1254 data
= diffstat_add(diffstat
, name_a
, name_b
);
1257 data
->is_unmerged
= 1;
1260 if (complete_rewrite
) {
1261 diff_populate_filespec(one
, 0);
1262 diff_populate_filespec(two
, 0);
1263 data
->deleted
= count_lines(one
->data
, one
->size
);
1264 data
->added
= count_lines(two
->data
, two
->size
);
1267 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1268 die("unable to read files to diff");
1270 if (file_is_binary(one
) || file_is_binary(two
)) {
1271 data
->is_binary
= 1;
1272 data
->added
= mf2
.size
;
1273 data
->deleted
= mf1
.size
;
1275 /* Crazy xdl interfaces.. */
1280 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1283 ecb
.outf
= xdiff_outf
;
1284 ecb
.priv
= diffstat
;
1285 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1289 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1290 struct diff_filespec
*one
,
1291 struct diff_filespec
*two
, struct diff_options
*o
)
1294 struct checkdiff_t data
;
1299 memset(&data
, 0, sizeof(data
));
1300 data
.xm
.consume
= checkdiff_consume
;
1301 data
.filename
= name_b
? name_b
: name_a
;
1303 data
.color_diff
= o
->color_diff
;
1305 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1306 die("unable to read files to diff");
1308 if (file_is_binary(two
))
1311 /* Crazy xdl interfaces.. */
1316 xpp
.flags
= XDF_NEED_MINIMAL
;
1319 ecb
.outf
= xdiff_outf
;
1321 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1325 struct diff_filespec
*alloc_filespec(const char *path
)
1327 int namelen
= strlen(path
);
1328 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1330 memset(spec
, 0, sizeof(*spec
));
1331 spec
->path
= (char *)(spec
+ 1);
1332 memcpy(spec
->path
, path
, namelen
+1);
1336 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1337 unsigned short mode
)
1340 spec
->mode
= canon_mode(mode
);
1341 hashcpy(spec
->sha1
, sha1
);
1342 spec
->sha1_valid
= !is_null_sha1(sha1
);
1347 * Given a name and sha1 pair, if the dircache tells us the file in
1348 * the work tree has that object contents, return true, so that
1349 * prepare_temp_file() does not have to inflate and extract.
1351 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1353 struct cache_entry
*ce
;
1357 /* We do not read the cache ourselves here, because the
1358 * benchmark with my previous version that always reads cache
1359 * shows that it makes things worse for diff-tree comparing
1360 * two linux-2.6 kernel trees in an already checked out work
1361 * tree. This is because most diff-tree comparisons deal with
1362 * only a small number of files, while reading the cache is
1363 * expensive for a large project, and its cost outweighs the
1364 * savings we get by not inflating the object to a temporary
1365 * file. Practically, this code only helps when we are used
1366 * by diff-cache --cached, which does read the cache before
1372 /* We want to avoid the working directory if our caller
1373 * doesn't need the data in a normal file, this system
1374 * is rather slow with its stat/open/mmap/close syscalls,
1375 * and the object is contained in a pack file. The pack
1376 * is probably already open and will be faster to obtain
1377 * the data through than the working directory. Loose
1378 * objects however would tend to be slower as they need
1379 * to be individually opened and inflated.
1381 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1385 pos
= cache_name_pos(name
, len
);
1388 ce
= active_cache
[pos
];
1389 if ((lstat(name
, &st
) < 0) ||
1390 !S_ISREG(st
.st_mode
) || /* careful! */
1391 ce_match_stat(ce
, &st
, 0) ||
1392 hashcmp(sha1
, ce
->sha1
))
1394 /* we return 1 only when we can stat, it is a regular file,
1395 * stat information matches, and sha1 recorded in the cache
1396 * matches. I.e. we know the file in the work tree really is
1397 * the same as the <name, sha1> pair.
1402 static struct sha1_size_cache
{
1403 unsigned char sha1
[20];
1405 } **sha1_size_cache
;
1406 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
1408 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
1413 struct sha1_size_cache
*e
;
1416 last
= sha1_size_cache_nr
;
1417 while (last
> first
) {
1418 int cmp
, next
= (last
+ first
) >> 1;
1419 e
= sha1_size_cache
[next
];
1420 cmp
= hashcmp(e
->sha1
, sha1
);
1432 /* insert to make it at "first" */
1433 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
1434 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
1435 sha1_size_cache
= xrealloc(sha1_size_cache
,
1436 sha1_size_cache_alloc
*
1437 sizeof(*sha1_size_cache
));
1439 sha1_size_cache_nr
++;
1440 if (first
< sha1_size_cache_nr
)
1441 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
1442 (sha1_size_cache_nr
- first
- 1) *
1443 sizeof(*sha1_size_cache
));
1444 e
= xmalloc(sizeof(struct sha1_size_cache
));
1445 sha1_size_cache
[first
] = e
;
1446 hashcpy(e
->sha1
, sha1
);
1451 static int populate_from_stdin(struct diff_filespec
*s
)
1453 #define INCREMENT 1024
1461 buf
= xrealloc(buf
, size
+ INCREMENT
);
1462 got
= xread(0, buf
+ size
, INCREMENT
);
1466 return error("error while reading from stdin %s",
1470 s
->should_munmap
= 0;
1477 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1480 char *data
= xmalloc(100);
1481 len
= snprintf(data
, 100,
1482 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1494 * While doing rename detection and pickaxe operation, we may need to
1495 * grab the data for the blob (or file) for our own in-core comparison.
1496 * diff_filespec has data and size fields for this purpose.
1498 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1501 if (!DIFF_FILE_VALID(s
))
1502 die("internal error: asking to populate invalid file.");
1503 if (S_ISDIR(s
->mode
))
1506 if (!use_size_cache
)
1512 if (S_ISDIRLNK(s
->mode
))
1513 return diff_populate_gitlink(s
, size_only
);
1515 if (!s
->sha1_valid
||
1516 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1522 if (!strcmp(s
->path
, "-"))
1523 return populate_from_stdin(s
);
1525 if (lstat(s
->path
, &st
) < 0) {
1526 if (errno
== ENOENT
) {
1530 s
->data
= (char *)"";
1535 s
->size
= xsize_t(st
.st_size
);
1540 if (S_ISLNK(st
.st_mode
)) {
1542 s
->data
= xmalloc(s
->size
);
1544 ret
= readlink(s
->path
, s
->data
, s
->size
);
1551 fd
= open(s
->path
, O_RDONLY
);
1554 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1556 s
->should_munmap
= 1;
1559 * Convert from working tree format to canonical git format
1562 buf
= convert_to_git(s
->path
, s
->data
, &size
);
1564 munmap(s
->data
, s
->size
);
1565 s
->should_munmap
= 0;
1572 enum object_type type
;
1573 struct sha1_size_cache
*e
;
1576 e
= locate_size_cache(s
->sha1
, 1, 0);
1581 type
= sha1_object_info(s
->sha1
, &s
->size
);
1583 locate_size_cache(s
->sha1
, 0, s
->size
);
1586 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1593 void diff_free_filespec_data(struct diff_filespec
*s
)
1597 else if (s
->should_munmap
)
1598 munmap(s
->data
, s
->size
);
1599 s
->should_free
= s
->should_munmap
= 0;
1605 static void prep_temp_blob(struct diff_tempfile
*temp
,
1608 const unsigned char *sha1
,
1613 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1615 die("unable to create temp-file");
1616 if (write_in_full(fd
, blob
, size
) != size
)
1617 die("unable to write temp-file");
1619 temp
->name
= temp
->tmp_path
;
1620 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1622 sprintf(temp
->mode
, "%06o", mode
);
1625 static void prepare_temp_file(const char *name
,
1626 struct diff_tempfile
*temp
,
1627 struct diff_filespec
*one
)
1629 if (!DIFF_FILE_VALID(one
)) {
1631 /* A '-' entry produces this for file-2, and
1632 * a '+' entry produces this for file-1.
1634 temp
->name
= "/dev/null";
1635 strcpy(temp
->hex
, ".");
1636 strcpy(temp
->mode
, ".");
1640 if (!one
->sha1_valid
||
1641 reuse_worktree_file(name
, one
->sha1
, 1)) {
1643 if (lstat(name
, &st
) < 0) {
1644 if (errno
== ENOENT
)
1645 goto not_a_valid_file
;
1646 die("stat(%s): %s", name
, strerror(errno
));
1648 if (S_ISLNK(st
.st_mode
)) {
1650 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1651 size_t sz
= xsize_t(st
.st_size
);
1652 if (sizeof(buf
) <= st
.st_size
)
1653 die("symlink too long: %s", name
);
1654 ret
= readlink(name
, buf
, sz
);
1656 die("readlink(%s)", name
);
1657 prep_temp_blob(temp
, buf
, sz
,
1659 one
->sha1
: null_sha1
),
1661 one
->mode
: S_IFLNK
));
1664 /* we can borrow from the file in the work tree */
1666 if (!one
->sha1_valid
)
1667 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1669 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1670 /* Even though we may sometimes borrow the
1671 * contents from the work tree, we always want
1672 * one->mode. mode is trustworthy even when
1673 * !(one->sha1_valid), as long as
1674 * DIFF_FILE_VALID(one).
1676 sprintf(temp
->mode
, "%06o", one
->mode
);
1681 if (diff_populate_filespec(one
, 0))
1682 die("cannot read data blob for %s", one
->path
);
1683 prep_temp_blob(temp
, one
->data
, one
->size
,
1684 one
->sha1
, one
->mode
);
1688 static void remove_tempfile(void)
1692 for (i
= 0; i
< 2; i
++)
1693 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1694 unlink(diff_temp
[i
].name
);
1695 diff_temp
[i
].name
= NULL
;
1699 static void remove_tempfile_on_signal(int signo
)
1702 signal(SIGINT
, SIG_DFL
);
1706 static int spawn_prog(const char *pgm
, const char **arg
)
1714 die("unable to fork");
1716 execvp(pgm
, (char *const*) arg
);
1720 while (waitpid(pid
, &status
, 0) < 0) {
1726 /* Earlier we did not check the exit status because
1727 * diff exits non-zero if files are different, and
1728 * we are not interested in knowing that. It was a
1729 * mistake which made it harder to quit a diff-*
1730 * session that uses the git-apply-patch-script as
1731 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1732 * should also exit non-zero only when it wants to
1733 * abort the entire diff-* session.
1735 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1740 /* An external diff command takes:
1742 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1743 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1746 static void run_external_diff(const char *pgm
,
1749 struct diff_filespec
*one
,
1750 struct diff_filespec
*two
,
1751 const char *xfrm_msg
,
1752 int complete_rewrite
)
1754 const char *spawn_arg
[10];
1755 struct diff_tempfile
*temp
= diff_temp
;
1757 static int atexit_asked
= 0;
1758 const char *othername
;
1759 const char **arg
= &spawn_arg
[0];
1761 othername
= (other
? other
: name
);
1763 prepare_temp_file(name
, &temp
[0], one
);
1764 prepare_temp_file(othername
, &temp
[1], two
);
1765 if (! atexit_asked
&&
1766 (temp
[0].name
== temp
[0].tmp_path
||
1767 temp
[1].name
== temp
[1].tmp_path
)) {
1769 atexit(remove_tempfile
);
1771 signal(SIGINT
, remove_tempfile_on_signal
);
1777 *arg
++ = temp
[0].name
;
1778 *arg
++ = temp
[0].hex
;
1779 *arg
++ = temp
[0].mode
;
1780 *arg
++ = temp
[1].name
;
1781 *arg
++ = temp
[1].hex
;
1782 *arg
++ = temp
[1].mode
;
1792 retval
= spawn_prog(pgm
, spawn_arg
);
1795 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1800 static const char *external_diff_attr(const char *name
)
1802 struct git_attr_check attr_diff_check
;
1804 setup_diff_attr_check(&attr_diff_check
);
1805 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
1806 const char *value
= attr_diff_check
.value
;
1807 if (!ATTR_TRUE(value
) &&
1808 !ATTR_FALSE(value
) &&
1809 !ATTR_UNSET(value
)) {
1810 struct ll_diff_driver
*drv
;
1812 if (!user_diff_tail
) {
1813 user_diff_tail
= &user_diff
;
1814 git_config(git_diff_ui_config
);
1816 for (drv
= user_diff
; drv
; drv
= drv
->next
)
1817 if (!strcmp(drv
->name
, value
))
1824 static void run_diff_cmd(const char *pgm
,
1827 struct diff_filespec
*one
,
1828 struct diff_filespec
*two
,
1829 const char *xfrm_msg
,
1830 struct diff_options
*o
,
1831 int complete_rewrite
)
1833 if (!o
->allow_external
)
1836 const char *cmd
= external_diff_attr(name
);
1842 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1847 builtin_diff(name
, other
? other
: name
,
1848 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1850 printf("* Unmerged path %s\n", name
);
1853 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1855 if (DIFF_FILE_VALID(one
)) {
1856 if (!one
->sha1_valid
) {
1858 if (!strcmp(one
->path
, "-")) {
1859 hashcpy(one
->sha1
, null_sha1
);
1862 if (lstat(one
->path
, &st
) < 0)
1863 die("stat %s", one
->path
);
1864 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1865 die("cannot hash %s\n", one
->path
);
1872 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1874 const char *pgm
= external_diff();
1875 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1876 struct diff_filespec
*one
;
1877 struct diff_filespec
*two
;
1880 char *name_munged
, *other_munged
;
1881 int complete_rewrite
= 0;
1884 if (DIFF_PAIR_UNMERGED(p
)) {
1886 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1890 name
= p
->one
->path
;
1891 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1892 name_munged
= quote_one(name
);
1893 other_munged
= quote_one(other
);
1894 one
= p
->one
; two
= p
->two
;
1896 diff_fill_sha1_info(one
);
1897 diff_fill_sha1_info(two
);
1900 switch (p
->status
) {
1901 case DIFF_STATUS_COPIED
:
1902 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1903 "similarity index %d%%\n"
1906 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1907 name_munged
, other_munged
);
1909 case DIFF_STATUS_RENAMED
:
1910 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1911 "similarity index %d%%\n"
1914 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1915 name_munged
, other_munged
);
1917 case DIFF_STATUS_MODIFIED
:
1919 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1920 "dissimilarity index %d%%\n",
1921 (int)(0.5 + p
->score
*
1923 complete_rewrite
= 1;
1932 if (hashcmp(one
->sha1
, two
->sha1
)) {
1933 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1937 if ((!fill_mmfile(&mf
, one
) && file_is_binary(one
)) ||
1938 (!fill_mmfile(&mf
, two
) && file_is_binary(two
)))
1941 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1943 abbrev
, sha1_to_hex(one
->sha1
),
1944 abbrev
, sha1_to_hex(two
->sha1
));
1945 if (one
->mode
== two
->mode
)
1946 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1947 " %06o", one
->mode
);
1948 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1953 xfrm_msg
= len
? msg
: NULL
;
1956 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1957 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1958 /* a filepair that changes between file and symlink
1959 * needs to be split into deletion and creation.
1961 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1962 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1964 null
= alloc_filespec(one
->path
);
1965 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1969 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1976 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1977 struct diffstat_t
*diffstat
)
1981 int complete_rewrite
= 0;
1983 if (DIFF_PAIR_UNMERGED(p
)) {
1985 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1989 name
= p
->one
->path
;
1990 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1992 diff_fill_sha1_info(p
->one
);
1993 diff_fill_sha1_info(p
->two
);
1995 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1996 complete_rewrite
= 1;
1997 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2000 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2005 if (DIFF_PAIR_UNMERGED(p
)) {
2010 name
= p
->one
->path
;
2011 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2013 diff_fill_sha1_info(p
->one
);
2014 diff_fill_sha1_info(p
->two
);
2016 builtin_checkdiff(name
, other
, p
->one
, p
->two
, o
);
2019 void diff_setup(struct diff_options
*options
)
2021 memset(options
, 0, sizeof(*options
));
2022 options
->line_termination
= '\n';
2023 options
->break_opt
= -1;
2024 options
->rename_limit
= -1;
2025 options
->context
= 3;
2026 options
->msg_sep
= "";
2028 options
->change
= diff_change
;
2029 options
->add_remove
= diff_addremove
;
2030 options
->color_diff
= diff_use_color_default
;
2031 options
->detect_rename
= diff_detect_rename_default
;
2034 int diff_setup_done(struct diff_options
*options
)
2038 if (options
->output_format
& DIFF_FORMAT_NAME
)
2040 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2042 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2044 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2047 die("--name-only, --name-status, --check and -s are mutually exclusive");
2049 if (options
->find_copies_harder
)
2050 options
->detect_rename
= DIFF_DETECT_COPY
;
2052 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2053 DIFF_FORMAT_NAME_STATUS
|
2054 DIFF_FORMAT_CHECKDIFF
|
2055 DIFF_FORMAT_NO_OUTPUT
))
2056 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2057 DIFF_FORMAT_NUMSTAT
|
2058 DIFF_FORMAT_DIFFSTAT
|
2059 DIFF_FORMAT_SHORTSTAT
|
2060 DIFF_FORMAT_SUMMARY
|
2064 * These cases always need recursive; we do not drop caller-supplied
2065 * recursive bits for other formats here.
2067 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2068 DIFF_FORMAT_NUMSTAT
|
2069 DIFF_FORMAT_DIFFSTAT
|
2070 DIFF_FORMAT_SHORTSTAT
|
2071 DIFF_FORMAT_SUMMARY
|
2072 DIFF_FORMAT_CHECKDIFF
))
2073 options
->recursive
= 1;
2075 * Also pickaxe would not work very well if you do not say recursive
2077 if (options
->pickaxe
)
2078 options
->recursive
= 1;
2080 if (options
->detect_rename
&& options
->rename_limit
< 0)
2081 options
->rename_limit
= diff_rename_limit_default
;
2082 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2084 /* read-cache does not die even when it fails
2085 * so it is safe for us to do this here. Also
2086 * it does not smudge active_cache or active_nr
2087 * when it fails, so we do not have to worry about
2088 * cleaning it up ourselves either.
2092 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
2094 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2095 options
->abbrev
= 40; /* full */
2098 * It does not make sense to show the first hit we happened
2099 * to have found. It does not make sense not to return with
2100 * exit code in such a case either.
2102 if (options
->quiet
) {
2103 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2104 options
->exit_with_status
= 1;
2108 * If we postprocess in diffcore, we cannot simply return
2109 * upon the first hit. We need to run diff as usual.
2111 if (options
->pickaxe
|| options
->filter
)
2117 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2127 if (c
== arg_short
) {
2131 if (val
&& isdigit(c
)) {
2133 int n
= strtoul(arg
, &end
, 10);
2144 eq
= strchr(arg
, '=');
2149 if (!len
|| strncmp(arg
, arg_long
, len
))
2154 if (!isdigit(*++eq
))
2156 n
= strtoul(eq
, &end
, 10);
2164 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2166 const char *arg
= av
[0];
2167 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2168 options
->output_format
|= DIFF_FORMAT_PATCH
;
2169 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2170 options
->output_format
|= DIFF_FORMAT_PATCH
;
2171 else if (!strcmp(arg
, "--raw"))
2172 options
->output_format
|= DIFF_FORMAT_RAW
;
2173 else if (!strcmp(arg
, "--patch-with-raw")) {
2174 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2176 else if (!strcmp(arg
, "--numstat")) {
2177 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2179 else if (!strcmp(arg
, "--shortstat")) {
2180 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2182 else if (!prefixcmp(arg
, "--stat")) {
2184 int width
= options
->stat_width
;
2185 int name_width
= options
->stat_name_width
;
2191 if (!prefixcmp(arg
, "-width="))
2192 width
= strtoul(arg
+ 7, &end
, 10);
2193 else if (!prefixcmp(arg
, "-name-width="))
2194 name_width
= strtoul(arg
+ 12, &end
, 10);
2197 width
= strtoul(arg
+1, &end
, 10);
2199 name_width
= strtoul(end
+1, &end
, 10);
2202 /* Important! This checks all the error cases! */
2205 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2206 options
->stat_name_width
= name_width
;
2207 options
->stat_width
= width
;
2209 else if (!strcmp(arg
, "--check"))
2210 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2211 else if (!strcmp(arg
, "--summary"))
2212 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2213 else if (!strcmp(arg
, "--patch-with-stat")) {
2214 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2216 else if (!strcmp(arg
, "-z"))
2217 options
->line_termination
= 0;
2218 else if (!prefixcmp(arg
, "-l"))
2219 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2220 else if (!strcmp(arg
, "--full-index"))
2221 options
->full_index
= 1;
2222 else if (!strcmp(arg
, "--binary")) {
2223 options
->output_format
|= DIFF_FORMAT_PATCH
;
2224 options
->binary
= 1;
2226 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
2229 else if (!strcmp(arg
, "--name-only"))
2230 options
->output_format
|= DIFF_FORMAT_NAME
;
2231 else if (!strcmp(arg
, "--name-status"))
2232 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2233 else if (!strcmp(arg
, "-R"))
2234 options
->reverse_diff
= 1;
2235 else if (!prefixcmp(arg
, "-S"))
2236 options
->pickaxe
= arg
+ 2;
2237 else if (!strcmp(arg
, "-s")) {
2238 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2240 else if (!prefixcmp(arg
, "-O"))
2241 options
->orderfile
= arg
+ 2;
2242 else if (!prefixcmp(arg
, "--diff-filter="))
2243 options
->filter
= arg
+ 14;
2244 else if (!strcmp(arg
, "--pickaxe-all"))
2245 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2246 else if (!strcmp(arg
, "--pickaxe-regex"))
2247 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2248 else if (!prefixcmp(arg
, "-B")) {
2249 if ((options
->break_opt
=
2250 diff_scoreopt_parse(arg
)) == -1)
2253 else if (!prefixcmp(arg
, "-M")) {
2254 if ((options
->rename_score
=
2255 diff_scoreopt_parse(arg
)) == -1)
2257 options
->detect_rename
= DIFF_DETECT_RENAME
;
2259 else if (!prefixcmp(arg
, "-C")) {
2260 if ((options
->rename_score
=
2261 diff_scoreopt_parse(arg
)) == -1)
2263 options
->detect_rename
= DIFF_DETECT_COPY
;
2265 else if (!strcmp(arg
, "--find-copies-harder"))
2266 options
->find_copies_harder
= 1;
2267 else if (!strcmp(arg
, "--abbrev"))
2268 options
->abbrev
= DEFAULT_ABBREV
;
2269 else if (!prefixcmp(arg
, "--abbrev=")) {
2270 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2271 if (options
->abbrev
< MINIMUM_ABBREV
)
2272 options
->abbrev
= MINIMUM_ABBREV
;
2273 else if (40 < options
->abbrev
)
2274 options
->abbrev
= 40;
2276 else if (!strcmp(arg
, "--color"))
2277 options
->color_diff
= 1;
2278 else if (!strcmp(arg
, "--no-color"))
2279 options
->color_diff
= 0;
2280 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2281 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2282 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2283 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2284 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2285 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2286 else if (!strcmp(arg
, "--color-words"))
2287 options
->color_diff
= options
->color_diff_words
= 1;
2288 else if (!strcmp(arg
, "--no-renames"))
2289 options
->detect_rename
= 0;
2290 else if (!strcmp(arg
, "--exit-code"))
2291 options
->exit_with_status
= 1;
2292 else if (!strcmp(arg
, "--quiet"))
2299 static int parse_num(const char **cp_p
)
2301 unsigned long num
, scale
;
2303 const char *cp
= *cp_p
;
2310 if ( !dot
&& ch
== '.' ) {
2313 } else if ( ch
== '%' ) {
2314 scale
= dot
? scale
*100 : 100;
2315 cp
++; /* % is always at the end */
2317 } else if ( ch
>= '0' && ch
<= '9' ) {
2318 if ( scale
< 100000 ) {
2320 num
= (num
*10) + (ch
-'0');
2329 /* user says num divided by scale and we say internally that
2330 * is MAX_SCORE * num / scale.
2332 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2335 int diff_scoreopt_parse(const char *opt
)
2337 int opt1
, opt2
, cmd
;
2342 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2343 return -1; /* that is not a -M, -C nor -B option */
2345 opt1
= parse_num(&opt
);
2351 else if (*opt
!= '/')
2352 return -1; /* we expect -B80/99 or -B80 */
2355 opt2
= parse_num(&opt
);
2360 return opt1
| (opt2
<< 16);
2363 struct diff_queue_struct diff_queued_diff
;
2365 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2367 if (queue
->alloc
<= queue
->nr
) {
2368 queue
->alloc
= alloc_nr(queue
->alloc
);
2369 queue
->queue
= xrealloc(queue
->queue
,
2370 sizeof(dp
) * queue
->alloc
);
2372 queue
->queue
[queue
->nr
++] = dp
;
2375 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2376 struct diff_filespec
*one
,
2377 struct diff_filespec
*two
)
2379 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2387 void diff_free_filepair(struct diff_filepair
*p
)
2389 diff_free_filespec_data(p
->one
);
2390 diff_free_filespec_data(p
->two
);
2396 /* This is different from find_unique_abbrev() in that
2397 * it stuffs the result with dots for alignment.
2399 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2404 return sha1_to_hex(sha1
);
2406 abbrev
= find_unique_abbrev(sha1
, len
);
2408 return sha1_to_hex(sha1
);
2409 abblen
= strlen(abbrev
);
2411 static char hex
[41];
2412 if (len
< abblen
&& abblen
<= len
+ 2)
2413 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2415 sprintf(hex
, "%s...", abbrev
);
2418 return sha1_to_hex(sha1
);
2421 static void diff_flush_raw(struct diff_filepair
*p
,
2422 struct diff_options
*options
)
2426 int abbrev
= options
->abbrev
;
2427 const char *path_one
, *path_two
;
2428 int inter_name_termination
= '\t';
2429 int line_termination
= options
->line_termination
;
2431 if (!line_termination
)
2432 inter_name_termination
= 0;
2434 path_one
= p
->one
->path
;
2435 path_two
= p
->two
->path
;
2436 if (line_termination
) {
2437 path_one
= quote_one(path_one
);
2438 path_two
= quote_one(path_two
);
2442 sprintf(status
, "%c%03d", p
->status
,
2443 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2445 status
[0] = p
->status
;
2448 switch (p
->status
) {
2449 case DIFF_STATUS_COPIED
:
2450 case DIFF_STATUS_RENAMED
:
2453 case DIFF_STATUS_ADDED
:
2454 case DIFF_STATUS_DELETED
:
2461 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2462 printf(":%06o %06o %s ",
2463 p
->one
->mode
, p
->two
->mode
,
2464 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2466 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2468 printf("%s%c%s", status
, inter_name_termination
, path_one
);
2470 printf("%c%s", inter_name_termination
, path_two
);
2471 putchar(line_termination
);
2472 if (path_one
!= p
->one
->path
)
2473 free((void*)path_one
);
2474 if (path_two
!= p
->two
->path
)
2475 free((void*)path_two
);
2478 static void diff_flush_name(struct diff_filepair
*p
, struct diff_options
*opt
)
2480 char *path
= p
->two
->path
;
2482 if (opt
->line_termination
)
2483 path
= quote_one(p
->two
->path
);
2484 printf("%s%c", path
, opt
->line_termination
);
2485 if (p
->two
->path
!= path
)
2489 int diff_unmodified_pair(struct diff_filepair
*p
)
2491 /* This function is written stricter than necessary to support
2492 * the currently implemented transformers, but the idea is to
2493 * let transformers to produce diff_filepairs any way they want,
2494 * and filter and clean them up here before producing the output.
2496 struct diff_filespec
*one
, *two
;
2498 if (DIFF_PAIR_UNMERGED(p
))
2499 return 0; /* unmerged is interesting */
2504 /* deletion, addition, mode or type change
2505 * and rename are all interesting.
2507 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2508 DIFF_PAIR_MODE_CHANGED(p
) ||
2509 strcmp(one
->path
, two
->path
))
2512 /* both are valid and point at the same path. that is, we are
2513 * dealing with a change.
2515 if (one
->sha1_valid
&& two
->sha1_valid
&&
2516 !hashcmp(one
->sha1
, two
->sha1
))
2517 return 1; /* no change */
2518 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2519 return 1; /* both look at the same file on the filesystem. */
2523 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2525 if (diff_unmodified_pair(p
))
2528 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2529 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2530 return; /* no tree diffs in patch format */
2535 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2536 struct diffstat_t
*diffstat
)
2538 if (diff_unmodified_pair(p
))
2541 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2542 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2543 return; /* no tree diffs in patch format */
2545 run_diffstat(p
, o
, diffstat
);
2548 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2549 struct diff_options
*o
)
2551 if (diff_unmodified_pair(p
))
2554 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2555 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2556 return; /* no tree diffs in patch format */
2558 run_checkdiff(p
, o
);
2561 int diff_queue_is_empty(void)
2563 struct diff_queue_struct
*q
= &diff_queued_diff
;
2565 for (i
= 0; i
< q
->nr
; i
++)
2566 if (!diff_unmodified_pair(q
->queue
[i
]))
2572 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2574 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2577 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2579 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2580 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2582 s
->size
, s
->xfrm_flags
);
2585 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2587 diff_debug_filespec(p
->one
, i
, "one");
2588 diff_debug_filespec(p
->two
, i
, "two");
2589 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2590 p
->score
, p
->status
? p
->status
: '?',
2591 p
->source_stays
, p
->broken_pair
);
2594 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2598 fprintf(stderr
, "%s\n", msg
);
2599 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2600 for (i
= 0; i
< q
->nr
; i
++) {
2601 struct diff_filepair
*p
= q
->queue
[i
];
2602 diff_debug_filepair(p
, i
);
2607 static void diff_resolve_rename_copy(void)
2610 struct diff_filepair
*p
, *pp
;
2611 struct diff_queue_struct
*q
= &diff_queued_diff
;
2613 diff_debug_queue("resolve-rename-copy", q
);
2615 for (i
= 0; i
< q
->nr
; i
++) {
2617 p
->status
= 0; /* undecided */
2618 if (DIFF_PAIR_UNMERGED(p
))
2619 p
->status
= DIFF_STATUS_UNMERGED
;
2620 else if (!DIFF_FILE_VALID(p
->one
))
2621 p
->status
= DIFF_STATUS_ADDED
;
2622 else if (!DIFF_FILE_VALID(p
->two
))
2623 p
->status
= DIFF_STATUS_DELETED
;
2624 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2625 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2627 /* from this point on, we are dealing with a pair
2628 * whose both sides are valid and of the same type, i.e.
2629 * either in-place edit or rename/copy edit.
2631 else if (DIFF_PAIR_RENAME(p
)) {
2632 if (p
->source_stays
) {
2633 p
->status
= DIFF_STATUS_COPIED
;
2636 /* See if there is some other filepair that
2637 * copies from the same source as us. If so
2638 * we are a copy. Otherwise we are either a
2639 * copy if the path stays, or a rename if it
2640 * does not, but we already handled "stays" case.
2642 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2644 if (strcmp(pp
->one
->path
, p
->one
->path
))
2645 continue; /* not us */
2646 if (!DIFF_PAIR_RENAME(pp
))
2647 continue; /* not a rename/copy */
2648 /* pp is a rename/copy from the same source */
2649 p
->status
= DIFF_STATUS_COPIED
;
2653 p
->status
= DIFF_STATUS_RENAMED
;
2655 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2656 p
->one
->mode
!= p
->two
->mode
||
2657 is_null_sha1(p
->one
->sha1
))
2658 p
->status
= DIFF_STATUS_MODIFIED
;
2660 /* This is a "no-change" entry and should not
2661 * happen anymore, but prepare for broken callers.
2663 error("feeding unmodified %s to diffcore",
2665 p
->status
= DIFF_STATUS_UNKNOWN
;
2668 diff_debug_queue("resolve-rename-copy done", q
);
2671 static int check_pair_status(struct diff_filepair
*p
)
2673 switch (p
->status
) {
2674 case DIFF_STATUS_UNKNOWN
:
2677 die("internal error in diff-resolve-rename-copy");
2683 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2685 int fmt
= opt
->output_format
;
2687 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2688 diff_flush_checkdiff(p
, opt
);
2689 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2690 diff_flush_raw(p
, opt
);
2691 else if (fmt
& DIFF_FORMAT_NAME
)
2692 diff_flush_name(p
, opt
);
2695 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2697 char *name
= quote_one(fs
->path
);
2699 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, name
);
2701 printf(" %s %s\n", newdelete
, name
);
2706 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2708 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2710 char *name
= quote_one(p
->two
->path
);
2711 printf(" mode change %06o => %06o %s\n",
2712 p
->one
->mode
, p
->two
->mode
, name
);
2716 printf(" mode change %06o => %06o\n",
2717 p
->one
->mode
, p
->two
->mode
);
2721 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2723 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
2725 printf(" %s %s (%d%%)\n", renamecopy
, names
,
2726 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2728 show_mode_change(p
, 0);
2731 static void diff_summary(struct diff_filepair
*p
)
2734 case DIFF_STATUS_DELETED
:
2735 show_file_mode_name("delete", p
->one
);
2737 case DIFF_STATUS_ADDED
:
2738 show_file_mode_name("create", p
->two
);
2740 case DIFF_STATUS_COPIED
:
2741 show_rename_copy("copy", p
);
2743 case DIFF_STATUS_RENAMED
:
2744 show_rename_copy("rename", p
);
2748 char *name
= quote_one(p
->two
->path
);
2749 printf(" rewrite %s (%d%%)\n", name
,
2750 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2752 show_mode_change(p
, 0);
2753 } else show_mode_change(p
, 1);
2759 struct xdiff_emit_state xm
;
2764 static int remove_space(char *line
, int len
)
2770 for (i
= 0; i
< len
; i
++)
2771 if (!isspace((c
= line
[i
])))
2777 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2779 struct patch_id_t
*data
= priv
;
2782 /* Ignore line numbers when computing the SHA1 of the patch */
2783 if (!prefixcmp(line
, "@@ -"))
2786 new_len
= remove_space(line
, len
);
2788 SHA1_Update(data
->ctx
, line
, new_len
);
2789 data
->patchlen
+= new_len
;
2792 /* returns 0 upon success, and writes result into sha1 */
2793 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2795 struct diff_queue_struct
*q
= &diff_queued_diff
;
2798 struct patch_id_t data
;
2799 char buffer
[PATH_MAX
* 4 + 20];
2802 memset(&data
, 0, sizeof(struct patch_id_t
));
2804 data
.xm
.consume
= patch_id_consume
;
2806 for (i
= 0; i
< q
->nr
; i
++) {
2811 struct diff_filepair
*p
= q
->queue
[i
];
2815 return error("internal diff status error");
2816 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2818 if (diff_unmodified_pair(p
))
2820 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2821 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2823 if (DIFF_PAIR_UNMERGED(p
))
2826 diff_fill_sha1_info(p
->one
);
2827 diff_fill_sha1_info(p
->two
);
2828 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2829 fill_mmfile(&mf2
, p
->two
) < 0)
2830 return error("unable to read files to diff");
2832 /* Maybe hash p->two? into the patch id? */
2833 if (file_is_binary(p
->two
))
2836 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2837 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2838 if (p
->one
->mode
== 0)
2839 len1
= snprintf(buffer
, sizeof(buffer
),
2840 "diff--gita/%.*sb/%.*s"
2847 len2
, p
->two
->path
);
2848 else if (p
->two
->mode
== 0)
2849 len1
= snprintf(buffer
, sizeof(buffer
),
2850 "diff--gita/%.*sb/%.*s"
2851 "deletedfilemode%06o"
2857 len1
, p
->one
->path
);
2859 len1
= snprintf(buffer
, sizeof(buffer
),
2860 "diff--gita/%.*sb/%.*s"
2866 len2
, p
->two
->path
);
2867 SHA1_Update(&ctx
, buffer
, len1
);
2869 xpp
.flags
= XDF_NEED_MINIMAL
;
2871 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2872 ecb
.outf
= xdiff_outf
;
2874 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2877 SHA1_Final(sha1
, &ctx
);
2881 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2883 struct diff_queue_struct
*q
= &diff_queued_diff
;
2885 int result
= diff_get_patch_id(options
, sha1
);
2887 for (i
= 0; i
< q
->nr
; i
++)
2888 diff_free_filepair(q
->queue
[i
]);
2892 q
->nr
= q
->alloc
= 0;
2897 static int is_summary_empty(const struct diff_queue_struct
*q
)
2901 for (i
= 0; i
< q
->nr
; i
++) {
2902 const struct diff_filepair
*p
= q
->queue
[i
];
2904 switch (p
->status
) {
2905 case DIFF_STATUS_DELETED
:
2906 case DIFF_STATUS_ADDED
:
2907 case DIFF_STATUS_COPIED
:
2908 case DIFF_STATUS_RENAMED
:
2913 if (p
->one
->mode
&& p
->two
->mode
&&
2914 p
->one
->mode
!= p
->two
->mode
)
2922 void diff_flush(struct diff_options
*options
)
2924 struct diff_queue_struct
*q
= &diff_queued_diff
;
2925 int i
, output_format
= options
->output_format
;
2929 * Order: raw, stat, summary, patch
2930 * or: name/name-status/checkdiff (other bits clear)
2935 if (output_format
& (DIFF_FORMAT_RAW
|
2937 DIFF_FORMAT_NAME_STATUS
|
2938 DIFF_FORMAT_CHECKDIFF
)) {
2939 for (i
= 0; i
< q
->nr
; i
++) {
2940 struct diff_filepair
*p
= q
->queue
[i
];
2941 if (check_pair_status(p
))
2942 flush_one_pair(p
, options
);
2947 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
2948 struct diffstat_t diffstat
;
2950 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2951 diffstat
.xm
.consume
= diffstat_consume
;
2952 for (i
= 0; i
< q
->nr
; i
++) {
2953 struct diff_filepair
*p
= q
->queue
[i
];
2954 if (check_pair_status(p
))
2955 diff_flush_stat(p
, options
, &diffstat
);
2957 if (output_format
& DIFF_FORMAT_NUMSTAT
)
2958 show_numstat(&diffstat
, options
);
2959 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
2960 show_stats(&diffstat
, options
);
2961 else if (output_format
& DIFF_FORMAT_SHORTSTAT
)
2962 show_shortstats(&diffstat
);
2966 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2967 for (i
= 0; i
< q
->nr
; i
++)
2968 diff_summary(q
->queue
[i
]);
2972 if (output_format
& DIFF_FORMAT_PATCH
) {
2974 if (options
->stat_sep
) {
2975 /* attach patch instead of inline */
2976 fputs(options
->stat_sep
, stdout
);
2978 putchar(options
->line_termination
);
2982 for (i
= 0; i
< q
->nr
; i
++) {
2983 struct diff_filepair
*p
= q
->queue
[i
];
2984 if (check_pair_status(p
))
2985 diff_flush_patch(p
, options
);
2989 if (output_format
& DIFF_FORMAT_CALLBACK
)
2990 options
->format_callback(q
, options
, options
->format_callback_data
);
2992 for (i
= 0; i
< q
->nr
; i
++)
2993 diff_free_filepair(q
->queue
[i
]);
2997 q
->nr
= q
->alloc
= 0;
3000 static void diffcore_apply_filter(const char *filter
)
3003 struct diff_queue_struct
*q
= &diff_queued_diff
;
3004 struct diff_queue_struct outq
;
3006 outq
.nr
= outq
.alloc
= 0;
3011 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3013 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3014 struct diff_filepair
*p
= q
->queue
[i
];
3015 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3017 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3019 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3020 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3021 strchr(filter
, p
->status
)))
3027 /* otherwise we will clear the whole queue
3028 * by copying the empty outq at the end of this
3029 * function, but first clear the current entries
3032 for (i
= 0; i
< q
->nr
; i
++)
3033 diff_free_filepair(q
->queue
[i
]);
3036 /* Only the matching ones */
3037 for (i
= 0; i
< q
->nr
; i
++) {
3038 struct diff_filepair
*p
= q
->queue
[i
];
3040 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3042 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3044 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3045 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3046 strchr(filter
, p
->status
)))
3049 diff_free_filepair(p
);
3056 void diffcore_std(struct diff_options
*options
)
3060 if (options
->break_opt
!= -1)
3061 diffcore_break(options
->break_opt
);
3062 if (options
->detect_rename
)
3063 diffcore_rename(options
);
3064 if (options
->break_opt
!= -1)
3065 diffcore_merge_broken();
3066 if (options
->pickaxe
)
3067 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3068 if (options
->orderfile
)
3069 diffcore_order(options
->orderfile
);
3070 diff_resolve_rename_copy();
3071 diffcore_apply_filter(options
->filter
);
3073 options
->has_changes
= !!diff_queued_diff
.nr
;
3077 void diff_addremove(struct diff_options
*options
,
3078 int addremove
, unsigned mode
,
3079 const unsigned char *sha1
,
3080 const char *base
, const char *path
)
3082 char concatpath
[PATH_MAX
];
3083 struct diff_filespec
*one
, *two
;
3085 /* This may look odd, but it is a preparation for
3086 * feeding "there are unchanged files which should
3087 * not produce diffs, but when you are doing copy
3088 * detection you would need them, so here they are"
3089 * entries to the diff-core. They will be prefixed
3090 * with something like '=' or '*' (I haven't decided
3091 * which but should not make any difference).
3092 * Feeding the same new and old to diff_change()
3093 * also has the same effect.
3094 * Before the final output happens, they are pruned after
3095 * merged into rename/copy pairs as appropriate.
3097 if (options
->reverse_diff
)
3098 addremove
= (addremove
== '+' ? '-' :
3099 addremove
== '-' ? '+' : addremove
);
3101 if (!path
) path
= "";
3102 sprintf(concatpath
, "%s%s", base
, path
);
3103 one
= alloc_filespec(concatpath
);
3104 two
= alloc_filespec(concatpath
);
3106 if (addremove
!= '+')
3107 fill_filespec(one
, sha1
, mode
);
3108 if (addremove
!= '-')
3109 fill_filespec(two
, sha1
, mode
);
3111 diff_queue(&diff_queued_diff
, one
, two
);
3112 options
->has_changes
= 1;
3115 void diff_change(struct diff_options
*options
,
3116 unsigned old_mode
, unsigned new_mode
,
3117 const unsigned char *old_sha1
,
3118 const unsigned char *new_sha1
,
3119 const char *base
, const char *path
)
3121 char concatpath
[PATH_MAX
];
3122 struct diff_filespec
*one
, *two
;
3124 if (options
->reverse_diff
) {
3126 const unsigned char *tmp_c
;
3127 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3128 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3130 if (!path
) path
= "";
3131 sprintf(concatpath
, "%s%s", base
, path
);
3132 one
= alloc_filespec(concatpath
);
3133 two
= alloc_filespec(concatpath
);
3134 fill_filespec(one
, old_sha1
, old_mode
);
3135 fill_filespec(two
, new_sha1
, new_mode
);
3137 diff_queue(&diff_queued_diff
, one
, two
);
3138 options
->has_changes
= 1;
3141 void diff_unmerge(struct diff_options
*options
,
3143 unsigned mode
, const unsigned char *sha1
)
3145 struct diff_filespec
*one
, *two
;
3146 one
= alloc_filespec(path
);
3147 two
= alloc_filespec(path
);
3148 fill_filespec(one
, sha1
, mode
);
3149 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;