2 * Copyright (C) 2005 Junio C Hamano
9 #include "xdiff-interface.h"
12 #include "run-command.h"
15 #ifdef NO_FAST_WORKING_DIRECTORY
16 #define FAST_WORKING_DIRECTORY 0
18 #define FAST_WORKING_DIRECTORY 1
21 static int diff_detect_rename_default
;
22 static int diff_rename_limit_default
= 200;
23 int diff_use_color_default
= -1;
24 static const char *external_diff_cmd_cfg
;
25 int diff_auto_refresh_index
= 1;
27 static char diff_colors
[][COLOR_MAXLEN
] = {
29 "", /* PLAIN (normal) */
30 "\033[1m", /* METAINFO (bold) */
31 "\033[36m", /* FRAGINFO (cyan) */
32 "\033[31m", /* OLD (red) */
33 "\033[32m", /* NEW (green) */
34 "\033[33m", /* COMMIT (yellow) */
35 "\033[41m", /* WHITESPACE (red background) */
38 static int parse_diff_color_slot(const char *var
, int ofs
)
40 if (!strcasecmp(var
+ofs
, "plain"))
42 if (!strcasecmp(var
+ofs
, "meta"))
44 if (!strcasecmp(var
+ofs
, "frag"))
46 if (!strcasecmp(var
+ofs
, "old"))
48 if (!strcasecmp(var
+ofs
, "new"))
50 if (!strcasecmp(var
+ofs
, "commit"))
52 if (!strcasecmp(var
+ofs
, "whitespace"))
53 return DIFF_WHITESPACE
;
54 die("bad config variable '%s'", var
);
57 static struct ll_diff_driver
{
59 struct ll_diff_driver
*next
;
61 } *user_diff
, **user_diff_tail
;
64 * Currently there is only "diff.<drivername>.command" variable;
65 * because there are "diff.color.<slot>" variables, we are parsing
66 * this in a bit convoluted way to allow low level diff driver
69 static int parse_lldiff_command(const char *var
, const char *ep
, const char *value
)
73 struct ll_diff_driver
*drv
;
77 for (drv
= user_diff
; drv
; drv
= drv
->next
)
78 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
81 drv
= xcalloc(1, sizeof(struct ll_diff_driver
));
82 drv
->name
= xmemdupz(name
, namelen
);
84 user_diff_tail
= &user_diff
;
85 *user_diff_tail
= drv
;
86 user_diff_tail
= &(drv
->next
);
89 return git_config_string(&(drv
->cmd
), var
, value
);
93 * 'diff.<what>.funcname' attribute can be specified in the configuration
94 * to define a customized regexp to find the beginning of a function to
95 * be used for hunk header lines of "diff -p" style output.
97 static struct funcname_pattern
{
100 struct funcname_pattern
*next
;
101 } *funcname_pattern_list
;
103 static int parse_funcname_pattern(const char *var
, const char *ep
, const char *value
)
107 struct funcname_pattern
*pp
;
109 name
= var
+ 5; /* "diff." */
112 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
113 if (!strncmp(pp
->name
, name
, namelen
) && !pp
->name
[namelen
])
116 pp
= xcalloc(1, sizeof(*pp
));
117 pp
->name
= xmemdupz(name
, namelen
);
118 pp
->next
= funcname_pattern_list
;
119 funcname_pattern_list
= pp
;
122 pp
->pattern
= xstrdup(value
);
127 * These are to give UI layer defaults.
128 * The core-level commands such as git-diff-files should
129 * never be affected by the setting of diff.renames
130 * the user happens to have in the configuration file.
132 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
134 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
135 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
138 if (!strcmp(var
, "diff.renames")) {
140 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
141 else if (!strcasecmp(value
, "copies") ||
142 !strcasecmp(value
, "copy"))
143 diff_detect_rename_default
= DIFF_DETECT_COPY
;
144 else if (git_config_bool(var
,value
))
145 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
148 if (!strcmp(var
, "diff.autorefreshindex")) {
149 diff_auto_refresh_index
= git_config_bool(var
, value
);
152 if (!strcmp(var
, "diff.external"))
153 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
154 if (!prefixcmp(var
, "diff.")) {
155 const char *ep
= strrchr(var
, '.');
157 if (ep
!= var
+ 4 && !strcmp(ep
, ".command"))
158 return parse_lldiff_command(var
, ep
, value
);
161 return git_diff_basic_config(var
, value
, cb
);
164 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
166 if (!strcmp(var
, "diff.renamelimit")) {
167 diff_rename_limit_default
= git_config_int(var
, value
);
171 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
172 int slot
= parse_diff_color_slot(var
, 11);
174 return config_error_nonbool(var
);
175 color_parse(value
, var
, diff_colors
[slot
]);
179 if (!prefixcmp(var
, "diff.")) {
180 const char *ep
= strrchr(var
, '.');
182 if (!strcmp(ep
, ".funcname")) {
184 return config_error_nonbool(var
);
185 return parse_funcname_pattern(var
, ep
, value
);
190 return git_color_default_config(var
, value
, cb
);
193 static char *quote_two(const char *one
, const char *two
)
195 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
196 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
199 strbuf_init(&res
, 0);
200 if (need_one
+ need_two
) {
201 strbuf_addch(&res
, '"');
202 quote_c_style(one
, &res
, NULL
, 1);
203 quote_c_style(two
, &res
, NULL
, 1);
204 strbuf_addch(&res
, '"');
206 strbuf_addstr(&res
, one
);
207 strbuf_addstr(&res
, two
);
209 return strbuf_detach(&res
, NULL
);
212 static const char *external_diff(void)
214 static const char *external_diff_cmd
= NULL
;
215 static int done_preparing
= 0;
218 return external_diff_cmd
;
219 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
220 if (!external_diff_cmd
)
221 external_diff_cmd
= external_diff_cmd_cfg
;
223 return external_diff_cmd
;
226 static struct diff_tempfile
{
227 const char *name
; /* filename external diff should read from */
230 char tmp_path
[PATH_MAX
];
233 static int count_lines(const char *data
, int size
)
235 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
242 completely_empty
= 0;
246 completely_empty
= 0;
249 if (completely_empty
)
252 count
++; /* no trailing newline */
256 static void print_line_count(FILE *file
, int count
)
260 fprintf(file
, "0,0");
266 fprintf(file
, "1,%d", count
);
271 static void copy_file_with_prefix(FILE *file
,
272 int prefix
, const char *data
, int size
,
273 const char *set
, const char *reset
)
275 int ch
, nl_just_seen
= 1;
290 fprintf(file
, "%s\n\\ No newline at end of file\n", reset
);
293 static void emit_rewrite_diff(const char *name_a
,
295 struct diff_filespec
*one
,
296 struct diff_filespec
*two
,
297 struct diff_options
*o
)
300 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
301 const char *name_a_tab
, *name_b_tab
;
302 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
303 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
304 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
305 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
306 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
307 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
309 name_a
+= (*name_a
== '/');
310 name_b
+= (*name_b
== '/');
311 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
312 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
314 strbuf_reset(&a_name
);
315 strbuf_reset(&b_name
);
316 quote_two_c_style(&a_name
, o
->a_prefix
, name_a
, 0);
317 quote_two_c_style(&b_name
, o
->b_prefix
, name_b
, 0);
319 diff_populate_filespec(one
, 0);
320 diff_populate_filespec(two
, 0);
321 lc_a
= count_lines(one
->data
, one
->size
);
322 lc_b
= count_lines(two
->data
, two
->size
);
324 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
325 metainfo
, a_name
.buf
, name_a_tab
, reset
,
326 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
327 print_line_count(o
->file
, lc_a
);
328 fprintf(o
->file
, " +");
329 print_line_count(o
->file
, lc_b
);
330 fprintf(o
->file
, " @@%s\n", reset
);
332 copy_file_with_prefix(o
->file
, '-', one
->data
, one
->size
, old
, reset
);
334 copy_file_with_prefix(o
->file
, '+', two
->data
, two
->size
, new, reset
);
337 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
339 if (!DIFF_FILE_VALID(one
)) {
340 mf
->ptr
= (char *)""; /* does not matter */
344 else if (diff_populate_filespec(one
, 0))
347 mf
->size
= one
->size
;
351 struct diff_words_buffer
{
354 long current
; /* output pointer */
355 int suppressed_newline
;
358 static void diff_words_append(char *line
, unsigned long len
,
359 struct diff_words_buffer
*buffer
)
361 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
362 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
363 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
367 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
368 buffer
->text
.size
+= len
;
371 struct diff_words_data
{
372 struct diff_words_buffer minus
, plus
;
376 static void print_word(FILE *file
, struct diff_words_buffer
*buffer
, int len
, int color
,
377 int suppress_newline
)
385 ptr
= buffer
->text
.ptr
+ buffer
->current
;
386 buffer
->current
+= len
;
388 if (ptr
[len
- 1] == '\n') {
393 fputs(diff_get_color(1, color
), file
);
394 fwrite(ptr
, len
, 1, file
);
395 fputs(diff_get_color(1, DIFF_RESET
), file
);
398 if (suppress_newline
)
399 buffer
->suppressed_newline
= 1;
405 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
407 struct diff_words_data
*diff_words
= priv
;
409 if (diff_words
->minus
.suppressed_newline
) {
411 putc('\n', diff_words
->file
);
412 diff_words
->minus
.suppressed_newline
= 0;
418 print_word(diff_words
->file
,
419 &diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
422 print_word(diff_words
->file
,
423 &diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
426 print_word(diff_words
->file
,
427 &diff_words
->plus
, len
, DIFF_PLAIN
, 0);
428 diff_words
->minus
.current
+= len
;
433 /* this executes the word diff on the accumulated buffers */
434 static void diff_words_show(struct diff_words_data
*diff_words
)
439 mmfile_t minus
, plus
;
442 memset(&xecfg
, 0, sizeof(xecfg
));
443 minus
.size
= diff_words
->minus
.text
.size
;
444 minus
.ptr
= xmalloc(minus
.size
);
445 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
446 for (i
= 0; i
< minus
.size
; i
++)
447 if (isspace(minus
.ptr
[i
]))
449 diff_words
->minus
.current
= 0;
451 plus
.size
= diff_words
->plus
.text
.size
;
452 plus
.ptr
= xmalloc(plus
.size
);
453 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
454 for (i
= 0; i
< plus
.size
; i
++)
455 if (isspace(plus
.ptr
[i
]))
457 diff_words
->plus
.current
= 0;
459 xpp
.flags
= XDF_NEED_MINIMAL
;
460 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
461 xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
465 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
467 if (diff_words
->minus
.suppressed_newline
) {
468 putc('\n', diff_words
->file
);
469 diff_words
->minus
.suppressed_newline
= 0;
473 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
475 struct emit_callback
{
476 int nparents
, color_diff
;
478 sane_truncate_fn truncate
;
479 const char **label_path
;
480 struct diff_words_data
*diff_words
;
485 static void free_diff_words_data(struct emit_callback
*ecbdata
)
487 if (ecbdata
->diff_words
) {
489 if (ecbdata
->diff_words
->minus
.text
.size
||
490 ecbdata
->diff_words
->plus
.text
.size
)
491 diff_words_show(ecbdata
->diff_words
);
493 free (ecbdata
->diff_words
->minus
.text
.ptr
);
494 free (ecbdata
->diff_words
->plus
.text
.ptr
);
495 free(ecbdata
->diff_words
);
496 ecbdata
->diff_words
= NULL
;
500 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
503 return diff_colors
[ix
];
507 static void emit_line(FILE *file
, const char *set
, const char *reset
, const char *line
, int len
)
509 int has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
510 if (has_trailing_newline
)
514 fwrite(line
, len
, 1, file
);
516 if (has_trailing_newline
)
520 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
522 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
523 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
526 emit_line(ecbdata
->file
, set
, reset
, line
, len
);
528 /* Emit just the prefix, then the rest. */
529 emit_line(ecbdata
->file
, set
, reset
, line
, ecbdata
->nparents
);
530 ws_check_emit(line
+ ecbdata
->nparents
,
531 len
- ecbdata
->nparents
, ecbdata
->ws_rule
,
532 ecbdata
->file
, set
, reset
, ws
);
536 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
543 return ecb
->truncate(line
, len
);
547 (void) utf8_width(&cp
, &l
);
549 break; /* truncated in the middle? */
554 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
558 struct emit_callback
*ecbdata
= priv
;
559 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
560 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
561 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
563 *(ecbdata
->found_changesp
) = 1;
565 if (ecbdata
->label_path
[0]) {
566 const char *name_a_tab
, *name_b_tab
;
568 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
569 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
571 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
572 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
573 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
574 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
575 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
578 /* This is not really necessary for now because
579 * this codepath only deals with two-way diffs.
581 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
583 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
584 ecbdata
->nparents
= i
- 1;
585 len
= sane_truncate_line(ecbdata
, line
, len
);
586 emit_line(ecbdata
->file
,
587 diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
589 if (line
[len
-1] != '\n')
590 putc('\n', ecbdata
->file
);
594 if (len
< ecbdata
->nparents
) {
595 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
600 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
601 /* fall back to normal diff */
602 free_diff_words_data(ecbdata
);
603 if (ecbdata
->diff_words
) {
604 if (line
[0] == '-') {
605 diff_words_append(line
, len
,
606 &ecbdata
->diff_words
->minus
);
608 } else if (line
[0] == '+') {
609 diff_words_append(line
, len
,
610 &ecbdata
->diff_words
->plus
);
613 if (ecbdata
->diff_words
->minus
.text
.size
||
614 ecbdata
->diff_words
->plus
.text
.size
)
615 diff_words_show(ecbdata
->diff_words
);
618 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
621 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
623 color
= DIFF_FILE_OLD
;
624 else if (line
[i
] == '+')
625 color
= DIFF_FILE_NEW
;
628 if (color
!= DIFF_FILE_NEW
) {
629 emit_line(ecbdata
->file
,
630 diff_get_color(ecbdata
->color_diff
, color
),
634 emit_add_line(reset
, ecbdata
, line
, len
);
637 static char *pprint_rename(const char *a
, const char *b
)
642 int pfx_length
, sfx_length
;
643 int len_a
= strlen(a
);
644 int len_b
= strlen(b
);
645 int a_midlen
, b_midlen
;
646 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
647 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
649 strbuf_init(&name
, 0);
650 if (qlen_a
|| qlen_b
) {
651 quote_c_style(a
, &name
, NULL
, 0);
652 strbuf_addstr(&name
, " => ");
653 quote_c_style(b
, &name
, NULL
, 0);
654 return strbuf_detach(&name
, NULL
);
657 /* Find common prefix */
659 while (*old
&& *new && *old
== *new) {
661 pfx_length
= old
- a
+ 1;
666 /* Find common suffix */
670 while (a
<= old
&& b
<= new && *old
== *new) {
672 sfx_length
= len_a
- (old
- a
);
678 * pfx{mid-a => mid-b}sfx
679 * {pfx-a => pfx-b}sfx
680 * pfx{sfx-a => sfx-b}
683 a_midlen
= len_a
- pfx_length
- sfx_length
;
684 b_midlen
= len_b
- pfx_length
- sfx_length
;
690 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
691 if (pfx_length
+ sfx_length
) {
692 strbuf_add(&name
, a
, pfx_length
);
693 strbuf_addch(&name
, '{');
695 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
696 strbuf_addstr(&name
, " => ");
697 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
698 if (pfx_length
+ sfx_length
) {
699 strbuf_addch(&name
, '}');
700 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
702 return strbuf_detach(&name
, NULL
);
708 struct diffstat_file
{
712 unsigned is_unmerged
:1;
713 unsigned is_binary
:1;
714 unsigned is_renamed
:1;
715 unsigned int added
, deleted
;
719 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
723 struct diffstat_file
*x
;
724 x
= xcalloc(sizeof (*x
), 1);
725 if (diffstat
->nr
== diffstat
->alloc
) {
726 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
727 diffstat
->files
= xrealloc(diffstat
->files
,
728 diffstat
->alloc
* sizeof(x
));
730 diffstat
->files
[diffstat
->nr
++] = x
;
732 x
->from_name
= xstrdup(name_a
);
733 x
->name
= xstrdup(name_b
);
738 x
->name
= xstrdup(name_a
);
743 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
745 struct diffstat_t
*diffstat
= priv
;
746 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
750 else if (line
[0] == '-')
754 const char mime_boundary_leader
[] = "------------";
756 static int scale_linear(int it
, int width
, int max_change
)
759 * make sure that at least one '-' is printed if there were deletions,
760 * and likewise for '+'.
764 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
767 static void show_name(FILE *file
,
768 const char *prefix
, const char *name
, int len
,
769 const char *reset
, const char *set
)
771 fprintf(file
, " %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
774 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
778 fprintf(file
, "%s", set
);
781 fprintf(file
, "%s", reset
);
784 static void fill_print_name(struct diffstat_file
*file
)
788 if (file
->print_name
)
791 if (!file
->is_renamed
) {
793 strbuf_init(&buf
, 0);
794 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
795 pname
= strbuf_detach(&buf
, NULL
);
798 strbuf_release(&buf
);
801 pname
= pprint_rename(file
->from_name
, file
->name
);
803 file
->print_name
= pname
;
806 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
808 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
809 int max_change
= 0, max_len
= 0;
810 int total_files
= data
->nr
;
811 int width
, name_width
;
812 const char *reset
, *set
, *add_c
, *del_c
;
817 width
= options
->stat_width
? options
->stat_width
: 80;
818 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
820 /* Sanity: give at least 5 columns to the graph,
821 * but leave at least 10 columns for the name.
827 else if (width
< name_width
+ 15)
828 name_width
= width
- 15;
830 /* Find the longest filename and max number of changes */
831 reset
= diff_get_color_opt(options
, DIFF_RESET
);
832 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
833 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
834 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
836 for (i
= 0; i
< data
->nr
; i
++) {
837 struct diffstat_file
*file
= data
->files
[i
];
838 int change
= file
->added
+ file
->deleted
;
839 fill_print_name(file
);
840 len
= strlen(file
->print_name
);
844 if (file
->is_binary
|| file
->is_unmerged
)
846 if (max_change
< change
)
850 /* Compute the width of the graph part;
851 * 10 is for one blank at the beginning of the line plus
852 * " | count " between the name and the graph.
854 * From here on, name_width is the width of the name area,
855 * and width is the width of the graph area.
857 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
858 if (width
< (name_width
+ 10) + max_change
)
859 width
= width
- (name_width
+ 10);
863 for (i
= 0; i
< data
->nr
; i
++) {
864 const char *prefix
= "";
865 char *name
= data
->files
[i
]->print_name
;
866 int added
= data
->files
[i
]->added
;
867 int deleted
= data
->files
[i
]->deleted
;
871 * "scale" the filename
874 name_len
= strlen(name
);
875 if (name_width
< name_len
) {
879 name
+= name_len
- len
;
880 slash
= strchr(name
, '/');
885 if (data
->files
[i
]->is_binary
) {
886 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
887 fprintf(options
->file
, " Bin ");
888 fprintf(options
->file
, "%s%d%s", del_c
, deleted
, reset
);
889 fprintf(options
->file
, " -> ");
890 fprintf(options
->file
, "%s%d%s", add_c
, added
, reset
);
891 fprintf(options
->file
, " bytes");
892 fprintf(options
->file
, "\n");
895 else if (data
->files
[i
]->is_unmerged
) {
896 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
897 fprintf(options
->file
, " Unmerged\n");
900 else if (!data
->files
[i
]->is_renamed
&&
901 (added
+ deleted
== 0)) {
907 * scale the add/delete
915 if (width
<= max_change
) {
916 add
= scale_linear(add
, width
, max_change
);
917 del
= scale_linear(del
, width
, max_change
);
920 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
921 fprintf(options
->file
, "%5d%s", added
+ deleted
,
922 added
+ deleted
? " " : "");
923 show_graph(options
->file
, '+', add
, add_c
, reset
);
924 show_graph(options
->file
, '-', del
, del_c
, reset
);
925 fprintf(options
->file
, "\n");
927 fprintf(options
->file
,
928 "%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
929 set
, total_files
, adds
, dels
, reset
);
932 static void show_shortstats(struct diffstat_t
* data
, struct diff_options
*options
)
934 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
939 for (i
= 0; i
< data
->nr
; i
++) {
940 if (!data
->files
[i
]->is_binary
&&
941 !data
->files
[i
]->is_unmerged
) {
942 int added
= data
->files
[i
]->added
;
943 int deleted
= data
->files
[i
]->deleted
;
944 if (!data
->files
[i
]->is_renamed
&&
945 (added
+ deleted
== 0)) {
953 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
954 total_files
, adds
, dels
);
957 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
964 for (i
= 0; i
< data
->nr
; i
++) {
965 struct diffstat_file
*file
= data
->files
[i
];
968 fprintf(options
->file
, "-\t-\t");
970 fprintf(options
->file
,
971 "%d\t%d\t", file
->added
, file
->deleted
);
972 if (options
->line_termination
) {
973 fill_print_name(file
);
974 if (!file
->is_renamed
)
975 write_name_quoted(file
->name
, options
->file
,
976 options
->line_termination
);
978 fputs(file
->print_name
, options
->file
);
979 putc(options
->line_termination
, options
->file
);
982 if (file
->is_renamed
) {
983 putc('\0', options
->file
);
984 write_name_quoted(file
->from_name
, options
->file
, '\0');
986 write_name_quoted(file
->name
, options
->file
, '\0');
991 struct dirstat_file
{
993 unsigned long changed
;
997 struct dirstat_file
*files
;
998 int alloc
, nr
, percent
, cumulative
;
1001 static long gather_dirstat(FILE *file
, struct dirstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1003 unsigned long this_dir
= 0;
1004 unsigned int sources
= 0;
1007 struct dirstat_file
*f
= dir
->files
;
1008 int namelen
= strlen(f
->name
);
1012 if (namelen
< baselen
)
1014 if (memcmp(f
->name
, base
, baselen
))
1016 slash
= strchr(f
->name
+ baselen
, '/');
1018 int newbaselen
= slash
+ 1 - f
->name
;
1019 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1031 * We don't report dirstat's for
1033 * - or cases where everything came from a single directory
1034 * under this directory (sources == 1).
1036 if (baselen
&& sources
!= 1) {
1037 int permille
= this_dir
* 1000 / changed
;
1039 int percent
= permille
/ 10;
1040 if (percent
>= dir
->percent
) {
1041 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1042 if (!dir
->cumulative
)
1050 static void show_dirstat(struct diff_options
*options
)
1053 unsigned long changed
;
1054 struct dirstat_dir dir
;
1055 struct diff_queue_struct
*q
= &diff_queued_diff
;
1060 dir
.percent
= options
->dirstat_percent
;
1061 dir
.cumulative
= options
->output_format
& DIFF_FORMAT_CUMULATIVE
;
1064 for (i
= 0; i
< q
->nr
; i
++) {
1065 struct diff_filepair
*p
= q
->queue
[i
];
1067 unsigned long copied
, added
, damage
;
1069 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1071 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1072 diff_populate_filespec(p
->one
, 0);
1073 diff_populate_filespec(p
->two
, 0);
1074 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1076 diff_free_filespec_data(p
->one
);
1077 diff_free_filespec_data(p
->two
);
1078 } else if (DIFF_FILE_VALID(p
->one
)) {
1079 diff_populate_filespec(p
->one
, 1);
1081 diff_free_filespec_data(p
->one
);
1082 } else if (DIFF_FILE_VALID(p
->two
)) {
1083 diff_populate_filespec(p
->two
, 1);
1085 added
= p
->two
->size
;
1086 diff_free_filespec_data(p
->two
);
1091 * Original minus copied is the removed material,
1092 * added is the new material. They are both damages
1093 * made to the preimage.
1095 damage
= (p
->one
->size
- copied
) + added
;
1097 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1098 dir
.files
[dir
.nr
].name
= name
;
1099 dir
.files
[dir
.nr
].changed
= damage
;
1104 /* This can happen even with many files, if everything was renames */
1108 /* Show all directories with more than x% of the changes */
1109 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1112 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1115 for (i
= 0; i
< diffstat
->nr
; i
++) {
1116 struct diffstat_file
*f
= diffstat
->files
[i
];
1117 if (f
->name
!= f
->print_name
)
1118 free(f
->print_name
);
1123 free(diffstat
->files
);
1126 struct checkdiff_t
{
1127 const char *filename
;
1129 struct diff_options
*o
;
1132 int trailing_blanks_start
;
1135 static int is_conflict_marker(const char *line
, unsigned long len
)
1142 firstchar
= line
[0];
1143 switch (firstchar
) {
1144 case '=': case '>': case '<':
1149 for (cnt
= 1; cnt
< 7; cnt
++)
1150 if (line
[cnt
] != firstchar
)
1152 /* line[0] thru line[6] are same as firstchar */
1153 if (firstchar
== '=') {
1154 /* divider between ours and theirs? */
1155 if (len
!= 8 || line
[7] != '\n')
1157 } else if (len
< 8 || !isspace(line
[7])) {
1158 /* not divider before ours nor after theirs */
1164 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1166 struct checkdiff_t
*data
= priv
;
1167 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1168 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1169 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1170 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1173 if (line
[0] == '+') {
1176 if (!ws_blank_line(line
+ 1, len
- 1, data
->ws_rule
))
1177 data
->trailing_blanks_start
= 0;
1178 else if (!data
->trailing_blanks_start
)
1179 data
->trailing_blanks_start
= data
->lineno
;
1180 if (is_conflict_marker(line
+ 1, len
- 1)) {
1182 fprintf(data
->o
->file
,
1183 "%s:%d: leftover conflict marker\n",
1184 data
->filename
, data
->lineno
);
1186 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1189 data
->status
|= bad
;
1190 err
= whitespace_error_string(bad
);
1191 fprintf(data
->o
->file
, "%s:%d: %s.\n",
1192 data
->filename
, data
->lineno
, err
);
1194 emit_line(data
->o
->file
, set
, reset
, line
, 1);
1195 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1196 data
->o
->file
, set
, reset
, ws
);
1197 } else if (line
[0] == ' ') {
1199 data
->trailing_blanks_start
= 0;
1200 } else if (line
[0] == '@') {
1201 char *plus
= strchr(line
, '+');
1203 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1205 die("invalid diff");
1206 data
->trailing_blanks_start
= 0;
1210 static unsigned char *deflate_it(char *data
,
1212 unsigned long *result_size
)
1215 unsigned char *deflated
;
1218 memset(&stream
, 0, sizeof(stream
));
1219 deflateInit(&stream
, zlib_compression_level
);
1220 bound
= deflateBound(&stream
, size
);
1221 deflated
= xmalloc(bound
);
1222 stream
.next_out
= deflated
;
1223 stream
.avail_out
= bound
;
1225 stream
.next_in
= (unsigned char *)data
;
1226 stream
.avail_in
= size
;
1227 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1229 deflateEnd(&stream
);
1230 *result_size
= stream
.total_out
;
1234 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1240 unsigned long orig_size
;
1241 unsigned long delta_size
;
1242 unsigned long deflate_size
;
1243 unsigned long data_size
;
1245 /* We could do deflated delta, or we could do just deflated two,
1246 * whichever is smaller.
1249 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1250 if (one
->size
&& two
->size
) {
1251 delta
= diff_delta(one
->ptr
, one
->size
,
1252 two
->ptr
, two
->size
,
1253 &delta_size
, deflate_size
);
1255 void *to_free
= delta
;
1256 orig_size
= delta_size
;
1257 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1262 if (delta
&& delta_size
< deflate_size
) {
1263 fprintf(file
, "delta %lu\n", orig_size
);
1266 data_size
= delta_size
;
1269 fprintf(file
, "literal %lu\n", two
->size
);
1272 data_size
= deflate_size
;
1275 /* emit data encoded in base85 */
1278 int bytes
= (52 < data_size
) ? 52 : data_size
;
1282 line
[0] = bytes
+ 'A' - 1;
1284 line
[0] = bytes
- 26 + 'a' - 1;
1285 encode_85(line
+ 1, cp
, bytes
);
1286 cp
= (char *) cp
+ bytes
;
1290 fprintf(file
, "\n");
1294 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1296 fprintf(file
, "GIT binary patch\n");
1297 emit_binary_diff_body(file
, one
, two
);
1298 emit_binary_diff_body(file
, two
, one
);
1301 static void setup_diff_attr_check(struct git_attr_check
*check
)
1303 static struct git_attr
*attr_diff
;
1306 attr_diff
= git_attr("diff", 4);
1308 check
[0].attr
= attr_diff
;
1311 static void diff_filespec_check_attr(struct diff_filespec
*one
)
1313 struct git_attr_check attr_diff_check
;
1314 int check_from_data
= 0;
1316 if (one
->checked_attr
)
1319 setup_diff_attr_check(&attr_diff_check
);
1321 one
->funcname_pattern_ident
= NULL
;
1323 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1327 value
= attr_diff_check
.value
;
1328 if (ATTR_TRUE(value
))
1330 else if (ATTR_FALSE(value
))
1333 check_from_data
= 1;
1335 /* funcname pattern ident */
1336 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1339 one
->funcname_pattern_ident
= value
;
1342 if (check_from_data
) {
1343 if (!one
->data
&& DIFF_FILE_VALID(one
))
1344 diff_populate_filespec(one
, 0);
1347 one
->is_binary
= buffer_is_binary(one
->data
, one
->size
);
1351 int diff_filespec_is_binary(struct diff_filespec
*one
)
1353 diff_filespec_check_attr(one
);
1354 return one
->is_binary
;
1357 static const char *funcname_pattern(const char *ident
)
1359 struct funcname_pattern
*pp
;
1361 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
1362 if (!strcmp(ident
, pp
->name
))
1367 static struct builtin_funcname_pattern
{
1369 const char *pattern
;
1370 } builtin_funcname_pattern
[] = {
1371 { "java", "!^[ ]*\\(catch\\|do\\|for\\|if\\|instanceof\\|"
1372 "new\\|return\\|switch\\|throw\\|while\\)\n"
1374 "[A-Za-z_][A-Za-z_0-9]*\\)\\{2,\\}"
1376 { "pascal", "^\\(\\(procedure\\|function\\|constructor\\|"
1377 "destructor\\|interface\\|implementation\\|"
1378 "initialization\\|finalization\\)[ \t]*.*\\)$"
1380 "^\\(.*=[ \t]*\\(class\\|record\\).*\\)$"
1382 { "bibtex", "\\(@[a-zA-Z]\\{1,\\}[ \t]*{\\{0,1\\}[ \t]*[^ \t\"@',\\#}{~%]*\\).*$" },
1383 { "tex", "^\\(\\\\\\(\\(sub\\)*section\\|chapter\\|part\\)\\*\\{0,1\\}{.*\\)$" },
1384 { "ruby", "^\\s*\\(\\(class\\|module\\|def\\)\\s.*\\)$" },
1387 static const char *diff_funcname_pattern(struct diff_filespec
*one
)
1389 const char *ident
, *pattern
;
1392 diff_filespec_check_attr(one
);
1393 ident
= one
->funcname_pattern_ident
;
1397 * If the config file has "funcname.default" defined, that
1398 * regexp is used; otherwise NULL is returned and xemit uses
1399 * the built-in default.
1401 return funcname_pattern("default");
1403 /* Look up custom "funcname.$ident" regexp from config. */
1404 pattern
= funcname_pattern(ident
);
1409 * And define built-in fallback patterns here. Note that
1410 * these can be overridden by the user's config settings.
1412 for (i
= 0; i
< ARRAY_SIZE(builtin_funcname_pattern
); i
++)
1413 if (!strcmp(ident
, builtin_funcname_pattern
[i
].name
))
1414 return builtin_funcname_pattern
[i
].pattern
;
1419 static void builtin_diff(const char *name_a
,
1421 struct diff_filespec
*one
,
1422 struct diff_filespec
*two
,
1423 const char *xfrm_msg
,
1424 struct diff_options
*o
,
1425 int complete_rewrite
)
1429 char *a_one
, *b_two
;
1430 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1431 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1433 a_one
= quote_two(o
->a_prefix
, name_a
+ (*name_a
== '/'));
1434 b_two
= quote_two(o
->b_prefix
, name_b
+ (*name_b
== '/'));
1435 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1436 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1437 fprintf(o
->file
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1438 if (lbl
[0][0] == '/') {
1440 fprintf(o
->file
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1441 if (xfrm_msg
&& xfrm_msg
[0])
1442 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1444 else if (lbl
[1][0] == '/') {
1445 fprintf(o
->file
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1446 if (xfrm_msg
&& xfrm_msg
[0])
1447 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1450 if (one
->mode
!= two
->mode
) {
1451 fprintf(o
->file
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1452 fprintf(o
->file
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1454 if (xfrm_msg
&& xfrm_msg
[0])
1455 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1457 * we do not run diff between different kind
1460 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1461 goto free_ab_and_return
;
1462 if (complete_rewrite
) {
1463 emit_rewrite_diff(name_a
, name_b
, one
, two
, o
);
1464 o
->found_changes
= 1;
1465 goto free_ab_and_return
;
1469 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1470 die("unable to read files to diff");
1472 if (!DIFF_OPT_TST(o
, TEXT
) &&
1473 (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
))) {
1474 /* Quite common confusing case */
1475 if (mf1
.size
== mf2
.size
&&
1476 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1477 goto free_ab_and_return
;
1478 if (DIFF_OPT_TST(o
, BINARY
))
1479 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1481 fprintf(o
->file
, "Binary files %s and %s differ\n",
1483 o
->found_changes
= 1;
1486 /* Crazy xdl interfaces.. */
1487 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1491 struct emit_callback ecbdata
;
1492 const char *funcname_pattern
;
1494 funcname_pattern
= diff_funcname_pattern(one
);
1495 if (!funcname_pattern
)
1496 funcname_pattern
= diff_funcname_pattern(two
);
1498 memset(&xecfg
, 0, sizeof(xecfg
));
1499 memset(&ecbdata
, 0, sizeof(ecbdata
));
1500 ecbdata
.label_path
= lbl
;
1501 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1502 ecbdata
.found_changesp
= &o
->found_changes
;
1503 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1504 ecbdata
.file
= o
->file
;
1505 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1506 xecfg
.ctxlen
= o
->context
;
1507 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1508 if (funcname_pattern
)
1509 xdiff_set_find_func(&xecfg
, funcname_pattern
);
1512 else if (!prefixcmp(diffopts
, "--unified="))
1513 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1514 else if (!prefixcmp(diffopts
, "-u"))
1515 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1516 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
)) {
1517 ecbdata
.diff_words
=
1518 xcalloc(1, sizeof(struct diff_words_data
));
1519 ecbdata
.diff_words
->file
= o
->file
;
1521 xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
1522 &xpp
, &xecfg
, &ecb
);
1523 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
))
1524 free_diff_words_data(&ecbdata
);
1528 diff_free_filespec_data(one
);
1529 diff_free_filespec_data(two
);
1535 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1536 struct diff_filespec
*one
,
1537 struct diff_filespec
*two
,
1538 struct diffstat_t
*diffstat
,
1539 struct diff_options
*o
,
1540 int complete_rewrite
)
1543 struct diffstat_file
*data
;
1545 data
= diffstat_add(diffstat
, name_a
, name_b
);
1548 data
->is_unmerged
= 1;
1551 if (complete_rewrite
) {
1552 diff_populate_filespec(one
, 0);
1553 diff_populate_filespec(two
, 0);
1554 data
->deleted
= count_lines(one
->data
, one
->size
);
1555 data
->added
= count_lines(two
->data
, two
->size
);
1556 goto free_and_return
;
1558 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1559 die("unable to read files to diff");
1561 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1562 data
->is_binary
= 1;
1563 data
->added
= mf2
.size
;
1564 data
->deleted
= mf1
.size
;
1566 /* Crazy xdl interfaces.. */
1571 memset(&xecfg
, 0, sizeof(xecfg
));
1572 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1573 xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
1574 &xpp
, &xecfg
, &ecb
);
1578 diff_free_filespec_data(one
);
1579 diff_free_filespec_data(two
);
1582 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1583 const char *attr_path
,
1584 struct diff_filespec
*one
,
1585 struct diff_filespec
*two
,
1586 struct diff_options
*o
)
1589 struct checkdiff_t data
;
1594 memset(&data
, 0, sizeof(data
));
1595 data
.filename
= name_b
? name_b
: name_a
;
1598 data
.ws_rule
= whitespace_rule(attr_path
);
1600 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1601 die("unable to read files to diff");
1604 * All the other codepaths check both sides, but not checking
1605 * the "old" side here is deliberate. We are checking the newly
1606 * introduced changes, and as long as the "new" side is text, we
1607 * can and should check what it introduces.
1609 if (diff_filespec_is_binary(two
))
1610 goto free_and_return
;
1612 /* Crazy xdl interfaces.. */
1617 memset(&xecfg
, 0, sizeof(xecfg
));
1618 xpp
.flags
= XDF_NEED_MINIMAL
;
1619 xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
1620 &xpp
, &xecfg
, &ecb
);
1622 if ((data
.ws_rule
& WS_TRAILING_SPACE
) &&
1623 data
.trailing_blanks_start
) {
1624 fprintf(o
->file
, "%s:%d: ends with blank lines.\n",
1625 data
.filename
, data
.trailing_blanks_start
);
1626 data
.status
= 1; /* report errors */
1630 diff_free_filespec_data(one
);
1631 diff_free_filespec_data(two
);
1633 DIFF_OPT_SET(o
, CHECK_FAILED
);
1636 struct diff_filespec
*alloc_filespec(const char *path
)
1638 int namelen
= strlen(path
);
1639 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1641 memset(spec
, 0, sizeof(*spec
));
1642 spec
->path
= (char *)(spec
+ 1);
1643 memcpy(spec
->path
, path
, namelen
+1);
1648 void free_filespec(struct diff_filespec
*spec
)
1650 if (!--spec
->count
) {
1651 diff_free_filespec_data(spec
);
1656 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1657 unsigned short mode
)
1660 spec
->mode
= canon_mode(mode
);
1661 hashcpy(spec
->sha1
, sha1
);
1662 spec
->sha1_valid
= !is_null_sha1(sha1
);
1667 * Given a name and sha1 pair, if the index tells us the file in
1668 * the work tree has that object contents, return true, so that
1669 * prepare_temp_file() does not have to inflate and extract.
1671 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1673 struct cache_entry
*ce
;
1677 /* We do not read the cache ourselves here, because the
1678 * benchmark with my previous version that always reads cache
1679 * shows that it makes things worse for diff-tree comparing
1680 * two linux-2.6 kernel trees in an already checked out work
1681 * tree. This is because most diff-tree comparisons deal with
1682 * only a small number of files, while reading the cache is
1683 * expensive for a large project, and its cost outweighs the
1684 * savings we get by not inflating the object to a temporary
1685 * file. Practically, this code only helps when we are used
1686 * by diff-cache --cached, which does read the cache before
1692 /* We want to avoid the working directory if our caller
1693 * doesn't need the data in a normal file, this system
1694 * is rather slow with its stat/open/mmap/close syscalls,
1695 * and the object is contained in a pack file. The pack
1696 * is probably already open and will be faster to obtain
1697 * the data through than the working directory. Loose
1698 * objects however would tend to be slower as they need
1699 * to be individually opened and inflated.
1701 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1705 pos
= cache_name_pos(name
, len
);
1708 ce
= active_cache
[pos
];
1711 * This is not the sha1 we are looking for, or
1712 * unreusable because it is not a regular file.
1714 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
1718 * If ce matches the file in the work tree, we can reuse it.
1720 if (ce_uptodate(ce
) ||
1721 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
1727 static int populate_from_stdin(struct diff_filespec
*s
)
1732 strbuf_init(&buf
, 0);
1733 if (strbuf_read(&buf
, 0, 0) < 0)
1734 return error("error while reading from stdin %s",
1737 s
->should_munmap
= 0;
1738 s
->data
= strbuf_detach(&buf
, &size
);
1744 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1747 char *data
= xmalloc(100);
1748 len
= snprintf(data
, 100,
1749 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1761 * While doing rename detection and pickaxe operation, we may need to
1762 * grab the data for the blob (or file) for our own in-core comparison.
1763 * diff_filespec has data and size fields for this purpose.
1765 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1768 if (!DIFF_FILE_VALID(s
))
1769 die("internal error: asking to populate invalid file.");
1770 if (S_ISDIR(s
->mode
))
1776 if (size_only
&& 0 < s
->size
)
1779 if (S_ISGITLINK(s
->mode
))
1780 return diff_populate_gitlink(s
, size_only
);
1782 if (!s
->sha1_valid
||
1783 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1788 if (!strcmp(s
->path
, "-"))
1789 return populate_from_stdin(s
);
1791 if (lstat(s
->path
, &st
) < 0) {
1792 if (errno
== ENOENT
) {
1796 s
->data
= (char *)"";
1801 s
->size
= xsize_t(st
.st_size
);
1806 if (S_ISLNK(st
.st_mode
)) {
1808 s
->data
= xmalloc(s
->size
);
1810 ret
= readlink(s
->path
, s
->data
, s
->size
);
1817 fd
= open(s
->path
, O_RDONLY
);
1820 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1822 s
->should_munmap
= 1;
1825 * Convert from working tree format to canonical git format
1827 strbuf_init(&buf
, 0);
1828 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
1830 munmap(s
->data
, s
->size
);
1831 s
->should_munmap
= 0;
1832 s
->data
= strbuf_detach(&buf
, &size
);
1838 enum object_type type
;
1840 type
= sha1_object_info(s
->sha1
, &s
->size
);
1842 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1849 void diff_free_filespec_blob(struct diff_filespec
*s
)
1853 else if (s
->should_munmap
)
1854 munmap(s
->data
, s
->size
);
1856 if (s
->should_free
|| s
->should_munmap
) {
1857 s
->should_free
= s
->should_munmap
= 0;
1862 void diff_free_filespec_data(struct diff_filespec
*s
)
1864 diff_free_filespec_blob(s
);
1869 static void prep_temp_blob(struct diff_tempfile
*temp
,
1872 const unsigned char *sha1
,
1877 fd
= git_mkstemp(temp
->tmp_path
, PATH_MAX
, ".diff_XXXXXX");
1879 die("unable to create temp-file: %s", strerror(errno
));
1880 if (write_in_full(fd
, blob
, size
) != size
)
1881 die("unable to write temp-file");
1883 temp
->name
= temp
->tmp_path
;
1884 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1886 sprintf(temp
->mode
, "%06o", mode
);
1889 static void prepare_temp_file(const char *name
,
1890 struct diff_tempfile
*temp
,
1891 struct diff_filespec
*one
)
1893 if (!DIFF_FILE_VALID(one
)) {
1895 /* A '-' entry produces this for file-2, and
1896 * a '+' entry produces this for file-1.
1898 temp
->name
= "/dev/null";
1899 strcpy(temp
->hex
, ".");
1900 strcpy(temp
->mode
, ".");
1904 if (!one
->sha1_valid
||
1905 reuse_worktree_file(name
, one
->sha1
, 1)) {
1907 if (lstat(name
, &st
) < 0) {
1908 if (errno
== ENOENT
)
1909 goto not_a_valid_file
;
1910 die("stat(%s): %s", name
, strerror(errno
));
1912 if (S_ISLNK(st
.st_mode
)) {
1914 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1915 size_t sz
= xsize_t(st
.st_size
);
1916 if (sizeof(buf
) <= st
.st_size
)
1917 die("symlink too long: %s", name
);
1918 ret
= readlink(name
, buf
, sz
);
1920 die("readlink(%s)", name
);
1921 prep_temp_blob(temp
, buf
, sz
,
1923 one
->sha1
: null_sha1
),
1925 one
->mode
: S_IFLNK
));
1928 /* we can borrow from the file in the work tree */
1930 if (!one
->sha1_valid
)
1931 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1933 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1934 /* Even though we may sometimes borrow the
1935 * contents from the work tree, we always want
1936 * one->mode. mode is trustworthy even when
1937 * !(one->sha1_valid), as long as
1938 * DIFF_FILE_VALID(one).
1940 sprintf(temp
->mode
, "%06o", one
->mode
);
1945 if (diff_populate_filespec(one
, 0))
1946 die("cannot read data blob for %s", one
->path
);
1947 prep_temp_blob(temp
, one
->data
, one
->size
,
1948 one
->sha1
, one
->mode
);
1952 static void remove_tempfile(void)
1956 for (i
= 0; i
< 2; i
++)
1957 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1958 unlink(diff_temp
[i
].name
);
1959 diff_temp
[i
].name
= NULL
;
1963 static void remove_tempfile_on_signal(int signo
)
1966 signal(SIGINT
, SIG_DFL
);
1970 /* An external diff command takes:
1972 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1973 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1976 static void run_external_diff(const char *pgm
,
1979 struct diff_filespec
*one
,
1980 struct diff_filespec
*two
,
1981 const char *xfrm_msg
,
1982 int complete_rewrite
)
1984 const char *spawn_arg
[10];
1985 struct diff_tempfile
*temp
= diff_temp
;
1987 static int atexit_asked
= 0;
1988 const char *othername
;
1989 const char **arg
= &spawn_arg
[0];
1991 othername
= (other
? other
: name
);
1993 prepare_temp_file(name
, &temp
[0], one
);
1994 prepare_temp_file(othername
, &temp
[1], two
);
1995 if (! atexit_asked
&&
1996 (temp
[0].name
== temp
[0].tmp_path
||
1997 temp
[1].name
== temp
[1].tmp_path
)) {
1999 atexit(remove_tempfile
);
2001 signal(SIGINT
, remove_tempfile_on_signal
);
2007 *arg
++ = temp
[0].name
;
2008 *arg
++ = temp
[0].hex
;
2009 *arg
++ = temp
[0].mode
;
2010 *arg
++ = temp
[1].name
;
2011 *arg
++ = temp
[1].hex
;
2012 *arg
++ = temp
[1].mode
;
2023 retval
= run_command_v_opt(spawn_arg
, 0);
2026 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2031 static const char *external_diff_attr(const char *name
)
2033 struct git_attr_check attr_diff_check
;
2038 setup_diff_attr_check(&attr_diff_check
);
2039 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
2040 const char *value
= attr_diff_check
.value
;
2041 if (!ATTR_TRUE(value
) &&
2042 !ATTR_FALSE(value
) &&
2043 !ATTR_UNSET(value
)) {
2044 struct ll_diff_driver
*drv
;
2046 for (drv
= user_diff
; drv
; drv
= drv
->next
)
2047 if (!strcmp(drv
->name
, value
))
2054 static void run_diff_cmd(const char *pgm
,
2057 const char *attr_path
,
2058 struct diff_filespec
*one
,
2059 struct diff_filespec
*two
,
2060 const char *xfrm_msg
,
2061 struct diff_options
*o
,
2062 int complete_rewrite
)
2064 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2067 const char *cmd
= external_diff_attr(attr_path
);
2073 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2078 builtin_diff(name
, other
? other
: name
,
2079 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2081 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2084 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2086 if (DIFF_FILE_VALID(one
)) {
2087 if (!one
->sha1_valid
) {
2089 if (!strcmp(one
->path
, "-")) {
2090 hashcpy(one
->sha1
, null_sha1
);
2093 if (lstat(one
->path
, &st
) < 0)
2094 die("stat %s", one
->path
);
2095 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2096 die("cannot hash %s\n", one
->path
);
2103 static int similarity_index(struct diff_filepair
*p
)
2105 return p
->score
* 100 / MAX_SCORE
;
2108 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2110 /* Strip the prefix but do not molest /dev/null and absolute paths */
2111 if (*namep
&& **namep
!= '/')
2112 *namep
+= prefix_length
;
2113 if (*otherp
&& **otherp
!= '/')
2114 *otherp
+= prefix_length
;
2117 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2119 const char *pgm
= external_diff();
2122 struct diff_filespec
*one
= p
->one
;
2123 struct diff_filespec
*two
= p
->two
;
2126 const char *attr_path
;
2127 int complete_rewrite
= 0;
2129 name
= p
->one
->path
;
2130 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2132 if (o
->prefix_length
)
2133 strip_prefix(o
->prefix_length
, &name
, &other
);
2135 if (DIFF_PAIR_UNMERGED(p
)) {
2136 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2137 NULL
, NULL
, NULL
, o
, 0);
2141 diff_fill_sha1_info(one
);
2142 diff_fill_sha1_info(two
);
2144 strbuf_init(&msg
, PATH_MAX
* 2 + 300);
2145 switch (p
->status
) {
2146 case DIFF_STATUS_COPIED
:
2147 strbuf_addf(&msg
, "similarity index %d%%", similarity_index(p
));
2148 strbuf_addstr(&msg
, "\ncopy from ");
2149 quote_c_style(name
, &msg
, NULL
, 0);
2150 strbuf_addstr(&msg
, "\ncopy to ");
2151 quote_c_style(other
, &msg
, NULL
, 0);
2152 strbuf_addch(&msg
, '\n');
2154 case DIFF_STATUS_RENAMED
:
2155 strbuf_addf(&msg
, "similarity index %d%%", similarity_index(p
));
2156 strbuf_addstr(&msg
, "\nrename from ");
2157 quote_c_style(name
, &msg
, NULL
, 0);
2158 strbuf_addstr(&msg
, "\nrename to ");
2159 quote_c_style(other
, &msg
, NULL
, 0);
2160 strbuf_addch(&msg
, '\n');
2162 case DIFF_STATUS_MODIFIED
:
2164 strbuf_addf(&msg
, "dissimilarity index %d%%\n",
2165 similarity_index(p
));
2166 complete_rewrite
= 1;
2175 if (hashcmp(one
->sha1
, two
->sha1
)) {
2176 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2178 if (DIFF_OPT_TST(o
, BINARY
)) {
2180 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2181 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2184 strbuf_addf(&msg
, "index %.*s..%.*s",
2185 abbrev
, sha1_to_hex(one
->sha1
),
2186 abbrev
, sha1_to_hex(two
->sha1
));
2187 if (one
->mode
== two
->mode
)
2188 strbuf_addf(&msg
, " %06o", one
->mode
);
2189 strbuf_addch(&msg
, '\n');
2193 strbuf_setlen(&msg
, msg
.len
- 1);
2194 xfrm_msg
= msg
.len
? msg
.buf
: NULL
;
2197 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2198 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2199 /* a filepair that changes between file and symlink
2200 * needs to be split into deletion and creation.
2202 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2203 run_diff_cmd(NULL
, name
, other
, attr_path
,
2204 one
, null
, xfrm_msg
, o
, 0);
2206 null
= alloc_filespec(one
->path
);
2207 run_diff_cmd(NULL
, name
, other
, attr_path
,
2208 null
, two
, xfrm_msg
, o
, 0);
2212 run_diff_cmd(pgm
, name
, other
, attr_path
,
2213 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2215 strbuf_release(&msg
);
2218 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2219 struct diffstat_t
*diffstat
)
2223 int complete_rewrite
= 0;
2225 if (DIFF_PAIR_UNMERGED(p
)) {
2227 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2231 name
= p
->one
->path
;
2232 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2234 if (o
->prefix_length
)
2235 strip_prefix(o
->prefix_length
, &name
, &other
);
2237 diff_fill_sha1_info(p
->one
);
2238 diff_fill_sha1_info(p
->two
);
2240 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2241 complete_rewrite
= 1;
2242 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2245 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2249 const char *attr_path
;
2251 if (DIFF_PAIR_UNMERGED(p
)) {
2256 name
= p
->one
->path
;
2257 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2258 attr_path
= other
? other
: name
;
2260 if (o
->prefix_length
)
2261 strip_prefix(o
->prefix_length
, &name
, &other
);
2263 diff_fill_sha1_info(p
->one
);
2264 diff_fill_sha1_info(p
->two
);
2266 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2269 void diff_setup(struct diff_options
*options
)
2271 memset(options
, 0, sizeof(*options
));
2273 options
->file
= stdout
;
2275 options
->line_termination
= '\n';
2276 options
->break_opt
= -1;
2277 options
->rename_limit
= -1;
2278 options
->dirstat_percent
= 3;
2279 options
->context
= 3;
2281 options
->change
= diff_change
;
2282 options
->add_remove
= diff_addremove
;
2283 if (diff_use_color_default
> 0)
2284 DIFF_OPT_SET(options
, COLOR_DIFF
);
2286 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2287 options
->detect_rename
= diff_detect_rename_default
;
2289 options
->a_prefix
= "a/";
2290 options
->b_prefix
= "b/";
2293 int diff_setup_done(struct diff_options
*options
)
2297 if (options
->output_format
& DIFF_FORMAT_NAME
)
2299 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2301 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2303 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2306 die("--name-only, --name-status, --check and -s are mutually exclusive");
2308 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2309 options
->detect_rename
= DIFF_DETECT_COPY
;
2311 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2312 options
->prefix
= NULL
;
2313 if (options
->prefix
)
2314 options
->prefix_length
= strlen(options
->prefix
);
2316 options
->prefix_length
= 0;
2318 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2319 DIFF_FORMAT_NAME_STATUS
|
2320 DIFF_FORMAT_CHECKDIFF
|
2321 DIFF_FORMAT_NO_OUTPUT
))
2322 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2323 DIFF_FORMAT_NUMSTAT
|
2324 DIFF_FORMAT_DIFFSTAT
|
2325 DIFF_FORMAT_SHORTSTAT
|
2326 DIFF_FORMAT_DIRSTAT
|
2327 DIFF_FORMAT_SUMMARY
|
2331 * These cases always need recursive; we do not drop caller-supplied
2332 * recursive bits for other formats here.
2334 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2335 DIFF_FORMAT_NUMSTAT
|
2336 DIFF_FORMAT_DIFFSTAT
|
2337 DIFF_FORMAT_SHORTSTAT
|
2338 DIFF_FORMAT_DIRSTAT
|
2339 DIFF_FORMAT_SUMMARY
|
2340 DIFF_FORMAT_CHECKDIFF
))
2341 DIFF_OPT_SET(options
, RECURSIVE
);
2343 * Also pickaxe would not work very well if you do not say recursive
2345 if (options
->pickaxe
)
2346 DIFF_OPT_SET(options
, RECURSIVE
);
2348 if (options
->detect_rename
&& options
->rename_limit
< 0)
2349 options
->rename_limit
= diff_rename_limit_default
;
2350 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2352 /* read-cache does not die even when it fails
2353 * so it is safe for us to do this here. Also
2354 * it does not smudge active_cache or active_nr
2355 * when it fails, so we do not have to worry about
2356 * cleaning it up ourselves either.
2360 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2361 options
->abbrev
= 40; /* full */
2364 * It does not make sense to show the first hit we happened
2365 * to have found. It does not make sense not to return with
2366 * exit code in such a case either.
2368 if (DIFF_OPT_TST(options
, QUIET
)) {
2369 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2370 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2374 * If we postprocess in diffcore, we cannot simply return
2375 * upon the first hit. We need to run diff as usual.
2377 if (options
->pickaxe
|| options
->filter
)
2378 DIFF_OPT_CLR(options
, QUIET
);
2383 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2393 if (c
== arg_short
) {
2397 if (val
&& isdigit(c
)) {
2399 int n
= strtoul(arg
, &end
, 10);
2410 eq
= strchr(arg
, '=');
2415 if (!len
|| strncmp(arg
, arg_long
, len
))
2420 if (!isdigit(*++eq
))
2422 n
= strtoul(eq
, &end
, 10);
2430 static int diff_scoreopt_parse(const char *opt
);
2432 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2434 const char *arg
= av
[0];
2436 /* Output format options */
2437 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2438 options
->output_format
|= DIFF_FORMAT_PATCH
;
2439 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2440 options
->output_format
|= DIFF_FORMAT_PATCH
;
2441 else if (!strcmp(arg
, "--raw"))
2442 options
->output_format
|= DIFF_FORMAT_RAW
;
2443 else if (!strcmp(arg
, "--patch-with-raw"))
2444 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2445 else if (!strcmp(arg
, "--numstat"))
2446 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2447 else if (!strcmp(arg
, "--shortstat"))
2448 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2449 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2450 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2451 else if (!strcmp(arg
, "--cumulative"))
2452 options
->output_format
|= DIFF_FORMAT_CUMULATIVE
;
2453 else if (!strcmp(arg
, "--check"))
2454 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2455 else if (!strcmp(arg
, "--summary"))
2456 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2457 else if (!strcmp(arg
, "--patch-with-stat"))
2458 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2459 else if (!strcmp(arg
, "--name-only"))
2460 options
->output_format
|= DIFF_FORMAT_NAME
;
2461 else if (!strcmp(arg
, "--name-status"))
2462 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2463 else if (!strcmp(arg
, "-s"))
2464 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2465 else if (!prefixcmp(arg
, "--stat")) {
2467 int width
= options
->stat_width
;
2468 int name_width
= options
->stat_name_width
;
2474 if (!prefixcmp(arg
, "-width="))
2475 width
= strtoul(arg
+ 7, &end
, 10);
2476 else if (!prefixcmp(arg
, "-name-width="))
2477 name_width
= strtoul(arg
+ 12, &end
, 10);
2480 width
= strtoul(arg
+1, &end
, 10);
2482 name_width
= strtoul(end
+1, &end
, 10);
2485 /* Important! This checks all the error cases! */
2488 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2489 options
->stat_name_width
= name_width
;
2490 options
->stat_width
= width
;
2493 /* renames options */
2494 else if (!prefixcmp(arg
, "-B")) {
2495 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2498 else if (!prefixcmp(arg
, "-M")) {
2499 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2501 options
->detect_rename
= DIFF_DETECT_RENAME
;
2503 else if (!prefixcmp(arg
, "-C")) {
2504 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2505 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2506 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2508 options
->detect_rename
= DIFF_DETECT_COPY
;
2510 else if (!strcmp(arg
, "--no-renames"))
2511 options
->detect_rename
= 0;
2512 else if (!strcmp(arg
, "--relative"))
2513 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2514 else if (!prefixcmp(arg
, "--relative=")) {
2515 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2516 options
->prefix
= arg
+ 11;
2520 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2521 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2522 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2523 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2524 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2525 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2528 else if (!strcmp(arg
, "--binary")) {
2529 options
->output_format
|= DIFF_FORMAT_PATCH
;
2530 DIFF_OPT_SET(options
, BINARY
);
2532 else if (!strcmp(arg
, "--full-index"))
2533 DIFF_OPT_SET(options
, FULL_INDEX
);
2534 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2535 DIFF_OPT_SET(options
, TEXT
);
2536 else if (!strcmp(arg
, "-R"))
2537 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2538 else if (!strcmp(arg
, "--find-copies-harder"))
2539 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2540 else if (!strcmp(arg
, "--follow"))
2541 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2542 else if (!strcmp(arg
, "--color"))
2543 DIFF_OPT_SET(options
, COLOR_DIFF
);
2544 else if (!strcmp(arg
, "--no-color"))
2545 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2546 else if (!strcmp(arg
, "--color-words"))
2547 options
->flags
|= DIFF_OPT_COLOR_DIFF
| DIFF_OPT_COLOR_DIFF_WORDS
;
2548 else if (!strcmp(arg
, "--exit-code"))
2549 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2550 else if (!strcmp(arg
, "--quiet"))
2551 DIFF_OPT_SET(options
, QUIET
);
2552 else if (!strcmp(arg
, "--ext-diff"))
2553 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2554 else if (!strcmp(arg
, "--no-ext-diff"))
2555 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2556 else if (!strcmp(arg
, "--ignore-submodules"))
2557 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
2560 else if (!strcmp(arg
, "-z"))
2561 options
->line_termination
= 0;
2562 else if (!prefixcmp(arg
, "-l"))
2563 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2564 else if (!prefixcmp(arg
, "-S"))
2565 options
->pickaxe
= arg
+ 2;
2566 else if (!strcmp(arg
, "--pickaxe-all"))
2567 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2568 else if (!strcmp(arg
, "--pickaxe-regex"))
2569 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2570 else if (!prefixcmp(arg
, "-O"))
2571 options
->orderfile
= arg
+ 2;
2572 else if (!prefixcmp(arg
, "--diff-filter="))
2573 options
->filter
= arg
+ 14;
2574 else if (!strcmp(arg
, "--abbrev"))
2575 options
->abbrev
= DEFAULT_ABBREV
;
2576 else if (!prefixcmp(arg
, "--abbrev=")) {
2577 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2578 if (options
->abbrev
< MINIMUM_ABBREV
)
2579 options
->abbrev
= MINIMUM_ABBREV
;
2580 else if (40 < options
->abbrev
)
2581 options
->abbrev
= 40;
2583 else if (!prefixcmp(arg
, "--src-prefix="))
2584 options
->a_prefix
= arg
+ 13;
2585 else if (!prefixcmp(arg
, "--dst-prefix="))
2586 options
->b_prefix
= arg
+ 13;
2587 else if (!strcmp(arg
, "--no-prefix"))
2588 options
->a_prefix
= options
->b_prefix
= "";
2589 else if (!prefixcmp(arg
, "--output=")) {
2590 options
->file
= fopen(arg
+ strlen("--output="), "w");
2591 options
->close_file
= 1;
2597 static int parse_num(const char **cp_p
)
2599 unsigned long num
, scale
;
2601 const char *cp
= *cp_p
;
2608 if ( !dot
&& ch
== '.' ) {
2611 } else if ( ch
== '%' ) {
2612 scale
= dot
? scale
*100 : 100;
2613 cp
++; /* % is always at the end */
2615 } else if ( ch
>= '0' && ch
<= '9' ) {
2616 if ( scale
< 100000 ) {
2618 num
= (num
*10) + (ch
-'0');
2627 /* user says num divided by scale and we say internally that
2628 * is MAX_SCORE * num / scale.
2630 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2633 static int diff_scoreopt_parse(const char *opt
)
2635 int opt1
, opt2
, cmd
;
2640 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2641 return -1; /* that is not a -M, -C nor -B option */
2643 opt1
= parse_num(&opt
);
2649 else if (*opt
!= '/')
2650 return -1; /* we expect -B80/99 or -B80 */
2653 opt2
= parse_num(&opt
);
2658 return opt1
| (opt2
<< 16);
2661 struct diff_queue_struct diff_queued_diff
;
2663 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2665 if (queue
->alloc
<= queue
->nr
) {
2666 queue
->alloc
= alloc_nr(queue
->alloc
);
2667 queue
->queue
= xrealloc(queue
->queue
,
2668 sizeof(dp
) * queue
->alloc
);
2670 queue
->queue
[queue
->nr
++] = dp
;
2673 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2674 struct diff_filespec
*one
,
2675 struct diff_filespec
*two
)
2677 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2685 void diff_free_filepair(struct diff_filepair
*p
)
2687 free_filespec(p
->one
);
2688 free_filespec(p
->two
);
2692 /* This is different from find_unique_abbrev() in that
2693 * it stuffs the result with dots for alignment.
2695 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2700 return sha1_to_hex(sha1
);
2702 abbrev
= find_unique_abbrev(sha1
, len
);
2703 abblen
= strlen(abbrev
);
2705 static char hex
[41];
2706 if (len
< abblen
&& abblen
<= len
+ 2)
2707 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2709 sprintf(hex
, "%s...", abbrev
);
2712 return sha1_to_hex(sha1
);
2715 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
2717 int line_termination
= opt
->line_termination
;
2718 int inter_name_termination
= line_termination
? '\t' : '\0';
2720 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2721 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
2722 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
2723 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
2726 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
2727 inter_name_termination
);
2729 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
2732 if (p
->status
== DIFF_STATUS_COPIED
||
2733 p
->status
== DIFF_STATUS_RENAMED
) {
2734 const char *name_a
, *name_b
;
2735 name_a
= p
->one
->path
;
2736 name_b
= p
->two
->path
;
2737 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2738 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
2739 write_name_quoted(name_b
, opt
->file
, line_termination
);
2741 const char *name_a
, *name_b
;
2742 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
2744 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2745 write_name_quoted(name_a
, opt
->file
, line_termination
);
2749 int diff_unmodified_pair(struct diff_filepair
*p
)
2751 /* This function is written stricter than necessary to support
2752 * the currently implemented transformers, but the idea is to
2753 * let transformers to produce diff_filepairs any way they want,
2754 * and filter and clean them up here before producing the output.
2756 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
2758 if (DIFF_PAIR_UNMERGED(p
))
2759 return 0; /* unmerged is interesting */
2761 /* deletion, addition, mode or type change
2762 * and rename are all interesting.
2764 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2765 DIFF_PAIR_MODE_CHANGED(p
) ||
2766 strcmp(one
->path
, two
->path
))
2769 /* both are valid and point at the same path. that is, we are
2770 * dealing with a change.
2772 if (one
->sha1_valid
&& two
->sha1_valid
&&
2773 !hashcmp(one
->sha1
, two
->sha1
))
2774 return 1; /* no change */
2775 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2776 return 1; /* both look at the same file on the filesystem. */
2780 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2782 if (diff_unmodified_pair(p
))
2785 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2786 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2787 return; /* no tree diffs in patch format */
2792 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2793 struct diffstat_t
*diffstat
)
2795 if (diff_unmodified_pair(p
))
2798 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2799 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2800 return; /* no tree diffs in patch format */
2802 run_diffstat(p
, o
, diffstat
);
2805 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2806 struct diff_options
*o
)
2808 if (diff_unmodified_pair(p
))
2811 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2812 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2813 return; /* no tree diffs in patch format */
2815 run_checkdiff(p
, o
);
2818 int diff_queue_is_empty(void)
2820 struct diff_queue_struct
*q
= &diff_queued_diff
;
2822 for (i
= 0; i
< q
->nr
; i
++)
2823 if (!diff_unmodified_pair(q
->queue
[i
]))
2829 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2831 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2834 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2836 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2837 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2839 s
->size
, s
->xfrm_flags
);
2842 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2844 diff_debug_filespec(p
->one
, i
, "one");
2845 diff_debug_filespec(p
->two
, i
, "two");
2846 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
2847 p
->score
, p
->status
? p
->status
: '?',
2848 p
->one
->rename_used
, p
->broken_pair
);
2851 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2855 fprintf(stderr
, "%s\n", msg
);
2856 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2857 for (i
= 0; i
< q
->nr
; i
++) {
2858 struct diff_filepair
*p
= q
->queue
[i
];
2859 diff_debug_filepair(p
, i
);
2864 static void diff_resolve_rename_copy(void)
2867 struct diff_filepair
*p
;
2868 struct diff_queue_struct
*q
= &diff_queued_diff
;
2870 diff_debug_queue("resolve-rename-copy", q
);
2872 for (i
= 0; i
< q
->nr
; i
++) {
2874 p
->status
= 0; /* undecided */
2875 if (DIFF_PAIR_UNMERGED(p
))
2876 p
->status
= DIFF_STATUS_UNMERGED
;
2877 else if (!DIFF_FILE_VALID(p
->one
))
2878 p
->status
= DIFF_STATUS_ADDED
;
2879 else if (!DIFF_FILE_VALID(p
->two
))
2880 p
->status
= DIFF_STATUS_DELETED
;
2881 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2882 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2884 /* from this point on, we are dealing with a pair
2885 * whose both sides are valid and of the same type, i.e.
2886 * either in-place edit or rename/copy edit.
2888 else if (DIFF_PAIR_RENAME(p
)) {
2890 * A rename might have re-connected a broken
2891 * pair up, causing the pathnames to be the
2892 * same again. If so, that's not a rename at
2893 * all, just a modification..
2895 * Otherwise, see if this source was used for
2896 * multiple renames, in which case we decrement
2897 * the count, and call it a copy.
2899 if (!strcmp(p
->one
->path
, p
->two
->path
))
2900 p
->status
= DIFF_STATUS_MODIFIED
;
2901 else if (--p
->one
->rename_used
> 0)
2902 p
->status
= DIFF_STATUS_COPIED
;
2904 p
->status
= DIFF_STATUS_RENAMED
;
2906 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2907 p
->one
->mode
!= p
->two
->mode
||
2908 is_null_sha1(p
->one
->sha1
))
2909 p
->status
= DIFF_STATUS_MODIFIED
;
2911 /* This is a "no-change" entry and should not
2912 * happen anymore, but prepare for broken callers.
2914 error("feeding unmodified %s to diffcore",
2916 p
->status
= DIFF_STATUS_UNKNOWN
;
2919 diff_debug_queue("resolve-rename-copy done", q
);
2922 static int check_pair_status(struct diff_filepair
*p
)
2924 switch (p
->status
) {
2925 case DIFF_STATUS_UNKNOWN
:
2928 die("internal error in diff-resolve-rename-copy");
2934 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2936 int fmt
= opt
->output_format
;
2938 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2939 diff_flush_checkdiff(p
, opt
);
2940 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2941 diff_flush_raw(p
, opt
);
2942 else if (fmt
& DIFF_FORMAT_NAME
) {
2943 const char *name_a
, *name_b
;
2944 name_a
= p
->two
->path
;
2946 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2947 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
2951 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
2954 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
2956 fprintf(file
, " %s ", newdelete
);
2957 write_name_quoted(fs
->path
, file
, '\n');
2961 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
2963 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2964 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
2965 show_name
? ' ' : '\n');
2967 write_name_quoted(p
->two
->path
, file
, '\n');
2972 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
2974 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
2976 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
2978 show_mode_change(file
, p
, 0);
2981 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
2984 case DIFF_STATUS_DELETED
:
2985 show_file_mode_name(file
, "delete", p
->one
);
2987 case DIFF_STATUS_ADDED
:
2988 show_file_mode_name(file
, "create", p
->two
);
2990 case DIFF_STATUS_COPIED
:
2991 show_rename_copy(file
, "copy", p
);
2993 case DIFF_STATUS_RENAMED
:
2994 show_rename_copy(file
, "rename", p
);
2998 fputs(" rewrite ", file
);
2999 write_name_quoted(p
->two
->path
, file
, ' ');
3000 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3002 show_mode_change(file
, p
, !p
->score
);
3012 static int remove_space(char *line
, int len
)
3018 for (i
= 0; i
< len
; i
++)
3019 if (!isspace((c
= line
[i
])))
3025 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3027 struct patch_id_t
*data
= priv
;
3030 /* Ignore line numbers when computing the SHA1 of the patch */
3031 if (!prefixcmp(line
, "@@ -"))
3034 new_len
= remove_space(line
, len
);
3036 SHA1_Update(data
->ctx
, line
, new_len
);
3037 data
->patchlen
+= new_len
;
3040 /* returns 0 upon success, and writes result into sha1 */
3041 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3043 struct diff_queue_struct
*q
= &diff_queued_diff
;
3046 struct patch_id_t data
;
3047 char buffer
[PATH_MAX
* 4 + 20];
3050 memset(&data
, 0, sizeof(struct patch_id_t
));
3053 for (i
= 0; i
< q
->nr
; i
++) {
3058 struct diff_filepair
*p
= q
->queue
[i
];
3061 memset(&xecfg
, 0, sizeof(xecfg
));
3063 return error("internal diff status error");
3064 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3066 if (diff_unmodified_pair(p
))
3068 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3069 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3071 if (DIFF_PAIR_UNMERGED(p
))
3074 diff_fill_sha1_info(p
->one
);
3075 diff_fill_sha1_info(p
->two
);
3076 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3077 fill_mmfile(&mf2
, p
->two
) < 0)
3078 return error("unable to read files to diff");
3080 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3081 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3082 if (p
->one
->mode
== 0)
3083 len1
= snprintf(buffer
, sizeof(buffer
),
3084 "diff--gita/%.*sb/%.*s"
3091 len2
, p
->two
->path
);
3092 else if (p
->two
->mode
== 0)
3093 len1
= snprintf(buffer
, sizeof(buffer
),
3094 "diff--gita/%.*sb/%.*s"
3095 "deletedfilemode%06o"
3101 len1
, p
->one
->path
);
3103 len1
= snprintf(buffer
, sizeof(buffer
),
3104 "diff--gita/%.*sb/%.*s"
3110 len2
, p
->two
->path
);
3111 SHA1_Update(&ctx
, buffer
, len1
);
3113 xpp
.flags
= XDF_NEED_MINIMAL
;
3115 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3116 xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
3117 &xpp
, &xecfg
, &ecb
);
3120 SHA1_Final(sha1
, &ctx
);
3124 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3126 struct diff_queue_struct
*q
= &diff_queued_diff
;
3128 int result
= diff_get_patch_id(options
, sha1
);
3130 for (i
= 0; i
< q
->nr
; i
++)
3131 diff_free_filepair(q
->queue
[i
]);
3135 q
->nr
= q
->alloc
= 0;
3140 static int is_summary_empty(const struct diff_queue_struct
*q
)
3144 for (i
= 0; i
< q
->nr
; i
++) {
3145 const struct diff_filepair
*p
= q
->queue
[i
];
3147 switch (p
->status
) {
3148 case DIFF_STATUS_DELETED
:
3149 case DIFF_STATUS_ADDED
:
3150 case DIFF_STATUS_COPIED
:
3151 case DIFF_STATUS_RENAMED
:
3156 if (p
->one
->mode
&& p
->two
->mode
&&
3157 p
->one
->mode
!= p
->two
->mode
)
3165 void diff_flush(struct diff_options
*options
)
3167 struct diff_queue_struct
*q
= &diff_queued_diff
;
3168 int i
, output_format
= options
->output_format
;
3172 * Order: raw, stat, summary, patch
3173 * or: name/name-status/checkdiff (other bits clear)
3178 if (output_format
& (DIFF_FORMAT_RAW
|
3180 DIFF_FORMAT_NAME_STATUS
|
3181 DIFF_FORMAT_CHECKDIFF
)) {
3182 for (i
= 0; i
< q
->nr
; i
++) {
3183 struct diff_filepair
*p
= q
->queue
[i
];
3184 if (check_pair_status(p
))
3185 flush_one_pair(p
, options
);
3190 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3191 struct diffstat_t diffstat
;
3193 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3194 for (i
= 0; i
< q
->nr
; i
++) {
3195 struct diff_filepair
*p
= q
->queue
[i
];
3196 if (check_pair_status(p
))
3197 diff_flush_stat(p
, options
, &diffstat
);
3199 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3200 show_numstat(&diffstat
, options
);
3201 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3202 show_stats(&diffstat
, options
);
3203 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3204 show_shortstats(&diffstat
, options
);
3205 free_diffstat_info(&diffstat
);
3208 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3209 show_dirstat(options
);
3211 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3212 for (i
= 0; i
< q
->nr
; i
++)
3213 diff_summary(options
->file
, q
->queue
[i
]);
3217 if (output_format
& DIFF_FORMAT_PATCH
) {
3219 putc(options
->line_termination
, options
->file
);
3220 if (options
->stat_sep
) {
3221 /* attach patch instead of inline */
3222 fputs(options
->stat_sep
, options
->file
);
3226 for (i
= 0; i
< q
->nr
; i
++) {
3227 struct diff_filepair
*p
= q
->queue
[i
];
3228 if (check_pair_status(p
))
3229 diff_flush_patch(p
, options
);
3233 if (output_format
& DIFF_FORMAT_CALLBACK
)
3234 options
->format_callback(q
, options
, options
->format_callback_data
);
3236 for (i
= 0; i
< q
->nr
; i
++)
3237 diff_free_filepair(q
->queue
[i
]);
3241 q
->nr
= q
->alloc
= 0;
3242 if (options
->close_file
)
3243 fclose(options
->file
);
3246 static void diffcore_apply_filter(const char *filter
)
3249 struct diff_queue_struct
*q
= &diff_queued_diff
;
3250 struct diff_queue_struct outq
;
3252 outq
.nr
= outq
.alloc
= 0;
3257 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3259 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3260 struct diff_filepair
*p
= q
->queue
[i
];
3261 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3263 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3265 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3266 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3267 strchr(filter
, p
->status
)))
3273 /* otherwise we will clear the whole queue
3274 * by copying the empty outq at the end of this
3275 * function, but first clear the current entries
3278 for (i
= 0; i
< q
->nr
; i
++)
3279 diff_free_filepair(q
->queue
[i
]);
3282 /* Only the matching ones */
3283 for (i
= 0; i
< q
->nr
; i
++) {
3284 struct diff_filepair
*p
= q
->queue
[i
];
3286 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3288 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3290 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3291 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3292 strchr(filter
, p
->status
)))
3295 diff_free_filepair(p
);
3302 /* Check whether two filespecs with the same mode and size are identical */
3303 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3304 struct diff_filespec
*two
)
3306 if (S_ISGITLINK(one
->mode
))
3308 if (diff_populate_filespec(one
, 0))
3310 if (diff_populate_filespec(two
, 0))
3312 return !memcmp(one
->data
, two
->data
, one
->size
);
3315 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3318 struct diff_queue_struct
*q
= &diff_queued_diff
;
3319 struct diff_queue_struct outq
;
3321 outq
.nr
= outq
.alloc
= 0;
3323 for (i
= 0; i
< q
->nr
; i
++) {
3324 struct diff_filepair
*p
= q
->queue
[i
];
3327 * 1. Entries that come from stat info dirtyness
3328 * always have both sides (iow, not create/delete),
3329 * one side of the object name is unknown, with
3330 * the same mode and size. Keep the ones that
3331 * do not match these criteria. They have real
3334 * 2. At this point, the file is known to be modified,
3335 * with the same mode and size, and the object
3336 * name of one side is unknown. Need to inspect
3337 * the identical contents.
3339 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3340 !DIFF_FILE_VALID(p
->two
) ||
3341 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3342 (p
->one
->mode
!= p
->two
->mode
) ||
3343 diff_populate_filespec(p
->one
, 1) ||
3344 diff_populate_filespec(p
->two
, 1) ||
3345 (p
->one
->size
!= p
->two
->size
) ||
3346 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3350 * The caller can subtract 1 from skip_stat_unmatch
3351 * to determine how many paths were dirty only
3352 * due to stat info mismatch.
3354 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3355 diffopt
->skip_stat_unmatch
++;
3356 diff_free_filepair(p
);
3363 void diffcore_std(struct diff_options
*options
)
3365 if (DIFF_OPT_TST(options
, QUIET
))
3368 if (options
->skip_stat_unmatch
&& !DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
3369 diffcore_skip_stat_unmatch(options
);
3370 if (options
->break_opt
!= -1)
3371 diffcore_break(options
->break_opt
);
3372 if (options
->detect_rename
)
3373 diffcore_rename(options
);
3374 if (options
->break_opt
!= -1)
3375 diffcore_merge_broken();
3376 if (options
->pickaxe
)
3377 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3378 if (options
->orderfile
)
3379 diffcore_order(options
->orderfile
);
3380 diff_resolve_rename_copy();
3381 diffcore_apply_filter(options
->filter
);
3383 if (diff_queued_diff
.nr
)
3384 DIFF_OPT_SET(options
, HAS_CHANGES
);
3386 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3389 int diff_result_code(struct diff_options
*opt
, int status
)
3392 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3393 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3395 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3396 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3398 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3399 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3404 void diff_addremove(struct diff_options
*options
,
3405 int addremove
, unsigned mode
,
3406 const unsigned char *sha1
,
3407 const char *concatpath
)
3409 struct diff_filespec
*one
, *two
;
3411 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
3414 /* This may look odd, but it is a preparation for
3415 * feeding "there are unchanged files which should
3416 * not produce diffs, but when you are doing copy
3417 * detection you would need them, so here they are"
3418 * entries to the diff-core. They will be prefixed
3419 * with something like '=' or '*' (I haven't decided
3420 * which but should not make any difference).
3421 * Feeding the same new and old to diff_change()
3422 * also has the same effect.
3423 * Before the final output happens, they are pruned after
3424 * merged into rename/copy pairs as appropriate.
3426 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3427 addremove
= (addremove
== '+' ? '-' :
3428 addremove
== '-' ? '+' : addremove
);
3430 if (options
->prefix
&&
3431 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3434 one
= alloc_filespec(concatpath
);
3435 two
= alloc_filespec(concatpath
);
3437 if (addremove
!= '+')
3438 fill_filespec(one
, sha1
, mode
);
3439 if (addremove
!= '-')
3440 fill_filespec(two
, sha1
, mode
);
3442 diff_queue(&diff_queued_diff
, one
, two
);
3443 DIFF_OPT_SET(options
, HAS_CHANGES
);
3446 void diff_change(struct diff_options
*options
,
3447 unsigned old_mode
, unsigned new_mode
,
3448 const unsigned char *old_sha1
,
3449 const unsigned char *new_sha1
,
3450 const char *concatpath
)
3452 struct diff_filespec
*one
, *two
;
3454 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
3455 && S_ISGITLINK(new_mode
))
3458 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3460 const unsigned char *tmp_c
;
3461 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3462 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3465 if (options
->prefix
&&
3466 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3469 one
= alloc_filespec(concatpath
);
3470 two
= alloc_filespec(concatpath
);
3471 fill_filespec(one
, old_sha1
, old_mode
);
3472 fill_filespec(two
, new_sha1
, new_mode
);
3474 diff_queue(&diff_queued_diff
, one
, two
);
3475 DIFF_OPT_SET(options
, HAS_CHANGES
);
3478 void diff_unmerge(struct diff_options
*options
,
3480 unsigned mode
, const unsigned char *sha1
)
3482 struct diff_filespec
*one
, *two
;
3484 if (options
->prefix
&&
3485 strncmp(path
, options
->prefix
, options
->prefix_length
))
3488 one
= alloc_filespec(path
);
3489 two
= alloc_filespec(path
);
3490 fill_filespec(one
, sha1
, mode
);
3491 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;