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 diff_populate_filespec(one
, 0);
211 diff_populate_filespec(two
, 0);
212 lc_a
= count_lines(one
->data
, one
->size
);
213 lc_b
= count_lines(two
->data
, two
->size
);
214 printf("--- a/%s\n+++ b/%s\n@@ -", name_a
, name_b
);
215 print_line_count(lc_a
);
217 print_line_count(lc_b
);
220 copy_file('-', one
->data
, one
->size
);
222 copy_file('+', two
->data
, two
->size
);
225 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
227 if (!DIFF_FILE_VALID(one
)) {
228 mf
->ptr
= (char *)""; /* does not matter */
232 else if (diff_populate_filespec(one
, 0))
235 mf
->size
= one
->size
;
239 struct diff_words_buffer
{
242 long current
; /* output pointer */
243 int suppressed_newline
;
246 static void diff_words_append(char *line
, unsigned long len
,
247 struct diff_words_buffer
*buffer
)
249 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
250 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
251 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
255 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
256 buffer
->text
.size
+= len
;
259 struct diff_words_data
{
260 struct xdiff_emit_state xm
;
261 struct diff_words_buffer minus
, plus
;
264 static void print_word(struct diff_words_buffer
*buffer
, int len
, int color
,
265 int suppress_newline
)
273 ptr
= buffer
->text
.ptr
+ buffer
->current
;
274 buffer
->current
+= len
;
276 if (ptr
[len
- 1] == '\n') {
281 fputs(diff_get_color(1, color
), stdout
);
282 fwrite(ptr
, len
, 1, stdout
);
283 fputs(diff_get_color(1, DIFF_RESET
), stdout
);
286 if (suppress_newline
)
287 buffer
->suppressed_newline
= 1;
293 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
295 struct diff_words_data
*diff_words
= priv
;
297 if (diff_words
->minus
.suppressed_newline
) {
300 diff_words
->minus
.suppressed_newline
= 0;
306 print_word(&diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
309 print_word(&diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
312 print_word(&diff_words
->plus
, len
, DIFF_PLAIN
, 0);
313 diff_words
->minus
.current
+= len
;
318 /* this executes the word diff on the accumulated buffers */
319 static void diff_words_show(struct diff_words_data
*diff_words
)
324 mmfile_t minus
, plus
;
327 minus
.size
= diff_words
->minus
.text
.size
;
328 minus
.ptr
= xmalloc(minus
.size
);
329 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
330 for (i
= 0; i
< minus
.size
; i
++)
331 if (isspace(minus
.ptr
[i
]))
333 diff_words
->minus
.current
= 0;
335 plus
.size
= diff_words
->plus
.text
.size
;
336 plus
.ptr
= xmalloc(plus
.size
);
337 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
338 for (i
= 0; i
< plus
.size
; i
++)
339 if (isspace(plus
.ptr
[i
]))
341 diff_words
->plus
.current
= 0;
343 xpp
.flags
= XDF_NEED_MINIMAL
;
344 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
346 ecb
.outf
= xdiff_outf
;
347 ecb
.priv
= diff_words
;
348 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
349 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
353 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
355 if (diff_words
->minus
.suppressed_newline
) {
357 diff_words
->minus
.suppressed_newline
= 0;
361 struct emit_callback
{
362 struct xdiff_emit_state xm
;
363 int nparents
, color_diff
;
364 const char **label_path
;
365 struct diff_words_data
*diff_words
;
368 static void free_diff_words_data(struct emit_callback
*ecbdata
)
370 if (ecbdata
->diff_words
) {
372 if (ecbdata
->diff_words
->minus
.text
.size
||
373 ecbdata
->diff_words
->plus
.text
.size
)
374 diff_words_show(ecbdata
->diff_words
);
376 if (ecbdata
->diff_words
->minus
.text
.ptr
)
377 free (ecbdata
->diff_words
->minus
.text
.ptr
);
378 if (ecbdata
->diff_words
->plus
.text
.ptr
)
379 free (ecbdata
->diff_words
->plus
.text
.ptr
);
380 free(ecbdata
->diff_words
);
381 ecbdata
->diff_words
= NULL
;
385 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
388 return diff_colors
[ix
];
392 static void emit_line(const char *set
, const char *reset
, const char *line
, int len
)
394 if (len
> 0 && line
[len
-1] == '\n')
397 fwrite(line
, len
, 1, stdout
);
401 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
403 int col0
= ecbdata
->nparents
;
404 int last_tab_in_indent
= -1;
405 int last_space_in_indent
= -1;
408 int need_highlight_leading_space
= 0;
409 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
410 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
413 emit_line(set
, reset
, line
, len
);
417 /* The line is a newly added line. Does it have funny leading
418 * whitespaces? In indent, SP should never precede a TAB.
420 for (i
= col0
; i
< len
; i
++) {
421 if (line
[i
] == '\t') {
422 last_tab_in_indent
= i
;
423 if (0 <= last_space_in_indent
)
424 need_highlight_leading_space
= 1;
426 else if (line
[i
] == ' ')
427 last_space_in_indent
= i
;
432 fwrite(line
, col0
, 1, stdout
);
433 fputs(reset
, stdout
);
434 if (((i
== len
) || line
[i
] == '\n') && i
!= col0
) {
435 /* The whole line was indent */
436 emit_line(ws
, reset
, line
+ col0
, len
- col0
);
440 if (need_highlight_leading_space
) {
441 while (i
< last_tab_in_indent
) {
442 if (line
[i
] == ' ') {
445 fputs(reset
, stdout
);
453 if (line
[tail
] == '\n' && i
< tail
)
456 if (!isspace(line
[tail
]))
460 if ((i
< tail
&& line
[tail
+ 1] != '\n')) {
461 /* This has whitespace between tail+1..len */
463 fwrite(line
+ i
, tail
- i
+ 1, 1, stdout
);
464 fputs(reset
, stdout
);
465 emit_line(ws
, reset
, line
+ tail
+ 1, len
- tail
- 1);
468 emit_line(set
, reset
, line
+ i
, len
- i
);
471 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
475 struct emit_callback
*ecbdata
= priv
;
476 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
477 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
479 if (ecbdata
->label_path
[0]) {
480 printf("%s--- %s%s\n", set
, ecbdata
->label_path
[0], reset
);
481 printf("%s+++ %s%s\n", set
, ecbdata
->label_path
[1], reset
);
482 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
485 /* This is not really necessary for now because
486 * this codepath only deals with two-way diffs.
488 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
490 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
491 ecbdata
->nparents
= i
- 1;
492 emit_line(diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
497 if (len
< ecbdata
->nparents
) {
499 emit_line(reset
, reset
, line
, len
);
504 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
505 /* fall back to normal diff */
506 free_diff_words_data(ecbdata
);
507 if (ecbdata
->diff_words
) {
508 if (line
[0] == '-') {
509 diff_words_append(line
, len
,
510 &ecbdata
->diff_words
->minus
);
512 } else if (line
[0] == '+') {
513 diff_words_append(line
, len
,
514 &ecbdata
->diff_words
->plus
);
517 if (ecbdata
->diff_words
->minus
.text
.size
||
518 ecbdata
->diff_words
->plus
.text
.size
)
519 diff_words_show(ecbdata
->diff_words
);
522 emit_line(set
, reset
, line
, len
);
525 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
527 color
= DIFF_FILE_OLD
;
528 else if (line
[i
] == '+')
529 color
= DIFF_FILE_NEW
;
532 if (color
!= DIFF_FILE_NEW
) {
533 emit_line(diff_get_color(ecbdata
->color_diff
, color
),
537 emit_add_line(reset
, ecbdata
, line
, len
);
540 static char *pprint_rename(const char *a
, const char *b
)
545 int pfx_length
, sfx_length
;
546 int len_a
= strlen(a
);
547 int len_b
= strlen(b
);
549 /* Find common prefix */
551 while (*old
&& *new && *old
== *new) {
553 pfx_length
= old
- a
+ 1;
558 /* Find common suffix */
562 while (a
<= old
&& b
<= new && *old
== *new) {
564 sfx_length
= len_a
- (old
- a
);
570 * pfx{mid-a => mid-b}sfx
571 * {pfx-a => pfx-b}sfx
572 * pfx{sfx-a => sfx-b}
575 if (pfx_length
+ sfx_length
) {
576 int a_midlen
= len_a
- pfx_length
- sfx_length
;
577 int b_midlen
= len_b
- pfx_length
- sfx_length
;
578 if (a_midlen
< 0) a_midlen
= 0;
579 if (b_midlen
< 0) b_midlen
= 0;
581 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
582 sprintf(name
, "%.*s{%.*s => %.*s}%s",
584 a_midlen
, a
+ pfx_length
,
585 b_midlen
, b
+ pfx_length
,
586 a
+ len_a
- sfx_length
);
589 name
= xmalloc(len_a
+ len_b
+ 5);
590 sprintf(name
, "%s => %s", a
, b
);
596 struct xdiff_emit_state xm
;
600 struct diffstat_file
{
602 unsigned is_unmerged
:1;
603 unsigned is_binary
:1;
604 unsigned is_renamed
:1;
605 unsigned int added
, deleted
;
609 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
613 struct diffstat_file
*x
;
614 x
= xcalloc(sizeof (*x
), 1);
615 if (diffstat
->nr
== diffstat
->alloc
) {
616 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
617 diffstat
->files
= xrealloc(diffstat
->files
,
618 diffstat
->alloc
* sizeof(x
));
620 diffstat
->files
[diffstat
->nr
++] = x
;
622 x
->name
= pprint_rename(name_a
, name_b
);
626 x
->name
= xstrdup(name_a
);
630 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
632 struct diffstat_t
*diffstat
= priv
;
633 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
637 else if (line
[0] == '-')
641 const char mime_boundary_leader
[] = "------------";
643 static int scale_linear(int it
, int width
, int max_change
)
646 * make sure that at least one '-' is printed if there were deletions,
647 * and likewise for '+'.
651 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
654 static void show_name(const char *prefix
, const char *name
, int len
,
655 const char *reset
, const char *set
)
657 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
660 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
670 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
672 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
673 int max_change
= 0, max_len
= 0;
674 int total_files
= data
->nr
;
675 int width
, name_width
;
676 const char *reset
, *set
, *add_c
, *del_c
;
681 width
= options
->stat_width
? options
->stat_width
: 80;
682 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
684 /* Sanity: give at least 5 columns to the graph,
685 * but leave at least 10 columns for the name.
687 if (width
< name_width
+ 15) {
688 if (name_width
<= 25)
689 width
= name_width
+ 15;
691 name_width
= width
- 15;
694 /* Find the longest filename and max number of changes */
695 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
696 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
697 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
698 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
700 for (i
= 0; i
< data
->nr
; i
++) {
701 struct diffstat_file
*file
= data
->files
[i
];
702 int change
= file
->added
+ file
->deleted
;
704 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
706 char *qname
= xmalloc(len
+ 1);
707 quote_c_style(file
->name
, qname
, NULL
, 0);
712 len
= strlen(file
->name
);
716 if (file
->is_binary
|| file
->is_unmerged
)
718 if (max_change
< change
)
722 /* Compute the width of the graph part;
723 * 10 is for one blank at the beginning of the line plus
724 * " | count " between the name and the graph.
726 * From here on, name_width is the width of the name area,
727 * and width is the width of the graph area.
729 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
730 if (width
< (name_width
+ 10) + max_change
)
731 width
= width
- (name_width
+ 10);
735 for (i
= 0; i
< data
->nr
; i
++) {
736 const char *prefix
= "";
737 char *name
= data
->files
[i
]->name
;
738 int added
= data
->files
[i
]->added
;
739 int deleted
= data
->files
[i
]->deleted
;
743 * "scale" the filename
746 name_len
= strlen(name
);
747 if (name_width
< name_len
) {
751 name
+= name_len
- len
;
752 slash
= strchr(name
, '/');
757 if (data
->files
[i
]->is_binary
) {
758 show_name(prefix
, name
, len
, reset
, set
);
760 goto free_diffstat_file
;
762 else if (data
->files
[i
]->is_unmerged
) {
763 show_name(prefix
, name
, len
, reset
, set
);
764 printf(" Unmerged\n");
765 goto free_diffstat_file
;
767 else if (!data
->files
[i
]->is_renamed
&&
768 (added
+ deleted
== 0)) {
770 goto free_diffstat_file
;
774 * scale the add/delete
782 if (width
<= max_change
) {
783 add
= scale_linear(add
, width
, max_change
);
784 del
= scale_linear(del
, width
, max_change
);
787 show_name(prefix
, name
, len
, reset
, set
);
788 printf("%5d ", added
+ deleted
);
789 show_graph('+', add
, add_c
, reset
);
790 show_graph('-', del
, del_c
, reset
);
793 free(data
->files
[i
]->name
);
794 free(data
->files
[i
]);
797 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
798 set
, total_files
, adds
, dels
, reset
);
801 static void show_shortstats(struct diffstat_t
* data
)
803 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
808 for (i
= 0; i
< data
->nr
; i
++) {
809 if (!data
->files
[i
]->is_binary
&&
810 !data
->files
[i
]->is_unmerged
) {
811 int added
= data
->files
[i
]->added
;
812 int deleted
= data
->files
[i
]->deleted
;
813 if (!data
->files
[i
]->is_renamed
&&
814 (added
+ deleted
== 0)) {
821 free(data
->files
[i
]->name
);
822 free(data
->files
[i
]);
826 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
827 total_files
, adds
, dels
);
830 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
834 for (i
= 0; i
< data
->nr
; i
++) {
835 struct diffstat_file
*file
= data
->files
[i
];
840 printf("%d\t%d\t", file
->added
, file
->deleted
);
841 if (options
->line_termination
&&
842 quote_c_style(file
->name
, NULL
, NULL
, 0))
843 quote_c_style(file
->name
, NULL
, stdout
, 0);
845 fputs(file
->name
, stdout
);
846 putchar(options
->line_termination
);
851 struct xdiff_emit_state xm
;
852 const char *filename
;
856 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
858 struct checkdiff_t
*data
= priv
;
860 if (line
[0] == '+') {
863 /* check space before tab */
864 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
867 if (line
[i
- 1] == '\t' && spaces
)
868 printf("%s:%d: space before tab:%.*s\n",
869 data
->filename
, data
->lineno
, (int)len
, line
);
871 /* check white space at line end */
872 if (line
[len
- 1] == '\n')
874 if (isspace(line
[len
- 1]))
875 printf("%s:%d: white space at end: %.*s\n",
876 data
->filename
, data
->lineno
, (int)len
, line
);
879 } else if (line
[0] == ' ')
881 else if (line
[0] == '@') {
882 char *plus
= strchr(line
, '+');
884 data
->lineno
= strtol(plus
, NULL
, 10);
890 static unsigned char *deflate_it(char *data
,
892 unsigned long *result_size
)
895 unsigned char *deflated
;
898 memset(&stream
, 0, sizeof(stream
));
899 deflateInit(&stream
, zlib_compression_level
);
900 bound
= deflateBound(&stream
, size
);
901 deflated
= xmalloc(bound
);
902 stream
.next_out
= deflated
;
903 stream
.avail_out
= bound
;
905 stream
.next_in
= (unsigned char *)data
;
906 stream
.avail_in
= size
;
907 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
910 *result_size
= stream
.total_out
;
914 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
920 unsigned long orig_size
;
921 unsigned long delta_size
;
922 unsigned long deflate_size
;
923 unsigned long data_size
;
925 /* We could do deflated delta, or we could do just deflated two,
926 * whichever is smaller.
929 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
930 if (one
->size
&& two
->size
) {
931 delta
= diff_delta(one
->ptr
, one
->size
,
933 &delta_size
, deflate_size
);
935 void *to_free
= delta
;
936 orig_size
= delta_size
;
937 delta
= deflate_it(delta
, delta_size
, &delta_size
);
942 if (delta
&& delta_size
< deflate_size
) {
943 printf("delta %lu\n", orig_size
);
946 data_size
= delta_size
;
949 printf("literal %lu\n", two
->size
);
952 data_size
= deflate_size
;
955 /* emit data encoded in base85 */
958 int bytes
= (52 < data_size
) ? 52 : data_size
;
962 line
[0] = bytes
+ 'A' - 1;
964 line
[0] = bytes
- 26 + 'a' - 1;
965 encode_85(line
+ 1, cp
, bytes
);
966 cp
= (char *) cp
+ bytes
;
973 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
975 printf("GIT binary patch\n");
976 emit_binary_diff_body(one
, two
);
977 emit_binary_diff_body(two
, one
);
980 #define FIRST_FEW_BYTES 8000
981 static int mmfile_is_binary(mmfile_t
*mf
)
984 if (FIRST_FEW_BYTES
< sz
)
985 sz
= FIRST_FEW_BYTES
;
986 return !!memchr(mf
->ptr
, 0, sz
);
989 static void builtin_diff(const char *name_a
,
991 struct diff_filespec
*one
,
992 struct diff_filespec
*two
,
993 const char *xfrm_msg
,
994 struct diff_options
*o
,
995 int complete_rewrite
)
1000 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
1001 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
1003 a_one
= quote_two("a/", name_a
);
1004 b_two
= quote_two("b/", name_b
);
1005 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1006 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1007 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1008 if (lbl
[0][0] == '/') {
1010 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1011 if (xfrm_msg
&& xfrm_msg
[0])
1012 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1014 else if (lbl
[1][0] == '/') {
1015 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1016 if (xfrm_msg
&& xfrm_msg
[0])
1017 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1020 if (one
->mode
!= two
->mode
) {
1021 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
1022 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
1024 if (xfrm_msg
&& xfrm_msg
[0])
1025 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1027 * we do not run diff between different kind
1030 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1031 goto free_ab_and_return
;
1032 if (complete_rewrite
) {
1033 emit_rewrite_diff(name_a
, name_b
, one
, two
);
1034 goto free_ab_and_return
;
1038 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1039 die("unable to read files to diff");
1041 if (!o
->text
&& (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))) {
1042 /* Quite common confusing case */
1043 if (mf1
.size
== mf2
.size
&&
1044 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1045 goto free_ab_and_return
;
1047 emit_binary_diff(&mf1
, &mf2
);
1049 printf("Binary files %s and %s differ\n",
1053 /* Crazy xdl interfaces.. */
1054 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1058 struct emit_callback ecbdata
;
1060 memset(&ecbdata
, 0, sizeof(ecbdata
));
1061 ecbdata
.label_path
= lbl
;
1062 ecbdata
.color_diff
= o
->color_diff
;
1063 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1064 xecfg
.ctxlen
= o
->context
;
1065 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1068 else if (!strncmp(diffopts
, "--unified=", 10))
1069 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1070 else if (!strncmp(diffopts
, "-u", 2))
1071 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1072 ecb
.outf
= xdiff_outf
;
1073 ecb
.priv
= &ecbdata
;
1074 ecbdata
.xm
.consume
= fn_out_consume
;
1075 if (o
->color_diff_words
)
1076 ecbdata
.diff_words
=
1077 xcalloc(1, sizeof(struct diff_words_data
));
1078 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1079 if (o
->color_diff_words
)
1080 free_diff_words_data(&ecbdata
);
1089 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1090 struct diff_filespec
*one
,
1091 struct diff_filespec
*two
,
1092 struct diffstat_t
*diffstat
,
1093 struct diff_options
*o
,
1094 int complete_rewrite
)
1097 struct diffstat_file
*data
;
1099 data
= diffstat_add(diffstat
, name_a
, name_b
);
1102 data
->is_unmerged
= 1;
1105 if (complete_rewrite
) {
1106 diff_populate_filespec(one
, 0);
1107 diff_populate_filespec(two
, 0);
1108 data
->deleted
= count_lines(one
->data
, one
->size
);
1109 data
->added
= count_lines(two
->data
, two
->size
);
1112 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1113 die("unable to read files to diff");
1115 if (mmfile_is_binary(&mf1
) || mmfile_is_binary(&mf2
))
1116 data
->is_binary
= 1;
1118 /* Crazy xdl interfaces.. */
1123 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1126 ecb
.outf
= xdiff_outf
;
1127 ecb
.priv
= diffstat
;
1128 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1132 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1133 struct diff_filespec
*one
,
1134 struct diff_filespec
*two
)
1137 struct checkdiff_t data
;
1142 memset(&data
, 0, sizeof(data
));
1143 data
.xm
.consume
= checkdiff_consume
;
1144 data
.filename
= name_b
? name_b
: name_a
;
1147 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1148 die("unable to read files to diff");
1150 if (mmfile_is_binary(&mf2
))
1153 /* Crazy xdl interfaces.. */
1158 xpp
.flags
= XDF_NEED_MINIMAL
;
1161 ecb
.outf
= xdiff_outf
;
1163 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1167 struct diff_filespec
*alloc_filespec(const char *path
)
1169 int namelen
= strlen(path
);
1170 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1172 memset(spec
, 0, sizeof(*spec
));
1173 spec
->path
= (char *)(spec
+ 1);
1174 memcpy(spec
->path
, path
, namelen
+1);
1178 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1179 unsigned short mode
)
1182 spec
->mode
= canon_mode(mode
);
1183 hashcpy(spec
->sha1
, sha1
);
1184 spec
->sha1_valid
= !is_null_sha1(sha1
);
1189 * Given a name and sha1 pair, if the dircache tells us the file in
1190 * the work tree has that object contents, return true, so that
1191 * prepare_temp_file() does not have to inflate and extract.
1193 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1195 struct cache_entry
*ce
;
1199 /* We do not read the cache ourselves here, because the
1200 * benchmark with my previous version that always reads cache
1201 * shows that it makes things worse for diff-tree comparing
1202 * two linux-2.6 kernel trees in an already checked out work
1203 * tree. This is because most diff-tree comparisons deal with
1204 * only a small number of files, while reading the cache is
1205 * expensive for a large project, and its cost outweighs the
1206 * savings we get by not inflating the object to a temporary
1207 * file. Practically, this code only helps when we are used
1208 * by diff-cache --cached, which does read the cache before
1214 /* We want to avoid the working directory if our caller
1215 * doesn't need the data in a normal file, this system
1216 * is rather slow with its stat/open/mmap/close syscalls,
1217 * and the object is contained in a pack file. The pack
1218 * is probably already open and will be faster to obtain
1219 * the data through than the working directory. Loose
1220 * objects however would tend to be slower as they need
1221 * to be individually opened and inflated.
1223 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1227 pos
= cache_name_pos(name
, len
);
1230 ce
= active_cache
[pos
];
1231 if ((lstat(name
, &st
) < 0) ||
1232 !S_ISREG(st
.st_mode
) || /* careful! */
1233 ce_match_stat(ce
, &st
, 0) ||
1234 hashcmp(sha1
, ce
->sha1
))
1236 /* we return 1 only when we can stat, it is a regular file,
1237 * stat information matches, and sha1 recorded in the cache
1238 * matches. I.e. we know the file in the work tree really is
1239 * the same as the <name, sha1> pair.
1244 static struct sha1_size_cache
{
1245 unsigned char sha1
[20];
1247 } **sha1_size_cache
;
1248 static int sha1_size_cache_nr
, sha1_size_cache_alloc
;
1250 static struct sha1_size_cache
*locate_size_cache(unsigned char *sha1
,
1255 struct sha1_size_cache
*e
;
1258 last
= sha1_size_cache_nr
;
1259 while (last
> first
) {
1260 int cmp
, next
= (last
+ first
) >> 1;
1261 e
= sha1_size_cache
[next
];
1262 cmp
= hashcmp(e
->sha1
, sha1
);
1274 /* insert to make it at "first" */
1275 if (sha1_size_cache_alloc
<= sha1_size_cache_nr
) {
1276 sha1_size_cache_alloc
= alloc_nr(sha1_size_cache_alloc
);
1277 sha1_size_cache
= xrealloc(sha1_size_cache
,
1278 sha1_size_cache_alloc
*
1279 sizeof(*sha1_size_cache
));
1281 sha1_size_cache_nr
++;
1282 if (first
< sha1_size_cache_nr
)
1283 memmove(sha1_size_cache
+ first
+ 1, sha1_size_cache
+ first
,
1284 (sha1_size_cache_nr
- first
- 1) *
1285 sizeof(*sha1_size_cache
));
1286 e
= xmalloc(sizeof(struct sha1_size_cache
));
1287 sha1_size_cache
[first
] = e
;
1288 hashcpy(e
->sha1
, sha1
);
1294 * While doing rename detection and pickaxe operation, we may need to
1295 * grab the data for the blob (or file) for our own in-core comparison.
1296 * diff_filespec has data and size fields for this purpose.
1298 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1301 if (!DIFF_FILE_VALID(s
))
1302 die("internal error: asking to populate invalid file.");
1303 if (S_ISDIR(s
->mode
))
1306 if (!use_size_cache
)
1311 if (!s
->sha1_valid
||
1312 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1315 if (lstat(s
->path
, &st
) < 0) {
1316 if (errno
== ENOENT
) {
1320 s
->data
= (char *)"";
1325 s
->size
= st
.st_size
;
1330 if (S_ISLNK(st
.st_mode
)) {
1332 s
->data
= xmalloc(s
->size
);
1334 ret
= readlink(s
->path
, s
->data
, s
->size
);
1341 fd
= open(s
->path
, O_RDONLY
);
1344 s
->data
= mmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1346 if (s
->data
== MAP_FAILED
)
1348 s
->should_munmap
= 1;
1352 struct sha1_size_cache
*e
;
1355 e
= locate_size_cache(s
->sha1
, 1, 0);
1360 if (!sha1_object_info(s
->sha1
, type
, &s
->size
))
1361 locate_size_cache(s
->sha1
, 0, s
->size
);
1364 s
->data
= read_sha1_file(s
->sha1
, type
, &s
->size
);
1371 void diff_free_filespec_data(struct diff_filespec
*s
)
1375 else if (s
->should_munmap
)
1376 munmap(s
->data
, s
->size
);
1377 s
->should_free
= s
->should_munmap
= 0;
1383 static void prep_temp_blob(struct diff_tempfile
*temp
,
1386 const unsigned char *sha1
,
1391 fd
= git_mkstemp(temp
->tmp_path
, TEMPFILE_PATH_LEN
, ".diff_XXXXXX");
1393 die("unable to create temp-file");
1394 if (write(fd
, blob
, size
) != size
)
1395 die("unable to write temp-file");
1397 temp
->name
= temp
->tmp_path
;
1398 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1400 sprintf(temp
->mode
, "%06o", mode
);
1403 static void prepare_temp_file(const char *name
,
1404 struct diff_tempfile
*temp
,
1405 struct diff_filespec
*one
)
1407 if (!DIFF_FILE_VALID(one
)) {
1409 /* A '-' entry produces this for file-2, and
1410 * a '+' entry produces this for file-1.
1412 temp
->name
= "/dev/null";
1413 strcpy(temp
->hex
, ".");
1414 strcpy(temp
->mode
, ".");
1418 if (!one
->sha1_valid
||
1419 reuse_worktree_file(name
, one
->sha1
, 1)) {
1421 if (lstat(name
, &st
) < 0) {
1422 if (errno
== ENOENT
)
1423 goto not_a_valid_file
;
1424 die("stat(%s): %s", name
, strerror(errno
));
1426 if (S_ISLNK(st
.st_mode
)) {
1428 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1429 if (sizeof(buf
) <= st
.st_size
)
1430 die("symlink too long: %s", name
);
1431 ret
= readlink(name
, buf
, st
.st_size
);
1433 die("readlink(%s)", name
);
1434 prep_temp_blob(temp
, buf
, st
.st_size
,
1436 one
->sha1
: null_sha1
),
1438 one
->mode
: S_IFLNK
));
1441 /* we can borrow from the file in the work tree */
1443 if (!one
->sha1_valid
)
1444 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1446 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1447 /* Even though we may sometimes borrow the
1448 * contents from the work tree, we always want
1449 * one->mode. mode is trustworthy even when
1450 * !(one->sha1_valid), as long as
1451 * DIFF_FILE_VALID(one).
1453 sprintf(temp
->mode
, "%06o", one
->mode
);
1458 if (diff_populate_filespec(one
, 0))
1459 die("cannot read data blob for %s", one
->path
);
1460 prep_temp_blob(temp
, one
->data
, one
->size
,
1461 one
->sha1
, one
->mode
);
1465 static void remove_tempfile(void)
1469 for (i
= 0; i
< 2; i
++)
1470 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1471 unlink(diff_temp
[i
].name
);
1472 diff_temp
[i
].name
= NULL
;
1476 static void remove_tempfile_on_signal(int signo
)
1479 signal(SIGINT
, SIG_DFL
);
1483 static int spawn_prog(const char *pgm
, const char **arg
)
1491 die("unable to fork");
1493 execvp(pgm
, (char *const*) arg
);
1497 while (waitpid(pid
, &status
, 0) < 0) {
1503 /* Earlier we did not check the exit status because
1504 * diff exits non-zero if files are different, and
1505 * we are not interested in knowing that. It was a
1506 * mistake which made it harder to quit a diff-*
1507 * session that uses the git-apply-patch-script as
1508 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1509 * should also exit non-zero only when it wants to
1510 * abort the entire diff-* session.
1512 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1517 /* An external diff command takes:
1519 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1520 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1523 static void run_external_diff(const char *pgm
,
1526 struct diff_filespec
*one
,
1527 struct diff_filespec
*two
,
1528 const char *xfrm_msg
,
1529 int complete_rewrite
)
1531 const char *spawn_arg
[10];
1532 struct diff_tempfile
*temp
= diff_temp
;
1534 static int atexit_asked
= 0;
1535 const char *othername
;
1536 const char **arg
= &spawn_arg
[0];
1538 othername
= (other
? other
: name
);
1540 prepare_temp_file(name
, &temp
[0], one
);
1541 prepare_temp_file(othername
, &temp
[1], two
);
1542 if (! atexit_asked
&&
1543 (temp
[0].name
== temp
[0].tmp_path
||
1544 temp
[1].name
== temp
[1].tmp_path
)) {
1546 atexit(remove_tempfile
);
1548 signal(SIGINT
, remove_tempfile_on_signal
);
1554 *arg
++ = temp
[0].name
;
1555 *arg
++ = temp
[0].hex
;
1556 *arg
++ = temp
[0].mode
;
1557 *arg
++ = temp
[1].name
;
1558 *arg
++ = temp
[1].hex
;
1559 *arg
++ = temp
[1].mode
;
1569 retval
= spawn_prog(pgm
, spawn_arg
);
1572 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1577 static void run_diff_cmd(const char *pgm
,
1580 struct diff_filespec
*one
,
1581 struct diff_filespec
*two
,
1582 const char *xfrm_msg
,
1583 struct diff_options
*o
,
1584 int complete_rewrite
)
1587 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1592 builtin_diff(name
, other
? other
: name
,
1593 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1595 printf("* Unmerged path %s\n", name
);
1598 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1600 if (DIFF_FILE_VALID(one
)) {
1601 if (!one
->sha1_valid
) {
1603 if (lstat(one
->path
, &st
) < 0)
1604 die("stat %s", one
->path
);
1605 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1606 die("cannot hash %s\n", one
->path
);
1613 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1615 const char *pgm
= external_diff();
1616 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1617 struct diff_filespec
*one
;
1618 struct diff_filespec
*two
;
1621 char *name_munged
, *other_munged
;
1622 int complete_rewrite
= 0;
1625 if (DIFF_PAIR_UNMERGED(p
)) {
1627 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1631 name
= p
->one
->path
;
1632 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1633 name_munged
= quote_one(name
);
1634 other_munged
= quote_one(other
);
1635 one
= p
->one
; two
= p
->two
;
1637 diff_fill_sha1_info(one
);
1638 diff_fill_sha1_info(two
);
1641 switch (p
->status
) {
1642 case DIFF_STATUS_COPIED
:
1643 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1644 "similarity index %d%%\n"
1647 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1648 name_munged
, other_munged
);
1650 case DIFF_STATUS_RENAMED
:
1651 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1652 "similarity index %d%%\n"
1655 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
),
1656 name_munged
, other_munged
);
1658 case DIFF_STATUS_MODIFIED
:
1660 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1661 "dissimilarity index %d%%\n",
1662 (int)(0.5 + p
->score
*
1664 complete_rewrite
= 1;
1673 if (hashcmp(one
->sha1
, two
->sha1
)) {
1674 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
1678 if ((!fill_mmfile(&mf
, one
) && mmfile_is_binary(&mf
)) ||
1679 (!fill_mmfile(&mf
, two
) && mmfile_is_binary(&mf
)))
1682 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1684 abbrev
, sha1_to_hex(one
->sha1
),
1685 abbrev
, sha1_to_hex(two
->sha1
));
1686 if (one
->mode
== two
->mode
)
1687 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1688 " %06o", one
->mode
);
1689 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
1694 xfrm_msg
= len
? msg
: NULL
;
1697 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
1698 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
1699 /* a filepair that changes between file and symlink
1700 * needs to be split into deletion and creation.
1702 struct diff_filespec
*null
= alloc_filespec(two
->path
);
1703 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
1705 null
= alloc_filespec(one
->path
);
1706 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
1710 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
1717 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
1718 struct diffstat_t
*diffstat
)
1722 int complete_rewrite
= 0;
1724 if (DIFF_PAIR_UNMERGED(p
)) {
1726 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
1730 name
= p
->one
->path
;
1731 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1733 diff_fill_sha1_info(p
->one
);
1734 diff_fill_sha1_info(p
->two
);
1736 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
1737 complete_rewrite
= 1;
1738 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
1741 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
1746 if (DIFF_PAIR_UNMERGED(p
)) {
1751 name
= p
->one
->path
;
1752 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1754 diff_fill_sha1_info(p
->one
);
1755 diff_fill_sha1_info(p
->two
);
1757 builtin_checkdiff(name
, other
, p
->one
, p
->two
);
1760 void diff_setup(struct diff_options
*options
)
1762 memset(options
, 0, sizeof(*options
));
1763 options
->line_termination
= '\n';
1764 options
->break_opt
= -1;
1765 options
->rename_limit
= -1;
1766 options
->context
= 3;
1767 options
->msg_sep
= "";
1769 options
->change
= diff_change
;
1770 options
->add_remove
= diff_addremove
;
1771 options
->color_diff
= diff_use_color_default
;
1772 options
->detect_rename
= diff_detect_rename_default
;
1775 int diff_setup_done(struct diff_options
*options
)
1779 if (options
->output_format
& DIFF_FORMAT_NAME
)
1781 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
1783 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
1785 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
1788 die("--name-only, --name-status, --check and -s are mutually exclusive");
1790 if (options
->find_copies_harder
)
1791 options
->detect_rename
= DIFF_DETECT_COPY
;
1793 if (options
->output_format
& (DIFF_FORMAT_NAME
|
1794 DIFF_FORMAT_NAME_STATUS
|
1795 DIFF_FORMAT_CHECKDIFF
|
1796 DIFF_FORMAT_NO_OUTPUT
))
1797 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
1798 DIFF_FORMAT_NUMSTAT
|
1799 DIFF_FORMAT_DIFFSTAT
|
1800 DIFF_FORMAT_SHORTSTAT
|
1801 DIFF_FORMAT_SUMMARY
|
1805 * These cases always need recursive; we do not drop caller-supplied
1806 * recursive bits for other formats here.
1808 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
1809 DIFF_FORMAT_NUMSTAT
|
1810 DIFF_FORMAT_DIFFSTAT
|
1811 DIFF_FORMAT_SHORTSTAT
|
1812 DIFF_FORMAT_SUMMARY
|
1813 DIFF_FORMAT_CHECKDIFF
))
1814 options
->recursive
= 1;
1816 * Also pickaxe would not work very well if you do not say recursive
1818 if (options
->pickaxe
)
1819 options
->recursive
= 1;
1821 if (options
->detect_rename
&& options
->rename_limit
< 0)
1822 options
->rename_limit
= diff_rename_limit_default
;
1823 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
1825 /* read-cache does not die even when it fails
1826 * so it is safe for us to do this here. Also
1827 * it does not smudge active_cache or active_nr
1828 * when it fails, so we do not have to worry about
1829 * cleaning it up ourselves either.
1833 if (options
->setup
& DIFF_SETUP_USE_SIZE_CACHE
)
1835 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
1836 options
->abbrev
= 40; /* full */
1841 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
1851 if (c
== arg_short
) {
1855 if (val
&& isdigit(c
)) {
1857 int n
= strtoul(arg
, &end
, 10);
1868 eq
= strchr(arg
, '=');
1873 if (!len
|| strncmp(arg
, arg_long
, len
))
1878 if (!isdigit(*++eq
))
1880 n
= strtoul(eq
, &end
, 10);
1888 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
1890 const char *arg
= av
[0];
1891 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
1892 options
->output_format
|= DIFF_FORMAT_PATCH
;
1893 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
1894 options
->output_format
|= DIFF_FORMAT_PATCH
;
1895 else if (!strcmp(arg
, "--raw"))
1896 options
->output_format
|= DIFF_FORMAT_RAW
;
1897 else if (!strcmp(arg
, "--patch-with-raw")) {
1898 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
1900 else if (!strcmp(arg
, "--numstat")) {
1901 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
1903 else if (!strcmp(arg
, "--shortstat")) {
1904 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
1906 else if (!strncmp(arg
, "--stat", 6)) {
1908 int width
= options
->stat_width
;
1909 int name_width
= options
->stat_name_width
;
1915 if (!strncmp(arg
, "-width=", 7))
1916 width
= strtoul(arg
+ 7, &end
, 10);
1917 else if (!strncmp(arg
, "-name-width=", 12))
1918 name_width
= strtoul(arg
+ 12, &end
, 10);
1921 width
= strtoul(arg
+1, &end
, 10);
1923 name_width
= strtoul(end
+1, &end
, 10);
1926 /* Important! This checks all the error cases! */
1929 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
1930 options
->stat_name_width
= name_width
;
1931 options
->stat_width
= width
;
1933 else if (!strcmp(arg
, "--check"))
1934 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
1935 else if (!strcmp(arg
, "--summary"))
1936 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
1937 else if (!strcmp(arg
, "--patch-with-stat")) {
1938 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
1940 else if (!strcmp(arg
, "-z"))
1941 options
->line_termination
= 0;
1942 else if (!strncmp(arg
, "-l", 2))
1943 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
1944 else if (!strcmp(arg
, "--full-index"))
1945 options
->full_index
= 1;
1946 else if (!strcmp(arg
, "--binary")) {
1947 options
->output_format
|= DIFF_FORMAT_PATCH
;
1948 options
->binary
= 1;
1950 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
1953 else if (!strcmp(arg
, "--name-only"))
1954 options
->output_format
|= DIFF_FORMAT_NAME
;
1955 else if (!strcmp(arg
, "--name-status"))
1956 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
1957 else if (!strcmp(arg
, "-R"))
1958 options
->reverse_diff
= 1;
1959 else if (!strncmp(arg
, "-S", 2))
1960 options
->pickaxe
= arg
+ 2;
1961 else if (!strcmp(arg
, "-s")) {
1962 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
1964 else if (!strncmp(arg
, "-O", 2))
1965 options
->orderfile
= arg
+ 2;
1966 else if (!strncmp(arg
, "--diff-filter=", 14))
1967 options
->filter
= arg
+ 14;
1968 else if (!strcmp(arg
, "--pickaxe-all"))
1969 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
1970 else if (!strcmp(arg
, "--pickaxe-regex"))
1971 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
1972 else if (!strncmp(arg
, "-B", 2)) {
1973 if ((options
->break_opt
=
1974 diff_scoreopt_parse(arg
)) == -1)
1977 else if (!strncmp(arg
, "-M", 2)) {
1978 if ((options
->rename_score
=
1979 diff_scoreopt_parse(arg
)) == -1)
1981 options
->detect_rename
= DIFF_DETECT_RENAME
;
1983 else if (!strncmp(arg
, "-C", 2)) {
1984 if ((options
->rename_score
=
1985 diff_scoreopt_parse(arg
)) == -1)
1987 options
->detect_rename
= DIFF_DETECT_COPY
;
1989 else if (!strcmp(arg
, "--find-copies-harder"))
1990 options
->find_copies_harder
= 1;
1991 else if (!strcmp(arg
, "--abbrev"))
1992 options
->abbrev
= DEFAULT_ABBREV
;
1993 else if (!strncmp(arg
, "--abbrev=", 9)) {
1994 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
1995 if (options
->abbrev
< MINIMUM_ABBREV
)
1996 options
->abbrev
= MINIMUM_ABBREV
;
1997 else if (40 < options
->abbrev
)
1998 options
->abbrev
= 40;
2000 else if (!strcmp(arg
, "--color"))
2001 options
->color_diff
= 1;
2002 else if (!strcmp(arg
, "--no-color"))
2003 options
->color_diff
= 0;
2004 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2005 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2006 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2007 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2008 else if (!strcmp(arg
, "--color-words"))
2009 options
->color_diff
= options
->color_diff_words
= 1;
2010 else if (!strcmp(arg
, "--no-renames"))
2011 options
->detect_rename
= 0;
2017 static int parse_num(const char **cp_p
)
2019 unsigned long num
, scale
;
2021 const char *cp
= *cp_p
;
2028 if ( !dot
&& ch
== '.' ) {
2031 } else if ( ch
== '%' ) {
2032 scale
= dot
? scale
*100 : 100;
2033 cp
++; /* % is always at the end */
2035 } else if ( ch
>= '0' && ch
<= '9' ) {
2036 if ( scale
< 100000 ) {
2038 num
= (num
*10) + (ch
-'0');
2047 /* user says num divided by scale and we say internally that
2048 * is MAX_SCORE * num / scale.
2050 return (num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
);
2053 int diff_scoreopt_parse(const char *opt
)
2055 int opt1
, opt2
, cmd
;
2060 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2061 return -1; /* that is not a -M, -C nor -B option */
2063 opt1
= parse_num(&opt
);
2069 else if (*opt
!= '/')
2070 return -1; /* we expect -B80/99 or -B80 */
2073 opt2
= parse_num(&opt
);
2078 return opt1
| (opt2
<< 16);
2081 struct diff_queue_struct diff_queued_diff
;
2083 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2085 if (queue
->alloc
<= queue
->nr
) {
2086 queue
->alloc
= alloc_nr(queue
->alloc
);
2087 queue
->queue
= xrealloc(queue
->queue
,
2088 sizeof(dp
) * queue
->alloc
);
2090 queue
->queue
[queue
->nr
++] = dp
;
2093 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2094 struct diff_filespec
*one
,
2095 struct diff_filespec
*two
)
2097 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2105 void diff_free_filepair(struct diff_filepair
*p
)
2107 diff_free_filespec_data(p
->one
);
2108 diff_free_filespec_data(p
->two
);
2114 /* This is different from find_unique_abbrev() in that
2115 * it stuffs the result with dots for alignment.
2117 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2122 return sha1_to_hex(sha1
);
2124 abbrev
= find_unique_abbrev(sha1
, len
);
2126 return sha1_to_hex(sha1
);
2127 abblen
= strlen(abbrev
);
2129 static char hex
[41];
2130 if (len
< abblen
&& abblen
<= len
+ 2)
2131 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2133 sprintf(hex
, "%s...", abbrev
);
2136 return sha1_to_hex(sha1
);
2139 static void diff_flush_raw(struct diff_filepair
*p
,
2140 struct diff_options
*options
)
2144 int abbrev
= options
->abbrev
;
2145 const char *path_one
, *path_two
;
2146 int inter_name_termination
= '\t';
2147 int line_termination
= options
->line_termination
;
2149 if (!line_termination
)
2150 inter_name_termination
= 0;
2152 path_one
= p
->one
->path
;
2153 path_two
= p
->two
->path
;
2154 if (line_termination
) {
2155 path_one
= quote_one(path_one
);
2156 path_two
= quote_one(path_two
);
2160 sprintf(status
, "%c%03d", p
->status
,
2161 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2163 status
[0] = p
->status
;
2166 switch (p
->status
) {
2167 case DIFF_STATUS_COPIED
:
2168 case DIFF_STATUS_RENAMED
:
2171 case DIFF_STATUS_ADDED
:
2172 case DIFF_STATUS_DELETED
:
2179 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2180 printf(":%06o %06o %s ",
2181 p
->one
->mode
, p
->two
->mode
,
2182 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2184 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2186 printf("%s%c%s", status
, inter_name_termination
, path_one
);
2188 printf("%c%s", inter_name_termination
, path_two
);
2189 putchar(line_termination
);
2190 if (path_one
!= p
->one
->path
)
2191 free((void*)path_one
);
2192 if (path_two
!= p
->two
->path
)
2193 free((void*)path_two
);
2196 static void diff_flush_name(struct diff_filepair
*p
, int line_termination
)
2198 char *path
= p
->two
->path
;
2200 if (line_termination
)
2201 path
= quote_one(p
->two
->path
);
2202 printf("%s%c", path
, line_termination
);
2203 if (p
->two
->path
!= path
)
2207 int diff_unmodified_pair(struct diff_filepair
*p
)
2209 /* This function is written stricter than necessary to support
2210 * the currently implemented transformers, but the idea is to
2211 * let transformers to produce diff_filepairs any way they want,
2212 * and filter and clean them up here before producing the output.
2214 struct diff_filespec
*one
, *two
;
2216 if (DIFF_PAIR_UNMERGED(p
))
2217 return 0; /* unmerged is interesting */
2222 /* deletion, addition, mode or type change
2223 * and rename are all interesting.
2225 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2226 DIFF_PAIR_MODE_CHANGED(p
) ||
2227 strcmp(one
->path
, two
->path
))
2230 /* both are valid and point at the same path. that is, we are
2231 * dealing with a change.
2233 if (one
->sha1_valid
&& two
->sha1_valid
&&
2234 !hashcmp(one
->sha1
, two
->sha1
))
2235 return 1; /* no change */
2236 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2237 return 1; /* both look at the same file on the filesystem. */
2241 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2243 if (diff_unmodified_pair(p
))
2246 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2247 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2248 return; /* no tree diffs in patch format */
2253 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2254 struct diffstat_t
*diffstat
)
2256 if (diff_unmodified_pair(p
))
2259 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2260 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2261 return; /* no tree diffs in patch format */
2263 run_diffstat(p
, o
, diffstat
);
2266 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2267 struct diff_options
*o
)
2269 if (diff_unmodified_pair(p
))
2272 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2273 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2274 return; /* no tree diffs in patch format */
2276 run_checkdiff(p
, o
);
2279 int diff_queue_is_empty(void)
2281 struct diff_queue_struct
*q
= &diff_queued_diff
;
2283 for (i
= 0; i
< q
->nr
; i
++)
2284 if (!diff_unmodified_pair(q
->queue
[i
]))
2290 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2292 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2295 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2297 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2298 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2300 s
->size
, s
->xfrm_flags
);
2303 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2305 diff_debug_filespec(p
->one
, i
, "one");
2306 diff_debug_filespec(p
->two
, i
, "two");
2307 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2308 p
->score
, p
->status
? p
->status
: '?',
2309 p
->source_stays
, p
->broken_pair
);
2312 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2316 fprintf(stderr
, "%s\n", msg
);
2317 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2318 for (i
= 0; i
< q
->nr
; i
++) {
2319 struct diff_filepair
*p
= q
->queue
[i
];
2320 diff_debug_filepair(p
, i
);
2325 static void diff_resolve_rename_copy(void)
2328 struct diff_filepair
*p
, *pp
;
2329 struct diff_queue_struct
*q
= &diff_queued_diff
;
2331 diff_debug_queue("resolve-rename-copy", q
);
2333 for (i
= 0; i
< q
->nr
; i
++) {
2335 p
->status
= 0; /* undecided */
2336 if (DIFF_PAIR_UNMERGED(p
))
2337 p
->status
= DIFF_STATUS_UNMERGED
;
2338 else if (!DIFF_FILE_VALID(p
->one
))
2339 p
->status
= DIFF_STATUS_ADDED
;
2340 else if (!DIFF_FILE_VALID(p
->two
))
2341 p
->status
= DIFF_STATUS_DELETED
;
2342 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2343 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2345 /* from this point on, we are dealing with a pair
2346 * whose both sides are valid and of the same type, i.e.
2347 * either in-place edit or rename/copy edit.
2349 else if (DIFF_PAIR_RENAME(p
)) {
2350 if (p
->source_stays
) {
2351 p
->status
= DIFF_STATUS_COPIED
;
2354 /* See if there is some other filepair that
2355 * copies from the same source as us. If so
2356 * we are a copy. Otherwise we are either a
2357 * copy if the path stays, or a rename if it
2358 * does not, but we already handled "stays" case.
2360 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2362 if (strcmp(pp
->one
->path
, p
->one
->path
))
2363 continue; /* not us */
2364 if (!DIFF_PAIR_RENAME(pp
))
2365 continue; /* not a rename/copy */
2366 /* pp is a rename/copy from the same source */
2367 p
->status
= DIFF_STATUS_COPIED
;
2371 p
->status
= DIFF_STATUS_RENAMED
;
2373 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2374 p
->one
->mode
!= p
->two
->mode
)
2375 p
->status
= DIFF_STATUS_MODIFIED
;
2377 /* This is a "no-change" entry and should not
2378 * happen anymore, but prepare for broken callers.
2380 error("feeding unmodified %s to diffcore",
2382 p
->status
= DIFF_STATUS_UNKNOWN
;
2385 diff_debug_queue("resolve-rename-copy done", q
);
2388 static int check_pair_status(struct diff_filepair
*p
)
2390 switch (p
->status
) {
2391 case DIFF_STATUS_UNKNOWN
:
2394 die("internal error in diff-resolve-rename-copy");
2400 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2402 int fmt
= opt
->output_format
;
2404 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2405 diff_flush_checkdiff(p
, opt
);
2406 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2407 diff_flush_raw(p
, opt
);
2408 else if (fmt
& DIFF_FORMAT_NAME
)
2409 diff_flush_name(p
, opt
->line_termination
);
2412 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2415 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, fs
->path
);
2417 printf(" %s %s\n", newdelete
, fs
->path
);
2421 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2423 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2425 printf(" mode change %06o => %06o %s\n",
2426 p
->one
->mode
, p
->two
->mode
, p
->two
->path
);
2428 printf(" mode change %06o => %06o\n",
2429 p
->one
->mode
, p
->two
->mode
);
2433 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2435 const char *old
, *new;
2437 /* Find common prefix */
2441 const char *slash_old
, *slash_new
;
2442 slash_old
= strchr(old
, '/');
2443 slash_new
= strchr(new, '/');
2446 slash_old
- old
!= slash_new
- new ||
2447 memcmp(old
, new, slash_new
- new))
2449 old
= slash_old
+ 1;
2450 new = slash_new
+ 1;
2452 /* p->one->path thru old is the common prefix, and old and new
2453 * through the end of names are renames
2455 if (old
!= p
->one
->path
)
2456 printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy
,
2457 (int)(old
- p
->one
->path
), p
->one
->path
,
2458 old
, new, (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2460 printf(" %s %s => %s (%d%%)\n", renamecopy
,
2461 p
->one
->path
, p
->two
->path
,
2462 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2463 show_mode_change(p
, 0);
2466 static void diff_summary(struct diff_filepair
*p
)
2469 case DIFF_STATUS_DELETED
:
2470 show_file_mode_name("delete", p
->one
);
2472 case DIFF_STATUS_ADDED
:
2473 show_file_mode_name("create", p
->two
);
2475 case DIFF_STATUS_COPIED
:
2476 show_rename_copy("copy", p
);
2478 case DIFF_STATUS_RENAMED
:
2479 show_rename_copy("rename", p
);
2483 printf(" rewrite %s (%d%%)\n", p
->two
->path
,
2484 (int)(0.5 + p
->score
* 100.0/MAX_SCORE
));
2485 show_mode_change(p
, 0);
2486 } else show_mode_change(p
, 1);
2492 struct xdiff_emit_state xm
;
2497 static int remove_space(char *line
, int len
)
2503 for (i
= 0; i
< len
; i
++)
2504 if (!isspace((c
= line
[i
])))
2510 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2512 struct patch_id_t
*data
= priv
;
2515 /* Ignore line numbers when computing the SHA1 of the patch */
2516 if (!strncmp(line
, "@@ -", 4))
2519 new_len
= remove_space(line
, len
);
2521 SHA1_Update(data
->ctx
, line
, new_len
);
2522 data
->patchlen
+= new_len
;
2525 /* returns 0 upon success, and writes result into sha1 */
2526 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2528 struct diff_queue_struct
*q
= &diff_queued_diff
;
2531 struct patch_id_t data
;
2532 char buffer
[PATH_MAX
* 4 + 20];
2535 memset(&data
, 0, sizeof(struct patch_id_t
));
2537 data
.xm
.consume
= patch_id_consume
;
2539 for (i
= 0; i
< q
->nr
; i
++) {
2544 struct diff_filepair
*p
= q
->queue
[i
];
2548 return error("internal diff status error");
2549 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2551 if (diff_unmodified_pair(p
))
2553 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2554 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2556 if (DIFF_PAIR_UNMERGED(p
))
2559 diff_fill_sha1_info(p
->one
);
2560 diff_fill_sha1_info(p
->two
);
2561 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2562 fill_mmfile(&mf2
, p
->two
) < 0)
2563 return error("unable to read files to diff");
2565 /* Maybe hash p->two? into the patch id? */
2566 if (mmfile_is_binary(&mf2
))
2569 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2570 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2571 if (p
->one
->mode
== 0)
2572 len1
= snprintf(buffer
, sizeof(buffer
),
2573 "diff--gita/%.*sb/%.*s"
2580 len2
, p
->two
->path
);
2581 else if (p
->two
->mode
== 0)
2582 len1
= snprintf(buffer
, sizeof(buffer
),
2583 "diff--gita/%.*sb/%.*s"
2584 "deletedfilemode%06o"
2590 len1
, p
->one
->path
);
2592 len1
= snprintf(buffer
, sizeof(buffer
),
2593 "diff--gita/%.*sb/%.*s"
2599 len2
, p
->two
->path
);
2600 SHA1_Update(&ctx
, buffer
, len1
);
2602 xpp
.flags
= XDF_NEED_MINIMAL
;
2604 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2605 ecb
.outf
= xdiff_outf
;
2607 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2610 SHA1_Final(sha1
, &ctx
);
2614 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2616 struct diff_queue_struct
*q
= &diff_queued_diff
;
2618 int result
= diff_get_patch_id(options
, sha1
);
2620 for (i
= 0; i
< q
->nr
; i
++)
2621 diff_free_filepair(q
->queue
[i
]);
2625 q
->nr
= q
->alloc
= 0;
2630 static int is_summary_empty(const struct diff_queue_struct
*q
)
2634 for (i
= 0; i
< q
->nr
; i
++) {
2635 const struct diff_filepair
*p
= q
->queue
[i
];
2637 switch (p
->status
) {
2638 case DIFF_STATUS_DELETED
:
2639 case DIFF_STATUS_ADDED
:
2640 case DIFF_STATUS_COPIED
:
2641 case DIFF_STATUS_RENAMED
:
2646 if (p
->one
->mode
&& p
->two
->mode
&&
2647 p
->one
->mode
!= p
->two
->mode
)
2655 void diff_flush(struct diff_options
*options
)
2657 struct diff_queue_struct
*q
= &diff_queued_diff
;
2658 int i
, output_format
= options
->output_format
;
2662 * Order: raw, stat, summary, patch
2663 * or: name/name-status/checkdiff (other bits clear)
2668 if (output_format
& (DIFF_FORMAT_RAW
|
2670 DIFF_FORMAT_NAME_STATUS
|
2671 DIFF_FORMAT_CHECKDIFF
)) {
2672 for (i
= 0; i
< q
->nr
; i
++) {
2673 struct diff_filepair
*p
= q
->queue
[i
];
2674 if (check_pair_status(p
))
2675 flush_one_pair(p
, options
);
2680 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
2681 struct diffstat_t diffstat
;
2683 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
2684 diffstat
.xm
.consume
= diffstat_consume
;
2685 for (i
= 0; i
< q
->nr
; i
++) {
2686 struct diff_filepair
*p
= q
->queue
[i
];
2687 if (check_pair_status(p
))
2688 diff_flush_stat(p
, options
, &diffstat
);
2690 if (output_format
& DIFF_FORMAT_NUMSTAT
)
2691 show_numstat(&diffstat
, options
);
2692 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
2693 show_stats(&diffstat
, options
);
2694 else if (output_format
& DIFF_FORMAT_SHORTSTAT
)
2695 show_shortstats(&diffstat
);
2699 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
2700 for (i
= 0; i
< q
->nr
; i
++)
2701 diff_summary(q
->queue
[i
]);
2705 if (output_format
& DIFF_FORMAT_PATCH
) {
2707 if (options
->stat_sep
) {
2708 /* attach patch instead of inline */
2709 fputs(options
->stat_sep
, stdout
);
2711 putchar(options
->line_termination
);
2715 for (i
= 0; i
< q
->nr
; i
++) {
2716 struct diff_filepair
*p
= q
->queue
[i
];
2717 if (check_pair_status(p
))
2718 diff_flush_patch(p
, options
);
2722 if (output_format
& DIFF_FORMAT_CALLBACK
)
2723 options
->format_callback(q
, options
, options
->format_callback_data
);
2725 for (i
= 0; i
< q
->nr
; i
++)
2726 diff_free_filepair(q
->queue
[i
]);
2730 q
->nr
= q
->alloc
= 0;
2733 static void diffcore_apply_filter(const char *filter
)
2736 struct diff_queue_struct
*q
= &diff_queued_diff
;
2737 struct diff_queue_struct outq
;
2739 outq
.nr
= outq
.alloc
= 0;
2744 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
2746 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
2747 struct diff_filepair
*p
= q
->queue
[i
];
2748 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2750 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2752 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2753 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2754 strchr(filter
, p
->status
)))
2760 /* otherwise we will clear the whole queue
2761 * by copying the empty outq at the end of this
2762 * function, but first clear the current entries
2765 for (i
= 0; i
< q
->nr
; i
++)
2766 diff_free_filepair(q
->queue
[i
]);
2769 /* Only the matching ones */
2770 for (i
= 0; i
< q
->nr
; i
++) {
2771 struct diff_filepair
*p
= q
->queue
[i
];
2773 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
2775 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
2777 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
2778 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
2779 strchr(filter
, p
->status
)))
2782 diff_free_filepair(p
);
2789 void diffcore_std(struct diff_options
*options
)
2791 if (options
->break_opt
!= -1)
2792 diffcore_break(options
->break_opt
);
2793 if (options
->detect_rename
)
2794 diffcore_rename(options
);
2795 if (options
->break_opt
!= -1)
2796 diffcore_merge_broken();
2797 if (options
->pickaxe
)
2798 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2799 if (options
->orderfile
)
2800 diffcore_order(options
->orderfile
);
2801 diff_resolve_rename_copy();
2802 diffcore_apply_filter(options
->filter
);
2806 void diffcore_std_no_resolve(struct diff_options
*options
)
2808 if (options
->pickaxe
)
2809 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
2810 if (options
->orderfile
)
2811 diffcore_order(options
->orderfile
);
2812 diffcore_apply_filter(options
->filter
);
2815 void diff_addremove(struct diff_options
*options
,
2816 int addremove
, unsigned mode
,
2817 const unsigned char *sha1
,
2818 const char *base
, const char *path
)
2820 char concatpath
[PATH_MAX
];
2821 struct diff_filespec
*one
, *two
;
2823 /* This may look odd, but it is a preparation for
2824 * feeding "there are unchanged files which should
2825 * not produce diffs, but when you are doing copy
2826 * detection you would need them, so here they are"
2827 * entries to the diff-core. They will be prefixed
2828 * with something like '=' or '*' (I haven't decided
2829 * which but should not make any difference).
2830 * Feeding the same new and old to diff_change()
2831 * also has the same effect.
2832 * Before the final output happens, they are pruned after
2833 * merged into rename/copy pairs as appropriate.
2835 if (options
->reverse_diff
)
2836 addremove
= (addremove
== '+' ? '-' :
2837 addremove
== '-' ? '+' : addremove
);
2839 if (!path
) path
= "";
2840 sprintf(concatpath
, "%s%s", base
, path
);
2841 one
= alloc_filespec(concatpath
);
2842 two
= alloc_filespec(concatpath
);
2844 if (addremove
!= '+')
2845 fill_filespec(one
, sha1
, mode
);
2846 if (addremove
!= '-')
2847 fill_filespec(two
, sha1
, mode
);
2849 diff_queue(&diff_queued_diff
, one
, two
);
2852 void diff_change(struct diff_options
*options
,
2853 unsigned old_mode
, unsigned new_mode
,
2854 const unsigned char *old_sha1
,
2855 const unsigned char *new_sha1
,
2856 const char *base
, const char *path
)
2858 char concatpath
[PATH_MAX
];
2859 struct diff_filespec
*one
, *two
;
2861 if (options
->reverse_diff
) {
2863 const unsigned char *tmp_c
;
2864 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
2865 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
2867 if (!path
) path
= "";
2868 sprintf(concatpath
, "%s%s", base
, path
);
2869 one
= alloc_filespec(concatpath
);
2870 two
= alloc_filespec(concatpath
);
2871 fill_filespec(one
, old_sha1
, old_mode
);
2872 fill_filespec(two
, new_sha1
, new_mode
);
2874 diff_queue(&diff_queued_diff
, one
, two
);
2877 void diff_unmerge(struct diff_options
*options
,
2879 unsigned mode
, const unsigned char *sha1
)
2881 struct diff_filespec
*one
, *two
;
2882 one
= alloc_filespec(path
);
2883 two
= alloc_filespec(path
);
2884 fill_filespec(one
, sha1
, mode
);
2885 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;