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
= 100;
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
)
134 if (!strcmp(var
, "diff.renamelimit")) {
135 diff_rename_limit_default
= git_config_int(var
, value
);
138 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
139 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
142 if (!strcmp(var
, "diff.renames")) {
144 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
145 else if (!strcasecmp(value
, "copies") ||
146 !strcasecmp(value
, "copy"))
147 diff_detect_rename_default
= DIFF_DETECT_COPY
;
148 else if (git_config_bool(var
,value
))
149 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
152 if (!strcmp(var
, "diff.autorefreshindex")) {
153 diff_auto_refresh_index
= git_config_bool(var
, value
);
156 if (!strcmp(var
, "diff.external")) {
158 return config_error_nonbool(var
);
159 external_diff_cmd_cfg
= xstrdup(value
);
162 if (!prefixcmp(var
, "diff.")) {
163 const char *ep
= strrchr(var
, '.');
165 if (ep
!= var
+ 4 && !strcmp(ep
, ".command"))
166 return parse_lldiff_command(var
, ep
, value
);
169 return git_diff_basic_config(var
, value
);
172 int git_diff_basic_config(const char *var
, const char *value
)
174 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
175 int slot
= parse_diff_color_slot(var
, 11);
177 return config_error_nonbool(var
);
178 color_parse(value
, var
, diff_colors
[slot
]);
182 if (!prefixcmp(var
, "diff.")) {
183 const char *ep
= strrchr(var
, '.');
185 if (!strcmp(ep
, ".funcname")) {
187 return config_error_nonbool(var
);
188 return parse_funcname_pattern(var
, ep
, value
);
193 return git_color_default_config(var
, value
);
196 static char *quote_two(const char *one
, const char *two
)
198 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
199 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
202 strbuf_init(&res
, 0);
203 if (need_one
+ need_two
) {
204 strbuf_addch(&res
, '"');
205 quote_c_style(one
, &res
, NULL
, 1);
206 quote_c_style(two
, &res
, NULL
, 1);
207 strbuf_addch(&res
, '"');
209 strbuf_addstr(&res
, one
);
210 strbuf_addstr(&res
, two
);
212 return strbuf_detach(&res
, NULL
);
215 static const char *external_diff(void)
217 static const char *external_diff_cmd
= NULL
;
218 static int done_preparing
= 0;
221 return external_diff_cmd
;
222 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
223 if (!external_diff_cmd
)
224 external_diff_cmd
= external_diff_cmd_cfg
;
226 return external_diff_cmd
;
229 static struct diff_tempfile
{
230 const char *name
; /* filename external diff should read from */
233 char tmp_path
[PATH_MAX
];
236 static int count_lines(const char *data
, int size
)
238 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
245 completely_empty
= 0;
249 completely_empty
= 0;
252 if (completely_empty
)
255 count
++; /* no trailing newline */
259 static void print_line_count(FILE *file
, int count
)
263 fprintf(file
, "0,0");
269 fprintf(file
, "1,%d", count
);
274 static void copy_file_with_prefix(FILE *file
,
275 int prefix
, const char *data
, int size
,
276 const char *set
, const char *reset
)
278 int ch
, nl_just_seen
= 1;
293 fprintf(file
, "%s\n\\ No newline at end of file\n", reset
);
296 static void emit_rewrite_diff(const char *name_a
,
298 struct diff_filespec
*one
,
299 struct diff_filespec
*two
,
300 struct diff_options
*o
)
303 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
304 const char *name_a_tab
, *name_b_tab
;
305 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
306 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
307 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
308 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
309 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
310 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
312 name_a
+= (*name_a
== '/');
313 name_b
+= (*name_b
== '/');
314 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
315 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
317 strbuf_reset(&a_name
);
318 strbuf_reset(&b_name
);
319 quote_two_c_style(&a_name
, o
->a_prefix
, name_a
, 0);
320 quote_two_c_style(&b_name
, o
->b_prefix
, name_b
, 0);
322 diff_populate_filespec(one
, 0);
323 diff_populate_filespec(two
, 0);
324 lc_a
= count_lines(one
->data
, one
->size
);
325 lc_b
= count_lines(two
->data
, two
->size
);
327 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
328 metainfo
, a_name
.buf
, name_a_tab
, reset
,
329 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
330 print_line_count(o
->file
, lc_a
);
331 fprintf(o
->file
, " +");
332 print_line_count(o
->file
, lc_b
);
333 fprintf(o
->file
, " @@%s\n", reset
);
335 copy_file_with_prefix(o
->file
, '-', one
->data
, one
->size
, old
, reset
);
337 copy_file_with_prefix(o
->file
, '+', two
->data
, two
->size
, new, reset
);
340 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
342 if (!DIFF_FILE_VALID(one
)) {
343 mf
->ptr
= (char *)""; /* does not matter */
347 else if (diff_populate_filespec(one
, 0))
350 mf
->size
= one
->size
;
354 struct diff_words_buffer
{
357 long current
; /* output pointer */
358 int suppressed_newline
;
361 static void diff_words_append(char *line
, unsigned long len
,
362 struct diff_words_buffer
*buffer
)
364 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
365 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
366 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
370 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
371 buffer
->text
.size
+= len
;
374 struct diff_words_data
{
375 struct xdiff_emit_state xm
;
376 struct diff_words_buffer minus
, plus
;
380 static void print_word(FILE *file
, struct diff_words_buffer
*buffer
, int len
, int color
,
381 int suppress_newline
)
389 ptr
= buffer
->text
.ptr
+ buffer
->current
;
390 buffer
->current
+= len
;
392 if (ptr
[len
- 1] == '\n') {
397 fputs(diff_get_color(1, color
), file
);
398 fwrite(ptr
, len
, 1, file
);
399 fputs(diff_get_color(1, DIFF_RESET
), file
);
402 if (suppress_newline
)
403 buffer
->suppressed_newline
= 1;
409 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
411 struct diff_words_data
*diff_words
= priv
;
413 if (diff_words
->minus
.suppressed_newline
) {
415 putc('\n', diff_words
->file
);
416 diff_words
->minus
.suppressed_newline
= 0;
422 print_word(diff_words
->file
,
423 &diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
426 print_word(diff_words
->file
,
427 &diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
430 print_word(diff_words
->file
,
431 &diff_words
->plus
, len
, DIFF_PLAIN
, 0);
432 diff_words
->minus
.current
+= len
;
437 /* this executes the word diff on the accumulated buffers */
438 static void diff_words_show(struct diff_words_data
*diff_words
)
443 mmfile_t minus
, plus
;
446 memset(&xecfg
, 0, sizeof(xecfg
));
447 minus
.size
= diff_words
->minus
.text
.size
;
448 minus
.ptr
= xmalloc(minus
.size
);
449 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
450 for (i
= 0; i
< minus
.size
; i
++)
451 if (isspace(minus
.ptr
[i
]))
453 diff_words
->minus
.current
= 0;
455 plus
.size
= diff_words
->plus
.text
.size
;
456 plus
.ptr
= xmalloc(plus
.size
);
457 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
458 for (i
= 0; i
< plus
.size
; i
++)
459 if (isspace(plus
.ptr
[i
]))
461 diff_words
->plus
.current
= 0;
463 xpp
.flags
= XDF_NEED_MINIMAL
;
464 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
465 ecb
.outf
= xdiff_outf
;
466 ecb
.priv
= diff_words
;
467 diff_words
->xm
.consume
= fn_out_diff_words_aux
;
468 xdi_diff(&minus
, &plus
, &xpp
, &xecfg
, &ecb
);
472 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
474 if (diff_words
->minus
.suppressed_newline
) {
475 putc('\n', diff_words
->file
);
476 diff_words
->minus
.suppressed_newline
= 0;
480 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
482 struct emit_callback
{
483 struct xdiff_emit_state xm
;
484 int nparents
, color_diff
;
486 sane_truncate_fn truncate
;
487 const char **label_path
;
488 struct diff_words_data
*diff_words
;
493 static void free_diff_words_data(struct emit_callback
*ecbdata
)
495 if (ecbdata
->diff_words
) {
497 if (ecbdata
->diff_words
->minus
.text
.size
||
498 ecbdata
->diff_words
->plus
.text
.size
)
499 diff_words_show(ecbdata
->diff_words
);
501 free (ecbdata
->diff_words
->minus
.text
.ptr
);
502 free (ecbdata
->diff_words
->plus
.text
.ptr
);
503 free(ecbdata
->diff_words
);
504 ecbdata
->diff_words
= NULL
;
508 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
511 return diff_colors
[ix
];
515 static void emit_line(FILE *file
, const char *set
, const char *reset
, const char *line
, int len
)
517 if (len
> 0 && line
[len
-1] == '\n')
521 fwrite(line
, len
, 1, file
);
526 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
528 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
529 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
532 emit_line(ecbdata
->file
, set
, reset
, line
, len
);
534 /* Emit just the prefix, then the rest. */
535 emit_line(ecbdata
->file
, set
, reset
, line
, ecbdata
->nparents
);
536 (void)check_and_emit_line(line
+ ecbdata
->nparents
,
537 len
- ecbdata
->nparents
, ecbdata
->ws_rule
,
538 ecbdata
->file
, set
, reset
, ws
);
542 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
549 return ecb
->truncate(line
, len
);
553 (void) utf8_width(&cp
, &l
);
555 break; /* truncated in the middle? */
560 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
564 struct emit_callback
*ecbdata
= priv
;
565 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
566 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
567 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
569 *(ecbdata
->found_changesp
) = 1;
571 if (ecbdata
->label_path
[0]) {
572 const char *name_a_tab
, *name_b_tab
;
574 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
575 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
577 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
578 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
579 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
580 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
581 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
584 /* This is not really necessary for now because
585 * this codepath only deals with two-way diffs.
587 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
589 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
590 ecbdata
->nparents
= i
- 1;
591 len
= sane_truncate_line(ecbdata
, line
, len
);
592 emit_line(ecbdata
->file
,
593 diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
595 if (line
[len
-1] != '\n')
596 putc('\n', ecbdata
->file
);
600 if (len
< ecbdata
->nparents
) {
601 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
606 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
607 /* fall back to normal diff */
608 free_diff_words_data(ecbdata
);
609 if (ecbdata
->diff_words
) {
610 if (line
[0] == '-') {
611 diff_words_append(line
, len
,
612 &ecbdata
->diff_words
->minus
);
614 } else if (line
[0] == '+') {
615 diff_words_append(line
, len
,
616 &ecbdata
->diff_words
->plus
);
619 if (ecbdata
->diff_words
->minus
.text
.size
||
620 ecbdata
->diff_words
->plus
.text
.size
)
621 diff_words_show(ecbdata
->diff_words
);
624 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
627 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
629 color
= DIFF_FILE_OLD
;
630 else if (line
[i
] == '+')
631 color
= DIFF_FILE_NEW
;
634 if (color
!= DIFF_FILE_NEW
) {
635 emit_line(ecbdata
->file
,
636 diff_get_color(ecbdata
->color_diff
, color
),
640 emit_add_line(reset
, ecbdata
, line
, len
);
643 static char *pprint_rename(const char *a
, const char *b
)
648 int pfx_length
, sfx_length
;
649 int len_a
= strlen(a
);
650 int len_b
= strlen(b
);
651 int a_midlen
, b_midlen
;
652 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
653 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
655 strbuf_init(&name
, 0);
656 if (qlen_a
|| qlen_b
) {
657 quote_c_style(a
, &name
, NULL
, 0);
658 strbuf_addstr(&name
, " => ");
659 quote_c_style(b
, &name
, NULL
, 0);
660 return strbuf_detach(&name
, NULL
);
663 /* Find common prefix */
665 while (*old
&& *new && *old
== *new) {
667 pfx_length
= old
- a
+ 1;
672 /* Find common suffix */
676 while (a
<= old
&& b
<= new && *old
== *new) {
678 sfx_length
= len_a
- (old
- a
);
684 * pfx{mid-a => mid-b}sfx
685 * {pfx-a => pfx-b}sfx
686 * pfx{sfx-a => sfx-b}
689 a_midlen
= len_a
- pfx_length
- sfx_length
;
690 b_midlen
= len_b
- pfx_length
- sfx_length
;
696 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
697 if (pfx_length
+ sfx_length
) {
698 strbuf_add(&name
, a
, pfx_length
);
699 strbuf_addch(&name
, '{');
701 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
702 strbuf_addstr(&name
, " => ");
703 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
704 if (pfx_length
+ sfx_length
) {
705 strbuf_addch(&name
, '}');
706 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
708 return strbuf_detach(&name
, NULL
);
712 struct xdiff_emit_state xm
;
716 struct diffstat_file
{
720 unsigned is_unmerged
:1;
721 unsigned is_binary
:1;
722 unsigned is_renamed
:1;
723 unsigned int added
, deleted
;
727 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
731 struct diffstat_file
*x
;
732 x
= xcalloc(sizeof (*x
), 1);
733 if (diffstat
->nr
== diffstat
->alloc
) {
734 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
735 diffstat
->files
= xrealloc(diffstat
->files
,
736 diffstat
->alloc
* sizeof(x
));
738 diffstat
->files
[diffstat
->nr
++] = x
;
740 x
->from_name
= xstrdup(name_a
);
741 x
->name
= xstrdup(name_b
);
746 x
->name
= xstrdup(name_a
);
751 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
753 struct diffstat_t
*diffstat
= priv
;
754 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
758 else if (line
[0] == '-')
762 const char mime_boundary_leader
[] = "------------";
764 static int scale_linear(int it
, int width
, int max_change
)
767 * make sure that at least one '-' is printed if there were deletions,
768 * and likewise for '+'.
772 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
775 static void show_name(FILE *file
,
776 const char *prefix
, const char *name
, int len
,
777 const char *reset
, const char *set
)
779 fprintf(file
, " %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
782 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
786 fprintf(file
, "%s", set
);
789 fprintf(file
, "%s", reset
);
792 static void fill_print_name(struct diffstat_file
*file
)
796 if (file
->print_name
)
799 if (!file
->is_renamed
) {
801 strbuf_init(&buf
, 0);
802 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
803 pname
= strbuf_detach(&buf
, NULL
);
806 strbuf_release(&buf
);
809 pname
= pprint_rename(file
->from_name
, file
->name
);
811 file
->print_name
= pname
;
814 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
816 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
817 int max_change
= 0, max_len
= 0;
818 int total_files
= data
->nr
;
819 int width
, name_width
;
820 const char *reset
, *set
, *add_c
, *del_c
;
825 width
= options
->stat_width
? options
->stat_width
: 80;
826 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
828 /* Sanity: give at least 5 columns to the graph,
829 * but leave at least 10 columns for the name.
831 if (width
< name_width
+ 15) {
832 if (name_width
<= 25)
833 width
= name_width
+ 15;
835 name_width
= width
- 15;
838 /* Find the longest filename and max number of changes */
839 reset
= diff_get_color_opt(options
, DIFF_RESET
);
840 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
841 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
842 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
844 for (i
= 0; i
< data
->nr
; i
++) {
845 struct diffstat_file
*file
= data
->files
[i
];
846 int change
= file
->added
+ file
->deleted
;
847 fill_print_name(file
);
848 len
= strlen(file
->print_name
);
852 if (file
->is_binary
|| file
->is_unmerged
)
854 if (max_change
< change
)
858 /* Compute the width of the graph part;
859 * 10 is for one blank at the beginning of the line plus
860 * " | count " between the name and the graph.
862 * From here on, name_width is the width of the name area,
863 * and width is the width of the graph area.
865 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
866 if (width
< (name_width
+ 10) + max_change
)
867 width
= width
- (name_width
+ 10);
871 for (i
= 0; i
< data
->nr
; i
++) {
872 const char *prefix
= "";
873 char *name
= data
->files
[i
]->print_name
;
874 int added
= data
->files
[i
]->added
;
875 int deleted
= data
->files
[i
]->deleted
;
879 * "scale" the filename
882 name_len
= strlen(name
);
883 if (name_width
< name_len
) {
887 name
+= name_len
- len
;
888 slash
= strchr(name
, '/');
893 if (data
->files
[i
]->is_binary
) {
894 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
895 fprintf(options
->file
, " Bin ");
896 fprintf(options
->file
, "%s%d%s", del_c
, deleted
, reset
);
897 fprintf(options
->file
, " -> ");
898 fprintf(options
->file
, "%s%d%s", add_c
, added
, reset
);
899 fprintf(options
->file
, " bytes");
900 fprintf(options
->file
, "\n");
903 else if (data
->files
[i
]->is_unmerged
) {
904 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
905 fprintf(options
->file
, " Unmerged\n");
908 else if (!data
->files
[i
]->is_renamed
&&
909 (added
+ deleted
== 0)) {
915 * scale the add/delete
923 if (width
<= max_change
) {
924 add
= scale_linear(add
, width
, max_change
);
925 del
= scale_linear(del
, width
, max_change
);
928 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
929 fprintf(options
->file
, "%5d ", added
+ deleted
);
930 show_graph(options
->file
, '+', add
, add_c
, reset
);
931 show_graph(options
->file
, '-', del
, del_c
, reset
);
932 fprintf(options
->file
, "\n");
934 fprintf(options
->file
,
935 "%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
936 set
, total_files
, adds
, dels
, reset
);
939 static void show_shortstats(struct diffstat_t
* data
, struct diff_options
*options
)
941 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
946 for (i
= 0; i
< data
->nr
; i
++) {
947 if (!data
->files
[i
]->is_binary
&&
948 !data
->files
[i
]->is_unmerged
) {
949 int added
= data
->files
[i
]->added
;
950 int deleted
= data
->files
[i
]->deleted
;
951 if (!data
->files
[i
]->is_renamed
&&
952 (added
+ deleted
== 0)) {
960 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
961 total_files
, adds
, dels
);
964 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
971 for (i
= 0; i
< data
->nr
; i
++) {
972 struct diffstat_file
*file
= data
->files
[i
];
975 fprintf(options
->file
, "-\t-\t");
977 fprintf(options
->file
,
978 "%d\t%d\t", file
->added
, file
->deleted
);
979 if (options
->line_termination
) {
980 fill_print_name(file
);
981 if (!file
->is_renamed
)
982 write_name_quoted(file
->name
, options
->file
,
983 options
->line_termination
);
985 fputs(file
->print_name
, options
->file
);
986 putc(options
->line_termination
, options
->file
);
989 if (file
->is_renamed
) {
990 putc('\0', options
->file
);
991 write_name_quoted(file
->from_name
, options
->file
, '\0');
993 write_name_quoted(file
->name
, options
->file
, '\0');
998 struct diffstat_dir
{
999 struct diffstat_file
**files
;
1000 int nr
, percent
, cumulative
;
1003 static long gather_dirstat(FILE *file
, struct diffstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1005 unsigned long this_dir
= 0;
1006 unsigned int sources
= 0;
1009 struct diffstat_file
*f
= *dir
->files
;
1010 int namelen
= strlen(f
->name
);
1014 if (namelen
< baselen
)
1016 if (memcmp(f
->name
, base
, baselen
))
1018 slash
= strchr(f
->name
+ baselen
, '/');
1020 int newbaselen
= slash
+ 1 - f
->name
;
1021 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1024 if (f
->is_unmerged
|| f
->is_binary
)
1027 this = f
->added
+ f
->deleted
;
1036 * We don't report dirstat's for
1038 * - or cases where everything came from a single directory
1039 * under this directory (sources == 1).
1041 if (baselen
&& sources
!= 1) {
1042 int permille
= this_dir
* 1000 / changed
;
1044 int percent
= permille
/ 10;
1045 if (percent
>= dir
->percent
) {
1046 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1047 if (!dir
->cumulative
)
1055 static void show_dirstat(struct diffstat_t
*data
, struct diff_options
*options
)
1058 unsigned long changed
;
1059 struct diffstat_dir dir
;
1061 /* Calculate total changes */
1063 for (i
= 0; i
< data
->nr
; i
++) {
1064 if (data
->files
[i
]->is_binary
|| data
->files
[i
]->is_unmerged
)
1066 changed
+= data
->files
[i
]->added
;
1067 changed
+= data
->files
[i
]->deleted
;
1070 /* This can happen even with many files, if everything was renames */
1074 /* Show all directories with more than x% of the changes */
1075 dir
.files
= data
->files
;
1077 dir
.percent
= options
->dirstat_percent
;
1078 dir
.cumulative
= options
->output_format
& DIFF_FORMAT_CUMULATIVE
;
1079 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1082 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1085 for (i
= 0; i
< diffstat
->nr
; i
++) {
1086 struct diffstat_file
*f
= diffstat
->files
[i
];
1087 if (f
->name
!= f
->print_name
)
1088 free(f
->print_name
);
1093 free(diffstat
->files
);
1096 struct checkdiff_t
{
1097 struct xdiff_emit_state xm
;
1098 const char *filename
;
1099 int lineno
, color_diff
;
1105 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1107 struct checkdiff_t
*data
= priv
;
1108 const char *ws
= diff_get_color(data
->color_diff
, DIFF_WHITESPACE
);
1109 const char *reset
= diff_get_color(data
->color_diff
, DIFF_RESET
);
1110 const char *set
= diff_get_color(data
->color_diff
, DIFF_FILE_NEW
);
1113 if (line
[0] == '+') {
1115 data
->status
= check_and_emit_line(line
+ 1, len
- 1,
1116 data
->ws_rule
, NULL
, NULL
, NULL
, NULL
);
1119 err
= whitespace_error_string(data
->status
);
1120 fprintf(data
->file
, "%s:%d: %s.\n", data
->filename
, data
->lineno
, err
);
1122 emit_line(data
->file
, set
, reset
, line
, 1);
1123 (void)check_and_emit_line(line
+ 1, len
- 1, data
->ws_rule
,
1124 data
->file
, set
, reset
, ws
);
1125 } else if (line
[0] == ' ')
1127 else if (line
[0] == '@') {
1128 char *plus
= strchr(line
, '+');
1130 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1132 die("invalid diff");
1136 static unsigned char *deflate_it(char *data
,
1138 unsigned long *result_size
)
1141 unsigned char *deflated
;
1144 memset(&stream
, 0, sizeof(stream
));
1145 deflateInit(&stream
, zlib_compression_level
);
1146 bound
= deflateBound(&stream
, size
);
1147 deflated
= xmalloc(bound
);
1148 stream
.next_out
= deflated
;
1149 stream
.avail_out
= bound
;
1151 stream
.next_in
= (unsigned char *)data
;
1152 stream
.avail_in
= size
;
1153 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1155 deflateEnd(&stream
);
1156 *result_size
= stream
.total_out
;
1160 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1166 unsigned long orig_size
;
1167 unsigned long delta_size
;
1168 unsigned long deflate_size
;
1169 unsigned long data_size
;
1171 /* We could do deflated delta, or we could do just deflated two,
1172 * whichever is smaller.
1175 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1176 if (one
->size
&& two
->size
) {
1177 delta
= diff_delta(one
->ptr
, one
->size
,
1178 two
->ptr
, two
->size
,
1179 &delta_size
, deflate_size
);
1181 void *to_free
= delta
;
1182 orig_size
= delta_size
;
1183 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1188 if (delta
&& delta_size
< deflate_size
) {
1189 fprintf(file
, "delta %lu\n", orig_size
);
1192 data_size
= delta_size
;
1195 fprintf(file
, "literal %lu\n", two
->size
);
1198 data_size
= deflate_size
;
1201 /* emit data encoded in base85 */
1204 int bytes
= (52 < data_size
) ? 52 : data_size
;
1208 line
[0] = bytes
+ 'A' - 1;
1210 line
[0] = bytes
- 26 + 'a' - 1;
1211 encode_85(line
+ 1, cp
, bytes
);
1212 cp
= (char *) cp
+ bytes
;
1216 fprintf(file
, "\n");
1220 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1222 fprintf(file
, "GIT binary patch\n");
1223 emit_binary_diff_body(file
, one
, two
);
1224 emit_binary_diff_body(file
, two
, one
);
1227 static void setup_diff_attr_check(struct git_attr_check
*check
)
1229 static struct git_attr
*attr_diff
;
1232 attr_diff
= git_attr("diff", 4);
1234 check
[0].attr
= attr_diff
;
1237 static void diff_filespec_check_attr(struct diff_filespec
*one
)
1239 struct git_attr_check attr_diff_check
;
1240 int check_from_data
= 0;
1242 if (one
->checked_attr
)
1245 setup_diff_attr_check(&attr_diff_check
);
1247 one
->funcname_pattern_ident
= NULL
;
1249 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1253 value
= attr_diff_check
.value
;
1254 if (ATTR_TRUE(value
))
1256 else if (ATTR_FALSE(value
))
1259 check_from_data
= 1;
1261 /* funcname pattern ident */
1262 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1265 one
->funcname_pattern_ident
= value
;
1268 if (check_from_data
) {
1269 if (!one
->data
&& DIFF_FILE_VALID(one
))
1270 diff_populate_filespec(one
, 0);
1273 one
->is_binary
= buffer_is_binary(one
->data
, one
->size
);
1277 int diff_filespec_is_binary(struct diff_filespec
*one
)
1279 diff_filespec_check_attr(one
);
1280 return one
->is_binary
;
1283 static const char *funcname_pattern(const char *ident
)
1285 struct funcname_pattern
*pp
;
1287 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
1288 if (!strcmp(ident
, pp
->name
))
1293 static struct builtin_funcname_pattern
{
1295 const char *pattern
;
1296 } builtin_funcname_pattern
[] = {
1297 { "java", "!^[ ]*\\(catch\\|do\\|for\\|if\\|instanceof\\|"
1298 "new\\|return\\|switch\\|throw\\|while\\)\n"
1300 "[A-Za-z_][A-Za-z_0-9]*\\)\\{2,\\}"
1302 { "tex", "^\\(\\\\\\(sub\\)*section{.*\\)$" },
1305 static const char *diff_funcname_pattern(struct diff_filespec
*one
)
1307 const char *ident
, *pattern
;
1310 diff_filespec_check_attr(one
);
1311 ident
= one
->funcname_pattern_ident
;
1315 * If the config file has "funcname.default" defined, that
1316 * regexp is used; otherwise NULL is returned and xemit uses
1317 * the built-in default.
1319 return funcname_pattern("default");
1321 /* Look up custom "funcname.$ident" regexp from config. */
1322 pattern
= funcname_pattern(ident
);
1327 * And define built-in fallback patterns here. Note that
1328 * these can be overridden by the user's config settings.
1330 for (i
= 0; i
< ARRAY_SIZE(builtin_funcname_pattern
); i
++)
1331 if (!strcmp(ident
, builtin_funcname_pattern
[i
].name
))
1332 return builtin_funcname_pattern
[i
].pattern
;
1337 static void builtin_diff(const char *name_a
,
1339 struct diff_filespec
*one
,
1340 struct diff_filespec
*two
,
1341 const char *xfrm_msg
,
1342 struct diff_options
*o
,
1343 int complete_rewrite
)
1347 char *a_one
, *b_two
;
1348 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1349 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1351 a_one
= quote_two(o
->a_prefix
, name_a
+ (*name_a
== '/'));
1352 b_two
= quote_two(o
->b_prefix
, name_b
+ (*name_b
== '/'));
1353 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1354 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1355 fprintf(o
->file
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1356 if (lbl
[0][0] == '/') {
1358 fprintf(o
->file
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1359 if (xfrm_msg
&& xfrm_msg
[0])
1360 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1362 else if (lbl
[1][0] == '/') {
1363 fprintf(o
->file
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1364 if (xfrm_msg
&& xfrm_msg
[0])
1365 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1368 if (one
->mode
!= two
->mode
) {
1369 fprintf(o
->file
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1370 fprintf(o
->file
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1372 if (xfrm_msg
&& xfrm_msg
[0])
1373 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1375 * we do not run diff between different kind
1378 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1379 goto free_ab_and_return
;
1380 if (complete_rewrite
) {
1381 emit_rewrite_diff(name_a
, name_b
, one
, two
, o
);
1382 o
->found_changes
= 1;
1383 goto free_ab_and_return
;
1387 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1388 die("unable to read files to diff");
1390 if (!DIFF_OPT_TST(o
, TEXT
) &&
1391 (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
))) {
1392 /* Quite common confusing case */
1393 if (mf1
.size
== mf2
.size
&&
1394 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1395 goto free_ab_and_return
;
1396 if (DIFF_OPT_TST(o
, BINARY
))
1397 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1399 fprintf(o
->file
, "Binary files %s and %s differ\n",
1401 o
->found_changes
= 1;
1404 /* Crazy xdl interfaces.. */
1405 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1409 struct emit_callback ecbdata
;
1410 const char *funcname_pattern
;
1412 funcname_pattern
= diff_funcname_pattern(one
);
1413 if (!funcname_pattern
)
1414 funcname_pattern
= diff_funcname_pattern(two
);
1416 memset(&xecfg
, 0, sizeof(xecfg
));
1417 memset(&ecbdata
, 0, sizeof(ecbdata
));
1418 ecbdata
.label_path
= lbl
;
1419 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1420 ecbdata
.found_changesp
= &o
->found_changes
;
1421 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1422 ecbdata
.file
= o
->file
;
1423 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1424 xecfg
.ctxlen
= o
->context
;
1425 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1426 if (funcname_pattern
)
1427 xdiff_set_find_func(&xecfg
, funcname_pattern
);
1430 else if (!prefixcmp(diffopts
, "--unified="))
1431 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1432 else if (!prefixcmp(diffopts
, "-u"))
1433 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1434 ecb
.outf
= xdiff_outf
;
1435 ecb
.priv
= &ecbdata
;
1436 ecbdata
.xm
.consume
= fn_out_consume
;
1437 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
)) {
1438 ecbdata
.diff_words
=
1439 xcalloc(1, sizeof(struct diff_words_data
));
1440 ecbdata
.diff_words
->file
= o
->file
;
1442 xdi_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1443 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
))
1444 free_diff_words_data(&ecbdata
);
1448 diff_free_filespec_data(one
);
1449 diff_free_filespec_data(two
);
1455 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1456 struct diff_filespec
*one
,
1457 struct diff_filespec
*two
,
1458 struct diffstat_t
*diffstat
,
1459 struct diff_options
*o
,
1460 int complete_rewrite
)
1463 struct diffstat_file
*data
;
1465 data
= diffstat_add(diffstat
, name_a
, name_b
);
1468 data
->is_unmerged
= 1;
1471 if (complete_rewrite
) {
1472 diff_populate_filespec(one
, 0);
1473 diff_populate_filespec(two
, 0);
1474 data
->deleted
= count_lines(one
->data
, one
->size
);
1475 data
->added
= count_lines(two
->data
, two
->size
);
1476 goto free_and_return
;
1478 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1479 die("unable to read files to diff");
1481 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1482 data
->is_binary
= 1;
1483 data
->added
= mf2
.size
;
1484 data
->deleted
= mf1
.size
;
1486 /* Crazy xdl interfaces.. */
1491 memset(&xecfg
, 0, sizeof(xecfg
));
1492 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1493 ecb
.outf
= xdiff_outf
;
1494 ecb
.priv
= diffstat
;
1495 xdi_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1499 diff_free_filespec_data(one
);
1500 diff_free_filespec_data(two
);
1503 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1504 const char *attr_path
,
1505 struct diff_filespec
*one
,
1506 struct diff_filespec
*two
, struct diff_options
*o
)
1509 struct checkdiff_t data
;
1514 memset(&data
, 0, sizeof(data
));
1515 data
.xm
.consume
= checkdiff_consume
;
1516 data
.filename
= name_b
? name_b
: name_a
;
1518 data
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1519 data
.ws_rule
= whitespace_rule(attr_path
);
1520 data
.file
= o
->file
;
1522 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1523 die("unable to read files to diff");
1525 if (diff_filespec_is_binary(two
))
1526 goto free_and_return
;
1528 /* Crazy xdl interfaces.. */
1533 memset(&xecfg
, 0, sizeof(xecfg
));
1534 xpp
.flags
= XDF_NEED_MINIMAL
;
1535 ecb
.outf
= xdiff_outf
;
1537 xdi_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
1540 diff_free_filespec_data(one
);
1541 diff_free_filespec_data(two
);
1543 DIFF_OPT_SET(o
, CHECK_FAILED
);
1546 struct diff_filespec
*alloc_filespec(const char *path
)
1548 int namelen
= strlen(path
);
1549 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1551 memset(spec
, 0, sizeof(*spec
));
1552 spec
->path
= (char *)(spec
+ 1);
1553 memcpy(spec
->path
, path
, namelen
+1);
1558 void free_filespec(struct diff_filespec
*spec
)
1560 if (!--spec
->count
) {
1561 diff_free_filespec_data(spec
);
1566 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1567 unsigned short mode
)
1570 spec
->mode
= canon_mode(mode
);
1571 hashcpy(spec
->sha1
, sha1
);
1572 spec
->sha1_valid
= !is_null_sha1(sha1
);
1577 * Given a name and sha1 pair, if the index tells us the file in
1578 * the work tree has that object contents, return true, so that
1579 * prepare_temp_file() does not have to inflate and extract.
1581 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1583 struct cache_entry
*ce
;
1587 /* We do not read the cache ourselves here, because the
1588 * benchmark with my previous version that always reads cache
1589 * shows that it makes things worse for diff-tree comparing
1590 * two linux-2.6 kernel trees in an already checked out work
1591 * tree. This is because most diff-tree comparisons deal with
1592 * only a small number of files, while reading the cache is
1593 * expensive for a large project, and its cost outweighs the
1594 * savings we get by not inflating the object to a temporary
1595 * file. Practically, this code only helps when we are used
1596 * by diff-cache --cached, which does read the cache before
1602 /* We want to avoid the working directory if our caller
1603 * doesn't need the data in a normal file, this system
1604 * is rather slow with its stat/open/mmap/close syscalls,
1605 * and the object is contained in a pack file. The pack
1606 * is probably already open and will be faster to obtain
1607 * the data through than the working directory. Loose
1608 * objects however would tend to be slower as they need
1609 * to be individually opened and inflated.
1611 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1615 pos
= cache_name_pos(name
, len
);
1618 ce
= active_cache
[pos
];
1621 * This is not the sha1 we are looking for, or
1622 * unreusable because it is not a regular file.
1624 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
1628 * If ce matches the file in the work tree, we can reuse it.
1630 if (ce_uptodate(ce
) ||
1631 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
1637 static int populate_from_stdin(struct diff_filespec
*s
)
1642 strbuf_init(&buf
, 0);
1643 if (strbuf_read(&buf
, 0, 0) < 0)
1644 return error("error while reading from stdin %s",
1647 s
->should_munmap
= 0;
1648 s
->data
= strbuf_detach(&buf
, &size
);
1654 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1657 char *data
= xmalloc(100);
1658 len
= snprintf(data
, 100,
1659 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1671 * While doing rename detection and pickaxe operation, we may need to
1672 * grab the data for the blob (or file) for our own in-core comparison.
1673 * diff_filespec has data and size fields for this purpose.
1675 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1678 if (!DIFF_FILE_VALID(s
))
1679 die("internal error: asking to populate invalid file.");
1680 if (S_ISDIR(s
->mode
))
1686 if (size_only
&& 0 < s
->size
)
1689 if (S_ISGITLINK(s
->mode
))
1690 return diff_populate_gitlink(s
, size_only
);
1692 if (!s
->sha1_valid
||
1693 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1698 if (!strcmp(s
->path
, "-"))
1699 return populate_from_stdin(s
);
1701 if (lstat(s
->path
, &st
) < 0) {
1702 if (errno
== ENOENT
) {
1706 s
->data
= (char *)"";
1711 s
->size
= xsize_t(st
.st_size
);
1716 if (S_ISLNK(st
.st_mode
)) {
1718 s
->data
= xmalloc(s
->size
);
1720 ret
= readlink(s
->path
, s
->data
, s
->size
);
1727 fd
= open(s
->path
, O_RDONLY
);
1730 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1732 s
->should_munmap
= 1;
1735 * Convert from working tree format to canonical git format
1737 strbuf_init(&buf
, 0);
1738 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
1740 munmap(s
->data
, s
->size
);
1741 s
->should_munmap
= 0;
1742 s
->data
= strbuf_detach(&buf
, &size
);
1748 enum object_type type
;
1750 type
= sha1_object_info(s
->sha1
, &s
->size
);
1752 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1759 void diff_free_filespec_blob(struct diff_filespec
*s
)
1763 else if (s
->should_munmap
)
1764 munmap(s
->data
, s
->size
);
1766 if (s
->should_free
|| s
->should_munmap
) {
1767 s
->should_free
= s
->should_munmap
= 0;
1772 void diff_free_filespec_data(struct diff_filespec
*s
)
1774 diff_free_filespec_blob(s
);
1779 static void prep_temp_blob(struct diff_tempfile
*temp
,
1782 const unsigned char *sha1
,
1787 fd
= git_mkstemp(temp
->tmp_path
, PATH_MAX
, ".diff_XXXXXX");
1789 die("unable to create temp-file: %s", strerror(errno
));
1790 if (write_in_full(fd
, blob
, size
) != size
)
1791 die("unable to write temp-file");
1793 temp
->name
= temp
->tmp_path
;
1794 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1796 sprintf(temp
->mode
, "%06o", mode
);
1799 static void prepare_temp_file(const char *name
,
1800 struct diff_tempfile
*temp
,
1801 struct diff_filespec
*one
)
1803 if (!DIFF_FILE_VALID(one
)) {
1805 /* A '-' entry produces this for file-2, and
1806 * a '+' entry produces this for file-1.
1808 temp
->name
= "/dev/null";
1809 strcpy(temp
->hex
, ".");
1810 strcpy(temp
->mode
, ".");
1814 if (!one
->sha1_valid
||
1815 reuse_worktree_file(name
, one
->sha1
, 1)) {
1817 if (lstat(name
, &st
) < 0) {
1818 if (errno
== ENOENT
)
1819 goto not_a_valid_file
;
1820 die("stat(%s): %s", name
, strerror(errno
));
1822 if (S_ISLNK(st
.st_mode
)) {
1824 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1825 size_t sz
= xsize_t(st
.st_size
);
1826 if (sizeof(buf
) <= st
.st_size
)
1827 die("symlink too long: %s", name
);
1828 ret
= readlink(name
, buf
, sz
);
1830 die("readlink(%s)", name
);
1831 prep_temp_blob(temp
, buf
, sz
,
1833 one
->sha1
: null_sha1
),
1835 one
->mode
: S_IFLNK
));
1838 /* we can borrow from the file in the work tree */
1840 if (!one
->sha1_valid
)
1841 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1843 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1844 /* Even though we may sometimes borrow the
1845 * contents from the work tree, we always want
1846 * one->mode. mode is trustworthy even when
1847 * !(one->sha1_valid), as long as
1848 * DIFF_FILE_VALID(one).
1850 sprintf(temp
->mode
, "%06o", one
->mode
);
1855 if (diff_populate_filespec(one
, 0))
1856 die("cannot read data blob for %s", one
->path
);
1857 prep_temp_blob(temp
, one
->data
, one
->size
,
1858 one
->sha1
, one
->mode
);
1862 static void remove_tempfile(void)
1866 for (i
= 0; i
< 2; i
++)
1867 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
1868 unlink(diff_temp
[i
].name
);
1869 diff_temp
[i
].name
= NULL
;
1873 static void remove_tempfile_on_signal(int signo
)
1876 signal(SIGINT
, SIG_DFL
);
1880 /* An external diff command takes:
1882 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1883 * infile2 infile2-sha1 infile2-mode [ rename-to ]
1886 static void run_external_diff(const char *pgm
,
1889 struct diff_filespec
*one
,
1890 struct diff_filespec
*two
,
1891 const char *xfrm_msg
,
1892 int complete_rewrite
)
1894 const char *spawn_arg
[10];
1895 struct diff_tempfile
*temp
= diff_temp
;
1897 static int atexit_asked
= 0;
1898 const char *othername
;
1899 const char **arg
= &spawn_arg
[0];
1901 othername
= (other
? other
: name
);
1903 prepare_temp_file(name
, &temp
[0], one
);
1904 prepare_temp_file(othername
, &temp
[1], two
);
1905 if (! atexit_asked
&&
1906 (temp
[0].name
== temp
[0].tmp_path
||
1907 temp
[1].name
== temp
[1].tmp_path
)) {
1909 atexit(remove_tempfile
);
1911 signal(SIGINT
, remove_tempfile_on_signal
);
1917 *arg
++ = temp
[0].name
;
1918 *arg
++ = temp
[0].hex
;
1919 *arg
++ = temp
[0].mode
;
1920 *arg
++ = temp
[1].name
;
1921 *arg
++ = temp
[1].hex
;
1922 *arg
++ = temp
[1].mode
;
1933 retval
= run_command_v_opt(spawn_arg
, 0);
1936 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
1941 static const char *external_diff_attr(const char *name
)
1943 struct git_attr_check attr_diff_check
;
1948 setup_diff_attr_check(&attr_diff_check
);
1949 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
1950 const char *value
= attr_diff_check
.value
;
1951 if (!ATTR_TRUE(value
) &&
1952 !ATTR_FALSE(value
) &&
1953 !ATTR_UNSET(value
)) {
1954 struct ll_diff_driver
*drv
;
1956 for (drv
= user_diff
; drv
; drv
= drv
->next
)
1957 if (!strcmp(drv
->name
, value
))
1964 static void run_diff_cmd(const char *pgm
,
1967 const char *attr_path
,
1968 struct diff_filespec
*one
,
1969 struct diff_filespec
*two
,
1970 const char *xfrm_msg
,
1971 struct diff_options
*o
,
1972 int complete_rewrite
)
1974 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
1977 const char *cmd
= external_diff_attr(attr_path
);
1983 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
1988 builtin_diff(name
, other
? other
: name
,
1989 one
, two
, xfrm_msg
, o
, complete_rewrite
);
1991 fprintf(o
->file
, "* Unmerged path %s\n", name
);
1994 static void diff_fill_sha1_info(struct diff_filespec
*one
)
1996 if (DIFF_FILE_VALID(one
)) {
1997 if (!one
->sha1_valid
) {
1999 if (!strcmp(one
->path
, "-")) {
2000 hashcpy(one
->sha1
, null_sha1
);
2003 if (lstat(one
->path
, &st
) < 0)
2004 die("stat %s", one
->path
);
2005 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2006 die("cannot hash %s\n", one
->path
);
2013 static int similarity_index(struct diff_filepair
*p
)
2015 return p
->score
* 100 / MAX_SCORE
;
2018 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2020 /* Strip the prefix but do not molest /dev/null and absolute paths */
2021 if (*namep
&& **namep
!= '/')
2022 *namep
+= prefix_length
;
2023 if (*otherp
&& **otherp
!= '/')
2024 *otherp
+= prefix_length
;
2027 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2029 const char *pgm
= external_diff();
2032 struct diff_filespec
*one
= p
->one
;
2033 struct diff_filespec
*two
= p
->two
;
2036 const char *attr_path
;
2037 int complete_rewrite
= 0;
2039 name
= p
->one
->path
;
2040 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2042 if (o
->prefix_length
)
2043 strip_prefix(o
->prefix_length
, &name
, &other
);
2045 if (DIFF_PAIR_UNMERGED(p
)) {
2046 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2047 NULL
, NULL
, NULL
, o
, 0);
2051 diff_fill_sha1_info(one
);
2052 diff_fill_sha1_info(two
);
2054 strbuf_init(&msg
, PATH_MAX
* 2 + 300);
2055 switch (p
->status
) {
2056 case DIFF_STATUS_COPIED
:
2057 strbuf_addf(&msg
, "similarity index %d%%", similarity_index(p
));
2058 strbuf_addstr(&msg
, "\ncopy from ");
2059 quote_c_style(name
, &msg
, NULL
, 0);
2060 strbuf_addstr(&msg
, "\ncopy to ");
2061 quote_c_style(other
, &msg
, NULL
, 0);
2062 strbuf_addch(&msg
, '\n');
2064 case DIFF_STATUS_RENAMED
:
2065 strbuf_addf(&msg
, "similarity index %d%%", similarity_index(p
));
2066 strbuf_addstr(&msg
, "\nrename from ");
2067 quote_c_style(name
, &msg
, NULL
, 0);
2068 strbuf_addstr(&msg
, "\nrename to ");
2069 quote_c_style(other
, &msg
, NULL
, 0);
2070 strbuf_addch(&msg
, '\n');
2072 case DIFF_STATUS_MODIFIED
:
2074 strbuf_addf(&msg
, "dissimilarity index %d%%\n",
2075 similarity_index(p
));
2076 complete_rewrite
= 1;
2085 if (hashcmp(one
->sha1
, two
->sha1
)) {
2086 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2088 if (DIFF_OPT_TST(o
, BINARY
)) {
2090 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2091 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2094 strbuf_addf(&msg
, "index %.*s..%.*s",
2095 abbrev
, sha1_to_hex(one
->sha1
),
2096 abbrev
, sha1_to_hex(two
->sha1
));
2097 if (one
->mode
== two
->mode
)
2098 strbuf_addf(&msg
, " %06o", one
->mode
);
2099 strbuf_addch(&msg
, '\n');
2103 strbuf_setlen(&msg
, msg
.len
- 1);
2104 xfrm_msg
= msg
.len
? msg
.buf
: NULL
;
2107 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2108 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2109 /* a filepair that changes between file and symlink
2110 * needs to be split into deletion and creation.
2112 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2113 run_diff_cmd(NULL
, name
, other
, attr_path
,
2114 one
, null
, xfrm_msg
, o
, 0);
2116 null
= alloc_filespec(one
->path
);
2117 run_diff_cmd(NULL
, name
, other
, attr_path
,
2118 null
, two
, xfrm_msg
, o
, 0);
2122 run_diff_cmd(pgm
, name
, other
, attr_path
,
2123 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2125 strbuf_release(&msg
);
2128 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2129 struct diffstat_t
*diffstat
)
2133 int complete_rewrite
= 0;
2135 if (DIFF_PAIR_UNMERGED(p
)) {
2137 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2141 name
= p
->one
->path
;
2142 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2144 if (o
->prefix_length
)
2145 strip_prefix(o
->prefix_length
, &name
, &other
);
2147 diff_fill_sha1_info(p
->one
);
2148 diff_fill_sha1_info(p
->two
);
2150 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2151 complete_rewrite
= 1;
2152 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2155 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2159 const char *attr_path
;
2161 if (DIFF_PAIR_UNMERGED(p
)) {
2166 name
= p
->one
->path
;
2167 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2168 attr_path
= other
? other
: name
;
2170 if (o
->prefix_length
)
2171 strip_prefix(o
->prefix_length
, &name
, &other
);
2173 diff_fill_sha1_info(p
->one
);
2174 diff_fill_sha1_info(p
->two
);
2176 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2179 void diff_setup(struct diff_options
*options
)
2181 memset(options
, 0, sizeof(*options
));
2183 options
->file
= stdout
;
2185 options
->line_termination
= '\n';
2186 options
->break_opt
= -1;
2187 options
->rename_limit
= -1;
2188 options
->dirstat_percent
= 3;
2189 options
->context
= 3;
2190 options
->msg_sep
= "";
2192 options
->change
= diff_change
;
2193 options
->add_remove
= diff_addremove
;
2194 if (diff_use_color_default
> 0)
2195 DIFF_OPT_SET(options
, COLOR_DIFF
);
2197 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2198 options
->detect_rename
= diff_detect_rename_default
;
2200 options
->a_prefix
= "a/";
2201 options
->b_prefix
= "b/";
2204 int diff_setup_done(struct diff_options
*options
)
2208 if (options
->output_format
& DIFF_FORMAT_NAME
)
2210 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2212 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2214 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2217 die("--name-only, --name-status, --check and -s are mutually exclusive");
2219 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2220 options
->detect_rename
= DIFF_DETECT_COPY
;
2222 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2223 options
->prefix
= NULL
;
2224 if (options
->prefix
)
2225 options
->prefix_length
= strlen(options
->prefix
);
2227 options
->prefix_length
= 0;
2229 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2230 DIFF_FORMAT_NAME_STATUS
|
2231 DIFF_FORMAT_CHECKDIFF
|
2232 DIFF_FORMAT_NO_OUTPUT
))
2233 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2234 DIFF_FORMAT_NUMSTAT
|
2235 DIFF_FORMAT_DIFFSTAT
|
2236 DIFF_FORMAT_SHORTSTAT
|
2237 DIFF_FORMAT_DIRSTAT
|
2238 DIFF_FORMAT_SUMMARY
|
2242 * These cases always need recursive; we do not drop caller-supplied
2243 * recursive bits for other formats here.
2245 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2246 DIFF_FORMAT_NUMSTAT
|
2247 DIFF_FORMAT_DIFFSTAT
|
2248 DIFF_FORMAT_SHORTSTAT
|
2249 DIFF_FORMAT_DIRSTAT
|
2250 DIFF_FORMAT_SUMMARY
|
2251 DIFF_FORMAT_CHECKDIFF
))
2252 DIFF_OPT_SET(options
, RECURSIVE
);
2254 * Also pickaxe would not work very well if you do not say recursive
2256 if (options
->pickaxe
)
2257 DIFF_OPT_SET(options
, RECURSIVE
);
2259 if (options
->detect_rename
&& options
->rename_limit
< 0)
2260 options
->rename_limit
= diff_rename_limit_default
;
2261 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2263 /* read-cache does not die even when it fails
2264 * so it is safe for us to do this here. Also
2265 * it does not smudge active_cache or active_nr
2266 * when it fails, so we do not have to worry about
2267 * cleaning it up ourselves either.
2271 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2272 options
->abbrev
= 40; /* full */
2275 * It does not make sense to show the first hit we happened
2276 * to have found. It does not make sense not to return with
2277 * exit code in such a case either.
2279 if (DIFF_OPT_TST(options
, QUIET
)) {
2280 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2281 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2285 * If we postprocess in diffcore, we cannot simply return
2286 * upon the first hit. We need to run diff as usual.
2288 if (options
->pickaxe
|| options
->filter
)
2289 DIFF_OPT_CLR(options
, QUIET
);
2294 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2304 if (c
== arg_short
) {
2308 if (val
&& isdigit(c
)) {
2310 int n
= strtoul(arg
, &end
, 10);
2321 eq
= strchr(arg
, '=');
2326 if (!len
|| strncmp(arg
, arg_long
, len
))
2331 if (!isdigit(*++eq
))
2333 n
= strtoul(eq
, &end
, 10);
2341 static int diff_scoreopt_parse(const char *opt
);
2343 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2345 const char *arg
= av
[0];
2347 /* Output format options */
2348 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2349 options
->output_format
|= DIFF_FORMAT_PATCH
;
2350 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2351 options
->output_format
|= DIFF_FORMAT_PATCH
;
2352 else if (!strcmp(arg
, "--raw"))
2353 options
->output_format
|= DIFF_FORMAT_RAW
;
2354 else if (!strcmp(arg
, "--patch-with-raw"))
2355 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2356 else if (!strcmp(arg
, "--numstat"))
2357 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2358 else if (!strcmp(arg
, "--shortstat"))
2359 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2360 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2361 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2362 else if (!strcmp(arg
, "--cumulative"))
2363 options
->output_format
|= DIFF_FORMAT_CUMULATIVE
;
2364 else if (!strcmp(arg
, "--check"))
2365 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2366 else if (!strcmp(arg
, "--summary"))
2367 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2368 else if (!strcmp(arg
, "--patch-with-stat"))
2369 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2370 else if (!strcmp(arg
, "--name-only"))
2371 options
->output_format
|= DIFF_FORMAT_NAME
;
2372 else if (!strcmp(arg
, "--name-status"))
2373 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2374 else if (!strcmp(arg
, "-s"))
2375 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2376 else if (!prefixcmp(arg
, "--stat")) {
2378 int width
= options
->stat_width
;
2379 int name_width
= options
->stat_name_width
;
2385 if (!prefixcmp(arg
, "-width="))
2386 width
= strtoul(arg
+ 7, &end
, 10);
2387 else if (!prefixcmp(arg
, "-name-width="))
2388 name_width
= strtoul(arg
+ 12, &end
, 10);
2391 width
= strtoul(arg
+1, &end
, 10);
2393 name_width
= strtoul(end
+1, &end
, 10);
2396 /* Important! This checks all the error cases! */
2399 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2400 options
->stat_name_width
= name_width
;
2401 options
->stat_width
= width
;
2404 /* renames options */
2405 else if (!prefixcmp(arg
, "-B")) {
2406 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2409 else if (!prefixcmp(arg
, "-M")) {
2410 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2412 options
->detect_rename
= DIFF_DETECT_RENAME
;
2414 else if (!prefixcmp(arg
, "-C")) {
2415 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2416 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2417 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2419 options
->detect_rename
= DIFF_DETECT_COPY
;
2421 else if (!strcmp(arg
, "--no-renames"))
2422 options
->detect_rename
= 0;
2423 else if (!strcmp(arg
, "--relative"))
2424 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2425 else if (!prefixcmp(arg
, "--relative=")) {
2426 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2427 options
->prefix
= arg
+ 11;
2431 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2432 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2433 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2434 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2435 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2436 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2439 else if (!strcmp(arg
, "--binary")) {
2440 options
->output_format
|= DIFF_FORMAT_PATCH
;
2441 DIFF_OPT_SET(options
, BINARY
);
2443 else if (!strcmp(arg
, "--full-index"))
2444 DIFF_OPT_SET(options
, FULL_INDEX
);
2445 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2446 DIFF_OPT_SET(options
, TEXT
);
2447 else if (!strcmp(arg
, "-R"))
2448 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2449 else if (!strcmp(arg
, "--find-copies-harder"))
2450 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2451 else if (!strcmp(arg
, "--follow"))
2452 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2453 else if (!strcmp(arg
, "--color"))
2454 DIFF_OPT_SET(options
, COLOR_DIFF
);
2455 else if (!strcmp(arg
, "--no-color"))
2456 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2457 else if (!strcmp(arg
, "--color-words"))
2458 options
->flags
|= DIFF_OPT_COLOR_DIFF
| DIFF_OPT_COLOR_DIFF_WORDS
;
2459 else if (!strcmp(arg
, "--exit-code"))
2460 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2461 else if (!strcmp(arg
, "--quiet"))
2462 DIFF_OPT_SET(options
, QUIET
);
2463 else if (!strcmp(arg
, "--ext-diff"))
2464 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2465 else if (!strcmp(arg
, "--no-ext-diff"))
2466 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2469 else if (!strcmp(arg
, "-z"))
2470 options
->line_termination
= 0;
2471 else if (!prefixcmp(arg
, "-l"))
2472 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2473 else if (!prefixcmp(arg
, "-S"))
2474 options
->pickaxe
= arg
+ 2;
2475 else if (!strcmp(arg
, "--pickaxe-all"))
2476 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2477 else if (!strcmp(arg
, "--pickaxe-regex"))
2478 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2479 else if (!prefixcmp(arg
, "-O"))
2480 options
->orderfile
= arg
+ 2;
2481 else if (!prefixcmp(arg
, "--diff-filter="))
2482 options
->filter
= arg
+ 14;
2483 else if (!strcmp(arg
, "--abbrev"))
2484 options
->abbrev
= DEFAULT_ABBREV
;
2485 else if (!prefixcmp(arg
, "--abbrev=")) {
2486 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2487 if (options
->abbrev
< MINIMUM_ABBREV
)
2488 options
->abbrev
= MINIMUM_ABBREV
;
2489 else if (40 < options
->abbrev
)
2490 options
->abbrev
= 40;
2492 else if (!prefixcmp(arg
, "--src-prefix="))
2493 options
->a_prefix
= arg
+ 13;
2494 else if (!prefixcmp(arg
, "--dst-prefix="))
2495 options
->b_prefix
= arg
+ 13;
2496 else if (!strcmp(arg
, "--no-prefix"))
2497 options
->a_prefix
= options
->b_prefix
= "";
2498 else if (!prefixcmp(arg
, "--output=")) {
2499 options
->file
= fopen(arg
+ strlen("--output="), "w");
2500 options
->close_file
= 1;
2506 static int parse_num(const char **cp_p
)
2508 unsigned long num
, scale
;
2510 const char *cp
= *cp_p
;
2517 if ( !dot
&& ch
== '.' ) {
2520 } else if ( ch
== '%' ) {
2521 scale
= dot
? scale
*100 : 100;
2522 cp
++; /* % is always at the end */
2524 } else if ( ch
>= '0' && ch
<= '9' ) {
2525 if ( scale
< 100000 ) {
2527 num
= (num
*10) + (ch
-'0');
2536 /* user says num divided by scale and we say internally that
2537 * is MAX_SCORE * num / scale.
2539 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2542 static int diff_scoreopt_parse(const char *opt
)
2544 int opt1
, opt2
, cmd
;
2549 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2550 return -1; /* that is not a -M, -C nor -B option */
2552 opt1
= parse_num(&opt
);
2558 else if (*opt
!= '/')
2559 return -1; /* we expect -B80/99 or -B80 */
2562 opt2
= parse_num(&opt
);
2567 return opt1
| (opt2
<< 16);
2570 struct diff_queue_struct diff_queued_diff
;
2572 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2574 if (queue
->alloc
<= queue
->nr
) {
2575 queue
->alloc
= alloc_nr(queue
->alloc
);
2576 queue
->queue
= xrealloc(queue
->queue
,
2577 sizeof(dp
) * queue
->alloc
);
2579 queue
->queue
[queue
->nr
++] = dp
;
2582 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2583 struct diff_filespec
*one
,
2584 struct diff_filespec
*two
)
2586 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2594 void diff_free_filepair(struct diff_filepair
*p
)
2596 free_filespec(p
->one
);
2597 free_filespec(p
->two
);
2601 /* This is different from find_unique_abbrev() in that
2602 * it stuffs the result with dots for alignment.
2604 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2609 return sha1_to_hex(sha1
);
2611 abbrev
= find_unique_abbrev(sha1
, len
);
2612 abblen
= strlen(abbrev
);
2614 static char hex
[41];
2615 if (len
< abblen
&& abblen
<= len
+ 2)
2616 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2618 sprintf(hex
, "%s...", abbrev
);
2621 return sha1_to_hex(sha1
);
2624 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
2626 int line_termination
= opt
->line_termination
;
2627 int inter_name_termination
= line_termination
? '\t' : '\0';
2629 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2630 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
2631 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
2632 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
2635 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
2636 inter_name_termination
);
2638 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
2641 if (p
->status
== DIFF_STATUS_COPIED
||
2642 p
->status
== DIFF_STATUS_RENAMED
) {
2643 const char *name_a
, *name_b
;
2644 name_a
= p
->one
->path
;
2645 name_b
= p
->two
->path
;
2646 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2647 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
2648 write_name_quoted(name_b
, opt
->file
, line_termination
);
2650 const char *name_a
, *name_b
;
2651 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
2653 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2654 write_name_quoted(name_a
, opt
->file
, line_termination
);
2658 int diff_unmodified_pair(struct diff_filepair
*p
)
2660 /* This function is written stricter than necessary to support
2661 * the currently implemented transformers, but the idea is to
2662 * let transformers to produce diff_filepairs any way they want,
2663 * and filter and clean them up here before producing the output.
2665 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
2667 if (DIFF_PAIR_UNMERGED(p
))
2668 return 0; /* unmerged is interesting */
2670 /* deletion, addition, mode or type change
2671 * and rename are all interesting.
2673 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2674 DIFF_PAIR_MODE_CHANGED(p
) ||
2675 strcmp(one
->path
, two
->path
))
2678 /* both are valid and point at the same path. that is, we are
2679 * dealing with a change.
2681 if (one
->sha1_valid
&& two
->sha1_valid
&&
2682 !hashcmp(one
->sha1
, two
->sha1
))
2683 return 1; /* no change */
2684 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2685 return 1; /* both look at the same file on the filesystem. */
2689 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2691 if (diff_unmodified_pair(p
))
2694 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2695 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2696 return; /* no tree diffs in patch format */
2701 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2702 struct diffstat_t
*diffstat
)
2704 if (diff_unmodified_pair(p
))
2707 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2708 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2709 return; /* no tree diffs in patch format */
2711 run_diffstat(p
, o
, diffstat
);
2714 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2715 struct diff_options
*o
)
2717 if (diff_unmodified_pair(p
))
2720 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2721 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2722 return; /* no tree diffs in patch format */
2724 run_checkdiff(p
, o
);
2727 int diff_queue_is_empty(void)
2729 struct diff_queue_struct
*q
= &diff_queued_diff
;
2731 for (i
= 0; i
< q
->nr
; i
++)
2732 if (!diff_unmodified_pair(q
->queue
[i
]))
2738 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2740 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2743 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2745 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2746 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2748 s
->size
, s
->xfrm_flags
);
2751 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2753 diff_debug_filespec(p
->one
, i
, "one");
2754 diff_debug_filespec(p
->two
, i
, "two");
2755 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
2756 p
->score
, p
->status
? p
->status
: '?',
2757 p
->one
->rename_used
, p
->broken_pair
);
2760 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2764 fprintf(stderr
, "%s\n", msg
);
2765 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2766 for (i
= 0; i
< q
->nr
; i
++) {
2767 struct diff_filepair
*p
= q
->queue
[i
];
2768 diff_debug_filepair(p
, i
);
2773 static void diff_resolve_rename_copy(void)
2776 struct diff_filepair
*p
;
2777 struct diff_queue_struct
*q
= &diff_queued_diff
;
2779 diff_debug_queue("resolve-rename-copy", q
);
2781 for (i
= 0; i
< q
->nr
; i
++) {
2783 p
->status
= 0; /* undecided */
2784 if (DIFF_PAIR_UNMERGED(p
))
2785 p
->status
= DIFF_STATUS_UNMERGED
;
2786 else if (!DIFF_FILE_VALID(p
->one
))
2787 p
->status
= DIFF_STATUS_ADDED
;
2788 else if (!DIFF_FILE_VALID(p
->two
))
2789 p
->status
= DIFF_STATUS_DELETED
;
2790 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2791 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2793 /* from this point on, we are dealing with a pair
2794 * whose both sides are valid and of the same type, i.e.
2795 * either in-place edit or rename/copy edit.
2797 else if (DIFF_PAIR_RENAME(p
)) {
2799 * A rename might have re-connected a broken
2800 * pair up, causing the pathnames to be the
2801 * same again. If so, that's not a rename at
2802 * all, just a modification..
2804 * Otherwise, see if this source was used for
2805 * multiple renames, in which case we decrement
2806 * the count, and call it a copy.
2808 if (!strcmp(p
->one
->path
, p
->two
->path
))
2809 p
->status
= DIFF_STATUS_MODIFIED
;
2810 else if (--p
->one
->rename_used
> 0)
2811 p
->status
= DIFF_STATUS_COPIED
;
2813 p
->status
= DIFF_STATUS_RENAMED
;
2815 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2816 p
->one
->mode
!= p
->two
->mode
||
2817 is_null_sha1(p
->one
->sha1
))
2818 p
->status
= DIFF_STATUS_MODIFIED
;
2820 /* This is a "no-change" entry and should not
2821 * happen anymore, but prepare for broken callers.
2823 error("feeding unmodified %s to diffcore",
2825 p
->status
= DIFF_STATUS_UNKNOWN
;
2828 diff_debug_queue("resolve-rename-copy done", q
);
2831 static int check_pair_status(struct diff_filepair
*p
)
2833 switch (p
->status
) {
2834 case DIFF_STATUS_UNKNOWN
:
2837 die("internal error in diff-resolve-rename-copy");
2843 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2845 int fmt
= opt
->output_format
;
2847 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2848 diff_flush_checkdiff(p
, opt
);
2849 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2850 diff_flush_raw(p
, opt
);
2851 else if (fmt
& DIFF_FORMAT_NAME
) {
2852 const char *name_a
, *name_b
;
2853 name_a
= p
->two
->path
;
2855 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2856 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
2860 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
2863 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
2865 fprintf(file
, " %s ", newdelete
);
2866 write_name_quoted(fs
->path
, file
, '\n');
2870 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
2872 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
2873 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
2874 show_name
? ' ' : '\n');
2876 write_name_quoted(p
->two
->path
, file
, '\n');
2881 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
2883 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
2885 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
2887 show_mode_change(file
, p
, 0);
2890 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
2893 case DIFF_STATUS_DELETED
:
2894 show_file_mode_name(file
, "delete", p
->one
);
2896 case DIFF_STATUS_ADDED
:
2897 show_file_mode_name(file
, "create", p
->two
);
2899 case DIFF_STATUS_COPIED
:
2900 show_rename_copy(file
, "copy", p
);
2902 case DIFF_STATUS_RENAMED
:
2903 show_rename_copy(file
, "rename", p
);
2907 fputs(" rewrite ", file
);
2908 write_name_quoted(p
->two
->path
, file
, ' ');
2909 fprintf(file
, "(%d%%)\n", similarity_index(p
));
2911 show_mode_change(file
, p
, !p
->score
);
2917 struct xdiff_emit_state xm
;
2922 static int remove_space(char *line
, int len
)
2928 for (i
= 0; i
< len
; i
++)
2929 if (!isspace((c
= line
[i
])))
2935 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
2937 struct patch_id_t
*data
= priv
;
2940 /* Ignore line numbers when computing the SHA1 of the patch */
2941 if (!prefixcmp(line
, "@@ -"))
2944 new_len
= remove_space(line
, len
);
2946 SHA1_Update(data
->ctx
, line
, new_len
);
2947 data
->patchlen
+= new_len
;
2950 /* returns 0 upon success, and writes result into sha1 */
2951 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
2953 struct diff_queue_struct
*q
= &diff_queued_diff
;
2956 struct patch_id_t data
;
2957 char buffer
[PATH_MAX
* 4 + 20];
2960 memset(&data
, 0, sizeof(struct patch_id_t
));
2962 data
.xm
.consume
= patch_id_consume
;
2964 for (i
= 0; i
< q
->nr
; i
++) {
2969 struct diff_filepair
*p
= q
->queue
[i
];
2972 memset(&xecfg
, 0, sizeof(xecfg
));
2974 return error("internal diff status error");
2975 if (p
->status
== DIFF_STATUS_UNKNOWN
)
2977 if (diff_unmodified_pair(p
))
2979 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2980 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2982 if (DIFF_PAIR_UNMERGED(p
))
2985 diff_fill_sha1_info(p
->one
);
2986 diff_fill_sha1_info(p
->two
);
2987 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
2988 fill_mmfile(&mf2
, p
->two
) < 0)
2989 return error("unable to read files to diff");
2991 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
2992 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
2993 if (p
->one
->mode
== 0)
2994 len1
= snprintf(buffer
, sizeof(buffer
),
2995 "diff--gita/%.*sb/%.*s"
3002 len2
, p
->two
->path
);
3003 else if (p
->two
->mode
== 0)
3004 len1
= snprintf(buffer
, sizeof(buffer
),
3005 "diff--gita/%.*sb/%.*s"
3006 "deletedfilemode%06o"
3012 len1
, p
->one
->path
);
3014 len1
= snprintf(buffer
, sizeof(buffer
),
3015 "diff--gita/%.*sb/%.*s"
3021 len2
, p
->two
->path
);
3022 SHA1_Update(&ctx
, buffer
, len1
);
3024 xpp
.flags
= XDF_NEED_MINIMAL
;
3026 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3027 ecb
.outf
= xdiff_outf
;
3029 xdi_diff(&mf1
, &mf2
, &xpp
, &xecfg
, &ecb
);
3032 SHA1_Final(sha1
, &ctx
);
3036 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3038 struct diff_queue_struct
*q
= &diff_queued_diff
;
3040 int result
= diff_get_patch_id(options
, sha1
);
3042 for (i
= 0; i
< q
->nr
; i
++)
3043 diff_free_filepair(q
->queue
[i
]);
3047 q
->nr
= q
->alloc
= 0;
3052 static int is_summary_empty(const struct diff_queue_struct
*q
)
3056 for (i
= 0; i
< q
->nr
; i
++) {
3057 const struct diff_filepair
*p
= q
->queue
[i
];
3059 switch (p
->status
) {
3060 case DIFF_STATUS_DELETED
:
3061 case DIFF_STATUS_ADDED
:
3062 case DIFF_STATUS_COPIED
:
3063 case DIFF_STATUS_RENAMED
:
3068 if (p
->one
->mode
&& p
->two
->mode
&&
3069 p
->one
->mode
!= p
->two
->mode
)
3077 void diff_flush(struct diff_options
*options
)
3079 struct diff_queue_struct
*q
= &diff_queued_diff
;
3080 int i
, output_format
= options
->output_format
;
3084 * Order: raw, stat, summary, patch
3085 * or: name/name-status/checkdiff (other bits clear)
3090 if (output_format
& (DIFF_FORMAT_RAW
|
3092 DIFF_FORMAT_NAME_STATUS
|
3093 DIFF_FORMAT_CHECKDIFF
)) {
3094 for (i
= 0; i
< q
->nr
; i
++) {
3095 struct diff_filepair
*p
= q
->queue
[i
];
3096 if (check_pair_status(p
))
3097 flush_one_pair(p
, options
);
3102 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIRSTAT
)) {
3103 struct diffstat_t diffstat
;
3105 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3106 diffstat
.xm
.consume
= diffstat_consume
;
3107 for (i
= 0; i
< q
->nr
; i
++) {
3108 struct diff_filepair
*p
= q
->queue
[i
];
3109 if (check_pair_status(p
))
3110 diff_flush_stat(p
, options
, &diffstat
);
3112 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3113 show_dirstat(&diffstat
, options
);
3114 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3115 show_numstat(&diffstat
, options
);
3116 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3117 show_stats(&diffstat
, options
);
3118 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3119 show_shortstats(&diffstat
, options
);
3120 free_diffstat_info(&diffstat
);
3124 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3125 for (i
= 0; i
< q
->nr
; i
++)
3126 diff_summary(options
->file
, q
->queue
[i
]);
3130 if (output_format
& DIFF_FORMAT_PATCH
) {
3132 if (options
->stat_sep
) {
3133 /* attach patch instead of inline */
3134 fputs(options
->stat_sep
, options
->file
);
3136 putc(options
->line_termination
, options
->file
);
3140 for (i
= 0; i
< q
->nr
; i
++) {
3141 struct diff_filepair
*p
= q
->queue
[i
];
3142 if (check_pair_status(p
))
3143 diff_flush_patch(p
, options
);
3147 if (output_format
& DIFF_FORMAT_CALLBACK
)
3148 options
->format_callback(q
, options
, options
->format_callback_data
);
3150 for (i
= 0; i
< q
->nr
; i
++)
3151 diff_free_filepair(q
->queue
[i
]);
3155 q
->nr
= q
->alloc
= 0;
3156 if (options
->close_file
)
3157 fclose(options
->file
);
3160 static void diffcore_apply_filter(const char *filter
)
3163 struct diff_queue_struct
*q
= &diff_queued_diff
;
3164 struct diff_queue_struct outq
;
3166 outq
.nr
= outq
.alloc
= 0;
3171 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3173 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3174 struct diff_filepair
*p
= q
->queue
[i
];
3175 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3177 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3179 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3180 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3181 strchr(filter
, p
->status
)))
3187 /* otherwise we will clear the whole queue
3188 * by copying the empty outq at the end of this
3189 * function, but first clear the current entries
3192 for (i
= 0; i
< q
->nr
; i
++)
3193 diff_free_filepair(q
->queue
[i
]);
3196 /* Only the matching ones */
3197 for (i
= 0; i
< q
->nr
; i
++) {
3198 struct diff_filepair
*p
= q
->queue
[i
];
3200 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3202 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3204 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3205 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3206 strchr(filter
, p
->status
)))
3209 diff_free_filepair(p
);
3216 /* Check whether two filespecs with the same mode and size are identical */
3217 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3218 struct diff_filespec
*two
)
3220 if (S_ISGITLINK(one
->mode
))
3222 if (diff_populate_filespec(one
, 0))
3224 if (diff_populate_filespec(two
, 0))
3226 return !memcmp(one
->data
, two
->data
, one
->size
);
3229 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3232 struct diff_queue_struct
*q
= &diff_queued_diff
;
3233 struct diff_queue_struct outq
;
3235 outq
.nr
= outq
.alloc
= 0;
3237 for (i
= 0; i
< q
->nr
; i
++) {
3238 struct diff_filepair
*p
= q
->queue
[i
];
3241 * 1. Entries that come from stat info dirtyness
3242 * always have both sides (iow, not create/delete),
3243 * one side of the object name is unknown, with
3244 * the same mode and size. Keep the ones that
3245 * do not match these criteria. They have real
3248 * 2. At this point, the file is known to be modified,
3249 * with the same mode and size, and the object
3250 * name of one side is unknown. Need to inspect
3251 * the identical contents.
3253 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3254 !DIFF_FILE_VALID(p
->two
) ||
3255 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3256 (p
->one
->mode
!= p
->two
->mode
) ||
3257 diff_populate_filespec(p
->one
, 1) ||
3258 diff_populate_filespec(p
->two
, 1) ||
3259 (p
->one
->size
!= p
->two
->size
) ||
3260 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3264 * The caller can subtract 1 from skip_stat_unmatch
3265 * to determine how many paths were dirty only
3266 * due to stat info mismatch.
3268 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3269 diffopt
->skip_stat_unmatch
++;
3270 diff_free_filepair(p
);
3277 void diffcore_std(struct diff_options
*options
)
3279 if (DIFF_OPT_TST(options
, QUIET
))
3282 if (options
->skip_stat_unmatch
&& !DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
3283 diffcore_skip_stat_unmatch(options
);
3284 if (options
->break_opt
!= -1)
3285 diffcore_break(options
->break_opt
);
3286 if (options
->detect_rename
)
3287 diffcore_rename(options
);
3288 if (options
->break_opt
!= -1)
3289 diffcore_merge_broken();
3290 if (options
->pickaxe
)
3291 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3292 if (options
->orderfile
)
3293 diffcore_order(options
->orderfile
);
3294 diff_resolve_rename_copy();
3295 diffcore_apply_filter(options
->filter
);
3297 if (diff_queued_diff
.nr
)
3298 DIFF_OPT_SET(options
, HAS_CHANGES
);
3300 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3303 int diff_result_code(struct diff_options
*opt
, int status
)
3306 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3307 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3309 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3310 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3312 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3313 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3318 void diff_addremove(struct diff_options
*options
,
3319 int addremove
, unsigned mode
,
3320 const unsigned char *sha1
,
3321 const char *base
, const char *path
)
3323 char concatpath
[PATH_MAX
];
3324 struct diff_filespec
*one
, *two
;
3326 /* This may look odd, but it is a preparation for
3327 * feeding "there are unchanged files which should
3328 * not produce diffs, but when you are doing copy
3329 * detection you would need them, so here they are"
3330 * entries to the diff-core. They will be prefixed
3331 * with something like '=' or '*' (I haven't decided
3332 * which but should not make any difference).
3333 * Feeding the same new and old to diff_change()
3334 * also has the same effect.
3335 * Before the final output happens, they are pruned after
3336 * merged into rename/copy pairs as appropriate.
3338 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3339 addremove
= (addremove
== '+' ? '-' :
3340 addremove
== '-' ? '+' : addremove
);
3342 if (!path
) path
= "";
3343 sprintf(concatpath
, "%s%s", base
, path
);
3345 if (options
->prefix
&&
3346 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3349 one
= alloc_filespec(concatpath
);
3350 two
= alloc_filespec(concatpath
);
3352 if (addremove
!= '+')
3353 fill_filespec(one
, sha1
, mode
);
3354 if (addremove
!= '-')
3355 fill_filespec(two
, sha1
, mode
);
3357 diff_queue(&diff_queued_diff
, one
, two
);
3358 DIFF_OPT_SET(options
, HAS_CHANGES
);
3361 void diff_change(struct diff_options
*options
,
3362 unsigned old_mode
, unsigned new_mode
,
3363 const unsigned char *old_sha1
,
3364 const unsigned char *new_sha1
,
3365 const char *base
, const char *path
)
3367 char concatpath
[PATH_MAX
];
3368 struct diff_filespec
*one
, *two
;
3370 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3372 const unsigned char *tmp_c
;
3373 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3374 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3376 if (!path
) path
= "";
3377 sprintf(concatpath
, "%s%s", base
, path
);
3379 if (options
->prefix
&&
3380 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3383 one
= alloc_filespec(concatpath
);
3384 two
= alloc_filespec(concatpath
);
3385 fill_filespec(one
, old_sha1
, old_mode
);
3386 fill_filespec(two
, new_sha1
, new_mode
);
3388 diff_queue(&diff_queued_diff
, one
, two
);
3389 DIFF_OPT_SET(options
, HAS_CHANGES
);
3392 void diff_unmerge(struct diff_options
*options
,
3394 unsigned mode
, const unsigned char *sha1
)
3396 struct diff_filespec
*one
, *two
;
3398 if (options
->prefix
&&
3399 strncmp(path
, options
->prefix
, options
->prefix_length
))
3402 one
= alloc_filespec(path
);
3403 two
= alloc_filespec(path
);
3404 fill_filespec(one
, sha1
, mode
);
3405 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;