2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "spawn-pipe.h"
14 #ifdef NO_FAST_WORKING_DIRECTORY
15 #define FAST_WORKING_DIRECTORY 0
17 #define FAST_WORKING_DIRECTORY 1
20 static int diff_detect_rename_default
;
21 static int diff_rename_limit_default
= -1;
22 static int diff_use_color_default
;
23 int diff_auto_refresh_index
= 1;
25 static char diff_colors
[][COLOR_MAXLEN
] = {
27 "", /* PLAIN (normal) */
28 "\033[1m", /* METAINFO (bold) */
29 "\033[36m", /* FRAGINFO (cyan) */
30 "\033[31m", /* OLD (red) */
31 "\033[32m", /* NEW (green) */
32 "\033[33m", /* COMMIT (yellow) */
33 "\033[41m", /* WHITESPACE (red background) */
36 static int parse_diff_color_slot(const char *var
, int ofs
)
38 if (!strcasecmp(var
+ofs
, "plain"))
40 if (!strcasecmp(var
+ofs
, "meta"))
42 if (!strcasecmp(var
+ofs
, "frag"))
44 if (!strcasecmp(var
+ofs
, "old"))
46 if (!strcasecmp(var
+ofs
, "new"))
48 if (!strcasecmp(var
+ofs
, "commit"))
50 if (!strcasecmp(var
+ofs
, "whitespace"))
51 return DIFF_WHITESPACE
;
52 die("bad config variable '%s'", var
);
55 static struct ll_diff_driver
{
57 struct ll_diff_driver
*next
;
59 } *user_diff
, **user_diff_tail
;
61 static void read_config_if_needed(void)
63 if (!user_diff_tail
) {
64 user_diff_tail
= &user_diff
;
65 git_config(git_diff_ui_config
);
70 * Currently there is only "diff.<drivername>.command" variable;
71 * because there are "diff.color.<slot>" variables, we are parsing
72 * this in a bit convoluted way to allow low level diff driver
75 static int parse_lldiff_command(const char *var
, const char *ep
, const char *value
)
79 struct ll_diff_driver
*drv
;
83 for (drv
= user_diff
; drv
; drv
= drv
->next
)
84 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
88 drv
= xcalloc(1, sizeof(struct ll_diff_driver
));
89 namebuf
= xmalloc(namelen
+ 1);
90 memcpy(namebuf
, name
, namelen
);
95 user_diff_tail
= &user_diff
;
96 *user_diff_tail
= drv
;
97 user_diff_tail
= &(drv
->next
);
101 return error("%s: lacks value", var
);
102 drv
->cmd
= strdup(value
);
107 * 'diff.<what>.funcname' attribute can be specified in the configuration
108 * to define a customized regexp to find the beginning of a function to
109 * be used for hunk header lines of "diff -p" style output.
111 static struct funcname_pattern
{
114 struct funcname_pattern
*next
;
115 } *funcname_pattern_list
;
117 static int parse_funcname_pattern(const char *var
, const char *ep
, const char *value
)
121 struct funcname_pattern
*pp
;
123 name
= var
+ 5; /* "diff." */
126 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
127 if (!strncmp(pp
->name
, name
, namelen
) && !pp
->name
[namelen
])
131 pp
= xcalloc(1, sizeof(*pp
));
132 namebuf
= xmalloc(namelen
+ 1);
133 memcpy(namebuf
, name
, namelen
);
134 namebuf
[namelen
] = 0;
136 pp
->next
= funcname_pattern_list
;
137 funcname_pattern_list
= pp
;
141 pp
->pattern
= xstrdup(value
);
146 * These are to give UI layer defaults.
147 * The core-level commands such as git-diff-files should
148 * never be affected by the setting of diff.renames
149 * the user happens to have in the configuration file.
151 int git_diff_ui_config(const char *var
, const char *value
)
153 if (!strcmp(var
, "diff.renamelimit")) {
154 diff_rename_limit_default
= git_config_int(var
, value
);
157 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
158 diff_use_color_default
= git_config_colorbool(var
, value
);
161 if (!strcmp(var
, "diff.renames")) {
163 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
164 else if (!strcasecmp(value
, "copies") ||
165 !strcasecmp(value
, "copy"))
166 diff_detect_rename_default
= DIFF_DETECT_COPY
;
167 else if (git_config_bool(var
,value
))
168 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
171 if (!strcmp(var
, "diff.autorefreshindex")) {
172 diff_auto_refresh_index
= git_config_bool(var
, value
);
175 if (!prefixcmp(var
, "diff.")) {
176 const char *ep
= strrchr(var
, '.');
179 if (!strcmp(ep
, ".command"))
180 return parse_lldiff_command(var
, ep
, value
);
181 if (!strcmp(ep
, ".funcname"))
182 return parse_funcname_pattern(var
, ep
, value
);
185 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
186 int slot
= parse_diff_color_slot(var
, 11);
187 color_parse(value
, var
, diff_colors
[slot
]);
191 return git_default_config(var
, value
);
194 static char *quote_one(const char *str
)
201 needlen
= quote_c_style(str
, NULL
, NULL
, 0);
204 xp
= xmalloc(needlen
+ 1);
205 quote_c_style(str
, xp
, NULL
, 0);
209 static char *quote_two(const char *one
, const char *two
)
211 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
212 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
215 if (need_one
+ need_two
) {
216 if (!need_one
) need_one
= strlen(one
);
217 if (!need_two
) need_one
= strlen(two
);
219 xp
= xmalloc(need_one
+ need_two
+ 3);
221 quote_c_style(one
, xp
+ 1, NULL
, 1);
222 quote_c_style(two
, xp
+ need_one
+ 1, NULL
, 1);
223 strcpy(xp
+ need_one
+ need_two
+ 1, "\"");
226 need_one
= strlen(one
);
227 need_two
= strlen(two
);
228 xp
= xmalloc(need_one
+ need_two
+ 1);
230 strcpy(xp
+ need_one
, two
);
234 static const char *external_diff(void)
236 static const char *external_diff_cmd
= NULL
;
237 static int done_preparing
= 0;
240 return external_diff_cmd
;
241 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
243 return external_diff_cmd
;
246 static struct diff_tempfile
{
247 const char *name
; /* filename external diff should read from */
250 char tmp_path
[PATH_MAX
];
253 static int count_lines(const char *data
, int size
)
255 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
262 completely_empty
= 0;
266 completely_empty
= 0;
269 if (completely_empty
)
272 count
++; /* no trailing newline */
276 static void print_line_count(int count
)
286 printf("1,%d", count
);
291 static void copy_file(int prefix
, const char *data
, int size
,
292 const char *set
, const char *reset
)
294 int ch
, nl_just_seen
= 1;
303 fputs(reset
, stdout
);
309 printf("%s\n\\ No newline at end of file\n", reset
);
312 static void emit_rewrite_diff(const char *name_a
,
314 struct diff_filespec
*one
,
315 struct diff_filespec
*two
,
319 const char *name_a_tab
, *name_b_tab
;
320 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
321 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
322 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
323 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
324 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
326 name_a
+= (*name_a
== '/');
327 name_b
+= (*name_b
== '/');
328 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
329 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
331 diff_populate_filespec(one
, 0);
332 diff_populate_filespec(two
, 0);
333 lc_a
= count_lines(one
->data
, one
->size
);
334 lc_b
= count_lines(two
->data
, two
->size
);
335 printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -",
336 metainfo
, name_a
, name_a_tab
, reset
,
337 metainfo
, name_b
, name_b_tab
, reset
, fraginfo
);
338 print_line_count(lc_a
);
340 print_line_count(lc_b
);
341 printf(" @@%s\n", reset
);
343 copy_file('-', one
->data
, one
->size
, old
, reset
);
345 copy_file('+', two
->data
, two
->size
, new, reset
);
348 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
350 if (!DIFF_FILE_VALID(one
)) {
351 mf
->ptr
= (char *)""; /* does not matter */
355 else if (diff_populate_filespec(one
, 0))
358 mf
->size
= one
->size
;
362 struct diff_words_buffer
{
365 long current
; /* output pointer */
366 int suppressed_newline
;
369 static void diff_words_append(char *line
, unsigned long len
,
370 struct diff_words_buffer
*buffer
)
372 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
373 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
374 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
378 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
379 buffer
->text
.size
+= len
;
382 struct diff_words_data
{
383 struct xdiff_emit_state xm
;
384 struct diff_words_buffer minus
, plus
;
387 static void print_word(struct diff_words_buffer
*buffer
, int len
, int color
,
388 int suppress_newline
)
396 ptr
= buffer
->text
.ptr
+ buffer
->current
;
397 buffer
->current
+= len
;
399 if (ptr
[len
- 1] == '\n') {
404 fputs(diff_get_color(1, color
), stdout
);
405 fwrite(ptr
, len
, 1, stdout
);
406 fputs(diff_get_color(1, DIFF_RESET
), stdout
);
409 if (suppress_newline
)
410 buffer
->suppressed_newline
= 1;
416 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
418 struct diff_words_data
*diff_words
= priv
;
420 if (diff_words
->minus
.suppressed_newline
) {
423 diff_words
->minus
.suppressed_newline
= 0;
429 print_word(&diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
432 print_word(&diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
435 print_word(&diff_words
->plus
, len
, DIFF_PLAIN
, 0);
436 diff_words
->minus
.current
+= len
;
441 /* this executes the word diff on the accumulated buffers */
442 static void diff_words_show(struct diff_words_data
*diff_words
)
447 mmfile_t minus
, plus
;
450 memset(&xecfg
, 0, sizeof(xecfg
));
451 minus
.size
= diff_words
->minus
.text
.size
;
452 minus
.ptr
= xmalloc(minus
.size
);
453 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
454 for (i
= 0; i
< minus
.size
; i
++)
455 if (isspace(minus
.ptr
[i
]))
457 diff_words
->minus
.current
= 0;
459 plus
.size
= diff_words
->plus
.text
.size
;
460 plus
.ptr
= xmalloc(plus
.size
);
461 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
462 for (i
= 0; i
< plus
.size
; i
++)
463 if (isspace(plus
.ptr
[i
]))
465 diff_words
->plus
.current
= 0;
467 xpp
.flags
= XDF_NEED_MINIMAL
;
468 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
469 ecb
.outf
= xdiff_outf
;
470 ecb
.priv
= diff_words
;
471 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
472 xdl_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
476 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
478 if (diff_words
->minus
.suppressed_newline
) {
480 diff_words
->minus
.suppressed_newline
= 0;
484 struct emit_callback
{
485 struct xdiff_emit_state xm
;
486 int nparents
, color_diff
;
487 const char **label_path
;
488 struct diff_words_data
*diff_words
;
492 static void free_diff_words_data(struct emit_callback
*ecbdata
)
494 if (ecbdata
->diff_words
) {
496 if (ecbdata
->diff_words
->minus
.text
.size
||
497 ecbdata
->diff_words
->plus
.text
.size
)
498 diff_words_show(ecbdata
->diff_words
);
500 if (ecbdata
->diff_words
->minus
.text
.ptr
)
501 free (ecbdata
->diff_words
->minus
.text
.ptr
);
502 if (ecbdata
->diff_words
->plus
.text
.ptr
)
503 free (ecbdata
->diff_words
->plus
.text
.ptr
);
504 free(ecbdata
->diff_words
);
505 ecbdata
->diff_words
= NULL
;
509 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
512 return diff_colors
[ix
];
516 static void emit_line(const char *set
, const char *reset
, const char *line
, int len
)
518 if (len
> 0 && line
[len
-1] == '\n')
521 fwrite(line
, len
, 1, stdout
);
525 static void emit_line_with_ws(int nparents
,
526 const char *set
, const char *reset
, const char *ws
,
527 const char *line
, int len
)
530 int last_tab_in_indent
= -1;
531 int last_space_in_indent
= -1;
534 int need_highlight_leading_space
= 0;
535 /* The line is a newly added line. Does it have funny leading
536 * whitespaces? In indent, SP should never precede a TAB.
538 for (i
= col0
; i
< len
; i
++) {
539 if (line
[i
] == '\t') {
540 last_tab_in_indent
= i
;
541 if (0 <= last_space_in_indent
)
542 need_highlight_leading_space
= 1;
544 else if (line
[i
] == ' ')
545 last_space_in_indent
= i
;
550 fwrite(line
, col0
, 1, stdout
);
551 fputs(reset
, stdout
);
552 if (((i
== len
) || line
[i
] == '\n') && i
!= col0
) {
553 /* The whole line was indent */
554 emit_line(ws
, reset
, line
+ col0
, len
- col0
);
558 if (need_highlight_leading_space
) {
559 while (i
< last_tab_in_indent
) {
560 if (line
[i
] == ' ') {
563 fputs(reset
, stdout
);
571 if (line
[tail
] == '\n' && i
< tail
)
574 if (!isspace(line
[tail
]))
578 if ((i
< tail
&& line
[tail
+ 1] != '\n')) {
579 /* This has whitespace between tail+1..len */
581 fwrite(line
+ i
, tail
- i
+ 1, 1, stdout
);
582 fputs(reset
, stdout
);
583 emit_line(ws
, reset
, line
+ tail
+ 1, len
- tail
- 1);
586 emit_line(set
, reset
, line
+ i
, len
- i
);
589 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
591 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
592 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
595 emit_line(set
, reset
, line
, len
);
597 emit_line_with_ws(ecbdata
->nparents
, set
, reset
, ws
,
601 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
605 struct emit_callback
*ecbdata
= priv
;
606 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
607 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
609 *(ecbdata
->found_changesp
) = 1;
611 if (ecbdata
->label_path
[0]) {
612 const char *name_a_tab
, *name_b_tab
;
614 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
615 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
617 printf("%s--- %s%s%s\n",
618 set
, ecbdata
->label_path
[0], reset
, name_a_tab
);
619 printf("%s+++ %s%s%s\n",
620 set
, ecbdata
->label_path
[1], reset
, name_b_tab
);
621 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
624 /* This is not really necessary for now because
625 * this codepath only deals with two-way diffs.
627 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
629 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
630 ecbdata
->nparents
= i
- 1;
631 emit_line(diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
636 if (len
< ecbdata
->nparents
) {
638 emit_line(reset
, reset
, line
, len
);
643 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
644 /* fall back to normal diff */
645 free_diff_words_data(ecbdata
);
646 if (ecbdata
->diff_words
) {
647 if (line
[0] == '-') {
648 diff_words_append(line
, len
,
649 &ecbdata
->diff_words
->minus
);
651 } else if (line
[0] == '+') {
652 diff_words_append(line
, len
,
653 &ecbdata
->diff_words
->plus
);
656 if (ecbdata
->diff_words
->minus
.text
.size
||
657 ecbdata
->diff_words
->plus
.text
.size
)
658 diff_words_show(ecbdata
->diff_words
);
661 emit_line(set
, reset
, line
, len
);
664 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
666 color
= DIFF_FILE_OLD
;
667 else if (line
[i
] == '+')
668 color
= DIFF_FILE_NEW
;
671 if (color
!= DIFF_FILE_NEW
) {
672 emit_line(diff_get_color(ecbdata
->color_diff
, color
),
676 emit_add_line(reset
, ecbdata
, line
, len
);
679 static char *pprint_rename(const char *a
, const char *b
)
684 int pfx_length
, sfx_length
;
685 int len_a
= strlen(a
);
686 int len_b
= strlen(b
);
687 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
688 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
690 if (qlen_a
|| qlen_b
) {
691 if (qlen_a
) len_a
= qlen_a
;
692 if (qlen_b
) len_b
= qlen_b
;
693 name
= xmalloc( len_a
+ len_b
+ 5 );
695 quote_c_style(a
, name
, NULL
, 0);
697 memcpy(name
, a
, len_a
);
698 memcpy(name
+ len_a
, " => ", 4);
700 quote_c_style(b
, name
+ len_a
+ 4, NULL
, 0);
702 memcpy(name
+ len_a
+ 4, b
, len_b
+ 1);
706 /* Find common prefix */
708 while (*old
&& *new && *old
== *new) {
710 pfx_length
= old
- a
+ 1;
715 /* Find common suffix */
719 while (a
<= old
&& b
<= new && *old
== *new) {
721 sfx_length
= len_a
- (old
- a
);
727 * pfx{mid-a => mid-b}sfx
728 * {pfx-a => pfx-b}sfx
729 * pfx{sfx-a => sfx-b}
732 if (pfx_length
+ sfx_length
) {
733 int a_midlen
= len_a
- pfx_length
- sfx_length
;
734 int b_midlen
= len_b
- pfx_length
- sfx_length
;
735 if (a_midlen
< 0) a_midlen
= 0;
736 if (b_midlen
< 0) b_midlen
= 0;
738 name
= xmalloc(pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
739 sprintf(name
, "%.*s{%.*s => %.*s}%s",
741 a_midlen
, a
+ pfx_length
,
742 b_midlen
, b
+ pfx_length
,
743 a
+ len_a
- sfx_length
);
746 name
= xmalloc(len_a
+ len_b
+ 5);
747 sprintf(name
, "%s => %s", a
, b
);
753 struct xdiff_emit_state xm
;
757 struct diffstat_file
{
759 unsigned is_unmerged
:1;
760 unsigned is_binary
:1;
761 unsigned is_renamed
:1;
762 unsigned int added
, deleted
;
766 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
770 struct diffstat_file
*x
;
771 x
= xcalloc(sizeof (*x
), 1);
772 if (diffstat
->nr
== diffstat
->alloc
) {
773 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
774 diffstat
->files
= xrealloc(diffstat
->files
,
775 diffstat
->alloc
* sizeof(x
));
777 diffstat
->files
[diffstat
->nr
++] = x
;
779 x
->name
= pprint_rename(name_a
, name_b
);
783 x
->name
= xstrdup(name_a
);
787 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
789 struct diffstat_t
*diffstat
= priv
;
790 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
794 else if (line
[0] == '-')
798 const char mime_boundary_leader
[] = "------------";
800 static int scale_linear(int it
, int width
, int max_change
)
803 * make sure that at least one '-' is printed if there were deletions,
804 * and likewise for '+'.
808 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
811 static void show_name(const char *prefix
, const char *name
, int len
,
812 const char *reset
, const char *set
)
814 printf(" %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
817 static void show_graph(char ch
, int cnt
, const char *set
, const char *reset
)
827 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
829 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
830 int max_change
= 0, max_len
= 0;
831 int total_files
= data
->nr
;
832 int width
, name_width
;
833 const char *reset
, *set
, *add_c
, *del_c
;
838 width
= options
->stat_width
? options
->stat_width
: 80;
839 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
841 /* Sanity: give at least 5 columns to the graph,
842 * but leave at least 10 columns for the name.
844 if (width
< name_width
+ 15) {
845 if (name_width
<= 25)
846 width
= name_width
+ 15;
848 name_width
= width
- 15;
851 /* Find the longest filename and max number of changes */
852 reset
= diff_get_color(options
->color_diff
, DIFF_RESET
);
853 set
= diff_get_color(options
->color_diff
, DIFF_PLAIN
);
854 add_c
= diff_get_color(options
->color_diff
, DIFF_FILE_NEW
);
855 del_c
= diff_get_color(options
->color_diff
, DIFF_FILE_OLD
);
857 for (i
= 0; i
< data
->nr
; i
++) {
858 struct diffstat_file
*file
= data
->files
[i
];
859 int change
= file
->added
+ file
->deleted
;
861 if (!file
->is_renamed
) { /* renames are already quoted by pprint_rename */
862 len
= quote_c_style(file
->name
, NULL
, NULL
, 0);
864 char *qname
= xmalloc(len
+ 1);
865 quote_c_style(file
->name
, qname
, NULL
, 0);
871 len
= strlen(file
->name
);
875 if (file
->is_binary
|| file
->is_unmerged
)
877 if (max_change
< change
)
881 /* Compute the width of the graph part;
882 * 10 is for one blank at the beginning of the line plus
883 * " | count " between the name and the graph.
885 * From here on, name_width is the width of the name area,
886 * and width is the width of the graph area.
888 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
889 if (width
< (name_width
+ 10) + max_change
)
890 width
= width
- (name_width
+ 10);
894 for (i
= 0; i
< data
->nr
; i
++) {
895 const char *prefix
= "";
896 char *name
= data
->files
[i
]->name
;
897 int added
= data
->files
[i
]->added
;
898 int deleted
= data
->files
[i
]->deleted
;
902 * "scale" the filename
905 name_len
= strlen(name
);
906 if (name_width
< name_len
) {
910 name
+= name_len
- len
;
911 slash
= strchr(name
, '/');
916 if (data
->files
[i
]->is_binary
) {
917 show_name(prefix
, name
, len
, reset
, set
);
919 printf("%s%d%s", del_c
, deleted
, reset
);
921 printf("%s%d%s", add_c
, added
, reset
);
924 goto free_diffstat_file
;
926 else if (data
->files
[i
]->is_unmerged
) {
927 show_name(prefix
, name
, len
, reset
, set
);
928 printf(" Unmerged\n");
929 goto free_diffstat_file
;
931 else if (!data
->files
[i
]->is_renamed
&&
932 (added
+ deleted
== 0)) {
934 goto free_diffstat_file
;
938 * scale the add/delete
946 if (width
<= max_change
) {
947 add
= scale_linear(add
, width
, max_change
);
948 del
= scale_linear(del
, width
, max_change
);
951 show_name(prefix
, name
, len
, reset
, set
);
952 printf("%5d ", added
+ deleted
);
953 show_graph('+', add
, add_c
, reset
);
954 show_graph('-', del
, del_c
, reset
);
957 free(data
->files
[i
]->name
);
958 free(data
->files
[i
]);
961 printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
962 set
, total_files
, adds
, dels
, reset
);
965 static void show_shortstats(struct diffstat_t
* data
)
967 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
972 for (i
= 0; i
< data
->nr
; i
++) {
973 if (!data
->files
[i
]->is_binary
&&
974 !data
->files
[i
]->is_unmerged
) {
975 int added
= data
->files
[i
]->added
;
976 int deleted
= data
->files
[i
]->deleted
;
977 if (!data
->files
[i
]->is_renamed
&&
978 (added
+ deleted
== 0)) {
985 free(data
->files
[i
]->name
);
986 free(data
->files
[i
]);
990 printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
991 total_files
, adds
, dels
);
994 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
998 for (i
= 0; i
< data
->nr
; i
++) {
999 struct diffstat_file
*file
= data
->files
[i
];
1001 if (file
->is_binary
)
1004 printf("%d\t%d\t", file
->added
, file
->deleted
);
1005 if (options
->line_termination
&& !file
->is_renamed
&&
1006 quote_c_style(file
->name
, NULL
, NULL
, 0))
1007 quote_c_style(file
->name
, NULL
, stdout
, 0);
1009 fputs(file
->name
, stdout
);
1010 putchar(options
->line_termination
);
1014 struct checkdiff_t
{
1015 struct xdiff_emit_state xm
;
1016 const char *filename
;
1017 int lineno
, color_diff
;
1020 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1022 struct checkdiff_t
*data
= priv
;
1023 const char *ws
= diff_get_color(data
->color_diff
, DIFF_WHITESPACE
);
1024 const char *reset
= diff_get_color(data
->color_diff
, DIFF_RESET
);
1025 const char *set
= diff_get_color(data
->color_diff
, DIFF_FILE_NEW
);
1027 if (line
[0] == '+') {
1028 int i
, spaces
= 0, space_before_tab
= 0, white_space_at_end
= 0;
1030 /* check space before tab */
1031 for (i
= 1; i
< len
&& (line
[i
] == ' ' || line
[i
] == '\t'); i
++)
1034 if (line
[i
- 1] == '\t' && spaces
)
1035 space_before_tab
= 1;
1037 /* check white space at line end */
1038 if (line
[len
- 1] == '\n')
1040 if (isspace(line
[len
- 1]))
1041 white_space_at_end
= 1;
1043 if (space_before_tab
|| white_space_at_end
) {
1044 printf("%s:%d: %s", data
->filename
, data
->lineno
, ws
);
1045 if (space_before_tab
) {
1046 printf("space before tab");
1047 if (white_space_at_end
)
1050 if (white_space_at_end
)
1051 printf("white space at end");
1052 printf(":%s ", reset
);
1053 emit_line_with_ws(1, set
, reset
, ws
, line
, len
);
1057 } else if (line
[0] == ' ')
1059 else if (line
[0] == '@') {
1060 char *plus
= strchr(line
, '+');
1062 data
->lineno
= strtol(plus
, NULL
, 10);
1064 die("invalid diff");
1068 static unsigned char *deflate_it(char *data
,
1070 unsigned long *result_size
)
1073 unsigned char *deflated
;
1076 memset(&stream
, 0, sizeof(stream
));
1077 deflateInit(&stream
, zlib_compression_level
);
1078 bound
= deflateBound(&stream
, size
);
1079 deflated
= xmalloc(bound
);
1080 stream
.next_out
= deflated
;
1081 stream
.avail_out
= bound
;
1083 stream
.next_in
= (unsigned char *)data
;
1084 stream
.avail_in
= size
;
1085 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1087 deflateEnd(&stream
);
1088 *result_size
= stream
.total_out
;
1092 static void emit_binary_diff_body(mmfile_t
*one
, mmfile_t
*two
)
1098 unsigned long orig_size
;
1099 unsigned long delta_size
;
1100 unsigned long deflate_size
;
1101 unsigned long data_size
;
1103 /* We could do deflated delta, or we could do just deflated two,
1104 * whichever is smaller.
1107 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1108 if (one
->size
&& two
->size
) {
1109 delta
= diff_delta(one
->ptr
, one
->size
,
1110 two
->ptr
, two
->size
,
1111 &delta_size
, deflate_size
);
1113 void *to_free
= delta
;
1114 orig_size
= delta_size
;
1115 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1120 if (delta
&& delta_size
< deflate_size
) {
1121 printf("delta %lu\n", orig_size
);
1124 data_size
= delta_size
;
1127 printf("literal %lu\n", two
->size
);
1130 data_size
= deflate_size
;
1133 /* emit data encoded in base85 */
1136 int bytes
= (52 < data_size
) ? 52 : data_size
;
1140 line
[0] = bytes
+ 'A' - 1;
1142 line
[0] = bytes
- 26 + 'a' - 1;
1143 encode_85(line
+ 1, cp
, bytes
);
1144 cp
= (char *) cp
+ bytes
;
1151 static void emit_binary_diff(mmfile_t
*one
, mmfile_t
*two
)
1153 printf("GIT binary patch\n");
1154 emit_binary_diff_body(one
, two
);
1155 emit_binary_diff_body(two
, one
);
1158 static void setup_diff_attr_check(struct git_attr_check
*check
)
1160 static struct git_attr
*attr_diff
;
1163 attr_diff
= git_attr("diff", 4);
1165 check
[0].attr
= attr_diff
;
1168 static void diff_filespec_check_attr(struct diff_filespec
*one
)
1170 struct git_attr_check attr_diff_check
;
1171 int check_from_data
= 0;
1173 if (one
->checked_attr
)
1176 setup_diff_attr_check(&attr_diff_check
);
1178 one
->funcname_pattern_ident
= NULL
;
1180 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1184 value
= attr_diff_check
.value
;
1185 if (ATTR_TRUE(value
))
1187 else if (ATTR_FALSE(value
))
1190 check_from_data
= 1;
1192 /* funcname pattern ident */
1193 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1196 one
->funcname_pattern_ident
= value
;
1199 if (check_from_data
) {
1200 if (!one
->data
&& DIFF_FILE_VALID(one
))
1201 diff_populate_filespec(one
, 0);
1204 one
->is_binary
= buffer_is_binary(one
->data
, one
->size
);
1208 int diff_filespec_is_binary(struct diff_filespec
*one
)
1210 diff_filespec_check_attr(one
);
1211 return one
->is_binary
;
1214 static const char *funcname_pattern(const char *ident
)
1216 struct funcname_pattern
*pp
;
1218 read_config_if_needed();
1219 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
1220 if (!strcmp(ident
, pp
->name
))
1225 static struct builtin_funcname_pattern
{
1227 const char *pattern
;
1228 } builtin_funcname_pattern
[] = {
1229 { "java", "!^[ ]*\\(catch\\|do\\|for\\|if\\|instanceof\\|"
1230 "new\\|return\\|switch\\|throw\\|while\\)\n"
1232 "[A-Za-z_][A-Za-z_0-9]*\\)\\{2,\\}"
1234 { "tex", "^\\(\\\\\\(sub\\)*section{.*\\)$" },
1237 static const char *diff_funcname_pattern(struct diff_filespec
*one
)
1239 const char *ident
, *pattern
;
1242 diff_filespec_check_attr(one
);
1243 ident
= one
->funcname_pattern_ident
;
1247 * If the config file has "funcname.default" defined, that
1248 * regexp is used; otherwise NULL is returned and xemit uses
1249 * the built-in default.
1251 return funcname_pattern("default");
1253 /* Look up custom "funcname.$ident" regexp from config. */
1254 pattern
= funcname_pattern(ident
);
1259 * And define built-in fallback patterns here. Note that
1260 * these can be overriden by the user's config settings.
1262 for (i
= 0; i
< ARRAY_SIZE(builtin_funcname_pattern
); i
++)
1263 if (!strcmp(ident
, builtin_funcname_pattern
[i
].name
))
1264 return builtin_funcname_pattern
[i
].pattern
;
1269 static void builtin_diff(const char *name_a
,
1271 struct diff_filespec
*one
,
1272 struct diff_filespec
*two
,
1273 const char *xfrm_msg
,
1274 struct diff_options
*o
,
1275 int complete_rewrite
)
1279 char *a_one
, *b_two
;
1280 const char *set
= diff_get_color(o
->color_diff
, DIFF_METAINFO
);
1281 const char *reset
= diff_get_color(o
->color_diff
, DIFF_RESET
);
1283 a_one
= quote_two("a/", name_a
+ (*name_a
== '/'));
1284 b_two
= quote_two("b/", name_b
+ (*name_b
== '/'));
1285 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1286 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1287 printf("%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1288 if (lbl
[0][0] == '/') {
1290 printf("%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1291 if (xfrm_msg
&& xfrm_msg
[0])
1292 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1294 else if (lbl
[1][0] == '/') {
1295 printf("%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1296 if (xfrm_msg
&& xfrm_msg
[0])
1297 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1300 if (one
->mode
!= two
->mode
) {
1301 printf("%sold mode %06o%s\n", set
, one
->mode
, reset
);
1302 printf("%snew mode %06o%s\n", set
, two
->mode
, reset
);
1304 if (xfrm_msg
&& xfrm_msg
[0])
1305 printf("%s%s%s\n", set
, xfrm_msg
, reset
);
1307 * we do not run diff between different kind
1310 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1311 goto free_ab_and_return
;
1312 if (complete_rewrite
) {
1313 emit_rewrite_diff(name_a
, name_b
, one
, two
,
1315 o
->found_changes
= 1;
1316 goto free_ab_and_return
;
1320 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1321 die("unable to read files to diff");
1324 (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
))) {
1325 /* Quite common confusing case */
1326 if (mf1
.size
== mf2
.size
&&
1327 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1328 goto free_ab_and_return
;
1330 emit_binary_diff(&mf1
, &mf2
);
1332 printf("Binary files %s and %s differ\n",
1334 o
->found_changes
= 1;
1337 /* Crazy xdl interfaces.. */
1338 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1342 struct emit_callback ecbdata
;
1343 const char *funcname_pattern
;
1345 funcname_pattern
= diff_funcname_pattern(one
);
1346 if (!funcname_pattern
)
1347 funcname_pattern
= diff_funcname_pattern(two
);
1349 memset(&xecfg
, 0, sizeof(xecfg
));
1350 memset(&ecbdata
, 0, sizeof(ecbdata
));
1351 ecbdata
.label_path
= lbl
;
1352 ecbdata
.color_diff
= o
->color_diff
;
1353 ecbdata
.found_changesp
= &o
->found_changes
;
1354 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1355 xecfg
.ctxlen
= o
->context
;
1356 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1357 if (funcname_pattern
)
1358 xdiff_set_find_func(&xecfg
, funcname_pattern
);
1361 else if (!prefixcmp(diffopts
, "--unified="))
1362 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1363 else if (!prefixcmp(diffopts
, "-u"))
1364 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1365 ecb
.outf
= xdiff_outf
;
1366 ecb
.priv
= &ecbdata
;
1367 ecbdata
.xm
.consume
= fn_out_consume
;
1368 if (o
->color_diff_words
)
1369 ecbdata
.diff_words
=
1370 xcalloc(1, sizeof(struct diff_words_data
));
1371 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1372 if (o
->color_diff_words
)
1373 free_diff_words_data(&ecbdata
);
1377 diff_free_filespec_data(one
);
1378 diff_free_filespec_data(two
);
1384 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1385 struct diff_filespec
*one
,
1386 struct diff_filespec
*two
,
1387 struct diffstat_t
*diffstat
,
1388 struct diff_options
*o
,
1389 int complete_rewrite
)
1392 struct diffstat_file
*data
;
1394 data
= diffstat_add(diffstat
, name_a
, name_b
);
1397 data
->is_unmerged
= 1;
1400 if (complete_rewrite
) {
1401 diff_populate_filespec(one
, 0);
1402 diff_populate_filespec(two
, 0);
1403 data
->deleted
= count_lines(one
->data
, one
->size
);
1404 data
->added
= count_lines(two
->data
, two
->size
);
1405 goto free_and_return
;
1407 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1408 die("unable to read files to diff");
1410 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1411 data
->is_binary
= 1;
1412 data
->added
= mf2
.size
;
1413 data
->deleted
= mf1
.size
;
1415 /* Crazy xdl interfaces.. */
1420 memset(&xecfg
, 0, sizeof(xecfg
));
1421 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1422 ecb
.outf
= xdiff_outf
;
1423 ecb
.priv
= diffstat
;
1424 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1428 diff_free_filespec_data(one
);
1429 diff_free_filespec_data(two
);
1432 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1433 struct diff_filespec
*one
,
1434 struct diff_filespec
*two
, struct diff_options
*o
)
1437 struct checkdiff_t data
;
1442 memset(&data
, 0, sizeof(data
));
1443 data
.xm
.consume
= checkdiff_consume
;
1444 data
.filename
= name_b
? name_b
: name_a
;
1446 data
.color_diff
= o
->color_diff
;
1448 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1449 die("unable to read files to diff");
1451 if (diff_filespec_is_binary(two
))
1452 goto free_and_return
;
1454 /* Crazy xdl interfaces.. */
1459 memset(&xecfg
, 0, sizeof(xecfg
));
1460 xpp
.flags
= XDF_NEED_MINIMAL
;
1461 ecb
.outf
= xdiff_outf
;
1463 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1466 diff_free_filespec_data(one
);
1467 diff_free_filespec_data(two
);
1470 struct diff_filespec
*alloc_filespec(const char *path
)
1472 int namelen
= strlen(path
);
1473 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1475 memset(spec
, 0, sizeof(*spec
));
1476 spec
->path
= (char *)(spec
+ 1);
1477 memcpy(spec
->path
, path
, namelen
+1);
1481 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1482 unsigned short mode
)
1485 spec
->mode
= canon_mode(mode
);
1486 hashcpy(spec
->sha1
, sha1
);
1487 spec
->sha1_valid
= !is_null_sha1(sha1
);
1492 * Given a name and sha1 pair, if the index tells us the file in
1493 * the work tree has that object contents, return true, so that
1494 * prepare_temp_file() does not have to inflate and extract.
1496 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1498 struct cache_entry
*ce
;
1502 /* We do not read the cache ourselves here, because the
1503 * benchmark with my previous version that always reads cache
1504 * shows that it makes things worse for diff-tree comparing
1505 * two linux-2.6 kernel trees in an already checked out work
1506 * tree. This is because most diff-tree comparisons deal with
1507 * only a small number of files, while reading the cache is
1508 * expensive for a large project, and its cost outweighs the
1509 * savings we get by not inflating the object to a temporary
1510 * file. Practically, this code only helps when we are used
1511 * by diff-cache --cached, which does read the cache before
1517 /* We want to avoid the working directory if our caller
1518 * doesn't need the data in a normal file, this system
1519 * is rather slow with its stat/open/mmap/close syscalls,
1520 * and the object is contained in a pack file. The pack
1521 * is probably already open and will be faster to obtain
1522 * the data through than the working directory. Loose
1523 * objects however would tend to be slower as they need
1524 * to be individually opened and inflated.
1526 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1530 pos
= cache_name_pos(name
, len
);
1533 ce
= active_cache
[pos
];
1534 if ((lstat(name
, &st
) < 0) ||
1535 !S_ISREG(st
.st_mode
) || /* careful! */
1536 ce_match_stat(ce
, &st
, 0) ||
1537 hashcmp(sha1
, ce
->sha1
))
1539 /* we return 1 only when we can stat, it is a regular file,
1540 * stat information matches, and sha1 recorded in the cache
1541 * matches. I.e. we know the file in the work tree really is
1542 * the same as the <name, sha1> pair.
1547 static int populate_from_stdin(struct diff_filespec
*s
)
1549 #define INCREMENT 1024
1557 buf
= xrealloc(buf
, size
+ INCREMENT
);
1558 got
= xread(0, buf
+ size
, INCREMENT
);
1562 return error("error while reading from stdin %s",
1566 s
->should_munmap
= 0;
1573 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1576 char *data
= xmalloc(100);
1577 len
= snprintf(data
, 100,
1578 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1590 * While doing rename detection and pickaxe operation, we may need to
1591 * grab the data for the blob (or file) for our own in-core comparison.
1592 * diff_filespec has data and size fields for this purpose.
1594 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1597 if (!DIFF_FILE_VALID(s
))
1598 die("internal error: asking to populate invalid file.");
1599 if (S_ISDIR(s
->mode
))
1605 if (size_only
&& 0 < s
->size
)
1608 if (S_ISGITLINK(s
->mode
))
1609 return diff_populate_gitlink(s
, size_only
);
1611 if (!s
->sha1_valid
||
1612 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1618 if (!strcmp(s
->path
, "-"))
1619 return populate_from_stdin(s
);
1621 if (lstat(s
->path
, &st
) < 0) {
1622 if (errno
== ENOENT
) {
1626 s
->data
= (char *)"";
1631 s
->size
= xsize_t(st
.st_size
);
1636 if (S_ISLNK(st
.st_mode
)) {
1638 s
->data
= xmalloc(s
->size
);
1640 ret
= readlink(s
->path
, s
->data
, s
->size
);
1647 fd
= open(s
->path
, O_RDONLY
);
1650 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1652 s
->should_munmap
= 1;
1655 * Convert from working tree format to canonical git format
1658 buf
= convert_to_git(s
->path
, s
->data
, &size
);
1660 munmap(s
->data
, s
->size
);
1661 s
->should_munmap
= 0;
1668 enum object_type type
;
1670 type
= sha1_object_info(s
->sha1
, &s
->size
);
1672 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1679 void diff_free_filespec_data(struct diff_filespec
*s
)
1683 else if (s
->should_munmap
)
1684 munmap(s
->data
, s
->size
);
1686 if (s
->should_free
|| s
->should_munmap
) {
1687 s
->should_free
= s
->should_munmap
= 0;
1694 static void prep_temp_blob(struct diff_tempfile
*temp
,
1697 const unsigned char *sha1
,
1702 fd
= git_mkstemp(temp
->tmp_path
, PATH_MAX
, ".diff_XXXXXX");
1704 die("unable to create temp-file: %s", strerror(errno
));
1705 if (write_in_full(fd
, blob
, size
) != size
)
1706 die("unable to write temp-file");
1708 temp
->name
= temp
->tmp_path
;
1709 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1711 sprintf(temp
->mode
, "%06o", mode
);
1714 static void prepare_temp_file(const char *name
,
1715 struct diff_tempfile
*temp
,
1716 struct diff_filespec
*one
)
1718 if (!DIFF_FILE_VALID(one
)) {
1720 /* A '-' entry produces this for file-2, and
1721 * a '+' entry produces this for file-1.
1723 temp
->name
= "/dev/null";
1724 strcpy(temp
->hex
, ".");
1725 strcpy(temp
->mode
, ".");
1729 if (!one
->sha1_valid
||
1730 reuse_worktree_file(name
, one
->sha1
, 1)) {
1732 if (lstat(name
, &st
) < 0) {
1733 if (errno
== ENOENT
)
1734 goto not_a_valid_file
;
1735 die("stat(%s): %s", name
, strerror(errno
));
1737 if (S_ISLNK(st
.st_mode
)) {
1739 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1740 size_t sz
= xsize_t(st
.st_size
);
1741 if (sizeof(buf
) <= st
.st_size
)
1742 die("symlink too long: %s", name
);
1743 ret
= readlink(name
, buf
, sz
);
1745 die("readlink(%s)", name
);
1746 prep_temp_blob(temp
, buf
, sz
,
1748 one
->sha1
: null_sha1
),
1750 one
->mode
: S_IFLNK
));
1753 /* we can borrow from the file in the work tree */
1755 if (!one
->sha1_valid
)
1756 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1758 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1759 /* Even though we may sometimes borrow the
1760 * contents from the work tree, we always want
1761 * one->mode. mode is trustworthy even when
1762 * !(one->sha1_valid), as long as
1763 * DIFF_FILE_VALID(one).
1765 sprintf(temp
->mode
, "%06o", one
->mode
);
1770 if (diff_populate_filespec(one
, 0))
1771 die("cannot read data blob for %s", one
->path
);
1772 prep_temp_blob(temp
, one
->data
, one
->size
,
1773 one
->sha1
, one
->mode
);
1777 static void remove_tempfile(void)
1781 for (i
= 0; i
< 2; i
++)
1782 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1783 unlink(diff_temp
[i
].name
);
1784 diff_temp
[i
].name
= NULL
;
1788 static void remove_tempfile_on_signal(int signo
)
1791 signal(SIGINT
, SIG_DFL
);
1795 static int spawn_prog(const char *pgm
, const char **arg
)
1801 pid
= spawnvpe_pipe(pgm
, arg
, (const char**) environ
, NULL
, NULL
);
1803 die("unable to fork");
1805 while (waitpid(pid
, &status
, 0) < 0) {
1811 /* Earlier we did not check the exit status because
1812 * diff exits non-zero if files are different, and
1813 * we are not interested in knowing that. It was a
1814 * mistake which made it harder to quit a diff-*
1815 * session that uses the git-apply-patch-script as
1816 * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF
1817 * should also exit non-zero only when it wants to
1818 * abort the entire diff-* session.
1820 if (WIFEXITED(status
) && !WEXITSTATUS(status
))
1825 /* An external diff command takes:
1827 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1828 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1831 static void run_external_diff(const char *pgm
,
1834 struct diff_filespec
*one
,
1835 struct diff_filespec
*two
,
1836 const char *xfrm_msg
,
1837 int complete_rewrite
)
1839 const char *spawn_arg
[10];
1840 struct diff_tempfile
*temp
= diff_temp
;
1842 static int atexit_asked
= 0;
1843 const char *othername
;
1844 const char **arg
= &spawn_arg
[1];
1846 othername
= (other
? other
: name
);
1848 prepare_temp_file(name
, &temp
[0], one
);
1849 prepare_temp_file(othername
, &temp
[1], two
);
1850 if (! atexit_asked
&&
1851 (temp
[0].name
== temp
[0].tmp_path
||
1852 temp
[1].name
== temp
[1].tmp_path
)) {
1854 atexit(remove_tempfile
);
1856 signal(SIGINT
, remove_tempfile_on_signal
);
1861 *arg
++ = temp
[0].name
;
1862 *arg
++ = temp
[0].hex
;
1863 *arg
++ = temp
[0].mode
;
1864 *arg
++ = temp
[1].name
;
1865 *arg
++ = temp
[1].hex
;
1866 *arg
++ = temp
[1].mode
;
1875 retval
= spawn_prog(pgm
, spawn_arg
);
1878 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1883 static const char *external_diff_attr(const char *name
)
1885 struct git_attr_check attr_diff_check
;
1887 setup_diff_attr_check(&attr_diff_check
);
1888 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
1889 const char *value
= attr_diff_check
.value
;
1890 if (!ATTR_TRUE(value
) &&
1891 !ATTR_FALSE(value
) &&
1892 !ATTR_UNSET(value
)) {
1893 struct ll_diff_driver
*drv
;
1895 read_config_if_needed();
1896 for (drv
= user_diff
; drv
; drv
= drv
->next
)
1897 if (!strcmp(drv
->name
, value
))
1904 static void run_diff_cmd(const char *pgm
,
1907 struct diff_filespec
*one
,
1908 struct diff_filespec
*two
,
1909 const char *xfrm_msg
,
1910 struct diff_options
*o
,
1911 int complete_rewrite
)
1913 if (!o
->allow_external
)
1916 const char *cmd
= external_diff_attr(name
);
1922 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1927 builtin_diff(name
, other
? other
: name
,
1928 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1930 printf("* Unmerged path %s\n", name
);
1933 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1935 if (DIFF_FILE_VALID(one
)) {
1936 if (!one
->sha1_valid
) {
1938 if (!strcmp(one
->path
, "-")) {
1939 hashcpy(one
->sha1
, null_sha1
);
1942 if (lstat(one
->path
, &st
) < 0)
1943 die("stat %s", one
->path
);
1944 if (index_path(one
->sha1
, one
->path
, &st
, 0))
1945 die("cannot hash %s\n", one
->path
);
1952 static int similarity_index(struct diff_filepair
*p
)
1954 return p
->score
* 100 / MAX_SCORE
;
1957 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
1959 const char *pgm
= external_diff();
1960 char msg
[PATH_MAX
*2+300], *xfrm_msg
;
1961 struct diff_filespec
*one
;
1962 struct diff_filespec
*two
;
1965 char *name_munged
, *other_munged
;
1966 int complete_rewrite
= 0;
1969 if (DIFF_PAIR_UNMERGED(p
)) {
1971 run_diff_cmd(pgm
, p
->one
->path
, NULL
, NULL
, NULL
, NULL
, o
, 0);
1975 name
= p
->one
->path
;
1976 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
1977 name_munged
= quote_one(name
);
1978 other_munged
= quote_one(other
);
1979 one
= p
->one
; two
= p
->two
;
1981 diff_fill_sha1_info(one
);
1982 diff_fill_sha1_info(two
);
1985 switch (p
->status
) {
1986 case DIFF_STATUS_COPIED
:
1987 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1988 "similarity index %d%%\n"
1991 similarity_index(p
), name_munged
, other_munged
);
1993 case DIFF_STATUS_RENAMED
:
1994 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
1995 "similarity index %d%%\n"
1998 similarity_index(p
), name_munged
, other_munged
);
2000 case DIFF_STATUS_MODIFIED
:
2002 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2003 "dissimilarity index %d%%\n",
2004 similarity_index(p
));
2005 complete_rewrite
= 1;
2014 if (hashcmp(one
->sha1
, two
->sha1
)) {
2015 int abbrev
= o
->full_index
? 40 : DEFAULT_ABBREV
;
2019 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2020 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2023 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2025 abbrev
, sha1_to_hex(one
->sha1
),
2026 abbrev
, sha1_to_hex(two
->sha1
));
2027 if (one
->mode
== two
->mode
)
2028 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
,
2029 " %06o", one
->mode
);
2030 len
+= snprintf(msg
+ len
, sizeof(msg
) - len
, "\n");
2035 xfrm_msg
= len
? msg
: NULL
;
2038 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2039 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2040 /* a filepair that changes between file and symlink
2041 * needs to be split into deletion and creation.
2043 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2044 run_diff_cmd(NULL
, name
, other
, one
, null
, xfrm_msg
, o
, 0);
2046 null
= alloc_filespec(one
->path
);
2047 run_diff_cmd(NULL
, name
, other
, null
, two
, xfrm_msg
, o
, 0);
2051 run_diff_cmd(pgm
, name
, other
, one
, two
, xfrm_msg
, o
,
2058 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2059 struct diffstat_t
*diffstat
)
2063 int complete_rewrite
= 0;
2065 if (DIFF_PAIR_UNMERGED(p
)) {
2067 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2071 name
= p
->one
->path
;
2072 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2074 diff_fill_sha1_info(p
->one
);
2075 diff_fill_sha1_info(p
->two
);
2077 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2078 complete_rewrite
= 1;
2079 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2082 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2087 if (DIFF_PAIR_UNMERGED(p
)) {
2092 name
= p
->one
->path
;
2093 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2095 diff_fill_sha1_info(p
->one
);
2096 diff_fill_sha1_info(p
->two
);
2098 builtin_checkdiff(name
, other
, p
->one
, p
->two
, o
);
2101 void diff_setup(struct diff_options
*options
)
2103 memset(options
, 0, sizeof(*options
));
2104 options
->line_termination
= '\n';
2105 options
->break_opt
= -1;
2106 options
->rename_limit
= -1;
2107 options
->context
= 3;
2108 options
->msg_sep
= "";
2110 options
->change
= diff_change
;
2111 options
->add_remove
= diff_addremove
;
2112 options
->color_diff
= diff_use_color_default
;
2113 options
->detect_rename
= diff_detect_rename_default
;
2116 int diff_setup_done(struct diff_options
*options
)
2120 if (options
->output_format
& DIFF_FORMAT_NAME
)
2122 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2124 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2126 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2129 die("--name-only, --name-status, --check and -s are mutually exclusive");
2131 if (options
->find_copies_harder
)
2132 options
->detect_rename
= DIFF_DETECT_COPY
;
2134 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2135 DIFF_FORMAT_NAME_STATUS
|
2136 DIFF_FORMAT_CHECKDIFF
|
2137 DIFF_FORMAT_NO_OUTPUT
))
2138 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2139 DIFF_FORMAT_NUMSTAT
|
2140 DIFF_FORMAT_DIFFSTAT
|
2141 DIFF_FORMAT_SHORTSTAT
|
2142 DIFF_FORMAT_SUMMARY
|
2146 * These cases always need recursive; we do not drop caller-supplied
2147 * recursive bits for other formats here.
2149 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2150 DIFF_FORMAT_NUMSTAT
|
2151 DIFF_FORMAT_DIFFSTAT
|
2152 DIFF_FORMAT_SHORTSTAT
|
2153 DIFF_FORMAT_SUMMARY
|
2154 DIFF_FORMAT_CHECKDIFF
))
2155 options
->recursive
= 1;
2157 * Also pickaxe would not work very well if you do not say recursive
2159 if (options
->pickaxe
)
2160 options
->recursive
= 1;
2162 if (options
->detect_rename
&& options
->rename_limit
< 0)
2163 options
->rename_limit
= diff_rename_limit_default
;
2164 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2166 /* read-cache does not die even when it fails
2167 * so it is safe for us to do this here. Also
2168 * it does not smudge active_cache or active_nr
2169 * when it fails, so we do not have to worry about
2170 * cleaning it up ourselves either.
2174 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2175 options
->abbrev
= 40; /* full */
2178 * It does not make sense to show the first hit we happened
2179 * to have found. It does not make sense not to return with
2180 * exit code in such a case either.
2182 if (options
->quiet
) {
2183 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2184 options
->exit_with_status
= 1;
2188 * If we postprocess in diffcore, we cannot simply return
2189 * upon the first hit. We need to run diff as usual.
2191 if (options
->pickaxe
|| options
->filter
)
2197 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2207 if (c
== arg_short
) {
2211 if (val
&& isdigit(c
)) {
2213 int n
= strtoul(arg
, &end
, 10);
2224 eq
= strchr(arg
, '=');
2229 if (!len
|| strncmp(arg
, arg_long
, len
))
2234 if (!isdigit(*++eq
))
2236 n
= strtoul(eq
, &end
, 10);
2244 static int diff_scoreopt_parse(const char *opt
);
2246 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2248 const char *arg
= av
[0];
2249 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2250 options
->output_format
|= DIFF_FORMAT_PATCH
;
2251 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2252 options
->output_format
|= DIFF_FORMAT_PATCH
;
2253 else if (!strcmp(arg
, "--raw"))
2254 options
->output_format
|= DIFF_FORMAT_RAW
;
2255 else if (!strcmp(arg
, "--patch-with-raw")) {
2256 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2258 else if (!strcmp(arg
, "--numstat")) {
2259 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2261 else if (!strcmp(arg
, "--shortstat")) {
2262 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2264 else if (!prefixcmp(arg
, "--stat")) {
2266 int width
= options
->stat_width
;
2267 int name_width
= options
->stat_name_width
;
2273 if (!prefixcmp(arg
, "-width="))
2274 width
= strtoul(arg
+ 7, &end
, 10);
2275 else if (!prefixcmp(arg
, "-name-width="))
2276 name_width
= strtoul(arg
+ 12, &end
, 10);
2279 width
= strtoul(arg
+1, &end
, 10);
2281 name_width
= strtoul(end
+1, &end
, 10);
2284 /* Important! This checks all the error cases! */
2287 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2288 options
->stat_name_width
= name_width
;
2289 options
->stat_width
= width
;
2291 else if (!strcmp(arg
, "--check"))
2292 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2293 else if (!strcmp(arg
, "--summary"))
2294 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2295 else if (!strcmp(arg
, "--patch-with-stat")) {
2296 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2298 else if (!strcmp(arg
, "-z"))
2299 options
->line_termination
= 0;
2300 else if (!prefixcmp(arg
, "-l"))
2301 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2302 else if (!strcmp(arg
, "--full-index"))
2303 options
->full_index
= 1;
2304 else if (!strcmp(arg
, "--binary")) {
2305 options
->output_format
|= DIFF_FORMAT_PATCH
;
2306 options
->binary
= 1;
2308 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text")) {
2311 else if (!strcmp(arg
, "--name-only"))
2312 options
->output_format
|= DIFF_FORMAT_NAME
;
2313 else if (!strcmp(arg
, "--name-status"))
2314 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2315 else if (!strcmp(arg
, "-R"))
2316 options
->reverse_diff
= 1;
2317 else if (!prefixcmp(arg
, "-S"))
2318 options
->pickaxe
= arg
+ 2;
2319 else if (!strcmp(arg
, "-s")) {
2320 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2322 else if (!prefixcmp(arg
, "-O"))
2323 options
->orderfile
= arg
+ 2;
2324 else if (!prefixcmp(arg
, "--diff-filter="))
2325 options
->filter
= arg
+ 14;
2326 else if (!strcmp(arg
, "--pickaxe-all"))
2327 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2328 else if (!strcmp(arg
, "--pickaxe-regex"))
2329 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2330 else if (!prefixcmp(arg
, "-B")) {
2331 if ((options
->break_opt
=
2332 diff_scoreopt_parse(arg
)) == -1)
2335 else if (!prefixcmp(arg
, "-M")) {
2336 if ((options
->rename_score
=
2337 diff_scoreopt_parse(arg
)) == -1)
2339 options
->detect_rename
= DIFF_DETECT_RENAME
;
2341 else if (!prefixcmp(arg
, "-C")) {
2342 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2343 options
->find_copies_harder
= 1;
2344 if ((options
->rename_score
=
2345 diff_scoreopt_parse(arg
)) == -1)
2347 options
->detect_rename
= DIFF_DETECT_COPY
;
2349 else if (!strcmp(arg
, "--find-copies-harder"))
2350 options
->find_copies_harder
= 1;
2351 else if (!strcmp(arg
, "--follow"))
2352 options
->follow_renames
= 1;
2353 else if (!strcmp(arg
, "--abbrev"))
2354 options
->abbrev
= DEFAULT_ABBREV
;
2355 else if (!prefixcmp(arg
, "--abbrev=")) {
2356 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2357 if (options
->abbrev
< MINIMUM_ABBREV
)
2358 options
->abbrev
= MINIMUM_ABBREV
;
2359 else if (40 < options
->abbrev
)
2360 options
->abbrev
= 40;
2362 else if (!strcmp(arg
, "--color"))
2363 options
->color_diff
= 1;
2364 else if (!strcmp(arg
, "--no-color"))
2365 options
->color_diff
= 0;
2366 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2367 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2368 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2369 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2370 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2371 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2372 else if (!strcmp(arg
, "--color-words"))
2373 options
->color_diff
= options
->color_diff_words
= 1;
2374 else if (!strcmp(arg
, "--no-renames"))
2375 options
->detect_rename
= 0;
2376 else if (!strcmp(arg
, "--exit-code"))
2377 options
->exit_with_status
= 1;
2378 else if (!strcmp(arg
, "--quiet"))
2380 else if (!strcmp(arg
, "--ext-diff"))
2381 options
->allow_external
= 1;
2382 else if (!strcmp(arg
, "--no-ext-diff"))
2383 options
->allow_external
= 0;
2389 static int parse_num(const char **cp_p
)
2391 unsigned long num
, scale
;
2393 const char *cp
= *cp_p
;
2400 if ( !dot
&& ch
== '.' ) {
2403 } else if ( ch
== '%' ) {
2404 scale
= dot
? scale
*100 : 100;
2405 cp
++; /* % is always at the end */
2407 } else if ( ch
>= '0' && ch
<= '9' ) {
2408 if ( scale
< 100000 ) {
2410 num
= (num
*10) + (ch
-'0');
2419 /* user says num divided by scale and we say internally that
2420 * is MAX_SCORE * num / scale.
2422 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2425 static int diff_scoreopt_parse(const char *opt
)
2427 int opt1
, opt2
, cmd
;
2432 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2433 return -1; /* that is not a -M, -C nor -B option */
2435 opt1
= parse_num(&opt
);
2441 else if (*opt
!= '/')
2442 return -1; /* we expect -B80/99 or -B80 */
2445 opt2
= parse_num(&opt
);
2450 return opt1
| (opt2
<< 16);
2453 struct diff_queue_struct diff_queued_diff
;
2455 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2457 if (queue
->alloc
<= queue
->nr
) {
2458 queue
->alloc
= alloc_nr(queue
->alloc
);
2459 queue
->queue
= xrealloc(queue
->queue
,
2460 sizeof(dp
) * queue
->alloc
);
2462 queue
->queue
[queue
->nr
++] = dp
;
2465 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2466 struct diff_filespec
*one
,
2467 struct diff_filespec
*two
)
2469 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2477 void diff_free_filepair(struct diff_filepair
*p
)
2479 diff_free_filespec_data(p
->one
);
2480 diff_free_filespec_data(p
->two
);
2486 /* This is different from find_unique_abbrev() in that
2487 * it stuffs the result with dots for alignment.
2489 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2494 return sha1_to_hex(sha1
);
2496 abbrev
= find_unique_abbrev(sha1
, len
);
2498 return sha1_to_hex(sha1
);
2499 abblen
= strlen(abbrev
);
2501 static char hex
[41];
2502 if (len
< abblen
&& abblen
<= len
+ 2)
2503 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2505 sprintf(hex
, "%s...", abbrev
);
2508 return sha1_to_hex(sha1
);
2511 static void diff_flush_raw(struct diff_filepair
*p
,
2512 struct diff_options
*options
)
2516 int abbrev
= options
->abbrev
;
2517 const char *path_one
, *path_two
;
2518 int inter_name_termination
= '\t';
2519 int line_termination
= options
->line_termination
;
2521 if (!line_termination
)
2522 inter_name_termination
= 0;
2524 path_one
= p
->one
->path
;
2525 path_two
= p
->two
->path
;
2526 if (line_termination
) {
2527 path_one
= quote_one(path_one
);
2528 path_two
= quote_one(path_two
);
2532 sprintf(status
, "%c%03d", p
->status
, similarity_index(p
));
2534 status
[0] = p
->status
;
2537 switch (p
->status
) {
2538 case DIFF_STATUS_COPIED
:
2539 case DIFF_STATUS_RENAMED
:
2542 case DIFF_STATUS_ADDED
:
2543 case DIFF_STATUS_DELETED
:
2550 if (!(options
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2551 printf(":%06o %06o %s ",
2552 p
->one
->mode
, p
->two
->mode
,
2553 diff_unique_abbrev(p
->one
->sha1
, abbrev
));
2555 diff_unique_abbrev(p
->two
->sha1
, abbrev
));
2557 printf("%s%c%s", status
, inter_name_termination
,
2558 two_paths
|| p
->one
->mode
? path_one
: path_two
);
2560 printf("%c%s", inter_name_termination
, path_two
);
2561 putchar(line_termination
);
2562 if (path_one
!= p
->one
->path
)
2563 free((void*)path_one
);
2564 if (path_two
!= p
->two
->path
)
2565 free((void*)path_two
);
2568 static void diff_flush_name(struct diff_filepair
*p
, struct diff_options
*opt
)
2570 char *path
= p
->two
->path
;
2572 if (opt
->line_termination
)
2573 path
= quote_one(p
->two
->path
);
2574 printf("%s%c", path
, opt
->line_termination
);
2575 if (p
->two
->path
!= path
)
2579 int diff_unmodified_pair(struct diff_filepair
*p
)
2581 /* This function is written stricter than necessary to support
2582 * the currently implemented transformers, but the idea is to
2583 * let transformers to produce diff_filepairs any way they want,
2584 * and filter and clean them up here before producing the output.
2586 struct diff_filespec
*one
, *two
;
2588 if (DIFF_PAIR_UNMERGED(p
))
2589 return 0; /* unmerged is interesting */
2594 /* deletion, addition, mode or type change
2595 * and rename are all interesting.
2597 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2598 DIFF_PAIR_MODE_CHANGED(p
) ||
2599 strcmp(one
->path
, two
->path
))
2602 /* both are valid and point at the same path. that is, we are
2603 * dealing with a change.
2605 if (one
->sha1_valid
&& two
->sha1_valid
&&
2606 !hashcmp(one
->sha1
, two
->sha1
))
2607 return 1; /* no change */
2608 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2609 return 1; /* both look at the same file on the filesystem. */
2613 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2615 if (diff_unmodified_pair(p
))
2618 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2619 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2620 return; /* no tree diffs in patch format */
2625 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2626 struct diffstat_t
*diffstat
)
2628 if (diff_unmodified_pair(p
))
2631 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2632 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2633 return; /* no tree diffs in patch format */
2635 run_diffstat(p
, o
, diffstat
);
2638 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2639 struct diff_options
*o
)
2641 if (diff_unmodified_pair(p
))
2644 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2645 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2646 return; /* no tree diffs in patch format */
2648 run_checkdiff(p
, o
);
2651 int diff_queue_is_empty(void)
2653 struct diff_queue_struct
*q
= &diff_queued_diff
;
2655 for (i
= 0; i
< q
->nr
; i
++)
2656 if (!diff_unmodified_pair(q
->queue
[i
]))
2662 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2664 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2667 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2669 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2670 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2672 s
->size
, s
->xfrm_flags
);
2675 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2677 diff_debug_filespec(p
->one
, i
, "one");
2678 diff_debug_filespec(p
->two
, i
, "two");
2679 fprintf(stderr
, "score %d, status %c stays %d broken %d\n",
2680 p
->score
, p
->status
? p
->status
: '?',
2681 p
->source_stays
, p
->broken_pair
);
2684 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2688 fprintf(stderr
, "%s\n", msg
);
2689 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2690 for (i
= 0; i
< q
->nr
; i
++) {
2691 struct diff_filepair
*p
= q
->queue
[i
];
2692 diff_debug_filepair(p
, i
);
2697 static void diff_resolve_rename_copy(void)
2700 struct diff_filepair
*p
, *pp
;
2701 struct diff_queue_struct
*q
= &diff_queued_diff
;
2703 diff_debug_queue("resolve-rename-copy", q
);
2705 for (i
= 0; i
< q
->nr
; i
++) {
2707 p
->status
= 0; /* undecided */
2708 if (DIFF_PAIR_UNMERGED(p
))
2709 p
->status
= DIFF_STATUS_UNMERGED
;
2710 else if (!DIFF_FILE_VALID(p
->one
))
2711 p
->status
= DIFF_STATUS_ADDED
;
2712 else if (!DIFF_FILE_VALID(p
->two
))
2713 p
->status
= DIFF_STATUS_DELETED
;
2714 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2715 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2717 /* from this point on, we are dealing with a pair
2718 * whose both sides are valid and of the same type, i.e.
2719 * either in-place edit or rename/copy edit.
2721 else if (DIFF_PAIR_RENAME(p
)) {
2722 if (p
->source_stays
) {
2723 p
->status
= DIFF_STATUS_COPIED
;
2726 /* See if there is some other filepair that
2727 * copies from the same source as us. If so
2728 * we are a copy. Otherwise we are either a
2729 * copy if the path stays, or a rename if it
2730 * does not, but we already handled "stays" case.
2732 for (j
= i
+ 1; j
< q
->nr
; j
++) {
2734 if (strcmp(pp
->one
->path
, p
->one
->path
))
2735 continue; /* not us */
2736 if (!DIFF_PAIR_RENAME(pp
))
2737 continue; /* not a rename/copy */
2738 /* pp is a rename/copy from the same source */
2739 p
->status
= DIFF_STATUS_COPIED
;
2743 p
->status
= DIFF_STATUS_RENAMED
;
2745 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2746 p
->one
->mode
!= p
->two
->mode
||
2747 is_null_sha1(p
->one
->sha1
))
2748 p
->status
= DIFF_STATUS_MODIFIED
;
2750 /* This is a "no-change" entry and should not
2751 * happen anymore, but prepare for broken callers.
2753 error("feeding unmodified %s to diffcore",
2755 p
->status
= DIFF_STATUS_UNKNOWN
;
2758 diff_debug_queue("resolve-rename-copy done", q
);
2761 static int check_pair_status(struct diff_filepair
*p
)
2763 switch (p
->status
) {
2764 case DIFF_STATUS_UNKNOWN
:
2767 die("internal error in diff-resolve-rename-copy");
2773 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2775 int fmt
= opt
->output_format
;
2777 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2778 diff_flush_checkdiff(p
, opt
);
2779 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2780 diff_flush_raw(p
, opt
);
2781 else if (fmt
& DIFF_FORMAT_NAME
)
2782 diff_flush_name(p
, opt
);
2785 static void show_file_mode_name(const char *newdelete
, struct diff_filespec
*fs
)
2787 char *name
= quote_one(fs
->path
);
2789 printf(" %s mode %06o %s\n", newdelete
, fs
->mode
, name
);
2791 printf(" %s %s\n", newdelete
, name
);
2796 static void show_mode_change(struct diff_filepair
*p
, int show_name
)
2798 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2800 char *name
= quote_one(p
->two
->path
);
2801 printf(" mode change %06o => %06o %s\n",
2802 p
->one
->mode
, p
->two
->mode
, name
);
2806 printf(" mode change %06o => %06o\n",
2807 p
->one
->mode
, p
->two
->mode
);
2811 static void show_rename_copy(const char *renamecopy
, struct diff_filepair
*p
)
2813 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
2815 printf(" %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
2817 show_mode_change(p
, 0);
2820 static void diff_summary(struct diff_filepair
*p
)
2823 case DIFF_STATUS_DELETED
:
2824 show_file_mode_name("delete", p
->one
);
2826 case DIFF_STATUS_ADDED
:
2827 show_file_mode_name("create", p
->two
);
2829 case DIFF_STATUS_COPIED
:
2830 show_rename_copy("copy", p
);
2832 case DIFF_STATUS_RENAMED
:
2833 show_rename_copy("rename", p
);
2837 char *name
= quote_one(p
->two
->path
);
2838 printf(" rewrite %s (%d%%)\n", name
,
2839 similarity_index(p
));
2841 show_mode_change(p
, 0);
2842 } else show_mode_change(p
, 1);
2848 struct xdiff_emit_state xm
;
2853 static int remove_space(char *line
, int len
)
2859 for (i
= 0; i
< len
; i
++)
2860 if (!isspace((c
= line
[i
])))
2866 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2868 struct patch_id_t
*data
= priv
;
2871 /* Ignore line numbers when computing the SHA1 of the patch */
2872 if (!prefixcmp(line
, "@@ -"))
2875 new_len
= remove_space(line
, len
);
2877 SHA1_Update(data
->ctx
, line
, new_len
);
2878 data
->patchlen
+= new_len
;
2881 /* returns 0 upon success, and writes result into sha1 */
2882 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2884 struct diff_queue_struct
*q
= &diff_queued_diff
;
2887 struct patch_id_t data
;
2888 char buffer
[PATH_MAX
* 4 + 20];
2891 memset(&data
, 0, sizeof(struct patch_id_t
));
2893 data
.xm
.consume
= patch_id_consume
;
2895 for (i
= 0; i
< q
->nr
; i
++) {
2900 struct diff_filepair
*p
= q
->queue
[i
];
2903 memset(&xecfg
, 0, sizeof(xecfg
));
2905 return error("internal diff status error");
2906 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2908 if (diff_unmodified_pair(p
))
2910 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2911 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2913 if (DIFF_PAIR_UNMERGED(p
))
2916 diff_fill_sha1_info(p
->one
);
2917 diff_fill_sha1_info(p
->two
);
2918 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2919 fill_mmfile(&mf2
, p
->two
) < 0)
2920 return error("unable to read files to diff");
2922 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2923 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2924 if (p
->one
->mode
== 0)
2925 len1
= snprintf(buffer
, sizeof(buffer
),
2926 "diff--gita/%.*sb/%.*s"
2933 len2
, p
->two
->path
);
2934 else if (p
->two
->mode
== 0)
2935 len1
= snprintf(buffer
, sizeof(buffer
),
2936 "diff--gita/%.*sb/%.*s"
2937 "deletedfilemode%06o"
2943 len1
, p
->one
->path
);
2945 len1
= snprintf(buffer
, sizeof(buffer
),
2946 "diff--gita/%.*sb/%.*s"
2952 len2
, p
->two
->path
);
2953 SHA1_Update(&ctx
, buffer
, len1
);
2955 xpp
.flags
= XDF_NEED_MINIMAL
;
2957 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
2958 ecb
.outf
= xdiff_outf
;
2960 xdl_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
2963 SHA1_Final(sha1
, &ctx
);
2967 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2969 struct diff_queue_struct
*q
= &diff_queued_diff
;
2971 int result
= diff_get_patch_id(options
, sha1
);
2973 for (i
= 0; i
< q
->nr
; i
++)
2974 diff_free_filepair(q
->queue
[i
]);
2978 q
->nr
= q
->alloc
= 0;
2983 static int is_summary_empty(const struct diff_queue_struct
*q
)
2987 for (i
= 0; i
< q
->nr
; i
++) {
2988 const struct diff_filepair
*p
= q
->queue
[i
];
2990 switch (p
->status
) {
2991 case DIFF_STATUS_DELETED
:
2992 case DIFF_STATUS_ADDED
:
2993 case DIFF_STATUS_COPIED
:
2994 case DIFF_STATUS_RENAMED
:
2999 if (p
->one
->mode
&& p
->two
->mode
&&
3000 p
->one
->mode
!= p
->two
->mode
)
3008 void diff_flush(struct diff_options
*options
)
3010 struct diff_queue_struct
*q
= &diff_queued_diff
;
3011 int i
, output_format
= options
->output_format
;
3015 * Order: raw, stat, summary, patch
3016 * or: name/name-status/checkdiff (other bits clear)
3021 if (output_format
& (DIFF_FORMAT_RAW
|
3023 DIFF_FORMAT_NAME_STATUS
|
3024 DIFF_FORMAT_CHECKDIFF
)) {
3025 for (i
= 0; i
< q
->nr
; i
++) {
3026 struct diff_filepair
*p
= q
->queue
[i
];
3027 if (check_pair_status(p
))
3028 flush_one_pair(p
, options
);
3033 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3034 struct diffstat_t diffstat
;
3036 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3037 diffstat
.xm
.consume
= diffstat_consume
;
3038 for (i
= 0; i
< q
->nr
; i
++) {
3039 struct diff_filepair
*p
= q
->queue
[i
];
3040 if (check_pair_status(p
))
3041 diff_flush_stat(p
, options
, &diffstat
);
3043 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3044 show_numstat(&diffstat
, options
);
3045 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3046 show_stats(&diffstat
, options
);
3047 else if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3048 show_shortstats(&diffstat
);
3052 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3053 for (i
= 0; i
< q
->nr
; i
++)
3054 diff_summary(q
->queue
[i
]);
3058 if (output_format
& DIFF_FORMAT_PATCH
) {
3060 if (options
->stat_sep
) {
3061 /* attach patch instead of inline */
3062 fputs(options
->stat_sep
, stdout
);
3064 putchar(options
->line_termination
);
3068 for (i
= 0; i
< q
->nr
; i
++) {
3069 struct diff_filepair
*p
= q
->queue
[i
];
3070 if (check_pair_status(p
))
3071 diff_flush_patch(p
, options
);
3075 if (output_format
& DIFF_FORMAT_CALLBACK
)
3076 options
->format_callback(q
, options
, options
->format_callback_data
);
3078 for (i
= 0; i
< q
->nr
; i
++)
3079 diff_free_filepair(q
->queue
[i
]);
3083 q
->nr
= q
->alloc
= 0;
3086 static void diffcore_apply_filter(const char *filter
)
3089 struct diff_queue_struct
*q
= &diff_queued_diff
;
3090 struct diff_queue_struct outq
;
3092 outq
.nr
= outq
.alloc
= 0;
3097 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3099 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3100 struct diff_filepair
*p
= q
->queue
[i
];
3101 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3103 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3105 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3106 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3107 strchr(filter
, p
->status
)))
3113 /* otherwise we will clear the whole queue
3114 * by copying the empty outq at the end of this
3115 * function, but first clear the current entries
3118 for (i
= 0; i
< q
->nr
; i
++)
3119 diff_free_filepair(q
->queue
[i
]);
3122 /* Only the matching ones */
3123 for (i
= 0; i
< q
->nr
; i
++) {
3124 struct diff_filepair
*p
= q
->queue
[i
];
3126 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3128 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3130 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3131 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3132 strchr(filter
, p
->status
)))
3135 diff_free_filepair(p
);
3142 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3145 struct diff_queue_struct
*q
= &diff_queued_diff
;
3146 struct diff_queue_struct outq
;
3148 outq
.nr
= outq
.alloc
= 0;
3150 for (i
= 0; i
< q
->nr
; i
++) {
3151 struct diff_filepair
*p
= q
->queue
[i
];
3154 * 1. Entries that come from stat info dirtyness
3155 * always have both sides (iow, not create/delete),
3156 * one side of the object name is unknown, with
3157 * the same mode and size. Keep the ones that
3158 * do not match these criteria. They have real
3161 * 2. At this point, the file is known to be modified,
3162 * with the same mode and size, and the object
3163 * name of one side is unknown. Need to inspect
3164 * the identical contents.
3166 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3167 !DIFF_FILE_VALID(p
->two
) ||
3168 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3169 (p
->one
->mode
!= p
->two
->mode
) ||
3170 diff_populate_filespec(p
->one
, 1) ||
3171 diff_populate_filespec(p
->two
, 1) ||
3172 (p
->one
->size
!= p
->two
->size
) ||
3174 diff_populate_filespec(p
->one
, 0) || /* (2) */
3175 diff_populate_filespec(p
->two
, 0) ||
3176 memcmp(p
->one
->data
, p
->two
->data
, p
->one
->size
))
3180 * The caller can subtract 1 from skip_stat_unmatch
3181 * to determine how many paths were dirty only
3182 * due to stat info mismatch.
3184 if (!diffopt
->no_index
)
3185 diffopt
->skip_stat_unmatch
++;
3186 diff_free_filepair(p
);
3193 void diffcore_std(struct diff_options
*options
)
3198 if (options
->skip_stat_unmatch
&& !options
->find_copies_harder
)
3199 diffcore_skip_stat_unmatch(options
);
3200 if (options
->break_opt
!= -1)
3201 diffcore_break(options
->break_opt
);
3202 if (options
->detect_rename
)
3203 diffcore_rename(options
);
3204 if (options
->break_opt
!= -1)
3205 diffcore_merge_broken();
3206 if (options
->pickaxe
)
3207 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3208 if (options
->orderfile
)
3209 diffcore_order(options
->orderfile
);
3210 diff_resolve_rename_copy();
3211 diffcore_apply_filter(options
->filter
);
3213 options
->has_changes
= !!diff_queued_diff
.nr
;
3217 void diff_addremove(struct diff_options
*options
,
3218 int addremove
, unsigned mode
,
3219 const unsigned char *sha1
,
3220 const char *base
, const char *path
)
3222 char concatpath
[PATH_MAX
];
3223 struct diff_filespec
*one
, *two
;
3225 /* This may look odd, but it is a preparation for
3226 * feeding "there are unchanged files which should
3227 * not produce diffs, but when you are doing copy
3228 * detection you would need them, so here they are"
3229 * entries to the diff-core. They will be prefixed
3230 * with something like '=' or '*' (I haven't decided
3231 * which but should not make any difference).
3232 * Feeding the same new and old to diff_change()
3233 * also has the same effect.
3234 * Before the final output happens, they are pruned after
3235 * merged into rename/copy pairs as appropriate.
3237 if (options
->reverse_diff
)
3238 addremove
= (addremove
== '+' ? '-' :
3239 addremove
== '-' ? '+' : addremove
);
3241 if (!path
) path
= "";
3242 sprintf(concatpath
, "%s%s", base
, path
);
3243 one
= alloc_filespec(concatpath
);
3244 two
= alloc_filespec(concatpath
);
3246 if (addremove
!= '+')
3247 fill_filespec(one
, sha1
, mode
);
3248 if (addremove
!= '-')
3249 fill_filespec(two
, sha1
, mode
);
3251 diff_queue(&diff_queued_diff
, one
, two
);
3252 options
->has_changes
= 1;
3255 void diff_change(struct diff_options
*options
,
3256 unsigned old_mode
, unsigned new_mode
,
3257 const unsigned char *old_sha1
,
3258 const unsigned char *new_sha1
,
3259 const char *base
, const char *path
)
3261 char concatpath
[PATH_MAX
];
3262 struct diff_filespec
*one
, *two
;
3264 if (options
->reverse_diff
) {
3266 const unsigned char *tmp_c
;
3267 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3268 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3270 if (!path
) path
= "";
3271 sprintf(concatpath
, "%s%s", base
, path
);
3272 one
= alloc_filespec(concatpath
);
3273 two
= alloc_filespec(concatpath
);
3274 fill_filespec(one
, old_sha1
, old_mode
);
3275 fill_filespec(two
, new_sha1
, new_mode
);
3277 diff_queue(&diff_queued_diff
, one
, two
);
3278 options
->has_changes
= 1;
3281 void diff_unmerge(struct diff_options
*options
,
3283 unsigned mode
, const unsigned char *sha1
)
3285 struct diff_filespec
*one
, *two
;
3286 one
= alloc_filespec(path
);
3287 two
= alloc_filespec(path
);
3288 fill_filespec(one
, sha1
, mode
);
3289 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;