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;
27 static int diff_mnemonic_prefix
;
29 static char diff_colors
[][COLOR_MAXLEN
] = {
31 "", /* PLAIN (normal) */
32 "\033[1m", /* METAINFO (bold) */
33 "\033[36m", /* FRAGINFO (cyan) */
34 "\033[31m", /* OLD (red) */
35 "\033[32m", /* NEW (green) */
36 "\033[33m", /* COMMIT (yellow) */
37 "\033[41m", /* WHITESPACE (red background) */
40 static int parse_diff_color_slot(const char *var
, int ofs
)
42 if (!strcasecmp(var
+ofs
, "plain"))
44 if (!strcasecmp(var
+ofs
, "meta"))
46 if (!strcasecmp(var
+ofs
, "frag"))
48 if (!strcasecmp(var
+ofs
, "old"))
50 if (!strcasecmp(var
+ofs
, "new"))
52 if (!strcasecmp(var
+ofs
, "commit"))
54 if (!strcasecmp(var
+ofs
, "whitespace"))
55 return DIFF_WHITESPACE
;
56 die("bad config variable '%s'", var
);
59 static struct ll_diff_driver
{
61 struct ll_diff_driver
*next
;
63 } *user_diff
, **user_diff_tail
;
66 * Currently there is only "diff.<drivername>.command" variable;
67 * because there are "diff.color.<slot>" variables, we are parsing
68 * this in a bit convoluted way to allow low level diff driver
71 static int parse_lldiff_command(const char *var
, const char *ep
, const char *value
)
75 struct ll_diff_driver
*drv
;
79 for (drv
= user_diff
; drv
; drv
= drv
->next
)
80 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
83 drv
= xcalloc(1, sizeof(struct ll_diff_driver
));
84 drv
->name
= xmemdupz(name
, namelen
);
86 user_diff_tail
= &user_diff
;
87 *user_diff_tail
= drv
;
88 user_diff_tail
= &(drv
->next
);
91 return git_config_string(&(drv
->cmd
), var
, value
);
95 * 'diff.<what>.funcname' attribute can be specified in the configuration
96 * to define a customized regexp to find the beginning of a function to
97 * be used for hunk header lines of "diff -p" style output.
99 struct funcname_pattern_entry
{
104 static struct funcname_pattern_list
{
105 struct funcname_pattern_list
*next
;
106 struct funcname_pattern_entry e
;
107 } *funcname_pattern_list
;
109 static int parse_funcname_pattern(const char *var
, const char *ep
, const char *value
, int cflags
)
113 struct funcname_pattern_list
*pp
;
115 name
= var
+ 5; /* "diff." */
118 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
119 if (!strncmp(pp
->e
.name
, name
, namelen
) && !pp
->e
.name
[namelen
])
122 pp
= xcalloc(1, sizeof(*pp
));
123 pp
->e
.name
= xmemdupz(name
, namelen
);
124 pp
->next
= funcname_pattern_list
;
125 funcname_pattern_list
= pp
;
128 pp
->e
.pattern
= xstrdup(value
);
129 pp
->e
.cflags
= cflags
;
134 * These are to give UI layer defaults.
135 * The core-level commands such as git-diff-files should
136 * never be affected by the setting of diff.renames
137 * the user happens to have in the configuration file.
139 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
141 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
142 diff_use_color_default
= git_config_colorbool(var
, value
, -1);
145 if (!strcmp(var
, "diff.renames")) {
147 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
148 else if (!strcasecmp(value
, "copies") ||
149 !strcasecmp(value
, "copy"))
150 diff_detect_rename_default
= DIFF_DETECT_COPY
;
151 else if (git_config_bool(var
,value
))
152 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
155 if (!strcmp(var
, "diff.autorefreshindex")) {
156 diff_auto_refresh_index
= git_config_bool(var
, value
);
159 if (!strcmp(var
, "diff.mnemonicprefix")) {
160 diff_mnemonic_prefix
= git_config_bool(var
, value
);
163 if (!strcmp(var
, "diff.external"))
164 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
165 if (!prefixcmp(var
, "diff.")) {
166 const char *ep
= strrchr(var
, '.');
168 if (ep
!= var
+ 4 && !strcmp(ep
, ".command"))
169 return parse_lldiff_command(var
, ep
, value
);
172 return git_diff_basic_config(var
, value
, cb
);
175 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
177 if (!strcmp(var
, "diff.renamelimit")) {
178 diff_rename_limit_default
= git_config_int(var
, value
);
182 if (!prefixcmp(var
, "diff.color.") || !prefixcmp(var
, "color.diff.")) {
183 int slot
= parse_diff_color_slot(var
, 11);
185 return config_error_nonbool(var
);
186 color_parse(value
, var
, diff_colors
[slot
]);
190 /* like GNU diff's --suppress-blank-empty option */
191 if (!strcmp(var
, "diff.suppress-blank-empty")) {
192 diff_suppress_blank_empty
= git_config_bool(var
, value
);
196 if (!prefixcmp(var
, "diff.")) {
197 const char *ep
= strrchr(var
, '.');
199 if (!strcmp(ep
, ".funcname")) {
201 return config_error_nonbool(var
);
202 return parse_funcname_pattern(var
, ep
, value
,
204 } else if (!strcmp(ep
, ".xfuncname")) {
206 return config_error_nonbool(var
);
207 return parse_funcname_pattern(var
, ep
, value
,
213 return git_color_default_config(var
, value
, cb
);
216 static char *quote_two(const char *one
, const char *two
)
218 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
219 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
222 strbuf_init(&res
, 0);
223 if (need_one
+ need_two
) {
224 strbuf_addch(&res
, '"');
225 quote_c_style(one
, &res
, NULL
, 1);
226 quote_c_style(two
, &res
, NULL
, 1);
227 strbuf_addch(&res
, '"');
229 strbuf_addstr(&res
, one
);
230 strbuf_addstr(&res
, two
);
232 return strbuf_detach(&res
, NULL
);
235 static const char *external_diff(void)
237 static const char *external_diff_cmd
= NULL
;
238 static int done_preparing
= 0;
241 return external_diff_cmd
;
242 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
243 if (!external_diff_cmd
)
244 external_diff_cmd
= external_diff_cmd_cfg
;
246 return external_diff_cmd
;
249 static struct diff_tempfile
{
250 const char *name
; /* filename external diff should read from */
253 char tmp_path
[PATH_MAX
];
256 static int count_lines(const char *data
, int size
)
258 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
265 completely_empty
= 0;
269 completely_empty
= 0;
272 if (completely_empty
)
275 count
++; /* no trailing newline */
279 static void print_line_count(FILE *file
, int count
)
283 fprintf(file
, "0,0");
289 fprintf(file
, "1,%d", count
);
294 static void copy_file_with_prefix(FILE *file
,
295 int prefix
, const char *data
, int size
,
296 const char *set
, const char *reset
)
298 int ch
, nl_just_seen
= 1;
313 fprintf(file
, "%s\n\\ No newline at end of file\n", reset
);
316 static void emit_rewrite_diff(const char *name_a
,
318 struct diff_filespec
*one
,
319 struct diff_filespec
*two
,
320 struct diff_options
*o
)
323 int color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
324 const char *name_a_tab
, *name_b_tab
;
325 const char *metainfo
= diff_get_color(color_diff
, DIFF_METAINFO
);
326 const char *fraginfo
= diff_get_color(color_diff
, DIFF_FRAGINFO
);
327 const char *old
= diff_get_color(color_diff
, DIFF_FILE_OLD
);
328 const char *new = diff_get_color(color_diff
, DIFF_FILE_NEW
);
329 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
330 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
331 const char *a_prefix
, *b_prefix
;
333 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
334 a_prefix
= o
->b_prefix
;
335 b_prefix
= o
->a_prefix
;
337 a_prefix
= o
->a_prefix
;
338 b_prefix
= o
->b_prefix
;
341 name_a
+= (*name_a
== '/');
342 name_b
+= (*name_b
== '/');
343 name_a_tab
= strchr(name_a
, ' ') ? "\t" : "";
344 name_b_tab
= strchr(name_b
, ' ') ? "\t" : "";
346 strbuf_reset(&a_name
);
347 strbuf_reset(&b_name
);
348 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
349 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
351 diff_populate_filespec(one
, 0);
352 diff_populate_filespec(two
, 0);
353 lc_a
= count_lines(one
->data
, one
->size
);
354 lc_b
= count_lines(two
->data
, two
->size
);
356 "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
357 metainfo
, a_name
.buf
, name_a_tab
, reset
,
358 metainfo
, b_name
.buf
, name_b_tab
, reset
, fraginfo
);
359 print_line_count(o
->file
, lc_a
);
360 fprintf(o
->file
, " +");
361 print_line_count(o
->file
, lc_b
);
362 fprintf(o
->file
, " @@%s\n", reset
);
364 copy_file_with_prefix(o
->file
, '-', one
->data
, one
->size
, old
, reset
);
366 copy_file_with_prefix(o
->file
, '+', two
->data
, two
->size
, new, reset
);
369 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
371 if (!DIFF_FILE_VALID(one
)) {
372 mf
->ptr
= (char *)""; /* does not matter */
376 else if (diff_populate_filespec(one
, 0))
379 mf
->size
= one
->size
;
383 struct diff_words_buffer
{
386 long current
; /* output pointer */
387 int suppressed_newline
;
390 static void diff_words_append(char *line
, unsigned long len
,
391 struct diff_words_buffer
*buffer
)
393 if (buffer
->text
.size
+ len
> buffer
->alloc
) {
394 buffer
->alloc
= (buffer
->text
.size
+ len
) * 3 / 2;
395 buffer
->text
.ptr
= xrealloc(buffer
->text
.ptr
, buffer
->alloc
);
399 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
400 buffer
->text
.size
+= len
;
403 struct diff_words_data
{
404 struct diff_words_buffer minus
, plus
;
408 static void print_word(FILE *file
, struct diff_words_buffer
*buffer
, int len
, int color
,
409 int suppress_newline
)
417 ptr
= buffer
->text
.ptr
+ buffer
->current
;
418 buffer
->current
+= len
;
420 if (ptr
[len
- 1] == '\n') {
425 fputs(diff_get_color(1, color
), file
);
426 fwrite(ptr
, len
, 1, file
);
427 fputs(diff_get_color(1, DIFF_RESET
), file
);
430 if (suppress_newline
)
431 buffer
->suppressed_newline
= 1;
437 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
439 struct diff_words_data
*diff_words
= priv
;
441 if (diff_words
->minus
.suppressed_newline
) {
443 putc('\n', diff_words
->file
);
444 diff_words
->minus
.suppressed_newline
= 0;
450 print_word(diff_words
->file
,
451 &diff_words
->minus
, len
, DIFF_FILE_OLD
, 1);
454 print_word(diff_words
->file
,
455 &diff_words
->plus
, len
, DIFF_FILE_NEW
, 0);
458 print_word(diff_words
->file
,
459 &diff_words
->plus
, len
, DIFF_PLAIN
, 0);
460 diff_words
->minus
.current
+= len
;
465 /* this executes the word diff on the accumulated buffers */
466 static void diff_words_show(struct diff_words_data
*diff_words
)
471 mmfile_t minus
, plus
;
474 memset(&xecfg
, 0, sizeof(xecfg
));
475 minus
.size
= diff_words
->minus
.text
.size
;
476 minus
.ptr
= xmalloc(minus
.size
);
477 memcpy(minus
.ptr
, diff_words
->minus
.text
.ptr
, minus
.size
);
478 for (i
= 0; i
< minus
.size
; i
++)
479 if (isspace(minus
.ptr
[i
]))
481 diff_words
->minus
.current
= 0;
483 plus
.size
= diff_words
->plus
.text
.size
;
484 plus
.ptr
= xmalloc(plus
.size
);
485 memcpy(plus
.ptr
, diff_words
->plus
.text
.ptr
, plus
.size
);
486 for (i
= 0; i
< plus
.size
; i
++)
487 if (isspace(plus
.ptr
[i
]))
489 diff_words
->plus
.current
= 0;
491 xpp
.flags
= XDF_NEED_MINIMAL
;
492 xecfg
.ctxlen
= diff_words
->minus
.alloc
+ diff_words
->plus
.alloc
;
493 xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
497 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
499 if (diff_words
->minus
.suppressed_newline
) {
500 putc('\n', diff_words
->file
);
501 diff_words
->minus
.suppressed_newline
= 0;
505 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
507 struct emit_callback
{
508 int nparents
, color_diff
;
510 sane_truncate_fn truncate
;
511 const char **label_path
;
512 struct diff_words_data
*diff_words
;
517 static void free_diff_words_data(struct emit_callback
*ecbdata
)
519 if (ecbdata
->diff_words
) {
521 if (ecbdata
->diff_words
->minus
.text
.size
||
522 ecbdata
->diff_words
->plus
.text
.size
)
523 diff_words_show(ecbdata
->diff_words
);
525 free (ecbdata
->diff_words
->minus
.text
.ptr
);
526 free (ecbdata
->diff_words
->plus
.text
.ptr
);
527 free(ecbdata
->diff_words
);
528 ecbdata
->diff_words
= NULL
;
532 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
535 return diff_colors
[ix
];
539 static void emit_line(FILE *file
, const char *set
, const char *reset
, const char *line
, int len
)
541 int has_trailing_newline
, has_trailing_carriage_return
;
543 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
544 if (has_trailing_newline
)
546 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
547 if (has_trailing_carriage_return
)
551 fwrite(line
, len
, 1, file
);
553 if (has_trailing_carriage_return
)
555 if (has_trailing_newline
)
559 static void emit_add_line(const char *reset
, struct emit_callback
*ecbdata
, const char *line
, int len
)
561 const char *ws
= diff_get_color(ecbdata
->color_diff
, DIFF_WHITESPACE
);
562 const char *set
= diff_get_color(ecbdata
->color_diff
, DIFF_FILE_NEW
);
565 emit_line(ecbdata
->file
, set
, reset
, line
, len
);
567 /* Emit just the prefix, then the rest. */
568 emit_line(ecbdata
->file
, set
, reset
, line
, ecbdata
->nparents
);
569 ws_check_emit(line
+ ecbdata
->nparents
,
570 len
- ecbdata
->nparents
, ecbdata
->ws_rule
,
571 ecbdata
->file
, set
, reset
, ws
);
575 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
582 return ecb
->truncate(line
, len
);
586 (void) utf8_width(&cp
, &l
);
588 break; /* truncated in the middle? */
593 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
597 struct emit_callback
*ecbdata
= priv
;
598 const char *meta
= diff_get_color(ecbdata
->color_diff
, DIFF_METAINFO
);
599 const char *plain
= diff_get_color(ecbdata
->color_diff
, DIFF_PLAIN
);
600 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
602 *(ecbdata
->found_changesp
) = 1;
604 if (ecbdata
->label_path
[0]) {
605 const char *name_a_tab
, *name_b_tab
;
607 name_a_tab
= strchr(ecbdata
->label_path
[0], ' ') ? "\t" : "";
608 name_b_tab
= strchr(ecbdata
->label_path
[1], ' ') ? "\t" : "";
610 fprintf(ecbdata
->file
, "%s--- %s%s%s\n",
611 meta
, ecbdata
->label_path
[0], reset
, name_a_tab
);
612 fprintf(ecbdata
->file
, "%s+++ %s%s%s\n",
613 meta
, ecbdata
->label_path
[1], reset
, name_b_tab
);
614 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
617 if (diff_suppress_blank_empty
618 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
623 /* This is not really necessary for now because
624 * this codepath only deals with two-way diffs.
626 for (i
= 0; i
< len
&& line
[i
] == '@'; i
++)
628 if (2 <= i
&& i
< len
&& line
[i
] == ' ') {
629 ecbdata
->nparents
= i
- 1;
630 len
= sane_truncate_line(ecbdata
, line
, len
);
631 emit_line(ecbdata
->file
,
632 diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
),
634 if (line
[len
-1] != '\n')
635 putc('\n', ecbdata
->file
);
639 if (len
< ecbdata
->nparents
) {
640 emit_line(ecbdata
->file
, reset
, reset
, line
, len
);
645 if (ecbdata
->diff_words
&& ecbdata
->nparents
!= 1)
646 /* fall back to normal diff */
647 free_diff_words_data(ecbdata
);
648 if (ecbdata
->diff_words
) {
649 if (line
[0] == '-') {
650 diff_words_append(line
, len
,
651 &ecbdata
->diff_words
->minus
);
653 } else if (line
[0] == '+') {
654 diff_words_append(line
, len
,
655 &ecbdata
->diff_words
->plus
);
658 if (ecbdata
->diff_words
->minus
.text
.size
||
659 ecbdata
->diff_words
->plus
.text
.size
)
660 diff_words_show(ecbdata
->diff_words
);
663 emit_line(ecbdata
->file
, plain
, reset
, line
, len
);
666 for (i
= 0; i
< ecbdata
->nparents
&& len
; i
++) {
668 color
= DIFF_FILE_OLD
;
669 else if (line
[i
] == '+')
670 color
= DIFF_FILE_NEW
;
673 if (color
!= DIFF_FILE_NEW
) {
674 emit_line(ecbdata
->file
,
675 diff_get_color(ecbdata
->color_diff
, color
),
679 emit_add_line(reset
, ecbdata
, line
, len
);
682 static char *pprint_rename(const char *a
, const char *b
)
687 int pfx_length
, sfx_length
;
688 int len_a
= strlen(a
);
689 int len_b
= strlen(b
);
690 int a_midlen
, b_midlen
;
691 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
692 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
694 strbuf_init(&name
, 0);
695 if (qlen_a
|| qlen_b
) {
696 quote_c_style(a
, &name
, NULL
, 0);
697 strbuf_addstr(&name
, " => ");
698 quote_c_style(b
, &name
, NULL
, 0);
699 return strbuf_detach(&name
, NULL
);
702 /* Find common prefix */
704 while (*old
&& *new && *old
== *new) {
706 pfx_length
= old
- a
+ 1;
711 /* Find common suffix */
715 while (a
<= old
&& b
<= new && *old
== *new) {
717 sfx_length
= len_a
- (old
- a
);
723 * pfx{mid-a => mid-b}sfx
724 * {pfx-a => pfx-b}sfx
725 * pfx{sfx-a => sfx-b}
728 a_midlen
= len_a
- pfx_length
- sfx_length
;
729 b_midlen
= len_b
- pfx_length
- sfx_length
;
735 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
736 if (pfx_length
+ sfx_length
) {
737 strbuf_add(&name
, a
, pfx_length
);
738 strbuf_addch(&name
, '{');
740 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
741 strbuf_addstr(&name
, " => ");
742 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
743 if (pfx_length
+ sfx_length
) {
744 strbuf_addch(&name
, '}');
745 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
747 return strbuf_detach(&name
, NULL
);
753 struct diffstat_file
{
757 unsigned is_unmerged
:1;
758 unsigned is_binary
:1;
759 unsigned is_renamed
:1;
760 unsigned int added
, deleted
;
764 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
768 struct diffstat_file
*x
;
769 x
= xcalloc(sizeof (*x
), 1);
770 if (diffstat
->nr
== diffstat
->alloc
) {
771 diffstat
->alloc
= alloc_nr(diffstat
->alloc
);
772 diffstat
->files
= xrealloc(diffstat
->files
,
773 diffstat
->alloc
* sizeof(x
));
775 diffstat
->files
[diffstat
->nr
++] = x
;
777 x
->from_name
= xstrdup(name_a
);
778 x
->name
= xstrdup(name_b
);
783 x
->name
= xstrdup(name_a
);
788 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
790 struct diffstat_t
*diffstat
= priv
;
791 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
795 else if (line
[0] == '-')
799 const char mime_boundary_leader
[] = "------------";
801 static int scale_linear(int it
, int width
, int max_change
)
804 * make sure that at least one '-' is printed if there were deletions,
805 * and likewise for '+'.
809 return ((it
- 1) * (width
- 1) + max_change
- 1) / (max_change
- 1);
812 static void show_name(FILE *file
,
813 const char *prefix
, const char *name
, int len
,
814 const char *reset
, const char *set
)
816 fprintf(file
, " %s%s%-*s%s |", set
, prefix
, len
, name
, reset
);
819 static void show_graph(FILE *file
, char ch
, int cnt
, const char *set
, const char *reset
)
823 fprintf(file
, "%s", set
);
826 fprintf(file
, "%s", reset
);
829 static void fill_print_name(struct diffstat_file
*file
)
833 if (file
->print_name
)
836 if (!file
->is_renamed
) {
838 strbuf_init(&buf
, 0);
839 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
840 pname
= strbuf_detach(&buf
, NULL
);
843 strbuf_release(&buf
);
846 pname
= pprint_rename(file
->from_name
, file
->name
);
848 file
->print_name
= pname
;
851 static void show_stats(struct diffstat_t
* data
, struct diff_options
*options
)
853 int i
, len
, add
, del
, total
, adds
= 0, dels
= 0;
854 int max_change
= 0, max_len
= 0;
855 int total_files
= data
->nr
;
856 int width
, name_width
;
857 const char *reset
, *set
, *add_c
, *del_c
;
862 width
= options
->stat_width
? options
->stat_width
: 80;
863 name_width
= options
->stat_name_width
? options
->stat_name_width
: 50;
865 /* Sanity: give at least 5 columns to the graph,
866 * but leave at least 10 columns for the name.
872 else if (width
< name_width
+ 15)
873 name_width
= width
- 15;
875 /* Find the longest filename and max number of changes */
876 reset
= diff_get_color_opt(options
, DIFF_RESET
);
877 set
= diff_get_color_opt(options
, DIFF_PLAIN
);
878 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
879 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
881 for (i
= 0; i
< data
->nr
; i
++) {
882 struct diffstat_file
*file
= data
->files
[i
];
883 int change
= file
->added
+ file
->deleted
;
884 fill_print_name(file
);
885 len
= strlen(file
->print_name
);
889 if (file
->is_binary
|| file
->is_unmerged
)
891 if (max_change
< change
)
895 /* Compute the width of the graph part;
896 * 10 is for one blank at the beginning of the line plus
897 * " | count " between the name and the graph.
899 * From here on, name_width is the width of the name area,
900 * and width is the width of the graph area.
902 name_width
= (name_width
< max_len
) ? name_width
: max_len
;
903 if (width
< (name_width
+ 10) + max_change
)
904 width
= width
- (name_width
+ 10);
908 for (i
= 0; i
< data
->nr
; i
++) {
909 const char *prefix
= "";
910 char *name
= data
->files
[i
]->print_name
;
911 int added
= data
->files
[i
]->added
;
912 int deleted
= data
->files
[i
]->deleted
;
916 * "scale" the filename
919 name_len
= strlen(name
);
920 if (name_width
< name_len
) {
924 name
+= name_len
- len
;
925 slash
= strchr(name
, '/');
930 if (data
->files
[i
]->is_binary
) {
931 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
932 fprintf(options
->file
, " Bin ");
933 fprintf(options
->file
, "%s%d%s", del_c
, deleted
, reset
);
934 fprintf(options
->file
, " -> ");
935 fprintf(options
->file
, "%s%d%s", add_c
, added
, reset
);
936 fprintf(options
->file
, " bytes");
937 fprintf(options
->file
, "\n");
940 else if (data
->files
[i
]->is_unmerged
) {
941 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
942 fprintf(options
->file
, " Unmerged\n");
945 else if (!data
->files
[i
]->is_renamed
&&
946 (added
+ deleted
== 0)) {
952 * scale the add/delete
960 if (width
<= max_change
) {
961 add
= scale_linear(add
, width
, max_change
);
962 del
= scale_linear(del
, width
, max_change
);
965 show_name(options
->file
, prefix
, name
, len
, reset
, set
);
966 fprintf(options
->file
, "%5d%s", added
+ deleted
,
967 added
+ deleted
? " " : "");
968 show_graph(options
->file
, '+', add
, add_c
, reset
);
969 show_graph(options
->file
, '-', del
, del_c
, reset
);
970 fprintf(options
->file
, "\n");
972 fprintf(options
->file
,
973 "%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
974 set
, total_files
, adds
, dels
, reset
);
977 static void show_shortstats(struct diffstat_t
* data
, struct diff_options
*options
)
979 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
984 for (i
= 0; i
< data
->nr
; i
++) {
985 if (!data
->files
[i
]->is_binary
&&
986 !data
->files
[i
]->is_unmerged
) {
987 int added
= data
->files
[i
]->added
;
988 int deleted
= data
->files
[i
]->deleted
;
989 if (!data
->files
[i
]->is_renamed
&&
990 (added
+ deleted
== 0)) {
998 fprintf(options
->file
, " %d files changed, %d insertions(+), %d deletions(-)\n",
999 total_files
, adds
, dels
);
1002 static void show_numstat(struct diffstat_t
* data
, struct diff_options
*options
)
1009 for (i
= 0; i
< data
->nr
; i
++) {
1010 struct diffstat_file
*file
= data
->files
[i
];
1012 if (file
->is_binary
)
1013 fprintf(options
->file
, "-\t-\t");
1015 fprintf(options
->file
,
1016 "%d\t%d\t", file
->added
, file
->deleted
);
1017 if (options
->line_termination
) {
1018 fill_print_name(file
);
1019 if (!file
->is_renamed
)
1020 write_name_quoted(file
->name
, options
->file
,
1021 options
->line_termination
);
1023 fputs(file
->print_name
, options
->file
);
1024 putc(options
->line_termination
, options
->file
);
1027 if (file
->is_renamed
) {
1028 putc('\0', options
->file
);
1029 write_name_quoted(file
->from_name
, options
->file
, '\0');
1031 write_name_quoted(file
->name
, options
->file
, '\0');
1036 struct dirstat_file
{
1038 unsigned long changed
;
1041 struct dirstat_dir
{
1042 struct dirstat_file
*files
;
1043 int alloc
, nr
, percent
, cumulative
;
1046 static long gather_dirstat(FILE *file
, struct dirstat_dir
*dir
, unsigned long changed
, const char *base
, int baselen
)
1048 unsigned long this_dir
= 0;
1049 unsigned int sources
= 0;
1052 struct dirstat_file
*f
= dir
->files
;
1053 int namelen
= strlen(f
->name
);
1057 if (namelen
< baselen
)
1059 if (memcmp(f
->name
, base
, baselen
))
1061 slash
= strchr(f
->name
+ baselen
, '/');
1063 int newbaselen
= slash
+ 1 - f
->name
;
1064 this = gather_dirstat(file
, dir
, changed
, f
->name
, newbaselen
);
1076 * We don't report dirstat's for
1078 * - or cases where everything came from a single directory
1079 * under this directory (sources == 1).
1081 if (baselen
&& sources
!= 1) {
1082 int permille
= this_dir
* 1000 / changed
;
1084 int percent
= permille
/ 10;
1085 if (percent
>= dir
->percent
) {
1086 fprintf(file
, "%4d.%01d%% %.*s\n", percent
, permille
% 10, baselen
, base
);
1087 if (!dir
->cumulative
)
1095 static int dirstat_compare(const void *_a
, const void *_b
)
1097 const struct dirstat_file
*a
= _a
;
1098 const struct dirstat_file
*b
= _b
;
1099 return strcmp(a
->name
, b
->name
);
1102 static void show_dirstat(struct diff_options
*options
)
1105 unsigned long changed
;
1106 struct dirstat_dir dir
;
1107 struct diff_queue_struct
*q
= &diff_queued_diff
;
1112 dir
.percent
= options
->dirstat_percent
;
1113 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
1116 for (i
= 0; i
< q
->nr
; i
++) {
1117 struct diff_filepair
*p
= q
->queue
[i
];
1119 unsigned long copied
, added
, damage
;
1121 name
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
1123 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
1124 diff_populate_filespec(p
->one
, 0);
1125 diff_populate_filespec(p
->two
, 0);
1126 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
, 0,
1128 diff_free_filespec_data(p
->one
);
1129 diff_free_filespec_data(p
->two
);
1130 } else if (DIFF_FILE_VALID(p
->one
)) {
1131 diff_populate_filespec(p
->one
, 1);
1133 diff_free_filespec_data(p
->one
);
1134 } else if (DIFF_FILE_VALID(p
->two
)) {
1135 diff_populate_filespec(p
->two
, 1);
1137 added
= p
->two
->size
;
1138 diff_free_filespec_data(p
->two
);
1143 * Original minus copied is the removed material,
1144 * added is the new material. They are both damages
1145 * made to the preimage. In --dirstat-by-file mode, count
1146 * damaged files, not damaged lines. This is done by
1147 * counting only a single damaged line per file.
1149 damage
= (p
->one
->size
- copied
) + added
;
1150 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
) && damage
> 0)
1153 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
1154 dir
.files
[dir
.nr
].name
= name
;
1155 dir
.files
[dir
.nr
].changed
= damage
;
1160 /* This can happen even with many files, if everything was renames */
1164 /* Show all directories with more than x% of the changes */
1165 qsort(dir
.files
, dir
.nr
, sizeof(dir
.files
[0]), dirstat_compare
);
1166 gather_dirstat(options
->file
, &dir
, changed
, "", 0);
1169 static void free_diffstat_info(struct diffstat_t
*diffstat
)
1172 for (i
= 0; i
< diffstat
->nr
; i
++) {
1173 struct diffstat_file
*f
= diffstat
->files
[i
];
1174 if (f
->name
!= f
->print_name
)
1175 free(f
->print_name
);
1180 free(diffstat
->files
);
1183 struct checkdiff_t
{
1184 const char *filename
;
1186 struct diff_options
*o
;
1189 int trailing_blanks_start
;
1192 static int is_conflict_marker(const char *line
, unsigned long len
)
1199 firstchar
= line
[0];
1200 switch (firstchar
) {
1201 case '=': case '>': case '<':
1206 for (cnt
= 1; cnt
< 7; cnt
++)
1207 if (line
[cnt
] != firstchar
)
1209 /* line[0] thru line[6] are same as firstchar */
1210 if (firstchar
== '=') {
1211 /* divider between ours and theirs? */
1212 if (len
!= 8 || line
[7] != '\n')
1214 } else if (len
< 8 || !isspace(line
[7])) {
1215 /* not divider before ours nor after theirs */
1221 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
1223 struct checkdiff_t
*data
= priv
;
1224 int color_diff
= DIFF_OPT_TST(data
->o
, COLOR_DIFF
);
1225 const char *ws
= diff_get_color(color_diff
, DIFF_WHITESPACE
);
1226 const char *reset
= diff_get_color(color_diff
, DIFF_RESET
);
1227 const char *set
= diff_get_color(color_diff
, DIFF_FILE_NEW
);
1230 if (line
[0] == '+') {
1233 if (!ws_blank_line(line
+ 1, len
- 1, data
->ws_rule
))
1234 data
->trailing_blanks_start
= 0;
1235 else if (!data
->trailing_blanks_start
)
1236 data
->trailing_blanks_start
= data
->lineno
;
1237 if (is_conflict_marker(line
+ 1, len
- 1)) {
1239 fprintf(data
->o
->file
,
1240 "%s:%d: leftover conflict marker\n",
1241 data
->filename
, data
->lineno
);
1243 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
1246 data
->status
|= bad
;
1247 err
= whitespace_error_string(bad
);
1248 fprintf(data
->o
->file
, "%s:%d: %s.\n",
1249 data
->filename
, data
->lineno
, err
);
1251 emit_line(data
->o
->file
, set
, reset
, line
, 1);
1252 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
1253 data
->o
->file
, set
, reset
, ws
);
1254 } else if (line
[0] == ' ') {
1256 data
->trailing_blanks_start
= 0;
1257 } else if (line
[0] == '@') {
1258 char *plus
= strchr(line
, '+');
1260 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
1262 die("invalid diff");
1263 data
->trailing_blanks_start
= 0;
1267 static unsigned char *deflate_it(char *data
,
1269 unsigned long *result_size
)
1272 unsigned char *deflated
;
1275 memset(&stream
, 0, sizeof(stream
));
1276 deflateInit(&stream
, zlib_compression_level
);
1277 bound
= deflateBound(&stream
, size
);
1278 deflated
= xmalloc(bound
);
1279 stream
.next_out
= deflated
;
1280 stream
.avail_out
= bound
;
1282 stream
.next_in
= (unsigned char *)data
;
1283 stream
.avail_in
= size
;
1284 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
1286 deflateEnd(&stream
);
1287 *result_size
= stream
.total_out
;
1291 static void emit_binary_diff_body(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1297 unsigned long orig_size
;
1298 unsigned long delta_size
;
1299 unsigned long deflate_size
;
1300 unsigned long data_size
;
1302 /* We could do deflated delta, or we could do just deflated two,
1303 * whichever is smaller.
1306 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
1307 if (one
->size
&& two
->size
) {
1308 delta
= diff_delta(one
->ptr
, one
->size
,
1309 two
->ptr
, two
->size
,
1310 &delta_size
, deflate_size
);
1312 void *to_free
= delta
;
1313 orig_size
= delta_size
;
1314 delta
= deflate_it(delta
, delta_size
, &delta_size
);
1319 if (delta
&& delta_size
< deflate_size
) {
1320 fprintf(file
, "delta %lu\n", orig_size
);
1323 data_size
= delta_size
;
1326 fprintf(file
, "literal %lu\n", two
->size
);
1329 data_size
= deflate_size
;
1332 /* emit data encoded in base85 */
1335 int bytes
= (52 < data_size
) ? 52 : data_size
;
1339 line
[0] = bytes
+ 'A' - 1;
1341 line
[0] = bytes
- 26 + 'a' - 1;
1342 encode_85(line
+ 1, cp
, bytes
);
1343 cp
= (char *) cp
+ bytes
;
1347 fprintf(file
, "\n");
1351 static void emit_binary_diff(FILE *file
, mmfile_t
*one
, mmfile_t
*two
)
1353 fprintf(file
, "GIT binary patch\n");
1354 emit_binary_diff_body(file
, one
, two
);
1355 emit_binary_diff_body(file
, two
, one
);
1358 static void setup_diff_attr_check(struct git_attr_check
*check
)
1360 static struct git_attr
*attr_diff
;
1363 attr_diff
= git_attr("diff", 4);
1365 check
[0].attr
= attr_diff
;
1368 static void diff_filespec_check_attr(struct diff_filespec
*one
)
1370 struct git_attr_check attr_diff_check
;
1371 int check_from_data
= 0;
1373 if (one
->checked_attr
)
1376 setup_diff_attr_check(&attr_diff_check
);
1378 one
->funcname_pattern_ident
= NULL
;
1380 if (!git_checkattr(one
->path
, 1, &attr_diff_check
)) {
1384 value
= attr_diff_check
.value
;
1385 if (ATTR_TRUE(value
))
1387 else if (ATTR_FALSE(value
))
1390 check_from_data
= 1;
1392 /* funcname pattern ident */
1393 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1396 one
->funcname_pattern_ident
= value
;
1399 if (check_from_data
) {
1400 if (!one
->data
&& DIFF_FILE_VALID(one
))
1401 diff_populate_filespec(one
, 0);
1404 one
->is_binary
= buffer_is_binary(one
->data
, one
->size
);
1408 int diff_filespec_is_binary(struct diff_filespec
*one
)
1410 diff_filespec_check_attr(one
);
1411 return one
->is_binary
;
1414 static const struct funcname_pattern_entry
*funcname_pattern(const char *ident
)
1416 struct funcname_pattern_list
*pp
;
1418 for (pp
= funcname_pattern_list
; pp
; pp
= pp
->next
)
1419 if (!strcmp(ident
, pp
->e
.name
))
1424 static const struct funcname_pattern_entry builtin_funcname_pattern
[] = {
1425 { "bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
1427 { "html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$", REG_EXTENDED
},
1429 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
1430 "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$",
1433 "^((procedure|function|constructor|destructor|interface|"
1434 "implementation|initialization|finalization)[ \t]*.*)$"
1436 "^(.*=[ \t]*(class|record).*)$",
1438 { "php", "^[\t ]*((function|class).*)", REG_EXTENDED
},
1439 { "python", "^[ \t]*((class|def)[ \t].*)$", REG_EXTENDED
},
1440 { "ruby", "^[ \t]*((class|module|def)[ \t].*)$",
1442 { "bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
1445 "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
1449 static const struct funcname_pattern_entry
*diff_funcname_pattern(struct diff_filespec
*one
)
1452 const struct funcname_pattern_entry
*pe
;
1455 diff_filespec_check_attr(one
);
1456 ident
= one
->funcname_pattern_ident
;
1460 * If the config file has "funcname.default" defined, that
1461 * regexp is used; otherwise NULL is returned and xemit uses
1462 * the built-in default.
1464 return funcname_pattern("default");
1466 /* Look up custom "funcname.$ident" regexp from config. */
1467 pe
= funcname_pattern(ident
);
1472 * And define built-in fallback patterns here. Note that
1473 * these can be overridden by the user's config settings.
1475 for (i
= 0; i
< ARRAY_SIZE(builtin_funcname_pattern
); i
++)
1476 if (!strcmp(ident
, builtin_funcname_pattern
[i
].name
))
1477 return &builtin_funcname_pattern
[i
];
1482 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
1484 if (!options
->a_prefix
)
1485 options
->a_prefix
= a
;
1486 if (!options
->b_prefix
)
1487 options
->b_prefix
= b
;
1490 static void builtin_diff(const char *name_a
,
1492 struct diff_filespec
*one
,
1493 struct diff_filespec
*two
,
1494 const char *xfrm_msg
,
1495 struct diff_options
*o
,
1496 int complete_rewrite
)
1500 char *a_one
, *b_two
;
1501 const char *set
= diff_get_color_opt(o
, DIFF_METAINFO
);
1502 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
1503 const char *a_prefix
, *b_prefix
;
1505 diff_set_mnemonic_prefix(o
, "a/", "b/");
1506 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
1507 a_prefix
= o
->b_prefix
;
1508 b_prefix
= o
->a_prefix
;
1510 a_prefix
= o
->a_prefix
;
1511 b_prefix
= o
->b_prefix
;
1514 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
1515 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
1516 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
1517 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
1518 fprintf(o
->file
, "%sdiff --git %s %s%s\n", set
, a_one
, b_two
, reset
);
1519 if (lbl
[0][0] == '/') {
1521 fprintf(o
->file
, "%snew file mode %06o%s\n", set
, two
->mode
, reset
);
1522 if (xfrm_msg
&& xfrm_msg
[0])
1523 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1525 else if (lbl
[1][0] == '/') {
1526 fprintf(o
->file
, "%sdeleted file mode %06o%s\n", set
, one
->mode
, reset
);
1527 if (xfrm_msg
&& xfrm_msg
[0])
1528 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1531 if (one
->mode
!= two
->mode
) {
1532 fprintf(o
->file
, "%sold mode %06o%s\n", set
, one
->mode
, reset
);
1533 fprintf(o
->file
, "%snew mode %06o%s\n", set
, two
->mode
, reset
);
1535 if (xfrm_msg
&& xfrm_msg
[0])
1536 fprintf(o
->file
, "%s%s%s\n", set
, xfrm_msg
, reset
);
1538 * we do not run diff between different kind
1541 if ((one
->mode
^ two
->mode
) & S_IFMT
)
1542 goto free_ab_and_return
;
1543 if (complete_rewrite
) {
1544 emit_rewrite_diff(name_a
, name_b
, one
, two
, o
);
1545 o
->found_changes
= 1;
1546 goto free_ab_and_return
;
1550 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1551 die("unable to read files to diff");
1553 if (!DIFF_OPT_TST(o
, TEXT
) &&
1554 (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
))) {
1555 /* Quite common confusing case */
1556 if (mf1
.size
== mf2
.size
&&
1557 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
))
1558 goto free_ab_and_return
;
1559 if (DIFF_OPT_TST(o
, BINARY
))
1560 emit_binary_diff(o
->file
, &mf1
, &mf2
);
1562 fprintf(o
->file
, "Binary files %s and %s differ\n",
1564 o
->found_changes
= 1;
1567 /* Crazy xdl interfaces.. */
1568 const char *diffopts
= getenv("GIT_DIFF_OPTS");
1572 struct emit_callback ecbdata
;
1573 const struct funcname_pattern_entry
*pe
;
1575 pe
= diff_funcname_pattern(one
);
1577 pe
= diff_funcname_pattern(two
);
1579 memset(&xecfg
, 0, sizeof(xecfg
));
1580 memset(&ecbdata
, 0, sizeof(ecbdata
));
1581 ecbdata
.label_path
= lbl
;
1582 ecbdata
.color_diff
= DIFF_OPT_TST(o
, COLOR_DIFF
);
1583 ecbdata
.found_changesp
= &o
->found_changes
;
1584 ecbdata
.ws_rule
= whitespace_rule(name_b
? name_b
: name_a
);
1585 ecbdata
.file
= o
->file
;
1586 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1587 xecfg
.ctxlen
= o
->context
;
1588 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
1590 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
1593 else if (!prefixcmp(diffopts
, "--unified="))
1594 xecfg
.ctxlen
= strtoul(diffopts
+ 10, NULL
, 10);
1595 else if (!prefixcmp(diffopts
, "-u"))
1596 xecfg
.ctxlen
= strtoul(diffopts
+ 2, NULL
, 10);
1597 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
)) {
1598 ecbdata
.diff_words
=
1599 xcalloc(1, sizeof(struct diff_words_data
));
1600 ecbdata
.diff_words
->file
= o
->file
;
1602 xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
1603 &xpp
, &xecfg
, &ecb
);
1604 if (DIFF_OPT_TST(o
, COLOR_DIFF_WORDS
))
1605 free_diff_words_data(&ecbdata
);
1609 diff_free_filespec_data(one
);
1610 diff_free_filespec_data(two
);
1616 static void builtin_diffstat(const char *name_a
, const char *name_b
,
1617 struct diff_filespec
*one
,
1618 struct diff_filespec
*two
,
1619 struct diffstat_t
*diffstat
,
1620 struct diff_options
*o
,
1621 int complete_rewrite
)
1624 struct diffstat_file
*data
;
1626 data
= diffstat_add(diffstat
, name_a
, name_b
);
1629 data
->is_unmerged
= 1;
1632 if (complete_rewrite
) {
1633 diff_populate_filespec(one
, 0);
1634 diff_populate_filespec(two
, 0);
1635 data
->deleted
= count_lines(one
->data
, one
->size
);
1636 data
->added
= count_lines(two
->data
, two
->size
);
1637 goto free_and_return
;
1639 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1640 die("unable to read files to diff");
1642 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
1643 data
->is_binary
= 1;
1644 data
->added
= mf2
.size
;
1645 data
->deleted
= mf1
.size
;
1647 /* Crazy xdl interfaces.. */
1652 memset(&xecfg
, 0, sizeof(xecfg
));
1653 xpp
.flags
= XDF_NEED_MINIMAL
| o
->xdl_opts
;
1654 xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
1655 &xpp
, &xecfg
, &ecb
);
1659 diff_free_filespec_data(one
);
1660 diff_free_filespec_data(two
);
1663 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
1664 const char *attr_path
,
1665 struct diff_filespec
*one
,
1666 struct diff_filespec
*two
,
1667 struct diff_options
*o
)
1670 struct checkdiff_t data
;
1675 memset(&data
, 0, sizeof(data
));
1676 data
.filename
= name_b
? name_b
: name_a
;
1679 data
.ws_rule
= whitespace_rule(attr_path
);
1681 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
1682 die("unable to read files to diff");
1685 * All the other codepaths check both sides, but not checking
1686 * the "old" side here is deliberate. We are checking the newly
1687 * introduced changes, and as long as the "new" side is text, we
1688 * can and should check what it introduces.
1690 if (diff_filespec_is_binary(two
))
1691 goto free_and_return
;
1693 /* Crazy xdl interfaces.. */
1698 memset(&xecfg
, 0, sizeof(xecfg
));
1699 xecfg
.ctxlen
= 1; /* at least one context line */
1700 xpp
.flags
= XDF_NEED_MINIMAL
;
1701 xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
1702 &xpp
, &xecfg
, &ecb
);
1704 if ((data
.ws_rule
& WS_TRAILING_SPACE
) &&
1705 data
.trailing_blanks_start
) {
1706 fprintf(o
->file
, "%s:%d: ends with blank lines.\n",
1707 data
.filename
, data
.trailing_blanks_start
);
1708 data
.status
= 1; /* report errors */
1712 diff_free_filespec_data(one
);
1713 diff_free_filespec_data(two
);
1715 DIFF_OPT_SET(o
, CHECK_FAILED
);
1718 struct diff_filespec
*alloc_filespec(const char *path
)
1720 int namelen
= strlen(path
);
1721 struct diff_filespec
*spec
= xmalloc(sizeof(*spec
) + namelen
+ 1);
1723 memset(spec
, 0, sizeof(*spec
));
1724 spec
->path
= (char *)(spec
+ 1);
1725 memcpy(spec
->path
, path
, namelen
+1);
1730 void free_filespec(struct diff_filespec
*spec
)
1732 if (!--spec
->count
) {
1733 diff_free_filespec_data(spec
);
1738 void fill_filespec(struct diff_filespec
*spec
, const unsigned char *sha1
,
1739 unsigned short mode
)
1742 spec
->mode
= canon_mode(mode
);
1743 hashcpy(spec
->sha1
, sha1
);
1744 spec
->sha1_valid
= !is_null_sha1(sha1
);
1749 * Given a name and sha1 pair, if the index tells us the file in
1750 * the work tree has that object contents, return true, so that
1751 * prepare_temp_file() does not have to inflate and extract.
1753 static int reuse_worktree_file(const char *name
, const unsigned char *sha1
, int want_file
)
1755 struct cache_entry
*ce
;
1759 /* We do not read the cache ourselves here, because the
1760 * benchmark with my previous version that always reads cache
1761 * shows that it makes things worse for diff-tree comparing
1762 * two linux-2.6 kernel trees in an already checked out work
1763 * tree. This is because most diff-tree comparisons deal with
1764 * only a small number of files, while reading the cache is
1765 * expensive for a large project, and its cost outweighs the
1766 * savings we get by not inflating the object to a temporary
1767 * file. Practically, this code only helps when we are used
1768 * by diff-cache --cached, which does read the cache before
1774 /* We want to avoid the working directory if our caller
1775 * doesn't need the data in a normal file, this system
1776 * is rather slow with its stat/open/mmap/close syscalls,
1777 * and the object is contained in a pack file. The pack
1778 * is probably already open and will be faster to obtain
1779 * the data through than the working directory. Loose
1780 * objects however would tend to be slower as they need
1781 * to be individually opened and inflated.
1783 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(sha1
, NULL
))
1787 pos
= cache_name_pos(name
, len
);
1790 ce
= active_cache
[pos
];
1793 * This is not the sha1 we are looking for, or
1794 * unreusable because it is not a regular file.
1796 if (hashcmp(sha1
, ce
->sha1
) || !S_ISREG(ce
->ce_mode
))
1800 * If ce matches the file in the work tree, we can reuse it.
1802 if (ce_uptodate(ce
) ||
1803 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
1809 static int populate_from_stdin(struct diff_filespec
*s
)
1814 strbuf_init(&buf
, 0);
1815 if (strbuf_read(&buf
, 0, 0) < 0)
1816 return error("error while reading from stdin %s",
1819 s
->should_munmap
= 0;
1820 s
->data
= strbuf_detach(&buf
, &size
);
1826 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
1829 char *data
= xmalloc(100);
1830 len
= snprintf(data
, 100,
1831 "Subproject commit %s\n", sha1_to_hex(s
->sha1
));
1843 * While doing rename detection and pickaxe operation, we may need to
1844 * grab the data for the blob (or file) for our own in-core comparison.
1845 * diff_filespec has data and size fields for this purpose.
1847 int diff_populate_filespec(struct diff_filespec
*s
, int size_only
)
1850 if (!DIFF_FILE_VALID(s
))
1851 die("internal error: asking to populate invalid file.");
1852 if (S_ISDIR(s
->mode
))
1858 if (size_only
&& 0 < s
->size
)
1861 if (S_ISGITLINK(s
->mode
))
1862 return diff_populate_gitlink(s
, size_only
);
1864 if (!s
->sha1_valid
||
1865 reuse_worktree_file(s
->path
, s
->sha1
, 0)) {
1870 if (!strcmp(s
->path
, "-"))
1871 return populate_from_stdin(s
);
1873 if (lstat(s
->path
, &st
) < 0) {
1874 if (errno
== ENOENT
) {
1878 s
->data
= (char *)"";
1883 s
->size
= xsize_t(st
.st_size
);
1888 if (S_ISLNK(st
.st_mode
)) {
1890 s
->data
= xmalloc(s
->size
);
1892 ret
= readlink(s
->path
, s
->data
, s
->size
);
1899 fd
= open(s
->path
, O_RDONLY
);
1902 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1904 s
->should_munmap
= 1;
1907 * Convert from working tree format to canonical git format
1909 strbuf_init(&buf
, 0);
1910 if (convert_to_git(s
->path
, s
->data
, s
->size
, &buf
, safe_crlf
)) {
1912 munmap(s
->data
, s
->size
);
1913 s
->should_munmap
= 0;
1914 s
->data
= strbuf_detach(&buf
, &size
);
1920 enum object_type type
;
1922 type
= sha1_object_info(s
->sha1
, &s
->size
);
1924 s
->data
= read_sha1_file(s
->sha1
, &type
, &s
->size
);
1931 void diff_free_filespec_blob(struct diff_filespec
*s
)
1935 else if (s
->should_munmap
)
1936 munmap(s
->data
, s
->size
);
1938 if (s
->should_free
|| s
->should_munmap
) {
1939 s
->should_free
= s
->should_munmap
= 0;
1944 void diff_free_filespec_data(struct diff_filespec
*s
)
1946 diff_free_filespec_blob(s
);
1951 static void prep_temp_blob(struct diff_tempfile
*temp
,
1954 const unsigned char *sha1
,
1959 fd
= git_mkstemp(temp
->tmp_path
, PATH_MAX
, ".diff_XXXXXX");
1961 die("unable to create temp-file: %s", strerror(errno
));
1962 if (write_in_full(fd
, blob
, size
) != size
)
1963 die("unable to write temp-file");
1965 temp
->name
= temp
->tmp_path
;
1966 strcpy(temp
->hex
, sha1_to_hex(sha1
));
1968 sprintf(temp
->mode
, "%06o", mode
);
1971 static void prepare_temp_file(const char *name
,
1972 struct diff_tempfile
*temp
,
1973 struct diff_filespec
*one
)
1975 if (!DIFF_FILE_VALID(one
)) {
1977 /* A '-' entry produces this for file-2, and
1978 * a '+' entry produces this for file-1.
1980 temp
->name
= "/dev/null";
1981 strcpy(temp
->hex
, ".");
1982 strcpy(temp
->mode
, ".");
1986 if (!one
->sha1_valid
||
1987 reuse_worktree_file(name
, one
->sha1
, 1)) {
1989 if (lstat(name
, &st
) < 0) {
1990 if (errno
== ENOENT
)
1991 goto not_a_valid_file
;
1992 die("stat(%s): %s", name
, strerror(errno
));
1994 if (S_ISLNK(st
.st_mode
)) {
1996 char buf
[PATH_MAX
+ 1]; /* ought to be SYMLINK_MAX */
1997 size_t sz
= xsize_t(st
.st_size
);
1998 if (sizeof(buf
) <= st
.st_size
)
1999 die("symlink too long: %s", name
);
2000 ret
= readlink(name
, buf
, sz
);
2002 die("readlink(%s)", name
);
2003 prep_temp_blob(temp
, buf
, sz
,
2005 one
->sha1
: null_sha1
),
2007 one
->mode
: S_IFLNK
));
2010 /* we can borrow from the file in the work tree */
2012 if (!one
->sha1_valid
)
2013 strcpy(temp
->hex
, sha1_to_hex(null_sha1
));
2015 strcpy(temp
->hex
, sha1_to_hex(one
->sha1
));
2016 /* Even though we may sometimes borrow the
2017 * contents from the work tree, we always want
2018 * one->mode. mode is trustworthy even when
2019 * !(one->sha1_valid), as long as
2020 * DIFF_FILE_VALID(one).
2022 sprintf(temp
->mode
, "%06o", one
->mode
);
2027 if (diff_populate_filespec(one
, 0))
2028 die("cannot read data blob for %s", one
->path
);
2029 prep_temp_blob(temp
, one
->data
, one
->size
,
2030 one
->sha1
, one
->mode
);
2034 static void remove_tempfile(void)
2038 for (i
= 0; i
< 2; i
++)
2039 if (diff_temp
[i
].name
== diff_temp
[i
].tmp_path
) {
2040 unlink(diff_temp
[i
].name
);
2041 diff_temp
[i
].name
= NULL
;
2045 static void remove_tempfile_on_signal(int signo
)
2048 signal(SIGINT
, SIG_DFL
);
2052 /* An external diff command takes:
2054 * diff-cmd name infile1 infile1-sha1 infile1-mode \
2055 * infile2 infile2-sha1 infile2-mode [ rename-to ]
2058 static void run_external_diff(const char *pgm
,
2061 struct diff_filespec
*one
,
2062 struct diff_filespec
*two
,
2063 const char *xfrm_msg
,
2064 int complete_rewrite
)
2066 const char *spawn_arg
[10];
2067 struct diff_tempfile
*temp
= diff_temp
;
2069 static int atexit_asked
= 0;
2070 const char *othername
;
2071 const char **arg
= &spawn_arg
[0];
2073 othername
= (other
? other
: name
);
2075 prepare_temp_file(name
, &temp
[0], one
);
2076 prepare_temp_file(othername
, &temp
[1], two
);
2077 if (! atexit_asked
&&
2078 (temp
[0].name
== temp
[0].tmp_path
||
2079 temp
[1].name
== temp
[1].tmp_path
)) {
2081 atexit(remove_tempfile
);
2083 signal(SIGINT
, remove_tempfile_on_signal
);
2089 *arg
++ = temp
[0].name
;
2090 *arg
++ = temp
[0].hex
;
2091 *arg
++ = temp
[0].mode
;
2092 *arg
++ = temp
[1].name
;
2093 *arg
++ = temp
[1].hex
;
2094 *arg
++ = temp
[1].mode
;
2105 retval
= run_command_v_opt(spawn_arg
, 0);
2108 fprintf(stderr
, "external diff died, stopping at %s.\n", name
);
2113 static const char *external_diff_attr(const char *name
)
2115 struct git_attr_check attr_diff_check
;
2120 setup_diff_attr_check(&attr_diff_check
);
2121 if (!git_checkattr(name
, 1, &attr_diff_check
)) {
2122 const char *value
= attr_diff_check
.value
;
2123 if (!ATTR_TRUE(value
) &&
2124 !ATTR_FALSE(value
) &&
2125 !ATTR_UNSET(value
)) {
2126 struct ll_diff_driver
*drv
;
2128 for (drv
= user_diff
; drv
; drv
= drv
->next
)
2129 if (!strcmp(drv
->name
, value
))
2136 static void run_diff_cmd(const char *pgm
,
2139 const char *attr_path
,
2140 struct diff_filespec
*one
,
2141 struct diff_filespec
*two
,
2142 const char *xfrm_msg
,
2143 struct diff_options
*o
,
2144 int complete_rewrite
)
2146 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
2149 const char *cmd
= external_diff_attr(attr_path
);
2155 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
2160 builtin_diff(name
, other
? other
: name
,
2161 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2163 fprintf(o
->file
, "* Unmerged path %s\n", name
);
2166 static void diff_fill_sha1_info(struct diff_filespec
*one
)
2168 if (DIFF_FILE_VALID(one
)) {
2169 if (!one
->sha1_valid
) {
2171 if (!strcmp(one
->path
, "-")) {
2172 hashcpy(one
->sha1
, null_sha1
);
2175 if (lstat(one
->path
, &st
) < 0)
2176 die("stat %s", one
->path
);
2177 if (index_path(one
->sha1
, one
->path
, &st
, 0))
2178 die("cannot hash %s\n", one
->path
);
2185 static int similarity_index(struct diff_filepair
*p
)
2187 return p
->score
* 100 / MAX_SCORE
;
2190 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
2192 /* Strip the prefix but do not molest /dev/null and absolute paths */
2193 if (*namep
&& **namep
!= '/')
2194 *namep
+= prefix_length
;
2195 if (*otherp
&& **otherp
!= '/')
2196 *otherp
+= prefix_length
;
2199 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
2201 const char *pgm
= external_diff();
2204 struct diff_filespec
*one
= p
->one
;
2205 struct diff_filespec
*two
= p
->two
;
2208 const char *attr_path
;
2209 int complete_rewrite
= 0;
2211 name
= p
->one
->path
;
2212 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2214 if (o
->prefix_length
)
2215 strip_prefix(o
->prefix_length
, &name
, &other
);
2217 if (DIFF_PAIR_UNMERGED(p
)) {
2218 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
2219 NULL
, NULL
, NULL
, o
, 0);
2223 diff_fill_sha1_info(one
);
2224 diff_fill_sha1_info(two
);
2226 strbuf_init(&msg
, PATH_MAX
* 2 + 300);
2227 switch (p
->status
) {
2228 case DIFF_STATUS_COPIED
:
2229 strbuf_addf(&msg
, "similarity index %d%%", similarity_index(p
));
2230 strbuf_addstr(&msg
, "\ncopy from ");
2231 quote_c_style(name
, &msg
, NULL
, 0);
2232 strbuf_addstr(&msg
, "\ncopy to ");
2233 quote_c_style(other
, &msg
, NULL
, 0);
2234 strbuf_addch(&msg
, '\n');
2236 case DIFF_STATUS_RENAMED
:
2237 strbuf_addf(&msg
, "similarity index %d%%", similarity_index(p
));
2238 strbuf_addstr(&msg
, "\nrename from ");
2239 quote_c_style(name
, &msg
, NULL
, 0);
2240 strbuf_addstr(&msg
, "\nrename to ");
2241 quote_c_style(other
, &msg
, NULL
, 0);
2242 strbuf_addch(&msg
, '\n');
2244 case DIFF_STATUS_MODIFIED
:
2246 strbuf_addf(&msg
, "dissimilarity index %d%%\n",
2247 similarity_index(p
));
2248 complete_rewrite
= 1;
2257 if (hashcmp(one
->sha1
, two
->sha1
)) {
2258 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
2260 if (DIFF_OPT_TST(o
, BINARY
)) {
2262 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
2263 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
2266 strbuf_addf(&msg
, "index %.*s..%.*s",
2267 abbrev
, sha1_to_hex(one
->sha1
),
2268 abbrev
, sha1_to_hex(two
->sha1
));
2269 if (one
->mode
== two
->mode
)
2270 strbuf_addf(&msg
, " %06o", one
->mode
);
2271 strbuf_addch(&msg
, '\n');
2275 strbuf_setlen(&msg
, msg
.len
- 1);
2276 xfrm_msg
= msg
.len
? msg
.buf
: NULL
;
2279 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
2280 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
2281 /* a filepair that changes between file and symlink
2282 * needs to be split into deletion and creation.
2284 struct diff_filespec
*null
= alloc_filespec(two
->path
);
2285 run_diff_cmd(NULL
, name
, other
, attr_path
,
2286 one
, null
, xfrm_msg
, o
, 0);
2288 null
= alloc_filespec(one
->path
);
2289 run_diff_cmd(NULL
, name
, other
, attr_path
,
2290 null
, two
, xfrm_msg
, o
, 0);
2294 run_diff_cmd(pgm
, name
, other
, attr_path
,
2295 one
, two
, xfrm_msg
, o
, complete_rewrite
);
2297 strbuf_release(&msg
);
2300 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
2301 struct diffstat_t
*diffstat
)
2305 int complete_rewrite
= 0;
2307 if (DIFF_PAIR_UNMERGED(p
)) {
2309 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, 0);
2313 name
= p
->one
->path
;
2314 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2316 if (o
->prefix_length
)
2317 strip_prefix(o
->prefix_length
, &name
, &other
);
2319 diff_fill_sha1_info(p
->one
);
2320 diff_fill_sha1_info(p
->two
);
2322 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
2323 complete_rewrite
= 1;
2324 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, complete_rewrite
);
2327 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
2331 const char *attr_path
;
2333 if (DIFF_PAIR_UNMERGED(p
)) {
2338 name
= p
->one
->path
;
2339 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
2340 attr_path
= other
? other
: name
;
2342 if (o
->prefix_length
)
2343 strip_prefix(o
->prefix_length
, &name
, &other
);
2345 diff_fill_sha1_info(p
->one
);
2346 diff_fill_sha1_info(p
->two
);
2348 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
2351 void diff_setup(struct diff_options
*options
)
2353 memset(options
, 0, sizeof(*options
));
2355 options
->file
= stdout
;
2357 options
->line_termination
= '\n';
2358 options
->break_opt
= -1;
2359 options
->rename_limit
= -1;
2360 options
->dirstat_percent
= 3;
2361 DIFF_OPT_CLR(options
, DIRSTAT_CUMULATIVE
);
2362 options
->context
= 3;
2364 options
->change
= diff_change
;
2365 options
->add_remove
= diff_addremove
;
2366 if (diff_use_color_default
> 0)
2367 DIFF_OPT_SET(options
, COLOR_DIFF
);
2369 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2370 options
->detect_rename
= diff_detect_rename_default
;
2372 if (!diff_mnemonic_prefix
) {
2373 options
->a_prefix
= "a/";
2374 options
->b_prefix
= "b/";
2378 int diff_setup_done(struct diff_options
*options
)
2382 if (options
->output_format
& DIFF_FORMAT_NAME
)
2384 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
2386 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
2388 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
2391 die("--name-only, --name-status, --check and -s are mutually exclusive");
2393 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
2394 options
->detect_rename
= DIFF_DETECT_COPY
;
2396 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
2397 options
->prefix
= NULL
;
2398 if (options
->prefix
)
2399 options
->prefix_length
= strlen(options
->prefix
);
2401 options
->prefix_length
= 0;
2403 if (options
->output_format
& (DIFF_FORMAT_NAME
|
2404 DIFF_FORMAT_NAME_STATUS
|
2405 DIFF_FORMAT_CHECKDIFF
|
2406 DIFF_FORMAT_NO_OUTPUT
))
2407 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
2408 DIFF_FORMAT_NUMSTAT
|
2409 DIFF_FORMAT_DIFFSTAT
|
2410 DIFF_FORMAT_SHORTSTAT
|
2411 DIFF_FORMAT_DIRSTAT
|
2412 DIFF_FORMAT_SUMMARY
|
2416 * These cases always need recursive; we do not drop caller-supplied
2417 * recursive bits for other formats here.
2419 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
2420 DIFF_FORMAT_NUMSTAT
|
2421 DIFF_FORMAT_DIFFSTAT
|
2422 DIFF_FORMAT_SHORTSTAT
|
2423 DIFF_FORMAT_DIRSTAT
|
2424 DIFF_FORMAT_SUMMARY
|
2425 DIFF_FORMAT_CHECKDIFF
))
2426 DIFF_OPT_SET(options
, RECURSIVE
);
2428 * Also pickaxe would not work very well if you do not say recursive
2430 if (options
->pickaxe
)
2431 DIFF_OPT_SET(options
, RECURSIVE
);
2433 if (options
->detect_rename
&& options
->rename_limit
< 0)
2434 options
->rename_limit
= diff_rename_limit_default
;
2435 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
2437 /* read-cache does not die even when it fails
2438 * so it is safe for us to do this here. Also
2439 * it does not smudge active_cache or active_nr
2440 * when it fails, so we do not have to worry about
2441 * cleaning it up ourselves either.
2445 if (options
->abbrev
<= 0 || 40 < options
->abbrev
)
2446 options
->abbrev
= 40; /* full */
2449 * It does not make sense to show the first hit we happened
2450 * to have found. It does not make sense not to return with
2451 * exit code in such a case either.
2453 if (DIFF_OPT_TST(options
, QUIET
)) {
2454 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
2455 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2461 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
2471 if (c
== arg_short
) {
2475 if (val
&& isdigit(c
)) {
2477 int n
= strtoul(arg
, &end
, 10);
2488 eq
= strchr(arg
, '=');
2493 if (!len
|| strncmp(arg
, arg_long
, len
))
2498 if (!isdigit(*++eq
))
2500 n
= strtoul(eq
, &end
, 10);
2508 static int diff_scoreopt_parse(const char *opt
);
2510 int diff_opt_parse(struct diff_options
*options
, const char **av
, int ac
)
2512 const char *arg
= av
[0];
2514 /* Output format options */
2515 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u"))
2516 options
->output_format
|= DIFF_FORMAT_PATCH
;
2517 else if (opt_arg(arg
, 'U', "unified", &options
->context
))
2518 options
->output_format
|= DIFF_FORMAT_PATCH
;
2519 else if (!strcmp(arg
, "--raw"))
2520 options
->output_format
|= DIFF_FORMAT_RAW
;
2521 else if (!strcmp(arg
, "--patch-with-raw"))
2522 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
;
2523 else if (!strcmp(arg
, "--numstat"))
2524 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
2525 else if (!strcmp(arg
, "--shortstat"))
2526 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
2527 else if (opt_arg(arg
, 'X', "dirstat", &options
->dirstat_percent
))
2528 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2529 else if (!strcmp(arg
, "--cumulative")) {
2530 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2531 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
2532 } else if (opt_arg(arg
, 0, "dirstat-by-file",
2533 &options
->dirstat_percent
)) {
2534 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
2535 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
2537 else if (!strcmp(arg
, "--check"))
2538 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
2539 else if (!strcmp(arg
, "--summary"))
2540 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
2541 else if (!strcmp(arg
, "--patch-with-stat"))
2542 options
->output_format
|= DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
;
2543 else if (!strcmp(arg
, "--name-only"))
2544 options
->output_format
|= DIFF_FORMAT_NAME
;
2545 else if (!strcmp(arg
, "--name-status"))
2546 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
2547 else if (!strcmp(arg
, "-s"))
2548 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
2549 else if (!prefixcmp(arg
, "--stat")) {
2551 int width
= options
->stat_width
;
2552 int name_width
= options
->stat_name_width
;
2558 if (!prefixcmp(arg
, "-width="))
2559 width
= strtoul(arg
+ 7, &end
, 10);
2560 else if (!prefixcmp(arg
, "-name-width="))
2561 name_width
= strtoul(arg
+ 12, &end
, 10);
2564 width
= strtoul(arg
+1, &end
, 10);
2566 name_width
= strtoul(end
+1, &end
, 10);
2569 /* Important! This checks all the error cases! */
2572 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
2573 options
->stat_name_width
= name_width
;
2574 options
->stat_width
= width
;
2577 /* renames options */
2578 else if (!prefixcmp(arg
, "-B")) {
2579 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
2582 else if (!prefixcmp(arg
, "-M")) {
2583 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2585 options
->detect_rename
= DIFF_DETECT_RENAME
;
2587 else if (!prefixcmp(arg
, "-C")) {
2588 if (options
->detect_rename
== DIFF_DETECT_COPY
)
2589 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2590 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
2592 options
->detect_rename
= DIFF_DETECT_COPY
;
2594 else if (!strcmp(arg
, "--no-renames"))
2595 options
->detect_rename
= 0;
2596 else if (!strcmp(arg
, "--relative"))
2597 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2598 else if (!prefixcmp(arg
, "--relative=")) {
2599 DIFF_OPT_SET(options
, RELATIVE_NAME
);
2600 options
->prefix
= arg
+ 11;
2604 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
2605 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2606 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
2607 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2608 else if (!strcmp(arg
, "--ignore-space-at-eol"))
2609 options
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2612 else if (!strcmp(arg
, "--binary")) {
2613 options
->output_format
|= DIFF_FORMAT_PATCH
;
2614 DIFF_OPT_SET(options
, BINARY
);
2616 else if (!strcmp(arg
, "--full-index"))
2617 DIFF_OPT_SET(options
, FULL_INDEX
);
2618 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
2619 DIFF_OPT_SET(options
, TEXT
);
2620 else if (!strcmp(arg
, "-R"))
2621 DIFF_OPT_SET(options
, REVERSE_DIFF
);
2622 else if (!strcmp(arg
, "--find-copies-harder"))
2623 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
2624 else if (!strcmp(arg
, "--follow"))
2625 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
2626 else if (!strcmp(arg
, "--color"))
2627 DIFF_OPT_SET(options
, COLOR_DIFF
);
2628 else if (!strcmp(arg
, "--no-color"))
2629 DIFF_OPT_CLR(options
, COLOR_DIFF
);
2630 else if (!strcmp(arg
, "--color-words"))
2631 options
->flags
|= DIFF_OPT_COLOR_DIFF
| DIFF_OPT_COLOR_DIFF_WORDS
;
2632 else if (!strcmp(arg
, "--exit-code"))
2633 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
2634 else if (!strcmp(arg
, "--quiet"))
2635 DIFF_OPT_SET(options
, QUIET
);
2636 else if (!strcmp(arg
, "--ext-diff"))
2637 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
2638 else if (!strcmp(arg
, "--no-ext-diff"))
2639 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
2640 else if (!strcmp(arg
, "--ignore-submodules"))
2641 DIFF_OPT_SET(options
, IGNORE_SUBMODULES
);
2644 else if (!strcmp(arg
, "-z"))
2645 options
->line_termination
= 0;
2646 else if (!prefixcmp(arg
, "-l"))
2647 options
->rename_limit
= strtoul(arg
+2, NULL
, 10);
2648 else if (!prefixcmp(arg
, "-S"))
2649 options
->pickaxe
= arg
+ 2;
2650 else if (!strcmp(arg
, "--pickaxe-all"))
2651 options
->pickaxe_opts
= DIFF_PICKAXE_ALL
;
2652 else if (!strcmp(arg
, "--pickaxe-regex"))
2653 options
->pickaxe_opts
= DIFF_PICKAXE_REGEX
;
2654 else if (!prefixcmp(arg
, "-O"))
2655 options
->orderfile
= arg
+ 2;
2656 else if (!prefixcmp(arg
, "--diff-filter="))
2657 options
->filter
= arg
+ 14;
2658 else if (!strcmp(arg
, "--abbrev"))
2659 options
->abbrev
= DEFAULT_ABBREV
;
2660 else if (!prefixcmp(arg
, "--abbrev=")) {
2661 options
->abbrev
= strtoul(arg
+ 9, NULL
, 10);
2662 if (options
->abbrev
< MINIMUM_ABBREV
)
2663 options
->abbrev
= MINIMUM_ABBREV
;
2664 else if (40 < options
->abbrev
)
2665 options
->abbrev
= 40;
2667 else if (!prefixcmp(arg
, "--src-prefix="))
2668 options
->a_prefix
= arg
+ 13;
2669 else if (!prefixcmp(arg
, "--dst-prefix="))
2670 options
->b_prefix
= arg
+ 13;
2671 else if (!strcmp(arg
, "--no-prefix"))
2672 options
->a_prefix
= options
->b_prefix
= "";
2673 else if (!prefixcmp(arg
, "--output=")) {
2674 options
->file
= fopen(arg
+ strlen("--output="), "w");
2675 options
->close_file
= 1;
2681 static int parse_num(const char **cp_p
)
2683 unsigned long num
, scale
;
2685 const char *cp
= *cp_p
;
2692 if ( !dot
&& ch
== '.' ) {
2695 } else if ( ch
== '%' ) {
2696 scale
= dot
? scale
*100 : 100;
2697 cp
++; /* % is always at the end */
2699 } else if ( ch
>= '0' && ch
<= '9' ) {
2700 if ( scale
< 100000 ) {
2702 num
= (num
*10) + (ch
-'0');
2711 /* user says num divided by scale and we say internally that
2712 * is MAX_SCORE * num / scale.
2714 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
2717 static int diff_scoreopt_parse(const char *opt
)
2719 int opt1
, opt2
, cmd
;
2724 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
2725 return -1; /* that is not a -M, -C nor -B option */
2727 opt1
= parse_num(&opt
);
2733 else if (*opt
!= '/')
2734 return -1; /* we expect -B80/99 or -B80 */
2737 opt2
= parse_num(&opt
);
2742 return opt1
| (opt2
<< 16);
2745 struct diff_queue_struct diff_queued_diff
;
2747 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
2749 if (queue
->alloc
<= queue
->nr
) {
2750 queue
->alloc
= alloc_nr(queue
->alloc
);
2751 queue
->queue
= xrealloc(queue
->queue
,
2752 sizeof(dp
) * queue
->alloc
);
2754 queue
->queue
[queue
->nr
++] = dp
;
2757 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
2758 struct diff_filespec
*one
,
2759 struct diff_filespec
*two
)
2761 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
2769 void diff_free_filepair(struct diff_filepair
*p
)
2771 free_filespec(p
->one
);
2772 free_filespec(p
->two
);
2776 /* This is different from find_unique_abbrev() in that
2777 * it stuffs the result with dots for alignment.
2779 const char *diff_unique_abbrev(const unsigned char *sha1
, int len
)
2784 return sha1_to_hex(sha1
);
2786 abbrev
= find_unique_abbrev(sha1
, len
);
2787 abblen
= strlen(abbrev
);
2789 static char hex
[41];
2790 if (len
< abblen
&& abblen
<= len
+ 2)
2791 sprintf(hex
, "%s%.*s", abbrev
, len
+3-abblen
, "..");
2793 sprintf(hex
, "%s...", abbrev
);
2796 return sha1_to_hex(sha1
);
2799 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
2801 int line_termination
= opt
->line_termination
;
2802 int inter_name_termination
= line_termination
? '\t' : '\0';
2804 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
2805 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
2806 diff_unique_abbrev(p
->one
->sha1
, opt
->abbrev
));
2807 fprintf(opt
->file
, "%s ", diff_unique_abbrev(p
->two
->sha1
, opt
->abbrev
));
2810 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
2811 inter_name_termination
);
2813 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
2816 if (p
->status
== DIFF_STATUS_COPIED
||
2817 p
->status
== DIFF_STATUS_RENAMED
) {
2818 const char *name_a
, *name_b
;
2819 name_a
= p
->one
->path
;
2820 name_b
= p
->two
->path
;
2821 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2822 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
2823 write_name_quoted(name_b
, opt
->file
, line_termination
);
2825 const char *name_a
, *name_b
;
2826 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
2828 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
2829 write_name_quoted(name_a
, opt
->file
, line_termination
);
2833 int diff_unmodified_pair(struct diff_filepair
*p
)
2835 /* This function is written stricter than necessary to support
2836 * the currently implemented transformers, but the idea is to
2837 * let transformers to produce diff_filepairs any way they want,
2838 * and filter and clean them up here before producing the output.
2840 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
2842 if (DIFF_PAIR_UNMERGED(p
))
2843 return 0; /* unmerged is interesting */
2845 /* deletion, addition, mode or type change
2846 * and rename are all interesting.
2848 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
2849 DIFF_PAIR_MODE_CHANGED(p
) ||
2850 strcmp(one
->path
, two
->path
))
2853 /* both are valid and point at the same path. that is, we are
2854 * dealing with a change.
2856 if (one
->sha1_valid
&& two
->sha1_valid
&&
2857 !hashcmp(one
->sha1
, two
->sha1
))
2858 return 1; /* no change */
2859 if (!one
->sha1_valid
&& !two
->sha1_valid
)
2860 return 1; /* both look at the same file on the filesystem. */
2864 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
2866 if (diff_unmodified_pair(p
))
2869 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2870 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2871 return; /* no tree diffs in patch format */
2876 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
2877 struct diffstat_t
*diffstat
)
2879 if (diff_unmodified_pair(p
))
2882 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2883 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2884 return; /* no tree diffs in patch format */
2886 run_diffstat(p
, o
, diffstat
);
2889 static void diff_flush_checkdiff(struct diff_filepair
*p
,
2890 struct diff_options
*o
)
2892 if (diff_unmodified_pair(p
))
2895 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
2896 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
2897 return; /* no tree diffs in patch format */
2899 run_checkdiff(p
, o
);
2902 int diff_queue_is_empty(void)
2904 struct diff_queue_struct
*q
= &diff_queued_diff
;
2906 for (i
= 0; i
< q
->nr
; i
++)
2907 if (!diff_unmodified_pair(q
->queue
[i
]))
2913 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
2915 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
2918 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
2920 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
2921 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
2923 s
->size
, s
->xfrm_flags
);
2926 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
2928 diff_debug_filespec(p
->one
, i
, "one");
2929 diff_debug_filespec(p
->two
, i
, "two");
2930 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
2931 p
->score
, p
->status
? p
->status
: '?',
2932 p
->one
->rename_used
, p
->broken_pair
);
2935 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
2939 fprintf(stderr
, "%s\n", msg
);
2940 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
2941 for (i
= 0; i
< q
->nr
; i
++) {
2942 struct diff_filepair
*p
= q
->queue
[i
];
2943 diff_debug_filepair(p
, i
);
2948 static void diff_resolve_rename_copy(void)
2951 struct diff_filepair
*p
;
2952 struct diff_queue_struct
*q
= &diff_queued_diff
;
2954 diff_debug_queue("resolve-rename-copy", q
);
2956 for (i
= 0; i
< q
->nr
; i
++) {
2958 p
->status
= 0; /* undecided */
2959 if (DIFF_PAIR_UNMERGED(p
))
2960 p
->status
= DIFF_STATUS_UNMERGED
;
2961 else if (!DIFF_FILE_VALID(p
->one
))
2962 p
->status
= DIFF_STATUS_ADDED
;
2963 else if (!DIFF_FILE_VALID(p
->two
))
2964 p
->status
= DIFF_STATUS_DELETED
;
2965 else if (DIFF_PAIR_TYPE_CHANGED(p
))
2966 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
2968 /* from this point on, we are dealing with a pair
2969 * whose both sides are valid and of the same type, i.e.
2970 * either in-place edit or rename/copy edit.
2972 else if (DIFF_PAIR_RENAME(p
)) {
2974 * A rename might have re-connected a broken
2975 * pair up, causing the pathnames to be the
2976 * same again. If so, that's not a rename at
2977 * all, just a modification..
2979 * Otherwise, see if this source was used for
2980 * multiple renames, in which case we decrement
2981 * the count, and call it a copy.
2983 if (!strcmp(p
->one
->path
, p
->two
->path
))
2984 p
->status
= DIFF_STATUS_MODIFIED
;
2985 else if (--p
->one
->rename_used
> 0)
2986 p
->status
= DIFF_STATUS_COPIED
;
2988 p
->status
= DIFF_STATUS_RENAMED
;
2990 else if (hashcmp(p
->one
->sha1
, p
->two
->sha1
) ||
2991 p
->one
->mode
!= p
->two
->mode
||
2992 is_null_sha1(p
->one
->sha1
))
2993 p
->status
= DIFF_STATUS_MODIFIED
;
2995 /* This is a "no-change" entry and should not
2996 * happen anymore, but prepare for broken callers.
2998 error("feeding unmodified %s to diffcore",
3000 p
->status
= DIFF_STATUS_UNKNOWN
;
3003 diff_debug_queue("resolve-rename-copy done", q
);
3006 static int check_pair_status(struct diff_filepair
*p
)
3008 switch (p
->status
) {
3009 case DIFF_STATUS_UNKNOWN
:
3012 die("internal error in diff-resolve-rename-copy");
3018 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
3020 int fmt
= opt
->output_format
;
3022 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
3023 diff_flush_checkdiff(p
, opt
);
3024 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
3025 diff_flush_raw(p
, opt
);
3026 else if (fmt
& DIFF_FORMAT_NAME
) {
3027 const char *name_a
, *name_b
;
3028 name_a
= p
->two
->path
;
3030 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
3031 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
3035 static void show_file_mode_name(FILE *file
, const char *newdelete
, struct diff_filespec
*fs
)
3038 fprintf(file
, " %s mode %06o ", newdelete
, fs
->mode
);
3040 fprintf(file
, " %s ", newdelete
);
3041 write_name_quoted(fs
->path
, file
, '\n');
3045 static void show_mode_change(FILE *file
, struct diff_filepair
*p
, int show_name
)
3047 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
3048 fprintf(file
, " mode change %06o => %06o%c", p
->one
->mode
, p
->two
->mode
,
3049 show_name
? ' ' : '\n');
3051 write_name_quoted(p
->two
->path
, file
, '\n');
3056 static void show_rename_copy(FILE *file
, const char *renamecopy
, struct diff_filepair
*p
)
3058 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
3060 fprintf(file
, " %s %s (%d%%)\n", renamecopy
, names
, similarity_index(p
));
3062 show_mode_change(file
, p
, 0);
3065 static void diff_summary(FILE *file
, struct diff_filepair
*p
)
3068 case DIFF_STATUS_DELETED
:
3069 show_file_mode_name(file
, "delete", p
->one
);
3071 case DIFF_STATUS_ADDED
:
3072 show_file_mode_name(file
, "create", p
->two
);
3074 case DIFF_STATUS_COPIED
:
3075 show_rename_copy(file
, "copy", p
);
3077 case DIFF_STATUS_RENAMED
:
3078 show_rename_copy(file
, "rename", p
);
3082 fputs(" rewrite ", file
);
3083 write_name_quoted(p
->two
->path
, file
, ' ');
3084 fprintf(file
, "(%d%%)\n", similarity_index(p
));
3086 show_mode_change(file
, p
, !p
->score
);
3096 static int remove_space(char *line
, int len
)
3102 for (i
= 0; i
< len
; i
++)
3103 if (!isspace((c
= line
[i
])))
3109 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
3111 struct patch_id_t
*data
= priv
;
3114 /* Ignore line numbers when computing the SHA1 of the patch */
3115 if (!prefixcmp(line
, "@@ -"))
3118 new_len
= remove_space(line
, len
);
3120 SHA1_Update(data
->ctx
, line
, new_len
);
3121 data
->patchlen
+= new_len
;
3124 /* returns 0 upon success, and writes result into sha1 */
3125 static int diff_get_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3127 struct diff_queue_struct
*q
= &diff_queued_diff
;
3130 struct patch_id_t data
;
3131 char buffer
[PATH_MAX
* 4 + 20];
3134 memset(&data
, 0, sizeof(struct patch_id_t
));
3137 for (i
= 0; i
< q
->nr
; i
++) {
3142 struct diff_filepair
*p
= q
->queue
[i
];
3145 memset(&xecfg
, 0, sizeof(xecfg
));
3147 return error("internal diff status error");
3148 if (p
->status
== DIFF_STATUS_UNKNOWN
)
3150 if (diff_unmodified_pair(p
))
3152 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
3153 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
3155 if (DIFF_PAIR_UNMERGED(p
))
3158 diff_fill_sha1_info(p
->one
);
3159 diff_fill_sha1_info(p
->two
);
3160 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
3161 fill_mmfile(&mf2
, p
->two
) < 0)
3162 return error("unable to read files to diff");
3164 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
3165 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
3166 if (p
->one
->mode
== 0)
3167 len1
= snprintf(buffer
, sizeof(buffer
),
3168 "diff--gita/%.*sb/%.*s"
3175 len2
, p
->two
->path
);
3176 else if (p
->two
->mode
== 0)
3177 len1
= snprintf(buffer
, sizeof(buffer
),
3178 "diff--gita/%.*sb/%.*s"
3179 "deletedfilemode%06o"
3185 len1
, p
->one
->path
);
3187 len1
= snprintf(buffer
, sizeof(buffer
),
3188 "diff--gita/%.*sb/%.*s"
3194 len2
, p
->two
->path
);
3195 SHA1_Update(&ctx
, buffer
, len1
);
3197 xpp
.flags
= XDF_NEED_MINIMAL
;
3199 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3200 xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
3201 &xpp
, &xecfg
, &ecb
);
3204 SHA1_Final(sha1
, &ctx
);
3208 int diff_flush_patch_id(struct diff_options
*options
, unsigned char *sha1
)
3210 struct diff_queue_struct
*q
= &diff_queued_diff
;
3212 int result
= diff_get_patch_id(options
, sha1
);
3214 for (i
= 0; i
< q
->nr
; i
++)
3215 diff_free_filepair(q
->queue
[i
]);
3219 q
->nr
= q
->alloc
= 0;
3224 static int is_summary_empty(const struct diff_queue_struct
*q
)
3228 for (i
= 0; i
< q
->nr
; i
++) {
3229 const struct diff_filepair
*p
= q
->queue
[i
];
3231 switch (p
->status
) {
3232 case DIFF_STATUS_DELETED
:
3233 case DIFF_STATUS_ADDED
:
3234 case DIFF_STATUS_COPIED
:
3235 case DIFF_STATUS_RENAMED
:
3240 if (p
->one
->mode
&& p
->two
->mode
&&
3241 p
->one
->mode
!= p
->two
->mode
)
3249 void diff_flush(struct diff_options
*options
)
3251 struct diff_queue_struct
*q
= &diff_queued_diff
;
3252 int i
, output_format
= options
->output_format
;
3256 * Order: raw, stat, summary, patch
3257 * or: name/name-status/checkdiff (other bits clear)
3262 if (output_format
& (DIFF_FORMAT_RAW
|
3264 DIFF_FORMAT_NAME_STATUS
|
3265 DIFF_FORMAT_CHECKDIFF
)) {
3266 for (i
= 0; i
< q
->nr
; i
++) {
3267 struct diff_filepair
*p
= q
->queue
[i
];
3268 if (check_pair_status(p
))
3269 flush_one_pair(p
, options
);
3274 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
)) {
3275 struct diffstat_t diffstat
;
3277 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
3278 for (i
= 0; i
< q
->nr
; i
++) {
3279 struct diff_filepair
*p
= q
->queue
[i
];
3280 if (check_pair_status(p
))
3281 diff_flush_stat(p
, options
, &diffstat
);
3283 if (output_format
& DIFF_FORMAT_NUMSTAT
)
3284 show_numstat(&diffstat
, options
);
3285 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
3286 show_stats(&diffstat
, options
);
3287 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
3288 show_shortstats(&diffstat
, options
);
3289 free_diffstat_info(&diffstat
);
3292 if (output_format
& DIFF_FORMAT_DIRSTAT
)
3293 show_dirstat(options
);
3295 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
3296 for (i
= 0; i
< q
->nr
; i
++)
3297 diff_summary(options
->file
, q
->queue
[i
]);
3301 if (output_format
& DIFF_FORMAT_PATCH
) {
3303 putc(options
->line_termination
, options
->file
);
3304 if (options
->stat_sep
) {
3305 /* attach patch instead of inline */
3306 fputs(options
->stat_sep
, options
->file
);
3310 for (i
= 0; i
< q
->nr
; i
++) {
3311 struct diff_filepair
*p
= q
->queue
[i
];
3312 if (check_pair_status(p
))
3313 diff_flush_patch(p
, options
);
3317 if (output_format
& DIFF_FORMAT_CALLBACK
)
3318 options
->format_callback(q
, options
, options
->format_callback_data
);
3320 for (i
= 0; i
< q
->nr
; i
++)
3321 diff_free_filepair(q
->queue
[i
]);
3325 q
->nr
= q
->alloc
= 0;
3326 if (options
->close_file
)
3327 fclose(options
->file
);
3330 static void diffcore_apply_filter(const char *filter
)
3333 struct diff_queue_struct
*q
= &diff_queued_diff
;
3334 struct diff_queue_struct outq
;
3336 outq
.nr
= outq
.alloc
= 0;
3341 if (strchr(filter
, DIFF_STATUS_FILTER_AON
)) {
3343 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
3344 struct diff_filepair
*p
= q
->queue
[i
];
3345 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3347 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3349 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3350 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3351 strchr(filter
, p
->status
)))
3357 /* otherwise we will clear the whole queue
3358 * by copying the empty outq at the end of this
3359 * function, but first clear the current entries
3362 for (i
= 0; i
< q
->nr
; i
++)
3363 diff_free_filepair(q
->queue
[i
]);
3366 /* Only the matching ones */
3367 for (i
= 0; i
< q
->nr
; i
++) {
3368 struct diff_filepair
*p
= q
->queue
[i
];
3370 if (((p
->status
== DIFF_STATUS_MODIFIED
) &&
3372 strchr(filter
, DIFF_STATUS_FILTER_BROKEN
)) ||
3374 strchr(filter
, DIFF_STATUS_MODIFIED
)))) ||
3375 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
3376 strchr(filter
, p
->status
)))
3379 diff_free_filepair(p
);
3386 /* Check whether two filespecs with the same mode and size are identical */
3387 static int diff_filespec_is_identical(struct diff_filespec
*one
,
3388 struct diff_filespec
*two
)
3390 if (S_ISGITLINK(one
->mode
))
3392 if (diff_populate_filespec(one
, 0))
3394 if (diff_populate_filespec(two
, 0))
3396 return !memcmp(one
->data
, two
->data
, one
->size
);
3399 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
3402 struct diff_queue_struct
*q
= &diff_queued_diff
;
3403 struct diff_queue_struct outq
;
3405 outq
.nr
= outq
.alloc
= 0;
3407 for (i
= 0; i
< q
->nr
; i
++) {
3408 struct diff_filepair
*p
= q
->queue
[i
];
3411 * 1. Entries that come from stat info dirtyness
3412 * always have both sides (iow, not create/delete),
3413 * one side of the object name is unknown, with
3414 * the same mode and size. Keep the ones that
3415 * do not match these criteria. They have real
3418 * 2. At this point, the file is known to be modified,
3419 * with the same mode and size, and the object
3420 * name of one side is unknown. Need to inspect
3421 * the identical contents.
3423 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
3424 !DIFF_FILE_VALID(p
->two
) ||
3425 (p
->one
->sha1_valid
&& p
->two
->sha1_valid
) ||
3426 (p
->one
->mode
!= p
->two
->mode
) ||
3427 diff_populate_filespec(p
->one
, 1) ||
3428 diff_populate_filespec(p
->two
, 1) ||
3429 (p
->one
->size
!= p
->two
->size
) ||
3430 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
3434 * The caller can subtract 1 from skip_stat_unmatch
3435 * to determine how many paths were dirty only
3436 * due to stat info mismatch.
3438 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
3439 diffopt
->skip_stat_unmatch
++;
3440 diff_free_filepair(p
);
3447 void diffcore_std(struct diff_options
*options
)
3449 if (options
->skip_stat_unmatch
)
3450 diffcore_skip_stat_unmatch(options
);
3451 if (options
->break_opt
!= -1)
3452 diffcore_break(options
->break_opt
);
3453 if (options
->detect_rename
)
3454 diffcore_rename(options
);
3455 if (options
->break_opt
!= -1)
3456 diffcore_merge_broken();
3457 if (options
->pickaxe
)
3458 diffcore_pickaxe(options
->pickaxe
, options
->pickaxe_opts
);
3459 if (options
->orderfile
)
3460 diffcore_order(options
->orderfile
);
3461 diff_resolve_rename_copy();
3462 diffcore_apply_filter(options
->filter
);
3464 if (diff_queued_diff
.nr
)
3465 DIFF_OPT_SET(options
, HAS_CHANGES
);
3467 DIFF_OPT_CLR(options
, HAS_CHANGES
);
3470 int diff_result_code(struct diff_options
*opt
, int status
)
3473 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3474 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
3476 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
3477 DIFF_OPT_TST(opt
, HAS_CHANGES
))
3479 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
3480 DIFF_OPT_TST(opt
, CHECK_FAILED
))
3485 void diff_addremove(struct diff_options
*options
,
3486 int addremove
, unsigned mode
,
3487 const unsigned char *sha1
,
3488 const char *concatpath
)
3490 struct diff_filespec
*one
, *two
;
3492 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(mode
))
3495 /* This may look odd, but it is a preparation for
3496 * feeding "there are unchanged files which should
3497 * not produce diffs, but when you are doing copy
3498 * detection you would need them, so here they are"
3499 * entries to the diff-core. They will be prefixed
3500 * with something like '=' or '*' (I haven't decided
3501 * which but should not make any difference).
3502 * Feeding the same new and old to diff_change()
3503 * also has the same effect.
3504 * Before the final output happens, they are pruned after
3505 * merged into rename/copy pairs as appropriate.
3507 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
3508 addremove
= (addremove
== '+' ? '-' :
3509 addremove
== '-' ? '+' : addremove
);
3511 if (options
->prefix
&&
3512 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3515 one
= alloc_filespec(concatpath
);
3516 two
= alloc_filespec(concatpath
);
3518 if (addremove
!= '+')
3519 fill_filespec(one
, sha1
, mode
);
3520 if (addremove
!= '-')
3521 fill_filespec(two
, sha1
, mode
);
3523 diff_queue(&diff_queued_diff
, one
, two
);
3524 DIFF_OPT_SET(options
, HAS_CHANGES
);
3527 void diff_change(struct diff_options
*options
,
3528 unsigned old_mode
, unsigned new_mode
,
3529 const unsigned char *old_sha1
,
3530 const unsigned char *new_sha1
,
3531 const char *concatpath
)
3533 struct diff_filespec
*one
, *two
;
3535 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
) && S_ISGITLINK(old_mode
)
3536 && S_ISGITLINK(new_mode
))
3539 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
3541 const unsigned char *tmp_c
;
3542 tmp
= old_mode
; old_mode
= new_mode
; new_mode
= tmp
;
3543 tmp_c
= old_sha1
; old_sha1
= new_sha1
; new_sha1
= tmp_c
;
3546 if (options
->prefix
&&
3547 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
3550 one
= alloc_filespec(concatpath
);
3551 two
= alloc_filespec(concatpath
);
3552 fill_filespec(one
, old_sha1
, old_mode
);
3553 fill_filespec(two
, new_sha1
, new_mode
);
3555 diff_queue(&diff_queued_diff
, one
, two
);
3556 DIFF_OPT_SET(options
, HAS_CHANGES
);
3559 void diff_unmerge(struct diff_options
*options
,
3561 unsigned mode
, const unsigned char *sha1
)
3563 struct diff_filespec
*one
, *two
;
3565 if (options
->prefix
&&
3566 strncmp(path
, options
->prefix
, options
->prefix_length
))
3569 one
= alloc_filespec(path
);
3570 two
= alloc_filespec(path
);
3571 fill_filespec(one
, sha1
, mode
);
3572 diff_queue(&diff_queued_diff
, one
, two
)->is_unmerged
= 1;