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 static int diff_suppress_blank_empty
;
24 int diff_use_color_default
= -1;
25 static const char *external_diff_cmd_cfg
;
26 int diff_auto_refresh_index
= 1;
28 static char diff_colors
[][COLOR_MAXLEN
] = {
30 "", /* PLAIN (normal) */
31 "\033[1m", /* METAINFO (bold) */
32 "\033[36m", /* FRAGINFO (cyan) */
33 "\033[31m", /* OLD (red) */
34 "\033[32m", /* NEW (green) */
35 "\033[33m", /* COMMIT (yellow) */
36 "\033[41m", /* WHITESPACE (red background) */
39 static int parse_diff_color_slot(const char *var
, int ofs
)
41 if (!strcasecmp(var
+ofs
, "plain"))
43 if (!strcasecmp(var
+ofs
, "meta"))
45 if (!strcasecmp(var
+ofs
, "frag"))
47 if (!strcasecmp(var
+ofs
, "old"))
49 if (!strcasecmp(var
+ofs
, "new"))
51 if (!strcasecmp(var
+ofs
, "commit"))
53 if (!strcasecmp(var
+ofs
, "whitespace"))
54 return DIFF_WHITESPACE
;
55 die("bad config variable '%s'", var
);
58 static struct ll_diff_driver
{
60 struct ll_diff_driver
*next
;
62 } *user_diff
, **user_diff_tail
;
65 * Currently there is only "diff.<drivername>.command" variable;
66 * because there are "diff.color.<slot>" variables, we are parsing
67 * this in a bit convoluted way to allow low level diff driver
70 static int parse_lldiff_command(const char *var
, const char *ep
, const char *value
)
74 struct ll_diff_driver
*drv
;
78 for (drv
= user_diff
; drv
; drv
= drv
->next
)
79 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
82 drv
= xcalloc(1, sizeof(struct ll_diff_driver
));
83 drv
->name
= xmemdupz(name
, namelen
);
85 user_diff_tail
= &user_diff
;
86 *user_diff_tail
= drv
;
87 user_diff_tail
= &(drv
->next
);
90 return git_config_string(&(drv
->cmd
), var
, value
);
94 * 'diff.<what>.funcname' attribute can be specified in the configuration
95 * to define a customized regexp to find the beginning of a function to
96 * be used for hunk header lines of "diff -p" style output.
98 struct funcname_pattern_entry
{
103 static struct funcname_pattern_list
{
104 struct funcname_pattern_list
*next
;
105 struct funcname_pattern_entry e
;
106 } *funcname_pattern_list
;
108 static int parse_funcname_pattern(const char *var
, const char *ep
, const char *value
, int cflags
)
112 struct funcname_pattern_list
*pp
;
114 name
= var
+ 5; /* "diff." */
117 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
118 if (!strncmp(pp
->e
.name
, name
, namelen
) && !pp
->e
.name
[namelen
])
121 pp
= xcalloc(1, sizeof(*pp
));
122 pp
->e
.name
= xmemdupz(name
, namelen
);
123 pp
->next
= funcname_pattern_list
;
124 funcname_pattern_list
= pp
;
127 pp
->e
.pattern
= xstrdup(value
);
128 pp
->e
.cflags
= cflags
;
133 * These are to give UI layer defaults.
134 * The core-level commands such as git-diff-files should
135 * never be affected by the setting of diff.renames
136 * the user happens to have in the configuration file.
138 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
140 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
141 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
144 if (!strcmp(var
, "diff.renames")) {
146 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
147 else if (!strcasecmp(value
, "copies") ||
148 !strcasecmp(value
, "copy"))
149 diff_detect_rename_default
= DIFF_DETECT_COPY
;
150 else if (git_config_bool(var
,value
))
151 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
154 if (!strcmp(var
, "diff.autorefreshindex")) {
155 diff_auto_refresh_index
= git_config_bool(var
, value
);
158 if (!strcmp(var
, "diff.external"))
159 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
160 if (!prefixcmp(var
, "diff.")) {
161 const char *ep
= strrchr(var
, '.');
163 if (ep
!= var
+ 4 && !strcmp(ep
, ".command"))
164 return parse_lldiff_command(var
, ep
, value
);
167 return git_diff_basic_config(var
, value
, cb
);
170 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
172 if (!strcmp(var
, "diff.renamelimit")) {
173 diff_rename_limit_default
= git_config_int(var
, value
);
177 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
178 int slot
= parse_diff_color_slot(var
, 11);
180 return config_error_nonbool(var
);
181 color_parse(value
, var
, diff_colors
[slot
]);
185 /* like GNU diff's --suppress-blank-empty option */
186 if (!strcmp(var
, "diff.suppress-blank-empty")) {
187 diff_suppress_blank_empty
= git_config_bool(var
, value
);
191 if (!prefixcmp(var
, "diff.")) {
192 const char *ep
= strrchr(var
, '.');
194 if (!strcmp(ep
, ".funcname")) {
196 return config_error_nonbool(var
);
197 return parse_funcname_pattern(var
, ep
, value
,
199 } else if (!strcmp(ep
, ".xfuncname")) {
201 return config_error_nonbool(var
);
202 return parse_funcname_pattern(var
, ep
, value
,
208 return git_color_default_config(var
, value
, cb
);
211 static char *quote_two(const char *one
, const char *two
)
213 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
214 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
217 strbuf_init(&res
, 0);
218 if (need_one
+ need_two
) {
219 strbuf_addch(&res
, '"');
220 quote_c_style(one
, &res
, NULL
, 1);
221 quote_c_style(two
, &res
, NULL
, 1);
222 strbuf_addch(&res
, '"');
224 strbuf_addstr(&res
, one
);
225 strbuf_addstr(&res
, two
);
227 return strbuf_detach(&res
, NULL
);
230 static const char *external_diff(void)
232 static const char *external_diff_cmd
= NULL
;
233 static int done_preparing
= 0;
236 return external_diff_cmd
;
237 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
238 if (!external_diff_cmd
)
239 external_diff_cmd
= external_diff_cmd_cfg
;
241 return external_diff_cmd
;
244 static struct diff_tempfile
{
245 const char *name
; /* filename external diff should read from */
248 char tmp_path
[PATH_MAX
];
251 static int count_lines(const char *data
, int size
)
253 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
260 completely_empty
= 0;
264 completely_empty
= 0;
267 if (completely_empty
)
270 count
++; /* no trailing newline */
274 static void print_line_count(FILE *file
, int count
)
278 fprintf(file
, "0,0");
284 fprintf(file
, "1,%d", count
);
289 static void copy_file_with_prefix(FILE *file
,
290 int prefix
, const char *data
, int size
,
291 const char *set
, const char *reset
)
293 int ch
, nl_just_seen
= 1;
308 fprintf(file
, "%s\n\\ No newline at end of file\n", reset
);
311 static void emit_rewrite_diff(const char *name_a
,
313 struct diff_filespec
*one
,
314 struct diff_filespec
*two
,
315 struct diff_options
*o
)
318 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
319 const char *name_a_tab
, *name_b_tab
;
320 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
321 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
322 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
323 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
324 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
325 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
327 name_a
+= (*name_a
== '/');
328 name_b
+= (*name_b
== '/');
329 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
330 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
332 strbuf_reset(&a_name
);
333 strbuf_reset(&b_name
);
334 quote_two_c_style(&a_name
, o
->a_prefix
, name_a
, 0);
335 quote_two_c_style(&b_name
, o
->b_prefix
, name_b
, 0);
337 diff_populate_filespec(one
, 0);
338 diff_populate_filespec(two
, 0);
339 lc_a
= count_lines(one
->data
, one
->size
);
340 lc_b
= count_lines(two
->data
, two
->size
);
342 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
343 metainfo
, a_name
.buf
, name_a_tab
, reset
,
344 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
345 print_line_count(o
->file
, lc_a
);
346 fprintf(o
->file
, " +");
347 print_line_count(o
->file
, lc_b
);
348 fprintf(o
->file
, " @@%s\n", reset
);
350 copy_file_with_prefix(o
->file
, '-', one
->data
, one
->size
, old
, reset
);
352 copy_file_with_prefix(o
->file
, '+', two
->data
, two
->size
, new, reset
);
355 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
357 if (!DIFF_FILE_VALID(one
)) {
358 mf
->ptr
= (char *)""; /* does not matter */
362 else if (diff_populate_filespec(one
, 0))
365 mf
->size
= one
->size
;
369 struct diff_words_buffer
{
372 long current
; /* output pointer */
373 int suppressed_newline
;
376 static void diff_words_append(char *line
, unsigned long len
,
377 struct diff_words_buffer
*buffer
)
379 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
380 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
381 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
385 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
386 buffer
->text
.size
+= len
;
389 struct diff_words_data
{
390 struct diff_words_buffer minus
, plus
;
394 static void print_word(FILE *file
, struct diff_words_buffer
*buffer
, int len
, int color
,
395 int suppress_newline
)
403 ptr
= buffer
->text
.ptr
+ buffer
->current
;
404 buffer
->current
+= len
;
406 if (ptr
[len
- 1] == '\n') {
411 fputs(diff_get_color(1, color
), file
);
412 fwrite(ptr
, len
, 1, file
);
413 fputs(diff_get_color(1, DIFF_RESET
), file
);
416 if (suppress_newline
)
417 buffer
->suppressed_newline
= 1;
423 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
425 struct diff_words_data
*diff_words
= priv
;
427 if (diff_words
->minus
.suppressed_newline
) {
429 putc('\n', diff_words
->file
);
430 diff_words
->minus
.suppressed_newline
= 0;
436 print_word(diff_words
->file
,
437 &diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
440 print_word(diff_words
->file
,
441 &diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
444 print_word(diff_words
->file
,
445 &diff_words
->plus
, len
, DIFF_PLAIN
, 0);
446 diff_words
->minus
.current
+= len
;
451 /* this executes the word diff on the accumulated buffers */
452 static void diff_words_show(struct diff_words_data
*diff_words
)
457 mmfile_t minus
, plus
;
460 memset(&xecfg
, 0, sizeof(xecfg
));
461 minus
.size
= diff_words
->minus
.text
.size
;
462 minus
.ptr
= xmalloc(minus
.size
);
463 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
464 for (i
= 0; i
< minus
.size
; i
++)
465 if (isspace(minus
.ptr
[i
]))
467 diff_words
->minus
.current
= 0;
469 plus
.size
= diff_words
->plus
.text
.size
;
470 plus
.ptr
= xmalloc(plus
.size
);
471 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
472 for (i
= 0; i
< plus
.size
; i
++)
473 if (isspace(plus
.ptr
[i
]))
475 diff_words
->plus
.current
= 0;
477 xpp
.flags
= XDF_NEED_MINIMAL
;
478 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
479 xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
483 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
485 if (diff_words
->minus
.suppressed_newline
) {
486 putc('\n', diff_words
->file
);
487 diff_words
->minus
.suppressed_newline
= 0;
491 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
493 struct emit_callback
{
494 int nparents
, color_diff
;
496 sane_truncate_fn truncate
;
497 const char **label_path
;
498 struct diff_words_data
*diff_words
;
503 static void free_diff_words_data(struct emit_callback
*ecbdata
)
505 if (ecbdata
->diff_words
) {
507 if (ecbdata
->diff_words
->minus
.text
.size
||
508 ecbdata
->diff_words
->plus
.text
.size
)
509 diff_words_show(ecbdata
->diff_words
);
511 free (ecbdata
->diff_words
->minus
.text
.ptr
);
512 free (ecbdata
->diff_words
->plus
.text
.ptr
);
513 free(ecbdata
->diff_words
);
514 ecbdata
->diff_words
= NULL
;
518 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
521 return diff_colors
[ix
];
525 static void emit_line(FILE *file
, const char *set
, const char *reset
, const char *line
, int len
)
527 int has_trailing_newline
, has_trailing_carriage_return
;
529 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
530 if (has_trailing_newline
)
532 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
533 if (has_trailing_carriage_return
)
537 fwrite(line
, len
, 1, file
);
539 if (has_trailing_carriage_return
)
541 if (has_trailing_newline
)
545 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
547 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
548 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
551 emit_line(ecbdata
->file
, set
, reset
, line
, len
);
553 /* Emit just the prefix, then the rest. */
554 emit_line(ecbdata
->file
, set
, reset
, line
, ecbdata
->nparents
);
555 ws_check_emit(line
+ ecbdata
->nparents
,
556 len
- ecbdata
->nparents
, ecbdata
->ws_rule
,
557 ecbdata
->file
, set
, reset
, ws
);
561 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
568 return ecb
->truncate(line
, len
);
572 (void) utf8_width(&cp
, &l
);
574 break; /* truncated in the middle? */
579 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
583 struct emit_callback
*ecbdata
= priv
;
584 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
585 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
586 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
588 *(ecbdata
->found_changesp
) = 1;
590 if (ecbdata
->label_path
[0]) {
591 const char *name_a_tab
, *name_b_tab
;
593 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
594 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
596 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
597 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
598 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
599 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
600 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
603 if (diff_suppress_blank_empty
604 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
609 /* This is not really necessary for now because
610 * this codepath only deals with two-way diffs.
612 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
614 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
615 ecbdata
->nparents
= i
- 1;
616 len
= sane_truncate_line(ecbdata
, line
, len
);
617 emit_line(ecbdata
->file
,
618 diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
620 if (line
[len
-1] != '\n')
621 putc('\n', ecbdata
->file
);
625 if (len
< ecbdata
->nparents
) {
626 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
631 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
632 /* fall back to normal diff */
633 free_diff_words_data(ecbdata
);
634 if (ecbdata
->diff_words
) {
635 if (line
[0] == '-') {
636 diff_words_append(line
, len
,
637 &ecbdata
->diff_words
->minus
);
639 } else if (line
[0] == '+') {
640 diff_words_append(line
, len
,
641 &ecbdata
->diff_words
->plus
);
644 if (ecbdata
->diff_words
->minus
.text
.size
||
645 ecbdata
->diff_words
->plus
.text
.size
)
646 diff_words_show(ecbdata
->diff_words
);
649 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
652 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
654 color
= DIFF_FILE_OLD
;
655 else if (line
[i
] == '+')
656 color
= DIFF_FILE_NEW
;
659 if (color
!= DIFF_FILE_NEW
) {
660 emit_line(ecbdata
->file
,
661 diff_get_color(ecbdata
->color_diff
, color
),
665 emit_add_line(reset
, ecbdata
, line
, len
);
668 static char *pprint_rename(const char *a
, const char *b
)
673 int pfx_length
, sfx_length
;
674 int len_a
= strlen(a
);
675 int len_b
= strlen(b
);
676 int a_midlen
, b_midlen
;
677 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
678 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
680 strbuf_init(&name
, 0);
681 if (qlen_a
|| qlen_b
) {
682 quote_c_style(a
, &name
, NULL
, 0);
683 strbuf_addstr(&name
, " => ");
684 quote_c_style(b
, &name
, NULL
, 0);
685 return strbuf_detach(&name
, NULL
);
688 /* Find common prefix */
690 while (*old
&& *new && *old
== *new) {
692 pfx_length
= old
- a
+ 1;
697 /* Find common suffix */
701 while (a
<= old
&& b
<= new && *old
== *new) {
703 sfx_length
= len_a
- (old
- a
);
709 * pfx{mid-a => mid-b}sfx
710 * {pfx-a => pfx-b}sfx
711 * pfx{sfx-a => sfx-b}
714 a_midlen
= len_a
- pfx_length
- sfx_length
;
715 b_midlen
= len_b
- pfx_length
- sfx_length
;
721 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
722 if (pfx_length
+ sfx_length
) {
723 strbuf_add(&name
, a
, pfx_length
);
724 strbuf_addch(&name
, '{');
726 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
727 strbuf_addstr(&name
, " => ");
728 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
729 if (pfx_length
+ sfx_length
) {
730 strbuf_addch(&name
, '}');
731 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
733 return strbuf_detach(&name
, NULL
);
739 struct diffstat_file
{
743 unsigned is_unmerged
:1;
744 unsigned is_binary
:1;
745 unsigned is_renamed
:1;
746 unsigned int added
, deleted
;
750 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
754 struct diffstat_file
*x
;
755 x
= xcalloc(sizeof (*x
), 1);
756 if (diffstat
->nr
== diffstat
->alloc
) {
757 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
758 diffstat
->files
= xrealloc(diffstat
->files
,
759 diffstat
->alloc
* sizeof(x
));
761 diffstat
->files
[diffstat
->nr
++] = x
;
763 x
->from_name
= xstrdup(name_a
);
764 x
->name
= xstrdup(name_b
);
769 x
->name
= xstrdup(name_a
);
774 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
776 struct diffstat_t
*diffstat
= priv
;
777 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
781 else if (line
[0] == '-')
785 const char mime_boundary_leader
[] = "------------";
787 static int scale_linear(int it
, int width
, int max_change
)
790 * make sure that at least one '-' is printed if there were deletions,
791 * and likewise for '+'.
795 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
798 static void show_name(FILE *file
,
799 const char *prefix
, const char *name
, int len
,
800 const char *reset
, const char *set
)
802 fprintf(file
, " %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
805 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
809 fprintf(file
, "%s", set
);
812 fprintf(file
, "%s", reset
);
815 static void fill_print_name(struct diffstat_file
*file
)
819 if (file
->print_name
)
822 if (!file
->is_renamed
) {
824 strbuf_init(&buf
, 0);
825 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
826 pname
= strbuf_detach(&buf
, NULL
);
829 strbuf_release(&buf
);
832 pname
= pprint_rename(file
->from_name
, file
->name
);
834 file
->print_name
= pname
;
837 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
839 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
840 int max_change
= 0, max_len
= 0;
841 int total_files
= data
->nr
;
842 int width
, name_width
;
843 const char *reset
, *set
, *add_c
, *del_c
;
848 width
= options
->stat_width
? options
->stat_width
: 80;
849 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
851 /* Sanity: give at least 5 columns to the graph,
852 * but leave at least 10 columns for the name.
858 else if (width
< name_width
+ 15)
859 name_width
= width
- 15;
861 /* Find the longest filename and max number of changes */
862 reset
= diff_get_color_opt(options
, DIFF_RESET
);
863 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
864 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
865 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
867 for (i
= 0; i
< data
->nr
; i
++) {
868 struct diffstat_file
*file
= data
->files
[i
];
869 int change
= file
->added
+ file
->deleted
;
870 fill_print_name(file
);
871 len
= strlen(file
->print_name
);
875 if (file
->is_binary
|| file
->is_unmerged
)
877 if (max_change
< change
)
881 /* Compute the width of the graph part;
882 * 10 is for one blank at the beginning of the line plus
883 * " | count " between the name and the graph.
885 * From here on, name_width is the width of the name area,
886 * and width is the width of the graph area.
888 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
889 if (width
< (name_width
+ 10) + max_change
)
890 width
= width
- (name_width
+ 10);
894 for (i
= 0; i
< data
->nr
; i
++) {
895 const char *prefix
= "";
896 char *name
= data
->files
[i
]->print_name
;
897 int added
= data
->files
[i
]->added
;
898 int deleted
= data
->files
[i
]->deleted
;
902 * "scale" the filename
905 name_len
= strlen(name
);
906 if (name_width
< name_len
) {
910 name
+= name_len
- len
;
911 slash
= strchr(name
, '/');
916 if (data
->files
[i
]->is_binary
) {
917 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
918 fprintf(options
->file
, " Bin ");
919 fprintf(options
->file
, "%s%d%s", del_c
, deleted
, reset
);
920 fprintf(options
->file
, " -> ");
921 fprintf(options
->file
, "%s%d%s", add_c
, added
, reset
);
922 fprintf(options
->file
, " bytes");
923 fprintf(options
->file
, "\n");
926 else if (data
->files
[i
]->is_unmerged
) {
927 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
928 fprintf(options
->file
, " Unmerged\n");
931 else if (!data
->files
[i
]->is_renamed
&&
932 (added
+ deleted
== 0)) {
938 * scale the add/delete
946 if (width
<= max_change
) {
947 add
= scale_linear(add
, width
, max_change
);
948 del
= scale_linear(del
, width
, max_change
);
951 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
952 fprintf(options
->file
, "%5d%s", added
+ deleted
,
953 added
+ deleted
? " " : "");
954 show_graph(options
->file
, '+', add
, add_c
, reset
);
955 show_graph(options
->file
, '-', del
, del_c
, reset
);
956 fprintf(options
->file
, "\n");
958 fprintf(options
->file
,
959 "%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
960 set
, total_files
, adds
, dels
, reset
);
963 static void show_shortstats(struct diffstat_t
* data
, struct diff_options
*options
)
965 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
970 for (i
= 0; i
< data
->nr
; i
++) {
971 if (!data
->files
[i
]->is_binary
&&
972 !data
->files
[i
]->is_unmerged
) {
973 int added
= data
->files
[i
]->added
;
974 int deleted
= data
->files
[i
]->deleted
;
975 if (!data
->files
[i
]->is_renamed
&&
976 (added
+ deleted
== 0)) {
984 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
985 total_files
, adds
, dels
);
988 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
995 for (i
= 0; i
< data
->nr
; i
++) {
996 struct diffstat_file
*file
= data
->files
[i
];
999 fprintf(options
->file
, "-\t-\t");
1001 fprintf(options
->file
,
1002 "%d\t%d\t", file
->added
, file
->deleted
);
1003 if (options
->line_termination
) {
1004 fill_print_name(file
);
1005 if (!file
->is_renamed
)
1006 write_name_quoted(file
->name
, options
->file
,
1007 options
->line_termination
);
1009 fputs(file
->print_name
, options
->file
);
1010 putc(options
->line_termination
, options
->file
);
1013 if (file
->is_renamed
) {
1014 putc('\0', options
->file
);
1015 write_name_quoted(file
->from_name
, options
->file
, '\0');
1017 write_name_quoted(file
->name
, options
->file
, '\0');
1022 struct dirstat_file
{
1024 unsigned long changed
;
1027 struct dirstat_dir
{
1028 struct dirstat_file
*files
;
1029 int alloc
, nr
, percent
, cumulative
;
1032 static long gather_dirstat(FILE *file
, struct dirstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1034 unsigned long this_dir
= 0;
1035 unsigned int sources
= 0;
1038 struct dirstat_file
*f
= dir
->files
;
1039 int namelen
= strlen(f
->name
);
1043 if (namelen
< baselen
)
1045 if (memcmp(f
->name
, base
, baselen
))
1047 slash
= strchr(f
->name
+ baselen
, '/');
1049 int newbaselen
= slash
+ 1 - f
->name
;
1050 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1062 * We don't report dirstat's for
1064 * - or cases where everything came from a single directory
1065 * under this directory (sources == 1).
1067 if (baselen
&& sources
!= 1) {
1068 int permille
= this_dir
* 1000 / changed
;
1070 int percent
= permille
/ 10;
1071 if (percent
>= dir
->percent
) {
1072 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1073 if (!dir
->cumulative
)
1081 static int dirstat_compare(const void *_a
, const void *_b
)
1083 const struct dirstat_file
*a
= _a
;
1084 const struct dirstat_file
*b
= _b
;
1085 return strcmp(a
->name
, b
->name
);
1088 static void show_dirstat(struct diff_options
*options
)
1091 unsigned long changed
;
1092 struct dirstat_dir dir
;
1093 struct diff_queue_struct
*q
= &diff_queued_diff
;
1098 dir
.percent
= options
->dirstat_percent
;
1099 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1102 for (i
= 0; i
< q
->nr
; i
++) {
1103 struct diff_filepair
*p
= q
->queue
[i
];
1105 unsigned long copied
, added
, damage
;
1107 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1109 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1110 diff_populate_filespec(p
->one
, 0);
1111 diff_populate_filespec(p
->two
, 0);
1112 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1114 diff_free_filespec_data(p
->one
);
1115 diff_free_filespec_data(p
->two
);
1116 } else if (DIFF_FILE_VALID(p
->one
)) {
1117 diff_populate_filespec(p
->one
, 1);
1119 diff_free_filespec_data(p
->one
);
1120 } else if (DIFF_FILE_VALID(p
->two
)) {
1121 diff_populate_filespec(p
->two
, 1);
1123 added
= p
->two
->size
;
1124 diff_free_filespec_data(p
->two
);
1129 * Original minus copied is the removed material,
1130 * added is the new material. They are both damages
1131 * made to the preimage.
1133 damage
= (p
->one
->size
- copied
) + added
;
1135 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1136 dir
.files
[dir
.nr
].name
= name
;
1137 dir
.files
[dir
.nr
].changed
= damage
;
1142 /* This can happen even with many files, if everything was renames */
1146 /* Show all directories with more than x% of the changes */
1147 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
1148 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1151 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1154 for (i
= 0; i
< diffstat
->nr
; i
++) {
1155 struct diffstat_file
*f
= diffstat
->files
[i
];
1156 if (f
->name
!= f
->print_name
)
1157 free(f
->print_name
);
1162 free(diffstat
->files
);
1165 struct checkdiff_t
{
1166 const char *filename
;
1168 struct diff_options
*o
;
1171 int trailing_blanks_start
;
1174 static int is_conflict_marker(const char *line
, unsigned long len
)
1181 firstchar
= line
[0];
1182 switch (firstchar
) {
1183 case '=': case '>': case '<':
1188 for (cnt
= 1; cnt
< 7; cnt
++)
1189 if (line
[cnt
] != firstchar
)
1191 /* line[0] thru line[6] are same as firstchar */
1192 if (firstchar
== '=') {
1193 /* divider between ours and theirs? */
1194 if (len
!= 8 || line
[7] != '\n')
1196 } else if (len
< 8 || !isspace(line
[7])) {
1197 /* not divider before ours nor after theirs */
1203 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1205 struct checkdiff_t
*data
= priv
;
1206 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1207 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1208 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1209 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1212 if (line
[0] == '+') {
1215 if (!ws_blank_line(line
+ 1, len
- 1, data
->ws_rule
))
1216 data
->trailing_blanks_start
= 0;
1217 else if (!data
->trailing_blanks_start
)
1218 data
->trailing_blanks_start
= data
->lineno
;
1219 if (is_conflict_marker(line
+ 1, len
- 1)) {
1221 fprintf(data
->o
->file
,
1222 "%s:%d: leftover conflict marker\n",
1223 data
->filename
, data
->lineno
);
1225 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1228 data
->status
|= bad
;
1229 err
= whitespace_error_string(bad
);
1230 fprintf(data
->o
->file
, "%s:%d: %s.\n",
1231 data
->filename
, data
->lineno
, err
);
1233 emit_line(data
->o
->file
, set
, reset
, line
, 1);
1234 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1235 data
->o
->file
, set
, reset
, ws
);
1236 } else if (line
[0] == ' ') {
1238 data
->trailing_blanks_start
= 0;
1239 } else if (line
[0] == '@') {
1240 char *plus
= strchr(line
, '+');
1242 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1244 die("invalid diff");
1245 data
->trailing_blanks_start
= 0;
1249 static unsigned char *deflate_it(char *data
,
1251 unsigned long *result_size
)
1254 unsigned char *deflated
;
1257 memset(&stream
, 0, sizeof(stream
));
1258 deflateInit(&stream
, zlib_compression_level
);
1259 bound
= deflateBound(&stream
, size
);
1260 deflated
= xmalloc(bound
);
1261 stream
.next_out
= deflated
;
1262 stream
.avail_out
= bound
;
1264 stream
.next_in
= (unsigned char *)data
;
1265 stream
.avail_in
= size
;
1266 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1268 deflateEnd(&stream
);
1269 *result_size
= stream
.total_out
;
1273 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1279 unsigned long orig_size
;
1280 unsigned long delta_size
;
1281 unsigned long deflate_size
;
1282 unsigned long data_size
;
1284 /* We could do deflated delta, or we could do just deflated two,
1285 * whichever is smaller.
1288 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1289 if (one
->size
&& two
->size
) {
1290 delta
= diff_delta(one
->ptr
, one
->size
,
1291 two
->ptr
, two
->size
,
1292 &delta_size
, deflate_size
);
1294 void *to_free
= delta
;
1295 orig_size
= delta_size
;
1296 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1301 if (delta
&& delta_size
< deflate_size
) {
1302 fprintf(file
, "delta %lu\n", orig_size
);
1305 data_size
= delta_size
;
1308 fprintf(file
, "literal %lu\n", two
->size
);
1311 data_size
= deflate_size
;
1314 /* emit data encoded in base85 */
1317 int bytes
= (52 < data_size
) ? 52 : data_size
;
1321 line
[0] = bytes
+ 'A' - 1;
1323 line
[0] = bytes
- 26 + 'a' - 1;
1324 encode_85(line
+ 1, cp
, bytes
);
1325 cp
= (char *) cp
+ bytes
;
1329 fprintf(file
, "\n");
1333 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1335 fprintf(file
, "GIT binary patch\n");
1336 emit_binary_diff_body(file
, one
, two
);
1337 emit_binary_diff_body(file
, two
, one
);
1340 static void setup_diff_attr_check(struct git_attr_check
*check
)
1342 static struct git_attr
*attr_diff
;
1345 attr_diff
= git_attr("diff", 4);
1347 check
[0].attr
= attr_diff
;
1350 static void diff_filespec_check_attr(struct diff_filespec
*one
)
1352 struct git_attr_check attr_diff_check
;
1353 int check_from_data
= 0;
1355 if (one
->checked_attr
)
1358 setup_diff_attr_check(&attr_diff_check
);
1360 one
->funcname_pattern_ident
= NULL
;
1362 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1366 value
= attr_diff_check
.value
;
1367 if (ATTR_TRUE(value
))
1369 else if (ATTR_FALSE(value
))
1372 check_from_data
= 1;
1374 /* funcname pattern ident */
1375 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1378 one
->funcname_pattern_ident
= value
;
1381 if (check_from_data
) {
1382 if (!one
->data
&& DIFF_FILE_VALID(one
))
1383 diff_populate_filespec(one
, 0);
1386 one
->is_binary
= buffer_is_binary(one
->data
, one
->size
);
1390 int diff_filespec_is_binary(struct diff_filespec
*one
)
1392 diff_filespec_check_attr(one
);
1393 return one
->is_binary
;
1396 static const struct funcname_pattern_entry
*funcname_pattern(const char *ident
)
1398 struct funcname_pattern_list
*pp
;
1400 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
1401 if (!strcmp(ident
, pp
->e
.name
))
1406 static const struct funcname_pattern_entry builtin_funcname_pattern
[] = {
1407 { "bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
1409 { "html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$", REG_EXTENDED
},
1411 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
1412 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$",
1415 "^((procedure|function|constructor|destructor|interface|"
1416 "implementation|initialization|finalization)[ \t]*.*)$"
1418 "^(.*=[ \t]*(class|record).*)$",
1420 { "php", "^[\t ]*((function|class).*)", REG_EXTENDED
},
1421 { "python", "^[ \t]*((class|def)[ \t].*)$", REG_EXTENDED
},
1422 { "ruby", "^[ \t]*((class|module|def)[ \t].*)$",
1424 { "bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
1427 "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
1431 static const struct funcname_pattern_entry
*diff_funcname_pattern(struct diff_filespec
*one
)
1434 const struct funcname_pattern_entry
*pe
;
1437 diff_filespec_check_attr(one
);
1438 ident
= one
->funcname_pattern_ident
;
1442 * If the config file has "funcname.default" defined, that
1443 * regexp is used; otherwise NULL is returned and xemit uses
1444 * the built-in default.
1446 return funcname_pattern("default");
1448 /* Look up custom "funcname.$ident" regexp from config. */
1449 pe
= funcname_pattern(ident
);
1454 * And define built-in fallback patterns here. Note that
1455 * these can be overridden by the user's config settings.
1457 for (i
= 0; i
< ARRAY_SIZE(builtin_funcname_pattern
); i
++)
1458 if (!strcmp(ident
, builtin_funcname_pattern
[i
].name
))
1459 return &builtin_funcname_pattern
[i
];
1464 static void builtin_diff(const char *name_a
,
1466 struct diff_filespec
*one
,
1467 struct diff_filespec
*two
,
1468 const char *xfrm_msg
,
1469 struct diff_options
*o
,
1470 int complete_rewrite
)
1474 char *a_one
, *b_two
;
1475 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1476 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1478 a_one
= quote_two(o
->a_prefix
, name_a
+ (*name_a
== '/'));
1479 b_two
= quote_two(o
->b_prefix
, name_b
+ (*name_b
== '/'));
1480 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1481 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1482 fprintf(o
->file
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1483 if (lbl
[0][0] == '/') {
1485 fprintf(o
->file
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1486 if (xfrm_msg
&& xfrm_msg
[0])
1487 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1489 else if (lbl
[1][0] == '/') {
1490 fprintf(o
->file
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1491 if (xfrm_msg
&& xfrm_msg
[0])
1492 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1495 if (one
->mode
!= two
->mode
) {
1496 fprintf(o
->file
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1497 fprintf(o
->file
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1499 if (xfrm_msg
&& xfrm_msg
[0])
1500 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1502 * we do not run diff between different kind
1505 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1506 goto free_ab_and_return
;
1507 if (complete_rewrite
) {
1508 emit_rewrite_diff(name_a
, name_b
, one
, two
, o
);
1509 o
->found_changes
= 1;
1510 goto free_ab_and_return
;
1514 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1515 die("unable to read files to diff");
1517 if (!DIFF_OPT_TST(o
, TEXT
) &&
1518 (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
))) {
1519 /* Quite common confusing case */
1520 if (mf1
.size
== mf2
.size
&&
1521 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1522 goto free_ab_and_return
;
1523 if (DIFF_OPT_TST(o
, BINARY
))
1524 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1526 fprintf(o
->file
, "Binary files %s and %s differ\n",
1528 o
->found_changes
= 1;
1531 /* Crazy xdl interfaces.. */
1532 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1536 struct emit_callback ecbdata
;
1537 const struct funcname_pattern_entry
*pe
;
1539 pe
= diff_funcname_pattern(one
);
1541 pe
= diff_funcname_pattern(two
);
1543 memset(&xecfg
, 0, sizeof(xecfg
));
1544 memset(&ecbdata
, 0, sizeof(ecbdata
));
1545 ecbdata
.label_path
= lbl
;
1546 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1547 ecbdata
.found_changesp
= &o
->found_changes
;
1548 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1549 ecbdata
.file
= o
->file
;
1550 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1551 xecfg
.ctxlen
= o
->context
;
1552 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1554 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
1557 else if (!prefixcmp(diffopts
, "--unified="))
1558 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1559 else if (!prefixcmp(diffopts
, "-u"))
1560 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1561 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
)) {
1562 ecbdata
.diff_words
=
1563 xcalloc(1, sizeof(struct diff_words_data
));
1564 ecbdata
.diff_words
->file
= o
->file
;
1566 xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
1567 &xpp
, &xecfg
, &ecb
);
1568 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
))
1569 free_diff_words_data(&ecbdata
);
1573 diff_free_filespec_data(one
);
1574 diff_free_filespec_data(two
);
1580 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1581 struct diff_filespec
*one
,
1582 struct diff_filespec
*two
,
1583 struct diffstat_t
*diffstat
,
1584 struct diff_options
*o
,
1585 int complete_rewrite
)
1588 struct diffstat_file
*data
;
1590 data
= diffstat_add(diffstat
, name_a
, name_b
);
1593 data
->is_unmerged
= 1;
1596 if (complete_rewrite
) {
1597 diff_populate_filespec(one
, 0);
1598 diff_populate_filespec(two
, 0);
1599 data
->deleted
= count_lines(one
->data
, one
->size
);
1600 data
->added
= count_lines(two
->data
, two
->size
);
1601 goto free_and_return
;
1603 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1604 die("unable to read files to diff");
1606 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1607 data
->is_binary
= 1;
1608 data
->added
= mf2
.size
;
1609 data
->deleted
= mf1
.size
;
1611 /* Crazy xdl interfaces.. */
1616 memset(&xecfg
, 0, sizeof(xecfg
));
1617 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1618 xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
1619 &xpp
, &xecfg
, &ecb
);
1623 diff_free_filespec_data(one
);
1624 diff_free_filespec_data(two
);
1627 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1628 const char *attr_path
,
1629 struct diff_filespec
*one
,
1630 struct diff_filespec
*two
,
1631 struct diff_options
*o
)
1634 struct checkdiff_t data
;
1639 memset(&data
, 0, sizeof(data
));
1640 data
.filename
= name_b
? name_b
: name_a
;
1643 data
.ws_rule
= whitespace_rule(attr_path
);
1645 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1646 die("unable to read files to diff");
1649 * All the other codepaths check both sides, but not checking
1650 * the "old" side here is deliberate. We are checking the newly
1651 * introduced changes, and as long as the "new" side is text, we
1652 * can and should check what it introduces.
1654 if (diff_filespec_is_binary(two
))
1655 goto free_and_return
;
1657 /* Crazy xdl interfaces.. */
1662 memset(&xecfg
, 0, sizeof(xecfg
));
1663 xecfg
.ctxlen
= 1; /* at least one context line */
1664 xpp
.flags
= XDF_NEED_MINIMAL
;
1665 xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
1666 &xpp
, &xecfg
, &ecb
);
1668 if ((data
.ws_rule
& WS_TRAILING_SPACE
) &&
1669 data
.trailing_blanks_start
) {
1670 fprintf(o
->file
, "%s:%d: ends with blank lines.\n",
1671 data
.filename
, data
.trailing_blanks_start
);
1672 data
.status
= 1; /* report errors */
1676 diff_free_filespec_data(one
);
1677 diff_free_filespec_data(two
);
1679 DIFF_OPT_SET(o
, CHECK_FAILED
);
1682 struct diff_filespec
*alloc_filespec(const char *path
)
1684 int namelen
= strlen(path
);
1685 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1687 memset(spec
, 0, sizeof(*spec
));
1688 spec
->path
= (char *)(spec
+ 1);
1689 memcpy(spec
->path
, path
, namelen
+1);
1694 void free_filespec(struct diff_filespec
*spec
)
1696 if (!--spec
->count
) {
1697 diff_free_filespec_data(spec
);
1702 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1703 unsigned short mode
)
1706 spec
->mode
= canon_mode(mode
);
1707 hashcpy(spec
->sha1
, sha1
);
1708 spec
->sha1_valid
= !is_null_sha1(sha1
);
1713 * Given a name and sha1 pair, if the index tells us the file in
1714 * the work tree has that object contents, return true, so that
1715 * prepare_temp_file() does not have to inflate and extract.
1717 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1719 struct cache_entry
*ce
;
1723 /* We do not read the cache ourselves here, because the
1724 * benchmark with my previous version that always reads cache
1725 * shows that it makes things worse for diff-tree comparing
1726 * two linux-2.6 kernel trees in an already checked out work
1727 * tree. This is because most diff-tree comparisons deal with
1728 * only a small number of files, while reading the cache is
1729 * expensive for a large project, and its cost outweighs the
1730 * savings we get by not inflating the object to a temporary
1731 * file. Practically, this code only helps when we are used
1732 * by diff-cache --cached, which does read the cache before
1738 /* We want to avoid the working directory if our caller
1739 * doesn't need the data in a normal file, this system
1740 * is rather slow with its stat/open/mmap/close syscalls,
1741 * and the object is contained in a pack file. The pack
1742 * is probably already open and will be faster to obtain
1743 * the data through than the working directory. Loose
1744 * objects however would tend to be slower as they need
1745 * to be individually opened and inflated.
1747 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1751 pos
= cache_name_pos(name
, len
);
1754 ce
= active_cache
[pos
];
1757 * This is not the sha1 we are looking for, or
1758 * unreusable because it is not a regular file.
1760 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
1764 * If ce matches the file in the work tree, we can reuse it.
1766 if (ce_uptodate(ce
) ||
1767 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
1773 static int populate_from_stdin(struct diff_filespec
*s
)
1778 strbuf_init(&buf
, 0);
1779 if (strbuf_read(&buf
, 0, 0) < 0)
1780 return error("error while reading from stdin %s",
1783 s
->should_munmap
= 0;
1784 s
->data
= strbuf_detach(&buf
, &size
);
1790 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1793 char *data
= xmalloc(100);
1794 len
= snprintf(data
, 100,
1795 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1807 * While doing rename detection and pickaxe operation, we may need to
1808 * grab the data for the blob (or file) for our own in-core comparison.
1809 * diff_filespec has data and size fields for this purpose.
1811 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1814 if (!DIFF_FILE_VALID(s
))
1815 die("internal error: asking to populate invalid file.");
1816 if (S_ISDIR(s
->mode
))
1822 if (size_only
&& 0 < s
->size
)
1825 if (S_ISGITLINK(s
->mode
))
1826 return diff_populate_gitlink(s
, size_only
);
1828 if (!s
->sha1_valid
||
1829 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1834 if (!strcmp(s
->path
, "-"))
1835 return populate_from_stdin(s
);
1837 if (lstat(s
->path
, &st
) < 0) {
1838 if (errno
== ENOENT
) {
1842 s
->data
= (char *)"";
1847 s
->size
= xsize_t(st
.st_size
);
1852 if (S_ISLNK(st
.st_mode
)) {
1854 s
->data
= xmalloc(s
->size
);
1856 ret
= readlink(s
->path
, s
->data
, s
->size
);
1863 fd
= open(s
->path
, O_RDONLY
);
1866 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1868 s
->should_munmap
= 1;
1871 * Convert from working tree format to canonical git format
1873 strbuf_init(&buf
, 0);
1874 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
1876 munmap(s
->data
, s
->size
);
1877 s
->should_munmap
= 0;
1878 s
->data
= strbuf_detach(&buf
, &size
);
1884 enum object_type type
;
1886 type
= sha1_object_info(s
->sha1
, &s
->size
);
1888 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1895 void diff_free_filespec_blob(struct diff_filespec
*s
)
1899 else if (s
->should_munmap
)
1900 munmap(s
->data
, s
->size
);
1902 if (s
->should_free
|| s
->should_munmap
) {
1903 s
->should_free
= s
->should_munmap
= 0;
1908 void diff_free_filespec_data(struct diff_filespec
*s
)
1910 diff_free_filespec_blob(s
);
1915 static void prep_temp_blob(struct diff_tempfile
*temp
,
1918 const unsigned char *sha1
,
1923 fd
= git_mkstemp(temp
->tmp_path
, PATH_MAX
, ".diff_XXXXXX");
1925 die("unable to create temp-file: %s", strerror(errno
));
1926 if (write_in_full(fd
, blob
, size
) != size
)
1927 die("unable to write temp-file");
1929 temp
->name
= temp
->tmp_path
;
1930 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1932 sprintf(temp
->mode
, "%06o", mode
);
1935 static void prepare_temp_file(const char *name
,
1936 struct diff_tempfile
*temp
,
1937 struct diff_filespec
*one
)
1939 if (!DIFF_FILE_VALID(one
)) {
1941 /* A '-' entry produces this for file-2, and
1942 * a '+' entry produces this for file-1.
1944 temp
->name
= "/dev/null";
1945 strcpy(temp
->hex
, ".");
1946 strcpy(temp
->mode
, ".");
1950 if (!one
->sha1_valid
||
1951 reuse_worktree_file(name
, one
->sha1
, 1)) {
1953 if (lstat(name
, &st
) < 0) {
1954 if (errno
== ENOENT
)
1955 goto not_a_valid_file
;
1956 die("stat(%s): %s", name
, strerror(errno
));
1958 if (S_ISLNK(st
.st_mode
)) {
1960 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1961 size_t sz
= xsize_t(st
.st_size
);
1962 if (sizeof(buf
) <= st
.st_size
)
1963 die("symlink too long: %s", name
);
1964 ret
= readlink(name
, buf
, sz
);
1966 die("readlink(%s)", name
);
1967 prep_temp_blob(temp
, buf
, sz
,
1969 one
->sha1
: null_sha1
),
1971 one
->mode
: S_IFLNK
));
1974 /* we can borrow from the file in the work tree */
1976 if (!one
->sha1_valid
)
1977 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
1979 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
1980 /* Even though we may sometimes borrow the
1981 * contents from the work tree, we always want
1982 * one->mode. mode is trustworthy even when
1983 * !(one->sha1_valid), as long as
1984 * DIFF_FILE_VALID(one).
1986 sprintf(temp
->mode
, "%06o", one
->mode
);
1991 if (diff_populate_filespec(one
, 0))
1992 die("cannot read data blob for %s", one
->path
);
1993 prep_temp_blob(temp
, one
->data
, one
->size
,
1994 one
->sha1
, one
->mode
);
1998 static void remove_tempfile(void)
2002 for (i
= 0; i
< 2; i
++)
2003 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
2004 unlink(diff_temp
[i
].name
);
2005 diff_temp
[i
].name
= NULL
;
2009 static void remove_tempfile_on_signal(int signo
)
2012 signal(SIGINT
, SIG_DFL
);
2016 /* An external diff command takes:
2018 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2019 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2022 static void run_external_diff(const char *pgm
,
2025 struct diff_filespec
*one
,
2026 struct diff_filespec
*two
,
2027 const char *xfrm_msg
,
2028 int complete_rewrite
)
2030 const char *spawn_arg
[10];
2031 struct diff_tempfile
*temp
= diff_temp
;
2033 static int atexit_asked
= 0;
2034 const char *othername
;
2035 const char **arg
= &spawn_arg
[0];
2037 othername
= (other
? other
: name
);
2039 prepare_temp_file(name
, &temp
[0], one
);
2040 prepare_temp_file(othername
, &temp
[1], two
);
2041 if (! atexit_asked
&&
2042 (temp
[0].name
== temp
[0].tmp_path
||
2043 temp
[1].name
== temp
[1].tmp_path
)) {
2045 atexit(remove_tempfile
);
2047 signal(SIGINT
, remove_tempfile_on_signal
);
2053 *arg
++ = temp
[0].name
;
2054 *arg
++ = temp
[0].hex
;
2055 *arg
++ = temp
[0].mode
;
2056 *arg
++ = temp
[1].name
;
2057 *arg
++ = temp
[1].hex
;
2058 *arg
++ = temp
[1].mode
;
2069 retval
= run_command_v_opt(spawn_arg
, 0);
2072 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2077 static const char *external_diff_attr(const char *name
)
2079 struct git_attr_check attr_diff_check
;
2084 setup_diff_attr_check(&attr_diff_check
);
2085 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
2086 const char *value
= attr_diff_check
.value
;
2087 if (!ATTR_TRUE(value
) &&
2088 !ATTR_FALSE(value
) &&
2089 !ATTR_UNSET(value
)) {
2090 struct ll_diff_driver
*drv
;
2092 for (drv
= user_diff
; drv
; drv
= drv
->next
)
2093 if (!strcmp(drv
->name
, value
))
2100 static void run_diff_cmd(const char *pgm
,
2103 const char *attr_path
,
2104 struct diff_filespec
*one
,
2105 struct diff_filespec
*two
,
2106 const char *xfrm_msg
,
2107 struct diff_options
*o
,
2108 int complete_rewrite
)
2110 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2113 const char *cmd
= external_diff_attr(attr_path
);
2119 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2124 builtin_diff(name
, other
? other
: name
,
2125 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2127 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2130 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2132 if (DIFF_FILE_VALID(one
)) {
2133 if (!one
->sha1_valid
) {
2135 if (!strcmp(one
->path
, "-")) {
2136 hashcpy(one
->sha1
, null_sha1
);
2139 if (lstat(one
->path
, &st
) < 0)
2140 die("stat %s", one
->path
);
2141 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2142 die("cannot hash %s\n", one
->path
);
2149 static int similarity_index(struct diff_filepair
*p
)
2151 return p
->score
* 100 / MAX_SCORE
;
2154 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2156 /* Strip the prefix but do not molest /dev/null and absolute paths */
2157 if (*namep
&& **namep
!= '/')
2158 *namep
+= prefix_length
;
2159 if (*otherp
&& **otherp
!= '/')
2160 *otherp
+= prefix_length
;
2163 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2165 const char *pgm
= external_diff();
2168 struct diff_filespec
*one
= p
->one
;
2169 struct diff_filespec
*two
= p
->two
;
2172 const char *attr_path
;
2173 int complete_rewrite
= 0;
2175 name
= p
->one
->path
;
2176 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2178 if (o
->prefix_length
)
2179 strip_prefix(o
->prefix_length
, &name
, &other
);
2181 if (DIFF_PAIR_UNMERGED(p
)) {
2182 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2183 NULL
, NULL
, NULL
, o
, 0);
2187 diff_fill_sha1_info(one
);
2188 diff_fill_sha1_info(two
);
2190 strbuf_init(&msg
, PATH_MAX
* 2 + 300);
2191 switch (p
->status
) {
2192 case DIFF_STATUS_COPIED
:
2193 strbuf_addf(&msg
, "similarity index %d%%", similarity_index(p
));
2194 strbuf_addstr(&msg
, "\ncopy from ");
2195 quote_c_style(name
, &msg
, NULL
, 0);
2196 strbuf_addstr(&msg
, "\ncopy to ");
2197 quote_c_style(other
, &msg
, NULL
, 0);
2198 strbuf_addch(&msg
, '\n');
2200 case DIFF_STATUS_RENAMED
:
2201 strbuf_addf(&msg
, "similarity index %d%%", similarity_index(p
));
2202 strbuf_addstr(&msg
, "\nrename from ");
2203 quote_c_style(name
, &msg
, NULL
, 0);
2204 strbuf_addstr(&msg
, "\nrename to ");
2205 quote_c_style(other
, &msg
, NULL
, 0);
2206 strbuf_addch(&msg
, '\n');
2208 case DIFF_STATUS_MODIFIED
:
2210 strbuf_addf(&msg
, "dissimilarity index %d%%\n",
2211 similarity_index(p
));
2212 complete_rewrite
= 1;
2221 if (hashcmp(one
->sha1
, two
->sha1
)) {
2222 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2224 if (DIFF_OPT_TST(o
, BINARY
)) {
2226 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2227 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2230 strbuf_addf(&msg
, "index %.*s..%.*s",
2231 abbrev
, sha1_to_hex(one
->sha1
),
2232 abbrev
, sha1_to_hex(two
->sha1
));
2233 if (one
->mode
== two
->mode
)
2234 strbuf_addf(&msg
, " %06o", one
->mode
);
2235 strbuf_addch(&msg
, '\n');
2239 strbuf_setlen(&msg
, msg
.len
- 1);
2240 xfrm_msg
= msg
.len
? msg
.buf
: NULL
;
2243 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2244 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2245 /* a filepair that changes between file and symlink
2246 * needs to be split into deletion and creation.
2248 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2249 run_diff_cmd(NULL
, name
, other
, attr_path
,
2250 one
, null
, xfrm_msg
, o
, 0);
2252 null
= alloc_filespec(one
->path
);
2253 run_diff_cmd(NULL
, name
, other
, attr_path
,
2254 null
, two
, xfrm_msg
, o
, 0);
2258 run_diff_cmd(pgm
, name
, other
, attr_path
,
2259 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2261 strbuf_release(&msg
);
2264 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2265 struct diffstat_t
*diffstat
)
2269 int complete_rewrite
= 0;
2271 if (DIFF_PAIR_UNMERGED(p
)) {
2273 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2277 name
= p
->one
->path
;
2278 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2280 if (o
->prefix_length
)
2281 strip_prefix(o
->prefix_length
, &name
, &other
);
2283 diff_fill_sha1_info(p
->one
);
2284 diff_fill_sha1_info(p
->two
);
2286 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2287 complete_rewrite
= 1;
2288 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2291 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2295 const char *attr_path
;
2297 if (DIFF_PAIR_UNMERGED(p
)) {
2302 name
= p
->one
->path
;
2303 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2304 attr_path
= other
? other
: name
;
2306 if (o
->prefix_length
)
2307 strip_prefix(o
->prefix_length
, &name
, &other
);
2309 diff_fill_sha1_info(p
->one
);
2310 diff_fill_sha1_info(p
->two
);
2312 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2315 void diff_setup(struct diff_options
*options
)
2317 memset(options
, 0, sizeof(*options
));
2319 options
->file
= stdout
;
2321 options
->line_termination
= '\n';
2322 options
->break_opt
= -1;
2323 options
->rename_limit
= -1;
2324 options
->dirstat_percent
= 3;
2325 DIFF_OPT_CLR(options
, DIRSTAT_CUMULATIVE
);
2326 options
->context
= 3;
2328 options
->change
= diff_change
;
2329 options
->add_remove
= diff_addremove
;
2330 if (diff_use_color_default
> 0)
2331 DIFF_OPT_SET(options
, COLOR_DIFF
);
2333 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2334 options
->detect_rename
= diff_detect_rename_default
;
2336 options
->a_prefix
= "a/";
2337 options
->b_prefix
= "b/";
2340 int diff_setup_done(struct diff_options
*options
)
2344 if (options
->output_format
& DIFF_FORMAT_NAME
)
2346 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2348 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2350 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2353 die("--name-only, --name-status, --check and -s are mutually exclusive");
2355 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2356 options
->detect_rename
= DIFF_DETECT_COPY
;
2358 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2359 options
->prefix
= NULL
;
2360 if (options
->prefix
)
2361 options
->prefix_length
= strlen(options
->prefix
);
2363 options
->prefix_length
= 0;
2365 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2366 DIFF_FORMAT_NAME_STATUS
|
2367 DIFF_FORMAT_CHECKDIFF
|
2368 DIFF_FORMAT_NO_OUTPUT
))
2369 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2370 DIFF_FORMAT_NUMSTAT
|
2371 DIFF_FORMAT_DIFFSTAT
|
2372 DIFF_FORMAT_SHORTSTAT
|
2373 DIFF_FORMAT_DIRSTAT
|
2374 DIFF_FORMAT_SUMMARY
|
2378 * These cases always need recursive; we do not drop caller-supplied
2379 * recursive bits for other formats here.
2381 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2382 DIFF_FORMAT_NUMSTAT
|
2383 DIFF_FORMAT_DIFFSTAT
|
2384 DIFF_FORMAT_SHORTSTAT
|
2385 DIFF_FORMAT_DIRSTAT
|
2386 DIFF_FORMAT_SUMMARY
|
2387 DIFF_FORMAT_CHECKDIFF
))
2388 DIFF_OPT_SET(options
, RECURSIVE
);
2390 * Also pickaxe would not work very well if you do not say recursive
2392 if (options
->pickaxe
)
2393 DIFF_OPT_SET(options
, RECURSIVE
);
2395 if (options
->detect_rename
&& options
->rename_limit
< 0)
2396 options
->rename_limit
= diff_rename_limit_default
;
2397 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2399 /* read-cache does not die even when it fails
2400 * so it is safe for us to do this here. Also
2401 * it does not smudge active_cache or active_nr
2402 * when it fails, so we do not have to worry about
2403 * cleaning it up ourselves either.
2407 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2408 options
->abbrev
= 40; /* full */
2411 * It does not make sense to show the first hit we happened
2412 * to have found. It does not make sense not to return with
2413 * exit code in such a case either.
2415 if (DIFF_OPT_TST(options
, QUIET
)) {
2416 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2417 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2423 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2433 if (c
== arg_short
) {
2437 if (val
&& isdigit(c
)) {
2439 int n
= strtoul(arg
, &end
, 10);
2450 eq
= strchr(arg
, '=');
2455 if (!len
|| strncmp(arg
, arg_long
, len
))
2460 if (!isdigit(*++eq
))
2462 n
= strtoul(eq
, &end
, 10);
2470 static int diff_scoreopt_parse(const char *opt
);
2472 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2474 const char *arg
= av
[0];
2476 /* Output format options */
2477 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2478 options
->output_format
|= DIFF_FORMAT_PATCH
;
2479 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2480 options
->output_format
|= DIFF_FORMAT_PATCH
;
2481 else if (!strcmp(arg
, "--raw"))
2482 options
->output_format
|= DIFF_FORMAT_RAW
;
2483 else if (!strcmp(arg
, "--patch-with-raw"))
2484 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2485 else if (!strcmp(arg
, "--numstat"))
2486 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2487 else if (!strcmp(arg
, "--shortstat"))
2488 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2489 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2490 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2491 else if (!strcmp(arg
, "--cumulative")) {
2492 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2493 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
2495 else if (!strcmp(arg
, "--check"))
2496 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2497 else if (!strcmp(arg
, "--summary"))
2498 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2499 else if (!strcmp(arg
, "--patch-with-stat"))
2500 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2501 else if (!strcmp(arg
, "--name-only"))
2502 options
->output_format
|= DIFF_FORMAT_NAME
;
2503 else if (!strcmp(arg
, "--name-status"))
2504 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2505 else if (!strcmp(arg
, "-s"))
2506 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2507 else if (!prefixcmp(arg
, "--stat")) {
2509 int width
= options
->stat_width
;
2510 int name_width
= options
->stat_name_width
;
2516 if (!prefixcmp(arg
, "-width="))
2517 width
= strtoul(arg
+ 7, &end
, 10);
2518 else if (!prefixcmp(arg
, "-name-width="))
2519 name_width
= strtoul(arg
+ 12, &end
, 10);
2522 width
= strtoul(arg
+1, &end
, 10);
2524 name_width
= strtoul(end
+1, &end
, 10);
2527 /* Important! This checks all the error cases! */
2530 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2531 options
->stat_name_width
= name_width
;
2532 options
->stat_width
= width
;
2535 /* renames options */
2536 else if (!prefixcmp(arg
, "-B")) {
2537 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2540 else if (!prefixcmp(arg
, "-M")) {
2541 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2543 options
->detect_rename
= DIFF_DETECT_RENAME
;
2545 else if (!prefixcmp(arg
, "-C")) {
2546 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2547 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2548 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2550 options
->detect_rename
= DIFF_DETECT_COPY
;
2552 else if (!strcmp(arg
, "--no-renames"))
2553 options
->detect_rename
= 0;
2554 else if (!strcmp(arg
, "--relative"))
2555 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2556 else if (!prefixcmp(arg
, "--relative=")) {
2557 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2558 options
->prefix
= arg
+ 11;
2562 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2563 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2564 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2565 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2566 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2567 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2570 else if (!strcmp(arg
, "--binary")) {
2571 options
->output_format
|= DIFF_FORMAT_PATCH
;
2572 DIFF_OPT_SET(options
, BINARY
);
2574 else if (!strcmp(arg
, "--full-index"))
2575 DIFF_OPT_SET(options
, FULL_INDEX
);
2576 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2577 DIFF_OPT_SET(options
, TEXT
);
2578 else if (!strcmp(arg
, "-R"))
2579 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2580 else if (!strcmp(arg
, "--find-copies-harder"))
2581 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2582 else if (!strcmp(arg
, "--follow"))
2583 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2584 else if (!strcmp(arg
, "--color"))
2585 DIFF_OPT_SET(options
, COLOR_DIFF
);
2586 else if (!strcmp(arg
, "--no-color"))
2587 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2588 else if (!strcmp(arg
, "--color-words"))
2589 options
->flags
|= DIFF_OPT_COLOR_DIFF
| DIFF_OPT_COLOR_DIFF_WORDS
;
2590 else if (!strcmp(arg
, "--exit-code"))
2591 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2592 else if (!strcmp(arg
, "--quiet"))
2593 DIFF_OPT_SET(options
, QUIET
);
2594 else if (!strcmp(arg
, "--ext-diff"))
2595 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2596 else if (!strcmp(arg
, "--no-ext-diff"))
2597 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2598 else if (!strcmp(arg
, "--ignore-submodules"))
2599 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
2602 else if (!strcmp(arg
, "-z"))
2603 options
->line_termination
= 0;
2604 else if (!prefixcmp(arg
, "-l"))
2605 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2606 else if (!prefixcmp(arg
, "-S"))
2607 options
->pickaxe
= arg
+ 2;
2608 else if (!strcmp(arg
, "--pickaxe-all"))
2609 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2610 else if (!strcmp(arg
, "--pickaxe-regex"))
2611 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2612 else if (!prefixcmp(arg
, "-O"))
2613 options
->orderfile
= arg
+ 2;
2614 else if (!prefixcmp(arg
, "--diff-filter="))
2615 options
->filter
= arg
+ 14;
2616 else if (!strcmp(arg
, "--abbrev"))
2617 options
->abbrev
= DEFAULT_ABBREV
;
2618 else if (!prefixcmp(arg
, "--abbrev=")) {
2619 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2620 if (options
->abbrev
< MINIMUM_ABBREV
)
2621 options
->abbrev
= MINIMUM_ABBREV
;
2622 else if (40 < options
->abbrev
)
2623 options
->abbrev
= 40;
2625 else if (!prefixcmp(arg
, "--src-prefix="))
2626 options
->a_prefix
= arg
+ 13;
2627 else if (!prefixcmp(arg
, "--dst-prefix="))
2628 options
->b_prefix
= arg
+ 13;
2629 else if (!strcmp(arg
, "--no-prefix"))
2630 options
->a_prefix
= options
->b_prefix
= "";
2631 else if (!prefixcmp(arg
, "--output=")) {
2632 options
->file
= fopen(arg
+ strlen("--output="), "w");
2633 options
->close_file
= 1;
2639 static int parse_num(const char **cp_p
)
2641 unsigned long num
, scale
;
2643 const char *cp
= *cp_p
;
2650 if ( !dot
&& ch
== '.' ) {
2653 } else if ( ch
== '%' ) {
2654 scale
= dot
? scale
*100 : 100;
2655 cp
++; /* % is always at the end */
2657 } else if ( ch
>= '0' && ch
<= '9' ) {
2658 if ( scale
< 100000 ) {
2660 num
= (num
*10) + (ch
-'0');
2669 /* user says num divided by scale and we say internally that
2670 * is MAX_SCORE * num / scale.
2672 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2675 static int diff_scoreopt_parse(const char *opt
)
2677 int opt1
, opt2
, cmd
;
2682 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2683 return -1; /* that is not a -M, -C nor -B option */
2685 opt1
= parse_num(&opt
);
2691 else if (*opt
!= '/')
2692 return -1; /* we expect -B80/99 or -B80 */
2695 opt2
= parse_num(&opt
);
2700 return opt1
| (opt2
<< 16);
2703 struct diff_queue_struct diff_queued_diff
;
2705 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2707 if (queue
->alloc
<= queue
->nr
) {
2708 queue
->alloc
= alloc_nr(queue
->alloc
);
2709 queue
->queue
= xrealloc(queue
->queue
,
2710 sizeof(dp
) * queue
->alloc
);
2712 queue
->queue
[queue
->nr
++] = dp
;
2715 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2716 struct diff_filespec
*one
,
2717 struct diff_filespec
*two
)
2719 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2727 void diff_free_filepair(struct diff_filepair
*p
)
2729 free_filespec(p
->one
);
2730 free_filespec(p
->two
);
2734 /* This is different from find_unique_abbrev() in that
2735 * it stuffs the result with dots for alignment.
2737 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2742 return sha1_to_hex(sha1
);
2744 abbrev
= find_unique_abbrev(sha1
, len
);
2745 abblen
= strlen(abbrev
);
2747 static char hex
[41];
2748 if (len
< abblen
&& abblen
<= len
+ 2)
2749 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2751 sprintf(hex
, "%s...", abbrev
);
2754 return sha1_to_hex(sha1
);
2757 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
2759 int line_termination
= opt
->line_termination
;
2760 int inter_name_termination
= line_termination
? '\t' : '\0';
2762 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2763 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
2764 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
2765 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
2768 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
2769 inter_name_termination
);
2771 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
2774 if (p
->status
== DIFF_STATUS_COPIED
||
2775 p
->status
== DIFF_STATUS_RENAMED
) {
2776 const char *name_a
, *name_b
;
2777 name_a
= p
->one
->path
;
2778 name_b
= p
->two
->path
;
2779 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2780 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
2781 write_name_quoted(name_b
, opt
->file
, line_termination
);
2783 const char *name_a
, *name_b
;
2784 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
2786 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2787 write_name_quoted(name_a
, opt
->file
, line_termination
);
2791 int diff_unmodified_pair(struct diff_filepair
*p
)
2793 /* This function is written stricter than necessary to support
2794 * the currently implemented transformers, but the idea is to
2795 * let transformers to produce diff_filepairs any way they want,
2796 * and filter and clean them up here before producing the output.
2798 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
2800 if (DIFF_PAIR_UNMERGED(p
))
2801 return 0; /* unmerged is interesting */
2803 /* deletion, addition, mode or type change
2804 * and rename are all interesting.
2806 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2807 DIFF_PAIR_MODE_CHANGED(p
) ||
2808 strcmp(one
->path
, two
->path
))
2811 /* both are valid and point at the same path. that is, we are
2812 * dealing with a change.
2814 if (one
->sha1_valid
&& two
->sha1_valid
&&
2815 !hashcmp(one
->sha1
, two
->sha1
))
2816 return 1; /* no change */
2817 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2818 return 1; /* both look at the same file on the filesystem. */
2822 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2824 if (diff_unmodified_pair(p
))
2827 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2828 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2829 return; /* no tree diffs in patch format */
2834 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2835 struct diffstat_t
*diffstat
)
2837 if (diff_unmodified_pair(p
))
2840 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2841 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2842 return; /* no tree diffs in patch format */
2844 run_diffstat(p
, o
, diffstat
);
2847 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2848 struct diff_options
*o
)
2850 if (diff_unmodified_pair(p
))
2853 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2854 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2855 return; /* no tree diffs in patch format */
2857 run_checkdiff(p
, o
);
2860 int diff_queue_is_empty(void)
2862 struct diff_queue_struct
*q
= &diff_queued_diff
;
2864 for (i
= 0; i
< q
->nr
; i
++)
2865 if (!diff_unmodified_pair(q
->queue
[i
]))
2871 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2873 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2876 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2878 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2879 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2881 s
->size
, s
->xfrm_flags
);
2884 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2886 diff_debug_filespec(p
->one
, i
, "one");
2887 diff_debug_filespec(p
->two
, i
, "two");
2888 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
2889 p
->score
, p
->status
? p
->status
: '?',
2890 p
->one
->rename_used
, p
->broken_pair
);
2893 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2897 fprintf(stderr
, "%s\n", msg
);
2898 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2899 for (i
= 0; i
< q
->nr
; i
++) {
2900 struct diff_filepair
*p
= q
->queue
[i
];
2901 diff_debug_filepair(p
, i
);
2906 static void diff_resolve_rename_copy(void)
2909 struct diff_filepair
*p
;
2910 struct diff_queue_struct
*q
= &diff_queued_diff
;
2912 diff_debug_queue("resolve-rename-copy", q
);
2914 for (i
= 0; i
< q
->nr
; i
++) {
2916 p
->status
= 0; /* undecided */
2917 if (DIFF_PAIR_UNMERGED(p
))
2918 p
->status
= DIFF_STATUS_UNMERGED
;
2919 else if (!DIFF_FILE_VALID(p
->one
))
2920 p
->status
= DIFF_STATUS_ADDED
;
2921 else if (!DIFF_FILE_VALID(p
->two
))
2922 p
->status
= DIFF_STATUS_DELETED
;
2923 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2924 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2926 /* from this point on, we are dealing with a pair
2927 * whose both sides are valid and of the same type, i.e.
2928 * either in-place edit or rename/copy edit.
2930 else if (DIFF_PAIR_RENAME(p
)) {
2932 * A rename might have re-connected a broken
2933 * pair up, causing the pathnames to be the
2934 * same again. If so, that's not a rename at
2935 * all, just a modification..
2937 * Otherwise, see if this source was used for
2938 * multiple renames, in which case we decrement
2939 * the count, and call it a copy.
2941 if (!strcmp(p
->one
->path
, p
->two
->path
))
2942 p
->status
= DIFF_STATUS_MODIFIED
;
2943 else if (--p
->one
->rename_used
> 0)
2944 p
->status
= DIFF_STATUS_COPIED
;
2946 p
->status
= DIFF_STATUS_RENAMED
;
2948 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2949 p
->one
->mode
!= p
->two
->mode
||
2950 is_null_sha1(p
->one
->sha1
))
2951 p
->status
= DIFF_STATUS_MODIFIED
;
2953 /* This is a "no-change" entry and should not
2954 * happen anymore, but prepare for broken callers.
2956 error("feeding unmodified %s to diffcore",
2958 p
->status
= DIFF_STATUS_UNKNOWN
;
2961 diff_debug_queue("resolve-rename-copy done", q
);
2964 static int check_pair_status(struct diff_filepair
*p
)
2966 switch (p
->status
) {
2967 case DIFF_STATUS_UNKNOWN
:
2970 die("internal error in diff-resolve-rename-copy");
2976 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
2978 int fmt
= opt
->output_format
;
2980 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
2981 diff_flush_checkdiff(p
, opt
);
2982 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
2983 diff_flush_raw(p
, opt
);
2984 else if (fmt
& DIFF_FORMAT_NAME
) {
2985 const char *name_a
, *name_b
;
2986 name_a
= p
->two
->path
;
2988 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2989 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
2993 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
2996 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
2998 fprintf(file
, " %s ", newdelete
);
2999 write_name_quoted(fs
->path
, file
, '\n');
3003 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
3005 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
3006 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
3007 show_name
? ' ' : '\n');
3009 write_name_quoted(p
->two
->path
, file
, '\n');
3014 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
3016 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
3018 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
3020 show_mode_change(file
, p
, 0);
3023 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
3026 case DIFF_STATUS_DELETED
:
3027 show_file_mode_name(file
, "delete", p
->one
);
3029 case DIFF_STATUS_ADDED
:
3030 show_file_mode_name(file
, "create", p
->two
);
3032 case DIFF_STATUS_COPIED
:
3033 show_rename_copy(file
, "copy", p
);
3035 case DIFF_STATUS_RENAMED
:
3036 show_rename_copy(file
, "rename", p
);
3040 fputs(" rewrite ", file
);
3041 write_name_quoted(p
->two
->path
, file
, ' ');
3042 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3044 show_mode_change(file
, p
, !p
->score
);
3054 static int remove_space(char *line
, int len
)
3060 for (i
= 0; i
< len
; i
++)
3061 if (!isspace((c
= line
[i
])))
3067 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3069 struct patch_id_t
*data
= priv
;
3072 /* Ignore line numbers when computing the SHA1 of the patch */
3073 if (!prefixcmp(line
, "@@ -"))
3076 new_len
= remove_space(line
, len
);
3078 SHA1_Update(data
->ctx
, line
, new_len
);
3079 data
->patchlen
+= new_len
;
3082 /* returns 0 upon success, and writes result into sha1 */
3083 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3085 struct diff_queue_struct
*q
= &diff_queued_diff
;
3088 struct patch_id_t data
;
3089 char buffer
[PATH_MAX
* 4 + 20];
3092 memset(&data
, 0, sizeof(struct patch_id_t
));
3095 for (i
= 0; i
< q
->nr
; i
++) {
3100 struct diff_filepair
*p
= q
->queue
[i
];
3103 memset(&xecfg
, 0, sizeof(xecfg
));
3105 return error("internal diff status error");
3106 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3108 if (diff_unmodified_pair(p
))
3110 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3111 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3113 if (DIFF_PAIR_UNMERGED(p
))
3116 diff_fill_sha1_info(p
->one
);
3117 diff_fill_sha1_info(p
->two
);
3118 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3119 fill_mmfile(&mf2
, p
->two
) < 0)
3120 return error("unable to read files to diff");
3122 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3123 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3124 if (p
->one
->mode
== 0)
3125 len1
= snprintf(buffer
, sizeof(buffer
),
3126 "diff--gita/%.*sb/%.*s"
3133 len2
, p
->two
->path
);
3134 else if (p
->two
->mode
== 0)
3135 len1
= snprintf(buffer
, sizeof(buffer
),
3136 "diff--gita/%.*sb/%.*s"
3137 "deletedfilemode%06o"
3143 len1
, p
->one
->path
);
3145 len1
= snprintf(buffer
, sizeof(buffer
),
3146 "diff--gita/%.*sb/%.*s"
3152 len2
, p
->two
->path
);
3153 SHA1_Update(&ctx
, buffer
, len1
);
3155 xpp
.flags
= XDF_NEED_MINIMAL
;
3157 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3158 xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
3159 &xpp
, &xecfg
, &ecb
);
3162 SHA1_Final(sha1
, &ctx
);
3166 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3168 struct diff_queue_struct
*q
= &diff_queued_diff
;
3170 int result
= diff_get_patch_id(options
, sha1
);
3172 for (i
= 0; i
< q
->nr
; i
++)
3173 diff_free_filepair(q
->queue
[i
]);
3177 q
->nr
= q
->alloc
= 0;
3182 static int is_summary_empty(const struct diff_queue_struct
*q
)
3186 for (i
= 0; i
< q
->nr
; i
++) {
3187 const struct diff_filepair
*p
= q
->queue
[i
];
3189 switch (p
->status
) {
3190 case DIFF_STATUS_DELETED
:
3191 case DIFF_STATUS_ADDED
:
3192 case DIFF_STATUS_COPIED
:
3193 case DIFF_STATUS_RENAMED
:
3198 if (p
->one
->mode
&& p
->two
->mode
&&
3199 p
->one
->mode
!= p
->two
->mode
)
3207 void diff_flush(struct diff_options
*options
)
3209 struct diff_queue_struct
*q
= &diff_queued_diff
;
3210 int i
, output_format
= options
->output_format
;
3214 * Order: raw, stat, summary, patch
3215 * or: name/name-status/checkdiff (other bits clear)
3220 if (output_format
& (DIFF_FORMAT_RAW
|
3222 DIFF_FORMAT_NAME_STATUS
|
3223 DIFF_FORMAT_CHECKDIFF
)) {
3224 for (i
= 0; i
< q
->nr
; i
++) {
3225 struct diff_filepair
*p
= q
->queue
[i
];
3226 if (check_pair_status(p
))
3227 flush_one_pair(p
, options
);
3232 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3233 struct diffstat_t diffstat
;
3235 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3236 for (i
= 0; i
< q
->nr
; i
++) {
3237 struct diff_filepair
*p
= q
->queue
[i
];
3238 if (check_pair_status(p
))
3239 diff_flush_stat(p
, options
, &diffstat
);
3241 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3242 show_numstat(&diffstat
, options
);
3243 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3244 show_stats(&diffstat
, options
);
3245 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3246 show_shortstats(&diffstat
, options
);
3247 free_diffstat_info(&diffstat
);
3250 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3251 show_dirstat(options
);
3253 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3254 for (i
= 0; i
< q
->nr
; i
++)
3255 diff_summary(options
->file
, q
->queue
[i
]);
3259 if (output_format
& DIFF_FORMAT_PATCH
) {
3261 putc(options
->line_termination
, options
->file
);
3262 if (options
->stat_sep
) {
3263 /* attach patch instead of inline */
3264 fputs(options
->stat_sep
, options
->file
);
3268 for (i
= 0; i
< q
->nr
; i
++) {
3269 struct diff_filepair
*p
= q
->queue
[i
];
3270 if (check_pair_status(p
))
3271 diff_flush_patch(p
, options
);
3275 if (output_format
& DIFF_FORMAT_CALLBACK
)
3276 options
->format_callback(q
, options
, options
->format_callback_data
);
3278 for (i
= 0; i
< q
->nr
; i
++)
3279 diff_free_filepair(q
->queue
[i
]);
3283 q
->nr
= q
->alloc
= 0;
3284 if (options
->close_file
)
3285 fclose(options
->file
);
3288 static void diffcore_apply_filter(const char *filter
)
3291 struct diff_queue_struct
*q
= &diff_queued_diff
;
3292 struct diff_queue_struct outq
;
3294 outq
.nr
= outq
.alloc
= 0;
3299 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3301 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3302 struct diff_filepair
*p
= q
->queue
[i
];
3303 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3305 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3307 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3308 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3309 strchr(filter
, p
->status
)))
3315 /* otherwise we will clear the whole queue
3316 * by copying the empty outq at the end of this
3317 * function, but first clear the current entries
3320 for (i
= 0; i
< q
->nr
; i
++)
3321 diff_free_filepair(q
->queue
[i
]);
3324 /* Only the matching ones */
3325 for (i
= 0; i
< q
->nr
; i
++) {
3326 struct diff_filepair
*p
= q
->queue
[i
];
3328 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3330 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3332 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3333 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3334 strchr(filter
, p
->status
)))
3337 diff_free_filepair(p
);
3344 /* Check whether two filespecs with the same mode and size are identical */
3345 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3346 struct diff_filespec
*two
)
3348 if (S_ISGITLINK(one
->mode
))
3350 if (diff_populate_filespec(one
, 0))
3352 if (diff_populate_filespec(two
, 0))
3354 return !memcmp(one
->data
, two
->data
, one
->size
);
3357 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3360 struct diff_queue_struct
*q
= &diff_queued_diff
;
3361 struct diff_queue_struct outq
;
3363 outq
.nr
= outq
.alloc
= 0;
3365 for (i
= 0; i
< q
->nr
; i
++) {
3366 struct diff_filepair
*p
= q
->queue
[i
];
3369 * 1. Entries that come from stat info dirtyness
3370 * always have both sides (iow, not create/delete),
3371 * one side of the object name is unknown, with
3372 * the same mode and size. Keep the ones that
3373 * do not match these criteria. They have real
3376 * 2. At this point, the file is known to be modified,
3377 * with the same mode and size, and the object
3378 * name of one side is unknown. Need to inspect
3379 * the identical contents.
3381 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3382 !DIFF_FILE_VALID(p
->two
) ||
3383 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3384 (p
->one
->mode
!= p
->two
->mode
) ||
3385 diff_populate_filespec(p
->one
, 1) ||
3386 diff_populate_filespec(p
->two
, 1) ||
3387 (p
->one
->size
!= p
->two
->size
) ||
3388 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3392 * The caller can subtract 1 from skip_stat_unmatch
3393 * to determine how many paths were dirty only
3394 * due to stat info mismatch.
3396 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3397 diffopt
->skip_stat_unmatch
++;
3398 diff_free_filepair(p
);
3405 void diffcore_std(struct diff_options
*options
)
3407 if (options
->skip_stat_unmatch
)
3408 diffcore_skip_stat_unmatch(options
);
3409 if (options
->break_opt
!= -1)
3410 diffcore_break(options
->break_opt
);
3411 if (options
->detect_rename
)
3412 diffcore_rename(options
);
3413 if (options
->break_opt
!= -1)
3414 diffcore_merge_broken();
3415 if (options
->pickaxe
)
3416 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3417 if (options
->orderfile
)
3418 diffcore_order(options
->orderfile
);
3419 diff_resolve_rename_copy();
3420 diffcore_apply_filter(options
->filter
);
3422 if (diff_queued_diff
.nr
)
3423 DIFF_OPT_SET(options
, HAS_CHANGES
);
3425 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3428 int diff_result_code(struct diff_options
*opt
, int status
)
3431 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3432 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3434 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3435 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3437 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3438 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3443 void diff_addremove(struct diff_options
*options
,
3444 int addremove
, unsigned mode
,
3445 const unsigned char *sha1
,
3446 const char *concatpath
)
3448 struct diff_filespec
*one
, *two
;
3450 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
3453 /* This may look odd, but it is a preparation for
3454 * feeding "there are unchanged files which should
3455 * not produce diffs, but when you are doing copy
3456 * detection you would need them, so here they are"
3457 * entries to the diff-core. They will be prefixed
3458 * with something like '=' or '*' (I haven't decided
3459 * which but should not make any difference).
3460 * Feeding the same new and old to diff_change()
3461 * also has the same effect.
3462 * Before the final output happens, they are pruned after
3463 * merged into rename/copy pairs as appropriate.
3465 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3466 addremove
= (addremove
== '+' ? '-' :
3467 addremove
== '-' ? '+' : addremove
);
3469 if (options
->prefix
&&
3470 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3473 one
= alloc_filespec(concatpath
);
3474 two
= alloc_filespec(concatpath
);
3476 if (addremove
!= '+')
3477 fill_filespec(one
, sha1
, mode
);
3478 if (addremove
!= '-')
3479 fill_filespec(two
, sha1
, mode
);
3481 diff_queue(&diff_queued_diff
, one
, two
);
3482 DIFF_OPT_SET(options
, HAS_CHANGES
);
3485 void diff_change(struct diff_options
*options
,
3486 unsigned old_mode
, unsigned new_mode
,
3487 const unsigned char *old_sha1
,
3488 const unsigned char *new_sha1
,
3489 const char *concatpath
)
3491 struct diff_filespec
*one
, *two
;
3493 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
3494 && S_ISGITLINK(new_mode
))
3497 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3499 const unsigned char *tmp_c
;
3500 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3501 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3504 if (options
->prefix
&&
3505 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3508 one
= alloc_filespec(concatpath
);
3509 two
= alloc_filespec(concatpath
);
3510 fill_filespec(one
, old_sha1
, old_mode
);
3511 fill_filespec(two
, new_sha1
, new_mode
);
3513 diff_queue(&diff_queued_diff
, one
, two
);
3514 DIFF_OPT_SET(options
, HAS_CHANGES
);
3517 void diff_unmerge(struct diff_options
*options
,
3519 unsigned mode
, const unsigned char *sha1
)
3521 struct diff_filespec
*one
, *two
;
3523 if (options
->prefix
&&
3524 strncmp(path
, options
->prefix
, options
->prefix_length
))
3527 one
= alloc_filespec(path
);
3528 two
= alloc_filespec(path
);
3529 fill_filespec(one
, sha1
, mode
);
3530 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;