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
;
387 static void free_diff_words_data(struct emit_callback
*ecbdata
)
389 if (ecbdata
->diff_words
) {
391 if (ecbdata
->diff_words
->minus
.text
.size
||
392 ecbdata
->diff_words
->plus
.text
.size
)
393 diff_words_show(ecbdata
->diff_words
);
395 if (ecbdata
->diff_words
->minus
.text
.ptr
)
396 free (ecbdata
->diff_words
->minus
.text
.ptr
);
397 if (ecbdata
->diff_words
->plus
.text
.ptr
)
398 free (ecbdata
->diff_words
->plus
.text
.ptr
);
399 free(ecbdata
->diff_words
);
400 ecbdata
->diff_words
= NULL
;
404 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
407 return diff_colors
[ix
];
411 static void emit_line(const char *set
, const char *reset
, const char *line
, int len
)
413 if (len
> 0 && line
[len
-1] == '\n')
416 fwrite(line
, len
, 1, stdout
);
420 static void emit_line_with_ws(int nparents
,
421 const char *set
, const char *reset
, const char *ws
,
422 const char *line
, int len
)
425 int last_tab_in_indent
= -1;
426 int last_space_in_indent
= -1;
429 int need_highlight_leading_space
= 0;
430 /* The line is a newly added line. Does it have funny leading
431 * whitespaces? In indent, SP should never precede a TAB.
433 for (i
= col0
; i
< len
; i
++) {
434 if (line
[i
] == '\t') {
435 last_tab_in_indent
= i
;
436 if (0 <= last_space_in_indent
)
437 need_highlight_leading_space
= 1;
439 else if (line
[i
] == ' ')
440 last_space_in_indent
= i
;
445 fwrite(line
, col0
, 1, stdout
);
446 fputs(reset
, stdout
);
447 if (((i
== len
) || line
[i
] == '\n') && i
!= col0
) {
448 /* The whole line was indent */
449 emit_line(ws
, reset
, line
+ col0
, len
- col0
);
453 if (need_highlight_leading_space
) {
454 while (i
< last_tab_in_indent
) {
455 if (line
[i
] == ' ') {
458 fputs(reset
, stdout
);
466 if (line
[tail
] == '\n' && i
< tail
)
469 if (!isspace(line
[tail
]))
473 if ((i
< tail
&& line
[tail
+ 1] != '\n')) {
474 /* This has whitespace between tail+1..len */
476 fwrite(line
+ i
, tail
- i
+ 1, 1, stdout
);
477 fputs(reset
, stdout
);
478 emit_line(ws
, reset
, line
+ tail
+ 1, len
- tail
- 1);
481 emit_line(set
, reset
, line
+ i
, len
- i
);
484 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
486 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
487 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
490 emit_line(set
, reset
, line
, len
);
492 emit_line_with_ws(ecbdata
->nparents
, set
, reset
, ws
,
496 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
500 struct emit_callback
*ecbdata
= priv
;
501 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
502 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
504 if (ecbdata
->label_path
[0]) {
505 const char *name_a_tab
, *name_b_tab
;
507 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
508 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
510 printf("%s--- %s%s%s\n",
511 set
, ecbdata
->label_path
[0], reset
, name_a_tab
);
512 printf("%s+++ %s%s%s\n",
513 set
, ecbdata
->label_path
[1], reset
, name_b_tab
);
514 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
517 /* This is not really necessary for now because
518 * this codepath only deals with two-way diffs.
520 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
522 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
523 ecbdata
->nparents
= i
- 1;
524 emit_line(diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
529 if (len
< ecbdata
->nparents
) {
531 emit_line(reset
, reset
, line
, len
);
536 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
537 /* fall back to normal diff */
538 free_diff_words_data(ecbdata
);
539 if (ecbdata
->diff_words
) {
540 if (line
[0] == '-') {
541 diff_words_append(line
, len
,
542 &ecbdata
->diff_words
->minus
);
544 } else if (line
[0] == '+') {
545 diff_words_append(line
, len
,
546 &ecbdata
->diff_words
->plus
);
549 if (ecbdata
->diff_words
->minus
.text
.size
||
550 ecbdata
->diff_words
->plus
.text
.size
)
551 diff_words_show(ecbdata
->diff_words
);
554 emit_line(set
, reset
, line
, len
);
557 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
559 color
= DIFF_FILE_OLD
;
560 else if (line
[i
] == '+')
561 color
= DIFF_FILE_NEW
;
564 if (color
!= DIFF_FILE_NEW
) {
565 emit_line(diff_get_color(ecbdata
->color_diff
, color
),
569 emit_add_line(reset
, ecbdata
, line
, len
);
572 static char *pprint_rename(const char *a
, const char *b
)
577 int pfx_length
, sfx_length
;
578 int len_a
= strlen(a
);
579 int len_b
= strlen(b
);
580 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
581 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
583 if (qlen_a
|| qlen_b
) {
584 if (qlen_a
) len_a
= qlen_a
;
585 if (qlen_b
) len_b
= qlen_b
;
586 name
= xmalloc( len_a
+ len_b
+ 5 );
588 quote_c_style(a
, name
, NULL
, 0);
590 memcpy(name
, a
, len_a
);
591 memcpy(name
+ len_a
, " => ", 4);
593 quote_c_style(b
, name
+ len_a
+ 4, NULL
, 0);
595 memcpy(name
+ len_a
+ 4, b
, len_b
+ 1);
599 /* Find common prefix */
601 while (*old
&& *new && *old
== *new) {
603 pfx_length
= old
- a
+ 1;
608 /* Find common suffix */
612 while (a
<= old
&& b
<= new && *old
== *new) {
614 sfx_length
= len_a
- (old
- a
);
620 * pfx{mid-a => mid-b}sfx
621 * {pfx-a => pfx-b}sfx
622 * pfx{sfx-a => sfx-b}
625 if (pfx_length
+ sfx_length
) {
626 int a_midlen
= len_a
- pfx_length
- sfx_length
;
627 int b_midlen
= len_b
- pfx_length
- sfx_length
;
628 if (a_midlen
< 0) a_midlen
= 0;
629 if (b_midlen
< 0) b_midlen
= 0;
631 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
632 sprintf(name
, "%.*s{%.*s => %.*s}%s",
634 a_midlen
, a
+ pfx_length
,
635 b_midlen
, b
+ pfx_length
,
636 a
+ len_a
- sfx_length
);
639 name
= xmalloc(len_a
+ len_b
+ 5);
640 sprintf(name
, "%s => %s", a
, b
);
646 struct xdiff_emit_state xm
;
650 struct diffstat_file
{
652 unsigned is_unmerged
:1;
653 unsigned is_binary
:1;
654 unsigned is_renamed
:1;
655 unsigned int added
, deleted
;
659 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
663 struct diffstat_file
*x
;
664 x
= xcalloc(sizeof (*x
), 1);
665 if (diffstat
->nr
== diffstat
->alloc
) {
666 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
667 diffstat
->files
= xrealloc(diffstat
->files
,
668 diffstat
->alloc
* sizeof(x
));
670 diffstat
->files
[diffstat
->nr
++] = x
;
672 x
->name
= pprint_rename(name_a
, name_b
);
676 x
->name
= xstrdup(name_a
);
680 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
682 struct diffstat_t
*diffstat
= priv
;
683 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
687 else if (line
[0] == '-')
691 const char mime_boundary_leader
[] = "------------";
693 static int scale_linear(int it
, int width
, int max_change
)
696 * make sure that at least one '-' is printed if there were deletions,
697 * and likewise for '+'.
701 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
704 static void show_name(const char *prefix
, const char *name
, int len
,
705 const char *reset
, const char *set
)
707 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
710 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
720 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
722 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
723 int max_change
= 0, max_len
= 0;
724 int total_files
= data
->nr
;
725 int width
, name_width
;
726 const char *reset
, *set
, *add_c
, *del_c
;
731 width
= options
->stat_width
? options
->stat_width
: 80;
732 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
734 /* Sanity: give at least 5 columns to the graph,
735 * but leave at least 10 columns for the name.
737 if (width
< name_width
+ 15) {
738 if (name_width
<= 25)
739 width
= name_width
+ 15;
741 name_width
= width
- 15;
744 /* Find the longest filename and max number of changes */
745 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
746 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
747 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
748 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
750 for (i
= 0; i
< data
->nr
; i
++) {
751 struct diffstat_file
*file
= data
->files
[i
];
752 int change
= file
->added
+ file
->deleted
;
754 if (!file
->is_renamed
) { /* renames are already quoted by pprint_rename */
755 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
757 char *qname
= xmalloc(len
+ 1);
758 quote_c_style(file
->name
, qname
, NULL
, 0);
764 len
= strlen(file
->name
);
768 if (file
->is_binary
|| file
->is_unmerged
)
770 if (max_change
< change
)
774 /* Compute the width of the graph part;
775 * 10 is for one blank at the beginning of the line plus
776 * " | count " between the name and the graph.
778 * From here on, name_width is the width of the name area,
779 * and width is the width of the graph area.
781 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
782 if (width
< (name_width
+ 10) + max_change
)
783 width
= width
- (name_width
+ 10);
787 for (i
= 0; i
< data
->nr
; i
++) {
788 const char *prefix
= "";
789 char *name
= data
->files
[i
]->name
;
790 int added
= data
->files
[i
]->added
;
791 int deleted
= data
->files
[i
]->deleted
;
795 * "scale" the filename
798 name_len
= strlen(name
);
799 if (name_width
< name_len
) {
803 name
+= name_len
- len
;
804 slash
= strchr(name
, '/');
809 if (data
->files
[i
]->is_binary
) {
810 show_name(prefix
, name
, len
, reset
, set
);
812 goto free_diffstat_file
;
814 else if (data
->files
[i
]->is_unmerged
) {
815 show_name(prefix
, name
, len
, reset
, set
);
816 printf(" Unmerged\n");
817 goto free_diffstat_file
;
819 else if (!data
->files
[i
]->is_renamed
&&
820 (added
+ deleted
== 0)) {
822 goto free_diffstat_file
;
826 * scale the add/delete
834 if (width
<= max_change
) {
835 add
= scale_linear(add
, width
, max_change
);
836 del
= scale_linear(del
, width
, max_change
);
839 show_name(prefix
, name
, len
, reset
, set
);
840 printf("%5d ", added
+ deleted
);
841 show_graph('+', add
, add_c
, reset
);
842 show_graph('-', del
, del_c
, reset
);
845 free(data
->files
[i
]->name
);
846 free(data
->files
[i
]);
849 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
850 set
, total_files
, adds
, dels
, reset
);
853 static void show_shortstats(struct diffstat_t
* data
)
855 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
860 for (i
= 0; i
< data
->nr
; i
++) {
861 if (!data
->files
[i
]->is_binary
&&
862 !data
->files
[i
]->is_unmerged
) {
863 int added
= data
->files
[i
]->added
;
864 int deleted
= data
->files
[i
]->deleted
;
865 if (!data
->files
[i
]->is_renamed
&&
866 (added
+ deleted
== 0)) {
873 free(data
->files
[i
]->name
);
874 free(data
->files
[i
]);
878 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
879 total_files
, adds
, dels
);
882 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
886 for (i
= 0; i
< data
->nr
; i
++) {
887 struct diffstat_file
*file
= data
->files
[i
];
892 printf("%d\t%d\t", file
->added
, file
->deleted
);
893 if (options
->line_termination
&& !file
->is_renamed
&&
894 quote_c_style(file
->name
, NULL
, NULL
, 0))
895 quote_c_style(file
->name
, NULL
, stdout
, 0);
897 fputs(file
->name
, stdout
);
898 putchar(options
->line_termination
);
903 struct xdiff_emit_state xm
;
904 const char *filename
;
905 int lineno
, color_diff
;
908 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
910 struct checkdiff_t
*data
= priv
;
911 const char *ws
= diff_get_color(data
->color_diff
, DIFF_WHITESPACE
);
912 const char *reset
= diff_get_color(data
->color_diff
, DIFF_RESET
);
913 const char *set
= diff_get_color(data
->color_diff
, DIFF_FILE_NEW
);
915 if (line
[0] == '+') {
916 int i
, spaces
= 0, space_before_tab
= 0, white_space_at_end
= 0;
918 /* check space before tab */
919 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
922 if (line
[i
- 1] == '\t' && spaces
)
923 space_before_tab
= 1;
925 /* check white space at line end */
926 if (line
[len
- 1] == '\n')
928 if (isspace(line
[len
- 1]))
929 white_space_at_end
= 1;
931 if (space_before_tab
|| white_space_at_end
) {
932 printf("%s:%d: %s", data
->filename
, data
->lineno
, ws
);
933 if (space_before_tab
) {
934 printf("space before tab");
935 if (white_space_at_end
)
938 if (white_space_at_end
)
939 printf("white space at end");
940 printf(":%s ", reset
);
941 emit_line_with_ws(1, set
, reset
, ws
, line
, len
);
945 } else if (line
[0] == ' ')
947 else if (line
[0] == '@') {
948 char *plus
= strchr(line
, '+');
950 data
->lineno
= strtol(plus
, NULL
, 10);
956 static unsigned char *deflate_it(char *data
,
958 unsigned long *result_size
)
961 unsigned char *deflated
;
964 memset(&stream
, 0, sizeof(stream
));
965 deflateInit(&stream
, zlib_compression_level
);
966 bound
= deflateBound(&stream
, size
);
967 deflated
= xmalloc(bound
);
968 stream
.next_out
= deflated
;
969 stream
.avail_out
= bound
;
971 stream
.next_in
= (unsigned char *)data
;
972 stream
.avail_in
= size
;
973 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
976 *result_size
= stream
.total_out
;
980 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
986 unsigned long orig_size
;
987 unsigned long delta_size
;
988 unsigned long deflate_size
;
989 unsigned long data_size
;
991 /* We could do deflated delta, or we could do just deflated two,
992 * whichever is smaller.
995 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
996 if (one
->size
&& two
->size
) {
997 delta
= diff_delta(one
->ptr
, one
->size
,
999 &delta_size
, deflate_size
);
1001 void *to_free
= delta
;
1002 orig_size
= delta_size
;
1003 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1008 if (delta
&& delta_size
< deflate_size
) {
1009 printf("delta %lu\n", orig_size
);
1012 data_size
= delta_size
;
1015 printf("literal %lu\n", two
->size
);
1018 data_size
= deflate_size
;
1021 /* emit data encoded in base85 */
1024 int bytes
= (52 < data_size
) ? 52 : data_size
;
1028 line
[0] = bytes
+ 'A' - 1;
1030 line
[0] = bytes
- 26 + 'a' - 1;
1031 encode_85(line
+ 1, cp
, bytes
);
1032 cp
= (char *) cp
+ bytes
;
1039 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
1041 printf("GIT binary patch\n");
1042 emit_binary_diff_body(one
, two
);
1043 emit_binary_diff_body(two
, one
);
1046 #define FIRST_FEW_BYTES 8000
1047 static int mmfile_is_binary(mmfile_t
*mf
)
1050 if (FIRST_FEW_BYTES
< sz
)
1051 sz
= FIRST_FEW_BYTES
;
1052 return !!memchr(mf
->ptr
, 0, sz
);
1055 static void builtin_diff(const char *name_a
,
1057 struct diff_filespec
*one
,
1058 struct diff_filespec
*two
,
1059 const char *xfrm_msg
,
1060 struct diff_options
*o
,
1061 int complete_rewrite
)
1065 char *a_one
, *b_two
;
1066 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
1067 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
1069 a_one
= quote_two("a/", name_a
+ (*name_a
== '/'));
1070 b_two
= quote_two("b/", name_b
+ (*name_b
== '/'));
1071 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1072 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1073 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1074 if (lbl
[0][0] == '/') {
1076 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1077 if (xfrm_msg
&& xfrm_msg
[0])
1078 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1080 else if (lbl
[1][0] == '/') {
1081 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1082 if (xfrm_msg
&& xfrm_msg
[0])
1083 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1086 if (one
->mode
!= two
->mode
) {
1087 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
1088 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
1090 if (xfrm_msg
&& xfrm_msg
[0])
1091 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1093 * we do not run diff between different kind
1096 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1097 goto free_ab_and_return
;
1098 if (complete_rewrite
) {
1099 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1101 goto free_ab_and_return
;
1105 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1106 die("unable to read files to diff");
1108 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
1109 /* Quite common confusing case */
1110 if (mf1
.size
== mf2
.size
&&
1111 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1112 goto free_ab_and_return
;
1114 emit_binary_diff(&mf1
, &mf2
);
1116 printf("Binary files %s and %s differ\n",
1120 /* Crazy xdl interfaces.. */
1121 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1125 struct emit_callback ecbdata
;
1127 memset(&ecbdata
, 0, sizeof(ecbdata
));
1128 ecbdata
.label_path
= lbl
;
1129 ecbdata
.color_diff
= o
->color_diff
;
1130 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1131 xecfg
.ctxlen
= o
->context
;
1132 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1135 else if (!prefixcmp(diffopts
, "--unified="))
1136 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1137 else if (!prefixcmp(diffopts
, "-u"))
1138 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1139 ecb
.outf
= xdiff_outf
;
1140 ecb
.priv
= &ecbdata
;
1141 ecbdata
.xm
.consume
= fn_out_consume
;
1142 if (o
->color_diff_words
)
1143 ecbdata
.diff_words
=
1144 xcalloc(1, sizeof(struct diff_words_data
));
1145 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1146 if (o
->color_diff_words
)
1147 free_diff_words_data(&ecbdata
);
1156 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1157 struct diff_filespec
*one
,
1158 struct diff_filespec
*two
,
1159 struct diffstat_t
*diffstat
,
1160 struct diff_options
*o
,
1161 int complete_rewrite
)
1164 struct diffstat_file
*data
;
1166 data
= diffstat_add(diffstat
, name_a
, name_b
);
1169 data
->is_unmerged
= 1;
1172 if (complete_rewrite
) {
1173 diff_populate_filespec(one
, 0);
1174 diff_populate_filespec(two
, 0);
1175 data
->deleted
= count_lines(one
->data
, one
->size
);
1176 data
->added
= count_lines(two
->data
, two
->size
);
1179 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1180 die("unable to read files to diff");
1182 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
1183 data
->is_binary
= 1;
1185 /* Crazy xdl interfaces.. */
1190 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1193 ecb
.outf
= xdiff_outf
;
1194 ecb
.priv
= diffstat
;
1195 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1199 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1200 struct diff_filespec
*one
,
1201 struct diff_filespec
*two
, struct diff_options
*o
)
1204 struct checkdiff_t data
;
1209 memset(&data
, 0, sizeof(data
));
1210 data
.xm
.consume
= checkdiff_consume
;
1211 data
.filename
= name_b
? name_b
: name_a
;
1213 data
.color_diff
= o
->color_diff
;
1215 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1216 die("unable to read files to diff");
1218 if (mmfile_is_binary(&mf2
))
1221 /* Crazy xdl interfaces.. */
1226 xpp
.flags
= XDF_NEED_MINIMAL
;
1229 ecb
.outf
= xdiff_outf
;
1231 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1235 struct diff_filespec
*alloc_filespec(const char *path
)
1237 int namelen
= strlen(path
);
1238 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1240 memset(spec
, 0, sizeof(*spec
));
1241 spec
->path
= (char *)(spec
+ 1);
1242 memcpy(spec
->path
, path
, namelen
+1);
1246 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1247 unsigned short mode
)
1250 spec
->mode
= canon_mode(mode
);
1251 hashcpy(spec
->sha1
, sha1
);
1252 spec
->sha1_valid
= !is_null_sha1(sha1
);
1257 * Given a name and sha1 pair, if the dircache tells us the file in
1258 * the work tree has that object contents, return true, so that
1259 * prepare_temp_file() does not have to inflate and extract.
1261 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1263 struct cache_entry
*ce
;
1267 /* We do not read the cache ourselves here, because the
1268 * benchmark with my previous version that always reads cache
1269 * shows that it makes things worse for diff-tree comparing
1270 * two linux-2.6 kernel trees in an already checked out work
1271 * tree. This is because most diff-tree comparisons deal with
1272 * only a small number of files, while reading the cache is
1273 * expensive for a large project, and its cost outweighs the
1274 * savings we get by not inflating the object to a temporary
1275 * file. Practically, this code only helps when we are used
1276 * by diff-cache --cached, which does read the cache before
1282 /* We want to avoid the working directory if our caller
1283 * doesn't need the data in a normal file, this system
1284 * is rather slow with its stat/open/mmap/close syscalls,
1285 * and the object is contained in a pack file. The pack
1286 * is probably already open and will be faster to obtain
1287 * the data through than the working directory. Loose
1288 * objects however would tend to be slower as they need
1289 * to be individually opened and inflated.
1291 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1295 pos
= cache_name_pos(name
, len
);
1298 ce
= active_cache
[pos
];
1299 if ((lstat(name
, &st
) < 0) ||
1300 !S_ISREG(st
.st_mode
) || /* careful! */
1301 ce_match_stat(ce
, &st
, 0) ||
1302 hashcmp(sha1
, ce
->sha1
))
1304 /* we return 1 only when we can stat, it is a regular file,
1305 * stat information matches, and sha1 recorded in the cache
1306 * matches. I.e. we know the file in the work tree really is
1307 * the same as the <name, sha1> pair.
1312 static struct sha1_size_cache
{
1313 unsigned char sha1
[20];
1315 } **sha1_size_cache
;
1316 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
1318 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
1323 struct sha1_size_cache
*e
;
1326 last
= sha1_size_cache_nr
;
1327 while (last
> first
) {
1328 int cmp
, next
= (last
+ first
) >> 1;
1329 e
= sha1_size_cache
[next
];
1330 cmp
= hashcmp(e
->sha1
, sha1
);
1342 /* insert to make it at "first" */
1343 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
1344 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
1345 sha1_size_cache
= xrealloc(sha1_size_cache
,
1346 sha1_size_cache_alloc
*
1347 sizeof(*sha1_size_cache
));
1349 sha1_size_cache_nr
++;
1350 if (first
< sha1_size_cache_nr
)
1351 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
1352 (sha1_size_cache_nr
- first
- 1) *
1353 sizeof(*sha1_size_cache
));
1354 e
= xmalloc(sizeof(struct sha1_size_cache
));
1355 sha1_size_cache
[first
] = e
;
1356 hashcpy(e
->sha1
, sha1
);
1362 * While doing rename detection and pickaxe operation, we may need to
1363 * grab the data for the blob (or file) for our own in-core comparison.
1364 * diff_filespec has data and size fields for this purpose.
1366 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1369 if (!DIFF_FILE_VALID(s
))
1370 die("internal error: asking to populate invalid file.");
1371 if (S_ISDIR(s
->mode
))
1374 if (!use_size_cache
)
1379 if (!s
->sha1_valid
||
1380 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1386 if (lstat(s
->path
, &st
) < 0) {
1387 if (errno
== ENOENT
) {
1391 s
->data
= (char *)"";
1396 s
->size
= st
.st_size
;
1401 if (S_ISLNK(st
.st_mode
)) {
1403 s
->data
= xmalloc(s
->size
);
1405 ret
= readlink(s
->path
, s
->data
, s
->size
);
1412 fd
= open(s
->path
, O_RDONLY
);
1415 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1417 s
->should_munmap
= 1;
1420 * Convert from working tree format to canonical git format
1424 if (convert_to_git(s
->path
, &buf
, &size
)) {
1425 munmap(s
->data
, s
->size
);
1426 s
->should_munmap
= 0;
1433 enum object_type type
;
1434 struct sha1_size_cache
*e
;
1437 e
= locate_size_cache(s
->sha1
, 1, 0);
1442 type
= sha1_object_info(s
->sha1
, &s
->size
);
1444 locate_size_cache(s
->sha1
, 0, s
->size
);
1447 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1454 void diff_free_filespec_data(struct diff_filespec
*s
)
1458 else if (s
->should_munmap
)
1459 munmap(s
->data
, s
->size
);
1460 s
->should_free
= s
->should_munmap
= 0;
1466 static void prep_temp_blob(struct diff_tempfile
*temp
,
1469 const unsigned char *sha1
,
1474 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1476 die("unable to create temp-file");
1477 if (write_in_full(fd
, blob
, size
) != size
)
1478 die("unable to write temp-file");
1480 temp
->name
= temp
->tmp_path
;
1481 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1483 sprintf(temp
->mode
, "%06o", mode
);
1486 static void prepare_temp_file(const char *name
,
1487 struct diff_tempfile
*temp
,
1488 struct diff_filespec
*one
)
1490 if (!DIFF_FILE_VALID(one
)) {
1492 /* A '-' entry produces this for file-2, and
1493 * a '+' entry produces this for file-1.
1495 temp
->name
= "/dev/null";
1496 strcpy(temp
->hex
, ".");
1497 strcpy(temp
->mode
, ".");
1501 if (!one
->sha1_valid
||
1502 reuse_worktree_file(name
, one
->sha1
, 1)) {
1504 if (lstat(name
, &st
) < 0) {
1505 if (errno
== ENOENT
)
1506 goto not_a_valid_file
;
1507 die("stat(%s): %s", name
, strerror(errno
));
1509 if (S_ISLNK(st
.st_mode
)) {
1511 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1512 if (sizeof(buf
) <= st
.st_size
)
1513 die("symlink too long: %s", name
);
1514 ret
= readlink(name
, buf
, st
.st_size
);
1516 die("readlink(%s)", name
);
1517 prep_temp_blob(temp
, buf
, st
.st_size
,
1519 one
->sha1
: null_sha1
),
1521 one
->mode
: S_IFLNK
));
1524 /* we can borrow from the file in the work tree */
1526 if (!one
->sha1_valid
)
1527 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1529 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1530 /* Even though we may sometimes borrow the
1531 * contents from the work tree, we always want
1532 * one->mode. mode is trustworthy even when
1533 * !(one->sha1_valid), as long as
1534 * DIFF_FILE_VALID(one).
1536 sprintf(temp
->mode
, "%06o", one
->mode
);
1541 if (diff_populate_filespec(one
, 0))
1542 die("cannot read data blob for %s", one
->path
);
1543 prep_temp_blob(temp
, one
->data
, one
->size
,
1544 one
->sha1
, one
->mode
);
1548 static void remove_tempfile(void)
1552 for (i
= 0; i
< 2; i
++)
1553 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1554 unlink(diff_temp
[i
].name
);
1555 diff_temp
[i
].name
= NULL
;
1559 static void remove_tempfile_on_signal(int signo
)
1562 signal(SIGINT
, SIG_DFL
);
1566 static int spawn_prog(const char *pgm
, const char **arg
)
1574 die("unable to fork");
1576 execvp(pgm
, (char *const*) arg
);
1580 while (waitpid(pid
, &status
, 0) < 0) {
1586 /* Earlier we did not check the exit status because
1587 * diff exits non-zero if files are different, and
1588 * we are not interested in knowing that. It was a
1589 * mistake which made it harder to quit a diff-*
1590 * session that uses the git-apply-patch-script as
1591 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1592 * should also exit non-zero only when it wants to
1593 * abort the entire diff-* session.
1595 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1600 /* An external diff command takes:
1602 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1603 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1606 static void run_external_diff(const char *pgm
,
1609 struct diff_filespec
*one
,
1610 struct diff_filespec
*two
,
1611 const char *xfrm_msg
,
1612 int complete_rewrite
)
1614 const char *spawn_arg
[10];
1615 struct diff_tempfile
*temp
= diff_temp
;
1617 static int atexit_asked
= 0;
1618 const char *othername
;
1619 const char **arg
= &spawn_arg
[0];
1621 othername
= (other
? other
: name
);
1623 prepare_temp_file(name
, &temp
[0], one
);
1624 prepare_temp_file(othername
, &temp
[1], two
);
1625 if (! atexit_asked
&&
1626 (temp
[0].name
== temp
[0].tmp_path
||
1627 temp
[1].name
== temp
[1].tmp_path
)) {
1629 atexit(remove_tempfile
);
1631 signal(SIGINT
, remove_tempfile_on_signal
);
1637 *arg
++ = temp
[0].name
;
1638 *arg
++ = temp
[0].hex
;
1639 *arg
++ = temp
[0].mode
;
1640 *arg
++ = temp
[1].name
;
1641 *arg
++ = temp
[1].hex
;
1642 *arg
++ = temp
[1].mode
;
1652 retval
= spawn_prog(pgm
, spawn_arg
);
1655 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1660 static void run_diff_cmd(const char *pgm
,
1663 struct diff_filespec
*one
,
1664 struct diff_filespec
*two
,
1665 const char *xfrm_msg
,
1666 struct diff_options
*o
,
1667 int complete_rewrite
)
1670 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1675 builtin_diff(name
, other
? other
: name
,
1676 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1678 printf("* Unmerged path %s\n", name
);
1681 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1683 if (DIFF_FILE_VALID(one
)) {
1684 if (!one
->sha1_valid
) {
1686 if (lstat(one
->path
, &st
) < 0)
1687 die("stat %s", one
->path
);
1688 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1689 die("cannot hash %s\n", one
->path
);
1696 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1698 const char *pgm
= external_diff();
1699 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1700 struct diff_filespec
*one
;
1701 struct diff_filespec
*two
;
1704 char *name_munged
, *other_munged
;
1705 int complete_rewrite
= 0;
1708 if (DIFF_PAIR_UNMERGED(p
)) {
1710 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1714 name
= p
->one
->path
;
1715 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1716 name_munged
= quote_one(name
);
1717 other_munged
= quote_one(other
);
1718 one
= p
->one
; two
= p
->two
;
1720 diff_fill_sha1_info(one
);
1721 diff_fill_sha1_info(two
);
1724 switch (p
->status
) {
1725 case DIFF_STATUS_COPIED
:
1726 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1727 "similarity index %d%%\n"
1730 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1731 name_munged
, other_munged
);
1733 case DIFF_STATUS_RENAMED
:
1734 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1735 "similarity index %d%%\n"
1738 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1739 name_munged
, other_munged
);
1741 case DIFF_STATUS_MODIFIED
:
1743 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1744 "dissimilarity index %d%%\n",
1745 (int)(0.5 + p
->score
*
1747 complete_rewrite
= 1;
1756 if (hashcmp(one
->sha1
, two
->sha1
)) {
1757 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1761 if ((!fill_mmfile(&mf
, one
) && mmfile_is_binary(&mf
)) ||
1762 (!fill_mmfile(&mf
, two
) && mmfile_is_binary(&mf
)))
1765 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1767 abbrev
, sha1_to_hex(one
->sha1
),
1768 abbrev
, sha1_to_hex(two
->sha1
));
1769 if (one
->mode
== two
->mode
)
1770 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1771 " %06o", one
->mode
);
1772 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1777 xfrm_msg
= len
? msg
: NULL
;
1780 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1781 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1782 /* a filepair that changes between file and symlink
1783 * needs to be split into deletion and creation.
1785 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1786 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1788 null
= alloc_filespec(one
->path
);
1789 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1793 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1800 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1801 struct diffstat_t
*diffstat
)
1805 int complete_rewrite
= 0;
1807 if (DIFF_PAIR_UNMERGED(p
)) {
1809 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1813 name
= p
->one
->path
;
1814 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1816 diff_fill_sha1_info(p
->one
);
1817 diff_fill_sha1_info(p
->two
);
1819 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1820 complete_rewrite
= 1;
1821 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1824 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1829 if (DIFF_PAIR_UNMERGED(p
)) {
1834 name
= p
->one
->path
;
1835 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1837 diff_fill_sha1_info(p
->one
);
1838 diff_fill_sha1_info(p
->two
);
1840 builtin_checkdiff(name
, other
, p
->one
, p
->two
, o
);
1843 void diff_setup(struct diff_options
*options
)
1845 memset(options
, 0, sizeof(*options
));
1846 options
->line_termination
= '\n';
1847 options
->break_opt
= -1;
1848 options
->rename_limit
= -1;
1849 options
->context
= 3;
1850 options
->msg_sep
= "";
1852 options
->change
= diff_change
;
1853 options
->add_remove
= diff_addremove
;
1854 options
->color_diff
= diff_use_color_default
;
1855 options
->detect_rename
= diff_detect_rename_default
;
1858 int diff_setup_done(struct diff_options
*options
)
1862 if (options
->output_format
& DIFF_FORMAT_NAME
)
1864 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
1866 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
1868 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
1871 die("--name-only, --name-status, --check and -s are mutually exclusive");
1873 if (options
->find_copies_harder
)
1874 options
->detect_rename
= DIFF_DETECT_COPY
;
1876 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1877 DIFF_FORMAT_NAME_STATUS
|
1878 DIFF_FORMAT_CHECKDIFF
|
1879 DIFF_FORMAT_NO_OUTPUT
))
1880 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1881 DIFF_FORMAT_NUMSTAT
|
1882 DIFF_FORMAT_DIFFSTAT
|
1883 DIFF_FORMAT_SHORTSTAT
|
1884 DIFF_FORMAT_SUMMARY
|
1888 * These cases always need recursive; we do not drop caller-supplied
1889 * recursive bits for other formats here.
1891 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1892 DIFF_FORMAT_NUMSTAT
|
1893 DIFF_FORMAT_DIFFSTAT
|
1894 DIFF_FORMAT_SHORTSTAT
|
1895 DIFF_FORMAT_SUMMARY
|
1896 DIFF_FORMAT_CHECKDIFF
))
1897 options
->recursive
= 1;
1899 * Also pickaxe would not work very well if you do not say recursive
1901 if (options
->pickaxe
)
1902 options
->recursive
= 1;
1904 if (options
->detect_rename
&& options
->rename_limit
< 0)
1905 options
->rename_limit
= diff_rename_limit_default
;
1906 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1908 /* read-cache does not die even when it fails
1909 * so it is safe for us to do this here. Also
1910 * it does not smudge active_cache or active_nr
1911 * when it fails, so we do not have to worry about
1912 * cleaning it up ourselves either.
1916 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1918 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1919 options
->abbrev
= 40; /* full */
1924 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1934 if (c
== arg_short
) {
1938 if (val
&& isdigit(c
)) {
1940 int n
= strtoul(arg
, &end
, 10);
1951 eq
= strchr(arg
, '=');
1956 if (!len
|| strncmp(arg
, arg_long
, len
))
1961 if (!isdigit(*++eq
))
1963 n
= strtoul(eq
, &end
, 10);
1971 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1973 const char *arg
= av
[0];
1974 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1975 options
->output_format
|= DIFF_FORMAT_PATCH
;
1976 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1977 options
->output_format
|= DIFF_FORMAT_PATCH
;
1978 else if (!strcmp(arg
, "--raw"))
1979 options
->output_format
|= DIFF_FORMAT_RAW
;
1980 else if (!strcmp(arg
, "--patch-with-raw")) {
1981 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1983 else if (!strcmp(arg
, "--numstat")) {
1984 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
1986 else if (!strcmp(arg
, "--shortstat")) {
1987 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
1989 else if (!prefixcmp(arg
, "--stat")) {
1991 int width
= options
->stat_width
;
1992 int name_width
= options
->stat_name_width
;
1998 if (!prefixcmp(arg
, "-width="))
1999 width
= strtoul(arg
+ 7, &end
, 10);
2000 else if (!prefixcmp(arg
, "-name-width="))
2001 name_width
= strtoul(arg
+ 12, &end
, 10);
2004 width
= strtoul(arg
+1, &end
, 10);
2006 name_width
= strtoul(end
+1, &end
, 10);
2009 /* Important! This checks all the error cases! */
2012 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2013 options
->stat_name_width
= name_width
;
2014 options
->stat_width
= width
;
2016 else if (!strcmp(arg
, "--check"))
2017 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2018 else if (!strcmp(arg
, "--summary"))
2019 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2020 else if (!strcmp(arg
, "--patch-with-stat")) {
2021 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2023 else if (!strcmp(arg
, "-z"))
2024 options
->line_termination
= 0;
2025 else if (!prefixcmp(arg
, "-l"))
2026 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2027 else if (!strcmp(arg
, "--full-index"))
2028 options
->full_index
= 1;
2029 else if (!strcmp(arg
, "--binary")) {
2030 options
->output_format
|= DIFF_FORMAT_PATCH
;
2031 options
->binary
= 1;
2033 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
2036 else if (!strcmp(arg
, "--name-only"))
2037 options
->output_format
|= DIFF_FORMAT_NAME
;
2038 else if (!strcmp(arg
, "--name-status"))
2039 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2040 else if (!strcmp(arg
, "-R"))
2041 options
->reverse_diff
= 1;
2042 else if (!prefixcmp(arg
, "-S"))
2043 options
->pickaxe
= arg
+ 2;
2044 else if (!strcmp(arg
, "-s")) {
2045 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2047 else if (!prefixcmp(arg
, "-O"))
2048 options
->orderfile
= arg
+ 2;
2049 else if (!prefixcmp(arg
, "--diff-filter="))
2050 options
->filter
= arg
+ 14;
2051 else if (!strcmp(arg
, "--pickaxe-all"))
2052 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2053 else if (!strcmp(arg
, "--pickaxe-regex"))
2054 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2055 else if (!prefixcmp(arg
, "-B")) {
2056 if ((options
->break_opt
=
2057 diff_scoreopt_parse(arg
)) == -1)
2060 else if (!prefixcmp(arg
, "-M")) {
2061 if ((options
->rename_score
=
2062 diff_scoreopt_parse(arg
)) == -1)
2064 options
->detect_rename
= DIFF_DETECT_RENAME
;
2066 else if (!prefixcmp(arg
, "-C")) {
2067 if ((options
->rename_score
=
2068 diff_scoreopt_parse(arg
)) == -1)
2070 options
->detect_rename
= DIFF_DETECT_COPY
;
2072 else if (!strcmp(arg
, "--find-copies-harder"))
2073 options
->find_copies_harder
= 1;
2074 else if (!strcmp(arg
, "--abbrev"))
2075 options
->abbrev
= DEFAULT_ABBREV
;
2076 else if (!prefixcmp(arg
, "--abbrev=")) {
2077 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2078 if (options
->abbrev
< MINIMUM_ABBREV
)
2079 options
->abbrev
= MINIMUM_ABBREV
;
2080 else if (40 < options
->abbrev
)
2081 options
->abbrev
= 40;
2083 else if (!strcmp(arg
, "--color"))
2084 options
->color_diff
= 1;
2085 else if (!strcmp(arg
, "--no-color"))
2086 options
->color_diff
= 0;
2087 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2088 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2089 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2090 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2091 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2092 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2093 else if (!strcmp(arg
, "--color-words"))
2094 options
->color_diff
= options
->color_diff_words
= 1;
2095 else if (!strcmp(arg
, "--no-renames"))
2096 options
->detect_rename
= 0;
2102 static int parse_num(const char **cp_p
)
2104 unsigned long num
, scale
;
2106 const char *cp
= *cp_p
;
2113 if ( !dot
&& ch
== '.' ) {
2116 } else if ( ch
== '%' ) {
2117 scale
= dot
? scale
*100 : 100;
2118 cp
++; /* % is always at the end */
2120 } else if ( ch
>= '0' && ch
<= '9' ) {
2121 if ( scale
< 100000 ) {
2123 num
= (num
*10) + (ch
-'0');
2132 /* user says num divided by scale and we say internally that
2133 * is MAX_SCORE * num / scale.
2135 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
2138 int diff_scoreopt_parse(const char *opt
)
2140 int opt1
, opt2
, cmd
;
2145 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2146 return -1; /* that is not a -M, -C nor -B option */
2148 opt1
= parse_num(&opt
);
2154 else if (*opt
!= '/')
2155 return -1; /* we expect -B80/99 or -B80 */
2158 opt2
= parse_num(&opt
);
2163 return opt1
| (opt2
<< 16);
2166 struct diff_queue_struct diff_queued_diff
;
2168 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2170 if (queue
->alloc
<= queue
->nr
) {
2171 queue
->alloc
= alloc_nr(queue
->alloc
);
2172 queue
->queue
= xrealloc(queue
->queue
,
2173 sizeof(dp
) * queue
->alloc
);
2175 queue
->queue
[queue
->nr
++] = dp
;
2178 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2179 struct diff_filespec
*one
,
2180 struct diff_filespec
*two
)
2182 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2190 void diff_free_filepair(struct diff_filepair
*p
)
2192 diff_free_filespec_data(p
->one
);
2193 diff_free_filespec_data(p
->two
);
2199 /* This is different from find_unique_abbrev() in that
2200 * it stuffs the result with dots for alignment.
2202 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2207 return sha1_to_hex(sha1
);
2209 abbrev
= find_unique_abbrev(sha1
, len
);
2211 return sha1_to_hex(sha1
);
2212 abblen
= strlen(abbrev
);
2214 static char hex
[41];
2215 if (len
< abblen
&& abblen
<= len
+ 2)
2216 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2218 sprintf(hex
, "%s...", abbrev
);
2221 return sha1_to_hex(sha1
);
2224 static void diff_flush_raw(struct diff_filepair
*p
,
2225 struct diff_options
*options
)
2229 int abbrev
= options
->abbrev
;
2230 const char *path_one
, *path_two
;
2231 int inter_name_termination
= '\t';
2232 int line_termination
= options
->line_termination
;
2234 if (!line_termination
)
2235 inter_name_termination
= 0;
2237 path_one
= p
->one
->path
;
2238 path_two
= p
->two
->path
;
2239 if (line_termination
) {
2240 path_one
= quote_one(path_one
);
2241 path_two
= quote_one(path_two
);
2245 sprintf(status
, "%c%03d", p
->status
,
2246 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2248 status
[0] = p
->status
;
2251 switch (p
->status
) {
2252 case DIFF_STATUS_COPIED
:
2253 case DIFF_STATUS_RENAMED
:
2256 case DIFF_STATUS_ADDED
:
2257 case DIFF_STATUS_DELETED
:
2264 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2265 printf(":%06o %06o %s ",
2266 p
->one
->mode
, p
->two
->mode
,
2267 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2269 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2271 printf("%s%c%s", status
, inter_name_termination
, path_one
);
2273 printf("%c%s", inter_name_termination
, path_two
);
2274 putchar(line_termination
);
2275 if (path_one
!= p
->one
->path
)
2276 free((void*)path_one
);
2277 if (path_two
!= p
->two
->path
)
2278 free((void*)path_two
);
2281 static void diff_flush_name(struct diff_filepair
*p
, struct diff_options
*opt
)
2283 char *path
= p
->two
->path
;
2285 if (opt
->line_termination
)
2286 path
= quote_one(p
->two
->path
);
2287 printf("%s%c", path
, opt
->line_termination
);
2288 if (p
->two
->path
!= path
)
2292 int diff_unmodified_pair(struct diff_filepair
*p
)
2294 /* This function is written stricter than necessary to support
2295 * the currently implemented transformers, but the idea is to
2296 * let transformers to produce diff_filepairs any way they want,
2297 * and filter and clean them up here before producing the output.
2299 struct diff_filespec
*one
, *two
;
2301 if (DIFF_PAIR_UNMERGED(p
))
2302 return 0; /* unmerged is interesting */
2307 /* deletion, addition, mode or type change
2308 * and rename are all interesting.
2310 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2311 DIFF_PAIR_MODE_CHANGED(p
) ||
2312 strcmp(one
->path
, two
->path
))
2315 /* both are valid and point at the same path. that is, we are
2316 * dealing with a change.
2318 if (one
->sha1_valid
&& two
->sha1_valid
&&
2319 !hashcmp(one
->sha1
, two
->sha1
))
2320 return 1; /* no change */
2321 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2322 return 1; /* both look at the same file on the filesystem. */
2326 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2328 if (diff_unmodified_pair(p
))
2331 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2332 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2333 return; /* no tree diffs in patch format */
2338 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2339 struct diffstat_t
*diffstat
)
2341 if (diff_unmodified_pair(p
))
2344 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2345 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2346 return; /* no tree diffs in patch format */
2348 run_diffstat(p
, o
, diffstat
);
2351 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2352 struct diff_options
*o
)
2354 if (diff_unmodified_pair(p
))
2357 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2358 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2359 return; /* no tree diffs in patch format */
2361 run_checkdiff(p
, o
);
2364 int diff_queue_is_empty(void)
2366 struct diff_queue_struct
*q
= &diff_queued_diff
;
2368 for (i
= 0; i
< q
->nr
; i
++)
2369 if (!diff_unmodified_pair(q
->queue
[i
]))
2375 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2377 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2380 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2382 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2383 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2385 s
->size
, s
->xfrm_flags
);
2388 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2390 diff_debug_filespec(p
->one
, i
, "one");
2391 diff_debug_filespec(p
->two
, i
, "two");
2392 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2393 p
->score
, p
->status
? p
->status
: '?',
2394 p
->source_stays
, p
->broken_pair
);
2397 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2401 fprintf(stderr
, "%s\n", msg
);
2402 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2403 for (i
= 0; i
< q
->nr
; i
++) {
2404 struct diff_filepair
*p
= q
->queue
[i
];
2405 diff_debug_filepair(p
, i
);
2410 static void diff_resolve_rename_copy(void)
2413 struct diff_filepair
*p
, *pp
;
2414 struct diff_queue_struct
*q
= &diff_queued_diff
;
2416 diff_debug_queue("resolve-rename-copy", q
);
2418 for (i
= 0; i
< q
->nr
; i
++) {
2420 p
->status
= 0; /* undecided */
2421 if (DIFF_PAIR_UNMERGED(p
))
2422 p
->status
= DIFF_STATUS_UNMERGED
;
2423 else if (!DIFF_FILE_VALID(p
->one
))
2424 p
->status
= DIFF_STATUS_ADDED
;
2425 else if (!DIFF_FILE_VALID(p
->two
))
2426 p
->status
= DIFF_STATUS_DELETED
;
2427 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2428 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2430 /* from this point on, we are dealing with a pair
2431 * whose both sides are valid and of the same type, i.e.
2432 * either in-place edit or rename/copy edit.
2434 else if (DIFF_PAIR_RENAME(p
)) {
2435 if (p
->source_stays
) {
2436 p
->status
= DIFF_STATUS_COPIED
;
2439 /* See if there is some other filepair that
2440 * copies from the same source as us. If so
2441 * we are a copy. Otherwise we are either a
2442 * copy if the path stays, or a rename if it
2443 * does not, but we already handled "stays" case.
2445 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2447 if (strcmp(pp
->one
->path
, p
->one
->path
))
2448 continue; /* not us */
2449 if (!DIFF_PAIR_RENAME(pp
))
2450 continue; /* not a rename/copy */
2451 /* pp is a rename/copy from the same source */
2452 p
->status
= DIFF_STATUS_COPIED
;
2456 p
->status
= DIFF_STATUS_RENAMED
;
2458 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2459 p
->one
->mode
!= p
->two
->mode
)
2460 p
->status
= DIFF_STATUS_MODIFIED
;
2462 /* This is a "no-change" entry and should not
2463 * happen anymore, but prepare for broken callers.
2465 error("feeding unmodified %s to diffcore",
2467 p
->status
= DIFF_STATUS_UNKNOWN
;
2470 diff_debug_queue("resolve-rename-copy done", q
);
2473 static int check_pair_status(struct diff_filepair
*p
)
2475 switch (p
->status
) {
2476 case DIFF_STATUS_UNKNOWN
:
2479 die("internal error in diff-resolve-rename-copy");
2485 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2487 int fmt
= opt
->output_format
;
2489 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2490 diff_flush_checkdiff(p
, opt
);
2491 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2492 diff_flush_raw(p
, opt
);
2493 else if (fmt
& DIFF_FORMAT_NAME
)
2494 diff_flush_name(p
, opt
);
2497 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2499 char *name
= quote_one(fs
->path
);
2501 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, name
);
2503 printf(" %s %s\n", newdelete
, name
);
2508 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2510 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2512 char *name
= quote_one(p
->two
->path
);
2513 printf(" mode change %06o => %06o %s\n",
2514 p
->one
->mode
, p
->two
->mode
, name
);
2518 printf(" mode change %06o => %06o\n",
2519 p
->one
->mode
, p
->two
->mode
);
2523 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2525 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
2527 printf(" %s %s (%d%%)\n", renamecopy
, names
,
2528 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2530 show_mode_change(p
, 0);
2533 static void diff_summary(struct diff_filepair
*p
)
2536 case DIFF_STATUS_DELETED
:
2537 show_file_mode_name("delete", p
->one
);
2539 case DIFF_STATUS_ADDED
:
2540 show_file_mode_name("create", p
->two
);
2542 case DIFF_STATUS_COPIED
:
2543 show_rename_copy("copy", p
);
2545 case DIFF_STATUS_RENAMED
:
2546 show_rename_copy("rename", p
);
2550 char *name
= quote_one(p
->two
->path
);
2551 printf(" rewrite %s (%d%%)\n", name
,
2552 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2554 show_mode_change(p
, 0);
2555 } else show_mode_change(p
, 1);
2561 struct xdiff_emit_state xm
;
2566 static int remove_space(char *line
, int len
)
2572 for (i
= 0; i
< len
; i
++)
2573 if (!isspace((c
= line
[i
])))
2579 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2581 struct patch_id_t
*data
= priv
;
2584 /* Ignore line numbers when computing the SHA1 of the patch */
2585 if (!prefixcmp(line
, "@@ -"))
2588 new_len
= remove_space(line
, len
);
2590 SHA1_Update(data
->ctx
, line
, new_len
);
2591 data
->patchlen
+= new_len
;
2594 /* returns 0 upon success, and writes result into sha1 */
2595 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2597 struct diff_queue_struct
*q
= &diff_queued_diff
;
2600 struct patch_id_t data
;
2601 char buffer
[PATH_MAX
* 4 + 20];
2604 memset(&data
, 0, sizeof(struct patch_id_t
));
2606 data
.xm
.consume
= patch_id_consume
;
2608 for (i
= 0; i
< q
->nr
; i
++) {
2613 struct diff_filepair
*p
= q
->queue
[i
];
2617 return error("internal diff status error");
2618 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2620 if (diff_unmodified_pair(p
))
2622 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2623 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2625 if (DIFF_PAIR_UNMERGED(p
))
2628 diff_fill_sha1_info(p
->one
);
2629 diff_fill_sha1_info(p
->two
);
2630 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2631 fill_mmfile(&mf2
, p
->two
) < 0)
2632 return error("unable to read files to diff");
2634 /* Maybe hash p->two? into the patch id? */
2635 if (mmfile_is_binary(&mf2
))
2638 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2639 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2640 if (p
->one
->mode
== 0)
2641 len1
= snprintf(buffer
, sizeof(buffer
),
2642 "diff--gita/%.*sb/%.*s"
2649 len2
, p
->two
->path
);
2650 else if (p
->two
->mode
== 0)
2651 len1
= snprintf(buffer
, sizeof(buffer
),
2652 "diff--gita/%.*sb/%.*s"
2653 "deletedfilemode%06o"
2659 len1
, p
->one
->path
);
2661 len1
= snprintf(buffer
, sizeof(buffer
),
2662 "diff--gita/%.*sb/%.*s"
2668 len2
, p
->two
->path
);
2669 SHA1_Update(&ctx
, buffer
, len1
);
2671 xpp
.flags
= XDF_NEED_MINIMAL
;
2673 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2674 ecb
.outf
= xdiff_outf
;
2676 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2679 SHA1_Final(sha1
, &ctx
);
2683 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2685 struct diff_queue_struct
*q
= &diff_queued_diff
;
2687 int result
= diff_get_patch_id(options
, sha1
);
2689 for (i
= 0; i
< q
->nr
; i
++)
2690 diff_free_filepair(q
->queue
[i
]);
2694 q
->nr
= q
->alloc
= 0;
2699 static int is_summary_empty(const struct diff_queue_struct
*q
)
2703 for (i
= 0; i
< q
->nr
; i
++) {
2704 const struct diff_filepair
*p
= q
->queue
[i
];
2706 switch (p
->status
) {
2707 case DIFF_STATUS_DELETED
:
2708 case DIFF_STATUS_ADDED
:
2709 case DIFF_STATUS_COPIED
:
2710 case DIFF_STATUS_RENAMED
:
2715 if (p
->one
->mode
&& p
->two
->mode
&&
2716 p
->one
->mode
!= p
->two
->mode
)
2724 void diff_flush(struct diff_options
*options
)
2726 struct diff_queue_struct
*q
= &diff_queued_diff
;
2727 int i
, output_format
= options
->output_format
;
2731 * Order: raw, stat, summary, patch
2732 * or: name/name-status/checkdiff (other bits clear)
2737 if (output_format
& (DIFF_FORMAT_RAW
|
2739 DIFF_FORMAT_NAME_STATUS
|
2740 DIFF_FORMAT_CHECKDIFF
)) {
2741 for (i
= 0; i
< q
->nr
; i
++) {
2742 struct diff_filepair
*p
= q
->queue
[i
];
2743 if (check_pair_status(p
))
2744 flush_one_pair(p
, options
);
2749 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
2750 struct diffstat_t diffstat
;
2752 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2753 diffstat
.xm
.consume
= diffstat_consume
;
2754 for (i
= 0; i
< q
->nr
; i
++) {
2755 struct diff_filepair
*p
= q
->queue
[i
];
2756 if (check_pair_status(p
))
2757 diff_flush_stat(p
, options
, &diffstat
);
2759 if (output_format
& DIFF_FORMAT_NUMSTAT
)
2760 show_numstat(&diffstat
, options
);
2761 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
2762 show_stats(&diffstat
, options
);
2763 else if (output_format
& DIFF_FORMAT_SHORTSTAT
)
2764 show_shortstats(&diffstat
);
2768 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2769 for (i
= 0; i
< q
->nr
; i
++)
2770 diff_summary(q
->queue
[i
]);
2774 if (output_format
& DIFF_FORMAT_PATCH
) {
2776 if (options
->stat_sep
) {
2777 /* attach patch instead of inline */
2778 fputs(options
->stat_sep
, stdout
);
2780 putchar(options
->line_termination
);
2784 for (i
= 0; i
< q
->nr
; i
++) {
2785 struct diff_filepair
*p
= q
->queue
[i
];
2786 if (check_pair_status(p
))
2787 diff_flush_patch(p
, options
);
2791 if (output_format
& DIFF_FORMAT_CALLBACK
)
2792 options
->format_callback(q
, options
, options
->format_callback_data
);
2794 for (i
= 0; i
< q
->nr
; i
++)
2795 diff_free_filepair(q
->queue
[i
]);
2799 q
->nr
= q
->alloc
= 0;
2802 static void diffcore_apply_filter(const char *filter
)
2805 struct diff_queue_struct
*q
= &diff_queued_diff
;
2806 struct diff_queue_struct outq
;
2808 outq
.nr
= outq
.alloc
= 0;
2813 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2815 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2816 struct diff_filepair
*p
= q
->queue
[i
];
2817 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2819 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2821 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2822 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2823 strchr(filter
, p
->status
)))
2829 /* otherwise we will clear the whole queue
2830 * by copying the empty outq at the end of this
2831 * function, but first clear the current entries
2834 for (i
= 0; i
< q
->nr
; i
++)
2835 diff_free_filepair(q
->queue
[i
]);
2838 /* Only the matching ones */
2839 for (i
= 0; i
< q
->nr
; i
++) {
2840 struct diff_filepair
*p
= q
->queue
[i
];
2842 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2844 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2846 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2847 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2848 strchr(filter
, p
->status
)))
2851 diff_free_filepair(p
);
2858 void diffcore_std(struct diff_options
*options
)
2860 if (options
->break_opt
!= -1)
2861 diffcore_break(options
->break_opt
);
2862 if (options
->detect_rename
)
2863 diffcore_rename(options
);
2864 if (options
->break_opt
!= -1)
2865 diffcore_merge_broken();
2866 if (options
->pickaxe
)
2867 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2868 if (options
->orderfile
)
2869 diffcore_order(options
->orderfile
);
2870 diff_resolve_rename_copy();
2871 diffcore_apply_filter(options
->filter
);
2875 void diffcore_std_no_resolve(struct diff_options
*options
)
2877 if (options
->pickaxe
)
2878 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2879 if (options
->orderfile
)
2880 diffcore_order(options
->orderfile
);
2881 diffcore_apply_filter(options
->filter
);
2884 void diff_addremove(struct diff_options
*options
,
2885 int addremove
, unsigned mode
,
2886 const unsigned char *sha1
,
2887 const char *base
, const char *path
)
2889 char concatpath
[PATH_MAX
];
2890 struct diff_filespec
*one
, *two
;
2892 /* This may look odd, but it is a preparation for
2893 * feeding "there are unchanged files which should
2894 * not produce diffs, but when you are doing copy
2895 * detection you would need them, so here they are"
2896 * entries to the diff-core. They will be prefixed
2897 * with something like '=' or '*' (I haven't decided
2898 * which but should not make any difference).
2899 * Feeding the same new and old to diff_change()
2900 * also has the same effect.
2901 * Before the final output happens, they are pruned after
2902 * merged into rename/copy pairs as appropriate.
2904 if (options
->reverse_diff
)
2905 addremove
= (addremove
== '+' ? '-' :
2906 addremove
== '-' ? '+' : addremove
);
2908 if (!path
) path
= "";
2909 sprintf(concatpath
, "%s%s", base
, path
);
2910 one
= alloc_filespec(concatpath
);
2911 two
= alloc_filespec(concatpath
);
2913 if (addremove
!= '+')
2914 fill_filespec(one
, sha1
, mode
);
2915 if (addremove
!= '-')
2916 fill_filespec(two
, sha1
, mode
);
2918 diff_queue(&diff_queued_diff
, one
, two
);
2921 void diff_change(struct diff_options
*options
,
2922 unsigned old_mode
, unsigned new_mode
,
2923 const unsigned char *old_sha1
,
2924 const unsigned char *new_sha1
,
2925 const char *base
, const char *path
)
2927 char concatpath
[PATH_MAX
];
2928 struct diff_filespec
*one
, *two
;
2930 if (options
->reverse_diff
) {
2932 const unsigned char *tmp_c
;
2933 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2934 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2936 if (!path
) path
= "";
2937 sprintf(concatpath
, "%s%s", base
, path
);
2938 one
= alloc_filespec(concatpath
);
2939 two
= alloc_filespec(concatpath
);
2940 fill_filespec(one
, old_sha1
, old_mode
);
2941 fill_filespec(two
, new_sha1
, new_mode
);
2943 diff_queue(&diff_queued_diff
, one
, two
);
2946 void diff_unmerge(struct diff_options
*options
,
2948 unsigned mode
, const unsigned char *sha1
)
2950 struct diff_filespec
*one
, *two
;
2951 one
= alloc_filespec(path
);
2952 two
= alloc_filespec(path
);
2953 fill_filespec(one
, sha1
, mode
);
2954 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;