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 (!strncmp(var
, "diff.color.", 11) || !strncmp(var
, "color.diff.", 11)) {
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
)
189 int ch
, nl_just_seen
= 1;
201 printf("\n\\ No newline at end of file\n");
204 static void emit_rewrite_diff(const char *name_a
,
206 struct diff_filespec
*one
,
207 struct diff_filespec
*two
)
210 const char *name_a_tab
, *name_b_tab
;
212 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
213 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
215 diff_populate_filespec(one
, 0);
216 diff_populate_filespec(two
, 0);
217 lc_a
= count_lines(one
->data
, one
->size
);
218 lc_b
= count_lines(two
->data
, two
->size
);
219 printf("--- a/%s%s\n+++ b/%s%s\n@@ -",
222 print_line_count(lc_a
);
224 print_line_count(lc_b
);
227 copy_file('-', one
->data
, one
->size
);
229 copy_file('+', two
->data
, two
->size
);
232 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
234 if (!DIFF_FILE_VALID(one
)) {
235 mf
->ptr
= (char *)""; /* does not matter */
239 else if (diff_populate_filespec(one
, 0))
242 mf
->size
= one
->size
;
246 struct diff_words_buffer
{
249 long current
; /* output pointer */
250 int suppressed_newline
;
253 static void diff_words_append(char *line
, unsigned long len
,
254 struct diff_words_buffer
*buffer
)
256 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
257 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
258 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
262 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
263 buffer
->text
.size
+= len
;
266 struct diff_words_data
{
267 struct xdiff_emit_state xm
;
268 struct diff_words_buffer minus
, plus
;
271 static void print_word(struct diff_words_buffer
*buffer
, int len
, int color
,
272 int suppress_newline
)
280 ptr
= buffer
->text
.ptr
+ buffer
->current
;
281 buffer
->current
+= len
;
283 if (ptr
[len
- 1] == '\n') {
288 fputs(diff_get_color(1, color
), stdout
);
289 fwrite(ptr
, len
, 1, stdout
);
290 fputs(diff_get_color(1, DIFF_RESET
), stdout
);
293 if (suppress_newline
)
294 buffer
->suppressed_newline
= 1;
300 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
302 struct diff_words_data
*diff_words
= priv
;
304 if (diff_words
->minus
.suppressed_newline
) {
307 diff_words
->minus
.suppressed_newline
= 0;
313 print_word(&diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
316 print_word(&diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
319 print_word(&diff_words
->plus
, len
, DIFF_PLAIN
, 0);
320 diff_words
->minus
.current
+= len
;
325 /* this executes the word diff on the accumulated buffers */
326 static void diff_words_show(struct diff_words_data
*diff_words
)
331 mmfile_t minus
, plus
;
334 minus
.size
= diff_words
->minus
.text
.size
;
335 minus
.ptr
= xmalloc(minus
.size
);
336 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
337 for (i
= 0; i
< minus
.size
; i
++)
338 if (isspace(minus
.ptr
[i
]))
340 diff_words
->minus
.current
= 0;
342 plus
.size
= diff_words
->plus
.text
.size
;
343 plus
.ptr
= xmalloc(plus
.size
);
344 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
345 for (i
= 0; i
< plus
.size
; i
++)
346 if (isspace(plus
.ptr
[i
]))
348 diff_words
->plus
.current
= 0;
350 xpp
.flags
= XDF_NEED_MINIMAL
;
351 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
353 ecb
.outf
= xdiff_outf
;
354 ecb
.priv
= diff_words
;
355 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
356 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
360 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
362 if (diff_words
->minus
.suppressed_newline
) {
364 diff_words
->minus
.suppressed_newline
= 0;
368 struct emit_callback
{
369 struct xdiff_emit_state xm
;
370 int nparents
, color_diff
;
371 const char **label_path
;
372 struct diff_words_data
*diff_words
;
375 static void free_diff_words_data(struct emit_callback
*ecbdata
)
377 if (ecbdata
->diff_words
) {
379 if (ecbdata
->diff_words
->minus
.text
.size
||
380 ecbdata
->diff_words
->plus
.text
.size
)
381 diff_words_show(ecbdata
->diff_words
);
383 if (ecbdata
->diff_words
->minus
.text
.ptr
)
384 free (ecbdata
->diff_words
->minus
.text
.ptr
);
385 if (ecbdata
->diff_words
->plus
.text
.ptr
)
386 free (ecbdata
->diff_words
->plus
.text
.ptr
);
387 free(ecbdata
->diff_words
);
388 ecbdata
->diff_words
= NULL
;
392 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
395 return diff_colors
[ix
];
399 static void emit_line(const char *set
, const char *reset
, const char *line
, int len
)
401 if (len
> 0 && line
[len
-1] == '\n')
404 fwrite(line
, len
, 1, stdout
);
408 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
410 int col0
= ecbdata
->nparents
;
411 int last_tab_in_indent
= -1;
412 int last_space_in_indent
= -1;
415 int need_highlight_leading_space
= 0;
416 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
417 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
420 emit_line(set
, reset
, line
, len
);
424 /* The line is a newly added line. Does it have funny leading
425 * whitespaces? In indent, SP should never precede a TAB.
427 for (i
= col0
; i
< len
; i
++) {
428 if (line
[i
] == '\t') {
429 last_tab_in_indent
= i
;
430 if (0 <= last_space_in_indent
)
431 need_highlight_leading_space
= 1;
433 else if (line
[i
] == ' ')
434 last_space_in_indent
= i
;
439 fwrite(line
, col0
, 1, stdout
);
440 fputs(reset
, stdout
);
441 if (((i
== len
) || line
[i
] == '\n') && i
!= col0
) {
442 /* The whole line was indent */
443 emit_line(ws
, reset
, line
+ col0
, len
- col0
);
447 if (need_highlight_leading_space
) {
448 while (i
< last_tab_in_indent
) {
449 if (line
[i
] == ' ') {
452 fputs(reset
, stdout
);
460 if (line
[tail
] == '\n' && i
< tail
)
463 if (!isspace(line
[tail
]))
467 if ((i
< tail
&& line
[tail
+ 1] != '\n')) {
468 /* This has whitespace between tail+1..len */
470 fwrite(line
+ i
, tail
- i
+ 1, 1, stdout
);
471 fputs(reset
, stdout
);
472 emit_line(ws
, reset
, line
+ tail
+ 1, len
- tail
- 1);
475 emit_line(set
, reset
, line
+ i
, len
- i
);
478 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
482 struct emit_callback
*ecbdata
= priv
;
483 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
484 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
486 if (ecbdata
->label_path
[0]) {
487 const char *name_a_tab
, *name_b_tab
;
489 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
490 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
492 printf("%s--- %s%s%s\n",
493 set
, ecbdata
->label_path
[0], reset
, name_a_tab
);
494 printf("%s+++ %s%s%s\n",
495 set
, ecbdata
->label_path
[1], reset
, name_b_tab
);
496 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
499 /* This is not really necessary for now because
500 * this codepath only deals with two-way diffs.
502 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
504 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
505 ecbdata
->nparents
= i
- 1;
506 emit_line(diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
511 if (len
< ecbdata
->nparents
) {
513 emit_line(reset
, reset
, line
, len
);
518 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
519 /* fall back to normal diff */
520 free_diff_words_data(ecbdata
);
521 if (ecbdata
->diff_words
) {
522 if (line
[0] == '-') {
523 diff_words_append(line
, len
,
524 &ecbdata
->diff_words
->minus
);
526 } else if (line
[0] == '+') {
527 diff_words_append(line
, len
,
528 &ecbdata
->diff_words
->plus
);
531 if (ecbdata
->diff_words
->minus
.text
.size
||
532 ecbdata
->diff_words
->plus
.text
.size
)
533 diff_words_show(ecbdata
->diff_words
);
536 emit_line(set
, reset
, line
, len
);
539 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
541 color
= DIFF_FILE_OLD
;
542 else if (line
[i
] == '+')
543 color
= DIFF_FILE_NEW
;
546 if (color
!= DIFF_FILE_NEW
) {
547 emit_line(diff_get_color(ecbdata
->color_diff
, color
),
551 emit_add_line(reset
, ecbdata
, line
, len
);
554 static char *pprint_rename(const char *a
, const char *b
)
559 int pfx_length
, sfx_length
;
560 int len_a
= strlen(a
);
561 int len_b
= strlen(b
);
563 /* Find common prefix */
565 while (*old
&& *new && *old
== *new) {
567 pfx_length
= old
- a
+ 1;
572 /* Find common suffix */
576 while (a
<= old
&& b
<= new && *old
== *new) {
578 sfx_length
= len_a
- (old
- a
);
584 * pfx{mid-a => mid-b}sfx
585 * {pfx-a => pfx-b}sfx
586 * pfx{sfx-a => sfx-b}
589 if (pfx_length
+ sfx_length
) {
590 int a_midlen
= len_a
- pfx_length
- sfx_length
;
591 int b_midlen
= len_b
- pfx_length
- sfx_length
;
592 if (a_midlen
< 0) a_midlen
= 0;
593 if (b_midlen
< 0) b_midlen
= 0;
595 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
596 sprintf(name
, "%.*s{%.*s => %.*s}%s",
598 a_midlen
, a
+ pfx_length
,
599 b_midlen
, b
+ pfx_length
,
600 a
+ len_a
- sfx_length
);
603 name
= xmalloc(len_a
+ len_b
+ 5);
604 sprintf(name
, "%s => %s", a
, b
);
610 struct xdiff_emit_state xm
;
614 struct diffstat_file
{
616 unsigned is_unmerged
:1;
617 unsigned is_binary
:1;
618 unsigned is_renamed
:1;
619 unsigned int added
, deleted
;
623 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
627 struct diffstat_file
*x
;
628 x
= xcalloc(sizeof (*x
), 1);
629 if (diffstat
->nr
== diffstat
->alloc
) {
630 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
631 diffstat
->files
= xrealloc(diffstat
->files
,
632 diffstat
->alloc
* sizeof(x
));
634 diffstat
->files
[diffstat
->nr
++] = x
;
636 x
->name
= pprint_rename(name_a
, name_b
);
640 x
->name
= xstrdup(name_a
);
644 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
646 struct diffstat_t
*diffstat
= priv
;
647 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
651 else if (line
[0] == '-')
655 const char mime_boundary_leader
[] = "------------";
657 static int scale_linear(int it
, int width
, int max_change
)
660 * make sure that at least one '-' is printed if there were deletions,
661 * and likewise for '+'.
665 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
668 static void show_name(const char *prefix
, const char *name
, int len
,
669 const char *reset
, const char *set
)
671 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
674 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
684 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
686 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
687 int max_change
= 0, max_len
= 0;
688 int total_files
= data
->nr
;
689 int width
, name_width
;
690 const char *reset
, *set
, *add_c
, *del_c
;
695 width
= options
->stat_width
? options
->stat_width
: 80;
696 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
698 /* Sanity: give at least 5 columns to the graph,
699 * but leave at least 10 columns for the name.
701 if (width
< name_width
+ 15) {
702 if (name_width
<= 25)
703 width
= name_width
+ 15;
705 name_width
= width
- 15;
708 /* Find the longest filename and max number of changes */
709 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
710 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
711 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
712 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
714 for (i
= 0; i
< data
->nr
; i
++) {
715 struct diffstat_file
*file
= data
->files
[i
];
716 int change
= file
->added
+ file
->deleted
;
718 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
720 char *qname
= xmalloc(len
+ 1);
721 quote_c_style(file
->name
, qname
, NULL
, 0);
726 len
= strlen(file
->name
);
730 if (file
->is_binary
|| file
->is_unmerged
)
732 if (max_change
< change
)
736 /* Compute the width of the graph part;
737 * 10 is for one blank at the beginning of the line plus
738 * " | count " between the name and the graph.
740 * From here on, name_width is the width of the name area,
741 * and width is the width of the graph area.
743 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
744 if (width
< (name_width
+ 10) + max_change
)
745 width
= width
- (name_width
+ 10);
749 for (i
= 0; i
< data
->nr
; i
++) {
750 const char *prefix
= "";
751 char *name
= data
->files
[i
]->name
;
752 int added
= data
->files
[i
]->added
;
753 int deleted
= data
->files
[i
]->deleted
;
757 * "scale" the filename
760 name_len
= strlen(name
);
761 if (name_width
< name_len
) {
765 name
+= name_len
- len
;
766 slash
= strchr(name
, '/');
771 if (data
->files
[i
]->is_binary
) {
772 show_name(prefix
, name
, len
, reset
, set
);
774 goto free_diffstat_file
;
776 else if (data
->files
[i
]->is_unmerged
) {
777 show_name(prefix
, name
, len
, reset
, set
);
778 printf(" Unmerged\n");
779 goto free_diffstat_file
;
781 else if (!data
->files
[i
]->is_renamed
&&
782 (added
+ deleted
== 0)) {
784 goto free_diffstat_file
;
788 * scale the add/delete
796 if (width
<= max_change
) {
797 add
= scale_linear(add
, width
, max_change
);
798 del
= scale_linear(del
, width
, max_change
);
801 show_name(prefix
, name
, len
, reset
, set
);
802 printf("%5d ", added
+ deleted
);
803 show_graph('+', add
, add_c
, reset
);
804 show_graph('-', del
, del_c
, reset
);
807 free(data
->files
[i
]->name
);
808 free(data
->files
[i
]);
811 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
812 set
, total_files
, adds
, dels
, reset
);
815 static void show_shortstats(struct diffstat_t
* data
)
817 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
822 for (i
= 0; i
< data
->nr
; i
++) {
823 if (!data
->files
[i
]->is_binary
&&
824 !data
->files
[i
]->is_unmerged
) {
825 int added
= data
->files
[i
]->added
;
826 int deleted
= data
->files
[i
]->deleted
;
827 if (!data
->files
[i
]->is_renamed
&&
828 (added
+ deleted
== 0)) {
835 free(data
->files
[i
]->name
);
836 free(data
->files
[i
]);
840 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
841 total_files
, adds
, dels
);
844 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
848 for (i
= 0; i
< data
->nr
; i
++) {
849 struct diffstat_file
*file
= data
->files
[i
];
854 printf("%d\t%d\t", file
->added
, file
->deleted
);
855 if (options
->line_termination
&&
856 quote_c_style(file
->name
, NULL
, NULL
, 0))
857 quote_c_style(file
->name
, NULL
, stdout
, 0);
859 fputs(file
->name
, stdout
);
860 putchar(options
->line_termination
);
865 struct xdiff_emit_state xm
;
866 const char *filename
;
870 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
872 struct checkdiff_t
*data
= priv
;
874 if (line
[0] == '+') {
877 /* check space before tab */
878 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
881 if (line
[i
- 1] == '\t' && spaces
)
882 printf("%s:%d: space before tab:%.*s\n",
883 data
->filename
, data
->lineno
, (int)len
, line
);
885 /* check white space at line end */
886 if (line
[len
- 1] == '\n')
888 if (isspace(line
[len
- 1]))
889 printf("%s:%d: white space at end: %.*s\n",
890 data
->filename
, data
->lineno
, (int)len
, line
);
893 } else if (line
[0] == ' ')
895 else if (line
[0] == '@') {
896 char *plus
= strchr(line
, '+');
898 data
->lineno
= strtol(plus
, NULL
, 10);
904 static unsigned char *deflate_it(char *data
,
906 unsigned long *result_size
)
909 unsigned char *deflated
;
912 memset(&stream
, 0, sizeof(stream
));
913 deflateInit(&stream
, zlib_compression_level
);
914 bound
= deflateBound(&stream
, size
);
915 deflated
= xmalloc(bound
);
916 stream
.next_out
= deflated
;
917 stream
.avail_out
= bound
;
919 stream
.next_in
= (unsigned char *)data
;
920 stream
.avail_in
= size
;
921 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
924 *result_size
= stream
.total_out
;
928 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
934 unsigned long orig_size
;
935 unsigned long delta_size
;
936 unsigned long deflate_size
;
937 unsigned long data_size
;
939 /* We could do deflated delta, or we could do just deflated two,
940 * whichever is smaller.
943 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
944 if (one
->size
&& two
->size
) {
945 delta
= diff_delta(one
->ptr
, one
->size
,
947 &delta_size
, deflate_size
);
949 void *to_free
= delta
;
950 orig_size
= delta_size
;
951 delta
= deflate_it(delta
, delta_size
, &delta_size
);
956 if (delta
&& delta_size
< deflate_size
) {
957 printf("delta %lu\n", orig_size
);
960 data_size
= delta_size
;
963 printf("literal %lu\n", two
->size
);
966 data_size
= deflate_size
;
969 /* emit data encoded in base85 */
972 int bytes
= (52 < data_size
) ? 52 : data_size
;
976 line
[0] = bytes
+ 'A' - 1;
978 line
[0] = bytes
- 26 + 'a' - 1;
979 encode_85(line
+ 1, cp
, bytes
);
980 cp
= (char *) cp
+ bytes
;
987 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
989 printf("GIT binary patch\n");
990 emit_binary_diff_body(one
, two
);
991 emit_binary_diff_body(two
, one
);
994 #define FIRST_FEW_BYTES 8000
995 static int mmfile_is_binary(mmfile_t
*mf
)
998 if (FIRST_FEW_BYTES
< sz
)
999 sz
= FIRST_FEW_BYTES
;
1000 return !!memchr(mf
->ptr
, 0, sz
);
1003 static void builtin_diff(const char *name_a
,
1005 struct diff_filespec
*one
,
1006 struct diff_filespec
*two
,
1007 const char *xfrm_msg
,
1008 struct diff_options
*o
,
1009 int complete_rewrite
)
1013 char *a_one
, *b_two
;
1014 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
1015 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
1017 a_one
= quote_two("a/", name_a
);
1018 b_two
= quote_two("b/", name_b
);
1019 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1020 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1021 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1022 if (lbl
[0][0] == '/') {
1024 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1025 if (xfrm_msg
&& xfrm_msg
[0])
1026 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1028 else if (lbl
[1][0] == '/') {
1029 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1030 if (xfrm_msg
&& xfrm_msg
[0])
1031 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1034 if (one
->mode
!= two
->mode
) {
1035 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
1036 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
1038 if (xfrm_msg
&& xfrm_msg
[0])
1039 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1041 * we do not run diff between different kind
1044 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1045 goto free_ab_and_return
;
1046 if (complete_rewrite
) {
1047 emit_rewrite_diff(name_a
, name_b
, one
, two
);
1048 goto free_ab_and_return
;
1052 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1053 die("unable to read files to diff");
1055 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
1056 /* Quite common confusing case */
1057 if (mf1
.size
== mf2
.size
&&
1058 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1059 goto free_ab_and_return
;
1061 emit_binary_diff(&mf1
, &mf2
);
1063 printf("Binary files %s and %s differ\n",
1067 /* Crazy xdl interfaces.. */
1068 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1072 struct emit_callback ecbdata
;
1074 memset(&ecbdata
, 0, sizeof(ecbdata
));
1075 ecbdata
.label_path
= lbl
;
1076 ecbdata
.color_diff
= o
->color_diff
;
1077 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1078 xecfg
.ctxlen
= o
->context
;
1079 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1082 else if (!strncmp(diffopts
, "--unified=", 10))
1083 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1084 else if (!strncmp(diffopts
, "-u", 2))
1085 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1086 ecb
.outf
= xdiff_outf
;
1087 ecb
.priv
= &ecbdata
;
1088 ecbdata
.xm
.consume
= fn_out_consume
;
1089 if (o
->color_diff_words
)
1090 ecbdata
.diff_words
=
1091 xcalloc(1, sizeof(struct diff_words_data
));
1092 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1093 if (o
->color_diff_words
)
1094 free_diff_words_data(&ecbdata
);
1103 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1104 struct diff_filespec
*one
,
1105 struct diff_filespec
*two
,
1106 struct diffstat_t
*diffstat
,
1107 struct diff_options
*o
,
1108 int complete_rewrite
)
1111 struct diffstat_file
*data
;
1113 data
= diffstat_add(diffstat
, name_a
, name_b
);
1116 data
->is_unmerged
= 1;
1119 if (complete_rewrite
) {
1120 diff_populate_filespec(one
, 0);
1121 diff_populate_filespec(two
, 0);
1122 data
->deleted
= count_lines(one
->data
, one
->size
);
1123 data
->added
= count_lines(two
->data
, two
->size
);
1126 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1127 die("unable to read files to diff");
1129 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
1130 data
->is_binary
= 1;
1132 /* Crazy xdl interfaces.. */
1137 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1140 ecb
.outf
= xdiff_outf
;
1141 ecb
.priv
= diffstat
;
1142 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1146 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1147 struct diff_filespec
*one
,
1148 struct diff_filespec
*two
)
1151 struct checkdiff_t data
;
1156 memset(&data
, 0, sizeof(data
));
1157 data
.xm
.consume
= checkdiff_consume
;
1158 data
.filename
= name_b
? name_b
: name_a
;
1161 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1162 die("unable to read files to diff");
1164 if (mmfile_is_binary(&mf2
))
1167 /* Crazy xdl interfaces.. */
1172 xpp
.flags
= XDF_NEED_MINIMAL
;
1175 ecb
.outf
= xdiff_outf
;
1177 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1181 struct diff_filespec
*alloc_filespec(const char *path
)
1183 int namelen
= strlen(path
);
1184 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1186 memset(spec
, 0, sizeof(*spec
));
1187 spec
->path
= (char *)(spec
+ 1);
1188 memcpy(spec
->path
, path
, namelen
+1);
1192 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1193 unsigned short mode
)
1196 spec
->mode
= canon_mode(mode
);
1197 hashcpy(spec
->sha1
, sha1
);
1198 spec
->sha1_valid
= !is_null_sha1(sha1
);
1203 * Given a name and sha1 pair, if the dircache tells us the file in
1204 * the work tree has that object contents, return true, so that
1205 * prepare_temp_file() does not have to inflate and extract.
1207 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1209 struct cache_entry
*ce
;
1213 /* We do not read the cache ourselves here, because the
1214 * benchmark with my previous version that always reads cache
1215 * shows that it makes things worse for diff-tree comparing
1216 * two linux-2.6 kernel trees in an already checked out work
1217 * tree. This is because most diff-tree comparisons deal with
1218 * only a small number of files, while reading the cache is
1219 * expensive for a large project, and its cost outweighs the
1220 * savings we get by not inflating the object to a temporary
1221 * file. Practically, this code only helps when we are used
1222 * by diff-cache --cached, which does read the cache before
1228 /* We want to avoid the working directory if our caller
1229 * doesn't need the data in a normal file, this system
1230 * is rather slow with its stat/open/mmap/close syscalls,
1231 * and the object is contained in a pack file. The pack
1232 * is probably already open and will be faster to obtain
1233 * the data through than the working directory. Loose
1234 * objects however would tend to be slower as they need
1235 * to be individually opened and inflated.
1237 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1241 pos
= cache_name_pos(name
, len
);
1244 ce
= active_cache
[pos
];
1245 if ((lstat(name
, &st
) < 0) ||
1246 !S_ISREG(st
.st_mode
) || /* careful! */
1247 ce_match_stat(ce
, &st
, 0) ||
1248 hashcmp(sha1
, ce
->sha1
))
1250 /* we return 1 only when we can stat, it is a regular file,
1251 * stat information matches, and sha1 recorded in the cache
1252 * matches. I.e. we know the file in the work tree really is
1253 * the same as the <name, sha1> pair.
1258 static struct sha1_size_cache
{
1259 unsigned char sha1
[20];
1261 } **sha1_size_cache
;
1262 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
1264 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
1269 struct sha1_size_cache
*e
;
1272 last
= sha1_size_cache_nr
;
1273 while (last
> first
) {
1274 int cmp
, next
= (last
+ first
) >> 1;
1275 e
= sha1_size_cache
[next
];
1276 cmp
= hashcmp(e
->sha1
, sha1
);
1288 /* insert to make it at "first" */
1289 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
1290 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
1291 sha1_size_cache
= xrealloc(sha1_size_cache
,
1292 sha1_size_cache_alloc
*
1293 sizeof(*sha1_size_cache
));
1295 sha1_size_cache_nr
++;
1296 if (first
< sha1_size_cache_nr
)
1297 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
1298 (sha1_size_cache_nr
- first
- 1) *
1299 sizeof(*sha1_size_cache
));
1300 e
= xmalloc(sizeof(struct sha1_size_cache
));
1301 sha1_size_cache
[first
] = e
;
1302 hashcpy(e
->sha1
, sha1
);
1308 * While doing rename detection and pickaxe operation, we may need to
1309 * grab the data for the blob (or file) for our own in-core comparison.
1310 * diff_filespec has data and size fields for this purpose.
1312 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1315 if (!DIFF_FILE_VALID(s
))
1316 die("internal error: asking to populate invalid file.");
1317 if (S_ISDIR(s
->mode
))
1320 if (!use_size_cache
)
1325 if (!s
->sha1_valid
||
1326 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1329 if (lstat(s
->path
, &st
) < 0) {
1330 if (errno
== ENOENT
) {
1334 s
->data
= (char *)"";
1339 s
->size
= st
.st_size
;
1344 if (S_ISLNK(st
.st_mode
)) {
1346 s
->data
= xmalloc(s
->size
);
1348 ret
= readlink(s
->path
, s
->data
, s
->size
);
1355 fd
= open(s
->path
, O_RDONLY
);
1358 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1360 s
->should_munmap
= 1;
1364 struct sha1_size_cache
*e
;
1367 e
= locate_size_cache(s
->sha1
, 1, 0);
1372 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
1373 locate_size_cache(s
->sha1
, 0, s
->size
);
1376 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
1383 void diff_free_filespec_data(struct diff_filespec
*s
)
1387 else if (s
->should_munmap
)
1388 munmap(s
->data
, s
->size
);
1389 s
->should_free
= s
->should_munmap
= 0;
1395 static void prep_temp_blob(struct diff_tempfile
*temp
,
1398 const unsigned char *sha1
,
1403 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1405 die("unable to create temp-file");
1406 if (write_in_full(fd
, blob
, size
) != size
)
1407 die("unable to write temp-file");
1409 temp
->name
= temp
->tmp_path
;
1410 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1412 sprintf(temp
->mode
, "%06o", mode
);
1415 static void prepare_temp_file(const char *name
,
1416 struct diff_tempfile
*temp
,
1417 struct diff_filespec
*one
)
1419 if (!DIFF_FILE_VALID(one
)) {
1421 /* A '-' entry produces this for file-2, and
1422 * a '+' entry produces this for file-1.
1424 temp
->name
= "/dev/null";
1425 strcpy(temp
->hex
, ".");
1426 strcpy(temp
->mode
, ".");
1430 if (!one
->sha1_valid
||
1431 reuse_worktree_file(name
, one
->sha1
, 1)) {
1433 if (lstat(name
, &st
) < 0) {
1434 if (errno
== ENOENT
)
1435 goto not_a_valid_file
;
1436 die("stat(%s): %s", name
, strerror(errno
));
1438 if (S_ISLNK(st
.st_mode
)) {
1440 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1441 if (sizeof(buf
) <= st
.st_size
)
1442 die("symlink too long: %s", name
);
1443 ret
= readlink(name
, buf
, st
.st_size
);
1445 die("readlink(%s)", name
);
1446 prep_temp_blob(temp
, buf
, st
.st_size
,
1448 one
->sha1
: null_sha1
),
1450 one
->mode
: S_IFLNK
));
1453 /* we can borrow from the file in the work tree */
1455 if (!one
->sha1_valid
)
1456 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1458 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1459 /* Even though we may sometimes borrow the
1460 * contents from the work tree, we always want
1461 * one->mode. mode is trustworthy even when
1462 * !(one->sha1_valid), as long as
1463 * DIFF_FILE_VALID(one).
1465 sprintf(temp
->mode
, "%06o", one
->mode
);
1470 if (diff_populate_filespec(one
, 0))
1471 die("cannot read data blob for %s", one
->path
);
1472 prep_temp_blob(temp
, one
->data
, one
->size
,
1473 one
->sha1
, one
->mode
);
1477 static void remove_tempfile(void)
1481 for (i
= 0; i
< 2; i
++)
1482 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1483 unlink(diff_temp
[i
].name
);
1484 diff_temp
[i
].name
= NULL
;
1488 static void remove_tempfile_on_signal(int signo
)
1491 signal(SIGINT
, SIG_DFL
);
1495 static int spawn_prog(const char *pgm
, const char **arg
)
1503 die("unable to fork");
1505 execvp(pgm
, (char *const*) arg
);
1509 while (waitpid(pid
, &status
, 0) < 0) {
1515 /* Earlier we did not check the exit status because
1516 * diff exits non-zero if files are different, and
1517 * we are not interested in knowing that. It was a
1518 * mistake which made it harder to quit a diff-*
1519 * session that uses the git-apply-patch-script as
1520 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1521 * should also exit non-zero only when it wants to
1522 * abort the entire diff-* session.
1524 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1529 /* An external diff command takes:
1531 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1532 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1535 static void run_external_diff(const char *pgm
,
1538 struct diff_filespec
*one
,
1539 struct diff_filespec
*two
,
1540 const char *xfrm_msg
,
1541 int complete_rewrite
)
1543 const char *spawn_arg
[10];
1544 struct diff_tempfile
*temp
= diff_temp
;
1546 static int atexit_asked
= 0;
1547 const char *othername
;
1548 const char **arg
= &spawn_arg
[0];
1550 othername
= (other
? other
: name
);
1552 prepare_temp_file(name
, &temp
[0], one
);
1553 prepare_temp_file(othername
, &temp
[1], two
);
1554 if (! atexit_asked
&&
1555 (temp
[0].name
== temp
[0].tmp_path
||
1556 temp
[1].name
== temp
[1].tmp_path
)) {
1558 atexit(remove_tempfile
);
1560 signal(SIGINT
, remove_tempfile_on_signal
);
1566 *arg
++ = temp
[0].name
;
1567 *arg
++ = temp
[0].hex
;
1568 *arg
++ = temp
[0].mode
;
1569 *arg
++ = temp
[1].name
;
1570 *arg
++ = temp
[1].hex
;
1571 *arg
++ = temp
[1].mode
;
1581 retval
= spawn_prog(pgm
, spawn_arg
);
1584 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1589 static void run_diff_cmd(const char *pgm
,
1592 struct diff_filespec
*one
,
1593 struct diff_filespec
*two
,
1594 const char *xfrm_msg
,
1595 struct diff_options
*o
,
1596 int complete_rewrite
)
1599 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1604 builtin_diff(name
, other
? other
: name
,
1605 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1607 printf("* Unmerged path %s\n", name
);
1610 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1612 if (DIFF_FILE_VALID(one
)) {
1613 if (!one
->sha1_valid
) {
1615 if (lstat(one
->path
, &st
) < 0)
1616 die("stat %s", one
->path
);
1617 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1618 die("cannot hash %s\n", one
->path
);
1625 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1627 const char *pgm
= external_diff();
1628 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1629 struct diff_filespec
*one
;
1630 struct diff_filespec
*two
;
1633 char *name_munged
, *other_munged
;
1634 int complete_rewrite
= 0;
1637 if (DIFF_PAIR_UNMERGED(p
)) {
1639 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1643 name
= p
->one
->path
;
1644 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1645 name_munged
= quote_one(name
);
1646 other_munged
= quote_one(other
);
1647 one
= p
->one
; two
= p
->two
;
1649 diff_fill_sha1_info(one
);
1650 diff_fill_sha1_info(two
);
1653 switch (p
->status
) {
1654 case DIFF_STATUS_COPIED
:
1655 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1656 "similarity index %d%%\n"
1659 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1660 name_munged
, other_munged
);
1662 case DIFF_STATUS_RENAMED
:
1663 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1664 "similarity index %d%%\n"
1667 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1668 name_munged
, other_munged
);
1670 case DIFF_STATUS_MODIFIED
:
1672 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1673 "dissimilarity index %d%%\n",
1674 (int)(0.5 + p
->score
*
1676 complete_rewrite
= 1;
1685 if (hashcmp(one
->sha1
, two
->sha1
)) {
1686 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1690 if ((!fill_mmfile(&mf
, one
) && mmfile_is_binary(&mf
)) ||
1691 (!fill_mmfile(&mf
, two
) && mmfile_is_binary(&mf
)))
1694 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1696 abbrev
, sha1_to_hex(one
->sha1
),
1697 abbrev
, sha1_to_hex(two
->sha1
));
1698 if (one
->mode
== two
->mode
)
1699 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1700 " %06o", one
->mode
);
1701 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1706 xfrm_msg
= len
? msg
: NULL
;
1709 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1710 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1711 /* a filepair that changes between file and symlink
1712 * needs to be split into deletion and creation.
1714 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1715 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1717 null
= alloc_filespec(one
->path
);
1718 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1722 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1729 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1730 struct diffstat_t
*diffstat
)
1734 int complete_rewrite
= 0;
1736 if (DIFF_PAIR_UNMERGED(p
)) {
1738 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1742 name
= p
->one
->path
;
1743 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1745 diff_fill_sha1_info(p
->one
);
1746 diff_fill_sha1_info(p
->two
);
1748 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1749 complete_rewrite
= 1;
1750 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1753 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1758 if (DIFF_PAIR_UNMERGED(p
)) {
1763 name
= p
->one
->path
;
1764 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1766 diff_fill_sha1_info(p
->one
);
1767 diff_fill_sha1_info(p
->two
);
1769 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1772 void diff_setup(struct diff_options
*options
)
1774 memset(options
, 0, sizeof(*options
));
1775 options
->line_termination
= '\n';
1776 options
->break_opt
= -1;
1777 options
->rename_limit
= -1;
1778 options
->context
= 3;
1779 options
->msg_sep
= "";
1781 options
->change
= diff_change
;
1782 options
->add_remove
= diff_addremove
;
1783 options
->color_diff
= diff_use_color_default
;
1784 options
->detect_rename
= diff_detect_rename_default
;
1787 int diff_setup_done(struct diff_options
*options
)
1791 if (options
->output_format
& DIFF_FORMAT_NAME
)
1793 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
1795 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
1797 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
1800 die("--name-only, --name-status, --check and -s are mutually exclusive");
1802 if (options
->find_copies_harder
)
1803 options
->detect_rename
= DIFF_DETECT_COPY
;
1805 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1806 DIFF_FORMAT_NAME_STATUS
|
1807 DIFF_FORMAT_CHECKDIFF
|
1808 DIFF_FORMAT_NO_OUTPUT
))
1809 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1810 DIFF_FORMAT_NUMSTAT
|
1811 DIFF_FORMAT_DIFFSTAT
|
1812 DIFF_FORMAT_SHORTSTAT
|
1813 DIFF_FORMAT_SUMMARY
|
1817 * These cases always need recursive; we do not drop caller-supplied
1818 * recursive bits for other formats here.
1820 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1821 DIFF_FORMAT_NUMSTAT
|
1822 DIFF_FORMAT_DIFFSTAT
|
1823 DIFF_FORMAT_SHORTSTAT
|
1824 DIFF_FORMAT_SUMMARY
|
1825 DIFF_FORMAT_CHECKDIFF
))
1826 options
->recursive
= 1;
1828 * Also pickaxe would not work very well if you do not say recursive
1830 if (options
->pickaxe
)
1831 options
->recursive
= 1;
1833 if (options
->detect_rename
&& options
->rename_limit
< 0)
1834 options
->rename_limit
= diff_rename_limit_default
;
1835 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1837 /* read-cache does not die even when it fails
1838 * so it is safe for us to do this here. Also
1839 * it does not smudge active_cache or active_nr
1840 * when it fails, so we do not have to worry about
1841 * cleaning it up ourselves either.
1845 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1847 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1848 options
->abbrev
= 40; /* full */
1853 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1863 if (c
== arg_short
) {
1867 if (val
&& isdigit(c
)) {
1869 int n
= strtoul(arg
, &end
, 10);
1880 eq
= strchr(arg
, '=');
1885 if (!len
|| strncmp(arg
, arg_long
, len
))
1890 if (!isdigit(*++eq
))
1892 n
= strtoul(eq
, &end
, 10);
1900 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1902 const char *arg
= av
[0];
1903 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1904 options
->output_format
|= DIFF_FORMAT_PATCH
;
1905 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1906 options
->output_format
|= DIFF_FORMAT_PATCH
;
1907 else if (!strcmp(arg
, "--raw"))
1908 options
->output_format
|= DIFF_FORMAT_RAW
;
1909 else if (!strcmp(arg
, "--patch-with-raw")) {
1910 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1912 else if (!strcmp(arg
, "--numstat")) {
1913 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
1915 else if (!strcmp(arg
, "--shortstat")) {
1916 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
1918 else if (!strncmp(arg
, "--stat", 6)) {
1920 int width
= options
->stat_width
;
1921 int name_width
= options
->stat_name_width
;
1927 if (!strncmp(arg
, "-width=", 7))
1928 width
= strtoul(arg
+ 7, &end
, 10);
1929 else if (!strncmp(arg
, "-name-width=", 12))
1930 name_width
= strtoul(arg
+ 12, &end
, 10);
1933 width
= strtoul(arg
+1, &end
, 10);
1935 name_width
= strtoul(end
+1, &end
, 10);
1938 /* Important! This checks all the error cases! */
1941 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1942 options
->stat_name_width
= name_width
;
1943 options
->stat_width
= width
;
1945 else if (!strcmp(arg
, "--check"))
1946 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1947 else if (!strcmp(arg
, "--summary"))
1948 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1949 else if (!strcmp(arg
, "--patch-with-stat")) {
1950 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1952 else if (!strcmp(arg
, "-z"))
1953 options
->line_termination
= 0;
1954 else if (!strncmp(arg
, "-l", 2))
1955 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1956 else if (!strcmp(arg
, "--full-index"))
1957 options
->full_index
= 1;
1958 else if (!strcmp(arg
, "--binary")) {
1959 options
->output_format
|= DIFF_FORMAT_PATCH
;
1960 options
->binary
= 1;
1962 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
1965 else if (!strcmp(arg
, "--name-only"))
1966 options
->output_format
|= DIFF_FORMAT_NAME
;
1967 else if (!strcmp(arg
, "--name-status"))
1968 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1969 else if (!strcmp(arg
, "-R"))
1970 options
->reverse_diff
= 1;
1971 else if (!strncmp(arg
, "-S", 2))
1972 options
->pickaxe
= arg
+ 2;
1973 else if (!strcmp(arg
, "-s")) {
1974 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1976 else if (!strncmp(arg
, "-O", 2))
1977 options
->orderfile
= arg
+ 2;
1978 else if (!strncmp(arg
, "--diff-filter=", 14))
1979 options
->filter
= arg
+ 14;
1980 else if (!strcmp(arg
, "--pickaxe-all"))
1981 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1982 else if (!strcmp(arg
, "--pickaxe-regex"))
1983 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1984 else if (!strncmp(arg
, "-B", 2)) {
1985 if ((options
->break_opt
=
1986 diff_scoreopt_parse(arg
)) == -1)
1989 else if (!strncmp(arg
, "-M", 2)) {
1990 if ((options
->rename_score
=
1991 diff_scoreopt_parse(arg
)) == -1)
1993 options
->detect_rename
= DIFF_DETECT_RENAME
;
1995 else if (!strncmp(arg
, "-C", 2)) {
1996 if ((options
->rename_score
=
1997 diff_scoreopt_parse(arg
)) == -1)
1999 options
->detect_rename
= DIFF_DETECT_COPY
;
2001 else if (!strcmp(arg
, "--find-copies-harder"))
2002 options
->find_copies_harder
= 1;
2003 else if (!strcmp(arg
, "--abbrev"))
2004 options
->abbrev
= DEFAULT_ABBREV
;
2005 else if (!strncmp(arg
, "--abbrev=", 9)) {
2006 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2007 if (options
->abbrev
< MINIMUM_ABBREV
)
2008 options
->abbrev
= MINIMUM_ABBREV
;
2009 else if (40 < options
->abbrev
)
2010 options
->abbrev
= 40;
2012 else if (!strcmp(arg
, "--color"))
2013 options
->color_diff
= 1;
2014 else if (!strcmp(arg
, "--no-color"))
2015 options
->color_diff
= 0;
2016 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2017 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2018 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2019 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2020 else if (!strcmp(arg
, "--color-words"))
2021 options
->color_diff
= options
->color_diff_words
= 1;
2022 else if (!strcmp(arg
, "--no-renames"))
2023 options
->detect_rename
= 0;
2029 static int parse_num(const char **cp_p
)
2031 unsigned long num
, scale
;
2033 const char *cp
= *cp_p
;
2040 if ( !dot
&& ch
== '.' ) {
2043 } else if ( ch
== '%' ) {
2044 scale
= dot
? scale
*100 : 100;
2045 cp
++; /* % is always at the end */
2047 } else if ( ch
>= '0' && ch
<= '9' ) {
2048 if ( scale
< 100000 ) {
2050 num
= (num
*10) + (ch
-'0');
2059 /* user says num divided by scale and we say internally that
2060 * is MAX_SCORE * num / scale.
2062 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
2065 int diff_scoreopt_parse(const char *opt
)
2067 int opt1
, opt2
, cmd
;
2072 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2073 return -1; /* that is not a -M, -C nor -B option */
2075 opt1
= parse_num(&opt
);
2081 else if (*opt
!= '/')
2082 return -1; /* we expect -B80/99 or -B80 */
2085 opt2
= parse_num(&opt
);
2090 return opt1
| (opt2
<< 16);
2093 struct diff_queue_struct diff_queued_diff
;
2095 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2097 if (queue
->alloc
<= queue
->nr
) {
2098 queue
->alloc
= alloc_nr(queue
->alloc
);
2099 queue
->queue
= xrealloc(queue
->queue
,
2100 sizeof(dp
) * queue
->alloc
);
2102 queue
->queue
[queue
->nr
++] = dp
;
2105 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2106 struct diff_filespec
*one
,
2107 struct diff_filespec
*two
)
2109 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2117 void diff_free_filepair(struct diff_filepair
*p
)
2119 diff_free_filespec_data(p
->one
);
2120 diff_free_filespec_data(p
->two
);
2126 /* This is different from find_unique_abbrev() in that
2127 * it stuffs the result with dots for alignment.
2129 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2134 return sha1_to_hex(sha1
);
2136 abbrev
= find_unique_abbrev(sha1
, len
);
2138 return sha1_to_hex(sha1
);
2139 abblen
= strlen(abbrev
);
2141 static char hex
[41];
2142 if (len
< abblen
&& abblen
<= len
+ 2)
2143 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2145 sprintf(hex
, "%s...", abbrev
);
2148 return sha1_to_hex(sha1
);
2151 static void diff_flush_raw(struct diff_filepair
*p
,
2152 struct diff_options
*options
)
2156 int abbrev
= options
->abbrev
;
2157 const char *path_one
, *path_two
;
2158 int inter_name_termination
= '\t';
2159 int line_termination
= options
->line_termination
;
2161 if (!line_termination
)
2162 inter_name_termination
= 0;
2164 path_one
= p
->one
->path
;
2165 path_two
= p
->two
->path
;
2166 if (line_termination
) {
2167 path_one
= quote_one(path_one
);
2168 path_two
= quote_one(path_two
);
2172 sprintf(status
, "%c%03d", p
->status
,
2173 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2175 status
[0] = p
->status
;
2178 switch (p
->status
) {
2179 case DIFF_STATUS_COPIED
:
2180 case DIFF_STATUS_RENAMED
:
2183 case DIFF_STATUS_ADDED
:
2184 case DIFF_STATUS_DELETED
:
2191 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2192 printf(":%06o %06o %s ",
2193 p
->one
->mode
, p
->two
->mode
,
2194 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2196 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2198 printf("%s%c%s", status
, inter_name_termination
, path_one
);
2200 printf("%c%s", inter_name_termination
, path_two
);
2201 putchar(line_termination
);
2202 if (path_one
!= p
->one
->path
)
2203 free((void*)path_one
);
2204 if (path_two
!= p
->two
->path
)
2205 free((void*)path_two
);
2208 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
2210 char *path
= p
->two
->path
;
2212 if (line_termination
)
2213 path
= quote_one(p
->two
->path
);
2214 printf("%s%c", path
, line_termination
);
2215 if (p
->two
->path
!= path
)
2219 int diff_unmodified_pair(struct diff_filepair
*p
)
2221 /* This function is written stricter than necessary to support
2222 * the currently implemented transformers, but the idea is to
2223 * let transformers to produce diff_filepairs any way they want,
2224 * and filter and clean them up here before producing the output.
2226 struct diff_filespec
*one
, *two
;
2228 if (DIFF_PAIR_UNMERGED(p
))
2229 return 0; /* unmerged is interesting */
2234 /* deletion, addition, mode or type change
2235 * and rename are all interesting.
2237 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2238 DIFF_PAIR_MODE_CHANGED(p
) ||
2239 strcmp(one
->path
, two
->path
))
2242 /* both are valid and point at the same path. that is, we are
2243 * dealing with a change.
2245 if (one
->sha1_valid
&& two
->sha1_valid
&&
2246 !hashcmp(one
->sha1
, two
->sha1
))
2247 return 1; /* no change */
2248 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2249 return 1; /* both look at the same file on the filesystem. */
2253 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2255 if (diff_unmodified_pair(p
))
2258 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2259 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2260 return; /* no tree diffs in patch format */
2265 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2266 struct diffstat_t
*diffstat
)
2268 if (diff_unmodified_pair(p
))
2271 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2272 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2273 return; /* no tree diffs in patch format */
2275 run_diffstat(p
, o
, diffstat
);
2278 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2279 struct diff_options
*o
)
2281 if (diff_unmodified_pair(p
))
2284 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2285 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2286 return; /* no tree diffs in patch format */
2288 run_checkdiff(p
, o
);
2291 int diff_queue_is_empty(void)
2293 struct diff_queue_struct
*q
= &diff_queued_diff
;
2295 for (i
= 0; i
< q
->nr
; i
++)
2296 if (!diff_unmodified_pair(q
->queue
[i
]))
2302 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2304 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2307 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2309 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2310 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2312 s
->size
, s
->xfrm_flags
);
2315 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2317 diff_debug_filespec(p
->one
, i
, "one");
2318 diff_debug_filespec(p
->two
, i
, "two");
2319 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2320 p
->score
, p
->status
? p
->status
: '?',
2321 p
->source_stays
, p
->broken_pair
);
2324 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2328 fprintf(stderr
, "%s\n", msg
);
2329 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2330 for (i
= 0; i
< q
->nr
; i
++) {
2331 struct diff_filepair
*p
= q
->queue
[i
];
2332 diff_debug_filepair(p
, i
);
2337 static void diff_resolve_rename_copy(void)
2340 struct diff_filepair
*p
, *pp
;
2341 struct diff_queue_struct
*q
= &diff_queued_diff
;
2343 diff_debug_queue("resolve-rename-copy", q
);
2345 for (i
= 0; i
< q
->nr
; i
++) {
2347 p
->status
= 0; /* undecided */
2348 if (DIFF_PAIR_UNMERGED(p
))
2349 p
->status
= DIFF_STATUS_UNMERGED
;
2350 else if (!DIFF_FILE_VALID(p
->one
))
2351 p
->status
= DIFF_STATUS_ADDED
;
2352 else if (!DIFF_FILE_VALID(p
->two
))
2353 p
->status
= DIFF_STATUS_DELETED
;
2354 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2355 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2357 /* from this point on, we are dealing with a pair
2358 * whose both sides are valid and of the same type, i.e.
2359 * either in-place edit or rename/copy edit.
2361 else if (DIFF_PAIR_RENAME(p
)) {
2362 if (p
->source_stays
) {
2363 p
->status
= DIFF_STATUS_COPIED
;
2366 /* See if there is some other filepair that
2367 * copies from the same source as us. If so
2368 * we are a copy. Otherwise we are either a
2369 * copy if the path stays, or a rename if it
2370 * does not, but we already handled "stays" case.
2372 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2374 if (strcmp(pp
->one
->path
, p
->one
->path
))
2375 continue; /* not us */
2376 if (!DIFF_PAIR_RENAME(pp
))
2377 continue; /* not a rename/copy */
2378 /* pp is a rename/copy from the same source */
2379 p
->status
= DIFF_STATUS_COPIED
;
2383 p
->status
= DIFF_STATUS_RENAMED
;
2385 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2386 p
->one
->mode
!= p
->two
->mode
)
2387 p
->status
= DIFF_STATUS_MODIFIED
;
2389 /* This is a "no-change" entry and should not
2390 * happen anymore, but prepare for broken callers.
2392 error("feeding unmodified %s to diffcore",
2394 p
->status
= DIFF_STATUS_UNKNOWN
;
2397 diff_debug_queue("resolve-rename-copy done", q
);
2400 static int check_pair_status(struct diff_filepair
*p
)
2402 switch (p
->status
) {
2403 case DIFF_STATUS_UNKNOWN
:
2406 die("internal error in diff-resolve-rename-copy");
2412 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2414 int fmt
= opt
->output_format
;
2416 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2417 diff_flush_checkdiff(p
, opt
);
2418 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2419 diff_flush_raw(p
, opt
);
2420 else if (fmt
& DIFF_FORMAT_NAME
)
2421 diff_flush_name(p
, opt
->line_termination
);
2424 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2427 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2429 printf(" %s %s\n", newdelete
, fs
->path
);
2433 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2435 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2437 printf(" mode change %06o => %06o %s\n",
2438 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2440 printf(" mode change %06o => %06o\n",
2441 p
->one
->mode
, p
->two
->mode
);
2445 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2447 const char *old
, *new;
2449 /* Find common prefix */
2453 const char *slash_old
, *slash_new
;
2454 slash_old
= strchr(old
, '/');
2455 slash_new
= strchr(new, '/');
2458 slash_old
- old
!= slash_new
- new ||
2459 memcmp(old
, new, slash_new
- new))
2461 old
= slash_old
+ 1;
2462 new = slash_new
+ 1;
2464 /* p->one->path thru old is the common prefix, and old and new
2465 * through the end of names are renames
2467 if (old
!= p
->one
->path
)
2468 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2469 (int)(old
- p
->one
->path
), p
->one
->path
,
2470 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2472 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2473 p
->one
->path
, p
->two
->path
,
2474 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2475 show_mode_change(p
, 0);
2478 static void diff_summary(struct diff_filepair
*p
)
2481 case DIFF_STATUS_DELETED
:
2482 show_file_mode_name("delete", p
->one
);
2484 case DIFF_STATUS_ADDED
:
2485 show_file_mode_name("create", p
->two
);
2487 case DIFF_STATUS_COPIED
:
2488 show_rename_copy("copy", p
);
2490 case DIFF_STATUS_RENAMED
:
2491 show_rename_copy("rename", p
);
2495 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2496 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2497 show_mode_change(p
, 0);
2498 } else show_mode_change(p
, 1);
2504 struct xdiff_emit_state xm
;
2509 static int remove_space(char *line
, int len
)
2515 for (i
= 0; i
< len
; i
++)
2516 if (!isspace((c
= line
[i
])))
2522 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2524 struct patch_id_t
*data
= priv
;
2527 /* Ignore line numbers when computing the SHA1 of the patch */
2528 if (!strncmp(line
, "@@ -", 4))
2531 new_len
= remove_space(line
, len
);
2533 SHA1_Update(data
->ctx
, line
, new_len
);
2534 data
->patchlen
+= new_len
;
2537 /* returns 0 upon success, and writes result into sha1 */
2538 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2540 struct diff_queue_struct
*q
= &diff_queued_diff
;
2543 struct patch_id_t data
;
2544 char buffer
[PATH_MAX
* 4 + 20];
2547 memset(&data
, 0, sizeof(struct patch_id_t
));
2549 data
.xm
.consume
= patch_id_consume
;
2551 for (i
= 0; i
< q
->nr
; i
++) {
2556 struct diff_filepair
*p
= q
->queue
[i
];
2560 return error("internal diff status error");
2561 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2563 if (diff_unmodified_pair(p
))
2565 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2566 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2568 if (DIFF_PAIR_UNMERGED(p
))
2571 diff_fill_sha1_info(p
->one
);
2572 diff_fill_sha1_info(p
->two
);
2573 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2574 fill_mmfile(&mf2
, p
->two
) < 0)
2575 return error("unable to read files to diff");
2577 /* Maybe hash p->two? into the patch id? */
2578 if (mmfile_is_binary(&mf2
))
2581 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2582 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2583 if (p
->one
->mode
== 0)
2584 len1
= snprintf(buffer
, sizeof(buffer
),
2585 "diff--gita/%.*sb/%.*s"
2592 len2
, p
->two
->path
);
2593 else if (p
->two
->mode
== 0)
2594 len1
= snprintf(buffer
, sizeof(buffer
),
2595 "diff--gita/%.*sb/%.*s"
2596 "deletedfilemode%06o"
2602 len1
, p
->one
->path
);
2604 len1
= snprintf(buffer
, sizeof(buffer
),
2605 "diff--gita/%.*sb/%.*s"
2611 len2
, p
->two
->path
);
2612 SHA1_Update(&ctx
, buffer
, len1
);
2614 xpp
.flags
= XDF_NEED_MINIMAL
;
2616 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2617 ecb
.outf
= xdiff_outf
;
2619 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2622 SHA1_Final(sha1
, &ctx
);
2626 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2628 struct diff_queue_struct
*q
= &diff_queued_diff
;
2630 int result
= diff_get_patch_id(options
, sha1
);
2632 for (i
= 0; i
< q
->nr
; i
++)
2633 diff_free_filepair(q
->queue
[i
]);
2637 q
->nr
= q
->alloc
= 0;
2642 static int is_summary_empty(const struct diff_queue_struct
*q
)
2646 for (i
= 0; i
< q
->nr
; i
++) {
2647 const struct diff_filepair
*p
= q
->queue
[i
];
2649 switch (p
->status
) {
2650 case DIFF_STATUS_DELETED
:
2651 case DIFF_STATUS_ADDED
:
2652 case DIFF_STATUS_COPIED
:
2653 case DIFF_STATUS_RENAMED
:
2658 if (p
->one
->mode
&& p
->two
->mode
&&
2659 p
->one
->mode
!= p
->two
->mode
)
2667 void diff_flush(struct diff_options
*options
)
2669 struct diff_queue_struct
*q
= &diff_queued_diff
;
2670 int i
, output_format
= options
->output_format
;
2674 * Order: raw, stat, summary, patch
2675 * or: name/name-status/checkdiff (other bits clear)
2680 if (output_format
& (DIFF_FORMAT_RAW
|
2682 DIFF_FORMAT_NAME_STATUS
|
2683 DIFF_FORMAT_CHECKDIFF
)) {
2684 for (i
= 0; i
< q
->nr
; i
++) {
2685 struct diff_filepair
*p
= q
->queue
[i
];
2686 if (check_pair_status(p
))
2687 flush_one_pair(p
, options
);
2692 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
2693 struct diffstat_t diffstat
;
2695 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2696 diffstat
.xm
.consume
= diffstat_consume
;
2697 for (i
= 0; i
< q
->nr
; i
++) {
2698 struct diff_filepair
*p
= q
->queue
[i
];
2699 if (check_pair_status(p
))
2700 diff_flush_stat(p
, options
, &diffstat
);
2702 if (output_format
& DIFF_FORMAT_NUMSTAT
)
2703 show_numstat(&diffstat
, options
);
2704 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
2705 show_stats(&diffstat
, options
);
2706 else if (output_format
& DIFF_FORMAT_SHORTSTAT
)
2707 show_shortstats(&diffstat
);
2711 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2712 for (i
= 0; i
< q
->nr
; i
++)
2713 diff_summary(q
->queue
[i
]);
2717 if (output_format
& DIFF_FORMAT_PATCH
) {
2719 if (options
->stat_sep
) {
2720 /* attach patch instead of inline */
2721 fputs(options
->stat_sep
, stdout
);
2723 putchar(options
->line_termination
);
2727 for (i
= 0; i
< q
->nr
; i
++) {
2728 struct diff_filepair
*p
= q
->queue
[i
];
2729 if (check_pair_status(p
))
2730 diff_flush_patch(p
, options
);
2734 if (output_format
& DIFF_FORMAT_CALLBACK
)
2735 options
->format_callback(q
, options
, options
->format_callback_data
);
2737 for (i
= 0; i
< q
->nr
; i
++)
2738 diff_free_filepair(q
->queue
[i
]);
2742 q
->nr
= q
->alloc
= 0;
2745 static void diffcore_apply_filter(const char *filter
)
2748 struct diff_queue_struct
*q
= &diff_queued_diff
;
2749 struct diff_queue_struct outq
;
2751 outq
.nr
= outq
.alloc
= 0;
2756 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2758 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2759 struct diff_filepair
*p
= q
->queue
[i
];
2760 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2762 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2764 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2765 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2766 strchr(filter
, p
->status
)))
2772 /* otherwise we will clear the whole queue
2773 * by copying the empty outq at the end of this
2774 * function, but first clear the current entries
2777 for (i
= 0; i
< q
->nr
; i
++)
2778 diff_free_filepair(q
->queue
[i
]);
2781 /* Only the matching ones */
2782 for (i
= 0; i
< q
->nr
; i
++) {
2783 struct diff_filepair
*p
= q
->queue
[i
];
2785 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2787 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2789 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2790 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2791 strchr(filter
, p
->status
)))
2794 diff_free_filepair(p
);
2801 void diffcore_std(struct diff_options
*options
)
2803 if (options
->break_opt
!= -1)
2804 diffcore_break(options
->break_opt
);
2805 if (options
->detect_rename
)
2806 diffcore_rename(options
);
2807 if (options
->break_opt
!= -1)
2808 diffcore_merge_broken();
2809 if (options
->pickaxe
)
2810 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2811 if (options
->orderfile
)
2812 diffcore_order(options
->orderfile
);
2813 diff_resolve_rename_copy();
2814 diffcore_apply_filter(options
->filter
);
2818 void diffcore_std_no_resolve(struct diff_options
*options
)
2820 if (options
->pickaxe
)
2821 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2822 if (options
->orderfile
)
2823 diffcore_order(options
->orderfile
);
2824 diffcore_apply_filter(options
->filter
);
2827 void diff_addremove(struct diff_options
*options
,
2828 int addremove
, unsigned mode
,
2829 const unsigned char *sha1
,
2830 const char *base
, const char *path
)
2832 char concatpath
[PATH_MAX
];
2833 struct diff_filespec
*one
, *two
;
2835 /* This may look odd, but it is a preparation for
2836 * feeding "there are unchanged files which should
2837 * not produce diffs, but when you are doing copy
2838 * detection you would need them, so here they are"
2839 * entries to the diff-core. They will be prefixed
2840 * with something like '=' or '*' (I haven't decided
2841 * which but should not make any difference).
2842 * Feeding the same new and old to diff_change()
2843 * also has the same effect.
2844 * Before the final output happens, they are pruned after
2845 * merged into rename/copy pairs as appropriate.
2847 if (options
->reverse_diff
)
2848 addremove
= (addremove
== '+' ? '-' :
2849 addremove
== '-' ? '+' : addremove
);
2851 if (!path
) path
= "";
2852 sprintf(concatpath
, "%s%s", base
, path
);
2853 one
= alloc_filespec(concatpath
);
2854 two
= alloc_filespec(concatpath
);
2856 if (addremove
!= '+')
2857 fill_filespec(one
, sha1
, mode
);
2858 if (addremove
!= '-')
2859 fill_filespec(two
, sha1
, mode
);
2861 diff_queue(&diff_queued_diff
, one
, two
);
2864 void diff_change(struct diff_options
*options
,
2865 unsigned old_mode
, unsigned new_mode
,
2866 const unsigned char *old_sha1
,
2867 const unsigned char *new_sha1
,
2868 const char *base
, const char *path
)
2870 char concatpath
[PATH_MAX
];
2871 struct diff_filespec
*one
, *two
;
2873 if (options
->reverse_diff
) {
2875 const unsigned char *tmp_c
;
2876 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2877 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2879 if (!path
) path
= "";
2880 sprintf(concatpath
, "%s%s", base
, path
);
2881 one
= alloc_filespec(concatpath
);
2882 two
= alloc_filespec(concatpath
);
2883 fill_filespec(one
, old_sha1
, old_mode
);
2884 fill_filespec(two
, new_sha1
, new_mode
);
2886 diff_queue(&diff_queued_diff
, one
, two
);
2889 void diff_unmerge(struct diff_options
*options
,
2891 unsigned mode
, const unsigned char *sha1
)
2893 struct diff_filespec
*one
, *two
;
2894 one
= alloc_filespec(path
);
2895 two
= alloc_filespec(path
);
2896 fill_filespec(one
, sha1
, mode
);
2897 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;