2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "spawn-pipe.h"
14 #ifdef NO_FAST_WORKING_DIRECTORY
15 #define FAST_WORKING_DIRECTORY 0
17 #define FAST_WORKING_DIRECTORY 1
20 static int diff_detect_rename_default
;
21 static int diff_rename_limit_default
= -1;
22 static int diff_use_color_default
;
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
;
61 * Currently there is only "diff.<drivername>.command" variable;
62 * because there are "diff.color.<slot>" variables, we are parsing
63 * this in a bit convoluted way to allow low level diff driver
66 static int parse_lldiff_command(const char *var
, const char *ep
, const char *value
)
70 struct ll_diff_driver
*drv
;
74 for (drv
= user_diff
; drv
; drv
= drv
->next
)
75 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
79 drv
= xcalloc(1, sizeof(struct ll_diff_driver
));
80 namebuf
= xmalloc(namelen
+ 1);
81 memcpy(namebuf
, name
, namelen
);
86 user_diff_tail
= &user_diff
;
87 *user_diff_tail
= drv
;
88 user_diff_tail
= &(drv
->next
);
92 return error("%s: lacks value", var
);
93 drv
->cmd
= strdup(value
);
98 * These are to give UI layer defaults.
99 * The core-level commands such as git-diff-files should
100 * never be affected by the setting of diff.renames
101 * the user happens to have in the configuration file.
103 int git_diff_ui_config(const char *var
, const char *value
)
105 if (!strcmp(var
, "diff.renamelimit")) {
106 diff_rename_limit_default
= git_config_int(var
, value
);
109 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
110 diff_use_color_default
= git_config_colorbool(var
, value
);
113 if (!strcmp(var
, "diff.renames")) {
115 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
116 else if (!strcasecmp(value
, "copies") ||
117 !strcasecmp(value
, "copy"))
118 diff_detect_rename_default
= DIFF_DETECT_COPY
;
119 else if (git_config_bool(var
,value
))
120 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
123 if (!prefixcmp(var
, "diff.")) {
124 const char *ep
= strrchr(var
, '.');
126 if (ep
!= var
+ 4 && !strcmp(ep
, ".command"))
127 return parse_lldiff_command(var
, ep
, value
);
129 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
130 int slot
= parse_diff_color_slot(var
, 11);
131 color_parse(value
, var
, diff_colors
[slot
]);
135 return git_default_config(var
, value
);
138 static char *quote_one(const char *str
)
145 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
148 xp
= xmalloc(needlen
+ 1);
149 quote_c_style(str
, xp
, NULL
, 0);
153 static char *quote_two(const char *one
, const char *two
)
155 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
156 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
159 if (need_one
+ need_two
) {
160 if (!need_one
) need_one
= strlen(one
);
161 if (!need_two
) need_one
= strlen(two
);
163 xp
= xmalloc(need_one
+ need_two
+ 3);
165 quote_c_style(one
, xp
+ 1, NULL
, 1);
166 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
167 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
170 need_one
= strlen(one
);
171 need_two
= strlen(two
);
172 xp
= xmalloc(need_one
+ need_two
+ 1);
174 strcpy(xp
+ need_one
, two
);
178 static const char *external_diff(void)
180 static const char *external_diff_cmd
= NULL
;
181 static int done_preparing
= 0;
184 return external_diff_cmd
;
185 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
187 return external_diff_cmd
;
190 #define TEMPFILE_PATH_LEN 50
192 static struct diff_tempfile
{
193 const char *name
; /* filename external diff should read from */
196 char tmp_path
[TEMPFILE_PATH_LEN
];
199 static int count_lines(const char *data
, int size
)
201 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
208 completely_empty
= 0;
212 completely_empty
= 0;
215 if (completely_empty
)
218 count
++; /* no trailing newline */
222 static void print_line_count(int count
)
232 printf("1,%d", count
);
237 static void copy_file(int prefix
, const char *data
, int size
,
238 const char *set
, const char *reset
)
240 int ch
, nl_just_seen
= 1;
249 fputs(reset
, stdout
);
255 printf("%s\n\\ No newline at end of file\n", reset
);
258 static void emit_rewrite_diff(const char *name_a
,
260 struct diff_filespec
*one
,
261 struct diff_filespec
*two
,
265 const char *name_a_tab
, *name_b_tab
;
266 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
267 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
268 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
269 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
270 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
272 name_a
+= (*name_a
== '/');
273 name_b
+= (*name_b
== '/');
274 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
275 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
277 diff_populate_filespec(one
, 0);
278 diff_populate_filespec(two
, 0);
279 lc_a
= count_lines(one
->data
, one
->size
);
280 lc_b
= count_lines(two
->data
, two
->size
);
281 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
282 metainfo
, name_a
, name_a_tab
, reset
,
283 metainfo
, name_b
, name_b_tab
, reset
, fraginfo
);
284 print_line_count(lc_a
);
286 print_line_count(lc_b
);
287 printf(" @@%s\n", reset
);
289 copy_file('-', one
->data
, one
->size
, old
, reset
);
291 copy_file('+', two
->data
, two
->size
, new, reset
);
294 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
296 if (!DIFF_FILE_VALID(one
)) {
297 mf
->ptr
= (char *)""; /* does not matter */
301 else if (diff_populate_filespec(one
, 0))
304 mf
->size
= one
->size
;
308 struct diff_words_buffer
{
311 long current
; /* output pointer */
312 int suppressed_newline
;
315 static void diff_words_append(char *line
, unsigned long len
,
316 struct diff_words_buffer
*buffer
)
318 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
319 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
320 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
324 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
325 buffer
->text
.size
+= len
;
328 struct diff_words_data
{
329 struct xdiff_emit_state xm
;
330 struct diff_words_buffer minus
, plus
;
333 static void print_word(struct diff_words_buffer
*buffer
, int len
, int color
,
334 int suppress_newline
)
342 ptr
= buffer
->text
.ptr
+ buffer
->current
;
343 buffer
->current
+= len
;
345 if (ptr
[len
- 1] == '\n') {
350 fputs(diff_get_color(1, color
), stdout
);
351 fwrite(ptr
, len
, 1, stdout
);
352 fputs(diff_get_color(1, DIFF_RESET
), stdout
);
355 if (suppress_newline
)
356 buffer
->suppressed_newline
= 1;
362 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
364 struct diff_words_data
*diff_words
= priv
;
366 if (diff_words
->minus
.suppressed_newline
) {
369 diff_words
->minus
.suppressed_newline
= 0;
375 print_word(&diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
378 print_word(&diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
381 print_word(&diff_words
->plus
, len
, DIFF_PLAIN
, 0);
382 diff_words
->minus
.current
+= len
;
387 /* this executes the word diff on the accumulated buffers */
388 static void diff_words_show(struct diff_words_data
*diff_words
)
393 mmfile_t minus
, plus
;
396 minus
.size
= diff_words
->minus
.text
.size
;
397 minus
.ptr
= xmalloc(minus
.size
);
398 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
399 for (i
= 0; i
< minus
.size
; i
++)
400 if (isspace(minus
.ptr
[i
]))
402 diff_words
->minus
.current
= 0;
404 plus
.size
= diff_words
->plus
.text
.size
;
405 plus
.ptr
= xmalloc(plus
.size
);
406 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
407 for (i
= 0; i
< plus
.size
; i
++)
408 if (isspace(plus
.ptr
[i
]))
410 diff_words
->plus
.current
= 0;
412 xpp
.flags
= XDF_NEED_MINIMAL
;
413 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
415 ecb
.outf
= xdiff_outf
;
416 ecb
.priv
= diff_words
;
417 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
418 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
422 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
424 if (diff_words
->minus
.suppressed_newline
) {
426 diff_words
->minus
.suppressed_newline
= 0;
430 struct emit_callback
{
431 struct xdiff_emit_state xm
;
432 int nparents
, color_diff
;
433 const char **label_path
;
434 struct diff_words_data
*diff_words
;
438 static void free_diff_words_data(struct emit_callback
*ecbdata
)
440 if (ecbdata
->diff_words
) {
442 if (ecbdata
->diff_words
->minus
.text
.size
||
443 ecbdata
->diff_words
->plus
.text
.size
)
444 diff_words_show(ecbdata
->diff_words
);
446 if (ecbdata
->diff_words
->minus
.text
.ptr
)
447 free (ecbdata
->diff_words
->minus
.text
.ptr
);
448 if (ecbdata
->diff_words
->plus
.text
.ptr
)
449 free (ecbdata
->diff_words
->plus
.text
.ptr
);
450 free(ecbdata
->diff_words
);
451 ecbdata
->diff_words
= NULL
;
455 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
458 return diff_colors
[ix
];
462 static void emit_line(const char *set
, const char *reset
, const char *line
, int len
)
464 if (len
> 0 && line
[len
-1] == '\n')
467 fwrite(line
, len
, 1, stdout
);
471 static void emit_line_with_ws(int nparents
,
472 const char *set
, const char *reset
, const char *ws
,
473 const char *line
, int len
)
476 int last_tab_in_indent
= -1;
477 int last_space_in_indent
= -1;
480 int need_highlight_leading_space
= 0;
481 /* The line is a newly added line. Does it have funny leading
482 * whitespaces? In indent, SP should never precede a TAB.
484 for (i
= col0
; i
< len
; i
++) {
485 if (line
[i
] == '\t') {
486 last_tab_in_indent
= i
;
487 if (0 <= last_space_in_indent
)
488 need_highlight_leading_space
= 1;
490 else if (line
[i
] == ' ')
491 last_space_in_indent
= i
;
496 fwrite(line
, col0
, 1, stdout
);
497 fputs(reset
, stdout
);
498 if (((i
== len
) || line
[i
] == '\n') && i
!= col0
) {
499 /* The whole line was indent */
500 emit_line(ws
, reset
, line
+ col0
, len
- col0
);
504 if (need_highlight_leading_space
) {
505 while (i
< last_tab_in_indent
) {
506 if (line
[i
] == ' ') {
509 fputs(reset
, stdout
);
517 if (line
[tail
] == '\n' && i
< tail
)
520 if (!isspace(line
[tail
]))
524 if ((i
< tail
&& line
[tail
+ 1] != '\n')) {
525 /* This has whitespace between tail+1..len */
527 fwrite(line
+ i
, tail
- i
+ 1, 1, stdout
);
528 fputs(reset
, stdout
);
529 emit_line(ws
, reset
, line
+ tail
+ 1, len
- tail
- 1);
532 emit_line(set
, reset
, line
+ i
, len
- i
);
535 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
537 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
538 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
541 emit_line(set
, reset
, line
, len
);
543 emit_line_with_ws(ecbdata
->nparents
, set
, reset
, ws
,
547 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
551 struct emit_callback
*ecbdata
= priv
;
552 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
553 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
555 *(ecbdata
->found_changesp
) = 1;
557 if (ecbdata
->label_path
[0]) {
558 const char *name_a_tab
, *name_b_tab
;
560 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
561 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
563 printf("%s--- %s%s%s\n",
564 set
, ecbdata
->label_path
[0], reset
, name_a_tab
);
565 printf("%s+++ %s%s%s\n",
566 set
, ecbdata
->label_path
[1], reset
, name_b_tab
);
567 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
570 /* This is not really necessary for now because
571 * this codepath only deals with two-way diffs.
573 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
575 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
576 ecbdata
->nparents
= i
- 1;
577 emit_line(diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
582 if (len
< ecbdata
->nparents
) {
584 emit_line(reset
, reset
, line
, len
);
589 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
590 /* fall back to normal diff */
591 free_diff_words_data(ecbdata
);
592 if (ecbdata
->diff_words
) {
593 if (line
[0] == '-') {
594 diff_words_append(line
, len
,
595 &ecbdata
->diff_words
->minus
);
597 } else if (line
[0] == '+') {
598 diff_words_append(line
, len
,
599 &ecbdata
->diff_words
->plus
);
602 if (ecbdata
->diff_words
->minus
.text
.size
||
603 ecbdata
->diff_words
->plus
.text
.size
)
604 diff_words_show(ecbdata
->diff_words
);
607 emit_line(set
, reset
, line
, len
);
610 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
612 color
= DIFF_FILE_OLD
;
613 else if (line
[i
] == '+')
614 color
= DIFF_FILE_NEW
;
617 if (color
!= DIFF_FILE_NEW
) {
618 emit_line(diff_get_color(ecbdata
->color_diff
, color
),
622 emit_add_line(reset
, ecbdata
, line
, len
);
625 static char *pprint_rename(const char *a
, const char *b
)
630 int pfx_length
, sfx_length
;
631 int len_a
= strlen(a
);
632 int len_b
= strlen(b
);
633 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
634 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
636 if (qlen_a
|| qlen_b
) {
637 if (qlen_a
) len_a
= qlen_a
;
638 if (qlen_b
) len_b
= qlen_b
;
639 name
= xmalloc( len_a
+ len_b
+ 5 );
641 quote_c_style(a
, name
, NULL
, 0);
643 memcpy(name
, a
, len_a
);
644 memcpy(name
+ len_a
, " => ", 4);
646 quote_c_style(b
, name
+ len_a
+ 4, NULL
, 0);
648 memcpy(name
+ len_a
+ 4, b
, len_b
+ 1);
652 /* Find common prefix */
654 while (*old
&& *new && *old
== *new) {
656 pfx_length
= old
- a
+ 1;
661 /* Find common suffix */
665 while (a
<= old
&& b
<= new && *old
== *new) {
667 sfx_length
= len_a
- (old
- a
);
673 * pfx{mid-a => mid-b}sfx
674 * {pfx-a => pfx-b}sfx
675 * pfx{sfx-a => sfx-b}
678 if (pfx_length
+ sfx_length
) {
679 int a_midlen
= len_a
- pfx_length
- sfx_length
;
680 int b_midlen
= len_b
- pfx_length
- sfx_length
;
681 if (a_midlen
< 0) a_midlen
= 0;
682 if (b_midlen
< 0) b_midlen
= 0;
684 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
685 sprintf(name
, "%.*s{%.*s => %.*s}%s",
687 a_midlen
, a
+ pfx_length
,
688 b_midlen
, b
+ pfx_length
,
689 a
+ len_a
- sfx_length
);
692 name
= xmalloc(len_a
+ len_b
+ 5);
693 sprintf(name
, "%s => %s", a
, b
);
699 struct xdiff_emit_state xm
;
703 struct diffstat_file
{
705 unsigned is_unmerged
:1;
706 unsigned is_binary
:1;
707 unsigned is_renamed
:1;
708 unsigned int added
, deleted
;
712 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
716 struct diffstat_file
*x
;
717 x
= xcalloc(sizeof (*x
), 1);
718 if (diffstat
->nr
== diffstat
->alloc
) {
719 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
720 diffstat
->files
= xrealloc(diffstat
->files
,
721 diffstat
->alloc
* sizeof(x
));
723 diffstat
->files
[diffstat
->nr
++] = x
;
725 x
->name
= pprint_rename(name_a
, name_b
);
729 x
->name
= xstrdup(name_a
);
733 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
735 struct diffstat_t
*diffstat
= priv
;
736 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
740 else if (line
[0] == '-')
744 const char mime_boundary_leader
[] = "------------";
746 static int scale_linear(int it
, int width
, int max_change
)
749 * make sure that at least one '-' is printed if there were deletions,
750 * and likewise for '+'.
754 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
757 static void show_name(const char *prefix
, const char *name
, int len
,
758 const char *reset
, const char *set
)
760 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
763 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
773 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
775 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
776 int max_change
= 0, max_len
= 0;
777 int total_files
= data
->nr
;
778 int width
, name_width
;
779 const char *reset
, *set
, *add_c
, *del_c
;
784 width
= options
->stat_width
? options
->stat_width
: 80;
785 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
787 /* Sanity: give at least 5 columns to the graph,
788 * but leave at least 10 columns for the name.
790 if (width
< name_width
+ 15) {
791 if (name_width
<= 25)
792 width
= name_width
+ 15;
794 name_width
= width
- 15;
797 /* Find the longest filename and max number of changes */
798 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
799 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
800 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
801 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
803 for (i
= 0; i
< data
->nr
; i
++) {
804 struct diffstat_file
*file
= data
->files
[i
];
805 int change
= file
->added
+ file
->deleted
;
807 if (!file
->is_renamed
) { /* renames are already quoted by pprint_rename */
808 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
810 char *qname
= xmalloc(len
+ 1);
811 quote_c_style(file
->name
, qname
, NULL
, 0);
817 len
= strlen(file
->name
);
821 if (file
->is_binary
|| file
->is_unmerged
)
823 if (max_change
< change
)
827 /* Compute the width of the graph part;
828 * 10 is for one blank at the beginning of the line plus
829 * " | count " between the name and the graph.
831 * From here on, name_width is the width of the name area,
832 * and width is the width of the graph area.
834 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
835 if (width
< (name_width
+ 10) + max_change
)
836 width
= width
- (name_width
+ 10);
840 for (i
= 0; i
< data
->nr
; i
++) {
841 const char *prefix
= "";
842 char *name
= data
->files
[i
]->name
;
843 int added
= data
->files
[i
]->added
;
844 int deleted
= data
->files
[i
]->deleted
;
848 * "scale" the filename
851 name_len
= strlen(name
);
852 if (name_width
< name_len
) {
856 name
+= name_len
- len
;
857 slash
= strchr(name
, '/');
862 if (data
->files
[i
]->is_binary
) {
863 show_name(prefix
, name
, len
, reset
, set
);
865 printf("%s%d%s", del_c
, deleted
, reset
);
867 printf("%s%d%s", add_c
, added
, reset
);
870 goto free_diffstat_file
;
872 else if (data
->files
[i
]->is_unmerged
) {
873 show_name(prefix
, name
, len
, reset
, set
);
874 printf(" Unmerged\n");
875 goto free_diffstat_file
;
877 else if (!data
->files
[i
]->is_renamed
&&
878 (added
+ deleted
== 0)) {
880 goto free_diffstat_file
;
884 * scale the add/delete
892 if (width
<= max_change
) {
893 add
= scale_linear(add
, width
, max_change
);
894 del
= scale_linear(del
, width
, max_change
);
897 show_name(prefix
, name
, len
, reset
, set
);
898 printf("%5d ", added
+ deleted
);
899 show_graph('+', add
, add_c
, reset
);
900 show_graph('-', del
, del_c
, reset
);
903 free(data
->files
[i
]->name
);
904 free(data
->files
[i
]);
907 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
908 set
, total_files
, adds
, dels
, reset
);
911 static void show_shortstats(struct diffstat_t
* data
)
913 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
918 for (i
= 0; i
< data
->nr
; i
++) {
919 if (!data
->files
[i
]->is_binary
&&
920 !data
->files
[i
]->is_unmerged
) {
921 int added
= data
->files
[i
]->added
;
922 int deleted
= data
->files
[i
]->deleted
;
923 if (!data
->files
[i
]->is_renamed
&&
924 (added
+ deleted
== 0)) {
931 free(data
->files
[i
]->name
);
932 free(data
->files
[i
]);
936 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
937 total_files
, adds
, dels
);
940 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
944 for (i
= 0; i
< data
->nr
; i
++) {
945 struct diffstat_file
*file
= data
->files
[i
];
950 printf("%d\t%d\t", file
->added
, file
->deleted
);
951 if (options
->line_termination
&& !file
->is_renamed
&&
952 quote_c_style(file
->name
, NULL
, NULL
, 0))
953 quote_c_style(file
->name
, NULL
, stdout
, 0);
955 fputs(file
->name
, stdout
);
956 putchar(options
->line_termination
);
961 struct xdiff_emit_state xm
;
962 const char *filename
;
963 int lineno
, color_diff
;
966 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
968 struct checkdiff_t
*data
= priv
;
969 const char *ws
= diff_get_color(data
->color_diff
, DIFF_WHITESPACE
);
970 const char *reset
= diff_get_color(data
->color_diff
, DIFF_RESET
);
971 const char *set
= diff_get_color(data
->color_diff
, DIFF_FILE_NEW
);
973 if (line
[0] == '+') {
974 int i
, spaces
= 0, space_before_tab
= 0, white_space_at_end
= 0;
976 /* check space before tab */
977 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
980 if (line
[i
- 1] == '\t' && spaces
)
981 space_before_tab
= 1;
983 /* check white space at line end */
984 if (line
[len
- 1] == '\n')
986 if (isspace(line
[len
- 1]))
987 white_space_at_end
= 1;
989 if (space_before_tab
|| white_space_at_end
) {
990 printf("%s:%d: %s", data
->filename
, data
->lineno
, ws
);
991 if (space_before_tab
) {
992 printf("space before tab");
993 if (white_space_at_end
)
996 if (white_space_at_end
)
997 printf("white space at end");
998 printf(":%s ", reset
);
999 emit_line_with_ws(1, set
, reset
, ws
, line
, len
);
1003 } else if (line
[0] == ' ')
1005 else if (line
[0] == '@') {
1006 char *plus
= strchr(line
, '+');
1008 data
->lineno
= strtol(plus
, NULL
, 10);
1010 die("invalid diff");
1014 static unsigned char *deflate_it(char *data
,
1016 unsigned long *result_size
)
1019 unsigned char *deflated
;
1022 memset(&stream
, 0, sizeof(stream
));
1023 deflateInit(&stream
, zlib_compression_level
);
1024 bound
= deflateBound(&stream
, size
);
1025 deflated
= xmalloc(bound
);
1026 stream
.next_out
= deflated
;
1027 stream
.avail_out
= bound
;
1029 stream
.next_in
= (unsigned char *)data
;
1030 stream
.avail_in
= size
;
1031 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1033 deflateEnd(&stream
);
1034 *result_size
= stream
.total_out
;
1038 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
1044 unsigned long orig_size
;
1045 unsigned long delta_size
;
1046 unsigned long deflate_size
;
1047 unsigned long data_size
;
1049 /* We could do deflated delta, or we could do just deflated two,
1050 * whichever is smaller.
1053 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1054 if (one
->size
&& two
->size
) {
1055 delta
= diff_delta(one
->ptr
, one
->size
,
1056 two
->ptr
, two
->size
,
1057 &delta_size
, deflate_size
);
1059 void *to_free
= delta
;
1060 orig_size
= delta_size
;
1061 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1066 if (delta
&& delta_size
< deflate_size
) {
1067 printf("delta %lu\n", orig_size
);
1070 data_size
= delta_size
;
1073 printf("literal %lu\n", two
->size
);
1076 data_size
= deflate_size
;
1079 /* emit data encoded in base85 */
1082 int bytes
= (52 < data_size
) ? 52 : data_size
;
1086 line
[0] = bytes
+ 'A' - 1;
1088 line
[0] = bytes
- 26 + 'a' - 1;
1089 encode_85(line
+ 1, cp
, bytes
);
1090 cp
= (char *) cp
+ bytes
;
1097 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
1099 printf("GIT binary patch\n");
1100 emit_binary_diff_body(one
, two
);
1101 emit_binary_diff_body(two
, one
);
1104 static void setup_diff_attr_check(struct git_attr_check
*check
)
1106 static struct git_attr
*attr_diff
;
1109 attr_diff
= git_attr("diff", 4);
1110 check
->attr
= attr_diff
;
1113 #define FIRST_FEW_BYTES 8000
1114 static int file_is_binary(struct diff_filespec
*one
)
1117 struct git_attr_check attr_diff_check
;
1119 setup_diff_attr_check(&attr_diff_check
);
1120 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1121 const char *value
= attr_diff_check
.value
;
1122 if (ATTR_TRUE(value
))
1124 else if (ATTR_FALSE(value
))
1129 if (!DIFF_FILE_VALID(one
))
1131 diff_populate_filespec(one
, 0);
1134 if (FIRST_FEW_BYTES
< sz
)
1135 sz
= FIRST_FEW_BYTES
;
1136 return !!memchr(one
->data
, 0, sz
);
1139 static void builtin_diff(const char *name_a
,
1141 struct diff_filespec
*one
,
1142 struct diff_filespec
*two
,
1143 const char *xfrm_msg
,
1144 struct diff_options
*o
,
1145 int complete_rewrite
)
1149 char *a_one
, *b_two
;
1150 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
1151 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
1153 a_one
= quote_two("a/", name_a
+ (*name_a
== '/'));
1154 b_two
= quote_two("b/", name_b
+ (*name_b
== '/'));
1155 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1156 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1157 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1158 if (lbl
[0][0] == '/') {
1160 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1161 if (xfrm_msg
&& xfrm_msg
[0])
1162 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1164 else if (lbl
[1][0] == '/') {
1165 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1166 if (xfrm_msg
&& xfrm_msg
[0])
1167 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1170 if (one
->mode
!= two
->mode
) {
1171 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
1172 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
1174 if (xfrm_msg
&& xfrm_msg
[0])
1175 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1177 * we do not run diff between different kind
1180 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1181 goto free_ab_and_return
;
1182 if (complete_rewrite
) {
1183 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1185 o
->found_changes
= 1;
1186 goto free_ab_and_return
;
1190 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1191 die("unable to read files to diff");
1193 if (!o
->text
&& (file_is_binary(one
) || file_is_binary(two
))) {
1194 /* Quite common confusing case */
1195 if (mf1
.size
== mf2
.size
&&
1196 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1197 goto free_ab_and_return
;
1199 emit_binary_diff(&mf1
, &mf2
);
1201 printf("Binary files %s and %s differ\n",
1203 o
->found_changes
= 1;
1206 /* Crazy xdl interfaces.. */
1207 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1211 struct emit_callback ecbdata
;
1213 memset(&ecbdata
, 0, sizeof(ecbdata
));
1214 ecbdata
.label_path
= lbl
;
1215 ecbdata
.color_diff
= o
->color_diff
;
1216 ecbdata
.found_changesp
= &o
->found_changes
;
1217 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1218 xecfg
.ctxlen
= o
->context
;
1219 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1222 else if (!prefixcmp(diffopts
, "--unified="))
1223 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1224 else if (!prefixcmp(diffopts
, "-u"))
1225 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1226 ecb
.outf
= xdiff_outf
;
1227 ecb
.priv
= &ecbdata
;
1228 ecbdata
.xm
.consume
= fn_out_consume
;
1229 if (o
->color_diff_words
)
1230 ecbdata
.diff_words
=
1231 xcalloc(1, sizeof(struct diff_words_data
));
1232 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1233 if (o
->color_diff_words
)
1234 free_diff_words_data(&ecbdata
);
1238 diff_free_filespec_data(one
);
1239 diff_free_filespec_data(two
);
1245 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1246 struct diff_filespec
*one
,
1247 struct diff_filespec
*two
,
1248 struct diffstat_t
*diffstat
,
1249 struct diff_options
*o
,
1250 int complete_rewrite
)
1253 struct diffstat_file
*data
;
1255 data
= diffstat_add(diffstat
, name_a
, name_b
);
1258 data
->is_unmerged
= 1;
1261 if (complete_rewrite
) {
1262 diff_populate_filespec(one
, 0);
1263 diff_populate_filespec(two
, 0);
1264 data
->deleted
= count_lines(one
->data
, one
->size
);
1265 data
->added
= count_lines(two
->data
, two
->size
);
1266 goto free_and_return
;
1268 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1269 die("unable to read files to diff");
1271 if (file_is_binary(one
) || file_is_binary(two
)) {
1272 data
->is_binary
= 1;
1273 data
->added
= mf2
.size
;
1274 data
->deleted
= mf1
.size
;
1276 /* Crazy xdl interfaces.. */
1281 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1284 ecb
.outf
= xdiff_outf
;
1285 ecb
.priv
= diffstat
;
1286 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1290 diff_free_filespec_data(one
);
1291 diff_free_filespec_data(two
);
1294 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1295 struct diff_filespec
*one
,
1296 struct diff_filespec
*two
, struct diff_options
*o
)
1299 struct checkdiff_t data
;
1304 memset(&data
, 0, sizeof(data
));
1305 data
.xm
.consume
= checkdiff_consume
;
1306 data
.filename
= name_b
? name_b
: name_a
;
1308 data
.color_diff
= o
->color_diff
;
1310 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1311 die("unable to read files to diff");
1313 if (file_is_binary(two
))
1314 goto free_and_return
;
1316 /* Crazy xdl interfaces.. */
1321 xpp
.flags
= XDF_NEED_MINIMAL
;
1324 ecb
.outf
= xdiff_outf
;
1326 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1329 diff_free_filespec_data(one
);
1330 diff_free_filespec_data(two
);
1333 struct diff_filespec
*alloc_filespec(const char *path
)
1335 int namelen
= strlen(path
);
1336 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1338 memset(spec
, 0, sizeof(*spec
));
1339 spec
->path
= (char *)(spec
+ 1);
1340 memcpy(spec
->path
, path
, namelen
+1);
1344 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1345 unsigned short mode
)
1348 spec
->mode
= canon_mode(mode
);
1349 hashcpy(spec
->sha1
, sha1
);
1350 spec
->sha1_valid
= !is_null_sha1(sha1
);
1355 * Given a name and sha1 pair, if the dircache tells us the file in
1356 * the work tree has that object contents, return true, so that
1357 * prepare_temp_file() does not have to inflate and extract.
1359 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1361 struct cache_entry
*ce
;
1365 /* We do not read the cache ourselves here, because the
1366 * benchmark with my previous version that always reads cache
1367 * shows that it makes things worse for diff-tree comparing
1368 * two linux-2.6 kernel trees in an already checked out work
1369 * tree. This is because most diff-tree comparisons deal with
1370 * only a small number of files, while reading the cache is
1371 * expensive for a large project, and its cost outweighs the
1372 * savings we get by not inflating the object to a temporary
1373 * file. Practically, this code only helps when we are used
1374 * by diff-cache --cached, which does read the cache before
1380 /* We want to avoid the working directory if our caller
1381 * doesn't need the data in a normal file, this system
1382 * is rather slow with its stat/open/mmap/close syscalls,
1383 * and the object is contained in a pack file. The pack
1384 * is probably already open and will be faster to obtain
1385 * the data through than the working directory. Loose
1386 * objects however would tend to be slower as they need
1387 * to be individually opened and inflated.
1389 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1393 pos
= cache_name_pos(name
, len
);
1396 ce
= active_cache
[pos
];
1397 if ((lstat(name
, &st
) < 0) ||
1398 !S_ISREG(st
.st_mode
) || /* careful! */
1399 ce_match_stat(ce
, &st
, 0) ||
1400 hashcmp(sha1
, ce
->sha1
))
1402 /* we return 1 only when we can stat, it is a regular file,
1403 * stat information matches, and sha1 recorded in the cache
1404 * matches. I.e. we know the file in the work tree really is
1405 * the same as the <name, sha1> pair.
1410 static int populate_from_stdin(struct diff_filespec
*s
)
1412 #define INCREMENT 1024
1420 buf
= xrealloc(buf
, size
+ INCREMENT
);
1421 got
= xread(0, buf
+ size
, INCREMENT
);
1425 return error("error while reading from stdin %s",
1429 s
->should_munmap
= 0;
1436 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1439 char *data
= xmalloc(100);
1440 len
= snprintf(data
, 100,
1441 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1453 * While doing rename detection and pickaxe operation, we may need to
1454 * grab the data for the blob (or file) for our own in-core comparison.
1455 * diff_filespec has data and size fields for this purpose.
1457 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1460 if (!DIFF_FILE_VALID(s
))
1461 die("internal error: asking to populate invalid file.");
1462 if (S_ISDIR(s
->mode
))
1468 if (size_only
&& 0 < s
->size
)
1471 if (S_ISDIRLNK(s
->mode
))
1472 return diff_populate_gitlink(s
, size_only
);
1474 if (!s
->sha1_valid
||
1475 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1481 if (!strcmp(s
->path
, "-"))
1482 return populate_from_stdin(s
);
1484 if (lstat(s
->path
, &st
) < 0) {
1485 if (errno
== ENOENT
) {
1489 s
->data
= (char *)"";
1494 s
->size
= xsize_t(st
.st_size
);
1499 if (S_ISLNK(st
.st_mode
)) {
1501 s
->data
= xmalloc(s
->size
);
1503 ret
= readlink(s
->path
, s
->data
, s
->size
);
1510 fd
= open(s
->path
, O_RDONLY
);
1513 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1515 s
->should_munmap
= 1;
1518 * Convert from working tree format to canonical git format
1521 buf
= convert_to_git(s
->path
, s
->data
, &size
);
1523 munmap(s
->data
, s
->size
);
1524 s
->should_munmap
= 0;
1531 enum object_type type
;
1533 type
= sha1_object_info(s
->sha1
, &s
->size
);
1535 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1542 void diff_free_filespec_data(struct diff_filespec
*s
)
1546 else if (s
->should_munmap
)
1547 munmap(s
->data
, s
->size
);
1549 if (s
->should_free
|| s
->should_munmap
) {
1550 s
->should_free
= s
->should_munmap
= 0;
1557 static void prep_temp_blob(struct diff_tempfile
*temp
,
1560 const unsigned char *sha1
,
1565 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1567 die("unable to create temp-file");
1568 if (write_in_full(fd
, blob
, size
) != size
)
1569 die("unable to write temp-file");
1571 temp
->name
= temp
->tmp_path
;
1572 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1574 sprintf(temp
->mode
, "%06o", mode
);
1577 static void prepare_temp_file(const char *name
,
1578 struct diff_tempfile
*temp
,
1579 struct diff_filespec
*one
)
1581 if (!DIFF_FILE_VALID(one
)) {
1583 /* A '-' entry produces this for file-2, and
1584 * a '+' entry produces this for file-1.
1586 temp
->name
= "/dev/null";
1587 strcpy(temp
->hex
, ".");
1588 strcpy(temp
->mode
, ".");
1592 if (!one
->sha1_valid
||
1593 reuse_worktree_file(name
, one
->sha1
, 1)) {
1595 if (lstat(name
, &st
) < 0) {
1596 if (errno
== ENOENT
)
1597 goto not_a_valid_file
;
1598 die("stat(%s): %s", name
, strerror(errno
));
1600 if (S_ISLNK(st
.st_mode
)) {
1602 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1603 size_t sz
= xsize_t(st
.st_size
);
1604 if (sizeof(buf
) <= st
.st_size
)
1605 die("symlink too long: %s", name
);
1606 ret
= readlink(name
, buf
, sz
);
1608 die("readlink(%s)", name
);
1609 prep_temp_blob(temp
, buf
, sz
,
1611 one
->sha1
: null_sha1
),
1613 one
->mode
: S_IFLNK
));
1616 /* we can borrow from the file in the work tree */
1618 if (!one
->sha1_valid
)
1619 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1621 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1622 /* Even though we may sometimes borrow the
1623 * contents from the work tree, we always want
1624 * one->mode. mode is trustworthy even when
1625 * !(one->sha1_valid), as long as
1626 * DIFF_FILE_VALID(one).
1628 sprintf(temp
->mode
, "%06o", one
->mode
);
1633 if (diff_populate_filespec(one
, 0))
1634 die("cannot read data blob for %s", one
->path
);
1635 prep_temp_blob(temp
, one
->data
, one
->size
,
1636 one
->sha1
, one
->mode
);
1640 static void remove_tempfile(void)
1644 for (i
= 0; i
< 2; i
++)
1645 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1646 unlink(diff_temp
[i
].name
);
1647 diff_temp
[i
].name
= NULL
;
1651 static void remove_tempfile_on_signal(int signo
)
1654 signal(SIGINT
, SIG_DFL
);
1658 static int spawn_prog(const char *pgm
, const char **arg
)
1664 pid
= spawnvpe_pipe(pgm
, arg
, environ
, NULL
, NULL
);
1666 die("unable to fork");
1668 while (waitpid(pid
, &status
, 0) < 0) {
1674 /* Earlier we did not check the exit status because
1675 * diff exits non-zero if files are different, and
1676 * we are not interested in knowing that. It was a
1677 * mistake which made it harder to quit a diff-*
1678 * session that uses the git-apply-patch-script as
1679 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1680 * should also exit non-zero only when it wants to
1681 * abort the entire diff-* session.
1683 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1688 /* An external diff command takes:
1690 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1691 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1694 static void run_external_diff(const char *pgm
,
1697 struct diff_filespec
*one
,
1698 struct diff_filespec
*two
,
1699 const char *xfrm_msg
,
1700 int complete_rewrite
)
1702 const char *spawn_arg
[10];
1703 struct diff_tempfile
*temp
= diff_temp
;
1705 static int atexit_asked
= 0;
1706 const char *othername
;
1707 const char **arg
= &spawn_arg
[1];
1709 othername
= (other
? other
: name
);
1711 prepare_temp_file(name
, &temp
[0], one
);
1712 prepare_temp_file(othername
, &temp
[1], two
);
1713 if (! atexit_asked
&&
1714 (temp
[0].name
== temp
[0].tmp_path
||
1715 temp
[1].name
== temp
[1].tmp_path
)) {
1717 atexit(remove_tempfile
);
1719 signal(SIGINT
, remove_tempfile_on_signal
);
1724 *arg
++ = temp
[0].name
;
1725 *arg
++ = temp
[0].hex
;
1726 *arg
++ = temp
[0].mode
;
1727 *arg
++ = temp
[1].name
;
1728 *arg
++ = temp
[1].hex
;
1729 *arg
++ = temp
[1].mode
;
1738 retval
= spawn_prog(pgm
, spawn_arg
);
1741 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1746 static const char *external_diff_attr(const char *name
)
1748 struct git_attr_check attr_diff_check
;
1750 setup_diff_attr_check(&attr_diff_check
);
1751 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
1752 const char *value
= attr_diff_check
.value
;
1753 if (!ATTR_TRUE(value
) &&
1754 !ATTR_FALSE(value
) &&
1755 !ATTR_UNSET(value
)) {
1756 struct ll_diff_driver
*drv
;
1758 if (!user_diff_tail
) {
1759 user_diff_tail
= &user_diff
;
1760 git_config(git_diff_ui_config
);
1762 for (drv
= user_diff
; drv
; drv
= drv
->next
)
1763 if (!strcmp(drv
->name
, value
))
1770 static void run_diff_cmd(const char *pgm
,
1773 struct diff_filespec
*one
,
1774 struct diff_filespec
*two
,
1775 const char *xfrm_msg
,
1776 struct diff_options
*o
,
1777 int complete_rewrite
)
1779 if (!o
->allow_external
)
1782 const char *cmd
= external_diff_attr(name
);
1788 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1793 builtin_diff(name
, other
? other
: name
,
1794 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1796 printf("* Unmerged path %s\n", name
);
1799 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1801 if (DIFF_FILE_VALID(one
)) {
1802 if (!one
->sha1_valid
) {
1804 if (!strcmp(one
->path
, "-")) {
1805 hashcpy(one
->sha1
, null_sha1
);
1808 if (lstat(one
->path
, &st
) < 0)
1809 die("stat %s", one
->path
);
1810 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1811 die("cannot hash %s\n", one
->path
);
1818 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1820 const char *pgm
= external_diff();
1821 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1822 struct diff_filespec
*one
;
1823 struct diff_filespec
*two
;
1826 char *name_munged
, *other_munged
;
1827 int complete_rewrite
= 0;
1830 if (DIFF_PAIR_UNMERGED(p
)) {
1832 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1836 name
= p
->one
->path
;
1837 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1838 name_munged
= quote_one(name
);
1839 other_munged
= quote_one(other
);
1840 one
= p
->one
; two
= p
->two
;
1842 diff_fill_sha1_info(one
);
1843 diff_fill_sha1_info(two
);
1846 switch (p
->status
) {
1847 case DIFF_STATUS_COPIED
:
1848 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1849 "similarity index %d%%\n"
1852 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1853 name_munged
, other_munged
);
1855 case DIFF_STATUS_RENAMED
:
1856 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1857 "similarity index %d%%\n"
1860 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1861 name_munged
, other_munged
);
1863 case DIFF_STATUS_MODIFIED
:
1865 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1866 "dissimilarity index %d%%\n",
1867 (int)(0.5 + p
->score
*
1869 complete_rewrite
= 1;
1878 if (hashcmp(one
->sha1
, two
->sha1
)) {
1879 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1883 if ((!fill_mmfile(&mf
, one
) && file_is_binary(one
)) ||
1884 (!fill_mmfile(&mf
, two
) && file_is_binary(two
)))
1887 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1889 abbrev
, sha1_to_hex(one
->sha1
),
1890 abbrev
, sha1_to_hex(two
->sha1
));
1891 if (one
->mode
== two
->mode
)
1892 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1893 " %06o", one
->mode
);
1894 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1899 xfrm_msg
= len
? msg
: NULL
;
1902 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1903 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1904 /* a filepair that changes between file and symlink
1905 * needs to be split into deletion and creation.
1907 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1908 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1910 null
= alloc_filespec(one
->path
);
1911 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1915 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1922 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1923 struct diffstat_t
*diffstat
)
1927 int complete_rewrite
= 0;
1929 if (DIFF_PAIR_UNMERGED(p
)) {
1931 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1935 name
= p
->one
->path
;
1936 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1938 diff_fill_sha1_info(p
->one
);
1939 diff_fill_sha1_info(p
->two
);
1941 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1942 complete_rewrite
= 1;
1943 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1946 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1951 if (DIFF_PAIR_UNMERGED(p
)) {
1956 name
= p
->one
->path
;
1957 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1959 diff_fill_sha1_info(p
->one
);
1960 diff_fill_sha1_info(p
->two
);
1962 builtin_checkdiff(name
, other
, p
->one
, p
->two
, o
);
1965 void diff_setup(struct diff_options
*options
)
1967 memset(options
, 0, sizeof(*options
));
1968 options
->line_termination
= '\n';
1969 options
->break_opt
= -1;
1970 options
->rename_limit
= -1;
1971 options
->context
= 3;
1972 options
->msg_sep
= "";
1974 options
->change
= diff_change
;
1975 options
->add_remove
= diff_addremove
;
1976 options
->color_diff
= diff_use_color_default
;
1977 options
->detect_rename
= diff_detect_rename_default
;
1980 int diff_setup_done(struct diff_options
*options
)
1984 if (options
->output_format
& DIFF_FORMAT_NAME
)
1986 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
1988 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
1990 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
1993 die("--name-only, --name-status, --check and -s are mutually exclusive");
1995 if (options
->find_copies_harder
)
1996 options
->detect_rename
= DIFF_DETECT_COPY
;
1998 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1999 DIFF_FORMAT_NAME_STATUS
|
2000 DIFF_FORMAT_CHECKDIFF
|
2001 DIFF_FORMAT_NO_OUTPUT
))
2002 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2003 DIFF_FORMAT_NUMSTAT
|
2004 DIFF_FORMAT_DIFFSTAT
|
2005 DIFF_FORMAT_SHORTSTAT
|
2006 DIFF_FORMAT_SUMMARY
|
2010 * These cases always need recursive; we do not drop caller-supplied
2011 * recursive bits for other formats here.
2013 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2014 DIFF_FORMAT_NUMSTAT
|
2015 DIFF_FORMAT_DIFFSTAT
|
2016 DIFF_FORMAT_SHORTSTAT
|
2017 DIFF_FORMAT_SUMMARY
|
2018 DIFF_FORMAT_CHECKDIFF
))
2019 options
->recursive
= 1;
2021 * Also pickaxe would not work very well if you do not say recursive
2023 if (options
->pickaxe
)
2024 options
->recursive
= 1;
2026 if (options
->detect_rename
&& options
->rename_limit
< 0)
2027 options
->rename_limit
= diff_rename_limit_default
;
2028 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2030 /* read-cache does not die even when it fails
2031 * so it is safe for us to do this here. Also
2032 * it does not smudge active_cache or active_nr
2033 * when it fails, so we do not have to worry about
2034 * cleaning it up ourselves either.
2038 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2039 options
->abbrev
= 40; /* full */
2042 * It does not make sense to show the first hit we happened
2043 * to have found. It does not make sense not to return with
2044 * exit code in such a case either.
2046 if (options
->quiet
) {
2047 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2048 options
->exit_with_status
= 1;
2052 * If we postprocess in diffcore, we cannot simply return
2053 * upon the first hit. We need to run diff as usual.
2055 if (options
->pickaxe
|| options
->filter
)
2061 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2071 if (c
== arg_short
) {
2075 if (val
&& isdigit(c
)) {
2077 int n
= strtoul(arg
, &end
, 10);
2088 eq
= strchr(arg
, '=');
2093 if (!len
|| strncmp(arg
, arg_long
, len
))
2098 if (!isdigit(*++eq
))
2100 n
= strtoul(eq
, &end
, 10);
2108 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2110 const char *arg
= av
[0];
2111 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2112 options
->output_format
|= DIFF_FORMAT_PATCH
;
2113 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2114 options
->output_format
|= DIFF_FORMAT_PATCH
;
2115 else if (!strcmp(arg
, "--raw"))
2116 options
->output_format
|= DIFF_FORMAT_RAW
;
2117 else if (!strcmp(arg
, "--patch-with-raw")) {
2118 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2120 else if (!strcmp(arg
, "--numstat")) {
2121 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2123 else if (!strcmp(arg
, "--shortstat")) {
2124 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2126 else if (!prefixcmp(arg
, "--stat")) {
2128 int width
= options
->stat_width
;
2129 int name_width
= options
->stat_name_width
;
2135 if (!prefixcmp(arg
, "-width="))
2136 width
= strtoul(arg
+ 7, &end
, 10);
2137 else if (!prefixcmp(arg
, "-name-width="))
2138 name_width
= strtoul(arg
+ 12, &end
, 10);
2141 width
= strtoul(arg
+1, &end
, 10);
2143 name_width
= strtoul(end
+1, &end
, 10);
2146 /* Important! This checks all the error cases! */
2149 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2150 options
->stat_name_width
= name_width
;
2151 options
->stat_width
= width
;
2153 else if (!strcmp(arg
, "--check"))
2154 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2155 else if (!strcmp(arg
, "--summary"))
2156 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2157 else if (!strcmp(arg
, "--patch-with-stat")) {
2158 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2160 else if (!strcmp(arg
, "-z"))
2161 options
->line_termination
= 0;
2162 else if (!prefixcmp(arg
, "-l"))
2163 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2164 else if (!strcmp(arg
, "--full-index"))
2165 options
->full_index
= 1;
2166 else if (!strcmp(arg
, "--binary")) {
2167 options
->output_format
|= DIFF_FORMAT_PATCH
;
2168 options
->binary
= 1;
2170 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
2173 else if (!strcmp(arg
, "--name-only"))
2174 options
->output_format
|= DIFF_FORMAT_NAME
;
2175 else if (!strcmp(arg
, "--name-status"))
2176 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2177 else if (!strcmp(arg
, "-R"))
2178 options
->reverse_diff
= 1;
2179 else if (!prefixcmp(arg
, "-S"))
2180 options
->pickaxe
= arg
+ 2;
2181 else if (!strcmp(arg
, "-s")) {
2182 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2184 else if (!prefixcmp(arg
, "-O"))
2185 options
->orderfile
= arg
+ 2;
2186 else if (!prefixcmp(arg
, "--diff-filter="))
2187 options
->filter
= arg
+ 14;
2188 else if (!strcmp(arg
, "--pickaxe-all"))
2189 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2190 else if (!strcmp(arg
, "--pickaxe-regex"))
2191 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2192 else if (!prefixcmp(arg
, "-B")) {
2193 if ((options
->break_opt
=
2194 diff_scoreopt_parse(arg
)) == -1)
2197 else if (!prefixcmp(arg
, "-M")) {
2198 if ((options
->rename_score
=
2199 diff_scoreopt_parse(arg
)) == -1)
2201 options
->detect_rename
= DIFF_DETECT_RENAME
;
2203 else if (!prefixcmp(arg
, "-C")) {
2204 if ((options
->rename_score
=
2205 diff_scoreopt_parse(arg
)) == -1)
2207 options
->detect_rename
= DIFF_DETECT_COPY
;
2209 else if (!strcmp(arg
, "--find-copies-harder"))
2210 options
->find_copies_harder
= 1;
2211 else if (!strcmp(arg
, "--abbrev"))
2212 options
->abbrev
= DEFAULT_ABBREV
;
2213 else if (!prefixcmp(arg
, "--abbrev=")) {
2214 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2215 if (options
->abbrev
< MINIMUM_ABBREV
)
2216 options
->abbrev
= MINIMUM_ABBREV
;
2217 else if (40 < options
->abbrev
)
2218 options
->abbrev
= 40;
2220 else if (!strcmp(arg
, "--color"))
2221 options
->color_diff
= 1;
2222 else if (!strcmp(arg
, "--no-color"))
2223 options
->color_diff
= 0;
2224 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2225 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2226 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2227 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2228 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2229 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2230 else if (!strcmp(arg
, "--color-words"))
2231 options
->color_diff
= options
->color_diff_words
= 1;
2232 else if (!strcmp(arg
, "--no-renames"))
2233 options
->detect_rename
= 0;
2234 else if (!strcmp(arg
, "--exit-code"))
2235 options
->exit_with_status
= 1;
2236 else if (!strcmp(arg
, "--quiet"))
2243 static int parse_num(const char **cp_p
)
2245 unsigned long num
, scale
;
2247 const char *cp
= *cp_p
;
2254 if ( !dot
&& ch
== '.' ) {
2257 } else if ( ch
== '%' ) {
2258 scale
= dot
? scale
*100 : 100;
2259 cp
++; /* % is always at the end */
2261 } else if ( ch
>= '0' && ch
<= '9' ) {
2262 if ( scale
< 100000 ) {
2264 num
= (num
*10) + (ch
-'0');
2273 /* user says num divided by scale and we say internally that
2274 * is MAX_SCORE * num / scale.
2276 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2279 int diff_scoreopt_parse(const char *opt
)
2281 int opt1
, opt2
, cmd
;
2286 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2287 return -1; /* that is not a -M, -C nor -B option */
2289 opt1
= parse_num(&opt
);
2295 else if (*opt
!= '/')
2296 return -1; /* we expect -B80/99 or -B80 */
2299 opt2
= parse_num(&opt
);
2304 return opt1
| (opt2
<< 16);
2307 struct diff_queue_struct diff_queued_diff
;
2309 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2311 if (queue
->alloc
<= queue
->nr
) {
2312 queue
->alloc
= alloc_nr(queue
->alloc
);
2313 queue
->queue
= xrealloc(queue
->queue
,
2314 sizeof(dp
) * queue
->alloc
);
2316 queue
->queue
[queue
->nr
++] = dp
;
2319 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2320 struct diff_filespec
*one
,
2321 struct diff_filespec
*two
)
2323 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2331 void diff_free_filepair(struct diff_filepair
*p
)
2333 diff_free_filespec_data(p
->one
);
2334 diff_free_filespec_data(p
->two
);
2340 /* This is different from find_unique_abbrev() in that
2341 * it stuffs the result with dots for alignment.
2343 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2348 return sha1_to_hex(sha1
);
2350 abbrev
= find_unique_abbrev(sha1
, len
);
2352 return sha1_to_hex(sha1
);
2353 abblen
= strlen(abbrev
);
2355 static char hex
[41];
2356 if (len
< abblen
&& abblen
<= len
+ 2)
2357 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2359 sprintf(hex
, "%s...", abbrev
);
2362 return sha1_to_hex(sha1
);
2365 static void diff_flush_raw(struct diff_filepair
*p
,
2366 struct diff_options
*options
)
2370 int abbrev
= options
->abbrev
;
2371 const char *path_one
, *path_two
;
2372 int inter_name_termination
= '\t';
2373 int line_termination
= options
->line_termination
;
2375 if (!line_termination
)
2376 inter_name_termination
= 0;
2378 path_one
= p
->one
->path
;
2379 path_two
= p
->two
->path
;
2380 if (line_termination
) {
2381 path_one
= quote_one(path_one
);
2382 path_two
= quote_one(path_two
);
2386 sprintf(status
, "%c%03d", p
->status
,
2387 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2389 status
[0] = p
->status
;
2392 switch (p
->status
) {
2393 case DIFF_STATUS_COPIED
:
2394 case DIFF_STATUS_RENAMED
:
2397 case DIFF_STATUS_ADDED
:
2398 case DIFF_STATUS_DELETED
:
2405 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2406 printf(":%06o %06o %s ",
2407 p
->one
->mode
, p
->two
->mode
,
2408 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2410 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2412 printf("%s%c%s", status
, inter_name_termination
, path_one
);
2414 printf("%c%s", inter_name_termination
, path_two
);
2415 putchar(line_termination
);
2416 if (path_one
!= p
->one
->path
)
2417 free((void*)path_one
);
2418 if (path_two
!= p
->two
->path
)
2419 free((void*)path_two
);
2422 static void diff_flush_name(struct diff_filepair
*p
, struct diff_options
*opt
)
2424 char *path
= p
->two
->path
;
2426 if (opt
->line_termination
)
2427 path
= quote_one(p
->two
->path
);
2428 printf("%s%c", path
, opt
->line_termination
);
2429 if (p
->two
->path
!= path
)
2433 int diff_unmodified_pair(struct diff_filepair
*p
)
2435 /* This function is written stricter than necessary to support
2436 * the currently implemented transformers, but the idea is to
2437 * let transformers to produce diff_filepairs any way they want,
2438 * and filter and clean them up here before producing the output.
2440 struct diff_filespec
*one
, *two
;
2442 if (DIFF_PAIR_UNMERGED(p
))
2443 return 0; /* unmerged is interesting */
2448 /* deletion, addition, mode or type change
2449 * and rename are all interesting.
2451 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2452 DIFF_PAIR_MODE_CHANGED(p
) ||
2453 strcmp(one
->path
, two
->path
))
2456 /* both are valid and point at the same path. that is, we are
2457 * dealing with a change.
2459 if (one
->sha1_valid
&& two
->sha1_valid
&&
2460 !hashcmp(one
->sha1
, two
->sha1
))
2461 return 1; /* no change */
2462 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2463 return 1; /* both look at the same file on the filesystem. */
2467 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2469 if (diff_unmodified_pair(p
))
2472 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2473 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2474 return; /* no tree diffs in patch format */
2479 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2480 struct diffstat_t
*diffstat
)
2482 if (diff_unmodified_pair(p
))
2485 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2486 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2487 return; /* no tree diffs in patch format */
2489 run_diffstat(p
, o
, diffstat
);
2492 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2493 struct diff_options
*o
)
2495 if (diff_unmodified_pair(p
))
2498 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2499 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2500 return; /* no tree diffs in patch format */
2502 run_checkdiff(p
, o
);
2505 int diff_queue_is_empty(void)
2507 struct diff_queue_struct
*q
= &diff_queued_diff
;
2509 for (i
= 0; i
< q
->nr
; i
++)
2510 if (!diff_unmodified_pair(q
->queue
[i
]))
2516 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2518 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2521 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2523 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2524 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2526 s
->size
, s
->xfrm_flags
);
2529 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2531 diff_debug_filespec(p
->one
, i
, "one");
2532 diff_debug_filespec(p
->two
, i
, "two");
2533 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2534 p
->score
, p
->status
? p
->status
: '?',
2535 p
->source_stays
, p
->broken_pair
);
2538 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2542 fprintf(stderr
, "%s\n", msg
);
2543 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2544 for (i
= 0; i
< q
->nr
; i
++) {
2545 struct diff_filepair
*p
= q
->queue
[i
];
2546 diff_debug_filepair(p
, i
);
2551 static void diff_resolve_rename_copy(void)
2554 struct diff_filepair
*p
, *pp
;
2555 struct diff_queue_struct
*q
= &diff_queued_diff
;
2557 diff_debug_queue("resolve-rename-copy", q
);
2559 for (i
= 0; i
< q
->nr
; i
++) {
2561 p
->status
= 0; /* undecided */
2562 if (DIFF_PAIR_UNMERGED(p
))
2563 p
->status
= DIFF_STATUS_UNMERGED
;
2564 else if (!DIFF_FILE_VALID(p
->one
))
2565 p
->status
= DIFF_STATUS_ADDED
;
2566 else if (!DIFF_FILE_VALID(p
->two
))
2567 p
->status
= DIFF_STATUS_DELETED
;
2568 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2569 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2571 /* from this point on, we are dealing with a pair
2572 * whose both sides are valid and of the same type, i.e.
2573 * either in-place edit or rename/copy edit.
2575 else if (DIFF_PAIR_RENAME(p
)) {
2576 if (p
->source_stays
) {
2577 p
->status
= DIFF_STATUS_COPIED
;
2580 /* See if there is some other filepair that
2581 * copies from the same source as us. If so
2582 * we are a copy. Otherwise we are either a
2583 * copy if the path stays, or a rename if it
2584 * does not, but we already handled "stays" case.
2586 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2588 if (strcmp(pp
->one
->path
, p
->one
->path
))
2589 continue; /* not us */
2590 if (!DIFF_PAIR_RENAME(pp
))
2591 continue; /* not a rename/copy */
2592 /* pp is a rename/copy from the same source */
2593 p
->status
= DIFF_STATUS_COPIED
;
2597 p
->status
= DIFF_STATUS_RENAMED
;
2599 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2600 p
->one
->mode
!= p
->two
->mode
||
2601 is_null_sha1(p
->one
->sha1
))
2602 p
->status
= DIFF_STATUS_MODIFIED
;
2604 /* This is a "no-change" entry and should not
2605 * happen anymore, but prepare for broken callers.
2607 error("feeding unmodified %s to diffcore",
2609 p
->status
= DIFF_STATUS_UNKNOWN
;
2612 diff_debug_queue("resolve-rename-copy done", q
);
2615 static int check_pair_status(struct diff_filepair
*p
)
2617 switch (p
->status
) {
2618 case DIFF_STATUS_UNKNOWN
:
2621 die("internal error in diff-resolve-rename-copy");
2627 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2629 int fmt
= opt
->output_format
;
2631 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2632 diff_flush_checkdiff(p
, opt
);
2633 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2634 diff_flush_raw(p
, opt
);
2635 else if (fmt
& DIFF_FORMAT_NAME
)
2636 diff_flush_name(p
, opt
);
2639 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2641 char *name
= quote_one(fs
->path
);
2643 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, name
);
2645 printf(" %s %s\n", newdelete
, name
);
2650 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2652 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2654 char *name
= quote_one(p
->two
->path
);
2655 printf(" mode change %06o => %06o %s\n",
2656 p
->one
->mode
, p
->two
->mode
, name
);
2660 printf(" mode change %06o => %06o\n",
2661 p
->one
->mode
, p
->two
->mode
);
2665 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2667 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
2669 printf(" %s %s (%d%%)\n", renamecopy
, names
,
2670 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2672 show_mode_change(p
, 0);
2675 static void diff_summary(struct diff_filepair
*p
)
2678 case DIFF_STATUS_DELETED
:
2679 show_file_mode_name("delete", p
->one
);
2681 case DIFF_STATUS_ADDED
:
2682 show_file_mode_name("create", p
->two
);
2684 case DIFF_STATUS_COPIED
:
2685 show_rename_copy("copy", p
);
2687 case DIFF_STATUS_RENAMED
:
2688 show_rename_copy("rename", p
);
2692 char *name
= quote_one(p
->two
->path
);
2693 printf(" rewrite %s (%d%%)\n", name
,
2694 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2696 show_mode_change(p
, 0);
2697 } else show_mode_change(p
, 1);
2703 struct xdiff_emit_state xm
;
2708 static int remove_space(char *line
, int len
)
2714 for (i
= 0; i
< len
; i
++)
2715 if (!isspace((c
= line
[i
])))
2721 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2723 struct patch_id_t
*data
= priv
;
2726 /* Ignore line numbers when computing the SHA1 of the patch */
2727 if (!prefixcmp(line
, "@@ -"))
2730 new_len
= remove_space(line
, len
);
2732 SHA1_Update(data
->ctx
, line
, new_len
);
2733 data
->patchlen
+= new_len
;
2736 /* returns 0 upon success, and writes result into sha1 */
2737 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2739 struct diff_queue_struct
*q
= &diff_queued_diff
;
2742 struct patch_id_t data
;
2743 char buffer
[PATH_MAX
* 4 + 20];
2746 memset(&data
, 0, sizeof(struct patch_id_t
));
2748 data
.xm
.consume
= patch_id_consume
;
2750 for (i
= 0; i
< q
->nr
; i
++) {
2755 struct diff_filepair
*p
= q
->queue
[i
];
2759 return error("internal diff status error");
2760 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2762 if (diff_unmodified_pair(p
))
2764 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2765 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2767 if (DIFF_PAIR_UNMERGED(p
))
2770 diff_fill_sha1_info(p
->one
);
2771 diff_fill_sha1_info(p
->two
);
2772 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2773 fill_mmfile(&mf2
, p
->two
) < 0)
2774 return error("unable to read files to diff");
2776 /* Maybe hash p->two? into the patch id? */
2777 if (file_is_binary(p
->two
))
2780 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2781 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2782 if (p
->one
->mode
== 0)
2783 len1
= snprintf(buffer
, sizeof(buffer
),
2784 "diff--gita/%.*sb/%.*s"
2791 len2
, p
->two
->path
);
2792 else if (p
->two
->mode
== 0)
2793 len1
= snprintf(buffer
, sizeof(buffer
),
2794 "diff--gita/%.*sb/%.*s"
2795 "deletedfilemode%06o"
2801 len1
, p
->one
->path
);
2803 len1
= snprintf(buffer
, sizeof(buffer
),
2804 "diff--gita/%.*sb/%.*s"
2810 len2
, p
->two
->path
);
2811 SHA1_Update(&ctx
, buffer
, len1
);
2813 xpp
.flags
= XDF_NEED_MINIMAL
;
2815 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2816 ecb
.outf
= xdiff_outf
;
2818 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2821 SHA1_Final(sha1
, &ctx
);
2825 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2827 struct diff_queue_struct
*q
= &diff_queued_diff
;
2829 int result
= diff_get_patch_id(options
, sha1
);
2831 for (i
= 0; i
< q
->nr
; i
++)
2832 diff_free_filepair(q
->queue
[i
]);
2836 q
->nr
= q
->alloc
= 0;
2841 static int is_summary_empty(const struct diff_queue_struct
*q
)
2845 for (i
= 0; i
< q
->nr
; i
++) {
2846 const struct diff_filepair
*p
= q
->queue
[i
];
2848 switch (p
->status
) {
2849 case DIFF_STATUS_DELETED
:
2850 case DIFF_STATUS_ADDED
:
2851 case DIFF_STATUS_COPIED
:
2852 case DIFF_STATUS_RENAMED
:
2857 if (p
->one
->mode
&& p
->two
->mode
&&
2858 p
->one
->mode
!= p
->two
->mode
)
2866 void diff_flush(struct diff_options
*options
)
2868 struct diff_queue_struct
*q
= &diff_queued_diff
;
2869 int i
, output_format
= options
->output_format
;
2873 * Order: raw, stat, summary, patch
2874 * or: name/name-status/checkdiff (other bits clear)
2879 if (output_format
& (DIFF_FORMAT_RAW
|
2881 DIFF_FORMAT_NAME_STATUS
|
2882 DIFF_FORMAT_CHECKDIFF
)) {
2883 for (i
= 0; i
< q
->nr
; i
++) {
2884 struct diff_filepair
*p
= q
->queue
[i
];
2885 if (check_pair_status(p
))
2886 flush_one_pair(p
, options
);
2891 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
2892 struct diffstat_t diffstat
;
2894 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2895 diffstat
.xm
.consume
= diffstat_consume
;
2896 for (i
= 0; i
< q
->nr
; i
++) {
2897 struct diff_filepair
*p
= q
->queue
[i
];
2898 if (check_pair_status(p
))
2899 diff_flush_stat(p
, options
, &diffstat
);
2901 if (output_format
& DIFF_FORMAT_NUMSTAT
)
2902 show_numstat(&diffstat
, options
);
2903 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
2904 show_stats(&diffstat
, options
);
2905 else if (output_format
& DIFF_FORMAT_SHORTSTAT
)
2906 show_shortstats(&diffstat
);
2910 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2911 for (i
= 0; i
< q
->nr
; i
++)
2912 diff_summary(q
->queue
[i
]);
2916 if (output_format
& DIFF_FORMAT_PATCH
) {
2918 if (options
->stat_sep
) {
2919 /* attach patch instead of inline */
2920 fputs(options
->stat_sep
, stdout
);
2922 putchar(options
->line_termination
);
2926 for (i
= 0; i
< q
->nr
; i
++) {
2927 struct diff_filepair
*p
= q
->queue
[i
];
2928 if (check_pair_status(p
))
2929 diff_flush_patch(p
, options
);
2933 if (output_format
& DIFF_FORMAT_CALLBACK
)
2934 options
->format_callback(q
, options
, options
->format_callback_data
);
2936 for (i
= 0; i
< q
->nr
; i
++)
2937 diff_free_filepair(q
->queue
[i
]);
2941 q
->nr
= q
->alloc
= 0;
2944 static void diffcore_apply_filter(const char *filter
)
2947 struct diff_queue_struct
*q
= &diff_queued_diff
;
2948 struct diff_queue_struct outq
;
2950 outq
.nr
= outq
.alloc
= 0;
2955 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2957 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2958 struct diff_filepair
*p
= q
->queue
[i
];
2959 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2961 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2963 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2964 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2965 strchr(filter
, p
->status
)))
2971 /* otherwise we will clear the whole queue
2972 * by copying the empty outq at the end of this
2973 * function, but first clear the current entries
2976 for (i
= 0; i
< q
->nr
; i
++)
2977 diff_free_filepair(q
->queue
[i
]);
2980 /* Only the matching ones */
2981 for (i
= 0; i
< q
->nr
; i
++) {
2982 struct diff_filepair
*p
= q
->queue
[i
];
2984 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2986 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2988 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2989 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2990 strchr(filter
, p
->status
)))
2993 diff_free_filepair(p
);
3000 void diffcore_std(struct diff_options
*options
)
3004 if (options
->break_opt
!= -1)
3005 diffcore_break(options
->break_opt
);
3006 if (options
->detect_rename
)
3007 diffcore_rename(options
);
3008 if (options
->break_opt
!= -1)
3009 diffcore_merge_broken();
3010 if (options
->pickaxe
)
3011 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3012 if (options
->orderfile
)
3013 diffcore_order(options
->orderfile
);
3014 diff_resolve_rename_copy();
3015 diffcore_apply_filter(options
->filter
);
3017 options
->has_changes
= !!diff_queued_diff
.nr
;
3021 void diff_addremove(struct diff_options
*options
,
3022 int addremove
, unsigned mode
,
3023 const unsigned char *sha1
,
3024 const char *base
, const char *path
)
3026 char concatpath
[PATH_MAX
];
3027 struct diff_filespec
*one
, *two
;
3029 /* This may look odd, but it is a preparation for
3030 * feeding "there are unchanged files which should
3031 * not produce diffs, but when you are doing copy
3032 * detection you would need them, so here they are"
3033 * entries to the diff-core. They will be prefixed
3034 * with something like '=' or '*' (I haven't decided
3035 * which but should not make any difference).
3036 * Feeding the same new and old to diff_change()
3037 * also has the same effect.
3038 * Before the final output happens, they are pruned after
3039 * merged into rename/copy pairs as appropriate.
3041 if (options
->reverse_diff
)
3042 addremove
= (addremove
== '+' ? '-' :
3043 addremove
== '-' ? '+' : addremove
);
3045 if (!path
) path
= "";
3046 sprintf(concatpath
, "%s%s", base
, path
);
3047 one
= alloc_filespec(concatpath
);
3048 two
= alloc_filespec(concatpath
);
3050 if (addremove
!= '+')
3051 fill_filespec(one
, sha1
, mode
);
3052 if (addremove
!= '-')
3053 fill_filespec(two
, sha1
, mode
);
3055 diff_queue(&diff_queued_diff
, one
, two
);
3056 options
->has_changes
= 1;
3059 void diff_change(struct diff_options
*options
,
3060 unsigned old_mode
, unsigned new_mode
,
3061 const unsigned char *old_sha1
,
3062 const unsigned char *new_sha1
,
3063 const char *base
, const char *path
)
3065 char concatpath
[PATH_MAX
];
3066 struct diff_filespec
*one
, *two
;
3068 if (options
->reverse_diff
) {
3070 const unsigned char *tmp_c
;
3071 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3072 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3074 if (!path
) path
= "";
3075 sprintf(concatpath
, "%s%s", base
, path
);
3076 one
= alloc_filespec(concatpath
);
3077 two
= alloc_filespec(concatpath
);
3078 fill_filespec(one
, old_sha1
, old_mode
);
3079 fill_filespec(two
, new_sha1
, new_mode
);
3081 diff_queue(&diff_queued_diff
, one
, two
);
3082 options
->has_changes
= 1;
3085 void diff_unmerge(struct diff_options
*options
,
3087 unsigned mode
, const unsigned char *sha1
)
3089 struct diff_filespec
*one
, *two
;
3090 one
= alloc_filespec(path
);
3091 two
= alloc_filespec(path
);
3092 fill_filespec(one
, sha1
, mode
);
3093 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;