2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #ifdef NO_FAST_WORKING_DIRECTORY
13 #define FAST_WORKING_DIRECTORY 0
15 #define FAST_WORKING_DIRECTORY 1
18 static int use_size_cache
;
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
);
55 * These are to give UI layer defaults.
56 * The core-level commands such as git-diff-files should
57 * never be affected by the setting of diff.renames
58 * the user happens to have in the configuration file.
60 int git_diff_ui_config(const char *var
, const char *value
)
62 if (!strcmp(var
, "diff.renamelimit")) {
63 diff_rename_limit_default
= git_config_int(var
, value
);
66 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
67 diff_use_color_default
= git_config_colorbool(var
, value
);
70 if (!strcmp(var
, "diff.renames")) {
72 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
73 else if (!strcasecmp(value
, "copies") ||
74 !strcasecmp(value
, "copy"))
75 diff_detect_rename_default
= DIFF_DETECT_COPY
;
76 else if (git_config_bool(var
,value
))
77 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
80 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
81 int slot
= parse_diff_color_slot(var
, 11);
82 color_parse(value
, var
, diff_colors
[slot
]);
85 return git_default_config(var
, value
);
88 static char *quote_one(const char *str
)
95 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
98 xp
= xmalloc(needlen
+ 1);
99 quote_c_style(str
, xp
, NULL
, 0);
103 static char *quote_two(const char *one
, const char *two
)
105 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
106 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
109 if (need_one
+ need_two
) {
110 if (!need_one
) need_one
= strlen(one
);
111 if (!need_two
) need_one
= strlen(two
);
113 xp
= xmalloc(need_one
+ need_two
+ 3);
115 quote_c_style(one
, xp
+ 1, NULL
, 1);
116 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
117 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
120 need_one
= strlen(one
);
121 need_two
= strlen(two
);
122 xp
= xmalloc(need_one
+ need_two
+ 1);
124 strcpy(xp
+ need_one
, two
);
128 static const char *external_diff(void)
130 static const char *external_diff_cmd
= NULL
;
131 static int done_preparing
= 0;
134 return external_diff_cmd
;
135 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
137 return external_diff_cmd
;
140 #define TEMPFILE_PATH_LEN 50
142 static struct diff_tempfile
{
143 const char *name
; /* filename external diff should read from */
146 char tmp_path
[TEMPFILE_PATH_LEN
];
149 static int count_lines(const char *data
, int size
)
151 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
158 completely_empty
= 0;
162 completely_empty
= 0;
165 if (completely_empty
)
168 count
++; /* no trailing newline */
172 static void print_line_count(int count
)
182 printf("1,%d", count
);
187 static void copy_file(int prefix
, const char *data
, int size
,
188 const char *set
, const char *reset
)
190 int ch
, nl_just_seen
= 1;
199 fputs(reset
, stdout
);
205 printf("%s\n\\ No newline at end of file\n", reset
);
208 static void emit_rewrite_diff(const char *name_a
,
210 struct diff_filespec
*one
,
211 struct diff_filespec
*two
,
215 const char *name_a_tab
, *name_b_tab
;
216 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
217 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
218 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
219 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
220 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
222 name_a
+= (*name_a
== '/');
223 name_b
+= (*name_b
== '/');
224 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
225 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
227 diff_populate_filespec(one
, 0);
228 diff_populate_filespec(two
, 0);
229 lc_a
= count_lines(one
->data
, one
->size
);
230 lc_b
= count_lines(two
->data
, two
->size
);
231 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
232 metainfo
, name_a
, name_a_tab
, reset
,
233 metainfo
, name_b
, name_b_tab
, reset
, fraginfo
);
234 print_line_count(lc_a
);
236 print_line_count(lc_b
);
237 printf(" @@%s\n", reset
);
239 copy_file('-', one
->data
, one
->size
, old
, reset
);
241 copy_file('+', two
->data
, two
->size
, new, reset
);
244 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
246 if (!DIFF_FILE_VALID(one
)) {
247 mf
->ptr
= (char *)""; /* does not matter */
251 else if (diff_populate_filespec(one
, 0))
254 mf
->size
= one
->size
;
258 struct diff_words_buffer
{
261 long current
; /* output pointer */
262 int suppressed_newline
;
265 static void diff_words_append(char *line
, unsigned long len
,
266 struct diff_words_buffer
*buffer
)
268 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
269 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
270 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
274 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
275 buffer
->text
.size
+= len
;
278 struct diff_words_data
{
279 struct xdiff_emit_state xm
;
280 struct diff_words_buffer minus
, plus
;
283 static void print_word(struct diff_words_buffer
*buffer
, int len
, int color
,
284 int suppress_newline
)
292 ptr
= buffer
->text
.ptr
+ buffer
->current
;
293 buffer
->current
+= len
;
295 if (ptr
[len
- 1] == '\n') {
300 fputs(diff_get_color(1, color
), stdout
);
301 fwrite(ptr
, len
, 1, stdout
);
302 fputs(diff_get_color(1, DIFF_RESET
), stdout
);
305 if (suppress_newline
)
306 buffer
->suppressed_newline
= 1;
312 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
314 struct diff_words_data
*diff_words
= priv
;
316 if (diff_words
->minus
.suppressed_newline
) {
319 diff_words
->minus
.suppressed_newline
= 0;
325 print_word(&diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
328 print_word(&diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
331 print_word(&diff_words
->plus
, len
, DIFF_PLAIN
, 0);
332 diff_words
->minus
.current
+= len
;
337 /* this executes the word diff on the accumulated buffers */
338 static void diff_words_show(struct diff_words_data
*diff_words
)
343 mmfile_t minus
, plus
;
346 minus
.size
= diff_words
->minus
.text
.size
;
347 minus
.ptr
= xmalloc(minus
.size
);
348 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
349 for (i
= 0; i
< minus
.size
; i
++)
350 if (isspace(minus
.ptr
[i
]))
352 diff_words
->minus
.current
= 0;
354 plus
.size
= diff_words
->plus
.text
.size
;
355 plus
.ptr
= xmalloc(plus
.size
);
356 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
357 for (i
= 0; i
< plus
.size
; i
++)
358 if (isspace(plus
.ptr
[i
]))
360 diff_words
->plus
.current
= 0;
362 xpp
.flags
= XDF_NEED_MINIMAL
;
363 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
365 ecb
.outf
= xdiff_outf
;
366 ecb
.priv
= diff_words
;
367 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
368 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
372 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
374 if (diff_words
->minus
.suppressed_newline
) {
376 diff_words
->minus
.suppressed_newline
= 0;
380 struct emit_callback
{
381 struct xdiff_emit_state xm
;
382 int nparents
, color_diff
;
383 const char **label_path
;
384 struct diff_words_data
*diff_words
;
388 static void free_diff_words_data(struct emit_callback
*ecbdata
)
390 if (ecbdata
->diff_words
) {
392 if (ecbdata
->diff_words
->minus
.text
.size
||
393 ecbdata
->diff_words
->plus
.text
.size
)
394 diff_words_show(ecbdata
->diff_words
);
396 if (ecbdata
->diff_words
->minus
.text
.ptr
)
397 free (ecbdata
->diff_words
->minus
.text
.ptr
);
398 if (ecbdata
->diff_words
->plus
.text
.ptr
)
399 free (ecbdata
->diff_words
->plus
.text
.ptr
);
400 free(ecbdata
->diff_words
);
401 ecbdata
->diff_words
= NULL
;
405 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
408 return diff_colors
[ix
];
412 static void emit_line(const char *set
, const char *reset
, const char *line
, int len
)
414 if (len
> 0 && line
[len
-1] == '\n')
417 fwrite(line
, len
, 1, stdout
);
421 static void emit_line_with_ws(int nparents
,
422 const char *set
, const char *reset
, const char *ws
,
423 const char *line
, int len
)
426 int last_tab_in_indent
= -1;
427 int last_space_in_indent
= -1;
430 int need_highlight_leading_space
= 0;
431 /* The line is a newly added line. Does it have funny leading
432 * whitespaces? In indent, SP should never precede a TAB.
434 for (i
= col0
; i
< len
; i
++) {
435 if (line
[i
] == '\t') {
436 last_tab_in_indent
= i
;
437 if (0 <= last_space_in_indent
)
438 need_highlight_leading_space
= 1;
440 else if (line
[i
] == ' ')
441 last_space_in_indent
= i
;
446 fwrite(line
, col0
, 1, stdout
);
447 fputs(reset
, stdout
);
448 if (((i
== len
) || line
[i
] == '\n') && i
!= col0
) {
449 /* The whole line was indent */
450 emit_line(ws
, reset
, line
+ col0
, len
- col0
);
454 if (need_highlight_leading_space
) {
455 while (i
< last_tab_in_indent
) {
456 if (line
[i
] == ' ') {
459 fputs(reset
, stdout
);
467 if (line
[tail
] == '\n' && i
< tail
)
470 if (!isspace(line
[tail
]))
474 if ((i
< tail
&& line
[tail
+ 1] != '\n')) {
475 /* This has whitespace between tail+1..len */
477 fwrite(line
+ i
, tail
- i
+ 1, 1, stdout
);
478 fputs(reset
, stdout
);
479 emit_line(ws
, reset
, line
+ tail
+ 1, len
- tail
- 1);
482 emit_line(set
, reset
, line
+ i
, len
- i
);
485 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
487 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
488 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
491 emit_line(set
, reset
, line
, len
);
493 emit_line_with_ws(ecbdata
->nparents
, set
, reset
, ws
,
497 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
501 struct emit_callback
*ecbdata
= priv
;
502 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
503 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
505 *(ecbdata
->found_changesp
) = 1;
507 if (ecbdata
->label_path
[0]) {
508 const char *name_a_tab
, *name_b_tab
;
510 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
511 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
513 printf("%s--- %s%s%s\n",
514 set
, ecbdata
->label_path
[0], reset
, name_a_tab
);
515 printf("%s+++ %s%s%s\n",
516 set
, ecbdata
->label_path
[1], reset
, name_b_tab
);
517 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
520 /* This is not really necessary for now because
521 * this codepath only deals with two-way diffs.
523 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
525 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
526 ecbdata
->nparents
= i
- 1;
527 emit_line(diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
532 if (len
< ecbdata
->nparents
) {
534 emit_line(reset
, reset
, line
, len
);
539 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
540 /* fall back to normal diff */
541 free_diff_words_data(ecbdata
);
542 if (ecbdata
->diff_words
) {
543 if (line
[0] == '-') {
544 diff_words_append(line
, len
,
545 &ecbdata
->diff_words
->minus
);
547 } else if (line
[0] == '+') {
548 diff_words_append(line
, len
,
549 &ecbdata
->diff_words
->plus
);
552 if (ecbdata
->diff_words
->minus
.text
.size
||
553 ecbdata
->diff_words
->plus
.text
.size
)
554 diff_words_show(ecbdata
->diff_words
);
557 emit_line(set
, reset
, line
, len
);
560 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
562 color
= DIFF_FILE_OLD
;
563 else if (line
[i
] == '+')
564 color
= DIFF_FILE_NEW
;
567 if (color
!= DIFF_FILE_NEW
) {
568 emit_line(diff_get_color(ecbdata
->color_diff
, color
),
572 emit_add_line(reset
, ecbdata
, line
, len
);
575 static char *pprint_rename(const char *a
, const char *b
)
580 int pfx_length
, sfx_length
;
581 int len_a
= strlen(a
);
582 int len_b
= strlen(b
);
583 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
584 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
586 if (qlen_a
|| qlen_b
) {
587 if (qlen_a
) len_a
= qlen_a
;
588 if (qlen_b
) len_b
= qlen_b
;
589 name
= xmalloc( len_a
+ len_b
+ 5 );
591 quote_c_style(a
, name
, NULL
, 0);
593 memcpy(name
, a
, len_a
);
594 memcpy(name
+ len_a
, " => ", 4);
596 quote_c_style(b
, name
+ len_a
+ 4, NULL
, 0);
598 memcpy(name
+ len_a
+ 4, b
, len_b
+ 1);
602 /* Find common prefix */
604 while (*old
&& *new && *old
== *new) {
606 pfx_length
= old
- a
+ 1;
611 /* Find common suffix */
615 while (a
<= old
&& b
<= new && *old
== *new) {
617 sfx_length
= len_a
- (old
- a
);
623 * pfx{mid-a => mid-b}sfx
624 * {pfx-a => pfx-b}sfx
625 * pfx{sfx-a => sfx-b}
628 if (pfx_length
+ sfx_length
) {
629 int a_midlen
= len_a
- pfx_length
- sfx_length
;
630 int b_midlen
= len_b
- pfx_length
- sfx_length
;
631 if (a_midlen
< 0) a_midlen
= 0;
632 if (b_midlen
< 0) b_midlen
= 0;
634 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
635 sprintf(name
, "%.*s{%.*s => %.*s}%s",
637 a_midlen
, a
+ pfx_length
,
638 b_midlen
, b
+ pfx_length
,
639 a
+ len_a
- sfx_length
);
642 name
= xmalloc(len_a
+ len_b
+ 5);
643 sprintf(name
, "%s => %s", a
, b
);
649 struct xdiff_emit_state xm
;
653 struct diffstat_file
{
655 unsigned is_unmerged
:1;
656 unsigned is_binary
:1;
657 unsigned is_renamed
:1;
658 unsigned int added
, deleted
;
662 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
666 struct diffstat_file
*x
;
667 x
= xcalloc(sizeof (*x
), 1);
668 if (diffstat
->nr
== diffstat
->alloc
) {
669 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
670 diffstat
->files
= xrealloc(diffstat
->files
,
671 diffstat
->alloc
* sizeof(x
));
673 diffstat
->files
[diffstat
->nr
++] = x
;
675 x
->name
= pprint_rename(name_a
, name_b
);
679 x
->name
= xstrdup(name_a
);
683 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
685 struct diffstat_t
*diffstat
= priv
;
686 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
690 else if (line
[0] == '-')
694 const char mime_boundary_leader
[] = "------------";
696 static int scale_linear(int it
, int width
, int max_change
)
699 * make sure that at least one '-' is printed if there were deletions,
700 * and likewise for '+'.
704 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
707 static void show_name(const char *prefix
, const char *name
, int len
,
708 const char *reset
, const char *set
)
710 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
713 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
723 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
725 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
726 int max_change
= 0, max_len
= 0;
727 int total_files
= data
->nr
;
728 int width
, name_width
;
729 const char *reset
, *set
, *add_c
, *del_c
;
734 width
= options
->stat_width
? options
->stat_width
: 80;
735 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
737 /* Sanity: give at least 5 columns to the graph,
738 * but leave at least 10 columns for the name.
740 if (width
< name_width
+ 15) {
741 if (name_width
<= 25)
742 width
= name_width
+ 15;
744 name_width
= width
- 15;
747 /* Find the longest filename and max number of changes */
748 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
749 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
750 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
751 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
753 for (i
= 0; i
< data
->nr
; i
++) {
754 struct diffstat_file
*file
= data
->files
[i
];
755 int change
= file
->added
+ file
->deleted
;
757 if (!file
->is_renamed
) { /* renames are already quoted by pprint_rename */
758 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
760 char *qname
= xmalloc(len
+ 1);
761 quote_c_style(file
->name
, qname
, NULL
, 0);
767 len
= strlen(file
->name
);
771 if (file
->is_binary
|| file
->is_unmerged
)
773 if (max_change
< change
)
777 /* Compute the width of the graph part;
778 * 10 is for one blank at the beginning of the line plus
779 * " | count " between the name and the graph.
781 * From here on, name_width is the width of the name area,
782 * and width is the width of the graph area.
784 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
785 if (width
< (name_width
+ 10) + max_change
)
786 width
= width
- (name_width
+ 10);
790 for (i
= 0; i
< data
->nr
; i
++) {
791 const char *prefix
= "";
792 char *name
= data
->files
[i
]->name
;
793 int added
= data
->files
[i
]->added
;
794 int deleted
= data
->files
[i
]->deleted
;
798 * "scale" the filename
801 name_len
= strlen(name
);
802 if (name_width
< name_len
) {
806 name
+= name_len
- len
;
807 slash
= strchr(name
, '/');
812 if (data
->files
[i
]->is_binary
) {
813 show_name(prefix
, name
, len
, reset
, set
);
815 printf("%s%d%s", del_c
, deleted
, reset
);
817 printf("%s%d%s", add_c
, added
, reset
);
820 goto free_diffstat_file
;
822 else if (data
->files
[i
]->is_unmerged
) {
823 show_name(prefix
, name
, len
, reset
, set
);
824 printf(" Unmerged\n");
825 goto free_diffstat_file
;
827 else if (!data
->files
[i
]->is_renamed
&&
828 (added
+ deleted
== 0)) {
830 goto free_diffstat_file
;
834 * scale the add/delete
842 if (width
<= max_change
) {
843 add
= scale_linear(add
, width
, max_change
);
844 del
= scale_linear(del
, width
, max_change
);
847 show_name(prefix
, name
, len
, reset
, set
);
848 printf("%5d ", added
+ deleted
);
849 show_graph('+', add
, add_c
, reset
);
850 show_graph('-', del
, del_c
, reset
);
853 free(data
->files
[i
]->name
);
854 free(data
->files
[i
]);
857 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
858 set
, total_files
, adds
, dels
, reset
);
861 static void show_shortstats(struct diffstat_t
* data
)
863 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
868 for (i
= 0; i
< data
->nr
; i
++) {
869 if (!data
->files
[i
]->is_binary
&&
870 !data
->files
[i
]->is_unmerged
) {
871 int added
= data
->files
[i
]->added
;
872 int deleted
= data
->files
[i
]->deleted
;
873 if (!data
->files
[i
]->is_renamed
&&
874 (added
+ deleted
== 0)) {
881 free(data
->files
[i
]->name
);
882 free(data
->files
[i
]);
886 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
887 total_files
, adds
, dels
);
890 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
894 for (i
= 0; i
< data
->nr
; i
++) {
895 struct diffstat_file
*file
= data
->files
[i
];
900 printf("%d\t%d\t", file
->added
, file
->deleted
);
901 if (options
->line_termination
&& !file
->is_renamed
&&
902 quote_c_style(file
->name
, NULL
, NULL
, 0))
903 quote_c_style(file
->name
, NULL
, stdout
, 0);
905 fputs(file
->name
, stdout
);
906 putchar(options
->line_termination
);
911 struct xdiff_emit_state xm
;
912 const char *filename
;
913 int lineno
, color_diff
;
916 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
918 struct checkdiff_t
*data
= priv
;
919 const char *ws
= diff_get_color(data
->color_diff
, DIFF_WHITESPACE
);
920 const char *reset
= diff_get_color(data
->color_diff
, DIFF_RESET
);
921 const char *set
= diff_get_color(data
->color_diff
, DIFF_FILE_NEW
);
923 if (line
[0] == '+') {
924 int i
, spaces
= 0, space_before_tab
= 0, white_space_at_end
= 0;
926 /* check space before tab */
927 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
930 if (line
[i
- 1] == '\t' && spaces
)
931 space_before_tab
= 1;
933 /* check white space at line end */
934 if (line
[len
- 1] == '\n')
936 if (isspace(line
[len
- 1]))
937 white_space_at_end
= 1;
939 if (space_before_tab
|| white_space_at_end
) {
940 printf("%s:%d: %s", data
->filename
, data
->lineno
, ws
);
941 if (space_before_tab
) {
942 printf("space before tab");
943 if (white_space_at_end
)
946 if (white_space_at_end
)
947 printf("white space at end");
948 printf(":%s ", reset
);
949 emit_line_with_ws(1, set
, reset
, ws
, line
, len
);
953 } else if (line
[0] == ' ')
955 else if (line
[0] == '@') {
956 char *plus
= strchr(line
, '+');
958 data
->lineno
= strtol(plus
, NULL
, 10);
964 static unsigned char *deflate_it(char *data
,
966 unsigned long *result_size
)
969 unsigned char *deflated
;
972 memset(&stream
, 0, sizeof(stream
));
973 deflateInit(&stream
, zlib_compression_level
);
974 bound
= deflateBound(&stream
, size
);
975 deflated
= xmalloc(bound
);
976 stream
.next_out
= deflated
;
977 stream
.avail_out
= bound
;
979 stream
.next_in
= (unsigned char *)data
;
980 stream
.avail_in
= size
;
981 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
984 *result_size
= stream
.total_out
;
988 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
994 unsigned long orig_size
;
995 unsigned long delta_size
;
996 unsigned long deflate_size
;
997 unsigned long data_size
;
999 /* We could do deflated delta, or we could do just deflated two,
1000 * whichever is smaller.
1003 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1004 if (one
->size
&& two
->size
) {
1005 delta
= diff_delta(one
->ptr
, one
->size
,
1006 two
->ptr
, two
->size
,
1007 &delta_size
, deflate_size
);
1009 void *to_free
= delta
;
1010 orig_size
= delta_size
;
1011 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1016 if (delta
&& delta_size
< deflate_size
) {
1017 printf("delta %lu\n", orig_size
);
1020 data_size
= delta_size
;
1023 printf("literal %lu\n", two
->size
);
1026 data_size
= deflate_size
;
1029 /* emit data encoded in base85 */
1032 int bytes
= (52 < data_size
) ? 52 : data_size
;
1036 line
[0] = bytes
+ 'A' - 1;
1038 line
[0] = bytes
- 26 + 'a' - 1;
1039 encode_85(line
+ 1, cp
, bytes
);
1040 cp
= (char *) cp
+ bytes
;
1047 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
1049 printf("GIT binary patch\n");
1050 emit_binary_diff_body(one
, two
);
1051 emit_binary_diff_body(two
, one
);
1054 #define FIRST_FEW_BYTES 8000
1055 static int mmfile_is_binary(mmfile_t
*mf
)
1058 if (FIRST_FEW_BYTES
< sz
)
1059 sz
= FIRST_FEW_BYTES
;
1060 return !!memchr(mf
->ptr
, 0, sz
);
1063 static void builtin_diff(const char *name_a
,
1065 struct diff_filespec
*one
,
1066 struct diff_filespec
*two
,
1067 const char *xfrm_msg
,
1068 struct diff_options
*o
,
1069 int complete_rewrite
)
1073 char *a_one
, *b_two
;
1074 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
1075 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
1077 a_one
= quote_two("a/", name_a
+ (*name_a
== '/'));
1078 b_two
= quote_two("b/", name_b
+ (*name_b
== '/'));
1079 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1080 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1081 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1082 if (lbl
[0][0] == '/') {
1084 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1085 if (xfrm_msg
&& xfrm_msg
[0])
1086 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1088 else if (lbl
[1][0] == '/') {
1089 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1090 if (xfrm_msg
&& xfrm_msg
[0])
1091 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1094 if (one
->mode
!= two
->mode
) {
1095 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
1096 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
1098 if (xfrm_msg
&& xfrm_msg
[0])
1099 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1101 * we do not run diff between different kind
1104 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1105 goto free_ab_and_return
;
1106 if (complete_rewrite
) {
1107 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1109 o
->found_changes
= 1;
1110 goto free_ab_and_return
;
1114 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1115 die("unable to read files to diff");
1117 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
1118 /* Quite common confusing case */
1119 if (mf1
.size
== mf2
.size
&&
1120 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1121 goto free_ab_and_return
;
1123 emit_binary_diff(&mf1
, &mf2
);
1125 printf("Binary files %s and %s differ\n",
1127 o
->found_changes
= 1;
1130 /* Crazy xdl interfaces.. */
1131 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1135 struct emit_callback ecbdata
;
1137 memset(&ecbdata
, 0, sizeof(ecbdata
));
1138 ecbdata
.label_path
= lbl
;
1139 ecbdata
.color_diff
= o
->color_diff
;
1140 ecbdata
.found_changesp
= &o
->found_changes
;
1141 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1142 xecfg
.ctxlen
= o
->context
;
1143 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1146 else if (!prefixcmp(diffopts
, "--unified="))
1147 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1148 else if (!prefixcmp(diffopts
, "-u"))
1149 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1150 ecb
.outf
= xdiff_outf
;
1151 ecb
.priv
= &ecbdata
;
1152 ecbdata
.xm
.consume
= fn_out_consume
;
1153 if (o
->color_diff_words
)
1154 ecbdata
.diff_words
=
1155 xcalloc(1, sizeof(struct diff_words_data
));
1156 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1157 if (o
->color_diff_words
)
1158 free_diff_words_data(&ecbdata
);
1167 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1168 struct diff_filespec
*one
,
1169 struct diff_filespec
*two
,
1170 struct diffstat_t
*diffstat
,
1171 struct diff_options
*o
,
1172 int complete_rewrite
)
1175 struct diffstat_file
*data
;
1177 data
= diffstat_add(diffstat
, name_a
, name_b
);
1180 data
->is_unmerged
= 1;
1183 if (complete_rewrite
) {
1184 diff_populate_filespec(one
, 0);
1185 diff_populate_filespec(two
, 0);
1186 data
->deleted
= count_lines(one
->data
, one
->size
);
1187 data
->added
= count_lines(two
->data
, two
->size
);
1190 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1191 die("unable to read files to diff");
1193 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
)) {
1194 data
->is_binary
= 1;
1195 data
->added
= mf2
.size
;
1196 data
->deleted
= mf1
.size
;
1198 /* Crazy xdl interfaces.. */
1203 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1206 ecb
.outf
= xdiff_outf
;
1207 ecb
.priv
= diffstat
;
1208 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1212 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1213 struct diff_filespec
*one
,
1214 struct diff_filespec
*two
, struct diff_options
*o
)
1217 struct checkdiff_t data
;
1222 memset(&data
, 0, sizeof(data
));
1223 data
.xm
.consume
= checkdiff_consume
;
1224 data
.filename
= name_b
? name_b
: name_a
;
1226 data
.color_diff
= o
->color_diff
;
1228 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1229 die("unable to read files to diff");
1231 if (mmfile_is_binary(&mf2
))
1234 /* Crazy xdl interfaces.. */
1239 xpp
.flags
= XDF_NEED_MINIMAL
;
1242 ecb
.outf
= xdiff_outf
;
1244 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1248 struct diff_filespec
*alloc_filespec(const char *path
)
1250 int namelen
= strlen(path
);
1251 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1253 memset(spec
, 0, sizeof(*spec
));
1254 spec
->path
= (char *)(spec
+ 1);
1255 memcpy(spec
->path
, path
, namelen
+1);
1259 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1260 unsigned short mode
)
1263 spec
->mode
= canon_mode(mode
);
1264 hashcpy(spec
->sha1
, sha1
);
1265 spec
->sha1_valid
= !is_null_sha1(sha1
);
1270 * Given a name and sha1 pair, if the dircache tells us the file in
1271 * the work tree has that object contents, return true, so that
1272 * prepare_temp_file() does not have to inflate and extract.
1274 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1276 struct cache_entry
*ce
;
1280 /* We do not read the cache ourselves here, because the
1281 * benchmark with my previous version that always reads cache
1282 * shows that it makes things worse for diff-tree comparing
1283 * two linux-2.6 kernel trees in an already checked out work
1284 * tree. This is because most diff-tree comparisons deal with
1285 * only a small number of files, while reading the cache is
1286 * expensive for a large project, and its cost outweighs the
1287 * savings we get by not inflating the object to a temporary
1288 * file. Practically, this code only helps when we are used
1289 * by diff-cache --cached, which does read the cache before
1295 /* We want to avoid the working directory if our caller
1296 * doesn't need the data in a normal file, this system
1297 * is rather slow with its stat/open/mmap/close syscalls,
1298 * and the object is contained in a pack file. The pack
1299 * is probably already open and will be faster to obtain
1300 * the data through than the working directory. Loose
1301 * objects however would tend to be slower as they need
1302 * to be individually opened and inflated.
1304 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1308 pos
= cache_name_pos(name
, len
);
1311 ce
= active_cache
[pos
];
1312 if ((lstat(name
, &st
) < 0) ||
1313 !S_ISREG(st
.st_mode
) || /* careful! */
1314 ce_match_stat(ce
, &st
, 0) ||
1315 hashcmp(sha1
, ce
->sha1
))
1317 /* we return 1 only when we can stat, it is a regular file,
1318 * stat information matches, and sha1 recorded in the cache
1319 * matches. I.e. we know the file in the work tree really is
1320 * the same as the <name, sha1> pair.
1325 static struct sha1_size_cache
{
1326 unsigned char sha1
[20];
1328 } **sha1_size_cache
;
1329 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
1331 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
1336 struct sha1_size_cache
*e
;
1339 last
= sha1_size_cache_nr
;
1340 while (last
> first
) {
1341 int cmp
, next
= (last
+ first
) >> 1;
1342 e
= sha1_size_cache
[next
];
1343 cmp
= hashcmp(e
->sha1
, sha1
);
1355 /* insert to make it at "first" */
1356 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
1357 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
1358 sha1_size_cache
= xrealloc(sha1_size_cache
,
1359 sha1_size_cache_alloc
*
1360 sizeof(*sha1_size_cache
));
1362 sha1_size_cache_nr
++;
1363 if (first
< sha1_size_cache_nr
)
1364 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
1365 (sha1_size_cache_nr
- first
- 1) *
1366 sizeof(*sha1_size_cache
));
1367 e
= xmalloc(sizeof(struct sha1_size_cache
));
1368 sha1_size_cache
[first
] = e
;
1369 hashcpy(e
->sha1
, sha1
);
1374 static int populate_from_stdin(struct diff_filespec
*s
)
1376 #define INCREMENT 1024
1384 buf
= xrealloc(buf
, size
+ INCREMENT
);
1385 got
= xread(0, buf
+ size
, INCREMENT
);
1389 return error("error while reading from stdin %s",
1393 s
->should_munmap
= 0;
1401 * While doing rename detection and pickaxe operation, we may need to
1402 * grab the data for the blob (or file) for our own in-core comparison.
1403 * diff_filespec has data and size fields for this purpose.
1405 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1408 if (!DIFF_FILE_VALID(s
))
1409 die("internal error: asking to populate invalid file.");
1410 if (S_ISDIR(s
->mode
))
1413 if (!use_size_cache
)
1418 if (!s
->sha1_valid
||
1419 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1425 if (!strcmp(s
->path
, "-"))
1426 return populate_from_stdin(s
);
1428 if (lstat(s
->path
, &st
) < 0) {
1429 if (errno
== ENOENT
) {
1433 s
->data
= (char *)"";
1438 s
->size
= xsize_t(st
.st_size
);
1443 if (S_ISLNK(st
.st_mode
)) {
1445 s
->data
= xmalloc(s
->size
);
1447 ret
= readlink(s
->path
, s
->data
, s
->size
);
1454 fd
= open(s
->path
, O_RDONLY
);
1457 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1459 s
->should_munmap
= 1;
1462 * Convert from working tree format to canonical git format
1466 if (convert_to_git(s
->path
, &buf
, &size
)) {
1467 munmap(s
->data
, s
->size
);
1468 s
->should_munmap
= 0;
1475 enum object_type type
;
1476 struct sha1_size_cache
*e
;
1479 e
= locate_size_cache(s
->sha1
, 1, 0);
1484 type
= sha1_object_info(s
->sha1
, &s
->size
);
1486 locate_size_cache(s
->sha1
, 0, s
->size
);
1489 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1496 void diff_free_filespec_data(struct diff_filespec
*s
)
1500 else if (s
->should_munmap
)
1501 munmap(s
->data
, s
->size
);
1502 s
->should_free
= s
->should_munmap
= 0;
1508 static void prep_temp_blob(struct diff_tempfile
*temp
,
1511 const unsigned char *sha1
,
1516 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1518 die("unable to create temp-file");
1519 if (write_in_full(fd
, blob
, size
) != size
)
1520 die("unable to write temp-file");
1522 temp
->name
= temp
->tmp_path
;
1523 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1525 sprintf(temp
->mode
, "%06o", mode
);
1528 static void prepare_temp_file(const char *name
,
1529 struct diff_tempfile
*temp
,
1530 struct diff_filespec
*one
)
1532 if (!DIFF_FILE_VALID(one
)) {
1534 /* A '-' entry produces this for file-2, and
1535 * a '+' entry produces this for file-1.
1537 temp
->name
= "/dev/null";
1538 strcpy(temp
->hex
, ".");
1539 strcpy(temp
->mode
, ".");
1543 if (!one
->sha1_valid
||
1544 reuse_worktree_file(name
, one
->sha1
, 1)) {
1546 if (lstat(name
, &st
) < 0) {
1547 if (errno
== ENOENT
)
1548 goto not_a_valid_file
;
1549 die("stat(%s): %s", name
, strerror(errno
));
1551 if (S_ISLNK(st
.st_mode
)) {
1553 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1554 size_t sz
= xsize_t(st
.st_size
);
1555 if (sizeof(buf
) <= st
.st_size
)
1556 die("symlink too long: %s", name
);
1557 ret
= readlink(name
, buf
, sz
);
1559 die("readlink(%s)", name
);
1560 prep_temp_blob(temp
, buf
, sz
,
1562 one
->sha1
: null_sha1
),
1564 one
->mode
: S_IFLNK
));
1567 /* we can borrow from the file in the work tree */
1569 if (!one
->sha1_valid
)
1570 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1572 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1573 /* Even though we may sometimes borrow the
1574 * contents from the work tree, we always want
1575 * one->mode. mode is trustworthy even when
1576 * !(one->sha1_valid), as long as
1577 * DIFF_FILE_VALID(one).
1579 sprintf(temp
->mode
, "%06o", one
->mode
);
1584 if (diff_populate_filespec(one
, 0))
1585 die("cannot read data blob for %s", one
->path
);
1586 prep_temp_blob(temp
, one
->data
, one
->size
,
1587 one
->sha1
, one
->mode
);
1591 static void remove_tempfile(void)
1595 for (i
= 0; i
< 2; i
++)
1596 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1597 unlink(diff_temp
[i
].name
);
1598 diff_temp
[i
].name
= NULL
;
1602 static void remove_tempfile_on_signal(int signo
)
1605 signal(SIGINT
, SIG_DFL
);
1609 static int spawn_prog(const char *pgm
, const char **arg
)
1617 die("unable to fork");
1619 execvp(pgm
, (char *const*) arg
);
1623 while (waitpid(pid
, &status
, 0) < 0) {
1629 /* Earlier we did not check the exit status because
1630 * diff exits non-zero if files are different, and
1631 * we are not interested in knowing that. It was a
1632 * mistake which made it harder to quit a diff-*
1633 * session that uses the git-apply-patch-script as
1634 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1635 * should also exit non-zero only when it wants to
1636 * abort the entire diff-* session.
1638 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1643 /* An external diff command takes:
1645 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1646 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1649 static void run_external_diff(const char *pgm
,
1652 struct diff_filespec
*one
,
1653 struct diff_filespec
*two
,
1654 const char *xfrm_msg
,
1655 int complete_rewrite
)
1657 const char *spawn_arg
[10];
1658 struct diff_tempfile
*temp
= diff_temp
;
1660 static int atexit_asked
= 0;
1661 const char *othername
;
1662 const char **arg
= &spawn_arg
[0];
1664 othername
= (other
? other
: name
);
1666 prepare_temp_file(name
, &temp
[0], one
);
1667 prepare_temp_file(othername
, &temp
[1], two
);
1668 if (! atexit_asked
&&
1669 (temp
[0].name
== temp
[0].tmp_path
||
1670 temp
[1].name
== temp
[1].tmp_path
)) {
1672 atexit(remove_tempfile
);
1674 signal(SIGINT
, remove_tempfile_on_signal
);
1680 *arg
++ = temp
[0].name
;
1681 *arg
++ = temp
[0].hex
;
1682 *arg
++ = temp
[0].mode
;
1683 *arg
++ = temp
[1].name
;
1684 *arg
++ = temp
[1].hex
;
1685 *arg
++ = temp
[1].mode
;
1695 retval
= spawn_prog(pgm
, spawn_arg
);
1698 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1703 static void run_diff_cmd(const char *pgm
,
1706 struct diff_filespec
*one
,
1707 struct diff_filespec
*two
,
1708 const char *xfrm_msg
,
1709 struct diff_options
*o
,
1710 int complete_rewrite
)
1713 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1718 builtin_diff(name
, other
? other
: name
,
1719 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1721 printf("* Unmerged path %s\n", name
);
1724 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1726 if (DIFF_FILE_VALID(one
)) {
1727 if (!one
->sha1_valid
) {
1729 if (!strcmp(one
->path
, "-")) {
1730 hashcpy(one
->sha1
, null_sha1
);
1733 if (lstat(one
->path
, &st
) < 0)
1734 die("stat %s", one
->path
);
1735 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1736 die("cannot hash %s\n", one
->path
);
1743 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1745 const char *pgm
= external_diff();
1746 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1747 struct diff_filespec
*one
;
1748 struct diff_filespec
*two
;
1751 char *name_munged
, *other_munged
;
1752 int complete_rewrite
= 0;
1755 if (DIFF_PAIR_UNMERGED(p
)) {
1757 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1761 name
= p
->one
->path
;
1762 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1763 name_munged
= quote_one(name
);
1764 other_munged
= quote_one(other
);
1765 one
= p
->one
; two
= p
->two
;
1767 diff_fill_sha1_info(one
);
1768 diff_fill_sha1_info(two
);
1771 switch (p
->status
) {
1772 case DIFF_STATUS_COPIED
:
1773 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1774 "similarity index %d%%\n"
1777 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1778 name_munged
, other_munged
);
1780 case DIFF_STATUS_RENAMED
:
1781 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1782 "similarity index %d%%\n"
1785 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1786 name_munged
, other_munged
);
1788 case DIFF_STATUS_MODIFIED
:
1790 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1791 "dissimilarity index %d%%\n",
1792 (int)(0.5 + p
->score
*
1794 complete_rewrite
= 1;
1803 if (hashcmp(one
->sha1
, two
->sha1
)) {
1804 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1808 if ((!fill_mmfile(&mf
, one
) && mmfile_is_binary(&mf
)) ||
1809 (!fill_mmfile(&mf
, two
) && mmfile_is_binary(&mf
)))
1812 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1814 abbrev
, sha1_to_hex(one
->sha1
),
1815 abbrev
, sha1_to_hex(two
->sha1
));
1816 if (one
->mode
== two
->mode
)
1817 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1818 " %06o", one
->mode
);
1819 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1824 xfrm_msg
= len
? msg
: NULL
;
1827 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1828 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1829 /* a filepair that changes between file and symlink
1830 * needs to be split into deletion and creation.
1832 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1833 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1835 null
= alloc_filespec(one
->path
);
1836 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1840 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1847 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1848 struct diffstat_t
*diffstat
)
1852 int complete_rewrite
= 0;
1854 if (DIFF_PAIR_UNMERGED(p
)) {
1856 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1860 name
= p
->one
->path
;
1861 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1863 diff_fill_sha1_info(p
->one
);
1864 diff_fill_sha1_info(p
->two
);
1866 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1867 complete_rewrite
= 1;
1868 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1871 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1876 if (DIFF_PAIR_UNMERGED(p
)) {
1881 name
= p
->one
->path
;
1882 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1884 diff_fill_sha1_info(p
->one
);
1885 diff_fill_sha1_info(p
->two
);
1887 builtin_checkdiff(name
, other
, p
->one
, p
->two
, o
);
1890 void diff_setup(struct diff_options
*options
)
1892 memset(options
, 0, sizeof(*options
));
1893 options
->line_termination
= '\n';
1894 options
->break_opt
= -1;
1895 options
->rename_limit
= -1;
1896 options
->context
= 3;
1897 options
->msg_sep
= "";
1899 options
->change
= diff_change
;
1900 options
->add_remove
= diff_addremove
;
1901 options
->color_diff
= diff_use_color_default
;
1902 options
->detect_rename
= diff_detect_rename_default
;
1905 int diff_setup_done(struct diff_options
*options
)
1909 if (options
->output_format
& DIFF_FORMAT_NAME
)
1911 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
1913 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
1915 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
1918 die("--name-only, --name-status, --check and -s are mutually exclusive");
1920 if (options
->find_copies_harder
)
1921 options
->detect_rename
= DIFF_DETECT_COPY
;
1923 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1924 DIFF_FORMAT_NAME_STATUS
|
1925 DIFF_FORMAT_CHECKDIFF
|
1926 DIFF_FORMAT_NO_OUTPUT
))
1927 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1928 DIFF_FORMAT_NUMSTAT
|
1929 DIFF_FORMAT_DIFFSTAT
|
1930 DIFF_FORMAT_SHORTSTAT
|
1931 DIFF_FORMAT_SUMMARY
|
1935 * These cases always need recursive; we do not drop caller-supplied
1936 * recursive bits for other formats here.
1938 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1939 DIFF_FORMAT_NUMSTAT
|
1940 DIFF_FORMAT_DIFFSTAT
|
1941 DIFF_FORMAT_SHORTSTAT
|
1942 DIFF_FORMAT_SUMMARY
|
1943 DIFF_FORMAT_CHECKDIFF
))
1944 options
->recursive
= 1;
1946 * Also pickaxe would not work very well if you do not say recursive
1948 if (options
->pickaxe
)
1949 options
->recursive
= 1;
1951 if (options
->detect_rename
&& options
->rename_limit
< 0)
1952 options
->rename_limit
= diff_rename_limit_default
;
1953 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1955 /* read-cache does not die even when it fails
1956 * so it is safe for us to do this here. Also
1957 * it does not smudge active_cache or active_nr
1958 * when it fails, so we do not have to worry about
1959 * cleaning it up ourselves either.
1963 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1965 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1966 options
->abbrev
= 40; /* full */
1969 * It does not make sense to show the first hit we happened
1970 * to have found. It does not make sense not to return with
1971 * exit code in such a case either.
1973 if (options
->quiet
) {
1974 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1975 options
->exit_with_status
= 1;
1979 * If we postprocess in diffcore, we cannot simply return
1980 * upon the first hit. We need to run diff as usual.
1982 if (options
->pickaxe
|| options
->filter
)
1988 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1998 if (c
== arg_short
) {
2002 if (val
&& isdigit(c
)) {
2004 int n
= strtoul(arg
, &end
, 10);
2015 eq
= strchr(arg
, '=');
2020 if (!len
|| strncmp(arg
, arg_long
, len
))
2025 if (!isdigit(*++eq
))
2027 n
= strtoul(eq
, &end
, 10);
2035 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2037 const char *arg
= av
[0];
2038 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2039 options
->output_format
|= DIFF_FORMAT_PATCH
;
2040 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2041 options
->output_format
|= DIFF_FORMAT_PATCH
;
2042 else if (!strcmp(arg
, "--raw"))
2043 options
->output_format
|= DIFF_FORMAT_RAW
;
2044 else if (!strcmp(arg
, "--patch-with-raw")) {
2045 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2047 else if (!strcmp(arg
, "--numstat")) {
2048 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2050 else if (!strcmp(arg
, "--shortstat")) {
2051 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2053 else if (!prefixcmp(arg
, "--stat")) {
2055 int width
= options
->stat_width
;
2056 int name_width
= options
->stat_name_width
;
2062 if (!prefixcmp(arg
, "-width="))
2063 width
= strtoul(arg
+ 7, &end
, 10);
2064 else if (!prefixcmp(arg
, "-name-width="))
2065 name_width
= strtoul(arg
+ 12, &end
, 10);
2068 width
= strtoul(arg
+1, &end
, 10);
2070 name_width
= strtoul(end
+1, &end
, 10);
2073 /* Important! This checks all the error cases! */
2076 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2077 options
->stat_name_width
= name_width
;
2078 options
->stat_width
= width
;
2080 else if (!strcmp(arg
, "--check"))
2081 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2082 else if (!strcmp(arg
, "--summary"))
2083 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2084 else if (!strcmp(arg
, "--patch-with-stat")) {
2085 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2087 else if (!strcmp(arg
, "-z"))
2088 options
->line_termination
= 0;
2089 else if (!prefixcmp(arg
, "-l"))
2090 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2091 else if (!strcmp(arg
, "--full-index"))
2092 options
->full_index
= 1;
2093 else if (!strcmp(arg
, "--binary")) {
2094 options
->output_format
|= DIFF_FORMAT_PATCH
;
2095 options
->binary
= 1;
2097 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
2100 else if (!strcmp(arg
, "--name-only"))
2101 options
->output_format
|= DIFF_FORMAT_NAME
;
2102 else if (!strcmp(arg
, "--name-status"))
2103 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2104 else if (!strcmp(arg
, "-R"))
2105 options
->reverse_diff
= 1;
2106 else if (!prefixcmp(arg
, "-S"))
2107 options
->pickaxe
= arg
+ 2;
2108 else if (!strcmp(arg
, "-s")) {
2109 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2111 else if (!prefixcmp(arg
, "-O"))
2112 options
->orderfile
= arg
+ 2;
2113 else if (!prefixcmp(arg
, "--diff-filter="))
2114 options
->filter
= arg
+ 14;
2115 else if (!strcmp(arg
, "--pickaxe-all"))
2116 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2117 else if (!strcmp(arg
, "--pickaxe-regex"))
2118 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2119 else if (!prefixcmp(arg
, "-B")) {
2120 if ((options
->break_opt
=
2121 diff_scoreopt_parse(arg
)) == -1)
2124 else if (!prefixcmp(arg
, "-M")) {
2125 if ((options
->rename_score
=
2126 diff_scoreopt_parse(arg
)) == -1)
2128 options
->detect_rename
= DIFF_DETECT_RENAME
;
2130 else if (!prefixcmp(arg
, "-C")) {
2131 if ((options
->rename_score
=
2132 diff_scoreopt_parse(arg
)) == -1)
2134 options
->detect_rename
= DIFF_DETECT_COPY
;
2136 else if (!strcmp(arg
, "--find-copies-harder"))
2137 options
->find_copies_harder
= 1;
2138 else if (!strcmp(arg
, "--abbrev"))
2139 options
->abbrev
= DEFAULT_ABBREV
;
2140 else if (!prefixcmp(arg
, "--abbrev=")) {
2141 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2142 if (options
->abbrev
< MINIMUM_ABBREV
)
2143 options
->abbrev
= MINIMUM_ABBREV
;
2144 else if (40 < options
->abbrev
)
2145 options
->abbrev
= 40;
2147 else if (!strcmp(arg
, "--color"))
2148 options
->color_diff
= 1;
2149 else if (!strcmp(arg
, "--no-color"))
2150 options
->color_diff
= 0;
2151 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2152 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2153 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2154 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2155 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2156 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2157 else if (!strcmp(arg
, "--color-words"))
2158 options
->color_diff
= options
->color_diff_words
= 1;
2159 else if (!strcmp(arg
, "--no-renames"))
2160 options
->detect_rename
= 0;
2161 else if (!strcmp(arg
, "--exit-code"))
2162 options
->exit_with_status
= 1;
2163 else if (!strcmp(arg
, "--quiet"))
2170 static int parse_num(const char **cp_p
)
2172 unsigned long num
, scale
;
2174 const char *cp
= *cp_p
;
2181 if ( !dot
&& ch
== '.' ) {
2184 } else if ( ch
== '%' ) {
2185 scale
= dot
? scale
*100 : 100;
2186 cp
++; /* % is always at the end */
2188 } else if ( ch
>= '0' && ch
<= '9' ) {
2189 if ( scale
< 100000 ) {
2191 num
= (num
*10) + (ch
-'0');
2200 /* user says num divided by scale and we say internally that
2201 * is MAX_SCORE * num / scale.
2203 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2206 int diff_scoreopt_parse(const char *opt
)
2208 int opt1
, opt2
, cmd
;
2213 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2214 return -1; /* that is not a -M, -C nor -B option */
2216 opt1
= parse_num(&opt
);
2222 else if (*opt
!= '/')
2223 return -1; /* we expect -B80/99 or -B80 */
2226 opt2
= parse_num(&opt
);
2231 return opt1
| (opt2
<< 16);
2234 struct diff_queue_struct diff_queued_diff
;
2236 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2238 if (queue
->alloc
<= queue
->nr
) {
2239 queue
->alloc
= alloc_nr(queue
->alloc
);
2240 queue
->queue
= xrealloc(queue
->queue
,
2241 sizeof(dp
) * queue
->alloc
);
2243 queue
->queue
[queue
->nr
++] = dp
;
2246 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2247 struct diff_filespec
*one
,
2248 struct diff_filespec
*two
)
2250 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2258 void diff_free_filepair(struct diff_filepair
*p
)
2260 diff_free_filespec_data(p
->one
);
2261 diff_free_filespec_data(p
->two
);
2267 /* This is different from find_unique_abbrev() in that
2268 * it stuffs the result with dots for alignment.
2270 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2275 return sha1_to_hex(sha1
);
2277 abbrev
= find_unique_abbrev(sha1
, len
);
2279 return sha1_to_hex(sha1
);
2280 abblen
= strlen(abbrev
);
2282 static char hex
[41];
2283 if (len
< abblen
&& abblen
<= len
+ 2)
2284 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2286 sprintf(hex
, "%s...", abbrev
);
2289 return sha1_to_hex(sha1
);
2292 static void diff_flush_raw(struct diff_filepair
*p
,
2293 struct diff_options
*options
)
2297 int abbrev
= options
->abbrev
;
2298 const char *path_one
, *path_two
;
2299 int inter_name_termination
= '\t';
2300 int line_termination
= options
->line_termination
;
2302 if (!line_termination
)
2303 inter_name_termination
= 0;
2305 path_one
= p
->one
->path
;
2306 path_two
= p
->two
->path
;
2307 if (line_termination
) {
2308 path_one
= quote_one(path_one
);
2309 path_two
= quote_one(path_two
);
2313 sprintf(status
, "%c%03d", p
->status
,
2314 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2316 status
[0] = p
->status
;
2319 switch (p
->status
) {
2320 case DIFF_STATUS_COPIED
:
2321 case DIFF_STATUS_RENAMED
:
2324 case DIFF_STATUS_ADDED
:
2325 case DIFF_STATUS_DELETED
:
2332 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2333 printf(":%06o %06o %s ",
2334 p
->one
->mode
, p
->two
->mode
,
2335 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2337 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2339 printf("%s%c%s", status
, inter_name_termination
, path_one
);
2341 printf("%c%s", inter_name_termination
, path_two
);
2342 putchar(line_termination
);
2343 if (path_one
!= p
->one
->path
)
2344 free((void*)path_one
);
2345 if (path_two
!= p
->two
->path
)
2346 free((void*)path_two
);
2349 static void diff_flush_name(struct diff_filepair
*p
, struct diff_options
*opt
)
2351 char *path
= p
->two
->path
;
2353 if (opt
->line_termination
)
2354 path
= quote_one(p
->two
->path
);
2355 printf("%s%c", path
, opt
->line_termination
);
2356 if (p
->two
->path
!= path
)
2360 int diff_unmodified_pair(struct diff_filepair
*p
)
2362 /* This function is written stricter than necessary to support
2363 * the currently implemented transformers, but the idea is to
2364 * let transformers to produce diff_filepairs any way they want,
2365 * and filter and clean them up here before producing the output.
2367 struct diff_filespec
*one
, *two
;
2369 if (DIFF_PAIR_UNMERGED(p
))
2370 return 0; /* unmerged is interesting */
2375 /* deletion, addition, mode or type change
2376 * and rename are all interesting.
2378 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2379 DIFF_PAIR_MODE_CHANGED(p
) ||
2380 strcmp(one
->path
, two
->path
))
2383 /* both are valid and point at the same path. that is, we are
2384 * dealing with a change.
2386 if (one
->sha1_valid
&& two
->sha1_valid
&&
2387 !hashcmp(one
->sha1
, two
->sha1
))
2388 return 1; /* no change */
2389 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2390 return 1; /* both look at the same file on the filesystem. */
2394 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2396 if (diff_unmodified_pair(p
))
2399 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2400 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2401 return; /* no tree diffs in patch format */
2406 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2407 struct diffstat_t
*diffstat
)
2409 if (diff_unmodified_pair(p
))
2412 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2413 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2414 return; /* no tree diffs in patch format */
2416 run_diffstat(p
, o
, diffstat
);
2419 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2420 struct diff_options
*o
)
2422 if (diff_unmodified_pair(p
))
2425 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2426 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2427 return; /* no tree diffs in patch format */
2429 run_checkdiff(p
, o
);
2432 int diff_queue_is_empty(void)
2434 struct diff_queue_struct
*q
= &diff_queued_diff
;
2436 for (i
= 0; i
< q
->nr
; i
++)
2437 if (!diff_unmodified_pair(q
->queue
[i
]))
2443 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2445 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2448 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2450 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2451 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2453 s
->size
, s
->xfrm_flags
);
2456 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2458 diff_debug_filespec(p
->one
, i
, "one");
2459 diff_debug_filespec(p
->two
, i
, "two");
2460 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2461 p
->score
, p
->status
? p
->status
: '?',
2462 p
->source_stays
, p
->broken_pair
);
2465 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2469 fprintf(stderr
, "%s\n", msg
);
2470 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2471 for (i
= 0; i
< q
->nr
; i
++) {
2472 struct diff_filepair
*p
= q
->queue
[i
];
2473 diff_debug_filepair(p
, i
);
2478 static void diff_resolve_rename_copy(void)
2481 struct diff_filepair
*p
, *pp
;
2482 struct diff_queue_struct
*q
= &diff_queued_diff
;
2484 diff_debug_queue("resolve-rename-copy", q
);
2486 for (i
= 0; i
< q
->nr
; i
++) {
2488 p
->status
= 0; /* undecided */
2489 if (DIFF_PAIR_UNMERGED(p
))
2490 p
->status
= DIFF_STATUS_UNMERGED
;
2491 else if (!DIFF_FILE_VALID(p
->one
))
2492 p
->status
= DIFF_STATUS_ADDED
;
2493 else if (!DIFF_FILE_VALID(p
->two
))
2494 p
->status
= DIFF_STATUS_DELETED
;
2495 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2496 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2498 /* from this point on, we are dealing with a pair
2499 * whose both sides are valid and of the same type, i.e.
2500 * either in-place edit or rename/copy edit.
2502 else if (DIFF_PAIR_RENAME(p
)) {
2503 if (p
->source_stays
) {
2504 p
->status
= DIFF_STATUS_COPIED
;
2507 /* See if there is some other filepair that
2508 * copies from the same source as us. If so
2509 * we are a copy. Otherwise we are either a
2510 * copy if the path stays, or a rename if it
2511 * does not, but we already handled "stays" case.
2513 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2515 if (strcmp(pp
->one
->path
, p
->one
->path
))
2516 continue; /* not us */
2517 if (!DIFF_PAIR_RENAME(pp
))
2518 continue; /* not a rename/copy */
2519 /* pp is a rename/copy from the same source */
2520 p
->status
= DIFF_STATUS_COPIED
;
2524 p
->status
= DIFF_STATUS_RENAMED
;
2526 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2527 p
->one
->mode
!= p
->two
->mode
||
2528 is_null_sha1(p
->one
->sha1
))
2529 p
->status
= DIFF_STATUS_MODIFIED
;
2531 /* This is a "no-change" entry and should not
2532 * happen anymore, but prepare for broken callers.
2534 error("feeding unmodified %s to diffcore",
2536 p
->status
= DIFF_STATUS_UNKNOWN
;
2539 diff_debug_queue("resolve-rename-copy done", q
);
2542 static int check_pair_status(struct diff_filepair
*p
)
2544 switch (p
->status
) {
2545 case DIFF_STATUS_UNKNOWN
:
2548 die("internal error in diff-resolve-rename-copy");
2554 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2556 int fmt
= opt
->output_format
;
2558 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2559 diff_flush_checkdiff(p
, opt
);
2560 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2561 diff_flush_raw(p
, opt
);
2562 else if (fmt
& DIFF_FORMAT_NAME
)
2563 diff_flush_name(p
, opt
);
2566 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2568 char *name
= quote_one(fs
->path
);
2570 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, name
);
2572 printf(" %s %s\n", newdelete
, name
);
2577 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2579 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2581 char *name
= quote_one(p
->two
->path
);
2582 printf(" mode change %06o => %06o %s\n",
2583 p
->one
->mode
, p
->two
->mode
, name
);
2587 printf(" mode change %06o => %06o\n",
2588 p
->one
->mode
, p
->two
->mode
);
2592 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2594 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
2596 printf(" %s %s (%d%%)\n", renamecopy
, names
,
2597 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2599 show_mode_change(p
, 0);
2602 static void diff_summary(struct diff_filepair
*p
)
2605 case DIFF_STATUS_DELETED
:
2606 show_file_mode_name("delete", p
->one
);
2608 case DIFF_STATUS_ADDED
:
2609 show_file_mode_name("create", p
->two
);
2611 case DIFF_STATUS_COPIED
:
2612 show_rename_copy("copy", p
);
2614 case DIFF_STATUS_RENAMED
:
2615 show_rename_copy("rename", p
);
2619 char *name
= quote_one(p
->two
->path
);
2620 printf(" rewrite %s (%d%%)\n", name
,
2621 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2623 show_mode_change(p
, 0);
2624 } else show_mode_change(p
, 1);
2630 struct xdiff_emit_state xm
;
2635 static int remove_space(char *line
, int len
)
2641 for (i
= 0; i
< len
; i
++)
2642 if (!isspace((c
= line
[i
])))
2648 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2650 struct patch_id_t
*data
= priv
;
2653 /* Ignore line numbers when computing the SHA1 of the patch */
2654 if (!prefixcmp(line
, "@@ -"))
2657 new_len
= remove_space(line
, len
);
2659 SHA1_Update(data
->ctx
, line
, new_len
);
2660 data
->patchlen
+= new_len
;
2663 /* returns 0 upon success, and writes result into sha1 */
2664 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2666 struct diff_queue_struct
*q
= &diff_queued_diff
;
2669 struct patch_id_t data
;
2670 char buffer
[PATH_MAX
* 4 + 20];
2673 memset(&data
, 0, sizeof(struct patch_id_t
));
2675 data
.xm
.consume
= patch_id_consume
;
2677 for (i
= 0; i
< q
->nr
; i
++) {
2682 struct diff_filepair
*p
= q
->queue
[i
];
2686 return error("internal diff status error");
2687 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2689 if (diff_unmodified_pair(p
))
2691 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2692 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2694 if (DIFF_PAIR_UNMERGED(p
))
2697 diff_fill_sha1_info(p
->one
);
2698 diff_fill_sha1_info(p
->two
);
2699 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2700 fill_mmfile(&mf2
, p
->two
) < 0)
2701 return error("unable to read files to diff");
2703 /* Maybe hash p->two? into the patch id? */
2704 if (mmfile_is_binary(&mf2
))
2707 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2708 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2709 if (p
->one
->mode
== 0)
2710 len1
= snprintf(buffer
, sizeof(buffer
),
2711 "diff--gita/%.*sb/%.*s"
2718 len2
, p
->two
->path
);
2719 else if (p
->two
->mode
== 0)
2720 len1
= snprintf(buffer
, sizeof(buffer
),
2721 "diff--gita/%.*sb/%.*s"
2722 "deletedfilemode%06o"
2728 len1
, p
->one
->path
);
2730 len1
= snprintf(buffer
, sizeof(buffer
),
2731 "diff--gita/%.*sb/%.*s"
2737 len2
, p
->two
->path
);
2738 SHA1_Update(&ctx
, buffer
, len1
);
2740 xpp
.flags
= XDF_NEED_MINIMAL
;
2742 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2743 ecb
.outf
= xdiff_outf
;
2745 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2748 SHA1_Final(sha1
, &ctx
);
2752 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2754 struct diff_queue_struct
*q
= &diff_queued_diff
;
2756 int result
= diff_get_patch_id(options
, sha1
);
2758 for (i
= 0; i
< q
->nr
; i
++)
2759 diff_free_filepair(q
->queue
[i
]);
2763 q
->nr
= q
->alloc
= 0;
2768 static int is_summary_empty(const struct diff_queue_struct
*q
)
2772 for (i
= 0; i
< q
->nr
; i
++) {
2773 const struct diff_filepair
*p
= q
->queue
[i
];
2775 switch (p
->status
) {
2776 case DIFF_STATUS_DELETED
:
2777 case DIFF_STATUS_ADDED
:
2778 case DIFF_STATUS_COPIED
:
2779 case DIFF_STATUS_RENAMED
:
2784 if (p
->one
->mode
&& p
->two
->mode
&&
2785 p
->one
->mode
!= p
->two
->mode
)
2793 void diff_flush(struct diff_options
*options
)
2795 struct diff_queue_struct
*q
= &diff_queued_diff
;
2796 int i
, output_format
= options
->output_format
;
2800 * Order: raw, stat, summary, patch
2801 * or: name/name-status/checkdiff (other bits clear)
2806 if (output_format
& (DIFF_FORMAT_RAW
|
2808 DIFF_FORMAT_NAME_STATUS
|
2809 DIFF_FORMAT_CHECKDIFF
)) {
2810 for (i
= 0; i
< q
->nr
; i
++) {
2811 struct diff_filepair
*p
= q
->queue
[i
];
2812 if (check_pair_status(p
))
2813 flush_one_pair(p
, options
);
2818 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
2819 struct diffstat_t diffstat
;
2821 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2822 diffstat
.xm
.consume
= diffstat_consume
;
2823 for (i
= 0; i
< q
->nr
; i
++) {
2824 struct diff_filepair
*p
= q
->queue
[i
];
2825 if (check_pair_status(p
))
2826 diff_flush_stat(p
, options
, &diffstat
);
2828 if (output_format
& DIFF_FORMAT_NUMSTAT
)
2829 show_numstat(&diffstat
, options
);
2830 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
2831 show_stats(&diffstat
, options
);
2832 else if (output_format
& DIFF_FORMAT_SHORTSTAT
)
2833 show_shortstats(&diffstat
);
2837 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2838 for (i
= 0; i
< q
->nr
; i
++)
2839 diff_summary(q
->queue
[i
]);
2843 if (output_format
& DIFF_FORMAT_PATCH
) {
2845 if (options
->stat_sep
) {
2846 /* attach patch instead of inline */
2847 fputs(options
->stat_sep
, stdout
);
2849 putchar(options
->line_termination
);
2853 for (i
= 0; i
< q
->nr
; i
++) {
2854 struct diff_filepair
*p
= q
->queue
[i
];
2855 if (check_pair_status(p
))
2856 diff_flush_patch(p
, options
);
2860 if (output_format
& DIFF_FORMAT_CALLBACK
)
2861 options
->format_callback(q
, options
, options
->format_callback_data
);
2863 for (i
= 0; i
< q
->nr
; i
++)
2864 diff_free_filepair(q
->queue
[i
]);
2868 q
->nr
= q
->alloc
= 0;
2871 static void diffcore_apply_filter(const char *filter
)
2874 struct diff_queue_struct
*q
= &diff_queued_diff
;
2875 struct diff_queue_struct outq
;
2877 outq
.nr
= outq
.alloc
= 0;
2882 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2884 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2885 struct diff_filepair
*p
= q
->queue
[i
];
2886 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2888 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2890 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2891 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2892 strchr(filter
, p
->status
)))
2898 /* otherwise we will clear the whole queue
2899 * by copying the empty outq at the end of this
2900 * function, but first clear the current entries
2903 for (i
= 0; i
< q
->nr
; i
++)
2904 diff_free_filepair(q
->queue
[i
]);
2907 /* Only the matching ones */
2908 for (i
= 0; i
< q
->nr
; i
++) {
2909 struct diff_filepair
*p
= q
->queue
[i
];
2911 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2913 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2915 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2916 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2917 strchr(filter
, p
->status
)))
2920 diff_free_filepair(p
);
2927 void diffcore_std(struct diff_options
*options
)
2931 if (options
->break_opt
!= -1)
2932 diffcore_break(options
->break_opt
);
2933 if (options
->detect_rename
)
2934 diffcore_rename(options
);
2935 if (options
->break_opt
!= -1)
2936 diffcore_merge_broken();
2937 if (options
->pickaxe
)
2938 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2939 if (options
->orderfile
)
2940 diffcore_order(options
->orderfile
);
2941 diff_resolve_rename_copy();
2942 diffcore_apply_filter(options
->filter
);
2944 options
->has_changes
= !!diff_queued_diff
.nr
;
2948 void diff_addremove(struct diff_options
*options
,
2949 int addremove
, unsigned mode
,
2950 const unsigned char *sha1
,
2951 const char *base
, const char *path
)
2953 char concatpath
[PATH_MAX
];
2954 struct diff_filespec
*one
, *two
;
2956 /* This may look odd, but it is a preparation for
2957 * feeding "there are unchanged files which should
2958 * not produce diffs, but when you are doing copy
2959 * detection you would need them, so here they are"
2960 * entries to the diff-core. They will be prefixed
2961 * with something like '=' or '*' (I haven't decided
2962 * which but should not make any difference).
2963 * Feeding the same new and old to diff_change()
2964 * also has the same effect.
2965 * Before the final output happens, they are pruned after
2966 * merged into rename/copy pairs as appropriate.
2968 if (options
->reverse_diff
)
2969 addremove
= (addremove
== '+' ? '-' :
2970 addremove
== '-' ? '+' : addremove
);
2972 if (!path
) path
= "";
2973 sprintf(concatpath
, "%s%s", base
, path
);
2974 one
= alloc_filespec(concatpath
);
2975 two
= alloc_filespec(concatpath
);
2977 if (addremove
!= '+')
2978 fill_filespec(one
, sha1
, mode
);
2979 if (addremove
!= '-')
2980 fill_filespec(two
, sha1
, mode
);
2982 diff_queue(&diff_queued_diff
, one
, two
);
2983 options
->has_changes
= 1;
2986 void diff_change(struct diff_options
*options
,
2987 unsigned old_mode
, unsigned new_mode
,
2988 const unsigned char *old_sha1
,
2989 const unsigned char *new_sha1
,
2990 const char *base
, const char *path
)
2992 char concatpath
[PATH_MAX
];
2993 struct diff_filespec
*one
, *two
;
2995 if (options
->reverse_diff
) {
2997 const unsigned char *tmp_c
;
2998 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2999 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3001 if (!path
) path
= "";
3002 sprintf(concatpath
, "%s%s", base
, path
);
3003 one
= alloc_filespec(concatpath
);
3004 two
= alloc_filespec(concatpath
);
3005 fill_filespec(one
, old_sha1
, old_mode
);
3006 fill_filespec(two
, new_sha1
, new_mode
);
3008 diff_queue(&diff_queued_diff
, one
, two
);
3009 options
->has_changes
= 1;
3012 void diff_unmerge(struct diff_options
*options
,
3014 unsigned mode
, const unsigned char *sha1
)
3016 struct diff_filespec
*one
, *two
;
3017 one
= alloc_filespec(path
);
3018 two
= alloc_filespec(path
);
3019 fill_filespec(one
, sha1
, mode
);
3020 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;